1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Driver for SAA6588 RDS decoder
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/i2c.h>
13 #include <linux/types.h>
14 #include <linux/videodev2.h>
15 #include <linux/init.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/poll.h>
19 #include <linux/wait.h>
20 #include <linux/uaccess.h>
22 #include <media/i2c/saa6588.h>
23 #include <media/v4l2-device.h>
27 static unsigned int debug
;
28 static unsigned int xtal
;
29 static unsigned int mmbs
;
30 static unsigned int plvl
;
31 static unsigned int bufblocks
= 100;
33 module_param(debug
, int, 0644);
34 MODULE_PARM_DESC(debug
, "enable debug messages");
35 module_param(xtal
, int, 0);
36 MODULE_PARM_DESC(xtal
, "select oscillator frequency (0..3), default 0");
37 module_param(mmbs
, int, 0);
38 MODULE_PARM_DESC(mmbs
, "enable MMBS mode: 0=off (default), 1=on");
39 module_param(plvl
, int, 0);
40 MODULE_PARM_DESC(plvl
, "select pause level (0..3), default 0");
41 module_param(bufblocks
, int, 0);
42 MODULE_PARM_DESC(bufblocks
, "number of buffered blocks, default 100");
44 MODULE_DESCRIPTION("v4l2 driver module for SAA6588 RDS decoder");
45 MODULE_AUTHOR("Hans J. Koch <koch@hjk-az.de>");
47 MODULE_LICENSE("GPL");
49 /* ---------------------------------------------------------------------- */
52 #define PREFIX "saa6588: "
53 #define dprintk if (debug) printk
56 struct v4l2_subdev sd
;
57 struct delayed_work work
;
59 unsigned char *buffer
;
60 unsigned int buf_size
;
61 unsigned int rd_index
;
62 unsigned int wr_index
;
63 unsigned int block_count
;
64 unsigned char last_blocknum
;
65 wait_queue_head_t read_queue
;
66 int data_available_for_read
;
70 static inline struct saa6588
*to_saa6588(struct v4l2_subdev
*sd
)
72 return container_of(sd
, struct saa6588
, sd
);
75 /* ---------------------------------------------------------------------- */
81 /* Initialization and mode control byte (0w) */
83 /* bit 0+1 (DAC0/DAC1) */
84 #define cModeStandard 0x00
85 #define cModeFastPI 0x01
86 #define cModeReducedRequest 0x02
87 #define cModeInvalid 0x03
90 #define cProcessingModeRDS 0x00
91 #define cProcessingModeRBDS 0x04
93 /* bit 3+4 (SYM0/SYM1) */
94 #define cErrCorrectionNone 0x00
95 #define cErrCorrection2Bits 0x08
96 #define cErrCorrection5Bits 0x10
97 #define cErrCorrectionNoneRBDS 0x18
100 #define cSyncNormal 0x00
101 #define cSyncRestart 0x20
104 #define cSigQualityDetectOFF 0x00
105 #define cSigQualityDetectON 0x40
108 #define cSigQualityTriggered 0x00
109 #define cSigQualityContinous 0x80
111 /* Pause level and flywheel control byte (1w) */
113 /* bits 0..5 (FEB0..FEB5) */
114 #define cFlywheelMaxBlocksMask 0x3F
115 #define cFlywheelDefault 0x20
117 /* bits 6+7 (PL0/PL1) */
118 #define cPauseLevel_11mV 0x00
119 #define cPauseLevel_17mV 0x40
120 #define cPauseLevel_27mV 0x80
121 #define cPauseLevel_43mV 0xC0
123 /* Pause time/oscillator frequency/quality detector control byte (1w) */
125 /* bits 0..4 (SQS0..SQS4) */
126 #define cQualityDetectSensMask 0x1F
127 #define cQualityDetectDefault 0x0F
130 #define cSelectOscFreqOFF 0x00
131 #define cSelectOscFreqON 0x20
133 /* bit 6+7 (PTF0/PTF1) */
134 #define cOscFreq_4332kHz 0x00
135 #define cOscFreq_8664kHz 0x40
136 #define cOscFreq_12996kHz 0x80
137 #define cOscFreq_17328kHz 0xC0
139 /* ---------------------------------------------------------------------- */
141 static bool block_from_buf(struct saa6588
*s
, unsigned char *buf
)
145 if (s
->rd_index
== s
->wr_index
) {
147 dprintk(PREFIX
"Read: buffer empty.\n");
152 dprintk(PREFIX
"Read: ");
153 for (i
= s
->rd_index
; i
< s
->rd_index
+ 3; i
++)
154 dprintk("0x%02x ", s
->buffer
[i
]);
157 memcpy(buf
, &s
->buffer
[s
->rd_index
], 3);
160 if (s
->rd_index
>= s
->buf_size
)
165 dprintk("%d blocks total.\n", s
->block_count
);
170 static void read_from_buf(struct saa6588
*s
, struct saa6588_command
*a
)
172 unsigned char __user
*buf_ptr
= a
->buffer
;
173 unsigned char buf
[3];
175 unsigned int rd_blocks
;
182 while (!a
->nonblocking
&& !s
->data_available_for_read
) {
183 int ret
= wait_event_interruptible(s
->read_queue
,
184 s
->data_available_for_read
);
185 if (ret
== -ERESTARTSYS
) {
191 rd_blocks
= a
->block_count
;
192 spin_lock_irqsave(&s
->lock
, flags
);
193 if (rd_blocks
> s
->block_count
)
194 rd_blocks
= s
->block_count
;
195 spin_unlock_irqrestore(&s
->lock
, flags
);
200 for (i
= 0; i
< rd_blocks
; i
++) {
203 spin_lock_irqsave(&s
->lock
, flags
);
204 got_block
= block_from_buf(s
, buf
);
205 spin_unlock_irqrestore(&s
->lock
, flags
);
208 if (copy_to_user(buf_ptr
, buf
, 3)) {
215 spin_lock_irqsave(&s
->lock
, flags
);
216 s
->data_available_for_read
= (s
->block_count
> 0);
217 spin_unlock_irqrestore(&s
->lock
, flags
);
220 static void block_to_buf(struct saa6588
*s
, unsigned char *blockbuf
)
225 dprintk(PREFIX
"New block: ");
227 for (i
= 0; i
< 3; ++i
) {
229 dprintk("0x%02x ", blockbuf
[i
]);
230 s
->buffer
[s
->wr_index
] = blockbuf
[i
];
234 if (s
->wr_index
>= s
->buf_size
)
237 if (s
->wr_index
== s
->rd_index
) {
239 if (s
->rd_index
>= s
->buf_size
)
245 dprintk("%d blocks total.\n", s
->block_count
);
248 static void saa6588_i2c_poll(struct saa6588
*s
)
250 struct i2c_client
*client
= v4l2_get_subdevdata(&s
->sd
);
252 unsigned char tmpbuf
[6];
253 unsigned char blocknum
;
256 /* Although we only need 3 bytes, we have to read at least 6.
257 SAA6588 returns garbage otherwise. */
258 if (6 != i2c_master_recv(client
, &tmpbuf
[0], 6)) {
260 dprintk(PREFIX
"read error!\n");
264 s
->sync
= tmpbuf
[0] & 0x10;
267 blocknum
= tmpbuf
[0] >> 5;
268 if (blocknum
== s
->last_blocknum
) {
270 dprintk("Saw block %d again.\n", blocknum
);
274 s
->last_blocknum
= blocknum
;
277 Byte order according to v4l2 specification:
279 Byte 0: Least Significant Byte of RDS Block
280 Byte 1: Most Significant Byte of RDS Block
281 Byte 2 Bit 7: Error bit. Indicates that an uncorrectable error
282 occurred during reception of this block.
283 Bit 6: Corrected bit. Indicates that an error was
284 corrected for this data block.
285 Bits 5-3: Same as bits 0-2.
286 Bits 2-0: Block number.
288 SAA6588 byte order is Status-MSB-LSB, so we have to swap the
289 first and the last of the 3 bytes block.
292 swap(tmpbuf
[2], tmpbuf
[0]);
294 /* Map 'Invalid block E' to 'Invalid Block' */
296 blocknum
= V4L2_RDS_BLOCK_INVALID
;
297 /* And if are not in mmbs mode, then 'Block E' is also mapped
298 to 'Invalid Block'. As far as I can tell MMBS is discontinued,
299 and if there is ever a need to support E blocks, then please
300 contact the linux-media mailinglist. */
301 else if (!mmbs
&& blocknum
== 5)
302 blocknum
= V4L2_RDS_BLOCK_INVALID
;
304 tmp
|= blocknum
<< 3; /* Received offset == Offset Name (OK ?) */
305 if ((tmpbuf
[2] & 0x03) == 0x03)
306 tmp
|= V4L2_RDS_BLOCK_ERROR
; /* uncorrectable error */
307 else if ((tmpbuf
[2] & 0x03) != 0x00)
308 tmp
|= V4L2_RDS_BLOCK_CORRECTED
; /* 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_work(struct work_struct
*work
)
320 struct saa6588
*s
= container_of(work
, struct saa6588
, work
.work
);
323 schedule_delayed_work(&s
->work
, msecs_to_jiffies(20));
326 static void saa6588_configure(struct saa6588
*s
)
328 struct i2c_client
*client
= v4l2_get_subdevdata(&s
->sd
);
329 unsigned char buf
[3];
332 buf
[0] = cSyncRestart
;
334 buf
[0] |= cProcessingModeRBDS
;
336 buf
[1] = cFlywheelDefault
;
339 buf
[1] |= cPauseLevel_11mV
;
342 buf
[1] |= cPauseLevel_17mV
;
345 buf
[1] |= cPauseLevel_27mV
;
348 buf
[1] |= cPauseLevel_43mV
;
350 default: /* nothing */
354 buf
[2] = cQualityDetectDefault
| cSelectOscFreqON
;
358 buf
[2] |= cOscFreq_4332kHz
;
361 buf
[2] |= cOscFreq_8664kHz
;
364 buf
[2] |= cOscFreq_12996kHz
;
367 buf
[2] |= cOscFreq_17328kHz
;
369 default: /* nothing */
373 dprintk(PREFIX
"writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n",
374 buf
[0], buf
[1], buf
[2]);
376 rc
= i2c_master_send(client
, buf
, 3);
378 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 saa6588_command
*a
= arg
;
389 /* --- close() for /dev/radio --- */
390 case SAA6588_CMD_CLOSE
:
391 s
->data_available_for_read
= 1;
392 wake_up_interruptible(&s
->read_queue
);
393 s
->data_available_for_read
= 0;
396 /* --- read() for /dev/radio --- */
397 case SAA6588_CMD_READ
:
400 /* --- poll() for /dev/radio --- */
401 case SAA6588_CMD_POLL
:
403 if (s
->data_available_for_read
)
404 a
->poll_mask
|= EPOLLIN
| EPOLLRDNORM
;
405 poll_wait(a
->instance
, &s
->read_queue
, a
->event_list
);
415 static int saa6588_g_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*vt
)
417 struct saa6588
*s
= to_saa6588(sd
);
419 vt
->capability
|= V4L2_TUNER_CAP_RDS
| V4L2_TUNER_CAP_RDS_BLOCK_IO
;
421 vt
->rxsubchans
|= V4L2_TUNER_SUB_RDS
;
425 static int saa6588_s_tuner(struct v4l2_subdev
*sd
, const struct v4l2_tuner
*vt
)
427 struct saa6588
*s
= to_saa6588(sd
);
429 saa6588_configure(s
);
433 /* ----------------------------------------------------------------------- */
435 static const struct v4l2_subdev_core_ops saa6588_core_ops
= {
436 .ioctl
= saa6588_ioctl
,
439 static const struct v4l2_subdev_tuner_ops saa6588_tuner_ops
= {
440 .g_tuner
= saa6588_g_tuner
,
441 .s_tuner
= saa6588_s_tuner
,
444 static const struct v4l2_subdev_ops saa6588_ops
= {
445 .core
= &saa6588_core_ops
,
446 .tuner
= &saa6588_tuner_ops
,
449 /* ---------------------------------------------------------------------- */
451 static int saa6588_probe(struct i2c_client
*client
,
452 const struct i2c_device_id
*id
)
455 struct v4l2_subdev
*sd
;
457 v4l_info(client
, "saa6588 found @ 0x%x (%s)\n",
458 client
->addr
<< 1, client
->adapter
->name
);
460 s
= devm_kzalloc(&client
->dev
, sizeof(*s
), GFP_KERNEL
);
464 s
->buf_size
= bufblocks
* 3;
466 s
->buffer
= devm_kzalloc(&client
->dev
, s
->buf_size
, GFP_KERNEL
);
467 if (s
->buffer
== NULL
)
470 v4l2_i2c_subdev_init(sd
, client
, &saa6588_ops
);
471 spin_lock_init(&s
->lock
);
475 s
->last_blocknum
= 0xff;
476 init_waitqueue_head(&s
->read_queue
);
477 s
->data_available_for_read
= 0;
479 saa6588_configure(s
);
481 /* start polling via eventd */
482 INIT_DELAYED_WORK(&s
->work
, saa6588_work
);
483 schedule_delayed_work(&s
->work
, 0);
487 static int saa6588_remove(struct i2c_client
*client
)
489 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
490 struct saa6588
*s
= to_saa6588(sd
);
492 v4l2_device_unregister_subdev(sd
);
494 cancel_delayed_work_sync(&s
->work
);
499 /* ----------------------------------------------------------------------- */
501 static const struct i2c_device_id saa6588_id
[] = {
505 MODULE_DEVICE_TABLE(i2c
, saa6588_id
);
507 static struct i2c_driver saa6588_driver
= {
511 .probe
= saa6588_probe
,
512 .remove
= saa6588_remove
,
513 .id_table
= saa6588_id
,
516 module_i2c_driver(saa6588_driver
);