pca953x_gpio.c 9.0 KB

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