pca9564.c revision 1.1.2.2 1 1.1.2.2 uebayasi /* $NetBSD: pca9564.c,v 1.1.2.2 2010/04/30 14:43:20 uebayasi Exp $ */
2 1.1.2.2 uebayasi
3 1.1.2.2 uebayasi /*
4 1.1.2.2 uebayasi * Copyright (c) 2010 NONAKA Kimihiro <nonaka (at) netbsd.org>
5 1.1.2.2 uebayasi * All rights reserved.
6 1.1.2.2 uebayasi *
7 1.1.2.2 uebayasi * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 uebayasi * modification, are permitted provided that the following conditions
9 1.1.2.2 uebayasi * are met:
10 1.1.2.2 uebayasi * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 uebayasi * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 uebayasi * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 uebayasi * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 uebayasi * documentation and/or other materials provided with the distribution.
15 1.1.2.2 uebayasi *
16 1.1.2.2 uebayasi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.2.2 uebayasi * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.2.2 uebayasi * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.2.2 uebayasi * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.2.2 uebayasi * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1.2.2 uebayasi * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1.2.2 uebayasi * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1.2.2 uebayasi * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1.2.2 uebayasi * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1.2.2 uebayasi * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1.2.2 uebayasi */
27 1.1.2.2 uebayasi
28 1.1.2.2 uebayasi #include <sys/cdefs.h>
29 1.1.2.2 uebayasi __KERNEL_RCSID(0, "$NetBSD: pca9564.c,v 1.1.2.2 2010/04/30 14:43:20 uebayasi Exp $");
30 1.1.2.2 uebayasi
31 1.1.2.2 uebayasi #include <sys/param.h>
32 1.1.2.2 uebayasi #include <sys/device.h>
33 1.1.2.2 uebayasi #include <sys/mutex.h>
34 1.1.2.2 uebayasi #include <sys/bus.h>
35 1.1.2.2 uebayasi
36 1.1.2.2 uebayasi #include <dev/i2c/i2cvar.h>
37 1.1.2.2 uebayasi
38 1.1.2.2 uebayasi #include <dev/ic/pca9564reg.h>
39 1.1.2.2 uebayasi #include <dev/ic/pca9564var.h>
40 1.1.2.2 uebayasi
41 1.1.2.2 uebayasi #if defined(PCA9564_DEBUG)
42 1.1.2.2 uebayasi int pca9564debug = 0;
43 1.1.2.2 uebayasi #define DPRINTF(s) if (pca9564debug) printf s
44 1.1.2.2 uebayasi #else
45 1.1.2.2 uebayasi #define DPRINTF(s)
46 1.1.2.2 uebayasi #endif
47 1.1.2.2 uebayasi
48 1.1.2.2 uebayasi static int pca9564_acquire_bus(void *, int);
49 1.1.2.2 uebayasi static void pca9564_release_bus(void *, int);
50 1.1.2.2 uebayasi
51 1.1.2.2 uebayasi static int pca9564_send_start(void *, int);
52 1.1.2.2 uebayasi static int pca9564_send_stop(void *, int);
53 1.1.2.2 uebayasi static int pca9564_initiate_xfer(void *, uint16_t, int);
54 1.1.2.2 uebayasi static int pca9564_read_byte(void *, uint8_t *, int);
55 1.1.2.2 uebayasi static int pca9564_write_byte(void *, uint8_t, int);
56 1.1.2.2 uebayasi
57 1.1.2.2 uebayasi static int pca9564_ack(void *, bool, int);
58 1.1.2.2 uebayasi
59 1.1.2.2 uebayasi #define CSR_READ(sc, r) (*sc->sc_ios.read_byte)(sc->sc_dev, r)
60 1.1.2.2 uebayasi #define CSR_WRITE(sc, r, v) (*sc->sc_ios.write_byte)(sc->sc_dev, r, v)
61 1.1.2.2 uebayasi
62 1.1.2.2 uebayasi void
63 1.1.2.2 uebayasi pca9564_attach(struct pca9564_softc *sc)
64 1.1.2.2 uebayasi {
65 1.1.2.2 uebayasi struct i2cbus_attach_args iba;
66 1.1.2.2 uebayasi
67 1.1.2.2 uebayasi aprint_naive("\n");
68 1.1.2.2 uebayasi aprint_normal(": PCA9564 I2C Controller\n");
69 1.1.2.2 uebayasi
70 1.1.2.2 uebayasi mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
71 1.1.2.2 uebayasi
72 1.1.2.2 uebayasi sc->sc_i2c.ic_cookie = sc;
73 1.1.2.2 uebayasi sc->sc_i2c.ic_acquire_bus = pca9564_acquire_bus;
74 1.1.2.2 uebayasi sc->sc_i2c.ic_release_bus = pca9564_release_bus;
75 1.1.2.2 uebayasi sc->sc_i2c.ic_send_start = pca9564_send_start;
76 1.1.2.2 uebayasi sc->sc_i2c.ic_send_stop = pca9564_send_stop;
77 1.1.2.2 uebayasi sc->sc_i2c.ic_initiate_xfer = pca9564_initiate_xfer;
78 1.1.2.2 uebayasi sc->sc_i2c.ic_read_byte = pca9564_read_byte;
79 1.1.2.2 uebayasi sc->sc_i2c.ic_write_byte = pca9564_write_byte;
80 1.1.2.2 uebayasi sc->sc_i2c.ic_exec = NULL;
81 1.1.2.2 uebayasi
82 1.1.2.2 uebayasi /* set serial clock rate */
83 1.1.2.2 uebayasi switch (sc->sc_i2c_clock) {
84 1.1.2.2 uebayasi case 330000: /* 330kHz */
85 1.1.2.2 uebayasi sc->sc_i2c_clock = I2CCON_CR_330KHZ;
86 1.1.2.2 uebayasi break;
87 1.1.2.2 uebayasi case 288000: /* 288kHz */
88 1.1.2.2 uebayasi sc->sc_i2c_clock = I2CCON_CR_288KHZ;
89 1.1.2.2 uebayasi break;
90 1.1.2.2 uebayasi case 217000: /* 217kHz */
91 1.1.2.2 uebayasi sc->sc_i2c_clock = I2CCON_CR_217KHZ;
92 1.1.2.2 uebayasi break;
93 1.1.2.2 uebayasi case 146000: /* 146kHz */
94 1.1.2.2 uebayasi sc->sc_i2c_clock = I2CCON_CR_146KHZ;
95 1.1.2.2 uebayasi break;
96 1.1.2.2 uebayasi case 88000: /* 88kHz */
97 1.1.2.2 uebayasi sc->sc_i2c_clock = I2CCON_CR_88KHZ;
98 1.1.2.2 uebayasi break;
99 1.1.2.2 uebayasi case 0: /* default */
100 1.1.2.2 uebayasi case 59000: /* 59kHz */
101 1.1.2.2 uebayasi sc->sc_i2c_clock = I2CCON_CR_59KHZ;
102 1.1.2.2 uebayasi break;
103 1.1.2.2 uebayasi case 44000: /* 44kHz */
104 1.1.2.2 uebayasi sc->sc_i2c_clock = I2CCON_CR_44KHZ;
105 1.1.2.2 uebayasi break;
106 1.1.2.2 uebayasi case 36000: /* 36kHz */
107 1.1.2.2 uebayasi sc->sc_i2c_clock = I2CCON_CR_36KHZ;
108 1.1.2.2 uebayasi break;
109 1.1.2.2 uebayasi default:
110 1.1.2.2 uebayasi aprint_error_dev(sc->sc_dev, "unknown i2c clock %dHz\n",
111 1.1.2.2 uebayasi sc->sc_i2c_clock);
112 1.1.2.2 uebayasi sc->sc_i2c_clock = I2CCON_CR_59KHZ;
113 1.1.2.2 uebayasi break;
114 1.1.2.2 uebayasi }
115 1.1.2.2 uebayasi
116 1.1.2.2 uebayasi iba.iba_tag = &sc->sc_i2c;
117 1.1.2.2 uebayasi (void) config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
118 1.1.2.2 uebayasi }
119 1.1.2.2 uebayasi
120 1.1.2.2 uebayasi static int
121 1.1.2.2 uebayasi pca9564_acquire_bus(void *cookie, int flags)
122 1.1.2.2 uebayasi {
123 1.1.2.2 uebayasi struct pca9564_softc *sc = cookie;
124 1.1.2.2 uebayasi uint8_t control;
125 1.1.2.2 uebayasi
126 1.1.2.2 uebayasi mutex_enter(&sc->sc_buslock);
127 1.1.2.2 uebayasi
128 1.1.2.2 uebayasi /* Enable SIO and set clock */
129 1.1.2.2 uebayasi control = CSR_READ(sc, PCA9564_I2CCON);
130 1.1.2.2 uebayasi control |= I2CCON_ENSIO;
131 1.1.2.2 uebayasi control &= ~(I2CCON_STA|I2CCON_STO|I2CCON_SI|I2CCON_AA);
132 1.1.2.2 uebayasi control &= ~I2CCON_CR_MASK;
133 1.1.2.2 uebayasi control |= sc->sc_i2c_clock;
134 1.1.2.2 uebayasi CSR_WRITE(sc, PCA9564_I2CCON, control);
135 1.1.2.2 uebayasi delay(500);
136 1.1.2.2 uebayasi
137 1.1.2.2 uebayasi return 0;
138 1.1.2.2 uebayasi }
139 1.1.2.2 uebayasi
140 1.1.2.2 uebayasi static void
141 1.1.2.2 uebayasi pca9564_release_bus(void *cookie, int flags)
142 1.1.2.2 uebayasi {
143 1.1.2.2 uebayasi struct pca9564_softc *sc = cookie;
144 1.1.2.2 uebayasi uint8_t control;
145 1.1.2.2 uebayasi
146 1.1.2.2 uebayasi /* Disable SIO */
147 1.1.2.2 uebayasi control = CSR_READ(sc, PCA9564_I2CCON);
148 1.1.2.2 uebayasi control &= ~I2CCON_ENSIO;
149 1.1.2.2 uebayasi CSR_WRITE(sc, PCA9564_I2CCON, control);
150 1.1.2.2 uebayasi
151 1.1.2.2 uebayasi mutex_exit(&sc->sc_buslock);
152 1.1.2.2 uebayasi }
153 1.1.2.2 uebayasi
154 1.1.2.2 uebayasi #define PCA9564_TIMEOUT 100 /* protocol timeout, in uSecs */
155 1.1.2.2 uebayasi
156 1.1.2.2 uebayasi static int
157 1.1.2.2 uebayasi pca9564_wait(struct pca9564_softc *sc, int flags)
158 1.1.2.2 uebayasi {
159 1.1.2.2 uebayasi int timeout;
160 1.1.2.2 uebayasi
161 1.1.2.2 uebayasi DPRINTF(("%s: status=%#x\n", __func__, CSR_READ(sc, PCA9564_I2CSTA)));
162 1.1.2.2 uebayasi for (timeout = PCA9564_TIMEOUT; timeout > 0; timeout--) {
163 1.1.2.2 uebayasi if (CSR_READ(sc, PCA9564_I2CCON) & I2CCON_SI)
164 1.1.2.2 uebayasi break;
165 1.1.2.2 uebayasi delay(1);
166 1.1.2.2 uebayasi }
167 1.1.2.2 uebayasi DPRINTF(("%s: status=%#x\n", __func__, CSR_READ(sc, PCA9564_I2CSTA)));
168 1.1.2.2 uebayasi if (timeout == 0) {
169 1.1.2.2 uebayasi aprint_error_dev(sc->sc_dev, "timeout\n");
170 1.1.2.2 uebayasi return ETIMEDOUT;
171 1.1.2.2 uebayasi }
172 1.1.2.2 uebayasi return 0;
173 1.1.2.2 uebayasi }
174 1.1.2.2 uebayasi
175 1.1.2.2 uebayasi static int
176 1.1.2.2 uebayasi pca9564_send_start(void *cookie, int flags)
177 1.1.2.2 uebayasi {
178 1.1.2.2 uebayasi struct pca9564_softc *sc = cookie;
179 1.1.2.2 uebayasi uint8_t control;
180 1.1.2.2 uebayasi
181 1.1.2.2 uebayasi DPRINTF(("%s: status=%#x\n", __func__, CSR_READ(sc, PCA9564_I2CSTA)));
182 1.1.2.2 uebayasi control = CSR_READ(sc, PCA9564_I2CCON);
183 1.1.2.2 uebayasi control |= I2CCON_STA;
184 1.1.2.2 uebayasi control &= ~(I2CCON_STO|I2CCON_SI);
185 1.1.2.2 uebayasi CSR_WRITE(sc, PCA9564_I2CCON, control);
186 1.1.2.2 uebayasi DPRINTF(("%s: status=%#x\n", __func__, CSR_READ(sc, PCA9564_I2CSTA)));
187 1.1.2.2 uebayasi
188 1.1.2.2 uebayasi return pca9564_wait(sc, flags);
189 1.1.2.2 uebayasi }
190 1.1.2.2 uebayasi
191 1.1.2.2 uebayasi static int
192 1.1.2.2 uebayasi pca9564_send_stop(void *cookie, int flags)
193 1.1.2.2 uebayasi {
194 1.1.2.2 uebayasi struct pca9564_softc *sc = cookie;
195 1.1.2.2 uebayasi uint8_t control;
196 1.1.2.2 uebayasi
197 1.1.2.2 uebayasi DPRINTF(("%s: status=%#x\n", __func__, CSR_READ(sc, PCA9564_I2CSTA)));
198 1.1.2.2 uebayasi control = CSR_READ(sc, PCA9564_I2CCON);
199 1.1.2.2 uebayasi control |= I2CCON_STO;
200 1.1.2.2 uebayasi control &= ~(I2CCON_STA|I2CCON_SI);
201 1.1.2.2 uebayasi CSR_WRITE(sc, PCA9564_I2CCON, control);
202 1.1.2.2 uebayasi DPRINTF(("%s: status=%#x\n", __func__, CSR_READ(sc, PCA9564_I2CSTA)));
203 1.1.2.2 uebayasi
204 1.1.2.2 uebayasi return 0;
205 1.1.2.2 uebayasi }
206 1.1.2.2 uebayasi
207 1.1.2.2 uebayasi static int
208 1.1.2.2 uebayasi pca9564_initiate_xfer(void *cookie, uint16_t addr, int flags)
209 1.1.2.2 uebayasi {
210 1.1.2.2 uebayasi struct pca9564_softc *sc = cookie;
211 1.1.2.2 uebayasi int error, rd_req = (flags & I2C_F_READ) != 0;
212 1.1.2.2 uebayasi uint8_t data, control;
213 1.1.2.2 uebayasi
214 1.1.2.2 uebayasi error = pca9564_send_start(sc, flags);
215 1.1.2.2 uebayasi if (error) {
216 1.1.2.2 uebayasi aprint_error_dev(sc->sc_dev, "failed to send start %s xfer\n",
217 1.1.2.2 uebayasi rd_req ? "read" : "write");
218 1.1.2.2 uebayasi return error;
219 1.1.2.2 uebayasi }
220 1.1.2.2 uebayasi
221 1.1.2.2 uebayasi DPRINTF(("%s: status=%#x\n", __func__, CSR_READ(sc, PCA9564_I2CSTA)));
222 1.1.2.2 uebayasi control = CSR_READ(sc, PCA9564_I2CCON);
223 1.1.2.2 uebayasi
224 1.1.2.2 uebayasi data = (addr << 1) | (rd_req ? 1 : 0);
225 1.1.2.2 uebayasi CSR_WRITE(sc, PCA9564_I2CDAT, data);
226 1.1.2.2 uebayasi
227 1.1.2.2 uebayasi control &= ~(I2CCON_STO|I2CCON_STA|I2CCON_SI);
228 1.1.2.2 uebayasi CSR_WRITE(sc, PCA9564_I2CCON, control);
229 1.1.2.2 uebayasi DPRINTF(("%s: status=%#x\n", __func__, CSR_READ(sc, PCA9564_I2CSTA)));
230 1.1.2.2 uebayasi
231 1.1.2.2 uebayasi error = pca9564_wait(sc, flags);
232 1.1.2.2 uebayasi if (error)
233 1.1.2.2 uebayasi aprint_error_dev(sc->sc_dev, "failed to initiate %s xfer\n",
234 1.1.2.2 uebayasi rd_req ? "read" : "write");
235 1.1.2.2 uebayasi return error;
236 1.1.2.2 uebayasi }
237 1.1.2.2 uebayasi
238 1.1.2.2 uebayasi static int
239 1.1.2.2 uebayasi pca9564_read_byte(void *cookie, uint8_t *bytep, int flags)
240 1.1.2.2 uebayasi {
241 1.1.2.2 uebayasi struct pca9564_softc *sc = cookie;
242 1.1.2.2 uebayasi int send_stop = (flags & I2C_F_STOP) != 0;
243 1.1.2.2 uebayasi int error;
244 1.1.2.2 uebayasi
245 1.1.2.2 uebayasi error = pca9564_ack(sc, !send_stop, flags);
246 1.1.2.2 uebayasi if (error) {
247 1.1.2.2 uebayasi aprint_error_dev(sc->sc_dev, "failed to ack\n");
248 1.1.2.2 uebayasi return error;
249 1.1.2.2 uebayasi }
250 1.1.2.2 uebayasi
251 1.1.2.2 uebayasi DPRINTF(("%s: status=%#x\n", __func__, CSR_READ(sc, PCA9564_I2CSTA)));
252 1.1.2.2 uebayasi *bytep = CSR_READ(sc, PCA9564_I2CDAT);
253 1.1.2.2 uebayasi DPRINTF(("%s: status=%#x, byte=%#x\n", __func__,
254 1.1.2.2 uebayasi CSR_READ(sc, PCA9564_I2CSTA), *bytep));
255 1.1.2.2 uebayasi
256 1.1.2.2 uebayasi if (send_stop)
257 1.1.2.2 uebayasi pca9564_send_stop(sc, flags);
258 1.1.2.2 uebayasi
259 1.1.2.2 uebayasi return 0;
260 1.1.2.2 uebayasi }
261 1.1.2.2 uebayasi
262 1.1.2.2 uebayasi static int
263 1.1.2.2 uebayasi pca9564_write_byte(void *cookie, uint8_t byte, int flags)
264 1.1.2.2 uebayasi {
265 1.1.2.2 uebayasi struct pca9564_softc *sc = cookie;
266 1.1.2.2 uebayasi int send_stop = (flags & I2C_F_STOP) != 0;
267 1.1.2.2 uebayasi int error;
268 1.1.2.2 uebayasi uint8_t control;
269 1.1.2.2 uebayasi
270 1.1.2.2 uebayasi DPRINTF(("%s: status=%#x, byte=%#x\n", __func__,
271 1.1.2.2 uebayasi CSR_READ(sc, PCA9564_I2CSTA), byte));
272 1.1.2.2 uebayasi control = CSR_READ(sc, PCA9564_I2CCON);
273 1.1.2.2 uebayasi
274 1.1.2.2 uebayasi CSR_WRITE(sc, PCA9564_I2CDAT, byte);
275 1.1.2.2 uebayasi
276 1.1.2.2 uebayasi control &= ~(I2CCON_STO|I2CCON_STA|I2CCON_SI);
277 1.1.2.2 uebayasi CSR_WRITE(sc, PCA9564_I2CCON, control);
278 1.1.2.2 uebayasi DPRINTF(("%s: status=%#x\n", __func__, CSR_READ(sc, PCA9564_I2CSTA)));
279 1.1.2.2 uebayasi
280 1.1.2.2 uebayasi error = pca9564_wait(sc, flags);
281 1.1.2.2 uebayasi if (error)
282 1.1.2.2 uebayasi aprint_error_dev(sc->sc_dev, "write byte failed\n");
283 1.1.2.2 uebayasi
284 1.1.2.2 uebayasi if (send_stop)
285 1.1.2.2 uebayasi pca9564_send_stop(sc, flags);
286 1.1.2.2 uebayasi
287 1.1.2.2 uebayasi return error;
288 1.1.2.2 uebayasi }
289 1.1.2.2 uebayasi
290 1.1.2.2 uebayasi static int
291 1.1.2.2 uebayasi pca9564_ack(void *cookie, bool ack, int flags)
292 1.1.2.2 uebayasi {
293 1.1.2.2 uebayasi struct pca9564_softc *sc = cookie;
294 1.1.2.2 uebayasi uint8_t control;
295 1.1.2.2 uebayasi
296 1.1.2.2 uebayasi DPRINTF(("%s: status=%#x\n", __func__, CSR_READ(sc, PCA9564_I2CSTA)));
297 1.1.2.2 uebayasi control = CSR_READ(sc, PCA9564_I2CCON);
298 1.1.2.2 uebayasi control &= ~(I2CCON_STO|I2CCON_STA|I2CCON_SI|I2CCON_AA);
299 1.1.2.2 uebayasi if (ack)
300 1.1.2.2 uebayasi control |= I2CCON_AA;
301 1.1.2.2 uebayasi CSR_WRITE(sc, PCA9564_I2CCON, control);
302 1.1.2.2 uebayasi DPRINTF(("%s: status=%#x\n", __func__, CSR_READ(sc, PCA9564_I2CSTA)));
303 1.1.2.2 uebayasi
304 1.1.2.2 uebayasi return pca9564_wait(sc, flags);
305 1.1.2.2 uebayasi }
306