2 * Copyright 2015 Verifone Int.
4 * Author: Nicolas Saenz Julienne <nicolassaenzj@gmail.com>
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.
11 * This driver is based on the gpio-tps65912 implementation.
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/regmap.h>
20 #include <linux/mfd/tps65218.h>
22 struct tps65218_gpio
{
23 struct tps65218
*tps65218
;
24 struct gpio_chip gpio_chip
;
27 static int tps65218_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
29 struct tps65218_gpio
*tps65218_gpio
= gpiochip_get_data(gc
);
30 struct tps65218
*tps65218
= tps65218_gpio
->tps65218
;
34 ret
= regmap_read(tps65218
->regmap
, TPS65218_REG_ENABLE2
, &val
);
38 return !!(val
& (TPS65218_ENABLE2_GPIO1
<< offset
));
41 static void tps65218_gpio_set(struct gpio_chip
*gc
, unsigned offset
,
44 struct tps65218_gpio
*tps65218_gpio
= gpiochip_get_data(gc
);
45 struct tps65218
*tps65218
= tps65218_gpio
->tps65218
;
48 tps65218_set_bits(tps65218
, TPS65218_REG_ENABLE2
,
49 TPS65218_ENABLE2_GPIO1
<< offset
,
50 TPS65218_ENABLE2_GPIO1
<< offset
,
53 tps65218_clear_bits(tps65218
, TPS65218_REG_ENABLE2
,
54 TPS65218_ENABLE2_GPIO1
<< offset
,
58 static int tps65218_gpio_output(struct gpio_chip
*gc
, unsigned offset
,
61 /* Only drives GPOs */
62 tps65218_gpio_set(gc
, offset
, value
);
66 static int tps65218_gpio_input(struct gpio_chip
*gc
, unsigned offset
)
71 static int tps65218_gpio_request(struct gpio_chip
*gc
, unsigned offset
)
73 struct tps65218_gpio
*tps65218_gpio
= gpiochip_get_data(gc
);
74 struct tps65218
*tps65218
= tps65218_gpio
->tps65218
;
77 if (gpiochip_line_is_open_source(gc
, offset
)) {
78 dev_err(gc
->parent
, "can't work as open source\n");
84 if (!gpiochip_line_is_open_drain(gc
, offset
)) {
85 dev_err(gc
->parent
, "GPO1 works only as open drain\n");
89 /* Disable sequencer for GPO1 */
90 ret
= tps65218_clear_bits(tps65218
, TPS65218_REG_SEQ7
,
91 TPS65218_SEQ7_GPO1_SEQ_MASK
,
97 ret
= tps65218_clear_bits(tps65218
, TPS65218_REG_CONFIG1
,
98 TPS65218_CONFIG1_IO1_SEL
,
106 ret
= tps65218_clear_bits(tps65218
, TPS65218_REG_CONFIG1
,
107 TPS65218_CONFIG1_IO1_SEL
,
108 TPS65218_PROTECT_L1
);
115 if (!gpiochip_line_is_open_drain(gc
, offset
)) {
116 dev_err(gc
->parent
, "GPO3 works only as open drain\n");
120 /* Disable sequencer for GPO3 */
121 ret
= tps65218_clear_bits(tps65218
, TPS65218_REG_SEQ7
,
122 TPS65218_SEQ7_GPO3_SEQ_MASK
,
123 TPS65218_PROTECT_L1
);
128 ret
= tps65218_clear_bits(tps65218
, TPS65218_REG_CONFIG2
,
129 TPS65218_CONFIG2_DC12_RST
,
130 TPS65218_PROTECT_L1
);
142 static int tps65218_gpio_set_config(struct gpio_chip
*gc
, unsigned offset
,
143 unsigned long config
)
145 struct tps65218_gpio
*tps65218_gpio
= gpiochip_get_data(gc
);
146 struct tps65218
*tps65218
= tps65218_gpio
->tps65218
;
147 enum pin_config_param param
= pinconf_to_config_param(config
);
152 /* GPO1 is hardwired to be open drain */
153 if (param
== PIN_CONFIG_DRIVE_OPEN_DRAIN
)
157 /* GPO2 is push-pull by default, can be set as open drain. */
158 if (param
== PIN_CONFIG_DRIVE_OPEN_DRAIN
)
159 return tps65218_clear_bits(tps65218
,
160 TPS65218_REG_CONFIG1
,
161 TPS65218_CONFIG1_GPO2_BUF
,
162 TPS65218_PROTECT_L1
);
163 if (param
== PIN_CONFIG_DRIVE_PUSH_PULL
)
164 return tps65218_set_bits(tps65218
,
165 TPS65218_REG_CONFIG1
,
166 TPS65218_CONFIG1_GPO2_BUF
,
167 TPS65218_CONFIG1_GPO2_BUF
,
168 TPS65218_PROTECT_L1
);
176 static const struct gpio_chip template_chip
= {
177 .label
= "gpio-tps65218",
178 .owner
= THIS_MODULE
,
179 .request
= tps65218_gpio_request
,
180 .direction_output
= tps65218_gpio_output
,
181 .direction_input
= tps65218_gpio_input
,
182 .get
= tps65218_gpio_get
,
183 .set
= tps65218_gpio_set
,
184 .set_config
= tps65218_gpio_set_config
,
190 static int tps65218_gpio_probe(struct platform_device
*pdev
)
192 struct tps65218
*tps65218
= dev_get_drvdata(pdev
->dev
.parent
);
193 struct tps65218_gpio
*tps65218_gpio
;
196 tps65218_gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*tps65218_gpio
),
201 tps65218_gpio
->tps65218
= tps65218
;
202 tps65218_gpio
->gpio_chip
= template_chip
;
203 tps65218_gpio
->gpio_chip
.parent
= &pdev
->dev
;
204 #ifdef CONFIG_OF_GPIO
205 tps65218_gpio
->gpio_chip
.of_node
= pdev
->dev
.of_node
;
208 ret
= devm_gpiochip_add_data(&pdev
->dev
, &tps65218_gpio
->gpio_chip
,
211 dev_err(&pdev
->dev
, "Failed to register gpiochip, %d\n", ret
);
215 platform_set_drvdata(pdev
, tps65218_gpio
);
220 static const struct of_device_id tps65218_dt_match
[] = {
221 { .compatible
= "ti,tps65218-gpio" },
224 MODULE_DEVICE_TABLE(of
, tps65218_dt_match
);
226 static const struct platform_device_id tps65218_gpio_id_table
[] = {
227 { "tps65218-gpio", },
230 MODULE_DEVICE_TABLE(platform
, tps65218_gpio_id_table
);
232 static struct platform_driver tps65218_gpio_driver
= {
234 .name
= "tps65218-gpio",
235 .of_match_table
= of_match_ptr(tps65218_dt_match
)
237 .probe
= tps65218_gpio_probe
,
238 .id_table
= tps65218_gpio_id_table
,
241 module_platform_driver(tps65218_gpio_driver
);
243 MODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>");
244 MODULE_DESCRIPTION("GPO interface for TPS65218 PMICs");
245 MODULE_LICENSE("GPL v2");
246 MODULE_ALIAS("platform:tps65218-gpio");