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 | |
| 19 | #include <linux/version.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <media/v4l2-ioctl.h> |
| 22 | #include <media/v4l2-device.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/interrupt.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 26 | #include <linux/i2c.h> |
| 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); |
| 47 | v->version = KERNEL_VERSION(0, 0, 1); |
| 48 | v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int timbradio_vidioc_g_tuner(struct file *file, void *priv, |
| 53 | struct v4l2_tuner *v) |
| 54 | { |
| 55 | struct timbradio *tr = video_drvdata(file); |
| 56 | return v4l2_subdev_call(tr->sd_tuner, tuner, g_tuner, v); |
| 57 | } |
| 58 | |
| 59 | static int timbradio_vidioc_s_tuner(struct file *file, void *priv, |
| 60 | struct v4l2_tuner *v) |
| 61 | { |
| 62 | struct timbradio *tr = video_drvdata(file); |
| 63 | return v4l2_subdev_call(tr->sd_tuner, tuner, s_tuner, v); |
| 64 | } |
| 65 | |
| 66 | static int timbradio_vidioc_g_input(struct file *filp, void *priv, |
| 67 | unsigned int *i) |
| 68 | { |
| 69 | *i = 0; |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int timbradio_vidioc_s_input(struct file *filp, void *priv, |
| 74 | unsigned int i) |
| 75 | { |
| 76 | return i ? -EINVAL : 0; |
| 77 | } |
| 78 | |
| 79 | static int timbradio_vidioc_g_audio(struct file *file, void *priv, |
| 80 | struct v4l2_audio *a) |
| 81 | { |
| 82 | a->index = 0; |
| 83 | strlcpy(a->name, "Radio", sizeof(a->name)); |
| 84 | a->capability = V4L2_AUDCAP_STEREO; |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int timbradio_vidioc_s_audio(struct file *file, void *priv, |
| 89 | struct v4l2_audio *a) |
| 90 | { |
| 91 | return a->index ? -EINVAL : 0; |
| 92 | } |
| 93 | |
| 94 | static int timbradio_vidioc_s_frequency(struct file *file, void *priv, |
| 95 | struct v4l2_frequency *f) |
| 96 | { |
| 97 | struct timbradio *tr = video_drvdata(file); |
| 98 | return v4l2_subdev_call(tr->sd_tuner, tuner, s_frequency, f); |
| 99 | } |
| 100 | |
| 101 | static int timbradio_vidioc_g_frequency(struct file *file, void *priv, |
| 102 | struct v4l2_frequency *f) |
| 103 | { |
| 104 | struct timbradio *tr = video_drvdata(file); |
| 105 | return v4l2_subdev_call(tr->sd_tuner, tuner, g_frequency, f); |
| 106 | } |
| 107 | |
| 108 | static int timbradio_vidioc_queryctrl(struct file *file, void *priv, |
| 109 | struct v4l2_queryctrl *qc) |
| 110 | { |
| 111 | struct timbradio *tr = video_drvdata(file); |
| 112 | return v4l2_subdev_call(tr->sd_dsp, core, queryctrl, qc); |
| 113 | } |
| 114 | |
| 115 | static int timbradio_vidioc_g_ctrl(struct file *file, void *priv, |
| 116 | struct v4l2_control *ctrl) |
| 117 | { |
| 118 | struct timbradio *tr = video_drvdata(file); |
| 119 | return v4l2_subdev_call(tr->sd_dsp, core, g_ctrl, ctrl); |
| 120 | } |
| 121 | |
| 122 | static int timbradio_vidioc_s_ctrl(struct file *file, void *priv, |
| 123 | struct v4l2_control *ctrl) |
| 124 | { |
| 125 | struct timbradio *tr = video_drvdata(file); |
| 126 | return v4l2_subdev_call(tr->sd_dsp, core, s_ctrl, ctrl); |
| 127 | } |
| 128 | |
| 129 | static const struct v4l2_ioctl_ops timbradio_ioctl_ops = { |
| 130 | .vidioc_querycap = timbradio_vidioc_querycap, |
| 131 | .vidioc_g_tuner = timbradio_vidioc_g_tuner, |
| 132 | .vidioc_s_tuner = timbradio_vidioc_s_tuner, |
| 133 | .vidioc_g_frequency = timbradio_vidioc_g_frequency, |
| 134 | .vidioc_s_frequency = timbradio_vidioc_s_frequency, |
| 135 | .vidioc_g_input = timbradio_vidioc_g_input, |
| 136 | .vidioc_s_input = timbradio_vidioc_s_input, |
| 137 | .vidioc_g_audio = timbradio_vidioc_g_audio, |
| 138 | .vidioc_s_audio = timbradio_vidioc_s_audio, |
| 139 | .vidioc_queryctrl = timbradio_vidioc_queryctrl, |
| 140 | .vidioc_g_ctrl = timbradio_vidioc_g_ctrl, |
| 141 | .vidioc_s_ctrl = timbradio_vidioc_s_ctrl |
| 142 | }; |
| 143 | |
| 144 | static const struct v4l2_file_operations timbradio_fops = { |
| 145 | .owner = THIS_MODULE, |
Hans Verkuil | 4f68775 | 2010-11-16 18:13:06 -0300 | [diff] [blame] | 146 | .unlocked_ioctl = video_ioctl2, |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | static int __devinit timbradio_probe(struct platform_device *pdev) |
| 150 | { |
| 151 | struct timb_radio_platform_data *pdata = pdev->dev.platform_data; |
| 152 | struct timbradio *tr; |
| 153 | int err; |
| 154 | |
| 155 | if (!pdata) { |
| 156 | dev_err(&pdev->dev, "Platform data missing\n"); |
| 157 | err = -EINVAL; |
| 158 | goto err; |
| 159 | } |
| 160 | |
| 161 | tr = kzalloc(sizeof(*tr), GFP_KERNEL); |
| 162 | if (!tr) { |
| 163 | err = -ENOMEM; |
| 164 | goto err; |
| 165 | } |
| 166 | |
| 167 | tr->pdata = *pdata; |
Hans Verkuil | 4f68775 | 2010-11-16 18:13:06 -0300 | [diff] [blame] | 168 | mutex_init(&tr->lock); |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 169 | |
| 170 | strlcpy(tr->video_dev.name, "Timberdale Radio", |
| 171 | sizeof(tr->video_dev.name)); |
| 172 | tr->video_dev.fops = &timbradio_fops; |
| 173 | tr->video_dev.ioctl_ops = &timbradio_ioctl_ops; |
| 174 | tr->video_dev.release = video_device_release_empty; |
| 175 | tr->video_dev.minor = -1; |
Hans Verkuil | 4f68775 | 2010-11-16 18:13:06 -0300 | [diff] [blame] | 176 | tr->video_dev.lock = &tr->lock; |
Richard Röjfors | d44d1f3 | 2010-02-03 12:59:39 -0300 | [diff] [blame] | 177 | |
| 178 | strlcpy(tr->v4l2_dev.name, DRIVER_NAME, sizeof(tr->v4l2_dev.name)); |
| 179 | err = v4l2_device_register(NULL, &tr->v4l2_dev); |
| 180 | if (err) |
| 181 | goto err_v4l2_dev; |
| 182 | |
| 183 | tr->video_dev.v4l2_dev = &tr->v4l2_dev; |
| 184 | |
| 185 | err = video_register_device(&tr->video_dev, VFL_TYPE_RADIO, -1); |
| 186 | if (err) { |
| 187 | dev_err(&pdev->dev, "Error reg video\n"); |
| 188 | goto err_video_req; |
| 189 | } |
| 190 | |
| 191 | video_set_drvdata(&tr->video_dev, tr); |
| 192 | |
| 193 | platform_set_drvdata(pdev, tr); |
| 194 | return 0; |
| 195 | |
| 196 | err_video_req: |
| 197 | video_device_release_empty(&tr->video_dev); |
| 198 | v4l2_device_unregister(&tr->v4l2_dev); |
| 199 | err_v4l2_dev: |
| 200 | kfree(tr); |
| 201 | err: |
| 202 | dev_err(&pdev->dev, "Failed to register: %d\n", err); |
| 203 | |
| 204 | return err; |
| 205 | } |
| 206 | |
| 207 | static int __devexit timbradio_remove(struct platform_device *pdev) |
| 208 | { |
| 209 | struct timbradio *tr = platform_get_drvdata(pdev); |
| 210 | |
| 211 | video_unregister_device(&tr->video_dev); |
| 212 | video_device_release_empty(&tr->video_dev); |
| 213 | |
| 214 | v4l2_device_unregister(&tr->v4l2_dev); |
| 215 | |
| 216 | kfree(tr); |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static struct platform_driver timbradio_platform_driver = { |
| 222 | .driver = { |
| 223 | .name = DRIVER_NAME, |
| 224 | .owner = THIS_MODULE, |
| 225 | }, |
| 226 | .probe = timbradio_probe, |
| 227 | .remove = timbradio_remove, |
| 228 | }; |
| 229 | |
| 230 | /*--------------------------------------------------------------------------*/ |
| 231 | |
| 232 | static int __init timbradio_init(void) |
| 233 | { |
| 234 | return platform_driver_register(&timbradio_platform_driver); |
| 235 | } |
| 236 | |
| 237 | static void __exit timbradio_exit(void) |
| 238 | { |
| 239 | platform_driver_unregister(&timbradio_platform_driver); |
| 240 | } |
| 241 | |
| 242 | module_init(timbradio_init); |
| 243 | module_exit(timbradio_exit); |
| 244 | |
| 245 | MODULE_DESCRIPTION("Timberdale Radio driver"); |
| 246 | MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>"); |
| 247 | MODULE_LICENSE("GPL v2"); |
| 248 | MODULE_ALIAS("platform:"DRIVER_NAME); |