pca953x_gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Take linux kernel driver drivers/gpio/gpio-pca953x.c for reference.
  4. *
  5. * Copyright (C) 2016 Peng Fan <van.freenix@gmail.com>
  6. *
  7. */
  8. /*
  9. * Note:
  10. * The driver's compatible table is borrowed from Linux Kernel,
  11. * but now max supported gpio pins is 24 and only PCA953X_TYPE
  12. * is supported. PCA957X_TYPE is not supported now.
  13. * Also the Polarity Inversion feature is not supported now.
  14. *
  15. * TODO:
  16. * 1. Support PCA957X_TYPE
  17. * 2. Support Polarity Inversion
  18. */
  19. #include <common.h>
  20. #include <errno.h>
  21. #include <dm.h>
  22. #include <fdtdec.h>
  23. #include <i2c.h>
  24. #include <malloc.h>
  25. #include <asm/gpio.h>
  26. #include <asm/io.h>
  27. #include <dm/device_compat.h>
  28. #include <dt-bindings/gpio/gpio.h>
  29. #include <linux/bitops.h>
  30. #define PCA953X_INPUT 0
  31. #define PCA953X_OUTPUT 1
  32. #define PCA953X_INVERT 2
  33. #define PCA953X_DIRECTION 3
  34. #define PCA957X_INPUT 0
  35. #define PCA957X_OUTPUT 5
  36. #define PCA957X_INVERT 1
  37. #define PCA957X_DIRECTION 4
  38. #define PCA_GPIO_MASK 0x00FF
  39. #define PCA_INT 0x0100
  40. #define PCA_PCAL BIT(9)
  41. #define PCA_LATCH_INT (PCA_PCAL | PCA_INT)
  42. #define PCA953X_TYPE 0x1000
  43. #define PCA957X_TYPE 0x2000
  44. #define PCA_TYPE_MASK 0xF000
  45. #define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
  46. enum {
  47. PCA953X_DIRECTION_IN,
  48. PCA953X_DIRECTION_OUT,
  49. };
  50. #define MAX_BANK 5
  51. #define BANK_SZ 8
  52. struct pca95xx_reg {
  53. int input;
  54. int output;
  55. int invert;
  56. int direction;
  57. };
  58. static const struct pca95xx_reg pca953x_regs = {
  59. .direction = PCA953X_DIRECTION,
  60. .output = PCA953X_OUTPUT,
  61. .input = PCA953X_INPUT,
  62. .invert = PCA953X_INVERT,
  63. };
  64. static const struct pca95xx_reg pca957x_regs = {
  65. .direction = PCA957X_DIRECTION,
  66. .output = PCA957X_OUTPUT,
  67. .input = PCA957X_INPUT,
  68. .invert = PCA957X_INVERT,
  69. };
  70. /*
  71. * struct pca953x_info - Data for pca953x/pca957x
  72. *
  73. * @dev: udevice structure for the device
  74. * @addr: i2c slave address
  75. * @invert: Polarity inversion or not
  76. * @gpio_count: the number of gpio pins that the device supports
  77. * @chip_type: indicate the chip type,PCA953X or PCA957X
  78. * @bank_count: the number of banks that the device supports
  79. * @reg_output: array to hold the value of output registers
  80. * @reg_direction: array to hold the value of direction registers
  81. * @regs: struct to hold the registers addresses
  82. */
  83. struct pca953x_info {
  84. struct udevice *dev;
  85. int addr;
  86. int invert;
  87. int gpio_count;
  88. int chip_type;
  89. int bank_count;
  90. u8 reg_output[MAX_BANK];
  91. u8 reg_direction[MAX_BANK];
  92. const struct pca95xx_reg *regs;
  93. };
  94. static int pca953x_write_single(struct udevice *dev, int reg, u8 val,
  95. int offset)
  96. {
  97. struct pca953x_info *info = dev_get_plat(dev);
  98. int bank_shift = fls((info->gpio_count - 1) / BANK_SZ);
  99. int off = offset / BANK_SZ;
  100. int ret = 0;
  101. ret = dm_i2c_write(dev, (reg << bank_shift) + off, &val, 1);
  102. if (ret) {
  103. dev_err(dev, "%s error\n", __func__);
  104. return ret;
  105. }
  106. return 0;
  107. }
  108. static int pca953x_read_single(struct udevice *dev, int reg, u8 *val,
  109. int offset)
  110. {
  111. struct pca953x_info *info = dev_get_plat(dev);
  112. int bank_shift = fls((info->gpio_count - 1) / BANK_SZ);
  113. int off = offset / BANK_SZ;
  114. int ret;
  115. u8 byte;
  116. ret = dm_i2c_read(dev, (reg << bank_shift) + off, &byte, 1);
  117. if (ret) {
  118. dev_err(dev, "%s error\n", __func__);
  119. return ret;
  120. }
  121. *val = byte;
  122. return 0;
  123. }
  124. static int pca953x_read_regs(struct udevice *dev, int reg, u8 *val)
  125. {
  126. struct pca953x_info *info = dev_get_plat(dev);
  127. int ret = 0;
  128. if (info->gpio_count <= 8) {
  129. ret = dm_i2c_read(dev, reg, val, 1);
  130. } else if (info->gpio_count <= 16) {
  131. ret = dm_i2c_read(dev, reg << 1, val, info->bank_count);
  132. } else if (info->gpio_count <= 24) {
  133. /* Auto increment */
  134. ret = dm_i2c_read(dev, (reg << 2) | 0x80, val,
  135. info->bank_count);
  136. } else if (info->gpio_count == 40) {
  137. /* Auto increment */
  138. ret = dm_i2c_read(dev, (reg << 3) | 0x80, val,
  139. info->bank_count);
  140. } else {
  141. dev_err(dev, "Unsupported now\n");
  142. return -EINVAL;
  143. }
  144. return ret;
  145. }
  146. static int pca953x_write_regs(struct udevice *dev, int reg, u8 *val)
  147. {
  148. struct pca953x_info *info = dev_get_plat(dev);
  149. int ret = 0;
  150. if (info->gpio_count <= 8) {
  151. ret = dm_i2c_write(dev, reg, val, 1);
  152. } else if (info->gpio_count <= 16) {
  153. ret = dm_i2c_write(dev, reg << 1, val, info->bank_count);
  154. } else if (info->gpio_count <= 24) {
  155. /* Auto increment */
  156. ret = dm_i2c_write(dev, (reg << 2) | 0x80, val,
  157. info->bank_count);
  158. } else if (info->gpio_count == 40) {
  159. /* Auto increment */
  160. ret = dm_i2c_write(dev, (reg << 3) | 0x80, val, info->bank_count);
  161. } else {
  162. return -EINVAL;
  163. }
  164. return ret;
  165. }
  166. static int pca953x_is_output(struct udevice *dev, int offset)
  167. {
  168. struct pca953x_info *info = dev_get_plat(dev);
  169. int bank = offset / BANK_SZ;
  170. int off = offset % BANK_SZ;
  171. /*0: output; 1: input */
  172. return !(info->reg_direction[bank] & (1 << off));
  173. }
  174. static int pca953x_get_value(struct udevice *dev, uint offset)
  175. {
  176. struct pca953x_info *info = dev_get_plat(dev);
  177. int ret;
  178. u8 val = 0;
  179. int off = offset % BANK_SZ;
  180. ret = pca953x_read_single(dev, info->regs->input, &val, offset);
  181. if (ret)
  182. return ret;
  183. return (val >> off) & 0x1;
  184. }
  185. static int pca953x_set_value(struct udevice *dev, uint offset, int value)
  186. {
  187. struct pca953x_info *info = dev_get_plat(dev);
  188. int bank = offset / BANK_SZ;
  189. int off = offset % BANK_SZ;
  190. u8 val;
  191. int ret;
  192. if (value)
  193. val = info->reg_output[bank] | (1 << off);
  194. else
  195. val = info->reg_output[bank] & ~(1 << off);
  196. ret = pca953x_write_single(dev, info->regs->output, val, offset);
  197. if (ret)
  198. return ret;
  199. info->reg_output[bank] = val;
  200. return 0;
  201. }
  202. static int pca953x_set_direction(struct udevice *dev, uint offset, int dir)
  203. {
  204. struct pca953x_info *info = dev_get_plat(dev);
  205. int bank = offset / BANK_SZ;
  206. int off = offset % BANK_SZ;
  207. u8 val;
  208. int ret;
  209. if (dir == PCA953X_DIRECTION_IN)
  210. val = info->reg_direction[bank] | (1 << off);
  211. else
  212. val = info->reg_direction[bank] & ~(1 << off);
  213. ret = pca953x_write_single(dev, info->regs->direction, val, offset);
  214. if (ret)
  215. return ret;
  216. info->reg_direction[bank] = val;
  217. return 0;
  218. }
  219. static int pca953x_direction_input(struct udevice *dev, uint offset)
  220. {
  221. return pca953x_set_direction(dev, offset, PCA953X_DIRECTION_IN);
  222. }
  223. static int pca953x_direction_output(struct udevice *dev, uint offset, int value)
  224. {
  225. /* Configure output value. */
  226. pca953x_set_value(dev, offset, value);
  227. /* Configure direction as output. */
  228. pca953x_set_direction(dev, offset, PCA953X_DIRECTION_OUT);
  229. return 0;
  230. }
  231. static int pca953x_get_function(struct udevice *dev, uint offset)
  232. {
  233. if (pca953x_is_output(dev, offset))
  234. return GPIOF_OUTPUT;
  235. else
  236. return GPIOF_INPUT;
  237. }
  238. static int pca953x_xlate(struct udevice *dev, struct gpio_desc *desc,
  239. struct ofnode_phandle_args *args)
  240. {
  241. desc->offset = args->args[0];
  242. desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
  243. return 0;
  244. }
  245. static const struct dm_gpio_ops pca953x_ops = {
  246. .direction_input = pca953x_direction_input,
  247. .direction_output = pca953x_direction_output,
  248. .get_value = pca953x_get_value,
  249. .set_value = pca953x_set_value,
  250. .get_function = pca953x_get_function,
  251. .xlate = pca953x_xlate,
  252. };
  253. static int pca953x_probe(struct udevice *dev)
  254. {
  255. struct pca953x_info *info = dev_get_plat(dev);
  256. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  257. char name[32], label[8], *str;
  258. int addr;
  259. ulong driver_data;
  260. int ret;
  261. int size;
  262. const u8 *tmp;
  263. u8 val[MAX_BANK];
  264. addr = dev_read_addr(dev);
  265. if (addr == 0)
  266. return -ENODEV;
  267. info->addr = addr;
  268. driver_data = dev_get_driver_data(dev);
  269. info->gpio_count = driver_data & PCA_GPIO_MASK;
  270. if (info->gpio_count > MAX_BANK * BANK_SZ) {
  271. dev_err(dev, "Max support %d pins now\n", MAX_BANK * BANK_SZ);
  272. return -EINVAL;
  273. }
  274. info->chip_type = PCA_CHIP_TYPE(driver_data);
  275. if (info->chip_type == PCA953X_TYPE)
  276. info->regs = &pca953x_regs;
  277. else
  278. info->regs = &pca957x_regs;
  279. info->bank_count = DIV_ROUND_UP(info->gpio_count, BANK_SZ);
  280. ret = pca953x_read_regs(dev, info->regs->output, info->reg_output);
  281. if (ret) {
  282. dev_err(dev, "Error reading output register\n");
  283. return ret;
  284. }
  285. ret = pca953x_read_regs(dev, PCA953X_DIRECTION, info->reg_direction);
  286. if (ret) {
  287. dev_err(dev, "Error reading direction register\n");
  288. return ret;
  289. }
  290. tmp = dev_read_prop(dev, "label", &size);
  291. if (tmp) {
  292. memcpy(label, tmp, sizeof(label) - 1);
  293. label[sizeof(label) - 1] = '\0';
  294. snprintf(name, sizeof(name), "%s@%x_", label, info->addr);
  295. } else {
  296. snprintf(name, sizeof(name), "gpio@%x_", info->addr);
  297. }
  298. /* Clear the polarity registers to no invert */
  299. memset(val, 0, MAX_BANK);
  300. ret = pca953x_write_regs(dev, info->regs->invert, val);
  301. if (ret < 0) {
  302. dev_err(dev, "Error writing invert register\n");
  303. return ret;
  304. }
  305. str = strdup(name);
  306. if (!str)
  307. return -ENOMEM;
  308. uc_priv->bank_name = str;
  309. uc_priv->gpio_count = info->gpio_count;
  310. dev_dbg(dev, "%s is ready\n", str);
  311. return 0;
  312. }
  313. #define OF_953X(__nrgpio, __int) (ulong)(__nrgpio | PCA953X_TYPE | __int)
  314. #define OF_957X(__nrgpio, __int) (ulong)(__nrgpio | PCA957X_TYPE | __int)
  315. static const struct udevice_id pca953x_ids[] = {
  316. { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
  317. { .compatible = "nxp,pca9534", .data = OF_953X(8, PCA_INT), },
  318. { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
  319. { .compatible = "nxp,pca9536", .data = OF_953X(4, 0), },
  320. { .compatible = "nxp,pca9537", .data = OF_953X(4, PCA_INT), },
  321. { .compatible = "nxp,pca9538", .data = OF_953X(8, PCA_INT), },
  322. { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
  323. { .compatible = "nxp,pca9554", .data = OF_953X(8, PCA_INT), },
  324. { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
  325. { .compatible = "nxp,pca9556", .data = OF_953X(8, 0), },
  326. { .compatible = "nxp,pca9557", .data = OF_953X(8, 0), },
  327. { .compatible = "nxp,pca9574", .data = OF_957X(8, PCA_INT), },
  328. { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
  329. { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
  330. { .compatible = "nxp,pcal6524", .data = OF_953X(24, PCA_LATCH_INT), },
  331. { .compatible = "maxim,max7310", .data = OF_953X(8, 0), },
  332. { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
  333. { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
  334. { .compatible = "maxim,max7315", .data = OF_953X(8, PCA_INT), },
  335. { .compatible = "ti,pca6107", .data = OF_953X(8, PCA_INT), },
  336. { .compatible = "ti,tca6408", .data = OF_953X(8, PCA_INT), },
  337. { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
  338. { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
  339. { .compatible = "ti,tca9539", .data = OF_953X(16, PCA_INT), },
  340. { .compatible = "onsemi,pca9654", .data = OF_953X(8, PCA_INT), },
  341. { .compatible = "exar,xra1202", .data = OF_953X(8, 0), },
  342. { }
  343. };
  344. U_BOOT_DRIVER(pca953x) = {
  345. .name = "pca953x",
  346. .id = UCLASS_GPIO,
  347. .ops = &pca953x_ops,
  348. .probe = pca953x_probe,
  349. .plat_auto = sizeof(struct pca953x_info),
  350. .of_match = pca953x_ids,
  351. };