xref: /linux/drivers/gpio/gpio-twl4030.c (revision e291c116f60f3c1ca98090f0f8e7c77e658562fb)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Access to GPIOs on TWL4030/TPS659x0 chips
4  *
5  * Copyright (C) 2006-2007 Texas Instruments, Inc.
6  * Copyright (C) 2006 MontaVista Software, Inc.
7  *
8  * Code re-arranged and cleaned up by:
9  *	Syed Mohammed Khasim <x0khasim@ti.com>
10  *
11  * Initial Code:
12  *	Andy Lowe / Nishanth Menon
13  */
14 
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/kthread.h>
19 #include <linux/irq.h>
20 #include <linux/gpio/driver.h>
21 #include <linux/platform_device.h>
22 #include <linux/of.h>
23 #include <linux/irqdomain.h>
24 
25 #include <linux/mfd/twl.h>
26 
27 /*
28  * The GPIO "subchip" supports 18 GPIOs which can be configured as
29  * inputs or outputs, with pullups or pulldowns on each pin.  Each
30  * GPIO can trigger interrupts on either or both edges.
31  *
32  * GPIO interrupts can be fed to either of two IRQ lines; this is
33  * intended to support multiple hosts.
34  *
35  * There are also two LED pins used sometimes as output-only GPIOs.
36  */
37 
38 /* genirq interfaces are not available to modules */
39 #ifdef MODULE
40 #define is_module()	true
41 #else
42 #define is_module()	false
43 #endif
44 
45 /* GPIO_CTRL Fields */
46 #define MASK_GPIO_CTRL_GPIO0CD1		BIT(0)
47 #define MASK_GPIO_CTRL_GPIO1CD2		BIT(1)
48 #define MASK_GPIO_CTRL_GPIO_ON		BIT(2)
49 
50 /* Mask for GPIO registers when aggregated into a 32-bit integer */
51 #define GPIO_32_MASK			0x0003ffff
52 
53 struct gpio_twl4030_priv {
54 	struct gpio_chip gpio_chip;
55 	struct mutex mutex;
56 	int irq_base;
57 
58 	/* Bitfields for state caching */
59 	unsigned int usage_count;
60 	unsigned int direction;
61 	unsigned int out_state;
62 };
63 
64 /*----------------------------------------------------------------------*/
65 
66 /*
67  * To configure TWL4030 GPIO module registers
68  */
69 static inline int gpio_twl4030_write(u8 address, u8 data)
70 {
71 	return twl_i2c_write_u8(TWL4030_MODULE_GPIO, data, address);
72 }
73 
74 /*----------------------------------------------------------------------*/
75 
76 /*
77  * LED register offsets from TWL_MODULE_LED base
78  * PWMs A and B are dedicated to LEDs A and B, respectively.
79  */
80 
81 #define TWL4030_LED_LEDEN_REG	0x00
82 #define TWL4030_PWMAON_REG	0x01
83 #define TWL4030_PWMAOFF_REG	0x02
84 #define TWL4030_PWMBON_REG	0x03
85 #define TWL4030_PWMBOFF_REG	0x04
86 
87 /* LEDEN bits */
88 #define LEDEN_LEDAON		BIT(0)
89 #define LEDEN_LEDBON		BIT(1)
90 #define LEDEN_LEDAEXT		BIT(2)
91 #define LEDEN_LEDBEXT		BIT(3)
92 #define LEDEN_LEDAPWM		BIT(4)
93 #define LEDEN_LEDBPWM		BIT(5)
94 #define LEDEN_PWM_LENGTHA	BIT(6)
95 #define LEDEN_PWM_LENGTHB	BIT(7)
96 
97 #define PWMxON_LENGTH		BIT(7)
98 
99 /*----------------------------------------------------------------------*/
100 
101 /*
102  * To read a TWL4030 GPIO module register
103  */
104 static inline int gpio_twl4030_read(u8 address)
105 {
106 	u8 data;
107 	int ret = 0;
108 
109 	ret = twl_i2c_read_u8(TWL4030_MODULE_GPIO, &data, address);
110 	return (ret < 0) ? ret : data;
111 }
112 
113 /*----------------------------------------------------------------------*/
114 
115 static u8 cached_leden;
116 
117 /* The LED lines are open drain outputs ... a FET pulls to GND, so an
118  * external pullup is needed.  We could also expose the integrated PWM
119  * as a LED brightness control; we initialize it as "always on".
120  */
121 static void twl4030_led_set_value(int led, int value)
122 {
123 	u8 mask = LEDEN_LEDAON | LEDEN_LEDAPWM;
124 
125 	if (led)
126 		mask <<= 1;
127 
128 	if (value)
129 		cached_leden &= ~mask;
130 	else
131 		cached_leden |= mask;
132 
133 	WARN_ON_ONCE(twl_i2c_write_u8(TWL4030_MODULE_LED, cached_leden,
134 				      TWL4030_LED_LEDEN_REG));
135 }
136 
137 static int twl4030_set_gpio_direction(int gpio, int is_input)
138 {
139 	u8 d_bnk = gpio >> 3;
140 	u8 d_msk = BIT(gpio & 0x7);
141 	u8 reg = 0;
142 	u8 base = REG_GPIODATADIR1 + d_bnk;
143 	int ret = 0;
144 
145 	ret = gpio_twl4030_read(base);
146 	if (ret >= 0) {
147 		if (is_input)
148 			reg = ret & ~d_msk;
149 		else
150 			reg = ret | d_msk;
151 
152 		ret = gpio_twl4030_write(base, reg);
153 	}
154 	return ret;
155 }
156 
157 static int twl4030_get_gpio_direction(int gpio)
158 {
159 	u8 d_bnk = gpio >> 3;
160 	u8 d_msk = BIT(gpio & 0x7);
161 	u8 base = REG_GPIODATADIR1 + d_bnk;
162 	int ret = 0;
163 
164 	ret = gpio_twl4030_read(base);
165 	if (ret < 0)
166 		return ret;
167 
168 	if (ret & d_msk)
169 		return GPIO_LINE_DIRECTION_OUT;
170 
171 	return GPIO_LINE_DIRECTION_IN;
172 }
173 
174 static int twl4030_set_gpio_dataout(int gpio, int enable)
175 {
176 	u8 d_bnk = gpio >> 3;
177 	u8 d_msk = BIT(gpio & 0x7);
178 	u8 base = 0;
179 
180 	if (enable)
181 		base = REG_SETGPIODATAOUT1 + d_bnk;
182 	else
183 		base = REG_CLEARGPIODATAOUT1 + d_bnk;
184 
185 	return gpio_twl4030_write(base, d_msk);
186 }
187 
188 static int twl4030_get_gpio_datain(int gpio)
189 {
190 	u8 d_bnk = gpio >> 3;
191 	u8 d_off = gpio & 0x7;
192 	u8 base = 0;
193 	int ret = 0;
194 
195 	base = REG_GPIODATAIN1 + d_bnk;
196 	ret = gpio_twl4030_read(base);
197 	if (ret > 0)
198 		ret = (ret >> d_off) & 0x1;
199 
200 	return ret;
201 }
202 
203 /*----------------------------------------------------------------------*/
204 
205 static int twl_request(struct gpio_chip *chip, unsigned offset)
206 {
207 	struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
208 	int status = 0;
209 
210 	mutex_lock(&priv->mutex);
211 
212 	/* Support the two LED outputs as output-only GPIOs. */
213 	if (offset >= TWL4030_GPIO_MAX) {
214 		u8	ledclr_mask = LEDEN_LEDAON | LEDEN_LEDAEXT
215 				| LEDEN_LEDAPWM | LEDEN_PWM_LENGTHA;
216 		u8	reg = TWL4030_PWMAON_REG;
217 
218 		offset -= TWL4030_GPIO_MAX;
219 		if (offset) {
220 			ledclr_mask <<= 1;
221 			reg = TWL4030_PWMBON_REG;
222 		}
223 
224 		/* initialize PWM to always-drive */
225 		/* Configure PWM OFF register first */
226 		status = twl_i2c_write_u8(TWL4030_MODULE_LED, 0x7f, reg + 1);
227 		if (status < 0)
228 			goto done;
229 
230 		/* Followed by PWM ON register */
231 		status = twl_i2c_write_u8(TWL4030_MODULE_LED, 0x7f, reg);
232 		if (status < 0)
233 			goto done;
234 
235 		/* init LED to not-driven (high) */
236 		status = twl_i2c_read_u8(TWL4030_MODULE_LED, &cached_leden,
237 					 TWL4030_LED_LEDEN_REG);
238 		if (status < 0)
239 			goto done;
240 		cached_leden &= ~ledclr_mask;
241 		status = twl_i2c_write_u8(TWL4030_MODULE_LED, cached_leden,
242 					  TWL4030_LED_LEDEN_REG);
243 		if (status < 0)
244 			goto done;
245 
246 		status = 0;
247 		goto done;
248 	}
249 
250 	/* on first use, turn GPIO module "on" */
251 	if (!priv->usage_count) {
252 		struct twl4030_gpio_platform_data *pdata;
253 		u8 value = MASK_GPIO_CTRL_GPIO_ON;
254 
255 		/* optionally have the first two GPIOs switch vMMC1
256 		 * and vMMC2 power supplies based on card presence.
257 		 */
258 		pdata = dev_get_platdata(chip->parent);
259 		if (pdata)
260 			value |= pdata->mmc_cd & 0x03;
261 
262 		status = gpio_twl4030_write(REG_GPIO_CTRL, value);
263 	}
264 
265 done:
266 	if (!status)
267 		priv->usage_count |= BIT(offset);
268 
269 	mutex_unlock(&priv->mutex);
270 	return status;
271 }
272 
273 static void twl_free(struct gpio_chip *chip, unsigned offset)
274 {
275 	struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
276 
277 	mutex_lock(&priv->mutex);
278 	if (offset >= TWL4030_GPIO_MAX) {
279 		twl4030_led_set_value(offset - TWL4030_GPIO_MAX, 1);
280 		goto out;
281 	}
282 
283 	priv->usage_count &= ~BIT(offset);
284 
285 	/* on last use, switch off GPIO module */
286 	if (!priv->usage_count)
287 		gpio_twl4030_write(REG_GPIO_CTRL, 0x0);
288 
289 out:
290 	mutex_unlock(&priv->mutex);
291 }
292 
293 static int twl_direction_in(struct gpio_chip *chip, unsigned offset)
294 {
295 	struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
296 	int ret;
297 
298 	mutex_lock(&priv->mutex);
299 	if (offset < TWL4030_GPIO_MAX)
300 		ret = twl4030_set_gpio_direction(offset, 1);
301 	else
302 		ret = -EINVAL;	/* LED outputs can't be set as input */
303 
304 	if (!ret)
305 		priv->direction &= ~BIT(offset);
306 
307 	mutex_unlock(&priv->mutex);
308 
309 	return ret;
310 }
311 
312 static int twl_get(struct gpio_chip *chip, unsigned offset)
313 {
314 	struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
315 	int ret;
316 	int status = 0;
317 
318 	mutex_lock(&priv->mutex);
319 	if (!(priv->usage_count & BIT(offset))) {
320 		ret = -EPERM;
321 		goto out;
322 	}
323 
324 	if (priv->direction & BIT(offset))
325 		status = priv->out_state & BIT(offset);
326 	else
327 		status = twl4030_get_gpio_datain(offset);
328 
329 	ret = (status < 0) ? status : !!status;
330 out:
331 	mutex_unlock(&priv->mutex);
332 	return ret;
333 }
334 
335 static void twl_set(struct gpio_chip *chip, unsigned offset, int value)
336 {
337 	struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
338 
339 	mutex_lock(&priv->mutex);
340 	if (offset < TWL4030_GPIO_MAX)
341 		twl4030_set_gpio_dataout(offset, value);
342 	else
343 		twl4030_led_set_value(offset - TWL4030_GPIO_MAX, value);
344 
345 	if (value)
346 		priv->out_state |= BIT(offset);
347 	else
348 		priv->out_state &= ~BIT(offset);
349 
350 	mutex_unlock(&priv->mutex);
351 }
352 
353 static int twl_direction_out(struct gpio_chip *chip, unsigned offset, int value)
354 {
355 	struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
356 	int ret = 0;
357 
358 	mutex_lock(&priv->mutex);
359 	if (offset < TWL4030_GPIO_MAX) {
360 		ret = twl4030_set_gpio_direction(offset, 0);
361 		if (ret) {
362 			mutex_unlock(&priv->mutex);
363 			return ret;
364 		}
365 	}
366 
367 	/*
368 	 *  LED gpios i.e. offset >= TWL4030_GPIO_MAX are always output
369 	 */
370 
371 	priv->direction |= BIT(offset);
372 	mutex_unlock(&priv->mutex);
373 
374 	twl_set(chip, offset, value);
375 
376 	return ret;
377 }
378 
379 static int twl_get_direction(struct gpio_chip *chip, unsigned offset)
380 {
381 	struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
382 	/*
383 	 * Default GPIO_LINE_DIRECTION_OUT
384 	 * LED GPIOs >= TWL4030_GPIO_MAX are always output
385 	 */
386 	int ret = GPIO_LINE_DIRECTION_OUT;
387 
388 	mutex_lock(&priv->mutex);
389 	if (offset < TWL4030_GPIO_MAX) {
390 		ret = twl4030_get_gpio_direction(offset);
391 		if (ret) {
392 			mutex_unlock(&priv->mutex);
393 			return ret;
394 		}
395 	}
396 	mutex_unlock(&priv->mutex);
397 
398 	return ret;
399 }
400 
401 static int twl_to_irq(struct gpio_chip *chip, unsigned offset)
402 {
403 	struct gpio_twl4030_priv *priv = gpiochip_get_data(chip);
404 
405 	return (priv->irq_base && (offset < TWL4030_GPIO_MAX))
406 		? (priv->irq_base + offset)
407 		: -EINVAL;
408 }
409 
410 static const struct gpio_chip template_chip = {
411 	.label			= "twl4030",
412 	.owner			= THIS_MODULE,
413 	.request		= twl_request,
414 	.free			= twl_free,
415 	.direction_input	= twl_direction_in,
416 	.direction_output	= twl_direction_out,
417 	.get_direction		= twl_get_direction,
418 	.get			= twl_get,
419 	.set			= twl_set,
420 	.to_irq			= twl_to_irq,
421 	.can_sleep		= true,
422 };
423 
424 /*----------------------------------------------------------------------*/
425 
426 static int gpio_twl4030_pulls(u32 ups, u32 downs)
427 {
428 	u8		message[5];
429 	unsigned	i, gpio_bit;
430 
431 	/* For most pins, a pulldown was enabled by default.
432 	 * We should have data that's specific to this board.
433 	 */
434 	for (gpio_bit = 1, i = 0; i < 5; i++) {
435 		u8		bit_mask;
436 		unsigned	j;
437 
438 		for (bit_mask = 0, j = 0; j < 8; j += 2, gpio_bit <<= 1) {
439 			if (ups & gpio_bit)
440 				bit_mask |= 1 << (j + 1);
441 			else if (downs & gpio_bit)
442 				bit_mask |= 1 << (j + 0);
443 		}
444 		message[i] = bit_mask;
445 	}
446 
447 	return twl_i2c_write(TWL4030_MODULE_GPIO, message,
448 				REG_GPIOPUPDCTR1, 5);
449 }
450 
451 static int gpio_twl4030_debounce(u32 debounce, u8 mmc_cd)
452 {
453 	u8		message[3];
454 
455 	/* 30 msec of debouncing is always used for MMC card detect,
456 	 * and is optional for everything else.
457 	 */
458 	message[0] = (debounce & 0xff) | (mmc_cd & 0x03);
459 	debounce >>= 8;
460 	message[1] = (debounce & 0xff);
461 	debounce >>= 8;
462 	message[2] = (debounce & 0x03);
463 
464 	return twl_i2c_write(TWL4030_MODULE_GPIO, message,
465 				REG_GPIO_DEBEN1, 3);
466 }
467 
468 static struct twl4030_gpio_platform_data *of_gpio_twl4030(struct device *dev,
469 				struct twl4030_gpio_platform_data *pdata)
470 {
471 	struct twl4030_gpio_platform_data *omap_twl_info;
472 
473 	omap_twl_info = devm_kzalloc(dev, sizeof(*omap_twl_info), GFP_KERNEL);
474 	if (!omap_twl_info)
475 		return NULL;
476 
477 	if (pdata)
478 		*omap_twl_info = *pdata;
479 
480 	omap_twl_info->use_leds = of_property_read_bool(dev->of_node,
481 			"ti,use-leds");
482 
483 	of_property_read_u32(dev->of_node, "ti,debounce",
484 			     &omap_twl_info->debounce);
485 	of_property_read_u32(dev->of_node, "ti,mmc-cd",
486 			     (u32 *)&omap_twl_info->mmc_cd);
487 	of_property_read_u32(dev->of_node, "ti,pullups",
488 			     &omap_twl_info->pullups);
489 	of_property_read_u32(dev->of_node, "ti,pulldowns",
490 			     &omap_twl_info->pulldowns);
491 
492 	return omap_twl_info;
493 }
494 
495 /* Cannot use as gpio_twl4030_probe() calls us */
496 static int gpio_twl4030_remove(struct platform_device *pdev)
497 {
498 	struct gpio_twl4030_priv *priv = platform_get_drvdata(pdev);
499 
500 	gpiochip_remove(&priv->gpio_chip);
501 
502 	/* REVISIT no support yet for deregistering all the IRQs */
503 	WARN_ON(!is_module());
504 	return 0;
505 }
506 
507 static int gpio_twl4030_probe(struct platform_device *pdev)
508 {
509 	struct twl4030_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
510 	struct device_node *node = pdev->dev.of_node;
511 	struct gpio_twl4030_priv *priv;
512 	int ret, irq_base;
513 
514 	priv = devm_kzalloc(&pdev->dev, sizeof(struct gpio_twl4030_priv),
515 			    GFP_KERNEL);
516 	if (!priv)
517 		return -ENOMEM;
518 
519 	/* maybe setup IRQs */
520 	if (is_module()) {
521 		dev_err(&pdev->dev, "can't dispatch IRQs from modules\n");
522 		goto no_irqs;
523 	}
524 
525 	irq_base = devm_irq_alloc_descs(&pdev->dev, -1,
526 					0, TWL4030_GPIO_MAX, 0);
527 	if (irq_base < 0) {
528 		dev_err(&pdev->dev, "Failed to alloc irq_descs\n");
529 		return irq_base;
530 	}
531 
532 	irq_domain_add_legacy(node, TWL4030_GPIO_MAX, irq_base, 0,
533 			      &irq_domain_simple_ops, NULL);
534 
535 	ret = twl4030_sih_setup(&pdev->dev, TWL4030_MODULE_GPIO, irq_base);
536 	if (ret < 0)
537 		return ret;
538 
539 	priv->irq_base = irq_base;
540 
541 no_irqs:
542 	priv->gpio_chip = template_chip;
543 	priv->gpio_chip.base = -1;
544 	priv->gpio_chip.ngpio = TWL4030_GPIO_MAX;
545 	priv->gpio_chip.parent = &pdev->dev;
546 
547 	mutex_init(&priv->mutex);
548 
549 	if (node)
550 		pdata = of_gpio_twl4030(&pdev->dev, pdata);
551 
552 	if (pdata == NULL) {
553 		dev_err(&pdev->dev, "Platform data is missing\n");
554 		return -ENXIO;
555 	}
556 
557 	/*
558 	 * NOTE:  boards may waste power if they don't set pullups
559 	 * and pulldowns correctly ... default for non-ULPI pins is
560 	 * pulldown, and some other pins may have external pullups
561 	 * or pulldowns.  Careful!
562 	 */
563 	ret = gpio_twl4030_pulls(pdata->pullups, pdata->pulldowns);
564 	if (ret)
565 		dev_dbg(&pdev->dev, "pullups %.05x %.05x --> %d\n",
566 			pdata->pullups, pdata->pulldowns, ret);
567 
568 	ret = gpio_twl4030_debounce(pdata->debounce, pdata->mmc_cd);
569 	if (ret)
570 		dev_dbg(&pdev->dev, "debounce %.03x %.01x --> %d\n",
571 			pdata->debounce, pdata->mmc_cd, ret);
572 
573 	/*
574 	 * NOTE: we assume VIBRA_CTL.VIBRA_EN, in MODULE_AUDIO_VOICE,
575 	 * is (still) clear if use_leds is set.
576 	 */
577 	if (pdata->use_leds)
578 		priv->gpio_chip.ngpio += 2;
579 
580 	ret = gpiochip_add_data(&priv->gpio_chip, priv);
581 	if (ret < 0) {
582 		dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
583 		priv->gpio_chip.ngpio = 0;
584 		gpio_twl4030_remove(pdev);
585 		goto out;
586 	}
587 
588 	platform_set_drvdata(pdev, priv);
589 
590 	if (pdata->setup) {
591 		int status;
592 
593 		status = pdata->setup(&pdev->dev, priv->gpio_chip.base,
594 				      TWL4030_GPIO_MAX);
595 		if (status)
596 			dev_dbg(&pdev->dev, "setup --> %d\n", status);
597 	}
598 
599 out:
600 	return ret;
601 }
602 
603 static const struct of_device_id twl_gpio_match[] = {
604 	{ .compatible = "ti,twl4030-gpio", },
605 	{ },
606 };
607 MODULE_DEVICE_TABLE(of, twl_gpio_match);
608 
609 /* Note:  this hardware lives inside an I2C-based multi-function device. */
610 MODULE_ALIAS("platform:twl4030_gpio");
611 
612 static struct platform_driver gpio_twl4030_driver = {
613 	.driver = {
614 		.name	= "twl4030_gpio",
615 		.of_match_table = twl_gpio_match,
616 	},
617 	.probe		= gpio_twl4030_probe,
618 	.remove		= gpio_twl4030_remove,
619 };
620 
621 static int __init gpio_twl4030_init(void)
622 {
623 	return platform_driver_register(&gpio_twl4030_driver);
624 }
625 subsys_initcall(gpio_twl4030_init);
626 
627 static void __exit gpio_twl4030_exit(void)
628 {
629 	platform_driver_unregister(&gpio_twl4030_driver);
630 }
631 module_exit(gpio_twl4030_exit);
632 
633 MODULE_AUTHOR("Texas Instruments, Inc.");
634 MODULE_DESCRIPTION("GPIO interface for TWL4030");
635 MODULE_LICENSE("GPL");
636