Matti Aaltonen | 87d1a50 | 2010-12-10 11:41:34 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Texas Instruments WL1273 FM radio. |
| 3 | * |
| 4 | * Copyright (C) 2010 Nokia Corporation |
| 5 | * Author: Matti J. Aaltonen <matti.j.aaltonen@nokia.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * version 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/firmware.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/mfd/wl1273-core.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <media/v4l2-common.h> |
| 27 | #include <media/v4l2-ctrls.h> |
| 28 | #include <media/v4l2-device.h> |
| 29 | #include <media/v4l2-ioctl.h> |
| 30 | |
| 31 | #define DRIVER_DESC "Wl1273 FM Radio" |
| 32 | |
| 33 | #define WL1273_POWER_SET_OFF 0 |
| 34 | #define WL1273_POWER_SET_FM BIT(0) |
| 35 | #define WL1273_POWER_SET_RDS BIT(1) |
| 36 | #define WL1273_POWER_SET_RETENTION BIT(4) |
| 37 | |
| 38 | #define WL1273_PUPD_SET_OFF 0x00 |
| 39 | #define WL1273_PUPD_SET_ON 0x01 |
| 40 | #define WL1273_PUPD_SET_RETENTION 0x10 |
| 41 | |
| 42 | #define WL1273_FREQ(x) (x * 10000 / 625) |
| 43 | #define WL1273_INV_FREQ(x) (x * 625 / 10000) |
| 44 | |
| 45 | /* |
| 46 | * static int radio_nr - The number of the radio device |
| 47 | * |
| 48 | * The default is 0. |
| 49 | */ |
| 50 | static int radio_nr; |
| 51 | module_param(radio_nr, int, 0); |
| 52 | MODULE_PARM_DESC(radio_nr, "The number of the radio device. Default = 0"); |
| 53 | |
| 54 | struct wl1273_device { |
| 55 | char *bus_type; |
| 56 | |
| 57 | u8 forbidden; |
| 58 | unsigned int preemphasis; |
| 59 | unsigned int spacing; |
| 60 | unsigned int tx_power; |
| 61 | unsigned int rx_frequency; |
| 62 | unsigned int tx_frequency; |
| 63 | unsigned int rangelow; |
| 64 | unsigned int rangehigh; |
| 65 | unsigned int band; |
| 66 | bool stereo; |
| 67 | |
| 68 | /* RDS */ |
| 69 | unsigned int rds_on; |
| 70 | struct delayed_work work; |
| 71 | |
| 72 | wait_queue_head_t read_queue; |
| 73 | struct mutex lock; /* for serializing fm radio operations */ |
| 74 | struct completion busy; |
| 75 | |
| 76 | unsigned char *buffer; |
| 77 | unsigned int buf_size; |
| 78 | unsigned int rd_index; |
| 79 | unsigned int wr_index; |
| 80 | |
| 81 | /* Selected interrupts */ |
| 82 | u16 irq_flags; |
| 83 | u16 irq_received; |
| 84 | |
| 85 | struct v4l2_ctrl_handler ctrl_handler; |
| 86 | struct v4l2_device v4l2dev; |
| 87 | struct video_device videodev; |
| 88 | struct device *dev; |
| 89 | struct wl1273_core *core; |
| 90 | struct file *owner; |
| 91 | char *write_buf; |
| 92 | unsigned int rds_users; |
| 93 | }; |
| 94 | |
| 95 | #define WL1273_IRQ_MASK (WL1273_FR_EVENT | \ |
| 96 | WL1273_POW_ENB_EVENT) |
| 97 | |
| 98 | /* |
| 99 | * static unsigned int rds_buf - the number of RDS buffer blocks used. |
| 100 | * |
| 101 | * The default number is 100. |
| 102 | */ |
| 103 | static unsigned int rds_buf = 100; |
| 104 | module_param(rds_buf, uint, 0); |
| 105 | MODULE_PARM_DESC(rds_buf, "Number of RDS buffer entries. Default = 100"); |
| 106 | |
| 107 | static int wl1273_fm_read_reg(struct wl1273_core *core, u8 reg, u16 *value) |
| 108 | { |
| 109 | struct i2c_client *client = core->client; |
| 110 | u8 b[2]; |
| 111 | int r; |
| 112 | |
| 113 | r = i2c_smbus_read_i2c_block_data(client, reg, sizeof(b), b); |
| 114 | if (r != 2) { |
| 115 | dev_err(&client->dev, "%s: Read: %d fails.\n", __func__, reg); |
| 116 | return -EREMOTEIO; |
| 117 | } |
| 118 | |
| 119 | *value = (u16)b[0] << 8 | b[1]; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int wl1273_fm_write_cmd(struct wl1273_core *core, u8 cmd, u16 param) |
| 125 | { |
| 126 | struct i2c_client *client = core->client; |
| 127 | u8 buf[] = { (param >> 8) & 0xff, param & 0xff }; |
| 128 | int r; |
| 129 | |
| 130 | r = i2c_smbus_write_i2c_block_data(client, cmd, sizeof(buf), buf); |
| 131 | if (r) { |
| 132 | dev_err(&client->dev, "%s: Cmd: %d fails.\n", __func__, cmd); |
| 133 | return r; |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int wl1273_fm_write_data(struct wl1273_core *core, u8 *data, u16 len) |
| 140 | { |
| 141 | struct i2c_client *client = core->client; |
| 142 | struct i2c_msg msg; |
| 143 | int r; |
| 144 | |
| 145 | msg.addr = client->addr; |
| 146 | msg.flags = 0; |
| 147 | msg.buf = data; |
| 148 | msg.len = len; |
| 149 | |
| 150 | r = i2c_transfer(client->adapter, &msg, 1); |
| 151 | if (r != 1) { |
| 152 | dev_err(&client->dev, "%s: write error.\n", __func__); |
| 153 | return -EREMOTEIO; |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static int wl1273_fm_write_fw(struct wl1273_core *core, |
| 160 | __u8 *fw, int len) |
| 161 | { |
| 162 | struct i2c_client *client = core->client; |
| 163 | struct i2c_msg msg; |
| 164 | int i, r = 0; |
| 165 | |
| 166 | msg.addr = client->addr; |
| 167 | msg.flags = 0; |
| 168 | |
| 169 | for (i = 0; i <= len; i++) { |
| 170 | msg.len = fw[0]; |
| 171 | msg.buf = fw + 1; |
| 172 | |
| 173 | fw += msg.len + 1; |
| 174 | dev_dbg(&client->dev, "%s:len[%d]: %d\n", __func__, i, msg.len); |
| 175 | |
| 176 | r = i2c_transfer(client->adapter, &msg, 1); |
| 177 | if (r < 0 && i < len + 1) |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | dev_dbg(&client->dev, "%s: i: %d\n", __func__, i); |
| 182 | dev_dbg(&client->dev, "%s: len + 1: %d\n", __func__, len + 1); |
| 183 | |
| 184 | /* Last transfer always fails. */ |
| 185 | if (i == len || r == 1) |
| 186 | r = 0; |
| 187 | |
| 188 | return r; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * wl1273_fm_set_audio() - Set audio mode. |
| 193 | * @core: A pointer to the device struct. |
| 194 | * @new_mode: The new audio mode. |
| 195 | * |
| 196 | * Audio modes are WL1273_AUDIO_DIGITAL and WL1273_AUDIO_ANALOG. |
| 197 | */ |
| 198 | static int wl1273_fm_set_audio(struct wl1273_core *core, unsigned int new_mode) |
| 199 | { |
| 200 | int r = 0; |
| 201 | |
| 202 | if (core->mode == WL1273_MODE_OFF || |
| 203 | core->mode == WL1273_MODE_SUSPENDED) |
| 204 | return -EPERM; |
| 205 | |
| 206 | if (core->mode == WL1273_MODE_RX && new_mode == WL1273_AUDIO_DIGITAL) { |
| 207 | r = wl1273_fm_write_cmd(core, WL1273_PCM_MODE_SET, |
| 208 | WL1273_PCM_DEF_MODE); |
| 209 | if (r) |
| 210 | goto out; |
| 211 | |
| 212 | r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET, |
| 213 | core->i2s_mode); |
| 214 | if (r) |
| 215 | goto out; |
| 216 | |
| 217 | r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE, |
| 218 | WL1273_AUDIO_ENABLE_I2S); |
| 219 | if (r) |
| 220 | goto out; |
| 221 | |
| 222 | } else if (core->mode == WL1273_MODE_RX && |
| 223 | new_mode == WL1273_AUDIO_ANALOG) { |
| 224 | r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE, |
| 225 | WL1273_AUDIO_ENABLE_ANALOG); |
| 226 | if (r) |
| 227 | goto out; |
| 228 | |
| 229 | } else if (core->mode == WL1273_MODE_TX && |
| 230 | new_mode == WL1273_AUDIO_DIGITAL) { |
| 231 | r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET, |
| 232 | core->i2s_mode); |
| 233 | if (r) |
| 234 | goto out; |
| 235 | |
| 236 | r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET, |
| 237 | WL1273_AUDIO_IO_SET_I2S); |
| 238 | if (r) |
| 239 | goto out; |
| 240 | |
| 241 | } else if (core->mode == WL1273_MODE_TX && |
| 242 | new_mode == WL1273_AUDIO_ANALOG) { |
| 243 | r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET, |
| 244 | WL1273_AUDIO_IO_SET_ANALOG); |
| 245 | if (r) |
| 246 | goto out; |
| 247 | } |
| 248 | |
| 249 | core->audio_mode = new_mode; |
| 250 | out: |
| 251 | return r; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * wl1273_fm_set_volume() - Set volume. |
| 256 | * @core: A pointer to the device struct. |
| 257 | * @volume: The new volume value. |
| 258 | */ |
| 259 | static int wl1273_fm_set_volume(struct wl1273_core *core, unsigned int volume) |
| 260 | { |
| 261 | u16 val; |
| 262 | int r; |
| 263 | |
| 264 | if (volume > WL1273_MAX_VOLUME) |
| 265 | return -EINVAL; |
| 266 | |
| 267 | if (core->volume == volume) |
| 268 | return 0; |
| 269 | |
| 270 | val = volume; |
| 271 | r = wl1273_fm_read_reg(core, WL1273_VOLUME_SET, &val); |
| 272 | if (r) |
| 273 | return r; |
| 274 | |
| 275 | core->volume = volume; |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | #define WL1273_FIFO_HAS_DATA(status) (1 << 5 & status) |
| 280 | #define WL1273_RDS_CORRECTABLE_ERROR (1 << 3) |
| 281 | #define WL1273_RDS_UNCORRECTABLE_ERROR (1 << 4) |
| 282 | |
| 283 | static int wl1273_fm_rds(struct wl1273_device *radio) |
| 284 | { |
| 285 | struct wl1273_core *core = radio->core; |
| 286 | struct i2c_client *client = core->client; |
| 287 | u16 val; |
| 288 | u8 b0 = WL1273_RDS_DATA_GET, status; |
| 289 | struct v4l2_rds_data rds = { 0, 0, 0 }; |
| 290 | struct i2c_msg msg[] = { |
| 291 | { |
| 292 | .addr = client->addr, |
| 293 | .flags = 0, |
| 294 | .buf = &b0, |
| 295 | .len = 1, |
| 296 | }, |
| 297 | { |
| 298 | .addr = client->addr, |
| 299 | .flags = I2C_M_RD, |
| 300 | .buf = (u8 *) &rds, |
| 301 | .len = sizeof(rds), |
| 302 | } |
| 303 | }; |
| 304 | int r; |
| 305 | |
| 306 | if (core->mode != WL1273_MODE_RX) |
| 307 | return 0; |
| 308 | |
| 309 | r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val); |
| 310 | if (r) |
| 311 | return r; |
| 312 | |
| 313 | if ((val & 0x01) == 0) { |
| 314 | /* RDS decoder not synchronized */ |
| 315 | return -EAGAIN; |
| 316 | } |
| 317 | |
| 318 | /* copy all four RDS blocks to internal buffer */ |
| 319 | do { |
| 320 | r = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); |
| 321 | if (r != ARRAY_SIZE(msg)) { |
| 322 | dev_err(radio->dev, WL1273_FM_DRIVER_NAME |
| 323 | ": %s: read_rds error r == %i)\n", |
| 324 | __func__, r); |
| 325 | } |
| 326 | |
| 327 | status = rds.block; |
| 328 | |
| 329 | if (!WL1273_FIFO_HAS_DATA(status)) |
| 330 | break; |
| 331 | |
| 332 | /* copy bits 0-2 (the block ID) to bits 3-5 */ |
| 333 | rds.block = V4L2_RDS_BLOCK_MSK & status; |
| 334 | rds.block |= rds.block << 3; |
| 335 | |
| 336 | /* copy the error bits to standard positions */ |
| 337 | if (WL1273_RDS_UNCORRECTABLE_ERROR & status) { |
| 338 | rds.block |= V4L2_RDS_BLOCK_ERROR; |
| 339 | rds.block &= ~V4L2_RDS_BLOCK_CORRECTED; |
| 340 | } else if (WL1273_RDS_CORRECTABLE_ERROR & status) { |
| 341 | rds.block &= ~V4L2_RDS_BLOCK_ERROR; |
| 342 | rds.block |= V4L2_RDS_BLOCK_CORRECTED; |
| 343 | } |
| 344 | |
| 345 | /* copy RDS block to internal buffer */ |
| 346 | memcpy(&radio->buffer[radio->wr_index], &rds, RDS_BLOCK_SIZE); |
| 347 | radio->wr_index += 3; |
| 348 | |
| 349 | /* wrap write pointer */ |
| 350 | if (radio->wr_index >= radio->buf_size) |
| 351 | radio->wr_index = 0; |
| 352 | |
| 353 | /* check for overflow & start over */ |
| 354 | if (radio->wr_index == radio->rd_index) { |
| 355 | dev_dbg(radio->dev, "RDS OVERFLOW"); |
| 356 | |
| 357 | radio->rd_index = 0; |
| 358 | radio->wr_index = 0; |
| 359 | break; |
| 360 | } |
| 361 | } while (WL1273_FIFO_HAS_DATA(status)); |
| 362 | |
| 363 | /* wake up read queue */ |
| 364 | if (radio->wr_index != radio->rd_index) |
| 365 | wake_up_interruptible(&radio->read_queue); |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static irqreturn_t wl1273_fm_irq_thread_handler(int irq, void *dev_id) |
| 371 | { |
| 372 | struct wl1273_device *radio = dev_id; |
| 373 | struct wl1273_core *core = radio->core; |
| 374 | u16 flags; |
| 375 | int r; |
| 376 | |
| 377 | r = wl1273_fm_read_reg(core, WL1273_FLAG_GET, &flags); |
| 378 | if (r) |
| 379 | goto out; |
| 380 | |
| 381 | if (flags & WL1273_BL_EVENT) { |
| 382 | radio->irq_received = flags; |
| 383 | dev_dbg(radio->dev, "IRQ: BL\n"); |
| 384 | } |
| 385 | |
| 386 | if (flags & WL1273_RDS_EVENT) { |
| 387 | msleep(200); |
| 388 | |
| 389 | wl1273_fm_rds(radio); |
| 390 | } |
| 391 | |
| 392 | if (flags & WL1273_BBLK_EVENT) |
| 393 | dev_dbg(radio->dev, "IRQ: BBLK\n"); |
| 394 | |
| 395 | if (flags & WL1273_LSYNC_EVENT) |
| 396 | dev_dbg(radio->dev, "IRQ: LSYNC\n"); |
| 397 | |
| 398 | if (flags & WL1273_LEV_EVENT) { |
| 399 | u16 level; |
| 400 | |
| 401 | r = wl1273_fm_read_reg(core, WL1273_RSSI_LVL_GET, &level); |
| 402 | if (r) |
| 403 | goto out; |
| 404 | |
| 405 | if (level > 14) |
| 406 | dev_dbg(radio->dev, "IRQ: LEV: 0x%x04\n", level); |
| 407 | } |
| 408 | |
| 409 | if (flags & WL1273_IFFR_EVENT) |
| 410 | dev_dbg(radio->dev, "IRQ: IFFR\n"); |
| 411 | |
| 412 | if (flags & WL1273_PI_EVENT) |
| 413 | dev_dbg(radio->dev, "IRQ: PI\n"); |
| 414 | |
| 415 | if (flags & WL1273_PD_EVENT) |
| 416 | dev_dbg(radio->dev, "IRQ: PD\n"); |
| 417 | |
| 418 | if (flags & WL1273_STIC_EVENT) |
| 419 | dev_dbg(radio->dev, "IRQ: STIC\n"); |
| 420 | |
| 421 | if (flags & WL1273_MAL_EVENT) |
| 422 | dev_dbg(radio->dev, "IRQ: MAL\n"); |
| 423 | |
| 424 | if (flags & WL1273_POW_ENB_EVENT) { |
| 425 | complete(&radio->busy); |
| 426 | dev_dbg(radio->dev, "NOT BUSY\n"); |
| 427 | dev_dbg(radio->dev, "IRQ: POW_ENB\n"); |
| 428 | } |
| 429 | |
| 430 | if (flags & WL1273_SCAN_OVER_EVENT) |
| 431 | dev_dbg(radio->dev, "IRQ: SCAN_OVER\n"); |
| 432 | |
| 433 | if (flags & WL1273_ERROR_EVENT) |
| 434 | dev_dbg(radio->dev, "IRQ: ERROR\n"); |
| 435 | |
| 436 | if (flags & WL1273_FR_EVENT) { |
| 437 | u16 freq; |
| 438 | |
| 439 | dev_dbg(radio->dev, "IRQ: FR:\n"); |
| 440 | |
| 441 | if (core->mode == WL1273_MODE_RX) { |
| 442 | r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET, |
| 443 | TUNER_MODE_STOP_SEARCH); |
| 444 | if (r) { |
| 445 | dev_err(radio->dev, |
| 446 | "%s: TUNER_MODE_SET fails: %d\n", |
| 447 | __func__, r); |
| 448 | goto out; |
| 449 | } |
| 450 | |
| 451 | r = wl1273_fm_read_reg(core, WL1273_FREQ_SET, &freq); |
| 452 | if (r) |
| 453 | goto out; |
| 454 | |
| 455 | if (radio->band == WL1273_BAND_JAPAN) |
| 456 | radio->rx_frequency = WL1273_BAND_JAPAN_LOW + |
| 457 | freq * 50; |
| 458 | else |
| 459 | radio->rx_frequency = WL1273_BAND_OTHER_LOW + |
| 460 | freq * 50; |
| 461 | /* |
| 462 | * The driver works better with this msleep, |
| 463 | * the documentation doesn't mention it. |
| 464 | */ |
| 465 | usleep_range(10000, 15000); |
| 466 | |
| 467 | dev_dbg(radio->dev, "%dkHz\n", radio->rx_frequency); |
| 468 | |
| 469 | } else { |
| 470 | r = wl1273_fm_read_reg(core, WL1273_CHANL_SET, &freq); |
| 471 | if (r) |
| 472 | goto out; |
| 473 | |
| 474 | dev_dbg(radio->dev, "%dkHz\n", freq); |
| 475 | } |
| 476 | dev_dbg(radio->dev, "%s: NOT BUSY\n", __func__); |
| 477 | } |
| 478 | |
| 479 | out: |
| 480 | wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, |
| 481 | radio->irq_flags); |
| 482 | complete(&radio->busy); |
| 483 | |
| 484 | return IRQ_HANDLED; |
| 485 | } |
| 486 | |
| 487 | static int wl1273_fm_set_tx_freq(struct wl1273_device *radio, unsigned int freq) |
| 488 | { |
| 489 | struct wl1273_core *core = radio->core; |
| 490 | int r = 0; |
| 491 | |
| 492 | if (freq < WL1273_BAND_TX_LOW) { |
| 493 | dev_err(radio->dev, |
| 494 | "Frequency out of range: %d < %d\n", freq, |
| 495 | WL1273_BAND_TX_LOW); |
| 496 | return -ERANGE; |
| 497 | } |
| 498 | |
| 499 | if (freq > WL1273_BAND_TX_HIGH) { |
| 500 | dev_err(radio->dev, |
| 501 | "Frequency out of range: %d > %d\n", freq, |
| 502 | WL1273_BAND_TX_HIGH); |
| 503 | return -ERANGE; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * The driver works better with this sleep, |
| 508 | * the documentation doesn't mention it. |
| 509 | */ |
| 510 | usleep_range(5000, 10000); |
| 511 | |
| 512 | dev_dbg(radio->dev, "%s: freq: %d kHz\n", __func__, freq); |
| 513 | |
| 514 | /* Set the current tx channel */ |
| 515 | r = wl1273_fm_write_cmd(core, WL1273_CHANL_SET, freq / 10); |
| 516 | if (r) |
| 517 | return r; |
| 518 | |
| 519 | INIT_COMPLETION(radio->busy); |
| 520 | |
| 521 | /* wait for the FR IRQ */ |
| 522 | r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000)); |
| 523 | if (!r) |
| 524 | return -ETIMEDOUT; |
| 525 | |
| 526 | dev_dbg(radio->dev, "WL1273_CHANL_SET: %d\n", r); |
| 527 | |
| 528 | /* Enable the output power */ |
| 529 | r = wl1273_fm_write_cmd(core, WL1273_POWER_ENB_SET, 1); |
| 530 | if (r) |
| 531 | return r; |
| 532 | |
| 533 | INIT_COMPLETION(radio->busy); |
| 534 | |
| 535 | /* wait for the POWER_ENB IRQ */ |
| 536 | r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000)); |
| 537 | if (!r) |
| 538 | return -ETIMEDOUT; |
| 539 | |
| 540 | radio->tx_frequency = freq; |
| 541 | dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %d\n", r); |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | static int wl1273_fm_set_rx_freq(struct wl1273_device *radio, unsigned int freq) |
| 547 | { |
| 548 | struct wl1273_core *core = radio->core; |
| 549 | int r, f; |
| 550 | |
| 551 | if (freq < radio->rangelow) { |
| 552 | dev_err(radio->dev, |
| 553 | "Frequency out of range: %d < %d\n", freq, |
| 554 | radio->rangelow); |
| 555 | r = -ERANGE; |
| 556 | goto err; |
| 557 | } |
| 558 | |
| 559 | if (freq > radio->rangehigh) { |
| 560 | dev_err(radio->dev, |
| 561 | "Frequency out of range: %d > %d\n", freq, |
| 562 | radio->rangehigh); |
| 563 | r = -ERANGE; |
| 564 | goto err; |
| 565 | } |
| 566 | |
| 567 | dev_dbg(radio->dev, "%s: %dkHz\n", __func__, freq); |
| 568 | |
| 569 | wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, radio->irq_flags); |
| 570 | |
| 571 | if (radio->band == WL1273_BAND_JAPAN) |
| 572 | f = (freq - WL1273_BAND_JAPAN_LOW) / 50; |
| 573 | else |
| 574 | f = (freq - WL1273_BAND_OTHER_LOW) / 50; |
| 575 | |
| 576 | r = wl1273_fm_write_cmd(core, WL1273_FREQ_SET, f); |
| 577 | if (r) { |
| 578 | dev_err(radio->dev, "FREQ_SET fails\n"); |
| 579 | goto err; |
| 580 | } |
| 581 | |
| 582 | r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET); |
| 583 | if (r) { |
| 584 | dev_err(radio->dev, "TUNER_MODE_SET fails\n"); |
| 585 | goto err; |
| 586 | } |
| 587 | |
| 588 | INIT_COMPLETION(radio->busy); |
| 589 | |
| 590 | r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000)); |
| 591 | if (!r) { |
| 592 | dev_err(radio->dev, "%s: TIMEOUT\n", __func__); |
| 593 | return -ETIMEDOUT; |
| 594 | } |
| 595 | |
| 596 | radio->rd_index = 0; |
| 597 | radio->wr_index = 0; |
| 598 | radio->rx_frequency = freq; |
| 599 | return 0; |
| 600 | err: |
| 601 | return r; |
| 602 | } |
| 603 | |
| 604 | static int wl1273_fm_get_freq(struct wl1273_device *radio) |
| 605 | { |
| 606 | struct wl1273_core *core = radio->core; |
| 607 | unsigned int freq; |
| 608 | u16 f; |
| 609 | int r; |
| 610 | |
| 611 | if (core->mode == WL1273_MODE_RX) { |
| 612 | r = wl1273_fm_read_reg(core, WL1273_FREQ_SET, &f); |
| 613 | if (r) |
| 614 | return r; |
| 615 | |
| 616 | dev_dbg(radio->dev, "Freq get: 0x%04x\n", f); |
| 617 | if (radio->band == WL1273_BAND_JAPAN) |
| 618 | freq = WL1273_BAND_JAPAN_LOW + 50 * f; |
| 619 | else |
| 620 | freq = WL1273_BAND_OTHER_LOW + 50 * f; |
| 621 | } else { |
| 622 | r = wl1273_fm_read_reg(core, WL1273_CHANL_SET, &f); |
| 623 | if (r) |
| 624 | return r; |
| 625 | |
| 626 | freq = f * 10; |
| 627 | } |
| 628 | |
| 629 | return freq; |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * wl1273_fm_upload_firmware_patch() - Upload the firmware. |
| 634 | * @radio: A pointer to the device struct. |
| 635 | * |
| 636 | * The firmware file consists of arrays of bytes where the first byte |
| 637 | * gives the array length. The first byte in the file gives the |
| 638 | * number of these arrays. |
| 639 | */ |
| 640 | static int wl1273_fm_upload_firmware_patch(struct wl1273_device *radio) |
| 641 | { |
| 642 | struct wl1273_core *core = radio->core; |
| 643 | unsigned int packet_num; |
| 644 | const struct firmware *fw_p; |
| 645 | const char *fw_name = "radio-wl1273-fw.bin"; |
| 646 | struct device *dev = radio->dev; |
| 647 | __u8 *ptr; |
Mauro Carvalho Chehab | fce50ac | 2010-12-27 11:44:50 -0300 | [diff] [blame] | 648 | int r; |
Matti Aaltonen | 87d1a50 | 2010-12-10 11:41:34 -0300 | [diff] [blame] | 649 | |
| 650 | dev_dbg(dev, "%s:\n", __func__); |
| 651 | |
| 652 | /* |
| 653 | * Uploading the firmware patch is not always necessary, |
| 654 | * so we only print an info message. |
| 655 | */ |
| 656 | if (request_firmware(&fw_p, fw_name, dev)) { |
| 657 | dev_info(dev, "%s - %s not found\n", __func__, fw_name); |
| 658 | |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | ptr = (__u8 *) fw_p->data; |
| 663 | packet_num = ptr[0]; |
| 664 | dev_dbg(dev, "%s: packets: %d\n", __func__, packet_num); |
| 665 | |
| 666 | r = wl1273_fm_write_fw(core, ptr + 1, packet_num); |
| 667 | if (r) { |
| 668 | dev_err(dev, "FW upload error: %d\n", r); |
| 669 | goto out; |
| 670 | } |
| 671 | |
| 672 | /* ignore possible error here */ |
| 673 | wl1273_fm_write_cmd(core, WL1273_RESET, 0); |
| 674 | |
Matti Aaltonen | 87d1a50 | 2010-12-10 11:41:34 -0300 | [diff] [blame] | 675 | dev_dbg(dev, "%s - download OK, r: %d\n", __func__, r); |
| 676 | out: |
| 677 | release_firmware(fw_p); |
| 678 | return r; |
| 679 | } |
| 680 | |
| 681 | static int wl1273_fm_stop(struct wl1273_device *radio) |
| 682 | { |
| 683 | struct wl1273_core *core = radio->core; |
| 684 | |
| 685 | if (core->mode == WL1273_MODE_RX) { |
| 686 | int r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, |
| 687 | WL1273_POWER_SET_OFF); |
| 688 | if (r) |
| 689 | dev_err(radio->dev, "%s: POWER_SET fails: %d\n", |
| 690 | __func__, r); |
| 691 | } else if (core->mode == WL1273_MODE_TX) { |
| 692 | int r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET, |
| 693 | WL1273_PUPD_SET_OFF); |
| 694 | if (r) |
| 695 | dev_err(radio->dev, |
| 696 | "%s: PUPD_SET fails: %d\n", __func__, r); |
| 697 | } |
| 698 | |
| 699 | if (core->pdata->disable) { |
| 700 | core->pdata->disable(); |
| 701 | dev_dbg(radio->dev, "Back to reset\n"); |
| 702 | } |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static int wl1273_fm_start(struct wl1273_device *radio, int new_mode) |
| 708 | { |
| 709 | struct wl1273_core *core = radio->core; |
| 710 | struct wl1273_fm_platform_data *pdata = core->pdata; |
| 711 | struct device *dev = radio->dev; |
| 712 | int r = -EINVAL; |
| 713 | |
| 714 | if (pdata->enable && core->mode == WL1273_MODE_OFF) { |
| 715 | dev_dbg(radio->dev, "Out of reset\n"); |
| 716 | |
| 717 | pdata->enable(); |
| 718 | msleep(250); |
| 719 | } |
| 720 | |
| 721 | if (new_mode == WL1273_MODE_RX) { |
| 722 | u16 val = WL1273_POWER_SET_FM; |
| 723 | |
| 724 | if (radio->rds_on) |
| 725 | val |= WL1273_POWER_SET_RDS; |
| 726 | |
| 727 | /* If this fails try again */ |
| 728 | r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, val); |
| 729 | if (r) { |
| 730 | msleep(100); |
| 731 | |
| 732 | r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, val); |
| 733 | if (r) { |
| 734 | dev_err(dev, "%s: POWER_SET fails\n", __func__); |
| 735 | goto fail; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | /* rds buffer configuration */ |
| 740 | radio->wr_index = 0; |
| 741 | radio->rd_index = 0; |
| 742 | |
| 743 | } else if (new_mode == WL1273_MODE_TX) { |
| 744 | /* If this fails try again once */ |
| 745 | r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET, |
| 746 | WL1273_PUPD_SET_ON); |
| 747 | if (r) { |
| 748 | msleep(100); |
| 749 | r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET, |
| 750 | WL1273_PUPD_SET_ON); |
| 751 | if (r) { |
| 752 | dev_err(dev, "%s: PUPD_SET fails\n", __func__); |
| 753 | goto fail; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | if (radio->rds_on) |
| 758 | r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 1); |
| 759 | else |
| 760 | r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 0); |
| 761 | } else { |
| 762 | dev_warn(dev, "%s: Illegal mode.\n", __func__); |
| 763 | } |
| 764 | |
| 765 | if (core->mode == WL1273_MODE_OFF) { |
| 766 | r = wl1273_fm_upload_firmware_patch(radio); |
| 767 | if (r) |
| 768 | dev_warn(dev, "Firmware upload failed.\n"); |
| 769 | |
| 770 | /* |
| 771 | * Sometimes the chip is in a wrong power state at this point. |
| 772 | * So we set the power once again. |
| 773 | */ |
| 774 | if (new_mode == WL1273_MODE_RX) { |
| 775 | u16 val = WL1273_POWER_SET_FM; |
| 776 | |
| 777 | if (radio->rds_on) |
| 778 | val |= WL1273_POWER_SET_RDS; |
| 779 | |
| 780 | r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, val); |
| 781 | if (r) { |
| 782 | dev_err(dev, "%s: POWER_SET fails\n", __func__); |
| 783 | goto fail; |
| 784 | } |
| 785 | } else if (new_mode == WL1273_MODE_TX) { |
| 786 | r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET, |
| 787 | WL1273_PUPD_SET_ON); |
| 788 | if (r) { |
| 789 | dev_err(dev, "%s: PUPD_SET fails\n", __func__); |
| 790 | goto fail; |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | return 0; |
| 796 | fail: |
| 797 | if (pdata->disable) |
| 798 | pdata->disable(); |
| 799 | |
| 800 | dev_dbg(dev, "%s: return: %d\n", __func__, r); |
| 801 | return r; |
| 802 | } |
| 803 | |
| 804 | static int wl1273_fm_suspend(struct wl1273_device *radio) |
| 805 | { |
| 806 | struct wl1273_core *core = radio->core; |
| 807 | int r = 0; |
| 808 | |
| 809 | /* Cannot go from OFF to SUSPENDED */ |
| 810 | if (core->mode == WL1273_MODE_RX) |
| 811 | r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, |
| 812 | WL1273_POWER_SET_RETENTION); |
| 813 | else if (core->mode == WL1273_MODE_TX) |
| 814 | r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET, |
| 815 | WL1273_PUPD_SET_RETENTION); |
| 816 | else |
| 817 | r = -EINVAL; |
| 818 | |
| 819 | if (r) { |
| 820 | dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r); |
| 821 | goto out; |
| 822 | } |
| 823 | |
| 824 | out: |
| 825 | return r; |
| 826 | } |
| 827 | |
| 828 | static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode) |
| 829 | { |
| 830 | struct wl1273_core *core = radio->core; |
| 831 | struct device *dev = radio->dev; |
| 832 | int old_mode; |
| 833 | int r; |
| 834 | |
| 835 | dev_dbg(dev, "%s\n", __func__); |
| 836 | dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden); |
| 837 | |
| 838 | old_mode = core->mode; |
| 839 | if (mode & radio->forbidden) { |
| 840 | r = -EPERM; |
| 841 | goto out; |
| 842 | } |
| 843 | |
| 844 | switch (mode) { |
| 845 | case WL1273_MODE_RX: |
| 846 | case WL1273_MODE_TX: |
| 847 | r = wl1273_fm_start(radio, mode); |
| 848 | if (r) { |
| 849 | dev_err(dev, "%s: Cannot start.\n", __func__); |
| 850 | wl1273_fm_stop(radio); |
| 851 | goto out; |
| 852 | } |
| 853 | |
| 854 | core->mode = mode; |
| 855 | r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, |
| 856 | radio->irq_flags); |
| 857 | if (r) { |
| 858 | dev_err(dev, "INT_MASK_SET fails.\n"); |
| 859 | goto out; |
| 860 | } |
| 861 | |
| 862 | /* remember previous settings */ |
| 863 | if (mode == WL1273_MODE_RX) { |
| 864 | r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency); |
| 865 | if (r) { |
| 866 | dev_err(dev, "set freq fails: %d.\n", r); |
| 867 | goto out; |
| 868 | } |
| 869 | |
| 870 | r = core->set_volume(core, core->volume); |
| 871 | if (r) { |
| 872 | dev_err(dev, "set volume fails: %d.\n", r); |
| 873 | goto out; |
| 874 | } |
| 875 | |
| 876 | dev_dbg(dev, "%s: Set vol: %d.\n", __func__, |
| 877 | core->volume); |
| 878 | } else { |
| 879 | r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency); |
| 880 | if (r) { |
| 881 | dev_err(dev, "set freq fails: %d.\n", r); |
| 882 | goto out; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__); |
| 887 | |
| 888 | r = core->set_audio(core, core->audio_mode); |
| 889 | if (r) |
| 890 | dev_err(dev, "Cannot set audio mode.\n"); |
| 891 | break; |
| 892 | |
| 893 | case WL1273_MODE_OFF: |
| 894 | r = wl1273_fm_stop(radio); |
| 895 | if (r) |
| 896 | dev_err(dev, "%s: Off fails: %d\n", __func__, r); |
| 897 | else |
| 898 | core->mode = WL1273_MODE_OFF; |
| 899 | |
| 900 | break; |
| 901 | |
| 902 | case WL1273_MODE_SUSPENDED: |
| 903 | r = wl1273_fm_suspend(radio); |
| 904 | if (r) |
| 905 | dev_err(dev, "%s: Suspend fails: %d\n", __func__, r); |
| 906 | else |
| 907 | core->mode = WL1273_MODE_SUSPENDED; |
| 908 | |
| 909 | break; |
| 910 | |
| 911 | default: |
| 912 | dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode); |
| 913 | r = -EINVAL; |
| 914 | break; |
| 915 | } |
| 916 | out: |
| 917 | if (r) |
| 918 | core->mode = old_mode; |
| 919 | |
| 920 | return r; |
| 921 | } |
| 922 | |
| 923 | static int wl1273_fm_set_seek(struct wl1273_device *radio, |
| 924 | unsigned int wrap_around, |
| 925 | unsigned int seek_upward, |
| 926 | int level) |
| 927 | { |
| 928 | struct wl1273_core *core = radio->core; |
| 929 | int r = 0; |
| 930 | unsigned int dir = (seek_upward == 0) ? 0 : 1; |
| 931 | unsigned int f; |
| 932 | |
| 933 | f = radio->rx_frequency; |
| 934 | dev_dbg(radio->dev, "rx_frequency: %d\n", f); |
| 935 | |
| 936 | if (dir && f + radio->spacing <= radio->rangehigh) |
| 937 | r = wl1273_fm_set_rx_freq(radio, f + radio->spacing); |
| 938 | else if (dir && wrap_around) |
| 939 | r = wl1273_fm_set_rx_freq(radio, radio->rangelow); |
| 940 | else if (f - radio->spacing >= radio->rangelow) |
| 941 | r = wl1273_fm_set_rx_freq(radio, f - radio->spacing); |
| 942 | else if (wrap_around) |
| 943 | r = wl1273_fm_set_rx_freq(radio, radio->rangehigh); |
| 944 | |
| 945 | if (r) |
| 946 | goto out; |
| 947 | |
| 948 | if (level < SCHAR_MIN || level > SCHAR_MAX) |
| 949 | return -EINVAL; |
| 950 | |
| 951 | INIT_COMPLETION(radio->busy); |
| 952 | dev_dbg(radio->dev, "%s: BUSY\n", __func__); |
| 953 | |
| 954 | r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, radio->irq_flags); |
| 955 | if (r) |
| 956 | goto out; |
| 957 | |
| 958 | dev_dbg(radio->dev, "%s\n", __func__); |
| 959 | |
| 960 | r = wl1273_fm_write_cmd(core, WL1273_SEARCH_LVL_SET, level); |
| 961 | if (r) |
| 962 | goto out; |
| 963 | |
| 964 | r = wl1273_fm_write_cmd(core, WL1273_SEARCH_DIR_SET, dir); |
| 965 | if (r) |
| 966 | goto out; |
| 967 | |
| 968 | r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET, |
| 969 | TUNER_MODE_AUTO_SEEK); |
| 970 | if (r) |
| 971 | goto out; |
| 972 | |
| 973 | wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000)); |
| 974 | if (!(radio->irq_received & WL1273_BL_EVENT)) |
| 975 | goto out; |
| 976 | |
| 977 | radio->irq_received &= ~WL1273_BL_EVENT; |
| 978 | |
| 979 | if (!wrap_around) |
| 980 | goto out; |
| 981 | |
| 982 | /* Wrap around */ |
| 983 | dev_dbg(radio->dev, "Wrap around in HW seek.\n"); |
| 984 | |
| 985 | if (seek_upward) |
| 986 | f = radio->rangelow; |
| 987 | else |
| 988 | f = radio->rangehigh; |
| 989 | |
| 990 | r = wl1273_fm_set_rx_freq(radio, f); |
| 991 | if (r) |
| 992 | goto out; |
| 993 | |
| 994 | INIT_COMPLETION(radio->busy); |
| 995 | dev_dbg(radio->dev, "%s: BUSY\n", __func__); |
| 996 | |
| 997 | r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET, |
| 998 | TUNER_MODE_AUTO_SEEK); |
| 999 | if (r) |
| 1000 | goto out; |
| 1001 | |
| 1002 | wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000)); |
| 1003 | out: |
| 1004 | dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r); |
| 1005 | return r; |
| 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * wl1273_fm_get_tx_ctune() - Get the TX tuning capacitor value. |
| 1010 | * @radio: A pointer to the device struct. |
| 1011 | */ |
| 1012 | static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio) |
| 1013 | { |
| 1014 | struct wl1273_core *core = radio->core; |
| 1015 | struct device *dev = radio->dev; |
| 1016 | u16 val; |
| 1017 | int r; |
| 1018 | |
| 1019 | if (core->mode == WL1273_MODE_OFF || |
| 1020 | core->mode == WL1273_MODE_SUSPENDED) |
| 1021 | return -EPERM; |
| 1022 | |
| 1023 | r = wl1273_fm_read_reg(core, WL1273_READ_FMANT_TUNE_VALUE, &val); |
| 1024 | if (r) { |
| 1025 | dev_err(dev, "%s: read error: %d\n", __func__, r); |
| 1026 | goto out; |
| 1027 | } |
| 1028 | |
| 1029 | out: |
| 1030 | return val; |
| 1031 | } |
| 1032 | |
| 1033 | /** |
| 1034 | * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value. |
| 1035 | * @radio: A pointer to the device struct. |
| 1036 | * @preemphasis: The new pre-amphasis value. |
| 1037 | * |
| 1038 | * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED, |
| 1039 | * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS. |
| 1040 | */ |
| 1041 | static int wl1273_fm_set_preemphasis(struct wl1273_device *radio, |
| 1042 | unsigned int preemphasis) |
| 1043 | { |
| 1044 | struct wl1273_core *core = radio->core; |
| 1045 | int r; |
| 1046 | u16 em; |
| 1047 | |
| 1048 | if (core->mode == WL1273_MODE_OFF || |
| 1049 | core->mode == WL1273_MODE_SUSPENDED) |
| 1050 | return -EPERM; |
| 1051 | |
| 1052 | mutex_lock(&core->lock); |
| 1053 | |
| 1054 | switch (preemphasis) { |
| 1055 | case V4L2_PREEMPHASIS_DISABLED: |
| 1056 | em = 1; |
| 1057 | break; |
| 1058 | case V4L2_PREEMPHASIS_50_uS: |
| 1059 | em = 0; |
| 1060 | break; |
| 1061 | case V4L2_PREEMPHASIS_75_uS: |
| 1062 | em = 2; |
| 1063 | break; |
| 1064 | default: |
| 1065 | r = -EINVAL; |
| 1066 | goto out; |
| 1067 | } |
| 1068 | |
| 1069 | r = wl1273_fm_write_cmd(core, WL1273_PREMPH_SET, em); |
| 1070 | if (r) |
| 1071 | goto out; |
| 1072 | |
| 1073 | radio->preemphasis = preemphasis; |
| 1074 | |
| 1075 | out: |
| 1076 | mutex_unlock(&core->lock); |
| 1077 | return r; |
| 1078 | } |
| 1079 | |
| 1080 | static int wl1273_fm_rds_on(struct wl1273_device *radio) |
| 1081 | { |
| 1082 | struct wl1273_core *core = radio->core; |
| 1083 | int r; |
| 1084 | |
| 1085 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1086 | if (radio->rds_on) |
| 1087 | return 0; |
| 1088 | |
| 1089 | r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, |
| 1090 | WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS); |
| 1091 | if (r) |
| 1092 | goto out; |
| 1093 | |
| 1094 | r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency); |
| 1095 | if (r) |
| 1096 | dev_err(radio->dev, "set freq fails: %d.\n", r); |
| 1097 | out: |
| 1098 | return r; |
| 1099 | } |
| 1100 | |
| 1101 | static int wl1273_fm_rds_off(struct wl1273_device *radio) |
| 1102 | { |
| 1103 | struct wl1273_core *core = radio->core; |
| 1104 | int r; |
| 1105 | |
| 1106 | if (!radio->rds_on) |
| 1107 | return 0; |
| 1108 | |
| 1109 | radio->irq_flags &= ~WL1273_RDS_EVENT; |
| 1110 | |
| 1111 | r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, radio->irq_flags); |
| 1112 | if (r) |
| 1113 | goto out; |
| 1114 | |
| 1115 | /* stop rds reception */ |
| 1116 | cancel_delayed_work(&radio->work); |
| 1117 | |
| 1118 | /* Service pending read */ |
| 1119 | wake_up_interruptible(&radio->read_queue); |
| 1120 | |
| 1121 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1122 | |
| 1123 | r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, WL1273_POWER_SET_FM); |
| 1124 | if (r) |
| 1125 | goto out; |
| 1126 | |
| 1127 | r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency); |
| 1128 | if (r) |
| 1129 | dev_err(radio->dev, "set freq fails: %d.\n", r); |
| 1130 | out: |
| 1131 | dev_dbg(radio->dev, "%s: exiting...\n", __func__); |
| 1132 | |
| 1133 | return r; |
| 1134 | } |
| 1135 | |
| 1136 | static int wl1273_fm_set_rds(struct wl1273_device *radio, unsigned int new_mode) |
| 1137 | { |
| 1138 | int r = 0; |
| 1139 | struct wl1273_core *core = radio->core; |
| 1140 | |
| 1141 | if (core->mode == WL1273_MODE_OFF || |
| 1142 | core->mode == WL1273_MODE_SUSPENDED) |
| 1143 | return -EPERM; |
| 1144 | |
| 1145 | if (new_mode == WL1273_RDS_RESET) { |
| 1146 | r = wl1273_fm_write_cmd(core, WL1273_RDS_CNTRL_SET, 1); |
| 1147 | return r; |
| 1148 | } |
| 1149 | |
| 1150 | if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) { |
| 1151 | r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 0); |
| 1152 | } else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) { |
| 1153 | r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 1); |
| 1154 | } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) { |
| 1155 | r = wl1273_fm_rds_off(radio); |
| 1156 | } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) { |
| 1157 | r = wl1273_fm_rds_on(radio); |
| 1158 | } else { |
| 1159 | dev_err(radio->dev, "%s: Unknown mode: %d\n", |
| 1160 | __func__, new_mode); |
| 1161 | r = -EINVAL; |
| 1162 | } |
| 1163 | |
| 1164 | if (!r) |
| 1165 | radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false; |
| 1166 | |
| 1167 | return r; |
| 1168 | } |
| 1169 | |
| 1170 | static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf, |
| 1171 | size_t count, loff_t *ppos) |
| 1172 | { |
| 1173 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1174 | u16 val; |
| 1175 | int r; |
| 1176 | |
| 1177 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1178 | |
| 1179 | if (radio->core->mode != WL1273_MODE_TX) |
| 1180 | return count; |
| 1181 | |
| 1182 | if (radio->rds_users == 0) { |
| 1183 | dev_warn(radio->dev, "%s: RDS not on.\n", __func__); |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | if (mutex_lock_interruptible(&radio->core->lock)) |
| 1188 | return -EINTR; |
| 1189 | /* |
| 1190 | * Multiple processes can open the device, but only |
| 1191 | * one gets to write to it. |
| 1192 | */ |
| 1193 | if (radio->owner && radio->owner != file) { |
| 1194 | r = -EBUSY; |
| 1195 | goto out; |
| 1196 | } |
| 1197 | radio->owner = file; |
| 1198 | |
| 1199 | /* Manual Mode */ |
| 1200 | if (count > 255) |
| 1201 | val = 255; |
| 1202 | else |
| 1203 | val = count; |
| 1204 | |
| 1205 | wl1273_fm_write_cmd(radio->core, WL1273_RDS_CONFIG_DATA_SET, val); |
| 1206 | |
| 1207 | if (copy_from_user(radio->write_buf + 1, buf, val)) { |
| 1208 | r = -EFAULT; |
| 1209 | goto out; |
| 1210 | } |
| 1211 | |
| 1212 | dev_dbg(radio->dev, "Count: %d\n", val); |
| 1213 | dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf); |
| 1214 | |
| 1215 | radio->write_buf[0] = WL1273_RDS_DATA_SET; |
| 1216 | wl1273_fm_write_data(radio->core, radio->write_buf, val + 1); |
| 1217 | |
| 1218 | r = val; |
| 1219 | out: |
| 1220 | mutex_unlock(&radio->core->lock); |
| 1221 | |
| 1222 | return r; |
| 1223 | } |
| 1224 | |
| 1225 | static unsigned int wl1273_fm_fops_poll(struct file *file, |
| 1226 | struct poll_table_struct *pts) |
| 1227 | { |
| 1228 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1229 | struct wl1273_core *core = radio->core; |
| 1230 | |
| 1231 | if (radio->owner && radio->owner != file) |
| 1232 | return -EBUSY; |
| 1233 | |
| 1234 | radio->owner = file; |
| 1235 | |
| 1236 | if (core->mode == WL1273_MODE_RX) { |
| 1237 | poll_wait(file, &radio->read_queue, pts); |
| 1238 | |
| 1239 | if (radio->rd_index != radio->wr_index) |
| 1240 | return POLLIN | POLLRDNORM; |
| 1241 | |
| 1242 | } else if (core->mode == WL1273_MODE_TX) { |
| 1243 | return POLLOUT | POLLWRNORM; |
| 1244 | } |
| 1245 | |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | static int wl1273_fm_fops_open(struct file *file) |
| 1250 | { |
| 1251 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1252 | struct wl1273_core *core = radio->core; |
| 1253 | int r = 0; |
| 1254 | |
| 1255 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1256 | |
| 1257 | if (core->mode == WL1273_MODE_RX && radio->rds_on && |
| 1258 | !radio->rds_users) { |
| 1259 | dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode); |
| 1260 | |
| 1261 | if (mutex_lock_interruptible(&core->lock)) |
| 1262 | return -EINTR; |
| 1263 | |
| 1264 | radio->irq_flags |= WL1273_RDS_EVENT; |
| 1265 | |
| 1266 | r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, |
| 1267 | radio->irq_flags); |
| 1268 | if (r) { |
| 1269 | mutex_unlock(&core->lock); |
| 1270 | goto out; |
| 1271 | } |
| 1272 | |
| 1273 | radio->rds_users++; |
| 1274 | |
| 1275 | mutex_unlock(&core->lock); |
| 1276 | } |
| 1277 | out: |
| 1278 | return r; |
| 1279 | } |
| 1280 | |
| 1281 | static int wl1273_fm_fops_release(struct file *file) |
| 1282 | { |
| 1283 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1284 | struct wl1273_core *core = radio->core; |
| 1285 | int r = 0; |
| 1286 | |
| 1287 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1288 | |
| 1289 | if (radio->rds_users > 0) { |
| 1290 | radio->rds_users--; |
| 1291 | if (radio->rds_users == 0) { |
| 1292 | if (mutex_lock_interruptible(&core->lock)) |
| 1293 | return -EINTR; |
| 1294 | |
| 1295 | radio->irq_flags &= ~WL1273_RDS_EVENT; |
| 1296 | |
| 1297 | if (core->mode == WL1273_MODE_RX) { |
| 1298 | r = wl1273_fm_write_cmd(core, |
| 1299 | WL1273_INT_MASK_SET, |
| 1300 | radio->irq_flags); |
| 1301 | if (r) { |
| 1302 | mutex_unlock(&core->lock); |
| 1303 | goto out; |
| 1304 | } |
| 1305 | } |
| 1306 | mutex_unlock(&core->lock); |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | if (file == radio->owner) |
| 1311 | radio->owner = NULL; |
| 1312 | out: |
| 1313 | return r; |
| 1314 | } |
| 1315 | |
| 1316 | static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf, |
| 1317 | size_t count, loff_t *ppos) |
| 1318 | { |
| 1319 | int r = 0; |
| 1320 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1321 | struct wl1273_core *core = radio->core; |
| 1322 | unsigned int block_count = 0; |
| 1323 | u16 val; |
| 1324 | |
| 1325 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1326 | |
| 1327 | if (radio->core->mode != WL1273_MODE_RX) |
| 1328 | return 0; |
| 1329 | |
| 1330 | if (radio->rds_users == 0) { |
| 1331 | dev_warn(radio->dev, "%s: RDS not on.\n", __func__); |
| 1332 | return 0; |
| 1333 | } |
| 1334 | |
| 1335 | if (mutex_lock_interruptible(&core->lock)) |
| 1336 | return -EINTR; |
| 1337 | |
| 1338 | /* |
| 1339 | * Multiple processes can open the device, but only |
| 1340 | * one at a time gets read access. |
| 1341 | */ |
| 1342 | if (radio->owner && radio->owner != file) { |
| 1343 | r = -EBUSY; |
| 1344 | goto out; |
| 1345 | } |
| 1346 | radio->owner = file; |
| 1347 | |
| 1348 | r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val); |
| 1349 | if (r) { |
| 1350 | dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__); |
| 1351 | goto out; |
| 1352 | } else if (val == 0) { |
| 1353 | dev_info(radio->dev, "RDS_SYNC: Not synchronized\n"); |
| 1354 | r = -ENODATA; |
| 1355 | goto out; |
| 1356 | } |
| 1357 | |
| 1358 | /* block if no new data available */ |
| 1359 | while (radio->wr_index == radio->rd_index) { |
| 1360 | if (file->f_flags & O_NONBLOCK) { |
| 1361 | r = -EWOULDBLOCK; |
| 1362 | goto out; |
| 1363 | } |
| 1364 | |
| 1365 | dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__); |
| 1366 | if (wait_event_interruptible(radio->read_queue, |
| 1367 | radio->wr_index != |
| 1368 | radio->rd_index) < 0) { |
| 1369 | r = -EINTR; |
| 1370 | goto out; |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | /* calculate block count from byte count */ |
| 1375 | count /= RDS_BLOCK_SIZE; |
| 1376 | |
| 1377 | /* copy RDS blocks from the internal buffer and to user buffer */ |
| 1378 | while (block_count < count) { |
| 1379 | if (radio->rd_index == radio->wr_index) |
| 1380 | break; |
| 1381 | |
| 1382 | /* always transfer complete RDS blocks */ |
| 1383 | if (copy_to_user(buf, &radio->buffer[radio->rd_index], |
| 1384 | RDS_BLOCK_SIZE)) |
| 1385 | break; |
| 1386 | |
| 1387 | /* increment and wrap the read pointer */ |
| 1388 | radio->rd_index += RDS_BLOCK_SIZE; |
| 1389 | if (radio->rd_index >= radio->buf_size) |
| 1390 | radio->rd_index = 0; |
| 1391 | |
| 1392 | /* increment counters */ |
| 1393 | block_count++; |
| 1394 | buf += RDS_BLOCK_SIZE; |
| 1395 | r += RDS_BLOCK_SIZE; |
| 1396 | } |
| 1397 | |
| 1398 | out: |
| 1399 | dev_dbg(radio->dev, "%s: exit\n", __func__); |
| 1400 | mutex_unlock(&core->lock); |
| 1401 | |
| 1402 | return r; |
| 1403 | } |
| 1404 | |
| 1405 | static const struct v4l2_file_operations wl1273_fops = { |
| 1406 | .owner = THIS_MODULE, |
| 1407 | .read = wl1273_fm_fops_read, |
| 1408 | .write = wl1273_fm_fops_write, |
| 1409 | .poll = wl1273_fm_fops_poll, |
Matti Aaltonen | 34b8fc8 | 2011-01-04 08:29:37 -0300 | [diff] [blame] | 1410 | .unlocked_ioctl = video_ioctl2, |
Matti Aaltonen | 87d1a50 | 2010-12-10 11:41:34 -0300 | [diff] [blame] | 1411 | .open = wl1273_fm_fops_open, |
| 1412 | .release = wl1273_fm_fops_release, |
| 1413 | }; |
| 1414 | |
| 1415 | static int wl1273_fm_vidioc_querycap(struct file *file, void *priv, |
| 1416 | struct v4l2_capability *capability) |
| 1417 | { |
| 1418 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1419 | |
| 1420 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1421 | |
| 1422 | strlcpy(capability->driver, WL1273_FM_DRIVER_NAME, |
| 1423 | sizeof(capability->driver)); |
| 1424 | strlcpy(capability->card, "Texas Instruments Wl1273 FM Radio", |
| 1425 | sizeof(capability->card)); |
| 1426 | strlcpy(capability->bus_info, radio->bus_type, |
| 1427 | sizeof(capability->bus_info)); |
| 1428 | |
| 1429 | capability->capabilities = V4L2_CAP_HW_FREQ_SEEK | |
| 1430 | V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_AUDIO | |
| 1431 | V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR | |
| 1432 | V4L2_CAP_RDS_OUTPUT; |
| 1433 | |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
| 1437 | static int wl1273_fm_vidioc_g_input(struct file *file, void *priv, |
| 1438 | unsigned int *i) |
| 1439 | { |
| 1440 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1441 | |
| 1442 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1443 | |
| 1444 | *i = 0; |
| 1445 | |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
| 1449 | static int wl1273_fm_vidioc_s_input(struct file *file, void *priv, |
| 1450 | unsigned int i) |
| 1451 | { |
| 1452 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1453 | |
| 1454 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1455 | |
| 1456 | if (i != 0) |
| 1457 | return -EINVAL; |
| 1458 | |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | /** |
| 1463 | * wl1273_fm_set_tx_power() - Set the transmission power value. |
| 1464 | * @core: A pointer to the device struct. |
| 1465 | * @power: The new power value. |
| 1466 | */ |
| 1467 | static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power) |
| 1468 | { |
| 1469 | int r; |
| 1470 | |
| 1471 | if (radio->core->mode == WL1273_MODE_OFF || |
| 1472 | radio->core->mode == WL1273_MODE_SUSPENDED) |
| 1473 | return -EPERM; |
| 1474 | |
| 1475 | mutex_lock(&radio->core->lock); |
| 1476 | |
| 1477 | /* Convert the dBuV value to chip presentation */ |
| 1478 | r = wl1273_fm_write_cmd(radio->core, WL1273_POWER_LEV_SET, 122 - power); |
| 1479 | if (r) |
| 1480 | goto out; |
| 1481 | |
| 1482 | radio->tx_power = power; |
| 1483 | |
| 1484 | out: |
| 1485 | mutex_unlock(&radio->core->lock); |
| 1486 | return r; |
| 1487 | } |
| 1488 | |
| 1489 | #define WL1273_SPACING_50kHz 1 |
| 1490 | #define WL1273_SPACING_100kHz 2 |
| 1491 | #define WL1273_SPACING_200kHz 4 |
| 1492 | |
| 1493 | static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio, |
| 1494 | unsigned int spacing) |
| 1495 | { |
| 1496 | int r; |
| 1497 | |
| 1498 | if (spacing == 0) { |
| 1499 | r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET, |
| 1500 | WL1273_SPACING_100kHz); |
| 1501 | radio->spacing = 100; |
| 1502 | } else if (spacing - 50000 < 25000) { |
| 1503 | r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET, |
| 1504 | WL1273_SPACING_50kHz); |
| 1505 | radio->spacing = 50; |
| 1506 | } else if (spacing - 100000 < 50000) { |
| 1507 | r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET, |
| 1508 | WL1273_SPACING_100kHz); |
| 1509 | radio->spacing = 100; |
| 1510 | } else { |
| 1511 | r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET, |
| 1512 | WL1273_SPACING_200kHz); |
| 1513 | radio->spacing = 200; |
| 1514 | } |
| 1515 | |
| 1516 | return r; |
| 1517 | } |
| 1518 | |
| 1519 | static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl) |
| 1520 | { |
| 1521 | struct wl1273_device *radio = ctrl->priv; |
| 1522 | struct wl1273_core *core = radio->core; |
| 1523 | |
| 1524 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1525 | |
| 1526 | if (mutex_lock_interruptible(&core->lock)) |
| 1527 | return -EINTR; |
| 1528 | |
| 1529 | switch (ctrl->id) { |
| 1530 | case V4L2_CID_TUNE_ANTENNA_CAPACITOR: |
| 1531 | ctrl->val = wl1273_fm_get_tx_ctune(radio); |
| 1532 | break; |
| 1533 | |
| 1534 | default: |
| 1535 | dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n", |
| 1536 | __func__, ctrl->id); |
| 1537 | break; |
| 1538 | } |
| 1539 | |
| 1540 | mutex_unlock(&core->lock); |
| 1541 | |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
| 1545 | #define WL1273_MUTE_SOFT_ENABLE (1 << 0) |
| 1546 | #define WL1273_MUTE_AC (1 << 1) |
| 1547 | #define WL1273_MUTE_HARD_LEFT (1 << 2) |
| 1548 | #define WL1273_MUTE_HARD_RIGHT (1 << 3) |
| 1549 | #define WL1273_MUTE_SOFT_FORCE (1 << 4) |
| 1550 | |
| 1551 | static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl) |
| 1552 | { |
| 1553 | return container_of(ctrl->handler, struct wl1273_device, ctrl_handler); |
| 1554 | } |
| 1555 | |
| 1556 | static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl) |
| 1557 | { |
| 1558 | struct wl1273_device *radio = to_radio(ctrl); |
| 1559 | struct wl1273_core *core = radio->core; |
| 1560 | int r = 0; |
| 1561 | |
| 1562 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1563 | |
| 1564 | switch (ctrl->id) { |
| 1565 | case V4L2_CID_AUDIO_MUTE: |
| 1566 | if (mutex_lock_interruptible(&core->lock)) |
| 1567 | return -EINTR; |
| 1568 | |
| 1569 | if (core->mode == WL1273_MODE_RX && ctrl->val) |
| 1570 | r = wl1273_fm_write_cmd(core, |
| 1571 | WL1273_MUTE_STATUS_SET, |
| 1572 | WL1273_MUTE_HARD_LEFT | |
| 1573 | WL1273_MUTE_HARD_RIGHT); |
| 1574 | else if (core->mode == WL1273_MODE_RX) |
| 1575 | r = wl1273_fm_write_cmd(core, |
| 1576 | WL1273_MUTE_STATUS_SET, 0x0); |
| 1577 | else if (core->mode == WL1273_MODE_TX && ctrl->val) |
| 1578 | r = wl1273_fm_write_cmd(core, WL1273_MUTE, 1); |
| 1579 | else if (core->mode == WL1273_MODE_TX) |
| 1580 | r = wl1273_fm_write_cmd(core, WL1273_MUTE, 0); |
| 1581 | |
| 1582 | mutex_unlock(&core->lock); |
| 1583 | break; |
| 1584 | |
| 1585 | case V4L2_CID_AUDIO_VOLUME: |
| 1586 | if (ctrl->val == 0) |
| 1587 | r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF); |
| 1588 | else |
| 1589 | r = core->set_volume(core, core->volume); |
| 1590 | break; |
| 1591 | |
| 1592 | case V4L2_CID_TUNE_PREEMPHASIS: |
| 1593 | r = wl1273_fm_set_preemphasis(radio, ctrl->val); |
| 1594 | break; |
| 1595 | |
| 1596 | case V4L2_CID_TUNE_POWER_LEVEL: |
| 1597 | r = wl1273_fm_set_tx_power(radio, ctrl->val); |
| 1598 | break; |
| 1599 | |
| 1600 | default: |
| 1601 | dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n", |
| 1602 | __func__, ctrl->id); |
| 1603 | break; |
| 1604 | } |
| 1605 | |
| 1606 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1607 | return r; |
| 1608 | } |
| 1609 | |
| 1610 | static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv, |
| 1611 | struct v4l2_audio *audio) |
| 1612 | { |
| 1613 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1614 | |
| 1615 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1616 | |
| 1617 | if (audio->index > 1) |
| 1618 | return -EINVAL; |
| 1619 | |
| 1620 | strlcpy(audio->name, "Radio", sizeof(audio->name)); |
| 1621 | audio->capability = V4L2_AUDCAP_STEREO; |
| 1622 | |
| 1623 | return 0; |
| 1624 | } |
| 1625 | |
| 1626 | static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv, |
| 1627 | struct v4l2_audio *audio) |
| 1628 | { |
| 1629 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1630 | |
| 1631 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1632 | |
| 1633 | if (audio->index != 0) |
| 1634 | return -EINVAL; |
| 1635 | |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
| 1639 | #define WL1273_RDS_NOT_SYNCHRONIZED 0 |
| 1640 | #define WL1273_RDS_SYNCHRONIZED 1 |
| 1641 | |
| 1642 | static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv, |
| 1643 | struct v4l2_tuner *tuner) |
| 1644 | { |
| 1645 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1646 | struct wl1273_core *core = radio->core; |
| 1647 | u16 val; |
| 1648 | int r; |
| 1649 | |
| 1650 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1651 | |
| 1652 | if (tuner->index > 0) |
| 1653 | return -EINVAL; |
| 1654 | |
| 1655 | strlcpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name)); |
| 1656 | tuner->type = V4L2_TUNER_RADIO; |
| 1657 | |
| 1658 | tuner->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW); |
| 1659 | tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH); |
| 1660 | |
| 1661 | tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS | |
| 1662 | V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO; |
| 1663 | |
| 1664 | if (radio->stereo) |
| 1665 | tuner->audmode = V4L2_TUNER_MODE_STEREO; |
| 1666 | else |
| 1667 | tuner->audmode = V4L2_TUNER_MODE_MONO; |
| 1668 | |
| 1669 | if (core->mode != WL1273_MODE_RX) |
| 1670 | return 0; |
| 1671 | |
| 1672 | if (mutex_lock_interruptible(&core->lock)) |
| 1673 | return -EINTR; |
| 1674 | |
| 1675 | r = wl1273_fm_read_reg(core, WL1273_STEREO_GET, &val); |
| 1676 | if (r) |
| 1677 | goto out; |
| 1678 | |
| 1679 | if (val == 1) |
| 1680 | tuner->rxsubchans = V4L2_TUNER_SUB_STEREO; |
| 1681 | else |
| 1682 | tuner->rxsubchans = V4L2_TUNER_SUB_MONO; |
| 1683 | |
| 1684 | r = wl1273_fm_read_reg(core, WL1273_RSSI_LVL_GET, &val); |
| 1685 | if (r) |
| 1686 | goto out; |
| 1687 | |
| 1688 | tuner->signal = (s16) val; |
| 1689 | dev_dbg(radio->dev, "Signal: %d\n", tuner->signal); |
| 1690 | |
| 1691 | tuner->afc = 0; |
| 1692 | |
| 1693 | r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val); |
| 1694 | if (r) |
| 1695 | goto out; |
| 1696 | |
| 1697 | if (val == WL1273_RDS_SYNCHRONIZED) |
| 1698 | tuner->rxsubchans |= V4L2_TUNER_SUB_RDS; |
| 1699 | out: |
| 1700 | mutex_unlock(&core->lock); |
| 1701 | |
| 1702 | return r; |
| 1703 | } |
| 1704 | |
| 1705 | static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv, |
| 1706 | struct v4l2_tuner *tuner) |
| 1707 | { |
| 1708 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1709 | struct wl1273_core *core = radio->core; |
| 1710 | int r = 0; |
| 1711 | |
| 1712 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1713 | dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index); |
| 1714 | dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name); |
| 1715 | dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability); |
| 1716 | dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans); |
| 1717 | dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow); |
| 1718 | dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh); |
| 1719 | |
| 1720 | if (tuner->index > 0) |
| 1721 | return -EINVAL; |
| 1722 | |
| 1723 | if (mutex_lock_interruptible(&core->lock)) |
| 1724 | return -EINTR; |
| 1725 | |
| 1726 | r = wl1273_fm_set_mode(radio, WL1273_MODE_RX); |
| 1727 | if (r) |
| 1728 | goto out; |
| 1729 | |
| 1730 | if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS) |
| 1731 | r = wl1273_fm_set_rds(radio, WL1273_RDS_ON); |
| 1732 | else |
| 1733 | r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF); |
| 1734 | |
| 1735 | if (r) |
| 1736 | dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r); |
| 1737 | |
| 1738 | if (tuner->audmode == V4L2_TUNER_MODE_MONO) { |
| 1739 | r = wl1273_fm_write_cmd(core, WL1273_MOST_MODE_SET, |
| 1740 | WL1273_RX_MONO); |
| 1741 | if (r < 0) { |
| 1742 | dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n", |
| 1743 | __func__, r); |
| 1744 | goto out; |
| 1745 | } |
| 1746 | radio->stereo = false; |
| 1747 | } else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) { |
| 1748 | r = wl1273_fm_write_cmd(core, WL1273_MOST_MODE_SET, |
| 1749 | WL1273_RX_STEREO); |
| 1750 | if (r < 0) { |
| 1751 | dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n", |
| 1752 | __func__, r); |
| 1753 | goto out; |
| 1754 | } |
| 1755 | radio->stereo = true; |
| 1756 | } else { |
| 1757 | dev_err(radio->dev, "%s: tuner->audmode: %d\n", |
| 1758 | __func__, tuner->audmode); |
| 1759 | r = -EINVAL; |
| 1760 | goto out; |
| 1761 | } |
| 1762 | |
| 1763 | out: |
| 1764 | mutex_unlock(&core->lock); |
| 1765 | |
| 1766 | return r; |
| 1767 | } |
| 1768 | |
| 1769 | static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv, |
| 1770 | struct v4l2_frequency *freq) |
| 1771 | { |
| 1772 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1773 | struct wl1273_core *core = radio->core; |
| 1774 | |
| 1775 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1776 | |
| 1777 | if (mutex_lock_interruptible(&core->lock)) |
| 1778 | return -EINTR; |
| 1779 | |
| 1780 | freq->type = V4L2_TUNER_RADIO; |
| 1781 | freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio)); |
| 1782 | |
| 1783 | mutex_unlock(&core->lock); |
| 1784 | |
| 1785 | return 0; |
| 1786 | } |
| 1787 | |
| 1788 | static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv, |
| 1789 | struct v4l2_frequency *freq) |
| 1790 | { |
| 1791 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1792 | struct wl1273_core *core = radio->core; |
| 1793 | int r; |
| 1794 | |
| 1795 | dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency); |
| 1796 | |
| 1797 | if (freq->type != V4L2_TUNER_RADIO) { |
| 1798 | dev_dbg(radio->dev, |
| 1799 | "freq->type != V4L2_TUNER_RADIO: %d\n", freq->type); |
| 1800 | return -EINVAL; |
| 1801 | } |
| 1802 | |
| 1803 | if (mutex_lock_interruptible(&core->lock)) |
| 1804 | return -EINTR; |
| 1805 | |
| 1806 | if (core->mode == WL1273_MODE_RX) { |
| 1807 | dev_dbg(radio->dev, "freq: %d\n", freq->frequency); |
| 1808 | |
| 1809 | r = wl1273_fm_set_rx_freq(radio, |
| 1810 | WL1273_INV_FREQ(freq->frequency)); |
| 1811 | if (r) |
| 1812 | dev_warn(radio->dev, WL1273_FM_DRIVER_NAME |
| 1813 | ": set frequency failed with %d\n", r); |
| 1814 | } else { |
| 1815 | r = wl1273_fm_set_tx_freq(radio, |
| 1816 | WL1273_INV_FREQ(freq->frequency)); |
| 1817 | if (r) |
| 1818 | dev_warn(radio->dev, WL1273_FM_DRIVER_NAME |
| 1819 | ": set frequency failed with %d\n", r); |
| 1820 | } |
| 1821 | |
| 1822 | mutex_unlock(&core->lock); |
| 1823 | |
| 1824 | dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n"); |
| 1825 | return r; |
| 1826 | } |
| 1827 | |
| 1828 | #define WL1273_DEFAULT_SEEK_LEVEL 7 |
| 1829 | |
| 1830 | static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv, |
| 1831 | struct v4l2_hw_freq_seek *seek) |
| 1832 | { |
| 1833 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1834 | struct wl1273_core *core = radio->core; |
| 1835 | int r; |
| 1836 | |
| 1837 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1838 | |
| 1839 | if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO) |
| 1840 | return -EINVAL; |
| 1841 | |
| 1842 | if (mutex_lock_interruptible(&core->lock)) |
| 1843 | return -EINTR; |
| 1844 | |
| 1845 | r = wl1273_fm_set_mode(radio, WL1273_MODE_RX); |
| 1846 | if (r) |
| 1847 | goto out; |
| 1848 | |
| 1849 | r = wl1273_fm_tx_set_spacing(radio, seek->spacing); |
| 1850 | if (r) |
| 1851 | dev_warn(radio->dev, "HW seek failed: %d\n", r); |
| 1852 | |
| 1853 | r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward, |
| 1854 | WL1273_DEFAULT_SEEK_LEVEL); |
| 1855 | if (r) |
| 1856 | dev_warn(radio->dev, "HW seek failed: %d\n", r); |
| 1857 | |
| 1858 | out: |
| 1859 | mutex_unlock(&core->lock); |
| 1860 | return r; |
| 1861 | } |
| 1862 | |
| 1863 | static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv, |
| 1864 | struct v4l2_modulator *modulator) |
| 1865 | { |
| 1866 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1867 | struct wl1273_core *core = radio->core; |
| 1868 | int r = 0; |
| 1869 | |
| 1870 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1871 | |
| 1872 | if (modulator->index > 0) |
| 1873 | return -EINVAL; |
| 1874 | |
| 1875 | if (mutex_lock_interruptible(&core->lock)) |
| 1876 | return -EINTR; |
| 1877 | |
| 1878 | r = wl1273_fm_set_mode(radio, WL1273_MODE_TX); |
| 1879 | if (r) |
| 1880 | goto out; |
| 1881 | |
| 1882 | if (modulator->txsubchans & V4L2_TUNER_SUB_RDS) |
| 1883 | r = wl1273_fm_set_rds(radio, WL1273_RDS_ON); |
| 1884 | else |
| 1885 | r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF); |
| 1886 | |
| 1887 | if (modulator->txsubchans & V4L2_TUNER_SUB_MONO) |
| 1888 | r = wl1273_fm_write_cmd(core, WL1273_MONO_SET, WL1273_TX_MONO); |
| 1889 | else |
| 1890 | r = wl1273_fm_write_cmd(core, WL1273_MONO_SET, |
| 1891 | WL1273_RX_STEREO); |
| 1892 | if (r < 0) |
| 1893 | dev_warn(radio->dev, WL1273_FM_DRIVER_NAME |
| 1894 | "MONO_SET fails: %d\n", r); |
| 1895 | out: |
| 1896 | mutex_unlock(&core->lock); |
| 1897 | |
| 1898 | return r; |
| 1899 | } |
| 1900 | |
| 1901 | static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv, |
| 1902 | struct v4l2_modulator *modulator) |
| 1903 | { |
| 1904 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1905 | struct wl1273_core *core = radio->core; |
| 1906 | u16 val; |
| 1907 | int r; |
| 1908 | |
| 1909 | dev_dbg(radio->dev, "%s\n", __func__); |
| 1910 | |
| 1911 | strlcpy(modulator->name, WL1273_FM_DRIVER_NAME, |
| 1912 | sizeof(modulator->name)); |
| 1913 | |
| 1914 | modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW); |
| 1915 | modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH); |
| 1916 | |
| 1917 | modulator->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS | |
| 1918 | V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO; |
| 1919 | |
| 1920 | if (core->mode != WL1273_MODE_TX) |
| 1921 | return 0; |
| 1922 | |
| 1923 | if (mutex_lock_interruptible(&core->lock)) |
| 1924 | return -EINTR; |
| 1925 | |
| 1926 | r = wl1273_fm_read_reg(core, WL1273_MONO_SET, &val); |
| 1927 | if (r) |
| 1928 | goto out; |
| 1929 | |
| 1930 | if (val == WL1273_TX_STEREO) |
| 1931 | modulator->txsubchans = V4L2_TUNER_SUB_STEREO; |
| 1932 | else |
| 1933 | modulator->txsubchans = V4L2_TUNER_SUB_MONO; |
| 1934 | |
| 1935 | if (radio->rds_on) |
| 1936 | modulator->txsubchans |= V4L2_TUNER_SUB_RDS; |
| 1937 | out: |
| 1938 | mutex_unlock(&core->lock); |
| 1939 | |
| 1940 | return 0; |
| 1941 | } |
| 1942 | |
| 1943 | static int wl1273_fm_vidioc_log_status(struct file *file, void *priv) |
| 1944 | { |
| 1945 | struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); |
| 1946 | struct wl1273_core *core = radio->core; |
| 1947 | struct device *dev = radio->dev; |
| 1948 | u16 val; |
| 1949 | int r; |
| 1950 | |
| 1951 | dev_info(dev, DRIVER_DESC); |
| 1952 | |
| 1953 | if (core->mode == WL1273_MODE_OFF) { |
| 1954 | dev_info(dev, "Mode: Off\n"); |
| 1955 | return 0; |
| 1956 | } |
| 1957 | |
| 1958 | if (core->mode == WL1273_MODE_SUSPENDED) { |
| 1959 | dev_info(dev, "Mode: Suspended\n"); |
| 1960 | return 0; |
| 1961 | } |
| 1962 | |
| 1963 | r = wl1273_fm_read_reg(core, WL1273_ASIC_ID_GET, &val); |
| 1964 | if (r) |
| 1965 | dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__); |
| 1966 | else |
| 1967 | dev_info(dev, "ASIC_ID: 0x%04x\n", val); |
| 1968 | |
| 1969 | r = wl1273_fm_read_reg(core, WL1273_ASIC_VER_GET, &val); |
| 1970 | if (r) |
| 1971 | dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__); |
| 1972 | else |
| 1973 | dev_info(dev, "ASIC Version: 0x%04x\n", val); |
| 1974 | |
| 1975 | r = wl1273_fm_read_reg(core, WL1273_FIRM_VER_GET, &val); |
| 1976 | if (r) |
| 1977 | dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__); |
| 1978 | else |
| 1979 | dev_info(dev, "FW version: %d(0x%04x)\n", val, val); |
| 1980 | |
| 1981 | r = wl1273_fm_read_reg(core, WL1273_BAND_SET, &val); |
| 1982 | if (r) |
| 1983 | dev_err(dev, "%s: Get BAND fails.\n", __func__); |
| 1984 | else |
| 1985 | dev_info(dev, "BAND: %d\n", val); |
| 1986 | |
| 1987 | if (core->mode == WL1273_MODE_TX) { |
| 1988 | r = wl1273_fm_read_reg(core, WL1273_PUPD_SET, &val); |
| 1989 | if (r) |
| 1990 | dev_err(dev, "%s: Get PUPD fails.\n", __func__); |
| 1991 | else |
| 1992 | dev_info(dev, "PUPD: 0x%04x\n", val); |
| 1993 | |
| 1994 | r = wl1273_fm_read_reg(core, WL1273_CHANL_SET, &val); |
| 1995 | if (r) |
| 1996 | dev_err(dev, "%s: Get CHANL fails.\n", __func__); |
| 1997 | else |
| 1998 | dev_info(dev, "Tx frequency: %dkHz\n", val*10); |
| 1999 | } else if (core->mode == WL1273_MODE_RX) { |
| 2000 | int bf = radio->rangelow; |
| 2001 | |
| 2002 | r = wl1273_fm_read_reg(core, WL1273_FREQ_SET, &val); |
| 2003 | if (r) |
| 2004 | dev_err(dev, "%s: Get FREQ fails.\n", __func__); |
| 2005 | else |
| 2006 | dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50); |
| 2007 | |
| 2008 | r = wl1273_fm_read_reg(core, WL1273_MOST_MODE_SET, &val); |
| 2009 | if (r) |
| 2010 | dev_err(dev, "%s: Get MOST_MODE fails.\n", |
| 2011 | __func__); |
| 2012 | else if (val == 0) |
| 2013 | dev_info(dev, "MOST_MODE: Stereo according to blend\n"); |
| 2014 | else if (val == 1) |
| 2015 | dev_info(dev, "MOST_MODE: Force mono output\n"); |
| 2016 | else |
| 2017 | dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val); |
| 2018 | |
| 2019 | r = wl1273_fm_read_reg(core, WL1273_MOST_BLEND_SET, &val); |
| 2020 | if (r) |
| 2021 | dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__); |
| 2022 | else if (val == 0) |
| 2023 | dev_info(dev, |
| 2024 | "MOST_BLEND: Switched blend & hysteresis.\n"); |
| 2025 | else if (val == 1) |
| 2026 | dev_info(dev, "MOST_BLEND: Soft blend.\n"); |
| 2027 | else |
| 2028 | dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val); |
| 2029 | |
| 2030 | r = wl1273_fm_read_reg(core, WL1273_STEREO_GET, &val); |
| 2031 | if (r) |
| 2032 | dev_err(dev, "%s: Get STEREO fails.\n", __func__); |
| 2033 | else if (val == 0) |
| 2034 | dev_info(dev, "STEREO: Not detected\n"); |
| 2035 | else if (val == 1) |
| 2036 | dev_info(dev, "STEREO: Detected\n"); |
| 2037 | else |
| 2038 | dev_info(dev, "STEREO: Unexpected value: %d\n", val); |
| 2039 | |
| 2040 | r = wl1273_fm_read_reg(core, WL1273_RSSI_LVL_GET, &val); |
| 2041 | if (r) |
| 2042 | dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__); |
| 2043 | else |
| 2044 | dev_info(dev, "RX signal strength: %d\n", (s16) val); |
| 2045 | |
| 2046 | r = wl1273_fm_read_reg(core, WL1273_POWER_SET, &val); |
| 2047 | if (r) |
| 2048 | dev_err(dev, "%s: Get POWER fails.\n", __func__); |
| 2049 | else |
| 2050 | dev_info(dev, "POWER: 0x%04x\n", val); |
| 2051 | |
| 2052 | r = wl1273_fm_read_reg(core, WL1273_INT_MASK_SET, &val); |
| 2053 | if (r) |
| 2054 | dev_err(dev, "%s: Get INT_MASK fails.\n", __func__); |
| 2055 | else |
| 2056 | dev_info(dev, "INT_MASK: 0x%04x\n", val); |
| 2057 | |
| 2058 | r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val); |
| 2059 | if (r) |
| 2060 | dev_err(dev, "%s: Get RDS_SYNC fails.\n", |
| 2061 | __func__); |
| 2062 | else if (val == 0) |
| 2063 | dev_info(dev, "RDS_SYNC: Not synchronized\n"); |
| 2064 | |
| 2065 | else if (val == 1) |
| 2066 | dev_info(dev, "RDS_SYNC: Synchronized\n"); |
| 2067 | else |
| 2068 | dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val); |
| 2069 | |
| 2070 | r = wl1273_fm_read_reg(core, WL1273_I2S_MODE_CONFIG_SET, &val); |
| 2071 | if (r) |
| 2072 | dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n", |
| 2073 | __func__); |
| 2074 | else |
| 2075 | dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val); |
| 2076 | |
| 2077 | r = wl1273_fm_read_reg(core, WL1273_VOLUME_SET, &val); |
| 2078 | if (r) |
| 2079 | dev_err(dev, "%s: Get VOLUME fails.\n", __func__); |
| 2080 | else |
| 2081 | dev_info(dev, "VOLUME: 0x%04x\n", val); |
| 2082 | } |
| 2083 | |
| 2084 | return 0; |
| 2085 | } |
| 2086 | |
| 2087 | static void wl1273_vdev_release(struct video_device *dev) |
| 2088 | { |
| 2089 | } |
| 2090 | |
| 2091 | static const struct v4l2_ctrl_ops wl1273_ctrl_ops = { |
| 2092 | .s_ctrl = wl1273_fm_vidioc_s_ctrl, |
| 2093 | .g_volatile_ctrl = wl1273_fm_g_volatile_ctrl, |
| 2094 | }; |
| 2095 | |
| 2096 | static const struct v4l2_ioctl_ops wl1273_ioctl_ops = { |
| 2097 | .vidioc_querycap = wl1273_fm_vidioc_querycap, |
| 2098 | .vidioc_g_input = wl1273_fm_vidioc_g_input, |
| 2099 | .vidioc_s_input = wl1273_fm_vidioc_s_input, |
| 2100 | .vidioc_g_audio = wl1273_fm_vidioc_g_audio, |
| 2101 | .vidioc_s_audio = wl1273_fm_vidioc_s_audio, |
| 2102 | .vidioc_g_tuner = wl1273_fm_vidioc_g_tuner, |
| 2103 | .vidioc_s_tuner = wl1273_fm_vidioc_s_tuner, |
| 2104 | .vidioc_g_frequency = wl1273_fm_vidioc_g_frequency, |
| 2105 | .vidioc_s_frequency = wl1273_fm_vidioc_s_frequency, |
| 2106 | .vidioc_s_hw_freq_seek = wl1273_fm_vidioc_s_hw_freq_seek, |
| 2107 | .vidioc_g_modulator = wl1273_fm_vidioc_g_modulator, |
| 2108 | .vidioc_s_modulator = wl1273_fm_vidioc_s_modulator, |
| 2109 | .vidioc_log_status = wl1273_fm_vidioc_log_status, |
| 2110 | }; |
| 2111 | |
| 2112 | static struct video_device wl1273_viddev_template = { |
| 2113 | .fops = &wl1273_fops, |
| 2114 | .ioctl_ops = &wl1273_ioctl_ops, |
| 2115 | .name = WL1273_FM_DRIVER_NAME, |
| 2116 | .release = wl1273_vdev_release, |
| 2117 | }; |
| 2118 | |
| 2119 | static int wl1273_fm_radio_remove(struct platform_device *pdev) |
| 2120 | { |
| 2121 | struct wl1273_device *radio = platform_get_drvdata(pdev); |
| 2122 | struct wl1273_core *core = radio->core; |
| 2123 | |
| 2124 | dev_info(&pdev->dev, "%s.\n", __func__); |
| 2125 | |
| 2126 | free_irq(core->client->irq, radio); |
| 2127 | core->pdata->free_resources(); |
| 2128 | |
| 2129 | v4l2_ctrl_handler_free(&radio->ctrl_handler); |
| 2130 | video_unregister_device(&radio->videodev); |
| 2131 | v4l2_device_unregister(&radio->v4l2dev); |
| 2132 | kfree(radio->buffer); |
| 2133 | kfree(radio->write_buf); |
| 2134 | kfree(radio); |
| 2135 | |
| 2136 | return 0; |
| 2137 | } |
| 2138 | |
| 2139 | static int __devinit wl1273_fm_radio_probe(struct platform_device *pdev) |
| 2140 | { |
| 2141 | struct wl1273_core **core = pdev->dev.platform_data; |
| 2142 | struct wl1273_device *radio; |
| 2143 | struct v4l2_ctrl *ctrl; |
| 2144 | int r = 0; |
| 2145 | |
| 2146 | pr_debug("%s\n", __func__); |
| 2147 | |
| 2148 | if (!core) { |
| 2149 | dev_err(&pdev->dev, "No platform data.\n"); |
| 2150 | r = -EINVAL; |
| 2151 | goto pdata_err; |
| 2152 | } |
| 2153 | |
| 2154 | radio = kzalloc(sizeof(*radio), GFP_KERNEL); |
| 2155 | if (!radio) { |
| 2156 | r = -ENOMEM; |
| 2157 | goto pdata_err; |
| 2158 | } |
| 2159 | |
| 2160 | /* RDS buffer allocation */ |
| 2161 | radio->buf_size = rds_buf * RDS_BLOCK_SIZE; |
| 2162 | radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL); |
| 2163 | if (!radio->buffer) { |
| 2164 | pr_err("Cannot allocate memory for RDS buffer.\n"); |
| 2165 | r = -ENOMEM; |
| 2166 | goto err_kmalloc; |
| 2167 | } |
| 2168 | |
| 2169 | radio->core = *core; |
| 2170 | radio->irq_flags = WL1273_IRQ_MASK; |
| 2171 | radio->dev = &radio->core->client->dev; |
| 2172 | radio->rds_on = false; |
| 2173 | radio->core->mode = WL1273_MODE_OFF; |
| 2174 | radio->tx_power = 118; |
| 2175 | radio->core->audio_mode = WL1273_AUDIO_ANALOG; |
| 2176 | radio->band = WL1273_BAND_OTHER; |
| 2177 | radio->core->i2s_mode = WL1273_I2S_DEF_MODE; |
| 2178 | radio->core->channel_number = 2; |
| 2179 | radio->core->volume = WL1273_DEFAULT_VOLUME; |
| 2180 | radio->rx_frequency = WL1273_BAND_OTHER_LOW; |
| 2181 | radio->tx_frequency = WL1273_BAND_OTHER_HIGH; |
| 2182 | radio->rangelow = WL1273_BAND_OTHER_LOW; |
| 2183 | radio->rangehigh = WL1273_BAND_OTHER_HIGH; |
| 2184 | radio->stereo = true; |
| 2185 | radio->bus_type = "I2C"; |
| 2186 | |
| 2187 | radio->core->write = wl1273_fm_write_cmd; |
| 2188 | radio->core->set_audio = wl1273_fm_set_audio; |
| 2189 | radio->core->set_volume = wl1273_fm_set_volume; |
| 2190 | |
| 2191 | if (radio->core->pdata->request_resources) { |
| 2192 | r = radio->core->pdata->request_resources(radio->core->client); |
| 2193 | if (r) { |
| 2194 | dev_err(radio->dev, WL1273_FM_DRIVER_NAME |
| 2195 | ": Cannot get platform data\n"); |
| 2196 | goto err_resources; |
| 2197 | } |
| 2198 | |
| 2199 | dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq); |
| 2200 | |
| 2201 | r = request_threaded_irq(radio->core->client->irq, NULL, |
| 2202 | wl1273_fm_irq_thread_handler, |
| 2203 | IRQF_ONESHOT | IRQF_TRIGGER_FALLING, |
| 2204 | "wl1273-fm", radio); |
| 2205 | if (r < 0) { |
| 2206 | dev_err(radio->dev, WL1273_FM_DRIVER_NAME |
| 2207 | ": Unable to register IRQ handler: %d\n", r); |
| 2208 | goto err_request_irq; |
| 2209 | } |
| 2210 | } else { |
| 2211 | dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ" |
| 2212 | " not configured"); |
| 2213 | r = -EINVAL; |
| 2214 | goto err_resources; |
| 2215 | } |
| 2216 | |
| 2217 | init_completion(&radio->busy); |
| 2218 | init_waitqueue_head(&radio->read_queue); |
| 2219 | |
| 2220 | radio->write_buf = kmalloc(256, GFP_KERNEL); |
| 2221 | if (!radio->write_buf) { |
| 2222 | r = -ENOMEM; |
| 2223 | goto write_buf_err; |
| 2224 | } |
| 2225 | |
| 2226 | radio->dev = &pdev->dev; |
| 2227 | radio->v4l2dev.ctrl_handler = &radio->ctrl_handler; |
| 2228 | radio->rds_users = 0; |
| 2229 | |
| 2230 | r = v4l2_device_register(&pdev->dev, &radio->v4l2dev); |
| 2231 | if (r) { |
| 2232 | dev_err(&pdev->dev, "Cannot register v4l2_device.\n"); |
| 2233 | goto device_register_err; |
| 2234 | } |
| 2235 | |
| 2236 | /* V4L2 configuration */ |
| 2237 | memcpy(&radio->videodev, &wl1273_viddev_template, |
| 2238 | sizeof(wl1273_viddev_template)); |
| 2239 | |
| 2240 | radio->videodev.v4l2_dev = &radio->v4l2dev; |
| 2241 | |
| 2242 | v4l2_ctrl_handler_init(&radio->ctrl_handler, 6); |
| 2243 | |
| 2244 | /* add in ascending ID order */ |
| 2245 | v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops, |
| 2246 | V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1, |
| 2247 | WL1273_DEFAULT_VOLUME); |
| 2248 | |
| 2249 | v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops, |
| 2250 | V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); |
| 2251 | |
| 2252 | v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops, |
| 2253 | V4L2_CID_TUNE_PREEMPHASIS, |
| 2254 | V4L2_PREEMPHASIS_75_uS, 0x03, |
| 2255 | V4L2_PREEMPHASIS_50_uS); |
| 2256 | |
| 2257 | v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops, |
| 2258 | V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118); |
| 2259 | |
| 2260 | ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops, |
| 2261 | V4L2_CID_TUNE_ANTENNA_CAPACITOR, |
| 2262 | 0, 255, 1, 255); |
| 2263 | if (ctrl) |
| 2264 | ctrl->is_volatile = 1; |
| 2265 | |
| 2266 | if (radio->ctrl_handler.error) { |
| 2267 | r = radio->ctrl_handler.error; |
| 2268 | dev_err(&pdev->dev, "Ctrl handler error: %d\n", r); |
| 2269 | goto handler_init_err; |
| 2270 | } |
| 2271 | |
| 2272 | video_set_drvdata(&radio->videodev, radio); |
| 2273 | platform_set_drvdata(pdev, radio); |
| 2274 | |
| 2275 | /* register video device */ |
| 2276 | r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr); |
| 2277 | if (r) { |
| 2278 | dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME |
| 2279 | ": Could not register video device\n"); |
| 2280 | goto handler_init_err; |
| 2281 | } |
| 2282 | |
| 2283 | return 0; |
| 2284 | |
| 2285 | handler_init_err: |
| 2286 | v4l2_ctrl_handler_free(&radio->ctrl_handler); |
| 2287 | v4l2_device_unregister(&radio->v4l2dev); |
| 2288 | device_register_err: |
| 2289 | kfree(radio->write_buf); |
| 2290 | write_buf_err: |
| 2291 | free_irq(radio->core->client->irq, radio); |
| 2292 | err_request_irq: |
| 2293 | radio->core->pdata->free_resources(); |
| 2294 | err_resources: |
| 2295 | kfree(radio->buffer); |
| 2296 | err_kmalloc: |
| 2297 | kfree(radio); |
| 2298 | pdata_err: |
| 2299 | return r; |
| 2300 | } |
| 2301 | |
| 2302 | MODULE_ALIAS("platform:wl1273_fm_radio"); |
| 2303 | |
| 2304 | static struct platform_driver wl1273_fm_radio_driver = { |
| 2305 | .probe = wl1273_fm_radio_probe, |
| 2306 | .remove = __devexit_p(wl1273_fm_radio_remove), |
| 2307 | .driver = { |
| 2308 | .name = "wl1273_fm_radio", |
| 2309 | .owner = THIS_MODULE, |
| 2310 | }, |
| 2311 | }; |
| 2312 | |
| 2313 | static int __init wl1273_fm_module_init(void) |
| 2314 | { |
| 2315 | pr_info("%s\n", __func__); |
| 2316 | return platform_driver_register(&wl1273_fm_radio_driver); |
| 2317 | } |
| 2318 | module_init(wl1273_fm_module_init); |
| 2319 | |
| 2320 | static void __exit wl1273_fm_module_exit(void) |
| 2321 | { |
| 2322 | flush_scheduled_work(); |
| 2323 | platform_driver_unregister(&wl1273_fm_radio_driver); |
| 2324 | pr_info(DRIVER_DESC ", Exiting.\n"); |
| 2325 | } |
| 2326 | module_exit(wl1273_fm_module_exit); |
| 2327 | |
| 2328 | MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>"); |
| 2329 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 2330 | MODULE_LICENSE("GPL"); |