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.
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
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>
31 #define DRIVER_DESC "Wl1273 FM Radio"
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)
38 #define WL1273_PUPD_SET_OFF 0x00
39 #define WL1273_PUPD_SET_ON 0x01
40 #define WL1273_PUPD_SET_RETENTION 0x10
42 #define WL1273_FREQ(x) (x * 10000 / 625)
43 #define WL1273_INV_FREQ(x) (x * 625 / 10000)
46 * static int radio_nr - The number of the radio device
51 module_param(radio_nr
, int, 0);
52 MODULE_PARM_DESC(radio_nr
, "The number of the radio device. Default = 0");
54 struct wl1273_device
{
58 unsigned int preemphasis
;
60 unsigned int tx_power
;
61 unsigned int rx_frequency
;
62 unsigned int tx_frequency
;
63 unsigned int rangelow
;
64 unsigned int rangehigh
;
71 wait_queue_head_t read_queue
;
72 struct mutex lock
; /* for serializing fm radio operations */
73 struct completion busy
;
75 unsigned char *buffer
;
76 unsigned int buf_size
;
77 unsigned int rd_index
;
78 unsigned int wr_index
;
80 /* Selected interrupts */
84 struct v4l2_ctrl_handler ctrl_handler
;
85 struct v4l2_device v4l2dev
;
86 struct video_device videodev
;
88 struct wl1273_core
*core
;
91 unsigned int rds_users
;
94 #define WL1273_IRQ_MASK (WL1273_FR_EVENT | \
98 * static unsigned int rds_buf - the number of RDS buffer blocks used.
100 * The default number is 100.
102 static unsigned int rds_buf
= 100;
103 module_param(rds_buf
, uint
, 0);
104 MODULE_PARM_DESC(rds_buf
, "Number of RDS buffer entries. Default = 100");
106 static int wl1273_fm_write_fw(struct wl1273_core
*core
,
109 struct i2c_client
*client
= core
->client
;
113 msg
.addr
= client
->addr
;
116 for (i
= 0; i
<= len
; i
++) {
121 dev_dbg(&client
->dev
, "%s:len[%d]: %d\n", __func__
, i
, msg
.len
);
123 r
= i2c_transfer(client
->adapter
, &msg
, 1);
124 if (r
< 0 && i
< len
+ 1)
128 dev_dbg(&client
->dev
, "%s: i: %d\n", __func__
, i
);
129 dev_dbg(&client
->dev
, "%s: len + 1: %d\n", __func__
, len
+ 1);
131 /* Last transfer always fails. */
132 if (i
== len
|| r
== 1)
138 #define WL1273_FIFO_HAS_DATA(status) (1 << 5 & status)
139 #define WL1273_RDS_CORRECTABLE_ERROR (1 << 3)
140 #define WL1273_RDS_UNCORRECTABLE_ERROR (1 << 4)
142 static int wl1273_fm_rds(struct wl1273_device
*radio
)
144 struct wl1273_core
*core
= radio
->core
;
145 struct i2c_client
*client
= core
->client
;
147 u8 b0
= WL1273_RDS_DATA_GET
, status
;
148 struct v4l2_rds_data rds
= { 0, 0, 0 };
149 struct i2c_msg msg
[] = {
151 .addr
= client
->addr
,
157 .addr
= client
->addr
,
165 if (core
->mode
!= WL1273_MODE_RX
)
168 r
= core
->read(core
, WL1273_RDS_SYNC_GET
, &val
);
172 if ((val
& 0x01) == 0) {
173 /* RDS decoder not synchronized */
177 /* copy all four RDS blocks to internal buffer */
179 r
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
180 if (r
!= ARRAY_SIZE(msg
)) {
181 dev_err(radio
->dev
, WL1273_FM_DRIVER_NAME
182 ": %s: read_rds error r == %i)\n",
188 if (!WL1273_FIFO_HAS_DATA(status
))
191 /* copy bits 0-2 (the block ID) to bits 3-5 */
192 rds
.block
= V4L2_RDS_BLOCK_MSK
& status
;
193 rds
.block
|= rds
.block
<< 3;
195 /* copy the error bits to standard positions */
196 if (WL1273_RDS_UNCORRECTABLE_ERROR
& status
) {
197 rds
.block
|= V4L2_RDS_BLOCK_ERROR
;
198 rds
.block
&= ~V4L2_RDS_BLOCK_CORRECTED
;
199 } else if (WL1273_RDS_CORRECTABLE_ERROR
& status
) {
200 rds
.block
&= ~V4L2_RDS_BLOCK_ERROR
;
201 rds
.block
|= V4L2_RDS_BLOCK_CORRECTED
;
204 /* copy RDS block to internal buffer */
205 memcpy(&radio
->buffer
[radio
->wr_index
], &rds
, RDS_BLOCK_SIZE
);
206 radio
->wr_index
+= 3;
208 /* wrap write pointer */
209 if (radio
->wr_index
>= radio
->buf_size
)
212 /* check for overflow & start over */
213 if (radio
->wr_index
== radio
->rd_index
) {
214 dev_dbg(radio
->dev
, "RDS OVERFLOW");
220 } while (WL1273_FIFO_HAS_DATA(status
));
222 /* wake up read queue */
223 if (radio
->wr_index
!= radio
->rd_index
)
224 wake_up_interruptible(&radio
->read_queue
);
229 static irqreturn_t
wl1273_fm_irq_thread_handler(int irq
, void *dev_id
)
231 struct wl1273_device
*radio
= dev_id
;
232 struct wl1273_core
*core
= radio
->core
;
236 r
= core
->read(core
, WL1273_FLAG_GET
, &flags
);
240 if (flags
& WL1273_BL_EVENT
) {
241 radio
->irq_received
= flags
;
242 dev_dbg(radio
->dev
, "IRQ: BL\n");
245 if (flags
& WL1273_RDS_EVENT
) {
248 wl1273_fm_rds(radio
);
251 if (flags
& WL1273_BBLK_EVENT
)
252 dev_dbg(radio
->dev
, "IRQ: BBLK\n");
254 if (flags
& WL1273_LSYNC_EVENT
)
255 dev_dbg(radio
->dev
, "IRQ: LSYNC\n");
257 if (flags
& WL1273_LEV_EVENT
) {
260 r
= core
->read(core
, WL1273_RSSI_LVL_GET
, &level
);
265 dev_dbg(radio
->dev
, "IRQ: LEV: 0x%x04\n", level
);
268 if (flags
& WL1273_IFFR_EVENT
)
269 dev_dbg(radio
->dev
, "IRQ: IFFR\n");
271 if (flags
& WL1273_PI_EVENT
)
272 dev_dbg(radio
->dev
, "IRQ: PI\n");
274 if (flags
& WL1273_PD_EVENT
)
275 dev_dbg(radio
->dev
, "IRQ: PD\n");
277 if (flags
& WL1273_STIC_EVENT
)
278 dev_dbg(radio
->dev
, "IRQ: STIC\n");
280 if (flags
& WL1273_MAL_EVENT
)
281 dev_dbg(radio
->dev
, "IRQ: MAL\n");
283 if (flags
& WL1273_POW_ENB_EVENT
) {
284 complete(&radio
->busy
);
285 dev_dbg(radio
->dev
, "NOT BUSY\n");
286 dev_dbg(radio
->dev
, "IRQ: POW_ENB\n");
289 if (flags
& WL1273_SCAN_OVER_EVENT
)
290 dev_dbg(radio
->dev
, "IRQ: SCAN_OVER\n");
292 if (flags
& WL1273_ERROR_EVENT
)
293 dev_dbg(radio
->dev
, "IRQ: ERROR\n");
295 if (flags
& WL1273_FR_EVENT
) {
298 dev_dbg(radio
->dev
, "IRQ: FR:\n");
300 if (core
->mode
== WL1273_MODE_RX
) {
301 r
= core
->write(core
, WL1273_TUNER_MODE_SET
,
302 TUNER_MODE_STOP_SEARCH
);
305 "%s: TUNER_MODE_SET fails: %d\n",
310 r
= core
->read(core
, WL1273_FREQ_SET
, &freq
);
314 if (radio
->band
== WL1273_BAND_JAPAN
)
315 radio
->rx_frequency
= WL1273_BAND_JAPAN_LOW
+
318 radio
->rx_frequency
= WL1273_BAND_OTHER_LOW
+
321 * The driver works better with this msleep,
322 * the documentation doesn't mention it.
324 usleep_range(10000, 15000);
326 dev_dbg(radio
->dev
, "%dkHz\n", radio
->rx_frequency
);
329 r
= core
->read(core
, WL1273_CHANL_SET
, &freq
);
333 dev_dbg(radio
->dev
, "%dkHz\n", freq
);
335 dev_dbg(radio
->dev
, "%s: NOT BUSY\n", __func__
);
339 core
->write(core
, WL1273_INT_MASK_SET
, radio
->irq_flags
);
340 complete(&radio
->busy
);
345 static int wl1273_fm_set_tx_freq(struct wl1273_device
*radio
, unsigned int freq
)
347 struct wl1273_core
*core
= radio
->core
;
350 if (freq
< WL1273_BAND_TX_LOW
) {
352 "Frequency out of range: %d < %d\n", freq
,
357 if (freq
> WL1273_BAND_TX_HIGH
) {
359 "Frequency out of range: %d > %d\n", freq
,
360 WL1273_BAND_TX_HIGH
);
365 * The driver works better with this sleep,
366 * the documentation doesn't mention it.
368 usleep_range(5000, 10000);
370 dev_dbg(radio
->dev
, "%s: freq: %d kHz\n", __func__
, freq
);
372 /* Set the current tx channel */
373 r
= core
->write(core
, WL1273_CHANL_SET
, freq
/ 10);
377 INIT_COMPLETION(radio
->busy
);
379 /* wait for the FR IRQ */
380 r
= wait_for_completion_timeout(&radio
->busy
, msecs_to_jiffies(2000));
384 dev_dbg(radio
->dev
, "WL1273_CHANL_SET: %d\n", r
);
386 /* Enable the output power */
387 r
= core
->write(core
, WL1273_POWER_ENB_SET
, 1);
391 INIT_COMPLETION(radio
->busy
);
393 /* wait for the POWER_ENB IRQ */
394 r
= wait_for_completion_timeout(&radio
->busy
, msecs_to_jiffies(1000));
398 radio
->tx_frequency
= freq
;
399 dev_dbg(radio
->dev
, "WL1273_POWER_ENB_SET: %d\n", r
);
404 static int wl1273_fm_set_rx_freq(struct wl1273_device
*radio
, unsigned int freq
)
406 struct wl1273_core
*core
= radio
->core
;
409 if (freq
< radio
->rangelow
) {
411 "Frequency out of range: %d < %d\n", freq
,
417 if (freq
> radio
->rangehigh
) {
419 "Frequency out of range: %d > %d\n", freq
,
425 dev_dbg(radio
->dev
, "%s: %dkHz\n", __func__
, freq
);
427 core
->write(core
, WL1273_INT_MASK_SET
, radio
->irq_flags
);
429 if (radio
->band
== WL1273_BAND_JAPAN
)
430 f
= (freq
- WL1273_BAND_JAPAN_LOW
) / 50;
432 f
= (freq
- WL1273_BAND_OTHER_LOW
) / 50;
434 r
= core
->write(core
, WL1273_FREQ_SET
, f
);
436 dev_err(radio
->dev
, "FREQ_SET fails\n");
440 r
= core
->write(core
, WL1273_TUNER_MODE_SET
, TUNER_MODE_PRESET
);
442 dev_err(radio
->dev
, "TUNER_MODE_SET fails\n");
446 INIT_COMPLETION(radio
->busy
);
448 r
= wait_for_completion_timeout(&radio
->busy
, msecs_to_jiffies(2000));
450 dev_err(radio
->dev
, "%s: TIMEOUT\n", __func__
);
456 radio
->rx_frequency
= freq
;
462 static int wl1273_fm_get_freq(struct wl1273_device
*radio
)
464 struct wl1273_core
*core
= radio
->core
;
469 if (core
->mode
== WL1273_MODE_RX
) {
470 r
= core
->read(core
, WL1273_FREQ_SET
, &f
);
474 dev_dbg(radio
->dev
, "Freq get: 0x%04x\n", f
);
475 if (radio
->band
== WL1273_BAND_JAPAN
)
476 freq
= WL1273_BAND_JAPAN_LOW
+ 50 * f
;
478 freq
= WL1273_BAND_OTHER_LOW
+ 50 * f
;
480 r
= core
->read(core
, WL1273_CHANL_SET
, &f
);
491 * wl1273_fm_upload_firmware_patch() - Upload the firmware.
492 * @radio: A pointer to the device struct.
494 * The firmware file consists of arrays of bytes where the first byte
495 * gives the array length. The first byte in the file gives the
496 * number of these arrays.
498 static int wl1273_fm_upload_firmware_patch(struct wl1273_device
*radio
)
500 struct wl1273_core
*core
= radio
->core
;
501 unsigned int packet_num
;
502 const struct firmware
*fw_p
;
503 const char *fw_name
= "radio-wl1273-fw.bin";
504 struct device
*dev
= radio
->dev
;
508 dev_dbg(dev
, "%s:\n", __func__
);
511 * Uploading the firmware patch is not always necessary,
512 * so we only print an info message.
514 if (request_firmware(&fw_p
, fw_name
, dev
)) {
515 dev_info(dev
, "%s - %s not found\n", __func__
, fw_name
);
520 ptr
= (__u8
*) fw_p
->data
;
522 dev_dbg(dev
, "%s: packets: %d\n", __func__
, packet_num
);
524 r
= wl1273_fm_write_fw(core
, ptr
+ 1, packet_num
);
526 dev_err(dev
, "FW upload error: %d\n", r
);
530 /* ignore possible error here */
531 core
->write(core
, WL1273_RESET
, 0);
533 dev_dbg(dev
, "%s - download OK, r: %d\n", __func__
, r
);
535 release_firmware(fw_p
);
539 static int wl1273_fm_stop(struct wl1273_device
*radio
)
541 struct wl1273_core
*core
= radio
->core
;
543 if (core
->mode
== WL1273_MODE_RX
) {
544 int r
= core
->write(core
, WL1273_POWER_SET
,
545 WL1273_POWER_SET_OFF
);
547 dev_err(radio
->dev
, "%s: POWER_SET fails: %d\n",
549 } else if (core
->mode
== WL1273_MODE_TX
) {
550 int r
= core
->write(core
, WL1273_PUPD_SET
,
551 WL1273_PUPD_SET_OFF
);
554 "%s: PUPD_SET fails: %d\n", __func__
, r
);
557 if (core
->pdata
->disable
) {
558 core
->pdata
->disable();
559 dev_dbg(radio
->dev
, "Back to reset\n");
565 static int wl1273_fm_start(struct wl1273_device
*radio
, int new_mode
)
567 struct wl1273_core
*core
= radio
->core
;
568 struct wl1273_fm_platform_data
*pdata
= core
->pdata
;
569 struct device
*dev
= radio
->dev
;
572 if (pdata
->enable
&& core
->mode
== WL1273_MODE_OFF
) {
573 dev_dbg(radio
->dev
, "Out of reset\n");
579 if (new_mode
== WL1273_MODE_RX
) {
580 u16 val
= WL1273_POWER_SET_FM
;
583 val
|= WL1273_POWER_SET_RDS
;
585 /* If this fails try again */
586 r
= core
->write(core
, WL1273_POWER_SET
, val
);
590 r
= core
->write(core
, WL1273_POWER_SET
, val
);
592 dev_err(dev
, "%s: POWER_SET fails\n", __func__
);
597 /* rds buffer configuration */
601 } else if (new_mode
== WL1273_MODE_TX
) {
602 /* If this fails try again once */
603 r
= core
->write(core
, WL1273_PUPD_SET
, WL1273_PUPD_SET_ON
);
606 r
= core
->write(core
, WL1273_PUPD_SET
,
609 dev_err(dev
, "%s: PUPD_SET fails\n", __func__
);
615 r
= core
->write(core
, WL1273_RDS_DATA_ENB
, 1);
617 r
= core
->write(core
, WL1273_RDS_DATA_ENB
, 0);
619 dev_warn(dev
, "%s: Illegal mode.\n", __func__
);
622 if (core
->mode
== WL1273_MODE_OFF
) {
623 r
= wl1273_fm_upload_firmware_patch(radio
);
625 dev_warn(dev
, "Firmware upload failed.\n");
628 * Sometimes the chip is in a wrong power state at this point.
629 * So we set the power once again.
631 if (new_mode
== WL1273_MODE_RX
) {
632 u16 val
= WL1273_POWER_SET_FM
;
635 val
|= WL1273_POWER_SET_RDS
;
637 r
= core
->write(core
, WL1273_POWER_SET
, val
);
639 dev_err(dev
, "%s: POWER_SET fails\n", __func__
);
642 } else if (new_mode
== WL1273_MODE_TX
) {
643 r
= core
->write(core
, WL1273_PUPD_SET
,
646 dev_err(dev
, "%s: PUPD_SET fails\n", __func__
);
657 dev_dbg(dev
, "%s: return: %d\n", __func__
, r
);
661 static int wl1273_fm_suspend(struct wl1273_device
*radio
)
663 struct wl1273_core
*core
= radio
->core
;
666 /* Cannot go from OFF to SUSPENDED */
667 if (core
->mode
== WL1273_MODE_RX
)
668 r
= core
->write(core
, WL1273_POWER_SET
,
669 WL1273_POWER_SET_RETENTION
);
670 else if (core
->mode
== WL1273_MODE_TX
)
671 r
= core
->write(core
, WL1273_PUPD_SET
,
672 WL1273_PUPD_SET_RETENTION
);
677 dev_err(radio
->dev
, "%s: POWER_SET fails: %d\n", __func__
, r
);
685 static int wl1273_fm_set_mode(struct wl1273_device
*radio
, int mode
)
687 struct wl1273_core
*core
= radio
->core
;
688 struct device
*dev
= radio
->dev
;
692 dev_dbg(dev
, "%s\n", __func__
);
693 dev_dbg(dev
, "Forbidden modes: 0x%02x\n", radio
->forbidden
);
695 old_mode
= core
->mode
;
696 if (mode
& radio
->forbidden
) {
704 r
= wl1273_fm_start(radio
, mode
);
706 dev_err(dev
, "%s: Cannot start.\n", __func__
);
707 wl1273_fm_stop(radio
);
712 r
= core
->write(core
, WL1273_INT_MASK_SET
, radio
->irq_flags
);
714 dev_err(dev
, "INT_MASK_SET fails.\n");
718 /* remember previous settings */
719 if (mode
== WL1273_MODE_RX
) {
720 r
= wl1273_fm_set_rx_freq(radio
, radio
->rx_frequency
);
722 dev_err(dev
, "set freq fails: %d.\n", r
);
726 r
= core
->set_volume(core
, core
->volume
);
728 dev_err(dev
, "set volume fails: %d.\n", r
);
732 dev_dbg(dev
, "%s: Set vol: %d.\n", __func__
,
735 r
= wl1273_fm_set_tx_freq(radio
, radio
->tx_frequency
);
737 dev_err(dev
, "set freq fails: %d.\n", r
);
742 dev_dbg(radio
->dev
, "%s: Set audio mode.\n", __func__
);
744 r
= core
->set_audio(core
, core
->audio_mode
);
746 dev_err(dev
, "Cannot set audio mode.\n");
749 case WL1273_MODE_OFF
:
750 r
= wl1273_fm_stop(radio
);
752 dev_err(dev
, "%s: Off fails: %d\n", __func__
, r
);
754 core
->mode
= WL1273_MODE_OFF
;
758 case WL1273_MODE_SUSPENDED
:
759 r
= wl1273_fm_suspend(radio
);
761 dev_err(dev
, "%s: Suspend fails: %d\n", __func__
, r
);
763 core
->mode
= WL1273_MODE_SUSPENDED
;
768 dev_err(dev
, "%s: Unknown mode: %d\n", __func__
, mode
);
774 core
->mode
= old_mode
;
779 static int wl1273_fm_set_seek(struct wl1273_device
*radio
,
780 unsigned int wrap_around
,
781 unsigned int seek_upward
,
784 struct wl1273_core
*core
= radio
->core
;
786 unsigned int dir
= (seek_upward
== 0) ? 0 : 1;
789 f
= radio
->rx_frequency
;
790 dev_dbg(radio
->dev
, "rx_frequency: %d\n", f
);
792 if (dir
&& f
+ radio
->spacing
<= radio
->rangehigh
)
793 r
= wl1273_fm_set_rx_freq(radio
, f
+ radio
->spacing
);
794 else if (dir
&& wrap_around
)
795 r
= wl1273_fm_set_rx_freq(radio
, radio
->rangelow
);
796 else if (f
- radio
->spacing
>= radio
->rangelow
)
797 r
= wl1273_fm_set_rx_freq(radio
, f
- radio
->spacing
);
798 else if (wrap_around
)
799 r
= wl1273_fm_set_rx_freq(radio
, radio
->rangehigh
);
804 if (level
< SCHAR_MIN
|| level
> SCHAR_MAX
)
807 INIT_COMPLETION(radio
->busy
);
808 dev_dbg(radio
->dev
, "%s: BUSY\n", __func__
);
810 r
= core
->write(core
, WL1273_INT_MASK_SET
, radio
->irq_flags
);
814 dev_dbg(radio
->dev
, "%s\n", __func__
);
816 r
= core
->write(core
, WL1273_SEARCH_LVL_SET
, level
);
820 r
= core
->write(core
, WL1273_SEARCH_DIR_SET
, dir
);
824 r
= core
->write(core
, WL1273_TUNER_MODE_SET
, TUNER_MODE_AUTO_SEEK
);
828 wait_for_completion_timeout(&radio
->busy
, msecs_to_jiffies(1000));
829 if (!(radio
->irq_received
& WL1273_BL_EVENT
))
832 radio
->irq_received
&= ~WL1273_BL_EVENT
;
838 dev_dbg(radio
->dev
, "Wrap around in HW seek.\n");
843 f
= radio
->rangehigh
;
845 r
= wl1273_fm_set_rx_freq(radio
, f
);
849 INIT_COMPLETION(radio
->busy
);
850 dev_dbg(radio
->dev
, "%s: BUSY\n", __func__
);
852 r
= core
->write(core
, WL1273_TUNER_MODE_SET
, TUNER_MODE_AUTO_SEEK
);
856 wait_for_completion_timeout(&radio
->busy
, msecs_to_jiffies(1000));
858 dev_dbg(radio
->dev
, "%s: Err: %d\n", __func__
, r
);
863 * wl1273_fm_get_tx_ctune() - Get the TX tuning capacitor value.
864 * @radio: A pointer to the device struct.
866 static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device
*radio
)
868 struct wl1273_core
*core
= radio
->core
;
869 struct device
*dev
= radio
->dev
;
873 if (core
->mode
== WL1273_MODE_OFF
||
874 core
->mode
== WL1273_MODE_SUSPENDED
)
877 r
= core
->read(core
, WL1273_READ_FMANT_TUNE_VALUE
, &val
);
879 dev_err(dev
, "%s: read error: %d\n", __func__
, r
);
888 * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
889 * @radio: A pointer to the device struct.
890 * @preemphasis: The new pre-amphasis value.
892 * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
893 * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
895 static int wl1273_fm_set_preemphasis(struct wl1273_device
*radio
,
896 unsigned int preemphasis
)
898 struct wl1273_core
*core
= radio
->core
;
902 if (core
->mode
== WL1273_MODE_OFF
||
903 core
->mode
== WL1273_MODE_SUSPENDED
)
906 mutex_lock(&core
->lock
);
908 switch (preemphasis
) {
909 case V4L2_PREEMPHASIS_DISABLED
:
912 case V4L2_PREEMPHASIS_50_uS
:
915 case V4L2_PREEMPHASIS_75_uS
:
923 r
= core
->write(core
, WL1273_PREMPH_SET
, em
);
927 radio
->preemphasis
= preemphasis
;
930 mutex_unlock(&core
->lock
);
934 static int wl1273_fm_rds_on(struct wl1273_device
*radio
)
936 struct wl1273_core
*core
= radio
->core
;
939 dev_dbg(radio
->dev
, "%s\n", __func__
);
943 r
= core
->write(core
, WL1273_POWER_SET
,
944 WL1273_POWER_SET_FM
| WL1273_POWER_SET_RDS
);
948 r
= wl1273_fm_set_rx_freq(radio
, radio
->rx_frequency
);
950 dev_err(radio
->dev
, "set freq fails: %d.\n", r
);
955 static int wl1273_fm_rds_off(struct wl1273_device
*radio
)
957 struct wl1273_core
*core
= radio
->core
;
963 radio
->irq_flags
&= ~WL1273_RDS_EVENT
;
965 r
= core
->write(core
, WL1273_INT_MASK_SET
, radio
->irq_flags
);
969 /* Service pending read */
970 wake_up_interruptible(&radio
->read_queue
);
972 dev_dbg(radio
->dev
, "%s\n", __func__
);
974 r
= core
->write(core
, WL1273_POWER_SET
, WL1273_POWER_SET_FM
);
978 r
= wl1273_fm_set_rx_freq(radio
, radio
->rx_frequency
);
980 dev_err(radio
->dev
, "set freq fails: %d.\n", r
);
982 dev_dbg(radio
->dev
, "%s: exiting...\n", __func__
);
987 static int wl1273_fm_set_rds(struct wl1273_device
*radio
, unsigned int new_mode
)
990 struct wl1273_core
*core
= radio
->core
;
992 if (core
->mode
== WL1273_MODE_OFF
||
993 core
->mode
== WL1273_MODE_SUSPENDED
)
996 if (new_mode
== WL1273_RDS_RESET
) {
997 r
= core
->write(core
, WL1273_RDS_CNTRL_SET
, 1);
1001 if (core
->mode
== WL1273_MODE_TX
&& new_mode
== WL1273_RDS_OFF
) {
1002 r
= core
->write(core
, WL1273_RDS_DATA_ENB
, 0);
1003 } else if (core
->mode
== WL1273_MODE_TX
&& new_mode
== WL1273_RDS_ON
) {
1004 r
= core
->write(core
, WL1273_RDS_DATA_ENB
, 1);
1005 } else if (core
->mode
== WL1273_MODE_RX
&& new_mode
== WL1273_RDS_OFF
) {
1006 r
= wl1273_fm_rds_off(radio
);
1007 } else if (core
->mode
== WL1273_MODE_RX
&& new_mode
== WL1273_RDS_ON
) {
1008 r
= wl1273_fm_rds_on(radio
);
1010 dev_err(radio
->dev
, "%s: Unknown mode: %d\n",
1011 __func__
, new_mode
);
1016 radio
->rds_on
= (new_mode
== WL1273_RDS_ON
) ? true : false;
1021 static ssize_t
wl1273_fm_fops_write(struct file
*file
, const char __user
*buf
,
1022 size_t count
, loff_t
*ppos
)
1024 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1025 struct wl1273_core
*core
= radio
->core
;
1029 dev_dbg(radio
->dev
, "%s\n", __func__
);
1031 if (core
->mode
!= WL1273_MODE_TX
)
1034 if (radio
->rds_users
== 0) {
1035 dev_warn(radio
->dev
, "%s: RDS not on.\n", __func__
);
1039 if (mutex_lock_interruptible(&core
->lock
))
1042 * Multiple processes can open the device, but only
1043 * one gets to write to it.
1045 if (radio
->owner
&& radio
->owner
!= file
) {
1049 radio
->owner
= file
;
1057 core
->write(core
, WL1273_RDS_CONFIG_DATA_SET
, val
);
1059 if (copy_from_user(radio
->write_buf
+ 1, buf
, val
)) {
1064 dev_dbg(radio
->dev
, "Count: %d\n", val
);
1065 dev_dbg(radio
->dev
, "From user: \"%s\"\n", radio
->write_buf
);
1067 radio
->write_buf
[0] = WL1273_RDS_DATA_SET
;
1068 core
->write_data(core
, radio
->write_buf
, val
+ 1);
1072 mutex_unlock(&core
->lock
);
1077 static unsigned int wl1273_fm_fops_poll(struct file
*file
,
1078 struct poll_table_struct
*pts
)
1080 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1081 struct wl1273_core
*core
= radio
->core
;
1083 if (radio
->owner
&& radio
->owner
!= file
)
1086 radio
->owner
= file
;
1088 if (core
->mode
== WL1273_MODE_RX
) {
1089 poll_wait(file
, &radio
->read_queue
, pts
);
1091 if (radio
->rd_index
!= radio
->wr_index
)
1092 return POLLIN
| POLLRDNORM
;
1094 } else if (core
->mode
== WL1273_MODE_TX
) {
1095 return POLLOUT
| POLLWRNORM
;
1101 static int wl1273_fm_fops_open(struct file
*file
)
1103 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1104 struct wl1273_core
*core
= radio
->core
;
1107 dev_dbg(radio
->dev
, "%s\n", __func__
);
1109 if (core
->mode
== WL1273_MODE_RX
&& radio
->rds_on
&&
1110 !radio
->rds_users
) {
1111 dev_dbg(radio
->dev
, "%s: Mode: %d\n", __func__
, core
->mode
);
1113 if (mutex_lock_interruptible(&core
->lock
))
1116 radio
->irq_flags
|= WL1273_RDS_EVENT
;
1118 r
= core
->write(core
, WL1273_INT_MASK_SET
,
1121 mutex_unlock(&core
->lock
);
1127 mutex_unlock(&core
->lock
);
1133 static int wl1273_fm_fops_release(struct file
*file
)
1135 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1136 struct wl1273_core
*core
= radio
->core
;
1139 dev_dbg(radio
->dev
, "%s\n", __func__
);
1141 if (radio
->rds_users
> 0) {
1143 if (radio
->rds_users
== 0) {
1144 if (mutex_lock_interruptible(&core
->lock
))
1147 radio
->irq_flags
&= ~WL1273_RDS_EVENT
;
1149 if (core
->mode
== WL1273_MODE_RX
) {
1150 r
= core
->write(core
,
1151 WL1273_INT_MASK_SET
,
1154 mutex_unlock(&core
->lock
);
1158 mutex_unlock(&core
->lock
);
1162 if (file
== radio
->owner
)
1163 radio
->owner
= NULL
;
1168 static ssize_t
wl1273_fm_fops_read(struct file
*file
, char __user
*buf
,
1169 size_t count
, loff_t
*ppos
)
1172 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1173 struct wl1273_core
*core
= radio
->core
;
1174 unsigned int block_count
= 0;
1177 dev_dbg(radio
->dev
, "%s\n", __func__
);
1179 if (core
->mode
!= WL1273_MODE_RX
)
1182 if (radio
->rds_users
== 0) {
1183 dev_warn(radio
->dev
, "%s: RDS not on.\n", __func__
);
1187 if (mutex_lock_interruptible(&core
->lock
))
1191 * Multiple processes can open the device, but only
1192 * one at a time gets read access.
1194 if (radio
->owner
&& radio
->owner
!= file
) {
1198 radio
->owner
= file
;
1200 r
= core
->read(core
, WL1273_RDS_SYNC_GET
, &val
);
1202 dev_err(radio
->dev
, "%s: Get RDS_SYNC fails.\n", __func__
);
1204 } else if (val
== 0) {
1205 dev_info(radio
->dev
, "RDS_SYNC: Not synchronized\n");
1210 /* block if no new data available */
1211 while (radio
->wr_index
== radio
->rd_index
) {
1212 if (file
->f_flags
& O_NONBLOCK
) {
1217 dev_dbg(radio
->dev
, "%s: Wait for RDS data.\n", __func__
);
1218 if (wait_event_interruptible(radio
->read_queue
,
1220 radio
->rd_index
) < 0) {
1226 /* calculate block count from byte count */
1227 count
/= RDS_BLOCK_SIZE
;
1229 /* copy RDS blocks from the internal buffer and to user buffer */
1230 while (block_count
< count
) {
1231 if (radio
->rd_index
== radio
->wr_index
)
1234 /* always transfer complete RDS blocks */
1235 if (copy_to_user(buf
, &radio
->buffer
[radio
->rd_index
],
1239 /* increment and wrap the read pointer */
1240 radio
->rd_index
+= RDS_BLOCK_SIZE
;
1241 if (radio
->rd_index
>= radio
->buf_size
)
1242 radio
->rd_index
= 0;
1244 /* increment counters */
1246 buf
+= RDS_BLOCK_SIZE
;
1247 r
+= RDS_BLOCK_SIZE
;
1251 dev_dbg(radio
->dev
, "%s: exit\n", __func__
);
1252 mutex_unlock(&core
->lock
);
1257 static const struct v4l2_file_operations wl1273_fops
= {
1258 .owner
= THIS_MODULE
,
1259 .read
= wl1273_fm_fops_read
,
1260 .write
= wl1273_fm_fops_write
,
1261 .poll
= wl1273_fm_fops_poll
,
1262 .unlocked_ioctl
= video_ioctl2
,
1263 .open
= wl1273_fm_fops_open
,
1264 .release
= wl1273_fm_fops_release
,
1267 static int wl1273_fm_vidioc_querycap(struct file
*file
, void *priv
,
1268 struct v4l2_capability
*capability
)
1270 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1272 dev_dbg(radio
->dev
, "%s\n", __func__
);
1274 strlcpy(capability
->driver
, WL1273_FM_DRIVER_NAME
,
1275 sizeof(capability
->driver
));
1276 strlcpy(capability
->card
, "Texas Instruments Wl1273 FM Radio",
1277 sizeof(capability
->card
));
1278 strlcpy(capability
->bus_info
, radio
->bus_type
,
1279 sizeof(capability
->bus_info
));
1281 capability
->capabilities
= V4L2_CAP_HW_FREQ_SEEK
|
1282 V4L2_CAP_TUNER
| V4L2_CAP_RADIO
| V4L2_CAP_AUDIO
|
1283 V4L2_CAP_RDS_CAPTURE
| V4L2_CAP_MODULATOR
|
1284 V4L2_CAP_RDS_OUTPUT
;
1289 static int wl1273_fm_vidioc_g_input(struct file
*file
, void *priv
,
1292 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1294 dev_dbg(radio
->dev
, "%s\n", __func__
);
1301 static int wl1273_fm_vidioc_s_input(struct file
*file
, void *priv
,
1304 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1306 dev_dbg(radio
->dev
, "%s\n", __func__
);
1315 * wl1273_fm_set_tx_power() - Set the transmission power value.
1316 * @core: A pointer to the device struct.
1317 * @power: The new power value.
1319 static int wl1273_fm_set_tx_power(struct wl1273_device
*radio
, u16 power
)
1321 struct wl1273_core
*core
= radio
->core
;
1324 if (core
->mode
== WL1273_MODE_OFF
||
1325 core
->mode
== WL1273_MODE_SUSPENDED
)
1328 mutex_lock(&core
->lock
);
1330 /* Convert the dBuV value to chip presentation */
1331 r
= core
->write(core
, WL1273_POWER_LEV_SET
, 122 - power
);
1335 radio
->tx_power
= power
;
1338 mutex_unlock(&core
->lock
);
1342 #define WL1273_SPACING_50kHz 1
1343 #define WL1273_SPACING_100kHz 2
1344 #define WL1273_SPACING_200kHz 4
1346 static int wl1273_fm_tx_set_spacing(struct wl1273_device
*radio
,
1347 unsigned int spacing
)
1349 struct wl1273_core
*core
= radio
->core
;
1353 r
= core
->write(core
, WL1273_SCAN_SPACING_SET
,
1354 WL1273_SPACING_100kHz
);
1355 radio
->spacing
= 100;
1356 } else if (spacing
- 50000 < 25000) {
1357 r
= core
->write(core
, WL1273_SCAN_SPACING_SET
,
1358 WL1273_SPACING_50kHz
);
1359 radio
->spacing
= 50;
1360 } else if (spacing
- 100000 < 50000) {
1361 r
= core
->write(core
, WL1273_SCAN_SPACING_SET
,
1362 WL1273_SPACING_100kHz
);
1363 radio
->spacing
= 100;
1365 r
= core
->write(core
, WL1273_SCAN_SPACING_SET
,
1366 WL1273_SPACING_200kHz
);
1367 radio
->spacing
= 200;
1373 static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
1375 struct wl1273_device
*radio
= ctrl
->priv
;
1376 struct wl1273_core
*core
= radio
->core
;
1378 dev_dbg(radio
->dev
, "%s\n", __func__
);
1380 if (mutex_lock_interruptible(&core
->lock
))
1384 case V4L2_CID_TUNE_ANTENNA_CAPACITOR
:
1385 ctrl
->val
= wl1273_fm_get_tx_ctune(radio
);
1389 dev_warn(radio
->dev
, "%s: Unknown IOCTL: %d\n",
1390 __func__
, ctrl
->id
);
1394 mutex_unlock(&core
->lock
);
1399 #define WL1273_MUTE_SOFT_ENABLE (1 << 0)
1400 #define WL1273_MUTE_AC (1 << 1)
1401 #define WL1273_MUTE_HARD_LEFT (1 << 2)
1402 #define WL1273_MUTE_HARD_RIGHT (1 << 3)
1403 #define WL1273_MUTE_SOFT_FORCE (1 << 4)
1405 static inline struct wl1273_device
*to_radio(struct v4l2_ctrl
*ctrl
)
1407 return container_of(ctrl
->handler
, struct wl1273_device
, ctrl_handler
);
1410 static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl
*ctrl
)
1412 struct wl1273_device
*radio
= to_radio(ctrl
);
1413 struct wl1273_core
*core
= radio
->core
;
1416 dev_dbg(radio
->dev
, "%s\n", __func__
);
1419 case V4L2_CID_AUDIO_MUTE
:
1420 if (mutex_lock_interruptible(&core
->lock
))
1423 if (core
->mode
== WL1273_MODE_RX
&& ctrl
->val
)
1424 r
= core
->write(core
,
1425 WL1273_MUTE_STATUS_SET
,
1426 WL1273_MUTE_HARD_LEFT
|
1427 WL1273_MUTE_HARD_RIGHT
);
1428 else if (core
->mode
== WL1273_MODE_RX
)
1429 r
= core
->write(core
,
1430 WL1273_MUTE_STATUS_SET
, 0x0);
1431 else if (core
->mode
== WL1273_MODE_TX
&& ctrl
->val
)
1432 r
= core
->write(core
, WL1273_MUTE
, 1);
1433 else if (core
->mode
== WL1273_MODE_TX
)
1434 r
= core
->write(core
, WL1273_MUTE
, 0);
1436 mutex_unlock(&core
->lock
);
1439 case V4L2_CID_AUDIO_VOLUME
:
1441 r
= wl1273_fm_set_mode(radio
, WL1273_MODE_OFF
);
1443 r
= core
->set_volume(core
, core
->volume
);
1446 case V4L2_CID_TUNE_PREEMPHASIS
:
1447 r
= wl1273_fm_set_preemphasis(radio
, ctrl
->val
);
1450 case V4L2_CID_TUNE_POWER_LEVEL
:
1451 r
= wl1273_fm_set_tx_power(radio
, ctrl
->val
);
1455 dev_warn(radio
->dev
, "%s: Unknown IOCTL: %d\n",
1456 __func__
, ctrl
->id
);
1460 dev_dbg(radio
->dev
, "%s\n", __func__
);
1464 static int wl1273_fm_vidioc_g_audio(struct file
*file
, void *priv
,
1465 struct v4l2_audio
*audio
)
1467 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1469 dev_dbg(radio
->dev
, "%s\n", __func__
);
1471 if (audio
->index
> 1)
1474 strlcpy(audio
->name
, "Radio", sizeof(audio
->name
));
1475 audio
->capability
= V4L2_AUDCAP_STEREO
;
1480 static int wl1273_fm_vidioc_s_audio(struct file
*file
, void *priv
,
1481 struct v4l2_audio
*audio
)
1483 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1485 dev_dbg(radio
->dev
, "%s\n", __func__
);
1487 if (audio
->index
!= 0)
1493 #define WL1273_RDS_NOT_SYNCHRONIZED 0
1494 #define WL1273_RDS_SYNCHRONIZED 1
1496 static int wl1273_fm_vidioc_g_tuner(struct file
*file
, void *priv
,
1497 struct v4l2_tuner
*tuner
)
1499 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1500 struct wl1273_core
*core
= radio
->core
;
1504 dev_dbg(radio
->dev
, "%s\n", __func__
);
1506 if (tuner
->index
> 0)
1509 strlcpy(tuner
->name
, WL1273_FM_DRIVER_NAME
, sizeof(tuner
->name
));
1510 tuner
->type
= V4L2_TUNER_RADIO
;
1512 tuner
->rangelow
= WL1273_FREQ(WL1273_BAND_JAPAN_LOW
);
1513 tuner
->rangehigh
= WL1273_FREQ(WL1273_BAND_OTHER_HIGH
);
1515 tuner
->capability
= V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_RDS
|
1516 V4L2_TUNER_CAP_STEREO
| V4L2_TUNER_CAP_RDS_BLOCK_IO
;
1519 tuner
->audmode
= V4L2_TUNER_MODE_STEREO
;
1521 tuner
->audmode
= V4L2_TUNER_MODE_MONO
;
1523 if (core
->mode
!= WL1273_MODE_RX
)
1526 if (mutex_lock_interruptible(&core
->lock
))
1529 r
= core
->read(core
, WL1273_STEREO_GET
, &val
);
1534 tuner
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1536 tuner
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1538 r
= core
->read(core
, WL1273_RSSI_LVL_GET
, &val
);
1542 tuner
->signal
= (s16
) val
;
1543 dev_dbg(radio
->dev
, "Signal: %d\n", tuner
->signal
);
1547 r
= core
->read(core
, WL1273_RDS_SYNC_GET
, &val
);
1551 if (val
== WL1273_RDS_SYNCHRONIZED
)
1552 tuner
->rxsubchans
|= V4L2_TUNER_SUB_RDS
;
1554 mutex_unlock(&core
->lock
);
1559 static int wl1273_fm_vidioc_s_tuner(struct file
*file
, void *priv
,
1560 struct v4l2_tuner
*tuner
)
1562 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1563 struct wl1273_core
*core
= radio
->core
;
1566 dev_dbg(radio
->dev
, "%s\n", __func__
);
1567 dev_dbg(radio
->dev
, "tuner->index: %d\n", tuner
->index
);
1568 dev_dbg(radio
->dev
, "tuner->name: %s\n", tuner
->name
);
1569 dev_dbg(radio
->dev
, "tuner->capability: 0x%04x\n", tuner
->capability
);
1570 dev_dbg(radio
->dev
, "tuner->rxsubchans: 0x%04x\n", tuner
->rxsubchans
);
1571 dev_dbg(radio
->dev
, "tuner->rangelow: %d\n", tuner
->rangelow
);
1572 dev_dbg(radio
->dev
, "tuner->rangehigh: %d\n", tuner
->rangehigh
);
1574 if (tuner
->index
> 0)
1577 if (mutex_lock_interruptible(&core
->lock
))
1580 r
= wl1273_fm_set_mode(radio
, WL1273_MODE_RX
);
1584 if (tuner
->rxsubchans
& V4L2_TUNER_SUB_RDS
)
1585 r
= wl1273_fm_set_rds(radio
, WL1273_RDS_ON
);
1587 r
= wl1273_fm_set_rds(radio
, WL1273_RDS_OFF
);
1590 dev_warn(radio
->dev
, "%s: RDS fails: %d\n", __func__
, r
);
1592 if (tuner
->audmode
== V4L2_TUNER_MODE_MONO
) {
1593 r
= core
->write(core
, WL1273_MOST_MODE_SET
, WL1273_RX_MONO
);
1595 dev_warn(radio
->dev
, "%s: MOST_MODE fails: %d\n",
1599 radio
->stereo
= false;
1600 } else if (tuner
->audmode
== V4L2_TUNER_MODE_STEREO
) {
1601 r
= core
->write(core
, WL1273_MOST_MODE_SET
, WL1273_RX_STEREO
);
1603 dev_warn(radio
->dev
, "%s: MOST_MODE fails: %d\n",
1607 radio
->stereo
= true;
1609 dev_err(radio
->dev
, "%s: tuner->audmode: %d\n",
1610 __func__
, tuner
->audmode
);
1616 mutex_unlock(&core
->lock
);
1621 static int wl1273_fm_vidioc_g_frequency(struct file
*file
, void *priv
,
1622 struct v4l2_frequency
*freq
)
1624 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1625 struct wl1273_core
*core
= radio
->core
;
1627 dev_dbg(radio
->dev
, "%s\n", __func__
);
1629 if (mutex_lock_interruptible(&core
->lock
))
1632 freq
->type
= V4L2_TUNER_RADIO
;
1633 freq
->frequency
= WL1273_FREQ(wl1273_fm_get_freq(radio
));
1635 mutex_unlock(&core
->lock
);
1640 static int wl1273_fm_vidioc_s_frequency(struct file
*file
, void *priv
,
1641 struct v4l2_frequency
*freq
)
1643 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1644 struct wl1273_core
*core
= radio
->core
;
1647 dev_dbg(radio
->dev
, "%s: %d\n", __func__
, freq
->frequency
);
1649 if (freq
->type
!= V4L2_TUNER_RADIO
) {
1651 "freq->type != V4L2_TUNER_RADIO: %d\n", freq
->type
);
1655 if (mutex_lock_interruptible(&core
->lock
))
1658 if (core
->mode
== WL1273_MODE_RX
) {
1659 dev_dbg(radio
->dev
, "freq: %d\n", freq
->frequency
);
1661 r
= wl1273_fm_set_rx_freq(radio
,
1662 WL1273_INV_FREQ(freq
->frequency
));
1664 dev_warn(radio
->dev
, WL1273_FM_DRIVER_NAME
1665 ": set frequency failed with %d\n", r
);
1667 r
= wl1273_fm_set_tx_freq(radio
,
1668 WL1273_INV_FREQ(freq
->frequency
));
1670 dev_warn(radio
->dev
, WL1273_FM_DRIVER_NAME
1671 ": set frequency failed with %d\n", r
);
1674 mutex_unlock(&core
->lock
);
1676 dev_dbg(radio
->dev
, "wl1273_vidioc_s_frequency: DONE\n");
1680 #define WL1273_DEFAULT_SEEK_LEVEL 7
1682 static int wl1273_fm_vidioc_s_hw_freq_seek(struct file
*file
, void *priv
,
1683 struct v4l2_hw_freq_seek
*seek
)
1685 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1686 struct wl1273_core
*core
= radio
->core
;
1689 dev_dbg(radio
->dev
, "%s\n", __func__
);
1691 if (seek
->tuner
!= 0 || seek
->type
!= V4L2_TUNER_RADIO
)
1694 if (mutex_lock_interruptible(&core
->lock
))
1697 r
= wl1273_fm_set_mode(radio
, WL1273_MODE_RX
);
1701 r
= wl1273_fm_tx_set_spacing(radio
, seek
->spacing
);
1703 dev_warn(radio
->dev
, "HW seek failed: %d\n", r
);
1705 r
= wl1273_fm_set_seek(radio
, seek
->wrap_around
, seek
->seek_upward
,
1706 WL1273_DEFAULT_SEEK_LEVEL
);
1708 dev_warn(radio
->dev
, "HW seek failed: %d\n", r
);
1711 mutex_unlock(&core
->lock
);
1715 static int wl1273_fm_vidioc_s_modulator(struct file
*file
, void *priv
,
1716 struct v4l2_modulator
*modulator
)
1718 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1719 struct wl1273_core
*core
= radio
->core
;
1722 dev_dbg(radio
->dev
, "%s\n", __func__
);
1724 if (modulator
->index
> 0)
1727 if (mutex_lock_interruptible(&core
->lock
))
1730 r
= wl1273_fm_set_mode(radio
, WL1273_MODE_TX
);
1734 if (modulator
->txsubchans
& V4L2_TUNER_SUB_RDS
)
1735 r
= wl1273_fm_set_rds(radio
, WL1273_RDS_ON
);
1737 r
= wl1273_fm_set_rds(radio
, WL1273_RDS_OFF
);
1739 if (modulator
->txsubchans
& V4L2_TUNER_SUB_MONO
)
1740 r
= core
->write(core
, WL1273_MONO_SET
, WL1273_TX_MONO
);
1742 r
= core
->write(core
, WL1273_MONO_SET
,
1745 dev_warn(radio
->dev
, WL1273_FM_DRIVER_NAME
1746 "MONO_SET fails: %d\n", r
);
1748 mutex_unlock(&core
->lock
);
1753 static int wl1273_fm_vidioc_g_modulator(struct file
*file
, void *priv
,
1754 struct v4l2_modulator
*modulator
)
1756 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1757 struct wl1273_core
*core
= radio
->core
;
1761 dev_dbg(radio
->dev
, "%s\n", __func__
);
1763 strlcpy(modulator
->name
, WL1273_FM_DRIVER_NAME
,
1764 sizeof(modulator
->name
));
1766 modulator
->rangelow
= WL1273_FREQ(WL1273_BAND_JAPAN_LOW
);
1767 modulator
->rangehigh
= WL1273_FREQ(WL1273_BAND_OTHER_HIGH
);
1769 modulator
->capability
= V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_RDS
|
1770 V4L2_TUNER_CAP_STEREO
| V4L2_TUNER_CAP_RDS_BLOCK_IO
;
1772 if (core
->mode
!= WL1273_MODE_TX
)
1775 if (mutex_lock_interruptible(&core
->lock
))
1778 r
= core
->read(core
, WL1273_MONO_SET
, &val
);
1782 if (val
== WL1273_TX_STEREO
)
1783 modulator
->txsubchans
= V4L2_TUNER_SUB_STEREO
;
1785 modulator
->txsubchans
= V4L2_TUNER_SUB_MONO
;
1788 modulator
->txsubchans
|= V4L2_TUNER_SUB_RDS
;
1790 mutex_unlock(&core
->lock
);
1795 static int wl1273_fm_vidioc_log_status(struct file
*file
, void *priv
)
1797 struct wl1273_device
*radio
= video_get_drvdata(video_devdata(file
));
1798 struct wl1273_core
*core
= radio
->core
;
1799 struct device
*dev
= radio
->dev
;
1803 dev_info(dev
, DRIVER_DESC
);
1805 if (core
->mode
== WL1273_MODE_OFF
) {
1806 dev_info(dev
, "Mode: Off\n");
1810 if (core
->mode
== WL1273_MODE_SUSPENDED
) {
1811 dev_info(dev
, "Mode: Suspended\n");
1815 r
= core
->read(core
, WL1273_ASIC_ID_GET
, &val
);
1817 dev_err(dev
, "%s: Get ASIC_ID fails.\n", __func__
);
1819 dev_info(dev
, "ASIC_ID: 0x%04x\n", val
);
1821 r
= core
->read(core
, WL1273_ASIC_VER_GET
, &val
);
1823 dev_err(dev
, "%s: Get ASIC_VER fails.\n", __func__
);
1825 dev_info(dev
, "ASIC Version: 0x%04x\n", val
);
1827 r
= core
->read(core
, WL1273_FIRM_VER_GET
, &val
);
1829 dev_err(dev
, "%s: Get FIRM_VER fails.\n", __func__
);
1831 dev_info(dev
, "FW version: %d(0x%04x)\n", val
, val
);
1833 r
= core
->read(core
, WL1273_BAND_SET
, &val
);
1835 dev_err(dev
, "%s: Get BAND fails.\n", __func__
);
1837 dev_info(dev
, "BAND: %d\n", val
);
1839 if (core
->mode
== WL1273_MODE_TX
) {
1840 r
= core
->read(core
, WL1273_PUPD_SET
, &val
);
1842 dev_err(dev
, "%s: Get PUPD fails.\n", __func__
);
1844 dev_info(dev
, "PUPD: 0x%04x\n", val
);
1846 r
= core
->read(core
, WL1273_CHANL_SET
, &val
);
1848 dev_err(dev
, "%s: Get CHANL fails.\n", __func__
);
1850 dev_info(dev
, "Tx frequency: %dkHz\n", val
*10);
1851 } else if (core
->mode
== WL1273_MODE_RX
) {
1852 int bf
= radio
->rangelow
;
1854 r
= core
->read(core
, WL1273_FREQ_SET
, &val
);
1856 dev_err(dev
, "%s: Get FREQ fails.\n", __func__
);
1858 dev_info(dev
, "RX Frequency: %dkHz\n", bf
+ val
*50);
1860 r
= core
->read(core
, WL1273_MOST_MODE_SET
, &val
);
1862 dev_err(dev
, "%s: Get MOST_MODE fails.\n",
1865 dev_info(dev
, "MOST_MODE: Stereo according to blend\n");
1867 dev_info(dev
, "MOST_MODE: Force mono output\n");
1869 dev_info(dev
, "MOST_MODE: Unexpected value: %d\n", val
);
1871 r
= core
->read(core
, WL1273_MOST_BLEND_SET
, &val
);
1873 dev_err(dev
, "%s: Get MOST_BLEND fails.\n", __func__
);
1876 "MOST_BLEND: Switched blend & hysteresis.\n");
1878 dev_info(dev
, "MOST_BLEND: Soft blend.\n");
1880 dev_info(dev
, "MOST_BLEND: Unexpected val: %d\n", val
);
1882 r
= core
->read(core
, WL1273_STEREO_GET
, &val
);
1884 dev_err(dev
, "%s: Get STEREO fails.\n", __func__
);
1886 dev_info(dev
, "STEREO: Not detected\n");
1888 dev_info(dev
, "STEREO: Detected\n");
1890 dev_info(dev
, "STEREO: Unexpected value: %d\n", val
);
1892 r
= core
->read(core
, WL1273_RSSI_LVL_GET
, &val
);
1894 dev_err(dev
, "%s: Get RSSI_LVL fails.\n", __func__
);
1896 dev_info(dev
, "RX signal strength: %d\n", (s16
) val
);
1898 r
= core
->read(core
, WL1273_POWER_SET
, &val
);
1900 dev_err(dev
, "%s: Get POWER fails.\n", __func__
);
1902 dev_info(dev
, "POWER: 0x%04x\n", val
);
1904 r
= core
->read(core
, WL1273_INT_MASK_SET
, &val
);
1906 dev_err(dev
, "%s: Get INT_MASK fails.\n", __func__
);
1908 dev_info(dev
, "INT_MASK: 0x%04x\n", val
);
1910 r
= core
->read(core
, WL1273_RDS_SYNC_GET
, &val
);
1912 dev_err(dev
, "%s: Get RDS_SYNC fails.\n",
1915 dev_info(dev
, "RDS_SYNC: Not synchronized\n");
1918 dev_info(dev
, "RDS_SYNC: Synchronized\n");
1920 dev_info(dev
, "RDS_SYNC: Unexpected value: %d\n", val
);
1922 r
= core
->read(core
, WL1273_I2S_MODE_CONFIG_SET
, &val
);
1924 dev_err(dev
, "%s: Get I2S_MODE_CONFIG fails.\n",
1927 dev_info(dev
, "I2S_MODE_CONFIG: 0x%04x\n", val
);
1929 r
= core
->read(core
, WL1273_VOLUME_SET
, &val
);
1931 dev_err(dev
, "%s: Get VOLUME fails.\n", __func__
);
1933 dev_info(dev
, "VOLUME: 0x%04x\n", val
);
1939 static void wl1273_vdev_release(struct video_device
*dev
)
1943 static const struct v4l2_ctrl_ops wl1273_ctrl_ops
= {
1944 .s_ctrl
= wl1273_fm_vidioc_s_ctrl
,
1945 .g_volatile_ctrl
= wl1273_fm_g_volatile_ctrl
,
1948 static const struct v4l2_ioctl_ops wl1273_ioctl_ops
= {
1949 .vidioc_querycap
= wl1273_fm_vidioc_querycap
,
1950 .vidioc_g_input
= wl1273_fm_vidioc_g_input
,
1951 .vidioc_s_input
= wl1273_fm_vidioc_s_input
,
1952 .vidioc_g_audio
= wl1273_fm_vidioc_g_audio
,
1953 .vidioc_s_audio
= wl1273_fm_vidioc_s_audio
,
1954 .vidioc_g_tuner
= wl1273_fm_vidioc_g_tuner
,
1955 .vidioc_s_tuner
= wl1273_fm_vidioc_s_tuner
,
1956 .vidioc_g_frequency
= wl1273_fm_vidioc_g_frequency
,
1957 .vidioc_s_frequency
= wl1273_fm_vidioc_s_frequency
,
1958 .vidioc_s_hw_freq_seek
= wl1273_fm_vidioc_s_hw_freq_seek
,
1959 .vidioc_g_modulator
= wl1273_fm_vidioc_g_modulator
,
1960 .vidioc_s_modulator
= wl1273_fm_vidioc_s_modulator
,
1961 .vidioc_log_status
= wl1273_fm_vidioc_log_status
,
1964 static struct video_device wl1273_viddev_template
= {
1965 .fops
= &wl1273_fops
,
1966 .ioctl_ops
= &wl1273_ioctl_ops
,
1967 .name
= WL1273_FM_DRIVER_NAME
,
1968 .release
= wl1273_vdev_release
,
1971 static int wl1273_fm_radio_remove(struct platform_device
*pdev
)
1973 struct wl1273_device
*radio
= platform_get_drvdata(pdev
);
1974 struct wl1273_core
*core
= radio
->core
;
1976 dev_info(&pdev
->dev
, "%s.\n", __func__
);
1978 free_irq(core
->client
->irq
, radio
);
1979 core
->pdata
->free_resources();
1981 v4l2_ctrl_handler_free(&radio
->ctrl_handler
);
1982 video_unregister_device(&radio
->videodev
);
1983 v4l2_device_unregister(&radio
->v4l2dev
);
1984 kfree(radio
->buffer
);
1985 kfree(radio
->write_buf
);
1991 static int __devinit
wl1273_fm_radio_probe(struct platform_device
*pdev
)
1993 struct wl1273_core
**core
= mfd_get_data(pdev
);
1994 struct wl1273_device
*radio
;
1995 struct v4l2_ctrl
*ctrl
;
1998 pr_debug("%s\n", __func__
);
2001 dev_err(&pdev
->dev
, "No platform data.\n");
2006 radio
= kzalloc(sizeof(*radio
), GFP_KERNEL
);
2012 /* RDS buffer allocation */
2013 radio
->buf_size
= rds_buf
* RDS_BLOCK_SIZE
;
2014 radio
->buffer
= kmalloc(radio
->buf_size
, GFP_KERNEL
);
2015 if (!radio
->buffer
) {
2016 pr_err("Cannot allocate memory for RDS buffer.\n");
2021 radio
->core
= *core
;
2022 radio
->irq_flags
= WL1273_IRQ_MASK
;
2023 radio
->dev
= &radio
->core
->client
->dev
;
2024 radio
->rds_on
= false;
2025 radio
->core
->mode
= WL1273_MODE_OFF
;
2026 radio
->tx_power
= 118;
2027 radio
->core
->audio_mode
= WL1273_AUDIO_ANALOG
;
2028 radio
->band
= WL1273_BAND_OTHER
;
2029 radio
->core
->i2s_mode
= WL1273_I2S_DEF_MODE
;
2030 radio
->core
->channel_number
= 2;
2031 radio
->core
->volume
= WL1273_DEFAULT_VOLUME
;
2032 radio
->rx_frequency
= WL1273_BAND_OTHER_LOW
;
2033 radio
->tx_frequency
= WL1273_BAND_OTHER_HIGH
;
2034 radio
->rangelow
= WL1273_BAND_OTHER_LOW
;
2035 radio
->rangehigh
= WL1273_BAND_OTHER_HIGH
;
2036 radio
->stereo
= true;
2037 radio
->bus_type
= "I2C";
2039 if (radio
->core
->pdata
->request_resources
) {
2040 r
= radio
->core
->pdata
->request_resources(radio
->core
->client
);
2042 dev_err(radio
->dev
, WL1273_FM_DRIVER_NAME
2043 ": Cannot get platform data\n");
2047 dev_dbg(radio
->dev
, "irq: %d\n", radio
->core
->client
->irq
);
2049 r
= request_threaded_irq(radio
->core
->client
->irq
, NULL
,
2050 wl1273_fm_irq_thread_handler
,
2051 IRQF_ONESHOT
| IRQF_TRIGGER_FALLING
,
2052 "wl1273-fm", radio
);
2054 dev_err(radio
->dev
, WL1273_FM_DRIVER_NAME
2055 ": Unable to register IRQ handler: %d\n", r
);
2056 goto err_request_irq
;
2059 dev_err(radio
->dev
, WL1273_FM_DRIVER_NAME
": Core WL1273 IRQ"
2065 init_completion(&radio
->busy
);
2066 init_waitqueue_head(&radio
->read_queue
);
2068 radio
->write_buf
= kmalloc(256, GFP_KERNEL
);
2069 if (!radio
->write_buf
) {
2074 radio
->dev
= &pdev
->dev
;
2075 radio
->v4l2dev
.ctrl_handler
= &radio
->ctrl_handler
;
2076 radio
->rds_users
= 0;
2078 r
= v4l2_device_register(&pdev
->dev
, &radio
->v4l2dev
);
2080 dev_err(&pdev
->dev
, "Cannot register v4l2_device.\n");
2081 goto device_register_err
;
2084 /* V4L2 configuration */
2085 memcpy(&radio
->videodev
, &wl1273_viddev_template
,
2086 sizeof(wl1273_viddev_template
));
2088 radio
->videodev
.v4l2_dev
= &radio
->v4l2dev
;
2090 v4l2_ctrl_handler_init(&radio
->ctrl_handler
, 6);
2092 /* add in ascending ID order */
2093 v4l2_ctrl_new_std(&radio
->ctrl_handler
, &wl1273_ctrl_ops
,
2094 V4L2_CID_AUDIO_VOLUME
, 0, WL1273_MAX_VOLUME
, 1,
2095 WL1273_DEFAULT_VOLUME
);
2097 v4l2_ctrl_new_std(&radio
->ctrl_handler
, &wl1273_ctrl_ops
,
2098 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 1);
2100 v4l2_ctrl_new_std_menu(&radio
->ctrl_handler
, &wl1273_ctrl_ops
,
2101 V4L2_CID_TUNE_PREEMPHASIS
,
2102 V4L2_PREEMPHASIS_75_uS
, 0x03,
2103 V4L2_PREEMPHASIS_50_uS
);
2105 v4l2_ctrl_new_std(&radio
->ctrl_handler
, &wl1273_ctrl_ops
,
2106 V4L2_CID_TUNE_POWER_LEVEL
, 91, 122, 1, 118);
2108 ctrl
= v4l2_ctrl_new_std(&radio
->ctrl_handler
, &wl1273_ctrl_ops
,
2109 V4L2_CID_TUNE_ANTENNA_CAPACITOR
,
2112 ctrl
->is_volatile
= 1;
2114 if (radio
->ctrl_handler
.error
) {
2115 r
= radio
->ctrl_handler
.error
;
2116 dev_err(&pdev
->dev
, "Ctrl handler error: %d\n", r
);
2117 goto handler_init_err
;
2120 video_set_drvdata(&radio
->videodev
, radio
);
2121 platform_set_drvdata(pdev
, radio
);
2123 /* register video device */
2124 r
= video_register_device(&radio
->videodev
, VFL_TYPE_RADIO
, radio_nr
);
2126 dev_err(&pdev
->dev
, WL1273_FM_DRIVER_NAME
2127 ": Could not register video device\n");
2128 goto handler_init_err
;
2134 v4l2_ctrl_handler_free(&radio
->ctrl_handler
);
2135 v4l2_device_unregister(&radio
->v4l2dev
);
2136 device_register_err
:
2137 kfree(radio
->write_buf
);
2139 free_irq(radio
->core
->client
->irq
, radio
);
2141 radio
->core
->pdata
->free_resources();
2143 kfree(radio
->buffer
);
2150 MODULE_ALIAS("platform:wl1273_fm_radio");
2152 static struct platform_driver wl1273_fm_radio_driver
= {
2153 .probe
= wl1273_fm_radio_probe
,
2154 .remove
= __devexit_p(wl1273_fm_radio_remove
),
2156 .name
= "wl1273_fm_radio",
2157 .owner
= THIS_MODULE
,
2161 static int __init
wl1273_fm_module_init(void)
2163 pr_info("%s\n", __func__
);
2164 return platform_driver_register(&wl1273_fm_radio_driver
);
2166 module_init(wl1273_fm_module_init
);
2168 static void __exit
wl1273_fm_module_exit(void)
2170 platform_driver_unregister(&wl1273_fm_radio_driver
);
2171 pr_info(DRIVER_DESC
", Exiting.\n");
2173 module_exit(wl1273_fm_module_exit
);
2175 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
2176 MODULE_DESCRIPTION(DRIVER_DESC
);
2177 MODULE_LICENSE("GPL");