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