18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
38c2ecf20Sopenharmony_ci *	Keerthy <j-keerthy@ti.com>
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
68c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License version 2 as
78c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This program is distributed "as is" WITHOUT ANY WARRANTY of any
108c2ecf20Sopenharmony_ci * kind, whether expressed or implied; without even the implied warranty
118c2ecf20Sopenharmony_ci * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
128c2ecf20Sopenharmony_ci * GNU General Public License version 2 for more details.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * Based on the TPS65218 driver
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h>
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
208c2ecf20Sopenharmony_ci#include <linux/regmap.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <linux/mfd/lp873x.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define BITS_PER_GPO		0x4
258c2ecf20Sopenharmony_ci#define LP873X_GPO_CTRL_OD	0x2
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct lp873x_gpio {
288c2ecf20Sopenharmony_ci	struct gpio_chip chip;
298c2ecf20Sopenharmony_ci	struct lp873x *lp873;
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic int lp873x_gpio_get_direction(struct gpio_chip *chip,
338c2ecf20Sopenharmony_ci				     unsigned int offset)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	/* This device is output only */
368c2ecf20Sopenharmony_ci	return GPIO_LINE_DIRECTION_OUT;
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic int lp873x_gpio_direction_input(struct gpio_chip *chip,
408c2ecf20Sopenharmony_ci				       unsigned int offset)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	/* This device is output only */
438c2ecf20Sopenharmony_ci	return -EINVAL;
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic int lp873x_gpio_direction_output(struct gpio_chip *chip,
478c2ecf20Sopenharmony_ci					unsigned int offset, int value)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	struct lp873x_gpio *gpio = gpiochip_get_data(chip);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	/* Set the initial value */
528c2ecf20Sopenharmony_ci	return regmap_update_bits(gpio->lp873->regmap, LP873X_REG_GPO_CTRL,
538c2ecf20Sopenharmony_ci				  BIT(offset * BITS_PER_GPO),
548c2ecf20Sopenharmony_ci				  value ? BIT(offset * BITS_PER_GPO) : 0);
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic int lp873x_gpio_get(struct gpio_chip *chip, unsigned int offset)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct lp873x_gpio *gpio = gpiochip_get_data(chip);
608c2ecf20Sopenharmony_ci	int ret, val;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	ret = regmap_read(gpio->lp873->regmap, LP873X_REG_GPO_CTRL, &val);
638c2ecf20Sopenharmony_ci	if (ret < 0)
648c2ecf20Sopenharmony_ci		return ret;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	return val & BIT(offset * BITS_PER_GPO);
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic void lp873x_gpio_set(struct gpio_chip *chip, unsigned int offset,
708c2ecf20Sopenharmony_ci			    int value)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct lp873x_gpio *gpio = gpiochip_get_data(chip);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	regmap_update_bits(gpio->lp873->regmap, LP873X_REG_GPO_CTRL,
758c2ecf20Sopenharmony_ci			   BIT(offset * BITS_PER_GPO),
768c2ecf20Sopenharmony_ci			   value ? BIT(offset * BITS_PER_GPO) : 0);
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic int lp873x_gpio_request(struct gpio_chip *gc, unsigned int offset)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct lp873x_gpio *gpio = gpiochip_get_data(gc);
828c2ecf20Sopenharmony_ci	int ret;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	switch (offset) {
858c2ecf20Sopenharmony_ci	case 0:
868c2ecf20Sopenharmony_ci		/* No MUX Set up Needed for GPO */
878c2ecf20Sopenharmony_ci		break;
888c2ecf20Sopenharmony_ci	case 1:
898c2ecf20Sopenharmony_ci		/* Setup the CLKIN_PIN_SEL MUX to GPO2 */
908c2ecf20Sopenharmony_ci		ret = regmap_update_bits(gpio->lp873->regmap, LP873X_REG_CONFIG,
918c2ecf20Sopenharmony_ci					 LP873X_CONFIG_CLKIN_PIN_SEL, 0);
928c2ecf20Sopenharmony_ci		if (ret)
938c2ecf20Sopenharmony_ci			return ret;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci		break;
968c2ecf20Sopenharmony_ci	default:
978c2ecf20Sopenharmony_ci		return -EINVAL;
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	return 0;
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic int lp873x_gpio_set_config(struct gpio_chip *gc, unsigned offset,
1048c2ecf20Sopenharmony_ci				  unsigned long config)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct lp873x_gpio *gpio = gpiochip_get_data(gc);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	switch (pinconf_to_config_param(config)) {
1098c2ecf20Sopenharmony_ci	case PIN_CONFIG_DRIVE_OPEN_DRAIN:
1108c2ecf20Sopenharmony_ci		return regmap_update_bits(gpio->lp873->regmap,
1118c2ecf20Sopenharmony_ci					  LP873X_REG_GPO_CTRL,
1128c2ecf20Sopenharmony_ci					  BIT(offset * BITS_PER_GPO +
1138c2ecf20Sopenharmony_ci					  LP873X_GPO_CTRL_OD),
1148c2ecf20Sopenharmony_ci					  BIT(offset * BITS_PER_GPO +
1158c2ecf20Sopenharmony_ci					  LP873X_GPO_CTRL_OD));
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	case PIN_CONFIG_DRIVE_PUSH_PULL:
1188c2ecf20Sopenharmony_ci		return regmap_update_bits(gpio->lp873->regmap,
1198c2ecf20Sopenharmony_ci					  LP873X_REG_GPO_CTRL,
1208c2ecf20Sopenharmony_ci					  BIT(offset * BITS_PER_GPO +
1218c2ecf20Sopenharmony_ci					  LP873X_GPO_CTRL_OD), 0);
1228c2ecf20Sopenharmony_ci	default:
1238c2ecf20Sopenharmony_ci		return -ENOTSUPP;
1248c2ecf20Sopenharmony_ci	}
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic const struct gpio_chip template_chip = {
1288c2ecf20Sopenharmony_ci	.label			= "lp873x-gpio",
1298c2ecf20Sopenharmony_ci	.owner			= THIS_MODULE,
1308c2ecf20Sopenharmony_ci	.request		= lp873x_gpio_request,
1318c2ecf20Sopenharmony_ci	.get_direction		= lp873x_gpio_get_direction,
1328c2ecf20Sopenharmony_ci	.direction_input	= lp873x_gpio_direction_input,
1338c2ecf20Sopenharmony_ci	.direction_output	= lp873x_gpio_direction_output,
1348c2ecf20Sopenharmony_ci	.get			= lp873x_gpio_get,
1358c2ecf20Sopenharmony_ci	.set			= lp873x_gpio_set,
1368c2ecf20Sopenharmony_ci	.set_config		= lp873x_gpio_set_config,
1378c2ecf20Sopenharmony_ci	.base			= -1,
1388c2ecf20Sopenharmony_ci	.ngpio			= 2,
1398c2ecf20Sopenharmony_ci	.can_sleep		= true,
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic int lp873x_gpio_probe(struct platform_device *pdev)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	struct lp873x_gpio *gpio;
1458c2ecf20Sopenharmony_ci	int ret;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
1488c2ecf20Sopenharmony_ci	if (!gpio)
1498c2ecf20Sopenharmony_ci		return -ENOMEM;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, gpio);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	gpio->lp873 = dev_get_drvdata(pdev->dev.parent);
1548c2ecf20Sopenharmony_ci	gpio->chip = template_chip;
1558c2ecf20Sopenharmony_ci	gpio->chip.parent = gpio->lp873->dev;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
1588c2ecf20Sopenharmony_ci	if (ret < 0) {
1598c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
1608c2ecf20Sopenharmony_ci		return ret;
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	return 0;
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic const struct platform_device_id lp873x_gpio_id_table[] = {
1678c2ecf20Sopenharmony_ci	{ "lp873x-gpio", },
1688c2ecf20Sopenharmony_ci	{ /* sentinel */ }
1698c2ecf20Sopenharmony_ci};
1708c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, lp873x_gpio_id_table);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic struct platform_driver lp873x_gpio_driver = {
1738c2ecf20Sopenharmony_ci	.driver = {
1748c2ecf20Sopenharmony_ci		.name = "lp873x-gpio",
1758c2ecf20Sopenharmony_ci	},
1768c2ecf20Sopenharmony_ci	.probe = lp873x_gpio_probe,
1778c2ecf20Sopenharmony_ci	.id_table = lp873x_gpio_id_table,
1788c2ecf20Sopenharmony_ci};
1798c2ecf20Sopenharmony_cimodule_platform_driver(lp873x_gpio_driver);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ciMODULE_AUTHOR("Keerthy <j-keerthy@ti.com>");
1828c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("LP873X GPIO driver");
1838c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
184