radio-platform-si4713.c 6.2 KB

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