xref: /openbmc/u-boot/drivers/core/syscon-uclass.c (revision 9d922450)
1 /*
2  * Copyright (C) 2015 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <syscon.h>
10 #include <dm.h>
11 #include <errno.h>
12 #include <regmap.h>
13 #include <dm/device-internal.h>
14 #include <dm/lists.h>
15 #include <dm/root.h>
16 #include <linux/err.h>
17 
18 struct regmap *syscon_get_regmap(struct udevice *dev)
19 {
20 	struct syscon_uc_info *priv;
21 
22 	if (device_get_uclass_id(dev) != UCLASS_SYSCON)
23 		return ERR_PTR(-ENOEXEC);
24 	priv = dev_get_uclass_priv(dev);
25 	return priv->regmap;
26 }
27 
28 static int syscon_pre_probe(struct udevice *dev)
29 {
30 	struct syscon_uc_info *priv = dev_get_uclass_priv(dev);
31 
32 	/*
33 	 * With OF_PLATDATA we really have no way of knowing the format of
34 	 * the device-specific platform data. So we assume that it starts with
35 	 * a 'reg' member, and this holds a single address and size. Drivers
36 	 * using OF_PLATDATA will need to ensure that this is true.
37 	 */
38 #if CONFIG_IS_ENABLED(OF_PLATDATA)
39 	struct syscon_base_platdata *plat = dev_get_platdata(dev);
40 
41 	return regmap_init_mem_platdata(dev, plat->reg, ARRAY_SIZE(plat->reg),
42 					&priv->regmap);
43 #else
44 	return regmap_init_mem(dev, &priv->regmap);
45 #endif
46 }
47 
48 int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
49 {
50 	struct udevice *dev;
51 	struct uclass *uc;
52 	int ret;
53 
54 	*devp = NULL;
55 	ret = uclass_get(UCLASS_SYSCON, &uc);
56 	if (ret)
57 		return ret;
58 	uclass_foreach_dev(dev, uc) {
59 		if (dev->driver_data == driver_data) {
60 			*devp = dev;
61 			return device_probe(dev);
62 		}
63 	}
64 
65 	return -ENODEV;
66 }
67 
68 struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
69 {
70 	struct syscon_uc_info *priv;
71 	struct udevice *dev;
72 	int ret;
73 
74 	ret = syscon_get_by_driver_data(driver_data, &dev);
75 	if (ret)
76 		return ERR_PTR(ret);
77 	priv = dev_get_uclass_priv(dev);
78 
79 	return priv->regmap;
80 }
81 
82 void *syscon_get_first_range(ulong driver_data)
83 {
84 	struct regmap *map;
85 
86 	map = syscon_get_regmap_by_driver_data(driver_data);
87 	if (IS_ERR(map))
88 		return map;
89 	return regmap_get_range(map, 0);
90 }
91 
92 UCLASS_DRIVER(syscon) = {
93 	.id		= UCLASS_SYSCON,
94 	.name		= "syscon",
95 	.per_device_auto_alloc_size = sizeof(struct syscon_uc_info),
96 	.pre_probe = syscon_pre_probe,
97 };
98 
99 static const struct udevice_id generic_syscon_ids[] = {
100 	{ .compatible = "syscon" },
101 	{ }
102 };
103 
104 U_BOOT_DRIVER(generic_syscon) = {
105 	.name	= "syscon",
106 	.id	= UCLASS_SYSCON,
107 	.of_match = generic_syscon_ids,
108 };
109