18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci/*
48c2ecf20Sopenharmony_ci * Copyright 2017-2018 Cadence
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Authors:
78c2ecf20Sopenharmony_ci *  Jan Kotas <jank@cadence.com>
88c2ecf20Sopenharmony_ci *  Boris Brezillon <boris.brezillon@free-electrons.com>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h>
128c2ecf20Sopenharmony_ci#include <linux/clk.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
178c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define CDNS_GPIO_BYPASS_MODE		0x00
208c2ecf20Sopenharmony_ci#define CDNS_GPIO_DIRECTION_MODE	0x04
218c2ecf20Sopenharmony_ci#define CDNS_GPIO_OUTPUT_EN		0x08
228c2ecf20Sopenharmony_ci#define CDNS_GPIO_OUTPUT_VALUE		0x0c
238c2ecf20Sopenharmony_ci#define CDNS_GPIO_INPUT_VALUE		0x10
248c2ecf20Sopenharmony_ci#define CDNS_GPIO_IRQ_MASK		0x14
258c2ecf20Sopenharmony_ci#define CDNS_GPIO_IRQ_EN		0x18
268c2ecf20Sopenharmony_ci#define CDNS_GPIO_IRQ_DIS		0x1c
278c2ecf20Sopenharmony_ci#define CDNS_GPIO_IRQ_STATUS		0x20
288c2ecf20Sopenharmony_ci#define CDNS_GPIO_IRQ_TYPE		0x24
298c2ecf20Sopenharmony_ci#define CDNS_GPIO_IRQ_VALUE		0x28
308c2ecf20Sopenharmony_ci#define CDNS_GPIO_IRQ_ANY_EDGE		0x2c
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistruct cdns_gpio_chip {
338c2ecf20Sopenharmony_ci	struct gpio_chip gc;
348c2ecf20Sopenharmony_ci	struct clk *pclk;
358c2ecf20Sopenharmony_ci	void __iomem *regs;
368c2ecf20Sopenharmony_ci	u32 bypass_orig;
378c2ecf20Sopenharmony_ci};
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic int cdns_gpio_request(struct gpio_chip *chip, unsigned int offset)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
428c2ecf20Sopenharmony_ci	unsigned long flags;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->bgpio_lock, flags);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	iowrite32(ioread32(cgpio->regs + CDNS_GPIO_BYPASS_MODE) & ~BIT(offset),
478c2ecf20Sopenharmony_ci		  cgpio->regs + CDNS_GPIO_BYPASS_MODE);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->bgpio_lock, flags);
508c2ecf20Sopenharmony_ci	return 0;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic void cdns_gpio_free(struct gpio_chip *chip, unsigned int offset)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
568c2ecf20Sopenharmony_ci	unsigned long flags;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->bgpio_lock, flags);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	iowrite32(ioread32(cgpio->regs + CDNS_GPIO_BYPASS_MODE) |
618c2ecf20Sopenharmony_ci		  (BIT(offset) & cgpio->bypass_orig),
628c2ecf20Sopenharmony_ci		  cgpio->regs + CDNS_GPIO_BYPASS_MODE);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->bgpio_lock, flags);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic void cdns_gpio_irq_mask(struct irq_data *d)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
708c2ecf20Sopenharmony_ci	struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	iowrite32(BIT(d->hwirq), cgpio->regs + CDNS_GPIO_IRQ_DIS);
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic void cdns_gpio_irq_unmask(struct irq_data *d)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
788c2ecf20Sopenharmony_ci	struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	iowrite32(BIT(d->hwirq), cgpio->regs + CDNS_GPIO_IRQ_EN);
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic int cdns_gpio_irq_set_type(struct irq_data *d, unsigned int type)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
868c2ecf20Sopenharmony_ci	struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
878c2ecf20Sopenharmony_ci	unsigned long flags;
888c2ecf20Sopenharmony_ci	u32 int_value;
898c2ecf20Sopenharmony_ci	u32 int_type;
908c2ecf20Sopenharmony_ci	u32 mask = BIT(d->hwirq);
918c2ecf20Sopenharmony_ci	int ret = 0;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->bgpio_lock, flags);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	int_value = ioread32(cgpio->regs + CDNS_GPIO_IRQ_VALUE) & ~mask;
968c2ecf20Sopenharmony_ci	int_type = ioread32(cgpio->regs + CDNS_GPIO_IRQ_TYPE) & ~mask;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	/*
998c2ecf20Sopenharmony_ci	 * The GPIO controller doesn't have an ACK register.
1008c2ecf20Sopenharmony_ci	 * All interrupt statuses are cleared on a status register read.
1018c2ecf20Sopenharmony_ci	 * Don't support edge interrupts for now.
1028c2ecf20Sopenharmony_ci	 */
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	if (type == IRQ_TYPE_LEVEL_HIGH) {
1058c2ecf20Sopenharmony_ci		int_type |= mask;
1068c2ecf20Sopenharmony_ci		int_value |= mask;
1078c2ecf20Sopenharmony_ci	} else if (type == IRQ_TYPE_LEVEL_LOW) {
1088c2ecf20Sopenharmony_ci		int_type |= mask;
1098c2ecf20Sopenharmony_ci	} else {
1108c2ecf20Sopenharmony_ci		ret = -EINVAL;
1118c2ecf20Sopenharmony_ci		goto err_irq_type;
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	iowrite32(int_value, cgpio->regs + CDNS_GPIO_IRQ_VALUE);
1158c2ecf20Sopenharmony_ci	iowrite32(int_type, cgpio->regs + CDNS_GPIO_IRQ_TYPE);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cierr_irq_type:
1188c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->bgpio_lock, flags);
1198c2ecf20Sopenharmony_ci	return ret;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic void cdns_gpio_irq_handler(struct irq_desc *desc)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct gpio_chip *chip = irq_desc_get_handler_data(desc);
1258c2ecf20Sopenharmony_ci	struct cdns_gpio_chip *cgpio = gpiochip_get_data(chip);
1268c2ecf20Sopenharmony_ci	struct irq_chip *irqchip = irq_desc_get_chip(desc);
1278c2ecf20Sopenharmony_ci	unsigned long status;
1288c2ecf20Sopenharmony_ci	int hwirq;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	chained_irq_enter(irqchip, desc);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	status = ioread32(cgpio->regs + CDNS_GPIO_IRQ_STATUS) &
1338c2ecf20Sopenharmony_ci		~ioread32(cgpio->regs + CDNS_GPIO_IRQ_MASK);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	for_each_set_bit(hwirq, &status, chip->ngpio)
1368c2ecf20Sopenharmony_ci		generic_handle_irq(irq_find_mapping(chip->irq.domain, hwirq));
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	chained_irq_exit(irqchip, desc);
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic struct irq_chip cdns_gpio_irqchip = {
1428c2ecf20Sopenharmony_ci	.name		= "cdns-gpio",
1438c2ecf20Sopenharmony_ci	.irq_mask	= cdns_gpio_irq_mask,
1448c2ecf20Sopenharmony_ci	.irq_unmask	= cdns_gpio_irq_unmask,
1458c2ecf20Sopenharmony_ci	.irq_set_type	= cdns_gpio_irq_set_type
1468c2ecf20Sopenharmony_ci};
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic int cdns_gpio_probe(struct platform_device *pdev)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	struct cdns_gpio_chip *cgpio;
1518c2ecf20Sopenharmony_ci	int ret, irq;
1528c2ecf20Sopenharmony_ci	u32 dir_prev;
1538c2ecf20Sopenharmony_ci	u32 num_gpios = 32;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	cgpio = devm_kzalloc(&pdev->dev, sizeof(*cgpio), GFP_KERNEL);
1568c2ecf20Sopenharmony_ci	if (!cgpio)
1578c2ecf20Sopenharmony_ci		return -ENOMEM;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	cgpio->regs = devm_platform_ioremap_resource(pdev, 0);
1608c2ecf20Sopenharmony_ci	if (IS_ERR(cgpio->regs))
1618c2ecf20Sopenharmony_ci		return PTR_ERR(cgpio->regs);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	of_property_read_u32(pdev->dev.of_node, "ngpios", &num_gpios);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	if (num_gpios > 32) {
1668c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "ngpios must be less or equal 32\n");
1678c2ecf20Sopenharmony_ci		return -EINVAL;
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	/*
1718c2ecf20Sopenharmony_ci	 * Set all pins as inputs by default, otherwise:
1728c2ecf20Sopenharmony_ci	 * gpiochip_lock_as_irq:
1738c2ecf20Sopenharmony_ci	 * tried to flag a GPIO set as output for IRQ
1748c2ecf20Sopenharmony_ci	 * Generic GPIO driver stores the direction value internally,
1758c2ecf20Sopenharmony_ci	 * so it needs to be changed before bgpio_init() is called.
1768c2ecf20Sopenharmony_ci	 */
1778c2ecf20Sopenharmony_ci	dir_prev = ioread32(cgpio->regs + CDNS_GPIO_DIRECTION_MODE);
1788c2ecf20Sopenharmony_ci	iowrite32(GENMASK(num_gpios - 1, 0),
1798c2ecf20Sopenharmony_ci		  cgpio->regs + CDNS_GPIO_DIRECTION_MODE);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	ret = bgpio_init(&cgpio->gc, &pdev->dev, 4,
1828c2ecf20Sopenharmony_ci			 cgpio->regs + CDNS_GPIO_INPUT_VALUE,
1838c2ecf20Sopenharmony_ci			 cgpio->regs + CDNS_GPIO_OUTPUT_VALUE,
1848c2ecf20Sopenharmony_ci			 NULL,
1858c2ecf20Sopenharmony_ci			 NULL,
1868c2ecf20Sopenharmony_ci			 cgpio->regs + CDNS_GPIO_DIRECTION_MODE,
1878c2ecf20Sopenharmony_ci			 BGPIOF_READ_OUTPUT_REG_SET);
1888c2ecf20Sopenharmony_ci	if (ret) {
1898c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to register generic gpio, %d\n",
1908c2ecf20Sopenharmony_ci			ret);
1918c2ecf20Sopenharmony_ci		goto err_revert_dir;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	cgpio->gc.label = dev_name(&pdev->dev);
1958c2ecf20Sopenharmony_ci	cgpio->gc.ngpio = num_gpios;
1968c2ecf20Sopenharmony_ci	cgpio->gc.parent = &pdev->dev;
1978c2ecf20Sopenharmony_ci	cgpio->gc.base = -1;
1988c2ecf20Sopenharmony_ci	cgpio->gc.owner = THIS_MODULE;
1998c2ecf20Sopenharmony_ci	cgpio->gc.request = cdns_gpio_request;
2008c2ecf20Sopenharmony_ci	cgpio->gc.free = cdns_gpio_free;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	cgpio->pclk = devm_clk_get(&pdev->dev, NULL);
2038c2ecf20Sopenharmony_ci	if (IS_ERR(cgpio->pclk)) {
2048c2ecf20Sopenharmony_ci		ret = PTR_ERR(cgpio->pclk);
2058c2ecf20Sopenharmony_ci		dev_err(&pdev->dev,
2068c2ecf20Sopenharmony_ci			"Failed to retrieve peripheral clock, %d\n", ret);
2078c2ecf20Sopenharmony_ci		goto err_revert_dir;
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(cgpio->pclk);
2118c2ecf20Sopenharmony_ci	if (ret) {
2128c2ecf20Sopenharmony_ci		dev_err(&pdev->dev,
2138c2ecf20Sopenharmony_ci			"Failed to enable the peripheral clock, %d\n", ret);
2148c2ecf20Sopenharmony_ci		goto err_revert_dir;
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	/*
2188c2ecf20Sopenharmony_ci	 * Optional irq_chip support
2198c2ecf20Sopenharmony_ci	 */
2208c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
2218c2ecf20Sopenharmony_ci	if (irq >= 0) {
2228c2ecf20Sopenharmony_ci		struct gpio_irq_chip *girq;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci		girq = &cgpio->gc.irq;
2258c2ecf20Sopenharmony_ci		girq->chip = &cdns_gpio_irqchip;
2268c2ecf20Sopenharmony_ci		girq->parent_handler = cdns_gpio_irq_handler;
2278c2ecf20Sopenharmony_ci		girq->num_parents = 1;
2288c2ecf20Sopenharmony_ci		girq->parents = devm_kcalloc(&pdev->dev, 1,
2298c2ecf20Sopenharmony_ci					     sizeof(*girq->parents),
2308c2ecf20Sopenharmony_ci					     GFP_KERNEL);
2318c2ecf20Sopenharmony_ci		if (!girq->parents) {
2328c2ecf20Sopenharmony_ci			ret = -ENOMEM;
2338c2ecf20Sopenharmony_ci			goto err_disable_clk;
2348c2ecf20Sopenharmony_ci		}
2358c2ecf20Sopenharmony_ci		girq->parents[0] = irq;
2368c2ecf20Sopenharmony_ci		girq->default_type = IRQ_TYPE_NONE;
2378c2ecf20Sopenharmony_ci		girq->handler = handle_level_irq;
2388c2ecf20Sopenharmony_ci	}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	ret = devm_gpiochip_add_data(&pdev->dev, &cgpio->gc, cgpio);
2418c2ecf20Sopenharmony_ci	if (ret < 0) {
2428c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
2438c2ecf20Sopenharmony_ci		goto err_disable_clk;
2448c2ecf20Sopenharmony_ci	}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	cgpio->bypass_orig = ioread32(cgpio->regs + CDNS_GPIO_BYPASS_MODE);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	/*
2498c2ecf20Sopenharmony_ci	 * Enable gpio outputs, ignored for input direction
2508c2ecf20Sopenharmony_ci	 */
2518c2ecf20Sopenharmony_ci	iowrite32(GENMASK(num_gpios - 1, 0),
2528c2ecf20Sopenharmony_ci		  cgpio->regs + CDNS_GPIO_OUTPUT_EN);
2538c2ecf20Sopenharmony_ci	iowrite32(0, cgpio->regs + CDNS_GPIO_BYPASS_MODE);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, cgpio);
2568c2ecf20Sopenharmony_ci	return 0;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cierr_disable_clk:
2598c2ecf20Sopenharmony_ci	clk_disable_unprepare(cgpio->pclk);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cierr_revert_dir:
2628c2ecf20Sopenharmony_ci	iowrite32(dir_prev, cgpio->regs + CDNS_GPIO_DIRECTION_MODE);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	return ret;
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_cistatic int cdns_gpio_remove(struct platform_device *pdev)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	struct cdns_gpio_chip *cgpio = platform_get_drvdata(pdev);
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	iowrite32(cgpio->bypass_orig, cgpio->regs + CDNS_GPIO_BYPASS_MODE);
2728c2ecf20Sopenharmony_ci	clk_disable_unprepare(cgpio->pclk);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	return 0;
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic const struct of_device_id cdns_of_ids[] = {
2788c2ecf20Sopenharmony_ci	{ .compatible = "cdns,gpio-r1p02" },
2798c2ecf20Sopenharmony_ci	{ /* sentinel */ },
2808c2ecf20Sopenharmony_ci};
2818c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, cdns_of_ids);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cistatic struct platform_driver cdns_gpio_driver = {
2848c2ecf20Sopenharmony_ci	.driver = {
2858c2ecf20Sopenharmony_ci		.name = "cdns-gpio",
2868c2ecf20Sopenharmony_ci		.of_match_table = cdns_of_ids,
2878c2ecf20Sopenharmony_ci	},
2888c2ecf20Sopenharmony_ci	.probe = cdns_gpio_probe,
2898c2ecf20Sopenharmony_ci	.remove = cdns_gpio_remove,
2908c2ecf20Sopenharmony_ci};
2918c2ecf20Sopenharmony_cimodule_platform_driver(cdns_gpio_driver);
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jan Kotas <jank@cadence.com>");
2948c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Cadence GPIO driver");
2958c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
2968c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:cdns-gpio");
297