radio-timb.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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/mfd/core.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/slab.h>
  26. #include <linux/i2c.h>
  27. #include <media/timb_radio.h>
  28. #define DRIVER_NAME "timb-radio"
  29. struct timbradio {
  30. struct timb_radio_platform_data pdata;
  31. struct v4l2_subdev *sd_tuner;
  32. struct v4l2_subdev *sd_dsp;
  33. struct video_device video_dev;
  34. struct v4l2_device v4l2_dev;
  35. struct mutex lock;
  36. };
  37. static int timbradio_vidioc_querycap(struct file *file, void *priv,
  38. struct v4l2_capability *v)
  39. {
  40. strlcpy(v->driver, DRIVER_NAME, sizeof(v->driver));
  41. strlcpy(v->card, "Timberdale Radio", sizeof(v->card));
  42. snprintf(v->bus_info, sizeof(v->bus_info), "platform:"DRIVER_NAME);
  43. v->version = KERNEL_VERSION(0, 0, 1);
  44. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  45. return 0;
  46. }
  47. static int timbradio_vidioc_g_tuner(struct file *file, void *priv,
  48. struct v4l2_tuner *v)
  49. {
  50. struct timbradio *tr = video_drvdata(file);
  51. return v4l2_subdev_call(tr->sd_tuner, tuner, g_tuner, v);
  52. }
  53. static int timbradio_vidioc_s_tuner(struct file *file, void *priv,
  54. struct v4l2_tuner *v)
  55. {
  56. struct timbradio *tr = video_drvdata(file);
  57. return v4l2_subdev_call(tr->sd_tuner, tuner, s_tuner, v);
  58. }
  59. static int timbradio_vidioc_g_input(struct file *filp, void *priv,
  60. unsigned int *i)
  61. {
  62. *i = 0;
  63. return 0;
  64. }
  65. static int timbradio_vidioc_s_input(struct file *filp, void *priv,
  66. unsigned int i)
  67. {
  68. return i ? -EINVAL : 0;
  69. }
  70. static int timbradio_vidioc_g_audio(struct file *file, void *priv,
  71. struct v4l2_audio *a)
  72. {
  73. a->index = 0;
  74. strlcpy(a->name, "Radio", sizeof(a->name));
  75. a->capability = V4L2_AUDCAP_STEREO;
  76. return 0;
  77. }
  78. static int timbradio_vidioc_s_audio(struct file *file, void *priv,
  79. struct v4l2_audio *a)
  80. {
  81. return a->index ? -EINVAL : 0;
  82. }
  83. static int timbradio_vidioc_s_frequency(struct file *file, void *priv,
  84. struct v4l2_frequency *f)
  85. {
  86. struct timbradio *tr = video_drvdata(file);
  87. return v4l2_subdev_call(tr->sd_tuner, tuner, s_frequency, f);
  88. }
  89. static int timbradio_vidioc_g_frequency(struct file *file, void *priv,
  90. struct v4l2_frequency *f)
  91. {
  92. struct timbradio *tr = video_drvdata(file);
  93. return v4l2_subdev_call(tr->sd_tuner, tuner, g_frequency, f);
  94. }
  95. static int timbradio_vidioc_queryctrl(struct file *file, void *priv,
  96. struct v4l2_queryctrl *qc)
  97. {
  98. struct timbradio *tr = video_drvdata(file);
  99. return v4l2_subdev_call(tr->sd_dsp, core, queryctrl, qc);
  100. }
  101. static int timbradio_vidioc_g_ctrl(struct file *file, void *priv,
  102. struct v4l2_control *ctrl)
  103. {
  104. struct timbradio *tr = video_drvdata(file);
  105. return v4l2_subdev_call(tr->sd_dsp, core, g_ctrl, ctrl);
  106. }
  107. static int timbradio_vidioc_s_ctrl(struct file *file, void *priv,
  108. struct v4l2_control *ctrl)
  109. {
  110. struct timbradio *tr = video_drvdata(file);
  111. return v4l2_subdev_call(tr->sd_dsp, core, s_ctrl, ctrl);
  112. }
  113. static const struct v4l2_ioctl_ops timbradio_ioctl_ops = {
  114. .vidioc_querycap = timbradio_vidioc_querycap,
  115. .vidioc_g_tuner = timbradio_vidioc_g_tuner,
  116. .vidioc_s_tuner = timbradio_vidioc_s_tuner,
  117. .vidioc_g_frequency = timbradio_vidioc_g_frequency,
  118. .vidioc_s_frequency = timbradio_vidioc_s_frequency,
  119. .vidioc_g_input = timbradio_vidioc_g_input,
  120. .vidioc_s_input = timbradio_vidioc_s_input,
  121. .vidioc_g_audio = timbradio_vidioc_g_audio,
  122. .vidioc_s_audio = timbradio_vidioc_s_audio,
  123. .vidioc_queryctrl = timbradio_vidioc_queryctrl,
  124. .vidioc_g_ctrl = timbradio_vidioc_g_ctrl,
  125. .vidioc_s_ctrl = timbradio_vidioc_s_ctrl
  126. };
  127. static const struct v4l2_file_operations timbradio_fops = {
  128. .owner = THIS_MODULE,
  129. .unlocked_ioctl = video_ioctl2,
  130. };
  131. static int __devinit timbradio_probe(struct platform_device *pdev)
  132. {
  133. struct timb_radio_platform_data *pdata = mfd_get_data(pdev);
  134. struct timbradio *tr;
  135. int err;
  136. if (!pdata) {
  137. dev_err(&pdev->dev, "Platform data missing\n");
  138. err = -EINVAL;
  139. goto err;
  140. }
  141. tr = kzalloc(sizeof(*tr), GFP_KERNEL);
  142. if (!tr) {
  143. err = -ENOMEM;
  144. goto err;
  145. }
  146. tr->pdata = *pdata;
  147. mutex_init(&tr->lock);
  148. strlcpy(tr->video_dev.name, "Timberdale Radio",
  149. sizeof(tr->video_dev.name));
  150. tr->video_dev.fops = &timbradio_fops;
  151. tr->video_dev.ioctl_ops = &timbradio_ioctl_ops;
  152. tr->video_dev.release = video_device_release_empty;
  153. tr->video_dev.minor = -1;
  154. tr->video_dev.lock = &tr->lock;
  155. strlcpy(tr->v4l2_dev.name, DRIVER_NAME, sizeof(tr->v4l2_dev.name));
  156. err = v4l2_device_register(NULL, &tr->v4l2_dev);
  157. if (err)
  158. goto err_v4l2_dev;
  159. tr->video_dev.v4l2_dev = &tr->v4l2_dev;
  160. err = video_register_device(&tr->video_dev, VFL_TYPE_RADIO, -1);
  161. if (err) {
  162. dev_err(&pdev->dev, "Error reg video\n");
  163. goto err_video_req;
  164. }
  165. video_set_drvdata(&tr->video_dev, tr);
  166. platform_set_drvdata(pdev, tr);
  167. return 0;
  168. err_video_req:
  169. video_device_release_empty(&tr->video_dev);
  170. v4l2_device_unregister(&tr->v4l2_dev);
  171. err_v4l2_dev:
  172. kfree(tr);
  173. err:
  174. dev_err(&pdev->dev, "Failed to register: %d\n", err);
  175. return err;
  176. }
  177. static int __devexit timbradio_remove(struct platform_device *pdev)
  178. {
  179. struct timbradio *tr = platform_get_drvdata(pdev);
  180. video_unregister_device(&tr->video_dev);
  181. video_device_release_empty(&tr->video_dev);
  182. v4l2_device_unregister(&tr->v4l2_dev);
  183. kfree(tr);
  184. return 0;
  185. }
  186. static struct platform_driver timbradio_platform_driver = {
  187. .driver = {
  188. .name = DRIVER_NAME,
  189. .owner = THIS_MODULE,
  190. },
  191. .probe = timbradio_probe,
  192. .remove = timbradio_remove,
  193. };
  194. /*--------------------------------------------------------------------------*/
  195. static int __init timbradio_init(void)
  196. {
  197. return platform_driver_register(&timbradio_platform_driver);
  198. }
  199. static void __exit timbradio_exit(void)
  200. {
  201. platform_driver_unregister(&timbradio_platform_driver);
  202. }
  203. module_init(timbradio_init);
  204. module_exit(timbradio_exit);
  205. MODULE_DESCRIPTION("Timberdale Radio driver");
  206. MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
  207. MODULE_LICENSE("GPL v2");
  208. MODULE_ALIAS("platform:"DRIVER_NAME);