xref: /linux/drivers/media/radio/si4713/radio-platform-si4713.c (revision e83ce3005db16243e1085925251fd0776bb60d09)
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 
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 /* module parameters */
36 static int radio_nr = -1;	/* radio device minor (-1 ==> auto assign) */
37 module_param(radio_nr, int, 0);
38 MODULE_PARM_DESC(radio_nr,
39 		 "Minor number for radio device (-1 ==> auto assign)");
40 
41 MODULE_LICENSE("GPL v2");
42 MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
43 MODULE_DESCRIPTION("Platform driver for Si4713 FM Radio Transmitter");
44 MODULE_VERSION("0.0.1");
45 MODULE_ALIAS("platform:radio-si4713");
46 
47 /* Driver state struct */
48 struct radio_si4713_device {
49 	struct v4l2_device		v4l2_dev;
50 	struct video_device		radio_dev;
51 	struct mutex lock;
52 };
53 
54 /* radio_si4713_fops - file operations interface */
55 static 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 	/* Note: locking is done at the subdev level in the i2c driver. */
61 	.unlocked_ioctl	= video_ioctl2,
62 };
63 
64 /* Video4Linux Interface */
65 
66 /* radio_si4713_querycap - query device capabilities */
67 static 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 	return 0;
76 }
77 
78 /*
79  * v4l2 ioctl call backs.
80  * we are just a wrapper for v4l2_sub_devs.
81  */
82 static inline struct v4l2_device *get_v4l2_dev(struct file *file)
83 {
84 	return &((struct radio_si4713_device *)video_drvdata(file))->v4l2_dev;
85 }
86 
87 static int radio_si4713_g_modulator(struct file *file, void *p,
88 				    struct v4l2_modulator *vm)
89 {
90 	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
91 					  g_modulator, vm);
92 }
93 
94 static int radio_si4713_s_modulator(struct file *file, void *p,
95 				    const struct v4l2_modulator *vm)
96 {
97 	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
98 					  s_modulator, vm);
99 }
100 
101 static int radio_si4713_g_frequency(struct file *file, void *p,
102 				    struct v4l2_frequency *vf)
103 {
104 	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
105 					  g_frequency, vf);
106 }
107 
108 static int radio_si4713_s_frequency(struct file *file, void *p,
109 				    const struct v4l2_frequency *vf)
110 {
111 	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
112 					  s_frequency, vf);
113 }
114 
115 static long radio_si4713_default(struct file *file, void *p,
116 				 bool valid_prio, unsigned int cmd, void *arg)
117 {
118 	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
119 					  ioctl, cmd, arg);
120 }
121 
122 static struct v4l2_ioctl_ops radio_si4713_ioctl_ops = {
123 	.vidioc_querycap	= radio_si4713_querycap,
124 	.vidioc_g_modulator	= radio_si4713_g_modulator,
125 	.vidioc_s_modulator	= radio_si4713_s_modulator,
126 	.vidioc_g_frequency	= radio_si4713_g_frequency,
127 	.vidioc_s_frequency	= radio_si4713_s_frequency,
128 	.vidioc_log_status      = v4l2_ctrl_log_status,
129 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
130 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
131 	.vidioc_default		= radio_si4713_default,
132 };
133 
134 /* radio_si4713_vdev_template - video device interface */
135 static const struct video_device radio_si4713_vdev_template = {
136 	.fops			= &radio_si4713_fops,
137 	.name			= "radio-si4713",
138 	.release		= video_device_release_empty,
139 	.ioctl_ops		= &radio_si4713_ioctl_ops,
140 	.vfl_dir		= VFL_DIR_TX,
141 };
142 
143 /* Platform driver interface */
144 /* radio_si4713_pdriver_probe - probe for the device */
145 static int radio_si4713_pdriver_probe(struct platform_device *pdev)
146 {
147 	struct radio_si4713_platform_data *pdata = pdev->dev.platform_data;
148 	struct radio_si4713_device *rsdev;
149 	struct v4l2_subdev *sd;
150 	int rval = 0;
151 
152 	if (!pdata) {
153 		dev_err(&pdev->dev, "Cannot proceed without platform data.\n");
154 		rval = -EINVAL;
155 		goto exit;
156 	}
157 
158 	rsdev = devm_kzalloc(&pdev->dev, sizeof(*rsdev), GFP_KERNEL);
159 	if (!rsdev) {
160 		dev_err(&pdev->dev, "Failed to alloc video device.\n");
161 		rval = -ENOMEM;
162 		goto exit;
163 	}
164 	mutex_init(&rsdev->lock);
165 
166 	rval = v4l2_device_register(&pdev->dev, &rsdev->v4l2_dev);
167 	if (rval) {
168 		dev_err(&pdev->dev, "Failed to register v4l2 device.\n");
169 		goto exit;
170 	}
171 
172 	sd = i2c_get_clientdata(pdata->subdev);
173 	rval = v4l2_device_register_subdev(&rsdev->v4l2_dev, sd);
174 	if (rval) {
175 		dev_err(&pdev->dev, "Cannot get v4l2 subdevice\n");
176 		goto unregister_v4l2_dev;
177 	}
178 
179 	rsdev->radio_dev = radio_si4713_vdev_template;
180 	rsdev->radio_dev.v4l2_dev = &rsdev->v4l2_dev;
181 	rsdev->radio_dev.ctrl_handler = sd->ctrl_handler;
182 	/* Serialize all access to the si4713 */
183 	rsdev->radio_dev.lock = &rsdev->lock;
184 	rsdev->radio_dev.device_caps = V4L2_CAP_MODULATOR | V4L2_CAP_RDS_OUTPUT;
185 	video_set_drvdata(&rsdev->radio_dev, rsdev);
186 	if (video_register_device(&rsdev->radio_dev, VFL_TYPE_RADIO, radio_nr)) {
187 		dev_err(&pdev->dev, "Could not register video device.\n");
188 		rval = -EIO;
189 		goto unregister_v4l2_dev;
190 	}
191 	dev_info(&pdev->dev, "New device successfully probed\n");
192 
193 	goto exit;
194 
195 unregister_v4l2_dev:
196 	v4l2_device_unregister(&rsdev->v4l2_dev);
197 exit:
198 	return rval;
199 }
200 
201 /* radio_si4713_pdriver_remove - remove the device */
202 static int radio_si4713_pdriver_remove(struct platform_device *pdev)
203 {
204 	struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
205 	struct radio_si4713_device *rsdev;
206 
207 	rsdev = container_of(v4l2_dev, struct radio_si4713_device, v4l2_dev);
208 	video_unregister_device(&rsdev->radio_dev);
209 	v4l2_device_unregister(&rsdev->v4l2_dev);
210 
211 	return 0;
212 }
213 
214 static struct platform_driver radio_si4713_pdriver = {
215 	.driver		= {
216 		.name	= "radio-si4713",
217 	},
218 	.probe		= radio_si4713_pdriver_probe,
219 	.remove         = radio_si4713_pdriver_remove,
220 };
221 
222 module_platform_driver(radio_si4713_pdriver);
223