Loading...
1/*
2 * TI TPS6591x GPIO driver
3 *
4 * Copyright 2010 Texas Instruments Inc.
5 *
6 * Author: Graeme Gregory <gg@slimlogic.co.uk>
7 * Author: Jorge Eduardo Candelaria jedu@slimlogic.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/errno.h>
19#include <linux/gpio.h>
20#include <linux/i2c.h>
21#include <linux/platform_device.h>
22#include <linux/mfd/tps65910.h>
23#include <linux/of_device.h>
24
25struct tps65910_gpio {
26 struct gpio_chip gpio_chip;
27 struct tps65910 *tps65910;
28};
29
30static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset)
31{
32 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
33 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
34 unsigned int val;
35
36 tps65910_reg_read(tps65910, TPS65910_GPIO0 + offset, &val);
37
38 if (val & GPIO_STS_MASK)
39 return 1;
40
41 return 0;
42}
43
44static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset,
45 int value)
46{
47 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
48 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
49
50 if (value)
51 tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
52 GPIO_SET_MASK);
53 else
54 tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
55 GPIO_SET_MASK);
56}
57
58static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset,
59 int value)
60{
61 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
62 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
63
64 /* Set the initial value */
65 tps65910_gpio_set(gc, offset, value);
66
67 return tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
68 GPIO_CFG_MASK);
69}
70
71static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset)
72{
73 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
74 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
75
76 return tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
77 GPIO_CFG_MASK);
78}
79
80#ifdef CONFIG_OF
81static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
82 struct tps65910 *tps65910, int chip_ngpio)
83{
84 struct tps65910_board *tps65910_board = tps65910->of_plat_data;
85 unsigned int prop_array[TPS6591X_MAX_NUM_GPIO];
86 int ngpio = min(chip_ngpio, TPS6591X_MAX_NUM_GPIO);
87 int ret;
88 int idx;
89
90 tps65910_board->gpio_base = -1;
91 ret = of_property_read_u32_array(tps65910->dev->of_node,
92 "ti,en-gpio-sleep", prop_array, ngpio);
93 if (ret < 0) {
94 dev_dbg(dev, "ti,en-gpio-sleep not specified\n");
95 return tps65910_board;
96 }
97
98 for (idx = 0; idx < ngpio; idx++)
99 tps65910_board->en_gpio_sleep[idx] = (prop_array[idx] != 0);
100
101 return tps65910_board;
102}
103#else
104static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
105 struct tps65910 *tps65910, int chip_ngpio)
106{
107 return NULL;
108}
109#endif
110
111static int tps65910_gpio_probe(struct platform_device *pdev)
112{
113 struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
114 struct tps65910_board *pdata = dev_get_platdata(tps65910->dev);
115 struct tps65910_gpio *tps65910_gpio;
116 int ret;
117 int i;
118
119 tps65910_gpio = devm_kzalloc(&pdev->dev,
120 sizeof(*tps65910_gpio), GFP_KERNEL);
121 if (!tps65910_gpio)
122 return -ENOMEM;
123
124 tps65910_gpio->tps65910 = tps65910;
125
126 tps65910_gpio->gpio_chip.owner = THIS_MODULE;
127 tps65910_gpio->gpio_chip.label = tps65910->i2c_client->name;
128
129 switch (tps65910_chip_id(tps65910)) {
130 case TPS65910:
131 tps65910_gpio->gpio_chip.ngpio = TPS65910_NUM_GPIO;
132 break;
133 case TPS65911:
134 tps65910_gpio->gpio_chip.ngpio = TPS65911_NUM_GPIO;
135 break;
136 default:
137 return -EINVAL;
138 }
139 tps65910_gpio->gpio_chip.can_sleep = true;
140 tps65910_gpio->gpio_chip.direction_input = tps65910_gpio_input;
141 tps65910_gpio->gpio_chip.direction_output = tps65910_gpio_output;
142 tps65910_gpio->gpio_chip.set = tps65910_gpio_set;
143 tps65910_gpio->gpio_chip.get = tps65910_gpio_get;
144 tps65910_gpio->gpio_chip.parent = &pdev->dev;
145#ifdef CONFIG_OF_GPIO
146 tps65910_gpio->gpio_chip.of_node = tps65910->dev->of_node;
147#endif
148 if (pdata && pdata->gpio_base)
149 tps65910_gpio->gpio_chip.base = pdata->gpio_base;
150 else
151 tps65910_gpio->gpio_chip.base = -1;
152
153 if (!pdata && tps65910->dev->of_node)
154 pdata = tps65910_parse_dt_for_gpio(&pdev->dev, tps65910,
155 tps65910_gpio->gpio_chip.ngpio);
156
157 if (!pdata)
158 goto skip_init;
159
160 /* Configure sleep control for gpios if provided */
161 for (i = 0; i < tps65910_gpio->gpio_chip.ngpio; ++i) {
162 if (!pdata->en_gpio_sleep[i])
163 continue;
164
165 ret = tps65910_reg_set_bits(tps65910,
166 TPS65910_GPIO0 + i, GPIO_SLEEP_MASK);
167 if (ret < 0)
168 dev_warn(tps65910->dev,
169 "GPIO Sleep setting failed with err %d\n", ret);
170 }
171
172skip_init:
173 ret = devm_gpiochip_add_data(&pdev->dev, &tps65910_gpio->gpio_chip,
174 tps65910_gpio);
175 if (ret < 0) {
176 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
177 return ret;
178 }
179
180 platform_set_drvdata(pdev, tps65910_gpio);
181
182 return ret;
183}
184
185static struct platform_driver tps65910_gpio_driver = {
186 .driver.name = "tps65910-gpio",
187 .driver.owner = THIS_MODULE,
188 .probe = tps65910_gpio_probe,
189};
190
191static int __init tps65910_gpio_init(void)
192{
193 return platform_driver_register(&tps65910_gpio_driver);
194}
195subsys_initcall(tps65910_gpio_init);
196
197static void __exit tps65910_gpio_exit(void)
198{
199 platform_driver_unregister(&tps65910_gpio_driver);
200}
201module_exit(tps65910_gpio_exit);
202
203MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
204MODULE_AUTHOR("Jorge Eduardo Candelaria jedu@slimlogic.co.uk>");
205MODULE_DESCRIPTION("GPIO interface for TPS65910/TPS6511 PMICs");
206MODULE_LICENSE("GPL v2");
207MODULE_ALIAS("platform:tps65910-gpio");
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * TI TPS6591x GPIO driver
4 *
5 * Copyright 2010 Texas Instruments Inc.
6 *
7 * Author: Graeme Gregory <gg@slimlogic.co.uk>
8 * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/errno.h>
14#include <linux/gpio/driver.h>
15#include <linux/i2c.h>
16#include <linux/platform_device.h>
17#include <linux/mfd/tps65910.h>
18#include <linux/of_device.h>
19
20struct tps65910_gpio {
21 struct gpio_chip gpio_chip;
22 struct tps65910 *tps65910;
23};
24
25static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset)
26{
27 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
28 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
29 unsigned int val;
30
31 regmap_read(tps65910->regmap, TPS65910_GPIO0 + offset, &val);
32
33 if (val & GPIO_STS_MASK)
34 return 1;
35
36 return 0;
37}
38
39static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset,
40 int value)
41{
42 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
43 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
44
45 if (value)
46 regmap_set_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
47 GPIO_SET_MASK);
48 else
49 regmap_clear_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
50 GPIO_SET_MASK);
51}
52
53static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset,
54 int value)
55{
56 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
57 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
58
59 /* Set the initial value */
60 tps65910_gpio_set(gc, offset, value);
61
62 return regmap_set_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
63 GPIO_CFG_MASK);
64}
65
66static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset)
67{
68 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
69 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
70
71 return regmap_clear_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
72 GPIO_CFG_MASK);
73}
74
75#ifdef CONFIG_OF
76static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
77 struct tps65910 *tps65910, int chip_ngpio)
78{
79 struct tps65910_board *tps65910_board = tps65910->of_plat_data;
80 unsigned int prop_array[TPS6591X_MAX_NUM_GPIO];
81 int ngpio = min(chip_ngpio, TPS6591X_MAX_NUM_GPIO);
82 int ret;
83 int idx;
84
85 tps65910_board->gpio_base = -1;
86 ret = of_property_read_u32_array(tps65910->dev->of_node,
87 "ti,en-gpio-sleep", prop_array, ngpio);
88 if (ret < 0) {
89 dev_dbg(dev, "ti,en-gpio-sleep not specified\n");
90 return tps65910_board;
91 }
92
93 for (idx = 0; idx < ngpio; idx++)
94 tps65910_board->en_gpio_sleep[idx] = (prop_array[idx] != 0);
95
96 return tps65910_board;
97}
98#else
99static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
100 struct tps65910 *tps65910, int chip_ngpio)
101{
102 return NULL;
103}
104#endif
105
106static int tps65910_gpio_probe(struct platform_device *pdev)
107{
108 struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
109 struct tps65910_board *pdata = dev_get_platdata(tps65910->dev);
110 struct tps65910_gpio *tps65910_gpio;
111 int ret;
112 int i;
113
114 device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
115
116 tps65910_gpio = devm_kzalloc(&pdev->dev,
117 sizeof(*tps65910_gpio), GFP_KERNEL);
118 if (!tps65910_gpio)
119 return -ENOMEM;
120
121 tps65910_gpio->tps65910 = tps65910;
122
123 tps65910_gpio->gpio_chip.owner = THIS_MODULE;
124 tps65910_gpio->gpio_chip.label = tps65910->i2c_client->name;
125
126 switch (tps65910_chip_id(tps65910)) {
127 case TPS65910:
128 tps65910_gpio->gpio_chip.ngpio = TPS65910_NUM_GPIO;
129 break;
130 case TPS65911:
131 tps65910_gpio->gpio_chip.ngpio = TPS65911_NUM_GPIO;
132 break;
133 default:
134 return -EINVAL;
135 }
136 tps65910_gpio->gpio_chip.can_sleep = true;
137 tps65910_gpio->gpio_chip.direction_input = tps65910_gpio_input;
138 tps65910_gpio->gpio_chip.direction_output = tps65910_gpio_output;
139 tps65910_gpio->gpio_chip.set = tps65910_gpio_set;
140 tps65910_gpio->gpio_chip.get = tps65910_gpio_get;
141 tps65910_gpio->gpio_chip.parent = &pdev->dev;
142
143 if (pdata && pdata->gpio_base)
144 tps65910_gpio->gpio_chip.base = pdata->gpio_base;
145 else
146 tps65910_gpio->gpio_chip.base = -1;
147
148 if (!pdata && tps65910->dev->of_node)
149 pdata = tps65910_parse_dt_for_gpio(&pdev->dev, tps65910,
150 tps65910_gpio->gpio_chip.ngpio);
151
152 if (!pdata)
153 goto skip_init;
154
155 /* Configure sleep control for gpios if provided */
156 for (i = 0; i < tps65910_gpio->gpio_chip.ngpio; ++i) {
157 if (!pdata->en_gpio_sleep[i])
158 continue;
159
160 ret = regmap_set_bits(tps65910->regmap,
161 TPS65910_GPIO0 + i, GPIO_SLEEP_MASK);
162 if (ret < 0)
163 dev_warn(tps65910->dev,
164 "GPIO Sleep setting failed with err %d\n", ret);
165 }
166
167skip_init:
168 return devm_gpiochip_add_data(&pdev->dev, &tps65910_gpio->gpio_chip,
169 tps65910_gpio);
170}
171
172static struct platform_driver tps65910_gpio_driver = {
173 .driver.name = "tps65910-gpio",
174 .probe = tps65910_gpio_probe,
175};
176
177static int __init tps65910_gpio_init(void)
178{
179 return platform_driver_register(&tps65910_gpio_driver);
180}
181subsys_initcall(tps65910_gpio_init);