Commit | Line | Data |
---|---|---|
1fd2121c EV |
1 | /* |
2 | * drivers/media/radio/radio-si4713.c | |
3 | * | |
4 | * Platform Driver for Silicon Labs Si4713 FM Radio Transmitter: | |
5 | * | |
6 | * Copyright (c) 2008 Instituto Nokia de Tecnologia - INdT | |
7 | * Contact: Eduardo Valentin <eduardo.valentin@nokia.com> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 | */ | |
23 | ||
24 | #include <linux/kernel.h> | |
25 | #include <linux/module.h> | |
26 | #include <linux/init.h> | |
1fd2121c EV |
27 | #include <linux/platform_device.h> |
28 | #include <linux/i2c.h> | |
29 | #include <linux/videodev2.h> | |
5a0e3ad6 | 30 | #include <linux/slab.h> |
1fd2121c EV |
31 | #include <media/v4l2-device.h> |
32 | #include <media/v4l2-common.h> | |
33 | #include <media/v4l2-ioctl.h> | |
34 | #include <media/radio-si4713.h> | |
35 | ||
36 | /* module parameters */ | |
37 | static int radio_nr = -1; /* radio device minor (-1 ==> auto assign) */ | |
38 | module_param(radio_nr, int, 0); | |
39 | MODULE_PARM_DESC(radio_nr, | |
40 | "Minor number for radio device (-1 ==> auto assign)"); | |
41 | ||
42 | MODULE_LICENSE("GPL"); | |
43 | MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>"); | |
44 | MODULE_DESCRIPTION("Platform driver for Si4713 FM Radio Transmitter"); | |
45 | MODULE_VERSION("0.0.1"); | |
46 | ||
47 | /* Driver state struct */ | |
48 | struct radio_si4713_device { | |
49 | struct v4l2_device v4l2_dev; | |
50 | struct video_device *radio_dev; | |
51 | }; | |
52 | ||
53 | /* radio_si4713_fops - file operations interface */ | |
54 | static const struct v4l2_file_operations radio_si4713_fops = { | |
55 | .owner = THIS_MODULE, | |
725ea8cf HV |
56 | /* Note: locking is done at the subdev level in the i2c driver. */ |
57 | .unlocked_ioctl = video_ioctl2, | |
1fd2121c EV |
58 | }; |
59 | ||
60 | /* Video4Linux Interface */ | |
61 | static int radio_si4713_fill_audout(struct v4l2_audioout *vao) | |
62 | { | |
63 | /* TODO: check presence of audio output */ | |
64 | strlcpy(vao->name, "FM Modulator Audio Out", 32); | |
65 | ||
66 | return 0; | |
67 | } | |
68 | ||
69 | static int radio_si4713_enumaudout(struct file *file, void *priv, | |
70 | struct v4l2_audioout *vao) | |
71 | { | |
72 | return radio_si4713_fill_audout(vao); | |
73 | } | |
74 | ||
75 | static int radio_si4713_g_audout(struct file *file, void *priv, | |
76 | struct v4l2_audioout *vao) | |
77 | { | |
78 | int rval = radio_si4713_fill_audout(vao); | |
79 | ||
80 | vao->index = 0; | |
81 | ||
82 | return rval; | |
83 | } | |
84 | ||
85 | static int radio_si4713_s_audout(struct file *file, void *priv, | |
86 | struct v4l2_audioout *vao) | |
87 | { | |
88 | return vao->index ? -EINVAL : 0; | |
89 | } | |
90 | ||
91 | /* radio_si4713_querycap - query device capabilities */ | |
92 | static int radio_si4713_querycap(struct file *file, void *priv, | |
93 | struct v4l2_capability *capability) | |
94 | { | |
95 | struct radio_si4713_device *rsdev; | |
96 | ||
97 | rsdev = video_get_drvdata(video_devdata(file)); | |
98 | ||
99 | strlcpy(capability->driver, "radio-si4713", sizeof(capability->driver)); | |
100 | strlcpy(capability->card, "Silicon Labs Si4713 Modulator", | |
101 | sizeof(capability->card)); | |
102 | capability->capabilities = V4L2_CAP_MODULATOR | V4L2_CAP_RDS_OUTPUT; | |
103 | ||
104 | return 0; | |
105 | } | |
106 | ||
107 | /* radio_si4713_queryctrl - enumerate control items */ | |
108 | static int radio_si4713_queryctrl(struct file *file, void *priv, | |
109 | struct v4l2_queryctrl *qc) | |
110 | { | |
111 | /* Must be sorted from low to high control ID! */ | |
112 | static const u32 user_ctrls[] = { | |
113 | V4L2_CID_USER_CLASS, | |
114 | V4L2_CID_AUDIO_MUTE, | |
115 | 0 | |
116 | }; | |
117 | ||
118 | /* Must be sorted from low to high control ID! */ | |
119 | static const u32 fmtx_ctrls[] = { | |
120 | V4L2_CID_FM_TX_CLASS, | |
121 | V4L2_CID_RDS_TX_DEVIATION, | |
122 | V4L2_CID_RDS_TX_PI, | |
123 | V4L2_CID_RDS_TX_PTY, | |
124 | V4L2_CID_RDS_TX_PS_NAME, | |
125 | V4L2_CID_RDS_TX_RADIO_TEXT, | |
126 | V4L2_CID_AUDIO_LIMITER_ENABLED, | |
127 | V4L2_CID_AUDIO_LIMITER_RELEASE_TIME, | |
128 | V4L2_CID_AUDIO_LIMITER_DEVIATION, | |
129 | V4L2_CID_AUDIO_COMPRESSION_ENABLED, | |
130 | V4L2_CID_AUDIO_COMPRESSION_GAIN, | |
131 | V4L2_CID_AUDIO_COMPRESSION_THRESHOLD, | |
132 | V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME, | |
133 | V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME, | |
134 | V4L2_CID_PILOT_TONE_ENABLED, | |
135 | V4L2_CID_PILOT_TONE_DEVIATION, | |
136 | V4L2_CID_PILOT_TONE_FREQUENCY, | |
137 | V4L2_CID_TUNE_PREEMPHASIS, | |
138 | V4L2_CID_TUNE_POWER_LEVEL, | |
139 | V4L2_CID_TUNE_ANTENNA_CAPACITOR, | |
140 | 0 | |
141 | }; | |
142 | static const u32 *ctrl_classes[] = { | |
143 | user_ctrls, | |
144 | fmtx_ctrls, | |
145 | NULL | |
146 | }; | |
147 | struct radio_si4713_device *rsdev; | |
148 | ||
149 | rsdev = video_get_drvdata(video_devdata(file)); | |
150 | ||
151 | qc->id = v4l2_ctrl_next(ctrl_classes, qc->id); | |
152 | if (qc->id == 0) | |
153 | return -EINVAL; | |
154 | ||
155 | if (qc->id == V4L2_CID_USER_CLASS || qc->id == V4L2_CID_FM_TX_CLASS) | |
156 | return v4l2_ctrl_query_fill(qc, 0, 0, 0, 0); | |
157 | ||
158 | return v4l2_device_call_until_err(&rsdev->v4l2_dev, 0, core, | |
159 | queryctrl, qc); | |
160 | } | |
161 | ||
162 | /* | |
163 | * v4l2 ioctl call backs. | |
164 | * we are just a wrapper for v4l2_sub_devs. | |
165 | */ | |
166 | static inline struct v4l2_device *get_v4l2_dev(struct file *file) | |
167 | { | |
168 | return &((struct radio_si4713_device *)video_drvdata(file))->v4l2_dev; | |
169 | } | |
170 | ||
171 | static int radio_si4713_g_ext_ctrls(struct file *file, void *p, | |
172 | struct v4l2_ext_controls *vecs) | |
173 | { | |
174 | return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core, | |
175 | g_ext_ctrls, vecs); | |
176 | } | |
177 | ||
178 | static int radio_si4713_s_ext_ctrls(struct file *file, void *p, | |
179 | struct v4l2_ext_controls *vecs) | |
180 | { | |
181 | return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core, | |
182 | s_ext_ctrls, vecs); | |
183 | } | |
184 | ||
185 | static int radio_si4713_g_ctrl(struct file *file, void *p, | |
186 | struct v4l2_control *vc) | |
187 | { | |
188 | return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core, | |
189 | g_ctrl, vc); | |
190 | } | |
191 | ||
192 | static int radio_si4713_s_ctrl(struct file *file, void *p, | |
193 | struct v4l2_control *vc) | |
194 | { | |
195 | return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core, | |
196 | s_ctrl, vc); | |
197 | } | |
198 | ||
199 | static int radio_si4713_g_modulator(struct file *file, void *p, | |
200 | struct v4l2_modulator *vm) | |
201 | { | |
202 | return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner, | |
203 | g_modulator, vm); | |
204 | } | |
205 | ||
206 | static int radio_si4713_s_modulator(struct file *file, void *p, | |
207 | struct v4l2_modulator *vm) | |
208 | { | |
209 | return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner, | |
210 | s_modulator, vm); | |
211 | } | |
212 | ||
213 | static int radio_si4713_g_frequency(struct file *file, void *p, | |
214 | struct v4l2_frequency *vf) | |
215 | { | |
216 | return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner, | |
217 | g_frequency, vf); | |
218 | } | |
219 | ||
220 | static int radio_si4713_s_frequency(struct file *file, void *p, | |
221 | struct v4l2_frequency *vf) | |
222 | { | |
223 | return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner, | |
224 | s_frequency, vf); | |
225 | } | |
226 | ||
227 | static long radio_si4713_default(struct file *file, void *p, int cmd, void *arg) | |
228 | { | |
229 | return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core, | |
230 | ioctl, cmd, arg); | |
231 | } | |
232 | ||
233 | static struct v4l2_ioctl_ops radio_si4713_ioctl_ops = { | |
234 | .vidioc_enumaudout = radio_si4713_enumaudout, | |
235 | .vidioc_g_audout = radio_si4713_g_audout, | |
236 | .vidioc_s_audout = radio_si4713_s_audout, | |
237 | .vidioc_querycap = radio_si4713_querycap, | |
238 | .vidioc_queryctrl = radio_si4713_queryctrl, | |
239 | .vidioc_g_ext_ctrls = radio_si4713_g_ext_ctrls, | |
240 | .vidioc_s_ext_ctrls = radio_si4713_s_ext_ctrls, | |
241 | .vidioc_g_ctrl = radio_si4713_g_ctrl, | |
242 | .vidioc_s_ctrl = radio_si4713_s_ctrl, | |
243 | .vidioc_g_modulator = radio_si4713_g_modulator, | |
244 | .vidioc_s_modulator = radio_si4713_s_modulator, | |
245 | .vidioc_g_frequency = radio_si4713_g_frequency, | |
246 | .vidioc_s_frequency = radio_si4713_s_frequency, | |
247 | .vidioc_default = radio_si4713_default, | |
248 | }; | |
249 | ||
250 | /* radio_si4713_vdev_template - video device interface */ | |
251 | static struct video_device radio_si4713_vdev_template = { | |
252 | .fops = &radio_si4713_fops, | |
253 | .name = "radio-si4713", | |
254 | .release = video_device_release, | |
255 | .ioctl_ops = &radio_si4713_ioctl_ops, | |
256 | }; | |
257 | ||
258 | /* Platform driver interface */ | |
259 | /* radio_si4713_pdriver_probe - probe for the device */ | |
260 | static int radio_si4713_pdriver_probe(struct platform_device *pdev) | |
261 | { | |
262 | struct radio_si4713_platform_data *pdata = pdev->dev.platform_data; | |
263 | struct radio_si4713_device *rsdev; | |
264 | struct i2c_adapter *adapter; | |
265 | struct v4l2_subdev *sd; | |
266 | int rval = 0; | |
267 | ||
268 | if (!pdata) { | |
269 | dev_err(&pdev->dev, "Cannot proceed without platform data.\n"); | |
270 | rval = -EINVAL; | |
271 | goto exit; | |
272 | } | |
273 | ||
274 | rsdev = kzalloc(sizeof *rsdev, GFP_KERNEL); | |
275 | if (!rsdev) { | |
276 | dev_err(&pdev->dev, "Failed to alloc video device.\n"); | |
277 | rval = -ENOMEM; | |
278 | goto exit; | |
279 | } | |
280 | ||
281 | rval = v4l2_device_register(&pdev->dev, &rsdev->v4l2_dev); | |
282 | if (rval) { | |
283 | dev_err(&pdev->dev, "Failed to register v4l2 device.\n"); | |
284 | goto free_rsdev; | |
285 | } | |
286 | ||
287 | adapter = i2c_get_adapter(pdata->i2c_bus); | |
288 | if (!adapter) { | |
289 | dev_err(&pdev->dev, "Cannot get i2c adapter %d\n", | |
290 | pdata->i2c_bus); | |
291 | rval = -ENODEV; | |
292 | goto unregister_v4l2_dev; | |
293 | } | |
294 | ||
9a1f8b34 | 295 | sd = v4l2_i2c_new_subdev_board(&rsdev->v4l2_dev, adapter, |
1fd2121c EV |
296 | pdata->subdev_board_info, NULL); |
297 | if (!sd) { | |
298 | dev_err(&pdev->dev, "Cannot get v4l2 subdevice\n"); | |
299 | rval = -ENODEV; | |
85c55efb | 300 | goto put_adapter; |
1fd2121c EV |
301 | } |
302 | ||
303 | rsdev->radio_dev = video_device_alloc(); | |
304 | if (!rsdev->radio_dev) { | |
305 | dev_err(&pdev->dev, "Failed to alloc video device.\n"); | |
306 | rval = -ENOMEM; | |
85c55efb | 307 | goto put_adapter; |
1fd2121c EV |
308 | } |
309 | ||
310 | memcpy(rsdev->radio_dev, &radio_si4713_vdev_template, | |
311 | sizeof(radio_si4713_vdev_template)); | |
312 | video_set_drvdata(rsdev->radio_dev, rsdev); | |
313 | if (video_register_device(rsdev->radio_dev, VFL_TYPE_RADIO, radio_nr)) { | |
314 | dev_err(&pdev->dev, "Could not register video device.\n"); | |
315 | rval = -EIO; | |
316 | goto free_vdev; | |
317 | } | |
318 | dev_info(&pdev->dev, "New device successfully probed\n"); | |
319 | ||
320 | goto exit; | |
321 | ||
322 | free_vdev: | |
323 | video_device_release(rsdev->radio_dev); | |
85c55efb JN |
324 | put_adapter: |
325 | i2c_put_adapter(adapter); | |
1fd2121c EV |
326 | unregister_v4l2_dev: |
327 | v4l2_device_unregister(&rsdev->v4l2_dev); | |
328 | free_rsdev: | |
329 | kfree(rsdev); | |
330 | exit: | |
331 | return rval; | |
332 | } | |
333 | ||
334 | /* radio_si4713_pdriver_remove - remove the device */ | |
335 | static int __exit radio_si4713_pdriver_remove(struct platform_device *pdev) | |
336 | { | |
337 | struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev); | |
338 | struct radio_si4713_device *rsdev = container_of(v4l2_dev, | |
339 | struct radio_si4713_device, | |
340 | v4l2_dev); | |
85c55efb JN |
341 | struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next, |
342 | struct v4l2_subdev, list); | |
343 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
1fd2121c EV |
344 | |
345 | video_unregister_device(rsdev->radio_dev); | |
85c55efb | 346 | i2c_put_adapter(client->adapter); |
1fd2121c EV |
347 | v4l2_device_unregister(&rsdev->v4l2_dev); |
348 | kfree(rsdev); | |
349 | ||
350 | return 0; | |
351 | } | |
352 | ||
353 | static struct platform_driver radio_si4713_pdriver = { | |
354 | .driver = { | |
355 | .name = "radio-si4713", | |
356 | }, | |
357 | .probe = radio_si4713_pdriver_probe, | |
358 | .remove = __exit_p(radio_si4713_pdriver_remove), | |
359 | }; | |
360 | ||
361 | /* Module Interface */ | |
362 | static int __init radio_si4713_module_init(void) | |
363 | { | |
364 | return platform_driver_register(&radio_si4713_pdriver); | |
365 | } | |
366 | ||
367 | static void __exit radio_si4713_module_exit(void) | |
368 | { | |
369 | platform_driver_unregister(&radio_si4713_pdriver); | |
370 | } | |
371 | ||
372 | module_init(radio_si4713_module_init); | |
373 | module_exit(radio_si4713_module_exit); | |
374 |