ziic.c revision 1.1 1 /* $NetBSD: ziic.c,v 1.1 2011/06/19 16:20:09 nonaka Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by NONAKA Kimihiro.
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ziic.c,v 1.1 2011/06/19 16:20:09 nonaka Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/bus.h>
39 #include <sys/mutex.h>
40
41 #include <dev/i2c/i2cvar.h>
42
43 #include <arm/xscale/pxa2x0reg.h>
44 #include <arm/xscale/pxa2x0var.h>
45 #include <arm/xscale/pxa2x0_i2c.h>
46
47 #ifdef PXAIIC_DEBUG
48 #define DPRINTF(s) printf s
49 #else
50 #define DPRINTF(s) do { } while (/*CONSTCOND*/0)
51 #endif
52
53 struct pxaiic_softc {
54 struct pxa2x0_i2c_softc sc_pxa_i2c;
55 void * sc_ih;
56
57 struct i2c_controller sc_i2c;
58 kmutex_t sc_buslock;
59 };
60
61 static int pxaiic_match(device_t, cfdata_t, void *);
62 static void pxaiic_attach(device_t, device_t, void *);
63
64 CFATTACH_DECL_NEW(pxaiic, sizeof(struct pxaiic_softc),
65 pxaiic_match, pxaiic_attach, NULL, NULL);
66
67 static int pxaiic_acquire_bus(void *, int);
68 static void pxaiic_release_bus(void *, int);
69 static int pxaiic_send_start(void *, int);
70 static int pxaiic_send_stop(void *, int);
71 static int pxaiic_initiate_xfer(void *, uint16_t, int);
72 static int pxaiic_read_byte(void *, uint8_t *, int);
73 static int pxaiic_write_byte(void *, uint8_t, int);
74
75 static int
76 pxaiic_match(device_t parent, cfdata_t cf, void *aux)
77 {
78 struct pxaip_attach_args *pxa = aux;
79
80 if (strcmp(cf->cf_name, pxa->pxa_name))
81 return 0;
82
83 pxa->pxa_size = PXA2X0_I2C_SIZE;
84 return 1;
85 }
86
87 static void
88 pxaiic_attach(device_t parent, device_t self, void *aux)
89 {
90 struct pxaiic_softc *sc = device_private(self);
91 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
92 struct pxaip_attach_args *pxa = aux;
93 struct i2cbus_attach_args iba;
94
95 aprint_normal(": I2C controller\n");
96 aprint_naive("\n");
97
98 psc->sc_dev = self;
99 psc->sc_iot = pxa->pxa_iot;
100 psc->sc_size = pxa->pxa_size;
101 psc->sc_flags = 0;
102 if (pxa2x0_i2c_attach_sub(psc)) {
103 aprint_error_dev(self, "unable to attach PXA I2C controller\n");
104 return;
105 }
106
107 mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_TTY);
108
109 #if 0
110 sc->sc_ih = pxa2x0_intr_establish(PXA2X0_INT_I2C, IPL_TTY,
111 pxa2x0_i2c_intr, &psc);
112 if (sc->sc_ih == NULL) {
113 aprint_error_dev(self, "unable to establish intr\n");
114 return; /* XXX: mutex_destroy, bus_space_unmap */
115 }
116 #endif
117
118 /* Initialize i2c_controller */
119 sc->sc_i2c.ic_cookie = sc;
120 sc->sc_i2c.ic_acquire_bus = pxaiic_acquire_bus;
121 sc->sc_i2c.ic_release_bus = pxaiic_release_bus;
122 sc->sc_i2c.ic_send_start = pxaiic_send_start;
123 sc->sc_i2c.ic_send_stop = pxaiic_send_stop;
124 sc->sc_i2c.ic_initiate_xfer = pxaiic_initiate_xfer;
125 sc->sc_i2c.ic_read_byte = pxaiic_read_byte;
126 sc->sc_i2c.ic_write_byte = pxaiic_write_byte;
127 sc->sc_i2c.ic_exec = NULL;
128
129 iba.iba_tag = &sc->sc_i2c;
130 (void)config_found_ia(psc->sc_dev, "i2cbus", &iba, iicbus_print);
131 }
132
133 static int
134 pxaiic_acquire_bus(void *cookie, int flags)
135 {
136 struct pxaiic_softc *sc = cookie;
137 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
138
139 mutex_enter(&sc->sc_buslock);
140 pxa2x0_i2c_open(psc);
141
142 return 0;
143 }
144
145 static void
146 pxaiic_release_bus(void *cookie, int flags)
147 {
148 struct pxaiic_softc *sc = cookie;
149 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
150
151 pxa2x0_i2c_close(psc);
152 mutex_exit(&sc->sc_buslock);
153 }
154
155 static int
156 pxaiic_send_start(void *cookie, int flags)
157 {
158 struct pxaiic_softc *sc = cookie;
159 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
160
161 return pxa2x0_i2c_send_start(psc, flags);
162 }
163
164 static int
165 pxaiic_send_stop(void *cookie, int flags)
166 {
167 struct pxaiic_softc *sc = cookie;
168 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
169
170 return pxa2x0_i2c_send_stop(psc, flags);
171 }
172
173 static int
174 pxaiic_initiate_xfer(void *cookie, uint16_t addr, int flags)
175 {
176 struct pxaiic_softc *sc = cookie;
177 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
178
179 return pxa2x0_i2c_initiate_xfer(psc, addr, flags);
180 }
181
182 static int
183 pxaiic_read_byte(void *cookie, uint8_t *bytep, int flags)
184 {
185 struct pxaiic_softc *sc = cookie;
186 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
187
188 return pxa2x0_i2c_read_byte(psc, bytep, flags);
189 }
190
191 static int
192 pxaiic_write_byte(void *cookie, uint8_t byte, int flags)
193 {
194 struct pxaiic_softc *sc = cookie;
195 struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
196
197 return pxa2x0_i2c_write_byte(psc, byte, flags);
198 }
199