radio-timb.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 <linux/platform_device.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/slab.h>
  24. #include <linux/i2c.h>
  25. #include <linux/module.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->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  43. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  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. const 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_s_frequency(struct file *file, void *priv,
  59. const struct v4l2_frequency *f)
  60. {
  61. struct timbradio *tr = video_drvdata(file);
  62. return v4l2_subdev_call(tr->sd_tuner, tuner, s_frequency, f);
  63. }
  64. static int timbradio_vidioc_g_frequency(struct file *file, void *priv,
  65. struct v4l2_frequency *f)
  66. {
  67. struct timbradio *tr = video_drvdata(file);
  68. return v4l2_subdev_call(tr->sd_tuner, tuner, g_frequency, f);
  69. }
  70. static const struct v4l2_ioctl_ops timbradio_ioctl_ops = {
  71. .vidioc_querycap = timbradio_vidioc_querycap,
  72. .vidioc_g_tuner = timbradio_vidioc_g_tuner,
  73. .vidioc_s_tuner = timbradio_vidioc_s_tuner,
  74. .vidioc_g_frequency = timbradio_vidioc_g_frequency,
  75. .vidioc_s_frequency = timbradio_vidioc_s_frequency,
  76. };
  77. static const struct v4l2_file_operations timbradio_fops = {
  78. .owner = THIS_MODULE,
  79. .unlocked_ioctl = video_ioctl2,
  80. };
  81. static int timbradio_probe(struct platform_device *pdev)
  82. {
  83. struct timb_radio_platform_data *pdata = pdev->dev.platform_data;
  84. struct timbradio *tr;
  85. int err;
  86. if (!pdata) {
  87. dev_err(&pdev->dev, "Platform data missing\n");
  88. err = -EINVAL;
  89. goto err;
  90. }
  91. tr = devm_kzalloc(&pdev->dev, sizeof(*tr), GFP_KERNEL);
  92. if (!tr) {
  93. err = -ENOMEM;
  94. goto err;
  95. }
  96. tr->pdata = *pdata;
  97. mutex_init(&tr->lock);
  98. strlcpy(tr->video_dev.name, "Timberdale Radio",
  99. sizeof(tr->video_dev.name));
  100. tr->video_dev.fops = &timbradio_fops;
  101. tr->video_dev.ioctl_ops = &timbradio_ioctl_ops;
  102. tr->video_dev.release = video_device_release_empty;
  103. tr->video_dev.minor = -1;
  104. tr->video_dev.lock = &tr->lock;
  105. strlcpy(tr->v4l2_dev.name, DRIVER_NAME, sizeof(tr->v4l2_dev.name));
  106. err = v4l2_device_register(NULL, &tr->v4l2_dev);
  107. if (err)
  108. goto err;
  109. tr->video_dev.v4l2_dev = &tr->v4l2_dev;
  110. tr->sd_tuner = v4l2_i2c_new_subdev_board(&tr->v4l2_dev,
  111. i2c_get_adapter(pdata->i2c_adapter), pdata->tuner, NULL);
  112. tr->sd_dsp = v4l2_i2c_new_subdev_board(&tr->v4l2_dev,
  113. i2c_get_adapter(pdata->i2c_adapter), pdata->dsp, NULL);
  114. if (tr->sd_tuner == NULL || tr->sd_dsp == NULL)
  115. goto err_video_req;
  116. tr->v4l2_dev.ctrl_handler = tr->sd_dsp->ctrl_handler;
  117. err = video_register_device(&tr->video_dev, VFL_TYPE_RADIO, -1);
  118. if (err) {
  119. dev_err(&pdev->dev, "Error reg video\n");
  120. goto err_video_req;
  121. }
  122. video_set_drvdata(&tr->video_dev, tr);
  123. platform_set_drvdata(pdev, tr);
  124. return 0;
  125. err_video_req:
  126. v4l2_device_unregister(&tr->v4l2_dev);
  127. err:
  128. dev_err(&pdev->dev, "Failed to register: %d\n", err);
  129. return err;
  130. }
  131. static int timbradio_remove(struct platform_device *pdev)
  132. {
  133. struct timbradio *tr = platform_get_drvdata(pdev);
  134. video_unregister_device(&tr->video_dev);
  135. v4l2_device_unregister(&tr->v4l2_dev);
  136. return 0;
  137. }
  138. static struct platform_driver timbradio_platform_driver = {
  139. .driver = {
  140. .name = DRIVER_NAME,
  141. .owner = THIS_MODULE,
  142. },
  143. .probe = timbradio_probe,
  144. .remove = timbradio_remove,
  145. };
  146. module_platform_driver(timbradio_platform_driver);
  147. MODULE_DESCRIPTION("Timberdale Radio driver");
  148. MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
  149. MODULE_LICENSE("GPL v2");
  150. MODULE_VERSION("0.0.2");
  151. MODULE_ALIAS("platform:"DRIVER_NAME);