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;
100 static int twl4030_set_gpio_direction(int gpio, int is_input)
102 u8 d_bnk = gpio >> 3;
103 u8 d_msk = BIT(gpio & 0x7);
105 u8 base = REG_GPIODATADIR1 + d_bnk;
108 mutex_lock(&gpio_lock);
109 ret = gpio_twl4030_read(base);
116 ret = gpio_twl4030_write(base, reg);
118 mutex_unlock(&gpio_lock);
122 static int twl4030_set_gpio_dataout(int gpio, int enable)
124 u8 d_bnk = gpio >> 3;
125 u8 d_msk = BIT(gpio & 0x7);
129 base = REG_SETGPIODATAOUT1 + d_bnk;
131 base = REG_CLEARGPIODATAOUT1 + d_bnk;
133 return gpio_twl4030_write(base, d_msk);
136 static int twl4030_get_gpio_datain(int gpio)
138 u8 d_bnk = gpio >> 3;
139 u8 d_off = gpio & 0x7;
143 if (unlikely((gpio >= TWL4030_GPIO_MAX)
144 || !(gpio_usage_count & BIT(gpio))))
147 base = REG_GPIODATAIN1 + d_bnk;
148 ret = gpio_twl4030_read(base);
150 ret = (ret >> d_off) & 0x1;
156 * Configure debounce timing value for a GPIO pin on TWL4030
158 int twl4030_set_gpio_debounce(int gpio, int enable)
160 u8 d_bnk = gpio >> 3;
161 u8 d_msk = BIT(gpio & 0x7);
166 if (unlikely((gpio >= TWL4030_GPIO_MAX)
167 || !(gpio_usage_count & BIT(gpio))))
170 base = REG_GPIO_DEBEN1 + d_bnk;
171 mutex_lock(&gpio_lock);
172 ret = gpio_twl4030_read(base);
179 ret = gpio_twl4030_write(base, reg);
181 mutex_unlock(&gpio_lock);
184 EXPORT_SYMBOL(twl4030_set_gpio_debounce);
188 * Configure Card detect for GPIO pin on TWL4030
190 * This means: VMMC1 or VMMC2 is enabled or disabled based
191 * on the status of GPIO-0 or GPIO-1 pins (respectively).
193 int twl4030_set_gpio_card_detect(int gpio, int enable)
196 u8 msk = (1 << gpio);
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))) {
206 mutex_lock(&gpio_lock);
207 ret = gpio_twl4030_read(REG_GPIO_CTRL);
210 reg = (u8) (ret | msk);
212 reg = (u8) (ret & ~msk);
214 ret = gpio_twl4030_write(REG_GPIO_CTRL, reg);
216 mutex_unlock(&gpio_lock);
221 /*----------------------------------------------------------------------*/
223 static int twl_request(struct gpio_chip *chip, unsigned offset)
227 mutex_lock(&gpio_lock);
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);
235 gpio_usage_count |= (0x1 << offset);
238 mutex_unlock(&gpio_lock);
242 static void twl_free(struct gpio_chip *chip, unsigned offset)
244 mutex_lock(&gpio_lock);
246 gpio_usage_count &= ~BIT(offset);
248 /* on last use, switch off GPIO module */
249 if (!gpio_usage_count)
250 gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
252 mutex_unlock(&gpio_lock);
255 static int twl_direction_in(struct gpio_chip *chip, unsigned offset)
257 return twl4030_set_gpio_direction(offset, 1);
260 static int twl_get(struct gpio_chip *chip, unsigned offset)
262 int status = twl4030_get_gpio_datain(offset);
264 return (status < 0) ? 0 : status;
267 static int twl_direction_out(struct gpio_chip *chip, unsigned offset, int value)
269 twl4030_set_gpio_dataout(offset, value);
270 return twl4030_set_gpio_direction(offset, 0);
273 static void twl_set(struct gpio_chip *chip, unsigned offset, int value)
275 twl4030_set_gpio_dataout(offset, value);
278 static int twl_to_irq(struct gpio_chip *chip, unsigned offset)
280 return twl4030_gpio_irq_base
281 ? (twl4030_gpio_irq_base + offset)
285 static struct gpio_chip twl_gpiochip = {
287 .owner = THIS_MODULE,
288 .request = twl_request,
290 .direction_input = twl_direction_in,
292 .direction_output = twl_direction_out,
294 .to_irq = twl_to_irq,
298 /*----------------------------------------------------------------------*/
300 static int __devinit gpio_twl4030_pulls(u32 ups, u32 downs)
303 unsigned i, gpio_bit;
305 /* For most pins, a pulldown was enabled by default.
306 * We should have data that's specific to this board.
308 for (gpio_bit = 1, i = 1; i < 6; i++) {
312 for (bit_mask = 0, j = 0; j < 8; j += 2, gpio_bit <<= 1) {
314 bit_mask |= 1 << (j + 1);
315 else if (downs & gpio_bit)
316 bit_mask |= 1 << (j + 0);
318 message[i] = bit_mask;
321 return twl4030_i2c_write(TWL4030_MODULE_GPIO, message,
322 REG_GPIOPUPDCTR1, 5);
325 static int gpio_twl4030_remove(struct platform_device *pdev);
327 static int __devinit gpio_twl4030_probe(struct platform_device *pdev)
329 struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
332 /* maybe setup IRQs */
333 if (pdata->irq_base) {
336 "can't dispatch IRQs from modules\n");
339 ret = twl4030_sih_setup(TWL4030_MODULE_GPIO);
342 WARN_ON(ret != pdata->irq_base);
343 twl4030_gpio_irq_base = ret;
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!
353 ret = gpio_twl4030_pulls(pdata->pullups, pdata->pulldowns);
355 dev_dbg(&pdev->dev, "pullups %.05x %.05x --> %d\n",
356 pdata->pullups, pdata->pulldowns,
359 twl_gpiochip.base = pdata->gpio_base;
360 twl_gpiochip.ngpio = TWL4030_GPIO_MAX;
361 twl_gpiochip.dev = &pdev->dev;
363 ret = gpiochip_add(&twl_gpiochip);
366 "could not register gpiochip, %d\n",
368 twl_gpiochip.ngpio = 0;
369 gpio_twl4030_remove(pdev);
370 } else if (pdata->setup) {
373 status = pdata->setup(&pdev->dev,
374 pdata->gpio_base, TWL4030_GPIO_MAX);
376 dev_dbg(&pdev->dev, "setup --> %d\n", status);
382 static int __devexit gpio_twl4030_remove(struct platform_device *pdev)
384 struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
387 if (pdata->teardown) {
388 status = pdata->teardown(&pdev->dev,
389 pdata->gpio_base, TWL4030_GPIO_MAX);
391 dev_dbg(&pdev->dev, "teardown --> %d\n", status);
396 status = gpiochip_remove(&twl_gpiochip);
403 /* REVISIT no support yet for deregistering all the IRQs */
408 /* Note: this hardware lives inside an I2C-based multi-function device. */
409 MODULE_ALIAS("platform:twl4030_gpio");
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),
418 static int __init gpio_twl4030_init(void)
420 return platform_driver_register(&gpio_twl4030_driver);
422 subsys_initcall(gpio_twl4030_init);
424 static void __exit gpio_twl4030_exit(void)
426 platform_driver_unregister(&gpio_twl4030_driver);
428 module_exit(gpio_twl4030_exit);
430 MODULE_AUTHOR("Texas Instruments, Inc.");
431 MODULE_DESCRIPTION("GPIO interface for TWL4030");
432 MODULE_LICENSE("GPL");