tca642x.c 8.2 KB

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