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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
The purpose of proposed change is to use single i2c-rockbox.h header and higher level functions i2c_write, i2c_read only. The bus_index may be defined in target specific config file. For example I2C_RTC_BUS, CODEC_I2C_BUS, PMU_I2C_BUS etc.
This will allow to drop target specific wrappers like fmradio_i2c* ones or wmcodec-{pp,coldfire}.c and make drivers utilizing i2c platform agnostic.


###############################################
#    Generic, public I2C interface prototypes #
#    firmware/export/i2c-rockbox.h            #
###############################################

/* hardware interface struct
 * 
 * In target tree there have to be platform specific implentation presen:
 * int i2c_hw_read(void *param,              Optional parameter pointer passed to lowlevel function
                   unsigned char i2c_addr,   i2c slave address (8bits ie 7bit address shifted 1 left)
                   int address,              optional submodule address (higher level than i2c) -1 if unused
                   unsigned char *buf,       pointer to buffer
                   int count);               number of bytes to read

 * int i2c_hw_write
 * void *param - optional parameter pointer passed to lowlevel function (set to NULL if unused)
 */
struct i2c_struct
{
    int (*i2c_hw_read)(void *, unsigned char, int, unsigned char *, int);
    int (*i2c_hw_write)(void *, unsigned char, int, const unsigned char *, int);
    void *param;
    int init;
};

/* arbitrary choosen */
#define  MAX_I2C_INTERFACES 5

/* Public I2C interface */
int i2c_write (int bus_index,
               unsigned char i2c_addr,
               int address,
               const unsigned char *buf,
               int count);
int i2c_read (int bus_index,
               unsigned char i2c_addr,
               int address,
               unsigned char *buf,
               int count);

int i2c_register_interface(int bus_index,
                           const struct i2c_struct *iface);

/* This one is implemented in target tree but is used globaly
 *  so we provide prototype here
 */

void i2c_init(void);

########################################################################
#    Generic, public i2c interface implementation                      #
#    firmware/drivers/i2c-rockbox.c                                    #
########################################################################

#include "system.h"
#include "stdbool.h"
#include "i2c-rockbox.h"

/* global array which holds ptrs to struct with lowlevel,
 * platform specific i2c functions
 */
static const struct i2c_struct *i2c_if[MAX_I2C_INTERFACES];

int i2c_write (int bus_index,
               unsigned char i2c_addr,
               int address,
               const unsigned char *buf,
               int count)
{
    const struct i2c_struct *iface;

    if ( bus_index >= MAX_I2C_INTERFACES )
        return -1;

    iface = i2c_if[bus_index];

    if (!iface->init)
        return -2;

    return iface->i2c_hw_write(iface->param, i2c_addr, address, buf, count);
}
int i2c_read (int bus_index,
               unsigned char i2c_addr,
               int address,
               unsigned char *buf,
               int count)
{
    const struct i2c_struct *iface;

    if ( bus_index >= MAX_I2C_INTERFACES )
        return -1;

    iface = i2c_if[bus_index];

    if (!iface->init)
       return -2;

    return iface->i2c_hw_read(iface->param, i2c_addr, address, buf, count);
}

int i2c_register_interface(int bus_index,
                           const struct i2c_struct *iface)
{
    if ( bus_index >= MAX_I2C_INTERFACES )
        return -1;

    i2c_if[bus_index] = iface;

    return 0;
}

###########################################################################################################
Example of platform specific implementation. This is modification of i2c-coldfire.c
firmware/export/i2c-coldfire.h moved to firmware/target/coldfire/i2c-target.h with striped off prototypes
Other i2c implementation should be fairly easy to adapt for proposed scheme as well. There is nothing which
stops us from registering different drivers for different bus_index.
###########################################################################################################

#include "cpu.h"
#include "kernel.h"
#include "logf.h"
#include "system.h"
#include "i2c-rockbox.h"
#include "i2c-target.h"

/* --- Local functions - declarations --- */

static int i2c_start(volatile unsigned char *iface);
static int i2c_wait_for_slave(volatile unsigned char *iface);
static int i2c_outb(volatile unsigned char *iface, unsigned char byte);
static inline void i2c_stop(volatile unsigned char *iface);

static int i2c_hw_write(void *param, unsigned char addr,
                        int address, const unsigned char *buf, int count);

static int i2c_hw_read(void *param, unsigned char addr,
                       int address, unsigned char *buf, int count);


/* --- Public functions - implementation --- */
void i2c_init(void)
{
    MFDR2 = 0x0d;
    MBCR2 = IEN;

    /* this HAVE TO be declared static to not
     * be placed on the stack
     */
    static const struct i2c_struct i2c_iface = {
        .i2c_hw_read = i2c_hw_read,
        .i2c_hw_write = i2c_hw_write,
        .param = (void *)I2C_IFACE_1,
        .init = 1
    };

    i2c_register_interface(0, &i2c_iface);
}

/* --- Local functions - implementation -- */

/* End I2C session on the given interface. */
static inline void i2c_stop(volatile unsigned char *iface)
{
    iface[O_MBCR] &= ~MSTA;
}

/*
 * Writes bytes to a I2C device.
 *
 * Returns number of bytes successfully sent or a negative value on error.
 */
static int i2c_hw_write(void *param, unsigned char addr, 
                        int address, const unsigned char *buf, int count)
{
    int i, rc;
    volatile unsigned char *iface = (volatile unsigned char *)param;

    if (count <= 0)
        return 0;

    rc = i2c_start(iface);
    if (rc < 0)
        return rc;

    rc = i2c_outb(iface, addr & 0xfe);
    if (rc < 0)
        return rc;
    if ( address > 0 )
    {
        rc = i2c_outb(iface, address);
        if (rc < 0)
            return rc;
    }

    for (i = 0; i < count; i++)
    {
        rc = i2c_outb(iface, *buf++);
        if (rc < 0)
            return rc;
    }
    i2c_stop(iface);

    return count;
}

/*
 * Reads bytes from a I2C device.
 *
 * Returns number of bytes successfully received or a negative value on error.
 */
static int i2c_hw_read(void *param, unsigned char addr, 
                       int address, unsigned char *buf, int count)
{
    int i, rc;
    volatile unsigned char *iface = (volatile unsigned char *)param;

    if (count <= 0)
        return 0;

    if ( address > 0)
    {
        rc = i2c_start(iface);
        if (rc < 0)
            return rc;

        rc = i2c_outb(iface, addr & 0xfe);
        if (rc < 0)
            return rc;

        rc = i2c_outb(iface, address);
        if (rc < 0)
            return rc;
    }

    rc = i2c_start(iface);
    if (rc < 0)
        return rc;

    rc = i2c_outb(iface, addr | 1);
    if (rc < 0)
        return rc;

    /* Switch to Rx mode */
    iface[O_MBCR] &= ~MTX;

    /* Turn on ACK generation if reading multiple bytes */
    if (count > 1)
        iface[O_MBCR] &= ~TXAK;

    /* Dummy read */
    rc = (int) iface[O_MBDR];

    for (i = count; i > 0; i--)
    {
        rc = i2c_wait_for_slave(iface);
        if (rc < 0)
            return rc;

        if (i == 2)
            /* Don't ACK the last byte to be read from the slave */
            iface[O_MBCR] |= TXAK;
        else if (i == 1)
            /* Generate STOP before reading last byte received */
            i2c_stop(iface);

        *buf++ = iface[O_MBDR];
    }
    return count;
}

/* Begin I2C session on the given interface.
 *
 * Returns 0 on success, negative value on error.
 */
static int i2c_start(volatile unsigned char *iface)
{
    /* Wait for bus to become free */
    int j = MAX_LOOP;
    while (--j && (iface[O_MBSR] & IBB))
        ;
    if (!j)
    {
        logf("i2c: bus is busy (iface=%08lX)", (uintptr_t)iface);
        return -1;
    }

    /* Generate START and prepare for write */
    iface[O_MBCR] |= (MSTA | TXAK | MTX);

    return 0; 
}

/* Wait for slave to act on given I2C interface.
 *
 * Returns 0 on success, negative value on error.
 */
static int i2c_wait_for_slave(volatile unsigned char *iface)
{
    int j = MAX_LOOP;
    while (--j && ! (iface[O_MBSR] & IIF))
        ;
    if (!j)
    {
        logf("i2c: IIF not set (iface=%08lX)", (uintptr_t)iface);
        i2c_stop(iface); 
        return -2;
    }

    /* Clear interrupt flag */
    iface[O_MBSR] &= ~IIF;

    return 0;
}

/* Write the given byte to the given I2C interface.
 *
 * Returns 0 on success, negative value on error.
 */
static int i2c_outb(volatile unsigned char *iface, unsigned char byte)
{
    int rc;

    iface[O_MBDR] = byte;

    rc = i2c_wait_for_slave(iface);
    if (rc < 0)
        return rc;

    /* Check that transfer is complete */
    if ( !(iface[O_MBSR] & ICF))
    {
        logf("i2c: transfer error (iface=%08lX)", (uintptr_t)iface);
        i2c_stop(iface); 
        return -3;
    }

    /* Check that the byte has been ACKed */
    if (iface[O_MBSR] & RXAK)
    {
        logf("i2c: no ACK (iface=%08lX)", (uintptr_t)iface);
        i2c_stop(iface); 
        return -4;
    }

    return 0;
}