Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Hans de Goede | 3ae1d13 | 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 | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 9 | #include <asm/arch/pmic_bus.h> |
Hans de Goede | 08607d1 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 10 | #include <asm/gpio.h> |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 11 | #include <axp_pmic.h> |
Hans de Goede | 08607d1 | 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 | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 16 | #include <errno.h> |
| 17 | |
Hans de Goede | 66bf508 | 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 | 3ae1d13 | 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 | 66bf508 | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 35 | static int axp_gpio_direction_input(struct udevice *dev, unsigned pin) |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 36 | { |
| 37 | u8 reg; |
| 38 | |
Samuel Holland | 5f9c844 | 2023-01-22 17:46:22 -0600 | [diff] [blame] | 39 | reg = axp_get_gpio_ctrl_reg(pin); |
| 40 | if (reg == 0) |
| 41 | return -EINVAL; |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 42 | |
Samuel Holland | 5f9c844 | 2023-01-22 17:46:22 -0600 | [diff] [blame] | 43 | return pmic_bus_write(reg, AXP_GPIO_CTRL_INPUT); |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 44 | } |
| 45 | |
Hans de Goede | 66bf508 | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 46 | static int axp_gpio_direction_output(struct udevice *dev, unsigned pin, |
| 47 | int val) |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 48 | { |
| 49 | __maybe_unused int ret; |
| 50 | u8 reg; |
| 51 | |
| 52 | switch (pin) { |
Chen-Yu Tsai | 2e8dced | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 53 | #ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC |
| 54 | /* Only available on later PMICs */ |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 55 | case SUNXI_GPIO_AXP0_VBUS_ENABLE: |
Chen-Yu Tsai | 2e8dced | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 56 | ret = pmic_bus_clrbits(AXP_MISC_CTRL, |
| 57 | AXP_MISC_CTRL_N_VBUSEN_FUNC); |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 58 | if (ret) |
| 59 | return ret; |
| 60 | |
| 61 | return axp_gpio_set_value(dev, pin, val); |
| 62 | #endif |
| 63 | default: |
| 64 | reg = axp_get_gpio_ctrl_reg(pin); |
| 65 | if (reg == 0) |
| 66 | return -EINVAL; |
| 67 | |
| 68 | return pmic_bus_write(reg, val ? AXP_GPIO_CTRL_OUTPUT_HIGH : |
| 69 | AXP_GPIO_CTRL_OUTPUT_LOW); |
| 70 | } |
| 71 | } |
| 72 | |
Hans de Goede | 66bf508 | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 73 | static int axp_gpio_get_value(struct udevice *dev, unsigned pin) |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 74 | { |
| 75 | u8 reg, val, mask; |
| 76 | int ret; |
| 77 | |
| 78 | switch (pin) { |
Chen-Yu Tsai | 2e8dced | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 79 | #ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC |
| 80 | /* Only available on later PMICs */ |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 81 | case SUNXI_GPIO_AXP0_VBUS_ENABLE: |
Chen-Yu Tsai | 2e8dced | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 82 | ret = pmic_bus_read(AXP_VBUS_IPSOUT, &val); |
| 83 | mask = AXP_VBUS_IPSOUT_DRIVEBUS; |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 84 | break; |
| 85 | #endif |
| 86 | default: |
| 87 | reg = axp_get_gpio_ctrl_reg(pin); |
| 88 | if (reg == 0) |
| 89 | return -EINVAL; |
| 90 | |
| 91 | ret = pmic_bus_read(AXP_GPIO_STATE, &val); |
| 92 | mask = 1 << (pin + AXP_GPIO_STATE_OFFSET); |
| 93 | } |
| 94 | if (ret) |
| 95 | return ret; |
| 96 | |
| 97 | return (val & mask) ? 1 : 0; |
| 98 | } |
| 99 | |
Hans de Goede | 66bf508 | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 100 | static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val) |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 101 | { |
| 102 | u8 reg; |
| 103 | |
| 104 | switch (pin) { |
Chen-Yu Tsai | 2e8dced | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 105 | #ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC |
| 106 | /* Only available on later PMICs */ |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 107 | case SUNXI_GPIO_AXP0_VBUS_ENABLE: |
| 108 | if (val) |
Chen-Yu Tsai | 2e8dced | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 109 | return pmic_bus_setbits(AXP_VBUS_IPSOUT, |
| 110 | AXP_VBUS_IPSOUT_DRIVEBUS); |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 111 | else |
Chen-Yu Tsai | 2e8dced | 2016-03-30 00:26:56 +0800 | [diff] [blame] | 112 | return pmic_bus_clrbits(AXP_VBUS_IPSOUT, |
| 113 | AXP_VBUS_IPSOUT_DRIVEBUS); |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 114 | #endif |
| 115 | default: |
| 116 | reg = axp_get_gpio_ctrl_reg(pin); |
| 117 | if (reg == 0) |
| 118 | return -EINVAL; |
| 119 | |
| 120 | return pmic_bus_write(reg, val ? AXP_GPIO_CTRL_OUTPUT_HIGH : |
| 121 | AXP_GPIO_CTRL_OUTPUT_LOW); |
| 122 | } |
| 123 | } |
| 124 | |
Hans de Goede | 08607d1 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 125 | static const struct dm_gpio_ops gpio_axp_ops = { |
| 126 | .direction_input = axp_gpio_direction_input, |
| 127 | .direction_output = axp_gpio_direction_output, |
| 128 | .get_value = axp_gpio_get_value, |
| 129 | .set_value = axp_gpio_set_value, |
| 130 | }; |
| 131 | |
| 132 | static int gpio_axp_probe(struct udevice *dev) |
| 133 | { |
| 134 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 135 | |
| 136 | /* Tell the uclass how many GPIOs we have */ |
| 137 | uc_priv->bank_name = strdup(SUNXI_GPIO_AXP0_PREFIX); |
| 138 | uc_priv->gpio_count = SUNXI_GPIO_AXP0_GPIO_COUNT; |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | U_BOOT_DRIVER(gpio_axp) = { |
| 144 | .name = "gpio_axp", |
| 145 | .id = UCLASS_GPIO, |
| 146 | .ops = &gpio_axp_ops, |
| 147 | .probe = gpio_axp_probe, |
| 148 | }; |
Hans de Goede | 08607d1 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 149 | |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 150 | int axp_gpio_init(void) |
| 151 | { |
Hans de Goede | 66bf508 | 2015-04-26 11:19:37 +0200 | [diff] [blame] | 152 | struct udevice *dev; |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 153 | int ret; |
| 154 | |
| 155 | ret = pmic_bus_init(); |
| 156 | if (ret) |
| 157 | return ret; |
Hans de Goede | 08607d1 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 158 | |
Hans de Goede | 08607d1 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 159 | /* There is no devicetree support for the axp yet, so bind directly */ |
| 160 | ret = device_bind_driver(dm_root(), "gpio_axp", "AXP-gpio", &dev); |
| 161 | if (ret) |
| 162 | return ret; |
Hans de Goede | 3ae1d13 | 2015-04-25 17:25:14 +0200 | [diff] [blame] | 163 | |
| 164 | return 0; |
| 165 | } |