162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2015-2023 Texas Instruments Incorporated - https://www.ti.com/
462306a36Sopenharmony_ci *	Andrew Davis <afd@ti.com>
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Based on the TPS65912 driver
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/gpio/driver.h>
1062306a36Sopenharmony_ci#include <linux/module.h>
1162306a36Sopenharmony_ci#include <linux/platform_device.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/mfd/tps65086.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_cistruct tps65086_gpio {
1662306a36Sopenharmony_ci	struct gpio_chip chip;
1762306a36Sopenharmony_ci	struct tps65086 *tps;
1862306a36Sopenharmony_ci};
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_cistatic int tps65086_gpio_get_direction(struct gpio_chip *chip,
2162306a36Sopenharmony_ci				       unsigned offset)
2262306a36Sopenharmony_ci{
2362306a36Sopenharmony_ci	/* This device is output only */
2462306a36Sopenharmony_ci	return GPIO_LINE_DIRECTION_OUT;
2562306a36Sopenharmony_ci}
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cistatic int tps65086_gpio_direction_input(struct gpio_chip *chip,
2862306a36Sopenharmony_ci					 unsigned offset)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	/* This device is output only */
3162306a36Sopenharmony_ci	return -EINVAL;
3262306a36Sopenharmony_ci}
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_cistatic int tps65086_gpio_direction_output(struct gpio_chip *chip,
3562306a36Sopenharmony_ci					  unsigned offset, int value)
3662306a36Sopenharmony_ci{
3762306a36Sopenharmony_ci	struct tps65086_gpio *gpio = gpiochip_get_data(chip);
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	/* Set the initial value */
4062306a36Sopenharmony_ci	regmap_update_bits(gpio->tps->regmap, TPS65086_GPOCTRL,
4162306a36Sopenharmony_ci			   BIT(4 + offset), value ? BIT(4 + offset) : 0);
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	return 0;
4462306a36Sopenharmony_ci}
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistatic int tps65086_gpio_get(struct gpio_chip *chip, unsigned offset)
4762306a36Sopenharmony_ci{
4862306a36Sopenharmony_ci	struct tps65086_gpio *gpio = gpiochip_get_data(chip);
4962306a36Sopenharmony_ci	int ret, val;
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	ret = regmap_read(gpio->tps->regmap, TPS65086_GPOCTRL, &val);
5262306a36Sopenharmony_ci	if (ret < 0)
5362306a36Sopenharmony_ci		return ret;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	return val & BIT(4 + offset);
5662306a36Sopenharmony_ci}
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_cistatic void tps65086_gpio_set(struct gpio_chip *chip, unsigned offset,
5962306a36Sopenharmony_ci			      int value)
6062306a36Sopenharmony_ci{
6162306a36Sopenharmony_ci	struct tps65086_gpio *gpio = gpiochip_get_data(chip);
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	regmap_update_bits(gpio->tps->regmap, TPS65086_GPOCTRL,
6462306a36Sopenharmony_ci			   BIT(4 + offset), value ? BIT(4 + offset) : 0);
6562306a36Sopenharmony_ci}
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_cistatic const struct gpio_chip template_chip = {
6862306a36Sopenharmony_ci	.label			= "tps65086-gpio",
6962306a36Sopenharmony_ci	.owner			= THIS_MODULE,
7062306a36Sopenharmony_ci	.get_direction		= tps65086_gpio_get_direction,
7162306a36Sopenharmony_ci	.direction_input	= tps65086_gpio_direction_input,
7262306a36Sopenharmony_ci	.direction_output	= tps65086_gpio_direction_output,
7362306a36Sopenharmony_ci	.get			= tps65086_gpio_get,
7462306a36Sopenharmony_ci	.set			= tps65086_gpio_set,
7562306a36Sopenharmony_ci	.base			= -1,
7662306a36Sopenharmony_ci	.ngpio			= 4,
7762306a36Sopenharmony_ci	.can_sleep		= true,
7862306a36Sopenharmony_ci};
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cistatic int tps65086_gpio_probe(struct platform_device *pdev)
8162306a36Sopenharmony_ci{
8262306a36Sopenharmony_ci	struct tps65086_gpio *gpio;
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
8562306a36Sopenharmony_ci	if (!gpio)
8662306a36Sopenharmony_ci		return -ENOMEM;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	gpio->tps = dev_get_drvdata(pdev->dev.parent);
8962306a36Sopenharmony_ci	gpio->chip = template_chip;
9062306a36Sopenharmony_ci	gpio->chip.parent = gpio->tps->dev;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	return devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
9362306a36Sopenharmony_ci}
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_cistatic const struct platform_device_id tps65086_gpio_id_table[] = {
9662306a36Sopenharmony_ci	{ "tps65086-gpio", },
9762306a36Sopenharmony_ci	{ /* sentinel */ }
9862306a36Sopenharmony_ci};
9962306a36Sopenharmony_ciMODULE_DEVICE_TABLE(platform, tps65086_gpio_id_table);
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_cistatic struct platform_driver tps65086_gpio_driver = {
10262306a36Sopenharmony_ci	.driver = {
10362306a36Sopenharmony_ci		.name = "tps65086-gpio",
10462306a36Sopenharmony_ci	},
10562306a36Sopenharmony_ci	.probe = tps65086_gpio_probe,
10662306a36Sopenharmony_ci	.id_table = tps65086_gpio_id_table,
10762306a36Sopenharmony_ci};
10862306a36Sopenharmony_cimodule_platform_driver(tps65086_gpio_driver);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ciMODULE_AUTHOR("Andrew Davis <afd@ti.com>");
11162306a36Sopenharmony_ciMODULE_DESCRIPTION("TPS65086 GPIO driver");
11262306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
113