Merge remote-tracking branch 'origin/develop-3.0-rk2928' into develop-3.0
[firefly-linux-kernel-4.4.55.git] / drivers / gpio / wm831x-gpio.c
1 /*
2  * wm831x-gpio.c  --  gpiolib support for Wolfson WM831x PMICs
3  *
4  * Copyright 2009 Wolfson Microelectronics PLC.
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  *
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/gpio.h>
19 #include <linux/mfd/core.h>
20 #include <linux/platform_device.h>
21 #include <linux/seq_file.h>
22
23 #include <linux/mfd/wm831x/core.h>
24 #include <linux/mfd/wm831x/pdata.h>
25 #include <linux/mfd/wm831x/gpio.h>
26 #include <linux/mfd/wm831x/irq.h>
27
28 struct wm831x_gpio {
29         struct wm831x *wm831x;
30         struct gpio_chip gpio_chip;
31 };
32
33 static inline struct wm831x_gpio *to_wm831x_gpio(struct gpio_chip *chip)
34 {
35         return container_of(chip, struct wm831x_gpio, gpio_chip);
36 }
37
38 static int wm831x_gpio_pull_up_down(struct gpio_chip *chip, unsigned offset, unsigned value)
39 {
40         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
41         struct wm831x *wm831x = wm831x_gpio->wm831x;
42
43         if(value == GPIOPullUp)
44                 value = WM831X_GPIO_PULL_UP;
45         else if(value == GPIOPullDown)
46                 value = WM831X_GPIO_PULL_DOWN;
47         else if(value == GPIONormal)
48                 value = WM831X_GPIO_PULL_NONE;
49         //printk("wm831x_gpio_pull_up_down=%x,%x\n",WM831X_GPIO1_CONTROL + offset,value);
50         return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset, 
51                 WM831X_GPN_PULL_MASK, value);
52 }
53
54 static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
55 {
56         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
57         struct wm831x *wm831x = wm831x_gpio->wm831x;
58         int val = WM831X_GPN_DIR;
59
60         if (wm831x->has_gpio_ena)
61                 val |= WM831X_GPN_TRI;
62         //printk("wm831x_gpio_direction_in=%x,%x\n",WM831X_GPIO1_CONTROL + offset,val);
63         return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
64                                WM831X_GPN_DIR | WM831X_GPN_TRI |
65                                WM831X_GPN_FN_MASK, val);
66 }
67
68 static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset)
69 {
70         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
71         struct wm831x *wm831x = wm831x_gpio->wm831x;
72         int ret;
73         int gpn_pol;
74         
75         ret = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + offset);
76         if (ret < 0)
77                 return ret;
78         gpn_pol = (ret & WM831X_GPN_POL_MASK) >> WM831X_GPN_POL_SHIFT;
79         
80         ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
81         //printk("wm831x_gpio_get=%x,%d,%d\n",ret,offset,gpn_pol);
82         if (ret < 0)
83                 return ret;
84         
85         return !((ret>>offset)^gpn_pol);
86 }
87
88 static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
89 {
90         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
91         struct wm831x *wm831x = wm831x_gpio->wm831x;
92
93         wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset,
94                         value << offset);
95 }
96
97 static int wm831x_gpio_direction_out(struct gpio_chip *chip,
98                                      unsigned offset, int value)
99 {
100         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
101         struct wm831x *wm831x = wm831x_gpio->wm831x;
102         int val = 0;
103         int ret;
104
105         if (wm831x->has_gpio_ena)
106                 val |= WM831X_GPN_TRI;
107         //printk("wm831x_gpio_direction_out=%x,%x\n",WM831X_GPIO1_CONTROL + offset,val);
108         ret = wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
109                               WM831X_GPN_DIR | WM831X_GPN_TRI |
110                               WM831X_GPN_FN_MASK | WM831X_GPN_POL_MASK, val|WM831X_GPN_POL);
111         if (ret < 0)
112                 return ret;
113
114         /* Can only set GPIO state once it's in output mode */
115         wm831x_gpio_set(chip, offset, value);
116
117         return 0;
118 }
119
120 static int wm831x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
121 {
122         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
123         struct wm831x *wm831x = wm831x_gpio->wm831x;
124
125         if (!wm831x->irq_base)
126                 return -EINVAL;
127
128         return wm831x->irq_base + WM831X_IRQ_GPIO_1 + offset;
129 }
130
131 static int wm831x_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
132                                     unsigned debounce)
133 {
134         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
135         struct wm831x *wm831x = wm831x_gpio->wm831x;
136         int reg = WM831X_GPIO1_CONTROL + offset;
137         int ret, fn;
138
139         ret = wm831x_reg_read(wm831x, reg);
140         if (ret < 0)
141                 return ret;
142
143         switch (ret & WM831X_GPN_FN_MASK) {
144         case 0:
145         case 1:
146                 break;
147         default:
148                 /* Not in GPIO mode */
149                 return -EBUSY;
150         }
151
152         if (debounce >= 32 && debounce <= 64)
153                 fn = 0;
154         else if (debounce >= 4000 && debounce <= 8000)
155                 fn = 1;
156         else
157                 return -EINVAL;
158         //printk("wm831x_gpio_set_debounce=%x,%x\n",WM831X_GPIO1_CONTROL + offset,fn);
159         return wm831x_set_bits(wm831x, reg, WM831X_GPN_FN_MASK, fn);
160 }
161
162 #ifdef CONFIG_DEBUG_FS
163 static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
164 {
165         struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
166         struct wm831x *wm831x = wm831x_gpio->wm831x;
167         int i, tristated;
168
169         for (i = 0; i < chip->ngpio; i++) {
170                 int gpio = i + chip->base;
171                 int reg;
172                 const char *label, *pull, *powerdomain;
173
174                 /* We report the GPIO even if it's not requested since
175                  * we're also reporting things like alternate
176                  * functions which apply even when the GPIO is not in
177                  * use as a GPIO.
178                  */
179                 label = gpiochip_is_requested(chip, i);
180                 if (!label)
181                         label = "Unrequested";
182
183                 seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
184
185                 reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i);
186                 if (reg < 0) {
187                         dev_err(wm831x->dev,
188                                 "GPIO control %d read failed: %d\n",
189                                 gpio, reg);
190                         seq_printf(s, "\n");
191                         continue;
192                 }
193
194                 switch (reg & WM831X_GPN_PULL_MASK) {
195                 case WM831X_GPIO_PULL_NONE:
196                         pull = "nopull";
197                         break;
198                 case WM831X_GPIO_PULL_DOWN:
199                         pull = "pulldown";
200                         break;
201                 case WM831X_GPIO_PULL_UP:
202                         pull = "pullup";
203                         break;
204                 default:
205                         pull = "INVALID PULL";
206                         break;
207                 }
208
209                 switch (i + 1) {
210                 case 1 ... 3:
211                 case 7 ... 9:
212                         if (reg & WM831X_GPN_PWR_DOM)
213                                 powerdomain = "VPMIC";
214                         else
215                                 powerdomain = "DBVDD";
216                         break;
217
218                 case 4 ... 6:
219                 case 10 ... 12:
220                         if (reg & WM831X_GPN_PWR_DOM)
221                                 powerdomain = "SYSVDD";
222                         else
223                                 powerdomain = "DBVDD";
224                         break;
225
226                 case 13 ... 16:
227                         powerdomain = "TPVDD";
228                         break;
229
230                 default:
231                         BUG();
232                         break;
233                 }
234
235                 tristated = reg & WM831X_GPN_TRI;
236                 if (wm831x->has_gpio_ena)
237                         tristated = !tristated;
238
239                 seq_printf(s, " %s %s %s %s%s\n"
240                            "                                  %s%s (0x%4x)\n",
241                            reg & WM831X_GPN_DIR ? "in" : "out",
242                            wm831x_gpio_get(chip, i) ? "high" : "low",
243                            pull,
244                            powerdomain,
245                            reg & WM831X_GPN_POL ? "" : " inverted",
246                            reg & WM831X_GPN_OD ? "open-drain" : "CMOS",
247                            tristated ? " tristated" : "",
248                            reg);
249         }
250 }
251 #else
252 #define wm831x_gpio_dbg_show NULL
253 #endif
254
255 static struct gpio_chip template_chip = {
256         .label                  = "wm831x",
257         .owner                  = THIS_MODULE,
258         .direction_input        = wm831x_gpio_direction_in,
259         .get                    = wm831x_gpio_get,
260         .direction_output       = wm831x_gpio_direction_out,
261         .set                    = wm831x_gpio_set,
262         .pull_updown    = wm831x_gpio_pull_up_down,
263         .to_irq                 = wm831x_gpio_to_irq,
264         .set_debounce           = wm831x_gpio_set_debounce,
265         .dbg_show               = wm831x_gpio_dbg_show,
266         .can_sleep              = 1,
267 };
268
269 static int __devinit wm831x_gpio_probe(struct platform_device *pdev)
270 {
271         struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
272         struct wm831x_pdata *pdata = wm831x->dev->platform_data;
273         struct wm831x_gpio *wm831x_gpio;
274         int ret;
275         printk("%s\n",__FUNCTION__);
276         wm831x_gpio = kzalloc(sizeof(*wm831x_gpio), GFP_KERNEL);
277         if (wm831x_gpio == NULL)
278                 return -ENOMEM;
279         
280         wm831x_gpio->wm831x = wm831x;
281         wm831x_gpio->gpio_chip = template_chip;
282         wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio;
283         wm831x_gpio->gpio_chip.dev = &pdev->dev;
284         if (pdata && pdata->gpio_base)
285                 wm831x_gpio->gpio_chip.base = pdata->gpio_base;
286         else
287                 wm831x_gpio->gpio_chip.base = -1;
288
289         ret = gpiochip_add(&wm831x_gpio->gpio_chip);
290         if (ret < 0) {
291                 dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
292                         ret);
293                 goto err;
294         }
295
296 #ifdef CONFIG_PLAT_RK
297         if (pdata && pdata->pin_type_init) {
298                 ret = pdata->pin_type_init(wm831x);
299                 if (ret != 0) {
300                         dev_err(wm831x->dev, "pin_type_init() failed: %d\n", ret);
301                         WARN_ON(gpiochip_remove(&wm831x_gpio->gpio_chip));
302                         goto err;
303                 }
304         }
305 #endif
306
307         platform_set_drvdata(pdev, wm831x_gpio);
308
309         return ret;
310
311 err:
312         kfree(wm831x_gpio);
313         return ret;
314 }
315
316 static int __devexit wm831x_gpio_remove(struct platform_device *pdev)
317 {
318         struct wm831x_gpio *wm831x_gpio = platform_get_drvdata(pdev);
319         int ret;
320
321         ret = gpiochip_remove(&wm831x_gpio->gpio_chip);
322         if (ret == 0)
323                 kfree(wm831x_gpio);
324
325         return ret;
326 }
327
328 static struct platform_driver wm831x_gpio_driver = {
329         .driver.name    = "wm831x-gpio",
330         .driver.owner   = THIS_MODULE,
331         .probe          = wm831x_gpio_probe,
332         .remove         = __devexit_p(wm831x_gpio_remove),
333 };
334
335 static int __init wm831x_gpio_init(void)
336 {
337         return platform_driver_register(&wm831x_gpio_driver);
338 }
339 subsys_initcall(wm831x_gpio_init);
340
341 static void __exit wm831x_gpio_exit(void)
342 {
343         platform_driver_unregister(&wm831x_gpio_driver);
344 }
345 module_exit(wm831x_gpio_exit);
346
347 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
348 MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
349 MODULE_LICENSE("GPL");
350 MODULE_ALIAS("platform:wm831x-gpio");