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;
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_write_fw(struct wl1273_core *core,
110 struct i2c_client *client = core->client;
114 msg.addr = client->addr;
117 for (i = 0; i <= len; i++) {
122 dev_dbg(&client->dev, "%s:len[%d]: %d\n", __func__, i, msg.len);
124 r = i2c_transfer(client->adapter, &msg, 1);
125 if (r < 0 && i < len + 1)
129 dev_dbg(&client->dev, "%s: i: %d\n", __func__, i);
130 dev_dbg(&client->dev, "%s: len + 1: %d\n", __func__, len + 1);
132 /* Last transfer always fails. */
133 if (i == len || r == 1)
139 #define WL1273_FIFO_HAS_DATA(status) (1 << 5 & status)
140 #define WL1273_RDS_CORRECTABLE_ERROR (1 << 3)
141 #define WL1273_RDS_UNCORRECTABLE_ERROR (1 << 4)
143 static int wl1273_fm_rds(struct wl1273_device *radio)
145 struct wl1273_core *core = radio->core;
146 struct i2c_client *client = core->client;
148 u8 b0 = WL1273_RDS_DATA_GET, status;
149 struct v4l2_rds_data rds = { 0, 0, 0 };
150 struct i2c_msg msg[] = {
152 .addr = client->addr,
158 .addr = client->addr,
166 if (core->mode != WL1273_MODE_RX)
169 r = core->read(core, WL1273_RDS_SYNC_GET, &val);
173 if ((val & 0x01) == 0) {
174 /* RDS decoder not synchronized */
178 /* copy all four RDS blocks to internal buffer */
180 r = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
181 if (r != ARRAY_SIZE(msg)) {
182 dev_err(radio->dev, WL1273_FM_DRIVER_NAME
183 ": %s: read_rds error r == %i)\n",
189 if (!WL1273_FIFO_HAS_DATA(status))
192 /* copy bits 0-2 (the block ID) to bits 3-5 */
193 rds.block = V4L2_RDS_BLOCK_MSK & status;
194 rds.block |= rds.block << 3;
196 /* copy the error bits to standard positions */
197 if (WL1273_RDS_UNCORRECTABLE_ERROR & status) {
198 rds.block |= V4L2_RDS_BLOCK_ERROR;
199 rds.block &= ~V4L2_RDS_BLOCK_CORRECTED;
200 } else if (WL1273_RDS_CORRECTABLE_ERROR & status) {
201 rds.block &= ~V4L2_RDS_BLOCK_ERROR;
202 rds.block |= V4L2_RDS_BLOCK_CORRECTED;
205 /* copy RDS block to internal buffer */
206 memcpy(&radio->buffer[radio->wr_index], &rds, RDS_BLOCK_SIZE);
207 radio->wr_index += 3;
209 /* wrap write pointer */
210 if (radio->wr_index >= radio->buf_size)
213 /* check for overflow & start over */
214 if (radio->wr_index == radio->rd_index) {
215 dev_dbg(radio->dev, "RDS OVERFLOW");
221 } while (WL1273_FIFO_HAS_DATA(status));
223 /* wake up read queue */
224 if (radio->wr_index != radio->rd_index)
225 wake_up_interruptible(&radio->read_queue);
230 static irqreturn_t wl1273_fm_irq_thread_handler(int irq, void *dev_id)
232 struct wl1273_device *radio = dev_id;
233 struct wl1273_core *core = radio->core;
237 r = core->read(core, WL1273_FLAG_GET, &flags);
241 if (flags & WL1273_BL_EVENT) {
242 radio->irq_received = flags;
243 dev_dbg(radio->dev, "IRQ: BL\n");
246 if (flags & WL1273_RDS_EVENT) {
249 wl1273_fm_rds(radio);
252 if (flags & WL1273_BBLK_EVENT)
253 dev_dbg(radio->dev, "IRQ: BBLK\n");
255 if (flags & WL1273_LSYNC_EVENT)
256 dev_dbg(radio->dev, "IRQ: LSYNC\n");
258 if (flags & WL1273_LEV_EVENT) {
261 r = core->read(core, WL1273_RSSI_LVL_GET, &level);
266 dev_dbg(radio->dev, "IRQ: LEV: 0x%x04\n", level);
269 if (flags & WL1273_IFFR_EVENT)
270 dev_dbg(radio->dev, "IRQ: IFFR\n");
272 if (flags & WL1273_PI_EVENT)
273 dev_dbg(radio->dev, "IRQ: PI\n");
275 if (flags & WL1273_PD_EVENT)
276 dev_dbg(radio->dev, "IRQ: PD\n");
278 if (flags & WL1273_STIC_EVENT)
279 dev_dbg(radio->dev, "IRQ: STIC\n");
281 if (flags & WL1273_MAL_EVENT)
282 dev_dbg(radio->dev, "IRQ: MAL\n");
284 if (flags & WL1273_POW_ENB_EVENT) {
285 complete(&radio->busy);
286 dev_dbg(radio->dev, "NOT BUSY\n");
287 dev_dbg(radio->dev, "IRQ: POW_ENB\n");
290 if (flags & WL1273_SCAN_OVER_EVENT)
291 dev_dbg(radio->dev, "IRQ: SCAN_OVER\n");
293 if (flags & WL1273_ERROR_EVENT)
294 dev_dbg(radio->dev, "IRQ: ERROR\n");
296 if (flags & WL1273_FR_EVENT) {
299 dev_dbg(radio->dev, "IRQ: FR:\n");
301 if (core->mode == WL1273_MODE_RX) {
302 r = core->write(core, WL1273_TUNER_MODE_SET,
303 TUNER_MODE_STOP_SEARCH);
306 "%s: TUNER_MODE_SET fails: %d\n",
311 r = core->read(core, WL1273_FREQ_SET, &freq);
315 if (radio->band == WL1273_BAND_JAPAN)
316 radio->rx_frequency = WL1273_BAND_JAPAN_LOW +
319 radio->rx_frequency = WL1273_BAND_OTHER_LOW +
322 * The driver works better with this msleep,
323 * the documentation doesn't mention it.
325 usleep_range(10000, 15000);
327 dev_dbg(radio->dev, "%dkHz\n", radio->rx_frequency);
330 r = core->read(core, WL1273_CHANL_SET, &freq);
334 dev_dbg(radio->dev, "%dkHz\n", freq);
336 dev_dbg(radio->dev, "%s: NOT BUSY\n", __func__);
340 core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
341 complete(&radio->busy);
346 static int wl1273_fm_set_tx_freq(struct wl1273_device *radio, unsigned int freq)
348 struct wl1273_core *core = radio->core;
351 if (freq < WL1273_BAND_TX_LOW) {
353 "Frequency out of range: %d < %d\n", freq,
358 if (freq > WL1273_BAND_TX_HIGH) {
360 "Frequency out of range: %d > %d\n", freq,
361 WL1273_BAND_TX_HIGH);
366 * The driver works better with this sleep,
367 * the documentation doesn't mention it.
369 usleep_range(5000, 10000);
371 dev_dbg(radio->dev, "%s: freq: %d kHz\n", __func__, freq);
373 /* Set the current tx channel */
374 r = core->write(core, WL1273_CHANL_SET, freq / 10);
378 INIT_COMPLETION(radio->busy);
380 /* wait for the FR IRQ */
381 r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
385 dev_dbg(radio->dev, "WL1273_CHANL_SET: %d\n", r);
387 /* Enable the output power */
388 r = core->write(core, WL1273_POWER_ENB_SET, 1);
392 INIT_COMPLETION(radio->busy);
394 /* wait for the POWER_ENB IRQ */
395 r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
399 radio->tx_frequency = freq;
400 dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %d\n", r);
405 static int wl1273_fm_set_rx_freq(struct wl1273_device *radio, unsigned int freq)
407 struct wl1273_core *core = radio->core;
410 if (freq < radio->rangelow) {
412 "Frequency out of range: %d < %d\n", freq,
418 if (freq > radio->rangehigh) {
420 "Frequency out of range: %d > %d\n", freq,
426 dev_dbg(radio->dev, "%s: %dkHz\n", __func__, freq);
428 core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
430 if (radio->band == WL1273_BAND_JAPAN)
431 f = (freq - WL1273_BAND_JAPAN_LOW) / 50;
433 f = (freq - WL1273_BAND_OTHER_LOW) / 50;
435 r = core->write(core, WL1273_FREQ_SET, f);
437 dev_err(radio->dev, "FREQ_SET fails\n");
441 r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET);
443 dev_err(radio->dev, "TUNER_MODE_SET fails\n");
447 INIT_COMPLETION(radio->busy);
449 r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
451 dev_err(radio->dev, "%s: TIMEOUT\n", __func__);
457 radio->rx_frequency = freq;
463 static int wl1273_fm_get_freq(struct wl1273_device *radio)
465 struct wl1273_core *core = radio->core;
470 if (core->mode == WL1273_MODE_RX) {
471 r = core->read(core, WL1273_FREQ_SET, &f);
475 dev_dbg(radio->dev, "Freq get: 0x%04x\n", f);
476 if (radio->band == WL1273_BAND_JAPAN)
477 freq = WL1273_BAND_JAPAN_LOW + 50 * f;
479 freq = WL1273_BAND_OTHER_LOW + 50 * f;
481 r = core->read(core, WL1273_CHANL_SET, &f);
492 * wl1273_fm_upload_firmware_patch() - Upload the firmware.
493 * @radio: A pointer to the device struct.
495 * The firmware file consists of arrays of bytes where the first byte
496 * gives the array length. The first byte in the file gives the
497 * number of these arrays.
499 static int wl1273_fm_upload_firmware_patch(struct wl1273_device *radio)
501 struct wl1273_core *core = radio->core;
502 unsigned int packet_num;
503 const struct firmware *fw_p;
504 const char *fw_name = "radio-wl1273-fw.bin";
505 struct device *dev = radio->dev;
509 dev_dbg(dev, "%s:\n", __func__);
512 * Uploading the firmware patch is not always necessary,
513 * so we only print an info message.
515 if (request_firmware(&fw_p, fw_name, dev)) {
516 dev_info(dev, "%s - %s not found\n", __func__, fw_name);
521 ptr = (__u8 *) fw_p->data;
523 dev_dbg(dev, "%s: packets: %d\n", __func__, packet_num);
525 r = wl1273_fm_write_fw(core, ptr + 1, packet_num);
527 dev_err(dev, "FW upload error: %d\n", r);
531 /* ignore possible error here */
532 core->write(core, WL1273_RESET, 0);
534 dev_dbg(dev, "%s - download OK, r: %d\n", __func__, r);
536 release_firmware(fw_p);
540 static int wl1273_fm_stop(struct wl1273_device *radio)
542 struct wl1273_core *core = radio->core;
544 if (core->mode == WL1273_MODE_RX) {
545 int r = core->write(core, WL1273_POWER_SET,
546 WL1273_POWER_SET_OFF);
548 dev_err(radio->dev, "%s: POWER_SET fails: %d\n",
550 } else if (core->mode == WL1273_MODE_TX) {
551 int r = core->write(core, WL1273_PUPD_SET,
552 WL1273_PUPD_SET_OFF);
555 "%s: PUPD_SET fails: %d\n", __func__, r);
558 if (core->pdata->disable) {
559 core->pdata->disable();
560 dev_dbg(radio->dev, "Back to reset\n");
566 static int wl1273_fm_start(struct wl1273_device *radio, int new_mode)
568 struct wl1273_core *core = radio->core;
569 struct wl1273_fm_platform_data *pdata = core->pdata;
570 struct device *dev = radio->dev;
573 if (pdata->enable && core->mode == WL1273_MODE_OFF) {
574 dev_dbg(radio->dev, "Out of reset\n");
580 if (new_mode == WL1273_MODE_RX) {
581 u16 val = WL1273_POWER_SET_FM;
584 val |= WL1273_POWER_SET_RDS;
586 /* If this fails try again */
587 r = core->write(core, WL1273_POWER_SET, val);
591 r = core->write(core, WL1273_POWER_SET, val);
593 dev_err(dev, "%s: POWER_SET fails\n", __func__);
598 /* rds buffer configuration */
602 } else if (new_mode == WL1273_MODE_TX) {
603 /* If this fails try again once */
604 r = core->write(core, WL1273_PUPD_SET, WL1273_PUPD_SET_ON);
607 r = core->write(core, WL1273_PUPD_SET,
610 dev_err(dev, "%s: PUPD_SET fails\n", __func__);
616 r = core->write(core, WL1273_RDS_DATA_ENB, 1);
618 r = core->write(core, WL1273_RDS_DATA_ENB, 0);
620 dev_warn(dev, "%s: Illegal mode.\n", __func__);
623 if (core->mode == WL1273_MODE_OFF) {
624 r = wl1273_fm_upload_firmware_patch(radio);
626 dev_warn(dev, "Firmware upload failed.\n");
629 * Sometimes the chip is in a wrong power state at this point.
630 * So we set the power once again.
632 if (new_mode == WL1273_MODE_RX) {
633 u16 val = WL1273_POWER_SET_FM;
636 val |= WL1273_POWER_SET_RDS;
638 r = core->write(core, WL1273_POWER_SET, val);
640 dev_err(dev, "%s: POWER_SET fails\n", __func__);
643 } else if (new_mode == WL1273_MODE_TX) {
644 r = core->write(core, WL1273_PUPD_SET,
647 dev_err(dev, "%s: PUPD_SET fails\n", __func__);
658 dev_dbg(dev, "%s: return: %d\n", __func__, r);
662 static int wl1273_fm_suspend(struct wl1273_device *radio)
664 struct wl1273_core *core = radio->core;
667 /* Cannot go from OFF to SUSPENDED */
668 if (core->mode == WL1273_MODE_RX)
669 r = core->write(core, WL1273_POWER_SET,
670 WL1273_POWER_SET_RETENTION);
671 else if (core->mode == WL1273_MODE_TX)
672 r = core->write(core, WL1273_PUPD_SET,
673 WL1273_PUPD_SET_RETENTION);
678 dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r);
686 static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode)
688 struct wl1273_core *core = radio->core;
689 struct device *dev = radio->dev;
693 dev_dbg(dev, "%s\n", __func__);
694 dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden);
696 old_mode = core->mode;
697 if (mode & radio->forbidden) {
705 r = wl1273_fm_start(radio, mode);
707 dev_err(dev, "%s: Cannot start.\n", __func__);
708 wl1273_fm_stop(radio);
713 r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
715 dev_err(dev, "INT_MASK_SET fails.\n");
719 /* remember previous settings */
720 if (mode == WL1273_MODE_RX) {
721 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
723 dev_err(dev, "set freq fails: %d.\n", r);
727 r = core->set_volume(core, core->volume);
729 dev_err(dev, "set volume fails: %d.\n", r);
733 dev_dbg(dev, "%s: Set vol: %d.\n", __func__,
736 r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency);
738 dev_err(dev, "set freq fails: %d.\n", r);
743 dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__);
745 r = core->set_audio(core, core->audio_mode);
747 dev_err(dev, "Cannot set audio mode.\n");
750 case WL1273_MODE_OFF:
751 r = wl1273_fm_stop(radio);
753 dev_err(dev, "%s: Off fails: %d\n", __func__, r);
755 core->mode = WL1273_MODE_OFF;
759 case WL1273_MODE_SUSPENDED:
760 r = wl1273_fm_suspend(radio);
762 dev_err(dev, "%s: Suspend fails: %d\n", __func__, r);
764 core->mode = WL1273_MODE_SUSPENDED;
769 dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode);
775 core->mode = old_mode;
780 static int wl1273_fm_set_seek(struct wl1273_device *radio,
781 unsigned int wrap_around,
782 unsigned int seek_upward,
785 struct wl1273_core *core = radio->core;
787 unsigned int dir = (seek_upward == 0) ? 0 : 1;
790 f = radio->rx_frequency;
791 dev_dbg(radio->dev, "rx_frequency: %d\n", f);
793 if (dir && f + radio->spacing <= radio->rangehigh)
794 r = wl1273_fm_set_rx_freq(radio, f + radio->spacing);
795 else if (dir && wrap_around)
796 r = wl1273_fm_set_rx_freq(radio, radio->rangelow);
797 else if (f - radio->spacing >= radio->rangelow)
798 r = wl1273_fm_set_rx_freq(radio, f - radio->spacing);
799 else if (wrap_around)
800 r = wl1273_fm_set_rx_freq(radio, radio->rangehigh);
805 if (level < SCHAR_MIN || level > SCHAR_MAX)
808 INIT_COMPLETION(radio->busy);
809 dev_dbg(radio->dev, "%s: BUSY\n", __func__);
811 r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
815 dev_dbg(radio->dev, "%s\n", __func__);
817 r = core->write(core, WL1273_SEARCH_LVL_SET, level);
821 r = core->write(core, WL1273_SEARCH_DIR_SET, dir);
825 r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
829 wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
830 if (!(radio->irq_received & WL1273_BL_EVENT))
833 radio->irq_received &= ~WL1273_BL_EVENT;
839 dev_dbg(radio->dev, "Wrap around in HW seek.\n");
844 f = radio->rangehigh;
846 r = wl1273_fm_set_rx_freq(radio, f);
850 INIT_COMPLETION(radio->busy);
851 dev_dbg(radio->dev, "%s: BUSY\n", __func__);
853 r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
857 wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
859 dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r);
864 * wl1273_fm_get_tx_ctune() - Get the TX tuning capacitor value.
865 * @radio: A pointer to the device struct.
867 static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio)
869 struct wl1273_core *core = radio->core;
870 struct device *dev = radio->dev;
874 if (core->mode == WL1273_MODE_OFF ||
875 core->mode == WL1273_MODE_SUSPENDED)
878 r = core->read(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
880 dev_err(dev, "%s: read error: %d\n", __func__, r);
889 * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
890 * @radio: A pointer to the device struct.
891 * @preemphasis: The new pre-amphasis value.
893 * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
894 * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
896 static int wl1273_fm_set_preemphasis(struct wl1273_device *radio,
897 unsigned int preemphasis)
899 struct wl1273_core *core = radio->core;
903 if (core->mode == WL1273_MODE_OFF ||
904 core->mode == WL1273_MODE_SUSPENDED)
907 mutex_lock(&core->lock);
909 switch (preemphasis) {
910 case V4L2_PREEMPHASIS_DISABLED:
913 case V4L2_PREEMPHASIS_50_uS:
916 case V4L2_PREEMPHASIS_75_uS:
924 r = core->write(core, WL1273_PREMPH_SET, em);
928 radio->preemphasis = preemphasis;
931 mutex_unlock(&core->lock);
935 static int wl1273_fm_rds_on(struct wl1273_device *radio)
937 struct wl1273_core *core = radio->core;
940 dev_dbg(radio->dev, "%s\n", __func__);
944 r = core->write(core, WL1273_POWER_SET,
945 WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
949 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
951 dev_err(radio->dev, "set freq fails: %d.\n", r);
956 static int wl1273_fm_rds_off(struct wl1273_device *radio)
958 struct wl1273_core *core = radio->core;
964 radio->irq_flags &= ~WL1273_RDS_EVENT;
966 r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
970 /* stop rds reception */
971 cancel_delayed_work(&radio->work);
973 /* Service pending read */
974 wake_up_interruptible(&radio->read_queue);
976 dev_dbg(radio->dev, "%s\n", __func__);
978 r = core->write(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
982 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
984 dev_err(radio->dev, "set freq fails: %d.\n", r);
986 dev_dbg(radio->dev, "%s: exiting...\n", __func__);
991 static int wl1273_fm_set_rds(struct wl1273_device *radio, unsigned int new_mode)
994 struct wl1273_core *core = radio->core;
996 if (core->mode == WL1273_MODE_OFF ||
997 core->mode == WL1273_MODE_SUSPENDED)
1000 if (new_mode == WL1273_RDS_RESET) {
1001 r = core->write(core, WL1273_RDS_CNTRL_SET, 1);
1005 if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) {
1006 r = core->write(core, WL1273_RDS_DATA_ENB, 0);
1007 } else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) {
1008 r = core->write(core, WL1273_RDS_DATA_ENB, 1);
1009 } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) {
1010 r = wl1273_fm_rds_off(radio);
1011 } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) {
1012 r = wl1273_fm_rds_on(radio);
1014 dev_err(radio->dev, "%s: Unknown mode: %d\n",
1015 __func__, new_mode);
1020 radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false;
1025 static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf,
1026 size_t count, loff_t *ppos)
1028 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1029 struct wl1273_core *core = radio->core;
1033 dev_dbg(radio->dev, "%s\n", __func__);
1035 if (core->mode != WL1273_MODE_TX)
1038 if (radio->rds_users == 0) {
1039 dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1043 if (mutex_lock_interruptible(&core->lock))
1046 * Multiple processes can open the device, but only
1047 * one gets to write to it.
1049 if (radio->owner && radio->owner != file) {
1053 radio->owner = file;
1061 core->write(core, WL1273_RDS_CONFIG_DATA_SET, val);
1063 if (copy_from_user(radio->write_buf + 1, buf, val)) {
1068 dev_dbg(radio->dev, "Count: %d\n", val);
1069 dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf);
1071 radio->write_buf[0] = WL1273_RDS_DATA_SET;
1072 core->write_data(core, radio->write_buf, val + 1);
1076 mutex_unlock(&core->lock);
1081 static unsigned int wl1273_fm_fops_poll(struct file *file,
1082 struct poll_table_struct *pts)
1084 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1085 struct wl1273_core *core = radio->core;
1087 if (radio->owner && radio->owner != file)
1090 radio->owner = file;
1092 if (core->mode == WL1273_MODE_RX) {
1093 poll_wait(file, &radio->read_queue, pts);
1095 if (radio->rd_index != radio->wr_index)
1096 return POLLIN | POLLRDNORM;
1098 } else if (core->mode == WL1273_MODE_TX) {
1099 return POLLOUT | POLLWRNORM;
1105 static int wl1273_fm_fops_open(struct file *file)
1107 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1108 struct wl1273_core *core = radio->core;
1111 dev_dbg(radio->dev, "%s\n", __func__);
1113 if (core->mode == WL1273_MODE_RX && radio->rds_on &&
1114 !radio->rds_users) {
1115 dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode);
1117 if (mutex_lock_interruptible(&core->lock))
1120 radio->irq_flags |= WL1273_RDS_EVENT;
1122 r = core->write(core, WL1273_INT_MASK_SET,
1125 mutex_unlock(&core->lock);
1131 mutex_unlock(&core->lock);
1137 static int wl1273_fm_fops_release(struct file *file)
1139 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1140 struct wl1273_core *core = radio->core;
1143 dev_dbg(radio->dev, "%s\n", __func__);
1145 if (radio->rds_users > 0) {
1147 if (radio->rds_users == 0) {
1148 if (mutex_lock_interruptible(&core->lock))
1151 radio->irq_flags &= ~WL1273_RDS_EVENT;
1153 if (core->mode == WL1273_MODE_RX) {
1154 r = core->write(core,
1155 WL1273_INT_MASK_SET,
1158 mutex_unlock(&core->lock);
1162 mutex_unlock(&core->lock);
1166 if (file == radio->owner)
1167 radio->owner = NULL;
1172 static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
1173 size_t count, loff_t *ppos)
1176 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1177 struct wl1273_core *core = radio->core;
1178 unsigned int block_count = 0;
1181 dev_dbg(radio->dev, "%s\n", __func__);
1183 if (core->mode != WL1273_MODE_RX)
1186 if (radio->rds_users == 0) {
1187 dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1191 if (mutex_lock_interruptible(&core->lock))
1195 * Multiple processes can open the device, but only
1196 * one at a time gets read access.
1198 if (radio->owner && radio->owner != file) {
1202 radio->owner = file;
1204 r = core->read(core, WL1273_RDS_SYNC_GET, &val);
1206 dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__);
1208 } else if (val == 0) {
1209 dev_info(radio->dev, "RDS_SYNC: Not synchronized\n");
1214 /* block if no new data available */
1215 while (radio->wr_index == radio->rd_index) {
1216 if (file->f_flags & O_NONBLOCK) {
1221 dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__);
1222 if (wait_event_interruptible(radio->read_queue,
1224 radio->rd_index) < 0) {
1230 /* calculate block count from byte count */
1231 count /= RDS_BLOCK_SIZE;
1233 /* copy RDS blocks from the internal buffer and to user buffer */
1234 while (block_count < count) {
1235 if (radio->rd_index == radio->wr_index)
1238 /* always transfer complete RDS blocks */
1239 if (copy_to_user(buf, &radio->buffer[radio->rd_index],
1243 /* increment and wrap the read pointer */
1244 radio->rd_index += RDS_BLOCK_SIZE;
1245 if (radio->rd_index >= radio->buf_size)
1246 radio->rd_index = 0;
1248 /* increment counters */
1250 buf += RDS_BLOCK_SIZE;
1251 r += RDS_BLOCK_SIZE;
1255 dev_dbg(radio->dev, "%s: exit\n", __func__);
1256 mutex_unlock(&core->lock);
1261 static const struct v4l2_file_operations wl1273_fops = {
1262 .owner = THIS_MODULE,
1263 .read = wl1273_fm_fops_read,
1264 .write = wl1273_fm_fops_write,
1265 .poll = wl1273_fm_fops_poll,
1266 .unlocked_ioctl = video_ioctl2,
1267 .open = wl1273_fm_fops_open,
1268 .release = wl1273_fm_fops_release,
1271 static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
1272 struct v4l2_capability *capability)
1274 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1276 dev_dbg(radio->dev, "%s\n", __func__);
1278 strlcpy(capability->driver, WL1273_FM_DRIVER_NAME,
1279 sizeof(capability->driver));
1280 strlcpy(capability->card, "Texas Instruments Wl1273 FM Radio",
1281 sizeof(capability->card));
1282 strlcpy(capability->bus_info, radio->bus_type,
1283 sizeof(capability->bus_info));
1285 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1286 V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
1287 V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
1288 V4L2_CAP_RDS_OUTPUT;
1293 static int wl1273_fm_vidioc_g_input(struct file *file, void *priv,
1296 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1298 dev_dbg(radio->dev, "%s\n", __func__);
1305 static int wl1273_fm_vidioc_s_input(struct file *file, void *priv,
1308 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1310 dev_dbg(radio->dev, "%s\n", __func__);
1319 * wl1273_fm_set_tx_power() - Set the transmission power value.
1320 * @core: A pointer to the device struct.
1321 * @power: The new power value.
1323 static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power)
1325 struct wl1273_core *core = radio->core;
1328 if (core->mode == WL1273_MODE_OFF ||
1329 core->mode == WL1273_MODE_SUSPENDED)
1332 mutex_lock(&core->lock);
1334 /* Convert the dBuV value to chip presentation */
1335 r = core->write(core, WL1273_POWER_LEV_SET, 122 - power);
1339 radio->tx_power = power;
1342 mutex_unlock(&core->lock);
1346 #define WL1273_SPACING_50kHz 1
1347 #define WL1273_SPACING_100kHz 2
1348 #define WL1273_SPACING_200kHz 4
1350 static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio,
1351 unsigned int spacing)
1353 struct wl1273_core *core = radio->core;
1357 r = core->write(core, WL1273_SCAN_SPACING_SET,
1358 WL1273_SPACING_100kHz);
1359 radio->spacing = 100;
1360 } else if (spacing - 50000 < 25000) {
1361 r = core->write(core, WL1273_SCAN_SPACING_SET,
1362 WL1273_SPACING_50kHz);
1363 radio->spacing = 50;
1364 } else if (spacing - 100000 < 50000) {
1365 r = core->write(core, WL1273_SCAN_SPACING_SET,
1366 WL1273_SPACING_100kHz);
1367 radio->spacing = 100;
1369 r = core->write(core, WL1273_SCAN_SPACING_SET,
1370 WL1273_SPACING_200kHz);
1371 radio->spacing = 200;
1377 static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1379 struct wl1273_device *radio = ctrl->priv;
1380 struct wl1273_core *core = radio->core;
1382 dev_dbg(radio->dev, "%s\n", __func__);
1384 if (mutex_lock_interruptible(&core->lock))
1388 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1389 ctrl->val = wl1273_fm_get_tx_ctune(radio);
1393 dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
1394 __func__, ctrl->id);
1398 mutex_unlock(&core->lock);
1403 #define WL1273_MUTE_SOFT_ENABLE (1 << 0)
1404 #define WL1273_MUTE_AC (1 << 1)
1405 #define WL1273_MUTE_HARD_LEFT (1 << 2)
1406 #define WL1273_MUTE_HARD_RIGHT (1 << 3)
1407 #define WL1273_MUTE_SOFT_FORCE (1 << 4)
1409 static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl)
1411 return container_of(ctrl->handler, struct wl1273_device, ctrl_handler);
1414 static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl)
1416 struct wl1273_device *radio = to_radio(ctrl);
1417 struct wl1273_core *core = radio->core;
1420 dev_dbg(radio->dev, "%s\n", __func__);
1423 case V4L2_CID_AUDIO_MUTE:
1424 if (mutex_lock_interruptible(&core->lock))
1427 if (core->mode == WL1273_MODE_RX && ctrl->val)
1428 r = core->write(core,
1429 WL1273_MUTE_STATUS_SET,
1430 WL1273_MUTE_HARD_LEFT |
1431 WL1273_MUTE_HARD_RIGHT);
1432 else if (core->mode == WL1273_MODE_RX)
1433 r = core->write(core,
1434 WL1273_MUTE_STATUS_SET, 0x0);
1435 else if (core->mode == WL1273_MODE_TX && ctrl->val)
1436 r = core->write(core, WL1273_MUTE, 1);
1437 else if (core->mode == WL1273_MODE_TX)
1438 r = core->write(core, WL1273_MUTE, 0);
1440 mutex_unlock(&core->lock);
1443 case V4L2_CID_AUDIO_VOLUME:
1445 r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF);
1447 r = core->set_volume(core, core->volume);
1450 case V4L2_CID_TUNE_PREEMPHASIS:
1451 r = wl1273_fm_set_preemphasis(radio, ctrl->val);
1454 case V4L2_CID_TUNE_POWER_LEVEL:
1455 r = wl1273_fm_set_tx_power(radio, ctrl->val);
1459 dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
1460 __func__, ctrl->id);
1464 dev_dbg(radio->dev, "%s\n", __func__);
1468 static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv,
1469 struct v4l2_audio *audio)
1471 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1473 dev_dbg(radio->dev, "%s\n", __func__);
1475 if (audio->index > 1)
1478 strlcpy(audio->name, "Radio", sizeof(audio->name));
1479 audio->capability = V4L2_AUDCAP_STEREO;
1484 static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
1485 struct v4l2_audio *audio)
1487 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1489 dev_dbg(radio->dev, "%s\n", __func__);
1491 if (audio->index != 0)
1497 #define WL1273_RDS_NOT_SYNCHRONIZED 0
1498 #define WL1273_RDS_SYNCHRONIZED 1
1500 static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv,
1501 struct v4l2_tuner *tuner)
1503 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1504 struct wl1273_core *core = radio->core;
1508 dev_dbg(radio->dev, "%s\n", __func__);
1510 if (tuner->index > 0)
1513 strlcpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name));
1514 tuner->type = V4L2_TUNER_RADIO;
1516 tuner->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
1517 tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
1519 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
1520 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
1523 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1525 tuner->audmode = V4L2_TUNER_MODE_MONO;
1527 if (core->mode != WL1273_MODE_RX)
1530 if (mutex_lock_interruptible(&core->lock))
1533 r = core->read(core, WL1273_STEREO_GET, &val);
1538 tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
1540 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1542 r = core->read(core, WL1273_RSSI_LVL_GET, &val);
1546 tuner->signal = (s16) val;
1547 dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);
1551 r = core->read(core, WL1273_RDS_SYNC_GET, &val);
1555 if (val == WL1273_RDS_SYNCHRONIZED)
1556 tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
1558 mutex_unlock(&core->lock);
1563 static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
1564 struct v4l2_tuner *tuner)
1566 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1567 struct wl1273_core *core = radio->core;
1570 dev_dbg(radio->dev, "%s\n", __func__);
1571 dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index);
1572 dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name);
1573 dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability);
1574 dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans);
1575 dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow);
1576 dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh);
1578 if (tuner->index > 0)
1581 if (mutex_lock_interruptible(&core->lock))
1584 r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1588 if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS)
1589 r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1591 r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1594 dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r);
1596 if (tuner->audmode == V4L2_TUNER_MODE_MONO) {
1597 r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
1599 dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1603 radio->stereo = false;
1604 } else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) {
1605 r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
1607 dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1611 radio->stereo = true;
1613 dev_err(radio->dev, "%s: tuner->audmode: %d\n",
1614 __func__, tuner->audmode);
1620 mutex_unlock(&core->lock);
1625 static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
1626 struct v4l2_frequency *freq)
1628 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1629 struct wl1273_core *core = radio->core;
1631 dev_dbg(radio->dev, "%s\n", __func__);
1633 if (mutex_lock_interruptible(&core->lock))
1636 freq->type = V4L2_TUNER_RADIO;
1637 freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio));
1639 mutex_unlock(&core->lock);
1644 static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
1645 struct v4l2_frequency *freq)
1647 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1648 struct wl1273_core *core = radio->core;
1651 dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency);
1653 if (freq->type != V4L2_TUNER_RADIO) {
1655 "freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
1659 if (mutex_lock_interruptible(&core->lock))
1662 if (core->mode == WL1273_MODE_RX) {
1663 dev_dbg(radio->dev, "freq: %d\n", freq->frequency);
1665 r = wl1273_fm_set_rx_freq(radio,
1666 WL1273_INV_FREQ(freq->frequency));
1668 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1669 ": set frequency failed with %d\n", r);
1671 r = wl1273_fm_set_tx_freq(radio,
1672 WL1273_INV_FREQ(freq->frequency));
1674 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1675 ": set frequency failed with %d\n", r);
1678 mutex_unlock(&core->lock);
1680 dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
1684 #define WL1273_DEFAULT_SEEK_LEVEL 7
1686 static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1687 struct v4l2_hw_freq_seek *seek)
1689 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1690 struct wl1273_core *core = radio->core;
1693 dev_dbg(radio->dev, "%s\n", __func__);
1695 if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
1698 if (mutex_lock_interruptible(&core->lock))
1701 r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1705 r = wl1273_fm_tx_set_spacing(radio, seek->spacing);
1707 dev_warn(radio->dev, "HW seek failed: %d\n", r);
1709 r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward,
1710 WL1273_DEFAULT_SEEK_LEVEL);
1712 dev_warn(radio->dev, "HW seek failed: %d\n", r);
1715 mutex_unlock(&core->lock);
1719 static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
1720 struct v4l2_modulator *modulator)
1722 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1723 struct wl1273_core *core = radio->core;
1726 dev_dbg(radio->dev, "%s\n", __func__);
1728 if (modulator->index > 0)
1731 if (mutex_lock_interruptible(&core->lock))
1734 r = wl1273_fm_set_mode(radio, WL1273_MODE_TX);
1738 if (modulator->txsubchans & V4L2_TUNER_SUB_RDS)
1739 r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1741 r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1743 if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
1744 r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
1746 r = core->write(core, WL1273_MONO_SET,
1749 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1750 "MONO_SET fails: %d\n", r);
1752 mutex_unlock(&core->lock);
1757 static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
1758 struct v4l2_modulator *modulator)
1760 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1761 struct wl1273_core *core = radio->core;
1765 dev_dbg(radio->dev, "%s\n", __func__);
1767 strlcpy(modulator->name, WL1273_FM_DRIVER_NAME,
1768 sizeof(modulator->name));
1770 modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
1771 modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
1773 modulator->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
1774 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
1776 if (core->mode != WL1273_MODE_TX)
1779 if (mutex_lock_interruptible(&core->lock))
1782 r = core->read(core, WL1273_MONO_SET, &val);
1786 if (val == WL1273_TX_STEREO)
1787 modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
1789 modulator->txsubchans = V4L2_TUNER_SUB_MONO;
1792 modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
1794 mutex_unlock(&core->lock);
1799 static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
1801 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1802 struct wl1273_core *core = radio->core;
1803 struct device *dev = radio->dev;
1807 dev_info(dev, DRIVER_DESC);
1809 if (core->mode == WL1273_MODE_OFF) {
1810 dev_info(dev, "Mode: Off\n");
1814 if (core->mode == WL1273_MODE_SUSPENDED) {
1815 dev_info(dev, "Mode: Suspended\n");
1819 r = core->read(core, WL1273_ASIC_ID_GET, &val);
1821 dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__);
1823 dev_info(dev, "ASIC_ID: 0x%04x\n", val);
1825 r = core->read(core, WL1273_ASIC_VER_GET, &val);
1827 dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__);
1829 dev_info(dev, "ASIC Version: 0x%04x\n", val);
1831 r = core->read(core, WL1273_FIRM_VER_GET, &val);
1833 dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__);
1835 dev_info(dev, "FW version: %d(0x%04x)\n", val, val);
1837 r = core->read(core, WL1273_BAND_SET, &val);
1839 dev_err(dev, "%s: Get BAND fails.\n", __func__);
1841 dev_info(dev, "BAND: %d\n", val);
1843 if (core->mode == WL1273_MODE_TX) {
1844 r = core->read(core, WL1273_PUPD_SET, &val);
1846 dev_err(dev, "%s: Get PUPD fails.\n", __func__);
1848 dev_info(dev, "PUPD: 0x%04x\n", val);
1850 r = core->read(core, WL1273_CHANL_SET, &val);
1852 dev_err(dev, "%s: Get CHANL fails.\n", __func__);
1854 dev_info(dev, "Tx frequency: %dkHz\n", val*10);
1855 } else if (core->mode == WL1273_MODE_RX) {
1856 int bf = radio->rangelow;
1858 r = core->read(core, WL1273_FREQ_SET, &val);
1860 dev_err(dev, "%s: Get FREQ fails.\n", __func__);
1862 dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50);
1864 r = core->read(core, WL1273_MOST_MODE_SET, &val);
1866 dev_err(dev, "%s: Get MOST_MODE fails.\n",
1869 dev_info(dev, "MOST_MODE: Stereo according to blend\n");
1871 dev_info(dev, "MOST_MODE: Force mono output\n");
1873 dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val);
1875 r = core->read(core, WL1273_MOST_BLEND_SET, &val);
1877 dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__);
1880 "MOST_BLEND: Switched blend & hysteresis.\n");
1882 dev_info(dev, "MOST_BLEND: Soft blend.\n");
1884 dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val);
1886 r = core->read(core, WL1273_STEREO_GET, &val);
1888 dev_err(dev, "%s: Get STEREO fails.\n", __func__);
1890 dev_info(dev, "STEREO: Not detected\n");
1892 dev_info(dev, "STEREO: Detected\n");
1894 dev_info(dev, "STEREO: Unexpected value: %d\n", val);
1896 r = core->read(core, WL1273_RSSI_LVL_GET, &val);
1898 dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__);
1900 dev_info(dev, "RX signal strength: %d\n", (s16) val);
1902 r = core->read(core, WL1273_POWER_SET, &val);
1904 dev_err(dev, "%s: Get POWER fails.\n", __func__);
1906 dev_info(dev, "POWER: 0x%04x\n", val);
1908 r = core->read(core, WL1273_INT_MASK_SET, &val);
1910 dev_err(dev, "%s: Get INT_MASK fails.\n", __func__);
1912 dev_info(dev, "INT_MASK: 0x%04x\n", val);
1914 r = core->read(core, WL1273_RDS_SYNC_GET, &val);
1916 dev_err(dev, "%s: Get RDS_SYNC fails.\n",
1919 dev_info(dev, "RDS_SYNC: Not synchronized\n");
1922 dev_info(dev, "RDS_SYNC: Synchronized\n");
1924 dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val);
1926 r = core->read(core, WL1273_I2S_MODE_CONFIG_SET, &val);
1928 dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n",
1931 dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val);
1933 r = core->read(core, WL1273_VOLUME_SET, &val);
1935 dev_err(dev, "%s: Get VOLUME fails.\n", __func__);
1937 dev_info(dev, "VOLUME: 0x%04x\n", val);
1943 static void wl1273_vdev_release(struct video_device *dev)
1947 static const struct v4l2_ctrl_ops wl1273_ctrl_ops = {
1948 .s_ctrl = wl1273_fm_vidioc_s_ctrl,
1949 .g_volatile_ctrl = wl1273_fm_g_volatile_ctrl,
1952 static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
1953 .vidioc_querycap = wl1273_fm_vidioc_querycap,
1954 .vidioc_g_input = wl1273_fm_vidioc_g_input,
1955 .vidioc_s_input = wl1273_fm_vidioc_s_input,
1956 .vidioc_g_audio = wl1273_fm_vidioc_g_audio,
1957 .vidioc_s_audio = wl1273_fm_vidioc_s_audio,
1958 .vidioc_g_tuner = wl1273_fm_vidioc_g_tuner,
1959 .vidioc_s_tuner = wl1273_fm_vidioc_s_tuner,
1960 .vidioc_g_frequency = wl1273_fm_vidioc_g_frequency,
1961 .vidioc_s_frequency = wl1273_fm_vidioc_s_frequency,
1962 .vidioc_s_hw_freq_seek = wl1273_fm_vidioc_s_hw_freq_seek,
1963 .vidioc_g_modulator = wl1273_fm_vidioc_g_modulator,
1964 .vidioc_s_modulator = wl1273_fm_vidioc_s_modulator,
1965 .vidioc_log_status = wl1273_fm_vidioc_log_status,
1968 static struct video_device wl1273_viddev_template = {
1969 .fops = &wl1273_fops,
1970 .ioctl_ops = &wl1273_ioctl_ops,
1971 .name = WL1273_FM_DRIVER_NAME,
1972 .release = wl1273_vdev_release,
1975 static int wl1273_fm_radio_remove(struct platform_device *pdev)
1977 struct wl1273_device *radio = platform_get_drvdata(pdev);
1978 struct wl1273_core *core = radio->core;
1980 dev_info(&pdev->dev, "%s.\n", __func__);
1982 free_irq(core->client->irq, radio);
1983 core->pdata->free_resources();
1985 v4l2_ctrl_handler_free(&radio->ctrl_handler);
1986 video_unregister_device(&radio->videodev);
1987 v4l2_device_unregister(&radio->v4l2dev);
1988 kfree(radio->buffer);
1989 kfree(radio->write_buf);
1995 static int __devinit wl1273_fm_radio_probe(struct platform_device *pdev)
1997 struct wl1273_core **core = pdev->dev.platform_data;
1998 struct wl1273_device *radio;
1999 struct v4l2_ctrl *ctrl;
2002 pr_debug("%s\n", __func__);
2005 dev_err(&pdev->dev, "No platform data.\n");
2010 radio = kzalloc(sizeof(*radio), GFP_KERNEL);
2016 /* RDS buffer allocation */
2017 radio->buf_size = rds_buf * RDS_BLOCK_SIZE;
2018 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
2019 if (!radio->buffer) {
2020 pr_err("Cannot allocate memory for RDS buffer.\n");
2025 radio->core = *core;
2026 radio->irq_flags = WL1273_IRQ_MASK;
2027 radio->dev = &radio->core->client->dev;
2028 radio->rds_on = false;
2029 radio->core->mode = WL1273_MODE_OFF;
2030 radio->tx_power = 118;
2031 radio->core->audio_mode = WL1273_AUDIO_ANALOG;
2032 radio->band = WL1273_BAND_OTHER;
2033 radio->core->i2s_mode = WL1273_I2S_DEF_MODE;
2034 radio->core->channel_number = 2;
2035 radio->core->volume = WL1273_DEFAULT_VOLUME;
2036 radio->rx_frequency = WL1273_BAND_OTHER_LOW;
2037 radio->tx_frequency = WL1273_BAND_OTHER_HIGH;
2038 radio->rangelow = WL1273_BAND_OTHER_LOW;
2039 radio->rangehigh = WL1273_BAND_OTHER_HIGH;
2040 radio->stereo = true;
2041 radio->bus_type = "I2C";
2043 if (radio->core->pdata->request_resources) {
2044 r = radio->core->pdata->request_resources(radio->core->client);
2046 dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2047 ": Cannot get platform data\n");
2051 dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq);
2053 r = request_threaded_irq(radio->core->client->irq, NULL,
2054 wl1273_fm_irq_thread_handler,
2055 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
2056 "wl1273-fm", radio);
2058 dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2059 ": Unable to register IRQ handler: %d\n", r);
2060 goto err_request_irq;
2063 dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ"
2069 init_completion(&radio->busy);
2070 init_waitqueue_head(&radio->read_queue);
2072 radio->write_buf = kmalloc(256, GFP_KERNEL);
2073 if (!radio->write_buf) {
2078 radio->dev = &pdev->dev;
2079 radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
2080 radio->rds_users = 0;
2082 r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
2084 dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
2085 goto device_register_err;
2088 /* V4L2 configuration */
2089 memcpy(&radio->videodev, &wl1273_viddev_template,
2090 sizeof(wl1273_viddev_template));
2092 radio->videodev.v4l2_dev = &radio->v4l2dev;
2094 v4l2_ctrl_handler_init(&radio->ctrl_handler, 6);
2096 /* add in ascending ID order */
2097 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2098 V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1,
2099 WL1273_DEFAULT_VOLUME);
2101 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2102 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
2104 v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops,
2105 V4L2_CID_TUNE_PREEMPHASIS,
2106 V4L2_PREEMPHASIS_75_uS, 0x03,
2107 V4L2_PREEMPHASIS_50_uS);
2109 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2110 V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118);
2112 ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2113 V4L2_CID_TUNE_ANTENNA_CAPACITOR,
2116 ctrl->is_volatile = 1;
2118 if (radio->ctrl_handler.error) {
2119 r = radio->ctrl_handler.error;
2120 dev_err(&pdev->dev, "Ctrl handler error: %d\n", r);
2121 goto handler_init_err;
2124 video_set_drvdata(&radio->videodev, radio);
2125 platform_set_drvdata(pdev, radio);
2127 /* register video device */
2128 r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
2130 dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
2131 ": Could not register video device\n");
2132 goto handler_init_err;
2138 v4l2_ctrl_handler_free(&radio->ctrl_handler);
2139 v4l2_device_unregister(&radio->v4l2dev);
2140 device_register_err:
2141 kfree(radio->write_buf);
2143 free_irq(radio->core->client->irq, radio);
2145 radio->core->pdata->free_resources();
2147 kfree(radio->buffer);
2154 MODULE_ALIAS("platform:wl1273_fm_radio");
2156 static struct platform_driver wl1273_fm_radio_driver = {
2157 .probe = wl1273_fm_radio_probe,
2158 .remove = __devexit_p(wl1273_fm_radio_remove),
2160 .name = "wl1273_fm_radio",
2161 .owner = THIS_MODULE,
2165 static int __init wl1273_fm_module_init(void)
2167 pr_info("%s\n", __func__);
2168 return platform_driver_register(&wl1273_fm_radio_driver);
2170 module_init(wl1273_fm_module_init);
2172 static void __exit wl1273_fm_module_exit(void)
2174 flush_scheduled_work();
2175 platform_driver_unregister(&wl1273_fm_radio_driver);
2176 pr_info(DRIVER_DESC ", Exiting.\n");
2178 module_exit(wl1273_fm_module_exit);
2180 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
2181 MODULE_DESCRIPTION(DRIVER_DESC);
2182 MODULE_LICENSE("GPL");