1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * PP502X and PP5002 I2C driver
12 * Based on code from the ipodlinux project - http://ipodlinux.org/
13 * Adapted for Rockbox in November 2005
15 * Original file: linux/arch/armnommu/mach-ipod/hardware.c
17 * Copyright (c) 2003-2005 Bernard Leach (leachbj@bouncycastle.org)
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version 2
22 * of the License, or (at your option) any later version.
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
25 * KIND, either express or implied.
27 ****************************************************************************/
39 #define I2C_CTRL (*(volatile unsigned char*)(I2C_BASE+0x00))
40 #define I2C_ADDR (*(volatile unsigned char*)(I2C_BASE+0x04))
41 #define I2C_DATA(X) (*(volatile unsigned char*)(I2C_BASE+0xc+(4*X)))
42 #define I2C_STATUS (*(volatile unsigned char*)(I2C_BASE+0x1c))
44 /* I2C_CTRL bit definitions */
47 /* I2C_STATUS bit definitions */
48 #define I2C_BUSY (1<<6)
50 /* Local functions definitions */
51 static struct mutex i2c_mtx SHAREDBSS_ATTR
;
53 #define POLL_TIMEOUT (HZ)
55 static int pp_i2c_wait_not_busy(void)
57 unsigned long timeout
;
58 timeout
= current_tick
+ POLL_TIMEOUT
;
59 while (TIME_BEFORE(current_tick
, timeout
)) {
60 if (!(I2C_STATUS
& I2C_BUSY
)) {
69 static int pp_i2c_read_bytes(unsigned int addr
, int len
, unsigned char *data
)
73 if (len
< 1 || len
> 4)
78 if (pp_i2c_wait_not_busy() < 0)
84 int old_irq_level
= disable_irq_save();
86 /* clear top 15 bits, left shift 1, or in 0x1 for a read */
87 I2C_ADDR
= ((addr
<< 17) >> 16) | 0x1;
91 I2C_CTRL
= (I2C_CTRL
& ~0x6) | ((len
-1) << 1);
95 restore_irq(old_irq_level
);
97 if (pp_i2c_wait_not_busy() < 0)
102 old_irq_level
= disable_irq_save();
106 for ( i
= 0; i
< len
; i
++ )
107 *data
++ = I2C_DATA(i
);
110 restore_irq(old_irq_level
);
116 static int pp_i2c_send_bytes(unsigned int addr
, int len
, unsigned char *data
)
120 if (len
< 1 || len
> 4)
125 if (pp_i2c_wait_not_busy() < 0)
131 int old_irq_level
= disable_irq_save();
133 /* clear top 15 bits, left shift 1 */
134 I2C_ADDR
= (addr
<< 17) >> 16;
138 for ( i
= 0; i
< len
; i
++ )
140 I2C_DATA(i
) = *data
++;
143 I2C_CTRL
= (I2C_CTRL
& ~0x6) | ((len
-1) << 1);
145 I2C_CTRL
|= I2C_SEND
;
147 restore_irq(old_irq_level
);
153 static int pp_i2c_send_byte(unsigned int addr
, int data0
)
155 unsigned char data
[1];
159 return pp_i2c_send_bytes(addr
, 1, data
);
162 /* Public functions */
165 mutex_lock(&i2c_mtx
);
168 void i2c_unlock(void)
170 mutex_unlock(&i2c_mtx
);
173 int i2c_readbytes(unsigned int dev_addr
, int addr
, int len
, unsigned char *data
)
177 mutex_lock(&i2c_mtx
);
180 pp_i2c_send_byte(dev_addr
, addr
);
185 n
= (len
< 4) ? len
: 4;
187 if (pp_i2c_read_bytes(dev_addr
, n
, data
+ i
) < 0)
194 mutex_unlock(&i2c_mtx
);
199 int i2c_readbyte(unsigned int dev_addr
, int addr
)
203 mutex_lock(&i2c_mtx
);
204 pp_i2c_send_byte(dev_addr
, addr
);
205 pp_i2c_read_bytes(dev_addr
, 1, &data
);
206 mutex_unlock(&i2c_mtx
);
211 int i2c_sendbytes(unsigned int addr
, int len
, const unsigned char *data
)
215 mutex_lock(&i2c_mtx
);
220 n
= (len
< 4) ? len
: 4;
222 if (pp_i2c_send_bytes(addr
, n
, (unsigned char *)(data
+ i
)) < 0)
229 mutex_unlock(&i2c_mtx
);
234 int pp_i2c_send(unsigned int addr
, int data0
, int data1
)
237 unsigned char data
[2];
242 mutex_lock(&i2c_mtx
);
243 retval
= pp_i2c_send_bytes(addr
, 2, data
);
244 mutex_unlock(&i2c_mtx
);
252 mutex_init(&i2c_mtx
);
255 /* GPIO port C disable port 0x10 */
256 GPIOC_ENABLE
&= ~0x10;
258 /* GPIO port C disable port 0x20 */
259 GPIOC_ENABLE
&= ~0x20;
262 #if CONFIG_I2C == I2C_PP5002
265 DEV_EN
|= DEV_I2C
; /* Enable I2C */
267 DEV_RS
|= DEV_I2C
; /* Start I2C Reset */
268 DEV_RS
&=~DEV_I2C
; /* End I2C Reset */
270 #if CONFIG_I2C == I2C_PP5020
271 outl(0x0, 0x600060a4);
272 #if defined(PHILIPS_HDD1630) || defined(PHILIPS_HDD6330) || \
273 defined(SAMSUNG_YH820) || defined(SAMSUNG_YH920) || \
274 defined(SAMSUNG_YH925) || defined(PBELL_VIBE500)
275 outl(inl(0x600060a4) | 0x20, 0x600060a4);
276 outl(inl(0x7000c020) | 0x3, 0x7000c020);
277 outl(0x55, 0x7000c02c);
278 outl(0x54, 0x7000c030);
280 outl(0x80 | (0 << 8), 0x600060a4);
282 #elif CONFIG_I2C == I2C_PP5024
283 #if defined(SANSA_E200) || defined(PHILIPS_SA9200)
284 /* Sansa OF sets this to 0x20 first, communicates with the AS3514
285 then sets it to 0x23 - this still works fine though */
286 outl(0x0, 0x600060a4);
287 outl(0x23, 0x600060a4);
288 #elif defined(SANSA_C200)
289 /* This is the init sequence from the Sansa c200 bootloader.
290 I'm not sure what's really necessary. */
291 pp_i2c_wait_not_busy();
294 outl(0x64, 0x600060a4);
296 outl(0x55, 0x7000c02c);
297 outl(0x54, 0x7000c030);
300 outl(0x1e, 0x600060a4);
302 ascodec_write(AS3514_SUPERVISOR
, 5);
303 #elif defined(PHILIPS_SA9200)
304 outl(0x0, 0x600060a4);
305 outl(inl(0x600060a4) | 0x20, 0x600060a4);
307 outl(inl(0x7000c020) | 0x3, 0x7000c020);
308 outl(0x55, 0x7000c02c);
309 outl(0x54, 0x7000c030);
313 i2c_readbyte(0x8, 0);