1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * i2c-algo-pcf.c i2c driver algorithms for PCF8584 adapters
4  *
5  *   Copyright (C) 1995-1997 Simon G. Vogl
6  *		   1998-2000 Hans Berglund
7  *
8  * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
9  * Frodo Looijaard <frodol@dds.nl>, and also from Martin Bailey
10  * <mbailey@littlefeet-inc.com>
11  *
12  * Partially rewriten by Oleg I. Vdovikin <vdovikin@jscc.ru> to handle multiple
13  * messages, proper stop/repstart signaling during receive, added detect code
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/delay.h>
19 #include <linux/errno.h>
20 #include <linux/i2c.h>
21 #include <linux/i2c-algo-pcf.h>
22 #include <linux/string_choices.h>
23 #include "i2c-algo-pcf.h"
24 
25 
26 #define DEB2(x) if (i2c_debug >= 2) x
27 #define DEB3(x) if (i2c_debug >= 3) x /* print several statistical values */
28 #define DEBPROTO(x) if (i2c_debug >= 9) x;
29 	/* debug the protocol by showing transferred bits */
30 #define DEF_TIMEOUT 16
31 
32 /*
33  * module parameters:
34  */
35 static int i2c_debug;
36 
37 /* setting states on the bus with the right timing: */
38 
39 #define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
40 #define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
41 #define get_own(adap) adap->getown(adap->data)
42 #define get_clock(adap) adap->getclock(adap->data)
43 #define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
44 #define i2c_inb(adap) adap->getpcf(adap->data, 0)
45 
46 /* other auxiliary functions */
47 
48 static void i2c_start(struct i2c_algo_pcf_data *adap)
49 {
50 	DEBPROTO(printk(KERN_DEBUG "S "));
51 	set_pcf(adap, 1, I2C_PCF_START);
52 }
53 
54 static void i2c_repstart(struct i2c_algo_pcf_data *adap)
55 {
56 	DEBPROTO(printk(" Sr "));
57 	set_pcf(adap, 1, I2C_PCF_REPSTART);
58 }
59 
60 static void i2c_stop(struct i2c_algo_pcf_data *adap)
61 {
62 	DEBPROTO(printk("P\n"));
63 	set_pcf(adap, 1, I2C_PCF_STOP);
64 }
65 
66 static void handle_lab(struct i2c_algo_pcf_data *adap, const int *status)
67 {
68 	DEB2(printk(KERN_INFO
69 		"i2c-algo-pcf.o: lost arbitration (CSR 0x%02x)\n",
70 		*status));
71 	/*
72 	 * Cleanup from LAB -- reset and enable ESO.
73 	 * This resets the PCF8584; since we've lost the bus, no
74 	 * further attempts should be made by callers to clean up
75 	 * (no i2c_stop() etc.)
76 	 */
77 	set_pcf(adap, 1, I2C_PCF_PIN);
78 	set_pcf(adap, 1, I2C_PCF_ESO);
79 	/*
80 	 * We pause for a time period sufficient for any running
81 	 * I2C transaction to complete -- the arbitration logic won't
82 	 * work properly until the next START is seen.
83 	 * It is assumed the bus driver or client has set a proper value.
84 	 *
85 	 * REVISIT: should probably use msleep instead of mdelay if we
86 	 * know we can sleep.
87 	 */
88 	if (adap->lab_mdelay)
89 		mdelay(adap->lab_mdelay);
90 
91 	DEB2(printk(KERN_INFO
92 		"i2c-algo-pcf.o: reset LAB condition (CSR 0x%02x)\n",
93 		get_pcf(adap, 1)));
94 }
95 
96 static int wait_for_bb(struct i2c_algo_pcf_data *adap)
97 {
98 
99 	int timeout = DEF_TIMEOUT;
100 	int status;
101 
102 	status = get_pcf(adap, 1);
103 
104 	while (!(status & I2C_PCF_BB) && --timeout) {
105 		udelay(100); /* wait for 100 us */
106 		status = get_pcf(adap, 1);
107 	}
108 
109 	if (timeout == 0) {
110 		printk(KERN_ERR "Timeout waiting for Bus Busy\n");
111 		return -ETIMEDOUT;
112 	}
113 
114 	return 0;
115 }
116 
117 static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status)
118 {
119 
120 	int timeout = DEF_TIMEOUT;
121 
122 	*status = get_pcf(adap, 1);
123 
124 	while ((*status & I2C_PCF_PIN) && --timeout) {
125 		adap->waitforpin(adap->data);
126 		*status = get_pcf(adap, 1);
127 	}
128 	if (*status & I2C_PCF_LAB) {
129 		handle_lab(adap, status);
130 		return -EINTR;
131 	}
132 
133 	if (timeout == 0)
134 		return -ETIMEDOUT;
135 
136 	return 0;
137 }
138 
139 /*
140  * This should perform the 'PCF8584 initialization sequence' as described
141  * in the Philips IC12 data book (1995, Aug 29).
142  * There should be a 30 clock cycle wait after reset, I assume this
143  * has been fulfilled.
144  * There should be a delay at the end equal to the longest I2C message
145  * to synchronize the BB-bit (in multimaster systems). How long is
146  * this? I assume 1 second is always long enough.
147  *
148  * vdovikin: added detect code for PCF8584
149  */
150 static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
151 {
152 	unsigned char temp;
153 
154 	DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: PCF state 0x%02x\n",
155 				get_pcf(adap, 1)));
156 
157 	/* S1=0x80: S0 selected, serial interface off */
158 	set_pcf(adap, 1, I2C_PCF_PIN);
159 	/*
160 	 * check to see S1 now used as R/W ctrl -
161 	 * PCF8584 does that when ESO is zero
162 	 */
163 	if (((temp = get_pcf(adap, 1)) & 0x7f) != (0)) {
164 		DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp));
165 		return -ENXIO; /* definitely not PCF8584 */
166 	}
167 
168 	/* load own address in S0, effective address is (own << 1) */
169 	i2c_outb(adap, get_own(adap));
170 	/* check it's really written */
171 	if ((temp = i2c_inb(adap)) != get_own(adap)) {
172 		DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp));
173 		return -ENXIO;
174 	}
175 
176 	/* S1=0xA0, next byte in S2 */
177 	set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
178 	/* check to see S2 now selected */
179 	if (((temp = get_pcf(adap, 1)) & 0x7f) != I2C_PCF_ES1) {
180 		DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp));
181 		return -ENXIO;
182 	}
183 
184 	/* load clock register S2 */
185 	i2c_outb(adap, get_clock(adap));
186 	/* check it's really written, the only 5 lowest bits does matter */
187 	if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) {
188 		DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp));
189 		return -ENXIO;
190 	}
191 
192 	/* Enable serial interface, idle, S0 selected */
193 	set_pcf(adap, 1, I2C_PCF_IDLE);
194 
195 	/* check to see PCF is really idled and we can access status register */
196 	if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) {
197 		DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp));
198 		return -ENXIO;
199 	}
200 
201 	printk(KERN_DEBUG "i2c-algo-pcf.o: detected and initialized PCF8584.\n");
202 
203 	return 0;
204 }
205 
206 static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
207 			 int count, int last)
208 {
209 	struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
210 	int wrcount, status, timeout;
211 
212 	for (wrcount=0; wrcount<count; ++wrcount) {
213 		DEB2(dev_dbg(&i2c_adap->dev, "i2c_write: writing %2.2X\n",
214 				buf[wrcount] & 0xff));
215 		i2c_outb(adap, buf[wrcount]);
216 		timeout = wait_for_pin(adap, &status);
217 		if (timeout) {
218 			if (timeout == -EINTR)
219 				return -EINTR; /* arbitration lost */
220 
221 			i2c_stop(adap);
222 			dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
223 			return -EREMOTEIO; /* got a better one ?? */
224 		}
225 		if (status & I2C_PCF_LRB) {
226 			i2c_stop(adap);
227 			dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
228 			return -EREMOTEIO; /* got a better one ?? */
229 		}
230 	}
231 	if (last)
232 		i2c_stop(adap);
233 	else
234 		i2c_repstart(adap);
235 
236 	return wrcount;
237 }
238 
239 static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
240 			 int count, int last)
241 {
242 	int i, status;
243 	struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
244 	int wfp;
245 
246 	/* increment number of bytes to read by one -- read dummy byte */
247 	for (i = 0; i <= count; i++) {
248 
249 		if ((wfp = wait_for_pin(adap, &status))) {
250 			if (wfp == -EINTR)
251 				return -EINTR; /* arbitration lost */
252 
253 			i2c_stop(adap);
254 			dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
255 			return -1;
256 		}
257 
258 		if ((status & I2C_PCF_LRB) && (i != count)) {
259 			i2c_stop(adap);
260 			dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
261 			return -1;
262 		}
263 
264 		if (i == count - 1) {
265 			set_pcf(adap, 1, I2C_PCF_ESO);
266 		} else if (i == count) {
267 			if (last)
268 				i2c_stop(adap);
269 			else
270 				i2c_repstart(adap);
271 		}
272 
273 		if (i)
274 			buf[i - 1] = i2c_inb(adap);
275 		else
276 			i2c_inb(adap); /* dummy read */
277 	}
278 
279 	return i - 1;
280 }
281 
282 
283 static int pcf_doAddress(struct i2c_algo_pcf_data *adap,
284 			 struct i2c_msg *msg)
285 {
286 	unsigned char addr = i2c_8bit_addr_from_msg(msg);
287 
288 	if (msg->flags & I2C_M_REV_DIR_ADDR)
289 		addr ^= 1;
290 	i2c_outb(adap, addr);
291 
292 	return 0;
293 }
294 
295 static int pcf_xfer(struct i2c_adapter *i2c_adap,
296 		    struct i2c_msg *msgs,
297 		    int num)
298 {
299 	struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
300 	struct i2c_msg *pmsg;
301 	int i;
302 	int ret=0, timeout, status;
303 
304 	if (adap->xfer_begin)
305 		adap->xfer_begin(adap->data);
306 
307 	/* Check for bus busy */
308 	timeout = wait_for_bb(adap);
309 	if (timeout) {
310 		DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
311 			    "Timeout waiting for BB in pcf_xfer\n");)
312 		i = -EIO;
313 		goto out;
314 	}
315 
316 	for (i = 0;ret >= 0 && i < num; i++) {
317 		pmsg = &msgs[i];
318 
319 		DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
320 		     str_read_write(pmsg->flags & I2C_M_RD),
321 		     pmsg->len, pmsg->addr, i + 1, num);)
322 
323 		ret = pcf_doAddress(adap, pmsg);
324 
325 		/* Send START */
326 		if (i == 0)
327 			i2c_start(adap);
328 
329 		/* Wait for PIN (pending interrupt NOT) */
330 		timeout = wait_for_pin(adap, &status);
331 		if (timeout) {
332 			if (timeout == -EINTR) {
333 				/* arbitration lost */
334 				i = -EINTR;
335 				goto out;
336 			}
337 			i2c_stop(adap);
338 			DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
339 				    "for PIN(1) in pcf_xfer\n");)
340 			i = -EREMOTEIO;
341 			goto out;
342 		}
343 
344 		/* Check LRB (last rcvd bit - slave ack) */
345 		if (status & I2C_PCF_LRB) {
346 			i2c_stop(adap);
347 			DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
348 			i = -EREMOTEIO;
349 			goto out;
350 		}
351 
352 		DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
353 			    i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
354 
355 		if (pmsg->flags & I2C_M_RD) {
356 			ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
357 					    (i + 1 == num));
358 
359 			if (ret != pmsg->len) {
360 				DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
361 					    "only read %d bytes.\n",ret));
362 			} else {
363 				DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
364 			}
365 		} else {
366 			ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
367 					    (i + 1 == num));
368 
369 			if (ret != pmsg->len) {
370 				DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
371 					    "only wrote %d bytes.\n",ret));
372 			} else {
373 				DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
374 			}
375 		}
376 	}
377 
378 out:
379 	if (adap->xfer_end)
380 		adap->xfer_end(adap->data);
381 	return i;
382 }
383 
384 static u32 pcf_func(struct i2c_adapter *adap)
385 {
386 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
387 	       I2C_FUNC_PROTOCOL_MANGLING;
388 }
389 
390 /* exported algorithm data: */
391 static const struct i2c_algorithm pcf_algo = {
392 	.xfer = pcf_xfer,
393 	.functionality = pcf_func,
394 };
395 
396 /*
397  * registering functions to load algorithms at runtime
398  */
399 int i2c_pcf_add_bus(struct i2c_adapter *adap)
400 {
401 	struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
402 	int rval;
403 
404 	DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
405 
406 	/* register new adapter to i2c module... */
407 	adap->algo = &pcf_algo;
408 
409 	if ((rval = pcf_init_8584(pcf_adap)))
410 		return rval;
411 
412 	rval = i2c_add_adapter(adap);
413 
414 	return rval;
415 }
416 EXPORT_SYMBOL(i2c_pcf_add_bus);
417 
418 MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
419 MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
420 MODULE_LICENSE("GPL");
421 
422 module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
423 MODULE_PARM_DESC(i2c_debug,
424 	"debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");
425