Colour targets: Revert an optimisation from almost 18 months ago that actually turned...
[Rockbox.git] / firmware / target / arm / i2c-pp.c
blob4d83208d9171ba6f936ff4cb78d191acb6e0d6a1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
29 #include "cpu.h"
30 #include "kernel.h"
31 #include "thread.h"
32 #include "logf.h"
33 #include "system.h"
34 #include "i2c-pp.h"
35 #include "as3514.h"
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)) {
48 return 0;
50 yield();
53 return -1;
56 static int pp_i2c_read_byte(unsigned int addr, unsigned int *data)
58 if (pp_i2c_wait_not_busy() < 0)
60 return -1;
64 unsigned int byte;
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 ;
70 I2C_CTRL |= 0x20;
72 I2C_CTRL |= I2C_SEND;
74 restore_irq(old_irq_level);
75 if (pp_i2c_wait_not_busy() < 0)
77 return -1;
79 old_irq_level = disable_irq_save();
81 byte = I2C_DATA(0);
83 if (data)
84 *data = byte;
86 restore_irq(old_irq_level);
89 return 0;
92 static int pp_i2c_send_bytes(unsigned int addr, unsigned int len, unsigned char *data)
94 unsigned int i;
96 if (len < 1 || len > 4)
98 return -1;
101 if (pp_i2c_wait_not_busy() < 0)
103 return -2;
107 int old_irq_level = disable_irq_save();
109 /* clear top 15 bits, left shift 1 */
110 I2C_ADDR = (addr << 17) >> 16;
112 I2C_CTRL &= ~0x20;
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);
126 return 0x0;
129 static int pp_i2c_send_byte(unsigned int addr, int data0)
131 unsigned char data[1];
133 data[0] = data0;
135 return pp_i2c_send_bytes(addr, 1, data);
138 /* Public functions */
139 void i2c_lock(void)
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) {
150 unsigned int temp;
151 int i;
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);
156 data[i] = temp;
158 mutex_unlock(&i2c_mtx);
159 return i;
162 int i2c_readbyte(unsigned int dev_addr, int addr)
164 int data;
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);
171 return data;
174 int pp_i2c_send(unsigned int addr, int data0, int data1)
176 int retval;
177 unsigned char data[2];
179 data[0] = data0;
180 data[1] = data1;
182 mutex_lock(&i2c_mtx);
183 retval = pp_i2c_send_bytes(addr, 2, data);
184 mutex_unlock(&i2c_mtx);
186 return retval;
189 void i2c_init(void)
191 /* From ipodlinux */
192 mutex_init(&i2c_mtx);
194 #ifdef IPOD_MINI
195 /* GPIO port C disable port 0x10 */
196 GPIOC_ENABLE &= ~0x10;
198 /* GPIO port C disable port 0x20 */
199 GPIOC_ENABLE &= ~0x20;
200 #endif
202 #if CONFIG_I2C == I2C_PP5002
203 DEV_EN |= 0x2;
204 #else
205 DEV_EN |= DEV_I2C; /* Enable I2C */
206 #endif
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();
224 outl(0, 0x600060a4);
225 outl(0x64, 0x600060a4);
227 outl(0x55, 0x7000c02c);
228 outl(0x54, 0x7000c030);
230 outl(0, 0x600060a4);
231 outl(0x1e, 0x600060a4);
233 pp_i2c_send(AS3514_I2C_ADDR, AS3514_SUPERVISOR, 5);
234 #endif
235 #endif
237 i2c_readbyte(0x8, 0);