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> |
| 17 | |
Hans de Goede | 421b32b | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 18 | static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val); |
| 19 | |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 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 | |
Hans de Goede | 421b32b | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 35 | static int axp_gpio_direction_input(struct udevice *dev, unsigned pin) |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 36 | { |
| 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 Goede | 421b32b | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 53 | static int axp_gpio_direction_output(struct udevice *dev, unsigned pin, |
| 54 | int val) |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 55 | { |
| 56 | __maybe_unused int ret; |
| 57 | u8 reg; |
| 58 | |
| 59 | switch (pin) { |
Chen-Yu Tsai | 81a8aa3 | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 60 | #ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC |
| 61 | /* Only available on later PMICs */ |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 62 | case SUNXI_GPIO_AXP0_VBUS_ENABLE: |
Chen-Yu Tsai | 81a8aa3 | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 63 | ret = pmic_bus_clrbits(AXP_MISC_CTRL, |
| 64 | AXP_MISC_CTRL_N_VBUSEN_FUNC); |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 65 | 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 Goede | 421b32b | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 80 | static int axp_gpio_get_value(struct udevice *dev, unsigned pin) |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 81 | { |
| 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 Tsai | 81a8aa3 | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 92 | #ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC |
| 93 | /* Only available on later PMICs */ |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 94 | case SUNXI_GPIO_AXP0_VBUS_ENABLE: |
Chen-Yu Tsai | 81a8aa3 | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 95 | ret = pmic_bus_read(AXP_VBUS_IPSOUT, &val); |
| 96 | mask = AXP_VBUS_IPSOUT_DRIVEBUS; |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 97 | 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 Goede | 421b32b | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 113 | 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] | 114 | { |
| 115 | u8 reg; |
| 116 | |
| 117 | switch (pin) { |
Chen-Yu Tsai | 81a8aa3 | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 118 | #ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC |
| 119 | /* Only available on later PMICs */ |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 120 | case SUNXI_GPIO_AXP0_VBUS_ENABLE: |
| 121 | if (val) |
Chen-Yu Tsai | 81a8aa3 | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 122 | return pmic_bus_setbits(AXP_VBUS_IPSOUT, |
| 123 | AXP_VBUS_IPSOUT_DRIVEBUS); |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 124 | else |
Chen-Yu Tsai | 81a8aa3 | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 125 | return pmic_bus_clrbits(AXP_VBUS_IPSOUT, |
| 126 | AXP_VBUS_IPSOUT_DRIVEBUS); |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 127 | #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 Goede | f9b7a04 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 138 | static 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 | |
| 145 | static 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 | |
| 156 | U_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 Goede | f9b7a04 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 162 | |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 163 | int axp_gpio_init(void) |
| 164 | { |
Hans de Goede | 421b32b | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 165 | struct udevice *dev; |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 166 | int ret; |
| 167 | |
| 168 | ret = pmic_bus_init(); |
| 169 | if (ret) |
| 170 | return ret; |
| 171 | |
Hans de Goede | f9b7a04 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 172 | /* 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 Goede | f9b7a04 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 176 | |
Hans de Goede | 2fcf033 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 177 | return 0; |
| 178 | } |