blob: ebc27b06718c0b64e24d2d2008f06ab0947c6a33 [file] [log] [blame]
Krzysztof Kozlowski221173a2017-12-26 19:09:42 +01001// SPDX-License-Identifier: GPL-2.0+
2//
3// Exynos specific support for Samsung pinctrl/gpiolib driver with eint support.
4//
5// Copyright (c) 2012 Samsung Electronics Co., Ltd.
6// http://www.samsung.com
7// Copyright (c) 2012 Linaro Ltd
8// http://www.linaro.org
9//
10// Author: Thomas Abraham <thomas.ab@samsung.com>
11//
12// This file contains the Samsung Exynos specific information required by the
13// the Samsung pinctrl/gpiolib driver. It also includes the implementation of
14// external gpio and wakeup interrupt support.
Thomas Abraham43b169d2012-09-07 06:07:19 +090015
Thomas Abraham43b169d2012-09-07 06:07:19 +090016#include <linux/device.h>
17#include <linux/interrupt.h>
18#include <linux/irqdomain.h>
19#include <linux/irq.h>
Catalin Marinasde88cbb2013-01-18 15:31:37 +000020#include <linux/irqchip/chained_irq.h>
Krzysztof Kozlowskicfa76dd2017-05-16 20:56:27 +020021#include <linux/of.h>
Thomas Abraham43b169d2012-09-07 06:07:19 +090022#include <linux/of_irq.h>
Thomas Abraham43b169d2012-09-07 06:07:19 +090023#include <linux/slab.h>
Tomasz Figa19846952013-03-18 22:31:50 +010024#include <linux/spinlock.h>
Marek Szyprowski07731012017-01-26 10:29:25 +010025#include <linux/regmap.h>
Thomas Abraham43b169d2012-09-07 06:07:19 +090026#include <linux/err.h>
Marek Szyprowski07731012017-01-26 10:29:25 +010027#include <linux/soc/samsung/exynos-pmu.h>
Krzysztof Kozlowskia8be2af2018-07-23 19:52:58 +020028#include <linux/soc/samsung/exynos-regs-pmu.h>
Thomas Abraham43b169d2012-09-07 06:07:19 +090029
Krzysztof Kozlowski4460dc22017-06-15 17:06:28 +020030#include <dt-bindings/pinctrl/samsung.h>
31
Thomas Abraham43b169d2012-09-07 06:07:19 +090032#include "pinctrl-samsung.h"
33#include "pinctrl-exynos.h"
34
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020035struct exynos_irq_chip {
36 struct irq_chip chip;
37
38 u32 eint_con;
39 u32 eint_mask;
40 u32 eint_pend;
Krzysztof Kozlowskia8be2af2018-07-23 19:52:58 +020041 u32 eint_wake_mask_value;
42 u32 eint_wake_mask_reg;
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020043};
44
45static inline struct exynos_irq_chip *to_exynos_irq_chip(struct irq_chip *chip)
46{
47 return container_of(chip, struct exynos_irq_chip, chip);
48}
Tomasz Figa499147c2013-03-18 22:31:52 +010049
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020050static void exynos_irq_mask(struct irq_data *irqd)
Thomas Abraham43b169d2012-09-07 06:07:19 +090051{
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020052 struct irq_chip *chip = irq_data_get_irq_chip(irqd);
53 struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
Tomasz Figa595be722012-10-11 10:11:16 +020054 struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020055 unsigned long reg_mask = our_chip->eint_mask + bank->eint_offset;
Thomas Abraham43b169d2012-09-07 06:07:19 +090056 unsigned long mask;
Doug Anderson5ae8cf72013-06-12 10:33:17 -070057 unsigned long flags;
58
59 spin_lock_irqsave(&bank->slock, flags);
Thomas Abraham43b169d2012-09-07 06:07:19 +090060
Chanwoo Choi8b1bd112016-11-09 17:40:10 +090061 mask = readl(bank->eint_base + reg_mask);
Tomasz Figa595be722012-10-11 10:11:16 +020062 mask |= 1 << irqd->hwirq;
Chanwoo Choi8b1bd112016-11-09 17:40:10 +090063 writel(mask, bank->eint_base + reg_mask);
Doug Anderson5ae8cf72013-06-12 10:33:17 -070064
65 spin_unlock_irqrestore(&bank->slock, flags);
Thomas Abraham43b169d2012-09-07 06:07:19 +090066}
67
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020068static void exynos_irq_ack(struct irq_data *irqd)
Thomas Abraham43b169d2012-09-07 06:07:19 +090069{
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020070 struct irq_chip *chip = irq_data_get_irq_chip(irqd);
71 struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
Tomasz Figa595be722012-10-11 10:11:16 +020072 struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020073 unsigned long reg_pend = our_chip->eint_pend + bank->eint_offset;
Thomas Abraham43b169d2012-09-07 06:07:19 +090074
Chanwoo Choi8b1bd112016-11-09 17:40:10 +090075 writel(1 << irqd->hwirq, bank->eint_base + reg_pend);
Thomas Abraham43b169d2012-09-07 06:07:19 +090076}
77
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020078static void exynos_irq_unmask(struct irq_data *irqd)
Doug Anderson5ace03f2013-06-12 10:33:18 -070079{
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020080 struct irq_chip *chip = irq_data_get_irq_chip(irqd);
81 struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
Doug Anderson5ace03f2013-06-12 10:33:18 -070082 struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020083 unsigned long reg_mask = our_chip->eint_mask + bank->eint_offset;
Doug Anderson5ace03f2013-06-12 10:33:18 -070084 unsigned long mask;
85 unsigned long flags;
86
Doug Anderson5a68e7a2013-06-17 09:50:43 -070087 /*
88 * Ack level interrupts right before unmask
89 *
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
93 * masked.
94 */
95 if (irqd_get_trigger_type(irqd) & IRQ_TYPE_LEVEL_MASK)
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020096 exynos_irq_ack(irqd);
Doug Anderson5a68e7a2013-06-17 09:50:43 -070097
Doug Anderson5ace03f2013-06-12 10:33:18 -070098 spin_lock_irqsave(&bank->slock, flags);
99
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900100 mask = readl(bank->eint_base + reg_mask);
Doug Anderson5ace03f2013-06-12 10:33:18 -0700101 mask &= ~(1 << irqd->hwirq);
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900102 writel(mask, bank->eint_base + reg_mask);
Doug Anderson5ace03f2013-06-12 10:33:18 -0700103
104 spin_unlock_irqrestore(&bank->slock, flags);
105}
106
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200107static int exynos_irq_set_type(struct irq_data *irqd, unsigned int type)
Thomas Abraham43b169d2012-09-07 06:07:19 +0900108{
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200109 struct irq_chip *chip = irq_data_get_irq_chip(irqd);
110 struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
Tomasz Figa595be722012-10-11 10:11:16 +0200111 struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
Tomasz Figaf6a82492014-08-09 01:48:05 +0200112 unsigned int shift = EXYNOS_EINT_CON_LEN * irqd->hwirq;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900113 unsigned int con, trig_type;
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200114 unsigned long reg_con = our_chip->eint_con + bank->eint_offset;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900115
116 switch (type) {
117 case IRQ_TYPE_EDGE_RISING:
118 trig_type = EXYNOS_EINT_EDGE_RISING;
119 break;
120 case IRQ_TYPE_EDGE_FALLING:
121 trig_type = EXYNOS_EINT_EDGE_FALLING;
122 break;
123 case IRQ_TYPE_EDGE_BOTH:
124 trig_type = EXYNOS_EINT_EDGE_BOTH;
125 break;
126 case IRQ_TYPE_LEVEL_HIGH:
127 trig_type = EXYNOS_EINT_LEVEL_HIGH;
128 break;
129 case IRQ_TYPE_LEVEL_LOW:
130 trig_type = EXYNOS_EINT_LEVEL_LOW;
131 break;
132 default:
133 pr_err("unsupported external interrupt type\n");
134 return -EINVAL;
135 }
136
137 if (type & IRQ_TYPE_EDGE_BOTH)
Thomas Gleixner40ec1682015-06-23 15:52:57 +0200138 irq_set_handler_locked(irqd, handle_edge_irq);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900139 else
Thomas Gleixner40ec1682015-06-23 15:52:57 +0200140 irq_set_handler_locked(irqd, handle_level_irq);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900141
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900142 con = readl(bank->eint_base + reg_con);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900143 con &= ~(EXYNOS_EINT_CON_MASK << shift);
144 con |= trig_type << shift;
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900145 writel(con, bank->eint_base + reg_con);
Tomasz Figaee2f5732012-09-21 07:33:48 +0900146
Tomasz Figaf6a82492014-08-09 01:48:05 +0200147 return 0;
148}
149
150static int exynos_irq_request_resources(struct irq_data *irqd)
151{
Tomasz Figaf6a82492014-08-09 01:48:05 +0200152 struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
Tomasz Figa94ce9442014-09-23 21:05:39 +0200153 const struct samsung_pin_bank_type *bank_type = bank->type;
Krzysztof Kozlowskibbed85f2017-07-18 19:43:28 +0200154 unsigned long reg_con, flags;
155 unsigned int shift, mask, con;
Tomasz Figaf6a82492014-08-09 01:48:05 +0200156 int ret;
157
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900158 ret = gpiochip_lock_as_irq(&bank->gpio_chip, irqd->hwirq);
Tomasz Figaf6a82492014-08-09 01:48:05 +0200159 if (ret) {
Linus Walleij58383c782015-11-04 09:56:26 +0100160 dev_err(bank->gpio_chip.parent,
161 "unable to lock pin %s-%lu IRQ\n",
Tomasz Figaf6a82492014-08-09 01:48:05 +0200162 bank->name, irqd->hwirq);
163 return ret;
164 }
165
Tomasz Figa43fc9e72013-03-18 22:31:53 +0100166 reg_con = bank->pctl_offset + bank_type->reg_offset[PINCFG_TYPE_FUNC];
Tomasz Figaf6a82492014-08-09 01:48:05 +0200167 shift = irqd->hwirq * bank_type->fld_width[PINCFG_TYPE_FUNC];
Tomasz Figa499147c2013-03-18 22:31:52 +0100168 mask = (1 << bank_type->fld_width[PINCFG_TYPE_FUNC]) - 1;
Tomasz Figaee2f5732012-09-21 07:33:48 +0900169
Tomasz Figa19846952013-03-18 22:31:50 +0100170 spin_lock_irqsave(&bank->slock, flags);
171
Krzysztof Kozlowskiaf0b0ba2017-06-14 15:18:28 +0200172 con = readl(bank->pctl_base + reg_con);
Tomasz Figaee2f5732012-09-21 07:33:48 +0900173 con &= ~(mask << shift);
Krzysztof Kozlowski4460dc22017-06-15 17:06:28 +0200174 con |= EXYNOS_PIN_FUNC_EINT << shift;
Krzysztof Kozlowskiaf0b0ba2017-06-14 15:18:28 +0200175 writel(con, bank->pctl_base + reg_con);
Tomasz Figaee2f5732012-09-21 07:33:48 +0900176
Tomasz Figa19846952013-03-18 22:31:50 +0100177 spin_unlock_irqrestore(&bank->slock, flags);
178
Thomas Abraham43b169d2012-09-07 06:07:19 +0900179 return 0;
180}
181
Tomasz Figaf6a82492014-08-09 01:48:05 +0200182static void exynos_irq_release_resources(struct irq_data *irqd)
183{
Tomasz Figaf6a82492014-08-09 01:48:05 +0200184 struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
Tomasz Figa94ce9442014-09-23 21:05:39 +0200185 const struct samsung_pin_bank_type *bank_type = bank->type;
Krzysztof Kozlowskibbed85f2017-07-18 19:43:28 +0200186 unsigned long reg_con, flags;
187 unsigned int shift, mask, con;
Tomasz Figaf6a82492014-08-09 01:48:05 +0200188
189 reg_con = bank->pctl_offset + bank_type->reg_offset[PINCFG_TYPE_FUNC];
190 shift = irqd->hwirq * bank_type->fld_width[PINCFG_TYPE_FUNC];
191 mask = (1 << bank_type->fld_width[PINCFG_TYPE_FUNC]) - 1;
192
Tomasz Figaf6a82492014-08-09 01:48:05 +0200193 spin_lock_irqsave(&bank->slock, flags);
194
Krzysztof Kozlowskiaf0b0ba2017-06-14 15:18:28 +0200195 con = readl(bank->pctl_base + reg_con);
Tomasz Figaf6a82492014-08-09 01:48:05 +0200196 con &= ~(mask << shift);
Krzysztof Kozlowski4460dc22017-06-15 17:06:28 +0200197 con |= EXYNOS_PIN_FUNC_INPUT << shift;
Krzysztof Kozlowskiaf0b0ba2017-06-14 15:18:28 +0200198 writel(con, bank->pctl_base + reg_con);
Tomasz Figaf6a82492014-08-09 01:48:05 +0200199
200 spin_unlock_irqrestore(&bank->slock, flags);
201
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900202 gpiochip_unlock_as_irq(&bank->gpio_chip, irqd->hwirq);
Tomasz Figaf6a82492014-08-09 01:48:05 +0200203}
204
Thomas Abraham43b169d2012-09-07 06:07:19 +0900205/*
206 * irq_chip for gpio interrupts.
207 */
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200208static struct exynos_irq_chip exynos_gpio_irq_chip = {
209 .chip = {
210 .name = "exynos_gpio_irq_chip",
211 .irq_unmask = exynos_irq_unmask,
212 .irq_mask = exynos_irq_mask,
213 .irq_ack = exynos_irq_ack,
214 .irq_set_type = exynos_irq_set_type,
Tomasz Figaf6a82492014-08-09 01:48:05 +0200215 .irq_request_resources = exynos_irq_request_resources,
216 .irq_release_resources = exynos_irq_release_resources,
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200217 },
218 .eint_con = EXYNOS_GPIO_ECON_OFFSET,
219 .eint_mask = EXYNOS_GPIO_EMASK_OFFSET,
220 .eint_pend = EXYNOS_GPIO_EPEND_OFFSET,
Krzysztof Kozlowskia8be2af2018-07-23 19:52:58 +0200221 /* eint_wake_mask_value not used */
Thomas Abraham43b169d2012-09-07 06:07:19 +0900222};
223
Abhilash Kesavan6f5e41b2014-10-09 19:24:30 +0530224static int exynos_eint_irq_map(struct irq_domain *h, unsigned int virq,
Thomas Abraham43b169d2012-09-07 06:07:19 +0900225 irq_hw_number_t hw)
226{
Tomasz Figa595be722012-10-11 10:11:16 +0200227 struct samsung_pin_bank *b = h->host_data;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900228
Tomasz Figa595be722012-10-11 10:11:16 +0200229 irq_set_chip_data(virq, b);
Abhilash Kesavan0d3d30d2014-10-09 19:24:29 +0530230 irq_set_chip_and_handler(virq, &b->irq_chip->chip,
Thomas Abraham43b169d2012-09-07 06:07:19 +0900231 handle_level_irq);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900232 return 0;
233}
234
Thomas Abraham43b169d2012-09-07 06:07:19 +0900235/*
Abhilash Kesavan6f5e41b2014-10-09 19:24:30 +0530236 * irq domain callbacks for external gpio and wakeup interrupt controllers.
Thomas Abraham43b169d2012-09-07 06:07:19 +0900237 */
Abhilash Kesavan6f5e41b2014-10-09 19:24:30 +0530238static const struct irq_domain_ops exynos_eint_irqd_ops = {
239 .map = exynos_eint_irq_map,
Thomas Abraham43b169d2012-09-07 06:07:19 +0900240 .xlate = irq_domain_xlate_twocell,
241};
242
243static irqreturn_t exynos_eint_gpio_irq(int irq, void *data)
244{
245 struct samsung_pinctrl_drv_data *d = data;
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200246 struct samsung_pin_bank *bank = d->pin_banks;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900247 unsigned int svc, group, pin, virq;
248
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900249 svc = readl(bank->eint_base + EXYNOS_SVC_OFFSET);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900250 group = EXYNOS_SVC_GROUP(svc);
251 pin = svc & EXYNOS_SVC_NUM_MASK;
252
253 if (!group)
254 return IRQ_HANDLED;
255 bank += (group - 1);
256
Tomasz Figa595be722012-10-11 10:11:16 +0200257 virq = irq_linear_revmap(bank->irq_domain, pin);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900258 if (!virq)
259 return IRQ_NONE;
260 generic_handle_irq(virq);
261 return IRQ_HANDLED;
262}
263
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200264struct exynos_eint_gpio_save {
265 u32 eint_con;
266 u32 eint_fltcon0;
267 u32 eint_fltcon1;
268};
269
Thomas Abraham43b169d2012-09-07 06:07:19 +0900270/*
271 * exynos_eint_gpio_init() - setup handling of external gpio interrupts.
272 * @d: driver data of samsung pinctrl driver.
273 */
Krzysztof Kozlowskicfa76dd2017-05-16 20:56:27 +0200274int exynos_eint_gpio_init(struct samsung_pinctrl_drv_data *d)
Thomas Abraham43b169d2012-09-07 06:07:19 +0900275{
Tomasz Figa595be722012-10-11 10:11:16 +0200276 struct samsung_pin_bank *bank;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900277 struct device *dev = d->dev;
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200278 int ret;
279 int i;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900280
281 if (!d->irq) {
282 dev_err(dev, "irq number not available\n");
283 return -EINVAL;
284 }
285
286 ret = devm_request_irq(dev, d->irq, exynos_eint_gpio_irq,
287 0, dev_name(dev), d);
288 if (ret) {
289 dev_err(dev, "irq request failed\n");
290 return -ENXIO;
291 }
292
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200293 bank = d->pin_banks;
294 for (i = 0; i < d->nr_banks; ++i, ++bank) {
Tomasz Figa595be722012-10-11 10:11:16 +0200295 if (bank->eint_type != EINT_TYPE_GPIO)
296 continue;
297 bank->irq_domain = irq_domain_add_linear(bank->of_node,
Abhilash Kesavan6f5e41b2014-10-09 19:24:30 +0530298 bank->nr_pins, &exynos_eint_irqd_ops, bank);
Tomasz Figa595be722012-10-11 10:11:16 +0200299 if (!bank->irq_domain) {
300 dev_err(dev, "gpio irq domain add failed\n");
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200301 ret = -ENXIO;
302 goto err_domains;
303 }
304
305 bank->soc_priv = devm_kzalloc(d->dev,
306 sizeof(struct exynos_eint_gpio_save), GFP_KERNEL);
307 if (!bank->soc_priv) {
308 irq_domain_remove(bank->irq_domain);
309 ret = -ENOMEM;
310 goto err_domains;
Tomasz Figa595be722012-10-11 10:11:16 +0200311 }
Abhilash Kesavan0d3d30d2014-10-09 19:24:29 +0530312
313 bank->irq_chip = &exynos_gpio_irq_chip;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900314 }
315
316 return 0;
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200317
318err_domains:
319 for (--i, --bank; i >= 0; --i, --bank) {
320 if (bank->eint_type != EINT_TYPE_GPIO)
321 continue;
322 irq_domain_remove(bank->irq_domain);
323 }
324
325 return ret;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900326}
327
Tomasz Figaad350cd2013-05-17 18:24:27 +0200328static int exynos_wkup_irq_set_wake(struct irq_data *irqd, unsigned int on)
329{
Krzysztof Kozlowskia8be2af2018-07-23 19:52:58 +0200330 struct irq_chip *chip = irq_data_get_irq_chip(irqd);
331 struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
Tomasz Figaad350cd2013-05-17 18:24:27 +0200332 struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
333 unsigned long bit = 1UL << (2 * bank->eint_offset + irqd->hwirq);
334
335 pr_info("wake %s for irq %d\n", on ? "enabled" : "disabled", irqd->irq);
336
337 if (!on)
Krzysztof Kozlowskib45eb402019-02-05 20:59:09 +0100338 our_chip->eint_wake_mask_value |= bit;
Tomasz Figaad350cd2013-05-17 18:24:27 +0200339 else
Krzysztof Kozlowskib45eb402019-02-05 20:59:09 +0100340 our_chip->eint_wake_mask_value &= ~bit;
Tomasz Figaad350cd2013-05-17 18:24:27 +0200341
342 return 0;
343}
344
Thomas Abraham43b169d2012-09-07 06:07:19 +0900345/*
346 * irq_chip for wakeup interrupts
347 */
Krzysztof Kozlowskibb928df2018-07-23 19:52:56 +0200348static const struct exynos_irq_chip s5pv210_wkup_irq_chip __initconst = {
349 .chip = {
350 .name = "s5pv210_wkup_irq_chip",
351 .irq_unmask = exynos_irq_unmask,
352 .irq_mask = exynos_irq_mask,
353 .irq_ack = exynos_irq_ack,
354 .irq_set_type = exynos_irq_set_type,
355 .irq_set_wake = exynos_wkup_irq_set_wake,
356 .irq_request_resources = exynos_irq_request_resources,
357 .irq_release_resources = exynos_irq_release_resources,
358 },
359 .eint_con = EXYNOS_WKUP_ECON_OFFSET,
360 .eint_mask = EXYNOS_WKUP_EMASK_OFFSET,
361 .eint_pend = EXYNOS_WKUP_EPEND_OFFSET,
Krzysztof Kozlowskia8be2af2018-07-23 19:52:58 +0200362 .eint_wake_mask_value = EXYNOS_EINT_WAKEUP_MASK_DISABLED,
363 /* Only difference with exynos4210_wkup_irq_chip: */
364 .eint_wake_mask_reg = S5PV210_EINT_WAKEUP_MASK,
Krzysztof Kozlowskibb928df2018-07-23 19:52:56 +0200365};
366
Krzysztof Kozlowski71b96c32017-05-23 20:41:39 +0200367static const struct exynos_irq_chip exynos4210_wkup_irq_chip __initconst = {
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200368 .chip = {
Abhilash Kesavan14c255d2014-10-09 19:24:31 +0530369 .name = "exynos4210_wkup_irq_chip",
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200370 .irq_unmask = exynos_irq_unmask,
371 .irq_mask = exynos_irq_mask,
372 .irq_ack = exynos_irq_ack,
373 .irq_set_type = exynos_irq_set_type,
374 .irq_set_wake = exynos_wkup_irq_set_wake,
Tomasz Figaf6a82492014-08-09 01:48:05 +0200375 .irq_request_resources = exynos_irq_request_resources,
376 .irq_release_resources = exynos_irq_release_resources,
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200377 },
378 .eint_con = EXYNOS_WKUP_ECON_OFFSET,
379 .eint_mask = EXYNOS_WKUP_EMASK_OFFSET,
380 .eint_pend = EXYNOS_WKUP_EPEND_OFFSET,
Krzysztof Kozlowskia8be2af2018-07-23 19:52:58 +0200381 .eint_wake_mask_value = EXYNOS_EINT_WAKEUP_MASK_DISABLED,
382 .eint_wake_mask_reg = EXYNOS_EINT_WAKEUP_MASK,
Thomas Abraham43b169d2012-09-07 06:07:19 +0900383};
384
Krzysztof Kozlowski71b96c32017-05-23 20:41:39 +0200385static const struct exynos_irq_chip exynos7_wkup_irq_chip __initconst = {
Abhilash Kesavan14c255d2014-10-09 19:24:31 +0530386 .chip = {
387 .name = "exynos7_wkup_irq_chip",
388 .irq_unmask = exynos_irq_unmask,
389 .irq_mask = exynos_irq_mask,
390 .irq_ack = exynos_irq_ack,
391 .irq_set_type = exynos_irq_set_type,
392 .irq_set_wake = exynos_wkup_irq_set_wake,
393 .irq_request_resources = exynos_irq_request_resources,
394 .irq_release_resources = exynos_irq_release_resources,
395 },
396 .eint_con = EXYNOS7_WKUP_ECON_OFFSET,
397 .eint_mask = EXYNOS7_WKUP_EMASK_OFFSET,
398 .eint_pend = EXYNOS7_WKUP_EPEND_OFFSET,
Krzysztof Kozlowskia8be2af2018-07-23 19:52:58 +0200399 .eint_wake_mask_value = EXYNOS_EINT_WAKEUP_MASK_DISABLED,
400 .eint_wake_mask_reg = EXYNOS5433_EINT_WAKEUP_MASK,
Abhilash Kesavan14c255d2014-10-09 19:24:31 +0530401};
402
403/* list of external wakeup controllers supported */
404static const struct of_device_id exynos_wkup_irq_ids[] = {
Krzysztof Kozlowskibb928df2018-07-23 19:52:56 +0200405 { .compatible = "samsung,s5pv210-wakeup-eint",
406 .data = &s5pv210_wkup_irq_chip },
Abhilash Kesavan14c255d2014-10-09 19:24:31 +0530407 { .compatible = "samsung,exynos4210-wakeup-eint",
408 .data = &exynos4210_wkup_irq_chip },
409 { .compatible = "samsung,exynos7-wakeup-eint",
410 .data = &exynos7_wkup_irq_chip },
411 { }
412};
413
Thomas Abraham43b169d2012-09-07 06:07:19 +0900414/* interrupt handler for wakeup interrupts 0..15 */
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200415static void exynos_irq_eint0_15(struct irq_desc *desc)
Thomas Abraham43b169d2012-09-07 06:07:19 +0900416{
Jiang Liu5663bb22015-06-04 12:13:16 +0800417 struct exynos_weint_data *eintd = irq_desc_get_handler_data(desc);
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200418 struct samsung_pin_bank *bank = eintd->bank;
Jiang Liu5663bb22015-06-04 12:13:16 +0800419 struct irq_chip *chip = irq_desc_get_chip(desc);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900420 int eint_irq;
421
422 chained_irq_enter(chip, desc);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900423
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200424 eint_irq = irq_linear_revmap(bank->irq_domain, eintd->irq);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900425 generic_handle_irq(eint_irq);
perr perr26fecf02016-08-16 18:45:29 +0800426
Thomas Abraham43b169d2012-09-07 06:07:19 +0900427 chained_irq_exit(chip, desc);
428}
429
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200430static inline void exynos_irq_demux_eint(unsigned long pend,
431 struct irq_domain *domain)
Thomas Abraham43b169d2012-09-07 06:07:19 +0900432{
433 unsigned int irq;
434
435 while (pend) {
436 irq = fls(pend) - 1;
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200437 generic_handle_irq(irq_find_mapping(domain, irq));
Thomas Abraham43b169d2012-09-07 06:07:19 +0900438 pend &= ~(1 << irq);
439 }
440}
441
442/* interrupt handler for wakeup interrupt 16 */
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200443static void exynos_irq_demux_eint16_31(struct irq_desc *desc)
Thomas Abraham43b169d2012-09-07 06:07:19 +0900444{
Jiang Liu5663bb22015-06-04 12:13:16 +0800445 struct irq_chip *chip = irq_desc_get_chip(desc);
446 struct exynos_muxed_weint_data *eintd = irq_desc_get_handler_data(desc);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900447 unsigned long pend;
Tomasz Figade590492012-09-21 07:33:55 +0900448 unsigned long mask;
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200449 int i;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900450
451 chained_irq_enter(chip, desc);
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200452
453 for (i = 0; i < eintd->nr_banks; ++i) {
454 struct samsung_pin_bank *b = eintd->banks[i];
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900455 pend = readl(b->eint_base + b->irq_chip->eint_pend
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200456 + b->eint_offset);
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900457 mask = readl(b->eint_base + b->irq_chip->eint_mask
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200458 + b->eint_offset);
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200459 exynos_irq_demux_eint(pend & ~mask, b->irq_domain);
460 }
461
Thomas Abraham43b169d2012-09-07 06:07:19 +0900462 chained_irq_exit(chip, desc);
463}
464
Thomas Abraham43b169d2012-09-07 06:07:19 +0900465/*
466 * exynos_eint_wkup_init() - setup handling of external wakeup interrupts.
467 * @d: driver data of samsung pinctrl driver.
468 */
Krzysztof Kozlowskicfa76dd2017-05-16 20:56:27 +0200469int exynos_eint_wkup_init(struct samsung_pinctrl_drv_data *d)
Thomas Abraham43b169d2012-09-07 06:07:19 +0900470{
471 struct device *dev = d->dev;
Tomasz Figac3ad0562012-09-21 07:34:01 +0900472 struct device_node *wkup_np = NULL;
473 struct device_node *np;
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200474 struct samsung_pin_bank *bank;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900475 struct exynos_weint_data *weint_data;
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200476 struct exynos_muxed_weint_data *muxed_data;
Abhilash Kesavan14c255d2014-10-09 19:24:31 +0530477 struct exynos_irq_chip *irq_chip;
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200478 unsigned int muxed_banks = 0;
479 unsigned int i;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900480 int idx, irq;
481
Tomasz Figac3ad0562012-09-21 07:34:01 +0900482 for_each_child_of_node(dev->of_node, np) {
Abhilash Kesavan14c255d2014-10-09 19:24:31 +0530483 const struct of_device_id *match;
484
485 match = of_match_node(exynos_wkup_irq_ids, np);
486 if (match) {
487 irq_chip = kmemdup(match->data,
488 sizeof(*irq_chip), GFP_KERNEL);
Krzysztof Kozlowskia1ea9a42017-05-23 20:41:40 +0200489 if (!irq_chip)
490 return -ENOMEM;
Tomasz Figac3ad0562012-09-21 07:34:01 +0900491 wkup_np = np;
492 break;
493 }
Thomas Abraham43b169d2012-09-07 06:07:19 +0900494 }
Tomasz Figac3ad0562012-09-21 07:34:01 +0900495 if (!wkup_np)
496 return -ENODEV;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900497
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200498 bank = d->pin_banks;
499 for (i = 0; i < d->nr_banks; ++i, ++bank) {
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200500 if (bank->eint_type != EINT_TYPE_WKUP)
501 continue;
502
503 bank->irq_domain = irq_domain_add_linear(bank->of_node,
Abhilash Kesavan6f5e41b2014-10-09 19:24:30 +0530504 bank->nr_pins, &exynos_eint_irqd_ops, bank);
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200505 if (!bank->irq_domain) {
506 dev_err(dev, "wkup irq domain add failed\n");
507 return -ENXIO;
508 }
509
Abhilash Kesavan14c255d2014-10-09 19:24:31 +0530510 bank->irq_chip = irq_chip;
Abhilash Kesavan0d3d30d2014-10-09 19:24:29 +0530511
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200512 if (!of_find_property(bank->of_node, "interrupts", NULL)) {
513 bank->eint_type = EINT_TYPE_WKUP_MUX;
514 ++muxed_banks;
515 continue;
516 }
517
Kees Cooka86854d2018-06-12 14:07:58 -0700518 weint_data = devm_kcalloc(dev,
519 bank->nr_pins, sizeof(*weint_data),
520 GFP_KERNEL);
Marek Szyprowskifa5c0f42017-01-19 14:48:45 +0100521 if (!weint_data)
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200522 return -ENOMEM;
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200523
524 for (idx = 0; idx < bank->nr_pins; ++idx) {
525 irq = irq_of_parse_and_map(bank->of_node, idx);
526 if (!irq) {
527 dev_err(dev, "irq number for eint-%s-%d not found\n",
528 bank->name, idx);
529 continue;
530 }
531 weint_data[idx].irq = idx;
532 weint_data[idx].bank = bank;
Thomas Gleixnerc21f7842015-06-21 21:11:07 +0200533 irq_set_chained_handler_and_data(irq,
534 exynos_irq_eint0_15,
535 &weint_data[idx]);
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200536 }
Thomas Abraham43b169d2012-09-07 06:07:19 +0900537 }
538
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200539 if (!muxed_banks)
540 return 0;
541
542 irq = irq_of_parse_and_map(wkup_np, 0);
543 if (!irq) {
544 dev_err(dev, "irq number for muxed EINTs not found\n");
545 return 0;
546 }
547
548 muxed_data = devm_kzalloc(dev, sizeof(*muxed_data)
549 + muxed_banks*sizeof(struct samsung_pin_bank *), GFP_KERNEL);
Marek Szyprowskifa5c0f42017-01-19 14:48:45 +0100550 if (!muxed_data)
Thomas Abraham43b169d2012-09-07 06:07:19 +0900551 return -ENOMEM;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900552
Thomas Gleixnerbb56fc352015-06-21 20:16:16 +0200553 irq_set_chained_handler_and_data(irq, exynos_irq_demux_eint16_31,
554 muxed_data);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900555
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200556 bank = d->pin_banks;
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200557 idx = 0;
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200558 for (i = 0; i < d->nr_banks; ++i, ++bank) {
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200559 if (bank->eint_type != EINT_TYPE_WKUP_MUX)
560 continue;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900561
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200562 muxed_data->banks[idx++] = bank;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900563 }
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200564 muxed_data->nr_banks = muxed_banks;
565
Thomas Abraham43b169d2012-09-07 06:07:19 +0900566 return 0;
567}
568
Krzysztof Kozlowskia8be2af2018-07-23 19:52:58 +0200569static void
570exynos_pinctrl_set_eint_wakeup_mask(struct samsung_pinctrl_drv_data *drvdata,
571 struct exynos_irq_chip *irq_chip)
572{
573 struct regmap *pmu_regs;
574
575 if (!drvdata->retention_ctrl || !drvdata->retention_ctrl->priv) {
576 dev_warn(drvdata->dev,
577 "No retention data configured bank with external wakeup interrupt. Wake-up mask will not be set.\n");
578 return;
579 }
580
581 pmu_regs = drvdata->retention_ctrl->priv;
582 dev_info(drvdata->dev,
Krzysztof Kozlowski01f19742018-08-06 18:33:40 +0200583 "Setting external wakeup interrupt mask: 0x%x\n",
Krzysztof Kozlowskia8be2af2018-07-23 19:52:58 +0200584 irq_chip->eint_wake_mask_value);
585
586 regmap_write(pmu_regs, irq_chip->eint_wake_mask_reg,
587 irq_chip->eint_wake_mask_value);
588}
589
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200590static void exynos_pinctrl_suspend_bank(
591 struct samsung_pinctrl_drv_data *drvdata,
592 struct samsung_pin_bank *bank)
593{
594 struct exynos_eint_gpio_save *save = bank->soc_priv;
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900595 void __iomem *regs = bank->eint_base;
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200596
597 save->eint_con = readl(regs + EXYNOS_GPIO_ECON_OFFSET
598 + bank->eint_offset);
599 save->eint_fltcon0 = readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
600 + 2 * bank->eint_offset);
601 save->eint_fltcon1 = readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
602 + 2 * bank->eint_offset + 4);
603
604 pr_debug("%s: save con %#010x\n", bank->name, save->eint_con);
605 pr_debug("%s: save fltcon0 %#010x\n", bank->name, save->eint_fltcon0);
606 pr_debug("%s: save fltcon1 %#010x\n", bank->name, save->eint_fltcon1);
607}
608
Krzysztof Kozlowskicfa76dd2017-05-16 20:56:27 +0200609void exynos_pinctrl_suspend(struct samsung_pinctrl_drv_data *drvdata)
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200610{
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200611 struct samsung_pin_bank *bank = drvdata->pin_banks;
Krzysztof Kozlowskia8be2af2018-07-23 19:52:58 +0200612 struct exynos_irq_chip *irq_chip = NULL;
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200613 int i;
614
Krzysztof Kozlowskia8be2af2018-07-23 19:52:58 +0200615 for (i = 0; i < drvdata->nr_banks; ++i, ++bank) {
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200616 if (bank->eint_type == EINT_TYPE_GPIO)
617 exynos_pinctrl_suspend_bank(drvdata, bank);
Krzysztof Kozlowskia8be2af2018-07-23 19:52:58 +0200618 else if (bank->eint_type == EINT_TYPE_WKUP) {
619 if (!irq_chip) {
620 irq_chip = bank->irq_chip;
621 exynos_pinctrl_set_eint_wakeup_mask(drvdata,
622 irq_chip);
623 } else if (bank->irq_chip != irq_chip) {
624 dev_warn(drvdata->dev,
625 "More than one external wakeup interrupt chip configured (bank: %s). This is not supported by hardware nor by driver.\n",
626 bank->name);
627 }
628 }
629 }
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200630}
631
632static void exynos_pinctrl_resume_bank(
633 struct samsung_pinctrl_drv_data *drvdata,
634 struct samsung_pin_bank *bank)
635{
636 struct exynos_eint_gpio_save *save = bank->soc_priv;
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900637 void __iomem *regs = bank->eint_base;
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200638
639 pr_debug("%s: con %#010x => %#010x\n", bank->name,
640 readl(regs + EXYNOS_GPIO_ECON_OFFSET
641 + bank->eint_offset), save->eint_con);
642 pr_debug("%s: fltcon0 %#010x => %#010x\n", bank->name,
643 readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
644 + 2 * bank->eint_offset), save->eint_fltcon0);
645 pr_debug("%s: fltcon1 %#010x => %#010x\n", bank->name,
646 readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
647 + 2 * bank->eint_offset + 4), save->eint_fltcon1);
648
649 writel(save->eint_con, regs + EXYNOS_GPIO_ECON_OFFSET
650 + bank->eint_offset);
651 writel(save->eint_fltcon0, regs + EXYNOS_GPIO_EFLTCON_OFFSET
652 + 2 * bank->eint_offset);
653 writel(save->eint_fltcon1, regs + EXYNOS_GPIO_EFLTCON_OFFSET
654 + 2 * bank->eint_offset + 4);
655}
656
Krzysztof Kozlowskicfa76dd2017-05-16 20:56:27 +0200657void exynos_pinctrl_resume(struct samsung_pinctrl_drv_data *drvdata)
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200658{
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200659 struct samsung_pin_bank *bank = drvdata->pin_banks;
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200660 int i;
661
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200662 for (i = 0; i < drvdata->nr_banks; ++i, ++bank)
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200663 if (bank->eint_type == EINT_TYPE_GPIO)
664 exynos_pinctrl_resume_bank(drvdata, bank);
665}
666
Marek Szyprowski07731012017-01-26 10:29:25 +0100667static void exynos_retention_enable(struct samsung_pinctrl_drv_data *drvdata)
668{
669 if (drvdata->retention_ctrl->refcnt)
670 atomic_inc(drvdata->retention_ctrl->refcnt);
671}
672
673static void exynos_retention_disable(struct samsung_pinctrl_drv_data *drvdata)
674{
675 struct samsung_retention_ctrl *ctrl = drvdata->retention_ctrl;
676 struct regmap *pmu_regs = ctrl->priv;
677 int i;
678
679 if (ctrl->refcnt && !atomic_dec_and_test(ctrl->refcnt))
680 return;
681
682 for (i = 0; i < ctrl->nr_regs; i++)
683 regmap_write(pmu_regs, ctrl->regs[i], ctrl->value);
684}
685
Krzysztof Kozlowskicfa76dd2017-05-16 20:56:27 +0200686struct samsung_retention_ctrl *
Marek Szyprowski07731012017-01-26 10:29:25 +0100687exynos_retention_init(struct samsung_pinctrl_drv_data *drvdata,
688 const struct samsung_retention_data *data)
689{
690 struct samsung_retention_ctrl *ctrl;
691 struct regmap *pmu_regs;
Marek Szyprowski8fe9bf02017-03-23 09:03:22 +0100692 int i;
Marek Szyprowski07731012017-01-26 10:29:25 +0100693
694 ctrl = devm_kzalloc(drvdata->dev, sizeof(*ctrl), GFP_KERNEL);
695 if (!ctrl)
696 return ERR_PTR(-ENOMEM);
697
698 pmu_regs = exynos_get_pmu_regmap();
699 if (IS_ERR(pmu_regs))
700 return ERR_CAST(pmu_regs);
701
702 ctrl->priv = pmu_regs;
703 ctrl->regs = data->regs;
704 ctrl->nr_regs = data->nr_regs;
705 ctrl->value = data->value;
706 ctrl->refcnt = data->refcnt;
707 ctrl->enable = exynos_retention_enable;
708 ctrl->disable = exynos_retention_disable;
709
Marek Szyprowski8fe9bf02017-03-23 09:03:22 +0100710 /* Ensure that retention is disabled on driver init */
711 for (i = 0; i < ctrl->nr_regs; i++)
712 regmap_write(pmu_regs, ctrl->regs[i], ctrl->value);
713
Marek Szyprowski07731012017-01-26 10:29:25 +0100714 return ctrl;
715}