summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/as3525/fmradio-i2c-as3525.c
blob: 6cdf0c7c7f747e5097c19dace51b7a30c2df155d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2008 by Bertrik Sikken
 *
 * 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.

    It is implemented using the generic i2c driver, which does "bit-banged"
    I2C with a couple of GPIO pins.
 */

#include "as3525.h"
#include "generic_i2c.h"
#include "fmradio_i2c.h"

#if     defined(SANSA_CLIP) || defined(SANSA_C200V2)
#define I2C_GPIO(x) GPIOB_PIN(x)
#define I2C_GPIO_DIR GPIOB_DIR
#define I2C_SCL_PIN 4
#define I2C_SDA_PIN 5

#elif     defined(SANSA_CLIPV2)
#define I2C_GPIO(x) GPIOB_PIN(x)
#define I2C_GPIO_DIR GPIOB_DIR
#define I2C_SCL_PIN 6
#define I2C_SDA_PIN 7

#elif   defined(SANSA_M200V4)
#define I2C_GPIO(x) GPIOD_PIN(x)
#define I2C_GPIO_DIR GPIOD_DIR
#define I2C_SCL_PIN 7
#define I2C_SDA_PIN 6

#elif   defined(SANSA_FUZE) || defined(SANSA_E200V2)
#define I2C_GPIO(x) GPIOA_PIN(x)
#define I2C_GPIO_DIR GPIOA_DIR
#define I2C_SCL_PIN 6
#define I2C_SDA_PIN 7

#else
#error no FM I2C GPIOPIN defines
#endif

static int fm_i2c_bus;

static void fm_scl_hi(void)
{
    I2C_GPIO(I2C_SCL_PIN) = 1 << I2C_SCL_PIN;
}

static void fm_scl_lo(void)
{
    I2C_GPIO(I2C_SCL_PIN) = 0;
}

static void fm_sda_hi(void)
{
    I2C_GPIO(I2C_SDA_PIN) = 1 << I2C_SDA_PIN;
}

static void fm_sda_lo(void)
{
    I2C_GPIO(I2C_SDA_PIN) = 0;
}

static void fm_sda_input(void)
{
    I2C_GPIO_DIR &= ~(1 << I2C_SDA_PIN);
}

static void fm_sda_output(void)
{
    I2C_GPIO_DIR |= 1 << I2C_SDA_PIN;
}

static void fm_scl_input(void)
{
    I2C_GPIO_DIR &= ~(1 << I2C_SCL_PIN);
}

static void fm_scl_output(void)
{
    I2C_GPIO_DIR |= 1 << I2C_SCL_PIN;
}

static int fm_sda(void)
{
    return I2C_GPIO(I2C_SDA_PIN);
}

static int fm_scl(void)
{
    return I2C_GPIO(I2C_SCL_PIN);
}

/* simple and crude delay, used for all delays in the generic i2c driver */
static void fm_delay(void)
{
    volatile int i;

    /* this loop is uncalibrated and could use more sophistication */
    for (i = 0; i < 20; i++) {
    }
}

/* interface towards the generic i2c driver */
static const struct i2c_interface fm_i2c_interface = {
    .scl_hi = fm_scl_hi,
    .scl_lo = fm_scl_lo,
    .sda_hi = fm_sda_hi,
    .sda_lo = fm_sda_lo,
    .sda_input = fm_sda_input,
    .sda_output = fm_sda_output,
    .scl_input = fm_scl_input,
    .scl_output = fm_scl_output,
    .scl = fm_scl,
    .sda = fm_sda,

    .delay_hd_sta = fm_delay,
    .delay_hd_dat = fm_delay,
    .delay_su_dat = fm_delay,
    .delay_su_sto = fm_delay,
    .delay_su_sta = fm_delay,
    .delay_thigh = fm_delay
};

/* initialise i2c for fmradio */
void fmradio_i2c_init(void)
{
    fm_i2c_bus = i2c_add_node(&fm_i2c_interface);
}

int fmradio_i2c_write(unsigned char address, const unsigned char* buf, int count)
{
    return i2c_write_data(fm_i2c_bus, address, -1, buf, count);
}

int fmradio_i2c_read(unsigned char address, unsigned char* buf, int count)
{
    return i2c_read_data(fm_i2c_bus, address, -1, buf, count);
}