Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 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 | |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 19 | #include <linux/io.h> |
| 20 | #include <media/v4l2-ioctl.h> |
| 21 | #include <media/v4l2-device.h> |
Hans Verkuil | 46821b1 | 2013-02-03 09:33:21 -0300 | [diff] [blame] | 22 | #include <media/v4l2-ctrls.h> |
| 23 | #include <media/v4l2-event.h> |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/interrupt.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 27 | #include <linux/i2c.h> |
Paul Gortmaker | 7a707b8 | 2011-07-03 14:03:12 -0400 | [diff] [blame] | 28 | #include <linux/module.h> |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 29 | #include <media/timb_radio.h> |
| 30 | |
| 31 | #define DRIVER_NAME "timb-radio" |
| 32 | |
| 33 | struct timbradio { |
| 34 | struct timb_radio_platform_data pdata; |
| 35 | struct v4l2_subdev *sd_tuner; |
| 36 | struct v4l2_subdev *sd_dsp; |
| 37 | struct video_device video_dev; |
| 38 | struct v4l2_device v4l2_dev; |
Hans Verkuil | 4f68775 | 2010-11-16 18:13:06 -0300 | [diff] [blame] | 39 | struct mutex lock; |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | |
| 43 | static int timbradio_vidioc_querycap(struct file *file, void *priv, |
| 44 | struct v4l2_capability *v) |
| 45 | { |
| 46 | strlcpy(v->driver, DRIVER_NAME, sizeof(v->driver)); |
| 47 | strlcpy(v->card, "Timberdale Radio", sizeof(v->card)); |
| 48 | snprintf(v->bus_info, sizeof(v->bus_info), "platform:"DRIVER_NAME); |
Hans Verkuil | d020f83 | 2013-02-03 09:05:16 -0300 | [diff] [blame] | 49 | v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
| 50 | v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS; |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int timbradio_vidioc_g_tuner(struct file *file, void *priv, |
| 55 | struct v4l2_tuner *v) |
| 56 | { |
| 57 | struct timbradio *tr = video_drvdata(file); |
| 58 | return v4l2_subdev_call(tr->sd_tuner, tuner, g_tuner, v); |
| 59 | } |
| 60 | |
| 61 | static int timbradio_vidioc_s_tuner(struct file *file, void *priv, |
Hans Verkuil | 2f73c7c | 2013-03-15 06:10:06 -0300 | [diff] [blame] | 62 | const struct v4l2_tuner *v) |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 63 | { |
| 64 | struct timbradio *tr = video_drvdata(file); |
| 65 | return v4l2_subdev_call(tr->sd_tuner, tuner, s_tuner, v); |
| 66 | } |
| 67 | |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 68 | static int timbradio_vidioc_s_frequency(struct file *file, void *priv, |
Hans Verkuil | b530a44 | 2013-03-19 04:09:26 -0300 | [diff] [blame] | 69 | const struct v4l2_frequency *f) |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 70 | { |
| 71 | struct timbradio *tr = video_drvdata(file); |
| 72 | return v4l2_subdev_call(tr->sd_tuner, tuner, s_frequency, f); |
| 73 | } |
| 74 | |
| 75 | static int timbradio_vidioc_g_frequency(struct file *file, void *priv, |
| 76 | struct v4l2_frequency *f) |
| 77 | { |
| 78 | struct timbradio *tr = video_drvdata(file); |
| 79 | return v4l2_subdev_call(tr->sd_tuner, tuner, g_frequency, f); |
| 80 | } |
| 81 | |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 82 | static const struct v4l2_ioctl_ops timbradio_ioctl_ops = { |
| 83 | .vidioc_querycap = timbradio_vidioc_querycap, |
| 84 | .vidioc_g_tuner = timbradio_vidioc_g_tuner, |
| 85 | .vidioc_s_tuner = timbradio_vidioc_s_tuner, |
| 86 | .vidioc_g_frequency = timbradio_vidioc_g_frequency, |
| 87 | .vidioc_s_frequency = timbradio_vidioc_s_frequency, |
Hans Verkuil | 46821b1 | 2013-02-03 09:33:21 -0300 | [diff] [blame] | 88 | .vidioc_log_status = v4l2_ctrl_log_status, |
| 89 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 90 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static const struct v4l2_file_operations timbradio_fops = { |
| 94 | .owner = THIS_MODULE, |
Hans Verkuil | 46821b1 | 2013-02-03 09:33:21 -0300 | [diff] [blame] | 95 | .open = v4l2_fh_open, |
| 96 | .release = v4l2_fh_release, |
| 97 | .poll = v4l2_ctrl_poll, |
Hans Verkuil | 4f68775 | 2010-11-16 18:13:06 -0300 | [diff] [blame] | 98 | .unlocked_ioctl = video_ioctl2, |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 99 | }; |
| 100 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 101 | static int timbradio_probe(struct platform_device *pdev) |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 102 | { |
Samuel Ortiz | 3271d38 | 2011-04-08 01:23:57 +0200 | [diff] [blame] | 103 | struct timb_radio_platform_data *pdata = pdev->dev.platform_data; |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 104 | struct timbradio *tr; |
| 105 | int err; |
| 106 | |
| 107 | if (!pdata) { |
| 108 | dev_err(&pdev->dev, "Platform data missing\n"); |
| 109 | err = -EINVAL; |
| 110 | goto err; |
| 111 | } |
| 112 | |
Julia Lawall | 2f20c49 | 2012-07-31 05:21:37 -0300 | [diff] [blame] | 113 | tr = devm_kzalloc(&pdev->dev, sizeof(*tr), GFP_KERNEL); |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 114 | if (!tr) { |
| 115 | err = -ENOMEM; |
| 116 | goto err; |
| 117 | } |
| 118 | |
| 119 | tr->pdata = *pdata; |
Hans Verkuil | 4f68775 | 2010-11-16 18:13:06 -0300 | [diff] [blame] | 120 | mutex_init(&tr->lock); |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 121 | |
| 122 | strlcpy(tr->video_dev.name, "Timberdale Radio", |
| 123 | sizeof(tr->video_dev.name)); |
| 124 | tr->video_dev.fops = &timbradio_fops; |
| 125 | tr->video_dev.ioctl_ops = &timbradio_ioctl_ops; |
| 126 | tr->video_dev.release = video_device_release_empty; |
| 127 | tr->video_dev.minor = -1; |
Hans Verkuil | 4f68775 | 2010-11-16 18:13:06 -0300 | [diff] [blame] | 128 | tr->video_dev.lock = &tr->lock; |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 129 | |
| 130 | strlcpy(tr->v4l2_dev.name, DRIVER_NAME, sizeof(tr->v4l2_dev.name)); |
| 131 | err = v4l2_device_register(NULL, &tr->v4l2_dev); |
| 132 | if (err) |
Julia Lawall | 2f20c49 | 2012-07-31 05:21:37 -0300 | [diff] [blame] | 133 | goto err; |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 134 | |
| 135 | tr->video_dev.v4l2_dev = &tr->v4l2_dev; |
| 136 | |
Hans Verkuil | 5bd8d2a | 2013-05-31 06:50:50 -0300 | [diff] [blame] | 137 | tr->sd_tuner = v4l2_i2c_new_subdev_board(&tr->v4l2_dev, |
| 138 | i2c_get_adapter(pdata->i2c_adapter), pdata->tuner, NULL); |
| 139 | tr->sd_dsp = v4l2_i2c_new_subdev_board(&tr->v4l2_dev, |
| 140 | i2c_get_adapter(pdata->i2c_adapter), pdata->dsp, NULL); |
| 141 | if (tr->sd_tuner == NULL || tr->sd_dsp == NULL) |
| 142 | goto err_video_req; |
| 143 | |
| 144 | tr->v4l2_dev.ctrl_handler = tr->sd_dsp->ctrl_handler; |
| 145 | |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 146 | err = video_register_device(&tr->video_dev, VFL_TYPE_RADIO, -1); |
| 147 | if (err) { |
| 148 | dev_err(&pdev->dev, "Error reg video\n"); |
| 149 | goto err_video_req; |
| 150 | } |
| 151 | |
| 152 | video_set_drvdata(&tr->video_dev, tr); |
| 153 | |
| 154 | platform_set_drvdata(pdev, tr); |
| 155 | return 0; |
| 156 | |
| 157 | err_video_req: |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 158 | v4l2_device_unregister(&tr->v4l2_dev); |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 159 | err: |
| 160 | dev_err(&pdev->dev, "Failed to register: %d\n", err); |
| 161 | |
| 162 | return err; |
| 163 | } |
| 164 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 165 | static int timbradio_remove(struct platform_device *pdev) |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 166 | { |
| 167 | struct timbradio *tr = platform_get_drvdata(pdev); |
| 168 | |
| 169 | video_unregister_device(&tr->video_dev); |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 170 | v4l2_device_unregister(&tr->v4l2_dev); |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static struct platform_driver timbradio_platform_driver = { |
| 175 | .driver = { |
| 176 | .name = DRIVER_NAME, |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 177 | }, |
| 178 | .probe = timbradio_probe, |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 179 | .remove = timbradio_remove, |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 180 | }; |
| 181 | |
Axel Lin | 1d6629b | 2012-01-10 03:21:49 -0300 | [diff] [blame] | 182 | module_platform_driver(timbradio_platform_driver); |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 183 | |
| 184 | MODULE_DESCRIPTION("Timberdale Radio driver"); |
| 185 | MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>"); |
| 186 | MODULE_LICENSE("GPL v2"); |
Mauro Carvalho Chehab | 29834c1 | 2011-06-25 10:15:42 -0300 | [diff] [blame] | 187 | MODULE_VERSION("0.0.2"); |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 188 | MODULE_ALIAS("platform:"DRIVER_NAME); |