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 RK3368_PULL_GRF_OFFSET 0x100
618 #define RK3368_PULL_PMU_OFFSET 0x10
620 static void rk3368_calc_pull_reg_and_bit(struct rockchip_pin_bank
*bank
,
621 int pin_num
, struct regmap
**regmap
,
624 struct rockchip_pinctrl
*info
= bank
->drvdata
;
626 /* The first 32 pins of the first bank are located in PMU */
627 if (bank
->bank_num
== 0) {
628 *regmap
= info
->regmap_pmu
;
629 *reg
= RK3368_PULL_PMU_OFFSET
;
631 *reg
+= ((pin_num
/ RK3188_PULL_PINS_PER_REG
) * 4);
632 *bit
= pin_num
% RK3188_PULL_PINS_PER_REG
;
633 *bit
*= RK3188_PULL_BITS_PER_PIN
;
635 *regmap
= info
->regmap_base
;
636 *reg
= RK3368_PULL_GRF_OFFSET
;
638 /* correct the offset, as we're starting with the 2nd bank */
640 *reg
+= bank
->bank_num
* RK3188_PULL_BANK_STRIDE
;
641 *reg
+= ((pin_num
/ RK3188_PULL_PINS_PER_REG
) * 4);
643 *bit
= (pin_num
% RK3188_PULL_PINS_PER_REG
);
644 *bit
*= RK3188_PULL_BITS_PER_PIN
;
648 #define RK3368_DRV_PMU_OFFSET 0x20
649 #define RK3368_DRV_GRF_OFFSET 0x200
651 static void rk3368_calc_drv_reg_and_bit(struct rockchip_pin_bank
*bank
,
652 int pin_num
, struct regmap
**regmap
,
655 struct rockchip_pinctrl
*info
= bank
->drvdata
;
657 /* The first 32 pins of the first bank are located in PMU */
658 if (bank
->bank_num
== 0) {
659 *regmap
= info
->regmap_pmu
;
660 *reg
= RK3368_DRV_PMU_OFFSET
;
662 *reg
+= ((pin_num
/ RK3288_DRV_PINS_PER_REG
) * 4);
663 *bit
= pin_num
% RK3288_DRV_PINS_PER_REG
;
664 *bit
*= RK3288_DRV_BITS_PER_PIN
;
666 *regmap
= info
->regmap_base
;
667 *reg
= RK3368_DRV_GRF_OFFSET
;
669 /* correct the offset, as we're starting with the 2nd bank */
671 *reg
+= bank
->bank_num
* RK3288_DRV_BANK_STRIDE
;
672 *reg
+= ((pin_num
/ RK3288_DRV_PINS_PER_REG
) * 4);
674 *bit
= (pin_num
% RK3288_DRV_PINS_PER_REG
);
675 *bit
*= RK3288_DRV_BITS_PER_PIN
;
679 static int rockchip_perpin_drv_list
[] = { 2, 4, 8, 12 };
681 static int rockchip_get_drive_perpin(struct rockchip_pin_bank
*bank
,
684 struct rockchip_pinctrl
*info
= bank
->drvdata
;
685 struct rockchip_pin_ctrl
*ctrl
= info
->ctrl
;
686 struct regmap
*regmap
;
691 ctrl
->drv_calc_reg(bank
, pin_num
, ®map
, ®
, &bit
);
693 ret
= regmap_read(regmap
, reg
, &data
);
698 data
&= (1 << RK3288_DRV_BITS_PER_PIN
) - 1;
700 return rockchip_perpin_drv_list
[data
];
703 static int rockchip_set_drive_perpin(struct rockchip_pin_bank
*bank
,
704 int pin_num
, int strength
)
706 struct rockchip_pinctrl
*info
= bank
->drvdata
;
707 struct rockchip_pin_ctrl
*ctrl
= info
->ctrl
;
708 struct regmap
*regmap
;
714 ctrl
->drv_calc_reg(bank
, pin_num
, ®map
, ®
, &bit
);
717 for (i
= 0; i
< ARRAY_SIZE(rockchip_perpin_drv_list
); i
++) {
718 if (rockchip_perpin_drv_list
[i
] == strength
) {
725 dev_err(info
->dev
, "unsupported driver strength %d\n",
730 spin_lock_irqsave(&bank
->slock
, flags
);
732 /* enable the write to the equivalent lower bits */
733 data
= ((1 << RK3288_DRV_BITS_PER_PIN
) - 1) << (bit
+ 16);
734 rmask
= data
| (data
>> 16);
735 data
|= (ret
<< bit
);
737 ret
= regmap_update_bits(regmap
, reg
, rmask
, data
);
738 spin_unlock_irqrestore(&bank
->slock
, flags
);
743 static int rockchip_get_pull(struct rockchip_pin_bank
*bank
, int pin_num
)
745 struct rockchip_pinctrl
*info
= bank
->drvdata
;
746 struct rockchip_pin_ctrl
*ctrl
= info
->ctrl
;
747 struct regmap
*regmap
;
752 /* rk3066b does support any pulls */
753 if (ctrl
->type
== RK3066B
)
754 return PIN_CONFIG_BIAS_DISABLE
;
756 ctrl
->pull_calc_reg(bank
, pin_num
, ®map
, ®
, &bit
);
758 ret
= regmap_read(regmap
, reg
, &data
);
762 switch (ctrl
->type
) {
764 return !(data
& BIT(bit
))
765 ? PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
766 : PIN_CONFIG_BIAS_DISABLE
;
771 data
&= (1 << RK3188_PULL_BITS_PER_PIN
) - 1;
775 return PIN_CONFIG_BIAS_DISABLE
;
777 return PIN_CONFIG_BIAS_PULL_UP
;
779 return PIN_CONFIG_BIAS_PULL_DOWN
;
781 return PIN_CONFIG_BIAS_BUS_HOLD
;
784 dev_err(info
->dev
, "unknown pull setting\n");
787 dev_err(info
->dev
, "unsupported pinctrl type\n");
792 static int rockchip_set_pull(struct rockchip_pin_bank
*bank
,
793 int pin_num
, int pull
)
795 struct rockchip_pinctrl
*info
= bank
->drvdata
;
796 struct rockchip_pin_ctrl
*ctrl
= info
->ctrl
;
797 struct regmap
*regmap
;
803 dev_dbg(info
->dev
, "setting pull of GPIO%d-%d to %d\n",
804 bank
->bank_num
, pin_num
, pull
);
806 /* rk3066b does support any pulls */
807 if (ctrl
->type
== RK3066B
)
808 return pull
? -EINVAL
: 0;
810 ctrl
->pull_calc_reg(bank
, pin_num
, ®map
, ®
, &bit
);
812 switch (ctrl
->type
) {
814 spin_lock_irqsave(&bank
->slock
, flags
);
816 data
= BIT(bit
+ 16);
817 if (pull
== PIN_CONFIG_BIAS_DISABLE
)
819 ret
= regmap_write(regmap
, reg
, data
);
821 spin_unlock_irqrestore(&bank
->slock
, flags
);
826 spin_lock_irqsave(&bank
->slock
, flags
);
828 /* enable the write to the equivalent lower bits */
829 data
= ((1 << RK3188_PULL_BITS_PER_PIN
) - 1) << (bit
+ 16);
830 rmask
= data
| (data
>> 16);
833 case PIN_CONFIG_BIAS_DISABLE
:
835 case PIN_CONFIG_BIAS_PULL_UP
:
838 case PIN_CONFIG_BIAS_PULL_DOWN
:
841 case PIN_CONFIG_BIAS_BUS_HOLD
:
845 spin_unlock_irqrestore(&bank
->slock
, flags
);
846 dev_err(info
->dev
, "unsupported pull setting %d\n",
851 ret
= regmap_update_bits(regmap
, reg
, rmask
, data
);
853 spin_unlock_irqrestore(&bank
->slock
, flags
);
856 dev_err(info
->dev
, "unsupported pinctrl type\n");
864 * Pinmux_ops handling
867 static int rockchip_pmx_get_funcs_count(struct pinctrl_dev
*pctldev
)
869 struct rockchip_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
871 return info
->nfunctions
;
874 static const char *rockchip_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
877 struct rockchip_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
879 return info
->functions
[selector
].name
;
882 static int rockchip_pmx_get_groups(struct pinctrl_dev
*pctldev
,
883 unsigned selector
, const char * const **groups
,
884 unsigned * const num_groups
)
886 struct rockchip_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
888 *groups
= info
->functions
[selector
].groups
;
889 *num_groups
= info
->functions
[selector
].ngroups
;
894 static int rockchip_pmx_set(struct pinctrl_dev
*pctldev
, unsigned selector
,
897 struct rockchip_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
898 const unsigned int *pins
= info
->groups
[group
].pins
;
899 const struct rockchip_pin_config
*data
= info
->groups
[group
].data
;
900 struct rockchip_pin_bank
*bank
;
903 dev_dbg(info
->dev
, "enable function %s group %s\n",
904 info
->functions
[selector
].name
, info
->groups
[group
].name
);
907 * for each pin in the pin group selected, program the correspoding pin
908 * pin function number in the config register.
910 for (cnt
= 0; cnt
< info
->groups
[group
].npins
; cnt
++) {
911 bank
= pin_to_bank(info
, pins
[cnt
]);
912 ret
= rockchip_set_mux(bank
, pins
[cnt
] - bank
->pin_base
,
919 /* revert the already done pin settings */
920 for (cnt
--; cnt
>= 0; cnt
--)
921 rockchip_set_mux(bank
, pins
[cnt
] - bank
->pin_base
, 0);
930 * The calls to gpio_direction_output() and gpio_direction_input()
931 * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
932 * function called from the gpiolib interface).
934 static int _rockchip_pmx_gpio_set_direction(struct gpio_chip
*chip
,
937 struct rockchip_pin_bank
*bank
;
942 bank
= gc_to_pin_bank(chip
);
944 ret
= rockchip_set_mux(bank
, pin
, RK_FUNC_GPIO
);
948 clk_enable(bank
->clk
);
949 spin_lock_irqsave(&bank
->slock
, flags
);
951 data
= readl_relaxed(bank
->reg_base
+ GPIO_SWPORT_DDR
);
952 /* set bit to 1 for output, 0 for input */
957 writel_relaxed(data
, bank
->reg_base
+ GPIO_SWPORT_DDR
);
959 spin_unlock_irqrestore(&bank
->slock
, flags
);
960 clk_disable(bank
->clk
);
965 static int rockchip_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
966 struct pinctrl_gpio_range
*range
,
967 unsigned offset
, bool input
)
969 struct rockchip_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
970 struct gpio_chip
*chip
;
974 pin
= offset
- chip
->base
;
975 dev_dbg(info
->dev
, "gpio_direction for pin %u as %s-%d to %s\n",
976 offset
, range
->name
, pin
, input
? "input" : "output");
978 return _rockchip_pmx_gpio_set_direction(chip
, offset
- chip
->base
,
982 static const struct pinmux_ops rockchip_pmx_ops
= {
983 .get_functions_count
= rockchip_pmx_get_funcs_count
,
984 .get_function_name
= rockchip_pmx_get_func_name
,
985 .get_function_groups
= rockchip_pmx_get_groups
,
986 .set_mux
= rockchip_pmx_set
,
987 .gpio_set_direction
= rockchip_pmx_gpio_set_direction
,
991 * Pinconf_ops handling
994 static bool rockchip_pinconf_pull_valid(struct rockchip_pin_ctrl
*ctrl
,
995 enum pin_config_param pull
)
997 switch (ctrl
->type
) {
999 return (pull
== PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
||
1000 pull
== PIN_CONFIG_BIAS_DISABLE
);
1002 return pull
? false : true;
1006 return (pull
!= PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
);
1012 static void rockchip_gpio_set(struct gpio_chip
*gc
, unsigned offset
, int value
);
1013 static int rockchip_gpio_get(struct gpio_chip
*gc
, unsigned offset
);
1015 /* set the pin config settings for a specified pin */
1016 static int rockchip_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
1017 unsigned long *configs
, unsigned num_configs
)
1019 struct rockchip_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
1020 struct rockchip_pin_bank
*bank
= pin_to_bank(info
, pin
);
1021 enum pin_config_param param
;
1026 for (i
= 0; i
< num_configs
; i
++) {
1027 param
= pinconf_to_config_param(configs
[i
]);
1028 arg
= pinconf_to_config_argument(configs
[i
]);
1031 case PIN_CONFIG_BIAS_DISABLE
:
1032 rc
= rockchip_set_pull(bank
, pin
- bank
->pin_base
,
1037 case PIN_CONFIG_BIAS_PULL_UP
:
1038 case PIN_CONFIG_BIAS_PULL_DOWN
:
1039 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
:
1040 case PIN_CONFIG_BIAS_BUS_HOLD
:
1041 if (!rockchip_pinconf_pull_valid(info
->ctrl
, param
))
1047 rc
= rockchip_set_pull(bank
, pin
- bank
->pin_base
,
1052 case PIN_CONFIG_OUTPUT
:
1053 rockchip_gpio_set(&bank
->gpio_chip
,
1054 pin
- bank
->pin_base
, arg
);
1055 rc
= _rockchip_pmx_gpio_set_direction(&bank
->gpio_chip
,
1056 pin
- bank
->pin_base
, false);
1060 case PIN_CONFIG_DRIVE_STRENGTH
:
1061 /* rk3288 is the first with per-pin drive-strength */
1062 if (!info
->ctrl
->drv_calc_reg
)
1065 rc
= rockchip_set_drive_perpin(bank
,
1066 pin
- bank
->pin_base
, arg
);
1074 } /* for each config */
1079 /* get the pin config settings for a specified pin */
1080 static int rockchip_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned int pin
,
1081 unsigned long *config
)
1083 struct rockchip_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
1084 struct rockchip_pin_bank
*bank
= pin_to_bank(info
, pin
);
1085 enum pin_config_param param
= pinconf_to_config_param(*config
);
1090 case PIN_CONFIG_BIAS_DISABLE
:
1091 if (rockchip_get_pull(bank
, pin
- bank
->pin_base
) != param
)
1096 case PIN_CONFIG_BIAS_PULL_UP
:
1097 case PIN_CONFIG_BIAS_PULL_DOWN
:
1098 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
:
1099 case PIN_CONFIG_BIAS_BUS_HOLD
:
1100 if (!rockchip_pinconf_pull_valid(info
->ctrl
, param
))
1103 if (rockchip_get_pull(bank
, pin
- bank
->pin_base
) != param
)
1108 case PIN_CONFIG_OUTPUT
:
1109 rc
= rockchip_get_mux(bank
, pin
- bank
->pin_base
);
1110 if (rc
!= RK_FUNC_GPIO
)
1113 rc
= rockchip_gpio_get(&bank
->gpio_chip
, pin
- bank
->pin_base
);
1119 case PIN_CONFIG_DRIVE_STRENGTH
:
1120 /* rk3288 is the first with per-pin drive-strength */
1121 if (!info
->ctrl
->drv_calc_reg
)
1124 rc
= rockchip_get_drive_perpin(bank
, pin
- bank
->pin_base
);
1135 *config
= pinconf_to_config_packed(param
, arg
);
1140 static const struct pinconf_ops rockchip_pinconf_ops
= {
1141 .pin_config_get
= rockchip_pinconf_get
,
1142 .pin_config_set
= rockchip_pinconf_set
,
1146 static const struct of_device_id rockchip_bank_match
[] = {
1147 { .compatible
= "rockchip,gpio-bank" },
1148 { .compatible
= "rockchip,rk3188-gpio-bank0" },
1152 static void rockchip_pinctrl_child_count(struct rockchip_pinctrl
*info
,
1153 struct device_node
*np
)
1155 struct device_node
*child
;
1157 for_each_child_of_node(np
, child
) {
1158 if (of_match_node(rockchip_bank_match
, child
))
1162 info
->ngroups
+= of_get_child_count(child
);
1166 static int rockchip_pinctrl_parse_groups(struct device_node
*np
,
1167 struct rockchip_pin_group
*grp
,
1168 struct rockchip_pinctrl
*info
,
1171 struct rockchip_pin_bank
*bank
;
1178 dev_dbg(info
->dev
, "group(%d): %s\n", index
, np
->name
);
1180 /* Initialise group */
1181 grp
->name
= np
->name
;
1184 * the binding format is rockchip,pins = <bank pin mux CONFIG>,
1185 * do sanity check and calculate pins number
1187 list
= of_get_property(np
, "rockchip,pins", &size
);
1188 /* we do not check return since it's safe node passed down */
1189 size
/= sizeof(*list
);
1190 if (!size
|| size
% 4) {
1191 dev_err(info
->dev
, "wrong pins number or pins and configs should be by 4\n");
1195 grp
->npins
= size
/ 4;
1197 grp
->pins
= devm_kzalloc(info
->dev
, grp
->npins
* sizeof(unsigned int),
1199 grp
->data
= devm_kzalloc(info
->dev
, grp
->npins
*
1200 sizeof(struct rockchip_pin_config
),
1202 if (!grp
->pins
|| !grp
->data
)
1205 for (i
= 0, j
= 0; i
< size
; i
+= 4, j
++) {
1206 const __be32
*phandle
;
1207 struct device_node
*np_config
;
1209 num
= be32_to_cpu(*list
++);
1210 bank
= bank_num_to_bank(info
, num
);
1212 return PTR_ERR(bank
);
1214 grp
->pins
[j
] = bank
->pin_base
+ be32_to_cpu(*list
++);
1215 grp
->data
[j
].func
= be32_to_cpu(*list
++);
1221 np_config
= of_find_node_by_phandle(be32_to_cpup(phandle
));
1222 ret
= pinconf_generic_parse_dt_config(np_config
, NULL
,
1223 &grp
->data
[j
].configs
, &grp
->data
[j
].nconfigs
);
1231 static int rockchip_pinctrl_parse_functions(struct device_node
*np
,
1232 struct rockchip_pinctrl
*info
,
1235 struct device_node
*child
;
1236 struct rockchip_pmx_func
*func
;
1237 struct rockchip_pin_group
*grp
;
1239 static u32 grp_index
;
1242 dev_dbg(info
->dev
, "parse function(%d): %s\n", index
, np
->name
);
1244 func
= &info
->functions
[index
];
1246 /* Initialise function */
1247 func
->name
= np
->name
;
1248 func
->ngroups
= of_get_child_count(np
);
1249 if (func
->ngroups
<= 0)
1252 func
->groups
= devm_kzalloc(info
->dev
,
1253 func
->ngroups
* sizeof(char *), GFP_KERNEL
);
1257 for_each_child_of_node(np
, child
) {
1258 func
->groups
[i
] = child
->name
;
1259 grp
= &info
->groups
[grp_index
++];
1260 ret
= rockchip_pinctrl_parse_groups(child
, grp
, info
, i
++);
1268 static int rockchip_pinctrl_parse_dt(struct platform_device
*pdev
,
1269 struct rockchip_pinctrl
*info
)
1271 struct device
*dev
= &pdev
->dev
;
1272 struct device_node
*np
= dev
->of_node
;
1273 struct device_node
*child
;
1277 rockchip_pinctrl_child_count(info
, np
);
1279 dev_dbg(&pdev
->dev
, "nfunctions = %d\n", info
->nfunctions
);
1280 dev_dbg(&pdev
->dev
, "ngroups = %d\n", info
->ngroups
);
1282 info
->functions
= devm_kzalloc(dev
, info
->nfunctions
*
1283 sizeof(struct rockchip_pmx_func
),
1285 if (!info
->functions
) {
1286 dev_err(dev
, "failed to allocate memory for function list\n");
1290 info
->groups
= devm_kzalloc(dev
, info
->ngroups
*
1291 sizeof(struct rockchip_pin_group
),
1293 if (!info
->groups
) {
1294 dev_err(dev
, "failed allocate memory for ping group list\n");
1300 for_each_child_of_node(np
, child
) {
1301 if (of_match_node(rockchip_bank_match
, child
))
1304 ret
= rockchip_pinctrl_parse_functions(child
, info
, i
++);
1306 dev_err(&pdev
->dev
, "failed to parse function\n");
1314 static int rockchip_pinctrl_register(struct platform_device
*pdev
,
1315 struct rockchip_pinctrl
*info
)
1317 struct pinctrl_desc
*ctrldesc
= &info
->pctl
;
1318 struct pinctrl_pin_desc
*pindesc
, *pdesc
;
1319 struct rockchip_pin_bank
*pin_bank
;
1323 ctrldesc
->name
= "rockchip-pinctrl";
1324 ctrldesc
->owner
= THIS_MODULE
;
1325 ctrldesc
->pctlops
= &rockchip_pctrl_ops
;
1326 ctrldesc
->pmxops
= &rockchip_pmx_ops
;
1327 ctrldesc
->confops
= &rockchip_pinconf_ops
;
1329 pindesc
= devm_kzalloc(&pdev
->dev
, sizeof(*pindesc
) *
1330 info
->ctrl
->nr_pins
, GFP_KERNEL
);
1332 dev_err(&pdev
->dev
, "mem alloc for pin descriptors failed\n");
1335 ctrldesc
->pins
= pindesc
;
1336 ctrldesc
->npins
= info
->ctrl
->nr_pins
;
1339 for (bank
= 0 , k
= 0; bank
< info
->ctrl
->nr_banks
; bank
++) {
1340 pin_bank
= &info
->ctrl
->pin_banks
[bank
];
1341 for (pin
= 0; pin
< pin_bank
->nr_pins
; pin
++, k
++) {
1343 pdesc
->name
= kasprintf(GFP_KERNEL
, "%s-%d",
1344 pin_bank
->name
, pin
);
1349 ret
= rockchip_pinctrl_parse_dt(pdev
, info
);
1353 info
->pctl_dev
= pinctrl_register(ctrldesc
, &pdev
->dev
, info
);
1354 if (IS_ERR(info
->pctl_dev
)) {
1355 dev_err(&pdev
->dev
, "could not register pinctrl driver\n");
1356 return PTR_ERR(info
->pctl_dev
);
1359 for (bank
= 0; bank
< info
->ctrl
->nr_banks
; ++bank
) {
1360 pin_bank
= &info
->ctrl
->pin_banks
[bank
];
1361 pin_bank
->grange
.name
= pin_bank
->name
;
1362 pin_bank
->grange
.id
= bank
;
1363 pin_bank
->grange
.pin_base
= pin_bank
->pin_base
;
1364 pin_bank
->grange
.base
= pin_bank
->gpio_chip
.base
;
1365 pin_bank
->grange
.npins
= pin_bank
->gpio_chip
.ngpio
;
1366 pin_bank
->grange
.gc
= &pin_bank
->gpio_chip
;
1367 pinctrl_add_gpio_range(info
->pctl_dev
, &pin_bank
->grange
);
1377 static void rockchip_gpio_set(struct gpio_chip
*gc
, unsigned offset
, int value
)
1379 struct rockchip_pin_bank
*bank
= gc_to_pin_bank(gc
);
1380 void __iomem
*reg
= bank
->reg_base
+ GPIO_SWPORT_DR
;
1381 unsigned long flags
;
1384 clk_enable(bank
->clk
);
1385 spin_lock_irqsave(&bank
->slock
, flags
);
1388 data
&= ~BIT(offset
);
1390 data
|= BIT(offset
);
1393 spin_unlock_irqrestore(&bank
->slock
, flags
);
1394 clk_disable(bank
->clk
);
1398 * Returns the level of the pin for input direction and setting of the DR
1399 * register for output gpios.
1401 static int rockchip_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
1403 struct rockchip_pin_bank
*bank
= gc_to_pin_bank(gc
);
1406 clk_enable(bank
->clk
);
1407 data
= readl(bank
->reg_base
+ GPIO_EXT_PORT
);
1408 clk_disable(bank
->clk
);
1415 * gpiolib gpio_direction_input callback function. The setting of the pin
1416 * mux function as 'gpio input' will be handled by the pinctrl susbsystem
1419 static int rockchip_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
1421 return pinctrl_gpio_direction_input(gc
->base
+ offset
);
1425 * gpiolib gpio_direction_output callback function. The setting of the pin
1426 * mux function as 'gpio output' will be handled by the pinctrl susbsystem
1429 static int rockchip_gpio_direction_output(struct gpio_chip
*gc
,
1430 unsigned offset
, int value
)
1432 rockchip_gpio_set(gc
, offset
, value
);
1433 return pinctrl_gpio_direction_output(gc
->base
+ offset
);
1437 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
1438 * and a virtual IRQ, if not already present.
1440 static int rockchip_gpio_to_irq(struct gpio_chip
*gc
, unsigned offset
)
1442 struct rockchip_pin_bank
*bank
= gc_to_pin_bank(gc
);
1448 virq
= irq_create_mapping(bank
->domain
, offset
);
1450 return (virq
) ? : -ENXIO
;
1453 static const struct gpio_chip rockchip_gpiolib_chip
= {
1454 .request
= gpiochip_generic_request
,
1455 .free
= gpiochip_generic_free
,
1456 .set
= rockchip_gpio_set
,
1457 .get
= rockchip_gpio_get
,
1458 .direction_input
= rockchip_gpio_direction_input
,
1459 .direction_output
= rockchip_gpio_direction_output
,
1460 .to_irq
= rockchip_gpio_to_irq
,
1461 .owner
= THIS_MODULE
,
1465 * Interrupt handling
1468 static void rockchip_irq_demux(struct irq_desc
*desc
)
1470 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
1471 struct rockchip_pin_bank
*bank
= irq_desc_get_handler_data(desc
);
1474 dev_dbg(bank
->drvdata
->dev
, "got irq for bank %s\n", bank
->name
);
1476 chained_irq_enter(chip
, desc
);
1478 pend
= readl_relaxed(bank
->reg_base
+ GPIO_INT_STATUS
);
1481 unsigned int irq
, virq
;
1485 virq
= irq_linear_revmap(bank
->domain
, irq
);
1488 dev_err(bank
->drvdata
->dev
, "unmapped irq %d\n", irq
);
1492 dev_dbg(bank
->drvdata
->dev
, "handling irq %d\n", irq
);
1495 * Triggering IRQ on both rising and falling edge
1496 * needs manual intervention.
1498 if (bank
->toggle_edge_mode
& BIT(irq
)) {
1499 u32 data
, data_old
, polarity
;
1500 unsigned long flags
;
1502 data
= readl_relaxed(bank
->reg_base
+ GPIO_EXT_PORT
);
1504 spin_lock_irqsave(&bank
->slock
, flags
);
1506 polarity
= readl_relaxed(bank
->reg_base
+
1508 if (data
& BIT(irq
))
1509 polarity
&= ~BIT(irq
);
1511 polarity
|= BIT(irq
);
1513 bank
->reg_base
+ GPIO_INT_POLARITY
);
1515 spin_unlock_irqrestore(&bank
->slock
, flags
);
1518 data
= readl_relaxed(bank
->reg_base
+
1520 } while ((data
& BIT(irq
)) != (data_old
& BIT(irq
)));
1523 generic_handle_irq(virq
);
1526 chained_irq_exit(chip
, desc
);
1529 static int rockchip_irq_set_type(struct irq_data
*d
, unsigned int type
)
1531 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
1532 struct rockchip_pin_bank
*bank
= gc
->private;
1533 u32 mask
= BIT(d
->hwirq
);
1537 unsigned long flags
;
1540 /* make sure the pin is configured as gpio input */
1541 ret
= rockchip_set_mux(bank
, d
->hwirq
, RK_FUNC_GPIO
);
1545 clk_enable(bank
->clk
);
1546 spin_lock_irqsave(&bank
->slock
, flags
);
1548 data
= readl_relaxed(bank
->reg_base
+ GPIO_SWPORT_DDR
);
1550 writel_relaxed(data
, bank
->reg_base
+ GPIO_SWPORT_DDR
);
1552 spin_unlock_irqrestore(&bank
->slock
, flags
);
1554 if (type
& IRQ_TYPE_EDGE_BOTH
)
1555 irq_set_handler_locked(d
, handle_edge_irq
);
1557 irq_set_handler_locked(d
, handle_level_irq
);
1559 spin_lock_irqsave(&bank
->slock
, flags
);
1562 level
= readl_relaxed(gc
->reg_base
+ GPIO_INTTYPE_LEVEL
);
1563 polarity
= readl_relaxed(gc
->reg_base
+ GPIO_INT_POLARITY
);
1566 case IRQ_TYPE_EDGE_BOTH
:
1567 bank
->toggle_edge_mode
|= mask
;
1571 * Determine gpio state. If 1 next interrupt should be falling
1574 data
= readl(bank
->reg_base
+ GPIO_EXT_PORT
);
1580 case IRQ_TYPE_EDGE_RISING
:
1581 bank
->toggle_edge_mode
&= ~mask
;
1585 case IRQ_TYPE_EDGE_FALLING
:
1586 bank
->toggle_edge_mode
&= ~mask
;
1590 case IRQ_TYPE_LEVEL_HIGH
:
1591 bank
->toggle_edge_mode
&= ~mask
;
1595 case IRQ_TYPE_LEVEL_LOW
:
1596 bank
->toggle_edge_mode
&= ~mask
;
1602 spin_unlock_irqrestore(&bank
->slock
, flags
);
1603 clk_disable(bank
->clk
);
1607 writel_relaxed(level
, gc
->reg_base
+ GPIO_INTTYPE_LEVEL
);
1608 writel_relaxed(polarity
, gc
->reg_base
+ GPIO_INT_POLARITY
);
1611 spin_unlock_irqrestore(&bank
->slock
, flags
);
1612 clk_disable(bank
->clk
);
1617 static void rockchip_irq_suspend(struct irq_data
*d
)
1619 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
1620 struct rockchip_pin_bank
*bank
= gc
->private;
1622 clk_enable(bank
->clk
);
1623 bank
->saved_masks
= irq_reg_readl(gc
, GPIO_INTMASK
);
1624 irq_reg_writel(gc
, ~gc
->wake_active
, GPIO_INTMASK
);
1625 clk_disable(bank
->clk
);
1628 static void rockchip_irq_resume(struct irq_data
*d
)
1630 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
1631 struct rockchip_pin_bank
*bank
= gc
->private;
1633 clk_enable(bank
->clk
);
1634 irq_reg_writel(gc
, bank
->saved_masks
, GPIO_INTMASK
);
1635 clk_disable(bank
->clk
);
1638 static void rockchip_irq_gc_mask_clr_bit(struct irq_data
*d
)
1640 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
1641 struct rockchip_pin_bank
*bank
= gc
->private;
1643 clk_enable(bank
->clk
);
1644 irq_gc_mask_clr_bit(d
);
1647 void rockchip_irq_gc_mask_set_bit(struct irq_data
*d
)
1649 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
1650 struct rockchip_pin_bank
*bank
= gc
->private;
1652 irq_gc_mask_set_bit(d
);
1653 clk_disable(bank
->clk
);
1656 static int rockchip_interrupts_register(struct platform_device
*pdev
,
1657 struct rockchip_pinctrl
*info
)
1659 struct rockchip_pin_ctrl
*ctrl
= info
->ctrl
;
1660 struct rockchip_pin_bank
*bank
= ctrl
->pin_banks
;
1661 unsigned int clr
= IRQ_NOREQUEST
| IRQ_NOPROBE
| IRQ_NOAUTOEN
;
1662 struct irq_chip_generic
*gc
;
1666 for (i
= 0; i
< ctrl
->nr_banks
; ++i
, ++bank
) {
1668 dev_warn(&pdev
->dev
, "bank %s is not valid\n",
1673 ret
= clk_enable(bank
->clk
);
1675 dev_err(&pdev
->dev
, "failed to enable clock for bank %s\n",
1680 bank
->domain
= irq_domain_add_linear(bank
->of_node
, 32,
1681 &irq_generic_chip_ops
, NULL
);
1682 if (!bank
->domain
) {
1683 dev_warn(&pdev
->dev
, "could not initialize irq domain for bank %s\n",
1685 clk_disable(bank
->clk
);
1689 ret
= irq_alloc_domain_generic_chips(bank
->domain
, 32, 1,
1690 "rockchip_gpio_irq", handle_level_irq
,
1691 clr
, 0, IRQ_GC_INIT_MASK_CACHE
);
1693 dev_err(&pdev
->dev
, "could not alloc generic chips for bank %s\n",
1695 irq_domain_remove(bank
->domain
);
1696 clk_disable(bank
->clk
);
1701 * Linux assumes that all interrupts start out disabled/masked.
1702 * Our driver only uses the concept of masked and always keeps
1703 * things enabled, so for us that's all masked and all enabled.
1705 writel_relaxed(0xffffffff, bank
->reg_base
+ GPIO_INTMASK
);
1706 writel_relaxed(0xffffffff, bank
->reg_base
+ GPIO_INTEN
);
1708 gc
= irq_get_domain_generic_chip(bank
->domain
, 0);
1709 gc
->reg_base
= bank
->reg_base
;
1711 gc
->chip_types
[0].regs
.mask
= GPIO_INTMASK
;
1712 gc
->chip_types
[0].regs
.ack
= GPIO_PORTS_EOI
;
1713 gc
->chip_types
[0].chip
.irq_ack
= irq_gc_ack_set_bit
;
1714 gc
->chip_types
[0].chip
.irq_mask
= rockchip_irq_gc_mask_set_bit
;
1715 gc
->chip_types
[0].chip
.irq_unmask
=
1716 rockchip_irq_gc_mask_clr_bit
;
1717 gc
->chip_types
[0].chip
.irq_set_wake
= irq_gc_set_wake
;
1718 gc
->chip_types
[0].chip
.irq_suspend
= rockchip_irq_suspend
;
1719 gc
->chip_types
[0].chip
.irq_resume
= rockchip_irq_resume
;
1720 gc
->chip_types
[0].chip
.irq_set_type
= rockchip_irq_set_type
;
1721 gc
->wake_enabled
= IRQ_MSK(bank
->nr_pins
);
1723 irq_set_chained_handler_and_data(bank
->irq
,
1724 rockchip_irq_demux
, bank
);
1726 /* map the gpio irqs here, when the clock is still running */
1727 for (j
= 0 ; j
< 32 ; j
++)
1728 irq_create_mapping(bank
->domain
, j
);
1730 clk_disable(bank
->clk
);
1736 static int rockchip_gpiolib_register(struct platform_device
*pdev
,
1737 struct rockchip_pinctrl
*info
)
1739 struct rockchip_pin_ctrl
*ctrl
= info
->ctrl
;
1740 struct rockchip_pin_bank
*bank
= ctrl
->pin_banks
;
1741 struct gpio_chip
*gc
;
1745 for (i
= 0; i
< ctrl
->nr_banks
; ++i
, ++bank
) {
1747 dev_warn(&pdev
->dev
, "bank %s is not valid\n",
1752 bank
->gpio_chip
= rockchip_gpiolib_chip
;
1754 gc
= &bank
->gpio_chip
;
1755 gc
->base
= bank
->pin_base
;
1756 gc
->ngpio
= bank
->nr_pins
;
1757 gc
->dev
= &pdev
->dev
;
1758 gc
->of_node
= bank
->of_node
;
1759 gc
->label
= bank
->name
;
1761 ret
= gpiochip_add(gc
);
1763 dev_err(&pdev
->dev
, "failed to register gpio_chip %s, error code: %d\n",
1769 rockchip_interrupts_register(pdev
, info
);
1774 for (--i
, --bank
; i
>= 0; --i
, --bank
) {
1777 gpiochip_remove(&bank
->gpio_chip
);
1782 static int rockchip_gpiolib_unregister(struct platform_device
*pdev
,
1783 struct rockchip_pinctrl
*info
)
1785 struct rockchip_pin_ctrl
*ctrl
= info
->ctrl
;
1786 struct rockchip_pin_bank
*bank
= ctrl
->pin_banks
;
1789 for (i
= 0; i
< ctrl
->nr_banks
; ++i
, ++bank
) {
1792 gpiochip_remove(&bank
->gpio_chip
);
1798 static int rockchip_get_bank_data(struct rockchip_pin_bank
*bank
,
1799 struct rockchip_pinctrl
*info
)
1801 struct resource res
;
1804 if (of_address_to_resource(bank
->of_node
, 0, &res
)) {
1805 dev_err(info
->dev
, "cannot find IO resource for bank\n");
1809 bank
->reg_base
= devm_ioremap_resource(info
->dev
, &res
);
1810 if (IS_ERR(bank
->reg_base
))
1811 return PTR_ERR(bank
->reg_base
);
1814 * special case, where parts of the pull setting-registers are
1815 * part of the PMU register space
1817 if (of_device_is_compatible(bank
->of_node
,
1818 "rockchip,rk3188-gpio-bank0")) {
1819 struct device_node
*node
;
1821 node
= of_parse_phandle(bank
->of_node
->parent
,
1824 if (of_address_to_resource(bank
->of_node
, 1, &res
)) {
1825 dev_err(info
->dev
, "cannot find IO resource for bank\n");
1829 base
= devm_ioremap_resource(info
->dev
, &res
);
1831 return PTR_ERR(base
);
1832 rockchip_regmap_config
.max_register
=
1833 resource_size(&res
) - 4;
1834 rockchip_regmap_config
.name
=
1835 "rockchip,rk3188-gpio-bank0-pull";
1836 bank
->regmap_pull
= devm_regmap_init_mmio(info
->dev
,
1838 &rockchip_regmap_config
);
1842 bank
->irq
= irq_of_parse_and_map(bank
->of_node
, 0);
1844 bank
->clk
= of_clk_get(bank
->of_node
, 0);
1845 if (IS_ERR(bank
->clk
))
1846 return PTR_ERR(bank
->clk
);
1848 return clk_prepare(bank
->clk
);
1851 static const struct of_device_id rockchip_pinctrl_dt_match
[];
1853 /* retrieve the soc specific data */
1854 static struct rockchip_pin_ctrl
*rockchip_pinctrl_get_soc_data(
1855 struct rockchip_pinctrl
*d
,
1856 struct platform_device
*pdev
)
1858 const struct of_device_id
*match
;
1859 struct device_node
*node
= pdev
->dev
.of_node
;
1860 struct device_node
*np
;
1861 struct rockchip_pin_ctrl
*ctrl
;
1862 struct rockchip_pin_bank
*bank
;
1863 int grf_offs
, pmu_offs
, i
, j
;
1865 match
= of_match_node(rockchip_pinctrl_dt_match
, node
);
1866 ctrl
= (struct rockchip_pin_ctrl
*)match
->data
;
1868 for_each_child_of_node(node
, np
) {
1869 if (!of_find_property(np
, "gpio-controller", NULL
))
1872 bank
= ctrl
->pin_banks
;
1873 for (i
= 0; i
< ctrl
->nr_banks
; ++i
, ++bank
) {
1874 if (!strcmp(bank
->name
, np
->name
)) {
1877 if (!rockchip_get_bank_data(bank
, d
))
1885 grf_offs
= ctrl
->grf_mux_offset
;
1886 pmu_offs
= ctrl
->pmu_mux_offset
;
1887 bank
= ctrl
->pin_banks
;
1888 for (i
= 0; i
< ctrl
->nr_banks
; ++i
, ++bank
) {
1891 spin_lock_init(&bank
->slock
);
1893 bank
->pin_base
= ctrl
->nr_pins
;
1894 ctrl
->nr_pins
+= bank
->nr_pins
;
1896 /* calculate iomux offsets */
1897 for (j
= 0; j
< 4; j
++) {
1898 struct rockchip_iomux
*iom
= &bank
->iomux
[j
];
1901 if (bank_pins
>= bank
->nr_pins
)
1904 /* preset offset value, set new start value */
1905 if (iom
->offset
>= 0) {
1906 if (iom
->type
& IOMUX_SOURCE_PMU
)
1907 pmu_offs
= iom
->offset
;
1909 grf_offs
= iom
->offset
;
1910 } else { /* set current offset */
1911 iom
->offset
= (iom
->type
& IOMUX_SOURCE_PMU
) ?
1912 pmu_offs
: grf_offs
;
1915 dev_dbg(d
->dev
, "bank %d, iomux %d has offset 0x%x\n",
1919 * Increase offset according to iomux width.
1920 * 4bit iomux'es are spread over two registers.
1922 inc
= (iom
->type
& IOMUX_WIDTH_4BIT
) ? 8 : 4;
1923 if (iom
->type
& IOMUX_SOURCE_PMU
)
1935 #define RK3288_GRF_GPIO6C_IOMUX 0x64
1936 #define GPIO6C6_SEL_WRITE_ENABLE BIT(28)
1938 static u32 rk3288_grf_gpio6c_iomux
;
1940 static int __maybe_unused
rockchip_pinctrl_suspend(struct device
*dev
)
1942 struct rockchip_pinctrl
*info
= dev_get_drvdata(dev
);
1943 int ret
= pinctrl_force_sleep(info
->pctl_dev
);
1949 * RK3288 GPIO6_C6 mux would be modified by Maskrom when resume, so save
1950 * the setting here, and restore it at resume.
1952 if (info
->ctrl
->type
== RK3288
) {
1953 ret
= regmap_read(info
->regmap_base
, RK3288_GRF_GPIO6C_IOMUX
,
1954 &rk3288_grf_gpio6c_iomux
);
1956 pinctrl_force_default(info
->pctl_dev
);
1964 static int __maybe_unused
rockchip_pinctrl_resume(struct device
*dev
)
1966 struct rockchip_pinctrl
*info
= dev_get_drvdata(dev
);
1967 int ret
= regmap_write(info
->regmap_base
, RK3288_GRF_GPIO6C_IOMUX
,
1968 rk3288_grf_gpio6c_iomux
|
1969 GPIO6C6_SEL_WRITE_ENABLE
);
1974 return pinctrl_force_default(info
->pctl_dev
);
1977 static SIMPLE_DEV_PM_OPS(rockchip_pinctrl_dev_pm_ops
, rockchip_pinctrl_suspend
,
1978 rockchip_pinctrl_resume
);
1980 static int rockchip_pinctrl_probe(struct platform_device
*pdev
)
1982 struct rockchip_pinctrl
*info
;
1983 struct device
*dev
= &pdev
->dev
;
1984 struct rockchip_pin_ctrl
*ctrl
;
1985 struct device_node
*np
= pdev
->dev
.of_node
, *node
;
1986 struct resource
*res
;
1990 if (!dev
->of_node
) {
1991 dev_err(dev
, "device tree node not found\n");
1995 info
= devm_kzalloc(dev
, sizeof(struct rockchip_pinctrl
), GFP_KERNEL
);
2001 ctrl
= rockchip_pinctrl_get_soc_data(info
, pdev
);
2003 dev_err(dev
, "driver data not available\n");
2008 node
= of_parse_phandle(np
, "rockchip,grf", 0);
2010 info
->regmap_base
= syscon_node_to_regmap(node
);
2011 if (IS_ERR(info
->regmap_base
))
2012 return PTR_ERR(info
->regmap_base
);
2014 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2015 base
= devm_ioremap_resource(&pdev
->dev
, res
);
2017 return PTR_ERR(base
);
2019 rockchip_regmap_config
.max_register
= resource_size(res
) - 4;
2020 rockchip_regmap_config
.name
= "rockchip,pinctrl";
2021 info
->regmap_base
= devm_regmap_init_mmio(&pdev
->dev
, base
,
2022 &rockchip_regmap_config
);
2024 /* to check for the old dt-bindings */
2025 info
->reg_size
= resource_size(res
);
2027 /* Honor the old binding, with pull registers as 2nd resource */
2028 if (ctrl
->type
== RK3188
&& info
->reg_size
< 0x200) {
2029 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
2030 base
= devm_ioremap_resource(&pdev
->dev
, res
);
2032 return PTR_ERR(base
);
2034 rockchip_regmap_config
.max_register
=
2035 resource_size(res
) - 4;
2036 rockchip_regmap_config
.name
= "rockchip,pinctrl-pull";
2037 info
->regmap_pull
= devm_regmap_init_mmio(&pdev
->dev
,
2039 &rockchip_regmap_config
);
2043 /* try to find the optional reference to the pmu syscon */
2044 node
= of_parse_phandle(np
, "rockchip,pmu", 0);
2046 info
->regmap_pmu
= syscon_node_to_regmap(node
);
2047 if (IS_ERR(info
->regmap_pmu
))
2048 return PTR_ERR(info
->regmap_pmu
);
2051 ret
= rockchip_gpiolib_register(pdev
, info
);
2055 ret
= rockchip_pinctrl_register(pdev
, info
);
2057 rockchip_gpiolib_unregister(pdev
, info
);
2061 platform_set_drvdata(pdev
, info
);
2066 static struct rockchip_pin_bank rk2928_pin_banks
[] = {
2067 PIN_BANK(0, 32, "gpio0"),
2068 PIN_BANK(1, 32, "gpio1"),
2069 PIN_BANK(2, 32, "gpio2"),
2070 PIN_BANK(3, 32, "gpio3"),
2073 static struct rockchip_pin_ctrl rk2928_pin_ctrl
= {
2074 .pin_banks
= rk2928_pin_banks
,
2075 .nr_banks
= ARRAY_SIZE(rk2928_pin_banks
),
2076 .label
= "RK2928-GPIO",
2078 .grf_mux_offset
= 0xa8,
2079 .pull_calc_reg
= rk2928_calc_pull_reg_and_bit
,
2082 static struct rockchip_pin_bank rk3036_pin_banks
[] = {
2083 PIN_BANK(0, 32, "gpio0"),
2084 PIN_BANK(1, 32, "gpio1"),
2085 PIN_BANK(2, 32, "gpio2"),
2088 static struct rockchip_pin_ctrl rk3036_pin_ctrl
= {
2089 .pin_banks
= rk3036_pin_banks
,
2090 .nr_banks
= ARRAY_SIZE(rk3036_pin_banks
),
2091 .label
= "RK3036-GPIO",
2093 .grf_mux_offset
= 0xa8,
2094 .pull_calc_reg
= rk2928_calc_pull_reg_and_bit
,
2097 static struct rockchip_pin_bank rk3066a_pin_banks
[] = {
2098 PIN_BANK(0, 32, "gpio0"),
2099 PIN_BANK(1, 32, "gpio1"),
2100 PIN_BANK(2, 32, "gpio2"),
2101 PIN_BANK(3, 32, "gpio3"),
2102 PIN_BANK(4, 32, "gpio4"),
2103 PIN_BANK(6, 16, "gpio6"),
2106 static struct rockchip_pin_ctrl rk3066a_pin_ctrl
= {
2107 .pin_banks
= rk3066a_pin_banks
,
2108 .nr_banks
= ARRAY_SIZE(rk3066a_pin_banks
),
2109 .label
= "RK3066a-GPIO",
2111 .grf_mux_offset
= 0xa8,
2112 .pull_calc_reg
= rk2928_calc_pull_reg_and_bit
,
2115 static struct rockchip_pin_bank rk3066b_pin_banks
[] = {
2116 PIN_BANK(0, 32, "gpio0"),
2117 PIN_BANK(1, 32, "gpio1"),
2118 PIN_BANK(2, 32, "gpio2"),
2119 PIN_BANK(3, 32, "gpio3"),
2122 static struct rockchip_pin_ctrl rk3066b_pin_ctrl
= {
2123 .pin_banks
= rk3066b_pin_banks
,
2124 .nr_banks
= ARRAY_SIZE(rk3066b_pin_banks
),
2125 .label
= "RK3066b-GPIO",
2127 .grf_mux_offset
= 0x60,
2130 static struct rockchip_pin_bank rk3188_pin_banks
[] = {
2131 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_GPIO_ONLY
, 0, 0, 0),
2132 PIN_BANK(1, 32, "gpio1"),
2133 PIN_BANK(2, 32, "gpio2"),
2134 PIN_BANK(3, 32, "gpio3"),
2137 static struct rockchip_pin_ctrl rk3188_pin_ctrl
= {
2138 .pin_banks
= rk3188_pin_banks
,
2139 .nr_banks
= ARRAY_SIZE(rk3188_pin_banks
),
2140 .label
= "RK3188-GPIO",
2142 .grf_mux_offset
= 0x60,
2143 .pull_calc_reg
= rk3188_calc_pull_reg_and_bit
,
2146 static struct rockchip_pin_bank rk3288_pin_banks
[] = {
2147 PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU
,
2152 PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED
,
2157 PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED
),
2158 PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT
),
2159 PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT
,
2164 PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED
,
2169 PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED
),
2170 PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0,
2175 PIN_BANK(8, 16, "gpio8"),
2178 static struct rockchip_pin_ctrl rk3288_pin_ctrl
= {
2179 .pin_banks
= rk3288_pin_banks
,
2180 .nr_banks
= ARRAY_SIZE(rk3288_pin_banks
),
2181 .label
= "RK3288-GPIO",
2183 .grf_mux_offset
= 0x0,
2184 .pmu_mux_offset
= 0x84,
2185 .pull_calc_reg
= rk3288_calc_pull_reg_and_bit
,
2186 .drv_calc_reg
= rk3288_calc_drv_reg_and_bit
,
2189 static struct rockchip_pin_bank rk3368_pin_banks
[] = {
2190 PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU
,
2195 PIN_BANK(1, 32, "gpio1"),
2196 PIN_BANK(2, 32, "gpio2"),
2197 PIN_BANK(3, 32, "gpio3"),
2200 static struct rockchip_pin_ctrl rk3368_pin_ctrl
= {
2201 .pin_banks
= rk3368_pin_banks
,
2202 .nr_banks
= ARRAY_SIZE(rk3368_pin_banks
),
2203 .label
= "RK3368-GPIO",
2205 .grf_mux_offset
= 0x0,
2206 .pmu_mux_offset
= 0x0,
2207 .pull_calc_reg
= rk3368_calc_pull_reg_and_bit
,
2208 .drv_calc_reg
= rk3368_calc_drv_reg_and_bit
,
2212 static const struct of_device_id rockchip_pinctrl_dt_match
[] = {
2213 { .compatible
= "rockchip,rk2928-pinctrl",
2214 .data
= (void *)&rk2928_pin_ctrl
},
2215 { .compatible
= "rockchip,rk3036-pinctrl",
2216 .data
= (void *)&rk3036_pin_ctrl
},
2217 { .compatible
= "rockchip,rk3066a-pinctrl",
2218 .data
= (void *)&rk3066a_pin_ctrl
},
2219 { .compatible
= "rockchip,rk3066b-pinctrl",
2220 .data
= (void *)&rk3066b_pin_ctrl
},
2221 { .compatible
= "rockchip,rk3188-pinctrl",
2222 .data
= (void *)&rk3188_pin_ctrl
},
2223 { .compatible
= "rockchip,rk3288-pinctrl",
2224 .data
= (void *)&rk3288_pin_ctrl
},
2225 { .compatible
= "rockchip,rk3368-pinctrl",
2226 .data
= (void *)&rk3368_pin_ctrl
},
2229 MODULE_DEVICE_TABLE(of
, rockchip_pinctrl_dt_match
);
2231 static struct platform_driver rockchip_pinctrl_driver
= {
2232 .probe
= rockchip_pinctrl_probe
,
2234 .name
= "rockchip-pinctrl",
2235 .pm
= &rockchip_pinctrl_dev_pm_ops
,
2236 .of_match_table
= rockchip_pinctrl_dt_match
,
2240 static int __init
rockchip_pinctrl_drv_register(void)
2242 return platform_driver_register(&rockchip_pinctrl_driver
);
2244 postcore_initcall(rockchip_pinctrl_drv_register
);
2246 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
2247 MODULE_DESCRIPTION("Rockchip pinctrl driver");
2248 MODULE_LICENSE("GPL v2");