pca953x.c 6.9 KB

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