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