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