1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Pinctrl for Cirrus Logic Madera codecs 4 * 5 * Copyright (C) 2016-2018 Cirrus Logic 6 */ 7 8 #include <linux/err.h> 9 #include <linux/module.h> 10 #include <linux/platform_device.h> 11 #include <linux/regmap.h> 12 #include <linux/slab.h> 13 #include <linux/pinctrl/machine.h> 14 #include <linux/pinctrl/pinctrl.h> 15 #include <linux/pinctrl/pinmux.h> 16 #include <linux/pinctrl/pinconf.h> 17 #include <linux/pinctrl/pinconf-generic.h> 18 19 #include <linux/mfd/madera/core.h> 20 #include <linux/mfd/madera/registers.h> 21 22 #include "../pinctrl-utils.h" 23 24 #include "pinctrl-madera.h" 25 26 /* 27 * Use pin GPIO names for consistency 28 * NOTE: IDs are zero-indexed for coding convenience 29 */ 30 static const struct pinctrl_pin_desc madera_pins[] = { 31 PINCTRL_PIN(0, "gpio1"), 32 PINCTRL_PIN(1, "gpio2"), 33 PINCTRL_PIN(2, "gpio3"), 34 PINCTRL_PIN(3, "gpio4"), 35 PINCTRL_PIN(4, "gpio5"), 36 PINCTRL_PIN(5, "gpio6"), 37 PINCTRL_PIN(6, "gpio7"), 38 PINCTRL_PIN(7, "gpio8"), 39 PINCTRL_PIN(8, "gpio9"), 40 PINCTRL_PIN(9, "gpio10"), 41 PINCTRL_PIN(10, "gpio11"), 42 PINCTRL_PIN(11, "gpio12"), 43 PINCTRL_PIN(12, "gpio13"), 44 PINCTRL_PIN(13, "gpio14"), 45 PINCTRL_PIN(14, "gpio15"), 46 PINCTRL_PIN(15, "gpio16"), 47 PINCTRL_PIN(16, "gpio17"), 48 PINCTRL_PIN(17, "gpio18"), 49 PINCTRL_PIN(18, "gpio19"), 50 PINCTRL_PIN(19, "gpio20"), 51 PINCTRL_PIN(20, "gpio21"), 52 PINCTRL_PIN(21, "gpio22"), 53 PINCTRL_PIN(22, "gpio23"), 54 PINCTRL_PIN(23, "gpio24"), 55 PINCTRL_PIN(24, "gpio25"), 56 PINCTRL_PIN(25, "gpio26"), 57 PINCTRL_PIN(26, "gpio27"), 58 PINCTRL_PIN(27, "gpio28"), 59 PINCTRL_PIN(28, "gpio29"), 60 PINCTRL_PIN(29, "gpio30"), 61 PINCTRL_PIN(30, "gpio31"), 62 PINCTRL_PIN(31, "gpio32"), 63 PINCTRL_PIN(32, "gpio33"), 64 PINCTRL_PIN(33, "gpio34"), 65 PINCTRL_PIN(34, "gpio35"), 66 PINCTRL_PIN(35, "gpio36"), 67 PINCTRL_PIN(36, "gpio37"), 68 PINCTRL_PIN(37, "gpio38"), 69 PINCTRL_PIN(38, "gpio39"), 70 PINCTRL_PIN(39, "gpio40"), 71 }; 72 73 /* 74 * All single-pin functions can be mapped to any GPIO, however pinmux applies 75 * functions to pin groups and only those groups declared as supporting that 76 * function. To make this work we must put each pin in its own dummy group so 77 * that the functions can be described as applying to all pins. 78 * Since these do not correspond to anything in the actual hardware - they are 79 * merely an adaptation to pinctrl's view of the world - we use the same name 80 * as the pin to avoid confusion when comparing with datasheet instructions 81 */ 82 static const char * const madera_pin_single_group_names[] = { 83 "gpio1", "gpio2", "gpio3", "gpio4", "gpio5", "gpio6", "gpio7", 84 "gpio8", "gpio9", "gpio10", "gpio11", "gpio12", "gpio13", "gpio14", 85 "gpio15", "gpio16", "gpio17", "gpio18", "gpio19", "gpio20", "gpio21", 86 "gpio22", "gpio23", "gpio24", "gpio25", "gpio26", "gpio27", "gpio28", 87 "gpio29", "gpio30", "gpio31", "gpio32", "gpio33", "gpio34", "gpio35", 88 "gpio36", "gpio37", "gpio38", "gpio39", "gpio40", 89 }; 90 91 /* set of pin numbers for single-pin groups, zero-indexed */ 92 static const unsigned int madera_pin_single_group_pins[] = { 93 0, 1, 2, 3, 4, 5, 6, 94 7, 8, 9, 10, 11, 12, 13, 95 14, 15, 16, 17, 18, 19, 20, 96 21, 22, 23, 24, 25, 26, 27, 97 28, 29, 30, 31, 32, 33, 34, 98 35, 36, 37, 38, 39, 99 }; 100 101 static const char * const madera_aif1_group_names[] = { "aif1" }; 102 static const char * const madera_aif2_group_names[] = { "aif2" }; 103 static const char * const madera_aif3_group_names[] = { "aif3" }; 104 static const char * const madera_aif4_group_names[] = { "aif4" }; 105 static const char * const madera_mif1_group_names[] = { "mif1" }; 106 static const char * const madera_mif2_group_names[] = { "mif2" }; 107 static const char * const madera_mif3_group_names[] = { "mif3" }; 108 static const char * const madera_dmic3_group_names[] = { "dmic3" }; 109 static const char * const madera_dmic4_group_names[] = { "dmic4" }; 110 static const char * const madera_dmic5_group_names[] = { "dmic5" }; 111 static const char * const madera_dmic6_group_names[] = { "dmic6" }; 112 static const char * const madera_spk1_group_names[] = { "pdmspk1" }; 113 static const char * const madera_spk2_group_names[] = { "pdmspk2" }; 114 115 /* 116 * alt-functions always apply to a single pin group, other functions always 117 * apply to all pins 118 */ 119 static const struct { 120 const char *name; 121 const char * const *group_names; 122 u32 func; 123 } madera_mux_funcs[] = { 124 { 125 .name = "aif1", 126 .group_names = madera_aif1_group_names, 127 .func = 0x000 128 }, 129 { 130 .name = "aif2", 131 .group_names = madera_aif2_group_names, 132 .func = 0x000 133 }, 134 { 135 .name = "aif3", 136 .group_names = madera_aif3_group_names, 137 .func = 0x000 138 }, 139 { 140 .name = "aif4", 141 .group_names = madera_aif4_group_names, 142 .func = 0x000 143 }, 144 { 145 .name = "mif1", 146 .group_names = madera_mif1_group_names, 147 .func = 0x000 148 }, 149 { 150 .name = "mif2", 151 .group_names = madera_mif2_group_names, 152 .func = 0x000 153 }, 154 { 155 .name = "mif3", 156 .group_names = madera_mif3_group_names, 157 .func = 0x000 158 }, 159 { 160 .name = "dmic3", 161 .group_names = madera_dmic3_group_names, 162 .func = 0x000 163 }, 164 { 165 .name = "dmic4", 166 .group_names = madera_dmic4_group_names, 167 .func = 0x000 168 }, 169 { 170 .name = "dmic5", 171 .group_names = madera_dmic5_group_names, 172 .func = 0x000 173 }, 174 { 175 .name = "dmic6", 176 .group_names = madera_dmic6_group_names, 177 .func = 0x000 178 }, 179 { 180 .name = "pdmspk1", 181 .group_names = madera_spk1_group_names, 182 .func = 0x000 183 }, 184 { 185 .name = "pdmspk2", 186 .group_names = madera_spk2_group_names, 187 .func = 0x000 188 }, 189 { 190 .name = "io", 191 .group_names = madera_pin_single_group_names, 192 .func = 0x001 193 }, 194 { 195 .name = "dsp-gpio", 196 .group_names = madera_pin_single_group_names, 197 .func = 0x002 198 }, 199 { 200 .name = "irq1", 201 .group_names = madera_pin_single_group_names, 202 .func = 0x003 203 }, 204 { 205 .name = "irq2", 206 .group_names = madera_pin_single_group_names, 207 .func = 0x004 208 }, 209 { 210 .name = "fll1-clk", 211 .group_names = madera_pin_single_group_names, 212 .func = 0x010 213 }, 214 { 215 .name = "fll2-clk", 216 .group_names = madera_pin_single_group_names, 217 .func = 0x011 218 }, 219 { 220 .name = "fll3-clk", 221 .group_names = madera_pin_single_group_names, 222 .func = 0x012 223 }, 224 { 225 .name = "fllao-clk", 226 .group_names = madera_pin_single_group_names, 227 .func = 0x013 228 }, 229 { 230 .name = "fll1-lock", 231 .group_names = madera_pin_single_group_names, 232 .func = 0x018 233 }, 234 { 235 .name = "fll2-lock", 236 .group_names = madera_pin_single_group_names, 237 .func = 0x019 238 }, 239 { 240 .name = "fll3-lock", 241 .group_names = madera_pin_single_group_names, 242 .func = 0x01a 243 }, 244 { 245 .name = "fllao-lock", 246 .group_names = madera_pin_single_group_names, 247 .func = 0x01b 248 }, 249 { 250 .name = "opclk", 251 .group_names = madera_pin_single_group_names, 252 .func = 0x040 253 }, 254 { 255 .name = "opclk-async", 256 .group_names = madera_pin_single_group_names, 257 .func = 0x041 258 }, 259 { 260 .name = "pwm1", 261 .group_names = madera_pin_single_group_names, 262 .func = 0x048 263 }, 264 { 265 .name = "pwm2", 266 .group_names = madera_pin_single_group_names, 267 .func = 0x049 268 }, 269 { 270 .name = "spdif", 271 .group_names = madera_pin_single_group_names, 272 .func = 0x04c 273 }, 274 { 275 .name = "asrc1-in1-lock", 276 .group_names = madera_pin_single_group_names, 277 .func = 0x088 278 }, 279 { 280 .name = "asrc1-in2-lock", 281 .group_names = madera_pin_single_group_names, 282 .func = 0x089 283 }, 284 { 285 .name = "asrc2-in1-lock", 286 .group_names = madera_pin_single_group_names, 287 .func = 0x08a 288 }, 289 { 290 .name = "asrc2-in2-lock", 291 .group_names = madera_pin_single_group_names, 292 .func = 0x08b 293 }, 294 { 295 .name = "spkl-short-circuit", 296 .group_names = madera_pin_single_group_names, 297 .func = 0x0b6 298 }, 299 { 300 .name = "spkr-short-circuit", 301 .group_names = madera_pin_single_group_names, 302 .func = 0x0b7 303 }, 304 { 305 .name = "spk-shutdown", 306 .group_names = madera_pin_single_group_names, 307 .func = 0x0e0 308 }, 309 { 310 .name = "spk-overheat-shutdown", 311 .group_names = madera_pin_single_group_names, 312 .func = 0x0e1 313 }, 314 { 315 .name = "spk-overheat-warn", 316 .group_names = madera_pin_single_group_names, 317 .func = 0x0e2 318 }, 319 { 320 .name = "timer1-sts", 321 .group_names = madera_pin_single_group_names, 322 .func = 0x140 323 }, 324 { 325 .name = "timer2-sts", 326 .group_names = madera_pin_single_group_names, 327 .func = 0x141 328 }, 329 { 330 .name = "timer3-sts", 331 .group_names = madera_pin_single_group_names, 332 .func = 0x142 333 }, 334 { 335 .name = "timer4-sts", 336 .group_names = madera_pin_single_group_names, 337 .func = 0x143 338 }, 339 { 340 .name = "timer5-sts", 341 .group_names = madera_pin_single_group_names, 342 .func = 0x144 343 }, 344 { 345 .name = "timer6-sts", 346 .group_names = madera_pin_single_group_names, 347 .func = 0x145 348 }, 349 { 350 .name = "timer7-sts", 351 .group_names = madera_pin_single_group_names, 352 .func = 0x146 353 }, 354 { 355 .name = "timer8-sts", 356 .group_names = madera_pin_single_group_names, 357 .func = 0x147 358 }, 359 { 360 .name = "log1-fifo-ne", 361 .group_names = madera_pin_single_group_names, 362 .func = 0x150 363 }, 364 { 365 .name = "log2-fifo-ne", 366 .group_names = madera_pin_single_group_names, 367 .func = 0x151 368 }, 369 { 370 .name = "log3-fifo-ne", 371 .group_names = madera_pin_single_group_names, 372 .func = 0x152 373 }, 374 { 375 .name = "log4-fifo-ne", 376 .group_names = madera_pin_single_group_names, 377 .func = 0x153 378 }, 379 { 380 .name = "log5-fifo-ne", 381 .group_names = madera_pin_single_group_names, 382 .func = 0x154 383 }, 384 { 385 .name = "log6-fifo-ne", 386 .group_names = madera_pin_single_group_names, 387 .func = 0x155 388 }, 389 { 390 .name = "log7-fifo-ne", 391 .group_names = madera_pin_single_group_names, 392 .func = 0x156 393 }, 394 { 395 .name = "log8-fifo-ne", 396 .group_names = madera_pin_single_group_names, 397 .func = 0x157 398 }, 399 }; 400 401 static u16 madera_pin_make_drv_str(struct madera_pin_private *priv, 402 unsigned int milliamps) 403 { 404 switch (milliamps) { 405 case 4: 406 return 0; 407 case 8: 408 return 2 << MADERA_GP1_DRV_STR_SHIFT; 409 default: 410 break; 411 } 412 413 dev_warn(priv->dev, "%u mA not a valid drive strength", milliamps); 414 415 return 0; 416 } 417 418 static unsigned int madera_pin_unmake_drv_str(struct madera_pin_private *priv, 419 u16 regval) 420 { 421 regval = (regval & MADERA_GP1_DRV_STR_MASK) >> MADERA_GP1_DRV_STR_SHIFT; 422 423 switch (regval) { 424 case 0: 425 return 4; 426 case 2: 427 return 8; 428 default: 429 return 0; 430 } 431 } 432 433 static int madera_get_groups_count(struct pinctrl_dev *pctldev) 434 { 435 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev); 436 437 /* Number of alt function groups plus number of single-pin groups */ 438 return priv->chip->n_pin_groups + priv->chip->n_pins; 439 } 440 441 static const char *madera_get_group_name(struct pinctrl_dev *pctldev, 442 unsigned int selector) 443 { 444 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev); 445 446 if (selector < priv->chip->n_pin_groups) 447 return priv->chip->pin_groups[selector].name; 448 449 selector -= priv->chip->n_pin_groups; 450 return madera_pin_single_group_names[selector]; 451 } 452 453 static int madera_get_group_pins(struct pinctrl_dev *pctldev, 454 unsigned int selector, 455 const unsigned int **pins, 456 unsigned int *num_pins) 457 { 458 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev); 459 460 if (selector < priv->chip->n_pin_groups) { 461 *pins = priv->chip->pin_groups[selector].pins; 462 *num_pins = priv->chip->pin_groups[selector].n_pins; 463 } else { 464 /* return the dummy group for a single pin */ 465 selector -= priv->chip->n_pin_groups; 466 *pins = &madera_pin_single_group_pins[selector]; 467 *num_pins = 1; 468 } 469 return 0; 470 } 471 472 static void madera_pin_dbg_show_fn(struct madera_pin_private *priv, 473 struct seq_file *s, 474 unsigned int pin, unsigned int fn) 475 { 476 const struct madera_pin_chip *chip = priv->chip; 477 int i, g_pin; 478 479 if (fn != 0) { 480 for (i = 0; i < ARRAY_SIZE(madera_mux_funcs); ++i) { 481 if (madera_mux_funcs[i].func == fn) { 482 seq_printf(s, " FN=%s", 483 madera_mux_funcs[i].name); 484 return; 485 } 486 } 487 return; /* ignore unknown function values */ 488 } 489 490 /* alt function */ 491 for (i = 0; i < chip->n_pin_groups; ++i) { 492 for (g_pin = 0; g_pin < chip->pin_groups[i].n_pins; ++g_pin) { 493 if (chip->pin_groups[i].pins[g_pin] == pin) { 494 seq_printf(s, " FN=%s", 495 chip->pin_groups[i].name); 496 return; 497 } 498 } 499 } 500 } 501 502 static void __maybe_unused madera_pin_dbg_show(struct pinctrl_dev *pctldev, 503 struct seq_file *s, 504 unsigned int pin) 505 { 506 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev); 507 unsigned int conf[2]; 508 unsigned int reg = MADERA_GPIO1_CTRL_1 + (2 * pin); 509 unsigned int fn; 510 int ret; 511 512 ret = regmap_read(priv->madera->regmap, reg, &conf[0]); 513 if (ret) 514 return; 515 516 ret = regmap_read(priv->madera->regmap, reg + 1, &conf[1]); 517 if (ret) 518 return; 519 520 seq_printf(s, "%04x:%04x", conf[0], conf[1]); 521 522 fn = (conf[0] & MADERA_GP1_FN_MASK) >> MADERA_GP1_FN_SHIFT; 523 madera_pin_dbg_show_fn(priv, s, pin, fn); 524 525 /* State of direction bit is only relevant if function==1 */ 526 if (fn == 1) { 527 if (conf[1] & MADERA_GP1_DIR_MASK) 528 seq_puts(s, " IN"); 529 else 530 seq_puts(s, " OUT"); 531 } 532 533 if (conf[1] & MADERA_GP1_PU_MASK) 534 seq_puts(s, " PU"); 535 536 if (conf[1] & MADERA_GP1_PD_MASK) 537 seq_puts(s, " PD"); 538 539 if (conf[0] & MADERA_GP1_DB_MASK) 540 seq_puts(s, " DB"); 541 542 if (conf[0] & MADERA_GP1_OP_CFG_MASK) 543 seq_puts(s, " OD"); 544 else 545 seq_puts(s, " CMOS"); 546 547 seq_printf(s, " DRV=%umA", madera_pin_unmake_drv_str(priv, conf[1])); 548 549 if (conf[0] & MADERA_GP1_IP_CFG_MASK) 550 seq_puts(s, " SCHMITT"); 551 } 552 553 554 static const struct pinctrl_ops madera_pin_group_ops = { 555 .get_groups_count = madera_get_groups_count, 556 .get_group_name = madera_get_group_name, 557 .get_group_pins = madera_get_group_pins, 558 #if IS_ENABLED(CONFIG_OF) 559 .dt_node_to_map = pinconf_generic_dt_node_to_map_all, 560 .dt_free_map = pinctrl_utils_free_map, 561 #endif 562 #if IS_ENABLED(CONFIG_DEBUG_FS) 563 .pin_dbg_show = madera_pin_dbg_show, 564 #endif 565 }; 566 567 static int madera_mux_get_funcs_count(struct pinctrl_dev *pctldev) 568 { 569 return ARRAY_SIZE(madera_mux_funcs); 570 } 571 572 static const char *madera_mux_get_func_name(struct pinctrl_dev *pctldev, 573 unsigned int selector) 574 { 575 return madera_mux_funcs[selector].name; 576 } 577 578 static int madera_mux_get_groups(struct pinctrl_dev *pctldev, 579 unsigned int selector, 580 const char * const **groups, 581 unsigned int * const num_groups) 582 { 583 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev); 584 585 *groups = madera_mux_funcs[selector].group_names; 586 587 if (madera_mux_funcs[selector].func == 0) { 588 /* alt func always maps to a single group */ 589 *num_groups = 1; 590 } else { 591 /* other funcs map to all available gpio pins */ 592 *num_groups = priv->chip->n_pins; 593 } 594 595 return 0; 596 } 597 598 static int madera_mux_set_mux(struct pinctrl_dev *pctldev, 599 unsigned int selector, 600 unsigned int group) 601 { 602 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev); 603 struct madera *madera = priv->madera; 604 const struct madera_pin_groups *pin_group = priv->chip->pin_groups; 605 unsigned int n_chip_groups = priv->chip->n_pin_groups; 606 const char *func_name = madera_mux_funcs[selector].name; 607 unsigned int reg; 608 int i, ret = 0; 609 610 dev_dbg(priv->dev, "%s selecting %u (%s) for group %u (%s)\n", 611 __func__, selector, func_name, group, 612 madera_get_group_name(pctldev, group)); 613 614 if (madera_mux_funcs[selector].func == 0) { 615 /* alt func pin assignments are codec-specific */ 616 for (i = 0; i < n_chip_groups; ++i) { 617 if (strcmp(func_name, pin_group->name) == 0) 618 break; 619 620 ++pin_group; 621 } 622 623 if (i == n_chip_groups) 624 return -EINVAL; 625 626 for (i = 0; i < pin_group->n_pins; ++i) { 627 reg = MADERA_GPIO1_CTRL_1 + (2 * pin_group->pins[i]); 628 629 dev_dbg(priv->dev, "%s setting 0x%x func bits to 0\n", 630 __func__, reg); 631 632 ret = regmap_update_bits(madera->regmap, reg, 633 MADERA_GP1_FN_MASK, 0); 634 if (ret) 635 break; 636 637 } 638 } else { 639 /* 640 * for other funcs the group will be the gpio number and will 641 * be offset by the number of chip-specific functions at the 642 * start of the group list 643 */ 644 group -= n_chip_groups; 645 reg = MADERA_GPIO1_CTRL_1 + (2 * group); 646 647 dev_dbg(priv->dev, "%s setting 0x%x func bits to 0x%x\n", 648 __func__, reg, madera_mux_funcs[selector].func); 649 650 ret = regmap_update_bits(madera->regmap, 651 reg, 652 MADERA_GP1_FN_MASK, 653 madera_mux_funcs[selector].func); 654 } 655 656 if (ret) 657 dev_err(priv->dev, "Failed to write to 0x%x (%d)\n", reg, ret); 658 659 return ret; 660 } 661 662 static int madera_gpio_set_direction(struct pinctrl_dev *pctldev, 663 struct pinctrl_gpio_range *range, 664 unsigned int offset, 665 bool input) 666 { 667 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev); 668 struct madera *madera = priv->madera; 669 unsigned int reg = MADERA_GPIO1_CTRL_2 + (2 * offset); 670 unsigned int val; 671 int ret; 672 673 if (input) 674 val = MADERA_GP1_DIR; 675 else 676 val = 0; 677 678 ret = regmap_update_bits(madera->regmap, reg, MADERA_GP1_DIR_MASK, val); 679 if (ret) 680 dev_err(priv->dev, "Failed to write to 0x%x (%d)\n", reg, ret); 681 682 return ret; 683 } 684 685 static int madera_gpio_request_enable(struct pinctrl_dev *pctldev, 686 struct pinctrl_gpio_range *range, 687 unsigned int offset) 688 { 689 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev); 690 struct madera *madera = priv->madera; 691 unsigned int reg = MADERA_GPIO1_CTRL_1 + (2 * offset); 692 int ret; 693 694 /* put the pin into GPIO mode */ 695 ret = regmap_update_bits(madera->regmap, reg, MADERA_GP1_FN_MASK, 1); 696 if (ret) 697 dev_err(priv->dev, "Failed to write to 0x%x (%d)\n", reg, ret); 698 699 return ret; 700 } 701 702 static void madera_gpio_disable_free(struct pinctrl_dev *pctldev, 703 struct pinctrl_gpio_range *range, 704 unsigned int offset) 705 { 706 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev); 707 struct madera *madera = priv->madera; 708 unsigned int reg = MADERA_GPIO1_CTRL_1 + (2 * offset); 709 int ret; 710 711 /* disable GPIO by setting to GPIO IN */ 712 madera_gpio_set_direction(pctldev, range, offset, true); 713 714 ret = regmap_update_bits(madera->regmap, reg, MADERA_GP1_FN_MASK, 1); 715 if (ret) 716 dev_err(priv->dev, "Failed to write to 0x%x (%d)\n", reg, ret); 717 } 718 719 static const struct pinmux_ops madera_pin_mux_ops = { 720 .get_functions_count = madera_mux_get_funcs_count, 721 .get_function_name = madera_mux_get_func_name, 722 .get_function_groups = madera_mux_get_groups, 723 .set_mux = madera_mux_set_mux, 724 .gpio_request_enable = madera_gpio_request_enable, 725 .gpio_disable_free = madera_gpio_disable_free, 726 .gpio_set_direction = madera_gpio_set_direction, 727 .strict = true, /* GPIO and other functions are exclusive */ 728 }; 729 730 static int madera_pin_conf_get(struct pinctrl_dev *pctldev, unsigned int pin, 731 unsigned long *config) 732 { 733 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev); 734 unsigned int param = pinconf_to_config_param(*config); 735 unsigned int result = 0; 736 unsigned int reg = MADERA_GPIO1_CTRL_1 + (2 * pin); 737 unsigned int conf[2]; 738 int ret; 739 740 ret = regmap_read(priv->madera->regmap, reg, &conf[0]); 741 if (!ret) 742 ret = regmap_read(priv->madera->regmap, reg + 1, &conf[1]); 743 744 if (ret) { 745 dev_err(priv->dev, "Failed to read GP%d conf (%d)\n", 746 pin + 1, ret); 747 return ret; 748 } 749 750 switch (param) { 751 case PIN_CONFIG_BIAS_BUS_HOLD: 752 conf[1] &= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK; 753 if (conf[1] == (MADERA_GP1_PU | MADERA_GP1_PD)) 754 result = 1; 755 break; 756 case PIN_CONFIG_BIAS_DISABLE: 757 conf[1] &= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK; 758 if (!conf[1]) 759 result = 1; 760 break; 761 case PIN_CONFIG_BIAS_PULL_DOWN: 762 conf[1] &= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK; 763 if (conf[1] == MADERA_GP1_PD_MASK) 764 result = 1; 765 break; 766 case PIN_CONFIG_BIAS_PULL_UP: 767 conf[1] &= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK; 768 if (conf[1] == MADERA_GP1_PU_MASK) 769 result = 1; 770 break; 771 case PIN_CONFIG_DRIVE_OPEN_DRAIN: 772 if (conf[0] & MADERA_GP1_OP_CFG_MASK) 773 result = 1; 774 break; 775 case PIN_CONFIG_DRIVE_PUSH_PULL: 776 if (!(conf[0] & MADERA_GP1_OP_CFG_MASK)) 777 result = 1; 778 break; 779 case PIN_CONFIG_DRIVE_STRENGTH: 780 result = madera_pin_unmake_drv_str(priv, conf[1]); 781 break; 782 case PIN_CONFIG_INPUT_DEBOUNCE: 783 if (conf[0] & MADERA_GP1_DB_MASK) 784 result = 1; 785 break; 786 case PIN_CONFIG_INPUT_ENABLE: 787 if (conf[0] & MADERA_GP1_DIR_MASK) 788 result = 1; 789 break; 790 case PIN_CONFIG_INPUT_SCHMITT: 791 case PIN_CONFIG_INPUT_SCHMITT_ENABLE: 792 if (conf[0] & MADERA_GP1_IP_CFG_MASK) 793 result = 1; 794 break; 795 case PIN_CONFIG_OUTPUT: 796 if ((conf[1] & MADERA_GP1_DIR_MASK) && 797 (conf[0] & MADERA_GP1_LVL_MASK)) 798 result = 1; 799 break; 800 default: 801 return -ENOTSUPP; 802 } 803 804 *config = pinconf_to_config_packed(param, result); 805 806 return 0; 807 } 808 809 static int madera_pin_conf_set(struct pinctrl_dev *pctldev, unsigned int pin, 810 unsigned long *configs, unsigned int num_configs) 811 { 812 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev); 813 u16 conf[2] = {0, 0}; 814 u16 mask[2] = {0, 0}; 815 unsigned int reg = MADERA_GPIO1_CTRL_1 + (2 * pin); 816 unsigned int val; 817 int ret; 818 819 while (num_configs) { 820 dev_dbg(priv->dev, "%s config 0x%lx\n", __func__, *configs); 821 822 switch (pinconf_to_config_param(*configs)) { 823 case PIN_CONFIG_BIAS_BUS_HOLD: 824 mask[1] |= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK; 825 conf[1] |= MADERA_GP1_PU | MADERA_GP1_PD; 826 break; 827 case PIN_CONFIG_BIAS_DISABLE: 828 mask[1] |= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK; 829 conf[1] &= ~(MADERA_GP1_PU | MADERA_GP1_PD); 830 break; 831 case PIN_CONFIG_BIAS_PULL_DOWN: 832 mask[1] |= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK; 833 conf[1] |= MADERA_GP1_PD; 834 conf[1] &= ~MADERA_GP1_PU; 835 break; 836 case PIN_CONFIG_BIAS_PULL_UP: 837 mask[1] |= MADERA_GP1_PU_MASK | MADERA_GP1_PD_MASK; 838 conf[1] |= MADERA_GP1_PU; 839 conf[1] &= ~MADERA_GP1_PD; 840 break; 841 case PIN_CONFIG_DRIVE_OPEN_DRAIN: 842 mask[0] |= MADERA_GP1_OP_CFG_MASK; 843 conf[0] |= MADERA_GP1_OP_CFG; 844 break; 845 case PIN_CONFIG_DRIVE_PUSH_PULL: 846 mask[0] |= MADERA_GP1_OP_CFG_MASK; 847 conf[0] &= ~MADERA_GP1_OP_CFG; 848 break; 849 case PIN_CONFIG_DRIVE_STRENGTH: 850 val = pinconf_to_config_argument(*configs); 851 mask[1] |= MADERA_GP1_DRV_STR_MASK; 852 conf[1] &= ~MADERA_GP1_DRV_STR_MASK; 853 conf[1] |= madera_pin_make_drv_str(priv, val); 854 break; 855 case PIN_CONFIG_INPUT_DEBOUNCE: 856 mask[0] |= MADERA_GP1_DB_MASK; 857 858 /* 859 * we can't configure debounce time per-pin so value 860 * is just a flag 861 */ 862 val = pinconf_to_config_argument(*configs); 863 if (val) 864 conf[0] |= MADERA_GP1_DB; 865 else 866 conf[0] &= ~MADERA_GP1_DB; 867 break; 868 case PIN_CONFIG_INPUT_ENABLE: 869 val = pinconf_to_config_argument(*configs); 870 mask[1] |= MADERA_GP1_DIR_MASK; 871 if (val) 872 conf[1] |= MADERA_GP1_DIR; 873 else 874 conf[1] &= ~MADERA_GP1_DIR; 875 break; 876 case PIN_CONFIG_INPUT_SCHMITT: 877 val = pinconf_to_config_argument(*configs); 878 mask[0] |= MADERA_GP1_IP_CFG; 879 if (val) 880 conf[0] |= MADERA_GP1_IP_CFG; 881 else 882 conf[0] &= ~MADERA_GP1_IP_CFG; 883 884 mask[1] |= MADERA_GP1_DIR_MASK; 885 conf[1] |= MADERA_GP1_DIR; 886 break; 887 case PIN_CONFIG_INPUT_SCHMITT_ENABLE: 888 mask[0] |= MADERA_GP1_IP_CFG; 889 conf[0] |= MADERA_GP1_IP_CFG; 890 mask[1] |= MADERA_GP1_DIR_MASK; 891 conf[1] |= MADERA_GP1_DIR; 892 break; 893 case PIN_CONFIG_OUTPUT: 894 val = pinconf_to_config_argument(*configs); 895 mask[0] |= MADERA_GP1_LVL_MASK; 896 if (val) 897 conf[0] |= MADERA_GP1_LVL; 898 else 899 conf[0] &= ~MADERA_GP1_LVL; 900 901 mask[1] |= MADERA_GP1_DIR_MASK; 902 conf[1] &= ~MADERA_GP1_DIR; 903 break; 904 default: 905 return -ENOTSUPP; 906 } 907 908 ++configs; 909 --num_configs; 910 } 911 912 dev_dbg(priv->dev, 913 "%s gpio%d 0x%x:0x%x 0x%x:0x%x\n", 914 __func__, pin + 1, reg, conf[0], reg + 1, conf[1]); 915 916 ret = regmap_update_bits(priv->madera->regmap, reg, mask[0], conf[0]); 917 if (ret) 918 goto err; 919 920 ++reg; 921 ret = regmap_update_bits(priv->madera->regmap, reg, mask[1], conf[1]); 922 if (ret) 923 goto err; 924 925 return 0; 926 927 err: 928 dev_err(priv->dev, 929 "Failed to write GPIO%d conf (%d) reg 0x%x\n", 930 pin + 1, ret, reg); 931 932 return ret; 933 } 934 935 static int madera_pin_conf_group_set(struct pinctrl_dev *pctldev, 936 unsigned int selector, 937 unsigned long *configs, 938 unsigned int num_configs) 939 { 940 struct madera_pin_private *priv = pinctrl_dev_get_drvdata(pctldev); 941 const struct madera_pin_groups *pin_group; 942 unsigned int n_groups = priv->chip->n_pin_groups; 943 int i, ret; 944 945 dev_dbg(priv->dev, "%s setting group %s\n", __func__, 946 madera_get_group_name(pctldev, selector)); 947 948 if (selector >= n_groups) { 949 /* group is a single pin, convert to pin number and set */ 950 return madera_pin_conf_set(pctldev, 951 selector - n_groups, 952 configs, 953 num_configs); 954 } else { 955 pin_group = &priv->chip->pin_groups[selector]; 956 957 for (i = 0; i < pin_group->n_pins; ++i) { 958 ret = madera_pin_conf_set(pctldev, 959 pin_group->pins[i], 960 configs, 961 num_configs); 962 if (ret) 963 return ret; 964 } 965 } 966 967 return 0; 968 } 969 970 static const struct pinconf_ops madera_pin_conf_ops = { 971 .is_generic = true, 972 .pin_config_get = madera_pin_conf_get, 973 .pin_config_set = madera_pin_conf_set, 974 .pin_config_group_set = madera_pin_conf_group_set, 975 }; 976 977 static struct pinctrl_desc madera_pin_desc = { 978 .name = "madera-pinctrl", 979 .pins = madera_pins, 980 .pctlops = &madera_pin_group_ops, 981 .pmxops = &madera_pin_mux_ops, 982 .confops = &madera_pin_conf_ops, 983 .owner = THIS_MODULE, 984 }; 985 986 static int madera_pin_probe(struct platform_device *pdev) 987 { 988 struct madera *madera = dev_get_drvdata(pdev->dev.parent); 989 const struct madera_pdata *pdata = dev_get_platdata(madera->dev); 990 struct madera_pin_private *priv; 991 int ret; 992 993 BUILD_BUG_ON(ARRAY_SIZE(madera_pin_single_group_names) != 994 ARRAY_SIZE(madera_pin_single_group_pins)); 995 996 dev_dbg(&pdev->dev, "%s\n", __func__); 997 998 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 999 if (!priv) 1000 return -ENOMEM; 1001 1002 priv->dev = &pdev->dev; 1003 priv->madera = madera; 1004 pdev->dev.of_node = madera->dev->of_node; 1005 1006 switch (madera->type) { 1007 case CS47L35: 1008 if (IS_ENABLED(CONFIG_PINCTRL_CS47L35)) 1009 priv->chip = &cs47l35_pin_chip; 1010 break; 1011 case CS47L85: 1012 case WM1840: 1013 if (IS_ENABLED(CONFIG_PINCTRL_CS47L85)) 1014 priv->chip = &cs47l85_pin_chip; 1015 break; 1016 case CS47L90: 1017 case CS47L91: 1018 if (IS_ENABLED(CONFIG_PINCTRL_CS47L90)) 1019 priv->chip = &cs47l90_pin_chip; 1020 break; 1021 default: 1022 break; 1023 } 1024 1025 if (!priv->chip) 1026 return -ENODEV; 1027 1028 madera_pin_desc.npins = priv->chip->n_pins; 1029 1030 ret = devm_pinctrl_register_and_init(&pdev->dev, 1031 &madera_pin_desc, 1032 priv, 1033 &priv->pctl); 1034 if (ret) { 1035 dev_err(priv->dev, "Failed pinctrl register (%d)\n", ret); 1036 return ret; 1037 } 1038 1039 /* if the configuration is provided through pdata, apply it */ 1040 if (pdata && pdata->gpio_configs) { 1041 ret = pinctrl_register_mappings(pdata->gpio_configs, 1042 pdata->n_gpio_configs); 1043 if (ret) { 1044 dev_err(priv->dev, 1045 "Failed to register pdata mappings (%d)\n", 1046 ret); 1047 return ret; 1048 } 1049 } 1050 1051 ret = pinctrl_enable(priv->pctl); 1052 if (ret) { 1053 dev_err(priv->dev, "Failed to enable pinctrl (%d)\n", ret); 1054 return ret; 1055 } 1056 1057 dev_dbg(priv->dev, "pinctrl probed ok\n"); 1058 1059 return 0; 1060 } 1061 1062 static struct platform_driver madera_pin_driver = { 1063 .probe = madera_pin_probe, 1064 .driver = { 1065 .name = "madera-pinctrl", 1066 }, 1067 }; 1068 1069 module_platform_driver(madera_pin_driver); 1070 1071 MODULE_DESCRIPTION("Madera pinctrl driver"); 1072 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>"); 1073 MODULE_LICENSE("GPL v2"); 1074