2 * tps65910_gpio.c -- access to GPIOs on TPS65910x chips
4 * Copyright (C) 2010 Mistral solutions Pvt Ltd <www.mistralsolutions.com>
6 * Based on twl4030-gpio.c
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/kthread.h>
27 #include <linux/irq.h>
28 #include <linux/gpio.h>
29 #include <linux/platform_device.h>
30 #include <linux/slab.h>
32 #include <linux/i2c/tps65910.h>
34 static int gpio_tps65910_remove(struct platform_device *pdev);
37 * The GPIO "subchip" supports 1 GPIOs which can be configured as
38 * inputs or outputs, with pullups or pulldowns on each pin. Each
39 * GPIO can trigger interrupts on either or both edges.
44 static struct gpio_chip tps65910_gpiochip;
45 static DEFINE_MUTEX(gpio_lock);
46 static unsigned int gpio_usage_count;
47 static struct work_struct gpio_work;
48 static struct mutex work_lock;
50 * To configure TPS65910 GPIO registers
52 static inline int gpio_tps65910_write(u8 address, u8 data)
54 return tps65910_i2c_write_u8(TPS65910_I2C_ID0, data, address);
59 * To read a TPS65910 GPIO module register
61 static inline int gpio_tps65910_read(u8 address)
66 ret = tps65910_i2c_read_u8(TPS65910_I2C_ID0, &data, address);
67 return (ret < 0) ? ret : data;
70 static int tps65910_request(struct gpio_chip *chip, unsigned offset)
74 mutex_lock(&gpio_lock);
76 /* initialize TPS65910 GPIO */
77 /* By default the GPIO_CKSYNC signal is GPIO */
78 if (!gpio_usage_count)
81 mutex_unlock(&gpio_lock);
85 static void tps65910_free(struct gpio_chip *chip, unsigned offset)
87 mutex_lock(&gpio_lock);
89 /* on last use, switch off GPIO module */
90 if (!gpio_usage_count)
93 mutex_unlock(&gpio_lock);
96 static int tps65910_direction_in(struct gpio_chip *chip, unsigned offset)
98 /* Configure TPS65910 GPIO as input */
101 mutex_lock(&gpio_lock);
103 val = gpio_tps65910_read(TPS65910_REG_GPIO0);
105 val &= ~(TPS65910_GPIO_CFG_OUTPUT);
107 val = gpio_tps65910_read(TPS65910_REG_GPIO0);
109 mutex_unlock(&gpio_lock);
114 static int tps65910_get(struct gpio_chip *chip, unsigned offset)
118 mutex_lock(&gpio_lock);
120 status = gpio_tps65910_read(TPS65910_REG_GPIO0);
122 mutex_unlock(&gpio_lock);
129 int tps65910_direction_out(struct gpio_chip *chip, unsigned offset, int value)
131 /* Configure TPS65910 GPIO as input */
134 mutex_lock(&gpio_lock);
135 val = gpio_tps65910_read(TPS65910_REG_GPIO0);
137 val |= TPS65910_GPIO_CFG_OUTPUT;
139 ret = gpio_tps65910_write(TPS65910_REG_GPIO0, val);
140 mutex_unlock(&gpio_lock);
147 static void tps65910_set(struct gpio_chip *chip, unsigned offset, int value)
152 mutex_lock(&gpio_lock);
153 val = gpio_tps65910_read(TPS65910_REG_GPIO0);
160 ret = gpio_tps65910_write(TPS65910_REG_GPIO0, val);
162 mutex_unlock(&gpio_lock);
167 static void tps65910_gpio_set_debounce(u8 debounce)
171 mutex_lock(&gpio_lock);
173 val = gpio_tps65910_read(TPS65910_REG_GPIO0);
175 if (debounce == TPS65910_DEBOUNCE_91_5_MS)
177 else if (debounce == TPS65910_DEBOUNCE_150_MS)
180 printk(KERN_ERR "Invalid argument to %s\n", __func__);
182 gpio_tps65910_write(TPS65910_REG_GPIO0, val);
184 mutex_unlock(&gpio_lock);
186 EXPORT_SYMBOL(tps65910_gpio_set_debounce);
189 static void tps65910_gpio_pullup_enable(void)
194 mutex_lock(&gpio_lock);
196 val = gpio_tps65910_read(TPS65910_REG_GPIO0);
200 ret = gpio_tps65910_write(TPS65910_REG_GPIO0, val);
202 mutex_unlock(&gpio_lock);
205 printk(KERN_ERR "Error writing to TPS65910_REG_GPIO0 in %s \n",
208 EXPORT_SYMBOL(tps65910_gpio_pullup_enable);
210 static void tps65910_gpio_pullup_disable(void)
215 mutex_lock(&gpio_lock);
217 val = gpio_tps65910_read(TPS65910_REG_GPIO0);
221 ret = gpio_tps65910_write(TPS65910_REG_GPIO0, val);
223 mutex_unlock(&gpio_lock);
225 EXPORT_SYMBOL(tps65910_gpio_pullup_disable);
227 static void tps65910_gpio_work(struct work_struct *work)
230 /* Read the status register and take action */
233 mutex_lock(&work_lock);
234 err = tps65910_i2c_read_u8(TPS65910_I2C_ID0, &status2,
235 TPS65910_REG_INT_STS);
238 case TPS65910_GPIO_F_IT:
239 printk(KERN_NOTICE "Received TPS65910 GPIO falling \
241 /* Clear interrupt */
242 tps65910_i2c_write_u8(TPS65910_I2C_ID0, status2,
243 TPS65910_REG_INT_STS);
244 /* Add code accroding to board requirment */
246 case TPS65910_GPIO_R_IT:
247 printk(KERN_NOTICE "Received TPS65910 GPIO Raising \
249 /* Clear interrupt */
250 tps65910_i2c_write_u8(TPS65910_I2C_ID0, status2,
251 TPS65910_REG_INT_STS);
252 /* Add code accroding to board requirment */
256 printk(KERN_ERR"Could not read TPS65910_REG_INT_STS\n");
259 mutex_unlock(&work_lock);
265 static irqreturn_t tps65910_gpio_isr(int irq, void *_tps65910)
267 /* Disable IRQ, schedule work, enable IRQ and acknowledge */
269 (void) schedule_work(&gpio_work);
275 static struct gpio_chip tps65910_gpiochip = {
277 .owner = THIS_MODULE,
278 .request = tps65910_request,
279 .free = tps65910_free,
280 .direction_input = tps65910_direction_in,
282 .direction_output = tps65910_direction_out,
287 static int __devinit gpio_tps65910_probe(struct platform_device *pdev)
292 struct tps65910_gpio *pdata = pdev->dev.platform_data;
294 if (pdata->gpio_mode == TPS65910_GPIO_AS_IRQ) {
296 if (pdata->irq_num) {
297 status = request_irq(pdata->irq_num, tps65910_gpio_isr,
298 IRQF_SHARED, "tps65910_gpio", pdev);
300 pr_err("tps65910: could not claim irq%d: %d\n",
301 pdata->irq_num, status);
306 INIT_WORK(&gpio_work, tps65910_gpio_work);
307 mutex_init(&work_lock);
309 tps65910_gpiochip.ngpio = TPS65910_GPIO_MAX;
310 tps65910_gpiochip.dev = &pdev->dev;
312 ret = gpiochip_add(&tps65910_gpiochip);
315 dev_err(&pdev->dev, "could not register gpiochip \
317 tps65910_gpiochip.ngpio = 0;
318 gpio_tps65910_remove(pdev);
321 if (pdata->gpio_setup)
322 pdata->gpio_setup(pdata);
327 static int gpio_tps65910_remove(struct platform_device *pdev)
329 struct tps65910_gpio *pdata = pdev->dev.platform_data;
332 if (pdata->gpio_taredown)
333 pdata->gpio_taredown(pdata);
334 if (pdata->gpio_mode == TPS65910_GPIO_AS_IRQ)
335 free_irq(pdata->irq_num, NULL);
337 status = gpiochip_remove(&tps65910_gpiochip);
343 static struct platform_driver gpio_tps65910_driver = {
344 .driver.name = "tps65910_gpio",
345 .driver.owner = THIS_MODULE,
346 .probe = gpio_tps65910_probe,
347 .remove = gpio_tps65910_remove,
350 static int __init gpio_tps65910_init(void)
352 return platform_driver_register(&gpio_tps65910_driver);
354 subsys_initcall(gpio_tps65910_init);
356 static void __exit gpio_tps65910_exit(void)
358 platform_driver_unregister(&gpio_tps65910_driver);
360 module_exit(gpio_tps65910_exit);
362 MODULE_AUTHOR("Mistral Solutions Pvt Ltd.");
363 MODULE_DESCRIPTION("GPIO interface for TPS65910");
364 MODULE_LICENSE("GPL");