2 * Pinctrl driver for Rockchip SoCs
4 * Copyright (c) 2013 MundoReader S.L.
5 * Author: Heiko Stuebner <heiko@sntech.de>
7 * With some ideas taken from pinctrl-samsung:
8 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
9 * http://www.samsung.com
10 * Copyright (c) 2012 Linaro Ltd
11 * http://www.linaro.org
14 * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as published
18 * by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
29 #include <linux/bitops.h>
30 #include <linux/gpio.h>
31 #include <linux/of_address.h>
32 #include <linux/of_irq.h>
33 #include <linux/pinctrl/machine.h>
34 #include <linux/pinctrl/pinconf.h>
35 #include <linux/pinctrl/pinctrl.h>
36 #include <linux/pinctrl/pinmux.h>
37 #include <linux/pinctrl/pinconf-generic.h>
38 #include <linux/irqchip/chained_irq.h>
39 #include <linux/clk.h>
40 #include <linux/regmap.h>
41 #include <linux/mfd/syscon.h>
42 #include <dt-bindings/pinctrl/rockchip.h>
47 /* GPIO control registers */
48 #define GPIO_SWPORT_DR 0x00
49 #define GPIO_SWPORT_DDR 0x04
50 #define GPIO_INTEN 0x30
51 #define GPIO_INTMASK 0x34
52 #define GPIO_INTTYPE_LEVEL 0x38
53 #define GPIO_INT_POLARITY 0x3c
54 #define GPIO_INT_STATUS 0x40
55 #define GPIO_INT_RAWSTATUS 0x44
56 #define GPIO_DEBOUNCE 0x48
57 #define GPIO_PORTS_EOI 0x4c
58 #define GPIO_EXT_PORT 0x50
59 #define GPIO_LS_SYNC 0x60
61 enum rockchip_pinctrl_type {
70 * Encode variants of iomux registers into a type variable
72 #define IOMUX_GPIO_ONLY BIT(0)
73 #define IOMUX_WIDTH_4BIT BIT(1)
74 #define IOMUX_SOURCE_PMU BIT(2)
75 #define IOMUX_UNROUTED BIT(3)
78 * @type: iomux variant using IOMUX_* constants
79 * @offset: if initialized to -1 it will be autocalculated, by specifying
80 * an initial offset value the relevant source offset can be reset
81 * to a new value for autocalculating the following iomux registers.
83 struct rockchip_iomux {
89 * @reg_base: register base of the gpio bank
90 * @reg_pull: optional separate register for additional pull settings
91 * @clk: clock of the gpio bank
92 * @irq: interrupt of the gpio bank
93 * @saved_masks: Saved content of GPIO_INTEN at suspend time.
94 * @pin_base: first pin number
95 * @nr_pins: number of pins in this bank
96 * @name: name of the bank
97 * @bank_num: number of the bank, to account for holes
98 * @iomux: array describing the 4 iomux sources of the bank
99 * @valid: are all necessary informations present
100 * @of_node: dt node of this bank
101 * @drvdata: common pinctrl basedata
102 * @domain: irqdomain of the gpio bank
103 * @gpio_chip: gpiolib chip
104 * @grange: gpio range
105 * @slock: spinlock for the gpio bank
107 struct rockchip_pin_bank {
108 void __iomem *reg_base;
109 struct regmap *regmap_pull;
117 struct rockchip_iomux iomux[4];
119 struct device_node *of_node;
120 struct rockchip_pinctrl *drvdata;
121 struct irq_domain *domain;
122 struct gpio_chip gpio_chip;
123 struct pinctrl_gpio_range grange;
125 u32 toggle_edge_mode;
128 #define PIN_BANK(id, pins, label) \
141 #define PIN_BANK_IOMUX_FLAGS(id, pins, label, iom0, iom1, iom2, iom3) \
147 { .type = iom0, .offset = -1 }, \
148 { .type = iom1, .offset = -1 }, \
149 { .type = iom2, .offset = -1 }, \
150 { .type = iom3, .offset = -1 }, \
156 struct rockchip_pin_ctrl {
157 struct rockchip_pin_bank *pin_banks;
161 enum rockchip_pinctrl_type type;
164 void (*pull_calc_reg)(struct rockchip_pin_bank *bank,
165 int pin_num, struct regmap **regmap,
167 void (*drv_calc_reg)(struct rockchip_pin_bank *bank,
168 int pin_num, struct regmap **regmap,
172 struct rockchip_pin_config {
174 unsigned long *configs;
175 unsigned int nconfigs;
179 * struct rockchip_pin_group: represent group of pins of a pinmux function.
180 * @name: name of the pin group, used to lookup the group.
181 * @pins: the pins included in this group.
182 * @npins: number of pins included in this group.
183 * @func: the mux function number to be programmed when selected.
184 * @configs: the config values to be set for each pin
185 * @nconfigs: number of configs for each pin
187 struct rockchip_pin_group {
191 struct rockchip_pin_config *data;
195 * struct rockchip_pmx_func: represent a pin function.
196 * @name: name of the pin function, used to lookup the function.
197 * @groups: one or more names of pin groups that provide this function.
198 * @num_groups: number of groups included in @groups.
200 struct rockchip_pmx_func {
206 struct rockchip_pinctrl {
207 struct regmap *regmap_base;
209 struct regmap *regmap_pull;
210 struct regmap *regmap_pmu;
212 struct rockchip_pin_ctrl *ctrl;
213 struct pinctrl_desc pctl;
214 struct pinctrl_dev *pctl_dev;
215 struct rockchip_pin_group *groups;
216 unsigned int ngroups;
217 struct rockchip_pmx_func *functions;
218 unsigned int nfunctions;
221 static struct regmap_config rockchip_regmap_config = {
227 static inline struct rockchip_pin_bank *gc_to_pin_bank(struct gpio_chip *gc)
229 return container_of(gc, struct rockchip_pin_bank, gpio_chip);
232 static const inline struct rockchip_pin_group *pinctrl_name_to_group(
233 const struct rockchip_pinctrl *info,
238 for (i = 0; i < info->ngroups; i++) {
239 if (!strcmp(info->groups[i].name, name))
240 return &info->groups[i];
247 * given a pin number that is local to a pin controller, find out the pin bank
248 * and the register base of the pin bank.
250 static struct rockchip_pin_bank *pin_to_bank(struct rockchip_pinctrl *info,
253 struct rockchip_pin_bank *b = info->ctrl->pin_banks;
255 while (pin >= (b->pin_base + b->nr_pins))
261 static struct rockchip_pin_bank *bank_num_to_bank(
262 struct rockchip_pinctrl *info,
265 struct rockchip_pin_bank *b = info->ctrl->pin_banks;
268 for (i = 0; i < info->ctrl->nr_banks; i++, b++) {
269 if (b->bank_num == num)
273 return ERR_PTR(-EINVAL);
277 * Pinctrl_ops handling
280 static int rockchip_get_groups_count(struct pinctrl_dev *pctldev)
282 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
284 return info->ngroups;
287 static const char *rockchip_get_group_name(struct pinctrl_dev *pctldev,
290 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
292 return info->groups[selector].name;
295 static int rockchip_get_group_pins(struct pinctrl_dev *pctldev,
296 unsigned selector, const unsigned **pins,
299 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
301 if (selector >= info->ngroups)
304 *pins = info->groups[selector].pins;
305 *npins = info->groups[selector].npins;
310 static int rockchip_dt_node_to_map(struct pinctrl_dev *pctldev,
311 struct device_node *np,
312 struct pinctrl_map **map, unsigned *num_maps)
314 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
315 const struct rockchip_pin_group *grp;
316 struct pinctrl_map *new_map;
317 struct device_node *parent;
322 * first find the group of this node and check if we need to create
323 * config maps for pins
325 grp = pinctrl_name_to_group(info, np->name);
327 dev_err(info->dev, "unable to find group for node %s\n",
332 map_num += grp->npins;
333 new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num,
342 parent = of_get_parent(np);
344 devm_kfree(pctldev->dev, new_map);
347 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
348 new_map[0].data.mux.function = parent->name;
349 new_map[0].data.mux.group = np->name;
352 /* create config map */
354 for (i = 0; i < grp->npins; i++) {
355 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
356 new_map[i].data.configs.group_or_pin =
357 pin_get_name(pctldev, grp->pins[i]);
358 new_map[i].data.configs.configs = grp->data[i].configs;
359 new_map[i].data.configs.num_configs = grp->data[i].nconfigs;
362 dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
363 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
368 static void rockchip_dt_free_map(struct pinctrl_dev *pctldev,
369 struct pinctrl_map *map, unsigned num_maps)
373 static const struct pinctrl_ops rockchip_pctrl_ops = {
374 .get_groups_count = rockchip_get_groups_count,
375 .get_group_name = rockchip_get_group_name,
376 .get_group_pins = rockchip_get_group_pins,
377 .dt_node_to_map = rockchip_dt_node_to_map,
378 .dt_free_map = rockchip_dt_free_map,
385 static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin)
387 struct rockchip_pinctrl *info = bank->drvdata;
388 int iomux_num = (pin / 8);
389 struct regmap *regmap;
397 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
398 dev_err(info->dev, "pin %d is unrouted\n", pin);
402 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
405 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
406 ? info->regmap_pmu : info->regmap_base;
408 /* get basic quadrupel of mux registers and the correct reg inside */
409 mask = (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) ? 0xf : 0x3;
410 reg = bank->iomux[iomux_num].offset;
411 if (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) {
419 ret = regmap_read(regmap, reg, &val);
423 return ((val >> bit) & mask);
427 * Set a new mux function for a pin.
429 * The register is divided into the upper and lower 16 bit. When changing
430 * a value, the previous register value is not read and changed. Instead
431 * it seems the changed bits are marked in the upper 16 bit, while the
432 * changed value gets set in the same offset in the lower 16 bit.
433 * All pin settings seem to be 2 bit wide in both the upper and lower
435 * @bank: pin bank to change
436 * @pin: pin to change
437 * @mux: new mux function to set
439 static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
441 struct rockchip_pinctrl *info = bank->drvdata;
442 int iomux_num = (pin / 8);
443 struct regmap *regmap;
452 if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
453 dev_err(info->dev, "pin %d is unrouted\n", pin);
457 if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) {
458 if (mux != RK_FUNC_GPIO) {
460 "pin %d only supports a gpio mux\n", pin);
467 dev_dbg(info->dev, "setting mux of GPIO%d-%d to %d\n",
468 bank->bank_num, pin, mux);
470 regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
471 ? info->regmap_pmu : info->regmap_base;
473 /* get basic quadrupel of mux registers and the correct reg inside */
474 mask = (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) ? 0xf : 0x3;
475 reg = bank->iomux[iomux_num].offset;
476 if (bank->iomux[iomux_num].type & IOMUX_WIDTH_4BIT) {
484 spin_lock_irqsave(&bank->slock, flags);
486 data = (mask << (bit + 16));
487 rmask = data | (data >> 16);
488 data |= (mux & mask) << bit;
489 ret = regmap_update_bits(regmap, reg, rmask, data);
491 spin_unlock_irqrestore(&bank->slock, flags);
496 #define RK2928_PULL_OFFSET 0x118
497 #define RK2928_PULL_PINS_PER_REG 16
498 #define RK2928_PULL_BANK_STRIDE 8
500 static void rk2928_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
501 int pin_num, struct regmap **regmap,
504 struct rockchip_pinctrl *info = bank->drvdata;
506 *regmap = info->regmap_base;
507 *reg = RK2928_PULL_OFFSET;
508 *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE;
509 *reg += (pin_num / RK2928_PULL_PINS_PER_REG) * 4;
511 *bit = pin_num % RK2928_PULL_PINS_PER_REG;
514 #define RK3188_PULL_OFFSET 0x164
515 #define RK3188_PULL_BITS_PER_PIN 2
516 #define RK3188_PULL_PINS_PER_REG 8
517 #define RK3188_PULL_BANK_STRIDE 16
518 #define RK3188_PULL_PMU_OFFSET 0x64
520 static void rk3188_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
521 int pin_num, struct regmap **regmap,
524 struct rockchip_pinctrl *info = bank->drvdata;
526 /* The first 12 pins of the first bank are located elsewhere */
527 if (bank->bank_num == 0 && pin_num < 12) {
528 *regmap = info->regmap_pmu ? info->regmap_pmu
530 *reg = info->regmap_pmu ? RK3188_PULL_PMU_OFFSET : 0;
531 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
532 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
533 *bit *= RK3188_PULL_BITS_PER_PIN;
535 *regmap = info->regmap_pull ? info->regmap_pull
537 *reg = info->regmap_pull ? 0 : RK3188_PULL_OFFSET;
539 /* correct the offset, as it is the 2nd pull register */
541 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
542 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
545 * The bits in these registers have an inverse ordering
546 * with the lowest pin being in bits 15:14 and the highest
549 *bit = 7 - (pin_num % RK3188_PULL_PINS_PER_REG);
550 *bit *= RK3188_PULL_BITS_PER_PIN;
554 #define RK3288_PULL_OFFSET 0x140
555 static void rk3288_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
556 int pin_num, struct regmap **regmap,
559 struct rockchip_pinctrl *info = bank->drvdata;
561 /* The first 24 pins of the first bank are located in PMU */
562 if (bank->bank_num == 0) {
563 *regmap = info->regmap_pmu;
564 *reg = RK3188_PULL_PMU_OFFSET;
566 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
567 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
568 *bit *= RK3188_PULL_BITS_PER_PIN;
570 *regmap = info->regmap_base;
571 *reg = RK3288_PULL_OFFSET;
573 /* correct the offset, as we're starting with the 2nd bank */
575 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
576 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
578 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
579 *bit *= RK3188_PULL_BITS_PER_PIN;
583 #define RK3288_DRV_PMU_OFFSET 0x70
584 #define RK3288_DRV_GRF_OFFSET 0x1c0
585 #define RK3288_DRV_BITS_PER_PIN 2
586 #define RK3288_DRV_PINS_PER_REG 8
587 #define RK3288_DRV_BANK_STRIDE 16
589 static void rk3288_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
590 int pin_num, struct regmap **regmap,
593 struct rockchip_pinctrl *info = bank->drvdata;
595 /* The first 24 pins of the first bank are located in PMU */
596 if (bank->bank_num == 0) {
597 *regmap = info->regmap_pmu;
598 *reg = RK3288_DRV_PMU_OFFSET;
600 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
601 *bit = pin_num % RK3288_DRV_PINS_PER_REG;
602 *bit *= RK3288_DRV_BITS_PER_PIN;
604 *regmap = info->regmap_base;
605 *reg = RK3288_DRV_GRF_OFFSET;
607 /* correct the offset, as we're starting with the 2nd bank */
609 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
610 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
612 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
613 *bit *= RK3288_DRV_BITS_PER_PIN;
617 #define RK3228_PULL_OFFSET 0x100
619 static void rk3228_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
620 int pin_num, struct regmap **regmap,
623 struct rockchip_pinctrl *info = bank->drvdata;
625 *regmap = info->regmap_base;
626 *reg = RK3228_PULL_OFFSET;
627 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
628 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
630 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
631 *bit *= RK3188_PULL_BITS_PER_PIN;
634 #define RK3228_DRV_GRF_OFFSET 0x200
636 static void rk3228_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
637 int pin_num, struct regmap **regmap,
640 struct rockchip_pinctrl *info = bank->drvdata;
642 *regmap = info->regmap_base;
643 *reg = RK3228_DRV_GRF_OFFSET;
644 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
645 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
647 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
648 *bit *= RK3288_DRV_BITS_PER_PIN;
651 #define RK3368_PULL_GRF_OFFSET 0x100
652 #define RK3368_PULL_PMU_OFFSET 0x10
654 static void rk3368_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
655 int pin_num, struct regmap **regmap,
658 struct rockchip_pinctrl *info = bank->drvdata;
660 /* The first 32 pins of the first bank are located in PMU */
661 if (bank->bank_num == 0) {
662 *regmap = info->regmap_pmu;
663 *reg = RK3368_PULL_PMU_OFFSET;
665 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
666 *bit = pin_num % RK3188_PULL_PINS_PER_REG;
667 *bit *= RK3188_PULL_BITS_PER_PIN;
669 *regmap = info->regmap_base;
670 *reg = RK3368_PULL_GRF_OFFSET;
672 /* correct the offset, as we're starting with the 2nd bank */
674 *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
675 *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
677 *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
678 *bit *= RK3188_PULL_BITS_PER_PIN;
682 #define RK3368_DRV_PMU_OFFSET 0x20
683 #define RK3368_DRV_GRF_OFFSET 0x200
685 static void rk3368_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
686 int pin_num, struct regmap **regmap,
689 struct rockchip_pinctrl *info = bank->drvdata;
691 /* The first 32 pins of the first bank are located in PMU */
692 if (bank->bank_num == 0) {
693 *regmap = info->regmap_pmu;
694 *reg = RK3368_DRV_PMU_OFFSET;
696 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
697 *bit = pin_num % RK3288_DRV_PINS_PER_REG;
698 *bit *= RK3288_DRV_BITS_PER_PIN;
700 *regmap = info->regmap_base;
701 *reg = RK3368_DRV_GRF_OFFSET;
703 /* correct the offset, as we're starting with the 2nd bank */
705 *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
706 *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
708 *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
709 *bit *= RK3288_DRV_BITS_PER_PIN;
713 static int rockchip_perpin_drv_list[] = { 2, 4, 8, 12 };
715 static int rockchip_get_drive_perpin(struct rockchip_pin_bank *bank,
718 struct rockchip_pinctrl *info = bank->drvdata;
719 struct rockchip_pin_ctrl *ctrl = info->ctrl;
720 struct regmap *regmap;
725 ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit);
727 ret = regmap_read(regmap, reg, &data);
732 data &= (1 << RK3288_DRV_BITS_PER_PIN) - 1;
734 return rockchip_perpin_drv_list[data];
737 static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank,
738 int pin_num, int strength)
740 struct rockchip_pinctrl *info = bank->drvdata;
741 struct rockchip_pin_ctrl *ctrl = info->ctrl;
742 struct regmap *regmap;
748 ctrl->drv_calc_reg(bank, pin_num, ®map, ®, &bit);
751 for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list); i++) {
752 if (rockchip_perpin_drv_list[i] == strength) {
759 dev_err(info->dev, "unsupported driver strength %d\n",
764 spin_lock_irqsave(&bank->slock, flags);
766 /* enable the write to the equivalent lower bits */
767 data = ((1 << RK3288_DRV_BITS_PER_PIN) - 1) << (bit + 16);
768 rmask = data | (data >> 16);
769 data |= (ret << bit);
771 ret = regmap_update_bits(regmap, reg, rmask, data);
772 spin_unlock_irqrestore(&bank->slock, flags);
777 static int rockchip_get_pull(struct rockchip_pin_bank *bank, int pin_num)
779 struct rockchip_pinctrl *info = bank->drvdata;
780 struct rockchip_pin_ctrl *ctrl = info->ctrl;
781 struct regmap *regmap;
786 /* rk3066b does support any pulls */
787 if (ctrl->type == RK3066B)
788 return PIN_CONFIG_BIAS_DISABLE;
790 ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit);
792 ret = regmap_read(regmap, reg, &data);
796 switch (ctrl->type) {
798 return !(data & BIT(bit))
799 ? PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
800 : PIN_CONFIG_BIAS_DISABLE;
805 data &= (1 << RK3188_PULL_BITS_PER_PIN) - 1;
809 return PIN_CONFIG_BIAS_DISABLE;
811 return PIN_CONFIG_BIAS_PULL_UP;
813 return PIN_CONFIG_BIAS_PULL_DOWN;
815 return PIN_CONFIG_BIAS_BUS_HOLD;
818 dev_err(info->dev, "unknown pull setting\n");
821 dev_err(info->dev, "unsupported pinctrl type\n");
826 static int rockchip_set_pull(struct rockchip_pin_bank *bank,
827 int pin_num, int pull)
829 struct rockchip_pinctrl *info = bank->drvdata;
830 struct rockchip_pin_ctrl *ctrl = info->ctrl;
831 struct regmap *regmap;
837 dev_dbg(info->dev, "setting pull of GPIO%d-%d to %d\n",
838 bank->bank_num, pin_num, pull);
840 /* rk3066b does support any pulls */
841 if (ctrl->type == RK3066B)
842 return pull ? -EINVAL : 0;
844 ctrl->pull_calc_reg(bank, pin_num, ®map, ®, &bit);
846 switch (ctrl->type) {
848 spin_lock_irqsave(&bank->slock, flags);
850 data = BIT(bit + 16);
851 if (pull == PIN_CONFIG_BIAS_DISABLE)
853 ret = regmap_write(regmap, reg, data);
855 spin_unlock_irqrestore(&bank->slock, flags);
860 spin_lock_irqsave(&bank->slock, flags);
862 /* enable the write to the equivalent lower bits */
863 data = ((1 << RK3188_PULL_BITS_PER_PIN) - 1) << (bit + 16);
864 rmask = data | (data >> 16);
867 case PIN_CONFIG_BIAS_DISABLE:
869 case PIN_CONFIG_BIAS_PULL_UP:
872 case PIN_CONFIG_BIAS_PULL_DOWN:
875 case PIN_CONFIG_BIAS_BUS_HOLD:
879 spin_unlock_irqrestore(&bank->slock, flags);
880 dev_err(info->dev, "unsupported pull setting %d\n",
885 ret = regmap_update_bits(regmap, reg, rmask, data);
887 spin_unlock_irqrestore(&bank->slock, flags);
890 dev_err(info->dev, "unsupported pinctrl type\n");
898 * Pinmux_ops handling
901 static int rockchip_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
903 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
905 return info->nfunctions;
908 static const char *rockchip_pmx_get_func_name(struct pinctrl_dev *pctldev,
911 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
913 return info->functions[selector].name;
916 static int rockchip_pmx_get_groups(struct pinctrl_dev *pctldev,
917 unsigned selector, const char * const **groups,
918 unsigned * const num_groups)
920 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
922 *groups = info->functions[selector].groups;
923 *num_groups = info->functions[selector].ngroups;
928 static int rockchip_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
931 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
932 const unsigned int *pins = info->groups[group].pins;
933 const struct rockchip_pin_config *data = info->groups[group].data;
934 struct rockchip_pin_bank *bank;
937 dev_dbg(info->dev, "enable function %s group %s\n",
938 info->functions[selector].name, info->groups[group].name);
941 * for each pin in the pin group selected, program the correspoding pin
942 * pin function number in the config register.
944 for (cnt = 0; cnt < info->groups[group].npins; cnt++) {
945 bank = pin_to_bank(info, pins[cnt]);
946 ret = rockchip_set_mux(bank, pins[cnt] - bank->pin_base,
953 /* revert the already done pin settings */
954 for (cnt--; cnt >= 0; cnt--)
955 rockchip_set_mux(bank, pins[cnt] - bank->pin_base, 0);
964 * The calls to gpio_direction_output() and gpio_direction_input()
965 * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
966 * function called from the gpiolib interface).
968 static int _rockchip_pmx_gpio_set_direction(struct gpio_chip *chip,
971 struct rockchip_pin_bank *bank;
976 bank = gc_to_pin_bank(chip);
978 ret = rockchip_set_mux(bank, pin, RK_FUNC_GPIO);
982 clk_enable(bank->clk);
983 spin_lock_irqsave(&bank->slock, flags);
985 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
986 /* set bit to 1 for output, 0 for input */
991 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
993 spin_unlock_irqrestore(&bank->slock, flags);
994 clk_disable(bank->clk);
999 static int rockchip_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
1000 struct pinctrl_gpio_range *range,
1001 unsigned offset, bool input)
1003 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1004 struct gpio_chip *chip;
1008 pin = offset - chip->base;
1009 dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n",
1010 offset, range->name, pin, input ? "input" : "output");
1012 return _rockchip_pmx_gpio_set_direction(chip, offset - chip->base,
1016 static const struct pinmux_ops rockchip_pmx_ops = {
1017 .get_functions_count = rockchip_pmx_get_funcs_count,
1018 .get_function_name = rockchip_pmx_get_func_name,
1019 .get_function_groups = rockchip_pmx_get_groups,
1020 .set_mux = rockchip_pmx_set,
1021 .gpio_set_direction = rockchip_pmx_gpio_set_direction,
1025 * Pinconf_ops handling
1028 static bool rockchip_pinconf_pull_valid(struct rockchip_pin_ctrl *ctrl,
1029 enum pin_config_param pull)
1031 switch (ctrl->type) {
1033 return (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT ||
1034 pull == PIN_CONFIG_BIAS_DISABLE);
1036 return pull ? false : true;
1040 return (pull != PIN_CONFIG_BIAS_PULL_PIN_DEFAULT);
1046 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
1047 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset);
1049 /* set the pin config settings for a specified pin */
1050 static int rockchip_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
1051 unsigned long *configs, unsigned num_configs)
1053 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1054 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
1055 enum pin_config_param param;
1060 for (i = 0; i < num_configs; i++) {
1061 param = pinconf_to_config_param(configs[i]);
1062 arg = pinconf_to_config_argument(configs[i]);
1065 case PIN_CONFIG_BIAS_DISABLE:
1066 rc = rockchip_set_pull(bank, pin - bank->pin_base,
1071 case PIN_CONFIG_BIAS_PULL_UP:
1072 case PIN_CONFIG_BIAS_PULL_DOWN:
1073 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
1074 case PIN_CONFIG_BIAS_BUS_HOLD:
1075 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
1081 rc = rockchip_set_pull(bank, pin - bank->pin_base,
1086 case PIN_CONFIG_OUTPUT:
1087 rockchip_gpio_set(&bank->gpio_chip,
1088 pin - bank->pin_base, arg);
1089 rc = _rockchip_pmx_gpio_set_direction(&bank->gpio_chip,
1090 pin - bank->pin_base, false);
1094 case PIN_CONFIG_DRIVE_STRENGTH:
1095 /* rk3288 is the first with per-pin drive-strength */
1096 if (!info->ctrl->drv_calc_reg)
1099 rc = rockchip_set_drive_perpin(bank,
1100 pin - bank->pin_base, arg);
1108 } /* for each config */
1113 /* get the pin config settings for a specified pin */
1114 static int rockchip_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
1115 unsigned long *config)
1117 struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1118 struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
1119 enum pin_config_param param = pinconf_to_config_param(*config);
1124 case PIN_CONFIG_BIAS_DISABLE:
1125 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
1130 case PIN_CONFIG_BIAS_PULL_UP:
1131 case PIN_CONFIG_BIAS_PULL_DOWN:
1132 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
1133 case PIN_CONFIG_BIAS_BUS_HOLD:
1134 if (!rockchip_pinconf_pull_valid(info->ctrl, param))
1137 if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
1142 case PIN_CONFIG_OUTPUT:
1143 rc = rockchip_get_mux(bank, pin - bank->pin_base);
1144 if (rc != RK_FUNC_GPIO)
1147 rc = rockchip_gpio_get(&bank->gpio_chip, pin - bank->pin_base);
1153 case PIN_CONFIG_DRIVE_STRENGTH:
1154 /* rk3288 is the first with per-pin drive-strength */
1155 if (!info->ctrl->drv_calc_reg)
1158 rc = rockchip_get_drive_perpin(bank, pin - bank->pin_base);
1169 *config = pinconf_to_config_packed(param, arg);
1174 static const struct pinconf_ops rockchip_pinconf_ops = {
1175 .pin_config_get = rockchip_pinconf_get,
1176 .pin_config_set = rockchip_pinconf_set,
1180 static const struct of_device_id rockchip_bank_match[] = {
1181 { .compatible = "rockchip,gpio-bank" },
1182 { .compatible = "rockchip,rk3188-gpio-bank0" },
1186 static void rockchip_pinctrl_child_count(struct rockchip_pinctrl *info,
1187 struct device_node *np)
1189 struct device_node *child;
1191 for_each_child_of_node(np, child) {
1192 if (of_match_node(rockchip_bank_match, child))
1196 info->ngroups += of_get_child_count(child);
1200 static int rockchip_pinctrl_parse_groups(struct device_node *np,
1201 struct rockchip_pin_group *grp,
1202 struct rockchip_pinctrl *info,
1205 struct rockchip_pin_bank *bank;
1212 dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
1214 /* Initialise group */
1215 grp->name = np->name;
1218 * the binding format is rockchip,pins = <bank pin mux CONFIG>,
1219 * do sanity check and calculate pins number
1221 list = of_get_property(np, "rockchip,pins", &size);
1222 /* we do not check return since it's safe node passed down */
1223 size /= sizeof(*list);
1224 if (!size || size % 4) {
1225 dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
1229 grp->npins = size / 4;
1231 grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
1233 grp->data = devm_kzalloc(info->dev, grp->npins *
1234 sizeof(struct rockchip_pin_config),
1236 if (!grp->pins || !grp->data)
1239 for (i = 0, j = 0; i < size; i += 4, j++) {
1240 const __be32 *phandle;
1241 struct device_node *np_config;
1243 num = be32_to_cpu(*list++);
1244 bank = bank_num_to_bank(info, num);
1246 return PTR_ERR(bank);
1248 grp->pins[j] = bank->pin_base + be32_to_cpu(*list++);
1249 grp->data[j].func = be32_to_cpu(*list++);
1255 np_config = of_find_node_by_phandle(be32_to_cpup(phandle));
1256 ret = pinconf_generic_parse_dt_config(np_config, NULL,
1257 &grp->data[j].configs, &grp->data[j].nconfigs);
1265 static int rockchip_pinctrl_parse_functions(struct device_node *np,
1266 struct rockchip_pinctrl *info,
1269 struct device_node *child;
1270 struct rockchip_pmx_func *func;
1271 struct rockchip_pin_group *grp;
1273 static u32 grp_index;
1276 dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
1278 func = &info->functions[index];
1280 /* Initialise function */
1281 func->name = np->name;
1282 func->ngroups = of_get_child_count(np);
1283 if (func->ngroups <= 0)
1286 func->groups = devm_kzalloc(info->dev,
1287 func->ngroups * sizeof(char *), GFP_KERNEL);
1291 for_each_child_of_node(np, child) {
1292 func->groups[i] = child->name;
1293 grp = &info->groups[grp_index++];
1294 ret = rockchip_pinctrl_parse_groups(child, grp, info, i++);
1302 static int rockchip_pinctrl_parse_dt(struct platform_device *pdev,
1303 struct rockchip_pinctrl *info)
1305 struct device *dev = &pdev->dev;
1306 struct device_node *np = dev->of_node;
1307 struct device_node *child;
1311 rockchip_pinctrl_child_count(info, np);
1313 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1314 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
1316 info->functions = devm_kzalloc(dev, info->nfunctions *
1317 sizeof(struct rockchip_pmx_func),
1319 if (!info->functions) {
1320 dev_err(dev, "failed to allocate memory for function list\n");
1324 info->groups = devm_kzalloc(dev, info->ngroups *
1325 sizeof(struct rockchip_pin_group),
1327 if (!info->groups) {
1328 dev_err(dev, "failed allocate memory for ping group list\n");
1334 for_each_child_of_node(np, child) {
1335 if (of_match_node(rockchip_bank_match, child))
1338 ret = rockchip_pinctrl_parse_functions(child, info, i++);
1340 dev_err(&pdev->dev, "failed to parse function\n");
1348 static int rockchip_pinctrl_register(struct platform_device *pdev,
1349 struct rockchip_pinctrl *info)
1351 struct pinctrl_desc *ctrldesc = &info->pctl;
1352 struct pinctrl_pin_desc *pindesc, *pdesc;
1353 struct rockchip_pin_bank *pin_bank;
1357 ctrldesc->name = "rockchip-pinctrl";
1358 ctrldesc->owner = THIS_MODULE;
1359 ctrldesc->pctlops = &rockchip_pctrl_ops;
1360 ctrldesc->pmxops = &rockchip_pmx_ops;
1361 ctrldesc->confops = &rockchip_pinconf_ops;
1363 pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
1364 info->ctrl->nr_pins, GFP_KERNEL);
1366 dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n");
1369 ctrldesc->pins = pindesc;
1370 ctrldesc->npins = info->ctrl->nr_pins;
1373 for (bank = 0 , k = 0; bank < info->ctrl->nr_banks; bank++) {
1374 pin_bank = &info->ctrl->pin_banks[bank];
1375 for (pin = 0; pin < pin_bank->nr_pins; pin++, k++) {
1377 pdesc->name = kasprintf(GFP_KERNEL, "%s-%d",
1378 pin_bank->name, pin);
1383 ret = rockchip_pinctrl_parse_dt(pdev, info);
1387 info->pctl_dev = pinctrl_register(ctrldesc, &pdev->dev, info);
1388 if (IS_ERR(info->pctl_dev)) {
1389 dev_err(&pdev->dev, "could not register pinctrl driver\n");
1390 return PTR_ERR(info->pctl_dev);
1393 for (bank = 0; bank < info->ctrl->nr_banks; ++bank) {
1394 pin_bank = &info->ctrl->pin_banks[bank];
1395 pin_bank->grange.name = pin_bank->name;
1396 pin_bank->grange.id = bank;
1397 pin_bank->grange.pin_base = pin_bank->pin_base;
1398 pin_bank->grange.base = pin_bank->gpio_chip.base;
1399 pin_bank->grange.npins = pin_bank->gpio_chip.ngpio;
1400 pin_bank->grange.gc = &pin_bank->gpio_chip;
1401 pinctrl_add_gpio_range(info->pctl_dev, &pin_bank->grange);
1411 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
1413 struct rockchip_pin_bank *bank = gc_to_pin_bank(gc);
1414 void __iomem *reg = bank->reg_base + GPIO_SWPORT_DR;
1415 unsigned long flags;
1418 clk_enable(bank->clk);
1419 spin_lock_irqsave(&bank->slock, flags);
1422 data &= ~BIT(offset);
1424 data |= BIT(offset);
1427 spin_unlock_irqrestore(&bank->slock, flags);
1428 clk_disable(bank->clk);
1432 * Returns the level of the pin for input direction and setting of the DR
1433 * register for output gpios.
1435 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset)
1437 struct rockchip_pin_bank *bank = gc_to_pin_bank(gc);
1440 clk_enable(bank->clk);
1441 data = readl(bank->reg_base + GPIO_EXT_PORT);
1442 clk_disable(bank->clk);
1449 * gpiolib gpio_direction_input callback function. The setting of the pin
1450 * mux function as 'gpio input' will be handled by the pinctrl susbsystem
1453 static int rockchip_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
1455 return pinctrl_gpio_direction_input(gc->base + offset);
1459 * gpiolib gpio_direction_output callback function. The setting of the pin
1460 * mux function as 'gpio output' will be handled by the pinctrl susbsystem
1463 static int rockchip_gpio_direction_output(struct gpio_chip *gc,
1464 unsigned offset, int value)
1466 rockchip_gpio_set(gc, offset, value);
1467 return pinctrl_gpio_direction_output(gc->base + offset);
1471 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
1472 * and a virtual IRQ, if not already present.
1474 static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
1476 struct rockchip_pin_bank *bank = gc_to_pin_bank(gc);
1482 virq = irq_create_mapping(bank->domain, offset);
1484 return (virq) ? : -ENXIO;
1487 static const struct gpio_chip rockchip_gpiolib_chip = {
1488 .request = gpiochip_generic_request,
1489 .free = gpiochip_generic_free,
1490 .set = rockchip_gpio_set,
1491 .get = rockchip_gpio_get,
1492 .direction_input = rockchip_gpio_direction_input,
1493 .direction_output = rockchip_gpio_direction_output,
1494 .to_irq = rockchip_gpio_to_irq,
1495 .owner = THIS_MODULE,
1499 * Interrupt handling
1502 static void rockchip_irq_demux(struct irq_desc *desc)
1504 struct irq_chip *chip = irq_desc_get_chip(desc);
1505 struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc);
1508 dev_dbg(bank->drvdata->dev, "got irq for bank %s\n", bank->name);
1510 chained_irq_enter(chip, desc);
1512 pend = readl_relaxed(bank->reg_base + GPIO_INT_STATUS);
1515 unsigned int irq, virq;
1519 virq = irq_linear_revmap(bank->domain, irq);
1522 dev_err(bank->drvdata->dev, "unmapped irq %d\n", irq);
1526 dev_dbg(bank->drvdata->dev, "handling irq %d\n", irq);
1529 * Triggering IRQ on both rising and falling edge
1530 * needs manual intervention.
1532 if (bank->toggle_edge_mode & BIT(irq)) {
1533 u32 data, data_old, polarity;
1534 unsigned long flags;
1536 data = readl_relaxed(bank->reg_base + GPIO_EXT_PORT);
1538 spin_lock_irqsave(&bank->slock, flags);
1540 polarity = readl_relaxed(bank->reg_base +
1542 if (data & BIT(irq))
1543 polarity &= ~BIT(irq);
1545 polarity |= BIT(irq);
1547 bank->reg_base + GPIO_INT_POLARITY);
1549 spin_unlock_irqrestore(&bank->slock, flags);
1552 data = readl_relaxed(bank->reg_base +
1554 } while ((data & BIT(irq)) != (data_old & BIT(irq)));
1557 generic_handle_irq(virq);
1560 chained_irq_exit(chip, desc);
1563 static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
1565 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1566 struct rockchip_pin_bank *bank = gc->private;
1567 u32 mask = BIT(d->hwirq);
1571 unsigned long flags;
1574 /* make sure the pin is configured as gpio input */
1575 ret = rockchip_set_mux(bank, d->hwirq, RK_FUNC_GPIO);
1579 clk_enable(bank->clk);
1580 spin_lock_irqsave(&bank->slock, flags);
1582 data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
1584 writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
1586 spin_unlock_irqrestore(&bank->slock, flags);
1588 if (type & IRQ_TYPE_EDGE_BOTH)
1589 irq_set_handler_locked(d, handle_edge_irq);
1591 irq_set_handler_locked(d, handle_level_irq);
1593 spin_lock_irqsave(&bank->slock, flags);
1596 level = readl_relaxed(gc->reg_base + GPIO_INTTYPE_LEVEL);
1597 polarity = readl_relaxed(gc->reg_base + GPIO_INT_POLARITY);
1600 case IRQ_TYPE_EDGE_BOTH:
1601 bank->toggle_edge_mode |= mask;
1605 * Determine gpio state. If 1 next interrupt should be falling
1608 data = readl(bank->reg_base + GPIO_EXT_PORT);
1614 case IRQ_TYPE_EDGE_RISING:
1615 bank->toggle_edge_mode &= ~mask;
1619 case IRQ_TYPE_EDGE_FALLING:
1620 bank->toggle_edge_mode &= ~mask;
1624 case IRQ_TYPE_LEVEL_HIGH:
1625 bank->toggle_edge_mode &= ~mask;
1629 case IRQ_TYPE_LEVEL_LOW:
1630 bank->toggle_edge_mode &= ~mask;
1636 spin_unlock_irqrestore(&bank->slock, flags);
1637 clk_disable(bank->clk);
1641 writel_relaxed(level, gc->reg_base + GPIO_INTTYPE_LEVEL);
1642 writel_relaxed(polarity, gc->reg_base + GPIO_INT_POLARITY);
1645 spin_unlock_irqrestore(&bank->slock, flags);
1646 clk_disable(bank->clk);
1651 static void rockchip_irq_suspend(struct irq_data *d)
1653 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1654 struct rockchip_pin_bank *bank = gc->private;
1656 clk_enable(bank->clk);
1657 bank->saved_masks = irq_reg_readl(gc, GPIO_INTMASK);
1658 irq_reg_writel(gc, ~gc->wake_active, GPIO_INTMASK);
1659 clk_disable(bank->clk);
1662 static void rockchip_irq_resume(struct irq_data *d)
1664 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1665 struct rockchip_pin_bank *bank = gc->private;
1667 clk_enable(bank->clk);
1668 irq_reg_writel(gc, bank->saved_masks, GPIO_INTMASK);
1669 clk_disable(bank->clk);
1672 static void rockchip_irq_gc_mask_clr_bit(struct irq_data *d)
1674 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1675 struct rockchip_pin_bank *bank = gc->private;
1677 clk_enable(bank->clk);
1678 irq_gc_mask_clr_bit(d);
1681 void rockchip_irq_gc_mask_set_bit(struct irq_data *d)
1683 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
1684 struct rockchip_pin_bank *bank = gc->private;
1686 irq_gc_mask_set_bit(d);
1687 clk_disable(bank->clk);
1690 static int rockchip_interrupts_register(struct platform_device *pdev,
1691 struct rockchip_pinctrl *info)
1693 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1694 struct rockchip_pin_bank *bank = ctrl->pin_banks;
1695 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
1696 struct irq_chip_generic *gc;
1700 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
1702 dev_warn(&pdev->dev, "bank %s is not valid\n",
1707 ret = clk_enable(bank->clk);
1709 dev_err(&pdev->dev, "failed to enable clock for bank %s\n",
1714 bank->domain = irq_domain_add_linear(bank->of_node, 32,
1715 &irq_generic_chip_ops, NULL);
1716 if (!bank->domain) {
1717 dev_warn(&pdev->dev, "could not initialize irq domain for bank %s\n",
1719 clk_disable(bank->clk);
1723 ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1,
1724 "rockchip_gpio_irq", handle_level_irq,
1725 clr, 0, IRQ_GC_INIT_MASK_CACHE);
1727 dev_err(&pdev->dev, "could not alloc generic chips for bank %s\n",
1729 irq_domain_remove(bank->domain);
1730 clk_disable(bank->clk);
1735 * Linux assumes that all interrupts start out disabled/masked.
1736 * Our driver only uses the concept of masked and always keeps
1737 * things enabled, so for us that's all masked and all enabled.
1739 writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTMASK);
1740 writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTEN);
1742 gc = irq_get_domain_generic_chip(bank->domain, 0);
1743 gc->reg_base = bank->reg_base;
1745 gc->chip_types[0].regs.mask = GPIO_INTMASK;
1746 gc->chip_types[0].regs.ack = GPIO_PORTS_EOI;
1747 gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
1748 gc->chip_types[0].chip.irq_mask = rockchip_irq_gc_mask_set_bit;
1749 gc->chip_types[0].chip.irq_unmask =
1750 rockchip_irq_gc_mask_clr_bit;
1751 gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
1752 gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend;
1753 gc->chip_types[0].chip.irq_resume = rockchip_irq_resume;
1754 gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type;
1755 gc->wake_enabled = IRQ_MSK(bank->nr_pins);
1757 irq_set_chained_handler_and_data(bank->irq,
1758 rockchip_irq_demux, bank);
1760 /* map the gpio irqs here, when the clock is still running */
1761 for (j = 0 ; j < 32 ; j++)
1762 irq_create_mapping(bank->domain, j);
1764 clk_disable(bank->clk);
1770 static int rockchip_gpiolib_register(struct platform_device *pdev,
1771 struct rockchip_pinctrl *info)
1773 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1774 struct rockchip_pin_bank *bank = ctrl->pin_banks;
1775 struct gpio_chip *gc;
1779 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
1781 dev_warn(&pdev->dev, "bank %s is not valid\n",
1786 bank->gpio_chip = rockchip_gpiolib_chip;
1788 gc = &bank->gpio_chip;
1789 gc->base = bank->pin_base;
1790 gc->ngpio = bank->nr_pins;
1791 gc->dev = &pdev->dev;
1792 gc->of_node = bank->of_node;
1793 gc->label = bank->name;
1795 ret = gpiochip_add(gc);
1797 dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
1803 rockchip_interrupts_register(pdev, info);
1808 for (--i, --bank; i >= 0; --i, --bank) {
1811 gpiochip_remove(&bank->gpio_chip);
1816 static int rockchip_gpiolib_unregister(struct platform_device *pdev,
1817 struct rockchip_pinctrl *info)
1819 struct rockchip_pin_ctrl *ctrl = info->ctrl;
1820 struct rockchip_pin_bank *bank = ctrl->pin_banks;
1823 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
1826 gpiochip_remove(&bank->gpio_chip);
1832 static int rockchip_get_bank_data(struct rockchip_pin_bank *bank,
1833 struct rockchip_pinctrl *info)
1835 struct resource res;
1838 if (of_address_to_resource(bank->of_node, 0, &res)) {
1839 dev_err(info->dev, "cannot find IO resource for bank\n");
1843 bank->reg_base = devm_ioremap_resource(info->dev, &res);
1844 if (IS_ERR(bank->reg_base))
1845 return PTR_ERR(bank->reg_base);
1848 * special case, where parts of the pull setting-registers are
1849 * part of the PMU register space
1851 if (of_device_is_compatible(bank->of_node,
1852 "rockchip,rk3188-gpio-bank0")) {
1853 struct device_node *node;
1855 node = of_parse_phandle(bank->of_node->parent,
1858 if (of_address_to_resource(bank->of_node, 1, &res)) {
1859 dev_err(info->dev, "cannot find IO resource for bank\n");
1863 base = devm_ioremap_resource(info->dev, &res);
1865 return PTR_ERR(base);
1866 rockchip_regmap_config.max_register =
1867 resource_size(&res) - 4;
1868 rockchip_regmap_config.name =
1869 "rockchip,rk3188-gpio-bank0-pull";
1870 bank->regmap_pull = devm_regmap_init_mmio(info->dev,
1872 &rockchip_regmap_config);
1876 bank->irq = irq_of_parse_and_map(bank->of_node, 0);
1878 bank->clk = of_clk_get(bank->of_node, 0);
1879 if (IS_ERR(bank->clk))
1880 return PTR_ERR(bank->clk);
1882 return clk_prepare(bank->clk);
1885 static const struct of_device_id rockchip_pinctrl_dt_match[];
1887 /* retrieve the soc specific data */
1888 static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data(
1889 struct rockchip_pinctrl *d,
1890 struct platform_device *pdev)
1892 const struct of_device_id *match;
1893 struct device_node *node = pdev->dev.of_node;
1894 struct device_node *np;
1895 struct rockchip_pin_ctrl *ctrl;
1896 struct rockchip_pin_bank *bank;
1897 int grf_offs, pmu_offs, i, j;
1899 match = of_match_node(rockchip_pinctrl_dt_match, node);
1900 ctrl = (struct rockchip_pin_ctrl *)match->data;
1902 for_each_child_of_node(node, np) {
1903 if (!of_find_property(np, "gpio-controller", NULL))
1906 bank = ctrl->pin_banks;
1907 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
1908 if (!strcmp(bank->name, np->name)) {
1911 if (!rockchip_get_bank_data(bank, d))
1919 grf_offs = ctrl->grf_mux_offset;
1920 pmu_offs = ctrl->pmu_mux_offset;
1921 bank = ctrl->pin_banks;
1922 for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
1925 spin_lock_init(&bank->slock);
1927 bank->pin_base = ctrl->nr_pins;
1928 ctrl->nr_pins += bank->nr_pins;
1930 /* calculate iomux offsets */
1931 for (j = 0; j < 4; j++) {
1932 struct rockchip_iomux *iom = &bank->iomux[j];
1935 if (bank_pins >= bank->nr_pins)
1938 /* preset offset value, set new start value */
1939 if (iom->offset >= 0) {
1940 if (iom->type & IOMUX_SOURCE_PMU)
1941 pmu_offs = iom->offset;
1943 grf_offs = iom->offset;
1944 } else { /* set current offset */
1945 iom->offset = (iom->type & IOMUX_SOURCE_PMU) ?
1946 pmu_offs : grf_offs;
1949 dev_dbg(d->dev, "bank %d, iomux %d has offset 0x%x\n",
1953 * Increase offset according to iomux width.
1954 * 4bit iomux'es are spread over two registers.
1956 inc = (iom->type & IOMUX_WIDTH_4BIT) ? 8 : 4;
1957 if (iom->type & IOMUX_SOURCE_PMU)
1969 #define RK3288_GRF_GPIO6C_IOMUX 0x64
1970 #define GPIO6C6_SEL_WRITE_ENABLE BIT(28)
1972 static u32 rk3288_grf_gpio6c_iomux;
1974 static int __maybe_unused rockchip_pinctrl_suspend(struct device *dev)
1976 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
1977 int ret = pinctrl_force_sleep(info->pctl_dev);
1983 * RK3288 GPIO6_C6 mux would be modified by Maskrom when resume, so save
1984 * the setting here, and restore it at resume.
1986 if (info->ctrl->type == RK3288) {
1987 ret = regmap_read(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
1988 &rk3288_grf_gpio6c_iomux);
1990 pinctrl_force_default(info->pctl_dev);
1998 static int __maybe_unused rockchip_pinctrl_resume(struct device *dev)
2000 struct rockchip_pinctrl *info = dev_get_drvdata(dev);
2001 int ret = regmap_write(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
2002 rk3288_grf_gpio6c_iomux |
2003 GPIO6C6_SEL_WRITE_ENABLE);
2008 return pinctrl_force_default(info->pctl_dev);
2011 static SIMPLE_DEV_PM_OPS(rockchip_pinctrl_dev_pm_ops, rockchip_pinctrl_suspend,
2012 rockchip_pinctrl_resume);
2014 static int rockchip_pinctrl_probe(struct platform_device *pdev)
2016 struct rockchip_pinctrl *info;
2017 struct device *dev = &pdev->dev;
2018 struct rockchip_pin_ctrl *ctrl;
2019 struct device_node *np = pdev->dev.of_node, *node;
2020 struct resource *res;
2024 if (!dev->of_node) {
2025 dev_err(dev, "device tree node not found\n");
2029 info = devm_kzalloc(dev, sizeof(struct rockchip_pinctrl), GFP_KERNEL);
2035 ctrl = rockchip_pinctrl_get_soc_data(info, pdev);
2037 dev_err(dev, "driver data not available\n");
2042 node = of_parse_phandle(np, "rockchip,grf", 0);
2044 info->regmap_base = syscon_node_to_regmap(node);
2045 if (IS_ERR(info->regmap_base))
2046 return PTR_ERR(info->regmap_base);
2048 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2049 base = devm_ioremap_resource(&pdev->dev, res);
2051 return PTR_ERR(base);
2053 rockchip_regmap_config.max_register = resource_size(res) - 4;
2054 rockchip_regmap_config.name = "rockchip,pinctrl";
2055 info->regmap_base = devm_regmap_init_mmio(&pdev->dev, base,
2056 &rockchip_regmap_config);
2058 /* to check for the old dt-bindings */
2059 info->reg_size = resource_size(res);
2061 /* Honor the old binding, with pull registers as 2nd resource */
2062 if (ctrl->type == RK3188 && info->reg_size < 0x200) {
2063 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2064 base = devm_ioremap_resource(&pdev->dev, res);
2066 return PTR_ERR(base);
2068 rockchip_regmap_config.max_register =
2069 resource_size(res) - 4;
2070 rockchip_regmap_config.name = "rockchip,pinctrl-pull";
2071 info->regmap_pull = devm_regmap_init_mmio(&pdev->dev,
2073 &rockchip_regmap_config);
2077 /* try to find the optional reference to the pmu syscon */
2078 node = of_parse_phandle(np, "rockchip,pmu", 0);
2080 info->regmap_pmu = syscon_node_to_regmap(node);
2081 if (IS_ERR(info->regmap_pmu))
2082 return PTR_ERR(info->regmap_pmu);
2085 ret = rockchip_gpiolib_register(pdev, info);
2089 ret = rockchip_pinctrl_register(pdev, info);
2091 rockchip_gpiolib_unregister(pdev, info);
2095 platform_set_drvdata(pdev, info);
2100 static struct rockchip_pin_bank rk2928_pin_banks[] = {
2101 PIN_BANK(0, 32, "gpio0"),
2102 PIN_BANK(1, 32, "gpio1"),
2103 PIN_BANK(2, 32, "gpio2"),
2104 PIN_BANK(3, 32, "gpio3"),
2107 static struct rockchip_pin_ctrl rk2928_pin_ctrl = {
2108 .pin_banks = rk2928_pin_banks,
2109 .nr_banks = ARRAY_SIZE(rk2928_pin_banks),
2110 .label = "RK2928-GPIO",
2112 .grf_mux_offset = 0xa8,
2113 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
2116 static struct rockchip_pin_bank rk3036_pin_banks[] = {
2117 PIN_BANK(0, 32, "gpio0"),
2118 PIN_BANK(1, 32, "gpio1"),
2119 PIN_BANK(2, 32, "gpio2"),
2122 static struct rockchip_pin_ctrl rk3036_pin_ctrl = {
2123 .pin_banks = rk3036_pin_banks,
2124 .nr_banks = ARRAY_SIZE(rk3036_pin_banks),
2125 .label = "RK3036-GPIO",
2127 .grf_mux_offset = 0xa8,
2128 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
2131 static struct rockchip_pin_bank rk3066a_pin_banks[] = {
2132 PIN_BANK(0, 32, "gpio0"),
2133 PIN_BANK(1, 32, "gpio1"),
2134 PIN_BANK(2, 32, "gpio2"),
2135 PIN_BANK(3, 32, "gpio3"),
2136 PIN_BANK(4, 32, "gpio4"),
2137 PIN_BANK(6, 16, "gpio6"),
2140 static struct rockchip_pin_ctrl rk3066a_pin_ctrl = {
2141 .pin_banks = rk3066a_pin_banks,
2142 .nr_banks = ARRAY_SIZE(rk3066a_pin_banks),
2143 .label = "RK3066a-GPIO",
2145 .grf_mux_offset = 0xa8,
2146 .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
2149 static struct rockchip_pin_bank rk3066b_pin_banks[] = {
2150 PIN_BANK(0, 32, "gpio0"),
2151 PIN_BANK(1, 32, "gpio1"),
2152 PIN_BANK(2, 32, "gpio2"),
2153 PIN_BANK(3, 32, "gpio3"),
2156 static struct rockchip_pin_ctrl rk3066b_pin_ctrl = {
2157 .pin_banks = rk3066b_pin_banks,
2158 .nr_banks = ARRAY_SIZE(rk3066b_pin_banks),
2159 .label = "RK3066b-GPIO",
2161 .grf_mux_offset = 0x60,
2164 static struct rockchip_pin_bank rk3188_pin_banks[] = {
2165 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_GPIO_ONLY, 0, 0, 0),
2166 PIN_BANK(1, 32, "gpio1"),
2167 PIN_BANK(2, 32, "gpio2"),
2168 PIN_BANK(3, 32, "gpio3"),
2171 static struct rockchip_pin_ctrl rk3188_pin_ctrl = {
2172 .pin_banks = rk3188_pin_banks,
2173 .nr_banks = ARRAY_SIZE(rk3188_pin_banks),
2174 .label = "RK3188-GPIO",
2176 .grf_mux_offset = 0x60,
2177 .pull_calc_reg = rk3188_calc_pull_reg_and_bit,
2180 static struct rockchip_pin_bank rk3228_pin_banks[] = {
2181 PIN_BANK(0, 32, "gpio0"),
2182 PIN_BANK(1, 32, "gpio1"),
2183 PIN_BANK(2, 32, "gpio2"),
2184 PIN_BANK(3, 32, "gpio3"),
2187 static struct rockchip_pin_ctrl rk3228_pin_ctrl = {
2188 .pin_banks = rk3228_pin_banks,
2189 .nr_banks = ARRAY_SIZE(rk3228_pin_banks),
2190 .label = "RK3228-GPIO",
2192 .grf_mux_offset = 0x0,
2193 .pull_calc_reg = rk3228_calc_pull_reg_and_bit,
2194 .drv_calc_reg = rk3228_calc_drv_reg_and_bit,
2197 static struct rockchip_pin_bank rk3288_pin_banks[] = {
2198 PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU,
2203 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED,
2208 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED),
2209 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT),
2210 PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT,
2215 PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED,
2220 PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED),
2221 PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0,
2226 PIN_BANK(8, 16, "gpio8"),
2229 static struct rockchip_pin_ctrl rk3288_pin_ctrl = {
2230 .pin_banks = rk3288_pin_banks,
2231 .nr_banks = ARRAY_SIZE(rk3288_pin_banks),
2232 .label = "RK3288-GPIO",
2234 .grf_mux_offset = 0x0,
2235 .pmu_mux_offset = 0x84,
2236 .pull_calc_reg = rk3288_calc_pull_reg_and_bit,
2237 .drv_calc_reg = rk3288_calc_drv_reg_and_bit,
2240 static struct rockchip_pin_bank rk3368_pin_banks[] = {
2241 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
2246 PIN_BANK(1, 32, "gpio1"),
2247 PIN_BANK(2, 32, "gpio2"),
2248 PIN_BANK(3, 32, "gpio3"),
2251 static struct rockchip_pin_ctrl rk3368_pin_ctrl = {
2252 .pin_banks = rk3368_pin_banks,
2253 .nr_banks = ARRAY_SIZE(rk3368_pin_banks),
2254 .label = "RK3368-GPIO",
2256 .grf_mux_offset = 0x0,
2257 .pmu_mux_offset = 0x0,
2258 .pull_calc_reg = rk3368_calc_pull_reg_and_bit,
2259 .drv_calc_reg = rk3368_calc_drv_reg_and_bit,
2263 static const struct of_device_id rockchip_pinctrl_dt_match[] = {
2264 { .compatible = "rockchip,rk2928-pinctrl",
2265 .data = (void *)&rk2928_pin_ctrl },
2266 { .compatible = "rockchip,rk3036-pinctrl",
2267 .data = (void *)&rk3036_pin_ctrl },
2268 { .compatible = "rockchip,rk3066a-pinctrl",
2269 .data = (void *)&rk3066a_pin_ctrl },
2270 { .compatible = "rockchip,rk3066b-pinctrl",
2271 .data = (void *)&rk3066b_pin_ctrl },
2272 { .compatible = "rockchip,rk3188-pinctrl",
2273 .data = (void *)&rk3188_pin_ctrl },
2274 { .compatible = "rockchip,rk3228-pinctrl",
2275 .data = (void *)&rk3228_pin_ctrl },
2276 { .compatible = "rockchip,rk3288-pinctrl",
2277 .data = (void *)&rk3288_pin_ctrl },
2278 { .compatible = "rockchip,rk3368-pinctrl",
2279 .data = (void *)&rk3368_pin_ctrl },
2282 MODULE_DEVICE_TABLE(of, rockchip_pinctrl_dt_match);
2284 static struct platform_driver rockchip_pinctrl_driver = {
2285 .probe = rockchip_pinctrl_probe,
2287 .name = "rockchip-pinctrl",
2288 .pm = &rockchip_pinctrl_dev_pm_ops,
2289 .of_match_table = rockchip_pinctrl_dt_match,
2293 static int __init rockchip_pinctrl_drv_register(void)
2295 return platform_driver_register(&rockchip_pinctrl_driver);
2297 postcore_initcall(rockchip_pinctrl_drv_register);
2299 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
2300 MODULE_DESCRIPTION("Rockchip pinctrl driver");
2301 MODULE_LICENSE("GPL v2");