xref: /linux/drivers/media/radio/si4713/radio-platform-si4713.c (revision 336879b1da97fffc097f77c6d6f818660f2826f0)
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>
27 #include <linux/platform_device.h>
28 #include <linux/i2c.h>
29 #include <linux/videodev2.h>
30 #include <linux/slab.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-fh.h>
35 #include <media/v4l2-ctrls.h>
36 #include <media/v4l2-event.h>
37 #include <media/radio-si4713.h>
38 
39 /* module parameters */
40 static int radio_nr = -1;	/* radio device minor (-1 ==> auto assign) */
41 module_param(radio_nr, int, 0);
42 MODULE_PARM_DESC(radio_nr,
43 		 "Minor number for radio device (-1 ==> auto assign)");
44 
45 MODULE_LICENSE("GPL v2");
46 MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
47 MODULE_DESCRIPTION("Platform driver for Si4713 FM Radio Transmitter");
48 MODULE_VERSION("0.0.1");
49 MODULE_ALIAS("platform:radio-si4713");
50 
51 /* Driver state struct */
52 struct radio_si4713_device {
53 	struct v4l2_device		v4l2_dev;
54 	struct video_device		radio_dev;
55 	struct mutex lock;
56 };
57 
58 /* radio_si4713_fops - file operations interface */
59 static const struct v4l2_file_operations radio_si4713_fops = {
60 	.owner		= THIS_MODULE,
61 	.open = v4l2_fh_open,
62 	.release = v4l2_fh_release,
63 	.poll = v4l2_ctrl_poll,
64 	/* Note: locking is done at the subdev level in the i2c driver. */
65 	.unlocked_ioctl	= video_ioctl2,
66 };
67 
68 /* Video4Linux Interface */
69 
70 /* radio_si4713_querycap - query device capabilities */
71 static int radio_si4713_querycap(struct file *file, void *priv,
72 					struct v4l2_capability *capability)
73 {
74 	strlcpy(capability->driver, "radio-si4713", sizeof(capability->driver));
75 	strlcpy(capability->card, "Silicon Labs Si4713 Modulator",
76 		sizeof(capability->card));
77 	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;
81 
82 	return 0;
83 }
84 
85 /*
86  * v4l2 ioctl call backs.
87  * we are just a wrapper for v4l2_sub_devs.
88  */
89 static inline struct v4l2_device *get_v4l2_dev(struct file *file)
90 {
91 	return &((struct radio_si4713_device *)video_drvdata(file))->v4l2_dev;
92 }
93 
94 static int radio_si4713_g_modulator(struct file *file, void *p,
95 				    struct v4l2_modulator *vm)
96 {
97 	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
98 					  g_modulator, vm);
99 }
100 
101 static int radio_si4713_s_modulator(struct file *file, void *p,
102 				    const struct v4l2_modulator *vm)
103 {
104 	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
105 					  s_modulator, vm);
106 }
107 
108 static int radio_si4713_g_frequency(struct file *file, void *p,
109 				    struct v4l2_frequency *vf)
110 {
111 	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
112 					  g_frequency, vf);
113 }
114 
115 static int radio_si4713_s_frequency(struct file *file, void *p,
116 				    const struct v4l2_frequency *vf)
117 {
118 	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
119 					  s_frequency, vf);
120 }
121 
122 static long radio_si4713_default(struct file *file, void *p,
123 				 bool valid_prio, unsigned int cmd, void *arg)
124 {
125 	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
126 					  ioctl, cmd, arg);
127 }
128 
129 static struct v4l2_ioctl_ops radio_si4713_ioctl_ops = {
130 	.vidioc_querycap	= radio_si4713_querycap,
131 	.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,
135 	.vidioc_log_status      = v4l2_ctrl_log_status,
136 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
137 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
138 	.vidioc_default		= radio_si4713_default,
139 };
140 
141 /* radio_si4713_vdev_template - video device interface */
142 static struct video_device radio_si4713_vdev_template = {
143 	.fops			= &radio_si4713_fops,
144 	.name			= "radio-si4713",
145 	.release		= video_device_release_empty,
146 	.ioctl_ops		= &radio_si4713_ioctl_ops,
147 	.vfl_dir		= VFL_DIR_TX,
148 };
149 
150 /* Platform driver interface */
151 /* radio_si4713_pdriver_probe - probe for the device */
152 static 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;
156 	struct i2c_adapter *adapter;
157 	struct v4l2_subdev *sd;
158 	int rval = 0;
159 
160 	if (!pdata) {
161 		dev_err(&pdev->dev, "Cannot proceed without platform data.\n");
162 		rval = -EINVAL;
163 		goto exit;
164 	}
165 
166 	rsdev = devm_kzalloc(&pdev->dev, sizeof(*rsdev), GFP_KERNEL);
167 	if (!rsdev) {
168 		dev_err(&pdev->dev, "Failed to alloc video device.\n");
169 		rval = -ENOMEM;
170 		goto exit;
171 	}
172 	mutex_init(&rsdev->lock);
173 
174 	rval = v4l2_device_register(&pdev->dev, &rsdev->v4l2_dev);
175 	if (rval) {
176 		dev_err(&pdev->dev, "Failed to register v4l2 device.\n");
177 		goto exit;
178 	}
179 
180 	adapter = i2c_get_adapter(pdata->i2c_bus);
181 	if (!adapter) {
182 		dev_err(&pdev->dev, "Cannot get i2c adapter %d\n",
183 			pdata->i2c_bus);
184 		rval = -ENODEV;
185 		goto unregister_v4l2_dev;
186 	}
187 
188 	sd = v4l2_i2c_new_subdev_board(&rsdev->v4l2_dev, adapter,
189 				       pdata->subdev_board_info, NULL);
190 	if (!sd) {
191 		dev_err(&pdev->dev, "Cannot get v4l2 subdevice\n");
192 		rval = -ENODEV;
193 		goto put_adapter;
194 	}
195 
196 	rsdev->radio_dev = radio_si4713_vdev_template;
197 	rsdev->radio_dev.v4l2_dev = &rsdev->v4l2_dev;
198 	rsdev->radio_dev.ctrl_handler = sd->ctrl_handler;
199 	/* Serialize all access to the si4713 */
200 	rsdev->radio_dev.lock = &rsdev->lock;
201 	video_set_drvdata(&rsdev->radio_dev, rsdev);
202 	if (video_register_device(&rsdev->radio_dev, VFL_TYPE_RADIO, radio_nr)) {
203 		dev_err(&pdev->dev, "Could not register video device.\n");
204 		rval = -EIO;
205 		goto put_adapter;
206 	}
207 	dev_info(&pdev->dev, "New device successfully probed\n");
208 
209 	goto exit;
210 
211 put_adapter:
212 	i2c_put_adapter(adapter);
213 unregister_v4l2_dev:
214 	v4l2_device_unregister(&rsdev->v4l2_dev);
215 exit:
216 	return rval;
217 }
218 
219 /* radio_si4713_pdriver_remove - remove the device */
220 static int radio_si4713_pdriver_remove(struct platform_device *pdev)
221 {
222 	struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
223 	struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
224 					    struct v4l2_subdev, list);
225 	struct i2c_client *client = v4l2_get_subdevdata(sd);
226 	struct radio_si4713_device *rsdev;
227 
228 	rsdev = container_of(v4l2_dev, struct radio_si4713_device, v4l2_dev);
229 	video_unregister_device(&rsdev->radio_dev);
230 	i2c_put_adapter(client->adapter);
231 	v4l2_device_unregister(&rsdev->v4l2_dev);
232 
233 	return 0;
234 }
235 
236 static struct platform_driver radio_si4713_pdriver = {
237 	.driver		= {
238 		.name	= "radio-si4713",
239 		.owner	= THIS_MODULE,
240 	},
241 	.probe		= radio_si4713_pdriver_probe,
242 	.remove         = radio_si4713_pdriver_remove,
243 };
244 
245 module_platform_driver(radio_si4713_pdriver);
246