1 /* $NetBSD: gpioiic.c,v 1.1 2009/08/09 08:18:00 mbalmer Exp $ */
2 /* $OpenBSD: gpioiic.c,v 1.8 2008/11/24 12:12:12 mbalmer Exp $ */
5 * Copyright (c) 2006 Alexander Yurchenko <grange@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
21 __KERNEL_RCSID(0, "$NetBSD: gpioiic.c,v 1.1 2009/08/09 08:18:00 mbalmer Exp $");
24 * I2C bus bit-banging through GPIO pins.
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/device.h>
31 #include <sys/rwlock.h>
33 #include <dev/gpio/gpiovar.h>
35 #include <dev/i2c/i2cvar.h>
36 #include <dev/i2c/i2c_bitbang.h>
38 #define GPIOIIC_PIN_SDA 0
39 #define GPIOIIC_PIN_SCL 1
40 #define GPIOIIC_NPINS 2
42 #define GPIOIIC_SDA 0x01
43 #define GPIOIIC_SCL 0x02
45 struct gpioiic_softc
{
47 struct gpio_pinmap sc_map
;
48 int _map
[GPIOIIC_NPINS
];
50 struct i2c_controller sc_i2c_tag
;
52 krwlock_t sc_i2c_lock
;
58 int gpioiic_match(device_t
, cfdata_t
, void *);
59 void gpioiic_attach(device_t
, device_t
, void *);
60 int gpioiic_detach(device_t
, int);
62 int gpioiic_i2c_acquire_bus(void *, int);
63 void gpioiic_i2c_release_bus(void *, int);
64 int gpioiic_i2c_send_start(void *, int);
65 int gpioiic_i2c_send_stop(void *, int);
66 int gpioiic_i2c_initiate_xfer(void *, i2c_addr_t
, int);
67 int gpioiic_i2c_read_byte(void *, u_int8_t
*, int);
68 int gpioiic_i2c_write_byte(void *, u_int8_t
, int);
70 void gpioiic_bb_set_bits(void *, u_int32_t
);
71 void gpioiic_bb_set_dir(void *, u_int32_t
);
72 u_int32_t
gpioiic_bb_read_bits(void *);
74 CFATTACH_DECL_NEW(gpioiic
, sizeof(struct gpioiic_softc
),
75 gpioiic_match
, gpioiic_attach
, gpioiic_detach
, NULL
);
77 extern struct cfdriver gpioiic_cd
;
79 static const struct i2c_bitbang_ops gpioiic_bbops
= {
83 { GPIOIIC_SDA
, GPIOIIC_SCL
, GPIOIIC_SDA
, 0 }
87 gpioiic_match(device_t parent
, cfdata_t cf
, void *aux
)
89 struct gpio_attach_args
*ga
= aux
;
91 if (strcmp(ga
->ga_dvname
, cf
->cf_name
))
94 if (ga
->ga_offset
== -1)
97 /* Check that we have enough pins */
98 if (gpio_npins(ga
->ga_mask
) != GPIOIIC_NPINS
) {
99 aprint_debug("%s: invalid pin mask 0x%02x\n", cf
->cf_name
,
107 gpioiic_attach(struct device
*parent
, struct device
*self
, void *aux
)
109 struct gpioiic_softc
*sc
= device_private(self
);
110 struct gpio_attach_args
*ga
= aux
;
111 struct i2cbus_attach_args iba
;
115 sc
->sc_gpio
= ga
->ga_gpio
;
116 sc
->sc_map
.pm_map
= sc
->_map
;
117 if (gpio_pin_map(sc
->sc_gpio
, ga
->ga_offset
, ga
->ga_mask
,
119 aprint_error(": can't map pins\n");
123 /* Configure SDA pin */
124 caps
= gpio_pin_caps(sc
->sc_gpio
, &sc
->sc_map
, GPIOIIC_PIN_SDA
);
125 if (!(caps
& GPIO_PIN_OUTPUT
)) {
126 aprint_error(": SDA pin is unable to drive output\n");
129 if (!(caps
& GPIO_PIN_INPUT
)) {
130 aprint_error(": SDA pin is unable to read input\n");
133 aprint_normal(": SDA[%d]", sc
->sc_map
.pm_map
[GPIOIIC_PIN_SDA
]);
134 sc
->sc_sda
= GPIO_PIN_OUTPUT
;
135 if (caps
& GPIO_PIN_OPENDRAIN
) {
136 aprint_normal(" open-drain");
137 sc
->sc_sda
|= GPIO_PIN_OPENDRAIN
;
138 } else if ((caps
& GPIO_PIN_PUSHPULL
) && (caps
& GPIO_PIN_TRISTATE
)) {
139 aprint_normal(" push-pull tri-state");
140 sc
->sc_sda
|= GPIO_PIN_PUSHPULL
;
142 if (caps
& GPIO_PIN_PULLUP
) {
143 aprint_normal(" pull-up");
144 sc
->sc_sda
|= GPIO_PIN_PULLUP
;
146 gpio_pin_ctl(sc
->sc_gpio
, &sc
->sc_map
, GPIOIIC_PIN_SDA
, sc
->sc_sda
);
148 /* Configure SCL pin */
149 caps
= gpio_pin_caps(sc
->sc_gpio
, &sc
->sc_map
, GPIOIIC_PIN_SCL
);
150 if (!(caps
& GPIO_PIN_OUTPUT
)) {
151 aprint_error(": SCL pin is unable to drive output\n");
154 aprint_normal(", SCL[%d]", sc
->sc_map
.pm_map
[GPIOIIC_PIN_SCL
]);
155 sc
->sc_scl
= GPIO_PIN_OUTPUT
;
156 if (caps
& GPIO_PIN_OPENDRAIN
) {
157 aprint_normal(" open-drain");
158 sc
->sc_scl
|= GPIO_PIN_OPENDRAIN
;
159 if (caps
& GPIO_PIN_PULLUP
) {
160 aprint_normal(" pull-up");
161 sc
->sc_scl
|= GPIO_PIN_PULLUP
;
163 } else if (caps
& GPIO_PIN_PUSHPULL
) {
164 aprint_normal(" push-pull");
165 sc
->sc_scl
|= GPIO_PIN_PUSHPULL
;
167 gpio_pin_ctl(sc
->sc_gpio
, &sc
->sc_map
, GPIOIIC_PIN_SCL
, sc
->sc_scl
);
172 rw_init(&sc
->sc_i2c_lock
);
173 sc
->sc_i2c_tag
.ic_cookie
= sc
;
174 sc
->sc_i2c_tag
.ic_acquire_bus
= gpioiic_i2c_acquire_bus
;
175 sc
->sc_i2c_tag
.ic_release_bus
= gpioiic_i2c_release_bus
;
176 sc
->sc_i2c_tag
.ic_send_start
= gpioiic_i2c_send_start
;
177 sc
->sc_i2c_tag
.ic_send_stop
= gpioiic_i2c_send_stop
;
178 sc
->sc_i2c_tag
.ic_initiate_xfer
= gpioiic_i2c_initiate_xfer
;
179 sc
->sc_i2c_tag
.ic_read_byte
= gpioiic_i2c_read_byte
;
180 sc
->sc_i2c_tag
.ic_write_byte
= gpioiic_i2c_write_byte
;
181 sc
->sc_i2c_tag
.ic_exec
= NULL
;
183 memset(&iba
, 0, sizeof(iba
));
184 iba
.iba_type
= I2C_TYPE_SMBUS
;
185 iba
.iba_tag
= &sc
->sc_i2c_tag
;
186 sc
->sc_i2c_dev
= config_found(self
, &iba
, iicbus_print
);
188 if (!pmf_device_register(self
, NULL
, NULL
))
189 aprint_error("%s: could not establish power handler\n",
194 gpio_pin_unmap(sc
->sc_gpio
, &sc
->sc_map
);
198 gpioiic_detach(struct device
*self
, int flags
)
200 struct gpioiic_softc
*sc
= device_private(self
);
203 if (sc
->sc_i2c_dev
!= NULL
)
204 rv
= config_detach(sc
->sc_i2c_dev
, flags
);
207 gpio_pin_unmap(sc
->sc_gpio
, &sc
->sc_map
);
208 pmf_device_deregister(self
);
214 gpioiic_i2c_acquire_bus(void *cookie
, int flags
)
216 struct gpioiic_softc
*sc
= cookie
;
218 if (flags
& I2C_F_POLL
)
221 rw_enter(&sc
->sc_i2c_lock
, RW_WRITER
);
226 gpioiic_i2c_release_bus(void *cookie
, int flags
)
228 struct gpioiic_softc
*sc
= cookie
;
230 if (flags
& I2C_F_POLL
)
233 rw_exit(&sc
->sc_i2c_lock
);
237 gpioiic_i2c_send_start(void *cookie
, int flags
)
239 return i2c_bitbang_send_start(cookie
, flags
, &gpioiic_bbops
);
243 gpioiic_i2c_send_stop(void *cookie
, int flags
)
245 return i2c_bitbang_send_stop(cookie
, flags
, &gpioiic_bbops
);
249 gpioiic_i2c_initiate_xfer(void *cookie
, i2c_addr_t addr
, int flags
)
251 return i2c_bitbang_initiate_xfer(cookie
, addr
, flags
, &gpioiic_bbops
);
255 gpioiic_i2c_read_byte(void *cookie
, u_int8_t
*bytep
, int flags
)
257 return i2c_bitbang_read_byte(cookie
, bytep
, flags
, &gpioiic_bbops
);
261 gpioiic_i2c_write_byte(void *cookie
, u_int8_t byte
, int flags
)
263 return i2c_bitbang_write_byte(cookie
, byte
, flags
, &gpioiic_bbops
);
267 gpioiic_bb_set_bits(void *cookie
, u_int32_t bits
)
269 struct gpioiic_softc
*sc
= cookie
;
271 gpio_pin_write(sc
->sc_gpio
, &sc
->sc_map
, GPIOIIC_PIN_SDA
,
272 bits
& GPIOIIC_SDA
? GPIO_PIN_HIGH
: GPIO_PIN_LOW
);
273 gpio_pin_write(sc
->sc_gpio
, &sc
->sc_map
, GPIOIIC_PIN_SCL
,
274 bits
& GPIOIIC_SCL
? GPIO_PIN_HIGH
: GPIO_PIN_LOW
);
278 gpioiic_bb_set_dir(void *cookie
, u_int32_t bits
)
280 struct gpioiic_softc
*sc
= cookie
;
281 int sda
= sc
->sc_sda
;
283 sda
&= ~(GPIO_PIN_INPUT
| GPIO_PIN_OUTPUT
| GPIO_PIN_TRISTATE
);
284 sda
|= (bits
& GPIOIIC_SDA
? GPIO_PIN_OUTPUT
: GPIO_PIN_INPUT
);
285 if ((sda
& GPIO_PIN_PUSHPULL
) && !(bits
& GPIOIIC_SDA
))
286 sda
|= GPIO_PIN_TRISTATE
;
287 if (sc
->sc_sda
!= sda
) {
289 gpio_pin_ctl(sc
->sc_gpio
, &sc
->sc_map
, GPIOIIC_PIN_SDA
,
295 gpioiic_bb_read_bits(void *cookie
)
297 struct gpioiic_softc
*sc
= cookie
;
299 return gpio_pin_read(sc
->sc_gpio
, &sc
->sc_map
,
300 GPIOIIC_PIN_SDA
) == GPIO_PIN_HIGH
? GPIOIIC_SDA
: 0;