pcf8584.c revision 1.14 1 1.14 jdc /* $NetBSD: pcf8584.c,v 1.14 2016/01/04 10:00:33 jdc Exp $ */
2 1.6 martin /* $OpenBSD: pcf8584.c,v 1.9 2007/10/20 18:46:21 kettenis Exp $ */
3 1.1 tnn
4 1.6 martin /*
5 1.6 martin * Copyright (c) 2006 David Gwynne <dlg (at) openbsd.org>
6 1.1 tnn *
7 1.6 martin * Permission to use, copy, modify, and distribute this software for any
8 1.6 martin * purpose with or without fee is hereby granted, provided that the above
9 1.6 martin * copyright notice and this permission notice appear in all copies.
10 1.1 tnn *
11 1.6 martin * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.6 martin * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.6 martin * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.6 martin * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.6 martin * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.6 martin * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.6 martin * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 tnn */
19 1.1 tnn
20 1.1 tnn #include <sys/param.h>
21 1.6 martin #include <sys/systm.h>
22 1.1 tnn #include <sys/device.h>
23 1.6 martin #include <sys/malloc.h>
24 1.1 tnn #include <sys/kernel.h>
25 1.6 martin #include <sys/rwlock.h>
26 1.6 martin #include <sys/proc.h>
27 1.9 dyoung #include <sys/bus.h>
28 1.6 martin
29 1.1 tnn #include <dev/i2c/i2cvar.h>
30 1.6 martin
31 1.1 tnn #include <dev/ic/pcf8584var.h>
32 1.13 jdc #include <dev/ic/pcf8584reg.h>
33 1.1 tnn
34 1.14 jdc /* Internal registers */
35 1.13 jdc #define PCF8584_S0 0x00
36 1.13 jdc #define PCF8584_S1 0x01
37 1.13 jdc #define PCF8584_S2 0x02
38 1.13 jdc #define PCF8584_S3 0x03
39 1.6 martin
40 1.6 martin void pcfiic_init(struct pcfiic_softc *);
41 1.6 martin int pcfiic_i2c_acquire_bus(void *, int);
42 1.6 martin void pcfiic_i2c_release_bus(void *, int);
43 1.6 martin int pcfiic_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
44 1.6 martin size_t, void *, size_t, int);
45 1.6 martin
46 1.6 martin int pcfiic_xmit(struct pcfiic_softc *, u_int8_t, const u_int8_t *,
47 1.14 jdc size_t, const u_int8_t *, size_t);
48 1.6 martin int pcfiic_recv(struct pcfiic_softc *, u_int8_t, u_int8_t *,
49 1.6 martin size_t);
50 1.6 martin
51 1.6 martin u_int8_t pcfiic_read(struct pcfiic_softc *, bus_size_t);
52 1.6 martin void pcfiic_write(struct pcfiic_softc *, bus_size_t, u_int8_t);
53 1.6 martin void pcfiic_choose_bus(struct pcfiic_softc *, u_int8_t);
54 1.13 jdc int pcfiic_wait_BBN(struct pcfiic_softc *);
55 1.6 martin int pcfiic_wait_pin(struct pcfiic_softc *, volatile u_int8_t *);
56 1.6 martin
57 1.6 martin void
58 1.6 martin pcfiic_init(struct pcfiic_softc *sc)
59 1.6 martin {
60 1.6 martin /* init S1 */
61 1.13 jdc pcfiic_write(sc, PCF8584_S1, PCF8584_CTRL_PIN);
62 1.6 martin /* own address */
63 1.13 jdc pcfiic_write(sc, PCF8584_S0, sc->sc_addr);
64 1.6 martin
65 1.6 martin /* select clock reg */
66 1.13 jdc pcfiic_write(sc, PCF8584_S1, PCF8584_CTRL_PIN | PCF8584_CTRL_ES1);
67 1.13 jdc pcfiic_write(sc, PCF8584_S0, sc->sc_clock);
68 1.6 martin
69 1.13 jdc pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_IDLE);
70 1.1 tnn
71 1.6 martin delay(200000); /* Multi-Master mode, wait for longest i2c message */
72 1.6 martin }
73 1.6 martin
74 1.6 martin void
75 1.6 martin pcfiic_attach(struct pcfiic_softc *sc, i2c_addr_t addr, u_int8_t clock,
76 1.6 martin int swapregs)
77 1.1 tnn {
78 1.6 martin struct i2cbus_attach_args iba;
79 1.1 tnn
80 1.6 martin if (swapregs) {
81 1.13 jdc sc->sc_regmap[PCF8584_S1] = PCF8584_S0;
82 1.13 jdc sc->sc_regmap[PCF8584_S0] = PCF8584_S1;
83 1.1 tnn } else {
84 1.13 jdc sc->sc_regmap[PCF8584_S0] = PCF8584_S0;
85 1.13 jdc sc->sc_regmap[PCF8584_S1] = PCF8584_S1;
86 1.1 tnn }
87 1.6 martin sc->sc_clock = clock;
88 1.6 martin sc->sc_addr = addr;
89 1.6 martin
90 1.6 martin pcfiic_init(sc);
91 1.6 martin
92 1.6 martin printf("\n");
93 1.6 martin
94 1.6 martin if (sc->sc_master)
95 1.6 martin pcfiic_choose_bus(sc, 0);
96 1.6 martin
97 1.6 martin rw_init(&sc->sc_lock);
98 1.6 martin sc->sc_i2c.ic_cookie = sc;
99 1.6 martin sc->sc_i2c.ic_acquire_bus = pcfiic_i2c_acquire_bus;
100 1.6 martin sc->sc_i2c.ic_release_bus = pcfiic_i2c_release_bus;
101 1.6 martin sc->sc_i2c.ic_exec = pcfiic_i2c_exec;
102 1.6 martin
103 1.6 martin bzero(&iba, sizeof(iba));
104 1.6 martin iba.iba_tag = &sc->sc_i2c;
105 1.6 martin config_found(sc->sc_dev, &iba, iicbus_print);
106 1.6 martin }
107 1.6 martin
108 1.6 martin int
109 1.6 martin pcfiic_intr(void *arg)
110 1.6 martin {
111 1.6 martin return (0);
112 1.1 tnn }
113 1.1 tnn
114 1.6 martin int
115 1.6 martin pcfiic_i2c_acquire_bus(void *arg, int flags)
116 1.6 martin {
117 1.6 martin struct pcfiic_softc *sc = arg;
118 1.6 martin
119 1.6 martin if (cold || sc->sc_poll || (flags & I2C_F_POLL))
120 1.6 martin return (0);
121 1.6 martin
122 1.6 martin rw_enter(&sc->sc_lock, RW_WRITER);
123 1.6 martin return 0;
124 1.6 martin }
125 1.6 martin
126 1.6 martin void
127 1.6 martin pcfiic_i2c_release_bus(void *arg, int flags)
128 1.6 martin {
129 1.6 martin struct pcfiic_softc *sc = arg;
130 1.6 martin
131 1.6 martin if (cold || sc->sc_poll || (flags & I2C_F_POLL))
132 1.6 martin return;
133 1.1 tnn
134 1.6 martin rw_exit(&sc->sc_lock);
135 1.1 tnn }
136 1.6 martin
137 1.6 martin int
138 1.6 martin pcfiic_i2c_exec(void *arg, i2c_op_t op, i2c_addr_t addr,
139 1.6 martin const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
140 1.6 martin {
141 1.6 martin struct pcfiic_softc *sc = arg;
142 1.6 martin int ret = 0;
143 1.6 martin
144 1.6 martin #if 0
145 1.6 martin printf("%s: exec op: %d addr: 0x%x cmdlen: %d len: %d flags 0x%x\n",
146 1.7 macallan device_xname(sc->sc_dev), op, addr, (int)cmdlen, (int)len, flags);
147 1.1 tnn #endif
148 1.1 tnn
149 1.6 martin if (cold || sc->sc_poll)
150 1.6 martin flags |= I2C_F_POLL;
151 1.6 martin
152 1.6 martin if (sc->sc_master)
153 1.6 martin pcfiic_choose_bus(sc, addr >> 7);
154 1.6 martin
155 1.12 jdc /*
156 1.12 jdc * If we are writing, write address, cmdbuf, buf.
157 1.12 jdc * If we are reading, write address, cmdbuf, then read address, buf.
158 1.12 jdc */
159 1.12 jdc if (I2C_OP_WRITE_P(op)) {
160 1.14 jdc ret = pcfiic_xmit(sc, addr & 0x7f, cmdbuf, cmdlen, buf, len);
161 1.12 jdc } else {
162 1.14 jdc if (pcfiic_xmit(sc, addr & 0x7f, cmdbuf, cmdlen, NULL, 0) != 0)
163 1.12 jdc return (1);
164 1.12 jdc ret = pcfiic_recv(sc, addr & 0x7f, buf, len);
165 1.6 martin }
166 1.6 martin return (ret);
167 1.6 martin }
168 1.6 martin
169 1.1 tnn int
170 1.14 jdc pcfiic_xmit(struct pcfiic_softc *sc, u_int8_t addr, const u_int8_t *cmdbuf,
171 1.14 jdc size_t cmdlen, const u_int8_t *buf, size_t len)
172 1.1 tnn {
173 1.6 martin int i, err = 0;
174 1.6 martin volatile u_int8_t r;
175 1.1 tnn
176 1.13 jdc if (pcfiic_wait_BBN(sc) != 0)
177 1.6 martin return (1);
178 1.1 tnn
179 1.13 jdc pcfiic_write(sc, PCF8584_S0, addr << 1);
180 1.13 jdc pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_START);
181 1.1 tnn
182 1.14 jdc for (i = 0; i <= cmdlen + len; i++) {
183 1.6 martin if (pcfiic_wait_pin(sc, &r) != 0) {
184 1.13 jdc pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_STOP);
185 1.6 martin return (1);
186 1.6 martin }
187 1.1 tnn
188 1.13 jdc if (r & PCF8584_STATUS_LRB) {
189 1.6 martin err = 1;
190 1.6 martin break;
191 1.6 martin }
192 1.6 martin
193 1.14 jdc if (i < cmdlen)
194 1.14 jdc pcfiic_write(sc, PCF8584_S0, cmdbuf[i]);
195 1.14 jdc else if (i < cmdlen + len)
196 1.14 jdc pcfiic_write(sc, PCF8584_S0, buf[i - cmdlen]);
197 1.6 martin }
198 1.13 jdc pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_STOP);
199 1.6 martin return (err);
200 1.1 tnn }
201 1.1 tnn
202 1.6 martin int
203 1.6 martin pcfiic_recv(struct pcfiic_softc *sc, u_int8_t addr, u_int8_t *buf, size_t len)
204 1.1 tnn {
205 1.6 martin int i = 0, err = 0;
206 1.6 martin volatile u_int8_t r;
207 1.6 martin
208 1.13 jdc if (pcfiic_wait_BBN(sc) != 0)
209 1.6 martin return (1);
210 1.6 martin
211 1.13 jdc pcfiic_write(sc, PCF8584_S0, (addr << 1) | 0x01);
212 1.13 jdc pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_START);
213 1.1 tnn
214 1.6 martin for (i = 0; i <= len; i++) {
215 1.6 martin if (pcfiic_wait_pin(sc, &r) != 0) {
216 1.13 jdc pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_STOP);
217 1.6 martin return (1);
218 1.1 tnn }
219 1.6 martin
220 1.13 jdc if ((i != len) && (r & PCF8584_STATUS_LRB)) {
221 1.13 jdc pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_STOP);
222 1.6 martin return (1);
223 1.1 tnn }
224 1.6 martin
225 1.6 martin if (i == len - 1) {
226 1.13 jdc pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_NAK);
227 1.6 martin } else if (i == len) {
228 1.13 jdc pcfiic_write(sc, PCF8584_S1, PCF8584_CMD_STOP);
229 1.1 tnn }
230 1.6 martin
231 1.13 jdc r = pcfiic_read(sc, PCF8584_S0);
232 1.6 martin if (i > 0)
233 1.6 martin buf[i - 1] = r;
234 1.1 tnn }
235 1.6 martin return (err);
236 1.6 martin }
237 1.6 martin
238 1.6 martin u_int8_t
239 1.6 martin pcfiic_read(struct pcfiic_softc *sc, bus_size_t r)
240 1.6 martin {
241 1.6 martin bus_space_barrier(sc->sc_iot, sc->sc_ioh, sc->sc_regmap[r], 1,
242 1.6 martin BUS_SPACE_BARRIER_READ);
243 1.6 martin return (bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->sc_regmap[r]));
244 1.6 martin }
245 1.6 martin
246 1.6 martin void
247 1.6 martin pcfiic_write(struct pcfiic_softc *sc, bus_size_t r, u_int8_t v)
248 1.6 martin {
249 1.6 martin bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->sc_regmap[r], v);
250 1.13 jdc (void)bus_space_read_1(sc->sc_iot, sc->sc_ioh, PCF8584_S1);
251 1.6 martin }
252 1.1 tnn
253 1.6 martin void
254 1.6 martin pcfiic_choose_bus(struct pcfiic_softc *sc, u_int8_t bus)
255 1.6 martin {
256 1.6 martin bus_space_write_1(sc->sc_iot, sc->sc_ioh2, 0, bus);
257 1.6 martin bus_space_barrier(sc->sc_iot, sc->sc_ioh2, 0, 1,
258 1.6 martin BUS_SPACE_BARRIER_WRITE);
259 1.1 tnn }
260 1.1 tnn
261 1.6 martin int
262 1.13 jdc pcfiic_wait_BBN(struct pcfiic_softc *sc)
263 1.1 tnn {
264 1.6 martin int i;
265 1.1 tnn
266 1.6 martin for (i = 0; i < 1000; i++) {
267 1.13 jdc if (pcfiic_read(sc, PCF8584_S1) & PCF8584_STATUS_BBN)
268 1.6 martin return (0);
269 1.6 martin delay(1000);
270 1.6 martin }
271 1.6 martin return (1);
272 1.1 tnn }
273 1.1 tnn
274 1.6 martin int
275 1.6 martin pcfiic_wait_pin(struct pcfiic_softc *sc, volatile u_int8_t *r)
276 1.1 tnn {
277 1.6 martin int i;
278 1.1 tnn
279 1.6 martin for (i = 0; i < 1000; i++) {
280 1.13 jdc *r = pcfiic_read(sc, PCF8584_S1);
281 1.13 jdc if ((*r & PCF8584_STATUS_PIN) == 0)
282 1.6 martin return (0);
283 1.6 martin delay(1000);
284 1.6 martin }
285 1.6 martin return (1);
286 1.1 tnn }
287