developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2018 MediaTek Inc. |
| 4 | * Author: Ryder Lee <ryder.lee@mediatek.com> |
| 5 | */ |
| 6 | |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <dm/device-internal.h> |
| 9 | #include <dm/lists.h> |
| 10 | #include <dm/pinctrl.h> |
| 11 | #include <asm/io.h> |
| 12 | #include <asm-generic/gpio.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 13 | #include <linux/bitops.h> |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 14 | |
| 15 | #include "pinctrl-mtk-common.h" |
| 16 | |
Fabien Parent | 105f6c8 | 2019-07-18 19:08:08 +0200 | [diff] [blame] | 17 | #if CONFIG_IS_ENABLED(PINCONF) |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 18 | /** |
| 19 | * struct mtk_drive_desc - the structure that holds the information |
| 20 | * of the driving current |
| 21 | * @min: the minimum current of this group |
| 22 | * @max: the maximum current of this group |
| 23 | * @step: the step current of this group |
| 24 | * @scal: the weight factor |
| 25 | * |
| 26 | * formula: output = ((input) / step - 1) * scal |
| 27 | */ |
| 28 | struct mtk_drive_desc { |
| 29 | u8 min; |
| 30 | u8 max; |
| 31 | u8 step; |
| 32 | u8 scal; |
| 33 | }; |
| 34 | |
| 35 | /* The groups of drive strength */ |
| 36 | static const struct mtk_drive_desc mtk_drive[] = { |
| 37 | [DRV_GRP0] = { 4, 16, 4, 1 }, |
| 38 | [DRV_GRP1] = { 4, 16, 4, 2 }, |
| 39 | [DRV_GRP2] = { 2, 8, 2, 1 }, |
| 40 | [DRV_GRP3] = { 2, 8, 2, 2 }, |
| 41 | [DRV_GRP4] = { 2, 16, 2, 1 }, |
| 42 | }; |
Fabien Parent | 105f6c8 | 2019-07-18 19:08:08 +0200 | [diff] [blame] | 43 | #endif |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 44 | |
| 45 | static const char *mtk_pinctrl_dummy_name = "_dummy"; |
| 46 | |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 47 | static void mtk_w32(struct udevice *dev, u8 i, u32 reg, u32 val) |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 48 | { |
| 49 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 50 | |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 51 | __raw_writel(val, priv->base[i] + reg); |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 52 | } |
| 53 | |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 54 | static u32 mtk_r32(struct udevice *dev, u8 i, u32 reg) |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 55 | { |
| 56 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 57 | |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 58 | return __raw_readl(priv->base[i] + reg); |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static inline int get_count_order(unsigned int count) |
| 62 | { |
| 63 | int order; |
| 64 | |
| 65 | order = fls(count) - 1; |
| 66 | if (count & (count - 1)) |
| 67 | order++; |
| 68 | return order; |
| 69 | } |
| 70 | |
| 71 | void mtk_rmw(struct udevice *dev, u32 reg, u32 mask, u32 set) |
| 72 | { |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 73 | return mtk_i_rmw(dev, 0, reg, mask, set); |
| 74 | } |
| 75 | |
| 76 | void mtk_i_rmw(struct udevice *dev, u8 i, u32 reg, u32 mask, u32 set) |
| 77 | { |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 78 | u32 val; |
| 79 | |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 80 | val = mtk_r32(dev, i, reg); |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 81 | val &= ~mask; |
| 82 | val |= set; |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 83 | mtk_w32(dev, i, reg, val); |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static int mtk_hw_pin_field_lookup(struct udevice *dev, int pin, |
| 87 | const struct mtk_pin_reg_calc *rc, |
| 88 | struct mtk_pin_field *pfd) |
| 89 | { |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 90 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 91 | const struct mtk_pin_field_calc *c, *e; |
| 92 | u32 bits; |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 93 | u32 base_calc = priv->soc->base_calc; |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 94 | |
| 95 | c = rc->range; |
| 96 | e = c + rc->nranges; |
| 97 | |
| 98 | while (c < e) { |
| 99 | if (pin >= c->s_pin && pin <= c->e_pin) |
| 100 | break; |
| 101 | c++; |
| 102 | } |
| 103 | |
| 104 | if (c >= e) |
| 105 | return -EINVAL; |
| 106 | |
| 107 | /* Calculated bits as the overall offset the pin is located at, |
| 108 | * if c->fixed is held, that determines the all the pins in the |
| 109 | * range use the same field with the s_pin. |
| 110 | */ |
| 111 | bits = c->fixed ? c->s_bit : c->s_bit + (pin - c->s_pin) * (c->x_bits); |
| 112 | |
| 113 | /* Fill pfd from bits. For example 32-bit register applied is assumed |
| 114 | * when c->sz_reg is equal to 32. |
| 115 | */ |
| 116 | pfd->offset = c->s_addr + c->x_addrs * (bits / c->sz_reg); |
| 117 | pfd->bitpos = bits % c->sz_reg; |
| 118 | pfd->mask = (1 << c->x_bits) - 1; |
| 119 | |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 120 | if (base_calc) |
| 121 | pfd->index = c->i_base; |
| 122 | else |
| 123 | pfd->index = 0; |
| 124 | |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 125 | /* pfd->next is used for indicating that bit wrapping-around happens |
| 126 | * which requires the manipulation for bit 0 starting in the next |
| 127 | * register to form the complete field read/write. |
| 128 | */ |
| 129 | pfd->next = pfd->bitpos + c->x_bits > c->sz_reg ? c->x_addrs : 0; |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static int mtk_hw_pin_field_get(struct udevice *dev, int pin, |
| 135 | int field, struct mtk_pin_field *pfd) |
| 136 | { |
| 137 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 138 | const struct mtk_pin_reg_calc *rc; |
| 139 | |
| 140 | if (field < 0 || field >= PINCTRL_PIN_REG_MAX) |
| 141 | return -EINVAL; |
| 142 | |
| 143 | if (priv->soc->reg_cal && priv->soc->reg_cal[field].range) |
| 144 | rc = &priv->soc->reg_cal[field]; |
| 145 | else |
| 146 | return -EINVAL; |
| 147 | |
| 148 | return mtk_hw_pin_field_lookup(dev, pin, rc, pfd); |
| 149 | } |
| 150 | |
| 151 | static void mtk_hw_bits_part(struct mtk_pin_field *pf, int *h, int *l) |
| 152 | { |
| 153 | *l = 32 - pf->bitpos; |
| 154 | *h = get_count_order(pf->mask) - *l; |
| 155 | } |
| 156 | |
| 157 | static void mtk_hw_write_cross_field(struct udevice *dev, |
| 158 | struct mtk_pin_field *pf, int value) |
| 159 | { |
| 160 | int nbits_l, nbits_h; |
| 161 | |
| 162 | mtk_hw_bits_part(pf, &nbits_h, &nbits_l); |
| 163 | |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 164 | mtk_i_rmw(dev, pf->index, pf->offset, pf->mask << pf->bitpos, |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 165 | (value & pf->mask) << pf->bitpos); |
| 166 | |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 167 | mtk_i_rmw(dev, pf->index, pf->offset + pf->next, BIT(nbits_h) - 1, |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 168 | (value & pf->mask) >> nbits_l); |
| 169 | } |
| 170 | |
| 171 | static void mtk_hw_read_cross_field(struct udevice *dev, |
| 172 | struct mtk_pin_field *pf, int *value) |
| 173 | { |
| 174 | int nbits_l, nbits_h, h, l; |
| 175 | |
| 176 | mtk_hw_bits_part(pf, &nbits_h, &nbits_l); |
| 177 | |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 178 | l = (mtk_r32(dev, pf->index, pf->offset) >> pf->bitpos) & (BIT(nbits_l) - 1); |
| 179 | h = (mtk_r32(dev, pf->index, pf->offset + pf->next)) & (BIT(nbits_h) - 1); |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 180 | |
| 181 | *value = (h << nbits_l) | l; |
| 182 | } |
| 183 | |
| 184 | static int mtk_hw_set_value(struct udevice *dev, int pin, int field, |
| 185 | int value) |
| 186 | { |
| 187 | struct mtk_pin_field pf; |
| 188 | int err; |
| 189 | |
| 190 | err = mtk_hw_pin_field_get(dev, pin, field, &pf); |
| 191 | if (err) |
| 192 | return err; |
| 193 | |
| 194 | if (!pf.next) |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 195 | mtk_i_rmw(dev, pf.index, pf.offset, pf.mask << pf.bitpos, |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 196 | (value & pf.mask) << pf.bitpos); |
| 197 | else |
| 198 | mtk_hw_write_cross_field(dev, &pf, value); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static int mtk_hw_get_value(struct udevice *dev, int pin, int field, |
| 204 | int *value) |
| 205 | { |
| 206 | struct mtk_pin_field pf; |
| 207 | int err; |
| 208 | |
| 209 | err = mtk_hw_pin_field_get(dev, pin, field, &pf); |
| 210 | if (err) |
| 211 | return err; |
| 212 | |
| 213 | if (!pf.next) |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 214 | *value = (mtk_r32(dev, pf.index, pf.offset) >> pf.bitpos) & pf.mask; |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 215 | else |
| 216 | mtk_hw_read_cross_field(dev, &pf, value); |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame] | 221 | #if CONFIG_IS_ENABLED(PINCONF) |
| 222 | static int mtk_get_pin_io_type(struct udevice *dev, int pin, |
| 223 | struct mtk_io_type_desc *io_type) |
| 224 | { |
| 225 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 226 | u8 io_n = priv->soc->pins[pin].io_n; |
| 227 | |
| 228 | if (io_n >= priv->soc->ntype) |
| 229 | return -EINVAL; |
| 230 | |
| 231 | io_type->name = priv->soc->io_type[io_n].name; |
| 232 | io_type->bias_set = priv->soc->io_type[io_n].bias_set; |
| 233 | io_type->drive_set = priv->soc->io_type[io_n].drive_set; |
| 234 | io_type->input_enable = priv->soc->io_type[io_n].input_enable; |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | #endif |
| 239 | |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 240 | static int mtk_get_groups_count(struct udevice *dev) |
| 241 | { |
| 242 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 243 | |
| 244 | return priv->soc->ngrps; |
| 245 | } |
| 246 | |
| 247 | static const char *mtk_get_pin_name(struct udevice *dev, |
| 248 | unsigned int selector) |
| 249 | { |
| 250 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 251 | |
developer | deb8406 | 2021-03-05 10:22:11 +0800 | [diff] [blame] | 252 | if (!priv->soc->pins[selector].name) |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 253 | return mtk_pinctrl_dummy_name; |
| 254 | |
| 255 | return priv->soc->pins[selector].name; |
| 256 | } |
| 257 | |
| 258 | static int mtk_get_pins_count(struct udevice *dev) |
| 259 | { |
| 260 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 261 | |
| 262 | return priv->soc->npins; |
| 263 | } |
| 264 | |
developer | 0163c08 | 2021-03-05 10:22:19 +0800 | [diff] [blame] | 265 | static int mtk_get_pin_muxing(struct udevice *dev, unsigned int selector, |
| 266 | char *buf, int size) |
| 267 | { |
| 268 | int val, err; |
developer | 0163c08 | 2021-03-05 10:22:19 +0800 | [diff] [blame] | 269 | err = mtk_hw_get_value(dev, selector, PINCTRL_PIN_REG_MODE, &val); |
| 270 | if (err) |
| 271 | return err; |
| 272 | |
| 273 | snprintf(buf, size, "Aux Func.%d", val); |
| 274 | return 0; |
| 275 | } |
| 276 | |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 277 | static const char *mtk_get_group_name(struct udevice *dev, |
| 278 | unsigned int selector) |
| 279 | { |
| 280 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 281 | |
| 282 | if (!priv->soc->grps[selector].name) |
| 283 | return mtk_pinctrl_dummy_name; |
| 284 | |
| 285 | return priv->soc->grps[selector].name; |
| 286 | } |
| 287 | |
| 288 | static int mtk_get_functions_count(struct udevice *dev) |
| 289 | { |
| 290 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 291 | |
| 292 | return priv->soc->nfuncs; |
| 293 | } |
| 294 | |
| 295 | static const char *mtk_get_function_name(struct udevice *dev, |
| 296 | unsigned int selector) |
| 297 | { |
| 298 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 299 | |
| 300 | if (!priv->soc->funcs[selector].name) |
| 301 | return mtk_pinctrl_dummy_name; |
| 302 | |
| 303 | return priv->soc->funcs[selector].name; |
| 304 | } |
| 305 | |
developer | dac0f76 | 2023-07-19 17:16:46 +0800 | [diff] [blame] | 306 | static int mtk_pinmux_set(struct udevice *dev, unsigned int pin_selector, |
| 307 | unsigned int func_selector) |
| 308 | { |
| 309 | int err; |
| 310 | |
| 311 | err = mtk_hw_set_value(dev, pin_selector, PINCTRL_PIN_REG_MODE, |
| 312 | func_selector); |
| 313 | if (err) |
| 314 | return err; |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 319 | static int mtk_pinmux_group_set(struct udevice *dev, |
| 320 | unsigned int group_selector, |
| 321 | unsigned int func_selector) |
| 322 | { |
| 323 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 324 | const struct mtk_group_desc *grp = |
| 325 | &priv->soc->grps[group_selector]; |
| 326 | int i; |
| 327 | |
| 328 | for (i = 0; i < grp->num_pins; i++) { |
developer | 67873bd | 2023-07-19 17:16:37 +0800 | [diff] [blame] | 329 | const int *pin_modes = grp->data; |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 330 | |
| 331 | mtk_hw_set_value(dev, grp->pins[i], PINCTRL_PIN_REG_MODE, |
| 332 | pin_modes[i]); |
| 333 | } |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | #if CONFIG_IS_ENABLED(PINCONF) |
| 339 | static const struct pinconf_param mtk_conf_params[] = { |
| 340 | { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 }, |
| 341 | { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 }, |
| 342 | { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 }, |
| 343 | { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 }, |
| 344 | { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 }, |
| 345 | { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 }, |
| 346 | { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 }, |
| 347 | { "output-enable", PIN_CONFIG_OUTPUT_ENABLE, 1 }, |
| 348 | { "output-high", PIN_CONFIG_OUTPUT, 1, }, |
| 349 | { "output-low", PIN_CONFIG_OUTPUT, 0, }, |
| 350 | { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 }, |
| 351 | }; |
| 352 | |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 353 | int mtk_pinconf_bias_set_v0(struct udevice *dev, u32 pin, bool disable, |
| 354 | bool pullup, u32 val) |
| 355 | { |
| 356 | return mtk_pinconf_bias_set_pu_pd(dev, pin, disable, pullup, val); |
| 357 | } |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 358 | |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 359 | int mtk_pinconf_bias_set_v1(struct udevice *dev, u32 pin, bool disable, |
| 360 | bool pullup, u32 val) |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 361 | { |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 362 | int err; |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 363 | |
Daniel Golle | 8c9bb0a | 2023-04-12 21:36:43 +0100 | [diff] [blame] | 364 | /* set pupd_r1_r0 if pullen_pullsel succeeded */ |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 365 | err = mtk_pinconf_bias_set_pullen_pullsel(dev, pin, disable, pullup, |
| 366 | val); |
Daniel Golle | 8c9bb0a | 2023-04-12 21:36:43 +0100 | [diff] [blame] | 367 | if (!err) |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 368 | return mtk_pinconf_bias_set_pupd_r1_r0(dev, pin, disable, |
| 369 | pullup, val); |
| 370 | |
| 371 | return err; |
| 372 | } |
| 373 | |
| 374 | int mtk_pinconf_bias_set_pu_pd(struct udevice *dev, u32 pin, bool disable, |
| 375 | bool pullup, u32 val) |
| 376 | { |
| 377 | int err; |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 378 | |
| 379 | if (disable) { |
| 380 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PU, 0); |
| 381 | if (err) |
| 382 | return err; |
| 383 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PD, 0); |
| 384 | if (err) |
| 385 | return err; |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 386 | } else { |
| 387 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PU, pullup); |
| 388 | if (err) |
| 389 | return err; |
| 390 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PD, !pullup); |
| 391 | if (err) |
| 392 | return err; |
| 393 | } |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 398 | int mtk_pinconf_bias_set_pullen_pullsel(struct udevice *dev, u32 pin, |
| 399 | bool disable, bool pullup, u32 val) |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 400 | { |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 401 | int err; |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 402 | |
| 403 | if (disable) { |
| 404 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PULLEN, 0); |
| 405 | if (err) |
| 406 | return err; |
| 407 | } else { |
| 408 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PULLEN, 1); |
| 409 | if (err) |
| 410 | return err; |
| 411 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PULLSEL, |
| 412 | pullup); |
| 413 | if (err) |
| 414 | return err; |
| 415 | } |
| 416 | |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | int mtk_pinconf_bias_set_pupd_r1_r0(struct udevice *dev, u32 pin, bool disable, |
| 421 | bool pullup, u32 val) |
| 422 | { |
| 423 | int err, r0, r1; |
| 424 | |
| 425 | r0 = !!(val & 1); |
| 426 | r1 = !!(val & 2); |
| 427 | |
| 428 | if (disable) { |
| 429 | pullup = 0; |
| 430 | r0 = 0; |
| 431 | r1 = 0; |
David Woodhouse | 3210275 | 2020-06-19 12:40:20 +0100 | [diff] [blame] | 432 | } |
| 433 | |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 434 | /* MTK HW PUPD bit: 1 for pull-down, 0 for pull-up */ |
| 435 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PUPD, !pullup); |
| 436 | if (err) |
| 437 | return err; |
| 438 | |
| 439 | /* Also set PUPD/R0/R1 if the pin has them */ |
| 440 | mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_R0, r0); |
| 441 | mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_R1, r1); |
| 442 | |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 446 | int mtk_pinconf_bias_set(struct udevice *dev, u32 pin, u32 arg, u32 val) |
| 447 | { |
| 448 | int err; |
| 449 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame] | 450 | struct mtk_io_type_desc io_type; |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 451 | int rev = priv->soc->rev; |
| 452 | bool disable, pullup; |
| 453 | |
| 454 | disable = (arg == PIN_CONFIG_BIAS_DISABLE); |
| 455 | pullup = (arg == PIN_CONFIG_BIAS_PULL_UP); |
| 456 | |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame] | 457 | if (!mtk_get_pin_io_type(dev, pin, &io_type)) { |
| 458 | if (io_type.bias_set) |
| 459 | err = io_type.bias_set(dev, pin, disable, pullup, |
| 460 | val); |
| 461 | else |
| 462 | err = -EINVAL; |
| 463 | |
| 464 | } else if (rev == MTK_PINCTRL_V0) { |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 465 | err = mtk_pinconf_bias_set_v0(dev, pin, disable, pullup, val); |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame] | 466 | } else { |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 467 | err = mtk_pinconf_bias_set_v1(dev, pin, disable, pullup, val); |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame] | 468 | } |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 469 | |
| 470 | return err; |
| 471 | } |
| 472 | |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 473 | int mtk_pinconf_input_enable_v1(struct udevice *dev, u32 pin, u32 arg) |
| 474 | { |
| 475 | int err; |
| 476 | |
| 477 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_IES, 1); |
| 478 | if (err) |
| 479 | return err; |
| 480 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR, 0); |
| 481 | if (err) |
| 482 | return err; |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 483 | |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 484 | return 0; |
| 485 | } |
| 486 | |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 487 | int mtk_pinconf_input_enable(struct udevice *dev, u32 pin, u32 arg) |
| 488 | { |
| 489 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame] | 490 | struct mtk_io_type_desc io_type; |
| 491 | |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 492 | int rev = priv->soc->rev; |
| 493 | |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame] | 494 | if (!mtk_get_pin_io_type(dev, pin, &io_type)) |
| 495 | if (io_type.input_enable) |
| 496 | return io_type.input_enable(dev, pin, arg); |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 497 | if (rev == MTK_PINCTRL_V1) |
| 498 | return mtk_pinconf_input_enable_v1(dev, pin, arg); |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 503 | int mtk_pinconf_drive_set_v0(struct udevice *dev, u32 pin, u32 arg) |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 504 | { |
| 505 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 506 | const struct mtk_pin_desc *desc = &priv->soc->pins[pin]; |
| 507 | const struct mtk_drive_desc *tb; |
| 508 | int err = -ENOTSUPP; |
| 509 | |
| 510 | tb = &mtk_drive[desc->drv_n]; |
| 511 | /* 4mA when (e8, e4) = (0, 0) |
| 512 | * 8mA when (e8, e4) = (0, 1) |
| 513 | * 12mA when (e8, e4) = (1, 0) |
| 514 | * 16mA when (e8, e4) = (1, 1) |
| 515 | */ |
| 516 | if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) { |
| 517 | arg = (arg / tb->step - 1) * tb->scal; |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 518 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_E4, |
| 519 | arg & 0x1); |
| 520 | if (err) |
| 521 | return err; |
| 522 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_E8, |
| 523 | (arg & 0x2) >> 1); |
| 524 | if (err) |
| 525 | return err; |
| 526 | } |
| 527 | |
developer | b42024c | 2023-07-19 17:16:42 +0800 | [diff] [blame] | 528 | return err; |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 529 | } |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 530 | |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 531 | int mtk_pinconf_drive_set_v1(struct udevice *dev, u32 pin, u32 arg) |
| 532 | { |
| 533 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 534 | const struct mtk_pin_desc *desc = &priv->soc->pins[pin]; |
| 535 | const struct mtk_drive_desc *tb; |
| 536 | int err = -ENOTSUPP; |
| 537 | |
| 538 | tb = &mtk_drive[desc->drv_n]; |
| 539 | if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) { |
| 540 | arg = (arg / tb->step - 1) * tb->scal; |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 541 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DRV, arg); |
| 542 | if (err) |
| 543 | return err; |
| 544 | } |
| 545 | |
developer | b42024c | 2023-07-19 17:16:42 +0800 | [diff] [blame] | 546 | return err; |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 547 | } |
| 548 | |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 549 | int mtk_pinconf_drive_set(struct udevice *dev, u32 pin, u32 arg) |
| 550 | { |
| 551 | int err; |
| 552 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame] | 553 | struct mtk_io_type_desc io_type; |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 554 | int rev = priv->soc->rev; |
| 555 | |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame] | 556 | if (!mtk_get_pin_io_type(dev, pin, &io_type)) { |
| 557 | if (io_type.drive_set) |
| 558 | err = io_type.drive_set(dev, pin, arg); |
| 559 | else |
| 560 | err = -EINVAL; |
| 561 | } else if (rev == MTK_PINCTRL_V0) { |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 562 | err = mtk_pinconf_drive_set_v0(dev, pin, arg); |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame] | 563 | } else { |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 564 | err = mtk_pinconf_drive_set_v1(dev, pin, arg); |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame] | 565 | } |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 566 | |
| 567 | return err; |
| 568 | } |
| 569 | |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 570 | static int mtk_pinconf_set(struct udevice *dev, unsigned int pin, |
| 571 | unsigned int param, unsigned int arg) |
| 572 | { |
| 573 | int err = 0; |
| 574 | |
| 575 | switch (param) { |
| 576 | case PIN_CONFIG_BIAS_DISABLE: |
| 577 | case PIN_CONFIG_BIAS_PULL_UP: |
| 578 | case PIN_CONFIG_BIAS_PULL_DOWN: |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 579 | err = mtk_pinconf_bias_set(dev, pin, param, arg); |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 580 | if (err) |
| 581 | goto err; |
| 582 | break; |
| 583 | case PIN_CONFIG_OUTPUT_ENABLE: |
| 584 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_SMT, 0); |
| 585 | if (err) |
| 586 | goto err; |
| 587 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR, 1); |
| 588 | if (err) |
| 589 | goto err; |
| 590 | break; |
| 591 | case PIN_CONFIG_INPUT_ENABLE: |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 592 | err = mtk_pinconf_input_enable(dev, pin, param); |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 593 | if (err) |
| 594 | goto err; |
| 595 | break; |
| 596 | case PIN_CONFIG_OUTPUT: |
| 597 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR, 1); |
| 598 | if (err) |
| 599 | goto err; |
| 600 | |
| 601 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DO, arg); |
| 602 | if (err) |
| 603 | goto err; |
| 604 | break; |
| 605 | case PIN_CONFIG_INPUT_SCHMITT_ENABLE: |
| 606 | /* arg = 1: Input mode & SMT enable ; |
| 607 | * arg = 0: Output mode & SMT disable |
| 608 | */ |
| 609 | arg = arg ? 2 : 1; |
| 610 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR, |
| 611 | arg & 1); |
| 612 | if (err) |
| 613 | goto err; |
| 614 | |
| 615 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_SMT, |
| 616 | !!(arg & 2)); |
| 617 | if (err) |
| 618 | goto err; |
| 619 | break; |
| 620 | case PIN_CONFIG_DRIVE_STRENGTH: |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 621 | err = mtk_pinconf_drive_set(dev, pin, arg); |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 622 | if (err) |
| 623 | goto err; |
| 624 | break; |
| 625 | |
| 626 | default: |
| 627 | err = -ENOTSUPP; |
| 628 | } |
| 629 | |
| 630 | err: |
| 631 | |
| 632 | return err; |
| 633 | } |
| 634 | |
| 635 | static int mtk_pinconf_group_set(struct udevice *dev, |
| 636 | unsigned int group_selector, |
| 637 | unsigned int param, unsigned int arg) |
| 638 | { |
| 639 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 640 | const struct mtk_group_desc *grp = |
| 641 | &priv->soc->grps[group_selector]; |
| 642 | int i, ret; |
| 643 | |
| 644 | for (i = 0; i < grp->num_pins; i++) { |
| 645 | ret = mtk_pinconf_set(dev, grp->pins[i], param, arg); |
| 646 | if (ret) |
| 647 | return ret; |
| 648 | } |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | #endif |
| 653 | |
| 654 | const struct pinctrl_ops mtk_pinctrl_ops = { |
| 655 | .get_pins_count = mtk_get_pins_count, |
| 656 | .get_pin_name = mtk_get_pin_name, |
developer | 0163c08 | 2021-03-05 10:22:19 +0800 | [diff] [blame] | 657 | .get_pin_muxing = mtk_get_pin_muxing, |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 658 | .get_groups_count = mtk_get_groups_count, |
| 659 | .get_group_name = mtk_get_group_name, |
| 660 | .get_functions_count = mtk_get_functions_count, |
| 661 | .get_function_name = mtk_get_function_name, |
developer | dac0f76 | 2023-07-19 17:16:46 +0800 | [diff] [blame] | 662 | .pinmux_set = mtk_pinmux_set, |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 663 | .pinmux_group_set = mtk_pinmux_group_set, |
| 664 | #if CONFIG_IS_ENABLED(PINCONF) |
| 665 | .pinconf_num_params = ARRAY_SIZE(mtk_conf_params), |
| 666 | .pinconf_params = mtk_conf_params, |
| 667 | .pinconf_set = mtk_pinconf_set, |
| 668 | .pinconf_group_set = mtk_pinconf_group_set, |
| 669 | #endif |
| 670 | .set_state = pinctrl_generic_set_state, |
| 671 | }; |
| 672 | |
developer | f4154da | 2021-03-05 10:22:26 +0800 | [diff] [blame] | 673 | #if CONFIG_IS_ENABLED(DM_GPIO) || \ |
Simon Glass | 035939e | 2021-07-10 21:14:30 -0600 | [diff] [blame] | 674 | (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_GPIO)) |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 675 | static int mtk_gpio_get(struct udevice *dev, unsigned int off) |
| 676 | { |
| 677 | int val, err; |
| 678 | |
| 679 | err = mtk_hw_get_value(dev->parent, off, PINCTRL_PIN_REG_DI, &val); |
| 680 | if (err) |
| 681 | return err; |
| 682 | |
| 683 | return !!val; |
| 684 | } |
| 685 | |
| 686 | static int mtk_gpio_set(struct udevice *dev, unsigned int off, int val) |
| 687 | { |
| 688 | return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_DO, !!val); |
| 689 | } |
| 690 | |
| 691 | static int mtk_gpio_get_direction(struct udevice *dev, unsigned int off) |
| 692 | { |
| 693 | int val, err; |
| 694 | |
| 695 | err = mtk_hw_get_value(dev->parent, off, PINCTRL_PIN_REG_DIR, &val); |
| 696 | if (err) |
| 697 | return err; |
| 698 | |
| 699 | return val ? GPIOF_OUTPUT : GPIOF_INPUT; |
| 700 | } |
| 701 | |
| 702 | static int mtk_gpio_direction_input(struct udevice *dev, unsigned int off) |
| 703 | { |
| 704 | return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_DIR, 0); |
| 705 | } |
| 706 | |
| 707 | static int mtk_gpio_direction_output(struct udevice *dev, |
| 708 | unsigned int off, int val) |
| 709 | { |
| 710 | mtk_gpio_set(dev, off, val); |
| 711 | |
| 712 | /* And set the requested value */ |
| 713 | return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_DIR, 1); |
| 714 | } |
| 715 | |
| 716 | static int mtk_gpio_request(struct udevice *dev, unsigned int off, |
| 717 | const char *label) |
| 718 | { |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 719 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev->parent); |
| 720 | |
| 721 | return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_MODE, |
| 722 | priv->soc->gpio_mode); |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | static int mtk_gpio_probe(struct udevice *dev) |
| 726 | { |
| 727 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev->parent); |
| 728 | struct gpio_dev_priv *uc_priv; |
| 729 | |
| 730 | uc_priv = dev_get_uclass_priv(dev); |
| 731 | uc_priv->bank_name = priv->soc->name; |
| 732 | uc_priv->gpio_count = priv->soc->npins; |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | static const struct dm_gpio_ops mtk_gpio_ops = { |
| 738 | .request = mtk_gpio_request, |
| 739 | .set_value = mtk_gpio_set, |
| 740 | .get_value = mtk_gpio_get, |
| 741 | .get_function = mtk_gpio_get_direction, |
| 742 | .direction_input = mtk_gpio_direction_input, |
| 743 | .direction_output = mtk_gpio_direction_output, |
| 744 | }; |
| 745 | |
| 746 | static struct driver mtk_gpio_driver = { |
| 747 | .name = "mediatek_gpio", |
| 748 | .id = UCLASS_GPIO, |
| 749 | .probe = mtk_gpio_probe, |
| 750 | .ops = &mtk_gpio_ops, |
| 751 | }; |
| 752 | |
| 753 | static int mtk_gpiochip_register(struct udevice *parent) |
| 754 | { |
| 755 | struct uclass_driver *drv; |
| 756 | struct udevice *dev; |
| 757 | int ret; |
| 758 | ofnode node; |
| 759 | |
| 760 | drv = lists_uclass_lookup(UCLASS_GPIO); |
| 761 | if (!drv) |
| 762 | return -ENOENT; |
| 763 | |
Heinrich Schuchardt | 0c67d12 | 2020-12-27 21:18:26 +0100 | [diff] [blame] | 764 | ret = -ENOENT; |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 765 | dev_for_each_subnode(node, parent) |
| 766 | if (ofnode_read_bool(node, "gpio-controller")) { |
| 767 | ret = 0; |
| 768 | break; |
| 769 | } |
| 770 | |
| 771 | if (ret) |
| 772 | return ret; |
| 773 | |
| 774 | ret = device_bind_with_driver_data(parent, &mtk_gpio_driver, |
| 775 | "mediatek_gpio", 0, node, |
| 776 | &dev); |
| 777 | if (ret) |
| 778 | return ret; |
| 779 | |
| 780 | return 0; |
| 781 | } |
developer | f4154da | 2021-03-05 10:22:26 +0800 | [diff] [blame] | 782 | #endif |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 783 | |
| 784 | int mtk_pinctrl_common_probe(struct udevice *dev, |
developer | 67873bd | 2023-07-19 17:16:37 +0800 | [diff] [blame] | 785 | const struct mtk_pinctrl_soc *soc) |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 786 | { |
| 787 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
developer | f4154da | 2021-03-05 10:22:26 +0800 | [diff] [blame] | 788 | int ret = 0; |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 789 | u32 i = 0; |
| 790 | fdt_addr_t addr; |
| 791 | u32 base_calc = soc->base_calc; |
| 792 | u32 nbase_names = soc->nbase_names; |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 793 | |
| 794 | priv->soc = soc; |
| 795 | |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 796 | if (!base_calc) |
| 797 | nbase_names = 1; |
| 798 | |
| 799 | for (i = 0; i < nbase_names; i++) { |
| 800 | addr = devfdt_get_addr_index(dev, i); |
| 801 | if (addr == FDT_ADDR_T_NONE) |
| 802 | return -EINVAL; |
| 803 | priv->base[i] = (void __iomem *)addr; |
| 804 | } |
| 805 | |
developer | f4154da | 2021-03-05 10:22:26 +0800 | [diff] [blame] | 806 | #if CONFIG_IS_ENABLED(DM_GPIO) || \ |
Simon Glass | 035939e | 2021-07-10 21:14:30 -0600 | [diff] [blame] | 807 | (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_GPIO)) |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 808 | ret = mtk_gpiochip_register(dev); |
developer | f4154da | 2021-03-05 10:22:26 +0800 | [diff] [blame] | 809 | #endif |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 810 | |
developer | f4154da | 2021-03-05 10:22:26 +0800 | [diff] [blame] | 811 | return ret; |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 812 | } |