xref: /openbsd-src/sys/dev/i2c/i2c.c (revision 471aeecfc619bc9b69519928152daf993376c2a1)
1*471aeecfSnaddy /*	$OpenBSD: i2c.c,v 1.19 2022/04/06 18:59:28 naddy Exp $	*/
21da3bef2Sgrange /*	$NetBSD: i2c.c,v 1.1 2003/09/30 00:35:31 thorpej Exp $	*/
31da3bef2Sgrange 
41da3bef2Sgrange /*
51da3bef2Sgrange  * Copyright (c) 2003 Wasabi Systems, Inc.
61da3bef2Sgrange  * All rights reserved.
71da3bef2Sgrange  *
81da3bef2Sgrange  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
91da3bef2Sgrange  *
101da3bef2Sgrange  * Redistribution and use in source and binary forms, with or without
111da3bef2Sgrange  * modification, are permitted provided that the following conditions
121da3bef2Sgrange  * are met:
131da3bef2Sgrange  * 1. Redistributions of source code must retain the above copyright
141da3bef2Sgrange  *    notice, this list of conditions and the following disclaimer.
151da3bef2Sgrange  * 2. Redistributions in binary form must reproduce the above copyright
161da3bef2Sgrange  *    notice, this list of conditions and the following disclaimer in the
171da3bef2Sgrange  *    documentation and/or other materials provided with the distribution.
181da3bef2Sgrange  * 3. All advertising materials mentioning features or use of this software
191da3bef2Sgrange  *    must display the following acknowledgement:
201da3bef2Sgrange  *      This product includes software developed for the NetBSD Project by
211da3bef2Sgrange  *      Wasabi Systems, Inc.
221da3bef2Sgrange  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
231da3bef2Sgrange  *    or promote products derived from this software without specific prior
241da3bef2Sgrange  *    written permission.
251da3bef2Sgrange  *
261da3bef2Sgrange  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
271da3bef2Sgrange  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281da3bef2Sgrange  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291da3bef2Sgrange  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
301da3bef2Sgrange  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311da3bef2Sgrange  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321da3bef2Sgrange  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331da3bef2Sgrange  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341da3bef2Sgrange  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351da3bef2Sgrange  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361da3bef2Sgrange  * POSSIBILITY OF SUCH DAMAGE.
371da3bef2Sgrange  */
381da3bef2Sgrange 
391da3bef2Sgrange #include <sys/param.h>
401da3bef2Sgrange #include <sys/systm.h>
411da3bef2Sgrange #include <sys/device.h>
421da3bef2Sgrange #include <sys/event.h>
431da3bef2Sgrange 
441165b4a3Sgrange #define _I2C_PRIVATE
451da3bef2Sgrange #include <dev/i2c/i2cvar.h>
461da3bef2Sgrange 
471da3bef2Sgrange #define IICCF_ADDR	0
481da3bef2Sgrange #define IICCF_SIZE	1
491da3bef2Sgrange 
501da3bef2Sgrange struct iic_softc {
511da3bef2Sgrange 	struct device sc_dev;
521da3bef2Sgrange 	i2c_tag_t sc_tag;
531da3bef2Sgrange };
541da3bef2Sgrange 
551da3bef2Sgrange int	iic_match(struct device *, void *, void *);
561da3bef2Sgrange void	iic_attach(struct device *, struct device *, void *);
571da3bef2Sgrange int	iic_search(struct device *, void *, void *);
581da3bef2Sgrange 
59*471aeecfSnaddy const struct cfattach iic_ca = {
601da3bef2Sgrange 	sizeof (struct iic_softc),
611da3bef2Sgrange 	iic_match,
621da3bef2Sgrange 	iic_attach
631da3bef2Sgrange };
641da3bef2Sgrange 
651da3bef2Sgrange struct cfdriver iic_cd = {
6627b5a9d5Sderaadt 	NULL, "iic", DV_DULL, CD_SKIPHIBERNATE
671da3bef2Sgrange };
681da3bef2Sgrange 
691da3bef2Sgrange int
iicbus_print(void * aux,const char * pnp)701da3bef2Sgrange iicbus_print(void *aux, const char *pnp)
711da3bef2Sgrange {
721da3bef2Sgrange 	struct i2cbus_attach_args *iba = aux;
731da3bef2Sgrange 
741da3bef2Sgrange 	if (pnp != NULL)
75a5a63700Sderaadt 		printf("%s at %s", iba->iba_name, pnp);
761da3bef2Sgrange 
771da3bef2Sgrange 	return (UNCONF);
781da3bef2Sgrange }
791da3bef2Sgrange 
801da3bef2Sgrange int
iic_print(void * aux,const char * pnp)811da3bef2Sgrange iic_print(void *aux, const char *pnp)
821da3bef2Sgrange {
831da3bef2Sgrange 	struct i2c_attach_args *ia = aux;
841da3bef2Sgrange 
8515e54819Sderaadt 	if (pnp != NULL)
86a5a63700Sderaadt 		printf("\"%s\" at %s", ia->ia_name, pnp);
871da3bef2Sgrange 	printf(" addr 0x%x", ia->ia_addr);
881da3bef2Sgrange 
891da3bef2Sgrange 	return (UNCONF);
901da3bef2Sgrange }
911da3bef2Sgrange 
921da3bef2Sgrange int
iic_search(struct device * parent,void * arg,void * aux)931da3bef2Sgrange iic_search(struct device *parent, void *arg, void *aux)
941da3bef2Sgrange {
951da3bef2Sgrange 	struct iic_softc *sc = (void *) parent;
961da3bef2Sgrange 	struct cfdata *cf = arg;
971da3bef2Sgrange 	struct i2c_attach_args ia;
981da3bef2Sgrange 
9975e335a1Sderaadt 	if (cf->cf_loc[IICCF_ADDR] != -1) {
1004314413fSdlg 		memset(&ia, 0, sizeof(ia));
1011da3bef2Sgrange 		ia.ia_tag = sc->sc_tag;
1021da3bef2Sgrange 		ia.ia_addr = cf->cf_loc[IICCF_ADDR];
1031da3bef2Sgrange 		ia.ia_size = cf->cf_loc[IICCF_SIZE];
104b1505fe4Sderaadt 		ia.ia_name = "unknown";
1051da3bef2Sgrange 
1061da3bef2Sgrange 		if (cf->cf_attach->ca_match(parent, cf, &ia) > 0)
1071da3bef2Sgrange 			config_attach(parent, cf, &ia, iic_print);
10875e335a1Sderaadt 	}
1091da3bef2Sgrange 	return (0);
1101da3bef2Sgrange }
1111da3bef2Sgrange 
1121da3bef2Sgrange int
iic_match(struct device * parent,void * arg,void * aux)1131da3bef2Sgrange iic_match(struct device *parent, void *arg, void *aux)
1141da3bef2Sgrange {
1151da3bef2Sgrange 	struct cfdata *cf = arg;
1161da3bef2Sgrange 	struct i2cbus_attach_args *iba = aux;
1171da3bef2Sgrange 
1181da3bef2Sgrange 	/* Just make sure we're looking for i2c. */
1191da3bef2Sgrange 	return (strcmp(iba->iba_name, cf->cf_driver->cd_name) == 0);
1201da3bef2Sgrange }
1211da3bef2Sgrange 
1221da3bef2Sgrange void
iic_attach(struct device * parent,struct device * self,void * aux)1231da3bef2Sgrange iic_attach(struct device *parent, struct device *self, void *aux)
1241da3bef2Sgrange {
1251da3bef2Sgrange 	struct iic_softc *sc = (void *) self;
1261da3bef2Sgrange 	struct i2cbus_attach_args *iba = aux;
1271da3bef2Sgrange 
1281da3bef2Sgrange 	sc->sc_tag = iba->iba_tag;
1291da3bef2Sgrange 
1301da3bef2Sgrange 	printf("\n");
1311da3bef2Sgrange 
1321da3bef2Sgrange 	/*
1331da3bef2Sgrange 	 * Attach all i2c devices described in the kernel
1341da3bef2Sgrange 	 * configuration file.
1351da3bef2Sgrange 	 */
1361da3bef2Sgrange 	config_search(iic_search, self, NULL);
1371165b4a3Sgrange 
1381165b4a3Sgrange 	/*
1391165b4a3Sgrange 	 * Scan for known device signatures.
1401165b4a3Sgrange 	 */
141a5a63700Sderaadt 	if (iba->iba_bus_scan)
142a5a63700Sderaadt 		(iba->iba_bus_scan)(self, aux, iba->iba_bus_scan_arg);
143a5a63700Sderaadt 	else
1441165b4a3Sgrange 		iic_scan(self, aux);
1451da3bef2Sgrange }
1467349a5e9Svisa 
1477349a5e9Svisa int
iic_is_compatible(const struct i2c_attach_args * ia,const char * name)1487349a5e9Svisa iic_is_compatible(const struct i2c_attach_args *ia, const char *name)
1497349a5e9Svisa {
1507349a5e9Svisa 	const char *end, *entry;
1517349a5e9Svisa 
1527349a5e9Svisa 	if (ia->ia_namelen > 0) {
1537349a5e9Svisa 		/* ia_name points to a concatenation of strings. */
1547349a5e9Svisa 		entry = ia->ia_name;
1557349a5e9Svisa 		end = entry + ia->ia_namelen;
1567349a5e9Svisa 		while (entry < end) {
1577349a5e9Svisa 			if (strcmp(entry, name) == 0)
1587349a5e9Svisa 				return (1);
1597349a5e9Svisa 			entry += strlen(entry) + 1;
1607349a5e9Svisa 		}
1617349a5e9Svisa 	} else {
1627349a5e9Svisa 		/* ia_name points to a string. */
1637349a5e9Svisa 		if (strcmp(ia->ia_name, name) == 0)
1647349a5e9Svisa 			return (1);
1657349a5e9Svisa 	}
1667349a5e9Svisa 
1677349a5e9Svisa 	return (0);
1687349a5e9Svisa }
169