xref: /linux/drivers/gpio/gpio-twl6040.c (revision 22c55fb9eb92395d999b8404d73e58540d11bdd8)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Access to GPOs on TWL6040 chip
4  *
5  * Copyright (C) 2012 Texas Instruments, Inc.
6  *
7  * Authors:
8  *	Sergio Aguirre <saaguirre@ti.com>
9  *	Peter Ujfalusi <peter.ujfalusi@ti.com>
10  */
11 
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/kthread.h>
15 #include <linux/irq.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/platform_device.h>
18 #include <linux/bitops.h>
19 #include <linux/of.h>
20 
21 #include <linux/mfd/twl6040.h>
22 
23 static int twl6040gpo_get(struct gpio_chip *chip, unsigned offset)
24 {
25 	struct twl6040 *twl6040 = gpiochip_get_data(chip);
26 	int ret = 0;
27 
28 	ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
29 	if (ret < 0)
30 		return ret;
31 
32 	return !!(ret & BIT(offset));
33 }
34 
35 static int twl6040gpo_get_direction(struct gpio_chip *chip, unsigned offset)
36 {
37 	return GPIO_LINE_DIRECTION_OUT;
38 }
39 
40 static int twl6040gpo_set(struct gpio_chip *chip, unsigned int offset,
41 			  int value)
42 {
43 	struct twl6040 *twl6040 = gpiochip_get_data(chip);
44 	int ret;
45 	u8 gpoctl;
46 
47 	ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
48 	if (ret < 0)
49 		return ret;
50 
51 	if (value)
52 		gpoctl = ret | BIT(offset);
53 	else
54 		gpoctl = ret & ~BIT(offset);
55 
56 	return twl6040_reg_write(twl6040, TWL6040_REG_GPOCTL, gpoctl);
57 }
58 
59 static int twl6040gpo_direction_out(struct gpio_chip *chip, unsigned int offset,
60 				    int value)
61 {
62 	/* This only drives GPOs, and can't change direction */
63 	return twl6040gpo_set(chip, offset, value);
64 }
65 
66 static struct gpio_chip twl6040gpo_chip = {
67 	.label			= "twl6040",
68 	.owner			= THIS_MODULE,
69 	.get			= twl6040gpo_get,
70 	.direction_output	= twl6040gpo_direction_out,
71 	.get_direction		= twl6040gpo_get_direction,
72 	.set			= twl6040gpo_set,
73 	.can_sleep		= true,
74 };
75 
76 /*----------------------------------------------------------------------*/
77 
78 static int gpo_twl6040_probe(struct platform_device *pdev)
79 {
80 	struct device *twl6040_core_dev = pdev->dev.parent;
81 	struct twl6040 *twl6040 = dev_get_drvdata(twl6040_core_dev);
82 	int ret;
83 
84 	device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
85 
86 	twl6040gpo_chip.base = -1;
87 
88 	if (twl6040_get_revid(twl6040) < TWL6041_REV_ES2_0)
89 		twl6040gpo_chip.ngpio = 3; /* twl6040 have 3 GPO */
90 	else
91 		twl6040gpo_chip.ngpio = 1; /* twl6041 have 1 GPO */
92 
93 	twl6040gpo_chip.parent = &pdev->dev;
94 
95 	ret = devm_gpiochip_add_data(&pdev->dev, &twl6040gpo_chip, twl6040);
96 	if (ret < 0) {
97 		dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
98 		twl6040gpo_chip.ngpio = 0;
99 	}
100 
101 	return ret;
102 }
103 
104 /* Note:  this hardware lives inside an I2C-based multi-function device. */
105 MODULE_ALIAS("platform:twl6040-gpo");
106 
107 static struct platform_driver gpo_twl6040_driver = {
108 	.driver = {
109 		.name	= "twl6040-gpo",
110 	},
111 	.probe		= gpo_twl6040_probe,
112 };
113 
114 module_platform_driver(gpo_twl6040_driver);
115 
116 MODULE_AUTHOR("Texas Instruments, Inc.");
117 MODULE_DESCRIPTION("GPO interface for TWL6040");
118 MODULE_LICENSE("GPL");
119