Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Delta TN48M CPLD GPIO driver
0004  *
0005  * Copyright (C) 2021 Sartura Ltd.
0006  *
0007  * Author: Robert Marko <robert.marko@sartura.hr>
0008  */
0009 
0010 #include <linux/device.h>
0011 #include <linux/gpio/driver.h>
0012 #include <linux/gpio/regmap.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 enum tn48m_gpio_type {
0019     TN48M_GP0 = 1,
0020     TN48M_GPI,
0021 };
0022 
0023 struct tn48m_gpio_config {
0024     int ngpio;
0025     int ngpio_per_reg;
0026     enum tn48m_gpio_type type;
0027 };
0028 
0029 static const struct tn48m_gpio_config tn48m_gpo_config = {
0030     .ngpio = 4,
0031     .ngpio_per_reg = 4,
0032     .type = TN48M_GP0,
0033 };
0034 
0035 static const struct tn48m_gpio_config tn48m_gpi_config = {
0036     .ngpio = 4,
0037     .ngpio_per_reg = 4,
0038     .type = TN48M_GPI,
0039 };
0040 
0041 static int tn48m_gpio_probe(struct platform_device *pdev)
0042 {
0043     const struct tn48m_gpio_config *gpio_config;
0044     struct gpio_regmap_config config = {};
0045     struct regmap *regmap;
0046     u32 base;
0047     int ret;
0048 
0049     if (!pdev->dev.parent)
0050         return -ENODEV;
0051 
0052     gpio_config = device_get_match_data(&pdev->dev);
0053     if (!gpio_config)
0054         return -ENODEV;
0055 
0056     ret = device_property_read_u32(&pdev->dev, "reg", &base);
0057     if (ret)
0058         return ret;
0059 
0060     regmap = dev_get_regmap(pdev->dev.parent, NULL);
0061     if (!regmap)
0062         return -ENODEV;
0063 
0064     config.regmap = regmap;
0065     config.parent = &pdev->dev;
0066     config.ngpio = gpio_config->ngpio;
0067     config.ngpio_per_reg = gpio_config->ngpio_per_reg;
0068     switch (gpio_config->type) {
0069     case TN48M_GP0:
0070         config.reg_set_base = base;
0071         break;
0072     case TN48M_GPI:
0073         config.reg_dat_base = base;
0074         break;
0075     default:
0076         return -EINVAL;
0077     }
0078 
0079     return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(&pdev->dev, &config));
0080 }
0081 
0082 static const struct of_device_id tn48m_gpio_of_match[] = {
0083     { .compatible = "delta,tn48m-gpo", .data = &tn48m_gpo_config },
0084     { .compatible = "delta,tn48m-gpi", .data = &tn48m_gpi_config },
0085     { }
0086 };
0087 MODULE_DEVICE_TABLE(of, tn48m_gpio_of_match);
0088 
0089 static struct platform_driver tn48m_gpio_driver = {
0090     .driver = {
0091         .name = "delta-tn48m-gpio",
0092         .of_match_table = tn48m_gpio_of_match,
0093     },
0094     .probe = tn48m_gpio_probe,
0095 };
0096 module_platform_driver(tn48m_gpio_driver);
0097 
0098 MODULE_AUTHOR("Robert Marko <robert.marko@sartura.hr>");
0099 MODULE_DESCRIPTION("Delta TN48M CPLD GPIO driver");
0100 MODULE_LICENSE("GPL");