2008-12-08 19:32:05 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-12-16 22:58:47 +00:00
|
|
|
* Copyright (C) 2008 by Bertrik Sikken
|
2008-12-08 19:32:05 +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.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
This is the fmradio_i2c interface, used by the radio driver
|
|
|
|
to communicate with the radio tuner chip.
|
2009-03-03 18:00:17 +00:00
|
|
|
|
2008-12-08 19:32:05 +00:00
|
|
|
It is implemented using the generic i2c driver, which does "bit-banged"
|
|
|
|
I2C with a couple of GPIO pins.
|
|
|
|
*/
|
|
|
|
|
2011-12-17 20:24:19 +00:00
|
|
|
#include "config.h"
|
2008-12-08 19:32:05 +00:00
|
|
|
#include "as3525.h"
|
2010-06-21 21:41:07 +00:00
|
|
|
#include "system.h"
|
2011-12-17 20:24:19 +00:00
|
|
|
#include "tuner.h"
|
2008-12-08 19:32:05 +00:00
|
|
|
#include "generic_i2c.h"
|
|
|
|
#include "fmradio_i2c.h"
|
2011-12-17 20:24:19 +00:00
|
|
|
#include "thread.h"
|
|
|
|
#include "rds.h"
|
2008-12-08 19:32:05 +00:00
|
|
|
|
2009-04-26 21:28:34 +00:00
|
|
|
#if defined(SANSA_CLIP) || defined(SANSA_C200V2)
|
2010-05-05 02:27:18 +00:00
|
|
|
#define I2C_SCL_GPIO(x) GPIOB_PIN(x)
|
|
|
|
#define I2C_SDA_GPIO(x) GPIOB_PIN(x)
|
|
|
|
#define I2C_SCL_GPIO_DIR GPIOB_DIR
|
|
|
|
#define I2C_SDA_GPIO_DIR GPIOB_DIR
|
2008-12-08 19:32:05 +00:00
|
|
|
#define I2C_SCL_PIN 4
|
|
|
|
#define I2C_SDA_PIN 5
|
|
|
|
|
2011-08-27 16:21:19 +00:00
|
|
|
#elif defined(SANSA_CLIPV2) || defined(SANSA_CLIPPLUS) \
|
|
|
|
|| defined(SANSA_CLIPZIP)
|
2010-05-05 02:27:18 +00:00
|
|
|
#define I2C_SCL_GPIO(x) GPIOB_PIN(x)
|
|
|
|
#define I2C_SDA_GPIO(x) GPIOB_PIN(x)
|
|
|
|
#define I2C_SCL_GPIO_DIR GPIOB_DIR
|
|
|
|
#define I2C_SDA_GPIO_DIR GPIOB_DIR
|
2009-12-31 19:15:20 +00:00
|
|
|
#define I2C_SCL_PIN 6
|
|
|
|
#define I2C_SDA_PIN 7
|
|
|
|
|
2008-12-09 17:21:16 +00:00
|
|
|
#elif defined(SANSA_M200V4)
|
2010-05-05 02:27:18 +00:00
|
|
|
#define I2C_SCL_GPIO(x) GPIOD_PIN(x)
|
|
|
|
#define I2C_SDA_GPIO(x) GPIOD_PIN(x)
|
|
|
|
#define I2C_SCL_GPIO_DIR GPIOD_DIR
|
|
|
|
#define I2C_SDA_GPIO_DIR GPIOD_DIR
|
2008-12-09 17:21:16 +00:00
|
|
|
#define I2C_SCL_PIN 7
|
|
|
|
#define I2C_SDA_PIN 6
|
|
|
|
|
2008-12-16 22:58:47 +00:00
|
|
|
#elif defined(SANSA_FUZE) || defined(SANSA_E200V2)
|
2010-05-05 02:27:18 +00:00
|
|
|
#define I2C_SCL_GPIO(x) GPIOA_PIN(x)
|
|
|
|
#define I2C_SDA_GPIO(x) GPIOA_PIN(x)
|
|
|
|
#define I2C_SCL_GPIO_DIR GPIOA_DIR
|
|
|
|
#define I2C_SDA_GPIO_DIR GPIOA_DIR
|
2008-12-16 22:58:47 +00:00
|
|
|
#define I2C_SCL_PIN 6
|
|
|
|
#define I2C_SDA_PIN 7
|
|
|
|
|
2010-05-05 04:06:44 +00:00
|
|
|
#elif defined(SANSA_FUZEV2)
|
|
|
|
#define I2C_SCL_GPIO(x) GPIOB_PIN(x)
|
|
|
|
#define I2C_SDA_GPIO(x) GPIOA_PIN(x)
|
|
|
|
#define I2C_SCL_GPIO_DIR GPIOB_DIR
|
|
|
|
#define I2C_SDA_GPIO_DIR GPIOA_DIR
|
|
|
|
#define I2C_SCL_PIN 1
|
|
|
|
#define I2C_SDA_PIN 0
|
|
|
|
|
2009-11-03 21:20:02 +00:00
|
|
|
#else
|
2008-12-09 17:21:16 +00:00
|
|
|
#error no FM I2C GPIOPIN defines
|
|
|
|
#endif
|
|
|
|
|
2009-03-03 18:00:17 +00:00
|
|
|
static int fm_i2c_bus;
|
|
|
|
|
2010-06-21 21:41:07 +00:00
|
|
|
static void fm_scl_dir(bool out)
|
2008-12-08 19:32:05 +00:00
|
|
|
{
|
2010-06-21 21:41:07 +00:00
|
|
|
if (out) {
|
|
|
|
I2C_SCL_GPIO_DIR |= 1 << I2C_SCL_PIN;
|
|
|
|
} else {
|
|
|
|
I2C_SCL_GPIO_DIR &= ~(1 << I2C_SCL_PIN);
|
|
|
|
}
|
2008-12-08 19:32:05 +00:00
|
|
|
}
|
|
|
|
|
2010-06-21 21:41:07 +00:00
|
|
|
static void fm_sda_dir(bool out)
|
2008-12-08 19:32:05 +00:00
|
|
|
{
|
2010-06-21 21:41:07 +00:00
|
|
|
if (out) {
|
|
|
|
I2C_SDA_GPIO_DIR |= 1 << I2C_SDA_PIN;
|
|
|
|
} else {
|
|
|
|
I2C_SDA_GPIO_DIR &= ~(1 << I2C_SDA_PIN);
|
|
|
|
}
|
2008-12-08 19:32:05 +00:00
|
|
|
}
|
|
|
|
|
2010-06-21 21:41:07 +00:00
|
|
|
static void fm_scl_out(bool level)
|
2008-12-08 19:32:05 +00:00
|
|
|
{
|
2010-06-21 21:41:07 +00:00
|
|
|
if (level) {
|
|
|
|
I2C_SCL_GPIO(I2C_SCL_PIN) = 1 << I2C_SCL_PIN;
|
|
|
|
} else {
|
|
|
|
I2C_SCL_GPIO(I2C_SCL_PIN) = 0;
|
|
|
|
}
|
2008-12-08 19:32:05 +00:00
|
|
|
}
|
|
|
|
|
2010-06-21 21:41:07 +00:00
|
|
|
static void fm_sda_out(bool level)
|
2008-12-08 19:32:05 +00:00
|
|
|
{
|
2010-06-21 21:41:07 +00:00
|
|
|
if (level) {
|
|
|
|
I2C_SDA_GPIO(I2C_SDA_PIN) = 1 << I2C_SDA_PIN;
|
|
|
|
} else {
|
|
|
|
I2C_SDA_GPIO(I2C_SDA_PIN) = 0;
|
|
|
|
}
|
2008-12-08 19:32:05 +00:00
|
|
|
}
|
|
|
|
|
2010-06-21 21:41:07 +00:00
|
|
|
static bool fm_scl_in(void)
|
2008-12-08 19:32:05 +00:00
|
|
|
{
|
2010-06-21 21:41:07 +00:00
|
|
|
return I2C_SCL_GPIO(I2C_SCL_PIN);
|
2008-12-08 19:32:05 +00:00
|
|
|
}
|
|
|
|
|
2010-06-21 21:41:07 +00:00
|
|
|
static bool fm_sda_in(void)
|
2008-12-08 19:32:05 +00:00
|
|
|
{
|
2010-05-05 02:27:18 +00:00
|
|
|
return I2C_SDA_GPIO(I2C_SDA_PIN);
|
2008-12-08 19:32:05 +00:00
|
|
|
}
|
|
|
|
|
2010-06-21 21:41:07 +00:00
|
|
|
static void fm_delay(int delay)
|
2008-12-08 19:32:05 +00:00
|
|
|
{
|
2010-06-21 21:41:07 +00:00
|
|
|
if (delay != 0) {
|
|
|
|
udelay(delay);
|
2008-12-08 19:32:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* interface towards the generic i2c driver */
|
2009-03-03 18:00:17 +00:00
|
|
|
static const struct i2c_interface fm_i2c_interface = {
|
2010-06-21 21:41:07 +00:00
|
|
|
.scl_out = fm_scl_out,
|
|
|
|
.scl_dir = fm_scl_dir,
|
|
|
|
.sda_out = fm_sda_out,
|
|
|
|
.sda_dir = fm_sda_dir,
|
|
|
|
.sda_in = fm_sda_in,
|
|
|
|
.scl_in = fm_scl_in,
|
|
|
|
.delay = fm_delay,
|
|
|
|
|
|
|
|
.delay_hd_sta = 1,
|
|
|
|
.delay_hd_dat = 0,
|
|
|
|
.delay_su_dat = 1,
|
|
|
|
.delay_su_sto = 1,
|
|
|
|
.delay_su_sta = 1,
|
|
|
|
.delay_thigh = 2
|
2008-12-08 19:32:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* initialise i2c for fmradio */
|
|
|
|
void fmradio_i2c_init(void)
|
|
|
|
{
|
2009-03-03 18:00:17 +00:00
|
|
|
fm_i2c_bus = i2c_add_node(&fm_i2c_interface);
|
2008-12-08 19:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int fmradio_i2c_write(unsigned char address, const unsigned char* buf, int count)
|
|
|
|
{
|
2010-05-05 04:06:44 +00:00
|
|
|
#ifdef SANSA_FUZEV2
|
2010-07-19 15:56:15 +00:00
|
|
|
bitclr32(&CCU_IO, 1<<12);
|
2010-05-05 04:06:44 +00:00
|
|
|
#endif
|
|
|
|
int ret = i2c_write_data(fm_i2c_bus, address, -1, buf, count);
|
|
|
|
#ifdef SANSA_FUZEV2
|
2010-07-19 15:56:15 +00:00
|
|
|
bitset32(&CCU_IO, 1<<12);
|
2010-05-05 04:06:44 +00:00
|
|
|
#endif
|
|
|
|
return ret;
|
2008-12-08 19:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int fmradio_i2c_read(unsigned char address, unsigned char* buf, int count)
|
|
|
|
{
|
2010-05-05 04:06:44 +00:00
|
|
|
#ifdef SANSA_FUZEV2
|
2010-07-19 15:56:15 +00:00
|
|
|
bitclr32(&CCU_IO, 1<<12);
|
2010-05-05 04:06:44 +00:00
|
|
|
#endif
|
|
|
|
int ret = i2c_read_data(fm_i2c_bus, address, -1, buf, count);
|
|
|
|
#ifdef SANSA_FUZEV2
|
2010-07-19 15:56:15 +00:00
|
|
|
bitset32(&CCU_IO, 1<<12);
|
2010-05-05 04:06:44 +00:00
|
|
|
#endif
|
|
|
|
return ret;
|
2008-12-08 19:32:05 +00:00
|
|
|
}
|
2011-12-17 20:24:19 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_RDS_CAP
|
|
|
|
/* Low-level RDS Support */
|
|
|
|
static struct semaphore rds_sema;
|
|
|
|
static uint32_t rds_stack[DEFAULT_STACK_SIZE/sizeof(uint32_t)];
|
|
|
|
|
|
|
|
/* RDS GPIO interrupt handler */
|
|
|
|
void tuner_isr(void)
|
|
|
|
{
|
|
|
|
/* read and clear the interrupt */
|
|
|
|
if (GPIOA_MIS & (1<<4)) {
|
|
|
|
semaphore_release(&rds_sema);
|
2012-01-08 22:29:25 +00:00
|
|
|
GPIOA_IC = (1<<4);
|
2011-12-17 20:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Captures RDS data and processes it */
|
|
|
|
static void NORETURN_ATTR rds_thread(void)
|
|
|
|
{
|
|
|
|
uint16_t rds_data[4];
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
semaphore_wait(&rds_sema, TIMEOUT_BLOCK);
|
|
|
|
if (si4700_rds_read_raw(rds_data) && rds_process(rds_data)) {
|
|
|
|
si4700_rds_set_event();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called with on=true after full radio power up, and with on=false before
|
|
|
|
powering down */
|
|
|
|
void si4700_rds_powerup(bool on)
|
|
|
|
{
|
|
|
|
GPIOA_IE &= ~(1<<4); /* disable GPIO interrupt */
|
|
|
|
|
|
|
|
if (on) {
|
|
|
|
GPIOA_DIR &= ~(1<<4); /* input */
|
|
|
|
GPIOA_IS &= ~(1<<4); /* edge detect */
|
|
|
|
GPIOA_IBE &= ~(1<<4); /* only one edge */
|
|
|
|
GPIOA_IEV &= ~(1<<4); /* falling edge */
|
|
|
|
GPIOA_IC = (1<<4); /* clear any pending interrupt */
|
|
|
|
GPIOA_IE |= (1<<4); /* enable GPIO interrupt */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* One-time RDS init at startup */
|
|
|
|
void si4700_rds_init(void)
|
|
|
|
{
|
|
|
|
semaphore_init(&rds_sema, 1, 0);
|
|
|
|
rds_init();
|
|
|
|
create_thread(rds_thread, rds_stack, sizeof(rds_stack), 0, "rds"
|
|
|
|
IF_PRIO(, PRIORITY_REALTIME) IF_COP(, CPU));
|
|
|
|
}
|
|
|
|
#endif /* HAVE_RDS_CAP */
|
|
|
|
|