2 * Coral-P(A)/Lime I2C adapter driver
4 * (C) 2011 DENX Software Engineering, Anatolij Gustschin <agust@denx.de>
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.
13 #include <linux/i2c.h>
16 #include "mb862xxfb.h"
17 #include "mb862xx_reg.h"
19 static int mb862xx_i2c_wait_event(struct i2c_adapter *adap)
21 struct mb862xxfb_par *par = adap->algo_data;
26 reg = inreg(i2c, GC_I2C_BCR);
27 if (reg & (I2C_INT | I2C_BER))
31 return (reg & I2C_BER) ? 0 : 1;
34 static int mb862xx_i2c_do_address(struct i2c_adapter *adap, int addr)
36 struct mb862xxfb_par *par = adap->algo_data;
38 outreg(i2c, GC_I2C_DAR, addr);
39 outreg(i2c, GC_I2C_CCR, I2C_CLOCK_AND_ENABLE);
40 outreg(i2c, GC_I2C_BCR, par->i2c_rs ? I2C_REPEATED_START : I2C_START);
41 if (!mb862xx_i2c_wait_event(adap))
43 par->i2c_rs = !(inreg(i2c, GC_I2C_BSR) & I2C_LRB);
47 static int mb862xx_i2c_write_byte(struct i2c_adapter *adap, u8 byte)
49 struct mb862xxfb_par *par = adap->algo_data;
51 outreg(i2c, GC_I2C_DAR, byte);
52 outreg(i2c, GC_I2C_BCR, I2C_START);
53 if (!mb862xx_i2c_wait_event(adap))
55 return !(inreg(i2c, GC_I2C_BSR) & I2C_LRB);
58 static int mb862xx_i2c_read_byte(struct i2c_adapter *adap, u8 *byte, int last)
60 struct mb862xxfb_par *par = adap->algo_data;
62 outreg(i2c, GC_I2C_BCR, I2C_START | (last ? 0 : I2C_ACK));
63 if (!mb862xx_i2c_wait_event(adap))
65 *byte = inreg(i2c, GC_I2C_DAR);
69 void mb862xx_i2c_stop(struct i2c_adapter *adap)
71 struct mb862xxfb_par *par = adap->algo_data;
73 outreg(i2c, GC_I2C_BCR, I2C_STOP);
74 outreg(i2c, GC_I2C_CCR, I2C_DISABLE);
78 static int mb862xx_i2c_read(struct i2c_adapter *adap, struct i2c_msg *m)
81 int last = m->len - 1;
83 for (i = 0; i < m->len; i++) {
84 if (!mb862xx_i2c_read_byte(adap, &m->buf[i], i == last)) {
92 static int mb862xx_i2c_write(struct i2c_adapter *adap, struct i2c_msg *m)
96 for (i = 0; i < m->len; i++) {
97 if (!mb862xx_i2c_write_byte(adap, m->buf[i])) {
105 static int mb862xx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
108 struct mb862xxfb_par *par = adap->algo_data;
113 dev_dbg(par->dev, "%s: %d msgs\n", __func__, num);
115 for (i = 0; i < num; i++) {
118 dev_dbg(par->dev, "%s: null msgs\n", __func__);
122 if (m->flags & I2C_M_RD)
125 err = mb862xx_i2c_do_address(adap, addr);
128 if (m->flags & I2C_M_RD)
129 err = mb862xx_i2c_read(adap, m);
131 err = mb862xx_i2c_write(adap, m);
135 mb862xx_i2c_stop(adap);
137 return (err < 0) ? err : i;
140 static u32 mb862xx_func(struct i2c_adapter *adap)
142 return I2C_FUNC_SMBUS_BYTE_DATA;
145 static const struct i2c_algorithm mb862xx_algo = {
146 .master_xfer = mb862xx_xfer,
147 .functionality = mb862xx_func,
150 static struct i2c_adapter mb862xx_i2c_adapter = {
151 .name = "MB862xx I2C adapter",
152 .algo = &mb862xx_algo,
153 .owner = THIS_MODULE,
156 int mb862xx_i2c_init(struct mb862xxfb_par *par)
160 mb862xx_i2c_adapter.algo_data = par;
161 par->adap = &mb862xx_i2c_adapter;
163 ret = i2c_add_adapter(par->adap);
165 dev_err(par->dev, "failed to add %s\n",
166 mb862xx_i2c_adapter.name);
171 void mb862xx_i2c_exit(struct mb862xxfb_par *par)
174 i2c_del_adapter(par->adap);