treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / i2c / saa6588.c
blobecb491d5f2ab8a97430a8338e0b97b706e11546c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 Driver for SAA6588 RDS decoder
5 (c) 2005 Hans J. Koch
7 */
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>
26 /* insmod options */
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 /* ---------------------------------------------------------------------- */
51 #define UNSET (-1U)
52 #define PREFIX "saa6588: "
53 #define dprintk if (debug) printk
55 struct saa6588 {
56 struct v4l2_subdev sd;
57 struct delayed_work work;
58 spinlock_t lock;
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;
67 u8 sync;
70 static inline struct saa6588 *to_saa6588(struct v4l2_subdev *sd)
72 return container_of(sd, struct saa6588, sd);
75 /* ---------------------------------------------------------------------- */
78 * SAA6588 defines
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
89 /* bit 2 (RBDS) */
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
99 /* bit 5 (NWSY) */
100 #define cSyncNormal 0x00
101 #define cSyncRestart 0x20
103 /* bit 6 (TSQD) */
104 #define cSigQualityDetectOFF 0x00
105 #define cSigQualityDetectON 0x40
107 /* bit 7 (SQCM) */
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
129 /* bit 5 (SOSC) */
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)
143 int i;
145 if (s->rd_index == s->wr_index) {
146 if (debug > 2)
147 dprintk(PREFIX "Read: buffer empty.\n");
148 return false;
151 if (debug > 2) {
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);
159 s->rd_index += 3;
160 if (s->rd_index >= s->buf_size)
161 s->rd_index = 0;
162 s->block_count--;
164 if (debug > 2)
165 dprintk("%d blocks total.\n", s->block_count);
167 return true;
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];
174 unsigned long flags;
175 unsigned int rd_blocks;
176 unsigned int i;
178 a->result = 0;
179 if (!a->buffer)
180 return;
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) {
186 a->result = -EINTR;
187 return;
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);
197 if (!rd_blocks)
198 return;
200 for (i = 0; i < rd_blocks; i++) {
201 bool got_block;
203 spin_lock_irqsave(&s->lock, flags);
204 got_block = block_from_buf(s, buf);
205 spin_unlock_irqrestore(&s->lock, flags);
206 if (!got_block)
207 break;
208 if (copy_to_user(buf_ptr, buf, 3)) {
209 a->result = -EFAULT;
210 return;
212 buf_ptr += 3;
213 a->result += 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)
222 unsigned int i;
224 if (debug > 3)
225 dprintk(PREFIX "New block: ");
227 for (i = 0; i < 3; ++i) {
228 if (debug > 3)
229 dprintk("0x%02x ", blockbuf[i]);
230 s->buffer[s->wr_index] = blockbuf[i];
231 s->wr_index++;
234 if (s->wr_index >= s->buf_size)
235 s->wr_index = 0;
237 if (s->wr_index == s->rd_index) {
238 s->rd_index += 3;
239 if (s->rd_index >= s->buf_size)
240 s->rd_index = 0;
241 } else
242 s->block_count++;
244 if (debug > 3)
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);
251 unsigned long flags;
252 unsigned char tmpbuf[6];
253 unsigned char blocknum;
254 unsigned char tmp;
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)) {
259 if (debug > 1)
260 dprintk(PREFIX "read error!\n");
261 return;
264 s->sync = tmpbuf[0] & 0x10;
265 if (!s->sync)
266 return;
267 blocknum = tmpbuf[0] >> 5;
268 if (blocknum == s->last_blocknum) {
269 if (debug > 3)
270 dprintk("Saw block %d again.\n", blocknum);
271 return;
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' */
295 if (blocknum == 6)
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;
303 tmp = blocknum;
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);
322 saa6588_i2c_poll(s);
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];
330 int rc;
332 buf[0] = cSyncRestart;
333 if (mmbs)
334 buf[0] |= cProcessingModeRBDS;
336 buf[1] = cFlywheelDefault;
337 switch (plvl) {
338 case 0:
339 buf[1] |= cPauseLevel_11mV;
340 break;
341 case 1:
342 buf[1] |= cPauseLevel_17mV;
343 break;
344 case 2:
345 buf[1] |= cPauseLevel_27mV;
346 break;
347 case 3:
348 buf[1] |= cPauseLevel_43mV;
349 break;
350 default: /* nothing */
351 break;
354 buf[2] = cQualityDetectDefault | cSelectOscFreqON;
356 switch (xtal) {
357 case 0:
358 buf[2] |= cOscFreq_4332kHz;
359 break;
360 case 1:
361 buf[2] |= cOscFreq_8664kHz;
362 break;
363 case 2:
364 buf[2] |= cOscFreq_12996kHz;
365 break;
366 case 3:
367 buf[2] |= cOscFreq_17328kHz;
368 break;
369 default: /* nothing */
370 break;
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);
377 if (rc != 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;
388 switch (cmd) {
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;
394 a->result = 0;
395 break;
396 /* --- read() for /dev/radio --- */
397 case SAA6588_CMD_READ:
398 read_from_buf(s, a);
399 break;
400 /* --- poll() for /dev/radio --- */
401 case SAA6588_CMD_POLL:
402 a->poll_mask = 0;
403 if (s->data_available_for_read)
404 a->poll_mask |= EPOLLIN | EPOLLRDNORM;
405 poll_wait(a->instance, &s->read_queue, a->event_list);
406 break;
408 default:
409 /* nothing */
410 return -ENOIOCTLCMD;
412 return 0;
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;
420 if (s->sync)
421 vt->rxsubchans |= V4L2_TUNER_SUB_RDS;
422 return 0;
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);
430 return 0;
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)
454 struct saa6588 *s;
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);
461 if (s == NULL)
462 return -ENOMEM;
464 s->buf_size = bufblocks * 3;
466 s->buffer = devm_kzalloc(&client->dev, s->buf_size, GFP_KERNEL);
467 if (s->buffer == NULL)
468 return -ENOMEM;
469 sd = &s->sd;
470 v4l2_i2c_subdev_init(sd, client, &saa6588_ops);
471 spin_lock_init(&s->lock);
472 s->block_count = 0;
473 s->wr_index = 0;
474 s->rd_index = 0;
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);
484 return 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);
496 return 0;
499 /* ----------------------------------------------------------------------- */
501 static const struct i2c_device_id saa6588_id[] = {
502 { "saa6588", 0 },
505 MODULE_DEVICE_TABLE(i2c, saa6588_id);
507 static struct i2c_driver saa6588_driver = {
508 .driver = {
509 .name = "saa6588",
511 .probe = saa6588_probe,
512 .remove = saa6588_remove,
513 .id_table = saa6588_id,
516 module_i2c_driver(saa6588_driver);