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