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