xref: /openbmc/linux/drivers/gpio/gpio-tps65218.c (revision 818cc6a5)
1 /*
2  * Copyright 2015 Verifone Int.
3  *
4  * Author: Nicolas Saenz Julienne <nicolassaenzj@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify i t
7  * under  the terms of the GNU General  Public License as published by th e
8  * Free Software Foundation;  either version 2 of the License, or (at you r
9  * option) any later version.
10  *
11  * This driver is based on the gpio-tps65912 implementation.
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/platform_device.h>
19 #include <linux/mfd/tps65218.h>
20 #include "gpiolib.h"
21 
22 struct tps65218_gpio {
23 	struct tps65218 *tps65218;
24 	struct gpio_chip gpio_chip;
25 };
26 
27 static int tps65218_gpio_get(struct gpio_chip *gc, unsigned offset)
28 {
29 	struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
30 	struct tps65218 *tps65218 = tps65218_gpio->tps65218;
31 	unsigned int val;
32 	int ret;
33 
34 	ret = tps65218_reg_read(tps65218, TPS65218_REG_ENABLE2, &val);
35 	if (ret)
36 		return ret;
37 
38 	return !!(val & (TPS65218_ENABLE2_GPIO1 << offset));
39 }
40 
41 static void tps65218_gpio_set(struct gpio_chip *gc, unsigned offset,
42 			      int value)
43 {
44 	struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
45 	struct tps65218 *tps65218 = tps65218_gpio->tps65218;
46 
47 	if (value)
48 		tps65218_set_bits(tps65218, TPS65218_REG_ENABLE2,
49 				  TPS65218_ENABLE2_GPIO1 << offset,
50 				  TPS65218_ENABLE2_GPIO1 << offset,
51 				  TPS65218_PROTECT_L1);
52 	else
53 		tps65218_clear_bits(tps65218, TPS65218_REG_ENABLE2,
54 				    TPS65218_ENABLE2_GPIO1 << offset,
55 				    TPS65218_PROTECT_L1);
56 }
57 
58 static int tps65218_gpio_output(struct gpio_chip *gc, unsigned offset,
59 				int value)
60 {
61 	/* Only drives GPOs */
62 	tps65218_gpio_set(gc, offset, value);
63 	return 0;
64 }
65 
66 static int tps65218_gpio_input(struct gpio_chip *gc, unsigned offset)
67 {
68 	return -EPERM;
69 }
70 
71 static int tps65218_gpio_request(struct gpio_chip *gc, unsigned offset)
72 {
73 	struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
74 	struct tps65218 *tps65218 = tps65218_gpio->tps65218;
75 	int ret;
76 
77 	if (gpiochip_line_is_open_source(gc, offset)) {
78 		dev_err(gc->parent, "can't work as open source\n");
79 		return -EINVAL;
80 	}
81 
82 	switch (offset) {
83 	case 0:
84 		if (!gpiochip_line_is_open_drain(gc, offset)) {
85 			dev_err(gc->parent, "GPO1 works only as open drain\n");
86 			return -EINVAL;
87 		}
88 
89 		/* Disable sequencer for GPO1 */
90 		ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7,
91 					  TPS65218_SEQ7_GPO1_SEQ_MASK,
92 					  TPS65218_PROTECT_L1);
93 		if (ret)
94 			return ret;
95 
96 		/* Setup GPO1 */
97 		ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1,
98 					  TPS65218_CONFIG1_IO1_SEL,
99 					  TPS65218_PROTECT_L1);
100 		if (ret)
101 			return ret;
102 
103 		break;
104 	case 1:
105 		/* GP02 is push-pull by default, can be set as open drain. */
106 		if (gpiochip_line_is_open_drain(gc, offset)) {
107 			ret = tps65218_clear_bits(tps65218,
108 						  TPS65218_REG_CONFIG1,
109 						  TPS65218_CONFIG1_GPO2_BUF,
110 						  TPS65218_PROTECT_L1);
111 			if (ret)
112 				return ret;
113 		}
114 
115 		/* Setup GPO2 */
116 		ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1,
117 					  TPS65218_CONFIG1_IO1_SEL,
118 					  TPS65218_PROTECT_L1);
119 		if (ret)
120 			return ret;
121 
122 		break;
123 
124 	case 2:
125 		if (!gpiochip_line_is_open_drain(gc, offset)) {
126 			dev_err(gc->parent, "GPO3 works only as open drain\n");
127 			return -EINVAL;
128 		}
129 
130 		/* Disable sequencer for GPO3 */
131 		ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7,
132 					  TPS65218_SEQ7_GPO3_SEQ_MASK,
133 					  TPS65218_PROTECT_L1);
134 		if (ret)
135 			return ret;
136 
137 		/* Setup GPO3 */
138 		ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG2,
139 					  TPS65218_CONFIG2_DC12_RST,
140 					  TPS65218_PROTECT_L1);
141 		if (ret)
142 			return ret;
143 
144 		break;
145 	default:
146 		return -EINVAL;
147 	}
148 
149 	return 0;
150 }
151 
152 static struct gpio_chip template_chip = {
153 	.label			= "gpio-tps65218",
154 	.owner			= THIS_MODULE,
155 	.request		= tps65218_gpio_request,
156 	.direction_output	= tps65218_gpio_output,
157 	.direction_input	= tps65218_gpio_input,
158 	.get			= tps65218_gpio_get,
159 	.set			= tps65218_gpio_set,
160 	.can_sleep		= true,
161 	.ngpio			= 3,
162 	.base			= -1,
163 };
164 
165 static int tps65218_gpio_probe(struct platform_device *pdev)
166 {
167 	struct tps65218 *tps65218 = dev_get_drvdata(pdev->dev.parent);
168 	struct tps65218_gpio *tps65218_gpio;
169 	int ret;
170 
171 	tps65218_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps65218_gpio),
172 				     GFP_KERNEL);
173 	if (!tps65218_gpio)
174 		return -ENOMEM;
175 
176 	tps65218_gpio->tps65218 = tps65218;
177 	tps65218_gpio->gpio_chip = template_chip;
178 	tps65218_gpio->gpio_chip.parent = &pdev->dev;
179 #ifdef CONFIG_OF_GPIO
180 	tps65218_gpio->gpio_chip.of_node = pdev->dev.of_node;
181 #endif
182 
183 	ret = gpiochip_add_data(&tps65218_gpio->gpio_chip, tps65218_gpio);
184 	if (ret < 0) {
185 		dev_err(&pdev->dev, "Failed to register gpiochip, %d\n", ret);
186 		return ret;
187 	}
188 
189 	platform_set_drvdata(pdev, tps65218_gpio);
190 
191 	return ret;
192 }
193 
194 static int tps65218_gpio_remove(struct platform_device *pdev)
195 {
196 	struct tps65218_gpio *tps65218_gpio = platform_get_drvdata(pdev);
197 
198 	gpiochip_remove(&tps65218_gpio->gpio_chip);
199 
200 	return 0;
201 }
202 
203 static const struct of_device_id tps65218_dt_match[] = {
204 	{ .compatible = "ti,tps65218-gpio" },
205 	{  }
206 };
207 MODULE_DEVICE_TABLE(of, tps65218_dt_match);
208 
209 static struct platform_driver tps65218_gpio_driver = {
210 	.driver = {
211 		.name = "tps65218-gpio",
212 		.of_match_table = of_match_ptr(tps65218_dt_match)
213 	},
214 	.probe = tps65218_gpio_probe,
215 	.remove = tps65218_gpio_remove,
216 };
217 
218 module_platform_driver(tps65218_gpio_driver);
219 
220 MODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>");
221 MODULE_DESCRIPTION("GPO interface for TPS65218 PMICs");
222 MODULE_LICENSE("GPL v2");
223 MODULE_ALIAS("platform:tps65218-gpio");
224