1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Tuner "middleware" for Silicon Labs SI4700 chip
12 * Copyright (C) 2008 Nils Wallménius
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
29 #include "tuner.h" /* tuner abstraction interface */
31 #include "fmradio_i2c.h" /* physical interface driver */
33 /* some models use the internal 32 kHz oscillator which needs special attention
34 during initialisation, power-up and power-down.
36 #if defined(SANSA_CLIP) || defined(SANSA_E200V2) || defined(SANSA_FUZE) || defined(SANSA_C200V2)
37 #define USE_INTERNAL_OSCILLATOR
38 #elif defined(TOSHIBA_GIGABEAT_S)
39 #define SI4700_GPIO_SETUP (SYSCONFIG1_GPIO1_HI_Z | \
40 SYSCONFIG1_GPIO2_HI_Z | \
41 SYSCONFIG1_GPIO3_MO_ST_I)
42 extern int si4700_st(void);
45 #ifndef SI4700_GPIO_SETUP
46 #define SI4700_GPIO_SETUP 0
49 #define SEEK_THRESHOLD 0x16
53 /** Registers and bits - "x" denotes Si4702/03 only (so they say) **/
58 #define SYSCONFIG1 0x4
59 #define SYSCONFIG2 0x5
60 #define SYSCONFIG3 0x6
63 #define BOOTCONFIG 0x9
64 #define STATUSRSSI 0xA
66 #define RDSA 0xC /* x */
67 #define RDSB 0xD /* x */
68 #define RDSC 0xE /* x */
69 #define RDSD 0xF /* x */
72 #define DEVICEID_PN (0xf << 12)
73 /* 0x01 = Si4700/01 */
74 /* 0x01 = Si4702/03 */
75 #define DEVICEID_MFGID (0xfff << 0)
80 #if 0 /* Informational */
82 #define CHIPID_REV (0x3f << 10)
83 #define CHIPID_DEV (0x1 << 9)
84 /* 0 before powerup */
85 /* 0 after powerup = Si4700 */
86 /* 1 after powerup = Si4701 */
87 #define CHIPID_FIRMWARE (0xff << 0)
90 #define CHIPID_REV (0x3f << 10)
91 #define CHIPID_DEV (0xf << 6)
92 /* 0000 before PU = Si4702 */
93 /* 0001 after PU = Si4702 */
94 /* 1000 before PU = Si4703 */
95 /* 1001 after PU = Si4703 */
96 #define CHIPID_FIRMWARE (0x3f << 0)
100 #define POWERCFG_DSMUTE (0x1 << 15)
101 #define POWERCFG_DMUTE (0x1 << 14)
102 #define POWERCFG_MONO (0x1 << 13)
103 #define POWERCFG_RDSM (0x1 << 11) /* x */
104 #define POWERCFG_SKMODE (0x1 << 10)
105 #define POWERCFG_SEEKUP (0x1 << 9)
106 #define POWERCFG_SEEK (0x1 << 8)
107 #define POWERCFG_DISABLE (0x1 << 6)
108 #define POWERCFG_ENABLE (0x1 << 0)
111 #define CHANNEL_TUNE (0x1 << 15)
112 #define CHANNEL_CHAN (0x3ff << 0)
113 #define CHANNEL_CHANw(x) ((x) & CHANNEL_CHAN)
115 /* SYSCONFIG1 (0x4) */
116 #define SYSCONFIG1_RDSIEN (0x1 << 15) /* x */
117 #define SYSCONFIG1_STCIEN (0x1 << 14)
118 #define SYSCONFIG1_RDS (0x1 << 12) /* x */
119 #define SYSCONFIG1_DE (0x1 << 11)
120 #define SYSCONFIG1_AGCD (0x1 << 10)
121 #define SYSCONFIG1_BLNDADJ (0x3 << 6)
122 #define SYSCONFIG1_BLNDADJ_31_39_RSSI (0x0 << 6)
123 #define SYSCONFIG1_BLNDADJ_37_55_RSSI (0x1 << 6)
124 #define SYSCONFIG1_BLNDADJ_19_37_RSSI (0x2 << 6)
125 #define SYSCONFIG1_BLNDADJ_25_43_RSSI (0x3 << 6)
126 #define SYSCONFIG1_GPIO3 (0x3 << 4)
127 #define SYSCONFIG1_GPIO3_HI_Z (0x0 << 4)
128 #define SYSCONFIG1_GPIO3_MO_ST_I (0x1 << 4)
129 #define SYSCONFIG1_GPIO3_LOW (0x2 << 4)
130 #define SYSCONFIG1_GPIO3_HI (0x3 << 4)
131 #define SYSCONFIG1_GPIO2 (0x3 << 2)
132 #define SYSCONFIG1_GPIO2_HI_Z (0x0 << 2)
133 #define SYSCONFIG1_GPIO2_STC_RDS_I (0x1 << 2)
134 #define SYSCONFIG1_GPIO2_LOW (0x2 << 2)
135 #define SYSCONFIG1_GPIO2_HI (0x3 << 2)
136 #define SYSCONFIG1_GPIO1 (0x3 << 0)
137 #define SYSCONFIG1_GPIO1_HI_Z (0x0 << 0)
138 #define SYSCONFIG1_GPIO1_LOW (0x2 << 0)
139 #define SYSCONFIG1_GPIO1_HI (0x3 << 0)
141 /* SYSCONFIG2 (0x5) */
142 #define SYSCONFIG2_SEEKTH (0xff << 8)
143 #define SYSCONFIG2_SKEETHw(x) (((x) << 8) & SYSCONFIG2_SEEKTH)
144 #define SYSCONFIG2_BAND (0x3 << 6)
145 #define SYSCONFIG2_BANDw(x) (((x) << 6) & SYSCONFIG2_BAND)
146 #define SYSCONFIG2_BANDr(x) (((x) & SYSCONFIG2_BAND) >> 6)
147 #define SYSCONFIG2_BAND_875_1080 (0x0 << 6) /* tenth-megahertz */
148 #define SYSCONFIG2_BAND_760_1080 (0x1 << 6)
149 #define SYSCONFIG2_BAND_760_900 (0x2 << 6)
150 #define SYSCONFIG2_SPACE (0x3 << 4)
151 #define SYSCONFIG2_SPACEw(x) (((x) << 4) & SYSCONFIG2_SPACE)
152 #define SYSCONFIG2_SPACEr(x) (((x) & SYSCONFIG2_SPACE) >> 4)
153 #define SYSCONFIG2_SPACE_200KHZ (0x0 << 4)
154 #define SYSCONFIG2_SPACE_100KHZ (0x1 << 4)
155 #define SYSCONFIG2_SPACE_50KHZ (0x2 << 4)
156 /* 4700/01 0000=mute,0001=-28dBFS..2dB steps..1111= +0dBFS */
157 /* 4702/03: VOLEXT=0: 0000=mute,0001=-28dBFS..2dB steps..1111= +0dBFS */
158 /* VOLEXT=1: 0000=mute,0001=-58dBFS..2dB steps..1111=-30dBFS */
159 #define SYSCONFIG2_VOLUME (0xf << 0)
160 #define SYSCONFIG2_VOLUMEw(x) ((x) & SYSCONFIG2_VOLUME)
162 /* SYSCONFIG3 (0x6) */
163 #define SYSCONFIG3_SMUTER (0x3 << 14)
164 #define SYSCONFIG3_SMUTER_FASTEST (0x0 << 14)
165 #define SYSCONFIG3_SMUTER_FAST (0x1 << 14)
166 #define SYSCONFIG3_SMUTER_SLOW (0x2 << 14)
167 #define SYSCONFIG3_SMUTER_SLOWEST (0x3 << 14)
168 #define SYSCONFIG3_SMUTEA (0x3 << 12)
169 #define SYSCONFIG3_SMUTEA_16DB (0x0 << 12)
170 #define SYSCONFIG3_SMUTEA_14DB (0x1 << 12)
171 #define SYSCONFIG3_SMUTEA_12DB (0x2 << 12)
172 #define SYSCONFIG3_SMUTEA_10DB (0x3 << 12)
173 #define SYSCONFIG3_VOLEXT (0x1 << 8) /* x */
174 #define SYSCONFIG3_SKSNR (0xf << 4)
175 #define SYSCONFIG3_SKSNRw(x) (((x) << 4) & SYSCONFIG3_SKSNR)
176 #define SYSCONFIG3_SKCNT (0xf << 0)
177 #define SYSCONFIG3_SKCNTw(x) (((x) << 0) & SYSCONFIG3_SKCNT)
180 /* 4700/01: 15=always 0, 13:0 = write with preexisting values! */
181 /* 4702/03: 13:0 = write with preexisting values! */
182 #define TEST1_XOSCEN (0x1 << 15) /* x */
183 #define TEST1_AHIZEN (0x1 << 14)
186 /* 15:0 = write with preexisting values! */
188 /* BOOTCONFIG (0x9) */
189 /* 15:0 = write with preexisting values! */
191 /* STATUSRSSI (0xA) */
192 #define STATUSRSSI_RDSR (0x1 << 15) /* x */
193 #define STATUSRSSI_STC (0x1 << 14)
194 #define STATUSRSSI_SFBL (0x1 << 13)
195 #define STATUSRSSI_AFCRL (0x1 << 12)
196 #define STATUSRSSI_RDSS (0x1 << 11) /* x */
197 #define STATUSRSSI_BLERA (0x3 << 9) /* x */
198 #define STATUSRSSI_ST (0x1 << 8)
199 #define STATUSRSSI_RSSI (0xff << 0)
200 #define STATUSRSSI_RSSIr(x) ((x) & 0xff)
203 #define READCHAN_BLERB (0x3 << 14) /* x */
204 #define READCHAN_BLERC (0x3 << 12) /* x */
205 #define READCHAN_BLERD (0x3 << 10) /* x */
206 #define READCHAN_READCHAN (0x3ff << 0)
208 /* RDSA-D (0xC-0xF) */
209 /* 4702/03: RDS Block A-D data */
211 static bool tuner_present
= false;
212 static int curr_frequency
= 87500000; /* Current station frequency (HZ) */
213 static uint16_t cache
[16];
215 /* reads <len> registers from radio at offset 0x0A into cache */
216 static void si4700_read(int len
)
219 unsigned char buf
[32];
220 unsigned char *ptr
= buf
;
223 fmradio_i2c_read(I2C_ADR
, buf
, len
* 2);
224 for (i
= 0; i
< len
; i
++) {
225 data
= ptr
[0] << 8 | ptr
[1];
226 cache
[(i
+ STATUSRSSI
) & 0xF] = data
;
231 /* writes <len> registers from cache to radio at offset 0x02 */
232 static void si4700_write(int len
)
235 unsigned char buf
[32];
236 unsigned char *ptr
= buf
;
239 for (i
= 0; i
< len
; i
++) {
240 data
= cache
[(i
+ POWERCFG
) & 0xF];
241 *ptr
++ = (data
>> 8) & 0xFF;
242 *ptr
++ = data
& 0xFF;
244 fmradio_i2c_write(I2C_ADR
, buf
, len
* 2);
247 /* Hide silly, wrapped and continuous register reading and make interface
248 * appear sane and normal. This also makes the driver compatible with
249 * using the 3-wire interface. */
250 static uint16_t si4700_read_reg(int reg
)
252 si4700_read(((reg
- STATUSRSSI
) & 0xF) + 1);
256 static void si4700_write_reg(int reg
, uint16_t value
)
259 si4700_write(((reg
- POWERCFG
) & 0xF) + 1);
262 static void si4700_write_masked(int reg
, uint16_t bits
, uint16_t mask
)
264 si4700_write_reg(reg
, (cache
[reg
] & ~mask
) | (bits
& mask
));
267 static void si4700_write_set(int reg
, uint16_t mask
)
269 si4700_write_reg(reg
, cache
[reg
] | mask
);
272 static void si4700_write_clear(int reg
, uint16_t mask
)
274 si4700_write_reg(reg
, cache
[reg
] & ~mask
);
277 #if (SI4700_GPIO_SETUP & SYSCONFIG1_GPIO3) != SYSCONFIG1_GPIO3_MO_ST_I
278 /* Poll i2c for the stereo status */
279 static inline int si4700_st(void)
281 return (si4700_read_reg(STATUSRSSI
) & STATUSRSSI_ST
) >> 8;
285 static void si4700_sleep(int snooze
)
290 /* ENABLE high, DISABLE high */
291 si4700_write_set(POWERCFG
,
292 POWERCFG_DISABLE
| POWERCFG_ENABLE
);
293 /* Bits self-clear once placed in powerdown. */
294 cache
[POWERCFG
] &= ~(POWERCFG_DISABLE
| POWERCFG_ENABLE
);
299 /* ENABLE high, DISABLE low */
300 si4700_write_masked(POWERCFG
, POWERCFG_ENABLE
,
301 POWERCFG_DISABLE
| POWERCFG_ENABLE
);
302 sleep(110 * HZ
/ 1000);
304 /* init register cache */
307 #if SI4700_GPIO_SETUP != 0
308 si4700_write_masked(SYSCONFIG1
, SI4700_GPIO_SETUP
,
309 SYSCONFIG1_GPIO1
| SYSCONFIG1_GPIO2
|
312 si4700_write_masked(SYSCONFIG2
,
313 SYSCONFIG2_SKEETHw(SEEK_THRESHOLD
) |
314 SYSCONFIG2_VOLUMEw(0xF),
315 SYSCONFIG2_VOLUME
| SYSCONFIG2_SEEKTH
);
319 bool si4700_detect(void)
324 detected
= (si4700_read_reg(DEVICEID
) == 0x1242);
330 void si4700_init(void)
332 /* check device id */
333 if (si4700_detect()) {
334 tuner_present
= true;
338 /* read all registers */
342 #ifdef USE_INTERNAL_OSCILLATOR
343 /* Enable the internal oscillator
344 (Si4702-16 needs this register to be initialised to 0x100) */
345 si4700_write_set(TEST1
, TEST1_XOSCEN
| 0x100);
354 static void si4700_set_frequency(int freq
)
356 static const unsigned int spacings
[3] =
358 200000, /* SYSCONFIG2_SPACE_200KHZ */
359 100000, /* SYSCONFIG2_SPACE_100KHZ */
360 50000, /* SYSCONFIG2_SPACE_50KHZ */
362 static const unsigned int bands
[3] =
364 87500000, /* SYSCONFIG2_BAND_875_1080 */
365 76000000, /* SYSCONFIG2_BAND_760_1080 */
366 76000000, /* SYSCONFIG2_BAND_760_900 */
369 /* check BAND and spacings */
370 int space
= SYSCONFIG2_SPACEr(cache
[SYSCONFIG2
]);
371 int band
= SYSCONFIG2_BANDr(cache
[SYSCONFIG2
]);
372 int chan
= (freq
- bands
[band
]) / spacings
[space
];
375 curr_frequency
= freq
;
379 /* tuning should be done within 60 ms according to the datasheet */
380 si4700_write_reg(CHANNEL
, CHANNEL_CHANw(chan
) | CHANNEL_TUNE
);
381 sleep(HZ
* 60 / 1000);
383 /* get tune result */
384 readchan
= si4700_read_reg(READCHAN
) & READCHAN_READCHAN
;
386 si4700_write_clear(CHANNEL
, CHANNEL_TUNE
);
387 } while (!((cache
[STATUSRSSI
] & STATUSRSSI_STC
) && (readchan
== chan
)));
390 static int si4700_tuned(void)
392 /* Primitive tuning check: sufficient level and AFC not railed */
393 uint16_t status
= si4700_read_reg(STATUSRSSI
);
394 if (STATUSRSSI_RSSIr(status
) >= SEEK_THRESHOLD
&&
395 (status
& STATUSRSSI_AFCRL
) == 0)
401 static void si4700_set_region(int region
)
403 const struct si4700_region_data
*rd
= &si4700_region_data
[region
];
404 uint16_t bandspacing
= SYSCONFIG2_BANDw(rd
->band
) |
405 SYSCONFIG2_SPACEw(rd
->spacing
);
406 uint16_t oldbs
= cache
[SYSCONFIG2
] & (SYSCONFIG2_BAND
| SYSCONFIG2_SPACE
);
408 si4700_write_masked(SYSCONFIG1
,
409 rd
->deemphasis
? SYSCONFIG1_DE
: 0,
411 si4700_write_masked(SYSCONFIG2
, bandspacing
,
412 SYSCONFIG2_BAND
| SYSCONFIG2_SPACE
);
414 /* Retune if this region change would change the channel number. */
415 if (oldbs
!= bandspacing
)
416 si4700_set_frequency(curr_frequency
);
419 /* tuner abstraction layer: set something to the tuner */
420 int si4700_set(int setting
, int value
)
427 /* else actually it's 'pause' */
430 case RADIO_FREQUENCY
:
431 si4700_set_frequency(value
);
434 case RADIO_SCAN_FREQUENCY
:
435 si4700_set_frequency(value
);
436 return si4700_tuned();
439 si4700_write_masked(POWERCFG
, value
? 0 : POWERCFG_DMUTE
,
444 si4700_set_region(value
);
447 case RADIO_FORCE_MONO
:
448 si4700_write_masked(POWERCFG
, value
? POWERCFG_MONO
: 0,
459 /* tuner abstraction layer: read something from the tuner */
460 int si4700_get(int setting
)
462 int val
= -1; /* default for unsupported query */
467 val
= tuner_present
? 1 : 0;
471 val
= si4700_tuned();
482 void si4700_dbg_info(struct si4700_dbg_info
*nfo
)
484 memset(nfo
->regs
, 0, sizeof (nfo
->regs
));
489 memcpy(nfo
->regs
, cache
, sizeof (nfo
->regs
));