radio-si4713.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * drivers/media/radio/radio-si4713.c
  3. *
  4. * Platform Driver for Silicon Labs Si4713 FM Radio Transmitter:
  5. *
  6. * Copyright (c) 2008 Instituto Nokia de Tecnologia - INdT
  7. * Contact: Eduardo Valentin <eduardo.valentin@nokia.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. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/i2c.h>
  28. #include <linux/videodev2.h>
  29. #include <linux/slab.h>
  30. #include <media/v4l2-device.h>
  31. #include <media/v4l2-common.h>
  32. #include <media/v4l2-ioctl.h>
  33. #include <media/radio-si4713.h>
  34. /* module parameters */
  35. static int radio_nr = -1; /* radio device minor (-1 ==> auto assign) */
  36. module_param(radio_nr, int, 0);
  37. MODULE_PARM_DESC(radio_nr,
  38. "Minor number for radio device (-1 ==> auto assign)");
  39. MODULE_LICENSE("GPL");
  40. MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
  41. MODULE_DESCRIPTION("Platform driver for Si4713 FM Radio Transmitter");
  42. MODULE_VERSION("0.0.1");
  43. /* Driver state struct */
  44. struct radio_si4713_device {
  45. struct v4l2_device v4l2_dev;
  46. struct video_device *radio_dev;
  47. };
  48. /* radio_si4713_fops - file operations interface */
  49. static const struct v4l2_file_operations radio_si4713_fops = {
  50. .owner = THIS_MODULE,
  51. /* Note: locking is done at the subdev level in the i2c driver. */
  52. .unlocked_ioctl = video_ioctl2,
  53. };
  54. /* Video4Linux Interface */
  55. static int radio_si4713_fill_audout(struct v4l2_audioout *vao)
  56. {
  57. /* TODO: check presence of audio output */
  58. strlcpy(vao->name, "FM Modulator Audio Out", 32);
  59. return 0;
  60. }
  61. static int radio_si4713_enumaudout(struct file *file, void *priv,
  62. struct v4l2_audioout *vao)
  63. {
  64. return radio_si4713_fill_audout(vao);
  65. }
  66. static int radio_si4713_g_audout(struct file *file, void *priv,
  67. struct v4l2_audioout *vao)
  68. {
  69. int rval = radio_si4713_fill_audout(vao);
  70. vao->index = 0;
  71. return rval;
  72. }
  73. static int radio_si4713_s_audout(struct file *file, void *priv,
  74. struct v4l2_audioout *vao)
  75. {
  76. return vao->index ? -EINVAL : 0;
  77. }
  78. /* radio_si4713_querycap - query device capabilities */
  79. static int radio_si4713_querycap(struct file *file, void *priv,
  80. struct v4l2_capability *capability)
  81. {
  82. struct radio_si4713_device *rsdev;
  83. rsdev = video_get_drvdata(video_devdata(file));
  84. strlcpy(capability->driver, "radio-si4713", sizeof(capability->driver));
  85. strlcpy(capability->card, "Silicon Labs Si4713 Modulator",
  86. sizeof(capability->card));
  87. capability->capabilities = V4L2_CAP_MODULATOR | V4L2_CAP_RDS_OUTPUT;
  88. return 0;
  89. }
  90. /* radio_si4713_queryctrl - enumerate control items */
  91. static int radio_si4713_queryctrl(struct file *file, void *priv,
  92. struct v4l2_queryctrl *qc)
  93. {
  94. /* Must be sorted from low to high control ID! */
  95. static const u32 user_ctrls[] = {
  96. V4L2_CID_USER_CLASS,
  97. V4L2_CID_AUDIO_MUTE,
  98. 0
  99. };
  100. /* Must be sorted from low to high control ID! */
  101. static const u32 fmtx_ctrls[] = {
  102. V4L2_CID_FM_TX_CLASS,
  103. V4L2_CID_RDS_TX_DEVIATION,
  104. V4L2_CID_RDS_TX_PI,
  105. V4L2_CID_RDS_TX_PTY,
  106. V4L2_CID_RDS_TX_PS_NAME,
  107. V4L2_CID_RDS_TX_RADIO_TEXT,
  108. V4L2_CID_AUDIO_LIMITER_ENABLED,
  109. V4L2_CID_AUDIO_LIMITER_RELEASE_TIME,
  110. V4L2_CID_AUDIO_LIMITER_DEVIATION,
  111. V4L2_CID_AUDIO_COMPRESSION_ENABLED,
  112. V4L2_CID_AUDIO_COMPRESSION_GAIN,
  113. V4L2_CID_AUDIO_COMPRESSION_THRESHOLD,
  114. V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME,
  115. V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME,
  116. V4L2_CID_PILOT_TONE_ENABLED,
  117. V4L2_CID_PILOT_TONE_DEVIATION,
  118. V4L2_CID_PILOT_TONE_FREQUENCY,
  119. V4L2_CID_TUNE_PREEMPHASIS,
  120. V4L2_CID_TUNE_POWER_LEVEL,
  121. V4L2_CID_TUNE_ANTENNA_CAPACITOR,
  122. 0
  123. };
  124. static const u32 *ctrl_classes[] = {
  125. user_ctrls,
  126. fmtx_ctrls,
  127. NULL
  128. };
  129. struct radio_si4713_device *rsdev;
  130. rsdev = video_get_drvdata(video_devdata(file));
  131. qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
  132. if (qc->id == 0)
  133. return -EINVAL;
  134. if (qc->id == V4L2_CID_USER_CLASS || qc->id == V4L2_CID_FM_TX_CLASS)
  135. return v4l2_ctrl_query_fill(qc, 0, 0, 0, 0);
  136. return v4l2_device_call_until_err(&rsdev->v4l2_dev, 0, core,
  137. queryctrl, qc);
  138. }
  139. /*
  140. * v4l2 ioctl call backs.
  141. * we are just a wrapper for v4l2_sub_devs.
  142. */
  143. static inline struct v4l2_device *get_v4l2_dev(struct file *file)
  144. {
  145. return &((struct radio_si4713_device *)video_drvdata(file))->v4l2_dev;
  146. }
  147. static int radio_si4713_g_ext_ctrls(struct file *file, void *p,
  148. struct v4l2_ext_controls *vecs)
  149. {
  150. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  151. g_ext_ctrls, vecs);
  152. }
  153. static int radio_si4713_s_ext_ctrls(struct file *file, void *p,
  154. struct v4l2_ext_controls *vecs)
  155. {
  156. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  157. s_ext_ctrls, vecs);
  158. }
  159. static int radio_si4713_g_ctrl(struct file *file, void *p,
  160. struct v4l2_control *vc)
  161. {
  162. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  163. g_ctrl, vc);
  164. }
  165. static int radio_si4713_s_ctrl(struct file *file, void *p,
  166. struct v4l2_control *vc)
  167. {
  168. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  169. s_ctrl, vc);
  170. }
  171. static int radio_si4713_g_modulator(struct file *file, void *p,
  172. struct v4l2_modulator *vm)
  173. {
  174. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  175. g_modulator, vm);
  176. }
  177. static int radio_si4713_s_modulator(struct file *file, void *p,
  178. struct v4l2_modulator *vm)
  179. {
  180. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  181. s_modulator, vm);
  182. }
  183. static int radio_si4713_g_frequency(struct file *file, void *p,
  184. struct v4l2_frequency *vf)
  185. {
  186. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  187. g_frequency, vf);
  188. }
  189. static int radio_si4713_s_frequency(struct file *file, void *p,
  190. struct v4l2_frequency *vf)
  191. {
  192. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  193. s_frequency, vf);
  194. }
  195. static long radio_si4713_default(struct file *file, void *p,
  196. bool valid_prio, int cmd, void *arg)
  197. {
  198. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  199. ioctl, cmd, arg);
  200. }
  201. static struct v4l2_ioctl_ops radio_si4713_ioctl_ops = {
  202. .vidioc_enumaudout = radio_si4713_enumaudout,
  203. .vidioc_g_audout = radio_si4713_g_audout,
  204. .vidioc_s_audout = radio_si4713_s_audout,
  205. .vidioc_querycap = radio_si4713_querycap,
  206. .vidioc_queryctrl = radio_si4713_queryctrl,
  207. .vidioc_g_ext_ctrls = radio_si4713_g_ext_ctrls,
  208. .vidioc_s_ext_ctrls = radio_si4713_s_ext_ctrls,
  209. .vidioc_g_ctrl = radio_si4713_g_ctrl,
  210. .vidioc_s_ctrl = radio_si4713_s_ctrl,
  211. .vidioc_g_modulator = radio_si4713_g_modulator,
  212. .vidioc_s_modulator = radio_si4713_s_modulator,
  213. .vidioc_g_frequency = radio_si4713_g_frequency,
  214. .vidioc_s_frequency = radio_si4713_s_frequency,
  215. .vidioc_default = radio_si4713_default,
  216. };
  217. /* radio_si4713_vdev_template - video device interface */
  218. static struct video_device radio_si4713_vdev_template = {
  219. .fops = &radio_si4713_fops,
  220. .name = "radio-si4713",
  221. .release = video_device_release,
  222. .ioctl_ops = &radio_si4713_ioctl_ops,
  223. };
  224. /* Platform driver interface */
  225. /* radio_si4713_pdriver_probe - probe for the device */
  226. static int radio_si4713_pdriver_probe(struct platform_device *pdev)
  227. {
  228. struct radio_si4713_platform_data *pdata = pdev->dev.platform_data;
  229. struct radio_si4713_device *rsdev;
  230. struct i2c_adapter *adapter;
  231. struct v4l2_subdev *sd;
  232. int rval = 0;
  233. if (!pdata) {
  234. dev_err(&pdev->dev, "Cannot proceed without platform data.\n");
  235. rval = -EINVAL;
  236. goto exit;
  237. }
  238. rsdev = kzalloc(sizeof *rsdev, GFP_KERNEL);
  239. if (!rsdev) {
  240. dev_err(&pdev->dev, "Failed to alloc video device.\n");
  241. rval = -ENOMEM;
  242. goto exit;
  243. }
  244. rval = v4l2_device_register(&pdev->dev, &rsdev->v4l2_dev);
  245. if (rval) {
  246. dev_err(&pdev->dev, "Failed to register v4l2 device.\n");
  247. goto free_rsdev;
  248. }
  249. adapter = i2c_get_adapter(pdata->i2c_bus);
  250. if (!adapter) {
  251. dev_err(&pdev->dev, "Cannot get i2c adapter %d\n",
  252. pdata->i2c_bus);
  253. rval = -ENODEV;
  254. goto unregister_v4l2_dev;
  255. }
  256. sd = v4l2_i2c_new_subdev_board(&rsdev->v4l2_dev, adapter,
  257. pdata->subdev_board_info, NULL);
  258. if (!sd) {
  259. dev_err(&pdev->dev, "Cannot get v4l2 subdevice\n");
  260. rval = -ENODEV;
  261. goto put_adapter;
  262. }
  263. rsdev->radio_dev = video_device_alloc();
  264. if (!rsdev->radio_dev) {
  265. dev_err(&pdev->dev, "Failed to alloc video device.\n");
  266. rval = -ENOMEM;
  267. goto put_adapter;
  268. }
  269. memcpy(rsdev->radio_dev, &radio_si4713_vdev_template,
  270. sizeof(radio_si4713_vdev_template));
  271. video_set_drvdata(rsdev->radio_dev, rsdev);
  272. if (video_register_device(rsdev->radio_dev, VFL_TYPE_RADIO, radio_nr)) {
  273. dev_err(&pdev->dev, "Could not register video device.\n");
  274. rval = -EIO;
  275. goto free_vdev;
  276. }
  277. dev_info(&pdev->dev, "New device successfully probed\n");
  278. goto exit;
  279. free_vdev:
  280. video_device_release(rsdev->radio_dev);
  281. put_adapter:
  282. i2c_put_adapter(adapter);
  283. unregister_v4l2_dev:
  284. v4l2_device_unregister(&rsdev->v4l2_dev);
  285. free_rsdev:
  286. kfree(rsdev);
  287. exit:
  288. return rval;
  289. }
  290. /* radio_si4713_pdriver_remove - remove the device */
  291. static int __exit radio_si4713_pdriver_remove(struct platform_device *pdev)
  292. {
  293. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  294. struct radio_si4713_device *rsdev = container_of(v4l2_dev,
  295. struct radio_si4713_device,
  296. v4l2_dev);
  297. struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
  298. struct v4l2_subdev, list);
  299. struct i2c_client *client = v4l2_get_subdevdata(sd);
  300. video_unregister_device(rsdev->radio_dev);
  301. i2c_put_adapter(client->adapter);
  302. v4l2_device_unregister(&rsdev->v4l2_dev);
  303. kfree(rsdev);
  304. return 0;
  305. }
  306. static struct platform_driver radio_si4713_pdriver = {
  307. .driver = {
  308. .name = "radio-si4713",
  309. },
  310. .probe = radio_si4713_pdriver_probe,
  311. .remove = __exit_p(radio_si4713_pdriver_remove),
  312. };
  313. /* Module Interface */
  314. static int __init radio_si4713_module_init(void)
  315. {
  316. return platform_driver_register(&radio_si4713_pdriver);
  317. }
  318. static void __exit radio_si4713_module_exit(void)
  319. {
  320. platform_driver_unregister(&radio_si4713_pdriver);
  321. }
  322. module_init(radio_si4713_module_init);
  323. module_exit(radio_si4713_module_exit);