Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 1 | /* |
Grant Likely | c103de2 | 2011-06-04 18:38:28 -0600 | [diff] [blame] | 2 | * Copyright (C) 2008, 2009 Provigent Ltd. |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * Driver for the ARM PrimeCell(tm) General Purpose Input/Output (PL061) |
| 9 | * |
| 10 | * Data sheet: ARM DDI 0190B, September 2000 |
| 11 | */ |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/module.h> |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 15 | #include <linux/io.h> |
| 16 | #include <linux/ioport.h> |
| 17 | #include <linux/irq.h> |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 18 | #include <linux/irqdomain.h> |
Catalin Marinas | de88cbb | 2013-01-18 15:31:37 +0000 | [diff] [blame] | 19 | #include <linux/irqchip/chained_irq.h> |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 20 | #include <linux/bitops.h> |
| 21 | #include <linux/workqueue.h> |
| 22 | #include <linux/gpio.h> |
| 23 | #include <linux/device.h> |
| 24 | #include <linux/amba/bus.h> |
| 25 | #include <linux/amba/pl061.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Haojian Zhuang | 39b70ee | 2013-02-17 19:42:51 +0800 | [diff] [blame] | 27 | #include <linux/pinctrl/consumer.h> |
Deepak Sikri | e198a8de | 2011-11-18 15:20:12 +0530 | [diff] [blame] | 28 | #include <linux/pm.h> |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 29 | |
| 30 | #define GPIODIR 0x400 |
| 31 | #define GPIOIS 0x404 |
| 32 | #define GPIOIBE 0x408 |
| 33 | #define GPIOIEV 0x40C |
| 34 | #define GPIOIE 0x410 |
| 35 | #define GPIORIS 0x414 |
| 36 | #define GPIOMIS 0x418 |
| 37 | #define GPIOIC 0x41C |
| 38 | |
| 39 | #define PL061_GPIO_NR 8 |
| 40 | |
Deepak Sikri | e198a8de | 2011-11-18 15:20:12 +0530 | [diff] [blame] | 41 | #ifdef CONFIG_PM |
| 42 | struct pl061_context_save_regs { |
| 43 | u8 gpio_data; |
| 44 | u8 gpio_dir; |
| 45 | u8 gpio_is; |
| 46 | u8 gpio_ibe; |
| 47 | u8 gpio_iev; |
| 48 | u8 gpio_ie; |
| 49 | }; |
| 50 | #endif |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 51 | |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 52 | struct pl061_gpio { |
Baruch Siach | 835c192 | 2012-11-22 11:46:14 +0200 | [diff] [blame] | 53 | spinlock_t lock; |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 54 | |
| 55 | void __iomem *base; |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 56 | struct irq_domain *domain; |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 57 | struct gpio_chip gc; |
Deepak Sikri | e198a8de | 2011-11-18 15:20:12 +0530 | [diff] [blame] | 58 | |
| 59 | #ifdef CONFIG_PM |
| 60 | struct pl061_context_save_regs csave_regs; |
| 61 | #endif |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Haojian Zhuang | 39b70ee | 2013-02-17 19:42:51 +0800 | [diff] [blame] | 64 | static int pl061_gpio_request(struct gpio_chip *chip, unsigned offset) |
| 65 | { |
| 66 | /* |
| 67 | * Map back to global GPIO space and request muxing, the direction |
| 68 | * parameter does not matter for this controller. |
| 69 | */ |
| 70 | int gpio = chip->base + offset; |
| 71 | |
| 72 | return pinctrl_request_gpio(gpio); |
| 73 | } |
| 74 | |
Axel Lin | 22ce446 | 2013-03-15 20:52:07 +0800 | [diff] [blame] | 75 | static void pl061_gpio_free(struct gpio_chip *chip, unsigned offset) |
| 76 | { |
| 77 | int gpio = chip->base + offset; |
| 78 | |
| 79 | pinctrl_free_gpio(gpio); |
| 80 | } |
| 81 | |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 82 | static int pl061_direction_input(struct gpio_chip *gc, unsigned offset) |
| 83 | { |
| 84 | struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc); |
| 85 | unsigned long flags; |
| 86 | unsigned char gpiodir; |
| 87 | |
| 88 | if (offset >= gc->ngpio) |
| 89 | return -EINVAL; |
| 90 | |
| 91 | spin_lock_irqsave(&chip->lock, flags); |
| 92 | gpiodir = readb(chip->base + GPIODIR); |
| 93 | gpiodir &= ~(1 << offset); |
| 94 | writeb(gpiodir, chip->base + GPIODIR); |
| 95 | spin_unlock_irqrestore(&chip->lock, flags); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int pl061_direction_output(struct gpio_chip *gc, unsigned offset, |
| 101 | int value) |
| 102 | { |
| 103 | struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc); |
| 104 | unsigned long flags; |
| 105 | unsigned char gpiodir; |
| 106 | |
| 107 | if (offset >= gc->ngpio) |
| 108 | return -EINVAL; |
| 109 | |
| 110 | spin_lock_irqsave(&chip->lock, flags); |
| 111 | writeb(!!value << offset, chip->base + (1 << (offset + 2))); |
| 112 | gpiodir = readb(chip->base + GPIODIR); |
| 113 | gpiodir |= 1 << offset; |
| 114 | writeb(gpiodir, chip->base + GPIODIR); |
viresh kumar | 64b997c5 | 2010-04-21 09:42:05 +0100 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | * gpio value is set again, because pl061 doesn't allow to set value of |
| 118 | * a gpio pin before configuring it in OUT mode. |
| 119 | */ |
| 120 | writeb(!!value << offset, chip->base + (1 << (offset + 2))); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 121 | spin_unlock_irqrestore(&chip->lock, flags); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int pl061_get_value(struct gpio_chip *gc, unsigned offset) |
| 127 | { |
| 128 | struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc); |
| 129 | |
| 130 | return !!readb(chip->base + (1 << (offset + 2))); |
| 131 | } |
| 132 | |
| 133 | static void pl061_set_value(struct gpio_chip *gc, unsigned offset, int value) |
| 134 | { |
| 135 | struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc); |
| 136 | |
| 137 | writeb(!!value << offset, chip->base + (1 << (offset + 2))); |
| 138 | } |
| 139 | |
Baruch Siach | 50efacf | 2009-06-30 11:41:39 -0700 | [diff] [blame] | 140 | static int pl061_to_irq(struct gpio_chip *gc, unsigned offset) |
| 141 | { |
| 142 | struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc); |
| 143 | |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 144 | return irq_create_mapping(chip->domain, offset); |
Baruch Siach | 50efacf | 2009-06-30 11:41:39 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Lennert Buytenhek | b222186 | 2011-01-12 17:00:16 -0800 | [diff] [blame] | 147 | static int pl061_irq_type(struct irq_data *d, unsigned trigger) |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 148 | { |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 149 | struct pl061_gpio *chip = irq_data_get_irq_chip_data(d); |
| 150 | int offset = irqd_to_hwirq(d); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 151 | unsigned long flags; |
| 152 | u8 gpiois, gpioibe, gpioiev; |
Linus Walleij | 438a2c9 | 2013-11-26 12:59:51 +0100 | [diff] [blame^] | 153 | u8 bit = BIT(offset); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 154 | |
Axel Lin | c1cc9b9 | 2010-05-26 14:42:19 -0700 | [diff] [blame] | 155 | if (offset < 0 || offset >= PL061_GPIO_NR) |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 156 | return -EINVAL; |
| 157 | |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 158 | spin_lock_irqsave(&chip->lock, flags); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 159 | |
| 160 | gpioiev = readb(chip->base + GPIOIEV); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 161 | gpiois = readb(chip->base + GPIOIS); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 162 | gpioibe = readb(chip->base + GPIOIBE); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 163 | |
Linus Walleij | 438a2c9 | 2013-11-26 12:59:51 +0100 | [diff] [blame^] | 164 | if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) { |
| 165 | gpiois |= bit; |
| 166 | if (trigger & IRQ_TYPE_LEVEL_HIGH) |
| 167 | gpioiev |= bit; |
| 168 | else |
| 169 | gpioiev &= ~bit; |
| 170 | } else |
| 171 | gpiois &= ~bit; |
| 172 | |
| 173 | if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) |
| 174 | /* Setting this makes GPIOEV be ignored */ |
| 175 | gpioibe |= bit; |
| 176 | else { |
| 177 | gpioibe &= ~bit; |
| 178 | if (trigger & IRQ_TYPE_EDGE_RISING) |
| 179 | gpioiev |= bit; |
| 180 | else if (trigger & IRQ_TYPE_EDGE_FALLING) |
| 181 | gpioiev &= ~bit; |
| 182 | } |
| 183 | |
| 184 | writeb(gpiois, chip->base + GPIOIS); |
| 185 | writeb(gpioibe, chip->base + GPIOIBE); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 186 | writeb(gpioiev, chip->base + GPIOIEV); |
| 187 | |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 188 | spin_unlock_irqrestore(&chip->lock, flags); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 193 | static void pl061_irq_handler(unsigned irq, struct irq_desc *desc) |
| 194 | { |
Rob Herring | 2de0dbc | 2012-01-04 10:36:07 -0600 | [diff] [blame] | 195 | unsigned long pending; |
| 196 | int offset; |
| 197 | struct pl061_gpio *chip = irq_desc_get_handler_data(desc); |
Rob Herring | dece904 | 2011-12-09 14:12:53 -0600 | [diff] [blame] | 198 | struct irq_chip *irqchip = irq_desc_get_chip(desc); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 199 | |
Rob Herring | dece904 | 2011-12-09 14:12:53 -0600 | [diff] [blame] | 200 | chained_irq_enter(irqchip, desc); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 201 | |
Rob Herring | 2de0dbc | 2012-01-04 10:36:07 -0600 | [diff] [blame] | 202 | pending = readb(chip->base + GPIOMIS); |
| 203 | writeb(pending, chip->base + GPIOIC); |
| 204 | if (pending) { |
Akinobu Mita | 984b3f5 | 2010-03-05 13:41:37 -0800 | [diff] [blame] | 205 | for_each_set_bit(offset, &pending, PL061_GPIO_NR) |
Baruch Siach | 50efacf | 2009-06-30 11:41:39 -0700 | [diff] [blame] | 206 | generic_handle_irq(pl061_to_irq(&chip->gc, offset)); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 207 | } |
Rob Herring | 2de0dbc | 2012-01-04 10:36:07 -0600 | [diff] [blame] | 208 | |
Rob Herring | dece904 | 2011-12-09 14:12:53 -0600 | [diff] [blame] | 209 | chained_irq_exit(irqchip, desc); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 212 | static void pl061_irq_mask(struct irq_data *d) |
Rob Herring | 3ab5247 | 2011-10-21 08:05:53 -0500 | [diff] [blame] | 213 | { |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 214 | struct pl061_gpio *chip = irq_data_get_irq_chip_data(d); |
| 215 | u8 mask = 1 << (irqd_to_hwirq(d) % PL061_GPIO_NR); |
| 216 | u8 gpioie; |
Rob Herring | 3ab5247 | 2011-10-21 08:05:53 -0500 | [diff] [blame] | 217 | |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 218 | spin_lock(&chip->lock); |
| 219 | gpioie = readb(chip->base + GPIOIE) & ~mask; |
| 220 | writeb(gpioie, chip->base + GPIOIE); |
| 221 | spin_unlock(&chip->lock); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 224 | static void pl061_irq_unmask(struct irq_data *d) |
| 225 | { |
| 226 | struct pl061_gpio *chip = irq_data_get_irq_chip_data(d); |
| 227 | u8 mask = 1 << (irqd_to_hwirq(d) % PL061_GPIO_NR); |
| 228 | u8 gpioie; |
| 229 | |
| 230 | spin_lock(&chip->lock); |
| 231 | gpioie = readb(chip->base + GPIOIE) | mask; |
| 232 | writeb(gpioie, chip->base + GPIOIE); |
| 233 | spin_unlock(&chip->lock); |
| 234 | } |
| 235 | |
Linus Walleij | f6f2931 | 2013-11-26 12:33:41 +0100 | [diff] [blame] | 236 | static unsigned int pl061_irq_startup(struct irq_data *d) |
| 237 | { |
| 238 | struct pl061_gpio *chip = irq_data_get_irq_chip_data(d); |
| 239 | |
| 240 | if (gpio_lock_as_irq(&chip->gc, irqd_to_hwirq(d))) |
| 241 | dev_err(chip->gc.dev, |
| 242 | "unable to lock HW IRQ %lu for IRQ\n", |
| 243 | irqd_to_hwirq(d)); |
| 244 | pl061_irq_unmask(d); |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static void pl061_irq_shutdown(struct irq_data *d) |
| 249 | { |
| 250 | struct pl061_gpio *chip = irq_data_get_irq_chip_data(d); |
| 251 | |
| 252 | pl061_irq_mask(d); |
| 253 | gpio_unlock_as_irq(&chip->gc, irqd_to_hwirq(d)); |
| 254 | } |
| 255 | |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 256 | static struct irq_chip pl061_irqchip = { |
| 257 | .name = "pl061 gpio", |
| 258 | .irq_mask = pl061_irq_mask, |
| 259 | .irq_unmask = pl061_irq_unmask, |
| 260 | .irq_set_type = pl061_irq_type, |
Linus Walleij | f6f2931 | 2013-11-26 12:33:41 +0100 | [diff] [blame] | 261 | .irq_startup = pl061_irq_startup, |
| 262 | .irq_shutdown = pl061_irq_shutdown, |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 263 | }; |
| 264 | |
Linus Walleij | f8f669f | 2013-10-11 19:40:16 +0200 | [diff] [blame] | 265 | static int pl061_irq_map(struct irq_domain *d, unsigned int irq, |
| 266 | irq_hw_number_t hwirq) |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 267 | { |
| 268 | struct pl061_gpio *chip = d->host_data; |
| 269 | |
Linus Walleij | f8f669f | 2013-10-11 19:40:16 +0200 | [diff] [blame] | 270 | irq_set_chip_and_handler_name(irq, &pl061_irqchip, handle_simple_irq, |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 271 | "pl061"); |
Linus Walleij | f8f669f | 2013-10-11 19:40:16 +0200 | [diff] [blame] | 272 | irq_set_chip_data(irq, chip); |
| 273 | irq_set_irq_type(irq, IRQ_TYPE_NONE); |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static const struct irq_domain_ops pl061_domain_ops = { |
| 279 | .map = pl061_irq_map, |
| 280 | .xlate = irq_domain_xlate_twocell, |
| 281 | }; |
| 282 | |
Tobias Klauser | 8944df7 | 2012-10-05 11:45:28 +0200 | [diff] [blame] | 283 | static int pl061_probe(struct amba_device *adev, const struct amba_id *id) |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 284 | { |
Tobias Klauser | 8944df7 | 2012-10-05 11:45:28 +0200 | [diff] [blame] | 285 | struct device *dev = &adev->dev; |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 286 | struct pl061_platform_data *pdata = dev_get_platdata(dev); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 287 | struct pl061_gpio *chip; |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 288 | int ret, irq, i, irq_base; |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 289 | |
Tobias Klauser | 8944df7 | 2012-10-05 11:45:28 +0200 | [diff] [blame] | 290 | chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 291 | if (chip == NULL) |
| 292 | return -ENOMEM; |
| 293 | |
Rob Herring | 76c05c8 | 2011-08-10 16:31:46 -0500 | [diff] [blame] | 294 | if (pdata) { |
| 295 | chip->gc.base = pdata->gpio_base; |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 296 | irq_base = pdata->irq_base; |
Linus Walleij | 7808755 | 2013-11-22 10:11:49 +0100 | [diff] [blame] | 297 | if (irq_base <= 0) { |
| 298 | dev_err(&adev->dev, "invalid IRQ base in pdata\n"); |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 299 | return -ENODEV; |
Linus Walleij | 7808755 | 2013-11-22 10:11:49 +0100 | [diff] [blame] | 300 | } |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 301 | } else { |
Rob Herring | 76c05c8 | 2011-08-10 16:31:46 -0500 | [diff] [blame] | 302 | chip->gc.base = -1; |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 303 | irq_base = 0; |
| 304 | } |
Rob Herring | 76c05c8 | 2011-08-10 16:31:46 -0500 | [diff] [blame] | 305 | |
Tobias Klauser | 8944df7 | 2012-10-05 11:45:28 +0200 | [diff] [blame] | 306 | if (!devm_request_mem_region(dev, adev->res.start, |
Linus Walleij | 7808755 | 2013-11-22 10:11:49 +0100 | [diff] [blame] | 307 | resource_size(&adev->res), "pl061")) { |
| 308 | dev_err(&adev->dev, "no memory region\n"); |
Tobias Klauser | 8944df7 | 2012-10-05 11:45:28 +0200 | [diff] [blame] | 309 | return -EBUSY; |
Linus Walleij | 7808755 | 2013-11-22 10:11:49 +0100 | [diff] [blame] | 310 | } |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 311 | |
Tobias Klauser | 8944df7 | 2012-10-05 11:45:28 +0200 | [diff] [blame] | 312 | chip->base = devm_ioremap(dev, adev->res.start, |
Haojian Zhuang | f1f7047 | 2013-02-17 19:42:49 +0800 | [diff] [blame] | 313 | resource_size(&adev->res)); |
Linus Walleij | 7808755 | 2013-11-22 10:11:49 +0100 | [diff] [blame] | 314 | if (!chip->base) { |
| 315 | dev_err(&adev->dev, "could not remap memory\n"); |
Tobias Klauser | 8944df7 | 2012-10-05 11:45:28 +0200 | [diff] [blame] | 316 | return -ENOMEM; |
Linus Walleij | 7808755 | 2013-11-22 10:11:49 +0100 | [diff] [blame] | 317 | } |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 318 | |
| 319 | spin_lock_init(&chip->lock); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 320 | |
Haojian Zhuang | 39b70ee | 2013-02-17 19:42:51 +0800 | [diff] [blame] | 321 | chip->gc.request = pl061_gpio_request; |
Axel Lin | 22ce446 | 2013-03-15 20:52:07 +0800 | [diff] [blame] | 322 | chip->gc.free = pl061_gpio_free; |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 323 | chip->gc.direction_input = pl061_direction_input; |
| 324 | chip->gc.direction_output = pl061_direction_output; |
| 325 | chip->gc.get = pl061_get_value; |
| 326 | chip->gc.set = pl061_set_value; |
Baruch Siach | 50efacf | 2009-06-30 11:41:39 -0700 | [diff] [blame] | 327 | chip->gc.to_irq = pl061_to_irq; |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 328 | chip->gc.ngpio = PL061_GPIO_NR; |
Tobias Klauser | 8944df7 | 2012-10-05 11:45:28 +0200 | [diff] [blame] | 329 | chip->gc.label = dev_name(dev); |
| 330 | chip->gc.dev = dev; |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 331 | chip->gc.owner = THIS_MODULE; |
| 332 | |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 333 | ret = gpiochip_add(&chip->gc); |
| 334 | if (ret) |
Tobias Klauser | 8944df7 | 2012-10-05 11:45:28 +0200 | [diff] [blame] | 335 | return ret; |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 336 | |
| 337 | /* |
| 338 | * irq_chip support |
| 339 | */ |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 340 | writeb(0, chip->base + GPIOIE); /* disable irqs */ |
Tobias Klauser | 8944df7 | 2012-10-05 11:45:28 +0200 | [diff] [blame] | 341 | irq = adev->irq[0]; |
Linus Walleij | 7808755 | 2013-11-22 10:11:49 +0100 | [diff] [blame] | 342 | if (irq < 0) { |
| 343 | dev_err(&adev->dev, "invalid IRQ\n"); |
Tobias Klauser | 8944df7 | 2012-10-05 11:45:28 +0200 | [diff] [blame] | 344 | return -ENODEV; |
Linus Walleij | 7808755 | 2013-11-22 10:11:49 +0100 | [diff] [blame] | 345 | } |
Tobias Klauser | 8944df7 | 2012-10-05 11:45:28 +0200 | [diff] [blame] | 346 | |
Thomas Gleixner | b51804b | 2011-03-24 21:27:36 +0000 | [diff] [blame] | 347 | irq_set_chained_handler(irq, pl061_irq_handler); |
Rob Herring | 2de0dbc | 2012-01-04 10:36:07 -0600 | [diff] [blame] | 348 | irq_set_handler_data(irq, chip); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 349 | |
Linus Walleij | 2ba3154 | 2013-11-27 08:47:02 +0100 | [diff] [blame] | 350 | chip->domain = irq_domain_add_simple(adev->dev.of_node, PL061_GPIO_NR, |
| 351 | irq_base, &pl061_domain_ops, chip); |
Linus Walleij | 7808755 | 2013-11-22 10:11:49 +0100 | [diff] [blame] | 352 | if (!chip->domain) { |
| 353 | dev_err(&adev->dev, "no irq domain\n"); |
Linus Walleij | 2ba3154 | 2013-11-27 08:47:02 +0100 | [diff] [blame] | 354 | return -ENODEV; |
Linus Walleij | 7808755 | 2013-11-22 10:11:49 +0100 | [diff] [blame] | 355 | } |
Linus Walleij | 2ba3154 | 2013-11-27 08:47:02 +0100 | [diff] [blame] | 356 | |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 357 | for (i = 0; i < PL061_GPIO_NR; i++) { |
Rob Herring | 76c05c8 | 2011-08-10 16:31:46 -0500 | [diff] [blame] | 358 | if (pdata) { |
| 359 | if (pdata->directions & (1 << i)) |
| 360 | pl061_direction_output(&chip->gc, i, |
| 361 | pdata->values & (1 << i)); |
| 362 | else |
| 363 | pl061_direction_input(&chip->gc, i); |
| 364 | } |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Tobias Klauser | 8944df7 | 2012-10-05 11:45:28 +0200 | [diff] [blame] | 367 | amba_set_drvdata(adev, chip); |
Linus Walleij | 7808755 | 2013-11-22 10:11:49 +0100 | [diff] [blame] | 368 | dev_info(&adev->dev, "PL061 GPIO chip @%08x registered\n", |
| 369 | adev->res.start); |
Deepak Sikri | e198a8de | 2011-11-18 15:20:12 +0530 | [diff] [blame] | 370 | |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 371 | return 0; |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Deepak Sikri | e198a8de | 2011-11-18 15:20:12 +0530 | [diff] [blame] | 374 | #ifdef CONFIG_PM |
| 375 | static int pl061_suspend(struct device *dev) |
| 376 | { |
| 377 | struct pl061_gpio *chip = dev_get_drvdata(dev); |
| 378 | int offset; |
| 379 | |
| 380 | chip->csave_regs.gpio_data = 0; |
| 381 | chip->csave_regs.gpio_dir = readb(chip->base + GPIODIR); |
| 382 | chip->csave_regs.gpio_is = readb(chip->base + GPIOIS); |
| 383 | chip->csave_regs.gpio_ibe = readb(chip->base + GPIOIBE); |
| 384 | chip->csave_regs.gpio_iev = readb(chip->base + GPIOIEV); |
| 385 | chip->csave_regs.gpio_ie = readb(chip->base + GPIOIE); |
| 386 | |
| 387 | for (offset = 0; offset < PL061_GPIO_NR; offset++) { |
| 388 | if (chip->csave_regs.gpio_dir & (1 << offset)) |
| 389 | chip->csave_regs.gpio_data |= |
| 390 | pl061_get_value(&chip->gc, offset) << offset; |
| 391 | } |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static int pl061_resume(struct device *dev) |
| 397 | { |
| 398 | struct pl061_gpio *chip = dev_get_drvdata(dev); |
| 399 | int offset; |
| 400 | |
| 401 | for (offset = 0; offset < PL061_GPIO_NR; offset++) { |
| 402 | if (chip->csave_regs.gpio_dir & (1 << offset)) |
| 403 | pl061_direction_output(&chip->gc, offset, |
| 404 | chip->csave_regs.gpio_data & |
| 405 | (1 << offset)); |
| 406 | else |
| 407 | pl061_direction_input(&chip->gc, offset); |
| 408 | } |
| 409 | |
| 410 | writeb(chip->csave_regs.gpio_is, chip->base + GPIOIS); |
| 411 | writeb(chip->csave_regs.gpio_ibe, chip->base + GPIOIBE); |
| 412 | writeb(chip->csave_regs.gpio_iev, chip->base + GPIOIEV); |
| 413 | writeb(chip->csave_regs.gpio_ie, chip->base + GPIOIE); |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
Viresh Kumar | 6e33ace | 2012-01-11 15:25:20 +0530 | [diff] [blame] | 418 | static const struct dev_pm_ops pl061_dev_pm_ops = { |
| 419 | .suspend = pl061_suspend, |
| 420 | .resume = pl061_resume, |
| 421 | .freeze = pl061_suspend, |
| 422 | .restore = pl061_resume, |
| 423 | }; |
Deepak Sikri | e198a8de | 2011-11-18 15:20:12 +0530 | [diff] [blame] | 424 | #endif |
| 425 | |
Russell King | 2c39c9e | 2010-07-27 08:50:16 +0100 | [diff] [blame] | 426 | static struct amba_id pl061_ids[] = { |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 427 | { |
| 428 | .id = 0x00041061, |
| 429 | .mask = 0x000fffff, |
| 430 | }, |
| 431 | { 0, 0 }, |
| 432 | }; |
| 433 | |
Dave Martin | 955b678 | 2011-10-05 15:15:21 +0100 | [diff] [blame] | 434 | MODULE_DEVICE_TABLE(amba, pl061_ids); |
| 435 | |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 436 | static struct amba_driver pl061_gpio_driver = { |
| 437 | .drv = { |
| 438 | .name = "pl061_gpio", |
Deepak Sikri | e198a8de | 2011-11-18 15:20:12 +0530 | [diff] [blame] | 439 | #ifdef CONFIG_PM |
| 440 | .pm = &pl061_dev_pm_ops, |
| 441 | #endif |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 442 | }, |
| 443 | .id_table = pl061_ids, |
| 444 | .probe = pl061_probe, |
| 445 | }; |
| 446 | |
| 447 | static int __init pl061_gpio_init(void) |
| 448 | { |
| 449 | return amba_driver_register(&pl061_gpio_driver); |
| 450 | } |
Haojian Zhuang | 5985d76 | 2013-01-18 15:31:13 +0800 | [diff] [blame] | 451 | module_init(pl061_gpio_init); |
Baruch Siach | 1e9c285 | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 452 | |
| 453 | MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>"); |
| 454 | MODULE_DESCRIPTION("PL061 GPIO driver"); |
| 455 | MODULE_LICENSE("GPL"); |