Merge branch 'modsplit-Oct31_2011' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / radio / wl128x / fmdrv_v4l2.c
blob4f5c43d2566c887444a91329b64a3960229e5278
1 /*
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>
33 #include "fmdrv.h"
34 #include "fmdrv_v4l2.h"
35 #include "fmdrv_common.h"
36 #include "fmdrv_rx.h"
37 #include "fmdrv_tx.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)
48 u8 rds_mode;
49 int ret;
50 struct fmdev *fmdev;
52 fmdev = video_drvdata(file);
54 if (!radio_disconnected) {
55 fmerr("FM device is already disconnected\n");
56 return -EIO;
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;
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;
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)
82 struct tx_rds rds;
83 int ret;
84 struct fmdev *fmdev;
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);
89 if (ret)
90 return -EFAULT;
92 fmdev = video_drvdata(file);
93 fm_tx_set_radio_text(fmdev, rds.text, rds.text_type);
94 fm_tx_set_af(fmdev, rds.af_freq);
96 return sizeof(rds);
99 static u32 fm_v4l2_fops_poll(struct file *file, struct poll_table_struct *pts)
101 int ret;
102 struct fmdev *fmdev;
104 fmdev = video_drvdata(file);
105 ret = fmc_is_rds_data_available(fmdev, file, pts);
106 if (ret < 0)
107 return POLLIN | POLLRDNORM;
109 return 0;
113 * Handle open request for "/dev/radioX" device.
114 * Start with FM RX mode as default.
116 static int fm_v4l2_fops_open(struct file *file)
118 int ret;
119 struct fmdev *fmdev = NULL;
121 /* Don't allow multiple open */
122 if (radio_disconnected) {
123 fmerr("FM device is already opened\n");
124 return -EBUSY;
127 fmdev = video_drvdata(file);
129 ret = fmc_prepare(fmdev);
130 if (ret < 0) {
131 fmerr("Unable to prepare FM CORE\n");
132 return ret;
135 fmdbg("Load FM RX firmware..\n");
137 ret = fmc_set_mode(fmdev, FM_MODE_RX);
138 if (ret < 0) {
139 fmerr("Unable to load FM RX firmware\n");
140 return ret;
142 radio_disconnected = 1;
144 return ret;
147 static int fm_v4l2_fops_release(struct file *file)
149 int ret;
150 struct fmdev *fmdev;
152 fmdev = video_drvdata(file);
153 if (!radio_disconnected) {
154 fmdbg("FM device is already closed\n");
155 return 0;
158 ret = fmc_set_mode(fmdev, FM_MODE_OFF);
159 if (ret < 0) {
160 fmerr("Unable to turn off the chip\n");
161 return ret;
164 ret = fmc_release(fmdev);
165 if (ret < 0) {
166 fmerr("FM CORE release failed\n");
167 return ret;
169 radio_disconnected = 0;
171 return ret;
174 /* V4L2 RADIO (/dev/radioX) device IOCTL interfaces */
175 static int fm_v4l2_vidioc_querycap(struct file *file, void *priv,
176 struct v4l2_capability *capability)
178 strlcpy(capability->driver, FM_DRV_NAME, sizeof(capability->driver));
179 strlcpy(capability->card, FM_DRV_CARD_SHORT_NAME,
180 sizeof(capability->card));
181 sprintf(capability->bus_info, "UART");
182 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_TUNER |
183 V4L2_CAP_RADIO | V4L2_CAP_MODULATOR |
184 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE |
185 V4L2_CAP_RDS_CAPTURE;
187 return 0;
190 static int fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
192 struct fmdev *fmdev = container_of(ctrl->handler,
193 struct fmdev, ctrl_handler);
195 switch (ctrl->id) {
196 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
197 ctrl->val = fm_tx_get_tune_cap_val(fmdev);
198 break;
199 default:
200 fmwarn("%s: Unknown IOCTL: %d\n", __func__, ctrl->id);
201 break;
204 return 0;
207 static int fm_v4l2_s_ctrl(struct v4l2_ctrl *ctrl)
209 struct fmdev *fmdev = container_of(ctrl->handler,
210 struct fmdev, ctrl_handler);
212 switch (ctrl->id) {
213 case V4L2_CID_AUDIO_VOLUME: /* set volume */
214 return fm_rx_set_volume(fmdev, (u16)ctrl->val);
216 case V4L2_CID_AUDIO_MUTE: /* set mute */
217 return fmc_set_mute_mode(fmdev, (u8)ctrl->val);
219 case V4L2_CID_TUNE_POWER_LEVEL:
220 /* set TX power level - ext control */
221 return fm_tx_set_pwr_lvl(fmdev, (u8)ctrl->val);
223 case V4L2_CID_TUNE_PREEMPHASIS:
224 return fm_tx_set_preemph_filter(fmdev, (u8) ctrl->val);
226 default:
227 return -EINVAL;
231 static int fm_v4l2_vidioc_g_audio(struct file *file, void *priv,
232 struct v4l2_audio *audio)
234 memset(audio, 0, sizeof(*audio));
235 strcpy(audio->name, "Radio");
236 audio->capability = V4L2_AUDCAP_STEREO;
238 return 0;
241 static int fm_v4l2_vidioc_s_audio(struct file *file, void *priv,
242 struct v4l2_audio *audio)
244 if (audio->index != 0)
245 return -EINVAL;
247 return 0;
250 /* Get tuner attributes. If current mode is NOT RX, return error */
251 static int fm_v4l2_vidioc_g_tuner(struct file *file, void *priv,
252 struct v4l2_tuner *tuner)
254 struct fmdev *fmdev = video_drvdata(file);
255 u32 bottom_freq;
256 u32 top_freq;
257 u16 stereo_mono_mode;
258 u16 rssilvl;
259 int ret;
261 if (tuner->index != 0)
262 return -EINVAL;
264 if (fmdev->curr_fmmode != FM_MODE_RX)
265 return -EPERM;
267 ret = fm_rx_get_band_freq_range(fmdev, &bottom_freq, &top_freq);
268 if (ret != 0)
269 return ret;
271 ret = fm_rx_get_stereo_mono(fmdev, &stereo_mono_mode);
272 if (ret != 0)
273 return ret;
275 ret = fm_rx_get_rssi_level(fmdev, &rssilvl);
276 if (ret != 0)
277 return ret;
279 strcpy(tuner->name, "FM");
280 tuner->type = V4L2_TUNER_RADIO;
281 /* Store rangelow and rangehigh freq in unit of 62.5 Hz */
282 tuner->rangelow = bottom_freq * 16;
283 tuner->rangehigh = top_freq * 16;
284 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO |
285 ((fmdev->rx.rds.flag == FM_RDS_ENABLE) ? V4L2_TUNER_SUB_RDS : 0);
286 tuner->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
287 V4L2_TUNER_CAP_LOW;
288 tuner->audmode = (stereo_mono_mode ?
289 V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO);
292 * Actual rssi value lies in between -128 to +127.
293 * Convert this range from 0 to 255 by adding +128
295 rssilvl += 128;
298 * Return signal strength value should be within 0 to 65535.
299 * Find out correct signal radio by multiplying (65535/255) = 257
301 tuner->signal = rssilvl * 257;
302 tuner->afc = 0;
304 return ret;
308 * Set tuner attributes. If current mode is NOT RX, set to RX.
309 * Currently, we set only audio mode (mono/stereo) and RDS state (on/off).
310 * Should we set other tuner attributes, too?
312 static int fm_v4l2_vidioc_s_tuner(struct file *file, void *priv,
313 struct v4l2_tuner *tuner)
315 struct fmdev *fmdev = video_drvdata(file);
316 u16 aud_mode;
317 u8 rds_mode;
318 int ret;
320 if (tuner->index != 0)
321 return -EINVAL;
323 aud_mode = (tuner->audmode == V4L2_TUNER_MODE_STEREO) ?
324 FM_STEREO_MODE : FM_MONO_MODE;
325 rds_mode = (tuner->rxsubchans & V4L2_TUNER_SUB_RDS) ?
326 FM_RDS_ENABLE : FM_RDS_DISABLE;
328 if (fmdev->curr_fmmode != FM_MODE_RX) {
329 ret = fmc_set_mode(fmdev, FM_MODE_RX);
330 if (ret < 0) {
331 fmerr("Failed to set RX mode\n");
332 return ret;
336 ret = fmc_set_stereo_mono(fmdev, aud_mode);
337 if (ret < 0) {
338 fmerr("Failed to set RX stereo/mono mode\n");
339 return ret;
342 ret = fmc_set_rds_mode(fmdev, rds_mode);
343 if (ret < 0)
344 fmerr("Failed to set RX RDS mode\n");
346 return ret;
349 /* Get tuner or modulator radio frequency */
350 static int fm_v4l2_vidioc_g_freq(struct file *file, void *priv,
351 struct v4l2_frequency *freq)
353 struct fmdev *fmdev = video_drvdata(file);
354 int ret;
356 ret = fmc_get_freq(fmdev, &freq->frequency);
357 if (ret < 0) {
358 fmerr("Failed to get frequency\n");
359 return ret;
362 /* Frequency unit of 62.5 Hz*/
363 freq->frequency = (u32) freq->frequency * 16;
365 return 0;
368 /* Set tuner or modulator radio frequency */
369 static int fm_v4l2_vidioc_s_freq(struct file *file, void *priv,
370 struct v4l2_frequency *freq)
372 struct fmdev *fmdev = video_drvdata(file);
375 * As V4L2_TUNER_CAP_LOW is set 1 user sends the frequency
376 * in units of 62.5 Hz.
378 freq->frequency = (u32)(freq->frequency / 16);
380 return fmc_set_freq(fmdev, freq->frequency);
383 /* Set hardware frequency seek. If current mode is NOT RX, set it RX. */
384 static int fm_v4l2_vidioc_s_hw_freq_seek(struct file *file, void *priv,
385 struct v4l2_hw_freq_seek *seek)
387 struct fmdev *fmdev = video_drvdata(file);
388 int ret;
390 if (fmdev->curr_fmmode != FM_MODE_RX) {
391 ret = fmc_set_mode(fmdev, FM_MODE_RX);
392 if (ret != 0) {
393 fmerr("Failed to set RX mode\n");
394 return ret;
398 ret = fm_rx_seek(fmdev, seek->seek_upward, seek->wrap_around,
399 seek->spacing);
400 if (ret < 0)
401 fmerr("RX seek failed - %d\n", ret);
403 return ret;
405 /* Get modulator attributes. If mode is not TX, return no attributes. */
406 static int fm_v4l2_vidioc_g_modulator(struct file *file, void *priv,
407 struct v4l2_modulator *mod)
409 struct fmdev *fmdev = video_drvdata(file);
411 if (mod->index != 0)
412 return -EINVAL;
414 if (fmdev->curr_fmmode != FM_MODE_TX)
415 return -EPERM;
417 mod->txsubchans = ((fmdev->tx_data.aud_mode == FM_STEREO_MODE) ?
418 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO) |
419 ((fmdev->tx_data.rds.flag == FM_RDS_ENABLE) ?
420 V4L2_TUNER_SUB_RDS : 0);
422 mod->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
423 V4L2_TUNER_CAP_LOW;
425 return 0;
428 /* Set modulator attributes. If mode is not TX, set to TX. */
429 static int fm_v4l2_vidioc_s_modulator(struct file *file, void *priv,
430 struct v4l2_modulator *mod)
432 struct fmdev *fmdev = video_drvdata(file);
433 u8 rds_mode;
434 u16 aud_mode;
435 int ret;
437 if (mod->index != 0)
438 return -EINVAL;
440 if (fmdev->curr_fmmode != FM_MODE_TX) {
441 ret = fmc_set_mode(fmdev, FM_MODE_TX);
442 if (ret != 0) {
443 fmerr("Failed to set TX mode\n");
444 return ret;
448 aud_mode = (mod->txsubchans & V4L2_TUNER_SUB_STEREO) ?
449 FM_STEREO_MODE : FM_MONO_MODE;
450 rds_mode = (mod->txsubchans & V4L2_TUNER_SUB_RDS) ?
451 FM_RDS_ENABLE : FM_RDS_DISABLE;
452 ret = fm_tx_set_stereo_mono(fmdev, aud_mode);
453 if (ret < 0) {
454 fmerr("Failed to set mono/stereo mode for TX\n");
455 return ret;
457 ret = fm_tx_set_rds_mode(fmdev, rds_mode);
458 if (ret < 0)
459 fmerr("Failed to set rds mode for TX\n");
461 return ret;
464 static const struct v4l2_file_operations fm_drv_fops = {
465 .owner = THIS_MODULE,
466 .read = fm_v4l2_fops_read,
467 .write = fm_v4l2_fops_write,
468 .poll = fm_v4l2_fops_poll,
469 .unlocked_ioctl = video_ioctl2,
470 .open = fm_v4l2_fops_open,
471 .release = fm_v4l2_fops_release,
474 static const struct v4l2_ctrl_ops fm_ctrl_ops = {
475 .s_ctrl = fm_v4l2_s_ctrl,
476 .g_volatile_ctrl = fm_g_volatile_ctrl,
478 static const struct v4l2_ioctl_ops fm_drv_ioctl_ops = {
479 .vidioc_querycap = fm_v4l2_vidioc_querycap,
480 .vidioc_g_audio = fm_v4l2_vidioc_g_audio,
481 .vidioc_s_audio = fm_v4l2_vidioc_s_audio,
482 .vidioc_g_tuner = fm_v4l2_vidioc_g_tuner,
483 .vidioc_s_tuner = fm_v4l2_vidioc_s_tuner,
484 .vidioc_g_frequency = fm_v4l2_vidioc_g_freq,
485 .vidioc_s_frequency = fm_v4l2_vidioc_s_freq,
486 .vidioc_s_hw_freq_seek = fm_v4l2_vidioc_s_hw_freq_seek,
487 .vidioc_g_modulator = fm_v4l2_vidioc_g_modulator,
488 .vidioc_s_modulator = fm_v4l2_vidioc_s_modulator
491 /* V4L2 RADIO device parent structure */
492 static struct video_device fm_viddev_template = {
493 .fops = &fm_drv_fops,
494 .ioctl_ops = &fm_drv_ioctl_ops,
495 .name = FM_DRV_NAME,
496 .release = video_device_release,
499 int fm_v4l2_init_video_device(struct fmdev *fmdev, int radio_nr)
501 struct v4l2_ctrl *ctrl;
502 int ret;
504 /* Init mutex for core locking */
505 mutex_init(&fmdev->mutex);
507 /* Allocate new video device */
508 gradio_dev = video_device_alloc();
509 if (NULL == gradio_dev) {
510 fmerr("Can't allocate video device\n");
511 return -ENOMEM;
514 /* Setup FM driver's V4L2 properties */
515 memcpy(gradio_dev, &fm_viddev_template, sizeof(fm_viddev_template));
517 video_set_drvdata(gradio_dev, fmdev);
519 gradio_dev->lock = &fmdev->mutex;
521 /* Register with V4L2 subsystem as RADIO device */
522 if (video_register_device(gradio_dev, VFL_TYPE_RADIO, radio_nr)) {
523 video_device_release(gradio_dev);
524 fmerr("Could not register video device\n");
525 return -ENOMEM;
528 fmdev->radio_dev = gradio_dev;
530 /* Register to v4l2 ctrl handler framework */
531 fmdev->radio_dev->ctrl_handler = &fmdev->ctrl_handler;
533 ret = v4l2_ctrl_handler_init(&fmdev->ctrl_handler, 5);
534 if (ret < 0) {
535 fmerr("(fmdev): Can't init ctrl handler\n");
536 v4l2_ctrl_handler_free(&fmdev->ctrl_handler);
537 return -EBUSY;
541 * Following controls are handled by V4L2 control framework.
542 * Added in ascending ID order.
544 v4l2_ctrl_new_std(&fmdev->ctrl_handler, &fm_ctrl_ops,
545 V4L2_CID_AUDIO_VOLUME, FM_RX_VOLUME_MIN,
546 FM_RX_VOLUME_MAX, 1, FM_RX_VOLUME_MAX);
548 v4l2_ctrl_new_std(&fmdev->ctrl_handler, &fm_ctrl_ops,
549 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
551 v4l2_ctrl_new_std_menu(&fmdev->ctrl_handler, &fm_ctrl_ops,
552 V4L2_CID_TUNE_PREEMPHASIS, V4L2_PREEMPHASIS_75_uS,
553 0, V4L2_PREEMPHASIS_75_uS);
555 v4l2_ctrl_new_std(&fmdev->ctrl_handler, &fm_ctrl_ops,
556 V4L2_CID_TUNE_POWER_LEVEL, FM_PWR_LVL_LOW,
557 FM_PWR_LVL_HIGH, 1, FM_PWR_LVL_HIGH);
559 ctrl = v4l2_ctrl_new_std(&fmdev->ctrl_handler, &fm_ctrl_ops,
560 V4L2_CID_TUNE_ANTENNA_CAPACITOR, 0,
561 255, 1, 255);
563 if (ctrl)
564 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
566 return 0;
569 void *fm_v4l2_deinit_video_device(void)
571 struct fmdev *fmdev;
574 fmdev = video_get_drvdata(gradio_dev);
576 /* Unregister to v4l2 ctrl handler framework*/
577 v4l2_ctrl_handler_free(&fmdev->ctrl_handler);
579 /* Unregister RADIO device from V4L2 subsystem */
580 video_unregister_device(gradio_dev);
582 return fmdev;