1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2015-2023 Texas Instruments Incorporated - https://www.ti.com/
4 * Andrew 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
15 struct tps65086_gpio {
16 struct gpio_chip chip;
17 struct tps65086 *tps;
18 };
19
tps65086_gpio_get_direction(struct gpio_chip * chip,unsigned offset)20 static int tps65086_gpio_get_direction(struct gpio_chip *chip,
21 unsigned offset)
22 {
23 /* This device is output only */
24 return GPIO_LINE_DIRECTION_OUT;
25 }
26
tps65086_gpio_direction_input(struct gpio_chip * chip,unsigned offset)27 static int tps65086_gpio_direction_input(struct gpio_chip *chip,
28 unsigned offset)
29 {
30 /* This device is output only */
31 return -EINVAL;
32 }
33
tps65086_gpio_direction_output(struct gpio_chip * chip,unsigned offset,int value)34 static 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 return regmap_update_bits(gpio->tps->regmap, TPS65086_GPOCTRL,
41 BIT(4 + offset), value ? BIT(4 + offset) : 0);
42 }
43
tps65086_gpio_get(struct gpio_chip * chip,unsigned offset)44 static int tps65086_gpio_get(struct gpio_chip *chip, unsigned offset)
45 {
46 struct tps65086_gpio *gpio = gpiochip_get_data(chip);
47 int ret, val;
48
49 ret = regmap_read(gpio->tps->regmap, TPS65086_GPOCTRL, &val);
50 if (ret < 0)
51 return ret;
52
53 return val & BIT(4 + offset);
54 }
55
tps65086_gpio_set(struct gpio_chip * chip,unsigned int offset,int value)56 static int tps65086_gpio_set(struct gpio_chip *chip, unsigned int offset,
57 int value)
58 {
59 struct tps65086_gpio *gpio = gpiochip_get_data(chip);
60
61 return regmap_update_bits(gpio->tps->regmap, TPS65086_GPOCTRL,
62 BIT(4 + offset), value ? BIT(4 + offset) : 0);
63 }
64
65 static const struct gpio_chip template_chip = {
66 .label = "tps65086-gpio",
67 .owner = THIS_MODULE,
68 .get_direction = tps65086_gpio_get_direction,
69 .direction_input = tps65086_gpio_direction_input,
70 .direction_output = tps65086_gpio_direction_output,
71 .get = tps65086_gpio_get,
72 .set = tps65086_gpio_set,
73 .base = -1,
74 .ngpio = 4,
75 .can_sleep = true,
76 };
77
tps65086_gpio_probe(struct platform_device * pdev)78 static int tps65086_gpio_probe(struct platform_device *pdev)
79 {
80 struct tps65086_gpio *gpio;
81
82 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
83 if (!gpio)
84 return -ENOMEM;
85
86 gpio->tps = dev_get_drvdata(pdev->dev.parent);
87 gpio->chip = template_chip;
88 gpio->chip.parent = gpio->tps->dev;
89
90 return devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
91 }
92
93 static const struct platform_device_id tps65086_gpio_id_table[] = {
94 { "tps65086-gpio", },
95 { /* sentinel */ }
96 };
97 MODULE_DEVICE_TABLE(platform, tps65086_gpio_id_table);
98
99 static struct platform_driver tps65086_gpio_driver = {
100 .driver = {
101 .name = "tps65086-gpio",
102 },
103 .probe = tps65086_gpio_probe,
104 .id_table = tps65086_gpio_id_table,
105 };
106 module_platform_driver(tps65086_gpio_driver);
107
108 MODULE_AUTHOR("Andrew Davis <afd@ti.com>");
109 MODULE_DESCRIPTION("TPS65086 GPIO driver");
110 MODULE_LICENSE("GPL v2");
111