18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  linux/drivers/gpio/gpio-mb86s7x.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2015 Fujitsu Semiconductor Limited
68c2ecf20Sopenharmony_ci *  Copyright (C) 2015 Linaro Ltd.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/acpi.h>
108c2ecf20Sopenharmony_ci#include <linux/io.h>
118c2ecf20Sopenharmony_ci#include <linux/init.h>
128c2ecf20Sopenharmony_ci#include <linux/clk.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/err.h>
158c2ecf20Sopenharmony_ci#include <linux/errno.h>
168c2ecf20Sopenharmony_ci#include <linux/ioport.h>
178c2ecf20Sopenharmony_ci#include <linux/of_device.h>
188c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h>
198c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
208c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
218c2ecf20Sopenharmony_ci#include <linux/slab.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include "gpiolib.h"
248c2ecf20Sopenharmony_ci#include "gpiolib-acpi.h"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/*
278c2ecf20Sopenharmony_ci * Only first 8bits of a register correspond to each pin,
288c2ecf20Sopenharmony_ci * so there are 4 registers for 32 pins.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci#define PDR(x)	(0x0 + x / 8 * 4)
318c2ecf20Sopenharmony_ci#define DDR(x)	(0x10 + x / 8 * 4)
328c2ecf20Sopenharmony_ci#define PFR(x)	(0x20 + x / 8 * 4)
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define OFFSET(x)	BIT((x) % 8)
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistruct mb86s70_gpio_chip {
378c2ecf20Sopenharmony_ci	struct gpio_chip gc;
388c2ecf20Sopenharmony_ci	void __iomem *base;
398c2ecf20Sopenharmony_ci	struct clk *clk;
408c2ecf20Sopenharmony_ci	spinlock_t lock;
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic int mb86s70_gpio_request(struct gpio_chip *gc, unsigned gpio)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
468c2ecf20Sopenharmony_ci	unsigned long flags;
478c2ecf20Sopenharmony_ci	u32 val;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gchip->lock, flags);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	val = readl(gchip->base + PFR(gpio));
528c2ecf20Sopenharmony_ci	val &= ~OFFSET(gpio);
538c2ecf20Sopenharmony_ci	writel(val, gchip->base + PFR(gpio));
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gchip->lock, flags);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	return 0;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic void mb86s70_gpio_free(struct gpio_chip *gc, unsigned gpio)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
638c2ecf20Sopenharmony_ci	unsigned long flags;
648c2ecf20Sopenharmony_ci	u32 val;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gchip->lock, flags);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	val = readl(gchip->base + PFR(gpio));
698c2ecf20Sopenharmony_ci	val |= OFFSET(gpio);
708c2ecf20Sopenharmony_ci	writel(val, gchip->base + PFR(gpio));
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gchip->lock, flags);
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic int mb86s70_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
788c2ecf20Sopenharmony_ci	unsigned long flags;
798c2ecf20Sopenharmony_ci	unsigned char val;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gchip->lock, flags);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	val = readl(gchip->base + DDR(gpio));
848c2ecf20Sopenharmony_ci	val &= ~OFFSET(gpio);
858c2ecf20Sopenharmony_ci	writel(val, gchip->base + DDR(gpio));
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gchip->lock, flags);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	return 0;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic int mb86s70_gpio_direction_output(struct gpio_chip *gc,
938c2ecf20Sopenharmony_ci					 unsigned gpio, int value)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
968c2ecf20Sopenharmony_ci	unsigned long flags;
978c2ecf20Sopenharmony_ci	unsigned char val;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gchip->lock, flags);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	val = readl(gchip->base + PDR(gpio));
1028c2ecf20Sopenharmony_ci	if (value)
1038c2ecf20Sopenharmony_ci		val |= OFFSET(gpio);
1048c2ecf20Sopenharmony_ci	else
1058c2ecf20Sopenharmony_ci		val &= ~OFFSET(gpio);
1068c2ecf20Sopenharmony_ci	writel(val, gchip->base + PDR(gpio));
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	val = readl(gchip->base + DDR(gpio));
1098c2ecf20Sopenharmony_ci	val |= OFFSET(gpio);
1108c2ecf20Sopenharmony_ci	writel(val, gchip->base + DDR(gpio));
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gchip->lock, flags);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return 0;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic int mb86s70_gpio_get(struct gpio_chip *gc, unsigned gpio)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	return !!(readl(gchip->base + PDR(gpio)) & OFFSET(gpio));
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic void mb86s70_gpio_set(struct gpio_chip *gc, unsigned gpio, int value)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
1278c2ecf20Sopenharmony_ci	unsigned long flags;
1288c2ecf20Sopenharmony_ci	unsigned char val;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gchip->lock, flags);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	val = readl(gchip->base + PDR(gpio));
1338c2ecf20Sopenharmony_ci	if (value)
1348c2ecf20Sopenharmony_ci		val |= OFFSET(gpio);
1358c2ecf20Sopenharmony_ci	else
1368c2ecf20Sopenharmony_ci		val &= ~OFFSET(gpio);
1378c2ecf20Sopenharmony_ci	writel(val, gchip->base + PDR(gpio));
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gchip->lock, flags);
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic int mb86s70_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	int irq, index;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	for (index = 0;; index++) {
1478c2ecf20Sopenharmony_ci		irq = platform_get_irq(to_platform_device(gc->parent), index);
1488c2ecf20Sopenharmony_ci		if (irq < 0)
1498c2ecf20Sopenharmony_ci			return irq;
1508c2ecf20Sopenharmony_ci		if (irq == 0)
1518c2ecf20Sopenharmony_ci			break;
1528c2ecf20Sopenharmony_ci		if (irq_get_irq_data(irq)->hwirq == offset)
1538c2ecf20Sopenharmony_ci			return irq;
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci	return -EINVAL;
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic int mb86s70_gpio_probe(struct platform_device *pdev)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	struct mb86s70_gpio_chip *gchip;
1618c2ecf20Sopenharmony_ci	int ret;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	gchip = devm_kzalloc(&pdev->dev, sizeof(*gchip), GFP_KERNEL);
1648c2ecf20Sopenharmony_ci	if (gchip == NULL)
1658c2ecf20Sopenharmony_ci		return -ENOMEM;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, gchip);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	gchip->base = devm_platform_ioremap_resource(pdev, 0);
1708c2ecf20Sopenharmony_ci	if (IS_ERR(gchip->base))
1718c2ecf20Sopenharmony_ci		return PTR_ERR(gchip->base);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	gchip->clk = devm_clk_get_optional(&pdev->dev, NULL);
1748c2ecf20Sopenharmony_ci	if (IS_ERR(gchip->clk))
1758c2ecf20Sopenharmony_ci		return PTR_ERR(gchip->clk);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(gchip->clk);
1788c2ecf20Sopenharmony_ci	if (ret)
1798c2ecf20Sopenharmony_ci		return ret;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	spin_lock_init(&gchip->lock);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	gchip->gc.direction_output = mb86s70_gpio_direction_output;
1848c2ecf20Sopenharmony_ci	gchip->gc.direction_input = mb86s70_gpio_direction_input;
1858c2ecf20Sopenharmony_ci	gchip->gc.request = mb86s70_gpio_request;
1868c2ecf20Sopenharmony_ci	gchip->gc.free = mb86s70_gpio_free;
1878c2ecf20Sopenharmony_ci	gchip->gc.get = mb86s70_gpio_get;
1888c2ecf20Sopenharmony_ci	gchip->gc.set = mb86s70_gpio_set;
1898c2ecf20Sopenharmony_ci	gchip->gc.to_irq = mb86s70_gpio_to_irq;
1908c2ecf20Sopenharmony_ci	gchip->gc.label = dev_name(&pdev->dev);
1918c2ecf20Sopenharmony_ci	gchip->gc.ngpio = 32;
1928c2ecf20Sopenharmony_ci	gchip->gc.owner = THIS_MODULE;
1938c2ecf20Sopenharmony_ci	gchip->gc.parent = &pdev->dev;
1948c2ecf20Sopenharmony_ci	gchip->gc.base = -1;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	ret = gpiochip_add_data(&gchip->gc, gchip);
1978c2ecf20Sopenharmony_ci	if (ret) {
1988c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "couldn't register gpio driver\n");
1998c2ecf20Sopenharmony_ci		clk_disable_unprepare(gchip->clk);
2008c2ecf20Sopenharmony_ci		return ret;
2018c2ecf20Sopenharmony_ci	}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	acpi_gpiochip_request_interrupts(&gchip->gc);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	return 0;
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic int mb86s70_gpio_remove(struct platform_device *pdev)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	struct mb86s70_gpio_chip *gchip = platform_get_drvdata(pdev);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	acpi_gpiochip_free_interrupts(&gchip->gc);
2138c2ecf20Sopenharmony_ci	gpiochip_remove(&gchip->gc);
2148c2ecf20Sopenharmony_ci	clk_disable_unprepare(gchip->clk);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	return 0;
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic const struct of_device_id mb86s70_gpio_dt_ids[] = {
2208c2ecf20Sopenharmony_ci	{ .compatible = "fujitsu,mb86s70-gpio" },
2218c2ecf20Sopenharmony_ci	{ /* sentinel */ }
2228c2ecf20Sopenharmony_ci};
2238c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mb86s70_gpio_dt_ids);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci#ifdef CONFIG_ACPI
2268c2ecf20Sopenharmony_cistatic const struct acpi_device_id mb86s70_gpio_acpi_ids[] = {
2278c2ecf20Sopenharmony_ci	{ "SCX0007" },
2288c2ecf20Sopenharmony_ci	{ /* sentinel */ }
2298c2ecf20Sopenharmony_ci};
2308c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, mb86s70_gpio_acpi_ids);
2318c2ecf20Sopenharmony_ci#endif
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistatic struct platform_driver mb86s70_gpio_driver = {
2348c2ecf20Sopenharmony_ci	.driver = {
2358c2ecf20Sopenharmony_ci		.name = "mb86s70-gpio",
2368c2ecf20Sopenharmony_ci		.of_match_table = mb86s70_gpio_dt_ids,
2378c2ecf20Sopenharmony_ci		.acpi_match_table = ACPI_PTR(mb86s70_gpio_acpi_ids),
2388c2ecf20Sopenharmony_ci	},
2398c2ecf20Sopenharmony_ci	.probe = mb86s70_gpio_probe,
2408c2ecf20Sopenharmony_ci	.remove = mb86s70_gpio_remove,
2418c2ecf20Sopenharmony_ci};
2428c2ecf20Sopenharmony_cimodule_platform_driver(mb86s70_gpio_driver);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MB86S7x GPIO Driver");
2458c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:mb86s70-gpio");
2468c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
247