gxiic.c revision 1.8 1 /* $NetBSD: gxiic.c,v 1.8 2016/02/14 19:54:20 chs 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.8 2016/02/14 19:54:20 chs 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_NEW(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_addr = pxa->pxa_addr;
92 sc->sc_pxa_i2c.sc_size = pxa->pxa_size;
93 sc->sc_pxa_i2c.sc_flags = 0;
94 if (pxa2x0_i2c_attach_sub(&sc->sc_pxa_i2c)) {
95 aprint_error_dev(self, "unable to attach PXA I2C\n");
96 return;
97 }
98
99 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
100
101 /* Initialize i2c_controller */
102 sc->sc_i2c.ic_cookie = sc;
103 sc->sc_i2c.ic_acquire_bus = gxiic_acquire_bus;
104 sc->sc_i2c.ic_release_bus = gxiic_release_bus;
105 sc->sc_i2c.ic_send_start = NULL;
106 sc->sc_i2c.ic_send_stop = NULL;
107 sc->sc_i2c.ic_initiate_xfer = NULL;
108 sc->sc_i2c.ic_read_byte = NULL;
109 sc->sc_i2c.ic_write_byte = NULL;
110 sc->sc_i2c.ic_exec = gxiic_exec;
111
112 memset(&iba, 0, sizeof(iba));
113 iba.iba_tag = &sc->sc_i2c;
114 pxa2x0_i2c_open(&sc->sc_pxa_i2c);
115 config_found_ia(sc->sc_pxa_i2c.sc_dev, "i2cbus", &iba, iicbus_print);
116 pxa2x0_i2c_close(&sc->sc_pxa_i2c);
117 }
118
119 static int
120 gxiic_acquire_bus(void *cookie, int flags)
121 {
122 struct gxiic_softc *sc = cookie;
123
124 mutex_enter(&sc->sc_lock);
125 pxa2x0_i2c_open(&sc->sc_pxa_i2c);
126
127 return 0;
128 }
129
130 static void
131 gxiic_release_bus(void *cookie, int flags)
132 {
133 struct gxiic_softc *sc = cookie;
134
135 pxa2x0_i2c_close(&sc->sc_pxa_i2c);
136 mutex_exit(&sc->sc_lock);
137 return;
138 }
139
140 static int
141 gxiic_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
142 size_t cmdlen, void *vbuf, size_t buflen, int flags)
143 {
144 struct gxiic_softc *sc = cookie;
145 int rv = -1;
146
147 if (I2C_OP_READ_P(op) && (cmdlen == 0) && (buflen == 1))
148 rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c, addr, (u_char *)vbuf);
149
150 if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
151 rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, *(u_char *)vbuf);
152 if (rv == 0)
153 rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
154 addr, (u_char *)vbuf);
155 }
156
157 if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 2)) {
158 rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, *(u_char *)vbuf);
159 if (rv == 0)
160 rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
161 addr, (u_char *)vbuf);
162 if (rv == 0)
163 rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
164 addr, (u_char *)(vbuf) + 1);
165 }
166
167 if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1))
168 rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, *(u_char *)vbuf);
169
170 if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1)) {
171 rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c,
172 addr, *(const u_char *)vcmd);
173 if (rv == 0)
174 rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c,
175 addr, *(u_char *)vbuf);
176 }
177
178 if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 2)) {
179 rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c,
180 addr, *(const u_char *)vcmd);
181 if (rv == 0)
182 rv = pxa2x0_i2c_write_2(&sc->sc_pxa_i2c,
183 addr, *(u_short *)vbuf);
184 }
185
186 /* Handle quick_read/quick_write ops - XXX Untested XXX */
187 if ((cmdlen == 0) && (buflen == 0))
188 rv = pxa2x0_i2c_quick(&sc->sc_pxa_i2c, addr,
189 I2C_OP_READ_P(op)?1:0);
190
191 return rv;
192 }
193