2 * Copyright 2015 Verifone Int.
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/mfd/tps65218.h>
21 struct tps65218_gpio {
22 struct tps65218 *tps65218;
23 struct gpio_chip gpio_chip;
26 static int tps65218_gpio_get(struct gpio_chip *gc, unsigned offset)
28 struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
29 struct tps65218 *tps65218 = tps65218_gpio->tps65218;
33 ret = tps65218_reg_read(tps65218, TPS65218_REG_ENABLE2, &val);
37 return !!(val & (TPS65218_ENABLE2_GPIO1 << offset));
40 static void tps65218_gpio_set(struct gpio_chip *gc, unsigned offset,
43 struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
44 struct tps65218 *tps65218 = tps65218_gpio->tps65218;
47 tps65218_set_bits(tps65218, TPS65218_REG_ENABLE2,
48 TPS65218_ENABLE2_GPIO1 << offset,
49 TPS65218_ENABLE2_GPIO1 << offset,
52 tps65218_clear_bits(tps65218, TPS65218_REG_ENABLE2,
53 TPS65218_ENABLE2_GPIO1 << offset,
57 static int tps65218_gpio_output(struct gpio_chip *gc, unsigned offset,
60 /* Only drives GPOs */
61 tps65218_gpio_set(gc, offset, value);
65 static int tps65218_gpio_input(struct gpio_chip *gc, unsigned offset)
70 static int tps65218_gpio_request(struct gpio_chip *gc, unsigned offset)
72 struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
73 struct tps65218 *tps65218 = tps65218_gpio->tps65218;
76 if (gpiochip_line_is_open_source(gc, offset)) {
77 dev_err(gc->parent, "can't work as open source\n");
83 if (!gpiochip_line_is_open_drain(gc, offset)) {
84 dev_err(gc->parent, "GPO1 works only as open drain\n");
88 /* Disable sequencer for GPO1 */
89 ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7,
90 TPS65218_SEQ7_GPO1_SEQ_MASK,
96 ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1,
97 TPS65218_CONFIG1_IO1_SEL,
105 ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1,
106 TPS65218_CONFIG1_IO1_SEL,
107 TPS65218_PROTECT_L1);
114 if (!gpiochip_line_is_open_drain(gc, offset)) {
115 dev_err(gc->parent, "GPO3 works only as open drain\n");
119 /* Disable sequencer for GPO3 */
120 ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7,
121 TPS65218_SEQ7_GPO3_SEQ_MASK,
122 TPS65218_PROTECT_L1);
127 ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG2,
128 TPS65218_CONFIG2_DC12_RST,
129 TPS65218_PROTECT_L1);
141 static int tps65218_gpio_set_single_ended(struct gpio_chip *gc,
143 enum single_ended_mode mode)
145 struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
146 struct tps65218 *tps65218 = tps65218_gpio->tps65218;
151 /* GPO1 is hardwired to be open drain */
152 if (mode == LINE_MODE_OPEN_DRAIN)
156 /* GPO2 is push-pull by default, can be set as open drain. */
157 if (mode == LINE_MODE_OPEN_DRAIN)
158 return tps65218_clear_bits(tps65218,
159 TPS65218_REG_CONFIG1,
160 TPS65218_CONFIG1_GPO2_BUF,
161 TPS65218_PROTECT_L1);
162 if (mode == LINE_MODE_PUSH_PULL)
163 return tps65218_set_bits(tps65218,
164 TPS65218_REG_CONFIG1,
165 TPS65218_CONFIG1_GPO2_BUF,
166 TPS65218_CONFIG1_GPO2_BUF,
167 TPS65218_PROTECT_L1);
175 static struct gpio_chip template_chip = {
176 .label = "gpio-tps65218",
177 .owner = THIS_MODULE,
178 .request = tps65218_gpio_request,
179 .direction_output = tps65218_gpio_output,
180 .direction_input = tps65218_gpio_input,
181 .get = tps65218_gpio_get,
182 .set = tps65218_gpio_set,
183 .set_single_ended = tps65218_gpio_set_single_ended,
189 static int tps65218_gpio_probe(struct platform_device *pdev)
191 struct tps65218 *tps65218 = dev_get_drvdata(pdev->dev.parent);
192 struct tps65218_gpio *tps65218_gpio;
195 tps65218_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps65218_gpio),
200 tps65218_gpio->tps65218 = tps65218;
201 tps65218_gpio->gpio_chip = template_chip;
202 tps65218_gpio->gpio_chip.parent = &pdev->dev;
203 #ifdef CONFIG_OF_GPIO
204 tps65218_gpio->gpio_chip.of_node = pdev->dev.of_node;
207 ret = gpiochip_add_data(&tps65218_gpio->gpio_chip, tps65218_gpio);
209 dev_err(&pdev->dev, "Failed to register gpiochip, %d\n", ret);
213 platform_set_drvdata(pdev, tps65218_gpio);
218 static int tps65218_gpio_remove(struct platform_device *pdev)
220 struct tps65218_gpio *tps65218_gpio = platform_get_drvdata(pdev);
222 gpiochip_remove(&tps65218_gpio->gpio_chip);
227 static const struct of_device_id tps65218_dt_match[] = {
228 { .compatible = "ti,tps65218-gpio" },
231 MODULE_DEVICE_TABLE(of, tps65218_dt_match);
233 static struct platform_driver tps65218_gpio_driver = {
235 .name = "tps65218-gpio",
236 .of_match_table = of_match_ptr(tps65218_dt_match)
238 .probe = tps65218_gpio_probe,
239 .remove = tps65218_gpio_remove,
242 module_platform_driver(tps65218_gpio_driver);
245 MODULE_DESCRIPTION("GPO interface for TPS65218 PMICs");
246 MODULE_LICENSE("GPL v2");
247 MODULE_ALIAS("platform:tps65218-gpio");