1 /* 2 * pin-controller/pin-mux/pin-config/gpio-driver for Samsung's SoC's. 3 * 4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. 5 * http://www.samsung.com 6 * Copyright (c) 2012 Linaro Ltd 7 * http://www.linaro.org 8 * 9 * Author: Thomas Abraham <thomas.ab@samsung.com> 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2 of the License, or 14 * (at your option) any later version. 15 * 16 * This driver implements the Samsung pinctrl driver. It supports setting up of 17 * pinmux and pinconf configurations. The gpiolib interface is also included. 18 * External interrupt (gpio and wakeup) support are not included in this driver 19 * but provides extensions to which platform specific implementation of the gpio 20 * and wakeup interrupts can be hooked to. 21 */ 22 23 #include <linux/module.h> 24 #include <linux/platform_device.h> 25 #include <linux/io.h> 26 #include <linux/slab.h> 27 #include <linux/err.h> 28 #include <linux/gpio.h> 29 #include <linux/irqdomain.h> 30 #include <linux/spinlock.h> 31 #include <linux/syscore_ops.h> 32 33 #include "../core.h" 34 #include "pinctrl-samsung.h" 35 36 /* maximum number of the memory resources */ 37 #define SAMSUNG_PINCTRL_NUM_RESOURCES 2 38 39 /* list of all possible config options supported */ 40 static struct pin_config { 41 const char *property; 42 enum pincfg_type param; 43 } cfg_params[] = { 44 { "samsung,pin-pud", PINCFG_TYPE_PUD }, 45 { "samsung,pin-drv", PINCFG_TYPE_DRV }, 46 { "samsung,pin-con-pdn", PINCFG_TYPE_CON_PDN }, 47 { "samsung,pin-pud-pdn", PINCFG_TYPE_PUD_PDN }, 48 { "samsung,pin-val", PINCFG_TYPE_DAT }, 49 }; 50 51 /* Global list of devices (struct samsung_pinctrl_drv_data) */ 52 static LIST_HEAD(drvdata_list); 53 54 static unsigned int pin_base; 55 56 static int samsung_get_group_count(struct pinctrl_dev *pctldev) 57 { 58 struct samsung_pinctrl_drv_data *pmx = pinctrl_dev_get_drvdata(pctldev); 59 60 return pmx->nr_groups; 61 } 62 63 static const char *samsung_get_group_name(struct pinctrl_dev *pctldev, 64 unsigned group) 65 { 66 struct samsung_pinctrl_drv_data *pmx = pinctrl_dev_get_drvdata(pctldev); 67 68 return pmx->pin_groups[group].name; 69 } 70 71 static int samsung_get_group_pins(struct pinctrl_dev *pctldev, 72 unsigned group, 73 const unsigned **pins, 74 unsigned *num_pins) 75 { 76 struct samsung_pinctrl_drv_data *pmx = pinctrl_dev_get_drvdata(pctldev); 77 78 *pins = pmx->pin_groups[group].pins; 79 *num_pins = pmx->pin_groups[group].num_pins; 80 81 return 0; 82 } 83 84 static int reserve_map(struct device *dev, struct pinctrl_map **map, 85 unsigned *reserved_maps, unsigned *num_maps, 86 unsigned reserve) 87 { 88 unsigned old_num = *reserved_maps; 89 unsigned new_num = *num_maps + reserve; 90 struct pinctrl_map *new_map; 91 92 if (old_num >= new_num) 93 return 0; 94 95 new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL); 96 if (!new_map) { 97 dev_err(dev, "krealloc(map) failed\n"); 98 return -ENOMEM; 99 } 100 101 memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map)); 102 103 *map = new_map; 104 *reserved_maps = new_num; 105 106 return 0; 107 } 108 109 static int add_map_mux(struct pinctrl_map **map, unsigned *reserved_maps, 110 unsigned *num_maps, const char *group, 111 const char *function) 112 { 113 if (WARN_ON(*num_maps == *reserved_maps)) 114 return -ENOSPC; 115 116 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP; 117 (*map)[*num_maps].data.mux.group = group; 118 (*map)[*num_maps].data.mux.function = function; 119 (*num_maps)++; 120 121 return 0; 122 } 123 124 static int add_map_configs(struct device *dev, struct pinctrl_map **map, 125 unsigned *reserved_maps, unsigned *num_maps, 126 const char *group, unsigned long *configs, 127 unsigned num_configs) 128 { 129 unsigned long *dup_configs; 130 131 if (WARN_ON(*num_maps == *reserved_maps)) 132 return -ENOSPC; 133 134 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs), 135 GFP_KERNEL); 136 if (!dup_configs) { 137 dev_err(dev, "kmemdup(configs) failed\n"); 138 return -ENOMEM; 139 } 140 141 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_GROUP; 142 (*map)[*num_maps].data.configs.group_or_pin = group; 143 (*map)[*num_maps].data.configs.configs = dup_configs; 144 (*map)[*num_maps].data.configs.num_configs = num_configs; 145 (*num_maps)++; 146 147 return 0; 148 } 149 150 static int add_config(struct device *dev, unsigned long **configs, 151 unsigned *num_configs, unsigned long config) 152 { 153 unsigned old_num = *num_configs; 154 unsigned new_num = old_num + 1; 155 unsigned long *new_configs; 156 157 new_configs = krealloc(*configs, sizeof(*new_configs) * new_num, 158 GFP_KERNEL); 159 if (!new_configs) { 160 dev_err(dev, "krealloc(configs) failed\n"); 161 return -ENOMEM; 162 } 163 164 new_configs[old_num] = config; 165 166 *configs = new_configs; 167 *num_configs = new_num; 168 169 return 0; 170 } 171 172 static void samsung_dt_free_map(struct pinctrl_dev *pctldev, 173 struct pinctrl_map *map, 174 unsigned num_maps) 175 { 176 int i; 177 178 for (i = 0; i < num_maps; i++) 179 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP) 180 kfree(map[i].data.configs.configs); 181 182 kfree(map); 183 } 184 185 static int samsung_dt_subnode_to_map(struct samsung_pinctrl_drv_data *drvdata, 186 struct device *dev, 187 struct device_node *np, 188 struct pinctrl_map **map, 189 unsigned *reserved_maps, 190 unsigned *num_maps) 191 { 192 int ret, i; 193 u32 val; 194 unsigned long config; 195 unsigned long *configs = NULL; 196 unsigned num_configs = 0; 197 unsigned reserve; 198 struct property *prop; 199 const char *group; 200 bool has_func = false; 201 202 ret = of_property_read_u32(np, "samsung,pin-function", &val); 203 if (!ret) 204 has_func = true; 205 206 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) { 207 ret = of_property_read_u32(np, cfg_params[i].property, &val); 208 if (!ret) { 209 config = PINCFG_PACK(cfg_params[i].param, val); 210 ret = add_config(dev, &configs, &num_configs, config); 211 if (ret < 0) 212 goto exit; 213 /* EINVAL=missing, which is fine since it's optional */ 214 } else if (ret != -EINVAL) { 215 dev_err(dev, "could not parse property %s\n", 216 cfg_params[i].property); 217 } 218 } 219 220 reserve = 0; 221 if (has_func) 222 reserve++; 223 if (num_configs) 224 reserve++; 225 ret = of_property_count_strings(np, "samsung,pins"); 226 if (ret < 0) { 227 dev_err(dev, "could not parse property samsung,pins\n"); 228 goto exit; 229 } 230 reserve *= ret; 231 232 ret = reserve_map(dev, map, reserved_maps, num_maps, reserve); 233 if (ret < 0) 234 goto exit; 235 236 of_property_for_each_string(np, "samsung,pins", prop, group) { 237 if (has_func) { 238 ret = add_map_mux(map, reserved_maps, 239 num_maps, group, np->full_name); 240 if (ret < 0) 241 goto exit; 242 } 243 244 if (num_configs) { 245 ret = add_map_configs(dev, map, reserved_maps, 246 num_maps, group, configs, 247 num_configs); 248 if (ret < 0) 249 goto exit; 250 } 251 } 252 253 ret = 0; 254 255 exit: 256 kfree(configs); 257 return ret; 258 } 259 260 static int samsung_dt_node_to_map(struct pinctrl_dev *pctldev, 261 struct device_node *np_config, 262 struct pinctrl_map **map, 263 unsigned *num_maps) 264 { 265 struct samsung_pinctrl_drv_data *drvdata; 266 unsigned reserved_maps; 267 struct device_node *np; 268 int ret; 269 270 drvdata = pinctrl_dev_get_drvdata(pctldev); 271 272 reserved_maps = 0; 273 *map = NULL; 274 *num_maps = 0; 275 276 if (!of_get_child_count(np_config)) 277 return samsung_dt_subnode_to_map(drvdata, pctldev->dev, 278 np_config, map, 279 &reserved_maps, 280 num_maps); 281 282 for_each_child_of_node(np_config, np) { 283 ret = samsung_dt_subnode_to_map(drvdata, pctldev->dev, np, map, 284 &reserved_maps, num_maps); 285 if (ret < 0) { 286 samsung_dt_free_map(pctldev, *map, *num_maps); 287 return ret; 288 } 289 } 290 291 return 0; 292 } 293 294 /* list of pinctrl callbacks for the pinctrl core */ 295 static const struct pinctrl_ops samsung_pctrl_ops = { 296 .get_groups_count = samsung_get_group_count, 297 .get_group_name = samsung_get_group_name, 298 .get_group_pins = samsung_get_group_pins, 299 .dt_node_to_map = samsung_dt_node_to_map, 300 .dt_free_map = samsung_dt_free_map, 301 }; 302 303 /* check if the selector is a valid pin function selector */ 304 static int samsung_get_functions_count(struct pinctrl_dev *pctldev) 305 { 306 struct samsung_pinctrl_drv_data *drvdata; 307 308 drvdata = pinctrl_dev_get_drvdata(pctldev); 309 return drvdata->nr_functions; 310 } 311 312 /* return the name of the pin function specified */ 313 static const char *samsung_pinmux_get_fname(struct pinctrl_dev *pctldev, 314 unsigned selector) 315 { 316 struct samsung_pinctrl_drv_data *drvdata; 317 318 drvdata = pinctrl_dev_get_drvdata(pctldev); 319 return drvdata->pmx_functions[selector].name; 320 } 321 322 /* return the groups associated for the specified function selector */ 323 static int samsung_pinmux_get_groups(struct pinctrl_dev *pctldev, 324 unsigned selector, const char * const **groups, 325 unsigned * const num_groups) 326 { 327 struct samsung_pinctrl_drv_data *drvdata; 328 329 drvdata = pinctrl_dev_get_drvdata(pctldev); 330 *groups = drvdata->pmx_functions[selector].groups; 331 *num_groups = drvdata->pmx_functions[selector].num_groups; 332 return 0; 333 } 334 335 /* 336 * given a pin number that is local to a pin controller, find out the pin bank 337 * and the register base of the pin bank. 338 */ 339 static void pin_to_reg_bank(struct samsung_pinctrl_drv_data *drvdata, 340 unsigned pin, void __iomem **reg, u32 *offset, 341 struct samsung_pin_bank **bank) 342 { 343 struct samsung_pin_bank *b; 344 345 b = drvdata->pin_banks; 346 347 while ((pin >= b->pin_base) && 348 ((b->pin_base + b->nr_pins - 1) < pin)) 349 b++; 350 351 *reg = b->pctl_base + b->pctl_offset; 352 *offset = pin - b->pin_base; 353 if (bank) 354 *bank = b; 355 } 356 357 /* enable or disable a pinmux function */ 358 static void samsung_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector, 359 unsigned group, bool enable) 360 { 361 struct samsung_pinctrl_drv_data *drvdata; 362 const struct samsung_pin_bank_type *type; 363 struct samsung_pin_bank *bank; 364 void __iomem *reg; 365 u32 mask, shift, data, pin_offset; 366 unsigned long flags; 367 const struct samsung_pmx_func *func; 368 const struct samsung_pin_group *grp; 369 370 drvdata = pinctrl_dev_get_drvdata(pctldev); 371 func = &drvdata->pmx_functions[selector]; 372 grp = &drvdata->pin_groups[group]; 373 374 pin_to_reg_bank(drvdata, grp->pins[0] - drvdata->pin_base, 375 ®, &pin_offset, &bank); 376 type = bank->type; 377 mask = (1 << type->fld_width[PINCFG_TYPE_FUNC]) - 1; 378 shift = pin_offset * type->fld_width[PINCFG_TYPE_FUNC]; 379 if (shift >= 32) { 380 /* Some banks have two config registers */ 381 shift -= 32; 382 reg += 4; 383 } 384 385 spin_lock_irqsave(&bank->slock, flags); 386 387 data = readl(reg + type->reg_offset[PINCFG_TYPE_FUNC]); 388 data &= ~(mask << shift); 389 if (enable) 390 data |= func->val << shift; 391 writel(data, reg + type->reg_offset[PINCFG_TYPE_FUNC]); 392 393 spin_unlock_irqrestore(&bank->slock, flags); 394 } 395 396 /* enable a specified pinmux by writing to registers */ 397 static int samsung_pinmux_set_mux(struct pinctrl_dev *pctldev, 398 unsigned selector, 399 unsigned group) 400 { 401 samsung_pinmux_setup(pctldev, selector, group, true); 402 return 0; 403 } 404 405 /* list of pinmux callbacks for the pinmux vertical in pinctrl core */ 406 static const struct pinmux_ops samsung_pinmux_ops = { 407 .get_functions_count = samsung_get_functions_count, 408 .get_function_name = samsung_pinmux_get_fname, 409 .get_function_groups = samsung_pinmux_get_groups, 410 .set_mux = samsung_pinmux_set_mux, 411 }; 412 413 /* set or get the pin config settings for a specified pin */ 414 static int samsung_pinconf_rw(struct pinctrl_dev *pctldev, unsigned int pin, 415 unsigned long *config, bool set) 416 { 417 struct samsung_pinctrl_drv_data *drvdata; 418 const struct samsung_pin_bank_type *type; 419 struct samsung_pin_bank *bank; 420 void __iomem *reg_base; 421 enum pincfg_type cfg_type = PINCFG_UNPACK_TYPE(*config); 422 u32 data, width, pin_offset, mask, shift; 423 u32 cfg_value, cfg_reg; 424 unsigned long flags; 425 426 drvdata = pinctrl_dev_get_drvdata(pctldev); 427 pin_to_reg_bank(drvdata, pin - drvdata->pin_base, ®_base, 428 &pin_offset, &bank); 429 type = bank->type; 430 431 if (cfg_type >= PINCFG_TYPE_NUM || !type->fld_width[cfg_type]) 432 return -EINVAL; 433 434 width = type->fld_width[cfg_type]; 435 cfg_reg = type->reg_offset[cfg_type]; 436 437 spin_lock_irqsave(&bank->slock, flags); 438 439 mask = (1 << width) - 1; 440 shift = pin_offset * width; 441 data = readl(reg_base + cfg_reg); 442 443 if (set) { 444 cfg_value = PINCFG_UNPACK_VALUE(*config); 445 data &= ~(mask << shift); 446 data |= (cfg_value << shift); 447 writel(data, reg_base + cfg_reg); 448 } else { 449 data >>= shift; 450 data &= mask; 451 *config = PINCFG_PACK(cfg_type, data); 452 } 453 454 spin_unlock_irqrestore(&bank->slock, flags); 455 456 return 0; 457 } 458 459 /* set the pin config settings for a specified pin */ 460 static int samsung_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, 461 unsigned long *configs, unsigned num_configs) 462 { 463 int i, ret; 464 465 for (i = 0; i < num_configs; i++) { 466 ret = samsung_pinconf_rw(pctldev, pin, &configs[i], true); 467 if (ret < 0) 468 return ret; 469 } /* for each config */ 470 471 return 0; 472 } 473 474 /* get the pin config settings for a specified pin */ 475 static int samsung_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin, 476 unsigned long *config) 477 { 478 return samsung_pinconf_rw(pctldev, pin, config, false); 479 } 480 481 /* set the pin config settings for a specified pin group */ 482 static int samsung_pinconf_group_set(struct pinctrl_dev *pctldev, 483 unsigned group, unsigned long *configs, 484 unsigned num_configs) 485 { 486 struct samsung_pinctrl_drv_data *drvdata; 487 const unsigned int *pins; 488 unsigned int cnt; 489 490 drvdata = pinctrl_dev_get_drvdata(pctldev); 491 pins = drvdata->pin_groups[group].pins; 492 493 for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++) 494 samsung_pinconf_set(pctldev, pins[cnt], configs, num_configs); 495 496 return 0; 497 } 498 499 /* get the pin config settings for a specified pin group */ 500 static int samsung_pinconf_group_get(struct pinctrl_dev *pctldev, 501 unsigned int group, unsigned long *config) 502 { 503 struct samsung_pinctrl_drv_data *drvdata; 504 const unsigned int *pins; 505 506 drvdata = pinctrl_dev_get_drvdata(pctldev); 507 pins = drvdata->pin_groups[group].pins; 508 samsung_pinconf_get(pctldev, pins[0], config); 509 return 0; 510 } 511 512 /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */ 513 static const struct pinconf_ops samsung_pinconf_ops = { 514 .pin_config_get = samsung_pinconf_get, 515 .pin_config_set = samsung_pinconf_set, 516 .pin_config_group_get = samsung_pinconf_group_get, 517 .pin_config_group_set = samsung_pinconf_group_set, 518 }; 519 520 /* 521 * The samsung_gpio_set_vlaue() should be called with "bank->slock" held 522 * to avoid race condition. 523 */ 524 static void samsung_gpio_set_value(struct gpio_chip *gc, 525 unsigned offset, int value) 526 { 527 struct samsung_pin_bank *bank = gpiochip_get_data(gc); 528 const struct samsung_pin_bank_type *type = bank->type; 529 void __iomem *reg; 530 u32 data; 531 532 reg = bank->pctl_base + bank->pctl_offset; 533 534 data = readl(reg + type->reg_offset[PINCFG_TYPE_DAT]); 535 data &= ~(1 << offset); 536 if (value) 537 data |= 1 << offset; 538 writel(data, reg + type->reg_offset[PINCFG_TYPE_DAT]); 539 } 540 541 /* gpiolib gpio_set callback function */ 542 static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value) 543 { 544 struct samsung_pin_bank *bank = gpiochip_get_data(gc); 545 unsigned long flags; 546 547 spin_lock_irqsave(&bank->slock, flags); 548 samsung_gpio_set_value(gc, offset, value); 549 spin_unlock_irqrestore(&bank->slock, flags); 550 } 551 552 /* gpiolib gpio_get callback function */ 553 static int samsung_gpio_get(struct gpio_chip *gc, unsigned offset) 554 { 555 void __iomem *reg; 556 u32 data; 557 struct samsung_pin_bank *bank = gpiochip_get_data(gc); 558 const struct samsung_pin_bank_type *type = bank->type; 559 560 reg = bank->pctl_base + bank->pctl_offset; 561 562 data = readl(reg + type->reg_offset[PINCFG_TYPE_DAT]); 563 data >>= offset; 564 data &= 1; 565 return data; 566 } 567 568 /* 569 * The samsung_gpio_set_direction() should be called with "bank->slock" held 570 * to avoid race condition. 571 * The calls to gpio_direction_output() and gpio_direction_input() 572 * leads to this function call. 573 */ 574 static int samsung_gpio_set_direction(struct gpio_chip *gc, 575 unsigned offset, bool input) 576 { 577 const struct samsung_pin_bank_type *type; 578 struct samsung_pin_bank *bank; 579 struct samsung_pinctrl_drv_data *drvdata; 580 void __iomem *reg; 581 u32 data, mask, shift; 582 583 bank = gpiochip_get_data(gc); 584 type = bank->type; 585 drvdata = bank->drvdata; 586 587 reg = bank->pctl_base + bank->pctl_offset 588 + type->reg_offset[PINCFG_TYPE_FUNC]; 589 590 mask = (1 << type->fld_width[PINCFG_TYPE_FUNC]) - 1; 591 shift = offset * type->fld_width[PINCFG_TYPE_FUNC]; 592 if (shift >= 32) { 593 /* Some banks have two config registers */ 594 shift -= 32; 595 reg += 4; 596 } 597 598 data = readl(reg); 599 data &= ~(mask << shift); 600 if (!input) 601 data |= FUNC_OUTPUT << shift; 602 writel(data, reg); 603 604 return 0; 605 } 606 607 /* gpiolib gpio_direction_input callback function. */ 608 static int samsung_gpio_direction_input(struct gpio_chip *gc, unsigned offset) 609 { 610 struct samsung_pin_bank *bank = gpiochip_get_data(gc); 611 unsigned long flags; 612 int ret; 613 614 spin_lock_irqsave(&bank->slock, flags); 615 ret = samsung_gpio_set_direction(gc, offset, true); 616 spin_unlock_irqrestore(&bank->slock, flags); 617 return ret; 618 } 619 620 /* gpiolib gpio_direction_output callback function. */ 621 static int samsung_gpio_direction_output(struct gpio_chip *gc, unsigned offset, 622 int value) 623 { 624 struct samsung_pin_bank *bank = gpiochip_get_data(gc); 625 unsigned long flags; 626 int ret; 627 628 spin_lock_irqsave(&bank->slock, flags); 629 samsung_gpio_set_value(gc, offset, value); 630 ret = samsung_gpio_set_direction(gc, offset, false); 631 spin_unlock_irqrestore(&bank->slock, flags); 632 633 return ret; 634 } 635 636 /* 637 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin 638 * and a virtual IRQ, if not already present. 639 */ 640 static int samsung_gpio_to_irq(struct gpio_chip *gc, unsigned offset) 641 { 642 struct samsung_pin_bank *bank = gpiochip_get_data(gc); 643 unsigned int virq; 644 645 if (!bank->irq_domain) 646 return -ENXIO; 647 648 virq = irq_create_mapping(bank->irq_domain, offset); 649 650 return (virq) ? : -ENXIO; 651 } 652 653 static struct samsung_pin_group *samsung_pinctrl_create_groups( 654 struct device *dev, 655 struct samsung_pinctrl_drv_data *drvdata, 656 unsigned int *cnt) 657 { 658 struct pinctrl_desc *ctrldesc = &drvdata->pctl; 659 struct samsung_pin_group *groups, *grp; 660 const struct pinctrl_pin_desc *pdesc; 661 int i; 662 663 groups = devm_kzalloc(dev, ctrldesc->npins * sizeof(*groups), 664 GFP_KERNEL); 665 if (!groups) 666 return ERR_PTR(-EINVAL); 667 grp = groups; 668 669 pdesc = ctrldesc->pins; 670 for (i = 0; i < ctrldesc->npins; ++i, ++pdesc, ++grp) { 671 grp->name = pdesc->name; 672 grp->pins = &pdesc->number; 673 grp->num_pins = 1; 674 } 675 676 *cnt = ctrldesc->npins; 677 return groups; 678 } 679 680 static int samsung_pinctrl_create_function(struct device *dev, 681 struct samsung_pinctrl_drv_data *drvdata, 682 struct device_node *func_np, 683 struct samsung_pmx_func *func) 684 { 685 int npins; 686 int ret; 687 int i; 688 689 if (of_property_read_u32(func_np, "samsung,pin-function", &func->val)) 690 return 0; 691 692 npins = of_property_count_strings(func_np, "samsung,pins"); 693 if (npins < 1) { 694 dev_err(dev, "invalid pin list in %s node", func_np->name); 695 return -EINVAL; 696 } 697 698 func->name = func_np->full_name; 699 700 func->groups = devm_kzalloc(dev, npins * sizeof(char *), GFP_KERNEL); 701 if (!func->groups) 702 return -ENOMEM; 703 704 for (i = 0; i < npins; ++i) { 705 const char *gname; 706 707 ret = of_property_read_string_index(func_np, "samsung,pins", 708 i, &gname); 709 if (ret) { 710 dev_err(dev, 711 "failed to read pin name %d from %s node\n", 712 i, func_np->name); 713 return ret; 714 } 715 716 func->groups[i] = gname; 717 } 718 719 func->num_groups = npins; 720 return 1; 721 } 722 723 static struct samsung_pmx_func *samsung_pinctrl_create_functions( 724 struct device *dev, 725 struct samsung_pinctrl_drv_data *drvdata, 726 unsigned int *cnt) 727 { 728 struct samsung_pmx_func *functions, *func; 729 struct device_node *dev_np = dev->of_node; 730 struct device_node *cfg_np; 731 unsigned int func_cnt = 0; 732 int ret; 733 734 /* 735 * Iterate over all the child nodes of the pin controller node 736 * and create pin groups and pin function lists. 737 */ 738 for_each_child_of_node(dev_np, cfg_np) { 739 struct device_node *func_np; 740 741 if (!of_get_child_count(cfg_np)) { 742 if (!of_find_property(cfg_np, 743 "samsung,pin-function", NULL)) 744 continue; 745 ++func_cnt; 746 continue; 747 } 748 749 for_each_child_of_node(cfg_np, func_np) { 750 if (!of_find_property(func_np, 751 "samsung,pin-function", NULL)) 752 continue; 753 ++func_cnt; 754 } 755 } 756 757 functions = devm_kzalloc(dev, func_cnt * sizeof(*functions), 758 GFP_KERNEL); 759 if (!functions) { 760 dev_err(dev, "failed to allocate memory for function list\n"); 761 return ERR_PTR(-EINVAL); 762 } 763 func = functions; 764 765 /* 766 * Iterate over all the child nodes of the pin controller node 767 * and create pin groups and pin function lists. 768 */ 769 func_cnt = 0; 770 for_each_child_of_node(dev_np, cfg_np) { 771 struct device_node *func_np; 772 773 if (!of_get_child_count(cfg_np)) { 774 ret = samsung_pinctrl_create_function(dev, drvdata, 775 cfg_np, func); 776 if (ret < 0) 777 return ERR_PTR(ret); 778 if (ret > 0) { 779 ++func; 780 ++func_cnt; 781 } 782 continue; 783 } 784 785 for_each_child_of_node(cfg_np, func_np) { 786 ret = samsung_pinctrl_create_function(dev, drvdata, 787 func_np, func); 788 if (ret < 0) 789 return ERR_PTR(ret); 790 if (ret > 0) { 791 ++func; 792 ++func_cnt; 793 } 794 } 795 } 796 797 *cnt = func_cnt; 798 return functions; 799 } 800 801 /* 802 * Parse the information about all the available pin groups and pin functions 803 * from device node of the pin-controller. A pin group is formed with all 804 * the pins listed in the "samsung,pins" property. 805 */ 806 807 static int samsung_pinctrl_parse_dt(struct platform_device *pdev, 808 struct samsung_pinctrl_drv_data *drvdata) 809 { 810 struct device *dev = &pdev->dev; 811 struct samsung_pin_group *groups; 812 struct samsung_pmx_func *functions; 813 unsigned int grp_cnt = 0, func_cnt = 0; 814 815 groups = samsung_pinctrl_create_groups(dev, drvdata, &grp_cnt); 816 if (IS_ERR(groups)) { 817 dev_err(dev, "failed to parse pin groups\n"); 818 return PTR_ERR(groups); 819 } 820 821 functions = samsung_pinctrl_create_functions(dev, drvdata, &func_cnt); 822 if (IS_ERR(functions)) { 823 dev_err(dev, "failed to parse pin functions\n"); 824 return PTR_ERR(functions); 825 } 826 827 drvdata->pin_groups = groups; 828 drvdata->nr_groups = grp_cnt; 829 drvdata->pmx_functions = functions; 830 drvdata->nr_functions = func_cnt; 831 832 return 0; 833 } 834 835 /* register the pinctrl interface with the pinctrl subsystem */ 836 static int samsung_pinctrl_register(struct platform_device *pdev, 837 struct samsung_pinctrl_drv_data *drvdata) 838 { 839 struct pinctrl_desc *ctrldesc = &drvdata->pctl; 840 struct pinctrl_pin_desc *pindesc, *pdesc; 841 struct samsung_pin_bank *pin_bank; 842 char *pin_names; 843 int pin, bank, ret; 844 845 ctrldesc->name = "samsung-pinctrl"; 846 ctrldesc->owner = THIS_MODULE; 847 ctrldesc->pctlops = &samsung_pctrl_ops; 848 ctrldesc->pmxops = &samsung_pinmux_ops; 849 ctrldesc->confops = &samsung_pinconf_ops; 850 851 pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) * 852 drvdata->nr_pins, GFP_KERNEL); 853 if (!pindesc) { 854 dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n"); 855 return -ENOMEM; 856 } 857 ctrldesc->pins = pindesc; 858 ctrldesc->npins = drvdata->nr_pins; 859 860 /* dynamically populate the pin number and pin name for pindesc */ 861 for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++) 862 pdesc->number = pin + drvdata->pin_base; 863 864 /* 865 * allocate space for storing the dynamically generated names for all 866 * the pins which belong to this pin-controller. 867 */ 868 pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH * 869 drvdata->nr_pins, GFP_KERNEL); 870 if (!pin_names) { 871 dev_err(&pdev->dev, "mem alloc for pin names failed\n"); 872 return -ENOMEM; 873 } 874 875 /* for each pin, the name of the pin is pin-bank name + pin number */ 876 for (bank = 0; bank < drvdata->nr_banks; bank++) { 877 pin_bank = &drvdata->pin_banks[bank]; 878 for (pin = 0; pin < pin_bank->nr_pins; pin++) { 879 sprintf(pin_names, "%s-%d", pin_bank->name, pin); 880 pdesc = pindesc + pin_bank->pin_base + pin; 881 pdesc->name = pin_names; 882 pin_names += PIN_NAME_LENGTH; 883 } 884 } 885 886 ret = samsung_pinctrl_parse_dt(pdev, drvdata); 887 if (ret) 888 return ret; 889 890 drvdata->pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc, 891 drvdata); 892 if (IS_ERR(drvdata->pctl_dev)) { 893 dev_err(&pdev->dev, "could not register pinctrl driver\n"); 894 return PTR_ERR(drvdata->pctl_dev); 895 } 896 897 for (bank = 0; bank < drvdata->nr_banks; ++bank) { 898 pin_bank = &drvdata->pin_banks[bank]; 899 pin_bank->grange.name = pin_bank->name; 900 pin_bank->grange.id = bank; 901 pin_bank->grange.pin_base = drvdata->pin_base 902 + pin_bank->pin_base; 903 pin_bank->grange.base = pin_bank->gpio_chip.base; 904 pin_bank->grange.npins = pin_bank->gpio_chip.ngpio; 905 pin_bank->grange.gc = &pin_bank->gpio_chip; 906 pinctrl_add_gpio_range(drvdata->pctl_dev, &pin_bank->grange); 907 } 908 909 return 0; 910 } 911 912 static const struct gpio_chip samsung_gpiolib_chip = { 913 .request = gpiochip_generic_request, 914 .free = gpiochip_generic_free, 915 .set = samsung_gpio_set, 916 .get = samsung_gpio_get, 917 .direction_input = samsung_gpio_direction_input, 918 .direction_output = samsung_gpio_direction_output, 919 .to_irq = samsung_gpio_to_irq, 920 .owner = THIS_MODULE, 921 }; 922 923 /* register the gpiolib interface with the gpiolib subsystem */ 924 static int samsung_gpiolib_register(struct platform_device *pdev, 925 struct samsung_pinctrl_drv_data *drvdata) 926 { 927 struct samsung_pin_bank *bank = drvdata->pin_banks; 928 struct gpio_chip *gc; 929 int ret; 930 int i; 931 932 for (i = 0; i < drvdata->nr_banks; ++i, ++bank) { 933 bank->gpio_chip = samsung_gpiolib_chip; 934 935 gc = &bank->gpio_chip; 936 gc->base = drvdata->pin_base + bank->pin_base; 937 gc->ngpio = bank->nr_pins; 938 gc->parent = &pdev->dev; 939 gc->of_node = bank->of_node; 940 gc->label = bank->name; 941 942 ret = gpiochip_add_data(gc, bank); 943 if (ret) { 944 dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n", 945 gc->label, ret); 946 goto fail; 947 } 948 } 949 950 return 0; 951 952 fail: 953 for (--i, --bank; i >= 0; --i, --bank) 954 gpiochip_remove(&bank->gpio_chip); 955 return ret; 956 } 957 958 /* unregister the gpiolib interface with the gpiolib subsystem */ 959 static int samsung_gpiolib_unregister(struct platform_device *pdev, 960 struct samsung_pinctrl_drv_data *drvdata) 961 { 962 struct samsung_pin_bank *bank = drvdata->pin_banks; 963 int i; 964 965 for (i = 0; i < drvdata->nr_banks; ++i, ++bank) 966 gpiochip_remove(&bank->gpio_chip); 967 968 return 0; 969 } 970 971 static const struct of_device_id samsung_pinctrl_dt_match[]; 972 973 /* retrieve the soc specific data */ 974 static const struct samsung_pin_ctrl * 975 samsung_pinctrl_get_soc_data(struct samsung_pinctrl_drv_data *d, 976 struct platform_device *pdev) 977 { 978 int id; 979 const struct of_device_id *match; 980 struct device_node *node = pdev->dev.of_node; 981 struct device_node *np; 982 const struct samsung_pin_bank_data *bdata; 983 const struct samsung_pin_ctrl *ctrl; 984 struct samsung_pin_bank *bank; 985 struct resource *res; 986 void __iomem *virt_base[SAMSUNG_PINCTRL_NUM_RESOURCES]; 987 int i; 988 989 id = of_alias_get_id(node, "pinctrl"); 990 if (id < 0) { 991 dev_err(&pdev->dev, "failed to get alias id\n"); 992 return ERR_PTR(-ENOENT); 993 } 994 match = of_match_node(samsung_pinctrl_dt_match, node); 995 ctrl = (struct samsung_pin_ctrl *)match->data + id; 996 997 d->suspend = ctrl->suspend; 998 d->resume = ctrl->resume; 999 d->nr_banks = ctrl->nr_banks; 1000 d->pin_banks = devm_kcalloc(&pdev->dev, d->nr_banks, 1001 sizeof(*d->pin_banks), GFP_KERNEL); 1002 if (!d->pin_banks) 1003 return ERR_PTR(-ENOMEM); 1004 1005 if (ctrl->nr_ext_resources + 1 > SAMSUNG_PINCTRL_NUM_RESOURCES) 1006 return ERR_PTR(-EINVAL); 1007 1008 for (i = 0; i < ctrl->nr_ext_resources + 1; i++) { 1009 res = platform_get_resource(pdev, IORESOURCE_MEM, i); 1010 virt_base[i] = devm_ioremap(&pdev->dev, res->start, 1011 resource_size(res)); 1012 if (IS_ERR(virt_base[i])) 1013 return ERR_PTR(-EIO); 1014 } 1015 1016 bank = d->pin_banks; 1017 bdata = ctrl->pin_banks; 1018 for (i = 0; i < ctrl->nr_banks; ++i, ++bdata, ++bank) { 1019 bank->type = bdata->type; 1020 bank->pctl_offset = bdata->pctl_offset; 1021 bank->nr_pins = bdata->nr_pins; 1022 bank->eint_func = bdata->eint_func; 1023 bank->eint_type = bdata->eint_type; 1024 bank->eint_mask = bdata->eint_mask; 1025 bank->eint_offset = bdata->eint_offset; 1026 bank->name = bdata->name; 1027 1028 spin_lock_init(&bank->slock); 1029 bank->drvdata = d; 1030 bank->pin_base = d->nr_pins; 1031 d->nr_pins += bank->nr_pins; 1032 1033 bank->eint_base = virt_base[0]; 1034 bank->pctl_base = virt_base[bdata->pctl_res_idx]; 1035 } 1036 1037 for_each_child_of_node(node, np) { 1038 if (!of_find_property(np, "gpio-controller", NULL)) 1039 continue; 1040 bank = d->pin_banks; 1041 for (i = 0; i < d->nr_banks; ++i, ++bank) { 1042 if (!strcmp(bank->name, np->name)) { 1043 bank->of_node = np; 1044 break; 1045 } 1046 } 1047 } 1048 1049 d->pin_base = pin_base; 1050 pin_base += d->nr_pins; 1051 1052 return ctrl; 1053 } 1054 1055 static int samsung_pinctrl_probe(struct platform_device *pdev) 1056 { 1057 struct samsung_pinctrl_drv_data *drvdata; 1058 const struct samsung_pin_ctrl *ctrl; 1059 struct device *dev = &pdev->dev; 1060 struct resource *res; 1061 int ret; 1062 1063 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); 1064 if (!drvdata) 1065 return -ENOMEM; 1066 1067 ctrl = samsung_pinctrl_get_soc_data(drvdata, pdev); 1068 if (IS_ERR(ctrl)) { 1069 dev_err(&pdev->dev, "driver data not available\n"); 1070 return PTR_ERR(ctrl); 1071 } 1072 drvdata->dev = dev; 1073 1074 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 1075 if (res) 1076 drvdata->irq = res->start; 1077 1078 ret = samsung_gpiolib_register(pdev, drvdata); 1079 if (ret) 1080 return ret; 1081 1082 ret = samsung_pinctrl_register(pdev, drvdata); 1083 if (ret) { 1084 samsung_gpiolib_unregister(pdev, drvdata); 1085 return ret; 1086 } 1087 1088 if (ctrl->eint_gpio_init) 1089 ctrl->eint_gpio_init(drvdata); 1090 if (ctrl->eint_wkup_init) 1091 ctrl->eint_wkup_init(drvdata); 1092 1093 platform_set_drvdata(pdev, drvdata); 1094 1095 /* Add to the global list */ 1096 list_add_tail(&drvdata->node, &drvdata_list); 1097 1098 return 0; 1099 } 1100 1101 #ifdef CONFIG_PM 1102 1103 /** 1104 * samsung_pinctrl_suspend_dev - save pinctrl state for suspend for a device 1105 * 1106 * Save data for all banks handled by this device. 1107 */ 1108 static void samsung_pinctrl_suspend_dev( 1109 struct samsung_pinctrl_drv_data *drvdata) 1110 { 1111 int i; 1112 1113 for (i = 0; i < drvdata->nr_banks; i++) { 1114 struct samsung_pin_bank *bank = &drvdata->pin_banks[i]; 1115 void __iomem *reg = bank->pctl_base + bank->pctl_offset; 1116 const u8 *offs = bank->type->reg_offset; 1117 const u8 *widths = bank->type->fld_width; 1118 enum pincfg_type type; 1119 1120 /* Registers without a powerdown config aren't lost */ 1121 if (!widths[PINCFG_TYPE_CON_PDN]) 1122 continue; 1123 1124 for (type = 0; type < PINCFG_TYPE_NUM; type++) 1125 if (widths[type]) 1126 bank->pm_save[type] = readl(reg + offs[type]); 1127 1128 if (widths[PINCFG_TYPE_FUNC] * bank->nr_pins > 32) { 1129 /* Some banks have two config registers */ 1130 bank->pm_save[PINCFG_TYPE_NUM] = 1131 readl(reg + offs[PINCFG_TYPE_FUNC] + 4); 1132 pr_debug("Save %s @ %p (con %#010x %08x)\n", 1133 bank->name, reg, 1134 bank->pm_save[PINCFG_TYPE_FUNC], 1135 bank->pm_save[PINCFG_TYPE_NUM]); 1136 } else { 1137 pr_debug("Save %s @ %p (con %#010x)\n", bank->name, 1138 reg, bank->pm_save[PINCFG_TYPE_FUNC]); 1139 } 1140 } 1141 1142 if (drvdata->suspend) 1143 drvdata->suspend(drvdata); 1144 } 1145 1146 /** 1147 * samsung_pinctrl_resume_dev - restore pinctrl state from suspend for a device 1148 * 1149 * Restore one of the banks that was saved during suspend. 1150 * 1151 * We don't bother doing anything complicated to avoid glitching lines since 1152 * we're called before pad retention is turned off. 1153 */ 1154 static void samsung_pinctrl_resume_dev(struct samsung_pinctrl_drv_data *drvdata) 1155 { 1156 int i; 1157 1158 if (drvdata->resume) 1159 drvdata->resume(drvdata); 1160 1161 for (i = 0; i < drvdata->nr_banks; i++) { 1162 struct samsung_pin_bank *bank = &drvdata->pin_banks[i]; 1163 void __iomem *reg = bank->pctl_base + bank->pctl_offset; 1164 const u8 *offs = bank->type->reg_offset; 1165 const u8 *widths = bank->type->fld_width; 1166 enum pincfg_type type; 1167 1168 /* Registers without a powerdown config aren't lost */ 1169 if (!widths[PINCFG_TYPE_CON_PDN]) 1170 continue; 1171 1172 if (widths[PINCFG_TYPE_FUNC] * bank->nr_pins > 32) { 1173 /* Some banks have two config registers */ 1174 pr_debug("%s @ %p (con %#010x %08x => %#010x %08x)\n", 1175 bank->name, reg, 1176 readl(reg + offs[PINCFG_TYPE_FUNC]), 1177 readl(reg + offs[PINCFG_TYPE_FUNC] + 4), 1178 bank->pm_save[PINCFG_TYPE_FUNC], 1179 bank->pm_save[PINCFG_TYPE_NUM]); 1180 writel(bank->pm_save[PINCFG_TYPE_NUM], 1181 reg + offs[PINCFG_TYPE_FUNC] + 4); 1182 } else { 1183 pr_debug("%s @ %p (con %#010x => %#010x)\n", bank->name, 1184 reg, readl(reg + offs[PINCFG_TYPE_FUNC]), 1185 bank->pm_save[PINCFG_TYPE_FUNC]); 1186 } 1187 for (type = 0; type < PINCFG_TYPE_NUM; type++) 1188 if (widths[type]) 1189 writel(bank->pm_save[type], reg + offs[type]); 1190 } 1191 } 1192 1193 /** 1194 * samsung_pinctrl_suspend - save pinctrl state for suspend 1195 * 1196 * Save data for all banks across all devices. 1197 */ 1198 static int samsung_pinctrl_suspend(void) 1199 { 1200 struct samsung_pinctrl_drv_data *drvdata; 1201 1202 list_for_each_entry(drvdata, &drvdata_list, node) { 1203 samsung_pinctrl_suspend_dev(drvdata); 1204 } 1205 1206 return 0; 1207 } 1208 1209 /** 1210 * samsung_pinctrl_resume - restore pinctrl state for suspend 1211 * 1212 * Restore data for all banks across all devices. 1213 */ 1214 static void samsung_pinctrl_resume(void) 1215 { 1216 struct samsung_pinctrl_drv_data *drvdata; 1217 1218 list_for_each_entry_reverse(drvdata, &drvdata_list, node) { 1219 samsung_pinctrl_resume_dev(drvdata); 1220 } 1221 } 1222 1223 #else 1224 #define samsung_pinctrl_suspend NULL 1225 #define samsung_pinctrl_resume NULL 1226 #endif 1227 1228 static struct syscore_ops samsung_pinctrl_syscore_ops = { 1229 .suspend = samsung_pinctrl_suspend, 1230 .resume = samsung_pinctrl_resume, 1231 }; 1232 1233 static const struct of_device_id samsung_pinctrl_dt_match[] = { 1234 #ifdef CONFIG_PINCTRL_EXYNOS 1235 { .compatible = "samsung,exynos3250-pinctrl", 1236 .data = (void *)exynos3250_pin_ctrl }, 1237 { .compatible = "samsung,exynos4210-pinctrl", 1238 .data = (void *)exynos4210_pin_ctrl }, 1239 { .compatible = "samsung,exynos4x12-pinctrl", 1240 .data = (void *)exynos4x12_pin_ctrl }, 1241 { .compatible = "samsung,exynos4415-pinctrl", 1242 .data = (void *)exynos4415_pin_ctrl }, 1243 { .compatible = "samsung,exynos5250-pinctrl", 1244 .data = (void *)exynos5250_pin_ctrl }, 1245 { .compatible = "samsung,exynos5260-pinctrl", 1246 .data = (void *)exynos5260_pin_ctrl }, 1247 { .compatible = "samsung,exynos5410-pinctrl", 1248 .data = (void *)exynos5410_pin_ctrl }, 1249 { .compatible = "samsung,exynos5420-pinctrl", 1250 .data = (void *)exynos5420_pin_ctrl }, 1251 { .compatible = "samsung,exynos5433-pinctrl", 1252 .data = (void *)exynos5433_pin_ctrl }, 1253 { .compatible = "samsung,s5pv210-pinctrl", 1254 .data = (void *)s5pv210_pin_ctrl }, 1255 { .compatible = "samsung,exynos7-pinctrl", 1256 .data = (void *)exynos7_pin_ctrl }, 1257 #endif 1258 #ifdef CONFIG_PINCTRL_S3C64XX 1259 { .compatible = "samsung,s3c64xx-pinctrl", 1260 .data = s3c64xx_pin_ctrl }, 1261 #endif 1262 #ifdef CONFIG_PINCTRL_S3C24XX 1263 { .compatible = "samsung,s3c2412-pinctrl", 1264 .data = s3c2412_pin_ctrl }, 1265 { .compatible = "samsung,s3c2416-pinctrl", 1266 .data = s3c2416_pin_ctrl }, 1267 { .compatible = "samsung,s3c2440-pinctrl", 1268 .data = s3c2440_pin_ctrl }, 1269 { .compatible = "samsung,s3c2450-pinctrl", 1270 .data = s3c2450_pin_ctrl }, 1271 #endif 1272 {}, 1273 }; 1274 MODULE_DEVICE_TABLE(of, samsung_pinctrl_dt_match); 1275 1276 static struct platform_driver samsung_pinctrl_driver = { 1277 .probe = samsung_pinctrl_probe, 1278 .driver = { 1279 .name = "samsung-pinctrl", 1280 .of_match_table = samsung_pinctrl_dt_match, 1281 .suppress_bind_attrs = true, 1282 }, 1283 }; 1284 1285 static int __init samsung_pinctrl_drv_register(void) 1286 { 1287 /* 1288 * Register syscore ops for save/restore of registers across suspend. 1289 * It's important to ensure that this driver is running at an earlier 1290 * initcall level than any arch-specific init calls that install syscore 1291 * ops that turn off pad retention (like exynos_pm_resume). 1292 */ 1293 register_syscore_ops(&samsung_pinctrl_syscore_ops); 1294 1295 return platform_driver_register(&samsung_pinctrl_driver); 1296 } 1297 postcore_initcall(samsung_pinctrl_drv_register); 1298 1299 static void __exit samsung_pinctrl_drv_unregister(void) 1300 { 1301 platform_driver_unregister(&samsung_pinctrl_driver); 1302 } 1303 module_exit(samsung_pinctrl_drv_unregister); 1304 1305 MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>"); 1306 MODULE_DESCRIPTION("Samsung pinctrl driver"); 1307 MODULE_LICENSE("GPL v2"); 1308