18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (C) 2014 STMicroelectronics – All Rights Reserved
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Author: Lee Jones <lee.jones@linaro.org>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  This is a re-write of Christophe Kerello's PMU driver.
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <dt-bindings/interrupt-controller/irq-st.h>
118c2ecf20Sopenharmony_ci#include <linux/err.h>
128c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
138c2ecf20Sopenharmony_ci#include <linux/of_device.h>
148c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
158c2ecf20Sopenharmony_ci#include <linux/regmap.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define STIH415_SYSCFG_642		0x0a8
198c2ecf20Sopenharmony_ci#define STIH416_SYSCFG_7543		0x87c
208c2ecf20Sopenharmony_ci#define STIH407_SYSCFG_5102		0x198
218c2ecf20Sopenharmony_ci#define STID127_SYSCFG_734		0x088
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define ST_A9_IRQ_MASK			0x001FFFFF
248c2ecf20Sopenharmony_ci#define ST_A9_IRQ_MAX_CHANS		2
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define ST_A9_IRQ_EN_CTI_0		BIT(0)
278c2ecf20Sopenharmony_ci#define ST_A9_IRQ_EN_CTI_1		BIT(1)
288c2ecf20Sopenharmony_ci#define ST_A9_IRQ_EN_PMU_0		BIT(2)
298c2ecf20Sopenharmony_ci#define ST_A9_IRQ_EN_PMU_1		BIT(3)
308c2ecf20Sopenharmony_ci#define ST_A9_IRQ_EN_PL310_L2		BIT(4)
318c2ecf20Sopenharmony_ci#define ST_A9_IRQ_EN_EXT_0		BIT(5)
328c2ecf20Sopenharmony_ci#define ST_A9_IRQ_EN_EXT_1		BIT(6)
338c2ecf20Sopenharmony_ci#define ST_A9_IRQ_EN_EXT_2		BIT(7)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define ST_A9_FIQ_N_SEL(dev, chan)	(dev << (8  + (chan * 3)))
368c2ecf20Sopenharmony_ci#define ST_A9_IRQ_N_SEL(dev, chan)	(dev << (14 + (chan * 3)))
378c2ecf20Sopenharmony_ci#define ST_A9_EXTIRQ_INV_SEL(dev)	(dev << 20)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistruct st_irq_syscfg {
408c2ecf20Sopenharmony_ci	struct regmap *regmap;
418c2ecf20Sopenharmony_ci	unsigned int syscfg;
428c2ecf20Sopenharmony_ci	unsigned int config;
438c2ecf20Sopenharmony_ci	bool ext_inverted;
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic const struct of_device_id st_irq_syscfg_match[] = {
478c2ecf20Sopenharmony_ci	{
488c2ecf20Sopenharmony_ci		.compatible = "st,stih415-irq-syscfg",
498c2ecf20Sopenharmony_ci		.data = (void *)STIH415_SYSCFG_642,
508c2ecf20Sopenharmony_ci	},
518c2ecf20Sopenharmony_ci	{
528c2ecf20Sopenharmony_ci		.compatible = "st,stih416-irq-syscfg",
538c2ecf20Sopenharmony_ci		.data = (void *)STIH416_SYSCFG_7543,
548c2ecf20Sopenharmony_ci	},
558c2ecf20Sopenharmony_ci	{
568c2ecf20Sopenharmony_ci		.compatible = "st,stih407-irq-syscfg",
578c2ecf20Sopenharmony_ci		.data = (void *)STIH407_SYSCFG_5102,
588c2ecf20Sopenharmony_ci	},
598c2ecf20Sopenharmony_ci	{
608c2ecf20Sopenharmony_ci		.compatible = "st,stid127-irq-syscfg",
618c2ecf20Sopenharmony_ci		.data = (void *)STID127_SYSCFG_734,
628c2ecf20Sopenharmony_ci	},
638c2ecf20Sopenharmony_ci	{}
648c2ecf20Sopenharmony_ci};
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic int st_irq_xlate(struct platform_device *pdev,
678c2ecf20Sopenharmony_ci			int device, int channel, bool irq)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	struct st_irq_syscfg *ddata = dev_get_drvdata(&pdev->dev);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/* Set the device enable bit. */
728c2ecf20Sopenharmony_ci	switch (device) {
738c2ecf20Sopenharmony_ci	case ST_IRQ_SYSCFG_EXT_0:
748c2ecf20Sopenharmony_ci		ddata->config |= ST_A9_IRQ_EN_EXT_0;
758c2ecf20Sopenharmony_ci		break;
768c2ecf20Sopenharmony_ci	case ST_IRQ_SYSCFG_EXT_1:
778c2ecf20Sopenharmony_ci		ddata->config |= ST_A9_IRQ_EN_EXT_1;
788c2ecf20Sopenharmony_ci		break;
798c2ecf20Sopenharmony_ci	case ST_IRQ_SYSCFG_EXT_2:
808c2ecf20Sopenharmony_ci		ddata->config |= ST_A9_IRQ_EN_EXT_2;
818c2ecf20Sopenharmony_ci		break;
828c2ecf20Sopenharmony_ci	case ST_IRQ_SYSCFG_CTI_0:
838c2ecf20Sopenharmony_ci		ddata->config |= ST_A9_IRQ_EN_CTI_0;
848c2ecf20Sopenharmony_ci		break;
858c2ecf20Sopenharmony_ci	case ST_IRQ_SYSCFG_CTI_1:
868c2ecf20Sopenharmony_ci		ddata->config |= ST_A9_IRQ_EN_CTI_1;
878c2ecf20Sopenharmony_ci		break;
888c2ecf20Sopenharmony_ci	case ST_IRQ_SYSCFG_PMU_0:
898c2ecf20Sopenharmony_ci		ddata->config |= ST_A9_IRQ_EN_PMU_0;
908c2ecf20Sopenharmony_ci		break;
918c2ecf20Sopenharmony_ci	case ST_IRQ_SYSCFG_PMU_1:
928c2ecf20Sopenharmony_ci		ddata->config |= ST_A9_IRQ_EN_PMU_1;
938c2ecf20Sopenharmony_ci		break;
948c2ecf20Sopenharmony_ci	case ST_IRQ_SYSCFG_pl310_L2:
958c2ecf20Sopenharmony_ci		ddata->config |= ST_A9_IRQ_EN_PL310_L2;
968c2ecf20Sopenharmony_ci		break;
978c2ecf20Sopenharmony_ci	case ST_IRQ_SYSCFG_DISABLED:
988c2ecf20Sopenharmony_ci		return 0;
998c2ecf20Sopenharmony_ci	default:
1008c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Unrecognised device %d\n", device);
1018c2ecf20Sopenharmony_ci		return -EINVAL;
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	/* Select IRQ/FIQ channel for device. */
1058c2ecf20Sopenharmony_ci	ddata->config |= irq ?
1068c2ecf20Sopenharmony_ci		ST_A9_IRQ_N_SEL(device, channel) :
1078c2ecf20Sopenharmony_ci		ST_A9_FIQ_N_SEL(device, channel);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	return 0;
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic int st_irq_syscfg_enable(struct platform_device *pdev)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	struct device_node *np = pdev->dev.of_node;
1158c2ecf20Sopenharmony_ci	struct st_irq_syscfg *ddata = dev_get_drvdata(&pdev->dev);
1168c2ecf20Sopenharmony_ci	int channels, ret, i;
1178c2ecf20Sopenharmony_ci	u32 device, invert;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	channels = of_property_count_u32_elems(np, "st,irq-device");
1208c2ecf20Sopenharmony_ci	if (channels != ST_A9_IRQ_MAX_CHANS) {
1218c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "st,enable-irq-device must have 2 elems\n");
1228c2ecf20Sopenharmony_ci		return -EINVAL;
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	channels = of_property_count_u32_elems(np, "st,fiq-device");
1268c2ecf20Sopenharmony_ci	if (channels != ST_A9_IRQ_MAX_CHANS) {
1278c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "st,enable-fiq-device must have 2 elems\n");
1288c2ecf20Sopenharmony_ci		return -EINVAL;
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	for (i = 0; i < ST_A9_IRQ_MAX_CHANS; i++) {
1328c2ecf20Sopenharmony_ci		of_property_read_u32_index(np, "st,irq-device", i, &device);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		ret = st_irq_xlate(pdev, device, i, true);
1358c2ecf20Sopenharmony_ci		if (ret)
1368c2ecf20Sopenharmony_ci			return ret;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci		of_property_read_u32_index(np, "st,fiq-device", i, &device);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci		ret = st_irq_xlate(pdev, device, i, false);
1418c2ecf20Sopenharmony_ci		if (ret)
1428c2ecf20Sopenharmony_ci			return ret;
1438c2ecf20Sopenharmony_ci	}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/* External IRQs may be inverted. */
1468c2ecf20Sopenharmony_ci	of_property_read_u32(np, "st,invert-ext", &invert);
1478c2ecf20Sopenharmony_ci	ddata->config |= ST_A9_EXTIRQ_INV_SEL(invert);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	return regmap_update_bits(ddata->regmap, ddata->syscfg,
1508c2ecf20Sopenharmony_ci				  ST_A9_IRQ_MASK, ddata->config);
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic int st_irq_syscfg_probe(struct platform_device *pdev)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	struct device_node *np = pdev->dev.of_node;
1568c2ecf20Sopenharmony_ci	const struct of_device_id *match;
1578c2ecf20Sopenharmony_ci	struct st_irq_syscfg *ddata;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
1608c2ecf20Sopenharmony_ci	if (!ddata)
1618c2ecf20Sopenharmony_ci		return -ENOMEM;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	match = of_match_device(st_irq_syscfg_match, &pdev->dev);
1648c2ecf20Sopenharmony_ci	if (!match)
1658c2ecf20Sopenharmony_ci		return -ENODEV;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	ddata->syscfg = (unsigned int)match->data;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	ddata->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1708c2ecf20Sopenharmony_ci	if (IS_ERR(ddata->regmap)) {
1718c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "syscfg phandle missing\n");
1728c2ecf20Sopenharmony_ci		return PTR_ERR(ddata->regmap);
1738c2ecf20Sopenharmony_ci	}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	dev_set_drvdata(&pdev->dev, ddata);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	return st_irq_syscfg_enable(pdev);
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic int __maybe_unused st_irq_syscfg_resume(struct device *dev)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	struct st_irq_syscfg *ddata = dev_get_drvdata(dev);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	return regmap_update_bits(ddata->regmap, ddata->syscfg,
1858c2ecf20Sopenharmony_ci				  ST_A9_IRQ_MASK, ddata->config);
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(st_irq_syscfg_pm_ops, NULL, st_irq_syscfg_resume);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic struct platform_driver st_irq_syscfg_driver = {
1918c2ecf20Sopenharmony_ci	.driver = {
1928c2ecf20Sopenharmony_ci		.name = "st_irq_syscfg",
1938c2ecf20Sopenharmony_ci		.pm = &st_irq_syscfg_pm_ops,
1948c2ecf20Sopenharmony_ci		.of_match_table = st_irq_syscfg_match,
1958c2ecf20Sopenharmony_ci	},
1968c2ecf20Sopenharmony_ci	.probe = st_irq_syscfg_probe,
1978c2ecf20Sopenharmony_ci};
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic int __init st_irq_syscfg_init(void)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	return platform_driver_register(&st_irq_syscfg_driver);
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_cicore_initcall(st_irq_syscfg_init);
204