xref: /linux/drivers/media/radio/radio-wl1273.c (revision b530a447bb588fdf43fdf4eb909e4ee1921d47ac)
187d1a50cSMatti Aaltonen /*
287d1a50cSMatti Aaltonen  * Driver for the Texas Instruments WL1273 FM radio.
387d1a50cSMatti Aaltonen  *
496a9cc68SMatti Aaltonen  * Copyright (C) 2011 Nokia Corporation
587d1a50cSMatti Aaltonen  * Author: Matti J. Aaltonen <matti.j.aaltonen@nokia.com>
687d1a50cSMatti Aaltonen  *
787d1a50cSMatti Aaltonen  * This program is free software; you can redistribute it and/or
887d1a50cSMatti Aaltonen  * modify it under the terms of the GNU General Public License
987d1a50cSMatti Aaltonen  * version 2 as published by the Free Software Foundation.
1087d1a50cSMatti Aaltonen  *
1187d1a50cSMatti Aaltonen  * This program is distributed in the hope that it will be useful,
1287d1a50cSMatti Aaltonen  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1387d1a50cSMatti Aaltonen  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
1487d1a50cSMatti Aaltonen  * GNU General Public License for more details.
1587d1a50cSMatti Aaltonen  *
1687d1a50cSMatti Aaltonen  * You should have received a copy of the GNU General Public License
1787d1a50cSMatti Aaltonen  * along with this program; if not, write to the Free Software
1887d1a50cSMatti Aaltonen  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1987d1a50cSMatti Aaltonen  */
2087d1a50cSMatti Aaltonen 
2187d1a50cSMatti Aaltonen #include <linux/delay.h>
2287d1a50cSMatti Aaltonen #include <linux/firmware.h>
2387d1a50cSMatti Aaltonen #include <linux/interrupt.h>
2487d1a50cSMatti Aaltonen #include <linux/mfd/wl1273-core.h>
2587d1a50cSMatti Aaltonen #include <linux/slab.h>
267a707b89SPaul Gortmaker #include <linux/module.h>
2787d1a50cSMatti Aaltonen #include <media/v4l2-common.h>
2887d1a50cSMatti Aaltonen #include <media/v4l2-ctrls.h>
2987d1a50cSMatti Aaltonen #include <media/v4l2-device.h>
3087d1a50cSMatti Aaltonen #include <media/v4l2-ioctl.h>
3187d1a50cSMatti Aaltonen 
3287d1a50cSMatti Aaltonen #define DRIVER_DESC "Wl1273 FM Radio"
3387d1a50cSMatti Aaltonen 
3487d1a50cSMatti Aaltonen #define WL1273_POWER_SET_OFF		0
3587d1a50cSMatti Aaltonen #define WL1273_POWER_SET_FM		BIT(0)
3687d1a50cSMatti Aaltonen #define WL1273_POWER_SET_RDS		BIT(1)
3787d1a50cSMatti Aaltonen #define WL1273_POWER_SET_RETENTION	BIT(4)
3887d1a50cSMatti Aaltonen 
3987d1a50cSMatti Aaltonen #define WL1273_PUPD_SET_OFF		0x00
4087d1a50cSMatti Aaltonen #define WL1273_PUPD_SET_ON		0x01
4187d1a50cSMatti Aaltonen #define WL1273_PUPD_SET_RETENTION	0x10
4287d1a50cSMatti Aaltonen 
4387d1a50cSMatti Aaltonen #define WL1273_FREQ(x)		(x * 10000 / 625)
4487d1a50cSMatti Aaltonen #define WL1273_INV_FREQ(x)	(x * 625 / 10000)
4587d1a50cSMatti Aaltonen 
4687d1a50cSMatti Aaltonen /*
4787d1a50cSMatti Aaltonen  * static int radio_nr - The number of the radio device
4887d1a50cSMatti Aaltonen  *
4987d1a50cSMatti Aaltonen  * The default is 0.
5087d1a50cSMatti Aaltonen  */
5187d1a50cSMatti Aaltonen static int radio_nr;
5287d1a50cSMatti Aaltonen module_param(radio_nr, int, 0);
5387d1a50cSMatti Aaltonen MODULE_PARM_DESC(radio_nr, "The number of the radio device. Default = 0");
5487d1a50cSMatti Aaltonen 
5587d1a50cSMatti Aaltonen struct wl1273_device {
5687d1a50cSMatti Aaltonen 	char *bus_type;
5787d1a50cSMatti Aaltonen 
5887d1a50cSMatti Aaltonen 	u8 forbidden;
5987d1a50cSMatti Aaltonen 	unsigned int preemphasis;
6087d1a50cSMatti Aaltonen 	unsigned int spacing;
6187d1a50cSMatti Aaltonen 	unsigned int tx_power;
6287d1a50cSMatti Aaltonen 	unsigned int rx_frequency;
6387d1a50cSMatti Aaltonen 	unsigned int tx_frequency;
6487d1a50cSMatti Aaltonen 	unsigned int rangelow;
6587d1a50cSMatti Aaltonen 	unsigned int rangehigh;
6687d1a50cSMatti Aaltonen 	unsigned int band;
6787d1a50cSMatti Aaltonen 	bool stereo;
6887d1a50cSMatti Aaltonen 
6987d1a50cSMatti Aaltonen 	/* RDS */
7087d1a50cSMatti Aaltonen 	unsigned int rds_on;
7187d1a50cSMatti Aaltonen 
7287d1a50cSMatti Aaltonen 	wait_queue_head_t read_queue;
7387d1a50cSMatti Aaltonen 	struct mutex lock; /* for serializing fm radio operations */
7487d1a50cSMatti Aaltonen 	struct completion busy;
7587d1a50cSMatti Aaltonen 
7687d1a50cSMatti Aaltonen 	unsigned char *buffer;
7787d1a50cSMatti Aaltonen 	unsigned int buf_size;
7887d1a50cSMatti Aaltonen 	unsigned int rd_index;
7987d1a50cSMatti Aaltonen 	unsigned int wr_index;
8087d1a50cSMatti Aaltonen 
8187d1a50cSMatti Aaltonen 	/* Selected interrupts */
8287d1a50cSMatti Aaltonen 	u16 irq_flags;
8387d1a50cSMatti Aaltonen 	u16 irq_received;
8487d1a50cSMatti Aaltonen 
8587d1a50cSMatti Aaltonen 	struct v4l2_ctrl_handler ctrl_handler;
8687d1a50cSMatti Aaltonen 	struct v4l2_device v4l2dev;
8787d1a50cSMatti Aaltonen 	struct video_device videodev;
8887d1a50cSMatti Aaltonen 	struct device *dev;
8987d1a50cSMatti Aaltonen 	struct wl1273_core *core;
9087d1a50cSMatti Aaltonen 	struct file *owner;
9187d1a50cSMatti Aaltonen 	char *write_buf;
9287d1a50cSMatti Aaltonen 	unsigned int rds_users;
9387d1a50cSMatti Aaltonen };
9487d1a50cSMatti Aaltonen 
9587d1a50cSMatti Aaltonen #define WL1273_IRQ_MASK	 (WL1273_FR_EVENT		|	\
9687d1a50cSMatti Aaltonen 			  WL1273_POW_ENB_EVENT)
9787d1a50cSMatti Aaltonen 
9887d1a50cSMatti Aaltonen /*
9987d1a50cSMatti Aaltonen  * static unsigned int rds_buf - the number of RDS buffer blocks used.
10087d1a50cSMatti Aaltonen  *
10187d1a50cSMatti Aaltonen  * The default number is 100.
10287d1a50cSMatti Aaltonen  */
10387d1a50cSMatti Aaltonen static unsigned int rds_buf = 100;
10487d1a50cSMatti Aaltonen module_param(rds_buf, uint, 0);
10587d1a50cSMatti Aaltonen MODULE_PARM_DESC(rds_buf, "Number of RDS buffer entries. Default = 100");
10687d1a50cSMatti Aaltonen 
10787d1a50cSMatti Aaltonen static int wl1273_fm_write_fw(struct wl1273_core *core,
10887d1a50cSMatti Aaltonen 			      __u8 *fw, int len)
10987d1a50cSMatti Aaltonen {
11087d1a50cSMatti Aaltonen 	struct i2c_client *client = core->client;
11187d1a50cSMatti Aaltonen 	struct i2c_msg msg;
11287d1a50cSMatti Aaltonen 	int i, r = 0;
11387d1a50cSMatti Aaltonen 
11487d1a50cSMatti Aaltonen 	msg.addr = client->addr;
11587d1a50cSMatti Aaltonen 	msg.flags = 0;
11687d1a50cSMatti Aaltonen 
11787d1a50cSMatti Aaltonen 	for (i = 0; i <= len; i++) {
11887d1a50cSMatti Aaltonen 		msg.len = fw[0];
11987d1a50cSMatti Aaltonen 		msg.buf = fw + 1;
12087d1a50cSMatti Aaltonen 
12187d1a50cSMatti Aaltonen 		fw += msg.len + 1;
12287d1a50cSMatti Aaltonen 		dev_dbg(&client->dev, "%s:len[%d]: %d\n", __func__, i, msg.len);
12387d1a50cSMatti Aaltonen 
12487d1a50cSMatti Aaltonen 		r = i2c_transfer(client->adapter, &msg, 1);
12587d1a50cSMatti Aaltonen 		if (r < 0 && i < len + 1)
12687d1a50cSMatti Aaltonen 			break;
12787d1a50cSMatti Aaltonen 	}
12887d1a50cSMatti Aaltonen 
12987d1a50cSMatti Aaltonen 	dev_dbg(&client->dev, "%s: i: %d\n", __func__, i);
13087d1a50cSMatti Aaltonen 	dev_dbg(&client->dev, "%s: len + 1: %d\n", __func__, len + 1);
13187d1a50cSMatti Aaltonen 
13287d1a50cSMatti Aaltonen 	/* Last transfer always fails. */
13387d1a50cSMatti Aaltonen 	if (i == len || r == 1)
13487d1a50cSMatti Aaltonen 		r = 0;
13587d1a50cSMatti Aaltonen 
13687d1a50cSMatti Aaltonen 	return r;
13787d1a50cSMatti Aaltonen }
13887d1a50cSMatti Aaltonen 
13987d1a50cSMatti Aaltonen #define WL1273_FIFO_HAS_DATA(status)	(1 << 5 & status)
14087d1a50cSMatti Aaltonen #define WL1273_RDS_CORRECTABLE_ERROR	(1 << 3)
14187d1a50cSMatti Aaltonen #define WL1273_RDS_UNCORRECTABLE_ERROR	(1 << 4)
14287d1a50cSMatti Aaltonen 
14387d1a50cSMatti Aaltonen static int wl1273_fm_rds(struct wl1273_device *radio)
14487d1a50cSMatti Aaltonen {
14587d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
14687d1a50cSMatti Aaltonen 	struct i2c_client *client = core->client;
14787d1a50cSMatti Aaltonen 	u16 val;
14887d1a50cSMatti Aaltonen 	u8 b0 = WL1273_RDS_DATA_GET, status;
14987d1a50cSMatti Aaltonen 	struct v4l2_rds_data rds = { 0, 0, 0 };
15087d1a50cSMatti Aaltonen 	struct i2c_msg msg[] = {
15187d1a50cSMatti Aaltonen 		{
15287d1a50cSMatti Aaltonen 			.addr = client->addr,
15387d1a50cSMatti Aaltonen 			.flags = 0,
15487d1a50cSMatti Aaltonen 			.buf = &b0,
15587d1a50cSMatti Aaltonen 			.len = 1,
15687d1a50cSMatti Aaltonen 		},
15787d1a50cSMatti Aaltonen 		{
15887d1a50cSMatti Aaltonen 			.addr = client->addr,
15987d1a50cSMatti Aaltonen 			.flags = I2C_M_RD,
16087d1a50cSMatti Aaltonen 			.buf = (u8 *) &rds,
16187d1a50cSMatti Aaltonen 			.len = sizeof(rds),
16287d1a50cSMatti Aaltonen 		}
16387d1a50cSMatti Aaltonen 	};
16487d1a50cSMatti Aaltonen 	int r;
16587d1a50cSMatti Aaltonen 
16687d1a50cSMatti Aaltonen 	if (core->mode != WL1273_MODE_RX)
16787d1a50cSMatti Aaltonen 		return 0;
16887d1a50cSMatti Aaltonen 
16996a9cc68SMatti Aaltonen 	r = core->read(core, WL1273_RDS_SYNC_GET, &val);
17087d1a50cSMatti Aaltonen 	if (r)
17187d1a50cSMatti Aaltonen 		return r;
17287d1a50cSMatti Aaltonen 
17387d1a50cSMatti Aaltonen 	if ((val & 0x01) == 0) {
17487d1a50cSMatti Aaltonen 		/* RDS decoder not synchronized */
17587d1a50cSMatti Aaltonen 		return -EAGAIN;
17687d1a50cSMatti Aaltonen 	}
17787d1a50cSMatti Aaltonen 
17887d1a50cSMatti Aaltonen 	/* copy all four RDS blocks to internal buffer */
17987d1a50cSMatti Aaltonen 	do {
18087d1a50cSMatti Aaltonen 		r = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
18187d1a50cSMatti Aaltonen 		if (r != ARRAY_SIZE(msg)) {
18287d1a50cSMatti Aaltonen 			dev_err(radio->dev, WL1273_FM_DRIVER_NAME
18387d1a50cSMatti Aaltonen 				": %s: read_rds error r == %i)\n",
18487d1a50cSMatti Aaltonen 				__func__, r);
18587d1a50cSMatti Aaltonen 		}
18687d1a50cSMatti Aaltonen 
18787d1a50cSMatti Aaltonen 		status = rds.block;
18887d1a50cSMatti Aaltonen 
18987d1a50cSMatti Aaltonen 		if (!WL1273_FIFO_HAS_DATA(status))
19087d1a50cSMatti Aaltonen 			break;
19187d1a50cSMatti Aaltonen 
19287d1a50cSMatti Aaltonen 		/* copy bits 0-2 (the block ID) to bits 3-5 */
19387d1a50cSMatti Aaltonen 		rds.block = V4L2_RDS_BLOCK_MSK & status;
19487d1a50cSMatti Aaltonen 		rds.block |= rds.block << 3;
19587d1a50cSMatti Aaltonen 
19687d1a50cSMatti Aaltonen 		/* copy the error bits to standard positions */
19787d1a50cSMatti Aaltonen 		if (WL1273_RDS_UNCORRECTABLE_ERROR & status) {
19887d1a50cSMatti Aaltonen 			rds.block |= V4L2_RDS_BLOCK_ERROR;
19987d1a50cSMatti Aaltonen 			rds.block &= ~V4L2_RDS_BLOCK_CORRECTED;
20087d1a50cSMatti Aaltonen 		} else if  (WL1273_RDS_CORRECTABLE_ERROR & status) {
20187d1a50cSMatti Aaltonen 			rds.block &= ~V4L2_RDS_BLOCK_ERROR;
20287d1a50cSMatti Aaltonen 			rds.block |= V4L2_RDS_BLOCK_CORRECTED;
20387d1a50cSMatti Aaltonen 		}
20487d1a50cSMatti Aaltonen 
20587d1a50cSMatti Aaltonen 		/* copy RDS block to internal buffer */
20687d1a50cSMatti Aaltonen 		memcpy(&radio->buffer[radio->wr_index], &rds, RDS_BLOCK_SIZE);
20787d1a50cSMatti Aaltonen 		radio->wr_index += 3;
20887d1a50cSMatti Aaltonen 
20987d1a50cSMatti Aaltonen 		/* wrap write pointer */
21087d1a50cSMatti Aaltonen 		if (radio->wr_index >= radio->buf_size)
21187d1a50cSMatti Aaltonen 			radio->wr_index = 0;
21287d1a50cSMatti Aaltonen 
21387d1a50cSMatti Aaltonen 		/* check for overflow & start over */
21487d1a50cSMatti Aaltonen 		if (radio->wr_index == radio->rd_index) {
21587d1a50cSMatti Aaltonen 			dev_dbg(radio->dev, "RDS OVERFLOW");
21687d1a50cSMatti Aaltonen 
21787d1a50cSMatti Aaltonen 			radio->rd_index = 0;
21887d1a50cSMatti Aaltonen 			radio->wr_index = 0;
21987d1a50cSMatti Aaltonen 			break;
22087d1a50cSMatti Aaltonen 		}
22187d1a50cSMatti Aaltonen 	} while (WL1273_FIFO_HAS_DATA(status));
22287d1a50cSMatti Aaltonen 
22387d1a50cSMatti Aaltonen 	/* wake up read queue */
22487d1a50cSMatti Aaltonen 	if (radio->wr_index != radio->rd_index)
22587d1a50cSMatti Aaltonen 		wake_up_interruptible(&radio->read_queue);
22687d1a50cSMatti Aaltonen 
22787d1a50cSMatti Aaltonen 	return 0;
22887d1a50cSMatti Aaltonen }
22987d1a50cSMatti Aaltonen 
23087d1a50cSMatti Aaltonen static irqreturn_t wl1273_fm_irq_thread_handler(int irq, void *dev_id)
23187d1a50cSMatti Aaltonen {
23287d1a50cSMatti Aaltonen 	struct wl1273_device *radio = dev_id;
23387d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
23487d1a50cSMatti Aaltonen 	u16 flags;
23587d1a50cSMatti Aaltonen 	int r;
23687d1a50cSMatti Aaltonen 
23796a9cc68SMatti Aaltonen 	r = core->read(core, WL1273_FLAG_GET, &flags);
23887d1a50cSMatti Aaltonen 	if (r)
23987d1a50cSMatti Aaltonen 		goto out;
24087d1a50cSMatti Aaltonen 
24187d1a50cSMatti Aaltonen 	if (flags & WL1273_BL_EVENT) {
24287d1a50cSMatti Aaltonen 		radio->irq_received = flags;
24387d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "IRQ: BL\n");
24487d1a50cSMatti Aaltonen 	}
24587d1a50cSMatti Aaltonen 
24687d1a50cSMatti Aaltonen 	if (flags & WL1273_RDS_EVENT) {
24787d1a50cSMatti Aaltonen 		msleep(200);
24887d1a50cSMatti Aaltonen 
24987d1a50cSMatti Aaltonen 		wl1273_fm_rds(radio);
25087d1a50cSMatti Aaltonen 	}
25187d1a50cSMatti Aaltonen 
25287d1a50cSMatti Aaltonen 	if (flags & WL1273_BBLK_EVENT)
25387d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "IRQ: BBLK\n");
25487d1a50cSMatti Aaltonen 
25587d1a50cSMatti Aaltonen 	if (flags & WL1273_LSYNC_EVENT)
25687d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "IRQ: LSYNC\n");
25787d1a50cSMatti Aaltonen 
25887d1a50cSMatti Aaltonen 	if (flags & WL1273_LEV_EVENT) {
25987d1a50cSMatti Aaltonen 		u16 level;
26087d1a50cSMatti Aaltonen 
26196a9cc68SMatti Aaltonen 		r = core->read(core, WL1273_RSSI_LVL_GET, &level);
26287d1a50cSMatti Aaltonen 		if (r)
26387d1a50cSMatti Aaltonen 			goto out;
26487d1a50cSMatti Aaltonen 
26587d1a50cSMatti Aaltonen 		if (level > 14)
26687d1a50cSMatti Aaltonen 			dev_dbg(radio->dev, "IRQ: LEV: 0x%x04\n", level);
26787d1a50cSMatti Aaltonen 	}
26887d1a50cSMatti Aaltonen 
26987d1a50cSMatti Aaltonen 	if (flags & WL1273_IFFR_EVENT)
27087d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "IRQ: IFFR\n");
27187d1a50cSMatti Aaltonen 
27287d1a50cSMatti Aaltonen 	if (flags & WL1273_PI_EVENT)
27387d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "IRQ: PI\n");
27487d1a50cSMatti Aaltonen 
27587d1a50cSMatti Aaltonen 	if (flags & WL1273_PD_EVENT)
27687d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "IRQ: PD\n");
27787d1a50cSMatti Aaltonen 
27887d1a50cSMatti Aaltonen 	if (flags & WL1273_STIC_EVENT)
27987d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "IRQ: STIC\n");
28087d1a50cSMatti Aaltonen 
28187d1a50cSMatti Aaltonen 	if (flags & WL1273_MAL_EVENT)
28287d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "IRQ: MAL\n");
28387d1a50cSMatti Aaltonen 
28487d1a50cSMatti Aaltonen 	if (flags & WL1273_POW_ENB_EVENT) {
28587d1a50cSMatti Aaltonen 		complete(&radio->busy);
28687d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "NOT BUSY\n");
28787d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "IRQ: POW_ENB\n");
28887d1a50cSMatti Aaltonen 	}
28987d1a50cSMatti Aaltonen 
29087d1a50cSMatti Aaltonen 	if (flags & WL1273_SCAN_OVER_EVENT)
29187d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "IRQ: SCAN_OVER\n");
29287d1a50cSMatti Aaltonen 
29387d1a50cSMatti Aaltonen 	if (flags & WL1273_ERROR_EVENT)
29487d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "IRQ: ERROR\n");
29587d1a50cSMatti Aaltonen 
29687d1a50cSMatti Aaltonen 	if (flags & WL1273_FR_EVENT) {
29787d1a50cSMatti Aaltonen 		u16 freq;
29887d1a50cSMatti Aaltonen 
29987d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "IRQ: FR:\n");
30087d1a50cSMatti Aaltonen 
30187d1a50cSMatti Aaltonen 		if (core->mode == WL1273_MODE_RX) {
30296a9cc68SMatti Aaltonen 			r = core->write(core, WL1273_TUNER_MODE_SET,
30387d1a50cSMatti Aaltonen 					TUNER_MODE_STOP_SEARCH);
30487d1a50cSMatti Aaltonen 			if (r) {
30587d1a50cSMatti Aaltonen 				dev_err(radio->dev,
30687d1a50cSMatti Aaltonen 					"%s: TUNER_MODE_SET fails: %d\n",
30787d1a50cSMatti Aaltonen 					__func__, r);
30887d1a50cSMatti Aaltonen 				goto out;
30987d1a50cSMatti Aaltonen 			}
31087d1a50cSMatti Aaltonen 
31196a9cc68SMatti Aaltonen 			r = core->read(core, WL1273_FREQ_SET, &freq);
31287d1a50cSMatti Aaltonen 			if (r)
31387d1a50cSMatti Aaltonen 				goto out;
31487d1a50cSMatti Aaltonen 
31587d1a50cSMatti Aaltonen 			if (radio->band == WL1273_BAND_JAPAN)
31687d1a50cSMatti Aaltonen 				radio->rx_frequency = WL1273_BAND_JAPAN_LOW +
31787d1a50cSMatti Aaltonen 					freq * 50;
31887d1a50cSMatti Aaltonen 			else
31987d1a50cSMatti Aaltonen 				radio->rx_frequency = WL1273_BAND_OTHER_LOW +
32087d1a50cSMatti Aaltonen 					freq * 50;
32187d1a50cSMatti Aaltonen 			/*
32287d1a50cSMatti Aaltonen 			 *  The driver works better with this msleep,
32387d1a50cSMatti Aaltonen 			 *  the documentation doesn't mention it.
32487d1a50cSMatti Aaltonen 			 */
32587d1a50cSMatti Aaltonen 			usleep_range(10000, 15000);
32687d1a50cSMatti Aaltonen 
32787d1a50cSMatti Aaltonen 			dev_dbg(radio->dev, "%dkHz\n", radio->rx_frequency);
32887d1a50cSMatti Aaltonen 
32987d1a50cSMatti Aaltonen 		} else {
33096a9cc68SMatti Aaltonen 			r = core->read(core, WL1273_CHANL_SET, &freq);
33187d1a50cSMatti Aaltonen 			if (r)
33287d1a50cSMatti Aaltonen 				goto out;
33387d1a50cSMatti Aaltonen 
33487d1a50cSMatti Aaltonen 			dev_dbg(radio->dev, "%dkHz\n", freq);
33587d1a50cSMatti Aaltonen 		}
33687d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "%s: NOT BUSY\n", __func__);
33787d1a50cSMatti Aaltonen 	}
33887d1a50cSMatti Aaltonen 
33987d1a50cSMatti Aaltonen out:
34096a9cc68SMatti Aaltonen 	core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
34187d1a50cSMatti Aaltonen 	complete(&radio->busy);
34287d1a50cSMatti Aaltonen 
34387d1a50cSMatti Aaltonen 	return IRQ_HANDLED;
34487d1a50cSMatti Aaltonen }
34587d1a50cSMatti Aaltonen 
34687d1a50cSMatti Aaltonen static int wl1273_fm_set_tx_freq(struct wl1273_device *radio, unsigned int freq)
34787d1a50cSMatti Aaltonen {
34887d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
34987d1a50cSMatti Aaltonen 	int r = 0;
35087d1a50cSMatti Aaltonen 
35187d1a50cSMatti Aaltonen 	if (freq < WL1273_BAND_TX_LOW) {
35287d1a50cSMatti Aaltonen 		dev_err(radio->dev,
35387d1a50cSMatti Aaltonen 			"Frequency out of range: %d < %d\n", freq,
35487d1a50cSMatti Aaltonen 			WL1273_BAND_TX_LOW);
35587d1a50cSMatti Aaltonen 		return -ERANGE;
35687d1a50cSMatti Aaltonen 	}
35787d1a50cSMatti Aaltonen 
35887d1a50cSMatti Aaltonen 	if (freq > WL1273_BAND_TX_HIGH) {
35987d1a50cSMatti Aaltonen 		dev_err(radio->dev,
36087d1a50cSMatti Aaltonen 			"Frequency out of range: %d > %d\n", freq,
36187d1a50cSMatti Aaltonen 			WL1273_BAND_TX_HIGH);
36287d1a50cSMatti Aaltonen 		return -ERANGE;
36387d1a50cSMatti Aaltonen 	}
36487d1a50cSMatti Aaltonen 
36587d1a50cSMatti Aaltonen 	/*
36687d1a50cSMatti Aaltonen 	 *  The driver works better with this sleep,
36787d1a50cSMatti Aaltonen 	 *  the documentation doesn't mention it.
36887d1a50cSMatti Aaltonen 	 */
36987d1a50cSMatti Aaltonen 	usleep_range(5000, 10000);
37087d1a50cSMatti Aaltonen 
37187d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s: freq: %d kHz\n", __func__, freq);
37287d1a50cSMatti Aaltonen 
37387d1a50cSMatti Aaltonen 	/* Set the current tx channel */
37496a9cc68SMatti Aaltonen 	r = core->write(core, WL1273_CHANL_SET, freq / 10);
37587d1a50cSMatti Aaltonen 	if (r)
37687d1a50cSMatti Aaltonen 		return r;
37787d1a50cSMatti Aaltonen 
37887d1a50cSMatti Aaltonen 	INIT_COMPLETION(radio->busy);
37987d1a50cSMatti Aaltonen 
38087d1a50cSMatti Aaltonen 	/* wait for the FR IRQ */
38187d1a50cSMatti Aaltonen 	r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
38287d1a50cSMatti Aaltonen 	if (!r)
38387d1a50cSMatti Aaltonen 		return -ETIMEDOUT;
38487d1a50cSMatti Aaltonen 
38587d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "WL1273_CHANL_SET: %d\n", r);
38687d1a50cSMatti Aaltonen 
38787d1a50cSMatti Aaltonen 	/* Enable the output power */
38896a9cc68SMatti Aaltonen 	r = core->write(core, WL1273_POWER_ENB_SET, 1);
38987d1a50cSMatti Aaltonen 	if (r)
39087d1a50cSMatti Aaltonen 		return r;
39187d1a50cSMatti Aaltonen 
39287d1a50cSMatti Aaltonen 	INIT_COMPLETION(radio->busy);
39387d1a50cSMatti Aaltonen 
39487d1a50cSMatti Aaltonen 	/* wait for the POWER_ENB IRQ */
39587d1a50cSMatti Aaltonen 	r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
39687d1a50cSMatti Aaltonen 	if (!r)
39787d1a50cSMatti Aaltonen 		return -ETIMEDOUT;
39887d1a50cSMatti Aaltonen 
39987d1a50cSMatti Aaltonen 	radio->tx_frequency = freq;
40087d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %d\n", r);
40187d1a50cSMatti Aaltonen 
40287d1a50cSMatti Aaltonen 	return	0;
40387d1a50cSMatti Aaltonen }
40487d1a50cSMatti Aaltonen 
40587d1a50cSMatti Aaltonen static int wl1273_fm_set_rx_freq(struct wl1273_device *radio, unsigned int freq)
40687d1a50cSMatti Aaltonen {
40787d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
40887d1a50cSMatti Aaltonen 	int r, f;
40987d1a50cSMatti Aaltonen 
41087d1a50cSMatti Aaltonen 	if (freq < radio->rangelow) {
41187d1a50cSMatti Aaltonen 		dev_err(radio->dev,
41287d1a50cSMatti Aaltonen 			"Frequency out of range: %d < %d\n", freq,
41387d1a50cSMatti Aaltonen 			radio->rangelow);
41487d1a50cSMatti Aaltonen 		r = -ERANGE;
41587d1a50cSMatti Aaltonen 		goto err;
41687d1a50cSMatti Aaltonen 	}
41787d1a50cSMatti Aaltonen 
41887d1a50cSMatti Aaltonen 	if (freq > radio->rangehigh) {
41987d1a50cSMatti Aaltonen 		dev_err(radio->dev,
42087d1a50cSMatti Aaltonen 			"Frequency out of range: %d > %d\n", freq,
42187d1a50cSMatti Aaltonen 			radio->rangehigh);
42287d1a50cSMatti Aaltonen 		r = -ERANGE;
42387d1a50cSMatti Aaltonen 		goto err;
42487d1a50cSMatti Aaltonen 	}
42587d1a50cSMatti Aaltonen 
42687d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s: %dkHz\n", __func__, freq);
42787d1a50cSMatti Aaltonen 
42896a9cc68SMatti Aaltonen 	core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
42987d1a50cSMatti Aaltonen 
43087d1a50cSMatti Aaltonen 	if (radio->band == WL1273_BAND_JAPAN)
43187d1a50cSMatti Aaltonen 		f = (freq - WL1273_BAND_JAPAN_LOW) / 50;
43287d1a50cSMatti Aaltonen 	else
43387d1a50cSMatti Aaltonen 		f = (freq - WL1273_BAND_OTHER_LOW) / 50;
43487d1a50cSMatti Aaltonen 
43596a9cc68SMatti Aaltonen 	r = core->write(core, WL1273_FREQ_SET, f);
43687d1a50cSMatti Aaltonen 	if (r) {
43787d1a50cSMatti Aaltonen 		dev_err(radio->dev, "FREQ_SET fails\n");
43887d1a50cSMatti Aaltonen 		goto err;
43987d1a50cSMatti Aaltonen 	}
44087d1a50cSMatti Aaltonen 
44196a9cc68SMatti Aaltonen 	r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET);
44287d1a50cSMatti Aaltonen 	if (r) {
44387d1a50cSMatti Aaltonen 		dev_err(radio->dev, "TUNER_MODE_SET fails\n");
44487d1a50cSMatti Aaltonen 		goto err;
44587d1a50cSMatti Aaltonen 	}
44687d1a50cSMatti Aaltonen 
44787d1a50cSMatti Aaltonen 	INIT_COMPLETION(radio->busy);
44887d1a50cSMatti Aaltonen 
44987d1a50cSMatti Aaltonen 	r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
45087d1a50cSMatti Aaltonen 	if (!r) {
45187d1a50cSMatti Aaltonen 		dev_err(radio->dev, "%s: TIMEOUT\n", __func__);
45287d1a50cSMatti Aaltonen 		return -ETIMEDOUT;
45387d1a50cSMatti Aaltonen 	}
45487d1a50cSMatti Aaltonen 
45587d1a50cSMatti Aaltonen 	radio->rd_index = 0;
45687d1a50cSMatti Aaltonen 	radio->wr_index = 0;
45787d1a50cSMatti Aaltonen 	radio->rx_frequency = freq;
45887d1a50cSMatti Aaltonen 	return 0;
45987d1a50cSMatti Aaltonen err:
46087d1a50cSMatti Aaltonen 	return r;
46187d1a50cSMatti Aaltonen }
46287d1a50cSMatti Aaltonen 
46387d1a50cSMatti Aaltonen static int wl1273_fm_get_freq(struct wl1273_device *radio)
46487d1a50cSMatti Aaltonen {
46587d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
46687d1a50cSMatti Aaltonen 	unsigned int freq;
46787d1a50cSMatti Aaltonen 	u16 f;
46887d1a50cSMatti Aaltonen 	int r;
46987d1a50cSMatti Aaltonen 
47087d1a50cSMatti Aaltonen 	if (core->mode == WL1273_MODE_RX) {
47196a9cc68SMatti Aaltonen 		r = core->read(core, WL1273_FREQ_SET, &f);
47287d1a50cSMatti Aaltonen 		if (r)
47387d1a50cSMatti Aaltonen 			return r;
47487d1a50cSMatti Aaltonen 
47587d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "Freq get: 0x%04x\n", f);
47687d1a50cSMatti Aaltonen 		if (radio->band == WL1273_BAND_JAPAN)
47787d1a50cSMatti Aaltonen 			freq = WL1273_BAND_JAPAN_LOW + 50 * f;
47887d1a50cSMatti Aaltonen 		else
47987d1a50cSMatti Aaltonen 			freq = WL1273_BAND_OTHER_LOW + 50 * f;
48087d1a50cSMatti Aaltonen 	} else {
48196a9cc68SMatti Aaltonen 		r = core->read(core, WL1273_CHANL_SET, &f);
48287d1a50cSMatti Aaltonen 		if (r)
48387d1a50cSMatti Aaltonen 			return r;
48487d1a50cSMatti Aaltonen 
48587d1a50cSMatti Aaltonen 		freq = f * 10;
48687d1a50cSMatti Aaltonen 	}
48787d1a50cSMatti Aaltonen 
48887d1a50cSMatti Aaltonen 	return freq;
48987d1a50cSMatti Aaltonen }
49087d1a50cSMatti Aaltonen 
49187d1a50cSMatti Aaltonen /**
49287d1a50cSMatti Aaltonen  * wl1273_fm_upload_firmware_patch() -	Upload the firmware.
49387d1a50cSMatti Aaltonen  * @radio:				A pointer to the device struct.
49487d1a50cSMatti Aaltonen  *
49587d1a50cSMatti Aaltonen  * The firmware file consists of arrays of bytes where the first byte
49687d1a50cSMatti Aaltonen  * gives the array length. The first byte in the file gives the
49787d1a50cSMatti Aaltonen  * number of these arrays.
49887d1a50cSMatti Aaltonen  */
49987d1a50cSMatti Aaltonen static int wl1273_fm_upload_firmware_patch(struct wl1273_device *radio)
50087d1a50cSMatti Aaltonen {
50187d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
50287d1a50cSMatti Aaltonen 	unsigned int packet_num;
50387d1a50cSMatti Aaltonen 	const struct firmware *fw_p;
50487d1a50cSMatti Aaltonen 	const char *fw_name = "radio-wl1273-fw.bin";
50587d1a50cSMatti Aaltonen 	struct device *dev = radio->dev;
50687d1a50cSMatti Aaltonen 	__u8 *ptr;
507fce50ac8SMauro Carvalho Chehab 	int r;
50887d1a50cSMatti Aaltonen 
50987d1a50cSMatti Aaltonen 	dev_dbg(dev, "%s:\n", __func__);
51087d1a50cSMatti Aaltonen 
51187d1a50cSMatti Aaltonen 	/*
51287d1a50cSMatti Aaltonen 	 * Uploading the firmware patch is not always necessary,
51387d1a50cSMatti Aaltonen 	 * so we only print an info message.
51487d1a50cSMatti Aaltonen 	 */
51587d1a50cSMatti Aaltonen 	if (request_firmware(&fw_p, fw_name, dev)) {
51687d1a50cSMatti Aaltonen 		dev_info(dev, "%s - %s not found\n", __func__, fw_name);
51787d1a50cSMatti Aaltonen 
51887d1a50cSMatti Aaltonen 		return 0;
51987d1a50cSMatti Aaltonen 	}
52087d1a50cSMatti Aaltonen 
52187d1a50cSMatti Aaltonen 	ptr = (__u8 *) fw_p->data;
52287d1a50cSMatti Aaltonen 	packet_num = ptr[0];
52387d1a50cSMatti Aaltonen 	dev_dbg(dev, "%s: packets: %d\n", __func__, packet_num);
52487d1a50cSMatti Aaltonen 
52587d1a50cSMatti Aaltonen 	r = wl1273_fm_write_fw(core, ptr + 1, packet_num);
52687d1a50cSMatti Aaltonen 	if (r) {
52787d1a50cSMatti Aaltonen 		dev_err(dev, "FW upload error: %d\n", r);
52887d1a50cSMatti Aaltonen 		goto out;
52987d1a50cSMatti Aaltonen 	}
53087d1a50cSMatti Aaltonen 
53187d1a50cSMatti Aaltonen 	/* ignore possible error here */
53296a9cc68SMatti Aaltonen 	core->write(core, WL1273_RESET, 0);
53387d1a50cSMatti Aaltonen 
53487d1a50cSMatti Aaltonen 	dev_dbg(dev, "%s - download OK, r: %d\n", __func__, r);
53587d1a50cSMatti Aaltonen out:
53687d1a50cSMatti Aaltonen 	release_firmware(fw_p);
53787d1a50cSMatti Aaltonen 	return r;
53887d1a50cSMatti Aaltonen }
53987d1a50cSMatti Aaltonen 
54087d1a50cSMatti Aaltonen static int wl1273_fm_stop(struct wl1273_device *radio)
54187d1a50cSMatti Aaltonen {
54287d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
54387d1a50cSMatti Aaltonen 
54487d1a50cSMatti Aaltonen 	if (core->mode == WL1273_MODE_RX) {
54596a9cc68SMatti Aaltonen 		int r = core->write(core, WL1273_POWER_SET,
54687d1a50cSMatti Aaltonen 				    WL1273_POWER_SET_OFF);
54787d1a50cSMatti Aaltonen 		if (r)
54887d1a50cSMatti Aaltonen 			dev_err(radio->dev, "%s: POWER_SET fails: %d\n",
54987d1a50cSMatti Aaltonen 				__func__, r);
55087d1a50cSMatti Aaltonen 	} else if (core->mode == WL1273_MODE_TX) {
55196a9cc68SMatti Aaltonen 		int r = core->write(core, WL1273_PUPD_SET,
55287d1a50cSMatti Aaltonen 				    WL1273_PUPD_SET_OFF);
55387d1a50cSMatti Aaltonen 		if (r)
55487d1a50cSMatti Aaltonen 			dev_err(radio->dev,
55587d1a50cSMatti Aaltonen 				"%s: PUPD_SET fails: %d\n", __func__, r);
55687d1a50cSMatti Aaltonen 	}
55787d1a50cSMatti Aaltonen 
55887d1a50cSMatti Aaltonen 	if (core->pdata->disable) {
55987d1a50cSMatti Aaltonen 		core->pdata->disable();
56087d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "Back to reset\n");
56187d1a50cSMatti Aaltonen 	}
56287d1a50cSMatti Aaltonen 
56387d1a50cSMatti Aaltonen 	return 0;
56487d1a50cSMatti Aaltonen }
56587d1a50cSMatti Aaltonen 
56687d1a50cSMatti Aaltonen static int wl1273_fm_start(struct wl1273_device *radio, int new_mode)
56787d1a50cSMatti Aaltonen {
56887d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
56987d1a50cSMatti Aaltonen 	struct wl1273_fm_platform_data *pdata = core->pdata;
57087d1a50cSMatti Aaltonen 	struct device *dev = radio->dev;
57187d1a50cSMatti Aaltonen 	int r = -EINVAL;
57287d1a50cSMatti Aaltonen 
57387d1a50cSMatti Aaltonen 	if (pdata->enable && core->mode == WL1273_MODE_OFF) {
57487d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "Out of reset\n");
57587d1a50cSMatti Aaltonen 
57687d1a50cSMatti Aaltonen 		pdata->enable();
57787d1a50cSMatti Aaltonen 		msleep(250);
57887d1a50cSMatti Aaltonen 	}
57987d1a50cSMatti Aaltonen 
58087d1a50cSMatti Aaltonen 	if (new_mode == WL1273_MODE_RX) {
58187d1a50cSMatti Aaltonen 		u16 val = WL1273_POWER_SET_FM;
58287d1a50cSMatti Aaltonen 
58387d1a50cSMatti Aaltonen 		if (radio->rds_on)
58487d1a50cSMatti Aaltonen 			val |= WL1273_POWER_SET_RDS;
58587d1a50cSMatti Aaltonen 
58687d1a50cSMatti Aaltonen 		/* If this fails try again */
58796a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_POWER_SET, val);
58887d1a50cSMatti Aaltonen 		if (r) {
58987d1a50cSMatti Aaltonen 			msleep(100);
59087d1a50cSMatti Aaltonen 
59196a9cc68SMatti Aaltonen 			r = core->write(core, WL1273_POWER_SET, val);
59287d1a50cSMatti Aaltonen 			if (r) {
59387d1a50cSMatti Aaltonen 				dev_err(dev, "%s: POWER_SET fails\n", __func__);
59487d1a50cSMatti Aaltonen 				goto fail;
59587d1a50cSMatti Aaltonen 			}
59687d1a50cSMatti Aaltonen 		}
59787d1a50cSMatti Aaltonen 
59887d1a50cSMatti Aaltonen 		/* rds buffer configuration */
59987d1a50cSMatti Aaltonen 		radio->wr_index = 0;
60087d1a50cSMatti Aaltonen 		radio->rd_index = 0;
60187d1a50cSMatti Aaltonen 
60287d1a50cSMatti Aaltonen 	} else if (new_mode == WL1273_MODE_TX) {
60387d1a50cSMatti Aaltonen 		/* If this fails try again once */
60496a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_PUPD_SET, WL1273_PUPD_SET_ON);
60587d1a50cSMatti Aaltonen 		if (r) {
60687d1a50cSMatti Aaltonen 			msleep(100);
60796a9cc68SMatti Aaltonen 			r = core->write(core, WL1273_PUPD_SET,
60887d1a50cSMatti Aaltonen 					WL1273_PUPD_SET_ON);
60987d1a50cSMatti Aaltonen 			if (r) {
61087d1a50cSMatti Aaltonen 				dev_err(dev, "%s: PUPD_SET fails\n", __func__);
61187d1a50cSMatti Aaltonen 				goto fail;
61287d1a50cSMatti Aaltonen 			}
61387d1a50cSMatti Aaltonen 		}
61487d1a50cSMatti Aaltonen 
61587d1a50cSMatti Aaltonen 		if (radio->rds_on)
61696a9cc68SMatti Aaltonen 			r = core->write(core, WL1273_RDS_DATA_ENB, 1);
61787d1a50cSMatti Aaltonen 		else
61896a9cc68SMatti Aaltonen 			r = core->write(core, WL1273_RDS_DATA_ENB, 0);
61987d1a50cSMatti Aaltonen 	} else {
62087d1a50cSMatti Aaltonen 		dev_warn(dev, "%s: Illegal mode.\n", __func__);
62187d1a50cSMatti Aaltonen 	}
62287d1a50cSMatti Aaltonen 
62387d1a50cSMatti Aaltonen 	if (core->mode == WL1273_MODE_OFF) {
62487d1a50cSMatti Aaltonen 		r = wl1273_fm_upload_firmware_patch(radio);
62587d1a50cSMatti Aaltonen 		if (r)
62687d1a50cSMatti Aaltonen 			dev_warn(dev, "Firmware upload failed.\n");
62787d1a50cSMatti Aaltonen 
62887d1a50cSMatti Aaltonen 		/*
62987d1a50cSMatti Aaltonen 		 * Sometimes the chip is in a wrong power state at this point.
63087d1a50cSMatti Aaltonen 		 * So we set the power once again.
63187d1a50cSMatti Aaltonen 		 */
63287d1a50cSMatti Aaltonen 		if (new_mode == WL1273_MODE_RX) {
63387d1a50cSMatti Aaltonen 			u16 val = WL1273_POWER_SET_FM;
63487d1a50cSMatti Aaltonen 
63587d1a50cSMatti Aaltonen 			if (radio->rds_on)
63687d1a50cSMatti Aaltonen 				val |= WL1273_POWER_SET_RDS;
63787d1a50cSMatti Aaltonen 
63896a9cc68SMatti Aaltonen 			r = core->write(core, WL1273_POWER_SET, val);
63987d1a50cSMatti Aaltonen 			if (r) {
64087d1a50cSMatti Aaltonen 				dev_err(dev, "%s: POWER_SET fails\n", __func__);
64187d1a50cSMatti Aaltonen 				goto fail;
64287d1a50cSMatti Aaltonen 			}
64387d1a50cSMatti Aaltonen 		} else if (new_mode == WL1273_MODE_TX) {
64496a9cc68SMatti Aaltonen 			r = core->write(core, WL1273_PUPD_SET,
64587d1a50cSMatti Aaltonen 					WL1273_PUPD_SET_ON);
64687d1a50cSMatti Aaltonen 			if (r) {
64787d1a50cSMatti Aaltonen 				dev_err(dev, "%s: PUPD_SET fails\n", __func__);
64887d1a50cSMatti Aaltonen 				goto fail;
64987d1a50cSMatti Aaltonen 			}
65087d1a50cSMatti Aaltonen 		}
65187d1a50cSMatti Aaltonen 	}
65287d1a50cSMatti Aaltonen 
65387d1a50cSMatti Aaltonen 	return 0;
65487d1a50cSMatti Aaltonen fail:
65587d1a50cSMatti Aaltonen 	if (pdata->disable)
65687d1a50cSMatti Aaltonen 		pdata->disable();
65787d1a50cSMatti Aaltonen 
65887d1a50cSMatti Aaltonen 	dev_dbg(dev, "%s: return: %d\n", __func__, r);
65987d1a50cSMatti Aaltonen 	return r;
66087d1a50cSMatti Aaltonen }
66187d1a50cSMatti Aaltonen 
66287d1a50cSMatti Aaltonen static int wl1273_fm_suspend(struct wl1273_device *radio)
66387d1a50cSMatti Aaltonen {
66487d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
66587d1a50cSMatti Aaltonen 	int r = 0;
66687d1a50cSMatti Aaltonen 
66787d1a50cSMatti Aaltonen 	/* Cannot go from OFF to SUSPENDED */
66887d1a50cSMatti Aaltonen 	if (core->mode == WL1273_MODE_RX)
66996a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_POWER_SET,
67087d1a50cSMatti Aaltonen 				WL1273_POWER_SET_RETENTION);
67187d1a50cSMatti Aaltonen 	else if (core->mode == WL1273_MODE_TX)
67296a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_PUPD_SET,
67387d1a50cSMatti Aaltonen 				WL1273_PUPD_SET_RETENTION);
67487d1a50cSMatti Aaltonen 	else
67587d1a50cSMatti Aaltonen 		r = -EINVAL;
67687d1a50cSMatti Aaltonen 
67787d1a50cSMatti Aaltonen 	if (r) {
67887d1a50cSMatti Aaltonen 		dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r);
67987d1a50cSMatti Aaltonen 		goto out;
68087d1a50cSMatti Aaltonen 	}
68187d1a50cSMatti Aaltonen 
68287d1a50cSMatti Aaltonen out:
68387d1a50cSMatti Aaltonen 	return r;
68487d1a50cSMatti Aaltonen }
68587d1a50cSMatti Aaltonen 
68687d1a50cSMatti Aaltonen static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode)
68787d1a50cSMatti Aaltonen {
68887d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
68987d1a50cSMatti Aaltonen 	struct device *dev = radio->dev;
69087d1a50cSMatti Aaltonen 	int old_mode;
69187d1a50cSMatti Aaltonen 	int r;
69287d1a50cSMatti Aaltonen 
69387d1a50cSMatti Aaltonen 	dev_dbg(dev, "%s\n", __func__);
69487d1a50cSMatti Aaltonen 	dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden);
69587d1a50cSMatti Aaltonen 
69687d1a50cSMatti Aaltonen 	old_mode = core->mode;
69787d1a50cSMatti Aaltonen 	if (mode & radio->forbidden) {
69887d1a50cSMatti Aaltonen 		r = -EPERM;
69987d1a50cSMatti Aaltonen 		goto out;
70087d1a50cSMatti Aaltonen 	}
70187d1a50cSMatti Aaltonen 
70287d1a50cSMatti Aaltonen 	switch (mode) {
70387d1a50cSMatti Aaltonen 	case WL1273_MODE_RX:
70487d1a50cSMatti Aaltonen 	case WL1273_MODE_TX:
70587d1a50cSMatti Aaltonen 		r = wl1273_fm_start(radio, mode);
70687d1a50cSMatti Aaltonen 		if (r) {
70787d1a50cSMatti Aaltonen 			dev_err(dev, "%s: Cannot start.\n", __func__);
70887d1a50cSMatti Aaltonen 			wl1273_fm_stop(radio);
70987d1a50cSMatti Aaltonen 			goto out;
71087d1a50cSMatti Aaltonen 		}
71187d1a50cSMatti Aaltonen 
71287d1a50cSMatti Aaltonen 		core->mode = mode;
71396a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
71487d1a50cSMatti Aaltonen 		if (r) {
71587d1a50cSMatti Aaltonen 			dev_err(dev, "INT_MASK_SET fails.\n");
71687d1a50cSMatti Aaltonen 			goto out;
71787d1a50cSMatti Aaltonen 		}
71887d1a50cSMatti Aaltonen 
71987d1a50cSMatti Aaltonen 		/* remember previous settings */
72087d1a50cSMatti Aaltonen 		if (mode == WL1273_MODE_RX) {
72187d1a50cSMatti Aaltonen 			r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
72287d1a50cSMatti Aaltonen 			if (r) {
72387d1a50cSMatti Aaltonen 				dev_err(dev, "set freq fails: %d.\n", r);
72487d1a50cSMatti Aaltonen 				goto out;
72587d1a50cSMatti Aaltonen 			}
72687d1a50cSMatti Aaltonen 
72787d1a50cSMatti Aaltonen 			r = core->set_volume(core, core->volume);
72887d1a50cSMatti Aaltonen 			if (r) {
72987d1a50cSMatti Aaltonen 				dev_err(dev, "set volume fails: %d.\n", r);
73087d1a50cSMatti Aaltonen 				goto out;
73187d1a50cSMatti Aaltonen 			}
73287d1a50cSMatti Aaltonen 
73387d1a50cSMatti Aaltonen 			dev_dbg(dev, "%s: Set vol: %d.\n", __func__,
73487d1a50cSMatti Aaltonen 				core->volume);
73587d1a50cSMatti Aaltonen 		} else {
73687d1a50cSMatti Aaltonen 			r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency);
73787d1a50cSMatti Aaltonen 			if (r) {
73887d1a50cSMatti Aaltonen 				dev_err(dev, "set freq fails: %d.\n", r);
73987d1a50cSMatti Aaltonen 				goto out;
74087d1a50cSMatti Aaltonen 			}
74187d1a50cSMatti Aaltonen 		}
74287d1a50cSMatti Aaltonen 
74387d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__);
74487d1a50cSMatti Aaltonen 
74587d1a50cSMatti Aaltonen 		r = core->set_audio(core, core->audio_mode);
74687d1a50cSMatti Aaltonen 		if (r)
74787d1a50cSMatti Aaltonen 			dev_err(dev, "Cannot set audio mode.\n");
74887d1a50cSMatti Aaltonen 		break;
74987d1a50cSMatti Aaltonen 
75087d1a50cSMatti Aaltonen 	case WL1273_MODE_OFF:
75187d1a50cSMatti Aaltonen 		r = wl1273_fm_stop(radio);
75287d1a50cSMatti Aaltonen 		if (r)
75387d1a50cSMatti Aaltonen 			dev_err(dev, "%s: Off fails: %d\n", __func__, r);
75487d1a50cSMatti Aaltonen 		else
75587d1a50cSMatti Aaltonen 			core->mode = WL1273_MODE_OFF;
75687d1a50cSMatti Aaltonen 
75787d1a50cSMatti Aaltonen 		break;
75887d1a50cSMatti Aaltonen 
75987d1a50cSMatti Aaltonen 	case WL1273_MODE_SUSPENDED:
76087d1a50cSMatti Aaltonen 		r = wl1273_fm_suspend(radio);
76187d1a50cSMatti Aaltonen 		if (r)
76287d1a50cSMatti Aaltonen 			dev_err(dev, "%s: Suspend fails: %d\n", __func__, r);
76387d1a50cSMatti Aaltonen 		else
76487d1a50cSMatti Aaltonen 			core->mode = WL1273_MODE_SUSPENDED;
76587d1a50cSMatti Aaltonen 
76687d1a50cSMatti Aaltonen 		break;
76787d1a50cSMatti Aaltonen 
76887d1a50cSMatti Aaltonen 	default:
76987d1a50cSMatti Aaltonen 		dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode);
77087d1a50cSMatti Aaltonen 		r = -EINVAL;
77187d1a50cSMatti Aaltonen 		break;
77287d1a50cSMatti Aaltonen 	}
77387d1a50cSMatti Aaltonen out:
77487d1a50cSMatti Aaltonen 	if (r)
77587d1a50cSMatti Aaltonen 		core->mode = old_mode;
77687d1a50cSMatti Aaltonen 
77787d1a50cSMatti Aaltonen 	return r;
77887d1a50cSMatti Aaltonen }
77987d1a50cSMatti Aaltonen 
78087d1a50cSMatti Aaltonen static int wl1273_fm_set_seek(struct wl1273_device *radio,
78187d1a50cSMatti Aaltonen 			      unsigned int wrap_around,
78287d1a50cSMatti Aaltonen 			      unsigned int seek_upward,
78387d1a50cSMatti Aaltonen 			      int level)
78487d1a50cSMatti Aaltonen {
78587d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
78687d1a50cSMatti Aaltonen 	int r = 0;
78787d1a50cSMatti Aaltonen 	unsigned int dir = (seek_upward == 0) ? 0 : 1;
78887d1a50cSMatti Aaltonen 	unsigned int f;
78987d1a50cSMatti Aaltonen 
79087d1a50cSMatti Aaltonen 	f = radio->rx_frequency;
79187d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "rx_frequency: %d\n", f);
79287d1a50cSMatti Aaltonen 
79387d1a50cSMatti Aaltonen 	if (dir && f + radio->spacing <= radio->rangehigh)
79487d1a50cSMatti Aaltonen 		r = wl1273_fm_set_rx_freq(radio, f + radio->spacing);
79587d1a50cSMatti Aaltonen 	else if (dir && wrap_around)
79687d1a50cSMatti Aaltonen 		r = wl1273_fm_set_rx_freq(radio, radio->rangelow);
79787d1a50cSMatti Aaltonen 	else if (f - radio->spacing >= radio->rangelow)
79887d1a50cSMatti Aaltonen 		r = wl1273_fm_set_rx_freq(radio, f - radio->spacing);
79987d1a50cSMatti Aaltonen 	else if (wrap_around)
80087d1a50cSMatti Aaltonen 		r = wl1273_fm_set_rx_freq(radio, radio->rangehigh);
80187d1a50cSMatti Aaltonen 
80287d1a50cSMatti Aaltonen 	if (r)
80387d1a50cSMatti Aaltonen 		goto out;
80487d1a50cSMatti Aaltonen 
80587d1a50cSMatti Aaltonen 	if (level < SCHAR_MIN || level > SCHAR_MAX)
80687d1a50cSMatti Aaltonen 		return -EINVAL;
80787d1a50cSMatti Aaltonen 
80887d1a50cSMatti Aaltonen 	INIT_COMPLETION(radio->busy);
80987d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s: BUSY\n", __func__);
81087d1a50cSMatti Aaltonen 
81196a9cc68SMatti Aaltonen 	r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
81287d1a50cSMatti Aaltonen 	if (r)
81387d1a50cSMatti Aaltonen 		goto out;
81487d1a50cSMatti Aaltonen 
81587d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
81687d1a50cSMatti Aaltonen 
81796a9cc68SMatti Aaltonen 	r = core->write(core, WL1273_SEARCH_LVL_SET, level);
81887d1a50cSMatti Aaltonen 	if (r)
81987d1a50cSMatti Aaltonen 		goto out;
82087d1a50cSMatti Aaltonen 
82196a9cc68SMatti Aaltonen 	r = core->write(core, WL1273_SEARCH_DIR_SET, dir);
82287d1a50cSMatti Aaltonen 	if (r)
82387d1a50cSMatti Aaltonen 		goto out;
82487d1a50cSMatti Aaltonen 
82596a9cc68SMatti Aaltonen 	r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
82687d1a50cSMatti Aaltonen 	if (r)
82787d1a50cSMatti Aaltonen 		goto out;
82887d1a50cSMatti Aaltonen 
82987d1a50cSMatti Aaltonen 	wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
83087d1a50cSMatti Aaltonen 	if (!(radio->irq_received & WL1273_BL_EVENT))
83187d1a50cSMatti Aaltonen 		goto out;
83287d1a50cSMatti Aaltonen 
83387d1a50cSMatti Aaltonen 	radio->irq_received &= ~WL1273_BL_EVENT;
83487d1a50cSMatti Aaltonen 
83587d1a50cSMatti Aaltonen 	if (!wrap_around)
83687d1a50cSMatti Aaltonen 		goto out;
83787d1a50cSMatti Aaltonen 
83887d1a50cSMatti Aaltonen 	/* Wrap around */
83987d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "Wrap around in HW seek.\n");
84087d1a50cSMatti Aaltonen 
84187d1a50cSMatti Aaltonen 	if (seek_upward)
84287d1a50cSMatti Aaltonen 		f = radio->rangelow;
84387d1a50cSMatti Aaltonen 	else
84487d1a50cSMatti Aaltonen 		f = radio->rangehigh;
84587d1a50cSMatti Aaltonen 
84687d1a50cSMatti Aaltonen 	r = wl1273_fm_set_rx_freq(radio, f);
84787d1a50cSMatti Aaltonen 	if (r)
84887d1a50cSMatti Aaltonen 		goto out;
84987d1a50cSMatti Aaltonen 
85087d1a50cSMatti Aaltonen 	INIT_COMPLETION(radio->busy);
85187d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s: BUSY\n", __func__);
85287d1a50cSMatti Aaltonen 
85396a9cc68SMatti Aaltonen 	r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
85487d1a50cSMatti Aaltonen 	if (r)
85587d1a50cSMatti Aaltonen 		goto out;
85687d1a50cSMatti Aaltonen 
85787d1a50cSMatti Aaltonen 	wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
85887d1a50cSMatti Aaltonen out:
85987d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r);
86087d1a50cSMatti Aaltonen 	return r;
86187d1a50cSMatti Aaltonen }
86287d1a50cSMatti Aaltonen 
86387d1a50cSMatti Aaltonen /**
86487d1a50cSMatti Aaltonen  * wl1273_fm_get_tx_ctune() -	Get the TX tuning capacitor value.
86587d1a50cSMatti Aaltonen  * @radio:			A pointer to the device struct.
86687d1a50cSMatti Aaltonen  */
86787d1a50cSMatti Aaltonen static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio)
86887d1a50cSMatti Aaltonen {
86987d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
87087d1a50cSMatti Aaltonen 	struct device *dev = radio->dev;
87187d1a50cSMatti Aaltonen 	u16 val;
87287d1a50cSMatti Aaltonen 	int r;
87387d1a50cSMatti Aaltonen 
87487d1a50cSMatti Aaltonen 	if (core->mode == WL1273_MODE_OFF ||
87587d1a50cSMatti Aaltonen 	    core->mode == WL1273_MODE_SUSPENDED)
87687d1a50cSMatti Aaltonen 		return -EPERM;
87787d1a50cSMatti Aaltonen 
87896a9cc68SMatti Aaltonen 	r = core->read(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
87987d1a50cSMatti Aaltonen 	if (r) {
88087d1a50cSMatti Aaltonen 		dev_err(dev, "%s: read error: %d\n", __func__, r);
88187d1a50cSMatti Aaltonen 		goto out;
88287d1a50cSMatti Aaltonen 	}
88387d1a50cSMatti Aaltonen 
88487d1a50cSMatti Aaltonen out:
88587d1a50cSMatti Aaltonen 	return val;
88687d1a50cSMatti Aaltonen }
88787d1a50cSMatti Aaltonen 
88887d1a50cSMatti Aaltonen /**
88987d1a50cSMatti Aaltonen  * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
89087d1a50cSMatti Aaltonen  * @radio:			 A pointer to the device struct.
89187d1a50cSMatti Aaltonen  * @preemphasis:		 The new pre-amphasis value.
89287d1a50cSMatti Aaltonen  *
89387d1a50cSMatti Aaltonen  * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
89487d1a50cSMatti Aaltonen  * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
89587d1a50cSMatti Aaltonen  */
89687d1a50cSMatti Aaltonen static int wl1273_fm_set_preemphasis(struct wl1273_device *radio,
89787d1a50cSMatti Aaltonen 				     unsigned int preemphasis)
89887d1a50cSMatti Aaltonen {
89987d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
90087d1a50cSMatti Aaltonen 	int r;
90187d1a50cSMatti Aaltonen 	u16 em;
90287d1a50cSMatti Aaltonen 
90387d1a50cSMatti Aaltonen 	if (core->mode == WL1273_MODE_OFF ||
90487d1a50cSMatti Aaltonen 	    core->mode == WL1273_MODE_SUSPENDED)
90587d1a50cSMatti Aaltonen 		return -EPERM;
90687d1a50cSMatti Aaltonen 
90787d1a50cSMatti Aaltonen 	mutex_lock(&core->lock);
90887d1a50cSMatti Aaltonen 
90987d1a50cSMatti Aaltonen 	switch (preemphasis) {
91087d1a50cSMatti Aaltonen 	case V4L2_PREEMPHASIS_DISABLED:
91187d1a50cSMatti Aaltonen 		em = 1;
91287d1a50cSMatti Aaltonen 		break;
91387d1a50cSMatti Aaltonen 	case V4L2_PREEMPHASIS_50_uS:
91487d1a50cSMatti Aaltonen 		em = 0;
91587d1a50cSMatti Aaltonen 		break;
91687d1a50cSMatti Aaltonen 	case V4L2_PREEMPHASIS_75_uS:
91787d1a50cSMatti Aaltonen 		em = 2;
91887d1a50cSMatti Aaltonen 		break;
91987d1a50cSMatti Aaltonen 	default:
92087d1a50cSMatti Aaltonen 		r = -EINVAL;
92187d1a50cSMatti Aaltonen 		goto out;
92287d1a50cSMatti Aaltonen 	}
92387d1a50cSMatti Aaltonen 
92496a9cc68SMatti Aaltonen 	r = core->write(core, WL1273_PREMPH_SET, em);
92587d1a50cSMatti Aaltonen 	if (r)
92687d1a50cSMatti Aaltonen 		goto out;
92787d1a50cSMatti Aaltonen 
92887d1a50cSMatti Aaltonen 	radio->preemphasis = preemphasis;
92987d1a50cSMatti Aaltonen 
93087d1a50cSMatti Aaltonen out:
93187d1a50cSMatti Aaltonen 	mutex_unlock(&core->lock);
93287d1a50cSMatti Aaltonen 	return r;
93387d1a50cSMatti Aaltonen }
93487d1a50cSMatti Aaltonen 
93587d1a50cSMatti Aaltonen static int wl1273_fm_rds_on(struct wl1273_device *radio)
93687d1a50cSMatti Aaltonen {
93787d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
93887d1a50cSMatti Aaltonen 	int r;
93987d1a50cSMatti Aaltonen 
94087d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
94187d1a50cSMatti Aaltonen 	if (radio->rds_on)
94287d1a50cSMatti Aaltonen 		return 0;
94387d1a50cSMatti Aaltonen 
94496a9cc68SMatti Aaltonen 	r = core->write(core, WL1273_POWER_SET,
94587d1a50cSMatti Aaltonen 			WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
94687d1a50cSMatti Aaltonen 	if (r)
94787d1a50cSMatti Aaltonen 		goto out;
94887d1a50cSMatti Aaltonen 
94987d1a50cSMatti Aaltonen 	r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
95087d1a50cSMatti Aaltonen 	if (r)
95187d1a50cSMatti Aaltonen 		dev_err(radio->dev, "set freq fails: %d.\n", r);
95287d1a50cSMatti Aaltonen out:
95387d1a50cSMatti Aaltonen 	return r;
95487d1a50cSMatti Aaltonen }
95587d1a50cSMatti Aaltonen 
95687d1a50cSMatti Aaltonen static int wl1273_fm_rds_off(struct wl1273_device *radio)
95787d1a50cSMatti Aaltonen {
95887d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
95987d1a50cSMatti Aaltonen 	int r;
96087d1a50cSMatti Aaltonen 
96187d1a50cSMatti Aaltonen 	if (!radio->rds_on)
96287d1a50cSMatti Aaltonen 		return 0;
96387d1a50cSMatti Aaltonen 
96487d1a50cSMatti Aaltonen 	radio->irq_flags &= ~WL1273_RDS_EVENT;
96587d1a50cSMatti Aaltonen 
96696a9cc68SMatti Aaltonen 	r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
96787d1a50cSMatti Aaltonen 	if (r)
96887d1a50cSMatti Aaltonen 		goto out;
96987d1a50cSMatti Aaltonen 
97087d1a50cSMatti Aaltonen 	/* Service pending read */
97187d1a50cSMatti Aaltonen 	wake_up_interruptible(&radio->read_queue);
97287d1a50cSMatti Aaltonen 
97387d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
97487d1a50cSMatti Aaltonen 
97596a9cc68SMatti Aaltonen 	r = core->write(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
97687d1a50cSMatti Aaltonen 	if (r)
97787d1a50cSMatti Aaltonen 		goto out;
97887d1a50cSMatti Aaltonen 
97987d1a50cSMatti Aaltonen 	r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
98087d1a50cSMatti Aaltonen 	if (r)
98187d1a50cSMatti Aaltonen 		dev_err(radio->dev, "set freq fails: %d.\n", r);
98287d1a50cSMatti Aaltonen out:
98387d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s: exiting...\n", __func__);
98487d1a50cSMatti Aaltonen 
98587d1a50cSMatti Aaltonen 	return r;
98687d1a50cSMatti Aaltonen }
98787d1a50cSMatti Aaltonen 
98887d1a50cSMatti Aaltonen static int wl1273_fm_set_rds(struct wl1273_device *radio, unsigned int new_mode)
98987d1a50cSMatti Aaltonen {
99087d1a50cSMatti Aaltonen 	int r = 0;
99187d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
99287d1a50cSMatti Aaltonen 
99387d1a50cSMatti Aaltonen 	if (core->mode == WL1273_MODE_OFF ||
99487d1a50cSMatti Aaltonen 	    core->mode == WL1273_MODE_SUSPENDED)
99587d1a50cSMatti Aaltonen 		return -EPERM;
99687d1a50cSMatti Aaltonen 
99787d1a50cSMatti Aaltonen 	if (new_mode == WL1273_RDS_RESET) {
99896a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_RDS_CNTRL_SET, 1);
99987d1a50cSMatti Aaltonen 		return r;
100087d1a50cSMatti Aaltonen 	}
100187d1a50cSMatti Aaltonen 
100287d1a50cSMatti Aaltonen 	if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) {
100396a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_RDS_DATA_ENB, 0);
100487d1a50cSMatti Aaltonen 	} else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) {
100596a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_RDS_DATA_ENB, 1);
100687d1a50cSMatti Aaltonen 	} else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) {
100787d1a50cSMatti Aaltonen 		r = wl1273_fm_rds_off(radio);
100887d1a50cSMatti Aaltonen 	} else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) {
100987d1a50cSMatti Aaltonen 		r = wl1273_fm_rds_on(radio);
101087d1a50cSMatti Aaltonen 	} else {
101187d1a50cSMatti Aaltonen 		dev_err(radio->dev, "%s: Unknown mode: %d\n",
101287d1a50cSMatti Aaltonen 			__func__, new_mode);
101387d1a50cSMatti Aaltonen 		r = -EINVAL;
101487d1a50cSMatti Aaltonen 	}
101587d1a50cSMatti Aaltonen 
101687d1a50cSMatti Aaltonen 	if (!r)
101787d1a50cSMatti Aaltonen 		radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false;
101887d1a50cSMatti Aaltonen 
101987d1a50cSMatti Aaltonen 	return r;
102087d1a50cSMatti Aaltonen }
102187d1a50cSMatti Aaltonen 
102287d1a50cSMatti Aaltonen static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf,
102387d1a50cSMatti Aaltonen 				    size_t count, loff_t *ppos)
102487d1a50cSMatti Aaltonen {
102587d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
102696a9cc68SMatti Aaltonen 	struct wl1273_core *core = radio->core;
102787d1a50cSMatti Aaltonen 	u16 val;
102887d1a50cSMatti Aaltonen 	int r;
102987d1a50cSMatti Aaltonen 
103087d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
103187d1a50cSMatti Aaltonen 
103296a9cc68SMatti Aaltonen 	if (core->mode != WL1273_MODE_TX)
103387d1a50cSMatti Aaltonen 		return count;
103487d1a50cSMatti Aaltonen 
103587d1a50cSMatti Aaltonen 	if (radio->rds_users == 0) {
103687d1a50cSMatti Aaltonen 		dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
103787d1a50cSMatti Aaltonen 		return 0;
103887d1a50cSMatti Aaltonen 	}
103987d1a50cSMatti Aaltonen 
104096a9cc68SMatti Aaltonen 	if (mutex_lock_interruptible(&core->lock))
104187d1a50cSMatti Aaltonen 		return -EINTR;
104287d1a50cSMatti Aaltonen 	/*
104387d1a50cSMatti Aaltonen 	 * Multiple processes can open the device, but only
104487d1a50cSMatti Aaltonen 	 * one gets to write to it.
104587d1a50cSMatti Aaltonen 	 */
104687d1a50cSMatti Aaltonen 	if (radio->owner && radio->owner != file) {
104787d1a50cSMatti Aaltonen 		r = -EBUSY;
104887d1a50cSMatti Aaltonen 		goto out;
104987d1a50cSMatti Aaltonen 	}
105087d1a50cSMatti Aaltonen 	radio->owner = file;
105187d1a50cSMatti Aaltonen 
105287d1a50cSMatti Aaltonen 	/* Manual Mode */
105387d1a50cSMatti Aaltonen 	if (count > 255)
105487d1a50cSMatti Aaltonen 		val = 255;
105587d1a50cSMatti Aaltonen 	else
105687d1a50cSMatti Aaltonen 		val = count;
105787d1a50cSMatti Aaltonen 
105896a9cc68SMatti Aaltonen 	core->write(core, WL1273_RDS_CONFIG_DATA_SET, val);
105987d1a50cSMatti Aaltonen 
106087d1a50cSMatti Aaltonen 	if (copy_from_user(radio->write_buf + 1, buf, val)) {
106187d1a50cSMatti Aaltonen 		r = -EFAULT;
106287d1a50cSMatti Aaltonen 		goto out;
106387d1a50cSMatti Aaltonen 	}
106487d1a50cSMatti Aaltonen 
106587d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "Count: %d\n", val);
106687d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf);
106787d1a50cSMatti Aaltonen 
106887d1a50cSMatti Aaltonen 	radio->write_buf[0] = WL1273_RDS_DATA_SET;
106996a9cc68SMatti Aaltonen 	core->write_data(core, radio->write_buf, val + 1);
107087d1a50cSMatti Aaltonen 
107187d1a50cSMatti Aaltonen 	r = val;
107287d1a50cSMatti Aaltonen out:
107396a9cc68SMatti Aaltonen 	mutex_unlock(&core->lock);
107487d1a50cSMatti Aaltonen 
107587d1a50cSMatti Aaltonen 	return r;
107687d1a50cSMatti Aaltonen }
107787d1a50cSMatti Aaltonen 
107887d1a50cSMatti Aaltonen static unsigned int wl1273_fm_fops_poll(struct file *file,
107987d1a50cSMatti Aaltonen 					struct poll_table_struct *pts)
108087d1a50cSMatti Aaltonen {
108187d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
108287d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
108387d1a50cSMatti Aaltonen 
108487d1a50cSMatti Aaltonen 	if (radio->owner && radio->owner != file)
108587d1a50cSMatti Aaltonen 		return -EBUSY;
108687d1a50cSMatti Aaltonen 
108787d1a50cSMatti Aaltonen 	radio->owner = file;
108887d1a50cSMatti Aaltonen 
108987d1a50cSMatti Aaltonen 	if (core->mode == WL1273_MODE_RX) {
109087d1a50cSMatti Aaltonen 		poll_wait(file, &radio->read_queue, pts);
109187d1a50cSMatti Aaltonen 
109287d1a50cSMatti Aaltonen 		if (radio->rd_index != radio->wr_index)
109387d1a50cSMatti Aaltonen 			return POLLIN | POLLRDNORM;
109487d1a50cSMatti Aaltonen 
109587d1a50cSMatti Aaltonen 	} else if (core->mode == WL1273_MODE_TX) {
109687d1a50cSMatti Aaltonen 		return POLLOUT | POLLWRNORM;
109787d1a50cSMatti Aaltonen 	}
109887d1a50cSMatti Aaltonen 
109987d1a50cSMatti Aaltonen 	return 0;
110087d1a50cSMatti Aaltonen }
110187d1a50cSMatti Aaltonen 
110287d1a50cSMatti Aaltonen static int wl1273_fm_fops_open(struct file *file)
110387d1a50cSMatti Aaltonen {
110487d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
110587d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
110687d1a50cSMatti Aaltonen 	int r = 0;
110787d1a50cSMatti Aaltonen 
110887d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
110987d1a50cSMatti Aaltonen 
111087d1a50cSMatti Aaltonen 	if (core->mode == WL1273_MODE_RX && radio->rds_on &&
111187d1a50cSMatti Aaltonen 	    !radio->rds_users) {
111287d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode);
111387d1a50cSMatti Aaltonen 
111487d1a50cSMatti Aaltonen 		if (mutex_lock_interruptible(&core->lock))
111587d1a50cSMatti Aaltonen 			return -EINTR;
111687d1a50cSMatti Aaltonen 
111787d1a50cSMatti Aaltonen 		radio->irq_flags |= WL1273_RDS_EVENT;
111887d1a50cSMatti Aaltonen 
111996a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_INT_MASK_SET,
112087d1a50cSMatti Aaltonen 				radio->irq_flags);
112187d1a50cSMatti Aaltonen 		if (r) {
112287d1a50cSMatti Aaltonen 			mutex_unlock(&core->lock);
112387d1a50cSMatti Aaltonen 			goto out;
112487d1a50cSMatti Aaltonen 		}
112587d1a50cSMatti Aaltonen 
112687d1a50cSMatti Aaltonen 		radio->rds_users++;
112787d1a50cSMatti Aaltonen 
112887d1a50cSMatti Aaltonen 		mutex_unlock(&core->lock);
112987d1a50cSMatti Aaltonen 	}
113087d1a50cSMatti Aaltonen out:
113187d1a50cSMatti Aaltonen 	return r;
113287d1a50cSMatti Aaltonen }
113387d1a50cSMatti Aaltonen 
113487d1a50cSMatti Aaltonen static int wl1273_fm_fops_release(struct file *file)
113587d1a50cSMatti Aaltonen {
113687d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
113787d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
113887d1a50cSMatti Aaltonen 	int r = 0;
113987d1a50cSMatti Aaltonen 
114087d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
114187d1a50cSMatti Aaltonen 
114287d1a50cSMatti Aaltonen 	if (radio->rds_users > 0) {
114387d1a50cSMatti Aaltonen 		radio->rds_users--;
114487d1a50cSMatti Aaltonen 		if (radio->rds_users == 0) {
114587d1a50cSMatti Aaltonen 			if (mutex_lock_interruptible(&core->lock))
114687d1a50cSMatti Aaltonen 				return -EINTR;
114787d1a50cSMatti Aaltonen 
114887d1a50cSMatti Aaltonen 			radio->irq_flags &= ~WL1273_RDS_EVENT;
114987d1a50cSMatti Aaltonen 
115087d1a50cSMatti Aaltonen 			if (core->mode == WL1273_MODE_RX) {
115196a9cc68SMatti Aaltonen 				r = core->write(core,
115287d1a50cSMatti Aaltonen 						WL1273_INT_MASK_SET,
115387d1a50cSMatti Aaltonen 						radio->irq_flags);
115487d1a50cSMatti Aaltonen 				if (r) {
115587d1a50cSMatti Aaltonen 					mutex_unlock(&core->lock);
115687d1a50cSMatti Aaltonen 					goto out;
115787d1a50cSMatti Aaltonen 				}
115887d1a50cSMatti Aaltonen 			}
115987d1a50cSMatti Aaltonen 			mutex_unlock(&core->lock);
116087d1a50cSMatti Aaltonen 		}
116187d1a50cSMatti Aaltonen 	}
116287d1a50cSMatti Aaltonen 
116387d1a50cSMatti Aaltonen 	if (file == radio->owner)
116487d1a50cSMatti Aaltonen 		radio->owner = NULL;
116587d1a50cSMatti Aaltonen out:
116687d1a50cSMatti Aaltonen 	return r;
116787d1a50cSMatti Aaltonen }
116887d1a50cSMatti Aaltonen 
116987d1a50cSMatti Aaltonen static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
117087d1a50cSMatti Aaltonen 				   size_t count, loff_t *ppos)
117187d1a50cSMatti Aaltonen {
117287d1a50cSMatti Aaltonen 	int r = 0;
117387d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
117487d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
117587d1a50cSMatti Aaltonen 	unsigned int block_count = 0;
117687d1a50cSMatti Aaltonen 	u16 val;
117787d1a50cSMatti Aaltonen 
117887d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
117987d1a50cSMatti Aaltonen 
118096a9cc68SMatti Aaltonen 	if (core->mode != WL1273_MODE_RX)
118187d1a50cSMatti Aaltonen 		return 0;
118287d1a50cSMatti Aaltonen 
118387d1a50cSMatti Aaltonen 	if (radio->rds_users == 0) {
118487d1a50cSMatti Aaltonen 		dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
118587d1a50cSMatti Aaltonen 		return 0;
118687d1a50cSMatti Aaltonen 	}
118787d1a50cSMatti Aaltonen 
118887d1a50cSMatti Aaltonen 	if (mutex_lock_interruptible(&core->lock))
118987d1a50cSMatti Aaltonen 		return -EINTR;
119087d1a50cSMatti Aaltonen 
119187d1a50cSMatti Aaltonen 	/*
119287d1a50cSMatti Aaltonen 	 * Multiple processes can open the device, but only
119387d1a50cSMatti Aaltonen 	 * one at a time gets read access.
119487d1a50cSMatti Aaltonen 	 */
119587d1a50cSMatti Aaltonen 	if (radio->owner && radio->owner != file) {
119687d1a50cSMatti Aaltonen 		r = -EBUSY;
119787d1a50cSMatti Aaltonen 		goto out;
119887d1a50cSMatti Aaltonen 	}
119987d1a50cSMatti Aaltonen 	radio->owner = file;
120087d1a50cSMatti Aaltonen 
120196a9cc68SMatti Aaltonen 	r = core->read(core, WL1273_RDS_SYNC_GET, &val);
120287d1a50cSMatti Aaltonen 	if (r) {
120387d1a50cSMatti Aaltonen 		dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__);
120487d1a50cSMatti Aaltonen 		goto out;
120587d1a50cSMatti Aaltonen 	} else if (val == 0) {
120687d1a50cSMatti Aaltonen 		dev_info(radio->dev, "RDS_SYNC: Not synchronized\n");
120787d1a50cSMatti Aaltonen 		r = -ENODATA;
120887d1a50cSMatti Aaltonen 		goto out;
120987d1a50cSMatti Aaltonen 	}
121087d1a50cSMatti Aaltonen 
121187d1a50cSMatti Aaltonen 	/* block if no new data available */
121287d1a50cSMatti Aaltonen 	while (radio->wr_index == radio->rd_index) {
121387d1a50cSMatti Aaltonen 		if (file->f_flags & O_NONBLOCK) {
121487d1a50cSMatti Aaltonen 			r = -EWOULDBLOCK;
121587d1a50cSMatti Aaltonen 			goto out;
121687d1a50cSMatti Aaltonen 		}
121787d1a50cSMatti Aaltonen 
121887d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__);
121987d1a50cSMatti Aaltonen 		if (wait_event_interruptible(radio->read_queue,
122087d1a50cSMatti Aaltonen 					     radio->wr_index !=
122187d1a50cSMatti Aaltonen 					     radio->rd_index) < 0) {
122287d1a50cSMatti Aaltonen 			r = -EINTR;
122387d1a50cSMatti Aaltonen 			goto out;
122487d1a50cSMatti Aaltonen 		}
122587d1a50cSMatti Aaltonen 	}
122687d1a50cSMatti Aaltonen 
122787d1a50cSMatti Aaltonen 	/* calculate block count from byte count */
122887d1a50cSMatti Aaltonen 	count /= RDS_BLOCK_SIZE;
122987d1a50cSMatti Aaltonen 
123087d1a50cSMatti Aaltonen 	/* copy RDS blocks from the internal buffer and to user buffer */
123187d1a50cSMatti Aaltonen 	while (block_count < count) {
123287d1a50cSMatti Aaltonen 		if (radio->rd_index == radio->wr_index)
123387d1a50cSMatti Aaltonen 			break;
123487d1a50cSMatti Aaltonen 
123587d1a50cSMatti Aaltonen 		/* always transfer complete RDS blocks */
123687d1a50cSMatti Aaltonen 		if (copy_to_user(buf, &radio->buffer[radio->rd_index],
123787d1a50cSMatti Aaltonen 				 RDS_BLOCK_SIZE))
123887d1a50cSMatti Aaltonen 			break;
123987d1a50cSMatti Aaltonen 
124087d1a50cSMatti Aaltonen 		/* increment and wrap the read pointer */
124187d1a50cSMatti Aaltonen 		radio->rd_index += RDS_BLOCK_SIZE;
124287d1a50cSMatti Aaltonen 		if (radio->rd_index >= radio->buf_size)
124387d1a50cSMatti Aaltonen 			radio->rd_index = 0;
124487d1a50cSMatti Aaltonen 
124587d1a50cSMatti Aaltonen 		/* increment counters */
124687d1a50cSMatti Aaltonen 		block_count++;
124787d1a50cSMatti Aaltonen 		buf += RDS_BLOCK_SIZE;
124887d1a50cSMatti Aaltonen 		r += RDS_BLOCK_SIZE;
124987d1a50cSMatti Aaltonen 	}
125087d1a50cSMatti Aaltonen 
125187d1a50cSMatti Aaltonen out:
125287d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s: exit\n", __func__);
125387d1a50cSMatti Aaltonen 	mutex_unlock(&core->lock);
125487d1a50cSMatti Aaltonen 
125587d1a50cSMatti Aaltonen 	return r;
125687d1a50cSMatti Aaltonen }
125787d1a50cSMatti Aaltonen 
125887d1a50cSMatti Aaltonen static const struct v4l2_file_operations wl1273_fops = {
125987d1a50cSMatti Aaltonen 	.owner		= THIS_MODULE,
126087d1a50cSMatti Aaltonen 	.read		= wl1273_fm_fops_read,
126187d1a50cSMatti Aaltonen 	.write		= wl1273_fm_fops_write,
126287d1a50cSMatti Aaltonen 	.poll		= wl1273_fm_fops_poll,
126334b8fc8eSMatti Aaltonen 	.unlocked_ioctl	= video_ioctl2,
126487d1a50cSMatti Aaltonen 	.open		= wl1273_fm_fops_open,
126587d1a50cSMatti Aaltonen 	.release	= wl1273_fm_fops_release,
126687d1a50cSMatti Aaltonen };
126787d1a50cSMatti Aaltonen 
126887d1a50cSMatti Aaltonen static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
126987d1a50cSMatti Aaltonen 				     struct v4l2_capability *capability)
127087d1a50cSMatti Aaltonen {
127187d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
127287d1a50cSMatti Aaltonen 
127387d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
127487d1a50cSMatti Aaltonen 
127587d1a50cSMatti Aaltonen 	strlcpy(capability->driver, WL1273_FM_DRIVER_NAME,
127687d1a50cSMatti Aaltonen 		sizeof(capability->driver));
127787d1a50cSMatti Aaltonen 	strlcpy(capability->card, "Texas Instruments Wl1273 FM Radio",
127887d1a50cSMatti Aaltonen 		sizeof(capability->card));
127987d1a50cSMatti Aaltonen 	strlcpy(capability->bus_info, radio->bus_type,
128087d1a50cSMatti Aaltonen 		sizeof(capability->bus_info));
128187d1a50cSMatti Aaltonen 
128287d1a50cSMatti Aaltonen 	capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
128387d1a50cSMatti Aaltonen 		V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
128487d1a50cSMatti Aaltonen 		V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
128587d1a50cSMatti Aaltonen 		V4L2_CAP_RDS_OUTPUT;
128687d1a50cSMatti Aaltonen 
128787d1a50cSMatti Aaltonen 	return 0;
128887d1a50cSMatti Aaltonen }
128987d1a50cSMatti Aaltonen 
129087d1a50cSMatti Aaltonen static int wl1273_fm_vidioc_g_input(struct file *file, void *priv,
129187d1a50cSMatti Aaltonen 				    unsigned int *i)
129287d1a50cSMatti Aaltonen {
129387d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
129487d1a50cSMatti Aaltonen 
129587d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
129687d1a50cSMatti Aaltonen 
129787d1a50cSMatti Aaltonen 	*i = 0;
129887d1a50cSMatti Aaltonen 
129987d1a50cSMatti Aaltonen 	return 0;
130087d1a50cSMatti Aaltonen }
130187d1a50cSMatti Aaltonen 
130287d1a50cSMatti Aaltonen static int wl1273_fm_vidioc_s_input(struct file *file, void *priv,
130387d1a50cSMatti Aaltonen 				    unsigned int i)
130487d1a50cSMatti Aaltonen {
130587d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
130687d1a50cSMatti Aaltonen 
130787d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
130887d1a50cSMatti Aaltonen 
130987d1a50cSMatti Aaltonen 	if (i != 0)
131087d1a50cSMatti Aaltonen 		return -EINVAL;
131187d1a50cSMatti Aaltonen 
131287d1a50cSMatti Aaltonen 	return 0;
131387d1a50cSMatti Aaltonen }
131487d1a50cSMatti Aaltonen 
131587d1a50cSMatti Aaltonen /**
131687d1a50cSMatti Aaltonen  * wl1273_fm_set_tx_power() -	Set the transmission power value.
131787d1a50cSMatti Aaltonen  * @core:			A pointer to the device struct.
131887d1a50cSMatti Aaltonen  * @power:			The new power value.
131987d1a50cSMatti Aaltonen  */
132087d1a50cSMatti Aaltonen static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power)
132187d1a50cSMatti Aaltonen {
132296a9cc68SMatti Aaltonen 	struct wl1273_core *core = radio->core;
132387d1a50cSMatti Aaltonen 	int r;
132487d1a50cSMatti Aaltonen 
132596a9cc68SMatti Aaltonen 	if (core->mode == WL1273_MODE_OFF ||
132696a9cc68SMatti Aaltonen 	    core->mode == WL1273_MODE_SUSPENDED)
132787d1a50cSMatti Aaltonen 		return -EPERM;
132887d1a50cSMatti Aaltonen 
132996a9cc68SMatti Aaltonen 	mutex_lock(&core->lock);
133087d1a50cSMatti Aaltonen 
133187d1a50cSMatti Aaltonen 	/* Convert the dBuV value to chip presentation */
133296a9cc68SMatti Aaltonen 	r = core->write(core, WL1273_POWER_LEV_SET, 122 - power);
133387d1a50cSMatti Aaltonen 	if (r)
133487d1a50cSMatti Aaltonen 		goto out;
133587d1a50cSMatti Aaltonen 
133687d1a50cSMatti Aaltonen 	radio->tx_power = power;
133787d1a50cSMatti Aaltonen 
133887d1a50cSMatti Aaltonen out:
133996a9cc68SMatti Aaltonen 	mutex_unlock(&core->lock);
134087d1a50cSMatti Aaltonen 	return r;
134187d1a50cSMatti Aaltonen }
134287d1a50cSMatti Aaltonen 
134387d1a50cSMatti Aaltonen #define WL1273_SPACING_50kHz	1
134487d1a50cSMatti Aaltonen #define WL1273_SPACING_100kHz	2
134587d1a50cSMatti Aaltonen #define WL1273_SPACING_200kHz	4
134687d1a50cSMatti Aaltonen 
134787d1a50cSMatti Aaltonen static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio,
134887d1a50cSMatti Aaltonen 				    unsigned int spacing)
134987d1a50cSMatti Aaltonen {
135096a9cc68SMatti Aaltonen 	struct wl1273_core *core = radio->core;
135187d1a50cSMatti Aaltonen 	int r;
135287d1a50cSMatti Aaltonen 
135387d1a50cSMatti Aaltonen 	if (spacing == 0) {
135496a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_SCAN_SPACING_SET,
135587d1a50cSMatti Aaltonen 				WL1273_SPACING_100kHz);
135687d1a50cSMatti Aaltonen 		radio->spacing = 100;
135787d1a50cSMatti Aaltonen 	} else if (spacing - 50000 < 25000) {
135896a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_SCAN_SPACING_SET,
135987d1a50cSMatti Aaltonen 				WL1273_SPACING_50kHz);
136087d1a50cSMatti Aaltonen 		radio->spacing = 50;
136187d1a50cSMatti Aaltonen 	} else if (spacing - 100000 < 50000) {
136296a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_SCAN_SPACING_SET,
136387d1a50cSMatti Aaltonen 				WL1273_SPACING_100kHz);
136487d1a50cSMatti Aaltonen 		radio->spacing = 100;
136587d1a50cSMatti Aaltonen 	} else {
136696a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_SCAN_SPACING_SET,
136787d1a50cSMatti Aaltonen 				WL1273_SPACING_200kHz);
136887d1a50cSMatti Aaltonen 		radio->spacing = 200;
136987d1a50cSMatti Aaltonen 	}
137087d1a50cSMatti Aaltonen 
137187d1a50cSMatti Aaltonen 	return r;
137287d1a50cSMatti Aaltonen }
137387d1a50cSMatti Aaltonen 
137487d1a50cSMatti Aaltonen static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
137587d1a50cSMatti Aaltonen {
137687d1a50cSMatti Aaltonen 	struct wl1273_device *radio = ctrl->priv;
137787d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
137887d1a50cSMatti Aaltonen 
137987d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
138087d1a50cSMatti Aaltonen 
138187d1a50cSMatti Aaltonen 	if (mutex_lock_interruptible(&core->lock))
138287d1a50cSMatti Aaltonen 		return -EINTR;
138387d1a50cSMatti Aaltonen 
138487d1a50cSMatti Aaltonen 	switch (ctrl->id) {
138587d1a50cSMatti Aaltonen 	case  V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1386ddac5c10SHans Verkuil 		ctrl->val = wl1273_fm_get_tx_ctune(radio);
138787d1a50cSMatti Aaltonen 		break;
138887d1a50cSMatti Aaltonen 
138987d1a50cSMatti Aaltonen 	default:
139087d1a50cSMatti Aaltonen 		dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
139187d1a50cSMatti Aaltonen 			 __func__, ctrl->id);
139287d1a50cSMatti Aaltonen 		break;
139387d1a50cSMatti Aaltonen 	}
139487d1a50cSMatti Aaltonen 
139587d1a50cSMatti Aaltonen 	mutex_unlock(&core->lock);
139687d1a50cSMatti Aaltonen 
139787d1a50cSMatti Aaltonen 	return 0;
139887d1a50cSMatti Aaltonen }
139987d1a50cSMatti Aaltonen 
140087d1a50cSMatti Aaltonen #define WL1273_MUTE_SOFT_ENABLE    (1 << 0)
140187d1a50cSMatti Aaltonen #define WL1273_MUTE_AC             (1 << 1)
140287d1a50cSMatti Aaltonen #define WL1273_MUTE_HARD_LEFT      (1 << 2)
140387d1a50cSMatti Aaltonen #define WL1273_MUTE_HARD_RIGHT     (1 << 3)
140487d1a50cSMatti Aaltonen #define WL1273_MUTE_SOFT_FORCE     (1 << 4)
140587d1a50cSMatti Aaltonen 
140687d1a50cSMatti Aaltonen static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl)
140787d1a50cSMatti Aaltonen {
140887d1a50cSMatti Aaltonen 	return container_of(ctrl->handler, struct wl1273_device, ctrl_handler);
140987d1a50cSMatti Aaltonen }
141087d1a50cSMatti Aaltonen 
141187d1a50cSMatti Aaltonen static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl)
141287d1a50cSMatti Aaltonen {
141387d1a50cSMatti Aaltonen 	struct wl1273_device *radio = to_radio(ctrl);
141487d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
141587d1a50cSMatti Aaltonen 	int r = 0;
141687d1a50cSMatti Aaltonen 
141787d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
141887d1a50cSMatti Aaltonen 
141987d1a50cSMatti Aaltonen 	switch (ctrl->id) {
142087d1a50cSMatti Aaltonen 	case V4L2_CID_AUDIO_MUTE:
142187d1a50cSMatti Aaltonen 		if (mutex_lock_interruptible(&core->lock))
142287d1a50cSMatti Aaltonen 			return -EINTR;
142387d1a50cSMatti Aaltonen 
142487d1a50cSMatti Aaltonen 		if (core->mode == WL1273_MODE_RX && ctrl->val)
142596a9cc68SMatti Aaltonen 			r = core->write(core,
142687d1a50cSMatti Aaltonen 					WL1273_MUTE_STATUS_SET,
142787d1a50cSMatti Aaltonen 					WL1273_MUTE_HARD_LEFT |
142887d1a50cSMatti Aaltonen 					WL1273_MUTE_HARD_RIGHT);
142987d1a50cSMatti Aaltonen 		else if (core->mode == WL1273_MODE_RX)
143096a9cc68SMatti Aaltonen 			r = core->write(core,
143187d1a50cSMatti Aaltonen 					WL1273_MUTE_STATUS_SET, 0x0);
143287d1a50cSMatti Aaltonen 		else if (core->mode == WL1273_MODE_TX && ctrl->val)
143396a9cc68SMatti Aaltonen 			r = core->write(core, WL1273_MUTE, 1);
143487d1a50cSMatti Aaltonen 		else if (core->mode == WL1273_MODE_TX)
143596a9cc68SMatti Aaltonen 			r = core->write(core, WL1273_MUTE, 0);
143687d1a50cSMatti Aaltonen 
143787d1a50cSMatti Aaltonen 		mutex_unlock(&core->lock);
143887d1a50cSMatti Aaltonen 		break;
143987d1a50cSMatti Aaltonen 
144087d1a50cSMatti Aaltonen 	case V4L2_CID_AUDIO_VOLUME:
144187d1a50cSMatti Aaltonen 		if (ctrl->val == 0)
144287d1a50cSMatti Aaltonen 			r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF);
144387d1a50cSMatti Aaltonen 		else
144487d1a50cSMatti Aaltonen 			r =  core->set_volume(core, core->volume);
144587d1a50cSMatti Aaltonen 		break;
144687d1a50cSMatti Aaltonen 
144787d1a50cSMatti Aaltonen 	case V4L2_CID_TUNE_PREEMPHASIS:
144887d1a50cSMatti Aaltonen 		r = wl1273_fm_set_preemphasis(radio, ctrl->val);
144987d1a50cSMatti Aaltonen 		break;
145087d1a50cSMatti Aaltonen 
145187d1a50cSMatti Aaltonen 	case V4L2_CID_TUNE_POWER_LEVEL:
145287d1a50cSMatti Aaltonen 		r = wl1273_fm_set_tx_power(radio, ctrl->val);
145387d1a50cSMatti Aaltonen 		break;
145487d1a50cSMatti Aaltonen 
145587d1a50cSMatti Aaltonen 	default:
145687d1a50cSMatti Aaltonen 		dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
145787d1a50cSMatti Aaltonen 			 __func__, ctrl->id);
145887d1a50cSMatti Aaltonen 		break;
145987d1a50cSMatti Aaltonen 	}
146087d1a50cSMatti Aaltonen 
146187d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
146287d1a50cSMatti Aaltonen 	return r;
146387d1a50cSMatti Aaltonen }
146487d1a50cSMatti Aaltonen 
146587d1a50cSMatti Aaltonen static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv,
146687d1a50cSMatti Aaltonen 				    struct v4l2_audio *audio)
146787d1a50cSMatti Aaltonen {
146887d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
146987d1a50cSMatti Aaltonen 
147087d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
147187d1a50cSMatti Aaltonen 
147287d1a50cSMatti Aaltonen 	if (audio->index > 1)
147387d1a50cSMatti Aaltonen 		return -EINVAL;
147487d1a50cSMatti Aaltonen 
147587d1a50cSMatti Aaltonen 	strlcpy(audio->name, "Radio", sizeof(audio->name));
147687d1a50cSMatti Aaltonen 	audio->capability = V4L2_AUDCAP_STEREO;
147787d1a50cSMatti Aaltonen 
147887d1a50cSMatti Aaltonen 	return 0;
147987d1a50cSMatti Aaltonen }
148087d1a50cSMatti Aaltonen 
148187d1a50cSMatti Aaltonen static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
14820e8025b9SHans Verkuil 				    const struct v4l2_audio *audio)
148387d1a50cSMatti Aaltonen {
148487d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
148587d1a50cSMatti Aaltonen 
148687d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
148787d1a50cSMatti Aaltonen 
148887d1a50cSMatti Aaltonen 	if (audio->index != 0)
148987d1a50cSMatti Aaltonen 		return -EINVAL;
149087d1a50cSMatti Aaltonen 
149187d1a50cSMatti Aaltonen 	return 0;
149287d1a50cSMatti Aaltonen }
149387d1a50cSMatti Aaltonen 
149487d1a50cSMatti Aaltonen #define WL1273_RDS_NOT_SYNCHRONIZED 0
149587d1a50cSMatti Aaltonen #define WL1273_RDS_SYNCHRONIZED 1
149687d1a50cSMatti Aaltonen 
149787d1a50cSMatti Aaltonen static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv,
149887d1a50cSMatti Aaltonen 				    struct v4l2_tuner *tuner)
149987d1a50cSMatti Aaltonen {
150087d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
150187d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
150287d1a50cSMatti Aaltonen 	u16 val;
150387d1a50cSMatti Aaltonen 	int r;
150487d1a50cSMatti Aaltonen 
150587d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
150687d1a50cSMatti Aaltonen 
150787d1a50cSMatti Aaltonen 	if (tuner->index > 0)
150887d1a50cSMatti Aaltonen 		return -EINVAL;
150987d1a50cSMatti Aaltonen 
151087d1a50cSMatti Aaltonen 	strlcpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name));
151187d1a50cSMatti Aaltonen 	tuner->type = V4L2_TUNER_RADIO;
151287d1a50cSMatti Aaltonen 
151387d1a50cSMatti Aaltonen 	tuner->rangelow	= WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
151487d1a50cSMatti Aaltonen 	tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
151587d1a50cSMatti Aaltonen 
151687d1a50cSMatti Aaltonen 	tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
151754f6019bSHans Verkuil 		V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO |
151854f6019bSHans Verkuil 		V4L2_TUNER_CAP_HWSEEK_BOUNDED | V4L2_TUNER_CAP_HWSEEK_WRAP;
151987d1a50cSMatti Aaltonen 
152087d1a50cSMatti Aaltonen 	if (radio->stereo)
152187d1a50cSMatti Aaltonen 		tuner->audmode = V4L2_TUNER_MODE_STEREO;
152287d1a50cSMatti Aaltonen 	else
152387d1a50cSMatti Aaltonen 		tuner->audmode = V4L2_TUNER_MODE_MONO;
152487d1a50cSMatti Aaltonen 
152587d1a50cSMatti Aaltonen 	if (core->mode != WL1273_MODE_RX)
152687d1a50cSMatti Aaltonen 		return 0;
152787d1a50cSMatti Aaltonen 
152887d1a50cSMatti Aaltonen 	if (mutex_lock_interruptible(&core->lock))
152987d1a50cSMatti Aaltonen 		return -EINTR;
153087d1a50cSMatti Aaltonen 
153196a9cc68SMatti Aaltonen 	r = core->read(core, WL1273_STEREO_GET, &val);
153287d1a50cSMatti Aaltonen 	if (r)
153387d1a50cSMatti Aaltonen 		goto out;
153487d1a50cSMatti Aaltonen 
153587d1a50cSMatti Aaltonen 	if (val == 1)
153687d1a50cSMatti Aaltonen 		tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
153787d1a50cSMatti Aaltonen 	else
153887d1a50cSMatti Aaltonen 		tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
153987d1a50cSMatti Aaltonen 
154096a9cc68SMatti Aaltonen 	r = core->read(core, WL1273_RSSI_LVL_GET, &val);
154187d1a50cSMatti Aaltonen 	if (r)
154287d1a50cSMatti Aaltonen 		goto out;
154387d1a50cSMatti Aaltonen 
154487d1a50cSMatti Aaltonen 	tuner->signal = (s16) val;
154587d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);
154687d1a50cSMatti Aaltonen 
154787d1a50cSMatti Aaltonen 	tuner->afc = 0;
154887d1a50cSMatti Aaltonen 
154996a9cc68SMatti Aaltonen 	r = core->read(core, WL1273_RDS_SYNC_GET, &val);
155087d1a50cSMatti Aaltonen 	if (r)
155187d1a50cSMatti Aaltonen 		goto out;
155287d1a50cSMatti Aaltonen 
155387d1a50cSMatti Aaltonen 	if (val == WL1273_RDS_SYNCHRONIZED)
155487d1a50cSMatti Aaltonen 		tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
155587d1a50cSMatti Aaltonen out:
155687d1a50cSMatti Aaltonen 	mutex_unlock(&core->lock);
155787d1a50cSMatti Aaltonen 
155887d1a50cSMatti Aaltonen 	return r;
155987d1a50cSMatti Aaltonen }
156087d1a50cSMatti Aaltonen 
156187d1a50cSMatti Aaltonen static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
156287d1a50cSMatti Aaltonen 				    struct v4l2_tuner *tuner)
156387d1a50cSMatti Aaltonen {
156487d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
156587d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
156687d1a50cSMatti Aaltonen 	int r = 0;
156787d1a50cSMatti Aaltonen 
156887d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
156987d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index);
157087d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name);
157187d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability);
157287d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans);
157387d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow);
157487d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh);
157587d1a50cSMatti Aaltonen 
157687d1a50cSMatti Aaltonen 	if (tuner->index > 0)
157787d1a50cSMatti Aaltonen 		return -EINVAL;
157887d1a50cSMatti Aaltonen 
157987d1a50cSMatti Aaltonen 	if (mutex_lock_interruptible(&core->lock))
158087d1a50cSMatti Aaltonen 		return -EINTR;
158187d1a50cSMatti Aaltonen 
158287d1a50cSMatti Aaltonen 	r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
158387d1a50cSMatti Aaltonen 	if (r)
158487d1a50cSMatti Aaltonen 		goto out;
158587d1a50cSMatti Aaltonen 
158687d1a50cSMatti Aaltonen 	if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS)
158787d1a50cSMatti Aaltonen 		r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
158887d1a50cSMatti Aaltonen 	else
158987d1a50cSMatti Aaltonen 		r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
159087d1a50cSMatti Aaltonen 
159187d1a50cSMatti Aaltonen 	if (r)
159287d1a50cSMatti Aaltonen 		dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r);
159387d1a50cSMatti Aaltonen 
159487d1a50cSMatti Aaltonen 	if (tuner->audmode == V4L2_TUNER_MODE_MONO) {
159596a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
159687d1a50cSMatti Aaltonen 		if (r < 0) {
159787d1a50cSMatti Aaltonen 			dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
159887d1a50cSMatti Aaltonen 				 __func__, r);
159987d1a50cSMatti Aaltonen 			goto out;
160087d1a50cSMatti Aaltonen 		}
160187d1a50cSMatti Aaltonen 		radio->stereo = false;
160287d1a50cSMatti Aaltonen 	} else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) {
160396a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
160487d1a50cSMatti Aaltonen 		if (r < 0) {
160587d1a50cSMatti Aaltonen 			dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
160687d1a50cSMatti Aaltonen 				 __func__, r);
160787d1a50cSMatti Aaltonen 			goto out;
160887d1a50cSMatti Aaltonen 		}
160987d1a50cSMatti Aaltonen 		radio->stereo = true;
161087d1a50cSMatti Aaltonen 	} else {
161187d1a50cSMatti Aaltonen 		dev_err(radio->dev, "%s: tuner->audmode: %d\n",
161287d1a50cSMatti Aaltonen 			 __func__, tuner->audmode);
161387d1a50cSMatti Aaltonen 		r = -EINVAL;
161487d1a50cSMatti Aaltonen 		goto out;
161587d1a50cSMatti Aaltonen 	}
161687d1a50cSMatti Aaltonen 
161787d1a50cSMatti Aaltonen out:
161887d1a50cSMatti Aaltonen 	mutex_unlock(&core->lock);
161987d1a50cSMatti Aaltonen 
162087d1a50cSMatti Aaltonen 	return r;
162187d1a50cSMatti Aaltonen }
162287d1a50cSMatti Aaltonen 
162387d1a50cSMatti Aaltonen static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
162487d1a50cSMatti Aaltonen 					struct v4l2_frequency *freq)
162587d1a50cSMatti Aaltonen {
162687d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
162787d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
162887d1a50cSMatti Aaltonen 
162987d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
163087d1a50cSMatti Aaltonen 
163187d1a50cSMatti Aaltonen 	if (mutex_lock_interruptible(&core->lock))
163287d1a50cSMatti Aaltonen 		return -EINTR;
163387d1a50cSMatti Aaltonen 
163487d1a50cSMatti Aaltonen 	freq->type = V4L2_TUNER_RADIO;
163587d1a50cSMatti Aaltonen 	freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio));
163687d1a50cSMatti Aaltonen 
163787d1a50cSMatti Aaltonen 	mutex_unlock(&core->lock);
163887d1a50cSMatti Aaltonen 
163987d1a50cSMatti Aaltonen 	return 0;
164087d1a50cSMatti Aaltonen }
164187d1a50cSMatti Aaltonen 
164287d1a50cSMatti Aaltonen static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
1643*b530a447SHans Verkuil 					const struct v4l2_frequency *freq)
164487d1a50cSMatti Aaltonen {
164587d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
164687d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
164787d1a50cSMatti Aaltonen 	int r;
164887d1a50cSMatti Aaltonen 
164987d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency);
165087d1a50cSMatti Aaltonen 
165187d1a50cSMatti Aaltonen 	if (freq->type != V4L2_TUNER_RADIO) {
165287d1a50cSMatti Aaltonen 		dev_dbg(radio->dev,
165387d1a50cSMatti Aaltonen 			"freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
165487d1a50cSMatti Aaltonen 		return -EINVAL;
165587d1a50cSMatti Aaltonen 	}
165687d1a50cSMatti Aaltonen 
165787d1a50cSMatti Aaltonen 	if (mutex_lock_interruptible(&core->lock))
165887d1a50cSMatti Aaltonen 		return -EINTR;
165987d1a50cSMatti Aaltonen 
166087d1a50cSMatti Aaltonen 	if (core->mode == WL1273_MODE_RX) {
166187d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "freq: %d\n", freq->frequency);
166287d1a50cSMatti Aaltonen 
166387d1a50cSMatti Aaltonen 		r = wl1273_fm_set_rx_freq(radio,
166487d1a50cSMatti Aaltonen 					  WL1273_INV_FREQ(freq->frequency));
166587d1a50cSMatti Aaltonen 		if (r)
166687d1a50cSMatti Aaltonen 			dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
166787d1a50cSMatti Aaltonen 				 ": set frequency failed with %d\n", r);
166887d1a50cSMatti Aaltonen 	} else {
166987d1a50cSMatti Aaltonen 		r = wl1273_fm_set_tx_freq(radio,
167087d1a50cSMatti Aaltonen 					  WL1273_INV_FREQ(freq->frequency));
167187d1a50cSMatti Aaltonen 		if (r)
167287d1a50cSMatti Aaltonen 			dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
167387d1a50cSMatti Aaltonen 				 ": set frequency failed with %d\n", r);
167487d1a50cSMatti Aaltonen 	}
167587d1a50cSMatti Aaltonen 
167687d1a50cSMatti Aaltonen 	mutex_unlock(&core->lock);
167787d1a50cSMatti Aaltonen 
167887d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
167987d1a50cSMatti Aaltonen 	return r;
168087d1a50cSMatti Aaltonen }
168187d1a50cSMatti Aaltonen 
168287d1a50cSMatti Aaltonen #define WL1273_DEFAULT_SEEK_LEVEL	7
168387d1a50cSMatti Aaltonen 
168487d1a50cSMatti Aaltonen static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1685ec6f4328SHans Verkuil 					   const struct v4l2_hw_freq_seek *seek)
168687d1a50cSMatti Aaltonen {
168787d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
168887d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
168987d1a50cSMatti Aaltonen 	int r;
169087d1a50cSMatti Aaltonen 
169187d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
169287d1a50cSMatti Aaltonen 
169387d1a50cSMatti Aaltonen 	if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
169487d1a50cSMatti Aaltonen 		return -EINVAL;
169587d1a50cSMatti Aaltonen 
1696617ade61SHans Verkuil 	if (file->f_flags & O_NONBLOCK)
1697617ade61SHans Verkuil 		return -EWOULDBLOCK;
1698617ade61SHans Verkuil 
169987d1a50cSMatti Aaltonen 	if (mutex_lock_interruptible(&core->lock))
170087d1a50cSMatti Aaltonen 		return -EINTR;
170187d1a50cSMatti Aaltonen 
170287d1a50cSMatti Aaltonen 	r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
170387d1a50cSMatti Aaltonen 	if (r)
170487d1a50cSMatti Aaltonen 		goto out;
170587d1a50cSMatti Aaltonen 
170687d1a50cSMatti Aaltonen 	r = wl1273_fm_tx_set_spacing(radio, seek->spacing);
170787d1a50cSMatti Aaltonen 	if (r)
170887d1a50cSMatti Aaltonen 		dev_warn(radio->dev, "HW seek failed: %d\n", r);
170987d1a50cSMatti Aaltonen 
171087d1a50cSMatti Aaltonen 	r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward,
171187d1a50cSMatti Aaltonen 			       WL1273_DEFAULT_SEEK_LEVEL);
171287d1a50cSMatti Aaltonen 	if (r)
171387d1a50cSMatti Aaltonen 		dev_warn(radio->dev, "HW seek failed: %d\n", r);
171487d1a50cSMatti Aaltonen 
171587d1a50cSMatti Aaltonen out:
171687d1a50cSMatti Aaltonen 	mutex_unlock(&core->lock);
171787d1a50cSMatti Aaltonen 	return r;
171887d1a50cSMatti Aaltonen }
171987d1a50cSMatti Aaltonen 
172087d1a50cSMatti Aaltonen static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
17213f70e1f5SHans Verkuil 					const struct v4l2_modulator *modulator)
172287d1a50cSMatti Aaltonen {
172387d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
172487d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
172587d1a50cSMatti Aaltonen 	int r = 0;
172687d1a50cSMatti Aaltonen 
172787d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
172887d1a50cSMatti Aaltonen 
172987d1a50cSMatti Aaltonen 	if (modulator->index > 0)
173087d1a50cSMatti Aaltonen 		return -EINVAL;
173187d1a50cSMatti Aaltonen 
173287d1a50cSMatti Aaltonen 	if (mutex_lock_interruptible(&core->lock))
173387d1a50cSMatti Aaltonen 		return -EINTR;
173487d1a50cSMatti Aaltonen 
173587d1a50cSMatti Aaltonen 	r = wl1273_fm_set_mode(radio, WL1273_MODE_TX);
173687d1a50cSMatti Aaltonen 	if (r)
173787d1a50cSMatti Aaltonen 		goto out;
173887d1a50cSMatti Aaltonen 
173987d1a50cSMatti Aaltonen 	if (modulator->txsubchans & V4L2_TUNER_SUB_RDS)
174087d1a50cSMatti Aaltonen 		r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
174187d1a50cSMatti Aaltonen 	else
174287d1a50cSMatti Aaltonen 		r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
174387d1a50cSMatti Aaltonen 
174487d1a50cSMatti Aaltonen 	if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
174596a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
174687d1a50cSMatti Aaltonen 	else
174796a9cc68SMatti Aaltonen 		r = core->write(core, WL1273_MONO_SET,
174887d1a50cSMatti Aaltonen 				WL1273_RX_STEREO);
174987d1a50cSMatti Aaltonen 	if (r < 0)
175087d1a50cSMatti Aaltonen 		dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
175187d1a50cSMatti Aaltonen 			 "MONO_SET fails: %d\n", r);
175287d1a50cSMatti Aaltonen out:
175387d1a50cSMatti Aaltonen 	mutex_unlock(&core->lock);
175487d1a50cSMatti Aaltonen 
175587d1a50cSMatti Aaltonen 	return r;
175687d1a50cSMatti Aaltonen }
175787d1a50cSMatti Aaltonen 
175887d1a50cSMatti Aaltonen static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
175987d1a50cSMatti Aaltonen 					struct v4l2_modulator *modulator)
176087d1a50cSMatti Aaltonen {
176187d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
176287d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
176387d1a50cSMatti Aaltonen 	u16 val;
176487d1a50cSMatti Aaltonen 	int r;
176587d1a50cSMatti Aaltonen 
176687d1a50cSMatti Aaltonen 	dev_dbg(radio->dev, "%s\n", __func__);
176787d1a50cSMatti Aaltonen 
176887d1a50cSMatti Aaltonen 	strlcpy(modulator->name, WL1273_FM_DRIVER_NAME,
176987d1a50cSMatti Aaltonen 		sizeof(modulator->name));
177087d1a50cSMatti Aaltonen 
177187d1a50cSMatti Aaltonen 	modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
177287d1a50cSMatti Aaltonen 	modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
177387d1a50cSMatti Aaltonen 
177487d1a50cSMatti Aaltonen 	modulator->capability =  V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
177587d1a50cSMatti Aaltonen 		V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
177687d1a50cSMatti Aaltonen 
177787d1a50cSMatti Aaltonen 	if (core->mode != WL1273_MODE_TX)
177887d1a50cSMatti Aaltonen 		return 0;
177987d1a50cSMatti Aaltonen 
178087d1a50cSMatti Aaltonen 	if (mutex_lock_interruptible(&core->lock))
178187d1a50cSMatti Aaltonen 		return -EINTR;
178287d1a50cSMatti Aaltonen 
178396a9cc68SMatti Aaltonen 	r = core->read(core, WL1273_MONO_SET, &val);
178487d1a50cSMatti Aaltonen 	if (r)
178587d1a50cSMatti Aaltonen 		goto out;
178687d1a50cSMatti Aaltonen 
178787d1a50cSMatti Aaltonen 	if (val == WL1273_TX_STEREO)
178887d1a50cSMatti Aaltonen 		modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
178987d1a50cSMatti Aaltonen 	else
179087d1a50cSMatti Aaltonen 		modulator->txsubchans = V4L2_TUNER_SUB_MONO;
179187d1a50cSMatti Aaltonen 
179287d1a50cSMatti Aaltonen 	if (radio->rds_on)
179387d1a50cSMatti Aaltonen 		modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
179487d1a50cSMatti Aaltonen out:
179587d1a50cSMatti Aaltonen 	mutex_unlock(&core->lock);
179687d1a50cSMatti Aaltonen 
179787d1a50cSMatti Aaltonen 	return 0;
179887d1a50cSMatti Aaltonen }
179987d1a50cSMatti Aaltonen 
180087d1a50cSMatti Aaltonen static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
180187d1a50cSMatti Aaltonen {
180287d1a50cSMatti Aaltonen 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
180387d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
180487d1a50cSMatti Aaltonen 	struct device *dev = radio->dev;
180587d1a50cSMatti Aaltonen 	u16 val;
180687d1a50cSMatti Aaltonen 	int r;
180787d1a50cSMatti Aaltonen 
180887d1a50cSMatti Aaltonen 	dev_info(dev, DRIVER_DESC);
180987d1a50cSMatti Aaltonen 
181087d1a50cSMatti Aaltonen 	if (core->mode == WL1273_MODE_OFF) {
181187d1a50cSMatti Aaltonen 		dev_info(dev, "Mode: Off\n");
181287d1a50cSMatti Aaltonen 		return 0;
181387d1a50cSMatti Aaltonen 	}
181487d1a50cSMatti Aaltonen 
181587d1a50cSMatti Aaltonen 	if (core->mode == WL1273_MODE_SUSPENDED) {
181687d1a50cSMatti Aaltonen 		dev_info(dev, "Mode: Suspended\n");
181787d1a50cSMatti Aaltonen 		return 0;
181887d1a50cSMatti Aaltonen 	}
181987d1a50cSMatti Aaltonen 
182096a9cc68SMatti Aaltonen 	r = core->read(core, WL1273_ASIC_ID_GET, &val);
182187d1a50cSMatti Aaltonen 	if (r)
182287d1a50cSMatti Aaltonen 		dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__);
182387d1a50cSMatti Aaltonen 	else
182487d1a50cSMatti Aaltonen 		dev_info(dev, "ASIC_ID: 0x%04x\n", val);
182587d1a50cSMatti Aaltonen 
182696a9cc68SMatti Aaltonen 	r = core->read(core, WL1273_ASIC_VER_GET, &val);
182787d1a50cSMatti Aaltonen 	if (r)
182887d1a50cSMatti Aaltonen 		dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__);
182987d1a50cSMatti Aaltonen 	else
183087d1a50cSMatti Aaltonen 		dev_info(dev, "ASIC Version: 0x%04x\n", val);
183187d1a50cSMatti Aaltonen 
183296a9cc68SMatti Aaltonen 	r = core->read(core, WL1273_FIRM_VER_GET, &val);
183387d1a50cSMatti Aaltonen 	if (r)
183487d1a50cSMatti Aaltonen 		dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__);
183587d1a50cSMatti Aaltonen 	else
183687d1a50cSMatti Aaltonen 		dev_info(dev, "FW version: %d(0x%04x)\n", val, val);
183787d1a50cSMatti Aaltonen 
183896a9cc68SMatti Aaltonen 	r = core->read(core, WL1273_BAND_SET, &val);
183987d1a50cSMatti Aaltonen 	if (r)
184087d1a50cSMatti Aaltonen 		dev_err(dev, "%s: Get BAND fails.\n", __func__);
184187d1a50cSMatti Aaltonen 	else
184287d1a50cSMatti Aaltonen 		dev_info(dev, "BAND: %d\n", val);
184387d1a50cSMatti Aaltonen 
184487d1a50cSMatti Aaltonen 	if (core->mode == WL1273_MODE_TX) {
184596a9cc68SMatti Aaltonen 		r = core->read(core, WL1273_PUPD_SET, &val);
184687d1a50cSMatti Aaltonen 		if (r)
184787d1a50cSMatti Aaltonen 			dev_err(dev, "%s: Get PUPD fails.\n", __func__);
184887d1a50cSMatti Aaltonen 		else
184987d1a50cSMatti Aaltonen 			dev_info(dev, "PUPD: 0x%04x\n", val);
185087d1a50cSMatti Aaltonen 
185196a9cc68SMatti Aaltonen 		r = core->read(core, WL1273_CHANL_SET, &val);
185287d1a50cSMatti Aaltonen 		if (r)
185387d1a50cSMatti Aaltonen 			dev_err(dev, "%s: Get CHANL fails.\n", __func__);
185487d1a50cSMatti Aaltonen 		else
185587d1a50cSMatti Aaltonen 			dev_info(dev, "Tx frequency: %dkHz\n", val*10);
185687d1a50cSMatti Aaltonen 	} else if (core->mode == WL1273_MODE_RX) {
185787d1a50cSMatti Aaltonen 		int bf = radio->rangelow;
185887d1a50cSMatti Aaltonen 
185996a9cc68SMatti Aaltonen 		r = core->read(core, WL1273_FREQ_SET, &val);
186087d1a50cSMatti Aaltonen 		if (r)
186187d1a50cSMatti Aaltonen 			dev_err(dev, "%s: Get FREQ fails.\n", __func__);
186287d1a50cSMatti Aaltonen 		else
186387d1a50cSMatti Aaltonen 			dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50);
186487d1a50cSMatti Aaltonen 
186596a9cc68SMatti Aaltonen 		r = core->read(core, WL1273_MOST_MODE_SET, &val);
186687d1a50cSMatti Aaltonen 		if (r)
186787d1a50cSMatti Aaltonen 			dev_err(dev, "%s: Get MOST_MODE fails.\n",
186887d1a50cSMatti Aaltonen 				__func__);
186987d1a50cSMatti Aaltonen 		else if (val == 0)
187087d1a50cSMatti Aaltonen 			dev_info(dev, "MOST_MODE: Stereo according to blend\n");
187187d1a50cSMatti Aaltonen 		else if (val == 1)
187287d1a50cSMatti Aaltonen 			dev_info(dev, "MOST_MODE: Force mono output\n");
187387d1a50cSMatti Aaltonen 		else
187487d1a50cSMatti Aaltonen 			dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val);
187587d1a50cSMatti Aaltonen 
187696a9cc68SMatti Aaltonen 		r = core->read(core, WL1273_MOST_BLEND_SET, &val);
187787d1a50cSMatti Aaltonen 		if (r)
187887d1a50cSMatti Aaltonen 			dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__);
187987d1a50cSMatti Aaltonen 		else if (val == 0)
188087d1a50cSMatti Aaltonen 			dev_info(dev,
188187d1a50cSMatti Aaltonen 				 "MOST_BLEND: Switched blend & hysteresis.\n");
188287d1a50cSMatti Aaltonen 		else if (val == 1)
188387d1a50cSMatti Aaltonen 			dev_info(dev, "MOST_BLEND: Soft blend.\n");
188487d1a50cSMatti Aaltonen 		else
188587d1a50cSMatti Aaltonen 			dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val);
188687d1a50cSMatti Aaltonen 
188796a9cc68SMatti Aaltonen 		r = core->read(core, WL1273_STEREO_GET, &val);
188887d1a50cSMatti Aaltonen 		if (r)
188987d1a50cSMatti Aaltonen 			dev_err(dev, "%s: Get STEREO fails.\n", __func__);
189087d1a50cSMatti Aaltonen 		else if (val == 0)
189187d1a50cSMatti Aaltonen 			dev_info(dev, "STEREO: Not detected\n");
189287d1a50cSMatti Aaltonen 		else if (val == 1)
189387d1a50cSMatti Aaltonen 			dev_info(dev, "STEREO: Detected\n");
189487d1a50cSMatti Aaltonen 		else
189587d1a50cSMatti Aaltonen 			dev_info(dev, "STEREO: Unexpected value: %d\n", val);
189687d1a50cSMatti Aaltonen 
189796a9cc68SMatti Aaltonen 		r = core->read(core, WL1273_RSSI_LVL_GET, &val);
189887d1a50cSMatti Aaltonen 		if (r)
189987d1a50cSMatti Aaltonen 			dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__);
190087d1a50cSMatti Aaltonen 		else
190187d1a50cSMatti Aaltonen 			dev_info(dev, "RX signal strength: %d\n", (s16) val);
190287d1a50cSMatti Aaltonen 
190396a9cc68SMatti Aaltonen 		r = core->read(core, WL1273_POWER_SET, &val);
190487d1a50cSMatti Aaltonen 		if (r)
190587d1a50cSMatti Aaltonen 			dev_err(dev, "%s: Get POWER fails.\n", __func__);
190687d1a50cSMatti Aaltonen 		else
190787d1a50cSMatti Aaltonen 			dev_info(dev, "POWER: 0x%04x\n", val);
190887d1a50cSMatti Aaltonen 
190996a9cc68SMatti Aaltonen 		r = core->read(core, WL1273_INT_MASK_SET, &val);
191087d1a50cSMatti Aaltonen 		if (r)
191187d1a50cSMatti Aaltonen 			dev_err(dev, "%s: Get INT_MASK fails.\n", __func__);
191287d1a50cSMatti Aaltonen 		else
191387d1a50cSMatti Aaltonen 			dev_info(dev, "INT_MASK: 0x%04x\n", val);
191487d1a50cSMatti Aaltonen 
191596a9cc68SMatti Aaltonen 		r = core->read(core, WL1273_RDS_SYNC_GET, &val);
191687d1a50cSMatti Aaltonen 		if (r)
191787d1a50cSMatti Aaltonen 			dev_err(dev, "%s: Get RDS_SYNC fails.\n",
191887d1a50cSMatti Aaltonen 				__func__);
191987d1a50cSMatti Aaltonen 		else if (val == 0)
192087d1a50cSMatti Aaltonen 			dev_info(dev, "RDS_SYNC: Not synchronized\n");
192187d1a50cSMatti Aaltonen 
192287d1a50cSMatti Aaltonen 		else if (val == 1)
192387d1a50cSMatti Aaltonen 			dev_info(dev, "RDS_SYNC: Synchronized\n");
192487d1a50cSMatti Aaltonen 		else
192587d1a50cSMatti Aaltonen 			dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val);
192687d1a50cSMatti Aaltonen 
192796a9cc68SMatti Aaltonen 		r = core->read(core, WL1273_I2S_MODE_CONFIG_SET, &val);
192887d1a50cSMatti Aaltonen 		if (r)
192987d1a50cSMatti Aaltonen 			dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n",
193087d1a50cSMatti Aaltonen 				__func__);
193187d1a50cSMatti Aaltonen 		else
193287d1a50cSMatti Aaltonen 			dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val);
193387d1a50cSMatti Aaltonen 
193496a9cc68SMatti Aaltonen 		r = core->read(core, WL1273_VOLUME_SET, &val);
193587d1a50cSMatti Aaltonen 		if (r)
193687d1a50cSMatti Aaltonen 			dev_err(dev, "%s: Get VOLUME fails.\n", __func__);
193787d1a50cSMatti Aaltonen 		else
193887d1a50cSMatti Aaltonen 			dev_info(dev, "VOLUME: 0x%04x\n", val);
193987d1a50cSMatti Aaltonen 	}
194087d1a50cSMatti Aaltonen 
194187d1a50cSMatti Aaltonen 	return 0;
194287d1a50cSMatti Aaltonen }
194387d1a50cSMatti Aaltonen 
194487d1a50cSMatti Aaltonen static void wl1273_vdev_release(struct video_device *dev)
194587d1a50cSMatti Aaltonen {
194687d1a50cSMatti Aaltonen }
194787d1a50cSMatti Aaltonen 
194887d1a50cSMatti Aaltonen static const struct v4l2_ctrl_ops wl1273_ctrl_ops = {
194987d1a50cSMatti Aaltonen 	.s_ctrl = wl1273_fm_vidioc_s_ctrl,
195087d1a50cSMatti Aaltonen 	.g_volatile_ctrl = wl1273_fm_g_volatile_ctrl,
195187d1a50cSMatti Aaltonen };
195287d1a50cSMatti Aaltonen 
195387d1a50cSMatti Aaltonen static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
195487d1a50cSMatti Aaltonen 	.vidioc_querycap	= wl1273_fm_vidioc_querycap,
195587d1a50cSMatti Aaltonen 	.vidioc_g_input		= wl1273_fm_vidioc_g_input,
195687d1a50cSMatti Aaltonen 	.vidioc_s_input		= wl1273_fm_vidioc_s_input,
195787d1a50cSMatti Aaltonen 	.vidioc_g_audio		= wl1273_fm_vidioc_g_audio,
195887d1a50cSMatti Aaltonen 	.vidioc_s_audio		= wl1273_fm_vidioc_s_audio,
195987d1a50cSMatti Aaltonen 	.vidioc_g_tuner		= wl1273_fm_vidioc_g_tuner,
196087d1a50cSMatti Aaltonen 	.vidioc_s_tuner		= wl1273_fm_vidioc_s_tuner,
196187d1a50cSMatti Aaltonen 	.vidioc_g_frequency	= wl1273_fm_vidioc_g_frequency,
196287d1a50cSMatti Aaltonen 	.vidioc_s_frequency	= wl1273_fm_vidioc_s_frequency,
196387d1a50cSMatti Aaltonen 	.vidioc_s_hw_freq_seek	= wl1273_fm_vidioc_s_hw_freq_seek,
196487d1a50cSMatti Aaltonen 	.vidioc_g_modulator	= wl1273_fm_vidioc_g_modulator,
196587d1a50cSMatti Aaltonen 	.vidioc_s_modulator	= wl1273_fm_vidioc_s_modulator,
196687d1a50cSMatti Aaltonen 	.vidioc_log_status      = wl1273_fm_vidioc_log_status,
196787d1a50cSMatti Aaltonen };
196887d1a50cSMatti Aaltonen 
196987d1a50cSMatti Aaltonen static struct video_device wl1273_viddev_template = {
197087d1a50cSMatti Aaltonen 	.fops			= &wl1273_fops,
197187d1a50cSMatti Aaltonen 	.ioctl_ops		= &wl1273_ioctl_ops,
197287d1a50cSMatti Aaltonen 	.name			= WL1273_FM_DRIVER_NAME,
197387d1a50cSMatti Aaltonen 	.release		= wl1273_vdev_release,
1974ce4a3d52SHans Verkuil 	.vfl_dir		= VFL_DIR_TX,
197587d1a50cSMatti Aaltonen };
197687d1a50cSMatti Aaltonen 
197787d1a50cSMatti Aaltonen static int wl1273_fm_radio_remove(struct platform_device *pdev)
197887d1a50cSMatti Aaltonen {
197987d1a50cSMatti Aaltonen 	struct wl1273_device *radio = platform_get_drvdata(pdev);
198087d1a50cSMatti Aaltonen 	struct wl1273_core *core = radio->core;
198187d1a50cSMatti Aaltonen 
198287d1a50cSMatti Aaltonen 	dev_info(&pdev->dev, "%s.\n", __func__);
198387d1a50cSMatti Aaltonen 
198487d1a50cSMatti Aaltonen 	free_irq(core->client->irq, radio);
198587d1a50cSMatti Aaltonen 	core->pdata->free_resources();
198687d1a50cSMatti Aaltonen 
198787d1a50cSMatti Aaltonen 	v4l2_ctrl_handler_free(&radio->ctrl_handler);
198887d1a50cSMatti Aaltonen 	video_unregister_device(&radio->videodev);
198987d1a50cSMatti Aaltonen 	v4l2_device_unregister(&radio->v4l2dev);
199087d1a50cSMatti Aaltonen 
199187d1a50cSMatti Aaltonen 	return 0;
199287d1a50cSMatti Aaltonen }
199387d1a50cSMatti Aaltonen 
19944c62e976SGreg Kroah-Hartman static int wl1273_fm_radio_probe(struct platform_device *pdev)
199587d1a50cSMatti Aaltonen {
19969e554696SSamuel Ortiz 	struct wl1273_core **core = pdev->dev.platform_data;
199787d1a50cSMatti Aaltonen 	struct wl1273_device *radio;
199887d1a50cSMatti Aaltonen 	struct v4l2_ctrl *ctrl;
199987d1a50cSMatti Aaltonen 	int r = 0;
200087d1a50cSMatti Aaltonen 
200187d1a50cSMatti Aaltonen 	pr_debug("%s\n", __func__);
200287d1a50cSMatti Aaltonen 
200387d1a50cSMatti Aaltonen 	if (!core) {
200487d1a50cSMatti Aaltonen 		dev_err(&pdev->dev, "No platform data.\n");
200587d1a50cSMatti Aaltonen 		r = -EINVAL;
200687d1a50cSMatti Aaltonen 		goto pdata_err;
200787d1a50cSMatti Aaltonen 	}
200887d1a50cSMatti Aaltonen 
2009d91e0139SJulia Lawall 	radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
201087d1a50cSMatti Aaltonen 	if (!radio) {
201187d1a50cSMatti Aaltonen 		r = -ENOMEM;
201287d1a50cSMatti Aaltonen 		goto pdata_err;
201387d1a50cSMatti Aaltonen 	}
201487d1a50cSMatti Aaltonen 
201587d1a50cSMatti Aaltonen 	/* RDS buffer allocation */
201687d1a50cSMatti Aaltonen 	radio->buf_size = rds_buf * RDS_BLOCK_SIZE;
2017d91e0139SJulia Lawall 	radio->buffer = devm_kzalloc(&pdev->dev, radio->buf_size, GFP_KERNEL);
201887d1a50cSMatti Aaltonen 	if (!radio->buffer) {
201987d1a50cSMatti Aaltonen 		pr_err("Cannot allocate memory for RDS buffer.\n");
202087d1a50cSMatti Aaltonen 		r = -ENOMEM;
2021d91e0139SJulia Lawall 		goto pdata_err;
202287d1a50cSMatti Aaltonen 	}
202387d1a50cSMatti Aaltonen 
202487d1a50cSMatti Aaltonen 	radio->core = *core;
202587d1a50cSMatti Aaltonen 	radio->irq_flags = WL1273_IRQ_MASK;
202687d1a50cSMatti Aaltonen 	radio->dev = &radio->core->client->dev;
202787d1a50cSMatti Aaltonen 	radio->rds_on = false;
202887d1a50cSMatti Aaltonen 	radio->core->mode = WL1273_MODE_OFF;
202987d1a50cSMatti Aaltonen 	radio->tx_power = 118;
203087d1a50cSMatti Aaltonen 	radio->core->audio_mode = WL1273_AUDIO_ANALOG;
203187d1a50cSMatti Aaltonen 	radio->band = WL1273_BAND_OTHER;
203287d1a50cSMatti Aaltonen 	radio->core->i2s_mode = WL1273_I2S_DEF_MODE;
203387d1a50cSMatti Aaltonen 	radio->core->channel_number = 2;
203487d1a50cSMatti Aaltonen 	radio->core->volume = WL1273_DEFAULT_VOLUME;
203587d1a50cSMatti Aaltonen 	radio->rx_frequency = WL1273_BAND_OTHER_LOW;
203687d1a50cSMatti Aaltonen 	radio->tx_frequency = WL1273_BAND_OTHER_HIGH;
203787d1a50cSMatti Aaltonen 	radio->rangelow = WL1273_BAND_OTHER_LOW;
203887d1a50cSMatti Aaltonen 	radio->rangehigh = WL1273_BAND_OTHER_HIGH;
203987d1a50cSMatti Aaltonen 	radio->stereo = true;
204087d1a50cSMatti Aaltonen 	radio->bus_type = "I2C";
204187d1a50cSMatti Aaltonen 
204287d1a50cSMatti Aaltonen 	if (radio->core->pdata->request_resources) {
204387d1a50cSMatti Aaltonen 		r = radio->core->pdata->request_resources(radio->core->client);
204487d1a50cSMatti Aaltonen 		if (r) {
204587d1a50cSMatti Aaltonen 			dev_err(radio->dev, WL1273_FM_DRIVER_NAME
204687d1a50cSMatti Aaltonen 				": Cannot get platform data\n");
2047d91e0139SJulia Lawall 			goto pdata_err;
204887d1a50cSMatti Aaltonen 		}
204987d1a50cSMatti Aaltonen 
205087d1a50cSMatti Aaltonen 		dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq);
205187d1a50cSMatti Aaltonen 
205287d1a50cSMatti Aaltonen 		r = request_threaded_irq(radio->core->client->irq, NULL,
205387d1a50cSMatti Aaltonen 					 wl1273_fm_irq_thread_handler,
205487d1a50cSMatti Aaltonen 					 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
205587d1a50cSMatti Aaltonen 					 "wl1273-fm", radio);
205687d1a50cSMatti Aaltonen 		if (r < 0) {
205787d1a50cSMatti Aaltonen 			dev_err(radio->dev, WL1273_FM_DRIVER_NAME
205887d1a50cSMatti Aaltonen 				": Unable to register IRQ handler: %d\n", r);
205987d1a50cSMatti Aaltonen 			goto err_request_irq;
206087d1a50cSMatti Aaltonen 		}
206187d1a50cSMatti Aaltonen 	} else {
206287d1a50cSMatti Aaltonen 		dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ"
206387d1a50cSMatti Aaltonen 			" not configured");
206487d1a50cSMatti Aaltonen 		r = -EINVAL;
2065d91e0139SJulia Lawall 		goto pdata_err;
206687d1a50cSMatti Aaltonen 	}
206787d1a50cSMatti Aaltonen 
206887d1a50cSMatti Aaltonen 	init_completion(&radio->busy);
206987d1a50cSMatti Aaltonen 	init_waitqueue_head(&radio->read_queue);
207087d1a50cSMatti Aaltonen 
2071d91e0139SJulia Lawall 	radio->write_buf = devm_kzalloc(&pdev->dev, 256, GFP_KERNEL);
207287d1a50cSMatti Aaltonen 	if (!radio->write_buf) {
207387d1a50cSMatti Aaltonen 		r = -ENOMEM;
207487d1a50cSMatti Aaltonen 		goto write_buf_err;
207587d1a50cSMatti Aaltonen 	}
207687d1a50cSMatti Aaltonen 
207787d1a50cSMatti Aaltonen 	radio->dev = &pdev->dev;
207887d1a50cSMatti Aaltonen 	radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
207987d1a50cSMatti Aaltonen 	radio->rds_users = 0;
208087d1a50cSMatti Aaltonen 
208187d1a50cSMatti Aaltonen 	r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
208287d1a50cSMatti Aaltonen 	if (r) {
208387d1a50cSMatti Aaltonen 		dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
2084d91e0139SJulia Lawall 		goto write_buf_err;
208587d1a50cSMatti Aaltonen 	}
208687d1a50cSMatti Aaltonen 
208787d1a50cSMatti Aaltonen 	/* V4L2 configuration */
20882b9b325dSEzequiel Garcia 	radio->videodev = wl1273_viddev_template;
208987d1a50cSMatti Aaltonen 
209087d1a50cSMatti Aaltonen 	radio->videodev.v4l2_dev = &radio->v4l2dev;
209187d1a50cSMatti Aaltonen 
209287d1a50cSMatti Aaltonen 	v4l2_ctrl_handler_init(&radio->ctrl_handler, 6);
209387d1a50cSMatti Aaltonen 
209487d1a50cSMatti Aaltonen 	/* add in ascending ID order */
209587d1a50cSMatti Aaltonen 	v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
209687d1a50cSMatti Aaltonen 			  V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1,
209787d1a50cSMatti Aaltonen 			  WL1273_DEFAULT_VOLUME);
209887d1a50cSMatti Aaltonen 
209987d1a50cSMatti Aaltonen 	v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
210087d1a50cSMatti Aaltonen 			  V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
210187d1a50cSMatti Aaltonen 
210287d1a50cSMatti Aaltonen 	v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops,
210387d1a50cSMatti Aaltonen 			       V4L2_CID_TUNE_PREEMPHASIS,
210487d1a50cSMatti Aaltonen 			       V4L2_PREEMPHASIS_75_uS, 0x03,
210587d1a50cSMatti Aaltonen 			       V4L2_PREEMPHASIS_50_uS);
210687d1a50cSMatti Aaltonen 
210787d1a50cSMatti Aaltonen 	v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
210887d1a50cSMatti Aaltonen 			  V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118);
210987d1a50cSMatti Aaltonen 
211087d1a50cSMatti Aaltonen 	ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
211187d1a50cSMatti Aaltonen 				 V4L2_CID_TUNE_ANTENNA_CAPACITOR,
211287d1a50cSMatti Aaltonen 				 0, 255, 1, 255);
211387d1a50cSMatti Aaltonen 	if (ctrl)
211488365105SHans Verkuil 		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
211587d1a50cSMatti Aaltonen 
211687d1a50cSMatti Aaltonen 	if (radio->ctrl_handler.error) {
211787d1a50cSMatti Aaltonen 		r = radio->ctrl_handler.error;
211887d1a50cSMatti Aaltonen 		dev_err(&pdev->dev, "Ctrl handler error: %d\n", r);
211987d1a50cSMatti Aaltonen 		goto handler_init_err;
212087d1a50cSMatti Aaltonen 	}
212187d1a50cSMatti Aaltonen 
212287d1a50cSMatti Aaltonen 	video_set_drvdata(&radio->videodev, radio);
212387d1a50cSMatti Aaltonen 	platform_set_drvdata(pdev, radio);
212487d1a50cSMatti Aaltonen 
212587d1a50cSMatti Aaltonen 	/* register video device */
212687d1a50cSMatti Aaltonen 	r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
212787d1a50cSMatti Aaltonen 	if (r) {
212887d1a50cSMatti Aaltonen 		dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
212987d1a50cSMatti Aaltonen 			": Could not register video device\n");
213087d1a50cSMatti Aaltonen 		goto handler_init_err;
213187d1a50cSMatti Aaltonen 	}
213287d1a50cSMatti Aaltonen 
213387d1a50cSMatti Aaltonen 	return 0;
213487d1a50cSMatti Aaltonen 
213587d1a50cSMatti Aaltonen handler_init_err:
213687d1a50cSMatti Aaltonen 	v4l2_ctrl_handler_free(&radio->ctrl_handler);
213787d1a50cSMatti Aaltonen 	v4l2_device_unregister(&radio->v4l2dev);
213887d1a50cSMatti Aaltonen write_buf_err:
213987d1a50cSMatti Aaltonen 	free_irq(radio->core->client->irq, radio);
214087d1a50cSMatti Aaltonen err_request_irq:
214187d1a50cSMatti Aaltonen 	radio->core->pdata->free_resources();
214287d1a50cSMatti Aaltonen pdata_err:
214387d1a50cSMatti Aaltonen 	return r;
214487d1a50cSMatti Aaltonen }
214587d1a50cSMatti Aaltonen 
214687d1a50cSMatti Aaltonen static struct platform_driver wl1273_fm_radio_driver = {
214787d1a50cSMatti Aaltonen 	.probe		= wl1273_fm_radio_probe,
21484c62e976SGreg Kroah-Hartman 	.remove		= wl1273_fm_radio_remove,
214987d1a50cSMatti Aaltonen 	.driver		= {
215087d1a50cSMatti Aaltonen 		.name	= "wl1273_fm_radio",
215187d1a50cSMatti Aaltonen 		.owner	= THIS_MODULE,
215287d1a50cSMatti Aaltonen 	},
215387d1a50cSMatti Aaltonen };
215487d1a50cSMatti Aaltonen 
21551d6629b1SAxel Lin module_platform_driver(wl1273_fm_radio_driver);
215687d1a50cSMatti Aaltonen 
215787d1a50cSMatti Aaltonen MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
215887d1a50cSMatti Aaltonen MODULE_DESCRIPTION(DRIVER_DESC);
215987d1a50cSMatti Aaltonen MODULE_LICENSE("GPL");
21601d6629b1SAxel Lin MODULE_ALIAS("platform:wl1273_fm_radio");
2161