xref: /openbmc/linux/drivers/mfd/syscon.c (revision 060f35a317ef09101b128f399dce7ed13d019461)
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 <dong.aisheng@linaro.org>
987d68730SDong Aisheng  */
1087d68730SDong Aisheng 
114bbe56edSKrzysztof Kozlowski #include <linux/cleanup.h>
12a00406b7SFabrice Gasnier #include <linux/clk.h>
1387d68730SDong Aisheng #include <linux/err.h>
143bafc09eSBaolin Wang #include <linux/hwspinlock.h>
1587d68730SDong Aisheng #include <linux/io.h>
161345da73SPaul Gortmaker #include <linux/init.h>
17bdb0066dSPankaj Dubey #include <linux/list.h>
18*e30d21edSRob Herring (Arm) #include <linux/mutex.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>
257d1e3bd9SJeremy Kerr #include <linux/reset.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 
31*e30d21edSRob Herring (Arm) static DEFINE_MUTEX(syscon_list_lock);
32bdb0066dSPankaj Dubey static LIST_HEAD(syscon_list);
33bdb0066dSPankaj Dubey 
3487d68730SDong Aisheng struct syscon {
35bdb0066dSPankaj Dubey 	struct device_node *np;
3687d68730SDong Aisheng 	struct regmap *regmap;
377d1e3bd9SJeremy Kerr 	struct reset_control *reset;
38bdb0066dSPankaj Dubey 	struct list_head list;
3987d68730SDong Aisheng };
4087d68730SDong Aisheng 
41c131045dSPhilipp Zabel static const struct regmap_config syscon_regmap_config = {
42bdb0066dSPankaj Dubey 	.reg_bits = 32,
43bdb0066dSPankaj Dubey 	.val_bits = 32,
44bdb0066dSPankaj Dubey 	.reg_stride = 4,
45bdb0066dSPankaj Dubey };
4687d68730SDong Aisheng 
of_syscon_register(struct device_node * np,bool check_res)477d1e3bd9SJeremy Kerr static struct syscon *of_syscon_register(struct device_node *np, bool check_res)
48bdb0066dSPankaj Dubey {
49a00406b7SFabrice Gasnier 	struct clk *clk;
50bdb0066dSPankaj Dubey 	struct regmap *regmap;
51bdb0066dSPankaj Dubey 	void __iomem *base;
52db2fb60cSDamien Riegel 	u32 reg_io_width;
53bdb0066dSPankaj Dubey 	int ret;
54bdb0066dSPankaj Dubey 	struct regmap_config syscon_config = syscon_regmap_config;
55ca668f0eSPhilipp Zabel 	struct resource res;
567d1e3bd9SJeremy Kerr 	struct reset_control *reset;
57bdb0066dSPankaj Dubey 
58*e30d21edSRob Herring (Arm) 	WARN_ON(!mutex_is_locked(&syscon_list_lock));
59*e30d21edSRob Herring (Arm) 
604bbe56edSKrzysztof Kozlowski 	struct syscon *syscon __free(kfree) = kzalloc(sizeof(*syscon), GFP_KERNEL);
61bdb0066dSPankaj Dubey 	if (!syscon)
62bdb0066dSPankaj Dubey 		return ERR_PTR(-ENOMEM);
63bdb0066dSPankaj Dubey 
644bbe56edSKrzysztof Kozlowski 	if (of_address_to_resource(np, 0, &res))
654bbe56edSKrzysztof Kozlowski 		return ERR_PTR(-ENOMEM);
66ca668f0eSPhilipp Zabel 
67452d0741SHector Martin 	base = of_iomap(np, 0);
684bbe56edSKrzysztof Kozlowski 	if (!base)
694bbe56edSKrzysztof Kozlowski 		return ERR_PTR(-ENOMEM);
70bdb0066dSPankaj Dubey 
71ca4582c2SJason A. Donenfeld 	/* Parse the device's DT node for an endianness specification */
72ca4582c2SJason A. Donenfeld 	if (of_property_read_bool(np, "big-endian"))
73ca4582c2SJason A. Donenfeld 		syscon_config.val_format_endian = REGMAP_ENDIAN_BIG;
74ca4582c2SJason A. Donenfeld 	else if (of_property_read_bool(np, "little-endian"))
75ca4582c2SJason A. Donenfeld 		syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
76ca4582c2SJason A. Donenfeld 	else if (of_property_read_bool(np, "native-endian"))
77ca4582c2SJason A. Donenfeld 		syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE;
78ca4582c2SJason A. Donenfeld 
79db2fb60cSDamien Riegel 	/*
80db2fb60cSDamien Riegel 	 * search for reg-io-width property in DT. If it is not provided,
81db2fb60cSDamien Riegel 	 * default to 4 bytes. regmap_init_mmio will return an error if values
82db2fb60cSDamien Riegel 	 * are invalid so there is no need to check them here.
83db2fb60cSDamien Riegel 	 */
84db2fb60cSDamien Riegel 	ret = of_property_read_u32(np, "reg-io-width", &reg_io_width);
85db2fb60cSDamien Riegel 	if (ret)
86db2fb60cSDamien Riegel 		reg_io_width = 4;
87db2fb60cSDamien Riegel 
883bafc09eSBaolin Wang 	ret = of_hwspin_lock_get_id(np, 0);
893bafc09eSBaolin Wang 	if (ret > 0 || (IS_ENABLED(CONFIG_HWSPINLOCK) && ret == 0)) {
903bafc09eSBaolin Wang 		syscon_config.use_hwlock = true;
913bafc09eSBaolin Wang 		syscon_config.hwlock_id = ret;
923bafc09eSBaolin Wang 		syscon_config.hwlock_mode = HWLOCK_IRQSTATE;
933bafc09eSBaolin Wang 	} else if (ret < 0) {
943bafc09eSBaolin Wang 		switch (ret) {
953bafc09eSBaolin Wang 		case -ENOENT:
963bafc09eSBaolin Wang 			/* Ignore missing hwlock, it's optional. */
973bafc09eSBaolin Wang 			break;
983bafc09eSBaolin Wang 		default:
993bafc09eSBaolin Wang 			pr_err("Failed to retrieve valid hwlock: %d\n", ret);
100df561f66SGustavo A. R. Silva 			fallthrough;
1013bafc09eSBaolin Wang 		case -EPROBE_DEFER:
1023bafc09eSBaolin Wang 			goto err_regmap;
1033bafc09eSBaolin Wang 		}
1043bafc09eSBaolin Wang 	}
1053bafc09eSBaolin Wang 
1067ff7d5ffSAndy Shevchenko 	syscon_config.name = kasprintf(GFP_KERNEL, "%pOFn@%pa", np, &res.start);
1073ef1130dSKunwu Chan 	if (!syscon_config.name) {
1083ef1130dSKunwu Chan 		ret = -ENOMEM;
1093ef1130dSKunwu Chan 		goto err_regmap;
1103ef1130dSKunwu Chan 	}
111db2fb60cSDamien Riegel 	syscon_config.reg_stride = reg_io_width;
112db2fb60cSDamien Riegel 	syscon_config.val_bits = reg_io_width * 8;
113ca668f0eSPhilipp Zabel 	syscon_config.max_register = resource_size(&res) - reg_io_width;
114db2fb60cSDamien Riegel 
115bdb0066dSPankaj Dubey 	regmap = regmap_init_mmio(NULL, base, &syscon_config);
11656a11881SLimeng 	kfree(syscon_config.name);
117bdb0066dSPankaj Dubey 	if (IS_ERR(regmap)) {
118bdb0066dSPankaj Dubey 		pr_err("regmap init failed\n");
119bdb0066dSPankaj Dubey 		ret = PTR_ERR(regmap);
120bdb0066dSPankaj Dubey 		goto err_regmap;
121bdb0066dSPankaj Dubey 	}
122bdb0066dSPankaj Dubey 
1237d1e3bd9SJeremy Kerr 	if (check_res) {
124a00406b7SFabrice Gasnier 		clk = of_clk_get(np, 0);
125a00406b7SFabrice Gasnier 		if (IS_ERR(clk)) {
126a00406b7SFabrice Gasnier 			ret = PTR_ERR(clk);
127a00406b7SFabrice Gasnier 			/* clock is optional */
128a00406b7SFabrice Gasnier 			if (ret != -ENOENT)
129a00406b7SFabrice Gasnier 				goto err_clk;
130a00406b7SFabrice Gasnier 		} else {
131a00406b7SFabrice Gasnier 			ret = regmap_mmio_attach_clk(regmap, clk);
132a00406b7SFabrice Gasnier 			if (ret)
1337d1e3bd9SJeremy Kerr 				goto err_attach_clk;
134a00406b7SFabrice Gasnier 		}
1357d1e3bd9SJeremy Kerr 
1367d1e3bd9SJeremy Kerr 		reset = of_reset_control_get_optional_exclusive(np, NULL);
1377d1e3bd9SJeremy Kerr 		if (IS_ERR(reset)) {
1387d1e3bd9SJeremy Kerr 			ret = PTR_ERR(reset);
1397d1e3bd9SJeremy Kerr 			goto err_attach_clk;
1407d1e3bd9SJeremy Kerr 		}
1417d1e3bd9SJeremy Kerr 
1427d1e3bd9SJeremy Kerr 		ret = reset_control_deassert(reset);
1437d1e3bd9SJeremy Kerr 		if (ret)
1447d1e3bd9SJeremy Kerr 			goto err_reset;
14539233b7cSPaul Cercueil 	}
146a00406b7SFabrice Gasnier 
147bdb0066dSPankaj Dubey 	syscon->regmap = regmap;
148bdb0066dSPankaj Dubey 	syscon->np = np;
149bdb0066dSPankaj Dubey 
150bdb0066dSPankaj Dubey 	list_add_tail(&syscon->list, &syscon_list);
151bdb0066dSPankaj Dubey 
1524bbe56edSKrzysztof Kozlowski 	return_ptr(syscon);
153bdb0066dSPankaj Dubey 
1547d1e3bd9SJeremy Kerr err_reset:
1557d1e3bd9SJeremy Kerr 	reset_control_put(reset);
1567d1e3bd9SJeremy Kerr err_attach_clk:
157a00406b7SFabrice Gasnier 	if (!IS_ERR(clk))
158a00406b7SFabrice Gasnier 		clk_put(clk);
159a00406b7SFabrice Gasnier err_clk:
160a00406b7SFabrice Gasnier 	regmap_exit(regmap);
161bdb0066dSPankaj Dubey err_regmap:
162bdb0066dSPankaj Dubey 	iounmap(base);
163bdb0066dSPankaj Dubey 	return ERR_PTR(ret);
16487d68730SDong Aisheng }
16587d68730SDong Aisheng 
device_node_get_regmap(struct device_node * np,bool check_res)16639233b7cSPaul Cercueil static struct regmap *device_node_get_regmap(struct device_node *np,
1677d1e3bd9SJeremy Kerr 					     bool check_res)
16887d68730SDong Aisheng {
169bdb0066dSPankaj Dubey 	struct syscon *entry, *syscon = NULL;
17087d68730SDong Aisheng 
171*e30d21edSRob Herring (Arm) 	mutex_lock(&syscon_list_lock);
17287d68730SDong Aisheng 
173bdb0066dSPankaj Dubey 	list_for_each_entry(entry, &syscon_list, list)
174bdb0066dSPankaj Dubey 		if (entry->np == np) {
175bdb0066dSPankaj Dubey 			syscon = entry;
176bdb0066dSPankaj Dubey 			break;
177bdb0066dSPankaj Dubey 		}
178bdb0066dSPankaj Dubey 
179bdb0066dSPankaj Dubey 	if (!syscon)
1807d1e3bd9SJeremy Kerr 		syscon = of_syscon_register(np, check_res);
181bdb0066dSPankaj Dubey 
182*e30d21edSRob Herring (Arm) 	mutex_unlock(&syscon_list_lock);
183*e30d21edSRob Herring (Arm) 
184bdb0066dSPankaj Dubey 	if (IS_ERR(syscon))
185bdb0066dSPankaj Dubey 		return ERR_CAST(syscon);
18687d68730SDong Aisheng 
18787d68730SDong Aisheng 	return syscon->regmap;
18887d68730SDong Aisheng }
18939233b7cSPaul Cercueil 
190b45fd493SPeter Griffin /**
191b45fd493SPeter Griffin  * of_syscon_register_regmap() - Register regmap for specified device node
192b45fd493SPeter Griffin  * @np: Device tree node
193b45fd493SPeter Griffin  * @regmap: Pointer to regmap object
194b45fd493SPeter Griffin  *
195b45fd493SPeter Griffin  * Register an externally created regmap object with syscon for the specified
196b45fd493SPeter Griffin  * device tree node. This regmap will then be returned to client drivers using
197b45fd493SPeter Griffin  * the syscon_regmap_lookup_by_phandle() API.
198b45fd493SPeter Griffin  *
199b45fd493SPeter Griffin  * Return: 0 on success, negative error code on failure.
200b45fd493SPeter Griffin  */
of_syscon_register_regmap(struct device_node * np,struct regmap * regmap)201b45fd493SPeter Griffin int of_syscon_register_regmap(struct device_node *np, struct regmap *regmap)
202b45fd493SPeter Griffin {
203b45fd493SPeter Griffin 	struct syscon *entry, *syscon = NULL;
204b45fd493SPeter Griffin 	int ret;
205b45fd493SPeter Griffin 
206b45fd493SPeter Griffin 	if (!np || !regmap)
207b45fd493SPeter Griffin 		return -EINVAL;
208b45fd493SPeter Griffin 
209b45fd493SPeter Griffin 	syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
210b45fd493SPeter Griffin 	if (!syscon)
211b45fd493SPeter Griffin 		return -ENOMEM;
212b45fd493SPeter Griffin 
213b45fd493SPeter Griffin 	/* check if syscon entry already exists */
214*e30d21edSRob Herring (Arm) 	mutex_lock(&syscon_list_lock);
215b45fd493SPeter Griffin 
216b45fd493SPeter Griffin 	list_for_each_entry(entry, &syscon_list, list)
217b45fd493SPeter Griffin 		if (entry->np == np) {
218b45fd493SPeter Griffin 			ret = -EEXIST;
219b45fd493SPeter Griffin 			goto err_unlock;
220b45fd493SPeter Griffin 		}
221b45fd493SPeter Griffin 
222b45fd493SPeter Griffin 	syscon->regmap = regmap;
223b45fd493SPeter Griffin 	syscon->np = np;
224b45fd493SPeter Griffin 
225b45fd493SPeter Griffin 	/* register the regmap in syscon list */
226b45fd493SPeter Griffin 	list_add_tail(&syscon->list, &syscon_list);
227*e30d21edSRob Herring (Arm) 	mutex_unlock(&syscon_list_lock);
228b45fd493SPeter Griffin 
229b45fd493SPeter Griffin 	return 0;
230b45fd493SPeter Griffin 
231b45fd493SPeter Griffin err_unlock:
232*e30d21edSRob Herring (Arm) 	mutex_unlock(&syscon_list_lock);
233b45fd493SPeter Griffin 	kfree(syscon);
234b45fd493SPeter Griffin 	return ret;
235b45fd493SPeter Griffin }
236b45fd493SPeter Griffin EXPORT_SYMBOL_GPL(of_syscon_register_regmap);
237b45fd493SPeter Griffin 
device_node_to_regmap(struct device_node * np)23839233b7cSPaul Cercueil struct regmap *device_node_to_regmap(struct device_node *np)
23939233b7cSPaul Cercueil {
24039233b7cSPaul Cercueil 	return device_node_get_regmap(np, false);
24139233b7cSPaul Cercueil }
24239233b7cSPaul Cercueil EXPORT_SYMBOL_GPL(device_node_to_regmap);
24339233b7cSPaul Cercueil 
syscon_node_to_regmap(struct device_node * np)24439233b7cSPaul Cercueil struct regmap *syscon_node_to_regmap(struct device_node *np)
24539233b7cSPaul Cercueil {
24639233b7cSPaul Cercueil 	if (!of_device_is_compatible(np, "syscon"))
24739233b7cSPaul Cercueil 		return ERR_PTR(-EINVAL);
24839233b7cSPaul Cercueil 
24939233b7cSPaul Cercueil 	return device_node_get_regmap(np, true);
25039233b7cSPaul Cercueil }
25187d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
25287d68730SDong Aisheng 
syscon_regmap_lookup_by_compatible(const char * s)25387d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
25487d68730SDong Aisheng {
25587d68730SDong Aisheng 	struct device_node *syscon_np;
25687d68730SDong Aisheng 	struct regmap *regmap;
25787d68730SDong Aisheng 
25887d68730SDong Aisheng 	syscon_np = of_find_compatible_node(NULL, NULL, s);
25987d68730SDong Aisheng 	if (!syscon_np)
26087d68730SDong Aisheng 		return ERR_PTR(-ENODEV);
26187d68730SDong Aisheng 
26287d68730SDong Aisheng 	regmap = syscon_node_to_regmap(syscon_np);
26387d68730SDong Aisheng 	of_node_put(syscon_np);
26487d68730SDong Aisheng 
26587d68730SDong Aisheng 	return regmap;
26687d68730SDong Aisheng }
26787d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
26887d68730SDong Aisheng 
syscon_regmap_lookup_by_phandle(struct device_node * np,const char * property)26987d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
27087d68730SDong Aisheng 					const char *property)
27187d68730SDong Aisheng {
27287d68730SDong Aisheng 	struct device_node *syscon_np;
27387d68730SDong Aisheng 	struct regmap *regmap;
27487d68730SDong Aisheng 
27545330bb4SPankaj Dubey 	if (property)
27687d68730SDong Aisheng 		syscon_np = of_parse_phandle(np, property, 0);
27745330bb4SPankaj Dubey 	else
27845330bb4SPankaj Dubey 		syscon_np = np;
27945330bb4SPankaj Dubey 
28087d68730SDong Aisheng 	if (!syscon_np)
28187d68730SDong Aisheng 		return ERR_PTR(-ENODEV);
28287d68730SDong Aisheng 
28387d68730SDong Aisheng 	regmap = syscon_node_to_regmap(syscon_np);
2841b01e66cSPeter Griffin 
2851b01e66cSPeter Griffin 	if (property)
28687d68730SDong Aisheng 		of_node_put(syscon_np);
28787d68730SDong Aisheng 
28887d68730SDong Aisheng 	return regmap;
28987d68730SDong Aisheng }
29087d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
29187d68730SDong Aisheng 
syscon_regmap_lookup_by_phandle_args(struct device_node * np,const char * property,int arg_count,unsigned int * out_args)2926a24f567SOrson Zhai struct regmap *syscon_regmap_lookup_by_phandle_args(struct device_node *np,
2936a24f567SOrson Zhai 					const char *property,
2946a24f567SOrson Zhai 					int arg_count,
2956a24f567SOrson Zhai 					unsigned int *out_args)
2966a24f567SOrson Zhai {
2976a24f567SOrson Zhai 	struct device_node *syscon_np;
2986a24f567SOrson Zhai 	struct of_phandle_args args;
2996a24f567SOrson Zhai 	struct regmap *regmap;
3006a24f567SOrson Zhai 	unsigned int index;
3016a24f567SOrson Zhai 	int rc;
3026a24f567SOrson Zhai 
3036a24f567SOrson Zhai 	rc = of_parse_phandle_with_fixed_args(np, property, arg_count,
3046a24f567SOrson Zhai 			0, &args);
3056a24f567SOrson Zhai 	if (rc)
3066a24f567SOrson Zhai 		return ERR_PTR(rc);
3076a24f567SOrson Zhai 
3086a24f567SOrson Zhai 	syscon_np = args.np;
3096a24f567SOrson Zhai 	if (!syscon_np)
3106a24f567SOrson Zhai 		return ERR_PTR(-ENODEV);
3116a24f567SOrson Zhai 
3126a24f567SOrson Zhai 	regmap = syscon_node_to_regmap(syscon_np);
3136a24f567SOrson Zhai 	for (index = 0; index < arg_count; index++)
3146a24f567SOrson Zhai 		out_args[index] = args.args[index];
3156a24f567SOrson Zhai 	of_node_put(syscon_np);
3166a24f567SOrson Zhai 
3176a24f567SOrson Zhai 	return regmap;
3186a24f567SOrson Zhai }
3196a24f567SOrson Zhai EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_args);
3206a24f567SOrson Zhai 
32186b9d170SEnric Balletbo i Serra /*
32286b9d170SEnric Balletbo i Serra  * It behaves the same as syscon_regmap_lookup_by_phandle() except where
32386b9d170SEnric Balletbo i Serra  * there is no regmap phandle. In this case, instead of returning -ENODEV,
32486b9d170SEnric Balletbo i Serra  * the function returns NULL.
32586b9d170SEnric Balletbo i Serra  */
syscon_regmap_lookup_by_phandle_optional(struct device_node * np,const char * property)32686b9d170SEnric Balletbo i Serra struct regmap *syscon_regmap_lookup_by_phandle_optional(struct device_node *np,
32786b9d170SEnric Balletbo i Serra 					const char *property)
32886b9d170SEnric Balletbo i Serra {
32986b9d170SEnric Balletbo i Serra 	struct regmap *regmap;
33086b9d170SEnric Balletbo i Serra 
33186b9d170SEnric Balletbo i Serra 	regmap = syscon_regmap_lookup_by_phandle(np, property);
33286b9d170SEnric Balletbo i Serra 	if (IS_ERR(regmap) && PTR_ERR(regmap) == -ENODEV)
33386b9d170SEnric Balletbo i Serra 		return NULL;
33486b9d170SEnric Balletbo i Serra 
33586b9d170SEnric Balletbo i Serra 	return regmap;
33686b9d170SEnric Balletbo i Serra }
33786b9d170SEnric Balletbo i Serra EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_optional);
33886b9d170SEnric Balletbo i Serra 
syscon_probe(struct platform_device * pdev)339f791be49SBill Pemberton static int syscon_probe(struct platform_device *pdev)
34087d68730SDong Aisheng {
34187d68730SDong Aisheng 	struct device *dev = &pdev->dev;
34229f9b6cfSPawel Moll 	struct syscon_platform_data *pdata = dev_get_platdata(dev);
34387d68730SDong Aisheng 	struct syscon *syscon;
344c131045dSPhilipp Zabel 	struct regmap_config syscon_config = syscon_regmap_config;
3455ab3a89aSAlexander Shiyan 	struct resource *res;
346f10111ccSAlexander Shiyan 	void __iomem *base;
34787d68730SDong Aisheng 
3485ab3a89aSAlexander Shiyan 	syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
34987d68730SDong Aisheng 	if (!syscon)
35087d68730SDong Aisheng 		return -ENOMEM;
35187d68730SDong Aisheng 
3525ab3a89aSAlexander Shiyan 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3535ab3a89aSAlexander Shiyan 	if (!res)
3545ab3a89aSAlexander Shiyan 		return -ENOENT;
3555ab3a89aSAlexander Shiyan 
356f10111ccSAlexander Shiyan 	base = devm_ioremap(dev, res->start, resource_size(res));
357f10111ccSAlexander Shiyan 	if (!base)
3585ab3a89aSAlexander Shiyan 		return -ENOMEM;
35987d68730SDong Aisheng 
360f2a19c5bSAndy Shevchenko 	syscon_config.max_register = resource_size(res) - 4;
36129f9b6cfSPawel Moll 	if (pdata)
362c131045dSPhilipp Zabel 		syscon_config.name = pdata->label;
363c131045dSPhilipp Zabel 	syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config);
36487d68730SDong Aisheng 	if (IS_ERR(syscon->regmap)) {
36587d68730SDong Aisheng 		dev_err(dev, "regmap init failed\n");
36687d68730SDong Aisheng 		return PTR_ERR(syscon->regmap);
36787d68730SDong Aisheng 	}
36887d68730SDong Aisheng 
36987d68730SDong Aisheng 	platform_set_drvdata(pdev, syscon);
37087d68730SDong Aisheng 
37138d8974eSAlexander Shiyan 	dev_dbg(dev, "regmap %pR registered\n", res);
37287d68730SDong Aisheng 
37387d68730SDong Aisheng 	return 0;
37487d68730SDong Aisheng }
37587d68730SDong Aisheng 
3765ab3a89aSAlexander Shiyan static const struct platform_device_id syscon_ids[] = {
3775ab3a89aSAlexander Shiyan 	{ "syscon", },
3785ab3a89aSAlexander Shiyan 	{ }
3795ab3a89aSAlexander Shiyan };
38087d68730SDong Aisheng 
38187d68730SDong Aisheng static struct platform_driver syscon_driver = {
38287d68730SDong Aisheng 	.driver = {
38387d68730SDong Aisheng 		.name = "syscon",
38487d68730SDong Aisheng 	},
38587d68730SDong Aisheng 	.probe		= syscon_probe,
3865ab3a89aSAlexander Shiyan 	.id_table	= syscon_ids,
38787d68730SDong Aisheng };
38887d68730SDong Aisheng 
syscon_init(void)38987d68730SDong Aisheng static int __init syscon_init(void)
39087d68730SDong Aisheng {
39187d68730SDong Aisheng 	return platform_driver_register(&syscon_driver);
39287d68730SDong Aisheng }
39387d68730SDong Aisheng postcore_initcall(syscon_init);
394