1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * drivers/media/radio/si470x/radio-si470x-i2c.c 4 * 5 * I2C driver for radios with Silicon Labs Si470x FM Radio Receivers 6 * 7 * Copyright (c) 2009 Samsung Electronics Co.Ltd 8 * Author: Joonyoung Shim <jy0922.shim@samsung.com> 9 */ 10 11 12 /* driver definitions */ 13 #define DRIVER_AUTHOR "Joonyoung Shim <jy0922.shim@samsung.com>"; 14 #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver" 15 #define DRIVER_DESC "I2C radio driver for Si470x FM Radio Receivers" 16 #define DRIVER_VERSION "1.0.2" 17 18 /* kernel includes */ 19 #include <linux/i2c.h> 20 #include <linux/slab.h> 21 #include <linux/delay.h> 22 #include <linux/gpio/consumer.h> 23 #include <linux/interrupt.h> 24 25 #include "radio-si470x.h" 26 27 28 /* I2C Device ID List */ 29 static const struct i2c_device_id si470x_i2c_id[] = { 30 /* Generic Entry */ 31 { "si470x", 0 }, 32 /* Terminating entry */ 33 { } 34 }; 35 MODULE_DEVICE_TABLE(i2c, si470x_i2c_id); 36 37 38 /************************************************************************** 39 * Module Parameters 40 **************************************************************************/ 41 42 /* Radio Nr */ 43 static int radio_nr = -1; 44 module_param(radio_nr, int, 0444); 45 MODULE_PARM_DESC(radio_nr, "Radio Nr"); 46 47 /* RDS buffer blocks */ 48 static unsigned int rds_buf = 100; 49 module_param(rds_buf, uint, 0444); 50 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*"); 51 52 /* RDS maximum block errors */ 53 static unsigned short max_rds_errors = 1; 54 /* 0 means 0 errors requiring correction */ 55 /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */ 56 /* 2 means 3-5 errors requiring correction */ 57 /* 3 means 6+ errors or errors in checkword, correction not possible */ 58 module_param(max_rds_errors, ushort, 0644); 59 MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*"); 60 61 62 63 /************************************************************************** 64 * I2C Definitions 65 **************************************************************************/ 66 67 /* Write starts with the upper byte of register 0x02 */ 68 #define WRITE_REG_NUM 8 69 #define WRITE_INDEX(i) (i + 0x02) 70 71 /* Read starts with the upper byte of register 0x0a */ 72 #define READ_REG_NUM RADIO_REGISTER_NUM 73 #define READ_INDEX(i) ((i + RADIO_REGISTER_NUM - 0x0a) % READ_REG_NUM) 74 75 76 77 /************************************************************************** 78 * General Driver Functions - REGISTERs 79 **************************************************************************/ 80 81 /* 82 * si470x_get_register - read register 83 */ 84 static int si470x_get_register(struct si470x_device *radio, int regnr) 85 { 86 __be16 buf[READ_REG_NUM]; 87 struct i2c_msg msgs[1] = { 88 { 89 .addr = radio->client->addr, 90 .flags = I2C_M_RD, 91 .len = sizeof(u16) * READ_REG_NUM, 92 .buf = (void *)buf 93 }, 94 }; 95 96 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1) 97 return -EIO; 98 99 radio->registers[regnr] = __be16_to_cpu(buf[READ_INDEX(regnr)]); 100 101 return 0; 102 } 103 104 105 /* 106 * si470x_set_register - write register 107 */ 108 static int si470x_set_register(struct si470x_device *radio, int regnr) 109 { 110 int i; 111 __be16 buf[WRITE_REG_NUM]; 112 struct i2c_msg msgs[1] = { 113 { 114 .addr = radio->client->addr, 115 .len = sizeof(u16) * WRITE_REG_NUM, 116 .buf = (void *)buf 117 }, 118 }; 119 120 for (i = 0; i < WRITE_REG_NUM; i++) 121 buf[i] = __cpu_to_be16(radio->registers[WRITE_INDEX(i)]); 122 123 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1) 124 return -EIO; 125 126 return 0; 127 } 128 129 130 131 /************************************************************************** 132 * General Driver Functions - ENTIRE REGISTERS 133 **************************************************************************/ 134 135 /* 136 * si470x_get_all_registers - read entire registers 137 */ 138 static int si470x_get_all_registers(struct si470x_device *radio) 139 { 140 int i; 141 __be16 buf[READ_REG_NUM]; 142 struct i2c_msg msgs[1] = { 143 { 144 .addr = radio->client->addr, 145 .flags = I2C_M_RD, 146 .len = sizeof(u16) * READ_REG_NUM, 147 .buf = (void *)buf 148 }, 149 }; 150 151 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1) 152 return -EIO; 153 154 for (i = 0; i < READ_REG_NUM; i++) 155 radio->registers[i] = __be16_to_cpu(buf[READ_INDEX(i)]); 156 157 return 0; 158 } 159 160 161 162 /************************************************************************** 163 * File Operations Interface 164 **************************************************************************/ 165 166 /* 167 * si470x_fops_open - file open 168 */ 169 static int si470x_fops_open(struct file *file) 170 { 171 struct si470x_device *radio = video_drvdata(file); 172 int retval = v4l2_fh_open(file); 173 174 if (retval) 175 return retval; 176 177 if (v4l2_fh_is_singular_file(file)) { 178 /* start radio */ 179 retval = si470x_start(radio); 180 if (retval < 0) 181 goto done; 182 183 /* enable RDS / STC interrupt */ 184 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDSIEN; 185 radio->registers[SYSCONFIG1] |= SYSCONFIG1_STCIEN; 186 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_GPIO2; 187 radio->registers[SYSCONFIG1] |= 0x1 << 2; 188 retval = si470x_set_register(radio, SYSCONFIG1); 189 } 190 191 done: 192 if (retval) 193 v4l2_fh_release(file); 194 return retval; 195 } 196 197 198 /* 199 * si470x_fops_release - file release 200 */ 201 static int si470x_fops_release(struct file *file) 202 { 203 struct si470x_device *radio = video_drvdata(file); 204 205 if (v4l2_fh_is_singular_file(file)) 206 /* stop radio */ 207 si470x_stop(radio); 208 209 return v4l2_fh_release(file); 210 } 211 212 213 214 /************************************************************************** 215 * Video4Linux Interface 216 **************************************************************************/ 217 218 /* 219 * si470x_vidioc_querycap - query device capabilities 220 */ 221 static int si470x_vidioc_querycap(struct file *file, void *priv, 222 struct v4l2_capability *capability) 223 { 224 strscpy(capability->driver, DRIVER_NAME, sizeof(capability->driver)); 225 strscpy(capability->card, DRIVER_CARD, sizeof(capability->card)); 226 capability->device_caps = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_READWRITE | 227 V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE; 228 capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS; 229 230 return 0; 231 } 232 233 234 235 /************************************************************************** 236 * I2C Interface 237 **************************************************************************/ 238 239 /* 240 * si470x_i2c_interrupt - interrupt handler 241 */ 242 static irqreturn_t si470x_i2c_interrupt(int irq, void *dev_id) 243 { 244 struct si470x_device *radio = dev_id; 245 unsigned char regnr; 246 unsigned char blocknum; 247 unsigned short bler; /* rds block errors */ 248 unsigned short rds; 249 unsigned char tmpbuf[3]; 250 int retval = 0; 251 252 /* check Seek/Tune Complete */ 253 retval = si470x_get_register(radio, STATUSRSSI); 254 if (retval < 0) 255 goto end; 256 257 if (radio->registers[STATUSRSSI] & STATUSRSSI_STC) 258 complete(&radio->completion); 259 260 /* safety checks */ 261 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) 262 goto end; 263 264 /* Update RDS registers */ 265 for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++) { 266 retval = si470x_get_register(radio, STATUSRSSI + regnr); 267 if (retval < 0) 268 goto end; 269 } 270 271 /* get rds blocks */ 272 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) 273 /* No RDS group ready, better luck next time */ 274 goto end; 275 276 for (blocknum = 0; blocknum < 4; blocknum++) { 277 switch (blocknum) { 278 default: 279 bler = (radio->registers[STATUSRSSI] & 280 STATUSRSSI_BLERA) >> 9; 281 rds = radio->registers[RDSA]; 282 break; 283 case 1: 284 bler = (radio->registers[READCHAN] & 285 READCHAN_BLERB) >> 14; 286 rds = radio->registers[RDSB]; 287 break; 288 case 2: 289 bler = (radio->registers[READCHAN] & 290 READCHAN_BLERC) >> 12; 291 rds = radio->registers[RDSC]; 292 break; 293 case 3: 294 bler = (radio->registers[READCHAN] & 295 READCHAN_BLERD) >> 10; 296 rds = radio->registers[RDSD]; 297 break; 298 } 299 300 /* Fill the V4L2 RDS buffer */ 301 put_unaligned_le16(rds, &tmpbuf); 302 tmpbuf[2] = blocknum; /* offset name */ 303 tmpbuf[2] |= blocknum << 3; /* received offset */ 304 if (bler > max_rds_errors) 305 tmpbuf[2] |= 0x80; /* uncorrectable errors */ 306 else if (bler > 0) 307 tmpbuf[2] |= 0x40; /* corrected error(s) */ 308 309 /* copy RDS block to internal buffer */ 310 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3); 311 radio->wr_index += 3; 312 313 /* wrap write pointer */ 314 if (radio->wr_index >= radio->buf_size) 315 radio->wr_index = 0; 316 317 /* check for overflow */ 318 if (radio->wr_index == radio->rd_index) { 319 /* increment and wrap read pointer */ 320 radio->rd_index += 3; 321 if (radio->rd_index >= radio->buf_size) 322 radio->rd_index = 0; 323 } 324 } 325 326 if (radio->wr_index != radio->rd_index) 327 wake_up_interruptible(&radio->read_queue); 328 329 end: 330 return IRQ_HANDLED; 331 } 332 333 334 /* 335 * si470x_i2c_probe - probe for the device 336 */ 337 static int si470x_i2c_probe(struct i2c_client *client, 338 const struct i2c_device_id *id) 339 { 340 struct si470x_device *radio; 341 int retval = 0; 342 unsigned char version_warning = 0; 343 344 /* private data allocation and initialization */ 345 radio = devm_kzalloc(&client->dev, sizeof(*radio), GFP_KERNEL); 346 if (!radio) { 347 retval = -ENOMEM; 348 goto err_initial; 349 } 350 351 radio->client = client; 352 radio->band = 1; /* Default to 76 - 108 MHz */ 353 mutex_init(&radio->lock); 354 init_completion(&radio->completion); 355 356 radio->get_register = si470x_get_register; 357 radio->set_register = si470x_set_register; 358 radio->fops_open = si470x_fops_open; 359 radio->fops_release = si470x_fops_release; 360 radio->vidioc_querycap = si470x_vidioc_querycap; 361 362 retval = v4l2_device_register(&client->dev, &radio->v4l2_dev); 363 if (retval < 0) { 364 dev_err(&client->dev, "couldn't register v4l2_device\n"); 365 goto err_initial; 366 } 367 368 v4l2_ctrl_handler_init(&radio->hdl, 2); 369 v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops, 370 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); 371 v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops, 372 V4L2_CID_AUDIO_VOLUME, 0, 15, 1, 15); 373 if (radio->hdl.error) { 374 retval = radio->hdl.error; 375 dev_err(&client->dev, "couldn't register control\n"); 376 goto err_dev; 377 } 378 379 /* video device initialization */ 380 radio->videodev = si470x_viddev_template; 381 radio->videodev.ctrl_handler = &radio->hdl; 382 radio->videodev.lock = &radio->lock; 383 radio->videodev.v4l2_dev = &radio->v4l2_dev; 384 radio->videodev.release = video_device_release_empty; 385 video_set_drvdata(&radio->videodev, radio); 386 387 radio->gpio_reset = devm_gpiod_get_optional(&client->dev, "reset", 388 GPIOD_OUT_LOW); 389 if (IS_ERR(radio->gpio_reset)) { 390 retval = PTR_ERR(radio->gpio_reset); 391 dev_err(&client->dev, "Failed to request gpio: %d\n", retval); 392 goto err_all; 393 } 394 395 if (radio->gpio_reset) 396 gpiod_set_value(radio->gpio_reset, 1); 397 398 /* power up : need 110ms */ 399 radio->registers[POWERCFG] = POWERCFG_ENABLE; 400 if (si470x_set_register(radio, POWERCFG) < 0) { 401 retval = -EIO; 402 goto err_all; 403 } 404 msleep(110); 405 406 /* get device and chip versions */ 407 if (si470x_get_all_registers(radio) < 0) { 408 retval = -EIO; 409 goto err_all; 410 } 411 dev_info(&client->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n", 412 radio->registers[DEVICEID], radio->registers[SI_CHIPID]); 413 if ((radio->registers[SI_CHIPID] & SI_CHIPID_FIRMWARE) < RADIO_FW_VERSION) { 414 dev_warn(&client->dev, 415 "This driver is known to work with firmware version %hu,\n", 416 RADIO_FW_VERSION); 417 dev_warn(&client->dev, 418 "but the device has firmware version %hu.\n", 419 radio->registers[SI_CHIPID] & SI_CHIPID_FIRMWARE); 420 version_warning = 1; 421 } 422 423 /* give out version warning */ 424 if (version_warning == 1) { 425 dev_warn(&client->dev, 426 "If you have some trouble using this driver,\n"); 427 dev_warn(&client->dev, 428 "please report to V4L ML at linux-media@vger.kernel.org\n"); 429 } 430 431 /* set initial frequency */ 432 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */ 433 434 /* rds buffer allocation */ 435 radio->buf_size = rds_buf * 3; 436 radio->buffer = devm_kmalloc(&client->dev, radio->buf_size, GFP_KERNEL); 437 if (!radio->buffer) { 438 retval = -EIO; 439 goto err_all; 440 } 441 442 /* rds buffer configuration */ 443 radio->wr_index = 0; 444 radio->rd_index = 0; 445 init_waitqueue_head(&radio->read_queue); 446 447 retval = devm_request_threaded_irq(&client->dev, client->irq, NULL, 448 si470x_i2c_interrupt, 449 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 450 DRIVER_NAME, radio); 451 if (retval) { 452 dev_err(&client->dev, "Failed to register interrupt\n"); 453 goto err_all; 454 } 455 456 /* register video device */ 457 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, 458 radio_nr); 459 if (retval) { 460 dev_warn(&client->dev, "Could not register video device\n"); 461 goto err_all; 462 } 463 i2c_set_clientdata(client, radio); 464 465 return 0; 466 err_all: 467 v4l2_ctrl_handler_free(&radio->hdl); 468 err_dev: 469 v4l2_device_unregister(&radio->v4l2_dev); 470 err_initial: 471 return retval; 472 } 473 474 475 /* 476 * si470x_i2c_remove - remove the device 477 */ 478 static int si470x_i2c_remove(struct i2c_client *client) 479 { 480 struct si470x_device *radio = i2c_get_clientdata(client); 481 482 video_unregister_device(&radio->videodev); 483 484 if (radio->gpio_reset) 485 gpiod_set_value(radio->gpio_reset, 0); 486 487 return 0; 488 } 489 490 491 #ifdef CONFIG_PM_SLEEP 492 /* 493 * si470x_i2c_suspend - suspend the device 494 */ 495 static int si470x_i2c_suspend(struct device *dev) 496 { 497 struct i2c_client *client = to_i2c_client(dev); 498 struct si470x_device *radio = i2c_get_clientdata(client); 499 500 /* power down */ 501 radio->registers[POWERCFG] |= POWERCFG_DISABLE; 502 if (si470x_set_register(radio, POWERCFG) < 0) 503 return -EIO; 504 505 return 0; 506 } 507 508 509 /* 510 * si470x_i2c_resume - resume the device 511 */ 512 static int si470x_i2c_resume(struct device *dev) 513 { 514 struct i2c_client *client = to_i2c_client(dev); 515 struct si470x_device *radio = i2c_get_clientdata(client); 516 517 /* power up : need 110ms */ 518 radio->registers[POWERCFG] |= POWERCFG_ENABLE; 519 if (si470x_set_register(radio, POWERCFG) < 0) 520 return -EIO; 521 msleep(110); 522 523 return 0; 524 } 525 526 static SIMPLE_DEV_PM_OPS(si470x_i2c_pm, si470x_i2c_suspend, si470x_i2c_resume); 527 #endif 528 529 #if IS_ENABLED(CONFIG_OF) 530 static const struct of_device_id si470x_of_match[] = { 531 { .compatible = "silabs,si470x" }, 532 { }, 533 }; 534 MODULE_DEVICE_TABLE(of, si470x_of_match); 535 #endif 536 537 /* 538 * si470x_i2c_driver - i2c driver interface 539 */ 540 static struct i2c_driver si470x_i2c_driver = { 541 .driver = { 542 .name = "si470x", 543 .of_match_table = of_match_ptr(si470x_of_match), 544 #ifdef CONFIG_PM_SLEEP 545 .pm = &si470x_i2c_pm, 546 #endif 547 }, 548 .probe = si470x_i2c_probe, 549 .remove = si470x_i2c_remove, 550 .id_table = si470x_i2c_id, 551 }; 552 553 module_i2c_driver(si470x_i2c_driver); 554 555 MODULE_LICENSE("GPL"); 556 MODULE_AUTHOR(DRIVER_AUTHOR); 557 MODULE_DESCRIPTION(DRIVER_DESC); 558 MODULE_VERSION(DRIVER_VERSION); 559