blob: 6c7597383ca2e0d54f8b71147e449afedb3c75ba [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>
Hans Verkuilcd29ed82013-04-08 07:23:32 -030034#include <media/v4l2-fh.h>
35#include <media/v4l2-ctrls.h>
36#include <media/v4l2-event.h>
Sebastian Reichel98bea622014-11-10 17:34:41 -030037#include "si4713.h"
Eduardo Valentin1fd21212009-08-08 08:45:49 -030038
39/* module parameters */
40static int radio_nr = -1; /* radio device minor (-1 ==> auto assign) */
41module_param(radio_nr, int, 0);
42MODULE_PARM_DESC(radio_nr,
43 "Minor number for radio device (-1 ==> auto assign)");
44
Eduardo Valentina782ae02013-03-19 11:41:32 -030045MODULE_LICENSE("GPL v2");
Eduardo Valentin1fd21212009-08-08 08:45:49 -030046MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
47MODULE_DESCRIPTION("Platform driver for Si4713 FM Radio Transmitter");
48MODULE_VERSION("0.0.1");
Eduardo Valentin04e1ac02013-03-19 11:41:34 -030049MODULE_ALIAS("platform:radio-si4713");
Eduardo Valentin1fd21212009-08-08 08:45:49 -030050
51/* Driver state struct */
52struct radio_si4713_device {
53 struct v4l2_device v4l2_dev;
Hans Verkuilfdf51592013-04-08 06:11:12 -030054 struct video_device radio_dev;
Hans Verkuil55b2a312013-04-08 06:31:30 -030055 struct mutex lock;
Eduardo Valentin1fd21212009-08-08 08:45:49 -030056};
57
58/* radio_si4713_fops - file operations interface */
59static const struct v4l2_file_operations radio_si4713_fops = {
60 .owner = THIS_MODULE,
Hans Verkuilcd29ed82013-04-08 07:23:32 -030061 .open = v4l2_fh_open,
62 .release = v4l2_fh_release,
63 .poll = v4l2_ctrl_poll,
Hans Verkuil725ea8c2010-11-14 09:48:24 -030064 /* Note: locking is done at the subdev level in the i2c driver. */
65 .unlocked_ioctl = video_ioctl2,
Eduardo Valentin1fd21212009-08-08 08:45:49 -030066};
67
68/* Video4Linux Interface */
Eduardo Valentin1fd21212009-08-08 08:45:49 -030069
70/* radio_si4713_querycap - query device capabilities */
71static int radio_si4713_querycap(struct file *file, void *priv,
72 struct v4l2_capability *capability)
73{
Eduardo Valentin1fd21212009-08-08 08:45:49 -030074 strlcpy(capability->driver, "radio-si4713", sizeof(capability->driver));
75 strlcpy(capability->card, "Silicon Labs Si4713 Modulator",
Eduardo Valentin385624a2013-03-19 11:41:31 -030076 sizeof(capability->card));
Hans Verkuila58841d2013-04-08 06:27:43 -030077 strlcpy(capability->bus_info, "platform:radio-si4713",
78 sizeof(capability->bus_info));
79 capability->device_caps = V4L2_CAP_MODULATOR | V4L2_CAP_RDS_OUTPUT;
80 capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS;
Eduardo Valentin1fd21212009-08-08 08:45:49 -030081
82 return 0;
83}
84
Eduardo Valentin1fd21212009-08-08 08:45:49 -030085/*
86 * v4l2 ioctl call backs.
87 * we are just a wrapper for v4l2_sub_devs.
88 */
89static inline struct v4l2_device *get_v4l2_dev(struct file *file)
90{
91 return &((struct radio_si4713_device *)video_drvdata(file))->v4l2_dev;
92}
93
Eduardo Valentin1fd21212009-08-08 08:45:49 -030094static int radio_si4713_g_modulator(struct file *file, void *p,
Eduardo Valentin385624a2013-03-19 11:41:31 -030095 struct v4l2_modulator *vm)
Eduardo Valentin1fd21212009-08-08 08:45:49 -030096{
97 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
Eduardo Valentin385624a2013-03-19 11:41:31 -030098 g_modulator, vm);
Eduardo Valentin1fd21212009-08-08 08:45:49 -030099}
100
101static int radio_si4713_s_modulator(struct file *file, void *p,
Eduardo Valentin385624a2013-03-19 11:41:31 -0300102 const struct v4l2_modulator *vm)
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300103{
104 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
Eduardo Valentin385624a2013-03-19 11:41:31 -0300105 s_modulator, vm);
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300106}
107
108static int radio_si4713_g_frequency(struct file *file, void *p,
Eduardo Valentin385624a2013-03-19 11:41:31 -0300109 struct v4l2_frequency *vf)
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300110{
111 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
Eduardo Valentin385624a2013-03-19 11:41:31 -0300112 g_frequency, vf);
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300113}
114
115static int radio_si4713_s_frequency(struct file *file, void *p,
Hans Verkuilb530a442013-03-19 04:09:26 -0300116 const struct v4l2_frequency *vf)
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300117{
118 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
Eduardo Valentin385624a2013-03-19 11:41:31 -0300119 s_frequency, vf);
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300120}
121
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300122static long radio_si4713_default(struct file *file, void *p,
Mauro Carvalho Chehab6d43be72013-03-26 08:04:52 -0300123 bool valid_prio, unsigned int cmd, void *arg)
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300124{
125 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
Eduardo Valentin385624a2013-03-19 11:41:31 -0300126 ioctl, cmd, arg);
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300127}
128
129static struct v4l2_ioctl_ops radio_si4713_ioctl_ops = {
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300130 .vidioc_querycap = radio_si4713_querycap,
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300131 .vidioc_g_modulator = radio_si4713_g_modulator,
132 .vidioc_s_modulator = radio_si4713_s_modulator,
133 .vidioc_g_frequency = radio_si4713_g_frequency,
134 .vidioc_s_frequency = radio_si4713_s_frequency,
Hans Verkuilcd29ed82013-04-08 07:23:32 -0300135 .vidioc_log_status = v4l2_ctrl_log_status,
136 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
137 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300138 .vidioc_default = radio_si4713_default,
139};
140
141/* radio_si4713_vdev_template - video device interface */
142static struct video_device radio_si4713_vdev_template = {
143 .fops = &radio_si4713_fops,
144 .name = "radio-si4713",
Hans Verkuilfdf51592013-04-08 06:11:12 -0300145 .release = video_device_release_empty,
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300146 .ioctl_ops = &radio_si4713_ioctl_ops,
Hans Verkuilce4a3d52013-01-05 08:52:12 -0300147 .vfl_dir = VFL_DIR_TX,
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300148};
149
150/* Platform driver interface */
151/* radio_si4713_pdriver_probe - probe for the device */
152static int radio_si4713_pdriver_probe(struct platform_device *pdev)
153{
154 struct radio_si4713_platform_data *pdata = pdev->dev.platform_data;
155 struct radio_si4713_device *rsdev;
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300156 struct v4l2_subdev *sd;
157 int rval = 0;
158
159 if (!pdata) {
160 dev_err(&pdev->dev, "Cannot proceed without platform data.\n");
161 rval = -EINVAL;
162 goto exit;
163 }
164
Julia Lawall28e9cc82012-07-31 05:21:36 -0300165 rsdev = devm_kzalloc(&pdev->dev, sizeof(*rsdev), GFP_KERNEL);
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300166 if (!rsdev) {
167 dev_err(&pdev->dev, "Failed to alloc video device.\n");
168 rval = -ENOMEM;
169 goto exit;
170 }
Hans Verkuil55b2a312013-04-08 06:31:30 -0300171 mutex_init(&rsdev->lock);
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300172
173 rval = v4l2_device_register(&pdev->dev, &rsdev->v4l2_dev);
174 if (rval) {
175 dev_err(&pdev->dev, "Failed to register v4l2 device.\n");
Julia Lawall28e9cc82012-07-31 05:21:36 -0300176 goto exit;
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300177 }
178
Sebastian Reichel98bea622014-11-10 17:34:41 -0300179 sd = i2c_get_clientdata(pdata->subdev);
180 rval = v4l2_device_register_subdev(&rsdev->v4l2_dev, sd);
181 if (rval) {
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300182 dev_err(&pdev->dev, "Cannot get v4l2 subdevice\n");
Sebastian Reichel98bea622014-11-10 17:34:41 -0300183 goto unregister_v4l2_dev;
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300184 }
185
Hans Verkuilfdf51592013-04-08 06:11:12 -0300186 rsdev->radio_dev = radio_si4713_vdev_template;
187 rsdev->radio_dev.v4l2_dev = &rsdev->v4l2_dev;
Hans Verkuil03aa1bc2013-04-08 17:27:18 -0300188 rsdev->radio_dev.ctrl_handler = sd->ctrl_handler;
Hans Verkuil55b2a312013-04-08 06:31:30 -0300189 /* Serialize all access to the si4713 */
190 rsdev->radio_dev.lock = &rsdev->lock;
Hans Verkuilfdf51592013-04-08 06:11:12 -0300191 video_set_drvdata(&rsdev->radio_dev, rsdev);
192 if (video_register_device(&rsdev->radio_dev, VFL_TYPE_RADIO, radio_nr)) {
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300193 dev_err(&pdev->dev, "Could not register video device.\n");
194 rval = -EIO;
Sebastian Reichel98bea622014-11-10 17:34:41 -0300195 goto unregister_v4l2_dev;
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300196 }
197 dev_info(&pdev->dev, "New device successfully probed\n");
198
199 goto exit;
200
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300201unregister_v4l2_dev:
202 v4l2_device_unregister(&rsdev->v4l2_dev);
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300203exit:
204 return rval;
205}
206
207/* radio_si4713_pdriver_remove - remove the device */
Dmitry Torokhovbf306902013-02-26 03:17:27 -0300208static int radio_si4713_pdriver_remove(struct platform_device *pdev)
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300209{
210 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
Eduardo Valentin385624a2013-03-19 11:41:31 -0300211 struct radio_si4713_device *rsdev;
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300212
Eduardo Valentin385624a2013-03-19 11:41:31 -0300213 rsdev = container_of(v4l2_dev, struct radio_si4713_device, v4l2_dev);
Hans Verkuilfdf51592013-04-08 06:11:12 -0300214 video_unregister_device(&rsdev->radio_dev);
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300215 v4l2_device_unregister(&rsdev->v4l2_dev);
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300216
217 return 0;
218}
219
220static struct platform_driver radio_si4713_pdriver = {
221 .driver = {
222 .name = "radio-si4713",
223 },
224 .probe = radio_si4713_pdriver_probe,
Dmitry Torokhovbf306902013-02-26 03:17:27 -0300225 .remove = radio_si4713_pdriver_remove,
Eduardo Valentin1fd21212009-08-08 08:45:49 -0300226};
227
Axel Lin1d6629b2012-01-10 03:21:49 -0300228module_platform_driver(radio_si4713_pdriver);