Home | History | Annotate | Line # | Download | only in i2c
i2c_exec.c revision 1.13
      1 /*	$NetBSD: i2c_exec.c,v 1.13 2019/12/22 23:23:32 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: i2c_exec.c,v 1.13 2019/12/22 23:23:32 thorpej Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/cpu.h>
     44 #include <sys/device.h>
     45 #include <sys/module.h>
     46 #include <sys/event.h>
     47 #include <sys/conf.h>
     48 #include <sys/kernel.h>
     49 
     50 #define	_I2C_PRIVATE
     51 #include <dev/i2c/i2cvar.h>
     52 
     53 static uint8_t	iic_smbus_crc8(uint16_t);
     54 static uint8_t	iic_smbus_pec(int, uint8_t *, uint8_t *);
     55 
     56 static int	i2cexec_modcmd(modcmd_t, void *);
     57 
     58 static inline int
     59 iic_op_flags(int flags)
     60 {
     61 
     62 	return flags | (cold ? I2C_F_POLL : 0);
     63 }
     64 
     65 /*
     66  * iic_tag_init:
     67  *
     68  *	Perform some basic initialization of the i2c controller tag.
     69  */
     70 void
     71 iic_tag_init(i2c_tag_t tag)
     72 {
     73 
     74 	memset(tag, 0, sizeof(*tag));
     75 	mutex_init(&tag->ic_bus_lock, MUTEX_DEFAULT, IPL_NONE);
     76 	LIST_INIT(&tag->ic_list);
     77 	LIST_INIT(&tag->ic_proc_list);
     78 }
     79 
     80 /*
     81  * iic_tag_fini:
     82  *
     83  *	Teardown of the i2c controller tag.
     84  */
     85 void
     86 iic_tag_fini(i2c_tag_t tag)
     87 {
     88 
     89 	mutex_destroy(&tag->ic_bus_lock);
     90 }
     91 
     92 /*
     93  * iic_acquire_bus:
     94  *
     95  *	Acquire the I2C bus for use by a client.
     96  */
     97 int
     98 iic_acquire_bus(i2c_tag_t tag, int flags)
     99 {
    100 
    101 	KASSERT(!cpu_intr_p());
    102 
    103 	flags = iic_op_flags(flags);
    104 
    105 	if (flags & I2C_F_POLL) {
    106 		/*
    107 		 * Polling should only be used in rare and/or
    108 		 * extreme circumstances; most i2c clients
    109 		 * should be allowed to sleep.
    110 		 *
    111 		 * Really, the ONLY user of I2C_F_POLL should be
    112 		 * "when cold", i.e. during early autoconfiguration
    113 		 * when there is only proc0, and we might have to
    114 		 * read SEEPROMs, etc.  There should be no other
    115 		 * users interfering with our access of the i2c bus
    116 		 * in that case.
    117 		 */
    118 		if (mutex_tryenter(&tag->ic_bus_lock) == 0) {
    119 			return EBUSY;
    120 		}
    121 	} else {
    122 		/*
    123 		 * N.B. We implement this as a mutex that we hold across
    124 		 * across a series of requests beause we'd like to get the
    125 		 * priority boost if a higher-priority process wants the
    126 		 * i2c bus while we're asleep waiting for the controller
    127 		 * to perform the I/O.
    128 		 *
    129 		 * XXXJRT Disable preemption here?  We'd like to keep
    130 		 * the CPU while holding this resource, unless we release
    131 		 * it voluntarily (which should only happen while waiting
    132 		 * for a controller to complete I/O).
    133 		 */
    134 		mutex_enter(&tag->ic_bus_lock);
    135 	}
    136 
    137 	int error = 0;
    138 	if (tag->ic_acquire_bus) {
    139 		error = (*tag->ic_acquire_bus)(tag->ic_cookie, flags);
    140 	}
    141 
    142 	return error;
    143 }
    144 
    145 /*
    146  * iic_release_bus:
    147  *
    148  *	Relese the I2C bus, allowing another client to use it.
    149  */
    150 void
    151 iic_release_bus(i2c_tag_t tag, int flags)
    152 {
    153 
    154 	KASSERT(!cpu_intr_p());
    155 
    156 	flags = iic_op_flags(flags);
    157 
    158 	if (tag->ic_release_bus) {
    159 		(*tag->ic_release_bus)(tag->ic_cookie, flags);
    160 	}
    161 
    162 	mutex_exit(&tag->ic_bus_lock);
    163 }
    164 
    165 /*
    166  * iic_exec:
    167  *
    168  *	Simplified I2C client interface engine.
    169  *
    170  *	This and the SMBus routines are the preferred interface for
    171  *	client access to I2C/SMBus, since many automated controllers
    172  *	do not provide access to the low-level primitives of the I2C
    173  *	bus protocol.
    174  */
    175 int
    176 iic_exec(i2c_tag_t tag, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
    177     size_t cmdlen, void *vbuf, size_t buflen, int flags)
    178 {
    179 	const uint8_t *cmd = vcmd;
    180 	uint8_t *buf = vbuf;
    181 	int error;
    182 	size_t len;
    183 
    184 	KASSERT(!cpu_intr_p());
    185 
    186 	flags = iic_op_flags(flags);
    187 
    188 	if ((flags & I2C_F_PEC) && cmdlen > 0 && tag->ic_exec != NULL) {
    189 		uint8_t data[33]; /* XXX */
    190 		uint8_t b[3];
    191 
    192 		b[0] = addr << 1;
    193 		b[1] = cmd[0];
    194 
    195 		switch (buflen) {
    196 		case 0:
    197 			data[0] = iic_smbus_pec(2, b, NULL);
    198 			buflen++;
    199 			break;
    200 		case 1:
    201 			b[2] = buf[0];
    202 			data[0] = iic_smbus_pec(3, b, NULL);
    203 			data[1] = b[2];
    204 			buflen++;
    205 			break;
    206 		case 2:
    207 			break;
    208 		default:
    209 			KASSERT(buflen+1 < sizeof(data));
    210 			memcpy(data, vbuf, buflen);
    211 			data[buflen] = iic_smbus_pec(2, b, data);
    212 			buflen++;
    213 			break;
    214 		}
    215 
    216 		return ((*tag->ic_exec)(tag->ic_cookie, op, addr, cmd,
    217 					cmdlen, data, buflen, flags));
    218 	}
    219 
    220 	/*
    221 	 * Defer to the controller if it provides an exec function.  Use
    222 	 * it if it does.
    223 	 */
    224 	if (tag->ic_exec != NULL)
    225 		return ((*tag->ic_exec)(tag->ic_cookie, op, addr, cmd,
    226 					cmdlen, buf, buflen, flags));
    227 
    228 	if ((len = cmdlen) != 0) {
    229 		if ((error = iic_initiate_xfer(tag, addr, flags)) != 0)
    230 			goto bad;
    231 		while (len--) {
    232 			if ((error = iic_write_byte(tag, *cmd++, flags)) != 0)
    233 				goto bad;
    234 		}
    235 	} else if (buflen == 0) {
    236 		/*
    237 		 * This is a quick_read()/quick_write() command with
    238 		 * neither command nor data bytes
    239 		 */
    240 		if (I2C_OP_STOP_P(op))
    241 			flags |= I2C_F_STOP;
    242 		if (I2C_OP_READ_P(op))
    243 			flags |= I2C_F_READ;
    244 		if ((error = iic_initiate_xfer(tag, addr, flags)) != 0)
    245 			goto bad;
    246 	}
    247 
    248 	if (I2C_OP_READ_P(op))
    249 		flags |= I2C_F_READ;
    250 
    251 	len = buflen;
    252 	while (len--) {
    253 		if (len == 0 && I2C_OP_STOP_P(op))
    254 			flags |= I2C_F_STOP;
    255 		if (I2C_OP_READ_P(op)) {
    256 			/* Send REPEATED START. */
    257 			if ((len + 1) == buflen &&
    258 			    (error = iic_initiate_xfer(tag, addr, flags)) != 0)
    259 				goto bad;
    260 			/* NACK on last byte. */
    261 			if (len == 0)
    262 				flags |= I2C_F_LAST;
    263 			if ((error = iic_read_byte(tag, buf++, flags)) != 0)
    264 				goto bad;
    265 		} else  {
    266 			/* Maybe send START. */
    267 			if ((len + 1) == buflen && cmdlen == 0 &&
    268 			    (error = iic_initiate_xfer(tag, addr, flags)) != 0)
    269 				goto bad;
    270 			if ((error = iic_write_byte(tag, *buf++, flags)) != 0)
    271 				goto bad;
    272 		}
    273 	}
    274 
    275 	return (0);
    276  bad:
    277 	iic_send_stop(tag, flags);
    278 	return (error);
    279 }
    280 
    281 /*
    282  * iic_smbus_write_byte:
    283  *
    284  *	Perform an SMBus "write byte" operation.
    285  */
    286 int
    287 iic_smbus_write_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
    288     uint8_t val, int flags)
    289 {
    290 
    291 	return (iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &cmd, 1,
    292 			 &val, 1, flags));
    293 }
    294 
    295 /*
    296  * iic_smbus_write_word:
    297  *
    298  *	Perform an SMBus "write word" operation.
    299  */
    300 int
    301 iic_smbus_write_word(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
    302     uint16_t val, int flags)
    303 {
    304 	uint8_t vbuf[2];
    305 
    306 	vbuf[0] = val & 0xff;
    307 	vbuf[1] = (val >> 8) & 0xff;
    308 
    309 	return (iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &cmd, 1,
    310 			 vbuf, 2, flags));
    311 }
    312 
    313 /*
    314  * iic_smbus_read_byte:
    315  *
    316  *	Perform an SMBus "read byte" operation.
    317  */
    318 int
    319 iic_smbus_read_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
    320     uint8_t *valp, int flags)
    321 {
    322 
    323 	return (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, &cmd, 1,
    324 			 valp, 1, flags));
    325 }
    326 
    327 /*
    328  * iic_smbus_read_word:
    329  *
    330  *	Perform an SMBus "read word" operation.
    331  */
    332 int
    333 iic_smbus_read_word(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
    334     uint16_t *valp, int flags)
    335 {
    336 
    337 	return (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, &cmd, 1,
    338 			 (uint8_t *)valp, 2, flags));
    339 }
    340 
    341 /*
    342  * iic_smbus_receive_byte:
    343  *
    344  *	Perform an SMBus "receive byte" operation.
    345  */
    346 int
    347 iic_smbus_receive_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t *valp,
    348     int flags)
    349 {
    350 
    351 	return (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, NULL, 0,
    352 			 valp, 1, flags));
    353 }
    354 
    355 /*
    356  * iic_smbus_send_byte:
    357  *
    358  *	Perform an SMBus "send byte" operation.
    359  */
    360 int
    361 iic_smbus_send_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t val, int flags)
    362 {
    363 
    364 	return (iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, NULL, 0,
    365 			 &val, 1, flags));
    366 }
    367 
    368 /*
    369  * iic_smbus_quick_read:
    370  *
    371  *	Perform an SMBus "quick read" operation.
    372  */
    373 int
    374 iic_smbus_quick_read(i2c_tag_t tag, i2c_addr_t addr, int flags)
    375 {
    376 
    377 	return (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, NULL, 0,
    378 			 NULL, 0, flags));
    379 }
    380 
    381 /*
    382  * iic_smbus_quick_write:
    383  *
    384  *	Perform an SMBus "quick write" operation.
    385  */
    386 int
    387 iic_smbus_quick_write(i2c_tag_t tag, i2c_addr_t addr, int flags)
    388 {
    389 
    390 	return (iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, NULL, 0,
    391 			 NULL, 0, flags));
    392 }
    393 
    394 /*
    395  * iic_smbus_block_read:
    396  *
    397  *	Perform an SMBus "block read" operation.
    398  */
    399 int
    400 iic_smbus_block_read(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
    401     uint8_t *vbuf, size_t buflen, int flags)
    402 {
    403 
    404 	return (iic_exec(tag, I2C_OP_READ_BLOCK, addr, &cmd, 1,
    405 			 vbuf, buflen, flags));
    406 }
    407 
    408 /*
    409  * iic_smbus_block_write:
    410  *
    411  *	Perform an SMBus "block write" operation.
    412  */
    413 int
    414 iic_smbus_block_write(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
    415     uint8_t *vbuf, size_t buflen, int flags)
    416 {
    417 
    418 	return (iic_exec(tag, I2C_OP_WRITE_BLOCK, addr, &cmd, 1,
    419 			 vbuf, buflen, flags));
    420 }
    421 
    422 /*
    423  * iic_smbus_crc8
    424  *
    425  *	Private helper for calculating packet error checksum
    426  */
    427 static uint8_t
    428 iic_smbus_crc8(uint16_t data)
    429 {
    430 	int i;
    431 
    432 	for (i = 0; i < 8; i++) {
    433 		if (data & 0x8000)
    434 			data = data ^ (0x1070U << 3);
    435 		data = data << 1;
    436 	}
    437 
    438 	return (uint8_t)(data >> 8);
    439 }
    440 
    441 /*
    442  * iic_smbus_pec
    443  *
    444  *	Private function for calculating packet error checking on SMBus
    445  *	packets.
    446  */
    447 static uint8_t
    448 iic_smbus_pec(int count, uint8_t *s, uint8_t *r)
    449 {
    450 	int i;
    451 	uint8_t crc = 0;
    452 
    453 	for (i = 0; i < count; i++)
    454 		crc = iic_smbus_crc8((crc ^ s[i]) << 8);
    455 	if (r != NULL)
    456 		for (i = 0; i <= r[0]; i++)
    457 			crc = iic_smbus_crc8((crc ^ r[i]) << 8);
    458 
    459 	return crc;
    460 }
    461 
    462 MODULE(MODULE_CLASS_MISC, i2cexec, NULL);
    463 
    464 static int
    465 i2cexec_modcmd(modcmd_t cmd, void *opaque)
    466 {
    467 	switch (cmd) {
    468 	case MODULE_CMD_INIT:
    469 	case MODULE_CMD_FINI:
    470 		return 0;
    471 		break;
    472 	default:
    473 		return ENOTTY;
    474 	}
    475 }
    476