pca953x_gpio.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 24 gpio pins
  18. * 3. Support Polarity Inversion
  19. */
  20. #include <common.h>
  21. #include <errno.h>
  22. #include <dm.h>
  23. #include <fdtdec.h>
  24. #include <i2c.h>
  25. #include <malloc.h>
  26. #include <asm/gpio.h>
  27. #include <asm/io.h>
  28. #include <dt-bindings/gpio/gpio.h>
  29. #define PCA953X_INPUT 0
  30. #define PCA953X_OUTPUT 1
  31. #define PCA953X_INVERT 2
  32. #define PCA953X_DIRECTION 3
  33. #define PCA_GPIO_MASK 0x00FF
  34. #define PCA_INT 0x0100
  35. #define PCA953X_TYPE 0x1000
  36. #define PCA957X_TYPE 0x2000
  37. #define PCA_TYPE_MASK 0xF000
  38. #define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
  39. enum {
  40. PCA953X_DIRECTION_IN,
  41. PCA953X_DIRECTION_OUT,
  42. };
  43. #define MAX_BANK 5
  44. #define BANK_SZ 8
  45. /*
  46. * struct pca953x_info - Data for pca953x
  47. *
  48. * @dev: udevice structure for the device
  49. * @addr: i2c slave address
  50. * @invert: Polarity inversion or not
  51. * @gpio_count: the number of gpio pins that the device supports
  52. * @chip_type: indicate the chip type,PCA953X or PCA957X
  53. * @bank_count: the number of banks that the device supports
  54. * @reg_output: array to hold the value of output registers
  55. * @reg_direction: array to hold the value of direction registers
  56. */
  57. struct pca953x_info {
  58. struct udevice *dev;
  59. int addr;
  60. int invert;
  61. int gpio_count;
  62. int chip_type;
  63. int bank_count;
  64. u8 reg_output[MAX_BANK];
  65. u8 reg_direction[MAX_BANK];
  66. };
  67. static int pca953x_write_single(struct udevice *dev, int reg, u8 val,
  68. int offset)
  69. {
  70. struct pca953x_info *info = dev_get_platdata(dev);
  71. int bank_shift = fls((info->gpio_count - 1) / BANK_SZ);
  72. int off = offset / BANK_SZ;
  73. int ret = 0;
  74. ret = dm_i2c_write(dev, (reg << bank_shift) + off, &val, 1);
  75. if (ret) {
  76. dev_err(dev, "%s error\n", __func__);
  77. return ret;
  78. }
  79. return 0;
  80. }
  81. static int pca953x_read_single(struct udevice *dev, int reg, u8 *val,
  82. int offset)
  83. {
  84. struct pca953x_info *info = dev_get_platdata(dev);
  85. int bank_shift = fls((info->gpio_count - 1) / BANK_SZ);
  86. int off = offset / BANK_SZ;
  87. int ret;
  88. u8 byte;
  89. ret = dm_i2c_read(dev, (reg << bank_shift) + off, &byte, 1);
  90. if (ret) {
  91. dev_err(dev, "%s error\n", __func__);
  92. return ret;
  93. }
  94. *val = byte;
  95. return 0;
  96. }
  97. static int pca953x_read_regs(struct udevice *dev, int reg, u8 *val)
  98. {
  99. struct pca953x_info *info = dev_get_platdata(dev);
  100. int ret = 0;
  101. if (info->gpio_count <= 8) {
  102. ret = dm_i2c_read(dev, reg, val, 1);
  103. } else if (info->gpio_count <= 16) {
  104. ret = dm_i2c_read(dev, reg << 1, val, info->bank_count);
  105. } else if (info->gpio_count == 40) {
  106. /* Auto increment */
  107. ret = dm_i2c_read(dev, (reg << 3) | 0x80, val,
  108. info->bank_count);
  109. } else {
  110. dev_err(dev, "Unsupported now\n");
  111. return -EINVAL;
  112. }
  113. return ret;
  114. }
  115. static int pca953x_is_output(struct udevice *dev, int offset)
  116. {
  117. struct pca953x_info *info = dev_get_platdata(dev);
  118. int bank = offset / BANK_SZ;
  119. int off = offset % BANK_SZ;
  120. /*0: output; 1: input */
  121. return !(info->reg_direction[bank] & (1 << off));
  122. }
  123. static int pca953x_get_value(struct udevice *dev, uint offset)
  124. {
  125. int ret;
  126. u8 val = 0;
  127. int off = offset % BANK_SZ;
  128. ret = pca953x_read_single(dev, PCA953X_INPUT, &val, offset);
  129. if (ret)
  130. return ret;
  131. return (val >> off) & 0x1;
  132. }
  133. static int pca953x_set_value(struct udevice *dev, uint offset, int value)
  134. {
  135. struct pca953x_info *info = dev_get_platdata(dev);
  136. int bank = offset / BANK_SZ;
  137. int off = offset % BANK_SZ;
  138. u8 val;
  139. int ret;
  140. if (value)
  141. val = info->reg_output[bank] | (1 << off);
  142. else
  143. val = info->reg_output[bank] & ~(1 << off);
  144. ret = pca953x_write_single(dev, PCA953X_OUTPUT, val, offset);
  145. if (ret)
  146. return ret;
  147. info->reg_output[bank] = val;
  148. return 0;
  149. }
  150. static int pca953x_set_direction(struct udevice *dev, uint offset, int dir)
  151. {
  152. struct pca953x_info *info = dev_get_platdata(dev);
  153. int bank = offset / BANK_SZ;
  154. int off = offset % BANK_SZ;
  155. u8 val;
  156. int ret;
  157. if (dir == PCA953X_DIRECTION_IN)
  158. val = info->reg_direction[bank] | (1 << off);
  159. else
  160. val = info->reg_direction[bank] & ~(1 << off);
  161. ret = pca953x_write_single(dev, PCA953X_DIRECTION, val, offset);
  162. if (ret)
  163. return ret;
  164. info->reg_direction[bank] = val;
  165. return 0;
  166. }
  167. static int pca953x_direction_input(struct udevice *dev, uint offset)
  168. {
  169. return pca953x_set_direction(dev, offset, PCA953X_DIRECTION_IN);
  170. }
  171. static int pca953x_direction_output(struct udevice *dev, uint offset, int value)
  172. {
  173. /* Configure output value. */
  174. pca953x_set_value(dev, offset, value);
  175. /* Configure direction as output. */
  176. pca953x_set_direction(dev, offset, PCA953X_DIRECTION_OUT);
  177. return 0;
  178. }
  179. static int pca953x_get_function(struct udevice *dev, uint offset)
  180. {
  181. if (pca953x_is_output(dev, offset))
  182. return GPIOF_OUTPUT;
  183. else
  184. return GPIOF_INPUT;
  185. }
  186. static int pca953x_xlate(struct udevice *dev, struct gpio_desc *desc,
  187. struct ofnode_phandle_args *args)
  188. {
  189. desc->offset = args->args[0];
  190. desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
  191. return 0;
  192. }
  193. static const struct dm_gpio_ops pca953x_ops = {
  194. .direction_input = pca953x_direction_input,
  195. .direction_output = pca953x_direction_output,
  196. .get_value = pca953x_get_value,
  197. .set_value = pca953x_set_value,
  198. .get_function = pca953x_get_function,
  199. .xlate = pca953x_xlate,
  200. };
  201. static int pca953x_probe(struct udevice *dev)
  202. {
  203. struct pca953x_info *info = dev_get_platdata(dev);
  204. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  205. char name[32], label[8], *str;
  206. int addr;
  207. ulong driver_data;
  208. int ret;
  209. int size;
  210. const u8 *tmp;
  211. addr = dev_read_addr(dev);
  212. if (addr == 0)
  213. return -ENODEV;
  214. info->addr = addr;
  215. driver_data = dev_get_driver_data(dev);
  216. info->gpio_count = driver_data & PCA_GPIO_MASK;
  217. if (info->gpio_count > MAX_BANK * BANK_SZ) {
  218. dev_err(dev, "Max support %d pins now\n", MAX_BANK * BANK_SZ);
  219. return -EINVAL;
  220. }
  221. info->chip_type = PCA_CHIP_TYPE(driver_data);
  222. if (info->chip_type != PCA953X_TYPE) {
  223. dev_err(dev, "Only support PCA953X chip type now.\n");
  224. return -EINVAL;
  225. }
  226. info->bank_count = DIV_ROUND_UP(info->gpio_count, BANK_SZ);
  227. ret = pca953x_read_regs(dev, PCA953X_OUTPUT, info->reg_output);
  228. if (ret) {
  229. dev_err(dev, "Error reading output register\n");
  230. return ret;
  231. }
  232. ret = pca953x_read_regs(dev, PCA953X_DIRECTION, info->reg_direction);
  233. if (ret) {
  234. dev_err(dev, "Error reading direction register\n");
  235. return ret;
  236. }
  237. tmp = dev_read_prop(dev, "label", &size);
  238. if (tmp) {
  239. memcpy(label, tmp, sizeof(label) - 1);
  240. label[sizeof(label) - 1] = '\0';
  241. snprintf(name, sizeof(name), "%s@%x_", label, info->addr);
  242. } else {
  243. snprintf(name, sizeof(name), "gpio@%x_", info->addr);
  244. }
  245. str = strdup(name);
  246. if (!str)
  247. return -ENOMEM;
  248. uc_priv->bank_name = str;
  249. uc_priv->gpio_count = info->gpio_count;
  250. dev_dbg(dev, "%s is ready\n", str);
  251. return 0;
  252. }
  253. #define OF_953X(__nrgpio, __int) (ulong)(__nrgpio | PCA953X_TYPE | __int)
  254. #define OF_957X(__nrgpio, __int) (ulong)(__nrgpio | PCA957X_TYPE | __int)
  255. static const struct udevice_id pca953x_ids[] = {
  256. { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
  257. { .compatible = "nxp,pca9534", .data = OF_953X(8, PCA_INT), },
  258. { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
  259. { .compatible = "nxp,pca9536", .data = OF_953X(4, 0), },
  260. { .compatible = "nxp,pca9537", .data = OF_953X(4, PCA_INT), },
  261. { .compatible = "nxp,pca9538", .data = OF_953X(8, PCA_INT), },
  262. { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
  263. { .compatible = "nxp,pca9554", .data = OF_953X(8, PCA_INT), },
  264. { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
  265. { .compatible = "nxp,pca9556", .data = OF_953X(8, 0), },
  266. { .compatible = "nxp,pca9557", .data = OF_953X(8, 0), },
  267. { .compatible = "nxp,pca9574", .data = OF_957X(8, PCA_INT), },
  268. { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
  269. { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
  270. { .compatible = "maxim,max7310", .data = OF_953X(8, 0), },
  271. { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
  272. { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
  273. { .compatible = "maxim,max7315", .data = OF_953X(8, PCA_INT), },
  274. { .compatible = "ti,pca6107", .data = OF_953X(8, PCA_INT), },
  275. { .compatible = "ti,tca6408", .data = OF_953X(8, PCA_INT), },
  276. { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
  277. { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
  278. { .compatible = "onsemi,pca9654", .data = OF_953X(8, PCA_INT), },
  279. { .compatible = "exar,xra1202", .data = OF_953X(8, 0), },
  280. { }
  281. };
  282. U_BOOT_DRIVER(pca953x) = {
  283. .name = "pca953x",
  284. .id = UCLASS_GPIO,
  285. .ops = &pca953x_ops,
  286. .probe = pca953x_probe,
  287. .platdata_auto_alloc_size = sizeof(struct pca953x_info),
  288. .of_match = pca953x_ids,
  289. };