radio-si470x-i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. __be16 buf[READ_REG_NUM];
  79. struct i2c_msg msgs[1] = {
  80. {
  81. .addr = radio->client->addr,
  82. .flags = I2C_M_RD,
  83. .len = sizeof(u16) * READ_REG_NUM,
  84. .buf = (void *)buf
  85. },
  86. };
  87. if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
  88. return -EIO;
  89. radio->registers[regnr] = __be16_to_cpu(buf[READ_INDEX(regnr)]);
  90. return 0;
  91. }
  92. /*
  93. * si470x_set_register - write register
  94. */
  95. int si470x_set_register(struct si470x_device *radio, int regnr)
  96. {
  97. int i;
  98. __be16 buf[WRITE_REG_NUM];
  99. struct i2c_msg msgs[1] = {
  100. {
  101. .addr = radio->client->addr,
  102. .len = sizeof(u16) * WRITE_REG_NUM,
  103. .buf = (void *)buf
  104. },
  105. };
  106. for (i = 0; i < WRITE_REG_NUM; i++)
  107. buf[i] = __cpu_to_be16(radio->registers[WRITE_INDEX(i)]);
  108. if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
  109. return -EIO;
  110. return 0;
  111. }
  112. /**************************************************************************
  113. * General Driver Functions - ENTIRE REGISTERS
  114. **************************************************************************/
  115. /*
  116. * si470x_get_all_registers - read entire registers
  117. */
  118. static int si470x_get_all_registers(struct si470x_device *radio)
  119. {
  120. int i;
  121. __be16 buf[READ_REG_NUM];
  122. struct i2c_msg msgs[1] = {
  123. {
  124. .addr = radio->client->addr,
  125. .flags = I2C_M_RD,
  126. .len = sizeof(u16) * READ_REG_NUM,
  127. .buf = (void *)buf
  128. },
  129. };
  130. if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
  131. return -EIO;
  132. for (i = 0; i < READ_REG_NUM; i++)
  133. radio->registers[i] = __be16_to_cpu(buf[READ_INDEX(i)]);
  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 = v4l2_fh_open(file);
  146. if (retval)
  147. return retval;
  148. if (v4l2_fh_is_singular_file(file)) {
  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. if (retval)
  162. v4l2_fh_release(file);
  163. return retval;
  164. }
  165. /*
  166. * si470x_fops_release - file release
  167. */
  168. int si470x_fops_release(struct file *file)
  169. {
  170. struct si470x_device *radio = video_drvdata(file);
  171. if (v4l2_fh_is_singular_file(file))
  172. /* stop radio */
  173. si470x_stop(radio);
  174. return v4l2_fh_release(file);
  175. }
  176. /**************************************************************************
  177. * Video4Linux Interface
  178. **************************************************************************/
  179. /*
  180. * si470x_vidioc_querycap - query device capabilities
  181. */
  182. int si470x_vidioc_querycap(struct file *file, void *priv,
  183. struct v4l2_capability *capability)
  184. {
  185. strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
  186. strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
  187. capability->device_caps = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_READWRITE |
  188. V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
  189. capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS;
  190. return 0;
  191. }
  192. /**************************************************************************
  193. * I2C Interface
  194. **************************************************************************/
  195. /*
  196. * si470x_i2c_interrupt - interrupt handler
  197. */
  198. static irqreturn_t si470x_i2c_interrupt(int irq, void *dev_id)
  199. {
  200. struct si470x_device *radio = dev_id;
  201. unsigned char regnr;
  202. unsigned char blocknum;
  203. unsigned short bler; /* rds block errors */
  204. unsigned short rds;
  205. unsigned char tmpbuf[3];
  206. int retval = 0;
  207. /* check Seek/Tune Complete */
  208. retval = si470x_get_register(radio, STATUSRSSI);
  209. if (retval < 0)
  210. goto end;
  211. if (radio->registers[STATUSRSSI] & STATUSRSSI_STC)
  212. complete(&radio->completion);
  213. /* safety checks */
  214. if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
  215. goto end;
  216. /* Update RDS registers */
  217. for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++) {
  218. retval = si470x_get_register(radio, STATUSRSSI + regnr);
  219. if (retval < 0)
  220. goto end;
  221. }
  222. /* get rds blocks */
  223. if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0)
  224. /* No RDS group ready, better luck next time */
  225. goto end;
  226. for (blocknum = 0; blocknum < 4; blocknum++) {
  227. switch (blocknum) {
  228. default:
  229. bler = (radio->registers[STATUSRSSI] &
  230. STATUSRSSI_BLERA) >> 9;
  231. rds = radio->registers[RDSA];
  232. break;
  233. case 1:
  234. bler = (radio->registers[READCHAN] &
  235. READCHAN_BLERB) >> 14;
  236. rds = radio->registers[RDSB];
  237. break;
  238. case 2:
  239. bler = (radio->registers[READCHAN] &
  240. READCHAN_BLERC) >> 12;
  241. rds = radio->registers[RDSC];
  242. break;
  243. case 3:
  244. bler = (radio->registers[READCHAN] &
  245. READCHAN_BLERD) >> 10;
  246. rds = radio->registers[RDSD];
  247. break;
  248. }
  249. /* Fill the V4L2 RDS buffer */
  250. put_unaligned_le16(rds, &tmpbuf);
  251. tmpbuf[2] = blocknum; /* offset name */
  252. tmpbuf[2] |= blocknum << 3; /* received offset */
  253. if (bler > max_rds_errors)
  254. tmpbuf[2] |= 0x80; /* uncorrectable errors */
  255. else if (bler > 0)
  256. tmpbuf[2] |= 0x40; /* corrected error(s) */
  257. /* copy RDS block to internal buffer */
  258. memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
  259. radio->wr_index += 3;
  260. /* wrap write pointer */
  261. if (radio->wr_index >= radio->buf_size)
  262. radio->wr_index = 0;
  263. /* check for overflow */
  264. if (radio->wr_index == radio->rd_index) {
  265. /* increment and wrap read pointer */
  266. radio->rd_index += 3;
  267. if (radio->rd_index >= radio->buf_size)
  268. radio->rd_index = 0;
  269. }
  270. }
  271. if (radio->wr_index != radio->rd_index)
  272. wake_up_interruptible(&radio->read_queue);
  273. end:
  274. return IRQ_HANDLED;
  275. }
  276. /*
  277. * si470x_i2c_probe - probe for the device
  278. */
  279. static int si470x_i2c_probe(struct i2c_client *client,
  280. const struct i2c_device_id *id)
  281. {
  282. struct si470x_device *radio;
  283. int retval = 0;
  284. unsigned char version_warning = 0;
  285. /* private data allocation and initialization */
  286. radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
  287. if (!radio) {
  288. retval = -ENOMEM;
  289. goto err_initial;
  290. }
  291. radio->client = client;
  292. radio->band = 1; /* Default to 76 - 108 MHz */
  293. mutex_init(&radio->lock);
  294. init_completion(&radio->completion);
  295. /* video device initialization */
  296. radio->videodev = si470x_viddev_template;
  297. video_set_drvdata(&radio->videodev, radio);
  298. /* power up : need 110ms */
  299. radio->registers[POWERCFG] = POWERCFG_ENABLE;
  300. if (si470x_set_register(radio, POWERCFG) < 0) {
  301. retval = -EIO;
  302. goto err_radio;
  303. }
  304. msleep(110);
  305. /* get device and chip versions */
  306. if (si470x_get_all_registers(radio) < 0) {
  307. retval = -EIO;
  308. goto err_radio;
  309. }
  310. dev_info(&client->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
  311. radio->registers[DEVICEID], radio->registers[SI_CHIPID]);
  312. if ((radio->registers[SI_CHIPID] & SI_CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
  313. dev_warn(&client->dev,
  314. "This driver is known to work with "
  315. "firmware version %hu,\n", RADIO_FW_VERSION);
  316. dev_warn(&client->dev,
  317. "but the device has firmware version %hu.\n",
  318. radio->registers[SI_CHIPID] & SI_CHIPID_FIRMWARE);
  319. version_warning = 1;
  320. }
  321. /* give out version warning */
  322. if (version_warning == 1) {
  323. dev_warn(&client->dev,
  324. "If you have some trouble using this driver,\n");
  325. dev_warn(&client->dev,
  326. "please report to V4L ML at "
  327. "linux-media@vger.kernel.org\n");
  328. }
  329. /* set initial frequency */
  330. si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
  331. /* rds buffer allocation */
  332. radio->buf_size = rds_buf * 3;
  333. radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
  334. if (!radio->buffer) {
  335. retval = -EIO;
  336. goto err_radio;
  337. }
  338. /* rds buffer configuration */
  339. radio->wr_index = 0;
  340. radio->rd_index = 0;
  341. init_waitqueue_head(&radio->read_queue);
  342. retval = request_threaded_irq(client->irq, NULL, si470x_i2c_interrupt,
  343. IRQF_TRIGGER_FALLING | IRQF_ONESHOT, DRIVER_NAME,
  344. radio);
  345. if (retval) {
  346. dev_err(&client->dev, "Failed to register interrupt\n");
  347. goto err_rds;
  348. }
  349. /* register video device */
  350. retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO,
  351. radio_nr);
  352. if (retval) {
  353. dev_warn(&client->dev, "Could not register video device\n");
  354. goto err_all;
  355. }
  356. i2c_set_clientdata(client, radio);
  357. return 0;
  358. err_all:
  359. free_irq(client->irq, radio);
  360. err_rds:
  361. kfree(radio->buffer);
  362. err_radio:
  363. kfree(radio);
  364. err_initial:
  365. return retval;
  366. }
  367. /*
  368. * si470x_i2c_remove - remove the device
  369. */
  370. static int si470x_i2c_remove(struct i2c_client *client)
  371. {
  372. struct si470x_device *radio = i2c_get_clientdata(client);
  373. free_irq(client->irq, radio);
  374. video_unregister_device(&radio->videodev);
  375. kfree(radio);
  376. return 0;
  377. }
  378. #ifdef CONFIG_PM_SLEEP
  379. /*
  380. * si470x_i2c_suspend - suspend the device
  381. */
  382. static int si470x_i2c_suspend(struct device *dev)
  383. {
  384. struct i2c_client *client = to_i2c_client(dev);
  385. struct si470x_device *radio = i2c_get_clientdata(client);
  386. /* power down */
  387. radio->registers[POWERCFG] |= POWERCFG_DISABLE;
  388. if (si470x_set_register(radio, POWERCFG) < 0)
  389. return -EIO;
  390. return 0;
  391. }
  392. /*
  393. * si470x_i2c_resume - resume the device
  394. */
  395. static int si470x_i2c_resume(struct device *dev)
  396. {
  397. struct i2c_client *client = to_i2c_client(dev);
  398. struct si470x_device *radio = i2c_get_clientdata(client);
  399. /* power up : need 110ms */
  400. radio->registers[POWERCFG] |= POWERCFG_ENABLE;
  401. if (si470x_set_register(radio, POWERCFG) < 0)
  402. return -EIO;
  403. msleep(110);
  404. return 0;
  405. }
  406. static SIMPLE_DEV_PM_OPS(si470x_i2c_pm, si470x_i2c_suspend, si470x_i2c_resume);
  407. #endif
  408. /*
  409. * si470x_i2c_driver - i2c driver interface
  410. */
  411. static struct i2c_driver si470x_i2c_driver = {
  412. .driver = {
  413. .name = "si470x",
  414. .owner = THIS_MODULE,
  415. #ifdef CONFIG_PM_SLEEP
  416. .pm = &si470x_i2c_pm,
  417. #endif
  418. },
  419. .probe = si470x_i2c_probe,
  420. .remove = si470x_i2c_remove,
  421. .id_table = si470x_i2c_id,
  422. };
  423. module_i2c_driver(si470x_i2c_driver);
  424. MODULE_LICENSE("GPL");
  425. MODULE_AUTHOR(DRIVER_AUTHOR);
  426. MODULE_DESCRIPTION(DRIVER_DESC);
  427. MODULE_VERSION(DRIVER_VERSION);