pca953x.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright 2008 Extreme Engineering Solutions, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0
  5. */
  6. /*
  7. * Driver for NXP's 4, 8 and 16 bit I2C gpio expanders (eg pca9537, pca9557,
  8. * pca9539, etc)
  9. */
  10. #include <common.h>
  11. #include <i2c.h>
  12. #include <pca953x.h>
  13. /* Default to an address that hopefully won't corrupt other i2c devices */
  14. #ifndef CONFIG_SYS_I2C_PCA953X_ADDR
  15. #define CONFIG_SYS_I2C_PCA953X_ADDR (~0)
  16. #endif
  17. enum {
  18. PCA953X_CMD_INFO,
  19. PCA953X_CMD_DEVICE,
  20. PCA953X_CMD_OUTPUT,
  21. PCA953X_CMD_INPUT,
  22. PCA953X_CMD_INVERT,
  23. };
  24. #ifdef CONFIG_SYS_I2C_PCA953X_WIDTH
  25. struct pca953x_chip_ngpio {
  26. uint8_t chip;
  27. uint8_t ngpio;
  28. };
  29. static struct pca953x_chip_ngpio pca953x_chip_ngpios[] =
  30. CONFIG_SYS_I2C_PCA953X_WIDTH;
  31. /*
  32. * Determine the number of GPIO pins supported. If we don't know we assume
  33. * 8 pins.
  34. */
  35. static int pca953x_ngpio(uint8_t chip)
  36. {
  37. int i;
  38. for (i = 0; i < ARRAY_SIZE(pca953x_chip_ngpios); i++)
  39. if (pca953x_chip_ngpios[i].chip == chip)
  40. return pca953x_chip_ngpios[i].ngpio;
  41. return 8;
  42. }
  43. #else
  44. static int pca953x_ngpio(uint8_t chip)
  45. {
  46. return 8;
  47. }
  48. #endif
  49. /*
  50. * Modify masked bits in register
  51. */
  52. static int pca953x_reg_write(uint8_t chip, uint addr, uint mask, uint data)
  53. {
  54. uint8_t valb;
  55. uint16_t valw;
  56. if (pca953x_ngpio(chip) <= 8) {
  57. if (i2c_read(chip, addr, 1, &valb, 1))
  58. return -1;
  59. valb &= ~mask;
  60. valb |= data;
  61. return i2c_write(chip, addr, 1, &valb, 1);
  62. } else {
  63. if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2))
  64. return -1;
  65. valw = le16_to_cpu(valw);
  66. valw &= ~mask;
  67. valw |= data;
  68. valw = cpu_to_le16(valw);
  69. return i2c_write(chip, addr << 1, 1, (u8*)&valw, 2);
  70. }
  71. }
  72. static int pca953x_reg_read(uint8_t chip, uint addr, uint *data)
  73. {
  74. uint8_t valb;
  75. uint16_t valw;
  76. if (pca953x_ngpio(chip) <= 8) {
  77. if (i2c_read(chip, addr, 1, &valb, 1))
  78. return -1;
  79. *data = (int)valb;
  80. } else {
  81. if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2))
  82. return -1;
  83. *data = (uint)le16_to_cpu(valw);
  84. }
  85. return 0;
  86. }
  87. /*
  88. * Set output value of IO pins in 'mask' to corresponding value in 'data'
  89. * 0 = low, 1 = high
  90. */
  91. int pca953x_set_val(uint8_t chip, uint mask, uint data)
  92. {
  93. return pca953x_reg_write(chip, PCA953X_OUT, mask, data);
  94. }
  95. /*
  96. * Set read polarity of IO pins in 'mask' to corresponding value in 'data'
  97. * 0 = read pin value, 1 = read inverted pin value
  98. */
  99. int pca953x_set_pol(uint8_t chip, uint mask, uint data)
  100. {
  101. return pca953x_reg_write(chip, PCA953X_POL, mask, data);
  102. }
  103. /*
  104. * Set direction of IO pins in 'mask' to corresponding value in 'data'
  105. * 0 = output, 1 = input
  106. */
  107. int pca953x_set_dir(uint8_t chip, uint mask, uint data)
  108. {
  109. return pca953x_reg_write(chip, PCA953X_CONF, mask, data);
  110. }
  111. /*
  112. * Read current logic level of all IO pins
  113. */
  114. int pca953x_get_val(uint8_t chip)
  115. {
  116. uint val;
  117. if (pca953x_reg_read(chip, PCA953X_IN, &val) < 0)
  118. return -1;
  119. return (int)val;
  120. }
  121. #ifdef CONFIG_CMD_PCA953X
  122. #ifdef CONFIG_CMD_PCA953X_INFO
  123. /*
  124. * Display pca953x information
  125. */
  126. static int pca953x_info(uint8_t chip)
  127. {
  128. int i;
  129. uint data;
  130. int nr_gpio = pca953x_ngpio(chip);
  131. int msb = nr_gpio - 1;
  132. printf("pca953x@ 0x%x (%d pins):\n\n", chip, nr_gpio);
  133. printf("gpio pins: ");
  134. for (i = msb; i >= 0; i--)
  135. printf("%x", i);
  136. printf("\n");
  137. for (i = 11 + nr_gpio; i > 0; i--)
  138. printf("-");
  139. printf("\n");
  140. if (pca953x_reg_read(chip, PCA953X_CONF, &data) < 0)
  141. return -1;
  142. printf("conf: ");
  143. for (i = msb; i >= 0; i--)
  144. printf("%c", data & (1 << i) ? 'i' : 'o');
  145. printf("\n");
  146. if (pca953x_reg_read(chip, PCA953X_POL, &data) < 0)
  147. return -1;
  148. printf("invert: ");
  149. for (i = msb; i >= 0; i--)
  150. printf("%c", data & (1 << i) ? '1' : '0');
  151. printf("\n");
  152. if (pca953x_reg_read(chip, PCA953X_IN, &data) < 0)
  153. return -1;
  154. printf("input: ");
  155. for (i = msb; i >= 0; i--)
  156. printf("%c", data & (1 << i) ? '1' : '0');
  157. printf("\n");
  158. if (pca953x_reg_read(chip, PCA953X_OUT, &data) < 0)
  159. return -1;
  160. printf("output: ");
  161. for (i = msb; i >= 0; i--)
  162. printf("%c", data & (1 << i) ? '1' : '0');
  163. printf("\n");
  164. return 0;
  165. }
  166. #endif /* CONFIG_CMD_PCA953X_INFO */
  167. cmd_tbl_t cmd_pca953x[] = {
  168. U_BOOT_CMD_MKENT(device, 3, 0, (void *)PCA953X_CMD_DEVICE, "", ""),
  169. U_BOOT_CMD_MKENT(output, 4, 0, (void *)PCA953X_CMD_OUTPUT, "", ""),
  170. U_BOOT_CMD_MKENT(input, 3, 0, (void *)PCA953X_CMD_INPUT, "", ""),
  171. U_BOOT_CMD_MKENT(invert, 4, 0, (void *)PCA953X_CMD_INVERT, "", ""),
  172. #ifdef CONFIG_CMD_PCA953X_INFO
  173. U_BOOT_CMD_MKENT(info, 2, 0, (void *)PCA953X_CMD_INFO, "", ""),
  174. #endif
  175. };
  176. int do_pca953x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  177. {
  178. static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR;
  179. int ret = CMD_RET_USAGE, val;
  180. ulong ul_arg2 = 0;
  181. ulong ul_arg3 = 0;
  182. cmd_tbl_t *c;
  183. c = find_cmd_tbl(argv[1], cmd_pca953x, ARRAY_SIZE(cmd_pca953x));
  184. /* All commands but "device" require 'maxargs' arguments */
  185. if (!c || !((argc == (c->maxargs)) ||
  186. (((long)c->cmd == PCA953X_CMD_DEVICE) &&
  187. (argc == (c->maxargs - 1))))) {
  188. return CMD_RET_USAGE;
  189. }
  190. /* arg2 used as chip number or pin number */
  191. if (argc > 2)
  192. ul_arg2 = simple_strtoul(argv[2], NULL, 16);
  193. /* arg3 used as pin or invert value */
  194. if (argc > 3)
  195. ul_arg3 = simple_strtoul(argv[3], NULL, 16) & 0x1;
  196. switch ((long)c->cmd) {
  197. #ifdef CONFIG_CMD_PCA953X_INFO
  198. case PCA953X_CMD_INFO:
  199. ret = pca953x_info(chip);
  200. if (ret)
  201. ret = CMD_RET_FAILURE;
  202. break;
  203. #endif
  204. case PCA953X_CMD_DEVICE:
  205. if (argc == 3)
  206. chip = (uint8_t)ul_arg2;
  207. printf("Current device address: 0x%x\n", chip);
  208. ret = CMD_RET_SUCCESS;
  209. break;
  210. case PCA953X_CMD_INPUT:
  211. ret = pca953x_set_dir(chip, (1 << ul_arg2),
  212. PCA953X_DIR_IN << ul_arg2);
  213. val = (pca953x_get_val(chip) & (1 << ul_arg2)) != 0;
  214. if (ret)
  215. ret = CMD_RET_FAILURE;
  216. else
  217. printf("chip 0x%02x, pin 0x%lx = %d\n", chip, ul_arg2,
  218. val);
  219. break;
  220. case PCA953X_CMD_OUTPUT:
  221. ret = pca953x_set_dir(chip, (1 << ul_arg2),
  222. (PCA953X_DIR_OUT << ul_arg2));
  223. if (!ret)
  224. ret = pca953x_set_val(chip, (1 << ul_arg2),
  225. (ul_arg3 << ul_arg2));
  226. if (ret)
  227. ret = CMD_RET_FAILURE;
  228. break;
  229. case PCA953X_CMD_INVERT:
  230. ret = pca953x_set_pol(chip, (1 << ul_arg2),
  231. (ul_arg3 << ul_arg2));
  232. if (ret)
  233. ret = CMD_RET_FAILURE;
  234. break;
  235. }
  236. if (ret == CMD_RET_FAILURE)
  237. eprintf("Error talking to chip at 0x%x\n", chip);
  238. return ret;
  239. }
  240. U_BOOT_CMD(
  241. pca953x, 5, 1, do_pca953x,
  242. "pca953x gpio access",
  243. "device [dev]\n"
  244. " - show or set current device address\n"
  245. #ifdef CONFIG_CMD_PCA953X_INFO
  246. "pca953x info\n"
  247. " - display info for current chip\n"
  248. #endif
  249. "pca953x output pin 0|1\n"
  250. " - set pin as output and drive low or high\n"
  251. "pca953x invert pin 0|1\n"
  252. " - disable/enable polarity inversion for reads\n"
  253. "pca953x input pin\n"
  254. " - set pin as input and read value"
  255. );
  256. #endif /* CONFIG_CMD_PCA953X */