2 * twl4030_gpio.c -- access to GPIOs on TWL4030/TPS659x0 chips
4 * Copyright (C) 2006-2007 Texas Instruments, Inc.
5 * Copyright (C) 2006 MontaVista Software, Inc.
7 * Code re-arranged and cleaned up by:
8 * Syed Mohammed Khasim <x0khasim@ti.com>
11 * Andy Lowe / Nishanth Menon
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.
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.
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
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>
37 #include <linux/i2c/twl4030.h>
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.
45 * GPIO interrupts can be fed to either of two IRQ lines; this is
46 * intended to support multiple hosts.
48 * There are also two LED pins used sometimes as output-only GPIOs.
50 * FIXME code currently only handles the first IRQ line.
54 static struct gpio_chip twl_gpiochip;
55 static int twl4030_gpio_irq_base;
57 /* genirq interfaces are not available to modules */
59 #define is_module() true
61 #define is_module() false
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)
69 /* Mask for GPIO registers when aggregated into a 32-bit integer */
70 #define GPIO_32_MASK 0x0003ffff
73 static DEFINE_MUTEX(gpio_lock);
75 /* store usage of each GPIO. - each bit represents one GPIO */
76 static unsigned int gpio_usage_count;
78 /*----------------------------------------------------------------------*/
81 * To configure TWL4030 GPIO module registers
83 static inline int gpio_twl4030_write(u8 address, u8 data)
85 return twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, data, address);
89 * To read a TWL4030 GPIO module register
91 static inline int gpio_twl4030_read(u8 address)
96 ret = twl4030_i2c_read_u8(TWL4030_MODULE_GPIO, &data, address);
97 return (ret < 0) ? ret : data;
101 * twl4030 GPIO request function
103 int twl4030_request_gpio(int gpio)
107 if (unlikely(gpio >= TWL4030_GPIO_MAX))
110 ret = gpio_request(twl_gpiochip.base + gpio, NULL);
114 mutex_lock(&gpio_lock);
115 if (gpio_usage_count & BIT(gpio)) {
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);
125 gpio_usage_count |= BIT(gpio);
127 gpio_free(twl_gpiochip.base + gpio);
129 mutex_unlock(&gpio_lock);
132 EXPORT_SYMBOL(twl4030_request_gpio);
135 * TWL4030 GPIO free module
137 int twl4030_free_gpio(int gpio)
141 if (unlikely(gpio >= TWL4030_GPIO_MAX))
144 mutex_lock(&gpio_lock);
146 if ((gpio_usage_count & BIT(gpio)) == 0) {
149 gpio_usage_count &= ~BIT(gpio);
150 gpio_free(twl_gpiochip.base + gpio);
153 /* Last time usage? - switch off GPIO module */
154 if (ret == 0 && !gpio_usage_count)
155 ret = gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
157 mutex_unlock(&gpio_lock);
160 EXPORT_SYMBOL(twl4030_free_gpio);
162 static int twl4030_set_gpio_direction(int gpio, int is_input)
164 u8 d_bnk = gpio >> 3;
165 u8 d_msk = BIT(gpio & 0x7);
167 u8 base = REG_GPIODATADIR1 + d_bnk;
170 mutex_lock(&gpio_lock);
171 ret = gpio_twl4030_read(base);
178 ret = gpio_twl4030_write(base, reg);
180 mutex_unlock(&gpio_lock);
184 static int twl4030_set_gpio_dataout(int gpio, int enable)
186 u8 d_bnk = gpio >> 3;
187 u8 d_msk = BIT(gpio & 0x7);
191 base = REG_SETGPIODATAOUT1 + d_bnk;
193 base = REG_CLEARGPIODATAOUT1 + d_bnk;
195 return gpio_twl4030_write(base, d_msk);
198 int twl4030_get_gpio_datain(int gpio)
200 u8 d_bnk = gpio >> 3;
201 u8 d_off = gpio & 0x7;
205 if (unlikely((gpio >= TWL4030_GPIO_MAX)
206 || !(gpio_usage_count & BIT(gpio))))
209 base = REG_GPIODATAIN1 + d_bnk;
210 ret = gpio_twl4030_read(base);
212 ret = (ret >> d_off) & 0x1;
216 EXPORT_SYMBOL(twl4030_get_gpio_datain);
219 * Configure debounce timing value for a GPIO pin on TWL4030
221 int twl4030_set_gpio_debounce(int gpio, int enable)
223 u8 d_bnk = gpio >> 3;
224 u8 d_msk = BIT(gpio & 0x7);
229 if (unlikely((gpio >= TWL4030_GPIO_MAX)
230 || !(gpio_usage_count & BIT(gpio))))
233 base = REG_GPIO_DEBEN1 + d_bnk;
234 mutex_lock(&gpio_lock);
235 ret = gpio_twl4030_read(base);
242 ret = gpio_twl4030_write(base, reg);
244 mutex_unlock(&gpio_lock);
247 EXPORT_SYMBOL(twl4030_set_gpio_debounce);
251 * Configure Card detect for GPIO pin on TWL4030
253 * This means: VMMC1 or VMMC2 is enabled or disabled based
254 * on the status of GPIO-0 or GPIO-1 pins (respectively).
256 int twl4030_set_gpio_card_detect(int gpio, int enable)
259 u8 msk = (1 << gpio);
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))) {
269 mutex_lock(&gpio_lock);
270 ret = gpio_twl4030_read(REG_GPIO_CTRL);
273 reg = (u8) (ret | msk);
275 reg = (u8) (ret & ~msk);
277 ret = gpio_twl4030_write(REG_GPIO_CTRL, reg);
279 mutex_unlock(&gpio_lock);
284 /*----------------------------------------------------------------------*/
286 static int twl_direction_in(struct gpio_chip *chip, unsigned offset)
288 return twl4030_set_gpio_direction(offset, 1);
291 static int twl_get(struct gpio_chip *chip, unsigned offset)
293 int status = twl4030_get_gpio_datain(offset);
295 return (status < 0) ? 0 : status;
298 static int twl_direction_out(struct gpio_chip *chip, unsigned offset, int value)
300 twl4030_set_gpio_dataout(offset, value);
301 return twl4030_set_gpio_direction(offset, 0);
304 static void twl_set(struct gpio_chip *chip, unsigned offset, int value)
306 twl4030_set_gpio_dataout(offset, value);
309 static int twl_to_irq(struct gpio_chip *chip, unsigned offset)
311 return twl4030_gpio_irq_base
312 ? (twl4030_gpio_irq_base + offset)
316 static struct gpio_chip twl_gpiochip = {
318 .owner = THIS_MODULE,
319 .direction_input = twl_direction_in,
321 .direction_output = twl_direction_out,
323 .to_irq = twl_to_irq,
327 /*----------------------------------------------------------------------*/
329 static int __devinit gpio_twl4030_pulls(u32 ups, u32 downs)
332 unsigned i, gpio_bit;
334 /* For most pins, a pulldown was enabled by default.
335 * We should have data that's specific to this board.
337 for (gpio_bit = 1, i = 1; i < 6; i++) {
341 for (bit_mask = 0, j = 0; j < 8; j += 2, gpio_bit <<= 1) {
343 bit_mask |= 1 << (j + 1);
344 else if (downs & gpio_bit)
345 bit_mask |= 1 << (j + 0);
347 message[i] = bit_mask;
350 return twl4030_i2c_write(TWL4030_MODULE_GPIO, message,
351 REG_GPIOPUPDCTR1, 5);
354 static int gpio_twl4030_remove(struct platform_device *pdev);
356 static int __devinit gpio_twl4030_probe(struct platform_device *pdev)
358 struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
361 /* maybe setup IRQs */
362 if (pdata->irq_base) {
365 "can't dispatch IRQs from modules\n");
368 ret = twl4030_sih_setup(TWL4030_MODULE_GPIO);
371 WARN_ON(ret != pdata->irq_base);
372 twl4030_gpio_irq_base = ret;
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!
382 ret = gpio_twl4030_pulls(pdata->pullups, pdata->pulldowns);
384 dev_dbg(&pdev->dev, "pullups %.05x %.05x --> %d\n",
385 pdata->pullups, pdata->pulldowns,
388 twl_gpiochip.base = pdata->gpio_base;
389 twl_gpiochip.ngpio = TWL4030_GPIO_MAX;
390 twl_gpiochip.dev = &pdev->dev;
392 ret = gpiochip_add(&twl_gpiochip);
395 "could not register gpiochip, %d\n",
397 twl_gpiochip.ngpio = 0;
398 gpio_twl4030_remove(pdev);
399 } else if (pdata->setup) {
402 status = pdata->setup(&pdev->dev,
403 pdata->gpio_base, TWL4030_GPIO_MAX);
405 dev_dbg(&pdev->dev, "setup --> %d\n", status);
411 static int __devexit gpio_twl4030_remove(struct platform_device *pdev)
413 struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
416 if (pdata->teardown) {
417 status = pdata->teardown(&pdev->dev,
418 pdata->gpio_base, TWL4030_GPIO_MAX);
420 dev_dbg(&pdev->dev, "teardown --> %d\n", status);
425 status = gpiochip_remove(&twl_gpiochip);
432 /* REVISIT no support yet for deregistering all the IRQs */
437 /* Note: this hardware lives inside an I2C-based multi-function device. */
438 MODULE_ALIAS("platform:twl4030_gpio");
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),
447 static int __init gpio_twl4030_init(void)
449 return platform_driver_register(&gpio_twl4030_driver);
451 subsys_initcall(gpio_twl4030_init);
453 static void __exit gpio_twl4030_exit(void)
455 platform_driver_unregister(&gpio_twl4030_driver);
457 module_exit(gpio_twl4030_exit);
459 MODULE_AUTHOR("Texas Instruments, Inc.");
460 MODULE_DESCRIPTION("GPIO interface for TWL4030");
461 MODULE_LICENSE("GPL");