Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 2 | /* |
Patrice Chotard | 789ee0e | 2017-10-23 09:53:58 +0200 | [diff] [blame] | 3 | * Copyright (C) 2017, STMicroelectronics - All Rights Reserved |
| 4 | * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics. |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Patrick Delaunay | 09e325e | 2020-11-06 19:01:33 +0100 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_GPIO |
| 8 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 9 | #include <clk.h> |
| 10 | #include <dm.h> |
| 11 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 13 | #include <asm/arch/stm32.h> |
| 14 | #include <asm/gpio.h> |
| 15 | #include <asm/io.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 18 | #include <linux/errno.h> |
| 19 | #include <linux/io.h> |
| 20 | |
Patrick Delaunay | 7dccd89 | 2021-10-22 20:12:34 +0200 | [diff] [blame] | 21 | #include "stm32_gpio_priv.h" |
| 22 | |
Patrick Delaunay | cf6da85 | 2020-10-02 14:08:54 +0200 | [diff] [blame] | 23 | #define STM32_GPIOS_PER_BANK 16 |
| 24 | |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 25 | #define MODE_BITS(gpio_pin) ((gpio_pin) * 2) |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 26 | #define MODE_BITS_MASK 3 |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 27 | #define BSRR_BIT(gpio_pin, value) BIT((gpio_pin) + (value ? 0 : 16)) |
| 28 | |
| 29 | #define PUPD_BITS(gpio_pin) ((gpio_pin) * 2) |
| 30 | #define PUPD_MASK 3 |
| 31 | |
| 32 | #define OTYPE_BITS(gpio_pin) (gpio_pin) |
| 33 | #define OTYPE_MSK 1 |
| 34 | |
| 35 | static void stm32_gpio_set_moder(struct stm32_gpio_regs *regs, |
| 36 | int idx, |
| 37 | int mode) |
| 38 | { |
| 39 | int bits_index; |
| 40 | int mask; |
| 41 | |
| 42 | bits_index = MODE_BITS(idx); |
| 43 | mask = MODE_BITS_MASK << bits_index; |
| 44 | |
| 45 | clrsetbits_le32(®s->moder, mask, mode << bits_index); |
| 46 | } |
| 47 | |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 48 | static int stm32_gpio_get_moder(struct stm32_gpio_regs *regs, int idx) |
| 49 | { |
| 50 | return (readl(®s->moder) >> MODE_BITS(idx)) & MODE_BITS_MASK; |
| 51 | } |
| 52 | |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 53 | static void stm32_gpio_set_otype(struct stm32_gpio_regs *regs, |
| 54 | int idx, |
| 55 | enum stm32_gpio_otype otype) |
| 56 | { |
| 57 | int bits; |
| 58 | |
| 59 | bits = OTYPE_BITS(idx); |
| 60 | clrsetbits_le32(®s->otyper, OTYPE_MSK << bits, otype << bits); |
| 61 | } |
| 62 | |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 63 | static enum stm32_gpio_otype stm32_gpio_get_otype(struct stm32_gpio_regs *regs, |
| 64 | int idx) |
| 65 | { |
| 66 | return (readl(®s->otyper) >> OTYPE_BITS(idx)) & OTYPE_MSK; |
| 67 | } |
| 68 | |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 69 | static void stm32_gpio_set_pupd(struct stm32_gpio_regs *regs, |
| 70 | int idx, |
| 71 | enum stm32_gpio_pupd pupd) |
| 72 | { |
| 73 | int bits; |
| 74 | |
| 75 | bits = PUPD_BITS(idx); |
| 76 | clrsetbits_le32(®s->pupdr, PUPD_MASK << bits, pupd << bits); |
| 77 | } |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 78 | |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 79 | static enum stm32_gpio_pupd stm32_gpio_get_pupd(struct stm32_gpio_regs *regs, |
| 80 | int idx) |
| 81 | { |
| 82 | return (readl(®s->pupdr) >> PUPD_BITS(idx)) & PUPD_MASK; |
| 83 | } |
| 84 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 85 | static bool stm32_gpio_is_mapped(struct udevice *dev, int offset) |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 86 | { |
| 87 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 88 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 89 | return !!(priv->gpio_range & BIT(offset)); |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 92 | static int stm32_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 93 | { |
| 94 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 95 | struct stm32_gpio_regs *regs = priv->regs; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 96 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 97 | if (!stm32_gpio_is_mapped(dev, offset)) |
| 98 | return -ENXIO; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 99 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 100 | stm32_gpio_set_moder(regs, offset, STM32_GPIO_MODE_IN); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int stm32_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 106 | int value) |
| 107 | { |
| 108 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 109 | struct stm32_gpio_regs *regs = priv->regs; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 110 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 111 | if (!stm32_gpio_is_mapped(dev, offset)) |
| 112 | return -ENXIO; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 113 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 114 | stm32_gpio_set_moder(regs, offset, STM32_GPIO_MODE_OUT); |
Patrice Chotard | 4e91500 | 2018-08-09 11:57:57 +0200 | [diff] [blame] | 115 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 116 | writel(BSRR_BIT(offset, value), ®s->bsrr); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int stm32_gpio_get_value(struct udevice *dev, unsigned offset) |
| 122 | { |
| 123 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 124 | struct stm32_gpio_regs *regs = priv->regs; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 125 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 126 | if (!stm32_gpio_is_mapped(dev, offset)) |
| 127 | return -ENXIO; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 128 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 129 | return readl(®s->idr) & BIT(offset) ? 1 : 0; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static int stm32_gpio_set_value(struct udevice *dev, unsigned offset, int value) |
| 133 | { |
| 134 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 135 | struct stm32_gpio_regs *regs = priv->regs; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 136 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 137 | if (!stm32_gpio_is_mapped(dev, offset)) |
| 138 | return -ENXIO; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 139 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 140 | writel(BSRR_BIT(offset, value), ®s->bsrr); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
Patrice Chotard | 1056123 | 2018-10-24 14:10:21 +0200 | [diff] [blame] | 145 | static int stm32_gpio_get_function(struct udevice *dev, unsigned int offset) |
| 146 | { |
| 147 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 148 | struct stm32_gpio_regs *regs = priv->regs; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 149 | int bits_index; |
| 150 | int mask; |
Patrice Chotard | 1056123 | 2018-10-24 14:10:21 +0200 | [diff] [blame] | 151 | u32 mode; |
| 152 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 153 | if (!stm32_gpio_is_mapped(dev, offset)) |
| 154 | return GPIOF_UNKNOWN; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 155 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 156 | bits_index = MODE_BITS(offset); |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 157 | mask = MODE_BITS_MASK << bits_index; |
| 158 | |
Patrice Chotard | 1056123 | 2018-10-24 14:10:21 +0200 | [diff] [blame] | 159 | mode = (readl(®s->moder) & mask) >> bits_index; |
| 160 | if (mode == STM32_GPIO_MODE_OUT) |
| 161 | return GPIOF_OUTPUT; |
| 162 | if (mode == STM32_GPIO_MODE_IN) |
| 163 | return GPIOF_INPUT; |
| 164 | if (mode == STM32_GPIO_MODE_AN) |
| 165 | return GPIOF_UNUSED; |
| 166 | |
| 167 | return GPIOF_FUNC; |
| 168 | } |
| 169 | |
Simon Glass | 54befdd | 2021-02-04 21:21:55 -0700 | [diff] [blame] | 170 | static int stm32_gpio_set_flags(struct udevice *dev, unsigned int offset, |
| 171 | ulong flags) |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 172 | { |
| 173 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 174 | struct stm32_gpio_regs *regs = priv->regs; |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 175 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 176 | if (!stm32_gpio_is_mapped(dev, offset)) |
| 177 | return -ENXIO; |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 178 | |
| 179 | if (flags & GPIOD_IS_OUT) { |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 180 | bool value = flags & GPIOD_IS_OUT_ACTIVE; |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 181 | |
| 182 | if (flags & GPIOD_OPEN_DRAIN) |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 183 | stm32_gpio_set_otype(regs, offset, STM32_GPIO_OTYPE_OD); |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 184 | else |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 185 | stm32_gpio_set_otype(regs, offset, STM32_GPIO_OTYPE_PP); |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 186 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 187 | stm32_gpio_set_moder(regs, offset, STM32_GPIO_MODE_OUT); |
| 188 | writel(BSRR_BIT(offset, value), ®s->bsrr); |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 189 | |
| 190 | } else if (flags & GPIOD_IS_IN) { |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 191 | stm32_gpio_set_moder(regs, offset, STM32_GPIO_MODE_IN); |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 192 | } |
Patrick Delaunay | f60bc02 | 2020-10-28 10:49:08 +0100 | [diff] [blame] | 193 | if (flags & GPIOD_PULL_UP) |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 194 | stm32_gpio_set_pupd(regs, offset, STM32_GPIO_PUPD_UP); |
Patrick Delaunay | f60bc02 | 2020-10-28 10:49:08 +0100 | [diff] [blame] | 195 | else if (flags & GPIOD_PULL_DOWN) |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 196 | stm32_gpio_set_pupd(regs, offset, STM32_GPIO_PUPD_DOWN); |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
Simon Glass | d063ce9 | 2021-02-04 21:21:56 -0700 | [diff] [blame] | 201 | static int stm32_gpio_get_flags(struct udevice *dev, unsigned int offset, |
| 202 | ulong *flagsp) |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 203 | { |
| 204 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 205 | struct stm32_gpio_regs *regs = priv->regs; |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 206 | ulong dir_flags = 0; |
| 207 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 208 | if (!stm32_gpio_is_mapped(dev, offset)) |
| 209 | return -ENXIO; |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 210 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 211 | switch (stm32_gpio_get_moder(regs, offset)) { |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 212 | case STM32_GPIO_MODE_OUT: |
| 213 | dir_flags |= GPIOD_IS_OUT; |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 214 | if (stm32_gpio_get_otype(regs, offset) == STM32_GPIO_OTYPE_OD) |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 215 | dir_flags |= GPIOD_OPEN_DRAIN; |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 216 | if (readl(®s->idr) & BIT(offset)) |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 217 | dir_flags |= GPIOD_IS_OUT_ACTIVE; |
| 218 | break; |
| 219 | case STM32_GPIO_MODE_IN: |
| 220 | dir_flags |= GPIOD_IS_IN; |
Patrick Delaunay | f60bc02 | 2020-10-28 10:49:08 +0100 | [diff] [blame] | 221 | break; |
| 222 | default: |
| 223 | break; |
| 224 | } |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 225 | switch (stm32_gpio_get_pupd(regs, offset)) { |
Patrick Delaunay | f60bc02 | 2020-10-28 10:49:08 +0100 | [diff] [blame] | 226 | case STM32_GPIO_PUPD_UP: |
| 227 | dir_flags |= GPIOD_PULL_UP; |
| 228 | break; |
| 229 | case STM32_GPIO_PUPD_DOWN: |
| 230 | dir_flags |= GPIOD_PULL_DOWN; |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 231 | break; |
| 232 | default: |
| 233 | break; |
| 234 | } |
Simon Glass | d063ce9 | 2021-02-04 21:21:56 -0700 | [diff] [blame] | 235 | *flagsp = dir_flags; |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 240 | static const struct dm_gpio_ops gpio_stm32_ops = { |
| 241 | .direction_input = stm32_gpio_direction_input, |
| 242 | .direction_output = stm32_gpio_direction_output, |
| 243 | .get_value = stm32_gpio_get_value, |
| 244 | .set_value = stm32_gpio_set_value, |
Patrice Chotard | 1056123 | 2018-10-24 14:10:21 +0200 | [diff] [blame] | 245 | .get_function = stm32_gpio_get_function, |
Simon Glass | 54befdd | 2021-02-04 21:21:55 -0700 | [diff] [blame] | 246 | .set_flags = stm32_gpio_set_flags, |
Simon Glass | d063ce9 | 2021-02-04 21:21:56 -0700 | [diff] [blame] | 247 | .get_flags = stm32_gpio_get_flags, |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | static int gpio_stm32_probe(struct udevice *dev) |
| 251 | { |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 252 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
Patrick Delaunay | cd7c951 | 2020-09-09 18:28:33 +0200 | [diff] [blame] | 253 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 254 | struct ofnode_phandle_args args; |
| 255 | const char *name; |
Patrice Chotard | 159d157 | 2018-12-03 10:52:53 +0100 | [diff] [blame] | 256 | struct clk clk; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 257 | fdt_addr_t addr; |
Patrick Delaunay | cd7c951 | 2020-09-09 18:28:33 +0200 | [diff] [blame] | 258 | int ret, i; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 259 | |
Patrick Delaunay | d78f968 | 2018-03-12 10:46:07 +0100 | [diff] [blame] | 260 | addr = dev_read_addr(dev); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 261 | if (addr == FDT_ADDR_T_NONE) |
| 262 | return -EINVAL; |
| 263 | |
| 264 | priv->regs = (struct stm32_gpio_regs *)addr; |
Patrice Chotard | 9f62b08 | 2019-01-04 10:55:06 +0100 | [diff] [blame] | 265 | |
Patrick Delaunay | d78f968 | 2018-03-12 10:46:07 +0100 | [diff] [blame] | 266 | name = dev_read_string(dev, "st,bank-name"); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 267 | if (!name) |
| 268 | return -EINVAL; |
| 269 | uc_priv->bank_name = name; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 270 | |
| 271 | i = 0; |
| 272 | ret = dev_read_phandle_with_args(dev, "gpio-ranges", |
| 273 | NULL, 3, i, &args); |
| 274 | |
Patrick Delaunay | e7f6638 | 2020-09-09 18:28:34 +0200 | [diff] [blame] | 275 | if (!ret && args.args_count < 3) |
| 276 | return -EINVAL; |
| 277 | |
Patrice Chotard | 5554a54 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 278 | uc_priv->gpio_count = STM32_GPIOS_PER_BANK; |
| 279 | if (ret == -ENOENT) |
Patrice Chotard | 6225305 | 2019-01-04 10:55:05 +0100 | [diff] [blame] | 280 | priv->gpio_range = GENMASK(STM32_GPIOS_PER_BANK - 1, 0); |
Patrice Chotard | 6225305 | 2019-01-04 10:55:05 +0100 | [diff] [blame] | 281 | |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 282 | while (ret != -ENOENT) { |
| 283 | priv->gpio_range |= GENMASK(args.args[2] + args.args[0] - 1, |
| 284 | args.args[0]); |
| 285 | |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 286 | ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3, |
| 287 | ++i, &args); |
Patrick Delaunay | e7f6638 | 2020-09-09 18:28:34 +0200 | [diff] [blame] | 288 | if (!ret && args.args_count < 3) |
| 289 | return -EINVAL; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | dev_dbg(dev, "addr = 0x%p bank_name = %s gpio_count = %d gpio_range = 0x%x\n", |
| 293 | (u32 *)priv->regs, uc_priv->bank_name, uc_priv->gpio_count, |
| 294 | priv->gpio_range); |
Patrick Delaunay | b1c6014 | 2020-04-22 14:29:17 +0200 | [diff] [blame] | 295 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 296 | ret = clk_get_by_index(dev, 0, &clk); |
| 297 | if (ret < 0) |
| 298 | return ret; |
| 299 | |
| 300 | ret = clk_enable(&clk); |
| 301 | |
| 302 | if (ret) { |
| 303 | dev_err(dev, "failed to enable clock\n"); |
| 304 | return ret; |
| 305 | } |
Patrick Delaunay | 09e325e | 2020-11-06 19:01:33 +0100 | [diff] [blame] | 306 | dev_dbg(dev, "clock enabled\n"); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 311 | U_BOOT_DRIVER(gpio_stm32) = { |
| 312 | .name = "gpio_stm32", |
| 313 | .id = UCLASS_GPIO, |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 314 | .probe = gpio_stm32_probe, |
| 315 | .ops = &gpio_stm32_ops, |
Bin Meng | b508ee5 | 2018-10-24 06:36:30 -0700 | [diff] [blame] | 316 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 317 | .priv_auto = sizeof(struct stm32_gpio_priv), |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 318 | }; |