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