xref: /netbsd-src/sys/dev/i2c/i2c.c (revision c71562d660be5e4ad22016bce45e96f08af190cc)
1 /*	$NetBSD: i2c.c,v 1.7 2006/03/29 06:41:24 thorpej 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/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/event.h>
42 #include <sys/conf.h>
43 
44 #include <dev/i2c/i2cvar.h>
45 
46 #include "locators.h"
47 
48 struct iic_softc {
49 	struct device sc_dev;
50 	i2c_tag_t sc_tag;
51 	int sc_type;
52 };
53 
54 int
55 iicbus_print(void *aux, const char *pnp)
56 {
57 	struct i2cbus_attach_args *iba = aux;
58 
59 	if (pnp != NULL)
60 		aprint_normal("%s at %s", iba->iba_name, pnp);
61 
62 	return (UNCONF);
63 }
64 
65 static int
66 iic_print(void *aux, const char *pnp)
67 {
68 	struct i2c_attach_args *ia = aux;
69 
70 	if (ia->ia_addr != (i2c_addr_t)-1)
71 		aprint_normal(" addr 0x%x", ia->ia_addr);
72 
73 	return (UNCONF);
74 }
75 
76 static int
77 iic_search(struct device *parent, struct cfdata *cf,
78 	   const int *ldesc, void *aux)
79 {
80 	struct iic_softc *sc = (void *) parent;
81 	struct i2c_attach_args ia;
82 
83 	ia.ia_tag = sc->sc_tag;
84 	ia.ia_addr = cf->cf_loc[IICCF_ADDR];
85 	ia.ia_size = cf->cf_loc[IICCF_SIZE];
86 	ia.ia_type = sc->sc_type;
87 
88 	if (config_match(parent, cf, &ia) > 0)
89 		config_attach(parent, cf, &ia, iic_print);
90 
91 	return (0);
92 }
93 
94 static int
95 iic_match(struct device *parent, struct cfdata *cf, void *aux)
96 {
97 	struct i2cbus_attach_args *iba = aux;
98 
99 	/* Just make sure we're looking for i2c. */
100 	return (strcmp(iba->iba_name, cf->cf_name) == 0);
101 }
102 
103 static void
104 iic_attach(struct device *parent, struct device *self, void *aux)
105 {
106 	struct iic_softc *sc = device_private(self);
107 	struct i2cbus_attach_args *iba = aux;
108 
109 	aprint_naive(": I2C bus\n");
110 	aprint_normal(": I2C bus\n");
111 
112 	sc->sc_tag = iba->iba_tag;
113 	sc->sc_type = iba->iba_type;
114 
115 	/*
116 	 * Attach all i2c devices described in the kernel
117 	 * configuration file.
118 	 */
119 	config_search_ia(iic_search, self, "iic", NULL);
120 }
121 
122 CFATTACH_DECL(iic, sizeof(struct iic_softc),
123     iic_match, iic_attach, NULL, NULL);
124