Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Atmel PIO pinctrl driver |
| 4 | * |
| 5 | * Copyright (C) 2016 Atmel Corporation |
| 6 | * Wenyou.Yang <wenyou.yang@atmel.com> |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Manikandan Muralidharan | 8f89f1b | 2025-02-10 12:21:41 +0530 | [diff] [blame] | 12 | #include <dm/device-internal.h> |
| 13 | #include <dm/lists.h> |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 14 | #include <dm/pinctrl.h> |
Wenyou Yang | d19b901 | 2017-09-14 11:07:42 +0800 | [diff] [blame] | 15 | #include <asm/hardware.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 17 | #include <linux/io.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <mach/at91_pio.h> |
| 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
| 23 | #define MAX_GPIO_BANKS 5 |
| 24 | #define MAX_NB_GPIO_PER_BANK 32 |
| 25 | |
| 26 | #define MAX_PINMUX_ENTRIES 200 |
| 27 | |
| 28 | struct at91_pinctrl_priv { |
| 29 | struct at91_port *reg_base[MAX_GPIO_BANKS]; |
| 30 | u32 nbanks; |
| 31 | }; |
| 32 | |
| 33 | #define PULL_UP BIT(0) |
| 34 | #define MULTI_DRIVE BIT(1) |
| 35 | #define DEGLITCH BIT(2) |
| 36 | #define PULL_DOWN BIT(3) |
| 37 | #define DIS_SCHMIT BIT(4) |
| 38 | #define DRIVE_STRENGTH_SHIFT 5 |
| 39 | #define DRIVE_STRENGTH_MASK 0x3 |
| 40 | #define DRIVE_STRENGTH (DRIVE_STRENGTH_MASK << DRIVE_STRENGTH_SHIFT) |
| 41 | #define OUTPUT BIT(7) |
| 42 | #define OUTPUT_VAL_SHIFT 8 |
| 43 | #define OUTPUT_VAL (0x1 << OUTPUT_VAL_SHIFT) |
Claudiu Beznea | 13c9c92 | 2019-03-25 10:34:00 +0000 | [diff] [blame] | 44 | #define SLEWRATE_SHIFT 9 |
| 45 | #define SLEWRATE_MASK 0x1 |
| 46 | #define SLEWRATE (SLEWRATE_MASK << SLEWRATE_SHIFT) |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 47 | #define DEBOUNCE BIT(16) |
| 48 | #define DEBOUNCE_VAL_SHIFT 17 |
| 49 | #define DEBOUNCE_VAL (0x3fff << DEBOUNCE_VAL_SHIFT) |
| 50 | |
| 51 | /** |
| 52 | * These defines will translated the dt binding settings to our internal |
| 53 | * settings. They are not necessarily the same value as the register setting. |
| 54 | * The actual drive strength current of low, medium and high must be looked up |
| 55 | * from the corresponding device datasheet. This value is different for pins |
| 56 | * that are even in the same banks. It is also dependent on VCC. |
| 57 | * DRIVE_STRENGTH_DEFAULT is just a placeholder to avoid changing the drive |
| 58 | * strength when there is no dt config for it. |
| 59 | */ |
Claudiu Beznea | ac3bfe9 | 2019-03-25 10:33:56 +0000 | [diff] [blame] | 60 | enum drive_strength_bit { |
| 61 | DRIVE_STRENGTH_BIT_DEF, |
| 62 | DRIVE_STRENGTH_BIT_LOW, |
| 63 | DRIVE_STRENGTH_BIT_MED, |
| 64 | DRIVE_STRENGTH_BIT_HI, |
| 65 | }; |
| 66 | |
| 67 | #define DRIVE_STRENGTH_BIT_MSK(name) (DRIVE_STRENGTH_BIT_##name << \ |
| 68 | DRIVE_STRENGTH_SHIFT) |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 69 | |
Claudiu Beznea | 13c9c92 | 2019-03-25 10:34:00 +0000 | [diff] [blame] | 70 | enum slewrate_bit { |
| 71 | SLEWRATE_BIT_DIS, |
| 72 | SLEWRATE_BIT_ENA, |
| 73 | }; |
| 74 | |
| 75 | #define SLEWRATE_BIT_MSK(name) (SLEWRATE_BIT_##name << SLEWRATE_SHIFT) |
| 76 | |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 77 | enum at91_mux { |
| 78 | AT91_MUX_GPIO = 0, |
| 79 | AT91_MUX_PERIPH_A = 1, |
| 80 | AT91_MUX_PERIPH_B = 2, |
| 81 | AT91_MUX_PERIPH_C = 3, |
| 82 | AT91_MUX_PERIPH_D = 4, |
| 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * struct at91_pinctrl_mux_ops - describes an AT91 mux ops group |
| 87 | * on new IP with support for periph C and D the way to mux in |
| 88 | * periph A and B has changed |
| 89 | * So provide the right callbacks |
| 90 | * if not present means the IP does not support it |
| 91 | * @mux_A_periph: assign the corresponding pin to the peripheral A function. |
| 92 | * @mux_B_periph: assign the corresponding pin to the peripheral B function. |
| 93 | * @mux_C_periph: assign the corresponding pin to the peripheral C function. |
| 94 | * @mux_D_periph: assign the corresponding pin to the peripheral D function. |
| 95 | * @set_deglitch: enable/disable the deglitch feature. |
| 96 | * @set_debounce: enable/disable the debounce feature. |
| 97 | * @set_pulldown: enable/disable the pulldown feature. |
| 98 | * @disable_schmitt_trig: disable schmitt trigger |
| 99 | */ |
| 100 | struct at91_pinctrl_mux_ops { |
| 101 | void (*mux_A_periph)(struct at91_port *pio, u32 mask); |
| 102 | void (*mux_B_periph)(struct at91_port *pio, u32 mask); |
| 103 | void (*mux_C_periph)(struct at91_port *pio, u32 mask); |
| 104 | void (*mux_D_periph)(struct at91_port *pio, u32 mask); |
| 105 | void (*set_deglitch)(struct at91_port *pio, u32 mask, bool is_on); |
| 106 | void (*set_debounce)(struct at91_port *pio, u32 mask, bool is_on, |
| 107 | u32 div); |
| 108 | void (*set_pulldown)(struct at91_port *pio, u32 mask, bool is_on); |
| 109 | void (*disable_schmitt_trig)(struct at91_port *pio, u32 mask); |
| 110 | void (*set_drivestrength)(struct at91_port *pio, u32 pin, |
| 111 | u32 strength); |
Claudiu Beznea | 13c9c92 | 2019-03-25 10:34:00 +0000 | [diff] [blame] | 112 | void (*set_slewrate)(struct at91_port *pio, u32 pin, u32 slewrate); |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | static u32 two_bit_pin_value_shift_amount(u32 pin) |
| 116 | { |
| 117 | /* return the shift value for a pin for "two bit" per pin registers, |
| 118 | * i.e. drive strength */ |
| 119 | return 2 * ((pin >= MAX_NB_GPIO_PER_BANK/2) |
| 120 | ? pin - MAX_NB_GPIO_PER_BANK/2 : pin); |
| 121 | } |
| 122 | |
| 123 | static void at91_mux_disable_interrupt(struct at91_port *pio, u32 mask) |
| 124 | { |
| 125 | writel(mask, &pio->idr); |
| 126 | } |
| 127 | |
| 128 | static void at91_mux_set_pullup(struct at91_port *pio, u32 mask, bool on) |
| 129 | { |
| 130 | if (on) |
| 131 | writel(mask, &pio->mux.pio3.ppddr); |
| 132 | |
| 133 | writel(mask, (on ? &pio->puer : &pio->pudr)); |
| 134 | } |
| 135 | |
| 136 | static void at91_mux_set_output(struct at91_port *pio, unsigned mask, |
| 137 | bool is_on, bool val) |
| 138 | { |
| 139 | writel(mask, (val ? &pio->sodr : &pio->codr)); |
| 140 | writel(mask, (is_on ? &pio->oer : &pio->odr)); |
| 141 | } |
| 142 | |
| 143 | static void at91_mux_set_multidrive(struct at91_port *pio, u32 mask, bool on) |
| 144 | { |
| 145 | writel(mask, (on ? &pio->mder : &pio->mddr)); |
| 146 | } |
| 147 | |
| 148 | static void at91_mux_set_A_periph(struct at91_port *pio, u32 mask) |
| 149 | { |
| 150 | writel(mask, &pio->mux.pio2.asr); |
| 151 | } |
| 152 | |
| 153 | static void at91_mux_set_B_periph(struct at91_port *pio, u32 mask) |
| 154 | { |
| 155 | writel(mask, &pio->mux.pio2.bsr); |
| 156 | } |
| 157 | |
| 158 | static void at91_mux_pio3_set_A_periph(struct at91_port *pio, u32 mask) |
| 159 | { |
| 160 | writel(readl(&pio->mux.pio3.abcdsr1) & ~mask, &pio->mux.pio3.abcdsr1); |
| 161 | writel(readl(&pio->mux.pio3.abcdsr2) & ~mask, &pio->mux.pio3.abcdsr2); |
| 162 | } |
| 163 | |
| 164 | static void at91_mux_pio3_set_B_periph(struct at91_port *pio, u32 mask) |
| 165 | { |
| 166 | writel(readl(&pio->mux.pio3.abcdsr1) | mask, &pio->mux.pio3.abcdsr1); |
| 167 | writel(readl(&pio->mux.pio3.abcdsr2) & ~mask, &pio->mux.pio3.abcdsr2); |
| 168 | } |
| 169 | |
| 170 | static void at91_mux_pio3_set_C_periph(struct at91_port *pio, u32 mask) |
| 171 | { |
| 172 | writel(readl(&pio->mux.pio3.abcdsr1) & ~mask, &pio->mux.pio3.abcdsr1); |
| 173 | writel(readl(&pio->mux.pio3.abcdsr2) | mask, &pio->mux.pio3.abcdsr2); |
| 174 | } |
| 175 | |
| 176 | static void at91_mux_pio3_set_D_periph(struct at91_port *pio, u32 mask) |
| 177 | { |
| 178 | writel(readl(&pio->mux.pio3.abcdsr1) | mask, &pio->mux.pio3.abcdsr1); |
| 179 | writel(readl(&pio->mux.pio3.abcdsr2) | mask, &pio->mux.pio3.abcdsr2); |
| 180 | } |
| 181 | |
| 182 | static void at91_mux_set_deglitch(struct at91_port *pio, u32 mask, bool is_on) |
| 183 | { |
| 184 | writel(mask, (is_on ? &pio->ifer : &pio->ifdr)); |
| 185 | } |
| 186 | |
| 187 | static void at91_mux_pio3_set_deglitch(struct at91_port *pio, |
| 188 | u32 mask, bool is_on) |
| 189 | { |
| 190 | if (is_on) |
| 191 | writel(mask, &pio->mux.pio3.ifscdr); |
| 192 | at91_mux_set_deglitch(pio, mask, is_on); |
| 193 | } |
| 194 | |
| 195 | static void at91_mux_pio3_set_debounce(struct at91_port *pio, u32 mask, |
| 196 | bool is_on, u32 div) |
| 197 | { |
| 198 | if (is_on) { |
| 199 | writel(mask, &pio->mux.pio3.ifscer); |
| 200 | writel(div & PIO_SCDR_DIV, &pio->mux.pio3.scdr); |
| 201 | writel(mask, &pio->ifer); |
| 202 | } else { |
| 203 | writel(mask, &pio->mux.pio3.ifscdr); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | static void at91_mux_pio3_set_pulldown(struct at91_port *pio, |
| 208 | u32 mask, bool is_on) |
| 209 | { |
| 210 | if (is_on) |
| 211 | writel(mask, &pio->pudr); |
| 212 | |
| 213 | writel(mask, (is_on ? &pio->mux.pio3.ppder : &pio->mux.pio3.ppddr)); |
| 214 | } |
| 215 | |
| 216 | static void at91_mux_pio3_disable_schmitt_trig(struct at91_port *pio, |
| 217 | u32 mask) |
| 218 | { |
| 219 | writel(readl(&pio->schmitt) | mask, &pio->schmitt); |
| 220 | } |
| 221 | |
| 222 | static void set_drive_strength(void *reg, u32 pin, u32 strength) |
| 223 | { |
| 224 | u32 shift = two_bit_pin_value_shift_amount(pin); |
| 225 | |
| 226 | clrsetbits_le32(reg, DRIVE_STRENGTH_MASK << shift, strength << shift); |
| 227 | } |
| 228 | |
| 229 | static void at91_mux_sama5d3_set_drivestrength(struct at91_port *pio, |
| 230 | u32 pin, u32 setting) |
| 231 | { |
| 232 | void *reg; |
| 233 | |
| 234 | reg = &pio->driver12; |
| 235 | if (pin >= MAX_NB_GPIO_PER_BANK / 2) |
| 236 | reg = &pio->driver2; |
| 237 | |
| 238 | /* do nothing if setting is zero */ |
| 239 | if (!setting) |
| 240 | return; |
| 241 | |
| 242 | /* strength is 1 to 1 with setting for SAMA5 */ |
| 243 | set_drive_strength(reg, pin, setting); |
| 244 | } |
| 245 | |
| 246 | static void at91_mux_sam9x5_set_drivestrength(struct at91_port *pio, |
| 247 | u32 pin, u32 setting) |
| 248 | { |
| 249 | void *reg; |
| 250 | |
| 251 | reg = &pio->driver1; |
| 252 | if (pin >= MAX_NB_GPIO_PER_BANK / 2) |
| 253 | reg = &pio->driver12; |
| 254 | |
| 255 | /* do nothing if setting is zero */ |
| 256 | if (!setting) |
| 257 | return; |
| 258 | |
| 259 | /* strength is inverse on SAM9x5s with our defines |
| 260 | * 0 = hi, 1 = med, 2 = low, 3 = rsvd */ |
Claudiu Beznea | ac3bfe9 | 2019-03-25 10:33:56 +0000 | [diff] [blame] | 261 | setting = DRIVE_STRENGTH_BIT_MSK(HI) - setting; |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 262 | |
| 263 | set_drive_strength(reg, pin, setting); |
| 264 | } |
| 265 | |
Claudiu Beznea | e42e6c6 | 2019-03-25 10:33:57 +0000 | [diff] [blame] | 266 | static void at91_mux_sam9x60_set_drivestrength(struct at91_port *pio, u32 pin, |
| 267 | u32 setting) |
| 268 | { |
| 269 | void *reg = &pio->driver12; |
| 270 | u32 tmp; |
| 271 | |
| 272 | if (setting <= DRIVE_STRENGTH_BIT_DEF || |
| 273 | setting == DRIVE_STRENGTH_BIT_MED || |
| 274 | setting > DRIVE_STRENGTH_BIT_HI) |
| 275 | return; |
| 276 | |
| 277 | tmp = readl(reg); |
| 278 | |
| 279 | /* Strength is 0: low, 1: hi */ |
| 280 | if (setting == DRIVE_STRENGTH_BIT_LOW) |
| 281 | tmp &= ~BIT(pin); |
| 282 | else |
| 283 | tmp |= BIT(pin); |
| 284 | |
| 285 | writel(tmp, reg); |
| 286 | } |
| 287 | |
Claudiu Beznea | 13c9c92 | 2019-03-25 10:34:00 +0000 | [diff] [blame] | 288 | static void at91_mux_sam9x60_set_slewrate(struct at91_port *pio, u32 pin, |
| 289 | u32 setting) |
| 290 | { |
| 291 | void *reg = &pio->reserved12[3]; |
| 292 | u32 tmp; |
| 293 | |
| 294 | if (setting < SLEWRATE_BIT_DIS || setting > SLEWRATE_BIT_ENA) |
| 295 | return; |
| 296 | |
| 297 | tmp = readl(reg); |
| 298 | |
| 299 | if (setting == SLEWRATE_BIT_DIS) |
| 300 | tmp &= ~BIT(pin); |
| 301 | else |
| 302 | tmp |= BIT(pin); |
| 303 | |
| 304 | writel(tmp, reg); |
| 305 | } |
| 306 | |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 307 | static struct at91_pinctrl_mux_ops at91rm9200_ops = { |
| 308 | .mux_A_periph = at91_mux_set_A_periph, |
| 309 | .mux_B_periph = at91_mux_set_B_periph, |
| 310 | .set_deglitch = at91_mux_set_deglitch, |
| 311 | }; |
| 312 | |
| 313 | static struct at91_pinctrl_mux_ops at91sam9x5_ops = { |
| 314 | .mux_A_periph = at91_mux_pio3_set_A_periph, |
| 315 | .mux_B_periph = at91_mux_pio3_set_B_periph, |
| 316 | .mux_C_periph = at91_mux_pio3_set_C_periph, |
| 317 | .mux_D_periph = at91_mux_pio3_set_D_periph, |
| 318 | .set_deglitch = at91_mux_pio3_set_deglitch, |
| 319 | .set_debounce = at91_mux_pio3_set_debounce, |
| 320 | .set_pulldown = at91_mux_pio3_set_pulldown, |
| 321 | .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig, |
| 322 | .set_drivestrength = at91_mux_sam9x5_set_drivestrength, |
| 323 | }; |
| 324 | |
| 325 | static struct at91_pinctrl_mux_ops sama5d3_ops = { |
| 326 | .mux_A_periph = at91_mux_pio3_set_A_periph, |
| 327 | .mux_B_periph = at91_mux_pio3_set_B_periph, |
| 328 | .mux_C_periph = at91_mux_pio3_set_C_periph, |
| 329 | .mux_D_periph = at91_mux_pio3_set_D_periph, |
| 330 | .set_deglitch = at91_mux_pio3_set_deglitch, |
| 331 | .set_debounce = at91_mux_pio3_set_debounce, |
| 332 | .set_pulldown = at91_mux_pio3_set_pulldown, |
| 333 | .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig, |
| 334 | .set_drivestrength = at91_mux_sama5d3_set_drivestrength, |
| 335 | }; |
| 336 | |
Claudiu Beznea | e42e6c6 | 2019-03-25 10:33:57 +0000 | [diff] [blame] | 337 | static struct at91_pinctrl_mux_ops sam9x60_ops = { |
| 338 | .mux_A_periph = at91_mux_pio3_set_A_periph, |
| 339 | .mux_B_periph = at91_mux_pio3_set_B_periph, |
| 340 | .mux_C_periph = at91_mux_pio3_set_C_periph, |
| 341 | .mux_D_periph = at91_mux_pio3_set_D_periph, |
| 342 | .set_deglitch = at91_mux_pio3_set_deglitch, |
| 343 | .set_debounce = at91_mux_pio3_set_debounce, |
| 344 | .set_pulldown = at91_mux_pio3_set_pulldown, |
| 345 | .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig, |
| 346 | .set_drivestrength = at91_mux_sam9x60_set_drivestrength, |
Claudiu Beznea | 13c9c92 | 2019-03-25 10:34:00 +0000 | [diff] [blame] | 347 | .set_slewrate = at91_mux_sam9x60_set_slewrate, |
Claudiu Beznea | e42e6c6 | 2019-03-25 10:33:57 +0000 | [diff] [blame] | 348 | }; |
| 349 | |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 350 | static void at91_mux_gpio_disable(struct at91_port *pio, u32 mask) |
| 351 | { |
| 352 | writel(mask, &pio->pdr); |
| 353 | } |
| 354 | |
| 355 | static void at91_mux_gpio_enable(struct at91_port *pio, u32 mask, bool input) |
| 356 | { |
| 357 | writel(mask, &pio->per); |
| 358 | writel(mask, (input ? &pio->odr : &pio->oer)); |
| 359 | } |
| 360 | |
| 361 | static int at91_pmx_set(struct at91_pinctrl_mux_ops *ops, |
| 362 | struct at91_port *pio, u32 mask, enum at91_mux mux) |
| 363 | { |
| 364 | at91_mux_disable_interrupt(pio, mask); |
| 365 | switch (mux) { |
| 366 | case AT91_MUX_GPIO: |
| 367 | at91_mux_gpio_enable(pio, mask, 1); |
| 368 | break; |
| 369 | case AT91_MUX_PERIPH_A: |
| 370 | ops->mux_A_periph(pio, mask); |
| 371 | break; |
| 372 | case AT91_MUX_PERIPH_B: |
| 373 | ops->mux_B_periph(pio, mask); |
| 374 | break; |
| 375 | case AT91_MUX_PERIPH_C: |
| 376 | if (!ops->mux_C_periph) |
| 377 | return -EINVAL; |
| 378 | ops->mux_C_periph(pio, mask); |
| 379 | break; |
| 380 | case AT91_MUX_PERIPH_D: |
| 381 | if (!ops->mux_D_periph) |
| 382 | return -EINVAL; |
| 383 | ops->mux_D_periph(pio, mask); |
| 384 | break; |
| 385 | } |
| 386 | if (mux) |
| 387 | at91_mux_gpio_disable(pio, mask); |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static int at91_pinconf_set(struct at91_pinctrl_mux_ops *ops, |
| 393 | struct at91_port *pio, u32 pin, u32 config) |
| 394 | { |
| 395 | u32 mask = BIT(pin); |
| 396 | |
| 397 | if ((config & PULL_UP) && (config & PULL_DOWN)) |
| 398 | return -EINVAL; |
| 399 | |
| 400 | at91_mux_set_output(pio, mask, config & OUTPUT, |
| 401 | (config & OUTPUT_VAL) >> OUTPUT_VAL_SHIFT); |
| 402 | at91_mux_set_pullup(pio, mask, config & PULL_UP); |
| 403 | at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE); |
| 404 | if (ops->set_deglitch) |
| 405 | ops->set_deglitch(pio, mask, config & DEGLITCH); |
| 406 | if (ops->set_debounce) |
| 407 | ops->set_debounce(pio, mask, config & DEBOUNCE, |
| 408 | (config & DEBOUNCE_VAL) >> DEBOUNCE_VAL_SHIFT); |
| 409 | if (ops->set_pulldown) |
| 410 | ops->set_pulldown(pio, mask, config & PULL_DOWN); |
| 411 | if (ops->disable_schmitt_trig && config & DIS_SCHMIT) |
| 412 | ops->disable_schmitt_trig(pio, mask); |
| 413 | if (ops->set_drivestrength) |
| 414 | ops->set_drivestrength(pio, pin, |
| 415 | (config & DRIVE_STRENGTH) >> DRIVE_STRENGTH_SHIFT); |
Claudiu Beznea | 13c9c92 | 2019-03-25 10:34:00 +0000 | [diff] [blame] | 416 | if (ops->set_slewrate) |
| 417 | ops->set_slewrate(pio, pin, |
| 418 | (config & SLEWRATE) >> SLEWRATE_SHIFT); |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static int at91_pin_check_config(struct udevice *dev, u32 bank, u32 pin) |
| 424 | { |
| 425 | struct at91_pinctrl_priv *priv = dev_get_priv(dev); |
| 426 | |
| 427 | if (bank >= priv->nbanks) { |
| 428 | debug("pin conf bank %d >= nbanks %d\n", bank, priv->nbanks); |
| 429 | return -EINVAL; |
| 430 | } |
| 431 | |
| 432 | if (pin >= MAX_NB_GPIO_PER_BANK) { |
| 433 | debug("pin conf pin %d >= %d\n", pin, MAX_NB_GPIO_PER_BANK); |
| 434 | return -EINVAL; |
| 435 | } |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | static int at91_pinctrl_set_state(struct udevice *dev, struct udevice *config) |
| 441 | { |
| 442 | struct at91_pinctrl_priv *priv = dev_get_priv(dev); |
| 443 | const void *blob = gd->fdt_blob; |
Simon Glass | 7a49443 | 2017-05-17 17:18:09 -0600 | [diff] [blame] | 444 | int node = dev_of_offset(config); |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 445 | u32 cells[MAX_PINMUX_ENTRIES]; |
| 446 | const u32 *list = cells; |
| 447 | u32 bank, pin; |
| 448 | u32 conf, mask, count, i; |
| 449 | int size; |
| 450 | int ret; |
| 451 | enum at91_mux mux; |
| 452 | struct at91_port *pio; |
| 453 | struct at91_pinctrl_mux_ops *ops = |
| 454 | (struct at91_pinctrl_mux_ops *)dev_get_driver_data(dev); |
| 455 | |
| 456 | /* |
| 457 | * the binding format is atmel,pins = <bank pin mux CONFIG ...>, |
| 458 | * do sanity check and calculate pins number |
| 459 | */ |
| 460 | size = fdtdec_get_int_array_count(blob, node, "atmel,pins", |
| 461 | cells, ARRAY_SIZE(cells)); |
| 462 | |
| 463 | /* we do not check return since it's safe node passed down */ |
| 464 | count = size >> 2; |
| 465 | if (!count) |
| 466 | return -EINVAL; |
| 467 | |
| 468 | for (i = 0; i < count; i++) { |
| 469 | bank = *list++; |
| 470 | pin = *list++; |
| 471 | mux = *list++; |
| 472 | conf = *list++; |
| 473 | |
| 474 | ret = at91_pin_check_config(dev, bank, pin); |
| 475 | if (ret) |
| 476 | return ret; |
| 477 | |
| 478 | pio = priv->reg_base[bank]; |
| 479 | mask = BIT(pin); |
| 480 | |
| 481 | ret = at91_pmx_set(ops, pio, mask, mux); |
| 482 | if (ret) |
| 483 | return ret; |
| 484 | |
| 485 | ret = at91_pinconf_set(ops, pio, pin, conf); |
| 486 | if (ret) |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | const struct pinctrl_ops at91_pinctrl_ops = { |
| 494 | .set_state = at91_pinctrl_set_state, |
| 495 | }; |
| 496 | |
Manikandan Muralidharan | 8f89f1b | 2025-02-10 12:21:41 +0530 | [diff] [blame] | 497 | /** |
| 498 | * at91_pinctrl_bind() - Iterates through all subnodes of the pinctrl device |
| 499 | * in the DT and binds them to U-Boot's device model. Each subnode |
| 500 | * typically represents a GPIO controller or pin configuration data. |
| 501 | * |
| 502 | * @dev: Pointer to the pinctrl device |
| 503 | * |
| 504 | * Returns 0 on success or negative error on failure |
| 505 | */ |
| 506 | static int at91_pinctrl_bind(struct udevice *dev) |
| 507 | { |
| 508 | ofnode gpio_node; |
| 509 | struct udevice *gpio; |
| 510 | int ret; |
| 511 | |
| 512 | ofnode_for_each_subnode(gpio_node, dev_ofnode(dev)) { |
| 513 | ret = lists_bind_fdt(dev, gpio_node, &gpio, NULL, false); |
| 514 | if (ret) |
| 515 | return ret; |
| 516 | } |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 521 | static int at91_pinctrl_probe(struct udevice *dev) |
| 522 | { |
| 523 | struct at91_pinctrl_priv *priv = dev_get_priv(dev); |
| 524 | fdt_addr_t addr_base; |
Manikandan Muralidharan | f4c31af | 2025-02-10 12:21:42 +0530 | [diff] [blame] | 525 | struct udevice *gpio_node; |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 526 | int index; |
| 527 | |
Manikandan Muralidharan | f4c31af | 2025-02-10 12:21:42 +0530 | [diff] [blame] | 528 | if (list_empty(&dev->child_head)) { |
| 529 | for (index = 0; index < MAX_GPIO_BANKS; index++) { |
| 530 | addr_base = devfdt_get_addr_index(dev, index); |
| 531 | if (addr_base == FDT_ADDR_T_NONE) |
| 532 | break; |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 533 | |
Manikandan Muralidharan | f4c31af | 2025-02-10 12:21:42 +0530 | [diff] [blame] | 534 | priv->reg_base[index] = (struct at91_port *)addr_base; |
| 535 | } |
| 536 | } else { |
| 537 | index = 0; |
| 538 | list_for_each_entry(gpio_node, &dev->child_head, sibling_node) { |
| 539 | addr_base = dev_read_addr(gpio_node); |
| 540 | if (addr_base == FDT_ADDR_T_NONE) |
| 541 | break; |
| 542 | |
| 543 | priv->reg_base[index] = (struct at91_port *)addr_base; |
| 544 | index++; |
| 545 | } |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 546 | } |
| 547 | |
Manikandan Muralidharan | f4c31af | 2025-02-10 12:21:42 +0530 | [diff] [blame] | 548 | priv->nbanks = index < MAX_GPIO_BANKS ? index : MAX_GPIO_BANKS; |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | static const struct udevice_id at91_pinctrl_match[] = { |
| 554 | { .compatible = "atmel,sama5d3-pinctrl", .data = (ulong)&sama5d3_ops }, |
| 555 | { .compatible = "atmel,at91sam9x5-pinctrl", .data = (ulong)&at91sam9x5_ops }, |
| 556 | { .compatible = "atmel,at91rm9200-pinctrl", .data = (ulong)&at91rm9200_ops }, |
Claudiu Beznea | d95dae1 | 2019-03-25 10:33:59 +0000 | [diff] [blame] | 557 | { .compatible = "microchip,sam9x60-pinctrl", .data = (ulong)&sam9x60_ops }, |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 558 | {} |
| 559 | }; |
| 560 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 561 | U_BOOT_DRIVER(atmel_sama5d3_pinctrl) = { |
| 562 | .name = "atmel_sama5d3_pinctrl", |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 563 | .id = UCLASS_PINCTRL, |
| 564 | .of_match = at91_pinctrl_match, |
| 565 | .probe = at91_pinctrl_probe, |
Manikandan Muralidharan | 8f89f1b | 2025-02-10 12:21:41 +0530 | [diff] [blame] | 566 | .bind = at91_pinctrl_bind, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 567 | .priv_auto = sizeof(struct at91_pinctrl_priv), |
Wenyou Yang | c24e013 | 2017-03-23 12:44:37 +0800 | [diff] [blame] | 568 | .ops = &at91_pinctrl_ops, |
| 569 | }; |
Walter Lozano | 48e5b04 | 2020-06-25 01:10:06 -0300 | [diff] [blame] | 570 | |
Simon Glass | df65db8 | 2020-12-28 20:34:57 -0700 | [diff] [blame] | 571 | DM_DRIVER_ALIAS(atmel_sama5d3_pinctrl, atmel_at91rm9200_pinctrl) |