blob: 17e82a9a01099bf6cc5f01b2ce4ab08a0f6b23a8 [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.
Matti Aaltonen87d1a502010-12-10 11:41:34 -030015 */
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>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040022#include <linux/module.h>
Matti Aaltonen87d1a502010-12-10 11:41:34 -030023#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;
Matti Aaltonen87d1a502010-12-10 11:41:34 -030067
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
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300103static 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
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300135#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
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300165 r = core->read(core, WL1273_RDS_SYNC_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300166 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
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300233 r = core->read(core, WL1273_FLAG_GET, &flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300234 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
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300257 r = core->read(core, WL1273_RSSI_LVL_GET, &level);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300258 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) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300298 r = core->write(core, WL1273_TUNER_MODE_SET,
299 TUNER_MODE_STOP_SEARCH);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300300 if (r) {
301 dev_err(radio->dev,
302 "%s: TUNER_MODE_SET fails: %d\n",
303 __func__, r);
304 goto out;
305 }
306
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300307 r = core->read(core, WL1273_FREQ_SET, &freq);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300308 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 {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300326 r = core->read(core, WL1273_CHANL_SET, &freq);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300327 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:
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300336 core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300337 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;
Nicholas Mc Guiredaa939d2015-02-05 04:58:48 -0300346 unsigned long t;
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300347
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 */
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300371 r = core->write(core, WL1273_CHANL_SET, freq / 10);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300372 if (r)
373 return r;
374
Wolfram Sang16735d02013-11-14 14:32:02 -0800375 reinit_completion(&radio->busy);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300376
377 /* wait for the FR IRQ */
Nicholas Mc Guiredaa939d2015-02-05 04:58:48 -0300378 t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
379 if (!t)
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300380 return -ETIMEDOUT;
381
Nicholas Mc Guiredaa939d2015-02-05 04:58:48 -0300382 dev_dbg(radio->dev, "WL1273_CHANL_SET: %lu\n", t);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300383
384 /* Enable the output power */
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300385 r = core->write(core, WL1273_POWER_ENB_SET, 1);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300386 if (r)
387 return r;
388
Wolfram Sang16735d02013-11-14 14:32:02 -0800389 reinit_completion(&radio->busy);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300390
391 /* wait for the POWER_ENB IRQ */
Nicholas Mc Guiredaa939d2015-02-05 04:58:48 -0300392 t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
393 if (!t)
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300394 return -ETIMEDOUT;
395
396 radio->tx_frequency = freq;
Nicholas Mc Guiredaa939d2015-02-05 04:58:48 -0300397 dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %lu\n", t);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300398
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;
Nicholas Mc Guiredaa939d2015-02-05 04:58:48 -0300406 unsigned long t;
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300407
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
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300426 core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300427
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
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300433 r = core->write(core, WL1273_FREQ_SET, f);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300434 if (r) {
435 dev_err(radio->dev, "FREQ_SET fails\n");
436 goto err;
437 }
438
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300439 r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300440 if (r) {
441 dev_err(radio->dev, "TUNER_MODE_SET fails\n");
442 goto err;
443 }
444
Wolfram Sang16735d02013-11-14 14:32:02 -0800445 reinit_completion(&radio->busy);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300446
Nicholas Mc Guiredaa939d2015-02-05 04:58:48 -0300447 t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
448 if (!t) {
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300449 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) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300469 r = core->read(core, WL1273_FREQ_SET, &f);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300470 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 {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300479 r = core->read(core, WL1273_CHANL_SET, &f);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300480 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;
Mauro Carvalho Chehabfce50ac2010-12-27 11:44:50 -0300505 int r;
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300506
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 */
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300530 core->write(core, WL1273_RESET, 0);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300531
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300532 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) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300543 int r = core->write(core, WL1273_POWER_SET,
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300544 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) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300549 int r = core->write(core, WL1273_PUPD_SET,
550 WL1273_PUPD_SET_OFF);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300551 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 */
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300585 r = core->write(core, WL1273_POWER_SET, val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300586 if (r) {
587 msleep(100);
588
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 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 */
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300602 r = core->write(core, WL1273_PUPD_SET, WL1273_PUPD_SET_ON);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300603 if (r) {
604 msleep(100);
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300605 r = core->write(core, WL1273_PUPD_SET,
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300606 WL1273_PUPD_SET_ON);
607 if (r) {
608 dev_err(dev, "%s: PUPD_SET fails\n", __func__);
609 goto fail;
610 }
611 }
612
Gustavo A. R. Silva13c2bed2017-06-22 01:01:22 -0300613 if (radio->rds_on) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300614 r = core->write(core, WL1273_RDS_DATA_ENB, 1);
Gustavo A. R. Silva13c2bed2017-06-22 01:01:22 -0300615 if (r) {
616 dev_err(dev, "%s: RDS_DATA_ENB ON fails\n",
617 __func__);
618 goto fail;
619 }
620 } else {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300621 r = core->write(core, WL1273_RDS_DATA_ENB, 0);
Gustavo A. R. Silva13c2bed2017-06-22 01:01:22 -0300622 if (r) {
623 dev_err(dev, "%s: RDS_DATA_ENB OFF fails\n",
624 __func__);
625 goto fail;
626 }
627 }
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300628 } else {
629 dev_warn(dev, "%s: Illegal mode.\n", __func__);
630 }
631
632 if (core->mode == WL1273_MODE_OFF) {
633 r = wl1273_fm_upload_firmware_patch(radio);
634 if (r)
635 dev_warn(dev, "Firmware upload failed.\n");
636
637 /*
638 * Sometimes the chip is in a wrong power state at this point.
639 * So we set the power once again.
640 */
641 if (new_mode == WL1273_MODE_RX) {
642 u16 val = WL1273_POWER_SET_FM;
643
644 if (radio->rds_on)
645 val |= WL1273_POWER_SET_RDS;
646
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300647 r = core->write(core, WL1273_POWER_SET, val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300648 if (r) {
649 dev_err(dev, "%s: POWER_SET fails\n", __func__);
650 goto fail;
651 }
652 } else if (new_mode == WL1273_MODE_TX) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300653 r = core->write(core, WL1273_PUPD_SET,
654 WL1273_PUPD_SET_ON);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300655 if (r) {
656 dev_err(dev, "%s: PUPD_SET fails\n", __func__);
657 goto fail;
658 }
659 }
660 }
661
662 return 0;
663fail:
664 if (pdata->disable)
665 pdata->disable();
666
667 dev_dbg(dev, "%s: return: %d\n", __func__, r);
668 return r;
669}
670
671static int wl1273_fm_suspend(struct wl1273_device *radio)
672{
673 struct wl1273_core *core = radio->core;
674 int r = 0;
675
676 /* Cannot go from OFF to SUSPENDED */
677 if (core->mode == WL1273_MODE_RX)
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300678 r = core->write(core, WL1273_POWER_SET,
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300679 WL1273_POWER_SET_RETENTION);
680 else if (core->mode == WL1273_MODE_TX)
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300681 r = core->write(core, WL1273_PUPD_SET,
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300682 WL1273_PUPD_SET_RETENTION);
683 else
684 r = -EINVAL;
685
686 if (r) {
687 dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r);
688 goto out;
689 }
690
691out:
692 return r;
693}
694
695static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode)
696{
697 struct wl1273_core *core = radio->core;
698 struct device *dev = radio->dev;
699 int old_mode;
700 int r;
701
702 dev_dbg(dev, "%s\n", __func__);
703 dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden);
704
705 old_mode = core->mode;
706 if (mode & radio->forbidden) {
707 r = -EPERM;
708 goto out;
709 }
710
711 switch (mode) {
712 case WL1273_MODE_RX:
713 case WL1273_MODE_TX:
714 r = wl1273_fm_start(radio, mode);
715 if (r) {
716 dev_err(dev, "%s: Cannot start.\n", __func__);
717 wl1273_fm_stop(radio);
718 goto out;
719 }
720
721 core->mode = mode;
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300722 r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300723 if (r) {
724 dev_err(dev, "INT_MASK_SET fails.\n");
725 goto out;
726 }
727
728 /* remember previous settings */
729 if (mode == WL1273_MODE_RX) {
730 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
731 if (r) {
732 dev_err(dev, "set freq fails: %d.\n", r);
733 goto out;
734 }
735
736 r = core->set_volume(core, core->volume);
737 if (r) {
738 dev_err(dev, "set volume fails: %d.\n", r);
739 goto out;
740 }
741
742 dev_dbg(dev, "%s: Set vol: %d.\n", __func__,
743 core->volume);
744 } else {
745 r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency);
746 if (r) {
747 dev_err(dev, "set freq fails: %d.\n", r);
748 goto out;
749 }
750 }
751
752 dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__);
753
754 r = core->set_audio(core, core->audio_mode);
755 if (r)
756 dev_err(dev, "Cannot set audio mode.\n");
757 break;
758
759 case WL1273_MODE_OFF:
760 r = wl1273_fm_stop(radio);
761 if (r)
762 dev_err(dev, "%s: Off fails: %d\n", __func__, r);
763 else
764 core->mode = WL1273_MODE_OFF;
765
766 break;
767
768 case WL1273_MODE_SUSPENDED:
769 r = wl1273_fm_suspend(radio);
770 if (r)
771 dev_err(dev, "%s: Suspend fails: %d\n", __func__, r);
772 else
773 core->mode = WL1273_MODE_SUSPENDED;
774
775 break;
776
777 default:
778 dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode);
779 r = -EINVAL;
780 break;
781 }
782out:
783 if (r)
784 core->mode = old_mode;
785
786 return r;
787}
788
789static int wl1273_fm_set_seek(struct wl1273_device *radio,
790 unsigned int wrap_around,
791 unsigned int seek_upward,
792 int level)
793{
794 struct wl1273_core *core = radio->core;
795 int r = 0;
796 unsigned int dir = (seek_upward == 0) ? 0 : 1;
797 unsigned int f;
798
799 f = radio->rx_frequency;
800 dev_dbg(radio->dev, "rx_frequency: %d\n", f);
801
802 if (dir && f + radio->spacing <= radio->rangehigh)
803 r = wl1273_fm_set_rx_freq(radio, f + radio->spacing);
804 else if (dir && wrap_around)
805 r = wl1273_fm_set_rx_freq(radio, radio->rangelow);
806 else if (f - radio->spacing >= radio->rangelow)
807 r = wl1273_fm_set_rx_freq(radio, f - radio->spacing);
808 else if (wrap_around)
809 r = wl1273_fm_set_rx_freq(radio, radio->rangehigh);
810
811 if (r)
812 goto out;
813
814 if (level < SCHAR_MIN || level > SCHAR_MAX)
815 return -EINVAL;
816
Wolfram Sang16735d02013-11-14 14:32:02 -0800817 reinit_completion(&radio->busy);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300818 dev_dbg(radio->dev, "%s: BUSY\n", __func__);
819
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300820 r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300821 if (r)
822 goto out;
823
824 dev_dbg(radio->dev, "%s\n", __func__);
825
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300826 r = core->write(core, WL1273_SEARCH_LVL_SET, level);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300827 if (r)
828 goto out;
829
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300830 r = core->write(core, WL1273_SEARCH_DIR_SET, dir);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300831 if (r)
832 goto out;
833
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300834 r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300835 if (r)
836 goto out;
837
Nicholas Mc Guirec973f76e2015-02-05 05:56:42 -0300838 /* wait for the FR IRQ */
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300839 wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
Nicholas Mc Guirec973f76e2015-02-05 05:56:42 -0300840 if (!(radio->irq_received & WL1273_BL_EVENT)) {
841 r = -ETIMEDOUT;
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300842 goto out;
Nicholas Mc Guirec973f76e2015-02-05 05:56:42 -0300843 }
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300844
845 radio->irq_received &= ~WL1273_BL_EVENT;
846
847 if (!wrap_around)
848 goto out;
849
850 /* Wrap around */
851 dev_dbg(radio->dev, "Wrap around in HW seek.\n");
852
853 if (seek_upward)
854 f = radio->rangelow;
855 else
856 f = radio->rangehigh;
857
858 r = wl1273_fm_set_rx_freq(radio, f);
859 if (r)
860 goto out;
861
Wolfram Sang16735d02013-11-14 14:32:02 -0800862 reinit_completion(&radio->busy);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300863 dev_dbg(radio->dev, "%s: BUSY\n", __func__);
864
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300865 r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300866 if (r)
867 goto out;
868
Nicholas Mc Guirec973f76e2015-02-05 05:56:42 -0300869 /* wait for the FR IRQ */
870 if (!wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000)))
871 r = -ETIMEDOUT;
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300872out:
873 dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r);
874 return r;
875}
876
877/**
878 * wl1273_fm_get_tx_ctune() - Get the TX tuning capacitor value.
879 * @radio: A pointer to the device struct.
880 */
881static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio)
882{
883 struct wl1273_core *core = radio->core;
884 struct device *dev = radio->dev;
885 u16 val;
886 int r;
887
888 if (core->mode == WL1273_MODE_OFF ||
889 core->mode == WL1273_MODE_SUSPENDED)
890 return -EPERM;
891
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300892 r = core->read(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300893 if (r) {
894 dev_err(dev, "%s: read error: %d\n", __func__, r);
895 goto out;
896 }
897
898out:
899 return val;
900}
901
902/**
903 * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
904 * @radio: A pointer to the device struct.
905 * @preemphasis: The new pre-amphasis value.
906 *
907 * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
908 * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
909 */
910static int wl1273_fm_set_preemphasis(struct wl1273_device *radio,
911 unsigned int preemphasis)
912{
913 struct wl1273_core *core = radio->core;
914 int r;
915 u16 em;
916
917 if (core->mode == WL1273_MODE_OFF ||
918 core->mode == WL1273_MODE_SUSPENDED)
919 return -EPERM;
920
921 mutex_lock(&core->lock);
922
923 switch (preemphasis) {
924 case V4L2_PREEMPHASIS_DISABLED:
925 em = 1;
926 break;
927 case V4L2_PREEMPHASIS_50_uS:
928 em = 0;
929 break;
930 case V4L2_PREEMPHASIS_75_uS:
931 em = 2;
932 break;
933 default:
934 r = -EINVAL;
935 goto out;
936 }
937
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300938 r = core->write(core, WL1273_PREMPH_SET, em);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300939 if (r)
940 goto out;
941
942 radio->preemphasis = preemphasis;
943
944out:
945 mutex_unlock(&core->lock);
946 return r;
947}
948
949static int wl1273_fm_rds_on(struct wl1273_device *radio)
950{
951 struct wl1273_core *core = radio->core;
952 int r;
953
954 dev_dbg(radio->dev, "%s\n", __func__);
955 if (radio->rds_on)
956 return 0;
957
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300958 r = core->write(core, WL1273_POWER_SET,
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300959 WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
960 if (r)
961 goto out;
962
963 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
964 if (r)
965 dev_err(radio->dev, "set freq fails: %d.\n", r);
966out:
967 return r;
968}
969
970static int wl1273_fm_rds_off(struct wl1273_device *radio)
971{
972 struct wl1273_core *core = radio->core;
973 int r;
974
975 if (!radio->rds_on)
976 return 0;
977
978 radio->irq_flags &= ~WL1273_RDS_EVENT;
979
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300980 r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300981 if (r)
982 goto out;
983
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300984 /* Service pending read */
985 wake_up_interruptible(&radio->read_queue);
986
987 dev_dbg(radio->dev, "%s\n", __func__);
988
Matti Aaltonen96a9cc62011-03-01 10:10:36 -0300989 r = core->write(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
Matti Aaltonen87d1a502010-12-10 11:41:34 -0300990 if (r)
991 goto out;
992
993 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
994 if (r)
995 dev_err(radio->dev, "set freq fails: %d.\n", r);
996out:
997 dev_dbg(radio->dev, "%s: exiting...\n", __func__);
998
999 return r;
1000}
1001
1002static int wl1273_fm_set_rds(struct wl1273_device *radio, unsigned int new_mode)
1003{
1004 int r = 0;
1005 struct wl1273_core *core = radio->core;
1006
1007 if (core->mode == WL1273_MODE_OFF ||
1008 core->mode == WL1273_MODE_SUSPENDED)
1009 return -EPERM;
1010
1011 if (new_mode == WL1273_RDS_RESET) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001012 r = core->write(core, WL1273_RDS_CNTRL_SET, 1);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001013 return r;
1014 }
1015
1016 if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001017 r = core->write(core, WL1273_RDS_DATA_ENB, 0);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001018 } else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001019 r = core->write(core, WL1273_RDS_DATA_ENB, 1);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001020 } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) {
1021 r = wl1273_fm_rds_off(radio);
1022 } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) {
1023 r = wl1273_fm_rds_on(radio);
1024 } else {
1025 dev_err(radio->dev, "%s: Unknown mode: %d\n",
1026 __func__, new_mode);
1027 r = -EINVAL;
1028 }
1029
1030 if (!r)
1031 radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false;
1032
1033 return r;
1034}
1035
1036static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf,
1037 size_t count, loff_t *ppos)
1038{
1039 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001040 struct wl1273_core *core = radio->core;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001041 u16 val;
1042 int r;
1043
1044 dev_dbg(radio->dev, "%s\n", __func__);
1045
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001046 if (core->mode != WL1273_MODE_TX)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001047 return count;
1048
1049 if (radio->rds_users == 0) {
1050 dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1051 return 0;
1052 }
1053
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001054 if (mutex_lock_interruptible(&core->lock))
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001055 return -EINTR;
1056 /*
1057 * Multiple processes can open the device, but only
1058 * one gets to write to it.
1059 */
1060 if (radio->owner && radio->owner != file) {
1061 r = -EBUSY;
1062 goto out;
1063 }
1064 radio->owner = file;
1065
1066 /* Manual Mode */
1067 if (count > 255)
1068 val = 255;
1069 else
1070 val = count;
1071
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001072 core->write(core, WL1273_RDS_CONFIG_DATA_SET, val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001073
1074 if (copy_from_user(radio->write_buf + 1, buf, val)) {
1075 r = -EFAULT;
1076 goto out;
1077 }
1078
1079 dev_dbg(radio->dev, "Count: %d\n", val);
1080 dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf);
1081
1082 radio->write_buf[0] = WL1273_RDS_DATA_SET;
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001083 core->write_data(core, radio->write_buf, val + 1);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001084
1085 r = val;
1086out:
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001087 mutex_unlock(&core->lock);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001088
1089 return r;
1090}
1091
1092static unsigned int wl1273_fm_fops_poll(struct file *file,
1093 struct poll_table_struct *pts)
1094{
1095 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1096 struct wl1273_core *core = radio->core;
1097
1098 if (radio->owner && radio->owner != file)
1099 return -EBUSY;
1100
1101 radio->owner = file;
1102
1103 if (core->mode == WL1273_MODE_RX) {
1104 poll_wait(file, &radio->read_queue, pts);
1105
1106 if (radio->rd_index != radio->wr_index)
1107 return POLLIN | POLLRDNORM;
1108
1109 } else if (core->mode == WL1273_MODE_TX) {
1110 return POLLOUT | POLLWRNORM;
1111 }
1112
1113 return 0;
1114}
1115
1116static int wl1273_fm_fops_open(struct file *file)
1117{
1118 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1119 struct wl1273_core *core = radio->core;
1120 int r = 0;
1121
1122 dev_dbg(radio->dev, "%s\n", __func__);
1123
1124 if (core->mode == WL1273_MODE_RX && radio->rds_on &&
1125 !radio->rds_users) {
1126 dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode);
1127
1128 if (mutex_lock_interruptible(&core->lock))
1129 return -EINTR;
1130
1131 radio->irq_flags |= WL1273_RDS_EVENT;
1132
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001133 r = core->write(core, WL1273_INT_MASK_SET,
1134 radio->irq_flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001135 if (r) {
1136 mutex_unlock(&core->lock);
1137 goto out;
1138 }
1139
1140 radio->rds_users++;
1141
1142 mutex_unlock(&core->lock);
1143 }
1144out:
1145 return r;
1146}
1147
1148static int wl1273_fm_fops_release(struct file *file)
1149{
1150 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1151 struct wl1273_core *core = radio->core;
1152 int r = 0;
1153
1154 dev_dbg(radio->dev, "%s\n", __func__);
1155
1156 if (radio->rds_users > 0) {
1157 radio->rds_users--;
1158 if (radio->rds_users == 0) {
1159 if (mutex_lock_interruptible(&core->lock))
1160 return -EINTR;
1161
1162 radio->irq_flags &= ~WL1273_RDS_EVENT;
1163
1164 if (core->mode == WL1273_MODE_RX) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001165 r = core->write(core,
1166 WL1273_INT_MASK_SET,
1167 radio->irq_flags);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001168 if (r) {
1169 mutex_unlock(&core->lock);
1170 goto out;
1171 }
1172 }
1173 mutex_unlock(&core->lock);
1174 }
1175 }
1176
1177 if (file == radio->owner)
1178 radio->owner = NULL;
1179out:
1180 return r;
1181}
1182
1183static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
1184 size_t count, loff_t *ppos)
1185{
1186 int r = 0;
1187 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1188 struct wl1273_core *core = radio->core;
1189 unsigned int block_count = 0;
1190 u16 val;
1191
1192 dev_dbg(radio->dev, "%s\n", __func__);
1193
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001194 if (core->mode != WL1273_MODE_RX)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001195 return 0;
1196
1197 if (radio->rds_users == 0) {
1198 dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1199 return 0;
1200 }
1201
1202 if (mutex_lock_interruptible(&core->lock))
1203 return -EINTR;
1204
1205 /*
1206 * Multiple processes can open the device, but only
1207 * one at a time gets read access.
1208 */
1209 if (radio->owner && radio->owner != file) {
1210 r = -EBUSY;
1211 goto out;
1212 }
1213 radio->owner = file;
1214
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001215 r = core->read(core, WL1273_RDS_SYNC_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001216 if (r) {
1217 dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__);
1218 goto out;
1219 } else if (val == 0) {
1220 dev_info(radio->dev, "RDS_SYNC: Not synchronized\n");
1221 r = -ENODATA;
1222 goto out;
1223 }
1224
1225 /* block if no new data available */
1226 while (radio->wr_index == radio->rd_index) {
1227 if (file->f_flags & O_NONBLOCK) {
1228 r = -EWOULDBLOCK;
1229 goto out;
1230 }
1231
1232 dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__);
1233 if (wait_event_interruptible(radio->read_queue,
1234 radio->wr_index !=
1235 radio->rd_index) < 0) {
1236 r = -EINTR;
1237 goto out;
1238 }
1239 }
1240
1241 /* calculate block count from byte count */
1242 count /= RDS_BLOCK_SIZE;
1243
1244 /* copy RDS blocks from the internal buffer and to user buffer */
1245 while (block_count < count) {
1246 if (radio->rd_index == radio->wr_index)
1247 break;
1248
1249 /* always transfer complete RDS blocks */
1250 if (copy_to_user(buf, &radio->buffer[radio->rd_index],
1251 RDS_BLOCK_SIZE))
1252 break;
1253
1254 /* increment and wrap the read pointer */
1255 radio->rd_index += RDS_BLOCK_SIZE;
1256 if (radio->rd_index >= radio->buf_size)
1257 radio->rd_index = 0;
1258
1259 /* increment counters */
1260 block_count++;
1261 buf += RDS_BLOCK_SIZE;
1262 r += RDS_BLOCK_SIZE;
1263 }
1264
1265out:
1266 dev_dbg(radio->dev, "%s: exit\n", __func__);
1267 mutex_unlock(&core->lock);
1268
1269 return r;
1270}
1271
1272static const struct v4l2_file_operations wl1273_fops = {
1273 .owner = THIS_MODULE,
1274 .read = wl1273_fm_fops_read,
1275 .write = wl1273_fm_fops_write,
1276 .poll = wl1273_fm_fops_poll,
Matti Aaltonen34b8fc82011-01-04 08:29:37 -03001277 .unlocked_ioctl = video_ioctl2,
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001278 .open = wl1273_fm_fops_open,
1279 .release = wl1273_fm_fops_release,
1280};
1281
1282static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
1283 struct v4l2_capability *capability)
1284{
1285 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1286
1287 dev_dbg(radio->dev, "%s\n", __func__);
1288
1289 strlcpy(capability->driver, WL1273_FM_DRIVER_NAME,
1290 sizeof(capability->driver));
1291 strlcpy(capability->card, "Texas Instruments Wl1273 FM Radio",
1292 sizeof(capability->card));
1293 strlcpy(capability->bus_info, radio->bus_type,
1294 sizeof(capability->bus_info));
1295
Hans Verkuil7027c4482014-11-24 06:37:24 -03001296 capability->device_caps = V4L2_CAP_HW_FREQ_SEEK |
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001297 V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
1298 V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
1299 V4L2_CAP_RDS_OUTPUT;
Hans Verkuil7027c4482014-11-24 06:37:24 -03001300 capability->capabilities = capability->device_caps |
1301 V4L2_CAP_DEVICE_CAPS;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001302
1303 return 0;
1304}
1305
1306static int wl1273_fm_vidioc_g_input(struct file *file, void *priv,
1307 unsigned int *i)
1308{
1309 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1310
1311 dev_dbg(radio->dev, "%s\n", __func__);
1312
1313 *i = 0;
1314
1315 return 0;
1316}
1317
1318static int wl1273_fm_vidioc_s_input(struct file *file, void *priv,
1319 unsigned int i)
1320{
1321 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1322
1323 dev_dbg(radio->dev, "%s\n", __func__);
1324
1325 if (i != 0)
1326 return -EINVAL;
1327
1328 return 0;
1329}
1330
1331/**
1332 * wl1273_fm_set_tx_power() - Set the transmission power value.
1333 * @core: A pointer to the device struct.
1334 * @power: The new power value.
1335 */
1336static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power)
1337{
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001338 struct wl1273_core *core = radio->core;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001339 int r;
1340
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001341 if (core->mode == WL1273_MODE_OFF ||
1342 core->mode == WL1273_MODE_SUSPENDED)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001343 return -EPERM;
1344
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001345 mutex_lock(&core->lock);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001346
1347 /* Convert the dBuV value to chip presentation */
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001348 r = core->write(core, WL1273_POWER_LEV_SET, 122 - power);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001349 if (r)
1350 goto out;
1351
1352 radio->tx_power = power;
1353
1354out:
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001355 mutex_unlock(&core->lock);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001356 return r;
1357}
1358
1359#define WL1273_SPACING_50kHz 1
1360#define WL1273_SPACING_100kHz 2
1361#define WL1273_SPACING_200kHz 4
1362
1363static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio,
1364 unsigned int spacing)
1365{
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001366 struct wl1273_core *core = radio->core;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001367 int r;
1368
1369 if (spacing == 0) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001370 r = core->write(core, WL1273_SCAN_SPACING_SET,
1371 WL1273_SPACING_100kHz);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001372 radio->spacing = 100;
1373 } else if (spacing - 50000 < 25000) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001374 r = core->write(core, WL1273_SCAN_SPACING_SET,
1375 WL1273_SPACING_50kHz);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001376 radio->spacing = 50;
1377 } else if (spacing - 100000 < 50000) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001378 r = core->write(core, WL1273_SCAN_SPACING_SET,
1379 WL1273_SPACING_100kHz);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001380 radio->spacing = 100;
1381 } else {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001382 r = core->write(core, WL1273_SCAN_SPACING_SET,
1383 WL1273_SPACING_200kHz);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001384 radio->spacing = 200;
1385 }
1386
1387 return r;
1388}
1389
1390static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1391{
1392 struct wl1273_device *radio = ctrl->priv;
1393 struct wl1273_core *core = radio->core;
1394
1395 dev_dbg(radio->dev, "%s\n", __func__);
1396
1397 if (mutex_lock_interruptible(&core->lock))
1398 return -EINTR;
1399
1400 switch (ctrl->id) {
1401 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
Hans Verkuilddac5c12011-06-10 05:43:34 -03001402 ctrl->val = wl1273_fm_get_tx_ctune(radio);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001403 break;
1404
1405 default:
1406 dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
1407 __func__, ctrl->id);
1408 break;
1409 }
1410
1411 mutex_unlock(&core->lock);
1412
1413 return 0;
1414}
1415
1416#define WL1273_MUTE_SOFT_ENABLE (1 << 0)
1417#define WL1273_MUTE_AC (1 << 1)
1418#define WL1273_MUTE_HARD_LEFT (1 << 2)
1419#define WL1273_MUTE_HARD_RIGHT (1 << 3)
1420#define WL1273_MUTE_SOFT_FORCE (1 << 4)
1421
1422static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl)
1423{
1424 return container_of(ctrl->handler, struct wl1273_device, ctrl_handler);
1425}
1426
1427static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl)
1428{
1429 struct wl1273_device *radio = to_radio(ctrl);
1430 struct wl1273_core *core = radio->core;
1431 int r = 0;
1432
1433 dev_dbg(radio->dev, "%s\n", __func__);
1434
1435 switch (ctrl->id) {
1436 case V4L2_CID_AUDIO_MUTE:
1437 if (mutex_lock_interruptible(&core->lock))
1438 return -EINTR;
1439
1440 if (core->mode == WL1273_MODE_RX && ctrl->val)
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001441 r = core->write(core,
1442 WL1273_MUTE_STATUS_SET,
1443 WL1273_MUTE_HARD_LEFT |
1444 WL1273_MUTE_HARD_RIGHT);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001445 else if (core->mode == WL1273_MODE_RX)
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001446 r = core->write(core,
1447 WL1273_MUTE_STATUS_SET, 0x0);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001448 else if (core->mode == WL1273_MODE_TX && ctrl->val)
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001449 r = core->write(core, WL1273_MUTE, 1);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001450 else if (core->mode == WL1273_MODE_TX)
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001451 r = core->write(core, WL1273_MUTE, 0);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001452
1453 mutex_unlock(&core->lock);
1454 break;
1455
1456 case V4L2_CID_AUDIO_VOLUME:
1457 if (ctrl->val == 0)
1458 r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF);
1459 else
1460 r = core->set_volume(core, core->volume);
1461 break;
1462
1463 case V4L2_CID_TUNE_PREEMPHASIS:
1464 r = wl1273_fm_set_preemphasis(radio, ctrl->val);
1465 break;
1466
1467 case V4L2_CID_TUNE_POWER_LEVEL:
1468 r = wl1273_fm_set_tx_power(radio, ctrl->val);
1469 break;
1470
1471 default:
1472 dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
1473 __func__, ctrl->id);
1474 break;
1475 }
1476
1477 dev_dbg(radio->dev, "%s\n", __func__);
1478 return r;
1479}
1480
1481static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv,
1482 struct v4l2_audio *audio)
1483{
1484 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1485
1486 dev_dbg(radio->dev, "%s\n", __func__);
1487
1488 if (audio->index > 1)
1489 return -EINVAL;
1490
1491 strlcpy(audio->name, "Radio", sizeof(audio->name));
1492 audio->capability = V4L2_AUDCAP_STEREO;
1493
1494 return 0;
1495}
1496
1497static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001498 const struct v4l2_audio *audio)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001499{
1500 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1501
1502 dev_dbg(radio->dev, "%s\n", __func__);
1503
1504 if (audio->index != 0)
1505 return -EINVAL;
1506
1507 return 0;
1508}
1509
1510#define WL1273_RDS_NOT_SYNCHRONIZED 0
1511#define WL1273_RDS_SYNCHRONIZED 1
1512
1513static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv,
1514 struct v4l2_tuner *tuner)
1515{
1516 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1517 struct wl1273_core *core = radio->core;
1518 u16 val;
1519 int r;
1520
1521 dev_dbg(radio->dev, "%s\n", __func__);
1522
1523 if (tuner->index > 0)
1524 return -EINVAL;
1525
1526 strlcpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name));
1527 tuner->type = V4L2_TUNER_RADIO;
1528
1529 tuner->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
1530 tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
1531
1532 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
Hans Verkuil54f60192012-05-27 07:25:06 -03001533 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO |
1534 V4L2_TUNER_CAP_HWSEEK_BOUNDED | V4L2_TUNER_CAP_HWSEEK_WRAP;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001535
1536 if (radio->stereo)
1537 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1538 else
1539 tuner->audmode = V4L2_TUNER_MODE_MONO;
1540
1541 if (core->mode != WL1273_MODE_RX)
1542 return 0;
1543
1544 if (mutex_lock_interruptible(&core->lock))
1545 return -EINTR;
1546
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001547 r = core->read(core, WL1273_STEREO_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001548 if (r)
1549 goto out;
1550
1551 if (val == 1)
1552 tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
1553 else
1554 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1555
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001556 r = core->read(core, WL1273_RSSI_LVL_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001557 if (r)
1558 goto out;
1559
1560 tuner->signal = (s16) val;
1561 dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);
1562
1563 tuner->afc = 0;
1564
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001565 r = core->read(core, WL1273_RDS_SYNC_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001566 if (r)
1567 goto out;
1568
1569 if (val == WL1273_RDS_SYNCHRONIZED)
1570 tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
1571out:
1572 mutex_unlock(&core->lock);
1573
1574 return r;
1575}
1576
1577static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001578 const struct v4l2_tuner *tuner)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001579{
1580 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1581 struct wl1273_core *core = radio->core;
1582 int r = 0;
1583
1584 dev_dbg(radio->dev, "%s\n", __func__);
1585 dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index);
1586 dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name);
1587 dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability);
1588 dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans);
1589 dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow);
1590 dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh);
1591
1592 if (tuner->index > 0)
1593 return -EINVAL;
1594
1595 if (mutex_lock_interruptible(&core->lock))
1596 return -EINTR;
1597
1598 r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1599 if (r)
1600 goto out;
1601
1602 if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS)
1603 r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1604 else
1605 r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1606
1607 if (r)
1608 dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r);
1609
1610 if (tuner->audmode == V4L2_TUNER_MODE_MONO) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001611 r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001612 if (r < 0) {
1613 dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1614 __func__, r);
1615 goto out;
1616 }
1617 radio->stereo = false;
1618 } else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001619 r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001620 if (r < 0) {
1621 dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1622 __func__, r);
1623 goto out;
1624 }
1625 radio->stereo = true;
1626 } else {
1627 dev_err(radio->dev, "%s: tuner->audmode: %d\n",
1628 __func__, tuner->audmode);
1629 r = -EINVAL;
1630 goto out;
1631 }
1632
1633out:
1634 mutex_unlock(&core->lock);
1635
1636 return r;
1637}
1638
1639static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
1640 struct v4l2_frequency *freq)
1641{
1642 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1643 struct wl1273_core *core = radio->core;
1644
1645 dev_dbg(radio->dev, "%s\n", __func__);
1646
1647 if (mutex_lock_interruptible(&core->lock))
1648 return -EINTR;
1649
1650 freq->type = V4L2_TUNER_RADIO;
1651 freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio));
1652
1653 mutex_unlock(&core->lock);
1654
1655 return 0;
1656}
1657
1658static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001659 const struct v4l2_frequency *freq)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001660{
1661 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1662 struct wl1273_core *core = radio->core;
1663 int r;
1664
1665 dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency);
1666
1667 if (freq->type != V4L2_TUNER_RADIO) {
1668 dev_dbg(radio->dev,
1669 "freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
1670 return -EINVAL;
1671 }
1672
1673 if (mutex_lock_interruptible(&core->lock))
1674 return -EINTR;
1675
1676 if (core->mode == WL1273_MODE_RX) {
1677 dev_dbg(radio->dev, "freq: %d\n", freq->frequency);
1678
1679 r = wl1273_fm_set_rx_freq(radio,
1680 WL1273_INV_FREQ(freq->frequency));
1681 if (r)
1682 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1683 ": set frequency failed with %d\n", r);
1684 } else {
1685 r = wl1273_fm_set_tx_freq(radio,
1686 WL1273_INV_FREQ(freq->frequency));
1687 if (r)
1688 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1689 ": set frequency failed with %d\n", r);
1690 }
1691
1692 mutex_unlock(&core->lock);
1693
1694 dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
1695 return r;
1696}
1697
1698#define WL1273_DEFAULT_SEEK_LEVEL 7
1699
1700static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
Hans Verkuilec6f4322012-09-14 07:41:18 -03001701 const struct v4l2_hw_freq_seek *seek)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001702{
1703 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1704 struct wl1273_core *core = radio->core;
1705 int r;
1706
1707 dev_dbg(radio->dev, "%s\n", __func__);
1708
1709 if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
1710 return -EINVAL;
1711
Hans Verkuil617ade62012-09-21 09:33:35 -03001712 if (file->f_flags & O_NONBLOCK)
1713 return -EWOULDBLOCK;
1714
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001715 if (mutex_lock_interruptible(&core->lock))
1716 return -EINTR;
1717
1718 r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1719 if (r)
1720 goto out;
1721
1722 r = wl1273_fm_tx_set_spacing(radio, seek->spacing);
1723 if (r)
1724 dev_warn(radio->dev, "HW seek failed: %d\n", r);
1725
1726 r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward,
1727 WL1273_DEFAULT_SEEK_LEVEL);
1728 if (r)
1729 dev_warn(radio->dev, "HW seek failed: %d\n", r);
1730
1731out:
1732 mutex_unlock(&core->lock);
1733 return r;
1734}
1735
1736static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
Hans Verkuil3f70e1f2012-09-04 12:08:47 -03001737 const struct v4l2_modulator *modulator)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001738{
1739 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1740 struct wl1273_core *core = radio->core;
1741 int r = 0;
1742
1743 dev_dbg(radio->dev, "%s\n", __func__);
1744
1745 if (modulator->index > 0)
1746 return -EINVAL;
1747
1748 if (mutex_lock_interruptible(&core->lock))
1749 return -EINTR;
1750
1751 r = wl1273_fm_set_mode(radio, WL1273_MODE_TX);
1752 if (r)
1753 goto out;
1754
1755 if (modulator->txsubchans & V4L2_TUNER_SUB_RDS)
1756 r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1757 else
1758 r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1759
1760 if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001761 r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001762 else
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001763 r = core->write(core, WL1273_MONO_SET,
1764 WL1273_RX_STEREO);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001765 if (r < 0)
1766 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1767 "MONO_SET fails: %d\n", r);
1768out:
1769 mutex_unlock(&core->lock);
1770
1771 return r;
1772}
1773
1774static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
1775 struct v4l2_modulator *modulator)
1776{
1777 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1778 struct wl1273_core *core = radio->core;
1779 u16 val;
1780 int r;
1781
1782 dev_dbg(radio->dev, "%s\n", __func__);
1783
1784 strlcpy(modulator->name, WL1273_FM_DRIVER_NAME,
1785 sizeof(modulator->name));
1786
1787 modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
1788 modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
1789
1790 modulator->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
1791 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
1792
1793 if (core->mode != WL1273_MODE_TX)
1794 return 0;
1795
1796 if (mutex_lock_interruptible(&core->lock))
1797 return -EINTR;
1798
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001799 r = core->read(core, WL1273_MONO_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001800 if (r)
1801 goto out;
1802
1803 if (val == WL1273_TX_STEREO)
1804 modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
1805 else
1806 modulator->txsubchans = V4L2_TUNER_SUB_MONO;
1807
1808 if (radio->rds_on)
1809 modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
1810out:
1811 mutex_unlock(&core->lock);
1812
1813 return 0;
1814}
1815
1816static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
1817{
1818 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1819 struct wl1273_core *core = radio->core;
1820 struct device *dev = radio->dev;
1821 u16 val;
1822 int r;
1823
1824 dev_info(dev, DRIVER_DESC);
1825
1826 if (core->mode == WL1273_MODE_OFF) {
1827 dev_info(dev, "Mode: Off\n");
1828 return 0;
1829 }
1830
1831 if (core->mode == WL1273_MODE_SUSPENDED) {
1832 dev_info(dev, "Mode: Suspended\n");
1833 return 0;
1834 }
1835
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001836 r = core->read(core, WL1273_ASIC_ID_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001837 if (r)
1838 dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__);
1839 else
1840 dev_info(dev, "ASIC_ID: 0x%04x\n", val);
1841
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001842 r = core->read(core, WL1273_ASIC_VER_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001843 if (r)
1844 dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__);
1845 else
1846 dev_info(dev, "ASIC Version: 0x%04x\n", val);
1847
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001848 r = core->read(core, WL1273_FIRM_VER_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001849 if (r)
1850 dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__);
1851 else
1852 dev_info(dev, "FW version: %d(0x%04x)\n", val, val);
1853
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001854 r = core->read(core, WL1273_BAND_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001855 if (r)
1856 dev_err(dev, "%s: Get BAND fails.\n", __func__);
1857 else
1858 dev_info(dev, "BAND: %d\n", val);
1859
1860 if (core->mode == WL1273_MODE_TX) {
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001861 r = core->read(core, WL1273_PUPD_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001862 if (r)
1863 dev_err(dev, "%s: Get PUPD fails.\n", __func__);
1864 else
1865 dev_info(dev, "PUPD: 0x%04x\n", val);
1866
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001867 r = core->read(core, WL1273_CHANL_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001868 if (r)
1869 dev_err(dev, "%s: Get CHANL fails.\n", __func__);
1870 else
1871 dev_info(dev, "Tx frequency: %dkHz\n", val*10);
1872 } else if (core->mode == WL1273_MODE_RX) {
1873 int bf = radio->rangelow;
1874
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001875 r = core->read(core, WL1273_FREQ_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001876 if (r)
1877 dev_err(dev, "%s: Get FREQ fails.\n", __func__);
1878 else
1879 dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50);
1880
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001881 r = core->read(core, WL1273_MOST_MODE_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001882 if (r)
1883 dev_err(dev, "%s: Get MOST_MODE fails.\n",
1884 __func__);
1885 else if (val == 0)
1886 dev_info(dev, "MOST_MODE: Stereo according to blend\n");
1887 else if (val == 1)
1888 dev_info(dev, "MOST_MODE: Force mono output\n");
1889 else
1890 dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val);
1891
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001892 r = core->read(core, WL1273_MOST_BLEND_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001893 if (r)
1894 dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__);
1895 else if (val == 0)
1896 dev_info(dev,
1897 "MOST_BLEND: Switched blend & hysteresis.\n");
1898 else if (val == 1)
1899 dev_info(dev, "MOST_BLEND: Soft blend.\n");
1900 else
1901 dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val);
1902
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001903 r = core->read(core, WL1273_STEREO_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001904 if (r)
1905 dev_err(dev, "%s: Get STEREO fails.\n", __func__);
1906 else if (val == 0)
1907 dev_info(dev, "STEREO: Not detected\n");
1908 else if (val == 1)
1909 dev_info(dev, "STEREO: Detected\n");
1910 else
1911 dev_info(dev, "STEREO: Unexpected value: %d\n", val);
1912
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001913 r = core->read(core, WL1273_RSSI_LVL_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001914 if (r)
1915 dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__);
1916 else
1917 dev_info(dev, "RX signal strength: %d\n", (s16) val);
1918
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001919 r = core->read(core, WL1273_POWER_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001920 if (r)
1921 dev_err(dev, "%s: Get POWER fails.\n", __func__);
1922 else
1923 dev_info(dev, "POWER: 0x%04x\n", val);
1924
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001925 r = core->read(core, WL1273_INT_MASK_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001926 if (r)
1927 dev_err(dev, "%s: Get INT_MASK fails.\n", __func__);
1928 else
1929 dev_info(dev, "INT_MASK: 0x%04x\n", val);
1930
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001931 r = core->read(core, WL1273_RDS_SYNC_GET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001932 if (r)
1933 dev_err(dev, "%s: Get RDS_SYNC fails.\n",
1934 __func__);
1935 else if (val == 0)
1936 dev_info(dev, "RDS_SYNC: Not synchronized\n");
1937
1938 else if (val == 1)
1939 dev_info(dev, "RDS_SYNC: Synchronized\n");
1940 else
1941 dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val);
1942
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001943 r = core->read(core, WL1273_I2S_MODE_CONFIG_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001944 if (r)
1945 dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n",
1946 __func__);
1947 else
1948 dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val);
1949
Matti Aaltonen96a9cc62011-03-01 10:10:36 -03001950 r = core->read(core, WL1273_VOLUME_SET, &val);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001951 if (r)
1952 dev_err(dev, "%s: Get VOLUME fails.\n", __func__);
1953 else
1954 dev_info(dev, "VOLUME: 0x%04x\n", val);
1955 }
1956
1957 return 0;
1958}
1959
1960static void wl1273_vdev_release(struct video_device *dev)
1961{
1962}
1963
1964static const struct v4l2_ctrl_ops wl1273_ctrl_ops = {
1965 .s_ctrl = wl1273_fm_vidioc_s_ctrl,
1966 .g_volatile_ctrl = wl1273_fm_g_volatile_ctrl,
1967};
1968
1969static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
1970 .vidioc_querycap = wl1273_fm_vidioc_querycap,
1971 .vidioc_g_input = wl1273_fm_vidioc_g_input,
1972 .vidioc_s_input = wl1273_fm_vidioc_s_input,
1973 .vidioc_g_audio = wl1273_fm_vidioc_g_audio,
1974 .vidioc_s_audio = wl1273_fm_vidioc_s_audio,
1975 .vidioc_g_tuner = wl1273_fm_vidioc_g_tuner,
1976 .vidioc_s_tuner = wl1273_fm_vidioc_s_tuner,
1977 .vidioc_g_frequency = wl1273_fm_vidioc_g_frequency,
1978 .vidioc_s_frequency = wl1273_fm_vidioc_s_frequency,
1979 .vidioc_s_hw_freq_seek = wl1273_fm_vidioc_s_hw_freq_seek,
1980 .vidioc_g_modulator = wl1273_fm_vidioc_g_modulator,
1981 .vidioc_s_modulator = wl1273_fm_vidioc_s_modulator,
1982 .vidioc_log_status = wl1273_fm_vidioc_log_status,
1983};
1984
1985static struct video_device wl1273_viddev_template = {
1986 .fops = &wl1273_fops,
1987 .ioctl_ops = &wl1273_ioctl_ops,
1988 .name = WL1273_FM_DRIVER_NAME,
1989 .release = wl1273_vdev_release,
Hans Verkuilce4a3d52013-01-05 08:52:12 -03001990 .vfl_dir = VFL_DIR_TX,
Matti Aaltonen87d1a502010-12-10 11:41:34 -03001991};
1992
1993static int wl1273_fm_radio_remove(struct platform_device *pdev)
1994{
1995 struct wl1273_device *radio = platform_get_drvdata(pdev);
1996 struct wl1273_core *core = radio->core;
1997
1998 dev_info(&pdev->dev, "%s.\n", __func__);
1999
2000 free_irq(core->client->irq, radio);
2001 core->pdata->free_resources();
2002
2003 v4l2_ctrl_handler_free(&radio->ctrl_handler);
2004 video_unregister_device(&radio->videodev);
2005 v4l2_device_unregister(&radio->v4l2dev);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002006
2007 return 0;
2008}
2009
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002010static int wl1273_fm_radio_probe(struct platform_device *pdev)
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002011{
Samuel Ortiz9e554692011-04-06 11:56:04 +02002012 struct wl1273_core **core = pdev->dev.platform_data;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002013 struct wl1273_device *radio;
2014 struct v4l2_ctrl *ctrl;
2015 int r = 0;
2016
2017 pr_debug("%s\n", __func__);
2018
2019 if (!core) {
2020 dev_err(&pdev->dev, "No platform data.\n");
2021 r = -EINVAL;
2022 goto pdata_err;
2023 }
2024
Julia Lawalld91e0132012-07-31 05:21:35 -03002025 radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002026 if (!radio) {
2027 r = -ENOMEM;
2028 goto pdata_err;
2029 }
2030
2031 /* RDS buffer allocation */
2032 radio->buf_size = rds_buf * RDS_BLOCK_SIZE;
Julia Lawalld91e0132012-07-31 05:21:35 -03002033 radio->buffer = devm_kzalloc(&pdev->dev, radio->buf_size, GFP_KERNEL);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002034 if (!radio->buffer) {
2035 pr_err("Cannot allocate memory for RDS buffer.\n");
2036 r = -ENOMEM;
Julia Lawalld91e0132012-07-31 05:21:35 -03002037 goto pdata_err;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002038 }
2039
2040 radio->core = *core;
2041 radio->irq_flags = WL1273_IRQ_MASK;
2042 radio->dev = &radio->core->client->dev;
2043 radio->rds_on = false;
2044 radio->core->mode = WL1273_MODE_OFF;
2045 radio->tx_power = 118;
2046 radio->core->audio_mode = WL1273_AUDIO_ANALOG;
2047 radio->band = WL1273_BAND_OTHER;
2048 radio->core->i2s_mode = WL1273_I2S_DEF_MODE;
2049 radio->core->channel_number = 2;
2050 radio->core->volume = WL1273_DEFAULT_VOLUME;
2051 radio->rx_frequency = WL1273_BAND_OTHER_LOW;
2052 radio->tx_frequency = WL1273_BAND_OTHER_HIGH;
2053 radio->rangelow = WL1273_BAND_OTHER_LOW;
2054 radio->rangehigh = WL1273_BAND_OTHER_HIGH;
2055 radio->stereo = true;
2056 radio->bus_type = "I2C";
2057
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002058 if (radio->core->pdata->request_resources) {
2059 r = radio->core->pdata->request_resources(radio->core->client);
2060 if (r) {
2061 dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2062 ": Cannot get platform data\n");
Julia Lawalld91e0132012-07-31 05:21:35 -03002063 goto pdata_err;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002064 }
2065
2066 dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq);
2067
2068 r = request_threaded_irq(radio->core->client->irq, NULL,
2069 wl1273_fm_irq_thread_handler,
2070 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
2071 "wl1273-fm", radio);
2072 if (r < 0) {
2073 dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2074 ": Unable to register IRQ handler: %d\n", r);
2075 goto err_request_irq;
2076 }
2077 } else {
Mauro Carvalho Chehab547633d2016-10-18 17:44:25 -02002078 dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ not configured");
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002079 r = -EINVAL;
Julia Lawalld91e0132012-07-31 05:21:35 -03002080 goto pdata_err;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002081 }
2082
2083 init_completion(&radio->busy);
2084 init_waitqueue_head(&radio->read_queue);
2085
Julia Lawalld91e0132012-07-31 05:21:35 -03002086 radio->write_buf = devm_kzalloc(&pdev->dev, 256, GFP_KERNEL);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002087 if (!radio->write_buf) {
2088 r = -ENOMEM;
2089 goto write_buf_err;
2090 }
2091
2092 radio->dev = &pdev->dev;
2093 radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
2094 radio->rds_users = 0;
2095
2096 r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
2097 if (r) {
2098 dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
Julia Lawalld91e0132012-07-31 05:21:35 -03002099 goto write_buf_err;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002100 }
2101
2102 /* V4L2 configuration */
Ezequiel Garcia2b9b3252012-10-23 15:57:25 -03002103 radio->videodev = wl1273_viddev_template;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002104
2105 radio->videodev.v4l2_dev = &radio->v4l2dev;
2106
2107 v4l2_ctrl_handler_init(&radio->ctrl_handler, 6);
2108
2109 /* add in ascending ID order */
2110 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2111 V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1,
2112 WL1273_DEFAULT_VOLUME);
2113
2114 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2115 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
2116
2117 v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops,
2118 V4L2_CID_TUNE_PREEMPHASIS,
2119 V4L2_PREEMPHASIS_75_uS, 0x03,
2120 V4L2_PREEMPHASIS_50_uS);
2121
2122 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2123 V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118);
2124
2125 ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2126 V4L2_CID_TUNE_ANTENNA_CAPACITOR,
2127 0, 255, 1, 255);
2128 if (ctrl)
Hans Verkuil88365102011-08-26 07:35:14 -03002129 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002130
2131 if (radio->ctrl_handler.error) {
2132 r = radio->ctrl_handler.error;
2133 dev_err(&pdev->dev, "Ctrl handler error: %d\n", r);
2134 goto handler_init_err;
2135 }
2136
2137 video_set_drvdata(&radio->videodev, radio);
2138 platform_set_drvdata(pdev, radio);
2139
2140 /* register video device */
2141 r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
2142 if (r) {
2143 dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
2144 ": Could not register video device\n");
2145 goto handler_init_err;
2146 }
2147
2148 return 0;
2149
2150handler_init_err:
2151 v4l2_ctrl_handler_free(&radio->ctrl_handler);
2152 v4l2_device_unregister(&radio->v4l2dev);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002153write_buf_err:
2154 free_irq(radio->core->client->irq, radio);
2155err_request_irq:
2156 radio->core->pdata->free_resources();
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002157pdata_err:
2158 return r;
2159}
2160
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002161static struct platform_driver wl1273_fm_radio_driver = {
2162 .probe = wl1273_fm_radio_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002163 .remove = wl1273_fm_radio_remove,
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002164 .driver = {
2165 .name = "wl1273_fm_radio",
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002166 },
2167};
2168
Axel Lin1d6629b2012-01-10 03:21:49 -03002169module_platform_driver(wl1273_fm_radio_driver);
Matti Aaltonen87d1a502010-12-10 11:41:34 -03002170
2171MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
2172MODULE_DESCRIPTION(DRIVER_DESC);
2173MODULE_LICENSE("GPL");
Axel Lin1d6629b2012-01-10 03:21:49 -03002174MODULE_ALIAS("platform:wl1273_fm_radio");