radio-platform-si4713.c 6.7 KB

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