Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Abilis Systems MODULE DESCRIPTION
0003  *
0004  * Copyright (C) Abilis Systems 2013
0005  *
0006  * Authors: Sascha Leuenberger <sascha.leuenberger@abilis.com>
0007  *          Christian Ruppert <christian.ruppert@abilis.com>
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/gpio/driver.h>
0014 #include <linux/slab.h>
0015 #include <linux/irq.h>
0016 #include <linux/irqdomain.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/io.h>
0019 #include <linux/of.h>
0020 #include <linux/of_platform.h>
0021 #include <linux/spinlock.h>
0022 #include <linux/bitops.h>
0023 #include <linux/pinctrl/consumer.h>
0024 
0025 #define TB10X_GPIO_DIR_IN   (0x00000000)
0026 #define TB10X_GPIO_DIR_OUT  (0x00000001)
0027 #define OFFSET_TO_REG_DDR   (0x00)
0028 #define OFFSET_TO_REG_DATA  (0x04)
0029 #define OFFSET_TO_REG_INT_EN    (0x08)
0030 #define OFFSET_TO_REG_CHANGE    (0x0C)
0031 #define OFFSET_TO_REG_WRMASK    (0x10)
0032 #define OFFSET_TO_REG_INT_TYPE  (0x14)
0033 
0034 
0035 /**
0036  * @base: register base address
0037  * @domain: IRQ domain of GPIO generated interrupts managed by this controller
0038  * @irq: Interrupt line of parent interrupt controller
0039  * @gc: gpio_chip structure associated to this GPIO controller
0040  */
0041 struct tb10x_gpio {
0042     void __iomem *base;
0043     struct irq_domain *domain;
0044     int irq;
0045     struct gpio_chip gc;
0046 };
0047 
0048 static inline u32 tb10x_reg_read(struct tb10x_gpio *gpio, unsigned int offs)
0049 {
0050     return ioread32(gpio->base + offs);
0051 }
0052 
0053 static inline void tb10x_reg_write(struct tb10x_gpio *gpio, unsigned int offs,
0054                 u32 val)
0055 {
0056     iowrite32(val, gpio->base + offs);
0057 }
0058 
0059 static inline void tb10x_set_bits(struct tb10x_gpio *gpio, unsigned int offs,
0060                 u32 mask, u32 val)
0061 {
0062     u32 r;
0063     unsigned long flags;
0064 
0065     raw_spin_lock_irqsave(&gpio->gc.bgpio_lock, flags);
0066 
0067     r = tb10x_reg_read(gpio, offs);
0068     r = (r & ~mask) | (val & mask);
0069 
0070     tb10x_reg_write(gpio, offs, r);
0071 
0072     raw_spin_unlock_irqrestore(&gpio->gc.bgpio_lock, flags);
0073 }
0074 
0075 static int tb10x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
0076 {
0077     struct tb10x_gpio *tb10x_gpio = gpiochip_get_data(chip);
0078 
0079     return irq_create_mapping(tb10x_gpio->domain, offset);
0080 }
0081 
0082 static int tb10x_gpio_irq_set_type(struct irq_data *data, unsigned int type)
0083 {
0084     if ((type & IRQF_TRIGGER_MASK) != IRQ_TYPE_EDGE_BOTH) {
0085         pr_err("Only (both) edge triggered interrupts supported.\n");
0086         return -EINVAL;
0087     }
0088 
0089     irqd_set_trigger_type(data, type);
0090 
0091     return IRQ_SET_MASK_OK;
0092 }
0093 
0094 static irqreturn_t tb10x_gpio_irq_cascade(int irq, void *data)
0095 {
0096     struct tb10x_gpio *tb10x_gpio = data;
0097     u32 r = tb10x_reg_read(tb10x_gpio, OFFSET_TO_REG_CHANGE);
0098     u32 m = tb10x_reg_read(tb10x_gpio, OFFSET_TO_REG_INT_EN);
0099     const unsigned long bits = r & m;
0100     int i;
0101 
0102     for_each_set_bit(i, &bits, 32)
0103         generic_handle_domain_irq(tb10x_gpio->domain, i);
0104 
0105     return IRQ_HANDLED;
0106 }
0107 
0108 static int tb10x_gpio_probe(struct platform_device *pdev)
0109 {
0110     struct tb10x_gpio *tb10x_gpio;
0111     struct device *dev = &pdev->dev;
0112     struct device_node *np = dev->of_node;
0113     int ret = -EBUSY;
0114     u32 ngpio;
0115 
0116     if (!np)
0117         return -EINVAL;
0118 
0119     if (of_property_read_u32(np, "abilis,ngpio", &ngpio))
0120         return -EINVAL;
0121 
0122     tb10x_gpio = devm_kzalloc(dev, sizeof(*tb10x_gpio), GFP_KERNEL);
0123     if (tb10x_gpio == NULL)
0124         return -ENOMEM;
0125 
0126     tb10x_gpio->base = devm_platform_ioremap_resource(pdev, 0);
0127     if (IS_ERR(tb10x_gpio->base))
0128         return PTR_ERR(tb10x_gpio->base);
0129 
0130     tb10x_gpio->gc.label =
0131         devm_kasprintf(dev, GFP_KERNEL, "%pOF", pdev->dev.of_node);
0132     if (!tb10x_gpio->gc.label)
0133         return -ENOMEM;
0134 
0135     /*
0136      * Initialize generic GPIO with one single register for reading and setting
0137      * the lines, no special set or clear registers and a data direction register
0138      * wher 1 means "output".
0139      */
0140     ret = bgpio_init(&tb10x_gpio->gc, dev, 4,
0141              tb10x_gpio->base + OFFSET_TO_REG_DATA,
0142              NULL,
0143              NULL,
0144              tb10x_gpio->base + OFFSET_TO_REG_DDR,
0145              NULL,
0146              0);
0147     if (ret) {
0148         dev_err(dev, "unable to init generic GPIO\n");
0149         return ret;
0150     }
0151     tb10x_gpio->gc.base = -1;
0152     tb10x_gpio->gc.parent = dev;
0153     tb10x_gpio->gc.owner = THIS_MODULE;
0154     /*
0155      * ngpio is set by bgpio_init() but we override it, this .request()
0156      * callback also overrides the one set up by generic GPIO.
0157      */
0158     tb10x_gpio->gc.ngpio = ngpio;
0159     tb10x_gpio->gc.request = gpiochip_generic_request;
0160     tb10x_gpio->gc.free = gpiochip_generic_free;
0161 
0162     ret = devm_gpiochip_add_data(dev, &tb10x_gpio->gc, tb10x_gpio);
0163     if (ret < 0) {
0164         dev_err(dev, "Could not add gpiochip.\n");
0165         return ret;
0166     }
0167 
0168     platform_set_drvdata(pdev, tb10x_gpio);
0169 
0170     if (of_find_property(np, "interrupt-controller", NULL)) {
0171         struct irq_chip_generic *gc;
0172 
0173         ret = platform_get_irq(pdev, 0);
0174         if (ret < 0)
0175             return ret;
0176 
0177         tb10x_gpio->gc.to_irq   = tb10x_gpio_to_irq;
0178         tb10x_gpio->irq     = ret;
0179 
0180         ret = devm_request_irq(dev, ret, tb10x_gpio_irq_cascade,
0181                 IRQF_TRIGGER_NONE | IRQF_SHARED,
0182                 dev_name(dev), tb10x_gpio);
0183         if (ret != 0)
0184             return ret;
0185 
0186         tb10x_gpio->domain = irq_domain_add_linear(np,
0187                         tb10x_gpio->gc.ngpio,
0188                         &irq_generic_chip_ops, NULL);
0189         if (!tb10x_gpio->domain) {
0190             return -ENOMEM;
0191         }
0192 
0193         ret = irq_alloc_domain_generic_chips(tb10x_gpio->domain,
0194                 tb10x_gpio->gc.ngpio, 1, tb10x_gpio->gc.label,
0195                 handle_edge_irq, IRQ_NOREQUEST, IRQ_NOPROBE,
0196                 IRQ_GC_INIT_MASK_CACHE);
0197         if (ret)
0198             return ret;
0199 
0200         gc = tb10x_gpio->domain->gc->gc[0];
0201         gc->reg_base                         = tb10x_gpio->base;
0202         gc->chip_types[0].type               = IRQ_TYPE_EDGE_BOTH;
0203         gc->chip_types[0].chip.irq_ack       = irq_gc_ack_set_bit;
0204         gc->chip_types[0].chip.irq_mask      = irq_gc_mask_clr_bit;
0205         gc->chip_types[0].chip.irq_unmask    = irq_gc_mask_set_bit;
0206         gc->chip_types[0].chip.irq_set_type  = tb10x_gpio_irq_set_type;
0207         gc->chip_types[0].regs.ack           = OFFSET_TO_REG_CHANGE;
0208         gc->chip_types[0].regs.mask          = OFFSET_TO_REG_INT_EN;
0209     }
0210 
0211     return 0;
0212 }
0213 
0214 static int tb10x_gpio_remove(struct platform_device *pdev)
0215 {
0216     struct tb10x_gpio *tb10x_gpio = platform_get_drvdata(pdev);
0217 
0218     if (tb10x_gpio->gc.to_irq) {
0219         irq_remove_generic_chip(tb10x_gpio->domain->gc->gc[0],
0220                     BIT(tb10x_gpio->gc.ngpio) - 1, 0, 0);
0221         kfree(tb10x_gpio->domain->gc);
0222         irq_domain_remove(tb10x_gpio->domain);
0223     }
0224 
0225     return 0;
0226 }
0227 
0228 static const struct of_device_id tb10x_gpio_dt_ids[] = {
0229     { .compatible = "abilis,tb10x-gpio" },
0230     { }
0231 };
0232 MODULE_DEVICE_TABLE(of, tb10x_gpio_dt_ids);
0233 
0234 static struct platform_driver tb10x_gpio_driver = {
0235     .probe      = tb10x_gpio_probe,
0236     .remove     = tb10x_gpio_remove,
0237     .driver = {
0238         .name   = "tb10x-gpio",
0239         .of_match_table = tb10x_gpio_dt_ids,
0240     }
0241 };
0242 
0243 module_platform_driver(tb10x_gpio_driver);
0244 MODULE_LICENSE("GPL");
0245 MODULE_DESCRIPTION("tb10x gpio.");