radio-timb.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/io.h>
  19. #include <media/v4l2-ioctl.h>
  20. #include <media/v4l2-device.h>
  21. #include <media/v4l2-ctrls.h>
  22. #include <media/v4l2-event.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/slab.h>
  26. #include <linux/i2c.h>
  27. #include <linux/module.h>
  28. #include <media/timb_radio.h>
  29. #define DRIVER_NAME "timb-radio"
  30. struct timbradio {
  31. struct timb_radio_platform_data pdata;
  32. struct v4l2_subdev *sd_tuner;
  33. struct v4l2_subdev *sd_dsp;
  34. struct video_device video_dev;
  35. struct v4l2_device v4l2_dev;
  36. struct mutex lock;
  37. };
  38. static int timbradio_vidioc_querycap(struct file *file, void *priv,
  39. struct v4l2_capability *v)
  40. {
  41. strlcpy(v->driver, DRIVER_NAME, sizeof(v->driver));
  42. strlcpy(v->card, "Timberdale Radio", sizeof(v->card));
  43. snprintf(v->bus_info, sizeof(v->bus_info), "platform:"DRIVER_NAME);
  44. v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  45. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  46. return 0;
  47. }
  48. static int timbradio_vidioc_g_tuner(struct file *file, void *priv,
  49. struct v4l2_tuner *v)
  50. {
  51. struct timbradio *tr = video_drvdata(file);
  52. return v4l2_subdev_call(tr->sd_tuner, tuner, g_tuner, v);
  53. }
  54. static int timbradio_vidioc_s_tuner(struct file *file, void *priv,
  55. const struct v4l2_tuner *v)
  56. {
  57. struct timbradio *tr = video_drvdata(file);
  58. return v4l2_subdev_call(tr->sd_tuner, tuner, s_tuner, v);
  59. }
  60. static int timbradio_vidioc_s_frequency(struct file *file, void *priv,
  61. const struct v4l2_frequency *f)
  62. {
  63. struct timbradio *tr = video_drvdata(file);
  64. return v4l2_subdev_call(tr->sd_tuner, tuner, s_frequency, f);
  65. }
  66. static int timbradio_vidioc_g_frequency(struct file *file, void *priv,
  67. struct v4l2_frequency *f)
  68. {
  69. struct timbradio *tr = video_drvdata(file);
  70. return v4l2_subdev_call(tr->sd_tuner, tuner, g_frequency, f);
  71. }
  72. static const struct v4l2_ioctl_ops timbradio_ioctl_ops = {
  73. .vidioc_querycap = timbradio_vidioc_querycap,
  74. .vidioc_g_tuner = timbradio_vidioc_g_tuner,
  75. .vidioc_s_tuner = timbradio_vidioc_s_tuner,
  76. .vidioc_g_frequency = timbradio_vidioc_g_frequency,
  77. .vidioc_s_frequency = timbradio_vidioc_s_frequency,
  78. .vidioc_log_status = v4l2_ctrl_log_status,
  79. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  80. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  81. };
  82. static const struct v4l2_file_operations timbradio_fops = {
  83. .owner = THIS_MODULE,
  84. .open = v4l2_fh_open,
  85. .release = v4l2_fh_release,
  86. .poll = v4l2_ctrl_poll,
  87. .unlocked_ioctl = video_ioctl2,
  88. };
  89. static int timbradio_probe(struct platform_device *pdev)
  90. {
  91. struct timb_radio_platform_data *pdata = pdev->dev.platform_data;
  92. struct timbradio *tr;
  93. int err;
  94. if (!pdata) {
  95. dev_err(&pdev->dev, "Platform data missing\n");
  96. err = -EINVAL;
  97. goto err;
  98. }
  99. tr = devm_kzalloc(&pdev->dev, sizeof(*tr), GFP_KERNEL);
  100. if (!tr) {
  101. err = -ENOMEM;
  102. goto err;
  103. }
  104. tr->pdata = *pdata;
  105. mutex_init(&tr->lock);
  106. strlcpy(tr->video_dev.name, "Timberdale Radio",
  107. sizeof(tr->video_dev.name));
  108. tr->video_dev.fops = &timbradio_fops;
  109. tr->video_dev.ioctl_ops = &timbradio_ioctl_ops;
  110. tr->video_dev.release = video_device_release_empty;
  111. tr->video_dev.minor = -1;
  112. tr->video_dev.lock = &tr->lock;
  113. strlcpy(tr->v4l2_dev.name, DRIVER_NAME, sizeof(tr->v4l2_dev.name));
  114. err = v4l2_device_register(NULL, &tr->v4l2_dev);
  115. if (err)
  116. goto err;
  117. tr->video_dev.v4l2_dev = &tr->v4l2_dev;
  118. tr->sd_tuner = v4l2_i2c_new_subdev_board(&tr->v4l2_dev,
  119. i2c_get_adapter(pdata->i2c_adapter), pdata->tuner, NULL);
  120. tr->sd_dsp = v4l2_i2c_new_subdev_board(&tr->v4l2_dev,
  121. i2c_get_adapter(pdata->i2c_adapter), pdata->dsp, NULL);
  122. if (tr->sd_tuner == NULL || tr->sd_dsp == NULL) {
  123. err = -ENODEV;
  124. goto err_video_req;
  125. }
  126. tr->v4l2_dev.ctrl_handler = tr->sd_dsp->ctrl_handler;
  127. err = video_register_device(&tr->video_dev, VFL_TYPE_RADIO, -1);
  128. if (err) {
  129. dev_err(&pdev->dev, "Error reg video\n");
  130. goto err_video_req;
  131. }
  132. video_set_drvdata(&tr->video_dev, tr);
  133. platform_set_drvdata(pdev, tr);
  134. return 0;
  135. err_video_req:
  136. v4l2_device_unregister(&tr->v4l2_dev);
  137. err:
  138. dev_err(&pdev->dev, "Failed to register: %d\n", err);
  139. return err;
  140. }
  141. static int timbradio_remove(struct platform_device *pdev)
  142. {
  143. struct timbradio *tr = platform_get_drvdata(pdev);
  144. video_unregister_device(&tr->video_dev);
  145. v4l2_device_unregister(&tr->v4l2_dev);
  146. return 0;
  147. }
  148. static struct platform_driver timbradio_platform_driver = {
  149. .driver = {
  150. .name = DRIVER_NAME,
  151. },
  152. .probe = timbradio_probe,
  153. .remove = timbradio_remove,
  154. };
  155. module_platform_driver(timbradio_platform_driver);
  156. MODULE_DESCRIPTION("Timberdale Radio driver");
  157. MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
  158. MODULE_LICENSE("GPL v2");
  159. MODULE_VERSION("0.0.2");
  160. MODULE_ALIAS("platform:"DRIVER_NAME);