2 * i2c-algo-sgi.c: i2c driver algorithm used by the VINO (SGI Indy) and
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License version 2 as published by the Free Software Foundation.
8 * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/errno.h>
15 #include <linux/delay.h>
17 #include <linux/i2c.h>
18 #include <linux/i2c-algo-sgi.h>
21 #define SGI_I2C_FORCE_IDLE (0 << 0)
22 #define SGI_I2C_NOT_IDLE (1 << 0)
23 #define SGI_I2C_WRITE (0 << 1)
24 #define SGI_I2C_READ (1 << 1)
25 #define SGI_I2C_RELEASE_BUS (0 << 2)
26 #define SGI_I2C_HOLD_BUS (1 << 2)
27 #define SGI_I2C_XFER_DONE (0 << 4)
28 #define SGI_I2C_XFER_BUSY (1 << 4)
29 #define SGI_I2C_ACK (0 << 5)
30 #define SGI_I2C_NACK (1 << 5)
31 #define SGI_I2C_BUS_OK (0 << 7)
32 #define SGI_I2C_BUS_ERR (1 << 7)
34 #define get_control() adap->getctrl(adap->data)
35 #define set_control(val) adap->setctrl(adap->data, val)
36 #define read_data() adap->rdata(adap->data)
37 #define write_data(val) adap->wdata(adap->data, val)
40 static int wait_xfer_done(struct i2c_algo_sgi_data
*adap
)
44 for (i
= 0; i
< adap
->xfer_timeout
; i
++) {
45 if ((get_control() & SGI_I2C_XFER_BUSY
) == 0)
53 static int wait_ack(struct i2c_algo_sgi_data
*adap
)
57 if (wait_xfer_done(adap
))
59 for (i
= 0; i
< adap
->ack_timeout
; i
++) {
60 if ((get_control() & SGI_I2C_NACK
) == 0)
68 static int force_idle(struct i2c_algo_sgi_data
*adap
)
72 set_control(SGI_I2C_FORCE_IDLE
);
73 for (i
= 0; i
< adap
->xfer_timeout
; i
++) {
74 if ((get_control() & SGI_I2C_NOT_IDLE
) == 0)
80 if (get_control() & SGI_I2C_BUS_ERR
)
85 static int do_address(struct i2c_algo_sgi_data
*adap
, unsigned int addr
,
89 set_control(SGI_I2C_NOT_IDLE
);
90 /* Check if bus is idle, eventually force it to do so */
91 if (get_control() & SGI_I2C_NOT_IDLE
)
94 /* Write out the i2c chip address and specify operation */
95 set_control(SGI_I2C_HOLD_BUS
| SGI_I2C_WRITE
| SGI_I2C_NOT_IDLE
);
104 static int i2c_read(struct i2c_algo_sgi_data
*adap
, unsigned char *buf
,
109 set_control(SGI_I2C_HOLD_BUS
| SGI_I2C_READ
| SGI_I2C_NOT_IDLE
);
110 for (i
= 0; i
< len
; i
++) {
111 if (wait_xfer_done(adap
))
113 buf
[i
] = read_data();
115 set_control(SGI_I2C_RELEASE_BUS
| SGI_I2C_FORCE_IDLE
);
121 static int i2c_write(struct i2c_algo_sgi_data
*adap
, unsigned char *buf
,
126 /* We are already in write state */
127 for (i
= 0; i
< len
; i
++) {
135 static int sgi_xfer(struct i2c_adapter
*i2c_adap
, struct i2c_msg
*msgs
,
138 struct i2c_algo_sgi_data
*adap
= i2c_adap
->algo_data
;
142 for (i
= 0; !err
&& i
< num
; i
++) {
144 err
= do_address(adap
, p
->addr
, p
->flags
& I2C_M_RD
);
147 if (p
->flags
& I2C_M_RD
)
148 err
= i2c_read(adap
, p
->buf
, p
->len
);
150 err
= i2c_write(adap
, p
->buf
, p
->len
);
153 return (err
< 0) ? err
: i
;
156 static u32
sgi_func(struct i2c_adapter
*adap
)
158 return I2C_FUNC_SMBUS_EMUL
;
161 static const struct i2c_algorithm sgi_algo
= {
162 .master_xfer
= sgi_xfer
,
163 .functionality
= sgi_func
,
167 * registering functions to load algorithms at runtime
169 int i2c_sgi_add_bus(struct i2c_adapter
*adap
)
171 adap
->algo
= &sgi_algo
;
173 return i2c_add_adapter(adap
);
175 EXPORT_SYMBOL(i2c_sgi_add_bus
);
177 MODULE_AUTHOR("Ladislav Michl <ladis@linux-mips.org>");
178 MODULE_DESCRIPTION("I2C-Bus SGI algorithm");
179 MODULE_LICENSE("GPL");