FS#10047 : Clipv2
[kugel-rb.git] / firmware / target / arm / as3525 / fmradio-i2c-as3525.c
blob6cdf0c7c7f747e5097c19dace51b7a30c2df155d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 by Bertrik Sikken
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 This is the fmradio_i2c interface, used by the radio driver
24 to communicate with the radio tuner chip.
26 It is implemented using the generic i2c driver, which does "bit-banged"
27 I2C with a couple of GPIO pins.
30 #include "as3525.h"
31 #include "generic_i2c.h"
32 #include "fmradio_i2c.h"
34 #if defined(SANSA_CLIP) || defined(SANSA_C200V2)
35 #define I2C_GPIO(x) GPIOB_PIN(x)
36 #define I2C_GPIO_DIR GPIOB_DIR
37 #define I2C_SCL_PIN 4
38 #define I2C_SDA_PIN 5
40 #elif defined(SANSA_CLIPV2)
41 #define I2C_GPIO(x) GPIOB_PIN(x)
42 #define I2C_GPIO_DIR GPIOB_DIR
43 #define I2C_SCL_PIN 6
44 #define I2C_SDA_PIN 7
46 #elif defined(SANSA_M200V4)
47 #define I2C_GPIO(x) GPIOD_PIN(x)
48 #define I2C_GPIO_DIR GPIOD_DIR
49 #define I2C_SCL_PIN 7
50 #define I2C_SDA_PIN 6
52 #elif defined(SANSA_FUZE) || defined(SANSA_E200V2)
53 #define I2C_GPIO(x) GPIOA_PIN(x)
54 #define I2C_GPIO_DIR GPIOA_DIR
55 #define I2C_SCL_PIN 6
56 #define I2C_SDA_PIN 7
58 #else
59 #error no FM I2C GPIOPIN defines
60 #endif
62 static int fm_i2c_bus;
64 static void fm_scl_hi(void)
66 I2C_GPIO(I2C_SCL_PIN) = 1 << I2C_SCL_PIN;
69 static void fm_scl_lo(void)
71 I2C_GPIO(I2C_SCL_PIN) = 0;
74 static void fm_sda_hi(void)
76 I2C_GPIO(I2C_SDA_PIN) = 1 << I2C_SDA_PIN;
79 static void fm_sda_lo(void)
81 I2C_GPIO(I2C_SDA_PIN) = 0;
84 static void fm_sda_input(void)
86 I2C_GPIO_DIR &= ~(1 << I2C_SDA_PIN);
89 static void fm_sda_output(void)
91 I2C_GPIO_DIR |= 1 << I2C_SDA_PIN;
94 static void fm_scl_input(void)
96 I2C_GPIO_DIR &= ~(1 << I2C_SCL_PIN);
99 static void fm_scl_output(void)
101 I2C_GPIO_DIR |= 1 << I2C_SCL_PIN;
104 static int fm_sda(void)
106 return I2C_GPIO(I2C_SDA_PIN);
109 static int fm_scl(void)
111 return I2C_GPIO(I2C_SCL_PIN);
114 /* simple and crude delay, used for all delays in the generic i2c driver */
115 static void fm_delay(void)
117 volatile int i;
119 /* this loop is uncalibrated and could use more sophistication */
120 for (i = 0; i < 20; i++) {
124 /* interface towards the generic i2c driver */
125 static const struct i2c_interface fm_i2c_interface = {
126 .scl_hi = fm_scl_hi,
127 .scl_lo = fm_scl_lo,
128 .sda_hi = fm_sda_hi,
129 .sda_lo = fm_sda_lo,
130 .sda_input = fm_sda_input,
131 .sda_output = fm_sda_output,
132 .scl_input = fm_scl_input,
133 .scl_output = fm_scl_output,
134 .scl = fm_scl,
135 .sda = fm_sda,
137 .delay_hd_sta = fm_delay,
138 .delay_hd_dat = fm_delay,
139 .delay_su_dat = fm_delay,
140 .delay_su_sto = fm_delay,
141 .delay_su_sta = fm_delay,
142 .delay_thigh = fm_delay
145 /* initialise i2c for fmradio */
146 void fmradio_i2c_init(void)
148 fm_i2c_bus = i2c_add_node(&fm_i2c_interface);
151 int fmradio_i2c_write(unsigned char address, const unsigned char* buf, int count)
153 return i2c_write_data(fm_i2c_bus, address, -1, buf, count);
156 int fmradio_i2c_read(unsigned char address, unsigned char* buf, int count)
158 return i2c_read_data(fm_i2c_bus, address, -1, buf, count);