RDA5802 tuner: fix small bug in rda5802_init (writing too much data)
[kugel-rb.git] / firmware / drivers / tuner / rda5802.c
blob43dffe23558ccdb3e4b26a99915160c1f7a105a6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Tuner "middleware" for RDA5802 chip present in some Sansa Clip+ players
12 * Copyright (C) 2010 Bertrik Sikken
13 * Copyright (C) 2008 Nils Wallménius (si4700 code that this was based on)
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
24 #include "config.h"
25 #include <stdbool.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include "kernel.h"
29 #include "tuner.h" /* tuner abstraction interface */
30 #include "fmradio.h"
31 #include "fmradio_i2c.h" /* physical interface driver */
33 #define SEEK_THRESHOLD 0x16
35 #define I2C_ADR 0x20
37 /** Registers and bits **/
38 #define POWERCFG 0x2
39 #define CHANNEL 0x3
40 #define SYSCONFIG1 0x4
41 #define SYSCONFIG2 0x5
42 #define SYSCONFIG3 0x6
44 #define READCHAN 0xA
45 #define STATUSRSSI 0xB
46 #define IDENT 0xC
49 /* POWERCFG (0x2) */
50 #define POWERCFG_DMUTE (0x1 << 14)
51 #define POWERCFG_MONO (0x1 << 13)
52 #define POWERCFG_SOFT_RESET (0x1 << 1)
53 #define POWERCFG_ENABLE (0x1 << 0)
55 /* CHANNEL (0x3) */
56 #define CHANNEL_CHAN (0x3ff << 6)
57 #define CHANNEL_CHANw(x) (((x) << 6) & CHANNEL_CHAN)
58 #define CHANNEL_TUNE (0x1 << 4)
59 #define CHANNEL_BAND (0x3 << 2)
60 #define CHANNEL_BANDw(x) (((x) << 2) & CHANNEL_BAND)
61 #define CHANNEL_BANDr(x) (((x) & CHANNEL_BAND) >> 2)
62 #define CHANNEL_BAND_870_1080 (0x0) /* tenth-megahertz */
63 #define CHANNEL_BAND_760_1080 (0x1)
64 #define CHANNEL_BAND_760_900 (0x2)
65 #define CHANNEL_BAND_650_760 (0x3)
66 #define CHANNEL_SPACE (0x3 << 0)
67 #define CHANNEL_SPACEw(x) (((x) << 0) & CHANNEL_SPACE)
68 #define CHANNEL_SPACEr(x) (((x) & CHANNEL_SPACE) >> 0)
69 #define CHANNEL_SPACE_100KHZ (0x0)
70 #define CHANNEL_SPACE_200KHZ (0x1)
71 #define CHANNEL_SPACE_50KHZ (0x2)
73 /* SYSCONFIG1 (0x4) */
74 #define SYSCONFIG1_DE (0x1 << 11)
75 #define SYSCONFIG1_SMUTE (0x1 << 9)
77 /* SYSCONFIG2 (0x5) */
78 #define SYSCONFIG2_VOLUME (0xF << 0)
80 /* READCHAN (0xA) */
81 #define READCHAN_READCHAN (0x3ff << 0)
82 #define READCHAN_READCHANr(x) (((x) & READCHAN_READCHAN) >> 0)
83 #define READCHAN_STC (0x1 << 14)
84 #define READCHAN_ST (0x1 << 10)
86 /* STATUSRSSI (0xB) */
87 #define STATUSRSSI_RSSI (0x7F << 9)
88 #define STATUSRSSI_RSSIr(x) (((x) & STATUSRSSI_RSSI) >> 9)
89 #define STATUSRSSI_FM_TRUE (0x1 << 8)
91 static const uint16_t initvals[16] = {
92 0x0000, 0x0000, 0xC401, 0x1B90,
93 0x0400, 0x866F, 0x8000, 0x4712,
94 0x5EC6, 0x0000, 0x406E, 0x2D80,
95 0x5803, 0x5804, 0x5804, 0x5804
98 static bool tuner_present = false;
99 static int curr_frequency = 87500000; /* Current station frequency (HZ) */
100 static uint16_t cache[16];
102 /* reads <len> registers from radio at offset 0x0A into cache */
103 static void rda5802_read(int len)
105 int i;
106 unsigned char buf[128];
107 unsigned char *ptr = buf;
108 uint16_t data;
110 fmradio_i2c_read(I2C_ADR, buf, len * 2);
111 for (i = 0; i < len; i++) {
112 data = ptr[0] << 8 | ptr[1];
113 cache[READCHAN + i] = data;
114 ptr += 2;
118 /* writes <len> registers from cache to radio at offset 0x02 */
119 static void rda5802_write(int len)
121 int i;
122 unsigned char buf[64];
123 unsigned char *ptr = buf;
124 uint16_t data;
126 for (i = 0; i < len; i++) {
127 data = cache[POWERCFG + i];
128 *ptr++ = (data >> 8) & 0xFF;
129 *ptr++ = data & 0xFF;
131 fmradio_i2c_write(I2C_ADR, buf, len * 2);
134 static uint16_t rda5802_read_reg(int reg)
136 rda5802_read((reg - READCHAN) + 1);
137 return cache[reg];
140 static void rda5802_write_reg(int reg, uint16_t value)
142 cache[reg] = value;
145 static void rda5802_write_cache(void)
147 rda5802_write(5);
150 static void rda5802_write_masked(int reg, uint16_t bits, uint16_t mask)
152 rda5802_write_reg(reg, (cache[reg] & ~mask) | (bits & mask));
155 static void rda5802_write_clear(int reg, uint16_t mask)
157 rda5802_write_reg(reg, cache[reg] & ~mask);
160 static void rda5802_write_set(int reg, uint16_t mask)
162 rda5802_write_reg(reg, cache[reg] | mask);
165 static void rda5802_sleep(int snooze)
167 if (snooze) {
168 rda5802_write_clear(POWERCFG, POWERCFG_ENABLE);
170 else {
171 rda5802_write_set(POWERCFG, POWERCFG_ENABLE);
173 rda5802_write_cache();
176 bool rda5802_detect(void)
178 return ((rda5802_read_reg(IDENT) & 0xFF00) == 0x5800);
181 void rda5802_init(void)
183 if (rda5802_detect()) {
184 tuner_present = true;
186 // soft-reset
187 rda5802_write_reg(POWERCFG, POWERCFG_SOFT_RESET);
188 rda5802_write(1);
189 sleep(HZ * 10 / 1000);
191 // write initialisation values
192 memcpy(cache, initvals, sizeof(cache));
193 rda5802_write(14);
194 sleep(HZ * 70 / 1000);
198 static void rda5802_set_frequency(int freq)
200 int i;
201 uint16_t readchan;
203 /* check BAND and spacings */
204 int start = CHANNEL_BANDr(cache[CHANNEL]) & 1 ? 76000000 : 87000000;
205 int chan = (freq - start) / 50000;
207 curr_frequency = freq;
209 for (i = 0; i < 5; i++) {
210 /* tune and wait a bit */
211 rda5802_write_masked(CHANNEL, CHANNEL_CHANw(chan) | CHANNEL_TUNE,
212 CHANNEL_CHAN | CHANNEL_TUNE);
213 rda5802_write_cache();
214 sleep(HZ * 70 / 1000);
215 rda5802_write_clear(CHANNEL, CHANNEL_TUNE);
216 rda5802_write_cache();
218 /* check if tuning was successful */
219 readchan = rda5802_read_reg(READCHAN);
220 if (readchan & READCHAN_STC) {
221 if (READCHAN_READCHANr(readchan) == chan) {
222 break;
228 static int rda5802_tuned(void)
230 /* Primitive tuning check: sufficient level and AFC not railed */
231 uint16_t status = rda5802_read_reg(STATUSRSSI);
232 if (STATUSRSSI_RSSIr(status) >= SEEK_THRESHOLD &&
233 (status & STATUSRSSI_FM_TRUE)) {
234 return 1;
237 return 0;
240 static void rda5802_set_region(int region)
242 const struct fm_region_data *rd = &fm_region_data[region];
243 int band = (rd->freq_min == 76000000) ?
244 CHANNEL_BAND_760_900 : CHANNEL_BAND_870_1080;
245 int deemphasis = (rd->deemphasis == 50) ? SYSCONFIG1_DE : 0;
247 uint16_t bandspacing = CHANNEL_BANDw(band) |
248 CHANNEL_SPACEw(CHANNEL_SPACE_50KHZ);
249 uint16_t oldbs = cache[CHANNEL] & (CHANNEL_BAND | CHANNEL_SPACE);
251 rda5802_write_masked(SYSCONFIG1, deemphasis, SYSCONFIG1_DE);
252 rda5802_write_masked(CHANNEL, bandspacing, CHANNEL_BAND | CHANNEL_SPACE);
253 rda5802_write_cache();
255 /* Retune if this region change would change the channel number. */
256 if (oldbs != bandspacing) {
257 rda5802_set_frequency(curr_frequency);
261 static bool rda5802_st(void)
263 return (rda5802_read_reg(READCHAN) & READCHAN_ST);
266 /* tuner abstraction layer: set something to the tuner */
267 int rda5802_set(int setting, int value)
269 switch (setting) {
270 case RADIO_SLEEP:
271 if (value != 2) {
272 rda5802_sleep(value);
274 break;
276 case RADIO_FREQUENCY:
277 rda5802_set_frequency(value);
278 break;
280 case RADIO_SCAN_FREQUENCY:
281 rda5802_set_frequency(value);
282 return rda5802_tuned();
284 case RADIO_MUTE:
285 rda5802_write_masked(POWERCFG, value ? 0 : POWERCFG_DMUTE,
286 POWERCFG_DMUTE);
287 rda5802_write_masked(SYSCONFIG1, (3 << 9), (3 << 9));
288 rda5802_write_set(SYSCONFIG2, SYSCONFIG2_VOLUME);
289 rda5802_write_cache();
290 break;
292 case RADIO_REGION:
293 rda5802_set_region(value);
294 break;
296 case RADIO_FORCE_MONO:
297 rda5802_write_masked(POWERCFG, value ? POWERCFG_MONO : 0,
298 POWERCFG_MONO);
299 rda5802_write_cache();
300 break;
302 default:
303 return -1;
306 return 1;
309 /* tuner abstraction layer: read something from the tuner */
310 int rda5802_get(int setting)
312 int val = -1; /* default for unsupported query */
314 switch (setting) {
315 case RADIO_PRESENT:
316 val = tuner_present ? 1 : 0;
317 break;
319 case RADIO_TUNED:
320 val = rda5802_tuned();
321 break;
323 case RADIO_STEREO:
324 val = rda5802_st();
325 break;
328 return val;
331 void rda5802_dbg_info(struct rda5802_dbg_info *nfo)
333 rda5802_read(6);
334 memcpy(nfo->regs, cache, sizeof (nfo->regs));