1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 Driver for SAA6588 RDS decoder 4 5 (c) 2005 Hans J. Koch 6 7 */ 8 9 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> 21 22 #include <media/i2c/saa6588.h> 23 #include <media/v4l2-device.h> 24 25 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; 32 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"); 43 44 MODULE_DESCRIPTION("v4l2 driver module for SAA6588 RDS decoder"); 45 MODULE_AUTHOR("Hans J. Koch <koch@hjk-az.de>"); 46 47 MODULE_LICENSE("GPL"); 48 49 /* ---------------------------------------------------------------------- */ 50 51 #define UNSET (-1U) 52 53 struct saa6588 { 54 struct v4l2_subdev sd; 55 struct delayed_work work; 56 spinlock_t lock; 57 unsigned char *buffer; 58 unsigned int buf_size; 59 unsigned int rd_index; 60 unsigned int wr_index; 61 unsigned int block_count; 62 unsigned char last_blocknum; 63 wait_queue_head_t read_queue; 64 int data_available_for_read; 65 u8 sync; 66 }; 67 68 static inline struct saa6588 *to_saa6588(struct v4l2_subdev *sd) 69 { 70 return container_of(sd, struct saa6588, sd); 71 } 72 73 /* ---------------------------------------------------------------------- */ 74 75 /* 76 * SAA6588 defines 77 */ 78 79 /* Initialization and mode control byte (0w) */ 80 81 /* bit 0+1 (DAC0/DAC1) */ 82 #define cModeStandard 0x00 83 #define cModeFastPI 0x01 84 #define cModeReducedRequest 0x02 85 #define cModeInvalid 0x03 86 87 /* bit 2 (RBDS) */ 88 #define cProcessingModeRDS 0x00 89 #define cProcessingModeRBDS 0x04 90 91 /* bit 3+4 (SYM0/SYM1) */ 92 #define cErrCorrectionNone 0x00 93 #define cErrCorrection2Bits 0x08 94 #define cErrCorrection5Bits 0x10 95 #define cErrCorrectionNoneRBDS 0x18 96 97 /* bit 5 (NWSY) */ 98 #define cSyncNormal 0x00 99 #define cSyncRestart 0x20 100 101 /* bit 6 (TSQD) */ 102 #define cSigQualityDetectOFF 0x00 103 #define cSigQualityDetectON 0x40 104 105 /* bit 7 (SQCM) */ 106 #define cSigQualityTriggered 0x00 107 #define cSigQualityContinous 0x80 108 109 /* Pause level and flywheel control byte (1w) */ 110 111 /* bits 0..5 (FEB0..FEB5) */ 112 #define cFlywheelMaxBlocksMask 0x3F 113 #define cFlywheelDefault 0x20 114 115 /* bits 6+7 (PL0/PL1) */ 116 #define cPauseLevel_11mV 0x00 117 #define cPauseLevel_17mV 0x40 118 #define cPauseLevel_27mV 0x80 119 #define cPauseLevel_43mV 0xC0 120 121 /* Pause time/oscillator frequency/quality detector control byte (1w) */ 122 123 /* bits 0..4 (SQS0..SQS4) */ 124 #define cQualityDetectSensMask 0x1F 125 #define cQualityDetectDefault 0x0F 126 127 /* bit 5 (SOSC) */ 128 #define cSelectOscFreqOFF 0x00 129 #define cSelectOscFreqON 0x20 130 131 /* bit 6+7 (PTF0/PTF1) */ 132 #define cOscFreq_4332kHz 0x00 133 #define cOscFreq_8664kHz 0x40 134 #define cOscFreq_12996kHz 0x80 135 #define cOscFreq_17328kHz 0xC0 136 137 /* ---------------------------------------------------------------------- */ 138 139 static bool block_from_buf(struct saa6588 *s, unsigned char *buf) 140 { 141 int i; 142 143 if (s->rd_index == s->wr_index) { 144 if (debug > 2) 145 v4l2_info(&s->sd, "Read: buffer empty.\n"); 146 return false; 147 } 148 149 if (debug > 2) { 150 v4l2_info(&s->sd, "Read: "); 151 for (i = s->rd_index; i < s->rd_index + 3; i++) 152 v4l2_info(&s->sd, "0x%02x ", s->buffer[i]); 153 } 154 155 memcpy(buf, &s->buffer[s->rd_index], 3); 156 157 s->rd_index += 3; 158 if (s->rd_index >= s->buf_size) 159 s->rd_index = 0; 160 s->block_count--; 161 162 if (debug > 2) 163 v4l2_info(&s->sd, "%d blocks total.\n", s->block_count); 164 165 return true; 166 } 167 168 static void read_from_buf(struct saa6588 *s, struct saa6588_command *a) 169 { 170 unsigned char __user *buf_ptr = a->buffer; 171 unsigned char buf[3]; 172 unsigned long flags; 173 unsigned int rd_blocks; 174 unsigned int i; 175 176 a->result = 0; 177 if (!a->buffer) 178 return; 179 180 while (!a->nonblocking && !s->data_available_for_read) { 181 int ret = wait_event_interruptible(s->read_queue, 182 s->data_available_for_read); 183 if (ret == -ERESTARTSYS) { 184 a->result = -EINTR; 185 return; 186 } 187 } 188 189 rd_blocks = a->block_count; 190 spin_lock_irqsave(&s->lock, flags); 191 if (rd_blocks > s->block_count) 192 rd_blocks = s->block_count; 193 spin_unlock_irqrestore(&s->lock, flags); 194 195 if (!rd_blocks) 196 return; 197 198 for (i = 0; i < rd_blocks; i++) { 199 bool got_block; 200 201 spin_lock_irqsave(&s->lock, flags); 202 got_block = block_from_buf(s, buf); 203 spin_unlock_irqrestore(&s->lock, flags); 204 if (!got_block) 205 break; 206 if (copy_to_user(buf_ptr, buf, 3)) { 207 a->result = -EFAULT; 208 return; 209 } 210 buf_ptr += 3; 211 a->result += 3; 212 } 213 spin_lock_irqsave(&s->lock, flags); 214 s->data_available_for_read = (s->block_count > 0); 215 spin_unlock_irqrestore(&s->lock, flags); 216 } 217 218 static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf) 219 { 220 unsigned int i; 221 222 if (debug > 3) 223 v4l2_info(&s->sd, "New block: "); 224 225 for (i = 0; i < 3; ++i) { 226 if (debug > 3) 227 v4l2_info(&s->sd, "0x%02x ", blockbuf[i]); 228 s->buffer[s->wr_index] = blockbuf[i]; 229 s->wr_index++; 230 } 231 232 if (s->wr_index >= s->buf_size) 233 s->wr_index = 0; 234 235 if (s->wr_index == s->rd_index) { 236 s->rd_index += 3; 237 if (s->rd_index >= s->buf_size) 238 s->rd_index = 0; 239 } else 240 s->block_count++; 241 242 if (debug > 3) 243 v4l2_info(&s->sd, "%d blocks total.\n", s->block_count); 244 } 245 246 static void saa6588_i2c_poll(struct saa6588 *s) 247 { 248 struct i2c_client *client = v4l2_get_subdevdata(&s->sd); 249 unsigned long flags; 250 unsigned char tmpbuf[6]; 251 unsigned char blocknum; 252 unsigned char tmp; 253 254 /* Although we only need 3 bytes, we have to read at least 6. 255 SAA6588 returns garbage otherwise. */ 256 if (6 != i2c_master_recv(client, &tmpbuf[0], 6)) { 257 if (debug > 1) 258 v4l2_info(&s->sd, "read error!\n"); 259 return; 260 } 261 262 s->sync = tmpbuf[0] & 0x10; 263 if (!s->sync) 264 return; 265 blocknum = tmpbuf[0] >> 5; 266 if (blocknum == s->last_blocknum) { 267 if (debug > 3) 268 v4l2_info(&s->sd, "Saw block %d again.\n", blocknum); 269 return; 270 } 271 272 s->last_blocknum = blocknum; 273 274 /* 275 Byte order according to v4l2 specification: 276 277 Byte 0: Least Significant Byte of RDS Block 278 Byte 1: Most Significant Byte of RDS Block 279 Byte 2 Bit 7: Error bit. Indicates that an uncorrectable error 280 occurred during reception of this block. 281 Bit 6: Corrected bit. Indicates that an error was 282 corrected for this data block. 283 Bits 5-3: Same as bits 0-2. 284 Bits 2-0: Block number. 285 286 SAA6588 byte order is Status-MSB-LSB, so we have to swap the 287 first and the last of the 3 bytes block. 288 */ 289 290 swap(tmpbuf[2], tmpbuf[0]); 291 292 /* Map 'Invalid block E' to 'Invalid Block' */ 293 if (blocknum == 6) 294 blocknum = V4L2_RDS_BLOCK_INVALID; 295 /* And if are not in mmbs mode, then 'Block E' is also mapped 296 to 'Invalid Block'. As far as I can tell MMBS is discontinued, 297 and if there is ever a need to support E blocks, then please 298 contact the linux-media mailinglist. */ 299 else if (!mmbs && blocknum == 5) 300 blocknum = V4L2_RDS_BLOCK_INVALID; 301 tmp = blocknum; 302 tmp |= blocknum << 3; /* Received offset == Offset Name (OK ?) */ 303 if ((tmpbuf[2] & 0x03) == 0x03) 304 tmp |= V4L2_RDS_BLOCK_ERROR; /* uncorrectable error */ 305 else if ((tmpbuf[2] & 0x03) != 0x00) 306 tmp |= V4L2_RDS_BLOCK_CORRECTED; /* corrected error */ 307 tmpbuf[2] = tmp; /* Is this enough ? Should we also check other bits ? */ 308 309 spin_lock_irqsave(&s->lock, flags); 310 block_to_buf(s, tmpbuf); 311 spin_unlock_irqrestore(&s->lock, flags); 312 s->data_available_for_read = 1; 313 wake_up_interruptible(&s->read_queue); 314 } 315 316 static void saa6588_work(struct work_struct *work) 317 { 318 struct saa6588 *s = container_of(work, struct saa6588, work.work); 319 320 saa6588_i2c_poll(s); 321 schedule_delayed_work(&s->work, msecs_to_jiffies(20)); 322 } 323 324 static void saa6588_configure(struct saa6588 *s) 325 { 326 struct i2c_client *client = v4l2_get_subdevdata(&s->sd); 327 unsigned char buf[3]; 328 int rc; 329 330 buf[0] = cSyncRestart; 331 if (mmbs) 332 buf[0] |= cProcessingModeRBDS; 333 334 buf[1] = cFlywheelDefault; 335 switch (plvl) { 336 case 0: 337 buf[1] |= cPauseLevel_11mV; 338 break; 339 case 1: 340 buf[1] |= cPauseLevel_17mV; 341 break; 342 case 2: 343 buf[1] |= cPauseLevel_27mV; 344 break; 345 case 3: 346 buf[1] |= cPauseLevel_43mV; 347 break; 348 default: /* nothing */ 349 break; 350 } 351 352 buf[2] = cQualityDetectDefault | cSelectOscFreqON; 353 354 switch (xtal) { 355 case 0: 356 buf[2] |= cOscFreq_4332kHz; 357 break; 358 case 1: 359 buf[2] |= cOscFreq_8664kHz; 360 break; 361 case 2: 362 buf[2] |= cOscFreq_12996kHz; 363 break; 364 case 3: 365 buf[2] |= cOscFreq_17328kHz; 366 break; 367 default: /* nothing */ 368 break; 369 } 370 371 if (debug) 372 v4l2_info(&s->sd, "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n", 373 buf[0], buf[1], buf[2]); 374 375 rc = i2c_master_send(client, buf, 3); 376 if (rc != 3) 377 v4l2_info(&s->sd, "i2c i/o error: rc == %d (should be 3)\n", rc); 378 } 379 380 /* ---------------------------------------------------------------------- */ 381 382 static long saa6588_command(struct v4l2_subdev *sd, unsigned int cmd, void *arg) 383 { 384 struct saa6588 *s = to_saa6588(sd); 385 struct saa6588_command *a = arg; 386 387 switch (cmd) { 388 /* --- close() for /dev/radio --- */ 389 case SAA6588_CMD_CLOSE: 390 s->data_available_for_read = 1; 391 wake_up_interruptible(&s->read_queue); 392 s->data_available_for_read = 0; 393 a->result = 0; 394 break; 395 /* --- read() for /dev/radio --- */ 396 case SAA6588_CMD_READ: 397 read_from_buf(s, a); 398 break; 399 /* --- poll() for /dev/radio --- */ 400 case SAA6588_CMD_POLL: 401 a->poll_mask = 0; 402 if (s->data_available_for_read) 403 a->poll_mask |= EPOLLIN | EPOLLRDNORM; 404 poll_wait(a->instance, &s->read_queue, a->event_list); 405 break; 406 407 default: 408 /* nothing */ 409 return -ENOIOCTLCMD; 410 } 411 return 0; 412 } 413 414 static int saa6588_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt) 415 { 416 struct saa6588 *s = to_saa6588(sd); 417 418 vt->capability |= V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO; 419 if (s->sync) 420 vt->rxsubchans |= V4L2_TUNER_SUB_RDS; 421 return 0; 422 } 423 424 static int saa6588_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt) 425 { 426 struct saa6588 *s = to_saa6588(sd); 427 428 saa6588_configure(s); 429 return 0; 430 } 431 432 /* ----------------------------------------------------------------------- */ 433 434 static const struct v4l2_subdev_core_ops saa6588_core_ops = { 435 .command = saa6588_command, 436 }; 437 438 static const struct v4l2_subdev_tuner_ops saa6588_tuner_ops = { 439 .g_tuner = saa6588_g_tuner, 440 .s_tuner = saa6588_s_tuner, 441 }; 442 443 static const struct v4l2_subdev_ops saa6588_ops = { 444 .core = &saa6588_core_ops, 445 .tuner = &saa6588_tuner_ops, 446 }; 447 448 /* ---------------------------------------------------------------------- */ 449 450 static int saa6588_probe(struct i2c_client *client) 451 { 452 struct saa6588 *s; 453 struct v4l2_subdev *sd; 454 455 v4l_info(client, "saa6588 found @ 0x%x (%s)\n", 456 client->addr << 1, client->adapter->name); 457 458 s = devm_kzalloc(&client->dev, sizeof(*s), GFP_KERNEL); 459 if (s == NULL) 460 return -ENOMEM; 461 462 s->buf_size = bufblocks * 3; 463 464 s->buffer = devm_kzalloc(&client->dev, s->buf_size, GFP_KERNEL); 465 if (s->buffer == NULL) 466 return -ENOMEM; 467 sd = &s->sd; 468 v4l2_i2c_subdev_init(sd, client, &saa6588_ops); 469 spin_lock_init(&s->lock); 470 s->block_count = 0; 471 s->wr_index = 0; 472 s->rd_index = 0; 473 s->last_blocknum = 0xff; 474 init_waitqueue_head(&s->read_queue); 475 s->data_available_for_read = 0; 476 477 saa6588_configure(s); 478 479 /* start polling via eventd */ 480 INIT_DELAYED_WORK(&s->work, saa6588_work); 481 schedule_delayed_work(&s->work, 0); 482 return 0; 483 } 484 485 static void saa6588_remove(struct i2c_client *client) 486 { 487 struct v4l2_subdev *sd = i2c_get_clientdata(client); 488 struct saa6588 *s = to_saa6588(sd); 489 490 v4l2_device_unregister_subdev(sd); 491 492 cancel_delayed_work_sync(&s->work); 493 } 494 495 /* ----------------------------------------------------------------------- */ 496 497 static const struct i2c_device_id saa6588_id[] = { 498 { "saa6588" }, 499 { } 500 }; 501 MODULE_DEVICE_TABLE(i2c, saa6588_id); 502 503 static struct i2c_driver saa6588_driver = { 504 .driver = { 505 .name = "saa6588", 506 }, 507 .probe = saa6588_probe, 508 .remove = saa6588_remove, 509 .id_table = saa6588_id, 510 }; 511 512 module_i2c_driver(saa6588_driver); 513