Apply FS #11423 - Use udelay in AMS driver for FM radio I2C
[kugel-rb.git] / firmware / target / arm / as3525 / fmradio-i2c-as3525.c
blob9e8dc63144d7468c3e05331dd645979770af7ada
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 "system.h"
32 #include "generic_i2c.h"
33 #include "fmradio_i2c.h"
35 #if defined(SANSA_CLIP) || defined(SANSA_C200V2)
36 #define I2C_SCL_GPIO(x) GPIOB_PIN(x)
37 #define I2C_SDA_GPIO(x) GPIOB_PIN(x)
38 #define I2C_SCL_GPIO_DIR GPIOB_DIR
39 #define I2C_SDA_GPIO_DIR GPIOB_DIR
40 #define I2C_SCL_PIN 4
41 #define I2C_SDA_PIN 5
43 #elif defined(SANSA_CLIPV2) || defined(SANSA_CLIPPLUS)
44 #define I2C_SCL_GPIO(x) GPIOB_PIN(x)
45 #define I2C_SDA_GPIO(x) GPIOB_PIN(x)
46 #define I2C_SCL_GPIO_DIR GPIOB_DIR
47 #define I2C_SDA_GPIO_DIR GPIOB_DIR
48 #define I2C_SCL_PIN 6
49 #define I2C_SDA_PIN 7
51 #elif defined(SANSA_M200V4)
52 #define I2C_SCL_GPIO(x) GPIOD_PIN(x)
53 #define I2C_SDA_GPIO(x) GPIOD_PIN(x)
54 #define I2C_SCL_GPIO_DIR GPIOD_DIR
55 #define I2C_SDA_GPIO_DIR GPIOD_DIR
56 #define I2C_SCL_PIN 7
57 #define I2C_SDA_PIN 6
59 #elif defined(SANSA_FUZE) || defined(SANSA_E200V2)
60 #define I2C_SCL_GPIO(x) GPIOA_PIN(x)
61 #define I2C_SDA_GPIO(x) GPIOA_PIN(x)
62 #define I2C_SCL_GPIO_DIR GPIOA_DIR
63 #define I2C_SDA_GPIO_DIR GPIOA_DIR
64 #define I2C_SCL_PIN 6
65 #define I2C_SDA_PIN 7
67 #elif defined(SANSA_FUZEV2)
68 #define I2C_SCL_GPIO(x) GPIOB_PIN(x)
69 #define I2C_SDA_GPIO(x) GPIOA_PIN(x)
70 #define I2C_SCL_GPIO_DIR GPIOB_DIR
71 #define I2C_SDA_GPIO_DIR GPIOA_DIR
72 #define I2C_SCL_PIN 1
73 #define I2C_SDA_PIN 0
75 #else
76 #error no FM I2C GPIOPIN defines
77 #endif
79 static int fm_i2c_bus;
81 static void fm_scl_dir(bool out)
83 if (out) {
84 I2C_SCL_GPIO_DIR |= 1 << I2C_SCL_PIN;
85 } else {
86 I2C_SCL_GPIO_DIR &= ~(1 << I2C_SCL_PIN);
90 static void fm_sda_dir(bool out)
92 if (out) {
93 I2C_SDA_GPIO_DIR |= 1 << I2C_SDA_PIN;
94 } else {
95 I2C_SDA_GPIO_DIR &= ~(1 << I2C_SDA_PIN);
99 static void fm_scl_out(bool level)
101 if (level) {
102 I2C_SCL_GPIO(I2C_SCL_PIN) = 1 << I2C_SCL_PIN;
103 } else {
104 I2C_SCL_GPIO(I2C_SCL_PIN) = 0;
108 static void fm_sda_out(bool level)
110 if (level) {
111 I2C_SDA_GPIO(I2C_SDA_PIN) = 1 << I2C_SDA_PIN;
112 } else {
113 I2C_SDA_GPIO(I2C_SDA_PIN) = 0;
117 static bool fm_scl_in(void)
119 return I2C_SCL_GPIO(I2C_SCL_PIN);
122 static bool fm_sda_in(void)
124 return I2C_SDA_GPIO(I2C_SDA_PIN);
127 static void fm_delay(int delay)
129 if (delay != 0) {
130 udelay(delay);
134 /* interface towards the generic i2c driver */
135 static const struct i2c_interface fm_i2c_interface = {
136 .scl_out = fm_scl_out,
137 .scl_dir = fm_scl_dir,
138 .sda_out = fm_sda_out,
139 .sda_dir = fm_sda_dir,
140 .sda_in = fm_sda_in,
141 .scl_in = fm_scl_in,
142 .delay = fm_delay,
144 .delay_hd_sta = 1,
145 .delay_hd_dat = 0,
146 .delay_su_dat = 1,
147 .delay_su_sto = 1,
148 .delay_su_sta = 1,
149 .delay_thigh = 2
152 /* initialise i2c for fmradio */
153 void fmradio_i2c_init(void)
155 fm_i2c_bus = i2c_add_node(&fm_i2c_interface);
158 int fmradio_i2c_write(unsigned char address, const unsigned char* buf, int count)
160 #ifdef SANSA_FUZEV2
161 CCU_IO &= ~(1<<12);
162 #endif
163 int ret = i2c_write_data(fm_i2c_bus, address, -1, buf, count);
164 #ifdef SANSA_FUZEV2
165 CCU_IO |= 1<<12;
166 #endif
167 return ret;
170 int fmradio_i2c_read(unsigned char address, unsigned char* buf, int count)
172 #ifdef SANSA_FUZEV2
173 CCU_IO &= ~(1<<12);
174 #endif
175 int ret = i2c_read_data(fm_i2c_bus, address, -1, buf, count);
176 #ifdef SANSA_FUZEV2
177 CCU_IO |= 1<<12;
178 #endif
179 return ret;