radio-si4713.c 10 KB

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