xref: /netbsd-src/sys/dev/i2c/i2c.c (revision 466a16a118933bd295a8a104f095714fadf9cf68)
1 /*	$NetBSD: i2c.c,v 1.22 2008/09/29 22:55:08 pgoyette 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.22 2008/09/29 22:55:08 pgoyette 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 #include <opt_i2cbus.h>
55 
56 struct iic_softc {
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(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
86 {
87 	struct iic_softc *sc = device_private(parent);
88 	struct i2c_attach_args ia;
89 
90 	ia.ia_tag = sc->sc_tag;
91 	ia.ia_addr = cf->cf_loc[IICCF_ADDR];
92 	ia.ia_size = cf->cf_loc[IICCF_SIZE];
93 	ia.ia_type = sc->sc_type;
94 
95 	if (config_match(parent, cf, &ia) > 0)
96 		config_attach(parent, cf, &ia, iic_print);
97 
98 	return (0);
99 }
100 
101 static int
102 iic_match(device_t parent, cfdata_t cf, void *aux)
103 {
104 
105 	return (1);
106 }
107 
108 static void
109 iic_attach(device_t parent, device_t self, void *aux)
110 {
111 	struct iic_softc *sc = device_private(self);
112 	struct i2cbus_attach_args *iba = aux;
113 	i2c_tag_t ic;
114 	int rv;
115 
116 	aprint_naive(": I2C bus\n");
117 	aprint_normal(": I2C bus\n");
118 
119 	sc->sc_tag = iba->iba_tag;
120 	sc->sc_type = iba->iba_type;
121 	ic = sc->sc_tag;
122 	ic->ic_devname = device_xname(self);
123 
124 	LIST_INIT(&(sc->sc_tag->ic_list));
125 	LIST_INIT(&(sc->sc_tag->ic_proc_list));
126 
127 	rv = kthread_create(PRI_NONE, 0, NULL, iic_smbus_intr_thread,
128 	    ic, &ic->ic_intr_thread, "%s", ic->ic_devname);
129 	if (rv)
130 		aprint_error_dev(self, "unable to create intr thread\n");
131 
132 #if I2C_SCAN
133 	if (sc->sc_type == I2C_TYPE_SMBUS) {
134 		int found = 0;
135 		i2c_addr_t addr;
136 		uint8_t cmd = 0, val;
137 
138 		for (addr = 0x0; addr < 0x80; addr++) {
139 			/* Skip i2c Alert Response Address */
140 			if (addr == 0x0c)
141 				continue;
142 			iic_acquire_bus(ic, 0);
143 			if (iic_exec(ic, I2C_OP_READ_WITH_STOP, addr,
144 			    &cmd, 1, &val, 1, 0) == 0) {
145 				if (found == 0)
146 					aprint_normal("%s: devices at",
147 							ic->ic_devname);
148 				found++;
149 				aprint_normal(" 0x%02x", addr);
150 			}
151 			iic_release_bus(ic, 0);
152 		}
153 		if (found == 0)
154 			aprint_normal("%s: no devices found", ic->ic_devname);
155 		aprint_normal("\n");
156 	}
157 #endif
158 
159 	if (!pmf_device_register(self, NULL, NULL))
160 		aprint_error_dev(self, "couldn't establish power handler\n");
161 
162 	/*
163 	 * Attach all i2c devices described in the kernel
164 	 * configuration file.
165 	 */
166 	config_search_ia(iic_search, self, "iic", NULL);
167 }
168 
169 static void
170 iic_smbus_intr_thread(void *aux)
171 {
172 	i2c_tag_t ic;
173 	struct ic_intr_list *il;
174 	int rv;
175 
176 	ic = (i2c_tag_t)aux;
177 	ic->ic_running = 1;
178 	ic->ic_pending = 0;
179 
180 	while (ic->ic_running) {
181 		if (ic->ic_pending == 0)
182 			rv = tsleep(ic, PZERO, "iicintr", hz);
183 		if (ic->ic_pending > 0) {
184 			LIST_FOREACH(il, &(ic->ic_proc_list), il_next) {
185 				(*il->il_intr)(il->il_intrarg);
186 			}
187 			ic->ic_pending--;
188 		}
189 	}
190 
191 	kthread_exit(0);
192 }
193 
194 void *
195 iic_smbus_intr_establish(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
196 {
197 	struct ic_intr_list *il;
198 
199 	il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
200 	if (il == NULL)
201 		return NULL;
202 
203 	il->il_intr = intr;
204 	il->il_intrarg = intrarg;
205 
206 	LIST_INSERT_HEAD(&(ic->ic_list), il, il_next);
207 
208 	return il;
209 }
210 
211 void
212 iic_smbus_intr_disestablish(i2c_tag_t ic, void *hdl)
213 {
214 	struct ic_intr_list *il;
215 
216 	il = (struct ic_intr_list *)hdl;
217 
218 	LIST_REMOVE(il, il_next);
219 	free(il, M_DEVBUF);
220 
221 	return;
222 }
223 
224 void *
225 iic_smbus_intr_establish_proc(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
226 {
227 	struct ic_intr_list *il;
228 
229 	il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
230 	if (il == NULL)
231 		return NULL;
232 
233 	il->il_intr = intr;
234 	il->il_intrarg = intrarg;
235 
236 	LIST_INSERT_HEAD(&(ic->ic_proc_list), il, il_next);
237 
238 	return il;
239 }
240 
241 void
242 iic_smbus_intr_disestablish_proc(i2c_tag_t ic, void *hdl)
243 {
244 	struct ic_intr_list *il;
245 
246 	il = (struct ic_intr_list *)hdl;
247 
248 	LIST_REMOVE(il, il_next);
249 	free(il, M_DEVBUF);
250 
251 	return;
252 }
253 
254 int
255 iic_smbus_intr(i2c_tag_t ic)
256 {
257 	struct ic_intr_list *il;
258 
259 	LIST_FOREACH(il, &(ic->ic_list), il_next) {
260 		(*il->il_intr)(il->il_intrarg);
261 	}
262 
263 	ic->ic_pending++;
264 	wakeup(ic);
265 
266 	return 1;
267 }
268 
269 CFATTACH_DECL_NEW(iic, sizeof(struct iic_softc),
270     iic_match, iic_attach, NULL, NULL);
271