[PATCH 3/3] V4L2: WL1273 FM Radio: Controls for the FM radio.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This file implements V4L2 controls for using the Texas Instruments
WL1273 FM Radio.

Signed-off-by: Matti J. Aaltonen <matti.j.aaltonen@xxxxxxxxx>
---
 drivers/media/radio/Kconfig        |   15 +
 drivers/media/radio/Makefile       |    1 +
 drivers/media/radio/radio-wl1273.c | 1849 ++++++++++++++++++++++++++++++++++++
 3 files changed, 1865 insertions(+), 0 deletions(-)
 create mode 100644 drivers/media/radio/radio-wl1273.c

diff --git a/drivers/media/radio/Kconfig b/drivers/media/radio/Kconfig
index 83567b8..209fd37 100644
--- a/drivers/media/radio/Kconfig
+++ b/drivers/media/radio/Kconfig
@@ -452,4 +452,19 @@ config RADIO_TIMBERDALE
 	  found behind the Timberdale FPGA on the Russellville board.
 	  Enabling this driver will automatically select the DSP and tuner.
 
+config RADIO_WL1273
+	tristate "Texas Instruments WL1273 I2C FM Radio"
+        depends on I2C && VIDEO_V4L2 && SND
+	select FW_LOADER
+	---help---
+	  Choose Y here if you have this FM radio chip.
+
+	  In order to control your radio card, you will need to use programs
+	  that are compatible with the Video For Linux 2 API.  Information on
+	  this API and pointers to "v4l2" programs may be found at
+	  <file:Documentation/video4linux/API.html>.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called radio-wl1273.
+
 endif # RADIO_ADAPTERS
diff --git a/drivers/media/radio/Makefile b/drivers/media/radio/Makefile
index f615583..d297074 100644
--- a/drivers/media/radio/Makefile
+++ b/drivers/media/radio/Makefile
@@ -26,5 +26,6 @@ obj-$(CONFIG_RADIO_TEA5764) += radio-tea5764.o
 obj-$(CONFIG_RADIO_SAA7706H) += saa7706h.o
 obj-$(CONFIG_RADIO_TEF6862) += tef6862.o
 obj-$(CONFIG_RADIO_TIMBERDALE) += radio-timb.o
+obj-$(CONFIG_RADIO_WL1273) += radio-wl1273.o
 
 EXTRA_CFLAGS += -Isound
diff --git a/drivers/media/radio/radio-wl1273.c b/drivers/media/radio/radio-wl1273.c
new file mode 100644
index 0000000..17b72df
--- /dev/null
+++ b/drivers/media/radio/radio-wl1273.c
@@ -0,0 +1,1849 @@
+/*
+ * Driver for the Texas Instruments WL1273 FM radio.
+ *
+ * Copyright (C) Nokia Corporation
+ * Author: Matti J. Aaltonen <matti.j.aaltonen@xxxxxxxxx>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#undef DEBUG
+
+#include <asm/unaligned.h>
+#include <linux/delay.h>
+#include <linux/firmware.h>
+#include <linux/mfd/wl1273-core.h>
+#include <linux/platform_device.h>
+#include <media/v4l2-common.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-ioctl.h>
+
+#define DRIVER_DESC "Wl1273 FM Radio - V4L2"
+
+#define WL1273_POWER_SET_OFF		0
+#define WL1273_POWER_SET_FM		(1 << 0)
+#define WL1273_POWER_SET_RDS		(1 << 1)
+#define WL1273_POWER_SET_RETENTION	(1 << 4)
+
+#define WL1273_PUPD_SET_OFF		0x00
+#define WL1273_PUPD_SET_ON		0x01
+#define WL1273_PUPD_SET_RETENTION	0x10
+
+#define WL1273_FREQ_MULT		(10000 / 625)
+#define WL1273_INV_FREQ_MULT		(625 / 10000)
+/*
+ * static unsigned char radio_region - Region
+ *
+ * The regions are 0=Japan, 1=USA-Europe. USA-Europe is the default.
+ */
+static unsigned char radio_region = 1;
+module_param(radio_region, byte, 0);
+MODULE_PARM_DESC(radio_region, "Region: 0=Japan, 1=USA-Europe*");
+
+/*
+ * static int radio_nr - The number of the radio device
+ *
+ * The default is 0.
+ */
+static int radio_nr = -1;
+module_param(radio_nr, int, 0);
+MODULE_PARM_DESC(radio_nr, "Radio Nr");
+
+struct wl1273_device {
+	struct v4l2_device v4l2dev;
+	struct video_device videodev;
+	struct device *dev;
+	struct wl1273_core *core;
+	bool rds_on;
+};
+
+static int wl1273_fm_set_tx_freq(struct wl1273_core *core, unsigned int freq)
+{
+	int r = 0;
+
+	if (freq < core->regions[core->region].bottom_frequency) {
+		dev_err(&core->i2c_dev->dev,
+			"Frequency out of range: %d < %d\n",
+			freq, core->regions[core->region].bottom_frequency);
+		return -EDOM;
+	}
+
+	if (freq > core->regions[core->region].top_frequency) {
+		dev_err(&core->i2c_dev->dev,
+			"Frequency out of range: %d > %d\n",
+			freq, core->regions[core->region].top_frequency);
+		return -EDOM;
+	}
+
+	/*
+	 *  The driver works better with this msleep,
+	 *  the documentation doesn't mention it.
+	 */
+	msleep(5);
+	dev_dbg(&core->i2c_dev->dev, "%s: freq: %d kHz\n", __func__, freq);
+
+	INIT_COMPLETION(core->busy);
+	/* Set the current tx channel */
+	r = wl1273_fm_write_cmd(core, WL1273_CHANL_SET, freq / 10);
+	if (r)
+		return r;
+
+	/* wait for the FR IRQ */
+	r = wait_for_completion_timeout(&core->busy, msecs_to_jiffies(1000));
+	if (!r)
+		return -ETIMEDOUT;
+
+	dev_dbg(&core->i2c_dev->dev, "WL1273_CHANL_SET: %d\n", r);
+
+	/* Enable the output power */
+	INIT_COMPLETION(core->busy);
+	r = wl1273_fm_write_cmd(core, WL1273_POWER_ENB_SET, 1);
+	if (r)
+		return r;
+
+	/* wait for the POWER_ENB IRQ */
+	r = wait_for_completion_timeout(&core->busy, msecs_to_jiffies(1000));
+	if (!r)
+		return -ETIMEDOUT;
+
+	core->tx_frequency = freq;
+	dev_dbg(&core->i2c_dev->dev, "WL1273_POWER_ENB_SET: %d\n", r);
+
+	return	0;
+}
+
+static int wl1273_fm_set_rx_freq(struct wl1273_core *core, unsigned int freq)
+{
+	int r;
+	int f;
+
+	if (freq < core->regions[core->region].bottom_frequency) {
+		dev_err(&core->i2c_dev->dev,
+			"Frequency out of range: %d < %d\n",
+			freq, core->regions[core->region].bottom_frequency);
+		r = -EDOM;
+		goto err;
+	}
+
+	if (freq > core->regions[core->region].top_frequency) {
+		dev_err(&core->i2c_dev->dev,
+			"Frequency out of range: %d > %d\n",
+			freq, core->regions[core->region].top_frequency);
+		r = -EDOM;
+		goto err;
+	}
+
+	dev_dbg(&core->i2c_dev->dev, "%s: %dkHz\n", __func__, freq);
+
+	wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET,
+			    core->irq_flags);
+
+	f = (freq - core->regions[core->region].bottom_frequency) / 50;
+	r = wl1273_fm_write_cmd(core, WL1273_FREQ_SET, f);
+	if (r)
+		goto err;
+
+	INIT_COMPLETION(core->busy);
+	r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET,
+				TUNER_MODE_PRESET);
+	if (r) {
+		complete(&core->busy);
+		goto err;
+	}
+
+	r = wait_for_completion_timeout(&core->busy, msecs_to_jiffies(2000));
+	if (!r)
+		return -ETIMEDOUT;
+
+	core->rd_index = 0;
+	core->wr_index = 0;
+	core->rx_frequency = freq;
+	return 0;
+
+err:
+	return r;
+}
+
+static int wl1273_fm_get_freq(struct wl1273_core *core)
+{
+	unsigned int freq;
+	u16 f;
+	int r;
+
+	if (core->mode == WL1273_MODE_RX) {
+		r = wl1273_fm_read_reg(core, WL1273_FREQ_SET, &f);
+		if (r)
+			return r;
+
+		dev_dbg(&core->i2c_dev->dev, "Freq get: 0x%04x\n", f);
+		freq = core->regions[core->region].bottom_frequency + 50 * f;
+	} else {
+		r = wl1273_fm_read_reg(core, WL1273_CHANL_SET, &f);
+		if (r)
+			return r;
+
+		freq = f * 10;
+	}
+
+	return freq;
+}
+
+static int wl1273_fm_set_seek(struct wl1273_core *core,
+			      unsigned int wrap_around,
+			      unsigned int seek_upward)
+{
+	int r;
+	unsigned int dir = (seek_upward == 0) ? 0 : 1;
+	unsigned int rx_frequency;
+
+	rx_frequency = core->rx_frequency;
+	dev_dbg(&core->i2c_dev->dev, "core->rx_frequency: %d\n",
+		rx_frequency);
+
+	if (dir)
+		r = wl1273_fm_set_rx_freq(core, rx_frequency + 100);
+	else
+		r = wl1273_fm_set_rx_freq(core, rx_frequency - 100);
+
+	if (r)
+		goto out;
+
+	INIT_COMPLETION(core->busy);
+	dev_dbg(&core->i2c_dev->dev, "%s: BUSY\n", __func__);
+
+	r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET,
+				core->irq_flags);
+	if (r)
+		goto out;
+
+	dev_dbg(&core->i2c_dev->dev, "%s\n", __func__);
+
+	r = wl1273_fm_write_cmd(core, WL1273_SEARCH_LVL_SET,
+				core->search_level);
+	if (r)
+		goto out;
+
+	r = wl1273_fm_write_cmd(core, WL1273_SEARCH_DIR_SET, dir);
+	if (r)
+		goto out;
+
+	r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET,
+				TUNER_MODE_AUTO_SEEK);
+	if (r)
+		goto out;
+
+	wait_for_completion_timeout(&core->busy, msecs_to_jiffies(1000));
+	if (!(core->irq_received & WL1273_BL_EVENT))
+		goto out;
+
+	core->irq_received &= ~WL1273_BL_EVENT;
+
+	if (!wrap_around)
+		goto out;
+
+	/* Wrap around */
+	dev_dbg(&core->i2c_dev->dev, "Wrap around in HW seek.\n");
+
+	if (seek_upward)
+		rx_frequency = core->regions[core->region].bottom_frequency;
+	else
+		rx_frequency = core->regions[core->region].top_frequency;
+
+	r = wl1273_fm_set_rx_freq(core, rx_frequency);
+	if (r)
+		goto out;
+
+	INIT_COMPLETION(core->busy);
+	dev_dbg(&core->i2c_dev->dev, "%s: BUSY\n", __func__);
+
+	r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET,
+				TUNER_MODE_AUTO_SEEK);
+	if (r)
+		goto out;
+
+	wait_for_completion_timeout(&core->busy, msecs_to_jiffies(1000));
+out:
+	dev_dbg(&core->i2c_dev->dev, "%s: Err: %d\n", __func__, r);
+	return r;
+}
+
+/**
+ * wl1273_fm_set_search_level() - Set the signal strength value that is
+ *				  used to decide if a channel has benn
+ *				  found in automatic seek.
+ * @core:			  A pointer to the device struct.
+ * @level:			  The threshold signal strengt, an eight bit
+ *				  signed value.
+ */
+static int wl1273_fm_set_search_level(struct wl1273_core *core, s16 level)
+{
+	if (core->mode == WL1273_MODE_OFF ||
+	    core->mode == WL1273_MODE_SUSPENDED)
+		return -EPERM;
+
+	if (level < SCHAR_MIN || level > SCHAR_MAX)
+		return -ERANGE;
+
+	core->search_level = level;
+
+	return 0;
+}
+
+/**
+ * wl1273_fm_upload_firmware_patch() -	Upload the firmware.
+ * @core:				A pointer to the device struct.
+ *
+ * The firmware file consists of arrays of bytes where the first byte
+ * gives the array length. The first byte in the file gives the
+ * number of these arrays.
+ */
+static int wl1273_fm_upload_firmware_patch(struct wl1273_core *core)
+{
+	unsigned int packet_num;
+	const struct firmware *fw_p;
+	const char *fw_name = "radio-wl1273-fw.bin";
+	struct i2c_client *client;
+	__u8 *ptr;
+	int i, n, len, r;
+	struct i2c_msg *msgs;
+
+	client = core->i2c_dev;
+	dev_dbg(&client->dev, "%s:\n", __func__);
+
+	if (request_firmware(&fw_p, fw_name, &client->dev)) {
+		dev_info(&client->dev, "%s - %s not found\n", __func__,
+			 fw_name);
+
+		return 0;
+	}
+
+	ptr = (__u8 *) fw_p->data;
+	packet_num = ptr[0];
+	dev_dbg(&client->dev, "%s: packets: %d\n", __func__, packet_num);
+
+	msgs = kmalloc((packet_num + 1)*sizeof(struct i2c_msg), GFP_KERNEL);
+	if (!msgs) {
+		r = -ENOMEM;
+		goto out;
+	}
+
+	i = 1;
+	for (n = 0; n <= packet_num; n++) {
+		len = ptr[i];
+
+		dev_dbg(&client->dev, "%s: len[%d]: %d\n",
+			__func__, n, len);
+
+		if (i + len + 1 <= fw_p->size) {
+			msgs[n].addr = client->addr;
+			msgs[n].flags = 0;
+			msgs[n].len = len;
+			msgs[n].buf = ptr + i + 1;
+		} else {
+			break;
+		}
+
+		i += len + 1;
+	}
+
+	r = i2c_transfer(client->adapter, msgs, packet_num);
+	kfree(msgs);
+
+	if (r != packet_num) {
+		dev_err(&client->dev, "FW upload error: %d\n", r);
+		dev_dbg(&client->dev, "%d != %d\n", packet_num, r);
+
+		r =  -EREMOTEIO;
+		goto out;
+	} else {
+		r = 0;
+	}
+
+	/* ignore possible error here */
+	wl1273_fm_write_cmd(core, WL1273_RESET, 0);
+	dev_dbg(&client->dev, "n: %d, i: %d\n", n, i);
+
+	if (n - 1  != packet_num)
+		dev_warn(&client->dev, "%s - incorrect firmware size.\n",
+			 __func__);
+
+	if (i != fw_p->size)
+		dev_warn(&client->dev, "%s - inconsistent firmware.\n",
+			 __func__);
+
+	dev_dbg(&client->dev, "%s - download OK, r: %d\n", __func__, r);
+
+out:
+	release_firmware(fw_p);
+	return r;
+}
+
+static int wl1273_fm_stop(struct wl1273_core *core)
+{
+	struct i2c_client *client = core->i2c_dev;
+	struct wl1273_fm_platform_data *pdata =
+		client->dev.platform_data;
+	int r = 0;
+
+	if (core->mode == WL1273_MODE_RX)
+		r = wl1273_fm_write_cmd(core, WL1273_POWER_SET,
+					WL1273_POWER_SET_OFF);
+	else if (core->mode == WL1273_MODE_TX)
+		r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
+					WL1273_PUPD_SET_OFF);
+	else
+		r = -EPERM;
+
+	if (r) {
+		dev_err(&core->i2c_dev->dev,
+			"%s: POWER_SET fails: %d\n", __func__, r);
+		goto out;
+	}
+
+	if (pdata->disable)
+		pdata->disable();
+
+	dev_dbg(&core->i2c_dev->dev, "Back to reset\n");
+
+out:
+	return r;
+}
+
+static int wl1273_fm_start(struct wl1273_core *core, int new_mode)
+{
+	struct i2c_client *client = core->i2c_dev;
+	struct wl1273_fm_platform_data *pdata =
+		client->dev.platform_data;
+	int r = -1;
+	u16 val;
+
+	if (pdata->enable) {
+		pdata->enable();
+		msleep(250);
+	}
+
+	if (new_mode == WL1273_MODE_RX) {
+		/* ignore if this fails */
+		r = wl1273_fm_write_cmd(core, WL1273_POWER_SET,
+					WL1273_POWER_SET_FM);
+		if (r)
+			msleep(100);
+
+		r = wl1273_fm_write_cmd(core, WL1273_POWER_SET,
+					WL1273_POWER_SET_FM);
+		if (r) {
+			dev_err(&client->dev, "%s: POWER_SET fails.\n",
+				__func__);
+			goto fail;
+		}
+
+		/* rds buffer configuration */
+		core->wr_index = 0;
+		core->rd_index = 0;
+
+	} else if (new_mode == WL1273_MODE_TX) {
+		/* ignore if this fails */
+		r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
+					WL1273_PUPD_SET_ON);
+		if (r)
+			msleep(100);
+
+		r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
+					WL1273_PUPD_SET_ON);
+		if (r) {
+			dev_err(&client->dev, "%s: PUPD_SET fails.\n",
+				__func__);
+			goto fail;
+		}
+
+	} else {
+		dev_warn(&client->dev, "%s: Illegal mode.\n", __func__);
+	}
+
+	if (core->mode == WL1273_MODE_OFF) {
+		dev_dbg(&core->i2c_dev->dev, "Out of reset\n");
+
+		r = wl1273_fm_upload_firmware_patch(core);
+		if (r)
+			dev_warn(&client->dev, "Firmware upload failed.\n");
+	}
+
+	r = wl1273_fm_read_reg(core, WL1273_ASIC_ID_GET, &val);
+	if (r)
+		dev_err(&client->dev, "%s: WL1273_ASIC_ID_GET fails.\n",
+			__func__);
+	else
+		dev_dbg(&client->dev, "%s: ASIC ID: 0x%04x\n", __func__, val);
+
+	r = wl1273_fm_read_reg(core, WL1273_ASIC_VER_GET, &val);
+	if (r)
+		dev_err(&client->dev, "%s: WL1273_ASIC_VER_GET fails.\n",
+			__func__);
+	else
+		dev_dbg(&client->dev, "%s: ASIC Version: 0x%04x\n", __func__,
+			val);
+
+	r = wl1273_fm_read_reg(core, WL1273_FIRM_VER_GET, &val);
+	if (r)
+		dev_err(&client->dev, "%s: WL1273_FIRM_VER_GET fails.\n",
+			__func__);
+	else
+		dev_dbg(&client->dev, "%s: FW version: %d(0x%04x)\n", __func__,
+			val, val);
+
+	r = wl1273_fm_read_reg(core, WL1273_INT_MASK_SET, &val);
+	if (r)
+		goto fail;
+
+	dev_dbg(&client->dev, "%s: Irq mask: 0x%04x\n", __func__, val);
+
+	return 0;
+fail:
+	if (pdata->disable)
+		pdata->disable();
+
+	dev_dbg(&client->dev, "%s: return: %d\n", __func__, r);
+	return r;
+}
+
+static int wl1273_fm_suspend(struct wl1273_core *core)
+{
+	int r = 0;
+	struct i2c_client *client = core->i2c_dev;
+	struct wl1273_fm_platform_data *pdata =
+		client->dev.platform_data;
+
+	/* Cannot go from OFF to SUSPENDED */
+	if (core->mode == WL1273_MODE_RX)
+		r = wl1273_fm_write_cmd(core, WL1273_POWER_SET,
+					WL1273_POWER_SET_RETENTION);
+	else if (core->mode == WL1273_MODE_TX)
+		r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
+					WL1273_PUPD_SET_RETENTION);
+	else
+		r = -EINVAL;
+
+	if (r) {
+		dev_err(&core->i2c_dev->dev,
+			"%s: POWER_SET fails: %d\n", __func__, r);
+		goto out;
+	}
+
+out:
+	if (pdata->disable)
+		pdata->disable();
+
+	dev_dbg(&core->i2c_dev->dev, "Back to reset\n");
+
+	return r;
+}
+
+static int wl1273_fm_set_mode(struct wl1273_core *core, int mode)
+{
+	int r;
+	int old_mode;
+
+	dev_dbg(&core->i2c_dev->dev, "%s\n", __func__);
+
+	if (core->mode == mode)
+		return 0;
+
+	mutex_lock(&core->lock);
+	old_mode = core->mode;
+
+	switch (mode) {
+	case WL1273_MODE_RX:
+	case WL1273_MODE_TX:
+		r = wl1273_fm_start(core, mode);
+		if (r) {
+			dev_err(&core->i2c_dev->dev, "%s: Cannot start.\n",
+				__func__);
+			wl1273_fm_stop(core);
+			core->mode = old_mode ;
+			goto out;
+		} else {
+			core->mode = mode;
+			r = wl1273_fm_set_audio(core, core->audio_mode);
+			if (r)
+				dev_err(&core->i2c_dev->dev,
+					"Cannot set audio mode.\n");
+		}
+
+		r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET,
+					core->irq_flags);
+		if (r) {
+			dev_err(&core->i2c_dev->dev,
+				"INT_MASK_SET fails.\n");
+			goto out;
+		}
+
+		/* remember previous frequencies */
+		if (mode == WL1273_MODE_RX) {
+			r = wl1273_fm_set_rx_freq(core, core->rx_frequency);
+			if (r) {
+				dev_err(&core->i2c_dev->dev,
+					"set freq fails: %d.\n", r);
+				goto out;
+			}
+		} else {
+			r = wl1273_fm_set_tx_freq(core, core->tx_frequency);
+			if (r) {
+				dev_err(&core->i2c_dev->dev,
+					"set freq fails: %d.\n", r);
+				goto out;
+			}
+		}
+
+		break;
+
+	case WL1273_MODE_OFF:
+		r = wl1273_fm_stop(core);
+		if (r) {
+			dev_err(&core->i2c_dev->dev,
+				"%s: Off fails: %d\n", __func__, r);
+			core->mode = old_mode;
+		} else {
+			core->mode = WL1273_MODE_OFF;
+		}
+		break;
+
+	case WL1273_MODE_SUSPENDED:
+		r = wl1273_fm_suspend(core);
+		if (r) {
+			dev_err(&core->i2c_dev->dev,
+				"%s: Suspend fails: %d\n", __func__, r);
+			core->mode = old_mode;
+
+		} else {
+			core->mode = WL1273_MODE_SUSPENDED;
+		}
+		break;
+
+	default:
+		dev_err(&core->i2c_dev->dev, "%s: Unknown mode: %d\n",
+			__func__, mode);
+		r = -EINVAL;
+		break;
+	}
+
+out:
+	mutex_unlock(&core->lock);
+
+	return r;
+}
+
+/**
+ * wl1273_fm_set_region() -	Change the current region.
+ * @core:			A pointer to the device structure.
+ * @region:			The ID of the new region.
+ *
+ * Wl1273 supports only two regions USA/Europe and Japan.
+ */
+static int wl1273_fm_set_region(struct wl1273_core *core, unsigned int region)
+{
+	int r = 0;
+	unsigned int new_frequency = 0;
+
+	dev_err(&core->i2c_dev->dev, "%s: number of resion: %d\n", __func__,
+		core->number_of_regions);
+
+	if (region == core->region)
+		return 0;
+
+	if (core->mode == WL1273_MODE_OFF ||
+	    core->mode == WL1273_MODE_SUSPENDED)
+		return -EPERM;
+
+	if (region >= core->number_of_regions)
+		return -EINVAL;
+
+	mutex_lock(&core->lock);
+
+	core->region = region;
+
+	if (core->rx_frequency < core->regions[core->region].bottom_frequency)
+		new_frequency = core->regions[core->region].bottom_frequency;
+	else if (core->rx_frequency > core->regions[core->region].top_frequency)
+		new_frequency = core->regions[core->region].top_frequency;
+
+	if (new_frequency) {
+		core->rx_frequency = new_frequency;
+		if (core->mode == WL1273_MODE_RX) {
+			r = wl1273_fm_set_rx_freq(core, new_frequency);
+			if (r)
+				goto out;
+		}
+	}
+
+	new_frequency = 0;
+	if (core->tx_frequency < core->regions[core->region].bottom_frequency)
+		new_frequency = core->regions[core->region].bottom_frequency;
+	else if (core->tx_frequency > core->regions[core->region].top_frequency)
+		new_frequency = core->regions[core->region].top_frequency;
+
+	if (new_frequency) {
+		core->tx_frequency = new_frequency;
+
+		if (core->mode == WL1273_MODE_TX) {
+			r = wl1273_fm_set_tx_freq(core, new_frequency);
+			if (r)
+				goto out;
+		}
+	}
+
+out:
+	mutex_unlock(&core->lock);
+	return r;
+}
+
+/**
+ * wl1273_fm_get_tx_ctune() -	Get the TX tuning capacitor value.
+ * @core:			A pointer to the device struct.
+ */
+static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_core *core)
+{
+	struct i2c_client *client = core->i2c_dev;
+	u16 val;
+	int r;
+
+	if (core->mode == WL1273_MODE_OFF ||
+	    core->mode == WL1273_MODE_SUSPENDED)
+		return -EPERM;
+
+	r = wl1273_fm_read_reg(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
+	if (r) {
+		dev_err(&client->dev, "%s: I2C error: %d\n", __func__, r);
+		goto out;
+	}
+
+out:
+	return val;
+}
+
+/**
+ * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
+ * @core:			 A pointer to the device struct.
+ * @preemphasis:		 The new pre-amphasis value.
+ *
+ * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
+ * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
+ */
+static int wl1273_fm_set_preemphasis(struct wl1273_core *core,
+				     unsigned int preemphasis)
+{
+	int r;
+	u16 em;
+
+	if (core->mode == WL1273_MODE_OFF ||
+	    core->mode == WL1273_MODE_SUSPENDED)
+		return -EPERM;
+
+	mutex_lock(&core->lock);
+
+	switch (preemphasis) {
+	case V4L2_PREEMPHASIS_DISABLED:
+		em = 1;
+		break;
+	case V4L2_PREEMPHASIS_50_uS:
+		em = 0;
+		break;
+	case V4L2_PREEMPHASIS_75_uS:
+		em = 2;
+		break;
+	default:
+		r = -EINVAL;
+		goto out;
+	}
+
+	r = wl1273_fm_write_cmd(core, WL1273_PREMPH_SET, em);
+	if (r)
+		goto out;
+
+	core->preemphasis = preemphasis;
+
+out:
+	mutex_unlock(&core->lock);
+	return r;
+}
+
+static int wl1273_fm_rds_on(struct wl1273_core *core)
+{
+	int r;
+
+	dev_dbg(&core->i2c_dev->dev, "%s\n", __func__);
+
+	if (core->mode == WL1273_MODE_TX)
+		return 0;
+
+	core->irq_flags |= WL1273_RDS_EVENT;
+
+	r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, core->irq_flags);
+	if (r)
+		goto err;
+
+	r = wl1273_fm_write_cmd(core, WL1273_POWER_SET,
+				WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
+	if (r)
+		goto err;
+
+	r = wl1273_fm_set_rx_freq(core, core->rx_frequency);
+	if (r) {
+		dev_err(&core->i2c_dev->dev,
+			"set freq fails: %d.\n", r);
+		goto err;
+	}
+	return 0;
+
+err:
+	dev_err(&core->i2c_dev->dev, "%s: Err: %d\n", __func__, r);
+	return r;
+}
+
+static int wl1273_fm_rds_off(struct wl1273_core *core)
+{
+	struct device *dev = &core->i2c_dev->dev;
+	int r;
+
+	if (core->mode == WL1273_MODE_TX)
+		return 0;
+
+	wait_for_completion_timeout(&core->busy, msecs_to_jiffies(1000));
+
+	core->irq_flags &= ~WL1273_RDS_EVENT;
+
+	r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET,
+				core->irq_flags);
+	if (r)
+		goto end;
+
+	/* stop rds reception */
+	cancel_delayed_work(&core->work);
+
+	/* Service pending read */
+	wake_up_interruptible(&core->read_queue);
+
+	dev_dbg(dev, "%s\n", __func__);
+
+	r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
+
+end:
+	dev_dbg(dev, "%s: exiting...\n", __func__);
+
+	return r;
+}
+
+static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf,
+				    size_t count, loff_t *ppos)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+	unsigned char *s;
+	u16 val;
+	int r;
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	if (radio->core->mode == WL1273_MODE_RX)
+		return count;
+
+	if (mutex_lock_interruptible(&radio->core->lock))
+		return -EINTR;
+
+	if (!radio->rds_on) {
+		r = wl1273_fm_rds_on(radio->core);
+		if (r)
+			goto out;
+
+		radio->rds_on = true;
+	}
+
+	/* Manual Mode */
+	if (count > 255)
+		val = 255;
+
+	wl1273_fm_write_cmd(radio->core, WL1273_RDS_CONFIG_DATA_SET, val);
+
+	s = kmalloc(val + 1, GFP_KERNEL);
+	if (!s) {
+		r = -ENOMEM;
+		goto out;
+	}
+
+	if (copy_from_user(s + 1, buf, count)) {
+		kfree(s);
+		r = -EFAULT;
+		goto out;
+	}
+
+	dev_dbg(radio->dev, "Count: %d\n", count);
+	dev_dbg(radio->dev, "From user: \"%s\"\n", s);
+
+	s[0] = WL1273_RDS_DATA_SET;
+	wl1273_fm_write_data(radio->core, s, val + 1);
+
+	kfree(s);
+	r = val;
+
+out:
+	mutex_unlock(&radio->core->lock);
+
+	return r;
+}
+
+static unsigned int wl1273_fm_fops_poll(struct file *file,
+					struct poll_table_struct *pts)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+	struct wl1273_core *core = radio->core;
+	unsigned int rd_index, wr_index;
+
+	/* TODO: handle the case of multiple readers */
+
+	poll_wait(file, &core->read_queue, pts);
+
+	rd_index = core->rd_index;
+	wr_index = core->wr_index;
+	if (rd_index != wr_index)
+		return POLLIN | POLLRDNORM;
+
+	return 0;
+}
+
+static int wl1273_fm_fops_open(struct file *file)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	return 0;
+}
+
+static int wl1273_fm_fops_release(struct file *file)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+	struct wl1273_core *core = radio->core;
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	if (radio->rds_on) {
+		if (mutex_lock_interruptible(&core->lock))
+			return -EINTR;
+
+		wl1273_fm_rds_off(core);
+		radio->rds_on = false;
+		mutex_unlock(&core->lock);
+	}
+
+	return 0;
+}
+
+static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
+				   size_t count, loff_t *ppos)
+{
+	int r = 0;
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+	struct wl1273_core *core = radio->core;
+	unsigned int block_count = 0;
+
+	/* TODO: handle the case of multiple readers */
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	if (radio->core->mode == WL1273_MODE_TX)
+		return 0;
+
+	if (mutex_lock_interruptible(&core->lock))
+		return -EINTR;
+
+	if (!radio->rds_on) {
+		r = wl1273_fm_rds_on(core);
+		if (r)
+			return r;
+
+		radio->rds_on = true;
+	}
+
+	/* block if no new data available */
+	while (core->wr_index == core->rd_index) {
+		if (file->f_flags & O_NONBLOCK) {
+			r = -EWOULDBLOCK;
+			goto out;
+		}
+
+		if (wait_event_interruptible(core->read_queue,
+					     core->wr_index !=
+					     core->rd_index) < 0) {
+			r = -EINTR;
+			goto out;
+		}
+	}
+
+	/* calculate block count from byte count */
+	count /= 3;
+
+	/* copy RDS blocks from the internal buffer and to user buffer */
+
+	while (block_count < count) {
+		if (core->rd_index == core->wr_index)
+			break;
+
+		/* always transfer complete RDS blocks */
+		if (copy_to_user(buf, &core->buffer[core->rd_index], 3))
+			break;
+
+		/* increment and wrap the read pointer */
+		core->rd_index += 3;
+		if (core->rd_index >= core->buf_size)
+			core->rd_index = 0;
+
+		/* increment counters */
+		block_count++;
+		buf += 3;
+		r += 3;
+	}
+
+out:
+	dev_dbg(radio->dev, "%s: exit\n", __func__);
+	mutex_unlock(&core->lock);
+
+	return r;
+}
+
+static const struct v4l2_file_operations wl1273_fops = {
+	.owner		= THIS_MODULE,
+	.read		= wl1273_fm_fops_read,
+	.write		= wl1273_fm_fops_write,
+	.poll		= wl1273_fm_fops_poll,
+	.ioctl		= video_ioctl2,
+	.open		= wl1273_fm_fops_open,
+	.release	= wl1273_fm_fops_release,
+};
+
+static struct v4l2_queryctrl wl1273_v4l2_queryctrl[] = {
+	{
+		.id		= V4L2_CID_AUDIO_VOLUME,
+		.type		= V4L2_CTRL_TYPE_INTEGER,
+		.name		= "Volume",
+		.minimum	= 0,
+		.maximum	= 0xffff,
+		.step		= 1,
+		.default_value	= 0x78b8,
+	},
+	{
+		.id		= V4L2_CID_AUDIO_MUTE,
+		.type		= V4L2_CTRL_TYPE_BOOLEAN,
+		.name		= "Mute",
+		.minimum	= 0,
+		.maximum	= 1,
+		.step		= 1,
+		.default_value	= 1,
+	},
+	{
+		.id		= V4L2_CID_AUDIO_LOUDNESS,
+		.flags		= V4L2_CTRL_FLAG_DISABLED,
+	},
+};
+
+static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
+				     struct v4l2_capability *capability)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+	struct wl1273_core *core = radio->core;
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	strlcpy(capability->driver, WL1273_FM_DRIVER_NAME,
+		sizeof(capability->driver));
+	strlcpy(capability->card, "Texas Instruments Wl1273 FM Radio",
+		sizeof(capability->card));
+	sprintf(capability->bus_info, "I2C");
+
+	capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
+		V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
+		V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
+		V4L2_CAP_RDS_OUTPUT;
+
+	return 0;
+}
+
+static int wl1273_fm_vidioc_g_input(struct file *file, void *priv,
+				    unsigned int *i)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	*i = 0;
+
+	return 0;
+}
+
+static int wl1273_fm_vidioc_s_input(struct file *file, void *priv,
+				    unsigned int i)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	if (i != 0)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int wl1273_fm_vidioc_queryctrl(struct file *file, void *priv,
+				      struct v4l2_queryctrl *qc)
+{
+	unsigned char i;
+	int r = -EINVAL;
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	for (i = 0; i < ARRAY_SIZE(wl1273_v4l2_queryctrl); i++) {
+		if (qc->id && qc->id == wl1273_v4l2_queryctrl[i].id) {
+			memcpy(qc, &wl1273_v4l2_queryctrl[i], sizeof(*qc));
+			r = 0;
+			break;
+		}
+	}
+	if (r < 0)
+		dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
+			 ": query control failed with %d\n", r);
+	return r;
+}
+
+/**
+ * wl1273_fm_set_tx_power() - 	Set the transmission power value.
+ * @core:			A pointer to the device struct.
+ * @power:			The new power value.
+ */
+static int wl1273_fm_set_tx_power(struct wl1273_core *core, u16 power)
+{
+	int r;
+
+	if (core->mode == WL1273_MODE_OFF ||
+	    core->mode == WL1273_MODE_SUSPENDED)
+		return -EPERM;
+
+	mutex_lock(&core->lock);
+
+	r = wl1273_fm_write_cmd(core, WL1273_POWER_LEV_SET, power);
+	if (r)
+		goto out;
+
+	core->tx_power = power;
+
+out:
+	mutex_unlock(&core->lock);
+	return r;
+}
+
+/**
+ * wl1273_fm_tx_set_spacing() -	Get TX channel spacing.
+ * @core:			A pointer to the device struct.
+ * @spacing:			The new channel spacing value.
+ *
+ * Available spacing values are WL1273_SPACING_50KHZ, WL1273_SPACING_100KHZ
+ * and WL1273_SPACING_200KHZ.
+ */
+static int wl1273_fm_tx_set_spacing(struct wl1273_core *core,
+				    unsigned int spacing)
+{
+	int r;
+
+	if (core->mode == WL1273_MODE_OFF ||
+	    core->mode == WL1273_MODE_SUSPENDED)
+		return -EPERM;
+
+	mutex_lock(&core->lock);
+
+	switch (spacing) {
+	case WL1273_SPACING_50KHZ:
+		r = wl1273_fm_write_cmd(core, WL1273_CHANL_BW_SET, 1);
+		if (r)
+			goto out;
+		break;
+	case WL1273_SPACING_100KHZ:
+		r = wl1273_fm_write_cmd(core, WL1273_CHANL_BW_SET, 2);
+		if (r)
+			goto out;
+		break;
+	case WL1273_SPACING_200KHZ:
+		r = wl1273_fm_write_cmd(core, WL1273_CHANL_BW_SET, 4);
+		if (r)
+			goto out;
+		break;
+	default:
+		r = -EINVAL;
+		goto out;
+		break;
+	}
+
+	core->spacing = spacing;
+
+out:
+	mutex_unlock(&core->lock);
+	return r;
+}
+
+/**
+ * wl1273_fm_set_tx_rds() -	Get the currect TX RDS mode.
+ * @core:			A pointer to the device structure.
+ * @new_mode:			Set the mode to new_mode.
+ *
+ * Possible values for new_mode are WL1273_RDS_TX_OFF, WL1273_RDS_TX_ON
+ * and WL1273_RDS_TX_RESET. WL1273_RDS_TX_RESET causes the RDS subsystem to
+ * reset but doesn't actually change the mode.
+ */
+static int wl1273_fm_set_tx_rds(struct wl1273_core *core, unsigned int new_mode)
+{
+	int r = 0;
+	struct i2c_client *client = core->i2c_dev;
+
+	if (core->mode == WL1273_MODE_OFF ||
+	    core->mode == WL1273_MODE_SUSPENDED)
+		return -EPERM;
+
+	mutex_lock(&core->lock);
+
+	switch (new_mode) {
+	case WL1273_RDS_TX_OFF:
+		r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 0);
+		core->rds_tx_mode = WL1273_RDS_TX_OFF;
+		break;
+
+	case WL1273_RDS_TX_ON:
+		r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 1);
+		core->rds_tx_mode = WL1273_RDS_TX_ON;
+		break;
+
+	case WL1273_RDS_TX_RESET:
+		r = wl1273_fm_write_cmd(core, WL1273_RDS_CNTRL_SET, 1);
+		break;
+
+	default:
+		dev_err(&client->dev, "%s: Unknown mode: %d\n", __func__,
+			new_mode);
+		break;
+	}
+
+	mutex_unlock(&core->lock);
+	return r;
+}
+
+static int wl1273_fm_vidioc_g_ctrl(struct file *file, void *priv,
+				   struct v4l2_control *ctrl)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+	struct wl1273_core *core = radio->core;
+	u16 val;
+	int r = 0;
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	if (mutex_lock_interruptible(&core->lock))
+		return -EINTR;
+
+	switch (ctrl->id) {
+	case V4L2_CID_AUDIO_MUTE:
+		if (core->mode == WL1273_MODE_RX)
+			r = wl1273_fm_read_reg(core, WL1273_MUTE_STATUS_SET,
+					       &val);
+		else
+			r = wl1273_fm_read_reg(core, WL1273_MUTE, &val);
+
+		if (r)
+			goto out;
+
+		dev_dbg(radio->dev,
+			"MUTE STATUS GET: 0x%02x\n", val);
+
+		if (val)
+			ctrl->value = 1;
+		else
+			ctrl->value = 0;
+
+		break;
+
+	case V4L2_CID_AUDIO_VOLUME:
+		ctrl->value = core->volume;
+		break;
+
+	case WL1273_CID_FM_RADIO_MODE:
+		ctrl->value = core->mode;
+		break;
+
+	case WL1273_CID_FM_REGION:
+		ctrl->value = core->region;
+		break;
+
+	case WL1273_CID_FM_CHAN_SPACING:
+		ctrl->value = core->spacing;
+		break;
+
+	case WL1273_CID_FM_RDS_CTRL:
+		ctrl->value = core->rds_tx_mode;
+		break;
+
+	case WL1273_CID_FM_CTUNE_VAL:
+		ctrl->value = wl1273_fm_get_tx_ctune(core);
+		break;
+
+	case WL1273_CID_TUNE_PREEMPHASIS:
+		ctrl->value = core->preemphasis;
+		break;
+
+	case WL1273_CID_TX_POWER:
+		ctrl->value = core->tx_power;
+		break;
+
+	case WL1273_CID_SEARCH_LVL:
+		ctrl->value = core->search_level;
+		break;
+
+	default:
+		dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
+			 __func__, ctrl->id);
+		break;
+	}
+
+out:
+	mutex_unlock(&core->lock);
+
+	return r;
+}
+
+#define WL1273_MUTE_SOFT_ENABLE    (1 << 0)
+#define WL1273_MUTE_AC             (1 << 1)
+#define WL1273_MUTE_HARD_LEFT      (1 << 2)
+#define WL1273_MUTE_HARD_RIGHT     (1 << 3)
+#define WL1273_MUTE_SOFT_FORCE     (1 << 4)
+
+static int wl1273_fm_vidioc_s_ctrl(struct file *file, void *priv,
+				   struct v4l2_control *ctrl)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+	struct wl1273_core *core = radio->core;
+	int r = 0;
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	switch (ctrl->id) {
+	case V4L2_CID_AUDIO_MUTE:
+		if (mutex_lock_interruptible(&core->lock))
+			return -EINTR;
+
+		if (core->mode == WL1273_MODE_RX && ctrl->value)
+			r = wl1273_fm_write_cmd(core,
+						WL1273_MUTE_STATUS_SET,
+						WL1273_MUTE_HARD_LEFT |
+						WL1273_MUTE_HARD_RIGHT);
+		else if (core->mode == WL1273_MODE_RX)
+			r = wl1273_fm_write_cmd(core,
+						WL1273_MUTE_STATUS_SET, 0x0);
+		else if (core->mode == WL1273_MODE_TX && ctrl->value)
+			r = wl1273_fm_write_cmd(core, WL1273_MUTE, 1);
+		else if (core->mode == WL1273_MODE_TX)
+			r = wl1273_fm_write_cmd(core, WL1273_MUTE, 0);
+
+		mutex_unlock(&core->lock);
+		break;
+
+	case V4L2_CID_AUDIO_VOLUME: {
+		u16 val;
+
+		r = wl1273_fm_read_reg(core, WL1273_VOLUME_SET, &val);
+		if (r)
+			break;
+
+		ctrl->value = val;
+		break;
+	}
+	case WL1273_CID_FM_RADIO_MODE:
+		dev_dbg(radio->dev, "WL1273_CID_FM_RADIO_MODE\n");
+		r = wl1273_fm_set_mode(core, ctrl->value);
+		break;
+
+	case WL1273_CID_FM_REGION:
+		r = wl1273_fm_set_region(core, ctrl->value);
+		break;
+
+	case WL1273_CID_FM_CHAN_SPACING:
+		r = wl1273_fm_tx_set_spacing(core, ctrl->value);
+		break;
+
+	case WL1273_CID_FM_RDS_CTRL:
+		r = wl1273_fm_set_tx_rds(core, ctrl->value);
+		break;
+
+	case WL1273_CID_TUNE_PREEMPHASIS:
+		r = wl1273_fm_set_preemphasis(core, ctrl->value);
+		break;
+
+	case WL1273_CID_TX_POWER:
+		r = wl1273_fm_set_tx_power(core, ctrl->value);
+		break;
+
+	case WL1273_CID_SEARCH_LVL:
+		r = wl1273_fm_set_search_level(core, ctrl->value);
+		break;
+
+	default:
+		dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
+			 __func__, ctrl->id);
+		break;
+	}
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+	return r;
+}
+
+static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv,
+				    struct v4l2_audio *audio)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	if (audio->index > 1)
+		return -EINVAL;
+
+	strcpy(audio->name, "Radio");
+	audio->capability = V4L2_AUDCAP_STEREO;
+
+	return 0;
+}
+
+static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
+				    struct v4l2_audio *audio)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	if (audio->index != 0)
+		return -EINVAL;
+
+	return 0;
+}
+
+#define WL1273_RDS_NOT_SYNCHRONIZED 0
+#define WL1273_RDS_SYNCHRONIZED 1
+
+static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv,
+				    struct v4l2_tuner *tuner)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+	struct wl1273_core *core = radio->core;
+	u16 val;
+	int r;
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	if (tuner->index > 0)
+		return -EINVAL;
+
+	strcpy(tuner->name, WL1273_FM_DRIVER_NAME);
+	tuner->type = V4L2_TUNER_RADIO;
+
+	tuner->rangelow	=
+		core->regions[core->region].bottom_frequency * WL1273_FREQ_MULT;
+	tuner->rangehigh =
+		core->regions[core->region].top_frequency * WL1273_FREQ_MULT;
+
+	tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
+	tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS;
+
+	if (core->mode != WL1273_MODE_RX)
+		return 0;
+
+	if (mutex_lock_interruptible(&core->lock))
+		return -EINTR;
+
+	r = wl1273_fm_read_reg(core, WL1273_RSSI_LVL_GET, &val);
+	if (r)
+		goto out;
+
+	tuner->signal = val * 256;
+	dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);
+
+	tuner->afc = 0;
+
+	r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val);
+	if (r)
+		goto out;
+
+	if (val == WL1273_RDS_SYNCHRONIZED)
+		tuner->rxsubchans = V4L2_TUNER_SUB_RDS;
+out:
+	mutex_unlock(&core->lock);
+
+	return r;
+}
+
+static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
+				    struct v4l2_tuner *tuner)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+	struct wl1273_core *core = radio->core;
+	int r = 0;
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	if (tuner->index > 0)
+		return -EINVAL;
+
+	if (mutex_lock_interruptible(&core->lock))
+		return -EINTR;
+
+	if (tuner->audmode == V4L2_TUNER_MODE_MONO)
+		r = wl1273_fm_write_cmd(core, WL1273_MOST_MODE_SET,
+					WL1273_RX_MONO);
+	else
+		r = wl1273_fm_write_cmd(core, WL1273_MOST_MODE_SET,
+					WL1273_RX_STEREO);
+
+	if (r < 0)
+		dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
+			 ": set tuner mode failed with %d\n", r);
+
+	mutex_unlock(&core->lock);
+
+	return r;
+}
+
+static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
+					struct v4l2_frequency *freq)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+	struct wl1273_core *core = radio->core;
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	if (mutex_lock_interruptible(&core->lock))
+		return -EINTR;
+
+	freq->type = V4L2_TUNER_RADIO;
+	freq->frequency = wl1273_fm_get_freq(core) * WL1273_FREQ_MULT;
+
+	mutex_unlock(&core->lock);
+
+	return 0;
+}
+
+static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
+					struct v4l2_frequency *freq)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+	struct wl1273_core *core = radio->core;
+	int r;
+
+	dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency);
+
+	if (freq->type != V4L2_TUNER_RADIO) {
+		dev_dbg(radio->dev,
+			"freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
+		return -EINVAL;
+	}
+
+	if (mutex_lock_interruptible(&core->lock))
+		return -EINTR;
+
+	wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, core->irq_flags);
+
+	if (core->mode == WL1273_MODE_RX) {
+		r = wl1273_fm_set_rx_freq(core, freq->frequency *
+					  WL1273_INV_FREQ_MULT);
+		if (r)
+			dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
+				 ": set frequency failed with %d\n", r);
+	} else {
+		r = wl1273_fm_set_tx_freq(core, freq->frequency *
+					  WL1273_INV_FREQ_MULT);
+		if (r)
+			dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
+				 ": set frequency failed with %d\n", r);
+	}
+
+	mutex_unlock(&core->lock);
+
+	dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
+	return r;
+}
+
+static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
+					   struct v4l2_hw_freq_seek *seek)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+	struct wl1273_core *core = radio->core;
+	int r;
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	if (core->mode != WL1273_MODE_RX)
+		return 0;
+
+	if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
+		return -EINVAL;
+
+	if (mutex_lock_interruptible(&core->lock))
+		return -EINTR;
+
+	r = wl1273_fm_set_seek(core, seek->wrap_around, seek->seek_upward);
+	if (r)
+		dev_warn(radio->dev, "HW seek failed: %d\n", r);
+
+	mutex_unlock(&core->lock);
+
+	return r;
+}
+
+static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
+					struct v4l2_modulator *modulator)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+	struct wl1273_core *core = radio->core;
+	int r = 0;
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	if (modulator->index > 0)
+		return -EINVAL;
+
+	if (mutex_lock_interruptible(&core->lock))
+		return -EINTR;
+
+	mutex_unlock(&core->lock);
+
+	return r;
+}
+
+static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
+					struct v4l2_modulator *modulator)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+	struct wl1273_core *core = radio->core;
+
+	dev_dbg(radio->dev, "%s\n", __func__);
+
+	if (mutex_lock_interruptible(&core->lock))
+		return -EINTR;
+
+	modulator->rangelow =
+		core->regions[core->region].bottom_frequency * WL1273_FREQ_MULT;
+	modulator->rangehigh =
+		core->regions[core->region].top_frequency * WL1273_FREQ_MULT;
+
+	modulator->capability = V4L2_TUNER_CAP_RDS;
+	mutex_unlock(&core->lock);
+
+	return 0;
+}
+
+static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
+{
+	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
+	struct wl1273_core *core = radio->core;
+	u16 val;
+	int r;
+
+	dev_info(radio->dev, DRIVER_DESC);
+
+	r = wl1273_fm_read_reg(core, WL1273_ASIC_ID_GET, &val);
+	if (r)
+		dev_err(radio->dev, "%s: WL1273_ASIC_ID_GET fails.\n",
+			__func__);
+	else
+		dev_info(radio->dev, "ASIC ID: 0x%04x\n", val);
+
+	r = wl1273_fm_read_reg(core, WL1273_ASIC_VER_GET, &val);
+	if (r)
+		dev_err(radio->dev, "%s: WL1273_ASIC_VER_GET fails.\n",
+			__func__);
+	else
+		dev_info(radio->dev, "ASIC Version: 0x%04x\n", val);
+
+	r = wl1273_fm_read_reg(core, WL1273_FIRM_VER_GET, &val);
+	if (r)
+		dev_err(radio->dev, "%s: WL1273_FIRM_VER_GET fails.\n",
+			__func__);
+	else
+		dev_info(radio->dev, "FW version: %d(0x%04x)\n", val, val);
+
+	/* TODO: Add TX stuff */
+	if (core->mode != WL1273_MODE_RX)
+		return 0;
+
+	r = wl1273_fm_read_reg(core, WL1273_STEREO_GET, &val);
+	if (r)
+		dev_err(radio->dev, "%s: WL1273_STEREO_GET fails.\n",
+			__func__);
+	else if (val == 0)
+		dev_info(radio->dev, "Stereo not detected\n");
+
+	else if (val == 1)
+		dev_info(radio->dev, "Stereo detected\n");
+	else
+		dev_info(radio->dev, "STEREO_GET: "
+			 "unexpected value: %d\n", val);
+
+	r = wl1273_fm_read_reg(core, WL1273_RSSI_LVL_GET, &val);
+	if (r)
+		dev_err(radio->dev, "%s: WL1273_RSSI_LVL_GET fails.\n",
+			__func__);
+	else
+		dev_info(radio->dev, "RX signal strength: %d\n", val);
+
+	r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val);
+	if (r)
+		dev_err(radio->dev, "%s: WL1273_RDS_SYNC_GET fails.\n",
+			__func__);
+	else if (val == 0)
+		dev_info(radio->dev, "RDS not synchronized\n");
+
+	else if (val == 1)
+		dev_info(radio->dev, "RDS is synchronized\n");
+	else
+		dev_info(radio->dev, "RDS_SYNC_GET: "
+			 "unexpected value: %d\n", val);
+
+	return 0;
+}
+
+static void wl1273_vdev_release(struct video_device *dev)
+{
+}
+
+static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
+	.vidioc_querycap	= wl1273_fm_vidioc_querycap,
+	.vidioc_g_input		= wl1273_fm_vidioc_g_input,
+	.vidioc_s_input		= wl1273_fm_vidioc_s_input,
+	.vidioc_queryctrl	= wl1273_fm_vidioc_queryctrl,
+	.vidioc_g_ctrl		= wl1273_fm_vidioc_g_ctrl,
+	.vidioc_s_ctrl		= wl1273_fm_vidioc_s_ctrl,
+	.vidioc_g_audio		= wl1273_fm_vidioc_g_audio,
+	.vidioc_s_audio		= wl1273_fm_vidioc_s_audio,
+	.vidioc_g_tuner		= wl1273_fm_vidioc_g_tuner,
+	.vidioc_s_tuner		= wl1273_fm_vidioc_s_tuner,
+	.vidioc_g_frequency	= wl1273_fm_vidioc_g_frequency,
+	.vidioc_s_frequency	= wl1273_fm_vidioc_s_frequency,
+	.vidioc_s_hw_freq_seek	= wl1273_fm_vidioc_s_hw_freq_seek,
+	.vidioc_g_modulator	= wl1273_fm_vidioc_g_modulator,
+	.vidioc_s_modulator	= wl1273_fm_vidioc_s_modulator,
+	.vidioc_log_status      = wl1273_fm_vidioc_log_status,
+};
+
+static struct video_device wl1273_viddev_template = {
+	.fops			= &wl1273_fops,
+	.ioctl_ops		= &wl1273_ioctl_ops,
+	.name			= WL1273_FM_DRIVER_NAME,
+	.release		= wl1273_vdev_release,
+};
+
+static int wl1273_fm_radio_remove(struct platform_device *pdev)
+{
+	struct wl1273_device *radio = platform_get_drvdata(pdev);;
+
+	dev_info(&pdev->dev, "%s.\n", __func__);
+
+	video_unregister_device(&radio->videodev);
+	v4l2_device_unregister(&radio->v4l2dev);
+	kfree(radio);
+
+	return 0;
+}
+
+static int __devinit wl1273_fm_radio_probe(struct platform_device *pdev)
+{
+	struct wl1273_core **pdata = pdev->dev.platform_data;
+	struct wl1273_device *radio;
+	int r = 0;
+
+	dev_dbg(&pdev->dev, "%s\n", __func__);
+
+	if (!pdata) {
+		dev_err(&pdev->dev, "No platform data.\n");
+		return -EINVAL;
+	}
+
+	radio = kzalloc(sizeof(*radio), GFP_KERNEL);
+	if (!radio)
+		return -ENOMEM;
+
+	radio->core = *pdata;
+	radio->dev = &pdev->dev;
+	radio->rds_on = false;
+
+	r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
+	if (r) {
+		dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
+		goto err_device_alloc;
+	}
+
+	/* V4L2 configuration */
+	memcpy(&radio->videodev, &wl1273_viddev_template,
+	       sizeof(wl1273_viddev_template));
+
+	radio->videodev.v4l2_dev = &radio->v4l2dev;
+
+	/* register video device */
+	r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
+	if (r) {
+		dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
+			": Could not register video device\n");
+		goto err_video_register;
+	}
+
+	video_set_drvdata(&radio->videodev, radio);
+	platform_set_drvdata(pdev, radio);
+
+	return 0;
+
+err_video_register:
+	v4l2_device_unregister(&radio->v4l2dev);
+err_device_alloc:
+	kfree(radio);
+
+	return r;
+}
+
+MODULE_ALIAS("platform:wl1273_fm_radio");
+
+static struct platform_driver wl1273_fm_radio_driver = {
+	.probe		= wl1273_fm_radio_probe,
+	.remove		= __devexit_p(wl1273_fm_radio_remove),
+	.driver		= {
+		.name	= "wl1273_fm_radio",
+		.owner	= THIS_MODULE,
+	},
+};
+
+static int __init wl1273_fm_module_init(void)
+{
+	pr_info("%s\n", __func__);
+	return platform_driver_register(&wl1273_fm_radio_driver);
+}
+module_init(wl1273_fm_module_init);
+
+static void __exit wl1273_fm_module_exit(void)
+{
+	flush_scheduled_work();
+	platform_driver_unregister(&wl1273_fm_radio_driver);
+	pr_info(DRIVER_DESC ", Exiting.\n");
+}
+module_exit(wl1273_fm_module_exit);
+
+MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@xxxxxxxxx>");
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE("GPL");
-- 
1.6.1.3

--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Input]     [Video for Linux]     [Gstreamer Embedded]     [Mplayer Users]     [Linux USB Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]
  Powered by Linux