]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/gpio/twl4030-gpio.c
0ca3a7fa6c03b729f08284078b6c6a68e45d7e01
[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         if (unlikely(gpio >= TWL4030_GPIO_MAX))
106                 return -EPERM;
107
108         return gpio_request(twl_gpiochip.base + gpio, NULL);
109 }
110 EXPORT_SYMBOL(twl4030_request_gpio);
111
112 /*
113  * TWL4030 GPIO free module
114  */
115 int twl4030_free_gpio(int gpio)
116 {
117         if (unlikely(gpio >= TWL4030_GPIO_MAX))
118                 return -EPERM;
119
120         gpio_free(twl_gpiochip.base + gpio);
121         return 0;
122 }
123 EXPORT_SYMBOL(twl4030_free_gpio);
124
125 static int twl4030_set_gpio_direction(int gpio, int is_input)
126 {
127         u8 d_bnk = gpio >> 3;
128         u8 d_msk = BIT(gpio & 0x7);
129         u8 reg = 0;
130         u8 base = REG_GPIODATADIR1 + d_bnk;
131         int ret = 0;
132
133         mutex_lock(&gpio_lock);
134         ret = gpio_twl4030_read(base);
135         if (ret >= 0) {
136                 if (is_input)
137                         reg = ret & ~d_msk;
138                 else
139                         reg = ret | d_msk;
140
141                 ret = gpio_twl4030_write(base, reg);
142         }
143         mutex_unlock(&gpio_lock);
144         return ret;
145 }
146
147 static int twl4030_set_gpio_dataout(int gpio, int enable)
148 {
149         u8 d_bnk = gpio >> 3;
150         u8 d_msk = BIT(gpio & 0x7);
151         u8 base = 0;
152
153         if (enable)
154                 base = REG_SETGPIODATAOUT1 + d_bnk;
155         else
156                 base = REG_CLEARGPIODATAOUT1 + d_bnk;
157
158         return gpio_twl4030_write(base, d_msk);
159 }
160
161 int twl4030_get_gpio_datain(int gpio)
162 {
163         u8 d_bnk = gpio >> 3;
164         u8 d_off = gpio & 0x7;
165         u8 base = 0;
166         int ret = 0;
167
168         if (unlikely((gpio >= TWL4030_GPIO_MAX)
169                 || !(gpio_usage_count & BIT(gpio))))
170                 return -EPERM;
171
172         base = REG_GPIODATAIN1 + d_bnk;
173         ret = gpio_twl4030_read(base);
174         if (ret > 0)
175                 ret = (ret >> d_off) & 0x1;
176
177         return ret;
178 }
179 EXPORT_SYMBOL(twl4030_get_gpio_datain);
180
181 /*
182  * Configure debounce timing value for a GPIO pin on TWL4030
183  */
184 int twl4030_set_gpio_debounce(int gpio, int enable)
185 {
186         u8 d_bnk = gpio >> 3;
187         u8 d_msk = BIT(gpio & 0x7);
188         u8 reg = 0;
189         u8 base = 0;
190         int ret = 0;
191
192         if (unlikely((gpio >= TWL4030_GPIO_MAX)
193                 || !(gpio_usage_count & BIT(gpio))))
194                 return -EPERM;
195
196         base = REG_GPIO_DEBEN1 + d_bnk;
197         mutex_lock(&gpio_lock);
198         ret = gpio_twl4030_read(base);
199         if (ret >= 0) {
200                 if (enable)
201                         reg = ret | d_msk;
202                 else
203                         reg = ret & ~d_msk;
204
205                 ret = gpio_twl4030_write(base, reg);
206         }
207         mutex_unlock(&gpio_lock);
208         return ret;
209 }
210 EXPORT_SYMBOL(twl4030_set_gpio_debounce);
211
212 #if 0
213 /*
214  * Configure Card detect for GPIO pin on TWL4030
215  *
216  * This means:  VMMC1 or VMMC2 is enabled or disabled based
217  * on the status of GPIO-0 or GPIO-1 pins (respectively).
218  */
219 int twl4030_set_gpio_card_detect(int gpio, int enable)
220 {
221         u8 reg = 0;
222         u8 msk = (1 << gpio);
223         int ret = 0;
224
225         /* Only GPIO 0 or 1 can be used for CD feature.. */
226         if (unlikely((gpio >= TWL4030_GPIO_MAX)
227                 || !(gpio_usage_count & BIT(gpio))
228                 || (gpio >= TWL4030_GPIO_MAX_CD))) {
229                 return -EPERM;
230         }
231
232         mutex_lock(&gpio_lock);
233         ret = gpio_twl4030_read(REG_GPIO_CTRL);
234         if (ret >= 0) {
235                 if (enable)
236                         reg = (u8) (ret | msk);
237                 else
238                         reg = (u8) (ret & ~msk);
239
240                 ret = gpio_twl4030_write(REG_GPIO_CTRL, reg);
241         }
242         mutex_unlock(&gpio_lock);
243         return ret;
244 }
245 #endif
246
247 /*----------------------------------------------------------------------*/
248
249 static int twl_request(struct gpio_chip *chip, unsigned offset)
250 {
251         int status = 0;
252
253         mutex_lock(&gpio_lock);
254
255         /* on first use, turn GPIO module "on" */
256         if (!gpio_usage_count)
257                 status = gpio_twl4030_write(REG_GPIO_CTRL,
258                                 MASK_GPIO_CTRL_GPIO_ON);
259
260         if (!status)
261                 gpio_usage_count |= (0x1 << offset);
262
263 done:
264         mutex_unlock(&gpio_lock);
265         return status;
266 }
267
268 static void twl_free(struct gpio_chip *chip, unsigned offset)
269 {
270         mutex_lock(&gpio_lock);
271
272         gpio_usage_count &= ~BIT(offset);
273
274         /* on last use, switch off GPIO module */
275         if (!gpio_usage_count)
276                 gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
277
278         mutex_unlock(&gpio_lock);
279 }
280
281 static int twl_direction_in(struct gpio_chip *chip, unsigned offset)
282 {
283         return twl4030_set_gpio_direction(offset, 1);
284 }
285
286 static int twl_get(struct gpio_chip *chip, unsigned offset)
287 {
288         int status = twl4030_get_gpio_datain(offset);
289
290         return (status < 0) ? 0 : status;
291 }
292
293 static int twl_direction_out(struct gpio_chip *chip, unsigned offset, int value)
294 {
295         twl4030_set_gpio_dataout(offset, value);
296         return twl4030_set_gpio_direction(offset, 0);
297 }
298
299 static void twl_set(struct gpio_chip *chip, unsigned offset, int value)
300 {
301         twl4030_set_gpio_dataout(offset, value);
302 }
303
304 static int twl_to_irq(struct gpio_chip *chip, unsigned offset)
305 {
306         return twl4030_gpio_irq_base
307                 ? (twl4030_gpio_irq_base + offset)
308                 : -EINVAL;
309 }
310
311 static struct gpio_chip twl_gpiochip = {
312         .label                  = "twl4030",
313         .owner                  = THIS_MODULE,
314         .request                = twl_request,
315         .free                   = twl_free,
316         .direction_input        = twl_direction_in,
317         .get                    = twl_get,
318         .direction_output       = twl_direction_out,
319         .set                    = twl_set,
320         .to_irq                 = twl_to_irq,
321         .can_sleep              = 1,
322 };
323
324 /*----------------------------------------------------------------------*/
325
326 static int __devinit gpio_twl4030_pulls(u32 ups, u32 downs)
327 {
328         u8              message[6];
329         unsigned        i, gpio_bit;
330
331         /* For most pins, a pulldown was enabled by default.
332          * We should have data that's specific to this board.
333          */
334         for (gpio_bit = 1, i = 1; i < 6; i++) {
335                 u8              bit_mask;
336                 unsigned        j;
337
338                 for (bit_mask = 0, j = 0; j < 8; j += 2, gpio_bit <<= 1) {
339                         if (ups & gpio_bit)
340                                 bit_mask |= 1 << (j + 1);
341                         else if (downs & gpio_bit)
342                                 bit_mask |= 1 << (j + 0);
343                 }
344                 message[i] = bit_mask;
345         }
346
347         return twl4030_i2c_write(TWL4030_MODULE_GPIO, message,
348                                 REG_GPIOPUPDCTR1, 5);
349 }
350
351 static int gpio_twl4030_remove(struct platform_device *pdev);
352
353 static int __devinit gpio_twl4030_probe(struct platform_device *pdev)
354 {
355         struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
356         int ret;
357
358         /* maybe setup IRQs */
359         if (pdata->irq_base) {
360                 if (is_module()) {
361                         dev_err(&pdev->dev,
362                                 "can't dispatch IRQs from modules\n");
363                         goto no_irqs;
364                 }
365                 ret = twl4030_sih_setup(TWL4030_MODULE_GPIO);
366                 if (ret < 0)
367                         return ret;
368                 WARN_ON(ret != pdata->irq_base);
369                 twl4030_gpio_irq_base = ret;
370         }
371
372 no_irqs:
373         /*
374          * NOTE:  boards may waste power if they don't set pullups
375          * and pulldowns correctly ... default for non-ULPI pins is
376          * pulldown, and some other pins may have external pullups
377          * or pulldowns.  Careful!
378          */
379         ret = gpio_twl4030_pulls(pdata->pullups, pdata->pulldowns);
380         if (ret)
381                 dev_dbg(&pdev->dev, "pullups %.05x %.05x --> %d\n",
382                                 pdata->pullups, pdata->pulldowns,
383                                 ret);
384
385         twl_gpiochip.base = pdata->gpio_base;
386         twl_gpiochip.ngpio = TWL4030_GPIO_MAX;
387         twl_gpiochip.dev = &pdev->dev;
388
389         ret = gpiochip_add(&twl_gpiochip);
390         if (ret < 0) {
391                 dev_err(&pdev->dev,
392                                 "could not register gpiochip, %d\n",
393                                 ret);
394                 twl_gpiochip.ngpio = 0;
395                 gpio_twl4030_remove(pdev);
396         } else if (pdata->setup) {
397                 int status;
398
399                 status = pdata->setup(&pdev->dev,
400                                 pdata->gpio_base, TWL4030_GPIO_MAX);
401                 if (status)
402                         dev_dbg(&pdev->dev, "setup --> %d\n", status);
403         }
404
405         return ret;
406 }
407
408 static int __devexit gpio_twl4030_remove(struct platform_device *pdev)
409 {
410         struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
411         int status;
412
413         if (pdata->teardown) {
414                 status = pdata->teardown(&pdev->dev,
415                                 pdata->gpio_base, TWL4030_GPIO_MAX);
416                 if (status) {
417                         dev_dbg(&pdev->dev, "teardown --> %d\n", status);
418                         return status;
419                 }
420         }
421
422         status = gpiochip_remove(&twl_gpiochip);
423         if (status < 0)
424                 return status;
425
426         if (is_module())
427                 return 0;
428
429         /* REVISIT no support yet for deregistering all the IRQs */
430         WARN_ON(1);
431         return -EIO;
432 }
433
434 /* Note:  this hardware lives inside an I2C-based multi-function device. */
435 MODULE_ALIAS("platform:twl4030_gpio");
436
437 static struct platform_driver gpio_twl4030_driver = {
438         .driver.name    = "twl4030_gpio",
439         .driver.owner   = THIS_MODULE,
440         .probe          = gpio_twl4030_probe,
441         .remove         = __devexit_p(gpio_twl4030_remove),
442 };
443
444 static int __init gpio_twl4030_init(void)
445 {
446         return platform_driver_register(&gpio_twl4030_driver);
447 }
448 subsys_initcall(gpio_twl4030_init);
449
450 static void __exit gpio_twl4030_exit(void)
451 {
452         platform_driver_unregister(&gpio_twl4030_driver);
453 }
454 module_exit(gpio_twl4030_exit);
455
456 MODULE_AUTHOR("Texas Instruments, Inc.");
457 MODULE_DESCRIPTION("GPIO interface for TWL4030");
458 MODULE_LICENSE("GPL");