xref: /linux-6.15/drivers/mfd/syscon.c (revision 408d1d57)
187d68730SDong Aisheng /*
287d68730SDong Aisheng  * System Control Driver
387d68730SDong Aisheng  *
487d68730SDong Aisheng  * Copyright (C) 2012 Freescale Semiconductor, Inc.
587d68730SDong Aisheng  * Copyright (C) 2012 Linaro Ltd.
687d68730SDong Aisheng  *
787d68730SDong Aisheng  * Author: Dong Aisheng <[email protected]>
887d68730SDong Aisheng  *
987d68730SDong Aisheng  * This program is free software; you can redistribute it and/or modify
1087d68730SDong Aisheng  * it under the terms of the GNU General Public License as published by
1187d68730SDong Aisheng  * the Free Software Foundation; either version 2 of the License, or
1287d68730SDong Aisheng  * (at your option) any later version.
1387d68730SDong Aisheng  */
1487d68730SDong Aisheng 
1587d68730SDong Aisheng #include <linux/err.h>
163bafc09eSBaolin Wang #include <linux/hwspinlock.h>
1787d68730SDong Aisheng #include <linux/io.h>
1887d68730SDong Aisheng #include <linux/module.h>
19bdb0066dSPankaj Dubey #include <linux/list.h>
2087d68730SDong Aisheng #include <linux/of.h>
2187d68730SDong Aisheng #include <linux/of_address.h>
2287d68730SDong Aisheng #include <linux/of_platform.h>
2329f9b6cfSPawel Moll #include <linux/platform_data/syscon.h>
2487d68730SDong Aisheng #include <linux/platform_device.h>
2587d68730SDong Aisheng #include <linux/regmap.h>
2675177deeSFabio Estevam #include <linux/mfd/syscon.h>
27bdb0066dSPankaj Dubey #include <linux/slab.h>
2887d68730SDong Aisheng 
2987d68730SDong Aisheng static struct platform_driver syscon_driver;
3087d68730SDong Aisheng 
31bdb0066dSPankaj Dubey static DEFINE_SPINLOCK(syscon_list_slock);
32bdb0066dSPankaj Dubey static LIST_HEAD(syscon_list);
33bdb0066dSPankaj Dubey 
3487d68730SDong Aisheng struct syscon {
35bdb0066dSPankaj Dubey 	struct device_node *np;
3687d68730SDong Aisheng 	struct regmap *regmap;
37bdb0066dSPankaj Dubey 	struct list_head list;
3887d68730SDong Aisheng };
3987d68730SDong Aisheng 
40c131045dSPhilipp Zabel static const struct regmap_config syscon_regmap_config = {
41bdb0066dSPankaj Dubey 	.reg_bits = 32,
42bdb0066dSPankaj Dubey 	.val_bits = 32,
43bdb0066dSPankaj Dubey 	.reg_stride = 4,
44bdb0066dSPankaj Dubey };
4587d68730SDong Aisheng 
46bdb0066dSPankaj Dubey static struct syscon *of_syscon_register(struct device_node *np)
47bdb0066dSPankaj Dubey {
48bdb0066dSPankaj Dubey 	struct syscon *syscon;
49bdb0066dSPankaj Dubey 	struct regmap *regmap;
50bdb0066dSPankaj Dubey 	void __iomem *base;
51db2fb60cSDamien Riegel 	u32 reg_io_width;
52bdb0066dSPankaj Dubey 	int ret;
53bdb0066dSPankaj Dubey 	struct regmap_config syscon_config = syscon_regmap_config;
54ca668f0eSPhilipp Zabel 	struct resource res;
55bdb0066dSPankaj Dubey 
56bdb0066dSPankaj Dubey 	if (!of_device_is_compatible(np, "syscon"))
57bdb0066dSPankaj Dubey 		return ERR_PTR(-EINVAL);
58bdb0066dSPankaj Dubey 
59bdb0066dSPankaj Dubey 	syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
60bdb0066dSPankaj Dubey 	if (!syscon)
61bdb0066dSPankaj Dubey 		return ERR_PTR(-ENOMEM);
62bdb0066dSPankaj Dubey 
63ca668f0eSPhilipp Zabel 	if (of_address_to_resource(np, 0, &res)) {
64ca668f0eSPhilipp Zabel 		ret = -ENOMEM;
65ca668f0eSPhilipp Zabel 		goto err_map;
66ca668f0eSPhilipp Zabel 	}
67ca668f0eSPhilipp Zabel 
68ca668f0eSPhilipp Zabel 	base = ioremap(res.start, resource_size(&res));
69bdb0066dSPankaj Dubey 	if (!base) {
70bdb0066dSPankaj Dubey 		ret = -ENOMEM;
71bdb0066dSPankaj Dubey 		goto err_map;
72bdb0066dSPankaj Dubey 	}
73bdb0066dSPankaj Dubey 
74bdb0066dSPankaj Dubey 	/* Parse the device's DT node for an endianness specification */
75bdb0066dSPankaj Dubey 	if (of_property_read_bool(np, "big-endian"))
76bdb0066dSPankaj Dubey 		syscon_config.val_format_endian = REGMAP_ENDIAN_BIG;
77bdb0066dSPankaj Dubey 	else if (of_property_read_bool(np, "little-endian"))
78bdb0066dSPankaj Dubey 		syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
79d29ccdb3SPaul Burton 	else if (of_property_read_bool(np, "native-endian"))
80d29ccdb3SPaul Burton 		syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE;
81bdb0066dSPankaj Dubey 
82db2fb60cSDamien Riegel 	/*
83db2fb60cSDamien Riegel 	 * search for reg-io-width property in DT. If it is not provided,
84db2fb60cSDamien Riegel 	 * default to 4 bytes. regmap_init_mmio will return an error if values
85db2fb60cSDamien Riegel 	 * are invalid so there is no need to check them here.
86db2fb60cSDamien Riegel 	 */
87db2fb60cSDamien Riegel 	ret = of_property_read_u32(np, "reg-io-width", &reg_io_width);
88db2fb60cSDamien Riegel 	if (ret)
89db2fb60cSDamien Riegel 		reg_io_width = 4;
90db2fb60cSDamien Riegel 
913bafc09eSBaolin Wang 	ret = of_hwspin_lock_get_id(np, 0);
923bafc09eSBaolin Wang 	if (ret > 0 || (IS_ENABLED(CONFIG_HWSPINLOCK) && ret == 0)) {
933bafc09eSBaolin Wang 		syscon_config.use_hwlock = true;
943bafc09eSBaolin Wang 		syscon_config.hwlock_id = ret;
953bafc09eSBaolin Wang 		syscon_config.hwlock_mode = HWLOCK_IRQSTATE;
963bafc09eSBaolin Wang 	} else if (ret < 0) {
973bafc09eSBaolin Wang 		switch (ret) {
983bafc09eSBaolin Wang 		case -ENOENT:
993bafc09eSBaolin Wang 			/* Ignore missing hwlock, it's optional. */
1003bafc09eSBaolin Wang 			break;
1013bafc09eSBaolin Wang 		default:
1023bafc09eSBaolin Wang 			pr_err("Failed to retrieve valid hwlock: %d\n", ret);
1033bafc09eSBaolin Wang 			/* fall-through */
1043bafc09eSBaolin Wang 		case -EPROBE_DEFER:
1053bafc09eSBaolin Wang 			goto err_regmap;
1063bafc09eSBaolin Wang 		}
1073bafc09eSBaolin Wang 	}
1083bafc09eSBaolin Wang 
109*408d1d57SDavid Lechner 	syscon_config.name = of_node_full_name(np);
110db2fb60cSDamien Riegel 	syscon_config.reg_stride = reg_io_width;
111db2fb60cSDamien Riegel 	syscon_config.val_bits = reg_io_width * 8;
112ca668f0eSPhilipp Zabel 	syscon_config.max_register = resource_size(&res) - reg_io_width;
113db2fb60cSDamien Riegel 
114bdb0066dSPankaj Dubey 	regmap = regmap_init_mmio(NULL, base, &syscon_config);
115bdb0066dSPankaj Dubey 	if (IS_ERR(regmap)) {
116bdb0066dSPankaj Dubey 		pr_err("regmap init failed\n");
117bdb0066dSPankaj Dubey 		ret = PTR_ERR(regmap);
118bdb0066dSPankaj Dubey 		goto err_regmap;
119bdb0066dSPankaj Dubey 	}
120bdb0066dSPankaj Dubey 
121bdb0066dSPankaj Dubey 	syscon->regmap = regmap;
122bdb0066dSPankaj Dubey 	syscon->np = np;
123bdb0066dSPankaj Dubey 
124bdb0066dSPankaj Dubey 	spin_lock(&syscon_list_slock);
125bdb0066dSPankaj Dubey 	list_add_tail(&syscon->list, &syscon_list);
126bdb0066dSPankaj Dubey 	spin_unlock(&syscon_list_slock);
127bdb0066dSPankaj Dubey 
128bdb0066dSPankaj Dubey 	return syscon;
129bdb0066dSPankaj Dubey 
130bdb0066dSPankaj Dubey err_regmap:
131bdb0066dSPankaj Dubey 	iounmap(base);
132bdb0066dSPankaj Dubey err_map:
133bdb0066dSPankaj Dubey 	kfree(syscon);
134bdb0066dSPankaj Dubey 	return ERR_PTR(ret);
13587d68730SDong Aisheng }
13687d68730SDong Aisheng 
13787d68730SDong Aisheng struct regmap *syscon_node_to_regmap(struct device_node *np)
13887d68730SDong Aisheng {
139bdb0066dSPankaj Dubey 	struct syscon *entry, *syscon = NULL;
14087d68730SDong Aisheng 
141bdb0066dSPankaj Dubey 	spin_lock(&syscon_list_slock);
14287d68730SDong Aisheng 
143bdb0066dSPankaj Dubey 	list_for_each_entry(entry, &syscon_list, list)
144bdb0066dSPankaj Dubey 		if (entry->np == np) {
145bdb0066dSPankaj Dubey 			syscon = entry;
146bdb0066dSPankaj Dubey 			break;
147bdb0066dSPankaj Dubey 		}
148bdb0066dSPankaj Dubey 
149bdb0066dSPankaj Dubey 	spin_unlock(&syscon_list_slock);
150bdb0066dSPankaj Dubey 
151bdb0066dSPankaj Dubey 	if (!syscon)
152bdb0066dSPankaj Dubey 		syscon = of_syscon_register(np);
153bdb0066dSPankaj Dubey 
154bdb0066dSPankaj Dubey 	if (IS_ERR(syscon))
155bdb0066dSPankaj Dubey 		return ERR_CAST(syscon);
15687d68730SDong Aisheng 
15787d68730SDong Aisheng 	return syscon->regmap;
15887d68730SDong Aisheng }
15987d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
16087d68730SDong Aisheng 
16187d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
16287d68730SDong Aisheng {
16387d68730SDong Aisheng 	struct device_node *syscon_np;
16487d68730SDong Aisheng 	struct regmap *regmap;
16587d68730SDong Aisheng 
16687d68730SDong Aisheng 	syscon_np = of_find_compatible_node(NULL, NULL, s);
16787d68730SDong Aisheng 	if (!syscon_np)
16887d68730SDong Aisheng 		return ERR_PTR(-ENODEV);
16987d68730SDong Aisheng 
17087d68730SDong Aisheng 	regmap = syscon_node_to_regmap(syscon_np);
17187d68730SDong Aisheng 	of_node_put(syscon_np);
17287d68730SDong Aisheng 
17387d68730SDong Aisheng 	return regmap;
17487d68730SDong Aisheng }
17587d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
17687d68730SDong Aisheng 
1775ab3a89aSAlexander Shiyan static int syscon_match_pdevname(struct device *dev, void *data)
1785ab3a89aSAlexander Shiyan {
1795ab3a89aSAlexander Shiyan 	return !strcmp(dev_name(dev), (const char *)data);
1805ab3a89aSAlexander Shiyan }
1815ab3a89aSAlexander Shiyan 
1825ab3a89aSAlexander Shiyan struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
1835ab3a89aSAlexander Shiyan {
1845ab3a89aSAlexander Shiyan 	struct device *dev;
1855ab3a89aSAlexander Shiyan 	struct syscon *syscon;
1865ab3a89aSAlexander Shiyan 
1875ab3a89aSAlexander Shiyan 	dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
1885ab3a89aSAlexander Shiyan 				 syscon_match_pdevname);
1895ab3a89aSAlexander Shiyan 	if (!dev)
1905ab3a89aSAlexander Shiyan 		return ERR_PTR(-EPROBE_DEFER);
1915ab3a89aSAlexander Shiyan 
1925ab3a89aSAlexander Shiyan 	syscon = dev_get_drvdata(dev);
1935ab3a89aSAlexander Shiyan 
1945ab3a89aSAlexander Shiyan 	return syscon->regmap;
1955ab3a89aSAlexander Shiyan }
1965ab3a89aSAlexander Shiyan EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
1975ab3a89aSAlexander Shiyan 
19887d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
19987d68730SDong Aisheng 					const char *property)
20087d68730SDong Aisheng {
20187d68730SDong Aisheng 	struct device_node *syscon_np;
20287d68730SDong Aisheng 	struct regmap *regmap;
20387d68730SDong Aisheng 
20445330bb4SPankaj Dubey 	if (property)
20587d68730SDong Aisheng 		syscon_np = of_parse_phandle(np, property, 0);
20645330bb4SPankaj Dubey 	else
20745330bb4SPankaj Dubey 		syscon_np = np;
20845330bb4SPankaj Dubey 
20987d68730SDong Aisheng 	if (!syscon_np)
21087d68730SDong Aisheng 		return ERR_PTR(-ENODEV);
21187d68730SDong Aisheng 
21287d68730SDong Aisheng 	regmap = syscon_node_to_regmap(syscon_np);
21387d68730SDong Aisheng 	of_node_put(syscon_np);
21487d68730SDong Aisheng 
21587d68730SDong Aisheng 	return regmap;
21687d68730SDong Aisheng }
21787d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
21887d68730SDong Aisheng 
219f791be49SBill Pemberton static int syscon_probe(struct platform_device *pdev)
22087d68730SDong Aisheng {
22187d68730SDong Aisheng 	struct device *dev = &pdev->dev;
22229f9b6cfSPawel Moll 	struct syscon_platform_data *pdata = dev_get_platdata(dev);
22387d68730SDong Aisheng 	struct syscon *syscon;
224c131045dSPhilipp Zabel 	struct regmap_config syscon_config = syscon_regmap_config;
2255ab3a89aSAlexander Shiyan 	struct resource *res;
226f10111ccSAlexander Shiyan 	void __iomem *base;
22787d68730SDong Aisheng 
2285ab3a89aSAlexander Shiyan 	syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
22987d68730SDong Aisheng 	if (!syscon)
23087d68730SDong Aisheng 		return -ENOMEM;
23187d68730SDong Aisheng 
2325ab3a89aSAlexander Shiyan 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2335ab3a89aSAlexander Shiyan 	if (!res)
2345ab3a89aSAlexander Shiyan 		return -ENOENT;
2355ab3a89aSAlexander Shiyan 
236f10111ccSAlexander Shiyan 	base = devm_ioremap(dev, res->start, resource_size(res));
237f10111ccSAlexander Shiyan 	if (!base)
2385ab3a89aSAlexander Shiyan 		return -ENOMEM;
23987d68730SDong Aisheng 
240c131045dSPhilipp Zabel 	syscon_config.max_register = res->end - res->start - 3;
24129f9b6cfSPawel Moll 	if (pdata)
242c131045dSPhilipp Zabel 		syscon_config.name = pdata->label;
243c131045dSPhilipp Zabel 	syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config);
24487d68730SDong Aisheng 	if (IS_ERR(syscon->regmap)) {
24587d68730SDong Aisheng 		dev_err(dev, "regmap init failed\n");
24687d68730SDong Aisheng 		return PTR_ERR(syscon->regmap);
24787d68730SDong Aisheng 	}
24887d68730SDong Aisheng 
24987d68730SDong Aisheng 	platform_set_drvdata(pdev, syscon);
25087d68730SDong Aisheng 
25138d8974eSAlexander Shiyan 	dev_dbg(dev, "regmap %pR registered\n", res);
25287d68730SDong Aisheng 
25387d68730SDong Aisheng 	return 0;
25487d68730SDong Aisheng }
25587d68730SDong Aisheng 
2565ab3a89aSAlexander Shiyan static const struct platform_device_id syscon_ids[] = {
2575ab3a89aSAlexander Shiyan 	{ "syscon", },
2585ab3a89aSAlexander Shiyan 	{ }
2595ab3a89aSAlexander Shiyan };
26087d68730SDong Aisheng 
26187d68730SDong Aisheng static struct platform_driver syscon_driver = {
26287d68730SDong Aisheng 	.driver = {
26387d68730SDong Aisheng 		.name = "syscon",
26487d68730SDong Aisheng 	},
26587d68730SDong Aisheng 	.probe		= syscon_probe,
2665ab3a89aSAlexander Shiyan 	.id_table	= syscon_ids,
26787d68730SDong Aisheng };
26887d68730SDong Aisheng 
26987d68730SDong Aisheng static int __init syscon_init(void)
27087d68730SDong Aisheng {
27187d68730SDong Aisheng 	return platform_driver_register(&syscon_driver);
27287d68730SDong Aisheng }
27387d68730SDong Aisheng postcore_initcall(syscon_init);
27487d68730SDong Aisheng 
27587d68730SDong Aisheng static void __exit syscon_exit(void)
27687d68730SDong Aisheng {
27787d68730SDong Aisheng 	platform_driver_unregister(&syscon_driver);
27887d68730SDong Aisheng }
27987d68730SDong Aisheng module_exit(syscon_exit);
28087d68730SDong Aisheng 
28187d68730SDong Aisheng MODULE_AUTHOR("Dong Aisheng <[email protected]>");
28287d68730SDong Aisheng MODULE_DESCRIPTION("System Control driver");
28387d68730SDong Aisheng MODULE_LICENSE("GPL v2");
284