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