pca953x.c 6.4 KB

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