blob: 6e632c8fc7308da348adeedccd347e880410253d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hans de Goede3ae1d132015-04-25 17:25:14 +02002/*
3 * (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com>
4 *
5 * X-Powers AXP Power Management ICs gpio driver
Hans de Goede3ae1d132015-04-25 17:25:14 +02006 */
7
Hans de Goede3ae1d132015-04-25 17:25:14 +02008#include <asm/arch/pmic_bus.h>
Hans de Goede08607d12015-04-22 11:31:22 +02009#include <asm/gpio.h>
Hans de Goeded9ee84b2015-10-03 15:18:33 +020010#include <axp_pmic.h>
Hans de Goede08607d12015-04-22 11:31:22 +020011#include <dm.h>
12#include <dm/device-internal.h>
13#include <dm/lists.h>
14#include <dm/root.h>
Hans de Goede3ae1d132015-04-25 17:25:14 +020015#include <errno.h>
Andre Przywaraf944a612022-09-06 10:36:38 +010016#include <sunxi_gpio.h>
Hans de Goede3ae1d132015-04-25 17:25:14 +020017
Hans de Goede66bf5082015-04-26 11:19:37 +020018static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val);
19
Hans de Goede3ae1d132015-04-25 17:25:14 +020020static u8 axp_get_gpio_ctrl_reg(unsigned pin)
21{
22 switch (pin) {
23 case 0: return AXP_GPIO0_CTRL;
24 case 1: return AXP_GPIO1_CTRL;
25#ifdef AXP_GPIO2_CTRL
26 case 2: return AXP_GPIO2_CTRL;
27#endif
28#ifdef AXP_GPIO3_CTRL
29 case 3: return AXP_GPIO3_CTRL;
30#endif
31 }
32 return 0;
33}
34
Hans de Goede66bf5082015-04-26 11:19:37 +020035static int axp_gpio_direction_input(struct udevice *dev, unsigned pin)
Hans de Goede3ae1d132015-04-25 17:25:14 +020036{
37 u8 reg;
38
Samuel Holland5f9c8442023-01-22 17:46:22 -060039 reg = axp_get_gpio_ctrl_reg(pin);
40 if (reg == 0)
41 return -EINVAL;
Hans de Goede3ae1d132015-04-25 17:25:14 +020042
Samuel Holland5f9c8442023-01-22 17:46:22 -060043 return pmic_bus_write(reg, AXP_GPIO_CTRL_INPUT);
Hans de Goede3ae1d132015-04-25 17:25:14 +020044}
45
Hans de Goede66bf5082015-04-26 11:19:37 +020046static int axp_gpio_direction_output(struct udevice *dev, unsigned pin,
47 int val)
Hans de Goede3ae1d132015-04-25 17:25:14 +020048{
49 __maybe_unused int ret;
50 u8 reg;
51
52 switch (pin) {
Chen-Yu Tsai2e8dced2016-03-30 00:26:56 +080053#ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
54 /* Only available on later PMICs */
Hans de Goede3ae1d132015-04-25 17:25:14 +020055 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
Chen-Yu Tsai2e8dced2016-03-30 00:26:56 +080056 ret = pmic_bus_clrbits(AXP_MISC_CTRL,
57 AXP_MISC_CTRL_N_VBUSEN_FUNC);
Hans de Goede3ae1d132015-04-25 17:25:14 +020058 if (ret)
59 return ret;
60
61 return axp_gpio_set_value(dev, pin, val);
62#endif
63 default:
64 reg = axp_get_gpio_ctrl_reg(pin);
65 if (reg == 0)
66 return -EINVAL;
67
68 return pmic_bus_write(reg, val ? AXP_GPIO_CTRL_OUTPUT_HIGH :
69 AXP_GPIO_CTRL_OUTPUT_LOW);
70 }
71}
72
Hans de Goede66bf5082015-04-26 11:19:37 +020073static int axp_gpio_get_value(struct udevice *dev, unsigned pin)
Hans de Goede3ae1d132015-04-25 17:25:14 +020074{
75 u8 reg, val, mask;
76 int ret;
77
78 switch (pin) {
Chen-Yu Tsai2e8dced2016-03-30 00:26:56 +080079#ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
80 /* Only available on later PMICs */
Hans de Goede3ae1d132015-04-25 17:25:14 +020081 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
Chen-Yu Tsai2e8dced2016-03-30 00:26:56 +080082 ret = pmic_bus_read(AXP_VBUS_IPSOUT, &val);
83 mask = AXP_VBUS_IPSOUT_DRIVEBUS;
Hans de Goede3ae1d132015-04-25 17:25:14 +020084 break;
85#endif
86 default:
87 reg = axp_get_gpio_ctrl_reg(pin);
88 if (reg == 0)
89 return -EINVAL;
90
91 ret = pmic_bus_read(AXP_GPIO_STATE, &val);
92 mask = 1 << (pin + AXP_GPIO_STATE_OFFSET);
93 }
94 if (ret)
95 return ret;
96
97 return (val & mask) ? 1 : 0;
98}
99
Hans de Goede66bf5082015-04-26 11:19:37 +0200100static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val)
Hans de Goede3ae1d132015-04-25 17:25:14 +0200101{
102 u8 reg;
103
104 switch (pin) {
Chen-Yu Tsai2e8dced2016-03-30 00:26:56 +0800105#ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
106 /* Only available on later PMICs */
Hans de Goede3ae1d132015-04-25 17:25:14 +0200107 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
108 if (val)
Chen-Yu Tsai2e8dced2016-03-30 00:26:56 +0800109 return pmic_bus_setbits(AXP_VBUS_IPSOUT,
110 AXP_VBUS_IPSOUT_DRIVEBUS);
Hans de Goede3ae1d132015-04-25 17:25:14 +0200111 else
Chen-Yu Tsai2e8dced2016-03-30 00:26:56 +0800112 return pmic_bus_clrbits(AXP_VBUS_IPSOUT,
113 AXP_VBUS_IPSOUT_DRIVEBUS);
Hans de Goede3ae1d132015-04-25 17:25:14 +0200114#endif
115 default:
116 reg = axp_get_gpio_ctrl_reg(pin);
117 if (reg == 0)
118 return -EINVAL;
119
120 return pmic_bus_write(reg, val ? AXP_GPIO_CTRL_OUTPUT_HIGH :
121 AXP_GPIO_CTRL_OUTPUT_LOW);
122 }
123}
124
Hans de Goede08607d12015-04-22 11:31:22 +0200125static const struct dm_gpio_ops gpio_axp_ops = {
126 .direction_input = axp_gpio_direction_input,
127 .direction_output = axp_gpio_direction_output,
128 .get_value = axp_gpio_get_value,
129 .set_value = axp_gpio_set_value,
130};
131
132static int gpio_axp_probe(struct udevice *dev)
133{
134 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
135
136 /* Tell the uclass how many GPIOs we have */
137 uc_priv->bank_name = strdup(SUNXI_GPIO_AXP0_PREFIX);
138 uc_priv->gpio_count = SUNXI_GPIO_AXP0_GPIO_COUNT;
139
140 return 0;
141}
142
143U_BOOT_DRIVER(gpio_axp) = {
144 .name = "gpio_axp",
145 .id = UCLASS_GPIO,
146 .ops = &gpio_axp_ops,
147 .probe = gpio_axp_probe,
148};
Hans de Goede08607d12015-04-22 11:31:22 +0200149
Hans de Goede3ae1d132015-04-25 17:25:14 +0200150int axp_gpio_init(void)
151{
Hans de Goede66bf5082015-04-26 11:19:37 +0200152 struct udevice *dev;
Hans de Goede3ae1d132015-04-25 17:25:14 +0200153 int ret;
154
155 ret = pmic_bus_init();
156 if (ret)
157 return ret;
Hans de Goede08607d12015-04-22 11:31:22 +0200158
Hans de Goede08607d12015-04-22 11:31:22 +0200159 /* There is no devicetree support for the axp yet, so bind directly */
160 ret = device_bind_driver(dm_root(), "gpio_axp", "AXP-gpio", &dev);
161 if (ret)
162 return ret;
Hans de Goede3ae1d132015-04-25 17:25:14 +0200163
164 return 0;
165}