Merge branch 'media_fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[cris-mirror.git] / drivers / media / radio / radio-wl1273.c
blob7ecc8e657663edba0c23a6b2cbb1628f1e7dd606
1 /*
2 * Driver for the Texas Instruments WL1273 FM radio.
4 * Copyright (C) 2010 Nokia Corporation
5 * Author: Matti J. Aaltonen <matti.j.aaltonen@nokia.com>
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.
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.
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
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 <media/v4l2-common.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
31 #define DRIVER_DESC "Wl1273 FM Radio"
33 #define WL1273_POWER_SET_OFF 0
34 #define WL1273_POWER_SET_FM BIT(0)
35 #define WL1273_POWER_SET_RDS BIT(1)
36 #define WL1273_POWER_SET_RETENTION BIT(4)
38 #define WL1273_PUPD_SET_OFF 0x00
39 #define WL1273_PUPD_SET_ON 0x01
40 #define WL1273_PUPD_SET_RETENTION 0x10
42 #define WL1273_FREQ(x) (x * 10000 / 625)
43 #define WL1273_INV_FREQ(x) (x * 625 / 10000)
46 * static int radio_nr - The number of the radio device
48 * The default is 0.
50 static int radio_nr;
51 module_param(radio_nr, int, 0);
52 MODULE_PARM_DESC(radio_nr, "The number of the radio device. Default = 0");
54 struct wl1273_device {
55 char *bus_type;
57 u8 forbidden;
58 unsigned int preemphasis;
59 unsigned int spacing;
60 unsigned int tx_power;
61 unsigned int rx_frequency;
62 unsigned int tx_frequency;
63 unsigned int rangelow;
64 unsigned int rangehigh;
65 unsigned int band;
66 bool stereo;
68 /* RDS */
69 unsigned int rds_on;
70 struct delayed_work work;
72 wait_queue_head_t read_queue;
73 struct mutex lock; /* for serializing fm radio operations */
74 struct completion busy;
76 unsigned char *buffer;
77 unsigned int buf_size;
78 unsigned int rd_index;
79 unsigned int wr_index;
81 /* Selected interrupts */
82 u16 irq_flags;
83 u16 irq_received;
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;
95 #define WL1273_IRQ_MASK (WL1273_FR_EVENT | \
96 WL1273_POW_ENB_EVENT)
99 * static unsigned int rds_buf - the number of RDS buffer blocks used.
101 * The default number is 100.
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");
107 static int wl1273_fm_read_reg(struct wl1273_core *core, u8 reg, u16 *value)
109 struct i2c_client *client = core->client;
110 u8 b[2];
111 int r;
113 r = i2c_smbus_read_i2c_block_data(client, reg, sizeof(b), b);
114 if (r != 2) {
115 dev_err(&client->dev, "%s: Read: %d fails.\n", __func__, reg);
116 return -EREMOTEIO;
119 *value = (u16)b[0] << 8 | b[1];
121 return 0;
124 static int wl1273_fm_write_cmd(struct wl1273_core *core, u8 cmd, u16 param)
126 struct i2c_client *client = core->client;
127 u8 buf[] = { (param >> 8) & 0xff, param & 0xff };
128 int r;
130 r = i2c_smbus_write_i2c_block_data(client, cmd, sizeof(buf), buf);
131 if (r) {
132 dev_err(&client->dev, "%s: Cmd: %d fails.\n", __func__, cmd);
133 return r;
136 return 0;
139 static int wl1273_fm_write_data(struct wl1273_core *core, u8 *data, u16 len)
141 struct i2c_client *client = core->client;
142 struct i2c_msg msg;
143 int r;
145 msg.addr = client->addr;
146 msg.flags = 0;
147 msg.buf = data;
148 msg.len = len;
150 r = i2c_transfer(client->adapter, &msg, 1);
151 if (r != 1) {
152 dev_err(&client->dev, "%s: write error.\n", __func__);
153 return -EREMOTEIO;
156 return 0;
159 static int wl1273_fm_write_fw(struct wl1273_core *core,
160 __u8 *fw, int len)
162 struct i2c_client *client = core->client;
163 struct i2c_msg msg;
164 int i, r = 0;
166 msg.addr = client->addr;
167 msg.flags = 0;
169 for (i = 0; i <= len; i++) {
170 msg.len = fw[0];
171 msg.buf = fw + 1;
173 fw += msg.len + 1;
174 dev_dbg(&client->dev, "%s:len[%d]: %d\n", __func__, i, msg.len);
176 r = i2c_transfer(client->adapter, &msg, 1);
177 if (r < 0 && i < len + 1)
178 break;
181 dev_dbg(&client->dev, "%s: i: %d\n", __func__, i);
182 dev_dbg(&client->dev, "%s: len + 1: %d\n", __func__, len + 1);
184 /* Last transfer always fails. */
185 if (i == len || r == 1)
186 r = 0;
188 return r;
192 * wl1273_fm_set_audio() - Set audio mode.
193 * @core: A pointer to the device struct.
194 * @new_mode: The new audio mode.
196 * Audio modes are WL1273_AUDIO_DIGITAL and WL1273_AUDIO_ANALOG.
198 static int wl1273_fm_set_audio(struct wl1273_core *core, unsigned int new_mode)
200 int r = 0;
202 if (core->mode == WL1273_MODE_OFF ||
203 core->mode == WL1273_MODE_SUSPENDED)
204 return -EPERM;
206 if (core->mode == WL1273_MODE_RX && new_mode == WL1273_AUDIO_DIGITAL) {
207 r = wl1273_fm_write_cmd(core, WL1273_PCM_MODE_SET,
208 WL1273_PCM_DEF_MODE);
209 if (r)
210 goto out;
212 r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
213 core->i2s_mode);
214 if (r)
215 goto out;
217 r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
218 WL1273_AUDIO_ENABLE_I2S);
219 if (r)
220 goto out;
222 } else if (core->mode == WL1273_MODE_RX &&
223 new_mode == WL1273_AUDIO_ANALOG) {
224 r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
225 WL1273_AUDIO_ENABLE_ANALOG);
226 if (r)
227 goto out;
229 } else if (core->mode == WL1273_MODE_TX &&
230 new_mode == WL1273_AUDIO_DIGITAL) {
231 r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
232 core->i2s_mode);
233 if (r)
234 goto out;
236 r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
237 WL1273_AUDIO_IO_SET_I2S);
238 if (r)
239 goto out;
241 } else if (core->mode == WL1273_MODE_TX &&
242 new_mode == WL1273_AUDIO_ANALOG) {
243 r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
244 WL1273_AUDIO_IO_SET_ANALOG);
245 if (r)
246 goto out;
249 core->audio_mode = new_mode;
250 out:
251 return r;
255 * wl1273_fm_set_volume() - Set volume.
256 * @core: A pointer to the device struct.
257 * @volume: The new volume value.
259 static int wl1273_fm_set_volume(struct wl1273_core *core, unsigned int volume)
261 u16 val;
262 int r;
264 if (volume > WL1273_MAX_VOLUME)
265 return -EINVAL;
267 if (core->volume == volume)
268 return 0;
270 val = volume;
271 r = wl1273_fm_read_reg(core, WL1273_VOLUME_SET, &val);
272 if (r)
273 return r;
275 core->volume = volume;
276 return 0;
279 #define WL1273_FIFO_HAS_DATA(status) (1 << 5 & status)
280 #define WL1273_RDS_CORRECTABLE_ERROR (1 << 3)
281 #define WL1273_RDS_UNCORRECTABLE_ERROR (1 << 4)
283 static int wl1273_fm_rds(struct wl1273_device *radio)
285 struct wl1273_core *core = radio->core;
286 struct i2c_client *client = core->client;
287 u16 val;
288 u8 b0 = WL1273_RDS_DATA_GET, status;
289 struct v4l2_rds_data rds = { 0, 0, 0 };
290 struct i2c_msg msg[] = {
292 .addr = client->addr,
293 .flags = 0,
294 .buf = &b0,
295 .len = 1,
298 .addr = client->addr,
299 .flags = I2C_M_RD,
300 .buf = (u8 *) &rds,
301 .len = sizeof(rds),
304 int r;
306 if (core->mode != WL1273_MODE_RX)
307 return 0;
309 r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val);
310 if (r)
311 return r;
313 if ((val & 0x01) == 0) {
314 /* RDS decoder not synchronized */
315 return -EAGAIN;
318 /* copy all four RDS blocks to internal buffer */
319 do {
320 r = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
321 if (r != ARRAY_SIZE(msg)) {
322 dev_err(radio->dev, WL1273_FM_DRIVER_NAME
323 ": %s: read_rds error r == %i)\n",
324 __func__, r);
327 status = rds.block;
329 if (!WL1273_FIFO_HAS_DATA(status))
330 break;
332 /* copy bits 0-2 (the block ID) to bits 3-5 */
333 rds.block = V4L2_RDS_BLOCK_MSK & status;
334 rds.block |= rds.block << 3;
336 /* copy the error bits to standard positions */
337 if (WL1273_RDS_UNCORRECTABLE_ERROR & status) {
338 rds.block |= V4L2_RDS_BLOCK_ERROR;
339 rds.block &= ~V4L2_RDS_BLOCK_CORRECTED;
340 } else if (WL1273_RDS_CORRECTABLE_ERROR & status) {
341 rds.block &= ~V4L2_RDS_BLOCK_ERROR;
342 rds.block |= V4L2_RDS_BLOCK_CORRECTED;
345 /* copy RDS block to internal buffer */
346 memcpy(&radio->buffer[radio->wr_index], &rds, RDS_BLOCK_SIZE);
347 radio->wr_index += 3;
349 /* wrap write pointer */
350 if (radio->wr_index >= radio->buf_size)
351 radio->wr_index = 0;
353 /* check for overflow & start over */
354 if (radio->wr_index == radio->rd_index) {
355 dev_dbg(radio->dev, "RDS OVERFLOW");
357 radio->rd_index = 0;
358 radio->wr_index = 0;
359 break;
361 } while (WL1273_FIFO_HAS_DATA(status));
363 /* wake up read queue */
364 if (radio->wr_index != radio->rd_index)
365 wake_up_interruptible(&radio->read_queue);
367 return 0;
370 static irqreturn_t wl1273_fm_irq_thread_handler(int irq, void *dev_id)
372 struct wl1273_device *radio = dev_id;
373 struct wl1273_core *core = radio->core;
374 u16 flags;
375 int r;
377 r = wl1273_fm_read_reg(core, WL1273_FLAG_GET, &flags);
378 if (r)
379 goto out;
381 if (flags & WL1273_BL_EVENT) {
382 radio->irq_received = flags;
383 dev_dbg(radio->dev, "IRQ: BL\n");
386 if (flags & WL1273_RDS_EVENT) {
387 msleep(200);
389 wl1273_fm_rds(radio);
392 if (flags & WL1273_BBLK_EVENT)
393 dev_dbg(radio->dev, "IRQ: BBLK\n");
395 if (flags & WL1273_LSYNC_EVENT)
396 dev_dbg(radio->dev, "IRQ: LSYNC\n");
398 if (flags & WL1273_LEV_EVENT) {
399 u16 level;
401 r = wl1273_fm_read_reg(core, WL1273_RSSI_LVL_GET, &level);
402 if (r)
403 goto out;
405 if (level > 14)
406 dev_dbg(radio->dev, "IRQ: LEV: 0x%x04\n", level);
409 if (flags & WL1273_IFFR_EVENT)
410 dev_dbg(radio->dev, "IRQ: IFFR\n");
412 if (flags & WL1273_PI_EVENT)
413 dev_dbg(radio->dev, "IRQ: PI\n");
415 if (flags & WL1273_PD_EVENT)
416 dev_dbg(radio->dev, "IRQ: PD\n");
418 if (flags & WL1273_STIC_EVENT)
419 dev_dbg(radio->dev, "IRQ: STIC\n");
421 if (flags & WL1273_MAL_EVENT)
422 dev_dbg(radio->dev, "IRQ: MAL\n");
424 if (flags & WL1273_POW_ENB_EVENT) {
425 complete(&radio->busy);
426 dev_dbg(radio->dev, "NOT BUSY\n");
427 dev_dbg(radio->dev, "IRQ: POW_ENB\n");
430 if (flags & WL1273_SCAN_OVER_EVENT)
431 dev_dbg(radio->dev, "IRQ: SCAN_OVER\n");
433 if (flags & WL1273_ERROR_EVENT)
434 dev_dbg(radio->dev, "IRQ: ERROR\n");
436 if (flags & WL1273_FR_EVENT) {
437 u16 freq;
439 dev_dbg(radio->dev, "IRQ: FR:\n");
441 if (core->mode == WL1273_MODE_RX) {
442 r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET,
443 TUNER_MODE_STOP_SEARCH);
444 if (r) {
445 dev_err(radio->dev,
446 "%s: TUNER_MODE_SET fails: %d\n",
447 __func__, r);
448 goto out;
451 r = wl1273_fm_read_reg(core, WL1273_FREQ_SET, &freq);
452 if (r)
453 goto out;
455 if (radio->band == WL1273_BAND_JAPAN)
456 radio->rx_frequency = WL1273_BAND_JAPAN_LOW +
457 freq * 50;
458 else
459 radio->rx_frequency = WL1273_BAND_OTHER_LOW +
460 freq * 50;
462 * The driver works better with this msleep,
463 * the documentation doesn't mention it.
465 usleep_range(10000, 15000);
467 dev_dbg(radio->dev, "%dkHz\n", radio->rx_frequency);
469 } else {
470 r = wl1273_fm_read_reg(core, WL1273_CHANL_SET, &freq);
471 if (r)
472 goto out;
474 dev_dbg(radio->dev, "%dkHz\n", freq);
476 dev_dbg(radio->dev, "%s: NOT BUSY\n", __func__);
479 out:
480 wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET,
481 radio->irq_flags);
482 complete(&radio->busy);
484 return IRQ_HANDLED;
487 static int wl1273_fm_set_tx_freq(struct wl1273_device *radio, unsigned int freq)
489 struct wl1273_core *core = radio->core;
490 int r = 0;
492 if (freq < WL1273_BAND_TX_LOW) {
493 dev_err(radio->dev,
494 "Frequency out of range: %d < %d\n", freq,
495 WL1273_BAND_TX_LOW);
496 return -ERANGE;
499 if (freq > WL1273_BAND_TX_HIGH) {
500 dev_err(radio->dev,
501 "Frequency out of range: %d > %d\n", freq,
502 WL1273_BAND_TX_HIGH);
503 return -ERANGE;
507 * The driver works better with this sleep,
508 * the documentation doesn't mention it.
510 usleep_range(5000, 10000);
512 dev_dbg(radio->dev, "%s: freq: %d kHz\n", __func__, freq);
514 /* Set the current tx channel */
515 r = wl1273_fm_write_cmd(core, WL1273_CHANL_SET, freq / 10);
516 if (r)
517 return r;
519 INIT_COMPLETION(radio->busy);
521 /* wait for the FR IRQ */
522 r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
523 if (!r)
524 return -ETIMEDOUT;
526 dev_dbg(radio->dev, "WL1273_CHANL_SET: %d\n", r);
528 /* Enable the output power */
529 r = wl1273_fm_write_cmd(core, WL1273_POWER_ENB_SET, 1);
530 if (r)
531 return r;
533 INIT_COMPLETION(radio->busy);
535 /* wait for the POWER_ENB IRQ */
536 r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
537 if (!r)
538 return -ETIMEDOUT;
540 radio->tx_frequency = freq;
541 dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %d\n", r);
543 return 0;
546 static int wl1273_fm_set_rx_freq(struct wl1273_device *radio, unsigned int freq)
548 struct wl1273_core *core = radio->core;
549 int r, f;
551 if (freq < radio->rangelow) {
552 dev_err(radio->dev,
553 "Frequency out of range: %d < %d\n", freq,
554 radio->rangelow);
555 r = -ERANGE;
556 goto err;
559 if (freq > radio->rangehigh) {
560 dev_err(radio->dev,
561 "Frequency out of range: %d > %d\n", freq,
562 radio->rangehigh);
563 r = -ERANGE;
564 goto err;
567 dev_dbg(radio->dev, "%s: %dkHz\n", __func__, freq);
569 wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, radio->irq_flags);
571 if (radio->band == WL1273_BAND_JAPAN)
572 f = (freq - WL1273_BAND_JAPAN_LOW) / 50;
573 else
574 f = (freq - WL1273_BAND_OTHER_LOW) / 50;
576 r = wl1273_fm_write_cmd(core, WL1273_FREQ_SET, f);
577 if (r) {
578 dev_err(radio->dev, "FREQ_SET fails\n");
579 goto err;
582 r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET);
583 if (r) {
584 dev_err(radio->dev, "TUNER_MODE_SET fails\n");
585 goto err;
588 INIT_COMPLETION(radio->busy);
590 r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
591 if (!r) {
592 dev_err(radio->dev, "%s: TIMEOUT\n", __func__);
593 return -ETIMEDOUT;
596 radio->rd_index = 0;
597 radio->wr_index = 0;
598 radio->rx_frequency = freq;
599 return 0;
600 err:
601 return r;
604 static int wl1273_fm_get_freq(struct wl1273_device *radio)
606 struct wl1273_core *core = radio->core;
607 unsigned int freq;
608 u16 f;
609 int r;
611 if (core->mode == WL1273_MODE_RX) {
612 r = wl1273_fm_read_reg(core, WL1273_FREQ_SET, &f);
613 if (r)
614 return r;
616 dev_dbg(radio->dev, "Freq get: 0x%04x\n", f);
617 if (radio->band == WL1273_BAND_JAPAN)
618 freq = WL1273_BAND_JAPAN_LOW + 50 * f;
619 else
620 freq = WL1273_BAND_OTHER_LOW + 50 * f;
621 } else {
622 r = wl1273_fm_read_reg(core, WL1273_CHANL_SET, &f);
623 if (r)
624 return r;
626 freq = f * 10;
629 return freq;
633 * wl1273_fm_upload_firmware_patch() - Upload the firmware.
634 * @radio: A pointer to the device struct.
636 * The firmware file consists of arrays of bytes where the first byte
637 * gives the array length. The first byte in the file gives the
638 * number of these arrays.
640 static int wl1273_fm_upload_firmware_patch(struct wl1273_device *radio)
642 struct wl1273_core *core = radio->core;
643 unsigned int packet_num;
644 const struct firmware *fw_p;
645 const char *fw_name = "radio-wl1273-fw.bin";
646 struct device *dev = radio->dev;
647 __u8 *ptr;
648 int r;
650 dev_dbg(dev, "%s:\n", __func__);
653 * Uploading the firmware patch is not always necessary,
654 * so we only print an info message.
656 if (request_firmware(&fw_p, fw_name, dev)) {
657 dev_info(dev, "%s - %s not found\n", __func__, fw_name);
659 return 0;
662 ptr = (__u8 *) fw_p->data;
663 packet_num = ptr[0];
664 dev_dbg(dev, "%s: packets: %d\n", __func__, packet_num);
666 r = wl1273_fm_write_fw(core, ptr + 1, packet_num);
667 if (r) {
668 dev_err(dev, "FW upload error: %d\n", r);
669 goto out;
672 /* ignore possible error here */
673 wl1273_fm_write_cmd(core, WL1273_RESET, 0);
675 dev_dbg(dev, "%s - download OK, r: %d\n", __func__, r);
676 out:
677 release_firmware(fw_p);
678 return r;
681 static int wl1273_fm_stop(struct wl1273_device *radio)
683 struct wl1273_core *core = radio->core;
685 if (core->mode == WL1273_MODE_RX) {
686 int r = wl1273_fm_write_cmd(core, WL1273_POWER_SET,
687 WL1273_POWER_SET_OFF);
688 if (r)
689 dev_err(radio->dev, "%s: POWER_SET fails: %d\n",
690 __func__, r);
691 } else if (core->mode == WL1273_MODE_TX) {
692 int r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
693 WL1273_PUPD_SET_OFF);
694 if (r)
695 dev_err(radio->dev,
696 "%s: PUPD_SET fails: %d\n", __func__, r);
699 if (core->pdata->disable) {
700 core->pdata->disable();
701 dev_dbg(radio->dev, "Back to reset\n");
704 return 0;
707 static int wl1273_fm_start(struct wl1273_device *radio, int new_mode)
709 struct wl1273_core *core = radio->core;
710 struct wl1273_fm_platform_data *pdata = core->pdata;
711 struct device *dev = radio->dev;
712 int r = -EINVAL;
714 if (pdata->enable && core->mode == WL1273_MODE_OFF) {
715 dev_dbg(radio->dev, "Out of reset\n");
717 pdata->enable();
718 msleep(250);
721 if (new_mode == WL1273_MODE_RX) {
722 u16 val = WL1273_POWER_SET_FM;
724 if (radio->rds_on)
725 val |= WL1273_POWER_SET_RDS;
727 /* If this fails try again */
728 r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, val);
729 if (r) {
730 msleep(100);
732 r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, val);
733 if (r) {
734 dev_err(dev, "%s: POWER_SET fails\n", __func__);
735 goto fail;
739 /* rds buffer configuration */
740 radio->wr_index = 0;
741 radio->rd_index = 0;
743 } else if (new_mode == WL1273_MODE_TX) {
744 /* If this fails try again once */
745 r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
746 WL1273_PUPD_SET_ON);
747 if (r) {
748 msleep(100);
749 r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
750 WL1273_PUPD_SET_ON);
751 if (r) {
752 dev_err(dev, "%s: PUPD_SET fails\n", __func__);
753 goto fail;
757 if (radio->rds_on)
758 r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 1);
759 else
760 r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 0);
761 } else {
762 dev_warn(dev, "%s: Illegal mode.\n", __func__);
765 if (core->mode == WL1273_MODE_OFF) {
766 r = wl1273_fm_upload_firmware_patch(radio);
767 if (r)
768 dev_warn(dev, "Firmware upload failed.\n");
771 * Sometimes the chip is in a wrong power state at this point.
772 * So we set the power once again.
774 if (new_mode == WL1273_MODE_RX) {
775 u16 val = WL1273_POWER_SET_FM;
777 if (radio->rds_on)
778 val |= WL1273_POWER_SET_RDS;
780 r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, val);
781 if (r) {
782 dev_err(dev, "%s: POWER_SET fails\n", __func__);
783 goto fail;
785 } else if (new_mode == WL1273_MODE_TX) {
786 r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
787 WL1273_PUPD_SET_ON);
788 if (r) {
789 dev_err(dev, "%s: PUPD_SET fails\n", __func__);
790 goto fail;
795 return 0;
796 fail:
797 if (pdata->disable)
798 pdata->disable();
800 dev_dbg(dev, "%s: return: %d\n", __func__, r);
801 return r;
804 static int wl1273_fm_suspend(struct wl1273_device *radio)
806 struct wl1273_core *core = radio->core;
807 int r = 0;
809 /* Cannot go from OFF to SUSPENDED */
810 if (core->mode == WL1273_MODE_RX)
811 r = wl1273_fm_write_cmd(core, WL1273_POWER_SET,
812 WL1273_POWER_SET_RETENTION);
813 else if (core->mode == WL1273_MODE_TX)
814 r = wl1273_fm_write_cmd(core, WL1273_PUPD_SET,
815 WL1273_PUPD_SET_RETENTION);
816 else
817 r = -EINVAL;
819 if (r) {
820 dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r);
821 goto out;
824 out:
825 return r;
828 static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode)
830 struct wl1273_core *core = radio->core;
831 struct device *dev = radio->dev;
832 int old_mode;
833 int r;
835 dev_dbg(dev, "%s\n", __func__);
836 dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden);
838 old_mode = core->mode;
839 if (mode & radio->forbidden) {
840 r = -EPERM;
841 goto out;
844 switch (mode) {
845 case WL1273_MODE_RX:
846 case WL1273_MODE_TX:
847 r = wl1273_fm_start(radio, mode);
848 if (r) {
849 dev_err(dev, "%s: Cannot start.\n", __func__);
850 wl1273_fm_stop(radio);
851 goto out;
854 core->mode = mode;
855 r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET,
856 radio->irq_flags);
857 if (r) {
858 dev_err(dev, "INT_MASK_SET fails.\n");
859 goto out;
862 /* remember previous settings */
863 if (mode == WL1273_MODE_RX) {
864 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
865 if (r) {
866 dev_err(dev, "set freq fails: %d.\n", r);
867 goto out;
870 r = core->set_volume(core, core->volume);
871 if (r) {
872 dev_err(dev, "set volume fails: %d.\n", r);
873 goto out;
876 dev_dbg(dev, "%s: Set vol: %d.\n", __func__,
877 core->volume);
878 } else {
879 r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency);
880 if (r) {
881 dev_err(dev, "set freq fails: %d.\n", r);
882 goto out;
886 dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__);
888 r = core->set_audio(core, core->audio_mode);
889 if (r)
890 dev_err(dev, "Cannot set audio mode.\n");
891 break;
893 case WL1273_MODE_OFF:
894 r = wl1273_fm_stop(radio);
895 if (r)
896 dev_err(dev, "%s: Off fails: %d\n", __func__, r);
897 else
898 core->mode = WL1273_MODE_OFF;
900 break;
902 case WL1273_MODE_SUSPENDED:
903 r = wl1273_fm_suspend(radio);
904 if (r)
905 dev_err(dev, "%s: Suspend fails: %d\n", __func__, r);
906 else
907 core->mode = WL1273_MODE_SUSPENDED;
909 break;
911 default:
912 dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode);
913 r = -EINVAL;
914 break;
916 out:
917 if (r)
918 core->mode = old_mode;
920 return r;
923 static int wl1273_fm_set_seek(struct wl1273_device *radio,
924 unsigned int wrap_around,
925 unsigned int seek_upward,
926 int level)
928 struct wl1273_core *core = radio->core;
929 int r = 0;
930 unsigned int dir = (seek_upward == 0) ? 0 : 1;
931 unsigned int f;
933 f = radio->rx_frequency;
934 dev_dbg(radio->dev, "rx_frequency: %d\n", f);
936 if (dir && f + radio->spacing <= radio->rangehigh)
937 r = wl1273_fm_set_rx_freq(radio, f + radio->spacing);
938 else if (dir && wrap_around)
939 r = wl1273_fm_set_rx_freq(radio, radio->rangelow);
940 else if (f - radio->spacing >= radio->rangelow)
941 r = wl1273_fm_set_rx_freq(radio, f - radio->spacing);
942 else if (wrap_around)
943 r = wl1273_fm_set_rx_freq(radio, radio->rangehigh);
945 if (r)
946 goto out;
948 if (level < SCHAR_MIN || level > SCHAR_MAX)
949 return -EINVAL;
951 INIT_COMPLETION(radio->busy);
952 dev_dbg(radio->dev, "%s: BUSY\n", __func__);
954 r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, radio->irq_flags);
955 if (r)
956 goto out;
958 dev_dbg(radio->dev, "%s\n", __func__);
960 r = wl1273_fm_write_cmd(core, WL1273_SEARCH_LVL_SET, level);
961 if (r)
962 goto out;
964 r = wl1273_fm_write_cmd(core, WL1273_SEARCH_DIR_SET, dir);
965 if (r)
966 goto out;
968 r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET,
969 TUNER_MODE_AUTO_SEEK);
970 if (r)
971 goto out;
973 wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
974 if (!(radio->irq_received & WL1273_BL_EVENT))
975 goto out;
977 radio->irq_received &= ~WL1273_BL_EVENT;
979 if (!wrap_around)
980 goto out;
982 /* Wrap around */
983 dev_dbg(radio->dev, "Wrap around in HW seek.\n");
985 if (seek_upward)
986 f = radio->rangelow;
987 else
988 f = radio->rangehigh;
990 r = wl1273_fm_set_rx_freq(radio, f);
991 if (r)
992 goto out;
994 INIT_COMPLETION(radio->busy);
995 dev_dbg(radio->dev, "%s: BUSY\n", __func__);
997 r = wl1273_fm_write_cmd(core, WL1273_TUNER_MODE_SET,
998 TUNER_MODE_AUTO_SEEK);
999 if (r)
1000 goto out;
1002 wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
1003 out:
1004 dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r);
1005 return r;
1009 * wl1273_fm_get_tx_ctune() - Get the TX tuning capacitor value.
1010 * @radio: A pointer to the device struct.
1012 static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio)
1014 struct wl1273_core *core = radio->core;
1015 struct device *dev = radio->dev;
1016 u16 val;
1017 int r;
1019 if (core->mode == WL1273_MODE_OFF ||
1020 core->mode == WL1273_MODE_SUSPENDED)
1021 return -EPERM;
1023 r = wl1273_fm_read_reg(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
1024 if (r) {
1025 dev_err(dev, "%s: read error: %d\n", __func__, r);
1026 goto out;
1029 out:
1030 return val;
1034 * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
1035 * @radio: A pointer to the device struct.
1036 * @preemphasis: The new pre-amphasis value.
1038 * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
1039 * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
1041 static int wl1273_fm_set_preemphasis(struct wl1273_device *radio,
1042 unsigned int preemphasis)
1044 struct wl1273_core *core = radio->core;
1045 int r;
1046 u16 em;
1048 if (core->mode == WL1273_MODE_OFF ||
1049 core->mode == WL1273_MODE_SUSPENDED)
1050 return -EPERM;
1052 mutex_lock(&core->lock);
1054 switch (preemphasis) {
1055 case V4L2_PREEMPHASIS_DISABLED:
1056 em = 1;
1057 break;
1058 case V4L2_PREEMPHASIS_50_uS:
1059 em = 0;
1060 break;
1061 case V4L2_PREEMPHASIS_75_uS:
1062 em = 2;
1063 break;
1064 default:
1065 r = -EINVAL;
1066 goto out;
1069 r = wl1273_fm_write_cmd(core, WL1273_PREMPH_SET, em);
1070 if (r)
1071 goto out;
1073 radio->preemphasis = preemphasis;
1075 out:
1076 mutex_unlock(&core->lock);
1077 return r;
1080 static int wl1273_fm_rds_on(struct wl1273_device *radio)
1082 struct wl1273_core *core = radio->core;
1083 int r;
1085 dev_dbg(radio->dev, "%s\n", __func__);
1086 if (radio->rds_on)
1087 return 0;
1089 r = wl1273_fm_write_cmd(core, WL1273_POWER_SET,
1090 WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
1091 if (r)
1092 goto out;
1094 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
1095 if (r)
1096 dev_err(radio->dev, "set freq fails: %d.\n", r);
1097 out:
1098 return r;
1101 static int wl1273_fm_rds_off(struct wl1273_device *radio)
1103 struct wl1273_core *core = radio->core;
1104 int r;
1106 if (!radio->rds_on)
1107 return 0;
1109 radio->irq_flags &= ~WL1273_RDS_EVENT;
1111 r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET, radio->irq_flags);
1112 if (r)
1113 goto out;
1115 /* stop rds reception */
1116 cancel_delayed_work(&radio->work);
1118 /* Service pending read */
1119 wake_up_interruptible(&radio->read_queue);
1121 dev_dbg(radio->dev, "%s\n", __func__);
1123 r = wl1273_fm_write_cmd(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
1124 if (r)
1125 goto out;
1127 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
1128 if (r)
1129 dev_err(radio->dev, "set freq fails: %d.\n", r);
1130 out:
1131 dev_dbg(radio->dev, "%s: exiting...\n", __func__);
1133 return r;
1136 static int wl1273_fm_set_rds(struct wl1273_device *radio, unsigned int new_mode)
1138 int r = 0;
1139 struct wl1273_core *core = radio->core;
1141 if (core->mode == WL1273_MODE_OFF ||
1142 core->mode == WL1273_MODE_SUSPENDED)
1143 return -EPERM;
1145 if (new_mode == WL1273_RDS_RESET) {
1146 r = wl1273_fm_write_cmd(core, WL1273_RDS_CNTRL_SET, 1);
1147 return r;
1150 if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) {
1151 r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 0);
1152 } else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) {
1153 r = wl1273_fm_write_cmd(core, WL1273_RDS_DATA_ENB, 1);
1154 } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) {
1155 r = wl1273_fm_rds_off(radio);
1156 } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) {
1157 r = wl1273_fm_rds_on(radio);
1158 } else {
1159 dev_err(radio->dev, "%s: Unknown mode: %d\n",
1160 __func__, new_mode);
1161 r = -EINVAL;
1164 if (!r)
1165 radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false;
1167 return r;
1170 static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf,
1171 size_t count, loff_t *ppos)
1173 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1174 u16 val;
1175 int r;
1177 dev_dbg(radio->dev, "%s\n", __func__);
1179 if (radio->core->mode != WL1273_MODE_TX)
1180 return count;
1182 if (radio->rds_users == 0) {
1183 dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1184 return 0;
1187 if (mutex_lock_interruptible(&radio->core->lock))
1188 return -EINTR;
1190 * Multiple processes can open the device, but only
1191 * one gets to write to it.
1193 if (radio->owner && radio->owner != file) {
1194 r = -EBUSY;
1195 goto out;
1197 radio->owner = file;
1199 /* Manual Mode */
1200 if (count > 255)
1201 val = 255;
1202 else
1203 val = count;
1205 wl1273_fm_write_cmd(radio->core, WL1273_RDS_CONFIG_DATA_SET, val);
1207 if (copy_from_user(radio->write_buf + 1, buf, val)) {
1208 r = -EFAULT;
1209 goto out;
1212 dev_dbg(radio->dev, "Count: %d\n", val);
1213 dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf);
1215 radio->write_buf[0] = WL1273_RDS_DATA_SET;
1216 wl1273_fm_write_data(radio->core, radio->write_buf, val + 1);
1218 r = val;
1219 out:
1220 mutex_unlock(&radio->core->lock);
1222 return r;
1225 static unsigned int wl1273_fm_fops_poll(struct file *file,
1226 struct poll_table_struct *pts)
1228 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1229 struct wl1273_core *core = radio->core;
1231 if (radio->owner && radio->owner != file)
1232 return -EBUSY;
1234 radio->owner = file;
1236 if (core->mode == WL1273_MODE_RX) {
1237 poll_wait(file, &radio->read_queue, pts);
1239 if (radio->rd_index != radio->wr_index)
1240 return POLLIN | POLLRDNORM;
1242 } else if (core->mode == WL1273_MODE_TX) {
1243 return POLLOUT | POLLWRNORM;
1246 return 0;
1249 static int wl1273_fm_fops_open(struct file *file)
1251 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1252 struct wl1273_core *core = radio->core;
1253 int r = 0;
1255 dev_dbg(radio->dev, "%s\n", __func__);
1257 if (core->mode == WL1273_MODE_RX && radio->rds_on &&
1258 !radio->rds_users) {
1259 dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode);
1261 if (mutex_lock_interruptible(&core->lock))
1262 return -EINTR;
1264 radio->irq_flags |= WL1273_RDS_EVENT;
1266 r = wl1273_fm_write_cmd(core, WL1273_INT_MASK_SET,
1267 radio->irq_flags);
1268 if (r) {
1269 mutex_unlock(&core->lock);
1270 goto out;
1273 radio->rds_users++;
1275 mutex_unlock(&core->lock);
1277 out:
1278 return r;
1281 static int wl1273_fm_fops_release(struct file *file)
1283 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1284 struct wl1273_core *core = radio->core;
1285 int r = 0;
1287 dev_dbg(radio->dev, "%s\n", __func__);
1289 if (radio->rds_users > 0) {
1290 radio->rds_users--;
1291 if (radio->rds_users == 0) {
1292 if (mutex_lock_interruptible(&core->lock))
1293 return -EINTR;
1295 radio->irq_flags &= ~WL1273_RDS_EVENT;
1297 if (core->mode == WL1273_MODE_RX) {
1298 r = wl1273_fm_write_cmd(core,
1299 WL1273_INT_MASK_SET,
1300 radio->irq_flags);
1301 if (r) {
1302 mutex_unlock(&core->lock);
1303 goto out;
1306 mutex_unlock(&core->lock);
1310 if (file == radio->owner)
1311 radio->owner = NULL;
1312 out:
1313 return r;
1316 static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
1317 size_t count, loff_t *ppos)
1319 int r = 0;
1320 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1321 struct wl1273_core *core = radio->core;
1322 unsigned int block_count = 0;
1323 u16 val;
1325 dev_dbg(radio->dev, "%s\n", __func__);
1327 if (radio->core->mode != WL1273_MODE_RX)
1328 return 0;
1330 if (radio->rds_users == 0) {
1331 dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1332 return 0;
1335 if (mutex_lock_interruptible(&core->lock))
1336 return -EINTR;
1339 * Multiple processes can open the device, but only
1340 * one at a time gets read access.
1342 if (radio->owner && radio->owner != file) {
1343 r = -EBUSY;
1344 goto out;
1346 radio->owner = file;
1348 r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val);
1349 if (r) {
1350 dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__);
1351 goto out;
1352 } else if (val == 0) {
1353 dev_info(radio->dev, "RDS_SYNC: Not synchronized\n");
1354 r = -ENODATA;
1355 goto out;
1358 /* block if no new data available */
1359 while (radio->wr_index == radio->rd_index) {
1360 if (file->f_flags & O_NONBLOCK) {
1361 r = -EWOULDBLOCK;
1362 goto out;
1365 dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__);
1366 if (wait_event_interruptible(radio->read_queue,
1367 radio->wr_index !=
1368 radio->rd_index) < 0) {
1369 r = -EINTR;
1370 goto out;
1374 /* calculate block count from byte count */
1375 count /= RDS_BLOCK_SIZE;
1377 /* copy RDS blocks from the internal buffer and to user buffer */
1378 while (block_count < count) {
1379 if (radio->rd_index == radio->wr_index)
1380 break;
1382 /* always transfer complete RDS blocks */
1383 if (copy_to_user(buf, &radio->buffer[radio->rd_index],
1384 RDS_BLOCK_SIZE))
1385 break;
1387 /* increment and wrap the read pointer */
1388 radio->rd_index += RDS_BLOCK_SIZE;
1389 if (radio->rd_index >= radio->buf_size)
1390 radio->rd_index = 0;
1392 /* increment counters */
1393 block_count++;
1394 buf += RDS_BLOCK_SIZE;
1395 r += RDS_BLOCK_SIZE;
1398 out:
1399 dev_dbg(radio->dev, "%s: exit\n", __func__);
1400 mutex_unlock(&core->lock);
1402 return r;
1405 static const struct v4l2_file_operations wl1273_fops = {
1406 .owner = THIS_MODULE,
1407 .read = wl1273_fm_fops_read,
1408 .write = wl1273_fm_fops_write,
1409 .poll = wl1273_fm_fops_poll,
1410 .unlocked_ioctl = video_ioctl2,
1411 .open = wl1273_fm_fops_open,
1412 .release = wl1273_fm_fops_release,
1415 static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
1416 struct v4l2_capability *capability)
1418 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1420 dev_dbg(radio->dev, "%s\n", __func__);
1422 strlcpy(capability->driver, WL1273_FM_DRIVER_NAME,
1423 sizeof(capability->driver));
1424 strlcpy(capability->card, "Texas Instruments Wl1273 FM Radio",
1425 sizeof(capability->card));
1426 strlcpy(capability->bus_info, radio->bus_type,
1427 sizeof(capability->bus_info));
1429 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1430 V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
1431 V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
1432 V4L2_CAP_RDS_OUTPUT;
1434 return 0;
1437 static int wl1273_fm_vidioc_g_input(struct file *file, void *priv,
1438 unsigned int *i)
1440 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1442 dev_dbg(radio->dev, "%s\n", __func__);
1444 *i = 0;
1446 return 0;
1449 static int wl1273_fm_vidioc_s_input(struct file *file, void *priv,
1450 unsigned int i)
1452 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1454 dev_dbg(radio->dev, "%s\n", __func__);
1456 if (i != 0)
1457 return -EINVAL;
1459 return 0;
1463 * wl1273_fm_set_tx_power() - Set the transmission power value.
1464 * @core: A pointer to the device struct.
1465 * @power: The new power value.
1467 static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power)
1469 int r;
1471 if (radio->core->mode == WL1273_MODE_OFF ||
1472 radio->core->mode == WL1273_MODE_SUSPENDED)
1473 return -EPERM;
1475 mutex_lock(&radio->core->lock);
1477 /* Convert the dBuV value to chip presentation */
1478 r = wl1273_fm_write_cmd(radio->core, WL1273_POWER_LEV_SET, 122 - power);
1479 if (r)
1480 goto out;
1482 radio->tx_power = power;
1484 out:
1485 mutex_unlock(&radio->core->lock);
1486 return r;
1489 #define WL1273_SPACING_50kHz 1
1490 #define WL1273_SPACING_100kHz 2
1491 #define WL1273_SPACING_200kHz 4
1493 static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio,
1494 unsigned int spacing)
1496 int r;
1498 if (spacing == 0) {
1499 r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET,
1500 WL1273_SPACING_100kHz);
1501 radio->spacing = 100;
1502 } else if (spacing - 50000 < 25000) {
1503 r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET,
1504 WL1273_SPACING_50kHz);
1505 radio->spacing = 50;
1506 } else if (spacing - 100000 < 50000) {
1507 r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET,
1508 WL1273_SPACING_100kHz);
1509 radio->spacing = 100;
1510 } else {
1511 r = wl1273_fm_write_cmd(radio->core, WL1273_SCAN_SPACING_SET,
1512 WL1273_SPACING_200kHz);
1513 radio->spacing = 200;
1516 return r;
1519 static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1521 struct wl1273_device *radio = ctrl->priv;
1522 struct wl1273_core *core = radio->core;
1524 dev_dbg(radio->dev, "%s\n", __func__);
1526 if (mutex_lock_interruptible(&core->lock))
1527 return -EINTR;
1529 switch (ctrl->id) {
1530 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1531 ctrl->val = wl1273_fm_get_tx_ctune(radio);
1532 break;
1534 default:
1535 dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
1536 __func__, ctrl->id);
1537 break;
1540 mutex_unlock(&core->lock);
1542 return 0;
1545 #define WL1273_MUTE_SOFT_ENABLE (1 << 0)
1546 #define WL1273_MUTE_AC (1 << 1)
1547 #define WL1273_MUTE_HARD_LEFT (1 << 2)
1548 #define WL1273_MUTE_HARD_RIGHT (1 << 3)
1549 #define WL1273_MUTE_SOFT_FORCE (1 << 4)
1551 static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl)
1553 return container_of(ctrl->handler, struct wl1273_device, ctrl_handler);
1556 static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl)
1558 struct wl1273_device *radio = to_radio(ctrl);
1559 struct wl1273_core *core = radio->core;
1560 int r = 0;
1562 dev_dbg(radio->dev, "%s\n", __func__);
1564 switch (ctrl->id) {
1565 case V4L2_CID_AUDIO_MUTE:
1566 if (mutex_lock_interruptible(&core->lock))
1567 return -EINTR;
1569 if (core->mode == WL1273_MODE_RX && ctrl->val)
1570 r = wl1273_fm_write_cmd(core,
1571 WL1273_MUTE_STATUS_SET,
1572 WL1273_MUTE_HARD_LEFT |
1573 WL1273_MUTE_HARD_RIGHT);
1574 else if (core->mode == WL1273_MODE_RX)
1575 r = wl1273_fm_write_cmd(core,
1576 WL1273_MUTE_STATUS_SET, 0x0);
1577 else if (core->mode == WL1273_MODE_TX && ctrl->val)
1578 r = wl1273_fm_write_cmd(core, WL1273_MUTE, 1);
1579 else if (core->mode == WL1273_MODE_TX)
1580 r = wl1273_fm_write_cmd(core, WL1273_MUTE, 0);
1582 mutex_unlock(&core->lock);
1583 break;
1585 case V4L2_CID_AUDIO_VOLUME:
1586 if (ctrl->val == 0)
1587 r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF);
1588 else
1589 r = core->set_volume(core, core->volume);
1590 break;
1592 case V4L2_CID_TUNE_PREEMPHASIS:
1593 r = wl1273_fm_set_preemphasis(radio, ctrl->val);
1594 break;
1596 case V4L2_CID_TUNE_POWER_LEVEL:
1597 r = wl1273_fm_set_tx_power(radio, ctrl->val);
1598 break;
1600 default:
1601 dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
1602 __func__, ctrl->id);
1603 break;
1606 dev_dbg(radio->dev, "%s\n", __func__);
1607 return r;
1610 static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv,
1611 struct v4l2_audio *audio)
1613 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1615 dev_dbg(radio->dev, "%s\n", __func__);
1617 if (audio->index > 1)
1618 return -EINVAL;
1620 strlcpy(audio->name, "Radio", sizeof(audio->name));
1621 audio->capability = V4L2_AUDCAP_STEREO;
1623 return 0;
1626 static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
1627 struct v4l2_audio *audio)
1629 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1631 dev_dbg(radio->dev, "%s\n", __func__);
1633 if (audio->index != 0)
1634 return -EINVAL;
1636 return 0;
1639 #define WL1273_RDS_NOT_SYNCHRONIZED 0
1640 #define WL1273_RDS_SYNCHRONIZED 1
1642 static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv,
1643 struct v4l2_tuner *tuner)
1645 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1646 struct wl1273_core *core = radio->core;
1647 u16 val;
1648 int r;
1650 dev_dbg(radio->dev, "%s\n", __func__);
1652 if (tuner->index > 0)
1653 return -EINVAL;
1655 strlcpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name));
1656 tuner->type = V4L2_TUNER_RADIO;
1658 tuner->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
1659 tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
1661 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
1662 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
1664 if (radio->stereo)
1665 tuner->audmode = V4L2_TUNER_MODE_STEREO;
1666 else
1667 tuner->audmode = V4L2_TUNER_MODE_MONO;
1669 if (core->mode != WL1273_MODE_RX)
1670 return 0;
1672 if (mutex_lock_interruptible(&core->lock))
1673 return -EINTR;
1675 r = wl1273_fm_read_reg(core, WL1273_STEREO_GET, &val);
1676 if (r)
1677 goto out;
1679 if (val == 1)
1680 tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
1681 else
1682 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1684 r = wl1273_fm_read_reg(core, WL1273_RSSI_LVL_GET, &val);
1685 if (r)
1686 goto out;
1688 tuner->signal = (s16) val;
1689 dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);
1691 tuner->afc = 0;
1693 r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val);
1694 if (r)
1695 goto out;
1697 if (val == WL1273_RDS_SYNCHRONIZED)
1698 tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
1699 out:
1700 mutex_unlock(&core->lock);
1702 return r;
1705 static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
1706 struct v4l2_tuner *tuner)
1708 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1709 struct wl1273_core *core = radio->core;
1710 int r = 0;
1712 dev_dbg(radio->dev, "%s\n", __func__);
1713 dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index);
1714 dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name);
1715 dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability);
1716 dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans);
1717 dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow);
1718 dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh);
1720 if (tuner->index > 0)
1721 return -EINVAL;
1723 if (mutex_lock_interruptible(&core->lock))
1724 return -EINTR;
1726 r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1727 if (r)
1728 goto out;
1730 if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS)
1731 r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1732 else
1733 r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1735 if (r)
1736 dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r);
1738 if (tuner->audmode == V4L2_TUNER_MODE_MONO) {
1739 r = wl1273_fm_write_cmd(core, WL1273_MOST_MODE_SET,
1740 WL1273_RX_MONO);
1741 if (r < 0) {
1742 dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1743 __func__, r);
1744 goto out;
1746 radio->stereo = false;
1747 } else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) {
1748 r = wl1273_fm_write_cmd(core, WL1273_MOST_MODE_SET,
1749 WL1273_RX_STEREO);
1750 if (r < 0) {
1751 dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1752 __func__, r);
1753 goto out;
1755 radio->stereo = true;
1756 } else {
1757 dev_err(radio->dev, "%s: tuner->audmode: %d\n",
1758 __func__, tuner->audmode);
1759 r = -EINVAL;
1760 goto out;
1763 out:
1764 mutex_unlock(&core->lock);
1766 return r;
1769 static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
1770 struct v4l2_frequency *freq)
1772 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1773 struct wl1273_core *core = radio->core;
1775 dev_dbg(radio->dev, "%s\n", __func__);
1777 if (mutex_lock_interruptible(&core->lock))
1778 return -EINTR;
1780 freq->type = V4L2_TUNER_RADIO;
1781 freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio));
1783 mutex_unlock(&core->lock);
1785 return 0;
1788 static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
1789 struct v4l2_frequency *freq)
1791 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1792 struct wl1273_core *core = radio->core;
1793 int r;
1795 dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency);
1797 if (freq->type != V4L2_TUNER_RADIO) {
1798 dev_dbg(radio->dev,
1799 "freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
1800 return -EINVAL;
1803 if (mutex_lock_interruptible(&core->lock))
1804 return -EINTR;
1806 if (core->mode == WL1273_MODE_RX) {
1807 dev_dbg(radio->dev, "freq: %d\n", freq->frequency);
1809 r = wl1273_fm_set_rx_freq(radio,
1810 WL1273_INV_FREQ(freq->frequency));
1811 if (r)
1812 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1813 ": set frequency failed with %d\n", r);
1814 } else {
1815 r = wl1273_fm_set_tx_freq(radio,
1816 WL1273_INV_FREQ(freq->frequency));
1817 if (r)
1818 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1819 ": set frequency failed with %d\n", r);
1822 mutex_unlock(&core->lock);
1824 dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
1825 return r;
1828 #define WL1273_DEFAULT_SEEK_LEVEL 7
1830 static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1831 struct v4l2_hw_freq_seek *seek)
1833 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1834 struct wl1273_core *core = radio->core;
1835 int r;
1837 dev_dbg(radio->dev, "%s\n", __func__);
1839 if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
1840 return -EINVAL;
1842 if (mutex_lock_interruptible(&core->lock))
1843 return -EINTR;
1845 r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1846 if (r)
1847 goto out;
1849 r = wl1273_fm_tx_set_spacing(radio, seek->spacing);
1850 if (r)
1851 dev_warn(radio->dev, "HW seek failed: %d\n", r);
1853 r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward,
1854 WL1273_DEFAULT_SEEK_LEVEL);
1855 if (r)
1856 dev_warn(radio->dev, "HW seek failed: %d\n", r);
1858 out:
1859 mutex_unlock(&core->lock);
1860 return r;
1863 static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
1864 struct v4l2_modulator *modulator)
1866 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1867 struct wl1273_core *core = radio->core;
1868 int r = 0;
1870 dev_dbg(radio->dev, "%s\n", __func__);
1872 if (modulator->index > 0)
1873 return -EINVAL;
1875 if (mutex_lock_interruptible(&core->lock))
1876 return -EINTR;
1878 r = wl1273_fm_set_mode(radio, WL1273_MODE_TX);
1879 if (r)
1880 goto out;
1882 if (modulator->txsubchans & V4L2_TUNER_SUB_RDS)
1883 r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1884 else
1885 r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1887 if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
1888 r = wl1273_fm_write_cmd(core, WL1273_MONO_SET, WL1273_TX_MONO);
1889 else
1890 r = wl1273_fm_write_cmd(core, WL1273_MONO_SET,
1891 WL1273_RX_STEREO);
1892 if (r < 0)
1893 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1894 "MONO_SET fails: %d\n", r);
1895 out:
1896 mutex_unlock(&core->lock);
1898 return r;
1901 static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
1902 struct v4l2_modulator *modulator)
1904 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1905 struct wl1273_core *core = radio->core;
1906 u16 val;
1907 int r;
1909 dev_dbg(radio->dev, "%s\n", __func__);
1911 strlcpy(modulator->name, WL1273_FM_DRIVER_NAME,
1912 sizeof(modulator->name));
1914 modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
1915 modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
1917 modulator->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
1918 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
1920 if (core->mode != WL1273_MODE_TX)
1921 return 0;
1923 if (mutex_lock_interruptible(&core->lock))
1924 return -EINTR;
1926 r = wl1273_fm_read_reg(core, WL1273_MONO_SET, &val);
1927 if (r)
1928 goto out;
1930 if (val == WL1273_TX_STEREO)
1931 modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
1932 else
1933 modulator->txsubchans = V4L2_TUNER_SUB_MONO;
1935 if (radio->rds_on)
1936 modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
1937 out:
1938 mutex_unlock(&core->lock);
1940 return 0;
1943 static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
1945 struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1946 struct wl1273_core *core = radio->core;
1947 struct device *dev = radio->dev;
1948 u16 val;
1949 int r;
1951 dev_info(dev, DRIVER_DESC);
1953 if (core->mode == WL1273_MODE_OFF) {
1954 dev_info(dev, "Mode: Off\n");
1955 return 0;
1958 if (core->mode == WL1273_MODE_SUSPENDED) {
1959 dev_info(dev, "Mode: Suspended\n");
1960 return 0;
1963 r = wl1273_fm_read_reg(core, WL1273_ASIC_ID_GET, &val);
1964 if (r)
1965 dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__);
1966 else
1967 dev_info(dev, "ASIC_ID: 0x%04x\n", val);
1969 r = wl1273_fm_read_reg(core, WL1273_ASIC_VER_GET, &val);
1970 if (r)
1971 dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__);
1972 else
1973 dev_info(dev, "ASIC Version: 0x%04x\n", val);
1975 r = wl1273_fm_read_reg(core, WL1273_FIRM_VER_GET, &val);
1976 if (r)
1977 dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__);
1978 else
1979 dev_info(dev, "FW version: %d(0x%04x)\n", val, val);
1981 r = wl1273_fm_read_reg(core, WL1273_BAND_SET, &val);
1982 if (r)
1983 dev_err(dev, "%s: Get BAND fails.\n", __func__);
1984 else
1985 dev_info(dev, "BAND: %d\n", val);
1987 if (core->mode == WL1273_MODE_TX) {
1988 r = wl1273_fm_read_reg(core, WL1273_PUPD_SET, &val);
1989 if (r)
1990 dev_err(dev, "%s: Get PUPD fails.\n", __func__);
1991 else
1992 dev_info(dev, "PUPD: 0x%04x\n", val);
1994 r = wl1273_fm_read_reg(core, WL1273_CHANL_SET, &val);
1995 if (r)
1996 dev_err(dev, "%s: Get CHANL fails.\n", __func__);
1997 else
1998 dev_info(dev, "Tx frequency: %dkHz\n", val*10);
1999 } else if (core->mode == WL1273_MODE_RX) {
2000 int bf = radio->rangelow;
2002 r = wl1273_fm_read_reg(core, WL1273_FREQ_SET, &val);
2003 if (r)
2004 dev_err(dev, "%s: Get FREQ fails.\n", __func__);
2005 else
2006 dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50);
2008 r = wl1273_fm_read_reg(core, WL1273_MOST_MODE_SET, &val);
2009 if (r)
2010 dev_err(dev, "%s: Get MOST_MODE fails.\n",
2011 __func__);
2012 else if (val == 0)
2013 dev_info(dev, "MOST_MODE: Stereo according to blend\n");
2014 else if (val == 1)
2015 dev_info(dev, "MOST_MODE: Force mono output\n");
2016 else
2017 dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val);
2019 r = wl1273_fm_read_reg(core, WL1273_MOST_BLEND_SET, &val);
2020 if (r)
2021 dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__);
2022 else if (val == 0)
2023 dev_info(dev,
2024 "MOST_BLEND: Switched blend & hysteresis.\n");
2025 else if (val == 1)
2026 dev_info(dev, "MOST_BLEND: Soft blend.\n");
2027 else
2028 dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val);
2030 r = wl1273_fm_read_reg(core, WL1273_STEREO_GET, &val);
2031 if (r)
2032 dev_err(dev, "%s: Get STEREO fails.\n", __func__);
2033 else if (val == 0)
2034 dev_info(dev, "STEREO: Not detected\n");
2035 else if (val == 1)
2036 dev_info(dev, "STEREO: Detected\n");
2037 else
2038 dev_info(dev, "STEREO: Unexpected value: %d\n", val);
2040 r = wl1273_fm_read_reg(core, WL1273_RSSI_LVL_GET, &val);
2041 if (r)
2042 dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__);
2043 else
2044 dev_info(dev, "RX signal strength: %d\n", (s16) val);
2046 r = wl1273_fm_read_reg(core, WL1273_POWER_SET, &val);
2047 if (r)
2048 dev_err(dev, "%s: Get POWER fails.\n", __func__);
2049 else
2050 dev_info(dev, "POWER: 0x%04x\n", val);
2052 r = wl1273_fm_read_reg(core, WL1273_INT_MASK_SET, &val);
2053 if (r)
2054 dev_err(dev, "%s: Get INT_MASK fails.\n", __func__);
2055 else
2056 dev_info(dev, "INT_MASK: 0x%04x\n", val);
2058 r = wl1273_fm_read_reg(core, WL1273_RDS_SYNC_GET, &val);
2059 if (r)
2060 dev_err(dev, "%s: Get RDS_SYNC fails.\n",
2061 __func__);
2062 else if (val == 0)
2063 dev_info(dev, "RDS_SYNC: Not synchronized\n");
2065 else if (val == 1)
2066 dev_info(dev, "RDS_SYNC: Synchronized\n");
2067 else
2068 dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val);
2070 r = wl1273_fm_read_reg(core, WL1273_I2S_MODE_CONFIG_SET, &val);
2071 if (r)
2072 dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n",
2073 __func__);
2074 else
2075 dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val);
2077 r = wl1273_fm_read_reg(core, WL1273_VOLUME_SET, &val);
2078 if (r)
2079 dev_err(dev, "%s: Get VOLUME fails.\n", __func__);
2080 else
2081 dev_info(dev, "VOLUME: 0x%04x\n", val);
2084 return 0;
2087 static void wl1273_vdev_release(struct video_device *dev)
2091 static const struct v4l2_ctrl_ops wl1273_ctrl_ops = {
2092 .s_ctrl = wl1273_fm_vidioc_s_ctrl,
2093 .g_volatile_ctrl = wl1273_fm_g_volatile_ctrl,
2096 static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
2097 .vidioc_querycap = wl1273_fm_vidioc_querycap,
2098 .vidioc_g_input = wl1273_fm_vidioc_g_input,
2099 .vidioc_s_input = wl1273_fm_vidioc_s_input,
2100 .vidioc_g_audio = wl1273_fm_vidioc_g_audio,
2101 .vidioc_s_audio = wl1273_fm_vidioc_s_audio,
2102 .vidioc_g_tuner = wl1273_fm_vidioc_g_tuner,
2103 .vidioc_s_tuner = wl1273_fm_vidioc_s_tuner,
2104 .vidioc_g_frequency = wl1273_fm_vidioc_g_frequency,
2105 .vidioc_s_frequency = wl1273_fm_vidioc_s_frequency,
2106 .vidioc_s_hw_freq_seek = wl1273_fm_vidioc_s_hw_freq_seek,
2107 .vidioc_g_modulator = wl1273_fm_vidioc_g_modulator,
2108 .vidioc_s_modulator = wl1273_fm_vidioc_s_modulator,
2109 .vidioc_log_status = wl1273_fm_vidioc_log_status,
2112 static struct video_device wl1273_viddev_template = {
2113 .fops = &wl1273_fops,
2114 .ioctl_ops = &wl1273_ioctl_ops,
2115 .name = WL1273_FM_DRIVER_NAME,
2116 .release = wl1273_vdev_release,
2119 static int wl1273_fm_radio_remove(struct platform_device *pdev)
2121 struct wl1273_device *radio = platform_get_drvdata(pdev);
2122 struct wl1273_core *core = radio->core;
2124 dev_info(&pdev->dev, "%s.\n", __func__);
2126 free_irq(core->client->irq, radio);
2127 core->pdata->free_resources();
2129 v4l2_ctrl_handler_free(&radio->ctrl_handler);
2130 video_unregister_device(&radio->videodev);
2131 v4l2_device_unregister(&radio->v4l2dev);
2132 kfree(radio->buffer);
2133 kfree(radio->write_buf);
2134 kfree(radio);
2136 return 0;
2139 static int __devinit wl1273_fm_radio_probe(struct platform_device *pdev)
2141 struct wl1273_core **core = pdev->dev.platform_data;
2142 struct wl1273_device *radio;
2143 struct v4l2_ctrl *ctrl;
2144 int r = 0;
2146 pr_debug("%s\n", __func__);
2148 if (!core) {
2149 dev_err(&pdev->dev, "No platform data.\n");
2150 r = -EINVAL;
2151 goto pdata_err;
2154 radio = kzalloc(sizeof(*radio), GFP_KERNEL);
2155 if (!radio) {
2156 r = -ENOMEM;
2157 goto pdata_err;
2160 /* RDS buffer allocation */
2161 radio->buf_size = rds_buf * RDS_BLOCK_SIZE;
2162 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
2163 if (!radio->buffer) {
2164 pr_err("Cannot allocate memory for RDS buffer.\n");
2165 r = -ENOMEM;
2166 goto err_kmalloc;
2169 radio->core = *core;
2170 radio->irq_flags = WL1273_IRQ_MASK;
2171 radio->dev = &radio->core->client->dev;
2172 radio->rds_on = false;
2173 radio->core->mode = WL1273_MODE_OFF;
2174 radio->tx_power = 118;
2175 radio->core->audio_mode = WL1273_AUDIO_ANALOG;
2176 radio->band = WL1273_BAND_OTHER;
2177 radio->core->i2s_mode = WL1273_I2S_DEF_MODE;
2178 radio->core->channel_number = 2;
2179 radio->core->volume = WL1273_DEFAULT_VOLUME;
2180 radio->rx_frequency = WL1273_BAND_OTHER_LOW;
2181 radio->tx_frequency = WL1273_BAND_OTHER_HIGH;
2182 radio->rangelow = WL1273_BAND_OTHER_LOW;
2183 radio->rangehigh = WL1273_BAND_OTHER_HIGH;
2184 radio->stereo = true;
2185 radio->bus_type = "I2C";
2187 radio->core->write = wl1273_fm_write_cmd;
2188 radio->core->set_audio = wl1273_fm_set_audio;
2189 radio->core->set_volume = wl1273_fm_set_volume;
2191 if (radio->core->pdata->request_resources) {
2192 r = radio->core->pdata->request_resources(radio->core->client);
2193 if (r) {
2194 dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2195 ": Cannot get platform data\n");
2196 goto err_resources;
2199 dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq);
2201 r = request_threaded_irq(radio->core->client->irq, NULL,
2202 wl1273_fm_irq_thread_handler,
2203 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
2204 "wl1273-fm", radio);
2205 if (r < 0) {
2206 dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2207 ": Unable to register IRQ handler: %d\n", r);
2208 goto err_request_irq;
2210 } else {
2211 dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ"
2212 " not configured");
2213 r = -EINVAL;
2214 goto err_resources;
2217 init_completion(&radio->busy);
2218 init_waitqueue_head(&radio->read_queue);
2220 radio->write_buf = kmalloc(256, GFP_KERNEL);
2221 if (!radio->write_buf) {
2222 r = -ENOMEM;
2223 goto write_buf_err;
2226 radio->dev = &pdev->dev;
2227 radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
2228 radio->rds_users = 0;
2230 r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
2231 if (r) {
2232 dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
2233 goto device_register_err;
2236 /* V4L2 configuration */
2237 memcpy(&radio->videodev, &wl1273_viddev_template,
2238 sizeof(wl1273_viddev_template));
2240 radio->videodev.v4l2_dev = &radio->v4l2dev;
2242 v4l2_ctrl_handler_init(&radio->ctrl_handler, 6);
2244 /* add in ascending ID order */
2245 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2246 V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1,
2247 WL1273_DEFAULT_VOLUME);
2249 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2250 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
2252 v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops,
2253 V4L2_CID_TUNE_PREEMPHASIS,
2254 V4L2_PREEMPHASIS_75_uS, 0x03,
2255 V4L2_PREEMPHASIS_50_uS);
2257 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2258 V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118);
2260 ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2261 V4L2_CID_TUNE_ANTENNA_CAPACITOR,
2262 0, 255, 1, 255);
2263 if (ctrl)
2264 ctrl->is_volatile = 1;
2266 if (radio->ctrl_handler.error) {
2267 r = radio->ctrl_handler.error;
2268 dev_err(&pdev->dev, "Ctrl handler error: %d\n", r);
2269 goto handler_init_err;
2272 video_set_drvdata(&radio->videodev, radio);
2273 platform_set_drvdata(pdev, radio);
2275 /* register video device */
2276 r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
2277 if (r) {
2278 dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
2279 ": Could not register video device\n");
2280 goto handler_init_err;
2283 return 0;
2285 handler_init_err:
2286 v4l2_ctrl_handler_free(&radio->ctrl_handler);
2287 v4l2_device_unregister(&radio->v4l2dev);
2288 device_register_err:
2289 kfree(radio->write_buf);
2290 write_buf_err:
2291 free_irq(radio->core->client->irq, radio);
2292 err_request_irq:
2293 radio->core->pdata->free_resources();
2294 err_resources:
2295 kfree(radio->buffer);
2296 err_kmalloc:
2297 kfree(radio);
2298 pdata_err:
2299 return r;
2302 MODULE_ALIAS("platform:wl1273_fm_radio");
2304 static struct platform_driver wl1273_fm_radio_driver = {
2305 .probe = wl1273_fm_radio_probe,
2306 .remove = __devexit_p(wl1273_fm_radio_remove),
2307 .driver = {
2308 .name = "wl1273_fm_radio",
2309 .owner = THIS_MODULE,
2313 static int __init wl1273_fm_module_init(void)
2315 pr_info("%s\n", __func__);
2316 return platform_driver_register(&wl1273_fm_radio_driver);
2318 module_init(wl1273_fm_module_init);
2320 static void __exit wl1273_fm_module_exit(void)
2322 flush_scheduled_work();
2323 platform_driver_unregister(&wl1273_fm_radio_driver);
2324 pr_info(DRIVER_DESC ", Exiting.\n");
2326 module_exit(wl1273_fm_module_exit);
2328 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
2329 MODULE_DESCRIPTION(DRIVER_DESC);
2330 MODULE_LICENSE("GPL");