fmdrv_v4l2.c (15269B)
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * FM Driver for Connectivity chip of Texas Instruments. 4 * This file provides interfaces to V4L2 subsystem. 5 * 6 * This module registers with V4L2 subsystem as Radio 7 * data system interface (/dev/radio). During the registration, 8 * it will expose two set of function pointers. 9 * 10 * 1) File operation related API (open, close, read, write, poll...etc). 11 * 2) Set of V4L2 IOCTL complaint API. 12 * 13 * Copyright (C) 2011 Texas Instruments 14 * Author: Raja Mani <raja_mani@ti.com> 15 * Author: Manjunatha Halli <manjunatha_halli@ti.com> 16 */ 17 18#include <linux/export.h> 19 20#include "fmdrv.h" 21#include "fmdrv_v4l2.h" 22#include "fmdrv_common.h" 23#include "fmdrv_rx.h" 24#include "fmdrv_tx.h" 25 26static struct video_device gradio_dev; 27static u8 radio_disconnected; 28 29/* -- V4L2 RADIO (/dev/radioX) device file operation interfaces --- */ 30 31/* Read RX RDS data */ 32static ssize_t fm_v4l2_fops_read(struct file *file, char __user * buf, 33 size_t count, loff_t *ppos) 34{ 35 u8 rds_mode; 36 int ret; 37 struct fmdev *fmdev; 38 39 fmdev = video_drvdata(file); 40 41 if (!radio_disconnected) { 42 fmerr("FM device is already disconnected\n"); 43 return -EIO; 44 } 45 46 if (mutex_lock_interruptible(&fmdev->mutex)) 47 return -ERESTARTSYS; 48 49 /* Turn on RDS mode if it is disabled */ 50 ret = fm_rx_get_rds_mode(fmdev, &rds_mode); 51 if (ret < 0) { 52 fmerr("Unable to read current rds mode\n"); 53 goto read_unlock; 54 } 55 56 if (rds_mode == FM_RDS_DISABLE) { 57 ret = fmc_set_rds_mode(fmdev, FM_RDS_ENABLE); 58 if (ret < 0) { 59 fmerr("Failed to enable rds mode\n"); 60 goto read_unlock; 61 } 62 } 63 64 /* Copy RDS data from internal buffer to user buffer */ 65 ret = fmc_transfer_rds_from_internal_buff(fmdev, file, buf, count); 66read_unlock: 67 mutex_unlock(&fmdev->mutex); 68 return ret; 69} 70 71/* Write TX RDS data */ 72static ssize_t fm_v4l2_fops_write(struct file *file, const char __user * buf, 73 size_t count, loff_t *ppos) 74{ 75 struct tx_rds rds; 76 int ret; 77 struct fmdev *fmdev; 78 79 ret = copy_from_user(&rds, buf, sizeof(rds)); 80 rds.text[sizeof(rds.text) - 1] = '\0'; 81 fmdbg("(%d)type: %d, text %s, af %d\n", 82 ret, rds.text_type, rds.text, rds.af_freq); 83 if (ret) 84 return -EFAULT; 85 86 fmdev = video_drvdata(file); 87 if (mutex_lock_interruptible(&fmdev->mutex)) 88 return -ERESTARTSYS; 89 fm_tx_set_radio_text(fmdev, rds.text, rds.text_type); 90 fm_tx_set_af(fmdev, rds.af_freq); 91 mutex_unlock(&fmdev->mutex); 92 93 return sizeof(rds); 94} 95 96static __poll_t fm_v4l2_fops_poll(struct file *file, struct poll_table_struct *pts) 97{ 98 int ret; 99 struct fmdev *fmdev; 100 101 fmdev = video_drvdata(file); 102 mutex_lock(&fmdev->mutex); 103 ret = fmc_is_rds_data_available(fmdev, file, pts); 104 mutex_unlock(&fmdev->mutex); 105 if (ret < 0) 106 return EPOLLIN | EPOLLRDNORM; 107 108 return 0; 109} 110 111/* 112 * Handle open request for "/dev/radioX" device. 113 * Start with FM RX mode as default. 114 */ 115static int fm_v4l2_fops_open(struct file *file) 116{ 117 int ret; 118 struct fmdev *fmdev = NULL; 119 120 /* Don't allow multiple open */ 121 if (radio_disconnected) { 122 fmerr("FM device is already opened\n"); 123 return -EBUSY; 124 } 125 126 fmdev = video_drvdata(file); 127 128 if (mutex_lock_interruptible(&fmdev->mutex)) 129 return -ERESTARTSYS; 130 ret = fmc_prepare(fmdev); 131 if (ret < 0) { 132 fmerr("Unable to prepare FM CORE\n"); 133 goto open_unlock; 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 goto open_unlock; 142 } 143 radio_disconnected = 1; 144 145open_unlock: 146 mutex_unlock(&fmdev->mutex); 147 return ret; 148} 149 150static int fm_v4l2_fops_release(struct file *file) 151{ 152 int ret; 153 struct fmdev *fmdev; 154 155 fmdev = video_drvdata(file); 156 if (!radio_disconnected) { 157 fmdbg("FM device is already closed\n"); 158 return 0; 159 } 160 161 mutex_lock(&fmdev->mutex); 162 ret = fmc_set_mode(fmdev, FM_MODE_OFF); 163 if (ret < 0) { 164 fmerr("Unable to turn off the chip\n"); 165 goto release_unlock; 166 } 167 168 ret = fmc_release(fmdev); 169 if (ret < 0) { 170 fmerr("FM CORE release failed\n"); 171 goto release_unlock; 172 } 173 radio_disconnected = 0; 174 175release_unlock: 176 mutex_unlock(&fmdev->mutex); 177 return ret; 178} 179 180/* V4L2 RADIO (/dev/radioX) device IOCTL interfaces */ 181static int fm_v4l2_vidioc_querycap(struct file *file, void *priv, 182 struct v4l2_capability *capability) 183{ 184 strscpy(capability->driver, FM_DRV_NAME, sizeof(capability->driver)); 185 strscpy(capability->card, FM_DRV_CARD_SHORT_NAME, 186 sizeof(capability->card)); 187 sprintf(capability->bus_info, "UART"); 188 return 0; 189} 190 191static 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 208static 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 232static int fm_v4l2_vidioc_g_audio(struct file *file, void *priv, 233 struct v4l2_audio *audio) 234{ 235 memset(audio, 0, sizeof(*audio)); 236 strscpy(audio->name, "Radio", sizeof(audio->name)); 237 audio->capability = V4L2_AUDCAP_STEREO; 238 239 return 0; 240} 241 242static int fm_v4l2_vidioc_s_audio(struct file *file, void *priv, 243 const 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 */ 252static 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 strscpy(tuner->name, "FM", sizeof(tuner->name)); 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 */ 315static int fm_v4l2_vidioc_s_tuner(struct file *file, void *priv, 316 const 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 */ 353static 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 */ 372static int fm_v4l2_vidioc_s_freq(struct file *file, void *priv, 373 const 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 return fmc_set_freq(fmdev, freq->frequency / 16); 382} 383 384/* Set hardware frequency seek. If current mode is NOT RX, set it RX. */ 385static int fm_v4l2_vidioc_s_hw_freq_seek(struct file *file, void *priv, 386 const struct v4l2_hw_freq_seek *seek) 387{ 388 struct fmdev *fmdev = video_drvdata(file); 389 int ret; 390 391 if (file->f_flags & O_NONBLOCK) 392 return -EWOULDBLOCK; 393 394 if (fmdev->curr_fmmode != FM_MODE_RX) { 395 ret = fmc_set_mode(fmdev, FM_MODE_RX); 396 if (ret != 0) { 397 fmerr("Failed to set RX mode\n"); 398 return ret; 399 } 400 } 401 402 ret = fm_rx_seek(fmdev, seek->seek_upward, seek->wrap_around, 403 seek->spacing); 404 if (ret < 0) 405 fmerr("RX seek failed - %d\n", ret); 406 407 return ret; 408} 409/* Get modulator attributes. If mode is not TX, return no attributes. */ 410static int fm_v4l2_vidioc_g_modulator(struct file *file, void *priv, 411 struct v4l2_modulator *mod) 412{ 413 struct fmdev *fmdev = video_drvdata(file); 414 415 if (mod->index != 0) 416 return -EINVAL; 417 418 if (fmdev->curr_fmmode != FM_MODE_TX) 419 return -EPERM; 420 421 mod->txsubchans = ((fmdev->tx_data.aud_mode == FM_STEREO_MODE) ? 422 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO) | 423 ((fmdev->tx_data.rds.flag == FM_RDS_ENABLE) ? 424 V4L2_TUNER_SUB_RDS : 0); 425 426 mod->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS | 427 V4L2_TUNER_CAP_LOW; 428 429 return 0; 430} 431 432/* Set modulator attributes. If mode is not TX, set to TX. */ 433static int fm_v4l2_vidioc_s_modulator(struct file *file, void *priv, 434 const struct v4l2_modulator *mod) 435{ 436 struct fmdev *fmdev = video_drvdata(file); 437 u8 rds_mode; 438 u16 aud_mode; 439 int ret; 440 441 if (mod->index != 0) 442 return -EINVAL; 443 444 if (fmdev->curr_fmmode != FM_MODE_TX) { 445 ret = fmc_set_mode(fmdev, FM_MODE_TX); 446 if (ret != 0) { 447 fmerr("Failed to set TX mode\n"); 448 return ret; 449 } 450 } 451 452 aud_mode = (mod->txsubchans & V4L2_TUNER_SUB_STEREO) ? 453 FM_STEREO_MODE : FM_MONO_MODE; 454 rds_mode = (mod->txsubchans & V4L2_TUNER_SUB_RDS) ? 455 FM_RDS_ENABLE : FM_RDS_DISABLE; 456 ret = fm_tx_set_stereo_mono(fmdev, aud_mode); 457 if (ret < 0) { 458 fmerr("Failed to set mono/stereo mode for TX\n"); 459 return ret; 460 } 461 ret = fm_tx_set_rds_mode(fmdev, rds_mode); 462 if (ret < 0) 463 fmerr("Failed to set rds mode for TX\n"); 464 465 return ret; 466} 467 468static const struct v4l2_file_operations fm_drv_fops = { 469 .owner = THIS_MODULE, 470 .read = fm_v4l2_fops_read, 471 .write = fm_v4l2_fops_write, 472 .poll = fm_v4l2_fops_poll, 473 .unlocked_ioctl = video_ioctl2, 474 .open = fm_v4l2_fops_open, 475 .release = fm_v4l2_fops_release, 476}; 477 478static const struct v4l2_ctrl_ops fm_ctrl_ops = { 479 .s_ctrl = fm_v4l2_s_ctrl, 480 .g_volatile_ctrl = fm_g_volatile_ctrl, 481}; 482static const struct v4l2_ioctl_ops fm_drv_ioctl_ops = { 483 .vidioc_querycap = fm_v4l2_vidioc_querycap, 484 .vidioc_g_audio = fm_v4l2_vidioc_g_audio, 485 .vidioc_s_audio = fm_v4l2_vidioc_s_audio, 486 .vidioc_g_tuner = fm_v4l2_vidioc_g_tuner, 487 .vidioc_s_tuner = fm_v4l2_vidioc_s_tuner, 488 .vidioc_g_frequency = fm_v4l2_vidioc_g_freq, 489 .vidioc_s_frequency = fm_v4l2_vidioc_s_freq, 490 .vidioc_s_hw_freq_seek = fm_v4l2_vidioc_s_hw_freq_seek, 491 .vidioc_g_modulator = fm_v4l2_vidioc_g_modulator, 492 .vidioc_s_modulator = fm_v4l2_vidioc_s_modulator 493}; 494 495/* V4L2 RADIO device parent structure */ 496static const struct video_device fm_viddev_template = { 497 .fops = &fm_drv_fops, 498 .ioctl_ops = &fm_drv_ioctl_ops, 499 .name = FM_DRV_NAME, 500 .release = video_device_release_empty, 501 /* 502 * To ensure both the tuner and modulator ioctls are accessible we 503 * set the vfl_dir to M2M to indicate this. 504 * 505 * It is not really a mem2mem device of course, but it can both receive 506 * and transmit using the same radio device. It's the only radio driver 507 * that does this and it should really be split in two radio devices, 508 * but that would affect applications using this driver. 509 */ 510 .vfl_dir = VFL_DIR_M2M, 511 .device_caps = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_TUNER | V4L2_CAP_RADIO | 512 V4L2_CAP_MODULATOR | V4L2_CAP_AUDIO | 513 V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE, 514}; 515 516int fm_v4l2_init_video_device(struct fmdev *fmdev, int radio_nr) 517{ 518 struct v4l2_ctrl *ctrl; 519 int ret; 520 521 strscpy(fmdev->v4l2_dev.name, FM_DRV_NAME, 522 sizeof(fmdev->v4l2_dev.name)); 523 ret = v4l2_device_register(NULL, &fmdev->v4l2_dev); 524 if (ret < 0) 525 return ret; 526 527 /* Init mutex for core locking */ 528 mutex_init(&fmdev->mutex); 529 530 /* Setup FM driver's V4L2 properties */ 531 gradio_dev = fm_viddev_template; 532 533 video_set_drvdata(&gradio_dev, fmdev); 534 535 gradio_dev.lock = &fmdev->mutex; 536 gradio_dev.v4l2_dev = &fmdev->v4l2_dev; 537 538 /* Register with V4L2 subsystem as RADIO device */ 539 if (video_register_device(&gradio_dev, VFL_TYPE_RADIO, radio_nr)) { 540 v4l2_device_unregister(&fmdev->v4l2_dev); 541 fmerr("Could not register video device\n"); 542 return -ENOMEM; 543 } 544 545 fmdev->radio_dev = &gradio_dev; 546 547 /* Register to v4l2 ctrl handler framework */ 548 fmdev->radio_dev->ctrl_handler = &fmdev->ctrl_handler; 549 550 ret = v4l2_ctrl_handler_init(&fmdev->ctrl_handler, 5); 551 if (ret < 0) { 552 fmerr("(fmdev): Can't init ctrl handler\n"); 553 v4l2_ctrl_handler_free(&fmdev->ctrl_handler); 554 video_unregister_device(fmdev->radio_dev); 555 v4l2_device_unregister(&fmdev->v4l2_dev); 556 return -EBUSY; 557 } 558 559 /* 560 * Following controls are handled by V4L2 control framework. 561 * Added in ascending ID order. 562 */ 563 v4l2_ctrl_new_std(&fmdev->ctrl_handler, &fm_ctrl_ops, 564 V4L2_CID_AUDIO_VOLUME, FM_RX_VOLUME_MIN, 565 FM_RX_VOLUME_MAX, 1, FM_RX_VOLUME_MAX); 566 567 v4l2_ctrl_new_std(&fmdev->ctrl_handler, &fm_ctrl_ops, 568 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); 569 570 v4l2_ctrl_new_std_menu(&fmdev->ctrl_handler, &fm_ctrl_ops, 571 V4L2_CID_TUNE_PREEMPHASIS, V4L2_PREEMPHASIS_75_uS, 572 0, V4L2_PREEMPHASIS_75_uS); 573 574 v4l2_ctrl_new_std(&fmdev->ctrl_handler, &fm_ctrl_ops, 575 V4L2_CID_TUNE_POWER_LEVEL, FM_PWR_LVL_LOW, 576 FM_PWR_LVL_HIGH, 1, FM_PWR_LVL_HIGH); 577 578 ctrl = v4l2_ctrl_new_std(&fmdev->ctrl_handler, &fm_ctrl_ops, 579 V4L2_CID_TUNE_ANTENNA_CAPACITOR, 0, 580 255, 1, 255); 581 582 if (ctrl) 583 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE; 584 585 return 0; 586} 587 588void *fm_v4l2_deinit_video_device(void) 589{ 590 struct fmdev *fmdev; 591 592 593 fmdev = video_get_drvdata(&gradio_dev); 594 595 /* Unregister to v4l2 ctrl handler framework*/ 596 v4l2_ctrl_handler_free(&fmdev->ctrl_handler); 597 598 /* Unregister RADIO device from V4L2 subsystem */ 599 video_unregister_device(&gradio_dev); 600 601 v4l2_device_unregister(&fmdev->v4l2_dev); 602 603 return fmdev; 604}