1 /*
2 * drivers/staging/media/radio-bcm2048.c
3 *
4 * Driver for I2C Broadcom BCM2048 FM Radio Receiver:
5 *
6 * Copyright (C) Nokia Corporation
7 * Contact: Eero Nurkkala <ext-eero.nurkkala@nokia.com>
8 *
9 * Copyright (C) Nils Faerber <nils.faerber@kernelconcepts.de>
10 *
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.
14 *
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.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * 02110-1301 USA
24 */
25
26 /*
27 * History:
28 * Eero Nurkkala <ext-eero.nurkkala@nokia.com>
29 * Version 0.0.1
30 * - Initial implementation
31 * 2010-02-21 Nils Faerber <nils.faerber@kernelconcepts.de>
32 * Version 0.0.2
33 * - Add support for interrupt driven rds data reading
34 */
35
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/version.h>
40 #include <linux/interrupt.h>
41 #include <linux/sysfs.h>
42 #include <linux/completion.h>
43 #include <linux/delay.h>
44 #include <linux/i2c.h>
45 #include <linux/videodev2.h>
46 #include <linux/mutex.h>
47 #include <linux/slab.h>
48 #include <media/v4l2-common.h>
49 #include <media/v4l2-ioctl.h>
50 #include "radio-bcm2048.h"
51
52 /* driver definitions */
53 #define BCM2048_DRIVER_AUTHOR "Eero Nurkkala <ext-eero.nurkkala@nokia.com>"
54 #define BCM2048_DRIVER_NAME BCM2048_NAME
55 #define BCM2048_DRIVER_VERSION KERNEL_VERSION(0, 0, 1)
56 #define BCM2048_DRIVER_CARD "Broadcom bcm2048 FM Radio Receiver"
57 #define BCM2048_DRIVER_DESC "I2C driver for BCM2048 FM Radio Receiver"
58
59 /* I2C Control Registers */
60 #define BCM2048_I2C_FM_RDS_SYSTEM 0x00
61 #define BCM2048_I2C_FM_CTRL 0x01
62 #define BCM2048_I2C_RDS_CTRL0 0x02
63 #define BCM2048_I2C_RDS_CTRL1 0x03
64 #define BCM2048_I2C_FM_AUDIO_PAUSE 0x04
65 #define BCM2048_I2C_FM_AUDIO_CTRL0 0x05
66 #define BCM2048_I2C_FM_AUDIO_CTRL1 0x06
67 #define BCM2048_I2C_FM_SEARCH_CTRL0 0x07
68 #define BCM2048_I2C_FM_SEARCH_CTRL1 0x08
69 #define BCM2048_I2C_FM_SEARCH_TUNE_MODE 0x09
70 #define BCM2048_I2C_FM_FREQ0 0x0a
71 #define BCM2048_I2C_FM_FREQ1 0x0b
72 #define BCM2048_I2C_FM_AF_FREQ0 0x0c
73 #define BCM2048_I2C_FM_AF_FREQ1 0x0d
74 #define BCM2048_I2C_FM_CARRIER 0x0e
75 #define BCM2048_I2C_FM_RSSI 0x0f
76 #define BCM2048_I2C_FM_RDS_MASK0 0x10
77 #define BCM2048_I2C_FM_RDS_MASK1 0x11
78 #define BCM2048_I2C_FM_RDS_FLAG0 0x12
79 #define BCM2048_I2C_FM_RDS_FLAG1 0x13
80 #define BCM2048_I2C_RDS_WLINE 0x14
81 #define BCM2048_I2C_RDS_BLKB_MATCH0 0x16
82 #define BCM2048_I2C_RDS_BLKB_MATCH1 0x17
83 #define BCM2048_I2C_RDS_BLKB_MASK0 0x18
84 #define BCM2048_I2C_RDS_BLKB_MASK1 0x19
85 #define BCM2048_I2C_RDS_PI_MATCH0 0x1a
86 #define BCM2048_I2C_RDS_PI_MATCH1 0x1b
87 #define BCM2048_I2C_RDS_PI_MASK0 0x1c
88 #define BCM2048_I2C_RDS_PI_MASK1 0x1d
89 #define BCM2048_I2C_SPARE1 0x20
90 #define BCM2048_I2C_SPARE2 0x21
91 #define BCM2048_I2C_FM_RDS_REV 0x28
92 #define BCM2048_I2C_SLAVE_CONFIGURATION 0x29
93 #define BCM2048_I2C_RDS_DATA 0x80
94 #define BCM2048_I2C_FM_BEST_TUNE_MODE 0x90
95
96 /* BCM2048_I2C_FM_RDS_SYSTEM */
97 #define BCM2048_FM_ON 0x01
98 #define BCM2048_RDS_ON 0x02
99
100 /* BCM2048_I2C_FM_CTRL */
101 #define BCM2048_BAND_SELECT 0x01
102 #define BCM2048_STEREO_MONO_AUTO_SELECT 0x02
103 #define BCM2048_STEREO_MONO_MANUAL_SELECT 0x04
104 #define BCM2048_STEREO_MONO_BLEND_SWITCH 0x08
105 #define BCM2048_HI_LO_INJECTION 0x10
106
107 /* BCM2048_I2C_RDS_CTRL0 */
108 #define BCM2048_RBDS_RDS_SELECT 0x01
109 #define BCM2048_FLUSH_FIFO 0x02
110
111 /* BCM2048_I2C_FM_AUDIO_PAUSE */
112 #define BCM2048_AUDIO_PAUSE_RSSI_TRESH 0x0f
113 #define BCM2048_AUDIO_PAUSE_DURATION 0xf0
114
115 /* BCM2048_I2C_FM_AUDIO_CTRL0 */
116 #define BCM2048_RF_MUTE 0x01
117 #define BCM2048_MANUAL_MUTE 0x02
118 #define BCM2048_DAC_OUTPUT_LEFT 0x04
119 #define BCM2048_DAC_OUTPUT_RIGHT 0x08
120 #define BCM2048_AUDIO_ROUTE_DAC 0x10
121 #define BCM2048_AUDIO_ROUTE_I2S 0x20
122 #define BCM2048_DE_EMPHASIS_SELECT 0x40
123 #define BCM2048_AUDIO_BANDWIDTH_SELECT 0x80
124
125 /* BCM2048_I2C_FM_SEARCH_CTRL0 */
126 #define BCM2048_SEARCH_RSSI_THRESHOLD 0x7f
127 #define BCM2048_SEARCH_DIRECTION 0x80
128
129 /* BCM2048_I2C_FM_SEARCH_TUNE_MODE */
130 #define BCM2048_FM_AUTO_SEARCH 0x03
131
132 /* BCM2048_I2C_FM_RSSI */
133 #define BCM2048_RSSI_VALUE 0xff
134
135 /* BCM2048_I2C_FM_RDS_MASK0 */
136 /* BCM2048_I2C_FM_RDS_MASK1 */
137 #define BCM2048_FM_FLAG_SEARCH_TUNE_FINISHED 0x01
138 #define BCM2048_FM_FLAG_SEARCH_TUNE_FAIL 0x02
139 #define BCM2048_FM_FLAG_RSSI_LOW 0x04
140 #define BCM2048_FM_FLAG_CARRIER_ERROR_HIGH 0x08
141 #define BCM2048_FM_FLAG_AUDIO_PAUSE_INDICATION 0x10
142 #define BCM2048_FLAG_STEREO_DETECTED 0x20
143 #define BCM2048_FLAG_STEREO_ACTIVE 0x40
144
145 /* BCM2048_I2C_RDS_DATA */
146 #define BCM2048_SLAVE_ADDRESS 0x3f
147 #define BCM2048_SLAVE_ENABLE 0x80
148
149 /* BCM2048_I2C_FM_BEST_TUNE_MODE */
150 #define BCM2048_BEST_TUNE_MODE 0x80
151
152 #define BCM2048_FM_FLAG_SEARCH_TUNE_FINISHED 0x01
153 #define BCM2048_FM_FLAG_SEARCH_TUNE_FAIL 0x02
154 #define BCM2048_FM_FLAG_RSSI_LOW 0x04
155 #define BCM2048_FM_FLAG_CARRIER_ERROR_HIGH 0x08
156 #define BCM2048_FM_FLAG_AUDIO_PAUSE_INDICATION 0x10
157 #define BCM2048_FLAG_STEREO_DETECTED 0x20
158 #define BCM2048_FLAG_STEREO_ACTIVE 0x40
159
160 #define BCM2048_RDS_FLAG_FIFO_WLINE 0x02
161 #define BCM2048_RDS_FLAG_B_BLOCK_MATCH 0x08
162 #define BCM2048_RDS_FLAG_SYNC_LOST 0x10
163 #define BCM2048_RDS_FLAG_PI_MATCH 0x20
164
165 #define BCM2048_RDS_MARK_END_BYTE0 0x7C
166 #define BCM2048_RDS_MARK_END_BYTEN 0xFF
167
168 #define BCM2048_FM_FLAGS_ALL (FM_FLAG_SEARCH_TUNE_FINISHED | \
169 FM_FLAG_SEARCH_TUNE_FAIL | \
170 FM_FLAG_RSSI_LOW | \
171 FM_FLAG_CARRIER_ERROR_HIGH | \
172 FM_FLAG_AUDIO_PAUSE_INDICATION | \
173 FLAG_STEREO_DETECTED | FLAG_STEREO_ACTIVE)
174
175 #define BCM2048_RDS_FLAGS_ALL (RDS_FLAG_FIFO_WLINE | \
176 RDS_FLAG_B_BLOCK_MATCH | \
177 RDS_FLAG_SYNC_LOST | RDS_FLAG_PI_MATCH)
178
179 #define BCM2048_DEFAULT_TIMEOUT 1500
180 #define BCM2048_AUTO_SEARCH_TIMEOUT 3000
181
182
183 #define BCM2048_FREQDEV_UNIT 10000
184 #define BCM2048_FREQV4L2_MULTI 625
185 #define dev_to_v4l2(f) ((f * BCM2048_FREQDEV_UNIT) / BCM2048_FREQV4L2_MULTI)
186 #define v4l2_to_dev(f) ((f * BCM2048_FREQV4L2_MULTI) / BCM2048_FREQDEV_UNIT)
187
188 #define msb(x) ((u8)((u16) x >> 8))
189 #define lsb(x) ((u8)((u16) x & 0x00FF))
190 #define compose_u16(msb, lsb) (((u16)msb << 8) | lsb)
191
192 #define BCM2048_DEFAULT_POWERING_DELAY 20
193 #define BCM2048_DEFAULT_REGION 0x02
194 #define BCM2048_DEFAULT_MUTE 0x01
195 #define BCM2048_DEFAULT_RSSI_THRESHOLD 0x64
196 #define BCM2048_DEFAULT_RDS_WLINE 0x7E
197
198 #define BCM2048_FM_SEARCH_INACTIVE 0x00
199 #define BCM2048_FM_PRE_SET_MODE 0x01
200 #define BCM2048_FM_AUTO_SEARCH_MODE 0x02
201 #define BCM2048_FM_AF_JUMP_MODE 0x03
202
203 #define BCM2048_FREQUENCY_BASE 64000
204
205 #define BCM2048_POWER_ON 0x01
206 #define BCM2048_POWER_OFF 0x00
207
208 #define BCM2048_ITEM_ENABLED 0x01
209 #define BCM2048_SEARCH_DIRECTION_UP 0x01
210
211 #define BCM2048_DE_EMPHASIS_75us 75
212 #define BCM2048_DE_EMPHASIS_50us 50
213
214 #define BCM2048_SCAN_FAIL 0x00
215 #define BCM2048_SCAN_OK 0x01
216
217 #define BCM2048_FREQ_ERROR_FLOOR -20
218 #define BCM2048_FREQ_ERROR_ROOF 20
219
220 /* -60 dB is reported as full signal strength */
221 #define BCM2048_RSSI_LEVEL_BASE -60
222 #define BCM2048_RSSI_LEVEL_ROOF -100
223 #define BCM2048_RSSI_LEVEL_ROOF_NEG 100
224 #define BCM2048_SIGNAL_MULTIPLIER (0xFFFF / \
225 (BCM2048_RSSI_LEVEL_ROOF_NEG + \
226 BCM2048_RSSI_LEVEL_BASE))
227
228 #define BCM2048_RDS_FIFO_DUPLE_SIZE 0x03
229 #define BCM2048_RDS_CRC_MASK 0x0F
230 #define BCM2048_RDS_CRC_NONE 0x00
231 #define BCM2048_RDS_CRC_MAX_2BITS 0x04
232 #define BCM2048_RDS_CRC_LEAST_2BITS 0x08
233 #define BCM2048_RDS_CRC_UNRECOVARABLE 0x0C
234
235 #define BCM2048_RDS_BLOCK_MASK 0xF0
236 #define BCM2048_RDS_BLOCK_A 0x00
237 #define BCM2048_RDS_BLOCK_B 0x10
238 #define BCM2048_RDS_BLOCK_C 0x20
239 #define BCM2048_RDS_BLOCK_D 0x30
240 #define BCM2048_RDS_BLOCK_C_SCORED 0x40
241 #define BCM2048_RDS_BLOCK_E 0x60
242
243 #define BCM2048_RDS_RT 0x20
244 #define BCM2048_RDS_PS 0x00
245
246 #define BCM2048_RDS_GROUP_AB_MASK 0x08
247 #define BCM2048_RDS_GROUP_A 0x00
248 #define BCM2048_RDS_GROUP_B 0x08
249
250 #define BCM2048_RDS_RT_AB_MASK 0x10
251 #define BCM2048_RDS_RT_A 0x00
252 #define BCM2048_RDS_RT_B 0x10
253 #define BCM2048_RDS_RT_INDEX 0x0F
254
255 #define BCM2048_RDS_PS_INDEX 0x03
256
257 struct rds_info {
258 u16 rds_pi;
259 #define BCM2048_MAX_RDS_RT (64 + 1)
260 u8 rds_rt[BCM2048_MAX_RDS_RT];
261 u8 rds_rt_group_b;
262 u8 rds_rt_ab;
263 #define BCM2048_MAX_RDS_PS (8 + 1)
264 u8 rds_ps[BCM2048_MAX_RDS_PS];
265 u8 rds_ps_group;
266 u8 rds_ps_group_cnt;
267 #define BCM2048_MAX_RDS_RADIO_TEXT 255
268 u8 radio_text[BCM2048_MAX_RDS_RADIO_TEXT + 3];
269 u8 text_len;
270 };
271
272 struct region_info {
273 u32 bottom_frequency;
274 u32 top_frequency;
275 u8 deemphasis;
276 u8 channel_spacing;
277 u8 region;
278 };
279
280 struct bcm2048_device {
281 struct i2c_client *client;
282 struct video_device videodev;
283 struct work_struct work;
284 struct completion compl;
285 struct mutex mutex;
286 struct bcm2048_platform_data *platform_data;
287 struct rds_info rds_info;
288 struct region_info region_info;
289 u16 frequency;
290 u8 cache_fm_rds_system;
291 u8 cache_fm_ctrl;
292 u8 cache_fm_audio_ctrl0;
293 u8 cache_fm_search_ctrl0;
294 u8 power_state;
295 u8 rds_state;
296 u8 fifo_size;
297 u8 scan_state;
298 u8 mute_state;
299
300 /* for rds data device read */
301 wait_queue_head_t read_queue;
302 unsigned int users;
303 unsigned char rds_data_available;
304 unsigned int rd_index;
305 };
306
307 static int radio_nr = -1; /* radio device minor (-1 ==> auto assign) */
308 module_param(radio_nr, int, 0);
309 MODULE_PARM_DESC(radio_nr,
310 "Minor number for radio device (-1 ==> auto assign)");
311
312 static struct region_info region_configs[] = {
313 /* USA */
314 {
315 .channel_spacing = 20,
316 .bottom_frequency = 87500,
317 .top_frequency = 108000,
318 .deemphasis = 75,
319 .region = 0,
320 },
321 /* Australia */
322 {
323 .channel_spacing = 20,
324 .bottom_frequency = 87500,
325 .top_frequency = 108000,
326 .deemphasis = 50,
327 .region = 1,
328 },
329 /* Europe */
330 {
331 .channel_spacing = 10,
332 .bottom_frequency = 87500,
333 .top_frequency = 108000,
334 .deemphasis = 50,
335 .region = 2,
336 },
337 /* Japan */
338 {
339 .channel_spacing = 10,
340 .bottom_frequency = 76000,
341 .top_frequency = 90000,
342 .deemphasis = 50,
343 .region = 3,
344 },
345 };
346
347 /*
348 * I2C Interface read / write
349 */
bcm2048_send_command(struct bcm2048_device * bdev,unsigned int reg,unsigned int value)350 static int bcm2048_send_command(struct bcm2048_device *bdev, unsigned int reg,
351 unsigned int value)
352 {
353 struct i2c_client *client = bdev->client;
354 u8 data[2];
355
356 if (!bdev->power_state) {
357 dev_err(&bdev->client->dev, "bcm2048: chip not powered!\n");
358 return -EIO;
359 }
360
361 data[0] = reg & 0xff;
362 data[1] = value & 0xff;
363
364 if (i2c_master_send(client, data, 2) == 2)
365 return 0;
366
367 dev_err(&bdev->client->dev, "BCM I2C error!\n");
368 dev_err(&bdev->client->dev, "Is Bluetooth up and running?\n");
369 return -EIO;
370 }
371
bcm2048_recv_command(struct bcm2048_device * bdev,unsigned int reg,u8 * value)372 static int bcm2048_recv_command(struct bcm2048_device *bdev, unsigned int reg,
373 u8 *value)
374 {
375 struct i2c_client *client = bdev->client;
376
377 if (!bdev->power_state) {
378 dev_err(&bdev->client->dev, "bcm2048: chip not powered!\n");
379 return -EIO;
380 }
381
382 value[0] = i2c_smbus_read_byte_data(client, reg & 0xff);
383
384 return 0;
385 }
386
bcm2048_recv_duples(struct bcm2048_device * bdev,unsigned int reg,u8 * value,u8 duples)387 static int bcm2048_recv_duples(struct bcm2048_device *bdev, unsigned int reg,
388 u8 *value, u8 duples)
389 {
390 struct i2c_client *client = bdev->client;
391 struct i2c_adapter *adap = client->adapter;
392 struct i2c_msg msg[2];
393 u8 buf;
394
395 if (!bdev->power_state) {
396 dev_err(&bdev->client->dev, "bcm2048: chip not powered!\n");
397 return -EIO;
398 }
399
400 buf = reg & 0xff;
401
402 msg[0].addr = client->addr;
403 msg[0].flags = client->flags & I2C_M_TEN;
404 msg[0].len = 1;
405 msg[0].buf = &buf;
406
407 msg[1].addr = client->addr;
408 msg[1].flags = client->flags & I2C_M_TEN;
409 msg[1].flags |= I2C_M_RD;
410 msg[1].len = duples;
411 msg[1].buf = value;
412
413 return i2c_transfer(adap, msg, 2);
414 }
415
416 /*
417 * BCM2048 - I2C register programming helpers
418 */
bcm2048_set_power_state(struct bcm2048_device * bdev,u8 power)419 static int bcm2048_set_power_state(struct bcm2048_device *bdev, u8 power)
420 {
421 int err = 0;
422
423 mutex_lock(&bdev->mutex);
424
425 if (power) {
426 bdev->power_state = BCM2048_POWER_ON;
427 bdev->cache_fm_rds_system |= BCM2048_FM_ON;
428 } else {
429 bdev->cache_fm_rds_system &= ~BCM2048_FM_ON;
430 }
431
432 /*
433 * Warning! FM cannot be turned off because then
434 * the I2C communications get ruined!
435 * Comment off the "if (power)" when the chip works!
436 */
437 if (power)
438 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_SYSTEM,
439 bdev->cache_fm_rds_system);
440 msleep(BCM2048_DEFAULT_POWERING_DELAY);
441
442 if (!power)
443 bdev->power_state = BCM2048_POWER_OFF;
444
445 mutex_unlock(&bdev->mutex);
446 return err;
447 }
448
bcm2048_get_power_state(struct bcm2048_device * bdev)449 static int bcm2048_get_power_state(struct bcm2048_device *bdev)
450 {
451 int err;
452 u8 value;
453
454 mutex_lock(&bdev->mutex);
455
456 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_SYSTEM, &value);
457
458 mutex_unlock(&bdev->mutex);
459
460 if (!err && (value & BCM2048_FM_ON))
461 return BCM2048_POWER_ON;
462
463 return err;
464 }
465
bcm2048_set_rds_no_lock(struct bcm2048_device * bdev,u8 rds_on)466 static int bcm2048_set_rds_no_lock(struct bcm2048_device *bdev, u8 rds_on)
467 {
468 int err;
469 u8 flags;
470
471 bdev->cache_fm_rds_system &= ~BCM2048_RDS_ON;
472
473 if (rds_on) {
474 bdev->cache_fm_rds_system |= BCM2048_RDS_ON;
475 bdev->rds_state = BCM2048_RDS_ON;
476 flags = BCM2048_RDS_FLAG_FIFO_WLINE;
477 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK1,
478 flags);
479 } else {
480 flags = 0;
481 bdev->rds_state = 0;
482 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK1,
483 flags);
484 memset(&bdev->rds_info, 0, sizeof(bdev->rds_info));
485 }
486
487 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_SYSTEM,
488 bdev->cache_fm_rds_system);
489
490 return err;
491 }
492
bcm2048_get_rds_no_lock(struct bcm2048_device * bdev)493 static int bcm2048_get_rds_no_lock(struct bcm2048_device *bdev)
494 {
495 int err;
496 u8 value;
497
498 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_SYSTEM, &value);
499
500 if (!err && (value & BCM2048_RDS_ON))
501 return BCM2048_ITEM_ENABLED;
502
503 return err;
504 }
505
bcm2048_set_rds(struct bcm2048_device * bdev,u8 rds_on)506 static int bcm2048_set_rds(struct bcm2048_device *bdev, u8 rds_on)
507 {
508 int err;
509
510 mutex_lock(&bdev->mutex);
511
512 err = bcm2048_set_rds_no_lock(bdev, rds_on);
513
514 mutex_unlock(&bdev->mutex);
515 return err;
516 }
517
bcm2048_get_rds(struct bcm2048_device * bdev)518 static int bcm2048_get_rds(struct bcm2048_device *bdev)
519 {
520 int err;
521
522 mutex_lock(&bdev->mutex);
523
524 err = bcm2048_get_rds_no_lock(bdev);
525
526 mutex_unlock(&bdev->mutex);
527 return err;
528 }
529
bcm2048_get_rds_pi(struct bcm2048_device * bdev)530 static int bcm2048_get_rds_pi(struct bcm2048_device *bdev)
531 {
532 return bdev->rds_info.rds_pi;
533 }
534
bcm2048_set_fm_automatic_stereo_mono(struct bcm2048_device * bdev,u8 enabled)535 static int bcm2048_set_fm_automatic_stereo_mono(struct bcm2048_device *bdev,
536 u8 enabled)
537 {
538 int err;
539
540 mutex_lock(&bdev->mutex);
541
542 bdev->cache_fm_ctrl &= ~BCM2048_STEREO_MONO_AUTO_SELECT;
543
544 if (enabled)
545 bdev->cache_fm_ctrl |= BCM2048_STEREO_MONO_AUTO_SELECT;
546
547 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_CTRL,
548 bdev->cache_fm_ctrl);
549
550 mutex_unlock(&bdev->mutex);
551 return err;
552 }
553
bcm2048_set_fm_hi_lo_injection(struct bcm2048_device * bdev,u8 hi_lo)554 static int bcm2048_set_fm_hi_lo_injection(struct bcm2048_device *bdev,
555 u8 hi_lo)
556 {
557 int err;
558
559 mutex_lock(&bdev->mutex);
560
561 bdev->cache_fm_ctrl &= ~BCM2048_HI_LO_INJECTION;
562
563 if (hi_lo)
564 bdev->cache_fm_ctrl |= BCM2048_HI_LO_INJECTION;
565
566 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_CTRL,
567 bdev->cache_fm_ctrl);
568
569 mutex_unlock(&bdev->mutex);
570 return err;
571 }
572
bcm2048_get_fm_hi_lo_injection(struct bcm2048_device * bdev)573 static int bcm2048_get_fm_hi_lo_injection(struct bcm2048_device *bdev)
574 {
575 int err;
576 u8 value;
577
578 mutex_lock(&bdev->mutex);
579
580 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_CTRL, &value);
581
582 mutex_unlock(&bdev->mutex);
583
584 if (!err && (value & BCM2048_HI_LO_INJECTION))
585 return BCM2048_ITEM_ENABLED;
586
587 return err;
588 }
589
bcm2048_set_fm_frequency(struct bcm2048_device * bdev,u32 frequency)590 static int bcm2048_set_fm_frequency(struct bcm2048_device *bdev, u32 frequency)
591 {
592 int err;
593
594 if (frequency < bdev->region_info.bottom_frequency ||
595 frequency > bdev->region_info.top_frequency)
596 return -EDOM;
597
598 frequency -= BCM2048_FREQUENCY_BASE;
599
600 mutex_lock(&bdev->mutex);
601
602 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_FREQ0, lsb(frequency));
603 err |= bcm2048_send_command(bdev, BCM2048_I2C_FM_FREQ1,
604 msb(frequency));
605
606 if (!err)
607 bdev->frequency = frequency;
608
609 mutex_unlock(&bdev->mutex);
610 return err;
611 }
612
bcm2048_get_fm_frequency(struct bcm2048_device * bdev)613 static int bcm2048_get_fm_frequency(struct bcm2048_device *bdev)
614 {
615 int err;
616 u8 lsb = 0, msb = 0;
617
618 mutex_lock(&bdev->mutex);
619
620 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_FREQ0, &lsb);
621 err |= bcm2048_recv_command(bdev, BCM2048_I2C_FM_FREQ1, &msb);
622
623 mutex_unlock(&bdev->mutex);
624
625 if (err)
626 return err;
627
628 err = compose_u16(msb, lsb);
629 err += BCM2048_FREQUENCY_BASE;
630
631 return err;
632 }
633
bcm2048_set_fm_af_frequency(struct bcm2048_device * bdev,u32 frequency)634 static int bcm2048_set_fm_af_frequency(struct bcm2048_device *bdev,
635 u32 frequency)
636 {
637 int err;
638
639 if (frequency < bdev->region_info.bottom_frequency ||
640 frequency > bdev->region_info.top_frequency)
641 return -EDOM;
642
643 frequency -= BCM2048_FREQUENCY_BASE;
644
645 mutex_lock(&bdev->mutex);
646
647 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_AF_FREQ0,
648 lsb(frequency));
649 err |= bcm2048_send_command(bdev, BCM2048_I2C_FM_AF_FREQ1,
650 msb(frequency));
651 if (!err)
652 bdev->frequency = frequency;
653
654 mutex_unlock(&bdev->mutex);
655 return err;
656 }
657
bcm2048_get_fm_af_frequency(struct bcm2048_device * bdev)658 static int bcm2048_get_fm_af_frequency(struct bcm2048_device *bdev)
659 {
660 int err;
661 u8 lsb = 0, msb = 0;
662
663 mutex_lock(&bdev->mutex);
664
665 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_AF_FREQ0, &lsb);
666 err |= bcm2048_recv_command(bdev, BCM2048_I2C_FM_AF_FREQ1, &msb);
667
668 mutex_unlock(&bdev->mutex);
669
670 if (err)
671 return err;
672
673 err = compose_u16(msb, lsb);
674 err += BCM2048_FREQUENCY_BASE;
675
676 return err;
677 }
678
bcm2048_set_fm_deemphasis(struct bcm2048_device * bdev,int d)679 static int bcm2048_set_fm_deemphasis(struct bcm2048_device *bdev, int d)
680 {
681 int err;
682 u8 deemphasis;
683
684 if (d == BCM2048_DE_EMPHASIS_75us)
685 deemphasis = BCM2048_DE_EMPHASIS_SELECT;
686 else
687 deemphasis = 0;
688
689 mutex_lock(&bdev->mutex);
690
691 bdev->cache_fm_audio_ctrl0 &= ~BCM2048_DE_EMPHASIS_SELECT;
692 bdev->cache_fm_audio_ctrl0 |= deemphasis;
693
694 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0,
695 bdev->cache_fm_audio_ctrl0);
696
697 if (!err)
698 bdev->region_info.deemphasis = d;
699
700 mutex_unlock(&bdev->mutex);
701
702 return err;
703 }
704
bcm2048_get_fm_deemphasis(struct bcm2048_device * bdev)705 static int bcm2048_get_fm_deemphasis(struct bcm2048_device *bdev)
706 {
707 int err;
708 u8 value;
709
710 mutex_lock(&bdev->mutex);
711
712 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0, &value);
713
714 mutex_unlock(&bdev->mutex);
715
716 if (!err) {
717 if (value & BCM2048_DE_EMPHASIS_SELECT)
718 return BCM2048_DE_EMPHASIS_75us;
719
720 return BCM2048_DE_EMPHASIS_50us;
721 }
722
723 return err;
724 }
725
bcm2048_set_region(struct bcm2048_device * bdev,u8 region)726 static int bcm2048_set_region(struct bcm2048_device *bdev, u8 region)
727 {
728 int err;
729 u32 new_frequency = 0;
730
731 if (region >= ARRAY_SIZE(region_configs))
732 return -EINVAL;
733
734 mutex_lock(&bdev->mutex);
735 bdev->region_info = region_configs[region];
736
737 if (region_configs[region].bottom_frequency < 87500)
738 bdev->cache_fm_ctrl |= BCM2048_BAND_SELECT;
739 else
740 bdev->cache_fm_ctrl &= ~BCM2048_BAND_SELECT;
741
742 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_CTRL,
743 bdev->cache_fm_ctrl);
744 if (err) {
745 mutex_unlock(&bdev->mutex);
746 goto done;
747 }
748 mutex_unlock(&bdev->mutex);
749
750 if (bdev->frequency < region_configs[region].bottom_frequency ||
751 bdev->frequency > region_configs[region].top_frequency)
752 new_frequency = region_configs[region].bottom_frequency;
753
754 if (new_frequency > 0) {
755 err = bcm2048_set_fm_frequency(bdev, new_frequency);
756
757 if (err)
758 goto done;
759 }
760
761 err = bcm2048_set_fm_deemphasis(bdev,
762 region_configs[region].deemphasis);
763
764 done:
765 return err;
766 }
767
bcm2048_get_region(struct bcm2048_device * bdev)768 static int bcm2048_get_region(struct bcm2048_device *bdev)
769 {
770 int err;
771
772 mutex_lock(&bdev->mutex);
773 err = bdev->region_info.region;
774 mutex_unlock(&bdev->mutex);
775
776 return err;
777 }
778
bcm2048_set_mute(struct bcm2048_device * bdev,u16 mute)779 static int bcm2048_set_mute(struct bcm2048_device *bdev, u16 mute)
780 {
781 int err;
782
783 mutex_lock(&bdev->mutex);
784
785 bdev->cache_fm_audio_ctrl0 &= ~(BCM2048_RF_MUTE | BCM2048_MANUAL_MUTE);
786
787 if (mute)
788 bdev->cache_fm_audio_ctrl0 |= (BCM2048_RF_MUTE |
789 BCM2048_MANUAL_MUTE);
790
791 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0,
792 bdev->cache_fm_audio_ctrl0);
793
794 if (!err)
795 bdev->mute_state = mute;
796
797 mutex_unlock(&bdev->mutex);
798 return err;
799 }
800
bcm2048_get_mute(struct bcm2048_device * bdev)801 static int bcm2048_get_mute(struct bcm2048_device *bdev)
802 {
803 int err;
804 u8 value;
805
806 mutex_lock(&bdev->mutex);
807
808 if (bdev->power_state) {
809 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0,
810 &value);
811 if (!err)
812 err = value & (BCM2048_RF_MUTE | BCM2048_MANUAL_MUTE);
813 } else {
814 err = bdev->mute_state;
815 }
816
817 mutex_unlock(&bdev->mutex);
818 return err;
819 }
820
bcm2048_set_audio_route(struct bcm2048_device * bdev,u8 route)821 static int bcm2048_set_audio_route(struct bcm2048_device *bdev, u8 route)
822 {
823 int err;
824
825 mutex_lock(&bdev->mutex);
826
827 route &= (BCM2048_AUDIO_ROUTE_DAC | BCM2048_AUDIO_ROUTE_I2S);
828 bdev->cache_fm_audio_ctrl0 &= ~(BCM2048_AUDIO_ROUTE_DAC |
829 BCM2048_AUDIO_ROUTE_I2S);
830 bdev->cache_fm_audio_ctrl0 |= route;
831
832 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0,
833 bdev->cache_fm_audio_ctrl0);
834
835 mutex_unlock(&bdev->mutex);
836 return err;
837 }
838
bcm2048_get_audio_route(struct bcm2048_device * bdev)839 static int bcm2048_get_audio_route(struct bcm2048_device *bdev)
840 {
841 int err;
842 u8 value;
843
844 mutex_lock(&bdev->mutex);
845
846 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0, &value);
847
848 mutex_unlock(&bdev->mutex);
849
850 if (!err)
851 return value & (BCM2048_AUDIO_ROUTE_DAC |
852 BCM2048_AUDIO_ROUTE_I2S);
853
854 return err;
855 }
856
bcm2048_set_dac_output(struct bcm2048_device * bdev,u8 channels)857 static int bcm2048_set_dac_output(struct bcm2048_device *bdev, u8 channels)
858 {
859 int err;
860
861 mutex_lock(&bdev->mutex);
862
863 bdev->cache_fm_audio_ctrl0 &= ~(BCM2048_DAC_OUTPUT_LEFT |
864 BCM2048_DAC_OUTPUT_RIGHT);
865 bdev->cache_fm_audio_ctrl0 |= channels;
866
867 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0,
868 bdev->cache_fm_audio_ctrl0);
869
870 mutex_unlock(&bdev->mutex);
871 return err;
872 }
873
bcm2048_get_dac_output(struct bcm2048_device * bdev)874 static int bcm2048_get_dac_output(struct bcm2048_device *bdev)
875 {
876 int err;
877 u8 value;
878
879 mutex_lock(&bdev->mutex);
880
881 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_AUDIO_CTRL0, &value);
882
883 mutex_unlock(&bdev->mutex);
884
885 if (!err)
886 return value & (BCM2048_DAC_OUTPUT_LEFT |
887 BCM2048_DAC_OUTPUT_RIGHT);
888
889 return err;
890 }
891
bcm2048_set_fm_search_rssi_threshold(struct bcm2048_device * bdev,u8 threshold)892 static int bcm2048_set_fm_search_rssi_threshold(struct bcm2048_device *bdev,
893 u8 threshold)
894 {
895 int err;
896
897 mutex_lock(&bdev->mutex);
898
899 threshold &= BCM2048_SEARCH_RSSI_THRESHOLD;
900 bdev->cache_fm_search_ctrl0 &= ~BCM2048_SEARCH_RSSI_THRESHOLD;
901 bdev->cache_fm_search_ctrl0 |= threshold;
902
903 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_SEARCH_CTRL0,
904 bdev->cache_fm_search_ctrl0);
905
906 mutex_unlock(&bdev->mutex);
907 return err;
908 }
909
bcm2048_get_fm_search_rssi_threshold(struct bcm2048_device * bdev)910 static int bcm2048_get_fm_search_rssi_threshold(struct bcm2048_device *bdev)
911 {
912 int err;
913 u8 value;
914
915 mutex_lock(&bdev->mutex);
916
917 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_SEARCH_CTRL0, &value);
918
919 mutex_unlock(&bdev->mutex);
920
921 if (!err)
922 return value & BCM2048_SEARCH_RSSI_THRESHOLD;
923
924 return err;
925 }
926
bcm2048_set_fm_search_mode_direction(struct bcm2048_device * bdev,u8 direction)927 static int bcm2048_set_fm_search_mode_direction(struct bcm2048_device *bdev,
928 u8 direction)
929 {
930 int err;
931
932 mutex_lock(&bdev->mutex);
933
934 bdev->cache_fm_search_ctrl0 &= ~BCM2048_SEARCH_DIRECTION;
935
936 if (direction)
937 bdev->cache_fm_search_ctrl0 |= BCM2048_SEARCH_DIRECTION;
938
939 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_SEARCH_CTRL0,
940 bdev->cache_fm_search_ctrl0);
941
942 mutex_unlock(&bdev->mutex);
943 return err;
944 }
945
bcm2048_get_fm_search_mode_direction(struct bcm2048_device * bdev)946 static int bcm2048_get_fm_search_mode_direction(struct bcm2048_device *bdev)
947 {
948 int err;
949 u8 value;
950
951 mutex_lock(&bdev->mutex);
952
953 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_SEARCH_CTRL0, &value);
954
955 mutex_unlock(&bdev->mutex);
956
957 if (!err && (value & BCM2048_SEARCH_DIRECTION))
958 return BCM2048_SEARCH_DIRECTION_UP;
959
960 return err;
961 }
962
bcm2048_set_fm_search_tune_mode(struct bcm2048_device * bdev,u8 mode)963 static int bcm2048_set_fm_search_tune_mode(struct bcm2048_device *bdev,
964 u8 mode)
965 {
966 int err, timeout, restart_rds = 0;
967 u8 value, flags;
968
969 value = mode & BCM2048_FM_AUTO_SEARCH;
970
971 flags = BCM2048_FM_FLAG_SEARCH_TUNE_FINISHED |
972 BCM2048_FM_FLAG_SEARCH_TUNE_FAIL;
973
974 mutex_lock(&bdev->mutex);
975
976 /*
977 * If RDS is enabled, and frequency is changed, RDS quits working.
978 * Thus, always restart RDS if it's enabled. Moreover, RDS must
979 * not be enabled while changing the frequency because it can
980 * provide a race to the mutex from the workqueue handler if RDS
981 * IRQ occurs while waiting for frequency changed IRQ.
982 */
983 if (bcm2048_get_rds_no_lock(bdev)) {
984 err = bcm2048_set_rds_no_lock(bdev, 0);
985 if (err)
986 goto unlock;
987 restart_rds = 1;
988 }
989
990 err = bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK0, flags);
991
992 if (err)
993 goto unlock;
994
995 bcm2048_send_command(bdev, BCM2048_I2C_FM_SEARCH_TUNE_MODE, value);
996
997 if (mode != BCM2048_FM_AUTO_SEARCH_MODE)
998 timeout = BCM2048_DEFAULT_TIMEOUT;
999 else
1000 timeout = BCM2048_AUTO_SEARCH_TIMEOUT;
1001
1002 if (!wait_for_completion_timeout(&bdev->compl,
1003 msecs_to_jiffies(timeout)))
1004 dev_err(&bdev->client->dev, "IRQ timeout.\n");
1005
1006 if (value)
1007 if (!bdev->scan_state)
1008 err = -EIO;
1009
1010 unlock:
1011 if (restart_rds)
1012 err |= bcm2048_set_rds_no_lock(bdev, 1);
1013
1014 mutex_unlock(&bdev->mutex);
1015
1016 return err;
1017 }
1018
bcm2048_get_fm_search_tune_mode(struct bcm2048_device * bdev)1019 static int bcm2048_get_fm_search_tune_mode(struct bcm2048_device *bdev)
1020 {
1021 int err;
1022 u8 value;
1023
1024 mutex_lock(&bdev->mutex);
1025
1026 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_SEARCH_TUNE_MODE,
1027 &value);
1028
1029 mutex_unlock(&bdev->mutex);
1030
1031 if (!err)
1032 return value & BCM2048_FM_AUTO_SEARCH;
1033
1034 return err;
1035 }
1036
bcm2048_set_rds_b_block_mask(struct bcm2048_device * bdev,u16 mask)1037 static int bcm2048_set_rds_b_block_mask(struct bcm2048_device *bdev, u16 mask)
1038 {
1039 int err;
1040
1041 mutex_lock(&bdev->mutex);
1042
1043 err = bcm2048_send_command(bdev,
1044 BCM2048_I2C_RDS_BLKB_MASK0, lsb(mask));
1045 err |= bcm2048_send_command(bdev,
1046 BCM2048_I2C_RDS_BLKB_MASK1, msb(mask));
1047
1048 mutex_unlock(&bdev->mutex);
1049 return err;
1050 }
1051
bcm2048_get_rds_b_block_mask(struct bcm2048_device * bdev)1052 static int bcm2048_get_rds_b_block_mask(struct bcm2048_device *bdev)
1053 {
1054 int err;
1055 u8 lsb = 0, msb = 0;
1056
1057 mutex_lock(&bdev->mutex);
1058
1059 err = bcm2048_recv_command(bdev,
1060 BCM2048_I2C_RDS_BLKB_MASK0, &lsb);
1061 err |= bcm2048_recv_command(bdev,
1062 BCM2048_I2C_RDS_BLKB_MASK1, &msb);
1063
1064 mutex_unlock(&bdev->mutex);
1065
1066 if (!err)
1067 return compose_u16(msb, lsb);
1068
1069 return err;
1070 }
1071
bcm2048_set_rds_b_block_match(struct bcm2048_device * bdev,u16 match)1072 static int bcm2048_set_rds_b_block_match(struct bcm2048_device *bdev,
1073 u16 match)
1074 {
1075 int err;
1076
1077 mutex_lock(&bdev->mutex);
1078
1079 err = bcm2048_send_command(bdev,
1080 BCM2048_I2C_RDS_BLKB_MATCH0, lsb(match));
1081 err |= bcm2048_send_command(bdev,
1082 BCM2048_I2C_RDS_BLKB_MATCH1, msb(match));
1083
1084 mutex_unlock(&bdev->mutex);
1085 return err;
1086 }
1087
bcm2048_get_rds_b_block_match(struct bcm2048_device * bdev)1088 static int bcm2048_get_rds_b_block_match(struct bcm2048_device *bdev)
1089 {
1090 int err;
1091 u8 lsb = 0, msb = 0;
1092
1093 mutex_lock(&bdev->mutex);
1094
1095 err = bcm2048_recv_command(bdev,
1096 BCM2048_I2C_RDS_BLKB_MATCH0, &lsb);
1097 err |= bcm2048_recv_command(bdev,
1098 BCM2048_I2C_RDS_BLKB_MATCH1, &msb);
1099
1100 mutex_unlock(&bdev->mutex);
1101
1102 if (!err)
1103 return compose_u16(msb, lsb);
1104
1105 return err;
1106 }
1107
bcm2048_set_rds_pi_mask(struct bcm2048_device * bdev,u16 mask)1108 static int bcm2048_set_rds_pi_mask(struct bcm2048_device *bdev, u16 mask)
1109 {
1110 int err;
1111
1112 mutex_lock(&bdev->mutex);
1113
1114 err = bcm2048_send_command(bdev,
1115 BCM2048_I2C_RDS_PI_MASK0, lsb(mask));
1116 err |= bcm2048_send_command(bdev,
1117 BCM2048_I2C_RDS_PI_MASK1, msb(mask));
1118
1119 mutex_unlock(&bdev->mutex);
1120 return err;
1121 }
1122
bcm2048_get_rds_pi_mask(struct bcm2048_device * bdev)1123 static int bcm2048_get_rds_pi_mask(struct bcm2048_device *bdev)
1124 {
1125 int err;
1126 u8 lsb = 0, msb = 0;
1127
1128 mutex_lock(&bdev->mutex);
1129
1130 err = bcm2048_recv_command(bdev,
1131 BCM2048_I2C_RDS_PI_MASK0, &lsb);
1132 err |= bcm2048_recv_command(bdev,
1133 BCM2048_I2C_RDS_PI_MASK1, &msb);
1134
1135 mutex_unlock(&bdev->mutex);
1136
1137 if (!err)
1138 return compose_u16(msb, lsb);
1139
1140 return err;
1141 }
1142
bcm2048_set_rds_pi_match(struct bcm2048_device * bdev,u16 match)1143 static int bcm2048_set_rds_pi_match(struct bcm2048_device *bdev, u16 match)
1144 {
1145 int err;
1146
1147 mutex_lock(&bdev->mutex);
1148
1149 err = bcm2048_send_command(bdev,
1150 BCM2048_I2C_RDS_PI_MATCH0, lsb(match));
1151 err |= bcm2048_send_command(bdev,
1152 BCM2048_I2C_RDS_PI_MATCH1, msb(match));
1153
1154 mutex_unlock(&bdev->mutex);
1155 return err;
1156 }
1157
bcm2048_get_rds_pi_match(struct bcm2048_device * bdev)1158 static int bcm2048_get_rds_pi_match(struct bcm2048_device *bdev)
1159 {
1160 int err;
1161 u8 lsb = 0, msb = 0;
1162
1163 mutex_lock(&bdev->mutex);
1164
1165 err = bcm2048_recv_command(bdev,
1166 BCM2048_I2C_RDS_PI_MATCH0, &lsb);
1167 err |= bcm2048_recv_command(bdev,
1168 BCM2048_I2C_RDS_PI_MATCH1, &msb);
1169
1170 mutex_unlock(&bdev->mutex);
1171
1172 if (!err)
1173 return compose_u16(msb, lsb);
1174
1175 return err;
1176 }
1177
bcm2048_set_fm_rds_mask(struct bcm2048_device * bdev,u16 mask)1178 static int bcm2048_set_fm_rds_mask(struct bcm2048_device *bdev, u16 mask)
1179 {
1180 int err;
1181
1182 mutex_lock(&bdev->mutex);
1183
1184 err = bcm2048_send_command(bdev,
1185 BCM2048_I2C_FM_RDS_MASK0, lsb(mask));
1186 err |= bcm2048_send_command(bdev,
1187 BCM2048_I2C_FM_RDS_MASK1, msb(mask));
1188
1189 mutex_unlock(&bdev->mutex);
1190 return err;
1191 }
1192
bcm2048_get_fm_rds_mask(struct bcm2048_device * bdev)1193 static int bcm2048_get_fm_rds_mask(struct bcm2048_device *bdev)
1194 {
1195 int err;
1196 u8 value0 = 0, value1 = 0;
1197
1198 mutex_lock(&bdev->mutex);
1199
1200 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_MASK0, &value0);
1201 err |= bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_MASK1, &value1);
1202
1203 mutex_unlock(&bdev->mutex);
1204
1205 if (!err)
1206 return compose_u16(value1, value0);
1207
1208 return err;
1209 }
1210
bcm2048_get_fm_rds_flags(struct bcm2048_device * bdev)1211 static int bcm2048_get_fm_rds_flags(struct bcm2048_device *bdev)
1212 {
1213 int err;
1214 u8 value0 = 0, value1 = 0;
1215
1216 mutex_lock(&bdev->mutex);
1217
1218 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_FLAG0, &value0);
1219 err |= bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_FLAG1, &value1);
1220
1221 mutex_unlock(&bdev->mutex);
1222
1223 if (!err)
1224 return compose_u16(value1, value0);
1225
1226 return err;
1227 }
1228
bcm2048_get_region_bottom_frequency(struct bcm2048_device * bdev)1229 static int bcm2048_get_region_bottom_frequency(struct bcm2048_device *bdev)
1230 {
1231 return bdev->region_info.bottom_frequency;
1232 }
1233
bcm2048_get_region_top_frequency(struct bcm2048_device * bdev)1234 static int bcm2048_get_region_top_frequency(struct bcm2048_device *bdev)
1235 {
1236 return bdev->region_info.top_frequency;
1237 }
1238
bcm2048_set_fm_best_tune_mode(struct bcm2048_device * bdev,u8 mode)1239 static int bcm2048_set_fm_best_tune_mode(struct bcm2048_device *bdev, u8 mode)
1240 {
1241 int err;
1242 u8 value = 0;
1243
1244 mutex_lock(&bdev->mutex);
1245
1246 /* Perform read as the manual indicates */
1247 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_BEST_TUNE_MODE,
1248 &value);
1249 value &= ~BCM2048_BEST_TUNE_MODE;
1250
1251 if (mode)
1252 value |= BCM2048_BEST_TUNE_MODE;
1253 err |= bcm2048_send_command(bdev, BCM2048_I2C_FM_BEST_TUNE_MODE,
1254 value);
1255
1256 mutex_unlock(&bdev->mutex);
1257 return err;
1258 }
1259
bcm2048_get_fm_best_tune_mode(struct bcm2048_device * bdev)1260 static int bcm2048_get_fm_best_tune_mode(struct bcm2048_device *bdev)
1261 {
1262 int err;
1263 u8 value;
1264
1265 mutex_lock(&bdev->mutex);
1266
1267 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_BEST_TUNE_MODE,
1268 &value);
1269
1270 mutex_unlock(&bdev->mutex);
1271
1272 if (!err && (value & BCM2048_BEST_TUNE_MODE))
1273 return BCM2048_ITEM_ENABLED;
1274
1275 return err;
1276 }
1277
bcm2048_get_fm_carrier_error(struct bcm2048_device * bdev)1278 static int bcm2048_get_fm_carrier_error(struct bcm2048_device *bdev)
1279 {
1280 int err = 0;
1281 s8 value;
1282
1283 mutex_lock(&bdev->mutex);
1284 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_CARRIER, &value);
1285 mutex_unlock(&bdev->mutex);
1286
1287 if (!err)
1288 return value;
1289
1290 return err;
1291 }
1292
bcm2048_get_fm_rssi(struct bcm2048_device * bdev)1293 static int bcm2048_get_fm_rssi(struct bcm2048_device *bdev)
1294 {
1295 int err;
1296 s8 value;
1297
1298 mutex_lock(&bdev->mutex);
1299 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RSSI, &value);
1300 mutex_unlock(&bdev->mutex);
1301
1302 if (!err)
1303 return value;
1304
1305 return err;
1306 }
1307
bcm2048_set_rds_wline(struct bcm2048_device * bdev,u8 wline)1308 static int bcm2048_set_rds_wline(struct bcm2048_device *bdev, u8 wline)
1309 {
1310 int err;
1311
1312 mutex_lock(&bdev->mutex);
1313
1314 err = bcm2048_send_command(bdev, BCM2048_I2C_RDS_WLINE, wline);
1315
1316 if (!err)
1317 bdev->fifo_size = wline;
1318
1319 mutex_unlock(&bdev->mutex);
1320 return err;
1321 }
1322
bcm2048_get_rds_wline(struct bcm2048_device * bdev)1323 static int bcm2048_get_rds_wline(struct bcm2048_device *bdev)
1324 {
1325 int err;
1326 u8 value;
1327
1328 mutex_lock(&bdev->mutex);
1329
1330 err = bcm2048_recv_command(bdev, BCM2048_I2C_RDS_WLINE, &value);
1331
1332 mutex_unlock(&bdev->mutex);
1333
1334 if (!err) {
1335 bdev->fifo_size = value;
1336 return value;
1337 }
1338
1339 return err;
1340 }
1341
bcm2048_checkrev(struct bcm2048_device * bdev)1342 static int bcm2048_checkrev(struct bcm2048_device *bdev)
1343 {
1344 int err;
1345 u8 version;
1346
1347 mutex_lock(&bdev->mutex);
1348
1349 err = bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_REV, &version);
1350
1351 mutex_unlock(&bdev->mutex);
1352
1353 if (!err) {
1354 dev_info(&bdev->client->dev, "BCM2048 Version 0x%x\n",
1355 version);
1356 return version;
1357 }
1358
1359 return err;
1360 }
1361
bcm2048_get_rds_rt(struct bcm2048_device * bdev,char * data)1362 static int bcm2048_get_rds_rt(struct bcm2048_device *bdev, char *data)
1363 {
1364 int err = 0, i, j = 0, ce = 0, cr = 0;
1365 char data_buffer[BCM2048_MAX_RDS_RT+1];
1366
1367 mutex_lock(&bdev->mutex);
1368
1369 if (!bdev->rds_info.text_len) {
1370 err = -EINVAL;
1371 goto unlock;
1372 }
1373
1374 for (i = 0; i < BCM2048_MAX_RDS_RT; i++) {
1375 if (bdev->rds_info.rds_rt[i]) {
1376 ce = i;
1377 /* Skip the carriage return */
1378 if (bdev->rds_info.rds_rt[i] != 0x0d) {
1379 data_buffer[j++] = bdev->rds_info.rds_rt[i];
1380 } else {
1381 cr = i;
1382 break;
1383 }
1384 }
1385 }
1386
1387 if (j <= BCM2048_MAX_RDS_RT)
1388 data_buffer[j] = 0;
1389
1390 for (i = 0; i < BCM2048_MAX_RDS_RT; i++) {
1391 if (!bdev->rds_info.rds_rt[i]) {
1392 if (cr && (i < cr)) {
1393 err = -EBUSY;
1394 goto unlock;
1395 }
1396 if (i < ce) {
1397 if (cr && (i >= cr))
1398 break;
1399 err = -EBUSY;
1400 goto unlock;
1401 }
1402 }
1403 }
1404
1405 memcpy(data, data_buffer, sizeof(data_buffer));
1406
1407 unlock:
1408 mutex_unlock(&bdev->mutex);
1409 return err;
1410 }
1411
bcm2048_get_rds_ps(struct bcm2048_device * bdev,char * data)1412 static int bcm2048_get_rds_ps(struct bcm2048_device *bdev, char *data)
1413 {
1414 int err = 0, i, j = 0;
1415 char data_buffer[BCM2048_MAX_RDS_PS+1];
1416
1417 mutex_lock(&bdev->mutex);
1418
1419 if (!bdev->rds_info.text_len) {
1420 err = -EINVAL;
1421 goto unlock;
1422 }
1423
1424 for (i = 0; i < BCM2048_MAX_RDS_PS; i++) {
1425 if (bdev->rds_info.rds_ps[i]) {
1426 data_buffer[j++] = bdev->rds_info.rds_ps[i];
1427 } else {
1428 if (i < (BCM2048_MAX_RDS_PS - 1)) {
1429 err = -EBUSY;
1430 goto unlock;
1431 }
1432 }
1433 }
1434
1435 if (j <= BCM2048_MAX_RDS_PS)
1436 data_buffer[j] = 0;
1437
1438 memcpy(data, data_buffer, sizeof(data_buffer));
1439
1440 unlock:
1441 mutex_unlock(&bdev->mutex);
1442 return err;
1443 }
1444
bcm2048_parse_rds_pi(struct bcm2048_device * bdev)1445 static void bcm2048_parse_rds_pi(struct bcm2048_device *bdev)
1446 {
1447 int i, cnt = 0;
1448 u16 pi;
1449
1450 for (i = 0; i < bdev->fifo_size; i += BCM2048_RDS_FIFO_DUPLE_SIZE) {
1451
1452 /* Block A match, only data without crc errors taken */
1453 if (bdev->rds_info.radio_text[i] == BCM2048_RDS_BLOCK_A) {
1454
1455 pi = (bdev->rds_info.radio_text[i+1] << 8) +
1456 bdev->rds_info.radio_text[i+2];
1457
1458 if (!bdev->rds_info.rds_pi) {
1459 bdev->rds_info.rds_pi = pi;
1460 return;
1461 }
1462 if (pi != bdev->rds_info.rds_pi) {
1463 cnt++;
1464 if (cnt > 3) {
1465 bdev->rds_info.rds_pi = pi;
1466 cnt = 0;
1467 }
1468 } else {
1469 cnt = 0;
1470 }
1471 }
1472 }
1473 }
1474
bcm2048_rds_block_crc(struct bcm2048_device * bdev,int i)1475 static int bcm2048_rds_block_crc(struct bcm2048_device *bdev, int i)
1476 {
1477 return bdev->rds_info.radio_text[i] & BCM2048_RDS_CRC_MASK;
1478 }
1479
bcm2048_parse_rds_rt_block(struct bcm2048_device * bdev,int i,int index,int crc)1480 static void bcm2048_parse_rds_rt_block(struct bcm2048_device *bdev, int i,
1481 int index, int crc)
1482 {
1483 /* Good data will overwrite poor data */
1484 if (crc) {
1485 if (!bdev->rds_info.rds_rt[index])
1486 bdev->rds_info.rds_rt[index] =
1487 bdev->rds_info.radio_text[i+1];
1488 if (!bdev->rds_info.rds_rt[index+1])
1489 bdev->rds_info.rds_rt[index+1] =
1490 bdev->rds_info.radio_text[i+2];
1491 } else {
1492 bdev->rds_info.rds_rt[index] = bdev->rds_info.radio_text[i+1];
1493 bdev->rds_info.rds_rt[index+1] =
1494 bdev->rds_info.radio_text[i+2];
1495 }
1496 }
1497
bcm2048_parse_rt_match_b(struct bcm2048_device * bdev,int i)1498 static int bcm2048_parse_rt_match_b(struct bcm2048_device *bdev, int i)
1499 {
1500 int crc, rt_id, rt_group_b, rt_ab, index = 0;
1501
1502 crc = bcm2048_rds_block_crc(bdev, i);
1503
1504 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1505 return -EIO;
1506
1507 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1508 BCM2048_RDS_BLOCK_B) {
1509
1510 rt_id = bdev->rds_info.radio_text[i+1] &
1511 BCM2048_RDS_BLOCK_MASK;
1512 rt_group_b = bdev->rds_info.radio_text[i+1] &
1513 BCM2048_RDS_GROUP_AB_MASK;
1514 rt_ab = bdev->rds_info.radio_text[i+2] &
1515 BCM2048_RDS_RT_AB_MASK;
1516
1517 if (rt_group_b != bdev->rds_info.rds_rt_group_b) {
1518 memset(bdev->rds_info.rds_rt, 0,
1519 sizeof(bdev->rds_info.rds_rt));
1520 bdev->rds_info.rds_rt_group_b = rt_group_b;
1521 }
1522
1523 if (rt_id == BCM2048_RDS_RT) {
1524 /* A to B or (vice versa), means: clear screen */
1525 if (rt_ab != bdev->rds_info.rds_rt_ab) {
1526 memset(bdev->rds_info.rds_rt, 0,
1527 sizeof(bdev->rds_info.rds_rt));
1528 bdev->rds_info.rds_rt_ab = rt_ab;
1529 }
1530
1531 index = bdev->rds_info.radio_text[i+2] &
1532 BCM2048_RDS_RT_INDEX;
1533
1534 if (bdev->rds_info.rds_rt_group_b)
1535 index <<= 1;
1536 else
1537 index <<= 2;
1538
1539 return index;
1540 }
1541 }
1542
1543 return -EIO;
1544 }
1545
bcm2048_parse_rt_match_c(struct bcm2048_device * bdev,int i,int index)1546 static int bcm2048_parse_rt_match_c(struct bcm2048_device *bdev, int i,
1547 int index)
1548 {
1549 int crc;
1550
1551 crc = bcm2048_rds_block_crc(bdev, i);
1552
1553 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1554 return 0;
1555
1556 BUG_ON((index+2) >= BCM2048_MAX_RDS_RT);
1557
1558 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1559 BCM2048_RDS_BLOCK_C) {
1560 if (bdev->rds_info.rds_rt_group_b)
1561 return 1;
1562 bcm2048_parse_rds_rt_block(bdev, i, index, crc);
1563 return 1;
1564 }
1565
1566 return 0;
1567 }
1568
bcm2048_parse_rt_match_d(struct bcm2048_device * bdev,int i,int index)1569 static void bcm2048_parse_rt_match_d(struct bcm2048_device *bdev, int i,
1570 int index)
1571 {
1572 int crc;
1573
1574 crc = bcm2048_rds_block_crc(bdev, i);
1575
1576 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1577 return;
1578
1579 BUG_ON((index+4) >= BCM2048_MAX_RDS_RT);
1580
1581 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1582 BCM2048_RDS_BLOCK_D)
1583 bcm2048_parse_rds_rt_block(bdev, i, index+2, crc);
1584 }
1585
bcm2048_parse_rds_rt(struct bcm2048_device * bdev)1586 static void bcm2048_parse_rds_rt(struct bcm2048_device *bdev)
1587 {
1588 int i, index = 0, crc, match_b = 0, match_c = 0, match_d = 0;
1589
1590 for (i = 0; i < bdev->fifo_size; i += BCM2048_RDS_FIFO_DUPLE_SIZE) {
1591
1592 if (match_b) {
1593 match_b = 0;
1594 index = bcm2048_parse_rt_match_b(bdev, i);
1595 if (index >= 0 && index <= (BCM2048_MAX_RDS_RT - 5))
1596 match_c = 1;
1597 continue;
1598 } else if (match_c) {
1599 match_c = 0;
1600 if (bcm2048_parse_rt_match_c(bdev, i, index))
1601 match_d = 1;
1602 continue;
1603 } else if (match_d) {
1604 match_d = 0;
1605 bcm2048_parse_rt_match_d(bdev, i, index);
1606 continue;
1607 }
1608
1609 /* Skip erroneous blocks due to messed up A block altogether */
1610 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK)
1611 == BCM2048_RDS_BLOCK_A) {
1612 crc = bcm2048_rds_block_crc(bdev, i);
1613 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1614 continue;
1615 /* Syncronize to a good RDS PI */
1616 if (((bdev->rds_info.radio_text[i+1] << 8) +
1617 bdev->rds_info.radio_text[i+2]) ==
1618 bdev->rds_info.rds_pi)
1619 match_b = 1;
1620 }
1621 }
1622 }
1623
bcm2048_parse_rds_ps_block(struct bcm2048_device * bdev,int i,int index,int crc)1624 static void bcm2048_parse_rds_ps_block(struct bcm2048_device *bdev, int i,
1625 int index, int crc)
1626 {
1627 /* Good data will overwrite poor data */
1628 if (crc) {
1629 if (!bdev->rds_info.rds_ps[index])
1630 bdev->rds_info.rds_ps[index] =
1631 bdev->rds_info.radio_text[i+1];
1632 if (!bdev->rds_info.rds_ps[index+1])
1633 bdev->rds_info.rds_ps[index+1] =
1634 bdev->rds_info.radio_text[i+2];
1635 } else {
1636 bdev->rds_info.rds_ps[index] = bdev->rds_info.radio_text[i+1];
1637 bdev->rds_info.rds_ps[index+1] =
1638 bdev->rds_info.radio_text[i+2];
1639 }
1640 }
1641
bcm2048_parse_ps_match_c(struct bcm2048_device * bdev,int i,int index)1642 static int bcm2048_parse_ps_match_c(struct bcm2048_device *bdev, int i,
1643 int index)
1644 {
1645 int crc;
1646
1647 crc = bcm2048_rds_block_crc(bdev, i);
1648
1649 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1650 return 0;
1651
1652 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1653 BCM2048_RDS_BLOCK_C)
1654 return 1;
1655
1656 return 0;
1657 }
1658
bcm2048_parse_ps_match_d(struct bcm2048_device * bdev,int i,int index)1659 static void bcm2048_parse_ps_match_d(struct bcm2048_device *bdev, int i,
1660 int index)
1661 {
1662 int crc;
1663
1664 crc = bcm2048_rds_block_crc(bdev, i);
1665
1666 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1667 return;
1668
1669 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1670 BCM2048_RDS_BLOCK_D)
1671 bcm2048_parse_rds_ps_block(bdev, i, index, crc);
1672 }
1673
bcm2048_parse_ps_match_b(struct bcm2048_device * bdev,int i)1674 static int bcm2048_parse_ps_match_b(struct bcm2048_device *bdev, int i)
1675 {
1676 int crc, index, ps_id, ps_group;
1677
1678 crc = bcm2048_rds_block_crc(bdev, i);
1679
1680 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1681 return -EIO;
1682
1683 /* Block B Radio PS match */
1684 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK) ==
1685 BCM2048_RDS_BLOCK_B) {
1686 ps_id = bdev->rds_info.radio_text[i+1] &
1687 BCM2048_RDS_BLOCK_MASK;
1688 ps_group = bdev->rds_info.radio_text[i+1] &
1689 BCM2048_RDS_GROUP_AB_MASK;
1690
1691 /*
1692 * Poor RSSI will lead to RDS data corruption
1693 * So using 3 (same) sequential values to justify major changes
1694 */
1695 if (ps_group != bdev->rds_info.rds_ps_group) {
1696 if (crc == BCM2048_RDS_CRC_NONE) {
1697 bdev->rds_info.rds_ps_group_cnt++;
1698 if (bdev->rds_info.rds_ps_group_cnt > 2) {
1699 bdev->rds_info.rds_ps_group = ps_group;
1700 bdev->rds_info.rds_ps_group_cnt = 0;
1701 dev_err(&bdev->client->dev,
1702 "RDS PS Group change!\n");
1703 } else {
1704 return -EIO;
1705 }
1706 } else {
1707 bdev->rds_info.rds_ps_group_cnt = 0;
1708 }
1709 }
1710
1711 if (ps_id == BCM2048_RDS_PS) {
1712 index = bdev->rds_info.radio_text[i+2] &
1713 BCM2048_RDS_PS_INDEX;
1714 index <<= 1;
1715 return index;
1716 }
1717 }
1718
1719 return -EIO;
1720 }
1721
bcm2048_parse_rds_ps(struct bcm2048_device * bdev)1722 static void bcm2048_parse_rds_ps(struct bcm2048_device *bdev)
1723 {
1724 int i, index = 0, crc, match_b = 0, match_c = 0, match_d = 0;
1725
1726 for (i = 0; i < bdev->fifo_size; i += BCM2048_RDS_FIFO_DUPLE_SIZE) {
1727
1728 if (match_b) {
1729 match_b = 0;
1730 index = bcm2048_parse_ps_match_b(bdev, i);
1731 if (index >= 0 && index < (BCM2048_MAX_RDS_PS - 1))
1732 match_c = 1;
1733 continue;
1734 } else if (match_c) {
1735 match_c = 0;
1736 if (bcm2048_parse_ps_match_c(bdev, i, index))
1737 match_d = 1;
1738 continue;
1739 } else if (match_d) {
1740 match_d = 0;
1741 bcm2048_parse_ps_match_d(bdev, i, index);
1742 continue;
1743 }
1744
1745 /* Skip erroneous blocks due to messed up A block altogether */
1746 if ((bdev->rds_info.radio_text[i] & BCM2048_RDS_BLOCK_MASK)
1747 == BCM2048_RDS_BLOCK_A) {
1748 crc = bcm2048_rds_block_crc(bdev, i);
1749 if (crc == BCM2048_RDS_CRC_UNRECOVARABLE)
1750 continue;
1751 /* Syncronize to a good RDS PI */
1752 if (((bdev->rds_info.radio_text[i+1] << 8) +
1753 bdev->rds_info.radio_text[i+2]) ==
1754 bdev->rds_info.rds_pi)
1755 match_b = 1;
1756 }
1757 }
1758 }
1759
bcm2048_rds_fifo_receive(struct bcm2048_device * bdev)1760 static void bcm2048_rds_fifo_receive(struct bcm2048_device *bdev)
1761 {
1762 int err;
1763
1764 mutex_lock(&bdev->mutex);
1765
1766 err = bcm2048_recv_duples(bdev, BCM2048_I2C_RDS_DATA,
1767 bdev->rds_info.radio_text, bdev->fifo_size);
1768 if (err != 2) {
1769 dev_err(&bdev->client->dev, "RDS Read problem\n");
1770 mutex_unlock(&bdev->mutex);
1771 return;
1772 }
1773
1774 bdev->rds_info.text_len = bdev->fifo_size;
1775
1776 bcm2048_parse_rds_pi(bdev);
1777 bcm2048_parse_rds_rt(bdev);
1778 bcm2048_parse_rds_ps(bdev);
1779
1780 mutex_unlock(&bdev->mutex);
1781
1782 wake_up_interruptible(&bdev->read_queue);
1783 }
1784
bcm2048_get_rds_data(struct bcm2048_device * bdev,char * data)1785 static int bcm2048_get_rds_data(struct bcm2048_device *bdev, char *data)
1786 {
1787 int err = 0, i, p = 0;
1788 char *data_buffer;
1789
1790 mutex_lock(&bdev->mutex);
1791
1792 if (!bdev->rds_info.text_len) {
1793 err = -EINVAL;
1794 goto unlock;
1795 }
1796
1797 data_buffer = kcalloc(BCM2048_MAX_RDS_RADIO_TEXT, 5, GFP_KERNEL);
1798 if (!data_buffer) {
1799 err = -ENOMEM;
1800 goto unlock;
1801 }
1802
1803 for (i = 0; i < bdev->rds_info.text_len; i++) {
1804 p += sprintf(data_buffer+p, "%x ",
1805 bdev->rds_info.radio_text[i]);
1806 }
1807
1808 memcpy(data, data_buffer, p);
1809 kfree(data_buffer);
1810
1811 unlock:
1812 mutex_unlock(&bdev->mutex);
1813 return err;
1814 }
1815
1816 /*
1817 * BCM2048 default initialization sequence
1818 */
bcm2048_init(struct bcm2048_device * bdev)1819 static int bcm2048_init(struct bcm2048_device *bdev)
1820 {
1821 int err;
1822
1823 err = bcm2048_set_power_state(bdev, BCM2048_POWER_ON);
1824 if (err < 0)
1825 goto exit;
1826
1827 err = bcm2048_set_audio_route(bdev, BCM2048_AUDIO_ROUTE_DAC);
1828 if (err < 0)
1829 goto exit;
1830
1831 err = bcm2048_set_dac_output(bdev, BCM2048_DAC_OUTPUT_LEFT |
1832 BCM2048_DAC_OUTPUT_RIGHT);
1833
1834 exit:
1835 return err;
1836 }
1837
1838 /*
1839 * BCM2048 default deinitialization sequence
1840 */
bcm2048_deinit(struct bcm2048_device * bdev)1841 static int bcm2048_deinit(struct bcm2048_device *bdev)
1842 {
1843 int err;
1844
1845 err = bcm2048_set_audio_route(bdev, 0);
1846 if (err < 0)
1847 goto exit;
1848
1849 err = bcm2048_set_dac_output(bdev, 0);
1850 if (err < 0)
1851 goto exit;
1852
1853 err = bcm2048_set_power_state(bdev, BCM2048_POWER_OFF);
1854 if (err < 0)
1855 goto exit;
1856
1857 exit:
1858 return err;
1859 }
1860
1861 /*
1862 * BCM2048 probe sequence
1863 */
bcm2048_probe(struct bcm2048_device * bdev)1864 static int bcm2048_probe(struct bcm2048_device *bdev)
1865 {
1866 int err;
1867
1868 err = bcm2048_set_power_state(bdev, BCM2048_POWER_ON);
1869 if (err < 0)
1870 goto unlock;
1871
1872 err = bcm2048_checkrev(bdev);
1873 if (err < 0)
1874 goto unlock;
1875
1876 err = bcm2048_set_mute(bdev, BCM2048_DEFAULT_MUTE);
1877 if (err < 0)
1878 goto unlock;
1879
1880 err = bcm2048_set_region(bdev, BCM2048_DEFAULT_REGION);
1881 if (err < 0)
1882 goto unlock;
1883
1884 err = bcm2048_set_fm_search_rssi_threshold(bdev,
1885 BCM2048_DEFAULT_RSSI_THRESHOLD);
1886 if (err < 0)
1887 goto unlock;
1888
1889 err = bcm2048_set_fm_automatic_stereo_mono(bdev, BCM2048_ITEM_ENABLED);
1890 if (err < 0)
1891 goto unlock;
1892
1893 err = bcm2048_get_rds_wline(bdev);
1894 if (err < BCM2048_DEFAULT_RDS_WLINE)
1895 err = bcm2048_set_rds_wline(bdev, BCM2048_DEFAULT_RDS_WLINE);
1896 if (err < 0)
1897 goto unlock;
1898
1899 err = bcm2048_set_power_state(bdev, BCM2048_POWER_OFF);
1900
1901 init_waitqueue_head(&bdev->read_queue);
1902 bdev->rds_data_available = 0;
1903 bdev->rd_index = 0;
1904 bdev->users = 0;
1905
1906 unlock:
1907 return err;
1908 }
1909
1910 /*
1911 * BCM2048 workqueue handler
1912 */
bcm2048_work(struct work_struct * work)1913 static void bcm2048_work(struct work_struct *work)
1914 {
1915 struct bcm2048_device *bdev;
1916 u8 flag_lsb = 0, flag_msb = 0, flags;
1917
1918 bdev = container_of(work, struct bcm2048_device, work);
1919 bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_FLAG0, &flag_lsb);
1920 bcm2048_recv_command(bdev, BCM2048_I2C_FM_RDS_FLAG1, &flag_msb);
1921
1922 if (flag_lsb & (BCM2048_FM_FLAG_SEARCH_TUNE_FINISHED |
1923 BCM2048_FM_FLAG_SEARCH_TUNE_FAIL)) {
1924
1925 if (flag_lsb & BCM2048_FM_FLAG_SEARCH_TUNE_FAIL)
1926 bdev->scan_state = BCM2048_SCAN_FAIL;
1927 else
1928 bdev->scan_state = BCM2048_SCAN_OK;
1929
1930 complete(&bdev->compl);
1931 }
1932
1933 if (flag_msb & BCM2048_RDS_FLAG_FIFO_WLINE) {
1934 bcm2048_rds_fifo_receive(bdev);
1935 if (bdev->rds_state) {
1936 flags = BCM2048_RDS_FLAG_FIFO_WLINE;
1937 bcm2048_send_command(bdev, BCM2048_I2C_FM_RDS_MASK1,
1938 flags);
1939 }
1940 bdev->rds_data_available = 1;
1941 bdev->rd_index = 0; /* new data, new start */
1942 }
1943 }
1944
1945 /*
1946 * BCM2048 interrupt handler
1947 */
bcm2048_handler(int irq,void * dev)1948 static irqreturn_t bcm2048_handler(int irq, void *dev)
1949 {
1950 struct bcm2048_device *bdev = dev;
1951
1952 dev_dbg(&bdev->client->dev, "IRQ called, queuing work\n");
1953 if (bdev->power_state)
1954 schedule_work(&bdev->work);
1955
1956 return IRQ_HANDLED;
1957 }
1958
1959 /*
1960 * BCM2048 sysfs interface definitions
1961 */
1962 #define property_write(prop, type, mask, check) \
1963 static ssize_t bcm2048_##prop##_write(struct device *dev, \
1964 struct device_attribute *attr, \
1965 const char *buf, \
1966 size_t count) \
1967 { \
1968 struct bcm2048_device *bdev = dev_get_drvdata(dev); \
1969 type value; \
1970 int err; \
1971 \
1972 if (!bdev) \
1973 return -ENODEV; \
1974 \
1975 if (sscanf(buf, mask, &value) != 1) \
1976 return -EINVAL; \
1977 \
1978 if (check) \
1979 return -EDOM; \
1980 \
1981 err = bcm2048_set_##prop(bdev, value); \
1982 \
1983 return err < 0 ? err : count; \
1984 }
1985
1986 #define property_read(prop, size, mask) \
1987 static ssize_t bcm2048_##prop##_read(struct device *dev, \
1988 struct device_attribute *attr, \
1989 char *buf) \
1990 { \
1991 struct bcm2048_device *bdev = dev_get_drvdata(dev); \
1992 int value; \
1993 \
1994 if (!bdev) \
1995 return -ENODEV; \
1996 \
1997 value = bcm2048_get_##prop(bdev); \
1998 \
1999 if (value >= 0) \
2000 value = sprintf(buf, mask "\n", value); \
2001 \
2002 return value; \
2003 }
2004
2005 #define property_signed_read(prop, size, mask) \
2006 static ssize_t bcm2048_##prop##_read(struct device *dev, \
2007 struct device_attribute *attr, \
2008 char *buf) \
2009 { \
2010 struct bcm2048_device *bdev = dev_get_drvdata(dev); \
2011 size value; \
2012 \
2013 if (!bdev) \
2014 return -ENODEV; \
2015 \
2016 value = bcm2048_get_##prop(bdev); \
2017 \
2018 value = sprintf(buf, mask "\n", value); \
2019 \
2020 return value; \
2021 }
2022
2023 #define DEFINE_SYSFS_PROPERTY(prop, signal, size, mask, check) \
2024 property_write(prop, signal size, mask, check) \
2025 property_read(prop, size, mask)
2026
2027 #define property_str_read(prop, size) \
2028 static ssize_t bcm2048_##prop##_read(struct device *dev, \
2029 struct device_attribute *attr, \
2030 char *buf) \
2031 { \
2032 struct bcm2048_device *bdev = dev_get_drvdata(dev); \
2033 int count; \
2034 u8 *out; \
2035 \
2036 if (!bdev) \
2037 return -ENODEV; \
2038 \
2039 out = kzalloc(size + 1, GFP_KERNEL); \
2040 if (!out) \
2041 return -ENOMEM; \
2042 \
2043 bcm2048_get_##prop(bdev, out); \
2044 count = sprintf(buf, "%s\n", out); \
2045 \
2046 kfree(out); \
2047 \
2048 return count; \
2049 }
2050
2051 DEFINE_SYSFS_PROPERTY(power_state, unsigned, int, "%u", 0)
2052 DEFINE_SYSFS_PROPERTY(mute, unsigned, int, "%u", 0)
2053 DEFINE_SYSFS_PROPERTY(audio_route, unsigned, int, "%u", 0)
2054 DEFINE_SYSFS_PROPERTY(dac_output, unsigned, int, "%u", 0)
2055
2056 DEFINE_SYSFS_PROPERTY(fm_hi_lo_injection, unsigned, int, "%u", 0)
2057 DEFINE_SYSFS_PROPERTY(fm_frequency, unsigned, int, "%u", 0)
2058 DEFINE_SYSFS_PROPERTY(fm_af_frequency, unsigned, int, "%u", 0)
2059 DEFINE_SYSFS_PROPERTY(fm_deemphasis, unsigned, int, "%u", 0)
2060 DEFINE_SYSFS_PROPERTY(fm_rds_mask, unsigned, int, "%u", 0)
2061 DEFINE_SYSFS_PROPERTY(fm_best_tune_mode, unsigned, int, "%u", 0)
2062 DEFINE_SYSFS_PROPERTY(fm_search_rssi_threshold, unsigned, int, "%u", 0)
2063 DEFINE_SYSFS_PROPERTY(fm_search_mode_direction, unsigned, int, "%u", 0)
2064 DEFINE_SYSFS_PROPERTY(fm_search_tune_mode, unsigned, int, "%u", value > 3)
2065
2066 DEFINE_SYSFS_PROPERTY(rds, unsigned, int, "%u", 0)
2067 DEFINE_SYSFS_PROPERTY(rds_b_block_mask, unsigned, int, "%u", 0)
2068 DEFINE_SYSFS_PROPERTY(rds_b_block_match, unsigned, int, "%u", 0)
2069 DEFINE_SYSFS_PROPERTY(rds_pi_mask, unsigned, int, "%u", 0)
2070 DEFINE_SYSFS_PROPERTY(rds_pi_match, unsigned, int, "%u", 0)
2071 DEFINE_SYSFS_PROPERTY(rds_wline, unsigned, int, "%u", 0)
2072 property_read(rds_pi, unsigned int, "%x")
2073 property_str_read(rds_rt, (BCM2048_MAX_RDS_RT + 1))
2074 property_str_read(rds_ps, (BCM2048_MAX_RDS_PS + 1))
2075
2076 property_read(fm_rds_flags, unsigned int, "%u")
2077 property_str_read(rds_data, BCM2048_MAX_RDS_RADIO_TEXT*5)
2078
2079 property_read(region_bottom_frequency, unsigned int, "%u")
2080 property_read(region_top_frequency, unsigned int, "%u")
2081 property_signed_read(fm_carrier_error, int, "%d")
2082 property_signed_read(fm_rssi, int, "%d")
2083 DEFINE_SYSFS_PROPERTY(region, unsigned, int, "%u", 0)
2084
2085 static struct device_attribute attrs[] = {
2086 __ATTR(power_state, S_IRUGO | S_IWUSR, bcm2048_power_state_read,
2087 bcm2048_power_state_write),
2088 __ATTR(mute, S_IRUGO | S_IWUSR, bcm2048_mute_read,
2089 bcm2048_mute_write),
2090 __ATTR(audio_route, S_IRUGO | S_IWUSR, bcm2048_audio_route_read,
2091 bcm2048_audio_route_write),
2092 __ATTR(dac_output, S_IRUGO | S_IWUSR, bcm2048_dac_output_read,
2093 bcm2048_dac_output_write),
2094 __ATTR(fm_hi_lo_injection, S_IRUGO | S_IWUSR,
2095 bcm2048_fm_hi_lo_injection_read,
2096 bcm2048_fm_hi_lo_injection_write),
2097 __ATTR(fm_frequency, S_IRUGO | S_IWUSR, bcm2048_fm_frequency_read,
2098 bcm2048_fm_frequency_write),
2099 __ATTR(fm_af_frequency, S_IRUGO | S_IWUSR,
2100 bcm2048_fm_af_frequency_read,
2101 bcm2048_fm_af_frequency_write),
2102 __ATTR(fm_deemphasis, S_IRUGO | S_IWUSR, bcm2048_fm_deemphasis_read,
2103 bcm2048_fm_deemphasis_write),
2104 __ATTR(fm_rds_mask, S_IRUGO | S_IWUSR, bcm2048_fm_rds_mask_read,
2105 bcm2048_fm_rds_mask_write),
2106 __ATTR(fm_best_tune_mode, S_IRUGO | S_IWUSR,
2107 bcm2048_fm_best_tune_mode_read,
2108 bcm2048_fm_best_tune_mode_write),
2109 __ATTR(fm_search_rssi_threshold, S_IRUGO | S_IWUSR,
2110 bcm2048_fm_search_rssi_threshold_read,
2111 bcm2048_fm_search_rssi_threshold_write),
2112 __ATTR(fm_search_mode_direction, S_IRUGO | S_IWUSR,
2113 bcm2048_fm_search_mode_direction_read,
2114 bcm2048_fm_search_mode_direction_write),
2115 __ATTR(fm_search_tune_mode, S_IRUGO | S_IWUSR,
2116 bcm2048_fm_search_tune_mode_read,
2117 bcm2048_fm_search_tune_mode_write),
2118 __ATTR(rds, S_IRUGO | S_IWUSR, bcm2048_rds_read,
2119 bcm2048_rds_write),
2120 __ATTR(rds_b_block_mask, S_IRUGO | S_IWUSR,
2121 bcm2048_rds_b_block_mask_read,
2122 bcm2048_rds_b_block_mask_write),
2123 __ATTR(rds_b_block_match, S_IRUGO | S_IWUSR,
2124 bcm2048_rds_b_block_match_read,
2125 bcm2048_rds_b_block_match_write),
2126 __ATTR(rds_pi_mask, S_IRUGO | S_IWUSR, bcm2048_rds_pi_mask_read,
2127 bcm2048_rds_pi_mask_write),
2128 __ATTR(rds_pi_match, S_IRUGO | S_IWUSR, bcm2048_rds_pi_match_read,
2129 bcm2048_rds_pi_match_write),
2130 __ATTR(rds_wline, S_IRUGO | S_IWUSR, bcm2048_rds_wline_read,
2131 bcm2048_rds_wline_write),
2132 __ATTR(rds_pi, S_IRUGO, bcm2048_rds_pi_read, NULL),
2133 __ATTR(rds_rt, S_IRUGO, bcm2048_rds_rt_read, NULL),
2134 __ATTR(rds_ps, S_IRUGO, bcm2048_rds_ps_read, NULL),
2135 __ATTR(fm_rds_flags, S_IRUGO, bcm2048_fm_rds_flags_read, NULL),
2136 __ATTR(region_bottom_frequency, S_IRUGO,
2137 bcm2048_region_bottom_frequency_read, NULL),
2138 __ATTR(region_top_frequency, S_IRUGO,
2139 bcm2048_region_top_frequency_read, NULL),
2140 __ATTR(fm_carrier_error, S_IRUGO,
2141 bcm2048_fm_carrier_error_read, NULL),
2142 __ATTR(fm_rssi, S_IRUGO,
2143 bcm2048_fm_rssi_read, NULL),
2144 __ATTR(region, S_IRUGO | S_IWUSR, bcm2048_region_read,
2145 bcm2048_region_write),
2146 __ATTR(rds_data, S_IRUGO, bcm2048_rds_data_read, NULL),
2147 };
2148
bcm2048_sysfs_unregister_properties(struct bcm2048_device * bdev,int size)2149 static int bcm2048_sysfs_unregister_properties(struct bcm2048_device *bdev,
2150 int size)
2151 {
2152 int i;
2153
2154 for (i = 0; i < size; i++)
2155 device_remove_file(&bdev->client->dev, &attrs[i]);
2156
2157 return 0;
2158 }
2159
bcm2048_sysfs_register_properties(struct bcm2048_device * bdev)2160 static int bcm2048_sysfs_register_properties(struct bcm2048_device *bdev)
2161 {
2162 int err = 0;
2163 int i;
2164
2165 for (i = 0; i < ARRAY_SIZE(attrs); i++) {
2166 if (device_create_file(&bdev->client->dev, &attrs[i]) != 0) {
2167 dev_err(&bdev->client->dev,
2168 "could not register sysfs entry\n");
2169 err = -EBUSY;
2170 bcm2048_sysfs_unregister_properties(bdev, i);
2171 break;
2172 }
2173 }
2174
2175 return err;
2176 }
2177
2178
bcm2048_fops_open(struct file * file)2179 static int bcm2048_fops_open(struct file *file)
2180 {
2181 struct bcm2048_device *bdev = video_drvdata(file);
2182
2183 bdev->users++;
2184 bdev->rd_index = 0;
2185 bdev->rds_data_available = 0;
2186
2187 return 0;
2188 }
2189
bcm2048_fops_release(struct file * file)2190 static int bcm2048_fops_release(struct file *file)
2191 {
2192 struct bcm2048_device *bdev = video_drvdata(file);
2193
2194 bdev->users--;
2195
2196 return 0;
2197 }
2198
bcm2048_fops_poll(struct file * file,struct poll_table_struct * pts)2199 static unsigned int bcm2048_fops_poll(struct file *file,
2200 struct poll_table_struct *pts)
2201 {
2202 struct bcm2048_device *bdev = video_drvdata(file);
2203 int retval = 0;
2204
2205 poll_wait(file, &bdev->read_queue, pts);
2206
2207 if (bdev->rds_data_available)
2208 retval = POLLIN | POLLRDNORM;
2209
2210 return retval;
2211 }
2212
bcm2048_fops_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)2213 static ssize_t bcm2048_fops_read(struct file *file, char __user *buf,
2214 size_t count, loff_t *ppos)
2215 {
2216 struct bcm2048_device *bdev = video_drvdata(file);
2217 int i;
2218 int retval = 0;
2219
2220 /* we return at least 3 bytes, one block */
2221 count = (count / 3) * 3; /* only multiples of 3 */
2222 if (count < 3)
2223 return -ENOBUFS;
2224
2225 while (!bdev->rds_data_available) {
2226 if (file->f_flags & O_NONBLOCK) {
2227 retval = -EWOULDBLOCK;
2228 goto done;
2229 }
2230 /* interruptible_sleep_on(&bdev->read_queue); */
2231 if (wait_event_interruptible(bdev->read_queue,
2232 bdev->rds_data_available) < 0) {
2233 retval = -EINTR;
2234 goto done;
2235 }
2236 }
2237
2238 mutex_lock(&bdev->mutex);
2239 /* copy data to userspace */
2240 i = bdev->fifo_size - bdev->rd_index;
2241 if (count > i)
2242 count = (i / 3) * 3;
2243
2244 i = 0;
2245 while (i < count) {
2246 unsigned char tmpbuf[3];
2247
2248 tmpbuf[i] = bdev->rds_info.radio_text[bdev->rd_index+i+2];
2249 tmpbuf[i+1] = bdev->rds_info.radio_text[bdev->rd_index+i+1];
2250 tmpbuf[i+2] = (bdev->rds_info.radio_text[bdev->rd_index + i] & 0xf0) >> 4;
2251 if ((bdev->rds_info.radio_text[bdev->rd_index+i] &
2252 BCM2048_RDS_CRC_MASK) == BCM2048_RDS_CRC_UNRECOVARABLE)
2253 tmpbuf[i+2] |= 0x80;
2254 if (copy_to_user(buf+i, tmpbuf, 3)) {
2255 retval = -EFAULT;
2256 break;
2257 }
2258 i += 3;
2259 }
2260
2261 bdev->rd_index += i;
2262 if (bdev->rd_index >= bdev->fifo_size)
2263 bdev->rds_data_available = 0;
2264
2265 mutex_unlock(&bdev->mutex);
2266 if (retval == 0)
2267 retval = i;
2268
2269 done:
2270 return retval;
2271 }
2272
2273 /*
2274 * bcm2048_fops - file operations interface
2275 */
2276 static const struct v4l2_file_operations bcm2048_fops = {
2277 .owner = THIS_MODULE,
2278 .unlocked_ioctl = video_ioctl2,
2279 /* for RDS read support */
2280 .open = bcm2048_fops_open,
2281 .release = bcm2048_fops_release,
2282 .read = bcm2048_fops_read,
2283 .poll = bcm2048_fops_poll
2284 };
2285
2286 /*
2287 * Video4Linux Interface
2288 */
2289 static struct v4l2_queryctrl bcm2048_v4l2_queryctrl[] = {
2290 {
2291 .id = V4L2_CID_AUDIO_VOLUME,
2292 .flags = V4L2_CTRL_FLAG_DISABLED,
2293 },
2294 {
2295 .id = V4L2_CID_AUDIO_BALANCE,
2296 .flags = V4L2_CTRL_FLAG_DISABLED,
2297 },
2298 {
2299 .id = V4L2_CID_AUDIO_BASS,
2300 .flags = V4L2_CTRL_FLAG_DISABLED,
2301 },
2302 {
2303 .id = V4L2_CID_AUDIO_TREBLE,
2304 .flags = V4L2_CTRL_FLAG_DISABLED,
2305 },
2306 {
2307 .id = V4L2_CID_AUDIO_MUTE,
2308 .type = V4L2_CTRL_TYPE_BOOLEAN,
2309 .name = "Mute",
2310 .minimum = 0,
2311 .maximum = 1,
2312 .step = 1,
2313 .default_value = 1,
2314 },
2315 {
2316 .id = V4L2_CID_AUDIO_LOUDNESS,
2317 .flags = V4L2_CTRL_FLAG_DISABLED,
2318 },
2319 };
2320
bcm2048_vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * capability)2321 static int bcm2048_vidioc_querycap(struct file *file, void *priv,
2322 struct v4l2_capability *capability)
2323 {
2324 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2325
2326 strlcpy(capability->driver, BCM2048_DRIVER_NAME,
2327 sizeof(capability->driver));
2328 strlcpy(capability->card, BCM2048_DRIVER_CARD,
2329 sizeof(capability->card));
2330 snprintf(capability->bus_info, 32, "I2C: 0x%X", bdev->client->addr);
2331 capability->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
2332 V4L2_CAP_HW_FREQ_SEEK;
2333 capability->capabilities = capability->device_caps |
2334 V4L2_CAP_DEVICE_CAPS;
2335
2336 return 0;
2337 }
2338
bcm2048_vidioc_g_input(struct file * filp,void * priv,unsigned int * i)2339 static int bcm2048_vidioc_g_input(struct file *filp, void *priv,
2340 unsigned int *i)
2341 {
2342 *i = 0;
2343
2344 return 0;
2345 }
2346
bcm2048_vidioc_s_input(struct file * filp,void * priv,unsigned int i)2347 static int bcm2048_vidioc_s_input(struct file *filp, void *priv,
2348 unsigned int i)
2349 {
2350 if (i)
2351 return -EINVAL;
2352
2353 return 0;
2354 }
2355
bcm2048_vidioc_queryctrl(struct file * file,void * priv,struct v4l2_queryctrl * qc)2356 static int bcm2048_vidioc_queryctrl(struct file *file, void *priv,
2357 struct v4l2_queryctrl *qc)
2358 {
2359 int i;
2360
2361 for (i = 0; i < ARRAY_SIZE(bcm2048_v4l2_queryctrl); i++) {
2362 if (qc->id && qc->id == bcm2048_v4l2_queryctrl[i].id) {
2363 *qc = bcm2048_v4l2_queryctrl[i];
2364 return 0;
2365 }
2366 }
2367
2368 return -EINVAL;
2369 }
2370
bcm2048_vidioc_g_ctrl(struct file * file,void * priv,struct v4l2_control * ctrl)2371 static int bcm2048_vidioc_g_ctrl(struct file *file, void *priv,
2372 struct v4l2_control *ctrl)
2373 {
2374 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2375 int err = 0;
2376
2377 if (!bdev)
2378 return -ENODEV;
2379
2380 switch (ctrl->id) {
2381 case V4L2_CID_AUDIO_MUTE:
2382 err = bcm2048_get_mute(bdev);
2383 if (err >= 0)
2384 ctrl->value = err;
2385 break;
2386 }
2387
2388 return err;
2389 }
2390
bcm2048_vidioc_s_ctrl(struct file * file,void * priv,struct v4l2_control * ctrl)2391 static int bcm2048_vidioc_s_ctrl(struct file *file, void *priv,
2392 struct v4l2_control *ctrl)
2393 {
2394 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2395 int err = 0;
2396
2397 if (!bdev)
2398 return -ENODEV;
2399
2400 switch (ctrl->id) {
2401 case V4L2_CID_AUDIO_MUTE:
2402 if (ctrl->value) {
2403 if (bdev->power_state) {
2404 err = bcm2048_set_mute(bdev, ctrl->value);
2405 err |= bcm2048_deinit(bdev);
2406 }
2407 } else {
2408 if (!bdev->power_state) {
2409 err = bcm2048_init(bdev);
2410 err |= bcm2048_set_mute(bdev, ctrl->value);
2411 }
2412 }
2413 break;
2414 }
2415
2416 return err;
2417 }
2418
bcm2048_vidioc_g_audio(struct file * file,void * priv,struct v4l2_audio * audio)2419 static int bcm2048_vidioc_g_audio(struct file *file, void *priv,
2420 struct v4l2_audio *audio)
2421 {
2422 if (audio->index > 1)
2423 return -EINVAL;
2424
2425 strncpy(audio->name, "Radio", 32);
2426 audio->capability = V4L2_AUDCAP_STEREO;
2427
2428 return 0;
2429 }
2430
bcm2048_vidioc_s_audio(struct file * file,void * priv,const struct v4l2_audio * audio)2431 static int bcm2048_vidioc_s_audio(struct file *file, void *priv,
2432 const struct v4l2_audio *audio)
2433 {
2434 if (audio->index != 0)
2435 return -EINVAL;
2436
2437 return 0;
2438 }
2439
bcm2048_vidioc_g_tuner(struct file * file,void * priv,struct v4l2_tuner * tuner)2440 static int bcm2048_vidioc_g_tuner(struct file *file, void *priv,
2441 struct v4l2_tuner *tuner)
2442 {
2443 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2444 s8 f_error;
2445 s8 rssi;
2446
2447 if (!bdev)
2448 return -ENODEV;
2449
2450 if (tuner->index > 0)
2451 return -EINVAL;
2452
2453 strncpy(tuner->name, "FM Receiver", 32);
2454 tuner->type = V4L2_TUNER_RADIO;
2455 tuner->rangelow =
2456 dev_to_v4l2(bcm2048_get_region_bottom_frequency(bdev));
2457 tuner->rangehigh =
2458 dev_to_v4l2(bcm2048_get_region_top_frequency(bdev));
2459 tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
2460 tuner->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
2461 tuner->audmode = V4L2_TUNER_MODE_STEREO;
2462 tuner->afc = 0;
2463 if (bdev->power_state) {
2464 /*
2465 * Report frequencies with high carrier errors to have zero
2466 * signal level
2467 */
2468 f_error = bcm2048_get_fm_carrier_error(bdev);
2469 if (f_error < BCM2048_FREQ_ERROR_FLOOR ||
2470 f_error > BCM2048_FREQ_ERROR_ROOF) {
2471 tuner->signal = 0;
2472 } else {
2473 /*
2474 * RSSI level -60 dB is defined to report full
2475 * signal strength
2476 */
2477 rssi = bcm2048_get_fm_rssi(bdev);
2478 if (rssi >= BCM2048_RSSI_LEVEL_BASE) {
2479 tuner->signal = 0xFFFF;
2480 } else if (rssi > BCM2048_RSSI_LEVEL_ROOF) {
2481 tuner->signal = (rssi +
2482 BCM2048_RSSI_LEVEL_ROOF_NEG)
2483 * BCM2048_SIGNAL_MULTIPLIER;
2484 } else {
2485 tuner->signal = 0;
2486 }
2487 }
2488 } else {
2489 tuner->signal = 0;
2490 }
2491
2492 return 0;
2493 }
2494
bcm2048_vidioc_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * tuner)2495 static int bcm2048_vidioc_s_tuner(struct file *file, void *priv,
2496 const struct v4l2_tuner *tuner)
2497 {
2498 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2499
2500 if (!bdev)
2501 return -ENODEV;
2502
2503 if (tuner->index > 0)
2504 return -EINVAL;
2505
2506 return 0;
2507 }
2508
bcm2048_vidioc_g_frequency(struct file * file,void * priv,struct v4l2_frequency * freq)2509 static int bcm2048_vidioc_g_frequency(struct file *file, void *priv,
2510 struct v4l2_frequency *freq)
2511 {
2512 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2513 int err = 0;
2514 int f;
2515
2516 if (!bdev->power_state)
2517 return -ENODEV;
2518
2519 freq->type = V4L2_TUNER_RADIO;
2520 f = bcm2048_get_fm_frequency(bdev);
2521
2522 if (f < 0)
2523 err = f;
2524 else
2525 freq->frequency = dev_to_v4l2(f);
2526
2527 return err;
2528 }
2529
bcm2048_vidioc_s_frequency(struct file * file,void * priv,const struct v4l2_frequency * freq)2530 static int bcm2048_vidioc_s_frequency(struct file *file, void *priv,
2531 const struct v4l2_frequency *freq)
2532 {
2533 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2534 int err;
2535
2536 if (freq->type != V4L2_TUNER_RADIO)
2537 return -EINVAL;
2538
2539 if (!bdev->power_state)
2540 return -ENODEV;
2541
2542 err = bcm2048_set_fm_frequency(bdev, v4l2_to_dev(freq->frequency));
2543 err |= bcm2048_set_fm_search_tune_mode(bdev, BCM2048_FM_PRE_SET_MODE);
2544
2545 return err;
2546 }
2547
bcm2048_vidioc_s_hw_freq_seek(struct file * file,void * priv,const struct v4l2_hw_freq_seek * seek)2548 static int bcm2048_vidioc_s_hw_freq_seek(struct file *file, void *priv,
2549 const struct v4l2_hw_freq_seek *seek)
2550 {
2551 struct bcm2048_device *bdev = video_get_drvdata(video_devdata(file));
2552 int err;
2553
2554 if (!bdev->power_state)
2555 return -ENODEV;
2556
2557 if ((seek->tuner != 0) || (seek->type != V4L2_TUNER_RADIO))
2558 return -EINVAL;
2559
2560 err = bcm2048_set_fm_search_mode_direction(bdev, seek->seek_upward);
2561 err |= bcm2048_set_fm_search_tune_mode(bdev,
2562 BCM2048_FM_AUTO_SEARCH_MODE);
2563
2564 return err;
2565 }
2566
2567 static struct v4l2_ioctl_ops bcm2048_ioctl_ops = {
2568 .vidioc_querycap = bcm2048_vidioc_querycap,
2569 .vidioc_g_input = bcm2048_vidioc_g_input,
2570 .vidioc_s_input = bcm2048_vidioc_s_input,
2571 .vidioc_queryctrl = bcm2048_vidioc_queryctrl,
2572 .vidioc_g_ctrl = bcm2048_vidioc_g_ctrl,
2573 .vidioc_s_ctrl = bcm2048_vidioc_s_ctrl,
2574 .vidioc_g_audio = bcm2048_vidioc_g_audio,
2575 .vidioc_s_audio = bcm2048_vidioc_s_audio,
2576 .vidioc_g_tuner = bcm2048_vidioc_g_tuner,
2577 .vidioc_s_tuner = bcm2048_vidioc_s_tuner,
2578 .vidioc_g_frequency = bcm2048_vidioc_g_frequency,
2579 .vidioc_s_frequency = bcm2048_vidioc_s_frequency,
2580 .vidioc_s_hw_freq_seek = bcm2048_vidioc_s_hw_freq_seek,
2581 };
2582
2583 /*
2584 * bcm2048_viddev_template - video device interface
2585 */
2586 static struct video_device bcm2048_viddev_template = {
2587 .fops = &bcm2048_fops,
2588 .name = BCM2048_DRIVER_NAME,
2589 .release = video_device_release_empty,
2590 .ioctl_ops = &bcm2048_ioctl_ops,
2591 };
2592
2593 /*
2594 * I2C driver interface
2595 */
bcm2048_i2c_driver_probe(struct i2c_client * client,const struct i2c_device_id * id)2596 static int bcm2048_i2c_driver_probe(struct i2c_client *client,
2597 const struct i2c_device_id *id)
2598 {
2599 struct bcm2048_device *bdev;
2600 int err;
2601
2602 bdev = kzalloc(sizeof(*bdev), GFP_KERNEL);
2603 if (!bdev) {
2604 err = -ENOMEM;
2605 goto exit;
2606 }
2607
2608 bdev->client = client;
2609 i2c_set_clientdata(client, bdev);
2610 mutex_init(&bdev->mutex);
2611 init_completion(&bdev->compl);
2612 INIT_WORK(&bdev->work, bcm2048_work);
2613
2614 if (client->irq) {
2615 err = request_irq(client->irq,
2616 bcm2048_handler, IRQF_TRIGGER_FALLING,
2617 client->name, bdev);
2618 if (err < 0) {
2619 dev_err(&client->dev, "Could not request IRQ\n");
2620 goto free_bdev;
2621 }
2622 dev_dbg(&client->dev, "IRQ requested.\n");
2623 } else {
2624 dev_dbg(&client->dev, "IRQ not configured. Using timeouts.\n");
2625 }
2626
2627 bdev->videodev = bcm2048_viddev_template;
2628 video_set_drvdata(&bdev->videodev, bdev);
2629 if (video_register_device(&bdev->videodev, VFL_TYPE_RADIO, radio_nr)) {
2630 dev_dbg(&client->dev, "Could not register video device.\n");
2631 err = -EIO;
2632 goto free_irq;
2633 }
2634
2635 err = bcm2048_sysfs_register_properties(bdev);
2636 if (err < 0) {
2637 dev_dbg(&client->dev, "Could not register sysfs interface.\n");
2638 goto free_registration;
2639 }
2640
2641 err = bcm2048_probe(bdev);
2642 if (err < 0) {
2643 dev_dbg(&client->dev, "Failed to probe device information.\n");
2644 goto free_sysfs;
2645 }
2646
2647 return 0;
2648
2649 free_sysfs:
2650 bcm2048_sysfs_unregister_properties(bdev, ARRAY_SIZE(attrs));
2651 free_registration:
2652 video_unregister_device(&bdev->videodev);
2653 free_irq:
2654 if (client->irq)
2655 free_irq(client->irq, bdev);
2656 free_bdev:
2657 i2c_set_clientdata(client, NULL);
2658 kfree(bdev);
2659 exit:
2660 return err;
2661 }
2662
bcm2048_i2c_driver_remove(struct i2c_client * client)2663 static int __exit bcm2048_i2c_driver_remove(struct i2c_client *client)
2664 {
2665 struct bcm2048_device *bdev = i2c_get_clientdata(client);
2666
2667 if (!client->adapter)
2668 return -ENODEV;
2669
2670 if (bdev) {
2671 bcm2048_sysfs_unregister_properties(bdev, ARRAY_SIZE(attrs));
2672 video_unregister_device(&bdev->videodev);
2673
2674 if (bdev->power_state)
2675 bcm2048_set_power_state(bdev, BCM2048_POWER_OFF);
2676
2677 if (client->irq > 0)
2678 free_irq(client->irq, bdev);
2679
2680 cancel_work_sync(&bdev->work);
2681
2682 kfree(bdev);
2683 }
2684
2685 return 0;
2686 }
2687
2688 /*
2689 * bcm2048_i2c_driver - i2c driver interface
2690 */
2691 static const struct i2c_device_id bcm2048_id[] = {
2692 { "bcm2048", 0 },
2693 { },
2694 };
2695 MODULE_DEVICE_TABLE(i2c, bcm2048_id);
2696
2697 static struct i2c_driver bcm2048_i2c_driver = {
2698 .driver = {
2699 .name = BCM2048_DRIVER_NAME,
2700 },
2701 .probe = bcm2048_i2c_driver_probe,
2702 .remove = __exit_p(bcm2048_i2c_driver_remove),
2703 .id_table = bcm2048_id,
2704 };
2705
2706 module_i2c_driver(bcm2048_i2c_driver);
2707
2708 MODULE_LICENSE("GPL");
2709 MODULE_AUTHOR(BCM2048_DRIVER_AUTHOR);
2710 MODULE_DESCRIPTION(BCM2048_DRIVER_DESC);
2711 MODULE_VERSION("0.0.2");
2712