gxiic.c revision 1.5.10.1 1 /* $NetBSD: gxiic.c,v 1.5.10.1 2011/06/23 14:19:06 cherry Exp $ */
2 /*
3 * Copyright (c) 2007 KIYOHARA Takashi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: gxiic.c,v 1.5.10.1 2011/06/23 14:19:06 cherry Exp $");
29
30 #include <sys/param.h>
31 #include <sys/device.h>
32 #include <sys/errno.h>
33 #include <sys/mutex.h>
34
35 #include <arm/xscale/pxa2x0var.h>
36 #include <arm/xscale/pxa2x0_i2c.h>
37
38 #include <evbarm/gumstix/gumstixvar.h>
39
40 #include <dev/i2c/i2cvar.h>
41
42
43 struct gxiic_softc {
44 struct pxa2x0_i2c_softc sc_pxa_i2c;
45
46 struct i2c_controller sc_i2c;
47 kmutex_t sc_lock;
48 };
49
50
51 static int gxiicmatch(device_t, cfdata_t, void *);
52 static void gxiicattach(device_t, device_t, void *);
53
54 /* fuctions for i2c_controller */
55 static int gxiic_acquire_bus(void *, int);
56 static void gxiic_release_bus(void *, int);
57 static int gxiic_exec(void *cookie, i2c_op_t, i2c_addr_t, const void *, size_t,
58 void *, size_t, int);
59
60
61 CFATTACH_DECL(gxiic, sizeof(struct gxiic_softc),
62 gxiicmatch, gxiicattach, NULL, NULL);
63
64
65 /* ARGSUSED */
66 static int
67 gxiicmatch(device_t parent, cfdata_t match, void *aux)
68 {
69 struct pxaip_attach_args *pxa = aux;
70
71 if (strcmp(pxa->pxa_name, match->cf_name) != 0)
72 return 0;
73
74 pxa->pxa_size = PXA2X0_I2C_SIZE;
75 return 1;
76 }
77
78 /* ARGSUSED */
79 static void
80 gxiicattach(device_t parent, device_t self, void *aux)
81 {
82 struct pxaip_attach_args *pxa = aux;
83 struct gxiic_softc *sc = device_private(self);
84 struct i2cbus_attach_args iba;
85
86 aprint_normal("\n");
87 aprint_naive("\n");
88
89 sc->sc_pxa_i2c.sc_dev = self;
90 sc->sc_pxa_i2c.sc_iot = pxa->pxa_iot;
91 sc->sc_pxa_i2c.sc_size = pxa->pxa_size;
92 sc->sc_pxa_i2c.sc_flags = 0;
93 if (pxa2x0_i2c_attach_sub(&sc->sc_pxa_i2c)) {
94 aprint_error_dev(self, "unable to attach PXA I2C\n");
95 return;
96 }
97
98 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
99
100 /* Initialize i2c_controller */
101 sc->sc_i2c.ic_cookie = sc;
102 sc->sc_i2c.ic_acquire_bus = gxiic_acquire_bus;
103 sc->sc_i2c.ic_release_bus = gxiic_release_bus;
104 sc->sc_i2c.ic_send_start = NULL;
105 sc->sc_i2c.ic_send_stop = NULL;
106 sc->sc_i2c.ic_initiate_xfer = NULL;
107 sc->sc_i2c.ic_read_byte = NULL;
108 sc->sc_i2c.ic_write_byte = NULL;
109 sc->sc_i2c.ic_exec = gxiic_exec;
110
111 iba.iba_tag = &sc->sc_i2c;
112 pxa2x0_i2c_open(&sc->sc_pxa_i2c);
113 config_found_ia(sc->sc_pxa_i2c.sc_dev, "i2cbus", &iba, iicbus_print);
114 pxa2x0_i2c_close(&sc->sc_pxa_i2c);
115 }
116
117 static int
118 gxiic_acquire_bus(void *cookie, int flags)
119 {
120 struct gxiic_softc *sc = cookie;
121
122 mutex_enter(&sc->sc_lock);
123 pxa2x0_i2c_open(&sc->sc_pxa_i2c);
124
125 return 0;
126 }
127
128 static void
129 gxiic_release_bus(void *cookie, int flags)
130 {
131 struct gxiic_softc *sc = cookie;
132
133 pxa2x0_i2c_close(&sc->sc_pxa_i2c);
134 mutex_exit(&sc->sc_lock);
135 return;
136 }
137
138 static int
139 gxiic_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
140 size_t cmdlen, void *vbuf, size_t buflen, int flags)
141 {
142 struct gxiic_softc *sc = cookie;
143 int rv = -1;
144
145 if (I2C_OP_READ_P(op) && (cmdlen == 0) && (buflen == 1))
146 rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c, addr, (u_char *)vbuf);
147
148 if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
149 rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, *(u_char *)vbuf);
150 if (rv == 0)
151 rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
152 addr, (u_char *)vbuf);
153 }
154
155 if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 2)) {
156 rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, *(u_char *)vbuf);
157 if (rv == 0)
158 rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
159 addr, (u_char *)vbuf);
160 if (rv == 0)
161 rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
162 addr, (u_char *)(vbuf) + 1);
163 }
164
165 if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1))
166 rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, *(u_char *)vbuf);
167
168 if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1)) {
169 rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c,
170 addr, *(const u_char *)vcmd);
171 if (rv == 0)
172 rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c,
173 addr, *(u_char *)vbuf);
174 }
175
176 if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 2)) {
177 rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c,
178 addr, *(const u_char *)vcmd);
179 if (rv == 0)
180 rv = pxa2x0_i2c_write_2(&sc->sc_pxa_i2c,
181 addr, *(u_short *)vbuf);
182 }
183
184 /* Handle quick_read/quick_write ops - XXX Untested XXX */
185 if ((cmdlen == 0) && (buflen == 0))
186 rv = pxa2x0_i2c_quick(&sc->sc_pxa_i2c, addr,
187 I2C_OP_READ_P(op)?1:0);
188
189 return rv;
190 }
191