2 * FM Driver for Connectivity chip of Texas Instruments.
3 * This file provides interfaces to V4L2 subsystem.
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.
9 * 1) File operation related API (open, close, read, write, poll...etc).
10 * 2) Set of V4L2 IOCTL complaint API.
12 * Copyright (C) 2011 Texas Instruments
13 * Author: Raja Mani <raja_mani@ti.com>
14 * Author: Manjunatha Halli <manjunatha_halli@ti.com>
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.
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.
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
31 #include <linux/export.h>
34 #include "fmdrv_v4l2.h"
35 #include "fmdrv_common.h"
39 static struct video_device
*gradio_dev
;
40 static u8 radio_disconnected
;
42 /* -- V4L2 RADIO (/dev/radioX) device file operation interfaces --- */
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
)
52 fmdev
= video_drvdata(file
);
54 if (!radio_disconnected
) {
55 fmerr("FM device is already disconnected\n");
59 /* Turn on RDS mode , if it is disabled */
60 ret
= fm_rx_get_rds_mode(fmdev
, &rds_mode
);
62 fmerr("Unable to read current rds mode\n");
66 if (rds_mode
== FM_RDS_DISABLE
) {
67 ret
= fmc_set_rds_mode(fmdev
, FM_RDS_ENABLE
);
69 fmerr("Failed to enable rds mode\n");
74 /* Copy RDS data from internal buffer to user buffer */
75 return fmc_transfer_rds_from_internal_buff(fmdev
, file
, buf
, count
);
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
)
86 ret
= copy_from_user(&rds
, buf
, sizeof(rds
));
87 fmdbg("(%d)type: %d, text %s, af %d\n",
88 ret
, rds
.text_type
, rds
.text
, rds
.af_freq
);
90 fmdev
= video_drvdata(file
);
91 fm_tx_set_radio_text(fmdev
, rds
.text
, rds
.text_type
);
92 fm_tx_set_af(fmdev
, rds
.af_freq
);
97 static u32
fm_v4l2_fops_poll(struct file
*file
, struct poll_table_struct
*pts
)
102 fmdev
= video_drvdata(file
);
103 ret
= fmc_is_rds_data_available(fmdev
, file
, pts
);
105 return POLLIN
| POLLRDNORM
;
111 * Handle open request for "/dev/radioX" device.
112 * Start with FM RX mode as default.
114 static int fm_v4l2_fops_open(struct file
*file
)
117 struct fmdev
*fmdev
= NULL
;
119 /* Don't allow multiple open */
120 if (radio_disconnected
) {
121 fmerr("FM device is already opened\n");
125 fmdev
= video_drvdata(file
);
127 ret
= fmc_prepare(fmdev
);
129 fmerr("Unable to prepare FM CORE\n");
133 fmdbg("Load FM RX firmware..\n");
135 ret
= fmc_set_mode(fmdev
, FM_MODE_RX
);
137 fmerr("Unable to load FM RX firmware\n");
140 radio_disconnected
= 1;
145 static int fm_v4l2_fops_release(struct file
*file
)
150 fmdev
= video_drvdata(file
);
151 if (!radio_disconnected
) {
152 fmdbg("FM device is already closed\n");
156 ret
= fmc_set_mode(fmdev
, FM_MODE_OFF
);
158 fmerr("Unable to turn off the chip\n");
162 ret
= fmc_release(fmdev
);
164 fmerr("FM CORE release failed\n");
167 radio_disconnected
= 0;
172 /* V4L2 RADIO (/dev/radioX) device IOCTL interfaces */
173 static int fm_v4l2_vidioc_querycap(struct file
*file
, void *priv
,
174 struct v4l2_capability
*capability
)
176 strlcpy(capability
->driver
, FM_DRV_NAME
, sizeof(capability
->driver
));
177 strlcpy(capability
->card
, FM_DRV_CARD_SHORT_NAME
,
178 sizeof(capability
->card
));
179 sprintf(capability
->bus_info
, "UART");
180 capability
->capabilities
= V4L2_CAP_HW_FREQ_SEEK
| V4L2_CAP_TUNER
|
181 V4L2_CAP_RADIO
| V4L2_CAP_MODULATOR
|
182 V4L2_CAP_AUDIO
| V4L2_CAP_READWRITE
|
183 V4L2_CAP_RDS_CAPTURE
;
188 static int fm_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
190 struct fmdev
*fmdev
= container_of(ctrl
->handler
,
191 struct fmdev
, ctrl_handler
);
194 case V4L2_CID_TUNE_ANTENNA_CAPACITOR
:
195 ctrl
->val
= fm_tx_get_tune_cap_val(fmdev
);
198 fmwarn("%s: Unknown IOCTL: %d\n", __func__
, ctrl
->id
);
205 static int fm_v4l2_s_ctrl(struct v4l2_ctrl
*ctrl
)
207 struct fmdev
*fmdev
= container_of(ctrl
->handler
,
208 struct fmdev
, ctrl_handler
);
211 case V4L2_CID_AUDIO_VOLUME
: /* set volume */
212 return fm_rx_set_volume(fmdev
, (u16
)ctrl
->val
);
214 case V4L2_CID_AUDIO_MUTE
: /* set mute */
215 return fmc_set_mute_mode(fmdev
, (u8
)ctrl
->val
);
217 case V4L2_CID_TUNE_POWER_LEVEL
:
218 /* set TX power level - ext control */
219 return fm_tx_set_pwr_lvl(fmdev
, (u8
)ctrl
->val
);
221 case V4L2_CID_TUNE_PREEMPHASIS
:
222 return fm_tx_set_preemph_filter(fmdev
, (u8
) ctrl
->val
);
229 static int fm_v4l2_vidioc_g_audio(struct file
*file
, void *priv
,
230 struct v4l2_audio
*audio
)
232 memset(audio
, 0, sizeof(*audio
));
233 strcpy(audio
->name
, "Radio");
234 audio
->capability
= V4L2_AUDCAP_STEREO
;
239 static int fm_v4l2_vidioc_s_audio(struct file
*file
, void *priv
,
240 struct v4l2_audio
*audio
)
242 if (audio
->index
!= 0)
248 /* Get tuner attributes. If current mode is NOT RX, return error */
249 static int fm_v4l2_vidioc_g_tuner(struct file
*file
, void *priv
,
250 struct v4l2_tuner
*tuner
)
252 struct fmdev
*fmdev
= video_drvdata(file
);
255 u16 stereo_mono_mode
;
259 if (tuner
->index
!= 0)
262 if (fmdev
->curr_fmmode
!= FM_MODE_RX
)
265 ret
= fm_rx_get_band_freq_range(fmdev
, &bottom_freq
, &top_freq
);
269 ret
= fm_rx_get_stereo_mono(fmdev
, &stereo_mono_mode
);
273 ret
= fm_rx_get_rssi_level(fmdev
, &rssilvl
);
277 strcpy(tuner
->name
, "FM");
278 tuner
->type
= V4L2_TUNER_RADIO
;
279 /* Store rangelow and rangehigh freq in unit of 62.5 Hz */
280 tuner
->rangelow
= bottom_freq
* 16;
281 tuner
->rangehigh
= top_freq
* 16;
282 tuner
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
|
283 ((fmdev
->rx
.rds
.flag
== FM_RDS_ENABLE
) ? V4L2_TUNER_SUB_RDS
: 0);
284 tuner
->capability
= V4L2_TUNER_CAP_STEREO
| V4L2_TUNER_CAP_RDS
|
286 tuner
->audmode
= (stereo_mono_mode
?
287 V4L2_TUNER_MODE_MONO
: V4L2_TUNER_MODE_STEREO
);
290 * Actual rssi value lies in between -128 to +127.
291 * Convert this range from 0 to 255 by adding +128
296 * Return signal strength value should be within 0 to 65535.
297 * Find out correct signal radio by multiplying (65535/255) = 257
299 tuner
->signal
= rssilvl
* 257;
306 * Set tuner attributes. If current mode is NOT RX, set to RX.
307 * Currently, we set only audio mode (mono/stereo) and RDS state (on/off).
308 * Should we set other tuner attributes, too?
310 static int fm_v4l2_vidioc_s_tuner(struct file
*file
, void *priv
,
311 struct v4l2_tuner
*tuner
)
313 struct fmdev
*fmdev
= video_drvdata(file
);
318 if (tuner
->index
!= 0)
321 aud_mode
= (tuner
->audmode
== V4L2_TUNER_MODE_STEREO
) ?
322 FM_STEREO_MODE
: FM_MONO_MODE
;
323 rds_mode
= (tuner
->rxsubchans
& V4L2_TUNER_SUB_RDS
) ?
324 FM_RDS_ENABLE
: FM_RDS_DISABLE
;
326 if (fmdev
->curr_fmmode
!= FM_MODE_RX
) {
327 ret
= fmc_set_mode(fmdev
, FM_MODE_RX
);
329 fmerr("Failed to set RX mode\n");
334 ret
= fmc_set_stereo_mono(fmdev
, aud_mode
);
336 fmerr("Failed to set RX stereo/mono mode\n");
340 ret
= fmc_set_rds_mode(fmdev
, rds_mode
);
342 fmerr("Failed to set RX RDS mode\n");
347 /* Get tuner or modulator radio frequency */
348 static int fm_v4l2_vidioc_g_freq(struct file
*file
, void *priv
,
349 struct v4l2_frequency
*freq
)
351 struct fmdev
*fmdev
= video_drvdata(file
);
354 ret
= fmc_get_freq(fmdev
, &freq
->frequency
);
356 fmerr("Failed to get frequency\n");
360 /* Frequency unit of 62.5 Hz*/
361 freq
->frequency
= (u32
) freq
->frequency
* 16;
366 /* Set tuner or modulator radio frequency */
367 static int fm_v4l2_vidioc_s_freq(struct file
*file
, void *priv
,
368 struct v4l2_frequency
*freq
)
370 struct fmdev
*fmdev
= video_drvdata(file
);
373 * As V4L2_TUNER_CAP_LOW is set 1 user sends the frequency
374 * in units of 62.5 Hz.
376 freq
->frequency
= (u32
)(freq
->frequency
/ 16);
378 return fmc_set_freq(fmdev
, freq
->frequency
);
381 /* Set hardware frequency seek. If current mode is NOT RX, set it RX. */
382 static int fm_v4l2_vidioc_s_hw_freq_seek(struct file
*file
, void *priv
,
383 struct v4l2_hw_freq_seek
*seek
)
385 struct fmdev
*fmdev
= video_drvdata(file
);
388 if (fmdev
->curr_fmmode
!= FM_MODE_RX
) {
389 ret
= fmc_set_mode(fmdev
, FM_MODE_RX
);
391 fmerr("Failed to set RX mode\n");
396 ret
= fm_rx_seek(fmdev
, seek
->seek_upward
, seek
->wrap_around
,
399 fmerr("RX seek failed - %d\n", ret
);
403 /* Get modulator attributes. If mode is not TX, return no attributes. */
404 static int fm_v4l2_vidioc_g_modulator(struct file
*file
, void *priv
,
405 struct v4l2_modulator
*mod
)
407 struct fmdev
*fmdev
= video_drvdata(file
);;
412 if (fmdev
->curr_fmmode
!= FM_MODE_TX
)
415 mod
->txsubchans
= ((fmdev
->tx_data
.aud_mode
== FM_STEREO_MODE
) ?
416 V4L2_TUNER_SUB_STEREO
: V4L2_TUNER_SUB_MONO
) |
417 ((fmdev
->tx_data
.rds
.flag
== FM_RDS_ENABLE
) ?
418 V4L2_TUNER_SUB_RDS
: 0);
420 mod
->capability
= V4L2_TUNER_CAP_STEREO
| V4L2_TUNER_CAP_RDS
|
426 /* Set modulator attributes. If mode is not TX, set to TX. */
427 static int fm_v4l2_vidioc_s_modulator(struct file
*file
, void *priv
,
428 struct v4l2_modulator
*mod
)
430 struct fmdev
*fmdev
= video_drvdata(file
);
438 if (fmdev
->curr_fmmode
!= FM_MODE_TX
) {
439 ret
= fmc_set_mode(fmdev
, FM_MODE_TX
);
441 fmerr("Failed to set TX mode\n");
446 aud_mode
= (mod
->txsubchans
& V4L2_TUNER_SUB_STEREO
) ?
447 FM_STEREO_MODE
: FM_MONO_MODE
;
448 rds_mode
= (mod
->txsubchans
& V4L2_TUNER_SUB_RDS
) ?
449 FM_RDS_ENABLE
: FM_RDS_DISABLE
;
450 ret
= fm_tx_set_stereo_mono(fmdev
, aud_mode
);
452 fmerr("Failed to set mono/stereo mode for TX\n");
455 ret
= fm_tx_set_rds_mode(fmdev
, rds_mode
);
457 fmerr("Failed to set rds mode for TX\n");
462 static const struct v4l2_file_operations fm_drv_fops
= {
463 .owner
= THIS_MODULE
,
464 .read
= fm_v4l2_fops_read
,
465 .write
= fm_v4l2_fops_write
,
466 .poll
= fm_v4l2_fops_poll
,
467 .unlocked_ioctl
= video_ioctl2
,
468 .open
= fm_v4l2_fops_open
,
469 .release
= fm_v4l2_fops_release
,
472 static const struct v4l2_ctrl_ops fm_ctrl_ops
= {
473 .s_ctrl
= fm_v4l2_s_ctrl
,
474 .g_volatile_ctrl
= fm_g_volatile_ctrl
,
476 static const struct v4l2_ioctl_ops fm_drv_ioctl_ops
= {
477 .vidioc_querycap
= fm_v4l2_vidioc_querycap
,
478 .vidioc_g_audio
= fm_v4l2_vidioc_g_audio
,
479 .vidioc_s_audio
= fm_v4l2_vidioc_s_audio
,
480 .vidioc_g_tuner
= fm_v4l2_vidioc_g_tuner
,
481 .vidioc_s_tuner
= fm_v4l2_vidioc_s_tuner
,
482 .vidioc_g_frequency
= fm_v4l2_vidioc_g_freq
,
483 .vidioc_s_frequency
= fm_v4l2_vidioc_s_freq
,
484 .vidioc_s_hw_freq_seek
= fm_v4l2_vidioc_s_hw_freq_seek
,
485 .vidioc_g_modulator
= fm_v4l2_vidioc_g_modulator
,
486 .vidioc_s_modulator
= fm_v4l2_vidioc_s_modulator
489 /* V4L2 RADIO device parent structure */
490 static struct video_device fm_viddev_template
= {
491 .fops
= &fm_drv_fops
,
492 .ioctl_ops
= &fm_drv_ioctl_ops
,
494 .release
= video_device_release
,
497 int fm_v4l2_init_video_device(struct fmdev
*fmdev
, int radio_nr
)
499 struct v4l2_ctrl
*ctrl
;
502 /* Init mutex for core locking */
503 mutex_init(&fmdev
->mutex
);
505 /* Allocate new video device */
506 gradio_dev
= video_device_alloc();
507 if (NULL
== gradio_dev
) {
508 fmerr("Can't allocate video device\n");
512 /* Setup FM driver's V4L2 properties */
513 memcpy(gradio_dev
, &fm_viddev_template
, sizeof(fm_viddev_template
));
515 video_set_drvdata(gradio_dev
, fmdev
);
517 gradio_dev
->lock
= &fmdev
->mutex
;
519 /* Register with V4L2 subsystem as RADIO device */
520 if (video_register_device(gradio_dev
, VFL_TYPE_RADIO
, radio_nr
)) {
521 video_device_release(gradio_dev
);
522 fmerr("Could not register video device\n");
526 fmdev
->radio_dev
= gradio_dev
;
528 /* Register to v4l2 ctrl handler framework */
529 fmdev
->radio_dev
->ctrl_handler
= &fmdev
->ctrl_handler
;
531 ret
= v4l2_ctrl_handler_init(&fmdev
->ctrl_handler
, 5);
533 fmerr("(fmdev): Can't init ctrl handler\n");
534 v4l2_ctrl_handler_free(&fmdev
->ctrl_handler
);
539 * Following controls are handled by V4L2 control framework.
540 * Added in ascending ID order.
542 v4l2_ctrl_new_std(&fmdev
->ctrl_handler
, &fm_ctrl_ops
,
543 V4L2_CID_AUDIO_VOLUME
, FM_RX_VOLUME_MIN
,
544 FM_RX_VOLUME_MAX
, 1, FM_RX_VOLUME_MAX
);
546 v4l2_ctrl_new_std(&fmdev
->ctrl_handler
, &fm_ctrl_ops
,
547 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 1);
549 v4l2_ctrl_new_std_menu(&fmdev
->ctrl_handler
, &fm_ctrl_ops
,
550 V4L2_CID_TUNE_PREEMPHASIS
, V4L2_PREEMPHASIS_75_uS
,
551 0, V4L2_PREEMPHASIS_75_uS
);
553 v4l2_ctrl_new_std(&fmdev
->ctrl_handler
, &fm_ctrl_ops
,
554 V4L2_CID_TUNE_POWER_LEVEL
, FM_PWR_LVL_LOW
,
555 FM_PWR_LVL_HIGH
, 1, FM_PWR_LVL_HIGH
);
557 ctrl
= v4l2_ctrl_new_std(&fmdev
->ctrl_handler
, &fm_ctrl_ops
,
558 V4L2_CID_TUNE_ANTENNA_CAPACITOR
, 0,
562 ctrl
->is_volatile
= 1;
567 void *fm_v4l2_deinit_video_device(void)
572 fmdev
= video_get_drvdata(gradio_dev
);
574 /* Unregister to v4l2 ctrl handler framework*/
575 v4l2_ctrl_handler_free(&fmdev
->ctrl_handler
);
577 /* Unregister RADIO device from V4L2 subsystem */
578 video_unregister_device(gradio_dev
);