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