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