blob: a93f681aa9d67c2f5dcb9bc6871f7239940960bd [file] [log] [blame]
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001/*
2 * Driver for the Texas Instruments WL1273 FM radio.
3 *
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03004 * Copyright (C) 2011 Nokia Corporation
Matti Aaltonen87d1a502010-12-10 11:41:34 -03005 * 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 Gortmaker7a707b82011-07-03 14:03:12 -040026#include <linux/module.h>
Matti Aaltonen87d1a502010-12-10 11:41:34 -030027#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 */
51static int radio_nr;
52module_param(radio_nr, int, 0);
53MODULE_PARM_DESC(radio_nr, "The number of the radio device. Default = 0");
54
55struct 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 Aaltonen87d1a502010-12-10 11:41:34 -030071
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 */
103static unsigned int rds_buf = 100;
104module_param(rds_buf, uint, 0);
105MODULE_PARM_DESC(rds_buf, "Number of RDS buffer entries. Default = 100");
106
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300107static 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 Aaltonen87d1a502010-12-10 11:41:34 -0300139#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
143static 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 Aaltonen96a9cc62011-03-01 10:10:36 -0300169 r = core->read(core, WL1273_RDS_SYNC_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300170 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
230static 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 Aaltonen96a9cc62011-03-01 10:10:36 -0300237 r = core->read(core, WL1273_FLAG_GET, &flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300238 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 Aaltonen96a9cc62011-03-01 10:10:36 -0300261 r = core->read(core, WL1273_RSSI_LVL_GET, &level);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300262 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 Aaltonen96a9cc62011-03-01 10:10:36 -0300302 r = core->write(core, WL1273_TUNER_MODE_SET,
303 TUNER_MODE_STOP_SEARCH);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300304 if (r) {
305 dev_err(radio->dev,
306 "%s: TUNER_MODE_SET fails: %d\n",
307 __func__, r);
308 goto out;
309 }
310
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300311 r = core->read(core, WL1273_FREQ_SET, &freq);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300312 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 Aaltonen96a9cc62011-03-01 10:10:36 -0300330 r = core->read(core, WL1273_CHANL_SET, &freq);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300331 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
339out:
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300340 core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300341 complete(&radio->busy);
342
343 return IRQ_HANDLED;
344}
345
346static 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;
Nicholas Mc Guiredaa939d2015-02-05 04:58:48 -0300350 unsigned long t;
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300351
352 if (freq < WL1273_BAND_TX_LOW) {
353 dev_err(radio->dev,
354 "Frequency out of range: %d < %d\n", freq,
355 WL1273_BAND_TX_LOW);
356 return -ERANGE;
357 }
358
359 if (freq > WL1273_BAND_TX_HIGH) {
360 dev_err(radio->dev,
361 "Frequency out of range: %d > %d\n", freq,
362 WL1273_BAND_TX_HIGH);
363 return -ERANGE;
364 }
365
366 /*
367 * The driver works better with this sleep,
368 * the documentation doesn't mention it.
369 */
370 usleep_range(5000, 10000);
371
372 dev_dbg(radio->dev, "%s: freq: %d kHz\n", __func__, freq);
373
374 /* Set the current tx channel */
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300375 r = core->write(core, WL1273_CHANL_SET, freq / 10);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300376 if (r)
377 return r;
378
Wolfram Sang16735d02013-11-14 14:32:02 -0800379 reinit_completion(&radio->busy);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300380
381 /* wait for the FR IRQ */
Nicholas Mc Guiredaa939d2015-02-05 04:58:48 -0300382 t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
383 if (!t)
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300384 return -ETIMEDOUT;
385
Nicholas Mc Guiredaa939d2015-02-05 04:58:48 -0300386 dev_dbg(radio->dev, "WL1273_CHANL_SET: %lu\n", t);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300387
388 /* Enable the output power */
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300389 r = core->write(core, WL1273_POWER_ENB_SET, 1);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300390 if (r)
391 return r;
392
Wolfram Sang16735d02013-11-14 14:32:02 -0800393 reinit_completion(&radio->busy);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300394
395 /* wait for the POWER_ENB IRQ */
Nicholas Mc Guiredaa939d2015-02-05 04:58:48 -0300396 t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
397 if (!t)
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300398 return -ETIMEDOUT;
399
400 radio->tx_frequency = freq;
Nicholas Mc Guiredaa939d2015-02-05 04:58:48 -0300401 dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %lu\n", t);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300402
403 return 0;
404}
405
406static int wl1273_fm_set_rx_freq(struct wl1273_device *radio, unsigned int freq)
407{
408 struct wl1273_core *core = radio->core;
409 int r, f;
Nicholas Mc Guiredaa939d2015-02-05 04:58:48 -0300410 unsigned long t;
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300411
412 if (freq < radio->rangelow) {
413 dev_err(radio->dev,
414 "Frequency out of range: %d < %d\n", freq,
415 radio->rangelow);
416 r = -ERANGE;
417 goto err;
418 }
419
420 if (freq > radio->rangehigh) {
421 dev_err(radio->dev,
422 "Frequency out of range: %d > %d\n", freq,
423 radio->rangehigh);
424 r = -ERANGE;
425 goto err;
426 }
427
428 dev_dbg(radio->dev, "%s: %dkHz\n", __func__, freq);
429
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300430 core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300431
432 if (radio->band == WL1273_BAND_JAPAN)
433 f = (freq - WL1273_BAND_JAPAN_LOW) / 50;
434 else
435 f = (freq - WL1273_BAND_OTHER_LOW) / 50;
436
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300437 r = core->write(core, WL1273_FREQ_SET, f);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300438 if (r) {
439 dev_err(radio->dev, "FREQ_SET fails\n");
440 goto err;
441 }
442
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300443 r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300444 if (r) {
445 dev_err(radio->dev, "TUNER_MODE_SET fails\n");
446 goto err;
447 }
448
Wolfram Sang16735d02013-11-14 14:32:02 -0800449 reinit_completion(&radio->busy);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300450
Nicholas Mc Guiredaa939d2015-02-05 04:58:48 -0300451 t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
452 if (!t) {
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300453 dev_err(radio->dev, "%s: TIMEOUT\n", __func__);
454 return -ETIMEDOUT;
455 }
456
457 radio->rd_index = 0;
458 radio->wr_index = 0;
459 radio->rx_frequency = freq;
460 return 0;
461err:
462 return r;
463}
464
465static int wl1273_fm_get_freq(struct wl1273_device *radio)
466{
467 struct wl1273_core *core = radio->core;
468 unsigned int freq;
469 u16 f;
470 int r;
471
472 if (core->mode == WL1273_MODE_RX) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300473 r = core->read(core, WL1273_FREQ_SET, &f);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300474 if (r)
475 return r;
476
477 dev_dbg(radio->dev, "Freq get: 0x%04x\n", f);
478 if (radio->band == WL1273_BAND_JAPAN)
479 freq = WL1273_BAND_JAPAN_LOW + 50 * f;
480 else
481 freq = WL1273_BAND_OTHER_LOW + 50 * f;
482 } else {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300483 r = core->read(core, WL1273_CHANL_SET, &f);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300484 if (r)
485 return r;
486
487 freq = f * 10;
488 }
489
490 return freq;
491}
492
493/**
494 * wl1273_fm_upload_firmware_patch() - Upload the firmware.
495 * @radio: A pointer to the device struct.
496 *
497 * The firmware file consists of arrays of bytes where the first byte
498 * gives the array length. The first byte in the file gives the
499 * number of these arrays.
500 */
501static int wl1273_fm_upload_firmware_patch(struct wl1273_device *radio)
502{
503 struct wl1273_core *core = radio->core;
504 unsigned int packet_num;
505 const struct firmware *fw_p;
506 const char *fw_name = "radio-wl1273-fw.bin";
507 struct device *dev = radio->dev;
508 __u8 *ptr;
Mauro Carvalho Chehabfce50ac2010-12-27 11:44:50 -0300509 int r;
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300510
511 dev_dbg(dev, "%s:\n", __func__);
512
513 /*
514 * Uploading the firmware patch is not always necessary,
515 * so we only print an info message.
516 */
517 if (request_firmware(&fw_p, fw_name, dev)) {
518 dev_info(dev, "%s - %s not found\n", __func__, fw_name);
519
520 return 0;
521 }
522
523 ptr = (__u8 *) fw_p->data;
524 packet_num = ptr[0];
525 dev_dbg(dev, "%s: packets: %d\n", __func__, packet_num);
526
527 r = wl1273_fm_write_fw(core, ptr + 1, packet_num);
528 if (r) {
529 dev_err(dev, "FW upload error: %d\n", r);
530 goto out;
531 }
532
533 /* ignore possible error here */
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300534 core->write(core, WL1273_RESET, 0);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300535
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300536 dev_dbg(dev, "%s - download OK, r: %d\n", __func__, r);
537out:
538 release_firmware(fw_p);
539 return r;
540}
541
542static int wl1273_fm_stop(struct wl1273_device *radio)
543{
544 struct wl1273_core *core = radio->core;
545
546 if (core->mode == WL1273_MODE_RX) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300547 int r = core->write(core, WL1273_POWER_SET,
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300548 WL1273_POWER_SET_OFF);
549 if (r)
550 dev_err(radio->dev, "%s: POWER_SET fails: %d\n",
551 __func__, r);
552 } else if (core->mode == WL1273_MODE_TX) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300553 int r = core->write(core, WL1273_PUPD_SET,
554 WL1273_PUPD_SET_OFF);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300555 if (r)
556 dev_err(radio->dev,
557 "%s: PUPD_SET fails: %d\n", __func__, r);
558 }
559
560 if (core->pdata->disable) {
561 core->pdata->disable();
562 dev_dbg(radio->dev, "Back to reset\n");
563 }
564
565 return 0;
566}
567
568static int wl1273_fm_start(struct wl1273_device *radio, int new_mode)
569{
570 struct wl1273_core *core = radio->core;
571 struct wl1273_fm_platform_data *pdata = core->pdata;
572 struct device *dev = radio->dev;
573 int r = -EINVAL;
574
575 if (pdata->enable && core->mode == WL1273_MODE_OFF) {
576 dev_dbg(radio->dev, "Out of reset\n");
577
578 pdata->enable();
579 msleep(250);
580 }
581
582 if (new_mode == WL1273_MODE_RX) {
583 u16 val = WL1273_POWER_SET_FM;
584
585 if (radio->rds_on)
586 val |= WL1273_POWER_SET_RDS;
587
588 /* If this fails try again */
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300589 r = core->write(core, WL1273_POWER_SET, val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300590 if (r) {
591 msleep(100);
592
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300593 r = core->write(core, WL1273_POWER_SET, val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300594 if (r) {
595 dev_err(dev, "%s: POWER_SET fails\n", __func__);
596 goto fail;
597 }
598 }
599
600 /* rds buffer configuration */
601 radio->wr_index = 0;
602 radio->rd_index = 0;
603
604 } else if (new_mode == WL1273_MODE_TX) {
605 /* If this fails try again once */
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300606 r = core->write(core, WL1273_PUPD_SET, WL1273_PUPD_SET_ON);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300607 if (r) {
608 msleep(100);
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300609 r = core->write(core, WL1273_PUPD_SET,
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300610 WL1273_PUPD_SET_ON);
611 if (r) {
612 dev_err(dev, "%s: PUPD_SET fails\n", __func__);
613 goto fail;
614 }
615 }
616
617 if (radio->rds_on)
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300618 r = core->write(core, WL1273_RDS_DATA_ENB, 1);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300619 else
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300620 r = core->write(core, WL1273_RDS_DATA_ENB, 0);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300621 } else {
622 dev_warn(dev, "%s: Illegal mode.\n", __func__);
623 }
624
625 if (core->mode == WL1273_MODE_OFF) {
626 r = wl1273_fm_upload_firmware_patch(radio);
627 if (r)
628 dev_warn(dev, "Firmware upload failed.\n");
629
630 /*
631 * Sometimes the chip is in a wrong power state at this point.
632 * So we set the power once again.
633 */
634 if (new_mode == WL1273_MODE_RX) {
635 u16 val = WL1273_POWER_SET_FM;
636
637 if (radio->rds_on)
638 val |= WL1273_POWER_SET_RDS;
639
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300640 r = core->write(core, WL1273_POWER_SET, val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300641 if (r) {
642 dev_err(dev, "%s: POWER_SET fails\n", __func__);
643 goto fail;
644 }
645 } else if (new_mode == WL1273_MODE_TX) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300646 r = core->write(core, WL1273_PUPD_SET,
647 WL1273_PUPD_SET_ON);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300648 if (r) {
649 dev_err(dev, "%s: PUPD_SET fails\n", __func__);
650 goto fail;
651 }
652 }
653 }
654
655 return 0;
656fail:
657 if (pdata->disable)
658 pdata->disable();
659
660 dev_dbg(dev, "%s: return: %d\n", __func__, r);
661 return r;
662}
663
664static int wl1273_fm_suspend(struct wl1273_device *radio)
665{
666 struct wl1273_core *core = radio->core;
667 int r = 0;
668
669 /* Cannot go from OFF to SUSPENDED */
670 if (core->mode == WL1273_MODE_RX)
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300671 r = core->write(core, WL1273_POWER_SET,
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300672 WL1273_POWER_SET_RETENTION);
673 else if (core->mode == WL1273_MODE_TX)
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300674 r = core->write(core, WL1273_PUPD_SET,
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300675 WL1273_PUPD_SET_RETENTION);
676 else
677 r = -EINVAL;
678
679 if (r) {
680 dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r);
681 goto out;
682 }
683
684out:
685 return r;
686}
687
688static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode)
689{
690 struct wl1273_core *core = radio->core;
691 struct device *dev = radio->dev;
692 int old_mode;
693 int r;
694
695 dev_dbg(dev, "%s\n", __func__);
696 dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden);
697
698 old_mode = core->mode;
699 if (mode & radio->forbidden) {
700 r = -EPERM;
701 goto out;
702 }
703
704 switch (mode) {
705 case WL1273_MODE_RX:
706 case WL1273_MODE_TX:
707 r = wl1273_fm_start(radio, mode);
708 if (r) {
709 dev_err(dev, "%s: Cannot start.\n", __func__);
710 wl1273_fm_stop(radio);
711 goto out;
712 }
713
714 core->mode = mode;
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300715 r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300716 if (r) {
717 dev_err(dev, "INT_MASK_SET fails.\n");
718 goto out;
719 }
720
721 /* remember previous settings */
722 if (mode == WL1273_MODE_RX) {
723 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
724 if (r) {
725 dev_err(dev, "set freq fails: %d.\n", r);
726 goto out;
727 }
728
729 r = core->set_volume(core, core->volume);
730 if (r) {
731 dev_err(dev, "set volume fails: %d.\n", r);
732 goto out;
733 }
734
735 dev_dbg(dev, "%s: Set vol: %d.\n", __func__,
736 core->volume);
737 } else {
738 r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency);
739 if (r) {
740 dev_err(dev, "set freq fails: %d.\n", r);
741 goto out;
742 }
743 }
744
745 dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__);
746
747 r = core->set_audio(core, core->audio_mode);
748 if (r)
749 dev_err(dev, "Cannot set audio mode.\n");
750 break;
751
752 case WL1273_MODE_OFF:
753 r = wl1273_fm_stop(radio);
754 if (r)
755 dev_err(dev, "%s: Off fails: %d\n", __func__, r);
756 else
757 core->mode = WL1273_MODE_OFF;
758
759 break;
760
761 case WL1273_MODE_SUSPENDED:
762 r = wl1273_fm_suspend(radio);
763 if (r)
764 dev_err(dev, "%s: Suspend fails: %d\n", __func__, r);
765 else
766 core->mode = WL1273_MODE_SUSPENDED;
767
768 break;
769
770 default:
771 dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode);
772 r = -EINVAL;
773 break;
774 }
775out:
776 if (r)
777 core->mode = old_mode;
778
779 return r;
780}
781
782static int wl1273_fm_set_seek(struct wl1273_device *radio,
783 unsigned int wrap_around,
784 unsigned int seek_upward,
785 int level)
786{
787 struct wl1273_core *core = radio->core;
788 int r = 0;
789 unsigned int dir = (seek_upward == 0) ? 0 : 1;
790 unsigned int f;
791
792 f = radio->rx_frequency;
793 dev_dbg(radio->dev, "rx_frequency: %d\n", f);
794
795 if (dir && f + radio->spacing <= radio->rangehigh)
796 r = wl1273_fm_set_rx_freq(radio, f + radio->spacing);
797 else if (dir && wrap_around)
798 r = wl1273_fm_set_rx_freq(radio, radio->rangelow);
799 else if (f - radio->spacing >= radio->rangelow)
800 r = wl1273_fm_set_rx_freq(radio, f - radio->spacing);
801 else if (wrap_around)
802 r = wl1273_fm_set_rx_freq(radio, radio->rangehigh);
803
804 if (r)
805 goto out;
806
807 if (level < SCHAR_MIN || level > SCHAR_MAX)
808 return -EINVAL;
809
Wolfram Sang16735d02013-11-14 14:32:02 -0800810 reinit_completion(&radio->busy);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300811 dev_dbg(radio->dev, "%s: BUSY\n", __func__);
812
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300813 r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300814 if (r)
815 goto out;
816
817 dev_dbg(radio->dev, "%s\n", __func__);
818
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300819 r = core->write(core, WL1273_SEARCH_LVL_SET, level);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300820 if (r)
821 goto out;
822
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300823 r = core->write(core, WL1273_SEARCH_DIR_SET, dir);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300824 if (r)
825 goto out;
826
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300827 r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300828 if (r)
829 goto out;
830
Nicholas Mc Guirec973f76e2015-02-05 05:56:42 -0300831 /* wait for the FR IRQ */
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300832 wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
Nicholas Mc Guirec973f76e2015-02-05 05:56:42 -0300833 if (!(radio->irq_received & WL1273_BL_EVENT)) {
834 r = -ETIMEDOUT;
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300835 goto out;
Nicholas Mc Guirec973f76e2015-02-05 05:56:42 -0300836 }
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300837
838 radio->irq_received &= ~WL1273_BL_EVENT;
839
840 if (!wrap_around)
841 goto out;
842
843 /* Wrap around */
844 dev_dbg(radio->dev, "Wrap around in HW seek.\n");
845
846 if (seek_upward)
847 f = radio->rangelow;
848 else
849 f = radio->rangehigh;
850
851 r = wl1273_fm_set_rx_freq(radio, f);
852 if (r)
853 goto out;
854
Wolfram Sang16735d02013-11-14 14:32:02 -0800855 reinit_completion(&radio->busy);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300856 dev_dbg(radio->dev, "%s: BUSY\n", __func__);
857
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300858 r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300859 if (r)
860 goto out;
861
Nicholas Mc Guirec973f76e2015-02-05 05:56:42 -0300862 /* wait for the FR IRQ */
863 if (!wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000)))
864 r = -ETIMEDOUT;
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300865out:
866 dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r);
867 return r;
868}
869
870/**
871 * wl1273_fm_get_tx_ctune() - Get the TX tuning capacitor value.
872 * @radio: A pointer to the device struct.
873 */
874static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio)
875{
876 struct wl1273_core *core = radio->core;
877 struct device *dev = radio->dev;
878 u16 val;
879 int r;
880
881 if (core->mode == WL1273_MODE_OFF ||
882 core->mode == WL1273_MODE_SUSPENDED)
883 return -EPERM;
884
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300885 r = core->read(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300886 if (r) {
887 dev_err(dev, "%s: read error: %d\n", __func__, r);
888 goto out;
889 }
890
891out:
892 return val;
893}
894
895/**
896 * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
897 * @radio: A pointer to the device struct.
898 * @preemphasis: The new pre-amphasis value.
899 *
900 * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
901 * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
902 */
903static int wl1273_fm_set_preemphasis(struct wl1273_device *radio,
904 unsigned int preemphasis)
905{
906 struct wl1273_core *core = radio->core;
907 int r;
908 u16 em;
909
910 if (core->mode == WL1273_MODE_OFF ||
911 core->mode == WL1273_MODE_SUSPENDED)
912 return -EPERM;
913
914 mutex_lock(&core->lock);
915
916 switch (preemphasis) {
917 case V4L2_PREEMPHASIS_DISABLED:
918 em = 1;
919 break;
920 case V4L2_PREEMPHASIS_50_uS:
921 em = 0;
922 break;
923 case V4L2_PREEMPHASIS_75_uS:
924 em = 2;
925 break;
926 default:
927 r = -EINVAL;
928 goto out;
929 }
930
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300931 r = core->write(core, WL1273_PREMPH_SET, em);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300932 if (r)
933 goto out;
934
935 radio->preemphasis = preemphasis;
936
937out:
938 mutex_unlock(&core->lock);
939 return r;
940}
941
942static int wl1273_fm_rds_on(struct wl1273_device *radio)
943{
944 struct wl1273_core *core = radio->core;
945 int r;
946
947 dev_dbg(radio->dev, "%s\n", __func__);
948 if (radio->rds_on)
949 return 0;
950
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300951 r = core->write(core, WL1273_POWER_SET,
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300952 WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
953 if (r)
954 goto out;
955
956 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
957 if (r)
958 dev_err(radio->dev, "set freq fails: %d.\n", r);
959out:
960 return r;
961}
962
963static int wl1273_fm_rds_off(struct wl1273_device *radio)
964{
965 struct wl1273_core *core = radio->core;
966 int r;
967
968 if (!radio->rds_on)
969 return 0;
970
971 radio->irq_flags &= ~WL1273_RDS_EVENT;
972
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300973 r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300974 if (r)
975 goto out;
976
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300977 /* Service pending read */
978 wake_up_interruptible(&radio->read_queue);
979
980 dev_dbg(radio->dev, "%s\n", __func__);
981
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300982 r = core->write(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300983 if (r)
984 goto out;
985
986 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
987 if (r)
988 dev_err(radio->dev, "set freq fails: %d.\n", r);
989out:
990 dev_dbg(radio->dev, "%s: exiting...\n", __func__);
991
992 return r;
993}
994
995static int wl1273_fm_set_rds(struct wl1273_device *radio, unsigned int new_mode)
996{
997 int r = 0;
998 struct wl1273_core *core = radio->core;
999
1000 if (core->mode == WL1273_MODE_OFF ||
1001 core->mode == WL1273_MODE_SUSPENDED)
1002 return -EPERM;
1003
1004 if (new_mode == WL1273_RDS_RESET) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001005 r = core->write(core, WL1273_RDS_CNTRL_SET, 1);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001006 return r;
1007 }
1008
1009 if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001010 r = core->write(core, WL1273_RDS_DATA_ENB, 0);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001011 } else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001012 r = core->write(core, WL1273_RDS_DATA_ENB, 1);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001013 } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) {
1014 r = wl1273_fm_rds_off(radio);
1015 } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) {
1016 r = wl1273_fm_rds_on(radio);
1017 } else {
1018 dev_err(radio->dev, "%s: Unknown mode: %d\n",
1019 __func__, new_mode);
1020 r = -EINVAL;
1021 }
1022
1023 if (!r)
1024 radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false;
1025
1026 return r;
1027}
1028
1029static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf,
1030 size_t count, loff_t *ppos)
1031{
1032 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001033 struct wl1273_core *core = radio->core;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001034 u16 val;
1035 int r;
1036
1037 dev_dbg(radio->dev, "%s\n", __func__);
1038
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001039 if (core->mode != WL1273_MODE_TX)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001040 return count;
1041
1042 if (radio->rds_users == 0) {
1043 dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1044 return 0;
1045 }
1046
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001047 if (mutex_lock_interruptible(&core->lock))
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001048 return -EINTR;
1049 /*
1050 * Multiple processes can open the device, but only
1051 * one gets to write to it.
1052 */
1053 if (radio->owner && radio->owner != file) {
1054 r = -EBUSY;
1055 goto out;
1056 }
1057 radio->owner = file;
1058
1059 /* Manual Mode */
1060 if (count > 255)
1061 val = 255;
1062 else
1063 val = count;
1064
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001065 core->write(core, WL1273_RDS_CONFIG_DATA_SET, val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001066
1067 if (copy_from_user(radio->write_buf + 1, buf, val)) {
1068 r = -EFAULT;
1069 goto out;
1070 }
1071
1072 dev_dbg(radio->dev, "Count: %d\n", val);
1073 dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf);
1074
1075 radio->write_buf[0] = WL1273_RDS_DATA_SET;
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001076 core->write_data(core, radio->write_buf, val + 1);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001077
1078 r = val;
1079out:
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001080 mutex_unlock(&core->lock);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001081
1082 return r;
1083}
1084
1085static unsigned int wl1273_fm_fops_poll(struct file *file,
1086 struct poll_table_struct *pts)
1087{
1088 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1089 struct wl1273_core *core = radio->core;
1090
1091 if (radio->owner && radio->owner != file)
1092 return -EBUSY;
1093
1094 radio->owner = file;
1095
1096 if (core->mode == WL1273_MODE_RX) {
1097 poll_wait(file, &radio->read_queue, pts);
1098
1099 if (radio->rd_index != radio->wr_index)
1100 return POLLIN | POLLRDNORM;
1101
1102 } else if (core->mode == WL1273_MODE_TX) {
1103 return POLLOUT | POLLWRNORM;
1104 }
1105
1106 return 0;
1107}
1108
1109static int wl1273_fm_fops_open(struct file *file)
1110{
1111 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1112 struct wl1273_core *core = radio->core;
1113 int r = 0;
1114
1115 dev_dbg(radio->dev, "%s\n", __func__);
1116
1117 if (core->mode == WL1273_MODE_RX && radio->rds_on &&
1118 !radio->rds_users) {
1119 dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode);
1120
1121 if (mutex_lock_interruptible(&core->lock))
1122 return -EINTR;
1123
1124 radio->irq_flags |= WL1273_RDS_EVENT;
1125
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001126 r = core->write(core, WL1273_INT_MASK_SET,
1127 radio->irq_flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001128 if (r) {
1129 mutex_unlock(&core->lock);
1130 goto out;
1131 }
1132
1133 radio->rds_users++;
1134
1135 mutex_unlock(&core->lock);
1136 }
1137out:
1138 return r;
1139}
1140
1141static int wl1273_fm_fops_release(struct file *file)
1142{
1143 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1144 struct wl1273_core *core = radio->core;
1145 int r = 0;
1146
1147 dev_dbg(radio->dev, "%s\n", __func__);
1148
1149 if (radio->rds_users > 0) {
1150 radio->rds_users--;
1151 if (radio->rds_users == 0) {
1152 if (mutex_lock_interruptible(&core->lock))
1153 return -EINTR;
1154
1155 radio->irq_flags &= ~WL1273_RDS_EVENT;
1156
1157 if (core->mode == WL1273_MODE_RX) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001158 r = core->write(core,
1159 WL1273_INT_MASK_SET,
1160 radio->irq_flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001161 if (r) {
1162 mutex_unlock(&core->lock);
1163 goto out;
1164 }
1165 }
1166 mutex_unlock(&core->lock);
1167 }
1168 }
1169
1170 if (file == radio->owner)
1171 radio->owner = NULL;
1172out:
1173 return r;
1174}
1175
1176static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
1177 size_t count, loff_t *ppos)
1178{
1179 int r = 0;
1180 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1181 struct wl1273_core *core = radio->core;
1182 unsigned int block_count = 0;
1183 u16 val;
1184
1185 dev_dbg(radio->dev, "%s\n", __func__);
1186
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001187 if (core->mode != WL1273_MODE_RX)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001188 return 0;
1189
1190 if (radio->rds_users == 0) {
1191 dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1192 return 0;
1193 }
1194
1195 if (mutex_lock_interruptible(&core->lock))
1196 return -EINTR;
1197
1198 /*
1199 * Multiple processes can open the device, but only
1200 * one at a time gets read access.
1201 */
1202 if (radio->owner && radio->owner != file) {
1203 r = -EBUSY;
1204 goto out;
1205 }
1206 radio->owner = file;
1207
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001208 r = core->read(core, WL1273_RDS_SYNC_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001209 if (r) {
1210 dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__);
1211 goto out;
1212 } else if (val == 0) {
1213 dev_info(radio->dev, "RDS_SYNC: Not synchronized\n");
1214 r = -ENODATA;
1215 goto out;
1216 }
1217
1218 /* block if no new data available */
1219 while (radio->wr_index == radio->rd_index) {
1220 if (file->f_flags & O_NONBLOCK) {
1221 r = -EWOULDBLOCK;
1222 goto out;
1223 }
1224
1225 dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__);
1226 if (wait_event_interruptible(radio->read_queue,
1227 radio->wr_index !=
1228 radio->rd_index) < 0) {
1229 r = -EINTR;
1230 goto out;
1231 }
1232 }
1233
1234 /* calculate block count from byte count */
1235 count /= RDS_BLOCK_SIZE;
1236
1237 /* copy RDS blocks from the internal buffer and to user buffer */
1238 while (block_count < count) {
1239 if (radio->rd_index == radio->wr_index)
1240 break;
1241
1242 /* always transfer complete RDS blocks */
1243 if (copy_to_user(buf, &radio->buffer[radio->rd_index],
1244 RDS_BLOCK_SIZE))
1245 break;
1246
1247 /* increment and wrap the read pointer */
1248 radio->rd_index += RDS_BLOCK_SIZE;
1249 if (radio->rd_index >= radio->buf_size)
1250 radio->rd_index = 0;
1251
1252 /* increment counters */
1253 block_count++;
1254 buf += RDS_BLOCK_SIZE;
1255 r += RDS_BLOCK_SIZE;
1256 }
1257
1258out:
1259 dev_dbg(radio->dev, "%s: exit\n", __func__);
1260 mutex_unlock(&core->lock);
1261
1262 return r;
1263}
1264
1265static const struct v4l2_file_operations wl1273_fops = {
1266 .owner = THIS_MODULE,
1267 .read = wl1273_fm_fops_read,
1268 .write = wl1273_fm_fops_write,
1269 .poll = wl1273_fm_fops_poll,
Matti Aaltonen34b8fc82011-01-04 08:29:37 -03001270 .unlocked_ioctl = video_ioctl2,
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001271 .open = wl1273_fm_fops_open,
1272 .release = wl1273_fm_fops_release,
1273};
1274
1275static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
1276 struct v4l2_capability *capability)
1277{
1278 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1279
1280 dev_dbg(radio->dev, "%s\n", __func__);
1281
1282 strlcpy(capability->driver, WL1273_FM_DRIVER_NAME,
1283 sizeof(capability->driver));
1284 strlcpy(capability->card, "Texas Instruments Wl1273 FM Radio",
1285 sizeof(capability->card));
1286 strlcpy(capability->bus_info, radio->bus_type,
1287 sizeof(capability->bus_info));
1288
Hans Verkuil7027c4482014-11-24 06:37:24 -03001289 capability->device_caps = V4L2_CAP_HW_FREQ_SEEK |
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001290 V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
1291 V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
1292 V4L2_CAP_RDS_OUTPUT;
Hans Verkuil7027c4482014-11-24 06:37:24 -03001293 capability->capabilities = capability->device_caps |
1294 V4L2_CAP_DEVICE_CAPS;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001295
1296 return 0;
1297}
1298
1299static int wl1273_fm_vidioc_g_input(struct file *file, void *priv,
1300 unsigned int *i)
1301{
1302 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1303
1304 dev_dbg(radio->dev, "%s\n", __func__);
1305
1306 *i = 0;
1307
1308 return 0;
1309}
1310
1311static int wl1273_fm_vidioc_s_input(struct file *file, void *priv,
1312 unsigned int i)
1313{
1314 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1315
1316 dev_dbg(radio->dev, "%s\n", __func__);
1317
1318 if (i != 0)
1319 return -EINVAL;
1320
1321 return 0;
1322}
1323
1324/**
1325 * wl1273_fm_set_tx_power() - Set the transmission power value.
1326 * @core: A pointer to the device struct.
1327 * @power: The new power value.
1328 */
1329static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power)
1330{
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001331 struct wl1273_core *core = radio->core;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001332 int r;
1333
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001334 if (core->mode == WL1273_MODE_OFF ||
1335 core->mode == WL1273_MODE_SUSPENDED)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001336 return -EPERM;
1337
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001338 mutex_lock(&core->lock);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001339
1340 /* Convert the dBuV value to chip presentation */
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001341 r = core->write(core, WL1273_POWER_LEV_SET, 122 - power);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001342 if (r)
1343 goto out;
1344
1345 radio->tx_power = power;
1346
1347out:
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001348 mutex_unlock(&core->lock);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001349 return r;
1350}
1351
1352#define WL1273_SPACING_50kHz 1
1353#define WL1273_SPACING_100kHz 2
1354#define WL1273_SPACING_200kHz 4
1355
1356static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio,
1357 unsigned int spacing)
1358{
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001359 struct wl1273_core *core = radio->core;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001360 int r;
1361
1362 if (spacing == 0) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001363 r = core->write(core, WL1273_SCAN_SPACING_SET,
1364 WL1273_SPACING_100kHz);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001365 radio->spacing = 100;
1366 } else if (spacing - 50000 < 25000) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001367 r = core->write(core, WL1273_SCAN_SPACING_SET,
1368 WL1273_SPACING_50kHz);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001369 radio->spacing = 50;
1370 } else if (spacing - 100000 < 50000) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001371 r = core->write(core, WL1273_SCAN_SPACING_SET,
1372 WL1273_SPACING_100kHz);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001373 radio->spacing = 100;
1374 } else {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001375 r = core->write(core, WL1273_SCAN_SPACING_SET,
1376 WL1273_SPACING_200kHz);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001377 radio->spacing = 200;
1378 }
1379
1380 return r;
1381}
1382
1383static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1384{
1385 struct wl1273_device *radio = ctrl->priv;
1386 struct wl1273_core *core = radio->core;
1387
1388 dev_dbg(radio->dev, "%s\n", __func__);
1389
1390 if (mutex_lock_interruptible(&core->lock))
1391 return -EINTR;
1392
1393 switch (ctrl->id) {
1394 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
Hans Verkuilddac5c12011-06-10 05:43:34 -03001395 ctrl->val = wl1273_fm_get_tx_ctune(radio);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001396 break;
1397
1398 default:
1399 dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
1400 __func__, ctrl->id);
1401 break;
1402 }
1403
1404 mutex_unlock(&core->lock);
1405
1406 return 0;
1407}
1408
1409#define WL1273_MUTE_SOFT_ENABLE (1 << 0)
1410#define WL1273_MUTE_AC (1 << 1)
1411#define WL1273_MUTE_HARD_LEFT (1 << 2)
1412#define WL1273_MUTE_HARD_RIGHT (1 << 3)
1413#define WL1273_MUTE_SOFT_FORCE (1 << 4)
1414
1415static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl)
1416{
1417 return container_of(ctrl->handler, struct wl1273_device, ctrl_handler);
1418}
1419
1420static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl)
1421{
1422 struct wl1273_device *radio = to_radio(ctrl);
1423 struct wl1273_core *core = radio->core;
1424 int r = 0;
1425
1426 dev_dbg(radio->dev, "%s\n", __func__);
1427
1428 switch (ctrl->id) {
1429 case V4L2_CID_AUDIO_MUTE:
1430 if (mutex_lock_interruptible(&core->lock))
1431 return -EINTR;
1432
1433 if (core->mode == WL1273_MODE_RX && ctrl->val)
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001434 r = core->write(core,
1435 WL1273_MUTE_STATUS_SET,
1436 WL1273_MUTE_HARD_LEFT |
1437 WL1273_MUTE_HARD_RIGHT);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001438 else if (core->mode == WL1273_MODE_RX)
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001439 r = core->write(core,
1440 WL1273_MUTE_STATUS_SET, 0x0);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001441 else if (core->mode == WL1273_MODE_TX && ctrl->val)
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001442 r = core->write(core, WL1273_MUTE, 1);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001443 else if (core->mode == WL1273_MODE_TX)
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001444 r = core->write(core, WL1273_MUTE, 0);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001445
1446 mutex_unlock(&core->lock);
1447 break;
1448
1449 case V4L2_CID_AUDIO_VOLUME:
1450 if (ctrl->val == 0)
1451 r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF);
1452 else
1453 r = core->set_volume(core, core->volume);
1454 break;
1455
1456 case V4L2_CID_TUNE_PREEMPHASIS:
1457 r = wl1273_fm_set_preemphasis(radio, ctrl->val);
1458 break;
1459
1460 case V4L2_CID_TUNE_POWER_LEVEL:
1461 r = wl1273_fm_set_tx_power(radio, ctrl->val);
1462 break;
1463
1464 default:
1465 dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
1466 __func__, ctrl->id);
1467 break;
1468 }
1469
1470 dev_dbg(radio->dev, "%s\n", __func__);
1471 return r;
1472}
1473
1474static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv,
1475 struct v4l2_audio *audio)
1476{
1477 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1478
1479 dev_dbg(radio->dev, "%s\n", __func__);
1480
1481 if (audio->index > 1)
1482 return -EINVAL;
1483
1484 strlcpy(audio->name, "Radio", sizeof(audio->name));
1485 audio->capability = V4L2_AUDCAP_STEREO;
1486
1487 return 0;
1488}
1489
1490static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001491 const struct v4l2_audio *audio)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001492{
1493 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1494
1495 dev_dbg(radio->dev, "%s\n", __func__);
1496
1497 if (audio->index != 0)
1498 return -EINVAL;
1499
1500 return 0;
1501}
1502
1503#define WL1273_RDS_NOT_SYNCHRONIZED 0
1504#define WL1273_RDS_SYNCHRONIZED 1
1505
1506static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv,
1507 struct v4l2_tuner *tuner)
1508{
1509 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1510 struct wl1273_core *core = radio->core;
1511 u16 val;
1512 int r;
1513
1514 dev_dbg(radio->dev, "%s\n", __func__);
1515
1516 if (tuner->index > 0)
1517 return -EINVAL;
1518
1519 strlcpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name));
1520 tuner->type = V4L2_TUNER_RADIO;
1521
1522 tuner->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
1523 tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
1524
1525 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
Hans Verkuil54f60192012-05-27 07:25:06 -03001526 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO |
1527 V4L2_TUNER_CAP_HWSEEK_BOUNDED | V4L2_TUNER_CAP_HWSEEK_WRAP;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001528
1529 if (radio->stereo)
1530 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1531 else
1532 tuner->audmode = V4L2_TUNER_MODE_MONO;
1533
1534 if (core->mode != WL1273_MODE_RX)
1535 return 0;
1536
1537 if (mutex_lock_interruptible(&core->lock))
1538 return -EINTR;
1539
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001540 r = core->read(core, WL1273_STEREO_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001541 if (r)
1542 goto out;
1543
1544 if (val == 1)
1545 tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
1546 else
1547 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1548
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001549 r = core->read(core, WL1273_RSSI_LVL_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001550 if (r)
1551 goto out;
1552
1553 tuner->signal = (s16) val;
1554 dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);
1555
1556 tuner->afc = 0;
1557
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001558 r = core->read(core, WL1273_RDS_SYNC_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001559 if (r)
1560 goto out;
1561
1562 if (val == WL1273_RDS_SYNCHRONIZED)
1563 tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
1564out:
1565 mutex_unlock(&core->lock);
1566
1567 return r;
1568}
1569
1570static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001571 const struct v4l2_tuner *tuner)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001572{
1573 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1574 struct wl1273_core *core = radio->core;
1575 int r = 0;
1576
1577 dev_dbg(radio->dev, "%s\n", __func__);
1578 dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index);
1579 dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name);
1580 dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability);
1581 dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans);
1582 dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow);
1583 dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh);
1584
1585 if (tuner->index > 0)
1586 return -EINVAL;
1587
1588 if (mutex_lock_interruptible(&core->lock))
1589 return -EINTR;
1590
1591 r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1592 if (r)
1593 goto out;
1594
1595 if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS)
1596 r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1597 else
1598 r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1599
1600 if (r)
1601 dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r);
1602
1603 if (tuner->audmode == V4L2_TUNER_MODE_MONO) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001604 r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001605 if (r < 0) {
1606 dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1607 __func__, r);
1608 goto out;
1609 }
1610 radio->stereo = false;
1611 } else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001612 r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001613 if (r < 0) {
1614 dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1615 __func__, r);
1616 goto out;
1617 }
1618 radio->stereo = true;
1619 } else {
1620 dev_err(radio->dev, "%s: tuner->audmode: %d\n",
1621 __func__, tuner->audmode);
1622 r = -EINVAL;
1623 goto out;
1624 }
1625
1626out:
1627 mutex_unlock(&core->lock);
1628
1629 return r;
1630}
1631
1632static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
1633 struct v4l2_frequency *freq)
1634{
1635 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1636 struct wl1273_core *core = radio->core;
1637
1638 dev_dbg(radio->dev, "%s\n", __func__);
1639
1640 if (mutex_lock_interruptible(&core->lock))
1641 return -EINTR;
1642
1643 freq->type = V4L2_TUNER_RADIO;
1644 freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio));
1645
1646 mutex_unlock(&core->lock);
1647
1648 return 0;
1649}
1650
1651static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001652 const struct v4l2_frequency *freq)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001653{
1654 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1655 struct wl1273_core *core = radio->core;
1656 int r;
1657
1658 dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency);
1659
1660 if (freq->type != V4L2_TUNER_RADIO) {
1661 dev_dbg(radio->dev,
1662 "freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
1663 return -EINVAL;
1664 }
1665
1666 if (mutex_lock_interruptible(&core->lock))
1667 return -EINTR;
1668
1669 if (core->mode == WL1273_MODE_RX) {
1670 dev_dbg(radio->dev, "freq: %d\n", freq->frequency);
1671
1672 r = wl1273_fm_set_rx_freq(radio,
1673 WL1273_INV_FREQ(freq->frequency));
1674 if (r)
1675 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1676 ": set frequency failed with %d\n", r);
1677 } else {
1678 r = wl1273_fm_set_tx_freq(radio,
1679 WL1273_INV_FREQ(freq->frequency));
1680 if (r)
1681 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1682 ": set frequency failed with %d\n", r);
1683 }
1684
1685 mutex_unlock(&core->lock);
1686
1687 dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
1688 return r;
1689}
1690
1691#define WL1273_DEFAULT_SEEK_LEVEL 7
1692
1693static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
Hans Verkuilec6f4322012-09-14 07:41:18 -03001694 const struct v4l2_hw_freq_seek *seek)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001695{
1696 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1697 struct wl1273_core *core = radio->core;
1698 int r;
1699
1700 dev_dbg(radio->dev, "%s\n", __func__);
1701
1702 if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
1703 return -EINVAL;
1704
Hans Verkuil617ade62012-09-21 09:33:35 -03001705 if (file->f_flags & O_NONBLOCK)
1706 return -EWOULDBLOCK;
1707
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001708 if (mutex_lock_interruptible(&core->lock))
1709 return -EINTR;
1710
1711 r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1712 if (r)
1713 goto out;
1714
1715 r = wl1273_fm_tx_set_spacing(radio, seek->spacing);
1716 if (r)
1717 dev_warn(radio->dev, "HW seek failed: %d\n", r);
1718
1719 r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward,
1720 WL1273_DEFAULT_SEEK_LEVEL);
1721 if (r)
1722 dev_warn(radio->dev, "HW seek failed: %d\n", r);
1723
1724out:
1725 mutex_unlock(&core->lock);
1726 return r;
1727}
1728
1729static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
Hans Verkuil3f70e1f52012-09-04 12:08:47 -03001730 const struct v4l2_modulator *modulator)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001731{
1732 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1733 struct wl1273_core *core = radio->core;
1734 int r = 0;
1735
1736 dev_dbg(radio->dev, "%s\n", __func__);
1737
1738 if (modulator->index > 0)
1739 return -EINVAL;
1740
1741 if (mutex_lock_interruptible(&core->lock))
1742 return -EINTR;
1743
1744 r = wl1273_fm_set_mode(radio, WL1273_MODE_TX);
1745 if (r)
1746 goto out;
1747
1748 if (modulator->txsubchans & V4L2_TUNER_SUB_RDS)
1749 r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1750 else
1751 r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1752
1753 if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001754 r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001755 else
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001756 r = core->write(core, WL1273_MONO_SET,
1757 WL1273_RX_STEREO);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001758 if (r < 0)
1759 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1760 "MONO_SET fails: %d\n", r);
1761out:
1762 mutex_unlock(&core->lock);
1763
1764 return r;
1765}
1766
1767static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
1768 struct v4l2_modulator *modulator)
1769{
1770 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1771 struct wl1273_core *core = radio->core;
1772 u16 val;
1773 int r;
1774
1775 dev_dbg(radio->dev, "%s\n", __func__);
1776
1777 strlcpy(modulator->name, WL1273_FM_DRIVER_NAME,
1778 sizeof(modulator->name));
1779
1780 modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
1781 modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
1782
1783 modulator->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
1784 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
1785
1786 if (core->mode != WL1273_MODE_TX)
1787 return 0;
1788
1789 if (mutex_lock_interruptible(&core->lock))
1790 return -EINTR;
1791
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001792 r = core->read(core, WL1273_MONO_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001793 if (r)
1794 goto out;
1795
1796 if (val == WL1273_TX_STEREO)
1797 modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
1798 else
1799 modulator->txsubchans = V4L2_TUNER_SUB_MONO;
1800
1801 if (radio->rds_on)
1802 modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
1803out:
1804 mutex_unlock(&core->lock);
1805
1806 return 0;
1807}
1808
1809static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
1810{
1811 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1812 struct wl1273_core *core = radio->core;
1813 struct device *dev = radio->dev;
1814 u16 val;
1815 int r;
1816
1817 dev_info(dev, DRIVER_DESC);
1818
1819 if (core->mode == WL1273_MODE_OFF) {
1820 dev_info(dev, "Mode: Off\n");
1821 return 0;
1822 }
1823
1824 if (core->mode == WL1273_MODE_SUSPENDED) {
1825 dev_info(dev, "Mode: Suspended\n");
1826 return 0;
1827 }
1828
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001829 r = core->read(core, WL1273_ASIC_ID_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001830 if (r)
1831 dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__);
1832 else
1833 dev_info(dev, "ASIC_ID: 0x%04x\n", val);
1834
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001835 r = core->read(core, WL1273_ASIC_VER_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001836 if (r)
1837 dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__);
1838 else
1839 dev_info(dev, "ASIC Version: 0x%04x\n", val);
1840
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001841 r = core->read(core, WL1273_FIRM_VER_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001842 if (r)
1843 dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__);
1844 else
1845 dev_info(dev, "FW version: %d(0x%04x)\n", val, val);
1846
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001847 r = core->read(core, WL1273_BAND_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001848 if (r)
1849 dev_err(dev, "%s: Get BAND fails.\n", __func__);
1850 else
1851 dev_info(dev, "BAND: %d\n", val);
1852
1853 if (core->mode == WL1273_MODE_TX) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001854 r = core->read(core, WL1273_PUPD_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001855 if (r)
1856 dev_err(dev, "%s: Get PUPD fails.\n", __func__);
1857 else
1858 dev_info(dev, "PUPD: 0x%04x\n", val);
1859
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001860 r = core->read(core, WL1273_CHANL_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001861 if (r)
1862 dev_err(dev, "%s: Get CHANL fails.\n", __func__);
1863 else
1864 dev_info(dev, "Tx frequency: %dkHz\n", val*10);
1865 } else if (core->mode == WL1273_MODE_RX) {
1866 int bf = radio->rangelow;
1867
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001868 r = core->read(core, WL1273_FREQ_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001869 if (r)
1870 dev_err(dev, "%s: Get FREQ fails.\n", __func__);
1871 else
1872 dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50);
1873
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001874 r = core->read(core, WL1273_MOST_MODE_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001875 if (r)
1876 dev_err(dev, "%s: Get MOST_MODE fails.\n",
1877 __func__);
1878 else if (val == 0)
1879 dev_info(dev, "MOST_MODE: Stereo according to blend\n");
1880 else if (val == 1)
1881 dev_info(dev, "MOST_MODE: Force mono output\n");
1882 else
1883 dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val);
1884
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001885 r = core->read(core, WL1273_MOST_BLEND_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001886 if (r)
1887 dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__);
1888 else if (val == 0)
1889 dev_info(dev,
1890 "MOST_BLEND: Switched blend & hysteresis.\n");
1891 else if (val == 1)
1892 dev_info(dev, "MOST_BLEND: Soft blend.\n");
1893 else
1894 dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val);
1895
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001896 r = core->read(core, WL1273_STEREO_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001897 if (r)
1898 dev_err(dev, "%s: Get STEREO fails.\n", __func__);
1899 else if (val == 0)
1900 dev_info(dev, "STEREO: Not detected\n");
1901 else if (val == 1)
1902 dev_info(dev, "STEREO: Detected\n");
1903 else
1904 dev_info(dev, "STEREO: Unexpected value: %d\n", val);
1905
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001906 r = core->read(core, WL1273_RSSI_LVL_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001907 if (r)
1908 dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__);
1909 else
1910 dev_info(dev, "RX signal strength: %d\n", (s16) val);
1911
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001912 r = core->read(core, WL1273_POWER_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001913 if (r)
1914 dev_err(dev, "%s: Get POWER fails.\n", __func__);
1915 else
1916 dev_info(dev, "POWER: 0x%04x\n", val);
1917
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001918 r = core->read(core, WL1273_INT_MASK_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001919 if (r)
1920 dev_err(dev, "%s: Get INT_MASK fails.\n", __func__);
1921 else
1922 dev_info(dev, "INT_MASK: 0x%04x\n", val);
1923
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001924 r = core->read(core, WL1273_RDS_SYNC_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001925 if (r)
1926 dev_err(dev, "%s: Get RDS_SYNC fails.\n",
1927 __func__);
1928 else if (val == 0)
1929 dev_info(dev, "RDS_SYNC: Not synchronized\n");
1930
1931 else if (val == 1)
1932 dev_info(dev, "RDS_SYNC: Synchronized\n");
1933 else
1934 dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val);
1935
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001936 r = core->read(core, WL1273_I2S_MODE_CONFIG_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001937 if (r)
1938 dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n",
1939 __func__);
1940 else
1941 dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val);
1942
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001943 r = core->read(core, WL1273_VOLUME_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001944 if (r)
1945 dev_err(dev, "%s: Get VOLUME fails.\n", __func__);
1946 else
1947 dev_info(dev, "VOLUME: 0x%04x\n", val);
1948 }
1949
1950 return 0;
1951}
1952
1953static void wl1273_vdev_release(struct video_device *dev)
1954{
1955}
1956
1957static const struct v4l2_ctrl_ops wl1273_ctrl_ops = {
1958 .s_ctrl = wl1273_fm_vidioc_s_ctrl,
1959 .g_volatile_ctrl = wl1273_fm_g_volatile_ctrl,
1960};
1961
1962static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
1963 .vidioc_querycap = wl1273_fm_vidioc_querycap,
1964 .vidioc_g_input = wl1273_fm_vidioc_g_input,
1965 .vidioc_s_input = wl1273_fm_vidioc_s_input,
1966 .vidioc_g_audio = wl1273_fm_vidioc_g_audio,
1967 .vidioc_s_audio = wl1273_fm_vidioc_s_audio,
1968 .vidioc_g_tuner = wl1273_fm_vidioc_g_tuner,
1969 .vidioc_s_tuner = wl1273_fm_vidioc_s_tuner,
1970 .vidioc_g_frequency = wl1273_fm_vidioc_g_frequency,
1971 .vidioc_s_frequency = wl1273_fm_vidioc_s_frequency,
1972 .vidioc_s_hw_freq_seek = wl1273_fm_vidioc_s_hw_freq_seek,
1973 .vidioc_g_modulator = wl1273_fm_vidioc_g_modulator,
1974 .vidioc_s_modulator = wl1273_fm_vidioc_s_modulator,
1975 .vidioc_log_status = wl1273_fm_vidioc_log_status,
1976};
1977
1978static struct video_device wl1273_viddev_template = {
1979 .fops = &wl1273_fops,
1980 .ioctl_ops = &wl1273_ioctl_ops,
1981 .name = WL1273_FM_DRIVER_NAME,
1982 .release = wl1273_vdev_release,
Hans Verkuilce4a3d52013-01-05 08:52:12 -03001983 .vfl_dir = VFL_DIR_TX,
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001984};
1985
1986static int wl1273_fm_radio_remove(struct platform_device *pdev)
1987{
1988 struct wl1273_device *radio = platform_get_drvdata(pdev);
1989 struct wl1273_core *core = radio->core;
1990
1991 dev_info(&pdev->dev, "%s.\n", __func__);
1992
1993 free_irq(core->client->irq, radio);
1994 core->pdata->free_resources();
1995
1996 v4l2_ctrl_handler_free(&radio->ctrl_handler);
1997 video_unregister_device(&radio->videodev);
1998 v4l2_device_unregister(&radio->v4l2dev);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001999
2000 return 0;
2001}
2002
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002003static int wl1273_fm_radio_probe(struct platform_device *pdev)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002004{
Samuel Ortiz9e554692011-04-06 11:56:04 +02002005 struct wl1273_core **core = pdev->dev.platform_data;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002006 struct wl1273_device *radio;
2007 struct v4l2_ctrl *ctrl;
2008 int r = 0;
2009
2010 pr_debug("%s\n", __func__);
2011
2012 if (!core) {
2013 dev_err(&pdev->dev, "No platform data.\n");
2014 r = -EINVAL;
2015 goto pdata_err;
2016 }
2017
Julia Lawalld91e0132012-07-31 05:21:35 -03002018 radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002019 if (!radio) {
2020 r = -ENOMEM;
2021 goto pdata_err;
2022 }
2023
2024 /* RDS buffer allocation */
2025 radio->buf_size = rds_buf * RDS_BLOCK_SIZE;
Julia Lawalld91e0132012-07-31 05:21:35 -03002026 radio->buffer = devm_kzalloc(&pdev->dev, radio->buf_size, GFP_KERNEL);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002027 if (!radio->buffer) {
2028 pr_err("Cannot allocate memory for RDS buffer.\n");
2029 r = -ENOMEM;
Julia Lawalld91e0132012-07-31 05:21:35 -03002030 goto pdata_err;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002031 }
2032
2033 radio->core = *core;
2034 radio->irq_flags = WL1273_IRQ_MASK;
2035 radio->dev = &radio->core->client->dev;
2036 radio->rds_on = false;
2037 radio->core->mode = WL1273_MODE_OFF;
2038 radio->tx_power = 118;
2039 radio->core->audio_mode = WL1273_AUDIO_ANALOG;
2040 radio->band = WL1273_BAND_OTHER;
2041 radio->core->i2s_mode = WL1273_I2S_DEF_MODE;
2042 radio->core->channel_number = 2;
2043 radio->core->volume = WL1273_DEFAULT_VOLUME;
2044 radio->rx_frequency = WL1273_BAND_OTHER_LOW;
2045 radio->tx_frequency = WL1273_BAND_OTHER_HIGH;
2046 radio->rangelow = WL1273_BAND_OTHER_LOW;
2047 radio->rangehigh = WL1273_BAND_OTHER_HIGH;
2048 radio->stereo = true;
2049 radio->bus_type = "I2C";
2050
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002051 if (radio->core->pdata->request_resources) {
2052 r = radio->core->pdata->request_resources(radio->core->client);
2053 if (r) {
2054 dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2055 ": Cannot get platform data\n");
Julia Lawalld91e0132012-07-31 05:21:35 -03002056 goto pdata_err;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002057 }
2058
2059 dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq);
2060
2061 r = request_threaded_irq(radio->core->client->irq, NULL,
2062 wl1273_fm_irq_thread_handler,
2063 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
2064 "wl1273-fm", radio);
2065 if (r < 0) {
2066 dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2067 ": Unable to register IRQ handler: %d\n", r);
2068 goto err_request_irq;
2069 }
2070 } else {
2071 dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ"
2072 " not configured");
2073 r = -EINVAL;
Julia Lawalld91e0132012-07-31 05:21:35 -03002074 goto pdata_err;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002075 }
2076
2077 init_completion(&radio->busy);
2078 init_waitqueue_head(&radio->read_queue);
2079
Julia Lawalld91e0132012-07-31 05:21:35 -03002080 radio->write_buf = devm_kzalloc(&pdev->dev, 256, GFP_KERNEL);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002081 if (!radio->write_buf) {
2082 r = -ENOMEM;
2083 goto write_buf_err;
2084 }
2085
2086 radio->dev = &pdev->dev;
2087 radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
2088 radio->rds_users = 0;
2089
2090 r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
2091 if (r) {
2092 dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
Julia Lawalld91e0132012-07-31 05:21:35 -03002093 goto write_buf_err;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002094 }
2095
2096 /* V4L2 configuration */
Ezequiel Garcia2b9b3252012-10-23 15:57:25 -03002097 radio->videodev = wl1273_viddev_template;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002098
2099 radio->videodev.v4l2_dev = &radio->v4l2dev;
2100
2101 v4l2_ctrl_handler_init(&radio->ctrl_handler, 6);
2102
2103 /* add in ascending ID order */
2104 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2105 V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1,
2106 WL1273_DEFAULT_VOLUME);
2107
2108 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2109 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
2110
2111 v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops,
2112 V4L2_CID_TUNE_PREEMPHASIS,
2113 V4L2_PREEMPHASIS_75_uS, 0x03,
2114 V4L2_PREEMPHASIS_50_uS);
2115
2116 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2117 V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118);
2118
2119 ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2120 V4L2_CID_TUNE_ANTENNA_CAPACITOR,
2121 0, 255, 1, 255);
2122 if (ctrl)
Hans Verkuil88365102011-08-26 07:35:14 -03002123 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002124
2125 if (radio->ctrl_handler.error) {
2126 r = radio->ctrl_handler.error;
2127 dev_err(&pdev->dev, "Ctrl handler error: %d\n", r);
2128 goto handler_init_err;
2129 }
2130
2131 video_set_drvdata(&radio->videodev, radio);
2132 platform_set_drvdata(pdev, radio);
2133
2134 /* register video device */
2135 r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
2136 if (r) {
2137 dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
2138 ": Could not register video device\n");
2139 goto handler_init_err;
2140 }
2141
2142 return 0;
2143
2144handler_init_err:
2145 v4l2_ctrl_handler_free(&radio->ctrl_handler);
2146 v4l2_device_unregister(&radio->v4l2dev);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002147write_buf_err:
2148 free_irq(radio->core->client->irq, radio);
2149err_request_irq:
2150 radio->core->pdata->free_resources();
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002151pdata_err:
2152 return r;
2153}
2154
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002155static struct platform_driver wl1273_fm_radio_driver = {
2156 .probe = wl1273_fm_radio_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002157 .remove = wl1273_fm_radio_remove,
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002158 .driver = {
2159 .name = "wl1273_fm_radio",
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002160 },
2161};
2162
Axel Lin1d6629b2012-01-10 03:21:49 -03002163module_platform_driver(wl1273_fm_radio_driver);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002164
2165MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
2166MODULE_DESCRIPTION(DRIVER_DESC);
2167MODULE_LICENSE("GPL");
Axel Lin1d6629b2012-01-10 03:21:49 -03002168MODULE_ALIAS("platform:wl1273_fm_radio");