summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/s5l8700/i2c-s5l8700.c
blob: 49dd0b98c060de2a6994a23deeab951d943652b5 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2009 by Bertrik Sikken
 *
 * 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 "kernel.h"
#include "i2c-s5l8700.h"

/*  Driver for the s5l8700 built-in I2C controller in master mode
    
    Both the i2c_read and i2c_write function take the following arguments:
    * slave, the address of the i2c slave device to read from / write to
    * address, optional sub-address in the i2c slave (unused if -1)
    * len, number of bytes to be transfered
    * data, pointer to data to be transfered
    A return value < 0 indicates an error.
    
    Note:
    * blocks the calling thread for the entire duraton of the i2c transfer but
      uses wakeup_wait/wakeup_signal to allow other threads to run.
    * ACK from slave is not checked, so functions never return an error
*/

static struct mutex i2c_mtx;

void i2c_init(void)
{
    mutex_init(&i2c_mtx);

    /* enable I2C pins */
    PCON10 = (2 << 2) |
             (2 << 0);

    /* enable I2C clock */
    PWRCON &= ~(1 << 5);

    /* initial config */
    IICADD = 0;
    IICCON = (1 << 7) | /* ACK_GEN */
             (0 << 6) | /* CLKSEL = PCLK/16 */
             (1 << 5) | /* INT_EN */
             (1 << 4) | /* IRQ clear */
             (3 << 0);  /* CK_REG */

    /* serial output on */
    IICSTAT = (1 << 4);
}

int i2c_write(unsigned char slave, int address, int len, const unsigned char *data)
{
    mutex_lock(&i2c_mtx);
    long timeout = current_tick + HZ / 50;

    /* START */
    IICDS = slave & ~1;
    IICSTAT = 0xF0;
    IICCON = 0xB3;
    while ((IICCON & 0x10) == 0)
        if (TIME_AFTER(current_tick, timeout))
        {
            mutex_unlock(&i2c_mtx);
            return 1;
        }

    
    if (address >= 0) {
        /* write address */
        IICDS = address;
        IICCON = 0xB3;
        while ((IICCON & 0x10) == 0)
            if (TIME_AFTER(current_tick, timeout))
            {
                mutex_unlock(&i2c_mtx);
                return 2;
            }
    }
    
    /* write data */
    while (len--) {
        IICDS = *data++;
        IICCON = 0xB3;
        while ((IICCON & 0x10) == 0)
            if (TIME_AFTER(current_tick, timeout))
            {
                mutex_unlock(&i2c_mtx);
                return 4;
            }
    }

    /* STOP */
    IICSTAT = 0xD0;
    IICCON = 0xB3;
    while ((IICSTAT & (1 << 5)) != 0)
        if (TIME_AFTER(current_tick, timeout))
        {
            mutex_unlock(&i2c_mtx);
            return 5;
        }
    
    mutex_unlock(&i2c_mtx);
    return 0;
}

int i2c_read(unsigned char slave, int address, int len, unsigned char *data)
{
    mutex_lock(&i2c_mtx);
    long timeout = current_tick + HZ / 50;

    if (address >= 0) {
        /* START */
        IICDS = slave & ~1;
        IICSTAT = 0xF0;
        IICCON = 0xB3;
        while ((IICCON & 0x10) == 0)
            if (TIME_AFTER(current_tick, timeout))
            {
                mutex_unlock(&i2c_mtx);
                return 1;
            }

        /* write address */
        IICDS = address;
        IICCON = 0xB3;
        while ((IICCON & 0x10) == 0)
            if (TIME_AFTER(current_tick, timeout))
            {
                mutex_unlock(&i2c_mtx);
                return 2;
            }
    }
    
    /* (repeated) START */
    IICDS = slave | 1;
    IICSTAT = 0xB0;
    IICCON = 0xB3;
    while ((IICCON & 0x10) == 0)
        if (TIME_AFTER(current_tick, timeout))
        {
            mutex_unlock(&i2c_mtx);
            return 3;
        }
    
    while (len--) {
        IICCON = (len == 0) ? 0x33 : 0xB3; /* NAK or ACK */
        while ((IICCON & 0x10) == 0)
            if (TIME_AFTER(current_tick, timeout))
            {
                mutex_unlock(&i2c_mtx);
                return 4;
            }
        *data++ = IICDS;
    }

    /* STOP */
    IICSTAT = 0x90;
    IICCON = 0xB3;
    while ((IICSTAT & (1 << 5)) != 0)
        if (TIME_AFTER(current_tick, timeout))
        {
            mutex_unlock(&i2c_mtx);
            return 5;
        }
    
    mutex_unlock(&i2c_mtx);
    return 0;
}