2 * Driver for the Texas Instruments WL1273 FM radio.
4 * Copyright (C) 2011 Nokia Corporation
5 * Author: Matti J. Aaltonen <matti.j.aaltonen@nokia.com>
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.
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.
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>
22 #include <linux/module.h>
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-ioctl.h>
28 #define DRIVER_DESC "Wl1273 FM Radio"
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)
35 #define WL1273_PUPD_SET_OFF 0x00
36 #define WL1273_PUPD_SET_ON 0x01
37 #define WL1273_PUPD_SET_RETENTION 0x10
39 #define WL1273_FREQ(x) (x * 10000 / 625)
40 #define WL1273_INV_FREQ(x) (x * 625 / 10000)
43 * static int radio_nr - The number of the radio device
48 module_param(radio_nr
, int, 0);
49 MODULE_PARM_DESC(radio_nr
, "The number of the radio device. Default = 0");
51 struct wl1273_device
{
55 unsigned int preemphasis
;
57 unsigned int tx_power
;
58 unsigned int rx_frequency
;
59 unsigned int tx_frequency
;
60 unsigned int rangelow
;
61 unsigned int rangehigh
;
68 wait_queue_head_t read_queue
;
69 struct mutex lock
; /* for serializing fm radio operations */
70 struct completion busy
;
72 unsigned char *buffer
;
73 unsigned int buf_size
;
74 unsigned int rd_index
;
75 unsigned int wr_index
;
77 /* Selected interrupts */
81 struct v4l2_ctrl_handler ctrl_handler
;
82 struct v4l2_device v4l2dev
;
83 struct video_device videodev
;
85 struct wl1273_core
*core
;
88 unsigned int rds_users
;
91 #define WL1273_IRQ_MASK (WL1273_FR_EVENT | \
95 * static unsigned int rds_buf - the number of RDS buffer blocks used.
97 * The default number is 100.
99 static unsigned int rds_buf
= 100;
100 module_param(rds_buf
, uint
, 0);
101 MODULE_PARM_DESC(rds_buf
, "Number of RDS buffer entries. Default = 100");
103 static int wl1273_fm_write_fw(struct wl1273_core
*core
,
106 struct i2c_client
*client
= core
->client
;
110 msg
.addr
= client
->addr
;
113 for (i
= 0; i
<= len
; i
++) {
118 dev_dbg(&client
->dev
, "%s:len[%d]: %d\n", __func__
, i
, msg
.len
);
120 r
= i2c_transfer(client
->adapter
, &msg
, 1);
121 if (r
< 0 && i
< len
+ 1)
125 dev_dbg(&client
->dev
, "%s: i: %d\n", __func__
, i
);
126 dev_dbg(&client
->dev
, "%s: len + 1: %d\n", __func__
, len
+ 1);
128 /* Last transfer always fails. */
129 if (i
== len
|| r
== 1)
135 #define WL1273_FIFO_HAS_DATA(status) (1 << 5 & status)
136 #define WL1273_RDS_CORRECTABLE_ERROR (1 << 3)
137 #define WL1273_RDS_UNCORRECTABLE_ERROR (1 << 4)
139 static int wl1273_fm_rds(struct wl1273_device
*radio
)
141 struct wl1273_core
*core
= radio
->core
;
142 struct i2c_client
*client
= core
->client
;
144 u8 b0
= WL1273_RDS_DATA_GET
, status
;
145 struct v4l2_rds_data rds
= { 0, 0, 0 };
146 struct i2c_msg msg
[] = {
148 .addr
= client
->addr
,
154 .addr
= client
->addr
,
162 if (core
->mode
!= WL1273_MODE_RX
)
165 r
= core
->read(core
, WL1273_RDS_SYNC_GET
, &val
);
169 if ((val
& 0x01) == 0) {
170 /* RDS decoder not synchronized */
174 /* copy all four RDS blocks to internal buffer */
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",
185 if (!WL1273_FIFO_HAS_DATA(status
))
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;
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
;
201 /* copy RDS block to internal buffer */
202 memcpy(&radio
->buffer
[radio
->wr_index
], &rds
, RDS_BLOCK_SIZE
);
203 radio
->wr_index
+= 3;
205 /* wrap write pointer */
206 if (radio
->wr_index
>= radio
->buf_size
)
209 /* check for overflow & start over */
210 if (radio
->wr_index
== radio
->rd_index
) {
211 dev_dbg(radio
->dev
, "RDS OVERFLOW");
217 } while (WL1273_FIFO_HAS_DATA(status
));
219 /* wake up read queue */
220 if (radio
->wr_index
!= radio
->rd_index
)
221 wake_up_interruptible(&radio
->read_queue
);
226 static irqreturn_t
wl1273_fm_irq_thread_handler(int irq
, void *dev_id
)
228 struct wl1273_device
*radio
= dev_id
;
229 struct wl1273_core
*core
= radio
->core
;
233 r
= core
->read(core
, WL1273_FLAG_GET
, &flags
);
237 if (flags
& WL1273_BL_EVENT
) {
238 radio
->irq_received
= flags
;
239 dev_dbg(radio
->dev
, "IRQ: BL\n");
242 if (flags
& WL1273_RDS_EVENT
) {
245 wl1273_fm_rds(radio
);
248 if (flags
& WL1273_BBLK_EVENT
)
249 dev_dbg(radio
->dev
, "IRQ: BBLK\n");
251 if (flags
& WL1273_LSYNC_EVENT
)
252 dev_dbg(radio
->dev
, "IRQ: LSYNC\n");
254 if (flags
& WL1273_LEV_EVENT
) {
257 r
= core
->read(core
, WL1273_RSSI_LVL_GET
, &level
);
262 dev_dbg(radio
->dev
, "IRQ: LEV: 0x%x04\n", level
);
265 if (flags
& WL1273_IFFR_EVENT
)
266 dev_dbg(radio
->dev
, "IRQ: IFFR\n");
268 if (flags
& WL1273_PI_EVENT
)
269 dev_dbg(radio
->dev
, "IRQ: PI\n");
271 if (flags
& WL1273_PD_EVENT
)
272 dev_dbg(radio
->dev
, "IRQ: PD\n");
274 if (flags
& WL1273_STIC_EVENT
)
275 dev_dbg(radio
->dev
, "IRQ: STIC\n");
277 if (flags
& WL1273_MAL_EVENT
)
278 dev_dbg(radio
->dev
, "IRQ: MAL\n");
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");
286 if (flags
& WL1273_SCAN_OVER_EVENT
)
287 dev_dbg(radio
->dev
, "IRQ: SCAN_OVER\n");
289 if (flags
& WL1273_ERROR_EVENT
)
290 dev_dbg(radio
->dev
, "IRQ: ERROR\n");
292 if (flags
& WL1273_FR_EVENT
) {
295 dev_dbg(radio
->dev
, "IRQ: FR:\n");
297 if (core
->mode
== WL1273_MODE_RX
) {
298 r
= core
->write(core
, WL1273_TUNER_MODE_SET
,
299 TUNER_MODE_STOP_SEARCH
);
302 "%s: TUNER_MODE_SET fails: %d\n",
307 r
= core
->read(core
, WL1273_FREQ_SET
, &freq
);
311 if (radio
->band
== WL1273_BAND_JAPAN
)
312 radio
->rx_frequency
= WL1273_BAND_JAPAN_LOW
+
315 radio
->rx_frequency
= WL1273_BAND_OTHER_LOW
+
318 * The driver works better with this msleep,
319 * the documentation doesn't mention it.
321 usleep_range(10000, 15000);
323 dev_dbg(radio
->dev
, "%dkHz\n", radio
->rx_frequency
);
326 r
= core
->read(core
, WL1273_CHANL_SET
, &freq
);
330 dev_dbg(radio
->dev
, "%dkHz\n", freq
);
332 dev_dbg(radio
->dev
, "%s: NOT BUSY\n", __func__
);
336 core
->write(core
, WL1273_INT_MASK_SET
, radio
->irq_flags
);
337 complete(&radio
->busy
);
342 static int wl1273_fm_set_tx_freq(struct wl1273_device
*radio
, unsigned int freq
)
344 struct wl1273_core
*core
= radio
->core
;
348 if (freq
< WL1273_BAND_TX_LOW
) {
350 "Frequency out of range: %d < %d\n", freq
,
355 if (freq
> WL1273_BAND_TX_HIGH
) {
357 "Frequency out of range: %d > %d\n", freq
,
358 WL1273_BAND_TX_HIGH
);
363 * The driver works better with this sleep,
364 * the documentation doesn't mention it.
366 usleep_range(5000, 10000);
368 dev_dbg(radio
->dev
, "%s: freq: %d kHz\n", __func__
, freq
);
370 /* Set the current tx channel */
371 r
= core
->write(core
, WL1273_CHANL_SET
, freq
/ 10);
375 reinit_completion(&radio
->busy
);
377 /* wait for the FR IRQ */
378 t
= wait_for_completion_timeout(&radio
->busy
, msecs_to_jiffies(2000));
382 dev_dbg(radio
->dev
, "WL1273_CHANL_SET: %lu\n", t
);
384 /* Enable the output power */
385 r
= core
->write(core
, WL1273_POWER_ENB_SET
, 1);
389 reinit_completion(&radio
->busy
);
391 /* wait for the POWER_ENB IRQ */
392 t
= wait_for_completion_timeout(&radio
->busy
, msecs_to_jiffies(1000));
396 radio
->tx_frequency
= freq
;
397 dev_dbg(radio
->dev
, "WL1273_POWER_ENB_SET: %lu\n", t
);
402 static int wl1273_fm_set_rx_freq(struct wl1273_device
*radio
, unsigned int freq
)
404 struct wl1273_core
*core
= radio
->core
;
408 if (freq
< radio
->rangelow
) {
410 "Frequency out of range: %d < %d\n", freq
,
416 if (freq
> radio
->rangehigh
) {
418 "Frequency out of range: %d > %d\n", freq
,
424 dev_dbg(radio
->dev
, "%s: %dkHz\n", __func__
, freq
);
426 core
->write(core
, WL1273_INT_MASK_SET
, radio
->irq_flags
);
428 if (radio
->band
== WL1273_BAND_JAPAN
)
429 f
= (freq
- WL1273_BAND_JAPAN_LOW
) / 50;
431 f
= (freq
- WL1273_BAND_OTHER_LOW
) / 50;
433 r
= core
->write(core
, WL1273_FREQ_SET
, f
);
435 dev_err(radio
->dev
, "FREQ_SET fails\n");
439 r
= core
->write(core
, WL1273_TUNER_MODE_SET
, TUNER_MODE_PRESET
);
441 dev_err(radio
->dev
, "TUNER_MODE_SET fails\n");
445 reinit_completion(&radio
->busy
);
447 t
= wait_for_completion_timeout(&radio
->busy
, msecs_to_jiffies(2000));
449 dev_err(radio
->dev
, "%s: TIMEOUT\n", __func__
);
455 radio
->rx_frequency
= freq
;
461 static int wl1273_fm_get_freq(struct wl1273_device
*radio
)
463 struct wl1273_core
*core
= radio
->core
;
468 if (core
->mode
== WL1273_MODE_RX
) {
469 r
= core
->read(core
, WL1273_FREQ_SET
, &f
);
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
;
477 freq
= WL1273_BAND_OTHER_LOW
+ 50 * f
;
479 r
= core
->read(core
, WL1273_CHANL_SET
, &f
);
490 * wl1273_fm_upload_firmware_patch() - Upload the firmware.
491 * @radio: A pointer to the device struct.
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.
497 static int wl1273_fm_upload_firmware_patch(struct wl1273_device
*radio
)
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
;
507 dev_dbg(dev
, "%s:\n", __func__
);
510 * Uploading the firmware patch is not always necessary,
511 * so we only print an info message.
513 if (request_firmware(&fw_p
, fw_name
, dev
)) {
514 dev_info(dev
, "%s - %s not found\n", __func__
, fw_name
);
519 ptr
= (__u8
*) fw_p
->data
;
521 dev_dbg(dev
, "%s: packets: %d\n", __func__
, packet_num
);
523 r
= wl1273_fm_write_fw(core
, ptr
+ 1, packet_num
);
525 dev_err(dev
, "FW upload error: %d\n", r
);
529 /* ignore possible error here */
530 core
->write(core
, WL1273_RESET
, 0);
532 dev_dbg(dev
, "%s - download OK, r: %d\n", __func__
, r
);
534 release_firmware(fw_p
);
538 static int wl1273_fm_stop(struct wl1273_device
*radio
)
540 struct wl1273_core
*core
= radio
->core
;
542 if (core
->mode
== WL1273_MODE_RX
) {
543 int r
= core
->write(core
, WL1273_POWER_SET
,
544 WL1273_POWER_SET_OFF
);
546 dev_err(radio
->dev
, "%s: POWER_SET fails: %d\n",
548 } else if (core
->mode
== WL1273_MODE_TX
) {
549 int r
= core
->write(core
, WL1273_PUPD_SET
,
550 WL1273_PUPD_SET_OFF
);
553 "%s: PUPD_SET fails: %d\n", __func__
, r
);
556 if (core
->pdata
->disable
) {
557 core
->pdata
->disable();
558 dev_dbg(radio
->dev
, "Back to reset\n");
564 static int wl1273_fm_start(struct wl1273_device
*radio
, int new_mode
)
566 struct wl1273_core
*core
= radio
->core
;
567 struct wl1273_fm_platform_data
*pdata
= core
->pdata
;
568 struct device
*dev
= radio
->dev
;
571 if (pdata
->enable
&& core
->mode
== WL1273_MODE_OFF
) {
572 dev_dbg(radio
->dev
, "Out of reset\n");
578 if (new_mode
== WL1273_MODE_RX
) {
579 u16 val
= WL1273_POWER_SET_FM
;
582 val
|= WL1273_POWER_SET_RDS
;
584 /* If this fails try again */
585 r
= core
->write(core
, WL1273_POWER_SET
, val
);
589 r
= core
->write(core
, WL1273_POWER_SET
, val
);
591 dev_err(dev
, "%s: POWER_SET fails\n", __func__
);
596 /* rds buffer configuration */
600 } else if (new_mode
== WL1273_MODE_TX
) {
601 /* If this fails try again once */
602 r
= core
->write(core
, WL1273_PUPD_SET
, WL1273_PUPD_SET_ON
);
605 r
= core
->write(core
, WL1273_PUPD_SET
,
608 dev_err(dev
, "%s: PUPD_SET fails\n", __func__
);
614 r
= core
->write(core
, WL1273_RDS_DATA_ENB
, 1);
616 dev_err(dev
, "%s: RDS_DATA_ENB ON fails\n",
621 r
= core
->write(core
, WL1273_RDS_DATA_ENB
, 0);
623 dev_err(dev
, "%s: RDS_DATA_ENB OFF fails\n",
629 dev_warn(dev
, "%s: Illegal mode.\n", __func__
);
632 if (core
->mode
== WL1273_MODE_OFF
) {
633 r
= wl1273_fm_upload_firmware_patch(radio
);
635 dev_warn(dev
, "Firmware upload failed.\n");
638 * Sometimes the chip is in a wrong power state at this point.
639 * So we set the power once again.
641 if (new_mode
== WL1273_MODE_RX
) {
642 u16 val
= WL1273_POWER_SET_FM
;
645 val
|= WL1273_POWER_SET_RDS
;
647 r
= core
->write(core
, WL1273_POWER_SET
, val
);
649 dev_err(dev
, "%s: POWER_SET fails\n", __func__
);
652 } else if (new_mode
== WL1273_MODE_TX
) {
653 r
= core
->write(core
, WL1273_PUPD_SET
,
656 dev_err(dev
, "%s: PUPD_SET fails\n", __func__
);
667 dev_dbg(dev
, "%s: return: %d\n", __func__
, r
);
671 static int wl1273_fm_suspend(struct wl1273_device
*radio
)
673 struct wl1273_core
*core
= radio
->core
;
676 /* Cannot go from OFF to SUSPENDED */
677 if (core
->mode
== WL1273_MODE_RX
)
678 r
= core
->write(core
, WL1273_POWER_SET
,
679 WL1273_POWER_SET_RETENTION
);
680 else if (core
->mode
== WL1273_MODE_TX
)
681 r
= core
->write(core
, WL1273_PUPD_SET
,
682 WL1273_PUPD_SET_RETENTION
);
687 dev_err(radio
->dev
, "%s: POWER_SET fails: %d\n", __func__
, r
);
695 static int wl1273_fm_set_mode(struct wl1273_device
*radio
, int mode
)
697 struct wl1273_core
*core
= radio
->core
;
698 struct device
*dev
= radio
->dev
;
702 dev_dbg(dev
, "%s\n", __func__
);
703 dev_dbg(dev
, "Forbidden modes: 0x%02x\n", radio
->forbidden
);
705 old_mode
= core
->mode
;
706 if (mode
& radio
->forbidden
) {
714 r
= wl1273_fm_start(radio
, mode
);
716 dev_err(dev
, "%s: Cannot start.\n", __func__
);
717 wl1273_fm_stop(radio
);
722 r
= core
->write(core
, WL1273_INT_MASK_SET
, radio
->irq_flags
);
724 dev_err(dev
, "INT_MASK_SET fails.\n");
728 /* remember previous settings */
729 if (mode
== WL1273_MODE_RX
) {
730 r
= wl1273_fm_set_rx_freq(radio
, radio
->rx_frequency
);
732 dev_err(dev
, "set freq fails: %d.\n", r
);
736 r
= core
->set_volume(core
, core
->volume
);
738 dev_err(dev
, "set volume fails: %d.\n", r
);
742 dev_dbg(dev
, "%s: Set vol: %d.\n", __func__
,
745 r
= wl1273_fm_set_tx_freq(radio
, radio
->tx_frequency
);
747 dev_err(dev
, "set freq fails: %d.\n", r
);
752 dev_dbg(radio
->dev
, "%s: Set audio mode.\n", __func__
);
754 r
= core
->set_audio(core
, core
->audio_mode
);
756 dev_err(dev
, "Cannot set audio mode.\n");
759 case WL1273_MODE_OFF
:
760 r
= wl1273_fm_stop(radio
);
762 dev_err(dev
, "%s: Off fails: %d\n", __func__
, r
);
764 core
->mode
= WL1273_MODE_OFF
;
768 case WL1273_MODE_SUSPENDED
:
769 r
= wl1273_fm_suspend(radio
);
771 dev_err(dev
, "%s: Suspend fails: %d\n", __func__
, r
);
773 core
->mode
= WL1273_MODE_SUSPENDED
;
778 dev_err(dev
, "%s: Unknown mode: %d\n", __func__
, mode
);
784 core
->mode
= old_mode
;
789 static int wl1273_fm_set_seek(struct wl1273_device
*radio
,
790 unsigned int wrap_around
,
791 unsigned int seek_upward
,
794 struct wl1273_core
*core
= radio
->core
;
796 unsigned int dir
= (seek_upward
== 0) ? 0 : 1;
799 f
= radio
->rx_frequency
;
800 dev_dbg(radio
->dev
, "rx_frequency: %d\n", f
);
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
);
814 if (level
< SCHAR_MIN
|| level
> SCHAR_MAX
)
817 reinit_completion(&radio
->busy
);
818 dev_dbg(radio
->dev
, "%s: BUSY\n", __func__
);
820 r
= core
->write(core
, WL1273_INT_MASK_SET
, radio
->irq_flags
);
824 dev_dbg(radio
->dev
, "%s\n", __func__
);
826 r
= core
->write(core
, WL1273_SEARCH_LVL_SET
, level
);
830 r
= core
->write(core
, WL1273_SEARCH_DIR_SET
, dir
);
834 r
= core
->write(core
, WL1273_TUNER_MODE_SET
, TUNER_MODE_AUTO_SEEK
);
838 /* wait for the FR IRQ */
839 wait_for_completion_timeout(&radio
->busy
, msecs_to_jiffies(1000));
840 if (!(radio
->irq_received
& WL1273_BL_EVENT
)) {
845 radio
->irq_received
&= ~WL1273_BL_EVENT
;
851 dev_dbg(radio
->dev
, "Wrap around in HW seek.\n");
856 f
= radio
->rangehigh
;
858 r
= wl1273_fm_set_rx_freq(radio
, f
);
862 reinit_completion(&radio
->busy
);
863 dev_dbg(radio
->dev
, "%s: BUSY\n", __func__
);
865 r
= core
->write(core
, WL1273_TUNER_MODE_SET
, TUNER_MODE_AUTO_SEEK
);
869 /* wait for the FR IRQ */
870 if (!wait_for_completion_timeout(&radio
->busy
, msecs_to_jiffies(1000)))
873 dev_dbg(radio
->dev
, "%s: Err: %d\n", __func__
, r
);
878 * wl1273_fm_get_tx_ctune() - Get the TX tuning capacitor value.
879 * @radio: A pointer to the device struct.
881 static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device
*radio
)
883 struct wl1273_core
*core
= radio
->core
;
884 struct device
*dev
= radio
->dev
;
888 if (core
->mode
== WL1273_MODE_OFF
||
889 core
->mode
== WL1273_MODE_SUSPENDED
)
892 r
= core
->read(core
, WL1273_READ_FMANT_TUNE_VALUE
, &val
);
894 dev_err(dev
, "%s: read error: %d\n", __func__
, r
);
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.
907 * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
908 * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
910 static int wl1273_fm_set_preemphasis(struct wl1273_device
*radio
,
911 unsigned int preemphasis
)
913 struct wl1273_core
*core
= radio
->core
;
917 if (core
->mode
== WL1273_MODE_OFF
||
918 core
->mode
== WL1273_MODE_SUSPENDED
)
921 mutex_lock(&core
->lock
);
923 switch (preemphasis
) {
924 case V4L2_PREEMPHASIS_DISABLED
:
927 case V4L2_PREEMPHASIS_50_uS
:
930 case V4L2_PREEMPHASIS_75_uS
:
938 r
= core
->write(core
, WL1273_PREMPH_SET
, em
);
942 radio
->preemphasis
= preemphasis
;
945 mutex_unlock(&core
->lock
);
949 static int wl1273_fm_rds_on(struct wl1273_device
*radio
)
951 struct wl1273_core
*core
= radio
->core
;
954 dev_dbg(radio
->dev
, "%s\n", __func__
);
958 r
= core
->write(core
, WL1273_POWER_SET
,
959 WL1273_POWER_SET_FM
| WL1273_POWER_SET_RDS
);
963 r
= wl1273_fm_set_rx_freq(radio
, radio
->rx_frequency
);
965 dev_err(radio
->dev
, "set freq fails: %d.\n", r
);
970 static int wl1273_fm_rds_off(struct wl1273_device
*radio
)
972 struct wl1273_core
*core
= radio
->core
;
978 radio
->irq_flags
&= ~WL1273_RDS_EVENT
;
980 r
= core
->write(core
, WL1273_INT_MASK_SET
, radio
->irq_flags
);
984 /* Service pending read */
985 wake_up_interruptible(&radio
->read_queue
);
987 dev_dbg(radio
->dev
, "%s\n", __func__
);
989 r
= core
->write(core
, WL1273_POWER_SET
, WL1273_POWER_SET_FM
);
993 r
= wl1273_fm_set_rx_freq(radio
, radio
->rx_frequency
);
995 dev_err(radio
->dev
, "set freq fails: %d.\n", r
);
997 dev_dbg(radio
->dev
, "%s: exiting...\n", __func__
);
1002 static int wl1273_fm_set_rds(struct wl1273_device
*radio
, unsigned int new_mode
)
1005 struct wl1273_core
*core
= radio
->core
;
1007 if (core
->mode
== WL1273_MODE_OFF
||
1008 core
->mode
== WL1273_MODE_SUSPENDED
)
1011 if (new_mode
== WL1273_RDS_RESET
) {
1012 r
= core
->write(core
, WL1273_RDS_CNTRL_SET
, 1);
1016 if (core
->mode
== WL1273_MODE_TX
&& new_mode
== WL1273_RDS_OFF
) {
1017 r
= core
->write(core
, WL1273_RDS_DATA_ENB
, 0);
1018 } else if (core
->mode
== WL1273_MODE_TX
&& new_mode
== WL1273_RDS_ON
) {
1019 r
= core
->write(core
, WL1273_RDS_DATA_ENB
, 1);
1020 } 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
);
1025 dev_err(radio
->dev
, "%s: Unknown mode: %d\n",
1026 __func__
, new_mode
);
1031 radio
->rds_on
= (new_mode
== WL1273_RDS_ON
) ? true : false;
1036 static ssize_t
wl1273_fm_fops_write(struct file
*file
, const char __user
*buf
,
1037 size_t count
, loff_t
*ppos
)
1039 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1040 struct wl1273_core
*core
= radio
->core
;
1044 dev_dbg(radio
->dev
, "%s\n", __func__
);
1046 if (core
->mode
!= WL1273_MODE_TX
)
1049 if (radio
->rds_users
== 0) {
1050 dev_warn(radio
->dev
, "%s: RDS not on.\n", __func__
);
1054 if (mutex_lock_interruptible(&core
->lock
))
1057 * Multiple processes can open the device, but only
1058 * one gets to write to it.
1060 if (radio
->owner
&& radio
->owner
!= file
) {
1064 radio
->owner
= file
;
1072 core
->write(core
, WL1273_RDS_CONFIG_DATA_SET
, val
);
1074 if (copy_from_user(radio
->write_buf
+ 1, buf
, val
)) {
1079 dev_dbg(radio
->dev
, "Count: %d\n", val
);
1080 dev_dbg(radio
->dev
, "From user: \"%s\"\n", radio
->write_buf
);
1082 radio
->write_buf
[0] = WL1273_RDS_DATA_SET
;
1083 core
->write_data(core
, radio
->write_buf
, val
+ 1);
1087 mutex_unlock(&core
->lock
);
1092 static __poll_t
wl1273_fm_fops_poll(struct file
*file
,
1093 struct poll_table_struct
*pts
)
1095 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1096 struct wl1273_core
*core
= radio
->core
;
1098 if (radio
->owner
&& radio
->owner
!= file
)
1101 radio
->owner
= file
;
1103 if (core
->mode
== WL1273_MODE_RX
) {
1104 poll_wait(file
, &radio
->read_queue
, pts
);
1106 if (radio
->rd_index
!= radio
->wr_index
)
1107 return EPOLLIN
| EPOLLRDNORM
;
1109 } else if (core
->mode
== WL1273_MODE_TX
) {
1110 return EPOLLOUT
| EPOLLWRNORM
;
1116 static int wl1273_fm_fops_open(struct file
*file
)
1118 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1119 struct wl1273_core
*core
= radio
->core
;
1122 dev_dbg(radio
->dev
, "%s\n", __func__
);
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
);
1128 if (mutex_lock_interruptible(&core
->lock
))
1131 radio
->irq_flags
|= WL1273_RDS_EVENT
;
1133 r
= core
->write(core
, WL1273_INT_MASK_SET
,
1136 mutex_unlock(&core
->lock
);
1142 mutex_unlock(&core
->lock
);
1148 static int wl1273_fm_fops_release(struct file
*file
)
1150 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1151 struct wl1273_core
*core
= radio
->core
;
1154 dev_dbg(radio
->dev
, "%s\n", __func__
);
1156 if (radio
->rds_users
> 0) {
1158 if (radio
->rds_users
== 0) {
1159 if (mutex_lock_interruptible(&core
->lock
))
1162 radio
->irq_flags
&= ~WL1273_RDS_EVENT
;
1164 if (core
->mode
== WL1273_MODE_RX
) {
1165 r
= core
->write(core
,
1166 WL1273_INT_MASK_SET
,
1169 mutex_unlock(&core
->lock
);
1173 mutex_unlock(&core
->lock
);
1177 if (file
== radio
->owner
)
1178 radio
->owner
= NULL
;
1183 static ssize_t
wl1273_fm_fops_read(struct file
*file
, char __user
*buf
,
1184 size_t count
, loff_t
*ppos
)
1187 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1188 struct wl1273_core
*core
= radio
->core
;
1189 unsigned int block_count
= 0;
1192 dev_dbg(radio
->dev
, "%s\n", __func__
);
1194 if (core
->mode
!= WL1273_MODE_RX
)
1197 if (radio
->rds_users
== 0) {
1198 dev_warn(radio
->dev
, "%s: RDS not on.\n", __func__
);
1202 if (mutex_lock_interruptible(&core
->lock
))
1206 * Multiple processes can open the device, but only
1207 * one at a time gets read access.
1209 if (radio
->owner
&& radio
->owner
!= file
) {
1213 radio
->owner
= file
;
1215 r
= core
->read(core
, WL1273_RDS_SYNC_GET
, &val
);
1217 dev_err(radio
->dev
, "%s: Get RDS_SYNC fails.\n", __func__
);
1219 } else if (val
== 0) {
1220 dev_info(radio
->dev
, "RDS_SYNC: Not synchronized\n");
1225 /* block if no new data available */
1226 while (radio
->wr_index
== radio
->rd_index
) {
1227 if (file
->f_flags
& O_NONBLOCK
) {
1232 dev_dbg(radio
->dev
, "%s: Wait for RDS data.\n", __func__
);
1233 if (wait_event_interruptible(radio
->read_queue
,
1235 radio
->rd_index
) < 0) {
1241 /* calculate block count from byte count */
1242 count
/= RDS_BLOCK_SIZE
;
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
)
1249 /* always transfer complete RDS blocks */
1250 if (copy_to_user(buf
, &radio
->buffer
[radio
->rd_index
],
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;
1259 /* increment counters */
1261 buf
+= RDS_BLOCK_SIZE
;
1262 r
+= RDS_BLOCK_SIZE
;
1266 dev_dbg(radio
->dev
, "%s: exit\n", __func__
);
1267 mutex_unlock(&core
->lock
);
1272 static 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
,
1277 .unlocked_ioctl
= video_ioctl2
,
1278 .open
= wl1273_fm_fops_open
,
1279 .release
= wl1273_fm_fops_release
,
1282 static int wl1273_fm_vidioc_querycap(struct file
*file
, void *priv
,
1283 struct v4l2_capability
*capability
)
1285 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1287 dev_dbg(radio
->dev
, "%s\n", __func__
);
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
));
1296 capability
->device_caps
= V4L2_CAP_HW_FREQ_SEEK
|
1297 V4L2_CAP_TUNER
| V4L2_CAP_RADIO
| V4L2_CAP_AUDIO
|
1298 V4L2_CAP_RDS_CAPTURE
| V4L2_CAP_MODULATOR
|
1299 V4L2_CAP_RDS_OUTPUT
;
1300 capability
->capabilities
= capability
->device_caps
|
1301 V4L2_CAP_DEVICE_CAPS
;
1306 static int wl1273_fm_vidioc_g_input(struct file
*file
, void *priv
,
1309 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1311 dev_dbg(radio
->dev
, "%s\n", __func__
);
1318 static int wl1273_fm_vidioc_s_input(struct file
*file
, void *priv
,
1321 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1323 dev_dbg(radio
->dev
, "%s\n", __func__
);
1332 * wl1273_fm_set_tx_power() - Set the transmission power value.
1333 * @radio: A pointer to the device struct.
1334 * @power: The new power value.
1336 static int wl1273_fm_set_tx_power(struct wl1273_device
*radio
, u16 power
)
1338 struct wl1273_core
*core
= radio
->core
;
1341 if (core
->mode
== WL1273_MODE_OFF
||
1342 core
->mode
== WL1273_MODE_SUSPENDED
)
1345 mutex_lock(&core
->lock
);
1347 /* Convert the dBuV value to chip presentation */
1348 r
= core
->write(core
, WL1273_POWER_LEV_SET
, 122 - power
);
1352 radio
->tx_power
= power
;
1355 mutex_unlock(&core
->lock
);
1359 #define WL1273_SPACING_50kHz 1
1360 #define WL1273_SPACING_100kHz 2
1361 #define WL1273_SPACING_200kHz 4
1363 static int wl1273_fm_tx_set_spacing(struct wl1273_device
*radio
,
1364 unsigned int spacing
)
1366 struct wl1273_core
*core
= radio
->core
;
1370 r
= core
->write(core
, WL1273_SCAN_SPACING_SET
,
1371 WL1273_SPACING_100kHz
);
1372 radio
->spacing
= 100;
1373 } else if (spacing
- 50000 < 25000) {
1374 r
= core
->write(core
, WL1273_SCAN_SPACING_SET
,
1375 WL1273_SPACING_50kHz
);
1376 radio
->spacing
= 50;
1377 } else if (spacing
- 100000 < 50000) {
1378 r
= core
->write(core
, WL1273_SCAN_SPACING_SET
,
1379 WL1273_SPACING_100kHz
);
1380 radio
->spacing
= 100;
1382 r
= core
->write(core
, WL1273_SCAN_SPACING_SET
,
1383 WL1273_SPACING_200kHz
);
1384 radio
->spacing
= 200;
1390 static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
1392 struct wl1273_device
*radio
= ctrl
->priv
;
1393 struct wl1273_core
*core
= radio
->core
;
1395 dev_dbg(radio
->dev
, "%s\n", __func__
);
1397 if (mutex_lock_interruptible(&core
->lock
))
1401 case V4L2_CID_TUNE_ANTENNA_CAPACITOR
:
1402 ctrl
->val
= wl1273_fm_get_tx_ctune(radio
);
1406 dev_warn(radio
->dev
, "%s: Unknown IOCTL: %d\n",
1407 __func__
, ctrl
->id
);
1411 mutex_unlock(&core
->lock
);
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)
1422 static inline struct wl1273_device
*to_radio(struct v4l2_ctrl
*ctrl
)
1424 return container_of(ctrl
->handler
, struct wl1273_device
, ctrl_handler
);
1427 static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl
*ctrl
)
1429 struct wl1273_device
*radio
= to_radio(ctrl
);
1430 struct wl1273_core
*core
= radio
->core
;
1433 dev_dbg(radio
->dev
, "%s\n", __func__
);
1436 case V4L2_CID_AUDIO_MUTE
:
1437 if (mutex_lock_interruptible(&core
->lock
))
1440 if (core
->mode
== WL1273_MODE_RX
&& ctrl
->val
)
1441 r
= core
->write(core
,
1442 WL1273_MUTE_STATUS_SET
,
1443 WL1273_MUTE_HARD_LEFT
|
1444 WL1273_MUTE_HARD_RIGHT
);
1445 else if (core
->mode
== WL1273_MODE_RX
)
1446 r
= core
->write(core
,
1447 WL1273_MUTE_STATUS_SET
, 0x0);
1448 else if (core
->mode
== WL1273_MODE_TX
&& ctrl
->val
)
1449 r
= core
->write(core
, WL1273_MUTE
, 1);
1450 else if (core
->mode
== WL1273_MODE_TX
)
1451 r
= core
->write(core
, WL1273_MUTE
, 0);
1453 mutex_unlock(&core
->lock
);
1456 case V4L2_CID_AUDIO_VOLUME
:
1458 r
= wl1273_fm_set_mode(radio
, WL1273_MODE_OFF
);
1460 r
= core
->set_volume(core
, core
->volume
);
1463 case V4L2_CID_TUNE_PREEMPHASIS
:
1464 r
= wl1273_fm_set_preemphasis(radio
, ctrl
->val
);
1467 case V4L2_CID_TUNE_POWER_LEVEL
:
1468 r
= wl1273_fm_set_tx_power(radio
, ctrl
->val
);
1472 dev_warn(radio
->dev
, "%s: Unknown IOCTL: %d\n",
1473 __func__
, ctrl
->id
);
1477 dev_dbg(radio
->dev
, "%s\n", __func__
);
1481 static int wl1273_fm_vidioc_g_audio(struct file
*file
, void *priv
,
1482 struct v4l2_audio
*audio
)
1484 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1486 dev_dbg(radio
->dev
, "%s\n", __func__
);
1488 if (audio
->index
> 1)
1491 strlcpy(audio
->name
, "Radio", sizeof(audio
->name
));
1492 audio
->capability
= V4L2_AUDCAP_STEREO
;
1497 static int wl1273_fm_vidioc_s_audio(struct file
*file
, void *priv
,
1498 const struct v4l2_audio
*audio
)
1500 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1502 dev_dbg(radio
->dev
, "%s\n", __func__
);
1504 if (audio
->index
!= 0)
1510 #define WL1273_RDS_NOT_SYNCHRONIZED 0
1511 #define WL1273_RDS_SYNCHRONIZED 1
1513 static int wl1273_fm_vidioc_g_tuner(struct file
*file
, void *priv
,
1514 struct v4l2_tuner
*tuner
)
1516 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1517 struct wl1273_core
*core
= radio
->core
;
1521 dev_dbg(radio
->dev
, "%s\n", __func__
);
1523 if (tuner
->index
> 0)
1526 strlcpy(tuner
->name
, WL1273_FM_DRIVER_NAME
, sizeof(tuner
->name
));
1527 tuner
->type
= V4L2_TUNER_RADIO
;
1529 tuner
->rangelow
= WL1273_FREQ(WL1273_BAND_JAPAN_LOW
);
1530 tuner
->rangehigh
= WL1273_FREQ(WL1273_BAND_OTHER_HIGH
);
1532 tuner
->capability
= V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_RDS
|
1533 V4L2_TUNER_CAP_STEREO
| V4L2_TUNER_CAP_RDS_BLOCK_IO
|
1534 V4L2_TUNER_CAP_HWSEEK_BOUNDED
| V4L2_TUNER_CAP_HWSEEK_WRAP
;
1537 tuner
->audmode
= V4L2_TUNER_MODE_STEREO
;
1539 tuner
->audmode
= V4L2_TUNER_MODE_MONO
;
1541 if (core
->mode
!= WL1273_MODE_RX
)
1544 if (mutex_lock_interruptible(&core
->lock
))
1547 r
= core
->read(core
, WL1273_STEREO_GET
, &val
);
1552 tuner
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1554 tuner
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1556 r
= core
->read(core
, WL1273_RSSI_LVL_GET
, &val
);
1560 tuner
->signal
= (s16
) val
;
1561 dev_dbg(radio
->dev
, "Signal: %d\n", tuner
->signal
);
1565 r
= core
->read(core
, WL1273_RDS_SYNC_GET
, &val
);
1569 if (val
== WL1273_RDS_SYNCHRONIZED
)
1570 tuner
->rxsubchans
|= V4L2_TUNER_SUB_RDS
;
1572 mutex_unlock(&core
->lock
);
1577 static int wl1273_fm_vidioc_s_tuner(struct file
*file
, void *priv
,
1578 const struct v4l2_tuner
*tuner
)
1580 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1581 struct wl1273_core
*core
= radio
->core
;
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
);
1592 if (tuner
->index
> 0)
1595 if (mutex_lock_interruptible(&core
->lock
))
1598 r
= wl1273_fm_set_mode(radio
, WL1273_MODE_RX
);
1602 if (tuner
->rxsubchans
& V4L2_TUNER_SUB_RDS
)
1603 r
= wl1273_fm_set_rds(radio
, WL1273_RDS_ON
);
1605 r
= wl1273_fm_set_rds(radio
, WL1273_RDS_OFF
);
1608 dev_warn(radio
->dev
, "%s: RDS fails: %d\n", __func__
, r
);
1610 if (tuner
->audmode
== V4L2_TUNER_MODE_MONO
) {
1611 r
= core
->write(core
, WL1273_MOST_MODE_SET
, WL1273_RX_MONO
);
1613 dev_warn(radio
->dev
, "%s: MOST_MODE fails: %d\n",
1617 radio
->stereo
= false;
1618 } else if (tuner
->audmode
== V4L2_TUNER_MODE_STEREO
) {
1619 r
= core
->write(core
, WL1273_MOST_MODE_SET
, WL1273_RX_STEREO
);
1621 dev_warn(radio
->dev
, "%s: MOST_MODE fails: %d\n",
1625 radio
->stereo
= true;
1627 dev_err(radio
->dev
, "%s: tuner->audmode: %d\n",
1628 __func__
, tuner
->audmode
);
1634 mutex_unlock(&core
->lock
);
1639 static int wl1273_fm_vidioc_g_frequency(struct file
*file
, void *priv
,
1640 struct v4l2_frequency
*freq
)
1642 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1643 struct wl1273_core
*core
= radio
->core
;
1645 dev_dbg(radio
->dev
, "%s\n", __func__
);
1647 if (mutex_lock_interruptible(&core
->lock
))
1650 freq
->type
= V4L2_TUNER_RADIO
;
1651 freq
->frequency
= WL1273_FREQ(wl1273_fm_get_freq(radio
));
1653 mutex_unlock(&core
->lock
);
1658 static int wl1273_fm_vidioc_s_frequency(struct file
*file
, void *priv
,
1659 const struct v4l2_frequency
*freq
)
1661 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1662 struct wl1273_core
*core
= radio
->core
;
1665 dev_dbg(radio
->dev
, "%s: %d\n", __func__
, freq
->frequency
);
1667 if (freq
->type
!= V4L2_TUNER_RADIO
) {
1669 "freq->type != V4L2_TUNER_RADIO: %d\n", freq
->type
);
1673 if (mutex_lock_interruptible(&core
->lock
))
1676 if (core
->mode
== WL1273_MODE_RX
) {
1677 dev_dbg(radio
->dev
, "freq: %d\n", freq
->frequency
);
1679 r
= wl1273_fm_set_rx_freq(radio
,
1680 WL1273_INV_FREQ(freq
->frequency
));
1682 dev_warn(radio
->dev
, WL1273_FM_DRIVER_NAME
1683 ": set frequency failed with %d\n", r
);
1685 r
= wl1273_fm_set_tx_freq(radio
,
1686 WL1273_INV_FREQ(freq
->frequency
));
1688 dev_warn(radio
->dev
, WL1273_FM_DRIVER_NAME
1689 ": set frequency failed with %d\n", r
);
1692 mutex_unlock(&core
->lock
);
1694 dev_dbg(radio
->dev
, "wl1273_vidioc_s_frequency: DONE\n");
1698 #define WL1273_DEFAULT_SEEK_LEVEL 7
1700 static int wl1273_fm_vidioc_s_hw_freq_seek(struct file
*file
, void *priv
,
1701 const struct v4l2_hw_freq_seek
*seek
)
1703 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1704 struct wl1273_core
*core
= radio
->core
;
1707 dev_dbg(radio
->dev
, "%s\n", __func__
);
1709 if (seek
->tuner
!= 0 || seek
->type
!= V4L2_TUNER_RADIO
)
1712 if (file
->f_flags
& O_NONBLOCK
)
1713 return -EWOULDBLOCK
;
1715 if (mutex_lock_interruptible(&core
->lock
))
1718 r
= wl1273_fm_set_mode(radio
, WL1273_MODE_RX
);
1722 r
= wl1273_fm_tx_set_spacing(radio
, seek
->spacing
);
1724 dev_warn(radio
->dev
, "HW seek failed: %d\n", r
);
1726 r
= wl1273_fm_set_seek(radio
, seek
->wrap_around
, seek
->seek_upward
,
1727 WL1273_DEFAULT_SEEK_LEVEL
);
1729 dev_warn(radio
->dev
, "HW seek failed: %d\n", r
);
1732 mutex_unlock(&core
->lock
);
1736 static int wl1273_fm_vidioc_s_modulator(struct file
*file
, void *priv
,
1737 const struct v4l2_modulator
*modulator
)
1739 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1740 struct wl1273_core
*core
= radio
->core
;
1743 dev_dbg(radio
->dev
, "%s\n", __func__
);
1745 if (modulator
->index
> 0)
1748 if (mutex_lock_interruptible(&core
->lock
))
1751 r
= wl1273_fm_set_mode(radio
, WL1273_MODE_TX
);
1755 if (modulator
->txsubchans
& V4L2_TUNER_SUB_RDS
)
1756 r
= wl1273_fm_set_rds(radio
, WL1273_RDS_ON
);
1758 r
= wl1273_fm_set_rds(radio
, WL1273_RDS_OFF
);
1760 if (modulator
->txsubchans
& V4L2_TUNER_SUB_MONO
)
1761 r
= core
->write(core
, WL1273_MONO_SET
, WL1273_TX_MONO
);
1763 r
= core
->write(core
, WL1273_MONO_SET
,
1766 dev_warn(radio
->dev
, WL1273_FM_DRIVER_NAME
1767 "MONO_SET fails: %d\n", r
);
1769 mutex_unlock(&core
->lock
);
1774 static int wl1273_fm_vidioc_g_modulator(struct file
*file
, void *priv
,
1775 struct v4l2_modulator
*modulator
)
1777 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1778 struct wl1273_core
*core
= radio
->core
;
1782 dev_dbg(radio
->dev
, "%s\n", __func__
);
1784 strlcpy(modulator
->name
, WL1273_FM_DRIVER_NAME
,
1785 sizeof(modulator
->name
));
1787 modulator
->rangelow
= WL1273_FREQ(WL1273_BAND_JAPAN_LOW
);
1788 modulator
->rangehigh
= WL1273_FREQ(WL1273_BAND_OTHER_HIGH
);
1790 modulator
->capability
= V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_RDS
|
1791 V4L2_TUNER_CAP_STEREO
| V4L2_TUNER_CAP_RDS_BLOCK_IO
;
1793 if (core
->mode
!= WL1273_MODE_TX
)
1796 if (mutex_lock_interruptible(&core
->lock
))
1799 r
= core
->read(core
, WL1273_MONO_SET
, &val
);
1803 if (val
== WL1273_TX_STEREO
)
1804 modulator
->txsubchans
= V4L2_TUNER_SUB_STEREO
;
1806 modulator
->txsubchans
= V4L2_TUNER_SUB_MONO
;
1809 modulator
->txsubchans
|= V4L2_TUNER_SUB_RDS
;
1811 mutex_unlock(&core
->lock
);
1816 static int wl1273_fm_vidioc_log_status(struct file
*file
, void *priv
)
1818 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1819 struct wl1273_core
*core
= radio
->core
;
1820 struct device
*dev
= radio
->dev
;
1824 dev_info(dev
, DRIVER_DESC
);
1826 if (core
->mode
== WL1273_MODE_OFF
) {
1827 dev_info(dev
, "Mode: Off\n");
1831 if (core
->mode
== WL1273_MODE_SUSPENDED
) {
1832 dev_info(dev
, "Mode: Suspended\n");
1836 r
= core
->read(core
, WL1273_ASIC_ID_GET
, &val
);
1838 dev_err(dev
, "%s: Get ASIC_ID fails.\n", __func__
);
1840 dev_info(dev
, "ASIC_ID: 0x%04x\n", val
);
1842 r
= core
->read(core
, WL1273_ASIC_VER_GET
, &val
);
1844 dev_err(dev
, "%s: Get ASIC_VER fails.\n", __func__
);
1846 dev_info(dev
, "ASIC Version: 0x%04x\n", val
);
1848 r
= core
->read(core
, WL1273_FIRM_VER_GET
, &val
);
1850 dev_err(dev
, "%s: Get FIRM_VER fails.\n", __func__
);
1852 dev_info(dev
, "FW version: %d(0x%04x)\n", val
, val
);
1854 r
= core
->read(core
, WL1273_BAND_SET
, &val
);
1856 dev_err(dev
, "%s: Get BAND fails.\n", __func__
);
1858 dev_info(dev
, "BAND: %d\n", val
);
1860 if (core
->mode
== WL1273_MODE_TX
) {
1861 r
= core
->read(core
, WL1273_PUPD_SET
, &val
);
1863 dev_err(dev
, "%s: Get PUPD fails.\n", __func__
);
1865 dev_info(dev
, "PUPD: 0x%04x\n", val
);
1867 r
= core
->read(core
, WL1273_CHANL_SET
, &val
);
1869 dev_err(dev
, "%s: Get CHANL fails.\n", __func__
);
1871 dev_info(dev
, "Tx frequency: %dkHz\n", val
*10);
1872 } else if (core
->mode
== WL1273_MODE_RX
) {
1873 int bf
= radio
->rangelow
;
1875 r
= core
->read(core
, WL1273_FREQ_SET
, &val
);
1877 dev_err(dev
, "%s: Get FREQ fails.\n", __func__
);
1879 dev_info(dev
, "RX Frequency: %dkHz\n", bf
+ val
*50);
1881 r
= core
->read(core
, WL1273_MOST_MODE_SET
, &val
);
1883 dev_err(dev
, "%s: Get MOST_MODE fails.\n",
1886 dev_info(dev
, "MOST_MODE: Stereo according to blend\n");
1888 dev_info(dev
, "MOST_MODE: Force mono output\n");
1890 dev_info(dev
, "MOST_MODE: Unexpected value: %d\n", val
);
1892 r
= core
->read(core
, WL1273_MOST_BLEND_SET
, &val
);
1894 dev_err(dev
, "%s: Get MOST_BLEND fails.\n", __func__
);
1897 "MOST_BLEND: Switched blend & hysteresis.\n");
1899 dev_info(dev
, "MOST_BLEND: Soft blend.\n");
1901 dev_info(dev
, "MOST_BLEND: Unexpected val: %d\n", val
);
1903 r
= core
->read(core
, WL1273_STEREO_GET
, &val
);
1905 dev_err(dev
, "%s: Get STEREO fails.\n", __func__
);
1907 dev_info(dev
, "STEREO: Not detected\n");
1909 dev_info(dev
, "STEREO: Detected\n");
1911 dev_info(dev
, "STEREO: Unexpected value: %d\n", val
);
1913 r
= core
->read(core
, WL1273_RSSI_LVL_GET
, &val
);
1915 dev_err(dev
, "%s: Get RSSI_LVL fails.\n", __func__
);
1917 dev_info(dev
, "RX signal strength: %d\n", (s16
) val
);
1919 r
= core
->read(core
, WL1273_POWER_SET
, &val
);
1921 dev_err(dev
, "%s: Get POWER fails.\n", __func__
);
1923 dev_info(dev
, "POWER: 0x%04x\n", val
);
1925 r
= core
->read(core
, WL1273_INT_MASK_SET
, &val
);
1927 dev_err(dev
, "%s: Get INT_MASK fails.\n", __func__
);
1929 dev_info(dev
, "INT_MASK: 0x%04x\n", val
);
1931 r
= core
->read(core
, WL1273_RDS_SYNC_GET
, &val
);
1933 dev_err(dev
, "%s: Get RDS_SYNC fails.\n",
1936 dev_info(dev
, "RDS_SYNC: Not synchronized\n");
1939 dev_info(dev
, "RDS_SYNC: Synchronized\n");
1941 dev_info(dev
, "RDS_SYNC: Unexpected value: %d\n", val
);
1943 r
= core
->read(core
, WL1273_I2S_MODE_CONFIG_SET
, &val
);
1945 dev_err(dev
, "%s: Get I2S_MODE_CONFIG fails.\n",
1948 dev_info(dev
, "I2S_MODE_CONFIG: 0x%04x\n", val
);
1950 r
= core
->read(core
, WL1273_VOLUME_SET
, &val
);
1952 dev_err(dev
, "%s: Get VOLUME fails.\n", __func__
);
1954 dev_info(dev
, "VOLUME: 0x%04x\n", val
);
1960 static void wl1273_vdev_release(struct video_device
*dev
)
1964 static 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
,
1969 static 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
,
1985 static const 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
,
1990 .vfl_dir
= VFL_DIR_TX
,
1993 static int wl1273_fm_radio_remove(struct platform_device
*pdev
)
1995 struct wl1273_device
*radio
= platform_get_drvdata(pdev
);
1996 struct wl1273_core
*core
= radio
->core
;
1998 dev_info(&pdev
->dev
, "%s.\n", __func__
);
2000 free_irq(core
->client
->irq
, radio
);
2001 core
->pdata
->free_resources();
2003 v4l2_ctrl_handler_free(&radio
->ctrl_handler
);
2004 video_unregister_device(&radio
->videodev
);
2005 v4l2_device_unregister(&radio
->v4l2dev
);
2010 static int wl1273_fm_radio_probe(struct platform_device
*pdev
)
2012 struct wl1273_core
**core
= pdev
->dev
.platform_data
;
2013 struct wl1273_device
*radio
;
2014 struct v4l2_ctrl
*ctrl
;
2017 pr_debug("%s\n", __func__
);
2020 dev_err(&pdev
->dev
, "No platform data.\n");
2025 radio
= devm_kzalloc(&pdev
->dev
, sizeof(*radio
), GFP_KERNEL
);
2031 /* RDS buffer allocation */
2032 radio
->buf_size
= rds_buf
* RDS_BLOCK_SIZE
;
2033 radio
->buffer
= devm_kzalloc(&pdev
->dev
, radio
->buf_size
, GFP_KERNEL
);
2034 if (!radio
->buffer
) {
2035 pr_err("Cannot allocate memory for RDS buffer.\n");
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";
2058 if (radio
->core
->pdata
->request_resources
) {
2059 r
= radio
->core
->pdata
->request_resources(radio
->core
->client
);
2061 dev_err(radio
->dev
, WL1273_FM_DRIVER_NAME
2062 ": Cannot get platform data\n");
2066 dev_dbg(radio
->dev
, "irq: %d\n", radio
->core
->client
->irq
);
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
);
2073 dev_err(radio
->dev
, WL1273_FM_DRIVER_NAME
2074 ": Unable to register IRQ handler: %d\n", r
);
2075 goto err_request_irq
;
2078 dev_err(radio
->dev
, WL1273_FM_DRIVER_NAME
": Core WL1273 IRQ not configured");
2083 init_completion(&radio
->busy
);
2084 init_waitqueue_head(&radio
->read_queue
);
2086 radio
->write_buf
= devm_kzalloc(&pdev
->dev
, 256, GFP_KERNEL
);
2087 if (!radio
->write_buf
) {
2092 radio
->dev
= &pdev
->dev
;
2093 radio
->v4l2dev
.ctrl_handler
= &radio
->ctrl_handler
;
2094 radio
->rds_users
= 0;
2096 r
= v4l2_device_register(&pdev
->dev
, &radio
->v4l2dev
);
2098 dev_err(&pdev
->dev
, "Cannot register v4l2_device.\n");
2102 /* V4L2 configuration */
2103 radio
->videodev
= wl1273_viddev_template
;
2105 radio
->videodev
.v4l2_dev
= &radio
->v4l2dev
;
2107 v4l2_ctrl_handler_init(&radio
->ctrl_handler
, 6);
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
);
2114 v4l2_ctrl_new_std(&radio
->ctrl_handler
, &wl1273_ctrl_ops
,
2115 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 1);
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
);
2122 v4l2_ctrl_new_std(&radio
->ctrl_handler
, &wl1273_ctrl_ops
,
2123 V4L2_CID_TUNE_POWER_LEVEL
, 91, 122, 1, 118);
2125 ctrl
= v4l2_ctrl_new_std(&radio
->ctrl_handler
, &wl1273_ctrl_ops
,
2126 V4L2_CID_TUNE_ANTENNA_CAPACITOR
,
2129 ctrl
->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
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
;
2137 video_set_drvdata(&radio
->videodev
, radio
);
2138 platform_set_drvdata(pdev
, radio
);
2140 /* register video device */
2141 r
= video_register_device(&radio
->videodev
, VFL_TYPE_RADIO
, radio_nr
);
2143 dev_err(&pdev
->dev
, WL1273_FM_DRIVER_NAME
2144 ": Could not register video device\n");
2145 goto handler_init_err
;
2151 v4l2_ctrl_handler_free(&radio
->ctrl_handler
);
2152 v4l2_device_unregister(&radio
->v4l2dev
);
2154 free_irq(radio
->core
->client
->irq
, radio
);
2156 radio
->core
->pdata
->free_resources();
2161 static struct platform_driver wl1273_fm_radio_driver
= {
2162 .probe
= wl1273_fm_radio_probe
,
2163 .remove
= wl1273_fm_radio_remove
,
2165 .name
= "wl1273_fm_radio",
2169 module_platform_driver(wl1273_fm_radio_driver
);
2171 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
2172 MODULE_DESCRIPTION(DRIVER_DESC
);
2173 MODULE_LICENSE("GPL");
2174 MODULE_ALIAS("platform:wl1273_fm_radio");