xref: /netbsd-src/sys/dev/i2c/i2c.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: i2c.c,v 1.42 2013/09/24 18:04:53 jdc 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.42 2013/09/24 18:04:53 jdc 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/kmem.h>
48 #include <sys/kthread.h>
49 #include <sys/proc.h>
50 #include <sys/kernel.h>
51 #include <sys/fcntl.h>
52 #include <sys/module.h>
53 
54 #include <dev/i2c/i2cvar.h>
55 
56 #include "locators.h"
57 
58 #define I2C_MAX_ADDR	0x3ff	/* 10-bit address, max */
59 
60 struct iic_softc {
61 	i2c_tag_t sc_tag;
62 	int sc_type;
63 	device_t sc_devices[I2C_MAX_ADDR + 1];
64 };
65 
66 static dev_type_open(iic_open);
67 static dev_type_close(iic_close);
68 static dev_type_ioctl(iic_ioctl);
69 
70 const struct cdevsw iic_cdevsw = {
71 	iic_open, iic_close, noread, nowrite, iic_ioctl,
72 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
73 };
74 
75 extern struct cfdriver iic_cd;
76 
77 static void	iic_smbus_intr_thread(void *);
78 static void	iic_fill_compat(struct i2c_attach_args*, const char*,
79 			size_t, char **);
80 
81 static int
82 iic_print_direct(void *aux, const char *pnp)
83 {
84 	struct i2c_attach_args *ia = aux;
85 
86 	if (pnp != NULL)
87 		aprint_normal("%s at %s addr 0x%02x", ia->ia_name, pnp,
88 			ia->ia_addr);
89 	else
90 		aprint_normal(" addr 0x%02x", ia->ia_addr);
91 
92 	return UNCONF;
93 }
94 
95 static int
96 iic_print(void *aux, const char *pnp)
97 {
98 	struct i2c_attach_args *ia = aux;
99 
100 	if (ia->ia_addr != (i2c_addr_t)-1)
101 		aprint_normal(" addr 0x%x", ia->ia_addr);
102 
103 	return UNCONF;
104 }
105 
106 static int
107 iic_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
108 {
109 	struct iic_softc *sc = device_private(parent);
110 	struct i2c_attach_args ia;
111 
112 	ia.ia_tag = sc->sc_tag;
113 	ia.ia_size = cf->cf_loc[IICCF_SIZE];
114 	ia.ia_type = sc->sc_type;
115 
116 	ia.ia_name = NULL;
117 	ia.ia_ncompat = 0;
118 	ia.ia_compat = NULL;
119 
120 	for (ia.ia_addr = 0; ia.ia_addr <= I2C_MAX_ADDR; ia.ia_addr++) {
121 		if (sc->sc_devices[ia.ia_addr] != NULL)
122 			continue;
123 
124 		if (cf->cf_loc[IICCF_ADDR] != -1 &&
125 		    cf->cf_loc[IICCF_ADDR] != ia.ia_addr)
126 			continue;
127 
128 		if (config_match(parent, cf, &ia) > 0)
129 			sc->sc_devices[ia.ia_addr] =
130 			    config_attach(parent, cf, &ia, iic_print);
131 	}
132 
133 	return 0;
134 }
135 
136 static void
137 iic_child_detach(device_t parent, device_t child)
138 {
139 	struct iic_softc *sc = device_private(parent);
140 	int i;
141 
142 	for (i = 0; i <= I2C_MAX_ADDR; i++)
143 		if (sc->sc_devices[i] == child) {
144 			sc->sc_devices[i] = NULL;
145 			break;
146 		}
147 }
148 
149 static int
150 iic_rescan(device_t self, const char *ifattr, const int *locators)
151 {
152 	config_search_ia(iic_search, self, ifattr, NULL);
153 	return 0;
154 }
155 
156 static int
157 iic_match(device_t parent, cfdata_t cf, void *aux)
158 {
159 
160 	return 1;
161 }
162 
163 static void
164 iic_attach(device_t parent, device_t self, void *aux)
165 {
166 	struct iic_softc *sc = device_private(self);
167 	struct i2cbus_attach_args *iba = aux;
168 	prop_array_t child_devices;
169 	prop_dictionary_t props;
170 	char *buf;
171 	i2c_tag_t ic;
172 	int rv;
173 	bool indirect_config;
174 
175 	aprint_naive("\n");
176 	aprint_normal(": I2C bus\n");
177 
178 	sc->sc_tag = iba->iba_tag;
179 	sc->sc_type = iba->iba_type;
180 	ic = sc->sc_tag;
181 	ic->ic_devname = device_xname(self);
182 
183 	LIST_INIT(&(sc->sc_tag->ic_list));
184 	LIST_INIT(&(sc->sc_tag->ic_proc_list));
185 
186 	rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL,
187 	    iic_smbus_intr_thread, ic, &ic->ic_intr_thread,
188 	    "%s", ic->ic_devname);
189 	if (rv)
190 		aprint_error_dev(self, "unable to create intr thread\n");
191 
192 	if (!pmf_device_register(self, NULL, NULL))
193 		aprint_error_dev(self, "couldn't establish power handler\n");
194 
195 	props = device_properties(parent);
196 	if (!prop_dictionary_get_bool(props, "i2c-indirect-config",
197 	    &indirect_config))
198 		indirect_config = true;
199 	child_devices = prop_dictionary_get(props, "i2c-child-devices");
200 	if (child_devices) {
201 		unsigned int i, count;
202 		prop_dictionary_t dev;
203 		prop_data_t cdata;
204 		uint32_t addr, size;
205 		uint64_t cookie;
206 		const char *name;
207 		struct i2c_attach_args ia;
208 		int loc[2];
209 
210 		memset(loc, 0, sizeof loc);
211 		count = prop_array_count(child_devices);
212 		for (i = 0; i < count; i++) {
213 			dev = prop_array_get(child_devices, i);
214 			if (!dev) continue;
215  			if (!prop_dictionary_get_cstring_nocopy(
216 			    dev, "name", &name))
217 				continue;
218 			if (!prop_dictionary_get_uint32(dev, "addr", &addr))
219 				continue;
220 			if (!prop_dictionary_get_uint64(dev, "cookie", &cookie))
221 				cookie = 0;
222 			loc[0] = addr;
223 			if (prop_dictionary_get_uint32(dev, "size", &size))
224 				loc[1] = size;
225 			else
226 				loc[1] = -1;
227 
228 			memset(&ia, 0, sizeof ia);
229 			ia.ia_addr = addr;
230 			ia.ia_type = sc->sc_type;
231 			ia.ia_tag = ic;
232 			ia.ia_name = name;
233 			ia.ia_cookie = cookie;
234 			ia.ia_size = size;
235 
236 			buf = NULL;
237 			cdata = prop_dictionary_get(dev, "compatible");
238 			if (cdata)
239 				iic_fill_compat(&ia,
240 				    prop_data_data_nocopy(cdata),
241 				    prop_data_size(cdata), &buf);
242 
243 			if (addr > I2C_MAX_ADDR) {
244 				aprint_error_dev(self,
245 				    "WARNING: ignoring bad device address "
246 				    "@ 0x%02x\n", addr);
247 			} else if (sc->sc_devices[addr] == NULL) {
248 				sc->sc_devices[addr] =
249 				    config_found_sm_loc(self, "iic", loc, &ia,
250 					iic_print_direct, NULL);
251 			}
252 
253 			if (ia.ia_compat)
254 				free(ia.ia_compat, M_TEMP);
255 			if (buf)
256 				free(buf, M_TEMP);
257 		}
258 	} else if (indirect_config) {
259 		/*
260 		 * Attach all i2c devices described in the kernel
261 		 * configuration file.
262 		 */
263 		iic_rescan(self, "iic", NULL);
264 	}
265 }
266 
267 static int
268 iic_detach(device_t self, int flags)
269 {
270 	struct iic_softc *sc = device_private(self);
271 	i2c_tag_t ic = sc->sc_tag;
272 	int i, error;
273 	void *hdl;
274 
275 	for (i = 0; i <= I2C_MAX_ADDR; i++) {
276 		if (sc->sc_devices[i]) {
277 			error = config_detach(sc->sc_devices[i], flags);
278 			if (error)
279 				return error;
280 		}
281 	}
282 
283 	if (ic->ic_running) {
284 		ic->ic_running = 0;
285 		wakeup(ic);
286 		kthread_join(ic->ic_intr_thread);
287 	}
288 
289 	if (!LIST_EMPTY(&ic->ic_list)) {
290 		device_printf(self, "WARNING: intr handler list not empty\n");
291 		while (!LIST_EMPTY(&ic->ic_list)) {
292 			hdl = LIST_FIRST(&ic->ic_list);
293 			iic_smbus_intr_disestablish(ic, hdl);
294 		}
295 	}
296 	if (!LIST_EMPTY(&ic->ic_proc_list)) {
297 		device_printf(self, "WARNING: proc handler list not empty\n");
298 		while (!LIST_EMPTY(&ic->ic_proc_list)) {
299 			hdl = LIST_FIRST(&ic->ic_proc_list);
300 			iic_smbus_intr_disestablish_proc(ic, hdl);
301 		}
302 	}
303 
304 	pmf_device_deregister(self);
305 
306 	return 0;
307 }
308 
309 static void
310 iic_smbus_intr_thread(void *aux)
311 {
312 	i2c_tag_t ic;
313 	struct ic_intr_list *il;
314 
315 	ic = (i2c_tag_t)aux;
316 	ic->ic_running = 1;
317 	ic->ic_pending = 0;
318 
319 	while (ic->ic_running) {
320 		if (ic->ic_pending == 0)
321 			tsleep(ic, PZERO, "iicintr", hz);
322 		if (ic->ic_pending > 0) {
323 			LIST_FOREACH(il, &(ic->ic_proc_list), il_next) {
324 				(*il->il_intr)(il->il_intrarg);
325 			}
326 			ic->ic_pending--;
327 		}
328 	}
329 
330 	kthread_exit(0);
331 }
332 
333 void *
334 iic_smbus_intr_establish(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
335 {
336 	struct ic_intr_list *il;
337 
338 	il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
339 	if (il == NULL)
340 		return NULL;
341 
342 	il->il_intr = intr;
343 	il->il_intrarg = intrarg;
344 
345 	LIST_INSERT_HEAD(&(ic->ic_list), il, il_next);
346 
347 	return il;
348 }
349 
350 void
351 iic_smbus_intr_disestablish(i2c_tag_t ic, void *hdl)
352 {
353 	struct ic_intr_list *il;
354 
355 	il = (struct ic_intr_list *)hdl;
356 
357 	LIST_REMOVE(il, il_next);
358 	free(il, M_DEVBUF);
359 
360 	return;
361 }
362 
363 void *
364 iic_smbus_intr_establish_proc(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
365 {
366 	struct ic_intr_list *il;
367 
368 	il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
369 	if (il == NULL)
370 		return NULL;
371 
372 	il->il_intr = intr;
373 	il->il_intrarg = intrarg;
374 
375 	LIST_INSERT_HEAD(&(ic->ic_proc_list), il, il_next);
376 
377 	return il;
378 }
379 
380 void
381 iic_smbus_intr_disestablish_proc(i2c_tag_t ic, void *hdl)
382 {
383 	struct ic_intr_list *il;
384 
385 	il = (struct ic_intr_list *)hdl;
386 
387 	LIST_REMOVE(il, il_next);
388 	free(il, M_DEVBUF);
389 
390 	return;
391 }
392 
393 int
394 iic_smbus_intr(i2c_tag_t ic)
395 {
396 	struct ic_intr_list *il;
397 
398 	LIST_FOREACH(il, &(ic->ic_list), il_next) {
399 		(*il->il_intr)(il->il_intrarg);
400 	}
401 
402 	ic->ic_pending++;
403 	wakeup(ic);
404 
405 	return 1;
406 }
407 
408 static void
409 iic_fill_compat(struct i2c_attach_args *ia, const char *compat, size_t len,
410 	char **buffer)
411 {
412 	int count, i;
413 	const char *c, *start, **ptr;
414 
415 	*buffer = NULL;
416 	for (i = count = 0, c = compat; i < len; i++, c++)
417 		if (*c == 0)
418 			count++;
419 	count += 2;
420 	ptr = malloc(sizeof(char*)*count, M_TEMP, M_WAITOK);
421 	if (!ptr) return;
422 
423 	for (i = count = 0, start = c = compat; i < len; i++, c++) {
424 		if (*c == 0) {
425 			ptr[count++] = start;
426 			start = c+1;
427 		}
428 	}
429 	if (start < compat+len) {
430 		/* last string not 0 terminated */
431 		size_t l = c-start;
432 		*buffer = malloc(l+1, M_TEMP, M_WAITOK);
433 		memcpy(*buffer, start, l);
434 		(*buffer)[l] = 0;
435 		ptr[count++] = *buffer;
436 	}
437 	ptr[count] = NULL;
438 
439 	ia->ia_compat = ptr;
440 	ia->ia_ncompat = count;
441 }
442 
443 int
444 iic_compat_match(struct i2c_attach_args *ia, const char ** compats)
445 {
446 	int i;
447 
448 	for (; compats && *compats; compats++) {
449 		for (i = 0; i < ia->ia_ncompat; i++) {
450 			if (strcmp(*compats, ia->ia_compat[i]) == 0)
451 				return 1;
452 		}
453 	}
454 	return 0;
455 }
456 
457 static int
458 iic_open(dev_t dev, int flag, int fmt, lwp_t *l)
459 {
460 	struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev));
461 
462 	if (sc == NULL)
463 		return ENXIO;
464 
465 	return 0;
466 }
467 
468 static int
469 iic_close(dev_t dev, int flag, int fmt, lwp_t *l)
470 {
471 	return 0;
472 }
473 
474 static int
475 iic_ioctl_exec(struct iic_softc *sc, i2c_ioctl_exec_t *iie, int flag)
476 {
477 	i2c_tag_t ic = sc->sc_tag;
478 	uint8_t buf[I2C_EXEC_MAX_BUFLEN];
479 	void *cmd = NULL;
480 	int error;
481 
482 	/* Validate parameters */
483 	if (iie->iie_addr > I2C_MAX_ADDR)
484 		return EINVAL;
485 	if (iie->iie_cmdlen > I2C_EXEC_MAX_CMDLEN ||
486 	    iie->iie_buflen > I2C_EXEC_MAX_BUFLEN)
487 		return EINVAL;
488 	if (iie->iie_cmd != NULL && iie->iie_cmdlen == 0)
489 		return EINVAL;
490 	if (iie->iie_buf != NULL && iie->iie_buflen == 0)
491 		return EINVAL;
492 	if (I2C_OP_WRITE_P(iie->iie_op) && (flag & FWRITE) == 0)
493 		return EBADF;
494 
495 #if 0
496 	/* Disallow userspace access to devices that have drivers attached. */
497 	if (sc->sc_devices[iie->iie_addr] != NULL)
498 		return EBUSY;
499 #endif
500 
501 	if (iie->iie_cmd != NULL) {
502 		cmd = kmem_alloc(iie->iie_cmdlen, KM_SLEEP);
503 		if (cmd == NULL)
504 			return ENOMEM;
505 		error = copyin(iie->iie_cmd, cmd, iie->iie_cmdlen);
506 		if (error) {
507 			kmem_free(cmd, iie->iie_cmdlen);
508 			return error;
509 		}
510 	}
511 
512 	iic_acquire_bus(ic, 0);
513 	error = iic_exec(ic, iie->iie_op, iie->iie_addr, cmd, iie->iie_cmdlen,
514 	    buf, iie->iie_buflen, 0);
515 	iic_release_bus(ic, 0);
516 
517 	/*
518 	 * Some drivers return error codes on failure, and others return -1.
519 	 */
520 	if (error < 0)
521 		error = EIO;
522 
523 	if (cmd)
524 		kmem_free(cmd, iie->iie_cmdlen);
525 
526 	if (error)
527 		return error;
528 
529 	if (iie->iie_buf)
530 		error = copyout(buf, iie->iie_buf, iie->iie_buflen);
531 
532 	return error;
533 }
534 
535 static int
536 iic_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
537 {
538 	struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev));
539 
540 	if (sc == NULL)
541 		return ENXIO;
542 
543 	switch (cmd) {
544 	case I2C_IOCTL_EXEC:
545 		return iic_ioctl_exec(sc, (i2c_ioctl_exec_t *)data, flag);
546 	default:
547 		return ENODEV;
548 	}
549 }
550 
551 
552 CFATTACH_DECL2_NEW(iic, sizeof(struct iic_softc),
553     iic_match, iic_attach, iic_detach, NULL, iic_rescan, iic_child_detach);
554 
555 MODULE(MODULE_CLASS_DRIVER, iic, NULL);
556 
557 #ifdef _MODULE
558 #include "ioconf.c"
559 #endif
560 
561 static int
562 iic_modcmd(modcmd_t cmd, void *opaque)
563 {
564 	int error;
565 
566 	error = 0;
567 	switch (cmd) {
568 	case MODULE_CMD_INIT:
569 #ifdef _MODULE
570 		error = config_init_component(cfdriver_ioconf_iic,
571 		    cfattach_ioconf_iic, cfdata_ioconf_iic);
572 		if (error)
573 			aprint_error("%s: unable to init component\n",
574 			    iic_cd.cd_name);
575 #endif
576 		break;
577 	case MODULE_CMD_FINI:
578 #ifdef _MODULE
579 		config_fini_component(cfdriver_ioconf_iic,
580 		    cfattach_ioconf_iic, cfdata_ioconf_iic);
581 #endif
582 		break;
583 	default:
584 		error = ENOTTY;
585 	}
586 	return error;
587 }
588