gpioiic.c revision 1.6.14.1 1 1.6.14.1 skrll /* $NetBSD: gpioiic.c,v 1.6.14.1 2015/09/22 12:05:57 skrll Exp $ */
2 1.1 mbalmer /* $OpenBSD: gpioiic.c,v 1.8 2008/11/24 12:12:12 mbalmer Exp $ */
3 1.1 mbalmer
4 1.1 mbalmer /*
5 1.1 mbalmer * Copyright (c) 2006 Alexander Yurchenko <grange (at) openbsd.org>
6 1.1 mbalmer *
7 1.1 mbalmer * Permission to use, copy, modify, and distribute this software for any
8 1.1 mbalmer * purpose with or without fee is hereby granted, provided that the above
9 1.1 mbalmer * copyright notice and this permission notice appear in all copies.
10 1.1 mbalmer *
11 1.1 mbalmer * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 mbalmer * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 mbalmer * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 mbalmer * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 mbalmer * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 mbalmer * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 mbalmer * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 mbalmer */
19 1.1 mbalmer
20 1.1 mbalmer #include <sys/cdefs.h>
21 1.6.14.1 skrll __KERNEL_RCSID(0, "$NetBSD: gpioiic.c,v 1.6.14.1 2015/09/22 12:05:57 skrll Exp $");
22 1.1 mbalmer
23 1.1 mbalmer /*
24 1.1 mbalmer * I2C bus bit-banging through GPIO pins.
25 1.1 mbalmer */
26 1.1 mbalmer
27 1.1 mbalmer #include <sys/param.h>
28 1.1 mbalmer #include <sys/systm.h>
29 1.1 mbalmer #include <sys/device.h>
30 1.1 mbalmer #include <sys/gpio.h>
31 1.1 mbalmer #include <sys/rwlock.h>
32 1.5 mbalmer #include <sys/module.h>
33 1.1 mbalmer
34 1.1 mbalmer #include <dev/gpio/gpiovar.h>
35 1.1 mbalmer
36 1.1 mbalmer #include <dev/i2c/i2cvar.h>
37 1.1 mbalmer #include <dev/i2c/i2c_bitbang.h>
38 1.1 mbalmer
39 1.1 mbalmer #define GPIOIIC_PIN_SDA 0
40 1.1 mbalmer #define GPIOIIC_PIN_SCL 1
41 1.1 mbalmer #define GPIOIIC_NPINS 2
42 1.1 mbalmer
43 1.1 mbalmer #define GPIOIIC_SDA 0x01
44 1.1 mbalmer #define GPIOIIC_SCL 0x02
45 1.1 mbalmer
46 1.5 mbalmer #define GPIOIIC_PIN_REVERSE 0x01
47 1.5 mbalmer
48 1.1 mbalmer struct gpioiic_softc {
49 1.1 mbalmer void * sc_gpio;
50 1.1 mbalmer struct gpio_pinmap sc_map;
51 1.1 mbalmer int _map[GPIOIIC_NPINS];
52 1.1 mbalmer
53 1.1 mbalmer struct i2c_controller sc_i2c_tag;
54 1.1 mbalmer device_t sc_i2c_dev;
55 1.1 mbalmer krwlock_t sc_i2c_lock;
56 1.1 mbalmer
57 1.5 mbalmer int sc_pin_sda;
58 1.5 mbalmer int sc_pin_scl;
59 1.5 mbalmer
60 1.1 mbalmer int sc_sda;
61 1.1 mbalmer int sc_scl;
62 1.1 mbalmer };
63 1.1 mbalmer
64 1.1 mbalmer int gpioiic_match(device_t, cfdata_t, void *);
65 1.1 mbalmer void gpioiic_attach(device_t, device_t, void *);
66 1.1 mbalmer int gpioiic_detach(device_t, int);
67 1.1 mbalmer
68 1.1 mbalmer int gpioiic_i2c_acquire_bus(void *, int);
69 1.1 mbalmer void gpioiic_i2c_release_bus(void *, int);
70 1.1 mbalmer int gpioiic_i2c_send_start(void *, int);
71 1.1 mbalmer int gpioiic_i2c_send_stop(void *, int);
72 1.1 mbalmer int gpioiic_i2c_initiate_xfer(void *, i2c_addr_t, int);
73 1.4 mbalmer int gpioiic_i2c_read_byte(void *, uint8_t *, int);
74 1.4 mbalmer int gpioiic_i2c_write_byte(void *, uint8_t, int);
75 1.1 mbalmer
76 1.4 mbalmer void gpioiic_bb_set_bits(void *, uint32_t);
77 1.4 mbalmer void gpioiic_bb_set_dir(void *, uint32_t);
78 1.4 mbalmer uint32_t gpioiic_bb_read_bits(void *);
79 1.1 mbalmer
80 1.1 mbalmer CFATTACH_DECL_NEW(gpioiic, sizeof(struct gpioiic_softc),
81 1.2 dyoung gpioiic_match, gpioiic_attach, gpioiic_detach, NULL);
82 1.1 mbalmer
83 1.1 mbalmer extern struct cfdriver gpioiic_cd;
84 1.1 mbalmer
85 1.1 mbalmer static const struct i2c_bitbang_ops gpioiic_bbops = {
86 1.1 mbalmer gpioiic_bb_set_bits,
87 1.1 mbalmer gpioiic_bb_set_dir,
88 1.1 mbalmer gpioiic_bb_read_bits,
89 1.1 mbalmer { GPIOIIC_SDA, GPIOIIC_SCL, GPIOIIC_SDA, 0 }
90 1.1 mbalmer };
91 1.1 mbalmer
92 1.1 mbalmer int
93 1.1 mbalmer gpioiic_match(device_t parent, cfdata_t cf, void *aux)
94 1.1 mbalmer {
95 1.1 mbalmer struct gpio_attach_args *ga = aux;
96 1.1 mbalmer
97 1.1 mbalmer if (strcmp(ga->ga_dvname, cf->cf_name))
98 1.1 mbalmer return 0;
99 1.1 mbalmer
100 1.1 mbalmer if (ga->ga_offset == -1)
101 1.1 mbalmer return 0;
102 1.1 mbalmer
103 1.1 mbalmer /* Check that we have enough pins */
104 1.1 mbalmer if (gpio_npins(ga->ga_mask) != GPIOIIC_NPINS) {
105 1.1 mbalmer aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name,
106 1.1 mbalmer ga->ga_mask);
107 1.1 mbalmer return 0;
108 1.1 mbalmer }
109 1.1 mbalmer return 1;
110 1.1 mbalmer }
111 1.1 mbalmer
112 1.1 mbalmer void
113 1.6 chs gpioiic_attach(device_t parent, device_t self, void *aux)
114 1.1 mbalmer {
115 1.1 mbalmer struct gpioiic_softc *sc = device_private(self);
116 1.1 mbalmer struct gpio_attach_args *ga = aux;
117 1.1 mbalmer struct i2cbus_attach_args iba;
118 1.1 mbalmer int caps;
119 1.1 mbalmer
120 1.1 mbalmer /* Map pins */
121 1.1 mbalmer sc->sc_gpio = ga->ga_gpio;
122 1.1 mbalmer sc->sc_map.pm_map = sc->_map;
123 1.5 mbalmer
124 1.5 mbalmer
125 1.1 mbalmer if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
126 1.1 mbalmer &sc->sc_map)) {
127 1.1 mbalmer aprint_error(": can't map pins\n");
128 1.1 mbalmer return;
129 1.1 mbalmer }
130 1.1 mbalmer
131 1.5 mbalmer if (ga->ga_flags & GPIOIIC_PIN_REVERSE) {
132 1.5 mbalmer sc->sc_pin_sda = GPIOIIC_PIN_SCL;
133 1.5 mbalmer sc->sc_pin_scl = GPIOIIC_PIN_SDA;
134 1.5 mbalmer } else {
135 1.5 mbalmer sc->sc_pin_sda = GPIOIIC_PIN_SDA;
136 1.5 mbalmer sc->sc_pin_scl = GPIOIIC_PIN_SCL;
137 1.5 mbalmer }
138 1.5 mbalmer
139 1.1 mbalmer /* Configure SDA pin */
140 1.5 mbalmer caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, sc->sc_pin_sda);
141 1.1 mbalmer if (!(caps & GPIO_PIN_OUTPUT)) {
142 1.1 mbalmer aprint_error(": SDA pin is unable to drive output\n");
143 1.1 mbalmer goto fail;
144 1.1 mbalmer }
145 1.1 mbalmer if (!(caps & GPIO_PIN_INPUT)) {
146 1.1 mbalmer aprint_error(": SDA pin is unable to read input\n");
147 1.1 mbalmer goto fail;
148 1.1 mbalmer }
149 1.5 mbalmer aprint_normal(": SDA[%d]", sc->sc_map.pm_map[sc->sc_pin_sda]);
150 1.1 mbalmer sc->sc_sda = GPIO_PIN_OUTPUT;
151 1.1 mbalmer if (caps & GPIO_PIN_OPENDRAIN) {
152 1.1 mbalmer aprint_normal(" open-drain");
153 1.1 mbalmer sc->sc_sda |= GPIO_PIN_OPENDRAIN;
154 1.1 mbalmer } else if ((caps & GPIO_PIN_PUSHPULL) && (caps & GPIO_PIN_TRISTATE)) {
155 1.1 mbalmer aprint_normal(" push-pull tri-state");
156 1.1 mbalmer sc->sc_sda |= GPIO_PIN_PUSHPULL;
157 1.1 mbalmer }
158 1.1 mbalmer if (caps & GPIO_PIN_PULLUP) {
159 1.1 mbalmer aprint_normal(" pull-up");
160 1.1 mbalmer sc->sc_sda |= GPIO_PIN_PULLUP;
161 1.1 mbalmer }
162 1.5 mbalmer gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, sc->sc_pin_sda, sc->sc_sda);
163 1.1 mbalmer
164 1.1 mbalmer /* Configure SCL pin */
165 1.5 mbalmer caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, sc->sc_pin_scl);
166 1.1 mbalmer if (!(caps & GPIO_PIN_OUTPUT)) {
167 1.1 mbalmer aprint_error(": SCL pin is unable to drive output\n");
168 1.1 mbalmer goto fail;
169 1.1 mbalmer }
170 1.5 mbalmer aprint_normal(", SCL[%d]", sc->sc_map.pm_map[sc->sc_pin_scl]);
171 1.1 mbalmer sc->sc_scl = GPIO_PIN_OUTPUT;
172 1.1 mbalmer if (caps & GPIO_PIN_OPENDRAIN) {
173 1.1 mbalmer aprint_normal(" open-drain");
174 1.1 mbalmer sc->sc_scl |= GPIO_PIN_OPENDRAIN;
175 1.1 mbalmer if (caps & GPIO_PIN_PULLUP) {
176 1.1 mbalmer aprint_normal(" pull-up");
177 1.1 mbalmer sc->sc_scl |= GPIO_PIN_PULLUP;
178 1.1 mbalmer }
179 1.1 mbalmer } else if (caps & GPIO_PIN_PUSHPULL) {
180 1.1 mbalmer aprint_normal(" push-pull");
181 1.1 mbalmer sc->sc_scl |= GPIO_PIN_PUSHPULL;
182 1.1 mbalmer }
183 1.5 mbalmer gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, sc->sc_pin_scl, sc->sc_scl);
184 1.1 mbalmer
185 1.1 mbalmer aprint_normal("\n");
186 1.1 mbalmer
187 1.1 mbalmer /* Attach I2C bus */
188 1.1 mbalmer rw_init(&sc->sc_i2c_lock);
189 1.1 mbalmer sc->sc_i2c_tag.ic_cookie = sc;
190 1.1 mbalmer sc->sc_i2c_tag.ic_acquire_bus = gpioiic_i2c_acquire_bus;
191 1.1 mbalmer sc->sc_i2c_tag.ic_release_bus = gpioiic_i2c_release_bus;
192 1.1 mbalmer sc->sc_i2c_tag.ic_send_start = gpioiic_i2c_send_start;
193 1.1 mbalmer sc->sc_i2c_tag.ic_send_stop = gpioiic_i2c_send_stop;
194 1.1 mbalmer sc->sc_i2c_tag.ic_initiate_xfer = gpioiic_i2c_initiate_xfer;
195 1.1 mbalmer sc->sc_i2c_tag.ic_read_byte = gpioiic_i2c_read_byte;
196 1.1 mbalmer sc->sc_i2c_tag.ic_write_byte = gpioiic_i2c_write_byte;
197 1.1 mbalmer sc->sc_i2c_tag.ic_exec = NULL;
198 1.1 mbalmer
199 1.1 mbalmer memset(&iba, 0, sizeof(iba));
200 1.1 mbalmer iba.iba_type = I2C_TYPE_SMBUS;
201 1.1 mbalmer iba.iba_tag = &sc->sc_i2c_tag;
202 1.1 mbalmer sc->sc_i2c_dev = config_found(self, &iba, iicbus_print);
203 1.1 mbalmer
204 1.1 mbalmer if (!pmf_device_register(self, NULL, NULL))
205 1.1 mbalmer aprint_error("%s: could not establish power handler\n",
206 1.1 mbalmer device_xname(self));
207 1.1 mbalmer return;
208 1.1 mbalmer
209 1.1 mbalmer fail:
210 1.1 mbalmer gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
211 1.1 mbalmer }
212 1.1 mbalmer
213 1.1 mbalmer int
214 1.6 chs gpioiic_detach(device_t self, int flags)
215 1.1 mbalmer {
216 1.1 mbalmer struct gpioiic_softc *sc = device_private(self);
217 1.1 mbalmer int rv = 0;
218 1.1 mbalmer
219 1.1 mbalmer if (sc->sc_i2c_dev != NULL)
220 1.1 mbalmer rv = config_detach(sc->sc_i2c_dev, flags);
221 1.1 mbalmer
222 1.1 mbalmer if (!rv) {
223 1.1 mbalmer gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
224 1.1 mbalmer pmf_device_deregister(self);
225 1.1 mbalmer }
226 1.1 mbalmer return rv;
227 1.1 mbalmer }
228 1.1 mbalmer
229 1.1 mbalmer int
230 1.1 mbalmer gpioiic_i2c_acquire_bus(void *cookie, int flags)
231 1.1 mbalmer {
232 1.1 mbalmer struct gpioiic_softc *sc = cookie;
233 1.1 mbalmer
234 1.1 mbalmer if (flags & I2C_F_POLL)
235 1.6.14.1 skrll return 1;
236 1.1 mbalmer
237 1.1 mbalmer rw_enter(&sc->sc_i2c_lock, RW_WRITER);
238 1.6.14.1 skrll return 0;
239 1.1 mbalmer }
240 1.1 mbalmer
241 1.1 mbalmer void
242 1.1 mbalmer gpioiic_i2c_release_bus(void *cookie, int flags)
243 1.1 mbalmer {
244 1.1 mbalmer struct gpioiic_softc *sc = cookie;
245 1.1 mbalmer
246 1.1 mbalmer if (flags & I2C_F_POLL)
247 1.1 mbalmer return;
248 1.1 mbalmer
249 1.1 mbalmer rw_exit(&sc->sc_i2c_lock);
250 1.1 mbalmer }
251 1.1 mbalmer
252 1.1 mbalmer int
253 1.1 mbalmer gpioiic_i2c_send_start(void *cookie, int flags)
254 1.1 mbalmer {
255 1.1 mbalmer return i2c_bitbang_send_start(cookie, flags, &gpioiic_bbops);
256 1.1 mbalmer }
257 1.1 mbalmer
258 1.1 mbalmer int
259 1.1 mbalmer gpioiic_i2c_send_stop(void *cookie, int flags)
260 1.1 mbalmer {
261 1.1 mbalmer return i2c_bitbang_send_stop(cookie, flags, &gpioiic_bbops);
262 1.1 mbalmer }
263 1.1 mbalmer
264 1.1 mbalmer int
265 1.1 mbalmer gpioiic_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
266 1.1 mbalmer {
267 1.1 mbalmer return i2c_bitbang_initiate_xfer(cookie, addr, flags, &gpioiic_bbops);
268 1.1 mbalmer }
269 1.1 mbalmer
270 1.1 mbalmer int
271 1.4 mbalmer gpioiic_i2c_read_byte(void *cookie, uint8_t *bytep, int flags)
272 1.1 mbalmer {
273 1.1 mbalmer return i2c_bitbang_read_byte(cookie, bytep, flags, &gpioiic_bbops);
274 1.1 mbalmer }
275 1.1 mbalmer
276 1.1 mbalmer int
277 1.4 mbalmer gpioiic_i2c_write_byte(void *cookie, uint8_t byte, int flags)
278 1.1 mbalmer {
279 1.1 mbalmer return i2c_bitbang_write_byte(cookie, byte, flags, &gpioiic_bbops);
280 1.1 mbalmer }
281 1.1 mbalmer
282 1.1 mbalmer void
283 1.4 mbalmer gpioiic_bb_set_bits(void *cookie, uint32_t bits)
284 1.1 mbalmer {
285 1.1 mbalmer struct gpioiic_softc *sc = cookie;
286 1.1 mbalmer
287 1.5 mbalmer gpio_pin_write(sc->sc_gpio, &sc->sc_map, sc->sc_pin_sda,
288 1.1 mbalmer bits & GPIOIIC_SDA ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
289 1.6.14.1 skrll gpio_pin_write(sc->sc_gpio, &sc->sc_map, sc->sc_pin_scl,
290 1.6.14.1 skrll bits & GPIOIIC_SCL ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
291 1.1 mbalmer }
292 1.1 mbalmer
293 1.1 mbalmer void
294 1.4 mbalmer gpioiic_bb_set_dir(void *cookie, uint32_t bits)
295 1.1 mbalmer {
296 1.1 mbalmer struct gpioiic_softc *sc = cookie;
297 1.1 mbalmer int sda = sc->sc_sda;
298 1.1 mbalmer
299 1.1 mbalmer sda &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE);
300 1.1 mbalmer sda |= (bits & GPIOIIC_SDA ? GPIO_PIN_OUTPUT : GPIO_PIN_INPUT);
301 1.1 mbalmer if ((sda & GPIO_PIN_PUSHPULL) && !(bits & GPIOIIC_SDA))
302 1.1 mbalmer sda |= GPIO_PIN_TRISTATE;
303 1.1 mbalmer if (sc->sc_sda != sda) {
304 1.1 mbalmer sc->sc_sda = sda;
305 1.5 mbalmer gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, sc->sc_pin_sda,
306 1.1 mbalmer sc->sc_sda);
307 1.1 mbalmer }
308 1.1 mbalmer }
309 1.1 mbalmer
310 1.4 mbalmer uint32_t
311 1.1 mbalmer gpioiic_bb_read_bits(void *cookie)
312 1.1 mbalmer {
313 1.1 mbalmer struct gpioiic_softc *sc = cookie;
314 1.4 mbalmer uint32_t bits = 0;
315 1.1 mbalmer
316 1.3 mbalmer if (gpio_pin_read(sc->sc_gpio, &sc->sc_map,
317 1.5 mbalmer sc->sc_pin_sda) == GPIO_PIN_HIGH)
318 1.3 mbalmer bits |= GPIOIIC_SDA;
319 1.3 mbalmer if (gpio_pin_read(sc->sc_gpio, &sc->sc_map,
320 1.5 mbalmer sc->sc_pin_scl) == GPIO_PIN_HIGH)
321 1.3 mbalmer bits |= GPIOIIC_SCL;
322 1.3 mbalmer
323 1.3 mbalmer return bits;
324 1.1 mbalmer }
325 1.5 mbalmer
326 1.5 mbalmer MODULE(MODULE_CLASS_DRIVER, gpioiic, "gpio,iic");
327 1.5 mbalmer
328 1.5 mbalmer #ifdef _MODULE
329 1.5 mbalmer #include "ioconf.c"
330 1.5 mbalmer #endif
331 1.5 mbalmer
332 1.5 mbalmer static int
333 1.5 mbalmer gpioiic_modcmd(modcmd_t cmd, void *opaque)
334 1.5 mbalmer {
335 1.5 mbalmer int error;
336 1.5 mbalmer
337 1.5 mbalmer error = 0;
338 1.5 mbalmer switch (cmd) {
339 1.5 mbalmer case MODULE_CMD_INIT:
340 1.5 mbalmer #ifdef _MODULE
341 1.5 mbalmer error = config_init_component(cfdriver_ioconf_gpioiic,
342 1.5 mbalmer cfattach_ioconf_gpioiic, cfdata_ioconf_gpioiic);
343 1.5 mbalmer if (error)
344 1.5 mbalmer aprint_error("%s: unable to init component\n",
345 1.5 mbalmer gpioiic_cd.cd_name);
346 1.5 mbalmer #endif
347 1.5 mbalmer break;
348 1.5 mbalmer case MODULE_CMD_FINI:
349 1.5 mbalmer #ifdef _MODULE
350 1.5 mbalmer config_fini_component(cfdriver_ioconf_gpioiic,
351 1.5 mbalmer cfattach_ioconf_gpioiic, cfdata_ioconf_gpioiic);
352 1.5 mbalmer #endif
353 1.5 mbalmer break;
354 1.5 mbalmer default:
355 1.5 mbalmer error = ENOTTY;
356 1.5 mbalmer }
357 1.5 mbalmer return error;
358 1.5 mbalmer }
359