xref: /openbsd-src/sys/dev/i2c/i2c.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: i2c.c,v 1.15 2007/10/09 16:57:47 deraadt Exp $	*/
2 /*	$NetBSD: i2c.c,v 1.1 2003/09/30 00:35:31 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 2003 Wasabi Systems, Inc.
6  * All rights reserved.
7  *
8  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed for the NetBSD Project by
21  *      Wasabi Systems, Inc.
22  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23  *    or promote products derived from this software without specific prior
24  *    written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/event.h>
43 #include <sys/conf.h>
44 
45 #define _I2C_PRIVATE
46 #include <dev/i2c/i2cvar.h>
47 
48 #define IICCF_ADDR	0
49 #define IICCF_SIZE	1
50 
51 struct iic_softc {
52 	struct device sc_dev;
53 	i2c_tag_t sc_tag;
54 };
55 
56 int	iic_match(struct device *, void *, void *);
57 void	iic_attach(struct device *, struct device *, void *);
58 int	iic_search(struct device *, void *, void *);
59 
60 struct cfattach iic_ca = {
61 	sizeof (struct iic_softc),
62 	iic_match,
63 	iic_attach
64 };
65 
66 struct cfdriver iic_cd = {
67 	NULL, "iic", DV_DULL
68 };
69 
70 int
71 iicbus_print(void *aux, const char *pnp)
72 {
73 	struct i2cbus_attach_args *iba = aux;
74 
75 	if (pnp != NULL)
76 		printf("%s at %s", iba->iba_name, pnp);
77 
78 	return (UNCONF);
79 }
80 
81 int
82 iic_print(void *aux, const char *pnp)
83 {
84 	struct i2c_attach_args *ia = aux;
85 
86 	if (pnp != NULL)
87 		printf("\"%s\" at %s", ia->ia_name, pnp);
88 	printf(" addr 0x%x", ia->ia_addr);
89 
90 	return (UNCONF);
91 }
92 
93 int
94 iic_search(struct device *parent, void *arg, void *aux)
95 {
96 	struct iic_softc *sc = (void *) parent;
97 	struct cfdata *cf = arg;
98 	struct i2c_attach_args ia;
99 
100 	if (cf->cf_loc[IICCF_ADDR] != -1) {
101 		memset(&ia, 0, sizeof(ia));
102 		ia.ia_tag = sc->sc_tag;
103 		ia.ia_addr = cf->cf_loc[IICCF_ADDR];
104 		ia.ia_size = cf->cf_loc[IICCF_SIZE];
105 		ia.ia_name = "unknown";
106 
107 		if (cf->cf_attach->ca_match(parent, cf, &ia) > 0)
108 			config_attach(parent, cf, &ia, iic_print);
109 	}
110 	return (0);
111 }
112 
113 int
114 iic_match(struct device *parent, void *arg, void *aux)
115 {
116 	struct cfdata *cf = arg;
117 	struct i2cbus_attach_args *iba = aux;
118 
119 	/* Just make sure we're looking for i2c. */
120 	return (strcmp(iba->iba_name, cf->cf_driver->cd_name) == 0);
121 }
122 
123 void
124 iic_attach(struct device *parent, struct device *self, void *aux)
125 {
126 	struct iic_softc *sc = (void *) self;
127 	struct i2cbus_attach_args *iba = aux;
128 
129 	sc->sc_tag = iba->iba_tag;
130 
131 	printf("\n");
132 
133 	/*
134 	 * Attach all i2c devices described in the kernel
135 	 * configuration file.
136 	 */
137 	config_search(iic_search, self, NULL);
138 
139 	/*
140 	 * Scan for known device signatures.
141 	 */
142 	if (iba->iba_bus_scan)
143 		(iba->iba_bus_scan)(self, aux, iba->iba_bus_scan_arg);
144 	else
145 		iic_scan(self, aux);
146 }
147