162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * GPIO driver for TPS68470 PMIC
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2017 Intel Corporation
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Authors:
862306a36Sopenharmony_ci *	Antti Laakso <antti.laakso@intel.com>
962306a36Sopenharmony_ci *	Tianshu Qiu <tian.shu.qiu@intel.com>
1062306a36Sopenharmony_ci *	Jian Xu Zheng <jian.xu.zheng@intel.com>
1162306a36Sopenharmony_ci *	Yuning Pu <yuning.pu@intel.com>
1262306a36Sopenharmony_ci */
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/gpio/driver.h>
1562306a36Sopenharmony_ci#include <linux/mfd/tps68470.h>
1662306a36Sopenharmony_ci#include <linux/module.h>
1762306a36Sopenharmony_ci#include <linux/platform_device.h>
1862306a36Sopenharmony_ci#include <linux/regmap.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define TPS68470_N_LOGIC_OUTPUT	3
2162306a36Sopenharmony_ci#define TPS68470_N_REGULAR_GPIO	7
2262306a36Sopenharmony_ci#define TPS68470_N_GPIO	(TPS68470_N_LOGIC_OUTPUT + TPS68470_N_REGULAR_GPIO)
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistruct tps68470_gpio_data {
2562306a36Sopenharmony_ci	struct regmap *tps68470_regmap;
2662306a36Sopenharmony_ci	struct gpio_chip gc;
2762306a36Sopenharmony_ci};
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_cistatic int tps68470_gpio_get(struct gpio_chip *gc, unsigned int offset)
3062306a36Sopenharmony_ci{
3162306a36Sopenharmony_ci	struct tps68470_gpio_data *tps68470_gpio = gpiochip_get_data(gc);
3262306a36Sopenharmony_ci	struct regmap *regmap = tps68470_gpio->tps68470_regmap;
3362306a36Sopenharmony_ci	unsigned int reg = TPS68470_REG_GPDO;
3462306a36Sopenharmony_ci	int val, ret;
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci	if (offset >= TPS68470_N_REGULAR_GPIO) {
3762306a36Sopenharmony_ci		offset -= TPS68470_N_REGULAR_GPIO;
3862306a36Sopenharmony_ci		reg = TPS68470_REG_SGPO;
3962306a36Sopenharmony_ci	}
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	ret = regmap_read(regmap, reg, &val);
4262306a36Sopenharmony_ci	if (ret) {
4362306a36Sopenharmony_ci		dev_err(tps68470_gpio->gc.parent, "reg 0x%x read failed\n",
4462306a36Sopenharmony_ci			TPS68470_REG_SGPO);
4562306a36Sopenharmony_ci		return ret;
4662306a36Sopenharmony_ci	}
4762306a36Sopenharmony_ci	return !!(val & BIT(offset));
4862306a36Sopenharmony_ci}
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cistatic int tps68470_gpio_get_direction(struct gpio_chip *gc,
5162306a36Sopenharmony_ci				       unsigned int offset)
5262306a36Sopenharmony_ci{
5362306a36Sopenharmony_ci	struct tps68470_gpio_data *tps68470_gpio = gpiochip_get_data(gc);
5462306a36Sopenharmony_ci	struct regmap *regmap = tps68470_gpio->tps68470_regmap;
5562306a36Sopenharmony_ci	int val, ret;
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	/* rest are always outputs */
5862306a36Sopenharmony_ci	if (offset >= TPS68470_N_REGULAR_GPIO)
5962306a36Sopenharmony_ci		return GPIO_LINE_DIRECTION_OUT;
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	ret = regmap_read(regmap, TPS68470_GPIO_CTL_REG_A(offset), &val);
6262306a36Sopenharmony_ci	if (ret) {
6362306a36Sopenharmony_ci		dev_err(tps68470_gpio->gc.parent, "reg 0x%x read failed\n",
6462306a36Sopenharmony_ci			TPS68470_GPIO_CTL_REG_A(offset));
6562306a36Sopenharmony_ci		return ret;
6662306a36Sopenharmony_ci	}
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	val &= TPS68470_GPIO_MODE_MASK;
6962306a36Sopenharmony_ci	return val >= TPS68470_GPIO_MODE_OUT_CMOS ? GPIO_LINE_DIRECTION_OUT :
7062306a36Sopenharmony_ci						    GPIO_LINE_DIRECTION_IN;
7162306a36Sopenharmony_ci}
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_cistatic void tps68470_gpio_set(struct gpio_chip *gc, unsigned int offset,
7462306a36Sopenharmony_ci				int value)
7562306a36Sopenharmony_ci{
7662306a36Sopenharmony_ci	struct tps68470_gpio_data *tps68470_gpio = gpiochip_get_data(gc);
7762306a36Sopenharmony_ci	struct regmap *regmap = tps68470_gpio->tps68470_regmap;
7862306a36Sopenharmony_ci	unsigned int reg = TPS68470_REG_GPDO;
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	if (offset >= TPS68470_N_REGULAR_GPIO) {
8162306a36Sopenharmony_ci		reg = TPS68470_REG_SGPO;
8262306a36Sopenharmony_ci		offset -= TPS68470_N_REGULAR_GPIO;
8362306a36Sopenharmony_ci	}
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	regmap_update_bits(regmap, reg, BIT(offset), value ? BIT(offset) : 0);
8662306a36Sopenharmony_ci}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_cistatic int tps68470_gpio_output(struct gpio_chip *gc, unsigned int offset,
8962306a36Sopenharmony_ci				int value)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	struct tps68470_gpio_data *tps68470_gpio = gpiochip_get_data(gc);
9262306a36Sopenharmony_ci	struct regmap *regmap = tps68470_gpio->tps68470_regmap;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	/* Set the initial value */
9562306a36Sopenharmony_ci	tps68470_gpio_set(gc, offset, value);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	/* rest are always outputs */
9862306a36Sopenharmony_ci	if (offset >= TPS68470_N_REGULAR_GPIO)
9962306a36Sopenharmony_ci		return 0;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	return regmap_update_bits(regmap, TPS68470_GPIO_CTL_REG_A(offset),
10262306a36Sopenharmony_ci				 TPS68470_GPIO_MODE_MASK,
10362306a36Sopenharmony_ci				 TPS68470_GPIO_MODE_OUT_CMOS);
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cistatic int tps68470_gpio_input(struct gpio_chip *gc, unsigned int offset)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	struct tps68470_gpio_data *tps68470_gpio = gpiochip_get_data(gc);
10962306a36Sopenharmony_ci	struct regmap *regmap = tps68470_gpio->tps68470_regmap;
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	/* rest are always outputs */
11262306a36Sopenharmony_ci	if (offset >= TPS68470_N_REGULAR_GPIO)
11362306a36Sopenharmony_ci		return -EINVAL;
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	return regmap_update_bits(regmap, TPS68470_GPIO_CTL_REG_A(offset),
11662306a36Sopenharmony_ci				   TPS68470_GPIO_MODE_MASK, 0x00);
11762306a36Sopenharmony_ci}
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_cistatic const char *tps68470_names[TPS68470_N_GPIO] = {
12062306a36Sopenharmony_ci	"gpio.0", "gpio.1", "gpio.2", "gpio.3",
12162306a36Sopenharmony_ci	"gpio.4", "gpio.5", "gpio.6",
12262306a36Sopenharmony_ci	"s_enable", "s_idle", "s_resetn",
12362306a36Sopenharmony_ci};
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_cistatic int tps68470_gpio_probe(struct platform_device *pdev)
12662306a36Sopenharmony_ci{
12762306a36Sopenharmony_ci	struct tps68470_gpio_data *tps68470_gpio;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	tps68470_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps68470_gpio),
13062306a36Sopenharmony_ci				     GFP_KERNEL);
13162306a36Sopenharmony_ci	if (!tps68470_gpio)
13262306a36Sopenharmony_ci		return -ENOMEM;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	tps68470_gpio->tps68470_regmap = dev_get_drvdata(pdev->dev.parent);
13562306a36Sopenharmony_ci	tps68470_gpio->gc.label = "tps68470-gpio";
13662306a36Sopenharmony_ci	tps68470_gpio->gc.owner = THIS_MODULE;
13762306a36Sopenharmony_ci	tps68470_gpio->gc.direction_input = tps68470_gpio_input;
13862306a36Sopenharmony_ci	tps68470_gpio->gc.direction_output = tps68470_gpio_output;
13962306a36Sopenharmony_ci	tps68470_gpio->gc.get = tps68470_gpio_get;
14062306a36Sopenharmony_ci	tps68470_gpio->gc.get_direction = tps68470_gpio_get_direction;
14162306a36Sopenharmony_ci	tps68470_gpio->gc.set = tps68470_gpio_set;
14262306a36Sopenharmony_ci	tps68470_gpio->gc.can_sleep = true;
14362306a36Sopenharmony_ci	tps68470_gpio->gc.names = tps68470_names;
14462306a36Sopenharmony_ci	tps68470_gpio->gc.ngpio = TPS68470_N_GPIO;
14562306a36Sopenharmony_ci	tps68470_gpio->gc.base = -1;
14662306a36Sopenharmony_ci	tps68470_gpio->gc.parent = &pdev->dev;
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	return devm_gpiochip_add_data(&pdev->dev, &tps68470_gpio->gc, tps68470_gpio);
14962306a36Sopenharmony_ci}
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_cistatic struct platform_driver tps68470_gpio_driver = {
15262306a36Sopenharmony_ci	.driver = {
15362306a36Sopenharmony_ci		   .name = "tps68470-gpio",
15462306a36Sopenharmony_ci	},
15562306a36Sopenharmony_ci	.probe = tps68470_gpio_probe,
15662306a36Sopenharmony_ci};
15762306a36Sopenharmony_cimodule_platform_driver(tps68470_gpio_driver);
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ciMODULE_ALIAS("platform:tps68470-gpio");
16062306a36Sopenharmony_ciMODULE_DESCRIPTION("GPIO driver for TPS68470 PMIC");
16162306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
162