radio-si470x-i2c.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. static 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. static 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. * si470x_fops - file operations interface
  164. */
  165. const struct v4l2_file_operations si470x_fops = {
  166. .owner = THIS_MODULE,
  167. .ioctl = video_ioctl2,
  168. .open = si470x_fops_open,
  169. .release = si470x_fops_release,
  170. };
  171. /**************************************************************************
  172. * Video4Linux Interface
  173. **************************************************************************/
  174. /*
  175. * si470x_vidioc_querycap - query device capabilities
  176. */
  177. int si470x_vidioc_querycap(struct file *file, void *priv,
  178. struct v4l2_capability *capability)
  179. {
  180. strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
  181. strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
  182. capability->version = DRIVER_KERNEL_VERSION;
  183. capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
  184. V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  185. return 0;
  186. }
  187. /**************************************************************************
  188. * I2C Interface
  189. **************************************************************************/
  190. /*
  191. * si470x_i2c_probe - probe for the device
  192. */
  193. static int __devinit si470x_i2c_probe(struct i2c_client *client,
  194. const struct i2c_device_id *id)
  195. {
  196. struct si470x_device *radio;
  197. int retval = 0;
  198. unsigned char version_warning = 0;
  199. /* private data allocation and initialization */
  200. radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
  201. if (!radio) {
  202. retval = -ENOMEM;
  203. goto err_initial;
  204. }
  205. radio->users = 0;
  206. radio->client = client;
  207. mutex_init(&radio->lock);
  208. /* video device allocation and initialization */
  209. radio->videodev = video_device_alloc();
  210. if (!radio->videodev) {
  211. retval = -ENOMEM;
  212. goto err_radio;
  213. }
  214. memcpy(radio->videodev, &si470x_viddev_template,
  215. sizeof(si470x_viddev_template));
  216. video_set_drvdata(radio->videodev, radio);
  217. /* power up : need 110ms */
  218. radio->registers[POWERCFG] = POWERCFG_ENABLE;
  219. if (si470x_set_register(radio, POWERCFG) < 0) {
  220. retval = -EIO;
  221. goto err_all;
  222. }
  223. msleep(110);
  224. /* get device and chip versions */
  225. if (si470x_get_all_registers(radio) < 0) {
  226. retval = -EIO;
  227. goto err_video;
  228. }
  229. dev_info(&client->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
  230. radio->registers[DEVICEID], radio->registers[CHIPID]);
  231. if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
  232. dev_warn(&client->dev,
  233. "This driver is known to work with "
  234. "firmware version %hu,\n", RADIO_FW_VERSION);
  235. dev_warn(&client->dev,
  236. "but the device has firmware version %hu.\n",
  237. radio->registers[CHIPID] & CHIPID_FIRMWARE);
  238. version_warning = 1;
  239. }
  240. /* give out version warning */
  241. if (version_warning == 1) {
  242. dev_warn(&client->dev,
  243. "If you have some trouble using this driver,\n");
  244. dev_warn(&client->dev,
  245. "please report to V4L ML at "
  246. "linux-media@vger.kernel.org\n");
  247. }
  248. /* set initial frequency */
  249. si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
  250. /* register video device */
  251. retval = video_register_device(radio->videodev, VFL_TYPE_RADIO,
  252. radio_nr);
  253. if (retval) {
  254. dev_warn(&client->dev, "Could not register video device\n");
  255. goto err_all;
  256. }
  257. i2c_set_clientdata(client, radio);
  258. return 0;
  259. err_all:
  260. err_video:
  261. video_device_release(radio->videodev);
  262. err_radio:
  263. kfree(radio);
  264. err_initial:
  265. return retval;
  266. }
  267. /*
  268. * si470x_i2c_remove - remove the device
  269. */
  270. static __devexit int si470x_i2c_remove(struct i2c_client *client)
  271. {
  272. struct si470x_device *radio = i2c_get_clientdata(client);
  273. video_unregister_device(radio->videodev);
  274. kfree(radio);
  275. i2c_set_clientdata(client, NULL);
  276. return 0;
  277. }
  278. /*
  279. * si470x_i2c_driver - i2c driver interface
  280. */
  281. static struct i2c_driver si470x_i2c_driver = {
  282. .driver = {
  283. .name = "si470x",
  284. .owner = THIS_MODULE,
  285. },
  286. .probe = si470x_i2c_probe,
  287. .remove = __devexit_p(si470x_i2c_remove),
  288. .id_table = si470x_i2c_id,
  289. };
  290. /**************************************************************************
  291. * Module Interface
  292. **************************************************************************/
  293. /*
  294. * si470x_i2c_init - module init
  295. */
  296. static int __init si470x_i2c_init(void)
  297. {
  298. printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
  299. return i2c_add_driver(&si470x_i2c_driver);
  300. }
  301. /*
  302. * si470x_i2c_exit - module exit
  303. */
  304. static void __exit si470x_i2c_exit(void)
  305. {
  306. i2c_del_driver(&si470x_i2c_driver);
  307. }
  308. module_init(si470x_i2c_init);
  309. module_exit(si470x_i2c_exit);
  310. MODULE_LICENSE("GPL");
  311. MODULE_AUTHOR(DRIVER_AUTHOR);
  312. MODULE_DESCRIPTION(DRIVER_DESC);
  313. MODULE_VERSION(DRIVER_VERSION);