blob: 982e699a5b816582034719737b2892455ce538f0 [file] [log] [blame]
Magnus Damma07e1032012-05-17 15:22:23 +09001/*
2 * Emma Mobile GPIO Support - GIO
3 *
4 * Copyright (C) 2012 Magnus Damm
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/init.h>
21#include <linux/platform_device.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
24#include <linux/ioport.h>
25#include <linux/io.h>
26#include <linux/irq.h>
27#include <linux/irqdomain.h>
28#include <linux/bitops.h>
29#include <linux/err.h>
Linus Walleij7275cb72018-02-09 00:45:50 +010030#include <linux/gpio/driver.h>
Magnus Damma07e1032012-05-17 15:22:23 +090031#include <linux/slab.h>
32#include <linux/module.h>
Magnus Damm640efa02013-07-03 13:14:32 +090033#include <linux/pinctrl/consumer.h>
Magnus Damma07e1032012-05-17 15:22:23 +090034
35struct em_gio_priv {
36 void __iomem *base0;
37 void __iomem *base1;
Magnus Damma07e1032012-05-17 15:22:23 +090038 spinlock_t sense_lock;
39 struct platform_device *pdev;
40 struct gpio_chip gpio_chip;
41 struct irq_chip irq_chip;
42 struct irq_domain *irq_domain;
43};
44
45#define GIO_E1 0x00
46#define GIO_E0 0x04
47#define GIO_EM 0x04
48#define GIO_OL 0x08
49#define GIO_OH 0x0c
50#define GIO_I 0x10
51#define GIO_IIA 0x14
52#define GIO_IEN 0x18
53#define GIO_IDS 0x1c
54#define GIO_IIM 0x1c
55#define GIO_RAW 0x20
56#define GIO_MST 0x24
57#define GIO_IIR 0x28
58
59#define GIO_IDT0 0x40
60#define GIO_IDT1 0x44
61#define GIO_IDT2 0x48
62#define GIO_IDT3 0x4c
63#define GIO_RAWBL 0x50
64#define GIO_RAWBH 0x54
65#define GIO_IRBL 0x58
66#define GIO_IRBH 0x5c
67
68#define GIO_IDT(n) (GIO_IDT0 + ((n) * 4))
69
70static inline unsigned long em_gio_read(struct em_gio_priv *p, int offs)
71{
72 if (offs < GIO_IDT0)
73 return ioread32(p->base0 + offs);
74 else
75 return ioread32(p->base1 + (offs - GIO_IDT0));
76}
77
78static inline void em_gio_write(struct em_gio_priv *p, int offs,
79 unsigned long value)
80{
81 if (offs < GIO_IDT0)
82 iowrite32(value, p->base0 + offs);
83 else
84 iowrite32(value, p->base1 + (offs - GIO_IDT0));
85}
86
Magnus Damma07e1032012-05-17 15:22:23 +090087static void em_gio_irq_disable(struct irq_data *d)
88{
Axel Lina9f77c92012-09-04 21:58:33 +080089 struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
Magnus Damma07e1032012-05-17 15:22:23 +090090
91 em_gio_write(p, GIO_IDS, BIT(irqd_to_hwirq(d)));
92}
93
94static void em_gio_irq_enable(struct irq_data *d)
95{
Axel Lina9f77c92012-09-04 21:58:33 +080096 struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
Magnus Damma07e1032012-05-17 15:22:23 +090097
98 em_gio_write(p, GIO_IEN, BIT(irqd_to_hwirq(d)));
99}
100
Linus Walleij57ef0422014-03-14 18:16:20 +0100101static int em_gio_irq_reqres(struct irq_data *d)
Linus Walleij0dc61622013-11-20 10:16:54 +0100102{
103 struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
Andy Shevchenko41d69082018-07-30 15:38:34 +0300104 int ret;
Linus Walleij0dc61622013-11-20 10:16:54 +0100105
Andy Shevchenko41d69082018-07-30 15:38:34 +0300106 ret = gpiochip_lock_as_irq(&p->gpio_chip, irqd_to_hwirq(d));
107 if (ret) {
Linus Walleij58383c782015-11-04 09:56:26 +0100108 dev_err(p->gpio_chip.parent,
Linus Walleij0dc61622013-11-20 10:16:54 +0100109 "unable to lock HW IRQ %lu for IRQ\n",
110 irqd_to_hwirq(d));
Andy Shevchenko41d69082018-07-30 15:38:34 +0300111 return ret;
Linus Walleij57ef0422014-03-14 18:16:20 +0100112 }
Linus Walleij0dc61622013-11-20 10:16:54 +0100113 return 0;
114}
115
Linus Walleij57ef0422014-03-14 18:16:20 +0100116static void em_gio_irq_relres(struct irq_data *d)
Linus Walleij0dc61622013-11-20 10:16:54 +0100117{
118 struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
119
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900120 gpiochip_unlock_as_irq(&p->gpio_chip, irqd_to_hwirq(d));
Linus Walleij0dc61622013-11-20 10:16:54 +0100121}
122
123
Magnus Damma07e1032012-05-17 15:22:23 +0900124#define GIO_ASYNC(x) (x + 8)
125
126static unsigned char em_gio_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
127 [IRQ_TYPE_EDGE_RISING] = GIO_ASYNC(0x00),
128 [IRQ_TYPE_EDGE_FALLING] = GIO_ASYNC(0x01),
129 [IRQ_TYPE_LEVEL_HIGH] = GIO_ASYNC(0x02),
130 [IRQ_TYPE_LEVEL_LOW] = GIO_ASYNC(0x03),
131 [IRQ_TYPE_EDGE_BOTH] = GIO_ASYNC(0x04),
132};
133
134static int em_gio_irq_set_type(struct irq_data *d, unsigned int type)
135{
136 unsigned char value = em_gio_sense_table[type & IRQ_TYPE_SENSE_MASK];
Axel Lina9f77c92012-09-04 21:58:33 +0800137 struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
Magnus Damma07e1032012-05-17 15:22:23 +0900138 unsigned int reg, offset, shift;
139 unsigned long flags;
140 unsigned long tmp;
141
142 if (!value)
143 return -EINVAL;
144
145 offset = irqd_to_hwirq(d);
146
147 pr_debug("gio: sense irq = %d, mode = %d\n", offset, value);
148
149 /* 8 x 4 bit fields in 4 IDT registers */
150 reg = GIO_IDT(offset >> 3);
151 shift = (offset & 0x07) << 4;
152
153 spin_lock_irqsave(&p->sense_lock, flags);
154
155 /* disable the interrupt in IIA */
156 tmp = em_gio_read(p, GIO_IIA);
157 tmp &= ~BIT(offset);
158 em_gio_write(p, GIO_IIA, tmp);
159
160 /* change the sense setting in IDT */
161 tmp = em_gio_read(p, reg);
162 tmp &= ~(0xf << shift);
163 tmp |= value << shift;
164 em_gio_write(p, reg, tmp);
165
166 /* clear pending interrupts */
167 em_gio_write(p, GIO_IIR, BIT(offset));
168
169 /* enable the interrupt in IIA */
170 tmp = em_gio_read(p, GIO_IIA);
171 tmp |= BIT(offset);
172 em_gio_write(p, GIO_IIA, tmp);
173
174 spin_unlock_irqrestore(&p->sense_lock, flags);
175
176 return 0;
177}
178
179static irqreturn_t em_gio_irq_handler(int irq, void *dev_id)
180{
181 struct em_gio_priv *p = dev_id;
182 unsigned long pending;
183 unsigned int offset, irqs_handled = 0;
184
185 while ((pending = em_gio_read(p, GIO_MST))) {
186 offset = __ffs(pending);
187 em_gio_write(p, GIO_IIR, BIT(offset));
188 generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
189 irqs_handled++;
190 }
191
192 return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
193}
194
195static inline struct em_gio_priv *gpio_to_priv(struct gpio_chip *chip)
196{
Linus Walleij6219e7b2015-12-06 00:36:39 +0100197 return gpiochip_get_data(chip);
Magnus Damma07e1032012-05-17 15:22:23 +0900198}
199
200static int em_gio_direction_input(struct gpio_chip *chip, unsigned offset)
201{
202 em_gio_write(gpio_to_priv(chip), GIO_E0, BIT(offset));
203 return 0;
204}
205
206static int em_gio_get(struct gpio_chip *chip, unsigned offset)
207{
Linus Walleij8388f292015-12-21 10:46:18 +0100208 return !!(em_gio_read(gpio_to_priv(chip), GIO_I) & BIT(offset));
Magnus Damma07e1032012-05-17 15:22:23 +0900209}
210
211static void __em_gio_set(struct gpio_chip *chip, unsigned int reg,
212 unsigned shift, int value)
213{
214 /* upper 16 bits contains mask and lower 16 actual value */
215 em_gio_write(gpio_to_priv(chip), reg,
Javier Martinez Canillas5f077642014-04-27 02:00:47 +0200216 (BIT(shift + 16)) | (value << shift));
Magnus Damma07e1032012-05-17 15:22:23 +0900217}
218
219static void em_gio_set(struct gpio_chip *chip, unsigned offset, int value)
220{
221 /* output is split into two registers */
222 if (offset < 16)
223 __em_gio_set(chip, GIO_OL, offset, value);
224 else
225 __em_gio_set(chip, GIO_OH, offset - 16, value);
226}
227
228static int em_gio_direction_output(struct gpio_chip *chip, unsigned offset,
229 int value)
230{
231 /* write GPIO value to output before selecting output mode of pin */
232 em_gio_set(chip, offset, value);
233 em_gio_write(gpio_to_priv(chip), GIO_E1, BIT(offset));
234 return 0;
235}
236
237static int em_gio_to_irq(struct gpio_chip *chip, unsigned offset)
238{
Linus Walleij73855002012-10-16 20:15:02 +0200239 return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset);
Magnus Damma07e1032012-05-17 15:22:23 +0900240}
241
Magnus Damm640efa02013-07-03 13:14:32 +0900242static int em_gio_request(struct gpio_chip *chip, unsigned offset)
243{
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200244 return pinctrl_gpio_request(chip->base + offset);
Magnus Damm640efa02013-07-03 13:14:32 +0900245}
246
247static void em_gio_free(struct gpio_chip *chip, unsigned offset)
248{
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200249 pinctrl_gpio_free(chip->base + offset);
Magnus Damm640efa02013-07-03 13:14:32 +0900250
251 /* Set the GPIO as an input to ensure that the next GPIO request won't
252 * drive the GPIO pin as an output.
253 */
254 em_gio_direction_input(chip, offset);
255}
256
Linus Walleij2d61e3e2013-10-11 19:21:34 +0200257static int em_gio_irq_domain_map(struct irq_domain *h, unsigned int irq,
258 irq_hw_number_t hwirq)
Magnus Damma07e1032012-05-17 15:22:23 +0900259{
260 struct em_gio_priv *p = h->host_data;
261
Linus Walleij2d61e3e2013-10-11 19:21:34 +0200262 pr_debug("gio: map hw irq = %d, irq = %d\n", (int)hwirq, irq);
Magnus Damma07e1032012-05-17 15:22:23 +0900263
Linus Walleij2d61e3e2013-10-11 19:21:34 +0200264 irq_set_chip_data(irq, h->host_data);
265 irq_set_chip_and_handler(irq, &p->irq_chip, handle_level_irq);
Magnus Damma07e1032012-05-17 15:22:23 +0900266 return 0;
267}
268
Krzysztof Kozlowski0b354dc2015-04-27 21:54:07 +0900269static const struct irq_domain_ops em_gio_irq_domain_ops = {
Magnus Damma07e1032012-05-17 15:22:23 +0900270 .map = em_gio_irq_domain_map,
Magnus Damm753c5982013-02-26 22:26:23 +0900271 .xlate = irq_domain_xlate_twocell,
Magnus Damma07e1032012-05-17 15:22:23 +0900272};
273
Bill Pemberton38363092012-11-19 13:22:34 -0500274static int em_gio_probe(struct platform_device *pdev)
Magnus Damma07e1032012-05-17 15:22:23 +0900275{
Magnus Damma07e1032012-05-17 15:22:23 +0900276 struct em_gio_priv *p;
277 struct resource *io[2], *irq[2];
278 struct gpio_chip *gpio_chip;
279 struct irq_chip *irq_chip;
280 const char *name = dev_name(&pdev->dev);
Geert Uytterhoeven527b3972015-06-23 15:48:02 +0200281 unsigned int ngpios;
Magnus Damma07e1032012-05-17 15:22:23 +0900282 int ret;
283
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900284 p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
Magnus Damma07e1032012-05-17 15:22:23 +0900285 if (!p) {
Magnus Damma07e1032012-05-17 15:22:23 +0900286 ret = -ENOMEM;
287 goto err0;
288 }
289
290 p->pdev = pdev;
291 platform_set_drvdata(pdev, p);
292 spin_lock_init(&p->sense_lock);
293
294 io[0] = platform_get_resource(pdev, IORESOURCE_MEM, 0);
295 io[1] = platform_get_resource(pdev, IORESOURCE_MEM, 1);
296 irq[0] = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
297 irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
298
Magnus Damm753c5982013-02-26 22:26:23 +0900299 if (!io[0] || !io[1] || !irq[0] || !irq[1]) {
300 dev_err(&pdev->dev, "missing IRQ or IOMEM\n");
Magnus Damma07e1032012-05-17 15:22:23 +0900301 ret = -EINVAL;
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900302 goto err0;
Magnus Damma07e1032012-05-17 15:22:23 +0900303 }
304
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900305 p->base0 = devm_ioremap_nocache(&pdev->dev, io[0]->start,
306 resource_size(io[0]));
Magnus Damma07e1032012-05-17 15:22:23 +0900307 if (!p->base0) {
308 dev_err(&pdev->dev, "failed to remap low I/O memory\n");
309 ret = -ENXIO;
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900310 goto err0;
Magnus Damma07e1032012-05-17 15:22:23 +0900311 }
312
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900313 p->base1 = devm_ioremap_nocache(&pdev->dev, io[1]->start,
314 resource_size(io[1]));
Magnus Damma07e1032012-05-17 15:22:23 +0900315 if (!p->base1) {
316 dev_err(&pdev->dev, "failed to remap high I/O memory\n");
317 ret = -ENXIO;
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900318 goto err0;
Magnus Damma07e1032012-05-17 15:22:23 +0900319 }
320
Geert Uytterhoeven527b3972015-06-23 15:48:02 +0200321 if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
322 dev_err(&pdev->dev, "Missing ngpios OF property\n");
323 ret = -EINVAL;
324 goto err0;
Magnus Damm753c5982013-02-26 22:26:23 +0900325 }
326
Magnus Damma07e1032012-05-17 15:22:23 +0900327 gpio_chip = &p->gpio_chip;
Ian Moltonb5927852013-09-02 16:44:55 +0100328 gpio_chip->of_node = pdev->dev.of_node;
Magnus Damma07e1032012-05-17 15:22:23 +0900329 gpio_chip->direction_input = em_gio_direction_input;
330 gpio_chip->get = em_gio_get;
331 gpio_chip->direction_output = em_gio_direction_output;
332 gpio_chip->set = em_gio_set;
333 gpio_chip->to_irq = em_gio_to_irq;
Magnus Damm640efa02013-07-03 13:14:32 +0900334 gpio_chip->request = em_gio_request;
335 gpio_chip->free = em_gio_free;
Magnus Damma07e1032012-05-17 15:22:23 +0900336 gpio_chip->label = name;
Linus Walleij58383c782015-11-04 09:56:26 +0100337 gpio_chip->parent = &pdev->dev;
Magnus Damma07e1032012-05-17 15:22:23 +0900338 gpio_chip->owner = THIS_MODULE;
Geert Uytterhoeven527b3972015-06-23 15:48:02 +0200339 gpio_chip->base = -1;
340 gpio_chip->ngpio = ngpios;
Magnus Damma07e1032012-05-17 15:22:23 +0900341
342 irq_chip = &p->irq_chip;
343 irq_chip->name = name;
344 irq_chip->irq_mask = em_gio_irq_disable;
345 irq_chip->irq_unmask = em_gio_irq_enable;
Magnus Damma07e1032012-05-17 15:22:23 +0900346 irq_chip->irq_set_type = em_gio_irq_set_type;
Linus Walleij57ef0422014-03-14 18:16:20 +0100347 irq_chip->irq_request_resources = em_gio_irq_reqres;
348 irq_chip->irq_release_resources = em_gio_irq_relres;
Magnus Damm03621b62013-11-20 09:23:44 +0900349 irq_chip->flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MASK_ON_SUSPEND;
Magnus Damma07e1032012-05-17 15:22:23 +0900350
Geert Uytterhoeven527b3972015-06-23 15:48:02 +0200351 p->irq_domain = irq_domain_add_simple(pdev->dev.of_node, ngpios, 0,
Linus Walleij73855002012-10-16 20:15:02 +0200352 &em_gio_irq_domain_ops, p);
Axel Lin16310812012-10-31 17:03:33 +0800353 if (!p->irq_domain) {
354 ret = -ENXIO;
Magnus Damma07e1032012-05-17 15:22:23 +0900355 dev_err(&pdev->dev, "cannot initialize irq domain\n");
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900356 goto err0;
Magnus Damma07e1032012-05-17 15:22:23 +0900357 }
358
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900359 if (devm_request_irq(&pdev->dev, irq[0]->start,
360 em_gio_irq_handler, 0, name, p)) {
Magnus Damma07e1032012-05-17 15:22:23 +0900361 dev_err(&pdev->dev, "failed to request low IRQ\n");
362 ret = -ENOENT;
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900363 goto err1;
Magnus Damma07e1032012-05-17 15:22:23 +0900364 }
365
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900366 if (devm_request_irq(&pdev->dev, irq[1]->start,
367 em_gio_irq_handler, 0, name, p)) {
Magnus Damma07e1032012-05-17 15:22:23 +0900368 dev_err(&pdev->dev, "failed to request high IRQ\n");
369 ret = -ENOENT;
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900370 goto err1;
Magnus Damma07e1032012-05-17 15:22:23 +0900371 }
372
Linus Walleij6219e7b2015-12-06 00:36:39 +0100373 ret = gpiochip_add_data(gpio_chip, p);
Magnus Damma07e1032012-05-17 15:22:23 +0900374 if (ret) {
375 dev_err(&pdev->dev, "failed to add GPIO controller\n");
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900376 goto err1;
Magnus Damma07e1032012-05-17 15:22:23 +0900377 }
Magnus Damm640efa02013-07-03 13:14:32 +0900378
Magnus Damma07e1032012-05-17 15:22:23 +0900379 return 0;
380
Magnus Damma07e1032012-05-17 15:22:23 +0900381err1:
Magnus Damm1cfe6f82013-03-13 20:06:30 +0900382 irq_domain_remove(p->irq_domain);
Magnus Damma07e1032012-05-17 15:22:23 +0900383err0:
384 return ret;
385}
386
Bill Pemberton206210c2012-11-19 13:25:50 -0500387static int em_gio_remove(struct platform_device *pdev)
Magnus Damma07e1032012-05-17 15:22:23 +0900388{
389 struct em_gio_priv *p = platform_get_drvdata(pdev);
Magnus Damma07e1032012-05-17 15:22:23 +0900390
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200391 gpiochip_remove(&p->gpio_chip);
Magnus Damma07e1032012-05-17 15:22:23 +0900392
Axel Lin16310812012-10-31 17:03:33 +0800393 irq_domain_remove(p->irq_domain);
Magnus Damma07e1032012-05-17 15:22:23 +0900394 return 0;
395}
396
Magnus Damm753c5982013-02-26 22:26:23 +0900397static const struct of_device_id em_gio_dt_ids[] = {
398 { .compatible = "renesas,em-gio", },
399 {},
400};
401MODULE_DEVICE_TABLE(of, em_gio_dt_ids);
402
Magnus Damma07e1032012-05-17 15:22:23 +0900403static struct platform_driver em_gio_device_driver = {
404 .probe = em_gio_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -0500405 .remove = em_gio_remove,
Magnus Damma07e1032012-05-17 15:22:23 +0900406 .driver = {
407 .name = "em_gio",
Magnus Damm753c5982013-02-26 22:26:23 +0900408 .of_match_table = em_gio_dt_ids,
Magnus Damma07e1032012-05-17 15:22:23 +0900409 }
410};
411
Magnus Damm753c5982013-02-26 22:26:23 +0900412static int __init em_gio_init(void)
413{
414 return platform_driver_register(&em_gio_device_driver);
415}
416postcore_initcall(em_gio_init);
417
418static void __exit em_gio_exit(void)
419{
420 platform_driver_unregister(&em_gio_device_driver);
421}
422module_exit(em_gio_exit);
Magnus Damma07e1032012-05-17 15:22:23 +0900423
424MODULE_AUTHOR("Magnus Damm");
425MODULE_DESCRIPTION("Renesas Emma Mobile GIO Driver");
426MODULE_LICENSE("GPL v2");