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>
38 /* Addresses to scan */
39 static unsigned short normal_i2c[] = {
48 static unsigned int debug = 0;
49 static unsigned int xtal = 0;
50 static unsigned int rbds = 0;
51 static unsigned int plvl = 0;
52 static unsigned int bufblocks = 100;
54 MODULE_PARM(debug, "i");
55 MODULE_PARM_DESC(debug, "enable debug messages");
56 MODULE_PARM(xtal, "i");
57 MODULE_PARM_DESC(xtal, "select oscillator frequency (0..3), default 0");
58 MODULE_PARM(rbds, "i");
59 MODULE_PARM_DESC(rbds, "select mode, 0=RDS, 1=RBDS, default 0");
60 MODULE_PARM(plvl, "i");
61 MODULE_PARM_DESC(plvl, "select pause level (0..3), default 0");
62 MODULE_PARM(bufblocks, "i");
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 i2c_client client;
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 struct i2c_driver driver;
92 static struct i2c_client client_template;
94 /* ---------------------------------------------------------------------- */
100 /* Initialization and mode control byte (0w) */
102 /* bit 0+1 (DAC0/DAC1) */
103 #define cModeStandard 0x00
104 #define cModeFastPI 0x01
105 #define cModeReducedRequest 0x02
106 #define cModeInvalid 0x03
109 #define cProcessingModeRDS 0x00
110 #define cProcessingModeRBDS 0x04
112 /* bit 3+4 (SYM0/SYM1) */
113 #define cErrCorrectionNone 0x00
114 #define cErrCorrection2Bits 0x08
115 #define cErrCorrection5Bits 0x10
116 #define cErrCorrectionNoneRBDS 0x18
119 #define cSyncNormal 0x00
120 #define cSyncRestart 0x20
123 #define cSigQualityDetectOFF 0x00
124 #define cSigQualityDetectON 0x40
127 #define cSigQualityTriggered 0x00
128 #define cSigQualityContinous 0x80
130 /* Pause level and flywheel control byte (1w) */
132 /* bits 0..5 (FEB0..FEB5) */
133 #define cFlywheelMaxBlocksMask 0x3F
134 #define cFlywheelDefault 0x20
136 /* bits 6+7 (PL0/PL1) */
137 #define cPauseLevel_11mV 0x00
138 #define cPauseLevel_17mV 0x40
139 #define cPauseLevel_27mV 0x80
140 #define cPauseLevel_43mV 0xC0
142 /* Pause time/oscillator frequency/quality detector control byte (1w) */
144 /* bits 0..4 (SQS0..SQS4) */
145 #define cQualityDetectSensMask 0x1F
146 #define cQualityDetectDefault 0x0F
149 #define cSelectOscFreqOFF 0x00
150 #define cSelectOscFreqON 0x20
152 /* bit 6+7 (PTF0/PTF1) */
153 #define cOscFreq_4332kHz 0x00
154 #define cOscFreq_8664kHz 0x40
155 #define cOscFreq_12996kHz 0x80
156 #define cOscFreq_17328kHz 0xC0
158 /* ---------------------------------------------------------------------- */
160 static int block_to_user_buf(struct saa6588 *s, unsigned char __user *user_buf)
164 if (s->rd_index == s->wr_index) {
166 dprintk(PREFIX "Read: buffer empty.\n");
171 dprintk(PREFIX "Read: ");
172 for (i = s->rd_index; i < s->rd_index + 3; i++)
173 dprintk("0x%02x ", s->buffer[i]);
176 if (copy_to_user(user_buf, &s->buffer[s->rd_index], 3))
180 if (s->rd_index >= s->buf_size)
185 dprintk("%d blocks total.\n", s->block_count);
190 static void read_from_buf(struct saa6588 *s, struct rds_command *a)
194 unsigned char __user *buf_ptr = a->buffer;
196 unsigned int rd_blocks;
202 while (!s->data_available_for_read) {
203 int ret = wait_event_interruptible(s->read_queue,
204 s->data_available_for_read);
205 if (ret == -ERESTARTSYS) {
211 spin_lock_irqsave(&s->lock, flags);
212 rd_blocks = a->block_count;
213 if (rd_blocks > s->block_count)
214 rd_blocks = s->block_count;
219 for (i = 0; i < rd_blocks; i++) {
220 if (block_to_user_buf(s, buf_ptr)) {
227 s->data_available_for_read = (s->block_count > 0);
228 spin_unlock_irqrestore(&s->lock, flags);
231 static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf)
236 dprintk(PREFIX "New block: ");
238 for (i = 0; i < 3; ++i) {
240 dprintk("0x%02x ", blockbuf[i]);
241 s->buffer[s->wr_index] = blockbuf[i];
245 if (s->wr_index >= s->buf_size)
248 if (s->wr_index == s->rd_index) {
250 if (s->rd_index >= s->buf_size)
256 dprintk("%d blocks total.\n", s->block_count);
259 static void saa6588_i2c_poll(struct saa6588 *s)
262 unsigned char tmpbuf[6];
263 unsigned char blocknum;
266 /* Although we only need 3 bytes, we have to read at least 6.
267 SAA6588 returns garbage otherwise */
268 if (6 != i2c_master_recv(&s->client, &tmpbuf[0], 6)) {
270 dprintk(PREFIX "read error!\n");
274 blocknum = tmpbuf[0] >> 5;
275 if (blocknum == s->last_blocknum) {
277 dprintk("Saw block %d again.\n", blocknum);
281 s->last_blocknum = blocknum;
284 Byte order according to v4l2 specification:
286 Byte 0: Least Significant Byte of RDS Block
287 Byte 1: Most Significant Byte of RDS Block
288 Byte 2 Bit 7: Error bit. Indicates that an uncorrectable error
289 occurred during reception of this block.
290 Bit 6: Corrected bit. Indicates that an error was
291 corrected for this data block.
292 Bits 5-3: Received Offset. Indicates the offset received
294 Bits 2-0: Offset Name. Indicates the offset applied to this data.
296 SAA6588 byte order is Status-MSB-LSB, so we have to swap the
297 first and the last of the 3 bytes block.
301 tmpbuf[2] = tmpbuf[0];
305 tmp |= blocknum << 3; /* Received offset == Offset Name (OK ?) */
306 if ((tmpbuf[2] & 0x03) == 0x03)
307 tmp |= 0x80; /* uncorrectable error */
308 else if ((tmpbuf[2] & 0x03) != 0x00)
309 tmp |= 0x40; /* corrected error */
310 tmpbuf[2] = tmp; /* Is this enough ? Should we also check other bits ? */
312 spin_lock_irqsave(&s->lock, flags);
313 block_to_buf(s, tmpbuf);
314 spin_unlock_irqrestore(&s->lock, flags);
315 s->data_available_for_read = 1;
316 wake_up_interruptible(&s->read_queue);
319 static void saa6588_timer(unsigned long data)
321 struct saa6588 *s = (struct saa6588 *)data;
323 schedule_work(&s->work);
326 static void saa6588_work(void *data)
328 struct saa6588 *s = (struct saa6588 *)data;
331 mod_timer(&s->timer, jiffies + HZ / 50); /* 20 msec */
334 static int saa6588_configure(struct saa6588 *s)
336 unsigned char buf[3];
339 buf[0] = cSyncRestart;
341 buf[0] |= cProcessingModeRBDS;
343 buf[1] = cFlywheelDefault;
346 buf[1] |= cPauseLevel_11mV;
349 buf[1] |= cPauseLevel_17mV;
352 buf[1] |= cPauseLevel_27mV;
355 buf[1] |= cPauseLevel_43mV;
357 default: /* nothing */
361 buf[2] = cQualityDetectDefault | cSelectOscFreqON;
365 buf[2] |= cOscFreq_4332kHz;
368 buf[2] |= cOscFreq_8664kHz;
371 buf[2] |= cOscFreq_12996kHz;
374 buf[2] |= cOscFreq_17328kHz;
376 default: /* nothing */
380 dprintk(PREFIX "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n",
381 buf[0], buf[1], buf[2]);
383 if (3 != (rc = i2c_master_send(&s->client, buf, 3)))
384 printk(PREFIX "i2c i/o error: rc == %d (should be 3)\n", rc);
389 /* ---------------------------------------------------------------------- */
391 static int saa6588_attach(struct i2c_adapter *adap, int addr, int kind)
394 client_template.adapter = adap;
395 client_template.addr = addr;
397 printk(PREFIX "chip found @ 0x%x\n", addr << 1);
399 if (NULL == (s = kmalloc(sizeof(*s), GFP_KERNEL)))
402 s->buf_size = bufblocks * 3;
404 if (NULL == (s->buffer = kmalloc(s->buf_size, GFP_KERNEL))) {
408 s->client = client_template;
412 s->last_blocknum = 0xff;
413 init_waitqueue_head(&s->read_queue);
414 s->data_available_for_read = 0;
415 i2c_set_clientdata(&s->client, s);
416 i2c_attach_client(&s->client);
418 saa6588_configure(s);
420 /* start polling via eventd */
421 INIT_WORK(&s->work, saa6588_work, s);
422 init_timer(&s->timer);
423 s->timer.function = saa6588_timer;
424 s->timer.data = (unsigned long)s;
425 schedule_work(&s->work);
430 static int saa6588_probe(struct i2c_adapter *adap)
432 #ifdef I2C_CLASS_TV_ANALOG
433 if (adap->class & I2C_CLASS_TV_ANALOG)
434 return i2c_probe(adap, &addr_data, saa6588_attach);
437 case I2C_ALGO_BIT | I2C_HW_B_BT848:
438 case I2C_ALGO_BIT | I2C_HW_B_RIVA:
439 case I2C_ALGO_SAA7134:
440 return i2c_probe(adap, &addr_data, saa6588_attach);
447 static int saa6588_detach(struct i2c_client *client)
449 struct saa6588 *s = i2c_get_clientdata(client);
451 del_timer_sync(&s->timer);
452 flush_scheduled_work();
454 i2c_detach_client(client);
460 static int saa6588_command(struct i2c_client *client, unsigned int cmd,
463 struct saa6588 *s = i2c_get_clientdata(client);
464 struct rds_command *a = (struct rds_command *)arg;
467 /* --- open() for /dev/radio --- */
469 a->result = 0; /* return error if chip doesn't work ??? */
471 /* --- close() for /dev/radio --- */
473 s->data_available_for_read = 1;
474 wake_up_interruptible(&s->read_queue);
477 /* --- read() for /dev/radio --- */
481 /* --- poll() for /dev/radio --- */
484 if (s->data_available_for_read) {
485 a->result |= POLLIN | POLLRDNORM;
487 poll_wait(a->instance, &s->read_queue, a->event_list);
497 /* ----------------------------------------------------------------------- */
499 static struct i2c_driver driver = {
500 .owner = THIS_MODULE,
501 .name = "i2c saa6588 driver",
502 .id = -1, /* FIXME */
503 .flags = I2C_DF_NOTIFY,
504 .attach_adapter = saa6588_probe,
505 .detach_client = saa6588_detach,
506 .command = saa6588_command,
509 static struct i2c_client client_template = {
511 .flags = I2C_CLIENT_ALLOW_USE,
515 static int __init saa6588_init_module(void)
517 return i2c_add_driver(&driver);
520 static void __exit saa6588_cleanup_module(void)
522 i2c_del_driver(&driver);
525 module_init(saa6588_init_module);
526 module_exit(saa6588_cleanup_module);
529 * Overrides for Emacs so that we follow Linus's tabbing style.
530 * ---------------------------------------------------------------------------