summaryrefslogtreecommitdiff
path: root/firmware/target/arm/tms320dm320/i2c-dm320.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/tms320dm320/i2c-dm320.c')
-rw-r--r--firmware/target/arm/tms320dm320/i2c-dm320.c138
1 files changed, 134 insertions, 4 deletions
diff --git a/firmware/target/arm/tms320dm320/i2c-dm320.c b/firmware/target/arm/tms320dm320/i2c-dm320.c
index 8bcc84dd8f..990dad0721 100644
--- a/firmware/target/arm/tms320dm320/i2c-dm320.c
+++ b/firmware/target/arm/tms320dm320/i2c-dm320.c
@@ -7,6 +7,7 @@
7 * \/ \/ \/ \/ \/ 7 * \/ \/ \/ \/ \/
8 * $Id$ 8 * $Id$
9 * 9 *
10 * Copyright (C) 2011 by Tomasz Moń
10 * Copyright (C) 2008 by Maurus Cuelenaere 11 * Copyright (C) 2008 by Maurus Cuelenaere
11 * 12 *
12 * DM320 I²C driver 13 * DM320 I²C driver
@@ -24,11 +25,11 @@
24#include "thread.h" 25#include "thread.h"
25#include "i2c-dm320.h" 26#include "i2c-dm320.h"
26 27
27#define I2C_SCS_COND_START 0x0001 28#ifdef HAVE_SOFTWARE_I2C
28#define I2C_SCS_COND_STOP 0x0002 29#include "generic_i2c.h"
29#define I2C_SCS_XMIT 0x0004 30#endif
30 31
31#define I2C_TX_ACK (1 << 8) 32#ifndef HAVE_SOFTWARE_I2C
32 33
33static struct mutex i2c_mtx; 34static struct mutex i2c_mtx;
34 35
@@ -42,6 +43,12 @@ static inline void i2c_end(void)
42 mutex_unlock(&i2c_mtx); 43 mutex_unlock(&i2c_mtx);
43} 44}
44 45
46#define I2C_SCS_COND_START 0x0001
47#define I2C_SCS_COND_STOP 0x0002
48#define I2C_SCS_XMIT 0x0004
49
50#define I2C_TX_ACK (1 << 8)
51
45static inline bool i2c_getack(void) 52static inline bool i2c_getack(void)
46{ 53{
47 return (IO_I2C_RXDATA >> 8) & 1; 54 return (IO_I2C_RXDATA >> 8) & 1;
@@ -158,3 +165,126 @@ void i2c_init(void)
158 IO_I2C_SCS &= ~0x8; //set clock to 100 kHz 165 IO_I2C_SCS &= ~0x8; //set clock to 100 kHz
159 IO_INTC_EINT2 &= ~INTR_EINT2_I2C; // disable I²C interrupt 166 IO_INTC_EINT2 &= ~INTR_EINT2_I2C; // disable I²C interrupt
160} 167}
168
169#else /* Software I2C implementation */
170
171#ifdef SANSA_CONNECT
172 /* SDA - GIO35 */
173 #define SDA_SET_REG IO_GIO_BITSET2
174 #define SDA_CLR_REG IO_GIO_BITCLR2
175 #define SOFTI2C_SDA (1 << 3)
176 /* SCL - GIO36 */
177 #define SCL_SET_REG IO_GIO_BITSET2
178 #define SCL_CLR_REG IO_GIO_BITCLR2
179 #define SOFTI2C_SCL (1 << 4)
180#else
181 #error Configure SDA and SCL lines
182#endif
183
184static int dm320_i2c_bus;
185
186static void dm320_scl_dir(bool out)
187{
188 if (out)
189 {
190 IO_GIO_DIR2 &= ~(SOFTI2C_SCL);
191 }
192 else
193 {
194 IO_GIO_DIR2 |= SOFTI2C_SCL;
195 }
196}
197
198static void dm320_sda_dir(bool out)
199{
200 if (out)
201 {
202 IO_GIO_DIR2 &= ~(SOFTI2C_SDA);
203 }
204 else
205 {
206 IO_GIO_DIR2 |= SOFTI2C_SDA;
207 }
208}
209
210static void dm320_scl_out(bool high)
211{
212 if (high)
213 {
214 SCL_SET_REG = SOFTI2C_SCL;
215 }
216 else
217 {
218 SCL_CLR_REG = SOFTI2C_SCL;
219 }
220}
221
222static void dm320_sda_out(bool high)
223{
224 if (high)
225 {
226 SDA_SET_REG = SOFTI2C_SDA;
227 }
228 else
229 {
230 SDA_CLR_REG = SOFTI2C_SDA;
231 }
232}
233
234static bool dm320_scl_in(void)
235{
236 return (SCL_SET_REG & SOFTI2C_SCL);
237}
238
239static bool dm320_sda_in(void)
240{
241 return (SDA_SET_REG & SOFTI2C_SDA);
242}
243
244/* simple delay */
245static void dm320_i2c_delay(int delay)
246{
247 udelay(delay);
248}
249
250/* interface towards the generic i2c driver */
251static const struct i2c_interface dm320_i2c_interface = {
252 .scl_dir = dm320_scl_dir,
253 .sda_dir = dm320_sda_dir,
254 .scl_out = dm320_scl_out,
255 .sda_out = dm320_sda_out,
256 .scl_in = dm320_scl_in,
257 .sda_in = dm320_sda_in,
258 .delay = dm320_i2c_delay,
259
260 /* uncalibrated */
261 .delay_hd_sta = 1,
262 .delay_hd_dat = 1,
263 .delay_su_dat = 1,
264 .delay_su_sto = 1,
265 .delay_su_sta = 1,
266 .delay_thigh = 1
267};
268
269void i2c_init(void)
270{
271#ifdef SANSA_CONNECT
272 IO_GIO_FSEL3 &= 0xFF0F; /* GIO35, GIO36 as normal GIO */
273 IO_GIO_INV2 &= ~(SOFTI2C_SDA | SOFTI2C_SCL); /* not inverted */
274#endif
275
276 /* generic_i2c takes care of setting direction */
277 dm320_i2c_bus = i2c_add_node(&dm320_i2c_interface);
278}
279
280int i2c_write(unsigned short address, const unsigned char* buf, int count)
281{
282 return i2c_write_data(dm320_i2c_bus, address, -1, buf, count);
283}
284
285int i2c_read(unsigned short address, unsigned char* buf, int count)
286{
287 return i2c_read_data(dm320_i2c_bus, address, -1, buf, count);
288}
289
290#endif