xref: /linux/drivers/media/radio/si4713/radio-platform-si4713.c (revision e1d700f7c94e755106749411706a38e39a93404b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * drivers/media/radio/radio-si4713.c
4  *
5  * Platform Driver for Silicon Labs Si4713 FM Radio Transmitter:
6  *
7  * Copyright (c) 2008 Instituto Nokia de Tecnologia - INdT
8  * Contact: Eduardo Valentin <eduardo.valentin@nokia.com>
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/platform_device.h>
15 #include <linux/i2c.h>
16 #include <linux/videodev2.h>
17 #include <linux/slab.h>
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-common.h>
20 #include <media/v4l2-ioctl.h>
21 #include <media/v4l2-fh.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-event.h>
24 #include "si4713.h"
25 
26 /* module parameters */
27 static int radio_nr = -1;	/* radio device minor (-1 ==> auto assign) */
28 module_param(radio_nr, int, 0);
29 MODULE_PARM_DESC(radio_nr,
30 		 "Minor number for radio device (-1 ==> auto assign)");
31 
32 MODULE_LICENSE("GPL v2");
33 MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
34 MODULE_DESCRIPTION("Platform driver for Si4713 FM Radio Transmitter");
35 MODULE_VERSION("0.0.1");
36 MODULE_ALIAS("platform:radio-si4713");
37 
38 /* Driver state struct */
39 struct radio_si4713_device {
40 	struct v4l2_device		v4l2_dev;
41 	struct video_device		radio_dev;
42 	struct mutex lock;
43 };
44 
45 /* radio_si4713_fops - file operations interface */
46 static const struct v4l2_file_operations radio_si4713_fops = {
47 	.owner		= THIS_MODULE,
48 	.open = v4l2_fh_open,
49 	.release = v4l2_fh_release,
50 	.poll = v4l2_ctrl_poll,
51 	/* Note: locking is done at the subdev level in the i2c driver. */
52 	.unlocked_ioctl	= video_ioctl2,
53 };
54 
55 /* Video4Linux Interface */
56 
57 /* radio_si4713_querycap - query device capabilities */
58 static int radio_si4713_querycap(struct file *file, void *priv,
59 					struct v4l2_capability *capability)
60 {
61 	strscpy(capability->driver, "radio-si4713", sizeof(capability->driver));
62 	strscpy(capability->card, "Silicon Labs Si4713 Modulator",
63 		sizeof(capability->card));
64 	strscpy(capability->bus_info, "platform:radio-si4713",
65 		sizeof(capability->bus_info));
66 	capability->device_caps = V4L2_CAP_MODULATOR | V4L2_CAP_RDS_OUTPUT;
67 	capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS;
68 
69 	return 0;
70 }
71 
72 /*
73  * v4l2 ioctl call backs.
74  * we are just a wrapper for v4l2_sub_devs.
75  */
76 static inline struct v4l2_device *get_v4l2_dev(struct file *file)
77 {
78 	return &((struct radio_si4713_device *)video_drvdata(file))->v4l2_dev;
79 }
80 
81 static int radio_si4713_g_modulator(struct file *file, void *p,
82 				    struct v4l2_modulator *vm)
83 {
84 	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
85 					  g_modulator, vm);
86 }
87 
88 static int radio_si4713_s_modulator(struct file *file, void *p,
89 				    const struct v4l2_modulator *vm)
90 {
91 	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
92 					  s_modulator, vm);
93 }
94 
95 static int radio_si4713_g_frequency(struct file *file, void *p,
96 				    struct v4l2_frequency *vf)
97 {
98 	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
99 					  g_frequency, vf);
100 }
101 
102 static int radio_si4713_s_frequency(struct file *file, void *p,
103 				    const struct v4l2_frequency *vf)
104 {
105 	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
106 					  s_frequency, vf);
107 }
108 
109 static long radio_si4713_default(struct file *file, void *p,
110 				 bool valid_prio, unsigned int cmd, void *arg)
111 {
112 	return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
113 					  ioctl, cmd, arg);
114 }
115 
116 static struct v4l2_ioctl_ops radio_si4713_ioctl_ops = {
117 	.vidioc_querycap	= radio_si4713_querycap,
118 	.vidioc_g_modulator	= radio_si4713_g_modulator,
119 	.vidioc_s_modulator	= radio_si4713_s_modulator,
120 	.vidioc_g_frequency	= radio_si4713_g_frequency,
121 	.vidioc_s_frequency	= radio_si4713_s_frequency,
122 	.vidioc_log_status      = v4l2_ctrl_log_status,
123 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
124 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
125 	.vidioc_default		= radio_si4713_default,
126 };
127 
128 /* radio_si4713_vdev_template - video device interface */
129 static const struct video_device radio_si4713_vdev_template = {
130 	.fops			= &radio_si4713_fops,
131 	.name			= "radio-si4713",
132 	.release		= video_device_release_empty,
133 	.ioctl_ops		= &radio_si4713_ioctl_ops,
134 	.vfl_dir		= VFL_DIR_TX,
135 };
136 
137 /* Platform driver interface */
138 /* radio_si4713_pdriver_probe - probe for the device */
139 static int radio_si4713_pdriver_probe(struct platform_device *pdev)
140 {
141 	struct radio_si4713_platform_data *pdata = pdev->dev.platform_data;
142 	struct radio_si4713_device *rsdev;
143 	struct v4l2_subdev *sd;
144 	int rval = 0;
145 
146 	if (!pdata) {
147 		dev_err(&pdev->dev, "Cannot proceed without platform data.\n");
148 		rval = -EINVAL;
149 		goto exit;
150 	}
151 
152 	rsdev = devm_kzalloc(&pdev->dev, sizeof(*rsdev), GFP_KERNEL);
153 	if (!rsdev) {
154 		dev_err(&pdev->dev, "Failed to alloc video device.\n");
155 		rval = -ENOMEM;
156 		goto exit;
157 	}
158 	mutex_init(&rsdev->lock);
159 
160 	rval = v4l2_device_register(&pdev->dev, &rsdev->v4l2_dev);
161 	if (rval) {
162 		dev_err(&pdev->dev, "Failed to register v4l2 device.\n");
163 		goto exit;
164 	}
165 
166 	sd = i2c_get_clientdata(pdata->subdev);
167 	rval = v4l2_device_register_subdev(&rsdev->v4l2_dev, sd);
168 	if (rval) {
169 		dev_err(&pdev->dev, "Cannot get v4l2 subdevice\n");
170 		goto unregister_v4l2_dev;
171 	}
172 
173 	rsdev->radio_dev = radio_si4713_vdev_template;
174 	rsdev->radio_dev.v4l2_dev = &rsdev->v4l2_dev;
175 	rsdev->radio_dev.ctrl_handler = sd->ctrl_handler;
176 	/* Serialize all access to the si4713 */
177 	rsdev->radio_dev.lock = &rsdev->lock;
178 	video_set_drvdata(&rsdev->radio_dev, rsdev);
179 	if (video_register_device(&rsdev->radio_dev, VFL_TYPE_RADIO, radio_nr)) {
180 		dev_err(&pdev->dev, "Could not register video device.\n");
181 		rval = -EIO;
182 		goto unregister_v4l2_dev;
183 	}
184 	dev_info(&pdev->dev, "New device successfully probed\n");
185 
186 	goto exit;
187 
188 unregister_v4l2_dev:
189 	v4l2_device_unregister(&rsdev->v4l2_dev);
190 exit:
191 	return rval;
192 }
193 
194 /* radio_si4713_pdriver_remove - remove the device */
195 static int radio_si4713_pdriver_remove(struct platform_device *pdev)
196 {
197 	struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
198 	struct radio_si4713_device *rsdev;
199 
200 	rsdev = container_of(v4l2_dev, struct radio_si4713_device, v4l2_dev);
201 	video_unregister_device(&rsdev->radio_dev);
202 	v4l2_device_unregister(&rsdev->v4l2_dev);
203 
204 	return 0;
205 }
206 
207 static struct platform_driver radio_si4713_pdriver = {
208 	.driver		= {
209 		.name	= "radio-si4713",
210 	},
211 	.probe		= radio_si4713_pdriver_probe,
212 	.remove         = radio_si4713_pdriver_remove,
213 };
214 
215 module_platform_driver(radio_si4713_pdriver);
216