Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com> |
| 4 | * |
| 5 | * X-Powers AXP Power Management ICs gpio driver |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 9 | #include <asm/arch/pmic_bus.h> |
Hans de Goede | f9b7a04 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 10 | #include <asm/gpio.h> |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 11 | #include <axp_pmic.h> |
Hans de Goede | f9b7a04 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 12 | #include <dm.h> |
| 13 | #include <dm/device-internal.h> |
| 14 | #include <dm/lists.h> |
| 15 | #include <dm/root.h> |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 16 | #include <errno.h> |
Andre Przywara | 207ed0a | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 17 | #include <sunxi_gpio.h> |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 18 | |
Hans de Goede | 421b32b | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 19 | static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val); |
| 20 | |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 21 | static 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 Goede | 421b32b | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 36 | static int axp_gpio_direction_input(struct udevice *dev, unsigned pin) |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 37 | { |
| 38 | u8 reg; |
| 39 | |
Samuel Holland | 09cbd38 | 2023-01-22 17:46:22 -0600 | [diff] [blame] | 40 | reg = axp_get_gpio_ctrl_reg(pin); |
| 41 | if (reg == 0) |
| 42 | return -EINVAL; |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 43 | |
Samuel Holland | 09cbd38 | 2023-01-22 17:46:22 -0600 | [diff] [blame] | 44 | return pmic_bus_write(reg, AXP_GPIO_CTRL_INPUT); |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Hans de Goede | 421b32b | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 47 | static int axp_gpio_direction_output(struct udevice *dev, unsigned pin, |
| 48 | int val) |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 49 | { |
| 50 | __maybe_unused int ret; |
| 51 | u8 reg; |
| 52 | |
| 53 | switch (pin) { |
Chen-Yu Tsai | 81a8aa3 | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 54 | #ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC |
| 55 | /* Only available on later PMICs */ |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 56 | case SUNXI_GPIO_AXP0_VBUS_ENABLE: |
Chen-Yu Tsai | 81a8aa3 | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 57 | ret = pmic_bus_clrbits(AXP_MISC_CTRL, |
| 58 | AXP_MISC_CTRL_N_VBUSEN_FUNC); |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 59 | 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 Goede | 421b32b | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 74 | static int axp_gpio_get_value(struct udevice *dev, unsigned pin) |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 75 | { |
| 76 | u8 reg, val, mask; |
| 77 | int ret; |
| 78 | |
| 79 | switch (pin) { |
Chen-Yu Tsai | 81a8aa3 | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 80 | #ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC |
| 81 | /* Only available on later PMICs */ |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 82 | case SUNXI_GPIO_AXP0_VBUS_ENABLE: |
Chen-Yu Tsai | 81a8aa3 | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 83 | ret = pmic_bus_read(AXP_VBUS_IPSOUT, &val); |
| 84 | mask = AXP_VBUS_IPSOUT_DRIVEBUS; |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 85 | 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 Goede | 421b32b | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 101 | static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val) |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 102 | { |
| 103 | u8 reg; |
| 104 | |
| 105 | switch (pin) { |
Chen-Yu Tsai | 81a8aa3 | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 106 | #ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC |
| 107 | /* Only available on later PMICs */ |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 108 | case SUNXI_GPIO_AXP0_VBUS_ENABLE: |
| 109 | if (val) |
Chen-Yu Tsai | 81a8aa3 | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 110 | return pmic_bus_setbits(AXP_VBUS_IPSOUT, |
| 111 | AXP_VBUS_IPSOUT_DRIVEBUS); |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 112 | else |
Chen-Yu Tsai | 81a8aa3 | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 113 | return pmic_bus_clrbits(AXP_VBUS_IPSOUT, |
| 114 | AXP_VBUS_IPSOUT_DRIVEBUS); |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 115 | #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 Goede | f9b7a04 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 126 | static 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 | |
| 133 | static 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 | |
| 144 | U_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 Goede | f9b7a04 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 150 | |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 151 | int axp_gpio_init(void) |
| 152 | { |
Hans de Goede | 421b32b | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 153 | struct udevice *dev; |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 154 | int ret; |
| 155 | |
| 156 | ret = pmic_bus_init(); |
| 157 | if (ret) |
| 158 | return ret; |
| 159 | |
Hans de Goede | f9b7a04 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 160 | /* 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 Goede | f9b7a04 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 164 | |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 165 | return 0; |
| 166 | } |