Hopefully finish off the red from r26051.
[kugel-rb.git] / firmware / target / mips / ingenic_jz47xx / i2c-jz4740.c
blobec08dd61bb34a58d6f4e4ce5ed1b819870c5a950
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 by Maurus Cuelenaere
12 * based on linux/arch/mips/jz4740/i2c.c
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #include "config.h"
24 #include "system.h"
25 #include "jz4740.h"
26 #include "logf.h"
29 * Jz4740 I2C routines.
31 * Copyright (C) 2005,2006 Ingenic Semiconductor Inc.
32 * Author: <lhhuang@ingenic.cn>
34 * This program is free software; you can distribute it and/or modify it
35 * under the terms of the GNU General Public License (Version 2) as
36 * published by the Free Software Foundation.
38 * This program is distributed in the hope it will be useful, but WITHOUT
39 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
40 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
41 * for more details.
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
49 /* I2C protocol */
50 #define I2C_READ 1
51 #define I2C_WRITE 0
53 #define TIMEOUT 1000
56 * I2C bus protocol basic routines
58 static int i2c_put_data(unsigned char data)
60 unsigned int timeout = TIMEOUT*10;
62 __i2c_write(data);
63 __i2c_set_drf();
64 while (__i2c_check_drf() != 0);
65 while (!__i2c_transmit_ended());
66 while (!__i2c_received_ack() && timeout)
67 timeout--;
69 if (timeout)
70 return 0;
71 else
72 return -1;
75 #if 0
76 static int i2c_put_data_nack(unsigned char data)
78 unsigned int timeout = TIMEOUT*10;
80 __i2c_write(data);
81 __i2c_set_drf();
82 while (__i2c_check_drf() != 0);
83 while (!__i2c_transmit_ended());
84 while (timeout--);
86 return 0;
88 #endif
90 static int i2c_get_data(unsigned char *data, int ack)
92 int timeout = TIMEOUT*10;
94 if (!ack)
95 __i2c_send_nack();
96 else
97 __i2c_send_ack();
99 while (__i2c_check_drf() == 0 && timeout)
100 timeout--;
102 if (timeout)
104 if (!ack)
105 __i2c_send_stop();
106 *data = __i2c_read();
107 __i2c_clear_drf();
108 return 0;
110 else
111 return -1;
114 void i2c_setclk(unsigned int i2cclk)
116 __i2c_set_clk(__cpm_get_i2sclk(), i2cclk);
120 * I2C interface
122 static void i2c_open(void)
124 __cpm_start_i2c();
125 i2c_setclk(10000); /* default 10 KHz */
126 __i2c_enable();
129 static void i2c_close(void)
131 udelay(300); /* wait for STOP goes over. */
132 __i2c_disable();
133 __cpm_stop_i2c();
136 int i2c_read(int device, unsigned char *buf, int count)
138 int cnt = count;
139 int timeout = 5;
141 device &= 0xFF;
143 i2c_open();
145 L_try_again:
146 if (timeout < 0)
147 goto L_timeout;
149 __i2c_send_nack(); /* Master does not send ACK, slave sends it */
151 __i2c_send_start();
152 if (i2c_put_data( (device << 1) | I2C_READ ) < 0)
153 goto device_err;
155 __i2c_send_ack(); /* Master sends ACK for continue reading */
157 while (cnt)
159 if (cnt == 1)
161 if (i2c_get_data(buf, 0) < 0)
162 break;
164 else
166 if (i2c_get_data(buf, 1) < 0)
167 break;
169 cnt--;
170 buf++;
173 __i2c_send_stop();
174 i2c_close();
175 return count - cnt;
177 device_err:
178 timeout--;
179 __i2c_send_stop();
180 goto L_try_again;
182 L_timeout:
183 __i2c_send_stop();
184 logf("Read I2C device 0x%2x failed.", device);
185 i2c_close();
186 return -1;
189 int i2c_write(int device, unsigned char *buf, int count)
191 int cnt = count;
192 int timeout = 5;
194 device &= 0xFF;
196 i2c_open();
198 __i2c_send_nack(); /* Master does not send ACK, slave sends it */
200 W_try_again:
201 if (timeout < 0)
202 goto W_timeout;
204 cnt = count;
206 __i2c_send_start();
207 if (i2c_put_data( (device << 1) | I2C_WRITE ) < 0)
208 goto device_err;
210 #if 0 //CONFIG_JZ_TPANEL_ATA2508
211 if (address == 0xff)
213 while (cnt)
215 if (i2c_put_data_nack(*buf) < 0)
216 break;
217 cnt--;
218 buf++;
221 else
222 #endif
224 while (cnt)
226 if (i2c_put_data(*buf) < 0)
227 break;
228 cnt--;
229 buf++;
233 __i2c_send_stop();
234 i2c_close();
235 return count - cnt;
237 device_err:
238 timeout--;
239 __i2c_send_stop();
240 goto W_try_again;
242 W_timeout:
243 logf("Write I2C device 0x%2x failed.", device);
244 __i2c_send_stop();
245 i2c_close();
246 return -1;
249 void i2c_init(void)
251 __gpio_as_i2c();