• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Driver for the Texas Instruments WL1273 FM radio.
3  *
4  * Copyright (C) 2011 Nokia Corporation
5  * Author: Matti J. Aaltonen <matti.j.aaltonen@nokia.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 #include <linux/delay.h>
22 #include <linux/firmware.h>
23 #include <linux/interrupt.h>
24 #include <linux/mfd/wl1273-core.h>
25 #include <linux/slab.h>
26 #include <linux/module.h>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-ioctl.h>
31 
32 #define DRIVER_DESC "Wl1273 FM Radio"
33 
34 #define WL1273_POWER_SET_OFF		0
35 #define WL1273_POWER_SET_FM		BIT(0)
36 #define WL1273_POWER_SET_RDS		BIT(1)
37 #define WL1273_POWER_SET_RETENTION	BIT(4)
38 
39 #define WL1273_PUPD_SET_OFF		0x00
40 #define WL1273_PUPD_SET_ON		0x01
41 #define WL1273_PUPD_SET_RETENTION	0x10
42 
43 #define WL1273_FREQ(x)		(x * 10000 / 625)
44 #define WL1273_INV_FREQ(x)	(x * 625 / 10000)
45 
46 /*
47  * static int radio_nr - The number of the radio device
48  *
49  * The default is 0.
50  */
51 static int radio_nr;
52 module_param(radio_nr, int, 0);
53 MODULE_PARM_DESC(radio_nr, "The number of the radio device. Default = 0");
54 
55 struct wl1273_device {
56 	char *bus_type;
57 
58 	u8 forbidden;
59 	unsigned int preemphasis;
60 	unsigned int spacing;
61 	unsigned int tx_power;
62 	unsigned int rx_frequency;
63 	unsigned int tx_frequency;
64 	unsigned int rangelow;
65 	unsigned int rangehigh;
66 	unsigned int band;
67 	bool stereo;
68 
69 	/* RDS */
70 	unsigned int rds_on;
71 
72 	wait_queue_head_t read_queue;
73 	struct mutex lock; /* for serializing fm radio operations */
74 	struct completion busy;
75 
76 	unsigned char *buffer;
77 	unsigned int buf_size;
78 	unsigned int rd_index;
79 	unsigned int wr_index;
80 
81 	/* Selected interrupts */
82 	u16 irq_flags;
83 	u16 irq_received;
84 
85 	struct v4l2_ctrl_handler ctrl_handler;
86 	struct v4l2_device v4l2dev;
87 	struct video_device videodev;
88 	struct device *dev;
89 	struct wl1273_core *core;
90 	struct file *owner;
91 	char *write_buf;
92 	unsigned int rds_users;
93 };
94 
95 #define WL1273_IRQ_MASK	 (WL1273_FR_EVENT		|	\
96 			  WL1273_POW_ENB_EVENT)
97 
98 /*
99  * static unsigned int rds_buf - the number of RDS buffer blocks used.
100  *
101  * The default number is 100.
102  */
103 static unsigned int rds_buf = 100;
104 module_param(rds_buf, uint, 0);
105 MODULE_PARM_DESC(rds_buf, "Number of RDS buffer entries. Default = 100");
106 
wl1273_fm_write_fw(struct wl1273_core * core,__u8 * fw,int len)107 static int wl1273_fm_write_fw(struct wl1273_core *core,
108 			      __u8 *fw, int len)
109 {
110 	struct i2c_client *client = core->client;
111 	struct i2c_msg msg;
112 	int i, r = 0;
113 
114 	msg.addr = client->addr;
115 	msg.flags = 0;
116 
117 	for (i = 0; i <= len; i++) {
118 		msg.len = fw[0];
119 		msg.buf = fw + 1;
120 
121 		fw += msg.len + 1;
122 		dev_dbg(&client->dev, "%s:len[%d]: %d\n", __func__, i, msg.len);
123 
124 		r = i2c_transfer(client->adapter, &msg, 1);
125 		if (r < 0 && i < len + 1)
126 			break;
127 	}
128 
129 	dev_dbg(&client->dev, "%s: i: %d\n", __func__, i);
130 	dev_dbg(&client->dev, "%s: len + 1: %d\n", __func__, len + 1);
131 
132 	/* Last transfer always fails. */
133 	if (i == len || r == 1)
134 		r = 0;
135 
136 	return r;
137 }
138 
139 #define WL1273_FIFO_HAS_DATA(status)	(1 << 5 & status)
140 #define WL1273_RDS_CORRECTABLE_ERROR	(1 << 3)
141 #define WL1273_RDS_UNCORRECTABLE_ERROR	(1 << 4)
142 
wl1273_fm_rds(struct wl1273_device * radio)143 static int wl1273_fm_rds(struct wl1273_device *radio)
144 {
145 	struct wl1273_core *core = radio->core;
146 	struct i2c_client *client = core->client;
147 	u16 val;
148 	u8 b0 = WL1273_RDS_DATA_GET, status;
149 	struct v4l2_rds_data rds = { 0, 0, 0 };
150 	struct i2c_msg msg[] = {
151 		{
152 			.addr = client->addr,
153 			.flags = 0,
154 			.buf = &b0,
155 			.len = 1,
156 		},
157 		{
158 			.addr = client->addr,
159 			.flags = I2C_M_RD,
160 			.buf = (u8 *) &rds,
161 			.len = sizeof(rds),
162 		}
163 	};
164 	int r;
165 
166 	if (core->mode != WL1273_MODE_RX)
167 		return 0;
168 
169 	r = core->read(core, WL1273_RDS_SYNC_GET, &val);
170 	if (r)
171 		return r;
172 
173 	if ((val & 0x01) == 0) {
174 		/* RDS decoder not synchronized */
175 		return -EAGAIN;
176 	}
177 
178 	/* copy all four RDS blocks to internal buffer */
179 	do {
180 		r = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
181 		if (r != ARRAY_SIZE(msg)) {
182 			dev_err(radio->dev, WL1273_FM_DRIVER_NAME
183 				": %s: read_rds error r == %i)\n",
184 				__func__, r);
185 		}
186 
187 		status = rds.block;
188 
189 		if (!WL1273_FIFO_HAS_DATA(status))
190 			break;
191 
192 		/* copy bits 0-2 (the block ID) to bits 3-5 */
193 		rds.block = V4L2_RDS_BLOCK_MSK & status;
194 		rds.block |= rds.block << 3;
195 
196 		/* copy the error bits to standard positions */
197 		if (WL1273_RDS_UNCORRECTABLE_ERROR & status) {
198 			rds.block |= V4L2_RDS_BLOCK_ERROR;
199 			rds.block &= ~V4L2_RDS_BLOCK_CORRECTED;
200 		} else if  (WL1273_RDS_CORRECTABLE_ERROR & status) {
201 			rds.block &= ~V4L2_RDS_BLOCK_ERROR;
202 			rds.block |= V4L2_RDS_BLOCK_CORRECTED;
203 		}
204 
205 		/* copy RDS block to internal buffer */
206 		memcpy(&radio->buffer[radio->wr_index], &rds, RDS_BLOCK_SIZE);
207 		radio->wr_index += 3;
208 
209 		/* wrap write pointer */
210 		if (radio->wr_index >= radio->buf_size)
211 			radio->wr_index = 0;
212 
213 		/* check for overflow & start over */
214 		if (radio->wr_index == radio->rd_index) {
215 			dev_dbg(radio->dev, "RDS OVERFLOW");
216 
217 			radio->rd_index = 0;
218 			radio->wr_index = 0;
219 			break;
220 		}
221 	} while (WL1273_FIFO_HAS_DATA(status));
222 
223 	/* wake up read queue */
224 	if (radio->wr_index != radio->rd_index)
225 		wake_up_interruptible(&radio->read_queue);
226 
227 	return 0;
228 }
229 
wl1273_fm_irq_thread_handler(int irq,void * dev_id)230 static irqreturn_t wl1273_fm_irq_thread_handler(int irq, void *dev_id)
231 {
232 	struct wl1273_device *radio = dev_id;
233 	struct wl1273_core *core = radio->core;
234 	u16 flags;
235 	int r;
236 
237 	r = core->read(core, WL1273_FLAG_GET, &flags);
238 	if (r)
239 		goto out;
240 
241 	if (flags & WL1273_BL_EVENT) {
242 		radio->irq_received = flags;
243 		dev_dbg(radio->dev, "IRQ: BL\n");
244 	}
245 
246 	if (flags & WL1273_RDS_EVENT) {
247 		msleep(200);
248 
249 		wl1273_fm_rds(radio);
250 	}
251 
252 	if (flags & WL1273_BBLK_EVENT)
253 		dev_dbg(radio->dev, "IRQ: BBLK\n");
254 
255 	if (flags & WL1273_LSYNC_EVENT)
256 		dev_dbg(radio->dev, "IRQ: LSYNC\n");
257 
258 	if (flags & WL1273_LEV_EVENT) {
259 		u16 level;
260 
261 		r = core->read(core, WL1273_RSSI_LVL_GET, &level);
262 		if (r)
263 			goto out;
264 
265 		if (level > 14)
266 			dev_dbg(radio->dev, "IRQ: LEV: 0x%x04\n", level);
267 	}
268 
269 	if (flags & WL1273_IFFR_EVENT)
270 		dev_dbg(radio->dev, "IRQ: IFFR\n");
271 
272 	if (flags & WL1273_PI_EVENT)
273 		dev_dbg(radio->dev, "IRQ: PI\n");
274 
275 	if (flags & WL1273_PD_EVENT)
276 		dev_dbg(radio->dev, "IRQ: PD\n");
277 
278 	if (flags & WL1273_STIC_EVENT)
279 		dev_dbg(radio->dev, "IRQ: STIC\n");
280 
281 	if (flags & WL1273_MAL_EVENT)
282 		dev_dbg(radio->dev, "IRQ: MAL\n");
283 
284 	if (flags & WL1273_POW_ENB_EVENT) {
285 		complete(&radio->busy);
286 		dev_dbg(radio->dev, "NOT BUSY\n");
287 		dev_dbg(radio->dev, "IRQ: POW_ENB\n");
288 	}
289 
290 	if (flags & WL1273_SCAN_OVER_EVENT)
291 		dev_dbg(radio->dev, "IRQ: SCAN_OVER\n");
292 
293 	if (flags & WL1273_ERROR_EVENT)
294 		dev_dbg(radio->dev, "IRQ: ERROR\n");
295 
296 	if (flags & WL1273_FR_EVENT) {
297 		u16 freq;
298 
299 		dev_dbg(radio->dev, "IRQ: FR:\n");
300 
301 		if (core->mode == WL1273_MODE_RX) {
302 			r = core->write(core, WL1273_TUNER_MODE_SET,
303 					TUNER_MODE_STOP_SEARCH);
304 			if (r) {
305 				dev_err(radio->dev,
306 					"%s: TUNER_MODE_SET fails: %d\n",
307 					__func__, r);
308 				goto out;
309 			}
310 
311 			r = core->read(core, WL1273_FREQ_SET, &freq);
312 			if (r)
313 				goto out;
314 
315 			if (radio->band == WL1273_BAND_JAPAN)
316 				radio->rx_frequency = WL1273_BAND_JAPAN_LOW +
317 					freq * 50;
318 			else
319 				radio->rx_frequency = WL1273_BAND_OTHER_LOW +
320 					freq * 50;
321 			/*
322 			 *  The driver works better with this msleep,
323 			 *  the documentation doesn't mention it.
324 			 */
325 			usleep_range(10000, 15000);
326 
327 			dev_dbg(radio->dev, "%dkHz\n", radio->rx_frequency);
328 
329 		} else {
330 			r = core->read(core, WL1273_CHANL_SET, &freq);
331 			if (r)
332 				goto out;
333 
334 			dev_dbg(radio->dev, "%dkHz\n", freq);
335 		}
336 		dev_dbg(radio->dev, "%s: NOT BUSY\n", __func__);
337 	}
338 
339 out:
340 	core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
341 	complete(&radio->busy);
342 
343 	return IRQ_HANDLED;
344 }
345 
wl1273_fm_set_tx_freq(struct wl1273_device * radio,unsigned int freq)346 static int wl1273_fm_set_tx_freq(struct wl1273_device *radio, unsigned int freq)
347 {
348 	struct wl1273_core *core = radio->core;
349 	int r = 0;
350 	unsigned long t;
351 
352 	if (freq < WL1273_BAND_TX_LOW) {
353 		dev_err(radio->dev,
354 			"Frequency out of range: %d < %d\n", freq,
355 			WL1273_BAND_TX_LOW);
356 		return -ERANGE;
357 	}
358 
359 	if (freq > WL1273_BAND_TX_HIGH) {
360 		dev_err(radio->dev,
361 			"Frequency out of range: %d > %d\n", freq,
362 			WL1273_BAND_TX_HIGH);
363 		return -ERANGE;
364 	}
365 
366 	/*
367 	 *  The driver works better with this sleep,
368 	 *  the documentation doesn't mention it.
369 	 */
370 	usleep_range(5000, 10000);
371 
372 	dev_dbg(radio->dev, "%s: freq: %d kHz\n", __func__, freq);
373 
374 	/* Set the current tx channel */
375 	r = core->write(core, WL1273_CHANL_SET, freq / 10);
376 	if (r)
377 		return r;
378 
379 	reinit_completion(&radio->busy);
380 
381 	/* wait for the FR IRQ */
382 	t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
383 	if (!t)
384 		return -ETIMEDOUT;
385 
386 	dev_dbg(radio->dev, "WL1273_CHANL_SET: %lu\n", t);
387 
388 	/* Enable the output power */
389 	r = core->write(core, WL1273_POWER_ENB_SET, 1);
390 	if (r)
391 		return r;
392 
393 	reinit_completion(&radio->busy);
394 
395 	/* wait for the POWER_ENB IRQ */
396 	t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
397 	if (!t)
398 		return -ETIMEDOUT;
399 
400 	radio->tx_frequency = freq;
401 	dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %lu\n", t);
402 
403 	return	0;
404 }
405 
wl1273_fm_set_rx_freq(struct wl1273_device * radio,unsigned int freq)406 static int wl1273_fm_set_rx_freq(struct wl1273_device *radio, unsigned int freq)
407 {
408 	struct wl1273_core *core = radio->core;
409 	int r, f;
410 	unsigned long t;
411 
412 	if (freq < radio->rangelow) {
413 		dev_err(radio->dev,
414 			"Frequency out of range: %d < %d\n", freq,
415 			radio->rangelow);
416 		r = -ERANGE;
417 		goto err;
418 	}
419 
420 	if (freq > radio->rangehigh) {
421 		dev_err(radio->dev,
422 			"Frequency out of range: %d > %d\n", freq,
423 			radio->rangehigh);
424 		r = -ERANGE;
425 		goto err;
426 	}
427 
428 	dev_dbg(radio->dev, "%s: %dkHz\n", __func__, freq);
429 
430 	core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
431 
432 	if (radio->band == WL1273_BAND_JAPAN)
433 		f = (freq - WL1273_BAND_JAPAN_LOW) / 50;
434 	else
435 		f = (freq - WL1273_BAND_OTHER_LOW) / 50;
436 
437 	r = core->write(core, WL1273_FREQ_SET, f);
438 	if (r) {
439 		dev_err(radio->dev, "FREQ_SET fails\n");
440 		goto err;
441 	}
442 
443 	r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET);
444 	if (r) {
445 		dev_err(radio->dev, "TUNER_MODE_SET fails\n");
446 		goto err;
447 	}
448 
449 	reinit_completion(&radio->busy);
450 
451 	t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
452 	if (!t) {
453 		dev_err(radio->dev, "%s: TIMEOUT\n", __func__);
454 		return -ETIMEDOUT;
455 	}
456 
457 	radio->rd_index = 0;
458 	radio->wr_index = 0;
459 	radio->rx_frequency = freq;
460 	return 0;
461 err:
462 	return r;
463 }
464 
wl1273_fm_get_freq(struct wl1273_device * radio)465 static int wl1273_fm_get_freq(struct wl1273_device *radio)
466 {
467 	struct wl1273_core *core = radio->core;
468 	unsigned int freq;
469 	u16 f;
470 	int r;
471 
472 	if (core->mode == WL1273_MODE_RX) {
473 		r = core->read(core, WL1273_FREQ_SET, &f);
474 		if (r)
475 			return r;
476 
477 		dev_dbg(radio->dev, "Freq get: 0x%04x\n", f);
478 		if (radio->band == WL1273_BAND_JAPAN)
479 			freq = WL1273_BAND_JAPAN_LOW + 50 * f;
480 		else
481 			freq = WL1273_BAND_OTHER_LOW + 50 * f;
482 	} else {
483 		r = core->read(core, WL1273_CHANL_SET, &f);
484 		if (r)
485 			return r;
486 
487 		freq = f * 10;
488 	}
489 
490 	return freq;
491 }
492 
493 /**
494  * wl1273_fm_upload_firmware_patch() -	Upload the firmware.
495  * @radio:				A pointer to the device struct.
496  *
497  * The firmware file consists of arrays of bytes where the first byte
498  * gives the array length. The first byte in the file gives the
499  * number of these arrays.
500  */
wl1273_fm_upload_firmware_patch(struct wl1273_device * radio)501 static int wl1273_fm_upload_firmware_patch(struct wl1273_device *radio)
502 {
503 	struct wl1273_core *core = radio->core;
504 	unsigned int packet_num;
505 	const struct firmware *fw_p;
506 	const char *fw_name = "radio-wl1273-fw.bin";
507 	struct device *dev = radio->dev;
508 	__u8 *ptr;
509 	int r;
510 
511 	dev_dbg(dev, "%s:\n", __func__);
512 
513 	/*
514 	 * Uploading the firmware patch is not always necessary,
515 	 * so we only print an info message.
516 	 */
517 	if (request_firmware(&fw_p, fw_name, dev)) {
518 		dev_info(dev, "%s - %s not found\n", __func__, fw_name);
519 
520 		return 0;
521 	}
522 
523 	ptr = (__u8 *) fw_p->data;
524 	packet_num = ptr[0];
525 	dev_dbg(dev, "%s: packets: %d\n", __func__, packet_num);
526 
527 	r = wl1273_fm_write_fw(core, ptr + 1, packet_num);
528 	if (r) {
529 		dev_err(dev, "FW upload error: %d\n", r);
530 		goto out;
531 	}
532 
533 	/* ignore possible error here */
534 	core->write(core, WL1273_RESET, 0);
535 
536 	dev_dbg(dev, "%s - download OK, r: %d\n", __func__, r);
537 out:
538 	release_firmware(fw_p);
539 	return r;
540 }
541 
wl1273_fm_stop(struct wl1273_device * radio)542 static int wl1273_fm_stop(struct wl1273_device *radio)
543 {
544 	struct wl1273_core *core = radio->core;
545 
546 	if (core->mode == WL1273_MODE_RX) {
547 		int r = core->write(core, WL1273_POWER_SET,
548 				    WL1273_POWER_SET_OFF);
549 		if (r)
550 			dev_err(radio->dev, "%s: POWER_SET fails: %d\n",
551 				__func__, r);
552 	} else if (core->mode == WL1273_MODE_TX) {
553 		int r = core->write(core, WL1273_PUPD_SET,
554 				    WL1273_PUPD_SET_OFF);
555 		if (r)
556 			dev_err(radio->dev,
557 				"%s: PUPD_SET fails: %d\n", __func__, r);
558 	}
559 
560 	if (core->pdata->disable) {
561 		core->pdata->disable();
562 		dev_dbg(radio->dev, "Back to reset\n");
563 	}
564 
565 	return 0;
566 }
567 
wl1273_fm_start(struct wl1273_device * radio,int new_mode)568 static int wl1273_fm_start(struct wl1273_device *radio, int new_mode)
569 {
570 	struct wl1273_core *core = radio->core;
571 	struct wl1273_fm_platform_data *pdata = core->pdata;
572 	struct device *dev = radio->dev;
573 	int r = -EINVAL;
574 
575 	if (pdata->enable && core->mode == WL1273_MODE_OFF) {
576 		dev_dbg(radio->dev, "Out of reset\n");
577 
578 		pdata->enable();
579 		msleep(250);
580 	}
581 
582 	if (new_mode == WL1273_MODE_RX) {
583 		u16 val = WL1273_POWER_SET_FM;
584 
585 		if (radio->rds_on)
586 			val |= WL1273_POWER_SET_RDS;
587 
588 		/* If this fails try again */
589 		r = core->write(core, WL1273_POWER_SET, val);
590 		if (r) {
591 			msleep(100);
592 
593 			r = core->write(core, WL1273_POWER_SET, val);
594 			if (r) {
595 				dev_err(dev, "%s: POWER_SET fails\n", __func__);
596 				goto fail;
597 			}
598 		}
599 
600 		/* rds buffer configuration */
601 		radio->wr_index = 0;
602 		radio->rd_index = 0;
603 
604 	} else if (new_mode == WL1273_MODE_TX) {
605 		/* If this fails try again once */
606 		r = core->write(core, WL1273_PUPD_SET, WL1273_PUPD_SET_ON);
607 		if (r) {
608 			msleep(100);
609 			r = core->write(core, WL1273_PUPD_SET,
610 					WL1273_PUPD_SET_ON);
611 			if (r) {
612 				dev_err(dev, "%s: PUPD_SET fails\n", __func__);
613 				goto fail;
614 			}
615 		}
616 
617 		if (radio->rds_on)
618 			r = core->write(core, WL1273_RDS_DATA_ENB, 1);
619 		else
620 			r = core->write(core, WL1273_RDS_DATA_ENB, 0);
621 	} else {
622 		dev_warn(dev, "%s: Illegal mode.\n", __func__);
623 	}
624 
625 	if (core->mode == WL1273_MODE_OFF) {
626 		r = wl1273_fm_upload_firmware_patch(radio);
627 		if (r)
628 			dev_warn(dev, "Firmware upload failed.\n");
629 
630 		/*
631 		 * Sometimes the chip is in a wrong power state at this point.
632 		 * So we set the power once again.
633 		 */
634 		if (new_mode == WL1273_MODE_RX) {
635 			u16 val = WL1273_POWER_SET_FM;
636 
637 			if (radio->rds_on)
638 				val |= WL1273_POWER_SET_RDS;
639 
640 			r = core->write(core, WL1273_POWER_SET, val);
641 			if (r) {
642 				dev_err(dev, "%s: POWER_SET fails\n", __func__);
643 				goto fail;
644 			}
645 		} else if (new_mode == WL1273_MODE_TX) {
646 			r = core->write(core, WL1273_PUPD_SET,
647 					WL1273_PUPD_SET_ON);
648 			if (r) {
649 				dev_err(dev, "%s: PUPD_SET fails\n", __func__);
650 				goto fail;
651 			}
652 		}
653 	}
654 
655 	return 0;
656 fail:
657 	if (pdata->disable)
658 		pdata->disable();
659 
660 	dev_dbg(dev, "%s: return: %d\n", __func__, r);
661 	return r;
662 }
663 
wl1273_fm_suspend(struct wl1273_device * radio)664 static int wl1273_fm_suspend(struct wl1273_device *radio)
665 {
666 	struct wl1273_core *core = radio->core;
667 	int r = 0;
668 
669 	/* Cannot go from OFF to SUSPENDED */
670 	if (core->mode == WL1273_MODE_RX)
671 		r = core->write(core, WL1273_POWER_SET,
672 				WL1273_POWER_SET_RETENTION);
673 	else if (core->mode == WL1273_MODE_TX)
674 		r = core->write(core, WL1273_PUPD_SET,
675 				WL1273_PUPD_SET_RETENTION);
676 	else
677 		r = -EINVAL;
678 
679 	if (r) {
680 		dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r);
681 		goto out;
682 	}
683 
684 out:
685 	return r;
686 }
687 
wl1273_fm_set_mode(struct wl1273_device * radio,int mode)688 static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode)
689 {
690 	struct wl1273_core *core = radio->core;
691 	struct device *dev = radio->dev;
692 	int old_mode;
693 	int r;
694 
695 	dev_dbg(dev, "%s\n", __func__);
696 	dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden);
697 
698 	old_mode = core->mode;
699 	if (mode & radio->forbidden) {
700 		r = -EPERM;
701 		goto out;
702 	}
703 
704 	switch (mode) {
705 	case WL1273_MODE_RX:
706 	case WL1273_MODE_TX:
707 		r = wl1273_fm_start(radio, mode);
708 		if (r) {
709 			dev_err(dev, "%s: Cannot start.\n", __func__);
710 			wl1273_fm_stop(radio);
711 			goto out;
712 		}
713 
714 		core->mode = mode;
715 		r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
716 		if (r) {
717 			dev_err(dev, "INT_MASK_SET fails.\n");
718 			goto out;
719 		}
720 
721 		/* remember previous settings */
722 		if (mode == WL1273_MODE_RX) {
723 			r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
724 			if (r) {
725 				dev_err(dev, "set freq fails: %d.\n", r);
726 				goto out;
727 			}
728 
729 			r = core->set_volume(core, core->volume);
730 			if (r) {
731 				dev_err(dev, "set volume fails: %d.\n", r);
732 				goto out;
733 			}
734 
735 			dev_dbg(dev, "%s: Set vol: %d.\n", __func__,
736 				core->volume);
737 		} else {
738 			r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency);
739 			if (r) {
740 				dev_err(dev, "set freq fails: %d.\n", r);
741 				goto out;
742 			}
743 		}
744 
745 		dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__);
746 
747 		r = core->set_audio(core, core->audio_mode);
748 		if (r)
749 			dev_err(dev, "Cannot set audio mode.\n");
750 		break;
751 
752 	case WL1273_MODE_OFF:
753 		r = wl1273_fm_stop(radio);
754 		if (r)
755 			dev_err(dev, "%s: Off fails: %d\n", __func__, r);
756 		else
757 			core->mode = WL1273_MODE_OFF;
758 
759 		break;
760 
761 	case WL1273_MODE_SUSPENDED:
762 		r = wl1273_fm_suspend(radio);
763 		if (r)
764 			dev_err(dev, "%s: Suspend fails: %d\n", __func__, r);
765 		else
766 			core->mode = WL1273_MODE_SUSPENDED;
767 
768 		break;
769 
770 	default:
771 		dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode);
772 		r = -EINVAL;
773 		break;
774 	}
775 out:
776 	if (r)
777 		core->mode = old_mode;
778 
779 	return r;
780 }
781 
wl1273_fm_set_seek(struct wl1273_device * radio,unsigned int wrap_around,unsigned int seek_upward,int level)782 static int wl1273_fm_set_seek(struct wl1273_device *radio,
783 			      unsigned int wrap_around,
784 			      unsigned int seek_upward,
785 			      int level)
786 {
787 	struct wl1273_core *core = radio->core;
788 	int r = 0;
789 	unsigned int dir = (seek_upward == 0) ? 0 : 1;
790 	unsigned int f;
791 
792 	f = radio->rx_frequency;
793 	dev_dbg(radio->dev, "rx_frequency: %d\n", f);
794 
795 	if (dir && f + radio->spacing <= radio->rangehigh)
796 		r = wl1273_fm_set_rx_freq(radio, f + radio->spacing);
797 	else if (dir && wrap_around)
798 		r = wl1273_fm_set_rx_freq(radio, radio->rangelow);
799 	else if (f - radio->spacing >= radio->rangelow)
800 		r = wl1273_fm_set_rx_freq(radio, f - radio->spacing);
801 	else if (wrap_around)
802 		r = wl1273_fm_set_rx_freq(radio, radio->rangehigh);
803 
804 	if (r)
805 		goto out;
806 
807 	if (level < SCHAR_MIN || level > SCHAR_MAX)
808 		return -EINVAL;
809 
810 	reinit_completion(&radio->busy);
811 	dev_dbg(radio->dev, "%s: BUSY\n", __func__);
812 
813 	r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
814 	if (r)
815 		goto out;
816 
817 	dev_dbg(radio->dev, "%s\n", __func__);
818 
819 	r = core->write(core, WL1273_SEARCH_LVL_SET, level);
820 	if (r)
821 		goto out;
822 
823 	r = core->write(core, WL1273_SEARCH_DIR_SET, dir);
824 	if (r)
825 		goto out;
826 
827 	r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
828 	if (r)
829 		goto out;
830 
831 	/* wait for the FR IRQ */
832 	wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
833 	if (!(radio->irq_received & WL1273_BL_EVENT)) {
834 		r = -ETIMEDOUT;
835 		goto out;
836 	}
837 
838 	radio->irq_received &= ~WL1273_BL_EVENT;
839 
840 	if (!wrap_around)
841 		goto out;
842 
843 	/* Wrap around */
844 	dev_dbg(radio->dev, "Wrap around in HW seek.\n");
845 
846 	if (seek_upward)
847 		f = radio->rangelow;
848 	else
849 		f = radio->rangehigh;
850 
851 	r = wl1273_fm_set_rx_freq(radio, f);
852 	if (r)
853 		goto out;
854 
855 	reinit_completion(&radio->busy);
856 	dev_dbg(radio->dev, "%s: BUSY\n", __func__);
857 
858 	r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
859 	if (r)
860 		goto out;
861 
862 	/* wait for the FR IRQ */
863 	if (!wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000)))
864 		r = -ETIMEDOUT;
865 out:
866 	dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r);
867 	return r;
868 }
869 
870 /**
871  * wl1273_fm_get_tx_ctune() -	Get the TX tuning capacitor value.
872  * @radio:			A pointer to the device struct.
873  */
wl1273_fm_get_tx_ctune(struct wl1273_device * radio)874 static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio)
875 {
876 	struct wl1273_core *core = radio->core;
877 	struct device *dev = radio->dev;
878 	u16 val;
879 	int r;
880 
881 	if (core->mode == WL1273_MODE_OFF ||
882 	    core->mode == WL1273_MODE_SUSPENDED)
883 		return -EPERM;
884 
885 	r = core->read(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
886 	if (r) {
887 		dev_err(dev, "%s: read error: %d\n", __func__, r);
888 		goto out;
889 	}
890 
891 out:
892 	return val;
893 }
894 
895 /**
896  * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
897  * @radio:			 A pointer to the device struct.
898  * @preemphasis:		 The new pre-amphasis value.
899  *
900  * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
901  * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
902  */
wl1273_fm_set_preemphasis(struct wl1273_device * radio,unsigned int preemphasis)903 static int wl1273_fm_set_preemphasis(struct wl1273_device *radio,
904 				     unsigned int preemphasis)
905 {
906 	struct wl1273_core *core = radio->core;
907 	int r;
908 	u16 em;
909 
910 	if (core->mode == WL1273_MODE_OFF ||
911 	    core->mode == WL1273_MODE_SUSPENDED)
912 		return -EPERM;
913 
914 	mutex_lock(&core->lock);
915 
916 	switch (preemphasis) {
917 	case V4L2_PREEMPHASIS_DISABLED:
918 		em = 1;
919 		break;
920 	case V4L2_PREEMPHASIS_50_uS:
921 		em = 0;
922 		break;
923 	case V4L2_PREEMPHASIS_75_uS:
924 		em = 2;
925 		break;
926 	default:
927 		r = -EINVAL;
928 		goto out;
929 	}
930 
931 	r = core->write(core, WL1273_PREMPH_SET, em);
932 	if (r)
933 		goto out;
934 
935 	radio->preemphasis = preemphasis;
936 
937 out:
938 	mutex_unlock(&core->lock);
939 	return r;
940 }
941 
wl1273_fm_rds_on(struct wl1273_device * radio)942 static int wl1273_fm_rds_on(struct wl1273_device *radio)
943 {
944 	struct wl1273_core *core = radio->core;
945 	int r;
946 
947 	dev_dbg(radio->dev, "%s\n", __func__);
948 	if (radio->rds_on)
949 		return 0;
950 
951 	r = core->write(core, WL1273_POWER_SET,
952 			WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
953 	if (r)
954 		goto out;
955 
956 	r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
957 	if (r)
958 		dev_err(radio->dev, "set freq fails: %d.\n", r);
959 out:
960 	return r;
961 }
962 
wl1273_fm_rds_off(struct wl1273_device * radio)963 static int wl1273_fm_rds_off(struct wl1273_device *radio)
964 {
965 	struct wl1273_core *core = radio->core;
966 	int r;
967 
968 	if (!radio->rds_on)
969 		return 0;
970 
971 	radio->irq_flags &= ~WL1273_RDS_EVENT;
972 
973 	r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
974 	if (r)
975 		goto out;
976 
977 	/* Service pending read */
978 	wake_up_interruptible(&radio->read_queue);
979 
980 	dev_dbg(radio->dev, "%s\n", __func__);
981 
982 	r = core->write(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
983 	if (r)
984 		goto out;
985 
986 	r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
987 	if (r)
988 		dev_err(radio->dev, "set freq fails: %d.\n", r);
989 out:
990 	dev_dbg(radio->dev, "%s: exiting...\n", __func__);
991 
992 	return r;
993 }
994 
wl1273_fm_set_rds(struct wl1273_device * radio,unsigned int new_mode)995 static int wl1273_fm_set_rds(struct wl1273_device *radio, unsigned int new_mode)
996 {
997 	int r = 0;
998 	struct wl1273_core *core = radio->core;
999 
1000 	if (core->mode == WL1273_MODE_OFF ||
1001 	    core->mode == WL1273_MODE_SUSPENDED)
1002 		return -EPERM;
1003 
1004 	if (new_mode == WL1273_RDS_RESET) {
1005 		r = core->write(core, WL1273_RDS_CNTRL_SET, 1);
1006 		return r;
1007 	}
1008 
1009 	if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) {
1010 		r = core->write(core, WL1273_RDS_DATA_ENB, 0);
1011 	} else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) {
1012 		r = core->write(core, WL1273_RDS_DATA_ENB, 1);
1013 	} else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) {
1014 		r = wl1273_fm_rds_off(radio);
1015 	} else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) {
1016 		r = wl1273_fm_rds_on(radio);
1017 	} else {
1018 		dev_err(radio->dev, "%s: Unknown mode: %d\n",
1019 			__func__, new_mode);
1020 		r = -EINVAL;
1021 	}
1022 
1023 	if (!r)
1024 		radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false;
1025 
1026 	return r;
1027 }
1028 
wl1273_fm_fops_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1029 static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf,
1030 				    size_t count, loff_t *ppos)
1031 {
1032 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1033 	struct wl1273_core *core = radio->core;
1034 	u16 val;
1035 	int r;
1036 
1037 	dev_dbg(radio->dev, "%s\n", __func__);
1038 
1039 	if (core->mode != WL1273_MODE_TX)
1040 		return count;
1041 
1042 	if (radio->rds_users == 0) {
1043 		dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1044 		return 0;
1045 	}
1046 
1047 	if (mutex_lock_interruptible(&core->lock))
1048 		return -EINTR;
1049 	/*
1050 	 * Multiple processes can open the device, but only
1051 	 * one gets to write to it.
1052 	 */
1053 	if (radio->owner && radio->owner != file) {
1054 		r = -EBUSY;
1055 		goto out;
1056 	}
1057 	radio->owner = file;
1058 
1059 	/* Manual Mode */
1060 	if (count > 255)
1061 		val = 255;
1062 	else
1063 		val = count;
1064 
1065 	core->write(core, WL1273_RDS_CONFIG_DATA_SET, val);
1066 
1067 	if (copy_from_user(radio->write_buf + 1, buf, val)) {
1068 		r = -EFAULT;
1069 		goto out;
1070 	}
1071 
1072 	dev_dbg(radio->dev, "Count: %d\n", val);
1073 	dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf);
1074 
1075 	radio->write_buf[0] = WL1273_RDS_DATA_SET;
1076 	core->write_data(core, radio->write_buf, val + 1);
1077 
1078 	r = val;
1079 out:
1080 	mutex_unlock(&core->lock);
1081 
1082 	return r;
1083 }
1084 
wl1273_fm_fops_poll(struct file * file,struct poll_table_struct * pts)1085 static unsigned int wl1273_fm_fops_poll(struct file *file,
1086 					struct poll_table_struct *pts)
1087 {
1088 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1089 	struct wl1273_core *core = radio->core;
1090 
1091 	if (radio->owner && radio->owner != file)
1092 		return -EBUSY;
1093 
1094 	radio->owner = file;
1095 
1096 	if (core->mode == WL1273_MODE_RX) {
1097 		poll_wait(file, &radio->read_queue, pts);
1098 
1099 		if (radio->rd_index != radio->wr_index)
1100 			return POLLIN | POLLRDNORM;
1101 
1102 	} else if (core->mode == WL1273_MODE_TX) {
1103 		return POLLOUT | POLLWRNORM;
1104 	}
1105 
1106 	return 0;
1107 }
1108 
wl1273_fm_fops_open(struct file * file)1109 static int wl1273_fm_fops_open(struct file *file)
1110 {
1111 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1112 	struct wl1273_core *core = radio->core;
1113 	int r = 0;
1114 
1115 	dev_dbg(radio->dev, "%s\n", __func__);
1116 
1117 	if (core->mode == WL1273_MODE_RX && radio->rds_on &&
1118 	    !radio->rds_users) {
1119 		dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode);
1120 
1121 		if (mutex_lock_interruptible(&core->lock))
1122 			return -EINTR;
1123 
1124 		radio->irq_flags |= WL1273_RDS_EVENT;
1125 
1126 		r = core->write(core, WL1273_INT_MASK_SET,
1127 				radio->irq_flags);
1128 		if (r) {
1129 			mutex_unlock(&core->lock);
1130 			goto out;
1131 		}
1132 
1133 		radio->rds_users++;
1134 
1135 		mutex_unlock(&core->lock);
1136 	}
1137 out:
1138 	return r;
1139 }
1140 
wl1273_fm_fops_release(struct file * file)1141 static int wl1273_fm_fops_release(struct file *file)
1142 {
1143 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1144 	struct wl1273_core *core = radio->core;
1145 	int r = 0;
1146 
1147 	dev_dbg(radio->dev, "%s\n", __func__);
1148 
1149 	if (radio->rds_users > 0) {
1150 		radio->rds_users--;
1151 		if (radio->rds_users == 0) {
1152 			mutex_lock(&core->lock);
1153 
1154 			radio->irq_flags &= ~WL1273_RDS_EVENT;
1155 
1156 			if (core->mode == WL1273_MODE_RX) {
1157 				r = core->write(core,
1158 						WL1273_INT_MASK_SET,
1159 						radio->irq_flags);
1160 				if (r) {
1161 					mutex_unlock(&core->lock);
1162 					goto out;
1163 				}
1164 			}
1165 			mutex_unlock(&core->lock);
1166 		}
1167 	}
1168 
1169 	if (file == radio->owner)
1170 		radio->owner = NULL;
1171 out:
1172 	return r;
1173 }
1174 
wl1273_fm_fops_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1175 static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
1176 				   size_t count, loff_t *ppos)
1177 {
1178 	int r = 0;
1179 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1180 	struct wl1273_core *core = radio->core;
1181 	unsigned int block_count = 0;
1182 	u16 val;
1183 
1184 	dev_dbg(radio->dev, "%s\n", __func__);
1185 
1186 	if (core->mode != WL1273_MODE_RX)
1187 		return 0;
1188 
1189 	if (radio->rds_users == 0) {
1190 		dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1191 		return 0;
1192 	}
1193 
1194 	if (mutex_lock_interruptible(&core->lock))
1195 		return -EINTR;
1196 
1197 	/*
1198 	 * Multiple processes can open the device, but only
1199 	 * one at a time gets read access.
1200 	 */
1201 	if (radio->owner && radio->owner != file) {
1202 		r = -EBUSY;
1203 		goto out;
1204 	}
1205 	radio->owner = file;
1206 
1207 	r = core->read(core, WL1273_RDS_SYNC_GET, &val);
1208 	if (r) {
1209 		dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__);
1210 		goto out;
1211 	} else if (val == 0) {
1212 		dev_info(radio->dev, "RDS_SYNC: Not synchronized\n");
1213 		r = -ENODATA;
1214 		goto out;
1215 	}
1216 
1217 	/* block if no new data available */
1218 	while (radio->wr_index == radio->rd_index) {
1219 		if (file->f_flags & O_NONBLOCK) {
1220 			r = -EWOULDBLOCK;
1221 			goto out;
1222 		}
1223 
1224 		dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__);
1225 		if (wait_event_interruptible(radio->read_queue,
1226 					     radio->wr_index !=
1227 					     radio->rd_index) < 0) {
1228 			r = -EINTR;
1229 			goto out;
1230 		}
1231 	}
1232 
1233 	/* calculate block count from byte count */
1234 	count /= RDS_BLOCK_SIZE;
1235 
1236 	/* copy RDS blocks from the internal buffer and to user buffer */
1237 	while (block_count < count) {
1238 		if (radio->rd_index == radio->wr_index)
1239 			break;
1240 
1241 		/* always transfer complete RDS blocks */
1242 		if (copy_to_user(buf, &radio->buffer[radio->rd_index],
1243 				 RDS_BLOCK_SIZE))
1244 			break;
1245 
1246 		/* increment and wrap the read pointer */
1247 		radio->rd_index += RDS_BLOCK_SIZE;
1248 		if (radio->rd_index >= radio->buf_size)
1249 			radio->rd_index = 0;
1250 
1251 		/* increment counters */
1252 		block_count++;
1253 		buf += RDS_BLOCK_SIZE;
1254 		r += RDS_BLOCK_SIZE;
1255 	}
1256 
1257 out:
1258 	dev_dbg(radio->dev, "%s: exit\n", __func__);
1259 	mutex_unlock(&core->lock);
1260 
1261 	return r;
1262 }
1263 
1264 static const struct v4l2_file_operations wl1273_fops = {
1265 	.owner		= THIS_MODULE,
1266 	.read		= wl1273_fm_fops_read,
1267 	.write		= wl1273_fm_fops_write,
1268 	.poll		= wl1273_fm_fops_poll,
1269 	.unlocked_ioctl	= video_ioctl2,
1270 	.open		= wl1273_fm_fops_open,
1271 	.release	= wl1273_fm_fops_release,
1272 };
1273 
wl1273_fm_vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * capability)1274 static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
1275 				     struct v4l2_capability *capability)
1276 {
1277 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1278 
1279 	dev_dbg(radio->dev, "%s\n", __func__);
1280 
1281 	strlcpy(capability->driver, WL1273_FM_DRIVER_NAME,
1282 		sizeof(capability->driver));
1283 	strlcpy(capability->card, "Texas Instruments Wl1273 FM Radio",
1284 		sizeof(capability->card));
1285 	strlcpy(capability->bus_info, radio->bus_type,
1286 		sizeof(capability->bus_info));
1287 
1288 	capability->device_caps = V4L2_CAP_HW_FREQ_SEEK |
1289 		V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
1290 		V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
1291 		V4L2_CAP_RDS_OUTPUT;
1292 	capability->capabilities = capability->device_caps |
1293 		V4L2_CAP_DEVICE_CAPS;
1294 
1295 	return 0;
1296 }
1297 
wl1273_fm_vidioc_g_input(struct file * file,void * priv,unsigned int * i)1298 static int wl1273_fm_vidioc_g_input(struct file *file, void *priv,
1299 				    unsigned int *i)
1300 {
1301 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1302 
1303 	dev_dbg(radio->dev, "%s\n", __func__);
1304 
1305 	*i = 0;
1306 
1307 	return 0;
1308 }
1309 
wl1273_fm_vidioc_s_input(struct file * file,void * priv,unsigned int i)1310 static int wl1273_fm_vidioc_s_input(struct file *file, void *priv,
1311 				    unsigned int i)
1312 {
1313 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1314 
1315 	dev_dbg(radio->dev, "%s\n", __func__);
1316 
1317 	if (i != 0)
1318 		return -EINVAL;
1319 
1320 	return 0;
1321 }
1322 
1323 /**
1324  * wl1273_fm_set_tx_power() -	Set the transmission power value.
1325  * @core:			A pointer to the device struct.
1326  * @power:			The new power value.
1327  */
wl1273_fm_set_tx_power(struct wl1273_device * radio,u16 power)1328 static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power)
1329 {
1330 	struct wl1273_core *core = radio->core;
1331 	int r;
1332 
1333 	if (core->mode == WL1273_MODE_OFF ||
1334 	    core->mode == WL1273_MODE_SUSPENDED)
1335 		return -EPERM;
1336 
1337 	mutex_lock(&core->lock);
1338 
1339 	/* Convert the dBuV value to chip presentation */
1340 	r = core->write(core, WL1273_POWER_LEV_SET, 122 - power);
1341 	if (r)
1342 		goto out;
1343 
1344 	radio->tx_power = power;
1345 
1346 out:
1347 	mutex_unlock(&core->lock);
1348 	return r;
1349 }
1350 
1351 #define WL1273_SPACING_50kHz	1
1352 #define WL1273_SPACING_100kHz	2
1353 #define WL1273_SPACING_200kHz	4
1354 
wl1273_fm_tx_set_spacing(struct wl1273_device * radio,unsigned int spacing)1355 static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio,
1356 				    unsigned int spacing)
1357 {
1358 	struct wl1273_core *core = radio->core;
1359 	int r;
1360 
1361 	if (spacing == 0) {
1362 		r = core->write(core, WL1273_SCAN_SPACING_SET,
1363 				WL1273_SPACING_100kHz);
1364 		radio->spacing = 100;
1365 	} else if (spacing - 50000 < 25000) {
1366 		r = core->write(core, WL1273_SCAN_SPACING_SET,
1367 				WL1273_SPACING_50kHz);
1368 		radio->spacing = 50;
1369 	} else if (spacing - 100000 < 50000) {
1370 		r = core->write(core, WL1273_SCAN_SPACING_SET,
1371 				WL1273_SPACING_100kHz);
1372 		radio->spacing = 100;
1373 	} else {
1374 		r = core->write(core, WL1273_SCAN_SPACING_SET,
1375 				WL1273_SPACING_200kHz);
1376 		radio->spacing = 200;
1377 	}
1378 
1379 	return r;
1380 }
1381 
wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl * ctrl)1382 static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1383 {
1384 	struct wl1273_device *radio = ctrl->priv;
1385 	struct wl1273_core *core = radio->core;
1386 
1387 	dev_dbg(radio->dev, "%s\n", __func__);
1388 
1389 	if (mutex_lock_interruptible(&core->lock))
1390 		return -EINTR;
1391 
1392 	switch (ctrl->id) {
1393 	case  V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1394 		ctrl->val = wl1273_fm_get_tx_ctune(radio);
1395 		break;
1396 
1397 	default:
1398 		dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
1399 			 __func__, ctrl->id);
1400 		break;
1401 	}
1402 
1403 	mutex_unlock(&core->lock);
1404 
1405 	return 0;
1406 }
1407 
1408 #define WL1273_MUTE_SOFT_ENABLE    (1 << 0)
1409 #define WL1273_MUTE_AC             (1 << 1)
1410 #define WL1273_MUTE_HARD_LEFT      (1 << 2)
1411 #define WL1273_MUTE_HARD_RIGHT     (1 << 3)
1412 #define WL1273_MUTE_SOFT_FORCE     (1 << 4)
1413 
to_radio(struct v4l2_ctrl * ctrl)1414 static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl)
1415 {
1416 	return container_of(ctrl->handler, struct wl1273_device, ctrl_handler);
1417 }
1418 
wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl * ctrl)1419 static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl)
1420 {
1421 	struct wl1273_device *radio = to_radio(ctrl);
1422 	struct wl1273_core *core = radio->core;
1423 	int r = 0;
1424 
1425 	dev_dbg(radio->dev, "%s\n", __func__);
1426 
1427 	switch (ctrl->id) {
1428 	case V4L2_CID_AUDIO_MUTE:
1429 		if (mutex_lock_interruptible(&core->lock))
1430 			return -EINTR;
1431 
1432 		if (core->mode == WL1273_MODE_RX && ctrl->val)
1433 			r = core->write(core,
1434 					WL1273_MUTE_STATUS_SET,
1435 					WL1273_MUTE_HARD_LEFT |
1436 					WL1273_MUTE_HARD_RIGHT);
1437 		else if (core->mode == WL1273_MODE_RX)
1438 			r = core->write(core,
1439 					WL1273_MUTE_STATUS_SET, 0x0);
1440 		else if (core->mode == WL1273_MODE_TX && ctrl->val)
1441 			r = core->write(core, WL1273_MUTE, 1);
1442 		else if (core->mode == WL1273_MODE_TX)
1443 			r = core->write(core, WL1273_MUTE, 0);
1444 
1445 		mutex_unlock(&core->lock);
1446 		break;
1447 
1448 	case V4L2_CID_AUDIO_VOLUME:
1449 		if (ctrl->val == 0)
1450 			r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF);
1451 		else
1452 			r =  core->set_volume(core, core->volume);
1453 		break;
1454 
1455 	case V4L2_CID_TUNE_PREEMPHASIS:
1456 		r = wl1273_fm_set_preemphasis(radio, ctrl->val);
1457 		break;
1458 
1459 	case V4L2_CID_TUNE_POWER_LEVEL:
1460 		r = wl1273_fm_set_tx_power(radio, ctrl->val);
1461 		break;
1462 
1463 	default:
1464 		dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
1465 			 __func__, ctrl->id);
1466 		break;
1467 	}
1468 
1469 	dev_dbg(radio->dev, "%s\n", __func__);
1470 	return r;
1471 }
1472 
wl1273_fm_vidioc_g_audio(struct file * file,void * priv,struct v4l2_audio * audio)1473 static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv,
1474 				    struct v4l2_audio *audio)
1475 {
1476 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1477 
1478 	dev_dbg(radio->dev, "%s\n", __func__);
1479 
1480 	if (audio->index > 1)
1481 		return -EINVAL;
1482 
1483 	strlcpy(audio->name, "Radio", sizeof(audio->name));
1484 	audio->capability = V4L2_AUDCAP_STEREO;
1485 
1486 	return 0;
1487 }
1488 
wl1273_fm_vidioc_s_audio(struct file * file,void * priv,const struct v4l2_audio * audio)1489 static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
1490 				    const struct v4l2_audio *audio)
1491 {
1492 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1493 
1494 	dev_dbg(radio->dev, "%s\n", __func__);
1495 
1496 	if (audio->index != 0)
1497 		return -EINVAL;
1498 
1499 	return 0;
1500 }
1501 
1502 #define WL1273_RDS_NOT_SYNCHRONIZED 0
1503 #define WL1273_RDS_SYNCHRONIZED 1
1504 
wl1273_fm_vidioc_g_tuner(struct file * file,void * priv,struct v4l2_tuner * tuner)1505 static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv,
1506 				    struct v4l2_tuner *tuner)
1507 {
1508 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1509 	struct wl1273_core *core = radio->core;
1510 	u16 val;
1511 	int r;
1512 
1513 	dev_dbg(radio->dev, "%s\n", __func__);
1514 
1515 	if (tuner->index > 0)
1516 		return -EINVAL;
1517 
1518 	strlcpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name));
1519 	tuner->type = V4L2_TUNER_RADIO;
1520 
1521 	tuner->rangelow	= WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
1522 	tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
1523 
1524 	tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
1525 		V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO |
1526 		V4L2_TUNER_CAP_HWSEEK_BOUNDED | V4L2_TUNER_CAP_HWSEEK_WRAP;
1527 
1528 	if (radio->stereo)
1529 		tuner->audmode = V4L2_TUNER_MODE_STEREO;
1530 	else
1531 		tuner->audmode = V4L2_TUNER_MODE_MONO;
1532 
1533 	if (core->mode != WL1273_MODE_RX)
1534 		return 0;
1535 
1536 	if (mutex_lock_interruptible(&core->lock))
1537 		return -EINTR;
1538 
1539 	r = core->read(core, WL1273_STEREO_GET, &val);
1540 	if (r)
1541 		goto out;
1542 
1543 	if (val == 1)
1544 		tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
1545 	else
1546 		tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1547 
1548 	r = core->read(core, WL1273_RSSI_LVL_GET, &val);
1549 	if (r)
1550 		goto out;
1551 
1552 	tuner->signal = (s16) val;
1553 	dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);
1554 
1555 	tuner->afc = 0;
1556 
1557 	r = core->read(core, WL1273_RDS_SYNC_GET, &val);
1558 	if (r)
1559 		goto out;
1560 
1561 	if (val == WL1273_RDS_SYNCHRONIZED)
1562 		tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
1563 out:
1564 	mutex_unlock(&core->lock);
1565 
1566 	return r;
1567 }
1568 
wl1273_fm_vidioc_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * tuner)1569 static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
1570 				    const struct v4l2_tuner *tuner)
1571 {
1572 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1573 	struct wl1273_core *core = radio->core;
1574 	int r = 0;
1575 
1576 	dev_dbg(radio->dev, "%s\n", __func__);
1577 	dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index);
1578 	dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name);
1579 	dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability);
1580 	dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans);
1581 	dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow);
1582 	dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh);
1583 
1584 	if (tuner->index > 0)
1585 		return -EINVAL;
1586 
1587 	if (mutex_lock_interruptible(&core->lock))
1588 		return -EINTR;
1589 
1590 	r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1591 	if (r)
1592 		goto out;
1593 
1594 	if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS)
1595 		r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1596 	else
1597 		r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1598 
1599 	if (r)
1600 		dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r);
1601 
1602 	if (tuner->audmode == V4L2_TUNER_MODE_MONO) {
1603 		r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
1604 		if (r < 0) {
1605 			dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1606 				 __func__, r);
1607 			goto out;
1608 		}
1609 		radio->stereo = false;
1610 	} else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) {
1611 		r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
1612 		if (r < 0) {
1613 			dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1614 				 __func__, r);
1615 			goto out;
1616 		}
1617 		radio->stereo = true;
1618 	} else {
1619 		dev_err(radio->dev, "%s: tuner->audmode: %d\n",
1620 			 __func__, tuner->audmode);
1621 		r = -EINVAL;
1622 		goto out;
1623 	}
1624 
1625 out:
1626 	mutex_unlock(&core->lock);
1627 
1628 	return r;
1629 }
1630 
wl1273_fm_vidioc_g_frequency(struct file * file,void * priv,struct v4l2_frequency * freq)1631 static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
1632 					struct v4l2_frequency *freq)
1633 {
1634 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1635 	struct wl1273_core *core = radio->core;
1636 
1637 	dev_dbg(radio->dev, "%s\n", __func__);
1638 
1639 	if (mutex_lock_interruptible(&core->lock))
1640 		return -EINTR;
1641 
1642 	freq->type = V4L2_TUNER_RADIO;
1643 	freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio));
1644 
1645 	mutex_unlock(&core->lock);
1646 
1647 	return 0;
1648 }
1649 
wl1273_fm_vidioc_s_frequency(struct file * file,void * priv,const struct v4l2_frequency * freq)1650 static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
1651 					const struct v4l2_frequency *freq)
1652 {
1653 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1654 	struct wl1273_core *core = radio->core;
1655 	int r;
1656 
1657 	dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency);
1658 
1659 	if (freq->type != V4L2_TUNER_RADIO) {
1660 		dev_dbg(radio->dev,
1661 			"freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
1662 		return -EINVAL;
1663 	}
1664 
1665 	if (mutex_lock_interruptible(&core->lock))
1666 		return -EINTR;
1667 
1668 	if (core->mode == WL1273_MODE_RX) {
1669 		dev_dbg(radio->dev, "freq: %d\n", freq->frequency);
1670 
1671 		r = wl1273_fm_set_rx_freq(radio,
1672 					  WL1273_INV_FREQ(freq->frequency));
1673 		if (r)
1674 			dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1675 				 ": set frequency failed with %d\n", r);
1676 	} else {
1677 		r = wl1273_fm_set_tx_freq(radio,
1678 					  WL1273_INV_FREQ(freq->frequency));
1679 		if (r)
1680 			dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1681 				 ": set frequency failed with %d\n", r);
1682 	}
1683 
1684 	mutex_unlock(&core->lock);
1685 
1686 	dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
1687 	return r;
1688 }
1689 
1690 #define WL1273_DEFAULT_SEEK_LEVEL	7
1691 
wl1273_fm_vidioc_s_hw_freq_seek(struct file * file,void * priv,const struct v4l2_hw_freq_seek * seek)1692 static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1693 					   const struct v4l2_hw_freq_seek *seek)
1694 {
1695 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1696 	struct wl1273_core *core = radio->core;
1697 	int r;
1698 
1699 	dev_dbg(radio->dev, "%s\n", __func__);
1700 
1701 	if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
1702 		return -EINVAL;
1703 
1704 	if (file->f_flags & O_NONBLOCK)
1705 		return -EWOULDBLOCK;
1706 
1707 	if (mutex_lock_interruptible(&core->lock))
1708 		return -EINTR;
1709 
1710 	r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1711 	if (r)
1712 		goto out;
1713 
1714 	r = wl1273_fm_tx_set_spacing(radio, seek->spacing);
1715 	if (r)
1716 		dev_warn(radio->dev, "HW seek failed: %d\n", r);
1717 
1718 	r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward,
1719 			       WL1273_DEFAULT_SEEK_LEVEL);
1720 	if (r)
1721 		dev_warn(radio->dev, "HW seek failed: %d\n", r);
1722 
1723 out:
1724 	mutex_unlock(&core->lock);
1725 	return r;
1726 }
1727 
wl1273_fm_vidioc_s_modulator(struct file * file,void * priv,const struct v4l2_modulator * modulator)1728 static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
1729 					const struct v4l2_modulator *modulator)
1730 {
1731 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1732 	struct wl1273_core *core = radio->core;
1733 	int r = 0;
1734 
1735 	dev_dbg(radio->dev, "%s\n", __func__);
1736 
1737 	if (modulator->index > 0)
1738 		return -EINVAL;
1739 
1740 	if (mutex_lock_interruptible(&core->lock))
1741 		return -EINTR;
1742 
1743 	r = wl1273_fm_set_mode(radio, WL1273_MODE_TX);
1744 	if (r)
1745 		goto out;
1746 
1747 	if (modulator->txsubchans & V4L2_TUNER_SUB_RDS)
1748 		r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1749 	else
1750 		r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1751 
1752 	if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
1753 		r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
1754 	else
1755 		r = core->write(core, WL1273_MONO_SET,
1756 				WL1273_RX_STEREO);
1757 	if (r < 0)
1758 		dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1759 			 "MONO_SET fails: %d\n", r);
1760 out:
1761 	mutex_unlock(&core->lock);
1762 
1763 	return r;
1764 }
1765 
wl1273_fm_vidioc_g_modulator(struct file * file,void * priv,struct v4l2_modulator * modulator)1766 static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
1767 					struct v4l2_modulator *modulator)
1768 {
1769 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1770 	struct wl1273_core *core = radio->core;
1771 	u16 val;
1772 	int r;
1773 
1774 	dev_dbg(radio->dev, "%s\n", __func__);
1775 
1776 	strlcpy(modulator->name, WL1273_FM_DRIVER_NAME,
1777 		sizeof(modulator->name));
1778 
1779 	modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
1780 	modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
1781 
1782 	modulator->capability =  V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
1783 		V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
1784 
1785 	if (core->mode != WL1273_MODE_TX)
1786 		return 0;
1787 
1788 	if (mutex_lock_interruptible(&core->lock))
1789 		return -EINTR;
1790 
1791 	r = core->read(core, WL1273_MONO_SET, &val);
1792 	if (r)
1793 		goto out;
1794 
1795 	if (val == WL1273_TX_STEREO)
1796 		modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
1797 	else
1798 		modulator->txsubchans = V4L2_TUNER_SUB_MONO;
1799 
1800 	if (radio->rds_on)
1801 		modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
1802 out:
1803 	mutex_unlock(&core->lock);
1804 
1805 	return 0;
1806 }
1807 
wl1273_fm_vidioc_log_status(struct file * file,void * priv)1808 static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
1809 {
1810 	struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1811 	struct wl1273_core *core = radio->core;
1812 	struct device *dev = radio->dev;
1813 	u16 val;
1814 	int r;
1815 
1816 	dev_info(dev, DRIVER_DESC);
1817 
1818 	if (core->mode == WL1273_MODE_OFF) {
1819 		dev_info(dev, "Mode: Off\n");
1820 		return 0;
1821 	}
1822 
1823 	if (core->mode == WL1273_MODE_SUSPENDED) {
1824 		dev_info(dev, "Mode: Suspended\n");
1825 		return 0;
1826 	}
1827 
1828 	r = core->read(core, WL1273_ASIC_ID_GET, &val);
1829 	if (r)
1830 		dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__);
1831 	else
1832 		dev_info(dev, "ASIC_ID: 0x%04x\n", val);
1833 
1834 	r = core->read(core, WL1273_ASIC_VER_GET, &val);
1835 	if (r)
1836 		dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__);
1837 	else
1838 		dev_info(dev, "ASIC Version: 0x%04x\n", val);
1839 
1840 	r = core->read(core, WL1273_FIRM_VER_GET, &val);
1841 	if (r)
1842 		dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__);
1843 	else
1844 		dev_info(dev, "FW version: %d(0x%04x)\n", val, val);
1845 
1846 	r = core->read(core, WL1273_BAND_SET, &val);
1847 	if (r)
1848 		dev_err(dev, "%s: Get BAND fails.\n", __func__);
1849 	else
1850 		dev_info(dev, "BAND: %d\n", val);
1851 
1852 	if (core->mode == WL1273_MODE_TX) {
1853 		r = core->read(core, WL1273_PUPD_SET, &val);
1854 		if (r)
1855 			dev_err(dev, "%s: Get PUPD fails.\n", __func__);
1856 		else
1857 			dev_info(dev, "PUPD: 0x%04x\n", val);
1858 
1859 		r = core->read(core, WL1273_CHANL_SET, &val);
1860 		if (r)
1861 			dev_err(dev, "%s: Get CHANL fails.\n", __func__);
1862 		else
1863 			dev_info(dev, "Tx frequency: %dkHz\n", val*10);
1864 	} else if (core->mode == WL1273_MODE_RX) {
1865 		int bf = radio->rangelow;
1866 
1867 		r = core->read(core, WL1273_FREQ_SET, &val);
1868 		if (r)
1869 			dev_err(dev, "%s: Get FREQ fails.\n", __func__);
1870 		else
1871 			dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50);
1872 
1873 		r = core->read(core, WL1273_MOST_MODE_SET, &val);
1874 		if (r)
1875 			dev_err(dev, "%s: Get MOST_MODE fails.\n",
1876 				__func__);
1877 		else if (val == 0)
1878 			dev_info(dev, "MOST_MODE: Stereo according to blend\n");
1879 		else if (val == 1)
1880 			dev_info(dev, "MOST_MODE: Force mono output\n");
1881 		else
1882 			dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val);
1883 
1884 		r = core->read(core, WL1273_MOST_BLEND_SET, &val);
1885 		if (r)
1886 			dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__);
1887 		else if (val == 0)
1888 			dev_info(dev,
1889 				 "MOST_BLEND: Switched blend & hysteresis.\n");
1890 		else if (val == 1)
1891 			dev_info(dev, "MOST_BLEND: Soft blend.\n");
1892 		else
1893 			dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val);
1894 
1895 		r = core->read(core, WL1273_STEREO_GET, &val);
1896 		if (r)
1897 			dev_err(dev, "%s: Get STEREO fails.\n", __func__);
1898 		else if (val == 0)
1899 			dev_info(dev, "STEREO: Not detected\n");
1900 		else if (val == 1)
1901 			dev_info(dev, "STEREO: Detected\n");
1902 		else
1903 			dev_info(dev, "STEREO: Unexpected value: %d\n", val);
1904 
1905 		r = core->read(core, WL1273_RSSI_LVL_GET, &val);
1906 		if (r)
1907 			dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__);
1908 		else
1909 			dev_info(dev, "RX signal strength: %d\n", (s16) val);
1910 
1911 		r = core->read(core, WL1273_POWER_SET, &val);
1912 		if (r)
1913 			dev_err(dev, "%s: Get POWER fails.\n", __func__);
1914 		else
1915 			dev_info(dev, "POWER: 0x%04x\n", val);
1916 
1917 		r = core->read(core, WL1273_INT_MASK_SET, &val);
1918 		if (r)
1919 			dev_err(dev, "%s: Get INT_MASK fails.\n", __func__);
1920 		else
1921 			dev_info(dev, "INT_MASK: 0x%04x\n", val);
1922 
1923 		r = core->read(core, WL1273_RDS_SYNC_GET, &val);
1924 		if (r)
1925 			dev_err(dev, "%s: Get RDS_SYNC fails.\n",
1926 				__func__);
1927 		else if (val == 0)
1928 			dev_info(dev, "RDS_SYNC: Not synchronized\n");
1929 
1930 		else if (val == 1)
1931 			dev_info(dev, "RDS_SYNC: Synchronized\n");
1932 		else
1933 			dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val);
1934 
1935 		r = core->read(core, WL1273_I2S_MODE_CONFIG_SET, &val);
1936 		if (r)
1937 			dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n",
1938 				__func__);
1939 		else
1940 			dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val);
1941 
1942 		r = core->read(core, WL1273_VOLUME_SET, &val);
1943 		if (r)
1944 			dev_err(dev, "%s: Get VOLUME fails.\n", __func__);
1945 		else
1946 			dev_info(dev, "VOLUME: 0x%04x\n", val);
1947 	}
1948 
1949 	return 0;
1950 }
1951 
wl1273_vdev_release(struct video_device * dev)1952 static void wl1273_vdev_release(struct video_device *dev)
1953 {
1954 }
1955 
1956 static const struct v4l2_ctrl_ops wl1273_ctrl_ops = {
1957 	.s_ctrl = wl1273_fm_vidioc_s_ctrl,
1958 	.g_volatile_ctrl = wl1273_fm_g_volatile_ctrl,
1959 };
1960 
1961 static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
1962 	.vidioc_querycap	= wl1273_fm_vidioc_querycap,
1963 	.vidioc_g_input		= wl1273_fm_vidioc_g_input,
1964 	.vidioc_s_input		= wl1273_fm_vidioc_s_input,
1965 	.vidioc_g_audio		= wl1273_fm_vidioc_g_audio,
1966 	.vidioc_s_audio		= wl1273_fm_vidioc_s_audio,
1967 	.vidioc_g_tuner		= wl1273_fm_vidioc_g_tuner,
1968 	.vidioc_s_tuner		= wl1273_fm_vidioc_s_tuner,
1969 	.vidioc_g_frequency	= wl1273_fm_vidioc_g_frequency,
1970 	.vidioc_s_frequency	= wl1273_fm_vidioc_s_frequency,
1971 	.vidioc_s_hw_freq_seek	= wl1273_fm_vidioc_s_hw_freq_seek,
1972 	.vidioc_g_modulator	= wl1273_fm_vidioc_g_modulator,
1973 	.vidioc_s_modulator	= wl1273_fm_vidioc_s_modulator,
1974 	.vidioc_log_status      = wl1273_fm_vidioc_log_status,
1975 };
1976 
1977 static struct video_device wl1273_viddev_template = {
1978 	.fops			= &wl1273_fops,
1979 	.ioctl_ops		= &wl1273_ioctl_ops,
1980 	.name			= WL1273_FM_DRIVER_NAME,
1981 	.release		= wl1273_vdev_release,
1982 	.vfl_dir		= VFL_DIR_TX,
1983 };
1984 
wl1273_fm_radio_remove(struct platform_device * pdev)1985 static int wl1273_fm_radio_remove(struct platform_device *pdev)
1986 {
1987 	struct wl1273_device *radio = platform_get_drvdata(pdev);
1988 	struct wl1273_core *core = radio->core;
1989 
1990 	dev_info(&pdev->dev, "%s.\n", __func__);
1991 
1992 	free_irq(core->client->irq, radio);
1993 	core->pdata->free_resources();
1994 
1995 	v4l2_ctrl_handler_free(&radio->ctrl_handler);
1996 	video_unregister_device(&radio->videodev);
1997 	v4l2_device_unregister(&radio->v4l2dev);
1998 
1999 	return 0;
2000 }
2001 
wl1273_fm_radio_probe(struct platform_device * pdev)2002 static int wl1273_fm_radio_probe(struct platform_device *pdev)
2003 {
2004 	struct wl1273_core **core = pdev->dev.platform_data;
2005 	struct wl1273_device *radio;
2006 	struct v4l2_ctrl *ctrl;
2007 	int r = 0;
2008 
2009 	pr_debug("%s\n", __func__);
2010 
2011 	if (!core) {
2012 		dev_err(&pdev->dev, "No platform data.\n");
2013 		r = -EINVAL;
2014 		goto pdata_err;
2015 	}
2016 
2017 	radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
2018 	if (!radio) {
2019 		r = -ENOMEM;
2020 		goto pdata_err;
2021 	}
2022 
2023 	/* RDS buffer allocation */
2024 	radio->buf_size = rds_buf * RDS_BLOCK_SIZE;
2025 	radio->buffer = devm_kzalloc(&pdev->dev, radio->buf_size, GFP_KERNEL);
2026 	if (!radio->buffer) {
2027 		pr_err("Cannot allocate memory for RDS buffer.\n");
2028 		r = -ENOMEM;
2029 		goto pdata_err;
2030 	}
2031 
2032 	radio->core = *core;
2033 	radio->irq_flags = WL1273_IRQ_MASK;
2034 	radio->dev = &radio->core->client->dev;
2035 	radio->rds_on = false;
2036 	radio->core->mode = WL1273_MODE_OFF;
2037 	radio->tx_power = 118;
2038 	radio->core->audio_mode = WL1273_AUDIO_ANALOG;
2039 	radio->band = WL1273_BAND_OTHER;
2040 	radio->core->i2s_mode = WL1273_I2S_DEF_MODE;
2041 	radio->core->channel_number = 2;
2042 	radio->core->volume = WL1273_DEFAULT_VOLUME;
2043 	radio->rx_frequency = WL1273_BAND_OTHER_LOW;
2044 	radio->tx_frequency = WL1273_BAND_OTHER_HIGH;
2045 	radio->rangelow = WL1273_BAND_OTHER_LOW;
2046 	radio->rangehigh = WL1273_BAND_OTHER_HIGH;
2047 	radio->stereo = true;
2048 	radio->bus_type = "I2C";
2049 
2050 	if (radio->core->pdata->request_resources) {
2051 		r = radio->core->pdata->request_resources(radio->core->client);
2052 		if (r) {
2053 			dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2054 				": Cannot get platform data\n");
2055 			goto pdata_err;
2056 		}
2057 
2058 		dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq);
2059 
2060 		r = request_threaded_irq(radio->core->client->irq, NULL,
2061 					 wl1273_fm_irq_thread_handler,
2062 					 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
2063 					 "wl1273-fm", radio);
2064 		if (r < 0) {
2065 			dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2066 				": Unable to register IRQ handler: %d\n", r);
2067 			goto err_request_irq;
2068 		}
2069 	} else {
2070 		dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ"
2071 			" not configured");
2072 		r = -EINVAL;
2073 		goto pdata_err;
2074 	}
2075 
2076 	init_completion(&radio->busy);
2077 	init_waitqueue_head(&radio->read_queue);
2078 
2079 	radio->write_buf = devm_kzalloc(&pdev->dev, 256, GFP_KERNEL);
2080 	if (!radio->write_buf) {
2081 		r = -ENOMEM;
2082 		goto write_buf_err;
2083 	}
2084 
2085 	radio->dev = &pdev->dev;
2086 	radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
2087 	radio->rds_users = 0;
2088 
2089 	r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
2090 	if (r) {
2091 		dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
2092 		goto write_buf_err;
2093 	}
2094 
2095 	/* V4L2 configuration */
2096 	radio->videodev = wl1273_viddev_template;
2097 
2098 	radio->videodev.v4l2_dev = &radio->v4l2dev;
2099 
2100 	v4l2_ctrl_handler_init(&radio->ctrl_handler, 6);
2101 
2102 	/* add in ascending ID order */
2103 	v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2104 			  V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1,
2105 			  WL1273_DEFAULT_VOLUME);
2106 
2107 	v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2108 			  V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
2109 
2110 	v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops,
2111 			       V4L2_CID_TUNE_PREEMPHASIS,
2112 			       V4L2_PREEMPHASIS_75_uS, 0x03,
2113 			       V4L2_PREEMPHASIS_50_uS);
2114 
2115 	v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2116 			  V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118);
2117 
2118 	ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2119 				 V4L2_CID_TUNE_ANTENNA_CAPACITOR,
2120 				 0, 255, 1, 255);
2121 	if (ctrl)
2122 		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
2123 
2124 	if (radio->ctrl_handler.error) {
2125 		r = radio->ctrl_handler.error;
2126 		dev_err(&pdev->dev, "Ctrl handler error: %d\n", r);
2127 		goto handler_init_err;
2128 	}
2129 
2130 	video_set_drvdata(&radio->videodev, radio);
2131 	platform_set_drvdata(pdev, radio);
2132 
2133 	/* register video device */
2134 	r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
2135 	if (r) {
2136 		dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
2137 			": Could not register video device\n");
2138 		goto handler_init_err;
2139 	}
2140 
2141 	return 0;
2142 
2143 handler_init_err:
2144 	v4l2_ctrl_handler_free(&radio->ctrl_handler);
2145 	v4l2_device_unregister(&radio->v4l2dev);
2146 write_buf_err:
2147 	free_irq(radio->core->client->irq, radio);
2148 err_request_irq:
2149 	radio->core->pdata->free_resources();
2150 pdata_err:
2151 	return r;
2152 }
2153 
2154 static struct platform_driver wl1273_fm_radio_driver = {
2155 	.probe		= wl1273_fm_radio_probe,
2156 	.remove		= wl1273_fm_radio_remove,
2157 	.driver		= {
2158 		.name	= "wl1273_fm_radio",
2159 	},
2160 };
2161 
2162 module_platform_driver(wl1273_fm_radio_driver);
2163 
2164 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
2165 MODULE_DESCRIPTION(DRIVER_DESC);
2166 MODULE_LICENSE("GPL");
2167 MODULE_ALIAS("platform:wl1273_fm_radio");
2168