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> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/interrupt.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 25 | #include <linux/i2c.h> |
Paul Gortmaker | 7a707b8 | 2011-07-03 14:03:12 -0400 | [diff] [blame] | 26 | #include <linux/module.h> |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 27 | #include <media/timb_radio.h> |
| 28 | |
| 29 | #define DRIVER_NAME "timb-radio" |
| 30 | |
| 31 | struct timbradio { |
| 32 | struct timb_radio_platform_data pdata; |
| 33 | struct v4l2_subdev *sd_tuner; |
| 34 | struct v4l2_subdev *sd_dsp; |
| 35 | struct video_device video_dev; |
| 36 | struct v4l2_device v4l2_dev; |
Hans Verkuil | 4f68775 | 2010-11-16 18:13:06 -0300 | [diff] [blame] | 37 | struct mutex lock; |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | |
| 41 | static int timbradio_vidioc_querycap(struct file *file, void *priv, |
| 42 | struct v4l2_capability *v) |
| 43 | { |
| 44 | strlcpy(v->driver, DRIVER_NAME, sizeof(v->driver)); |
| 45 | strlcpy(v->card, "Timberdale Radio", sizeof(v->card)); |
| 46 | snprintf(v->bus_info, sizeof(v->bus_info), "platform:"DRIVER_NAME); |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 47 | v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int timbradio_vidioc_g_tuner(struct file *file, void *priv, |
| 52 | struct v4l2_tuner *v) |
| 53 | { |
| 54 | struct timbradio *tr = video_drvdata(file); |
| 55 | return v4l2_subdev_call(tr->sd_tuner, tuner, g_tuner, v); |
| 56 | } |
| 57 | |
| 58 | static int timbradio_vidioc_s_tuner(struct file *file, void *priv, |
| 59 | struct v4l2_tuner *v) |
| 60 | { |
| 61 | struct timbradio *tr = video_drvdata(file); |
| 62 | return v4l2_subdev_call(tr->sd_tuner, tuner, s_tuner, v); |
| 63 | } |
| 64 | |
| 65 | static int timbradio_vidioc_g_input(struct file *filp, void *priv, |
| 66 | unsigned int *i) |
| 67 | { |
| 68 | *i = 0; |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static int timbradio_vidioc_s_input(struct file *filp, void *priv, |
| 73 | unsigned int i) |
| 74 | { |
| 75 | return i ? -EINVAL : 0; |
| 76 | } |
| 77 | |
| 78 | static int timbradio_vidioc_g_audio(struct file *file, void *priv, |
| 79 | struct v4l2_audio *a) |
| 80 | { |
| 81 | a->index = 0; |
| 82 | strlcpy(a->name, "Radio", sizeof(a->name)); |
| 83 | a->capability = V4L2_AUDCAP_STEREO; |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int timbradio_vidioc_s_audio(struct file *file, void *priv, |
Hans Verkuil | 0e8025b9 | 2012-09-04 11:59:31 -0300 | [diff] [blame] | 88 | const struct v4l2_audio *a) |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 89 | { |
| 90 | return a->index ? -EINVAL : 0; |
| 91 | } |
| 92 | |
| 93 | static int timbradio_vidioc_s_frequency(struct file *file, void *priv, |
| 94 | struct v4l2_frequency *f) |
| 95 | { |
| 96 | struct timbradio *tr = video_drvdata(file); |
| 97 | return v4l2_subdev_call(tr->sd_tuner, tuner, s_frequency, f); |
| 98 | } |
| 99 | |
| 100 | static int timbradio_vidioc_g_frequency(struct file *file, void *priv, |
| 101 | struct v4l2_frequency *f) |
| 102 | { |
| 103 | struct timbradio *tr = video_drvdata(file); |
| 104 | return v4l2_subdev_call(tr->sd_tuner, tuner, g_frequency, f); |
| 105 | } |
| 106 | |
| 107 | static int timbradio_vidioc_queryctrl(struct file *file, void *priv, |
| 108 | struct v4l2_queryctrl *qc) |
| 109 | { |
| 110 | struct timbradio *tr = video_drvdata(file); |
| 111 | return v4l2_subdev_call(tr->sd_dsp, core, queryctrl, qc); |
| 112 | } |
| 113 | |
| 114 | static int timbradio_vidioc_g_ctrl(struct file *file, void *priv, |
| 115 | struct v4l2_control *ctrl) |
| 116 | { |
| 117 | struct timbradio *tr = video_drvdata(file); |
| 118 | return v4l2_subdev_call(tr->sd_dsp, core, g_ctrl, ctrl); |
| 119 | } |
| 120 | |
| 121 | static int timbradio_vidioc_s_ctrl(struct file *file, void *priv, |
| 122 | struct v4l2_control *ctrl) |
| 123 | { |
| 124 | struct timbradio *tr = video_drvdata(file); |
| 125 | return v4l2_subdev_call(tr->sd_dsp, core, s_ctrl, ctrl); |
| 126 | } |
| 127 | |
| 128 | static const struct v4l2_ioctl_ops timbradio_ioctl_ops = { |
| 129 | .vidioc_querycap = timbradio_vidioc_querycap, |
| 130 | .vidioc_g_tuner = timbradio_vidioc_g_tuner, |
| 131 | .vidioc_s_tuner = timbradio_vidioc_s_tuner, |
| 132 | .vidioc_g_frequency = timbradio_vidioc_g_frequency, |
| 133 | .vidioc_s_frequency = timbradio_vidioc_s_frequency, |
| 134 | .vidioc_g_input = timbradio_vidioc_g_input, |
| 135 | .vidioc_s_input = timbradio_vidioc_s_input, |
| 136 | .vidioc_g_audio = timbradio_vidioc_g_audio, |
| 137 | .vidioc_s_audio = timbradio_vidioc_s_audio, |
| 138 | .vidioc_queryctrl = timbradio_vidioc_queryctrl, |
| 139 | .vidioc_g_ctrl = timbradio_vidioc_g_ctrl, |
| 140 | .vidioc_s_ctrl = timbradio_vidioc_s_ctrl |
| 141 | }; |
| 142 | |
| 143 | static const struct v4l2_file_operations timbradio_fops = { |
| 144 | .owner = THIS_MODULE, |
Hans Verkuil | 4f68775 | 2010-11-16 18:13:06 -0300 | [diff] [blame] | 145 | .unlocked_ioctl = video_ioctl2, |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | static int __devinit timbradio_probe(struct platform_device *pdev) |
| 149 | { |
Samuel Ortiz | 3271d38 | 2011-04-08 01:23:57 +0200 | [diff] [blame] | 150 | struct timb_radio_platform_data *pdata = pdev->dev.platform_data; |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 151 | struct timbradio *tr; |
| 152 | int err; |
| 153 | |
| 154 | if (!pdata) { |
| 155 | dev_err(&pdev->dev, "Platform data missing\n"); |
| 156 | err = -EINVAL; |
| 157 | goto err; |
| 158 | } |
| 159 | |
Julia Lawall | 2f20c49 | 2012-07-31 05:21:37 -0300 | [diff] [blame] | 160 | tr = devm_kzalloc(&pdev->dev, sizeof(*tr), GFP_KERNEL); |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 161 | if (!tr) { |
| 162 | err = -ENOMEM; |
| 163 | goto err; |
| 164 | } |
| 165 | |
| 166 | tr->pdata = *pdata; |
Hans Verkuil | 4f68775 | 2010-11-16 18:13:06 -0300 | [diff] [blame] | 167 | mutex_init(&tr->lock); |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 168 | |
| 169 | strlcpy(tr->video_dev.name, "Timberdale Radio", |
| 170 | sizeof(tr->video_dev.name)); |
| 171 | tr->video_dev.fops = &timbradio_fops; |
| 172 | tr->video_dev.ioctl_ops = &timbradio_ioctl_ops; |
| 173 | tr->video_dev.release = video_device_release_empty; |
| 174 | tr->video_dev.minor = -1; |
Hans Verkuil | 4f68775 | 2010-11-16 18:13:06 -0300 | [diff] [blame] | 175 | tr->video_dev.lock = &tr->lock; |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 176 | |
| 177 | strlcpy(tr->v4l2_dev.name, DRIVER_NAME, sizeof(tr->v4l2_dev.name)); |
| 178 | err = v4l2_device_register(NULL, &tr->v4l2_dev); |
| 179 | if (err) |
Julia Lawall | 2f20c49 | 2012-07-31 05:21:37 -0300 | [diff] [blame] | 180 | goto err; |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 181 | |
| 182 | tr->video_dev.v4l2_dev = &tr->v4l2_dev; |
| 183 | |
| 184 | err = video_register_device(&tr->video_dev, VFL_TYPE_RADIO, -1); |
| 185 | if (err) { |
| 186 | dev_err(&pdev->dev, "Error reg video\n"); |
| 187 | goto err_video_req; |
| 188 | } |
| 189 | |
| 190 | video_set_drvdata(&tr->video_dev, tr); |
| 191 | |
| 192 | platform_set_drvdata(pdev, tr); |
| 193 | return 0; |
| 194 | |
| 195 | err_video_req: |
| 196 | video_device_release_empty(&tr->video_dev); |
| 197 | v4l2_device_unregister(&tr->v4l2_dev); |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 198 | err: |
| 199 | dev_err(&pdev->dev, "Failed to register: %d\n", err); |
| 200 | |
| 201 | return err; |
| 202 | } |
| 203 | |
| 204 | static int __devexit timbradio_remove(struct platform_device *pdev) |
| 205 | { |
| 206 | struct timbradio *tr = platform_get_drvdata(pdev); |
| 207 | |
| 208 | video_unregister_device(&tr->video_dev); |
| 209 | video_device_release_empty(&tr->video_dev); |
| 210 | |
| 211 | v4l2_device_unregister(&tr->v4l2_dev); |
| 212 | |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static struct platform_driver timbradio_platform_driver = { |
| 217 | .driver = { |
| 218 | .name = DRIVER_NAME, |
| 219 | .owner = THIS_MODULE, |
| 220 | }, |
| 221 | .probe = timbradio_probe, |
Arnd Bergmann | cd624c7 | 2012-05-03 18:22:22 -0300 | [diff] [blame] | 222 | .remove = __devexit_p(timbradio_remove), |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 223 | }; |
| 224 | |
Axel Lin | 1d6629b | 2012-01-10 03:21:49 -0300 | [diff] [blame] | 225 | module_platform_driver(timbradio_platform_driver); |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 226 | |
| 227 | MODULE_DESCRIPTION("Timberdale Radio driver"); |
| 228 | MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>"); |
| 229 | MODULE_LICENSE("GPL v2"); |
Mauro Carvalho Chehab | 29834c1 | 2011-06-25 10:15:42 -0300 | [diff] [blame] | 230 | MODULE_VERSION("0.0.2"); |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 231 | MODULE_ALIAS("platform:"DRIVER_NAME); |