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 ****************************************************************************/
37 /* Local functions definitions */
38 static struct mutex i2c_mtx SHAREDBSS_ATTR
;
40 #define POLL_TIMEOUT (HZ)
42 static int pp_i2c_wait_not_busy(void)
44 unsigned long timeout
;
45 timeout
= current_tick
+ POLL_TIMEOUT
;
46 while (TIME_BEFORE(current_tick
, timeout
)) {
47 if (!(I2C_STATUS
& I2C_BUSY
)) {
56 static int pp_i2c_read_byte(unsigned int addr
, unsigned int *data
)
58 if (pp_i2c_wait_not_busy() < 0)
65 int old_irq_level
= disable_irq_save();
67 /* clear top 15 bits, left shift 1, or in 0x1 for a read */
68 I2C_ADDR
= ((addr
<< 17) >> 16) | 0x1 ;
74 restore_irq(old_irq_level
);
75 if (pp_i2c_wait_not_busy() < 0)
79 old_irq_level
= disable_irq_save();
86 restore_irq(old_irq_level
);
92 static int pp_i2c_send_bytes(unsigned int addr
, unsigned int len
, unsigned char *data
)
96 if (len
< 1 || len
> 4)
101 if (pp_i2c_wait_not_busy() < 0)
107 int old_irq_level
= disable_irq_save();
109 /* clear top 15 bits, left shift 1 */
110 I2C_ADDR
= (addr
<< 17) >> 16;
114 for ( i
= 0; i
< len
; i
++ )
116 I2C_DATA(i
) = *data
++;
119 I2C_CTRL
= (I2C_CTRL
& ~0x26) | ((len
-1) << 1);
121 I2C_CTRL
|= I2C_SEND
;
123 restore_irq(old_irq_level
);
129 static int pp_i2c_send_byte(unsigned int addr
, int data0
)
131 unsigned char data
[1];
135 return pp_i2c_send_bytes(addr
, 1, data
);
138 /* Public functions */
141 mutex_lock(&i2c_mtx
);
144 void i2c_unlock(void)
146 mutex_unlock(&i2c_mtx
);
149 int i2c_readbytes(unsigned int dev_addr
, int addr
, int len
, unsigned char *data
) {
152 mutex_lock(&i2c_mtx
);
153 pp_i2c_send_byte(dev_addr
, addr
);
154 for (i
= 0; i
< len
; i
++) {
155 pp_i2c_read_byte(dev_addr
, &temp
);
158 mutex_unlock(&i2c_mtx
);
162 int i2c_readbyte(unsigned int dev_addr
, int addr
)
166 mutex_lock(&i2c_mtx
);
167 pp_i2c_send_byte(dev_addr
, addr
);
168 pp_i2c_read_byte(dev_addr
, &data
);
169 mutex_unlock(&i2c_mtx
);
174 int pp_i2c_send(unsigned int addr
, int data0
, int data1
)
177 unsigned char data
[2];
182 mutex_lock(&i2c_mtx
);
183 retval
= pp_i2c_send_bytes(addr
, 2, data
);
184 mutex_unlock(&i2c_mtx
);
192 mutex_init(&i2c_mtx
);
195 /* GPIO port C disable port 0x10 */
196 GPIOC_ENABLE
&= ~0x10;
198 /* GPIO port C disable port 0x20 */
199 GPIOC_ENABLE
&= ~0x20;
202 #if CONFIG_I2C == I2C_PP5002
205 DEV_EN
|= DEV_I2C
; /* Enable I2C */
207 DEV_RS
|= DEV_I2C
; /* Start I2C Reset */
208 DEV_RS
&=~DEV_I2C
; /* End I2C Reset */
210 #if CONFIG_I2C == I2C_PP5020
211 outl(0x0, 0x600060a4);
212 outl(0x80 | (0 << 8), 0x600060a4);
213 #elif CONFIG_I2C == I2C_PP5024
214 #if defined(SANSA_E200) || defined(PHILIPS_SA9200)
215 /* Sansa OF sets this to 0x20 first, communicates with the AS3514
216 then sets it to 0x23 - this still works fine though */
217 outl(0x0, 0x600060a4);
218 outl(0x23, 0x600060a4);
219 #elif defined(SANSA_C200)
220 /* This is the init sequence from the Sansa c200 bootloader.
221 I'm not sure what's really necessary. */
222 pp_i2c_wait_not_busy();
225 outl(0x64, 0x600060a4);
227 outl(0x55, 0x7000c02c);
228 outl(0x54, 0x7000c030);
231 outl(0x1e, 0x600060a4);
233 pp_i2c_send(AS3514_I2C_ADDR
, AS3514_SUPERVISOR
, 5);
237 i2c_readbyte(0x8, 0);