Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * sl28cpld GPIO driver
0004  *
0005  * Copyright 2020 Michael Walle <michael@walle.cc>
0006  */
0007 
0008 #include <linux/device.h>
0009 #include <linux/gpio/driver.h>
0010 #include <linux/gpio/regmap.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/kernel.h>
0013 #include <linux/mod_devicetable.h>
0014 #include <linux/module.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/regmap.h>
0017 
0018 /* GPIO flavor */
0019 #define GPIO_REG_DIR    0x00
0020 #define GPIO_REG_OUT    0x01
0021 #define GPIO_REG_IN 0x02
0022 #define GPIO_REG_IE 0x03
0023 #define GPIO_REG_IP 0x04
0024 
0025 /* input-only flavor */
0026 #define GPI_REG_IN  0x00
0027 
0028 /* output-only flavor */
0029 #define GPO_REG_OUT 0x00
0030 
0031 enum sl28cpld_gpio_type {
0032     SL28CPLD_GPIO = 1,
0033     SL28CPLD_GPI,
0034     SL28CPLD_GPO,
0035 };
0036 
0037 static const struct regmap_irq sl28cpld_gpio_irqs[] = {
0038     REGMAP_IRQ_REG_LINE(0, 8),
0039     REGMAP_IRQ_REG_LINE(1, 8),
0040     REGMAP_IRQ_REG_LINE(2, 8),
0041     REGMAP_IRQ_REG_LINE(3, 8),
0042     REGMAP_IRQ_REG_LINE(4, 8),
0043     REGMAP_IRQ_REG_LINE(5, 8),
0044     REGMAP_IRQ_REG_LINE(6, 8),
0045     REGMAP_IRQ_REG_LINE(7, 8),
0046 };
0047 
0048 static int sl28cpld_gpio_irq_init(struct platform_device *pdev,
0049                   unsigned int base,
0050                   struct gpio_regmap_config *config)
0051 {
0052     struct regmap_irq_chip_data *irq_data;
0053     struct regmap_irq_chip *irq_chip;
0054     struct device *dev = &pdev->dev;
0055     int irq, ret;
0056 
0057     if (!device_property_read_bool(dev, "interrupt-controller"))
0058         return 0;
0059 
0060     irq = platform_get_irq(pdev, 0);
0061     if (irq < 0)
0062         return irq;
0063 
0064     irq_chip = devm_kzalloc(dev, sizeof(*irq_chip), GFP_KERNEL);
0065     if (!irq_chip)
0066         return -ENOMEM;
0067 
0068     irq_chip->name = "sl28cpld-gpio-irq";
0069     irq_chip->irqs = sl28cpld_gpio_irqs;
0070     irq_chip->num_irqs = ARRAY_SIZE(sl28cpld_gpio_irqs);
0071     irq_chip->num_regs = 1;
0072     irq_chip->status_base = base + GPIO_REG_IP;
0073     irq_chip->mask_base = base + GPIO_REG_IE;
0074     irq_chip->mask_invert = true;
0075     irq_chip->ack_base = base + GPIO_REG_IP;
0076 
0077     ret = devm_regmap_add_irq_chip_fwnode(dev, dev_fwnode(dev),
0078                           config->regmap, irq,
0079                           IRQF_SHARED | IRQF_ONESHOT,
0080                           0, irq_chip, &irq_data);
0081     if (ret)
0082         return ret;
0083 
0084     config->irq_domain = regmap_irq_get_domain(irq_data);
0085 
0086     return 0;
0087 }
0088 
0089 static int sl28cpld_gpio_probe(struct platform_device *pdev)
0090 {
0091     struct gpio_regmap_config config = {0};
0092     enum sl28cpld_gpio_type type;
0093     struct regmap *regmap;
0094     u32 base;
0095     int ret;
0096 
0097     if (!pdev->dev.parent)
0098         return -ENODEV;
0099 
0100     type = (uintptr_t)device_get_match_data(&pdev->dev);
0101     if (!type)
0102         return -ENODEV;
0103 
0104     ret = device_property_read_u32(&pdev->dev, "reg", &base);
0105     if (ret)
0106         return -EINVAL;
0107 
0108     regmap = dev_get_regmap(pdev->dev.parent, NULL);
0109     if (!regmap)
0110         return -ENODEV;
0111 
0112     config.regmap = regmap;
0113     config.parent = &pdev->dev;
0114     config.ngpio = 8;
0115 
0116     switch (type) {
0117     case SL28CPLD_GPIO:
0118         config.reg_dat_base = base + GPIO_REG_IN;
0119         config.reg_set_base = base + GPIO_REG_OUT;
0120         /* reg_dir_out_base might be zero */
0121         config.reg_dir_out_base = GPIO_REGMAP_ADDR(base + GPIO_REG_DIR);
0122 
0123         /* This type supports interrupts */
0124         ret = sl28cpld_gpio_irq_init(pdev, base, &config);
0125         if (ret)
0126             return ret;
0127         break;
0128     case SL28CPLD_GPO:
0129         config.reg_set_base = base + GPO_REG_OUT;
0130         break;
0131     case SL28CPLD_GPI:
0132         config.reg_dat_base = base + GPI_REG_IN;
0133         break;
0134     default:
0135         dev_err(&pdev->dev, "unknown type %d\n", type);
0136         return -ENODEV;
0137     }
0138 
0139     return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(&pdev->dev, &config));
0140 }
0141 
0142 static const struct of_device_id sl28cpld_gpio_of_match[] = {
0143     { .compatible = "kontron,sl28cpld-gpio", .data = (void *)SL28CPLD_GPIO },
0144     { .compatible = "kontron,sl28cpld-gpi", .data = (void *)SL28CPLD_GPI },
0145     { .compatible = "kontron,sl28cpld-gpo", .data = (void *)SL28CPLD_GPO },
0146     {}
0147 };
0148 MODULE_DEVICE_TABLE(of, sl28cpld_gpio_of_match);
0149 
0150 static struct platform_driver sl28cpld_gpio_driver = {
0151     .probe = sl28cpld_gpio_probe,
0152     .driver = {
0153         .name = "sl28cpld-gpio",
0154         .of_match_table = sl28cpld_gpio_of_match,
0155     },
0156 };
0157 module_platform_driver(sl28cpld_gpio_driver);
0158 
0159 MODULE_DESCRIPTION("sl28cpld GPIO Driver");
0160 MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
0161 MODULE_LICENSE("GPL");