Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / gpio / gpio-tps65910.c
blob0c0b445c75c05ecd868d94ca34d97e575f5107b9
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * TI TPS6591x GPIO driver
5 * Copyright 2010 Texas Instruments Inc.
7 * Author: Graeme Gregory <gg@slimlogic.co.uk>
8 * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
9 */
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>
20 struct tps65910_gpio {
21 struct gpio_chip gpio_chip;
22 struct tps65910 *tps65910;
25 static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset)
27 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
28 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
29 unsigned int val;
31 regmap_read(tps65910->regmap, TPS65910_GPIO0 + offset, &val);
33 if (val & GPIO_STS_MASK)
34 return 1;
36 return 0;
39 static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset,
40 int value)
42 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
43 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
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);
53 static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset,
54 int value)
56 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
57 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
59 /* Set the initial value */
60 tps65910_gpio_set(gc, offset, value);
62 return regmap_set_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
63 GPIO_CFG_MASK);
66 static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset)
68 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
69 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
71 return regmap_clear_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
72 GPIO_CFG_MASK);
75 #ifdef CONFIG_OF
76 static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
77 struct tps65910 *tps65910, int chip_ngpio)
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;
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;
93 for (idx = 0; idx < ngpio; idx++)
94 tps65910_board->en_gpio_sleep[idx] = (prop_array[idx] != 0);
96 return tps65910_board;
98 #else
99 static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
100 struct tps65910 *tps65910, int chip_ngpio)
102 return NULL;
104 #endif
106 static int tps65910_gpio_probe(struct platform_device *pdev)
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;
114 tps65910_gpio = devm_kzalloc(&pdev->dev,
115 sizeof(*tps65910_gpio), GFP_KERNEL);
116 if (!tps65910_gpio)
117 return -ENOMEM;
119 tps65910_gpio->tps65910 = tps65910;
121 tps65910_gpio->gpio_chip.owner = THIS_MODULE;
122 tps65910_gpio->gpio_chip.label = tps65910->i2c_client->name;
124 switch (tps65910_chip_id(tps65910)) {
125 case TPS65910:
126 tps65910_gpio->gpio_chip.ngpio = TPS65910_NUM_GPIO;
127 break;
128 case TPS65911:
129 tps65910_gpio->gpio_chip.ngpio = TPS65911_NUM_GPIO;
130 break;
131 default:
132 return -EINVAL;
134 tps65910_gpio->gpio_chip.can_sleep = true;
135 tps65910_gpio->gpio_chip.direction_input = tps65910_gpio_input;
136 tps65910_gpio->gpio_chip.direction_output = tps65910_gpio_output;
137 tps65910_gpio->gpio_chip.set = tps65910_gpio_set;
138 tps65910_gpio->gpio_chip.get = tps65910_gpio_get;
139 tps65910_gpio->gpio_chip.parent = &pdev->dev;
140 #ifdef CONFIG_OF_GPIO
141 tps65910_gpio->gpio_chip.of_node = tps65910->dev->of_node;
142 #endif
143 if (pdata && pdata->gpio_base)
144 tps65910_gpio->gpio_chip.base = pdata->gpio_base;
145 else
146 tps65910_gpio->gpio_chip.base = -1;
148 if (!pdata && tps65910->dev->of_node)
149 pdata = tps65910_parse_dt_for_gpio(&pdev->dev, tps65910,
150 tps65910_gpio->gpio_chip.ngpio);
152 if (!pdata)
153 goto skip_init;
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;
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);
167 skip_init:
168 ret = devm_gpiochip_add_data(&pdev->dev, &tps65910_gpio->gpio_chip,
169 tps65910_gpio);
170 if (ret < 0) {
171 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
172 return ret;
175 platform_set_drvdata(pdev, tps65910_gpio);
177 return ret;
180 static struct platform_driver tps65910_gpio_driver = {
181 .driver.name = "tps65910-gpio",
182 .probe = tps65910_gpio_probe,
185 static int __init tps65910_gpio_init(void)
187 return platform_driver_register(&tps65910_gpio_driver);
189 subsys_initcall(tps65910_gpio_init);