0050-gpio-axp-Select-variant-from-compatible-at-runtime.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. From 697eb56ed8c9b3814ddbd87ae0c6e749ccafc9e3 Mon Sep 17 00:00:00 2001
  2. From: Samuel Holland <[email protected]>
  3. Date: Sat, 28 Aug 2021 00:27:19 -0500
  4. Subject: [PATCH 50/90] gpio: axp: Select variant from compatible at runtime
  5. There are three major variants of the AXP PMIC GPIO functionality (plus
  6. PMICs with no GPIOs at all). Except for GPIO3 on the AXP209, which uses
  7. a different register layout, it is straightforward to support all three
  8. variants with a single driver. Do this, and in the process remove the
  9. GPIO-related definitions from the PMIC-specific headers, and therefore
  10. the dependency on AXP_PMIC_BUS.
  11. Signed-off-by: Samuel Holland <[email protected]>
  12. ---
  13. drivers/gpio/Kconfig | 1 -
  14. drivers/gpio/axp_gpio.c | 137 +++++++++++++++++++++-------------------
  15. drivers/power/axp209.c | 6 +-
  16. drivers/power/axp221.c | 4 +-
  17. include/axp152.h | 10 ---
  18. include/axp209.h | 16 ++---
  19. include/axp221.h | 13 ++--
  20. include/axp809.h | 8 ---
  21. include/axp818.h | 8 ---
  22. 9 files changed, 89 insertions(+), 114 deletions(-)
  23. --- a/drivers/gpio/Kconfig
  24. +++ b/drivers/gpio/Kconfig
  25. @@ -107,7 +107,6 @@ config ALTERA_PIO
  26. config AXP_GPIO
  27. bool "X-Powers AXP PMICs GPIO driver"
  28. depends on DM_GPIO && PMIC_AXP
  29. - depends on AXP_PMIC_BUS
  30. help
  31. This driver supports the GPIO pins on
  32. X-Powers AXP152, AXP2xx, and AXP8xx PMICs.
  33. --- a/drivers/gpio/axp_gpio.c
  34. +++ b/drivers/gpio/axp_gpio.c
  35. @@ -7,110 +7,117 @@
  36. #include <common.h>
  37. #include <asm/gpio.h>
  38. -#include <axp_pmic.h>
  39. #include <dm.h>
  40. +#include <dm/device-internal.h>
  41. #include <errno.h>
  42. #include <power/pmic.h>
  43. #define AXP_GPIO_PREFIX "AXP0-"
  44. #define AXP_GPIO_COUNT 4
  45. -static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val);
  46. -
  47. -static u8 axp_get_gpio_ctrl_reg(unsigned pin)
  48. -{
  49. - switch (pin) {
  50. - case 0: return AXP_GPIO0_CTRL;
  51. - case 1: return AXP_GPIO1_CTRL;
  52. -#ifdef AXP_GPIO2_CTRL
  53. - case 2: return AXP_GPIO2_CTRL;
  54. -#endif
  55. -#ifdef AXP_GPIO3_CTRL
  56. - case 3: return AXP_GPIO3_CTRL;
  57. -#endif
  58. - }
  59. - return 0;
  60. -}
  61. -
  62. -static int axp_gpio_direction_input(struct udevice *dev, unsigned pin)
  63. -{
  64. - u8 reg;
  65. -
  66. - reg = axp_get_gpio_ctrl_reg(pin);
  67. - if (reg == 0)
  68. - return -EINVAL;
  69. -
  70. - return pmic_reg_write(dev->parent, reg, AXP_GPIO_CTRL_INPUT);
  71. -}
  72. -
  73. -static int axp_gpio_direction_output(struct udevice *dev, unsigned pin,
  74. - int val)
  75. -{
  76. - u8 reg;
  77. -
  78. - reg = axp_get_gpio_ctrl_reg(pin);
  79. - if (reg == 0)
  80. - return -EINVAL;
  81. -
  82. - return pmic_reg_write(dev->parent, reg,
  83. - val ? AXP_GPIO_CTRL_OUTPUT_HIGH :
  84. - AXP_GPIO_CTRL_OUTPUT_LOW);
  85. -}
  86. +#define AXP_GPIO_CTRL_MASK 0x7
  87. +#define AXP_GPIO_CTRL_OUTPUT_LOW 0
  88. +#define AXP_GPIO_CTRL_OUTPUT_HIGH 1
  89. +
  90. +struct axp_gpio_desc {
  91. + const u8 *pins;
  92. + u8 npins;
  93. + u8 status_reg;
  94. + u8 status_offset;
  95. + u8 input_mux;
  96. +};
  97. static int axp_gpio_get_value(struct udevice *dev, unsigned pin)
  98. {
  99. - u8 reg, mask;
  100. + const struct axp_gpio_desc *desc = dev_get_priv(dev);
  101. int ret;
  102. - reg = axp_get_gpio_ctrl_reg(pin);
  103. - if (reg == 0)
  104. - return -EINVAL;
  105. -
  106. - ret = pmic_reg_read(dev->parent, AXP_GPIO_STATE);
  107. + ret = pmic_reg_read(dev->parent, desc->status_reg);
  108. if (ret < 0)
  109. return ret;
  110. - mask = 1 << (pin + AXP_GPIO_STATE_OFFSET);
  111. -
  112. - return (ret & mask) ? 1 : 0;
  113. + return !!(ret & BIT(desc->status_offset + pin));
  114. }
  115. -static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val)
  116. +static int axp_gpio_set_flags(struct udevice *dev, unsigned pin, ulong flags)
  117. {
  118. - u8 reg;
  119. + const struct axp_gpio_desc *desc = dev_get_priv(dev);
  120. + u8 mux;
  121. - reg = axp_get_gpio_ctrl_reg(pin);
  122. - if (reg == 0)
  123. + if (flags & (GPIOD_MASK_DSTYPE | GPIOD_MASK_PULL))
  124. return -EINVAL;
  125. - return pmic_reg_write(dev->parent, reg,
  126. - val ? AXP_GPIO_CTRL_OUTPUT_HIGH :
  127. - AXP_GPIO_CTRL_OUTPUT_LOW);
  128. + if (flags & GPIOD_IS_IN)
  129. + mux = desc->input_mux;
  130. + else if (flags & GPIOD_IS_OUT_ACTIVE)
  131. + mux = AXP_GPIO_CTRL_OUTPUT_HIGH;
  132. + else
  133. + mux = AXP_GPIO_CTRL_OUTPUT_LOW;
  134. +
  135. + return pmic_clrsetbits(dev->parent, desc->pins[pin],
  136. + AXP_GPIO_CTRL_MASK, mux);
  137. }
  138. static const struct dm_gpio_ops axp_gpio_ops = {
  139. - .direction_input = axp_gpio_direction_input,
  140. - .direction_output = axp_gpio_direction_output,
  141. .get_value = axp_gpio_get_value,
  142. - .set_value = axp_gpio_set_value,
  143. + .xlate = gpio_xlate_offs_flags,
  144. + .set_flags = axp_gpio_set_flags,
  145. };
  146. static int axp_gpio_probe(struct udevice *dev)
  147. {
  148. + struct axp_gpio_desc *desc = (void *)dev_get_driver_data(dev);
  149. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  150. + dev_set_priv(dev, desc);
  151. +
  152. /* Tell the uclass how many GPIOs we have */
  153. uc_priv->bank_name = AXP_GPIO_PREFIX;
  154. - uc_priv->gpio_count = AXP_GPIO_COUNT;
  155. + uc_priv->gpio_count = desc->npins;
  156. return 0;
  157. }
  158. +static const u8 axp152_gpio_pins[] = {
  159. + 0x90, 0x91, 0x92, 0x93,
  160. +};
  161. +
  162. +static const struct axp_gpio_desc axp152_gpio_desc = {
  163. + .pins = axp152_gpio_pins,
  164. + .npins = ARRAY_SIZE(axp152_gpio_pins),
  165. + .status_reg = 0x97,
  166. + .status_offset = 4,
  167. + .input_mux = 3,
  168. +};
  169. +
  170. +static const u8 axp209_gpio_pins[] = {
  171. + 0x90, 0x92, 0x93,
  172. +};
  173. +
  174. +static const struct axp_gpio_desc axp209_gpio_desc = {
  175. + .pins = axp209_gpio_pins,
  176. + .npins = ARRAY_SIZE(axp209_gpio_pins),
  177. + .status_reg = 0x94,
  178. + .status_offset = 4,
  179. + .input_mux = 2,
  180. +};
  181. +
  182. +static const u8 axp221_gpio_pins[] = {
  183. + 0x90, 0x92,
  184. +};
  185. +
  186. +static const struct axp_gpio_desc axp221_gpio_desc = {
  187. + .pins = axp221_gpio_pins,
  188. + .npins = ARRAY_SIZE(axp221_gpio_pins),
  189. + .status_reg = 0x94,
  190. + .input_mux = 2,
  191. +};
  192. +
  193. static const struct udevice_id axp_gpio_ids[] = {
  194. - { .compatible = "x-powers,axp152-gpio" },
  195. - { .compatible = "x-powers,axp209-gpio" },
  196. - { .compatible = "x-powers,axp221-gpio" },
  197. - { .compatible = "x-powers,axp813-gpio" },
  198. + { .compatible = "x-powers,axp152-gpio", .data = (ulong)&axp152_gpio_desc },
  199. + { .compatible = "x-powers,axp209-gpio", .data = (ulong)&axp209_gpio_desc },
  200. + { .compatible = "x-powers,axp221-gpio", .data = (ulong)&axp221_gpio_desc },
  201. + { .compatible = "x-powers,axp813-gpio", .data = (ulong)&axp221_gpio_desc },
  202. { }
  203. };
  204. --- a/drivers/power/axp209.c
  205. +++ b/drivers/power/axp209.c
  206. @@ -215,15 +215,15 @@ int axp_init(void)
  207. * Turn off LDOIO regulators / tri-state GPIO pins, when rebooting
  208. * from android these are sometimes on.
  209. */
  210. - rc = pmic_bus_write(AXP_GPIO0_CTRL, AXP_GPIO_CTRL_INPUT);
  211. + rc = pmic_bus_write(AXP209_GPIO0_CTRL, AXP209_GPIO_CTRL_INPUT);
  212. if (rc)
  213. return rc;
  214. - rc = pmic_bus_write(AXP_GPIO1_CTRL, AXP_GPIO_CTRL_INPUT);
  215. + rc = pmic_bus_write(AXP209_GPIO1_CTRL, AXP209_GPIO_CTRL_INPUT);
  216. if (rc)
  217. return rc;
  218. - rc = pmic_bus_write(AXP_GPIO2_CTRL, AXP_GPIO_CTRL_INPUT);
  219. + rc = pmic_bus_write(AXP209_GPIO2_CTRL, AXP209_GPIO_CTRL_INPUT);
  220. if (rc)
  221. return rc;
  222. --- a/drivers/power/axp221.c
  223. +++ b/drivers/power/axp221.c
  224. @@ -226,11 +226,11 @@ int axp_init(void)
  225. * Turn off LDOIO regulators / tri-state GPIO pins, when rebooting
  226. * from android these are sometimes on.
  227. */
  228. - ret = pmic_bus_write(AXP_GPIO0_CTRL, AXP_GPIO_CTRL_INPUT);
  229. + ret = pmic_bus_write(AXP221_GPIO0_CTRL, AXP221_GPIO_CTRL_INPUT);
  230. if (ret)
  231. return ret;
  232. - ret = pmic_bus_write(AXP_GPIO1_CTRL, AXP_GPIO_CTRL_INPUT);
  233. + ret = pmic_bus_write(AXP221_GPIO1_CTRL, AXP221_GPIO_CTRL_INPUT);
  234. if (ret)
  235. return ret;
  236. --- a/include/axp152.h
  237. +++ b/include/axp152.h
  238. @@ -14,17 +14,7 @@ enum axp152_reg {
  239. #define AXP152_POWEROFF (1 << 7)
  240. -/* For axp_gpio.c */
  241. #ifdef CONFIG_AXP152_POWER
  242. #define AXP_POWER_STATUS 0x00
  243. #define AXP_POWER_STATUS_ALDO_IN BIT(0)
  244. -#define AXP_GPIO0_CTRL 0x90
  245. -#define AXP_GPIO1_CTRL 0x91
  246. -#define AXP_GPIO2_CTRL 0x92
  247. -#define AXP_GPIO3_CTRL 0x93
  248. -#define AXP_GPIO_CTRL_OUTPUT_LOW 0x00 /* Drive pin low */
  249. -#define AXP_GPIO_CTRL_OUTPUT_HIGH 0x01 /* Drive pin high */
  250. -#define AXP_GPIO_CTRL_INPUT 0x02 /* Input */
  251. -#define AXP_GPIO_STATE 0x97
  252. -#define AXP_GPIO_STATE_OFFSET 0
  253. #endif
  254. --- a/include/axp209.h
  255. +++ b/include/axp209.h
  256. @@ -21,6 +21,9 @@ enum axp209_reg {
  257. AXP209_IRQ_ENABLE5 = 0x44,
  258. AXP209_IRQ_STATUS5 = 0x4c,
  259. AXP209_SHUTDOWN = 0x32,
  260. + AXP209_GPIO0_CTRL = 0x90,
  261. + AXP209_GPIO1_CTRL = 0x92,
  262. + AXP209_GPIO2_CTRL = 0x93,
  263. };
  264. #define AXP209_POWER_STATUS_ON_BY_DC BIT(0)
  265. @@ -73,16 +76,11 @@ enum axp209_reg {
  266. #define AXP209_POWEROFF BIT(7)
  267. -/* For axp_gpio.c */
  268. +#define AXP209_GPIO_CTRL_OUTPUT_LOW 0x00
  269. +#define AXP209_GPIO_CTRL_OUTPUT_HIGH 0x01
  270. +#define AXP209_GPIO_CTRL_INPUT 0x02
  271. +
  272. #ifdef CONFIG_AXP209_POWER
  273. #define AXP_POWER_STATUS 0x00
  274. #define AXP_POWER_STATUS_ALDO_IN BIT(0)
  275. -#define AXP_GPIO0_CTRL 0x90
  276. -#define AXP_GPIO1_CTRL 0x92
  277. -#define AXP_GPIO2_CTRL 0x93
  278. -#define AXP_GPIO_CTRL_OUTPUT_LOW 0x00 /* Drive pin low */
  279. -#define AXP_GPIO_CTRL_OUTPUT_HIGH 0x01 /* Drive pin high */
  280. -#define AXP_GPIO_CTRL_INPUT 0x02 /* Input */
  281. -#define AXP_GPIO_STATE 0x94
  282. -#define AXP_GPIO_STATE_OFFSET 4
  283. #endif
  284. --- a/include/axp221.h
  285. +++ b/include/axp221.h
  286. @@ -44,20 +44,17 @@
  287. #define AXP221_ALDO3_CTRL 0x2a
  288. #define AXP221_SHUTDOWN 0x32
  289. #define AXP221_SHUTDOWN_POWEROFF (1 << 7)
  290. +#define AXP221_GPIO0_CTRL 0x90
  291. +#define AXP221_GPIO1_CTRL 0x92
  292. +#define AXP221_GPIO_CTRL_OUTPUT_LOW 0x00
  293. +#define AXP221_GPIO_CTRL_OUTPUT_HIGH 0x01
  294. +#define AXP221_GPIO_CTRL_INPUT 0x02
  295. #define AXP221_PAGE 0xff
  296. /* Page 1 addresses */
  297. #define AXP221_SID 0x20
  298. -/* For axp_gpio.c */
  299. #ifdef CONFIG_AXP221_POWER
  300. #define AXP_POWER_STATUS 0x00
  301. #define AXP_POWER_STATUS_ALDO_IN BIT(0)
  302. -#define AXP_GPIO0_CTRL 0x90
  303. -#define AXP_GPIO1_CTRL 0x92
  304. -#define AXP_GPIO_CTRL_OUTPUT_LOW 0x00 /* Drive pin low */
  305. -#define AXP_GPIO_CTRL_OUTPUT_HIGH 0x01 /* Drive pin high */
  306. -#define AXP_GPIO_CTRL_INPUT 0x02 /* Input */
  307. -#define AXP_GPIO_STATE 0x94
  308. -#define AXP_GPIO_STATE_OFFSET 0
  309. #endif
  310. --- a/include/axp809.h
  311. +++ b/include/axp809.h
  312. @@ -43,15 +43,7 @@
  313. #define AXP809_SHUTDOWN 0x32
  314. #define AXP809_SHUTDOWN_POWEROFF (1 << 7)
  315. -/* For axp_gpio.c */
  316. #ifdef CONFIG_AXP809_POWER
  317. #define AXP_POWER_STATUS 0x00
  318. #define AXP_POWER_STATUS_ALDO_IN BIT(0)
  319. -#define AXP_GPIO0_CTRL 0x90
  320. -#define AXP_GPIO1_CTRL 0x92
  321. -#define AXP_GPIO_CTRL_OUTPUT_LOW 0x00 /* Drive pin low */
  322. -#define AXP_GPIO_CTRL_OUTPUT_HIGH 0x01 /* Drive pin high */
  323. -#define AXP_GPIO_CTRL_INPUT 0x02 /* Input */
  324. -#define AXP_GPIO_STATE 0x94
  325. -#define AXP_GPIO_STATE_OFFSET 0
  326. #endif
  327. --- a/include/axp818.h
  328. +++ b/include/axp818.h
  329. @@ -57,15 +57,7 @@
  330. #define AXP818_SHUTDOWN 0x32
  331. #define AXP818_SHUTDOWN_POWEROFF (1 << 7)
  332. -/* For axp_gpio.c */
  333. #ifdef CONFIG_AXP818_POWER
  334. #define AXP_POWER_STATUS 0x00
  335. #define AXP_POWER_STATUS_ALDO_IN BIT(0)
  336. -#define AXP_GPIO0_CTRL 0x90
  337. -#define AXP_GPIO1_CTRL 0x92
  338. -#define AXP_GPIO_CTRL_OUTPUT_LOW 0x00 /* Drive pin low */
  339. -#define AXP_GPIO_CTRL_OUTPUT_HIGH 0x01 /* Drive pin high */
  340. -#define AXP_GPIO_CTRL_INPUT 0x02 /* Input */
  341. -#define AXP_GPIO_STATE 0x94
  342. -#define AXP_GPIO_STATE_OFFSET 0
  343. #endif