summaryrefslogtreecommitdiffstats
path: root/firmware/target/mips/ingenic_jz47xx/i2c-jz4740.c
blob: dadea65599a4d85143ee0fc5a0bdef80004cbecb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2009 by Maurus Cuelenaere
 *
 * based on linux/arch/mips/jz4740/i2c.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "config.h"
#include "system.h"
#include "jz4740.h"
#include "logf.h"
#include "i2c.h"

/*
 * Jz4740 I2C routines.
 * 
 * Copyright (C) 2005,2006 Ingenic Semiconductor Inc.
 * Author: <lhhuang@ingenic.cn>
 *
 *  This program is free software; you can distribute it and/or modify it
 *  under the terms of the GNU General Public License (Version 2) as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
 *
 */

/* I2C protocol */
#define I2C_READ    1
#define I2C_WRITE   0

#define TIMEOUT     1000

/*
 * I2C bus protocol basic routines
 */
static int i2c_put_data(unsigned char data)
{
    unsigned int timeout = TIMEOUT*10;

    __i2c_write(data);
    __i2c_set_drf();
    while (__i2c_check_drf() != 0);
    while (!__i2c_transmit_ended());
    while (!__i2c_received_ack() && timeout)
        timeout--;

    if (timeout)
        return 0;
    else
        return -1;
}

#if 0
static int i2c_put_data_nack(unsigned char data)
{
    unsigned int timeout = TIMEOUT*10;

    __i2c_write(data);
    __i2c_set_drf();
    while (__i2c_check_drf() != 0);
    while (!__i2c_transmit_ended());
    while (timeout--);

    return 0;
}
#endif

static int i2c_get_data(unsigned char *data, int ack)
{
    int timeout = TIMEOUT*10;

    if (!ack)
        __i2c_send_nack();
    else
        __i2c_send_ack();

    while (__i2c_check_drf() == 0 && timeout)
        timeout--;

    if (timeout)
    {
        if (!ack)
            __i2c_send_stop();
        *data = __i2c_read();
        __i2c_clear_drf();
        return 0;
    }
    else
        return -1;
}

void i2c_setclk(unsigned int i2cclk)
{
    __i2c_set_clk(__cpm_get_i2sclk(), i2cclk);
}

/*
 * I2C interface
 */
static void i2c_open(void)
{
    __cpm_start_i2c();
    i2c_setclk(10000); /* default 10 KHz */
    __i2c_enable();
}

static void i2c_close(void)
{
    udelay(300); /* wait for STOP goes over. */
    __i2c_disable();
    __cpm_stop_i2c();
}

int i2c_read(int device, unsigned char *buf, int count)
{
    int cnt = count;
    int timeout = 5;

    device &= 0xFF;

    i2c_open();

L_try_again:
    if (timeout < 0)
        goto L_timeout;

    __i2c_send_nack();    /* Master does not send ACK, slave sends it */

    __i2c_send_start();
    if (i2c_put_data( (device << 1) | I2C_READ ) < 0)
        goto device_err;

    __i2c_send_ack();    /* Master sends ACK for continue reading */

    while (cnt)
    {
        if (cnt == 1)
        {
            if (i2c_get_data(buf, 0) < 0)
                break;
        }
        else
        {
            if (i2c_get_data(buf, 1) < 0)
                break;
        }
        cnt--;
        buf++;
    }

    __i2c_send_stop();
    i2c_close();
    return count - cnt;

device_err:
    timeout--;
    __i2c_send_stop();
    goto L_try_again;

L_timeout:
    __i2c_send_stop();
    logf("Read I2C device 0x%2x failed.", device);
    i2c_close();
    return -1;
}

int i2c_write(int device, const unsigned char *buf, int count)
{
    int cnt = count;
    int timeout = 5;

    device &= 0xFF;

    i2c_open();

    __i2c_send_nack();    /* Master does not send ACK, slave sends it */

W_try_again:
    if (timeout < 0)
        goto W_timeout;

    cnt = count;

    __i2c_send_start();
    if (i2c_put_data( (device << 1) | I2C_WRITE ) < 0)
        goto device_err;

#if 0 //CONFIG_JZ_TPANEL_ATA2508
    if (address == 0xff)
    {
        while (cnt)
        {
            if (i2c_put_data_nack(*buf) < 0)
                break;
            cnt--;
            buf++;
        }
    }
    else
#endif
    {
        while (cnt)
        {
            if (i2c_put_data(*buf) < 0)
                break;
            cnt--;
            buf++;
        }
    }

    __i2c_send_stop();
    i2c_close();
    return count - cnt;

device_err:
    timeout--;
    __i2c_send_stop();
    goto W_try_again;

W_timeout:
    logf("Write I2C device 0x%2x failed.", device);
    __i2c_send_stop();
    i2c_close();
    return -1;
}

void i2c_init(void)
{
    __gpio_as_i2c();
}