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