2 * Exynos specific support for Samsung pinctrl/gpiolib driver with eint support.
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
6 * Copyright (c) 2012 Linaro Ltd
7 * http://www.linaro.org
9 * Author: Thomas Abraham <thomas.ab@samsung.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This file contains the Samsung Exynos specific information required by the
17 * the Samsung pinctrl/gpiolib driver. It also includes the implementation of
18 * external gpio and wakeup interrupt support.
21 #include <linux/device.h>
22 #include <linux/interrupt.h>
23 #include <linux/irqdomain.h>
24 #include <linux/irq.h>
25 #include <linux/irqchip/chained_irq.h>
27 #include <linux/of_irq.h>
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/regmap.h>
31 #include <linux/err.h>
32 #include <linux/soc/samsung/exynos-pmu.h>
34 #include "pinctrl-samsung.h"
35 #include "pinctrl-exynos.h"
37 struct exynos_irq_chip
{
45 static inline struct exynos_irq_chip
*to_exynos_irq_chip(struct irq_chip
*chip
)
47 return container_of(chip
, struct exynos_irq_chip
, chip
);
50 static void exynos_irq_mask(struct irq_data
*irqd
)
52 struct irq_chip
*chip
= irq_data_get_irq_chip(irqd
);
53 struct exynos_irq_chip
*our_chip
= to_exynos_irq_chip(chip
);
54 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(irqd
);
55 unsigned long reg_mask
= our_chip
->eint_mask
+ bank
->eint_offset
;
59 spin_lock_irqsave(&bank
->slock
, flags
);
61 mask
= readl(bank
->eint_base
+ reg_mask
);
62 mask
|= 1 << irqd
->hwirq
;
63 writel(mask
, bank
->eint_base
+ reg_mask
);
65 spin_unlock_irqrestore(&bank
->slock
, flags
);
68 static void exynos_irq_ack(struct irq_data
*irqd
)
70 struct irq_chip
*chip
= irq_data_get_irq_chip(irqd
);
71 struct exynos_irq_chip
*our_chip
= to_exynos_irq_chip(chip
);
72 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(irqd
);
73 unsigned long reg_pend
= our_chip
->eint_pend
+ bank
->eint_offset
;
75 writel(1 << irqd
->hwirq
, bank
->eint_base
+ reg_pend
);
78 static void exynos_irq_unmask(struct irq_data
*irqd
)
80 struct irq_chip
*chip
= irq_data_get_irq_chip(irqd
);
81 struct exynos_irq_chip
*our_chip
= to_exynos_irq_chip(chip
);
82 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(irqd
);
83 unsigned long reg_mask
= our_chip
->eint_mask
+ bank
->eint_offset
;
88 * Ack level interrupts right before unmask
90 * If we don't do this we'll get a double-interrupt. Level triggered
91 * interrupts must not fire an interrupt if the level is not
92 * _currently_ active, even if it was active while the interrupt was
95 if (irqd_get_trigger_type(irqd
) & IRQ_TYPE_LEVEL_MASK
)
98 spin_lock_irqsave(&bank
->slock
, flags
);
100 mask
= readl(bank
->eint_base
+ reg_mask
);
101 mask
&= ~(1 << irqd
->hwirq
);
102 writel(mask
, bank
->eint_base
+ reg_mask
);
104 spin_unlock_irqrestore(&bank
->slock
, flags
);
107 static int exynos_irq_set_type(struct irq_data
*irqd
, unsigned int type
)
109 struct irq_chip
*chip
= irq_data_get_irq_chip(irqd
);
110 struct exynos_irq_chip
*our_chip
= to_exynos_irq_chip(chip
);
111 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(irqd
);
112 unsigned int shift
= EXYNOS_EINT_CON_LEN
* irqd
->hwirq
;
113 unsigned int con
, trig_type
;
114 unsigned long reg_con
= our_chip
->eint_con
+ bank
->eint_offset
;
117 case IRQ_TYPE_EDGE_RISING
:
118 trig_type
= EXYNOS_EINT_EDGE_RISING
;
120 case IRQ_TYPE_EDGE_FALLING
:
121 trig_type
= EXYNOS_EINT_EDGE_FALLING
;
123 case IRQ_TYPE_EDGE_BOTH
:
124 trig_type
= EXYNOS_EINT_EDGE_BOTH
;
126 case IRQ_TYPE_LEVEL_HIGH
:
127 trig_type
= EXYNOS_EINT_LEVEL_HIGH
;
129 case IRQ_TYPE_LEVEL_LOW
:
130 trig_type
= EXYNOS_EINT_LEVEL_LOW
;
133 pr_err("unsupported external interrupt type\n");
137 if (type
& IRQ_TYPE_EDGE_BOTH
)
138 irq_set_handler_locked(irqd
, handle_edge_irq
);
140 irq_set_handler_locked(irqd
, handle_level_irq
);
142 con
= readl(bank
->eint_base
+ reg_con
);
143 con
&= ~(EXYNOS_EINT_CON_MASK
<< shift
);
144 con
|= trig_type
<< shift
;
145 writel(con
, bank
->eint_base
+ reg_con
);
150 static int exynos_irq_request_resources(struct irq_data
*irqd
)
152 struct irq_chip
*chip
= irq_data_get_irq_chip(irqd
);
153 struct exynos_irq_chip
*our_chip
= to_exynos_irq_chip(chip
);
154 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(irqd
);
155 const struct samsung_pin_bank_type
*bank_type
= bank
->type
;
156 unsigned int shift
= EXYNOS_EINT_CON_LEN
* irqd
->hwirq
;
157 unsigned long reg_con
= our_chip
->eint_con
+ bank
->eint_offset
;
163 ret
= gpiochip_lock_as_irq(&bank
->gpio_chip
, irqd
->hwirq
);
165 dev_err(bank
->gpio_chip
.parent
,
166 "unable to lock pin %s-%lu IRQ\n",
167 bank
->name
, irqd
->hwirq
);
171 reg_con
= bank
->pctl_offset
+ bank_type
->reg_offset
[PINCFG_TYPE_FUNC
];
172 shift
= irqd
->hwirq
* bank_type
->fld_width
[PINCFG_TYPE_FUNC
];
173 mask
= (1 << bank_type
->fld_width
[PINCFG_TYPE_FUNC
]) - 1;
175 spin_lock_irqsave(&bank
->slock
, flags
);
177 con
= readl(bank
->eint_base
+ reg_con
);
178 con
&= ~(mask
<< shift
);
179 con
|= EXYNOS_EINT_FUNC
<< shift
;
180 writel(con
, bank
->eint_base
+ reg_con
);
182 spin_unlock_irqrestore(&bank
->slock
, flags
);
187 static void exynos_irq_release_resources(struct irq_data
*irqd
)
189 struct irq_chip
*chip
= irq_data_get_irq_chip(irqd
);
190 struct exynos_irq_chip
*our_chip
= to_exynos_irq_chip(chip
);
191 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(irqd
);
192 const struct samsung_pin_bank_type
*bank_type
= bank
->type
;
193 unsigned int shift
= EXYNOS_EINT_CON_LEN
* irqd
->hwirq
;
194 unsigned long reg_con
= our_chip
->eint_con
+ bank
->eint_offset
;
199 reg_con
= bank
->pctl_offset
+ bank_type
->reg_offset
[PINCFG_TYPE_FUNC
];
200 shift
= irqd
->hwirq
* bank_type
->fld_width
[PINCFG_TYPE_FUNC
];
201 mask
= (1 << bank_type
->fld_width
[PINCFG_TYPE_FUNC
]) - 1;
203 spin_lock_irqsave(&bank
->slock
, flags
);
205 con
= readl(bank
->eint_base
+ reg_con
);
206 con
&= ~(mask
<< shift
);
207 con
|= FUNC_INPUT
<< shift
;
208 writel(con
, bank
->eint_base
+ reg_con
);
210 spin_unlock_irqrestore(&bank
->slock
, flags
);
212 gpiochip_unlock_as_irq(&bank
->gpio_chip
, irqd
->hwirq
);
216 * irq_chip for gpio interrupts.
218 static struct exynos_irq_chip exynos_gpio_irq_chip
= {
220 .name
= "exynos_gpio_irq_chip",
221 .irq_unmask
= exynos_irq_unmask
,
222 .irq_mask
= exynos_irq_mask
,
223 .irq_ack
= exynos_irq_ack
,
224 .irq_set_type
= exynos_irq_set_type
,
225 .irq_request_resources
= exynos_irq_request_resources
,
226 .irq_release_resources
= exynos_irq_release_resources
,
228 .eint_con
= EXYNOS_GPIO_ECON_OFFSET
,
229 .eint_mask
= EXYNOS_GPIO_EMASK_OFFSET
,
230 .eint_pend
= EXYNOS_GPIO_EPEND_OFFSET
,
233 static int exynos_eint_irq_map(struct irq_domain
*h
, unsigned int virq
,
236 struct samsung_pin_bank
*b
= h
->host_data
;
238 irq_set_chip_data(virq
, b
);
239 irq_set_chip_and_handler(virq
, &b
->irq_chip
->chip
,
245 * irq domain callbacks for external gpio and wakeup interrupt controllers.
247 static const struct irq_domain_ops exynos_eint_irqd_ops
= {
248 .map
= exynos_eint_irq_map
,
249 .xlate
= irq_domain_xlate_twocell
,
252 static irqreturn_t
exynos_eint_gpio_irq(int irq
, void *data
)
254 struct samsung_pinctrl_drv_data
*d
= data
;
255 struct samsung_pin_bank
*bank
= d
->pin_banks
;
256 unsigned int svc
, group
, pin
, virq
;
258 svc
= readl(bank
->eint_base
+ EXYNOS_SVC_OFFSET
);
259 group
= EXYNOS_SVC_GROUP(svc
);
260 pin
= svc
& EXYNOS_SVC_NUM_MASK
;
266 virq
= irq_linear_revmap(bank
->irq_domain
, pin
);
269 generic_handle_irq(virq
);
273 struct exynos_eint_gpio_save
{
280 * exynos_eint_gpio_init() - setup handling of external gpio interrupts.
281 * @d: driver data of samsung pinctrl driver.
283 int exynos_eint_gpio_init(struct samsung_pinctrl_drv_data
*d
)
285 struct samsung_pin_bank
*bank
;
286 struct device
*dev
= d
->dev
;
291 dev_err(dev
, "irq number not available\n");
295 ret
= devm_request_irq(dev
, d
->irq
, exynos_eint_gpio_irq
,
296 0, dev_name(dev
), d
);
298 dev_err(dev
, "irq request failed\n");
303 for (i
= 0; i
< d
->nr_banks
; ++i
, ++bank
) {
304 if (bank
->eint_type
!= EINT_TYPE_GPIO
)
306 bank
->irq_domain
= irq_domain_add_linear(bank
->of_node
,
307 bank
->nr_pins
, &exynos_eint_irqd_ops
, bank
);
308 if (!bank
->irq_domain
) {
309 dev_err(dev
, "gpio irq domain add failed\n");
314 bank
->soc_priv
= devm_kzalloc(d
->dev
,
315 sizeof(struct exynos_eint_gpio_save
), GFP_KERNEL
);
316 if (!bank
->soc_priv
) {
317 irq_domain_remove(bank
->irq_domain
);
322 bank
->irq_chip
= &exynos_gpio_irq_chip
;
328 for (--i
, --bank
; i
>= 0; --i
, --bank
) {
329 if (bank
->eint_type
!= EINT_TYPE_GPIO
)
331 irq_domain_remove(bank
->irq_domain
);
337 static u32 exynos_eint_wake_mask
= 0xffffffff;
339 u32
exynos_get_eint_wake_mask(void)
341 return exynos_eint_wake_mask
;
344 static int exynos_wkup_irq_set_wake(struct irq_data
*irqd
, unsigned int on
)
346 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(irqd
);
347 unsigned long bit
= 1UL << (2 * bank
->eint_offset
+ irqd
->hwirq
);
349 pr_info("wake %s for irq %d\n", on
? "enabled" : "disabled", irqd
->irq
);
352 exynos_eint_wake_mask
|= bit
;
354 exynos_eint_wake_mask
&= ~bit
;
360 * irq_chip for wakeup interrupts
362 static const struct exynos_irq_chip exynos4210_wkup_irq_chip __initconst
= {
364 .name
= "exynos4210_wkup_irq_chip",
365 .irq_unmask
= exynos_irq_unmask
,
366 .irq_mask
= exynos_irq_mask
,
367 .irq_ack
= exynos_irq_ack
,
368 .irq_set_type
= exynos_irq_set_type
,
369 .irq_set_wake
= exynos_wkup_irq_set_wake
,
370 .irq_request_resources
= exynos_irq_request_resources
,
371 .irq_release_resources
= exynos_irq_release_resources
,
373 .eint_con
= EXYNOS_WKUP_ECON_OFFSET
,
374 .eint_mask
= EXYNOS_WKUP_EMASK_OFFSET
,
375 .eint_pend
= EXYNOS_WKUP_EPEND_OFFSET
,
378 static const struct exynos_irq_chip exynos7_wkup_irq_chip __initconst
= {
380 .name
= "exynos7_wkup_irq_chip",
381 .irq_unmask
= exynos_irq_unmask
,
382 .irq_mask
= exynos_irq_mask
,
383 .irq_ack
= exynos_irq_ack
,
384 .irq_set_type
= exynos_irq_set_type
,
385 .irq_set_wake
= exynos_wkup_irq_set_wake
,
386 .irq_request_resources
= exynos_irq_request_resources
,
387 .irq_release_resources
= exynos_irq_release_resources
,
389 .eint_con
= EXYNOS7_WKUP_ECON_OFFSET
,
390 .eint_mask
= EXYNOS7_WKUP_EMASK_OFFSET
,
391 .eint_pend
= EXYNOS7_WKUP_EPEND_OFFSET
,
394 /* list of external wakeup controllers supported */
395 static const struct of_device_id exynos_wkup_irq_ids
[] = {
396 { .compatible
= "samsung,exynos4210-wakeup-eint",
397 .data
= &exynos4210_wkup_irq_chip
},
398 { .compatible
= "samsung,exynos7-wakeup-eint",
399 .data
= &exynos7_wkup_irq_chip
},
403 /* interrupt handler for wakeup interrupts 0..15 */
404 static void exynos_irq_eint0_15(struct irq_desc
*desc
)
406 struct exynos_weint_data
*eintd
= irq_desc_get_handler_data(desc
);
407 struct samsung_pin_bank
*bank
= eintd
->bank
;
408 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
411 chained_irq_enter(chip
, desc
);
413 eint_irq
= irq_linear_revmap(bank
->irq_domain
, eintd
->irq
);
414 generic_handle_irq(eint_irq
);
416 chained_irq_exit(chip
, desc
);
419 static inline void exynos_irq_demux_eint(unsigned long pend
,
420 struct irq_domain
*domain
)
426 generic_handle_irq(irq_find_mapping(domain
, irq
));
431 /* interrupt handler for wakeup interrupt 16 */
432 static void exynos_irq_demux_eint16_31(struct irq_desc
*desc
)
434 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
435 struct exynos_muxed_weint_data
*eintd
= irq_desc_get_handler_data(desc
);
440 chained_irq_enter(chip
, desc
);
442 for (i
= 0; i
< eintd
->nr_banks
; ++i
) {
443 struct samsung_pin_bank
*b
= eintd
->banks
[i
];
444 pend
= readl(b
->eint_base
+ b
->irq_chip
->eint_pend
446 mask
= readl(b
->eint_base
+ b
->irq_chip
->eint_mask
448 exynos_irq_demux_eint(pend
& ~mask
, b
->irq_domain
);
451 chained_irq_exit(chip
, desc
);
455 * exynos_eint_wkup_init() - setup handling of external wakeup interrupts.
456 * @d: driver data of samsung pinctrl driver.
458 int exynos_eint_wkup_init(struct samsung_pinctrl_drv_data
*d
)
460 struct device
*dev
= d
->dev
;
461 struct device_node
*wkup_np
= NULL
;
462 struct device_node
*np
;
463 struct samsung_pin_bank
*bank
;
464 struct exynos_weint_data
*weint_data
;
465 struct exynos_muxed_weint_data
*muxed_data
;
466 struct exynos_irq_chip
*irq_chip
;
467 unsigned int muxed_banks
= 0;
471 for_each_child_of_node(dev
->of_node
, np
) {
472 const struct of_device_id
*match
;
474 match
= of_match_node(exynos_wkup_irq_ids
, np
);
476 irq_chip
= kmemdup(match
->data
,
477 sizeof(*irq_chip
), GFP_KERNEL
);
488 for (i
= 0; i
< d
->nr_banks
; ++i
, ++bank
) {
489 if (bank
->eint_type
!= EINT_TYPE_WKUP
)
492 bank
->irq_domain
= irq_domain_add_linear(bank
->of_node
,
493 bank
->nr_pins
, &exynos_eint_irqd_ops
, bank
);
494 if (!bank
->irq_domain
) {
495 dev_err(dev
, "wkup irq domain add failed\n");
499 bank
->irq_chip
= irq_chip
;
501 if (!of_find_property(bank
->of_node
, "interrupts", NULL
)) {
502 bank
->eint_type
= EINT_TYPE_WKUP_MUX
;
507 weint_data
= devm_kzalloc(dev
, bank
->nr_pins
508 * sizeof(*weint_data
), GFP_KERNEL
);
512 for (idx
= 0; idx
< bank
->nr_pins
; ++idx
) {
513 irq
= irq_of_parse_and_map(bank
->of_node
, idx
);
515 dev_err(dev
, "irq number for eint-%s-%d not found\n",
519 weint_data
[idx
].irq
= idx
;
520 weint_data
[idx
].bank
= bank
;
521 irq_set_chained_handler_and_data(irq
,
530 irq
= irq_of_parse_and_map(wkup_np
, 0);
532 dev_err(dev
, "irq number for muxed EINTs not found\n");
536 muxed_data
= devm_kzalloc(dev
, sizeof(*muxed_data
)
537 + muxed_banks
*sizeof(struct samsung_pin_bank
*), GFP_KERNEL
);
541 irq_set_chained_handler_and_data(irq
, exynos_irq_demux_eint16_31
,
546 for (i
= 0; i
< d
->nr_banks
; ++i
, ++bank
) {
547 if (bank
->eint_type
!= EINT_TYPE_WKUP_MUX
)
550 muxed_data
->banks
[idx
++] = bank
;
552 muxed_data
->nr_banks
= muxed_banks
;
557 static void exynos_pinctrl_suspend_bank(
558 struct samsung_pinctrl_drv_data
*drvdata
,
559 struct samsung_pin_bank
*bank
)
561 struct exynos_eint_gpio_save
*save
= bank
->soc_priv
;
562 void __iomem
*regs
= bank
->eint_base
;
564 save
->eint_con
= readl(regs
+ EXYNOS_GPIO_ECON_OFFSET
565 + bank
->eint_offset
);
566 save
->eint_fltcon0
= readl(regs
+ EXYNOS_GPIO_EFLTCON_OFFSET
567 + 2 * bank
->eint_offset
);
568 save
->eint_fltcon1
= readl(regs
+ EXYNOS_GPIO_EFLTCON_OFFSET
569 + 2 * bank
->eint_offset
+ 4);
571 pr_debug("%s: save con %#010x\n", bank
->name
, save
->eint_con
);
572 pr_debug("%s: save fltcon0 %#010x\n", bank
->name
, save
->eint_fltcon0
);
573 pr_debug("%s: save fltcon1 %#010x\n", bank
->name
, save
->eint_fltcon1
);
576 void exynos_pinctrl_suspend(struct samsung_pinctrl_drv_data
*drvdata
)
578 struct samsung_pin_bank
*bank
= drvdata
->pin_banks
;
581 for (i
= 0; i
< drvdata
->nr_banks
; ++i
, ++bank
)
582 if (bank
->eint_type
== EINT_TYPE_GPIO
)
583 exynos_pinctrl_suspend_bank(drvdata
, bank
);
586 static void exynos_pinctrl_resume_bank(
587 struct samsung_pinctrl_drv_data
*drvdata
,
588 struct samsung_pin_bank
*bank
)
590 struct exynos_eint_gpio_save
*save
= bank
->soc_priv
;
591 void __iomem
*regs
= bank
->eint_base
;
593 pr_debug("%s: con %#010x => %#010x\n", bank
->name
,
594 readl(regs
+ EXYNOS_GPIO_ECON_OFFSET
595 + bank
->eint_offset
), save
->eint_con
);
596 pr_debug("%s: fltcon0 %#010x => %#010x\n", bank
->name
,
597 readl(regs
+ EXYNOS_GPIO_EFLTCON_OFFSET
598 + 2 * bank
->eint_offset
), save
->eint_fltcon0
);
599 pr_debug("%s: fltcon1 %#010x => %#010x\n", bank
->name
,
600 readl(regs
+ EXYNOS_GPIO_EFLTCON_OFFSET
601 + 2 * bank
->eint_offset
+ 4), save
->eint_fltcon1
);
603 writel(save
->eint_con
, regs
+ EXYNOS_GPIO_ECON_OFFSET
604 + bank
->eint_offset
);
605 writel(save
->eint_fltcon0
, regs
+ EXYNOS_GPIO_EFLTCON_OFFSET
606 + 2 * bank
->eint_offset
);
607 writel(save
->eint_fltcon1
, regs
+ EXYNOS_GPIO_EFLTCON_OFFSET
608 + 2 * bank
->eint_offset
+ 4);
611 void exynos_pinctrl_resume(struct samsung_pinctrl_drv_data
*drvdata
)
613 struct samsung_pin_bank
*bank
= drvdata
->pin_banks
;
616 for (i
= 0; i
< drvdata
->nr_banks
; ++i
, ++bank
)
617 if (bank
->eint_type
== EINT_TYPE_GPIO
)
618 exynos_pinctrl_resume_bank(drvdata
, bank
);
621 static void exynos_retention_enable(struct samsung_pinctrl_drv_data
*drvdata
)
623 if (drvdata
->retention_ctrl
->refcnt
)
624 atomic_inc(drvdata
->retention_ctrl
->refcnt
);
627 static void exynos_retention_disable(struct samsung_pinctrl_drv_data
*drvdata
)
629 struct samsung_retention_ctrl
*ctrl
= drvdata
->retention_ctrl
;
630 struct regmap
*pmu_regs
= ctrl
->priv
;
633 if (ctrl
->refcnt
&& !atomic_dec_and_test(ctrl
->refcnt
))
636 for (i
= 0; i
< ctrl
->nr_regs
; i
++)
637 regmap_write(pmu_regs
, ctrl
->regs
[i
], ctrl
->value
);
640 struct samsung_retention_ctrl
*
641 exynos_retention_init(struct samsung_pinctrl_drv_data
*drvdata
,
642 const struct samsung_retention_data
*data
)
644 struct samsung_retention_ctrl
*ctrl
;
645 struct regmap
*pmu_regs
;
648 ctrl
= devm_kzalloc(drvdata
->dev
, sizeof(*ctrl
), GFP_KERNEL
);
650 return ERR_PTR(-ENOMEM
);
652 pmu_regs
= exynos_get_pmu_regmap();
653 if (IS_ERR(pmu_regs
))
654 return ERR_CAST(pmu_regs
);
656 ctrl
->priv
= pmu_regs
;
657 ctrl
->regs
= data
->regs
;
658 ctrl
->nr_regs
= data
->nr_regs
;
659 ctrl
->value
= data
->value
;
660 ctrl
->refcnt
= data
->refcnt
;
661 ctrl
->enable
= exynos_retention_enable
;
662 ctrl
->disable
= exynos_retention_disable
;
664 /* Ensure that retention is disabled on driver init */
665 for (i
= 0; i
< ctrl
->nr_regs
; i
++)
666 regmap_write(pmu_regs
, ctrl
->regs
[i
], ctrl
->value
);