Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Qualcomm GPIO driver |
| 4 | * |
| 5 | * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com> |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 12 | #include <asm/gpio.h> |
| 13 | #include <asm/io.h> |
Caleb Connolly | 2c2cc3e | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 14 | #include <mach/gpio.h> |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 18 | /* OE */ |
| 19 | #define GPIO_OE_DISABLE (0x0 << 9) |
| 20 | #define GPIO_OE_ENABLE (0x1 << 9) |
| 21 | #define GPIO_OE_MASK (0x1 << 9) |
| 22 | |
| 23 | /* GPIO_IN_OUT register shifts. */ |
| 24 | #define GPIO_IN 0 |
| 25 | #define GPIO_OUT 1 |
| 26 | |
| 27 | struct msm_gpio_bank { |
| 28 | phys_addr_t base; |
Caleb Connolly | 2c2cc3e | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 29 | const struct msm_pin_data *pin_data; |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 30 | }; |
| 31 | |
Caleb Connolly | 2c2cc3e | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 32 | #define GPIO_CONFIG_REG(dev, x) \ |
| 33 | (qcom_pin_offset(((struct msm_gpio_bank *)dev_get_priv(dev))->pin_data->pin_offsets, x)) |
| 34 | |
| 35 | #define GPIO_IN_OUT_REG(dev, x) \ |
| 36 | (GPIO_CONFIG_REG(dev, x) + 0x4) |
| 37 | |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 38 | static int msm_gpio_direction_input(struct udevice *dev, unsigned int gpio) |
| 39 | { |
| 40 | struct msm_gpio_bank *priv = dev_get_priv(dev); |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 41 | |
Caleb Connolly | a245aec | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 42 | /* Always NOP for special pins, assume they're in the correct state */ |
| 43 | if (qcom_is_special_pin(priv->pin_data, gpio)) |
| 44 | return 0; |
| 45 | |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 46 | /* Disable OE bit */ |
Caleb Connolly | 2c2cc3e | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 47 | clrsetbits_le32(priv->base + GPIO_CONFIG_REG(dev, gpio), |
| 48 | GPIO_OE_MASK, GPIO_OE_DISABLE); |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
Caleb Connolly | 6156e39 | 2023-11-14 12:55:43 +0000 | [diff] [blame] | 53 | static int msm_gpio_set_value(struct udevice *dev, unsigned int gpio, int value) |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 54 | { |
| 55 | struct msm_gpio_bank *priv = dev_get_priv(dev); |
| 56 | |
Caleb Connolly | a245aec | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 57 | /* Always NOP for special pins, assume they're in the correct state */ |
| 58 | if (qcom_is_special_pin(priv->pin_data, gpio)) |
| 59 | return 0; |
| 60 | |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 61 | value = !!value; |
| 62 | /* set value */ |
Caleb Connolly | 2c2cc3e | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 63 | writel(value << GPIO_OUT, priv->base + GPIO_IN_OUT_REG(dev, gpio)); |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Caleb Connolly | 6156e39 | 2023-11-14 12:55:43 +0000 | [diff] [blame] | 68 | static int msm_gpio_direction_output(struct udevice *dev, unsigned int gpio, |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 69 | int value) |
| 70 | { |
| 71 | struct msm_gpio_bank *priv = dev_get_priv(dev); |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 72 | |
Caleb Connolly | a245aec | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 73 | /* Always NOP for special pins, assume they're in the correct state */ |
| 74 | if (qcom_is_special_pin(priv->pin_data, gpio)) |
| 75 | return 0; |
| 76 | |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 77 | value = !!value; |
| 78 | /* set value */ |
Caleb Connolly | 2c2cc3e | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 79 | writel(value << GPIO_OUT, priv->base + GPIO_IN_OUT_REG(dev, gpio)); |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 80 | /* switch direction */ |
Caleb Connolly | 2c2cc3e | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 81 | clrsetbits_le32(priv->base + GPIO_CONFIG_REG(dev, gpio), |
| 82 | GPIO_OE_MASK, GPIO_OE_ENABLE); |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Caleb Connolly | 6156e39 | 2023-11-14 12:55:43 +0000 | [diff] [blame] | 87 | static int msm_gpio_get_value(struct udevice *dev, unsigned int gpio) |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 88 | { |
| 89 | struct msm_gpio_bank *priv = dev_get_priv(dev); |
| 90 | |
Caleb Connolly | a245aec | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 91 | /* Always NOP for special pins, assume they're in the correct state */ |
| 92 | if (qcom_is_special_pin(priv->pin_data, gpio)) |
| 93 | return 0; |
| 94 | |
Caleb Connolly | 2c2cc3e | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 95 | return !!(readl(priv->base + GPIO_IN_OUT_REG(dev, gpio)) >> GPIO_IN); |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Caleb Connolly | 6156e39 | 2023-11-14 12:55:43 +0000 | [diff] [blame] | 98 | static int msm_gpio_get_function(struct udevice *dev, unsigned int gpio) |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 99 | { |
| 100 | struct msm_gpio_bank *priv = dev_get_priv(dev); |
| 101 | |
Caleb Connolly | a245aec | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 102 | /* Always NOP for special pins, assume they're in the correct state */ |
| 103 | if (qcom_is_special_pin(priv->pin_data, gpio)) |
| 104 | return 0; |
| 105 | |
Caleb Connolly | 2c2cc3e | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 106 | if (readl(priv->base + GPIO_CONFIG_REG(dev, gpio)) & GPIO_OE_ENABLE) |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 107 | return GPIOF_OUTPUT; |
| 108 | |
| 109 | return GPIOF_INPUT; |
| 110 | } |
| 111 | |
| 112 | static const struct dm_gpio_ops gpio_msm_ops = { |
| 113 | .direction_input = msm_gpio_direction_input, |
| 114 | .direction_output = msm_gpio_direction_output, |
| 115 | .get_value = msm_gpio_get_value, |
| 116 | .set_value = msm_gpio_set_value, |
| 117 | .get_function = msm_gpio_get_function, |
| 118 | }; |
| 119 | |
| 120 | static int msm_gpio_probe(struct udevice *dev) |
| 121 | { |
| 122 | struct msm_gpio_bank *priv = dev_get_priv(dev); |
| 123 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 124 | priv->base = dev_read_addr(dev); |
Caleb Connolly | 2c2cc3e | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 125 | priv->pin_data = (struct msm_pin_data *)dev_get_driver_data(dev); |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 126 | |
| 127 | return priv->base == FDT_ADDR_T_NONE ? -EINVAL : 0; |
| 128 | } |
| 129 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 130 | static int msm_gpio_of_to_plat(struct udevice *dev) |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 131 | { |
| 132 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Caleb Connolly | 2c2cc3e | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 133 | const struct msm_pin_data *pin_data = (struct msm_pin_data *)dev_get_driver_data(dev); |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 134 | |
Caleb Connolly | 2c2cc3e | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 135 | /* Get the pin count from the pinctrl driver */ |
| 136 | uc_priv->gpio_count = pin_data->pin_count; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 137 | uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 138 | "gpio-bank-name", NULL); |
| 139 | if (uc_priv->bank_name == NULL) |
| 140 | uc_priv->bank_name = "soc"; |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 145 | U_BOOT_DRIVER(gpio_msm) = { |
| 146 | .name = "gpio_msm", |
| 147 | .id = UCLASS_GPIO, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 148 | .of_to_plat = msm_gpio_of_to_plat, |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 149 | .probe = msm_gpio_probe, |
| 150 | .ops = &gpio_msm_ops, |
Sumit Garg | 0ddabb6 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 151 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 152 | .priv_auto = sizeof(struct msm_gpio_bank), |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 153 | }; |