blob: 2ce758c1ebb1d0f1b4d87518bfe9d52ea08f6410 [file] [log] [blame]
Mauro Carvalho Chehab10b89ee2005-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>
Hans Verkuil7f6adea2009-02-19 17:31:17 -030026#include <linux/videodev2.h>
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -070027#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>
Hans Verkuilc3fda7f2009-02-11 19:23:57 -030035#include <media/v4l2-device.h>
Hans Verkuilf1f8c902009-02-11 19:28:30 -030036#include <media/v4l2-chip-ident.h>
Hans Verkuilb5ffc222009-02-11 19:14:09 -030037#include <media/v4l2-i2c-drv-legacy.h>
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -070038
39/* Addresses to scan */
40static unsigned short normal_i2c[] = {
41 0x20 >> 1,
42 0x22 >> 1,
43 I2C_CLIENT_END,
44};
45
46I2C_CLIENT_INSMOD;
47
48/* insmod options */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030049static unsigned int debug;
50static unsigned int xtal;
51static unsigned int rbds;
52static unsigned int plvl;
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -070053static unsigned int bufblocks = 100;
54
Eric Sesterhenn / snakebyte9b565eb2006-01-13 14:10:23 -020055module_param(debug, int, 0644);
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -070056MODULE_PARM_DESC(debug, "enable debug messages");
Eric Sesterhenn / snakebyte9b565eb2006-01-13 14:10:23 -020057module_param(xtal, int, 0);
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -070058MODULE_PARM_DESC(xtal, "select oscillator frequency (0..3), default 0");
Eric Sesterhenn / snakebyte9b565eb2006-01-13 14:10:23 -020059module_param(rbds, int, 0);
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -070060MODULE_PARM_DESC(rbds, "select mode, 0=RDS, 1=RBDS, default 0");
Eric Sesterhenn / snakebyte9b565eb2006-01-13 14:10:23 -020061module_param(plvl, int, 0);
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -070062MODULE_PARM_DESC(plvl, "select pause level (0..3), default 0");
Eric Sesterhenn / snakebyte9b565eb2006-01-13 14:10:23 -020063module_param(bufblocks, int, 0);
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -070064MODULE_PARM_DESC(bufblocks, "number of buffered blocks, default 100");
65
66MODULE_DESCRIPTION("v4l2 driver module for SAA6588 RDS decoder");
67MODULE_AUTHOR("Hans J. Koch <koch@hjk-az.de>");
68
69MODULE_LICENSE("GPL");
70
71/* ---------------------------------------------------------------------- */
72
73#define UNSET (-1U)
74#define PREFIX "saa6588: "
75#define dprintk if (debug) printk
76
77struct saa6588 {
Hans Verkuilc3fda7f2009-02-11 19:23:57 -030078 struct v4l2_subdev sd;
Jean Delvarefb6991d2009-03-07 07:44:12 -030079 struct delayed_work work;
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -070080 spinlock_t lock;
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;
89};
90
Hans Verkuilc3fda7f2009-02-11 19:23:57 -030091static inline struct saa6588 *to_saa6588(struct v4l2_subdev *sd)
92{
93 return container_of(sd, struct saa6588, sd);
94}
95
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -070096/* ---------------------------------------------------------------------- */
97
98/*
99 * SAA6588 defines
100 */
101
102/* Initialization and mode control byte (0w) */
103
104/* bit 0+1 (DAC0/DAC1) */
105#define cModeStandard 0x00
106#define cModeFastPI 0x01
107#define cModeReducedRequest 0x02
108#define cModeInvalid 0x03
109
110/* bit 2 (RBDS) */
111#define cProcessingModeRDS 0x00
112#define cProcessingModeRBDS 0x04
113
114/* bit 3+4 (SYM0/SYM1) */
115#define cErrCorrectionNone 0x00
116#define cErrCorrection2Bits 0x08
117#define cErrCorrection5Bits 0x10
118#define cErrCorrectionNoneRBDS 0x18
119
120/* bit 5 (NWSY) */
121#define cSyncNormal 0x00
122#define cSyncRestart 0x20
123
124/* bit 6 (TSQD) */
125#define cSigQualityDetectOFF 0x00
126#define cSigQualityDetectON 0x40
127
128/* bit 7 (SQCM) */
129#define cSigQualityTriggered 0x00
130#define cSigQualityContinous 0x80
131
132/* Pause level and flywheel control byte (1w) */
133
134/* bits 0..5 (FEB0..FEB5) */
135#define cFlywheelMaxBlocksMask 0x3F
136#define cFlywheelDefault 0x20
137
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
143
144/* Pause time/oscillator frequency/quality detector control byte (1w) */
145
146/* bits 0..4 (SQS0..SQS4) */
147#define cQualityDetectSensMask 0x1F
148#define cQualityDetectDefault 0x0F
149
150/* bit 5 (SOSC) */
151#define cSelectOscFreqOFF 0x00
152#define cSelectOscFreqON 0x20
153
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
159
160/* ---------------------------------------------------------------------- */
161
Al Viroae8aed02005-09-29 00:37:34 +0100162static int block_to_user_buf(struct saa6588 *s, unsigned char __user *user_buf)
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700163{
164 int i;
165
166 if (s->rd_index == s->wr_index) {
167 if (debug > 2)
168 dprintk(PREFIX "Read: buffer empty.\n");
169 return 0;
170 }
171
172 if (debug > 2) {
173 dprintk(PREFIX "Read: ");
174 for (i = s->rd_index; i < s->rd_index + 3; i++)
175 dprintk("0x%02x ", s->buffer[i]);
176 }
177
178 if (copy_to_user(user_buf, &s->buffer[s->rd_index], 3))
179 return -EFAULT;
180
181 s->rd_index += 3;
182 if (s->rd_index >= s->buf_size)
183 s->rd_index = 0;
184 s->block_count--;
185
186 if (debug > 2)
187 dprintk("%d blocks total.\n", s->block_count);
188
189 return 1;
190}
191
192static void read_from_buf(struct saa6588 *s, struct rds_command *a)
193{
194 unsigned long flags;
195
Al Viroae8aed02005-09-29 00:37:34 +0100196 unsigned char __user *buf_ptr = a->buffer;
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700197 unsigned int i;
198 unsigned int rd_blocks;
199
200 a->result = 0;
201 if (!a->buffer)
202 return;
203
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) {
208 a->result = -EINTR;
209 return;
210 }
211 }
212
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;
217
Ira Snydera5bbc7d2006-11-20 07:20:48 -0300218 if (!rd_blocks) {
219 spin_unlock_irqrestore(&s->lock, flags);
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700220 return;
Ira Snydera5bbc7d2006-11-20 07:20:48 -0300221 }
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700222
223 for (i = 0; i < rd_blocks; i++) {
224 if (block_to_user_buf(s, buf_ptr)) {
225 buf_ptr += 3;
226 a->result++;
227 } else
228 break;
229 }
230 a->result *= 3;
231 s->data_available_for_read = (s->block_count > 0);
232 spin_unlock_irqrestore(&s->lock, flags);
233}
234
235static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf)
236{
237 unsigned int i;
238
239 if (debug > 3)
240 dprintk(PREFIX "New block: ");
241
242 for (i = 0; i < 3; ++i) {
243 if (debug > 3)
244 dprintk("0x%02x ", blockbuf[i]);
245 s->buffer[s->wr_index] = blockbuf[i];
246 s->wr_index++;
247 }
248
249 if (s->wr_index >= s->buf_size)
250 s->wr_index = 0;
251
252 if (s->wr_index == s->rd_index) {
Hans J. Koch6c3d67a2005-11-08 21:38:39 -0800253 s->rd_index += 3;
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700254 if (s->rd_index >= s->buf_size)
255 s->rd_index = 0;
256 } else
257 s->block_count++;
258
259 if (debug > 3)
260 dprintk("%d blocks total.\n", s->block_count);
261}
262
263static void saa6588_i2c_poll(struct saa6588 *s)
264{
Hans Verkuilc3fda7f2009-02-11 19:23:57 -0300265 struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700266 unsigned long flags;
267 unsigned char tmpbuf[6];
268 unsigned char blocknum;
269 unsigned char tmp;
270
271 /* Although we only need 3 bytes, we have to read at least 6.
272 SAA6588 returns garbage otherwise */
Hans Verkuilc3fda7f2009-02-11 19:23:57 -0300273 if (6 != i2c_master_recv(client, &tmpbuf[0], 6)) {
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700274 if (debug > 1)
275 dprintk(PREFIX "read error!\n");
276 return;
277 }
278
279 blocknum = tmpbuf[0] >> 5;
280 if (blocknum == s->last_blocknum) {
281 if (debug > 3)
282 dprintk("Saw block %d again.\n", blocknum);
283 return;
284 }
285
286 s->last_blocknum = blocknum;
287
288 /*
289 Byte order according to v4l2 specification:
290
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
298 by the sync system.
299 Bits 2-0: Offset Name. Indicates the offset applied to this data.
300
301 SAA6588 byte order is Status-MSB-LSB, so we have to swap the
302 first and the last of the 3 bytes block.
303 */
304
305 tmp = tmpbuf[2];
306 tmpbuf[2] = tmpbuf[0];
307 tmpbuf[0] = tmp;
308
309 tmp = blocknum;
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 ? */
316
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);
322}
323
David Howellsc4028952006-11-22 14:57:56 +0000324static void saa6588_work(struct work_struct *work)
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700325{
Jean Delvarefb6991d2009-03-07 07:44:12 -0300326 struct saa6588 *s = container_of(work, struct saa6588, work.work);
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700327
328 saa6588_i2c_poll(s);
Jean Delvarefb6991d2009-03-07 07:44:12 -0300329 schedule_delayed_work(&s->work, msecs_to_jiffies(20));
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700330}
331
332static int saa6588_configure(struct saa6588 *s)
333{
Hans Verkuilc3fda7f2009-02-11 19:23:57 -0300334 struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700335 unsigned char buf[3];
336 int rc;
337
338 buf[0] = cSyncRestart;
339 if (rbds)
340 buf[0] |= cProcessingModeRBDS;
341
342 buf[1] = cFlywheelDefault;
343 switch (plvl) {
344 case 0:
345 buf[1] |= cPauseLevel_11mV;
346 break;
347 case 1:
348 buf[1] |= cPauseLevel_17mV;
349 break;
350 case 2:
351 buf[1] |= cPauseLevel_27mV;
352 break;
353 case 3:
354 buf[1] |= cPauseLevel_43mV;
355 break;
356 default: /* nothing */
357 break;
358 }
359
360 buf[2] = cQualityDetectDefault | cSelectOscFreqON;
361
362 switch (xtal) {
363 case 0:
364 buf[2] |= cOscFreq_4332kHz;
365 break;
366 case 1:
367 buf[2] |= cOscFreq_8664kHz;
368 break;
369 case 2:
370 buf[2] |= cOscFreq_12996kHz;
371 break;
372 case 3:
373 buf[2] |= cOscFreq_17328kHz;
374 break;
375 default: /* nothing */
376 break;
377 }
378
379 dprintk(PREFIX "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n",
380 buf[0], buf[1], buf[2]);
381
Hans Verkuilc3fda7f2009-02-11 19:23:57 -0300382 rc = i2c_master_send(client, buf, 3);
Hans Verkuilb5ffc222009-02-11 19:14:09 -0300383 if (rc != 3)
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700384 printk(PREFIX "i2c i/o error: rc == %d (should be 3)\n", rc);
385
386 return 0;
387}
388
389/* ---------------------------------------------------------------------- */
390
Hans Verkuilc3fda7f2009-02-11 19:23:57 -0300391static long saa6588_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700392{
Hans Verkuilc3fda7f2009-02-11 19:23:57 -0300393 struct saa6588 *s = to_saa6588(sd);
394 struct rds_command *a = arg;
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700395
396 switch (cmd) {
397 /* --- open() for /dev/radio --- */
398 case RDS_CMD_OPEN:
399 a->result = 0; /* return error if chip doesn't work ??? */
400 break;
401 /* --- close() for /dev/radio --- */
402 case RDS_CMD_CLOSE:
403 s->data_available_for_read = 1;
404 wake_up_interruptible(&s->read_queue);
405 a->result = 0;
406 break;
407 /* --- read() for /dev/radio --- */
408 case RDS_CMD_READ:
409 read_from_buf(s, a);
410 break;
411 /* --- poll() for /dev/radio --- */
412 case RDS_CMD_POLL:
413 a->result = 0;
414 if (s->data_available_for_read) {
415 a->result |= POLLIN | POLLRDNORM;
416 }
417 poll_wait(a->instance, &s->read_queue, a->event_list);
418 break;
419
420 default:
421 /* nothing */
Hans Verkuilc3fda7f2009-02-11 19:23:57 -0300422 return -ENOIOCTLCMD;
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700423 }
424 return 0;
425}
426
Hans Verkuilf1f8c902009-02-11 19:28:30 -0300427static int saa6588_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
428{
429 struct i2c_client *client = v4l2_get_subdevdata(sd);
430
431 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA6588, 0);
432}
433
Hans Verkuilc3fda7f2009-02-11 19:23:57 -0300434static int saa6588_command(struct i2c_client *client, unsigned cmd, void *arg)
435{
436 return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
437}
438
439/* ----------------------------------------------------------------------- */
440
441static const struct v4l2_subdev_core_ops saa6588_core_ops = {
Hans Verkuilf1f8c902009-02-11 19:28:30 -0300442 .g_chip_ident = saa6588_g_chip_ident,
Hans Verkuilc3fda7f2009-02-11 19:23:57 -0300443 .ioctl = saa6588_ioctl,
444};
445
446static const struct v4l2_subdev_ops saa6588_ops = {
447 .core = &saa6588_core_ops,
448};
449
450/* ---------------------------------------------------------------------- */
451
452static int saa6588_probe(struct i2c_client *client,
453 const struct i2c_device_id *id)
454{
455 struct saa6588 *s;
456 struct v4l2_subdev *sd;
457
458 v4l_info(client, "saa6588 found @ 0x%x (%s)\n",
459 client->addr << 1, client->adapter->name);
460
461 s = kzalloc(sizeof(*s), GFP_KERNEL);
462 if (s == NULL)
463 return -ENOMEM;
464
465 s->buf_size = bufblocks * 3;
466
467 s->buffer = kmalloc(s->buf_size, GFP_KERNEL);
468 if (s->buffer == NULL) {
469 kfree(s);
470 return -ENOMEM;
471 }
472 sd = &s->sd;
473 v4l2_i2c_subdev_init(sd, client, &saa6588_ops);
474 spin_lock_init(&s->lock);
475 s->block_count = 0;
476 s->wr_index = 0;
477 s->rd_index = 0;
478 s->last_blocknum = 0xff;
479 init_waitqueue_head(&s->read_queue);
480 s->data_available_for_read = 0;
481
482 saa6588_configure(s);
483
484 /* start polling via eventd */
Jean Delvarefb6991d2009-03-07 07:44:12 -0300485 INIT_DELAYED_WORK(&s->work, saa6588_work);
486 schedule_delayed_work(&s->work, 0);
Hans Verkuilc3fda7f2009-02-11 19:23:57 -0300487 return 0;
488}
489
490static int saa6588_remove(struct i2c_client *client)
491{
492 struct v4l2_subdev *sd = i2c_get_clientdata(client);
493 struct saa6588 *s = to_saa6588(sd);
494
495 v4l2_device_unregister_subdev(sd);
496
Jean Delvarefb6991d2009-03-07 07:44:12 -0300497 cancel_delayed_work_sync(&s->work);
Hans Verkuilc3fda7f2009-02-11 19:23:57 -0300498
499 kfree(s->buffer);
500 kfree(s);
501 return 0;
502}
503
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700504/* ----------------------------------------------------------------------- */
505
Hans Verkuilb5ffc222009-02-11 19:14:09 -0300506static const struct i2c_device_id saa6588_id[] = {
507 { "saa6588", 0 },
508 { }
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700509};
Hans Verkuilb5ffc222009-02-11 19:14:09 -0300510MODULE_DEVICE_TABLE(i2c, saa6588_id);
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700511
Hans Verkuilb5ffc222009-02-11 19:14:09 -0300512static struct v4l2_i2c_driver_data v4l2_i2c_data = {
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700513 .name = "saa6588",
Hans Verkuilb5ffc222009-02-11 19:14:09 -0300514 .command = saa6588_command,
515 .probe = saa6588_probe,
516 .remove = saa6588_remove,
Hans Verkuilb5ffc222009-02-11 19:14:09 -0300517 .id_table = saa6588_id,
Mauro Carvalho Chehab10b89ee2005-09-09 13:04:03 -0700518};