radio-timb.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * radio-timb.c Timberdale FPGA Radio driver
  3. * Copyright (c) 2009 Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/version.h>
  19. #include <linux/io.h>
  20. #include <media/v4l2-ioctl.h>
  21. #include <media/v4l2-device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/slab.h>
  25. #include <linux/i2c.h>
  26. #include <media/timb_radio.h>
  27. #define DRIVER_NAME "timb-radio"
  28. struct timbradio {
  29. struct timb_radio_platform_data pdata;
  30. struct v4l2_subdev *sd_tuner;
  31. struct v4l2_subdev *sd_dsp;
  32. struct video_device video_dev;
  33. struct v4l2_device v4l2_dev;
  34. struct mutex lock;
  35. };
  36. static int timbradio_vidioc_querycap(struct file *file, void *priv,
  37. struct v4l2_capability *v)
  38. {
  39. strlcpy(v->driver, DRIVER_NAME, sizeof(v->driver));
  40. strlcpy(v->card, "Timberdale Radio", sizeof(v->card));
  41. snprintf(v->bus_info, sizeof(v->bus_info), "platform:"DRIVER_NAME);
  42. v->version = KERNEL_VERSION(0, 0, 1);
  43. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  44. return 0;
  45. }
  46. static int timbradio_vidioc_g_tuner(struct file *file, void *priv,
  47. struct v4l2_tuner *v)
  48. {
  49. struct timbradio *tr = video_drvdata(file);
  50. return v4l2_subdev_call(tr->sd_tuner, tuner, g_tuner, v);
  51. }
  52. static int timbradio_vidioc_s_tuner(struct file *file, void *priv,
  53. struct v4l2_tuner *v)
  54. {
  55. struct timbradio *tr = video_drvdata(file);
  56. return v4l2_subdev_call(tr->sd_tuner, tuner, s_tuner, v);
  57. }
  58. static int timbradio_vidioc_g_input(struct file *filp, void *priv,
  59. unsigned int *i)
  60. {
  61. *i = 0;
  62. return 0;
  63. }
  64. static int timbradio_vidioc_s_input(struct file *filp, void *priv,
  65. unsigned int i)
  66. {
  67. return i ? -EINVAL : 0;
  68. }
  69. static int timbradio_vidioc_g_audio(struct file *file, void *priv,
  70. struct v4l2_audio *a)
  71. {
  72. a->index = 0;
  73. strlcpy(a->name, "Radio", sizeof(a->name));
  74. a->capability = V4L2_AUDCAP_STEREO;
  75. return 0;
  76. }
  77. static int timbradio_vidioc_s_audio(struct file *file, void *priv,
  78. struct v4l2_audio *a)
  79. {
  80. return a->index ? -EINVAL : 0;
  81. }
  82. static int timbradio_vidioc_s_frequency(struct file *file, void *priv,
  83. struct v4l2_frequency *f)
  84. {
  85. struct timbradio *tr = video_drvdata(file);
  86. return v4l2_subdev_call(tr->sd_tuner, tuner, s_frequency, f);
  87. }
  88. static int timbradio_vidioc_g_frequency(struct file *file, void *priv,
  89. struct v4l2_frequency *f)
  90. {
  91. struct timbradio *tr = video_drvdata(file);
  92. return v4l2_subdev_call(tr->sd_tuner, tuner, g_frequency, f);
  93. }
  94. static int timbradio_vidioc_queryctrl(struct file *file, void *priv,
  95. struct v4l2_queryctrl *qc)
  96. {
  97. struct timbradio *tr = video_drvdata(file);
  98. return v4l2_subdev_call(tr->sd_dsp, core, queryctrl, qc);
  99. }
  100. static int timbradio_vidioc_g_ctrl(struct file *file, void *priv,
  101. struct v4l2_control *ctrl)
  102. {
  103. struct timbradio *tr = video_drvdata(file);
  104. return v4l2_subdev_call(tr->sd_dsp, core, g_ctrl, ctrl);
  105. }
  106. static int timbradio_vidioc_s_ctrl(struct file *file, void *priv,
  107. struct v4l2_control *ctrl)
  108. {
  109. struct timbradio *tr = video_drvdata(file);
  110. return v4l2_subdev_call(tr->sd_dsp, core, s_ctrl, ctrl);
  111. }
  112. static const struct v4l2_ioctl_ops timbradio_ioctl_ops = {
  113. .vidioc_querycap = timbradio_vidioc_querycap,
  114. .vidioc_g_tuner = timbradio_vidioc_g_tuner,
  115. .vidioc_s_tuner = timbradio_vidioc_s_tuner,
  116. .vidioc_g_frequency = timbradio_vidioc_g_frequency,
  117. .vidioc_s_frequency = timbradio_vidioc_s_frequency,
  118. .vidioc_g_input = timbradio_vidioc_g_input,
  119. .vidioc_s_input = timbradio_vidioc_s_input,
  120. .vidioc_g_audio = timbradio_vidioc_g_audio,
  121. .vidioc_s_audio = timbradio_vidioc_s_audio,
  122. .vidioc_queryctrl = timbradio_vidioc_queryctrl,
  123. .vidioc_g_ctrl = timbradio_vidioc_g_ctrl,
  124. .vidioc_s_ctrl = timbradio_vidioc_s_ctrl
  125. };
  126. static const struct v4l2_file_operations timbradio_fops = {
  127. .owner = THIS_MODULE,
  128. .unlocked_ioctl = video_ioctl2,
  129. };
  130. static int __devinit timbradio_probe(struct platform_device *pdev)
  131. {
  132. struct timb_radio_platform_data *pdata = pdev->dev.platform_data;
  133. struct timbradio *tr;
  134. int err;
  135. if (!pdata) {
  136. dev_err(&pdev->dev, "Platform data missing\n");
  137. err = -EINVAL;
  138. goto err;
  139. }
  140. tr = kzalloc(sizeof(*tr), GFP_KERNEL);
  141. if (!tr) {
  142. err = -ENOMEM;
  143. goto err;
  144. }
  145. tr->pdata = *pdata;
  146. mutex_init(&tr->lock);
  147. strlcpy(tr->video_dev.name, "Timberdale Radio",
  148. sizeof(tr->video_dev.name));
  149. tr->video_dev.fops = &timbradio_fops;
  150. tr->video_dev.ioctl_ops = &timbradio_ioctl_ops;
  151. tr->video_dev.release = video_device_release_empty;
  152. tr->video_dev.minor = -1;
  153. tr->video_dev.lock = &tr->lock;
  154. strlcpy(tr->v4l2_dev.name, DRIVER_NAME, sizeof(tr->v4l2_dev.name));
  155. err = v4l2_device_register(NULL, &tr->v4l2_dev);
  156. if (err)
  157. goto err_v4l2_dev;
  158. tr->video_dev.v4l2_dev = &tr->v4l2_dev;
  159. err = video_register_device(&tr->video_dev, VFL_TYPE_RADIO, -1);
  160. if (err) {
  161. dev_err(&pdev->dev, "Error reg video\n");
  162. goto err_video_req;
  163. }
  164. video_set_drvdata(&tr->video_dev, tr);
  165. platform_set_drvdata(pdev, tr);
  166. return 0;
  167. err_video_req:
  168. video_device_release_empty(&tr->video_dev);
  169. v4l2_device_unregister(&tr->v4l2_dev);
  170. err_v4l2_dev:
  171. kfree(tr);
  172. err:
  173. dev_err(&pdev->dev, "Failed to register: %d\n", err);
  174. return err;
  175. }
  176. static int __devexit timbradio_remove(struct platform_device *pdev)
  177. {
  178. struct timbradio *tr = platform_get_drvdata(pdev);
  179. video_unregister_device(&tr->video_dev);
  180. video_device_release_empty(&tr->video_dev);
  181. v4l2_device_unregister(&tr->v4l2_dev);
  182. kfree(tr);
  183. return 0;
  184. }
  185. static struct platform_driver timbradio_platform_driver = {
  186. .driver = {
  187. .name = DRIVER_NAME,
  188. .owner = THIS_MODULE,
  189. },
  190. .probe = timbradio_probe,
  191. .remove = timbradio_remove,
  192. };
  193. /*--------------------------------------------------------------------------*/
  194. static int __init timbradio_init(void)
  195. {
  196. return platform_driver_register(&timbradio_platform_driver);
  197. }
  198. static void __exit timbradio_exit(void)
  199. {
  200. platform_driver_unregister(&timbradio_platform_driver);
  201. }
  202. module_init(timbradio_init);
  203. module_exit(timbradio_exit);
  204. MODULE_DESCRIPTION("Timberdale Radio driver");
  205. MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
  206. MODULE_LICENSE("GPL v2");
  207. MODULE_ALIAS("platform:"DRIVER_NAME);