pca953x.c 7.1 KB

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