blob: 84a7375cee0a36456bb25fdcfd0c045b76f13449 [file] [log] [blame]
Thomas Gleixner0b712182019-05-28 09:57:08 -07001// SPDX-License-Identifier: GPL-2.0-only
Magnus Damma07e1032012-05-17 15:22:23 +09002/*
3 * Emma Mobile GPIO Support - GIO
4 *
5 * Copyright (C) 2012 Magnus Damm
Magnus Damma07e1032012-05-17 15:22:23 +09006 */
7
8#include <linux/init.h>
9#include <linux/platform_device.h>
10#include <linux/spinlock.h>
11#include <linux/interrupt.h>
12#include <linux/ioport.h>
13#include <linux/io.h>
14#include <linux/irq.h>
15#include <linux/irqdomain.h>
16#include <linux/bitops.h>
17#include <linux/err.h>
Linus Walleij7275cb72018-02-09 00:45:50 +010018#include <linux/gpio/driver.h>
Magnus Damma07e1032012-05-17 15:22:23 +090019#include <linux/slab.h>
20#include <linux/module.h>
Magnus Damm640efa02013-07-03 13:14:32 +090021#include <linux/pinctrl/consumer.h>
Magnus Damma07e1032012-05-17 15:22:23 +090022
23struct em_gio_priv {
24 void __iomem *base0;
25 void __iomem *base1;
Magnus Damma07e1032012-05-17 15:22:23 +090026 spinlock_t sense_lock;
27 struct platform_device *pdev;
28 struct gpio_chip gpio_chip;
29 struct irq_chip irq_chip;
30 struct irq_domain *irq_domain;
31};
32
33#define GIO_E1 0x00
34#define GIO_E0 0x04
35#define GIO_EM 0x04
36#define GIO_OL 0x08
37#define GIO_OH 0x0c
38#define GIO_I 0x10
39#define GIO_IIA 0x14
40#define GIO_IEN 0x18
41#define GIO_IDS 0x1c
42#define GIO_IIM 0x1c
43#define GIO_RAW 0x20
44#define GIO_MST 0x24
45#define GIO_IIR 0x28
46
47#define GIO_IDT0 0x40
48#define GIO_IDT1 0x44
49#define GIO_IDT2 0x48
50#define GIO_IDT3 0x4c
51#define GIO_RAWBL 0x50
52#define GIO_RAWBH 0x54
53#define GIO_IRBL 0x58
54#define GIO_IRBH 0x5c
55
56#define GIO_IDT(n) (GIO_IDT0 + ((n) * 4))
57
58static inline unsigned long em_gio_read(struct em_gio_priv *p, int offs)
59{
60 if (offs < GIO_IDT0)
61 return ioread32(p->base0 + offs);
62 else
63 return ioread32(p->base1 + (offs - GIO_IDT0));
64}
65
66static inline void em_gio_write(struct em_gio_priv *p, int offs,
67 unsigned long value)
68{
69 if (offs < GIO_IDT0)
70 iowrite32(value, p->base0 + offs);
71 else
72 iowrite32(value, p->base1 + (offs - GIO_IDT0));
73}
74
Magnus Damma07e1032012-05-17 15:22:23 +090075static void em_gio_irq_disable(struct irq_data *d)
76{
Axel Lina9f77c92012-09-04 21:58:33 +080077 struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
Magnus Damma07e1032012-05-17 15:22:23 +090078
79 em_gio_write(p, GIO_IDS, BIT(irqd_to_hwirq(d)));
80}
81
82static void em_gio_irq_enable(struct irq_data *d)
83{
Axel Lina9f77c92012-09-04 21:58:33 +080084 struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
Magnus Damma07e1032012-05-17 15:22:23 +090085
86 em_gio_write(p, GIO_IEN, BIT(irqd_to_hwirq(d)));
87}
88
Linus Walleij57ef0422014-03-14 18:16:20 +010089static int em_gio_irq_reqres(struct irq_data *d)
Linus Walleij0dc61622013-11-20 10:16:54 +010090{
91 struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
Andy Shevchenko41d69082018-07-30 15:38:34 +030092 int ret;
Linus Walleij0dc61622013-11-20 10:16:54 +010093
Andy Shevchenko41d69082018-07-30 15:38:34 +030094 ret = gpiochip_lock_as_irq(&p->gpio_chip, irqd_to_hwirq(d));
95 if (ret) {
Linus Walleij58383c782015-11-04 09:56:26 +010096 dev_err(p->gpio_chip.parent,
Linus Walleij0dc61622013-11-20 10:16:54 +010097 "unable to lock HW IRQ %lu for IRQ\n",
98 irqd_to_hwirq(d));
Andy Shevchenko41d69082018-07-30 15:38:34 +030099 return ret;
Linus Walleij57ef0422014-03-14 18:16:20 +0100100 }
Linus Walleij0dc61622013-11-20 10:16:54 +0100101 return 0;
102}
103
Linus Walleij57ef0422014-03-14 18:16:20 +0100104static void em_gio_irq_relres(struct irq_data *d)
Linus Walleij0dc61622013-11-20 10:16:54 +0100105{
106 struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
107
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900108 gpiochip_unlock_as_irq(&p->gpio_chip, irqd_to_hwirq(d));
Linus Walleij0dc61622013-11-20 10:16:54 +0100109}
110
111
Magnus Damma07e1032012-05-17 15:22:23 +0900112#define GIO_ASYNC(x) (x + 8)
113
114static unsigned char em_gio_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
115 [IRQ_TYPE_EDGE_RISING] = GIO_ASYNC(0x00),
116 [IRQ_TYPE_EDGE_FALLING] = GIO_ASYNC(0x01),
117 [IRQ_TYPE_LEVEL_HIGH] = GIO_ASYNC(0x02),
118 [IRQ_TYPE_LEVEL_LOW] = GIO_ASYNC(0x03),
119 [IRQ_TYPE_EDGE_BOTH] = GIO_ASYNC(0x04),
120};
121
122static int em_gio_irq_set_type(struct irq_data *d, unsigned int type)
123{
124 unsigned char value = em_gio_sense_table[type & IRQ_TYPE_SENSE_MASK];
Axel Lina9f77c92012-09-04 21:58:33 +0800125 struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
Magnus Damma07e1032012-05-17 15:22:23 +0900126 unsigned int reg, offset, shift;
127 unsigned long flags;
128 unsigned long tmp;
129
130 if (!value)
131 return -EINVAL;
132
133 offset = irqd_to_hwirq(d);
134
135 pr_debug("gio: sense irq = %d, mode = %d\n", offset, value);
136
137 /* 8 x 4 bit fields in 4 IDT registers */
138 reg = GIO_IDT(offset >> 3);
139 shift = (offset & 0x07) << 4;
140
141 spin_lock_irqsave(&p->sense_lock, flags);
142
143 /* disable the interrupt in IIA */
144 tmp = em_gio_read(p, GIO_IIA);
145 tmp &= ~BIT(offset);
146 em_gio_write(p, GIO_IIA, tmp);
147
148 /* change the sense setting in IDT */
149 tmp = em_gio_read(p, reg);
150 tmp &= ~(0xf << shift);
151 tmp |= value << shift;
152 em_gio_write(p, reg, tmp);
153
154 /* clear pending interrupts */
155 em_gio_write(p, GIO_IIR, BIT(offset));
156
157 /* enable the interrupt in IIA */
158 tmp = em_gio_read(p, GIO_IIA);
159 tmp |= BIT(offset);
160 em_gio_write(p, GIO_IIA, tmp);
161
162 spin_unlock_irqrestore(&p->sense_lock, flags);
163
164 return 0;
165}
166
167static irqreturn_t em_gio_irq_handler(int irq, void *dev_id)
168{
169 struct em_gio_priv *p = dev_id;
170 unsigned long pending;
171 unsigned int offset, irqs_handled = 0;
172
173 while ((pending = em_gio_read(p, GIO_MST))) {
174 offset = __ffs(pending);
175 em_gio_write(p, GIO_IIR, BIT(offset));
176 generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
177 irqs_handled++;
178 }
179
180 return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
181}
182
183static inline struct em_gio_priv *gpio_to_priv(struct gpio_chip *chip)
184{
Linus Walleij6219e7b2015-12-06 00:36:39 +0100185 return gpiochip_get_data(chip);
Magnus Damma07e1032012-05-17 15:22:23 +0900186}
187
188static int em_gio_direction_input(struct gpio_chip *chip, unsigned offset)
189{
190 em_gio_write(gpio_to_priv(chip), GIO_E0, BIT(offset));
191 return 0;
192}
193
194static int em_gio_get(struct gpio_chip *chip, unsigned offset)
195{
Linus Walleij8388f292015-12-21 10:46:18 +0100196 return !!(em_gio_read(gpio_to_priv(chip), GIO_I) & BIT(offset));
Magnus Damma07e1032012-05-17 15:22:23 +0900197}
198
199static void __em_gio_set(struct gpio_chip *chip, unsigned int reg,
200 unsigned shift, int value)
201{
202 /* upper 16 bits contains mask and lower 16 actual value */
203 em_gio_write(gpio_to_priv(chip), reg,
Javier Martinez Canillas5f077642014-04-27 02:00:47 +0200204 (BIT(shift + 16)) | (value << shift));
Magnus Damma07e1032012-05-17 15:22:23 +0900205}
206
207static void em_gio_set(struct gpio_chip *chip, unsigned offset, int value)
208{
209 /* output is split into two registers */
210 if (offset < 16)
211 __em_gio_set(chip, GIO_OL, offset, value);
212 else
213 __em_gio_set(chip, GIO_OH, offset - 16, value);
214}
215
216static int em_gio_direction_output(struct gpio_chip *chip, unsigned offset,
217 int value)
218{
219 /* write GPIO value to output before selecting output mode of pin */
220 em_gio_set(chip, offset, value);
221 em_gio_write(gpio_to_priv(chip), GIO_E1, BIT(offset));
222 return 0;
223}
224
225static int em_gio_to_irq(struct gpio_chip *chip, unsigned offset)
226{
Linus Walleij73855002012-10-16 20:15:02 +0200227 return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset);
Magnus Damma07e1032012-05-17 15:22:23 +0900228}
229
Magnus Damm640efa02013-07-03 13:14:32 +0900230static int em_gio_request(struct gpio_chip *chip, unsigned offset)
231{
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200232 return pinctrl_gpio_request(chip->base + offset);
Magnus Damm640efa02013-07-03 13:14:32 +0900233}
234
235static void em_gio_free(struct gpio_chip *chip, unsigned offset)
236{
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200237 pinctrl_gpio_free(chip->base + offset);
Magnus Damm640efa02013-07-03 13:14:32 +0900238
239 /* Set the GPIO as an input to ensure that the next GPIO request won't
240 * drive the GPIO pin as an output.
241 */
242 em_gio_direction_input(chip, offset);
243}
244
Linus Walleij2d61e3e2013-10-11 19:21:34 +0200245static int em_gio_irq_domain_map(struct irq_domain *h, unsigned int irq,
246 irq_hw_number_t hwirq)
Magnus Damma07e1032012-05-17 15:22:23 +0900247{
248 struct em_gio_priv *p = h->host_data;
249
Linus Walleij2d61e3e2013-10-11 19:21:34 +0200250 pr_debug("gio: map hw irq = %d, irq = %d\n", (int)hwirq, irq);
Magnus Damma07e1032012-05-17 15:22:23 +0900251
Linus Walleij2d61e3e2013-10-11 19:21:34 +0200252 irq_set_chip_data(irq, h->host_data);
253 irq_set_chip_and_handler(irq, &p->irq_chip, handle_level_irq);
Magnus Damma07e1032012-05-17 15:22:23 +0900254 return 0;
255}
256
Krzysztof Kozlowski0b354dc2015-04-27 21:54:07 +0900257static const struct irq_domain_ops em_gio_irq_domain_ops = {
Magnus Damma07e1032012-05-17 15:22:23 +0900258 .map = em_gio_irq_domain_map,
Magnus Damm753c5982013-02-26 22:26:23 +0900259 .xlate = irq_domain_xlate_twocell,
Magnus Damma07e1032012-05-17 15:22:23 +0900260};
261
Bill Pemberton38363092012-11-19 13:22:34 -0500262static int em_gio_probe(struct platform_device *pdev)
Magnus Damma07e1032012-05-17 15:22:23 +0900263{
Magnus Damma07e1032012-05-17 15:22:23 +0900264 struct em_gio_priv *p;
265 struct resource *io[2], *irq[2];
266 struct gpio_chip *gpio_chip;
267 struct irq_chip *irq_chip;
268 const char *name = dev_name(&pdev->dev);
Geert Uytterhoeven527b3972015-06-23 15:48:02 +0200269 unsigned int ngpios;
Magnus Damma07e1032012-05-17 15:22:23 +0900270 int ret;
271
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900272 p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
Magnus Damma07e1032012-05-17 15:22:23 +0900273 if (!p) {
Magnus Damma07e1032012-05-17 15:22:23 +0900274 ret = -ENOMEM;
275 goto err0;
276 }
277
278 p->pdev = pdev;
279 platform_set_drvdata(pdev, p);
280 spin_lock_init(&p->sense_lock);
281
282 io[0] = platform_get_resource(pdev, IORESOURCE_MEM, 0);
283 io[1] = platform_get_resource(pdev, IORESOURCE_MEM, 1);
284 irq[0] = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
285 irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
286
Magnus Damm753c5982013-02-26 22:26:23 +0900287 if (!io[0] || !io[1] || !irq[0] || !irq[1]) {
288 dev_err(&pdev->dev, "missing IRQ or IOMEM\n");
Magnus Damma07e1032012-05-17 15:22:23 +0900289 ret = -EINVAL;
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900290 goto err0;
Magnus Damma07e1032012-05-17 15:22:23 +0900291 }
292
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900293 p->base0 = devm_ioremap_nocache(&pdev->dev, io[0]->start,
294 resource_size(io[0]));
Magnus Damma07e1032012-05-17 15:22:23 +0900295 if (!p->base0) {
296 dev_err(&pdev->dev, "failed to remap low I/O memory\n");
297 ret = -ENXIO;
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900298 goto err0;
Magnus Damma07e1032012-05-17 15:22:23 +0900299 }
300
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900301 p->base1 = devm_ioremap_nocache(&pdev->dev, io[1]->start,
302 resource_size(io[1]));
Magnus Damma07e1032012-05-17 15:22:23 +0900303 if (!p->base1) {
304 dev_err(&pdev->dev, "failed to remap high I/O memory\n");
305 ret = -ENXIO;
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900306 goto err0;
Magnus Damma07e1032012-05-17 15:22:23 +0900307 }
308
Geert Uytterhoeven527b3972015-06-23 15:48:02 +0200309 if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
310 dev_err(&pdev->dev, "Missing ngpios OF property\n");
311 ret = -EINVAL;
312 goto err0;
Magnus Damm753c5982013-02-26 22:26:23 +0900313 }
314
Magnus Damma07e1032012-05-17 15:22:23 +0900315 gpio_chip = &p->gpio_chip;
Ian Moltonb5927852013-09-02 16:44:55 +0100316 gpio_chip->of_node = pdev->dev.of_node;
Magnus Damma07e1032012-05-17 15:22:23 +0900317 gpio_chip->direction_input = em_gio_direction_input;
318 gpio_chip->get = em_gio_get;
319 gpio_chip->direction_output = em_gio_direction_output;
320 gpio_chip->set = em_gio_set;
321 gpio_chip->to_irq = em_gio_to_irq;
Magnus Damm640efa02013-07-03 13:14:32 +0900322 gpio_chip->request = em_gio_request;
323 gpio_chip->free = em_gio_free;
Magnus Damma07e1032012-05-17 15:22:23 +0900324 gpio_chip->label = name;
Linus Walleij58383c782015-11-04 09:56:26 +0100325 gpio_chip->parent = &pdev->dev;
Magnus Damma07e1032012-05-17 15:22:23 +0900326 gpio_chip->owner = THIS_MODULE;
Geert Uytterhoeven527b3972015-06-23 15:48:02 +0200327 gpio_chip->base = -1;
328 gpio_chip->ngpio = ngpios;
Magnus Damma07e1032012-05-17 15:22:23 +0900329
330 irq_chip = &p->irq_chip;
331 irq_chip->name = name;
332 irq_chip->irq_mask = em_gio_irq_disable;
333 irq_chip->irq_unmask = em_gio_irq_enable;
Magnus Damma07e1032012-05-17 15:22:23 +0900334 irq_chip->irq_set_type = em_gio_irq_set_type;
Linus Walleij57ef0422014-03-14 18:16:20 +0100335 irq_chip->irq_request_resources = em_gio_irq_reqres;
336 irq_chip->irq_release_resources = em_gio_irq_relres;
Magnus Damm03621b62013-11-20 09:23:44 +0900337 irq_chip->flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MASK_ON_SUSPEND;
Magnus Damma07e1032012-05-17 15:22:23 +0900338
Geert Uytterhoeven527b3972015-06-23 15:48:02 +0200339 p->irq_domain = irq_domain_add_simple(pdev->dev.of_node, ngpios, 0,
Linus Walleij73855002012-10-16 20:15:02 +0200340 &em_gio_irq_domain_ops, p);
Axel Lin16310812012-10-31 17:03:33 +0800341 if (!p->irq_domain) {
342 ret = -ENXIO;
Magnus Damma07e1032012-05-17 15:22:23 +0900343 dev_err(&pdev->dev, "cannot initialize irq domain\n");
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900344 goto err0;
Magnus Damma07e1032012-05-17 15:22:23 +0900345 }
346
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900347 if (devm_request_irq(&pdev->dev, irq[0]->start,
348 em_gio_irq_handler, 0, name, p)) {
Magnus Damma07e1032012-05-17 15:22:23 +0900349 dev_err(&pdev->dev, "failed to request low IRQ\n");
350 ret = -ENOENT;
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900351 goto err1;
Magnus Damma07e1032012-05-17 15:22:23 +0900352 }
353
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900354 if (devm_request_irq(&pdev->dev, irq[1]->start,
355 em_gio_irq_handler, 0, name, p)) {
Magnus Damma07e1032012-05-17 15:22:23 +0900356 dev_err(&pdev->dev, "failed to request high IRQ\n");
357 ret = -ENOENT;
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900358 goto err1;
Magnus Damma07e1032012-05-17 15:22:23 +0900359 }
360
Linus Walleij6219e7b2015-12-06 00:36:39 +0100361 ret = gpiochip_add_data(gpio_chip, p);
Magnus Damma07e1032012-05-17 15:22:23 +0900362 if (ret) {
363 dev_err(&pdev->dev, "failed to add GPIO controller\n");
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900364 goto err1;
Magnus Damma07e1032012-05-17 15:22:23 +0900365 }
Magnus Damm640efa02013-07-03 13:14:32 +0900366
Magnus Damma07e1032012-05-17 15:22:23 +0900367 return 0;
368
Magnus Damma07e1032012-05-17 15:22:23 +0900369err1:
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900370 irq_domain_remove(p->irq_domain);
Magnus Damma07e1032012-05-17 15:22:23 +0900371err0:
372 return ret;
373}
374
Bill Pemberton206210c2012-11-19 13:25:50 -0500375static int em_gio_remove(struct platform_device *pdev)
Magnus Damma07e1032012-05-17 15:22:23 +0900376{
377 struct em_gio_priv *p = platform_get_drvdata(pdev);
Magnus Damma07e1032012-05-17 15:22:23 +0900378
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200379 gpiochip_remove(&p->gpio_chip);
Magnus Damma07e1032012-05-17 15:22:23 +0900380
Axel Lin16310812012-10-31 17:03:33 +0800381 irq_domain_remove(p->irq_domain);
Magnus Damma07e1032012-05-17 15:22:23 +0900382 return 0;
383}
384
Magnus Damm753c5982013-02-26 22:26:23 +0900385static const struct of_device_id em_gio_dt_ids[] = {
386 { .compatible = "renesas,em-gio", },
387 {},
388};
389MODULE_DEVICE_TABLE(of, em_gio_dt_ids);
390
Magnus Damma07e1032012-05-17 15:22:23 +0900391static struct platform_driver em_gio_device_driver = {
392 .probe = em_gio_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -0500393 .remove = em_gio_remove,
Magnus Damma07e1032012-05-17 15:22:23 +0900394 .driver = {
395 .name = "em_gio",
Magnus Damm753c5982013-02-26 22:26:23 +0900396 .of_match_table = em_gio_dt_ids,
Magnus Damma07e1032012-05-17 15:22:23 +0900397 }
398};
399
Magnus Damm753c5982013-02-26 22:26:23 +0900400static int __init em_gio_init(void)
401{
402 return platform_driver_register(&em_gio_device_driver);
403}
404postcore_initcall(em_gio_init);
405
406static void __exit em_gio_exit(void)
407{
408 platform_driver_unregister(&em_gio_device_driver);
409}
410module_exit(em_gio_exit);
Magnus Damma07e1032012-05-17 15:22:23 +0900411
412MODULE_AUTHOR("Magnus Damm");
413MODULE_DESCRIPTION("Renesas Emma Mobile GIO Driver");
414MODULE_LICENSE("GPL v2");