nbpiic.c revision 1.1 1 1.1 kiyohara /* $NetBSD: nbpiic.c,v 1.1 2011/08/06 03:53:40 kiyohara Exp $ */
2 1.1 kiyohara /*
3 1.1 kiyohara * Copyright (c) 2011 KIYOHARA Takashi
4 1.1 kiyohara * All rights reserved.
5 1.1 kiyohara *
6 1.1 kiyohara * Redistribution and use in source and binary forms, with or without
7 1.1 kiyohara * modification, are permitted provided that the following conditions
8 1.1 kiyohara * are met:
9 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright
10 1.1 kiyohara * notice, this list of conditions and the following disclaimer.
11 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the
13 1.1 kiyohara * documentation and/or other materials provided with the distribution.
14 1.1 kiyohara *
15 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
26 1.1 kiyohara */
27 1.1 kiyohara #include <sys/cdefs.h>
28 1.1 kiyohara __KERNEL_RCSID(0, "$NetBSD: nbpiic.c,v 1.1 2011/08/06 03:53:40 kiyohara Exp $");
29 1.1 kiyohara
30 1.1 kiyohara #include <sys/param.h>
31 1.1 kiyohara #include <sys/device.h>
32 1.1 kiyohara #include <sys/mutex.h>
33 1.1 kiyohara
34 1.1 kiyohara #include <machine/platid.h>
35 1.1 kiyohara #include <machine/platid_mask.h>
36 1.1 kiyohara
37 1.1 kiyohara #include <arm/xscale/pxa2x0var.h>
38 1.1 kiyohara #include <arm/xscale/pxa2x0_i2c.h>
39 1.1 kiyohara
40 1.1 kiyohara #include <hpcarm/dev/nbppconvar.h>
41 1.1 kiyohara
42 1.1 kiyohara #include <dev/i2c/i2cvar.h>
43 1.1 kiyohara
44 1.1 kiyohara #include "nbppcon.h"
45 1.1 kiyohara
46 1.1 kiyohara #define NBPIIC_SLAVE_ADDR 0x70
47 1.1 kiyohara
48 1.1 kiyohara struct nbpiic_softc {
49 1.1 kiyohara struct pxa2x0_i2c_softc sc_pxa_i2c;
50 1.1 kiyohara
51 1.1 kiyohara struct i2c_controller sc_i2c;
52 1.1 kiyohara kmutex_t sc_lock;
53 1.1 kiyohara
54 1.1 kiyohara device_t sc_pcon;
55 1.1 kiyohara char sc_buf[16];
56 1.1 kiyohara int sc_idx;
57 1.1 kiyohara };
58 1.1 kiyohara
59 1.1 kiyohara
60 1.1 kiyohara static int pxaiic_match(device_t, cfdata_t, void *);
61 1.1 kiyohara static void pxaiic_attach(device_t, device_t, void *);
62 1.1 kiyohara
63 1.1 kiyohara static int nbpiic_intr(void *);
64 1.1 kiyohara int nbpiic_poll(void *, int, char *);
65 1.1 kiyohara
66 1.1 kiyohara /* fuctions for i2c_controller */
67 1.1 kiyohara static int nbpiic_acquire_bus(void *, int);
68 1.1 kiyohara static void nbpiic_release_bus(void *, int);
69 1.1 kiyohara static int nbpiic_exec(void *cookie, i2c_op_t, i2c_addr_t, const void *, size_t,
70 1.1 kiyohara void *, size_t, int);
71 1.1 kiyohara
72 1.1 kiyohara CFATTACH_DECL_NEW(pxaiic, sizeof(struct nbpiic_softc),
73 1.1 kiyohara pxaiic_match, pxaiic_attach, NULL, NULL);
74 1.1 kiyohara
75 1.1 kiyohara
76 1.1 kiyohara /* ARGSUSED */
77 1.1 kiyohara static int
78 1.1 kiyohara pxaiic_match(device_t parent, cfdata_t match, void *aux)
79 1.1 kiyohara {
80 1.1 kiyohara struct pxaip_attach_args *pxa = aux;
81 1.1 kiyohara
82 1.1 kiyohara if (strcmp(pxa->pxa_name, match->cf_name) != 0 ||
83 1.1 kiyohara !platid_match(&platid, &platid_mask_MACH_PSIONTEKLOGIX_NETBOOK_PRO))
84 1.1 kiyohara return 0;
85 1.1 kiyohara
86 1.1 kiyohara pxa->pxa_size = PXA2X0_I2C_SIZE;
87 1.1 kiyohara return 1;
88 1.1 kiyohara }
89 1.1 kiyohara
90 1.1 kiyohara /* ARGSUSED */
91 1.1 kiyohara static void
92 1.1 kiyohara pxaiic_attach(device_t parent, device_t self, void *aux)
93 1.1 kiyohara {
94 1.1 kiyohara struct nbpiic_softc *sc = device_private(self);
95 1.1 kiyohara struct pxaip_attach_args *pxa = aux;
96 1.1 kiyohara struct i2cbus_attach_args iba;
97 1.1 kiyohara void *ih;
98 1.1 kiyohara
99 1.1 kiyohara aprint_normal("\n");
100 1.1 kiyohara aprint_naive("\n");
101 1.1 kiyohara
102 1.1 kiyohara sc->sc_pxa_i2c.sc_dev = self;
103 1.1 kiyohara sc->sc_pxa_i2c.sc_iot = pxa->pxa_iot;
104 1.1 kiyohara sc->sc_pxa_i2c.sc_addr = pxa->pxa_addr;
105 1.1 kiyohara sc->sc_pxa_i2c.sc_size = pxa->pxa_size;
106 1.1 kiyohara sc->sc_pxa_i2c.sc_isar = NBPIIC_SLAVE_ADDR;
107 1.1 kiyohara sc->sc_pxa_i2c.sc_stat = PI2C_STAT_INIT;
108 1.1 kiyohara if (pxa2x0_i2c_attach_sub(&sc->sc_pxa_i2c)) {
109 1.1 kiyohara aprint_error_dev(self, "unable to attach PXA I2C\n");
110 1.1 kiyohara return;
111 1.1 kiyohara }
112 1.1 kiyohara
113 1.1 kiyohara /* Initialize mutex with IPL_HIGH. Keyboard was connected to us. */
114 1.1 kiyohara mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
115 1.1 kiyohara
116 1.1 kiyohara ih = pxa2x0_intr_establish(pxa->pxa_intr, IPL_HIGH, nbpiic_intr, sc);
117 1.1 kiyohara if (ih == NULL) {
118 1.1 kiyohara aprint_error_dev(self, "intr_establish failed\n");
119 1.1 kiyohara return;
120 1.1 kiyohara }
121 1.1 kiyohara
122 1.1 kiyohara /* Initialize i2c_controller */
123 1.1 kiyohara sc->sc_i2c.ic_cookie = sc;
124 1.1 kiyohara sc->sc_i2c.ic_acquire_bus = nbpiic_acquire_bus;
125 1.1 kiyohara sc->sc_i2c.ic_release_bus = nbpiic_release_bus;
126 1.1 kiyohara sc->sc_i2c.ic_send_start = NULL;
127 1.1 kiyohara sc->sc_i2c.ic_send_stop = NULL;
128 1.1 kiyohara sc->sc_i2c.ic_initiate_xfer = NULL;
129 1.1 kiyohara sc->sc_i2c.ic_read_byte = NULL;
130 1.1 kiyohara sc->sc_i2c.ic_write_byte = NULL;
131 1.1 kiyohara sc->sc_i2c.ic_exec = nbpiic_exec;
132 1.1 kiyohara
133 1.1 kiyohara iba.iba_tag = &sc->sc_i2c;
134 1.1 kiyohara pxa2x0_i2c_open(&sc->sc_pxa_i2c);
135 1.1 kiyohara config_found_ia(self, "i2cbus", &iba, iicbus_print);
136 1.1 kiyohara
137 1.1 kiyohara sc->sc_pcon = device_find_by_xname("nbppcon0");
138 1.1 kiyohara
139 1.1 kiyohara /* Don't close I2C-bus. We are slave device. */
140 1.1 kiyohara }
141 1.1 kiyohara
142 1.1 kiyohara static int
143 1.1 kiyohara nbpiic_intr(void *arg)
144 1.1 kiyohara {
145 1.1 kiyohara struct nbpiic_softc *sc = arg;
146 1.1 kiyohara struct pxa2x0_i2c_softc *pi2c = &sc->sc_pxa_i2c;
147 1.1 kiyohara int handled, len;
148 1.1 kiyohara
149 1.1 kiyohara handled = pxa2x0_i2c_intr_sub(pi2c, &len, &sc->sc_buf[sc->sc_idx]);
150 1.1 kiyohara if (len != 0)
151 1.1 kiyohara sc->sc_idx += len;
152 1.1 kiyohara if (pi2c->sc_stat == PI2C_STAT_STOP) {
153 1.1 kiyohara #if NNBPPCON > 0
154 1.1 kiyohara nbppcon_intr(sc->sc_pcon, sc->sc_idx, sc->sc_buf);
155 1.1 kiyohara #endif
156 1.1 kiyohara sc->sc_idx = 0;
157 1.1 kiyohara pi2c->sc_stat = PI2C_STAT_INIT;
158 1.1 kiyohara }
159 1.1 kiyohara
160 1.1 kiyohara return handled;
161 1.1 kiyohara }
162 1.1 kiyohara
163 1.1 kiyohara int
164 1.1 kiyohara nbpiic_poll(void *cookie, int buflen, char *buf)
165 1.1 kiyohara {
166 1.1 kiyohara struct nbpiic_softc *sc = cookie;
167 1.1 kiyohara int rv;
168 1.1 kiyohara
169 1.1 kiyohara if (sc->sc_idx > 0) {
170 1.1 kiyohara if (buflen < sc->sc_idx) {
171 1.1 kiyohara (void) pxa2x0_i2c_poll(&sc->sc_pxa_i2c,
172 1.1 kiyohara sizeof(sc->sc_buf) - sc->sc_idx, buf + sc->sc_idx,
173 1.1 kiyohara I2C_F_READ);
174 1.1 kiyohara sc->sc_idx = 0;
175 1.1 kiyohara } else
176 1.1 kiyohara memcpy(buf, sc->sc_buf, sc->sc_idx);
177 1.1 kiyohara }
178 1.1 kiyohara rv = pxa2x0_i2c_poll(&sc->sc_pxa_i2c,
179 1.1 kiyohara buflen - sc->sc_idx, buf + sc->sc_idx, I2C_F_READ);
180 1.1 kiyohara sc->sc_idx = 0;
181 1.1 kiyohara
182 1.1 kiyohara return rv;
183 1.1 kiyohara }
184 1.1 kiyohara
185 1.1 kiyohara static int
186 1.1 kiyohara nbpiic_acquire_bus(void *cookie, int flags)
187 1.1 kiyohara {
188 1.1 kiyohara struct nbpiic_softc *sc = cookie;
189 1.1 kiyohara
190 1.1 kiyohara mutex_enter(&sc->sc_lock);
191 1.1 kiyohara
192 1.1 kiyohara return 0;
193 1.1 kiyohara }
194 1.1 kiyohara
195 1.1 kiyohara static void
196 1.1 kiyohara nbpiic_release_bus(void *cookie, int flags)
197 1.1 kiyohara {
198 1.1 kiyohara struct nbpiic_softc *sc = cookie;
199 1.1 kiyohara
200 1.1 kiyohara mutex_exit(&sc->sc_lock);
201 1.1 kiyohara
202 1.1 kiyohara return;
203 1.1 kiyohara }
204 1.1 kiyohara
205 1.1 kiyohara static int
206 1.1 kiyohara nbpiic_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
207 1.1 kiyohara size_t cmdlen, void *vbuf, size_t buflen, int flags)
208 1.1 kiyohara {
209 1.1 kiyohara struct nbpiic_softc *sc = cookie;
210 1.1 kiyohara int rv = -1;
211 1.1 kiyohara const u_char cmd = *(const u_char *)vcmd;
212 1.1 kiyohara
213 1.1 kiyohara if (I2C_OP_READ_P(op) && (cmdlen == 0) && (buflen == 1))
214 1.1 kiyohara rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c, addr, (u_char *)vbuf);
215 1.1 kiyohara
216 1.1 kiyohara if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
217 1.1 kiyohara rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, cmd);
218 1.1 kiyohara if (rv == 0)
219 1.1 kiyohara rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
220 1.1 kiyohara addr, (u_char *)vbuf);
221 1.1 kiyohara }
222 1.1 kiyohara
223 1.1 kiyohara if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 2)) {
224 1.1 kiyohara printf("%s: read cmdlen=1, buflen=2: Ooops, maybe error...\n", __func__);
225 1.1 kiyohara rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, cmd);
226 1.1 kiyohara if (rv == 0)
227 1.1 kiyohara rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
228 1.1 kiyohara addr, &((u_char *)vbuf)[0]);
229 1.1 kiyohara if (rv == 0)
230 1.1 kiyohara rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
231 1.1 kiyohara addr, &((u_char *)vbuf)[1]);
232 1.1 kiyohara }
233 1.1 kiyohara
234 1.1 kiyohara if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1))
235 1.1 kiyohara rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, *(u_char *)vbuf);
236 1.1 kiyohara
237 1.1 kiyohara if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1)) {
238 1.1 kiyohara u_short v = (cmd << 8) | ((u_char *)vbuf)[0];
239 1.1 kiyohara
240 1.1 kiyohara rv = pxa2x0_i2c_write_2(&sc->sc_pxa_i2c, addr, v);
241 1.1 kiyohara }
242 1.1 kiyohara
243 1.1 kiyohara if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 2)) {
244 1.1 kiyohara printf("%s: write cmdlen=1, buflen=2: Ooops, maybe error...\n", __func__);
245 1.1 kiyohara rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, cmd);
246 1.1 kiyohara if (rv == 0) {
247 1.1 kiyohara u_short v =
248 1.1 kiyohara (((u_char *)vbuf)[0] << 8) | ((u_char *)vbuf)[1];
249 1.1 kiyohara
250 1.1 kiyohara rv = pxa2x0_i2c_write_2(&sc->sc_pxa_i2c, addr, v);
251 1.1 kiyohara }
252 1.1 kiyohara }
253 1.1 kiyohara
254 1.1 kiyohara /* Handle quick_read/quick_write ops - XXX Untested XXX */
255 1.1 kiyohara if ((cmdlen == 0) && (buflen == 0))
256 1.1 kiyohara rv = pxa2x0_i2c_quick(&sc->sc_pxa_i2c, addr,
257 1.1 kiyohara I2C_OP_READ_P(op) ? 1 : 0);
258 1.1 kiyohara
259 1.1 kiyohara return rv;
260 1.1 kiyohara }
261