pca9554.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* $OpenBSD: pca9554.c,v 1.17 2008/09/10 16:13:43 reyk Exp $ */
  2. /*
  3. * Copyright (c) 2005 Theo de Raadt
  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. #include <sys/param.h>
  18. #include <sys/systm.h>
  19. #include <sys/device.h>
  20. #include <sys/gpio.h>
  21. #include <sys/sensors.h>
  22. #include <dev/i2c/i2cvar.h>
  23. #include <dev/gpio/gpiovar.h>
  24. /* Philips 9554/6/7 registers */
  25. #define PCA9554_IN 0x00
  26. #define PCA9554_OUT 0x01
  27. #define PCA9554_POLARITY 0x02
  28. #define PCA9554_CONFIG 0x03
  29. /* Philips 9555 registers */
  30. #define PCA9555_IN0 0x00
  31. #define PCA9555_IN1 0x01
  32. #define PCA9555_OUT0 0x02
  33. #define PCA9555_OUT1 0x03
  34. #define PCA9555_POLARITY0 0x04
  35. #define PCA9555_POLARITY1 0x05
  36. #define PCA9555_CONFIG0 0x06
  37. #define PCA9555_CONFIG1 0x07
  38. /* Sensors */
  39. #define PCAGPIO_NPINS 16
  40. #define PCAGPIO_NPORTS 2
  41. #define PCAGPIO_PORT(_pin) ((_pin) > 7 ? 1 : 0)
  42. #define PCAGPIO_BIT(_pin) (1 << ((_pin) % 8))
  43. /* Register mapping index */
  44. enum pcigpio_cmd {
  45. PCAGPIO_IN = 0,
  46. PCAGPIO_OUT,
  47. PCAGPIO_POLARITY,
  48. PCAGPIO_CONFIG,
  49. PCAGPIO_MAX
  50. };
  51. struct pcagpio_softc {
  52. struct device sc_dev;
  53. i2c_tag_t sc_tag;
  54. i2c_addr_t sc_addr;
  55. u_int8_t sc_npins;
  56. u_int8_t sc_control[PCAGPIO_NPORTS];
  57. u_int8_t sc_polarity[PCAGPIO_NPORTS];
  58. u_int8_t sc_regs[PCAGPIO_NPORTS][PCAGPIO_MAX];
  59. struct gpio_chipset_tag sc_gpio_gc;
  60. gpio_pin_t sc_gpio_pins[PCAGPIO_NPINS];
  61. struct ksensor sc_sensor[PCAGPIO_NPINS];
  62. struct ksensordev sc_sensordev;
  63. };
  64. int pcagpio_match(struct device *, void *, void *);
  65. void pcagpio_attach(struct device *, struct device *, void *);
  66. int pcagpio_init(struct pcagpio_softc *, int, u_int8_t *);
  67. void pcagpio_refresh(void *);
  68. int pcagpio_gpio_pin_read(void *, int);
  69. void pcagpio_gpio_pin_write(void *, int, int);
  70. void pcagpio_gpio_pin_ctl(void *, int, int);
  71. struct cfattach pcagpio_ca = {
  72. sizeof(struct pcagpio_softc), pcagpio_match, pcagpio_attach
  73. };
  74. struct cfdriver pcagpio_cd = {
  75. NULL, "pcagpio", DV_DULL
  76. };
  77. int
  78. pcagpio_match(struct device *parent, void *match, void *aux)
  79. {
  80. struct i2c_attach_args *ia = aux;
  81. if (strcmp(ia->ia_name, "PCA9554") == 0 ||
  82. strcmp(ia->ia_name, "PCA9554M") == 0 ||
  83. strcmp(ia->ia_name, "pca9555") == 0 ||
  84. strcmp(ia->ia_name, "pca9556") == 0 ||
  85. strcmp(ia->ia_name, "pca9557") == 0)
  86. return (1);
  87. return (0);
  88. }
  89. void
  90. pcagpio_attach(struct device *parent, struct device *self, void *aux)
  91. {
  92. struct pcagpio_softc *sc = (struct pcagpio_softc *)self;
  93. struct i2c_attach_args *ia = aux;
  94. struct gpiobus_attach_args gba;
  95. int outputs = 0, i, port, bit;
  96. u_int8_t data[PCAGPIO_NPORTS];
  97. sc->sc_tag = ia->ia_tag;
  98. sc->sc_addr = ia->ia_addr;
  99. if (strcmp(ia->ia_name, "pca9555") == 0) {
  100. /* The pca9555 has two 8 bit ports */
  101. sc->sc_regs[0][PCAGPIO_IN] = PCA9555_IN0;
  102. sc->sc_regs[0][PCAGPIO_OUT] = PCA9555_OUT0;
  103. sc->sc_regs[0][PCAGPIO_POLARITY] = PCA9555_POLARITY0;
  104. sc->sc_regs[0][PCAGPIO_CONFIG] = PCA9555_CONFIG0;
  105. sc->sc_regs[1][PCAGPIO_IN] = PCA9555_IN1;
  106. sc->sc_regs[1][PCAGPIO_OUT] = PCA9555_OUT1;
  107. sc->sc_regs[1][PCAGPIO_POLARITY] = PCA9555_POLARITY1;
  108. sc->sc_regs[1][PCAGPIO_CONFIG] = PCA9555_CONFIG1;
  109. sc->sc_npins = 16;
  110. } else {
  111. /* All other supported devices have one 8 bit port */
  112. sc->sc_regs[0][PCAGPIO_IN] = PCA9554_IN;
  113. sc->sc_regs[0][PCAGPIO_OUT] = PCA9554_OUT;
  114. sc->sc_regs[0][PCAGPIO_POLARITY] = PCA9554_POLARITY;
  115. sc->sc_regs[0][PCAGPIO_CONFIG] = PCA9554_CONFIG;
  116. sc->sc_npins = 8;
  117. }
  118. if (pcagpio_init(sc, 0, &data[0]) != 0)
  119. return;
  120. if (sc->sc_npins > 8 && pcagpio_init(sc, 1, &data[1]) != 0)
  121. return;
  122. /* Initialize sensor data. */
  123. strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
  124. sizeof(sc->sc_sensordev.xname));
  125. for (i = 0; i < sc->sc_npins; i++) {
  126. port = PCAGPIO_PORT(i);
  127. bit = PCAGPIO_BIT(i);
  128. sc->sc_sensor[i].type = SENSOR_INDICATOR;
  129. if ((sc->sc_control[port] & bit) == 0) {
  130. strlcpy(sc->sc_sensor[i].desc, "out",
  131. sizeof(sc->sc_sensor[i].desc));
  132. outputs++;
  133. } else
  134. strlcpy(sc->sc_sensor[i].desc, "in",
  135. sizeof(sc->sc_sensor[i].desc));
  136. }
  137. if (sensor_task_register(sc, pcagpio_refresh, 5) == NULL) {
  138. printf(", unable to register update task\n");
  139. return;
  140. }
  141. #if 0
  142. for (i = 0; i < sc->sc_npins; i++)
  143. sensor_attach(&sc->sc_sensordev, &sc->sc_sensor[i]);
  144. sensordev_install(&sc->sc_sensordev);
  145. #endif
  146. printf(":");
  147. if (sc->sc_npins - outputs)
  148. printf(" %d inputs", sc->sc_npins - outputs);
  149. if (outputs)
  150. printf(" %d outputs", outputs);
  151. printf("\n");
  152. for (i = 0; i < sc->sc_npins; i++) {
  153. port = PCAGPIO_PORT(i);
  154. bit = PCAGPIO_BIT(i);
  155. sc->sc_gpio_pins[i].pin_num = i;
  156. sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
  157. if ((sc->sc_control[port] & bit) == 0) {
  158. sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_OUTPUT;
  159. sc->sc_gpio_pins[i].pin_state = data[port] &
  160. bit ? GPIO_PIN_HIGH : GPIO_PIN_LOW;
  161. }
  162. }
  163. /* Create controller tag */
  164. sc->sc_gpio_gc.gp_cookie = sc;
  165. sc->sc_gpio_gc.gp_pin_read = pcagpio_gpio_pin_read;
  166. sc->sc_gpio_gc.gp_pin_write = pcagpio_gpio_pin_write;
  167. sc->sc_gpio_gc.gp_pin_ctl = pcagpio_gpio_pin_ctl;
  168. gba.gba_name = "gpio";
  169. gba.gba_gc = &sc->sc_gpio_gc;
  170. gba.gba_pins = sc->sc_gpio_pins;
  171. gba.gba_npins = sc->sc_npins;
  172. config_found(&sc->sc_dev, &gba, gpiobus_print);
  173. }
  174. int
  175. pcagpio_init(struct pcagpio_softc *sc, int port, u_int8_t *datap)
  176. {
  177. u_int8_t cmd, data;
  178. cmd = sc->sc_regs[port][PCAGPIO_CONFIG];
  179. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  180. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0)) {
  181. printf(": failed to initialize\n");
  182. return (-1);
  183. }
  184. sc->sc_control[port] = data;
  185. cmd = sc->sc_regs[port][PCAGPIO_POLARITY];
  186. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  187. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0)) {
  188. printf(": failed to initialize\n");
  189. return (-1);
  190. }
  191. sc->sc_polarity[port] = data;
  192. cmd = sc->sc_regs[port][PCAGPIO_OUT];
  193. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  194. sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0)) {
  195. printf(": failed to initialize\n");
  196. return (-1);
  197. }
  198. *datap = data;
  199. return (0);
  200. }
  201. void
  202. pcagpio_refresh(void *arg)
  203. {
  204. struct pcagpio_softc *sc = arg;
  205. u_int8_t cmd, bit, in[PCAGPIO_NPORTS], out[PCAGPIO_NPORTS];
  206. int i, port;
  207. iic_acquire_bus(sc->sc_tag, 0);
  208. for (i = 0; i < PCAGPIO_NPORTS; i++) {
  209. cmd = sc->sc_regs[i][PCAGPIO_IN];
  210. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  211. sc->sc_addr, &cmd, sizeof cmd, &in[i], sizeof in[i], 0))
  212. goto invalid;
  213. cmd = sc->sc_regs[i][PCAGPIO_OUT];
  214. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  215. sc->sc_addr, &cmd, sizeof cmd, &out[i], sizeof out[i], 0))
  216. goto invalid;
  217. }
  218. for (i = 0; i < sc->sc_npins; i++) {
  219. port = PCAGPIO_PORT(i);
  220. bit = PCAGPIO_BIT(i);
  221. if ((sc->sc_control[port] & bit))
  222. sc->sc_sensor[i].value = (in[port] & bit) ? 1 : 0;
  223. else
  224. sc->sc_sensor[i].value = (out[port] & bit) ? 1 : 0;
  225. }
  226. invalid:
  227. iic_release_bus(sc->sc_tag, 0);
  228. }
  229. int
  230. pcagpio_gpio_pin_read(void *arg, int pin)
  231. {
  232. struct pcagpio_softc *sc = arg;
  233. u_int8_t cmd, in;
  234. int port, bit;
  235. port = PCAGPIO_PORT(pin);
  236. bit = PCAGPIO_BIT(pin);
  237. cmd = sc->sc_regs[port][PCAGPIO_IN];
  238. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  239. sc->sc_addr, &cmd, sizeof cmd, &in, sizeof in, 0))
  240. return 0;
  241. return ((in ^ sc->sc_polarity[port]) & bit) ? 1 : 0;
  242. }
  243. void
  244. pcagpio_gpio_pin_write(void *arg, int pin, int value)
  245. {
  246. struct pcagpio_softc *sc = arg;
  247. u_int8_t cmd, out, mask;
  248. int port, bit;
  249. port = PCAGPIO_PORT(pin);
  250. bit = PCAGPIO_BIT(pin);
  251. mask = 0xff ^ bit;
  252. cmd = sc->sc_regs[port][PCAGPIO_OUT];
  253. if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
  254. sc->sc_addr, &cmd, sizeof cmd, &out, sizeof out, 0))
  255. return;
  256. out = (out & mask) | (value ? bit : 0);
  257. cmd = sc->sc_regs[port][PCAGPIO_OUT];
  258. if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
  259. sc->sc_addr, &cmd, sizeof cmd, &out, sizeof out, 0))
  260. return;
  261. }
  262. void
  263. pcagpio_gpio_pin_ctl(void *arg, int pin, int flags)
  264. {
  265. #if 0
  266. struct pcagpio_softc *sc = arg;
  267. u_int32_t conf;
  268. pcagpio_gpio_pin_select(sc, pin);
  269. conf = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
  270. GSCGPIO_CONF);
  271. conf &= ~(GSCGPIO_CONF_OUTPUTEN | GSCGPIO_CONF_PUSHPULL |
  272. GSCGPIO_CONF_PULLUP);
  273. if ((flags & GPIO_PIN_TRISTATE) == 0)
  274. conf |= GSCGPIO_CONF_OUTPUTEN;
  275. if (flags & GPIO_PIN_PUSHPULL)
  276. conf |= GSCGPIO_CONF_PUSHPULL;
  277. if (flags & GPIO_PIN_PULLUP)
  278. conf |= GSCGPIO_CONF_PULLUP;
  279. bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
  280. GSCGPIO_CONF, conf);
  281. #endif
  282. }