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