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