0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/errno.h>
0014 #include <linux/gpio/driver.h>
0015 #include <linux/kernel.h>
0016 #include <linux/init.h>
0017 #include <linux/mfd/tps6586x.h>
0018 #include <linux/of_device.h>
0019 #include <linux/platform_device.h>
0020
0021
0022 #define TPS6586X_GPIOSET1 0x5d
0023 #define TPS6586X_GPIOSET2 0x5e
0024
0025 struct tps6586x_gpio {
0026 struct gpio_chip gpio_chip;
0027 struct device *parent;
0028 };
0029
0030 static int tps6586x_gpio_get(struct gpio_chip *gc, unsigned offset)
0031 {
0032 struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
0033 uint8_t val;
0034 int ret;
0035
0036 ret = tps6586x_read(tps6586x_gpio->parent, TPS6586X_GPIOSET2, &val);
0037 if (ret)
0038 return ret;
0039
0040 return !!(val & (1 << offset));
0041 }
0042
0043 static void tps6586x_gpio_set(struct gpio_chip *gc, unsigned offset,
0044 int value)
0045 {
0046 struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
0047
0048 tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET2,
0049 value << offset, 1 << offset);
0050 }
0051
0052 static int tps6586x_gpio_output(struct gpio_chip *gc, unsigned offset,
0053 int value)
0054 {
0055 struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
0056 uint8_t val, mask;
0057
0058 tps6586x_gpio_set(gc, offset, value);
0059
0060 val = 0x1 << (offset * 2);
0061 mask = 0x3 << (offset * 2);
0062
0063 return tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET1,
0064 val, mask);
0065 }
0066
0067 static int tps6586x_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
0068 {
0069 struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
0070
0071 return tps6586x_irq_get_virq(tps6586x_gpio->parent,
0072 TPS6586X_INT_PLDO_0 + offset);
0073 }
0074
0075 static int tps6586x_gpio_probe(struct platform_device *pdev)
0076 {
0077 struct tps6586x_platform_data *pdata;
0078 struct tps6586x_gpio *tps6586x_gpio;
0079
0080 device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
0081
0082 pdata = dev_get_platdata(pdev->dev.parent);
0083 tps6586x_gpio = devm_kzalloc(&pdev->dev,
0084 sizeof(*tps6586x_gpio), GFP_KERNEL);
0085 if (!tps6586x_gpio)
0086 return -ENOMEM;
0087
0088 tps6586x_gpio->parent = pdev->dev.parent;
0089
0090 tps6586x_gpio->gpio_chip.owner = THIS_MODULE;
0091 tps6586x_gpio->gpio_chip.label = pdev->name;
0092 tps6586x_gpio->gpio_chip.parent = &pdev->dev;
0093 tps6586x_gpio->gpio_chip.ngpio = 4;
0094 tps6586x_gpio->gpio_chip.can_sleep = true;
0095
0096
0097 tps6586x_gpio->gpio_chip.direction_output = tps6586x_gpio_output;
0098 tps6586x_gpio->gpio_chip.set = tps6586x_gpio_set;
0099 tps6586x_gpio->gpio_chip.get = tps6586x_gpio_get;
0100 tps6586x_gpio->gpio_chip.to_irq = tps6586x_gpio_to_irq;
0101
0102 if (pdata && pdata->gpio_base)
0103 tps6586x_gpio->gpio_chip.base = pdata->gpio_base;
0104 else
0105 tps6586x_gpio->gpio_chip.base = -1;
0106
0107 return devm_gpiochip_add_data(&pdev->dev, &tps6586x_gpio->gpio_chip,
0108 tps6586x_gpio);
0109 }
0110
0111 static struct platform_driver tps6586x_gpio_driver = {
0112 .driver.name = "tps6586x-gpio",
0113 .probe = tps6586x_gpio_probe,
0114 };
0115
0116 static int __init tps6586x_gpio_init(void)
0117 {
0118 return platform_driver_register(&tps6586x_gpio_driver);
0119 }
0120 subsys_initcall(tps6586x_gpio_init);