Merge tag 'staging-6.6-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6-block.git] / drivers / gpio / gpio-tps65910.c
CommitLineData
d5a4da15 1// SPDX-License-Identifier: GPL-2.0+
2537df72 2/*
c103de24 3 * TI TPS6591x GPIO driver
2537df72
GG
4 *
5 * Copyright 2010 Texas Instruments Inc.
6 *
7 * Author: Graeme Gregory <gg@slimlogic.co.uk>
02c7a13e 8 * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
2537df72
GG
9 */
10
11#include <linux/kernel.h>
02c7a13e 12#include <linux/init.h>
2537df72 13#include <linux/errno.h>
5d75683e 14#include <linux/gpio/driver.h>
2537df72 15#include <linux/i2c.h>
10bbc48d 16#include <linux/platform_device.h>
2537df72 17#include <linux/mfd/tps65910.h>
e91d0f05 18#include <linux/of.h>
2537df72 19
10bbc48d
LD
20struct tps65910_gpio {
21 struct gpio_chip gpio_chip;
22 struct tps65910 *tps65910;
23};
24
2537df72
GG
25static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset)
26{
b7c17b1b 27 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
10bbc48d 28 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
3f7e8275 29 unsigned int val;
2537df72 30
a233d4eb 31 regmap_read(tps65910->regmap, TPS65910_GPIO0 + offset, &val);
2537df72 32
11ad14f8 33 if (val & GPIO_STS_MASK)
2537df72
GG
34 return 1;
35
36 return 0;
37}
38
39static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset,
40 int value)
41{
b7c17b1b 42 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
10bbc48d 43 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
2537df72
GG
44
45 if (value)
a233d4eb 46 regmap_set_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
11ad14f8 47 GPIO_SET_MASK);
2537df72 48 else
a233d4eb 49 regmap_clear_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
11ad14f8 50 GPIO_SET_MASK);
2537df72
GG
51}
52
53static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset,
54 int value)
55{
b7c17b1b 56 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
10bbc48d 57 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
2537df72
GG
58
59 /* Set the initial value */
94bd2442 60 tps65910_gpio_set(gc, offset, value);
2537df72 61
a233d4eb 62 return regmap_set_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
11ad14f8 63 GPIO_CFG_MASK);
2537df72
GG
64}
65
66static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset)
67{
b7c17b1b 68 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
10bbc48d 69 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
2537df72 70
a233d4eb 71 return regmap_clear_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
11ad14f8 72 GPIO_CFG_MASK);
2537df72
GG
73}
74
6fe02e9f
LD
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
3836309d 106static int tps65910_gpio_probe(struct platform_device *pdev)
2537df72 107{
10bbc48d
LD
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;
2537df72 111 int ret;
10bbc48d
LD
112 int i;
113
6dbe6c07
AS
114 device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
115
10bbc48d
LD
116 tps65910_gpio = devm_kzalloc(&pdev->dev,
117 sizeof(*tps65910_gpio), GFP_KERNEL);
0661175a 118 if (!tps65910_gpio)
10bbc48d 119 return -ENOMEM;
2537df72 120
10bbc48d 121 tps65910_gpio->tps65910 = tps65910;
2537df72 122
10bbc48d
LD
123 tps65910_gpio->gpio_chip.owner = THIS_MODULE;
124 tps65910_gpio->gpio_chip.label = tps65910->i2c_client->name;
11ad14f8 125
195c65e8 126 switch (tps65910_chip_id(tps65910)) {
11ad14f8 127 case TPS65910:
10bbc48d 128 tps65910_gpio->gpio_chip.ngpio = TPS65910_NUM_GPIO;
58956ba2 129 break;
11ad14f8 130 case TPS65911:
10bbc48d 131 tps65910_gpio->gpio_chip.ngpio = TPS65911_NUM_GPIO;
58956ba2 132 break;
11ad14f8 133 default:
10bbc48d 134 return -EINVAL;
11ad14f8 135 }
9fb1f39e 136 tps65910_gpio->gpio_chip.can_sleep = true;
10bbc48d
LD
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;
58383c78 141 tps65910_gpio->gpio_chip.parent = &pdev->dev;
6dbe6c07 142
10bbc48d
LD
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
6fe02e9f
LD
148 if (!pdata && tps65910->dev->of_node)
149 pdata = tps65910_parse_dt_for_gpio(&pdev->dev, tps65910,
150 tps65910_gpio->gpio_chip.ngpio);
151
10bbc48d
LD
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
a233d4eb 160 ret = regmap_set_bits(tps65910->regmap,
10bbc48d
LD
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:
96a03e86
AA
168 return devm_gpiochip_add_data(&pdev->dev, &tps65910_gpio->gpio_chip,
169 tps65910_gpio);
2537df72 170}
10bbc48d 171
10bbc48d
LD
172static struct platform_driver tps65910_gpio_driver = {
173 .driver.name = "tps65910-gpio",
10bbc48d 174 .probe = tps65910_gpio_probe,
10bbc48d
LD
175};
176
177static int __init tps65910_gpio_init(void)
178{
179 return platform_driver_register(&tps65910_gpio_driver);
180}
181subsys_initcall(tps65910_gpio_init);