radio-si4713.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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, int cmd, void *arg)
  196. {
  197. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  198. ioctl, cmd, arg);
  199. }
  200. static struct v4l2_ioctl_ops radio_si4713_ioctl_ops = {
  201. .vidioc_enumaudout = radio_si4713_enumaudout,
  202. .vidioc_g_audout = radio_si4713_g_audout,
  203. .vidioc_s_audout = radio_si4713_s_audout,
  204. .vidioc_querycap = radio_si4713_querycap,
  205. .vidioc_queryctrl = radio_si4713_queryctrl,
  206. .vidioc_g_ext_ctrls = radio_si4713_g_ext_ctrls,
  207. .vidioc_s_ext_ctrls = radio_si4713_s_ext_ctrls,
  208. .vidioc_g_ctrl = radio_si4713_g_ctrl,
  209. .vidioc_s_ctrl = radio_si4713_s_ctrl,
  210. .vidioc_g_modulator = radio_si4713_g_modulator,
  211. .vidioc_s_modulator = radio_si4713_s_modulator,
  212. .vidioc_g_frequency = radio_si4713_g_frequency,
  213. .vidioc_s_frequency = radio_si4713_s_frequency,
  214. .vidioc_default = radio_si4713_default,
  215. };
  216. /* radio_si4713_vdev_template - video device interface */
  217. static struct video_device radio_si4713_vdev_template = {
  218. .fops = &radio_si4713_fops,
  219. .name = "radio-si4713",
  220. .release = video_device_release,
  221. .ioctl_ops = &radio_si4713_ioctl_ops,
  222. };
  223. /* Platform driver interface */
  224. /* radio_si4713_pdriver_probe - probe for the device */
  225. static int radio_si4713_pdriver_probe(struct platform_device *pdev)
  226. {
  227. struct radio_si4713_platform_data *pdata = pdev->dev.platform_data;
  228. struct radio_si4713_device *rsdev;
  229. struct i2c_adapter *adapter;
  230. struct v4l2_subdev *sd;
  231. int rval = 0;
  232. if (!pdata) {
  233. dev_err(&pdev->dev, "Cannot proceed without platform data.\n");
  234. rval = -EINVAL;
  235. goto exit;
  236. }
  237. rsdev = kzalloc(sizeof *rsdev, GFP_KERNEL);
  238. if (!rsdev) {
  239. dev_err(&pdev->dev, "Failed to alloc video device.\n");
  240. rval = -ENOMEM;
  241. goto exit;
  242. }
  243. rval = v4l2_device_register(&pdev->dev, &rsdev->v4l2_dev);
  244. if (rval) {
  245. dev_err(&pdev->dev, "Failed to register v4l2 device.\n");
  246. goto free_rsdev;
  247. }
  248. adapter = i2c_get_adapter(pdata->i2c_bus);
  249. if (!adapter) {
  250. dev_err(&pdev->dev, "Cannot get i2c adapter %d\n",
  251. pdata->i2c_bus);
  252. rval = -ENODEV;
  253. goto unregister_v4l2_dev;
  254. }
  255. sd = v4l2_i2c_new_subdev_board(&rsdev->v4l2_dev, adapter,
  256. pdata->subdev_board_info, NULL);
  257. if (!sd) {
  258. dev_err(&pdev->dev, "Cannot get v4l2 subdevice\n");
  259. rval = -ENODEV;
  260. goto put_adapter;
  261. }
  262. rsdev->radio_dev = video_device_alloc();
  263. if (!rsdev->radio_dev) {
  264. dev_err(&pdev->dev, "Failed to alloc video device.\n");
  265. rval = -ENOMEM;
  266. goto put_adapter;
  267. }
  268. memcpy(rsdev->radio_dev, &radio_si4713_vdev_template,
  269. sizeof(radio_si4713_vdev_template));
  270. video_set_drvdata(rsdev->radio_dev, rsdev);
  271. if (video_register_device(rsdev->radio_dev, VFL_TYPE_RADIO, radio_nr)) {
  272. dev_err(&pdev->dev, "Could not register video device.\n");
  273. rval = -EIO;
  274. goto free_vdev;
  275. }
  276. dev_info(&pdev->dev, "New device successfully probed\n");
  277. goto exit;
  278. free_vdev:
  279. video_device_release(rsdev->radio_dev);
  280. put_adapter:
  281. i2c_put_adapter(adapter);
  282. unregister_v4l2_dev:
  283. v4l2_device_unregister(&rsdev->v4l2_dev);
  284. free_rsdev:
  285. kfree(rsdev);
  286. exit:
  287. return rval;
  288. }
  289. /* radio_si4713_pdriver_remove - remove the device */
  290. static int __exit radio_si4713_pdriver_remove(struct platform_device *pdev)
  291. {
  292. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  293. struct radio_si4713_device *rsdev = container_of(v4l2_dev,
  294. struct radio_si4713_device,
  295. v4l2_dev);
  296. struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
  297. struct v4l2_subdev, list);
  298. struct i2c_client *client = v4l2_get_subdevdata(sd);
  299. video_unregister_device(rsdev->radio_dev);
  300. i2c_put_adapter(client->adapter);
  301. v4l2_device_unregister(&rsdev->v4l2_dev);
  302. kfree(rsdev);
  303. return 0;
  304. }
  305. static struct platform_driver radio_si4713_pdriver = {
  306. .driver = {
  307. .name = "radio-si4713",
  308. },
  309. .probe = radio_si4713_pdriver_probe,
  310. .remove = __exit_p(radio_si4713_pdriver_remove),
  311. };
  312. /* Module Interface */
  313. static int __init radio_si4713_module_init(void)
  314. {
  315. return platform_driver_register(&radio_si4713_pdriver);
  316. }
  317. static void __exit radio_si4713_module_exit(void)
  318. {
  319. platform_driver_unregister(&radio_si4713_pdriver);
  320. }
  321. module_init(radio_si4713_module_init);
  322. module_exit(radio_si4713_module_exit);