blob: 43a1150055ced93cc6c3585b3fea3c3e790701ee [file] [log] [blame]
Thomas Gleixnerfe963fd2019-05-23 11:14:48 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +00002/*
3 * Copyright 2015 Verifone Int.
4 *
5 * Author: Nicolas Saenz Julienne <nicolassaenzj@gmail.com>
6 *
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +00007 * This driver is based on the gpio-tps65912 implementation.
8 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/errno.h>
13#include <linux/gpio/driver.h>
14#include <linux/platform_device.h>
Keerthy0aced352016-09-19 13:09:02 +053015#include <linux/regmap.h>
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +000016#include <linux/mfd/tps65218.h>
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +000017
18struct tps65218_gpio {
19 struct tps65218 *tps65218;
20 struct gpio_chip gpio_chip;
21};
22
23static int tps65218_gpio_get(struct gpio_chip *gc, unsigned offset)
24{
25 struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
26 struct tps65218 *tps65218 = tps65218_gpio->tps65218;
27 unsigned int val;
28 int ret;
29
Keerthy0aced352016-09-19 13:09:02 +053030 ret = regmap_read(tps65218->regmap, TPS65218_REG_ENABLE2, &val);
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +000031 if (ret)
32 return ret;
33
34 return !!(val & (TPS65218_ENABLE2_GPIO1 << offset));
35}
36
37static void tps65218_gpio_set(struct gpio_chip *gc, unsigned offset,
38 int value)
39{
40 struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
41 struct tps65218 *tps65218 = tps65218_gpio->tps65218;
42
43 if (value)
44 tps65218_set_bits(tps65218, TPS65218_REG_ENABLE2,
45 TPS65218_ENABLE2_GPIO1 << offset,
46 TPS65218_ENABLE2_GPIO1 << offset,
47 TPS65218_PROTECT_L1);
48 else
49 tps65218_clear_bits(tps65218, TPS65218_REG_ENABLE2,
50 TPS65218_ENABLE2_GPIO1 << offset,
51 TPS65218_PROTECT_L1);
52}
53
54static int tps65218_gpio_output(struct gpio_chip *gc, unsigned offset,
55 int value)
56{
57 /* Only drives GPOs */
Axel Lin818cc6a2016-02-15 20:10:40 +080058 tps65218_gpio_set(gc, offset, value);
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +000059 return 0;
60}
61
62static int tps65218_gpio_input(struct gpio_chip *gc, unsigned offset)
63{
64 return -EPERM;
65}
66
67static int tps65218_gpio_request(struct gpio_chip *gc, unsigned offset)
68{
69 struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
70 struct tps65218 *tps65218 = tps65218_gpio->tps65218;
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +000071 int ret;
72
Linus Walleij143b65d2016-02-16 15:41:42 +010073 if (gpiochip_line_is_open_source(gc, offset)) {
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +000074 dev_err(gc->parent, "can't work as open source\n");
75 return -EINVAL;
76 }
77
78 switch (offset) {
79 case 0:
Linus Walleij143b65d2016-02-16 15:41:42 +010080 if (!gpiochip_line_is_open_drain(gc, offset)) {
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +000081 dev_err(gc->parent, "GPO1 works only as open drain\n");
82 return -EINVAL;
83 }
84
85 /* Disable sequencer for GPO1 */
86 ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7,
87 TPS65218_SEQ7_GPO1_SEQ_MASK,
88 TPS65218_PROTECT_L1);
89 if (ret)
90 return ret;
91
92 /* Setup GPO1 */
93 ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1,
94 TPS65218_CONFIG1_IO1_SEL,
95 TPS65218_PROTECT_L1);
96 if (ret)
97 return ret;
98
99 break;
100 case 1:
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +0000101 /* Setup GPO2 */
102 ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1,
103 TPS65218_CONFIG1_IO1_SEL,
104 TPS65218_PROTECT_L1);
105 if (ret)
106 return ret;
107
108 break;
109
110 case 2:
Linus Walleij143b65d2016-02-16 15:41:42 +0100111 if (!gpiochip_line_is_open_drain(gc, offset)) {
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +0000112 dev_err(gc->parent, "GPO3 works only as open drain\n");
113 return -EINVAL;
114 }
115
116 /* Disable sequencer for GPO3 */
117 ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7,
118 TPS65218_SEQ7_GPO3_SEQ_MASK,
119 TPS65218_PROTECT_L1);
120 if (ret)
121 return ret;
122
123 /* Setup GPO3 */
124 ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG2,
125 TPS65218_CONFIG2_DC12_RST,
126 TPS65218_PROTECT_L1);
127 if (ret)
128 return ret;
129
130 break;
131 default:
132 return -EINVAL;
133 }
134
135 return 0;
136}
137
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300138static int tps65218_gpio_set_config(struct gpio_chip *gc, unsigned offset,
139 unsigned long config)
Linus Walleijf30e49f2016-04-08 14:13:53 +0200140{
141 struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
142 struct tps65218 *tps65218 = tps65218_gpio->tps65218;
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300143 enum pin_config_param param = pinconf_to_config_param(config);
Linus Walleijf30e49f2016-04-08 14:13:53 +0200144
145 switch (offset) {
146 case 0:
147 case 2:
148 /* GPO1 is hardwired to be open drain */
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300149 if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN)
Linus Walleijf30e49f2016-04-08 14:13:53 +0200150 return 0;
151 return -ENOTSUPP;
152 case 1:
153 /* GPO2 is push-pull by default, can be set as open drain. */
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300154 if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN)
Linus Walleijf30e49f2016-04-08 14:13:53 +0200155 return tps65218_clear_bits(tps65218,
156 TPS65218_REG_CONFIG1,
157 TPS65218_CONFIG1_GPO2_BUF,
158 TPS65218_PROTECT_L1);
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300159 if (param == PIN_CONFIG_DRIVE_PUSH_PULL)
Linus Walleijf30e49f2016-04-08 14:13:53 +0200160 return tps65218_set_bits(tps65218,
161 TPS65218_REG_CONFIG1,
162 TPS65218_CONFIG1_GPO2_BUF,
163 TPS65218_CONFIG1_GPO2_BUF,
164 TPS65218_PROTECT_L1);
165 return -ENOTSUPP;
166 default:
167 break;
168 }
169 return -ENOTSUPP;
170}
171
Julia Lawalle35b5ab2016-09-11 14:14:37 +0200172static const struct gpio_chip template_chip = {
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +0000173 .label = "gpio-tps65218",
174 .owner = THIS_MODULE,
175 .request = tps65218_gpio_request,
176 .direction_output = tps65218_gpio_output,
177 .direction_input = tps65218_gpio_input,
178 .get = tps65218_gpio_get,
179 .set = tps65218_gpio_set,
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300180 .set_config = tps65218_gpio_set_config,
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +0000181 .can_sleep = true,
182 .ngpio = 3,
183 .base = -1,
184};
185
186static int tps65218_gpio_probe(struct platform_device *pdev)
187{
188 struct tps65218 *tps65218 = dev_get_drvdata(pdev->dev.parent);
189 struct tps65218_gpio *tps65218_gpio;
190 int ret;
191
192 tps65218_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps65218_gpio),
193 GFP_KERNEL);
194 if (!tps65218_gpio)
195 return -ENOMEM;
196
197 tps65218_gpio->tps65218 = tps65218;
198 tps65218_gpio->gpio_chip = template_chip;
199 tps65218_gpio->gpio_chip.parent = &pdev->dev;
200#ifdef CONFIG_OF_GPIO
201 tps65218_gpio->gpio_chip.of_node = pdev->dev.of_node;
202#endif
203
Wei Yongjun8df759c2016-09-16 01:51:10 +0000204 ret = devm_gpiochip_add_data(&pdev->dev, &tps65218_gpio->gpio_chip,
205 tps65218_gpio);
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +0000206 if (ret < 0) {
207 dev_err(&pdev->dev, "Failed to register gpiochip, %d\n", ret);
208 return ret;
209 }
210
211 platform_set_drvdata(pdev, tps65218_gpio);
212
213 return ret;
214}
215
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +0000216static const struct of_device_id tps65218_dt_match[] = {
217 { .compatible = "ti,tps65218-gpio" },
218 { }
219};
220MODULE_DEVICE_TABLE(of, tps65218_dt_match);
221
Keerthya944a892016-06-28 18:00:52 +0530222static const struct platform_device_id tps65218_gpio_id_table[] = {
223 { "tps65218-gpio", },
224 { /* sentinel */ }
225};
226MODULE_DEVICE_TABLE(platform, tps65218_gpio_id_table);
227
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +0000228static struct platform_driver tps65218_gpio_driver = {
229 .driver = {
230 .name = "tps65218-gpio",
231 .of_match_table = of_match_ptr(tps65218_dt_match)
232 },
233 .probe = tps65218_gpio_probe,
Keerthya944a892016-06-28 18:00:52 +0530234 .id_table = tps65218_gpio_id_table,
Nicolas Saenz Juliennec366c762016-01-30 17:06:44 +0000235};
236
237module_platform_driver(tps65218_gpio_driver);
238
239MODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>");
240MODULE_DESCRIPTION("GPO interface for TPS65218 PMICs");
241MODULE_LICENSE("GPL v2");
242MODULE_ALIAS("platform:tps65218-gpio");