Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / gpio / gpio-tps65218.c
blob313c0e48460773771823d3114090a77099cac788
1 /*
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/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;
30 unsigned int val;
31 int ret;
33 ret = tps65218_reg_read(tps65218, TPS65218_REG_ENABLE2, &val);
34 if (ret)
35 return ret;
37 return !!(val & (TPS65218_ENABLE2_GPIO1 << offset));
40 static void tps65218_gpio_set(struct gpio_chip *gc, unsigned offset,
41 int value)
43 struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
44 struct tps65218 *tps65218 = tps65218_gpio->tps65218;
46 if (value)
47 tps65218_set_bits(tps65218, TPS65218_REG_ENABLE2,
48 TPS65218_ENABLE2_GPIO1 << offset,
49 TPS65218_ENABLE2_GPIO1 << offset,
50 TPS65218_PROTECT_L1);
51 else
52 tps65218_clear_bits(tps65218, TPS65218_REG_ENABLE2,
53 TPS65218_ENABLE2_GPIO1 << offset,
54 TPS65218_PROTECT_L1);
57 static int tps65218_gpio_output(struct gpio_chip *gc, unsigned offset,
58 int value)
60 /* Only drives GPOs */
61 tps65218_gpio_set(gc, offset, value);
62 return 0;
65 static int tps65218_gpio_input(struct gpio_chip *gc, unsigned offset)
67 return -EPERM;
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;
74 int ret;
76 if (gpiochip_line_is_open_source(gc, offset)) {
77 dev_err(gc->parent, "can't work as open source\n");
78 return -EINVAL;
81 switch (offset) {
82 case 0:
83 if (!gpiochip_line_is_open_drain(gc, offset)) {
84 dev_err(gc->parent, "GPO1 works only as open drain\n");
85 return -EINVAL;
88 /* Disable sequencer for GPO1 */
89 ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7,
90 TPS65218_SEQ7_GPO1_SEQ_MASK,
91 TPS65218_PROTECT_L1);
92 if (ret)
93 return ret;
95 /* Setup GPO1 */
96 ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1,
97 TPS65218_CONFIG1_IO1_SEL,
98 TPS65218_PROTECT_L1);
99 if (ret)
100 return ret;
102 break;
103 case 1:
104 /* GP02 is push-pull by default, can be set as open drain. */
105 if (gpiochip_line_is_open_drain(gc, offset)) {
106 ret = tps65218_clear_bits(tps65218,
107 TPS65218_REG_CONFIG1,
108 TPS65218_CONFIG1_GPO2_BUF,
109 TPS65218_PROTECT_L1);
110 if (ret)
111 return ret;
114 /* Setup GPO2 */
115 ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1,
116 TPS65218_CONFIG1_IO1_SEL,
117 TPS65218_PROTECT_L1);
118 if (ret)
119 return ret;
121 break;
123 case 2:
124 if (!gpiochip_line_is_open_drain(gc, offset)) {
125 dev_err(gc->parent, "GPO3 works only as open drain\n");
126 return -EINVAL;
129 /* Disable sequencer for GPO3 */
130 ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7,
131 TPS65218_SEQ7_GPO3_SEQ_MASK,
132 TPS65218_PROTECT_L1);
133 if (ret)
134 return ret;
136 /* Setup GPO3 */
137 ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG2,
138 TPS65218_CONFIG2_DC12_RST,
139 TPS65218_PROTECT_L1);
140 if (ret)
141 return ret;
143 break;
144 default:
145 return -EINVAL;
148 return 0;
151 static struct gpio_chip template_chip = {
152 .label = "gpio-tps65218",
153 .owner = THIS_MODULE,
154 .request = tps65218_gpio_request,
155 .direction_output = tps65218_gpio_output,
156 .direction_input = tps65218_gpio_input,
157 .get = tps65218_gpio_get,
158 .set = tps65218_gpio_set,
159 .can_sleep = true,
160 .ngpio = 3,
161 .base = -1,
164 static int tps65218_gpio_probe(struct platform_device *pdev)
166 struct tps65218 *tps65218 = dev_get_drvdata(pdev->dev.parent);
167 struct tps65218_gpio *tps65218_gpio;
168 int ret;
170 tps65218_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps65218_gpio),
171 GFP_KERNEL);
172 if (!tps65218_gpio)
173 return -ENOMEM;
175 tps65218_gpio->tps65218 = tps65218;
176 tps65218_gpio->gpio_chip = template_chip;
177 tps65218_gpio->gpio_chip.parent = &pdev->dev;
178 #ifdef CONFIG_OF_GPIO
179 tps65218_gpio->gpio_chip.of_node = pdev->dev.of_node;
180 #endif
182 ret = gpiochip_add_data(&tps65218_gpio->gpio_chip, tps65218_gpio);
183 if (ret < 0) {
184 dev_err(&pdev->dev, "Failed to register gpiochip, %d\n", ret);
185 return ret;
188 platform_set_drvdata(pdev, tps65218_gpio);
190 return ret;
193 static int tps65218_gpio_remove(struct platform_device *pdev)
195 struct tps65218_gpio *tps65218_gpio = platform_get_drvdata(pdev);
197 gpiochip_remove(&tps65218_gpio->gpio_chip);
199 return 0;
202 static const struct of_device_id tps65218_dt_match[] = {
203 { .compatible = "ti,tps65218-gpio" },
206 MODULE_DEVICE_TABLE(of, tps65218_dt_match);
208 static struct platform_driver tps65218_gpio_driver = {
209 .driver = {
210 .name = "tps65218-gpio",
211 .of_match_table = of_match_ptr(tps65218_dt_match)
213 .probe = tps65218_gpio_probe,
214 .remove = tps65218_gpio_remove,
217 module_platform_driver(tps65218_gpio_driver);
219 MODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>");
220 MODULE_DESCRIPTION("GPO interface for TPS65218 PMICs");
221 MODULE_LICENSE("GPL v2");
222 MODULE_ALIAS("platform:tps65218-gpio");