FS#11417 by Joe Balough: fix audio/tuner on philips hdd6330
[kugel-rb.git] / firmware / target / arm / i2c-pp.c
blob58740b5c66c16160da86ea712d81302c3f0f88f5
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.h"
35 #include "i2c-pp.h"
36 #include "ascodec.h"
37 #include "as3514.h"
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 */
45 #define I2C_SEND 0x80
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)) {
61 return 0;
63 yield();
66 return -1;
69 static int pp_i2c_read_bytes(unsigned int addr, int len, unsigned char *data)
71 int i;
73 if (len < 1 || len > 4)
75 return -1;
78 if (pp_i2c_wait_not_busy() < 0)
80 return -2;
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;
89 I2C_CTRL |= 0x20;
91 I2C_CTRL = (I2C_CTRL & ~0x6) | ((len-1) << 1);
93 I2C_CTRL |= I2C_SEND;
95 restore_irq(old_irq_level);
97 if (pp_i2c_wait_not_busy() < 0)
99 return -2;
102 old_irq_level = disable_irq_save();
104 if (data)
106 for ( i = 0; i < len; i++ )
107 *data++ = I2C_DATA(i);
110 restore_irq(old_irq_level);
113 return 0;
116 static int pp_i2c_send_bytes(unsigned int addr, int len, unsigned char *data)
118 int i;
120 if (len < 1 || len > 4)
122 return -1;
125 if (pp_i2c_wait_not_busy() < 0)
127 return -2;
131 int old_irq_level = disable_irq_save();
133 /* clear top 15 bits, left shift 1 */
134 I2C_ADDR = (addr << 17) >> 16;
136 I2C_CTRL &= ~0x20;
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);
150 return 0;
153 static int pp_i2c_send_byte(unsigned int addr, int data0)
155 unsigned char data[1];
157 data[0] = data0;
159 return pp_i2c_send_bytes(addr, 1, data);
162 /* Public functions */
163 void i2c_lock(void)
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)
175 int i, n;
177 mutex_lock(&i2c_mtx);
179 if (addr >= 0)
180 pp_i2c_send_byte(dev_addr, addr);
182 i = 0;
183 while (len > 0)
185 n = (len < 4) ? len : 4;
187 if (pp_i2c_read_bytes(dev_addr, n, data + i) < 0)
188 break;
190 len -= n;
191 i += n;
194 mutex_unlock(&i2c_mtx);
196 return i;
199 int i2c_readbyte(unsigned int dev_addr, int addr)
201 unsigned char data;
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);
208 return (int)data;
211 int i2c_sendbytes(unsigned int addr, int len, const unsigned char *data)
213 int i, n;
215 mutex_lock(&i2c_mtx);
217 i = 0;
218 while (len > 0)
220 n = (len < 4) ? len : 4;
222 if (pp_i2c_send_bytes(addr, n, (unsigned char *)(data + i)) < 0)
223 break;
225 len -= n;
226 i += n;
229 mutex_unlock(&i2c_mtx);
231 return i;
234 int pp_i2c_send(unsigned int addr, int data0, int data1)
236 int retval;
237 unsigned char data[2];
239 data[0] = data0;
240 data[1] = data1;
242 mutex_lock(&i2c_mtx);
243 retval = pp_i2c_send_bytes(addr, 2, data);
244 mutex_unlock(&i2c_mtx);
246 return retval;
249 void i2c_init(void)
251 /* From ipodlinux */
252 mutex_init(&i2c_mtx);
254 #ifdef IPOD_MINI
255 /* GPIO port C disable port 0x10 */
256 GPIOC_ENABLE &= ~0x10;
258 /* GPIO port C disable port 0x20 */
259 GPIOC_ENABLE &= ~0x20;
260 #endif
262 #if CONFIG_I2C == I2C_PP5002
263 DEV_EN |= 0x2;
264 #else
265 DEV_EN |= DEV_I2C; /* Enable I2C */
266 #endif
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);
279 #else
280 outl(0x80 | (0 << 8), 0x600060a4);
281 #endif
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();
293 outl(0, 0x600060a4);
294 outl(0x64, 0x600060a4);
296 outl(0x55, 0x7000c02c);
297 outl(0x54, 0x7000c030);
299 outl(0, 0x600060a4);
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);
310 #endif
311 #endif
313 i2c_readbyte(0x8, 0);