2 * Emma Mobile GPIO Support - GIO
4 * Copyright (C) 2012 Magnus Damm
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
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.
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
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>
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>
38 unsigned int irq_base;
39 spinlock_t sense_lock;
40 struct platform_device *pdev;
41 struct gpio_chip gpio_chip;
42 struct irq_chip irq_chip;
43 struct irq_domain *irq_domain;
64 #define GIO_RAWBL 0x50
65 #define GIO_RAWBH 0x54
69 #define GIO_IDT(n) (GIO_IDT0 + ((n) * 4))
71 static inline unsigned long em_gio_read(struct em_gio_priv *p, int offs)
74 return ioread32(p->base0 + offs);
76 return ioread32(p->base1 + (offs - GIO_IDT0));
79 static inline void em_gio_write(struct em_gio_priv *p, int offs,
83 iowrite32(value, p->base0 + offs);
85 iowrite32(value, p->base1 + (offs - GIO_IDT0));
88 static inline struct em_gio_priv *irq_to_priv(struct irq_data *d)
90 struct irq_chip *chip = irq_data_get_irq_chip(d);
91 return container_of(chip, struct em_gio_priv, irq_chip);
94 static void em_gio_irq_disable(struct irq_data *d)
96 struct em_gio_priv *p = irq_to_priv(d);
98 em_gio_write(p, GIO_IDS, BIT(irqd_to_hwirq(d)));
101 static void em_gio_irq_enable(struct irq_data *d)
103 struct em_gio_priv *p = irq_to_priv(d);
105 em_gio_write(p, GIO_IEN, BIT(irqd_to_hwirq(d)));
108 #define GIO_ASYNC(x) (x + 8)
110 static unsigned char em_gio_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
111 [IRQ_TYPE_EDGE_RISING] = GIO_ASYNC(0x00),
112 [IRQ_TYPE_EDGE_FALLING] = GIO_ASYNC(0x01),
113 [IRQ_TYPE_LEVEL_HIGH] = GIO_ASYNC(0x02),
114 [IRQ_TYPE_LEVEL_LOW] = GIO_ASYNC(0x03),
115 [IRQ_TYPE_EDGE_BOTH] = GIO_ASYNC(0x04),
118 static int em_gio_irq_set_type(struct irq_data *d, unsigned int type)
120 unsigned char value = em_gio_sense_table[type & IRQ_TYPE_SENSE_MASK];
121 struct em_gio_priv *p = irq_to_priv(d);
122 unsigned int reg, offset, shift;
129 offset = irqd_to_hwirq(d);
131 pr_debug("gio: sense irq = %d, mode = %d\n", offset, value);
133 /* 8 x 4 bit fields in 4 IDT registers */
134 reg = GIO_IDT(offset >> 3);
135 shift = (offset & 0x07) << 4;
137 spin_lock_irqsave(&p->sense_lock, flags);
139 /* disable the interrupt in IIA */
140 tmp = em_gio_read(p, GIO_IIA);
142 em_gio_write(p, GIO_IIA, tmp);
144 /* change the sense setting in IDT */
145 tmp = em_gio_read(p, reg);
146 tmp &= ~(0xf << shift);
147 tmp |= value << shift;
148 em_gio_write(p, reg, tmp);
150 /* clear pending interrupts */
151 em_gio_write(p, GIO_IIR, BIT(offset));
153 /* enable the interrupt in IIA */
154 tmp = em_gio_read(p, GIO_IIA);
156 em_gio_write(p, GIO_IIA, tmp);
158 spin_unlock_irqrestore(&p->sense_lock, flags);
163 static irqreturn_t em_gio_irq_handler(int irq, void *dev_id)
165 struct em_gio_priv *p = dev_id;
166 unsigned long pending;
167 unsigned int offset, irqs_handled = 0;
169 while ((pending = em_gio_read(p, GIO_MST))) {
170 offset = __ffs(pending);
171 em_gio_write(p, GIO_IIR, BIT(offset));
172 generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
176 return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
179 static inline struct em_gio_priv *gpio_to_priv(struct gpio_chip *chip)
181 return container_of(chip, struct em_gio_priv, gpio_chip);
184 static int em_gio_direction_input(struct gpio_chip *chip, unsigned offset)
186 em_gio_write(gpio_to_priv(chip), GIO_E0, BIT(offset));
190 static int em_gio_get(struct gpio_chip *chip, unsigned offset)
192 return (int)(em_gio_read(gpio_to_priv(chip), GIO_I) & BIT(offset));
195 static void __em_gio_set(struct gpio_chip *chip, unsigned int reg,
196 unsigned shift, int value)
198 /* upper 16 bits contains mask and lower 16 actual value */
199 em_gio_write(gpio_to_priv(chip), reg,
200 (1 << (shift + 16)) | (value << shift));
203 static void em_gio_set(struct gpio_chip *chip, unsigned offset, int value)
205 /* output is split into two registers */
207 __em_gio_set(chip, GIO_OL, offset, value);
209 __em_gio_set(chip, GIO_OH, offset - 16, value);
212 static int em_gio_direction_output(struct gpio_chip *chip, unsigned offset,
215 /* write GPIO value to output before selecting output mode of pin */
216 em_gio_set(chip, offset, value);
217 em_gio_write(gpio_to_priv(chip), GIO_E1, BIT(offset));
221 static int em_gio_to_irq(struct gpio_chip *chip, unsigned offset)
223 return irq_find_mapping(gpio_to_priv(chip)->irq_domain, offset);
226 static int em_gio_irq_domain_map(struct irq_domain *h, unsigned int virq,
229 struct em_gio_priv *p = h->host_data;
231 pr_debug("gio: map hw irq = %d, virq = %d\n", (int)hw, virq);
233 irq_set_chip_data(virq, h->host_data);
234 irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq);
235 set_irq_flags(virq, IRQF_VALID); /* kill me now */
239 static struct irq_domain_ops em_gio_irq_domain_ops = {
240 .map = em_gio_irq_domain_map,
243 static int __devinit em_gio_irq_domain_init(struct em_gio_priv *p)
245 struct platform_device *pdev = p->pdev;
246 struct gpio_em_config *pdata = pdev->dev.platform_data;
248 p->irq_base = irq_alloc_descs(pdata->irq_base, 0,
249 pdata->number_of_pins, numa_node_id());
250 if (IS_ERR_VALUE(p->irq_base)) {
251 dev_err(&pdev->dev, "cannot get irq_desc\n");
254 pr_debug("gio: hw base = %d, nr = %d, sw base = %d\n",
255 pdata->gpio_base, pdata->number_of_pins, p->irq_base);
257 p->irq_domain = irq_domain_add_legacy(pdev->dev.of_node,
258 pdata->number_of_pins,
260 &em_gio_irq_domain_ops, p);
261 if (!p->irq_domain) {
262 irq_free_descs(p->irq_base, pdata->number_of_pins);
269 static void __devexit em_gio_irq_domain_cleanup(struct em_gio_priv *p)
271 struct gpio_em_config *pdata = p->pdev->dev.platform_data;
273 irq_free_descs(p->irq_base, pdata->number_of_pins);
274 /* FIXME: irq domain wants to be freed! */
277 static int __devinit em_gio_probe(struct platform_device *pdev)
279 struct gpio_em_config *pdata = pdev->dev.platform_data;
280 struct em_gio_priv *p;
281 struct resource *io[2], *irq[2];
282 struct gpio_chip *gpio_chip;
283 struct irq_chip *irq_chip;
284 const char *name = dev_name(&pdev->dev);
287 p = kzalloc(sizeof(*p), GFP_KERNEL);
289 dev_err(&pdev->dev, "failed to allocate driver data\n");
295 platform_set_drvdata(pdev, p);
296 spin_lock_init(&p->sense_lock);
298 io[0] = platform_get_resource(pdev, IORESOURCE_MEM, 0);
299 io[1] = platform_get_resource(pdev, IORESOURCE_MEM, 1);
300 irq[0] = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
301 irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
303 if (!io[0] || !io[1] || !irq[0] || !irq[1] || !pdata) {
304 dev_err(&pdev->dev, "missing IRQ, IOMEM or configuration\n");
309 p->base0 = ioremap_nocache(io[0]->start, resource_size(io[0]));
311 dev_err(&pdev->dev, "failed to remap low I/O memory\n");
316 p->base1 = ioremap_nocache(io[1]->start, resource_size(io[1]));
318 dev_err(&pdev->dev, "failed to remap high I/O memory\n");
323 gpio_chip = &p->gpio_chip;
324 gpio_chip->direction_input = em_gio_direction_input;
325 gpio_chip->get = em_gio_get;
326 gpio_chip->direction_output = em_gio_direction_output;
327 gpio_chip->set = em_gio_set;
328 gpio_chip->to_irq = em_gio_to_irq;
329 gpio_chip->label = name;
330 gpio_chip->owner = THIS_MODULE;
331 gpio_chip->base = pdata->gpio_base;
332 gpio_chip->ngpio = pdata->number_of_pins;
334 irq_chip = &p->irq_chip;
335 irq_chip->name = name;
336 irq_chip->irq_mask = em_gio_irq_disable;
337 irq_chip->irq_unmask = em_gio_irq_enable;
338 irq_chip->irq_enable = em_gio_irq_enable;
339 irq_chip->irq_disable = em_gio_irq_disable;
340 irq_chip->irq_set_type = em_gio_irq_set_type;
341 irq_chip->flags = IRQCHIP_SKIP_SET_WAKE;
343 ret = em_gio_irq_domain_init(p);
345 dev_err(&pdev->dev, "cannot initialize irq domain\n");
349 if (request_irq(irq[0]->start, em_gio_irq_handler, 0, name, p)) {
350 dev_err(&pdev->dev, "failed to request low IRQ\n");
355 if (request_irq(irq[1]->start, em_gio_irq_handler, 0, name, p)) {
356 dev_err(&pdev->dev, "failed to request high IRQ\n");
361 ret = gpiochip_add(gpio_chip);
363 dev_err(&pdev->dev, "failed to add GPIO controller\n");
369 free_irq(irq[1]->start, pdev);
371 free_irq(irq[0]->start, pdev);
373 em_gio_irq_domain_cleanup(p);
384 static int __devexit em_gio_remove(struct platform_device *pdev)
386 struct em_gio_priv *p = platform_get_drvdata(pdev);
387 struct resource *irq[2];
390 ret = gpiochip_remove(&p->gpio_chip);
394 irq[0] = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
395 irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
397 free_irq(irq[1]->start, pdev);
398 free_irq(irq[0]->start, pdev);
399 em_gio_irq_domain_cleanup(p);
406 static struct platform_driver em_gio_device_driver = {
407 .probe = em_gio_probe,
408 .remove = __devexit_p(em_gio_remove),
414 module_platform_driver(em_gio_device_driver);
416 MODULE_AUTHOR("Magnus Damm");
417 MODULE_DESCRIPTION("Renesas Emma Mobile GIO Driver");
418 MODULE_LICENSE("GPL v2");