2007-06-06 19:23:48 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
* Tuner driver for the Sanyo LV24020LP
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Ivan Zupan
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2007-06-06 19:23:48 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "config.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "tuner.h" /* tuner abstraction interface */
|
2007-07-14 22:00:50 +00:00
|
|
|
#include "power.h"
|
2007-06-06 19:23:48 +00:00
|
|
|
#include "fmradio.h" /* physical interface driver */
|
|
|
|
#include "sound.h"
|
|
|
|
#include "system.h"
|
|
|
|
|
|
|
|
#ifndef BOOTLOADER
|
|
|
|
|
2007-07-14 22:00:50 +00:00
|
|
|
static struct mutex tuner_mtx;
|
|
|
|
|
2010-11-11 21:13:29 +00:00
|
|
|
/* define RSSI range */
|
|
|
|
#define RSSI_MIN 5
|
|
|
|
#define RSSI_MAX 75
|
|
|
|
|
2007-06-06 19:23:48 +00:00
|
|
|
/* define to enable tuner logging */
|
2009-03-21 00:13:20 +00:00
|
|
|
#undef SANYO_TUNER_LOG_FILE
|
|
|
|
#undef SANYO_TUNER_LOGF
|
2007-06-06 19:23:48 +00:00
|
|
|
|
2009-03-21 00:13:20 +00:00
|
|
|
#ifdef SANYO_TUNER_LOG_FILE
|
2007-06-06 19:23:48 +00:00
|
|
|
#include "file.h"
|
|
|
|
|
|
|
|
static int fd_log = -1;
|
|
|
|
|
|
|
|
#define TUNER_LOG_OPEN() if (fd_log < 0) \
|
2010-05-06 17:35:04 +00:00
|
|
|
fd_log = creat("/tuner_dump.txt", 0666)
|
2007-06-06 19:23:48 +00:00
|
|
|
/* syncing required because close() is never called */
|
|
|
|
#define TUNER_LOG_SYNC() fsync(fd_log)
|
|
|
|
#define TUNER_LOG(s...) fdprintf(fd_log, s)
|
2009-03-21 00:13:20 +00:00
|
|
|
|
|
|
|
#elif defined(SANYO_TUNER_LOGF)
|
2009-11-03 16:25:03 +00:00
|
|
|
/*#define LOGF_ENABLE*/
|
2009-03-21 00:13:20 +00:00
|
|
|
#include "logf.h"
|
|
|
|
|
|
|
|
#define TUNER_LOG_OPEN()
|
|
|
|
#define TUNER_LOG_SYNC()
|
|
|
|
#define TUNER_LOG(s...) logf(s)
|
|
|
|
|
2007-06-06 19:23:48 +00:00
|
|
|
#else
|
|
|
|
#define TUNER_LOG_OPEN()
|
|
|
|
#define TUNER_LOG_SYNC()
|
|
|
|
#define TUNER_LOG(s...)
|
|
|
|
#endif /* SANYO_TUNER_LOG */
|
|
|
|
|
|
|
|
/** tuner register defines **/
|
|
|
|
|
2008-06-07 19:23:57 +00:00
|
|
|
#if defined(SANSA_E200) || defined(SANSA_C200)
|
2010-06-22 04:00:34 +00:00
|
|
|
#define TUNER_GPIO_INPUT_VAL GPIOH_INPUT_VAL
|
|
|
|
#define TUNER_GPIO_OUTPUT_EN_SET(mask) GPIO_SET_BITWISE(GPIOH_OUTPUT_EN, mask)
|
|
|
|
#define TUNER_GPIO_OUTPUT_EN_CLEAR(mask) GPIO_CLEAR_BITWISE(GPIOH_OUTPUT_EN, mask)
|
|
|
|
#define TUNER_GPIO_OUTPUT_VAL_SET(mask) GPIO_SET_BITWISE(GPIOH_OUTPUT_VAL, mask)
|
|
|
|
#define TUNER_GPIO_OUTPUT_VAL_CLEAR(mask) GPIO_CLEAR_BITWISE(GPIOH_OUTPUT_VAL, mask)
|
2007-06-06 19:23:48 +00:00
|
|
|
#define FM_NRW_PIN 3
|
|
|
|
#define FM_CLOCK_PIN 4
|
|
|
|
#define FM_DATA_PIN 5
|
2009-01-22 22:05:04 +00:00
|
|
|
|
2008-06-07 19:23:57 +00:00
|
|
|
#elif defined(COWON_D2)
|
2010-06-22 04:00:34 +00:00
|
|
|
#define TUNER_GPIO_INPUT_VAL GPIOC
|
|
|
|
#define TUNER_GPIO_OUTPUT_EN_SET(mask) (GPIOC_DIR |= (mask))
|
|
|
|
#define TUNER_GPIO_OUTPUT_EN_CLEAR(mask) (GPIOC_DIR &= ~(mask))
|
|
|
|
#define TUNER_GPIO_OUTPUT_VAL_SET(mask) (GPIOC |= (mask))
|
|
|
|
#define TUNER_GPIO_OUTPUT_VAL_CLEAR(mask) (GPIOC &= ~(mask))
|
2008-06-07 19:23:57 +00:00
|
|
|
#define FM_NRW_PIN 31
|
|
|
|
#define FM_CLOCK_PIN 29
|
|
|
|
#define FM_DATA_PIN 30
|
2009-03-20 23:14:05 +00:00
|
|
|
|
2008-06-07 19:23:57 +00:00
|
|
|
#else
|
|
|
|
#error GPIOs undefined for this target
|
|
|
|
#endif
|
|
|
|
|
2007-06-06 19:23:48 +00:00
|
|
|
#define FM_CLK_DELAY 1
|
|
|
|
|
|
|
|
/* block 1 registers */
|
|
|
|
|
|
|
|
/* R */
|
|
|
|
#define CHIP_ID 0x00
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define BLK_SEL 0x01
|
|
|
|
#define BLK1 0x01
|
|
|
|
#define BLK2 0x02
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define MSRC_SEL 0x02
|
|
|
|
#define MSR_O (1 << 7)
|
|
|
|
#define AFC_LVL (1 << 6)
|
|
|
|
#define AFC_SPD (1 << 5)
|
|
|
|
#define MSS_SD (1 << 2)
|
|
|
|
#define MSS_FM (1 << 1)
|
|
|
|
#define MSS_IF (1 << 0)
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define FM_OSC 0x03
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define SD_OSC 0x04
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define IF_OSC 0x05
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define CNT_CTRL 0x06
|
|
|
|
#define CNT1_CLR (1 << 7)
|
|
|
|
#define CTAB(x) ((x) & (0x7 << 4))
|
|
|
|
#define CTAB_STOP_2 (0x0 << 4)
|
|
|
|
#define CTAB_STOP_8 (0x1 << 4)
|
|
|
|
#define CTAB_STOP_32 (0x2 << 4)
|
|
|
|
#define CTAB_STOP_128 (0x3 << 4)
|
|
|
|
#define CTAB_STOP_512 (0x4 << 4)
|
|
|
|
#define CTAB_STOP_2048 (0x5 << 4)
|
|
|
|
#define CTAB_STOP_8192 (0x6 << 4)
|
|
|
|
#define CTAB_STOP_32768 (0x7 << 4)
|
|
|
|
#define SWP_CNT_L (1 << 3)
|
|
|
|
#define CNT_EN (1 << 2)
|
|
|
|
#define CNT_SEL (1 << 1)
|
|
|
|
#define CNT_SET (1 << 0)
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define IRQ_MSK 0x08
|
|
|
|
#define IM_MS (1 << 6)
|
|
|
|
#define IRQ_LVL (1 << 3)
|
|
|
|
#define IM_AFC (1 << 2)
|
|
|
|
#define IM_FS (1 << 1)
|
|
|
|
#define IM_CNT2 (1 << 0)
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define FM_CAP 0x09
|
|
|
|
|
|
|
|
/* R */
|
|
|
|
#define CNT_L 0x0a /* Counter register low value */
|
|
|
|
|
|
|
|
/* R */
|
|
|
|
#define CNT_H 0x0b /* Counter register high value */
|
|
|
|
|
|
|
|
/* R */
|
|
|
|
#define CTRL_STAT 0x0c
|
|
|
|
#define AFC_FLG (1 << 0)
|
|
|
|
|
|
|
|
/* R */
|
|
|
|
#define RADIO_STAT 0x0d
|
|
|
|
#define RSS_MS (1 << 7)
|
|
|
|
#define RSS_FS(x) ((x) & 0x7f)
|
|
|
|
#define RSS_FS_GET(x) ((x) & 0x7f)
|
|
|
|
#define RSS_FS_SET(x) (x)
|
|
|
|
/* Note: Reading this register will clear field strength and mono/stereo interrupt. */
|
|
|
|
|
|
|
|
/* R */
|
|
|
|
#define IRQ_ID 0x0e
|
|
|
|
#define II_CNT2 (1 << 5)
|
|
|
|
#define II_AFC (1 << 3)
|
|
|
|
#define II_FS_MS (1 << 0)
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define IRQ_OUT 0x0f
|
|
|
|
|
|
|
|
/* block 2 registers - offset added in order to id and avoid manual
|
|
|
|
switching */
|
|
|
|
#define BLK2_START 0x10
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define RADIO_CTRL1 (0x02 + BLK2_START)
|
|
|
|
#define EN_MEAS (1 << 7)
|
|
|
|
#define EN_AFC (1 << 6)
|
|
|
|
#define DIR_AFC (1 << 3)
|
|
|
|
#define RST_AFC (1 << 2)
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define IF_CENTER (0x03 + BLK2_START)
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define IF_BW (0x05 + BLK2_START)
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define RADIO_CTRL2 (0x06 + BLK2_START)
|
|
|
|
#define VREF2 (1 << 7)
|
|
|
|
#define VREF (1 << 6)
|
|
|
|
#define STABI_BP (1 << 5)
|
|
|
|
#define IF_PM_L (1 << 4)
|
|
|
|
#define AGCSP (1 << 1)
|
|
|
|
#define AM_ANT_BSW (1 << 0) /* ?? */
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define RADIO_CTRL3 (0x07 + BLK2_START)
|
|
|
|
#define AGC_SLVL (1 << 7)
|
|
|
|
#define VOLSH (1 << 6)
|
|
|
|
#define TB_ON (1 << 5)
|
|
|
|
#define AMUTE_L (1 << 4)
|
|
|
|
#define SE_FM (1 << 3)
|
|
|
|
#define SE_BE (1 << 1)
|
|
|
|
#define SE_EXT (1 << 0) /* For LV24000=0, LV24001/24002=Ext source enab. */
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define STEREO_CTRL (0x08 + BLK2_START)
|
|
|
|
#define FRCST (1 << 7)
|
|
|
|
#define FMCS(x) ((x) & (0x7 << 4))
|
|
|
|
#define FMCS_GET(x) (((x) & (0x7 << 4)) >> 4)
|
|
|
|
#define FMCS_SET(x) ((x) << 4)
|
|
|
|
#define AUTOSSR (1 << 3)
|
|
|
|
#define PILTCA (1 << 2)
|
|
|
|
#define SD_PM (1 << 1)
|
|
|
|
#define ST_M (1 << 0)
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define AUDIO_CTRL1 (0x09 + BLK2_START)
|
|
|
|
#define TONE_LVL(x) ((x) & (0xf << 4))
|
|
|
|
#define TONE_LVL_GET(x) (((x) & (0xf << 4)) >> 4)
|
|
|
|
#define TONE_LVL_SET(x) ((x) << 4)
|
|
|
|
#define VOL_LVL(x) ((x) & 0xf)
|
|
|
|
#define VOL_LVL_GET(x) ((x) & 0xf)
|
2013-03-24 15:03:31 +00:00
|
|
|
#define VOL_LVL_SET(x) ((x) << 0)
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define AUDIO_CTRL2 (0x0a + BLK2_START)
|
|
|
|
#define BASS_PP (1 << 0)
|
|
|
|
#define BASS_P (1 << 1) /* BASS_P, BASS_N are mutually-exclusive */
|
|
|
|
#define BASS_N (1 << 2)
|
|
|
|
#define TREB_P (1 << 3) /* TREB_P, TREB_N are mutually-exclusive */
|
|
|
|
#define TREB_N (1 << 4)
|
|
|
|
#define DEEMP (1 << 5)
|
|
|
|
#define BPFREQ(x) ((x) & (0x3 << 6))
|
|
|
|
#define BPFREQ_2_0K (0x0 << 6)
|
|
|
|
#define BPFREQ_1_0K (0x1 << 6)
|
|
|
|
#define BPFREQ_0_5K (0x2 << 6)
|
|
|
|
#define BPFREQ_HIGH (0x3 << 6)
|
|
|
|
|
|
|
|
/* W */
|
|
|
|
#define PW_SCTRL (0x0b + BLK2_START)
|
|
|
|
#define SS_CTRL(x) ((x) & (0x7 << 5))
|
|
|
|
#define SS_CTRL_GET(x) (((x) & (0x7 << 5)) >> 5)
|
|
|
|
#define SS_CTRL_SET(x) ((x) << 5)
|
|
|
|
#define SM_CTRL(x) ((x) & (0x7 << 2))
|
|
|
|
#define SM_CTRL_GET(x) (((x) & (0x7 << 2)) >> 2)
|
|
|
|
#define SM_CTRL_SET(x) ((x) << 2)
|
|
|
|
#define PW_HPA (1 << 1) /* LV24002 only */
|
|
|
|
#define PW_RAD (1 << 0)
|
|
|
|
|
|
|
|
/* shadow for writeable registers */
|
|
|
|
#define TUNER_POWERED (1 << 0)
|
|
|
|
#define TUNER_PRESENT (1 << 1)
|
|
|
|
#define TUNER_AWAKE (1 << 2)
|
|
|
|
#define TUNER_PRESENCE_CHECKED (1 << 3)
|
|
|
|
static unsigned tuner_status = 0;
|
|
|
|
|
2007-07-14 11:20:31 +00:00
|
|
|
static unsigned char lv24020lp_regs[0x1c];
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
static const int sw_osc_low = 10; /* 30; */
|
|
|
|
static const int sw_osc_high = 240; /* 200; */
|
|
|
|
static const int sw_cap_low = 0;
|
|
|
|
static const int sw_cap_high = 191;
|
|
|
|
|
|
|
|
/* linear coefficients used for tuning */
|
|
|
|
static int coef_00, coef_01, coef_10, coef_11;
|
|
|
|
|
|
|
|
/* DAC control register set values */
|
2008-04-20 10:24:15 +00:00
|
|
|
static int if_set, sd_set;
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
static inline bool tuner_awake(void)
|
|
|
|
{
|
|
|
|
return (tuner_status & TUNER_AWAKE) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send a byte to the tuner - expects write mode to be current */
|
2007-07-14 11:20:31 +00:00
|
|
|
static void lv24020lp_send_byte(unsigned int byte)
|
2007-06-06 19:23:48 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
2010-06-22 04:00:34 +00:00
|
|
|
TUNER_GPIO_OUTPUT_VAL_CLEAR(1 << FM_CLOCK_PIN);
|
|
|
|
|
2008-06-07 19:23:57 +00:00
|
|
|
|
|
|
|
if (byte & 1)
|
2010-06-22 04:00:34 +00:00
|
|
|
TUNER_GPIO_OUTPUT_VAL_SET(1 << FM_DATA_PIN);
|
2008-06-07 19:23:57 +00:00
|
|
|
else
|
2010-06-22 04:00:34 +00:00
|
|
|
TUNER_GPIO_OUTPUT_VAL_CLEAR(1 << FM_DATA_PIN);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
2007-12-06 04:03:26 +00:00
|
|
|
udelay(FM_CLK_DELAY);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
2010-06-22 04:00:34 +00:00
|
|
|
TUNER_GPIO_OUTPUT_VAL_SET(1 << FM_CLOCK_PIN);
|
2007-06-06 19:23:48 +00:00
|
|
|
udelay(FM_CLK_DELAY);
|
|
|
|
|
|
|
|
byte >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* end a write cycle on the tuner */
|
2007-07-14 11:20:31 +00:00
|
|
|
static void lv24020lp_end_write(void)
|
2007-06-06 19:23:48 +00:00
|
|
|
{
|
|
|
|
/* switch back to read mode */
|
2010-06-22 04:00:34 +00:00
|
|
|
TUNER_GPIO_OUTPUT_EN_CLEAR(1 << FM_DATA_PIN);
|
|
|
|
TUNER_GPIO_OUTPUT_VAL_CLEAR(1 << FM_NRW_PIN);
|
2007-12-06 04:03:26 +00:00
|
|
|
udelay(FM_CLK_DELAY);
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* prepare a write cycle on the tuner */
|
2007-07-14 11:20:31 +00:00
|
|
|
static unsigned int lv24020lp_begin_write(unsigned int address)
|
2007-06-06 19:23:48 +00:00
|
|
|
{
|
|
|
|
/* Get register's block, translate address */
|
|
|
|
unsigned int blk = (address >= BLK2_START) ?
|
|
|
|
(address -= BLK2_START, BLK2) : BLK1;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Prepare 3-wire bus pins for write cycle */
|
2010-06-22 04:00:34 +00:00
|
|
|
TUNER_GPIO_OUTPUT_VAL_SET(1 << FM_NRW_PIN);
|
|
|
|
TUNER_GPIO_OUTPUT_EN_SET(1 << FM_DATA_PIN);
|
2007-06-06 19:23:48 +00:00
|
|
|
udelay(FM_CLK_DELAY);
|
|
|
|
|
|
|
|
/* current block == register block? */
|
2007-07-14 11:20:31 +00:00
|
|
|
if (blk == lv24020lp_regs[BLK_SEL])
|
2007-06-06 19:23:48 +00:00
|
|
|
return address;
|
|
|
|
|
|
|
|
/* switch block */
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_regs[BLK_SEL] = blk;
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
/* data first */
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_send_byte(blk);
|
2007-06-06 19:23:48 +00:00
|
|
|
/* then address */
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_send_byte(BLK_SEL);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_end_write();
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write a byte to a tuner register */
|
2007-07-14 11:20:31 +00:00
|
|
|
static void lv24020lp_write(unsigned int address, unsigned int data)
|
2007-06-06 19:23:48 +00:00
|
|
|
{
|
|
|
|
/* shadow logical values but do logical=>physical remappings on some
|
|
|
|
registers' data. */
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_regs[address] = data;
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
switch (address)
|
|
|
|
{
|
|
|
|
case FM_OSC:
|
|
|
|
/* L: 000..255
|
|
|
|
* P: 255..000 */
|
|
|
|
data = 255 - data;
|
|
|
|
break;
|
|
|
|
case FM_CAP:
|
|
|
|
/* L: 000..063, 064..191
|
|
|
|
* P: 255..192, 127..000 */
|
|
|
|
data = ((data < 64) ? 255 : (255 - 64)) - data;
|
|
|
|
break;
|
|
|
|
case RADIO_CTRL1:
|
|
|
|
/* L: data
|
|
|
|
* P: data | always "1" bits */
|
|
|
|
data |= (1 << 4) | (1 << 1) | (1 << 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-07-14 22:00:50 +00:00
|
|
|
/* Check if interface is turned on */
|
|
|
|
if (!(tuner_status & TUNER_POWERED))
|
|
|
|
return;
|
|
|
|
|
2007-07-14 11:20:31 +00:00
|
|
|
address = lv24020lp_begin_write(address);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
/* data first */
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_send_byte(data);
|
2007-06-06 19:23:48 +00:00
|
|
|
/* then address */
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_send_byte(address);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_end_write();
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* helpers to set/clear register bits */
|
2007-12-06 04:03:26 +00:00
|
|
|
static void lv24020lp_write_set(unsigned int address, unsigned int bits)
|
2007-06-06 19:23:48 +00:00
|
|
|
{
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_write(address, lv24020lp_regs[address] | bits);
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
2007-12-06 04:03:26 +00:00
|
|
|
static void lv24020lp_write_clear(unsigned int address, unsigned int bits)
|
2007-06-06 19:23:48 +00:00
|
|
|
{
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write(address, lv24020lp_regs[address] & ~bits);
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read a byte from a tuner register */
|
2007-07-14 11:20:31 +00:00
|
|
|
static unsigned int lv24020lp_read(unsigned int address)
|
2007-06-06 19:23:48 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned int toread;
|
|
|
|
|
2007-07-14 22:00:50 +00:00
|
|
|
/* Check if interface is turned on */
|
|
|
|
if (!(tuner_status & TUNER_POWERED))
|
|
|
|
return 0;
|
|
|
|
|
2007-07-14 11:20:31 +00:00
|
|
|
address = lv24020lp_begin_write(address);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
/* address */
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_send_byte(address);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_end_write();
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
/* data */
|
|
|
|
toread = 0;
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
2010-06-22 04:00:34 +00:00
|
|
|
TUNER_GPIO_OUTPUT_VAL_CLEAR(1 << FM_CLOCK_PIN);
|
2007-06-06 19:23:48 +00:00
|
|
|
udelay(FM_CLK_DELAY);
|
|
|
|
|
2009-01-22 22:05:04 +00:00
|
|
|
if (TUNER_GPIO_INPUT_VAL & (1 << FM_DATA_PIN))
|
2008-06-07 19:23:57 +00:00
|
|
|
toread |= (1 << i);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
2010-06-22 04:00:34 +00:00
|
|
|
TUNER_GPIO_OUTPUT_VAL_SET(1 << FM_CLOCK_PIN);
|
2007-12-06 04:03:26 +00:00
|
|
|
udelay(FM_CLK_DELAY);
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
2008-06-07 19:23:57 +00:00
|
|
|
return toread;
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* enables auto frequency centering */
|
|
|
|
static void enable_afc(bool enabled)
|
|
|
|
{
|
2007-07-14 11:20:31 +00:00
|
|
|
unsigned int radio_ctrl1 = lv24020lp_regs[RADIO_CTRL1];
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
radio_ctrl1 &= ~RST_AFC;
|
|
|
|
radio_ctrl1 |= EN_AFC;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
radio_ctrl1 |= RST_AFC;
|
|
|
|
radio_ctrl1 &= ~EN_AFC;
|
|
|
|
}
|
|
|
|
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_write(RADIO_CTRL1, radio_ctrl1);
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int calculate_coef(unsigned fkhz)
|
|
|
|
{
|
|
|
|
/* Overflow below 66000kHz --
|
|
|
|
My tuner tunes down to a min of ~72600kHz but datasheet mentions
|
|
|
|
66000kHz as the minimum. ?? Perhaps 76000kHz was intended? */
|
|
|
|
return fkhz < 66000 ?
|
|
|
|
0x7fffffff : 0x81d1a47efc5cb700ull / ((uint64_t)fkhz*fkhz);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int interpolate_x(int expected_y, int x1, int x2, int y1, int y2)
|
|
|
|
{
|
|
|
|
return y1 == y2 ?
|
|
|
|
0 : (int64_t)(expected_y - y1)*(x2 - x1) / (y2 - y1) + x1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int interpolate_y(int expected_x, int x1, int x2, int y1, int y2)
|
|
|
|
{
|
|
|
|
return x1 == x2 ?
|
|
|
|
0 : (int64_t)(expected_x - x1)*(y2 - y1) / (x2 - x1) + y1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this performs measurements of IF, FM and Stereo frequencies
|
|
|
|
* Input can be: MSS_FM, MSS_IF, MSS_SD */
|
|
|
|
static int tuner_measure(unsigned char type, int scale, int duration)
|
|
|
|
{
|
|
|
|
int64_t finval;
|
|
|
|
|
|
|
|
/* enable measuring */
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_set(MSRC_SEL, type);
|
|
|
|
lv24020lp_write_clear(CNT_CTRL, CNT_SEL);
|
|
|
|
lv24020lp_write_set(RADIO_CTRL1, EN_MEAS);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
/* reset counter */
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_set(CNT_CTRL, CNT1_CLR);
|
|
|
|
lv24020lp_write_clear(CNT_CTRL, CNT1_CLR);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
/* start counter, delay for specified time and stop it */
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_set(CNT_CTRL, CNT_EN);
|
2010-06-22 07:27:34 +00:00
|
|
|
|
|
|
|
#ifdef CPU_PP
|
|
|
|
/* obtain actual duration, including interrupts that occurred and
|
|
|
|
* the time to write the counter stop */
|
|
|
|
long usec = USEC_TIMER;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
udelay(duration*1000);
|
|
|
|
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_clear(CNT_CTRL, CNT_EN);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
2010-06-22 07:27:34 +00:00
|
|
|
#ifdef CPU_PP
|
|
|
|
duration = (USEC_TIMER - usec) / 1000;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* This function takes a loooong time and other stuff needs
|
|
|
|
running by now */
|
|
|
|
yield();
|
|
|
|
|
2007-06-06 19:23:48 +00:00
|
|
|
/* read tick count */
|
2007-07-14 11:20:31 +00:00
|
|
|
finval = (lv24020lp_read(CNT_H) << 8) | lv24020lp_read(CNT_L);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
/* restore measure mode */
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_clear(RADIO_CTRL1, EN_MEAS);
|
|
|
|
lv24020lp_write_clear(MSRC_SEL, type);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
/* convert value */
|
|
|
|
if (type == MSS_FM)
|
|
|
|
finval = scale*finval*256 / duration;
|
|
|
|
else
|
|
|
|
finval = scale*finval / duration;
|
|
|
|
|
|
|
|
return (int)finval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the FM oscillator frequency */
|
2007-07-14 22:08:20 +00:00
|
|
|
static void set_frequency(int freq)
|
2007-06-06 19:23:48 +00:00
|
|
|
{
|
|
|
|
int coef, cap_value, osc_value;
|
|
|
|
int f1, f2, x1, x2;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
TUNER_LOG_OPEN();
|
|
|
|
|
|
|
|
TUNER_LOG("set_frequency(%d)\n", freq);
|
|
|
|
|
|
|
|
enable_afc(false);
|
|
|
|
|
2010-06-22 07:27:34 +00:00
|
|
|
/* For the LV2400x, the tuned frequency is the sum of the displayed
|
|
|
|
* frequency and the preset IF frequency, in formula:
|
|
|
|
* Tuned FM frequency = displayed frequency + preset IF frequency
|
|
|
|
*
|
|
|
|
* For example: when the IF frequency of LV2400x is preset at 110 kHz,
|
|
|
|
* it must be tuned at 88.51 MHz to receive the radio station at 88.4 MHz.
|
2013-03-24 15:03:31 +00:00
|
|
|
* -- AN2400S04@ \96 V0.4
|
2010-06-22 07:27:34 +00:00
|
|
|
*/
|
|
|
|
freq += if_set;
|
|
|
|
|
2007-06-06 19:23:48 +00:00
|
|
|
/* MHz -> kHz */
|
|
|
|
freq /= 1000;
|
|
|
|
|
|
|
|
TUNER_LOG("Select cap:\n");
|
|
|
|
|
|
|
|
coef = calculate_coef(freq);
|
|
|
|
cap_value = interpolate_x(coef, sw_cap_low, sw_cap_high,
|
|
|
|
coef_00, coef_01);
|
|
|
|
|
|
|
|
osc_value = sw_osc_low;
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_write(FM_OSC, osc_value);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
/* Just in case - don't go into infinite loop */
|
|
|
|
for (count = 0; count < 30; count++)
|
|
|
|
{
|
|
|
|
int y0 = interpolate_y(cap_value, sw_cap_low, sw_cap_high,
|
|
|
|
coef_00, coef_01);
|
|
|
|
int y1 = interpolate_y(cap_value, sw_cap_low, sw_cap_high,
|
|
|
|
coef_10, coef_11);
|
|
|
|
int coef_fcur, cap_new, coef_cor, range;
|
|
|
|
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_write(FM_CAP, cap_value);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
range = y1 - y0;
|
|
|
|
f1 = tuner_measure(MSS_FM, 1, 16);
|
|
|
|
coef_fcur = calculate_coef(f1);
|
|
|
|
coef_cor = calculate_coef((f1*1000 + 32*256) / 1000);
|
|
|
|
y0 = coef_cor;
|
|
|
|
y1 = y0 + range;
|
|
|
|
|
|
|
|
TUNER_LOG("%d %d %d %d %d %d %d %d\n",
|
|
|
|
f1, cap_value, coef, coef_fcur, coef_cor, y0, y1, range);
|
|
|
|
|
|
|
|
if (coef >= y0 && coef <= y1)
|
|
|
|
{
|
|
|
|
osc_value = interpolate_x(coef, sw_osc_low, sw_osc_high,
|
|
|
|
y0, y1);
|
|
|
|
|
|
|
|
if (osc_value >= sw_osc_low && osc_value <= sw_osc_high)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cap_new = interpolate_x(coef, cap_value, sw_cap_high,
|
|
|
|
coef_fcur, coef_01);
|
|
|
|
|
|
|
|
if (cap_new == cap_value)
|
|
|
|
{
|
|
|
|
if (coef < coef_fcur)
|
|
|
|
cap_value++;
|
|
|
|
else
|
|
|
|
cap_value--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cap_value = cap_new;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TUNER_LOG("osc_value: %d\n", osc_value);
|
|
|
|
|
|
|
|
TUNER_LOG("Tune:\n");
|
|
|
|
|
|
|
|
x1 = sw_osc_low, x2 = sw_osc_high;
|
|
|
|
/* FM_OSC already at SW_OSC low and f1 is already the measured
|
|
|
|
frequency */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int x2_new;
|
|
|
|
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_write(FM_OSC, x2);
|
2007-06-06 19:23:48 +00:00
|
|
|
f2 = tuner_measure(MSS_FM, 1, 16);
|
|
|
|
|
|
|
|
if (abs(f2 - freq) <= 16)
|
|
|
|
{
|
|
|
|
TUNER_LOG("%d %d %d %d\n", f1, f2, x1, x2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
x2_new = interpolate_x(freq, x1, x2, f1, f2);
|
|
|
|
|
|
|
|
x1 = x2, f1 = f2, x2 = x2_new;
|
|
|
|
TUNER_LOG("%d %d %d %d\n", f1, f2, x1, x2);
|
|
|
|
}
|
|
|
|
while (x2 != 0);
|
|
|
|
|
|
|
|
if (x2 == 0)
|
|
|
|
{
|
|
|
|
/* May still be close enough */
|
|
|
|
TUNER_LOG("tuning failed - diff: %d\n", f2 - freq);
|
|
|
|
}
|
|
|
|
|
|
|
|
enable_afc(true);
|
|
|
|
|
|
|
|
TUNER_LOG("\n");
|
|
|
|
|
|
|
|
TUNER_LOG_SYNC();
|
|
|
|
}
|
|
|
|
|
2007-12-06 04:03:26 +00:00
|
|
|
#define TOO_SMALL (1 << 0)
|
|
|
|
#define TOO_BIG (1 << 1)
|
|
|
|
#define APPROACH_UP_1 (1 << 2)
|
|
|
|
#define APPROACH_DOWN_1 (1 << 3)
|
|
|
|
|
2007-06-06 19:23:48 +00:00
|
|
|
static void fine_step_tune(int (*setcmp)(int regval), int regval, int step)
|
|
|
|
{
|
|
|
|
/* Registers are not always stable, timeout if best fit not found soon
|
|
|
|
enough */
|
|
|
|
unsigned long abort = current_tick + HZ*2;
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
while (TIME_BEFORE(current_tick, abort))
|
|
|
|
{
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
regval = regval + step;
|
|
|
|
|
|
|
|
cmp = setcmp(regval);
|
|
|
|
|
|
|
|
if (cmp == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
step = abs(step);
|
|
|
|
|
|
|
|
if (cmp < 0)
|
|
|
|
{
|
2007-12-06 04:03:26 +00:00
|
|
|
flags |= TOO_SMALL;
|
2007-06-06 19:23:48 +00:00
|
|
|
if (step == 1)
|
2007-12-06 04:03:26 +00:00
|
|
|
flags |= APPROACH_UP_1;
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
step = -step;
|
2007-12-06 04:03:26 +00:00
|
|
|
flags |= TOO_BIG;
|
2007-06-06 19:23:48 +00:00
|
|
|
if (step == -1)
|
2007-12-06 04:03:26 +00:00
|
|
|
step |= APPROACH_DOWN_1;
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
2007-12-06 04:03:26 +00:00
|
|
|
if ((flags & APPROACH_UP_1) && (flags & APPROACH_DOWN_1))
|
|
|
|
break; /* approached with step=1: best fit value found */
|
2007-06-06 19:23:48 +00:00
|
|
|
|
2007-12-06 04:03:26 +00:00
|
|
|
if ((flags & TOO_SMALL) && (flags & TOO_BIG))
|
2007-06-06 19:23:48 +00:00
|
|
|
{
|
|
|
|
step /= 2;
|
|
|
|
if (step == 0)
|
|
|
|
step = 1;
|
2007-12-06 04:03:26 +00:00
|
|
|
flags &= ~(TOO_SMALL | TOO_BIG);
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int if_setcmp(int regval)
|
|
|
|
{
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_write(IF_OSC, regval);
|
|
|
|
lv24020lp_write(IF_CENTER, regval);
|
|
|
|
lv24020lp_write(IF_BW, 65*regval/100);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
if_set = tuner_measure(MSS_IF, 1000, 32);
|
|
|
|
|
|
|
|
/* This register is bounces around by a few hundred Hz and doesn't seem
|
|
|
|
to be precisely tuneable. Just do 110000 +/- 500 since it's not very
|
|
|
|
critical it seems. */
|
2007-07-14 22:00:50 +00:00
|
|
|
if (abs(if_set - 110000) <= 500)
|
2007-06-06 19:23:48 +00:00
|
|
|
return 0;
|
|
|
|
|
2007-07-14 22:00:50 +00:00
|
|
|
return if_set < 110000 ? -1 : 1;
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sd_setcmp(int regval)
|
|
|
|
{
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_write(SD_OSC, regval);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
sd_set = tuner_measure(MSS_SD, 1000, 32);
|
|
|
|
|
|
|
|
if (abs(sd_set - 38300) <= 31)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return sd_set < 38300 ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
2007-07-14 11:20:31 +00:00
|
|
|
static void set_sleep(bool sleep)
|
2007-06-06 19:23:48 +00:00
|
|
|
{
|
2012-04-02 20:11:24 +00:00
|
|
|
tuner_power(!sleep);
|
2007-06-06 19:23:48 +00:00
|
|
|
if (sleep || tuner_awake())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((tuner_status & (TUNER_PRESENT | TUNER_POWERED)) !=
|
|
|
|
(TUNER_PRESENT | TUNER_POWERED))
|
|
|
|
return;
|
|
|
|
|
|
|
|
enable_afc(false);
|
|
|
|
|
|
|
|
/* 2. Calibrate the IF frequency at 110 kHz: */
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_clear(RADIO_CTRL2, IF_PM_L);
|
2007-06-06 19:23:48 +00:00
|
|
|
fine_step_tune(if_setcmp, 0x80, 8);
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_set(RADIO_CTRL2, IF_PM_L);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
/* 3. Calibrate the stereo decoder clock at 38.3 kHz: */
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_set(STEREO_CTRL, SD_PM);
|
2007-06-06 19:23:48 +00:00
|
|
|
fine_step_tune(sd_setcmp, 0x80, 8);
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_clear(STEREO_CTRL, SD_PM);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
/* calculate FM tuning coefficients */
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_write(FM_CAP, sw_cap_low);
|
|
|
|
lv24020lp_write(FM_OSC, sw_osc_low);
|
2007-06-06 19:23:48 +00:00
|
|
|
coef_00 = calculate_coef(tuner_measure(MSS_FM, 1, 64));
|
|
|
|
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_write(FM_CAP, sw_cap_high);
|
2007-06-06 19:23:48 +00:00
|
|
|
coef_01 = calculate_coef(tuner_measure(MSS_FM, 1, 64));
|
|
|
|
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_write(FM_CAP, sw_cap_low);
|
|
|
|
lv24020lp_write(FM_OSC, sw_osc_high);
|
2007-06-06 19:23:48 +00:00
|
|
|
coef_10 = calculate_coef(tuner_measure(MSS_FM, 1, 64));
|
|
|
|
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_write(FM_CAP, sw_cap_high);
|
2007-06-06 19:23:48 +00:00
|
|
|
coef_11 = calculate_coef(tuner_measure(MSS_FM, 1, 64));
|
|
|
|
|
|
|
|
/* set various audio level settings */
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_write(AUDIO_CTRL1, TONE_LVL_SET(0) | VOL_LVL_SET(0));
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_set(RADIO_CTRL2, AGCSP);
|
|
|
|
lv24020lp_write_set(RADIO_CTRL3, VOLSH);
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_write(STEREO_CTRL, FMCS_SET(7) | AUTOSSR);
|
|
|
|
lv24020lp_write(PW_SCTRL, SS_CTRL_SET(3) | SM_CTRL_SET(1) |
|
|
|
|
PW_RAD);
|
2007-07-14 22:00:50 +00:00
|
|
|
|
|
|
|
tuner_status |= TUNER_AWAKE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lp24020lp_tuned(void)
|
|
|
|
{
|
|
|
|
return RSS_FS(lv24020lp_read(RADIO_STAT)) < 0x1f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lv24020lp_debug_info(int setting)
|
|
|
|
{
|
|
|
|
int val = -1;
|
|
|
|
|
|
|
|
if (setting >= LV24020LP_DEBUG_FIRST && setting <= LV24020LP_DEBUG_LAST)
|
|
|
|
{
|
|
|
|
val = 0;
|
|
|
|
|
|
|
|
if (tuner_awake())
|
|
|
|
{
|
|
|
|
switch (setting)
|
|
|
|
{
|
|
|
|
/* tuner-specific debug info */
|
|
|
|
case LV24020LP_CTRL_STAT:
|
|
|
|
val = lv24020lp_read(CTRL_STAT);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LV24020LP_REG_STAT:
|
|
|
|
val = lv24020lp_read(RADIO_STAT);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LV24020LP_MSS_FM:
|
|
|
|
val = tuner_measure(MSS_FM, 1, 16);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LV24020LP_MSS_IF:
|
|
|
|
val = tuner_measure(MSS_IF, 1000, 16);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LV24020LP_MSS_SD:
|
|
|
|
val = tuner_measure(MSS_SD, 1000, 16);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LV24020LP_IF_SET:
|
|
|
|
val = if_set;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LV24020LP_SD_SET:
|
|
|
|
val = sd_set;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Public interfaces **/
|
2007-07-14 22:00:50 +00:00
|
|
|
void lv24020lp_init(void)
|
|
|
|
{
|
|
|
|
mutex_init(&tuner_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void lv24020lp_lock(void)
|
|
|
|
{
|
|
|
|
mutex_lock(&tuner_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void lv24020lp_unlock(void)
|
|
|
|
{
|
|
|
|
mutex_unlock(&tuner_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function expects the driver to be locked externally */
|
2007-07-14 11:20:31 +00:00
|
|
|
void lv24020lp_power(bool status)
|
2007-06-06 19:23:48 +00:00
|
|
|
{
|
|
|
|
static const unsigned char tuner_defaults[][2] =
|
|
|
|
{
|
|
|
|
/* Block 1 writeable registers */
|
|
|
|
{ MSRC_SEL, AFC_LVL },
|
|
|
|
{ FM_OSC, 0x80 },
|
|
|
|
{ SD_OSC, 0x80 },
|
|
|
|
{ IF_OSC, 0x80 },
|
|
|
|
{ CNT_CTRL, CNT1_CLR | SWP_CNT_L },
|
|
|
|
{ IRQ_MSK, 0x00 }, /* IRQ_LVL -> Low to High */
|
|
|
|
{ FM_CAP, 0x80 },
|
|
|
|
/* { IRQ_OUT, 0x00 }, No action on this register (skip) */
|
|
|
|
/* Block 2 writeable registers */
|
|
|
|
{ RADIO_CTRL1, EN_AFC },
|
|
|
|
{ IF_CENTER, 0x80 },
|
|
|
|
{ IF_BW, 65*0x80 / 100 }, /* 65% of IF_OSC */
|
|
|
|
{ RADIO_CTRL2, IF_PM_L },
|
|
|
|
{ RADIO_CTRL3, AGC_SLVL | SE_FM },
|
|
|
|
{ STEREO_CTRL, FMCS_SET(4) | AUTOSSR },
|
|
|
|
{ AUDIO_CTRL1, TONE_LVL_SET(7) | VOL_LVL_SET(7) },
|
|
|
|
{ AUDIO_CTRL2, BPFREQ_HIGH }, /* deemphasis 50us */
|
|
|
|
{ PW_SCTRL, SS_CTRL_SET(3) | SM_CTRL_SET(3) | PW_RAD },
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
if (status)
|
|
|
|
{
|
2007-07-14 22:00:50 +00:00
|
|
|
tuner_status |= (TUNER_PRESENCE_CHECKED | TUNER_POWERED);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
/* if tuner is present, CHIP ID is 0x09 */
|
2007-07-14 11:20:31 +00:00
|
|
|
if (lv24020lp_read(CHIP_ID) == 0x09)
|
2007-06-06 19:23:48 +00:00
|
|
|
{
|
|
|
|
tuner_status |= TUNER_PRESENT;
|
|
|
|
|
|
|
|
/* After power-up, the LV2400x needs to be initialized as
|
|
|
|
follows: */
|
|
|
|
|
|
|
|
/* 1. Write default values to the registers: */
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_regs[BLK_SEL] = 0; /* Force a switch on the first */
|
2007-06-06 19:23:48 +00:00
|
|
|
for (i = 0; i < ARRAYLEN(tuner_defaults); i++)
|
2007-07-14 11:20:31 +00:00
|
|
|
lv24020lp_write(tuner_defaults[i][0], tuner_defaults[i][1]);
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
/* Complete the startup calibration if the tuner is woken */
|
2007-07-14 22:00:50 +00:00
|
|
|
sleep(HZ/10);
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-07-14 11:20:31 +00:00
|
|
|
/* Power off */
|
2007-06-06 19:23:48 +00:00
|
|
|
if (tuner_status & TUNER_PRESENT)
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_clear(PW_SCTRL, PW_RAD);
|
2007-12-05 02:38:35 +00:00
|
|
|
|
|
|
|
tuner_status &= ~(TUNER_POWERED | TUNER_AWAKE);
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-14 11:20:31 +00:00
|
|
|
int lv24020lp_set(int setting, int value)
|
2007-06-06 19:23:48 +00:00
|
|
|
{
|
|
|
|
int val = 1;
|
|
|
|
|
2007-07-14 22:00:50 +00:00
|
|
|
mutex_lock(&tuner_mtx);
|
|
|
|
|
2007-06-06 19:23:48 +00:00
|
|
|
switch(setting)
|
|
|
|
{
|
|
|
|
case RADIO_SLEEP:
|
2007-07-14 11:20:31 +00:00
|
|
|
set_sleep(value);
|
2007-06-06 19:23:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RADIO_FREQUENCY:
|
2007-07-14 11:20:31 +00:00
|
|
|
set_frequency(value);
|
2007-06-06 19:23:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RADIO_SCAN_FREQUENCY:
|
|
|
|
/* TODO: really implement this */
|
2007-07-14 11:20:31 +00:00
|
|
|
set_frequency(value);
|
2007-07-14 22:00:50 +00:00
|
|
|
val = lp24020lp_tuned();
|
2007-06-06 19:23:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RADIO_MUTE:
|
|
|
|
if (value)
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_clear(RADIO_CTRL3, AMUTE_L);
|
2007-06-06 19:23:48 +00:00
|
|
|
else
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_set(RADIO_CTRL3, AMUTE_L);
|
2007-06-06 19:23:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RADIO_REGION:
|
2010-07-26 20:15:16 +00:00
|
|
|
{
|
|
|
|
const struct fm_region_data *rd = &fm_region_data[value];
|
|
|
|
if (rd->deemphasis == 75)
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_set(AUDIO_CTRL2, DEEMP);
|
2007-07-14 11:20:31 +00:00
|
|
|
else
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_clear(AUDIO_CTRL2, DEEMP);
|
2007-06-06 19:23:48 +00:00
|
|
|
break;
|
2010-07-26 20:15:16 +00:00
|
|
|
}
|
2007-06-06 19:23:48 +00:00
|
|
|
|
|
|
|
case RADIO_FORCE_MONO:
|
|
|
|
if (value)
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_set(STEREO_CTRL, ST_M);
|
2007-06-06 19:23:48 +00:00
|
|
|
else
|
2007-12-06 04:03:26 +00:00
|
|
|
lv24020lp_write_clear(STEREO_CTRL, ST_M);
|
2007-06-06 19:23:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2007-07-14 22:00:50 +00:00
|
|
|
value = -1;
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
2007-07-14 22:00:50 +00:00
|
|
|
mutex_unlock(&tuner_mtx);
|
|
|
|
|
2007-06-06 19:23:48 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2007-07-14 11:20:31 +00:00
|
|
|
int lv24020lp_get(int setting)
|
2007-06-06 19:23:48 +00:00
|
|
|
{
|
|
|
|
int val = -1;
|
2010-11-11 21:13:29 +00:00
|
|
|
const unsigned char fst[7] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f};
|
|
|
|
unsigned char fst_ndx, fs;
|
2007-06-06 19:23:48 +00:00
|
|
|
|
2007-07-14 22:00:50 +00:00
|
|
|
mutex_lock(&tuner_mtx);
|
|
|
|
|
2007-06-06 19:23:48 +00:00
|
|
|
switch(setting)
|
|
|
|
{
|
|
|
|
case RADIO_TUNED:
|
|
|
|
/* TODO: really implement this */
|
2007-07-14 22:00:50 +00:00
|
|
|
val = lp24020lp_tuned();
|
2007-06-06 19:23:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RADIO_STEREO:
|
2007-07-14 11:20:31 +00:00
|
|
|
val = (lv24020lp_read(RADIO_STAT) & RSS_MS) != 0;
|
2007-06-06 19:23:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RADIO_PRESENT:
|
2007-07-14 11:20:31 +00:00
|
|
|
{
|
|
|
|
bool fmstatus = true;
|
|
|
|
|
|
|
|
if (!(tuner_status & TUNER_PRESENCE_CHECKED))
|
2007-11-05 07:01:32 +00:00
|
|
|
fmstatus = tuner_power(true);
|
2007-07-14 11:20:31 +00:00
|
|
|
|
2007-06-06 19:23:48 +00:00
|
|
|
val = (tuner_status & TUNER_PRESENT) != 0;
|
2007-07-14 11:20:31 +00:00
|
|
|
|
|
|
|
if (!fmstatus)
|
2007-11-05 07:01:32 +00:00
|
|
|
tuner_power(false);
|
2007-06-06 19:23:48 +00:00
|
|
|
break;
|
2007-07-14 11:20:31 +00:00
|
|
|
}
|
2007-06-06 19:23:48 +00:00
|
|
|
|
2010-11-11 21:13:29 +00:00
|
|
|
case RADIO_RSSI:
|
|
|
|
fs = RSS_FS(lv24020lp_read(RADIO_STAT));
|
|
|
|
for(fst_ndx=0; fst_ndx<7; fst_ndx++)
|
|
|
|
if(fs == fst[fst_ndx])
|
|
|
|
break;
|
|
|
|
val = 75 - 10*fst_ndx;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RADIO_RSSI_MIN:
|
|
|
|
val = RSSI_MIN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RADIO_RSSI_MAX:
|
|
|
|
val = RSSI_MAX;
|
|
|
|
break;
|
|
|
|
|
2007-07-14 22:00:50 +00:00
|
|
|
default:
|
|
|
|
val = lv24020lp_debug_info(setting);
|
2007-06-06 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
2007-07-14 22:00:50 +00:00
|
|
|
mutex_unlock(&tuner_mtx);
|
|
|
|
|
2007-06-06 19:23:48 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
#endif /* BOOTLOADER */
|