radio-wl1273.c 50.5 KB
Newer Older
1 2 3
/*
 * Driver for the Texas Instruments WL1273 FM radio.
 *
4
 * Copyright (C) 2011 Nokia Corporation
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Author: Matti J. Aaltonen <matti.j.aaltonen@nokia.com>
 *
 * 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.
 */

#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/interrupt.h>
#include <linux/mfd/wl1273-core.h>
#include <linux/slab.h>
22
#include <linux/module.h>
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
#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;

165
	r = core->read(core, WL1273_RDS_SYNC_GET, &val);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	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;

233
	r = core->read(core, WL1273_FLAG_GET, &flags);
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	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;

257
		r = core->read(core, WL1273_RSSI_LVL_GET, &level);
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
		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) {
298 299
			r = core->write(core, WL1273_TUNER_MODE_SET,
					TUNER_MODE_STOP_SEARCH);
300 301 302 303 304 305 306
			if (r) {
				dev_err(radio->dev,
					"%s: TUNER_MODE_SET fails: %d\n",
					__func__, r);
				goto out;
			}

307
			r = core->read(core, WL1273_FREQ_SET, &freq);
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
			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 {
326
			r = core->read(core, WL1273_CHANL_SET, &freq);
327 328 329 330 331 332 333 334 335
			if (r)
				goto out;

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

out:
336
	core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
337 338 339 340 341 342 343 344 345
	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;
346
	unsigned long t;
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370

	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 */
371
	r = core->write(core, WL1273_CHANL_SET, freq / 10);
372 373 374
	if (r)
		return r;

375
	reinit_completion(&radio->busy);
376 377

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

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

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

389
	reinit_completion(&radio->busy);
390 391

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

	radio->tx_frequency = freq;
397
	dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %lu\n", t);
398 399 400 401 402 403 404 405

	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;
406
	unsigned long t;
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

	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);

426
	core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
427 428 429 430 431 432

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

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

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

445
	reinit_completion(&radio->busy);
446

447 448
	t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
	if (!t) {
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
		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;
}

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

	if (core->mode == WL1273_MODE_RX) {
469
		r = core->read(core, WL1273_FREQ_SET, &f);
470 471 472 473 474 475 476 477 478
		if (r)
			return r;

		dev_dbg(radio->dev, "Freq get: 0x%04x\n", f);
		if (radio->band == WL1273_BAND_JAPAN)
			freq = WL1273_BAND_JAPAN_LOW + 50 * f;
		else
			freq = WL1273_BAND_OTHER_LOW + 50 * f;
	} else {
479
		r = core->read(core, WL1273_CHANL_SET, &f);
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
		if (r)
			return r;

		freq = f * 10;
	}

	return freq;
}

/**
 * wl1273_fm_upload_firmware_patch() -	Upload the firmware.
 * @radio:				A pointer to the device struct.
 *
 * The firmware file consists of arrays of bytes where the first byte
 * gives the array length. The first byte in the file gives the
 * number of these arrays.
 */
static int wl1273_fm_upload_firmware_patch(struct wl1273_device *radio)
{
	struct wl1273_core *core = radio->core;
	unsigned int packet_num;
	const struct firmware *fw_p;
	const char *fw_name = "radio-wl1273-fw.bin";
	struct device *dev = radio->dev;
	__u8 *ptr;
505
	int r;
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529

	dev_dbg(dev, "%s:\n", __func__);

	/*
	 * Uploading the firmware patch is not always necessary,
	 * so we only print an info message.
	 */
	if (request_firmware(&fw_p, fw_name, dev)) {
		dev_info(dev, "%s - %s not found\n", __func__, fw_name);

		return 0;
	}

	ptr = (__u8 *) fw_p->data;
	packet_num = ptr[0];
	dev_dbg(dev, "%s: packets: %d\n", __func__, packet_num);

	r = wl1273_fm_write_fw(core, ptr + 1, packet_num);
	if (r) {
		dev_err(dev, "FW upload error: %d\n", r);
		goto out;
	}

	/* ignore possible error here */
530
	core->write(core, WL1273_RESET, 0);
531 532 533 534 535 536 537 538 539 540 541 542

	dev_dbg(dev, "%s - download OK, r: %d\n", __func__, r);
out:
	release_firmware(fw_p);
	return r;
}

static int wl1273_fm_stop(struct wl1273_device *radio)
{
	struct wl1273_core *core = radio->core;

	if (core->mode == WL1273_MODE_RX) {
543
		int r = core->write(core, WL1273_POWER_SET,
544 545 546 547 548
				    WL1273_POWER_SET_OFF);
		if (r)
			dev_err(radio->dev, "%s: POWER_SET fails: %d\n",
				__func__, r);
	} else if (core->mode == WL1273_MODE_TX) {
549 550
		int r = core->write(core, WL1273_PUPD_SET,
				    WL1273_PUPD_SET_OFF);
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
		if (r)
			dev_err(radio->dev,
				"%s: PUPD_SET fails: %d\n", __func__, r);
	}

	if (core->pdata->disable) {
		core->pdata->disable();
		dev_dbg(radio->dev, "Back to reset\n");
	}

	return 0;
}

static int wl1273_fm_start(struct wl1273_device *radio, int new_mode)
{
	struct wl1273_core *core = radio->core;
	struct wl1273_fm_platform_data *pdata = core->pdata;
	struct device *dev = radio->dev;
	int r = -EINVAL;

	if (pdata->enable && core->mode == WL1273_MODE_OFF) {
		dev_dbg(radio->dev, "Out of reset\n");

		pdata->enable();
		msleep(250);
	}

	if (new_mode == WL1273_MODE_RX) {
		u16 val = WL1273_POWER_SET_FM;

		if (radio->rds_on)
			val |= WL1273_POWER_SET_RDS;

		/* If this fails try again */
585
		r = core->write(core, WL1273_POWER_SET, val);
586 587 588
		if (r) {
			msleep(100);

589
			r = core->write(core, WL1273_POWER_SET, val);
590 591 592 593 594 595 596 597 598 599 600 601
			if (r) {
				dev_err(dev, "%s: POWER_SET fails\n", __func__);
				goto fail;
			}
		}

		/* rds buffer configuration */
		radio->wr_index = 0;
		radio->rd_index = 0;

	} else if (new_mode == WL1273_MODE_TX) {
		/* If this fails try again once */
602
		r = core->write(core, WL1273_PUPD_SET, WL1273_PUPD_SET_ON);
603 604
		if (r) {
			msleep(100);
605
			r = core->write(core, WL1273_PUPD_SET,
606 607 608 609 610 611 612
					WL1273_PUPD_SET_ON);
			if (r) {
				dev_err(dev, "%s: PUPD_SET fails\n", __func__);
				goto fail;
			}
		}

613
		if (radio->rds_on) {
614
			r = core->write(core, WL1273_RDS_DATA_ENB, 1);
615 616 617 618 619 620
			if (r) {
				dev_err(dev, "%s: RDS_DATA_ENB ON fails\n",
					__func__);
				goto fail;
			}
		} else {
621
			r = core->write(core, WL1273_RDS_DATA_ENB, 0);
622 623 624 625 626 627
			if (r) {
				dev_err(dev, "%s: RDS_DATA_ENB OFF fails\n",
					__func__);
				goto fail;
			}
		}
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
	} else {
		dev_warn(dev, "%s: Illegal mode.\n", __func__);
	}

	if (core->mode == WL1273_MODE_OFF) {
		r = wl1273_fm_upload_firmware_patch(radio);
		if (r)
			dev_warn(dev, "Firmware upload failed.\n");

		/*
		 * Sometimes the chip is in a wrong power state at this point.
		 * So we set the power once again.
		 */
		if (new_mode == WL1273_MODE_RX) {
			u16 val = WL1273_POWER_SET_FM;

			if (radio->rds_on)
				val |= WL1273_POWER_SET_RDS;

647
			r = core->write(core, WL1273_POWER_SET, val);
648 649 650 651 652
			if (r) {
				dev_err(dev, "%s: POWER_SET fails\n", __func__);
				goto fail;
			}
		} else if (new_mode == WL1273_MODE_TX) {
653 654
			r = core->write(core, WL1273_PUPD_SET,
					WL1273_PUPD_SET_ON);
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
			if (r) {
				dev_err(dev, "%s: PUPD_SET fails\n", __func__);
				goto fail;
			}
		}
	}

	return 0;
fail:
	if (pdata->disable)
		pdata->disable();

	dev_dbg(dev, "%s: return: %d\n", __func__, r);
	return r;
}

static int wl1273_fm_suspend(struct wl1273_device *radio)
{
	struct wl1273_core *core = radio->core;
674
	int r;
675 676 677

	/* Cannot go from OFF to SUSPENDED */
	if (core->mode == WL1273_MODE_RX)
678
		r = core->write(core, WL1273_POWER_SET,
679 680
				WL1273_POWER_SET_RETENTION);
	else if (core->mode == WL1273_MODE_TX)
681
		r = core->write(core, WL1273_PUPD_SET,
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
				WL1273_PUPD_SET_RETENTION);
	else
		r = -EINVAL;

	if (r) {
		dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r);
		goto out;
	}

out:
	return r;
}

static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode)
{
	struct wl1273_core *core = radio->core;
	struct device *dev = radio->dev;
	int old_mode;
	int r;

	dev_dbg(dev, "%s\n", __func__);
	dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden);

	old_mode = core->mode;
	if (mode & radio->forbidden) {
		r = -EPERM;
		goto out;
	}

	switch (mode) {
	case WL1273_MODE_RX:
	case WL1273_MODE_TX:
		r = wl1273_fm_start(radio, mode);
		if (r) {
			dev_err(dev, "%s: Cannot start.\n", __func__);
			wl1273_fm_stop(radio);
			goto out;
		}

		core->mode = mode;
722
		r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
		if (r) {
			dev_err(dev, "INT_MASK_SET fails.\n");
			goto out;
		}

		/* remember previous settings */
		if (mode == WL1273_MODE_RX) {
			r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
			if (r) {
				dev_err(dev, "set freq fails: %d.\n", r);
				goto out;
			}

			r = core->set_volume(core, core->volume);
			if (r) {
				dev_err(dev, "set volume fails: %d.\n", r);
				goto out;
			}

			dev_dbg(dev, "%s: Set vol: %d.\n", __func__,
				core->volume);
		} else {
			r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency);
			if (r) {
				dev_err(dev, "set freq fails: %d.\n", r);
				goto out;
			}
		}

		dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__);

		r = core->set_audio(core, core->audio_mode);
		if (r)
			dev_err(dev, "Cannot set audio mode.\n");
		break;

	case WL1273_MODE_OFF:
		r = wl1273_fm_stop(radio);
		if (r)
			dev_err(dev, "%s: Off fails: %d\n", __func__, r);
		else
			core->mode = WL1273_MODE_OFF;

		break;

	case WL1273_MODE_SUSPENDED:
		r = wl1273_fm_suspend(radio);
		if (r)
			dev_err(dev, "%s: Suspend fails: %d\n", __func__, r);
		else
			core->mode = WL1273_MODE_SUSPENDED;

		break;

	default:
		dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode);
		r = -EINVAL;
		break;
	}
out:
	if (r)
		core->mode = old_mode;

	return r;
}

static int wl1273_fm_set_seek(struct wl1273_device *radio,
			      unsigned int wrap_around,
			      unsigned int seek_upward,
			      int level)
{
	struct wl1273_core *core = radio->core;
	int r = 0;
	unsigned int dir = (seek_upward == 0) ? 0 : 1;
	unsigned int f;

	f = radio->rx_frequency;
	dev_dbg(radio->dev, "rx_frequency: %d\n", f);

	if (dir && f + radio->spacing <= radio->rangehigh)
		r = wl1273_fm_set_rx_freq(radio, f + radio->spacing);
	else if (dir && wrap_around)
		r = wl1273_fm_set_rx_freq(radio, radio->rangelow);
	else if (f - radio->spacing >= radio->rangelow)
		r = wl1273_fm_set_rx_freq(radio, f - radio->spacing);
	else if (wrap_around)
		r = wl1273_fm_set_rx_freq(radio, radio->rangehigh);

	if (r)
		goto out;

	if (level < SCHAR_MIN || level > SCHAR_MAX)
		return -EINVAL;

817
	reinit_completion(&radio->busy);
818 819
	dev_dbg(radio->dev, "%s: BUSY\n", __func__);

820
	r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
821 822 823 824 825
	if (r)
		goto out;

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

826
	r = core->write(core, WL1273_SEARCH_LVL_SET, level);
827 828 829
	if (r)
		goto out;

830
	r = core->write(core, WL1273_SEARCH_DIR_SET, dir);
831 832 833
	if (r)
		goto out;

834
	r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
835 836 837
	if (r)
		goto out;

838
	/* wait for the FR IRQ */
839
	wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
840 841
	if (!(radio->irq_received & WL1273_BL_EVENT)) {
		r = -ETIMEDOUT;
842
		goto out;
843
	}
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861

	radio->irq_received &= ~WL1273_BL_EVENT;

	if (!wrap_around)
		goto out;

	/* Wrap around */
	dev_dbg(radio->dev, "Wrap around in HW seek.\n");

	if (seek_upward)
		f = radio->rangelow;
	else
		f = radio->rangehigh;

	r = wl1273_fm_set_rx_freq(radio, f);
	if (r)
		goto out;

862
	reinit_completion(&radio->busy);
863 864
	dev_dbg(radio->dev, "%s: BUSY\n", __func__);

865
	r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
866 867 868
	if (r)
		goto out;

869 870 871
	/* wait for the FR IRQ */
	if (!wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000)))
		r = -ETIMEDOUT;
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
out:
	dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r);
	return r;
}

/**
 * wl1273_fm_get_tx_ctune() -	Get the TX tuning capacitor value.
 * @radio:			A pointer to the device struct.
 */
static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio)
{
	struct wl1273_core *core = radio->core;
	struct device *dev = radio->dev;
	u16 val;
	int r;

	if (core->mode == WL1273_MODE_OFF ||
	    core->mode == WL1273_MODE_SUSPENDED)
		return -EPERM;

892
	r = core->read(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
	if (r) {
		dev_err(dev, "%s: read error: %d\n", __func__, r);
		goto out;
	}

out:
	return val;
}

/**
 * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
 * @radio:			 A pointer to the device struct.
 * @preemphasis:		 The new pre-amphasis value.
 *
 * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
 * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
 */
static int wl1273_fm_set_preemphasis(struct wl1273_device *radio,
				     unsigned int preemphasis)
{
	struct wl1273_core *core = radio->core;
	int r;
	u16 em;

	if (core->mode == WL1273_MODE_OFF ||
	    core->mode == WL1273_MODE_SUSPENDED)
		return -EPERM;

	mutex_lock(&core->lock);

	switch (preemphasis) {
	case V4L2_PREEMPHASIS_DISABLED:
		em = 1;
		break;
	case V4L2_PREEMPHASIS_50_uS:
		em = 0;
		break;
	case V4L2_PREEMPHASIS_75_uS:
		em = 2;
		break;
	default:
		r = -EINVAL;
		goto out;
	}

938
	r = core->write(core, WL1273_PREMPH_SET, em);
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
	if (r)
		goto out;

	radio->preemphasis = preemphasis;

out:
	mutex_unlock(&core->lock);
	return r;
}

static int wl1273_fm_rds_on(struct wl1273_device *radio)
{
	struct wl1273_core *core = radio->core;
	int r;

	dev_dbg(radio->dev, "%s\n", __func__);
	if (radio->rds_on)
		return 0;

958
	r = core->write(core, WL1273_POWER_SET,
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
			WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
	if (r)
		goto out;

	r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
	if (r)
		dev_err(radio->dev, "set freq fails: %d.\n", r);
out:
	return r;
}

static int wl1273_fm_rds_off(struct wl1273_device *radio)
{
	struct wl1273_core *core = radio->core;
	int r;

	if (!radio->rds_on)
		return 0;

	radio->irq_flags &= ~WL1273_RDS_EVENT;

980
	r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
981 982 983 984 985 986 987 988
	if (r)
		goto out;

	/* Service pending read */
	wake_up_interruptible(&radio->read_queue);

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

989
	r = core->write(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	if (r)
		goto out;

	r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
	if (r)
		dev_err(radio->dev, "set freq fails: %d.\n", r);
out:
	dev_dbg(radio->dev, "%s: exiting...\n", __func__);

	return r;
}

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

	if (core->mode == WL1273_MODE_OFF ||
	    core->mode == WL1273_MODE_SUSPENDED)
		return -EPERM;

	if (new_mode == WL1273_RDS_RESET) {
1012
		r = core->write(core, WL1273_RDS_CNTRL_SET, 1);
1013 1014 1015 1016
		return r;
	}

	if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) {
1017
		r = core->write(core, WL1273_RDS_DATA_ENB, 0);
1018
	} else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) {
1019
		r = core->write(core, WL1273_RDS_DATA_ENB, 1);
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
	} else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) {
		r = wl1273_fm_rds_off(radio);
	} else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) {
		r = wl1273_fm_rds_on(radio);
	} else {
		dev_err(radio->dev, "%s: Unknown mode: %d\n",
			__func__, new_mode);
		r = -EINVAL;
	}

	if (!r)
		radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false;

	return r;
}

static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf,
				    size_t count, loff_t *ppos)
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1040
	struct wl1273_core *core = radio->core;
1041 1042 1043 1044 1045
	u16 val;
	int r;

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

1046
	if (core->mode != WL1273_MODE_TX)
1047 1048 1049 1050 1051 1052 1053
		return count;

	if (radio->rds_users == 0) {
		dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
		return 0;
	}

1054
	if (mutex_lock_interruptible(&core->lock))
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
		return -EINTR;
	/*
	 * Multiple processes can open the device, but only
	 * one gets to write to it.
	 */
	if (radio->owner && radio->owner != file) {
		r = -EBUSY;
		goto out;
	}
	radio->owner = file;

	/* Manual Mode */
	if (count > 255)
		val = 255;
	else
		val = count;

1072
	core->write(core, WL1273_RDS_CONFIG_DATA_SET, val);
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082

	if (copy_from_user(radio->write_buf + 1, buf, val)) {
		r = -EFAULT;
		goto out;
	}

	dev_dbg(radio->dev, "Count: %d\n", val);
	dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf);

	radio->write_buf[0] = WL1273_RDS_DATA_SET;
1083
	core->write_data(core, radio->write_buf, val + 1);
1084 1085 1086

	r = val;
out:
1087
	mutex_unlock(&core->lock);
1088 1089 1090 1091

	return r;
}

A
Al Viro 已提交
1092
static __poll_t wl1273_fm_fops_poll(struct file *file,
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
					struct poll_table_struct *pts)
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
	struct wl1273_core *core = radio->core;

	if (radio->owner && radio->owner != file)
		return -EBUSY;

	radio->owner = file;

	if (core->mode == WL1273_MODE_RX) {
		poll_wait(file, &radio->read_queue, pts);

		if (radio->rd_index != radio->wr_index)
1107
			return EPOLLIN | EPOLLRDNORM;
1108 1109

	} else if (core->mode == WL1273_MODE_TX) {
1110
		return EPOLLOUT | EPOLLWRNORM;
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
	}

	return 0;
}

static int wl1273_fm_fops_open(struct file *file)
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
	struct wl1273_core *core = radio->core;
	int r = 0;

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

	if (core->mode == WL1273_MODE_RX && radio->rds_on &&
	    !radio->rds_users) {
		dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode);

		if (mutex_lock_interruptible(&core->lock))
			return -EINTR;

		radio->irq_flags |= WL1273_RDS_EVENT;

1133 1134
		r = core->write(core, WL1273_INT_MASK_SET,
				radio->irq_flags);
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
		if (r) {
			mutex_unlock(&core->lock);
			goto out;
		}

		radio->rds_users++;

		mutex_unlock(&core->lock);
	}
out:
	return r;
}

static int wl1273_fm_fops_release(struct file *file)
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
	struct wl1273_core *core = radio->core;
	int r = 0;

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

	if (radio->rds_users > 0) {
		radio->rds_users--;
		if (radio->rds_users == 0) {
			if (mutex_lock_interruptible(&core->lock))
				return -EINTR;

			radio->irq_flags &= ~WL1273_RDS_EVENT;

			if (core->mode == WL1273_MODE_RX) {
1165 1166 1167
				r = core->write(core,
						WL1273_INT_MASK_SET,
						radio->irq_flags);
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
				if (r) {
					mutex_unlock(&core->lock);
					goto out;
				}
			}
			mutex_unlock(&core->lock);
		}
	}

	if (file == radio->owner)
		radio->owner = NULL;
out:
	return r;
}

static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
				   size_t count, loff_t *ppos)
{
	int r = 0;
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
	struct wl1273_core *core = radio->core;
	unsigned int block_count = 0;
	u16 val;

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

1194
	if (core->mode != WL1273_MODE_RX)
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
		return 0;

	if (radio->rds_users == 0) {
		dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
		return 0;
	}

	if (mutex_lock_interruptible(&core->lock))
		return -EINTR;

	/*
	 * Multiple processes can open the device, but only
	 * one at a time gets read access.
	 */
	if (radio->owner && radio->owner != file) {
		r = -EBUSY;
		goto out;
	}
	radio->owner = file;

1215
	r = core->read(core, WL1273_RDS_SYNC_GET, &val);
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
	if (r) {
		dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__);
		goto out;
	} else if (val == 0) {
		dev_info(radio->dev, "RDS_SYNC: Not synchronized\n");
		r = -ENODATA;
		goto out;
	}

	/* block if no new data available */
	while (radio->wr_index == radio->rd_index) {
		if (file->f_flags & O_NONBLOCK) {
			r = -EWOULDBLOCK;
			goto out;
		}

		dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__);
		if (wait_event_interruptible(radio->read_queue,
					     radio->wr_index !=
					     radio->rd_index) < 0) {
			r = -EINTR;
			goto out;
		}
	}

	/* calculate block count from byte count */
	count /= RDS_BLOCK_SIZE;

	/* copy RDS blocks from the internal buffer and to user buffer */
	while (block_count < count) {
		if (radio->rd_index == radio->wr_index)
			break;

		/* always transfer complete RDS blocks */
		if (copy_to_user(buf, &radio->buffer[radio->rd_index],
				 RDS_BLOCK_SIZE))
			break;

		/* increment and wrap the read pointer */
		radio->rd_index += RDS_BLOCK_SIZE;
		if (radio->rd_index >= radio->buf_size)
			radio->rd_index = 0;

		/* increment counters */
		block_count++;
		buf += RDS_BLOCK_SIZE;
		r += RDS_BLOCK_SIZE;
	}

out:
	dev_dbg(radio->dev, "%s: exit\n", __func__);
	mutex_unlock(&core->lock);

	return r;
}

static const struct v4l2_file_operations wl1273_fops = {
	.owner		= THIS_MODULE,
	.read		= wl1273_fm_fops_read,
	.write		= wl1273_fm_fops_write,
	.poll		= wl1273_fm_fops_poll,
1277
	.unlocked_ioctl	= video_ioctl2,
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
	.open		= wl1273_fm_fops_open,
	.release	= wl1273_fm_fops_release,
};

static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
				     struct v4l2_capability *capability)
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));

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

	strlcpy(capability->driver, WL1273_FM_DRIVER_NAME,
		sizeof(capability->driver));
	strlcpy(capability->card, "Texas Instruments Wl1273 FM Radio",
		sizeof(capability->card));
	strlcpy(capability->bus_info, radio->bus_type,
		sizeof(capability->bus_info));

H
Hans Verkuil 已提交
1296
	capability->device_caps = V4L2_CAP_HW_FREQ_SEEK |
1297 1298 1299
		V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
		V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
		V4L2_CAP_RDS_OUTPUT;
H
Hans Verkuil 已提交
1300 1301
	capability->capabilities = capability->device_caps |
		V4L2_CAP_DEVICE_CAPS;
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332

	return 0;
}

static int wl1273_fm_vidioc_g_input(struct file *file, void *priv,
				    unsigned int *i)
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));

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

	*i = 0;

	return 0;
}

static int wl1273_fm_vidioc_s_input(struct file *file, void *priv,
				    unsigned int i)
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));

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

	if (i != 0)
		return -EINVAL;

	return 0;
}

/**
 * wl1273_fm_set_tx_power() -	Set the transmission power value.
1333
 * @radio:			A pointer to the device struct.
1334 1335 1336 1337
 * @power:			The new power value.
 */
static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power)
{
1338
	struct wl1273_core *core = radio->core;
1339 1340
	int r;

1341 1342
	if (core->mode == WL1273_MODE_OFF ||
	    core->mode == WL1273_MODE_SUSPENDED)
1343 1344
		return -EPERM;

1345
	mutex_lock(&core->lock);
1346 1347

	/* Convert the dBuV value to chip presentation */
1348
	r = core->write(core, WL1273_POWER_LEV_SET, 122 - power);
1349 1350 1351 1352 1353 1354
	if (r)
		goto out;

	radio->tx_power = power;

out:
1355
	mutex_unlock(&core->lock);
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
	return r;
}

#define WL1273_SPACING_50kHz	1
#define WL1273_SPACING_100kHz	2
#define WL1273_SPACING_200kHz	4

static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio,
				    unsigned int spacing)
{
1366
	struct wl1273_core *core = radio->core;
1367 1368 1369
	int r;

	if (spacing == 0) {
1370 1371
		r = core->write(core, WL1273_SCAN_SPACING_SET,
				WL1273_SPACING_100kHz);
1372 1373
		radio->spacing = 100;
	} else if (spacing - 50000 < 25000) {
1374 1375
		r = core->write(core, WL1273_SCAN_SPACING_SET,
				WL1273_SPACING_50kHz);
1376 1377
		radio->spacing = 50;
	} else if (spacing - 100000 < 50000) {
1378 1379
		r = core->write(core, WL1273_SCAN_SPACING_SET,
				WL1273_SPACING_100kHz);
1380 1381
		radio->spacing = 100;
	} else {
1382 1383
		r = core->write(core, WL1273_SCAN_SPACING_SET,
				WL1273_SPACING_200kHz);
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
		radio->spacing = 200;
	}

	return r;
}

static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
{
	struct wl1273_device *radio = ctrl->priv;
	struct wl1273_core *core = radio->core;

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

	if (mutex_lock_interruptible(&core->lock))
		return -EINTR;

	switch (ctrl->id) {
	case  V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1402
		ctrl->val = wl1273_fm_get_tx_ctune(radio);
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
		break;

	default:
		dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
			 __func__, ctrl->id);
		break;
	}

	mutex_unlock(&core->lock);

	return 0;
}

#define WL1273_MUTE_SOFT_ENABLE    (1 << 0)
#define WL1273_MUTE_AC             (1 << 1)
#define WL1273_MUTE_HARD_LEFT      (1 << 2)
#define WL1273_MUTE_HARD_RIGHT     (1 << 3)
#define WL1273_MUTE_SOFT_FORCE     (1 << 4)

static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl)
{
	return container_of(ctrl->handler, struct wl1273_device, ctrl_handler);
}

static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl)
{
	struct wl1273_device *radio = to_radio(ctrl);
	struct wl1273_core *core = radio->core;
	int r = 0;

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

	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
		if (mutex_lock_interruptible(&core->lock))
			return -EINTR;

		if (core->mode == WL1273_MODE_RX && ctrl->val)
1441 1442 1443 1444
			r = core->write(core,
					WL1273_MUTE_STATUS_SET,
					WL1273_MUTE_HARD_LEFT |
					WL1273_MUTE_HARD_RIGHT);
1445
		else if (core->mode == WL1273_MODE_RX)
1446 1447
			r = core->write(core,
					WL1273_MUTE_STATUS_SET, 0x0);
1448
		else if (core->mode == WL1273_MODE_TX && ctrl->val)
1449
			r = core->write(core, WL1273_MUTE, 1);
1450
		else if (core->mode == WL1273_MODE_TX)
1451
			r = core->write(core, WL1273_MUTE, 0);
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497

		mutex_unlock(&core->lock);
		break;

	case V4L2_CID_AUDIO_VOLUME:
		if (ctrl->val == 0)
			r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF);
		else
			r =  core->set_volume(core, core->volume);
		break;

	case V4L2_CID_TUNE_PREEMPHASIS:
		r = wl1273_fm_set_preemphasis(radio, ctrl->val);
		break;

	case V4L2_CID_TUNE_POWER_LEVEL:
		r = wl1273_fm_set_tx_power(radio, ctrl->val);
		break;

	default:
		dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
			 __func__, ctrl->id);
		break;
	}

	dev_dbg(radio->dev, "%s\n", __func__);
	return r;
}

static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv,
				    struct v4l2_audio *audio)
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));

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

	if (audio->index > 1)
		return -EINVAL;

	strlcpy(audio->name, "Radio", sizeof(audio->name));
	audio->capability = V4L2_AUDCAP_STEREO;

	return 0;
}

static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
1498
				    const struct v4l2_audio *audio)
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));

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

	if (audio->index != 0)
		return -EINVAL;

	return 0;
}

#define WL1273_RDS_NOT_SYNCHRONIZED 0
#define WL1273_RDS_SYNCHRONIZED 1

static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv,
				    struct v4l2_tuner *tuner)
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
	struct wl1273_core *core = radio->core;
	u16 val;
	int r;

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

	if (tuner->index > 0)
		return -EINVAL;

	strlcpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name));
	tuner->type = V4L2_TUNER_RADIO;

	tuner->rangelow	= WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
	tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);

	tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
1533 1534
		V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO |
		V4L2_TUNER_CAP_HWSEEK_BOUNDED | V4L2_TUNER_CAP_HWSEEK_WRAP;
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546

	if (radio->stereo)
		tuner->audmode = V4L2_TUNER_MODE_STEREO;
	else
		tuner->audmode = V4L2_TUNER_MODE_MONO;

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

	if (mutex_lock_interruptible(&core->lock))
		return -EINTR;

1547
	r = core->read(core, WL1273_STEREO_GET, &val);
1548 1549 1550 1551 1552 1553 1554 1555
	if (r)
		goto out;

	if (val == 1)
		tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
	else
		tuner->rxsubchans = V4L2_TUNER_SUB_MONO;

1556
	r = core->read(core, WL1273_RSSI_LVL_GET, &val);
1557 1558 1559 1560 1561 1562 1563 1564
	if (r)
		goto out;

	tuner->signal = (s16) val;
	dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);

	tuner->afc = 0;

1565
	r = core->read(core, WL1273_RDS_SYNC_GET, &val);
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
	if (r)
		goto out;

	if (val == WL1273_RDS_SYNCHRONIZED)
		tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
out:
	mutex_unlock(&core->lock);

	return r;
}

static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
1578
				    const struct v4l2_tuner *tuner)
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
	struct wl1273_core *core = radio->core;
	int r = 0;

	dev_dbg(radio->dev, "%s\n", __func__);
	dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index);
	dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name);
	dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability);
	dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans);
	dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow);
	dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh);

	if (tuner->index > 0)
		return -EINVAL;

	if (mutex_lock_interruptible(&core->lock))
		return -EINTR;

	r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
	if (r)
		goto out;

	if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS)
		r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
	else
		r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);

	if (r)
		dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r);

	if (tuner->audmode == V4L2_TUNER_MODE_MONO) {
1611
		r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
1612 1613 1614 1615 1616 1617 1618
		if (r < 0) {
			dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
				 __func__, r);
			goto out;
		}
		radio->stereo = false;
	} else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) {
1619
		r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
		if (r < 0) {
			dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
				 __func__, r);
			goto out;
		}
		radio->stereo = true;
	} else {
		dev_err(radio->dev, "%s: tuner->audmode: %d\n",
			 __func__, tuner->audmode);
		r = -EINVAL;
		goto out;
	}

out:
	mutex_unlock(&core->lock);

	return r;
}

static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
					struct v4l2_frequency *freq)
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
	struct wl1273_core *core = radio->core;

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

	if (mutex_lock_interruptible(&core->lock))
		return -EINTR;

	freq->type = V4L2_TUNER_RADIO;
	freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio));

	mutex_unlock(&core->lock);

	return 0;
}

static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
1659
					const struct v4l2_frequency *freq)
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
	struct wl1273_core *core = radio->core;
	int r;

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

	if (freq->type != V4L2_TUNER_RADIO) {
		dev_dbg(radio->dev,
			"freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
		return -EINVAL;
	}

	if (mutex_lock_interruptible(&core->lock))
		return -EINTR;

	if (core->mode == WL1273_MODE_RX) {
		dev_dbg(radio->dev, "freq: %d\n", freq->frequency);

		r = wl1273_fm_set_rx_freq(radio,
					  WL1273_INV_FREQ(freq->frequency));
		if (r)
			dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
				 ": set frequency failed with %d\n", r);
	} else {
		r = wl1273_fm_set_tx_freq(radio,
					  WL1273_INV_FREQ(freq->frequency));
		if (r)
			dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
				 ": set frequency failed with %d\n", r);
	}

	mutex_unlock(&core->lock);

	dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
	return r;
}

#define WL1273_DEFAULT_SEEK_LEVEL	7

static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1701
					   const struct v4l2_hw_freq_seek *seek)
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
	struct wl1273_core *core = radio->core;
	int r;

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

	if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
		return -EINVAL;

1712 1713 1714
	if (file->f_flags & O_NONBLOCK)
		return -EWOULDBLOCK;

1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
	if (mutex_lock_interruptible(&core->lock))
		return -EINTR;

	r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
	if (r)
		goto out;

	r = wl1273_fm_tx_set_spacing(radio, seek->spacing);
	if (r)
		dev_warn(radio->dev, "HW seek failed: %d\n", r);

	r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward,
			       WL1273_DEFAULT_SEEK_LEVEL);
	if (r)
		dev_warn(radio->dev, "HW seek failed: %d\n", r);

out:
	mutex_unlock(&core->lock);
	return r;
}

static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
1737
					const struct v4l2_modulator *modulator)
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
	struct wl1273_core *core = radio->core;
	int r = 0;

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

	if (modulator->index > 0)
		return -EINVAL;

	if (mutex_lock_interruptible(&core->lock))
		return -EINTR;

	r = wl1273_fm_set_mode(radio, WL1273_MODE_TX);
	if (r)
		goto out;

	if (modulator->txsubchans & V4L2_TUNER_SUB_RDS)
		r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
	else
		r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);

	if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
1761
		r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
1762
	else
1763 1764
		r = core->write(core, WL1273_MONO_SET,
				WL1273_RX_STEREO);
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
	if (r < 0)
		dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
			 "MONO_SET fails: %d\n", r);
out:
	mutex_unlock(&core->lock);

	return r;
}

static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
					struct v4l2_modulator *modulator)
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
	struct wl1273_core *core = radio->core;
	u16 val;
	int r;

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

	strlcpy(modulator->name, WL1273_FM_DRIVER_NAME,
		sizeof(modulator->name));

	modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
	modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);

	modulator->capability =  V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
		V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;

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

	if (mutex_lock_interruptible(&core->lock))
		return -EINTR;

1799
	r = core->read(core, WL1273_MONO_SET, &val);
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
	if (r)
		goto out;

	if (val == WL1273_TX_STEREO)
		modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
	else
		modulator->txsubchans = V4L2_TUNER_SUB_MONO;

	if (radio->rds_on)
		modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
out:
	mutex_unlock(&core->lock);

	return 0;
}

static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
{
	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
	struct wl1273_core *core = radio->core;
	struct device *dev = radio->dev;
	u16 val;
	int r;

	dev_info(dev, DRIVER_DESC);

	if (core->mode == WL1273_MODE_OFF) {
		dev_info(dev, "Mode: Off\n");
		return 0;
	}

	if (core->mode == WL1273_MODE_SUSPENDED) {
		dev_info(dev, "Mode: Suspended\n");
		return 0;
	}

1836
	r = core->read(core, WL1273_ASIC_ID_GET, &val);
1837 1838 1839 1840 1841
	if (r)
		dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__);
	else
		dev_info(dev, "ASIC_ID: 0x%04x\n", val);

1842
	r = core->read(core, WL1273_ASIC_VER_GET, &val);
1843 1844 1845 1846 1847
	if (r)
		dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__);
	else
		dev_info(dev, "ASIC Version: 0x%04x\n", val);

1848
	r = core->read(core, WL1273_FIRM_VER_GET, &val);
1849 1850 1851 1852 1853
	if (r)
		dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__);
	else
		dev_info(dev, "FW version: %d(0x%04x)\n", val, val);

1854
	r = core->read(core, WL1273_BAND_SET, &val);
1855 1856 1857 1858 1859 1860
	if (r)
		dev_err(dev, "%s: Get BAND fails.\n", __func__);
	else
		dev_info(dev, "BAND: %d\n", val);

	if (core->mode == WL1273_MODE_TX) {
1861
		r = core->read(core, WL1273_PUPD_SET, &val);
1862 1863 1864 1865 1866
		if (r)
			dev_err(dev, "%s: Get PUPD fails.\n", __func__);
		else
			dev_info(dev, "PUPD: 0x%04x\n", val);

1867
		r = core->read(core, WL1273_CHANL_SET, &val);
1868 1869 1870 1871 1872 1873 1874
		if (r)
			dev_err(dev, "%s: Get CHANL fails.\n", __func__);
		else
			dev_info(dev, "Tx frequency: %dkHz\n", val*10);
	} else if (core->mode == WL1273_MODE_RX) {
		int bf = radio->rangelow;

1875
		r = core->read(core, WL1273_FREQ_SET, &val);
1876 1877 1878 1879 1880
		if (r)
			dev_err(dev, "%s: Get FREQ fails.\n", __func__);
		else
			dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50);

1881
		r = core->read(core, WL1273_MOST_MODE_SET, &val);
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
		if (r)
			dev_err(dev, "%s: Get MOST_MODE fails.\n",
				__func__);
		else if (val == 0)
			dev_info(dev, "MOST_MODE: Stereo according to blend\n");
		else if (val == 1)
			dev_info(dev, "MOST_MODE: Force mono output\n");
		else
			dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val);

1892
		r = core->read(core, WL1273_MOST_BLEND_SET, &val);
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
		if (r)
			dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__);
		else if (val == 0)
			dev_info(dev,
				 "MOST_BLEND: Switched blend & hysteresis.\n");
		else if (val == 1)
			dev_info(dev, "MOST_BLEND: Soft blend.\n");
		else
			dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val);

1903
		r = core->read(core, WL1273_STEREO_GET, &val);
1904 1905 1906 1907 1908 1909 1910 1911 1912
		if (r)
			dev_err(dev, "%s: Get STEREO fails.\n", __func__);
		else if (val == 0)
			dev_info(dev, "STEREO: Not detected\n");
		else if (val == 1)
			dev_info(dev, "STEREO: Detected\n");
		else
			dev_info(dev, "STEREO: Unexpected value: %d\n", val);

1913
		r = core->read(core, WL1273_RSSI_LVL_GET, &val);
1914 1915 1916 1917 1918
		if (r)
			dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__);
		else
			dev_info(dev, "RX signal strength: %d\n", (s16) val);

1919
		r = core->read(core, WL1273_POWER_SET, &val);
1920 1921 1922 1923 1924
		if (r)
			dev_err(dev, "%s: Get POWER fails.\n", __func__);
		else
			dev_info(dev, "POWER: 0x%04x\n", val);

1925
		r = core->read(core, WL1273_INT_MASK_SET, &val);
1926 1927 1928 1929 1930
		if (r)
			dev_err(dev, "%s: Get INT_MASK fails.\n", __func__);
		else
			dev_info(dev, "INT_MASK: 0x%04x\n", val);

1931
		r = core->read(core, WL1273_RDS_SYNC_GET, &val);
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
		if (r)
			dev_err(dev, "%s: Get RDS_SYNC fails.\n",
				__func__);
		else if (val == 0)
			dev_info(dev, "RDS_SYNC: Not synchronized\n");

		else if (val == 1)
			dev_info(dev, "RDS_SYNC: Synchronized\n");
		else
			dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val);

1943
		r = core->read(core, WL1273_I2S_MODE_CONFIG_SET, &val);
1944 1945 1946 1947 1948 1949
		if (r)
			dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n",
				__func__);
		else
			dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val);

1950
		r = core->read(core, WL1273_VOLUME_SET, &val);
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
		if (r)
			dev_err(dev, "%s: Get VOLUME fails.\n", __func__);
		else
			dev_info(dev, "VOLUME: 0x%04x\n", val);
	}

	return 0;
}

static void wl1273_vdev_release(struct video_device *dev)
{
}

static const struct v4l2_ctrl_ops wl1273_ctrl_ops = {
	.s_ctrl = wl1273_fm_vidioc_s_ctrl,
	.g_volatile_ctrl = wl1273_fm_g_volatile_ctrl,
};

static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
	.vidioc_querycap	= wl1273_fm_vidioc_querycap,
	.vidioc_g_input		= wl1273_fm_vidioc_g_input,
	.vidioc_s_input		= wl1273_fm_vidioc_s_input,
	.vidioc_g_audio		= wl1273_fm_vidioc_g_audio,
	.vidioc_s_audio		= wl1273_fm_vidioc_s_audio,
	.vidioc_g_tuner		= wl1273_fm_vidioc_g_tuner,
	.vidioc_s_tuner		= wl1273_fm_vidioc_s_tuner,
	.vidioc_g_frequency	= wl1273_fm_vidioc_g_frequency,
	.vidioc_s_frequency	= wl1273_fm_vidioc_s_frequency,
	.vidioc_s_hw_freq_seek	= wl1273_fm_vidioc_s_hw_freq_seek,
	.vidioc_g_modulator	= wl1273_fm_vidioc_g_modulator,
	.vidioc_s_modulator	= wl1273_fm_vidioc_s_modulator,
	.vidioc_log_status      = wl1273_fm_vidioc_log_status,
};

1985
static const struct video_device wl1273_viddev_template = {
1986 1987 1988 1989
	.fops			= &wl1273_fops,
	.ioctl_ops		= &wl1273_ioctl_ops,
	.name			= WL1273_FM_DRIVER_NAME,
	.release		= wl1273_vdev_release,
1990
	.vfl_dir		= VFL_DIR_TX,
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
};

static int wl1273_fm_radio_remove(struct platform_device *pdev)
{
	struct wl1273_device *radio = platform_get_drvdata(pdev);
	struct wl1273_core *core = radio->core;

	dev_info(&pdev->dev, "%s.\n", __func__);

	free_irq(core->client->irq, radio);
	core->pdata->free_resources();

	v4l2_ctrl_handler_free(&radio->ctrl_handler);
	video_unregister_device(&radio->videodev);
	v4l2_device_unregister(&radio->v4l2dev);

	return 0;
}

2010
static int wl1273_fm_radio_probe(struct platform_device *pdev)
2011
{
2012
	struct wl1273_core **core = pdev->dev.platform_data;
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
	struct wl1273_device *radio;
	struct v4l2_ctrl *ctrl;
	int r = 0;

	pr_debug("%s\n", __func__);

	if (!core) {
		dev_err(&pdev->dev, "No platform data.\n");
		r = -EINVAL;
		goto pdata_err;
	}

2025
	radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
2026 2027 2028 2029 2030 2031 2032
	if (!radio) {
		r = -ENOMEM;
		goto pdata_err;
	}

	/* RDS buffer allocation */
	radio->buf_size = rds_buf * RDS_BLOCK_SIZE;
2033
	radio->buffer = devm_kzalloc(&pdev->dev, radio->buf_size, GFP_KERNEL);
2034 2035 2036
	if (!radio->buffer) {
		pr_err("Cannot allocate memory for RDS buffer.\n");
		r = -ENOMEM;
2037
		goto pdata_err;
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
	}

	radio->core = *core;
	radio->irq_flags = WL1273_IRQ_MASK;
	radio->dev = &radio->core->client->dev;
	radio->rds_on = false;
	radio->core->mode = WL1273_MODE_OFF;
	radio->tx_power = 118;
	radio->core->audio_mode = WL1273_AUDIO_ANALOG;
	radio->band = WL1273_BAND_OTHER;
	radio->core->i2s_mode = WL1273_I2S_DEF_MODE;
	radio->core->channel_number = 2;
	radio->core->volume = WL1273_DEFAULT_VOLUME;
	radio->rx_frequency = WL1273_BAND_OTHER_LOW;
	radio->tx_frequency = WL1273_BAND_OTHER_HIGH;
	radio->rangelow = WL1273_BAND_OTHER_LOW;
	radio->rangehigh = WL1273_BAND_OTHER_HIGH;
	radio->stereo = true;
	radio->bus_type = "I2C";

	if (radio->core->pdata->request_resources) {
		r = radio->core->pdata->request_resources(radio->core->client);
		if (r) {
			dev_err(radio->dev, WL1273_FM_DRIVER_NAME
				": Cannot get platform data\n");
2063
			goto pdata_err;
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
		}

		dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq);

		r = request_threaded_irq(radio->core->client->irq, NULL,
					 wl1273_fm_irq_thread_handler,
					 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
					 "wl1273-fm", radio);
		if (r < 0) {
			dev_err(radio->dev, WL1273_FM_DRIVER_NAME
				": Unable to register IRQ handler: %d\n", r);
			goto err_request_irq;
		}
	} else {
2078
		dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ not configured");
2079
		r = -EINVAL;
2080
		goto pdata_err;
2081 2082 2083 2084 2085
	}

	init_completion(&radio->busy);
	init_waitqueue_head(&radio->read_queue);

2086
	radio->write_buf = devm_kzalloc(&pdev->dev, 256, GFP_KERNEL);
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
	if (!radio->write_buf) {
		r = -ENOMEM;
		goto write_buf_err;
	}

	radio->dev = &pdev->dev;
	radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
	radio->rds_users = 0;

	r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
	if (r) {
		dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
2099
		goto write_buf_err;
2100 2101 2102
	}

	/* V4L2 configuration */
2103
	radio->videodev = wl1273_viddev_template;
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128

	radio->videodev.v4l2_dev = &radio->v4l2dev;

	v4l2_ctrl_handler_init(&radio->ctrl_handler, 6);

	/* add in ascending ID order */
	v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
			  V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1,
			  WL1273_DEFAULT_VOLUME);

	v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
			  V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);

	v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops,
			       V4L2_CID_TUNE_PREEMPHASIS,
			       V4L2_PREEMPHASIS_75_uS, 0x03,
			       V4L2_PREEMPHASIS_50_uS);

	v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
			  V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118);

	ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
				 V4L2_CID_TUNE_ANTENNA_CAPACITOR,
				 0, 255, 1, 255);
	if (ctrl)
2129
		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162

	if (radio->ctrl_handler.error) {
		r = radio->ctrl_handler.error;
		dev_err(&pdev->dev, "Ctrl handler error: %d\n", r);
		goto handler_init_err;
	}

	video_set_drvdata(&radio->videodev, radio);
	platform_set_drvdata(pdev, radio);

	/* register video device */
	r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
	if (r) {
		dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
			": Could not register video device\n");
		goto handler_init_err;
	}

	return 0;

handler_init_err:
	v4l2_ctrl_handler_free(&radio->ctrl_handler);
	v4l2_device_unregister(&radio->v4l2dev);
write_buf_err:
	free_irq(radio->core->client->irq, radio);
err_request_irq:
	radio->core->pdata->free_resources();
pdata_err:
	return r;
}

static struct platform_driver wl1273_fm_radio_driver = {
	.probe		= wl1273_fm_radio_probe,
2163
	.remove		= wl1273_fm_radio_remove,
2164 2165 2166 2167 2168
	.driver		= {
		.name	= "wl1273_fm_radio",
	},
};

2169
module_platform_driver(wl1273_fm_radio_driver);
2170 2171 2172 2173

MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
2174
MODULE_ALIAS("platform:wl1273_fm_radio");