radio-si470x-i2c.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. /*
  24. * ToDo:
  25. * - RDS support
  26. */
  27. /* driver definitions */
  28. #define DRIVER_AUTHOR "Joonyoung Shim <jy0922.shim@samsung.com>";
  29. #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 0)
  30. #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
  31. #define DRIVER_DESC "I2C radio driver for Si470x FM Radio Receivers"
  32. #define DRIVER_VERSION "1.0.0"
  33. /* kernel includes */
  34. #include <linux/i2c.h>
  35. #include <linux/delay.h>
  36. #include "radio-si470x.h"
  37. /* I2C Device ID List */
  38. static const struct i2c_device_id si470x_i2c_id[] = {
  39. /* Generic Entry */
  40. { "si470x", 0 },
  41. /* Terminating entry */
  42. { }
  43. };
  44. MODULE_DEVICE_TABLE(i2c, si470x_i2c_id);
  45. /**************************************************************************
  46. * Module Parameters
  47. **************************************************************************/
  48. /* Radio Nr */
  49. static int radio_nr = -1;
  50. module_param(radio_nr, int, 0444);
  51. MODULE_PARM_DESC(radio_nr, "Radio Nr");
  52. /**************************************************************************
  53. * I2C Definitions
  54. **************************************************************************/
  55. /* Write starts with the upper byte of register 0x02 */
  56. #define WRITE_REG_NUM 8
  57. #define WRITE_INDEX(i) (i + 0x02)
  58. /* Read starts with the upper byte of register 0x0a */
  59. #define READ_REG_NUM RADIO_REGISTER_NUM
  60. #define READ_INDEX(i) ((i + RADIO_REGISTER_NUM - 0x0a) % READ_REG_NUM)
  61. /**************************************************************************
  62. * General Driver Functions - REGISTERs
  63. **************************************************************************/
  64. /*
  65. * si470x_get_register - read register
  66. */
  67. int si470x_get_register(struct si470x_device *radio, int regnr)
  68. {
  69. u16 buf[READ_REG_NUM];
  70. struct i2c_msg msgs[1] = {
  71. { radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM,
  72. (void *)buf },
  73. };
  74. if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
  75. return -EIO;
  76. radio->registers[regnr] = __be16_to_cpu(buf[READ_INDEX(regnr)]);
  77. return 0;
  78. }
  79. /*
  80. * si470x_set_register - write register
  81. */
  82. int si470x_set_register(struct si470x_device *radio, int regnr)
  83. {
  84. int i;
  85. u16 buf[WRITE_REG_NUM];
  86. struct i2c_msg msgs[1] = {
  87. { radio->client->addr, 0, sizeof(u16) * WRITE_REG_NUM,
  88. (void *)buf },
  89. };
  90. for (i = 0; i < WRITE_REG_NUM; i++)
  91. buf[i] = __cpu_to_be16(radio->registers[WRITE_INDEX(i)]);
  92. if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
  93. return -EIO;
  94. return 0;
  95. }
  96. /**************************************************************************
  97. * General Driver Functions - ENTIRE REGISTERS
  98. **************************************************************************/
  99. /*
  100. * si470x_get_all_registers - read entire registers
  101. */
  102. static int si470x_get_all_registers(struct si470x_device *radio)
  103. {
  104. int i;
  105. u16 buf[READ_REG_NUM];
  106. struct i2c_msg msgs[1] = {
  107. { radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM,
  108. (void *)buf },
  109. };
  110. if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
  111. return -EIO;
  112. for (i = 0; i < READ_REG_NUM; i++)
  113. radio->registers[i] = __be16_to_cpu(buf[READ_INDEX(i)]);
  114. return 0;
  115. }
  116. /**************************************************************************
  117. * General Driver Functions - DISCONNECT_CHECK
  118. **************************************************************************/
  119. /*
  120. * si470x_disconnect_check - check whether radio disconnects
  121. */
  122. int si470x_disconnect_check(struct si470x_device *radio)
  123. {
  124. return 0;
  125. }
  126. /**************************************************************************
  127. * File Operations Interface
  128. **************************************************************************/
  129. /*
  130. * si470x_fops_open - file open
  131. */
  132. int si470x_fops_open(struct file *file)
  133. {
  134. struct si470x_device *radio = video_drvdata(file);
  135. int retval = 0;
  136. mutex_lock(&radio->lock);
  137. radio->users++;
  138. if (radio->users == 1)
  139. /* start radio */
  140. retval = si470x_start(radio);
  141. mutex_unlock(&radio->lock);
  142. return retval;
  143. }
  144. /*
  145. * si470x_fops_release - file release
  146. */
  147. int si470x_fops_release(struct file *file)
  148. {
  149. struct si470x_device *radio = video_drvdata(file);
  150. int retval = 0;
  151. /* safety check */
  152. if (!radio)
  153. return -ENODEV;
  154. mutex_lock(&radio->lock);
  155. radio->users--;
  156. if (radio->users == 0)
  157. /* stop radio */
  158. retval = si470x_stop(radio);
  159. mutex_unlock(&radio->lock);
  160. return retval;
  161. }
  162. /**************************************************************************
  163. * Video4Linux Interface
  164. **************************************************************************/
  165. /*
  166. * si470x_vidioc_querycap - query device capabilities
  167. */
  168. int si470x_vidioc_querycap(struct file *file, void *priv,
  169. struct v4l2_capability *capability)
  170. {
  171. strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
  172. strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
  173. capability->version = DRIVER_KERNEL_VERSION;
  174. capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
  175. V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  176. return 0;
  177. }
  178. /**************************************************************************
  179. * I2C Interface
  180. **************************************************************************/
  181. /*
  182. * si470x_i2c_probe - probe for the device
  183. */
  184. static int __devinit si470x_i2c_probe(struct i2c_client *client,
  185. const struct i2c_device_id *id)
  186. {
  187. struct si470x_device *radio;
  188. int retval = 0;
  189. unsigned char version_warning = 0;
  190. /* private data allocation and initialization */
  191. radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
  192. if (!radio) {
  193. retval = -ENOMEM;
  194. goto err_initial;
  195. }
  196. radio->users = 0;
  197. radio->client = client;
  198. mutex_init(&radio->lock);
  199. /* video device allocation and initialization */
  200. radio->videodev = video_device_alloc();
  201. if (!radio->videodev) {
  202. retval = -ENOMEM;
  203. goto err_radio;
  204. }
  205. memcpy(radio->videodev, &si470x_viddev_template,
  206. sizeof(si470x_viddev_template));
  207. video_set_drvdata(radio->videodev, radio);
  208. /* power up : need 110ms */
  209. radio->registers[POWERCFG] = POWERCFG_ENABLE;
  210. if (si470x_set_register(radio, POWERCFG) < 0) {
  211. retval = -EIO;
  212. goto err_all;
  213. }
  214. msleep(110);
  215. /* get device and chip versions */
  216. if (si470x_get_all_registers(radio) < 0) {
  217. retval = -EIO;
  218. goto err_video;
  219. }
  220. dev_info(&client->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
  221. radio->registers[DEVICEID], radio->registers[CHIPID]);
  222. if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
  223. dev_warn(&client->dev,
  224. "This driver is known to work with "
  225. "firmware version %hu,\n", RADIO_FW_VERSION);
  226. dev_warn(&client->dev,
  227. "but the device has firmware version %hu.\n",
  228. radio->registers[CHIPID] & CHIPID_FIRMWARE);
  229. version_warning = 1;
  230. }
  231. /* give out version warning */
  232. if (version_warning == 1) {
  233. dev_warn(&client->dev,
  234. "If you have some trouble using this driver,\n");
  235. dev_warn(&client->dev,
  236. "please report to V4L ML at "
  237. "linux-media@vger.kernel.org\n");
  238. }
  239. /* set initial frequency */
  240. si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
  241. /* register video device */
  242. retval = video_register_device(radio->videodev, VFL_TYPE_RADIO,
  243. radio_nr);
  244. if (retval) {
  245. dev_warn(&client->dev, "Could not register video device\n");
  246. goto err_all;
  247. }
  248. i2c_set_clientdata(client, radio);
  249. return 0;
  250. err_all:
  251. err_video:
  252. video_device_release(radio->videodev);
  253. err_radio:
  254. kfree(radio);
  255. err_initial:
  256. return retval;
  257. }
  258. /*
  259. * si470x_i2c_remove - remove the device
  260. */
  261. static __devexit int si470x_i2c_remove(struct i2c_client *client)
  262. {
  263. struct si470x_device *radio = i2c_get_clientdata(client);
  264. video_unregister_device(radio->videodev);
  265. kfree(radio);
  266. i2c_set_clientdata(client, NULL);
  267. return 0;
  268. }
  269. /*
  270. * si470x_i2c_driver - i2c driver interface
  271. */
  272. static struct i2c_driver si470x_i2c_driver = {
  273. .driver = {
  274. .name = "si470x",
  275. .owner = THIS_MODULE,
  276. },
  277. .probe = si470x_i2c_probe,
  278. .remove = __devexit_p(si470x_i2c_remove),
  279. .id_table = si470x_i2c_id,
  280. };
  281. /**************************************************************************
  282. * Module Interface
  283. **************************************************************************/
  284. /*
  285. * si470x_i2c_init - module init
  286. */
  287. static int __init si470x_i2c_init(void)
  288. {
  289. printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
  290. return i2c_add_driver(&si470x_i2c_driver);
  291. }
  292. /*
  293. * si470x_i2c_exit - module exit
  294. */
  295. static void __exit si470x_i2c_exit(void)
  296. {
  297. i2c_del_driver(&si470x_i2c_driver);
  298. }
  299. module_init(si470x_i2c_init);
  300. module_exit(si470x_i2c_exit);
  301. MODULE_LICENSE("GPL");
  302. MODULE_AUTHOR(DRIVER_AUTHOR);
  303. MODULE_DESCRIPTION(DRIVER_DESC);
  304. MODULE_VERSION(DRIVER_VERSION);