]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/gpio/gpio-twl4030.c
gpio: twl4030: Introduce private structure to store variables needed runtime
[mirror_ubuntu-jammy-kernel.git] / drivers / gpio / gpio-twl4030.c
CommitLineData
e9d35947 1/*
c103de24 2 * Access to GPIOs on TWL4030/TPS659x0 chips
e9d35947
DB
3 *
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>
b8589e2a
BC
35#include <linux/of.h>
36#include <linux/irqdomain.h>
e9d35947 37
b07682b6 38#include <linux/i2c/twl.h>
e9d35947
DB
39
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
e9d35947
DB
52/* 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
72c7901e
PU
70struct gpio_twl4030_priv {
71 struct gpio_chip gpio_chip;
72 int irq_base;
73
74 unsigned int usage_count;
75};
e9d35947
DB
76
77/*----------------------------------------------------------------------*/
78
72c7901e
PU
79static 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
e9d35947
DB
84/*
85 * To configure TWL4030 GPIO module registers
86 */
87static inline int gpio_twl4030_write(u8 address, u8 data)
88{
fc7b92fc 89 return twl_i2c_write_u8(TWL4030_MODULE_GPIO, data, address);
e9d35947
DB
90}
91
92/*----------------------------------------------------------------------*/
93
94/*
9859eb99 95 * LED register offsets from TWL_MODULE_LED base
e9d35947
DB
96 * PWMs A and B are dedicated to LEDs A and B, respectively.
97 */
98
9859eb99
PU
99#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
e9d35947
DB
104
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
e9d35947
DB
115#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
fc7b92fc 127 ret = twl_i2c_read_u8(TWL4030_MODULE_GPIO, &data, address);
e9d35947
DB
128 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;
fc7b92fc 152 status = twl_i2c_write_u8(TWL4030_MODULE_LED, cached_leden,
9859eb99 153 TWL4030_LED_LEDEN_REG);
e9d35947
DB
154 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
e9d35947
DB
200 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
e9d35947
DB
208/*----------------------------------------------------------------------*/
209
210static int twl_request(struct gpio_chip *chip, unsigned offset)
211{
72c7901e 212 struct gpio_twl4030_priv *priv = to_gpio_twl4030(chip);
e9d35947
DB
213 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;
9859eb99 221 u8 reg = TWL4030_PWMAON_REG;
e9d35947
DB
222
223 offset -= TWL4030_GPIO_MAX;
224 if (offset) {
225 ledclr_mask <<= 1;
9859eb99 226 reg = TWL4030_PWMBON_REG;
e9d35947
DB
227 }
228
229 /* initialize PWM to always-drive */
9859eb99
PU
230 /* Configure PWM OFF register first */
231 status = twl_i2c_write_u8(TWL4030_MODULE_LED, 0x7f, reg + 1);
e9d35947
DB
232 if (status < 0)
233 goto done;
9859eb99
PU
234
235 /* Followed by PWM ON register */
236 status = twl_i2c_write_u8(TWL4030_MODULE_LED, 0x7f, reg);
e9d35947
DB
237 if (status < 0)
238 goto done;
239
240 /* init LED to not-driven (high) */
9859eb99
PU
241 status = twl_i2c_read_u8(TWL4030_MODULE_LED, &cached_leden,
242 TWL4030_LED_LEDEN_REG);
e9d35947
DB
243 if (status < 0)
244 goto done;
245 cached_leden &= ~ledclr_mask;
9859eb99
PU
246 status = twl_i2c_write_u8(TWL4030_MODULE_LED, cached_leden,
247 TWL4030_LED_LEDEN_REG);
e9d35947
DB
248 if (status < 0)
249 goto done;
250
251 status = 0;
252 goto done;
253 }
254
255 /* on first use, turn GPIO module "on" */
72c7901e 256 if (!priv->usage_count) {
e9d35947
DB
257 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;
b8589e2a
BC
264 if (pdata)
265 value |= pdata->mmc_cd & 0x03;
e9d35947
DB
266
267 status = gpio_twl4030_write(REG_GPIO_CTRL, value);
268 }
269
72c7901e 270done:
e9d35947 271 if (!status)
72c7901e 272 priv->usage_count |= BIT(offset);
e9d35947 273
e9d35947
DB
274 mutex_unlock(&gpio_lock);
275 return status;
276}
277
278static void twl_free(struct gpio_chip *chip, unsigned offset)
279{
72c7901e
PU
280 struct gpio_twl4030_priv *priv = to_gpio_twl4030(chip);
281
e9d35947
DB
282 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
72c7901e 289 priv->usage_count &= ~BIT(offset);
e9d35947
DB
290
291 /* on last use, switch off GPIO module */
72c7901e 292 if (!priv->usage_count)
e9d35947
DB
293 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{
72c7901e 307 struct gpio_twl4030_priv *priv = to_gpio_twl4030(chip);
e9d35947
DB
308 int status = 0;
309
72c7901e
PU
310 if (!(priv->usage_count & BIT(offset)))
311 return -EPERM;
312
e9d35947
DB
313 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;
72c7901e 319
e9d35947
DB
320 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{
72c7901e
PU
344 struct gpio_twl4030_priv *priv = to_gpio_twl4030(chip);
345
346 return (priv->irq_base && (offset < TWL4030_GPIO_MAX))
347 ? (priv->irq_base + offset)
e9d35947
DB
348 : -EINVAL;
349}
350
72c7901e 351static struct gpio_chip template_chip = {
e9d35947
DB
352 .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
3836309d 366static int gpio_twl4030_pulls(u32 ups, u32 downs)
e9d35947 367{
14591d88 368 u8 message[5];
e9d35947
DB
369 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 */
14591d88 374 for (gpio_bit = 1, i = 0; i < 5; i++) {
e9d35947
DB
375 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
fc7b92fc 387 return twl_i2c_write(TWL4030_MODULE_GPIO, message,
e9d35947
DB
388 REG_GPIOPUPDCTR1, 5);
389}
390
3836309d 391static int gpio_twl4030_debounce(u32 debounce, u8 mmc_cd)
cabb3fc4 392{
14591d88 393 u8 message[3];
cabb3fc4
DB
394
395 /* 30 msec of debouncing is always used for MMC card detect,
396 * and is optional for everything else.
397 */
14591d88 398 message[0] = (debounce & 0xff) | (mmc_cd & 0x03);
cabb3fc4 399 debounce >>= 8;
14591d88 400 message[1] = (debounce & 0xff);
cabb3fc4 401 debounce >>= 8;
14591d88 402 message[2] = (debounce & 0x03);
cabb3fc4 403
fc7b92fc 404 return twl_i2c_write(TWL4030_MODULE_GPIO, message,
cabb3fc4
DB
405 REG_GPIO_DEBEN1, 3);
406}
407
e9d35947
DB
408static int gpio_twl4030_remove(struct platform_device *pdev);
409
f74ce8fb
FV
410static 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
f74ce8fb
FV
418 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
3836309d 433static int gpio_twl4030_probe(struct platform_device *pdev)
e9d35947
DB
434{
435 struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
b8589e2a 436 struct device_node *node = pdev->dev.of_node;
72c7901e 437 struct gpio_twl4030_priv *priv;
2d9dd99b 438 int ret, irq_base;
e9d35947 439
72c7901e
PU
440 priv = devm_kzalloc(&pdev->dev, sizeof(struct gpio_twl4030_priv),
441 GFP_KERNEL);
442 if (!priv)
443 return -ENOMEM;
444
e9d35947 445 /* maybe setup IRQs */
2d9dd99b
BC
446 if (is_module()) {
447 dev_err(&pdev->dev, "can't dispatch IRQs from modules\n");
448 goto no_irqs;
449 }
450
451 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;
e9d35947
DB
455 }
456
b8589e2a
BC
457 irq_domain_add_legacy(node, TWL4030_GPIO_MAX, irq_base, 0,
458 &irq_domain_simple_ops, NULL);
459
2d9dd99b
BC
460 ret = twl4030_sih_setup(&pdev->dev, TWL4030_MODULE_GPIO, irq_base);
461 if (ret < 0)
462 return ret;
463
72c7901e 464 priv->irq_base = irq_base;
2d9dd99b 465
e9d35947 466no_irqs:
72c7901e
PU
467 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;
e9d35947 471
f74ce8fb
FV
472 if (node)
473 pdata = of_gpio_twl4030(&pdev->dev);
b8589e2a 474
f74ce8fb
FV
475 if (pdata == NULL) {
476 dev_err(&pdev->dev, "Platform data is missing\n");
477 return -ENXIO;
b8589e2a 478 }
e9d35947 479
f74ce8fb
FV
480 /*
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)
72c7901e 501 priv->gpio_chip.ngpio += 2;
f74ce8fb 502
72c7901e 503 ret = gpiochip_add(&priv->gpio_chip);
e9d35947 504 if (ret < 0) {
2d9dd99b 505 dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
72c7901e 506 priv->gpio_chip.ngpio = 0;
e9d35947 507 gpio_twl4030_remove(pdev);
a940d9a1
TL
508 goto out;
509 }
510
72c7901e 511 platform_set_drvdata(pdev, priv);
a940d9a1
TL
512
513 if (pdata && pdata->setup) {
e9d35947
DB
514 int status;
515
72c7901e
PU
516 status = pdata->setup(&pdev->dev, priv->gpio_chip.base,
517 TWL4030_GPIO_MAX);
e9d35947
DB
518 if (status)
519 dev_dbg(&pdev->dev, "setup --> %d\n", status);
520 }
521
a940d9a1 522out:
e9d35947
DB
523 return ret;
524}
525
206210ce 526/* Cannot use as gpio_twl4030_probe() calls us */
46c529cf 527static int gpio_twl4030_remove(struct platform_device *pdev)
e9d35947
DB
528{
529 struct twl4030_gpio_platform_data *pdata = pdev->dev.platform_data;
72c7901e 530 struct gpio_twl4030_priv *priv = platform_get_drvdata(pdev);
e9d35947
DB
531 int status;
532
b8589e2a 533 if (pdata && pdata->teardown) {
72c7901e
PU
534 status = pdata->teardown(&pdev->dev, priv->gpio_chip.base,
535 TWL4030_GPIO_MAX);
e9d35947
DB
536 if (status) {
537 dev_dbg(&pdev->dev, "teardown --> %d\n", status);
538 return status;
539 }
540 }
541
72c7901e 542 status = gpiochip_remove(&priv->gpio_chip);
e9d35947
DB
543 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
b8589e2a
BC
554static const struct of_device_id twl_gpio_match[] = {
555 { .compatible = "ti,twl4030-gpio", },
556 { },
557};
558MODULE_DEVICE_TABLE(of, twl_gpio_match);
559
e9d35947
DB
560/* Note: this hardware lives inside an I2C-based multi-function device. */
561MODULE_ALIAS("platform:twl4030_gpio");
562
563static struct platform_driver gpio_twl4030_driver = {
b8589e2a
BC
564 .driver = {
565 .name = "twl4030_gpio",
566 .owner = THIS_MODULE,
567 .of_match_table = of_match_ptr(twl_gpio_match),
568 },
e9d35947 569 .probe = gpio_twl4030_probe,
46c529cf 570 .remove = gpio_twl4030_remove,
e9d35947
DB
571};
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");