2 * drivers/staging/media/radio-bcm2048.c
4 * Driver for I2C Broadcom BCM2048 FM Radio Receiver:
6 * Copyright (C) Nokia Corporation
7 * Contact: Eero Nurkkala <ext-eero.nurkkala@nokia.com>
9 * Copyright (C) Nils Faerber <nils.faerber@kernelconcepts.de>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * version 2 as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
24 * Eero Nurkkala <ext-eero.nurkkala@nokia.com>
26 * - Initial implementation
27 * 2010-02-21 Nils Faerber <nils.faerber@kernelconcepts.de>
29 * - Add support for interrupt driven rds data reading
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/version.h>
36 #include <linux/interrupt.h>
37 #include <linux/sysfs.h>
38 #include <linux/completion.h>
39 #include <linux/delay.h>
40 #include <linux/i2c.h>
41 #include <linux/videodev2.h>
42 #include <linux/mutex.h>
43 #include <linux/slab.h>
44 #include <media/v4l2-common.h>
45 #include <media/v4l2-ioctl.h>
46 #include "radio-bcm2048.h"
48 /* driver definitions */
49 #define BCM2048_DRIVER_AUTHOR "Eero Nurkkala <ext-eero.nurkkala@nokia.com>"
50 #define BCM2048_DRIVER_NAME BCM2048_NAME
51 #define BCM2048_DRIVER_VERSION KERNEL_VERSION(0, 0, 1)
52 #define BCM2048_DRIVER_CARD "Broadcom bcm2048 FM Radio Receiver"
53 #define BCM2048_DRIVER_DESC "I2C driver for BCM2048 FM Radio Receiver"
55 /* I2C Control Registers */
56 #define BCM2048_I2C_FM_RDS_SYSTEM 0x00
57 #define BCM2048_I2C_FM_CTRL 0x01
58 #define BCM2048_I2C_RDS_CTRL0 0x02
59 #define BCM2048_I2C_RDS_CTRL1 0x03
60 #define BCM2048_I2C_FM_AUDIO_PAUSE 0x04
61 #define BCM2048_I2C_FM_AUDIO_CTRL0 0x05
62 #define BCM2048_I2C_FM_AUDIO_CTRL1 0x06
63 #define BCM2048_I2C_FM_SEARCH_CTRL0 0x07
64 #define BCM2048_I2C_FM_SEARCH_CTRL1 0x08
65 #define BCM2048_I2C_FM_SEARCH_TUNE_MODE 0x09
66 #define BCM2048_I2C_FM_FREQ0 0x0a
67 #define BCM2048_I2C_FM_FREQ1 0x0b
68 #define BCM2048_I2C_FM_AF_FREQ0 0x0c
69 #define BCM2048_I2C_FM_AF_FREQ1 0x0d
70 #define BCM2048_I2C_FM_CARRIER 0x0e
71 #define BCM2048_I2C_FM_RSSI 0x0f
72 #define BCM2048_I2C_FM_RDS_MASK0 0x10
73 #define BCM2048_I2C_FM_RDS_MASK1 0x11
74 #define BCM2048_I2C_FM_RDS_FLAG0 0x12
75 #define BCM2048_I2C_FM_RDS_FLAG1 0x13
76 #define BCM2048_I2C_RDS_WLINE 0x14
77 #define BCM2048_I2C_RDS_BLKB_MATCH0 0x16
78 #define BCM2048_I2C_RDS_BLKB_MATCH1 0x17
79 #define BCM2048_I2C_RDS_BLKB_MASK0 0x18
80 #define BCM2048_I2C_RDS_BLKB_MASK1 0x19
81 #define BCM2048_I2C_RDS_PI_MATCH0 0x1a
82 #define BCM2048_I2C_RDS_PI_MATCH1 0x1b
83 #define BCM2048_I2C_RDS_PI_MASK0 0x1c
84 #define BCM2048_I2C_RDS_PI_MASK1 0x1d
85 #define BCM2048_I2C_SPARE1 0x20
86 #define BCM2048_I2C_SPARE2 0x21
87 #define BCM2048_I2C_FM_RDS_REV 0x28
88 #define BCM2048_I2C_SLAVE_CONFIGURATION 0x29
89 #define BCM2048_I2C_RDS_DATA 0x80
90 #define BCM2048_I2C_FM_BEST_TUNE_MODE 0x90
92 /* BCM2048_I2C_FM_RDS_SYSTEM */
93 #define BCM2048_FM_ON 0x01
94 #define BCM2048_RDS_ON 0x02
96 /* BCM2048_I2C_FM_CTRL */
97 #define BCM2048_BAND_SELECT 0x01
98 #define BCM2048_STEREO_MONO_AUTO_SELECT 0x02
99 #define BCM2048_STEREO_MONO_MANUAL_SELECT 0x04
100 #define BCM2048_STEREO_MONO_BLEND_SWITCH 0x08
101 #define BCM2048_HI_LO_INJECTION 0x10
103 /* BCM2048_I2C_RDS_CTRL0 */
104 #define BCM2048_RBDS_RDS_SELECT 0x01
105 #define BCM2048_FLUSH_FIFO 0x02
107 /* BCM2048_I2C_FM_AUDIO_PAUSE */
108 #define BCM2048_AUDIO_PAUSE_RSSI_TRESH 0x0f
109 #define BCM2048_AUDIO_PAUSE_DURATION 0xf0
111 /* BCM2048_I2C_FM_AUDIO_CTRL0 */
112 #define BCM2048_RF_MUTE 0x01
113 #define BCM2048_MANUAL_MUTE 0x02
114 #define BCM2048_DAC_OUTPUT_LEFT 0x04
115 #define BCM2048_DAC_OUTPUT_RIGHT 0x08
116 #define BCM2048_AUDIO_ROUTE_DAC 0x10
117 #define BCM2048_AUDIO_ROUTE_I2S 0x20
118 #define BCM2048_DE_EMPHASIS_SELECT 0x40
119 #define BCM2048_AUDIO_BANDWIDTH_SELECT 0x80
121 /* BCM2048_I2C_FM_SEARCH_CTRL0 */
122 #define BCM2048_SEARCH_RSSI_THRESHOLD 0x7f
123 #define BCM2048_SEARCH_DIRECTION 0x80
125 /* BCM2048_I2C_FM_SEARCH_TUNE_MODE */
126 #define BCM2048_FM_AUTO_SEARCH 0x03
128 /* BCM2048_I2C_FM_RSSI */
129 #define BCM2048_RSSI_VALUE 0xff
131 /* BCM2048_I2C_FM_RDS_MASK0 */
132 /* BCM2048_I2C_FM_RDS_MASK1 */
133 #define BCM2048_FM_FLAG_SEARCH_TUNE_FINISHED 0x01
134 #define BCM2048_FM_FLAG_SEARCH_TUNE_FAIL 0x02
135 #define BCM2048_FM_FLAG_RSSI_LOW 0x04
136 #define BCM2048_FM_FLAG_CARRIER_ERROR_HIGH 0x08
137 #define BCM2048_FM_FLAG_AUDIO_PAUSE_INDICATION 0x10
138 #define BCM2048_FLAG_STEREO_DETECTED 0x20
139 #define BCM2048_FLAG_STEREO_ACTIVE 0x40
141 /* BCM2048_I2C_RDS_DATA */
142 #define BCM2048_SLAVE_ADDRESS 0x3f
143 #define BCM2048_SLAVE_ENABLE 0x80
145 /* BCM2048_I2C_FM_BEST_TUNE_MODE */
146 #define BCM2048_BEST_TUNE_MODE 0x80
148 #define BCM2048_FM_FLAG_SEARCH_TUNE_FINISHED 0x01
149 #define BCM2048_FM_FLAG_SEARCH_TUNE_FAIL 0x02
150 #define BCM2048_FM_FLAG_RSSI_LOW 0x04
151 #define BCM2048_FM_FLAG_CARRIER_ERROR_HIGH 0x08
152 #define BCM2048_FM_FLAG_AUDIO_PAUSE_INDICATION 0x10
153 #define BCM2048_FLAG_STEREO_DETECTED 0x20
154 #define BCM2048_FLAG_STEREO_ACTIVE 0x40
156 #define BCM2048_RDS_FLAG_FIFO_WLINE 0x02
157 #define BCM2048_RDS_FLAG_B_BLOCK_MATCH 0x08
158 #define BCM2048_RDS_FLAG_SYNC_LOST 0x10
159 #define BCM2048_RDS_FLAG_PI_MATCH 0x20
161 #define BCM2048_RDS_MARK_END_BYTE0 0x7C
162 #define BCM2048_RDS_MARK_END_BYTEN 0xFF
164 #define BCM2048_FM_FLAGS_ALL (FM_FLAG_SEARCH_TUNE_FINISHED | \
165 FM_FLAG_SEARCH_TUNE_FAIL | \
167 FM_FLAG_CARRIER_ERROR_HIGH | \
168 FM_FLAG_AUDIO_PAUSE_INDICATION | \
169 FLAG_STEREO_DETECTED | FLAG_STEREO_ACTIVE)
171 #define BCM2048_RDS_FLAGS_ALL (RDS_FLAG_FIFO_WLINE | \
172 RDS_FLAG_B_BLOCK_MATCH | \
173 RDS_FLAG_SYNC_LOST | RDS_FLAG_PI_MATCH)
175 #define BCM2048_DEFAULT_TIMEOUT 1500
176 #define BCM2048_AUTO_SEARCH_TIMEOUT 3000
178 #define BCM2048_FREQDEV_UNIT 10000
179 #define BCM2048_FREQV4L2_MULTI 625
180 #define dev_to_v4l2(f) ((f * BCM2048_FREQDEV_UNIT) / BCM2048_FREQV4L2_MULTI)
181 #define v4l2_to_dev(f) ((f * BCM2048_FREQV4L2_MULTI) / BCM2048_FREQDEV_UNIT)
183 #define msb(x) ((u8)((u16)x >> 8))
184 #define lsb(x) ((u8)((u16)x & 0x00FF))
185 #define compose_u16(msb, lsb) (((u16)msb << 8) | lsb)
187 #define BCM2048_DEFAULT_POWERING_DELAY 20
188 #define BCM2048_DEFAULT_REGION 0x02
189 #define BCM2048_DEFAULT_MUTE 0x01
190 #define BCM2048_DEFAULT_RSSI_THRESHOLD 0x64
191 #define BCM2048_DEFAULT_RDS_WLINE 0x7E
193 #define BCM2048_FM_SEARCH_INACTIVE 0x00
194 #define BCM2048_FM_PRE_SET_MODE 0x01
195 #define BCM2048_FM_AUTO_SEARCH_MODE 0x02
196 #define BCM2048_FM_AF_JUMP_MODE 0x03
198 #define BCM2048_FREQUENCY_BASE 64000
200 #define BCM2048_POWER_ON 0x01
201 #define BCM2048_POWER_OFF 0x00
203 #define BCM2048_ITEM_ENABLED 0x01
204 #define BCM2048_SEARCH_DIRECTION_UP 0x01
206 #define BCM2048_DE_EMPHASIS_75us 75
207 #define BCM2048_DE_EMPHASIS_50us 50
209 #define BCM2048_SCAN_FAIL 0x00
210 #define BCM2048_SCAN_OK 0x01
212 #define BCM2048_FREQ_ERROR_FLOOR -20
213 #define BCM2048_FREQ_ERROR_ROOF 20
215 /* -60 dB is reported as full signal strength */
216 #define BCM2048_RSSI_LEVEL_BASE -60
217 #define BCM2048_RSSI_LEVEL_ROOF -100
218 #define BCM2048_RSSI_LEVEL_ROOF_NEG 100
219 #define BCM2048_SIGNAL_MULTIPLIER (0xFFFF / \
220 (BCM2048_RSSI_LEVEL_ROOF_NEG + \
221 BCM2048_RSSI_LEVEL_BASE))
223 #define BCM2048_RDS_FIFO_DUPLE_SIZE 0x03
224 #define BCM2048_RDS_CRC_MASK 0x0F
225 #define BCM2048_RDS_CRC_NONE 0x00
226 #define BCM2048_RDS_CRC_MAX_2BITS 0x04
227 #define BCM2048_RDS_CRC_LEAST_2BITS 0x08
228 #define BCM2048_RDS_CRC_UNRECOVARABLE 0x0C
230 #define BCM2048_RDS_BLOCK_MASK 0xF0
231 #define BCM2048_RDS_BLOCK_A 0x00
232 #define BCM2048_RDS_BLOCK_B 0x10
233 #define BCM2048_RDS_BLOCK_C 0x20
234 #define BCM2048_RDS_BLOCK_D 0x30
235 #define BCM2048_RDS_BLOCK_C_SCORED 0x40
236 #define BCM2048_RDS_BLOCK_E 0x60
238 #define BCM2048_RDS_RT 0x20
239 #define BCM2048_RDS_PS 0x00
241 #define BCM2048_RDS_GROUP_AB_MASK 0x08
242 #define BCM2048_RDS_GROUP_A 0x00
243 #define BCM2048_RDS_GROUP_B 0x08
245 #define BCM2048_RDS_RT_AB_MASK 0x10
246 #define BCM2048_RDS_RT_A 0x00
247 #define BCM2048_RDS_RT_B 0x10
248 #define BCM2048_RDS_RT_INDEX 0x0F
250 #define BCM2048_RDS_PS_INDEX 0x03
254 #define BCM2048_MAX_RDS_RT (64 + 1)
255 u8 rds_rt[BCM2048_MAX_RDS_RT];
258 #define BCM2048_MAX_RDS_PS (8 + 1)
259 u8 rds_ps[BCM2048_MAX_RDS_PS];
262 #define BCM2048_MAX_RDS_RADIO_TEXT 255
263 u8 radio_text[BCM2048_MAX_RDS_RADIO_TEXT + 3];
268 u32 bottom_frequency;
275 struct bcm2048_device {
276 struct i2c_client *client;
277 struct video_device videodev;
278 struct work_struct work;
279 struct completion compl;
281 struct bcm2048_platform_data *platform_data;
282 struct rds_info rds_info;
283 struct region_info region_info;
285 u8 cache_fm_rds_system;
287 u8 cache_fm_audio_ctrl0;
288 u8 cache_fm_search_ctrl0;
295 /* for rds data device read */
296 wait_queue_head_t read_queue;
298 unsigned char rds_data_available;
299 unsigned int rd_index;
302 static int radio_nr = -1; /* radio device minor (-1 ==> auto assign) */
303 module_param(radio_nr, int, 0000);
304 MODULE_PARM_DESC(radio_nr,
305 "Minor number for radio device (-1 ==> auto assign)");
307 static const struct region_info region_configs[] = {
310 .channel_spacing = 20,
311 .bottom_frequency = 87500,
312 .top_frequency = 108000,
318 .channel_spacing = 20,
319 .bottom_frequency = 87500,
320 .top_frequency = 108000,
326 .channel_spacing = 10,
327 .bottom_frequency = 87500,
328 .top_frequency = 108000,
334 .channel_spacing = 10,
335 .bottom_frequency = 76000,
336 .top_frequency = 90000,
343 * I2C Interface read / write
345 static int bcm2048_send_command(struct bcm2048_device *bdev, unsigned int reg,
348 struct i2c_client *client = bdev->client;
351 if (!bdev->power_state) {
352 dev_err(&bdev->client->dev, "bcm2048: chip not powered!\n");
356 data[0] = reg & 0xff;
357 data[1] = value & 0xff;
359 if (i2c_master_send(client, data, 2) == 2)
362 dev_err(&bdev->client->dev, "BCM I2C error!\n");
363 dev_err(&bdev->client->dev, "Is Bluetooth up and running?\n");
367 static int bcm2048_recv_command(struct bcm2048_device *bdev, unsigned int reg,
370 struct i2c_client *client = bdev->client;
372 if (!bdev->power_state) {
373 dev_err(&bdev->client->dev, "bcm2048: chip not powered!\n");
377 value[0] = i2c_smbus_read_byte_data(client, reg & 0xff);
382 static int bcm2048_recv_duples(struct bcm2048_device *bdev, unsigned int reg,
383 u8 *value, u8 duples)
385 struct i2c_client *client = bdev->client;
386 struct i2c_adapter *adap = client->adapter;
387 struct i2c_msg msg[2];
390 if (!bdev->power_state) {
391 dev_err(&bdev->client->dev, "bcm2048: chip not powered!\n");
397 msg[0].addr = client->addr;
398 msg[0].flags = client->flags & I2C_M_TEN;
402 msg[1].addr = client->addr;
403 msg[1].flags = client->flags & I2C_M_TEN;
404 msg[1].flags |= I2C_M_RD;
408 return i2c_transfer(adap, msg, 2);
412 * BCM2048 - I2C register programming helpers
414 static int bcm2048_set_power_state(struct bcm2048_device *bdev, u8 power)
418 mutex_lock(&bdev->mutex);
421 bdev->power_state = BCM2048_POWER_ON;
422 bdev->cache_fm_rds_system |= BCM2048_FM_ON;
424 bdev->cache_fm_rds_system &= ~BCM2048_FM_ON;
428 * Warning! FM cannot be turned off because then
429 * the I2C communications get ruined!
430 * Comment off the "if (power)" when the chip works!
433 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_SYSTEM,
434 bdev->cache_fm_rds_system);
435 msleep(BCM2048_DEFAULT_POWERING_DELAY);
438 bdev->power_state = BCM2048_POWER_OFF;
440 mutex_unlock(&bdev->mutex);
444 static int bcm2048_get_power_state(struct bcm2048_device *bdev)
449 mutex_lock(&bdev->mutex);
451 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_SYSTEM, &value);
453 mutex_unlock(&bdev->mutex);
455 if (!err && (value & BCM2048_FM_ON))
456 return BCM2048_POWER_ON;
461 static int bcm2048_set_rds_no_lock(struct bcm2048_device *bdev, u8 rds_on)
466 bdev->cache_fm_rds_system &= ~BCM2048_RDS_ON;
469 bdev->cache_fm_rds_system |= BCM2048_RDS_ON;
470 bdev->rds_state = BCM2048_RDS_ON;
471 flags = BCM2048_RDS_FLAG_FIFO_WLINE;
472 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK1,
477 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK1,
479 memset(&bdev->rds_info, 0, sizeof(bdev->rds_info));
484 return bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_SYSTEM,
485 bdev->cache_fm_rds_system);
488 static int bcm2048_get_rds_no_lock(struct bcm2048_device *bdev)
493 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_SYSTEM, &value);
495 if (!err && (value & BCM2048_RDS_ON))
496 return BCM2048_ITEM_ENABLED;
501 static int bcm2048_set_rds(struct bcm2048_device *bdev, u8 rds_on)
505 mutex_lock(&bdev->mutex);
507 err = bcm2048_set_rds_no_lock(bdev, rds_on);
509 mutex_unlock(&bdev->mutex);
513 static int bcm2048_get_rds(struct bcm2048_device *bdev)
517 mutex_lock(&bdev->mutex);
519 err = bcm2048_get_rds_no_lock(bdev);
521 mutex_unlock(&bdev->mutex);
525 static int bcm2048_get_rds_pi(struct bcm2048_device *bdev)
527 return bdev->rds_info.rds_pi;
530 static int bcm2048_set_fm_automatic_stereo_mono(struct bcm2048_device *bdev,
535 mutex_lock(&bdev->mutex);
537 bdev->cache_fm_ctrl &= ~BCM2048_STEREO_MONO_AUTO_SELECT;
540 bdev->cache_fm_ctrl |= BCM2048_STEREO_MONO_AUTO_SELECT;
542 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_CTRL,
543 bdev->cache_fm_ctrl);
545 mutex_unlock(&bdev->mutex);
549 static int bcm2048_set_fm_hi_lo_injection(struct bcm2048_device *bdev,
554 mutex_lock(&bdev->mutex);
556 bdev->cache_fm_ctrl &= ~BCM2048_HI_LO_INJECTION;
559 bdev->cache_fm_ctrl |= BCM2048_HI_LO_INJECTION;
561 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_CTRL,
562 bdev->cache_fm_ctrl);
564 mutex_unlock(&bdev->mutex);
568 static int bcm2048_get_fm_hi_lo_injection(struct bcm2048_device *bdev)
573 mutex_lock(&bdev->mutex);
575 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_CTRL, &value);
577 mutex_unlock(&bdev->mutex);
579 if (!err && (value & BCM2048_HI_LO_INJECTION))
580 return BCM2048_ITEM_ENABLED;
585 static int bcm2048_set_fm_frequency(struct bcm2048_device *bdev, u32 frequency)
589 if (frequency < bdev->region_info.bottom_frequency ||
590 frequency > bdev->region_info.top_frequency)
593 frequency -= BCM2048_FREQUENCY_BASE;
595 mutex_lock(&bdev->mutex);
597 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_FREQ0, lsb(frequency));
598 err |= bcm2048_send_command(bdev, BCM2048_I2C_FM_FREQ1,
602 bdev->frequency = frequency;
604 mutex_unlock(&bdev->mutex);
608 static int bcm2048_get_fm_frequency(struct bcm2048_device *bdev)
613 mutex_lock(&bdev->mutex);
615 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_FREQ0, &lsb);
616 err |= bcm2048_recv_command(bdev, BCM2048_I2C_FM_FREQ1, &msb);
618 mutex_unlock(&bdev->mutex);
623 err = compose_u16(msb, lsb);
624 err += BCM2048_FREQUENCY_BASE;
629 static int bcm2048_set_fm_af_frequency(struct bcm2048_device *bdev,
634 if (frequency < bdev->region_info.bottom_frequency ||
635 frequency > bdev->region_info.top_frequency)
638 frequency -= BCM2048_FREQUENCY_BASE;
640 mutex_lock(&bdev->mutex);
642 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_AF_FREQ0,
644 err |= bcm2048_send_command(bdev, BCM2048_I2C_FM_AF_FREQ1,
647 bdev->frequency = frequency;
649 mutex_unlock(&bdev->mutex);
653 static int bcm2048_get_fm_af_frequency(struct bcm2048_device *bdev)
658 mutex_lock(&bdev->mutex);
660 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_AF_FREQ0, &lsb);
661 err |= bcm2048_recv_command(bdev, BCM2048_I2C_FM_AF_FREQ1, &msb);
663 mutex_unlock(&bdev->mutex);
668 err = compose_u16(msb, lsb);
669 err += BCM2048_FREQUENCY_BASE;
674 static int bcm2048_set_fm_deemphasis(struct bcm2048_device *bdev, int d)
679 if (d == BCM2048_DE_EMPHASIS_75us)
680 deemphasis = BCM2048_DE_EMPHASIS_SELECT;
684 mutex_lock(&bdev->mutex);
686 bdev->cache_fm_audio_ctrl0 &= ~BCM2048_DE_EMPHASIS_SELECT;
687 bdev->cache_fm_audio_ctrl0 |= deemphasis;
689 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0,
690 bdev->cache_fm_audio_ctrl0);
693 bdev->region_info.deemphasis = d;
695 mutex_unlock(&bdev->mutex);
700 static int bcm2048_get_fm_deemphasis(struct bcm2048_device *bdev)
705 mutex_lock(&bdev->mutex);
707 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0, &value);
709 mutex_unlock(&bdev->mutex);
712 if (value & BCM2048_DE_EMPHASIS_SELECT)
713 return BCM2048_DE_EMPHASIS_75us;
715 return BCM2048_DE_EMPHASIS_50us;
721 static int bcm2048_set_region(struct bcm2048_device *bdev, u8 region)
724 u32 new_frequency = 0;
726 if (region >= ARRAY_SIZE(region_configs))
729 mutex_lock(&bdev->mutex);
730 bdev->region_info = region_configs[region];
732 if (region_configs[region].bottom_frequency < 87500)
733 bdev->cache_fm_ctrl |= BCM2048_BAND_SELECT;
735 bdev->cache_fm_ctrl &= ~BCM2048_BAND_SELECT;
737 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_CTRL,
738 bdev->cache_fm_ctrl);
740 mutex_unlock(&bdev->mutex);
743 mutex_unlock(&bdev->mutex);
745 if (bdev->frequency < region_configs[region].bottom_frequency ||
746 bdev->frequency > region_configs[region].top_frequency)
747 new_frequency = region_configs[region].bottom_frequency;
749 if (new_frequency > 0) {
750 err = bcm2048_set_fm_frequency(bdev, new_frequency);
756 err = bcm2048_set_fm_deemphasis(bdev,
757 region_configs[region].deemphasis);
763 static int bcm2048_get_region(struct bcm2048_device *bdev)
767 mutex_lock(&bdev->mutex);
768 err = bdev->region_info.region;
769 mutex_unlock(&bdev->mutex);
774 static int bcm2048_set_mute(struct bcm2048_device *bdev, u16 mute)
778 mutex_lock(&bdev->mutex);
780 bdev->cache_fm_audio_ctrl0 &= ~(BCM2048_RF_MUTE | BCM2048_MANUAL_MUTE);
783 bdev->cache_fm_audio_ctrl0 |= (BCM2048_RF_MUTE |
784 BCM2048_MANUAL_MUTE);
786 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0,
787 bdev->cache_fm_audio_ctrl0);
790 bdev->mute_state = mute;
792 mutex_unlock(&bdev->mutex);
796 static int bcm2048_get_mute(struct bcm2048_device *bdev)
801 mutex_lock(&bdev->mutex);
803 if (bdev->power_state) {
804 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0,
807 err = value & (BCM2048_RF_MUTE | BCM2048_MANUAL_MUTE);
809 err = bdev->mute_state;
812 mutex_unlock(&bdev->mutex);
816 static int bcm2048_set_audio_route(struct bcm2048_device *bdev, u8 route)
820 mutex_lock(&bdev->mutex);
822 route &= (BCM2048_AUDIO_ROUTE_DAC | BCM2048_AUDIO_ROUTE_I2S);
823 bdev->cache_fm_audio_ctrl0 &= ~(BCM2048_AUDIO_ROUTE_DAC |
824 BCM2048_AUDIO_ROUTE_I2S);
825 bdev->cache_fm_audio_ctrl0 |= route;
827 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0,
828 bdev->cache_fm_audio_ctrl0);
830 mutex_unlock(&bdev->mutex);
834 static int bcm2048_get_audio_route(struct bcm2048_device *bdev)
839 mutex_lock(&bdev->mutex);
841 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0, &value);
843 mutex_unlock(&bdev->mutex);
846 return value & (BCM2048_AUDIO_ROUTE_DAC |
847 BCM2048_AUDIO_ROUTE_I2S);
852 static int bcm2048_set_dac_output(struct bcm2048_device *bdev, u8 channels)
856 mutex_lock(&bdev->mutex);
858 bdev->cache_fm_audio_ctrl0 &= ~(BCM2048_DAC_OUTPUT_LEFT |
859 BCM2048_DAC_OUTPUT_RIGHT);
860 bdev->cache_fm_audio_ctrl0 |= channels;
862 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0,
863 bdev->cache_fm_audio_ctrl0);
865 mutex_unlock(&bdev->mutex);
869 static int bcm2048_get_dac_output(struct bcm2048_device *bdev)
874 mutex_lock(&bdev->mutex);
876 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0, &value);
878 mutex_unlock(&bdev->mutex);
881 return value & (BCM2048_DAC_OUTPUT_LEFT |
882 BCM2048_DAC_OUTPUT_RIGHT);
887 static int bcm2048_set_fm_search_rssi_threshold(struct bcm2048_device *bdev,
892 mutex_lock(&bdev->mutex);
894 threshold &= BCM2048_SEARCH_RSSI_THRESHOLD;
895 bdev->cache_fm_search_ctrl0 &= ~BCM2048_SEARCH_RSSI_THRESHOLD;
896 bdev->cache_fm_search_ctrl0 |= threshold;
898 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_SEARCH_CTRL0,
899 bdev->cache_fm_search_ctrl0);
901 mutex_unlock(&bdev->mutex);
905 static int bcm2048_get_fm_search_rssi_threshold(struct bcm2048_device *bdev)
910 mutex_lock(&bdev->mutex);
912 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_SEARCH_CTRL0, &value);
914 mutex_unlock(&bdev->mutex);
917 return value & BCM2048_SEARCH_RSSI_THRESHOLD;
922 static int bcm2048_set_fm_search_mode_direction(struct bcm2048_device *bdev,
927 mutex_lock(&bdev->mutex);
929 bdev->cache_fm_search_ctrl0 &= ~BCM2048_SEARCH_DIRECTION;
932 bdev->cache_fm_search_ctrl0 |= BCM2048_SEARCH_DIRECTION;
934 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_SEARCH_CTRL0,
935 bdev->cache_fm_search_ctrl0);
937 mutex_unlock(&bdev->mutex);
941 static int bcm2048_get_fm_search_mode_direction(struct bcm2048_device *bdev)
946 mutex_lock(&bdev->mutex);
948 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_SEARCH_CTRL0, &value);
950 mutex_unlock(&bdev->mutex);
952 if (!err && (value & BCM2048_SEARCH_DIRECTION))
953 return BCM2048_SEARCH_DIRECTION_UP;
958 static int bcm2048_set_fm_search_tune_mode(struct bcm2048_device *bdev,
961 int err, timeout, restart_rds = 0;
964 value = mode & BCM2048_FM_AUTO_SEARCH;
966 flags = BCM2048_FM_FLAG_SEARCH_TUNE_FINISHED |
967 BCM2048_FM_FLAG_SEARCH_TUNE_FAIL;
969 mutex_lock(&bdev->mutex);
972 * If RDS is enabled, and frequency is changed, RDS quits working.
973 * Thus, always restart RDS if it's enabled. Moreover, RDS must
974 * not be enabled while changing the frequency because it can
975 * provide a race to the mutex from the workqueue handler if RDS
976 * IRQ occurs while waiting for frequency changed IRQ.
978 if (bcm2048_get_rds_no_lock(bdev)) {
979 err = bcm2048_set_rds_no_lock(bdev, 0);
985 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK0, flags);
990 bcm2048_send_command(bdev, BCM2048_I2C_FM_SEARCH_TUNE_MODE, value);
992 if (mode != BCM2048_FM_AUTO_SEARCH_MODE)
993 timeout = BCM2048_DEFAULT_TIMEOUT;
995 timeout = BCM2048_AUTO_SEARCH_TIMEOUT;
997 if (!wait_for_completion_timeout(&bdev->compl,
998 msecs_to_jiffies(timeout)))
999 dev_err(&bdev->client->dev, "IRQ timeout.\n");
1002 if (!bdev->scan_state)
1007 err |= bcm2048_set_rds_no_lock(bdev, 1);
1009 mutex_unlock(&bdev->mutex);
1014 static int bcm2048_get_fm_search_tune_mode(struct bcm2048_device *bdev)
1019 mutex_lock(&bdev->mutex);
1021 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_SEARCH_TUNE_MODE,
1024 mutex_unlock(&bdev->mutex);
1027 return value & BCM2048_FM_AUTO_SEARCH;
1032 static int bcm2048_set_rds_b_block_mask(struct bcm2048_device *bdev, u16 mask)
1036 mutex_lock(&bdev->mutex);
1038 err = bcm2048_send_command(bdev, BCM2048_I2C_RDS_BLKB_MASK0,
1040 err |= bcm2048_send_command(bdev, BCM2048_I2C_RDS_BLKB_MASK1,
1043 mutex_unlock(&bdev->mutex);
1047 static int bcm2048_get_rds_b_block_mask(struct bcm2048_device *bdev)
1050 u8 lsb = 0, msb = 0;
1052 mutex_lock(&bdev->mutex);
1054 err = bcm2048_recv_command(bdev, BCM2048_I2C_RDS_BLKB_MASK0, &lsb);
1055 err |= bcm2048_recv_command(bdev, BCM2048_I2C_RDS_BLKB_MASK1, &msb);
1057 mutex_unlock(&bdev->mutex);
1060 return compose_u16(msb, lsb);
1065 static int bcm2048_set_rds_b_block_match(struct bcm2048_device *bdev,
1070 mutex_lock(&bdev->mutex);
1072 err = bcm2048_send_command(bdev, BCM2048_I2C_RDS_BLKB_MATCH0,
1074 err |= bcm2048_send_command(bdev, BCM2048_I2C_RDS_BLKB_MATCH1,
1077 mutex_unlock(&bdev->mutex);
1081 static int bcm2048_get_rds_b_block_match(struct bcm2048_device *bdev)
1084 u8 lsb = 0, msb = 0;
1086 mutex_lock(&bdev->mutex);
1088 err = bcm2048_recv_command(bdev, BCM2048_I2C_RDS_BLKB_MATCH0, &lsb);
1089 err |= bcm2048_recv_command(bdev, BCM2048_I2C_RDS_BLKB_MATCH1, &msb);
1091 mutex_unlock(&bdev->mutex);
1094 return compose_u16(msb, lsb);
1099 static int bcm2048_set_rds_pi_mask(struct bcm2048_device *bdev, u16 mask)
1103 mutex_lock(&bdev->mutex);
1105 err = bcm2048_send_command(bdev, BCM2048_I2C_RDS_PI_MASK0, lsb(mask));
1106 err |= bcm2048_send_command(bdev, BCM2048_I2C_RDS_PI_MASK1, msb(mask));
1108 mutex_unlock(&bdev->mutex);
1112 static int bcm2048_get_rds_pi_mask(struct bcm2048_device *bdev)
1115 u8 lsb = 0, msb = 0;
1117 mutex_lock(&bdev->mutex);
1119 err = bcm2048_recv_command(bdev, BCM2048_I2C_RDS_PI_MASK0, &lsb);
1120 err |= bcm2048_recv_command(bdev, BCM2048_I2C_RDS_PI_MASK1, &msb);
1122 mutex_unlock(&bdev->mutex);
1125 return compose_u16(msb, lsb);
1130 static int bcm2048_set_rds_pi_match(struct bcm2048_device *bdev, u16 match)
1134 mutex_lock(&bdev->mutex);
1136 err = bcm2048_send_command(bdev, BCM2048_I2C_RDS_PI_MATCH0,
1138 err |= bcm2048_send_command(bdev, BCM2048_I2C_RDS_PI_MATCH1,
1141 mutex_unlock(&bdev->mutex);
1145 static int bcm2048_get_rds_pi_match(struct bcm2048_device *bdev)
1148 u8 lsb = 0, msb = 0;
1150 mutex_lock(&bdev->mutex);
1152 err = bcm2048_recv_command(bdev, BCM2048_I2C_RDS_PI_MATCH0, &lsb);
1153 err |= bcm2048_recv_command(bdev, BCM2048_I2C_RDS_PI_MATCH1, &msb);
1155 mutex_unlock(&bdev->mutex);
1158 return compose_u16(msb, lsb);
1163 static int bcm2048_set_fm_rds_mask(struct bcm2048_device *bdev, u16 mask)
1167 mutex_lock(&bdev->mutex);
1169 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK0, lsb(mask));
1170 err |= bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK1, msb(mask));
1172 mutex_unlock(&bdev->mutex);
1176 static int bcm2048_get_fm_rds_mask(struct bcm2048_device *bdev)
1179 u8 value0 = 0, value1 = 0;
1181 mutex_lock(&bdev->mutex);
1183 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_MASK0, &value0);
1184 err |= bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_MASK1, &value1);
1186 mutex_unlock(&bdev->mutex);
1189 return compose_u16(value1, value0);
1194 static int bcm2048_get_fm_rds_flags(struct bcm2048_device *bdev)
1197 u8 value0 = 0, value1 = 0;
1199 mutex_lock(&bdev->mutex);
1201 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_FLAG0, &value0);
1202 err |= bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_FLAG1, &value1);
1204 mutex_unlock(&bdev->mutex);
1207 return compose_u16(value1, value0);
1212 static int bcm2048_get_region_bottom_frequency(struct bcm2048_device *bdev)
1214 return bdev->region_info.bottom_frequency;
1217 static int bcm2048_get_region_top_frequency(struct bcm2048_device *bdev)
1219 return bdev->region_info.top_frequency;
1222 static int bcm2048_set_fm_best_tune_mode(struct bcm2048_device *bdev, u8 mode)
1227 mutex_lock(&bdev->mutex);
1229 /* Perform read as the manual indicates */
1230 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_BEST_TUNE_MODE,
1232 value &= ~BCM2048_BEST_TUNE_MODE;
1235 value |= BCM2048_BEST_TUNE_MODE;
1236 err |= bcm2048_send_command(bdev, BCM2048_I2C_FM_BEST_TUNE_MODE,
1239 mutex_unlock(&bdev->mutex);
1243 static int bcm2048_get_fm_best_tune_mode(struct bcm2048_device *bdev)
1248 mutex_lock(&bdev->mutex);
1250 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_BEST_TUNE_MODE,
1253 mutex_unlock(&bdev->mutex);
1255 if (!err && (value & BCM2048_BEST_TUNE_MODE))
1256 return BCM2048_ITEM_ENABLED;
1261 static int bcm2048_get_fm_carrier_error(struct bcm2048_device *bdev)
1266 mutex_lock(&bdev->mutex);
1267 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_CARRIER, &value);
1268 mutex_unlock(&bdev->mutex);
1276 static int bcm2048_get_fm_rssi(struct bcm2048_device *bdev)
1281 mutex_lock(&bdev->mutex);
1282 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RSSI, &value);
1283 mutex_unlock(&bdev->mutex);
1291 static int bcm2048_set_rds_wline(struct bcm2048_device *bdev, u8 wline)
1295 mutex_lock(&bdev->mutex);
1297 err = bcm2048_send_command(bdev, BCM2048_I2C_RDS_WLINE, wline);
1300 bdev->fifo_size = wline;
1302 mutex_unlock(&bdev->mutex);
1306 static int bcm2048_get_rds_wline(struct bcm2048_device *bdev)
1311 mutex_lock(&bdev->mutex);
1313 err = bcm2048_recv_command(bdev, BCM2048_I2C_RDS_WLINE, &value);
1315 mutex_unlock(&bdev->mutex);
1318 bdev->fifo_size = value;
1325 static int bcm2048_checkrev(struct bcm2048_device *bdev)
1330 mutex_lock(&bdev->mutex);
1332 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_REV, &version);
1334 mutex_unlock(&bdev->mutex);
1337 dev_info(&bdev->client->dev, "BCM2048 Version 0x%x\n",
1345 static int bcm2048_get_rds_rt(struct bcm2048_device *bdev, char *data)
1347 int err = 0, i, j = 0, ce = 0, cr = 0;
1348 char data_buffer[BCM2048_MAX_RDS_RT + 1];
1350 mutex_lock(&bdev->mutex);
1352 if (!bdev->rds_info.text_len) {
1357 for (i = 0; i < BCM2048_MAX_RDS_RT; i++) {
1358 if (bdev->rds_info.rds_rt[i]) {
1360 /* Skip the carriage return */
1361 if (bdev->rds_info.rds_rt[i] != 0x0d) {
1362 data_buffer[j++] = bdev->rds_info.rds_rt[i];
1370 if (j <= BCM2048_MAX_RDS_RT)
1373 for (i = 0; i < BCM2048_MAX_RDS_RT; i++) {
1374 if (!bdev->rds_info.rds_rt[i]) {
1375 if (cr && (i < cr)) {
1380 if (cr && (i >= cr))
1388 memcpy(data, data_buffer, sizeof(data_buffer));
1391 mutex_unlock(&bdev->mutex);
1395 static int bcm2048_get_rds_ps(struct bcm2048_device *bdev, char *data)
1397 int err = 0, i, j = 0;
1398 char data_buffer[BCM2048_MAX_RDS_PS + 1];
1400 mutex_lock(&bdev->mutex);
1402 if (!bdev->rds_info.text_len) {
1407 for (i = 0; i < BCM2048_MAX_RDS_PS; i++) {
1408 if (bdev->rds_info.rds_ps[i]) {
1409 data_buffer[j++] = bdev->rds_info.rds_ps[i];
1411 if (i < (BCM2048_MAX_RDS_PS - 1)) {
1418 if (j <= BCM2048_MAX_RDS_PS)
1421 memcpy(data, data_buffer, sizeof(data_buffer));
1424 mutex_unlock(&bdev->mutex);
1428 static void bcm2048_parse_rds_pi(struct bcm2048_device *bdev)
1433 for (i = 0; i < bdev->fifo_size; i += BCM2048_RDS_FIFO_DUPLE_SIZE) {
1434 /* Block A match, only data without crc errors taken */
1435 if (bdev->rds_info.radio_text[i] == BCM2048_RDS_BLOCK_A) {
1436 pi = (bdev->rds_info.radio_text[i + 1] << 8) +
1437 bdev->rds_info.radio_text[i + 2];
1439 if (!bdev->rds_info.rds_pi) {
1440 bdev->rds_info.rds_pi = pi;
1443 if (pi != bdev->rds_info.rds_pi) {
1446 bdev->rds_info.rds_pi = pi;
1456 static int bcm2048_rds_block_crc(struct bcm2048_device *bdev, int i)
1458 return bdev->rds_info.radio_text[i] & BCM2048_RDS_CRC_MASK;
1461 static void bcm2048_parse_rds_rt_block(struct bcm2048_device *bdev, int i,
1464 /* Good data will overwrite poor data */
1466 if (!bdev->rds_info.rds_rt[index])
1467 bdev->rds_info.rds_rt[index] =
1468 bdev->rds_info.radio_text[i + 1];
1469 if (!bdev->rds_info.rds_rt[index + 1])
1470 bdev->rds_info.rds_rt[index + 1] =
1471 bdev->rds_info.radio_text[i + 2];
1473 bdev->rds_info.rds_rt[index] =
1474 bdev->rds_info.radio_text[i + 1];
1475 bdev->rds_info.rds_rt[index + 1] =
1476 bdev->rds_info.radio_text[i + 2];
1480 static int bcm2048_parse_rt_match_b(struct bcm2048_device *bdev, int i)
1482 int crc, rt_id, rt_group_b, rt_ab, index = 0;
1484 crc = bcm2048_rds_block_crc(bdev, i);
1486 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1489 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1490 BCM2048_RDS_BLOCK_B) {
1491 rt_id = bdev->rds_info.radio_text[i + 1] &
1492 BCM2048_RDS_BLOCK_MASK;
1493 rt_group_b = bdev->rds_info.radio_text[i + 1] &
1494 BCM2048_RDS_GROUP_AB_MASK;
1495 rt_ab = bdev->rds_info.radio_text[i + 2] &
1496 BCM2048_RDS_RT_AB_MASK;
1498 if (rt_group_b != bdev->rds_info.rds_rt_group_b) {
1499 memset(bdev->rds_info.rds_rt, 0,
1500 sizeof(bdev->rds_info.rds_rt));
1501 bdev->rds_info.rds_rt_group_b = rt_group_b;
1504 if (rt_id == BCM2048_RDS_RT) {
1505 /* A to B or (vice versa), means: clear screen */
1506 if (rt_ab != bdev->rds_info.rds_rt_ab) {
1507 memset(bdev->rds_info.rds_rt, 0,
1508 sizeof(bdev->rds_info.rds_rt));
1509 bdev->rds_info.rds_rt_ab = rt_ab;
1512 index = bdev->rds_info.radio_text[i + 2] &
1513 BCM2048_RDS_RT_INDEX;
1515 if (bdev->rds_info.rds_rt_group_b)
1527 static int bcm2048_parse_rt_match_c(struct bcm2048_device *bdev, int i,
1532 crc = bcm2048_rds_block_crc(bdev, i);
1534 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1537 if ((index + 2) >= BCM2048_MAX_RDS_RT) {
1538 dev_err(&bdev->client->dev,
1539 "Incorrect index = %d\n", index);
1543 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1544 BCM2048_RDS_BLOCK_C) {
1545 if (bdev->rds_info.rds_rt_group_b)
1547 bcm2048_parse_rds_rt_block(bdev, i, index, crc);
1554 static void bcm2048_parse_rt_match_d(struct bcm2048_device *bdev, int i,
1559 crc = bcm2048_rds_block_crc(bdev, i);
1561 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1564 if ((index + 4) >= BCM2048_MAX_RDS_RT) {
1565 dev_err(&bdev->client->dev,
1566 "Incorrect index = %d\n", index);
1570 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1571 BCM2048_RDS_BLOCK_D)
1572 bcm2048_parse_rds_rt_block(bdev, i, index + 2, crc);
1575 static void bcm2048_parse_rds_rt(struct bcm2048_device *bdev)
1577 int i, index = 0, crc, match_b = 0, match_c = 0, match_d = 0;
1579 for (i = 0; i < bdev->fifo_size; i += BCM2048_RDS_FIFO_DUPLE_SIZE) {
1582 index = bcm2048_parse_rt_match_b(bdev, i);
1583 if (index >= 0 && index <= (BCM2048_MAX_RDS_RT - 5))
1586 } else if (match_c) {
1588 if (bcm2048_parse_rt_match_c(bdev, i, index))
1591 } else if (match_d) {
1593 bcm2048_parse_rt_match_d(bdev, i, index);
1597 /* Skip erroneous blocks due to messed up A block altogether */
1598 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1599 BCM2048_RDS_BLOCK_A) {
1600 crc = bcm2048_rds_block_crc(bdev, i);
1601 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1603 /* Synchronize to a good RDS PI */
1604 if (((bdev->rds_info.radio_text[i + 1] << 8) +
1605 bdev->rds_info.radio_text[i + 2]) ==
1606 bdev->rds_info.rds_pi)
1612 static void bcm2048_parse_rds_ps_block(struct bcm2048_device *bdev, int i,
1615 /* Good data will overwrite poor data */
1617 if (!bdev->rds_info.rds_ps[index])
1618 bdev->rds_info.rds_ps[index] =
1619 bdev->rds_info.radio_text[i + 1];
1620 if (!bdev->rds_info.rds_ps[index + 1])
1621 bdev->rds_info.rds_ps[index + 1] =
1622 bdev->rds_info.radio_text[i + 2];
1624 bdev->rds_info.rds_ps[index] =
1625 bdev->rds_info.radio_text[i + 1];
1626 bdev->rds_info.rds_ps[index + 1] =
1627 bdev->rds_info.radio_text[i + 2];
1631 static int bcm2048_parse_ps_match_c(struct bcm2048_device *bdev, int i,
1636 crc = bcm2048_rds_block_crc(bdev, i);
1638 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1641 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1642 BCM2048_RDS_BLOCK_C)
1648 static void bcm2048_parse_ps_match_d(struct bcm2048_device *bdev, int i,
1653 crc = bcm2048_rds_block_crc(bdev, i);
1655 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1658 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1659 BCM2048_RDS_BLOCK_D)
1660 bcm2048_parse_rds_ps_block(bdev, i, index, crc);
1663 static int bcm2048_parse_ps_match_b(struct bcm2048_device *bdev, int i)
1665 int crc, index, ps_id, ps_group;
1667 crc = bcm2048_rds_block_crc(bdev, i);
1669 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1672 /* Block B Radio PS match */
1673 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1674 BCM2048_RDS_BLOCK_B) {
1675 ps_id = bdev->rds_info.radio_text[i + 1] &
1676 BCM2048_RDS_BLOCK_MASK;
1677 ps_group = bdev->rds_info.radio_text[i + 1] &
1678 BCM2048_RDS_GROUP_AB_MASK;
1681 * Poor RSSI will lead to RDS data corruption
1682 * So using 3 (same) sequential values to justify major changes
1684 if (ps_group != bdev->rds_info.rds_ps_group) {
1685 if (crc == BCM2048_RDS_CRC_NONE) {
1686 bdev->rds_info.rds_ps_group_cnt++;
1687 if (bdev->rds_info.rds_ps_group_cnt > 2) {
1688 bdev->rds_info.rds_ps_group = ps_group;
1689 bdev->rds_info.rds_ps_group_cnt = 0;
1690 dev_err(&bdev->client->dev,
1691 "RDS PS Group change!\n");
1696 bdev->rds_info.rds_ps_group_cnt = 0;
1700 if (ps_id == BCM2048_RDS_PS) {
1701 index = bdev->rds_info.radio_text[i + 2] &
1702 BCM2048_RDS_PS_INDEX;
1711 static void bcm2048_parse_rds_ps(struct bcm2048_device *bdev)
1713 int i, index = 0, crc, match_b = 0, match_c = 0, match_d = 0;
1715 for (i = 0; i < bdev->fifo_size; i += BCM2048_RDS_FIFO_DUPLE_SIZE) {
1718 index = bcm2048_parse_ps_match_b(bdev, i);
1719 if (index >= 0 && index < (BCM2048_MAX_RDS_PS - 1))
1722 } else if (match_c) {
1724 if (bcm2048_parse_ps_match_c(bdev, i, index))
1727 } else if (match_d) {
1729 bcm2048_parse_ps_match_d(bdev, i, index);
1733 /* Skip erroneous blocks due to messed up A block altogether */
1734 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1735 BCM2048_RDS_BLOCK_A) {
1736 crc = bcm2048_rds_block_crc(bdev, i);
1737 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1739 /* Synchronize to a good RDS PI */
1740 if (((bdev->rds_info.radio_text[i + 1] << 8) +
1741 bdev->rds_info.radio_text[i + 2]) ==
1742 bdev->rds_info.rds_pi)
1748 static void bcm2048_rds_fifo_receive(struct bcm2048_device *bdev)
1752 mutex_lock(&bdev->mutex);
1754 err = bcm2048_recv_duples(bdev, BCM2048_I2C_RDS_DATA,
1755 bdev->rds_info.radio_text, bdev->fifo_size);
1757 dev_err(&bdev->client->dev, "RDS Read problem\n");
1758 mutex_unlock(&bdev->mutex);
1762 bdev->rds_info.text_len = bdev->fifo_size;
1764 bcm2048_parse_rds_pi(bdev);
1765 bcm2048_parse_rds_rt(bdev);
1766 bcm2048_parse_rds_ps(bdev);
1768 mutex_unlock(&bdev->mutex);
1770 wake_up_interruptible(&bdev->read_queue);
1773 static int bcm2048_get_rds_data(struct bcm2048_device *bdev, char *data)
1775 int err = 0, i, p = 0;
1778 mutex_lock(&bdev->mutex);
1780 if (!bdev->rds_info.text_len) {
1785 data_buffer = kcalloc(BCM2048_MAX_RDS_RADIO_TEXT, 5, GFP_KERNEL);
1791 for (i = 0; i < bdev->rds_info.text_len; i++) {
1792 p += sprintf(data_buffer + p, "%x ",
1793 bdev->rds_info.radio_text[i]);
1796 memcpy(data, data_buffer, p);
1800 mutex_unlock(&bdev->mutex);
1805 * BCM2048 default initialization sequence
1807 static int bcm2048_init(struct bcm2048_device *bdev)
1811 err = bcm2048_set_power_state(bdev, BCM2048_POWER_ON);
1815 err = bcm2048_set_audio_route(bdev, BCM2048_AUDIO_ROUTE_DAC);
1819 err = bcm2048_set_dac_output(bdev, BCM2048_DAC_OUTPUT_LEFT |
1820 BCM2048_DAC_OUTPUT_RIGHT);
1827 * BCM2048 default deinitialization sequence
1829 static int bcm2048_deinit(struct bcm2048_device *bdev)
1833 err = bcm2048_set_audio_route(bdev, 0);
1837 err = bcm2048_set_dac_output(bdev, 0);
1841 return bcm2048_set_power_state(bdev, BCM2048_POWER_OFF);
1845 * BCM2048 probe sequence
1847 static int bcm2048_probe(struct bcm2048_device *bdev)
1851 err = bcm2048_set_power_state(bdev, BCM2048_POWER_ON);
1855 err = bcm2048_checkrev(bdev);
1859 err = bcm2048_set_mute(bdev, BCM2048_DEFAULT_MUTE);
1863 err = bcm2048_set_region(bdev, BCM2048_DEFAULT_REGION);
1867 err = bcm2048_set_fm_search_rssi_threshold(bdev,
1868 BCM2048_DEFAULT_RSSI_THRESHOLD);
1872 err = bcm2048_set_fm_automatic_stereo_mono(bdev, BCM2048_ITEM_ENABLED);
1876 err = bcm2048_get_rds_wline(bdev);
1877 if (err < BCM2048_DEFAULT_RDS_WLINE)
1878 err = bcm2048_set_rds_wline(bdev, BCM2048_DEFAULT_RDS_WLINE);
1882 err = bcm2048_set_power_state(bdev, BCM2048_POWER_OFF);
1884 init_waitqueue_head(&bdev->read_queue);
1885 bdev->rds_data_available = 0;
1894 * BCM2048 workqueue handler
1896 static void bcm2048_work(struct work_struct *work)
1898 struct bcm2048_device *bdev;
1899 u8 flag_lsb = 0, flag_msb = 0, flags;
1901 bdev = container_of(work, struct bcm2048_device, work);
1902 bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_FLAG0, &flag_lsb);
1903 bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_FLAG1, &flag_msb);
1905 if (flag_lsb & (BCM2048_FM_FLAG_SEARCH_TUNE_FINISHED |
1906 BCM2048_FM_FLAG_SEARCH_TUNE_FAIL)) {
1907 if (flag_lsb & BCM2048_FM_FLAG_SEARCH_TUNE_FAIL)
1908 bdev->scan_state = BCM2048_SCAN_FAIL;
1910 bdev->scan_state = BCM2048_SCAN_OK;
1912 complete(&bdev->compl);
1915 if (flag_msb & BCM2048_RDS_FLAG_FIFO_WLINE) {
1916 bcm2048_rds_fifo_receive(bdev);
1917 if (bdev->rds_state) {
1918 flags = BCM2048_RDS_FLAG_FIFO_WLINE;
1919 bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK1,
1922 bdev->rds_data_available = 1;
1923 bdev->rd_index = 0; /* new data, new start */
1928 * BCM2048 interrupt handler
1930 static irqreturn_t bcm2048_handler(int irq, void *dev)
1932 struct bcm2048_device *bdev = dev;
1934 dev_dbg(&bdev->client->dev, "IRQ called, queuing work\n");
1935 if (bdev->power_state)
1936 schedule_work(&bdev->work);
1942 * BCM2048 sysfs interface definitions
1944 #define property_write(prop, type, mask, check) \
1945 static ssize_t bcm2048_##prop##_write(struct device *dev, \
1946 struct device_attribute *attr, \
1950 struct bcm2048_device *bdev = dev_get_drvdata(dev); \
1957 if (sscanf(buf, mask, &value) != 1) \
1963 err = bcm2048_set_##prop(bdev, value); \
1965 return err < 0 ? err : count; \
1968 #define property_read(prop, size, mask) \
1969 static ssize_t bcm2048_##prop##_read(struct device *dev, \
1970 struct device_attribute *attr, \
1973 struct bcm2048_device *bdev = dev_get_drvdata(dev); \
1979 value = bcm2048_get_##prop(bdev); \
1982 value = sprintf(buf, mask "\n", value); \
1987 #define property_signed_read(prop, size, mask) \
1988 static ssize_t bcm2048_##prop##_read(struct device *dev, \
1989 struct device_attribute *attr, \
1992 struct bcm2048_device *bdev = dev_get_drvdata(dev); \
1998 value = bcm2048_get_##prop(bdev); \
2000 return sprintf(buf, mask "\n", value); \
2003 #define DEFINE_SYSFS_PROPERTY(prop, signal, size, mask, check) \
2004 property_write(prop, signal size, mask, check) \
2005 property_read(prop, size, mask)
2007 #define property_str_read(prop, size) \
2008 static ssize_t bcm2048_##prop##_read(struct device *dev, \
2009 struct device_attribute *attr, \
2012 struct bcm2048_device *bdev = dev_get_drvdata(dev); \
2019 out = kzalloc(size + 1, GFP_KERNEL); \
2023 bcm2048_get_##prop(bdev, out); \
2024 count = sprintf(buf, "%s\n", out); \
2031 DEFINE_SYSFS_PROPERTY(power_state, unsigned, int, "%u", 0)
2032 DEFINE_SYSFS_PROPERTY(mute, unsigned, int, "%u", 0)
2033 DEFINE_SYSFS_PROPERTY(audio_route, unsigned, int, "%u", 0)
2034 DEFINE_SYSFS_PROPERTY(dac_output, unsigned, int, "%u", 0)
2036 DEFINE_SYSFS_PROPERTY(fm_hi_lo_injection, unsigned, int, "%u", 0)
2037 DEFINE_SYSFS_PROPERTY(fm_frequency, unsigned, int, "%u", 0)
2038 DEFINE_SYSFS_PROPERTY(fm_af_frequency, unsigned, int, "%u", 0)
2039 DEFINE_SYSFS_PROPERTY(fm_deemphasis, unsigned, int, "%u", 0)
2040 DEFINE_SYSFS_PROPERTY(fm_rds_mask, unsigned, int, "%u", 0)
2041 DEFINE_SYSFS_PROPERTY(fm_best_tune_mode, unsigned, int, "%u", 0)
2042 DEFINE_SYSFS_PROPERTY(fm_search_rssi_threshold, unsigned, int, "%u", 0)
2043 DEFINE_SYSFS_PROPERTY(fm_search_mode_direction, unsigned, int, "%u", 0)
2044 DEFINE_SYSFS_PROPERTY(fm_search_tune_mode, unsigned, int, "%u", value > 3)
2046 DEFINE_SYSFS_PROPERTY(rds, unsigned, int, "%u", 0)
2047 DEFINE_SYSFS_PROPERTY(rds_b_block_mask, unsigned, int, "%u", 0)
2048 DEFINE_SYSFS_PROPERTY(rds_b_block_match, unsigned, int, "%u", 0)
2049 DEFINE_SYSFS_PROPERTY(rds_pi_mask, unsigned, int, "%u", 0)
2050 DEFINE_SYSFS_PROPERTY(rds_pi_match, unsigned, int, "%u", 0)
2051 DEFINE_SYSFS_PROPERTY(rds_wline, unsigned, int, "%u", 0)
2052 property_read(rds_pi, unsigned int, "%x")
2053 property_str_read(rds_rt, (BCM2048_MAX_RDS_RT + 1))
2054 property_str_read(rds_ps, (BCM2048_MAX_RDS_PS + 1))
2056 property_read(fm_rds_flags, unsigned int, "%u")
2057 property_str_read(rds_data, BCM2048_MAX_RDS_RADIO_TEXT * 5)
2059 property_read(region_bottom_frequency, unsigned int, "%u")
2060 property_read(region_top_frequency, unsigned int, "%u")
2061 property_signed_read(fm_carrier_error, int, "%d")
2062 property_signed_read(fm_rssi, int, "%d")
2063 DEFINE_SYSFS_PROPERTY(region, unsigned, int, "%u", 0)
2065 static struct device_attribute attrs[] = {
2066 __ATTR(power_state, 0644, bcm2048_power_state_read,
2067 bcm2048_power_state_write),
2068 __ATTR(mute, 0644, bcm2048_mute_read,
2069 bcm2048_mute_write),
2070 __ATTR(audio_route, 0644, bcm2048_audio_route_read,
2071 bcm2048_audio_route_write),
2072 __ATTR(dac_output, 0644, bcm2048_dac_output_read,
2073 bcm2048_dac_output_write),
2074 __ATTR(fm_hi_lo_injection, 0644,
2075 bcm2048_fm_hi_lo_injection_read,
2076 bcm2048_fm_hi_lo_injection_write),
2077 __ATTR(fm_frequency, 0644, bcm2048_fm_frequency_read,
2078 bcm2048_fm_frequency_write),
2079 __ATTR(fm_af_frequency, 0644,
2080 bcm2048_fm_af_frequency_read,
2081 bcm2048_fm_af_frequency_write),
2082 __ATTR(fm_deemphasis, 0644, bcm2048_fm_deemphasis_read,
2083 bcm2048_fm_deemphasis_write),
2084 __ATTR(fm_rds_mask, 0644, bcm2048_fm_rds_mask_read,
2085 bcm2048_fm_rds_mask_write),
2086 __ATTR(fm_best_tune_mode, 0644,
2087 bcm2048_fm_best_tune_mode_read,
2088 bcm2048_fm_best_tune_mode_write),
2089 __ATTR(fm_search_rssi_threshold, 0644,
2090 bcm2048_fm_search_rssi_threshold_read,
2091 bcm2048_fm_search_rssi_threshold_write),
2092 __ATTR(fm_search_mode_direction, 0644,
2093 bcm2048_fm_search_mode_direction_read,
2094 bcm2048_fm_search_mode_direction_write),
2095 __ATTR(fm_search_tune_mode, 0644,
2096 bcm2048_fm_search_tune_mode_read,
2097 bcm2048_fm_search_tune_mode_write),
2098 __ATTR(rds, 0644, bcm2048_rds_read,
2100 __ATTR(rds_b_block_mask, 0644,
2101 bcm2048_rds_b_block_mask_read,
2102 bcm2048_rds_b_block_mask_write),
2103 __ATTR(rds_b_block_match, 0644,
2104 bcm2048_rds_b_block_match_read,
2105 bcm2048_rds_b_block_match_write),
2106 __ATTR(rds_pi_mask, 0644, bcm2048_rds_pi_mask_read,
2107 bcm2048_rds_pi_mask_write),
2108 __ATTR(rds_pi_match, 0644, bcm2048_rds_pi_match_read,
2109 bcm2048_rds_pi_match_write),
2110 __ATTR(rds_wline, 0644, bcm2048_rds_wline_read,
2111 bcm2048_rds_wline_write),
2112 __ATTR(rds_pi, 0444, bcm2048_rds_pi_read, NULL),
2113 __ATTR(rds_rt, 0444, bcm2048_rds_rt_read, NULL),
2114 __ATTR(rds_ps, 0444, bcm2048_rds_ps_read, NULL),
2115 __ATTR(fm_rds_flags, 0444, bcm2048_fm_rds_flags_read, NULL),
2116 __ATTR(region_bottom_frequency, 0444,
2117 bcm2048_region_bottom_frequency_read, NULL),
2118 __ATTR(region_top_frequency, 0444,
2119 bcm2048_region_top_frequency_read, NULL),
2120 __ATTR(fm_carrier_error, 0444,
2121 bcm2048_fm_carrier_error_read, NULL),
2122 __ATTR(fm_rssi, 0444,
2123 bcm2048_fm_rssi_read, NULL),
2124 __ATTR(region, 0644, bcm2048_region_read,
2125 bcm2048_region_write),
2126 __ATTR(rds_data, 0444, bcm2048_rds_data_read, NULL),
2129 static int bcm2048_sysfs_unregister_properties(struct bcm2048_device *bdev,
2134 for (i = 0; i < size; i++)
2135 device_remove_file(&bdev->client->dev, &attrs[i]);
2140 static int bcm2048_sysfs_register_properties(struct bcm2048_device *bdev)
2145 for (i = 0; i < ARRAY_SIZE(attrs); i++) {
2146 if (device_create_file(&bdev->client->dev, &attrs[i]) != 0) {
2147 dev_err(&bdev->client->dev,
2148 "could not register sysfs entry\n");
2150 bcm2048_sysfs_unregister_properties(bdev, i);
2158 static int bcm2048_fops_open(struct file *file)
2160 struct bcm2048_device *bdev = video_drvdata(file);
2164 bdev->rds_data_available = 0;
2169 static int bcm2048_fops_release(struct file *file)
2171 struct bcm2048_device *bdev = video_drvdata(file);
2178 static unsigned int bcm2048_fops_poll(struct file *file,
2179 struct poll_table_struct *pts)
2181 struct bcm2048_device *bdev = video_drvdata(file);
2184 poll_wait(file, &bdev->read_queue, pts);
2186 if (bdev->rds_data_available)
2187 retval = POLLIN | POLLRDNORM;
2192 static ssize_t bcm2048_fops_read(struct file *file, char __user *buf,
2193 size_t count, loff_t *ppos)
2195 struct bcm2048_device *bdev = video_drvdata(file);
2199 /* we return at least 3 bytes, one block */
2200 count = (count / 3) * 3; /* only multiples of 3 */
2204 while (!bdev->rds_data_available) {
2205 if (file->f_flags & O_NONBLOCK) {
2206 retval = -EWOULDBLOCK;
2209 /* interruptible_sleep_on(&bdev->read_queue); */
2210 if (wait_event_interruptible(bdev->read_queue,
2211 bdev->rds_data_available) < 0) {
2217 mutex_lock(&bdev->mutex);
2218 /* copy data to userspace */
2219 i = bdev->fifo_size - bdev->rd_index;
2221 count = (i / 3) * 3;
2225 unsigned char tmpbuf[3];
2227 tmpbuf[i] = bdev->rds_info.radio_text[bdev->rd_index + i + 2];
2229 bdev->rds_info.radio_text[bdev->rd_index + i + 1];
2231 (bdev->rds_info.radio_text[bdev->rd_index + i] &
2233 if ((bdev->rds_info.radio_text[bdev->rd_index + i] &
2234 BCM2048_RDS_CRC_MASK) == BCM2048_RDS_CRC_UNRECOVARABLE)
2235 tmpbuf[i + 2] |= 0x80;
2236 if (copy_to_user(buf + i, tmpbuf, 3)) {
2243 bdev->rd_index += i;
2244 if (bdev->rd_index >= bdev->fifo_size)
2245 bdev->rds_data_available = 0;
2247 mutex_unlock(&bdev->mutex);
2256 * bcm2048_fops - file operations interface
2258 static const struct v4l2_file_operations bcm2048_fops = {
2259 .owner = THIS_MODULE,
2260 .unlocked_ioctl = video_ioctl2,
2261 /* for RDS read support */
2262 .open = bcm2048_fops_open,
2263 .release = bcm2048_fops_release,
2264 .read = bcm2048_fops_read,
2265 .poll = bcm2048_fops_poll
2269 * Video4Linux Interface
2271 static struct v4l2_queryctrl bcm2048_v4l2_queryctrl[] = {
2273 .id = V4L2_CID_AUDIO_VOLUME,
2274 .flags = V4L2_CTRL_FLAG_DISABLED,
2277 .id = V4L2_CID_AUDIO_BALANCE,
2278 .flags = V4L2_CTRL_FLAG_DISABLED,
2281 .id = V4L2_CID_AUDIO_BASS,
2282 .flags = V4L2_CTRL_FLAG_DISABLED,
2285 .id = V4L2_CID_AUDIO_TREBLE,
2286 .flags = V4L2_CTRL_FLAG_DISABLED,
2289 .id = V4L2_CID_AUDIO_MUTE,
2290 .type = V4L2_CTRL_TYPE_BOOLEAN,
2298 .id = V4L2_CID_AUDIO_LOUDNESS,
2299 .flags = V4L2_CTRL_FLAG_DISABLED,
2303 static int bcm2048_vidioc_querycap(struct file *file, void *priv,
2304 struct v4l2_capability *capability)
2306 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2308 strlcpy(capability->driver, BCM2048_DRIVER_NAME,
2309 sizeof(capability->driver));
2310 strlcpy(capability->card, BCM2048_DRIVER_CARD,
2311 sizeof(capability->card));
2312 snprintf(capability->bus_info, 32, "I2C: 0x%X", bdev->client->addr);
2313 capability->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
2314 V4L2_CAP_HW_FREQ_SEEK;
2315 capability->capabilities = capability->device_caps |
2316 V4L2_CAP_DEVICE_CAPS;
2321 static int bcm2048_vidioc_g_input(struct file *filp, void *priv,
2329 static int bcm2048_vidioc_s_input(struct file *filp, void *priv,
2338 static int bcm2048_vidioc_queryctrl(struct file *file, void *priv,
2339 struct v4l2_queryctrl *qc)
2343 for (i = 0; i < ARRAY_SIZE(bcm2048_v4l2_queryctrl); i++) {
2344 if (qc->id && qc->id == bcm2048_v4l2_queryctrl[i].id) {
2345 *qc = bcm2048_v4l2_queryctrl[i];
2353 static int bcm2048_vidioc_g_ctrl(struct file *file, void *priv,
2354 struct v4l2_control *ctrl)
2356 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2363 case V4L2_CID_AUDIO_MUTE:
2364 err = bcm2048_get_mute(bdev);
2373 static int bcm2048_vidioc_s_ctrl(struct file *file, void *priv,
2374 struct v4l2_control *ctrl)
2376 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2383 case V4L2_CID_AUDIO_MUTE:
2385 if (bdev->power_state) {
2386 err = bcm2048_set_mute(bdev, ctrl->value);
2387 err |= bcm2048_deinit(bdev);
2390 if (!bdev->power_state) {
2391 err = bcm2048_init(bdev);
2392 err |= bcm2048_set_mute(bdev, ctrl->value);
2401 static int bcm2048_vidioc_g_audio(struct file *file, void *priv,
2402 struct v4l2_audio *audio)
2404 if (audio->index > 1)
2407 strncpy(audio->name, "Radio", 32);
2408 audio->capability = V4L2_AUDCAP_STEREO;
2413 static int bcm2048_vidioc_s_audio(struct file *file, void *priv,
2414 const struct v4l2_audio *audio)
2416 if (audio->index != 0)
2422 static int bcm2048_vidioc_g_tuner(struct file *file, void *priv,
2423 struct v4l2_tuner *tuner)
2425 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2432 if (tuner->index > 0)
2435 strncpy(tuner->name, "FM Receiver", 32);
2436 tuner->type = V4L2_TUNER_RADIO;
2438 dev_to_v4l2(bcm2048_get_region_bottom_frequency(bdev));
2440 dev_to_v4l2(bcm2048_get_region_top_frequency(bdev));
2441 tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
2442 tuner->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
2443 tuner->audmode = V4L2_TUNER_MODE_STEREO;
2445 if (bdev->power_state) {
2447 * Report frequencies with high carrier errors to have zero
2450 f_error = bcm2048_get_fm_carrier_error(bdev);
2451 if (f_error < BCM2048_FREQ_ERROR_FLOOR ||
2452 f_error > BCM2048_FREQ_ERROR_ROOF) {
2456 * RSSI level -60 dB is defined to report full
2459 rssi = bcm2048_get_fm_rssi(bdev);
2460 if (rssi >= BCM2048_RSSI_LEVEL_BASE) {
2461 tuner->signal = 0xFFFF;
2462 } else if (rssi > BCM2048_RSSI_LEVEL_ROOF) {
2463 tuner->signal = (rssi +
2464 BCM2048_RSSI_LEVEL_ROOF_NEG)
2465 * BCM2048_SIGNAL_MULTIPLIER;
2477 static int bcm2048_vidioc_s_tuner(struct file *file, void *priv,
2478 const struct v4l2_tuner *tuner)
2480 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2485 if (tuner->index > 0)
2491 static int bcm2048_vidioc_g_frequency(struct file *file, void *priv,
2492 struct v4l2_frequency *freq)
2494 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2498 if (!bdev->power_state)
2501 freq->type = V4L2_TUNER_RADIO;
2502 f = bcm2048_get_fm_frequency(bdev);
2507 freq->frequency = dev_to_v4l2(f);
2512 static int bcm2048_vidioc_s_frequency(struct file *file, void *priv,
2513 const struct v4l2_frequency *freq)
2515 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2518 if (freq->type != V4L2_TUNER_RADIO)
2521 if (!bdev->power_state)
2524 err = bcm2048_set_fm_frequency(bdev, v4l2_to_dev(freq->frequency));
2525 err |= bcm2048_set_fm_search_tune_mode(bdev, BCM2048_FM_PRE_SET_MODE);
2530 static int bcm2048_vidioc_s_hw_freq_seek(struct file *file, void *priv,
2531 const struct v4l2_hw_freq_seek *seek)
2533 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2536 if (!bdev->power_state)
2539 if ((seek->tuner != 0) || (seek->type != V4L2_TUNER_RADIO))
2542 err = bcm2048_set_fm_search_mode_direction(bdev, seek->seek_upward);
2543 err |= bcm2048_set_fm_search_tune_mode(bdev,
2544 BCM2048_FM_AUTO_SEARCH_MODE);
2549 static const struct v4l2_ioctl_ops bcm2048_ioctl_ops = {
2550 .vidioc_querycap = bcm2048_vidioc_querycap,
2551 .vidioc_g_input = bcm2048_vidioc_g_input,
2552 .vidioc_s_input = bcm2048_vidioc_s_input,
2553 .vidioc_queryctrl = bcm2048_vidioc_queryctrl,
2554 .vidioc_g_ctrl = bcm2048_vidioc_g_ctrl,
2555 .vidioc_s_ctrl = bcm2048_vidioc_s_ctrl,
2556 .vidioc_g_audio = bcm2048_vidioc_g_audio,
2557 .vidioc_s_audio = bcm2048_vidioc_s_audio,
2558 .vidioc_g_tuner = bcm2048_vidioc_g_tuner,
2559 .vidioc_s_tuner = bcm2048_vidioc_s_tuner,
2560 .vidioc_g_frequency = bcm2048_vidioc_g_frequency,
2561 .vidioc_s_frequency = bcm2048_vidioc_s_frequency,
2562 .vidioc_s_hw_freq_seek = bcm2048_vidioc_s_hw_freq_seek,
2566 * bcm2048_viddev_template - video device interface
2568 static struct video_device bcm2048_viddev_template = {
2569 .fops = &bcm2048_fops,
2570 .name = BCM2048_DRIVER_NAME,
2571 .release = video_device_release_empty,
2572 .ioctl_ops = &bcm2048_ioctl_ops,
2576 * I2C driver interface
2578 static int bcm2048_i2c_driver_probe(struct i2c_client *client,
2579 const struct i2c_device_id *id)
2581 struct bcm2048_device *bdev;
2584 bdev = kzalloc(sizeof(*bdev), GFP_KERNEL);
2590 bdev->client = client;
2591 i2c_set_clientdata(client, bdev);
2592 mutex_init(&bdev->mutex);
2593 init_completion(&bdev->compl);
2594 INIT_WORK(&bdev->work, bcm2048_work);
2597 err = request_irq(client->irq,
2598 bcm2048_handler, IRQF_TRIGGER_FALLING,
2599 client->name, bdev);
2601 dev_err(&client->dev, "Could not request IRQ\n");
2604 dev_dbg(&client->dev, "IRQ requested.\n");
2606 dev_dbg(&client->dev, "IRQ not configured. Using timeouts.\n");
2609 bdev->videodev = bcm2048_viddev_template;
2610 video_set_drvdata(&bdev->videodev, bdev);
2611 if (video_register_device(&bdev->videodev, VFL_TYPE_RADIO, radio_nr)) {
2612 dev_dbg(&client->dev, "Could not register video device.\n");
2617 err = bcm2048_sysfs_register_properties(bdev);
2619 dev_dbg(&client->dev, "Could not register sysfs interface.\n");
2620 goto free_registration;
2623 err = bcm2048_probe(bdev);
2625 dev_dbg(&client->dev, "Failed to probe device information.\n");
2632 bcm2048_sysfs_unregister_properties(bdev, ARRAY_SIZE(attrs));
2634 video_unregister_device(&bdev->videodev);
2637 free_irq(client->irq, bdev);
2639 i2c_set_clientdata(client, NULL);
2645 static int bcm2048_i2c_driver_remove(struct i2c_client *client)
2647 struct bcm2048_device *bdev = i2c_get_clientdata(client);
2649 if (!client->adapter)
2653 bcm2048_sysfs_unregister_properties(bdev, ARRAY_SIZE(attrs));
2654 video_unregister_device(&bdev->videodev);
2656 if (bdev->power_state)
2657 bcm2048_set_power_state(bdev, BCM2048_POWER_OFF);
2659 if (client->irq > 0)
2660 free_irq(client->irq, bdev);
2662 cancel_work_sync(&bdev->work);
2671 * bcm2048_i2c_driver - i2c driver interface
2673 static const struct i2c_device_id bcm2048_id[] = {
2677 MODULE_DEVICE_TABLE(i2c, bcm2048_id);
2679 static struct i2c_driver bcm2048_i2c_driver = {
2681 .name = BCM2048_DRIVER_NAME,
2683 .probe = bcm2048_i2c_driver_probe,
2684 .remove = bcm2048_i2c_driver_remove,
2685 .id_table = bcm2048_id,
2688 module_i2c_driver(bcm2048_i2c_driver);
2690 MODULE_LICENSE("GPL");
2691 MODULE_AUTHOR(BCM2048_DRIVER_AUTHOR);
2692 MODULE_DESCRIPTION(BCM2048_DRIVER_DESC);
2693 MODULE_VERSION("0.0.2");