2 * L3 bus algorithm module.
4 * Copyright (C) 2001 Russell King, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Note that L3 buses can share the same pins as I2C buses, so we must
11 * _not_ generate an I2C start condition. An I2C start condition is
12 * defined as a high-to-low transition of the data line while the clock
13 * is high. Therefore, we must only change the data line while the
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/delay.h>
19 #include <linux/slab.h>
20 #include <linux/init.h>
21 #include <linux/errno.h>
22 #include <linux/sched.h>
23 #include <linux/l3/l3.h>
24 #include <linux/l3/algo-bit.h>
26 #define setdat(adap,val) adap->setdat(adap->data, val)
27 #define setclk(adap,val) adap->setclk(adap->data, val)
28 #define setmode(adap,val) adap->setmode(adap->data, val)
29 #define setdatin(adap) adap->setdir(adap->data, 1)
30 #define setdatout(adap) adap->setdir(adap->data, 0)
31 #define getdat(adap) adap->getdat(adap->data)
34 * Send one byte of data to the chip. Data is latched into the chip on
35 * the rising edge of the clock.
37 static void sendbyte(struct l3_algo_bit_data
*adap
, unsigned int byte
)
41 for (i
= 0; i
< 8; i
++) {
43 udelay(adap
->data_hold
);
44 setdat(adap
, byte
& 1);
45 udelay(adap
->data_setup
);
47 udelay(adap
->clock_high
);
53 * Send a set of bytes to the chip. We need to pulse the MODE line
54 * between each byte, but never at the start nor at the end of the
57 static void sendbytes(struct l3_algo_bit_data
*adap
, const char *buf
, int len
)
61 for (i
= 0; i
< len
; i
++) {
63 udelay(adap
->mode_hold
);
68 udelay(adap
->mode_setup
);
69 sendbyte(adap
, buf
[i
]);
74 * Read one byte of data from the chip. Data is latched into the chip on
75 * the rising edge of the clock.
77 static unsigned int readbyte(struct l3_algo_bit_data
*adap
)
79 unsigned int byte
= 0;
82 for (i
= 0; i
< 8; i
++) {
84 udelay(adap
->data_hold
+ adap
->data_setup
);
88 udelay(adap
->clock_high
);
95 * Read a set of bytes from the chip. We need to pulse the MODE line
96 * between each byte, but never at the start nor at the end of the
99 static void readbytes(struct l3_algo_bit_data
*adap
, char *buf
, int len
)
103 for (i
= 0; i
< len
; i
++) {
105 udelay(adap
->mode_hold
);
109 udelay(adap
->mode_setup
);
110 buf
[i
] = readbyte(adap
);
114 static int l3_xfer(struct l3_adapter
*l3_adap
, struct l3_msg msgs
[], int num
)
116 struct l3_algo_bit_data
*adap
= l3_adap
->algo_data
;
120 * If we share an I2C bus, ensure that it is in STOP mode
128 for (i
= 0; i
< num
; i
++) {
129 struct l3_msg
*pmsg
= &msgs
[i
];
131 if (!(pmsg
->flags
& L3_M_NOADDR
)) {
133 udelay(adap
->mode_setup
);
134 sendbyte(adap
, pmsg
->addr
);
135 udelay(adap
->mode_hold
);
138 if (pmsg
->flags
& L3_M_RD
) {
140 readbytes(adap
, pmsg
->buf
, pmsg
->len
);
143 sendbytes(adap
, pmsg
->buf
, pmsg
->len
);
148 * Ensure that we leave the bus in I2C stop mode.
158 static struct l3_algorithm l3_bit_algo
= {
159 name
: "L3 bit-shift algorithm",
163 int l3_bit_add_bus(struct l3_adapter
*adap
)
165 adap
->algo
= &l3_bit_algo
;
166 return l3_add_adapter(adap
);
169 int l3_bit_del_bus(struct l3_adapter
*adap
)
171 return l3_del_adapter(adap
);
174 EXPORT_SYMBOL(l3_bit_add_bus
);
175 EXPORT_SYMBOL(l3_bit_del_bus
);