[media] radio-timb: convert to the control framework
[firefly-linux-kernel-4.4.55.git] / drivers / media / radio / radio-timb.c
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/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>
24 #include <linux/slab.h>
25 #include <linux/i2c.h>
26 #include <linux/module.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;
37         struct mutex            lock;
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->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
48         v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
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         const 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_s_frequency(struct file *file, void *priv,
67         const struct v4l2_frequency *f)
68 {
69         struct timbradio *tr = video_drvdata(file);
70         return v4l2_subdev_call(tr->sd_tuner, tuner, s_frequency, f);
71 }
72
73 static int timbradio_vidioc_g_frequency(struct file *file, void *priv,
74         struct v4l2_frequency *f)
75 {
76         struct timbradio *tr = video_drvdata(file);
77         return v4l2_subdev_call(tr->sd_tuner, tuner, g_frequency, f);
78 }
79
80 static const struct v4l2_ioctl_ops timbradio_ioctl_ops = {
81         .vidioc_querycap        = timbradio_vidioc_querycap,
82         .vidioc_g_tuner         = timbradio_vidioc_g_tuner,
83         .vidioc_s_tuner         = timbradio_vidioc_s_tuner,
84         .vidioc_g_frequency     = timbradio_vidioc_g_frequency,
85         .vidioc_s_frequency     = timbradio_vidioc_s_frequency,
86 };
87
88 static const struct v4l2_file_operations timbradio_fops = {
89         .owner          = THIS_MODULE,
90         .unlocked_ioctl = video_ioctl2,
91 };
92
93 static int timbradio_probe(struct platform_device *pdev)
94 {
95         struct timb_radio_platform_data *pdata = pdev->dev.platform_data;
96         struct timbradio *tr;
97         int err;
98
99         if (!pdata) {
100                 dev_err(&pdev->dev, "Platform data missing\n");
101                 err = -EINVAL;
102                 goto err;
103         }
104
105         tr = devm_kzalloc(&pdev->dev, sizeof(*tr), GFP_KERNEL);
106         if (!tr) {
107                 err = -ENOMEM;
108                 goto err;
109         }
110
111         tr->pdata = *pdata;
112         mutex_init(&tr->lock);
113
114         strlcpy(tr->video_dev.name, "Timberdale Radio",
115                 sizeof(tr->video_dev.name));
116         tr->video_dev.fops = &timbradio_fops;
117         tr->video_dev.ioctl_ops = &timbradio_ioctl_ops;
118         tr->video_dev.release = video_device_release_empty;
119         tr->video_dev.minor = -1;
120         tr->video_dev.lock = &tr->lock;
121
122         strlcpy(tr->v4l2_dev.name, DRIVER_NAME, sizeof(tr->v4l2_dev.name));
123         err = v4l2_device_register(NULL, &tr->v4l2_dev);
124         if (err)
125                 goto err;
126
127         tr->video_dev.v4l2_dev = &tr->v4l2_dev;
128
129         err = video_register_device(&tr->video_dev, VFL_TYPE_RADIO, -1);
130         if (err) {
131                 dev_err(&pdev->dev, "Error reg video\n");
132                 goto err_video_req;
133         }
134
135         video_set_drvdata(&tr->video_dev, tr);
136
137         platform_set_drvdata(pdev, tr);
138         return 0;
139
140 err_video_req:
141         video_device_release_empty(&tr->video_dev);
142         v4l2_device_unregister(&tr->v4l2_dev);
143 err:
144         dev_err(&pdev->dev, "Failed to register: %d\n", err);
145
146         return err;
147 }
148
149 static int timbradio_remove(struct platform_device *pdev)
150 {
151         struct timbradio *tr = platform_get_drvdata(pdev);
152
153         video_unregister_device(&tr->video_dev);
154         video_device_release_empty(&tr->video_dev);
155
156         v4l2_device_unregister(&tr->v4l2_dev);
157
158         return 0;
159 }
160
161 static struct platform_driver timbradio_platform_driver = {
162         .driver = {
163                 .name   = DRIVER_NAME,
164                 .owner  = THIS_MODULE,
165         },
166         .probe          = timbradio_probe,
167         .remove         = timbradio_remove,
168 };
169
170 module_platform_driver(timbradio_platform_driver);
171
172 MODULE_DESCRIPTION("Timberdale Radio driver");
173 MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
174 MODULE_LICENSE("GPL v2");
175 MODULE_VERSION("0.0.2");
176 MODULE_ALIAS("platform:"DRIVER_NAME);