blob: af6631697f58bf13ca6068ac3255d2d5e7351634 [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>
Andre Przywara207ed0a2022-09-06 10:36:38 +010017#include <sunxi_gpio.h>
Hans de Goede2fcf0332015-04-25 17:25:14 +020018
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
Samuel Holland09cbd382023-01-22 17:46:22 -060040 reg = axp_get_gpio_ctrl_reg(pin);
41 if (reg == 0)
42 return -EINVAL;
Hans de Goede2fcf0332015-04-25 17:25:14 +020043
Samuel Holland09cbd382023-01-22 17:46:22 -060044 return pmic_bus_write(reg, AXP_GPIO_CTRL_INPUT);
Hans de Goede2fcf0332015-04-25 17:25:14 +020045}
46
Hans de Goede421b32b2015-04-26 11:19:37 +020047static int axp_gpio_direction_output(struct udevice *dev, unsigned pin,
48 int val)
Hans de Goede2fcf0332015-04-25 17:25:14 +020049{
50 __maybe_unused int ret;
51 u8 reg;
52
53 switch (pin) {
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +080054#ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
55 /* Only available on later PMICs */
Hans de Goede2fcf0332015-04-25 17:25:14 +020056 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +080057 ret = pmic_bus_clrbits(AXP_MISC_CTRL,
58 AXP_MISC_CTRL_N_VBUSEN_FUNC);
Hans de Goede2fcf0332015-04-25 17:25:14 +020059 if (ret)
60 return ret;
61
62 return axp_gpio_set_value(dev, pin, val);
63#endif
64 default:
65 reg = axp_get_gpio_ctrl_reg(pin);
66 if (reg == 0)
67 return -EINVAL;
68
69 return pmic_bus_write(reg, val ? AXP_GPIO_CTRL_OUTPUT_HIGH :
70 AXP_GPIO_CTRL_OUTPUT_LOW);
71 }
72}
73
Hans de Goede421b32b2015-04-26 11:19:37 +020074static int axp_gpio_get_value(struct udevice *dev, unsigned pin)
Hans de Goede2fcf0332015-04-25 17:25:14 +020075{
76 u8 reg, val, mask;
77 int ret;
78
79 switch (pin) {
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +080080#ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
81 /* Only available on later PMICs */
Hans de Goede2fcf0332015-04-25 17:25:14 +020082 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +080083 ret = pmic_bus_read(AXP_VBUS_IPSOUT, &val);
84 mask = AXP_VBUS_IPSOUT_DRIVEBUS;
Hans de Goede2fcf0332015-04-25 17:25:14 +020085 break;
86#endif
87 default:
88 reg = axp_get_gpio_ctrl_reg(pin);
89 if (reg == 0)
90 return -EINVAL;
91
92 ret = pmic_bus_read(AXP_GPIO_STATE, &val);
93 mask = 1 << (pin + AXP_GPIO_STATE_OFFSET);
94 }
95 if (ret)
96 return ret;
97
98 return (val & mask) ? 1 : 0;
99}
100
Hans de Goede421b32b2015-04-26 11:19:37 +0200101static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val)
Hans de Goede2fcf0332015-04-25 17:25:14 +0200102{
103 u8 reg;
104
105 switch (pin) {
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +0800106#ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
107 /* Only available on later PMICs */
Hans de Goede2fcf0332015-04-25 17:25:14 +0200108 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
109 if (val)
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +0800110 return pmic_bus_setbits(AXP_VBUS_IPSOUT,
111 AXP_VBUS_IPSOUT_DRIVEBUS);
Hans de Goede2fcf0332015-04-25 17:25:14 +0200112 else
Chen-Yu Tsai81a8aa32016-03-30 00:26:56 +0800113 return pmic_bus_clrbits(AXP_VBUS_IPSOUT,
114 AXP_VBUS_IPSOUT_DRIVEBUS);
Hans de Goede2fcf0332015-04-25 17:25:14 +0200115#endif
116 default:
117 reg = axp_get_gpio_ctrl_reg(pin);
118 if (reg == 0)
119 return -EINVAL;
120
121 return pmic_bus_write(reg, val ? AXP_GPIO_CTRL_OUTPUT_HIGH :
122 AXP_GPIO_CTRL_OUTPUT_LOW);
123 }
124}
125
Hans de Goedef9b7a042015-04-22 11:31:22 +0200126static const struct dm_gpio_ops gpio_axp_ops = {
127 .direction_input = axp_gpio_direction_input,
128 .direction_output = axp_gpio_direction_output,
129 .get_value = axp_gpio_get_value,
130 .set_value = axp_gpio_set_value,
131};
132
133static int gpio_axp_probe(struct udevice *dev)
134{
135 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
136
137 /* Tell the uclass how many GPIOs we have */
138 uc_priv->bank_name = strdup(SUNXI_GPIO_AXP0_PREFIX);
139 uc_priv->gpio_count = SUNXI_GPIO_AXP0_GPIO_COUNT;
140
141 return 0;
142}
143
144U_BOOT_DRIVER(gpio_axp) = {
145 .name = "gpio_axp",
146 .id = UCLASS_GPIO,
147 .ops = &gpio_axp_ops,
148 .probe = gpio_axp_probe,
149};
Hans de Goedef9b7a042015-04-22 11:31:22 +0200150
Hans de Goede2fcf0332015-04-25 17:25:14 +0200151int axp_gpio_init(void)
152{
Hans de Goede421b32b2015-04-26 11:19:37 +0200153 struct udevice *dev;
Hans de Goede2fcf0332015-04-25 17:25:14 +0200154 int ret;
155
156 ret = pmic_bus_init();
157 if (ret)
158 return ret;
159
Hans de Goedef9b7a042015-04-22 11:31:22 +0200160 /* There is no devicetree support for the axp yet, so bind directly */
161 ret = device_bind_driver(dm_root(), "gpio_axp", "AXP-gpio", &dev);
162 if (ret)
163 return ret;
Hans de Goedef9b7a042015-04-22 11:31:22 +0200164
Hans de Goede2fcf0332015-04-25 17:25:14 +0200165 return 0;
166}