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