tca642x.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright 2013 Texas Instruments, Inc.
  3. * Author: Dan Murphy <dmurphy@ti.com>
  4. *
  5. * Derived work from the pca953x.c driver
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <i2c.h>
  24. #include <tca642x.h>
  25. /* tca642x register address definitions */
  26. struct tca642x_bank_info tca642x_regs[] = {
  27. { .input_reg = 0x00,
  28. .output_reg = 0x04,
  29. .polarity_reg = 0x08,
  30. .configuration_reg = 0x0c },
  31. { .input_reg = 0x01,
  32. .output_reg = 0x05,
  33. .polarity_reg = 0x09,
  34. .configuration_reg = 0x0d },
  35. { .input_reg = 0x02,
  36. .output_reg = 0x06,
  37. .polarity_reg = 0x0a,
  38. .configuration_reg = 0x0e },
  39. };
  40. /*
  41. * Modify masked bits in register
  42. */
  43. static int tca642x_reg_write(uchar chip, uint8_t addr,
  44. uint8_t reg_bit, uint8_t data)
  45. {
  46. uint8_t valw;
  47. int org_bus_num;
  48. int ret;
  49. org_bus_num = i2c_get_bus_num();
  50. i2c_set_bus_num(CONFIG_SYS_I2C_TCA642X_BUS_NUM);
  51. if (i2c_read(chip, addr, 1, (uint8_t *)&valw, 1)) {
  52. printf("Could not read before writing\n");
  53. ret = -1;
  54. goto error;
  55. }
  56. valw &= ~reg_bit;
  57. valw |= data;
  58. ret = i2c_write(chip, addr, 1, (u8 *)&valw, 1);
  59. error:
  60. i2c_set_bus_num(org_bus_num);
  61. return ret;
  62. }
  63. static int tca642x_reg_read(uchar chip, uint8_t addr, uint8_t *data)
  64. {
  65. uint8_t valw;
  66. int org_bus_num;
  67. int ret = 0;
  68. org_bus_num = i2c_get_bus_num();
  69. i2c_set_bus_num(CONFIG_SYS_I2C_TCA642X_BUS_NUM);
  70. if (i2c_read(chip, addr, 1, (u8 *)&valw, 1)) {
  71. ret = -1;
  72. goto error;
  73. }
  74. *data = valw;
  75. error:
  76. i2c_set_bus_num(org_bus_num);
  77. return ret;
  78. }
  79. /*
  80. * Set output value of IO pins in 'reg_bit' to corresponding value in 'data'
  81. * 0 = low, 1 = high
  82. */
  83. int tca642x_set_val(uchar chip, uint8_t gpio_bank,
  84. uint8_t reg_bit, uint8_t data)
  85. {
  86. uint8_t out_reg = tca642x_regs[gpio_bank].output_reg;
  87. return tca642x_reg_write(chip, out_reg, reg_bit, data);
  88. }
  89. /*
  90. * Set read polarity of IO pins in 'reg_bit' to corresponding value in 'data'
  91. * 0 = read pin value, 1 = read inverted pin value
  92. */
  93. int tca642x_set_pol(uchar chip, uint8_t gpio_bank,
  94. uint8_t reg_bit, uint8_t data)
  95. {
  96. uint8_t pol_reg = tca642x_regs[gpio_bank].polarity_reg;
  97. return tca642x_reg_write(chip, pol_reg, reg_bit, data);
  98. }
  99. /*
  100. * Set direction of IO pins in 'reg_bit' to corresponding value in 'data'
  101. * 0 = output, 1 = input
  102. */
  103. int tca642x_set_dir(uchar chip, uint8_t gpio_bank,
  104. uint8_t reg_bit, uint8_t data)
  105. {
  106. uint8_t config_reg = tca642x_regs[gpio_bank].configuration_reg;
  107. return tca642x_reg_write(chip, config_reg, reg_bit, data);
  108. }
  109. /*
  110. * Read current logic level of all IO pins
  111. */
  112. int tca642x_get_val(uchar chip, uint8_t gpio_bank)
  113. {
  114. uint8_t val;
  115. uint8_t in_reg = tca642x_regs[gpio_bank].input_reg;
  116. if (tca642x_reg_read(chip, in_reg, &val) < 0)
  117. return -1;
  118. return (int)val;
  119. }
  120. /*
  121. * Set the inital register states for the tca642x gpio expander
  122. */
  123. int tca642x_set_inital_state(uchar chip, struct tca642x_bank_info init_data[])
  124. {
  125. int i, ret;
  126. uint8_t config_reg;
  127. uint8_t polarity_reg;
  128. uint8_t output_reg;
  129. for (i = 0; i < 3; i++) {
  130. config_reg = tca642x_regs[i].configuration_reg;
  131. ret = tca642x_reg_write(chip, config_reg, 0xff,
  132. init_data[i].configuration_reg);
  133. polarity_reg = tca642x_regs[i].polarity_reg;
  134. ret = tca642x_reg_write(chip, polarity_reg, 0xff,
  135. init_data[i].polarity_reg);
  136. output_reg = tca642x_regs[i].output_reg;
  137. ret = tca642x_reg_write(chip, output_reg, 0xff,
  138. init_data[i].output_reg);
  139. }
  140. return ret;
  141. }
  142. #ifdef CONFIG_CMD_TCA642X
  143. /*
  144. * Display tca642x information
  145. */
  146. static int tca642x_info(uchar chip)
  147. {
  148. int i, j;
  149. uint8_t data;
  150. printf("tca642x@ 0x%x (%d pins):\n", chip, 24);
  151. for (i = 0; i < 3; i++) {
  152. printf("Bank %i\n", i);
  153. if (tca642x_reg_read(chip,
  154. tca642x_regs[i].configuration_reg,
  155. &data) < 0)
  156. return -1;
  157. printf("\tConfiguration: ");
  158. for (j = 7; j >= 0; j--)
  159. printf("%c", data & (1 << j) ? 'i' : 'o');
  160. printf("\n");
  161. if (tca642x_reg_read(chip,
  162. tca642x_regs[i].polarity_reg, &data) < 0)
  163. return -1;
  164. printf("\tPolarity: ");
  165. for (j = 7; j >= 0; j--)
  166. printf("%c", data & (1 << j) ? '1' : '0');
  167. printf("\n");
  168. if (tca642x_reg_read(chip,
  169. tca642x_regs[i].input_reg, &data) < 0)
  170. return -1;
  171. printf("\tInput value: ");
  172. for (j = 7; j >= 0; j--)
  173. printf("%c", data & (1 << j) ? '1' : '0');
  174. printf("\n");
  175. if (tca642x_reg_read(chip,
  176. tca642x_regs[i].output_reg, &data) < 0)
  177. return -1;
  178. printf("\tOutput value: ");
  179. for (j = 7; j >= 0; j--)
  180. printf("%c", data & (1 << j) ? '1' : '0');
  181. printf("\n");
  182. }
  183. return 0;
  184. }
  185. cmd_tbl_t cmd_tca642x[] = {
  186. U_BOOT_CMD_MKENT(device, 3, 0, (void *)TCA642X_CMD_DEVICE, "", ""),
  187. U_BOOT_CMD_MKENT(output, 4, 0, (void *)TCA642X_CMD_OUTPUT, "", ""),
  188. U_BOOT_CMD_MKENT(input, 3, 0, (void *)TCA642X_CMD_INPUT, "", ""),
  189. U_BOOT_CMD_MKENT(invert, 4, 0, (void *)TCA642X_CMD_INVERT, "", ""),
  190. U_BOOT_CMD_MKENT(info, 2, 0, (void *)TCA642X_CMD_INFO, "", ""),
  191. };
  192. int do_tca642x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  193. {
  194. static uchar chip = CONFIG_SYS_I2C_TCA642X_ADDR;
  195. int ret = CMD_RET_USAGE, val;
  196. uint8_t gpio_bank = 0;
  197. uint8_t bank_shift;
  198. ulong ul_arg2 = 0;
  199. ulong ul_arg3 = 0;
  200. cmd_tbl_t *c;
  201. c = find_cmd_tbl(argv[1], cmd_tca642x, ARRAY_SIZE(cmd_tca642x));
  202. /* All commands but "device" require 'maxargs' arguments */
  203. if (!c ||
  204. !((argc == (c->maxargs)) ||
  205. (((int)c->cmd == TCA642X_CMD_DEVICE) &&
  206. (argc == (c->maxargs - 1))))) {
  207. return CMD_RET_USAGE;
  208. }
  209. /* arg2 used as chip number or pin number */
  210. if (argc > 2)
  211. ul_arg2 = simple_strtoul(argv[2], NULL, 10);
  212. /* arg3 used as pin or invert value */
  213. if (argc > 3) {
  214. ul_arg3 = simple_strtoul(argv[3], NULL, 10) & 0x1;
  215. if (ul_arg2 <= 7) {
  216. gpio_bank = 0;
  217. } else if ((ul_arg2 >= 10) && (ul_arg2 <= 17)) {
  218. gpio_bank = 1;
  219. } else if ((ul_arg2 >= 20) && (ul_arg2 <= 27)) {
  220. gpio_bank = 2;
  221. } else {
  222. printf("Requested pin is not available\n");
  223. ret = CMD_RET_FAILURE;
  224. goto error;
  225. }
  226. }
  227. switch ((int)c->cmd) {
  228. case TCA642X_CMD_INFO:
  229. ret = tca642x_info(chip);
  230. if (ret)
  231. ret = CMD_RET_FAILURE;
  232. break;
  233. case TCA642X_CMD_DEVICE:
  234. if (argc == 3)
  235. chip = (uint8_t)ul_arg2;
  236. printf("Current device address: 0x%x\n", chip);
  237. ret = CMD_RET_SUCCESS;
  238. break;
  239. case TCA642X_CMD_INPUT:
  240. bank_shift = ul_arg2 - (gpio_bank * 10);
  241. ret = tca642x_set_dir(chip, gpio_bank, (1 << bank_shift),
  242. TCA642X_DIR_IN << bank_shift);
  243. val = (tca642x_get_val(chip, gpio_bank) &
  244. (1 << bank_shift)) != 0;
  245. if (ret)
  246. ret = CMD_RET_FAILURE;
  247. else
  248. printf("chip 0x%02x, pin 0x%lx = %d\n", chip,
  249. ul_arg2, val);
  250. break;
  251. case TCA642X_CMD_OUTPUT:
  252. bank_shift = ul_arg2 - (gpio_bank * 10);
  253. ret = tca642x_set_dir(chip, gpio_bank, (1 << bank_shift),
  254. (TCA642X_DIR_OUT << bank_shift));
  255. if (!ret)
  256. ret = tca642x_set_val(chip,
  257. gpio_bank, (1 << bank_shift),
  258. (ul_arg3 << bank_shift));
  259. if (ret)
  260. ret = CMD_RET_FAILURE;
  261. break;
  262. case TCA642X_CMD_INVERT:
  263. bank_shift = ul_arg2 - (gpio_bank * 10);
  264. ret = tca642x_set_pol(chip, gpio_bank, (1 << bank_shift),
  265. (ul_arg3 << bank_shift));
  266. if (ret)
  267. ret = CMD_RET_FAILURE;
  268. break;
  269. }
  270. error:
  271. if (ret == CMD_RET_FAILURE)
  272. eprintf("Error talking to chip at 0x%x\n", chip);
  273. return ret;
  274. }
  275. U_BOOT_CMD(
  276. tca642x, 5, 1, do_tca642x,
  277. "tca642x gpio access",
  278. "device [dev]\n"
  279. " - show or set current device address\n"
  280. "tca642x info\n"
  281. " - display info for current chip\n"
  282. "tca642x output pin 0|1\n"
  283. " - set pin as output and drive low or high\n"
  284. "tca642x invert pin 0|1\n"
  285. " - disable/enable polarity inversion for reads\n"
  286. "tca642x input pin\n"
  287. " - set pin as input and read value"
  288. );
  289. #endif /* CONFIG_CMD_TCA642X */