1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
4 * Andrew F. Davis <afd@ti.com>
5 *
6 * Based on the TPS65912 driver
7 */
8
9#include <linux/gpio/driver.h>
10#include <linux/module.h>
11#include <linux/platform_device.h>
12
13#include <linux/mfd/tps65086.h>
14
15struct tps65086_gpio {
16 struct gpio_chip chip;
17 struct tps65086 *tps;
18};
19
20static int tps65086_gpio_get_direction(struct gpio_chip *chip,
21 unsigned offset)
22{
23 /* This device is output only */
24 return 0;
25}
26
27static int tps65086_gpio_direction_input(struct gpio_chip *chip,
28 unsigned offset)
29{
30 /* This device is output only */
31 return -EINVAL;
32}
33
34static int tps65086_gpio_direction_output(struct gpio_chip *chip,
35 unsigned offset, int value)
36{
37 struct tps65086_gpio *gpio = gpiochip_get_data(chip);
38
39 /* Set the initial value */
40 regmap_update_bits(gpio->tps->regmap, TPS65086_GPOCTRL,
41 BIT(4 + offset), value ? BIT(4 + offset) : 0);
42
43 return 0;
44}
45
46static int tps65086_gpio_get(struct gpio_chip *chip, unsigned offset)
47{
48 struct tps65086_gpio *gpio = gpiochip_get_data(chip);
49 int ret, val;
50
51 ret = regmap_read(gpio->tps->regmap, TPS65086_GPOCTRL, &val);
52 if (ret < 0)
53 return ret;
54
55 return val & BIT(4 + offset);
56}
57
58static void tps65086_gpio_set(struct gpio_chip *chip, unsigned offset,
59 int value)
60{
61 struct tps65086_gpio *gpio = gpiochip_get_data(chip);
62
63 regmap_update_bits(gpio->tps->regmap, TPS65086_GPOCTRL,
64 BIT(4 + offset), value ? BIT(4 + offset) : 0);
65}
66
67static const struct gpio_chip template_chip = {
68 .label = "tps65086-gpio",
69 .owner = THIS_MODULE,
70 .get_direction = tps65086_gpio_get_direction,
71 .direction_input = tps65086_gpio_direction_input,
72 .direction_output = tps65086_gpio_direction_output,
73 .get = tps65086_gpio_get,
74 .set = tps65086_gpio_set,
75 .base = -1,
76 .ngpio = 4,
77 .can_sleep = true,
78};
79
80static int tps65086_gpio_probe(struct platform_device *pdev)
81{
82 struct tps65086_gpio *gpio;
83 int ret;
84
85 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
86 if (!gpio)
87 return -ENOMEM;
88
89 platform_set_drvdata(pdev, gpio);
90
91 gpio->tps = dev_get_drvdata(pdev->dev.parent);
92 gpio->chip = template_chip;
93 gpio->chip.parent = gpio->tps->dev;
94
95 ret = gpiochip_add_data(&gpio->chip, gpio);
96 if (ret < 0) {
97 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
98 return ret;
99 }
100
101 return 0;
102}
103
104static int tps65086_gpio_remove(struct platform_device *pdev)
105{
106 struct tps65086_gpio *gpio = platform_get_drvdata(pdev);
107
108 gpiochip_remove(&gpio->chip);
109
110 return 0;
111}
112
113static const struct platform_device_id tps65086_gpio_id_table[] = {
114 { "tps65086-gpio", },
115 { /* sentinel */ }
116};
117MODULE_DEVICE_TABLE(platform, tps65086_gpio_id_table);
118
119static struct platform_driver tps65086_gpio_driver = {
120 .driver = {
121 .name = "tps65086-gpio",
122 },
123 .probe = tps65086_gpio_probe,
124 .remove = tps65086_gpio_remove,
125 .id_table = tps65086_gpio_id_table,
126};
127module_platform_driver(tps65086_gpio_driver);
128
129MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
130MODULE_DESCRIPTION("TPS65086 GPIO driver");
131MODULE_LICENSE("GPL v2");
132