radio-si470x-i2c.c 14 KB

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