blob: f05024259f0150707957dfc37b6e973ae0bcb011 [file] [log] [blame]
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -07001/*
2 Driver for SAA6588 RDS decoder
3
4 (c) 2005 Hans J. Koch
5
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.
10
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.
15
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.
19*/
20
21
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>
33
Mauro Carvalho Chehabfa3fcce2006-03-23 21:45:24 -030034#include <media/rds.h>
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -070035
36/* Addresses to scan */
37static unsigned short normal_i2c[] = {
38 0x20 >> 1,
39 0x22 >> 1,
40 I2C_CLIENT_END,
41};
42
43I2C_CLIENT_INSMOD;
44
45/* insmod options */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030046static unsigned int debug;
47static unsigned int xtal;
48static unsigned int rbds;
49static unsigned int plvl;
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -070050static unsigned int bufblocks = 100;
51
Eric Sesterhenn / snakebyte9b565eb2006-01-13 14:10:23 -020052module_param(debug, int, 0644);
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -070053MODULE_PARM_DESC(debug, "enable debug messages");
Eric Sesterhenn / snakebyte9b565eb2006-01-13 14:10:23 -020054module_param(xtal, int, 0);
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -070055MODULE_PARM_DESC(xtal, "select oscillator frequency (0..3), default 0");
Eric Sesterhenn / snakebyte9b565eb2006-01-13 14:10:23 -020056module_param(rbds, int, 0);
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -070057MODULE_PARM_DESC(rbds, "select mode, 0=RDS, 1=RBDS, default 0");
Eric Sesterhenn / snakebyte9b565eb2006-01-13 14:10:23 -020058module_param(plvl, int, 0);
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -070059MODULE_PARM_DESC(plvl, "select pause level (0..3), default 0");
Eric Sesterhenn / snakebyte9b565eb2006-01-13 14:10:23 -020060module_param(bufblocks, int, 0);
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -070061MODULE_PARM_DESC(bufblocks, "number of buffered blocks, default 100");
62
63MODULE_DESCRIPTION("v4l2 driver module for SAA6588 RDS decoder");
64MODULE_AUTHOR("Hans J. Koch <koch@hjk-az.de>");
65
66MODULE_LICENSE("GPL");
67
68/* ---------------------------------------------------------------------- */
69
70#define UNSET (-1U)
71#define PREFIX "saa6588: "
72#define dprintk if (debug) printk
73
74struct saa6588 {
75 struct i2c_client client;
76 struct work_struct work;
77 struct timer_list timer;
78 spinlock_t lock;
79 unsigned char *buffer;
80 unsigned int buf_size;
81 unsigned int rd_index;
82 unsigned int wr_index;
83 unsigned int block_count;
84 unsigned char last_blocknum;
85 wait_queue_head_t read_queue;
86 int data_available_for_read;
87};
88
89static struct i2c_driver driver;
90static struct i2c_client client_template;
91
92/* ---------------------------------------------------------------------- */
93
94/*
95 * SAA6588 defines
96 */
97
98/* Initialization and mode control byte (0w) */
99
100/* bit 0+1 (DAC0/DAC1) */
101#define cModeStandard 0x00
102#define cModeFastPI 0x01
103#define cModeReducedRequest 0x02
104#define cModeInvalid 0x03
105
106/* bit 2 (RBDS) */
107#define cProcessingModeRDS 0x00
108#define cProcessingModeRBDS 0x04
109
110/* bit 3+4 (SYM0/SYM1) */
111#define cErrCorrectionNone 0x00
112#define cErrCorrection2Bits 0x08
113#define cErrCorrection5Bits 0x10
114#define cErrCorrectionNoneRBDS 0x18
115
116/* bit 5 (NWSY) */
117#define cSyncNormal 0x00
118#define cSyncRestart 0x20
119
120/* bit 6 (TSQD) */
121#define cSigQualityDetectOFF 0x00
122#define cSigQualityDetectON 0x40
123
124/* bit 7 (SQCM) */
125#define cSigQualityTriggered 0x00
126#define cSigQualityContinous 0x80
127
128/* Pause level and flywheel control byte (1w) */
129
130/* bits 0..5 (FEB0..FEB5) */
131#define cFlywheelMaxBlocksMask 0x3F
132#define cFlywheelDefault 0x20
133
134/* bits 6+7 (PL0/PL1) */
135#define cPauseLevel_11mV 0x00
136#define cPauseLevel_17mV 0x40
137#define cPauseLevel_27mV 0x80
138#define cPauseLevel_43mV 0xC0
139
140/* Pause time/oscillator frequency/quality detector control byte (1w) */
141
142/* bits 0..4 (SQS0..SQS4) */
143#define cQualityDetectSensMask 0x1F
144#define cQualityDetectDefault 0x0F
145
146/* bit 5 (SOSC) */
147#define cSelectOscFreqOFF 0x00
148#define cSelectOscFreqON 0x20
149
150/* bit 6+7 (PTF0/PTF1) */
151#define cOscFreq_4332kHz 0x00
152#define cOscFreq_8664kHz 0x40
153#define cOscFreq_12996kHz 0x80
154#define cOscFreq_17328kHz 0xC0
155
156/* ---------------------------------------------------------------------- */
157
Al Viroae8aed02005-09-29 00:37:34 +0100158static int block_to_user_buf(struct saa6588 *s, unsigned char __user *user_buf)
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700159{
160 int i;
161
162 if (s->rd_index == s->wr_index) {
163 if (debug > 2)
164 dprintk(PREFIX "Read: buffer empty.\n");
165 return 0;
166 }
167
168 if (debug > 2) {
169 dprintk(PREFIX "Read: ");
170 for (i = s->rd_index; i < s->rd_index + 3; i++)
171 dprintk("0x%02x ", s->buffer[i]);
172 }
173
174 if (copy_to_user(user_buf, &s->buffer[s->rd_index], 3))
175 return -EFAULT;
176
177 s->rd_index += 3;
178 if (s->rd_index >= s->buf_size)
179 s->rd_index = 0;
180 s->block_count--;
181
182 if (debug > 2)
183 dprintk("%d blocks total.\n", s->block_count);
184
185 return 1;
186}
187
188static void read_from_buf(struct saa6588 *s, struct rds_command *a)
189{
190 unsigned long flags;
191
Al Viroae8aed02005-09-29 00:37:34 +0100192 unsigned char __user *buf_ptr = a->buffer;
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700193 unsigned int i;
194 unsigned int rd_blocks;
195
196 a->result = 0;
197 if (!a->buffer)
198 return;
199
200 while (!s->data_available_for_read) {
201 int ret = wait_event_interruptible(s->read_queue,
202 s->data_available_for_read);
203 if (ret == -ERESTARTSYS) {
204 a->result = -EINTR;
205 return;
206 }
207 }
208
209 spin_lock_irqsave(&s->lock, flags);
210 rd_blocks = a->block_count;
211 if (rd_blocks > s->block_count)
212 rd_blocks = s->block_count;
213
Ira Snydera5bbc7d2006-11-20 07:20:48 -0300214 if (!rd_blocks) {
215 spin_unlock_irqrestore(&s->lock, flags);
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700216 return;
Ira Snydera5bbc7d2006-11-20 07:20:48 -0300217 }
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700218
219 for (i = 0; i < rd_blocks; i++) {
220 if (block_to_user_buf(s, buf_ptr)) {
221 buf_ptr += 3;
222 a->result++;
223 } else
224 break;
225 }
226 a->result *= 3;
227 s->data_available_for_read = (s->block_count > 0);
228 spin_unlock_irqrestore(&s->lock, flags);
229}
230
231static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf)
232{
233 unsigned int i;
234
235 if (debug > 3)
236 dprintk(PREFIX "New block: ");
237
238 for (i = 0; i < 3; ++i) {
239 if (debug > 3)
240 dprintk("0x%02x ", blockbuf[i]);
241 s->buffer[s->wr_index] = blockbuf[i];
242 s->wr_index++;
243 }
244
245 if (s->wr_index >= s->buf_size)
246 s->wr_index = 0;
247
248 if (s->wr_index == s->rd_index) {
Hans J. Koch6c3d67a2005-11-08 21:38:39 -0800249 s->rd_index += 3;
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700250 if (s->rd_index >= s->buf_size)
251 s->rd_index = 0;
252 } else
253 s->block_count++;
254
255 if (debug > 3)
256 dprintk("%d blocks total.\n", s->block_count);
257}
258
259static void saa6588_i2c_poll(struct saa6588 *s)
260{
261 unsigned long flags;
262 unsigned char tmpbuf[6];
263 unsigned char blocknum;
264 unsigned char tmp;
265
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)) {
269 if (debug > 1)
270 dprintk(PREFIX "read error!\n");
271 return;
272 }
273
274 blocknum = tmpbuf[0] >> 5;
275 if (blocknum == s->last_blocknum) {
276 if (debug > 3)
277 dprintk("Saw block %d again.\n", blocknum);
278 return;
279 }
280
281 s->last_blocknum = blocknum;
282
283 /*
284 Byte order according to v4l2 specification:
285
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
293 by the sync system.
294 Bits 2-0: Offset Name. Indicates the offset applied to this data.
295
296 SAA6588 byte order is Status-MSB-LSB, so we have to swap the
297 first and the last of the 3 bytes block.
298 */
299
300 tmp = tmpbuf[2];
301 tmpbuf[2] = tmpbuf[0];
302 tmpbuf[0] = tmp;
303
304 tmp = blocknum;
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 ? */
311
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);
317}
318
319static void saa6588_timer(unsigned long data)
320{
321 struct saa6588 *s = (struct saa6588 *)data;
322
323 schedule_work(&s->work);
324}
325
David Howellsc4028952006-11-22 14:57:56 +0000326static void saa6588_work(struct work_struct *work)
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700327{
David Howellsc4028952006-11-22 14:57:56 +0000328 struct saa6588 *s = container_of(work, struct saa6588, work);
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700329
330 saa6588_i2c_poll(s);
Hans J. Koche222f832005-11-08 21:36:35 -0800331 mod_timer(&s->timer, jiffies + msecs_to_jiffies(20));
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700332}
333
334static int saa6588_configure(struct saa6588 *s)
335{
336 unsigned char buf[3];
337 int rc;
338
339 buf[0] = cSyncRestart;
340 if (rbds)
341 buf[0] |= cProcessingModeRBDS;
342
343 buf[1] = cFlywheelDefault;
344 switch (plvl) {
345 case 0:
346 buf[1] |= cPauseLevel_11mV;
347 break;
348 case 1:
349 buf[1] |= cPauseLevel_17mV;
350 break;
351 case 2:
352 buf[1] |= cPauseLevel_27mV;
353 break;
354 case 3:
355 buf[1] |= cPauseLevel_43mV;
356 break;
357 default: /* nothing */
358 break;
359 }
360
361 buf[2] = cQualityDetectDefault | cSelectOscFreqON;
362
363 switch (xtal) {
364 case 0:
365 buf[2] |= cOscFreq_4332kHz;
366 break;
367 case 1:
368 buf[2] |= cOscFreq_8664kHz;
369 break;
370 case 2:
371 buf[2] |= cOscFreq_12996kHz;
372 break;
373 case 3:
374 buf[2] |= cOscFreq_17328kHz;
375 break;
376 default: /* nothing */
377 break;
378 }
379
380 dprintk(PREFIX "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n",
381 buf[0], buf[1], buf[2]);
382
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);
385
386 return 0;
387}
388
389/* ---------------------------------------------------------------------- */
390
391static int saa6588_attach(struct i2c_adapter *adap, int addr, int kind)
392{
393 struct saa6588 *s;
394 client_template.adapter = adap;
395 client_template.addr = addr;
396
397 printk(PREFIX "chip found @ 0x%x\n", addr << 1);
398
399 if (NULL == (s = kmalloc(sizeof(*s), GFP_KERNEL)))
400 return -ENOMEM;
401
402 s->buf_size = bufblocks * 3;
403
404 if (NULL == (s->buffer = kmalloc(s->buf_size, GFP_KERNEL))) {
405 kfree(s);
406 return -ENOMEM;
407 }
Hans-Jürgen Koch588005e2007-09-12 16:20:43 -0300408 spin_lock_init(&s->lock);
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700409 s->client = client_template;
410 s->block_count = 0;
411 s->wr_index = 0;
412 s->rd_index = 0;
413 s->last_blocknum = 0xff;
414 init_waitqueue_head(&s->read_queue);
415 s->data_available_for_read = 0;
416 i2c_set_clientdata(&s->client, s);
417 i2c_attach_client(&s->client);
418
419 saa6588_configure(s);
420
421 /* start polling via eventd */
David Howellsc4028952006-11-22 14:57:56 +0000422 INIT_WORK(&s->work, saa6588_work);
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700423 init_timer(&s->timer);
424 s->timer.function = saa6588_timer;
425 s->timer.data = (unsigned long)s;
426 schedule_work(&s->work);
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700427 return 0;
428}
429
430static int saa6588_probe(struct i2c_adapter *adap)
431{
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700432 if (adap->class & I2C_CLASS_TV_ANALOG)
433 return i2c_probe(adap, &addr_data, saa6588_attach);
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700434 return 0;
435}
436
437static int saa6588_detach(struct i2c_client *client)
438{
439 struct saa6588 *s = i2c_get_clientdata(client);
440
441 del_timer_sync(&s->timer);
442 flush_scheduled_work();
443
444 i2c_detach_client(client);
445 kfree(s->buffer);
446 kfree(s);
447 return 0;
448}
449
450static int saa6588_command(struct i2c_client *client, unsigned int cmd,
451 void *arg)
452{
453 struct saa6588 *s = i2c_get_clientdata(client);
454 struct rds_command *a = (struct rds_command *)arg;
455
456 switch (cmd) {
457 /* --- open() for /dev/radio --- */
458 case RDS_CMD_OPEN:
459 a->result = 0; /* return error if chip doesn't work ??? */
460 break;
461 /* --- close() for /dev/radio --- */
462 case RDS_CMD_CLOSE:
463 s->data_available_for_read = 1;
464 wake_up_interruptible(&s->read_queue);
465 a->result = 0;
466 break;
467 /* --- read() for /dev/radio --- */
468 case RDS_CMD_READ:
469 read_from_buf(s, a);
470 break;
471 /* --- poll() for /dev/radio --- */
472 case RDS_CMD_POLL:
473 a->result = 0;
474 if (s->data_available_for_read) {
475 a->result |= POLLIN | POLLRDNORM;
476 }
477 poll_wait(a->instance, &s->read_queue, a->event_list);
478 break;
479
480 default:
481 /* nothing */
482 break;
483 }
484 return 0;
485}
486
487/* ----------------------------------------------------------------------- */
488
489static struct i2c_driver driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100490 .driver = {
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -0200491 .name = "saa6588",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100492 },
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700493 .id = -1, /* FIXME */
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700494 .attach_adapter = saa6588_probe,
495 .detach_client = saa6588_detach,
496 .command = saa6588_command,
497};
498
499static struct i2c_client client_template = {
500 .name = "saa6588",
Mauro Carvalho Chehab10b89ee32005-09-09 13:04:03 -0700501 .driver = &driver,
502};
503
504static int __init saa6588_init_module(void)
505{
506 return i2c_add_driver(&driver);
507}
508
509static void __exit saa6588_cleanup_module(void)
510{
511 i2c_del_driver(&driver);
512}
513
514module_init(saa6588_init_module);
515module_exit(saa6588_cleanup_module);
516
517/*
518 * Overrides for Emacs so that we follow Linus's tabbing style.
519 * ---------------------------------------------------------------------------
520 * Local variables:
521 * c-basic-offset: 8
522 * End:
523 */