pca953x.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 <command.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. #if defined(CONFIG_CMD_PCA953X) && !defined(CONFIG_SPL_BUILD)
  122. /*
  123. * Display pca953x information
  124. */
  125. static int pca953x_info(uint8_t chip)
  126. {
  127. int i;
  128. uint data;
  129. int nr_gpio = pca953x_ngpio(chip);
  130. int msb = nr_gpio - 1;
  131. printf("pca953x@ 0x%x (%d pins):\n\n", chip, nr_gpio);
  132. printf("gpio pins: ");
  133. for (i = msb; i >= 0; i--)
  134. printf("%x", i);
  135. printf("\n");
  136. for (i = 11 + nr_gpio; i > 0; i--)
  137. printf("-");
  138. printf("\n");
  139. if (pca953x_reg_read(chip, PCA953X_CONF, &data) < 0)
  140. return -1;
  141. printf("conf: ");
  142. for (i = msb; i >= 0; i--)
  143. printf("%c", data & (1 << i) ? 'i' : 'o');
  144. printf("\n");
  145. if (pca953x_reg_read(chip, PCA953X_POL, &data) < 0)
  146. return -1;
  147. printf("invert: ");
  148. for (i = msb; i >= 0; i--)
  149. printf("%c", data & (1 << i) ? '1' : '0');
  150. printf("\n");
  151. if (pca953x_reg_read(chip, PCA953X_IN, &data) < 0)
  152. return -1;
  153. printf("input: ");
  154. for (i = msb; i >= 0; i--)
  155. printf("%c", data & (1 << i) ? '1' : '0');
  156. printf("\n");
  157. if (pca953x_reg_read(chip, PCA953X_OUT, &data) < 0)
  158. return -1;
  159. printf("output: ");
  160. for (i = msb; i >= 0; i--)
  161. printf("%c", data & (1 << i) ? '1' : '0');
  162. printf("\n");
  163. return 0;
  164. }
  165. static struct cmd_tbl cmd_pca953x[] = {
  166. U_BOOT_CMD_MKENT(device, 3, 0, (void *)PCA953X_CMD_DEVICE, "", ""),
  167. U_BOOT_CMD_MKENT(output, 4, 0, (void *)PCA953X_CMD_OUTPUT, "", ""),
  168. U_BOOT_CMD_MKENT(input, 3, 0, (void *)PCA953X_CMD_INPUT, "", ""),
  169. U_BOOT_CMD_MKENT(invert, 4, 0, (void *)PCA953X_CMD_INVERT, "", ""),
  170. U_BOOT_CMD_MKENT(info, 2, 0, (void *)PCA953X_CMD_INFO, "", ""),
  171. };
  172. static int do_pca953x(struct cmd_tbl *cmdtp, int flag, int argc,
  173. char *const argv[])
  174. {
  175. static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR;
  176. int ret = CMD_RET_USAGE, val;
  177. ulong ul_arg2 = 0;
  178. ulong ul_arg3 = 0;
  179. struct cmd_tbl *c;
  180. c = find_cmd_tbl(argv[1], cmd_pca953x, ARRAY_SIZE(cmd_pca953x));
  181. /* All commands but "device" require 'maxargs' arguments */
  182. if (!c || !((argc == (c->maxargs)) ||
  183. (((long)c->cmd == PCA953X_CMD_DEVICE) &&
  184. (argc == (c->maxargs - 1))))) {
  185. return CMD_RET_USAGE;
  186. }
  187. /* arg2 used as chip number or pin number */
  188. if (argc > 2)
  189. ul_arg2 = simple_strtoul(argv[2], NULL, 16);
  190. /* arg3 used as pin or invert value */
  191. if (argc > 3)
  192. ul_arg3 = simple_strtoul(argv[3], NULL, 16) & 0x1;
  193. switch ((long)c->cmd) {
  194. case PCA953X_CMD_INFO:
  195. ret = pca953x_info(chip);
  196. if (ret)
  197. ret = CMD_RET_FAILURE;
  198. break;
  199. case PCA953X_CMD_DEVICE:
  200. if (argc == 3)
  201. chip = (uint8_t)ul_arg2;
  202. printf("Current device address: 0x%x\n", chip);
  203. ret = CMD_RET_SUCCESS;
  204. break;
  205. case PCA953X_CMD_INPUT:
  206. ret = pca953x_set_dir(chip, (1 << ul_arg2),
  207. PCA953X_DIR_IN << ul_arg2);
  208. val = (pca953x_get_val(chip) & (1 << ul_arg2)) != 0;
  209. if (ret)
  210. ret = CMD_RET_FAILURE;
  211. else
  212. printf("chip 0x%02x, pin 0x%lx = %d\n", chip, ul_arg2,
  213. val);
  214. break;
  215. case PCA953X_CMD_OUTPUT:
  216. ret = pca953x_set_dir(chip, (1 << ul_arg2),
  217. (PCA953X_DIR_OUT << ul_arg2));
  218. if (!ret)
  219. ret = pca953x_set_val(chip, (1 << ul_arg2),
  220. (ul_arg3 << ul_arg2));
  221. if (ret)
  222. ret = CMD_RET_FAILURE;
  223. break;
  224. case PCA953X_CMD_INVERT:
  225. ret = pca953x_set_pol(chip, (1 << ul_arg2),
  226. (ul_arg3 << ul_arg2));
  227. if (ret)
  228. ret = CMD_RET_FAILURE;
  229. break;
  230. }
  231. if (ret == CMD_RET_FAILURE)
  232. eprintf("Error talking to chip at 0x%x\n", chip);
  233. return ret;
  234. }
  235. U_BOOT_CMD(
  236. pca953x, 5, 1, do_pca953x,
  237. "pca953x gpio access",
  238. "device [dev]\n"
  239. " - show or set current device address\n"
  240. "pca953x info\n"
  241. " - display info for current chip\n"
  242. "pca953x output pin 0|1\n"
  243. " - set pin as output and drive low or high\n"
  244. "pca953x invert pin 0|1\n"
  245. " - disable/enable polarity inversion for reads\n"
  246. "pca953x input pin\n"
  247. " - set pin as input and read value"
  248. );
  249. #endif /* CONFIG_CMD_PCA953X */