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