1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/kthread.h>
28#include <linux/irq.h>
29#include <linux/gpio.h>
30#include <linux/platform_device.h>
31#include <linux/of.h>
32
33#include <linux/mfd/twl6040.h>
34
35static struct gpio_chip twl6040gpo_chip;
36
37static int twl6040gpo_get(struct gpio_chip *chip, unsigned offset)
38{
39 struct twl6040 *twl6040 = dev_get_drvdata(chip->dev->parent);
40 int ret = 0;
41
42 ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
43 if (ret < 0)
44 return ret;
45
46 return (ret >> offset) & 1;
47}
48
49static int twl6040gpo_direction_out(struct gpio_chip *chip, unsigned offset,
50 int value)
51{
52
53 return 0;
54}
55
56static void twl6040gpo_set(struct gpio_chip *chip, unsigned offset, int value)
57{
58 struct twl6040 *twl6040 = dev_get_drvdata(chip->dev->parent);
59 int ret;
60 u8 gpoctl;
61
62 ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
63 if (ret < 0)
64 return;
65
66 if (value)
67 gpoctl = ret | (1 << offset);
68 else
69 gpoctl = ret & ~(1 << offset);
70
71 twl6040_reg_write(twl6040, TWL6040_REG_GPOCTL, gpoctl);
72}
73
74static struct gpio_chip twl6040gpo_chip = {
75 .label = "twl6040",
76 .owner = THIS_MODULE,
77 .get = twl6040gpo_get,
78 .direction_output = twl6040gpo_direction_out,
79 .set = twl6040gpo_set,
80 .can_sleep = 1,
81};
82
83
84
85static int gpo_twl6040_probe(struct platform_device *pdev)
86{
87 struct twl6040_gpo_data *pdata = pdev->dev.platform_data;
88 struct device *twl6040_core_dev = pdev->dev.parent;
89 struct twl6040 *twl6040 = dev_get_drvdata(twl6040_core_dev);
90 int ret;
91
92 if (pdata)
93 twl6040gpo_chip.base = pdata->gpio_base;
94 else
95 twl6040gpo_chip.base = -1;
96
97 if (twl6040_get_revid(twl6040) < TWL6041_REV_ES2_0)
98 twl6040gpo_chip.ngpio = 3;
99 else
100 twl6040gpo_chip.ngpio = 1;
101
102 twl6040gpo_chip.dev = &pdev->dev;
103#ifdef CONFIG_OF_GPIO
104 twl6040gpo_chip.of_node = twl6040_core_dev->of_node;
105#endif
106
107 ret = gpiochip_add(&twl6040gpo_chip);
108 if (ret < 0) {
109 dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
110 twl6040gpo_chip.ngpio = 0;
111 }
112
113 return ret;
114}
115
116static int gpo_twl6040_remove(struct platform_device *pdev)
117{
118 return gpiochip_remove(&twl6040gpo_chip);
119}
120
121
122MODULE_ALIAS("platform:twl6040-gpo");
123
124static struct platform_driver gpo_twl6040_driver = {
125 .driver = {
126 .name = "twl6040-gpo",
127 .owner = THIS_MODULE,
128 },
129 .probe = gpo_twl6040_probe,
130 .remove = gpo_twl6040_remove,
131};
132
133module_platform_driver(gpo_twl6040_driver);
134
135MODULE_AUTHOR("Texas Instruments, Inc.");
136MODULE_DESCRIPTION("GPO interface for TWL6040");
137MODULE_LICENSE("GPL");
138