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 | |
| 222 | if (!priv->soc->grps[selector].name) |
| 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 | |
| 235 | static const char *mtk_get_group_name(struct udevice *dev, |
| 236 | unsigned int selector) |
| 237 | { |
| 238 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 239 | |
| 240 | if (!priv->soc->grps[selector].name) |
| 241 | return mtk_pinctrl_dummy_name; |
| 242 | |
| 243 | return priv->soc->grps[selector].name; |
| 244 | } |
| 245 | |
| 246 | static int mtk_get_functions_count(struct udevice *dev) |
| 247 | { |
| 248 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 249 | |
| 250 | return priv->soc->nfuncs; |
| 251 | } |
| 252 | |
| 253 | static const char *mtk_get_function_name(struct udevice *dev, |
| 254 | unsigned int selector) |
| 255 | { |
| 256 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 257 | |
| 258 | if (!priv->soc->funcs[selector].name) |
| 259 | return mtk_pinctrl_dummy_name; |
| 260 | |
| 261 | return priv->soc->funcs[selector].name; |
| 262 | } |
| 263 | |
| 264 | static int mtk_pinmux_group_set(struct udevice *dev, |
| 265 | unsigned int group_selector, |
| 266 | unsigned int func_selector) |
| 267 | { |
| 268 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 269 | const struct mtk_group_desc *grp = |
| 270 | &priv->soc->grps[group_selector]; |
| 271 | int i; |
| 272 | |
| 273 | for (i = 0; i < grp->num_pins; i++) { |
| 274 | int *pin_modes = grp->data; |
| 275 | |
| 276 | mtk_hw_set_value(dev, grp->pins[i], PINCTRL_PIN_REG_MODE, |
| 277 | pin_modes[i]); |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | #if CONFIG_IS_ENABLED(PINCONF) |
| 284 | static const struct pinconf_param mtk_conf_params[] = { |
| 285 | { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 }, |
| 286 | { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 }, |
| 287 | { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 }, |
| 288 | { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 }, |
| 289 | { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 }, |
| 290 | { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 }, |
| 291 | { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 }, |
| 292 | { "output-enable", PIN_CONFIG_OUTPUT_ENABLE, 1 }, |
| 293 | { "output-high", PIN_CONFIG_OUTPUT, 1, }, |
| 294 | { "output-low", PIN_CONFIG_OUTPUT, 0, }, |
| 295 | { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 }, |
| 296 | }; |
| 297 | |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 298 | |
| 299 | int mtk_pinconf_bias_set_v0(struct udevice *dev, u32 pin, u32 arg) |
| 300 | { |
| 301 | int err, disable, pullup; |
| 302 | |
| 303 | disable = (arg == PIN_CONFIG_BIAS_DISABLE); |
| 304 | pullup = (arg == PIN_CONFIG_BIAS_PULL_UP); |
| 305 | |
| 306 | if (disable) { |
| 307 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PU, 0); |
| 308 | if (err) |
| 309 | return err; |
| 310 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PD, 0); |
| 311 | if (err) |
| 312 | return err; |
| 313 | |
| 314 | } else { |
| 315 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PU, pullup); |
| 316 | if (err) |
| 317 | return err; |
| 318 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PD, !pullup); |
| 319 | if (err) |
| 320 | return err; |
| 321 | } |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | int mtk_pinconf_bias_set_v1(struct udevice *dev, u32 pin, u32 arg) |
| 327 | { |
| 328 | int err, disable, pullup; |
| 329 | |
| 330 | disable = (arg == PIN_CONFIG_BIAS_DISABLE); |
| 331 | pullup = (arg == PIN_CONFIG_BIAS_PULL_UP); |
| 332 | |
| 333 | if (disable) { |
| 334 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PULLEN, 0); |
| 335 | if (err) |
| 336 | return err; |
| 337 | } else { |
| 338 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PULLEN, 1); |
| 339 | if (err) |
| 340 | return err; |
| 341 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PULLSEL, |
| 342 | pullup); |
| 343 | if (err) |
| 344 | return err; |
| 345 | } |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | int mtk_pinconf_input_enable_v1(struct udevice *dev, u32 pin, u32 arg) |
| 351 | { |
| 352 | int err; |
| 353 | |
| 354 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_IES, 1); |
| 355 | if (err) |
| 356 | return err; |
| 357 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR, 0); |
| 358 | if (err) |
| 359 | return err; |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | int mtk_pinconf_drive_set_v0(struct udevice *dev, u32 pin, u32 arg) |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 364 | { |
| 365 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 366 | const struct mtk_pin_desc *desc = &priv->soc->pins[pin]; |
| 367 | const struct mtk_drive_desc *tb; |
| 368 | int err = -ENOTSUPP; |
| 369 | |
| 370 | tb = &mtk_drive[desc->drv_n]; |
| 371 | /* 4mA when (e8, e4) = (0, 0) |
| 372 | * 8mA when (e8, e4) = (0, 1) |
| 373 | * 12mA when (e8, e4) = (1, 0) |
| 374 | * 16mA when (e8, e4) = (1, 1) |
| 375 | */ |
| 376 | if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) { |
| 377 | arg = (arg / tb->step - 1) * tb->scal; |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 378 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_E4, |
| 379 | arg & 0x1); |
| 380 | if (err) |
| 381 | return err; |
| 382 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_E8, |
| 383 | (arg & 0x2) >> 1); |
| 384 | if (err) |
| 385 | return err; |
| 386 | } |
| 387 | |
| 388 | return 0; |
| 389 | } |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 390 | |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 391 | |
| 392 | int mtk_pinconf_drive_set_v1(struct udevice *dev, u32 pin, u32 arg) |
| 393 | { |
| 394 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 395 | const struct mtk_pin_desc *desc = &priv->soc->pins[pin]; |
| 396 | const struct mtk_drive_desc *tb; |
| 397 | int err = -ENOTSUPP; |
| 398 | |
| 399 | tb = &mtk_drive[desc->drv_n]; |
| 400 | if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) { |
| 401 | arg = (arg / tb->step - 1) * tb->scal; |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 402 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DRV, arg); |
| 403 | if (err) |
| 404 | return err; |
| 405 | } |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static int mtk_pinconf_set(struct udevice *dev, unsigned int pin, |
| 411 | unsigned int param, unsigned int arg) |
| 412 | { |
| 413 | int err = 0; |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 414 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 415 | int rev = priv->soc->rev; |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 416 | |
| 417 | switch (param) { |
| 418 | case PIN_CONFIG_BIAS_DISABLE: |
| 419 | case PIN_CONFIG_BIAS_PULL_UP: |
| 420 | case PIN_CONFIG_BIAS_PULL_DOWN: |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 421 | if (rev == MTK_PINCTRL_V0) |
| 422 | err = mtk_pinconf_bias_set_v0(dev, pin, param); |
| 423 | else |
| 424 | err = mtk_pinconf_bias_set_v1(dev, pin, param); |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 425 | if (err) |
| 426 | goto err; |
| 427 | break; |
| 428 | case PIN_CONFIG_OUTPUT_ENABLE: |
| 429 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_SMT, 0); |
| 430 | if (err) |
| 431 | goto err; |
| 432 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR, 1); |
| 433 | if (err) |
| 434 | goto err; |
| 435 | break; |
| 436 | case PIN_CONFIG_INPUT_ENABLE: |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 437 | if (rev == MTK_PINCTRL_V1) |
| 438 | err = mtk_pinconf_input_enable_v1(dev, pin, param); |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 439 | if (err) |
| 440 | goto err; |
| 441 | break; |
| 442 | case PIN_CONFIG_OUTPUT: |
| 443 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR, 1); |
| 444 | if (err) |
| 445 | goto err; |
| 446 | |
| 447 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DO, arg); |
| 448 | if (err) |
| 449 | goto err; |
| 450 | break; |
| 451 | case PIN_CONFIG_INPUT_SCHMITT_ENABLE: |
| 452 | /* arg = 1: Input mode & SMT enable ; |
| 453 | * arg = 0: Output mode & SMT disable |
| 454 | */ |
| 455 | arg = arg ? 2 : 1; |
| 456 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR, |
| 457 | arg & 1); |
| 458 | if (err) |
| 459 | goto err; |
| 460 | |
| 461 | err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_SMT, |
| 462 | !!(arg & 2)); |
| 463 | if (err) |
| 464 | goto err; |
| 465 | break; |
| 466 | case PIN_CONFIG_DRIVE_STRENGTH: |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 467 | if (rev == MTK_PINCTRL_V0) |
| 468 | err = mtk_pinconf_drive_set_v0(dev, pin, arg); |
| 469 | else |
| 470 | err = mtk_pinconf_drive_set_v1(dev, pin, arg); |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 471 | if (err) |
| 472 | goto err; |
| 473 | break; |
| 474 | |
| 475 | default: |
| 476 | err = -ENOTSUPP; |
| 477 | } |
| 478 | |
| 479 | err: |
| 480 | |
| 481 | return err; |
| 482 | } |
| 483 | |
| 484 | static int mtk_pinconf_group_set(struct udevice *dev, |
| 485 | unsigned int group_selector, |
| 486 | unsigned int param, unsigned int arg) |
| 487 | { |
| 488 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 489 | const struct mtk_group_desc *grp = |
| 490 | &priv->soc->grps[group_selector]; |
| 491 | int i, ret; |
| 492 | |
| 493 | for (i = 0; i < grp->num_pins; i++) { |
| 494 | ret = mtk_pinconf_set(dev, grp->pins[i], param, arg); |
| 495 | if (ret) |
| 496 | return ret; |
| 497 | } |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | #endif |
| 502 | |
| 503 | const struct pinctrl_ops mtk_pinctrl_ops = { |
| 504 | .get_pins_count = mtk_get_pins_count, |
| 505 | .get_pin_name = mtk_get_pin_name, |
| 506 | .get_groups_count = mtk_get_groups_count, |
| 507 | .get_group_name = mtk_get_group_name, |
| 508 | .get_functions_count = mtk_get_functions_count, |
| 509 | .get_function_name = mtk_get_function_name, |
| 510 | .pinmux_group_set = mtk_pinmux_group_set, |
| 511 | #if CONFIG_IS_ENABLED(PINCONF) |
| 512 | .pinconf_num_params = ARRAY_SIZE(mtk_conf_params), |
| 513 | .pinconf_params = mtk_conf_params, |
| 514 | .pinconf_set = mtk_pinconf_set, |
| 515 | .pinconf_group_set = mtk_pinconf_group_set, |
| 516 | #endif |
| 517 | .set_state = pinctrl_generic_set_state, |
| 518 | }; |
| 519 | |
| 520 | static int mtk_gpio_get(struct udevice *dev, unsigned int off) |
| 521 | { |
| 522 | int val, err; |
| 523 | |
| 524 | err = mtk_hw_get_value(dev->parent, off, PINCTRL_PIN_REG_DI, &val); |
| 525 | if (err) |
| 526 | return err; |
| 527 | |
| 528 | return !!val; |
| 529 | } |
| 530 | |
| 531 | static int mtk_gpio_set(struct udevice *dev, unsigned int off, int val) |
| 532 | { |
| 533 | return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_DO, !!val); |
| 534 | } |
| 535 | |
| 536 | static int mtk_gpio_get_direction(struct udevice *dev, unsigned int off) |
| 537 | { |
| 538 | int val, err; |
| 539 | |
| 540 | err = mtk_hw_get_value(dev->parent, off, PINCTRL_PIN_REG_DIR, &val); |
| 541 | if (err) |
| 542 | return err; |
| 543 | |
| 544 | return val ? GPIOF_OUTPUT : GPIOF_INPUT; |
| 545 | } |
| 546 | |
| 547 | static int mtk_gpio_direction_input(struct udevice *dev, unsigned int off) |
| 548 | { |
| 549 | return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_DIR, 0); |
| 550 | } |
| 551 | |
| 552 | static int mtk_gpio_direction_output(struct udevice *dev, |
| 553 | unsigned int off, int val) |
| 554 | { |
| 555 | mtk_gpio_set(dev, off, val); |
| 556 | |
| 557 | /* And set the requested value */ |
| 558 | return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_DIR, 1); |
| 559 | } |
| 560 | |
| 561 | static int mtk_gpio_request(struct udevice *dev, unsigned int off, |
| 562 | const char *label) |
| 563 | { |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 564 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev->parent); |
| 565 | |
| 566 | return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_MODE, |
| 567 | priv->soc->gpio_mode); |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static int mtk_gpio_probe(struct udevice *dev) |
| 571 | { |
| 572 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev->parent); |
| 573 | struct gpio_dev_priv *uc_priv; |
| 574 | |
| 575 | uc_priv = dev_get_uclass_priv(dev); |
| 576 | uc_priv->bank_name = priv->soc->name; |
| 577 | uc_priv->gpio_count = priv->soc->npins; |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | static const struct dm_gpio_ops mtk_gpio_ops = { |
| 583 | .request = mtk_gpio_request, |
| 584 | .set_value = mtk_gpio_set, |
| 585 | .get_value = mtk_gpio_get, |
| 586 | .get_function = mtk_gpio_get_direction, |
| 587 | .direction_input = mtk_gpio_direction_input, |
| 588 | .direction_output = mtk_gpio_direction_output, |
| 589 | }; |
| 590 | |
| 591 | static struct driver mtk_gpio_driver = { |
| 592 | .name = "mediatek_gpio", |
| 593 | .id = UCLASS_GPIO, |
| 594 | .probe = mtk_gpio_probe, |
| 595 | .ops = &mtk_gpio_ops, |
| 596 | }; |
| 597 | |
| 598 | static int mtk_gpiochip_register(struct udevice *parent) |
| 599 | { |
| 600 | struct uclass_driver *drv; |
| 601 | struct udevice *dev; |
| 602 | int ret; |
| 603 | ofnode node; |
| 604 | |
| 605 | drv = lists_uclass_lookup(UCLASS_GPIO); |
| 606 | if (!drv) |
| 607 | return -ENOENT; |
| 608 | |
| 609 | dev_for_each_subnode(node, parent) |
| 610 | if (ofnode_read_bool(node, "gpio-controller")) { |
| 611 | ret = 0; |
| 612 | break; |
| 613 | } |
| 614 | |
| 615 | if (ret) |
| 616 | return ret; |
| 617 | |
| 618 | ret = device_bind_with_driver_data(parent, &mtk_gpio_driver, |
| 619 | "mediatek_gpio", 0, node, |
| 620 | &dev); |
| 621 | if (ret) |
| 622 | return ret; |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | int mtk_pinctrl_common_probe(struct udevice *dev, |
| 628 | struct mtk_pinctrl_soc *soc) |
| 629 | { |
| 630 | struct mtk_pinctrl_priv *priv = dev_get_priv(dev); |
| 631 | int ret; |
| 632 | |
| 633 | priv->base = dev_read_addr_ptr(dev); |
Sean Anderson | 42db70b | 2020-06-24 06:41:13 -0400 | [diff] [blame] | 634 | if (!priv->base) |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 635 | return -EINVAL; |
| 636 | |
| 637 | priv->soc = soc; |
| 638 | |
| 639 | ret = mtk_gpiochip_register(dev); |
| 640 | if (ret) |
| 641 | return ret; |
| 642 | |
| 643 | return 0; |
| 644 | } |