xref: /linux-6.15/drivers/mfd/syscon.c (revision 39233b7c)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
287d68730SDong Aisheng /*
387d68730SDong Aisheng  * System Control Driver
487d68730SDong Aisheng  *
587d68730SDong Aisheng  * Copyright (C) 2012 Freescale Semiconductor, Inc.
687d68730SDong Aisheng  * Copyright (C) 2012 Linaro Ltd.
787d68730SDong Aisheng  *
887d68730SDong Aisheng  * Author: Dong Aisheng <[email protected]>
987d68730SDong Aisheng  */
1087d68730SDong Aisheng 
11a00406b7SFabrice Gasnier #include <linux/clk.h>
1287d68730SDong Aisheng #include <linux/err.h>
133bafc09eSBaolin Wang #include <linux/hwspinlock.h>
1487d68730SDong Aisheng #include <linux/io.h>
151345da73SPaul Gortmaker #include <linux/init.h>
16bdb0066dSPankaj Dubey #include <linux/list.h>
1787d68730SDong Aisheng #include <linux/of.h>
1887d68730SDong Aisheng #include <linux/of_address.h>
1987d68730SDong Aisheng #include <linux/of_platform.h>
2029f9b6cfSPawel Moll #include <linux/platform_data/syscon.h>
2187d68730SDong Aisheng #include <linux/platform_device.h>
2287d68730SDong Aisheng #include <linux/regmap.h>
2375177deeSFabio Estevam #include <linux/mfd/syscon.h>
24bdb0066dSPankaj Dubey #include <linux/slab.h>
2587d68730SDong Aisheng 
2687d68730SDong Aisheng static struct platform_driver syscon_driver;
2787d68730SDong Aisheng 
28bdb0066dSPankaj Dubey static DEFINE_SPINLOCK(syscon_list_slock);
29bdb0066dSPankaj Dubey static LIST_HEAD(syscon_list);
30bdb0066dSPankaj Dubey 
3187d68730SDong Aisheng struct syscon {
32bdb0066dSPankaj Dubey 	struct device_node *np;
3387d68730SDong Aisheng 	struct regmap *regmap;
34bdb0066dSPankaj Dubey 	struct list_head list;
3587d68730SDong Aisheng };
3687d68730SDong Aisheng 
37c131045dSPhilipp Zabel static const struct regmap_config syscon_regmap_config = {
38bdb0066dSPankaj Dubey 	.reg_bits = 32,
39bdb0066dSPankaj Dubey 	.val_bits = 32,
40bdb0066dSPankaj Dubey 	.reg_stride = 4,
41bdb0066dSPankaj Dubey };
4287d68730SDong Aisheng 
43*39233b7cSPaul Cercueil static struct syscon *of_syscon_register(struct device_node *np, bool check_clk)
44bdb0066dSPankaj Dubey {
45a00406b7SFabrice Gasnier 	struct clk *clk;
46bdb0066dSPankaj Dubey 	struct syscon *syscon;
47bdb0066dSPankaj Dubey 	struct regmap *regmap;
48bdb0066dSPankaj Dubey 	void __iomem *base;
49db2fb60cSDamien Riegel 	u32 reg_io_width;
50bdb0066dSPankaj Dubey 	int ret;
51bdb0066dSPankaj Dubey 	struct regmap_config syscon_config = syscon_regmap_config;
52ca668f0eSPhilipp Zabel 	struct resource res;
53bdb0066dSPankaj Dubey 
54bdb0066dSPankaj Dubey 	syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
55bdb0066dSPankaj Dubey 	if (!syscon)
56bdb0066dSPankaj Dubey 		return ERR_PTR(-ENOMEM);
57bdb0066dSPankaj Dubey 
58ca668f0eSPhilipp Zabel 	if (of_address_to_resource(np, 0, &res)) {
59ca668f0eSPhilipp Zabel 		ret = -ENOMEM;
60ca668f0eSPhilipp Zabel 		goto err_map;
61ca668f0eSPhilipp Zabel 	}
62ca668f0eSPhilipp Zabel 
63ca668f0eSPhilipp Zabel 	base = ioremap(res.start, resource_size(&res));
64bdb0066dSPankaj Dubey 	if (!base) {
65bdb0066dSPankaj Dubey 		ret = -ENOMEM;
66bdb0066dSPankaj Dubey 		goto err_map;
67bdb0066dSPankaj Dubey 	}
68bdb0066dSPankaj Dubey 
69bdb0066dSPankaj Dubey 	/* Parse the device's DT node for an endianness specification */
70bdb0066dSPankaj Dubey 	if (of_property_read_bool(np, "big-endian"))
71bdb0066dSPankaj Dubey 		syscon_config.val_format_endian = REGMAP_ENDIAN_BIG;
72bdb0066dSPankaj Dubey 	else if (of_property_read_bool(np, "little-endian"))
73bdb0066dSPankaj Dubey 		syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
74d29ccdb3SPaul Burton 	else if (of_property_read_bool(np, "native-endian"))
75d29ccdb3SPaul Burton 		syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE;
76bdb0066dSPankaj Dubey 
77db2fb60cSDamien Riegel 	/*
78db2fb60cSDamien Riegel 	 * search for reg-io-width property in DT. If it is not provided,
79db2fb60cSDamien Riegel 	 * default to 4 bytes. regmap_init_mmio will return an error if values
80db2fb60cSDamien Riegel 	 * are invalid so there is no need to check them here.
81db2fb60cSDamien Riegel 	 */
82db2fb60cSDamien Riegel 	ret = of_property_read_u32(np, "reg-io-width", &reg_io_width);
83db2fb60cSDamien Riegel 	if (ret)
84db2fb60cSDamien Riegel 		reg_io_width = 4;
85db2fb60cSDamien Riegel 
863bafc09eSBaolin Wang 	ret = of_hwspin_lock_get_id(np, 0);
873bafc09eSBaolin Wang 	if (ret > 0 || (IS_ENABLED(CONFIG_HWSPINLOCK) && ret == 0)) {
883bafc09eSBaolin Wang 		syscon_config.use_hwlock = true;
893bafc09eSBaolin Wang 		syscon_config.hwlock_id = ret;
903bafc09eSBaolin Wang 		syscon_config.hwlock_mode = HWLOCK_IRQSTATE;
913bafc09eSBaolin Wang 	} else if (ret < 0) {
923bafc09eSBaolin Wang 		switch (ret) {
933bafc09eSBaolin Wang 		case -ENOENT:
943bafc09eSBaolin Wang 			/* Ignore missing hwlock, it's optional. */
953bafc09eSBaolin Wang 			break;
963bafc09eSBaolin Wang 		default:
973bafc09eSBaolin Wang 			pr_err("Failed to retrieve valid hwlock: %d\n", ret);
983bafc09eSBaolin Wang 			/* fall-through */
993bafc09eSBaolin Wang 		case -EPROBE_DEFER:
1003bafc09eSBaolin Wang 			goto err_regmap;
1013bafc09eSBaolin Wang 		}
1023bafc09eSBaolin Wang 	}
1033bafc09eSBaolin Wang 
104408d1d57SDavid Lechner 	syscon_config.name = of_node_full_name(np);
105db2fb60cSDamien Riegel 	syscon_config.reg_stride = reg_io_width;
106db2fb60cSDamien Riegel 	syscon_config.val_bits = reg_io_width * 8;
107ca668f0eSPhilipp Zabel 	syscon_config.max_register = resource_size(&res) - reg_io_width;
108500f9ff5SJeffy Chen 	syscon_config.name = of_node_full_name(np);
109db2fb60cSDamien Riegel 
110bdb0066dSPankaj Dubey 	regmap = regmap_init_mmio(NULL, base, &syscon_config);
111bdb0066dSPankaj Dubey 	if (IS_ERR(regmap)) {
112bdb0066dSPankaj Dubey 		pr_err("regmap init failed\n");
113bdb0066dSPankaj Dubey 		ret = PTR_ERR(regmap);
114bdb0066dSPankaj Dubey 		goto err_regmap;
115bdb0066dSPankaj Dubey 	}
116bdb0066dSPankaj Dubey 
117*39233b7cSPaul Cercueil 	if (check_clk) {
118a00406b7SFabrice Gasnier 		clk = of_clk_get(np, 0);
119a00406b7SFabrice Gasnier 		if (IS_ERR(clk)) {
120a00406b7SFabrice Gasnier 			ret = PTR_ERR(clk);
121a00406b7SFabrice Gasnier 			/* clock is optional */
122a00406b7SFabrice Gasnier 			if (ret != -ENOENT)
123a00406b7SFabrice Gasnier 				goto err_clk;
124a00406b7SFabrice Gasnier 		} else {
125a00406b7SFabrice Gasnier 			ret = regmap_mmio_attach_clk(regmap, clk);
126a00406b7SFabrice Gasnier 			if (ret)
127a00406b7SFabrice Gasnier 				goto err_attach;
128a00406b7SFabrice Gasnier 		}
129*39233b7cSPaul Cercueil 	}
130a00406b7SFabrice Gasnier 
131bdb0066dSPankaj Dubey 	syscon->regmap = regmap;
132bdb0066dSPankaj Dubey 	syscon->np = np;
133bdb0066dSPankaj Dubey 
134bdb0066dSPankaj Dubey 	spin_lock(&syscon_list_slock);
135bdb0066dSPankaj Dubey 	list_add_tail(&syscon->list, &syscon_list);
136bdb0066dSPankaj Dubey 	spin_unlock(&syscon_list_slock);
137bdb0066dSPankaj Dubey 
138bdb0066dSPankaj Dubey 	return syscon;
139bdb0066dSPankaj Dubey 
140a00406b7SFabrice Gasnier err_attach:
141a00406b7SFabrice Gasnier 	if (!IS_ERR(clk))
142a00406b7SFabrice Gasnier 		clk_put(clk);
143a00406b7SFabrice Gasnier err_clk:
144a00406b7SFabrice Gasnier 	regmap_exit(regmap);
145bdb0066dSPankaj Dubey err_regmap:
146bdb0066dSPankaj Dubey 	iounmap(base);
147bdb0066dSPankaj Dubey err_map:
148bdb0066dSPankaj Dubey 	kfree(syscon);
149bdb0066dSPankaj Dubey 	return ERR_PTR(ret);
15087d68730SDong Aisheng }
15187d68730SDong Aisheng 
152*39233b7cSPaul Cercueil static struct regmap *device_node_get_regmap(struct device_node *np,
153*39233b7cSPaul Cercueil 					     bool check_clk)
15487d68730SDong Aisheng {
155bdb0066dSPankaj Dubey 	struct syscon *entry, *syscon = NULL;
15687d68730SDong Aisheng 
157bdb0066dSPankaj Dubey 	spin_lock(&syscon_list_slock);
15887d68730SDong Aisheng 
159bdb0066dSPankaj Dubey 	list_for_each_entry(entry, &syscon_list, list)
160bdb0066dSPankaj Dubey 		if (entry->np == np) {
161bdb0066dSPankaj Dubey 			syscon = entry;
162bdb0066dSPankaj Dubey 			break;
163bdb0066dSPankaj Dubey 		}
164bdb0066dSPankaj Dubey 
165bdb0066dSPankaj Dubey 	spin_unlock(&syscon_list_slock);
166bdb0066dSPankaj Dubey 
167bdb0066dSPankaj Dubey 	if (!syscon)
168*39233b7cSPaul Cercueil 		syscon = of_syscon_register(np, check_clk);
169bdb0066dSPankaj Dubey 
170bdb0066dSPankaj Dubey 	if (IS_ERR(syscon))
171bdb0066dSPankaj Dubey 		return ERR_CAST(syscon);
17287d68730SDong Aisheng 
17387d68730SDong Aisheng 	return syscon->regmap;
17487d68730SDong Aisheng }
175*39233b7cSPaul Cercueil 
176*39233b7cSPaul Cercueil struct regmap *device_node_to_regmap(struct device_node *np)
177*39233b7cSPaul Cercueil {
178*39233b7cSPaul Cercueil 	return device_node_get_regmap(np, false);
179*39233b7cSPaul Cercueil }
180*39233b7cSPaul Cercueil EXPORT_SYMBOL_GPL(device_node_to_regmap);
181*39233b7cSPaul Cercueil 
182*39233b7cSPaul Cercueil struct regmap *syscon_node_to_regmap(struct device_node *np)
183*39233b7cSPaul Cercueil {
184*39233b7cSPaul Cercueil 	if (!of_device_is_compatible(np, "syscon"))
185*39233b7cSPaul Cercueil 		return ERR_PTR(-EINVAL);
186*39233b7cSPaul Cercueil 
187*39233b7cSPaul Cercueil 	return device_node_get_regmap(np, true);
188*39233b7cSPaul Cercueil }
18987d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
19087d68730SDong Aisheng 
19187d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
19287d68730SDong Aisheng {
19387d68730SDong Aisheng 	struct device_node *syscon_np;
19487d68730SDong Aisheng 	struct regmap *regmap;
19587d68730SDong Aisheng 
19687d68730SDong Aisheng 	syscon_np = of_find_compatible_node(NULL, NULL, s);
19787d68730SDong Aisheng 	if (!syscon_np)
19887d68730SDong Aisheng 		return ERR_PTR(-ENODEV);
19987d68730SDong Aisheng 
20087d68730SDong Aisheng 	regmap = syscon_node_to_regmap(syscon_np);
20187d68730SDong Aisheng 	of_node_put(syscon_np);
20287d68730SDong Aisheng 
20387d68730SDong Aisheng 	return regmap;
20487d68730SDong Aisheng }
20587d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
20687d68730SDong Aisheng 
20787d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
20887d68730SDong Aisheng 					const char *property)
20987d68730SDong Aisheng {
21087d68730SDong Aisheng 	struct device_node *syscon_np;
21187d68730SDong Aisheng 	struct regmap *regmap;
21287d68730SDong Aisheng 
21345330bb4SPankaj Dubey 	if (property)
21487d68730SDong Aisheng 		syscon_np = of_parse_phandle(np, property, 0);
21545330bb4SPankaj Dubey 	else
21645330bb4SPankaj Dubey 		syscon_np = np;
21745330bb4SPankaj Dubey 
21887d68730SDong Aisheng 	if (!syscon_np)
21987d68730SDong Aisheng 		return ERR_PTR(-ENODEV);
22087d68730SDong Aisheng 
22187d68730SDong Aisheng 	regmap = syscon_node_to_regmap(syscon_np);
22287d68730SDong Aisheng 	of_node_put(syscon_np);
22387d68730SDong Aisheng 
22487d68730SDong Aisheng 	return regmap;
22587d68730SDong Aisheng }
22687d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
22787d68730SDong Aisheng 
228f791be49SBill Pemberton static int syscon_probe(struct platform_device *pdev)
22987d68730SDong Aisheng {
23087d68730SDong Aisheng 	struct device *dev = &pdev->dev;
23129f9b6cfSPawel Moll 	struct syscon_platform_data *pdata = dev_get_platdata(dev);
23287d68730SDong Aisheng 	struct syscon *syscon;
233c131045dSPhilipp Zabel 	struct regmap_config syscon_config = syscon_regmap_config;
2345ab3a89aSAlexander Shiyan 	struct resource *res;
235f10111ccSAlexander Shiyan 	void __iomem *base;
23687d68730SDong Aisheng 
2375ab3a89aSAlexander Shiyan 	syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
23887d68730SDong Aisheng 	if (!syscon)
23987d68730SDong Aisheng 		return -ENOMEM;
24087d68730SDong Aisheng 
2415ab3a89aSAlexander Shiyan 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2425ab3a89aSAlexander Shiyan 	if (!res)
2435ab3a89aSAlexander Shiyan 		return -ENOENT;
2445ab3a89aSAlexander Shiyan 
245f10111ccSAlexander Shiyan 	base = devm_ioremap(dev, res->start, resource_size(res));
246f10111ccSAlexander Shiyan 	if (!base)
2475ab3a89aSAlexander Shiyan 		return -ENOMEM;
24887d68730SDong Aisheng 
249c131045dSPhilipp Zabel 	syscon_config.max_register = res->end - res->start - 3;
25029f9b6cfSPawel Moll 	if (pdata)
251c131045dSPhilipp Zabel 		syscon_config.name = pdata->label;
252c131045dSPhilipp Zabel 	syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config);
25387d68730SDong Aisheng 	if (IS_ERR(syscon->regmap)) {
25487d68730SDong Aisheng 		dev_err(dev, "regmap init failed\n");
25587d68730SDong Aisheng 		return PTR_ERR(syscon->regmap);
25687d68730SDong Aisheng 	}
25787d68730SDong Aisheng 
25887d68730SDong Aisheng 	platform_set_drvdata(pdev, syscon);
25987d68730SDong Aisheng 
26038d8974eSAlexander Shiyan 	dev_dbg(dev, "regmap %pR registered\n", res);
26187d68730SDong Aisheng 
26287d68730SDong Aisheng 	return 0;
26387d68730SDong Aisheng }
26487d68730SDong Aisheng 
2655ab3a89aSAlexander Shiyan static const struct platform_device_id syscon_ids[] = {
2665ab3a89aSAlexander Shiyan 	{ "syscon", },
2675ab3a89aSAlexander Shiyan 	{ }
2685ab3a89aSAlexander Shiyan };
26987d68730SDong Aisheng 
27087d68730SDong Aisheng static struct platform_driver syscon_driver = {
27187d68730SDong Aisheng 	.driver = {
27287d68730SDong Aisheng 		.name = "syscon",
27387d68730SDong Aisheng 	},
27487d68730SDong Aisheng 	.probe		= syscon_probe,
2755ab3a89aSAlexander Shiyan 	.id_table	= syscon_ids,
27687d68730SDong Aisheng };
27787d68730SDong Aisheng 
27887d68730SDong Aisheng static int __init syscon_init(void)
27987d68730SDong Aisheng {
28087d68730SDong Aisheng 	return platform_driver_register(&syscon_driver);
28187d68730SDong Aisheng }
28287d68730SDong Aisheng postcore_initcall(syscon_init);
283