1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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.
31 #include "generic_i2c.h"
32 #include "fmradio_i2c.h"
34 #if defined(SANSA_CLIP) || defined(SANSA_C200V2)
35 #define I2C_SCL_GPIO(x) GPIOB_PIN(x)
36 #define I2C_SDA_GPIO(x) GPIOB_PIN(x)
37 #define I2C_SCL_GPIO_DIR GPIOB_DIR
38 #define I2C_SDA_GPIO_DIR GPIOB_DIR
42 #elif defined(SANSA_CLIPV2) || defined(SANSA_CLIPPLUS)
43 #define I2C_SCL_GPIO(x) GPIOB_PIN(x)
44 #define I2C_SDA_GPIO(x) GPIOB_PIN(x)
45 #define I2C_SCL_GPIO_DIR GPIOB_DIR
46 #define I2C_SDA_GPIO_DIR GPIOB_DIR
50 #elif defined(SANSA_M200V4)
51 #define I2C_SCL_GPIO(x) GPIOD_PIN(x)
52 #define I2C_SDA_GPIO(x) GPIOD_PIN(x)
53 #define I2C_SCL_GPIO_DIR GPIOD_DIR
54 #define I2C_SDA_GPIO_DIR GPIOD_DIR
58 #elif defined(SANSA_FUZE) || defined(SANSA_E200V2)
59 #define I2C_SCL_GPIO(x) GPIOA_PIN(x)
60 #define I2C_SDA_GPIO(x) GPIOA_PIN(x)
61 #define I2C_SCL_GPIO_DIR GPIOA_DIR
62 #define I2C_SDA_GPIO_DIR GPIOA_DIR
66 #elif defined(SANSA_FUZEV2)
67 #define I2C_SCL_GPIO(x) GPIOB_PIN(x)
68 #define I2C_SDA_GPIO(x) GPIOA_PIN(x)
69 #define I2C_SCL_GPIO_DIR GPIOB_DIR
70 #define I2C_SDA_GPIO_DIR GPIOA_DIR
75 #error no FM I2C GPIOPIN defines
78 static int fm_i2c_bus
;
80 static void fm_scl_hi(void)
82 I2C_SCL_GPIO(I2C_SCL_PIN
) = 1 << I2C_SCL_PIN
;
85 static void fm_scl_lo(void)
87 I2C_SCL_GPIO(I2C_SCL_PIN
) = 0;
90 static void fm_sda_hi(void)
92 I2C_SDA_GPIO(I2C_SDA_PIN
) = 1 << I2C_SDA_PIN
;
95 static void fm_sda_lo(void)
97 I2C_SDA_GPIO(I2C_SDA_PIN
) = 0;
100 static void fm_sda_input(void)
102 I2C_SDA_GPIO_DIR
&= ~(1 << I2C_SDA_PIN
);
105 static void fm_sda_output(void)
107 I2C_SDA_GPIO_DIR
|= 1 << I2C_SDA_PIN
;
110 static void fm_scl_input(void)
112 I2C_SCL_GPIO_DIR
&= ~(1 << I2C_SCL_PIN
);
115 static void fm_scl_output(void)
117 I2C_SCL_GPIO_DIR
|= 1 << I2C_SCL_PIN
;
120 static int fm_sda(void)
122 return I2C_SDA_GPIO(I2C_SDA_PIN
);
125 static int fm_scl(void)
127 return I2C_SCL_GPIO(I2C_SCL_PIN
);
130 /* simple and crude delay, used for all delays in the generic i2c driver */
131 static void fm_delay(void)
135 /* this loop is uncalibrated and could use more sophistication */
136 for (i
= 0; i
< 20; i
++) {
140 /* interface towards the generic i2c driver */
141 static const struct i2c_interface fm_i2c_interface
= {
146 .sda_input
= fm_sda_input
,
147 .sda_output
= fm_sda_output
,
148 .scl_input
= fm_scl_input
,
149 .scl_output
= fm_scl_output
,
153 .delay_hd_sta
= fm_delay
,
154 .delay_hd_dat
= fm_delay
,
155 .delay_su_dat
= fm_delay
,
156 .delay_su_sto
= fm_delay
,
157 .delay_su_sta
= fm_delay
,
158 .delay_thigh
= fm_delay
161 /* initialise i2c for fmradio */
162 void fmradio_i2c_init(void)
164 fm_i2c_bus
= i2c_add_node(&fm_i2c_interface
);
167 int fmradio_i2c_write(unsigned char address
, const unsigned char* buf
, int count
)
172 int ret
= i2c_write_data(fm_i2c_bus
, address
, -1, buf
, count
);
179 int fmradio_i2c_read(unsigned char address
, unsigned char* buf
, int count
)
184 int ret
= i2c_read_data(fm_i2c_bus
, address
, -1, buf
, count
);