blob: 934081d2b7ae7f199e7631e98a46025bb2e0e5c2 [file] [log] [blame]
Anatolij Gustschinf8a6b1f2011-05-24 15:19:48 +02001/*
2 * Coral-P(A)/Lime I2C adapter driver
3 *
4 * (C) 2011 DENX Software Engineering, Anatolij Gustschin <agust@denx.de>
5 *
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.
9 *
10 */
11
12#include <linux/fb.h>
13#include <linux/i2c.h>
14#include <linux/io.h>
Stephen Rothwell520fd842011-05-26 11:09:10 +100015#include <linux/delay.h>
Anatolij Gustschinf8a6b1f2011-05-24 15:19:48 +020016
17#include "mb862xxfb.h"
18#include "mb862xx_reg.h"
19
20static int mb862xx_i2c_wait_event(struct i2c_adapter *adap)
21{
22 struct mb862xxfb_par *par = adap->algo_data;
23 u32 reg;
24
25 do {
Anatolij Gustschin363d58f2011-09-01 17:53:41 +020026 udelay(10);
Anatolij Gustschinf8a6b1f2011-05-24 15:19:48 +020027 reg = inreg(i2c, GC_I2C_BCR);
28 if (reg & (I2C_INT | I2C_BER))
29 break;
30 } while (1);
31
32 return (reg & I2C_BER) ? 0 : 1;
33}
34
35static int mb862xx_i2c_do_address(struct i2c_adapter *adap, int addr)
36{
37 struct mb862xxfb_par *par = adap->algo_data;
38
39 outreg(i2c, GC_I2C_DAR, addr);
40 outreg(i2c, GC_I2C_CCR, I2C_CLOCK_AND_ENABLE);
41 outreg(i2c, GC_I2C_BCR, par->i2c_rs ? I2C_REPEATED_START : I2C_START);
42 if (!mb862xx_i2c_wait_event(adap))
43 return -EIO;
44 par->i2c_rs = !(inreg(i2c, GC_I2C_BSR) & I2C_LRB);
45 return par->i2c_rs;
46}
47
48static int mb862xx_i2c_write_byte(struct i2c_adapter *adap, u8 byte)
49{
50 struct mb862xxfb_par *par = adap->algo_data;
51
52 outreg(i2c, GC_I2C_DAR, byte);
53 outreg(i2c, GC_I2C_BCR, I2C_START);
54 if (!mb862xx_i2c_wait_event(adap))
55 return -EIO;
56 return !(inreg(i2c, GC_I2C_BSR) & I2C_LRB);
57}
58
59static int mb862xx_i2c_read_byte(struct i2c_adapter *adap, u8 *byte, int last)
60{
61 struct mb862xxfb_par *par = adap->algo_data;
62
63 outreg(i2c, GC_I2C_BCR, I2C_START | (last ? 0 : I2C_ACK));
64 if (!mb862xx_i2c_wait_event(adap))
65 return 0;
66 *byte = inreg(i2c, GC_I2C_DAR);
67 return 1;
68}
69
70void mb862xx_i2c_stop(struct i2c_adapter *adap)
71{
72 struct mb862xxfb_par *par = adap->algo_data;
73
74 outreg(i2c, GC_I2C_BCR, I2C_STOP);
75 outreg(i2c, GC_I2C_CCR, I2C_DISABLE);
76 par->i2c_rs = 0;
77}
78
79static int mb862xx_i2c_read(struct i2c_adapter *adap, struct i2c_msg *m)
80{
81 int i, ret = 0;
82 int last = m->len - 1;
83
84 for (i = 0; i < m->len; i++) {
85 if (!mb862xx_i2c_read_byte(adap, &m->buf[i], i == last)) {
86 ret = -EIO;
87 break;
88 }
89 }
90 return ret;
91}
92
93static int mb862xx_i2c_write(struct i2c_adapter *adap, struct i2c_msg *m)
94{
95 int i, ret = 0;
96
97 for (i = 0; i < m->len; i++) {
98 if (!mb862xx_i2c_write_byte(adap, m->buf[i])) {
99 ret = -EIO;
100 break;
101 }
102 }
103 return ret;
104}
105
106static int mb862xx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
107 int num)
108{
109 struct mb862xxfb_par *par = adap->algo_data;
110 struct i2c_msg *m;
111 int addr;
112 int i = 0, err = 0;
113
114 dev_dbg(par->dev, "%s: %d msgs\n", __func__, num);
115
116 for (i = 0; i < num; i++) {
117 m = &msgs[i];
118 if (!m->len) {
119 dev_dbg(par->dev, "%s: null msgs\n", __func__);
120 continue;
121 }
122 addr = m->addr;
123 if (m->flags & I2C_M_RD)
124 addr |= 1;
125
126 err = mb862xx_i2c_do_address(adap, addr);
127 if (err < 0)
128 break;
129 if (m->flags & I2C_M_RD)
130 err = mb862xx_i2c_read(adap, m);
131 else
132 err = mb862xx_i2c_write(adap, m);
133 }
134
135 if (i)
136 mb862xx_i2c_stop(adap);
137
138 return (err < 0) ? err : i;
139}
140
141static u32 mb862xx_func(struct i2c_adapter *adap)
142{
143 return I2C_FUNC_SMBUS_BYTE_DATA;
144}
145
146static const struct i2c_algorithm mb862xx_algo = {
147 .master_xfer = mb862xx_xfer,
148 .functionality = mb862xx_func,
149};
150
151static struct i2c_adapter mb862xx_i2c_adapter = {
152 .name = "MB862xx I2C adapter",
153 .algo = &mb862xx_algo,
154 .owner = THIS_MODULE,
155};
156
157int mb862xx_i2c_init(struct mb862xxfb_par *par)
158{
159 int ret;
160
161 mb862xx_i2c_adapter.algo_data = par;
162 par->adap = &mb862xx_i2c_adapter;
163
164 ret = i2c_add_adapter(par->adap);
165 if (ret < 0) {
166 dev_err(par->dev, "failed to add %s\n",
167 mb862xx_i2c_adapter.name);
168 }
169 return ret;
170}
171
172void mb862xx_i2c_exit(struct mb862xxfb_par *par)
173{
174 if (par->adap) {
175 i2c_del_adapter(par->adap);
176 par->adap = NULL;
177 }
178}