1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com>
4 *
5 * X-Powers AXP Power Management ICs gpio driver
6 */
7
8 #include <common.h>
9 #include <asm/arch/pmic_bus.h>
10 #include <asm/gpio.h>
11 #include <axp_pmic.h>
12 #include <dm.h>
13 #include <dm/device-internal.h>
14 #include <dm/lists.h>
15 #include <dm/root.h>
16 #include <errno.h>
17
18 static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val);
19
axp_get_gpio_ctrl_reg(unsigned pin)20 static 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
axp_gpio_direction_input(struct udevice * dev,unsigned pin)35 static int axp_gpio_direction_input(struct udevice *dev, unsigned pin)
36 {
37 u8 reg;
38
39 reg = axp_get_gpio_ctrl_reg(pin);
40 if (reg == 0)
41 return -EINVAL;
42
43 return pmic_bus_write(reg, AXP_GPIO_CTRL_INPUT);
44 }
45
axp_gpio_direction_output(struct udevice * dev,unsigned pin,int val)46 static int axp_gpio_direction_output(struct udevice *dev, unsigned pin,
47 int val)
48 {
49 __maybe_unused int ret;
50 u8 reg;
51
52 switch (pin) {
53 #ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
54 /* Only available on later PMICs */
55 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
56 ret = pmic_bus_clrbits(AXP_MISC_CTRL,
57 AXP_MISC_CTRL_N_VBUSEN_FUNC);
58 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
axp_gpio_get_value(struct udevice * dev,unsigned pin)73 static int axp_gpio_get_value(struct udevice *dev, unsigned pin)
74 {
75 u8 reg, val, mask;
76 int ret;
77
78 switch (pin) {
79 #ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
80 /* Only available on later PMICs */
81 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
82 ret = pmic_bus_read(AXP_VBUS_IPSOUT, &val);
83 mask = AXP_VBUS_IPSOUT_DRIVEBUS;
84 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
axp_gpio_set_value(struct udevice * dev,unsigned pin,int val)100 static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val)
101 {
102 u8 reg;
103
104 switch (pin) {
105 #ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
106 /* Only available on later PMICs */
107 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
108 if (val)
109 return pmic_bus_setbits(AXP_VBUS_IPSOUT,
110 AXP_VBUS_IPSOUT_DRIVEBUS);
111 else
112 return pmic_bus_clrbits(AXP_VBUS_IPSOUT,
113 AXP_VBUS_IPSOUT_DRIVEBUS);
114 #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
125 static 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
gpio_axp_probe(struct udevice * dev)132 static 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
143 U_BOOT_DRIVER(gpio_axp) = {
144 .name = "gpio_axp",
145 .id = UCLASS_GPIO,
146 .ops = &gpio_axp_ops,
147 .probe = gpio_axp_probe,
148 };
149
axp_gpio_init(void)150 int axp_gpio_init(void)
151 {
152 struct udevice *dev;
153 int ret;
154
155 ret = pmic_bus_init();
156 if (ret)
157 return ret;
158
159 /* 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;
163
164 return 0;
165 }
166