blob: d0536973585703d7d60de13e4cac780c523aafda [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>
30#include <linux/gpio.h>
31#include <linux/slab.h>
32#include <linux/module.h>
33#include <linux/platform_data/gpio-em.h>
34
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
101#define GIO_ASYNC(x) (x + 8)
102
103static unsigned char em_gio_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
104 [IRQ_TYPE_EDGE_RISING] = GIO_ASYNC(0x00),
105 [IRQ_TYPE_EDGE_FALLING] = GIO_ASYNC(0x01),
106 [IRQ_TYPE_LEVEL_HIGH] = GIO_ASYNC(0x02),
107 [IRQ_TYPE_LEVEL_LOW] = GIO_ASYNC(0x03),
108 [IRQ_TYPE_EDGE_BOTH] = GIO_ASYNC(0x04),
109};
110
111static int em_gio_irq_set_type(struct irq_data *d, unsigned int type)
112{
113 unsigned char value = em_gio_sense_table[type & IRQ_TYPE_SENSE_MASK];
Axel Lina9f77c92012-09-04 21:58:33 +0800114 struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
Magnus Damma07e1032012-05-17 15:22:23 +0900115 unsigned int reg, offset, shift;
116 unsigned long flags;
117 unsigned long tmp;
118
119 if (!value)
120 return -EINVAL;
121
122 offset = irqd_to_hwirq(d);
123
124 pr_debug("gio: sense irq = %d, mode = %d\n", offset, value);
125
126 /* 8 x 4 bit fields in 4 IDT registers */
127 reg = GIO_IDT(offset >> 3);
128 shift = (offset & 0x07) << 4;
129
130 spin_lock_irqsave(&p->sense_lock, flags);
131
132 /* disable the interrupt in IIA */
133 tmp = em_gio_read(p, GIO_IIA);
134 tmp &= ~BIT(offset);
135 em_gio_write(p, GIO_IIA, tmp);
136
137 /* change the sense setting in IDT */
138 tmp = em_gio_read(p, reg);
139 tmp &= ~(0xf << shift);
140 tmp |= value << shift;
141 em_gio_write(p, reg, tmp);
142
143 /* clear pending interrupts */
144 em_gio_write(p, GIO_IIR, BIT(offset));
145
146 /* enable the interrupt in IIA */
147 tmp = em_gio_read(p, GIO_IIA);
148 tmp |= BIT(offset);
149 em_gio_write(p, GIO_IIA, tmp);
150
151 spin_unlock_irqrestore(&p->sense_lock, flags);
152
153 return 0;
154}
155
156static irqreturn_t em_gio_irq_handler(int irq, void *dev_id)
157{
158 struct em_gio_priv *p = dev_id;
159 unsigned long pending;
160 unsigned int offset, irqs_handled = 0;
161
162 while ((pending = em_gio_read(p, GIO_MST))) {
163 offset = __ffs(pending);
164 em_gio_write(p, GIO_IIR, BIT(offset));
165 generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
166 irqs_handled++;
167 }
168
169 return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
170}
171
172static inline struct em_gio_priv *gpio_to_priv(struct gpio_chip *chip)
173{
174 return container_of(chip, struct em_gio_priv, gpio_chip);
175}
176
177static int em_gio_direction_input(struct gpio_chip *chip, unsigned offset)
178{
179 em_gio_write(gpio_to_priv(chip), GIO_E0, BIT(offset));
180 return 0;
181}
182
183static int em_gio_get(struct gpio_chip *chip, unsigned offset)
184{
185 return (int)(em_gio_read(gpio_to_priv(chip), GIO_I) & BIT(offset));
186}
187
188static void __em_gio_set(struct gpio_chip *chip, unsigned int reg,
189 unsigned shift, int value)
190{
191 /* upper 16 bits contains mask and lower 16 actual value */
192 em_gio_write(gpio_to_priv(chip), reg,
193 (1 << (shift + 16)) | (value << shift));
194}
195
196static void em_gio_set(struct gpio_chip *chip, unsigned offset, int value)
197{
198 /* output is split into two registers */
199 if (offset < 16)
200 __em_gio_set(chip, GIO_OL, offset, value);
201 else
202 __em_gio_set(chip, GIO_OH, offset - 16, value);
203}
204
205static int em_gio_direction_output(struct gpio_chip *chip, unsigned offset,
206 int value)
207{
208 /* write GPIO value to output before selecting output mode of pin */
209 em_gio_set(chip, offset, value);
210 em_gio_write(gpio_to_priv(chip), GIO_E1, BIT(offset));
211 return 0;
212}
213
214static int em_gio_to_irq(struct gpio_chip *chip, unsigned offset)
215{
Linus Walleij73855002012-10-16 20:15:02 +0200216 return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset);
Magnus Damma07e1032012-05-17 15:22:23 +0900217}
218
219static int em_gio_irq_domain_map(struct irq_domain *h, unsigned int virq,
220 irq_hw_number_t hw)
221{
222 struct em_gio_priv *p = h->host_data;
223
224 pr_debug("gio: map hw irq = %d, virq = %d\n", (int)hw, virq);
225
226 irq_set_chip_data(virq, h->host_data);
227 irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq);
228 set_irq_flags(virq, IRQF_VALID); /* kill me now */
229 return 0;
230}
231
232static struct irq_domain_ops em_gio_irq_domain_ops = {
233 .map = em_gio_irq_domain_map,
Magnus Damm753c5982013-02-26 22:26:23 +0900234 .xlate = irq_domain_xlate_twocell,
Magnus Damma07e1032012-05-17 15:22:23 +0900235};
236
Bill Pemberton38363092012-11-19 13:22:34 -0500237static int em_gio_probe(struct platform_device *pdev)
Magnus Damma07e1032012-05-17 15:22:23 +0900238{
Magnus Damm753c5982013-02-26 22:26:23 +0900239 struct gpio_em_config pdata_dt;
Magnus Damma07e1032012-05-17 15:22:23 +0900240 struct gpio_em_config *pdata = pdev->dev.platform_data;
241 struct em_gio_priv *p;
242 struct resource *io[2], *irq[2];
243 struct gpio_chip *gpio_chip;
244 struct irq_chip *irq_chip;
245 const char *name = dev_name(&pdev->dev);
246 int ret;
247
248 p = kzalloc(sizeof(*p), GFP_KERNEL);
249 if (!p) {
250 dev_err(&pdev->dev, "failed to allocate driver data\n");
251 ret = -ENOMEM;
252 goto err0;
253 }
254
255 p->pdev = pdev;
256 platform_set_drvdata(pdev, p);
257 spin_lock_init(&p->sense_lock);
258
259 io[0] = platform_get_resource(pdev, IORESOURCE_MEM, 0);
260 io[1] = platform_get_resource(pdev, IORESOURCE_MEM, 1);
261 irq[0] = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
262 irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
263
Magnus Damm753c5982013-02-26 22:26:23 +0900264 if (!io[0] || !io[1] || !irq[0] || !irq[1]) {
265 dev_err(&pdev->dev, "missing IRQ or IOMEM\n");
Magnus Damma07e1032012-05-17 15:22:23 +0900266 ret = -EINVAL;
267 goto err1;
268 }
269
270 p->base0 = ioremap_nocache(io[0]->start, resource_size(io[0]));
271 if (!p->base0) {
272 dev_err(&pdev->dev, "failed to remap low I/O memory\n");
273 ret = -ENXIO;
274 goto err1;
275 }
276
277 p->base1 = ioremap_nocache(io[1]->start, resource_size(io[1]));
278 if (!p->base1) {
279 dev_err(&pdev->dev, "failed to remap high I/O memory\n");
280 ret = -ENXIO;
281 goto err2;
282 }
283
Magnus Damm753c5982013-02-26 22:26:23 +0900284 if (!pdata) {
285 memset(&pdata_dt, 0, sizeof(pdata_dt));
286 pdata = &pdata_dt;
287
288 if (of_property_read_u32(pdev->dev.of_node, "ngpios",
289 &pdata->number_of_pins)) {
290 dev_err(&pdev->dev, "Missing ngpios OF property\n");
291 ret = -EINVAL;
292 goto err3;
293 }
294
295 ret = of_alias_get_id(pdev->dev.of_node, "gpio");
296 if (ret < 0) {
297 dev_err(&pdev->dev, "Couldn't get OF id\n");
298 goto err3;
299 }
300 pdata->gpio_base = ret * 32; /* 32 GPIOs per instance */
301 }
302
Magnus Damma07e1032012-05-17 15:22:23 +0900303 gpio_chip = &p->gpio_chip;
304 gpio_chip->direction_input = em_gio_direction_input;
305 gpio_chip->get = em_gio_get;
306 gpio_chip->direction_output = em_gio_direction_output;
307 gpio_chip->set = em_gio_set;
308 gpio_chip->to_irq = em_gio_to_irq;
309 gpio_chip->label = name;
310 gpio_chip->owner = THIS_MODULE;
311 gpio_chip->base = pdata->gpio_base;
312 gpio_chip->ngpio = pdata->number_of_pins;
313
314 irq_chip = &p->irq_chip;
315 irq_chip->name = name;
316 irq_chip->irq_mask = em_gio_irq_disable;
317 irq_chip->irq_unmask = em_gio_irq_enable;
318 irq_chip->irq_enable = em_gio_irq_enable;
319 irq_chip->irq_disable = em_gio_irq_disable;
320 irq_chip->irq_set_type = em_gio_irq_set_type;
321 irq_chip->flags = IRQCHIP_SKIP_SET_WAKE;
322
Magnus Dammc7886b12013-02-13 00:56:13 +0900323 p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
Linus Walleij73855002012-10-16 20:15:02 +0200324 pdata->number_of_pins,
Magnus Dammc7886b12013-02-13 00:56:13 +0900325 pdata->irq_base,
Linus Walleij73855002012-10-16 20:15:02 +0200326 &em_gio_irq_domain_ops, p);
Axel Lin16310812012-10-31 17:03:33 +0800327 if (!p->irq_domain) {
328 ret = -ENXIO;
Magnus Damma07e1032012-05-17 15:22:23 +0900329 dev_err(&pdev->dev, "cannot initialize irq domain\n");
330 goto err3;
331 }
332
333 if (request_irq(irq[0]->start, em_gio_irq_handler, 0, name, p)) {
334 dev_err(&pdev->dev, "failed to request low IRQ\n");
335 ret = -ENOENT;
336 goto err4;
337 }
338
339 if (request_irq(irq[1]->start, em_gio_irq_handler, 0, name, p)) {
340 dev_err(&pdev->dev, "failed to request high IRQ\n");
341 ret = -ENOENT;
342 goto err5;
343 }
344
345 ret = gpiochip_add(gpio_chip);
346 if (ret) {
347 dev_err(&pdev->dev, "failed to add GPIO controller\n");
348 goto err6;
349 }
350 return 0;
351
352err6:
353 free_irq(irq[1]->start, pdev);
354err5:
355 free_irq(irq[0]->start, pdev);
356err4:
Linus Walleij73855002012-10-16 20:15:02 +0200357 irq_domain_remove(p->irq_domain);
Magnus Damma07e1032012-05-17 15:22:23 +0900358err3:
359 iounmap(p->base1);
360err2:
361 iounmap(p->base0);
362err1:
363 kfree(p);
364err0:
365 return ret;
366}
367
Bill Pemberton206210c2012-11-19 13:25:50 -0500368static int em_gio_remove(struct platform_device *pdev)
Magnus Damma07e1032012-05-17 15:22:23 +0900369{
370 struct em_gio_priv *p = platform_get_drvdata(pdev);
371 struct resource *irq[2];
372 int ret;
373
374 ret = gpiochip_remove(&p->gpio_chip);
375 if (ret)
376 return ret;
377
378 irq[0] = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
379 irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
380
381 free_irq(irq[1]->start, pdev);
382 free_irq(irq[0]->start, pdev);
Axel Lin16310812012-10-31 17:03:33 +0800383 irq_domain_remove(p->irq_domain);
Magnus Damma07e1032012-05-17 15:22:23 +0900384 iounmap(p->base1);
385 iounmap(p->base0);
386 kfree(p);
387 return 0;
388}
389
Magnus Damm753c5982013-02-26 22:26:23 +0900390static const struct of_device_id em_gio_dt_ids[] = {
391 { .compatible = "renesas,em-gio", },
392 {},
393};
394MODULE_DEVICE_TABLE(of, em_gio_dt_ids);
395
Magnus Damma07e1032012-05-17 15:22:23 +0900396static struct platform_driver em_gio_device_driver = {
397 .probe = em_gio_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -0500398 .remove = em_gio_remove,
Magnus Damma07e1032012-05-17 15:22:23 +0900399 .driver = {
400 .name = "em_gio",
Magnus Damm753c5982013-02-26 22:26:23 +0900401 .of_match_table = em_gio_dt_ids,
402 .owner = THIS_MODULE,
Magnus Damma07e1032012-05-17 15:22:23 +0900403 }
404};
405
Magnus Damm753c5982013-02-26 22:26:23 +0900406static int __init em_gio_init(void)
407{
408 return platform_driver_register(&em_gio_device_driver);
409}
410postcore_initcall(em_gio_init);
411
412static void __exit em_gio_exit(void)
413{
414 platform_driver_unregister(&em_gio_device_driver);
415}
416module_exit(em_gio_exit);
Magnus Damma07e1032012-05-17 15:22:23 +0900417
418MODULE_AUTHOR("Magnus Damm");
419MODULE_DESCRIPTION("Renesas Emma Mobile GIO Driver");
420MODULE_LICENSE("GPL v2");