blob: 73058cf40b4b9a8f55d92b19d7627baf76cce526 [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>
9#include <asm/arch/gpio.h>
10#include <asm/arch/pmic_bus.h>
Hans de Goedef9b7a042015-04-22 11:31:22 +020011#include <asm/gpio.h>
Hans de Goede6944aff2015-10-03 15:18:33 +020012#include <axp_pmic.h>
Hans de Goedef9b7a042015-04-22 11:31:22 +020013#include <dm.h>
14#include <dm/device-internal.h>
15#include <dm/lists.h>
16#include <dm/root.h>
Hans de Goede2fcf0332015-04-25 17:25:14 +020017#include <errno.h>
18
Hans de Goede421b32b2015-04-26 11:19:37 +020019static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val);
20
Hans de Goede2fcf0332015-04-25 17:25:14 +020021static u8 axp_get_gpio_ctrl_reg(unsigned pin)
22{
23 switch (pin) {
24 case 0: return AXP_GPIO0_CTRL;
25 case 1: return AXP_GPIO1_CTRL;
26#ifdef AXP_GPIO2_CTRL
27 case 2: return AXP_GPIO2_CTRL;
28#endif
29#ifdef AXP_GPIO3_CTRL
30 case 3: return AXP_GPIO3_CTRL;
31#endif
32 }
33 return 0;
34}
35
Hans de Goede421b32b2015-04-26 11:19:37 +020036static int axp_gpio_direction_input(struct udevice *dev, unsigned pin)
Hans de Goede2fcf0332015-04-25 17:25:14 +020037{
38 u8 reg;
39
40 switch (pin) {
41#ifndef CONFIG_AXP152_POWER /* NA on axp152 */
42 case SUNXI_GPIO_AXP0_VBUS_DETECT:
43 return 0;
44#endif
45 default:
46 reg = axp_get_gpio_ctrl_reg(pin);
47 if (reg == 0)
48 return -EINVAL;
49
50 return pmic_bus_write(reg, AXP_GPIO_CTRL_INPUT);
51 }
52}
53
Hans de Goede421b32b2015-04-26 11:19:37 +020054static int axp_gpio_direction_output(struct udevice *dev, unsigned pin,
55 int val)
Hans de Goede2fcf0332015-04-25 17:25:14 +020056{
57 __maybe_unused int ret;
58 u8 reg;
59
60 switch (pin) {
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +080061#ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
62 /* Only available on later PMICs */
Hans de Goede2fcf0332015-04-25 17:25:14 +020063 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +080064 ret = pmic_bus_clrbits(AXP_MISC_CTRL,
65 AXP_MISC_CTRL_N_VBUSEN_FUNC);
Hans de Goede2fcf0332015-04-25 17:25:14 +020066 if (ret)
67 return ret;
68
69 return axp_gpio_set_value(dev, pin, val);
70#endif
71 default:
72 reg = axp_get_gpio_ctrl_reg(pin);
73 if (reg == 0)
74 return -EINVAL;
75
76 return pmic_bus_write(reg, val ? AXP_GPIO_CTRL_OUTPUT_HIGH :
77 AXP_GPIO_CTRL_OUTPUT_LOW);
78 }
79}
80
Hans de Goede421b32b2015-04-26 11:19:37 +020081static int axp_gpio_get_value(struct udevice *dev, unsigned pin)
Hans de Goede2fcf0332015-04-25 17:25:14 +020082{
83 u8 reg, val, mask;
84 int ret;
85
86 switch (pin) {
87#ifndef CONFIG_AXP152_POWER /* NA on axp152 */
88 case SUNXI_GPIO_AXP0_VBUS_DETECT:
89 ret = pmic_bus_read(AXP_POWER_STATUS, &val);
90 mask = AXP_POWER_STATUS_VBUS_PRESENT;
91 break;
92#endif
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +080093#ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
94 /* Only available on later PMICs */
Hans de Goede2fcf0332015-04-25 17:25:14 +020095 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +080096 ret = pmic_bus_read(AXP_VBUS_IPSOUT, &val);
97 mask = AXP_VBUS_IPSOUT_DRIVEBUS;
Hans de Goede2fcf0332015-04-25 17:25:14 +020098 break;
99#endif
100 default:
101 reg = axp_get_gpio_ctrl_reg(pin);
102 if (reg == 0)
103 return -EINVAL;
104
105 ret = pmic_bus_read(AXP_GPIO_STATE, &val);
106 mask = 1 << (pin + AXP_GPIO_STATE_OFFSET);
107 }
108 if (ret)
109 return ret;
110
111 return (val & mask) ? 1 : 0;
112}
113
Hans de Goede421b32b2015-04-26 11:19:37 +0200114static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val)
Hans de Goede2fcf0332015-04-25 17:25:14 +0200115{
116 u8 reg;
117
118 switch (pin) {
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +0800119#ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
120 /* Only available on later PMICs */
Hans de Goede2fcf0332015-04-25 17:25:14 +0200121 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
122 if (val)
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +0800123 return pmic_bus_setbits(AXP_VBUS_IPSOUT,
124 AXP_VBUS_IPSOUT_DRIVEBUS);
Hans de Goede2fcf0332015-04-25 17:25:14 +0200125 else
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +0800126 return pmic_bus_clrbits(AXP_VBUS_IPSOUT,
127 AXP_VBUS_IPSOUT_DRIVEBUS);
Hans de Goede2fcf0332015-04-25 17:25:14 +0200128#endif
129 default:
130 reg = axp_get_gpio_ctrl_reg(pin);
131 if (reg == 0)
132 return -EINVAL;
133
134 return pmic_bus_write(reg, val ? AXP_GPIO_CTRL_OUTPUT_HIGH :
135 AXP_GPIO_CTRL_OUTPUT_LOW);
136 }
137}
138
Hans de Goedef9b7a042015-04-22 11:31:22 +0200139static const struct dm_gpio_ops gpio_axp_ops = {
140 .direction_input = axp_gpio_direction_input,
141 .direction_output = axp_gpio_direction_output,
142 .get_value = axp_gpio_get_value,
143 .set_value = axp_gpio_set_value,
144};
145
146static int gpio_axp_probe(struct udevice *dev)
147{
148 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
149
150 /* Tell the uclass how many GPIOs we have */
151 uc_priv->bank_name = strdup(SUNXI_GPIO_AXP0_PREFIX);
152 uc_priv->gpio_count = SUNXI_GPIO_AXP0_GPIO_COUNT;
153
154 return 0;
155}
156
157U_BOOT_DRIVER(gpio_axp) = {
158 .name = "gpio_axp",
159 .id = UCLASS_GPIO,
160 .ops = &gpio_axp_ops,
161 .probe = gpio_axp_probe,
162};
Hans de Goedef9b7a042015-04-22 11:31:22 +0200163
Hans de Goede2fcf0332015-04-25 17:25:14 +0200164int axp_gpio_init(void)
165{
Hans de Goede421b32b2015-04-26 11:19:37 +0200166 struct udevice *dev;
Hans de Goede2fcf0332015-04-25 17:25:14 +0200167 int ret;
168
169 ret = pmic_bus_init();
170 if (ret)
171 return ret;
172
Hans de Goedef9b7a042015-04-22 11:31:22 +0200173 /* There is no devicetree support for the axp yet, so bind directly */
174 ret = device_bind_driver(dm_root(), "gpio_axp", "AXP-gpio", &dev);
175 if (ret)
176 return ret;
Hans de Goedef9b7a042015-04-22 11:31:22 +0200177
Hans de Goede2fcf0332015-04-25 17:25:14 +0200178 return 0;
179}