1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
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
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.
56 * I2C bus protocol basic routines
58 static int i2c_put_data(unsigned char data
)
60 unsigned int timeout
= TIMEOUT
*10;
64 while (__i2c_check_drf() != 0);
65 while (!__i2c_transmit_ended());
66 while (!__i2c_received_ack() && timeout
)
76 static int i2c_put_data_nack(unsigned char data
)
78 unsigned int timeout
= TIMEOUT
*10;
82 while (__i2c_check_drf() != 0);
83 while (!__i2c_transmit_ended());
90 static int i2c_get_data(unsigned char *data
, int ack
)
92 int timeout
= TIMEOUT
*10;
99 while (__i2c_check_drf() == 0 && timeout
)
106 *data
= __i2c_read();
114 void i2c_setclk(unsigned int i2cclk
)
116 __i2c_set_clk(__cpm_get_i2sclk(), i2cclk
);
122 static void i2c_open(void)
125 i2c_setclk(10000); /* default 10 KHz */
129 static void i2c_close(void)
131 udelay(300); /* wait for STOP goes over. */
136 int i2c_read(int device
, unsigned char *buf
, int count
)
149 __i2c_send_nack(); /* Master does not send ACK, slave sends it */
152 if (i2c_put_data( (device
<< 1) | I2C_READ
) < 0)
155 __i2c_send_ack(); /* Master sends ACK for continue reading */
161 if (i2c_get_data(buf
, 0) < 0)
166 if (i2c_get_data(buf
, 1) < 0)
184 logf("Read I2C device 0x%2x failed.", device
);
189 int i2c_write(int device
, unsigned char *buf
, int count
)
198 __i2c_send_nack(); /* Master does not send ACK, slave sends it */
207 if (i2c_put_data( (device
<< 1) | I2C_WRITE
) < 0)
210 #if 0 //CONFIG_JZ_TPANEL_ATA2508
215 if (i2c_put_data_nack(*buf
) < 0)
226 if (i2c_put_data(*buf
) < 0)
243 logf("Write I2C device 0x%2x failed.", device
);