Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 1 | /* |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 2 | * GPIOs on MPC512x/8349/8572/8610 and compatible |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2008 Peter Korsgaard <jacmet@sunsite.dk> |
| 5 | * |
| 6 | * This file is licensed under the terms of the GNU General Public License |
| 7 | * version 2. This program is licensed "as is" without any warranty of any |
| 8 | * kind, whether express or implied. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/of.h> |
| 16 | #include <linux/of_gpio.h> |
Rob Herring | 5af5073 | 2013-09-17 14:28:33 -0500 | [diff] [blame] | 17 | #include <linux/of_irq.h> |
Ricardo Ribalda Delgado | 98686d9a5 | 2015-01-18 12:39:32 +0100 | [diff] [blame] | 18 | #include <linux/of_platform.h> |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 19 | #include <linux/gpio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 21 | #include <linux/irq.h> |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 22 | |
| 23 | #define MPC8XXX_GPIO_PINS 32 |
| 24 | |
| 25 | #define GPIO_DIR 0x00 |
| 26 | #define GPIO_ODR 0x04 |
| 27 | #define GPIO_DAT 0x08 |
| 28 | #define GPIO_IER 0x0c |
| 29 | #define GPIO_IMR 0x10 |
| 30 | #define GPIO_ICR 0x14 |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 31 | #define GPIO_ICR2 0x18 |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 32 | |
| 33 | struct mpc8xxx_gpio_chip { |
| 34 | struct of_mm_gpio_chip mm_gc; |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 35 | raw_spinlock_t lock; |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * shadowed data register to be able to clear/set output pins in |
| 39 | * open drain mode safely |
| 40 | */ |
| 41 | u32 data; |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 42 | struct irq_domain *irq; |
Ricardo Ribalda Delgado | 257e107 | 2015-01-18 12:39:33 +0100 | [diff] [blame] | 43 | unsigned int irqn; |
Uwe Kleine-König | 01a04dd | 2012-05-21 21:57:39 +0200 | [diff] [blame] | 44 | const void *of_dev_id_data; |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static inline u32 mpc8xxx_gpio2mask(unsigned int gpio) |
| 48 | { |
| 49 | return 1u << (MPC8XXX_GPIO_PINS - 1 - gpio); |
| 50 | } |
| 51 | |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 52 | static void mpc8xxx_gpio_save_regs(struct of_mm_gpio_chip *mm) |
| 53 | { |
Linus Walleij | 709d71a | 2015-12-07 10:34:28 +0100 | [diff] [blame^] | 54 | struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(&mm->gc); |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 55 | |
| 56 | mpc8xxx_gc->data = in_be32(mm->regs + GPIO_DAT); |
| 57 | } |
| 58 | |
Felix Radensky | c1a676d | 2009-08-12 08:57:39 +0300 | [diff] [blame] | 59 | /* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs |
| 60 | * defined as output cannot be determined by reading GPDAT register, |
| 61 | * so we use shadow data register instead. The status of input pins |
| 62 | * is determined by reading GPDAT register. |
| 63 | */ |
| 64 | static int mpc8572_gpio_get(struct gpio_chip *gc, unsigned int gpio) |
| 65 | { |
| 66 | u32 val; |
| 67 | struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc); |
Linus Walleij | 709d71a | 2015-12-07 10:34:28 +0100 | [diff] [blame^] | 68 | struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc); |
Liu Gang | 1aeef30 | 2013-11-22 16:12:40 +0800 | [diff] [blame] | 69 | u32 out_mask, out_shadow; |
Felix Radensky | c1a676d | 2009-08-12 08:57:39 +0300 | [diff] [blame] | 70 | |
Liu Gang | 1aeef30 | 2013-11-22 16:12:40 +0800 | [diff] [blame] | 71 | out_mask = in_be32(mm->regs + GPIO_DIR); |
Felix Radensky | c1a676d | 2009-08-12 08:57:39 +0300 | [diff] [blame] | 72 | |
Liu Gang | 1aeef30 | 2013-11-22 16:12:40 +0800 | [diff] [blame] | 73 | val = in_be32(mm->regs + GPIO_DAT) & ~out_mask; |
| 74 | out_shadow = mpc8xxx_gc->data & out_mask; |
| 75 | |
Linus Walleij | c759174 | 2015-12-21 11:20:56 +0100 | [diff] [blame] | 76 | return !!((val | out_shadow) & mpc8xxx_gpio2mask(gpio)); |
Felix Radensky | c1a676d | 2009-08-12 08:57:39 +0300 | [diff] [blame] | 77 | } |
| 78 | |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 79 | static int mpc8xxx_gpio_get(struct gpio_chip *gc, unsigned int gpio) |
| 80 | { |
| 81 | struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc); |
| 82 | |
| 83 | return in_be32(mm->regs + GPIO_DAT) & mpc8xxx_gpio2mask(gpio); |
| 84 | } |
| 85 | |
| 86 | static void mpc8xxx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val) |
| 87 | { |
| 88 | struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc); |
Linus Walleij | 709d71a | 2015-12-07 10:34:28 +0100 | [diff] [blame^] | 89 | struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc); |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 90 | unsigned long flags; |
| 91 | |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 92 | raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags); |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 93 | |
| 94 | if (val) |
| 95 | mpc8xxx_gc->data |= mpc8xxx_gpio2mask(gpio); |
| 96 | else |
| 97 | mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask(gpio); |
| 98 | |
| 99 | out_be32(mm->regs + GPIO_DAT, mpc8xxx_gc->data); |
| 100 | |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 101 | raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags); |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Rojhalat Ibrahim | e5db3b3 | 2014-11-04 17:12:09 +0100 | [diff] [blame] | 104 | static void mpc8xxx_gpio_set_multiple(struct gpio_chip *gc, |
| 105 | unsigned long *mask, unsigned long *bits) |
| 106 | { |
| 107 | struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc); |
Linus Walleij | 709d71a | 2015-12-07 10:34:28 +0100 | [diff] [blame^] | 108 | struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc); |
Rojhalat Ibrahim | e5db3b3 | 2014-11-04 17:12:09 +0100 | [diff] [blame] | 109 | unsigned long flags; |
| 110 | int i; |
| 111 | |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 112 | raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags); |
Rojhalat Ibrahim | e5db3b3 | 2014-11-04 17:12:09 +0100 | [diff] [blame] | 113 | |
| 114 | for (i = 0; i < gc->ngpio; i++) { |
| 115 | if (*mask == 0) |
| 116 | break; |
| 117 | if (__test_and_clear_bit(i, mask)) { |
| 118 | if (test_bit(i, bits)) |
| 119 | mpc8xxx_gc->data |= mpc8xxx_gpio2mask(i); |
| 120 | else |
| 121 | mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask(i); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | out_be32(mm->regs + GPIO_DAT, mpc8xxx_gc->data); |
| 126 | |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 127 | raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags); |
Rojhalat Ibrahim | e5db3b3 | 2014-11-04 17:12:09 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 130 | static int mpc8xxx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio) |
| 131 | { |
| 132 | struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc); |
Linus Walleij | 709d71a | 2015-12-07 10:34:28 +0100 | [diff] [blame^] | 133 | struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc); |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 134 | unsigned long flags; |
| 135 | |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 136 | raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags); |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 137 | |
| 138 | clrbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio)); |
| 139 | |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 140 | raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags); |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int mpc8xxx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) |
| 146 | { |
| 147 | struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc); |
Linus Walleij | 709d71a | 2015-12-07 10:34:28 +0100 | [diff] [blame^] | 148 | struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc); |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 149 | unsigned long flags; |
| 150 | |
| 151 | mpc8xxx_gpio_set(gc, gpio, val); |
| 152 | |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 153 | raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags); |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 154 | |
| 155 | setbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio)); |
| 156 | |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 157 | raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags); |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Wolfram Sang | 28538df | 2011-12-13 10:12:48 +0100 | [diff] [blame] | 162 | static int mpc5121_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) |
| 163 | { |
| 164 | /* GPIO 28..31 are input only on MPC5121 */ |
| 165 | if (gpio >= 28) |
| 166 | return -EINVAL; |
| 167 | |
| 168 | return mpc8xxx_gpio_dir_out(gc, gpio, val); |
| 169 | } |
| 170 | |
Uwe Kleine-König | 0ba69e0 | 2015-07-16 21:08:23 +0200 | [diff] [blame] | 171 | static int mpc5125_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) |
| 172 | { |
| 173 | /* GPIO 0..3 are input only on MPC5125 */ |
| 174 | if (gpio <= 3) |
| 175 | return -EINVAL; |
| 176 | |
| 177 | return mpc8xxx_gpio_dir_out(gc, gpio, val); |
| 178 | } |
| 179 | |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 180 | static int mpc8xxx_gpio_to_irq(struct gpio_chip *gc, unsigned offset) |
| 181 | { |
Linus Walleij | 709d71a | 2015-12-07 10:34:28 +0100 | [diff] [blame^] | 182 | struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 183 | |
| 184 | if (mpc8xxx_gc->irq && offset < MPC8XXX_GPIO_PINS) |
| 185 | return irq_create_mapping(mpc8xxx_gc->irq, offset); |
| 186 | else |
| 187 | return -ENXIO; |
| 188 | } |
| 189 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 190 | static void mpc8xxx_gpio_irq_cascade(struct irq_desc *desc) |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 191 | { |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 192 | struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_desc_get_handler_data(desc); |
Felix Radensky | cfadd83 | 2011-10-11 10:24:21 +0200 | [diff] [blame] | 193 | struct irq_chip *chip = irq_desc_get_chip(desc); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 194 | struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc; |
| 195 | unsigned int mask; |
| 196 | |
| 197 | mask = in_be32(mm->regs + GPIO_IER) & in_be32(mm->regs + GPIO_IMR); |
| 198 | if (mask) |
| 199 | generic_handle_irq(irq_linear_revmap(mpc8xxx_gc->irq, |
| 200 | 32 - ffs(mask))); |
Thomas Gleixner | d6de85e | 2012-05-03 12:22:06 +0200 | [diff] [blame] | 201 | if (chip->irq_eoi) |
| 202 | chip->irq_eoi(&desc->irq_data); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Lennert Buytenhek | 94347cb | 2011-03-08 22:26:58 +0000 | [diff] [blame] | 205 | static void mpc8xxx_irq_unmask(struct irq_data *d) |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 206 | { |
Lennert Buytenhek | 94347cb | 2011-03-08 22:26:58 +0000 | [diff] [blame] | 207 | struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 208 | struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc; |
| 209 | unsigned long flags; |
| 210 | |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 211 | raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 212 | |
Grant Likely | 476eb49 | 2011-05-04 15:02:15 +1000 | [diff] [blame] | 213 | setbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(irqd_to_hwirq(d))); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 214 | |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 215 | raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Lennert Buytenhek | 94347cb | 2011-03-08 22:26:58 +0000 | [diff] [blame] | 218 | static void mpc8xxx_irq_mask(struct irq_data *d) |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 219 | { |
Lennert Buytenhek | 94347cb | 2011-03-08 22:26:58 +0000 | [diff] [blame] | 220 | struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 221 | struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc; |
| 222 | unsigned long flags; |
| 223 | |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 224 | raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 225 | |
Grant Likely | 476eb49 | 2011-05-04 15:02:15 +1000 | [diff] [blame] | 226 | clrbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(irqd_to_hwirq(d))); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 227 | |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 228 | raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Lennert Buytenhek | 94347cb | 2011-03-08 22:26:58 +0000 | [diff] [blame] | 231 | static void mpc8xxx_irq_ack(struct irq_data *d) |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 232 | { |
Lennert Buytenhek | 94347cb | 2011-03-08 22:26:58 +0000 | [diff] [blame] | 233 | struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 234 | struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc; |
| 235 | |
Grant Likely | 476eb49 | 2011-05-04 15:02:15 +1000 | [diff] [blame] | 236 | out_be32(mm->regs + GPIO_IER, mpc8xxx_gpio2mask(irqd_to_hwirq(d))); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Lennert Buytenhek | 94347cb | 2011-03-08 22:26:58 +0000 | [diff] [blame] | 239 | static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type) |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 240 | { |
Lennert Buytenhek | 94347cb | 2011-03-08 22:26:58 +0000 | [diff] [blame] | 241 | struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 242 | struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc; |
| 243 | unsigned long flags; |
| 244 | |
| 245 | switch (flow_type) { |
| 246 | case IRQ_TYPE_EDGE_FALLING: |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 247 | raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 248 | setbits32(mm->regs + GPIO_ICR, |
Grant Likely | 476eb49 | 2011-05-04 15:02:15 +1000 | [diff] [blame] | 249 | mpc8xxx_gpio2mask(irqd_to_hwirq(d))); |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 250 | raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 251 | break; |
| 252 | |
| 253 | case IRQ_TYPE_EDGE_BOTH: |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 254 | raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 255 | clrbits32(mm->regs + GPIO_ICR, |
Grant Likely | 476eb49 | 2011-05-04 15:02:15 +1000 | [diff] [blame] | 256 | mpc8xxx_gpio2mask(irqd_to_hwirq(d))); |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 257 | raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 258 | break; |
| 259 | |
| 260 | default: |
| 261 | return -EINVAL; |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
Lennert Buytenhek | 94347cb | 2011-03-08 22:26:58 +0000 | [diff] [blame] | 267 | static int mpc512x_irq_set_type(struct irq_data *d, unsigned int flow_type) |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 268 | { |
Lennert Buytenhek | 94347cb | 2011-03-08 22:26:58 +0000 | [diff] [blame] | 269 | struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d); |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 270 | struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc; |
Grant Likely | 476eb49 | 2011-05-04 15:02:15 +1000 | [diff] [blame] | 271 | unsigned long gpio = irqd_to_hwirq(d); |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 272 | void __iomem *reg; |
| 273 | unsigned int shift; |
| 274 | unsigned long flags; |
| 275 | |
| 276 | if (gpio < 16) { |
| 277 | reg = mm->regs + GPIO_ICR; |
| 278 | shift = (15 - gpio) * 2; |
| 279 | } else { |
| 280 | reg = mm->regs + GPIO_ICR2; |
| 281 | shift = (15 - (gpio % 16)) * 2; |
| 282 | } |
| 283 | |
| 284 | switch (flow_type) { |
| 285 | case IRQ_TYPE_EDGE_FALLING: |
| 286 | case IRQ_TYPE_LEVEL_LOW: |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 287 | raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags); |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 288 | clrsetbits_be32(reg, 3 << shift, 2 << shift); |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 289 | raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags); |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 290 | break; |
| 291 | |
| 292 | case IRQ_TYPE_EDGE_RISING: |
| 293 | case IRQ_TYPE_LEVEL_HIGH: |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 294 | raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags); |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 295 | clrsetbits_be32(reg, 3 << shift, 1 << shift); |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 296 | raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags); |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 297 | break; |
| 298 | |
| 299 | case IRQ_TYPE_EDGE_BOTH: |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 300 | raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags); |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 301 | clrbits32(reg, 3 << shift); |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 302 | raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags); |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 303 | break; |
| 304 | |
| 305 | default: |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 312 | static struct irq_chip mpc8xxx_irq_chip = { |
| 313 | .name = "mpc8xxx-gpio", |
Lennert Buytenhek | 94347cb | 2011-03-08 22:26:58 +0000 | [diff] [blame] | 314 | .irq_unmask = mpc8xxx_irq_unmask, |
| 315 | .irq_mask = mpc8xxx_irq_mask, |
| 316 | .irq_ack = mpc8xxx_irq_ack, |
Uwe Kleine-König | 82e39b0 | 2015-07-16 21:08:22 +0200 | [diff] [blame] | 317 | /* this might get overwritten in mpc8xxx_probe() */ |
Lennert Buytenhek | 94347cb | 2011-03-08 22:26:58 +0000 | [diff] [blame] | 318 | .irq_set_type = mpc8xxx_irq_set_type, |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 319 | }; |
| 320 | |
Linus Walleij | 5ba17ae | 2013-10-11 19:37:30 +0200 | [diff] [blame] | 321 | static int mpc8xxx_gpio_irq_map(struct irq_domain *h, unsigned int irq, |
| 322 | irq_hw_number_t hwirq) |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 323 | { |
Linus Walleij | 5ba17ae | 2013-10-11 19:37:30 +0200 | [diff] [blame] | 324 | irq_set_chip_data(irq, h->host_data); |
| 325 | irq_set_chip_and_handler(irq, &mpc8xxx_irq_chip, handle_level_irq); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
Krzysztof Kozlowski | 0b354dc | 2015-04-27 21:54:07 +0900 | [diff] [blame] | 330 | static const struct irq_domain_ops mpc8xxx_gpio_irq_ops = { |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 331 | .map = mpc8xxx_gpio_irq_map, |
Grant Likely | ff8c3ab | 2012-01-24 17:09:13 -0700 | [diff] [blame] | 332 | .xlate = irq_domain_xlate_twocell, |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 333 | }; |
| 334 | |
Uwe Kleine-König | 82e39b0 | 2015-07-16 21:08:22 +0200 | [diff] [blame] | 335 | struct mpc8xxx_gpio_devtype { |
| 336 | int (*gpio_dir_out)(struct gpio_chip *, unsigned int, int); |
| 337 | int (*gpio_get)(struct gpio_chip *, unsigned int); |
| 338 | int (*irq_set_type)(struct irq_data *, unsigned int); |
| 339 | }; |
| 340 | |
| 341 | static const struct mpc8xxx_gpio_devtype mpc512x_gpio_devtype = { |
| 342 | .gpio_dir_out = mpc5121_gpio_dir_out, |
| 343 | .irq_set_type = mpc512x_irq_set_type, |
| 344 | }; |
| 345 | |
Uwe Kleine-König | 0ba69e0 | 2015-07-16 21:08:23 +0200 | [diff] [blame] | 346 | static const struct mpc8xxx_gpio_devtype mpc5125_gpio_devtype = { |
| 347 | .gpio_dir_out = mpc5125_gpio_dir_out, |
| 348 | .irq_set_type = mpc512x_irq_set_type, |
| 349 | }; |
| 350 | |
Uwe Kleine-König | 82e39b0 | 2015-07-16 21:08:22 +0200 | [diff] [blame] | 351 | static const struct mpc8xxx_gpio_devtype mpc8572_gpio_devtype = { |
| 352 | .gpio_get = mpc8572_gpio_get, |
| 353 | }; |
| 354 | |
| 355 | static const struct mpc8xxx_gpio_devtype mpc8xxx_gpio_devtype_default = { |
| 356 | .gpio_dir_out = mpc8xxx_gpio_dir_out, |
| 357 | .gpio_get = mpc8xxx_gpio_get, |
| 358 | .irq_set_type = mpc8xxx_irq_set_type, |
| 359 | }; |
| 360 | |
Uwe Kleine-König | 4183afe | 2015-07-16 21:08:21 +0200 | [diff] [blame] | 361 | static const struct of_device_id mpc8xxx_gpio_ids[] = { |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 362 | { .compatible = "fsl,mpc8349-gpio", }, |
Uwe Kleine-König | 82e39b0 | 2015-07-16 21:08:22 +0200 | [diff] [blame] | 363 | { .compatible = "fsl,mpc8572-gpio", .data = &mpc8572_gpio_devtype, }, |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 364 | { .compatible = "fsl,mpc8610-gpio", }, |
Uwe Kleine-König | 82e39b0 | 2015-07-16 21:08:22 +0200 | [diff] [blame] | 365 | { .compatible = "fsl,mpc5121-gpio", .data = &mpc512x_gpio_devtype, }, |
Uwe Kleine-König | 0ba69e0 | 2015-07-16 21:08:23 +0200 | [diff] [blame] | 366 | { .compatible = "fsl,mpc5125-gpio", .data = &mpc5125_gpio_devtype, }, |
Kumar Gala | 15a5148 | 2011-10-22 16:20:42 -0500 | [diff] [blame] | 367 | { .compatible = "fsl,pq3-gpio", }, |
Anatolij Gustschin | d1dcfbb | 2011-01-08 16:51:16 +0100 | [diff] [blame] | 368 | { .compatible = "fsl,qoriq-gpio", }, |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 369 | {} |
| 370 | }; |
| 371 | |
Ricardo Ribalda Delgado | 98686d9a5 | 2015-01-18 12:39:32 +0100 | [diff] [blame] | 372 | static int mpc8xxx_probe(struct platform_device *pdev) |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 373 | { |
Ricardo Ribalda Delgado | 98686d9a5 | 2015-01-18 12:39:32 +0100 | [diff] [blame] | 374 | struct device_node *np = pdev->dev.of_node; |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 375 | struct mpc8xxx_gpio_chip *mpc8xxx_gc; |
| 376 | struct of_mm_gpio_chip *mm_gc; |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 377 | struct gpio_chip *gc; |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 378 | const struct of_device_id *id; |
Uwe Kleine-König | 82e39b0 | 2015-07-16 21:08:22 +0200 | [diff] [blame] | 379 | const struct mpc8xxx_gpio_devtype *devtype = |
| 380 | of_device_get_match_data(&pdev->dev); |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 381 | int ret; |
| 382 | |
Ricardo Ribalda Delgado | 98686d9a5 | 2015-01-18 12:39:32 +0100 | [diff] [blame] | 383 | mpc8xxx_gc = devm_kzalloc(&pdev->dev, sizeof(*mpc8xxx_gc), GFP_KERNEL); |
| 384 | if (!mpc8xxx_gc) |
| 385 | return -ENOMEM; |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 386 | |
Ricardo Ribalda Delgado | 257e107 | 2015-01-18 12:39:33 +0100 | [diff] [blame] | 387 | platform_set_drvdata(pdev, mpc8xxx_gc); |
| 388 | |
Alexander Stein | 5059361 | 2015-07-21 15:54:30 +0200 | [diff] [blame] | 389 | raw_spin_lock_init(&mpc8xxx_gc->lock); |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 390 | |
| 391 | mm_gc = &mpc8xxx_gc->mm_gc; |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 392 | gc = &mm_gc->gc; |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 393 | |
| 394 | mm_gc->save_regs = mpc8xxx_gpio_save_regs; |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 395 | gc->ngpio = MPC8XXX_GPIO_PINS; |
| 396 | gc->direction_input = mpc8xxx_gpio_dir_in; |
Uwe Kleine-König | 82e39b0 | 2015-07-16 21:08:22 +0200 | [diff] [blame] | 397 | |
| 398 | if (!devtype) |
| 399 | devtype = &mpc8xxx_gpio_devtype_default; |
| 400 | |
| 401 | /* |
| 402 | * It's assumed that only a single type of gpio controller is available |
| 403 | * on the current machine, so overwriting global data is fine. |
| 404 | */ |
| 405 | mpc8xxx_irq_chip.irq_set_type = devtype->irq_set_type; |
| 406 | |
| 407 | gc->direction_output = devtype->gpio_dir_out ?: mpc8xxx_gpio_dir_out; |
| 408 | gc->get = devtype->gpio_get ?: mpc8xxx_gpio_get; |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 409 | gc->set = mpc8xxx_gpio_set; |
Rojhalat Ibrahim | e5db3b3 | 2014-11-04 17:12:09 +0100 | [diff] [blame] | 410 | gc->set_multiple = mpc8xxx_gpio_set_multiple; |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 411 | gc->to_irq = mpc8xxx_gpio_to_irq; |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 412 | |
Linus Walleij | 709d71a | 2015-12-07 10:34:28 +0100 | [diff] [blame^] | 413 | ret = of_mm_gpiochip_add_data(np, mm_gc, mpc8xxx_gc); |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 414 | if (ret) |
Ricardo Ribalda Delgado | 98686d9a5 | 2015-01-18 12:39:32 +0100 | [diff] [blame] | 415 | return ret; |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 416 | |
Ricardo Ribalda Delgado | 257e107 | 2015-01-18 12:39:33 +0100 | [diff] [blame] | 417 | mpc8xxx_gc->irqn = irq_of_parse_and_map(np, 0); |
| 418 | if (mpc8xxx_gc->irqn == NO_IRQ) |
Ricardo Ribalda Delgado | 98686d9a5 | 2015-01-18 12:39:32 +0100 | [diff] [blame] | 419 | return 0; |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 420 | |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 421 | mpc8xxx_gc->irq = irq_domain_add_linear(np, MPC8XXX_GPIO_PINS, |
| 422 | &mpc8xxx_gpio_irq_ops, mpc8xxx_gc); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 423 | if (!mpc8xxx_gc->irq) |
Ricardo Ribalda Delgado | 98686d9a5 | 2015-01-18 12:39:32 +0100 | [diff] [blame] | 424 | return 0; |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 425 | |
Anatolij Gustschin | e39d5ef | 2010-08-09 07:58:48 +0200 | [diff] [blame] | 426 | id = of_match_node(mpc8xxx_gpio_ids, np); |
| 427 | if (id) |
| 428 | mpc8xxx_gc->of_dev_id_data = id->data; |
| 429 | |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 430 | /* ack and mask all irqs */ |
| 431 | out_be32(mm_gc->regs + GPIO_IER, 0xffffffff); |
| 432 | out_be32(mm_gc->regs + GPIO_IMR, 0); |
| 433 | |
Thomas Gleixner | 0537981 | 2015-06-21 21:10:46 +0200 | [diff] [blame] | 434 | irq_set_chained_handler_and_data(mpc8xxx_gc->irqn, |
| 435 | mpc8xxx_gpio_irq_cascade, mpc8xxx_gc); |
Ricardo Ribalda Delgado | 257e107 | 2015-01-18 12:39:33 +0100 | [diff] [blame] | 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | static int mpc8xxx_remove(struct platform_device *pdev) |
| 441 | { |
| 442 | struct mpc8xxx_gpio_chip *mpc8xxx_gc = platform_get_drvdata(pdev); |
| 443 | |
| 444 | if (mpc8xxx_gc->irq) { |
Thomas Gleixner | 0537981 | 2015-06-21 21:10:46 +0200 | [diff] [blame] | 445 | irq_set_chained_handler_and_data(mpc8xxx_gc->irqn, NULL, NULL); |
Ricardo Ribalda Delgado | 257e107 | 2015-01-18 12:39:33 +0100 | [diff] [blame] | 446 | irq_domain_remove(mpc8xxx_gc->irq); |
| 447 | } |
| 448 | |
| 449 | of_mm_gpiochip_remove(&mpc8xxx_gc->mm_gc); |
Peter Korsgaard | 345e5c8 | 2010-01-07 17:57:46 +0100 | [diff] [blame] | 450 | |
Peter Korsgaard | 1e16dfc | 2008-09-23 17:35:38 +0200 | [diff] [blame] | 451 | return 0; |
| 452 | } |
Ricardo Ribalda Delgado | 98686d9a5 | 2015-01-18 12:39:32 +0100 | [diff] [blame] | 453 | |
| 454 | static struct platform_driver mpc8xxx_plat_driver = { |
| 455 | .probe = mpc8xxx_probe, |
Ricardo Ribalda Delgado | 257e107 | 2015-01-18 12:39:33 +0100 | [diff] [blame] | 456 | .remove = mpc8xxx_remove, |
Ricardo Ribalda Delgado | 98686d9a5 | 2015-01-18 12:39:32 +0100 | [diff] [blame] | 457 | .driver = { |
| 458 | .name = "gpio-mpc8xxx", |
| 459 | .of_match_table = mpc8xxx_gpio_ids, |
| 460 | }, |
| 461 | }; |
| 462 | |
| 463 | static int __init mpc8xxx_init(void) |
| 464 | { |
| 465 | return platform_driver_register(&mpc8xxx_plat_driver); |
| 466 | } |
| 467 | |
| 468 | arch_initcall(mpc8xxx_init); |