blob: a263ddd94945d1cc377c5c264390f45494cbfd87 [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>
Thomas Abraham43b169d2012-09-07 06:07:19 +090028
Krzysztof Kozlowski4460dc22017-06-15 17:06:28 +020029#include <dt-bindings/pinctrl/samsung.h>
30
Thomas Abraham43b169d2012-09-07 06:07:19 +090031#include "pinctrl-samsung.h"
32#include "pinctrl-exynos.h"
33
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020034struct exynos_irq_chip {
35 struct irq_chip chip;
36
37 u32 eint_con;
38 u32 eint_mask;
39 u32 eint_pend;
40};
41
42static inline struct exynos_irq_chip *to_exynos_irq_chip(struct irq_chip *chip)
43{
44 return container_of(chip, struct exynos_irq_chip, chip);
45}
Tomasz Figa499147c2013-03-18 22:31:52 +010046
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020047static void exynos_irq_mask(struct irq_data *irqd)
Thomas Abraham43b169d2012-09-07 06:07:19 +090048{
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020049 struct irq_chip *chip = irq_data_get_irq_chip(irqd);
50 struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
Tomasz Figa595be722012-10-11 10:11:16 +020051 struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020052 unsigned long reg_mask = our_chip->eint_mask + bank->eint_offset;
Thomas Abraham43b169d2012-09-07 06:07:19 +090053 unsigned long mask;
Doug Anderson5ae8cf72013-06-12 10:33:17 -070054 unsigned long flags;
55
56 spin_lock_irqsave(&bank->slock, flags);
Thomas Abraham43b169d2012-09-07 06:07:19 +090057
Chanwoo Choi8b1bd112016-11-09 17:40:10 +090058 mask = readl(bank->eint_base + reg_mask);
Tomasz Figa595be722012-10-11 10:11:16 +020059 mask |= 1 << irqd->hwirq;
Chanwoo Choi8b1bd112016-11-09 17:40:10 +090060 writel(mask, bank->eint_base + reg_mask);
Doug Anderson5ae8cf72013-06-12 10:33:17 -070061
62 spin_unlock_irqrestore(&bank->slock, flags);
Thomas Abraham43b169d2012-09-07 06:07:19 +090063}
64
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020065static void exynos_irq_ack(struct irq_data *irqd)
Thomas Abraham43b169d2012-09-07 06:07:19 +090066{
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020067 struct irq_chip *chip = irq_data_get_irq_chip(irqd);
68 struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
Tomasz Figa595be722012-10-11 10:11:16 +020069 struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020070 unsigned long reg_pend = our_chip->eint_pend + bank->eint_offset;
Thomas Abraham43b169d2012-09-07 06:07:19 +090071
Chanwoo Choi8b1bd112016-11-09 17:40:10 +090072 writel(1 << irqd->hwirq, bank->eint_base + reg_pend);
Thomas Abraham43b169d2012-09-07 06:07:19 +090073}
74
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020075static void exynos_irq_unmask(struct irq_data *irqd)
Doug Anderson5ace03f2013-06-12 10:33:18 -070076{
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020077 struct irq_chip *chip = irq_data_get_irq_chip(irqd);
78 struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
Doug Anderson5ace03f2013-06-12 10:33:18 -070079 struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020080 unsigned long reg_mask = our_chip->eint_mask + bank->eint_offset;
Doug Anderson5ace03f2013-06-12 10:33:18 -070081 unsigned long mask;
82 unsigned long flags;
83
Doug Anderson5a68e7a2013-06-17 09:50:43 -070084 /*
85 * Ack level interrupts right before unmask
86 *
87 * If we don't do this we'll get a double-interrupt. Level triggered
88 * interrupts must not fire an interrupt if the level is not
89 * _currently_ active, even if it was active while the interrupt was
90 * masked.
91 */
92 if (irqd_get_trigger_type(irqd) & IRQ_TYPE_LEVEL_MASK)
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +020093 exynos_irq_ack(irqd);
Doug Anderson5a68e7a2013-06-17 09:50:43 -070094
Doug Anderson5ace03f2013-06-12 10:33:18 -070095 spin_lock_irqsave(&bank->slock, flags);
96
Chanwoo Choi8b1bd112016-11-09 17:40:10 +090097 mask = readl(bank->eint_base + reg_mask);
Doug Anderson5ace03f2013-06-12 10:33:18 -070098 mask &= ~(1 << irqd->hwirq);
Chanwoo Choi8b1bd112016-11-09 17:40:10 +090099 writel(mask, bank->eint_base + reg_mask);
Doug Anderson5ace03f2013-06-12 10:33:18 -0700100
101 spin_unlock_irqrestore(&bank->slock, flags);
102}
103
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200104static int exynos_irq_set_type(struct irq_data *irqd, unsigned int type)
Thomas Abraham43b169d2012-09-07 06:07:19 +0900105{
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200106 struct irq_chip *chip = irq_data_get_irq_chip(irqd);
107 struct exynos_irq_chip *our_chip = to_exynos_irq_chip(chip);
Tomasz Figa595be722012-10-11 10:11:16 +0200108 struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
Tomasz Figaf6a82492014-08-09 01:48:05 +0200109 unsigned int shift = EXYNOS_EINT_CON_LEN * irqd->hwirq;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900110 unsigned int con, trig_type;
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200111 unsigned long reg_con = our_chip->eint_con + bank->eint_offset;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900112
113 switch (type) {
114 case IRQ_TYPE_EDGE_RISING:
115 trig_type = EXYNOS_EINT_EDGE_RISING;
116 break;
117 case IRQ_TYPE_EDGE_FALLING:
118 trig_type = EXYNOS_EINT_EDGE_FALLING;
119 break;
120 case IRQ_TYPE_EDGE_BOTH:
121 trig_type = EXYNOS_EINT_EDGE_BOTH;
122 break;
123 case IRQ_TYPE_LEVEL_HIGH:
124 trig_type = EXYNOS_EINT_LEVEL_HIGH;
125 break;
126 case IRQ_TYPE_LEVEL_LOW:
127 trig_type = EXYNOS_EINT_LEVEL_LOW;
128 break;
129 default:
130 pr_err("unsupported external interrupt type\n");
131 return -EINVAL;
132 }
133
134 if (type & IRQ_TYPE_EDGE_BOTH)
Thomas Gleixner40ec1682015-06-23 15:52:57 +0200135 irq_set_handler_locked(irqd, handle_edge_irq);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900136 else
Thomas Gleixner40ec1682015-06-23 15:52:57 +0200137 irq_set_handler_locked(irqd, handle_level_irq);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900138
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900139 con = readl(bank->eint_base + reg_con);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900140 con &= ~(EXYNOS_EINT_CON_MASK << shift);
141 con |= trig_type << shift;
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900142 writel(con, bank->eint_base + reg_con);
Tomasz Figaee2f5732012-09-21 07:33:48 +0900143
Tomasz Figaf6a82492014-08-09 01:48:05 +0200144 return 0;
145}
146
147static int exynos_irq_request_resources(struct irq_data *irqd)
148{
Tomasz Figaf6a82492014-08-09 01:48:05 +0200149 struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
Tomasz Figa94ce9442014-09-23 21:05:39 +0200150 const struct samsung_pin_bank_type *bank_type = bank->type;
Krzysztof Kozlowskibbed85f2017-07-18 19:43:28 +0200151 unsigned long reg_con, flags;
152 unsigned int shift, mask, con;
Tomasz Figaf6a82492014-08-09 01:48:05 +0200153 int ret;
154
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900155 ret = gpiochip_lock_as_irq(&bank->gpio_chip, irqd->hwirq);
Tomasz Figaf6a82492014-08-09 01:48:05 +0200156 if (ret) {
Linus Walleij58383c782015-11-04 09:56:26 +0100157 dev_err(bank->gpio_chip.parent,
158 "unable to lock pin %s-%lu IRQ\n",
Tomasz Figaf6a82492014-08-09 01:48:05 +0200159 bank->name, irqd->hwirq);
160 return ret;
161 }
162
Tomasz Figa43fc9e72013-03-18 22:31:53 +0100163 reg_con = bank->pctl_offset + bank_type->reg_offset[PINCFG_TYPE_FUNC];
Tomasz Figaf6a82492014-08-09 01:48:05 +0200164 shift = irqd->hwirq * bank_type->fld_width[PINCFG_TYPE_FUNC];
Tomasz Figa499147c2013-03-18 22:31:52 +0100165 mask = (1 << bank_type->fld_width[PINCFG_TYPE_FUNC]) - 1;
Tomasz Figaee2f5732012-09-21 07:33:48 +0900166
Tomasz Figa19846952013-03-18 22:31:50 +0100167 spin_lock_irqsave(&bank->slock, flags);
168
Krzysztof Kozlowskiaf0b0ba2017-06-14 15:18:28 +0200169 con = readl(bank->pctl_base + reg_con);
Tomasz Figaee2f5732012-09-21 07:33:48 +0900170 con &= ~(mask << shift);
Krzysztof Kozlowski4460dc22017-06-15 17:06:28 +0200171 con |= EXYNOS_PIN_FUNC_EINT << shift;
Krzysztof Kozlowskiaf0b0ba2017-06-14 15:18:28 +0200172 writel(con, bank->pctl_base + reg_con);
Tomasz Figaee2f5732012-09-21 07:33:48 +0900173
Tomasz Figa19846952013-03-18 22:31:50 +0100174 spin_unlock_irqrestore(&bank->slock, flags);
175
Thomas Abraham43b169d2012-09-07 06:07:19 +0900176 return 0;
177}
178
Tomasz Figaf6a82492014-08-09 01:48:05 +0200179static void exynos_irq_release_resources(struct irq_data *irqd)
180{
Tomasz Figaf6a82492014-08-09 01:48:05 +0200181 struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
Tomasz Figa94ce9442014-09-23 21:05:39 +0200182 const struct samsung_pin_bank_type *bank_type = bank->type;
Krzysztof Kozlowskibbed85f2017-07-18 19:43:28 +0200183 unsigned long reg_con, flags;
184 unsigned int shift, mask, con;
Tomasz Figaf6a82492014-08-09 01:48:05 +0200185
186 reg_con = bank->pctl_offset + bank_type->reg_offset[PINCFG_TYPE_FUNC];
187 shift = irqd->hwirq * bank_type->fld_width[PINCFG_TYPE_FUNC];
188 mask = (1 << bank_type->fld_width[PINCFG_TYPE_FUNC]) - 1;
189
Tomasz Figaf6a82492014-08-09 01:48:05 +0200190 spin_lock_irqsave(&bank->slock, flags);
191
Krzysztof Kozlowskiaf0b0ba2017-06-14 15:18:28 +0200192 con = readl(bank->pctl_base + reg_con);
Tomasz Figaf6a82492014-08-09 01:48:05 +0200193 con &= ~(mask << shift);
Krzysztof Kozlowski4460dc22017-06-15 17:06:28 +0200194 con |= EXYNOS_PIN_FUNC_INPUT << shift;
Krzysztof Kozlowskiaf0b0ba2017-06-14 15:18:28 +0200195 writel(con, bank->pctl_base + reg_con);
Tomasz Figaf6a82492014-08-09 01:48:05 +0200196
197 spin_unlock_irqrestore(&bank->slock, flags);
198
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900199 gpiochip_unlock_as_irq(&bank->gpio_chip, irqd->hwirq);
Tomasz Figaf6a82492014-08-09 01:48:05 +0200200}
201
Thomas Abraham43b169d2012-09-07 06:07:19 +0900202/*
203 * irq_chip for gpio interrupts.
204 */
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200205static struct exynos_irq_chip exynos_gpio_irq_chip = {
206 .chip = {
207 .name = "exynos_gpio_irq_chip",
208 .irq_unmask = exynos_irq_unmask,
209 .irq_mask = exynos_irq_mask,
210 .irq_ack = exynos_irq_ack,
211 .irq_set_type = exynos_irq_set_type,
Tomasz Figaf6a82492014-08-09 01:48:05 +0200212 .irq_request_resources = exynos_irq_request_resources,
213 .irq_release_resources = exynos_irq_release_resources,
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200214 },
215 .eint_con = EXYNOS_GPIO_ECON_OFFSET,
216 .eint_mask = EXYNOS_GPIO_EMASK_OFFSET,
217 .eint_pend = EXYNOS_GPIO_EPEND_OFFSET,
Thomas Abraham43b169d2012-09-07 06:07:19 +0900218};
219
Abhilash Kesavan6f5e41b2014-10-09 19:24:30 +0530220static int exynos_eint_irq_map(struct irq_domain *h, unsigned int virq,
Thomas Abraham43b169d2012-09-07 06:07:19 +0900221 irq_hw_number_t hw)
222{
Tomasz Figa595be722012-10-11 10:11:16 +0200223 struct samsung_pin_bank *b = h->host_data;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900224
Tomasz Figa595be722012-10-11 10:11:16 +0200225 irq_set_chip_data(virq, b);
Abhilash Kesavan0d3d30d2014-10-09 19:24:29 +0530226 irq_set_chip_and_handler(virq, &b->irq_chip->chip,
Thomas Abraham43b169d2012-09-07 06:07:19 +0900227 handle_level_irq);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900228 return 0;
229}
230
Thomas Abraham43b169d2012-09-07 06:07:19 +0900231/*
Abhilash Kesavan6f5e41b2014-10-09 19:24:30 +0530232 * irq domain callbacks for external gpio and wakeup interrupt controllers.
Thomas Abraham43b169d2012-09-07 06:07:19 +0900233 */
Abhilash Kesavan6f5e41b2014-10-09 19:24:30 +0530234static const struct irq_domain_ops exynos_eint_irqd_ops = {
235 .map = exynos_eint_irq_map,
Thomas Abraham43b169d2012-09-07 06:07:19 +0900236 .xlate = irq_domain_xlate_twocell,
237};
238
239static irqreturn_t exynos_eint_gpio_irq(int irq, void *data)
240{
241 struct samsung_pinctrl_drv_data *d = data;
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200242 struct samsung_pin_bank *bank = d->pin_banks;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900243 unsigned int svc, group, pin, virq;
244
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900245 svc = readl(bank->eint_base + EXYNOS_SVC_OFFSET);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900246 group = EXYNOS_SVC_GROUP(svc);
247 pin = svc & EXYNOS_SVC_NUM_MASK;
248
249 if (!group)
250 return IRQ_HANDLED;
251 bank += (group - 1);
252
Tomasz Figa595be722012-10-11 10:11:16 +0200253 virq = irq_linear_revmap(bank->irq_domain, pin);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900254 if (!virq)
255 return IRQ_NONE;
256 generic_handle_irq(virq);
257 return IRQ_HANDLED;
258}
259
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200260struct exynos_eint_gpio_save {
261 u32 eint_con;
262 u32 eint_fltcon0;
263 u32 eint_fltcon1;
264};
265
Thomas Abraham43b169d2012-09-07 06:07:19 +0900266/*
267 * exynos_eint_gpio_init() - setup handling of external gpio interrupts.
268 * @d: driver data of samsung pinctrl driver.
269 */
Krzysztof Kozlowskicfa76dd2017-05-16 20:56:27 +0200270int exynos_eint_gpio_init(struct samsung_pinctrl_drv_data *d)
Thomas Abraham43b169d2012-09-07 06:07:19 +0900271{
Tomasz Figa595be722012-10-11 10:11:16 +0200272 struct samsung_pin_bank *bank;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900273 struct device *dev = d->dev;
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200274 int ret;
275 int i;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900276
277 if (!d->irq) {
278 dev_err(dev, "irq number not available\n");
279 return -EINVAL;
280 }
281
282 ret = devm_request_irq(dev, d->irq, exynos_eint_gpio_irq,
283 0, dev_name(dev), d);
284 if (ret) {
285 dev_err(dev, "irq request failed\n");
286 return -ENXIO;
287 }
288
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200289 bank = d->pin_banks;
290 for (i = 0; i < d->nr_banks; ++i, ++bank) {
Tomasz Figa595be722012-10-11 10:11:16 +0200291 if (bank->eint_type != EINT_TYPE_GPIO)
292 continue;
293 bank->irq_domain = irq_domain_add_linear(bank->of_node,
Abhilash Kesavan6f5e41b2014-10-09 19:24:30 +0530294 bank->nr_pins, &exynos_eint_irqd_ops, bank);
Tomasz Figa595be722012-10-11 10:11:16 +0200295 if (!bank->irq_domain) {
296 dev_err(dev, "gpio irq domain add failed\n");
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200297 ret = -ENXIO;
298 goto err_domains;
299 }
300
301 bank->soc_priv = devm_kzalloc(d->dev,
302 sizeof(struct exynos_eint_gpio_save), GFP_KERNEL);
303 if (!bank->soc_priv) {
304 irq_domain_remove(bank->irq_domain);
305 ret = -ENOMEM;
306 goto err_domains;
Tomasz Figa595be722012-10-11 10:11:16 +0200307 }
Abhilash Kesavan0d3d30d2014-10-09 19:24:29 +0530308
309 bank->irq_chip = &exynos_gpio_irq_chip;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900310 }
311
312 return 0;
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200313
314err_domains:
315 for (--i, --bank; i >= 0; --i, --bank) {
316 if (bank->eint_type != EINT_TYPE_GPIO)
317 continue;
318 irq_domain_remove(bank->irq_domain);
319 }
320
321 return ret;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900322}
323
Tomasz Figaad350cd2013-05-17 18:24:27 +0200324static u32 exynos_eint_wake_mask = 0xffffffff;
325
326u32 exynos_get_eint_wake_mask(void)
327{
328 return exynos_eint_wake_mask;
329}
330
331static int exynos_wkup_irq_set_wake(struct irq_data *irqd, unsigned int on)
332{
333 struct samsung_pin_bank *bank = irq_data_get_irq_chip_data(irqd);
334 unsigned long bit = 1UL << (2 * bank->eint_offset + irqd->hwirq);
335
336 pr_info("wake %s for irq %d\n", on ? "enabled" : "disabled", irqd->irq);
337
338 if (!on)
339 exynos_eint_wake_mask |= bit;
340 else
341 exynos_eint_wake_mask &= ~bit;
342
343 return 0;
344}
345
Thomas Abraham43b169d2012-09-07 06:07:19 +0900346/*
347 * irq_chip for wakeup interrupts
348 */
Krzysztof Kozlowski71b96c32017-05-23 20:41:39 +0200349static const struct exynos_irq_chip exynos4210_wkup_irq_chip __initconst = {
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200350 .chip = {
Abhilash Kesavan14c255d2014-10-09 19:24:31 +0530351 .name = "exynos4210_wkup_irq_chip",
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200352 .irq_unmask = exynos_irq_unmask,
353 .irq_mask = exynos_irq_mask,
354 .irq_ack = exynos_irq_ack,
355 .irq_set_type = exynos_irq_set_type,
356 .irq_set_wake = exynos_wkup_irq_set_wake,
Tomasz Figaf6a82492014-08-09 01:48:05 +0200357 .irq_request_resources = exynos_irq_request_resources,
358 .irq_release_resources = exynos_irq_release_resources,
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200359 },
360 .eint_con = EXYNOS_WKUP_ECON_OFFSET,
361 .eint_mask = EXYNOS_WKUP_EMASK_OFFSET,
362 .eint_pend = EXYNOS_WKUP_EPEND_OFFSET,
Thomas Abraham43b169d2012-09-07 06:07:19 +0900363};
364
Krzysztof Kozlowski71b96c32017-05-23 20:41:39 +0200365static const struct exynos_irq_chip exynos7_wkup_irq_chip __initconst = {
Abhilash Kesavan14c255d2014-10-09 19:24:31 +0530366 .chip = {
367 .name = "exynos7_wkup_irq_chip",
368 .irq_unmask = exynos_irq_unmask,
369 .irq_mask = exynos_irq_mask,
370 .irq_ack = exynos_irq_ack,
371 .irq_set_type = exynos_irq_set_type,
372 .irq_set_wake = exynos_wkup_irq_set_wake,
373 .irq_request_resources = exynos_irq_request_resources,
374 .irq_release_resources = exynos_irq_release_resources,
375 },
376 .eint_con = EXYNOS7_WKUP_ECON_OFFSET,
377 .eint_mask = EXYNOS7_WKUP_EMASK_OFFSET,
378 .eint_pend = EXYNOS7_WKUP_EPEND_OFFSET,
379};
380
381/* list of external wakeup controllers supported */
382static const struct of_device_id exynos_wkup_irq_ids[] = {
383 { .compatible = "samsung,exynos4210-wakeup-eint",
384 .data = &exynos4210_wkup_irq_chip },
385 { .compatible = "samsung,exynos7-wakeup-eint",
386 .data = &exynos7_wkup_irq_chip },
387 { }
388};
389
Thomas Abraham43b169d2012-09-07 06:07:19 +0900390/* interrupt handler for wakeup interrupts 0..15 */
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200391static void exynos_irq_eint0_15(struct irq_desc *desc)
Thomas Abraham43b169d2012-09-07 06:07:19 +0900392{
Jiang Liu5663bb22015-06-04 12:13:16 +0800393 struct exynos_weint_data *eintd = irq_desc_get_handler_data(desc);
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200394 struct samsung_pin_bank *bank = eintd->bank;
Jiang Liu5663bb22015-06-04 12:13:16 +0800395 struct irq_chip *chip = irq_desc_get_chip(desc);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900396 int eint_irq;
397
398 chained_irq_enter(chip, desc);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900399
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200400 eint_irq = irq_linear_revmap(bank->irq_domain, eintd->irq);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900401 generic_handle_irq(eint_irq);
perr perr26fecf02016-08-16 18:45:29 +0800402
Thomas Abraham43b169d2012-09-07 06:07:19 +0900403 chained_irq_exit(chip, desc);
404}
405
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200406static inline void exynos_irq_demux_eint(unsigned long pend,
407 struct irq_domain *domain)
Thomas Abraham43b169d2012-09-07 06:07:19 +0900408{
409 unsigned int irq;
410
411 while (pend) {
412 irq = fls(pend) - 1;
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200413 generic_handle_irq(irq_find_mapping(domain, irq));
Thomas Abraham43b169d2012-09-07 06:07:19 +0900414 pend &= ~(1 << irq);
415 }
416}
417
418/* interrupt handler for wakeup interrupt 16 */
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200419static void exynos_irq_demux_eint16_31(struct irq_desc *desc)
Thomas Abraham43b169d2012-09-07 06:07:19 +0900420{
Jiang Liu5663bb22015-06-04 12:13:16 +0800421 struct irq_chip *chip = irq_desc_get_chip(desc);
422 struct exynos_muxed_weint_data *eintd = irq_desc_get_handler_data(desc);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900423 unsigned long pend;
Tomasz Figade590492012-09-21 07:33:55 +0900424 unsigned long mask;
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200425 int i;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900426
427 chained_irq_enter(chip, desc);
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200428
429 for (i = 0; i < eintd->nr_banks; ++i) {
430 struct samsung_pin_bank *b = eintd->banks[i];
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900431 pend = readl(b->eint_base + b->irq_chip->eint_pend
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200432 + b->eint_offset);
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900433 mask = readl(b->eint_base + b->irq_chip->eint_mask
Tomasz Figa2e4a4fd2014-07-02 17:41:01 +0200434 + b->eint_offset);
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200435 exynos_irq_demux_eint(pend & ~mask, b->irq_domain);
436 }
437
Thomas Abraham43b169d2012-09-07 06:07:19 +0900438 chained_irq_exit(chip, desc);
439}
440
Thomas Abraham43b169d2012-09-07 06:07:19 +0900441/*
442 * exynos_eint_wkup_init() - setup handling of external wakeup interrupts.
443 * @d: driver data of samsung pinctrl driver.
444 */
Krzysztof Kozlowskicfa76dd2017-05-16 20:56:27 +0200445int exynos_eint_wkup_init(struct samsung_pinctrl_drv_data *d)
Thomas Abraham43b169d2012-09-07 06:07:19 +0900446{
447 struct device *dev = d->dev;
Tomasz Figac3ad0562012-09-21 07:34:01 +0900448 struct device_node *wkup_np = NULL;
449 struct device_node *np;
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200450 struct samsung_pin_bank *bank;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900451 struct exynos_weint_data *weint_data;
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200452 struct exynos_muxed_weint_data *muxed_data;
Abhilash Kesavan14c255d2014-10-09 19:24:31 +0530453 struct exynos_irq_chip *irq_chip;
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200454 unsigned int muxed_banks = 0;
455 unsigned int i;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900456 int idx, irq;
457
Tomasz Figac3ad0562012-09-21 07:34:01 +0900458 for_each_child_of_node(dev->of_node, np) {
Abhilash Kesavan14c255d2014-10-09 19:24:31 +0530459 const struct of_device_id *match;
460
461 match = of_match_node(exynos_wkup_irq_ids, np);
462 if (match) {
463 irq_chip = kmemdup(match->data,
464 sizeof(*irq_chip), GFP_KERNEL);
Krzysztof Kozlowskia1ea9a42017-05-23 20:41:40 +0200465 if (!irq_chip)
466 return -ENOMEM;
Tomasz Figac3ad0562012-09-21 07:34:01 +0900467 wkup_np = np;
468 break;
469 }
Thomas Abraham43b169d2012-09-07 06:07:19 +0900470 }
Tomasz Figac3ad0562012-09-21 07:34:01 +0900471 if (!wkup_np)
472 return -ENODEV;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900473
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200474 bank = d->pin_banks;
475 for (i = 0; i < d->nr_banks; ++i, ++bank) {
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200476 if (bank->eint_type != EINT_TYPE_WKUP)
477 continue;
478
479 bank->irq_domain = irq_domain_add_linear(bank->of_node,
Abhilash Kesavan6f5e41b2014-10-09 19:24:30 +0530480 bank->nr_pins, &exynos_eint_irqd_ops, bank);
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200481 if (!bank->irq_domain) {
482 dev_err(dev, "wkup irq domain add failed\n");
483 return -ENXIO;
484 }
485
Abhilash Kesavan14c255d2014-10-09 19:24:31 +0530486 bank->irq_chip = irq_chip;
Abhilash Kesavan0d3d30d2014-10-09 19:24:29 +0530487
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200488 if (!of_find_property(bank->of_node, "interrupts", NULL)) {
489 bank->eint_type = EINT_TYPE_WKUP_MUX;
490 ++muxed_banks;
491 continue;
492 }
493
Kees Cooka86854d2018-06-12 14:07:58 -0700494 weint_data = devm_kcalloc(dev,
495 bank->nr_pins, sizeof(*weint_data),
496 GFP_KERNEL);
Marek Szyprowskifa5c0f42017-01-19 14:48:45 +0100497 if (!weint_data)
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200498 return -ENOMEM;
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200499
500 for (idx = 0; idx < bank->nr_pins; ++idx) {
501 irq = irq_of_parse_and_map(bank->of_node, idx);
502 if (!irq) {
503 dev_err(dev, "irq number for eint-%s-%d not found\n",
504 bank->name, idx);
505 continue;
506 }
507 weint_data[idx].irq = idx;
508 weint_data[idx].bank = bank;
Thomas Gleixnerc21f7842015-06-21 21:11:07 +0200509 irq_set_chained_handler_and_data(irq,
510 exynos_irq_eint0_15,
511 &weint_data[idx]);
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200512 }
Thomas Abraham43b169d2012-09-07 06:07:19 +0900513 }
514
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200515 if (!muxed_banks)
516 return 0;
517
518 irq = irq_of_parse_and_map(wkup_np, 0);
519 if (!irq) {
520 dev_err(dev, "irq number for muxed EINTs not found\n");
521 return 0;
522 }
523
524 muxed_data = devm_kzalloc(dev, sizeof(*muxed_data)
525 + muxed_banks*sizeof(struct samsung_pin_bank *), GFP_KERNEL);
Marek Szyprowskifa5c0f42017-01-19 14:48:45 +0100526 if (!muxed_data)
Thomas Abraham43b169d2012-09-07 06:07:19 +0900527 return -ENOMEM;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900528
Thomas Gleixnerbb56fc352015-06-21 20:16:16 +0200529 irq_set_chained_handler_and_data(irq, exynos_irq_demux_eint16_31,
530 muxed_data);
Thomas Abraham43b169d2012-09-07 06:07:19 +0900531
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200532 bank = d->pin_banks;
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200533 idx = 0;
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200534 for (i = 0; i < d->nr_banks; ++i, ++bank) {
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200535 if (bank->eint_type != EINT_TYPE_WKUP_MUX)
536 continue;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900537
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200538 muxed_data->banks[idx++] = bank;
Thomas Abraham43b169d2012-09-07 06:07:19 +0900539 }
Tomasz Figaa04b07c2012-10-11 10:11:18 +0200540 muxed_data->nr_banks = muxed_banks;
541
Thomas Abraham43b169d2012-09-07 06:07:19 +0900542 return 0;
543}
544
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200545static void exynos_pinctrl_suspend_bank(
546 struct samsung_pinctrl_drv_data *drvdata,
547 struct samsung_pin_bank *bank)
548{
549 struct exynos_eint_gpio_save *save = bank->soc_priv;
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900550 void __iomem *regs = bank->eint_base;
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200551
552 save->eint_con = readl(regs + EXYNOS_GPIO_ECON_OFFSET
553 + bank->eint_offset);
554 save->eint_fltcon0 = readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
555 + 2 * bank->eint_offset);
556 save->eint_fltcon1 = readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
557 + 2 * bank->eint_offset + 4);
558
559 pr_debug("%s: save con %#010x\n", bank->name, save->eint_con);
560 pr_debug("%s: save fltcon0 %#010x\n", bank->name, save->eint_fltcon0);
561 pr_debug("%s: save fltcon1 %#010x\n", bank->name, save->eint_fltcon1);
562}
563
Krzysztof Kozlowskicfa76dd2017-05-16 20:56:27 +0200564void exynos_pinctrl_suspend(struct samsung_pinctrl_drv_data *drvdata)
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200565{
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200566 struct samsung_pin_bank *bank = drvdata->pin_banks;
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200567 int i;
568
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200569 for (i = 0; i < drvdata->nr_banks; ++i, ++bank)
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200570 if (bank->eint_type == EINT_TYPE_GPIO)
571 exynos_pinctrl_suspend_bank(drvdata, bank);
572}
573
574static void exynos_pinctrl_resume_bank(
575 struct samsung_pinctrl_drv_data *drvdata,
576 struct samsung_pin_bank *bank)
577{
578 struct exynos_eint_gpio_save *save = bank->soc_priv;
Chanwoo Choi8b1bd112016-11-09 17:40:10 +0900579 void __iomem *regs = bank->eint_base;
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200580
581 pr_debug("%s: con %#010x => %#010x\n", bank->name,
582 readl(regs + EXYNOS_GPIO_ECON_OFFSET
583 + bank->eint_offset), save->eint_con);
584 pr_debug("%s: fltcon0 %#010x => %#010x\n", bank->name,
585 readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
586 + 2 * bank->eint_offset), save->eint_fltcon0);
587 pr_debug("%s: fltcon1 %#010x => %#010x\n", bank->name,
588 readl(regs + EXYNOS_GPIO_EFLTCON_OFFSET
589 + 2 * bank->eint_offset + 4), save->eint_fltcon1);
590
591 writel(save->eint_con, regs + EXYNOS_GPIO_ECON_OFFSET
592 + bank->eint_offset);
593 writel(save->eint_fltcon0, regs + EXYNOS_GPIO_EFLTCON_OFFSET
594 + 2 * bank->eint_offset);
595 writel(save->eint_fltcon1, regs + EXYNOS_GPIO_EFLTCON_OFFSET
596 + 2 * bank->eint_offset + 4);
597}
598
Krzysztof Kozlowskicfa76dd2017-05-16 20:56:27 +0200599void exynos_pinctrl_resume(struct samsung_pinctrl_drv_data *drvdata)
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200600{
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200601 struct samsung_pin_bank *bank = drvdata->pin_banks;
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200602 int i;
603
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200604 for (i = 0; i < drvdata->nr_banks; ++i, ++bank)
Tomasz Figa7ccbc602013-05-22 16:03:17 +0200605 if (bank->eint_type == EINT_TYPE_GPIO)
606 exynos_pinctrl_resume_bank(drvdata, bank);
607}
608
Marek Szyprowski07731012017-01-26 10:29:25 +0100609static void exynos_retention_enable(struct samsung_pinctrl_drv_data *drvdata)
610{
611 if (drvdata->retention_ctrl->refcnt)
612 atomic_inc(drvdata->retention_ctrl->refcnt);
613}
614
615static void exynos_retention_disable(struct samsung_pinctrl_drv_data *drvdata)
616{
617 struct samsung_retention_ctrl *ctrl = drvdata->retention_ctrl;
618 struct regmap *pmu_regs = ctrl->priv;
619 int i;
620
621 if (ctrl->refcnt && !atomic_dec_and_test(ctrl->refcnt))
622 return;
623
624 for (i = 0; i < ctrl->nr_regs; i++)
625 regmap_write(pmu_regs, ctrl->regs[i], ctrl->value);
626}
627
Krzysztof Kozlowskicfa76dd2017-05-16 20:56:27 +0200628struct samsung_retention_ctrl *
Marek Szyprowski07731012017-01-26 10:29:25 +0100629exynos_retention_init(struct samsung_pinctrl_drv_data *drvdata,
630 const struct samsung_retention_data *data)
631{
632 struct samsung_retention_ctrl *ctrl;
633 struct regmap *pmu_regs;
Marek Szyprowski8fe9bf02017-03-23 09:03:22 +0100634 int i;
Marek Szyprowski07731012017-01-26 10:29:25 +0100635
636 ctrl = devm_kzalloc(drvdata->dev, sizeof(*ctrl), GFP_KERNEL);
637 if (!ctrl)
638 return ERR_PTR(-ENOMEM);
639
640 pmu_regs = exynos_get_pmu_regmap();
641 if (IS_ERR(pmu_regs))
642 return ERR_CAST(pmu_regs);
643
644 ctrl->priv = pmu_regs;
645 ctrl->regs = data->regs;
646 ctrl->nr_regs = data->nr_regs;
647 ctrl->value = data->value;
648 ctrl->refcnt = data->refcnt;
649 ctrl->enable = exynos_retention_enable;
650 ctrl->disable = exynos_retention_disable;
651
Marek Szyprowski8fe9bf02017-03-23 09:03:22 +0100652 /* Ensure that retention is disabled on driver init */
653 for (i = 0; i < ctrl->nr_regs; i++)
654 regmap_write(pmu_regs, ctrl->regs[i], ctrl->value);
655
Marek Szyprowski07731012017-01-26 10:29:25 +0100656 return ctrl;
657}