xref: /linux-6.15/drivers/mfd/syscon.c (revision d29ccdb3)
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>
1687d68730SDong Aisheng #include <linux/io.h>
1787d68730SDong Aisheng #include <linux/module.h>
18bdb0066dSPankaj Dubey #include <linux/list.h>
1987d68730SDong Aisheng #include <linux/of.h>
2087d68730SDong Aisheng #include <linux/of_address.h>
2187d68730SDong Aisheng #include <linux/of_platform.h>
2229f9b6cfSPawel Moll #include <linux/platform_data/syscon.h>
2387d68730SDong Aisheng #include <linux/platform_device.h>
2487d68730SDong Aisheng #include <linux/regmap.h>
2575177deeSFabio Estevam #include <linux/mfd/syscon.h>
26bdb0066dSPankaj Dubey #include <linux/slab.h>
2787d68730SDong Aisheng 
2887d68730SDong Aisheng static struct platform_driver syscon_driver;
2987d68730SDong Aisheng 
30bdb0066dSPankaj Dubey static DEFINE_SPINLOCK(syscon_list_slock);
31bdb0066dSPankaj Dubey static LIST_HEAD(syscon_list);
32bdb0066dSPankaj Dubey 
3387d68730SDong Aisheng struct syscon {
34bdb0066dSPankaj Dubey 	struct device_node *np;
3587d68730SDong Aisheng 	struct regmap *regmap;
36bdb0066dSPankaj Dubey 	struct list_head list;
3787d68730SDong Aisheng };
3887d68730SDong Aisheng 
39c131045dSPhilipp Zabel static const struct regmap_config syscon_regmap_config = {
40bdb0066dSPankaj Dubey 	.reg_bits = 32,
41bdb0066dSPankaj Dubey 	.val_bits = 32,
42bdb0066dSPankaj Dubey 	.reg_stride = 4,
43bdb0066dSPankaj Dubey };
4487d68730SDong Aisheng 
45bdb0066dSPankaj Dubey static struct syscon *of_syscon_register(struct device_node *np)
46bdb0066dSPankaj Dubey {
47bdb0066dSPankaj Dubey 	struct syscon *syscon;
48bdb0066dSPankaj Dubey 	struct regmap *regmap;
49bdb0066dSPankaj Dubey 	void __iomem *base;
50db2fb60cSDamien Riegel 	u32 reg_io_width;
51bdb0066dSPankaj Dubey 	int ret;
52bdb0066dSPankaj Dubey 	struct regmap_config syscon_config = syscon_regmap_config;
53ca668f0eSPhilipp Zabel 	struct resource res;
54bdb0066dSPankaj Dubey 
55bdb0066dSPankaj Dubey 	if (!of_device_is_compatible(np, "syscon"))
56bdb0066dSPankaj Dubey 		return ERR_PTR(-EINVAL);
57bdb0066dSPankaj Dubey 
58bdb0066dSPankaj Dubey 	syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
59bdb0066dSPankaj Dubey 	if (!syscon)
60bdb0066dSPankaj Dubey 		return ERR_PTR(-ENOMEM);
61bdb0066dSPankaj Dubey 
62ca668f0eSPhilipp Zabel 	if (of_address_to_resource(np, 0, &res)) {
63ca668f0eSPhilipp Zabel 		ret = -ENOMEM;
64ca668f0eSPhilipp Zabel 		goto err_map;
65ca668f0eSPhilipp Zabel 	}
66ca668f0eSPhilipp Zabel 
67ca668f0eSPhilipp Zabel 	base = ioremap(res.start, resource_size(&res));
68bdb0066dSPankaj Dubey 	if (!base) {
69bdb0066dSPankaj Dubey 		ret = -ENOMEM;
70bdb0066dSPankaj Dubey 		goto err_map;
71bdb0066dSPankaj Dubey 	}
72bdb0066dSPankaj Dubey 
73bdb0066dSPankaj Dubey 	/* Parse the device's DT node for an endianness specification */
74bdb0066dSPankaj Dubey 	if (of_property_read_bool(np, "big-endian"))
75bdb0066dSPankaj Dubey 		syscon_config.val_format_endian = REGMAP_ENDIAN_BIG;
76bdb0066dSPankaj Dubey 	else if (of_property_read_bool(np, "little-endian"))
77bdb0066dSPankaj Dubey 		syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
78*d29ccdb3SPaul Burton 	else if (of_property_read_bool(np, "native-endian"))
79*d29ccdb3SPaul Burton 		syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE;
80bdb0066dSPankaj Dubey 
81db2fb60cSDamien Riegel 	/*
82db2fb60cSDamien Riegel 	 * search for reg-io-width property in DT. If it is not provided,
83db2fb60cSDamien Riegel 	 * default to 4 bytes. regmap_init_mmio will return an error if values
84db2fb60cSDamien Riegel 	 * are invalid so there is no need to check them here.
85db2fb60cSDamien Riegel 	 */
86db2fb60cSDamien Riegel 	ret = of_property_read_u32(np, "reg-io-width", &reg_io_width);
87db2fb60cSDamien Riegel 	if (ret)
88db2fb60cSDamien Riegel 		reg_io_width = 4;
89db2fb60cSDamien Riegel 
90db2fb60cSDamien Riegel 	syscon_config.reg_stride = reg_io_width;
91db2fb60cSDamien Riegel 	syscon_config.val_bits = reg_io_width * 8;
92ca668f0eSPhilipp Zabel 	syscon_config.max_register = resource_size(&res) - reg_io_width;
93db2fb60cSDamien Riegel 
94bdb0066dSPankaj Dubey 	regmap = regmap_init_mmio(NULL, base, &syscon_config);
95bdb0066dSPankaj Dubey 	if (IS_ERR(regmap)) {
96bdb0066dSPankaj Dubey 		pr_err("regmap init failed\n");
97bdb0066dSPankaj Dubey 		ret = PTR_ERR(regmap);
98bdb0066dSPankaj Dubey 		goto err_regmap;
99bdb0066dSPankaj Dubey 	}
100bdb0066dSPankaj Dubey 
101bdb0066dSPankaj Dubey 	syscon->regmap = regmap;
102bdb0066dSPankaj Dubey 	syscon->np = np;
103bdb0066dSPankaj Dubey 
104bdb0066dSPankaj Dubey 	spin_lock(&syscon_list_slock);
105bdb0066dSPankaj Dubey 	list_add_tail(&syscon->list, &syscon_list);
106bdb0066dSPankaj Dubey 	spin_unlock(&syscon_list_slock);
107bdb0066dSPankaj Dubey 
108bdb0066dSPankaj Dubey 	return syscon;
109bdb0066dSPankaj Dubey 
110bdb0066dSPankaj Dubey err_regmap:
111bdb0066dSPankaj Dubey 	iounmap(base);
112bdb0066dSPankaj Dubey err_map:
113bdb0066dSPankaj Dubey 	kfree(syscon);
114bdb0066dSPankaj Dubey 	return ERR_PTR(ret);
11587d68730SDong Aisheng }
11687d68730SDong Aisheng 
11787d68730SDong Aisheng struct regmap *syscon_node_to_regmap(struct device_node *np)
11887d68730SDong Aisheng {
119bdb0066dSPankaj Dubey 	struct syscon *entry, *syscon = NULL;
12087d68730SDong Aisheng 
121bdb0066dSPankaj Dubey 	spin_lock(&syscon_list_slock);
12287d68730SDong Aisheng 
123bdb0066dSPankaj Dubey 	list_for_each_entry(entry, &syscon_list, list)
124bdb0066dSPankaj Dubey 		if (entry->np == np) {
125bdb0066dSPankaj Dubey 			syscon = entry;
126bdb0066dSPankaj Dubey 			break;
127bdb0066dSPankaj Dubey 		}
128bdb0066dSPankaj Dubey 
129bdb0066dSPankaj Dubey 	spin_unlock(&syscon_list_slock);
130bdb0066dSPankaj Dubey 
131bdb0066dSPankaj Dubey 	if (!syscon)
132bdb0066dSPankaj Dubey 		syscon = of_syscon_register(np);
133bdb0066dSPankaj Dubey 
134bdb0066dSPankaj Dubey 	if (IS_ERR(syscon))
135bdb0066dSPankaj Dubey 		return ERR_CAST(syscon);
13687d68730SDong Aisheng 
13787d68730SDong Aisheng 	return syscon->regmap;
13887d68730SDong Aisheng }
13987d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
14087d68730SDong Aisheng 
14187d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
14287d68730SDong Aisheng {
14387d68730SDong Aisheng 	struct device_node *syscon_np;
14487d68730SDong Aisheng 	struct regmap *regmap;
14587d68730SDong Aisheng 
14687d68730SDong Aisheng 	syscon_np = of_find_compatible_node(NULL, NULL, s);
14787d68730SDong Aisheng 	if (!syscon_np)
14887d68730SDong Aisheng 		return ERR_PTR(-ENODEV);
14987d68730SDong Aisheng 
15087d68730SDong Aisheng 	regmap = syscon_node_to_regmap(syscon_np);
15187d68730SDong Aisheng 	of_node_put(syscon_np);
15287d68730SDong Aisheng 
15387d68730SDong Aisheng 	return regmap;
15487d68730SDong Aisheng }
15587d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
15687d68730SDong Aisheng 
1575ab3a89aSAlexander Shiyan static int syscon_match_pdevname(struct device *dev, void *data)
1585ab3a89aSAlexander Shiyan {
1595ab3a89aSAlexander Shiyan 	return !strcmp(dev_name(dev), (const char *)data);
1605ab3a89aSAlexander Shiyan }
1615ab3a89aSAlexander Shiyan 
1625ab3a89aSAlexander Shiyan struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
1635ab3a89aSAlexander Shiyan {
1645ab3a89aSAlexander Shiyan 	struct device *dev;
1655ab3a89aSAlexander Shiyan 	struct syscon *syscon;
1665ab3a89aSAlexander Shiyan 
1675ab3a89aSAlexander Shiyan 	dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
1685ab3a89aSAlexander Shiyan 				 syscon_match_pdevname);
1695ab3a89aSAlexander Shiyan 	if (!dev)
1705ab3a89aSAlexander Shiyan 		return ERR_PTR(-EPROBE_DEFER);
1715ab3a89aSAlexander Shiyan 
1725ab3a89aSAlexander Shiyan 	syscon = dev_get_drvdata(dev);
1735ab3a89aSAlexander Shiyan 
1745ab3a89aSAlexander Shiyan 	return syscon->regmap;
1755ab3a89aSAlexander Shiyan }
1765ab3a89aSAlexander Shiyan EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
1775ab3a89aSAlexander Shiyan 
17887d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
17987d68730SDong Aisheng 					const char *property)
18087d68730SDong Aisheng {
18187d68730SDong Aisheng 	struct device_node *syscon_np;
18287d68730SDong Aisheng 	struct regmap *regmap;
18387d68730SDong Aisheng 
18445330bb4SPankaj Dubey 	if (property)
18587d68730SDong Aisheng 		syscon_np = of_parse_phandle(np, property, 0);
18645330bb4SPankaj Dubey 	else
18745330bb4SPankaj Dubey 		syscon_np = np;
18845330bb4SPankaj Dubey 
18987d68730SDong Aisheng 	if (!syscon_np)
19087d68730SDong Aisheng 		return ERR_PTR(-ENODEV);
19187d68730SDong Aisheng 
19287d68730SDong Aisheng 	regmap = syscon_node_to_regmap(syscon_np);
19387d68730SDong Aisheng 	of_node_put(syscon_np);
19487d68730SDong Aisheng 
19587d68730SDong Aisheng 	return regmap;
19687d68730SDong Aisheng }
19787d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
19887d68730SDong Aisheng 
199f791be49SBill Pemberton static int syscon_probe(struct platform_device *pdev)
20087d68730SDong Aisheng {
20187d68730SDong Aisheng 	struct device *dev = &pdev->dev;
20229f9b6cfSPawel Moll 	struct syscon_platform_data *pdata = dev_get_platdata(dev);
20387d68730SDong Aisheng 	struct syscon *syscon;
204c131045dSPhilipp Zabel 	struct regmap_config syscon_config = syscon_regmap_config;
2055ab3a89aSAlexander Shiyan 	struct resource *res;
206f10111ccSAlexander Shiyan 	void __iomem *base;
20787d68730SDong Aisheng 
2085ab3a89aSAlexander Shiyan 	syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
20987d68730SDong Aisheng 	if (!syscon)
21087d68730SDong Aisheng 		return -ENOMEM;
21187d68730SDong Aisheng 
2125ab3a89aSAlexander Shiyan 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2135ab3a89aSAlexander Shiyan 	if (!res)
2145ab3a89aSAlexander Shiyan 		return -ENOENT;
2155ab3a89aSAlexander Shiyan 
216f10111ccSAlexander Shiyan 	base = devm_ioremap(dev, res->start, resource_size(res));
217f10111ccSAlexander Shiyan 	if (!base)
2185ab3a89aSAlexander Shiyan 		return -ENOMEM;
21987d68730SDong Aisheng 
220c131045dSPhilipp Zabel 	syscon_config.max_register = res->end - res->start - 3;
22129f9b6cfSPawel Moll 	if (pdata)
222c131045dSPhilipp Zabel 		syscon_config.name = pdata->label;
223c131045dSPhilipp Zabel 	syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config);
22487d68730SDong Aisheng 	if (IS_ERR(syscon->regmap)) {
22587d68730SDong Aisheng 		dev_err(dev, "regmap init failed\n");
22687d68730SDong Aisheng 		return PTR_ERR(syscon->regmap);
22787d68730SDong Aisheng 	}
22887d68730SDong Aisheng 
22987d68730SDong Aisheng 	platform_set_drvdata(pdev, syscon);
23087d68730SDong Aisheng 
23138d8974eSAlexander Shiyan 	dev_dbg(dev, "regmap %pR registered\n", res);
23287d68730SDong Aisheng 
23387d68730SDong Aisheng 	return 0;
23487d68730SDong Aisheng }
23587d68730SDong Aisheng 
2365ab3a89aSAlexander Shiyan static const struct platform_device_id syscon_ids[] = {
2375ab3a89aSAlexander Shiyan 	{ "syscon", },
2385ab3a89aSAlexander Shiyan 	{ }
2395ab3a89aSAlexander Shiyan };
24087d68730SDong Aisheng 
24187d68730SDong Aisheng static struct platform_driver syscon_driver = {
24287d68730SDong Aisheng 	.driver = {
24387d68730SDong Aisheng 		.name = "syscon",
24487d68730SDong Aisheng 	},
24587d68730SDong Aisheng 	.probe		= syscon_probe,
2465ab3a89aSAlexander Shiyan 	.id_table	= syscon_ids,
24787d68730SDong Aisheng };
24887d68730SDong Aisheng 
24987d68730SDong Aisheng static int __init syscon_init(void)
25087d68730SDong Aisheng {
25187d68730SDong Aisheng 	return platform_driver_register(&syscon_driver);
25287d68730SDong Aisheng }
25387d68730SDong Aisheng postcore_initcall(syscon_init);
25487d68730SDong Aisheng 
25587d68730SDong Aisheng static void __exit syscon_exit(void)
25687d68730SDong Aisheng {
25787d68730SDong Aisheng 	platform_driver_unregister(&syscon_driver);
25887d68730SDong Aisheng }
25987d68730SDong Aisheng module_exit(syscon_exit);
26087d68730SDong Aisheng 
26187d68730SDong Aisheng MODULE_AUTHOR("Dong Aisheng <[email protected]>");
26287d68730SDong Aisheng MODULE_DESCRIPTION("System Control driver");
26387d68730SDong Aisheng MODULE_LICENSE("GPL v2");
264