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)
105 if (unlikely(gpio >= TWL4030_GPIO_MAX))
108 return gpio_request(twl_gpiochip.base + gpio, NULL);
110 EXPORT_SYMBOL(twl4030_request_gpio);
113 * TWL4030 GPIO free module
115 int twl4030_free_gpio(int gpio)
117 if (unlikely(gpio >= TWL4030_GPIO_MAX))
120 gpio_free(twl_gpiochip.base + gpio);
123 EXPORT_SYMBOL(twl4030_free_gpio);
125 static int twl4030_set_gpio_direction(int gpio, int is_input)
127 u8 d_bnk = gpio >> 3;
128 u8 d_msk = BIT(gpio & 0x7);
130 u8 base = REG_GPIODATADIR1 + d_bnk;
133 mutex_lock(&gpio_lock);
134 ret = gpio_twl4030_read(base);
141 ret = gpio_twl4030_write(base, reg);
143 mutex_unlock(&gpio_lock);
147 static int twl4030_set_gpio_dataout(int gpio, int enable)
149 u8 d_bnk = gpio >> 3;
150 u8 d_msk = BIT(gpio & 0x7);
154 base = REG_SETGPIODATAOUT1 + d_bnk;
156 base = REG_CLEARGPIODATAOUT1 + d_bnk;
158 return gpio_twl4030_write(base, d_msk);
161 int twl4030_get_gpio_datain(int gpio)
163 u8 d_bnk = gpio >> 3;
164 u8 d_off = gpio & 0x7;
168 if (unlikely((gpio >= TWL4030_GPIO_MAX)
169 || !(gpio_usage_count & BIT(gpio))))
172 base = REG_GPIODATAIN1 + d_bnk;
173 ret = gpio_twl4030_read(base);
175 ret = (ret >> d_off) & 0x1;
179 EXPORT_SYMBOL(twl4030_get_gpio_datain);
182 * Configure debounce timing value for a GPIO pin on TWL4030
184 int twl4030_set_gpio_debounce(int gpio, int enable)
186 u8 d_bnk = gpio >> 3;
187 u8 d_msk = BIT(gpio & 0x7);
192 if (unlikely((gpio >= TWL4030_GPIO_MAX)
193 || !(gpio_usage_count & BIT(gpio))))
196 base = REG_GPIO_DEBEN1 + d_bnk;
197 mutex_lock(&gpio_lock);
198 ret = gpio_twl4030_read(base);
205 ret = gpio_twl4030_write(base, reg);
207 mutex_unlock(&gpio_lock);
210 EXPORT_SYMBOL(twl4030_set_gpio_debounce);
214 * Configure Card detect for GPIO pin on TWL4030
216 * This means: VMMC1 or VMMC2 is enabled or disabled based
217 * on the status of GPIO-0 or GPIO-1 pins (respectively).
219 int twl4030_set_gpio_card_detect(int gpio, int enable)
222 u8 msk = (1 << gpio);
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))) {
232 mutex_lock(&gpio_lock);
233 ret = gpio_twl4030_read(REG_GPIO_CTRL);
236 reg = (u8) (ret | msk);
238 reg = (u8) (ret & ~msk);
240 ret = gpio_twl4030_write(REG_GPIO_CTRL, reg);
242 mutex_unlock(&gpio_lock);
247 /*----------------------------------------------------------------------*/
249 static int twl_request(struct gpio_chip *chip, unsigned offset)
253 mutex_lock(&gpio_lock);
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);
261 gpio_usage_count |= (0x1 << offset);
264 mutex_unlock(&gpio_lock);
268 static void twl_free(struct gpio_chip *chip, unsigned offset)
270 mutex_lock(&gpio_lock);
272 gpio_usage_count &= ~BIT(offset);
274 /* on last use, switch off GPIO module */
275 if (!gpio_usage_count)
276 gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
278 mutex_unlock(&gpio_lock);
281 static int twl_direction_in(struct gpio_chip *chip, unsigned offset)
283 return twl4030_set_gpio_direction(offset, 1);
286 static int twl_get(struct gpio_chip *chip, unsigned offset)
288 int status = twl4030_get_gpio_datain(offset);
290 return (status < 0) ? 0 : status;
293 static int twl_direction_out(struct gpio_chip *chip, unsigned offset, int value)
295 twl4030_set_gpio_dataout(offset, value);
296 return twl4030_set_gpio_direction(offset, 0);
299 static void twl_set(struct gpio_chip *chip, unsigned offset, int value)
301 twl4030_set_gpio_dataout(offset, value);
304 static int twl_to_irq(struct gpio_chip *chip, unsigned offset)
306 return twl4030_gpio_irq_base
307 ? (twl4030_gpio_irq_base + offset)
311 static struct gpio_chip twl_gpiochip = {
313 .owner = THIS_MODULE,
314 .request = twl_request,
316 .direction_input = twl_direction_in,
318 .direction_output = twl_direction_out,
320 .to_irq = twl_to_irq,
324 /*----------------------------------------------------------------------*/
326 static int __devinit gpio_twl4030_pulls(u32 ups, u32 downs)
329 unsigned i, gpio_bit;
331 /* For most pins, a pulldown was enabled by default.
332 * We should have data that's specific to this board.
334 for (gpio_bit = 1, i = 1; i < 6; i++) {
338 for (bit_mask = 0, j = 0; j < 8; j += 2, gpio_bit <<= 1) {
340 bit_mask |= 1 << (j + 1);
341 else if (downs & gpio_bit)
342 bit_mask |= 1 << (j + 0);
344 message[i] = bit_mask;
347 return twl4030_i2c_write(TWL4030_MODULE_GPIO, message,
348 REG_GPIOPUPDCTR1, 5);
351 static int gpio_twl4030_remove(struct platform_device *pdev);
353 static int __devinit gpio_twl4030_probe(struct platform_device *pdev)
355 struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
358 /* maybe setup IRQs */
359 if (pdata->irq_base) {
362 "can't dispatch IRQs from modules\n");
365 ret = twl4030_sih_setup(TWL4030_MODULE_GPIO);
368 WARN_ON(ret != pdata->irq_base);
369 twl4030_gpio_irq_base = ret;
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!
379 ret = gpio_twl4030_pulls(pdata->pullups, pdata->pulldowns);
381 dev_dbg(&pdev->dev, "pullups %.05x %.05x --> %d\n",
382 pdata->pullups, pdata->pulldowns,
385 twl_gpiochip.base = pdata->gpio_base;
386 twl_gpiochip.ngpio = TWL4030_GPIO_MAX;
387 twl_gpiochip.dev = &pdev->dev;
389 ret = gpiochip_add(&twl_gpiochip);
392 "could not register gpiochip, %d\n",
394 twl_gpiochip.ngpio = 0;
395 gpio_twl4030_remove(pdev);
396 } else if (pdata->setup) {
399 status = pdata->setup(&pdev->dev,
400 pdata->gpio_base, TWL4030_GPIO_MAX);
402 dev_dbg(&pdev->dev, "setup --> %d\n", status);
408 static int __devexit gpio_twl4030_remove(struct platform_device *pdev)
410 struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
413 if (pdata->teardown) {
414 status = pdata->teardown(&pdev->dev,
415 pdata->gpio_base, TWL4030_GPIO_MAX);
417 dev_dbg(&pdev->dev, "teardown --> %d\n", status);
422 status = gpiochip_remove(&twl_gpiochip);
429 /* REVISIT no support yet for deregistering all the IRQs */
434 /* Note: this hardware lives inside an I2C-based multi-function device. */
435 MODULE_ALIAS("platform:twl4030_gpio");
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),
444 static int __init gpio_twl4030_init(void)
446 return platform_driver_register(&gpio_twl4030_driver);
448 subsys_initcall(gpio_twl4030_init);
450 static void __exit gpio_twl4030_exit(void)
452 platform_driver_unregister(&gpio_twl4030_driver);
454 module_exit(gpio_twl4030_exit);
456 MODULE_AUTHOR("Texas Instruments, Inc.");
457 MODULE_DESCRIPTION("GPIO interface for TWL4030");
458 MODULE_LICENSE("GPL");