xref: /freebsd/sys/dev/iicbus/iiconf.c (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998 Nicolas Souchu
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/lock.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/bus.h>
37 
38 #include <dev/iicbus/iiconf.h>
39 #include <dev/iicbus/iicbus.h>
40 #include "iicbus_if.h"
41 
42 /*
43  * Encode a system errno value into the IIC_Exxxxx space by setting the
44  * IIC_ERRNO marker bit, so that iic2errno() can turn it back into a plain
45  * system errno value later.  This lets controller- and bus-layer code get
46  * important system errno values (such as EINTR/ERESTART) back to the caller.
47  */
48 int
49 errno2iic(int errno)
50 {
51 	return ((errno == 0) ? 0 : errno | IIC_ERRNO);
52 }
53 
54 /*
55  * Translate IIC_Exxxxx status values to vaguely-equivelent errno values.
56  */
57 int
58 iic2errno(int iic_status)
59 {
60 	switch (iic_status) {
61 	case IIC_NOERR:         return (0);
62 	case IIC_EBUSERR:       return (EALREADY);
63 	case IIC_ENOACK:        return (EIO);
64 	case IIC_ETIMEOUT:      return (ETIMEDOUT);
65 	case IIC_EBUSBSY:       return (EWOULDBLOCK);
66 	case IIC_ESTATUS:       return (EPROTO);
67 	case IIC_EUNDERFLOW:    return (EIO);
68 	case IIC_EOVERFLOW:     return (EOVERFLOW);
69 	case IIC_ENOTSUPP:      return (EOPNOTSUPP);
70 	case IIC_ENOADDR:       return (EADDRNOTAVAIL);
71 	case IIC_ERESOURCE:     return (ENOMEM);
72 	default:
73 		/*
74 		 * If the high bit is set, that means it's a system errno value
75 		 * that was encoded into the IIC_Exxxxxx space by setting the
76 		 * IIC_ERRNO marker bit.  If lots of high-order bits are set,
77 		 * then it's one of the negative pseudo-errors such as ERESTART
78 		 * and we return it as-is.  Otherwise it's a plain "small
79 		 * positive integer" errno, so just remove the IIC_ERRNO marker
80 		 * bit.  If it's some unknown number without the high bit set,
81 		 * there isn't much we can do except call it an I/O error.
82 		 */
83 		if ((iic_status & IIC_ERRNO) == 0)
84 			return (EIO);
85 		if ((iic_status & 0xFFFF0000) != 0)
86 			return (iic_status);
87 		return (iic_status & ~IIC_ERRNO);
88 	}
89 }
90 
91 /*
92  * iicbus_intr()
93  */
94 void
95 iicbus_intr(device_t bus, int event, char *buf)
96 {
97 	struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
98 
99 	/* call owner's intr routine */
100 	if (sc->owner)
101 		IICBUS_INTR(sc->owner, event, buf);
102 
103 	return;
104 }
105 
106 static int
107 iicbus_poll(struct iicbus_softc *sc, int how)
108 {
109 	int error;
110 
111 	IICBUS_ASSERT_LOCKED(sc);
112 	switch (how & IIC_INTRWAIT) {
113 	case IIC_WAIT | IIC_INTR:
114 		error = mtx_sleep(sc, &sc->lock, IICPRI|PCATCH, "iicreq", 0);
115 		break;
116 
117 	case IIC_WAIT | IIC_NOINTR:
118 		error = mtx_sleep(sc, &sc->lock, IICPRI, "iicreq", 0);
119 		break;
120 
121 	default:
122 		return (IIC_EBUSBSY);
123 	}
124 
125 	return (errno2iic(error));
126 }
127 
128 /*
129  * iicbus_request_bus()
130  *
131  * Allocate the device to perform transfers.
132  *
133  * how	: IIC_WAIT or IIC_DONTWAIT
134  */
135 int
136 iicbus_request_bus(device_t bus, device_t dev, int how)
137 {
138 	struct iic_reqbus_data reqdata;
139 	struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
140 	int error = 0;
141 
142 	IICBUS_LOCK(sc);
143 
144 	for (;;) {
145 		if (sc->owner == NULL)
146 			break;
147 		if ((how & IIC_RECURSIVE) && sc->owner == dev)
148 			break;
149 		if ((error = iicbus_poll(sc, how)) != 0)
150 			break;
151 	}
152 
153 	if (error == 0) {
154 		++sc->owncount;
155 		if (sc->owner == NULL) {
156 			sc->owner = dev;
157 			/*
158 			 * Mark the device busy while it owns the bus, to
159 			 * prevent detaching the device, bus, or hardware
160 			 * controller, until ownership is relinquished.  If the
161 			 * device is doing IO from its probe method before
162 			 * attaching, it cannot be busied; mark the bus busy.
163 			 */
164 			if (device_get_state(dev) < DS_ATTACHING)
165 				sc->busydev = bus;
166 			else
167 				sc->busydev = dev;
168 			device_busy(sc->busydev);
169 			/*
170 			 * Drop the lock around the call to the bus driver, it
171 			 * should be allowed to sleep in the IIC_WAIT case.
172 			 * Drivers might also need to grab locks that would
173 			 * cause a LOR if our lock is held.
174 			 */
175 			IICBUS_UNLOCK(sc);
176 			/* Ask the underlying layers if the request is ok */
177 			reqdata.dev = dev;
178 			reqdata.bus = bus;
179 			reqdata.flags = how | IIC_REQBUS_DEV;
180 			error = IICBUS_CALLBACK(device_get_parent(bus),
181 			    IIC_REQUEST_BUS, (caddr_t)&reqdata);
182 			IICBUS_LOCK(sc);
183 
184 			if (error != 0) {
185 				sc->owner = NULL;
186 				sc->owncount = 0;
187 				wakeup_one(sc);
188 				device_unbusy(sc->busydev);
189 			}
190 		}
191 	}
192 
193 	IICBUS_UNLOCK(sc);
194 
195 	return (error);
196 }
197 
198 /*
199  * iicbus_release_bus()
200  *
201  * Release the device allocated with iicbus_request_dev()
202  */
203 int
204 iicbus_release_bus(device_t bus, device_t dev)
205 {
206 	struct iic_reqbus_data reqdata;
207 	struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
208 
209 	IICBUS_LOCK(sc);
210 
211 	if (sc->owner != dev) {
212 		IICBUS_UNLOCK(sc);
213 		return (IIC_EBUSBSY);
214 	}
215 
216 	if (--sc->owncount == 0) {
217 		/* Drop the lock while informing the low-level driver. */
218 		IICBUS_UNLOCK(sc);
219 		reqdata.dev = dev;
220 		reqdata.bus = bus;
221 		reqdata.flags = IIC_REQBUS_DEV;
222 		IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS,
223 		    (caddr_t)&reqdata);
224 		IICBUS_LOCK(sc);
225 		sc->owner = NULL;
226 		wakeup_one(sc);
227 		device_unbusy(sc->busydev);
228 	}
229 	IICBUS_UNLOCK(sc);
230 	return (0);
231 }
232 
233 /*
234  * iicbus_started()
235  *
236  * Test if the iicbus is started by the controller
237  */
238 int
239 iicbus_started(device_t bus)
240 {
241 	struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
242 
243 	return (sc->started);
244 }
245 
246 /*
247  * iicbus_start()
248  *
249  * Send start condition to the slave addressed by 'slave'
250  */
251 int
252 iicbus_start(device_t bus, u_char slave, int timeout)
253 {
254 	struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
255 	int error = 0;
256 
257 	if (sc->started)
258 		return (IIC_ESTATUS); /* protocol error, bus already started */
259 
260 	if (!(error = IICBUS_START(device_get_parent(bus), slave, timeout)))
261 		sc->started = slave;
262 	else
263 		sc->started = 0;
264 
265 	return (error);
266 }
267 
268 /*
269  * iicbus_repeated_start()
270  *
271  * Send start condition to the slave addressed by 'slave'
272  */
273 int
274 iicbus_repeated_start(device_t bus, u_char slave, int timeout)
275 {
276 	struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
277 	int error = 0;
278 
279 	if (!sc->started)
280 		return (IIC_ESTATUS); /* protocol error, bus not started */
281 
282 	if (!(error = IICBUS_REPEATED_START(device_get_parent(bus), slave, timeout)))
283 		sc->started = slave;
284 	else
285 		sc->started = 0;
286 
287 	return (error);
288 }
289 
290 /*
291  * iicbus_stop()
292  *
293  * Send stop condition to the bus
294  */
295 int
296 iicbus_stop(device_t bus)
297 {
298 	struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
299 	int error = 0;
300 
301 	if (!sc->started)
302 		return (IIC_ESTATUS); /* protocol error, bus not started */
303 
304 	error = IICBUS_STOP(device_get_parent(bus));
305 
306 	/* refuse any further access */
307 	sc->started = 0;
308 
309 	return (error);
310 }
311 
312 /*
313  * iicbus_write()
314  *
315  * Write a block of data to the slave previously started by
316  * iicbus_start() call
317  */
318 int
319 iicbus_write(device_t bus, const char *buf, int len, int *sent, int timeout)
320 {
321 	struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
322 
323 	/* a slave must have been started for writing */
324 	if (sc->started == 0 || (sc->strict != 0 && (sc->started & LSB) != 0))
325 		return (IIC_ESTATUS);
326 
327 	return (IICBUS_WRITE(device_get_parent(bus), buf, len, sent, timeout));
328 }
329 
330 /*
331  * iicbus_read()
332  *
333  * Read a block of data from the slave previously started by
334  * iicbus_read() call
335  */
336 int
337 iicbus_read(device_t bus, char *buf, int len, int *read, int last, int delay)
338 {
339 	struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
340 
341 	/* a slave must have been started for reading */
342 	if (sc->started == 0 || (sc->strict != 0 && (sc->started & LSB) == 0))
343 		return (IIC_ESTATUS);
344 
345 	return (IICBUS_READ(device_get_parent(bus), buf, len, read, last, delay));
346 }
347 
348 /*
349  * iicbus_write_byte()
350  *
351  * Write a byte to the slave previously started by iicbus_start() call
352  */
353 int
354 iicbus_write_byte(device_t bus, char byte, int timeout)
355 {
356 	struct iicbus_softc *sc = device_get_softc(bus);
357 	char data = byte;
358 	int sent;
359 
360 	/* a slave must have been started for writing */
361 	if (sc->started == 0 || (sc->strict != 0 && (sc->started & LSB) != 0))
362 		return (IIC_ESTATUS);
363 
364 	return (iicbus_write(bus, &data, 1, &sent, timeout));
365 }
366 
367 /*
368  * iicbus_read_byte()
369  *
370  * Read a byte from the slave previously started by iicbus_start() call
371  */
372 int
373 iicbus_read_byte(device_t bus, char *byte, int timeout)
374 {
375 	struct iicbus_softc *sc = device_get_softc(bus);
376 	int read;
377 
378 	/* a slave must have been started for reading */
379 	if (sc->started == 0 || (sc->strict != 0 && (sc->started & LSB) == 0))
380 		return (IIC_ESTATUS);
381 
382 	return (iicbus_read(bus, byte, 1, &read, IIC_LAST_READ, timeout));
383 }
384 
385 /*
386  * iicbus_block_write()
387  *
388  * Write a block of data to slave ; start/stop protocol managed
389  */
390 int
391 iicbus_block_write(device_t bus, u_char slave, char *buf, int len, int *sent)
392 {
393 	u_char addr = slave & ~LSB;
394 	int error;
395 
396 	if ((error = iicbus_start(bus, addr, 0)))
397 		return (error);
398 
399 	error = iicbus_write(bus, buf, len, sent, 0);
400 
401 	iicbus_stop(bus);
402 
403 	return (error);
404 }
405 
406 /*
407  * iicbus_block_read()
408  *
409  * Read a block of data from slave ; start/stop protocol managed
410  */
411 int
412 iicbus_block_read(device_t bus, u_char slave, char *buf, int len, int *read)
413 {
414 	u_char addr = slave | LSB;
415 	int error;
416 
417 	if ((error = iicbus_start(bus, addr, 0)))
418 		return (error);
419 
420 	error = iicbus_read(bus, buf, len, read, IIC_LAST_READ, 0);
421 
422 	iicbus_stop(bus);
423 
424 	return (error);
425 }
426 
427 /*
428  * iicbus_transfer()
429  *
430  * Do an aribtrary number of transfers on the iicbus.  We pass these
431  * raw requests to the bridge driver.  If the bridge driver supports
432  * them directly, then it manages all the details.  If not, it can use
433  * the helper function iicbus_transfer_gen() which will do the
434  * transfers at a low level.
435  *
436  * Pointers passed in as part of iic_msg must be kernel pointers.
437  * Callers that have user addresses to manage must do so on their own.
438  */
439 int
440 iicbus_transfer(device_t bus, struct iic_msg *msgs, uint32_t nmsgs)
441 {
442 
443 	return (IICBUS_TRANSFER(device_get_parent(bus), msgs, nmsgs));
444 }
445 
446 int
447 iicbus_transfer_excl(device_t dev, struct iic_msg *msgs, uint32_t nmsgs,
448     int how)
449 {
450 	device_t bus;
451 	int error;
452 
453 	bus = device_get_parent(dev);
454 	error = iicbus_request_bus(bus, dev, how);
455 	if (error == 0)
456 		error = IICBUS_TRANSFER(bus, msgs, nmsgs);
457 	iicbus_release_bus(bus, dev);
458 	return (error);
459 }
460 
461 /*
462  * Generic version of iicbus_transfer that calls the appropriate
463  * routines to accomplish this.  See note above about acceptable
464  * buffer addresses.
465  */
466 int
467 iicbus_transfer_gen(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
468 {
469 	int i, error, lenread, lenwrote, nkid, rpstart, addr;
470 	device_t *children, bus;
471 	bool started;
472 
473 	if ((error = device_get_children(dev, &children, &nkid)) != 0)
474 		return (IIC_ERESOURCE);
475 	if (nkid != 1) {
476 		free(children, M_TEMP);
477 		return (IIC_ENOTSUPP);
478 	}
479 	bus = children[0];
480 	rpstart = 0;
481 	free(children, M_TEMP);
482 	started = false;
483 	for (i = 0, error = 0; i < nmsgs && error == 0; i++) {
484 		addr = msgs[i].slave;
485 		if (msgs[i].flags & IIC_M_RD)
486 			addr |= LSB;
487 		else
488 			addr &= ~LSB;
489 
490 		if (!(msgs[i].flags & IIC_M_NOSTART)) {
491 			if (rpstart)
492 				error = iicbus_repeated_start(bus, addr, 0);
493 			else
494 				error = iicbus_start(bus, addr, 0);
495 			if (error != 0)
496 				break;
497 			started = true;
498 		}
499 
500 		if (msgs[i].flags & IIC_M_RD)
501 			error = iicbus_read(bus, msgs[i].buf, msgs[i].len,
502 			    &lenread, IIC_LAST_READ, 0);
503 		else
504 			error = iicbus_write(bus, msgs[i].buf, msgs[i].len,
505 			    &lenwrote, 0);
506 		if (error != 0)
507 			break;
508 
509 		if (!(msgs[i].flags & IIC_M_NOSTOP)) {
510 			rpstart = 0;
511 			iicbus_stop(bus);
512 		} else {
513 			rpstart = 1;	/* Next message gets repeated start */
514 		}
515 	}
516 	if (error != 0 && started)
517 		iicbus_stop(bus);
518 	return (error);
519 }
520 
521 int
522 iicdev_readfrom(device_t slavedev, uint8_t regaddr, void *buffer,
523     uint16_t buflen, int waithow)
524 {
525 	struct iic_msg msgs[2];
526 	uint8_t slaveaddr;
527 
528 	/*
529 	 * Two transfers back to back with a repeat-start between them; first we
530 	 * write the address-within-device, then we read from the device.
531 	 */
532 	slaveaddr = iicbus_get_addr(slavedev);
533 
534 	msgs[0].slave = slaveaddr;
535 	msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
536 	msgs[0].len   = 1;
537 	msgs[0].buf   = &regaddr;
538 
539 	msgs[1].slave = slaveaddr;
540 	msgs[1].flags = IIC_M_RD;
541 	msgs[1].len   = buflen;
542 	msgs[1].buf   = buffer;
543 
544 	return (iicbus_transfer_excl(slavedev, msgs, nitems(msgs), waithow));
545 }
546 
547 int iicdev_writeto(device_t slavedev, uint8_t regaddr, void *buffer,
548     uint16_t buflen, int waithow)
549 {
550 	struct iic_msg msg;
551 	uint8_t local_buffer[32];
552 	uint8_t *bufptr;
553 	size_t bufsize;
554 	int error;
555 
556 	/*
557 	 * Ideally, we would do two transfers back to back with no stop or start
558 	 * between them using an array of 2 iic_msgs; first we'd write the
559 	 * address byte using the IIC_M_NOSTOP flag, then we write the data
560 	 * using IIC_M_NOSTART, all in a single transfer.  Unfortunately,
561 	 * several i2c hardware drivers don't support that (perhaps because the
562 	 * hardware itself can't support it).  So instead we gather the
563 	 * scattered bytes into a single buffer here before writing them using a
564 	 * single iic_msg.  This function is typically used to write a few bytes
565 	 * at a time, so we try to use a small local buffer on the stack, but
566 	 * fall back to allocating a temporary buffer when necessary.
567 	 */
568 
569 	bufsize = buflen + 1;
570 	if (bufsize <= sizeof(local_buffer)) {
571 		bufptr = local_buffer;
572 	} else {
573 		bufptr = malloc(bufsize, M_DEVBUF,
574 		    (waithow & IIC_WAIT) ? M_WAITOK : M_NOWAIT);
575 		if (bufptr == NULL)
576 			return (errno2iic(ENOMEM));
577 	}
578 
579 	bufptr[0] = regaddr;
580 	memcpy(&bufptr[1], buffer, buflen);
581 
582 	msg.slave = iicbus_get_addr(slavedev);
583 	msg.flags = IIC_M_WR;
584 	msg.len   = bufsize;
585 	msg.buf   = bufptr;
586 
587 	error = iicbus_transfer_excl(slavedev, &msg, 1, waithow);
588 
589 	if (bufptr != local_buffer)
590 		free(bufptr, M_DEVBUF);
591 
592 	return (error);
593 }
594