tca642x.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 int tca642x_get_bank(int pin)
  187. {
  188. int gpio_bank;
  189. if (pin <= 7) {
  190. gpio_bank = 0;
  191. } else if ((pin >= 10) && (pin <= 17)) {
  192. gpio_bank = 1;
  193. } else if ((pin >= 20) && (pin <= 27)) {
  194. gpio_bank = 2;
  195. } else {
  196. printf("Requested pin is not available\n");
  197. gpio_bank = -1;
  198. }
  199. return gpio_bank;
  200. }
  201. static struct cmd_tbl cmd_tca642x[] = {
  202. U_BOOT_CMD_MKENT(device, 3, 0, (void *)TCA642X_CMD_DEVICE, "", ""),
  203. U_BOOT_CMD_MKENT(output, 4, 0, (void *)TCA642X_CMD_OUTPUT, "", ""),
  204. U_BOOT_CMD_MKENT(input, 3, 0, (void *)TCA642X_CMD_INPUT, "", ""),
  205. U_BOOT_CMD_MKENT(invert, 4, 0, (void *)TCA642X_CMD_INVERT, "", ""),
  206. U_BOOT_CMD_MKENT(info, 2, 0, (void *)TCA642X_CMD_INFO, "", ""),
  207. };
  208. static int do_tca642x(struct cmd_tbl *cmdtp, int flag, int argc,
  209. char *const argv[])
  210. {
  211. static uchar chip = CONFIG_SYS_I2C_TCA642X_ADDR;
  212. int ret = CMD_RET_USAGE, val;
  213. int gpio_bank = 0;
  214. uint8_t bank_shift;
  215. ulong ul_arg2 = 0;
  216. ulong ul_arg3 = 0;
  217. struct cmd_tbl *c;
  218. c = find_cmd_tbl(argv[1], cmd_tca642x, ARRAY_SIZE(cmd_tca642x));
  219. /* All commands but "device" require 'maxargs' arguments */
  220. if (!c ||
  221. !((argc == (c->maxargs)) ||
  222. (((int)c->cmd == TCA642X_CMD_DEVICE) &&
  223. (argc == (c->maxargs - 1))))) {
  224. return CMD_RET_USAGE;
  225. }
  226. /* arg2 used as chip number or pin number */
  227. if (argc > 2)
  228. ul_arg2 = dectoul(argv[2], NULL);
  229. /* arg3 used as pin or invert value */
  230. if (argc > 3)
  231. ul_arg3 = dectoul(argv[3], NULL) & 0x1;
  232. switch ((int)c->cmd) {
  233. case TCA642X_CMD_INFO:
  234. ret = tca642x_info(chip);
  235. if (ret)
  236. ret = CMD_RET_FAILURE;
  237. break;
  238. case TCA642X_CMD_DEVICE:
  239. if (argc == 3)
  240. chip = (uint8_t)ul_arg2;
  241. printf("Current device address: 0x%x\n", chip);
  242. ret = CMD_RET_SUCCESS;
  243. break;
  244. case TCA642X_CMD_INPUT:
  245. gpio_bank = tca642x_get_bank(ul_arg2);
  246. if (gpio_bank < 0) {
  247. ret = CMD_RET_FAILURE;
  248. goto error;
  249. }
  250. bank_shift = ul_arg2 - (gpio_bank * 10);
  251. ret = tca642x_set_dir(chip, gpio_bank, (1 << bank_shift),
  252. TCA642X_DIR_IN << bank_shift);
  253. val = (tca642x_get_val(chip, gpio_bank) &
  254. (1 << bank_shift)) != 0;
  255. if (ret)
  256. ret = CMD_RET_FAILURE;
  257. else
  258. printf("chip 0x%02x, pin 0x%lx = %d\n", chip,
  259. ul_arg2, val);
  260. break;
  261. case TCA642X_CMD_OUTPUT:
  262. gpio_bank = tca642x_get_bank(ul_arg2);
  263. if (gpio_bank < 0) {
  264. ret = CMD_RET_FAILURE;
  265. goto error;
  266. }
  267. bank_shift = ul_arg2 - (gpio_bank * 10);
  268. ret = tca642x_set_dir(chip, gpio_bank, (1 << bank_shift),
  269. (TCA642X_DIR_OUT << bank_shift));
  270. if (!ret)
  271. ret = tca642x_set_val(chip,
  272. gpio_bank, (1 << bank_shift),
  273. (ul_arg3 << bank_shift));
  274. if (ret)
  275. ret = CMD_RET_FAILURE;
  276. break;
  277. case TCA642X_CMD_INVERT:
  278. gpio_bank = tca642x_get_bank(ul_arg2);
  279. if (gpio_bank < 0) {
  280. ret = CMD_RET_FAILURE;
  281. goto error;
  282. }
  283. bank_shift = ul_arg2 - (gpio_bank * 10);
  284. ret = tca642x_set_pol(chip, gpio_bank, (1 << bank_shift),
  285. (ul_arg3 << bank_shift));
  286. if (ret)
  287. ret = CMD_RET_FAILURE;
  288. break;
  289. }
  290. error:
  291. if (ret == CMD_RET_FAILURE)
  292. eprintf("Error talking to chip at 0x%x\n", chip);
  293. return ret;
  294. }
  295. U_BOOT_CMD(
  296. tca642x, 5, 1, do_tca642x,
  297. "tca642x gpio access",
  298. "device [dev]\n"
  299. " - show or set current device address\n"
  300. "tca642x info\n"
  301. " - display info for current chip\n"
  302. "tca642x output pin 0|1\n"
  303. " - set pin as output and drive low or high\n"
  304. "tca642x invert pin 0|1\n"
  305. " - disable/enable polarity inversion for reads\n"
  306. "tca642x input pin\n"
  307. " - set pin as input and read value"
  308. );
  309. #endif /* CONFIG_CMD_TCA642X */