sync hh.org
[hh.org.git] / drivers / l3 / l3-algo-bit.c
blob1d53d1e3761b306e40fb19ca193a7e63a3ec25f9
1 /*
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
14 * clock is low.
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)
39 int i;
41 for (i = 0; i < 8; i++) {
42 setclk(adap, 0);
43 udelay(adap->data_hold);
44 setdat(adap, byte & 1);
45 udelay(adap->data_setup);
46 setclk(adap, 1);
47 udelay(adap->clock_high);
48 byte >>= 1;
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
55 * transfer.
57 static void sendbytes(struct l3_algo_bit_data *adap, const char *buf, int len)
59 int i;
61 for (i = 0; i < len; i++) {
62 if (i) {
63 udelay(adap->mode_hold);
64 setmode(adap, 0);
65 udelay(adap->mode);
67 setmode(adap, 1);
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;
80 int i;
82 for (i = 0; i < 8; i++) {
83 setclk(adap, 0);
84 udelay(adap->data_hold + adap->data_setup);
85 setclk(adap, 1);
86 if (getdat(adap))
87 byte |= 1 << i;
88 udelay(adap->clock_high);
91 return byte;
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
97 * transfer.
99 static void readbytes(struct l3_algo_bit_data *adap, char *buf, int len)
101 int i;
103 for (i = 0; i < len; i++) {
104 if (i) {
105 udelay(adap->mode_hold);
106 setmode(adap, 0);
108 setmode(adap, 1);
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;
117 int i;
120 * If we share an I2C bus, ensure that it is in STOP mode
122 setclk(adap, 1);
123 setdat(adap, 1);
124 setmode(adap, 1);
125 setdatout(adap);
126 udelay(adap->mode);
128 for (i = 0; i < num; i++) {
129 struct l3_msg *pmsg = &msgs[i];
131 if (!(pmsg->flags & L3_M_NOADDR)) {
132 setmode(adap, 0);
133 udelay(adap->mode_setup);
134 sendbyte(adap, pmsg->addr);
135 udelay(adap->mode_hold);
138 if (pmsg->flags & L3_M_RD) {
139 setdatin(adap);
140 readbytes(adap, pmsg->buf, pmsg->len);
141 } else {
142 setdatout(adap);
143 sendbytes(adap, pmsg->buf, pmsg->len);
148 * Ensure that we leave the bus in I2C stop mode.
150 setclk(adap, 1);
151 setdat(adap, 1);
152 setmode(adap, 0);
153 setdatin(adap);
155 return num;
158 static struct l3_algorithm l3_bit_algo = {
159 name: "L3 bit-shift algorithm",
160 xfer: l3_xfer,
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);