2 * Copyright 2015 IBM Corp.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
16 #include <linux/spinlock.h>
17 #include <linux/platform_device.h>
18 #include <linux/gpio/driver.h>
19 #include <linux/pinctrl/consumer.h>
22 struct gpio_chip chip;
28 struct aspeed_gpio_bank {
34 static const struct aspeed_gpio_bank aspeed_gpio_banks[] = {
38 .names = { 'A', 'B', 'C', 'D' },
43 .names = { 'E', 'F', 'G', 'H' },
48 .names = { 'I', 'J', 'K', 'L' },
53 .names = { 'M', 'N', 'O', 'P' },
58 .names = { 'Q', 'R', 'S', 'T' },
63 .names = { 'U', 'V', 'W', 'X' },
66 * A bank exists for { 'Y', 'Z', "AA", "AB" }, but is not implemented.
67 * Only half of GPIOs Y support interrupt configuration, and none of Z,
68 * AA or AB do as they are output only.
72 #define GPIO_BANK(x) ((x) >> 5)
73 #define GPIO_OFFSET(x) ((x) & 0x1f)
74 #define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
76 #define GPIO_DATA 0x00
79 #define GPIO_IRQ_ENABLE 0x00
80 #define GPIO_IRQ_TYPE0 0x04
81 #define GPIO_IRQ_TYPE1 0x08
82 #define GPIO_IRQ_TYPE2 0x0c
83 #define GPIO_IRQ_STATUS 0x10
85 static const struct aspeed_gpio_bank *to_bank(unsigned int offset)
87 unsigned int bank = GPIO_BANK(offset);
89 WARN_ON(bank > ARRAY_SIZE(aspeed_gpio_banks));
90 return &aspeed_gpio_banks[bank];
93 static void __iomem *bank_val_reg(struct aspeed_gpio *gpio,
94 const struct aspeed_gpio_bank *bank,
97 return gpio->base + bank->val_regs + reg;
100 static void __iomem *bank_irq_reg(struct aspeed_gpio *gpio,
101 const struct aspeed_gpio_bank *bank,
104 return gpio->base + bank->irq_regs + reg;
107 static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset)
109 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
110 const struct aspeed_gpio_bank *bank = to_bank(offset);
112 return !!(ioread32(bank_val_reg(gpio, bank, GPIO_DATA))
116 static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
119 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
120 const struct aspeed_gpio_bank *bank = to_bank(offset);
124 addr = bank_val_reg(gpio, bank, GPIO_DATA);
125 reg = ioread32(addr);
128 reg |= GPIO_BIT(offset);
130 reg &= ~GPIO_BIT(offset);
132 iowrite32(reg, addr);
135 static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
138 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
141 spin_lock_irqsave(&gpio->lock, flags);
143 __aspeed_gpio_set(gc, offset, val);
145 spin_unlock_irqrestore(&gpio->lock, flags);
148 static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
150 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
151 const struct aspeed_gpio_bank *bank = to_bank(offset);
155 spin_lock_irqsave(&gpio->lock, flags);
157 reg = ioread32(bank_val_reg(gpio, bank, GPIO_DIR));
158 iowrite32(reg & ~GPIO_BIT(offset), bank_val_reg(gpio, bank, GPIO_DIR));
160 spin_unlock_irqrestore(&gpio->lock, flags);
165 static int aspeed_gpio_dir_out(struct gpio_chip *gc,
166 unsigned int offset, int val)
168 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
169 const struct aspeed_gpio_bank *bank = to_bank(offset);
173 spin_lock_irqsave(&gpio->lock, flags);
175 reg = ioread32(bank_val_reg(gpio, bank, GPIO_DIR));
176 iowrite32(reg | GPIO_BIT(offset), bank_val_reg(gpio, bank, GPIO_DIR));
178 __aspeed_gpio_set(gc, offset, val);
180 spin_unlock_irqrestore(&gpio->lock, flags);
185 static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
187 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
188 const struct aspeed_gpio_bank *bank = to_bank(offset);
192 spin_lock_irqsave(&gpio->lock, flags);
194 val = ioread32(bank_val_reg(gpio, bank, GPIO_DIR)) & GPIO_BIT(offset);
196 spin_unlock_irqrestore(&gpio->lock, flags);
202 static inline int irqd_to_aspeed_gpio_data(struct irq_data *d,
203 struct aspeed_gpio **gpio,
204 const struct aspeed_gpio_bank **bank,
209 offset = irqd_to_hwirq(d);
211 *gpio = irq_data_get_irq_chip_data(d);
212 *bank = to_bank(offset);
213 *bit = GPIO_BIT(offset);
218 static void aspeed_gpio_irq_ack(struct irq_data *d)
220 const struct aspeed_gpio_bank *bank;
221 struct aspeed_gpio *gpio;
223 void __iomem *status_addr;
227 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
231 status_addr = bank_irq_reg(gpio, bank, GPIO_IRQ_STATUS);
233 spin_lock_irqsave(&gpio->lock, flags);
234 iowrite32(bit, status_addr);
235 spin_unlock_irqrestore(&gpio->lock, flags);
238 static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set)
240 const struct aspeed_gpio_bank *bank;
241 struct aspeed_gpio *gpio;
247 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
251 addr = bank_irq_reg(gpio, bank, GPIO_IRQ_ENABLE);
253 spin_lock_irqsave(&gpio->lock, flags);
255 reg = ioread32(addr);
260 iowrite32(reg, addr);
262 spin_unlock_irqrestore(&gpio->lock, flags);
265 static void aspeed_gpio_irq_mask(struct irq_data *d)
267 aspeed_gpio_irq_set_mask(d, false);
270 static void aspeed_gpio_irq_unmask(struct irq_data *d)
272 aspeed_gpio_irq_set_mask(d, true);
275 static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type)
281 const struct aspeed_gpio_bank *bank;
282 irq_flow_handler_t handler;
283 struct aspeed_gpio *gpio;
288 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
292 switch (type & IRQ_TYPE_SENSE_MASK) {
293 case IRQ_TYPE_EDGE_BOTH:
295 case IRQ_TYPE_EDGE_RISING:
297 case IRQ_TYPE_EDGE_FALLING:
298 handler = handle_edge_irq;
300 case IRQ_TYPE_LEVEL_HIGH:
302 case IRQ_TYPE_LEVEL_LOW:
304 handler = handle_level_irq;
310 spin_lock_irqsave(&gpio->lock, flags);
312 addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE0);
313 reg = ioread32(addr);
314 reg = (reg & ~bit) | type0;
315 iowrite32(reg, addr);
317 addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE1);
318 reg = ioread32(addr);
319 reg = (reg & ~bit) | type1;
320 iowrite32(reg, addr);
322 addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE2);
323 reg = ioread32(addr);
324 reg = (reg & ~bit) | type2;
325 iowrite32(reg, addr);
327 spin_unlock_irqrestore(&gpio->lock, flags);
329 irq_set_handler_locked(d, handler);
334 static void aspeed_gpio_irq_handler(struct irq_desc *desc)
336 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
337 struct irq_chip *ic = irq_desc_get_chip(desc);
338 struct aspeed_gpio *data = gpiochip_get_data(gc);
339 unsigned int i, p, girq;
342 chained_irq_enter(ic, desc);
344 for (i = 0; i < ARRAY_SIZE(aspeed_gpio_banks); i++) {
345 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
347 reg = ioread32(bank_irq_reg(data, bank, GPIO_IRQ_STATUS));
349 for_each_set_bit(p, ®, 32) {
350 girq = irq_find_mapping(gc->irqdomain, i * 32 + p);
351 generic_handle_irq(girq);
356 chained_irq_exit(ic, desc);
359 static struct irq_chip aspeed_gpio_irqchip = {
360 .name = "aspeed-gpio",
361 .irq_ack = aspeed_gpio_irq_ack,
362 .irq_mask = aspeed_gpio_irq_mask,
363 .irq_unmask = aspeed_gpio_irq_unmask,
364 .irq_set_type = aspeed_gpio_set_type,
367 static int aspeed_gpio_setup_irqs(struct aspeed_gpio *gpio,
368 struct platform_device *pdev)
372 rc = platform_get_irq(pdev, 0);
378 rc = gpiochip_irqchip_add(&gpio->chip, &aspeed_gpio_irqchip,
379 0, handle_bad_irq, IRQ_TYPE_NONE);
381 dev_info(&pdev->dev, "Could not add irqchip\n");
385 gpiochip_set_chained_irqchip(&gpio->chip, &aspeed_gpio_irqchip,
386 gpio->irq, aspeed_gpio_irq_handler);
391 static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset)
393 return pinctrl_request_gpio(chip->base + offset);
396 static void aspeed_gpio_free(struct gpio_chip *chip, unsigned int offset)
398 pinctrl_free_gpio(chip->base + offset);
401 static int __init aspeed_gpio_probe(struct platform_device *pdev)
403 struct aspeed_gpio *gpio;
404 struct resource *res;
407 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
411 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
412 gpio->base = devm_ioremap_resource(&pdev->dev, res);
413 if (IS_ERR(gpio->base))
414 return PTR_ERR(gpio->base);
416 spin_lock_init(&gpio->lock);
418 gpio->chip.ngpio = ARRAY_SIZE(aspeed_gpio_banks) * 32;
420 gpio->chip.parent = &pdev->dev;
421 gpio->chip.direction_input = aspeed_gpio_dir_in;
422 gpio->chip.direction_output = aspeed_gpio_dir_out;
423 gpio->chip.get_direction = aspeed_gpio_get_direction;
424 gpio->chip.request = aspeed_gpio_request;
425 gpio->chip.free = aspeed_gpio_free;
426 gpio->chip.get = aspeed_gpio_get;
427 gpio->chip.set = aspeed_gpio_set;
428 gpio->chip.label = dev_name(&pdev->dev);
429 gpio->chip.base = -1;
431 rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
435 return aspeed_gpio_setup_irqs(gpio, pdev);
438 static const struct of_device_id aspeed_gpio_of_table[] = {
439 { .compatible = "aspeed,ast2400-gpio" },
440 { .compatible = "aspeed,ast2500-gpio" },
443 MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table);
445 static struct platform_driver aspeed_gpio_driver = {
447 .name = KBUILD_MODNAME,
448 .of_match_table = aspeed_gpio_of_table,
452 module_platform_driver_probe(aspeed_gpio_driver, aspeed_gpio_probe);
454 MODULE_DESCRIPTION("Aspeed GPIO Driver");
455 MODULE_LICENSE("GPL");