radio-si4713.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. strlcpy(capability->driver, "radio-si4713", sizeof(capability->driver));
  83. strlcpy(capability->card, "Silicon Labs Si4713 Modulator",
  84. sizeof(capability->card));
  85. capability->capabilities = V4L2_CAP_MODULATOR | V4L2_CAP_RDS_OUTPUT;
  86. return 0;
  87. }
  88. /* radio_si4713_queryctrl - enumerate control items */
  89. static int radio_si4713_queryctrl(struct file *file, void *priv,
  90. struct v4l2_queryctrl *qc)
  91. {
  92. /* Must be sorted from low to high control ID! */
  93. static const u32 user_ctrls[] = {
  94. V4L2_CID_USER_CLASS,
  95. V4L2_CID_AUDIO_MUTE,
  96. 0
  97. };
  98. /* Must be sorted from low to high control ID! */
  99. static const u32 fmtx_ctrls[] = {
  100. V4L2_CID_FM_TX_CLASS,
  101. V4L2_CID_RDS_TX_DEVIATION,
  102. V4L2_CID_RDS_TX_PI,
  103. V4L2_CID_RDS_TX_PTY,
  104. V4L2_CID_RDS_TX_PS_NAME,
  105. V4L2_CID_RDS_TX_RADIO_TEXT,
  106. V4L2_CID_AUDIO_LIMITER_ENABLED,
  107. V4L2_CID_AUDIO_LIMITER_RELEASE_TIME,
  108. V4L2_CID_AUDIO_LIMITER_DEVIATION,
  109. V4L2_CID_AUDIO_COMPRESSION_ENABLED,
  110. V4L2_CID_AUDIO_COMPRESSION_GAIN,
  111. V4L2_CID_AUDIO_COMPRESSION_THRESHOLD,
  112. V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME,
  113. V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME,
  114. V4L2_CID_PILOT_TONE_ENABLED,
  115. V4L2_CID_PILOT_TONE_DEVIATION,
  116. V4L2_CID_PILOT_TONE_FREQUENCY,
  117. V4L2_CID_TUNE_PREEMPHASIS,
  118. V4L2_CID_TUNE_POWER_LEVEL,
  119. V4L2_CID_TUNE_ANTENNA_CAPACITOR,
  120. 0
  121. };
  122. static const u32 *ctrl_classes[] = {
  123. user_ctrls,
  124. fmtx_ctrls,
  125. NULL
  126. };
  127. struct radio_si4713_device *rsdev;
  128. rsdev = video_get_drvdata(video_devdata(file));
  129. qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
  130. if (qc->id == 0)
  131. return -EINVAL;
  132. if (qc->id == V4L2_CID_USER_CLASS || qc->id == V4L2_CID_FM_TX_CLASS)
  133. return v4l2_ctrl_query_fill(qc, 0, 0, 0, 0);
  134. return v4l2_device_call_until_err(&rsdev->v4l2_dev, 0, core,
  135. queryctrl, qc);
  136. }
  137. /*
  138. * v4l2 ioctl call backs.
  139. * we are just a wrapper for v4l2_sub_devs.
  140. */
  141. static inline struct v4l2_device *get_v4l2_dev(struct file *file)
  142. {
  143. return &((struct radio_si4713_device *)video_drvdata(file))->v4l2_dev;
  144. }
  145. static int radio_si4713_g_ext_ctrls(struct file *file, void *p,
  146. struct v4l2_ext_controls *vecs)
  147. {
  148. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  149. g_ext_ctrls, vecs);
  150. }
  151. static int radio_si4713_s_ext_ctrls(struct file *file, void *p,
  152. struct v4l2_ext_controls *vecs)
  153. {
  154. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  155. s_ext_ctrls, vecs);
  156. }
  157. static int radio_si4713_g_ctrl(struct file *file, void *p,
  158. struct v4l2_control *vc)
  159. {
  160. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  161. g_ctrl, vc);
  162. }
  163. static int radio_si4713_s_ctrl(struct file *file, void *p,
  164. struct v4l2_control *vc)
  165. {
  166. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  167. s_ctrl, vc);
  168. }
  169. static int radio_si4713_g_modulator(struct file *file, void *p,
  170. struct v4l2_modulator *vm)
  171. {
  172. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  173. g_modulator, vm);
  174. }
  175. static int radio_si4713_s_modulator(struct file *file, void *p,
  176. struct v4l2_modulator *vm)
  177. {
  178. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  179. s_modulator, vm);
  180. }
  181. static int radio_si4713_g_frequency(struct file *file, void *p,
  182. struct v4l2_frequency *vf)
  183. {
  184. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  185. g_frequency, vf);
  186. }
  187. static int radio_si4713_s_frequency(struct file *file, void *p,
  188. struct v4l2_frequency *vf)
  189. {
  190. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  191. s_frequency, vf);
  192. }
  193. static long radio_si4713_default(struct file *file, void *p,
  194. bool valid_prio, 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,
  255. pdata->subdev_board_info, NULL);
  256. if (!sd) {
  257. dev_err(&pdev->dev, "Cannot get v4l2 subdevice\n");
  258. rval = -ENODEV;
  259. goto put_adapter;
  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 put_adapter;
  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. put_adapter:
  280. i2c_put_adapter(adapter);
  281. unregister_v4l2_dev:
  282. v4l2_device_unregister(&rsdev->v4l2_dev);
  283. free_rsdev:
  284. kfree(rsdev);
  285. exit:
  286. return rval;
  287. }
  288. /* radio_si4713_pdriver_remove - remove the device */
  289. static int __exit radio_si4713_pdriver_remove(struct platform_device *pdev)
  290. {
  291. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  292. struct radio_si4713_device *rsdev = container_of(v4l2_dev,
  293. struct radio_si4713_device,
  294. v4l2_dev);
  295. struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
  296. struct v4l2_subdev, list);
  297. struct i2c_client *client = v4l2_get_subdevdata(sd);
  298. video_unregister_device(rsdev->radio_dev);
  299. i2c_put_adapter(client->adapter);
  300. v4l2_device_unregister(&rsdev->v4l2_dev);
  301. kfree(rsdev);
  302. return 0;
  303. }
  304. static struct platform_driver radio_si4713_pdriver = {
  305. .driver = {
  306. .name = "radio-si4713",
  307. },
  308. .probe = radio_si4713_pdriver_probe,
  309. .remove = __exit_p(radio_si4713_pdriver_remove),
  310. };
  311. module_platform_driver(radio_si4713_pdriver);