xref: /linux/drivers/media/radio/wl128x/fmdrv_v4l2.c (revision 4413e16d9d21673bb5048a2e542f1aaa00015c2e)
1 /*
2  *  FM Driver for Connectivity chip of Texas Instruments.
3  *  This file provides interfaces to V4L2 subsystem.
4  *
5  *  This module registers with V4L2 subsystem as Radio
6  *  data system interface (/dev/radio). During the registration,
7  *  it will expose two set of function pointers.
8  *
9  *    1) File operation related API (open, close, read, write, poll...etc).
10  *    2) Set of V4L2 IOCTL complaint API.
11  *
12  *  Copyright (C) 2011 Texas Instruments
13  *  Author: Raja Mani <raja_mani@ti.com>
14  *  Author: Manjunatha Halli <manjunatha_halli@ti.com>
15  *
16  *  This program is free software; you can redistribute it and/or modify
17  *  it under the terms of the GNU General Public License version 2 as
18  *  published by the Free Software Foundation.
19  *
20  *  This program is distributed in the hope that it will be useful,
21  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *  GNU General Public License for more details.
24  *
25  *  You should have received a copy of the GNU General Public License
26  *  along with this program; if not, write to the Free Software
27  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  *
29  */
30 
31 #include <linux/export.h>
32 
33 #include "fmdrv.h"
34 #include "fmdrv_v4l2.h"
35 #include "fmdrv_common.h"
36 #include "fmdrv_rx.h"
37 #include "fmdrv_tx.h"
38 
39 static struct video_device *gradio_dev;
40 static u8 radio_disconnected;
41 
42 /* -- V4L2 RADIO (/dev/radioX) device file operation interfaces --- */
43 
44 /* Read RX RDS data */
45 static ssize_t fm_v4l2_fops_read(struct file *file, char __user * buf,
46 					size_t count, loff_t *ppos)
47 {
48 	u8 rds_mode;
49 	int ret;
50 	struct fmdev *fmdev;
51 
52 	fmdev = video_drvdata(file);
53 
54 	if (!radio_disconnected) {
55 		fmerr("FM device is already disconnected\n");
56 		return -EIO;
57 	}
58 
59 	/* Turn on RDS mode , if it is disabled */
60 	ret = fm_rx_get_rds_mode(fmdev, &rds_mode);
61 	if (ret < 0) {
62 		fmerr("Unable to read current rds mode\n");
63 		return ret;
64 	}
65 
66 	if (rds_mode == FM_RDS_DISABLE) {
67 		ret = fmc_set_rds_mode(fmdev, FM_RDS_ENABLE);
68 		if (ret < 0) {
69 			fmerr("Failed to enable rds mode\n");
70 			return ret;
71 		}
72 	}
73 
74 	/* Copy RDS data from internal buffer to user buffer */
75 	return fmc_transfer_rds_from_internal_buff(fmdev, file, buf, count);
76 }
77 
78 /* Write TX RDS data */
79 static ssize_t fm_v4l2_fops_write(struct file *file, const char __user * buf,
80 		size_t count, loff_t *ppos)
81 {
82 	struct tx_rds rds;
83 	int ret;
84 	struct fmdev *fmdev;
85 
86 	ret = copy_from_user(&rds, buf, sizeof(rds));
87 	rds.text[sizeof(rds.text) - 1] = '\0';
88 	fmdbg("(%d)type: %d, text %s, af %d\n",
89 		   ret, rds.text_type, rds.text, rds.af_freq);
90 	if (ret)
91 		return -EFAULT;
92 
93 	fmdev = video_drvdata(file);
94 	fm_tx_set_radio_text(fmdev, rds.text, rds.text_type);
95 	fm_tx_set_af(fmdev, rds.af_freq);
96 
97 	return sizeof(rds);
98 }
99 
100 static u32 fm_v4l2_fops_poll(struct file *file, struct poll_table_struct *pts)
101 {
102 	int ret;
103 	struct fmdev *fmdev;
104 
105 	fmdev = video_drvdata(file);
106 	ret = fmc_is_rds_data_available(fmdev, file, pts);
107 	if (ret < 0)
108 		return POLLIN | POLLRDNORM;
109 
110 	return 0;
111 }
112 
113 /*
114  * Handle open request for "/dev/radioX" device.
115  * Start with FM RX mode as default.
116  */
117 static int fm_v4l2_fops_open(struct file *file)
118 {
119 	int ret;
120 	struct fmdev *fmdev = NULL;
121 
122 	/* Don't allow multiple open */
123 	if (radio_disconnected) {
124 		fmerr("FM device is already opened\n");
125 		return -EBUSY;
126 	}
127 
128 	fmdev = video_drvdata(file);
129 
130 	ret = fmc_prepare(fmdev);
131 	if (ret < 0) {
132 		fmerr("Unable to prepare FM CORE\n");
133 		return ret;
134 	}
135 
136 	fmdbg("Load FM RX firmware..\n");
137 
138 	ret = fmc_set_mode(fmdev, FM_MODE_RX);
139 	if (ret < 0) {
140 		fmerr("Unable to load FM RX firmware\n");
141 		return ret;
142 	}
143 	radio_disconnected = 1;
144 
145 	return ret;
146 }
147 
148 static int fm_v4l2_fops_release(struct file *file)
149 {
150 	int ret;
151 	struct fmdev *fmdev;
152 
153 	fmdev = video_drvdata(file);
154 	if (!radio_disconnected) {
155 		fmdbg("FM device is already closed\n");
156 		return 0;
157 	}
158 
159 	ret = fmc_set_mode(fmdev, FM_MODE_OFF);
160 	if (ret < 0) {
161 		fmerr("Unable to turn off the chip\n");
162 		return ret;
163 	}
164 
165 	ret = fmc_release(fmdev);
166 	if (ret < 0) {
167 		fmerr("FM CORE release failed\n");
168 		return ret;
169 	}
170 	radio_disconnected = 0;
171 
172 	return ret;
173 }
174 
175 /* V4L2 RADIO (/dev/radioX) device IOCTL interfaces */
176 static int fm_v4l2_vidioc_querycap(struct file *file, void *priv,
177 		struct v4l2_capability *capability)
178 {
179 	strlcpy(capability->driver, FM_DRV_NAME, sizeof(capability->driver));
180 	strlcpy(capability->card, FM_DRV_CARD_SHORT_NAME,
181 			sizeof(capability->card));
182 	sprintf(capability->bus_info, "UART");
183 	capability->capabilities = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_TUNER |
184 		V4L2_CAP_RADIO | V4L2_CAP_MODULATOR |
185 		V4L2_CAP_AUDIO | V4L2_CAP_READWRITE |
186 		V4L2_CAP_RDS_CAPTURE;
187 
188 	return 0;
189 }
190 
191 static int fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
192 {
193 	struct fmdev *fmdev = container_of(ctrl->handler,
194 			struct fmdev, ctrl_handler);
195 
196 	switch (ctrl->id) {
197 	case  V4L2_CID_TUNE_ANTENNA_CAPACITOR:
198 		ctrl->val = fm_tx_get_tune_cap_val(fmdev);
199 		break;
200 	default:
201 		fmwarn("%s: Unknown IOCTL: %d\n", __func__, ctrl->id);
202 		break;
203 	}
204 
205 	return 0;
206 }
207 
208 static int fm_v4l2_s_ctrl(struct v4l2_ctrl *ctrl)
209 {
210 	struct fmdev *fmdev = container_of(ctrl->handler,
211 			struct fmdev, ctrl_handler);
212 
213 	switch (ctrl->id) {
214 	case V4L2_CID_AUDIO_VOLUME:	/* set volume */
215 		return fm_rx_set_volume(fmdev, (u16)ctrl->val);
216 
217 	case V4L2_CID_AUDIO_MUTE:	/* set mute */
218 		return fmc_set_mute_mode(fmdev, (u8)ctrl->val);
219 
220 	case V4L2_CID_TUNE_POWER_LEVEL:
221 		/* set TX power level - ext control */
222 		return fm_tx_set_pwr_lvl(fmdev, (u8)ctrl->val);
223 
224 	case V4L2_CID_TUNE_PREEMPHASIS:
225 		return fm_tx_set_preemph_filter(fmdev, (u8) ctrl->val);
226 
227 	default:
228 		return -EINVAL;
229 	}
230 }
231 
232 static int fm_v4l2_vidioc_g_audio(struct file *file, void *priv,
233 		struct v4l2_audio *audio)
234 {
235 	memset(audio, 0, sizeof(*audio));
236 	strcpy(audio->name, "Radio");
237 	audio->capability = V4L2_AUDCAP_STEREO;
238 
239 	return 0;
240 }
241 
242 static int fm_v4l2_vidioc_s_audio(struct file *file, void *priv,
243 		struct v4l2_audio *audio)
244 {
245 	if (audio->index != 0)
246 		return -EINVAL;
247 
248 	return 0;
249 }
250 
251 /* Get tuner attributes. If current mode is NOT RX, return error */
252 static int fm_v4l2_vidioc_g_tuner(struct file *file, void *priv,
253 		struct v4l2_tuner *tuner)
254 {
255 	struct fmdev *fmdev = video_drvdata(file);
256 	u32 bottom_freq;
257 	u32 top_freq;
258 	u16 stereo_mono_mode;
259 	u16 rssilvl;
260 	int ret;
261 
262 	if (tuner->index != 0)
263 		return -EINVAL;
264 
265 	if (fmdev->curr_fmmode != FM_MODE_RX)
266 		return -EPERM;
267 
268 	ret = fm_rx_get_band_freq_range(fmdev, &bottom_freq, &top_freq);
269 	if (ret != 0)
270 		return ret;
271 
272 	ret = fm_rx_get_stereo_mono(fmdev, &stereo_mono_mode);
273 	if (ret != 0)
274 		return ret;
275 
276 	ret = fm_rx_get_rssi_level(fmdev, &rssilvl);
277 	if (ret != 0)
278 		return ret;
279 
280 	strcpy(tuner->name, "FM");
281 	tuner->type = V4L2_TUNER_RADIO;
282 	/* Store rangelow and rangehigh freq in unit of 62.5 Hz */
283 	tuner->rangelow = bottom_freq * 16;
284 	tuner->rangehigh = top_freq * 16;
285 	tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO |
286 	((fmdev->rx.rds.flag == FM_RDS_ENABLE) ? V4L2_TUNER_SUB_RDS : 0);
287 	tuner->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
288 			    V4L2_TUNER_CAP_LOW |
289 			    V4L2_TUNER_CAP_HWSEEK_BOUNDED |
290 			    V4L2_TUNER_CAP_HWSEEK_WRAP;
291 	tuner->audmode = (stereo_mono_mode ?
292 			  V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO);
293 
294 	/*
295 	 * Actual rssi value lies in between -128 to +127.
296 	 * Convert this range from 0 to 255 by adding +128
297 	 */
298 	rssilvl += 128;
299 
300 	/*
301 	 * Return signal strength value should be within 0 to 65535.
302 	 * Find out correct signal radio by multiplying (65535/255) = 257
303 	 */
304 	tuner->signal = rssilvl * 257;
305 	tuner->afc = 0;
306 
307 	return ret;
308 }
309 
310 /*
311  * Set tuner attributes. If current mode is NOT RX, set to RX.
312  * Currently, we set only audio mode (mono/stereo) and RDS state (on/off).
313  * Should we set other tuner attributes, too?
314  */
315 static int fm_v4l2_vidioc_s_tuner(struct file *file, void *priv,
316 		struct v4l2_tuner *tuner)
317 {
318 	struct fmdev *fmdev = video_drvdata(file);
319 	u16 aud_mode;
320 	u8 rds_mode;
321 	int ret;
322 
323 	if (tuner->index != 0)
324 		return -EINVAL;
325 
326 	aud_mode = (tuner->audmode == V4L2_TUNER_MODE_STEREO) ?
327 			FM_STEREO_MODE : FM_MONO_MODE;
328 	rds_mode = (tuner->rxsubchans & V4L2_TUNER_SUB_RDS) ?
329 			FM_RDS_ENABLE : FM_RDS_DISABLE;
330 
331 	if (fmdev->curr_fmmode != FM_MODE_RX) {
332 		ret = fmc_set_mode(fmdev, FM_MODE_RX);
333 		if (ret < 0) {
334 			fmerr("Failed to set RX mode\n");
335 			return ret;
336 		}
337 	}
338 
339 	ret = fmc_set_stereo_mono(fmdev, aud_mode);
340 	if (ret < 0) {
341 		fmerr("Failed to set RX stereo/mono mode\n");
342 		return ret;
343 	}
344 
345 	ret = fmc_set_rds_mode(fmdev, rds_mode);
346 	if (ret < 0)
347 		fmerr("Failed to set RX RDS mode\n");
348 
349 	return ret;
350 }
351 
352 /* Get tuner or modulator radio frequency */
353 static int fm_v4l2_vidioc_g_freq(struct file *file, void *priv,
354 		struct v4l2_frequency *freq)
355 {
356 	struct fmdev *fmdev = video_drvdata(file);
357 	int ret;
358 
359 	ret = fmc_get_freq(fmdev, &freq->frequency);
360 	if (ret < 0) {
361 		fmerr("Failed to get frequency\n");
362 		return ret;
363 	}
364 
365 	/* Frequency unit of 62.5 Hz*/
366 	freq->frequency = (u32) freq->frequency * 16;
367 
368 	return 0;
369 }
370 
371 /* Set tuner or modulator radio frequency */
372 static int fm_v4l2_vidioc_s_freq(struct file *file, void *priv,
373 		struct v4l2_frequency *freq)
374 {
375 	struct fmdev *fmdev = video_drvdata(file);
376 
377 	/*
378 	 * As V4L2_TUNER_CAP_LOW is set 1 user sends the frequency
379 	 * in units of 62.5 Hz.
380 	 */
381 	freq->frequency = (u32)(freq->frequency / 16);
382 
383 	return fmc_set_freq(fmdev, freq->frequency);
384 }
385 
386 /* Set hardware frequency seek. If current mode is NOT RX, set it RX. */
387 static int fm_v4l2_vidioc_s_hw_freq_seek(struct file *file, void *priv,
388 		struct v4l2_hw_freq_seek *seek)
389 {
390 	struct fmdev *fmdev = video_drvdata(file);
391 	int ret;
392 
393 	if (fmdev->curr_fmmode != FM_MODE_RX) {
394 		ret = fmc_set_mode(fmdev, FM_MODE_RX);
395 		if (ret != 0) {
396 			fmerr("Failed to set RX mode\n");
397 			return ret;
398 		}
399 	}
400 
401 	ret = fm_rx_seek(fmdev, seek->seek_upward, seek->wrap_around,
402 			seek->spacing);
403 	if (ret < 0)
404 		fmerr("RX seek failed - %d\n", ret);
405 
406 	return ret;
407 }
408 /* Get modulator attributes. If mode is not TX, return no attributes. */
409 static int fm_v4l2_vidioc_g_modulator(struct file *file, void *priv,
410 		struct v4l2_modulator *mod)
411 {
412 	struct fmdev *fmdev = video_drvdata(file);
413 
414 	if (mod->index != 0)
415 		return -EINVAL;
416 
417 	if (fmdev->curr_fmmode != FM_MODE_TX)
418 		return -EPERM;
419 
420 	mod->txsubchans = ((fmdev->tx_data.aud_mode == FM_STEREO_MODE) ?
421 				V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO) |
422 				((fmdev->tx_data.rds.flag == FM_RDS_ENABLE) ?
423 				V4L2_TUNER_SUB_RDS : 0);
424 
425 	mod->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
426 				V4L2_TUNER_CAP_LOW;
427 
428 	return 0;
429 }
430 
431 /* Set modulator attributes. If mode is not TX, set to TX. */
432 static int fm_v4l2_vidioc_s_modulator(struct file *file, void *priv,
433 		struct v4l2_modulator *mod)
434 {
435 	struct fmdev *fmdev = video_drvdata(file);
436 	u8 rds_mode;
437 	u16 aud_mode;
438 	int ret;
439 
440 	if (mod->index != 0)
441 		return -EINVAL;
442 
443 	if (fmdev->curr_fmmode != FM_MODE_TX) {
444 		ret = fmc_set_mode(fmdev, FM_MODE_TX);
445 		if (ret != 0) {
446 			fmerr("Failed to set TX mode\n");
447 			return ret;
448 		}
449 	}
450 
451 	aud_mode = (mod->txsubchans & V4L2_TUNER_SUB_STEREO) ?
452 			FM_STEREO_MODE : FM_MONO_MODE;
453 	rds_mode = (mod->txsubchans & V4L2_TUNER_SUB_RDS) ?
454 			FM_RDS_ENABLE : FM_RDS_DISABLE;
455 	ret = fm_tx_set_stereo_mono(fmdev, aud_mode);
456 	if (ret < 0) {
457 		fmerr("Failed to set mono/stereo mode for TX\n");
458 		return ret;
459 	}
460 	ret = fm_tx_set_rds_mode(fmdev, rds_mode);
461 	if (ret < 0)
462 		fmerr("Failed to set rds mode for TX\n");
463 
464 	return ret;
465 }
466 
467 static const struct v4l2_file_operations fm_drv_fops = {
468 	.owner = THIS_MODULE,
469 	.read = fm_v4l2_fops_read,
470 	.write = fm_v4l2_fops_write,
471 	.poll = fm_v4l2_fops_poll,
472 	.unlocked_ioctl = video_ioctl2,
473 	.open = fm_v4l2_fops_open,
474 	.release = fm_v4l2_fops_release,
475 };
476 
477 static const struct v4l2_ctrl_ops fm_ctrl_ops = {
478 	.s_ctrl = fm_v4l2_s_ctrl,
479 	.g_volatile_ctrl = fm_g_volatile_ctrl,
480 };
481 static const struct v4l2_ioctl_ops fm_drv_ioctl_ops = {
482 	.vidioc_querycap = fm_v4l2_vidioc_querycap,
483 	.vidioc_g_audio = fm_v4l2_vidioc_g_audio,
484 	.vidioc_s_audio = fm_v4l2_vidioc_s_audio,
485 	.vidioc_g_tuner = fm_v4l2_vidioc_g_tuner,
486 	.vidioc_s_tuner = fm_v4l2_vidioc_s_tuner,
487 	.vidioc_g_frequency = fm_v4l2_vidioc_g_freq,
488 	.vidioc_s_frequency = fm_v4l2_vidioc_s_freq,
489 	.vidioc_s_hw_freq_seek = fm_v4l2_vidioc_s_hw_freq_seek,
490 	.vidioc_g_modulator = fm_v4l2_vidioc_g_modulator,
491 	.vidioc_s_modulator = fm_v4l2_vidioc_s_modulator
492 };
493 
494 /* V4L2 RADIO device parent structure */
495 static struct video_device fm_viddev_template = {
496 	.fops = &fm_drv_fops,
497 	.ioctl_ops = &fm_drv_ioctl_ops,
498 	.name = FM_DRV_NAME,
499 	.release = video_device_release,
500 };
501 
502 int fm_v4l2_init_video_device(struct fmdev *fmdev, int radio_nr)
503 {
504 	struct v4l2_ctrl *ctrl;
505 	int ret;
506 
507 	/* Init mutex for core locking */
508 	mutex_init(&fmdev->mutex);
509 
510 	/* Allocate new video device */
511 	gradio_dev = video_device_alloc();
512 	if (NULL == gradio_dev) {
513 		fmerr("Can't allocate video device\n");
514 		return -ENOMEM;
515 	}
516 
517 	/* Setup FM driver's V4L2 properties */
518 	memcpy(gradio_dev, &fm_viddev_template, sizeof(fm_viddev_template));
519 
520 	video_set_drvdata(gradio_dev, fmdev);
521 
522 	gradio_dev->lock = &fmdev->mutex;
523 	/* Locking in file operations other than ioctl should be done
524 	   by the driver, not the V4L2 core.
525 	   This driver needs auditing so that this flag can be removed. */
526 	set_bit(V4L2_FL_LOCK_ALL_FOPS, &gradio_dev->flags);
527 
528 	/* Register with V4L2 subsystem as RADIO device */
529 	if (video_register_device(gradio_dev, VFL_TYPE_RADIO, radio_nr)) {
530 		video_device_release(gradio_dev);
531 		fmerr("Could not register video device\n");
532 		return -ENOMEM;
533 	}
534 
535 	fmdev->radio_dev = gradio_dev;
536 
537 	/* Register to v4l2 ctrl handler framework */
538 	fmdev->radio_dev->ctrl_handler = &fmdev->ctrl_handler;
539 
540 	ret = v4l2_ctrl_handler_init(&fmdev->ctrl_handler, 5);
541 	if (ret < 0) {
542 		fmerr("(fmdev): Can't init ctrl handler\n");
543 		v4l2_ctrl_handler_free(&fmdev->ctrl_handler);
544 		return -EBUSY;
545 	}
546 
547 	/*
548 	 * Following controls are handled by V4L2 control framework.
549 	 * Added in ascending ID order.
550 	 */
551 	v4l2_ctrl_new_std(&fmdev->ctrl_handler, &fm_ctrl_ops,
552 			V4L2_CID_AUDIO_VOLUME, FM_RX_VOLUME_MIN,
553 			FM_RX_VOLUME_MAX, 1, FM_RX_VOLUME_MAX);
554 
555 	v4l2_ctrl_new_std(&fmdev->ctrl_handler, &fm_ctrl_ops,
556 			V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
557 
558 	v4l2_ctrl_new_std_menu(&fmdev->ctrl_handler, &fm_ctrl_ops,
559 			V4L2_CID_TUNE_PREEMPHASIS, V4L2_PREEMPHASIS_75_uS,
560 			0, V4L2_PREEMPHASIS_75_uS);
561 
562 	v4l2_ctrl_new_std(&fmdev->ctrl_handler, &fm_ctrl_ops,
563 			V4L2_CID_TUNE_POWER_LEVEL, FM_PWR_LVL_LOW,
564 			FM_PWR_LVL_HIGH, 1, FM_PWR_LVL_HIGH);
565 
566 	ctrl = v4l2_ctrl_new_std(&fmdev->ctrl_handler, &fm_ctrl_ops,
567 			V4L2_CID_TUNE_ANTENNA_CAPACITOR, 0,
568 			255, 1, 255);
569 
570 	if (ctrl)
571 		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
572 
573 	return 0;
574 }
575 
576 void *fm_v4l2_deinit_video_device(void)
577 {
578 	struct fmdev *fmdev;
579 
580 
581 	fmdev = video_get_drvdata(gradio_dev);
582 
583 	/* Unregister to v4l2 ctrl handler framework*/
584 	v4l2_ctrl_handler_free(&fmdev->ctrl_handler);
585 
586 	/* Unregister RADIO device from V4L2 subsystem */
587 	video_unregister_device(gradio_dev);
588 
589 	return fmdev;
590 }
591