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