1 /* $OpenBSD: i2c.c,v 1.19 2022/04/06 18:59:28 naddy 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
44 #define _I2C_PRIVATE
45 #include <dev/i2c/i2cvar.h>
46
47 #define IICCF_ADDR 0
48 #define IICCF_SIZE 1
49
50 struct iic_softc {
51 struct device sc_dev;
52 i2c_tag_t sc_tag;
53 };
54
55 int iic_match(struct device *, void *, void *);
56 void iic_attach(struct device *, struct device *, void *);
57 int iic_search(struct device *, void *, void *);
58
59 const struct cfattach iic_ca = {
60 sizeof (struct iic_softc),
61 iic_match,
62 iic_attach
63 };
64
65 struct cfdriver iic_cd = {
66 NULL, "iic", DV_DULL, CD_SKIPHIBERNATE
67 };
68
69 int
iicbus_print(void * aux,const char * pnp)70 iicbus_print(void *aux, const char *pnp)
71 {
72 struct i2cbus_attach_args *iba = aux;
73
74 if (pnp != NULL)
75 printf("%s at %s", iba->iba_name, pnp);
76
77 return (UNCONF);
78 }
79
80 int
iic_print(void * aux,const char * pnp)81 iic_print(void *aux, const char *pnp)
82 {
83 struct i2c_attach_args *ia = aux;
84
85 if (pnp != NULL)
86 printf("\"%s\" at %s", ia->ia_name, pnp);
87 printf(" addr 0x%x", ia->ia_addr);
88
89 return (UNCONF);
90 }
91
92 int
iic_search(struct device * parent,void * arg,void * aux)93 iic_search(struct device *parent, void *arg, void *aux)
94 {
95 struct iic_softc *sc = (void *) parent;
96 struct cfdata *cf = arg;
97 struct i2c_attach_args ia;
98
99 if (cf->cf_loc[IICCF_ADDR] != -1) {
100 memset(&ia, 0, sizeof(ia));
101 ia.ia_tag = sc->sc_tag;
102 ia.ia_addr = cf->cf_loc[IICCF_ADDR];
103 ia.ia_size = cf->cf_loc[IICCF_SIZE];
104 ia.ia_name = "unknown";
105
106 if (cf->cf_attach->ca_match(parent, cf, &ia) > 0)
107 config_attach(parent, cf, &ia, iic_print);
108 }
109 return (0);
110 }
111
112 int
iic_match(struct device * parent,void * arg,void * aux)113 iic_match(struct device *parent, void *arg, void *aux)
114 {
115 struct cfdata *cf = arg;
116 struct i2cbus_attach_args *iba = aux;
117
118 /* Just make sure we're looking for i2c. */
119 return (strcmp(iba->iba_name, cf->cf_driver->cd_name) == 0);
120 }
121
122 void
iic_attach(struct device * parent,struct device * self,void * aux)123 iic_attach(struct device *parent, struct device *self, void *aux)
124 {
125 struct iic_softc *sc = (void *) self;
126 struct i2cbus_attach_args *iba = aux;
127
128 sc->sc_tag = iba->iba_tag;
129
130 printf("\n");
131
132 /*
133 * Attach all i2c devices described in the kernel
134 * configuration file.
135 */
136 config_search(iic_search, self, NULL);
137
138 /*
139 * Scan for known device signatures.
140 */
141 if (iba->iba_bus_scan)
142 (iba->iba_bus_scan)(self, aux, iba->iba_bus_scan_arg);
143 else
144 iic_scan(self, aux);
145 }
146
147 int
iic_is_compatible(const struct i2c_attach_args * ia,const char * name)148 iic_is_compatible(const struct i2c_attach_args *ia, const char *name)
149 {
150 const char *end, *entry;
151
152 if (ia->ia_namelen > 0) {
153 /* ia_name points to a concatenation of strings. */
154 entry = ia->ia_name;
155 end = entry + ia->ia_namelen;
156 while (entry < end) {
157 if (strcmp(entry, name) == 0)
158 return (1);
159 entry += strlen(entry) + 1;
160 }
161 } else {
162 /* ia_name points to a string. */
163 if (strcmp(ia->ia_name, name) == 0)
164 return (1);
165 }
166
167 return (0);
168 }
169