xref: /openbmc/linux/drivers/media/radio/radio-isa.c (revision cd238eff)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Framework for ISA radio drivers.
4  * This takes care of all the V4L2 scaffolding, allowing the ISA drivers
5  * to concentrate on the actual hardware operation.
6  *
7  * Copyright (C) 2012 Hans Verkuil <hans.verkuil@cisco.com>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/ioport.h>
13 #include <linux/delay.h>
14 #include <linux/videodev2.h>
15 #include <linux/io.h>
16 #include <linux/slab.h>
17 #include <media/v4l2-device.h>
18 #include <media/v4l2-ioctl.h>
19 #include <media/v4l2-fh.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-event.h>
22 
23 #include "radio-isa.h"
24 
25 MODULE_AUTHOR("Hans Verkuil");
26 MODULE_DESCRIPTION("A framework for ISA radio drivers.");
27 MODULE_LICENSE("GPL");
28 
29 #define FREQ_LOW  (87U * 16000U)
30 #define FREQ_HIGH (108U * 16000U)
31 
32 static int radio_isa_querycap(struct file *file, void  *priv,
33 					struct v4l2_capability *v)
34 {
35 	struct radio_isa_card *isa = video_drvdata(file);
36 
37 	strscpy(v->driver, isa->drv->driver.driver.name, sizeof(v->driver));
38 	strscpy(v->card, isa->drv->card, sizeof(v->card));
39 	snprintf(v->bus_info, sizeof(v->bus_info), "ISA:%s", isa->v4l2_dev.name);
40 
41 	v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
42 	v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
43 	return 0;
44 }
45 
46 static int radio_isa_g_tuner(struct file *file, void *priv,
47 				struct v4l2_tuner *v)
48 {
49 	struct radio_isa_card *isa = video_drvdata(file);
50 	const struct radio_isa_ops *ops = isa->drv->ops;
51 
52 	if (v->index > 0)
53 		return -EINVAL;
54 
55 	strscpy(v->name, "FM", sizeof(v->name));
56 	v->type = V4L2_TUNER_RADIO;
57 	v->rangelow = FREQ_LOW;
58 	v->rangehigh = FREQ_HIGH;
59 	v->capability = V4L2_TUNER_CAP_LOW;
60 	if (isa->drv->has_stereo)
61 		v->capability |= V4L2_TUNER_CAP_STEREO;
62 
63 	if (ops->g_rxsubchans)
64 		v->rxsubchans = ops->g_rxsubchans(isa);
65 	else
66 		v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
67 	v->audmode = isa->stereo ? V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
68 	if (ops->g_signal)
69 		v->signal = ops->g_signal(isa);
70 	else
71 		v->signal = (v->rxsubchans & V4L2_TUNER_SUB_STEREO) ?
72 								0xffff : 0;
73 	return 0;
74 }
75 
76 static int radio_isa_s_tuner(struct file *file, void *priv,
77 				const struct v4l2_tuner *v)
78 {
79 	struct radio_isa_card *isa = video_drvdata(file);
80 	const struct radio_isa_ops *ops = isa->drv->ops;
81 
82 	if (v->index)
83 		return -EINVAL;
84 	if (ops->s_stereo) {
85 		isa->stereo = (v->audmode == V4L2_TUNER_MODE_STEREO);
86 		return ops->s_stereo(isa, isa->stereo);
87 	}
88 	return 0;
89 }
90 
91 static int radio_isa_s_frequency(struct file *file, void *priv,
92 				const struct v4l2_frequency *f)
93 {
94 	struct radio_isa_card *isa = video_drvdata(file);
95 	u32 freq = f->frequency;
96 	int res;
97 
98 	if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
99 		return -EINVAL;
100 	freq = clamp(freq, FREQ_LOW, FREQ_HIGH);
101 	res = isa->drv->ops->s_frequency(isa, freq);
102 	if (res == 0)
103 		isa->freq = freq;
104 	return res;
105 }
106 
107 static int radio_isa_g_frequency(struct file *file, void *priv,
108 				struct v4l2_frequency *f)
109 {
110 	struct radio_isa_card *isa = video_drvdata(file);
111 
112 	if (f->tuner != 0)
113 		return -EINVAL;
114 	f->type = V4L2_TUNER_RADIO;
115 	f->frequency = isa->freq;
116 	return 0;
117 }
118 
119 static int radio_isa_s_ctrl(struct v4l2_ctrl *ctrl)
120 {
121 	struct radio_isa_card *isa =
122 		container_of(ctrl->handler, struct radio_isa_card, hdl);
123 
124 	switch (ctrl->id) {
125 	case V4L2_CID_AUDIO_MUTE:
126 		return isa->drv->ops->s_mute_volume(isa, ctrl->val,
127 				isa->volume ? isa->volume->val : 0);
128 	}
129 	return -EINVAL;
130 }
131 
132 static int radio_isa_log_status(struct file *file, void *priv)
133 {
134 	struct radio_isa_card *isa = video_drvdata(file);
135 
136 	v4l2_info(&isa->v4l2_dev, "I/O Port = 0x%03x\n", isa->io);
137 	v4l2_ctrl_handler_log_status(&isa->hdl, isa->v4l2_dev.name);
138 	return 0;
139 }
140 
141 static const struct v4l2_ctrl_ops radio_isa_ctrl_ops = {
142 	.s_ctrl = radio_isa_s_ctrl,
143 };
144 
145 static const struct v4l2_file_operations radio_isa_fops = {
146 	.owner		= THIS_MODULE,
147 	.open		= v4l2_fh_open,
148 	.release	= v4l2_fh_release,
149 	.poll		= v4l2_ctrl_poll,
150 	.unlocked_ioctl	= video_ioctl2,
151 };
152 
153 static const struct v4l2_ioctl_ops radio_isa_ioctl_ops = {
154 	.vidioc_querycap    = radio_isa_querycap,
155 	.vidioc_g_tuner     = radio_isa_g_tuner,
156 	.vidioc_s_tuner     = radio_isa_s_tuner,
157 	.vidioc_g_frequency = radio_isa_g_frequency,
158 	.vidioc_s_frequency = radio_isa_s_frequency,
159 	.vidioc_log_status  = radio_isa_log_status,
160 	.vidioc_subscribe_event   = v4l2_ctrl_subscribe_event,
161 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
162 };
163 
164 int radio_isa_match(struct device *pdev, unsigned int dev)
165 {
166 	struct radio_isa_driver *drv = pdev->platform_data;
167 
168 	return drv->probe || drv->io_params[dev] >= 0;
169 }
170 EXPORT_SYMBOL_GPL(radio_isa_match);
171 
172 static bool radio_isa_valid_io(const struct radio_isa_driver *drv, int io)
173 {
174 	int i;
175 
176 	for (i = 0; i < drv->num_of_io_ports; i++)
177 		if (drv->io_ports[i] == io)
178 			return true;
179 	return false;
180 }
181 
182 static struct radio_isa_card *radio_isa_alloc(struct radio_isa_driver *drv,
183 				struct device *pdev)
184 {
185 	struct v4l2_device *v4l2_dev;
186 	struct radio_isa_card *isa = drv->ops->alloc();
187 	if (!isa)
188 		return NULL;
189 
190 	dev_set_drvdata(pdev, isa);
191 	isa->drv = drv;
192 	v4l2_dev = &isa->v4l2_dev;
193 	strscpy(v4l2_dev->name, dev_name(pdev), sizeof(v4l2_dev->name));
194 
195 	return isa;
196 }
197 
198 static int radio_isa_common_probe(struct radio_isa_card *isa,
199 				  struct device *pdev,
200 				  int radio_nr, unsigned region_size)
201 {
202 	const struct radio_isa_driver *drv = isa->drv;
203 	const struct radio_isa_ops *ops = drv->ops;
204 	struct v4l2_device *v4l2_dev = &isa->v4l2_dev;
205 	int res;
206 
207 	if (!request_region(isa->io, region_size, v4l2_dev->name)) {
208 		v4l2_err(v4l2_dev, "port 0x%x already in use\n", isa->io);
209 		kfree(isa);
210 		return -EBUSY;
211 	}
212 
213 	res = v4l2_device_register(pdev, v4l2_dev);
214 	if (res < 0) {
215 		v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
216 		goto err_dev_reg;
217 	}
218 
219 	v4l2_ctrl_handler_init(&isa->hdl, 1);
220 	isa->mute = v4l2_ctrl_new_std(&isa->hdl, &radio_isa_ctrl_ops,
221 				V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
222 	if (drv->max_volume)
223 		isa->volume = v4l2_ctrl_new_std(&isa->hdl, &radio_isa_ctrl_ops,
224 			V4L2_CID_AUDIO_VOLUME, 0, drv->max_volume, 1,
225 			drv->max_volume);
226 	v4l2_dev->ctrl_handler = &isa->hdl;
227 	if (isa->hdl.error) {
228 		res = isa->hdl.error;
229 		v4l2_err(v4l2_dev, "Could not register controls\n");
230 		goto err_hdl;
231 	}
232 	if (drv->max_volume)
233 		v4l2_ctrl_cluster(2, &isa->mute);
234 	v4l2_dev->ctrl_handler = &isa->hdl;
235 
236 	mutex_init(&isa->lock);
237 	isa->vdev.lock = &isa->lock;
238 	strscpy(isa->vdev.name, v4l2_dev->name, sizeof(isa->vdev.name));
239 	isa->vdev.v4l2_dev = v4l2_dev;
240 	isa->vdev.fops = &radio_isa_fops;
241 	isa->vdev.ioctl_ops = &radio_isa_ioctl_ops;
242 	isa->vdev.release = video_device_release_empty;
243 	video_set_drvdata(&isa->vdev, isa);
244 	isa->freq = FREQ_LOW;
245 	isa->stereo = drv->has_stereo;
246 
247 	if (ops->init)
248 		res = ops->init(isa);
249 	if (!res)
250 		res = v4l2_ctrl_handler_setup(&isa->hdl);
251 	if (!res)
252 		res = ops->s_frequency(isa, isa->freq);
253 	if (!res && ops->s_stereo)
254 		res = ops->s_stereo(isa, isa->stereo);
255 	if (res < 0) {
256 		v4l2_err(v4l2_dev, "Could not setup card\n");
257 		goto err_hdl;
258 	}
259 	res = video_register_device(&isa->vdev, VFL_TYPE_RADIO, radio_nr);
260 
261 	if (res < 0) {
262 		v4l2_err(v4l2_dev, "Could not register device node\n");
263 		goto err_hdl;
264 	}
265 
266 	v4l2_info(v4l2_dev, "Initialized radio card %s on port 0x%03x\n",
267 			drv->card, isa->io);
268 	return 0;
269 
270 err_hdl:
271 	v4l2_ctrl_handler_free(&isa->hdl);
272 err_dev_reg:
273 	release_region(isa->io, region_size);
274 	kfree(isa);
275 	return res;
276 }
277 
278 static int radio_isa_common_remove(struct radio_isa_card *isa,
279 				   unsigned region_size)
280 {
281 	const struct radio_isa_ops *ops = isa->drv->ops;
282 
283 	ops->s_mute_volume(isa, true, isa->volume ? isa->volume->cur.val : 0);
284 	video_unregister_device(&isa->vdev);
285 	v4l2_ctrl_handler_free(&isa->hdl);
286 	v4l2_device_unregister(&isa->v4l2_dev);
287 	release_region(isa->io, region_size);
288 	v4l2_info(&isa->v4l2_dev, "Removed radio card %s\n", isa->drv->card);
289 	kfree(isa);
290 	return 0;
291 }
292 
293 int radio_isa_probe(struct device *pdev, unsigned int dev)
294 {
295 	struct radio_isa_driver *drv = pdev->platform_data;
296 	const struct radio_isa_ops *ops = drv->ops;
297 	struct v4l2_device *v4l2_dev;
298 	struct radio_isa_card *isa;
299 
300 	isa = radio_isa_alloc(drv, pdev);
301 	if (!isa)
302 		return -ENOMEM;
303 	isa->io = drv->io_params[dev];
304 	v4l2_dev = &isa->v4l2_dev;
305 
306 	if (drv->probe && ops->probe) {
307 		int i;
308 
309 		for (i = 0; i < drv->num_of_io_ports; ++i) {
310 			int io = drv->io_ports[i];
311 
312 			if (request_region(io, drv->region_size, v4l2_dev->name)) {
313 				bool found = ops->probe(isa, io);
314 
315 				release_region(io, drv->region_size);
316 				if (found) {
317 					isa->io = io;
318 					break;
319 				}
320 			}
321 		}
322 	}
323 
324 	if (!radio_isa_valid_io(drv, isa->io)) {
325 		int i;
326 
327 		if (isa->io < 0)
328 			return -ENODEV;
329 		v4l2_err(v4l2_dev, "you must set an I/O address with io=0x%03x",
330 				drv->io_ports[0]);
331 		for (i = 1; i < drv->num_of_io_ports; i++)
332 			printk(KERN_CONT "/0x%03x", drv->io_ports[i]);
333 		printk(KERN_CONT ".\n");
334 		kfree(isa);
335 		return -EINVAL;
336 	}
337 
338 	return radio_isa_common_probe(isa, pdev, drv->radio_nr_params[dev],
339 					drv->region_size);
340 }
341 EXPORT_SYMBOL_GPL(radio_isa_probe);
342 
343 int radio_isa_remove(struct device *pdev, unsigned int dev)
344 {
345 	struct radio_isa_card *isa = dev_get_drvdata(pdev);
346 
347 	return radio_isa_common_remove(isa, isa->drv->region_size);
348 }
349 EXPORT_SYMBOL_GPL(radio_isa_remove);
350 
351 #ifdef CONFIG_PNP
352 int radio_isa_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
353 {
354 	struct pnp_driver *pnp_drv = to_pnp_driver(dev->dev.driver);
355 	struct radio_isa_driver *drv = container_of(pnp_drv,
356 					struct radio_isa_driver, pnp_driver);
357 	struct radio_isa_card *isa;
358 
359 	if (!pnp_port_valid(dev, 0))
360 		return -ENODEV;
361 
362 	isa = radio_isa_alloc(drv, &dev->dev);
363 	if (!isa)
364 		return -ENOMEM;
365 
366 	isa->io = pnp_port_start(dev, 0);
367 
368 	return radio_isa_common_probe(isa, &dev->dev, drv->radio_nr_params[0],
369 					pnp_port_len(dev, 0));
370 }
371 EXPORT_SYMBOL_GPL(radio_isa_pnp_probe);
372 
373 void radio_isa_pnp_remove(struct pnp_dev *dev)
374 {
375 	struct radio_isa_card *isa = dev_get_drvdata(&dev->dev);
376 
377 	radio_isa_common_remove(isa, pnp_port_len(dev, 0));
378 }
379 EXPORT_SYMBOL_GPL(radio_isa_pnp_remove);
380 #endif
381