radio-si470x-i2c.c 13 KB

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