1d5a4da15SLinus Walleij // SPDX-License-Identifier: GPL-2.0+
2c103de24SGrant Likely /*
3c103de24SGrant Likely * TI TPS6591x GPIO driver
4c103de24SGrant Likely *
5c103de24SGrant Likely * Copyright 2010 Texas Instruments Inc.
6c103de24SGrant Likely *
7c103de24SGrant Likely * Author: Graeme Gregory <gg@slimlogic.co.uk>
802c7a13eSPaul Gortmaker * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
9c103de24SGrant Likely */
10c103de24SGrant Likely
11c103de24SGrant Likely #include <linux/kernel.h>
1202c7a13eSPaul Gortmaker #include <linux/init.h>
13c103de24SGrant Likely #include <linux/errno.h>
145d75683eSLinus Walleij #include <linux/gpio/driver.h>
15c103de24SGrant Likely #include <linux/i2c.h>
1610bbc48dSLaxman Dewangan #include <linux/platform_device.h>
17c103de24SGrant Likely #include <linux/mfd/tps65910.h>
18*e91d0f05SRob Herring #include <linux/of.h>
19c103de24SGrant Likely
2010bbc48dSLaxman Dewangan struct tps65910_gpio {
2110bbc48dSLaxman Dewangan struct gpio_chip gpio_chip;
2210bbc48dSLaxman Dewangan struct tps65910 *tps65910;
2310bbc48dSLaxman Dewangan };
2410bbc48dSLaxman Dewangan
tps65910_gpio_get(struct gpio_chip * gc,unsigned offset)25c103de24SGrant Likely static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset)
26c103de24SGrant Likely {
27b7c17b1bSLinus Walleij struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
2810bbc48dSLaxman Dewangan struct tps65910 *tps65910 = tps65910_gpio->tps65910;
293f7e8275SRhyland Klein unsigned int val;
30c103de24SGrant Likely
31a233d4ebSMichał Mirosław regmap_read(tps65910->regmap, TPS65910_GPIO0 + offset, &val);
32c103de24SGrant Likely
33c103de24SGrant Likely if (val & GPIO_STS_MASK)
34c103de24SGrant Likely return 1;
35c103de24SGrant Likely
36c103de24SGrant Likely return 0;
37c103de24SGrant Likely }
38c103de24SGrant Likely
tps65910_gpio_set(struct gpio_chip * gc,unsigned offset,int value)39c103de24SGrant Likely static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset,
40c103de24SGrant Likely int value)
41c103de24SGrant Likely {
42b7c17b1bSLinus Walleij struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
4310bbc48dSLaxman Dewangan struct tps65910 *tps65910 = tps65910_gpio->tps65910;
44c103de24SGrant Likely
45c103de24SGrant Likely if (value)
46a233d4ebSMichał Mirosław regmap_set_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
47c103de24SGrant Likely GPIO_SET_MASK);
48c103de24SGrant Likely else
49a233d4ebSMichał Mirosław regmap_clear_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
50c103de24SGrant Likely GPIO_SET_MASK);
51c103de24SGrant Likely }
52c103de24SGrant Likely
tps65910_gpio_output(struct gpio_chip * gc,unsigned offset,int value)53c103de24SGrant Likely static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset,
54c103de24SGrant Likely int value)
55c103de24SGrant Likely {
56b7c17b1bSLinus Walleij struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
5710bbc48dSLaxman Dewangan struct tps65910 *tps65910 = tps65910_gpio->tps65910;
58c103de24SGrant Likely
59c103de24SGrant Likely /* Set the initial value */
6094bd2442SLaxman Dewangan tps65910_gpio_set(gc, offset, value);
61c103de24SGrant Likely
62a233d4ebSMichał Mirosław return regmap_set_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
63c103de24SGrant Likely GPIO_CFG_MASK);
64c103de24SGrant Likely }
65c103de24SGrant Likely
tps65910_gpio_input(struct gpio_chip * gc,unsigned offset)66c103de24SGrant Likely static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset)
67c103de24SGrant Likely {
68b7c17b1bSLinus Walleij struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
6910bbc48dSLaxman Dewangan struct tps65910 *tps65910 = tps65910_gpio->tps65910;
70c103de24SGrant Likely
71a233d4ebSMichał Mirosław return regmap_clear_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
72c103de24SGrant Likely GPIO_CFG_MASK);
73c103de24SGrant Likely }
74c103de24SGrant Likely
756fe02e9fSLaxman Dewangan #ifdef CONFIG_OF
tps65910_parse_dt_for_gpio(struct device * dev,struct tps65910 * tps65910,int chip_ngpio)766fe02e9fSLaxman Dewangan static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
776fe02e9fSLaxman Dewangan struct tps65910 *tps65910, int chip_ngpio)
786fe02e9fSLaxman Dewangan {
796fe02e9fSLaxman Dewangan struct tps65910_board *tps65910_board = tps65910->of_plat_data;
806fe02e9fSLaxman Dewangan unsigned int prop_array[TPS6591X_MAX_NUM_GPIO];
816fe02e9fSLaxman Dewangan int ngpio = min(chip_ngpio, TPS6591X_MAX_NUM_GPIO);
826fe02e9fSLaxman Dewangan int ret;
836fe02e9fSLaxman Dewangan int idx;
846fe02e9fSLaxman Dewangan
856fe02e9fSLaxman Dewangan tps65910_board->gpio_base = -1;
866fe02e9fSLaxman Dewangan ret = of_property_read_u32_array(tps65910->dev->of_node,
876fe02e9fSLaxman Dewangan "ti,en-gpio-sleep", prop_array, ngpio);
886fe02e9fSLaxman Dewangan if (ret < 0) {
896fe02e9fSLaxman Dewangan dev_dbg(dev, "ti,en-gpio-sleep not specified\n");
906fe02e9fSLaxman Dewangan return tps65910_board;
916fe02e9fSLaxman Dewangan }
926fe02e9fSLaxman Dewangan
936fe02e9fSLaxman Dewangan for (idx = 0; idx < ngpio; idx++)
946fe02e9fSLaxman Dewangan tps65910_board->en_gpio_sleep[idx] = (prop_array[idx] != 0);
956fe02e9fSLaxman Dewangan
966fe02e9fSLaxman Dewangan return tps65910_board;
976fe02e9fSLaxman Dewangan }
986fe02e9fSLaxman Dewangan #else
tps65910_parse_dt_for_gpio(struct device * dev,struct tps65910 * tps65910,int chip_ngpio)996fe02e9fSLaxman Dewangan static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
1006fe02e9fSLaxman Dewangan struct tps65910 *tps65910, int chip_ngpio)
1016fe02e9fSLaxman Dewangan {
1026fe02e9fSLaxman Dewangan return NULL;
1036fe02e9fSLaxman Dewangan }
1046fe02e9fSLaxman Dewangan #endif
1056fe02e9fSLaxman Dewangan
tps65910_gpio_probe(struct platform_device * pdev)1063836309dSBill Pemberton static int tps65910_gpio_probe(struct platform_device *pdev)
107c103de24SGrant Likely {
10810bbc48dSLaxman Dewangan struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
10910bbc48dSLaxman Dewangan struct tps65910_board *pdata = dev_get_platdata(tps65910->dev);
11010bbc48dSLaxman Dewangan struct tps65910_gpio *tps65910_gpio;
111c103de24SGrant Likely int ret;
11210bbc48dSLaxman Dewangan int i;
113c103de24SGrant Likely
1146dbe6c07SAndy Shevchenko device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
1156dbe6c07SAndy Shevchenko
11610bbc48dSLaxman Dewangan tps65910_gpio = devm_kzalloc(&pdev->dev,
11710bbc48dSLaxman Dewangan sizeof(*tps65910_gpio), GFP_KERNEL);
1180661175aSJingoo Han if (!tps65910_gpio)
11910bbc48dSLaxman Dewangan return -ENOMEM;
120c103de24SGrant Likely
12110bbc48dSLaxman Dewangan tps65910_gpio->tps65910 = tps65910;
12210bbc48dSLaxman Dewangan
12310bbc48dSLaxman Dewangan tps65910_gpio->gpio_chip.owner = THIS_MODULE;
12410bbc48dSLaxman Dewangan tps65910_gpio->gpio_chip.label = tps65910->i2c_client->name;
125c103de24SGrant Likely
126c103de24SGrant Likely switch (tps65910_chip_id(tps65910)) {
127c103de24SGrant Likely case TPS65910:
12810bbc48dSLaxman Dewangan tps65910_gpio->gpio_chip.ngpio = TPS65910_NUM_GPIO;
129c7c85184SLinus Torvalds break;
130c103de24SGrant Likely case TPS65911:
13110bbc48dSLaxman Dewangan tps65910_gpio->gpio_chip.ngpio = TPS65911_NUM_GPIO;
132c7c85184SLinus Torvalds break;
133c103de24SGrant Likely default:
13410bbc48dSLaxman Dewangan return -EINVAL;
135c103de24SGrant Likely }
1369fb1f39eSLinus Walleij tps65910_gpio->gpio_chip.can_sleep = true;
13710bbc48dSLaxman Dewangan tps65910_gpio->gpio_chip.direction_input = tps65910_gpio_input;
13810bbc48dSLaxman Dewangan tps65910_gpio->gpio_chip.direction_output = tps65910_gpio_output;
13910bbc48dSLaxman Dewangan tps65910_gpio->gpio_chip.set = tps65910_gpio_set;
14010bbc48dSLaxman Dewangan tps65910_gpio->gpio_chip.get = tps65910_gpio_get;
14158383c78SLinus Walleij tps65910_gpio->gpio_chip.parent = &pdev->dev;
1426dbe6c07SAndy Shevchenko
14310bbc48dSLaxman Dewangan if (pdata && pdata->gpio_base)
14410bbc48dSLaxman Dewangan tps65910_gpio->gpio_chip.base = pdata->gpio_base;
14510bbc48dSLaxman Dewangan else
14610bbc48dSLaxman Dewangan tps65910_gpio->gpio_chip.base = -1;
147c103de24SGrant Likely
1486fe02e9fSLaxman Dewangan if (!pdata && tps65910->dev->of_node)
1496fe02e9fSLaxman Dewangan pdata = tps65910_parse_dt_for_gpio(&pdev->dev, tps65910,
1506fe02e9fSLaxman Dewangan tps65910_gpio->gpio_chip.ngpio);
1516fe02e9fSLaxman Dewangan
15210bbc48dSLaxman Dewangan if (!pdata)
15310bbc48dSLaxman Dewangan goto skip_init;
154c103de24SGrant Likely
15510bbc48dSLaxman Dewangan /* Configure sleep control for gpios if provided */
15610bbc48dSLaxman Dewangan for (i = 0; i < tps65910_gpio->gpio_chip.ngpio; ++i) {
15710bbc48dSLaxman Dewangan if (!pdata->en_gpio_sleep[i])
15810bbc48dSLaxman Dewangan continue;
15910bbc48dSLaxman Dewangan
160a233d4ebSMichał Mirosław ret = regmap_set_bits(tps65910->regmap,
1619467d298SLaxman Dewangan TPS65910_GPIO0 + i, GPIO_SLEEP_MASK);
1629467d298SLaxman Dewangan if (ret < 0)
1639467d298SLaxman Dewangan dev_warn(tps65910->dev,
16410bbc48dSLaxman Dewangan "GPIO Sleep setting failed with err %d\n", ret);
1659467d298SLaxman Dewangan }
1669467d298SLaxman Dewangan
16710bbc48dSLaxman Dewangan skip_init:
16896a03e86SAlexandru Ardelean return devm_gpiochip_add_data(&pdev->dev, &tps65910_gpio->gpio_chip,
169bf6e855aSLaxman Dewangan tps65910_gpio);
17010bbc48dSLaxman Dewangan }
17110bbc48dSLaxman Dewangan
17210bbc48dSLaxman Dewangan static struct platform_driver tps65910_gpio_driver = {
17310bbc48dSLaxman Dewangan .driver.name = "tps65910-gpio",
17410bbc48dSLaxman Dewangan .probe = tps65910_gpio_probe,
17510bbc48dSLaxman Dewangan };
17610bbc48dSLaxman Dewangan
tps65910_gpio_init(void)17710bbc48dSLaxman Dewangan static int __init tps65910_gpio_init(void)
17810bbc48dSLaxman Dewangan {
17910bbc48dSLaxman Dewangan return platform_driver_register(&tps65910_gpio_driver);
18010bbc48dSLaxman Dewangan }
18110bbc48dSLaxman Dewangan subsys_initcall(tps65910_gpio_init);
182