xref: /netbsd-src/sys/dev/i2c/i2c.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /*	$NetBSD: i2c.c,v 1.19 2008/04/06 20:25:59 cegger 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.c,v 1.19 2008/04/06 20:25:59 cegger Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/event.h>
45 #include <sys/conf.h>
46 #include <sys/malloc.h>
47 #include <sys/kthread.h>
48 #include <sys/proc.h>
49 #include <sys/kernel.h>
50 
51 #include <dev/i2c/i2cvar.h>
52 
53 #include "locators.h"
54 
55 struct iic_softc {
56 	struct device sc_dev;
57 	i2c_tag_t sc_tag;
58 	int sc_type;
59 };
60 
61 static void	iic_smbus_intr_thread(void *);
62 
63 int
64 iicbus_print(void *aux, const char *pnp)
65 {
66 
67 	if (pnp != NULL)
68 		aprint_normal("iic at %s", pnp);
69 
70 	return (UNCONF);
71 }
72 
73 static int
74 iic_print(void *aux, const char *pnp)
75 {
76 	struct i2c_attach_args *ia = aux;
77 
78 	if (ia->ia_addr != (i2c_addr_t)-1)
79 		aprint_normal(" addr 0x%x", ia->ia_addr);
80 
81 	return (UNCONF);
82 }
83 
84 static int
85 iic_search(struct device *parent, struct cfdata *cf,
86     const int *ldesc, void *aux)
87 {
88 	struct iic_softc *sc = (void *) parent;
89 	struct i2c_attach_args ia;
90 
91 	ia.ia_tag = sc->sc_tag;
92 	ia.ia_addr = cf->cf_loc[IICCF_ADDR];
93 	ia.ia_size = cf->cf_loc[IICCF_SIZE];
94 	ia.ia_type = sc->sc_type;
95 
96 	if (config_match(parent, cf, &ia) > 0)
97 		config_attach(parent, cf, &ia, iic_print);
98 
99 	return (0);
100 }
101 
102 static int
103 iic_match(struct device *parent, struct cfdata *cf,
104     void *aux)
105 {
106 
107 	return (1);
108 }
109 
110 static void
111 iic_attach(struct device *parent, struct device *self, void *aux)
112 {
113 	struct iic_softc *sc = device_private(self);
114 	struct i2cbus_attach_args *iba = aux;
115 	i2c_tag_t ic;
116 	int rv;
117 
118 	aprint_naive(": I2C bus\n");
119 	aprint_normal(": I2C bus\n");
120 
121 	sc->sc_tag = iba->iba_tag;
122 	sc->sc_type = iba->iba_type;
123 	ic = sc->sc_tag;
124 	ic->ic_devname = device_xname(self);
125 
126 	LIST_INIT(&(sc->sc_tag->ic_list));
127 	LIST_INIT(&(sc->sc_tag->ic_proc_list));
128 
129 	rv = kthread_create(PRI_NONE, 0, NULL, iic_smbus_intr_thread,
130 	    ic, &ic->ic_intr_thread, "%s", ic->ic_devname);
131 	if (rv)
132 		printf("%s: unable to create intr thread\n", ic->ic_devname);
133 
134 #if notyet
135 	if (sc->sc_type == I2C_TYPE_SMBUS) {
136 		int found = 0;
137 		i2c_addr_t addr;
138 		uint8_t cmd = 0, val;
139 
140 		for (addr = 0x0; addr < 0x80; addr++) {
141 			iic_acquire_bus(ic, 0);
142 			if (iic_exec(ic, I2C_OP_READ_WITH_STOP, addr,
143 			    &cmd, 1, &val, 1, 0) == 0) {
144 				if (found == 0)
145 					aprint_normal("%s: devices at",
146 							ic->ic_devname);
147 				found++;
148 				aprint_normal(" 0x%02x", addr);
149 			}
150 			iic_release_bus(ic, 0);
151 		}
152 		if (found == 0)
153 			aprint_normal("%s: no devices found", ic->ic_devname);
154 		aprint_normal("\n");
155 	}
156 #endif
157 
158 	if (!pmf_device_register(self, NULL, NULL))
159 		aprint_error_dev(self, "couldn't establish power handler\n");
160 
161 	/*
162 	 * Attach all i2c devices described in the kernel
163 	 * configuration file.
164 	 */
165 	config_search_ia(iic_search, self, "iic", NULL);
166 }
167 
168 static void
169 iic_smbus_intr_thread(void *aux)
170 {
171 	i2c_tag_t ic;
172 	struct ic_intr_list *il;
173 	int rv;
174 
175 	ic = (i2c_tag_t)aux;
176 	ic->ic_running = 1;
177 	ic->ic_pending = 0;
178 
179 	while (ic->ic_running) {
180 		if (ic->ic_pending == 0)
181 			rv = tsleep(ic, PZERO, "iicintr", hz);
182 		if (ic->ic_pending > 0) {
183 			LIST_FOREACH(il, &(ic->ic_proc_list), il_next) {
184 				(*il->il_intr)(il->il_intrarg);
185 			}
186 			ic->ic_pending--;
187 		}
188 	}
189 
190 	kthread_exit(0);
191 }
192 
193 void *
194 iic_smbus_intr_establish(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
195 {
196 	struct ic_intr_list *il;
197 
198 	il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
199 	if (il == NULL)
200 		return NULL;
201 
202 	il->il_intr = intr;
203 	il->il_intrarg = intrarg;
204 
205 	LIST_INSERT_HEAD(&(ic->ic_list), il, il_next);
206 
207 	return il;
208 }
209 
210 void
211 iic_smbus_intr_disestablish(i2c_tag_t ic, void *hdl)
212 {
213 	struct ic_intr_list *il;
214 
215 	il = (struct ic_intr_list *)hdl;
216 
217 	LIST_REMOVE(il, il_next);
218 	free(il, M_DEVBUF);
219 
220 	return;
221 }
222 
223 void *
224 iic_smbus_intr_establish_proc(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
225 {
226 	struct ic_intr_list *il;
227 
228 	il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
229 	if (il == NULL)
230 		return NULL;
231 
232 	il->il_intr = intr;
233 	il->il_intrarg = intrarg;
234 
235 	LIST_INSERT_HEAD(&(ic->ic_proc_list), il, il_next);
236 
237 	return il;
238 }
239 
240 void
241 iic_smbus_intr_disestablish_proc(i2c_tag_t ic, void *hdl)
242 {
243 	struct ic_intr_list *il;
244 
245 	il = (struct ic_intr_list *)hdl;
246 
247 	LIST_REMOVE(il, il_next);
248 	free(il, M_DEVBUF);
249 
250 	return;
251 }
252 
253 int
254 iic_smbus_intr(i2c_tag_t ic)
255 {
256 	struct ic_intr_list *il;
257 
258 	LIST_FOREACH(il, &(ic->ic_list), il_next) {
259 		(*il->il_intr)(il->il_intrarg);
260 	}
261 
262 	ic->ic_pending++;
263 	wakeup(ic);
264 
265 	return 1;
266 }
267 
268 CFATTACH_DECL(iic, sizeof(struct iic_softc),
269     iic_match, iic_attach, NULL, NULL);
270