pca953x_gpio.c 8.8 KB

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