radio-si470x-i2c.c 14 KB

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