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