18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * GPIO driver for Exar XR17V35X chip
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/bitops.h>
88c2ecf20Sopenharmony_ci#include <linux/device.h>
98c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h>
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/pci.h>
148c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define EXAR_OFFSET_MPIOLVL_LO 0x90
178c2ecf20Sopenharmony_ci#define EXAR_OFFSET_MPIOSEL_LO 0x93
188c2ecf20Sopenharmony_ci#define EXAR_OFFSET_MPIOLVL_HI 0x96
198c2ecf20Sopenharmony_ci#define EXAR_OFFSET_MPIOSEL_HI 0x99
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define DRIVER_NAME "gpio_exar"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic DEFINE_IDA(ida_index);
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistruct exar_gpio_chip {
268c2ecf20Sopenharmony_ci	struct gpio_chip gpio_chip;
278c2ecf20Sopenharmony_ci	struct mutex lock;
288c2ecf20Sopenharmony_ci	int index;
298c2ecf20Sopenharmony_ci	void __iomem *regs;
308c2ecf20Sopenharmony_ci	char name[20];
318c2ecf20Sopenharmony_ci	unsigned int first_pin;
328c2ecf20Sopenharmony_ci};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic void exar_update(struct gpio_chip *chip, unsigned int reg, int val,
358c2ecf20Sopenharmony_ci			unsigned int offset)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
388c2ecf20Sopenharmony_ci	int temp;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	mutex_lock(&exar_gpio->lock);
418c2ecf20Sopenharmony_ci	temp = readb(exar_gpio->regs + reg);
428c2ecf20Sopenharmony_ci	temp &= ~BIT(offset);
438c2ecf20Sopenharmony_ci	if (val)
448c2ecf20Sopenharmony_ci		temp |= BIT(offset);
458c2ecf20Sopenharmony_ci	writeb(temp, exar_gpio->regs + reg);
468c2ecf20Sopenharmony_ci	mutex_unlock(&exar_gpio->lock);
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic int exar_set_direction(struct gpio_chip *chip, int direction,
508c2ecf20Sopenharmony_ci			      unsigned int offset)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
538c2ecf20Sopenharmony_ci	unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
548c2ecf20Sopenharmony_ci		EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
558c2ecf20Sopenharmony_ci	unsigned int bit  = (offset + exar_gpio->first_pin) % 8;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	exar_update(chip, addr, direction, bit);
588c2ecf20Sopenharmony_ci	return 0;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic int exar_get(struct gpio_chip *chip, unsigned int reg)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
648c2ecf20Sopenharmony_ci	int value;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	mutex_lock(&exar_gpio->lock);
678c2ecf20Sopenharmony_ci	value = readb(exar_gpio->regs + reg);
688c2ecf20Sopenharmony_ci	mutex_unlock(&exar_gpio->lock);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	return value;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic int exar_get_direction(struct gpio_chip *chip, unsigned int offset)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
768c2ecf20Sopenharmony_ci	unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
778c2ecf20Sopenharmony_ci		EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
788c2ecf20Sopenharmony_ci	unsigned int bit  = (offset + exar_gpio->first_pin) % 8;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	if (exar_get(chip, addr) & BIT(bit))
818c2ecf20Sopenharmony_ci		return GPIO_LINE_DIRECTION_IN;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return GPIO_LINE_DIRECTION_OUT;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int exar_get_value(struct gpio_chip *chip, unsigned int offset)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
898c2ecf20Sopenharmony_ci	unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
908c2ecf20Sopenharmony_ci		EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
918c2ecf20Sopenharmony_ci	unsigned int bit  = (offset + exar_gpio->first_pin) % 8;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	return !!(exar_get(chip, addr) & BIT(bit));
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic void exar_set_value(struct gpio_chip *chip, unsigned int offset,
978c2ecf20Sopenharmony_ci			   int value)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
1008c2ecf20Sopenharmony_ci	unsigned int addr = (offset + exar_gpio->first_pin) / 8 ?
1018c2ecf20Sopenharmony_ci		EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
1028c2ecf20Sopenharmony_ci	unsigned int bit  = (offset + exar_gpio->first_pin) % 8;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	exar_update(chip, addr, value, bit);
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic int exar_direction_output(struct gpio_chip *chip, unsigned int offset,
1088c2ecf20Sopenharmony_ci				 int value)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	exar_set_value(chip, offset, value);
1118c2ecf20Sopenharmony_ci	return exar_set_direction(chip, 0, offset);
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic int exar_direction_input(struct gpio_chip *chip, unsigned int offset)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	return exar_set_direction(chip, 1, offset);
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic int gpio_exar_probe(struct platform_device *pdev)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	struct pci_dev *pcidev = to_pci_dev(pdev->dev.parent);
1228c2ecf20Sopenharmony_ci	struct exar_gpio_chip *exar_gpio;
1238c2ecf20Sopenharmony_ci	u32 first_pin, ngpios;
1248c2ecf20Sopenharmony_ci	void __iomem *p;
1258c2ecf20Sopenharmony_ci	int index, ret;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	/*
1288c2ecf20Sopenharmony_ci	 * The UART driver must have mapped region 0 prior to registering this
1298c2ecf20Sopenharmony_ci	 * device - use it.
1308c2ecf20Sopenharmony_ci	 */
1318c2ecf20Sopenharmony_ci	p = pcim_iomap_table(pcidev)[0];
1328c2ecf20Sopenharmony_ci	if (!p)
1338c2ecf20Sopenharmony_ci		return -ENOMEM;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	ret = device_property_read_u32(&pdev->dev, "exar,first-pin",
1368c2ecf20Sopenharmony_ci				       &first_pin);
1378c2ecf20Sopenharmony_ci	if (ret)
1388c2ecf20Sopenharmony_ci		return ret;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	ret = device_property_read_u32(&pdev->dev, "ngpios", &ngpios);
1418c2ecf20Sopenharmony_ci	if (ret)
1428c2ecf20Sopenharmony_ci		return ret;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	exar_gpio = devm_kzalloc(&pdev->dev, sizeof(*exar_gpio), GFP_KERNEL);
1458c2ecf20Sopenharmony_ci	if (!exar_gpio)
1468c2ecf20Sopenharmony_ci		return -ENOMEM;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	mutex_init(&exar_gpio->lock);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	index = ida_simple_get(&ida_index, 0, 0, GFP_KERNEL);
1518c2ecf20Sopenharmony_ci	if (index < 0) {
1528c2ecf20Sopenharmony_ci		ret = index;
1538c2ecf20Sopenharmony_ci		goto err_mutex_destroy;
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	sprintf(exar_gpio->name, "exar_gpio%d", index);
1578c2ecf20Sopenharmony_ci	exar_gpio->gpio_chip.label = exar_gpio->name;
1588c2ecf20Sopenharmony_ci	exar_gpio->gpio_chip.parent = &pdev->dev;
1598c2ecf20Sopenharmony_ci	exar_gpio->gpio_chip.direction_output = exar_direction_output;
1608c2ecf20Sopenharmony_ci	exar_gpio->gpio_chip.direction_input = exar_direction_input;
1618c2ecf20Sopenharmony_ci	exar_gpio->gpio_chip.get_direction = exar_get_direction;
1628c2ecf20Sopenharmony_ci	exar_gpio->gpio_chip.get = exar_get_value;
1638c2ecf20Sopenharmony_ci	exar_gpio->gpio_chip.set = exar_set_value;
1648c2ecf20Sopenharmony_ci	exar_gpio->gpio_chip.base = -1;
1658c2ecf20Sopenharmony_ci	exar_gpio->gpio_chip.ngpio = ngpios;
1668c2ecf20Sopenharmony_ci	exar_gpio->regs = p;
1678c2ecf20Sopenharmony_ci	exar_gpio->index = index;
1688c2ecf20Sopenharmony_ci	exar_gpio->first_pin = first_pin;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	ret = devm_gpiochip_add_data(&pdev->dev,
1718c2ecf20Sopenharmony_ci				     &exar_gpio->gpio_chip, exar_gpio);
1728c2ecf20Sopenharmony_ci	if (ret)
1738c2ecf20Sopenharmony_ci		goto err_destroy;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, exar_gpio);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	return 0;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cierr_destroy:
1808c2ecf20Sopenharmony_ci	ida_simple_remove(&ida_index, index);
1818c2ecf20Sopenharmony_cierr_mutex_destroy:
1828c2ecf20Sopenharmony_ci	mutex_destroy(&exar_gpio->lock);
1838c2ecf20Sopenharmony_ci	return ret;
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic int gpio_exar_remove(struct platform_device *pdev)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	struct exar_gpio_chip *exar_gpio = platform_get_drvdata(pdev);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	ida_simple_remove(&ida_index, exar_gpio->index);
1918c2ecf20Sopenharmony_ci	mutex_destroy(&exar_gpio->lock);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	return 0;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic struct platform_driver gpio_exar_driver = {
1978c2ecf20Sopenharmony_ci	.probe	= gpio_exar_probe,
1988c2ecf20Sopenharmony_ci	.remove	= gpio_exar_remove,
1998c2ecf20Sopenharmony_ci	.driver	= {
2008c2ecf20Sopenharmony_ci		.name = DRIVER_NAME,
2018c2ecf20Sopenharmony_ci	},
2028c2ecf20Sopenharmony_ci};
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cimodule_platform_driver(gpio_exar_driver);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:" DRIVER_NAME);
2078c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Exar GPIO driver");
2088c2ecf20Sopenharmony_ciMODULE_AUTHOR("Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>");
2098c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
210