pca953x_gpio.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 24 gpio pins
  19. * 3. Support Polarity 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 5
  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 if (info->gpio_count == 40) {
  108. /* Auto increment */
  109. ret = dm_i2c_read(dev, (reg << 3) | 0x80, val, info->bank_count);
  110. } else {
  111. dev_err(dev, "Unsupported now\n");
  112. return -EINVAL;
  113. }
  114. return ret;
  115. }
  116. static int pca953x_is_output(struct udevice *dev, int offset)
  117. {
  118. struct pca953x_info *info = dev_get_platdata(dev);
  119. int bank = offset / BANK_SZ;
  120. int off = offset % BANK_SZ;
  121. /*0: output; 1: input */
  122. return !(info->reg_direction[bank] & (1 << off));
  123. }
  124. static int pca953x_get_value(struct udevice *dev, unsigned offset)
  125. {
  126. int ret;
  127. u8 val = 0;
  128. ret = pca953x_read_single(dev, PCA953X_INPUT, &val, offset);
  129. if (ret)
  130. return ret;
  131. return (val >> offset) & 0x1;
  132. }
  133. static int pca953x_set_value(struct udevice *dev, unsigned offset,
  134. int value)
  135. {
  136. struct pca953x_info *info = dev_get_platdata(dev);
  137. int bank = offset / BANK_SZ;
  138. int off = offset % BANK_SZ;
  139. u8 val;
  140. int ret;
  141. if (value)
  142. val = info->reg_output[bank] | (1 << off);
  143. else
  144. val = info->reg_output[bank] & ~(1 << off);
  145. ret = pca953x_write_single(dev, PCA953X_OUTPUT, val, offset);
  146. if (ret)
  147. return ret;
  148. info->reg_output[bank] = val;
  149. return 0;
  150. }
  151. static int pca953x_set_direction(struct udevice *dev, unsigned offset, int dir)
  152. {
  153. struct pca953x_info *info = dev_get_platdata(dev);
  154. int bank = offset / BANK_SZ;
  155. int off = offset % BANK_SZ;
  156. u8 val;
  157. int ret;
  158. if (dir == PCA953X_DIRECTION_IN)
  159. val = info->reg_direction[bank] | (1 << off);
  160. else
  161. val = info->reg_direction[bank] & ~(1 << off);
  162. ret = pca953x_write_single(dev, PCA953X_DIRECTION, val, offset);
  163. if (ret)
  164. return ret;
  165. info->reg_direction[bank] = val;
  166. return 0;
  167. }
  168. static int pca953x_direction_input(struct udevice *dev, unsigned offset)
  169. {
  170. return pca953x_set_direction(dev, offset, PCA953X_DIRECTION_IN);
  171. }
  172. static int pca953x_direction_output(struct udevice *dev, unsigned offset,
  173. int value)
  174. {
  175. /* Configure output value. */
  176. pca953x_set_value(dev, offset, value);
  177. /* Configure direction as output. */
  178. pca953x_set_direction(dev, offset, PCA953X_DIRECTION_OUT);
  179. return 0;
  180. }
  181. static int pca953x_get_function(struct udevice *dev, unsigned offset)
  182. {
  183. if (pca953x_is_output(dev, offset))
  184. return GPIOF_OUTPUT;
  185. else
  186. return GPIOF_INPUT;
  187. }
  188. static int pca953x_xlate(struct udevice *dev, struct gpio_desc *desc,
  189. struct fdtdec_phandle_args *args)
  190. {
  191. desc->offset = args->args[0];
  192. desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
  193. return 0;
  194. }
  195. static const struct dm_gpio_ops pca953x_ops = {
  196. .direction_input = pca953x_direction_input,
  197. .direction_output = pca953x_direction_output,
  198. .get_value = pca953x_get_value,
  199. .set_value = pca953x_set_value,
  200. .get_function = pca953x_get_function,
  201. .xlate = pca953x_xlate,
  202. };
  203. static int pca953x_probe(struct udevice *dev)
  204. {
  205. struct pca953x_info *info = dev_get_platdata(dev);
  206. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  207. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  208. char name[32], *str;
  209. int addr;
  210. ulong driver_data;
  211. int ret;
  212. if (!info) {
  213. dev_err(dev, "platdata not ready\n");
  214. return -ENOMEM;
  215. }
  216. if (!chip) {
  217. dev_err(dev, "i2c not ready\n");
  218. return -ENODEV;
  219. }
  220. addr = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "reg", 0);
  221. if (addr == 0)
  222. return -ENODEV;
  223. info->addr = addr;
  224. driver_data = dev_get_driver_data(dev);
  225. info->gpio_count = driver_data & PCA_GPIO_MASK;
  226. if (info->gpio_count > MAX_BANK * BANK_SZ) {
  227. dev_err(dev, "Max support %d pins now\n", MAX_BANK * BANK_SZ);
  228. return -EINVAL;
  229. }
  230. info->chip_type = PCA_CHIP_TYPE(driver_data);
  231. if (info->chip_type != PCA953X_TYPE) {
  232. dev_err(dev, "Only support PCA953X chip type now.\n");
  233. return -EINVAL;
  234. }
  235. info->bank_count = DIV_ROUND_UP(info->gpio_count, BANK_SZ);
  236. ret = pca953x_read_regs(dev, PCA953X_OUTPUT, info->reg_output);
  237. if (ret) {
  238. dev_err(dev, "Error reading output register\n");
  239. return ret;
  240. }
  241. ret = pca953x_read_regs(dev, PCA953X_DIRECTION, info->reg_direction);
  242. if (ret) {
  243. dev_err(dev, "Error reading direction register\n");
  244. return ret;
  245. }
  246. snprintf(name, sizeof(name), "gpio@%x_", info->addr);
  247. str = strdup(name);
  248. if (!str)
  249. return -ENOMEM;
  250. uc_priv->bank_name = str;
  251. uc_priv->gpio_count = info->gpio_count;
  252. dev_dbg(dev, "%s is ready\n", str);
  253. return 0;
  254. }
  255. #define OF_953X(__nrgpio, __int) (ulong)(__nrgpio | PCA953X_TYPE | __int)
  256. #define OF_957X(__nrgpio, __int) (ulong)(__nrgpio | PCA957X_TYPE | __int)
  257. static const struct udevice_id pca953x_ids[] = {
  258. { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
  259. { .compatible = "nxp,pca9534", .data = OF_953X(8, PCA_INT), },
  260. { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
  261. { .compatible = "nxp,pca9536", .data = OF_953X(4, 0), },
  262. { .compatible = "nxp,pca9537", .data = OF_953X(4, PCA_INT), },
  263. { .compatible = "nxp,pca9538", .data = OF_953X(8, PCA_INT), },
  264. { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
  265. { .compatible = "nxp,pca9554", .data = OF_953X(8, PCA_INT), },
  266. { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
  267. { .compatible = "nxp,pca9556", .data = OF_953X(8, 0), },
  268. { .compatible = "nxp,pca9557", .data = OF_953X(8, 0), },
  269. { .compatible = "nxp,pca9574", .data = OF_957X(8, PCA_INT), },
  270. { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
  271. { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
  272. { .compatible = "maxim,max7310", .data = OF_953X(8, 0), },
  273. { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
  274. { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
  275. { .compatible = "maxim,max7315", .data = OF_953X(8, PCA_INT), },
  276. { .compatible = "ti,pca6107", .data = OF_953X(8, PCA_INT), },
  277. { .compatible = "ti,tca6408", .data = OF_953X(8, PCA_INT), },
  278. { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
  279. { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
  280. { .compatible = "onsemi,pca9654", .data = OF_953X(8, PCA_INT), },
  281. { .compatible = "exar,xra1202", .data = OF_953X(8, 0), },
  282. { }
  283. };
  284. U_BOOT_DRIVER(pca953x) = {
  285. .name = "pca953x",
  286. .id = UCLASS_GPIO,
  287. .ops = &pca953x_ops,
  288. .probe = pca953x_probe,
  289. .platdata_auto_alloc_size = sizeof(struct pca953x_info),
  290. .of_match = pca953x_ids,
  291. };