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