radio-si470x-i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. /* driver definitions */
  24. #define DRIVER_AUTHOR "Joonyoung Shim <jy0922.shim@samsung.com>";
  25. #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 1)
  26. #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
  27. #define DRIVER_DESC "I2C radio driver for Si470x FM Radio Receivers"
  28. #define DRIVER_VERSION "1.0.1"
  29. /* kernel includes */
  30. #include <linux/i2c.h>
  31. #include <linux/slab.h>
  32. #include <linux/delay.h>
  33. #include <linux/interrupt.h>
  34. #include "radio-si470x.h"
  35. /* I2C Device ID List */
  36. static const struct i2c_device_id si470x_i2c_id[] = {
  37. /* Generic Entry */
  38. { "si470x", 0 },
  39. /* Terminating entry */
  40. { }
  41. };
  42. MODULE_DEVICE_TABLE(i2c, si470x_i2c_id);
  43. /**************************************************************************
  44. * Module Parameters
  45. **************************************************************************/
  46. /* Radio Nr */
  47. static int radio_nr = -1;
  48. module_param(radio_nr, int, 0444);
  49. MODULE_PARM_DESC(radio_nr, "Radio Nr");
  50. /* RDS buffer blocks */
  51. static unsigned int rds_buf = 100;
  52. module_param(rds_buf, uint, 0444);
  53. MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
  54. /* RDS maximum block errors */
  55. static unsigned short max_rds_errors = 1;
  56. /* 0 means 0 errors requiring correction */
  57. /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
  58. /* 2 means 3-5 errors requiring correction */
  59. /* 3 means 6+ errors or errors in checkword, correction not possible */
  60. module_param(max_rds_errors, ushort, 0644);
  61. MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
  62. /**************************************************************************
  63. * I2C Definitions
  64. **************************************************************************/
  65. /* Write starts with the upper byte of register 0x02 */
  66. #define WRITE_REG_NUM 8
  67. #define WRITE_INDEX(i) (i + 0x02)
  68. /* Read starts with the upper byte of register 0x0a */
  69. #define READ_REG_NUM RADIO_REGISTER_NUM
  70. #define READ_INDEX(i) ((i + RADIO_REGISTER_NUM - 0x0a) % READ_REG_NUM)
  71. /**************************************************************************
  72. * General Driver Functions - REGISTERs
  73. **************************************************************************/
  74. /*
  75. * si470x_get_register - read register
  76. */
  77. int si470x_get_register(struct si470x_device *radio, int regnr)
  78. {
  79. u16 buf[READ_REG_NUM];
  80. struct i2c_msg msgs[1] = {
  81. { radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM,
  82. (void *)buf },
  83. };
  84. if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
  85. return -EIO;
  86. radio->registers[regnr] = __be16_to_cpu(buf[READ_INDEX(regnr)]);
  87. return 0;
  88. }
  89. /*
  90. * si470x_set_register - write register
  91. */
  92. int si470x_set_register(struct si470x_device *radio, int regnr)
  93. {
  94. int i;
  95. u16 buf[WRITE_REG_NUM];
  96. struct i2c_msg msgs[1] = {
  97. { radio->client->addr, 0, sizeof(u16) * WRITE_REG_NUM,
  98. (void *)buf },
  99. };
  100. for (i = 0; i < WRITE_REG_NUM; i++)
  101. buf[i] = __cpu_to_be16(radio->registers[WRITE_INDEX(i)]);
  102. if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
  103. return -EIO;
  104. return 0;
  105. }
  106. /**************************************************************************
  107. * General Driver Functions - ENTIRE REGISTERS
  108. **************************************************************************/
  109. /*
  110. * si470x_get_all_registers - read entire registers
  111. */
  112. static int si470x_get_all_registers(struct si470x_device *radio)
  113. {
  114. int i;
  115. u16 buf[READ_REG_NUM];
  116. struct i2c_msg msgs[1] = {
  117. { radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM,
  118. (void *)buf },
  119. };
  120. if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
  121. return -EIO;
  122. for (i = 0; i < READ_REG_NUM; i++)
  123. radio->registers[i] = __be16_to_cpu(buf[READ_INDEX(i)]);
  124. return 0;
  125. }
  126. /**************************************************************************
  127. * General Driver Functions - DISCONNECT_CHECK
  128. **************************************************************************/
  129. /*
  130. * si470x_disconnect_check - check whether radio disconnects
  131. */
  132. int si470x_disconnect_check(struct si470x_device *radio)
  133. {
  134. return 0;
  135. }
  136. /**************************************************************************
  137. * File Operations Interface
  138. **************************************************************************/
  139. /*
  140. * si470x_fops_open - file open
  141. */
  142. int si470x_fops_open(struct file *file)
  143. {
  144. struct si470x_device *radio = video_drvdata(file);
  145. int retval = 0;
  146. mutex_lock(&radio->lock);
  147. radio->users++;
  148. if (radio->users == 1) {
  149. /* start radio */
  150. retval = si470x_start(radio);
  151. if (retval < 0)
  152. goto done;
  153. /* enable RDS / STC interrupt */
  154. radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDSIEN;
  155. radio->registers[SYSCONFIG1] |= SYSCONFIG1_STCIEN;
  156. radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_GPIO2;
  157. radio->registers[SYSCONFIG1] |= 0x1 << 2;
  158. retval = si470x_set_register(radio, SYSCONFIG1);
  159. }
  160. done:
  161. mutex_unlock(&radio->lock);
  162. return retval;
  163. }
  164. /*
  165. * si470x_fops_release - file release
  166. */
  167. int si470x_fops_release(struct file *file)
  168. {
  169. struct si470x_device *radio = video_drvdata(file);
  170. int retval = 0;
  171. /* safety check */
  172. if (!radio)
  173. return -ENODEV;
  174. mutex_lock(&radio->lock);
  175. radio->users--;
  176. if (radio->users == 0)
  177. /* stop radio */
  178. retval = si470x_stop(radio);
  179. mutex_unlock(&radio->lock);
  180. return retval;
  181. }
  182. /**************************************************************************
  183. * Video4Linux Interface
  184. **************************************************************************/
  185. /*
  186. * si470x_vidioc_querycap - query device capabilities
  187. */
  188. int si470x_vidioc_querycap(struct file *file, void *priv,
  189. struct v4l2_capability *capability)
  190. {
  191. strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
  192. strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
  193. capability->version = DRIVER_KERNEL_VERSION;
  194. capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
  195. V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  196. return 0;
  197. }
  198. /**************************************************************************
  199. * I2C Interface
  200. **************************************************************************/
  201. /*
  202. * si470x_i2c_interrupt_work - rds processing function
  203. */
  204. static void si470x_i2c_interrupt_work(struct work_struct *work)
  205. {
  206. struct si470x_device *radio = container_of(work,
  207. struct si470x_device, radio_work);
  208. unsigned char regnr;
  209. unsigned char blocknum;
  210. unsigned short bler; /* rds block errors */
  211. unsigned short rds;
  212. unsigned char tmpbuf[3];
  213. int retval = 0;
  214. /* check Seek/Tune Complete */
  215. retval = si470x_get_register(radio, STATUSRSSI);
  216. if (retval < 0)
  217. return;
  218. if (radio->registers[STATUSRSSI] & STATUSRSSI_STC)
  219. complete(&radio->completion);
  220. /* safety checks */
  221. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
  222. return;
  223. /* Update RDS registers */
  224. for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++) {
  225. retval = si470x_get_register(radio, STATUSRSSI + regnr);
  226. if (retval < 0)
  227. return;
  228. }
  229. /* get rds blocks */
  230. if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0)
  231. /* No RDS group ready, better luck next time */
  232. return;
  233. for (blocknum = 0; blocknum < 4; blocknum++) {
  234. switch (blocknum) {
  235. default:
  236. bler = (radio->registers[STATUSRSSI] &
  237. STATUSRSSI_BLERA) >> 9;
  238. rds = radio->registers[RDSA];
  239. break;
  240. case 1:
  241. bler = (radio->registers[READCHAN] &
  242. READCHAN_BLERB) >> 14;
  243. rds = radio->registers[RDSB];
  244. break;
  245. case 2:
  246. bler = (radio->registers[READCHAN] &
  247. READCHAN_BLERC) >> 12;
  248. rds = radio->registers[RDSC];
  249. break;
  250. case 3:
  251. bler = (radio->registers[READCHAN] &
  252. READCHAN_BLERD) >> 10;
  253. rds = radio->registers[RDSD];
  254. break;
  255. };
  256. /* Fill the V4L2 RDS buffer */
  257. put_unaligned_le16(rds, &tmpbuf);
  258. tmpbuf[2] = blocknum; /* offset name */
  259. tmpbuf[2] |= blocknum << 3; /* received offset */
  260. if (bler > max_rds_errors)
  261. tmpbuf[2] |= 0x80; /* uncorrectable errors */
  262. else if (bler > 0)
  263. tmpbuf[2] |= 0x40; /* corrected error(s) */
  264. /* copy RDS block to internal buffer */
  265. memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
  266. radio->wr_index += 3;
  267. /* wrap write pointer */
  268. if (radio->wr_index >= radio->buf_size)
  269. radio->wr_index = 0;
  270. /* check for overflow */
  271. if (radio->wr_index == radio->rd_index) {
  272. /* increment and wrap read pointer */
  273. radio->rd_index += 3;
  274. if (radio->rd_index >= radio->buf_size)
  275. radio->rd_index = 0;
  276. }
  277. }
  278. if (radio->wr_index != radio->rd_index)
  279. wake_up_interruptible(&radio->read_queue);
  280. }
  281. /*
  282. * si470x_i2c_interrupt - interrupt handler
  283. */
  284. static irqreturn_t si470x_i2c_interrupt(int irq, void *dev_id)
  285. {
  286. struct si470x_device *radio = dev_id;
  287. if (!work_pending(&radio->radio_work))
  288. schedule_work(&radio->radio_work);
  289. return IRQ_HANDLED;
  290. }
  291. /*
  292. * si470x_i2c_probe - probe for the device
  293. */
  294. static int __devinit si470x_i2c_probe(struct i2c_client *client,
  295. const struct i2c_device_id *id)
  296. {
  297. struct si470x_device *radio;
  298. int retval = 0;
  299. unsigned char version_warning = 0;
  300. /* private data allocation and initialization */
  301. radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
  302. if (!radio) {
  303. retval = -ENOMEM;
  304. goto err_initial;
  305. }
  306. INIT_WORK(&radio->radio_work, si470x_i2c_interrupt_work);
  307. radio->users = 0;
  308. radio->client = client;
  309. mutex_init(&radio->lock);
  310. /* video device allocation and initialization */
  311. radio->videodev = video_device_alloc();
  312. if (!radio->videodev) {
  313. retval = -ENOMEM;
  314. goto err_radio;
  315. }
  316. memcpy(radio->videodev, &si470x_viddev_template,
  317. sizeof(si470x_viddev_template));
  318. video_set_drvdata(radio->videodev, radio);
  319. /* power up : need 110ms */
  320. radio->registers[POWERCFG] = POWERCFG_ENABLE;
  321. if (si470x_set_register(radio, POWERCFG) < 0) {
  322. retval = -EIO;
  323. goto err_video;
  324. }
  325. msleep(110);
  326. /* get device and chip versions */
  327. if (si470x_get_all_registers(radio) < 0) {
  328. retval = -EIO;
  329. goto err_video;
  330. }
  331. dev_info(&client->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
  332. radio->registers[DEVICEID], radio->registers[CHIPID]);
  333. if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
  334. dev_warn(&client->dev,
  335. "This driver is known to work with "
  336. "firmware version %hu,\n", RADIO_FW_VERSION);
  337. dev_warn(&client->dev,
  338. "but the device has firmware version %hu.\n",
  339. radio->registers[CHIPID] & CHIPID_FIRMWARE);
  340. version_warning = 1;
  341. }
  342. /* give out version warning */
  343. if (version_warning == 1) {
  344. dev_warn(&client->dev,
  345. "If you have some trouble using this driver,\n");
  346. dev_warn(&client->dev,
  347. "please report to V4L ML at "
  348. "linux-media@vger.kernel.org\n");
  349. }
  350. /* set initial frequency */
  351. si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
  352. /* rds buffer allocation */
  353. radio->buf_size = rds_buf * 3;
  354. radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
  355. if (!radio->buffer) {
  356. retval = -EIO;
  357. goto err_video;
  358. }
  359. /* rds buffer configuration */
  360. radio->wr_index = 0;
  361. radio->rd_index = 0;
  362. init_waitqueue_head(&radio->read_queue);
  363. /* mark Seek/Tune Complete Interrupt enabled */
  364. radio->stci_enabled = true;
  365. init_completion(&radio->completion);
  366. retval = request_irq(client->irq, si470x_i2c_interrupt,
  367. IRQF_TRIGGER_FALLING, DRIVER_NAME, radio);
  368. if (retval) {
  369. dev_err(&client->dev, "Failed to register interrupt\n");
  370. goto err_rds;
  371. }
  372. /* register video device */
  373. retval = video_register_device(radio->videodev, VFL_TYPE_RADIO,
  374. radio_nr);
  375. if (retval) {
  376. dev_warn(&client->dev, "Could not register video device\n");
  377. goto err_all;
  378. }
  379. i2c_set_clientdata(client, radio);
  380. return 0;
  381. err_all:
  382. free_irq(client->irq, radio);
  383. err_rds:
  384. kfree(radio->buffer);
  385. err_video:
  386. video_device_release(radio->videodev);
  387. err_radio:
  388. kfree(radio);
  389. err_initial:
  390. return retval;
  391. }
  392. /*
  393. * si470x_i2c_remove - remove the device
  394. */
  395. static __devexit int si470x_i2c_remove(struct i2c_client *client)
  396. {
  397. struct si470x_device *radio = i2c_get_clientdata(client);
  398. free_irq(client->irq, radio);
  399. cancel_work_sync(&radio->radio_work);
  400. video_unregister_device(radio->videodev);
  401. kfree(radio);
  402. return 0;
  403. }
  404. #ifdef CONFIG_PM
  405. /*
  406. * si470x_i2c_suspend - suspend the device
  407. */
  408. static int si470x_i2c_suspend(struct i2c_client *client, pm_message_t mesg)
  409. {
  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 i2c_client *client)
  421. {
  422. struct si470x_device *radio = i2c_get_clientdata(client);
  423. /* power up : need 110ms */
  424. radio->registers[POWERCFG] |= POWERCFG_ENABLE;
  425. if (si470x_set_register(radio, POWERCFG) < 0)
  426. return -EIO;
  427. msleep(110);
  428. return 0;
  429. }
  430. #else
  431. #define si470x_i2c_suspend NULL
  432. #define si470x_i2c_resume NULL
  433. #endif
  434. /*
  435. * si470x_i2c_driver - i2c driver interface
  436. */
  437. static struct i2c_driver si470x_i2c_driver = {
  438. .driver = {
  439. .name = "si470x",
  440. .owner = THIS_MODULE,
  441. },
  442. .probe = si470x_i2c_probe,
  443. .remove = __devexit_p(si470x_i2c_remove),
  444. .suspend = si470x_i2c_suspend,
  445. .resume = si470x_i2c_resume,
  446. .id_table = si470x_i2c_id,
  447. };
  448. /**************************************************************************
  449. * Module Interface
  450. **************************************************************************/
  451. /*
  452. * si470x_i2c_init - module init
  453. */
  454. static int __init si470x_i2c_init(void)
  455. {
  456. printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
  457. return i2c_add_driver(&si470x_i2c_driver);
  458. }
  459. /*
  460. * si470x_i2c_exit - module exit
  461. */
  462. static void __exit si470x_i2c_exit(void)
  463. {
  464. i2c_del_driver(&si470x_i2c_driver);
  465. }
  466. module_init(si470x_i2c_init);
  467. module_exit(si470x_i2c_exit);
  468. MODULE_LICENSE("GPL");
  469. MODULE_AUTHOR(DRIVER_AUTHOR);
  470. MODULE_DESCRIPTION(DRIVER_DESC);
  471. MODULE_VERSION(DRIVER_VERSION);