Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 OKI SEMICONDUCTOR Co., LTD. |
| 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 as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
| 16 | */ |
Paul Gortmaker | bb207ef | 2011-07-03 13:38:09 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 18 | #include <linux/kernel.h> |
Andrew Morton | 545554e | 2011-05-24 17:13:43 -0700 | [diff] [blame] | 19 | #include <linux/slab.h> |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 20 | #include <linux/pci.h> |
| 21 | #include <linux/gpio.h> |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/irq.h> |
| 24 | |
| 25 | #define IOH_EDGE_FALLING 0 |
| 26 | #define IOH_EDGE_RISING BIT(0) |
| 27 | #define IOH_LEVEL_L BIT(1) |
| 28 | #define IOH_LEVEL_H (BIT(0) | BIT(1)) |
| 29 | #define IOH_EDGE_BOTH BIT(2) |
| 30 | #define IOH_IM_MASK (BIT(0) | BIT(1) | BIT(2)) |
| 31 | |
| 32 | #define IOH_IRQ_BASE 0 |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 33 | |
| 34 | #define PCI_VENDOR_ID_ROHM 0x10DB |
| 35 | |
| 36 | struct ioh_reg_comn { |
| 37 | u32 ien; |
| 38 | u32 istatus; |
| 39 | u32 idisp; |
| 40 | u32 iclr; |
| 41 | u32 imask; |
| 42 | u32 imaskclr; |
| 43 | u32 po; |
| 44 | u32 pi; |
| 45 | u32 pm; |
| 46 | u32 im_0; |
| 47 | u32 im_1; |
| 48 | u32 reserved; |
| 49 | }; |
| 50 | |
| 51 | struct ioh_regs { |
| 52 | struct ioh_reg_comn regs[8]; |
| 53 | u32 reserve1[16]; |
| 54 | u32 ioh_sel_reg[4]; |
| 55 | u32 reserve2[11]; |
| 56 | u32 srst; |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * struct ioh_gpio_reg_data - The register store data. |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 61 | * @ien_reg To store contents of interrupt enable register. |
| 62 | * @imask_reg: To store contents of interrupt mask regist |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 63 | * @po_reg: To store contents of PO register. |
| 64 | * @pm_reg: To store contents of PM register. |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 65 | * @im0_reg: To store contents of interrupt mode regist0 |
| 66 | * @im1_reg: To store contents of interrupt mode regist1 |
Tomoya MORINAGA | b490fa0 | 2011-08-05 13:04:22 +0900 | [diff] [blame] | 67 | * @use_sel_reg: To store contents of GPIO_USE_SEL0~3 |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 68 | */ |
| 69 | struct ioh_gpio_reg_data { |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 70 | u32 ien_reg; |
| 71 | u32 imask_reg; |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 72 | u32 po_reg; |
| 73 | u32 pm_reg; |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 74 | u32 im0_reg; |
| 75 | u32 im1_reg; |
Tomoya MORINAGA | b490fa0 | 2011-08-05 13:04:22 +0900 | [diff] [blame] | 76 | u32 use_sel_reg; |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | /** |
| 80 | * struct ioh_gpio - GPIO private data structure. |
| 81 | * @base: PCI base address of Memory mapped I/O register. |
| 82 | * @reg: Memory mapped IOH GPIO register list. |
| 83 | * @dev: Pointer to device structure. |
| 84 | * @gpio: Data for GPIO infrastructure. |
| 85 | * @ioh_gpio_reg: Memory mapped Register data is saved here |
| 86 | * when suspend. |
Tomoya MORINAGA | b490fa0 | 2011-08-05 13:04:22 +0900 | [diff] [blame] | 87 | * @gpio_use_sel: Save GPIO_USE_SEL1~4 register for PM |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 88 | * @ch: Indicate GPIO channel |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 89 | * @irq_base: Save base of IRQ number for interrupt |
Axel Lin | 02a6794 | 2012-07-29 10:54:42 +0800 | [diff] [blame] | 90 | * @spinlock: Used for register access protection |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 91 | */ |
| 92 | struct ioh_gpio { |
| 93 | void __iomem *base; |
| 94 | struct ioh_regs __iomem *reg; |
| 95 | struct device *dev; |
| 96 | struct gpio_chip gpio; |
| 97 | struct ioh_gpio_reg_data ioh_gpio_reg; |
Tomoya MORINAGA | b490fa0 | 2011-08-05 13:04:22 +0900 | [diff] [blame] | 98 | u32 gpio_use_sel; |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 99 | int ch; |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 100 | int irq_base; |
| 101 | spinlock_t spinlock; |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | static const int num_ports[] = {6, 12, 16, 16, 15, 16, 16, 12}; |
| 105 | |
| 106 | static void ioh_gpio_set(struct gpio_chip *gpio, unsigned nr, int val) |
| 107 | { |
| 108 | u32 reg_val; |
Linus Walleij | 4731557 | 2015-12-07 10:12:05 +0100 | [diff] [blame] | 109 | struct ioh_gpio *chip = gpiochip_get_data(gpio); |
Axel Lin | 02a6794 | 2012-07-29 10:54:42 +0800 | [diff] [blame] | 110 | unsigned long flags; |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 111 | |
Axel Lin | 02a6794 | 2012-07-29 10:54:42 +0800 | [diff] [blame] | 112 | spin_lock_irqsave(&chip->spinlock, flags); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 113 | reg_val = ioread32(&chip->reg->regs[chip->ch].po); |
| 114 | if (val) |
| 115 | reg_val |= (1 << nr); |
| 116 | else |
| 117 | reg_val &= ~(1 << nr); |
| 118 | |
| 119 | iowrite32(reg_val, &chip->reg->regs[chip->ch].po); |
Axel Lin | 02a6794 | 2012-07-29 10:54:42 +0800 | [diff] [blame] | 120 | spin_unlock_irqrestore(&chip->spinlock, flags); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static int ioh_gpio_get(struct gpio_chip *gpio, unsigned nr) |
| 124 | { |
Linus Walleij | 4731557 | 2015-12-07 10:12:05 +0100 | [diff] [blame] | 125 | struct ioh_gpio *chip = gpiochip_get_data(gpio); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 126 | |
Linus Walleij | 6f14dd6 | 2015-12-21 11:18:44 +0100 | [diff] [blame] | 127 | return !!(ioread32(&chip->reg->regs[chip->ch].pi) & (1 << nr)); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static int ioh_gpio_direction_output(struct gpio_chip *gpio, unsigned nr, |
| 131 | int val) |
| 132 | { |
Linus Walleij | 4731557 | 2015-12-07 10:12:05 +0100 | [diff] [blame] | 133 | struct ioh_gpio *chip = gpiochip_get_data(gpio); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 134 | u32 pm; |
| 135 | u32 reg_val; |
Axel Lin | 02a6794 | 2012-07-29 10:54:42 +0800 | [diff] [blame] | 136 | unsigned long flags; |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 137 | |
Axel Lin | 02a6794 | 2012-07-29 10:54:42 +0800 | [diff] [blame] | 138 | spin_lock_irqsave(&chip->spinlock, flags); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 139 | pm = ioread32(&chip->reg->regs[chip->ch].pm) & |
| 140 | ((1 << num_ports[chip->ch]) - 1); |
| 141 | pm |= (1 << nr); |
| 142 | iowrite32(pm, &chip->reg->regs[chip->ch].pm); |
| 143 | |
| 144 | reg_val = ioread32(&chip->reg->regs[chip->ch].po); |
| 145 | if (val) |
| 146 | reg_val |= (1 << nr); |
| 147 | else |
| 148 | reg_val &= ~(1 << nr); |
Peter Tyser | ba43861 | 2011-03-24 18:17:14 -0500 | [diff] [blame] | 149 | iowrite32(reg_val, &chip->reg->regs[chip->ch].po); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 150 | |
Axel Lin | 02a6794 | 2012-07-29 10:54:42 +0800 | [diff] [blame] | 151 | spin_unlock_irqrestore(&chip->spinlock, flags); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static int ioh_gpio_direction_input(struct gpio_chip *gpio, unsigned nr) |
| 157 | { |
Linus Walleij | 4731557 | 2015-12-07 10:12:05 +0100 | [diff] [blame] | 158 | struct ioh_gpio *chip = gpiochip_get_data(gpio); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 159 | u32 pm; |
Axel Lin | 02a6794 | 2012-07-29 10:54:42 +0800 | [diff] [blame] | 160 | unsigned long flags; |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 161 | |
Axel Lin | 02a6794 | 2012-07-29 10:54:42 +0800 | [diff] [blame] | 162 | spin_lock_irqsave(&chip->spinlock, flags); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 163 | pm = ioread32(&chip->reg->regs[chip->ch].pm) & |
| 164 | ((1 << num_ports[chip->ch]) - 1); |
| 165 | pm &= ~(1 << nr); |
| 166 | iowrite32(pm, &chip->reg->regs[chip->ch].pm); |
Axel Lin | 02a6794 | 2012-07-29 10:54:42 +0800 | [diff] [blame] | 167 | spin_unlock_irqrestore(&chip->spinlock, flags); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Andrew Morton | 545554e | 2011-05-24 17:13:43 -0700 | [diff] [blame] | 172 | #ifdef CONFIG_PM |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 173 | /* |
| 174 | * Save register configuration and disable interrupts. |
| 175 | */ |
| 176 | static void ioh_gpio_save_reg_conf(struct ioh_gpio *chip) |
| 177 | { |
Tomoya MORINAGA | b490fa0 | 2011-08-05 13:04:22 +0900 | [diff] [blame] | 178 | int i; |
| 179 | |
| 180 | for (i = 0; i < 8; i ++, chip++) { |
| 181 | chip->ioh_gpio_reg.po_reg = |
| 182 | ioread32(&chip->reg->regs[chip->ch].po); |
| 183 | chip->ioh_gpio_reg.pm_reg = |
| 184 | ioread32(&chip->reg->regs[chip->ch].pm); |
| 185 | chip->ioh_gpio_reg.ien_reg = |
| 186 | ioread32(&chip->reg->regs[chip->ch].ien); |
| 187 | chip->ioh_gpio_reg.imask_reg = |
| 188 | ioread32(&chip->reg->regs[chip->ch].imask); |
| 189 | chip->ioh_gpio_reg.im0_reg = |
| 190 | ioread32(&chip->reg->regs[chip->ch].im_0); |
| 191 | chip->ioh_gpio_reg.im1_reg = |
| 192 | ioread32(&chip->reg->regs[chip->ch].im_1); |
| 193 | if (i < 4) |
| 194 | chip->ioh_gpio_reg.use_sel_reg = |
| 195 | ioread32(&chip->reg->ioh_sel_reg[i]); |
| 196 | } |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* |
| 200 | * This function restores the register configuration of the GPIO device. |
| 201 | */ |
| 202 | static void ioh_gpio_restore_reg_conf(struct ioh_gpio *chip) |
| 203 | { |
Tomoya MORINAGA | b490fa0 | 2011-08-05 13:04:22 +0900 | [diff] [blame] | 204 | int i; |
| 205 | |
| 206 | for (i = 0; i < 8; i ++, chip++) { |
| 207 | iowrite32(chip->ioh_gpio_reg.po_reg, |
| 208 | &chip->reg->regs[chip->ch].po); |
| 209 | iowrite32(chip->ioh_gpio_reg.pm_reg, |
| 210 | &chip->reg->regs[chip->ch].pm); |
| 211 | iowrite32(chip->ioh_gpio_reg.ien_reg, |
| 212 | &chip->reg->regs[chip->ch].ien); |
| 213 | iowrite32(chip->ioh_gpio_reg.imask_reg, |
| 214 | &chip->reg->regs[chip->ch].imask); |
| 215 | iowrite32(chip->ioh_gpio_reg.im0_reg, |
| 216 | &chip->reg->regs[chip->ch].im_0); |
| 217 | iowrite32(chip->ioh_gpio_reg.im1_reg, |
| 218 | &chip->reg->regs[chip->ch].im_1); |
| 219 | if (i < 4) |
| 220 | iowrite32(chip->ioh_gpio_reg.use_sel_reg, |
| 221 | &chip->reg->ioh_sel_reg[i]); |
| 222 | } |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 223 | } |
Andrew Morton | 545554e | 2011-05-24 17:13:43 -0700 | [diff] [blame] | 224 | #endif |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 225 | |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 226 | static int ioh_gpio_to_irq(struct gpio_chip *gpio, unsigned offset) |
| 227 | { |
Linus Walleij | 4731557 | 2015-12-07 10:12:05 +0100 | [diff] [blame] | 228 | struct ioh_gpio *chip = gpiochip_get_data(gpio); |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 229 | return chip->irq_base + offset; |
| 230 | } |
| 231 | |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 232 | static void ioh_gpio_setup(struct ioh_gpio *chip, int num_port) |
| 233 | { |
| 234 | struct gpio_chip *gpio = &chip->gpio; |
| 235 | |
| 236 | gpio->label = dev_name(chip->dev); |
| 237 | gpio->owner = THIS_MODULE; |
| 238 | gpio->direction_input = ioh_gpio_direction_input; |
| 239 | gpio->get = ioh_gpio_get; |
| 240 | gpio->direction_output = ioh_gpio_direction_output; |
| 241 | gpio->set = ioh_gpio_set; |
| 242 | gpio->dbg_show = NULL; |
| 243 | gpio->base = -1; |
| 244 | gpio->ngpio = num_port; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 245 | gpio->can_sleep = false; |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 246 | gpio->to_irq = ioh_gpio_to_irq; |
| 247 | } |
| 248 | |
| 249 | static int ioh_irq_type(struct irq_data *d, unsigned int type) |
| 250 | { |
| 251 | u32 im; |
Márton Németh | dd9328a | 2012-01-15 10:57:34 +0100 | [diff] [blame] | 252 | void __iomem *im_reg; |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 253 | u32 ien; |
| 254 | u32 im_pos; |
| 255 | int ch; |
| 256 | unsigned long flags; |
| 257 | u32 val; |
| 258 | int irq = d->irq; |
| 259 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 260 | struct ioh_gpio *chip = gc->private; |
| 261 | |
| 262 | ch = irq - chip->irq_base; |
| 263 | if (irq <= chip->irq_base + 7) { |
| 264 | im_reg = &chip->reg->regs[chip->ch].im_0; |
| 265 | im_pos = ch; |
| 266 | } else { |
| 267 | im_reg = &chip->reg->regs[chip->ch].im_1; |
| 268 | im_pos = ch - 8; |
| 269 | } |
| 270 | dev_dbg(chip->dev, "%s:irq=%d type=%d ch=%d pos=%d type=%d\n", |
| 271 | __func__, irq, type, ch, im_pos, type); |
| 272 | |
| 273 | spin_lock_irqsave(&chip->spinlock, flags); |
| 274 | |
| 275 | switch (type) { |
| 276 | case IRQ_TYPE_EDGE_RISING: |
| 277 | val = IOH_EDGE_RISING; |
| 278 | break; |
| 279 | case IRQ_TYPE_EDGE_FALLING: |
| 280 | val = IOH_EDGE_FALLING; |
| 281 | break; |
| 282 | case IRQ_TYPE_EDGE_BOTH: |
| 283 | val = IOH_EDGE_BOTH; |
| 284 | break; |
| 285 | case IRQ_TYPE_LEVEL_HIGH: |
| 286 | val = IOH_LEVEL_H; |
| 287 | break; |
| 288 | case IRQ_TYPE_LEVEL_LOW: |
| 289 | val = IOH_LEVEL_L; |
| 290 | break; |
| 291 | case IRQ_TYPE_PROBE: |
| 292 | goto end; |
| 293 | default: |
| 294 | dev_warn(chip->dev, "%s: unknown type(%dd)", |
| 295 | __func__, type); |
| 296 | goto end; |
| 297 | } |
| 298 | |
| 299 | /* Set interrupt mode */ |
| 300 | im = ioread32(im_reg) & ~(IOH_IM_MASK << (im_pos * 4)); |
| 301 | iowrite32(im | (val << (im_pos * 4)), im_reg); |
| 302 | |
| 303 | /* iclr */ |
| 304 | iowrite32(BIT(ch), &chip->reg->regs[chip->ch].iclr); |
| 305 | |
| 306 | /* IMASKCLR */ |
| 307 | iowrite32(BIT(ch), &chip->reg->regs[chip->ch].imaskclr); |
| 308 | |
| 309 | /* Enable interrupt */ |
| 310 | ien = ioread32(&chip->reg->regs[chip->ch].ien); |
| 311 | iowrite32(ien | BIT(ch), &chip->reg->regs[chip->ch].ien); |
| 312 | end: |
| 313 | spin_unlock_irqrestore(&chip->spinlock, flags); |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | static void ioh_irq_unmask(struct irq_data *d) |
| 319 | { |
| 320 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 321 | struct ioh_gpio *chip = gc->private; |
| 322 | |
| 323 | iowrite32(1 << (d->irq - chip->irq_base), |
| 324 | &chip->reg->regs[chip->ch].imaskclr); |
| 325 | } |
| 326 | |
| 327 | static void ioh_irq_mask(struct irq_data *d) |
| 328 | { |
| 329 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 330 | struct ioh_gpio *chip = gc->private; |
| 331 | |
| 332 | iowrite32(1 << (d->irq - chip->irq_base), |
| 333 | &chip->reg->regs[chip->ch].imask); |
| 334 | } |
| 335 | |
Feng Tang | 4d05221 | 2011-12-13 23:53:50 +0800 | [diff] [blame] | 336 | static void ioh_irq_disable(struct irq_data *d) |
| 337 | { |
| 338 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 339 | struct ioh_gpio *chip = gc->private; |
| 340 | unsigned long flags; |
| 341 | u32 ien; |
| 342 | |
| 343 | spin_lock_irqsave(&chip->spinlock, flags); |
| 344 | ien = ioread32(&chip->reg->regs[chip->ch].ien); |
| 345 | ien &= ~(1 << (d->irq - chip->irq_base)); |
| 346 | iowrite32(ien, &chip->reg->regs[chip->ch].ien); |
| 347 | spin_unlock_irqrestore(&chip->spinlock, flags); |
| 348 | } |
| 349 | |
| 350 | static void ioh_irq_enable(struct irq_data *d) |
| 351 | { |
| 352 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 353 | struct ioh_gpio *chip = gc->private; |
| 354 | unsigned long flags; |
| 355 | u32 ien; |
| 356 | |
| 357 | spin_lock_irqsave(&chip->spinlock, flags); |
| 358 | ien = ioread32(&chip->reg->regs[chip->ch].ien); |
| 359 | ien |= 1 << (d->irq - chip->irq_base); |
| 360 | iowrite32(ien, &chip->reg->regs[chip->ch].ien); |
| 361 | spin_unlock_irqrestore(&chip->spinlock, flags); |
| 362 | } |
| 363 | |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 364 | static irqreturn_t ioh_gpio_handler(int irq, void *dev_id) |
| 365 | { |
| 366 | struct ioh_gpio *chip = dev_id; |
| 367 | u32 reg_val; |
| 368 | int i, j; |
| 369 | int ret = IRQ_NONE; |
| 370 | |
Feng Tang | f9ea14e | 2011-12-13 23:53:49 +0800 | [diff] [blame] | 371 | for (i = 0; i < 8; i++, chip++) { |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 372 | reg_val = ioread32(&chip->reg->regs[i].istatus); |
| 373 | for (j = 0; j < num_ports[i]; j++) { |
| 374 | if (reg_val & BIT(j)) { |
| 375 | dev_dbg(chip->dev, |
| 376 | "%s:[%d]:irq=%d status=0x%x\n", |
| 377 | __func__, j, irq, reg_val); |
| 378 | iowrite32(BIT(j), |
| 379 | &chip->reg->regs[chip->ch].iclr); |
| 380 | generic_handle_irq(chip->irq_base + j); |
| 381 | ret = IRQ_HANDLED; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | return ret; |
| 386 | } |
| 387 | |
Bartosz Golaszewski | e3fe07e | 2017-05-25 10:37:38 +0200 | [diff] [blame] | 388 | static int ioh_gpio_alloc_generic_chip(struct ioh_gpio *chip, |
| 389 | unsigned int irq_start, |
| 390 | unsigned int num) |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 391 | { |
| 392 | struct irq_chip_generic *gc; |
| 393 | struct irq_chip_type *ct; |
Bartosz Golaszewski | 469d594 | 2017-08-09 14:25:04 +0200 | [diff] [blame] | 394 | int rv; |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 395 | |
Bartosz Golaszewski | 469d594 | 2017-08-09 14:25:04 +0200 | [diff] [blame] | 396 | gc = devm_irq_alloc_generic_chip(chip->dev, "ioh_gpio", 1, irq_start, |
| 397 | chip->base, handle_simple_irq); |
Bartosz Golaszewski | e3fe07e | 2017-05-25 10:37:38 +0200 | [diff] [blame] | 398 | if (!gc) |
| 399 | return -ENOMEM; |
| 400 | |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 401 | gc->private = chip; |
| 402 | ct = gc->chip_types; |
| 403 | |
| 404 | ct->chip.irq_mask = ioh_irq_mask; |
| 405 | ct->chip.irq_unmask = ioh_irq_unmask; |
| 406 | ct->chip.irq_set_type = ioh_irq_type; |
Feng Tang | 4d05221 | 2011-12-13 23:53:50 +0800 | [diff] [blame] | 407 | ct->chip.irq_disable = ioh_irq_disable; |
| 408 | ct->chip.irq_enable = ioh_irq_enable; |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 409 | |
Bartosz Golaszewski | 469d594 | 2017-08-09 14:25:04 +0200 | [diff] [blame] | 410 | rv = devm_irq_setup_generic_chip(chip->dev, gc, IRQ_MSK(num), |
| 411 | IRQ_GC_INIT_MASK_CACHE, |
| 412 | IRQ_NOREQUEST | IRQ_NOPROBE, 0); |
Bartosz Golaszewski | e3fe07e | 2017-05-25 10:37:38 +0200 | [diff] [blame] | 413 | |
Bartosz Golaszewski | 469d594 | 2017-08-09 14:25:04 +0200 | [diff] [blame] | 414 | return rv; |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 417 | static int ioh_gpio_probe(struct pci_dev *pdev, |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 418 | const struct pci_device_id *id) |
| 419 | { |
| 420 | int ret; |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 421 | int i, j; |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 422 | struct ioh_gpio *chip; |
| 423 | void __iomem *base; |
Márton Németh | dd9328a | 2012-01-15 10:57:34 +0100 | [diff] [blame] | 424 | void *chip_save; |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 425 | int irq_base; |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 426 | |
| 427 | ret = pci_enable_device(pdev); |
| 428 | if (ret) { |
| 429 | dev_err(&pdev->dev, "%s : pci_enable_device failed", __func__); |
| 430 | goto err_pci_enable; |
| 431 | } |
| 432 | |
| 433 | ret = pci_request_regions(pdev, KBUILD_MODNAME); |
| 434 | if (ret) { |
| 435 | dev_err(&pdev->dev, "pci_request_regions failed-%d", ret); |
| 436 | goto err_request_regions; |
| 437 | } |
| 438 | |
| 439 | base = pci_iomap(pdev, 1, 0); |
Márton Németh | 2bd1c85 | 2012-01-15 10:57:43 +0100 | [diff] [blame] | 440 | if (!base) { |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 441 | dev_err(&pdev->dev, "%s : pci_iomap failed", __func__); |
| 442 | ret = -ENOMEM; |
| 443 | goto err_iomap; |
| 444 | } |
| 445 | |
| 446 | chip_save = kzalloc(sizeof(*chip) * 8, GFP_KERNEL); |
| 447 | if (chip_save == NULL) { |
| 448 | dev_err(&pdev->dev, "%s : kzalloc failed", __func__); |
| 449 | ret = -ENOMEM; |
| 450 | goto err_kzalloc; |
| 451 | } |
| 452 | |
| 453 | chip = chip_save; |
| 454 | for (i = 0; i < 8; i++, chip++) { |
| 455 | chip->dev = &pdev->dev; |
| 456 | chip->base = base; |
| 457 | chip->reg = chip->base; |
| 458 | chip->ch = i; |
Axel Lin | 7e3a70f | 2012-02-01 10:50:05 +0800 | [diff] [blame] | 459 | spin_lock_init(&chip->spinlock); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 460 | ioh_gpio_setup(chip, num_ports[i]); |
Linus Walleij | 4731557 | 2015-12-07 10:12:05 +0100 | [diff] [blame] | 461 | ret = gpiochip_add_data(&chip->gpio, chip); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 462 | if (ret) { |
| 463 | dev_err(&pdev->dev, "IOH gpio: Failed to register GPIO\n"); |
| 464 | goto err_gpiochip_add; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | chip = chip_save; |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 469 | for (j = 0; j < 8; j++, chip++) { |
Bartosz Golaszewski | e971ac9 | 2017-03-04 17:23:33 +0100 | [diff] [blame] | 470 | irq_base = devm_irq_alloc_descs(&pdev->dev, -1, IOH_IRQ_BASE, |
| 471 | num_ports[j], NUMA_NO_NODE); |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 472 | if (irq_base < 0) { |
| 473 | dev_warn(&pdev->dev, |
| 474 | "ml_ioh_gpio: Failed to get IRQ base num\n"); |
Wei Yongjun | df46dce | 2013-05-21 23:11:10 +0800 | [diff] [blame] | 475 | ret = irq_base; |
Bartosz Golaszewski | e971ac9 | 2017-03-04 17:23:33 +0100 | [diff] [blame] | 476 | goto err_gpiochip_add; |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 477 | } |
| 478 | chip->irq_base = irq_base; |
Bartosz Golaszewski | e3fe07e | 2017-05-25 10:37:38 +0200 | [diff] [blame] | 479 | |
| 480 | ret = ioh_gpio_alloc_generic_chip(chip, |
| 481 | irq_base, num_ports[j]); |
| 482 | if (ret) |
| 483 | goto err_gpiochip_add; |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | chip = chip_save; |
Bartosz Golaszewski | e971ac9 | 2017-03-04 17:23:33 +0100 | [diff] [blame] | 487 | ret = devm_request_irq(&pdev->dev, pdev->irq, ioh_gpio_handler, |
| 488 | IRQF_SHARED, KBUILD_MODNAME, chip); |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 489 | if (ret != 0) { |
| 490 | dev_err(&pdev->dev, |
| 491 | "%s request_irq failed\n", __func__); |
Bartosz Golaszewski | e971ac9 | 2017-03-04 17:23:33 +0100 | [diff] [blame] | 492 | goto err_gpiochip_add; |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 493 | } |
| 494 | |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 495 | pci_set_drvdata(pdev, chip); |
| 496 | |
| 497 | return 0; |
| 498 | |
| 499 | err_gpiochip_add: |
Axel Lin | 3330057 | 2011-06-14 19:12:57 +0800 | [diff] [blame] | 500 | while (--i >= 0) { |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 501 | chip--; |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 502 | gpiochip_remove(&chip->gpio); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 503 | } |
| 504 | kfree(chip_save); |
| 505 | |
| 506 | err_kzalloc: |
| 507 | pci_iounmap(pdev, base); |
| 508 | |
| 509 | err_iomap: |
| 510 | pci_release_regions(pdev); |
| 511 | |
| 512 | err_request_regions: |
| 513 | pci_disable_device(pdev); |
| 514 | |
| 515 | err_pci_enable: |
| 516 | |
| 517 | dev_err(&pdev->dev, "%s Failed returns %d\n", __func__, ret); |
| 518 | return ret; |
| 519 | } |
| 520 | |
Bill Pemberton | 206210c | 2012-11-19 13:25:50 -0500 | [diff] [blame] | 521 | static void ioh_gpio_remove(struct pci_dev *pdev) |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 522 | { |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 523 | int i; |
| 524 | struct ioh_gpio *chip = pci_get_drvdata(pdev); |
Márton Németh | dd9328a | 2012-01-15 10:57:34 +0100 | [diff] [blame] | 525 | void *chip_save; |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 526 | |
| 527 | chip_save = chip; |
Tomoya MORINAGA | 54be566 | 2011-08-05 13:04:21 +0900 | [diff] [blame] | 528 | |
Bartosz Golaszewski | e971ac9 | 2017-03-04 17:23:33 +0100 | [diff] [blame] | 529 | for (i = 0; i < 8; i++, chip++) |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 530 | gpiochip_remove(&chip->gpio); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 531 | |
| 532 | chip = chip_save; |
| 533 | pci_iounmap(pdev, chip->base); |
| 534 | pci_release_regions(pdev); |
| 535 | pci_disable_device(pdev); |
| 536 | kfree(chip); |
| 537 | } |
| 538 | |
| 539 | #ifdef CONFIG_PM |
| 540 | static int ioh_gpio_suspend(struct pci_dev *pdev, pm_message_t state) |
| 541 | { |
| 542 | s32 ret; |
| 543 | struct ioh_gpio *chip = pci_get_drvdata(pdev); |
Tomoya MORINAGA | b490fa0 | 2011-08-05 13:04:22 +0900 | [diff] [blame] | 544 | unsigned long flags; |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 545 | |
Tomoya MORINAGA | b490fa0 | 2011-08-05 13:04:22 +0900 | [diff] [blame] | 546 | spin_lock_irqsave(&chip->spinlock, flags); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 547 | ioh_gpio_save_reg_conf(chip); |
Tomoya MORINAGA | b490fa0 | 2011-08-05 13:04:22 +0900 | [diff] [blame] | 548 | spin_unlock_irqrestore(&chip->spinlock, flags); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 549 | |
| 550 | ret = pci_save_state(pdev); |
| 551 | if (ret) { |
| 552 | dev_err(&pdev->dev, "pci_save_state Failed-%d\n", ret); |
| 553 | return ret; |
| 554 | } |
| 555 | pci_disable_device(pdev); |
| 556 | pci_set_power_state(pdev, PCI_D0); |
| 557 | ret = pci_enable_wake(pdev, PCI_D0, 1); |
| 558 | if (ret) |
| 559 | dev_err(&pdev->dev, "pci_enable_wake Failed -%d\n", ret); |
| 560 | |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | static int ioh_gpio_resume(struct pci_dev *pdev) |
| 565 | { |
| 566 | s32 ret; |
| 567 | struct ioh_gpio *chip = pci_get_drvdata(pdev); |
Tomoya MORINAGA | b490fa0 | 2011-08-05 13:04:22 +0900 | [diff] [blame] | 568 | unsigned long flags; |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 569 | |
| 570 | ret = pci_enable_wake(pdev, PCI_D0, 0); |
| 571 | |
| 572 | pci_set_power_state(pdev, PCI_D0); |
| 573 | ret = pci_enable_device(pdev); |
| 574 | if (ret) { |
| 575 | dev_err(&pdev->dev, "pci_enable_device Failed-%d ", ret); |
| 576 | return ret; |
| 577 | } |
| 578 | pci_restore_state(pdev); |
| 579 | |
Tomoya MORINAGA | b490fa0 | 2011-08-05 13:04:22 +0900 | [diff] [blame] | 580 | spin_lock_irqsave(&chip->spinlock, flags); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 581 | iowrite32(0x01, &chip->reg->srst); |
| 582 | iowrite32(0x00, &chip->reg->srst); |
| 583 | ioh_gpio_restore_reg_conf(chip); |
Tomoya MORINAGA | b490fa0 | 2011-08-05 13:04:22 +0900 | [diff] [blame] | 584 | spin_unlock_irqrestore(&chip->spinlock, flags); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | #else |
| 589 | #define ioh_gpio_suspend NULL |
| 590 | #define ioh_gpio_resume NULL |
| 591 | #endif |
| 592 | |
Jingoo Han | 14f4a88 | 2013-12-03 08:08:45 +0900 | [diff] [blame] | 593 | static const struct pci_device_id ioh_gpio_pcidev_id[] = { |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 594 | { PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x802E) }, |
| 595 | { 0, } |
| 596 | }; |
Axel Lin | 19234cd | 2011-03-11 14:58:30 -0800 | [diff] [blame] | 597 | MODULE_DEVICE_TABLE(pci, ioh_gpio_pcidev_id); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 598 | |
| 599 | static struct pci_driver ioh_gpio_driver = { |
| 600 | .name = "ml_ioh_gpio", |
| 601 | .id_table = ioh_gpio_pcidev_id, |
| 602 | .probe = ioh_gpio_probe, |
Bill Pemberton | 8283c4f | 2012-11-19 13:20:08 -0500 | [diff] [blame] | 603 | .remove = ioh_gpio_remove, |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 604 | .suspend = ioh_gpio_suspend, |
| 605 | .resume = ioh_gpio_resume |
| 606 | }; |
| 607 | |
Axel Lin | 93baa65 | 2012-04-06 20:13:30 +0800 | [diff] [blame] | 608 | module_pci_driver(ioh_gpio_driver); |
Tomoya MORINAGA | 49a3679 | 2011-01-12 17:00:22 -0800 | [diff] [blame] | 609 | |
| 610 | MODULE_DESCRIPTION("OKI SEMICONDUCTOR ML-IOH series GPIO Driver"); |
| 611 | MODULE_LICENSE("GPL"); |