]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/gpio/twl4030-gpio.c
23150274c50593dd9165841f6e0d76f58dc85ca4
[linux-2.6-omap-h63xx.git] / drivers / gpio / twl4030-gpio.c
1 /*
2  * twl4030_gpio.c -- access to GPIOs on TWL4030/TPS659x0 chips
3  *
4  * Copyright (C) 2006-2007 Texas Instruments, Inc.
5  * Copyright (C) 2006 MontaVista Software, Inc.
6  *
7  * Code re-arranged and cleaned up by:
8  *      Syed Mohammed Khasim <x0khasim@ti.com>
9  *
10  * Initial Code:
11  *      Andy Lowe / Nishanth Menon
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
26  */
27
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
31 #include <linux/kthread.h>
32 #include <linux/irq.h>
33 #include <linux/gpio.h>
34 #include <linux/platform_device.h>
35 #include <linux/slab.h>
36
37 #include <linux/i2c/twl4030.h>
38
39
40 /*
41  * The GPIO "subchip" supports 18 GPIOs which can be configured as
42  * inputs or outputs, with pullups or pulldowns on each pin.  Each
43  * GPIO can trigger interrupts on either or both edges.
44  *
45  * GPIO interrupts can be fed to either of two IRQ lines; this is
46  * intended to support multiple hosts.
47  *
48  * There are also two LED pins used sometimes as output-only GPIOs.
49  *
50  * FIXME code currently only handles the first IRQ line.
51  */
52
53
54 static struct gpio_chip twl_gpiochip;
55 static int twl4030_gpio_irq_base;
56
57 /* genirq interfaces are not available to modules */
58 #ifdef MODULE
59 #define is_module()     true
60 #else
61 #define is_module()     false
62 #endif
63
64 /* GPIO_CTRL Fields */
65 #define MASK_GPIO_CTRL_GPIO0CD1         BIT(0)
66 #define MASK_GPIO_CTRL_GPIO1CD2         BIT(1)
67 #define MASK_GPIO_CTRL_GPIO_ON          BIT(2)
68
69 /* Mask for GPIO registers when aggregated into a 32-bit integer */
70 #define GPIO_32_MASK                    0x0003ffff
71
72 /* Data structures */
73 static DEFINE_MUTEX(gpio_lock);
74
75 /* store usage of each GPIO. - each bit represents one GPIO */
76 static unsigned int gpio_usage_count;
77
78 /*----------------------------------------------------------------------*/
79
80 /*
81  * To configure TWL4030 GPIO module registers
82  */
83 static inline int gpio_twl4030_write(u8 address, u8 data)
84 {
85         return twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, data, address);
86 }
87
88 /*
89  * To read a TWL4030 GPIO module register
90  */
91 static inline int gpio_twl4030_read(u8 address)
92 {
93         u8 data;
94         int ret = 0;
95
96         ret = twl4030_i2c_read_u8(TWL4030_MODULE_GPIO, &data, address);
97         return (ret < 0) ? ret : data;
98 }
99
100 /*
101  * twl4030 GPIO request function
102  */
103 int twl4030_request_gpio(int gpio)
104 {
105         int ret = 0;
106
107         if (unlikely(gpio >= TWL4030_GPIO_MAX))
108                 return -EPERM;
109
110         ret = gpio_request(twl_gpiochip.base + gpio, NULL);
111         if (ret < 0)
112                 return ret;
113
114         mutex_lock(&gpio_lock);
115         if (gpio_usage_count & BIT(gpio)) {
116                 ret = -EBUSY;
117         } else {
118                 /* First time usage? - switch on GPIO module */
119                 if (!gpio_usage_count) {
120                         ret = gpio_twl4030_write(REG_GPIO_CTRL,
121                                         MASK_GPIO_CTRL_GPIO_ON);
122
123                 }
124                 if (!ret)
125                         gpio_usage_count |= BIT(gpio);
126                 else
127                         gpio_free(twl_gpiochip.base + gpio);
128         }
129         mutex_unlock(&gpio_lock);
130         return ret;
131 }
132 EXPORT_SYMBOL(twl4030_request_gpio);
133
134 /*
135  * TWL4030 GPIO free module
136  */
137 int twl4030_free_gpio(int gpio)
138 {
139         int ret = 0;
140
141         if (unlikely(gpio >= TWL4030_GPIO_MAX))
142                 return -EPERM;
143
144         mutex_lock(&gpio_lock);
145
146         if ((gpio_usage_count & BIT(gpio)) == 0) {
147                 ret = -EPERM;
148         } else {
149                 gpio_usage_count &= ~BIT(gpio);
150                 gpio_free(twl_gpiochip.base + gpio);
151         }
152
153         /* Last time usage? - switch off GPIO module */
154         if (ret == 0 && !gpio_usage_count)
155                 ret = gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
156
157         mutex_unlock(&gpio_lock);
158         return ret;
159 }
160 EXPORT_SYMBOL(twl4030_free_gpio);
161
162 static int twl4030_set_gpio_direction(int gpio, int is_input)
163 {
164         u8 d_bnk = gpio >> 3;
165         u8 d_msk = BIT(gpio & 0x7);
166         u8 reg = 0;
167         u8 base = REG_GPIODATADIR1 + d_bnk;
168         int ret = 0;
169
170         mutex_lock(&gpio_lock);
171         ret = gpio_twl4030_read(base);
172         if (ret >= 0) {
173                 if (is_input)
174                         reg = ret & ~d_msk;
175                 else
176                         reg = ret | d_msk;
177
178                 ret = gpio_twl4030_write(base, reg);
179         }
180         mutex_unlock(&gpio_lock);
181         return ret;
182 }
183
184 static int twl4030_set_gpio_dataout(int gpio, int enable)
185 {
186         u8 d_bnk = gpio >> 3;
187         u8 d_msk = BIT(gpio & 0x7);
188         u8 base = 0;
189
190         if (enable)
191                 base = REG_SETGPIODATAOUT1 + d_bnk;
192         else
193                 base = REG_CLEARGPIODATAOUT1 + d_bnk;
194
195         return gpio_twl4030_write(base, d_msk);
196 }
197
198 int twl4030_get_gpio_datain(int gpio)
199 {
200         u8 d_bnk = gpio >> 3;
201         u8 d_off = gpio & 0x7;
202         u8 base = 0;
203         int ret = 0;
204
205         if (unlikely((gpio >= TWL4030_GPIO_MAX)
206                 || !(gpio_usage_count & BIT(gpio))))
207                 return -EPERM;
208
209         base = REG_GPIODATAIN1 + d_bnk;
210         ret = gpio_twl4030_read(base);
211         if (ret > 0)
212                 ret = (ret >> d_off) & 0x1;
213
214         return ret;
215 }
216 EXPORT_SYMBOL(twl4030_get_gpio_datain);
217
218 /*
219  * Configure debounce timing value for a GPIO pin on TWL4030
220  */
221 int twl4030_set_gpio_debounce(int gpio, int enable)
222 {
223         u8 d_bnk = gpio >> 3;
224         u8 d_msk = BIT(gpio & 0x7);
225         u8 reg = 0;
226         u8 base = 0;
227         int ret = 0;
228
229         if (unlikely((gpio >= TWL4030_GPIO_MAX)
230                 || !(gpio_usage_count & BIT(gpio))))
231                 return -EPERM;
232
233         base = REG_GPIO_DEBEN1 + d_bnk;
234         mutex_lock(&gpio_lock);
235         ret = gpio_twl4030_read(base);
236         if (ret >= 0) {
237                 if (enable)
238                         reg = ret | d_msk;
239                 else
240                         reg = ret & ~d_msk;
241
242                 ret = gpio_twl4030_write(base, reg);
243         }
244         mutex_unlock(&gpio_lock);
245         return ret;
246 }
247 EXPORT_SYMBOL(twl4030_set_gpio_debounce);
248
249 #if 0
250 /*
251  * Configure Card detect for GPIO pin on TWL4030
252  *
253  * This means:  VMMC1 or VMMC2 is enabled or disabled based
254  * on the status of GPIO-0 or GPIO-1 pins (respectively).
255  */
256 int twl4030_set_gpio_card_detect(int gpio, int enable)
257 {
258         u8 reg = 0;
259         u8 msk = (1 << gpio);
260         int ret = 0;
261
262         /* Only GPIO 0 or 1 can be used for CD feature.. */
263         if (unlikely((gpio >= TWL4030_GPIO_MAX)
264                 || !(gpio_usage_count & BIT(gpio))
265                 || (gpio >= TWL4030_GPIO_MAX_CD))) {
266                 return -EPERM;
267         }
268
269         mutex_lock(&gpio_lock);
270         ret = gpio_twl4030_read(REG_GPIO_CTRL);
271         if (ret >= 0) {
272                 if (enable)
273                         reg = (u8) (ret | msk);
274                 else
275                         reg = (u8) (ret & ~msk);
276
277                 ret = gpio_twl4030_write(REG_GPIO_CTRL, reg);
278         }
279         mutex_unlock(&gpio_lock);
280         return ret;
281 }
282 #endif
283
284 /*----------------------------------------------------------------------*/
285
286 static int twl_direction_in(struct gpio_chip *chip, unsigned offset)
287 {
288         return twl4030_set_gpio_direction(offset, 1);
289 }
290
291 static int twl_get(struct gpio_chip *chip, unsigned offset)
292 {
293         int status = twl4030_get_gpio_datain(offset);
294
295         return (status < 0) ? 0 : status;
296 }
297
298 static int twl_direction_out(struct gpio_chip *chip, unsigned offset, int value)
299 {
300         twl4030_set_gpio_dataout(offset, value);
301         return twl4030_set_gpio_direction(offset, 0);
302 }
303
304 static void twl_set(struct gpio_chip *chip, unsigned offset, int value)
305 {
306         twl4030_set_gpio_dataout(offset, value);
307 }
308
309 static int twl_to_irq(struct gpio_chip *chip, unsigned offset)
310 {
311         return twl4030_gpio_irq_base
312                 ? (twl4030_gpio_irq_base + offset)
313                 : -EINVAL;
314 }
315
316 static struct gpio_chip twl_gpiochip = {
317         .label                  = "twl4030",
318         .owner                  = THIS_MODULE,
319         .direction_input        = twl_direction_in,
320         .get                    = twl_get,
321         .direction_output       = twl_direction_out,
322         .set                    = twl_set,
323         .to_irq                 = twl_to_irq,
324         .can_sleep              = 1,
325 };
326
327 /*----------------------------------------------------------------------*/
328
329 static int __devinit gpio_twl4030_pulls(u32 ups, u32 downs)
330 {
331         u8              message[6];
332         unsigned        i, gpio_bit;
333
334         /* For most pins, a pulldown was enabled by default.
335          * We should have data that's specific to this board.
336          */
337         for (gpio_bit = 1, i = 1; i < 6; i++) {
338                 u8              bit_mask;
339                 unsigned        j;
340
341                 for (bit_mask = 0, j = 0; j < 8; j += 2, gpio_bit <<= 1) {
342                         if (ups & gpio_bit)
343                                 bit_mask |= 1 << (j + 1);
344                         else if (downs & gpio_bit)
345                                 bit_mask |= 1 << (j + 0);
346                 }
347                 message[i] = bit_mask;
348         }
349
350         return twl4030_i2c_write(TWL4030_MODULE_GPIO, message,
351                                 REG_GPIOPUPDCTR1, 5);
352 }
353
354 static int gpio_twl4030_remove(struct platform_device *pdev);
355
356 static int __devinit gpio_twl4030_probe(struct platform_device *pdev)
357 {
358         struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
359         int ret;
360
361         /* maybe setup IRQs */
362         if (pdata->irq_base) {
363                 if (is_module()) {
364                         dev_err(&pdev->dev,
365                                 "can't dispatch IRQs from modules\n");
366                         goto no_irqs;
367                 }
368                 ret = twl4030_sih_setup(TWL4030_MODULE_GPIO);
369                 if (ret < 0)
370                         return ret;
371                 WARN_ON(ret != pdata->irq_base);
372                 twl4030_gpio_irq_base = ret;
373         }
374
375 no_irqs:
376         /*
377          * NOTE:  boards may waste power if they don't set pullups
378          * and pulldowns correctly ... default for non-ULPI pins is
379          * pulldown, and some other pins may have external pullups
380          * or pulldowns.  Careful!
381          */
382         ret = gpio_twl4030_pulls(pdata->pullups, pdata->pulldowns);
383         if (ret)
384                 dev_dbg(&pdev->dev, "pullups %.05x %.05x --> %d\n",
385                                 pdata->pullups, pdata->pulldowns,
386                                 ret);
387
388         twl_gpiochip.base = pdata->gpio_base;
389         twl_gpiochip.ngpio = TWL4030_GPIO_MAX;
390         twl_gpiochip.dev = &pdev->dev;
391
392         ret = gpiochip_add(&twl_gpiochip);
393         if (ret < 0) {
394                 dev_err(&pdev->dev,
395                                 "could not register gpiochip, %d\n",
396                                 ret);
397                 twl_gpiochip.ngpio = 0;
398                 gpio_twl4030_remove(pdev);
399         } else if (pdata->setup) {
400                 int status;
401
402                 status = pdata->setup(&pdev->dev,
403                                 pdata->gpio_base, TWL4030_GPIO_MAX);
404                 if (status)
405                         dev_dbg(&pdev->dev, "setup --> %d\n", status);
406         }
407
408         return ret;
409 }
410
411 static int __devexit gpio_twl4030_remove(struct platform_device *pdev)
412 {
413         struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
414         int status;
415
416         if (pdata->teardown) {
417                 status = pdata->teardown(&pdev->dev,
418                                 pdata->gpio_base, TWL4030_GPIO_MAX);
419                 if (status) {
420                         dev_dbg(&pdev->dev, "teardown --> %d\n", status);
421                         return status;
422                 }
423         }
424
425         status = gpiochip_remove(&twl_gpiochip);
426         if (status < 0)
427                 return status;
428
429         if (is_module())
430                 return 0;
431
432         /* REVISIT no support yet for deregistering all the IRQs */
433         WARN_ON(1);
434         return -EIO;
435 }
436
437 /* Note:  this hardware lives inside an I2C-based multi-function device. */
438 MODULE_ALIAS("platform:twl4030_gpio");
439
440 static struct platform_driver gpio_twl4030_driver = {
441         .driver.name    = "twl4030_gpio",
442         .driver.owner   = THIS_MODULE,
443         .probe          = gpio_twl4030_probe,
444         .remove         = __devexit_p(gpio_twl4030_remove),
445 };
446
447 static int __init gpio_twl4030_init(void)
448 {
449         return platform_driver_register(&gpio_twl4030_driver);
450 }
451 subsys_initcall(gpio_twl4030_init);
452
453 static void __exit gpio_twl4030_exit(void)
454 {
455         platform_driver_unregister(&gpio_twl4030_driver);
456 }
457 module_exit(gpio_twl4030_exit);
458
459 MODULE_AUTHOR("Texas Instruments, Inc.");
460 MODULE_DESCRIPTION("GPIO interface for TWL4030");
461 MODULE_LICENSE("GPL");