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