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_CARD "Broadcom bcm2048 FM Radio Receiver"
52 #define BCM2048_DRIVER_DESC "I2C driver for BCM2048 FM Radio Receiver"
54 /* I2C Control Registers */
55 #define BCM2048_I2C_FM_RDS_SYSTEM 0x00
56 #define BCM2048_I2C_FM_CTRL 0x01
57 #define BCM2048_I2C_RDS_CTRL0 0x02
58 #define BCM2048_I2C_RDS_CTRL1 0x03
59 #define BCM2048_I2C_FM_AUDIO_PAUSE 0x04
60 #define BCM2048_I2C_FM_AUDIO_CTRL0 0x05
61 #define BCM2048_I2C_FM_AUDIO_CTRL1 0x06
62 #define BCM2048_I2C_FM_SEARCH_CTRL0 0x07
63 #define BCM2048_I2C_FM_SEARCH_CTRL1 0x08
64 #define BCM2048_I2C_FM_SEARCH_TUNE_MODE 0x09
65 #define BCM2048_I2C_FM_FREQ0 0x0a
66 #define BCM2048_I2C_FM_FREQ1 0x0b
67 #define BCM2048_I2C_FM_AF_FREQ0 0x0c
68 #define BCM2048_I2C_FM_AF_FREQ1 0x0d
69 #define BCM2048_I2C_FM_CARRIER 0x0e
70 #define BCM2048_I2C_FM_RSSI 0x0f
71 #define BCM2048_I2C_FM_RDS_MASK0 0x10
72 #define BCM2048_I2C_FM_RDS_MASK1 0x11
73 #define BCM2048_I2C_FM_RDS_FLAG0 0x12
74 #define BCM2048_I2C_FM_RDS_FLAG1 0x13
75 #define BCM2048_I2C_RDS_WLINE 0x14
76 #define BCM2048_I2C_RDS_BLKB_MATCH0 0x16
77 #define BCM2048_I2C_RDS_BLKB_MATCH1 0x17
78 #define BCM2048_I2C_RDS_BLKB_MASK0 0x18
79 #define BCM2048_I2C_RDS_BLKB_MASK1 0x19
80 #define BCM2048_I2C_RDS_PI_MATCH0 0x1a
81 #define BCM2048_I2C_RDS_PI_MATCH1 0x1b
82 #define BCM2048_I2C_RDS_PI_MASK0 0x1c
83 #define BCM2048_I2C_RDS_PI_MASK1 0x1d
84 #define BCM2048_I2C_SPARE1 0x20
85 #define BCM2048_I2C_SPARE2 0x21
86 #define BCM2048_I2C_FM_RDS_REV 0x28
87 #define BCM2048_I2C_SLAVE_CONFIGURATION 0x29
88 #define BCM2048_I2C_RDS_DATA 0x80
89 #define BCM2048_I2C_FM_BEST_TUNE_MODE 0x90
91 /* BCM2048_I2C_FM_RDS_SYSTEM */
92 #define BCM2048_FM_ON 0x01
93 #define BCM2048_RDS_ON 0x02
95 /* BCM2048_I2C_FM_CTRL */
96 #define BCM2048_BAND_SELECT 0x01
97 #define BCM2048_STEREO_MONO_AUTO_SELECT 0x02
98 #define BCM2048_STEREO_MONO_MANUAL_SELECT 0x04
99 #define BCM2048_STEREO_MONO_BLEND_SWITCH 0x08
100 #define BCM2048_HI_LO_INJECTION 0x10
102 /* BCM2048_I2C_RDS_CTRL0 */
103 #define BCM2048_RBDS_RDS_SELECT 0x01
104 #define BCM2048_FLUSH_FIFO 0x02
106 /* BCM2048_I2C_FM_AUDIO_PAUSE */
107 #define BCM2048_AUDIO_PAUSE_RSSI_TRESH 0x0f
108 #define BCM2048_AUDIO_PAUSE_DURATION 0xf0
110 /* BCM2048_I2C_FM_AUDIO_CTRL0 */
111 #define BCM2048_RF_MUTE 0x01
112 #define BCM2048_MANUAL_MUTE 0x02
113 #define BCM2048_DAC_OUTPUT_LEFT 0x04
114 #define BCM2048_DAC_OUTPUT_RIGHT 0x08
115 #define BCM2048_AUDIO_ROUTE_DAC 0x10
116 #define BCM2048_AUDIO_ROUTE_I2S 0x20
117 #define BCM2048_DE_EMPHASIS_SELECT 0x40
118 #define BCM2048_AUDIO_BANDWIDTH_SELECT 0x80
120 /* BCM2048_I2C_FM_SEARCH_CTRL0 */
121 #define BCM2048_SEARCH_RSSI_THRESHOLD 0x7f
122 #define BCM2048_SEARCH_DIRECTION 0x80
124 /* BCM2048_I2C_FM_SEARCH_TUNE_MODE */
125 #define BCM2048_FM_AUTO_SEARCH 0x03
127 /* BCM2048_I2C_FM_RSSI */
128 #define BCM2048_RSSI_VALUE 0xff
130 /* BCM2048_I2C_FM_RDS_MASK0 */
131 /* BCM2048_I2C_FM_RDS_MASK1 */
132 #define BCM2048_FM_FLAG_SEARCH_TUNE_FINISHED 0x01
133 #define BCM2048_FM_FLAG_SEARCH_TUNE_FAIL 0x02
134 #define BCM2048_FM_FLAG_RSSI_LOW 0x04
135 #define BCM2048_FM_FLAG_CARRIER_ERROR_HIGH 0x08
136 #define BCM2048_FM_FLAG_AUDIO_PAUSE_INDICATION 0x10
137 #define BCM2048_FLAG_STEREO_DETECTED 0x20
138 #define BCM2048_FLAG_STEREO_ACTIVE 0x40
140 /* BCM2048_I2C_RDS_DATA */
141 #define BCM2048_SLAVE_ADDRESS 0x3f
142 #define BCM2048_SLAVE_ENABLE 0x80
144 /* BCM2048_I2C_FM_BEST_TUNE_MODE */
145 #define BCM2048_BEST_TUNE_MODE 0x80
147 #define BCM2048_FM_FLAG_SEARCH_TUNE_FINISHED 0x01
148 #define BCM2048_FM_FLAG_SEARCH_TUNE_FAIL 0x02
149 #define BCM2048_FM_FLAG_RSSI_LOW 0x04
150 #define BCM2048_FM_FLAG_CARRIER_ERROR_HIGH 0x08
151 #define BCM2048_FM_FLAG_AUDIO_PAUSE_INDICATION 0x10
152 #define BCM2048_FLAG_STEREO_DETECTED 0x20
153 #define BCM2048_FLAG_STEREO_ACTIVE 0x40
155 #define BCM2048_RDS_FLAG_FIFO_WLINE 0x02
156 #define BCM2048_RDS_FLAG_B_BLOCK_MATCH 0x08
157 #define BCM2048_RDS_FLAG_SYNC_LOST 0x10
158 #define BCM2048_RDS_FLAG_PI_MATCH 0x20
160 #define BCM2048_RDS_MARK_END_BYTE0 0x7C
161 #define BCM2048_RDS_MARK_END_BYTEN 0xFF
163 #define BCM2048_FM_FLAGS_ALL (FM_FLAG_SEARCH_TUNE_FINISHED | \
164 FM_FLAG_SEARCH_TUNE_FAIL | \
166 FM_FLAG_CARRIER_ERROR_HIGH | \
167 FM_FLAG_AUDIO_PAUSE_INDICATION | \
168 FLAG_STEREO_DETECTED | FLAG_STEREO_ACTIVE)
170 #define BCM2048_RDS_FLAGS_ALL (RDS_FLAG_FIFO_WLINE | \
171 RDS_FLAG_B_BLOCK_MATCH | \
172 RDS_FLAG_SYNC_LOST | RDS_FLAG_PI_MATCH)
174 #define BCM2048_DEFAULT_TIMEOUT 1500
175 #define BCM2048_AUTO_SEARCH_TIMEOUT 3000
177 #define BCM2048_FREQDEV_UNIT 10000
178 #define BCM2048_FREQV4L2_MULTI 625
179 #define dev_to_v4l2(f) (((f) * BCM2048_FREQDEV_UNIT) / BCM2048_FREQV4L2_MULTI)
180 #define v4l2_to_dev(f) (((f) * BCM2048_FREQV4L2_MULTI) / BCM2048_FREQDEV_UNIT)
182 #define msb(x) ((u8)((u16)(x) >> 8))
183 #define lsb(x) ((u8)((u16)(x) & 0x00FF))
184 #define compose_u16(msb, lsb) (((u16)(msb) << 8) | (lsb))
186 #define BCM2048_DEFAULT_POWERING_DELAY 20
187 #define BCM2048_DEFAULT_REGION 0x02
188 #define BCM2048_DEFAULT_MUTE 0x01
189 #define BCM2048_DEFAULT_RSSI_THRESHOLD 0x64
190 #define BCM2048_DEFAULT_RDS_WLINE 0x7E
192 #define BCM2048_FM_SEARCH_INACTIVE 0x00
193 #define BCM2048_FM_PRE_SET_MODE 0x01
194 #define BCM2048_FM_AUTO_SEARCH_MODE 0x02
195 #define BCM2048_FM_AF_JUMP_MODE 0x03
197 #define BCM2048_FREQUENCY_BASE 64000
199 #define BCM2048_POWER_ON 0x01
200 #define BCM2048_POWER_OFF 0x00
202 #define BCM2048_ITEM_ENABLED 0x01
203 #define BCM2048_SEARCH_DIRECTION_UP 0x01
205 #define BCM2048_DE_EMPHASIS_75us 75
206 #define BCM2048_DE_EMPHASIS_50us 50
208 #define BCM2048_SCAN_FAIL 0x00
209 #define BCM2048_SCAN_OK 0x01
211 #define BCM2048_FREQ_ERROR_FLOOR -20
212 #define BCM2048_FREQ_ERROR_ROOF 20
214 /* -60 dB is reported as full signal strength */
215 #define BCM2048_RSSI_LEVEL_BASE -60
216 #define BCM2048_RSSI_LEVEL_ROOF -100
217 #define BCM2048_RSSI_LEVEL_ROOF_NEG 100
218 #define BCM2048_SIGNAL_MULTIPLIER (0xFFFF / \
219 (BCM2048_RSSI_LEVEL_ROOF_NEG + \
220 BCM2048_RSSI_LEVEL_BASE))
222 #define BCM2048_RDS_FIFO_DUPLE_SIZE 0x03
223 #define BCM2048_RDS_CRC_MASK 0x0F
224 #define BCM2048_RDS_CRC_NONE 0x00
225 #define BCM2048_RDS_CRC_MAX_2BITS 0x04
226 #define BCM2048_RDS_CRC_LEAST_2BITS 0x08
227 #define BCM2048_RDS_CRC_UNRECOVARABLE 0x0C
229 #define BCM2048_RDS_BLOCK_MASK 0xF0
230 #define BCM2048_RDS_BLOCK_A 0x00
231 #define BCM2048_RDS_BLOCK_B 0x10
232 #define BCM2048_RDS_BLOCK_C 0x20
233 #define BCM2048_RDS_BLOCK_D 0x30
234 #define BCM2048_RDS_BLOCK_C_SCORED 0x40
235 #define BCM2048_RDS_BLOCK_E 0x60
237 #define BCM2048_RDS_RT 0x20
238 #define BCM2048_RDS_PS 0x00
240 #define BCM2048_RDS_GROUP_AB_MASK 0x08
241 #define BCM2048_RDS_GROUP_A 0x00
242 #define BCM2048_RDS_GROUP_B 0x08
244 #define BCM2048_RDS_RT_AB_MASK 0x10
245 #define BCM2048_RDS_RT_A 0x00
246 #define BCM2048_RDS_RT_B 0x10
247 #define BCM2048_RDS_RT_INDEX 0x0F
249 #define BCM2048_RDS_PS_INDEX 0x03
253 #define BCM2048_MAX_RDS_RT (64 + 1)
254 u8 rds_rt[BCM2048_MAX_RDS_RT];
257 #define BCM2048_MAX_RDS_PS (8 + 1)
258 u8 rds_ps[BCM2048_MAX_RDS_PS];
261 #define BCM2048_MAX_RDS_RADIO_TEXT 255
262 u8 radio_text[BCM2048_MAX_RDS_RADIO_TEXT + 3];
267 u32 bottom_frequency;
274 struct bcm2048_device {
275 struct i2c_client *client;
276 struct video_device videodev;
277 struct work_struct work;
278 struct completion compl;
280 struct bcm2048_platform_data *platform_data;
281 struct rds_info rds_info;
282 struct region_info region_info;
284 u8 cache_fm_rds_system;
286 u8 cache_fm_audio_ctrl0;
287 u8 cache_fm_search_ctrl0;
294 /* for rds data device read */
295 wait_queue_head_t read_queue;
297 unsigned char rds_data_available;
298 unsigned int rd_index;
301 static int radio_nr = -1; /* radio device minor (-1 ==> auto assign) */
302 module_param(radio_nr, int, 0000);
303 MODULE_PARM_DESC(radio_nr,
304 "Minor number for radio device (-1 ==> auto assign)");
306 static const struct region_info region_configs[] = {
309 .channel_spacing = 20,
310 .bottom_frequency = 87500,
311 .top_frequency = 108000,
317 .channel_spacing = 20,
318 .bottom_frequency = 87500,
319 .top_frequency = 108000,
325 .channel_spacing = 10,
326 .bottom_frequency = 87500,
327 .top_frequency = 108000,
333 .channel_spacing = 10,
334 .bottom_frequency = 76000,
335 .top_frequency = 90000,
342 * I2C Interface read / write
344 static int bcm2048_send_command(struct bcm2048_device *bdev, unsigned int reg,
347 struct i2c_client *client = bdev->client;
350 if (!bdev->power_state) {
351 dev_err(&bdev->client->dev, "bcm2048: chip not powered!\n");
355 data[0] = reg & 0xff;
356 data[1] = value & 0xff;
358 if (i2c_master_send(client, data, 2) == 2)
361 dev_err(&bdev->client->dev, "BCM I2C error!\n");
362 dev_err(&bdev->client->dev, "Is Bluetooth up and running?\n");
366 static int bcm2048_recv_command(struct bcm2048_device *bdev, unsigned int reg,
369 struct i2c_client *client = bdev->client;
371 if (!bdev->power_state) {
372 dev_err(&bdev->client->dev, "bcm2048: chip not powered!\n");
376 value[0] = i2c_smbus_read_byte_data(client, reg & 0xff);
381 static int bcm2048_recv_duples(struct bcm2048_device *bdev, unsigned int reg,
382 u8 *value, u8 duples)
384 struct i2c_client *client = bdev->client;
385 struct i2c_adapter *adap = client->adapter;
386 struct i2c_msg msg[2];
389 if (!bdev->power_state) {
390 dev_err(&bdev->client->dev, "bcm2048: chip not powered!\n");
396 msg[0].addr = client->addr;
397 msg[0].flags = client->flags & I2C_M_TEN;
401 msg[1].addr = client->addr;
402 msg[1].flags = client->flags & I2C_M_TEN;
403 msg[1].flags |= I2C_M_RD;
407 return i2c_transfer(adap, msg, 2);
411 * BCM2048 - I2C register programming helpers
413 static int bcm2048_set_power_state(struct bcm2048_device *bdev, u8 power)
417 mutex_lock(&bdev->mutex);
420 bdev->power_state = BCM2048_POWER_ON;
421 bdev->cache_fm_rds_system |= BCM2048_FM_ON;
423 bdev->cache_fm_rds_system &= ~BCM2048_FM_ON;
427 * Warning! FM cannot be turned off because then
428 * the I2C communications get ruined!
429 * Comment off the "if (power)" when the chip works!
432 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_SYSTEM,
433 bdev->cache_fm_rds_system);
434 msleep(BCM2048_DEFAULT_POWERING_DELAY);
437 bdev->power_state = BCM2048_POWER_OFF;
439 mutex_unlock(&bdev->mutex);
443 static int bcm2048_get_power_state(struct bcm2048_device *bdev)
448 mutex_lock(&bdev->mutex);
450 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_SYSTEM, &value);
452 mutex_unlock(&bdev->mutex);
454 if (!err && (value & BCM2048_FM_ON))
455 return BCM2048_POWER_ON;
460 static int bcm2048_set_rds_no_lock(struct bcm2048_device *bdev, u8 rds_on)
465 bdev->cache_fm_rds_system &= ~BCM2048_RDS_ON;
468 bdev->cache_fm_rds_system |= BCM2048_RDS_ON;
469 bdev->rds_state = BCM2048_RDS_ON;
470 flags = BCM2048_RDS_FLAG_FIFO_WLINE;
471 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK1,
476 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK1,
478 memset(&bdev->rds_info, 0, sizeof(bdev->rds_info));
483 return bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_SYSTEM,
484 bdev->cache_fm_rds_system);
487 static int bcm2048_get_rds_no_lock(struct bcm2048_device *bdev)
492 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_SYSTEM, &value);
494 if (!err && (value & BCM2048_RDS_ON))
495 return BCM2048_ITEM_ENABLED;
500 static int bcm2048_set_rds(struct bcm2048_device *bdev, u8 rds_on)
504 mutex_lock(&bdev->mutex);
506 err = bcm2048_set_rds_no_lock(bdev, rds_on);
508 mutex_unlock(&bdev->mutex);
512 static int bcm2048_get_rds(struct bcm2048_device *bdev)
516 mutex_lock(&bdev->mutex);
518 err = bcm2048_get_rds_no_lock(bdev);
520 mutex_unlock(&bdev->mutex);
524 static int bcm2048_get_rds_pi(struct bcm2048_device *bdev)
526 return bdev->rds_info.rds_pi;
529 static int bcm2048_set_fm_automatic_stereo_mono(struct bcm2048_device *bdev,
534 mutex_lock(&bdev->mutex);
536 bdev->cache_fm_ctrl &= ~BCM2048_STEREO_MONO_AUTO_SELECT;
539 bdev->cache_fm_ctrl |= BCM2048_STEREO_MONO_AUTO_SELECT;
541 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_CTRL,
542 bdev->cache_fm_ctrl);
544 mutex_unlock(&bdev->mutex);
548 static int bcm2048_set_fm_hi_lo_injection(struct bcm2048_device *bdev,
553 mutex_lock(&bdev->mutex);
555 bdev->cache_fm_ctrl &= ~BCM2048_HI_LO_INJECTION;
558 bdev->cache_fm_ctrl |= BCM2048_HI_LO_INJECTION;
560 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_CTRL,
561 bdev->cache_fm_ctrl);
563 mutex_unlock(&bdev->mutex);
567 static int bcm2048_get_fm_hi_lo_injection(struct bcm2048_device *bdev)
572 mutex_lock(&bdev->mutex);
574 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_CTRL, &value);
576 mutex_unlock(&bdev->mutex);
578 if (!err && (value & BCM2048_HI_LO_INJECTION))
579 return BCM2048_ITEM_ENABLED;
584 static int bcm2048_set_fm_frequency(struct bcm2048_device *bdev, u32 frequency)
588 if (frequency < bdev->region_info.bottom_frequency ||
589 frequency > bdev->region_info.top_frequency)
592 frequency -= BCM2048_FREQUENCY_BASE;
594 mutex_lock(&bdev->mutex);
596 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_FREQ0, lsb(frequency));
597 err |= bcm2048_send_command(bdev, BCM2048_I2C_FM_FREQ1,
601 bdev->frequency = frequency;
603 mutex_unlock(&bdev->mutex);
607 static int bcm2048_get_fm_frequency(struct bcm2048_device *bdev)
612 mutex_lock(&bdev->mutex);
614 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_FREQ0, &lsb);
615 err |= bcm2048_recv_command(bdev, BCM2048_I2C_FM_FREQ1, &msb);
617 mutex_unlock(&bdev->mutex);
622 err = compose_u16(msb, lsb);
623 err += BCM2048_FREQUENCY_BASE;
628 static int bcm2048_set_fm_af_frequency(struct bcm2048_device *bdev,
633 if (frequency < bdev->region_info.bottom_frequency ||
634 frequency > bdev->region_info.top_frequency)
637 frequency -= BCM2048_FREQUENCY_BASE;
639 mutex_lock(&bdev->mutex);
641 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_AF_FREQ0,
643 err |= bcm2048_send_command(bdev, BCM2048_I2C_FM_AF_FREQ1,
646 bdev->frequency = frequency;
648 mutex_unlock(&bdev->mutex);
652 static int bcm2048_get_fm_af_frequency(struct bcm2048_device *bdev)
657 mutex_lock(&bdev->mutex);
659 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_AF_FREQ0, &lsb);
660 err |= bcm2048_recv_command(bdev, BCM2048_I2C_FM_AF_FREQ1, &msb);
662 mutex_unlock(&bdev->mutex);
667 err = compose_u16(msb, lsb);
668 err += BCM2048_FREQUENCY_BASE;
673 static int bcm2048_set_fm_deemphasis(struct bcm2048_device *bdev, int d)
678 if (d == BCM2048_DE_EMPHASIS_75us)
679 deemphasis = BCM2048_DE_EMPHASIS_SELECT;
683 mutex_lock(&bdev->mutex);
685 bdev->cache_fm_audio_ctrl0 &= ~BCM2048_DE_EMPHASIS_SELECT;
686 bdev->cache_fm_audio_ctrl0 |= deemphasis;
688 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0,
689 bdev->cache_fm_audio_ctrl0);
692 bdev->region_info.deemphasis = d;
694 mutex_unlock(&bdev->mutex);
699 static int bcm2048_get_fm_deemphasis(struct bcm2048_device *bdev)
704 mutex_lock(&bdev->mutex);
706 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0, &value);
708 mutex_unlock(&bdev->mutex);
711 if (value & BCM2048_DE_EMPHASIS_SELECT)
712 return BCM2048_DE_EMPHASIS_75us;
714 return BCM2048_DE_EMPHASIS_50us;
720 static int bcm2048_set_region(struct bcm2048_device *bdev, u8 region)
723 u32 new_frequency = 0;
725 if (region >= ARRAY_SIZE(region_configs))
728 mutex_lock(&bdev->mutex);
729 bdev->region_info = region_configs[region];
731 if (region_configs[region].bottom_frequency < 87500)
732 bdev->cache_fm_ctrl |= BCM2048_BAND_SELECT;
734 bdev->cache_fm_ctrl &= ~BCM2048_BAND_SELECT;
736 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_CTRL,
737 bdev->cache_fm_ctrl);
739 mutex_unlock(&bdev->mutex);
742 mutex_unlock(&bdev->mutex);
744 if (bdev->frequency < region_configs[region].bottom_frequency ||
745 bdev->frequency > region_configs[region].top_frequency)
746 new_frequency = region_configs[region].bottom_frequency;
748 if (new_frequency > 0) {
749 err = bcm2048_set_fm_frequency(bdev, new_frequency);
755 err = bcm2048_set_fm_deemphasis(bdev,
756 region_configs[region].deemphasis);
762 static int bcm2048_get_region(struct bcm2048_device *bdev)
766 mutex_lock(&bdev->mutex);
767 err = bdev->region_info.region;
768 mutex_unlock(&bdev->mutex);
773 static int bcm2048_set_mute(struct bcm2048_device *bdev, u16 mute)
777 mutex_lock(&bdev->mutex);
779 bdev->cache_fm_audio_ctrl0 &= ~(BCM2048_RF_MUTE | BCM2048_MANUAL_MUTE);
782 bdev->cache_fm_audio_ctrl0 |= (BCM2048_RF_MUTE |
783 BCM2048_MANUAL_MUTE);
785 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0,
786 bdev->cache_fm_audio_ctrl0);
789 bdev->mute_state = mute;
791 mutex_unlock(&bdev->mutex);
795 static int bcm2048_get_mute(struct bcm2048_device *bdev)
800 mutex_lock(&bdev->mutex);
802 if (bdev->power_state) {
803 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0,
806 err = value & (BCM2048_RF_MUTE | BCM2048_MANUAL_MUTE);
808 err = bdev->mute_state;
811 mutex_unlock(&bdev->mutex);
815 static int bcm2048_set_audio_route(struct bcm2048_device *bdev, u8 route)
819 mutex_lock(&bdev->mutex);
821 route &= (BCM2048_AUDIO_ROUTE_DAC | BCM2048_AUDIO_ROUTE_I2S);
822 bdev->cache_fm_audio_ctrl0 &= ~(BCM2048_AUDIO_ROUTE_DAC |
823 BCM2048_AUDIO_ROUTE_I2S);
824 bdev->cache_fm_audio_ctrl0 |= route;
826 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0,
827 bdev->cache_fm_audio_ctrl0);
829 mutex_unlock(&bdev->mutex);
833 static int bcm2048_get_audio_route(struct bcm2048_device *bdev)
838 mutex_lock(&bdev->mutex);
840 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0, &value);
842 mutex_unlock(&bdev->mutex);
845 return value & (BCM2048_AUDIO_ROUTE_DAC |
846 BCM2048_AUDIO_ROUTE_I2S);
851 static int bcm2048_set_dac_output(struct bcm2048_device *bdev, u8 channels)
855 mutex_lock(&bdev->mutex);
857 bdev->cache_fm_audio_ctrl0 &= ~(BCM2048_DAC_OUTPUT_LEFT |
858 BCM2048_DAC_OUTPUT_RIGHT);
859 bdev->cache_fm_audio_ctrl0 |= channels;
861 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0,
862 bdev->cache_fm_audio_ctrl0);
864 mutex_unlock(&bdev->mutex);
868 static int bcm2048_get_dac_output(struct bcm2048_device *bdev)
873 mutex_lock(&bdev->mutex);
875 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0, &value);
877 mutex_unlock(&bdev->mutex);
880 return value & (BCM2048_DAC_OUTPUT_LEFT |
881 BCM2048_DAC_OUTPUT_RIGHT);
886 static int bcm2048_set_fm_search_rssi_threshold(struct bcm2048_device *bdev,
891 mutex_lock(&bdev->mutex);
893 threshold &= BCM2048_SEARCH_RSSI_THRESHOLD;
894 bdev->cache_fm_search_ctrl0 &= ~BCM2048_SEARCH_RSSI_THRESHOLD;
895 bdev->cache_fm_search_ctrl0 |= threshold;
897 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_SEARCH_CTRL0,
898 bdev->cache_fm_search_ctrl0);
900 mutex_unlock(&bdev->mutex);
904 static int bcm2048_get_fm_search_rssi_threshold(struct bcm2048_device *bdev)
909 mutex_lock(&bdev->mutex);
911 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_SEARCH_CTRL0, &value);
913 mutex_unlock(&bdev->mutex);
916 return value & BCM2048_SEARCH_RSSI_THRESHOLD;
921 static int bcm2048_set_fm_search_mode_direction(struct bcm2048_device *bdev,
926 mutex_lock(&bdev->mutex);
928 bdev->cache_fm_search_ctrl0 &= ~BCM2048_SEARCH_DIRECTION;
931 bdev->cache_fm_search_ctrl0 |= BCM2048_SEARCH_DIRECTION;
933 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_SEARCH_CTRL0,
934 bdev->cache_fm_search_ctrl0);
936 mutex_unlock(&bdev->mutex);
940 static int bcm2048_get_fm_search_mode_direction(struct bcm2048_device *bdev)
945 mutex_lock(&bdev->mutex);
947 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_SEARCH_CTRL0, &value);
949 mutex_unlock(&bdev->mutex);
951 if (!err && (value & BCM2048_SEARCH_DIRECTION))
952 return BCM2048_SEARCH_DIRECTION_UP;
957 static int bcm2048_set_fm_search_tune_mode(struct bcm2048_device *bdev,
960 int err, timeout, restart_rds = 0;
963 value = mode & BCM2048_FM_AUTO_SEARCH;
965 flags = BCM2048_FM_FLAG_SEARCH_TUNE_FINISHED |
966 BCM2048_FM_FLAG_SEARCH_TUNE_FAIL;
968 mutex_lock(&bdev->mutex);
971 * If RDS is enabled, and frequency is changed, RDS quits working.
972 * Thus, always restart RDS if it's enabled. Moreover, RDS must
973 * not be enabled while changing the frequency because it can
974 * provide a race to the mutex from the workqueue handler if RDS
975 * IRQ occurs while waiting for frequency changed IRQ.
977 if (bcm2048_get_rds_no_lock(bdev)) {
978 err = bcm2048_set_rds_no_lock(bdev, 0);
984 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK0, flags);
989 bcm2048_send_command(bdev, BCM2048_I2C_FM_SEARCH_TUNE_MODE, value);
991 if (mode != BCM2048_FM_AUTO_SEARCH_MODE)
992 timeout = BCM2048_DEFAULT_TIMEOUT;
994 timeout = BCM2048_AUTO_SEARCH_TIMEOUT;
996 if (!wait_for_completion_timeout(&bdev->compl,
997 msecs_to_jiffies(timeout)))
998 dev_err(&bdev->client->dev, "IRQ timeout.\n");
1001 if (!bdev->scan_state)
1006 err |= bcm2048_set_rds_no_lock(bdev, 1);
1008 mutex_unlock(&bdev->mutex);
1013 static int bcm2048_get_fm_search_tune_mode(struct bcm2048_device *bdev)
1018 mutex_lock(&bdev->mutex);
1020 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_SEARCH_TUNE_MODE,
1023 mutex_unlock(&bdev->mutex);
1026 return value & BCM2048_FM_AUTO_SEARCH;
1031 static int bcm2048_set_rds_b_block_mask(struct bcm2048_device *bdev, u16 mask)
1035 mutex_lock(&bdev->mutex);
1037 err = bcm2048_send_command(bdev, BCM2048_I2C_RDS_BLKB_MASK0,
1039 err |= bcm2048_send_command(bdev, BCM2048_I2C_RDS_BLKB_MASK1,
1042 mutex_unlock(&bdev->mutex);
1046 static int bcm2048_get_rds_b_block_mask(struct bcm2048_device *bdev)
1049 u8 lsb = 0, msb = 0;
1051 mutex_lock(&bdev->mutex);
1053 err = bcm2048_recv_command(bdev, BCM2048_I2C_RDS_BLKB_MASK0, &lsb);
1054 err |= bcm2048_recv_command(bdev, BCM2048_I2C_RDS_BLKB_MASK1, &msb);
1056 mutex_unlock(&bdev->mutex);
1059 return compose_u16(msb, lsb);
1064 static int bcm2048_set_rds_b_block_match(struct bcm2048_device *bdev,
1069 mutex_lock(&bdev->mutex);
1071 err = bcm2048_send_command(bdev, BCM2048_I2C_RDS_BLKB_MATCH0,
1073 err |= bcm2048_send_command(bdev, BCM2048_I2C_RDS_BLKB_MATCH1,
1076 mutex_unlock(&bdev->mutex);
1080 static int bcm2048_get_rds_b_block_match(struct bcm2048_device *bdev)
1083 u8 lsb = 0, msb = 0;
1085 mutex_lock(&bdev->mutex);
1087 err = bcm2048_recv_command(bdev, BCM2048_I2C_RDS_BLKB_MATCH0, &lsb);
1088 err |= bcm2048_recv_command(bdev, BCM2048_I2C_RDS_BLKB_MATCH1, &msb);
1090 mutex_unlock(&bdev->mutex);
1093 return compose_u16(msb, lsb);
1098 static int bcm2048_set_rds_pi_mask(struct bcm2048_device *bdev, u16 mask)
1102 mutex_lock(&bdev->mutex);
1104 err = bcm2048_send_command(bdev, BCM2048_I2C_RDS_PI_MASK0, lsb(mask));
1105 err |= bcm2048_send_command(bdev, BCM2048_I2C_RDS_PI_MASK1, msb(mask));
1107 mutex_unlock(&bdev->mutex);
1111 static int bcm2048_get_rds_pi_mask(struct bcm2048_device *bdev)
1114 u8 lsb = 0, msb = 0;
1116 mutex_lock(&bdev->mutex);
1118 err = bcm2048_recv_command(bdev, BCM2048_I2C_RDS_PI_MASK0, &lsb);
1119 err |= bcm2048_recv_command(bdev, BCM2048_I2C_RDS_PI_MASK1, &msb);
1121 mutex_unlock(&bdev->mutex);
1124 return compose_u16(msb, lsb);
1129 static int bcm2048_set_rds_pi_match(struct bcm2048_device *bdev, u16 match)
1133 mutex_lock(&bdev->mutex);
1135 err = bcm2048_send_command(bdev, BCM2048_I2C_RDS_PI_MATCH0,
1137 err |= bcm2048_send_command(bdev, BCM2048_I2C_RDS_PI_MATCH1,
1140 mutex_unlock(&bdev->mutex);
1144 static int bcm2048_get_rds_pi_match(struct bcm2048_device *bdev)
1147 u8 lsb = 0, msb = 0;
1149 mutex_lock(&bdev->mutex);
1151 err = bcm2048_recv_command(bdev, BCM2048_I2C_RDS_PI_MATCH0, &lsb);
1152 err |= bcm2048_recv_command(bdev, BCM2048_I2C_RDS_PI_MATCH1, &msb);
1154 mutex_unlock(&bdev->mutex);
1157 return compose_u16(msb, lsb);
1162 static int bcm2048_set_fm_rds_mask(struct bcm2048_device *bdev, u16 mask)
1166 mutex_lock(&bdev->mutex);
1168 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK0, lsb(mask));
1169 err |= bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK1, msb(mask));
1171 mutex_unlock(&bdev->mutex);
1175 static int bcm2048_get_fm_rds_mask(struct bcm2048_device *bdev)
1178 u8 value0 = 0, value1 = 0;
1180 mutex_lock(&bdev->mutex);
1182 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_MASK0, &value0);
1183 err |= bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_MASK1, &value1);
1185 mutex_unlock(&bdev->mutex);
1188 return compose_u16(value1, value0);
1193 static int bcm2048_get_fm_rds_flags(struct bcm2048_device *bdev)
1196 u8 value0 = 0, value1 = 0;
1198 mutex_lock(&bdev->mutex);
1200 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_FLAG0, &value0);
1201 err |= bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_FLAG1, &value1);
1203 mutex_unlock(&bdev->mutex);
1206 return compose_u16(value1, value0);
1211 static int bcm2048_get_region_bottom_frequency(struct bcm2048_device *bdev)
1213 return bdev->region_info.bottom_frequency;
1216 static int bcm2048_get_region_top_frequency(struct bcm2048_device *bdev)
1218 return bdev->region_info.top_frequency;
1221 static int bcm2048_set_fm_best_tune_mode(struct bcm2048_device *bdev, u8 mode)
1226 mutex_lock(&bdev->mutex);
1228 /* Perform read as the manual indicates */
1229 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_BEST_TUNE_MODE,
1231 value &= ~BCM2048_BEST_TUNE_MODE;
1234 value |= BCM2048_BEST_TUNE_MODE;
1235 err |= bcm2048_send_command(bdev, BCM2048_I2C_FM_BEST_TUNE_MODE,
1238 mutex_unlock(&bdev->mutex);
1242 static int bcm2048_get_fm_best_tune_mode(struct bcm2048_device *bdev)
1247 mutex_lock(&bdev->mutex);
1249 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_BEST_TUNE_MODE,
1252 mutex_unlock(&bdev->mutex);
1254 if (!err && (value & BCM2048_BEST_TUNE_MODE))
1255 return BCM2048_ITEM_ENABLED;
1260 static int bcm2048_get_fm_carrier_error(struct bcm2048_device *bdev)
1265 mutex_lock(&bdev->mutex);
1266 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_CARRIER, &value);
1267 mutex_unlock(&bdev->mutex);
1275 static int bcm2048_get_fm_rssi(struct bcm2048_device *bdev)
1280 mutex_lock(&bdev->mutex);
1281 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RSSI, &value);
1282 mutex_unlock(&bdev->mutex);
1290 static int bcm2048_set_rds_wline(struct bcm2048_device *bdev, u8 wline)
1294 mutex_lock(&bdev->mutex);
1296 err = bcm2048_send_command(bdev, BCM2048_I2C_RDS_WLINE, wline);
1299 bdev->fifo_size = wline;
1301 mutex_unlock(&bdev->mutex);
1305 static int bcm2048_get_rds_wline(struct bcm2048_device *bdev)
1310 mutex_lock(&bdev->mutex);
1312 err = bcm2048_recv_command(bdev, BCM2048_I2C_RDS_WLINE, &value);
1314 mutex_unlock(&bdev->mutex);
1317 bdev->fifo_size = value;
1324 static int bcm2048_checkrev(struct bcm2048_device *bdev)
1329 mutex_lock(&bdev->mutex);
1331 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_REV, &version);
1333 mutex_unlock(&bdev->mutex);
1336 dev_info(&bdev->client->dev, "BCM2048 Version 0x%x\n",
1344 static int bcm2048_get_rds_rt(struct bcm2048_device *bdev, char *data)
1346 int err = 0, i, j = 0, ce = 0, cr = 0;
1347 char data_buffer[BCM2048_MAX_RDS_RT + 1];
1349 mutex_lock(&bdev->mutex);
1351 if (!bdev->rds_info.text_len) {
1356 for (i = 0; i < BCM2048_MAX_RDS_RT; i++) {
1357 if (bdev->rds_info.rds_rt[i]) {
1359 /* Skip the carriage return */
1360 if (bdev->rds_info.rds_rt[i] != 0x0d) {
1361 data_buffer[j++] = bdev->rds_info.rds_rt[i];
1369 if (j <= BCM2048_MAX_RDS_RT)
1372 for (i = 0; i < BCM2048_MAX_RDS_RT; i++) {
1373 if (!bdev->rds_info.rds_rt[i]) {
1374 if (cr && (i < cr)) {
1379 if (cr && (i >= cr))
1387 memcpy(data, data_buffer, sizeof(data_buffer));
1390 mutex_unlock(&bdev->mutex);
1394 static int bcm2048_get_rds_ps(struct bcm2048_device *bdev, char *data)
1396 int err = 0, i, j = 0;
1397 char data_buffer[BCM2048_MAX_RDS_PS + 1];
1399 mutex_lock(&bdev->mutex);
1401 if (!bdev->rds_info.text_len) {
1406 for (i = 0; i < BCM2048_MAX_RDS_PS; i++) {
1407 if (bdev->rds_info.rds_ps[i]) {
1408 data_buffer[j++] = bdev->rds_info.rds_ps[i];
1410 if (i < (BCM2048_MAX_RDS_PS - 1)) {
1417 if (j <= BCM2048_MAX_RDS_PS)
1420 memcpy(data, data_buffer, sizeof(data_buffer));
1423 mutex_unlock(&bdev->mutex);
1427 static void bcm2048_parse_rds_pi(struct bcm2048_device *bdev)
1432 for (i = 0; i < bdev->fifo_size; i += BCM2048_RDS_FIFO_DUPLE_SIZE) {
1433 /* Block A match, only data without crc errors taken */
1434 if (bdev->rds_info.radio_text[i] == BCM2048_RDS_BLOCK_A) {
1435 pi = (bdev->rds_info.radio_text[i + 1] << 8) +
1436 bdev->rds_info.radio_text[i + 2];
1438 if (!bdev->rds_info.rds_pi) {
1439 bdev->rds_info.rds_pi = pi;
1442 if (pi != bdev->rds_info.rds_pi) {
1445 bdev->rds_info.rds_pi = pi;
1455 static int bcm2048_rds_block_crc(struct bcm2048_device *bdev, int i)
1457 return bdev->rds_info.radio_text[i] & BCM2048_RDS_CRC_MASK;
1460 static void bcm2048_parse_rds_rt_block(struct bcm2048_device *bdev, int i,
1463 /* Good data will overwrite poor data */
1465 if (!bdev->rds_info.rds_rt[index])
1466 bdev->rds_info.rds_rt[index] =
1467 bdev->rds_info.radio_text[i + 1];
1468 if (!bdev->rds_info.rds_rt[index + 1])
1469 bdev->rds_info.rds_rt[index + 1] =
1470 bdev->rds_info.radio_text[i + 2];
1472 bdev->rds_info.rds_rt[index] =
1473 bdev->rds_info.radio_text[i + 1];
1474 bdev->rds_info.rds_rt[index + 1] =
1475 bdev->rds_info.radio_text[i + 2];
1479 static int bcm2048_parse_rt_match_b(struct bcm2048_device *bdev, int i)
1481 int crc, rt_id, rt_group_b, rt_ab, index = 0;
1483 crc = bcm2048_rds_block_crc(bdev, i);
1485 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1488 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1489 BCM2048_RDS_BLOCK_B) {
1490 rt_id = bdev->rds_info.radio_text[i + 1] &
1491 BCM2048_RDS_BLOCK_MASK;
1492 rt_group_b = bdev->rds_info.radio_text[i + 1] &
1493 BCM2048_RDS_GROUP_AB_MASK;
1494 rt_ab = bdev->rds_info.radio_text[i + 2] &
1495 BCM2048_RDS_RT_AB_MASK;
1497 if (rt_group_b != bdev->rds_info.rds_rt_group_b) {
1498 memset(bdev->rds_info.rds_rt, 0,
1499 sizeof(bdev->rds_info.rds_rt));
1500 bdev->rds_info.rds_rt_group_b = rt_group_b;
1503 if (rt_id == BCM2048_RDS_RT) {
1504 /* A to B or (vice versa), means: clear screen */
1505 if (rt_ab != bdev->rds_info.rds_rt_ab) {
1506 memset(bdev->rds_info.rds_rt, 0,
1507 sizeof(bdev->rds_info.rds_rt));
1508 bdev->rds_info.rds_rt_ab = rt_ab;
1511 index = bdev->rds_info.radio_text[i + 2] &
1512 BCM2048_RDS_RT_INDEX;
1514 if (bdev->rds_info.rds_rt_group_b)
1526 static int bcm2048_parse_rt_match_c(struct bcm2048_device *bdev, int i,
1531 crc = bcm2048_rds_block_crc(bdev, i);
1533 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1536 if ((index + 2) >= BCM2048_MAX_RDS_RT) {
1537 dev_err(&bdev->client->dev,
1538 "Incorrect index = %d\n", index);
1542 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1543 BCM2048_RDS_BLOCK_C) {
1544 if (bdev->rds_info.rds_rt_group_b)
1546 bcm2048_parse_rds_rt_block(bdev, i, index, crc);
1553 static void bcm2048_parse_rt_match_d(struct bcm2048_device *bdev, int i,
1558 crc = bcm2048_rds_block_crc(bdev, i);
1560 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1563 if ((index + 4) >= BCM2048_MAX_RDS_RT) {
1564 dev_err(&bdev->client->dev,
1565 "Incorrect index = %d\n", index);
1569 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1570 BCM2048_RDS_BLOCK_D)
1571 bcm2048_parse_rds_rt_block(bdev, i, index + 2, crc);
1574 static void bcm2048_parse_rds_rt(struct bcm2048_device *bdev)
1576 int i, index = 0, crc, match_b = 0, match_c = 0, match_d = 0;
1578 for (i = 0; i < bdev->fifo_size; i += BCM2048_RDS_FIFO_DUPLE_SIZE) {
1581 index = bcm2048_parse_rt_match_b(bdev, i);
1582 if (index >= 0 && index <= (BCM2048_MAX_RDS_RT - 5))
1585 } else if (match_c) {
1587 if (bcm2048_parse_rt_match_c(bdev, i, index))
1590 } else if (match_d) {
1592 bcm2048_parse_rt_match_d(bdev, i, index);
1596 /* Skip erroneous blocks due to messed up A block altogether */
1597 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1598 BCM2048_RDS_BLOCK_A) {
1599 crc = bcm2048_rds_block_crc(bdev, i);
1600 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1602 /* Synchronize to a good RDS PI */
1603 if (((bdev->rds_info.radio_text[i + 1] << 8) +
1604 bdev->rds_info.radio_text[i + 2]) ==
1605 bdev->rds_info.rds_pi)
1611 static void bcm2048_parse_rds_ps_block(struct bcm2048_device *bdev, int i,
1614 /* Good data will overwrite poor data */
1616 if (!bdev->rds_info.rds_ps[index])
1617 bdev->rds_info.rds_ps[index] =
1618 bdev->rds_info.radio_text[i + 1];
1619 if (!bdev->rds_info.rds_ps[index + 1])
1620 bdev->rds_info.rds_ps[index + 1] =
1621 bdev->rds_info.radio_text[i + 2];
1623 bdev->rds_info.rds_ps[index] =
1624 bdev->rds_info.radio_text[i + 1];
1625 bdev->rds_info.rds_ps[index + 1] =
1626 bdev->rds_info.radio_text[i + 2];
1630 static int bcm2048_parse_ps_match_c(struct bcm2048_device *bdev, int i,
1635 crc = bcm2048_rds_block_crc(bdev, i);
1637 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1640 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1641 BCM2048_RDS_BLOCK_C)
1647 static void bcm2048_parse_ps_match_d(struct bcm2048_device *bdev, int i,
1652 crc = bcm2048_rds_block_crc(bdev, i);
1654 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1657 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1658 BCM2048_RDS_BLOCK_D)
1659 bcm2048_parse_rds_ps_block(bdev, i, index, crc);
1662 static int bcm2048_parse_ps_match_b(struct bcm2048_device *bdev, int i)
1664 int crc, index, ps_id, ps_group;
1666 crc = bcm2048_rds_block_crc(bdev, i);
1668 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1671 /* Block B Radio PS match */
1672 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1673 BCM2048_RDS_BLOCK_B) {
1674 ps_id = bdev->rds_info.radio_text[i + 1] &
1675 BCM2048_RDS_BLOCK_MASK;
1676 ps_group = bdev->rds_info.radio_text[i + 1] &
1677 BCM2048_RDS_GROUP_AB_MASK;
1680 * Poor RSSI will lead to RDS data corruption
1681 * So using 3 (same) sequential values to justify major changes
1683 if (ps_group != bdev->rds_info.rds_ps_group) {
1684 if (crc == BCM2048_RDS_CRC_NONE) {
1685 bdev->rds_info.rds_ps_group_cnt++;
1686 if (bdev->rds_info.rds_ps_group_cnt > 2) {
1687 bdev->rds_info.rds_ps_group = ps_group;
1688 bdev->rds_info.rds_ps_group_cnt = 0;
1689 dev_err(&bdev->client->dev,
1690 "RDS PS Group change!\n");
1695 bdev->rds_info.rds_ps_group_cnt = 0;
1699 if (ps_id == BCM2048_RDS_PS) {
1700 index = bdev->rds_info.radio_text[i + 2] &
1701 BCM2048_RDS_PS_INDEX;
1710 static void bcm2048_parse_rds_ps(struct bcm2048_device *bdev)
1712 int i, index = 0, crc, match_b = 0, match_c = 0, match_d = 0;
1714 for (i = 0; i < bdev->fifo_size; i += BCM2048_RDS_FIFO_DUPLE_SIZE) {
1717 index = bcm2048_parse_ps_match_b(bdev, i);
1718 if (index >= 0 && index < (BCM2048_MAX_RDS_PS - 1))
1721 } else if (match_c) {
1723 if (bcm2048_parse_ps_match_c(bdev, i, index))
1726 } else if (match_d) {
1728 bcm2048_parse_ps_match_d(bdev, i, index);
1732 /* Skip erroneous blocks due to messed up A block altogether */
1733 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1734 BCM2048_RDS_BLOCK_A) {
1735 crc = bcm2048_rds_block_crc(bdev, i);
1736 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1738 /* Synchronize to a good RDS PI */
1739 if (((bdev->rds_info.radio_text[i + 1] << 8) +
1740 bdev->rds_info.radio_text[i + 2]) ==
1741 bdev->rds_info.rds_pi)
1747 static void bcm2048_rds_fifo_receive(struct bcm2048_device *bdev)
1751 mutex_lock(&bdev->mutex);
1753 err = bcm2048_recv_duples(bdev, BCM2048_I2C_RDS_DATA,
1754 bdev->rds_info.radio_text, bdev->fifo_size);
1756 dev_err(&bdev->client->dev, "RDS Read problem\n");
1757 mutex_unlock(&bdev->mutex);
1761 bdev->rds_info.text_len = bdev->fifo_size;
1763 bcm2048_parse_rds_pi(bdev);
1764 bcm2048_parse_rds_rt(bdev);
1765 bcm2048_parse_rds_ps(bdev);
1767 mutex_unlock(&bdev->mutex);
1769 wake_up_interruptible(&bdev->read_queue);
1772 static int bcm2048_get_rds_data(struct bcm2048_device *bdev, char *data)
1774 int err = 0, i, p = 0;
1777 mutex_lock(&bdev->mutex);
1779 if (!bdev->rds_info.text_len) {
1784 data_buffer = kcalloc(BCM2048_MAX_RDS_RADIO_TEXT, 5, GFP_KERNEL);
1790 for (i = 0; i < bdev->rds_info.text_len; i++) {
1791 p += sprintf(data_buffer + p, "%x ",
1792 bdev->rds_info.radio_text[i]);
1795 memcpy(data, data_buffer, p);
1799 mutex_unlock(&bdev->mutex);
1804 * BCM2048 default initialization sequence
1806 static int bcm2048_init(struct bcm2048_device *bdev)
1810 err = bcm2048_set_power_state(bdev, BCM2048_POWER_ON);
1814 err = bcm2048_set_audio_route(bdev, BCM2048_AUDIO_ROUTE_DAC);
1818 err = bcm2048_set_dac_output(bdev, BCM2048_DAC_OUTPUT_LEFT |
1819 BCM2048_DAC_OUTPUT_RIGHT);
1826 * BCM2048 default deinitialization sequence
1828 static int bcm2048_deinit(struct bcm2048_device *bdev)
1832 err = bcm2048_set_audio_route(bdev, 0);
1836 err = bcm2048_set_dac_output(bdev, 0);
1840 return bcm2048_set_power_state(bdev, BCM2048_POWER_OFF);
1844 * BCM2048 probe sequence
1846 static int bcm2048_probe(struct bcm2048_device *bdev)
1850 err = bcm2048_set_power_state(bdev, BCM2048_POWER_ON);
1854 err = bcm2048_checkrev(bdev);
1858 err = bcm2048_set_mute(bdev, BCM2048_DEFAULT_MUTE);
1862 err = bcm2048_set_region(bdev, BCM2048_DEFAULT_REGION);
1866 err = bcm2048_set_fm_search_rssi_threshold(bdev,
1867 BCM2048_DEFAULT_RSSI_THRESHOLD);
1871 err = bcm2048_set_fm_automatic_stereo_mono(bdev, BCM2048_ITEM_ENABLED);
1875 err = bcm2048_get_rds_wline(bdev);
1876 if (err < BCM2048_DEFAULT_RDS_WLINE)
1877 err = bcm2048_set_rds_wline(bdev, BCM2048_DEFAULT_RDS_WLINE);
1881 err = bcm2048_set_power_state(bdev, BCM2048_POWER_OFF);
1883 init_waitqueue_head(&bdev->read_queue);
1884 bdev->rds_data_available = 0;
1893 * BCM2048 workqueue handler
1895 static void bcm2048_work(struct work_struct *work)
1897 struct bcm2048_device *bdev;
1898 u8 flag_lsb = 0, flag_msb = 0, flags;
1900 bdev = container_of(work, struct bcm2048_device, work);
1901 bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_FLAG0, &flag_lsb);
1902 bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_FLAG1, &flag_msb);
1904 if (flag_lsb & (BCM2048_FM_FLAG_SEARCH_TUNE_FINISHED |
1905 BCM2048_FM_FLAG_SEARCH_TUNE_FAIL)) {
1906 if (flag_lsb & BCM2048_FM_FLAG_SEARCH_TUNE_FAIL)
1907 bdev->scan_state = BCM2048_SCAN_FAIL;
1909 bdev->scan_state = BCM2048_SCAN_OK;
1911 complete(&bdev->compl);
1914 if (flag_msb & BCM2048_RDS_FLAG_FIFO_WLINE) {
1915 bcm2048_rds_fifo_receive(bdev);
1916 if (bdev->rds_state) {
1917 flags = BCM2048_RDS_FLAG_FIFO_WLINE;
1918 bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK1,
1921 bdev->rds_data_available = 1;
1922 bdev->rd_index = 0; /* new data, new start */
1927 * BCM2048 interrupt handler
1929 static irqreturn_t bcm2048_handler(int irq, void *dev)
1931 struct bcm2048_device *bdev = dev;
1933 dev_dbg(&bdev->client->dev, "IRQ called, queuing work\n");
1934 if (bdev->power_state)
1935 schedule_work(&bdev->work);
1941 * BCM2048 sysfs interface definitions
1943 #define property_write(prop, type, mask, check) \
1944 static ssize_t bcm2048_##prop##_write(struct device *dev, \
1945 struct device_attribute *attr, \
1949 struct bcm2048_device *bdev = dev_get_drvdata(dev); \
1956 if (sscanf(buf, mask, &value) != 1) \
1962 err = bcm2048_set_##prop(bdev, value); \
1964 return err < 0 ? err : count; \
1967 #define property_read(prop, mask) \
1968 static ssize_t bcm2048_##prop##_read(struct device *dev, \
1969 struct device_attribute *attr, \
1972 struct bcm2048_device *bdev = dev_get_drvdata(dev); \
1978 value = bcm2048_get_##prop(bdev); \
1981 value = sprintf(buf, mask "\n", value); \
1986 #define property_signed_read(prop, size, mask) \
1987 static ssize_t bcm2048_##prop##_read(struct device *dev, \
1988 struct device_attribute *attr, \
1991 struct bcm2048_device *bdev = dev_get_drvdata(dev); \
1997 value = bcm2048_get_##prop(bdev); \
1999 return sprintf(buf, mask "\n", value); \
2002 #define DEFINE_SYSFS_PROPERTY(prop, prop_type, mask, check) \
2003 property_write(prop, prop_type, mask, check) \
2004 property_read(prop, mask) \
2006 #define property_str_read(prop, size) \
2007 static ssize_t bcm2048_##prop##_read(struct device *dev, \
2008 struct device_attribute *attr, \
2011 struct bcm2048_device *bdev = dev_get_drvdata(dev); \
2018 out = kzalloc((size) + 1, GFP_KERNEL); \
2022 bcm2048_get_##prop(bdev, out); \
2023 count = sprintf(buf, "%s\n", out); \
2030 DEFINE_SYSFS_PROPERTY(power_state, unsigned int, "%u", 0)
2031 DEFINE_SYSFS_PROPERTY(mute, unsigned int, "%u", 0)
2032 DEFINE_SYSFS_PROPERTY(audio_route, unsigned int, "%u", 0)
2033 DEFINE_SYSFS_PROPERTY(dac_output, unsigned int, "%u", 0)
2035 DEFINE_SYSFS_PROPERTY(fm_hi_lo_injection, unsigned int, "%u", 0)
2036 DEFINE_SYSFS_PROPERTY(fm_frequency, unsigned int, "%u", 0)
2037 DEFINE_SYSFS_PROPERTY(fm_af_frequency, unsigned int, "%u", 0)
2038 DEFINE_SYSFS_PROPERTY(fm_deemphasis, unsigned int, "%u", 0)
2039 DEFINE_SYSFS_PROPERTY(fm_rds_mask, unsigned int, "%u", 0)
2040 DEFINE_SYSFS_PROPERTY(fm_best_tune_mode, unsigned int, "%u", 0)
2041 DEFINE_SYSFS_PROPERTY(fm_search_rssi_threshold, unsigned int, "%u", 0)
2042 DEFINE_SYSFS_PROPERTY(fm_search_mode_direction, unsigned int, "%u", 0)
2043 DEFINE_SYSFS_PROPERTY(fm_search_tune_mode, unsigned int, "%u", value > 3)
2045 DEFINE_SYSFS_PROPERTY(rds, unsigned int, "%u", 0)
2046 DEFINE_SYSFS_PROPERTY(rds_b_block_mask, unsigned int, "%u", 0)
2047 DEFINE_SYSFS_PROPERTY(rds_b_block_match, unsigned int, "%u", 0)
2048 DEFINE_SYSFS_PROPERTY(rds_pi_mask, unsigned int, "%u", 0)
2049 DEFINE_SYSFS_PROPERTY(rds_pi_match, unsigned int, "%u", 0)
2050 DEFINE_SYSFS_PROPERTY(rds_wline, unsigned int, "%u", 0)
2051 property_read(rds_pi, "%x")
2052 property_str_read(rds_rt, (BCM2048_MAX_RDS_RT + 1))
2053 property_str_read(rds_ps, (BCM2048_MAX_RDS_PS + 1))
2055 property_read(fm_rds_flags, "%u")
2056 property_str_read(rds_data, BCM2048_MAX_RDS_RADIO_TEXT * 5)
2058 property_read(region_bottom_frequency, "%u")
2059 property_read(region_top_frequency, "%u")
2060 property_signed_read(fm_carrier_error, int, "%d")
2061 property_signed_read(fm_rssi, int, "%d")
2062 DEFINE_SYSFS_PROPERTY(region, unsigned int, "%u", 0)
2064 static struct device_attribute attrs[] = {
2065 __ATTR(power_state, 0644, bcm2048_power_state_read,
2066 bcm2048_power_state_write),
2067 __ATTR(mute, 0644, bcm2048_mute_read,
2068 bcm2048_mute_write),
2069 __ATTR(audio_route, 0644, bcm2048_audio_route_read,
2070 bcm2048_audio_route_write),
2071 __ATTR(dac_output, 0644, bcm2048_dac_output_read,
2072 bcm2048_dac_output_write),
2073 __ATTR(fm_hi_lo_injection, 0644,
2074 bcm2048_fm_hi_lo_injection_read,
2075 bcm2048_fm_hi_lo_injection_write),
2076 __ATTR(fm_frequency, 0644, bcm2048_fm_frequency_read,
2077 bcm2048_fm_frequency_write),
2078 __ATTR(fm_af_frequency, 0644,
2079 bcm2048_fm_af_frequency_read,
2080 bcm2048_fm_af_frequency_write),
2081 __ATTR(fm_deemphasis, 0644, bcm2048_fm_deemphasis_read,
2082 bcm2048_fm_deemphasis_write),
2083 __ATTR(fm_rds_mask, 0644, bcm2048_fm_rds_mask_read,
2084 bcm2048_fm_rds_mask_write),
2085 __ATTR(fm_best_tune_mode, 0644,
2086 bcm2048_fm_best_tune_mode_read,
2087 bcm2048_fm_best_tune_mode_write),
2088 __ATTR(fm_search_rssi_threshold, 0644,
2089 bcm2048_fm_search_rssi_threshold_read,
2090 bcm2048_fm_search_rssi_threshold_write),
2091 __ATTR(fm_search_mode_direction, 0644,
2092 bcm2048_fm_search_mode_direction_read,
2093 bcm2048_fm_search_mode_direction_write),
2094 __ATTR(fm_search_tune_mode, 0644,
2095 bcm2048_fm_search_tune_mode_read,
2096 bcm2048_fm_search_tune_mode_write),
2097 __ATTR(rds, 0644, bcm2048_rds_read,
2099 __ATTR(rds_b_block_mask, 0644,
2100 bcm2048_rds_b_block_mask_read,
2101 bcm2048_rds_b_block_mask_write),
2102 __ATTR(rds_b_block_match, 0644,
2103 bcm2048_rds_b_block_match_read,
2104 bcm2048_rds_b_block_match_write),
2105 __ATTR(rds_pi_mask, 0644, bcm2048_rds_pi_mask_read,
2106 bcm2048_rds_pi_mask_write),
2107 __ATTR(rds_pi_match, 0644, bcm2048_rds_pi_match_read,
2108 bcm2048_rds_pi_match_write),
2109 __ATTR(rds_wline, 0644, bcm2048_rds_wline_read,
2110 bcm2048_rds_wline_write),
2111 __ATTR(rds_pi, 0444, bcm2048_rds_pi_read, NULL),
2112 __ATTR(rds_rt, 0444, bcm2048_rds_rt_read, NULL),
2113 __ATTR(rds_ps, 0444, bcm2048_rds_ps_read, NULL),
2114 __ATTR(fm_rds_flags, 0444, bcm2048_fm_rds_flags_read, NULL),
2115 __ATTR(region_bottom_frequency, 0444,
2116 bcm2048_region_bottom_frequency_read, NULL),
2117 __ATTR(region_top_frequency, 0444,
2118 bcm2048_region_top_frequency_read, NULL),
2119 __ATTR(fm_carrier_error, 0444,
2120 bcm2048_fm_carrier_error_read, NULL),
2121 __ATTR(fm_rssi, 0444,
2122 bcm2048_fm_rssi_read, NULL),
2123 __ATTR(region, 0644, bcm2048_region_read,
2124 bcm2048_region_write),
2125 __ATTR(rds_data, 0444, bcm2048_rds_data_read, NULL),
2128 static int bcm2048_sysfs_unregister_properties(struct bcm2048_device *bdev,
2133 for (i = 0; i < size; i++)
2134 device_remove_file(&bdev->client->dev, &attrs[i]);
2139 static int bcm2048_sysfs_register_properties(struct bcm2048_device *bdev)
2144 for (i = 0; i < ARRAY_SIZE(attrs); i++) {
2145 if (device_create_file(&bdev->client->dev, &attrs[i]) != 0) {
2146 dev_err(&bdev->client->dev,
2147 "could not register sysfs entry\n");
2149 bcm2048_sysfs_unregister_properties(bdev, i);
2157 static int bcm2048_fops_open(struct file *file)
2159 struct bcm2048_device *bdev = video_drvdata(file);
2163 bdev->rds_data_available = 0;
2168 static int bcm2048_fops_release(struct file *file)
2170 struct bcm2048_device *bdev = video_drvdata(file);
2177 static __poll_t bcm2048_fops_poll(struct file *file,
2178 struct poll_table_struct *pts)
2180 struct bcm2048_device *bdev = video_drvdata(file);
2181 __poll_t retval = 0;
2183 poll_wait(file, &bdev->read_queue, pts);
2185 if (bdev->rds_data_available)
2186 retval = EPOLLIN | EPOLLRDNORM;
2191 static ssize_t bcm2048_fops_read(struct file *file, char __user *buf,
2192 size_t count, loff_t *ppos)
2194 struct bcm2048_device *bdev = video_drvdata(file);
2198 /* we return at least 3 bytes, one block */
2199 count = (count / 3) * 3; /* only multiples of 3 */
2203 while (!bdev->rds_data_available) {
2204 if (file->f_flags & O_NONBLOCK) {
2205 retval = -EWOULDBLOCK;
2208 /* interruptible_sleep_on(&bdev->read_queue); */
2209 if (wait_event_interruptible(bdev->read_queue,
2210 bdev->rds_data_available) < 0) {
2216 mutex_lock(&bdev->mutex);
2217 /* copy data to userspace */
2218 i = bdev->fifo_size - bdev->rd_index;
2220 count = (i / 3) * 3;
2224 unsigned char tmpbuf[3];
2226 tmpbuf[i] = bdev->rds_info.radio_text[bdev->rd_index + i + 2];
2228 bdev->rds_info.radio_text[bdev->rd_index + i + 1];
2230 (bdev->rds_info.radio_text[bdev->rd_index + i] &
2232 if ((bdev->rds_info.radio_text[bdev->rd_index + i] &
2233 BCM2048_RDS_CRC_MASK) == BCM2048_RDS_CRC_UNRECOVARABLE)
2234 tmpbuf[i + 2] |= 0x80;
2235 if (copy_to_user(buf + i, tmpbuf, 3)) {
2242 bdev->rd_index += i;
2243 if (bdev->rd_index >= bdev->fifo_size)
2244 bdev->rds_data_available = 0;
2246 mutex_unlock(&bdev->mutex);
2255 * bcm2048_fops - file operations interface
2257 static const struct v4l2_file_operations bcm2048_fops = {
2258 .owner = THIS_MODULE,
2259 .unlocked_ioctl = video_ioctl2,
2260 /* for RDS read support */
2261 .open = bcm2048_fops_open,
2262 .release = bcm2048_fops_release,
2263 .read = bcm2048_fops_read,
2264 .poll = bcm2048_fops_poll
2268 * Video4Linux Interface
2270 static struct v4l2_queryctrl bcm2048_v4l2_queryctrl[] = {
2272 .id = V4L2_CID_AUDIO_VOLUME,
2273 .flags = V4L2_CTRL_FLAG_DISABLED,
2276 .id = V4L2_CID_AUDIO_BALANCE,
2277 .flags = V4L2_CTRL_FLAG_DISABLED,
2280 .id = V4L2_CID_AUDIO_BASS,
2281 .flags = V4L2_CTRL_FLAG_DISABLED,
2284 .id = V4L2_CID_AUDIO_TREBLE,
2285 .flags = V4L2_CTRL_FLAG_DISABLED,
2288 .id = V4L2_CID_AUDIO_MUTE,
2289 .type = V4L2_CTRL_TYPE_BOOLEAN,
2297 .id = V4L2_CID_AUDIO_LOUDNESS,
2298 .flags = V4L2_CTRL_FLAG_DISABLED,
2302 static int bcm2048_vidioc_querycap(struct file *file, void *priv,
2303 struct v4l2_capability *capability)
2305 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2307 strlcpy(capability->driver, BCM2048_DRIVER_NAME,
2308 sizeof(capability->driver));
2309 strlcpy(capability->card, BCM2048_DRIVER_CARD,
2310 sizeof(capability->card));
2311 snprintf(capability->bus_info, 32, "I2C: 0x%X", bdev->client->addr);
2312 capability->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
2313 V4L2_CAP_HW_FREQ_SEEK;
2314 capability->capabilities = capability->device_caps |
2315 V4L2_CAP_DEVICE_CAPS;
2320 static int bcm2048_vidioc_g_input(struct file *filp, void *priv,
2328 static int bcm2048_vidioc_s_input(struct file *filp, void *priv,
2337 static int bcm2048_vidioc_queryctrl(struct file *file, void *priv,
2338 struct v4l2_queryctrl *qc)
2342 for (i = 0; i < ARRAY_SIZE(bcm2048_v4l2_queryctrl); i++) {
2343 if (qc->id && qc->id == bcm2048_v4l2_queryctrl[i].id) {
2344 *qc = bcm2048_v4l2_queryctrl[i];
2352 static int bcm2048_vidioc_g_ctrl(struct file *file, void *priv,
2353 struct v4l2_control *ctrl)
2355 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2362 case V4L2_CID_AUDIO_MUTE:
2363 err = bcm2048_get_mute(bdev);
2372 static int bcm2048_vidioc_s_ctrl(struct file *file, void *priv,
2373 struct v4l2_control *ctrl)
2375 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2382 case V4L2_CID_AUDIO_MUTE:
2384 if (bdev->power_state) {
2385 err = bcm2048_set_mute(bdev, ctrl->value);
2386 err |= bcm2048_deinit(bdev);
2389 if (!bdev->power_state) {
2390 err = bcm2048_init(bdev);
2391 err |= bcm2048_set_mute(bdev, ctrl->value);
2400 static int bcm2048_vidioc_g_audio(struct file *file, void *priv,
2401 struct v4l2_audio *audio)
2403 if (audio->index > 1)
2406 strncpy(audio->name, "Radio", 32);
2407 audio->capability = V4L2_AUDCAP_STEREO;
2412 static int bcm2048_vidioc_s_audio(struct file *file, void *priv,
2413 const struct v4l2_audio *audio)
2415 if (audio->index != 0)
2421 static int bcm2048_vidioc_g_tuner(struct file *file, void *priv,
2422 struct v4l2_tuner *tuner)
2424 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2431 if (tuner->index > 0)
2434 strncpy(tuner->name, "FM Receiver", 32);
2435 tuner->type = V4L2_TUNER_RADIO;
2437 dev_to_v4l2(bcm2048_get_region_bottom_frequency(bdev));
2439 dev_to_v4l2(bcm2048_get_region_top_frequency(bdev));
2440 tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
2441 tuner->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
2442 tuner->audmode = V4L2_TUNER_MODE_STEREO;
2444 if (bdev->power_state) {
2446 * Report frequencies with high carrier errors to have zero
2449 f_error = bcm2048_get_fm_carrier_error(bdev);
2450 if (f_error < BCM2048_FREQ_ERROR_FLOOR ||
2451 f_error > BCM2048_FREQ_ERROR_ROOF) {
2455 * RSSI level -60 dB is defined to report full
2458 rssi = bcm2048_get_fm_rssi(bdev);
2459 if (rssi >= BCM2048_RSSI_LEVEL_BASE) {
2460 tuner->signal = 0xFFFF;
2461 } else if (rssi > BCM2048_RSSI_LEVEL_ROOF) {
2462 tuner->signal = (rssi +
2463 BCM2048_RSSI_LEVEL_ROOF_NEG)
2464 * BCM2048_SIGNAL_MULTIPLIER;
2476 static int bcm2048_vidioc_s_tuner(struct file *file, void *priv,
2477 const struct v4l2_tuner *tuner)
2479 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2484 if (tuner->index > 0)
2490 static int bcm2048_vidioc_g_frequency(struct file *file, void *priv,
2491 struct v4l2_frequency *freq)
2493 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2497 if (!bdev->power_state)
2500 freq->type = V4L2_TUNER_RADIO;
2501 f = bcm2048_get_fm_frequency(bdev);
2506 freq->frequency = dev_to_v4l2(f);
2511 static int bcm2048_vidioc_s_frequency(struct file *file, void *priv,
2512 const struct v4l2_frequency *freq)
2514 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2517 if (freq->type != V4L2_TUNER_RADIO)
2520 if (!bdev->power_state)
2523 err = bcm2048_set_fm_frequency(bdev, v4l2_to_dev(freq->frequency));
2524 err |= bcm2048_set_fm_search_tune_mode(bdev, BCM2048_FM_PRE_SET_MODE);
2529 static int bcm2048_vidioc_s_hw_freq_seek(struct file *file, void *priv,
2530 const struct v4l2_hw_freq_seek *seek)
2532 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2535 if (!bdev->power_state)
2538 if ((seek->tuner != 0) || (seek->type != V4L2_TUNER_RADIO))
2541 err = bcm2048_set_fm_search_mode_direction(bdev, seek->seek_upward);
2542 err |= bcm2048_set_fm_search_tune_mode(bdev,
2543 BCM2048_FM_AUTO_SEARCH_MODE);
2548 static const struct v4l2_ioctl_ops bcm2048_ioctl_ops = {
2549 .vidioc_querycap = bcm2048_vidioc_querycap,
2550 .vidioc_g_input = bcm2048_vidioc_g_input,
2551 .vidioc_s_input = bcm2048_vidioc_s_input,
2552 .vidioc_queryctrl = bcm2048_vidioc_queryctrl,
2553 .vidioc_g_ctrl = bcm2048_vidioc_g_ctrl,
2554 .vidioc_s_ctrl = bcm2048_vidioc_s_ctrl,
2555 .vidioc_g_audio = bcm2048_vidioc_g_audio,
2556 .vidioc_s_audio = bcm2048_vidioc_s_audio,
2557 .vidioc_g_tuner = bcm2048_vidioc_g_tuner,
2558 .vidioc_s_tuner = bcm2048_vidioc_s_tuner,
2559 .vidioc_g_frequency = bcm2048_vidioc_g_frequency,
2560 .vidioc_s_frequency = bcm2048_vidioc_s_frequency,
2561 .vidioc_s_hw_freq_seek = bcm2048_vidioc_s_hw_freq_seek,
2565 * bcm2048_viddev_template - video device interface
2567 static const struct video_device bcm2048_viddev_template = {
2568 .fops = &bcm2048_fops,
2569 .name = BCM2048_DRIVER_NAME,
2570 .release = video_device_release_empty,
2571 .ioctl_ops = &bcm2048_ioctl_ops,
2575 * I2C driver interface
2577 static int bcm2048_i2c_driver_probe(struct i2c_client *client,
2578 const struct i2c_device_id *id)
2580 struct bcm2048_device *bdev;
2583 bdev = kzalloc(sizeof(*bdev), GFP_KERNEL);
2589 bdev->client = client;
2590 i2c_set_clientdata(client, bdev);
2591 mutex_init(&bdev->mutex);
2592 init_completion(&bdev->compl);
2593 INIT_WORK(&bdev->work, bcm2048_work);
2596 err = request_irq(client->irq,
2597 bcm2048_handler, IRQF_TRIGGER_FALLING,
2598 client->name, bdev);
2600 dev_err(&client->dev, "Could not request IRQ\n");
2603 dev_dbg(&client->dev, "IRQ requested.\n");
2605 dev_dbg(&client->dev, "IRQ not configured. Using timeouts.\n");
2608 bdev->videodev = bcm2048_viddev_template;
2609 video_set_drvdata(&bdev->videodev, bdev);
2610 if (video_register_device(&bdev->videodev, VFL_TYPE_RADIO, radio_nr)) {
2611 dev_dbg(&client->dev, "Could not register video device.\n");
2616 err = bcm2048_sysfs_register_properties(bdev);
2618 dev_dbg(&client->dev, "Could not register sysfs interface.\n");
2619 goto free_registration;
2622 err = bcm2048_probe(bdev);
2624 dev_dbg(&client->dev, "Failed to probe device information.\n");
2631 bcm2048_sysfs_unregister_properties(bdev, ARRAY_SIZE(attrs));
2633 video_unregister_device(&bdev->videodev);
2636 free_irq(client->irq, bdev);
2638 i2c_set_clientdata(client, NULL);
2644 static int bcm2048_i2c_driver_remove(struct i2c_client *client)
2646 struct bcm2048_device *bdev = i2c_get_clientdata(client);
2648 if (!client->adapter)
2652 bcm2048_sysfs_unregister_properties(bdev, ARRAY_SIZE(attrs));
2653 video_unregister_device(&bdev->videodev);
2655 if (bdev->power_state)
2656 bcm2048_set_power_state(bdev, BCM2048_POWER_OFF);
2658 if (client->irq > 0)
2659 free_irq(client->irq, bdev);
2661 cancel_work_sync(&bdev->work);
2670 * bcm2048_i2c_driver - i2c driver interface
2672 static const struct i2c_device_id bcm2048_id[] = {
2676 MODULE_DEVICE_TABLE(i2c, bcm2048_id);
2678 static struct i2c_driver bcm2048_i2c_driver = {
2680 .name = BCM2048_DRIVER_NAME,
2682 .probe = bcm2048_i2c_driver_probe,
2683 .remove = bcm2048_i2c_driver_remove,
2684 .id_table = bcm2048_id,
2687 module_i2c_driver(bcm2048_i2c_driver);
2689 MODULE_LICENSE("GPL");
2690 MODULE_AUTHOR(BCM2048_DRIVER_AUTHOR);
2691 MODULE_DESCRIPTION(BCM2048_DRIVER_DESC);
2692 MODULE_VERSION("0.0.2");