radio-timb.c 5.1 KB

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