Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 1 | /* |
| 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> |
Magnus Damm | 640efa0 | 2013-07-03 13:14:32 +0900 | [diff] [blame] | 33 | #include <linux/pinctrl/consumer.h> |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 34 | #include <linux/platform_data/gpio-em.h> |
| 35 | |
| 36 | struct em_gio_priv { |
| 37 | void __iomem *base0; |
| 38 | void __iomem *base1; |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 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; |
| 44 | }; |
| 45 | |
| 46 | #define GIO_E1 0x00 |
| 47 | #define GIO_E0 0x04 |
| 48 | #define GIO_EM 0x04 |
| 49 | #define GIO_OL 0x08 |
| 50 | #define GIO_OH 0x0c |
| 51 | #define GIO_I 0x10 |
| 52 | #define GIO_IIA 0x14 |
| 53 | #define GIO_IEN 0x18 |
| 54 | #define GIO_IDS 0x1c |
| 55 | #define GIO_IIM 0x1c |
| 56 | #define GIO_RAW 0x20 |
| 57 | #define GIO_MST 0x24 |
| 58 | #define GIO_IIR 0x28 |
| 59 | |
| 60 | #define GIO_IDT0 0x40 |
| 61 | #define GIO_IDT1 0x44 |
| 62 | #define GIO_IDT2 0x48 |
| 63 | #define GIO_IDT3 0x4c |
| 64 | #define GIO_RAWBL 0x50 |
| 65 | #define GIO_RAWBH 0x54 |
| 66 | #define GIO_IRBL 0x58 |
| 67 | #define GIO_IRBH 0x5c |
| 68 | |
| 69 | #define GIO_IDT(n) (GIO_IDT0 + ((n) * 4)) |
| 70 | |
| 71 | static inline unsigned long em_gio_read(struct em_gio_priv *p, int offs) |
| 72 | { |
| 73 | if (offs < GIO_IDT0) |
| 74 | return ioread32(p->base0 + offs); |
| 75 | else |
| 76 | return ioread32(p->base1 + (offs - GIO_IDT0)); |
| 77 | } |
| 78 | |
| 79 | static inline void em_gio_write(struct em_gio_priv *p, int offs, |
| 80 | unsigned long value) |
| 81 | { |
| 82 | if (offs < GIO_IDT0) |
| 83 | iowrite32(value, p->base0 + offs); |
| 84 | else |
| 85 | iowrite32(value, p->base1 + (offs - GIO_IDT0)); |
| 86 | } |
| 87 | |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 88 | static void em_gio_irq_disable(struct irq_data *d) |
| 89 | { |
Axel Lin | a9f77c9 | 2012-09-04 21:58:33 +0800 | [diff] [blame] | 90 | struct em_gio_priv *p = irq_data_get_irq_chip_data(d); |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 91 | |
| 92 | em_gio_write(p, GIO_IDS, BIT(irqd_to_hwirq(d))); |
| 93 | } |
| 94 | |
| 95 | static void em_gio_irq_enable(struct irq_data *d) |
| 96 | { |
Axel Lin | a9f77c9 | 2012-09-04 21:58:33 +0800 | [diff] [blame] | 97 | struct em_gio_priv *p = irq_data_get_irq_chip_data(d); |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 98 | |
| 99 | em_gio_write(p, GIO_IEN, BIT(irqd_to_hwirq(d))); |
| 100 | } |
| 101 | |
Linus Walleij | 57ef042 | 2014-03-14 18:16:20 +0100 | [diff] [blame] | 102 | static int em_gio_irq_reqres(struct irq_data *d) |
Linus Walleij | 0dc6162 | 2013-11-20 10:16:54 +0100 | [diff] [blame] | 103 | { |
| 104 | struct em_gio_priv *p = irq_data_get_irq_chip_data(d); |
| 105 | |
Linus Walleij | 57ef042 | 2014-03-14 18:16:20 +0100 | [diff] [blame] | 106 | if (gpio_lock_as_irq(&p->gpio_chip, irqd_to_hwirq(d))) { |
Linus Walleij | 0dc6162 | 2013-11-20 10:16:54 +0100 | [diff] [blame] | 107 | dev_err(p->gpio_chip.dev, |
| 108 | "unable to lock HW IRQ %lu for IRQ\n", |
| 109 | irqd_to_hwirq(d)); |
Linus Walleij | 57ef042 | 2014-03-14 18:16:20 +0100 | [diff] [blame] | 110 | return -EINVAL; |
| 111 | } |
Linus Walleij | 0dc6162 | 2013-11-20 10:16:54 +0100 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
Linus Walleij | 57ef042 | 2014-03-14 18:16:20 +0100 | [diff] [blame] | 115 | static void em_gio_irq_relres(struct irq_data *d) |
Linus Walleij | 0dc6162 | 2013-11-20 10:16:54 +0100 | [diff] [blame] | 116 | { |
| 117 | struct em_gio_priv *p = irq_data_get_irq_chip_data(d); |
| 118 | |
Linus Walleij | 0dc6162 | 2013-11-20 10:16:54 +0100 | [diff] [blame] | 119 | gpio_unlock_as_irq(&p->gpio_chip, irqd_to_hwirq(d)); |
| 120 | } |
| 121 | |
| 122 | |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 123 | #define GIO_ASYNC(x) (x + 8) |
| 124 | |
| 125 | static unsigned char em_gio_sense_table[IRQ_TYPE_SENSE_MASK + 1] = { |
| 126 | [IRQ_TYPE_EDGE_RISING] = GIO_ASYNC(0x00), |
| 127 | [IRQ_TYPE_EDGE_FALLING] = GIO_ASYNC(0x01), |
| 128 | [IRQ_TYPE_LEVEL_HIGH] = GIO_ASYNC(0x02), |
| 129 | [IRQ_TYPE_LEVEL_LOW] = GIO_ASYNC(0x03), |
| 130 | [IRQ_TYPE_EDGE_BOTH] = GIO_ASYNC(0x04), |
| 131 | }; |
| 132 | |
| 133 | static int em_gio_irq_set_type(struct irq_data *d, unsigned int type) |
| 134 | { |
| 135 | unsigned char value = em_gio_sense_table[type & IRQ_TYPE_SENSE_MASK]; |
Axel Lin | a9f77c9 | 2012-09-04 21:58:33 +0800 | [diff] [blame] | 136 | struct em_gio_priv *p = irq_data_get_irq_chip_data(d); |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 137 | unsigned int reg, offset, shift; |
| 138 | unsigned long flags; |
| 139 | unsigned long tmp; |
| 140 | |
| 141 | if (!value) |
| 142 | return -EINVAL; |
| 143 | |
| 144 | offset = irqd_to_hwirq(d); |
| 145 | |
| 146 | pr_debug("gio: sense irq = %d, mode = %d\n", offset, value); |
| 147 | |
| 148 | /* 8 x 4 bit fields in 4 IDT registers */ |
| 149 | reg = GIO_IDT(offset >> 3); |
| 150 | shift = (offset & 0x07) << 4; |
| 151 | |
| 152 | spin_lock_irqsave(&p->sense_lock, flags); |
| 153 | |
| 154 | /* disable the interrupt in IIA */ |
| 155 | tmp = em_gio_read(p, GIO_IIA); |
| 156 | tmp &= ~BIT(offset); |
| 157 | em_gio_write(p, GIO_IIA, tmp); |
| 158 | |
| 159 | /* change the sense setting in IDT */ |
| 160 | tmp = em_gio_read(p, reg); |
| 161 | tmp &= ~(0xf << shift); |
| 162 | tmp |= value << shift; |
| 163 | em_gio_write(p, reg, tmp); |
| 164 | |
| 165 | /* clear pending interrupts */ |
| 166 | em_gio_write(p, GIO_IIR, BIT(offset)); |
| 167 | |
| 168 | /* enable the interrupt in IIA */ |
| 169 | tmp = em_gio_read(p, GIO_IIA); |
| 170 | tmp |= BIT(offset); |
| 171 | em_gio_write(p, GIO_IIA, tmp); |
| 172 | |
| 173 | spin_unlock_irqrestore(&p->sense_lock, flags); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static irqreturn_t em_gio_irq_handler(int irq, void *dev_id) |
| 179 | { |
| 180 | struct em_gio_priv *p = dev_id; |
| 181 | unsigned long pending; |
| 182 | unsigned int offset, irqs_handled = 0; |
| 183 | |
| 184 | while ((pending = em_gio_read(p, GIO_MST))) { |
| 185 | offset = __ffs(pending); |
| 186 | em_gio_write(p, GIO_IIR, BIT(offset)); |
| 187 | generic_handle_irq(irq_find_mapping(p->irq_domain, offset)); |
| 188 | irqs_handled++; |
| 189 | } |
| 190 | |
| 191 | return irqs_handled ? IRQ_HANDLED : IRQ_NONE; |
| 192 | } |
| 193 | |
| 194 | static inline struct em_gio_priv *gpio_to_priv(struct gpio_chip *chip) |
| 195 | { |
| 196 | return container_of(chip, struct em_gio_priv, gpio_chip); |
| 197 | } |
| 198 | |
| 199 | static int em_gio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 200 | { |
| 201 | em_gio_write(gpio_to_priv(chip), GIO_E0, BIT(offset)); |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int em_gio_get(struct gpio_chip *chip, unsigned offset) |
| 206 | { |
| 207 | return (int)(em_gio_read(gpio_to_priv(chip), GIO_I) & BIT(offset)); |
| 208 | } |
| 209 | |
| 210 | static void __em_gio_set(struct gpio_chip *chip, unsigned int reg, |
| 211 | unsigned shift, int value) |
| 212 | { |
| 213 | /* upper 16 bits contains mask and lower 16 actual value */ |
| 214 | em_gio_write(gpio_to_priv(chip), reg, |
| 215 | (1 << (shift + 16)) | (value << shift)); |
| 216 | } |
| 217 | |
| 218 | static void em_gio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 219 | { |
| 220 | /* output is split into two registers */ |
| 221 | if (offset < 16) |
| 222 | __em_gio_set(chip, GIO_OL, offset, value); |
| 223 | else |
| 224 | __em_gio_set(chip, GIO_OH, offset - 16, value); |
| 225 | } |
| 226 | |
| 227 | static int em_gio_direction_output(struct gpio_chip *chip, unsigned offset, |
| 228 | int value) |
| 229 | { |
| 230 | /* write GPIO value to output before selecting output mode of pin */ |
| 231 | em_gio_set(chip, offset, value); |
| 232 | em_gio_write(gpio_to_priv(chip), GIO_E1, BIT(offset)); |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static int em_gio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 237 | { |
Linus Walleij | 7385500 | 2012-10-16 20:15:02 +0200 | [diff] [blame] | 238 | return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset); |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 239 | } |
| 240 | |
Magnus Damm | 640efa0 | 2013-07-03 13:14:32 +0900 | [diff] [blame] | 241 | static int em_gio_request(struct gpio_chip *chip, unsigned offset) |
| 242 | { |
| 243 | return pinctrl_request_gpio(chip->base + offset); |
| 244 | } |
| 245 | |
| 246 | static void em_gio_free(struct gpio_chip *chip, unsigned offset) |
| 247 | { |
| 248 | pinctrl_free_gpio(chip->base + offset); |
| 249 | |
| 250 | /* Set the GPIO as an input to ensure that the next GPIO request won't |
| 251 | * drive the GPIO pin as an output. |
| 252 | */ |
| 253 | em_gio_direction_input(chip, offset); |
| 254 | } |
| 255 | |
Linus Walleij | 2d61e3e | 2013-10-11 19:21:34 +0200 | [diff] [blame] | 256 | static int em_gio_irq_domain_map(struct irq_domain *h, unsigned int irq, |
| 257 | irq_hw_number_t hwirq) |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 258 | { |
| 259 | struct em_gio_priv *p = h->host_data; |
| 260 | |
Linus Walleij | 2d61e3e | 2013-10-11 19:21:34 +0200 | [diff] [blame] | 261 | pr_debug("gio: map hw irq = %d, irq = %d\n", (int)hwirq, irq); |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 262 | |
Linus Walleij | 2d61e3e | 2013-10-11 19:21:34 +0200 | [diff] [blame] | 263 | irq_set_chip_data(irq, h->host_data); |
| 264 | irq_set_chip_and_handler(irq, &p->irq_chip, handle_level_irq); |
| 265 | set_irq_flags(irq, IRQF_VALID); /* kill me now */ |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static struct irq_domain_ops em_gio_irq_domain_ops = { |
| 270 | .map = em_gio_irq_domain_map, |
Magnus Damm | 753c598 | 2013-02-26 22:26:23 +0900 | [diff] [blame] | 271 | .xlate = irq_domain_xlate_twocell, |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 272 | }; |
| 273 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 274 | static int em_gio_probe(struct platform_device *pdev) |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 275 | { |
Magnus Damm | 753c598 | 2013-02-26 22:26:23 +0900 | [diff] [blame] | 276 | struct gpio_em_config pdata_dt; |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 277 | struct gpio_em_config *pdata = dev_get_platdata(&pdev->dev); |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 278 | struct em_gio_priv *p; |
| 279 | struct resource *io[2], *irq[2]; |
| 280 | struct gpio_chip *gpio_chip; |
| 281 | struct irq_chip *irq_chip; |
| 282 | const char *name = dev_name(&pdev->dev); |
| 283 | int ret; |
| 284 | |
Magnus Damm | 1cfe6f8 | 2013-03-13 20:06:30 +0900 | [diff] [blame] | 285 | p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL); |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 286 | if (!p) { |
| 287 | dev_err(&pdev->dev, "failed to allocate driver data\n"); |
| 288 | ret = -ENOMEM; |
| 289 | goto err0; |
| 290 | } |
| 291 | |
| 292 | p->pdev = pdev; |
| 293 | platform_set_drvdata(pdev, p); |
| 294 | spin_lock_init(&p->sense_lock); |
| 295 | |
| 296 | io[0] = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 297 | io[1] = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 298 | irq[0] = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 299 | irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1); |
| 300 | |
Magnus Damm | 753c598 | 2013-02-26 22:26:23 +0900 | [diff] [blame] | 301 | if (!io[0] || !io[1] || !irq[0] || !irq[1]) { |
| 302 | dev_err(&pdev->dev, "missing IRQ or IOMEM\n"); |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 303 | ret = -EINVAL; |
Magnus Damm | 1cfe6f8 | 2013-03-13 20:06:30 +0900 | [diff] [blame] | 304 | goto err0; |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 305 | } |
| 306 | |
Magnus Damm | 1cfe6f8 | 2013-03-13 20:06:30 +0900 | [diff] [blame] | 307 | p->base0 = devm_ioremap_nocache(&pdev->dev, io[0]->start, |
| 308 | resource_size(io[0])); |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 309 | if (!p->base0) { |
| 310 | dev_err(&pdev->dev, "failed to remap low I/O memory\n"); |
| 311 | ret = -ENXIO; |
Magnus Damm | 1cfe6f8 | 2013-03-13 20:06:30 +0900 | [diff] [blame] | 312 | goto err0; |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 313 | } |
| 314 | |
Magnus Damm | 1cfe6f8 | 2013-03-13 20:06:30 +0900 | [diff] [blame] | 315 | p->base1 = devm_ioremap_nocache(&pdev->dev, io[1]->start, |
| 316 | resource_size(io[1])); |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 317 | if (!p->base1) { |
| 318 | dev_err(&pdev->dev, "failed to remap high I/O memory\n"); |
| 319 | ret = -ENXIO; |
Magnus Damm | 1cfe6f8 | 2013-03-13 20:06:30 +0900 | [diff] [blame] | 320 | goto err0; |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 321 | } |
| 322 | |
Magnus Damm | 753c598 | 2013-02-26 22:26:23 +0900 | [diff] [blame] | 323 | if (!pdata) { |
| 324 | memset(&pdata_dt, 0, sizeof(pdata_dt)); |
| 325 | pdata = &pdata_dt; |
| 326 | |
| 327 | if (of_property_read_u32(pdev->dev.of_node, "ngpios", |
| 328 | &pdata->number_of_pins)) { |
| 329 | dev_err(&pdev->dev, "Missing ngpios OF property\n"); |
| 330 | ret = -EINVAL; |
Magnus Damm | 1cfe6f8 | 2013-03-13 20:06:30 +0900 | [diff] [blame] | 331 | goto err0; |
Magnus Damm | 753c598 | 2013-02-26 22:26:23 +0900 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | ret = of_alias_get_id(pdev->dev.of_node, "gpio"); |
| 335 | if (ret < 0) { |
| 336 | dev_err(&pdev->dev, "Couldn't get OF id\n"); |
Magnus Damm | 1cfe6f8 | 2013-03-13 20:06:30 +0900 | [diff] [blame] | 337 | goto err0; |
Magnus Damm | 753c598 | 2013-02-26 22:26:23 +0900 | [diff] [blame] | 338 | } |
| 339 | pdata->gpio_base = ret * 32; /* 32 GPIOs per instance */ |
| 340 | } |
| 341 | |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 342 | gpio_chip = &p->gpio_chip; |
Ian Molton | b592785 | 2013-09-02 16:44:55 +0100 | [diff] [blame] | 343 | gpio_chip->of_node = pdev->dev.of_node; |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 344 | gpio_chip->direction_input = em_gio_direction_input; |
| 345 | gpio_chip->get = em_gio_get; |
| 346 | gpio_chip->direction_output = em_gio_direction_output; |
| 347 | gpio_chip->set = em_gio_set; |
| 348 | gpio_chip->to_irq = em_gio_to_irq; |
Magnus Damm | 640efa0 | 2013-07-03 13:14:32 +0900 | [diff] [blame] | 349 | gpio_chip->request = em_gio_request; |
| 350 | gpio_chip->free = em_gio_free; |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 351 | gpio_chip->label = name; |
Magnus Damm | 969bf7a | 2013-11-20 09:23:26 +0900 | [diff] [blame] | 352 | gpio_chip->dev = &pdev->dev; |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 353 | gpio_chip->owner = THIS_MODULE; |
| 354 | gpio_chip->base = pdata->gpio_base; |
| 355 | gpio_chip->ngpio = pdata->number_of_pins; |
| 356 | |
| 357 | irq_chip = &p->irq_chip; |
| 358 | irq_chip->name = name; |
| 359 | irq_chip->irq_mask = em_gio_irq_disable; |
| 360 | irq_chip->irq_unmask = em_gio_irq_enable; |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 361 | irq_chip->irq_set_type = em_gio_irq_set_type; |
Linus Walleij | 57ef042 | 2014-03-14 18:16:20 +0100 | [diff] [blame] | 362 | irq_chip->irq_request_resources = em_gio_irq_reqres; |
| 363 | irq_chip->irq_release_resources = em_gio_irq_relres; |
Magnus Damm | 03621b6 | 2013-11-20 09:23:44 +0900 | [diff] [blame] | 364 | irq_chip->flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MASK_ON_SUSPEND; |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 365 | |
Magnus Damm | c7886b1 | 2013-02-13 00:56:13 +0900 | [diff] [blame] | 366 | p->irq_domain = irq_domain_add_simple(pdev->dev.of_node, |
Linus Walleij | 7385500 | 2012-10-16 20:15:02 +0200 | [diff] [blame] | 367 | pdata->number_of_pins, |
Magnus Damm | c7886b1 | 2013-02-13 00:56:13 +0900 | [diff] [blame] | 368 | pdata->irq_base, |
Linus Walleij | 7385500 | 2012-10-16 20:15:02 +0200 | [diff] [blame] | 369 | &em_gio_irq_domain_ops, p); |
Axel Lin | 1631081 | 2012-10-31 17:03:33 +0800 | [diff] [blame] | 370 | if (!p->irq_domain) { |
| 371 | ret = -ENXIO; |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 372 | dev_err(&pdev->dev, "cannot initialize irq domain\n"); |
Magnus Damm | 1cfe6f8 | 2013-03-13 20:06:30 +0900 | [diff] [blame] | 373 | goto err0; |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 374 | } |
| 375 | |
Magnus Damm | 1cfe6f8 | 2013-03-13 20:06:30 +0900 | [diff] [blame] | 376 | if (devm_request_irq(&pdev->dev, irq[0]->start, |
| 377 | em_gio_irq_handler, 0, name, p)) { |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 378 | dev_err(&pdev->dev, "failed to request low IRQ\n"); |
| 379 | ret = -ENOENT; |
Magnus Damm | 1cfe6f8 | 2013-03-13 20:06:30 +0900 | [diff] [blame] | 380 | goto err1; |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 381 | } |
| 382 | |
Magnus Damm | 1cfe6f8 | 2013-03-13 20:06:30 +0900 | [diff] [blame] | 383 | if (devm_request_irq(&pdev->dev, irq[1]->start, |
| 384 | em_gio_irq_handler, 0, name, p)) { |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 385 | dev_err(&pdev->dev, "failed to request high IRQ\n"); |
| 386 | ret = -ENOENT; |
Magnus Damm | 1cfe6f8 | 2013-03-13 20:06:30 +0900 | [diff] [blame] | 387 | goto err1; |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | ret = gpiochip_add(gpio_chip); |
| 391 | if (ret) { |
| 392 | dev_err(&pdev->dev, "failed to add GPIO controller\n"); |
Magnus Damm | 1cfe6f8 | 2013-03-13 20:06:30 +0900 | [diff] [blame] | 393 | goto err1; |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 394 | } |
Magnus Damm | 640efa0 | 2013-07-03 13:14:32 +0900 | [diff] [blame] | 395 | |
| 396 | if (pdata->pctl_name) { |
| 397 | ret = gpiochip_add_pin_range(gpio_chip, pdata->pctl_name, 0, |
| 398 | gpio_chip->base, gpio_chip->ngpio); |
| 399 | if (ret < 0) |
| 400 | dev_warn(&pdev->dev, "failed to add pin range\n"); |
| 401 | } |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 402 | return 0; |
| 403 | |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 404 | err1: |
Magnus Damm | 1cfe6f8 | 2013-03-13 20:06:30 +0900 | [diff] [blame] | 405 | irq_domain_remove(p->irq_domain); |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 406 | err0: |
| 407 | return ret; |
| 408 | } |
| 409 | |
Bill Pemberton | 206210c | 2012-11-19 13:25:50 -0500 | [diff] [blame] | 410 | static int em_gio_remove(struct platform_device *pdev) |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 411 | { |
| 412 | struct em_gio_priv *p = platform_get_drvdata(pdev); |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 413 | int ret; |
| 414 | |
| 415 | ret = gpiochip_remove(&p->gpio_chip); |
| 416 | if (ret) |
| 417 | return ret; |
| 418 | |
Axel Lin | 1631081 | 2012-10-31 17:03:33 +0800 | [diff] [blame] | 419 | irq_domain_remove(p->irq_domain); |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
Magnus Damm | 753c598 | 2013-02-26 22:26:23 +0900 | [diff] [blame] | 423 | static const struct of_device_id em_gio_dt_ids[] = { |
| 424 | { .compatible = "renesas,em-gio", }, |
| 425 | {}, |
| 426 | }; |
| 427 | MODULE_DEVICE_TABLE(of, em_gio_dt_ids); |
| 428 | |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 429 | static struct platform_driver em_gio_device_driver = { |
| 430 | .probe = em_gio_probe, |
Bill Pemberton | 8283c4f | 2012-11-19 13:20:08 -0500 | [diff] [blame] | 431 | .remove = em_gio_remove, |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 432 | .driver = { |
| 433 | .name = "em_gio", |
Magnus Damm | 753c598 | 2013-02-26 22:26:23 +0900 | [diff] [blame] | 434 | .of_match_table = em_gio_dt_ids, |
| 435 | .owner = THIS_MODULE, |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 436 | } |
| 437 | }; |
| 438 | |
Magnus Damm | 753c598 | 2013-02-26 22:26:23 +0900 | [diff] [blame] | 439 | static int __init em_gio_init(void) |
| 440 | { |
| 441 | return platform_driver_register(&em_gio_device_driver); |
| 442 | } |
| 443 | postcore_initcall(em_gio_init); |
| 444 | |
| 445 | static void __exit em_gio_exit(void) |
| 446 | { |
| 447 | platform_driver_unregister(&em_gio_device_driver); |
| 448 | } |
| 449 | module_exit(em_gio_exit); |
Magnus Damm | a07e103 | 2012-05-17 15:22:23 +0900 | [diff] [blame] | 450 | |
| 451 | MODULE_AUTHOR("Magnus Damm"); |
| 452 | MODULE_DESCRIPTION("Renesas Emma Mobile GIO Driver"); |
| 453 | MODULE_LICENSE("GPL v2"); |