radio-si4713.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 v2");
  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. MODULE_ALIAS("platform:radio-si4713");
  44. /* Driver state struct */
  45. struct radio_si4713_device {
  46. struct v4l2_device v4l2_dev;
  47. struct video_device radio_dev;
  48. };
  49. /* radio_si4713_fops - file operations interface */
  50. static const struct v4l2_file_operations radio_si4713_fops = {
  51. .owner = THIS_MODULE,
  52. /* Note: locking is done at the subdev level in the i2c driver. */
  53. .unlocked_ioctl = video_ioctl2,
  54. };
  55. /* Video4Linux Interface */
  56. /* radio_si4713_querycap - query device capabilities */
  57. static int radio_si4713_querycap(struct file *file, void *priv,
  58. struct v4l2_capability *capability)
  59. {
  60. strlcpy(capability->driver, "radio-si4713", sizeof(capability->driver));
  61. strlcpy(capability->card, "Silicon Labs Si4713 Modulator",
  62. sizeof(capability->card));
  63. strlcpy(capability->bus_info, "platform:radio-si4713",
  64. sizeof(capability->bus_info));
  65. capability->device_caps = V4L2_CAP_MODULATOR | V4L2_CAP_RDS_OUTPUT;
  66. capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS;
  67. return 0;
  68. }
  69. /* radio_si4713_queryctrl - enumerate control items */
  70. static int radio_si4713_queryctrl(struct file *file, void *priv,
  71. struct v4l2_queryctrl *qc)
  72. {
  73. /* Must be sorted from low to high control ID! */
  74. static const u32 user_ctrls[] = {
  75. V4L2_CID_USER_CLASS,
  76. V4L2_CID_AUDIO_MUTE,
  77. 0
  78. };
  79. /* Must be sorted from low to high control ID! */
  80. static const u32 fmtx_ctrls[] = {
  81. V4L2_CID_FM_TX_CLASS,
  82. V4L2_CID_RDS_TX_DEVIATION,
  83. V4L2_CID_RDS_TX_PI,
  84. V4L2_CID_RDS_TX_PTY,
  85. V4L2_CID_RDS_TX_PS_NAME,
  86. V4L2_CID_RDS_TX_RADIO_TEXT,
  87. V4L2_CID_AUDIO_LIMITER_ENABLED,
  88. V4L2_CID_AUDIO_LIMITER_RELEASE_TIME,
  89. V4L2_CID_AUDIO_LIMITER_DEVIATION,
  90. V4L2_CID_AUDIO_COMPRESSION_ENABLED,
  91. V4L2_CID_AUDIO_COMPRESSION_GAIN,
  92. V4L2_CID_AUDIO_COMPRESSION_THRESHOLD,
  93. V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME,
  94. V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME,
  95. V4L2_CID_PILOT_TONE_ENABLED,
  96. V4L2_CID_PILOT_TONE_DEVIATION,
  97. V4L2_CID_PILOT_TONE_FREQUENCY,
  98. V4L2_CID_TUNE_PREEMPHASIS,
  99. V4L2_CID_TUNE_POWER_LEVEL,
  100. V4L2_CID_TUNE_ANTENNA_CAPACITOR,
  101. 0
  102. };
  103. static const u32 *ctrl_classes[] = {
  104. user_ctrls,
  105. fmtx_ctrls,
  106. NULL
  107. };
  108. struct radio_si4713_device *rsdev;
  109. rsdev = video_get_drvdata(video_devdata(file));
  110. qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
  111. if (qc->id == 0)
  112. return -EINVAL;
  113. if (qc->id == V4L2_CID_USER_CLASS || qc->id == V4L2_CID_FM_TX_CLASS)
  114. return v4l2_ctrl_query_fill(qc, 0, 0, 0, 0);
  115. return v4l2_device_call_until_err(&rsdev->v4l2_dev, 0, core,
  116. queryctrl, qc);
  117. }
  118. /*
  119. * v4l2 ioctl call backs.
  120. * we are just a wrapper for v4l2_sub_devs.
  121. */
  122. static inline struct v4l2_device *get_v4l2_dev(struct file *file)
  123. {
  124. return &((struct radio_si4713_device *)video_drvdata(file))->v4l2_dev;
  125. }
  126. static int radio_si4713_g_ext_ctrls(struct file *file, void *p,
  127. struct v4l2_ext_controls *vecs)
  128. {
  129. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  130. g_ext_ctrls, vecs);
  131. }
  132. static int radio_si4713_s_ext_ctrls(struct file *file, void *p,
  133. struct v4l2_ext_controls *vecs)
  134. {
  135. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  136. s_ext_ctrls, vecs);
  137. }
  138. static int radio_si4713_g_ctrl(struct file *file, void *p,
  139. struct v4l2_control *vc)
  140. {
  141. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  142. g_ctrl, vc);
  143. }
  144. static int radio_si4713_s_ctrl(struct file *file, void *p,
  145. struct v4l2_control *vc)
  146. {
  147. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  148. s_ctrl, vc);
  149. }
  150. static int radio_si4713_g_modulator(struct file *file, void *p,
  151. struct v4l2_modulator *vm)
  152. {
  153. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  154. g_modulator, vm);
  155. }
  156. static int radio_si4713_s_modulator(struct file *file, void *p,
  157. const struct v4l2_modulator *vm)
  158. {
  159. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  160. s_modulator, vm);
  161. }
  162. static int radio_si4713_g_frequency(struct file *file, void *p,
  163. struct v4l2_frequency *vf)
  164. {
  165. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  166. g_frequency, vf);
  167. }
  168. static int radio_si4713_s_frequency(struct file *file, void *p,
  169. const struct v4l2_frequency *vf)
  170. {
  171. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  172. s_frequency, vf);
  173. }
  174. static long radio_si4713_default(struct file *file, void *p,
  175. bool valid_prio, unsigned int cmd, void *arg)
  176. {
  177. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  178. ioctl, cmd, arg);
  179. }
  180. static struct v4l2_ioctl_ops radio_si4713_ioctl_ops = {
  181. .vidioc_querycap = radio_si4713_querycap,
  182. .vidioc_queryctrl = radio_si4713_queryctrl,
  183. .vidioc_g_ext_ctrls = radio_si4713_g_ext_ctrls,
  184. .vidioc_s_ext_ctrls = radio_si4713_s_ext_ctrls,
  185. .vidioc_g_ctrl = radio_si4713_g_ctrl,
  186. .vidioc_s_ctrl = radio_si4713_s_ctrl,
  187. .vidioc_g_modulator = radio_si4713_g_modulator,
  188. .vidioc_s_modulator = radio_si4713_s_modulator,
  189. .vidioc_g_frequency = radio_si4713_g_frequency,
  190. .vidioc_s_frequency = radio_si4713_s_frequency,
  191. .vidioc_default = radio_si4713_default,
  192. };
  193. /* radio_si4713_vdev_template - video device interface */
  194. static struct video_device radio_si4713_vdev_template = {
  195. .fops = &radio_si4713_fops,
  196. .name = "radio-si4713",
  197. .release = video_device_release_empty,
  198. .ioctl_ops = &radio_si4713_ioctl_ops,
  199. .vfl_dir = VFL_DIR_TX,
  200. };
  201. /* Platform driver interface */
  202. /* radio_si4713_pdriver_probe - probe for the device */
  203. static int radio_si4713_pdriver_probe(struct platform_device *pdev)
  204. {
  205. struct radio_si4713_platform_data *pdata = pdev->dev.platform_data;
  206. struct radio_si4713_device *rsdev;
  207. struct i2c_adapter *adapter;
  208. struct v4l2_subdev *sd;
  209. int rval = 0;
  210. if (!pdata) {
  211. dev_err(&pdev->dev, "Cannot proceed without platform data.\n");
  212. rval = -EINVAL;
  213. goto exit;
  214. }
  215. rsdev = devm_kzalloc(&pdev->dev, sizeof(*rsdev), GFP_KERNEL);
  216. if (!rsdev) {
  217. dev_err(&pdev->dev, "Failed to alloc video device.\n");
  218. rval = -ENOMEM;
  219. goto exit;
  220. }
  221. rval = v4l2_device_register(&pdev->dev, &rsdev->v4l2_dev);
  222. if (rval) {
  223. dev_err(&pdev->dev, "Failed to register v4l2 device.\n");
  224. goto exit;
  225. }
  226. adapter = i2c_get_adapter(pdata->i2c_bus);
  227. if (!adapter) {
  228. dev_err(&pdev->dev, "Cannot get i2c adapter %d\n",
  229. pdata->i2c_bus);
  230. rval = -ENODEV;
  231. goto unregister_v4l2_dev;
  232. }
  233. sd = v4l2_i2c_new_subdev_board(&rsdev->v4l2_dev, adapter,
  234. pdata->subdev_board_info, NULL);
  235. if (!sd) {
  236. dev_err(&pdev->dev, "Cannot get v4l2 subdevice\n");
  237. rval = -ENODEV;
  238. goto put_adapter;
  239. }
  240. rsdev->radio_dev = radio_si4713_vdev_template;
  241. rsdev->radio_dev.v4l2_dev = &rsdev->v4l2_dev;
  242. video_set_drvdata(&rsdev->radio_dev, rsdev);
  243. if (video_register_device(&rsdev->radio_dev, VFL_TYPE_RADIO, radio_nr)) {
  244. dev_err(&pdev->dev, "Could not register video device.\n");
  245. rval = -EIO;
  246. goto put_adapter;
  247. }
  248. dev_info(&pdev->dev, "New device successfully probed\n");
  249. goto exit;
  250. put_adapter:
  251. i2c_put_adapter(adapter);
  252. unregister_v4l2_dev:
  253. v4l2_device_unregister(&rsdev->v4l2_dev);
  254. exit:
  255. return rval;
  256. }
  257. /* radio_si4713_pdriver_remove - remove the device */
  258. static int radio_si4713_pdriver_remove(struct platform_device *pdev)
  259. {
  260. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  261. struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
  262. struct v4l2_subdev, list);
  263. struct i2c_client *client = v4l2_get_subdevdata(sd);
  264. struct radio_si4713_device *rsdev;
  265. rsdev = container_of(v4l2_dev, struct radio_si4713_device, v4l2_dev);
  266. video_unregister_device(&rsdev->radio_dev);
  267. i2c_put_adapter(client->adapter);
  268. v4l2_device_unregister(&rsdev->v4l2_dev);
  269. return 0;
  270. }
  271. static struct platform_driver radio_si4713_pdriver = {
  272. .driver = {
  273. .name = "radio-si4713",
  274. .owner = THIS_MODULE,
  275. },
  276. .probe = radio_si4713_pdriver_probe,
  277. .remove = radio_si4713_pdriver_remove,
  278. };
  279. module_platform_driver(radio_si4713_pdriver);