sd-as3525v1: set up bank selection data outside of the loop
[kugel-rb.git] / firmware / target / arm / as3525 / fmradio-i2c-as3525.c
blob33d12f9fa7bd72da76914a0c337625867e432d03
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_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
39 #define I2C_SCL_PIN 4
40 #define I2C_SDA_PIN 5
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
47 #define I2C_SCL_PIN 6
48 #define I2C_SDA_PIN 7
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
55 #define I2C_SCL_PIN 7
56 #define I2C_SDA_PIN 6
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
63 #define I2C_SCL_PIN 6
64 #define I2C_SDA_PIN 7
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
71 #define I2C_SCL_PIN 1
72 #define I2C_SDA_PIN 0
74 #else
75 #error no FM I2C GPIOPIN defines
76 #endif
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)
133 volatile int i;
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 = {
142 .scl_hi = fm_scl_hi,
143 .scl_lo = fm_scl_lo,
144 .sda_hi = fm_sda_hi,
145 .sda_lo = fm_sda_lo,
146 .sda_input = fm_sda_input,
147 .sda_output = fm_sda_output,
148 .scl_input = fm_scl_input,
149 .scl_output = fm_scl_output,
150 .scl = fm_scl,
151 .sda = fm_sda,
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)
169 #ifdef SANSA_FUZEV2
170 CCU_IO &= ~(1<<12);
171 #endif
172 int ret = i2c_write_data(fm_i2c_bus, address, -1, buf, count);
173 #ifdef SANSA_FUZEV2
174 CCU_IO |= 1<<12;
175 #endif
176 return ret;
179 int fmradio_i2c_read(unsigned char address, unsigned char* buf, int count)
181 #ifdef SANSA_FUZEV2
182 CCU_IO &= ~(1<<12);
183 #endif
184 int ret = i2c_read_data(fm_i2c_bus, address, -1, buf, count);
185 #ifdef SANSA_FUZEV2
186 CCU_IO |= 1<<12;
187 #endif
188 return ret;