blob: 4643f9cd0cae904196e686ba9a640e0dcedcffe6 [file] [log] [blame]
David Brownelle9d359472008-10-20 23:51:46 +02001/*
Grant Likelyc103de22011-06-04 18:38:28 -06002 * Access to GPIOs on TWL4030/TPS659x0 chips
David Brownelle9d359472008-10-20 23:51:46 +02003 *
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>
Benoit Coussonb8589e22012-02-29 22:51:32 +010035#include <linux/of.h>
36#include <linux/irqdomain.h>
David Brownelle9d359472008-10-20 23:51:46 +020037
Santosh Shilimkarb07682b2009-12-13 20:05:51 +010038#include <linux/i2c/twl.h>
David Brownelle9d359472008-10-20 23:51:46 +020039
40
41/*
42 * The GPIO "subchip" supports 18 GPIOs which can be configured as
43 * inputs or outputs, with pullups or pulldowns on each pin. Each
44 * GPIO can trigger interrupts on either or both edges.
45 *
46 * GPIO interrupts can be fed to either of two IRQ lines; this is
47 * intended to support multiple hosts.
48 *
49 * There are also two LED pins used sometimes as output-only GPIOs.
50 */
51
David Brownelle9d359472008-10-20 23:51:46 +020052/* genirq interfaces are not available to modules */
53#ifdef MODULE
54#define is_module() true
55#else
56#define is_module() false
57#endif
58
59/* GPIO_CTRL Fields */
60#define MASK_GPIO_CTRL_GPIO0CD1 BIT(0)
61#define MASK_GPIO_CTRL_GPIO1CD2 BIT(1)
62#define MASK_GPIO_CTRL_GPIO_ON BIT(2)
63
64/* Mask for GPIO registers when aggregated into a 32-bit integer */
65#define GPIO_32_MASK 0x0003ffff
66
67/* Data structures */
68static DEFINE_MUTEX(gpio_lock);
69
Peter Ujfalusi72c79012012-12-06 10:52:05 +000070struct gpio_twl4030_priv {
71 struct gpio_chip gpio_chip;
72 int irq_base;
73
74 unsigned int usage_count;
75};
David Brownelle9d359472008-10-20 23:51:46 +020076
77/*----------------------------------------------------------------------*/
78
Peter Ujfalusi72c79012012-12-06 10:52:05 +000079static inline struct gpio_twl4030_priv *to_gpio_twl4030(struct gpio_chip *chip)
80{
81 return container_of(chip, struct gpio_twl4030_priv, gpio_chip);
82}
83
David Brownelle9d359472008-10-20 23:51:46 +020084/*
85 * To configure TWL4030 GPIO module registers
86 */
87static inline int gpio_twl4030_write(u8 address, u8 data)
88{
Balaji T Kfc7b92f2009-12-13 21:23:33 +010089 return twl_i2c_write_u8(TWL4030_MODULE_GPIO, data, address);
David Brownelle9d359472008-10-20 23:51:46 +020090}
91
92/*----------------------------------------------------------------------*/
93
94/*
Peter Ujfalusi9859eb92012-11-13 10:35:13 +010095 * LED register offsets from TWL_MODULE_LED base
David Brownelle9d359472008-10-20 23:51:46 +020096 * PWMs A and B are dedicated to LEDs A and B, respectively.
97 */
98
Peter Ujfalusi9859eb92012-11-13 10:35:13 +010099#define TWL4030_LED_LEDEN_REG 0x00
100#define TWL4030_PWMAON_REG 0x01
101#define TWL4030_PWMAOFF_REG 0x02
102#define TWL4030_PWMBON_REG 0x03
103#define TWL4030_PWMBOFF_REG 0x04
David Brownelle9d359472008-10-20 23:51:46 +0200104
105/* LEDEN bits */
106#define LEDEN_LEDAON BIT(0)
107#define LEDEN_LEDBON BIT(1)
108#define LEDEN_LEDAEXT BIT(2)
109#define LEDEN_LEDBEXT BIT(3)
110#define LEDEN_LEDAPWM BIT(4)
111#define LEDEN_LEDBPWM BIT(5)
112#define LEDEN_PWM_LENGTHA BIT(6)
113#define LEDEN_PWM_LENGTHB BIT(7)
114
David Brownelle9d359472008-10-20 23:51:46 +0200115#define PWMxON_LENGTH BIT(7)
116
117/*----------------------------------------------------------------------*/
118
119/*
120 * To read a TWL4030 GPIO module register
121 */
122static inline int gpio_twl4030_read(u8 address)
123{
124 u8 data;
125 int ret = 0;
126
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100127 ret = twl_i2c_read_u8(TWL4030_MODULE_GPIO, &data, address);
David Brownelle9d359472008-10-20 23:51:46 +0200128 return (ret < 0) ? ret : data;
129}
130
131/*----------------------------------------------------------------------*/
132
133static u8 cached_leden; /* protected by gpio_lock */
134
135/* The LED lines are open drain outputs ... a FET pulls to GND, so an
136 * external pullup is needed. We could also expose the integrated PWM
137 * as a LED brightness control; we initialize it as "always on".
138 */
139static void twl4030_led_set_value(int led, int value)
140{
141 u8 mask = LEDEN_LEDAON | LEDEN_LEDAPWM;
142 int status;
143
144 if (led)
145 mask <<= 1;
146
147 mutex_lock(&gpio_lock);
148 if (value)
149 cached_leden &= ~mask;
150 else
151 cached_leden |= mask;
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100152 status = twl_i2c_write_u8(TWL4030_MODULE_LED, cached_leden,
Peter Ujfalusi9859eb92012-11-13 10:35:13 +0100153 TWL4030_LED_LEDEN_REG);
David Brownelle9d359472008-10-20 23:51:46 +0200154 mutex_unlock(&gpio_lock);
155}
156
157static int twl4030_set_gpio_direction(int gpio, int is_input)
158{
159 u8 d_bnk = gpio >> 3;
160 u8 d_msk = BIT(gpio & 0x7);
161 u8 reg = 0;
162 u8 base = REG_GPIODATADIR1 + d_bnk;
163 int ret = 0;
164
165 mutex_lock(&gpio_lock);
166 ret = gpio_twl4030_read(base);
167 if (ret >= 0) {
168 if (is_input)
169 reg = ret & ~d_msk;
170 else
171 reg = ret | d_msk;
172
173 ret = gpio_twl4030_write(base, reg);
174 }
175 mutex_unlock(&gpio_lock);
176 return ret;
177}
178
179static int twl4030_set_gpio_dataout(int gpio, int enable)
180{
181 u8 d_bnk = gpio >> 3;
182 u8 d_msk = BIT(gpio & 0x7);
183 u8 base = 0;
184
185 if (enable)
186 base = REG_SETGPIODATAOUT1 + d_bnk;
187 else
188 base = REG_CLEARGPIODATAOUT1 + d_bnk;
189
190 return gpio_twl4030_write(base, d_msk);
191}
192
193static int twl4030_get_gpio_datain(int gpio)
194{
195 u8 d_bnk = gpio >> 3;
196 u8 d_off = gpio & 0x7;
197 u8 base = 0;
198 int ret = 0;
199
David Brownelle9d359472008-10-20 23:51:46 +0200200 base = REG_GPIODATAIN1 + d_bnk;
201 ret = gpio_twl4030_read(base);
202 if (ret > 0)
203 ret = (ret >> d_off) & 0x1;
204
205 return ret;
206}
207
David Brownelle9d359472008-10-20 23:51:46 +0200208/*----------------------------------------------------------------------*/
209
210static int twl_request(struct gpio_chip *chip, unsigned offset)
211{
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000212 struct gpio_twl4030_priv *priv = to_gpio_twl4030(chip);
David Brownelle9d359472008-10-20 23:51:46 +0200213 int status = 0;
214
215 mutex_lock(&gpio_lock);
216
217 /* Support the two LED outputs as output-only GPIOs. */
218 if (offset >= TWL4030_GPIO_MAX) {
219 u8 ledclr_mask = LEDEN_LEDAON | LEDEN_LEDAEXT
220 | LEDEN_LEDAPWM | LEDEN_PWM_LENGTHA;
Peter Ujfalusi9859eb92012-11-13 10:35:13 +0100221 u8 reg = TWL4030_PWMAON_REG;
David Brownelle9d359472008-10-20 23:51:46 +0200222
223 offset -= TWL4030_GPIO_MAX;
224 if (offset) {
225 ledclr_mask <<= 1;
Peter Ujfalusi9859eb92012-11-13 10:35:13 +0100226 reg = TWL4030_PWMBON_REG;
David Brownelle9d359472008-10-20 23:51:46 +0200227 }
228
229 /* initialize PWM to always-drive */
Peter Ujfalusi9859eb92012-11-13 10:35:13 +0100230 /* Configure PWM OFF register first */
231 status = twl_i2c_write_u8(TWL4030_MODULE_LED, 0x7f, reg + 1);
David Brownelle9d359472008-10-20 23:51:46 +0200232 if (status < 0)
233 goto done;
Peter Ujfalusi9859eb92012-11-13 10:35:13 +0100234
235 /* Followed by PWM ON register */
236 status = twl_i2c_write_u8(TWL4030_MODULE_LED, 0x7f, reg);
David Brownelle9d359472008-10-20 23:51:46 +0200237 if (status < 0)
238 goto done;
239
240 /* init LED to not-driven (high) */
Peter Ujfalusi9859eb92012-11-13 10:35:13 +0100241 status = twl_i2c_read_u8(TWL4030_MODULE_LED, &cached_leden,
242 TWL4030_LED_LEDEN_REG);
David Brownelle9d359472008-10-20 23:51:46 +0200243 if (status < 0)
244 goto done;
245 cached_leden &= ~ledclr_mask;
Peter Ujfalusi9859eb92012-11-13 10:35:13 +0100246 status = twl_i2c_write_u8(TWL4030_MODULE_LED, cached_leden,
247 TWL4030_LED_LEDEN_REG);
David Brownelle9d359472008-10-20 23:51:46 +0200248 if (status < 0)
249 goto done;
250
251 status = 0;
252 goto done;
253 }
254
255 /* on first use, turn GPIO module "on" */
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000256 if (!priv->usage_count) {
David Brownelle9d359472008-10-20 23:51:46 +0200257 struct twl4030_gpio_platform_data *pdata;
258 u8 value = MASK_GPIO_CTRL_GPIO_ON;
259
260 /* optionally have the first two GPIOs switch vMMC1
261 * and vMMC2 power supplies based on card presence.
262 */
263 pdata = chip->dev->platform_data;
Benoit Coussonb8589e22012-02-29 22:51:32 +0100264 if (pdata)
265 value |= pdata->mmc_cd & 0x03;
David Brownelle9d359472008-10-20 23:51:46 +0200266
267 status = gpio_twl4030_write(REG_GPIO_CTRL, value);
268 }
269
David Brownelle9d359472008-10-20 23:51:46 +0200270done:
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000271 if (!status)
272 priv->usage_count |= BIT(offset);
273
David Brownelle9d359472008-10-20 23:51:46 +0200274 mutex_unlock(&gpio_lock);
275 return status;
276}
277
278static void twl_free(struct gpio_chip *chip, unsigned offset)
279{
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000280 struct gpio_twl4030_priv *priv = to_gpio_twl4030(chip);
281
David Brownelle9d359472008-10-20 23:51:46 +0200282 if (offset >= TWL4030_GPIO_MAX) {
283 twl4030_led_set_value(offset - TWL4030_GPIO_MAX, 1);
284 return;
285 }
286
287 mutex_lock(&gpio_lock);
288
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000289 priv->usage_count &= ~BIT(offset);
David Brownelle9d359472008-10-20 23:51:46 +0200290
291 /* on last use, switch off GPIO module */
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000292 if (!priv->usage_count)
David Brownelle9d359472008-10-20 23:51:46 +0200293 gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
294
295 mutex_unlock(&gpio_lock);
296}
297
298static int twl_direction_in(struct gpio_chip *chip, unsigned offset)
299{
300 return (offset < TWL4030_GPIO_MAX)
301 ? twl4030_set_gpio_direction(offset, 1)
302 : -EINVAL;
303}
304
305static int twl_get(struct gpio_chip *chip, unsigned offset)
306{
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000307 struct gpio_twl4030_priv *priv = to_gpio_twl4030(chip);
David Brownelle9d359472008-10-20 23:51:46 +0200308 int status = 0;
309
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000310 if (!(priv->usage_count & BIT(offset)))
311 return -EPERM;
312
David Brownelle9d359472008-10-20 23:51:46 +0200313 if (offset < TWL4030_GPIO_MAX)
314 status = twl4030_get_gpio_datain(offset);
315 else if (offset == TWL4030_GPIO_MAX)
316 status = cached_leden & LEDEN_LEDAON;
317 else
318 status = cached_leden & LEDEN_LEDBON;
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000319
David Brownelle9d359472008-10-20 23:51:46 +0200320 return (status < 0) ? 0 : status;
321}
322
323static int twl_direction_out(struct gpio_chip *chip, unsigned offset, int value)
324{
325 if (offset < TWL4030_GPIO_MAX) {
326 twl4030_set_gpio_dataout(offset, value);
327 return twl4030_set_gpio_direction(offset, 0);
328 } else {
329 twl4030_led_set_value(offset - TWL4030_GPIO_MAX, value);
330 return 0;
331 }
332}
333
334static void twl_set(struct gpio_chip *chip, unsigned offset, int value)
335{
336 if (offset < TWL4030_GPIO_MAX)
337 twl4030_set_gpio_dataout(offset, value);
338 else
339 twl4030_led_set_value(offset - TWL4030_GPIO_MAX, value);
340}
341
342static int twl_to_irq(struct gpio_chip *chip, unsigned offset)
343{
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000344 struct gpio_twl4030_priv *priv = to_gpio_twl4030(chip);
345
346 return (priv->irq_base && (offset < TWL4030_GPIO_MAX))
347 ? (priv->irq_base + offset)
David Brownelle9d359472008-10-20 23:51:46 +0200348 : -EINVAL;
349}
350
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000351static struct gpio_chip template_chip = {
David Brownelle9d359472008-10-20 23:51:46 +0200352 .label = "twl4030",
353 .owner = THIS_MODULE,
354 .request = twl_request,
355 .free = twl_free,
356 .direction_input = twl_direction_in,
357 .get = twl_get,
358 .direction_output = twl_direction_out,
359 .set = twl_set,
360 .to_irq = twl_to_irq,
361 .can_sleep = 1,
362};
363
364/*----------------------------------------------------------------------*/
365
Bill Pemberton38363092012-11-19 13:22:34 -0500366static int gpio_twl4030_pulls(u32 ups, u32 downs)
David Brownelle9d359472008-10-20 23:51:46 +0200367{
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100368 u8 message[5];
David Brownelle9d359472008-10-20 23:51:46 +0200369 unsigned i, gpio_bit;
370
371 /* For most pins, a pulldown was enabled by default.
372 * We should have data that's specific to this board.
373 */
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100374 for (gpio_bit = 1, i = 0; i < 5; i++) {
David Brownelle9d359472008-10-20 23:51:46 +0200375 u8 bit_mask;
376 unsigned j;
377
378 for (bit_mask = 0, j = 0; j < 8; j += 2, gpio_bit <<= 1) {
379 if (ups & gpio_bit)
380 bit_mask |= 1 << (j + 1);
381 else if (downs & gpio_bit)
382 bit_mask |= 1 << (j + 0);
383 }
384 message[i] = bit_mask;
385 }
386
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100387 return twl_i2c_write(TWL4030_MODULE_GPIO, message,
David Brownelle9d359472008-10-20 23:51:46 +0200388 REG_GPIOPUPDCTR1, 5);
389}
390
Bill Pemberton38363092012-11-19 13:22:34 -0500391static int gpio_twl4030_debounce(u32 debounce, u8 mmc_cd)
David Brownellcabb3fc2009-01-06 14:42:26 -0800392{
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100393 u8 message[3];
David Brownellcabb3fc2009-01-06 14:42:26 -0800394
395 /* 30 msec of debouncing is always used for MMC card detect,
396 * and is optional for everything else.
397 */
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100398 message[0] = (debounce & 0xff) | (mmc_cd & 0x03);
David Brownellcabb3fc2009-01-06 14:42:26 -0800399 debounce >>= 8;
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100400 message[1] = (debounce & 0xff);
David Brownellcabb3fc2009-01-06 14:42:26 -0800401 debounce >>= 8;
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100402 message[2] = (debounce & 0x03);
David Brownellcabb3fc2009-01-06 14:42:26 -0800403
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100404 return twl_i2c_write(TWL4030_MODULE_GPIO, message,
David Brownellcabb3fc2009-01-06 14:42:26 -0800405 REG_GPIO_DEBEN1, 3);
406}
407
David Brownelle9d359472008-10-20 23:51:46 +0200408static int gpio_twl4030_remove(struct platform_device *pdev);
409
Florian Vaussardf74ce8f2012-09-05 09:46:25 +0200410static struct twl4030_gpio_platform_data *of_gpio_twl4030(struct device *dev)
411{
412 struct twl4030_gpio_platform_data *omap_twl_info;
413
414 omap_twl_info = devm_kzalloc(dev, sizeof(*omap_twl_info), GFP_KERNEL);
415 if (!omap_twl_info)
416 return NULL;
417
Florian Vaussardf74ce8f2012-09-05 09:46:25 +0200418 omap_twl_info->use_leds = of_property_read_bool(dev->of_node,
419 "ti,use-leds");
420
421 of_property_read_u32(dev->of_node, "ti,debounce",
422 &omap_twl_info->debounce);
423 of_property_read_u32(dev->of_node, "ti,mmc-cd",
424 (u32 *)&omap_twl_info->mmc_cd);
425 of_property_read_u32(dev->of_node, "ti,pullups",
426 &omap_twl_info->pullups);
427 of_property_read_u32(dev->of_node, "ti,pulldowns",
428 &omap_twl_info->pulldowns);
429
430 return omap_twl_info;
431}
432
Bill Pemberton38363092012-11-19 13:22:34 -0500433static int gpio_twl4030_probe(struct platform_device *pdev)
David Brownelle9d359472008-10-20 23:51:46 +0200434{
435 struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
Benoit Coussonb8589e22012-02-29 22:51:32 +0100436 struct device_node *node = pdev->dev.of_node;
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000437 struct gpio_twl4030_priv *priv;
Benoit Cousson2d9dd992012-02-29 22:48:32 +0100438 int ret, irq_base;
David Brownelle9d359472008-10-20 23:51:46 +0200439
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000440 priv = devm_kzalloc(&pdev->dev, sizeof(struct gpio_twl4030_priv),
441 GFP_KERNEL);
442 if (!priv)
443 return -ENOMEM;
444
David Brownelle9d359472008-10-20 23:51:46 +0200445 /* maybe setup IRQs */
Benoit Cousson2d9dd992012-02-29 22:48:32 +0100446 if (is_module()) {
447 dev_err(&pdev->dev, "can't dispatch IRQs from modules\n");
448 goto no_irqs;
David Brownelle9d359472008-10-20 23:51:46 +0200449 }
450
Benoit Cousson2d9dd992012-02-29 22:48:32 +0100451 irq_base = irq_alloc_descs(-1, 0, TWL4030_GPIO_MAX, 0);
452 if (irq_base < 0) {
453 dev_err(&pdev->dev, "Failed to alloc irq_descs\n");
454 return irq_base;
455 }
456
Benoit Coussonb8589e22012-02-29 22:51:32 +0100457 irq_domain_add_legacy(node, TWL4030_GPIO_MAX, irq_base, 0,
458 &irq_domain_simple_ops, NULL);
459
Benoit Cousson2d9dd992012-02-29 22:48:32 +0100460 ret = twl4030_sih_setup(&pdev->dev, TWL4030_MODULE_GPIO, irq_base);
461 if (ret < 0)
462 return ret;
463
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000464 priv->irq_base = irq_base;
Benoit Cousson2d9dd992012-02-29 22:48:32 +0100465
David Brownelle9d359472008-10-20 23:51:46 +0200466no_irqs:
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000467 priv->gpio_chip = template_chip;
468 priv->gpio_chip.base = -1;
469 priv->gpio_chip.ngpio = TWL4030_GPIO_MAX;
470 priv->gpio_chip.dev = &pdev->dev;
David Brownelle9d359472008-10-20 23:51:46 +0200471
Florian Vaussardf74ce8f2012-09-05 09:46:25 +0200472 if (node)
473 pdata = of_gpio_twl4030(&pdev->dev);
Benoit Coussonb8589e22012-02-29 22:51:32 +0100474
Florian Vaussardf74ce8f2012-09-05 09:46:25 +0200475 if (pdata == NULL) {
476 dev_err(&pdev->dev, "Platform data is missing\n");
477 return -ENXIO;
Benoit Coussonb8589e22012-02-29 22:51:32 +0100478 }
David Brownelle9d359472008-10-20 23:51:46 +0200479
Florian Vaussardf74ce8f2012-09-05 09:46:25 +0200480 /*
481 * NOTE: boards may waste power if they don't set pullups
482 * and pulldowns correctly ... default for non-ULPI pins is
483 * pulldown, and some other pins may have external pullups
484 * or pulldowns. Careful!
485 */
486 ret = gpio_twl4030_pulls(pdata->pullups, pdata->pulldowns);
487 if (ret)
488 dev_dbg(&pdev->dev, "pullups %.05x %.05x --> %d\n",
489 pdata->pullups, pdata->pulldowns, ret);
490
491 ret = gpio_twl4030_debounce(pdata->debounce, pdata->mmc_cd);
492 if (ret)
493 dev_dbg(&pdev->dev, "debounce %.03x %.01x --> %d\n",
494 pdata->debounce, pdata->mmc_cd, ret);
495
496 /*
497 * NOTE: we assume VIBRA_CTL.VIBRA_EN, in MODULE_AUDIO_VOICE,
498 * is (still) clear if use_leds is set.
499 */
500 if (pdata->use_leds)
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000501 priv->gpio_chip.ngpio += 2;
Florian Vaussardf74ce8f2012-09-05 09:46:25 +0200502
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000503 ret = gpiochip_add(&priv->gpio_chip);
David Brownelle9d359472008-10-20 23:51:46 +0200504 if (ret < 0) {
Benoit Cousson2d9dd992012-02-29 22:48:32 +0100505 dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000506 priv->gpio_chip.ngpio = 0;
David Brownelle9d359472008-10-20 23:51:46 +0200507 gpio_twl4030_remove(pdev);
Tony Lindgrena940d9a2012-09-04 17:43:30 -0700508 goto out;
509 }
510
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000511 platform_set_drvdata(pdev, priv);
Tony Lindgrena940d9a2012-09-04 17:43:30 -0700512
513 if (pdata && pdata->setup) {
David Brownelle9d359472008-10-20 23:51:46 +0200514 int status;
515
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000516 status = pdata->setup(&pdev->dev, priv->gpio_chip.base,
517 TWL4030_GPIO_MAX);
David Brownelle9d359472008-10-20 23:51:46 +0200518 if (status)
519 dev_dbg(&pdev->dev, "setup --> %d\n", status);
520 }
521
Tony Lindgrena940d9a2012-09-04 17:43:30 -0700522out:
David Brownelle9d359472008-10-20 23:51:46 +0200523 return ret;
524}
525
Bill Pemberton206210c2012-11-19 13:25:50 -0500526/* Cannot use as gpio_twl4030_probe() calls us */
Mike Frysinger46c529c2009-10-26 16:50:06 -0700527static int gpio_twl4030_remove(struct platform_device *pdev)
David Brownelle9d359472008-10-20 23:51:46 +0200528{
529 struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000530 struct gpio_twl4030_priv *priv = platform_get_drvdata(pdev);
David Brownelle9d359472008-10-20 23:51:46 +0200531 int status;
532
Benoit Coussonb8589e22012-02-29 22:51:32 +0100533 if (pdata && pdata->teardown) {
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000534 status = pdata->teardown(&pdev->dev, priv->gpio_chip.base,
535 TWL4030_GPIO_MAX);
David Brownelle9d359472008-10-20 23:51:46 +0200536 if (status) {
537 dev_dbg(&pdev->dev, "teardown --> %d\n", status);
538 return status;
539 }
540 }
541
Peter Ujfalusi72c79012012-12-06 10:52:05 +0000542 status = gpiochip_remove(&priv->gpio_chip);
David Brownelle9d359472008-10-20 23:51:46 +0200543 if (status < 0)
544 return status;
545
546 if (is_module())
547 return 0;
548
549 /* REVISIT no support yet for deregistering all the IRQs */
550 WARN_ON(1);
551 return -EIO;
552}
553
Benoit Coussonb8589e22012-02-29 22:51:32 +0100554static const struct of_device_id twl_gpio_match[] = {
555 { .compatible = "ti,twl4030-gpio", },
556 { },
557};
558MODULE_DEVICE_TABLE(of, twl_gpio_match);
559
David Brownelle9d359472008-10-20 23:51:46 +0200560/* Note: this hardware lives inside an I2C-based multi-function device. */
561MODULE_ALIAS("platform:twl4030_gpio");
562
563static struct platform_driver gpio_twl4030_driver = {
Benoit Coussonb8589e22012-02-29 22:51:32 +0100564 .driver = {
565 .name = "twl4030_gpio",
566 .owner = THIS_MODULE,
567 .of_match_table = of_match_ptr(twl_gpio_match),
568 },
David Brownelle9d359472008-10-20 23:51:46 +0200569 .probe = gpio_twl4030_probe,
Mike Frysinger46c529c2009-10-26 16:50:06 -0700570 .remove = gpio_twl4030_remove,
David Brownelle9d359472008-10-20 23:51:46 +0200571};
572
573static int __init gpio_twl4030_init(void)
574{
575 return platform_driver_register(&gpio_twl4030_driver);
576}
577subsys_initcall(gpio_twl4030_init);
578
579static void __exit gpio_twl4030_exit(void)
580{
581 platform_driver_unregister(&gpio_twl4030_driver);
582}
583module_exit(gpio_twl4030_exit);
584
585MODULE_AUTHOR("Texas Instruments, Inc.");
586MODULE_DESCRIPTION("GPIO interface for TWL4030");
587MODULE_LICENSE("GPL");