1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * GPIO driver for TI TPS65912x PMICs
4 *
5 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
6 * Andrew F. Davis <afd@ti.com>
7 *
8 * Based on the Arizona GPIO driver and the previous TPS65912 driver by
9 * Margarita Olaya Cabrera <magi@slimlogic.co.uk>
10 */
11
12 #include <linux/gpio/driver.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15
16 #include <linux/mfd/tps65912.h>
17
18 struct tps65912_gpio {
19 struct gpio_chip gpio_chip;
20 struct tps65912 *tps;
21 };
22
tps65912_gpio_get_direction(struct gpio_chip * gc,unsigned offset)23 static int tps65912_gpio_get_direction(struct gpio_chip *gc,
24 unsigned offset)
25 {
26 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
27
28 int ret, val;
29
30 ret = regmap_read(gpio->tps->regmap, TPS65912_GPIO1 + offset, &val);
31 if (ret)
32 return ret;
33
34 if (val & GPIO_CFG_MASK)
35 return GPIO_LINE_DIRECTION_OUT;
36 else
37 return GPIO_LINE_DIRECTION_IN;
38 }
39
tps65912_gpio_direction_input(struct gpio_chip * gc,unsigned offset)40 static int tps65912_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
41 {
42 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
43
44 return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
45 GPIO_CFG_MASK, 0);
46 }
47
tps65912_gpio_direction_output(struct gpio_chip * gc,unsigned offset,int value)48 static int tps65912_gpio_direction_output(struct gpio_chip *gc,
49 unsigned offset, int value)
50 {
51 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
52 int ret;
53
54 /* Set the initial value */
55 ret = regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
56 GPIO_SET_MASK, value ? GPIO_SET_MASK : 0);
57 if (ret)
58 return ret;
59
60 return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
61 GPIO_CFG_MASK, GPIO_CFG_MASK);
62 }
63
tps65912_gpio_get(struct gpio_chip * gc,unsigned offset)64 static int tps65912_gpio_get(struct gpio_chip *gc, unsigned offset)
65 {
66 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
67 int ret, val;
68
69 ret = regmap_read(gpio->tps->regmap, TPS65912_GPIO1 + offset, &val);
70 if (ret)
71 return ret;
72
73 if (val & GPIO_STS_MASK)
74 return 1;
75
76 return 0;
77 }
78
tps65912_gpio_set(struct gpio_chip * gc,unsigned int offset,int value)79 static int tps65912_gpio_set(struct gpio_chip *gc, unsigned int offset,
80 int value)
81 {
82 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
83
84 return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
85 GPIO_SET_MASK, value ? GPIO_SET_MASK : 0);
86 }
87
88 static const struct gpio_chip template_chip = {
89 .label = "tps65912-gpio",
90 .owner = THIS_MODULE,
91 .get_direction = tps65912_gpio_get_direction,
92 .direction_input = tps65912_gpio_direction_input,
93 .direction_output = tps65912_gpio_direction_output,
94 .get = tps65912_gpio_get,
95 .set_rv = tps65912_gpio_set,
96 .base = -1,
97 .ngpio = 5,
98 .can_sleep = true,
99 };
100
tps65912_gpio_probe(struct platform_device * pdev)101 static int tps65912_gpio_probe(struct platform_device *pdev)
102 {
103 struct tps65912 *tps = dev_get_drvdata(pdev->dev.parent);
104 struct tps65912_gpio *gpio;
105
106 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
107 if (!gpio)
108 return -ENOMEM;
109
110 gpio->tps = dev_get_drvdata(pdev->dev.parent);
111 gpio->gpio_chip = template_chip;
112 gpio->gpio_chip.parent = tps->dev;
113
114 return devm_gpiochip_add_data(&pdev->dev, &gpio->gpio_chip, gpio);
115 }
116
117 static const struct platform_device_id tps65912_gpio_id_table[] = {
118 { "tps65912-gpio", },
119 { /* sentinel */ }
120 };
121 MODULE_DEVICE_TABLE(platform, tps65912_gpio_id_table);
122
123 static struct platform_driver tps65912_gpio_driver = {
124 .driver = {
125 .name = "tps65912-gpio",
126 },
127 .probe = tps65912_gpio_probe,
128 .id_table = tps65912_gpio_id_table,
129 };
130 module_platform_driver(tps65912_gpio_driver);
131
132 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
133 MODULE_DESCRIPTION("TPS65912 GPIO driver");
134 MODULE_LICENSE("GPL v2");
135