2 * Driver for the Texas Instruments WL1273 FM radio.
4 * Copyright (C) 2010 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;
70 struct delayed_work work;
72 wait_queue_head_t read_queue;
73 struct mutex lock; /* for serializing fm radio operations */
74 struct completion busy;
76 unsigned char *buffer;
77 unsigned int buf_size;
78 unsigned int rd_index;
79 unsigned int wr_index;
81 /* Selected interrupts */
85 struct v4l2_ctrl_handler ctrl_handler;
86 struct v4l2_device v4l2dev;
87 struct video_device videodev;
89 struct wl1273_core *core;
92 unsigned int rds_users;
95 #define WL1273_IRQ_MASK (WL1273_FR_EVENT | \
99 * static unsigned int rds_buf - the number of RDS buffer blocks used.
101 * The default number is 100.
103 static unsigned int rds_buf = 100;
104 module_param(rds_buf, uint, 0);
105 MODULE_PARM_DESC(rds_buf, "Number of RDS buffer entries. Default = 100");
107 static int wl1273_fm_read_reg(struct wl1273_core *core, u8 reg, u16 *value)
109 struct i2c_client *client = core->client;
113 r = i2c_smbus_read_i2c_block_data(client, reg, sizeof(b), b);
115 dev_err(&client->dev, "%s: Read: %d fails.\n", __func__, reg);
119 *value = (u16)b[0] << 8 | b[1];
124 static int wl1273_fm_write_cmd(struct wl1273_core *core, u8 cmd, u16 param)
126 struct i2c_client *client = core->client;
127 u8 buf[] = { (param >> 8) & 0xff, param & 0xff };
130 r = i2c_smbus_write_i2c_block_data(client, cmd, sizeof(buf), buf);
132 dev_err(&client->dev, "%s: Cmd: %d fails.\n", __func__, cmd);
139 static int wl1273_fm_write_data(struct wl1273_core *core, u8 *data, u16 len)
141 struct i2c_client *client = core->client;
145 msg.addr = client->addr;
150 r = i2c_transfer(client->adapter, &msg, 1);
152 dev_err(&client->dev, "%s: write error.\n", __func__);
159 static int wl1273_fm_write_fw(struct wl1273_core *core,
162 struct i2c_client *client = core->client;
166 msg.addr = client->addr;
169 for (i = 0; i <= len; i++) {
174 dev_dbg(&client->dev, "%s:len[%d]: %d\n", __func__, i, msg.len);
176 r = i2c_transfer(client->adapter, &msg, 1);
177 if (r < 0 && i < len + 1)
181 dev_dbg(&client->dev, "%s: i: %d\n", __func__, i);
182 dev_dbg(&client->dev, "%s: len + 1: %d\n", __func__, len + 1);
184 /* Last transfer always fails. */
185 if (i == len || r == 1)
192 * wl1273_fm_set_audio() - Set audio mode.
193 * @core: A pointer to the device struct.
194 * @new_mode: The new audio mode.
196 * Audio modes are WL1273_AUDIO_DIGITAL and WL1273_AUDIO_ANALOG.
198 static int wl1273_fm_set_audio(struct wl1273_core *core, unsigned int new_mode)
202 if (core->mode == WL1273_MODE_OFF ||
203 core->mode == WL1273_MODE_SUSPENDED)
206 if (core->mode == WL1273_MODE_RX && new_mode == WL1273_AUDIO_DIGITAL) {
207 r = wl1273_fm_write_cmd(core, WL1273_PCM_MODE_SET,
208 WL1273_PCM_DEF_MODE);
212 r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
217 r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
218 WL1273_AUDIO_ENABLE_I2S);
222 } else if (core->mode == WL1273_MODE_RX &&
223 new_mode == WL1273_AUDIO_ANALOG) {
224 r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
225 WL1273_AUDIO_ENABLE_ANALOG);
229 } else if (core->mode == WL1273_MODE_TX &&
230 new_mode == WL1273_AUDIO_DIGITAL) {
231 r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
236 r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
237 WL1273_AUDIO_IO_SET_I2S);
241 } else if (core->mode == WL1273_MODE_TX &&
242 new_mode == WL1273_AUDIO_ANALOG) {
243 r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
244 WL1273_AUDIO_IO_SET_ANALOG);
249 core->audio_mode = new_mode;
255 * wl1273_fm_set_volume() - Set volume.
256 * @core: A pointer to the device struct.
257 * @volume: The new volume value.
259 static int wl1273_fm_set_volume(struct wl1273_core *core, unsigned int volume)
264 if (volume > WL1273_MAX_VOLUME)
267 if (core->volume == volume)
271 r = wl1273_fm_read_reg(core, WL1273_VOLUME_SET, &val);
275 core->volume = volume;
279 #define WL1273_FIFO_HAS_DATA(status) (1 << 5 & status)
280 #define WL1273_RDS_CORRECTABLE_ERROR (1 << 3)
281 #define WL1273_RDS_UNCORRECTABLE_ERROR (1 << 4)
283 static int wl1273_fm_rds(struct wl1273_device *radio)
285 struct wl1273_core *core = radio->core;
286 struct i2c_client *client = core->client;
288 u8 b0 = WL1273_RDS_DATA_GET, status;
289 struct v4l2_rds_data rds = { 0, 0, 0 };
290 struct i2c_msg msg[] = {
292 .addr = client->addr,
298 .addr = client->addr,
306 if (core->mode != WL1273_MODE_RX)
309 r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val);
313 if ((val & 0x01) == 0) {
314 /* RDS decoder not synchronized */
318 /* copy all four RDS blocks to internal buffer */
320 r = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
321 if (r != ARRAY_SIZE(msg)) {
322 dev_err(radio->dev, WL1273_FM_DRIVER_NAME
323 ": %s: read_rds error r == %i)\n",
329 if (!WL1273_FIFO_HAS_DATA(status))
332 /* copy bits 0-2 (the block ID) to bits 3-5 */
333 rds.block = V4L2_RDS_BLOCK_MSK & status;
334 rds.block |= rds.block << 3;
336 /* copy the error bits to standard positions */
337 if (WL1273_RDS_UNCORRECTABLE_ERROR & status) {
338 rds.block |= V4L2_RDS_BLOCK_ERROR;
339 rds.block &= ~V4L2_RDS_BLOCK_CORRECTED;
340 } else if (WL1273_RDS_CORRECTABLE_ERROR & status) {
341 rds.block &= ~V4L2_RDS_BLOCK_ERROR;
342 rds.block |= V4L2_RDS_BLOCK_CORRECTED;
345 /* copy RDS block to internal buffer */
346 memcpy(&radio->buffer[radio->wr_index], &rds, RDS_BLOCK_SIZE);
347 radio->wr_index += 3;
349 /* wrap write pointer */
350 if (radio->wr_index >= radio->buf_size)
353 /* check for overflow & start over */
354 if (radio->wr_index == radio->rd_index) {
355 dev_dbg(radio->dev, "RDS OVERFLOW");
361 } while (WL1273_FIFO_HAS_DATA(status));
363 /* wake up read queue */
364 if (radio->wr_index != radio->rd_index)
365 wake_up_interruptible(&radio->read_queue);
370 static irqreturn_t wl1273_fm_irq_thread_handler(int irq, void *dev_id)
372 struct wl1273_device *radio = dev_id;
373 struct wl1273_core *core = radio->core;
377 r = wl1273_fm_read_reg(core, WL1273_FLAG_GET, &flags);
381 if (flags & WL1273_BL_EVENT) {
382 radio->irq_received = flags;
383 dev_dbg(radio->dev, "IRQ: BL\n");
386 if (flags & WL1273_RDS_EVENT) {
389 wl1273_fm_rds(radio);
392 if (flags & WL1273_BBLK_EVENT)
393 dev_dbg(radio->dev, "IRQ: BBLK\n");
395 if (flags & WL1273_LSYNC_EVENT)
396 dev_dbg(radio->dev, "IRQ: LSYNC\n");
398 if (flags & WL1273_LEV_EVENT) {
401 r = wl1273_fm_read_reg(core, WL1273_RSSI_LVL_GET, &level);
406 dev_dbg(radio->dev, "IRQ: LEV: 0x%x04\n", level);
409 if (flags & WL1273_IFFR_EVENT)
410 dev_dbg(radio->dev, "IRQ: IFFR\n");
412 if (flags & WL1273_PI_EVENT)
413 dev_dbg(radio->dev, "IRQ: PI\n");
415 if (flags & WL1273_PD_EVENT)
416 dev_dbg(radio->dev, "IRQ: PD\n");
418 if (flags & WL1273_STIC_EVENT)
419 dev_dbg(radio->dev, "IRQ: STIC\n");
421 if (flags & WL1273_MAL_EVENT)
422 dev_dbg(radio->dev, "IRQ: MAL\n");
424 if (flags & WL1273_POW_ENB_EVENT) {
425 complete(&radio->busy);
426 dev_dbg(radio->dev, "NOT BUSY\n");
427 dev_dbg(radio->dev, "IRQ: POW_ENB\n");
430 if (flags & WL1273_SCAN_OVER_EVENT)
431 dev_dbg(radio->dev, "IRQ: SCAN_OVER\n");
433 if (flags & WL1273_ERROR_EVENT)
434 dev_dbg(radio->dev, "IRQ: ERROR\n");
436 if (flags & WL1273_FR_EVENT) {
439 dev_dbg(radio->dev, "IRQ: FR:\n");
441 if (core->mode == WL1273_MODE_RX) {
442 r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET,
443 TUNER_MODE_STOP_SEARCH);
446 "%s: TUNER_MODE_SET fails: %d\n",
451 r = wl1273_fm_read_reg(core, WL1273_FREQ_SET, &freq);
455 if (radio->band == WL1273_BAND_JAPAN)
456 radio->rx_frequency = WL1273_BAND_JAPAN_LOW +
459 radio->rx_frequency = WL1273_BAND_OTHER_LOW +
462 * The driver works better with this msleep,
463 * the documentation doesn't mention it.
465 usleep_range(10000, 15000);
467 dev_dbg(radio->dev, "%dkHz\n", radio->rx_frequency);
470 r = wl1273_fm_read_reg(core, WL1273_CHANL_SET, &freq);
474 dev_dbg(radio->dev, "%dkHz\n", freq);
476 dev_dbg(radio->dev, "%s: NOT BUSY\n", __func__);
480 wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET,
482 complete(&radio->busy);
487 static int wl1273_fm_set_tx_freq(struct wl1273_device *radio, unsigned int freq)
489 struct wl1273_core *core = radio->core;
492 if (freq < WL1273_BAND_TX_LOW) {
494 "Frequency out of range: %d < %d\n", freq,
499 if (freq > WL1273_BAND_TX_HIGH) {
501 "Frequency out of range: %d > %d\n", freq,
502 WL1273_BAND_TX_HIGH);
507 * The driver works better with this sleep,
508 * the documentation doesn't mention it.
510 usleep_range(5000, 10000);
512 dev_dbg(radio->dev, "%s: freq: %d kHz\n", __func__, freq);
514 /* Set the current tx channel */
515 r = wl1273_fm_write_cmd(core, WL1273_CHANL_SET, freq / 10);
519 INIT_COMPLETION(radio->busy);
521 /* wait for the FR IRQ */
522 r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
526 dev_dbg(radio->dev, "WL1273_CHANL_SET: %d\n", r);
528 /* Enable the output power */
529 r = wl1273_fm_write_cmd(core, WL1273_POWER_ENB_SET, 1);
533 INIT_COMPLETION(radio->busy);
535 /* wait for the POWER_ENB IRQ */
536 r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
540 radio->tx_frequency = freq;
541 dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %d\n", r);
546 static int wl1273_fm_set_rx_freq(struct wl1273_device *radio, unsigned int freq)
548 struct wl1273_core *core = radio->core;
551 if (freq < radio->rangelow) {
553 "Frequency out of range: %d < %d\n", freq,
559 if (freq > radio->rangehigh) {
561 "Frequency out of range: %d > %d\n", freq,
567 dev_dbg(radio->dev, "%s: %dkHz\n", __func__, freq);
569 wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, radio->irq_flags);
571 if (radio->band == WL1273_BAND_JAPAN)
572 f = (freq - WL1273_BAND_JAPAN_LOW) / 50;
574 f = (freq - WL1273_BAND_OTHER_LOW) / 50;
576 r = wl1273_fm_write_cmd(core, WL1273_FREQ_SET, f);
578 dev_err(radio->dev, "FREQ_SET fails\n");
582 r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET);
584 dev_err(radio->dev, "TUNER_MODE_SET fails\n");
588 INIT_COMPLETION(radio->busy);
590 r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
592 dev_err(radio->dev, "%s: TIMEOUT\n", __func__);
598 radio->rx_frequency = freq;
604 static int wl1273_fm_get_freq(struct wl1273_device *radio)
606 struct wl1273_core *core = radio->core;
611 if (core->mode == WL1273_MODE_RX) {
612 r = wl1273_fm_read_reg(core, WL1273_FREQ_SET, &f);
616 dev_dbg(radio->dev, "Freq get: 0x%04x\n", f);
617 if (radio->band == WL1273_BAND_JAPAN)
618 freq = WL1273_BAND_JAPAN_LOW + 50 * f;
620 freq = WL1273_BAND_OTHER_LOW + 50 * f;
622 r = wl1273_fm_read_reg(core, WL1273_CHANL_SET, &f);
633 * wl1273_fm_upload_firmware_patch() - Upload the firmware.
634 * @radio: A pointer to the device struct.
636 * The firmware file consists of arrays of bytes where the first byte
637 * gives the array length. The first byte in the file gives the
638 * number of these arrays.
640 static int wl1273_fm_upload_firmware_patch(struct wl1273_device *radio)
642 struct wl1273_core *core = radio->core;
643 unsigned int packet_num;
644 const struct firmware *fw_p;
645 const char *fw_name = "radio-wl1273-fw.bin";
646 struct device *dev = radio->dev;
650 dev_dbg(dev, "%s:\n", __func__);
653 * Uploading the firmware patch is not always necessary,
654 * so we only print an info message.
656 if (request_firmware(&fw_p, fw_name, dev)) {
657 dev_info(dev, "%s - %s not found\n", __func__, fw_name);
662 ptr = (__u8 *) fw_p->data;
664 dev_dbg(dev, "%s: packets: %d\n", __func__, packet_num);
666 r = wl1273_fm_write_fw(core, ptr + 1, packet_num);
668 dev_err(dev, "FW upload error: %d\n", r);
672 /* ignore possible error here */
673 wl1273_fm_write_cmd(core, WL1273_RESET, 0);
675 dev_dbg(dev, "%s - download OK, r: %d\n", __func__, r);
677 release_firmware(fw_p);
681 static int wl1273_fm_stop(struct wl1273_device *radio)
683 struct wl1273_core *core = radio->core;
685 if (core->mode == WL1273_MODE_RX) {
686 int r = wl1273_fm_write_cmd(core, WL1273_POWER_SET,
687 WL1273_POWER_SET_OFF);
689 dev_err(radio->dev, "%s: POWER_SET fails: %d\n",
691 } else if (core->mode == WL1273_MODE_TX) {
692 int r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
693 WL1273_PUPD_SET_OFF);
696 "%s: PUPD_SET fails: %d\n", __func__, r);
699 if (core->pdata->disable) {
700 core->pdata->disable();
701 dev_dbg(radio->dev, "Back to reset\n");
707 static int wl1273_fm_start(struct wl1273_device *radio, int new_mode)
709 struct wl1273_core *core = radio->core;
710 struct wl1273_fm_platform_data *pdata = core->pdata;
711 struct device *dev = radio->dev;
714 if (pdata->enable && core->mode == WL1273_MODE_OFF) {
715 dev_dbg(radio->dev, "Out of reset\n");
721 if (new_mode == WL1273_MODE_RX) {
722 u16 val = WL1273_POWER_SET_FM;
725 val |= WL1273_POWER_SET_RDS;
727 /* If this fails try again */
728 r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, val);
732 r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, val);
734 dev_err(dev, "%s: POWER_SET fails\n", __func__);
739 /* rds buffer configuration */
743 } else if (new_mode == WL1273_MODE_TX) {
744 /* If this fails try again once */
745 r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
749 r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
752 dev_err(dev, "%s: PUPD_SET fails\n", __func__);
758 r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 1);
760 r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 0);
762 dev_warn(dev, "%s: Illegal mode.\n", __func__);
765 if (core->mode == WL1273_MODE_OFF) {
766 r = wl1273_fm_upload_firmware_patch(radio);
768 dev_warn(dev, "Firmware upload failed.\n");
771 * Sometimes the chip is in a wrong power state at this point.
772 * So we set the power once again.
774 if (new_mode == WL1273_MODE_RX) {
775 u16 val = WL1273_POWER_SET_FM;
778 val |= WL1273_POWER_SET_RDS;
780 r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, val);
782 dev_err(dev, "%s: POWER_SET fails\n", __func__);
785 } else if (new_mode == WL1273_MODE_TX) {
786 r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
789 dev_err(dev, "%s: PUPD_SET fails\n", __func__);
800 dev_dbg(dev, "%s: return: %d\n", __func__, r);
804 static int wl1273_fm_suspend(struct wl1273_device *radio)
806 struct wl1273_core *core = radio->core;
809 /* Cannot go from OFF to SUSPENDED */
810 if (core->mode == WL1273_MODE_RX)
811 r = wl1273_fm_write_cmd(core, WL1273_POWER_SET,
812 WL1273_POWER_SET_RETENTION);
813 else if (core->mode == WL1273_MODE_TX)
814 r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
815 WL1273_PUPD_SET_RETENTION);
820 dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r);
828 static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode)
830 struct wl1273_core *core = radio->core;
831 struct device *dev = radio->dev;
835 dev_dbg(dev, "%s\n", __func__);
836 dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden);
838 old_mode = core->mode;
839 if (mode & radio->forbidden) {
847 r = wl1273_fm_start(radio, mode);
849 dev_err(dev, "%s: Cannot start.\n", __func__);
850 wl1273_fm_stop(radio);
855 r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET,
858 dev_err(dev, "INT_MASK_SET fails.\n");
862 /* remember previous settings */
863 if (mode == WL1273_MODE_RX) {
864 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
866 dev_err(dev, "set freq fails: %d.\n", r);
870 r = core->set_volume(core, core->volume);
872 dev_err(dev, "set volume fails: %d.\n", r);
876 dev_dbg(dev, "%s: Set vol: %d.\n", __func__,
879 r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency);
881 dev_err(dev, "set freq fails: %d.\n", r);
886 dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__);
888 r = core->set_audio(core, core->audio_mode);
890 dev_err(dev, "Cannot set audio mode.\n");
893 case WL1273_MODE_OFF:
894 r = wl1273_fm_stop(radio);
896 dev_err(dev, "%s: Off fails: %d\n", __func__, r);
898 core->mode = WL1273_MODE_OFF;
902 case WL1273_MODE_SUSPENDED:
903 r = wl1273_fm_suspend(radio);
905 dev_err(dev, "%s: Suspend fails: %d\n", __func__, r);
907 core->mode = WL1273_MODE_SUSPENDED;
912 dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode);
918 core->mode = old_mode;
923 static int wl1273_fm_set_seek(struct wl1273_device *radio,
924 unsigned int wrap_around,
925 unsigned int seek_upward,
928 struct wl1273_core *core = radio->core;
930 unsigned int dir = (seek_upward == 0) ? 0 : 1;
933 f = radio->rx_frequency;
934 dev_dbg(radio->dev, "rx_frequency: %d\n", f);
936 if (dir && f + radio->spacing <= radio->rangehigh)
937 r = wl1273_fm_set_rx_freq(radio, f + radio->spacing);
938 else if (dir && wrap_around)
939 r = wl1273_fm_set_rx_freq(radio, radio->rangelow);
940 else if (f - radio->spacing >= radio->rangelow)
941 r = wl1273_fm_set_rx_freq(radio, f - radio->spacing);
942 else if (wrap_around)
943 r = wl1273_fm_set_rx_freq(radio, radio->rangehigh);
948 if (level < SCHAR_MIN || level > SCHAR_MAX)
951 INIT_COMPLETION(radio->busy);
952 dev_dbg(radio->dev, "%s: BUSY\n", __func__);
954 r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, radio->irq_flags);
958 dev_dbg(radio->dev, "%s\n", __func__);
960 r = wl1273_fm_write_cmd(core, WL1273_SEARCH_LVL_SET, level);
964 r = wl1273_fm_write_cmd(core, WL1273_SEARCH_DIR_SET, dir);
968 r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET,
969 TUNER_MODE_AUTO_SEEK);
973 wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
974 if (!(radio->irq_received & WL1273_BL_EVENT))
977 radio->irq_received &= ~WL1273_BL_EVENT;
983 dev_dbg(radio->dev, "Wrap around in HW seek.\n");
988 f = radio->rangehigh;
990 r = wl1273_fm_set_rx_freq(radio, f);
994 INIT_COMPLETION(radio->busy);
995 dev_dbg(radio->dev, "%s: BUSY\n", __func__);
997 r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET,
998 TUNER_MODE_AUTO_SEEK);
1002 wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
1004 dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r);
1009 * wl1273_fm_get_tx_ctune() - Get the TX tuning capacitor value.
1010 * @radio: A pointer to the device struct.
1012 static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio)
1014 struct wl1273_core *core = radio->core;
1015 struct device *dev = radio->dev;
1019 if (core->mode == WL1273_MODE_OFF ||
1020 core->mode == WL1273_MODE_SUSPENDED)
1023 r = wl1273_fm_read_reg(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
1025 dev_err(dev, "%s: read error: %d\n", __func__, r);
1034 * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
1035 * @radio: A pointer to the device struct.
1036 * @preemphasis: The new pre-amphasis value.
1038 * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
1039 * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
1041 static int wl1273_fm_set_preemphasis(struct wl1273_device *radio,
1042 unsigned int preemphasis)
1044 struct wl1273_core *core = radio->core;
1048 if (core->mode == WL1273_MODE_OFF ||
1049 core->mode == WL1273_MODE_SUSPENDED)
1052 mutex_lock(&core->lock);
1054 switch (preemphasis) {
1055 case V4L2_PREEMPHASIS_DISABLED:
1058 case V4L2_PREEMPHASIS_50_uS:
1061 case V4L2_PREEMPHASIS_75_uS:
1069 r = wl1273_fm_write_cmd(core, WL1273_PREMPH_SET, em);
1073 radio->preemphasis = preemphasis;
1076 mutex_unlock(&core->lock);
1080 static int wl1273_fm_rds_on(struct wl1273_device *radio)
1082 struct wl1273_core *core = radio->core;
1085 dev_dbg(radio->dev, "%s\n", __func__);
1089 r = wl1273_fm_write_cmd(core, WL1273_POWER_SET,
1090 WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
1094 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
1096 dev_err(radio->dev, "set freq fails: %d.\n", r);
1101 static int wl1273_fm_rds_off(struct wl1273_device *radio)
1103 struct wl1273_core *core = radio->core;
1109 radio->irq_flags &= ~WL1273_RDS_EVENT;
1111 r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, radio->irq_flags);
1115 /* stop rds reception */
1116 cancel_delayed_work(&radio->work);
1118 /* Service pending read */
1119 wake_up_interruptible(&radio->read_queue);
1121 dev_dbg(radio->dev, "%s\n", __func__);
1123 r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
1127 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
1129 dev_err(radio->dev, "set freq fails: %d.\n", r);
1131 dev_dbg(radio->dev, "%s: exiting...\n", __func__);
1136 static int wl1273_fm_set_rds(struct wl1273_device *radio, unsigned int new_mode)
1139 struct wl1273_core *core = radio->core;
1141 if (core->mode == WL1273_MODE_OFF ||
1142 core->mode == WL1273_MODE_SUSPENDED)
1145 if (new_mode == WL1273_RDS_RESET) {
1146 r = wl1273_fm_write_cmd(core, WL1273_RDS_CNTRL_SET, 1);
1150 if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) {
1151 r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 0);
1152 } else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) {
1153 r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 1);
1154 } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) {
1155 r = wl1273_fm_rds_off(radio);
1156 } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) {
1157 r = wl1273_fm_rds_on(radio);
1159 dev_err(radio->dev, "%s: Unknown mode: %d\n",
1160 __func__, new_mode);
1165 radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false;
1170 static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf,
1171 size_t count, loff_t *ppos)
1173 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1177 dev_dbg(radio->dev, "%s\n", __func__);
1179 if (radio->core->mode != WL1273_MODE_TX)
1182 if (radio->rds_users == 0) {
1183 dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1187 if (mutex_lock_interruptible(&radio->core->lock))
1190 * Multiple processes can open the device, but only
1191 * one gets to write to it.
1193 if (radio->owner && radio->owner != file) {
1197 radio->owner = file;
1205 wl1273_fm_write_cmd(radio->core, WL1273_RDS_CONFIG_DATA_SET, val);
1207 if (copy_from_user(radio->write_buf + 1, buf, val)) {
1212 dev_dbg(radio->dev, "Count: %d\n", val);
1213 dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf);
1215 radio->write_buf[0] = WL1273_RDS_DATA_SET;
1216 wl1273_fm_write_data(radio->core, radio->write_buf, val + 1);
1220 mutex_unlock(&radio->core->lock);
1225 static unsigned int wl1273_fm_fops_poll(struct file *file,
1226 struct poll_table_struct *pts)
1228 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1229 struct wl1273_core *core = radio->core;
1231 if (radio->owner && radio->owner != file)
1234 radio->owner = file;
1236 if (core->mode == WL1273_MODE_RX) {
1237 poll_wait(file, &radio->read_queue, pts);
1239 if (radio->rd_index != radio->wr_index)
1240 return POLLIN | POLLRDNORM;
1242 } else if (core->mode == WL1273_MODE_TX) {
1243 return POLLOUT | POLLWRNORM;
1249 static int wl1273_fm_fops_open(struct file *file)
1251 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1252 struct wl1273_core *core = radio->core;
1255 dev_dbg(radio->dev, "%s\n", __func__);
1257 if (core->mode == WL1273_MODE_RX && radio->rds_on &&
1258 !radio->rds_users) {
1259 dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode);
1261 if (mutex_lock_interruptible(&core->lock))
1264 radio->irq_flags |= WL1273_RDS_EVENT;
1266 r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET,
1269 mutex_unlock(&core->lock);
1275 mutex_unlock(&core->lock);
1281 static int wl1273_fm_fops_release(struct file *file)
1283 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1284 struct wl1273_core *core = radio->core;
1287 dev_dbg(radio->dev, "%s\n", __func__);
1289 if (radio->rds_users > 0) {
1291 if (radio->rds_users == 0) {
1292 if (mutex_lock_interruptible(&core->lock))
1295 radio->irq_flags &= ~WL1273_RDS_EVENT;
1297 if (core->mode == WL1273_MODE_RX) {
1298 r = wl1273_fm_write_cmd(core,
1299 WL1273_INT_MASK_SET,
1302 mutex_unlock(&core->lock);
1306 mutex_unlock(&core->lock);
1310 if (file == radio->owner)
1311 radio->owner = NULL;
1316 static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
1317 size_t count, loff_t *ppos)
1320 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1321 struct wl1273_core *core = radio->core;
1322 unsigned int block_count = 0;
1325 dev_dbg(radio->dev, "%s\n", __func__);
1327 if (radio->core->mode != WL1273_MODE_RX)
1330 if (radio->rds_users == 0) {
1331 dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1335 if (mutex_lock_interruptible(&core->lock))
1339 * Multiple processes can open the device, but only
1340 * one at a time gets read access.
1342 if (radio->owner && radio->owner != file) {
1346 radio->owner = file;
1348 r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val);
1350 dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__);
1352 } else if (val == 0) {
1353 dev_info(radio->dev, "RDS_SYNC: Not synchronized\n");
1358 /* block if no new data available */
1359 while (radio->wr_index == radio->rd_index) {
1360 if (file->f_flags & O_NONBLOCK) {
1365 dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__);
1366 if (wait_event_interruptible(radio->read_queue,
1368 radio->rd_index) < 0) {
1374 /* calculate block count from byte count */
1375 count /= RDS_BLOCK_SIZE;
1377 /* copy RDS blocks from the internal buffer and to user buffer */
1378 while (block_count < count) {
1379 if (radio->rd_index == radio->wr_index)
1382 /* always transfer complete RDS blocks */
1383 if (copy_to_user(buf, &radio->buffer[radio->rd_index],
1387 /* increment and wrap the read pointer */
1388 radio->rd_index += RDS_BLOCK_SIZE;
1389 if (radio->rd_index >= radio->buf_size)
1390 radio->rd_index = 0;
1392 /* increment counters */
1394 buf += RDS_BLOCK_SIZE;
1395 r += RDS_BLOCK_SIZE;
1399 dev_dbg(radio->dev, "%s: exit\n", __func__);
1400 mutex_unlock(&core->lock);
1405 static const struct v4l2_file_operations wl1273_fops = {
1406 .owner = THIS_MODULE,
1407 .read = wl1273_fm_fops_read,
1408 .write = wl1273_fm_fops_write,
1409 .poll = wl1273_fm_fops_poll,
1410 .unlocked_ioctl = video_ioctl2,
1411 .open = wl1273_fm_fops_open,
1412 .release = wl1273_fm_fops_release,
1415 static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
1416 struct v4l2_capability *capability)
1418 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1420 dev_dbg(radio->dev, "%s\n", __func__);
1422 strlcpy(capability->driver, WL1273_FM_DRIVER_NAME,
1423 sizeof(capability->driver));
1424 strlcpy(capability->card, "Texas Instruments Wl1273 FM Radio",
1425 sizeof(capability->card));
1426 strlcpy(capability->bus_info, radio->bus_type,
1427 sizeof(capability->bus_info));
1429 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1430 V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
1431 V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
1432 V4L2_CAP_RDS_OUTPUT;
1437 static int wl1273_fm_vidioc_g_input(struct file *file, void *priv,
1440 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1442 dev_dbg(radio->dev, "%s\n", __func__);
1449 static int wl1273_fm_vidioc_s_input(struct file *file, void *priv,
1452 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1454 dev_dbg(radio->dev, "%s\n", __func__);
1463 * wl1273_fm_set_tx_power() - Set the transmission power value.
1464 * @core: A pointer to the device struct.
1465 * @power: The new power value.
1467 static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power)
1471 if (radio->core->mode == WL1273_MODE_OFF ||
1472 radio->core->mode == WL1273_MODE_SUSPENDED)
1475 mutex_lock(&radio->core->lock);
1477 /* Convert the dBuV value to chip presentation */
1478 r = wl1273_fm_write_cmd(radio->core, WL1273_POWER_LEV_SET, 122 - power);
1482 radio->tx_power = power;
1485 mutex_unlock(&radio->core->lock);
1489 #define WL1273_SPACING_50kHz 1
1490 #define WL1273_SPACING_100kHz 2
1491 #define WL1273_SPACING_200kHz 4
1493 static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio,
1494 unsigned int spacing)
1499 r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET,
1500 WL1273_SPACING_100kHz);
1501 radio->spacing = 100;
1502 } else if (spacing - 50000 < 25000) {
1503 r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET,
1504 WL1273_SPACING_50kHz);
1505 radio->spacing = 50;
1506 } else if (spacing - 100000 < 50000) {
1507 r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET,
1508 WL1273_SPACING_100kHz);
1509 radio->spacing = 100;
1511 r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET,
1512 WL1273_SPACING_200kHz);
1513 radio->spacing = 200;
1519 static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1521 struct wl1273_device *radio = ctrl->priv;
1522 struct wl1273_core *core = radio->core;
1524 dev_dbg(radio->dev, "%s\n", __func__);
1526 if (mutex_lock_interruptible(&core->lock))
1530 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1531 ctrl->val = wl1273_fm_get_tx_ctune(radio);
1535 dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
1536 __func__, ctrl->id);
1540 mutex_unlock(&core->lock);
1545 #define WL1273_MUTE_SOFT_ENABLE (1 << 0)
1546 #define WL1273_MUTE_AC (1 << 1)
1547 #define WL1273_MUTE_HARD_LEFT (1 << 2)
1548 #define WL1273_MUTE_HARD_RIGHT (1 << 3)
1549 #define WL1273_MUTE_SOFT_FORCE (1 << 4)
1551 static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl)
1553 return container_of(ctrl->handler, struct wl1273_device, ctrl_handler);
1556 static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl)
1558 struct wl1273_device *radio = to_radio(ctrl);
1559 struct wl1273_core *core = radio->core;
1562 dev_dbg(radio->dev, "%s\n", __func__);
1565 case V4L2_CID_AUDIO_MUTE:
1566 if (mutex_lock_interruptible(&core->lock))
1569 if (core->mode == WL1273_MODE_RX && ctrl->val)
1570 r = wl1273_fm_write_cmd(core,
1571 WL1273_MUTE_STATUS_SET,
1572 WL1273_MUTE_HARD_LEFT |
1573 WL1273_MUTE_HARD_RIGHT);
1574 else if (core->mode == WL1273_MODE_RX)
1575 r = wl1273_fm_write_cmd(core,
1576 WL1273_MUTE_STATUS_SET, 0x0);
1577 else if (core->mode == WL1273_MODE_TX && ctrl->val)
1578 r = wl1273_fm_write_cmd(core, WL1273_MUTE, 1);
1579 else if (core->mode == WL1273_MODE_TX)
1580 r = wl1273_fm_write_cmd(core, WL1273_MUTE, 0);
1582 mutex_unlock(&core->lock);
1585 case V4L2_CID_AUDIO_VOLUME:
1587 r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF);
1589 r = core->set_volume(core, core->volume);
1592 case V4L2_CID_TUNE_PREEMPHASIS:
1593 r = wl1273_fm_set_preemphasis(radio, ctrl->val);
1596 case V4L2_CID_TUNE_POWER_LEVEL:
1597 r = wl1273_fm_set_tx_power(radio, ctrl->val);
1601 dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
1602 __func__, ctrl->id);
1606 dev_dbg(radio->dev, "%s\n", __func__);
1610 static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv,
1611 struct v4l2_audio *audio)
1613 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1615 dev_dbg(radio->dev, "%s\n", __func__);
1617 if (audio->index > 1)
1620 strlcpy(audio->name, "Radio", sizeof(audio->name));
1621 audio->capability = V4L2_AUDCAP_STEREO;
1626 static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
1627 struct v4l2_audio *audio)
1629 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1631 dev_dbg(radio->dev, "%s\n", __func__);
1633 if (audio->index != 0)
1639 #define WL1273_RDS_NOT_SYNCHRONIZED 0
1640 #define WL1273_RDS_SYNCHRONIZED 1
1642 static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv,
1643 struct v4l2_tuner *tuner)
1645 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1646 struct wl1273_core *core = radio->core;
1650 dev_dbg(radio->dev, "%s\n", __func__);
1652 if (tuner->index > 0)
1655 strlcpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name));
1656 tuner->type = V4L2_TUNER_RADIO;
1658 tuner->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
1659 tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
1661 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
1662 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
1665 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1667 tuner->audmode = V4L2_TUNER_MODE_MONO;
1669 if (core->mode != WL1273_MODE_RX)
1672 if (mutex_lock_interruptible(&core->lock))
1675 r = wl1273_fm_read_reg(core, WL1273_STEREO_GET, &val);
1680 tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
1682 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1684 r = wl1273_fm_read_reg(core, WL1273_RSSI_LVL_GET, &val);
1688 tuner->signal = (s16) val;
1689 dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);
1693 r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val);
1697 if (val == WL1273_RDS_SYNCHRONIZED)
1698 tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
1700 mutex_unlock(&core->lock);
1705 static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
1706 struct v4l2_tuner *tuner)
1708 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1709 struct wl1273_core *core = radio->core;
1712 dev_dbg(radio->dev, "%s\n", __func__);
1713 dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index);
1714 dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name);
1715 dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability);
1716 dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans);
1717 dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow);
1718 dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh);
1720 if (tuner->index > 0)
1723 if (mutex_lock_interruptible(&core->lock))
1726 r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1730 if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS)
1731 r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1733 r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1736 dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r);
1738 if (tuner->audmode == V4L2_TUNER_MODE_MONO) {
1739 r = wl1273_fm_write_cmd(core, WL1273_MOST_MODE_SET,
1742 dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1746 radio->stereo = false;
1747 } else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) {
1748 r = wl1273_fm_write_cmd(core, WL1273_MOST_MODE_SET,
1751 dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1755 radio->stereo = true;
1757 dev_err(radio->dev, "%s: tuner->audmode: %d\n",
1758 __func__, tuner->audmode);
1764 mutex_unlock(&core->lock);
1769 static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
1770 struct v4l2_frequency *freq)
1772 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1773 struct wl1273_core *core = radio->core;
1775 dev_dbg(radio->dev, "%s\n", __func__);
1777 if (mutex_lock_interruptible(&core->lock))
1780 freq->type = V4L2_TUNER_RADIO;
1781 freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio));
1783 mutex_unlock(&core->lock);
1788 static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
1789 struct v4l2_frequency *freq)
1791 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1792 struct wl1273_core *core = radio->core;
1795 dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency);
1797 if (freq->type != V4L2_TUNER_RADIO) {
1799 "freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
1803 if (mutex_lock_interruptible(&core->lock))
1806 if (core->mode == WL1273_MODE_RX) {
1807 dev_dbg(radio->dev, "freq: %d\n", freq->frequency);
1809 r = wl1273_fm_set_rx_freq(radio,
1810 WL1273_INV_FREQ(freq->frequency));
1812 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1813 ": set frequency failed with %d\n", r);
1815 r = wl1273_fm_set_tx_freq(radio,
1816 WL1273_INV_FREQ(freq->frequency));
1818 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1819 ": set frequency failed with %d\n", r);
1822 mutex_unlock(&core->lock);
1824 dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
1828 #define WL1273_DEFAULT_SEEK_LEVEL 7
1830 static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1831 struct v4l2_hw_freq_seek *seek)
1833 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1834 struct wl1273_core *core = radio->core;
1837 dev_dbg(radio->dev, "%s\n", __func__);
1839 if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
1842 if (mutex_lock_interruptible(&core->lock))
1845 r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1849 r = wl1273_fm_tx_set_spacing(radio, seek->spacing);
1851 dev_warn(radio->dev, "HW seek failed: %d\n", r);
1853 r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward,
1854 WL1273_DEFAULT_SEEK_LEVEL);
1856 dev_warn(radio->dev, "HW seek failed: %d\n", r);
1859 mutex_unlock(&core->lock);
1863 static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
1864 struct v4l2_modulator *modulator)
1866 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1867 struct wl1273_core *core = radio->core;
1870 dev_dbg(radio->dev, "%s\n", __func__);
1872 if (modulator->index > 0)
1875 if (mutex_lock_interruptible(&core->lock))
1878 r = wl1273_fm_set_mode(radio, WL1273_MODE_TX);
1882 if (modulator->txsubchans & V4L2_TUNER_SUB_RDS)
1883 r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1885 r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1887 if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
1888 r = wl1273_fm_write_cmd(core, WL1273_MONO_SET, WL1273_TX_MONO);
1890 r = wl1273_fm_write_cmd(core, WL1273_MONO_SET,
1893 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1894 "MONO_SET fails: %d\n", r);
1896 mutex_unlock(&core->lock);
1901 static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
1902 struct v4l2_modulator *modulator)
1904 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1905 struct wl1273_core *core = radio->core;
1909 dev_dbg(radio->dev, "%s\n", __func__);
1911 strlcpy(modulator->name, WL1273_FM_DRIVER_NAME,
1912 sizeof(modulator->name));
1914 modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
1915 modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
1917 modulator->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
1918 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
1920 if (core->mode != WL1273_MODE_TX)
1923 if (mutex_lock_interruptible(&core->lock))
1926 r = wl1273_fm_read_reg(core, WL1273_MONO_SET, &val);
1930 if (val == WL1273_TX_STEREO)
1931 modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
1933 modulator->txsubchans = V4L2_TUNER_SUB_MONO;
1936 modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
1938 mutex_unlock(&core->lock);
1943 static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
1945 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1946 struct wl1273_core *core = radio->core;
1947 struct device *dev = radio->dev;
1951 dev_info(dev, DRIVER_DESC);
1953 if (core->mode == WL1273_MODE_OFF) {
1954 dev_info(dev, "Mode: Off\n");
1958 if (core->mode == WL1273_MODE_SUSPENDED) {
1959 dev_info(dev, "Mode: Suspended\n");
1963 r = wl1273_fm_read_reg(core, WL1273_ASIC_ID_GET, &val);
1965 dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__);
1967 dev_info(dev, "ASIC_ID: 0x%04x\n", val);
1969 r = wl1273_fm_read_reg(core, WL1273_ASIC_VER_GET, &val);
1971 dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__);
1973 dev_info(dev, "ASIC Version: 0x%04x\n", val);
1975 r = wl1273_fm_read_reg(core, WL1273_FIRM_VER_GET, &val);
1977 dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__);
1979 dev_info(dev, "FW version: %d(0x%04x)\n", val, val);
1981 r = wl1273_fm_read_reg(core, WL1273_BAND_SET, &val);
1983 dev_err(dev, "%s: Get BAND fails.\n", __func__);
1985 dev_info(dev, "BAND: %d\n", val);
1987 if (core->mode == WL1273_MODE_TX) {
1988 r = wl1273_fm_read_reg(core, WL1273_PUPD_SET, &val);
1990 dev_err(dev, "%s: Get PUPD fails.\n", __func__);
1992 dev_info(dev, "PUPD: 0x%04x\n", val);
1994 r = wl1273_fm_read_reg(core, WL1273_CHANL_SET, &val);
1996 dev_err(dev, "%s: Get CHANL fails.\n", __func__);
1998 dev_info(dev, "Tx frequency: %dkHz\n", val*10);
1999 } else if (core->mode == WL1273_MODE_RX) {
2000 int bf = radio->rangelow;
2002 r = wl1273_fm_read_reg(core, WL1273_FREQ_SET, &val);
2004 dev_err(dev, "%s: Get FREQ fails.\n", __func__);
2006 dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50);
2008 r = wl1273_fm_read_reg(core, WL1273_MOST_MODE_SET, &val);
2010 dev_err(dev, "%s: Get MOST_MODE fails.\n",
2013 dev_info(dev, "MOST_MODE: Stereo according to blend\n");
2015 dev_info(dev, "MOST_MODE: Force mono output\n");
2017 dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val);
2019 r = wl1273_fm_read_reg(core, WL1273_MOST_BLEND_SET, &val);
2021 dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__);
2024 "MOST_BLEND: Switched blend & hysteresis.\n");
2026 dev_info(dev, "MOST_BLEND: Soft blend.\n");
2028 dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val);
2030 r = wl1273_fm_read_reg(core, WL1273_STEREO_GET, &val);
2032 dev_err(dev, "%s: Get STEREO fails.\n", __func__);
2034 dev_info(dev, "STEREO: Not detected\n");
2036 dev_info(dev, "STEREO: Detected\n");
2038 dev_info(dev, "STEREO: Unexpected value: %d\n", val);
2040 r = wl1273_fm_read_reg(core, WL1273_RSSI_LVL_GET, &val);
2042 dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__);
2044 dev_info(dev, "RX signal strength: %d\n", (s16) val);
2046 r = wl1273_fm_read_reg(core, WL1273_POWER_SET, &val);
2048 dev_err(dev, "%s: Get POWER fails.\n", __func__);
2050 dev_info(dev, "POWER: 0x%04x\n", val);
2052 r = wl1273_fm_read_reg(core, WL1273_INT_MASK_SET, &val);
2054 dev_err(dev, "%s: Get INT_MASK fails.\n", __func__);
2056 dev_info(dev, "INT_MASK: 0x%04x\n", val);
2058 r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val);
2060 dev_err(dev, "%s: Get RDS_SYNC fails.\n",
2063 dev_info(dev, "RDS_SYNC: Not synchronized\n");
2066 dev_info(dev, "RDS_SYNC: Synchronized\n");
2068 dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val);
2070 r = wl1273_fm_read_reg(core, WL1273_I2S_MODE_CONFIG_SET, &val);
2072 dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n",
2075 dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val);
2077 r = wl1273_fm_read_reg(core, WL1273_VOLUME_SET, &val);
2079 dev_err(dev, "%s: Get VOLUME fails.\n", __func__);
2081 dev_info(dev, "VOLUME: 0x%04x\n", val);
2087 static void wl1273_vdev_release(struct video_device *dev)
2091 static const struct v4l2_ctrl_ops wl1273_ctrl_ops = {
2092 .s_ctrl = wl1273_fm_vidioc_s_ctrl,
2093 .g_volatile_ctrl = wl1273_fm_g_volatile_ctrl,
2096 static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
2097 .vidioc_querycap = wl1273_fm_vidioc_querycap,
2098 .vidioc_g_input = wl1273_fm_vidioc_g_input,
2099 .vidioc_s_input = wl1273_fm_vidioc_s_input,
2100 .vidioc_g_audio = wl1273_fm_vidioc_g_audio,
2101 .vidioc_s_audio = wl1273_fm_vidioc_s_audio,
2102 .vidioc_g_tuner = wl1273_fm_vidioc_g_tuner,
2103 .vidioc_s_tuner = wl1273_fm_vidioc_s_tuner,
2104 .vidioc_g_frequency = wl1273_fm_vidioc_g_frequency,
2105 .vidioc_s_frequency = wl1273_fm_vidioc_s_frequency,
2106 .vidioc_s_hw_freq_seek = wl1273_fm_vidioc_s_hw_freq_seek,
2107 .vidioc_g_modulator = wl1273_fm_vidioc_g_modulator,
2108 .vidioc_s_modulator = wl1273_fm_vidioc_s_modulator,
2109 .vidioc_log_status = wl1273_fm_vidioc_log_status,
2112 static struct video_device wl1273_viddev_template = {
2113 .fops = &wl1273_fops,
2114 .ioctl_ops = &wl1273_ioctl_ops,
2115 .name = WL1273_FM_DRIVER_NAME,
2116 .release = wl1273_vdev_release,
2119 static int wl1273_fm_radio_remove(struct platform_device *pdev)
2121 struct wl1273_device *radio = platform_get_drvdata(pdev);
2122 struct wl1273_core *core = radio->core;
2124 dev_info(&pdev->dev, "%s.\n", __func__);
2126 free_irq(core->client->irq, radio);
2127 core->pdata->free_resources();
2129 v4l2_ctrl_handler_free(&radio->ctrl_handler);
2130 video_unregister_device(&radio->videodev);
2131 v4l2_device_unregister(&radio->v4l2dev);
2132 kfree(radio->buffer);
2133 kfree(radio->write_buf);
2139 static int __devinit wl1273_fm_radio_probe(struct platform_device *pdev)
2141 struct wl1273_core **core = pdev->dev.platform_data;
2142 struct wl1273_device *radio;
2143 struct v4l2_ctrl *ctrl;
2146 pr_debug("%s\n", __func__);
2149 dev_err(&pdev->dev, "No platform data.\n");
2154 radio = kzalloc(sizeof(*radio), GFP_KERNEL);
2160 /* RDS buffer allocation */
2161 radio->buf_size = rds_buf * RDS_BLOCK_SIZE;
2162 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
2163 if (!radio->buffer) {
2164 pr_err("Cannot allocate memory for RDS buffer.\n");
2169 radio->core = *core;
2170 radio->irq_flags = WL1273_IRQ_MASK;
2171 radio->dev = &radio->core->client->dev;
2172 radio->rds_on = false;
2173 radio->core->mode = WL1273_MODE_OFF;
2174 radio->tx_power = 118;
2175 radio->core->audio_mode = WL1273_AUDIO_ANALOG;
2176 radio->band = WL1273_BAND_OTHER;
2177 radio->core->i2s_mode = WL1273_I2S_DEF_MODE;
2178 radio->core->channel_number = 2;
2179 radio->core->volume = WL1273_DEFAULT_VOLUME;
2180 radio->rx_frequency = WL1273_BAND_OTHER_LOW;
2181 radio->tx_frequency = WL1273_BAND_OTHER_HIGH;
2182 radio->rangelow = WL1273_BAND_OTHER_LOW;
2183 radio->rangehigh = WL1273_BAND_OTHER_HIGH;
2184 radio->stereo = true;
2185 radio->bus_type = "I2C";
2187 radio->core->write = wl1273_fm_write_cmd;
2188 radio->core->set_audio = wl1273_fm_set_audio;
2189 radio->core->set_volume = wl1273_fm_set_volume;
2191 if (radio->core->pdata->request_resources) {
2192 r = radio->core->pdata->request_resources(radio->core->client);
2194 dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2195 ": Cannot get platform data\n");
2199 dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq);
2201 r = request_threaded_irq(radio->core->client->irq, NULL,
2202 wl1273_fm_irq_thread_handler,
2203 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
2204 "wl1273-fm", radio);
2206 dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2207 ": Unable to register IRQ handler: %d\n", r);
2208 goto err_request_irq;
2211 dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ"
2217 init_completion(&radio->busy);
2218 init_waitqueue_head(&radio->read_queue);
2220 radio->write_buf = kmalloc(256, GFP_KERNEL);
2221 if (!radio->write_buf) {
2226 radio->dev = &pdev->dev;
2227 radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
2228 radio->rds_users = 0;
2230 r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
2232 dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
2233 goto device_register_err;
2236 /* V4L2 configuration */
2237 memcpy(&radio->videodev, &wl1273_viddev_template,
2238 sizeof(wl1273_viddev_template));
2240 radio->videodev.v4l2_dev = &radio->v4l2dev;
2242 v4l2_ctrl_handler_init(&radio->ctrl_handler, 6);
2244 /* add in ascending ID order */
2245 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2246 V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1,
2247 WL1273_DEFAULT_VOLUME);
2249 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2250 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
2252 v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops,
2253 V4L2_CID_TUNE_PREEMPHASIS,
2254 V4L2_PREEMPHASIS_75_uS, 0x03,
2255 V4L2_PREEMPHASIS_50_uS);
2257 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2258 V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118);
2260 ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2261 V4L2_CID_TUNE_ANTENNA_CAPACITOR,
2264 ctrl->is_volatile = 1;
2266 if (radio->ctrl_handler.error) {
2267 r = radio->ctrl_handler.error;
2268 dev_err(&pdev->dev, "Ctrl handler error: %d\n", r);
2269 goto handler_init_err;
2272 video_set_drvdata(&radio->videodev, radio);
2273 platform_set_drvdata(pdev, radio);
2275 /* register video device */
2276 r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
2278 dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
2279 ": Could not register video device\n");
2280 goto handler_init_err;
2286 v4l2_ctrl_handler_free(&radio->ctrl_handler);
2287 v4l2_device_unregister(&radio->v4l2dev);
2288 device_register_err:
2289 kfree(radio->write_buf);
2291 free_irq(radio->core->client->irq, radio);
2293 radio->core->pdata->free_resources();
2295 kfree(radio->buffer);
2302 MODULE_ALIAS("platform:wl1273_fm_radio");
2304 static struct platform_driver wl1273_fm_radio_driver = {
2305 .probe = wl1273_fm_radio_probe,
2306 .remove = __devexit_p(wl1273_fm_radio_remove),
2308 .name = "wl1273_fm_radio",
2309 .owner = THIS_MODULE,
2313 static int __init wl1273_fm_module_init(void)
2315 pr_info("%s\n", __func__);
2316 return platform_driver_register(&wl1273_fm_radio_driver);
2318 module_init(wl1273_fm_module_init);
2320 static void __exit wl1273_fm_module_exit(void)
2322 flush_scheduled_work();
2323 platform_driver_unregister(&wl1273_fm_radio_driver);
2324 pr_info(DRIVER_DESC ", Exiting.\n");
2326 module_exit(wl1273_fm_module_exit);
2328 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
2329 MODULE_DESCRIPTION(DRIVER_DESC);
2330 MODULE_LICENSE("GPL");