162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * GPIO driver for TI TPS65219 PMICs 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2022 Texas Instruments Incorporated - http://www.ti.com/ 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#include <linux/bits.h> 962306a36Sopenharmony_ci#include <linux/gpio/driver.h> 1062306a36Sopenharmony_ci#include <linux/mfd/tps65219.h> 1162306a36Sopenharmony_ci#include <linux/module.h> 1262306a36Sopenharmony_ci#include <linux/platform_device.h> 1362306a36Sopenharmony_ci#include <linux/regmap.h> 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#define TPS65219_GPIO0_DIR_MASK BIT(3) 1662306a36Sopenharmony_ci#define TPS65219_GPIO0_OFFSET 2 1762306a36Sopenharmony_ci#define TPS65219_GPIO0_IDX 0 1862306a36Sopenharmony_ci#define TPS65219_GPIO_DIR_IN 1 1962306a36Sopenharmony_ci#define TPS65219_GPIO_DIR_OUT 0 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_cistruct tps65219_gpio { 2262306a36Sopenharmony_ci struct gpio_chip gpio_chip; 2362306a36Sopenharmony_ci struct tps65219 *tps; 2462306a36Sopenharmony_ci}; 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_cistatic int tps65219_gpio_get_direction(struct gpio_chip *gc, unsigned int offset) 2762306a36Sopenharmony_ci{ 2862306a36Sopenharmony_ci struct tps65219_gpio *gpio = gpiochip_get_data(gc); 2962306a36Sopenharmony_ci int ret, val; 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci if (offset != TPS65219_GPIO0_IDX) 3262306a36Sopenharmony_ci return GPIO_LINE_DIRECTION_OUT; 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci ret = regmap_read(gpio->tps->regmap, TPS65219_REG_MFP_1_CONFIG, &val); 3562306a36Sopenharmony_ci if (ret) 3662306a36Sopenharmony_ci return ret; 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci return !!(val & TPS65219_GPIO0_DIR_MASK); 3962306a36Sopenharmony_ci} 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_cistatic int tps65219_gpio_get(struct gpio_chip *gc, unsigned int offset) 4262306a36Sopenharmony_ci{ 4362306a36Sopenharmony_ci struct tps65219_gpio *gpio = gpiochip_get_data(gc); 4462306a36Sopenharmony_ci struct device *dev = gpio->tps->dev; 4562306a36Sopenharmony_ci int ret, val; 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci if (offset != TPS65219_GPIO0_IDX) { 4862306a36Sopenharmony_ci dev_err(dev, "GPIO%d is output only, cannot get\n", offset); 4962306a36Sopenharmony_ci return -ENOTSUPP; 5062306a36Sopenharmony_ci } 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci ret = regmap_read(gpio->tps->regmap, TPS65219_REG_MFP_CTRL, &val); 5362306a36Sopenharmony_ci if (ret) 5462306a36Sopenharmony_ci return ret; 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci ret = !!(val & BIT(TPS65219_MFP_GPIO_STATUS_MASK)); 5762306a36Sopenharmony_ci dev_warn(dev, "GPIO%d = %d, MULTI_DEVICE_ENABLE, not a standard GPIO\n", offset, ret); 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci /* 6062306a36Sopenharmony_ci * Depending on NVM config, return an error if direction is output, otherwise the GPIO0 6162306a36Sopenharmony_ci * status bit. 6262306a36Sopenharmony_ci */ 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci if (tps65219_gpio_get_direction(gc, offset) == TPS65219_GPIO_DIR_OUT) 6562306a36Sopenharmony_ci return -ENOTSUPP; 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci return ret; 6862306a36Sopenharmony_ci} 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_cistatic void tps65219_gpio_set(struct gpio_chip *gc, unsigned int offset, int value) 7162306a36Sopenharmony_ci{ 7262306a36Sopenharmony_ci struct tps65219_gpio *gpio = gpiochip_get_data(gc); 7362306a36Sopenharmony_ci struct device *dev = gpio->tps->dev; 7462306a36Sopenharmony_ci int v, mask, bit; 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci bit = (offset == TPS65219_GPIO0_IDX) ? TPS65219_GPIO0_OFFSET : offset - 1; 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci mask = BIT(bit); 7962306a36Sopenharmony_ci v = value ? mask : 0; 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci if (regmap_update_bits(gpio->tps->regmap, TPS65219_REG_GENERAL_CONFIG, mask, v)) 8262306a36Sopenharmony_ci dev_err(dev, "GPIO%d, set to value %d failed.\n", offset, value); 8362306a36Sopenharmony_ci} 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_cistatic int tps65219_gpio_change_direction(struct gpio_chip *gc, unsigned int offset, 8662306a36Sopenharmony_ci unsigned int direction) 8762306a36Sopenharmony_ci{ 8862306a36Sopenharmony_ci struct tps65219_gpio *gpio = gpiochip_get_data(gc); 8962306a36Sopenharmony_ci struct device *dev = gpio->tps->dev; 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci /* 9262306a36Sopenharmony_ci * Documentation is stating that GPIO0 direction must not be changed in Linux: 9362306a36Sopenharmony_ci * Table 8-34. MFP_1_CONFIG(3): MULTI_DEVICE_ENABLE, should only be changed in INITIALIZE 9462306a36Sopenharmony_ci * state (prior to ON Request). 9562306a36Sopenharmony_ci * Set statically by NVM, changing direction in application can cause a hang. 9662306a36Sopenharmony_ci * Below can be used for test purpose only. 9762306a36Sopenharmony_ci */ 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_DEBUG_GPIO)) { 10062306a36Sopenharmony_ci int ret = regmap_update_bits(gpio->tps->regmap, TPS65219_REG_MFP_1_CONFIG, 10162306a36Sopenharmony_ci TPS65219_GPIO0_DIR_MASK, direction); 10262306a36Sopenharmony_ci if (ret) { 10362306a36Sopenharmony_ci dev_err(dev, 10462306a36Sopenharmony_ci "GPIO DEBUG enabled: Fail to change direction to %u for GPIO%d.\n", 10562306a36Sopenharmony_ci direction, offset); 10662306a36Sopenharmony_ci return ret; 10762306a36Sopenharmony_ci } 10862306a36Sopenharmony_ci } 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci dev_err(dev, 11162306a36Sopenharmony_ci "GPIO%d direction set by NVM, change to %u failed, not allowed by specification\n", 11262306a36Sopenharmony_ci offset, direction); 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci return -ENOTSUPP; 11562306a36Sopenharmony_ci} 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_cistatic int tps65219_gpio_direction_input(struct gpio_chip *gc, unsigned int offset) 11862306a36Sopenharmony_ci{ 11962306a36Sopenharmony_ci struct tps65219_gpio *gpio = gpiochip_get_data(gc); 12062306a36Sopenharmony_ci struct device *dev = gpio->tps->dev; 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci if (offset != TPS65219_GPIO0_IDX) { 12362306a36Sopenharmony_ci dev_err(dev, "GPIO%d is output only, cannot change to input\n", offset); 12462306a36Sopenharmony_ci return -ENOTSUPP; 12562306a36Sopenharmony_ci } 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci if (tps65219_gpio_get_direction(gc, offset) == TPS65219_GPIO_DIR_IN) 12862306a36Sopenharmony_ci return 0; 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci return tps65219_gpio_change_direction(gc, offset, TPS65219_GPIO_DIR_IN); 13162306a36Sopenharmony_ci} 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_cistatic int tps65219_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value) 13462306a36Sopenharmony_ci{ 13562306a36Sopenharmony_ci tps65219_gpio_set(gc, offset, value); 13662306a36Sopenharmony_ci if (offset != TPS65219_GPIO0_IDX) 13762306a36Sopenharmony_ci return 0; 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci if (tps65219_gpio_get_direction(gc, offset) == TPS65219_GPIO_DIR_OUT) 14062306a36Sopenharmony_ci return 0; 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci return tps65219_gpio_change_direction(gc, offset, TPS65219_GPIO_DIR_OUT); 14362306a36Sopenharmony_ci} 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_cistatic const struct gpio_chip tps65219_template_chip = { 14662306a36Sopenharmony_ci .label = "tps65219-gpio", 14762306a36Sopenharmony_ci .owner = THIS_MODULE, 14862306a36Sopenharmony_ci .get_direction = tps65219_gpio_get_direction, 14962306a36Sopenharmony_ci .direction_input = tps65219_gpio_direction_input, 15062306a36Sopenharmony_ci .direction_output = tps65219_gpio_direction_output, 15162306a36Sopenharmony_ci .get = tps65219_gpio_get, 15262306a36Sopenharmony_ci .set = tps65219_gpio_set, 15362306a36Sopenharmony_ci .base = -1, 15462306a36Sopenharmony_ci .ngpio = 3, 15562306a36Sopenharmony_ci .can_sleep = true, 15662306a36Sopenharmony_ci}; 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_cistatic int tps65219_gpio_probe(struct platform_device *pdev) 15962306a36Sopenharmony_ci{ 16062306a36Sopenharmony_ci struct tps65219 *tps = dev_get_drvdata(pdev->dev.parent); 16162306a36Sopenharmony_ci struct tps65219_gpio *gpio; 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL); 16462306a36Sopenharmony_ci if (!gpio) 16562306a36Sopenharmony_ci return -ENOMEM; 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci gpio->tps = tps; 16862306a36Sopenharmony_ci gpio->gpio_chip = tps65219_template_chip; 16962306a36Sopenharmony_ci gpio->gpio_chip.parent = tps->dev; 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci return devm_gpiochip_add_data(&pdev->dev, &gpio->gpio_chip, gpio); 17262306a36Sopenharmony_ci} 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_cistatic struct platform_driver tps65219_gpio_driver = { 17562306a36Sopenharmony_ci .driver = { 17662306a36Sopenharmony_ci .name = "tps65219-gpio", 17762306a36Sopenharmony_ci }, 17862306a36Sopenharmony_ci .probe = tps65219_gpio_probe, 17962306a36Sopenharmony_ci}; 18062306a36Sopenharmony_cimodule_platform_driver(tps65219_gpio_driver); 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ciMODULE_ALIAS("platform:tps65219-gpio"); 18362306a36Sopenharmony_ciMODULE_AUTHOR("Jonathan Cormier <jcormier@criticallink.com>"); 18462306a36Sopenharmony_ciMODULE_DESCRIPTION("TPS65219 GPIO driver"); 18562306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 186