blob: 35585dc8ac9b55157b139ef493ec9a56e752b7be [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hans de Goede2fcf0332015-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 Goede2fcf0332015-04-25 17:25:14 +02006 */
7
8#include <common.h>
Hans de Goede2fcf0332015-04-25 17:25:14 +02009#include <asm/arch/pmic_bus.h>
Hans de Goedef9b7a042015-04-22 11:31:22 +020010#include <asm/gpio.h>
Hans de Goede6944aff2015-10-03 15:18:33 +020011#include <axp_pmic.h>
Hans de Goedef9b7a042015-04-22 11:31:22 +020012#include <dm.h>
13#include <dm/device-internal.h>
14#include <dm/lists.h>
15#include <dm/root.h>
Hans de Goede2fcf0332015-04-25 17:25:14 +020016#include <errno.h>
17
Hans de Goede421b32b2015-04-26 11:19:37 +020018static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val);
19
Hans de Goede2fcf0332015-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 Goede421b32b2015-04-26 11:19:37 +020035static int axp_gpio_direction_input(struct udevice *dev, unsigned pin)
Hans de Goede2fcf0332015-04-25 17:25:14 +020036{
37 u8 reg;
38
39 switch (pin) {
40#ifndef CONFIG_AXP152_POWER /* NA on axp152 */
41 case SUNXI_GPIO_AXP0_VBUS_DETECT:
42 return 0;
43#endif
44 default:
45 reg = axp_get_gpio_ctrl_reg(pin);
46 if (reg == 0)
47 return -EINVAL;
48
49 return pmic_bus_write(reg, AXP_GPIO_CTRL_INPUT);
50 }
51}
52
Hans de Goede421b32b2015-04-26 11:19:37 +020053static int axp_gpio_direction_output(struct udevice *dev, unsigned pin,
54 int val)
Hans de Goede2fcf0332015-04-25 17:25:14 +020055{
56 __maybe_unused int ret;
57 u8 reg;
58
59 switch (pin) {
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +080060#ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
61 /* Only available on later PMICs */
Hans de Goede2fcf0332015-04-25 17:25:14 +020062 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +080063 ret = pmic_bus_clrbits(AXP_MISC_CTRL,
64 AXP_MISC_CTRL_N_VBUSEN_FUNC);
Hans de Goede2fcf0332015-04-25 17:25:14 +020065 if (ret)
66 return ret;
67
68 return axp_gpio_set_value(dev, pin, val);
69#endif
70 default:
71 reg = axp_get_gpio_ctrl_reg(pin);
72 if (reg == 0)
73 return -EINVAL;
74
75 return pmic_bus_write(reg, val ? AXP_GPIO_CTRL_OUTPUT_HIGH :
76 AXP_GPIO_CTRL_OUTPUT_LOW);
77 }
78}
79
Hans de Goede421b32b2015-04-26 11:19:37 +020080static int axp_gpio_get_value(struct udevice *dev, unsigned pin)
Hans de Goede2fcf0332015-04-25 17:25:14 +020081{
82 u8 reg, val, mask;
83 int ret;
84
85 switch (pin) {
86#ifndef CONFIG_AXP152_POWER /* NA on axp152 */
87 case SUNXI_GPIO_AXP0_VBUS_DETECT:
88 ret = pmic_bus_read(AXP_POWER_STATUS, &val);
89 mask = AXP_POWER_STATUS_VBUS_PRESENT;
90 break;
91#endif
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +080092#ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
93 /* Only available on later PMICs */
Hans de Goede2fcf0332015-04-25 17:25:14 +020094 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +080095 ret = pmic_bus_read(AXP_VBUS_IPSOUT, &val);
96 mask = AXP_VBUS_IPSOUT_DRIVEBUS;
Hans de Goede2fcf0332015-04-25 17:25:14 +020097 break;
98#endif
99 default:
100 reg = axp_get_gpio_ctrl_reg(pin);
101 if (reg == 0)
102 return -EINVAL;
103
104 ret = pmic_bus_read(AXP_GPIO_STATE, &val);
105 mask = 1 << (pin + AXP_GPIO_STATE_OFFSET);
106 }
107 if (ret)
108 return ret;
109
110 return (val & mask) ? 1 : 0;
111}
112
Hans de Goede421b32b2015-04-26 11:19:37 +0200113static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val)
Hans de Goede2fcf0332015-04-25 17:25:14 +0200114{
115 u8 reg;
116
117 switch (pin) {
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +0800118#ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
119 /* Only available on later PMICs */
Hans de Goede2fcf0332015-04-25 17:25:14 +0200120 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
121 if (val)
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +0800122 return pmic_bus_setbits(AXP_VBUS_IPSOUT,
123 AXP_VBUS_IPSOUT_DRIVEBUS);
Hans de Goede2fcf0332015-04-25 17:25:14 +0200124 else
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +0800125 return pmic_bus_clrbits(AXP_VBUS_IPSOUT,
126 AXP_VBUS_IPSOUT_DRIVEBUS);
Hans de Goede2fcf0332015-04-25 17:25:14 +0200127#endif
128 default:
129 reg = axp_get_gpio_ctrl_reg(pin);
130 if (reg == 0)
131 return -EINVAL;
132
133 return pmic_bus_write(reg, val ? AXP_GPIO_CTRL_OUTPUT_HIGH :
134 AXP_GPIO_CTRL_OUTPUT_LOW);
135 }
136}
137
Hans de Goedef9b7a042015-04-22 11:31:22 +0200138static const struct dm_gpio_ops gpio_axp_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};
144
145static int gpio_axp_probe(struct udevice *dev)
146{
147 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
148
149 /* Tell the uclass how many GPIOs we have */
150 uc_priv->bank_name = strdup(SUNXI_GPIO_AXP0_PREFIX);
151 uc_priv->gpio_count = SUNXI_GPIO_AXP0_GPIO_COUNT;
152
153 return 0;
154}
155
156U_BOOT_DRIVER(gpio_axp) = {
157 .name = "gpio_axp",
158 .id = UCLASS_GPIO,
159 .ops = &gpio_axp_ops,
160 .probe = gpio_axp_probe,
161};
Hans de Goedef9b7a042015-04-22 11:31:22 +0200162
Hans de Goede2fcf0332015-04-25 17:25:14 +0200163int axp_gpio_init(void)
164{
Hans de Goede421b32b2015-04-26 11:19:37 +0200165 struct udevice *dev;
Hans de Goede2fcf0332015-04-25 17:25:14 +0200166 int ret;
167
168 ret = pmic_bus_init();
169 if (ret)
170 return ret;
171
Hans de Goedef9b7a042015-04-22 11:31:22 +0200172 /* There is no devicetree support for the axp yet, so bind directly */
173 ret = device_bind_driver(dm_root(), "gpio_axp", "AXP-gpio", &dev);
174 if (ret)
175 return ret;
Hans de Goedef9b7a042015-04-22 11:31:22 +0200176
Hans de Goede2fcf0332015-04-25 17:25:14 +0200177 return 0;
178}