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