2 Driver for SAA6588 RDS decoder
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/i2c.h>
25 #include <linux/types.h>
26 #include <linux/videodev.h>
27 #include <linux/init.h>
28 #include <linux/errno.h>
29 #include <linux/slab.h>
30 #include <linux/poll.h>
31 #include <linux/wait.h>
32 #include <asm/uaccess.h>
34 #include <media/rds.h>
35 #include <media/v4l2-device.h>
36 #include <media/v4l2-i2c-drv-legacy.h>
38 /* Addresses to scan */
39 static unsigned short normal_i2c[] = {
48 static unsigned int debug;
49 static unsigned int xtal;
50 static unsigned int rbds;
51 static unsigned int plvl;
52 static unsigned int bufblocks = 100;
54 module_param(debug, int, 0644);
55 MODULE_PARM_DESC(debug, "enable debug messages");
56 module_param(xtal, int, 0);
57 MODULE_PARM_DESC(xtal, "select oscillator frequency (0..3), default 0");
58 module_param(rbds, int, 0);
59 MODULE_PARM_DESC(rbds, "select mode, 0=RDS, 1=RBDS, default 0");
60 module_param(plvl, int, 0);
61 MODULE_PARM_DESC(plvl, "select pause level (0..3), default 0");
62 module_param(bufblocks, int, 0);
63 MODULE_PARM_DESC(bufblocks, "number of buffered blocks, default 100");
65 MODULE_DESCRIPTION("v4l2 driver module for SAA6588 RDS decoder");
66 MODULE_AUTHOR("Hans J. Koch <koch@hjk-az.de>");
68 MODULE_LICENSE("GPL");
70 /* ---------------------------------------------------------------------- */
73 #define PREFIX "saa6588: "
74 #define dprintk if (debug) printk
77 struct v4l2_subdev sd;
78 struct work_struct work;
79 struct timer_list timer;
81 unsigned char *buffer;
82 unsigned int buf_size;
83 unsigned int rd_index;
84 unsigned int wr_index;
85 unsigned int block_count;
86 unsigned char last_blocknum;
87 wait_queue_head_t read_queue;
88 int data_available_for_read;
91 static inline struct saa6588 *to_saa6588(struct v4l2_subdev *sd)
93 return container_of(sd, struct saa6588, sd);
96 /* ---------------------------------------------------------------------- */
102 /* Initialization and mode control byte (0w) */
104 /* bit 0+1 (DAC0/DAC1) */
105 #define cModeStandard 0x00
106 #define cModeFastPI 0x01
107 #define cModeReducedRequest 0x02
108 #define cModeInvalid 0x03
111 #define cProcessingModeRDS 0x00
112 #define cProcessingModeRBDS 0x04
114 /* bit 3+4 (SYM0/SYM1) */
115 #define cErrCorrectionNone 0x00
116 #define cErrCorrection2Bits 0x08
117 #define cErrCorrection5Bits 0x10
118 #define cErrCorrectionNoneRBDS 0x18
121 #define cSyncNormal 0x00
122 #define cSyncRestart 0x20
125 #define cSigQualityDetectOFF 0x00
126 #define cSigQualityDetectON 0x40
129 #define cSigQualityTriggered 0x00
130 #define cSigQualityContinous 0x80
132 /* Pause level and flywheel control byte (1w) */
134 /* bits 0..5 (FEB0..FEB5) */
135 #define cFlywheelMaxBlocksMask 0x3F
136 #define cFlywheelDefault 0x20
138 /* bits 6+7 (PL0/PL1) */
139 #define cPauseLevel_11mV 0x00
140 #define cPauseLevel_17mV 0x40
141 #define cPauseLevel_27mV 0x80
142 #define cPauseLevel_43mV 0xC0
144 /* Pause time/oscillator frequency/quality detector control byte (1w) */
146 /* bits 0..4 (SQS0..SQS4) */
147 #define cQualityDetectSensMask 0x1F
148 #define cQualityDetectDefault 0x0F
151 #define cSelectOscFreqOFF 0x00
152 #define cSelectOscFreqON 0x20
154 /* bit 6+7 (PTF0/PTF1) */
155 #define cOscFreq_4332kHz 0x00
156 #define cOscFreq_8664kHz 0x40
157 #define cOscFreq_12996kHz 0x80
158 #define cOscFreq_17328kHz 0xC0
160 /* ---------------------------------------------------------------------- */
162 static int block_to_user_buf(struct saa6588 *s, unsigned char __user *user_buf)
166 if (s->rd_index == s->wr_index) {
168 dprintk(PREFIX "Read: buffer empty.\n");
173 dprintk(PREFIX "Read: ");
174 for (i = s->rd_index; i < s->rd_index + 3; i++)
175 dprintk("0x%02x ", s->buffer[i]);
178 if (copy_to_user(user_buf, &s->buffer[s->rd_index], 3))
182 if (s->rd_index >= s->buf_size)
187 dprintk("%d blocks total.\n", s->block_count);
192 static void read_from_buf(struct saa6588 *s, struct rds_command *a)
196 unsigned char __user *buf_ptr = a->buffer;
198 unsigned int rd_blocks;
204 while (!s->data_available_for_read) {
205 int ret = wait_event_interruptible(s->read_queue,
206 s->data_available_for_read);
207 if (ret == -ERESTARTSYS) {
213 spin_lock_irqsave(&s->lock, flags);
214 rd_blocks = a->block_count;
215 if (rd_blocks > s->block_count)
216 rd_blocks = s->block_count;
219 spin_unlock_irqrestore(&s->lock, flags);
223 for (i = 0; i < rd_blocks; i++) {
224 if (block_to_user_buf(s, buf_ptr)) {
231 s->data_available_for_read = (s->block_count > 0);
232 spin_unlock_irqrestore(&s->lock, flags);
235 static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf)
240 dprintk(PREFIX "New block: ");
242 for (i = 0; i < 3; ++i) {
244 dprintk("0x%02x ", blockbuf[i]);
245 s->buffer[s->wr_index] = blockbuf[i];
249 if (s->wr_index >= s->buf_size)
252 if (s->wr_index == s->rd_index) {
254 if (s->rd_index >= s->buf_size)
260 dprintk("%d blocks total.\n", s->block_count);
263 static void saa6588_i2c_poll(struct saa6588 *s)
265 struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
267 unsigned char tmpbuf[6];
268 unsigned char blocknum;
271 /* Although we only need 3 bytes, we have to read at least 6.
272 SAA6588 returns garbage otherwise */
273 if (6 != i2c_master_recv(client, &tmpbuf[0], 6)) {
275 dprintk(PREFIX "read error!\n");
279 blocknum = tmpbuf[0] >> 5;
280 if (blocknum == s->last_blocknum) {
282 dprintk("Saw block %d again.\n", blocknum);
286 s->last_blocknum = blocknum;
289 Byte order according to v4l2 specification:
291 Byte 0: Least Significant Byte of RDS Block
292 Byte 1: Most Significant Byte of RDS Block
293 Byte 2 Bit 7: Error bit. Indicates that an uncorrectable error
294 occurred during reception of this block.
295 Bit 6: Corrected bit. Indicates that an error was
296 corrected for this data block.
297 Bits 5-3: Received Offset. Indicates the offset received
299 Bits 2-0: Offset Name. Indicates the offset applied to this data.
301 SAA6588 byte order is Status-MSB-LSB, so we have to swap the
302 first and the last of the 3 bytes block.
306 tmpbuf[2] = tmpbuf[0];
310 tmp |= blocknum << 3; /* Received offset == Offset Name (OK ?) */
311 if ((tmpbuf[2] & 0x03) == 0x03)
312 tmp |= 0x80; /* uncorrectable error */
313 else if ((tmpbuf[2] & 0x03) != 0x00)
314 tmp |= 0x40; /* corrected error */
315 tmpbuf[2] = tmp; /* Is this enough ? Should we also check other bits ? */
317 spin_lock_irqsave(&s->lock, flags);
318 block_to_buf(s, tmpbuf);
319 spin_unlock_irqrestore(&s->lock, flags);
320 s->data_available_for_read = 1;
321 wake_up_interruptible(&s->read_queue);
324 static void saa6588_timer(unsigned long data)
326 struct saa6588 *s = (struct saa6588 *)data;
328 schedule_work(&s->work);
331 static void saa6588_work(struct work_struct *work)
333 struct saa6588 *s = container_of(work, struct saa6588, work);
336 mod_timer(&s->timer, jiffies + msecs_to_jiffies(20));
339 static int saa6588_configure(struct saa6588 *s)
341 struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
342 unsigned char buf[3];
345 buf[0] = cSyncRestart;
347 buf[0] |= cProcessingModeRBDS;
349 buf[1] = cFlywheelDefault;
352 buf[1] |= cPauseLevel_11mV;
355 buf[1] |= cPauseLevel_17mV;
358 buf[1] |= cPauseLevel_27mV;
361 buf[1] |= cPauseLevel_43mV;
363 default: /* nothing */
367 buf[2] = cQualityDetectDefault | cSelectOscFreqON;
371 buf[2] |= cOscFreq_4332kHz;
374 buf[2] |= cOscFreq_8664kHz;
377 buf[2] |= cOscFreq_12996kHz;
380 buf[2] |= cOscFreq_17328kHz;
382 default: /* nothing */
386 dprintk(PREFIX "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n",
387 buf[0], buf[1], buf[2]);
389 rc = i2c_master_send(client, buf, 3);
391 printk(PREFIX "i2c i/o error: rc == %d (should be 3)\n", rc);
396 /* ---------------------------------------------------------------------- */
398 static long saa6588_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
400 struct saa6588 *s = to_saa6588(sd);
401 struct rds_command *a = arg;
404 /* --- open() for /dev/radio --- */
406 a->result = 0; /* return error if chip doesn't work ??? */
408 /* --- close() for /dev/radio --- */
410 s->data_available_for_read = 1;
411 wake_up_interruptible(&s->read_queue);
414 /* --- read() for /dev/radio --- */
418 /* --- poll() for /dev/radio --- */
421 if (s->data_available_for_read) {
422 a->result |= POLLIN | POLLRDNORM;
424 poll_wait(a->instance, &s->read_queue, a->event_list);
434 static int saa6588_command(struct i2c_client *client, unsigned cmd, void *arg)
436 return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
439 /* ----------------------------------------------------------------------- */
441 static const struct v4l2_subdev_core_ops saa6588_core_ops = {
442 .ioctl = saa6588_ioctl,
445 static const struct v4l2_subdev_ops saa6588_ops = {
446 .core = &saa6588_core_ops,
449 /* ---------------------------------------------------------------------- */
451 static int saa6588_probe(struct i2c_client *client,
452 const struct i2c_device_id *id)
455 struct v4l2_subdev *sd;
457 v4l_info(client, "saa6588 found @ 0x%x (%s)\n",
458 client->addr << 1, client->adapter->name);
460 s = kzalloc(sizeof(*s), GFP_KERNEL);
464 s->buf_size = bufblocks * 3;
466 s->buffer = kmalloc(s->buf_size, GFP_KERNEL);
467 if (s->buffer == NULL) {
472 v4l2_i2c_subdev_init(sd, client, &saa6588_ops);
473 spin_lock_init(&s->lock);
477 s->last_blocknum = 0xff;
478 init_waitqueue_head(&s->read_queue);
479 s->data_available_for_read = 0;
481 saa6588_configure(s);
483 /* start polling via eventd */
484 INIT_WORK(&s->work, saa6588_work);
485 init_timer(&s->timer);
486 s->timer.function = saa6588_timer;
487 s->timer.data = (unsigned long)s;
488 schedule_work(&s->work);
492 static int saa6588_remove(struct i2c_client *client)
494 struct v4l2_subdev *sd = i2c_get_clientdata(client);
495 struct saa6588 *s = to_saa6588(sd);
497 v4l2_device_unregister_subdev(sd);
499 del_timer_sync(&s->timer);
500 flush_scheduled_work();
507 /* ----------------------------------------------------------------------- */
509 static const struct i2c_device_id saa6588_id[] = {
513 MODULE_DEVICE_TABLE(i2c, saa6588_id);
515 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
517 .command = saa6588_command,
518 .probe = saa6588_probe,
519 .remove = saa6588_remove,
520 .legacy_class = I2C_CLASS_TV_ANALOG,
521 .id_table = saa6588_id,