blob: 1507c9d508d7c59e98ea0f58cbc017726b3329c5 [file] [log] [blame]
Eduardo Valentin1fd21212009-08-08 08:45:49 -03001/*
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>
Eduardo Valentin1fd21212009-08-08 08:45:49 -030027#include <linux/platform_device.h>
28#include <linux/i2c.h>
29#include <linux/videodev2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Eduardo Valentin1fd21212009-08-08 08:45:49 -030031#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 */
37static int radio_nr = -1; /* radio device minor (-1 ==> auto assign) */
38module_param(radio_nr, int, 0);
39MODULE_PARM_DESC(radio_nr,
40 "Minor number for radio device (-1 ==> auto assign)");
41
42MODULE_LICENSE("GPL");
43MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
44MODULE_DESCRIPTION("Platform driver for Si4713 FM Radio Transmitter");
45MODULE_VERSION("0.0.1");
46
47/* Driver state struct */
48struct radio_si4713_device {
49 struct v4l2_device v4l2_dev;
50 struct video_device *radio_dev;
51};
52
53/* radio_si4713_fops - file operations interface */
54static const struct v4l2_file_operations radio_si4713_fops = {
55 .owner = THIS_MODULE,
Hans Verkuil725ea8c2010-11-14 09:48:24 -030056 /* Note: locking is done at the subdev level in the i2c driver. */
57 .unlocked_ioctl = video_ioctl2,
Eduardo Valentin1fd21212009-08-08 08:45:49 -030058};
59
60/* Video4Linux Interface */
61static 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
69static int radio_si4713_enumaudout(struct file *file, void *priv,
70 struct v4l2_audioout *vao)
71{
72 return radio_si4713_fill_audout(vao);
73}
74
75static 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
85static int radio_si4713_s_audout(struct file *file, void *priv,
Hans Verkuilba9425b2012-09-04 12:03:49 -030086 const struct v4l2_audioout *vao)
Eduardo Valentin1fd21212009-08-08 08:45:49 -030087{
88 return vao->index ? -EINVAL : 0;
89}
90
91/* radio_si4713_querycap - query device capabilities */
92static int radio_si4713_querycap(struct file *file, void *priv,
93 struct v4l2_capability *capability)
94{
Eduardo Valentin1fd21212009-08-08 08:45:49 -030095 strlcpy(capability->driver, "radio-si4713", sizeof(capability->driver));
96 strlcpy(capability->card, "Silicon Labs Si4713 Modulator",
97 sizeof(capability->card));
98 capability->capabilities = V4L2_CAP_MODULATOR | V4L2_CAP_RDS_OUTPUT;
99
100 return 0;
101}
102
103/* radio_si4713_queryctrl - enumerate control items */
104static int radio_si4713_queryctrl(struct file *file, void *priv,
105 struct v4l2_queryctrl *qc)
106{
107 /* Must be sorted from low to high control ID! */
108 static const u32 user_ctrls[] = {
109 V4L2_CID_USER_CLASS,
110 V4L2_CID_AUDIO_MUTE,
111 0
112 };
113
114 /* Must be sorted from low to high control ID! */
115 static const u32 fmtx_ctrls[] = {
116 V4L2_CID_FM_TX_CLASS,
117 V4L2_CID_RDS_TX_DEVIATION,
118 V4L2_CID_RDS_TX_PI,
119 V4L2_CID_RDS_TX_PTY,
120 V4L2_CID_RDS_TX_PS_NAME,
121 V4L2_CID_RDS_TX_RADIO_TEXT,
122 V4L2_CID_AUDIO_LIMITER_ENABLED,
123 V4L2_CID_AUDIO_LIMITER_RELEASE_TIME,
124 V4L2_CID_AUDIO_LIMITER_DEVIATION,
125 V4L2_CID_AUDIO_COMPRESSION_ENABLED,
126 V4L2_CID_AUDIO_COMPRESSION_GAIN,
127 V4L2_CID_AUDIO_COMPRESSION_THRESHOLD,
128 V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME,
129 V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME,
130 V4L2_CID_PILOT_TONE_ENABLED,
131 V4L2_CID_PILOT_TONE_DEVIATION,
132 V4L2_CID_PILOT_TONE_FREQUENCY,
133 V4L2_CID_TUNE_PREEMPHASIS,
134 V4L2_CID_TUNE_POWER_LEVEL,
135 V4L2_CID_TUNE_ANTENNA_CAPACITOR,
136 0
137 };
138 static const u32 *ctrl_classes[] = {
139 user_ctrls,
140 fmtx_ctrls,
141 NULL
142 };
143 struct radio_si4713_device *rsdev;
144
145 rsdev = video_get_drvdata(video_devdata(file));
146
147 qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
148 if (qc->id == 0)
149 return -EINVAL;
150
151 if (qc->id == V4L2_CID_USER_CLASS || qc->id == V4L2_CID_FM_TX_CLASS)
152 return v4l2_ctrl_query_fill(qc, 0, 0, 0, 0);
153
154 return v4l2_device_call_until_err(&rsdev->v4l2_dev, 0, core,
155 queryctrl, qc);
156}
157
158/*
159 * v4l2 ioctl call backs.
160 * we are just a wrapper for v4l2_sub_devs.
161 */
162static inline struct v4l2_device *get_v4l2_dev(struct file *file)
163{
164 return &((struct radio_si4713_device *)video_drvdata(file))->v4l2_dev;
165}
166
167static int radio_si4713_g_ext_ctrls(struct file *file, void *p,
168 struct v4l2_ext_controls *vecs)
169{
170 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
171 g_ext_ctrls, vecs);
172}
173
174static int radio_si4713_s_ext_ctrls(struct file *file, void *p,
175 struct v4l2_ext_controls *vecs)
176{
177 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
178 s_ext_ctrls, vecs);
179}
180
181static int radio_si4713_g_ctrl(struct file *file, void *p,
182 struct v4l2_control *vc)
183{
184 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
185 g_ctrl, vc);
186}
187
188static int radio_si4713_s_ctrl(struct file *file, void *p,
189 struct v4l2_control *vc)
190{
191 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
192 s_ctrl, vc);
193}
194
195static int radio_si4713_g_modulator(struct file *file, void *p,
196 struct v4l2_modulator *vm)
197{
198 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
199 g_modulator, vm);
200}
201
202static int radio_si4713_s_modulator(struct file *file, void *p,
Hans Verkuil3f70e1f2012-09-04 12:08:47 -0300203 const struct v4l2_modulator *vm)
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300204{
205 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
206 s_modulator, vm);
207}
208
209static int radio_si4713_g_frequency(struct file *file, void *p,
210 struct v4l2_frequency *vf)
211{
212 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
213 g_frequency, vf);
214}
215
216static int radio_si4713_s_frequency(struct file *file, void *p,
217 struct v4l2_frequency *vf)
218{
219 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
220 s_frequency, vf);
221}
222
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300223static long radio_si4713_default(struct file *file, void *p,
224 bool valid_prio, int cmd, void *arg)
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300225{
226 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
227 ioctl, cmd, arg);
228}
229
230static struct v4l2_ioctl_ops radio_si4713_ioctl_ops = {
231 .vidioc_enumaudout = radio_si4713_enumaudout,
232 .vidioc_g_audout = radio_si4713_g_audout,
233 .vidioc_s_audout = radio_si4713_s_audout,
234 .vidioc_querycap = radio_si4713_querycap,
235 .vidioc_queryctrl = radio_si4713_queryctrl,
236 .vidioc_g_ext_ctrls = radio_si4713_g_ext_ctrls,
237 .vidioc_s_ext_ctrls = radio_si4713_s_ext_ctrls,
238 .vidioc_g_ctrl = radio_si4713_g_ctrl,
239 .vidioc_s_ctrl = radio_si4713_s_ctrl,
240 .vidioc_g_modulator = radio_si4713_g_modulator,
241 .vidioc_s_modulator = radio_si4713_s_modulator,
242 .vidioc_g_frequency = radio_si4713_g_frequency,
243 .vidioc_s_frequency = radio_si4713_s_frequency,
244 .vidioc_default = radio_si4713_default,
245};
246
247/* radio_si4713_vdev_template - video device interface */
248static struct video_device radio_si4713_vdev_template = {
249 .fops = &radio_si4713_fops,
250 .name = "radio-si4713",
251 .release = video_device_release,
252 .ioctl_ops = &radio_si4713_ioctl_ops,
Hans Verkuilce4a3d52013-01-05 08:52:12 -0300253 .vfl_dir = VFL_DIR_TX,
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300254};
255
256/* Platform driver interface */
257/* radio_si4713_pdriver_probe - probe for the device */
258static int radio_si4713_pdriver_probe(struct platform_device *pdev)
259{
260 struct radio_si4713_platform_data *pdata = pdev->dev.platform_data;
261 struct radio_si4713_device *rsdev;
262 struct i2c_adapter *adapter;
263 struct v4l2_subdev *sd;
264 int rval = 0;
265
266 if (!pdata) {
267 dev_err(&pdev->dev, "Cannot proceed without platform data.\n");
268 rval = -EINVAL;
269 goto exit;
270 }
271
Julia Lawall28e9cc82012-07-31 05:21:36 -0300272 rsdev = devm_kzalloc(&pdev->dev, sizeof(*rsdev), GFP_KERNEL);
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300273 if (!rsdev) {
274 dev_err(&pdev->dev, "Failed to alloc video device.\n");
275 rval = -ENOMEM;
276 goto exit;
277 }
278
279 rval = v4l2_device_register(&pdev->dev, &rsdev->v4l2_dev);
280 if (rval) {
281 dev_err(&pdev->dev, "Failed to register v4l2 device.\n");
Julia Lawall28e9cc82012-07-31 05:21:36 -0300282 goto exit;
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300283 }
284
285 adapter = i2c_get_adapter(pdata->i2c_bus);
286 if (!adapter) {
287 dev_err(&pdev->dev, "Cannot get i2c adapter %d\n",
288 pdata->i2c_bus);
289 rval = -ENODEV;
290 goto unregister_v4l2_dev;
291 }
292
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300293 sd = v4l2_i2c_new_subdev_board(&rsdev->v4l2_dev, adapter,
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300294 pdata->subdev_board_info, NULL);
295 if (!sd) {
296 dev_err(&pdev->dev, "Cannot get v4l2 subdevice\n");
297 rval = -ENODEV;
Jarkko Nikula85c55ef2010-09-21 05:49:42 -0300298 goto put_adapter;
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300299 }
300
301 rsdev->radio_dev = video_device_alloc();
302 if (!rsdev->radio_dev) {
303 dev_err(&pdev->dev, "Failed to alloc video device.\n");
304 rval = -ENOMEM;
Jarkko Nikula85c55ef2010-09-21 05:49:42 -0300305 goto put_adapter;
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300306 }
307
308 memcpy(rsdev->radio_dev, &radio_si4713_vdev_template,
309 sizeof(radio_si4713_vdev_template));
310 video_set_drvdata(rsdev->radio_dev, rsdev);
311 if (video_register_device(rsdev->radio_dev, VFL_TYPE_RADIO, radio_nr)) {
312 dev_err(&pdev->dev, "Could not register video device.\n");
313 rval = -EIO;
314 goto free_vdev;
315 }
316 dev_info(&pdev->dev, "New device successfully probed\n");
317
318 goto exit;
319
320free_vdev:
321 video_device_release(rsdev->radio_dev);
Jarkko Nikula85c55ef2010-09-21 05:49:42 -0300322put_adapter:
323 i2c_put_adapter(adapter);
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300324unregister_v4l2_dev:
325 v4l2_device_unregister(&rsdev->v4l2_dev);
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300326exit:
327 return rval;
328}
329
330/* radio_si4713_pdriver_remove - remove the device */
331static int __exit radio_si4713_pdriver_remove(struct platform_device *pdev)
332{
333 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
334 struct radio_si4713_device *rsdev = container_of(v4l2_dev,
335 struct radio_si4713_device,
336 v4l2_dev);
Jarkko Nikula85c55ef2010-09-21 05:49:42 -0300337 struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
338 struct v4l2_subdev, list);
339 struct i2c_client *client = v4l2_get_subdevdata(sd);
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300340
341 video_unregister_device(rsdev->radio_dev);
Jarkko Nikula85c55ef2010-09-21 05:49:42 -0300342 i2c_put_adapter(client->adapter);
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300343 v4l2_device_unregister(&rsdev->v4l2_dev);
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300344
345 return 0;
346}
347
348static struct platform_driver radio_si4713_pdriver = {
349 .driver = {
350 .name = "radio-si4713",
351 },
352 .probe = radio_si4713_pdriver_probe,
353 .remove = __exit_p(radio_si4713_pdriver_remove),
354};
355
Axel Lin1d6629b2012-01-10 03:21:49 -0300356module_platform_driver(radio_si4713_pdriver);