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/videodev2.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-chip-ident.h>
37 #include <media/v4l2-i2c-drv.h>
41 static unsigned int debug;
42 static unsigned int xtal;
43 static unsigned int rbds;
44 static unsigned int plvl;
45 static unsigned int bufblocks = 100;
47 module_param(debug, int, 0644);
48 MODULE_PARM_DESC(debug, "enable debug messages");
49 module_param(xtal, int, 0);
50 MODULE_PARM_DESC(xtal, "select oscillator frequency (0..3), default 0");
51 module_param(rbds, int, 0);
52 MODULE_PARM_DESC(rbds, "select mode, 0=RDS, 1=RBDS, default 0");
53 module_param(plvl, int, 0);
54 MODULE_PARM_DESC(plvl, "select pause level (0..3), default 0");
55 module_param(bufblocks, int, 0);
56 MODULE_PARM_DESC(bufblocks, "number of buffered blocks, default 100");
58 MODULE_DESCRIPTION("v4l2 driver module for SAA6588 RDS decoder");
59 MODULE_AUTHOR("Hans J. Koch <koch@hjk-az.de>");
61 MODULE_LICENSE("GPL");
63 /* ---------------------------------------------------------------------- */
66 #define PREFIX "saa6588: "
67 #define dprintk if (debug) printk
70 struct v4l2_subdev sd;
71 struct delayed_work work;
73 unsigned char *buffer;
74 unsigned int buf_size;
75 unsigned int rd_index;
76 unsigned int wr_index;
77 unsigned int block_count;
78 unsigned char last_blocknum;
79 wait_queue_head_t read_queue;
80 int data_available_for_read;
83 static inline struct saa6588 *to_saa6588(struct v4l2_subdev *sd)
85 return container_of(sd, struct saa6588, sd);
88 /* ---------------------------------------------------------------------- */
94 /* Initialization and mode control byte (0w) */
96 /* bit 0+1 (DAC0/DAC1) */
97 #define cModeStandard 0x00
98 #define cModeFastPI 0x01
99 #define cModeReducedRequest 0x02
100 #define cModeInvalid 0x03
103 #define cProcessingModeRDS 0x00
104 #define cProcessingModeRBDS 0x04
106 /* bit 3+4 (SYM0/SYM1) */
107 #define cErrCorrectionNone 0x00
108 #define cErrCorrection2Bits 0x08
109 #define cErrCorrection5Bits 0x10
110 #define cErrCorrectionNoneRBDS 0x18
113 #define cSyncNormal 0x00
114 #define cSyncRestart 0x20
117 #define cSigQualityDetectOFF 0x00
118 #define cSigQualityDetectON 0x40
121 #define cSigQualityTriggered 0x00
122 #define cSigQualityContinous 0x80
124 /* Pause level and flywheel control byte (1w) */
126 /* bits 0..5 (FEB0..FEB5) */
127 #define cFlywheelMaxBlocksMask 0x3F
128 #define cFlywheelDefault 0x20
130 /* bits 6+7 (PL0/PL1) */
131 #define cPauseLevel_11mV 0x00
132 #define cPauseLevel_17mV 0x40
133 #define cPauseLevel_27mV 0x80
134 #define cPauseLevel_43mV 0xC0
136 /* Pause time/oscillator frequency/quality detector control byte (1w) */
138 /* bits 0..4 (SQS0..SQS4) */
139 #define cQualityDetectSensMask 0x1F
140 #define cQualityDetectDefault 0x0F
143 #define cSelectOscFreqOFF 0x00
144 #define cSelectOscFreqON 0x20
146 /* bit 6+7 (PTF0/PTF1) */
147 #define cOscFreq_4332kHz 0x00
148 #define cOscFreq_8664kHz 0x40
149 #define cOscFreq_12996kHz 0x80
150 #define cOscFreq_17328kHz 0xC0
152 /* ---------------------------------------------------------------------- */
154 static int block_to_user_buf(struct saa6588 *s, unsigned char __user *user_buf)
158 if (s->rd_index == s->wr_index) {
160 dprintk(PREFIX "Read: buffer empty.\n");
165 dprintk(PREFIX "Read: ");
166 for (i = s->rd_index; i < s->rd_index + 3; i++)
167 dprintk("0x%02x ", s->buffer[i]);
170 if (copy_to_user(user_buf, &s->buffer[s->rd_index], 3))
174 if (s->rd_index >= s->buf_size)
179 dprintk("%d blocks total.\n", s->block_count);
184 static void read_from_buf(struct saa6588 *s, struct rds_command *a)
188 unsigned char __user *buf_ptr = a->buffer;
190 unsigned int rd_blocks;
196 while (!s->data_available_for_read) {
197 int ret = wait_event_interruptible(s->read_queue,
198 s->data_available_for_read);
199 if (ret == -ERESTARTSYS) {
205 spin_lock_irqsave(&s->lock, flags);
206 rd_blocks = a->block_count;
207 if (rd_blocks > s->block_count)
208 rd_blocks = s->block_count;
211 spin_unlock_irqrestore(&s->lock, flags);
215 for (i = 0; i < rd_blocks; i++) {
216 if (block_to_user_buf(s, buf_ptr)) {
223 s->data_available_for_read = (s->block_count > 0);
224 spin_unlock_irqrestore(&s->lock, flags);
227 static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf)
232 dprintk(PREFIX "New block: ");
234 for (i = 0; i < 3; ++i) {
236 dprintk("0x%02x ", blockbuf[i]);
237 s->buffer[s->wr_index] = blockbuf[i];
241 if (s->wr_index >= s->buf_size)
244 if (s->wr_index == s->rd_index) {
246 if (s->rd_index >= s->buf_size)
252 dprintk("%d blocks total.\n", s->block_count);
255 static void saa6588_i2c_poll(struct saa6588 *s)
257 struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
259 unsigned char tmpbuf[6];
260 unsigned char blocknum;
263 /* Although we only need 3 bytes, we have to read at least 6.
264 SAA6588 returns garbage otherwise */
265 if (6 != i2c_master_recv(client, &tmpbuf[0], 6)) {
267 dprintk(PREFIX "read error!\n");
271 blocknum = tmpbuf[0] >> 5;
272 if (blocknum == s->last_blocknum) {
274 dprintk("Saw block %d again.\n", blocknum);
278 s->last_blocknum = blocknum;
281 Byte order according to v4l2 specification:
283 Byte 0: Least Significant Byte of RDS Block
284 Byte 1: Most Significant Byte of RDS Block
285 Byte 2 Bit 7: Error bit. Indicates that an uncorrectable error
286 occurred during reception of this block.
287 Bit 6: Corrected bit. Indicates that an error was
288 corrected for this data block.
289 Bits 5-3: Received Offset. Indicates the offset received
291 Bits 2-0: Offset Name. Indicates the offset applied to this data.
293 SAA6588 byte order is Status-MSB-LSB, so we have to swap the
294 first and the last of the 3 bytes block.
298 tmpbuf[2] = tmpbuf[0];
302 tmp |= blocknum << 3; /* Received offset == Offset Name (OK ?) */
303 if ((tmpbuf[2] & 0x03) == 0x03)
304 tmp |= 0x80; /* uncorrectable error */
305 else if ((tmpbuf[2] & 0x03) != 0x00)
306 tmp |= 0x40; /* corrected error */
307 tmpbuf[2] = tmp; /* Is this enough ? Should we also check other bits ? */
309 spin_lock_irqsave(&s->lock, flags);
310 block_to_buf(s, tmpbuf);
311 spin_unlock_irqrestore(&s->lock, flags);
312 s->data_available_for_read = 1;
313 wake_up_interruptible(&s->read_queue);
316 static void saa6588_work(struct work_struct *work)
318 struct saa6588 *s = container_of(work, struct saa6588, work.work);
321 schedule_delayed_work(&s->work, msecs_to_jiffies(20));
324 static int saa6588_configure(struct saa6588 *s)
326 struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
327 unsigned char buf[3];
330 buf[0] = cSyncRestart;
332 buf[0] |= cProcessingModeRBDS;
334 buf[1] = cFlywheelDefault;
337 buf[1] |= cPauseLevel_11mV;
340 buf[1] |= cPauseLevel_17mV;
343 buf[1] |= cPauseLevel_27mV;
346 buf[1] |= cPauseLevel_43mV;
348 default: /* nothing */
352 buf[2] = cQualityDetectDefault | cSelectOscFreqON;
356 buf[2] |= cOscFreq_4332kHz;
359 buf[2] |= cOscFreq_8664kHz;
362 buf[2] |= cOscFreq_12996kHz;
365 buf[2] |= cOscFreq_17328kHz;
367 default: /* nothing */
371 dprintk(PREFIX "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n",
372 buf[0], buf[1], buf[2]);
374 rc = i2c_master_send(client, buf, 3);
376 printk(PREFIX "i2c i/o error: rc == %d (should be 3)\n", rc);
381 /* ---------------------------------------------------------------------- */
383 static long saa6588_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
385 struct saa6588 *s = to_saa6588(sd);
386 struct rds_command *a = arg;
389 /* --- open() for /dev/radio --- */
391 a->result = 0; /* return error if chip doesn't work ??? */
393 /* --- close() for /dev/radio --- */
395 s->data_available_for_read = 1;
396 wake_up_interruptible(&s->read_queue);
399 /* --- read() for /dev/radio --- */
403 /* --- poll() for /dev/radio --- */
406 if (s->data_available_for_read) {
407 a->result |= POLLIN | POLLRDNORM;
409 poll_wait(a->instance, &s->read_queue, a->event_list);
419 static int saa6588_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
421 struct i2c_client *client = v4l2_get_subdevdata(sd);
423 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA6588, 0);
426 /* ----------------------------------------------------------------------- */
428 static const struct v4l2_subdev_core_ops saa6588_core_ops = {
429 .g_chip_ident = saa6588_g_chip_ident,
430 .ioctl = saa6588_ioctl,
433 static const struct v4l2_subdev_ops saa6588_ops = {
434 .core = &saa6588_core_ops,
437 /* ---------------------------------------------------------------------- */
439 static int saa6588_probe(struct i2c_client *client,
440 const struct i2c_device_id *id)
443 struct v4l2_subdev *sd;
445 v4l_info(client, "saa6588 found @ 0x%x (%s)\n",
446 client->addr << 1, client->adapter->name);
448 s = kzalloc(sizeof(*s), GFP_KERNEL);
452 s->buf_size = bufblocks * 3;
454 s->buffer = kmalloc(s->buf_size, GFP_KERNEL);
455 if (s->buffer == NULL) {
460 v4l2_i2c_subdev_init(sd, client, &saa6588_ops);
461 spin_lock_init(&s->lock);
465 s->last_blocknum = 0xff;
466 init_waitqueue_head(&s->read_queue);
467 s->data_available_for_read = 0;
469 saa6588_configure(s);
471 /* start polling via eventd */
472 INIT_DELAYED_WORK(&s->work, saa6588_work);
473 schedule_delayed_work(&s->work, 0);
477 static int saa6588_remove(struct i2c_client *client)
479 struct v4l2_subdev *sd = i2c_get_clientdata(client);
480 struct saa6588 *s = to_saa6588(sd);
482 v4l2_device_unregister_subdev(sd);
484 cancel_delayed_work_sync(&s->work);
491 /* ----------------------------------------------------------------------- */
493 static const struct i2c_device_id saa6588_id[] = {
497 MODULE_DEVICE_TABLE(i2c, saa6588_id);
499 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
501 .probe = saa6588_probe,
502 .remove = saa6588_remove,
503 .id_table = saa6588_id,