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>
37 /* Addresses to scan */
38 static unsigned short normal_i2c[] = {
47 static unsigned int debug = 0;
48 static unsigned int xtal = 0;
49 static unsigned int rbds = 0;
50 static unsigned int plvl = 0;
51 static unsigned int bufblocks = 100;
53 MODULE_PARM(debug, "i");
54 MODULE_PARM_DESC(debug, "enable debug messages");
55 MODULE_PARM(xtal, "i");
56 MODULE_PARM_DESC(xtal, "select oscillator frequency (0..3), default 0");
57 MODULE_PARM(rbds, "i");
58 MODULE_PARM_DESC(rbds, "select mode, 0=RDS, 1=RBDS, default 0");
59 MODULE_PARM(plvl, "i");
60 MODULE_PARM_DESC(plvl, "select pause level (0..3), default 0");
61 MODULE_PARM(bufblocks, "i");
62 MODULE_PARM_DESC(bufblocks, "number of buffered blocks, default 100");
64 MODULE_DESCRIPTION("v4l2 driver module for SAA6588 RDS decoder");
65 MODULE_AUTHOR("Hans J. Koch <koch@hjk-az.de>");
67 MODULE_LICENSE("GPL");
69 /* ---------------------------------------------------------------------- */
72 #define PREFIX "saa6588: "
73 #define dprintk if (debug) printk
76 struct i2c_client client;
77 struct work_struct work;
78 struct timer_list timer;
80 unsigned char *buffer;
81 unsigned int buf_size;
82 unsigned int rd_index;
83 unsigned int wr_index;
84 unsigned int block_count;
85 unsigned char last_blocknum;
86 wait_queue_head_t read_queue;
87 int data_available_for_read;
90 static struct i2c_driver driver;
91 static struct i2c_client client_template;
93 /* ---------------------------------------------------------------------- */
99 /* Initialization and mode control byte (0w) */
101 /* bit 0+1 (DAC0/DAC1) */
102 #define cModeStandard 0x00
103 #define cModeFastPI 0x01
104 #define cModeReducedRequest 0x02
105 #define cModeInvalid 0x03
108 #define cProcessingModeRDS 0x00
109 #define cProcessingModeRBDS 0x04
111 /* bit 3+4 (SYM0/SYM1) */
112 #define cErrCorrectionNone 0x00
113 #define cErrCorrection2Bits 0x08
114 #define cErrCorrection5Bits 0x10
115 #define cErrCorrectionNoneRBDS 0x18
118 #define cSyncNormal 0x00
119 #define cSyncRestart 0x20
122 #define cSigQualityDetectOFF 0x00
123 #define cSigQualityDetectON 0x40
126 #define cSigQualityTriggered 0x00
127 #define cSigQualityContinous 0x80
129 /* Pause level and flywheel control byte (1w) */
131 /* bits 0..5 (FEB0..FEB5) */
132 #define cFlywheelMaxBlocksMask 0x3F
133 #define cFlywheelDefault 0x20
135 /* bits 6+7 (PL0/PL1) */
136 #define cPauseLevel_11mV 0x00
137 #define cPauseLevel_17mV 0x40
138 #define cPauseLevel_27mV 0x80
139 #define cPauseLevel_43mV 0xC0
141 /* Pause time/oscillator frequency/quality detector control byte (1w) */
143 /* bits 0..4 (SQS0..SQS4) */
144 #define cQualityDetectSensMask 0x1F
145 #define cQualityDetectDefault 0x0F
148 #define cSelectOscFreqOFF 0x00
149 #define cSelectOscFreqON 0x20
151 /* bit 6+7 (PTF0/PTF1) */
152 #define cOscFreq_4332kHz 0x00
153 #define cOscFreq_8664kHz 0x40
154 #define cOscFreq_12996kHz 0x80
155 #define cOscFreq_17328kHz 0xC0
157 /* ---------------------------------------------------------------------- */
159 static int block_to_user_buf(struct saa6588 *s, unsigned char __user *user_buf)
163 if (s->rd_index == s->wr_index) {
165 dprintk(PREFIX "Read: buffer empty.\n");
170 dprintk(PREFIX "Read: ");
171 for (i = s->rd_index; i < s->rd_index + 3; i++)
172 dprintk("0x%02x ", s->buffer[i]);
175 if (copy_to_user(user_buf, &s->buffer[s->rd_index], 3))
179 if (s->rd_index >= s->buf_size)
184 dprintk("%d blocks total.\n", s->block_count);
189 static void read_from_buf(struct saa6588 *s, struct rds_command *a)
193 unsigned char __user *buf_ptr = a->buffer;
195 unsigned int rd_blocks;
201 while (!s->data_available_for_read) {
202 int ret = wait_event_interruptible(s->read_queue,
203 s->data_available_for_read);
204 if (ret == -ERESTARTSYS) {
210 spin_lock_irqsave(&s->lock, flags);
211 rd_blocks = a->block_count;
212 if (rd_blocks > s->block_count)
213 rd_blocks = s->block_count;
218 for (i = 0; i < rd_blocks; i++) {
219 if (block_to_user_buf(s, buf_ptr)) {
226 s->data_available_for_read = (s->block_count > 0);
227 spin_unlock_irqrestore(&s->lock, flags);
230 static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf)
235 dprintk(PREFIX "New block: ");
237 for (i = 0; i < 3; ++i) {
239 dprintk("0x%02x ", blockbuf[i]);
240 s->buffer[s->wr_index] = blockbuf[i];
244 if (s->wr_index >= s->buf_size)
247 if (s->wr_index == s->rd_index) {
249 if (s->rd_index >= s->buf_size)
255 dprintk("%d blocks total.\n", s->block_count);
258 static void saa6588_i2c_poll(struct saa6588 *s)
261 unsigned char tmpbuf[6];
262 unsigned char blocknum;
265 /* Although we only need 3 bytes, we have to read at least 6.
266 SAA6588 returns garbage otherwise */
267 if (6 != i2c_master_recv(&s->client, &tmpbuf[0], 6)) {
269 dprintk(PREFIX "read error!\n");
273 blocknum = tmpbuf[0] >> 5;
274 if (blocknum == s->last_blocknum) {
276 dprintk("Saw block %d again.\n", blocknum);
280 s->last_blocknum = blocknum;
283 Byte order according to v4l2 specification:
285 Byte 0: Least Significant Byte of RDS Block
286 Byte 1: Most Significant Byte of RDS Block
287 Byte 2 Bit 7: Error bit. Indicates that an uncorrectable error
288 occurred during reception of this block.
289 Bit 6: Corrected bit. Indicates that an error was
290 corrected for this data block.
291 Bits 5-3: Received Offset. Indicates the offset received
293 Bits 2-0: Offset Name. Indicates the offset applied to this data.
295 SAA6588 byte order is Status-MSB-LSB, so we have to swap the
296 first and the last of the 3 bytes block.
300 tmpbuf[2] = tmpbuf[0];
304 tmp |= blocknum << 3; /* Received offset == Offset Name (OK ?) */
305 if ((tmpbuf[2] & 0x03) == 0x03)
306 tmp |= 0x80; /* uncorrectable error */
307 else if ((tmpbuf[2] & 0x03) != 0x00)
308 tmp |= 0x40; /* corrected error */
309 tmpbuf[2] = tmp; /* Is this enough ? Should we also check other bits ? */
311 spin_lock_irqsave(&s->lock, flags);
312 block_to_buf(s, tmpbuf);
313 spin_unlock_irqrestore(&s->lock, flags);
314 s->data_available_for_read = 1;
315 wake_up_interruptible(&s->read_queue);
318 static void saa6588_timer(unsigned long data)
320 struct saa6588 *s = (struct saa6588 *)data;
322 schedule_work(&s->work);
325 static void saa6588_work(void *data)
327 struct saa6588 *s = (struct saa6588 *)data;
330 mod_timer(&s->timer, jiffies + msecs_to_jiffies(20));
333 static int saa6588_configure(struct saa6588 *s)
335 unsigned char buf[3];
338 buf[0] = cSyncRestart;
340 buf[0] |= cProcessingModeRBDS;
342 buf[1] = cFlywheelDefault;
345 buf[1] |= cPauseLevel_11mV;
348 buf[1] |= cPauseLevel_17mV;
351 buf[1] |= cPauseLevel_27mV;
354 buf[1] |= cPauseLevel_43mV;
356 default: /* nothing */
360 buf[2] = cQualityDetectDefault | cSelectOscFreqON;
364 buf[2] |= cOscFreq_4332kHz;
367 buf[2] |= cOscFreq_8664kHz;
370 buf[2] |= cOscFreq_12996kHz;
373 buf[2] |= cOscFreq_17328kHz;
375 default: /* nothing */
379 dprintk(PREFIX "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n",
380 buf[0], buf[1], buf[2]);
382 if (3 != (rc = i2c_master_send(&s->client, buf, 3)))
383 printk(PREFIX "i2c i/o error: rc == %d (should be 3)\n", rc);
388 /* ---------------------------------------------------------------------- */
390 static int saa6588_attach(struct i2c_adapter *adap, int addr, int kind)
393 client_template.adapter = adap;
394 client_template.addr = addr;
396 printk(PREFIX "chip found @ 0x%x\n", addr << 1);
398 if (NULL == (s = kmalloc(sizeof(*s), GFP_KERNEL)))
401 s->buf_size = bufblocks * 3;
403 if (NULL == (s->buffer = kmalloc(s->buf_size, GFP_KERNEL))) {
407 s->client = client_template;
411 s->last_blocknum = 0xff;
412 init_waitqueue_head(&s->read_queue);
413 s->data_available_for_read = 0;
414 i2c_set_clientdata(&s->client, s);
415 i2c_attach_client(&s->client);
417 saa6588_configure(s);
419 /* start polling via eventd */
420 INIT_WORK(&s->work, saa6588_work, s);
421 init_timer(&s->timer);
422 s->timer.function = saa6588_timer;
423 s->timer.data = (unsigned long)s;
424 schedule_work(&s->work);
428 static int saa6588_probe(struct i2c_adapter *adap)
430 #ifdef I2C_CLASS_TV_ANALOG
431 if (adap->class & I2C_CLASS_TV_ANALOG)
432 return i2c_probe(adap, &addr_data, saa6588_attach);
438 return i2c_probe(adap, &addr_data, saa6588_attach);
445 static int saa6588_detach(struct i2c_client *client)
447 struct saa6588 *s = i2c_get_clientdata(client);
449 del_timer_sync(&s->timer);
450 flush_scheduled_work();
452 i2c_detach_client(client);
458 static int saa6588_command(struct i2c_client *client, unsigned int cmd,
461 struct saa6588 *s = i2c_get_clientdata(client);
462 struct rds_command *a = (struct rds_command *)arg;
465 /* --- open() for /dev/radio --- */
467 a->result = 0; /* return error if chip doesn't work ??? */
469 /* --- close() for /dev/radio --- */
471 s->data_available_for_read = 1;
472 wake_up_interruptible(&s->read_queue);
475 /* --- read() for /dev/radio --- */
479 /* --- poll() for /dev/radio --- */
482 if (s->data_available_for_read) {
483 a->result |= POLLIN | POLLRDNORM;
485 poll_wait(a->instance, &s->read_queue, a->event_list);
495 /* ----------------------------------------------------------------------- */
497 static struct i2c_driver driver = {
498 .owner = THIS_MODULE,
499 .name = "i2c saa6588 driver",
500 .id = -1, /* FIXME */
501 .flags = I2C_DF_NOTIFY,
502 .attach_adapter = saa6588_probe,
503 .detach_client = saa6588_detach,
504 .command = saa6588_command,
507 static struct i2c_client client_template = {
509 .flags = I2C_CLIENT_ALLOW_USE,
513 static int __init saa6588_init_module(void)
515 return i2c_add_driver(&driver);
518 static void __exit saa6588_cleanup_module(void)
520 i2c_del_driver(&driver);
523 module_init(saa6588_init_module);
524 module_exit(saa6588_cleanup_module);
527 * Overrides for Emacs so that we follow Linus's tabbing style.
528 * ---------------------------------------------------------------------------