xref: /openbsd-src/sys/dev/i2c/i2c.c (revision daf88648c0e349d5c02e1504293082072c981640)
1 /*	$OpenBSD: i2c.c,v 1.14 2006/02/26 20:24:46 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 #include "ipmi.h"
52 
53 struct iic_softc {
54 	struct device sc_dev;
55 	i2c_tag_t sc_tag;
56 };
57 
58 int	iic_match(struct device *, void *, void *);
59 void	iic_attach(struct device *, struct device *, void *);
60 int	iic_search(struct device *, void *, void *);
61 
62 struct cfattach iic_ca = {
63 	sizeof (struct iic_softc),
64 	iic_match,
65 	iic_attach
66 };
67 
68 struct cfdriver iic_cd = {
69 	NULL, "iic", DV_DULL
70 };
71 
72 int
73 iicbus_print(void *aux, const char *pnp)
74 {
75 	struct i2cbus_attach_args *iba = aux;
76 
77 	if (pnp != NULL)
78 		printf("%s at %s", iba->iba_name, pnp);
79 
80 	return (UNCONF);
81 }
82 
83 int
84 iic_print(void *aux, const char *pnp)
85 {
86 	struct i2c_attach_args *ia = aux;
87 
88 	if (pnp != NULL)
89 		printf("\"%s\" at %s", ia->ia_name, pnp);
90 	printf(" addr 0x%x", ia->ia_addr);
91 
92 	return (UNCONF);
93 }
94 
95 int
96 iic_search(struct device *parent, void *arg, void *aux)
97 {
98 	struct iic_softc *sc = (void *) parent;
99 	struct cfdata *cf = arg;
100 	struct i2c_attach_args ia;
101 
102 	if (cf->cf_loc[IICCF_ADDR] != -1) {
103 		memset(&ia, 0, sizeof(ia));
104 		ia.ia_tag = sc->sc_tag;
105 		ia.ia_addr = cf->cf_loc[IICCF_ADDR];
106 		ia.ia_size = cf->cf_loc[IICCF_SIZE];
107 		ia.ia_name = "unknown";
108 
109 		if (cf->cf_attach->ca_match(parent, cf, &ia) > 0)
110 			config_attach(parent, cf, &ia, iic_print);
111 	}
112 	return (0);
113 }
114 
115 int
116 iic_match(struct device *parent, void *arg, void *aux)
117 {
118 	struct cfdata *cf = arg;
119 	struct i2cbus_attach_args *iba = aux;
120 
121 	/* Just make sure we're looking for i2c. */
122 	return (strcmp(iba->iba_name, cf->cf_driver->cd_name) == 0);
123 }
124 
125 void
126 iic_attach(struct device *parent, struct device *self, void *aux)
127 {
128 	struct iic_softc *sc = (void *) self;
129 	struct i2cbus_attach_args *iba = aux;
130 
131 	sc->sc_tag = iba->iba_tag;
132 
133 #if NIPMI > 0
134 	extern int ipmi_enabled;
135 
136 	if (ipmi_enabled) {
137 		printf(": disabled to avoid ipmi0 interactions\n");
138 		return;
139 	}
140 #endif
141 
142 	printf("\n");
143 
144 	/*
145 	 * Attach all i2c devices described in the kernel
146 	 * configuration file.
147 	 */
148 	config_search(iic_search, self, NULL);
149 
150 	/*
151 	 * Scan for known device signatures.
152 	 */
153 	if (iba->iba_bus_scan)
154 		(iba->iba_bus_scan)(self, aux, iba->iba_bus_scan_arg);
155 	else
156 		iic_scan(self, aux);
157 }
158