1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/platform_device.h>
24#include <linux/i2c.h>
25#include <linux/videodev2.h>
26#include <linux/slab.h>
27#include <media/v4l2-device.h>
28#include <media/v4l2-common.h>
29#include <media/v4l2-ioctl.h>
30#include <media/v4l2-fh.h>
31#include <media/v4l2-ctrls.h>
32#include <media/v4l2-event.h>
33#include "si4713.h"
34
35
36static int radio_nr = -1;
37module_param(radio_nr, int, 0);
38MODULE_PARM_DESC(radio_nr,
39 "Minor number for radio device (-1 ==> auto assign)");
40
41MODULE_LICENSE("GPL v2");
42MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
43MODULE_DESCRIPTION("Platform driver for Si4713 FM Radio Transmitter");
44MODULE_VERSION("0.0.1");
45MODULE_ALIAS("platform:radio-si4713");
46
47
48struct radio_si4713_device {
49 struct v4l2_device v4l2_dev;
50 struct video_device radio_dev;
51 struct mutex lock;
52};
53
54
55static const struct v4l2_file_operations radio_si4713_fops = {
56 .owner = THIS_MODULE,
57 .open = v4l2_fh_open,
58 .release = v4l2_fh_release,
59 .poll = v4l2_ctrl_poll,
60
61 .unlocked_ioctl = video_ioctl2,
62};
63
64
65
66
67static int radio_si4713_querycap(struct file *file, void *priv,
68 struct v4l2_capability *capability)
69{
70 strscpy(capability->driver, "radio-si4713", sizeof(capability->driver));
71 strscpy(capability->card, "Silicon Labs Si4713 Modulator",
72 sizeof(capability->card));
73 strscpy(capability->bus_info, "platform:radio-si4713",
74 sizeof(capability->bus_info));
75 capability->device_caps = V4L2_CAP_MODULATOR | V4L2_CAP_RDS_OUTPUT;
76 capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS;
77
78 return 0;
79}
80
81
82
83
84
85static inline struct v4l2_device *get_v4l2_dev(struct file *file)
86{
87 return &((struct radio_si4713_device *)video_drvdata(file))->v4l2_dev;
88}
89
90static int radio_si4713_g_modulator(struct file *file, void *p,
91 struct v4l2_modulator *vm)
92{
93 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
94 g_modulator, vm);
95}
96
97static int radio_si4713_s_modulator(struct file *file, void *p,
98 const struct v4l2_modulator *vm)
99{
100 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
101 s_modulator, vm);
102}
103
104static int radio_si4713_g_frequency(struct file *file, void *p,
105 struct v4l2_frequency *vf)
106{
107 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
108 g_frequency, vf);
109}
110
111static int radio_si4713_s_frequency(struct file *file, void *p,
112 const struct v4l2_frequency *vf)
113{
114 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
115 s_frequency, vf);
116}
117
118static long radio_si4713_default(struct file *file, void *p,
119 bool valid_prio, unsigned int cmd, void *arg)
120{
121 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
122 ioctl, cmd, arg);
123}
124
125static struct v4l2_ioctl_ops radio_si4713_ioctl_ops = {
126 .vidioc_querycap = radio_si4713_querycap,
127 .vidioc_g_modulator = radio_si4713_g_modulator,
128 .vidioc_s_modulator = radio_si4713_s_modulator,
129 .vidioc_g_frequency = radio_si4713_g_frequency,
130 .vidioc_s_frequency = radio_si4713_s_frequency,
131 .vidioc_log_status = v4l2_ctrl_log_status,
132 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
133 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
134 .vidioc_default = radio_si4713_default,
135};
136
137
138static const struct video_device radio_si4713_vdev_template = {
139 .fops = &radio_si4713_fops,
140 .name = "radio-si4713",
141 .release = video_device_release_empty,
142 .ioctl_ops = &radio_si4713_ioctl_ops,
143 .vfl_dir = VFL_DIR_TX,
144};
145
146
147
148static int radio_si4713_pdriver_probe(struct platform_device *pdev)
149{
150 struct radio_si4713_platform_data *pdata = pdev->dev.platform_data;
151 struct radio_si4713_device *rsdev;
152 struct v4l2_subdev *sd;
153 int rval = 0;
154
155 if (!pdata) {
156 dev_err(&pdev->dev, "Cannot proceed without platform data.\n");
157 rval = -EINVAL;
158 goto exit;
159 }
160
161 rsdev = devm_kzalloc(&pdev->dev, sizeof(*rsdev), GFP_KERNEL);
162 if (!rsdev) {
163 dev_err(&pdev->dev, "Failed to alloc video device.\n");
164 rval = -ENOMEM;
165 goto exit;
166 }
167 mutex_init(&rsdev->lock);
168
169 rval = v4l2_device_register(&pdev->dev, &rsdev->v4l2_dev);
170 if (rval) {
171 dev_err(&pdev->dev, "Failed to register v4l2 device.\n");
172 goto exit;
173 }
174
175 sd = i2c_get_clientdata(pdata->subdev);
176 rval = v4l2_device_register_subdev(&rsdev->v4l2_dev, sd);
177 if (rval) {
178 dev_err(&pdev->dev, "Cannot get v4l2 subdevice\n");
179 goto unregister_v4l2_dev;
180 }
181
182 rsdev->radio_dev = radio_si4713_vdev_template;
183 rsdev->radio_dev.v4l2_dev = &rsdev->v4l2_dev;
184 rsdev->radio_dev.ctrl_handler = sd->ctrl_handler;
185
186 rsdev->radio_dev.lock = &rsdev->lock;
187 video_set_drvdata(&rsdev->radio_dev, rsdev);
188 if (video_register_device(&rsdev->radio_dev, VFL_TYPE_RADIO, radio_nr)) {
189 dev_err(&pdev->dev, "Could not register video device.\n");
190 rval = -EIO;
191 goto unregister_v4l2_dev;
192 }
193 dev_info(&pdev->dev, "New device successfully probed\n");
194
195 goto exit;
196
197unregister_v4l2_dev:
198 v4l2_device_unregister(&rsdev->v4l2_dev);
199exit:
200 return rval;
201}
202
203
204static int radio_si4713_pdriver_remove(struct platform_device *pdev)
205{
206 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
207 struct radio_si4713_device *rsdev;
208
209 rsdev = container_of(v4l2_dev, struct radio_si4713_device, v4l2_dev);
210 video_unregister_device(&rsdev->radio_dev);
211 v4l2_device_unregister(&rsdev->v4l2_dev);
212
213 return 0;
214}
215
216static struct platform_driver radio_si4713_pdriver = {
217 .driver = {
218 .name = "radio-si4713",
219 },
220 .probe = radio_si4713_pdriver_probe,
221 .remove = radio_si4713_pdriver_remove,
222};
223
224module_platform_driver(radio_si4713_pdriver);
225