Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * GPIO driver for TPS68470 PMIC
0004  *
0005  * Copyright (C) 2017 Intel Corporation
0006  *
0007  * Authors:
0008  *  Antti Laakso <antti.laakso@intel.com>
0009  *  Tianshu Qiu <tian.shu.qiu@intel.com>
0010  *  Jian Xu Zheng <jian.xu.zheng@intel.com>
0011  *  Yuning Pu <yuning.pu@intel.com>
0012  */
0013 
0014 #include <linux/gpio/driver.h>
0015 #include <linux/mfd/tps68470.h>
0016 #include <linux/module.h>
0017 #include <linux/platform_device.h>
0018 #include <linux/regmap.h>
0019 
0020 #define TPS68470_N_LOGIC_OUTPUT 3
0021 #define TPS68470_N_REGULAR_GPIO 7
0022 #define TPS68470_N_GPIO (TPS68470_N_LOGIC_OUTPUT + TPS68470_N_REGULAR_GPIO)
0023 
0024 struct tps68470_gpio_data {
0025     struct regmap *tps68470_regmap;
0026     struct gpio_chip gc;
0027 };
0028 
0029 static int tps68470_gpio_get(struct gpio_chip *gc, unsigned int offset)
0030 {
0031     struct tps68470_gpio_data *tps68470_gpio = gpiochip_get_data(gc);
0032     struct regmap *regmap = tps68470_gpio->tps68470_regmap;
0033     unsigned int reg = TPS68470_REG_GPDO;
0034     int val, ret;
0035 
0036     if (offset >= TPS68470_N_REGULAR_GPIO) {
0037         offset -= TPS68470_N_REGULAR_GPIO;
0038         reg = TPS68470_REG_SGPO;
0039     }
0040 
0041     ret = regmap_read(regmap, reg, &val);
0042     if (ret) {
0043         dev_err(tps68470_gpio->gc.parent, "reg 0x%x read failed\n",
0044             TPS68470_REG_SGPO);
0045         return ret;
0046     }
0047     return !!(val & BIT(offset));
0048 }
0049 
0050 static int tps68470_gpio_get_direction(struct gpio_chip *gc,
0051                        unsigned int offset)
0052 {
0053     struct tps68470_gpio_data *tps68470_gpio = gpiochip_get_data(gc);
0054     struct regmap *regmap = tps68470_gpio->tps68470_regmap;
0055     int val, ret;
0056 
0057     /* rest are always outputs */
0058     if (offset >= TPS68470_N_REGULAR_GPIO)
0059         return GPIO_LINE_DIRECTION_OUT;
0060 
0061     ret = regmap_read(regmap, TPS68470_GPIO_CTL_REG_A(offset), &val);
0062     if (ret) {
0063         dev_err(tps68470_gpio->gc.parent, "reg 0x%x read failed\n",
0064             TPS68470_GPIO_CTL_REG_A(offset));
0065         return ret;
0066     }
0067 
0068     val &= TPS68470_GPIO_MODE_MASK;
0069     return val >= TPS68470_GPIO_MODE_OUT_CMOS ? GPIO_LINE_DIRECTION_OUT :
0070                             GPIO_LINE_DIRECTION_IN;
0071 }
0072 
0073 static void tps68470_gpio_set(struct gpio_chip *gc, unsigned int offset,
0074                 int value)
0075 {
0076     struct tps68470_gpio_data *tps68470_gpio = gpiochip_get_data(gc);
0077     struct regmap *regmap = tps68470_gpio->tps68470_regmap;
0078     unsigned int reg = TPS68470_REG_GPDO;
0079 
0080     if (offset >= TPS68470_N_REGULAR_GPIO) {
0081         reg = TPS68470_REG_SGPO;
0082         offset -= TPS68470_N_REGULAR_GPIO;
0083     }
0084 
0085     regmap_update_bits(regmap, reg, BIT(offset), value ? BIT(offset) : 0);
0086 }
0087 
0088 static int tps68470_gpio_output(struct gpio_chip *gc, unsigned int offset,
0089                 int value)
0090 {
0091     struct tps68470_gpio_data *tps68470_gpio = gpiochip_get_data(gc);
0092     struct regmap *regmap = tps68470_gpio->tps68470_regmap;
0093 
0094     /* rest are always outputs */
0095     if (offset >= TPS68470_N_REGULAR_GPIO)
0096         return 0;
0097 
0098     /* Set the initial value */
0099     tps68470_gpio_set(gc, offset, value);
0100 
0101     return regmap_update_bits(regmap, TPS68470_GPIO_CTL_REG_A(offset),
0102                  TPS68470_GPIO_MODE_MASK,
0103                  TPS68470_GPIO_MODE_OUT_CMOS);
0104 }
0105 
0106 static int tps68470_gpio_input(struct gpio_chip *gc, unsigned int offset)
0107 {
0108     struct tps68470_gpio_data *tps68470_gpio = gpiochip_get_data(gc);
0109     struct regmap *regmap = tps68470_gpio->tps68470_regmap;
0110 
0111     /* rest are always outputs */
0112     if (offset >= TPS68470_N_REGULAR_GPIO)
0113         return -EINVAL;
0114 
0115     return regmap_update_bits(regmap, TPS68470_GPIO_CTL_REG_A(offset),
0116                    TPS68470_GPIO_MODE_MASK, 0x00);
0117 }
0118 
0119 static const char *tps68470_names[TPS68470_N_GPIO] = {
0120     "gpio.0", "gpio.1", "gpio.2", "gpio.3",
0121     "gpio.4", "gpio.5", "gpio.6",
0122     "s_enable", "s_idle", "s_resetn",
0123 };
0124 
0125 static int tps68470_gpio_probe(struct platform_device *pdev)
0126 {
0127     struct tps68470_gpio_data *tps68470_gpio;
0128 
0129     tps68470_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps68470_gpio),
0130                      GFP_KERNEL);
0131     if (!tps68470_gpio)
0132         return -ENOMEM;
0133 
0134     tps68470_gpio->tps68470_regmap = dev_get_drvdata(pdev->dev.parent);
0135     tps68470_gpio->gc.label = "tps68470-gpio";
0136     tps68470_gpio->gc.owner = THIS_MODULE;
0137     tps68470_gpio->gc.direction_input = tps68470_gpio_input;
0138     tps68470_gpio->gc.direction_output = tps68470_gpio_output;
0139     tps68470_gpio->gc.get = tps68470_gpio_get;
0140     tps68470_gpio->gc.get_direction = tps68470_gpio_get_direction;
0141     tps68470_gpio->gc.set = tps68470_gpio_set;
0142     tps68470_gpio->gc.can_sleep = true;
0143     tps68470_gpio->gc.names = tps68470_names;
0144     tps68470_gpio->gc.ngpio = TPS68470_N_GPIO;
0145     tps68470_gpio->gc.base = -1;
0146     tps68470_gpio->gc.parent = &pdev->dev;
0147 
0148     return devm_gpiochip_add_data(&pdev->dev, &tps68470_gpio->gc, tps68470_gpio);
0149 }
0150 
0151 static struct platform_driver tps68470_gpio_driver = {
0152     .driver = {
0153            .name = "tps68470-gpio",
0154     },
0155     .probe = tps68470_gpio_probe,
0156 };
0157 module_platform_driver(tps68470_gpio_driver);
0158 
0159 MODULE_ALIAS("platform:tps68470-gpio");
0160 MODULE_DESCRIPTION("GPIO driver for TPS68470 PMIC");
0161 MODULE_LICENSE("GPL v2");