aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpio/gpio-tps65910.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpio/gpio-tps65910.c')
-rw-r--r--drivers/gpio/gpio-tps65910.c188
1 files changed, 144 insertions, 44 deletions
diff --git a/drivers/gpio/gpio-tps65910.c b/drivers/gpio/gpio-tps65910.c
index 7eef648a335..c1ad2884f2e 100644
--- a/drivers/gpio/gpio-tps65910.c
+++ b/drivers/gpio/gpio-tps65910.c
@@ -18,14 +18,27 @@
18#include <linux/errno.h> 18#include <linux/errno.h>
19#include <linux/gpio.h> 19#include <linux/gpio.h>
20#include <linux/i2c.h> 20#include <linux/i2c.h>
21#include <linux/platform_device.h>
21#include <linux/mfd/tps65910.h> 22#include <linux/mfd/tps65910.h>
23#include <linux/of_device.h>
24
25struct tps65910_gpio {
26 struct gpio_chip gpio_chip;
27 struct tps65910 *tps65910;
28};
29
30static inline struct tps65910_gpio *to_tps65910_gpio(struct gpio_chip *chip)
31{
32 return container_of(chip, struct tps65910_gpio, gpio_chip);
33}
22 34
23static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset) 35static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset)
24{ 36{
25 struct tps65910 *tps65910 = container_of(gc, struct tps65910, gpio); 37 struct tps65910_gpio *tps65910_gpio = to_tps65910_gpio(gc);
26 uint8_t val; 38 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
39 unsigned int val;
27 40
28 tps65910->read(tps65910, TPS65910_GPIO0 + offset, 1, &val); 41 tps65910_reg_read(tps65910, TPS65910_GPIO0 + offset, &val);
29 42
30 if (val & GPIO_STS_MASK) 43 if (val & GPIO_STS_MASK)
31 return 1; 44 return 1;
@@ -36,83 +49,170 @@ static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset)
36static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset, 49static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset,
37 int value) 50 int value)
38{ 51{
39 struct tps65910 *tps65910 = container_of(gc, struct tps65910, gpio); 52 struct tps65910_gpio *tps65910_gpio = to_tps65910_gpio(gc);
53 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
40 54
41 if (value) 55 if (value)
42 tps65910_set_bits(tps65910, TPS65910_GPIO0 + offset, 56 tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
43 GPIO_SET_MASK); 57 GPIO_SET_MASK);
44 else 58 else
45 tps65910_clear_bits(tps65910, TPS65910_GPIO0 + offset, 59 tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
46 GPIO_SET_MASK); 60 GPIO_SET_MASK);
47} 61}
48 62
49static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset, 63static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset,
50 int value) 64 int value)
51{ 65{
52 struct tps65910 *tps65910 = container_of(gc, struct tps65910, gpio); 66 struct tps65910_gpio *tps65910_gpio = to_tps65910_gpio(gc);
67 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
53 68
54 /* Set the initial value */ 69 /* Set the initial value */
55 tps65910_gpio_set(gc, offset, value); 70 tps65910_gpio_set(gc, offset, value);
56 71
57 return tps65910_set_bits(tps65910, TPS65910_GPIO0 + offset, 72 return tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
58 GPIO_CFG_MASK); 73 GPIO_CFG_MASK);
59} 74}
60 75
61static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset) 76static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset)
62{ 77{
63 struct tps65910 *tps65910 = container_of(gc, struct tps65910, gpio); 78 struct tps65910_gpio *tps65910_gpio = to_tps65910_gpio(gc);
79 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
64 80
65 return tps65910_clear_bits(tps65910, TPS65910_GPIO0 + offset, 81 return tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
66 GPIO_CFG_MASK); 82 GPIO_CFG_MASK);
67} 83}
68 84
69void tps65910_gpio_init(struct tps65910 *tps65910, int gpio_base) 85#ifdef CONFIG_OF
86static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
87 struct tps65910 *tps65910, int chip_ngpio)
70{ 88{
89 struct tps65910_board *tps65910_board = tps65910->of_plat_data;
90 unsigned int prop_array[TPS6591X_MAX_NUM_GPIO];
91 int ngpio = min(chip_ngpio, TPS6591X_MAX_NUM_GPIO);
71 int ret; 92 int ret;
72 struct tps65910_board *board_data; 93 int idx;
94
95 tps65910_board->gpio_base = -1;
96 ret = of_property_read_u32_array(tps65910->dev->of_node,
97 "ti,en-gpio-sleep", prop_array, ngpio);
98 if (ret < 0) {
99 dev_dbg(dev, "ti,en-gpio-sleep not specified\n");
100 return tps65910_board;
101 }
73 102
74 if (!gpio_base) 103 for (idx = 0; idx < ngpio; idx++)
75 return; 104 tps65910_board->en_gpio_sleep[idx] = (prop_array[idx] != 0);
76 105
77 tps65910->gpio.owner = THIS_MODULE; 106 return tps65910_board;
78 tps65910->gpio.label = tps65910->i2c_client->name; 107}
79 tps65910->gpio.dev = tps65910->dev; 108#else
80 tps65910->gpio.base = gpio_base; 109static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
110 struct tps65910 *tps65910, int chip_ngpio)
111{
112 return NULL;
113}
114#endif
115
116static int __devinit tps65910_gpio_probe(struct platform_device *pdev)
117{
118 struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
119 struct tps65910_board *pdata = dev_get_platdata(tps65910->dev);
120 struct tps65910_gpio *tps65910_gpio;
121 int ret;
122 int i;
123
124 tps65910_gpio = devm_kzalloc(&pdev->dev,
125 sizeof(*tps65910_gpio), GFP_KERNEL);
126 if (!tps65910_gpio) {
127 dev_err(&pdev->dev, "Could not allocate tps65910_gpio\n");
128 return -ENOMEM;
129 }
130
131 tps65910_gpio->tps65910 = tps65910;
132
133 tps65910_gpio->gpio_chip.owner = THIS_MODULE;
134 tps65910_gpio->gpio_chip.label = tps65910->i2c_client->name;
81 135
82 switch(tps65910_chip_id(tps65910)) { 136 switch(tps65910_chip_id(tps65910)) {
83 case TPS65910: 137 case TPS65910:
84 tps65910->gpio.ngpio = TPS65910_NUM_GPIO; 138 tps65910_gpio->gpio_chip.ngpio = TPS65910_NUM_GPIO;
85 break; 139 break;
86 case TPS65911: 140 case TPS65911:
87 tps65910->gpio.ngpio = TPS65911_NUM_GPIO; 141 tps65910_gpio->gpio_chip.ngpio = TPS65911_NUM_GPIO;
88 break; 142 break;
89 default: 143 default:
90 return; 144 return -EINVAL;
145 }
146 tps65910_gpio->gpio_chip.can_sleep = 1;
147 tps65910_gpio->gpio_chip.direction_input = tps65910_gpio_input;
148 tps65910_gpio->gpio_chip.direction_output = tps65910_gpio_output;
149 tps65910_gpio->gpio_chip.set = tps65910_gpio_set;
150 tps65910_gpio->gpio_chip.get = tps65910_gpio_get;
151 tps65910_gpio->gpio_chip.dev = &pdev->dev;
152 if (pdata && pdata->gpio_base)
153 tps65910_gpio->gpio_chip.base = pdata->gpio_base;
154 else
155 tps65910_gpio->gpio_chip.base = -1;
156
157 if (!pdata && tps65910->dev->of_node)
158 pdata = tps65910_parse_dt_for_gpio(&pdev->dev, tps65910,
159 tps65910_gpio->gpio_chip.ngpio);
160
161 if (!pdata)
162 goto skip_init;
163
164 /* Configure sleep control for gpios if provided */
165 for (i = 0; i < tps65910_gpio->gpio_chip.ngpio; ++i) {
166 if (!pdata->en_gpio_sleep[i])
167 continue;
168
169 ret = tps65910_reg_set_bits(tps65910,
170 TPS65910_GPIO0 + i, GPIO_SLEEP_MASK);
171 if (ret < 0)
172 dev_warn(tps65910->dev,
173 "GPIO Sleep setting failed with err %d\n", ret);
91 } 174 }
92 tps65910->gpio.can_sleep = 1; 175
93 176skip_init:
94 tps65910->gpio.direction_input = tps65910_gpio_input; 177 ret = gpiochip_add(&tps65910_gpio->gpio_chip);
95 tps65910->gpio.direction_output = tps65910_gpio_output; 178 if (ret < 0) {
96 tps65910->gpio.set = tps65910_gpio_set; 179 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
97 tps65910->gpio.get = tps65910_gpio_get; 180 return ret;
98
99 /* Configure sleep control for gpios */
100 board_data = dev_get_platdata(tps65910->dev);
101 if (board_data) {
102 int i;
103 for (i = 0; i < tps65910->gpio.ngpio; ++i) {
104 if (board_data->en_gpio_sleep[i]) {
105 ret = tps65910_set_bits(tps65910,
106 TPS65910_GPIO0 + i, GPIO_SLEEP_MASK);
107 if (ret < 0)
108 dev_warn(tps65910->dev,
109 "GPIO Sleep setting failed\n");
110 }
111 }
112 } 181 }
113 182
114 ret = gpiochip_add(&tps65910->gpio); 183 platform_set_drvdata(pdev, tps65910_gpio);
184
185 return ret;
186}
187
188static int __devexit tps65910_gpio_remove(struct platform_device *pdev)
189{
190 struct tps65910_gpio *tps65910_gpio = platform_get_drvdata(pdev);
115 191
116 if (ret) 192 return gpiochip_remove(&tps65910_gpio->gpio_chip);
117 dev_warn(tps65910->dev, "GPIO registration failed: %d\n", ret);
118} 193}
194
195static struct platform_driver tps65910_gpio_driver = {
196 .driver.name = "tps65910-gpio",
197 .driver.owner = THIS_MODULE,
198 .probe = tps65910_gpio_probe,
199 .remove = __devexit_p(tps65910_gpio_remove),
200};
201
202static int __init tps65910_gpio_init(void)
203{
204 return platform_driver_register(&tps65910_gpio_driver);
205}
206subsys_initcall(tps65910_gpio_init);
207
208static void __exit tps65910_gpio_exit(void)
209{
210 platform_driver_unregister(&tps65910_gpio_driver);
211}
212module_exit(tps65910_gpio_exit);
213
214MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
215MODULE_AUTHOR("Jorge Eduardo Candelaria jedu@slimlogic.co.uk>");
216MODULE_DESCRIPTION("GPIO interface for TPS65910/TPS6511 PMICs");
217MODULE_LICENSE("GPL v2");
218MODULE_ALIAS("platform:tps65910-gpio");