1/*
2 * Copyright 2012 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Ben Skeggs
23 */
24#include "priv.h"
25
26#ifdef CONFIG_NOUVEAU_I2C_INTERNAL
27#define T_TIMEOUT  2200000
28#define T_RISEFALL 1000
29#define T_HOLD     5000
30
31static inline void
32i2c_drive_scl(struct nvkm_i2c_port *port, int state)
33{
34	port->func->drive_scl(port, state);
35}
36
37static inline void
38i2c_drive_sda(struct nvkm_i2c_port *port, int state)
39{
40	port->func->drive_sda(port, state);
41}
42
43static inline int
44i2c_sense_scl(struct nvkm_i2c_port *port)
45{
46	return port->func->sense_scl(port);
47}
48
49static inline int
50i2c_sense_sda(struct nvkm_i2c_port *port)
51{
52	return port->func->sense_sda(port);
53}
54
55static void
56i2c_delay(struct nvkm_i2c_port *port, u32 nsec)
57{
58	udelay((nsec + 500) / 1000);
59}
60
61static bool
62i2c_raise_scl(struct nvkm_i2c_port *port)
63{
64	u32 timeout = T_TIMEOUT / T_RISEFALL;
65
66	i2c_drive_scl(port, 1);
67	do {
68		i2c_delay(port, T_RISEFALL);
69	} while (!i2c_sense_scl(port) && --timeout);
70
71	return timeout != 0;
72}
73
74static int
75i2c_start(struct nvkm_i2c_port *port)
76{
77	int ret = 0;
78
79	if (!i2c_sense_scl(port) ||
80	    !i2c_sense_sda(port)) {
81		i2c_drive_scl(port, 0);
82		i2c_drive_sda(port, 1);
83		if (!i2c_raise_scl(port))
84			ret = -EBUSY;
85	}
86
87	i2c_drive_sda(port, 0);
88	i2c_delay(port, T_HOLD);
89	i2c_drive_scl(port, 0);
90	i2c_delay(port, T_HOLD);
91	return ret;
92}
93
94static void
95i2c_stop(struct nvkm_i2c_port *port)
96{
97	i2c_drive_scl(port, 0);
98	i2c_drive_sda(port, 0);
99	i2c_delay(port, T_RISEFALL);
100
101	i2c_drive_scl(port, 1);
102	i2c_delay(port, T_HOLD);
103	i2c_drive_sda(port, 1);
104	i2c_delay(port, T_HOLD);
105}
106
107static int
108i2c_bitw(struct nvkm_i2c_port *port, int sda)
109{
110	i2c_drive_sda(port, sda);
111	i2c_delay(port, T_RISEFALL);
112
113	if (!i2c_raise_scl(port))
114		return -ETIMEDOUT;
115	i2c_delay(port, T_HOLD);
116
117	i2c_drive_scl(port, 0);
118	i2c_delay(port, T_HOLD);
119	return 0;
120}
121
122static int
123i2c_bitr(struct nvkm_i2c_port *port)
124{
125	int sda;
126
127	i2c_drive_sda(port, 1);
128	i2c_delay(port, T_RISEFALL);
129
130	if (!i2c_raise_scl(port))
131		return -ETIMEDOUT;
132	i2c_delay(port, T_HOLD);
133
134	sda = i2c_sense_sda(port);
135
136	i2c_drive_scl(port, 0);
137	i2c_delay(port, T_HOLD);
138	return sda;
139}
140
141static int
142i2c_get_byte(struct nvkm_i2c_port *port, u8 *byte, bool last)
143{
144	int i, bit;
145
146	*byte = 0;
147	for (i = 7; i >= 0; i--) {
148		bit = i2c_bitr(port);
149		if (bit < 0)
150			return bit;
151		*byte |= bit << i;
152	}
153
154	return i2c_bitw(port, last ? 1 : 0);
155}
156
157static int
158i2c_put_byte(struct nvkm_i2c_port *port, u8 byte)
159{
160	int i, ret;
161	for (i = 7; i >= 0; i--) {
162		ret = i2c_bitw(port, !!(byte & (1 << i)));
163		if (ret < 0)
164			return ret;
165	}
166
167	ret = i2c_bitr(port);
168	if (ret == 1) /* nack */
169		ret = -EIO;
170	return ret;
171}
172
173static int
174i2c_addr(struct nvkm_i2c_port *port, struct i2c_msg *msg)
175{
176	u32 addr = msg->addr << 1;
177	if (msg->flags & I2C_M_RD)
178		addr |= 1;
179	return i2c_put_byte(port, addr);
180}
181
182static int
183i2c_bit_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
184{
185	struct nvkm_i2c_port *port = adap->algo_data;
186	struct i2c_msg *msg = msgs;
187	int ret = 0, mcnt = num;
188
189	ret = nvkm_i2c(port)->acquire(port, nsecs_to_jiffies(T_TIMEOUT));
190	if (ret)
191		return ret;
192
193	while (!ret && mcnt--) {
194		u8 remaining = msg->len;
195		u8 *ptr = msg->buf;
196
197		ret = i2c_start(port);
198		if (ret == 0)
199			ret = i2c_addr(port, msg);
200
201		if (msg->flags & I2C_M_RD) {
202			while (!ret && remaining--)
203				ret = i2c_get_byte(port, ptr++, !remaining);
204		} else {
205			while (!ret && remaining--)
206				ret = i2c_put_byte(port, *ptr++);
207		}
208
209		msg++;
210	}
211
212	i2c_stop(port);
213	nvkm_i2c(port)->release(port);
214	return (ret < 0) ? ret : num;
215}
216#else
217static int
218i2c_bit_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
219{
220	return -ENODEV;
221}
222#endif
223
224static u32
225i2c_bit_func(struct i2c_adapter *adap)
226{
227	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
228}
229
230const struct i2c_algorithm nvkm_i2c_bit_algo = {
231	.master_xfer = i2c_bit_xfer,
232	.functionality = i2c_bit_func
233};
234