gpioiic.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* $OpenBSD: gpioiic.c,v 1.10 2013/04/19 23:44:34 miod Exp $ */
  2. /*
  3. * Copyright (c) 2006 Alexander Yurchenko <grange@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * I2C bus bit-banging through GPIO pins.
  19. */
  20. #include <sys/param.h>
  21. #include <sys/systm.h>
  22. #include <sys/device.h>
  23. #include <sys/gpio.h>
  24. #include <sys/rwlock.h>
  25. #include <dev/gpio/gpiovar.h>
  26. #include <dev/i2c/i2cvar.h>
  27. #include <dev/i2c/i2c_bitbang.h>
  28. #define GPIOIIC_PIN_SDA 0
  29. #define GPIOIIC_PIN_SCL 1
  30. #define GPIOIIC_NPINS 2
  31. /* flags */
  32. #define GPIOIIC_PIN_REVERSE 0x01
  33. #define GPIOIIC_SDA 0x01
  34. #define GPIOIIC_SCL 0x02
  35. struct gpioiic_softc {
  36. struct device sc_dev;
  37. void * sc_gpio;
  38. struct gpio_pinmap sc_map;
  39. int __map[GPIOIIC_NPINS];
  40. struct i2c_controller sc_i2c_tag;
  41. struct rwlock sc_i2c_lock;
  42. int sc_pin_sda;
  43. int sc_pin_scl;
  44. int sc_sda;
  45. int sc_scl;
  46. };
  47. int gpioiic_match(struct device *, void *, void *);
  48. void gpioiic_attach(struct device *, struct device *, void *);
  49. int gpioiic_detach(struct device *, int);
  50. int gpioiic_i2c_acquire_bus(void *, int);
  51. void gpioiic_i2c_release_bus(void *, int);
  52. int gpioiic_i2c_send_start(void *, int);
  53. int gpioiic_i2c_send_stop(void *, int);
  54. int gpioiic_i2c_initiate_xfer(void *, i2c_addr_t, int);
  55. int gpioiic_i2c_read_byte(void *, u_int8_t *, int);
  56. int gpioiic_i2c_write_byte(void *, u_int8_t, int);
  57. void gpioiic_bb_set_bits(void *, u_int32_t);
  58. void gpioiic_bb_set_dir(void *, u_int32_t);
  59. u_int32_t gpioiic_bb_read_bits(void *);
  60. struct cfattach gpioiic_ca = {
  61. sizeof(struct gpioiic_softc),
  62. gpioiic_match,
  63. gpioiic_attach,
  64. gpioiic_detach
  65. };
  66. struct cfdriver gpioiic_cd = {
  67. NULL, "gpioiic", DV_DULL
  68. };
  69. static const struct i2c_bitbang_ops gpioiic_bbops = {
  70. gpioiic_bb_set_bits,
  71. gpioiic_bb_set_dir,
  72. gpioiic_bb_read_bits,
  73. { GPIOIIC_SDA, GPIOIIC_SCL, GPIOIIC_SDA, 0 }
  74. };
  75. int
  76. gpioiic_match(struct device *parent, void *match, void *aux)
  77. {
  78. struct cfdata *cf = match;
  79. struct gpio_attach_args *ga = aux;
  80. if (ga->ga_offset == -1)
  81. return 0;
  82. return (strcmp(cf->cf_driver->cd_name, "gpioiic") == 0);
  83. }
  84. void
  85. gpioiic_attach(struct device *parent, struct device *self, void *aux)
  86. {
  87. struct gpioiic_softc *sc = (struct gpioiic_softc *)self;
  88. struct gpio_attach_args *ga = aux;
  89. struct i2cbus_attach_args iba;
  90. int caps;
  91. /* Check that we have enough pins */
  92. if (gpio_npins(ga->ga_mask) != GPIOIIC_NPINS) {
  93. printf(": invalid pin mask\n");
  94. return;
  95. }
  96. /* Map pins */
  97. sc->sc_gpio = ga->ga_gpio;
  98. sc->sc_map.pm_map = sc->__map;
  99. if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
  100. &sc->sc_map)) {
  101. printf(": can't map pins\n");
  102. return;
  103. }
  104. if (ga->ga_flags & GPIOIIC_PIN_REVERSE) {
  105. sc->sc_pin_sda = GPIOIIC_PIN_SCL;
  106. sc->sc_pin_scl = GPIOIIC_PIN_SDA;
  107. } else {
  108. sc->sc_pin_sda = GPIOIIC_PIN_SDA;
  109. sc->sc_pin_scl = GPIOIIC_PIN_SCL;
  110. }
  111. /* Configure SDA pin */
  112. caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, sc->sc_pin_sda);
  113. if (!(caps & GPIO_PIN_OUTPUT)) {
  114. printf(": SDA pin is unable to drive output\n");
  115. goto fail;
  116. }
  117. if (!(caps & GPIO_PIN_INPUT)) {
  118. printf(": SDA pin is unable to read input\n");
  119. goto fail;
  120. }
  121. printf(": SDA[%d]", sc->sc_map.pm_map[sc->sc_pin_sda]);
  122. sc->sc_sda = GPIO_PIN_OUTPUT;
  123. if (caps & GPIO_PIN_OPENDRAIN) {
  124. printf(" open-drain");
  125. sc->sc_sda |= GPIO_PIN_OPENDRAIN;
  126. } else if ((caps & GPIO_PIN_PUSHPULL) && (caps & GPIO_PIN_TRISTATE)) {
  127. printf(" push-pull tri-state");
  128. sc->sc_sda |= GPIO_PIN_PUSHPULL;
  129. }
  130. if (caps & GPIO_PIN_PULLUP) {
  131. printf(" pull-up");
  132. sc->sc_sda |= GPIO_PIN_PULLUP;
  133. }
  134. gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, sc->sc_pin_sda, sc->sc_sda);
  135. /* Configure SCL pin */
  136. caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, sc->sc_pin_scl);
  137. if (!(caps & GPIO_PIN_OUTPUT)) {
  138. printf(": SCL pin is unable to drive output\n");
  139. goto fail;
  140. }
  141. printf(", SCL[%d]", sc->sc_map.pm_map[sc->sc_pin_scl]);
  142. sc->sc_scl = GPIO_PIN_OUTPUT;
  143. if (caps & GPIO_PIN_OPENDRAIN) {
  144. printf(" open-drain");
  145. sc->sc_scl |= GPIO_PIN_OPENDRAIN;
  146. if (caps & GPIO_PIN_PULLUP) {
  147. printf(" pull-up");
  148. sc->sc_scl |= GPIO_PIN_PULLUP;
  149. }
  150. } else if (caps & GPIO_PIN_PUSHPULL) {
  151. printf(" push-pull");
  152. sc->sc_scl |= GPIO_PIN_PUSHPULL;
  153. }
  154. gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, sc->sc_pin_scl, sc->sc_scl);
  155. printf("\n");
  156. /* Attach I2C bus */
  157. rw_init(&sc->sc_i2c_lock, "iiclk");
  158. sc->sc_i2c_tag.ic_cookie = sc;
  159. sc->sc_i2c_tag.ic_acquire_bus = gpioiic_i2c_acquire_bus;
  160. sc->sc_i2c_tag.ic_release_bus = gpioiic_i2c_release_bus;
  161. sc->sc_i2c_tag.ic_send_start = gpioiic_i2c_send_start;
  162. sc->sc_i2c_tag.ic_send_stop = gpioiic_i2c_send_stop;
  163. sc->sc_i2c_tag.ic_initiate_xfer = gpioiic_i2c_initiate_xfer;
  164. sc->sc_i2c_tag.ic_read_byte = gpioiic_i2c_read_byte;
  165. sc->sc_i2c_tag.ic_write_byte = gpioiic_i2c_write_byte;
  166. bzero(&iba, sizeof(iba));
  167. iba.iba_name = "iic";
  168. iba.iba_tag = &sc->sc_i2c_tag;
  169. config_found(self, &iba, iicbus_print);
  170. return;
  171. fail:
  172. gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
  173. }
  174. int
  175. gpioiic_detach(struct device *self, int flags)
  176. {
  177. return (0);
  178. }
  179. int
  180. gpioiic_i2c_acquire_bus(void *cookie, int flags)
  181. {
  182. struct gpioiic_softc *sc = cookie;
  183. if (cold || (flags & I2C_F_POLL))
  184. return (0);
  185. return (rw_enter(&sc->sc_i2c_lock, RW_WRITE | RW_INTR));
  186. }
  187. void
  188. gpioiic_i2c_release_bus(void *cookie, int flags)
  189. {
  190. struct gpioiic_softc *sc = cookie;
  191. if (cold || (flags & I2C_F_POLL))
  192. return;
  193. rw_exit(&sc->sc_i2c_lock);
  194. }
  195. int
  196. gpioiic_i2c_send_start(void *cookie, int flags)
  197. {
  198. return (i2c_bitbang_send_start(cookie, flags, &gpioiic_bbops));
  199. }
  200. int
  201. gpioiic_i2c_send_stop(void *cookie, int flags)
  202. {
  203. return (i2c_bitbang_send_stop(cookie, flags, &gpioiic_bbops));
  204. }
  205. int
  206. gpioiic_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
  207. {
  208. return (i2c_bitbang_initiate_xfer(cookie, addr, flags, &gpioiic_bbops));
  209. }
  210. int
  211. gpioiic_i2c_read_byte(void *cookie, u_int8_t *bytep, int flags)
  212. {
  213. return (i2c_bitbang_read_byte(cookie, bytep, flags, &gpioiic_bbops));
  214. }
  215. int
  216. gpioiic_i2c_write_byte(void *cookie, u_int8_t byte, int flags)
  217. {
  218. return (i2c_bitbang_write_byte(cookie, byte, flags, &gpioiic_bbops));
  219. }
  220. void
  221. gpioiic_bb_set_bits(void *cookie, u_int32_t bits)
  222. {
  223. struct gpioiic_softc *sc = cookie;
  224. gpio_pin_write(sc->sc_gpio, &sc->sc_map, sc->sc_pin_sda,
  225. bits & GPIOIIC_SDA ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
  226. gpio_pin_write(sc->sc_gpio, &sc->sc_map, sc->sc_pin_scl,
  227. bits & GPIOIIC_SCL ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
  228. }
  229. void
  230. gpioiic_bb_set_dir(void *cookie, u_int32_t bits)
  231. {
  232. struct gpioiic_softc *sc = cookie;
  233. int sda = sc->sc_sda;
  234. sda &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE);
  235. sda |= (bits & GPIOIIC_SDA ? GPIO_PIN_OUTPUT : GPIO_PIN_INPUT);
  236. if ((sda & GPIO_PIN_PUSHPULL) && !(bits & GPIOIIC_SDA))
  237. sda |= GPIO_PIN_TRISTATE;
  238. if (sc->sc_sda != sda) {
  239. sc->sc_sda = sda;
  240. gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, sc->sc_pin_sda,
  241. sc->sc_sda);
  242. }
  243. }
  244. u_int32_t
  245. gpioiic_bb_read_bits(void *cookie)
  246. {
  247. struct gpioiic_softc *sc = cookie;
  248. u_int32_t bits = 0;
  249. if (gpio_pin_read(sc->sc_gpio, &sc->sc_map, GPIOIIC_PIN_SDA) ==
  250. GPIO_PIN_HIGH)
  251. bits |= GPIOIIC_SDA;
  252. if (gpio_pin_read(sc->sc_gpio, &sc->sc_map, GPIOIIC_PIN_SCL) ==
  253. GPIO_PIN_HIGH)
  254. bits |= GPIOIIC_SCL;
  255. return bits;
  256. }