radio-platform-si4713.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/v4l2-fh.h>
  34. #include <media/v4l2-ctrls.h>
  35. #include <media/v4l2-event.h>
  36. #include "si4713.h"
  37. /* module parameters */
  38. static int radio_nr = -1; /* radio device minor (-1 ==> auto assign) */
  39. module_param(radio_nr, int, 0);
  40. MODULE_PARM_DESC(radio_nr,
  41. "Minor number for radio device (-1 ==> auto assign)");
  42. MODULE_LICENSE("GPL v2");
  43. MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
  44. MODULE_DESCRIPTION("Platform driver for Si4713 FM Radio Transmitter");
  45. MODULE_VERSION("0.0.1");
  46. MODULE_ALIAS("platform:radio-si4713");
  47. /* Driver state struct */
  48. struct radio_si4713_device {
  49. struct v4l2_device v4l2_dev;
  50. struct video_device radio_dev;
  51. struct mutex lock;
  52. };
  53. /* radio_si4713_fops - file operations interface */
  54. static const struct v4l2_file_operations radio_si4713_fops = {
  55. .owner = THIS_MODULE,
  56. .open = v4l2_fh_open,
  57. .release = v4l2_fh_release,
  58. .poll = v4l2_ctrl_poll,
  59. /* Note: locking is done at the subdev level in the i2c driver. */
  60. .unlocked_ioctl = video_ioctl2,
  61. };
  62. /* Video4Linux Interface */
  63. /* radio_si4713_querycap - query device capabilities */
  64. static int radio_si4713_querycap(struct file *file, void *priv,
  65. struct v4l2_capability *capability)
  66. {
  67. strlcpy(capability->driver, "radio-si4713", sizeof(capability->driver));
  68. strlcpy(capability->card, "Silicon Labs Si4713 Modulator",
  69. sizeof(capability->card));
  70. strlcpy(capability->bus_info, "platform:radio-si4713",
  71. sizeof(capability->bus_info));
  72. capability->device_caps = V4L2_CAP_MODULATOR | V4L2_CAP_RDS_OUTPUT;
  73. capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS;
  74. return 0;
  75. }
  76. /*
  77. * v4l2 ioctl call backs.
  78. * we are just a wrapper for v4l2_sub_devs.
  79. */
  80. static inline struct v4l2_device *get_v4l2_dev(struct file *file)
  81. {
  82. return &((struct radio_si4713_device *)video_drvdata(file))->v4l2_dev;
  83. }
  84. static int radio_si4713_g_modulator(struct file *file, void *p,
  85. struct v4l2_modulator *vm)
  86. {
  87. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  88. g_modulator, vm);
  89. }
  90. static int radio_si4713_s_modulator(struct file *file, void *p,
  91. const struct v4l2_modulator *vm)
  92. {
  93. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  94. s_modulator, vm);
  95. }
  96. static int radio_si4713_g_frequency(struct file *file, void *p,
  97. struct v4l2_frequency *vf)
  98. {
  99. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  100. g_frequency, vf);
  101. }
  102. static int radio_si4713_s_frequency(struct file *file, void *p,
  103. const struct v4l2_frequency *vf)
  104. {
  105. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
  106. s_frequency, vf);
  107. }
  108. static long radio_si4713_default(struct file *file, void *p,
  109. bool valid_prio, unsigned int cmd, void *arg)
  110. {
  111. return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
  112. ioctl, cmd, arg);
  113. }
  114. static struct v4l2_ioctl_ops radio_si4713_ioctl_ops = {
  115. .vidioc_querycap = radio_si4713_querycap,
  116. .vidioc_g_modulator = radio_si4713_g_modulator,
  117. .vidioc_s_modulator = radio_si4713_s_modulator,
  118. .vidioc_g_frequency = radio_si4713_g_frequency,
  119. .vidioc_s_frequency = radio_si4713_s_frequency,
  120. .vidioc_log_status = v4l2_ctrl_log_status,
  121. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  122. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  123. .vidioc_default = radio_si4713_default,
  124. };
  125. /* radio_si4713_vdev_template - video device interface */
  126. static struct video_device radio_si4713_vdev_template = {
  127. .fops = &radio_si4713_fops,
  128. .name = "radio-si4713",
  129. .release = video_device_release_empty,
  130. .ioctl_ops = &radio_si4713_ioctl_ops,
  131. .vfl_dir = VFL_DIR_TX,
  132. };
  133. /* Platform driver interface */
  134. /* radio_si4713_pdriver_probe - probe for the device */
  135. static int radio_si4713_pdriver_probe(struct platform_device *pdev)
  136. {
  137. struct radio_si4713_platform_data *pdata = pdev->dev.platform_data;
  138. struct radio_si4713_device *rsdev;
  139. struct v4l2_subdev *sd;
  140. int rval = 0;
  141. if (!pdata) {
  142. dev_err(&pdev->dev, "Cannot proceed without platform data.\n");
  143. rval = -EINVAL;
  144. goto exit;
  145. }
  146. rsdev = devm_kzalloc(&pdev->dev, sizeof(*rsdev), GFP_KERNEL);
  147. if (!rsdev) {
  148. dev_err(&pdev->dev, "Failed to alloc video device.\n");
  149. rval = -ENOMEM;
  150. goto exit;
  151. }
  152. mutex_init(&rsdev->lock);
  153. rval = v4l2_device_register(&pdev->dev, &rsdev->v4l2_dev);
  154. if (rval) {
  155. dev_err(&pdev->dev, "Failed to register v4l2 device.\n");
  156. goto exit;
  157. }
  158. sd = i2c_get_clientdata(pdata->subdev);
  159. rval = v4l2_device_register_subdev(&rsdev->v4l2_dev, sd);
  160. if (rval) {
  161. dev_err(&pdev->dev, "Cannot get v4l2 subdevice\n");
  162. goto unregister_v4l2_dev;
  163. }
  164. rsdev->radio_dev = radio_si4713_vdev_template;
  165. rsdev->radio_dev.v4l2_dev = &rsdev->v4l2_dev;
  166. rsdev->radio_dev.ctrl_handler = sd->ctrl_handler;
  167. /* Serialize all access to the si4713 */
  168. rsdev->radio_dev.lock = &rsdev->lock;
  169. video_set_drvdata(&rsdev->radio_dev, rsdev);
  170. if (video_register_device(&rsdev->radio_dev, VFL_TYPE_RADIO, radio_nr)) {
  171. dev_err(&pdev->dev, "Could not register video device.\n");
  172. rval = -EIO;
  173. goto unregister_v4l2_dev;
  174. }
  175. dev_info(&pdev->dev, "New device successfully probed\n");
  176. goto exit;
  177. unregister_v4l2_dev:
  178. v4l2_device_unregister(&rsdev->v4l2_dev);
  179. exit:
  180. return rval;
  181. }
  182. /* radio_si4713_pdriver_remove - remove the device */
  183. static int radio_si4713_pdriver_remove(struct platform_device *pdev)
  184. {
  185. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  186. struct radio_si4713_device *rsdev;
  187. rsdev = container_of(v4l2_dev, struct radio_si4713_device, v4l2_dev);
  188. video_unregister_device(&rsdev->radio_dev);
  189. v4l2_device_unregister(&rsdev->v4l2_dev);
  190. return 0;
  191. }
  192. static struct platform_driver radio_si4713_pdriver = {
  193. .driver = {
  194. .name = "radio-si4713",
  195. },
  196. .probe = radio_si4713_pdriver_probe,
  197. .remove = radio_si4713_pdriver_remove,
  198. };
  199. module_platform_driver(radio_si4713_pdriver);