<commit_before>/*
 * Driver for the Texas Instruments WL1273 FM radio.
 *
 * Copyright (C) 2011 Nokia Corporation
 * Author: <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/interrupt.h>
#include <linux/mfd/wl1273-core.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>

#define DRIVER_DESC "Wl1273 FM Radio"

#define WL1273_POWER_SET_OFF		0
#define WL1273_POWER_SET_FM		BIT(0)
#define WL1273_POWER_SET_RDS		BIT(1)
#define WL1273_POWER_SET_RETENTION	BIT(4)

#define WL1273_PUPD_SET_OFF		0x00
#define WL1273_PUPD_SET_ON		0x01
#define WL1273_PUPD_SET_RETENTION	0x10

#define WL1273_FREQ(x)		(x * 10000 / 625)
#define WL1273_INV_FREQ(x)	(x * 625 / 10000)

/*
 * static int radio_nr - The number of the radio device
 *
 * The default is 0.
 */
static int radio_nr;
module_param(radio_nr, int, 0);
MODULE_PARM_DESC(radio_nr, "The number of the radio device. Default = 0");

struct wl1273_device {
	char *bus_type;

	u8 forbidden;
	unsigned int preemphasis;
	unsigned int spacing;
	unsigned int tx_power;
	unsigned int rx_frequency;
	unsigned int tx_frequency;
	unsigned int rangelow;
	unsigned int rangehigh;
	unsigned int band;
	bool stereo;

	/* RDS */
	unsigned int rds_on;

	wait_queue_head_t read_queue;
	struct mutex lock; /* for serializing fm radio operations */
	struct completion busy;

	unsigned char *buffer;
	unsigned int buf_size;
	unsigned int rd_index;
	unsigned int wr_index;

	/* Selected interrupts */
	u16 irq_flags;
	u16 irq_received;

	struct v4l2_ctrl_handler ctrl_handler;
	struct v4l2_device v4l2dev;
	struct video_device videodev;
	struct device *dev;
	struct wl1273_core *core;
	struct file *owner;
	char *write_buf;
	unsigned int rds_users;
};

#define WL1273_IRQ_MASK	 (WL1273_FR_EVENT		|	\
			  WL1273_POW_ENB_EVENT)

/*
 * static unsigned int rds_buf - the number of RDS buffer blocks used.
 *
 * The default number is 100.
 */
static unsigned int rds_buf = 100;
module_param(rds_buf, uint, 0);
MODULE_PARM_DESC(rds_buf, "Number of RDS buffer entries. Default = 100");

static int wl1273_fm_write_fw(struct wl1273_core *core,
			      __u8 *fw, int len)
{
	struct i2c_client *client = core->client;
	struct i2c_msg msg;
	int i, r = 0;

	msg.addr = client->addr;
	msg.flags = 0;

	for (i = 0; i <= len; i++) {
		msg.len = fw[0];
		msg.buf = fw + 1;

		fw += msg.len + 1;
		dev_dbg(&client->dev, "%s:len[%d]: %d\n", __func__, i, msg.len);

		r = i2c_transfer(client->adapter, &msg, 1);
		if (r < 0 && i < len + 1)
			break;
	}

	dev_dbg(&client->dev, "%s: i: %d\n", __func__, i);
	dev_dbg(&client->dev, "%s: len + 1: %d\n", __func__, len + 1);

	/* Last transfer always fails. */
	if (i == len || r == 1)
		r = 0;

	return r;
}

#define WL1273_FIFO_HAS_DATA(status)	(1 << 5 & status)
#define WL1273_RDS_CORRECTABLE_ERROR	(1 << 3)
#define WL1273_RDS_UNCORRECTABLE_ERROR	(1 << 4)

static int wl1273_fm_rds(struct wl1273_device *radio)
{
	struct wl1273_core *core = radio->core;
	struct i2c_client *client = core->client;
	u16 val;
	u8 b0 = WL1273_RDS_DATA_GET, status;
	struct v4l2_rds_data rds = { 0, 0, 0 };
	struct i2c_msg msg[] = {
		{
			.addr = client->addr,
			.flags = 0,
			.buf = &b0,
			.len = 1,
		},
		{
			.addr = client->addr,
			.flags = I2C_M_RD,
			.buf = (u8 *) &rds,
			.len = sizeof(rds),
		}
	};
	int r;

	if (core->mode != WL1273_MODE_RX)
		return 0;

	r = core->read(core, WL1273_RDS_SYNC_GET, &val);
	if (r)
		return r;

	if ((val & 0x01) == 0) {
		/* RDS decoder not synchronized */
		return -EAGAIN;
	}

	/* copy all four RDS blocks to internal buffer */
	do {
		r = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
		if (r != ARRAY_SIZE(msg)) {
			dev_err(radio->dev, WL1273_FM_DRIVER_NAME
				": %s: read_rds error r == %i)\n",
				__func__, r);
		}

		status = rds.block;

		if (!WL1273_FIFO_HAS_DATA(status))
			break;

		/* copy bits 0-2 (the block ID) to bits 3-5 */
		rds.block = V4L2_RDS_BLOCK_MSK & status;
		rds.block |= rds.block << 3;

		/* copy the error bits to standard positions */
		if (WL1273_RDS_UNCORRECTABLE_ERROR & status) {
			rds.block |= V4L2_RDS_BLOCK_ERROR;
			rds.block &= ~V4L2_RDS_BLOCK_CORRECTED;
		} else if  (WL1273_RDS_CORRECTABLE_ERROR & status) {
			rds.block &= ~V4L2_RDS_BLOCK_ERROR;
			rds.block |= V4L2_RDS_BLOCK_CORRECTED;
		}

		/* copy RDS block to internal buffer */
		memcpy(&radio->buffer[radio->wr_index], &rds, RDS_BLOCK_SIZE);
		radio->wr_index += 3;

		/* wrap write pointer */
		if (radio->wr_index >= radio->buf_size)
			radio->wr_index = 0;

		/* check for overflow & start over */
		if (radio->wr_index == radio->rd_index) {
			dev_dbg(radio->dev, "RDS OVERFLOW");

			radio->rd_index = 0;
			radio->wr_index = 0;
			break;
		}
	} while (WL1273_FIFO_HAS_DATA(status));

	/* wake up read queue */
	if (radio->wr_index != radio->rd_index)
		wake_up_interruptible(&radio->read_queue);

	return 0;
}

static irqreturn_t wl1273_fm_irq_thread_handler(int irq, void *dev_id)
{
	struct wl1273_device *radio = dev_id;
	struct wl1273_core *core = radio->core;
	u16 flags;
	int r;

	r = core->read(core, WL1273_FLAG_GET, &flags);
	if (r)
		goto out;

	if (flags & WL1273_BL_EVENT) {
		radio->irq_received = flags;
		dev_dbg(radio->dev, "IRQ: BL\n");
	}

	if (flags & WL1273_RDS_EVENT) {
		msleep(200);

		wl1273_fm_rds(radio);
	}

	if (flags & WL1273_BBLK_EVENT)
		dev_dbg(radio->dev, "IRQ: BBLK\n");

	if (flags & WL1273_LSYNC_EVENT)
		dev_dbg(radio->dev, "IRQ: LSYNC\n");

	if (flags & WL1273_LEV_EVENT) {
		u16 level;

		r = core->read(core, WL1273_RSSI_LVL_GET, &level);
		if (r)
			goto out;

		if (level > 14)
			dev_dbg(radio->dev, "IRQ: LEV: 0x%x04\n", level);
	}

	if (flags & WL1273_IFFR_EVENT)
		dev_dbg(radio->dev, "IRQ: IFFR\n");

	if (flags & WL1273_PI_EVENT)
		dev_dbg(radio->dev, "IRQ: PI\n");

	if (flags & WL1273_PD_EVENT)
		dev_dbg(radio->dev, "IRQ: PD\n");

	if (flags & WL1273_STIC_EVENT)
		dev_dbg(radio->dev, "IRQ: STIC\n");

	if (flags & WL1273_MAL_EVENT)
		dev_dbg(radio->dev, "IRQ: MAL\n");

	if (flags & WL1273_POW_ENB_EVENT) {
		complete(&radio->busy);
		dev_dbg(radio->dev, "NOT BUSY\n");
		dev_dbg(radio->dev, "IRQ: POW_ENB\n");
	}

	if (flags & WL1273_SCAN_OVER_EVENT)
		dev_dbg(radio->dev, "IRQ: SCAN_OVER\n");

	if (flags & WL1273_ERROR_EVENT)
		dev_dbg(radio->dev, "IRQ: ERROR\n");

	if (flags & WL1273_FR_EVENT) {
		u16 freq;

		dev_dbg(radio->dev, "IRQ: FR:\n");

		if (core->mode == WL1273_MODE_RX) {
			r = core->write(core, WL1273_TUNER_MODE_SET,
					TUNER_MODE_STOP_SEARCH);
			if (r) {
				dev_err(radio->dev,
					"%s: TUNER_MODE_SET fails: %d\n",
					__func__, r);
				goto out;
			}

			r = core->read(core, WL1273_FREQ_SET, &freq);
			if (r)
				goto out;

			if (radio->band == WL1273_BAND_JAPAN)
				radio->rx_frequency = WL1273_BAND_JAPAN_LOW +
					freq * 50;
			else
				radio->rx_frequency = WL1273_BAND_OTHER_LOW +
					freq * 50;
			/*
			 *  The driver works better with this msleep,
			 *  the documentation doesn't mention it.
			 */
			usleep_range(10000, 15000);

			dev_dbg(radio->dev, "%dkHz\n", radio->rx_frequency);

		} else {
			r = core->read(core, WL1273_CHANL_SET, &freq);
			if (r)
				goto out;

			dev_dbg(radio->dev, "%dkHz\n", freq);
		}
		dev_dbg(radio->dev, "%s: NOT BUSY\n", __func__);
	}

out:
	core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
	complete(&radio->busy);

	return IRQ_HANDLED;
}

static int wl1273_fm_set_tx_freq(struct wl1273_device *radio, unsigned int freq)
{
	struct wl1273_core *core = radio->core;
	int r = 0;

	if (freq < WL1273_BAND_TX_LOW) {
		dev_err(radio->dev,
			"Frequency out of range: %d < %d\n", freq,
			WL1273_BAND_TX_LOW);
		return -ERANGE;
	}

	if (freq > WL1273_BAND_TX_HIGH) {
		dev_err(radio->dev,
			"Frequency out of range: %d > %d\n", freq,
			WL1273_BAND_TX_HIGH);
		return -ERANGE;
	}

	/*
	 *  The driver works better with this sleep,
	 *  the documentation doesn't mention it.
	 */
	usleep_range(5000, 10000);

	dev_dbg(radio->dev, "%s: freq: %d kHz\n", __func__, freq);

	/* Set the current tx channel */
	r = core->write(core, WL1273_CHANL_SET, freq / 10);
	if (r)
		return r;

	reinit_completion(&radio->busy);

	/* wait for the FR IRQ */
	r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
	if (!r)
		return -ETIMEDOUT;

	dev_dbg(radio->dev, "WL1273_CHANL_SET: %d\n", r);

	/* Enable the output power */
	r = core->write(core, WL1273_POWER_ENB_SET, 1);
	if (r)
		return r;

	reinit_completion(&radio->busy);

	/* wait for the POWER_ENB IRQ */
	r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
	if (!r)
		return -ETIMEDOUT;

	radio->tx_frequency = freq;
	dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %d\n", r);

	return	0;
}

static int wl1273_fm_set_rx_freq(struct wl1273_device *radio, unsigned int freq)
{
	struct wl1273_core *core = radio->core;
	int r, f;

	if (freq < radio->rangelow) {
		dev_err(radio->dev,
			"Frequency out of range: %d < %d\n", freq,
			radio->rangelow);
		r = -ERANGE;
		goto err;
	}

	if (freq > radio->rangehigh) {
		dev_err(radio->dev,
			"Frequency out of range: %d > %d\n", freq,
			radio->rangehigh);
		r = -ERANGE;
		goto err;
	}

	dev_dbg(radio->dev, "%s: %dkHz\n", __func__, freq);

	core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);

	if (radio->band == WL1273_BAND_JAPAN)
		f = (freq - WL1273_BAND_JAPAN_LOW) / 50;
	else
		f = (freq - WL1273_BAND_OTHER_LOW) / 50;

	r = core->write(core, WL1273_FREQ_SET, f);
	if (r) {
		dev_err(radio->dev, "FREQ_SET fails\n");
		goto err;
	}

	r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET);
	if (r) {
		dev_err(radio->dev, "TUNER_MODE_SET fails\n");
		goto err;
	}

	reinit_completion(&radio->busy);

	r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
	if (!r) {
		dev_err(radio->dev, "%s: TIMEOUT\n", __func__);
		return -ETIMEDOUT;
	}

	radio->rd_index = 0;
	radio->wr_index = 0;
	radio->rx_frequency = freq;
	return 0;
err:
	return r;
}<commit_msg>[media] media: radio: assign wait_for_completion_timeout to appropriately typed var<commit_after>/*
 * Driver for the Texas Instruments WL1273 FM radio.
 *
 * Copyright (C) 2011 Nokia Corporation
 * Author: <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/interrupt.h>
#include <linux/mfd/wl1273-core.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>

#define DRIVER_DESC "Wl1273 FM Radio"

#define WL1273_POWER_SET_OFF		0
#define WL1273_POWER_SET_FM		BIT(0)
#define WL1273_POWER_SET_RDS		BIT(1)
#define WL1273_POWER_SET_RETENTION	BIT(4)

#define WL1273_PUPD_SET_OFF		0x00
#define WL1273_PUPD_SET_ON		0x01
#define WL1273_PUPD_SET_RETENTION	0x10

#define WL1273_FREQ(x)		(x * 10000 / 625)
#define WL1273_INV_FREQ(x)	(x * 625 / 10000)

/*
 * static int radio_nr - The number of the radio device
 *
 * The default is 0.
 */
static int radio_nr;
module_param(radio_nr, int, 0);
MODULE_PARM_DESC(radio_nr, "The number of the radio device. Default = 0");

struct wl1273_device {
	char *bus_type;

	u8 forbidden;
	unsigned int preemphasis;
	unsigned int spacing;
	unsigned int tx_power;
	unsigned int rx_frequency;
	unsigned int tx_frequency;
	unsigned int rangelow;
	unsigned int rangehigh;
	unsigned int band;
	bool stereo;

	/* RDS */
	unsigned int rds_on;

	wait_queue_head_t read_queue;
	struct mutex lock; /* for serializing fm radio operations */
	struct completion busy;

	unsigned char *buffer;
	unsigned int buf_size;
	unsigned int rd_index;
	unsigned int wr_index;

	/* Selected interrupts */
	u16 irq_flags;
	u16 irq_received;

	struct v4l2_ctrl_handler ctrl_handler;
	struct v4l2_device v4l2dev;
	struct video_device videodev;
	struct device *dev;
	struct wl1273_core *core;
	struct file *owner;
	char *write_buf;
	unsigned int rds_users;
};

#define WL1273_IRQ_MASK	 (WL1273_FR_EVENT		|	\
			  WL1273_POW_ENB_EVENT)

/*
 * static unsigned int rds_buf - the number of RDS buffer blocks used.
 *
 * The default number is 100.
 */
static unsigned int rds_buf = 100;
module_param(rds_buf, uint, 0);
MODULE_PARM_DESC(rds_buf, "Number of RDS buffer entries. Default = 100");

static int wl1273_fm_write_fw(struct wl1273_core *core,
			      __u8 *fw, int len)
{
	struct i2c_client *client = core->client;
	struct i2c_msg msg;
	int i, r = 0;

	msg.addr = client->addr;
	msg.flags = 0;

	for (i = 0; i <= len; i++) {
		msg.len = fw[0];
		msg.buf = fw + 1;

		fw += msg.len + 1;
		dev_dbg(&client->dev, "%s:len[%d]: %d\n", __func__, i, msg.len);

		r = i2c_transfer(client->adapter, &msg, 1);
		if (r < 0 && i < len + 1)
			break;
	}

	dev_dbg(&client->dev, "%s: i: %d\n", __func__, i);
	dev_dbg(&client->dev, "%s: len + 1: %d\n", __func__, len + 1);

	/* Last transfer always fails. */
	if (i == len || r == 1)
		r = 0;

	return r;
}

#define WL1273_FIFO_HAS_DATA(status)	(1 << 5 & status)
#define WL1273_RDS_CORRECTABLE_ERROR	(1 << 3)
#define WL1273_RDS_UNCORRECTABLE_ERROR	(1 << 4)

static int wl1273_fm_rds(struct wl1273_device *radio)
{
	struct wl1273_core *core = radio->core;
	struct i2c_client *client = core->client;
	u16 val;
	u8 b0 = WL1273_RDS_DATA_GET, status;
	struct v4l2_rds_data rds = { 0, 0, 0 };
	struct i2c_msg msg[] = {
		{
			.addr = client->addr,
			.flags = 0,
			.buf = &b0,
			.len = 1,
		},
		{
			.addr = client->addr,
			.flags = I2C_M_RD,
			.buf = (u8 *) &rds,
			.len = sizeof(rds),
		}
	};
	int r;

	if (core->mode != WL1273_MODE_RX)
		return 0;

	r = core->read(core, WL1273_RDS_SYNC_GET, &val);
	if (r)
		return r;

	if ((val & 0x01) == 0) {
		/* RDS decoder not synchronized */
		return -EAGAIN;
	}

	/* copy all four RDS blocks to internal buffer */
	do {
		r = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
		if (r != ARRAY_SIZE(msg)) {
			dev_err(radio->dev, WL1273_FM_DRIVER_NAME
				": %s: read_rds error r == %i)\n",
				__func__, r);
		}

		status = rds.block;

		if (!WL1273_FIFO_HAS_DATA(status))
			break;

		/* copy bits 0-2 (the block ID) to bits 3-5 */
		rds.block = V4L2_RDS_BLOCK_MSK & status;
		rds.block |= rds.block << 3;

		/* copy the error bits to standard positions */
		if (WL1273_RDS_UNCORRECTABLE_ERROR & status) {
			rds.block |= V4L2_RDS_BLOCK_ERROR;
			rds.block &= ~V4L2_RDS_BLOCK_CORRECTED;
		} else if  (WL1273_RDS_CORRECTABLE_ERROR & status) {
			rds.block &= ~V4L2_RDS_BLOCK_ERROR;
			rds.block |= V4L2_RDS_BLOCK_CORRECTED;
		}

		/* copy RDS block to internal buffer */
		memcpy(&radio->buffer[radio->wr_index], &rds, RDS_BLOCK_SIZE);
		radio->wr_index += 3;

		/* wrap write pointer */
		if (radio->wr_index >= radio->buf_size)
			radio->wr_index = 0;

		/* check for overflow & start over */
		if (radio->wr_index == radio->rd_index) {
			dev_dbg(radio->dev, "RDS OVERFLOW");

			radio->rd_index = 0;
			radio->wr_index = 0;
			break;
		}
	} while (WL1273_FIFO_HAS_DATA(status));

	/* wake up read queue */
	if (radio->wr_index != radio->rd_index)
		wake_up_interruptible(&radio->read_queue);

	return 0;
}

static irqreturn_t wl1273_fm_irq_thread_handler(int irq, void *dev_id)
{
	struct wl1273_device *radio = dev_id;
	struct wl1273_core *core = radio->core;
	u16 flags;
	int r;

	r = core->read(core, WL1273_FLAG_GET, &flags);
	if (r)
		goto out;

	if (flags & WL1273_BL_EVENT) {
		radio->irq_received = flags;
		dev_dbg(radio->dev, "IRQ: BL\n");
	}

	if (flags & WL1273_RDS_EVENT) {
		msleep(200);

		wl1273_fm_rds(radio);
	}

	if (flags & WL1273_BBLK_EVENT)
		dev_dbg(radio->dev, "IRQ: BBLK\n");

	if (flags & WL1273_LSYNC_EVENT)
		dev_dbg(radio->dev, "IRQ: LSYNC\n");

	if (flags & WL1273_LEV_EVENT) {
		u16 level;

		r = core->read(core, WL1273_RSSI_LVL_GET, &level);
		if (r)
			goto out;

		if (level > 14)
			dev_dbg(radio->dev, "IRQ: LEV: 0x%x04\n", level);
	}

	if (flags & WL1273_IFFR_EVENT)
		dev_dbg(radio->dev, "IRQ: IFFR\n");

	if (flags & WL1273_PI_EVENT)
		dev_dbg(radio->dev, "IRQ: PI\n");

	if (flags & WL1273_PD_EVENT)
		dev_dbg(radio->dev, "IRQ: PD\n");

	if (flags & WL1273_STIC_EVENT)
		dev_dbg(radio->dev, "IRQ: STIC\n");

	if (flags & WL1273_MAL_EVENT)
		dev_dbg(radio->dev, "IRQ: MAL\n");

	if (flags & WL1273_POW_ENB_EVENT) {
		complete(&radio->busy);
		dev_dbg(radio->dev, "NOT BUSY\n");
		dev_dbg(radio->dev, "IRQ: POW_ENB\n");
	}

	if (flags & WL1273_SCAN_OVER_EVENT)
		dev_dbg(radio->dev, "IRQ: SCAN_OVER\n");

	if (flags & WL1273_ERROR_EVENT)
		dev_dbg(radio->dev, "IRQ: ERROR\n");

	if (flags & WL1273_FR_EVENT) {
		u16 freq;

		dev_dbg(radio->dev, "IRQ: FR:\n");

		if (core->mode == WL1273_MODE_RX) {
			r = core->write(core, WL1273_TUNER_MODE_SET,
					TUNER_MODE_STOP_SEARCH);
			if (r) {
				dev_err(radio->dev,
					"%s: TUNER_MODE_SET fails: %d\n",
					__func__, r);
				goto out;
			}

			r = core->read(core, WL1273_FREQ_SET, &freq);
			if (r)
				goto out;

			if (radio->band == WL1273_BAND_JAPAN)
				radio->rx_frequency = WL1273_BAND_JAPAN_LOW +
					freq * 50;
			else
				radio->rx_frequency = WL1273_BAND_OTHER_LOW +
					freq * 50;
			/*
			 *  The driver works better with this msleep,
			 *  the documentation doesn't mention it.
			 */
			usleep_range(10000, 15000);

			dev_dbg(radio->dev, "%dkHz\n", radio->rx_frequency);

		} else {
			r = core->read(core, WL1273_CHANL_SET, &freq);
			if (r)
				goto out;

			dev_dbg(radio->dev, "%dkHz\n", freq);
		}
		dev_dbg(radio->dev, "%s: NOT BUSY\n", __func__);
	}

out:
	core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
	complete(&radio->busy);

	return IRQ_HANDLED;
}

static int wl1273_fm_set_tx_freq(struct wl1273_device *radio, unsigned int freq)
{
	struct wl1273_core *core = radio->core;
	int r = 0;
	unsigned long t;

	if (freq < WL1273_BAND_TX_LOW) {
		dev_err(radio->dev,
			"Frequency out of range: %d < %d\n", freq,
			WL1273_BAND_TX_LOW);
		return -ERANGE;
	}

	if (freq > WL1273_BAND_TX_HIGH) {
		dev_err(radio->dev,
			"Frequency out of range: %d > %d\n", freq,
			WL1273_BAND_TX_HIGH);
		return -ERANGE;
	}

	/*
	 *  The driver works better with this sleep,
	 *  the documentation doesn't mention it.
	 */
	usleep_range(5000, 10000);

	dev_dbg(radio->dev, "%s: freq: %d kHz\n", __func__, freq);

	/* Set the current tx channel */
	r = core->write(core, WL1273_CHANL_SET, freq / 10);
	if (r)
		return r;

	reinit_completion(&radio->busy);

	/* wait for the FR IRQ */
	t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
	if (!t)
		return -ETIMEDOUT;

	dev_dbg(radio->dev, "WL1273_CHANL_SET: %lu\n", t);

	/* Enable the output power */
	r = core->write(core, WL1273_POWER_ENB_SET, 1);
	if (r)
		return r;

	reinit_completion(&radio->busy);

	/* wait for the POWER_ENB IRQ */
	t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
	if (!t)
		return -ETIMEDOUT;

	radio->tx_frequency = freq;
	dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %lu\n", t);

	return	0;
}

static int wl1273_fm_set_rx_freq(struct wl1273_device *radio, unsigned int freq)
{
	struct wl1273_core *core = radio->core;
	int r, f;
	unsigned long t;

	if (freq < radio->rangelow) {
		dev_err(radio->dev,
			"Frequency out of range: %d < %d\n", freq,
			radio->rangelow);
		r = -ERANGE;
		goto err;
	}

	if (freq > radio->rangehigh) {
		dev_err(radio->dev,
			"Frequency out of range: %d > %d\n", freq,
			radio->rangehigh);
		r = -ERANGE;
		goto err;
	}

	dev_dbg(radio->dev, "%s: %dkHz\n", __func__, freq);

	core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);

	if (radio->band == WL1273_BAND_JAPAN)
		f = (freq - WL1273_BAND_JAPAN_LOW) / 50;
	else
		f = (freq - WL1273_BAND_OTHER_LOW) / 50;

	r = core->write(core, WL1273_FREQ_SET, f);
	if (r) {
		dev_err(radio->dev, "FREQ_SET fails\n");
		goto err;
	}

	r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET);
	if (r) {
		dev_err(radio->dev, "TUNER_MODE_SET fails\n");
		goto err;
	}

	reinit_completion(&radio->busy);

	t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
	if (!t) {
		dev_err(radio->dev, "%s: TIMEOUT\n", __func__);
		return -ETIMEDOUT;
	}

	radio->rd_index = 0;
	radio->wr_index = 0;
	radio->rx_frequency = freq;
	return 0;
err:
	return r;
}
{'fraction_non_alphanumeric': 0.14013630846793015, 'fraction_numerical': 0.0494818410979367, 'mean_word_length': 3.7511643379906854, 'pattern_counts': {'":': 2, '<': 59, '<?xml version=': 0, '>': 233, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5305297', 'n_tokens_mistral': 10245, 'n_tokens_neox': 8874, 'n_words': 2132}