Minor changes and fix HAVE_LCD_FLIP
[kugel-rb.git] / firmware / drivers / tuner / lv24020lp.c
blob75796a5d7ba230103f0849848c61e37f335bbd27
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 * Tuner driver for the Sanyo LV24020LP
11 * Copyright (C) 2007 Ivan Zupan
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include "config.h"
26 #include "thread.h"
27 #include "kernel.h"
28 #include "tuner.h" /* tuner abstraction interface */
29 #include "power.h"
30 #include "fmradio.h" /* physical interface driver */
31 #include "sound.h"
32 #include "system.h"
34 #ifndef BOOTLOADER
36 static struct mutex tuner_mtx;
38 /* define to enable tuner logging */
39 #undef SANYO_TUNER_LOG_FILE
40 #undef SANYO_TUNER_LOGF
42 #ifdef SANYO_TUNER_LOG_FILE
43 #include "file.h"
45 static int fd_log = -1;
47 #define TUNER_LOG_OPEN() if (fd_log < 0) \
48 fd_log = creat("/tuner_dump.txt", 0666)
49 /* syncing required because close() is never called */
50 #define TUNER_LOG_SYNC() fsync(fd_log)
51 #define TUNER_LOG(s...) fdprintf(fd_log, s)
53 #elif defined(SANYO_TUNER_LOGF)
54 /*#define LOGF_ENABLE*/
55 #include "logf.h"
57 #define TUNER_LOG_OPEN()
58 #define TUNER_LOG_SYNC()
59 #define TUNER_LOG(s...) logf(s)
61 #else
62 #define TUNER_LOG_OPEN()
63 #define TUNER_LOG_SYNC()
64 #define TUNER_LOG(s...)
65 #endif /* SANYO_TUNER_LOG */
67 /** tuner register defines **/
69 #if defined(SANSA_E200) || defined(SANSA_C200)
70 #define TUNER_GPIO_OUTPUT_EN GPIOH_OUTPUT_EN
71 #define TUNER_GPIO_OUTPUT_VAL GPIOH_OUTPUT_VAL
72 #define TUNER_GPIO_INPUT_VAL GPIOH_INPUT_VAL
73 #define FM_NRW_PIN 3
74 #define FM_CLOCK_PIN 4
75 #define FM_DATA_PIN 5
77 #elif defined(IAUDIO_7)
78 #define TUNER_GPIO_OUTPUT_EN GPIOA_DIR
79 #define TUNER_GPIO_OUTPUT_VAL GPIOA
80 #define TUNER_GPIO_INPUT_VAL GPIOA
81 #define FM_CLOCK_PIN 5
82 #define FM_DATA_PIN 6
83 #define FM_NRW_PIN 7
84 #elif defined(COWON_D2)
85 #define TUNER_GPIO_OUTPUT_EN GPIOC_DIR
86 #define TUNER_GPIO_OUTPUT_VAL GPIOC
87 #define TUNER_GPIO_INPUT_VAL GPIOC
88 #define FM_NRW_PIN 31
89 #define FM_CLOCK_PIN 29
90 #define FM_DATA_PIN 30
92 #else
93 #error GPIOs undefined for this target
94 #endif
96 #define FM_CLK_DELAY 1
98 /* block 1 registers */
100 /* R */
101 #define CHIP_ID 0x00
103 /* W */
104 #define BLK_SEL 0x01
105 #define BLK1 0x01
106 #define BLK2 0x02
108 /* W */
109 #define MSRC_SEL 0x02
110 #define MSR_O (1 << 7)
111 #define AFC_LVL (1 << 6)
112 #define AFC_SPD (1 << 5)
113 #define MSS_SD (1 << 2)
114 #define MSS_FM (1 << 1)
115 #define MSS_IF (1 << 0)
117 /* W */
118 #define FM_OSC 0x03
120 /* W */
121 #define SD_OSC 0x04
123 /* W */
124 #define IF_OSC 0x05
126 /* W */
127 #define CNT_CTRL 0x06
128 #define CNT1_CLR (1 << 7)
129 #define CTAB(x) ((x) & (0x7 << 4))
130 #define CTAB_STOP_2 (0x0 << 4)
131 #define CTAB_STOP_8 (0x1 << 4)
132 #define CTAB_STOP_32 (0x2 << 4)
133 #define CTAB_STOP_128 (0x3 << 4)
134 #define CTAB_STOP_512 (0x4 << 4)
135 #define CTAB_STOP_2048 (0x5 << 4)
136 #define CTAB_STOP_8192 (0x6 << 4)
137 #define CTAB_STOP_32768 (0x7 << 4)
138 #define SWP_CNT_L (1 << 3)
139 #define CNT_EN (1 << 2)
140 #define CNT_SEL (1 << 1)
141 #define CNT_SET (1 << 0)
143 /* W */
144 #define IRQ_MSK 0x08
145 #define IM_MS (1 << 6)
146 #define IRQ_LVL (1 << 3)
147 #define IM_AFC (1 << 2)
148 #define IM_FS (1 << 1)
149 #define IM_CNT2 (1 << 0)
151 /* W */
152 #define FM_CAP 0x09
154 /* R */
155 #define CNT_L 0x0a /* Counter register low value */
157 /* R */
158 #define CNT_H 0x0b /* Counter register high value */
160 /* R */
161 #define CTRL_STAT 0x0c
162 #define AFC_FLG (1 << 0)
164 /* R */
165 #define RADIO_STAT 0x0d
166 #define RSS_MS (1 << 7)
167 #define RSS_FS(x) ((x) & 0x7f)
168 #define RSS_FS_GET(x) ((x) & 0x7f)
169 #define RSS_FS_SET(x) (x)
170 /* Note: Reading this register will clear field strength and mono/stereo interrupt. */
172 /* R */
173 #define IRQ_ID 0x0e
174 #define II_CNT2 (1 << 5)
175 #define II_AFC (1 << 3)
176 #define II_FS_MS (1 << 0)
178 /* W */
179 #define IRQ_OUT 0x0f
181 /* block 2 registers - offset added in order to id and avoid manual
182 switching */
183 #define BLK2_START 0x10
185 /* W */
186 #define RADIO_CTRL1 (0x02 + BLK2_START)
187 #define EN_MEAS (1 << 7)
188 #define EN_AFC (1 << 6)
189 #define DIR_AFC (1 << 3)
190 #define RST_AFC (1 << 2)
192 /* W */
193 #define IF_CENTER (0x03 + BLK2_START)
195 /* W */
196 #define IF_BW (0x05 + BLK2_START)
198 /* W */
199 #define RADIO_CTRL2 (0x06 + BLK2_START)
200 #define VREF2 (1 << 7)
201 #define VREF (1 << 6)
202 #define STABI_BP (1 << 5)
203 #define IF_PM_L (1 << 4)
204 #define AGCSP (1 << 1)
205 #define AM_ANT_BSW (1 << 0) /* ?? */
207 /* W */
208 #define RADIO_CTRL3 (0x07 + BLK2_START)
209 #define AGC_SLVL (1 << 7)
210 #define VOLSH (1 << 6)
211 #define TB_ON (1 << 5)
212 #define AMUTE_L (1 << 4)
213 #define SE_FM (1 << 3)
214 #define SE_BE (1 << 1)
215 #define SE_EXT (1 << 0) /* For LV24000=0, LV24001/24002=Ext source enab. */
217 /* W */
218 #define STEREO_CTRL (0x08 + BLK2_START)
219 #define FRCST (1 << 7)
220 #define FMCS(x) ((x) & (0x7 << 4))
221 #define FMCS_GET(x) (((x) & (0x7 << 4)) >> 4)
222 #define FMCS_SET(x) ((x) << 4)
223 #define AUTOSSR (1 << 3)
224 #define PILTCA (1 << 2)
225 #define SD_PM (1 << 1)
226 #define ST_M (1 << 0)
228 /* W */
229 #define AUDIO_CTRL1 (0x09 + BLK2_START)
230 #define TONE_LVL(x) ((x) & (0xf << 4))
231 #define TONE_LVL_GET(x) (((x) & (0xf << 4)) >> 4)
232 #define TONE_LVL_SET(x) ((x) << 4)
233 #define VOL_LVL(x) ((x) & 0xf)
234 #define VOL_LVL_GET(x) ((x) & 0xf)
235 #define VOL_LVL_SET(x) ((x) << 4)
237 /* W */
238 #define AUDIO_CTRL2 (0x0a + BLK2_START)
239 #define BASS_PP (1 << 0)
240 #define BASS_P (1 << 1) /* BASS_P, BASS_N are mutually-exclusive */
241 #define BASS_N (1 << 2)
242 #define TREB_P (1 << 3) /* TREB_P, TREB_N are mutually-exclusive */
243 #define TREB_N (1 << 4)
244 #define DEEMP (1 << 5)
245 #define BPFREQ(x) ((x) & (0x3 << 6))
246 #define BPFREQ_2_0K (0x0 << 6)
247 #define BPFREQ_1_0K (0x1 << 6)
248 #define BPFREQ_0_5K (0x2 << 6)
249 #define BPFREQ_HIGH (0x3 << 6)
251 /* W */
252 #define PW_SCTRL (0x0b + BLK2_START)
253 #define SS_CTRL(x) ((x) & (0x7 << 5))
254 #define SS_CTRL_GET(x) (((x) & (0x7 << 5)) >> 5)
255 #define SS_CTRL_SET(x) ((x) << 5)
256 #define SM_CTRL(x) ((x) & (0x7 << 2))
257 #define SM_CTRL_GET(x) (((x) & (0x7 << 2)) >> 2)
258 #define SM_CTRL_SET(x) ((x) << 2)
259 #define PW_HPA (1 << 1) /* LV24002 only */
260 #define PW_RAD (1 << 0)
262 /* shadow for writeable registers */
263 #define TUNER_POWERED (1 << 0)
264 #define TUNER_PRESENT (1 << 1)
265 #define TUNER_AWAKE (1 << 2)
266 #define TUNER_PRESENCE_CHECKED (1 << 3)
267 static unsigned tuner_status = 0;
269 static unsigned char lv24020lp_regs[0x1c];
271 static const int sw_osc_low = 10; /* 30; */
272 static const int sw_osc_high = 240; /* 200; */
273 static const int sw_cap_low = 0;
274 static const int sw_cap_high = 191;
276 /* linear coefficients used for tuning */
277 static int coef_00, coef_01, coef_10, coef_11;
279 /* DAC control register set values */
280 static int if_set, sd_set;
282 static inline bool tuner_awake(void)
284 return (tuner_status & TUNER_AWAKE) != 0;
287 /* send a byte to the tuner - expects write mode to be current */
288 static void lv24020lp_send_byte(unsigned int byte)
290 int i;
292 for (i = 0; i < 8; i++)
294 TUNER_GPIO_OUTPUT_VAL &= ~(1 << FM_CLOCK_PIN);
296 if (byte & 1)
297 TUNER_GPIO_OUTPUT_VAL |= (1 << FM_DATA_PIN);
298 else
299 TUNER_GPIO_OUTPUT_VAL &= ~(1 << FM_DATA_PIN);
301 udelay(FM_CLK_DELAY);
303 TUNER_GPIO_OUTPUT_VAL |= (1 << FM_CLOCK_PIN);
304 udelay(FM_CLK_DELAY);
306 byte >>= 1;
310 /* end a write cycle on the tuner */
311 static void lv24020lp_end_write(void)
313 /* switch back to read mode */
314 TUNER_GPIO_OUTPUT_EN &= ~(1 << FM_DATA_PIN);
315 TUNER_GPIO_OUTPUT_VAL &= ~(1 << FM_NRW_PIN);
316 udelay(FM_CLK_DELAY);
319 /* prepare a write cycle on the tuner */
320 static unsigned int lv24020lp_begin_write(unsigned int address)
322 /* Get register's block, translate address */
323 unsigned int blk = (address >= BLK2_START) ?
324 (address -= BLK2_START, BLK2) : BLK1;
326 for (;;)
328 /* Prepare 3-wire bus pins for write cycle */
329 TUNER_GPIO_OUTPUT_VAL |= (1 << FM_NRW_PIN);
330 TUNER_GPIO_OUTPUT_EN |= (1 << FM_DATA_PIN);
331 udelay(FM_CLK_DELAY);
333 /* current block == register block? */
334 if (blk == lv24020lp_regs[BLK_SEL])
335 return address;
337 /* switch block */
338 lv24020lp_regs[BLK_SEL] = blk;
340 /* data first */
341 lv24020lp_send_byte(blk);
342 /* then address */
343 lv24020lp_send_byte(BLK_SEL);
345 lv24020lp_end_write();
349 /* write a byte to a tuner register */
350 static void lv24020lp_write(unsigned int address, unsigned int data)
352 /* shadow logical values but do logical=>physical remappings on some
353 registers' data. */
354 lv24020lp_regs[address] = data;
356 switch (address)
358 case FM_OSC:
359 /* L: 000..255
360 * P: 255..000 */
361 data = 255 - data;
362 break;
363 case FM_CAP:
364 /* L: 000..063, 064..191
365 * P: 255..192, 127..000 */
366 data = ((data < 64) ? 255 : (255 - 64)) - data;
367 break;
368 case RADIO_CTRL1:
369 /* L: data
370 * P: data | always "1" bits */
371 data |= (1 << 4) | (1 << 1) | (1 << 0);
372 break;
375 /* Check if interface is turned on */
376 if (!(tuner_status & TUNER_POWERED))
377 return;
379 address = lv24020lp_begin_write(address);
381 /* data first */
382 lv24020lp_send_byte(data);
383 /* then address */
384 lv24020lp_send_byte(address);
386 lv24020lp_end_write();
389 /* helpers to set/clear register bits */
390 static void lv24020lp_write_set(unsigned int address, unsigned int bits)
392 lv24020lp_write(address, lv24020lp_regs[address] | bits);
395 static void lv24020lp_write_clear(unsigned int address, unsigned int bits)
397 lv24020lp_write(address, lv24020lp_regs[address] & ~bits);
400 /* read a byte from a tuner register */
401 static unsigned int lv24020lp_read(unsigned int address)
403 int i;
404 unsigned int toread;
406 /* Check if interface is turned on */
407 if (!(tuner_status & TUNER_POWERED))
408 return 0;
410 address = lv24020lp_begin_write(address);
412 /* address */
413 lv24020lp_send_byte(address);
415 lv24020lp_end_write();
417 /* data */
418 toread = 0;
419 for (i = 0; i < 8; i++)
421 TUNER_GPIO_OUTPUT_VAL &= ~(1 << FM_CLOCK_PIN);
422 udelay(FM_CLK_DELAY);
424 if (TUNER_GPIO_INPUT_VAL & (1 << FM_DATA_PIN))
425 toread |= (1 << i);
427 TUNER_GPIO_OUTPUT_VAL |= (1 << FM_CLOCK_PIN);
428 udelay(FM_CLK_DELAY);
431 return toread;
434 /* enables auto frequency centering */
435 static void enable_afc(bool enabled)
437 unsigned int radio_ctrl1 = lv24020lp_regs[RADIO_CTRL1];
439 if (enabled)
441 radio_ctrl1 &= ~RST_AFC;
442 radio_ctrl1 |= EN_AFC;
444 else
446 radio_ctrl1 |= RST_AFC;
447 radio_ctrl1 &= ~EN_AFC;
450 lv24020lp_write(RADIO_CTRL1, radio_ctrl1);
453 static int calculate_coef(unsigned fkhz)
455 /* Overflow below 66000kHz --
456 My tuner tunes down to a min of ~72600kHz but datasheet mentions
457 66000kHz as the minimum. ?? Perhaps 76000kHz was intended? */
458 return fkhz < 66000 ?
459 0x7fffffff : 0x81d1a47efc5cb700ull / ((uint64_t)fkhz*fkhz);
462 static int interpolate_x(int expected_y, int x1, int x2, int y1, int y2)
464 return y1 == y2 ?
465 0 : (int64_t)(expected_y - y1)*(x2 - x1) / (y2 - y1) + x1;
468 static int interpolate_y(int expected_x, int x1, int x2, int y1, int y2)
470 return x1 == x2 ?
471 0 : (int64_t)(expected_x - x1)*(y2 - y1) / (x2 - x1) + y1;
474 /* this performs measurements of IF, FM and Stereo frequencies
475 * Input can be: MSS_FM, MSS_IF, MSS_SD */
476 static int tuner_measure(unsigned char type, int scale, int duration)
478 int64_t finval;
480 /* enable measuring */
481 lv24020lp_write_set(MSRC_SEL, type);
482 lv24020lp_write_clear(CNT_CTRL, CNT_SEL);
483 lv24020lp_write_set(RADIO_CTRL1, EN_MEAS);
485 /* reset counter */
486 lv24020lp_write_set(CNT_CTRL, CNT1_CLR);
487 lv24020lp_write_clear(CNT_CTRL, CNT1_CLR);
489 /* start counter, delay for specified time and stop it */
490 lv24020lp_write_set(CNT_CTRL, CNT_EN);
491 udelay(duration*1000 - 16);
492 lv24020lp_write_clear(CNT_CTRL, CNT_EN);
494 /* read tick count */
495 finval = (lv24020lp_read(CNT_H) << 8) | lv24020lp_read(CNT_L);
497 /* restore measure mode */
498 lv24020lp_write_clear(RADIO_CTRL1, EN_MEAS);
499 lv24020lp_write_clear(MSRC_SEL, type);
501 /* convert value */
502 if (type == MSS_FM)
503 finval = scale*finval*256 / duration;
504 else
505 finval = scale*finval / duration;
507 /* This function takes a loooong time and other stuff needs
508 running by now */
509 yield();
511 return (int)finval;
514 /* set the FM oscillator frequency */
515 static void set_frequency(int freq)
517 int coef, cap_value, osc_value;
518 int f1, f2, x1, x2;
519 int count;
521 TUNER_LOG_OPEN();
523 TUNER_LOG("set_frequency(%d)\n", freq);
525 enable_afc(false);
527 /* MHz -> kHz */
528 freq /= 1000;
530 TUNER_LOG("Select cap:\n");
532 coef = calculate_coef(freq);
533 cap_value = interpolate_x(coef, sw_cap_low, sw_cap_high,
534 coef_00, coef_01);
536 osc_value = sw_osc_low;
537 lv24020lp_write(FM_OSC, osc_value);
539 /* Just in case - don't go into infinite loop */
540 for (count = 0; count < 30; count++)
542 int y0 = interpolate_y(cap_value, sw_cap_low, sw_cap_high,
543 coef_00, coef_01);
544 int y1 = interpolate_y(cap_value, sw_cap_low, sw_cap_high,
545 coef_10, coef_11);
546 int coef_fcur, cap_new, coef_cor, range;
548 lv24020lp_write(FM_CAP, cap_value);
550 range = y1 - y0;
551 f1 = tuner_measure(MSS_FM, 1, 16);
552 coef_fcur = calculate_coef(f1);
553 coef_cor = calculate_coef((f1*1000 + 32*256) / 1000);
554 y0 = coef_cor;
555 y1 = y0 + range;
557 TUNER_LOG("%d %d %d %d %d %d %d %d\n",
558 f1, cap_value, coef, coef_fcur, coef_cor, y0, y1, range);
560 if (coef >= y0 && coef <= y1)
562 osc_value = interpolate_x(coef, sw_osc_low, sw_osc_high,
563 y0, y1);
565 if (osc_value >= sw_osc_low && osc_value <= sw_osc_high)
566 break;
569 cap_new = interpolate_x(coef, cap_value, sw_cap_high,
570 coef_fcur, coef_01);
572 if (cap_new == cap_value)
574 if (coef < coef_fcur)
575 cap_value++;
576 else
577 cap_value--;
579 else
581 cap_value = cap_new;
585 TUNER_LOG("osc_value: %d\n", osc_value);
587 TUNER_LOG("Tune:\n");
589 x1 = sw_osc_low, x2 = sw_osc_high;
590 /* FM_OSC already at SW_OSC low and f1 is already the measured
591 frequency */
595 int x2_new;
597 lv24020lp_write(FM_OSC, x2);
598 f2 = tuner_measure(MSS_FM, 1, 16);
600 if (abs(f2 - freq) <= 16)
602 TUNER_LOG("%d %d %d %d\n", f1, f2, x1, x2);
603 break;
606 x2_new = interpolate_x(freq, x1, x2, f1, f2);
608 x1 = x2, f1 = f2, x2 = x2_new;
609 TUNER_LOG("%d %d %d %d\n", f1, f2, x1, x2);
611 while (x2 != 0);
613 if (x2 == 0)
615 /* May still be close enough */
616 TUNER_LOG("tuning failed - diff: %d\n", f2 - freq);
619 enable_afc(true);
621 TUNER_LOG("\n");
623 TUNER_LOG_SYNC();
626 #define TOO_SMALL (1 << 0)
627 #define TOO_BIG (1 << 1)
628 #define APPROACH_UP_1 (1 << 2)
629 #define APPROACH_DOWN_1 (1 << 3)
631 static void fine_step_tune(int (*setcmp)(int regval), int regval, int step)
633 /* Registers are not always stable, timeout if best fit not found soon
634 enough */
635 unsigned long abort = current_tick + HZ*2;
636 int flags = 0;
638 while (TIME_BEFORE(current_tick, abort))
640 int cmp;
642 regval = regval + step;
644 cmp = setcmp(regval);
646 if (cmp == 0)
647 break;
649 step = abs(step);
651 if (cmp < 0)
653 flags |= TOO_SMALL;
654 if (step == 1)
655 flags |= APPROACH_UP_1;
657 else
659 step = -step;
660 flags |= TOO_BIG;
661 if (step == -1)
662 step |= APPROACH_DOWN_1;
665 if ((flags & APPROACH_UP_1) && (flags & APPROACH_DOWN_1))
666 break; /* approached with step=1: best fit value found */
668 if ((flags & TOO_SMALL) && (flags & TOO_BIG))
670 step /= 2;
671 if (step == 0)
672 step = 1;
673 flags &= ~(TOO_SMALL | TOO_BIG);
678 static int if_setcmp(int regval)
680 lv24020lp_write(IF_OSC, regval);
681 lv24020lp_write(IF_CENTER, regval);
682 lv24020lp_write(IF_BW, 65*regval/100);
684 if_set = tuner_measure(MSS_IF, 1000, 32);
686 /* This register is bounces around by a few hundred Hz and doesn't seem
687 to be precisely tuneable. Just do 110000 +/- 500 since it's not very
688 critical it seems. */
689 if (abs(if_set - 110000) <= 500)
690 return 0;
692 return if_set < 110000 ? -1 : 1;
695 static int sd_setcmp(int regval)
697 lv24020lp_write(SD_OSC, regval);
699 sd_set = tuner_measure(MSS_SD, 1000, 32);
701 if (abs(sd_set - 38300) <= 31)
702 return 0;
704 return sd_set < 38300 ? -1 : 1;
707 static void set_sleep(bool sleep)
709 if (sleep || tuner_awake())
710 return;
712 if ((tuner_status & (TUNER_PRESENT | TUNER_POWERED)) !=
713 (TUNER_PRESENT | TUNER_POWERED))
714 return;
716 enable_afc(false);
718 /* 2. Calibrate the IF frequency at 110 kHz: */
719 lv24020lp_write_clear(RADIO_CTRL2, IF_PM_L);
720 fine_step_tune(if_setcmp, 0x80, 8);
721 lv24020lp_write_set(RADIO_CTRL2, IF_PM_L);
723 /* 3. Calibrate the stereo decoder clock at 38.3 kHz: */
724 lv24020lp_write_set(STEREO_CTRL, SD_PM);
725 fine_step_tune(sd_setcmp, 0x80, 8);
726 lv24020lp_write_clear(STEREO_CTRL, SD_PM);
728 /* calculate FM tuning coefficients */
729 lv24020lp_write(FM_CAP, sw_cap_low);
730 lv24020lp_write(FM_OSC, sw_osc_low);
731 coef_00 = calculate_coef(tuner_measure(MSS_FM, 1, 64));
733 lv24020lp_write(FM_CAP, sw_cap_high);
734 coef_01 = calculate_coef(tuner_measure(MSS_FM, 1, 64));
736 lv24020lp_write(FM_CAP, sw_cap_low);
737 lv24020lp_write(FM_OSC, sw_osc_high);
738 coef_10 = calculate_coef(tuner_measure(MSS_FM, 1, 64));
740 lv24020lp_write(FM_CAP, sw_cap_high);
741 coef_11 = calculate_coef(tuner_measure(MSS_FM, 1, 64));
743 /* set various audio level settings */
744 lv24020lp_write(AUDIO_CTRL1, TONE_LVL_SET(0) | VOL_LVL_SET(0));
745 lv24020lp_write_set(RADIO_CTRL2, AGCSP);
746 lv24020lp_write_set(RADIO_CTRL3, VOLSH);
747 lv24020lp_write(STEREO_CTRL, FMCS_SET(7) | AUTOSSR);
748 lv24020lp_write(PW_SCTRL, SS_CTRL_SET(3) | SM_CTRL_SET(1) |
749 PW_RAD);
751 tuner_status |= TUNER_AWAKE;
754 static int lp24020lp_tuned(void)
756 return RSS_FS(lv24020lp_read(RADIO_STAT)) < 0x1f;
759 static int lv24020lp_debug_info(int setting)
761 int val = -1;
763 if (setting >= LV24020LP_DEBUG_FIRST && setting <= LV24020LP_DEBUG_LAST)
765 val = 0;
767 if (tuner_awake())
769 switch (setting)
771 /* tuner-specific debug info */
772 case LV24020LP_CTRL_STAT:
773 val = lv24020lp_read(CTRL_STAT);
774 break;
776 case LV24020LP_REG_STAT:
777 val = lv24020lp_read(RADIO_STAT);
778 break;
780 case LV24020LP_MSS_FM:
781 val = tuner_measure(MSS_FM, 1, 16);
782 break;
784 case LV24020LP_MSS_IF:
785 val = tuner_measure(MSS_IF, 1000, 16);
786 break;
788 case LV24020LP_MSS_SD:
789 val = tuner_measure(MSS_SD, 1000, 16);
790 break;
792 case LV24020LP_IF_SET:
793 val = if_set;
794 break;
796 case LV24020LP_SD_SET:
797 val = sd_set;
798 break;
803 return val;
806 /** Public interfaces **/
807 void lv24020lp_init(void)
809 mutex_init(&tuner_mtx);
812 void lv24020lp_lock(void)
814 mutex_lock(&tuner_mtx);
817 void lv24020lp_unlock(void)
819 mutex_unlock(&tuner_mtx);
822 /* This function expects the driver to be locked externally */
823 void lv24020lp_power(bool status)
825 static const unsigned char tuner_defaults[][2] =
827 /* Block 1 writeable registers */
828 { MSRC_SEL, AFC_LVL },
829 { FM_OSC, 0x80 },
830 { SD_OSC, 0x80 },
831 { IF_OSC, 0x80 },
832 { CNT_CTRL, CNT1_CLR | SWP_CNT_L },
833 { IRQ_MSK, 0x00 }, /* IRQ_LVL -> Low to High */
834 { FM_CAP, 0x80 },
835 /* { IRQ_OUT, 0x00 }, No action on this register (skip) */
836 /* Block 2 writeable registers */
837 { RADIO_CTRL1, EN_AFC },
838 { IF_CENTER, 0x80 },
839 { IF_BW, 65*0x80 / 100 }, /* 65% of IF_OSC */
840 { RADIO_CTRL2, IF_PM_L },
841 { RADIO_CTRL3, AGC_SLVL | SE_FM },
842 { STEREO_CTRL, FMCS_SET(4) | AUTOSSR },
843 { AUDIO_CTRL1, TONE_LVL_SET(7) | VOL_LVL_SET(7) },
844 { AUDIO_CTRL2, BPFREQ_HIGH }, /* deemphasis 50us */
845 { PW_SCTRL, SS_CTRL_SET(3) | SM_CTRL_SET(3) | PW_RAD },
848 unsigned i;
850 if (status)
852 tuner_status |= (TUNER_PRESENCE_CHECKED | TUNER_POWERED);
854 /* if tuner is present, CHIP ID is 0x09 */
855 if (lv24020lp_read(CHIP_ID) == 0x09)
857 tuner_status |= TUNER_PRESENT;
859 /* After power-up, the LV2400x needs to be initialized as
860 follows: */
862 /* 1. Write default values to the registers: */
863 lv24020lp_regs[BLK_SEL] = 0; /* Force a switch on the first */
864 for (i = 0; i < ARRAYLEN(tuner_defaults); i++)
865 lv24020lp_write(tuner_defaults[i][0], tuner_defaults[i][1]);
867 /* Complete the startup calibration if the tuner is woken */
868 sleep(HZ/10);
871 else
873 /* Power off */
874 if (tuner_status & TUNER_PRESENT)
875 lv24020lp_write_clear(PW_SCTRL, PW_RAD);
877 tuner_status &= ~(TUNER_POWERED | TUNER_AWAKE);
881 int lv24020lp_set(int setting, int value)
883 int val = 1;
885 mutex_lock(&tuner_mtx);
887 switch(setting)
889 case RADIO_SLEEP:
890 set_sleep(value);
891 break;
893 case RADIO_FREQUENCY:
894 set_frequency(value);
895 break;
897 case RADIO_SCAN_FREQUENCY:
898 /* TODO: really implement this */
899 set_frequency(value);
900 val = lp24020lp_tuned();
901 break;
903 case RADIO_MUTE:
904 if (value)
905 lv24020lp_write_clear(RADIO_CTRL3, AMUTE_L);
906 else
907 lv24020lp_write_set(RADIO_CTRL3, AMUTE_L);
908 break;
910 case RADIO_REGION:
911 if (lv24020lp_region_data[value])
912 lv24020lp_write_set(AUDIO_CTRL2, DEEMP);
913 else
914 lv24020lp_write_clear(AUDIO_CTRL2, DEEMP);
915 break;
917 case RADIO_FORCE_MONO:
918 if (value)
919 lv24020lp_write_set(STEREO_CTRL, ST_M);
920 else
921 lv24020lp_write_clear(STEREO_CTRL, ST_M);
922 break;
924 default:
925 value = -1;
928 mutex_unlock(&tuner_mtx);
930 return val;
933 int lv24020lp_get(int setting)
935 int val = -1;
937 mutex_lock(&tuner_mtx);
939 switch(setting)
941 case RADIO_TUNED:
942 /* TODO: really implement this */
943 val = lp24020lp_tuned();
944 break;
946 case RADIO_STEREO:
947 val = (lv24020lp_read(RADIO_STAT) & RSS_MS) != 0;
948 break;
950 case RADIO_PRESENT:
952 bool fmstatus = true;
954 if (!(tuner_status & TUNER_PRESENCE_CHECKED))
955 fmstatus = tuner_power(true);
957 val = (tuner_status & TUNER_PRESENT) != 0;
959 if (!fmstatus)
960 tuner_power(false);
961 break;
964 default:
965 val = lv24020lp_debug_info(setting);
968 mutex_unlock(&tuner_mtx);
970 return val;
972 #endif /* BOOTLOADER */