pca953x.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 and 8 bit I2C gpio expanders (eg pca9537, pca9557, etc)
  20. * TODO: support additional devices with more than 8-bits GPIO
  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. /*
  37. * Modify masked bits in register
  38. */
  39. static int pca953x_reg_write(uint8_t chip, uint addr, uint mask, uint data)
  40. {
  41. uint8_t val;
  42. if (i2c_read(chip, addr, 1, &val, 1))
  43. return -1;
  44. val &= ~mask;
  45. val |= data;
  46. return i2c_write(chip, addr, 1, &val, 1);
  47. }
  48. /*
  49. * Set output value of IO pins in 'mask' to corresponding value in 'data'
  50. * 0 = low, 1 = high
  51. */
  52. int pca953x_set_val(uint8_t chip, uint mask, uint data)
  53. {
  54. return pca953x_reg_write(chip, PCA953X_OUT, mask, data);
  55. }
  56. /*
  57. * Set read polarity of IO pins in 'mask' to corresponding value in 'data'
  58. * 0 = read pin value, 1 = read inverted pin value
  59. */
  60. int pca953x_set_pol(uint8_t chip, uint mask, uint data)
  61. {
  62. return pca953x_reg_write(chip, PCA953X_POL, mask, data);
  63. }
  64. /*
  65. * Set direction of IO pins in 'mask' to corresponding value in 'data'
  66. * 0 = output, 1 = input
  67. */
  68. int pca953x_set_dir(uint8_t chip, uint mask, uint data)
  69. {
  70. return pca953x_reg_write(chip, PCA953X_CONF, mask, data);
  71. }
  72. /*
  73. * Read current logic level of all IO pins
  74. */
  75. int pca953x_get_val(uint8_t chip)
  76. {
  77. uint8_t val;
  78. if (i2c_read(chip, 0, 1, &val, 1))
  79. return -1;
  80. return (int)val;
  81. }
  82. #ifdef CONFIG_CMD_PCA953X
  83. #ifdef CONFIG_CMD_PCA953X_INFO
  84. /*
  85. * Display pca953x information
  86. */
  87. static int pca953x_info(uint8_t chip)
  88. {
  89. int i;
  90. uint8_t data;
  91. printf("pca953x@ 0x%x:\n\n", chip);
  92. printf("gpio pins: 76543210\n");
  93. printf("-------------------\n");
  94. if (i2c_read(chip, PCA953X_CONF, 1, &data, 1))
  95. return -1;
  96. printf("conf: ");
  97. for (i = 7; i >= 0; i--)
  98. printf("%c", data & (1 << i) ? 'i' : 'o');
  99. printf("\n");
  100. if (i2c_read(chip, PCA953X_POL, 1, &data, 1))
  101. return -1;
  102. printf("invert: ");
  103. for (i = 7; i >= 0; i--)
  104. printf("%c", data & (1 << i) ? '1' : '0');
  105. printf("\n");
  106. if (i2c_read(chip, PCA953X_IN, 1, &data, 1))
  107. return -1;
  108. printf("input: ");
  109. for (i = 7; i >= 0; i--)
  110. printf("%c", data & (1 << i) ? '1' : '0');
  111. printf("\n");
  112. if (i2c_read(chip, PCA953X_OUT, 1, &data, 1))
  113. return -1;
  114. printf("output: ");
  115. for (i = 7; i >= 0; i--)
  116. printf("%c", data & (1 << i) ? '1' : '0');
  117. printf("\n");
  118. return 0;
  119. }
  120. #endif /* CONFIG_CMD_PCA953X_INFO */
  121. cmd_tbl_t cmd_pca953x[] = {
  122. U_BOOT_CMD_MKENT(device, 3, 0, (void *)PCA953X_CMD_DEVICE, "", ""),
  123. U_BOOT_CMD_MKENT(output, 4, 0, (void *)PCA953X_CMD_OUTPUT, "", ""),
  124. U_BOOT_CMD_MKENT(input, 3, 0, (void *)PCA953X_CMD_INPUT, "", ""),
  125. U_BOOT_CMD_MKENT(invert, 4, 0, (void *)PCA953X_CMD_INVERT, "", ""),
  126. #ifdef CONFIG_CMD_PCA953X_INFO
  127. U_BOOT_CMD_MKENT(info, 2, 0, (void *)PCA953X_CMD_INFO, "", ""),
  128. #endif
  129. };
  130. int do_pca953x(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  131. {
  132. static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR;
  133. int val;
  134. ulong ul_arg2 = 0;
  135. ulong ul_arg3 = 0;
  136. cmd_tbl_t *c;
  137. c = find_cmd_tbl(argv[1], cmd_pca953x, ARRAY_SIZE(cmd_pca953x));
  138. /* All commands but "device" require 'maxargs' arguments */
  139. if (!c || !((argc == (c->maxargs)) ||
  140. (((int)c->cmd == PCA953X_CMD_DEVICE) &&
  141. (argc == (c->maxargs - 1))))) {
  142. cmd_usage(cmdtp);
  143. return 1;
  144. }
  145. /* arg2 used as chip number or pin number */
  146. if (argc > 2)
  147. ul_arg2 = simple_strtoul(argv[2], NULL, 16);
  148. /* arg3 used as pin or invert value */
  149. if (argc > 3)
  150. ul_arg3 = simple_strtoul(argv[3], NULL, 16) & 0x1;
  151. switch ((int)c->cmd) {
  152. #ifdef CONFIG_CMD_PCA953X_INFO
  153. case PCA953X_CMD_INFO:
  154. return pca953x_info(chip);
  155. #endif
  156. case PCA953X_CMD_DEVICE:
  157. if (argc == 3)
  158. chip = (uint8_t)ul_arg2;
  159. printf("Current device address: 0x%x\n", chip);
  160. return 0;
  161. case PCA953X_CMD_INPUT:
  162. pca953x_set_dir(chip, (1 << ul_arg2),
  163. PCA953X_DIR_IN << ul_arg2);
  164. val = (pca953x_get_val(chip) & (1 << ul_arg2)) != 0;
  165. printf("chip 0x%02x, pin 0x%lx = %d\n", chip, ul_arg2, val);
  166. return val;
  167. case PCA953X_CMD_OUTPUT:
  168. pca953x_set_dir(chip, (1 << ul_arg2),
  169. (PCA953X_DIR_OUT << ul_arg2));
  170. return pca953x_set_val(chip, (1 << ul_arg2),
  171. (ul_arg3 << ul_arg2));
  172. case PCA953X_CMD_INVERT:
  173. return pca953x_set_pol(chip, (1 << ul_arg2),
  174. (ul_arg3 << ul_arg2));
  175. default:
  176. /* We should never get here */
  177. return 1;
  178. }
  179. }
  180. U_BOOT_CMD(
  181. pca953x, 5, 1, do_pca953x,
  182. "pca953x - pca953x gpio access\n",
  183. "device [dev]\n"
  184. " - show or set current device address\n"
  185. #ifdef CONFIG_CMD_PCA953X_INFO
  186. "pca953x info\n"
  187. " - display info for current chip\n"
  188. #endif
  189. "pca953x output pin 0|1\n"
  190. " - set pin as output and drive low or high\n"
  191. "pca953x invert pin 0|1\n"
  192. " - disable/enable polarity inversion for reads\n"
  193. "pca953x intput pin\n"
  194. " - set pin as input and read value\n"
  195. );
  196. #endif /* CONFIG_CMD_PCA953X */