GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / base / soc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) ST-Ericsson SA 2011
4  *
5  * Author: Lee Jones <lee.jones@linaro.org> for ST-Ericsson.
6  */
7
8 #include <linux/sysfs.h>
9 #include <linux/init.h>
10 #include <linux/stat.h>
11 #include <linux/slab.h>
12 #include <linux/idr.h>
13 #include <linux/spinlock.h>
14 #include <linux/sys_soc.h>
15 #include <linux/err.h>
16 #include <linux/glob.h>
17
18 static DEFINE_IDA(soc_ida);
19
20 static ssize_t soc_info_get(struct device *dev,
21                             struct device_attribute *attr,
22                             char *buf);
23
24 struct soc_device {
25         struct device dev;
26         struct soc_device_attribute *attr;
27         int soc_dev_num;
28 };
29
30 static struct bus_type soc_bus_type = {
31         .name  = "soc",
32 };
33
34 static DEVICE_ATTR(machine,  S_IRUGO, soc_info_get,  NULL);
35 static DEVICE_ATTR(family,   S_IRUGO, soc_info_get,  NULL);
36 static DEVICE_ATTR(soc_id,   S_IRUGO, soc_info_get,  NULL);
37 static DEVICE_ATTR(revision, S_IRUGO, soc_info_get,  NULL);
38
39 struct device *soc_device_to_device(struct soc_device *soc_dev)
40 {
41         return &soc_dev->dev;
42 }
43
44 static umode_t soc_attribute_mode(struct kobject *kobj,
45                                 struct attribute *attr,
46                                 int index)
47 {
48         struct device *dev = container_of(kobj, struct device, kobj);
49         struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
50
51         if ((attr == &dev_attr_machine.attr)
52             && (soc_dev->attr->machine != NULL))
53                 return attr->mode;
54         if ((attr == &dev_attr_family.attr)
55             && (soc_dev->attr->family != NULL))
56                 return attr->mode;
57         if ((attr == &dev_attr_revision.attr)
58             && (soc_dev->attr->revision != NULL))
59                 return attr->mode;
60         if ((attr == &dev_attr_soc_id.attr)
61             && (soc_dev->attr->soc_id != NULL))
62                 return attr->mode;
63
64         /* Unknown or unfilled attribute. */
65         return 0;
66 }
67
68 static ssize_t soc_info_get(struct device *dev,
69                             struct device_attribute *attr,
70                             char *buf)
71 {
72         struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
73
74         if (attr == &dev_attr_machine)
75                 return sprintf(buf, "%s\n", soc_dev->attr->machine);
76         if (attr == &dev_attr_family)
77                 return sprintf(buf, "%s\n", soc_dev->attr->family);
78         if (attr == &dev_attr_revision)
79                 return sprintf(buf, "%s\n", soc_dev->attr->revision);
80         if (attr == &dev_attr_soc_id)
81                 return sprintf(buf, "%s\n", soc_dev->attr->soc_id);
82
83         return -EINVAL;
84
85 }
86
87 static struct attribute *soc_attr[] = {
88         &dev_attr_machine.attr,
89         &dev_attr_family.attr,
90         &dev_attr_soc_id.attr,
91         &dev_attr_revision.attr,
92         NULL,
93 };
94
95 static const struct attribute_group soc_attr_group = {
96         .attrs = soc_attr,
97         .is_visible = soc_attribute_mode,
98 };
99
100 static const struct attribute_group *soc_attr_groups[] = {
101         &soc_attr_group,
102         NULL,
103 };
104
105 static void soc_release(struct device *dev)
106 {
107         struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
108
109         kfree(soc_dev);
110 }
111
112 static struct soc_device_attribute *early_soc_dev_attr;
113
114 struct soc_device *soc_device_register(struct soc_device_attribute *soc_dev_attr)
115 {
116         struct soc_device *soc_dev;
117         int ret;
118
119         if (!soc_bus_type.p) {
120                 if (early_soc_dev_attr)
121                         return ERR_PTR(-EBUSY);
122                 early_soc_dev_attr = soc_dev_attr;
123                 return NULL;
124         }
125
126         soc_dev = kzalloc(sizeof(*soc_dev), GFP_KERNEL);
127         if (!soc_dev) {
128                 ret = -ENOMEM;
129                 goto out1;
130         }
131
132         /* Fetch a unique (reclaimable) SOC ID. */
133         ret = ida_simple_get(&soc_ida, 0, 0, GFP_KERNEL);
134         if (ret < 0)
135                 goto out2;
136         soc_dev->soc_dev_num = ret;
137
138         soc_dev->attr = soc_dev_attr;
139         soc_dev->dev.bus = &soc_bus_type;
140         soc_dev->dev.groups = soc_attr_groups;
141         soc_dev->dev.release = soc_release;
142
143         dev_set_name(&soc_dev->dev, "soc%d", soc_dev->soc_dev_num);
144
145         ret = device_register(&soc_dev->dev);
146         if (ret)
147                 goto out3;
148
149         return soc_dev;
150
151 out3:
152         ida_simple_remove(&soc_ida, soc_dev->soc_dev_num);
153         put_device(&soc_dev->dev);
154         soc_dev = NULL;
155 out2:
156         kfree(soc_dev);
157 out1:
158         return ERR_PTR(ret);
159 }
160 EXPORT_SYMBOL_GPL(soc_device_register);
161
162 /* Ensure soc_dev->attr is freed prior to calling soc_device_unregister. */
163 void soc_device_unregister(struct soc_device *soc_dev)
164 {
165         ida_simple_remove(&soc_ida, soc_dev->soc_dev_num);
166
167         device_unregister(&soc_dev->dev);
168         early_soc_dev_attr = NULL;
169 }
170 EXPORT_SYMBOL_GPL(soc_device_unregister);
171
172 static int __init soc_bus_register(void)
173 {
174         int ret;
175
176         ret = bus_register(&soc_bus_type);
177         if (ret)
178                 return ret;
179
180         if (early_soc_dev_attr)
181                 return PTR_ERR(soc_device_register(early_soc_dev_attr));
182
183         return 0;
184 }
185 core_initcall(soc_bus_register);
186
187 static int soc_device_match_attr(const struct soc_device_attribute *attr,
188                                  const struct soc_device_attribute *match)
189 {
190         if (match->machine &&
191             (!attr->machine || !glob_match(match->machine, attr->machine)))
192                 return 0;
193
194         if (match->family &&
195             (!attr->family || !glob_match(match->family, attr->family)))
196                 return 0;
197
198         if (match->revision &&
199             (!attr->revision || !glob_match(match->revision, attr->revision)))
200                 return 0;
201
202         if (match->soc_id &&
203             (!attr->soc_id || !glob_match(match->soc_id, attr->soc_id)))
204                 return 0;
205
206         return 1;
207 }
208
209 static int soc_device_match_one(struct device *dev, void *arg)
210 {
211         struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
212
213         return soc_device_match_attr(soc_dev->attr, arg);
214 }
215
216 /*
217  * soc_device_match - identify the SoC in the machine
218  * @matches: zero-terminated array of possible matches
219  *
220  * returns the first matching entry of the argument array, or NULL
221  * if none of them match.
222  *
223  * This function is meant as a helper in place of of_match_node()
224  * in cases where either no device tree is available or the information
225  * in a device node is insufficient to identify a particular variant
226  * by its compatible strings or other properties. For new devices,
227  * the DT binding should always provide unique compatible strings
228  * that allow the use of of_match_node() instead.
229  *
230  * The calling function can use the .data entry of the
231  * soc_device_attribute to pass a structure or function pointer for
232  * each entry.
233  */
234 const struct soc_device_attribute *soc_device_match(
235         const struct soc_device_attribute *matches)
236 {
237         int ret = 0;
238
239         if (!matches)
240                 return NULL;
241
242         while (!ret) {
243                 if (!(matches->machine || matches->family ||
244                       matches->revision || matches->soc_id))
245                         break;
246                 ret = bus_for_each_dev(&soc_bus_type, NULL, (void *)matches,
247                                        soc_device_match_one);
248                 if (ret < 0 && early_soc_dev_attr)
249                         ret = soc_device_match_attr(early_soc_dev_attr,
250                                                     matches);
251                 if (ret < 0)
252                         return NULL;
253                 if (!ret)
254                         matches++;
255                 else
256                         return matches;
257         }
258         return NULL;
259 }
260 EXPORT_SYMBOL_GPL(soc_device_match);