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