blob: 5c4a66dd8cf6e9616e917e8ba1d1bdc64cdb8e17 [file] [log] [blame]
Simon Glass6494d702014-02-26 15:59:18 -07001/*
2 * Copyright (c) 2013 Google, Inc
3 *
4 * (C) Copyright 2012
5 * Pavel Herrmann <morpheus.ibis@gmail.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <common.h>
11#include <errno.h>
12#include <malloc.h>
13#include <dm/device.h>
14#include <dm/device-internal.h>
15#include <dm/lists.h>
16#include <dm/uclass.h>
17#include <dm/uclass-internal.h>
18#include <dm/util.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
22struct uclass *uclass_find(enum uclass_id key)
23{
24 struct uclass *uc;
25
Simon Glassc910e2e2014-07-23 06:55:15 -060026 if (!gd->dm_root)
27 return NULL;
Simon Glass6494d702014-02-26 15:59:18 -070028 /*
29 * TODO(sjg@chromium.org): Optimise this, perhaps moving the found
30 * node to the start of the list, or creating a linear array mapping
31 * id to node.
32 */
33 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
34 if (uc->uc_drv->id == key)
35 return uc;
36 }
37
38 return NULL;
39}
40
41/**
42 * uclass_add() - Create new uclass in list
43 * @id: Id number to create
44 * @ucp: Returns pointer to uclass, or NULL on error
45 * @return 0 on success, -ve on error
46 *
47 * The new uclass is added to the list. There must be only one uclass for
48 * each id.
49 */
50static int uclass_add(enum uclass_id id, struct uclass **ucp)
51{
52 struct uclass_driver *uc_drv;
53 struct uclass *uc;
54 int ret;
55
56 *ucp = NULL;
57 uc_drv = lists_uclass_lookup(id);
58 if (!uc_drv) {
Masahiro Yamada643e6902015-07-07 18:51:32 +090059 debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
60 id);
Simon Glass6494d702014-02-26 15:59:18 -070061 return -ENOENT;
62 }
Simon Glass6494d702014-02-26 15:59:18 -070063 uc = calloc(1, sizeof(*uc));
64 if (!uc)
65 return -ENOMEM;
66 if (uc_drv->priv_auto_alloc_size) {
67 uc->priv = calloc(1, uc_drv->priv_auto_alloc_size);
68 if (!uc->priv) {
69 ret = -ENOMEM;
70 goto fail_mem;
71 }
72 }
73 uc->uc_drv = uc_drv;
74 INIT_LIST_HEAD(&uc->sibling_node);
75 INIT_LIST_HEAD(&uc->dev_head);
Simon Glass89876a52014-06-11 23:29:49 -060076 list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST);
Simon Glass6494d702014-02-26 15:59:18 -070077
78 if (uc_drv->init) {
79 ret = uc_drv->init(uc);
80 if (ret)
81 goto fail;
82 }
83
84 *ucp = uc;
85
86 return 0;
87fail:
88 if (uc_drv->priv_auto_alloc_size) {
89 free(uc->priv);
90 uc->priv = NULL;
91 }
92 list_del(&uc->sibling_node);
93fail_mem:
94 free(uc);
95
96 return ret;
97}
98
99int uclass_destroy(struct uclass *uc)
100{
101 struct uclass_driver *uc_drv;
Simon Glass07d260e2015-04-19 07:20:58 -0600102 struct udevice *dev;
Simon Glass6494d702014-02-26 15:59:18 -0700103 int ret;
104
Simon Glass07d260e2015-04-19 07:20:58 -0600105 /*
106 * We cannot use list_for_each_entry_safe() here. If a device in this
107 * uclass has a child device also in this uclass, it will be also be
108 * unbound (by the recursion in the call to device_unbind() below).
109 * We can loop until the list is empty.
110 */
111 while (!list_empty(&uc->dev_head)) {
112 dev = list_first_entry(&uc->dev_head, struct udevice,
113 uclass_node);
Simon Glass6494d702014-02-26 15:59:18 -0700114 ret = device_remove(dev);
115 if (ret)
116 return ret;
117 ret = device_unbind(dev);
118 if (ret)
119 return ret;
120 }
121
122 uc_drv = uc->uc_drv;
123 if (uc_drv->destroy)
124 uc_drv->destroy(uc);
125 list_del(&uc->sibling_node);
126 if (uc_drv->priv_auto_alloc_size)
127 free(uc->priv);
128 free(uc);
129
130 return 0;
131}
132
133int uclass_get(enum uclass_id id, struct uclass **ucp)
134{
135 struct uclass *uc;
136
137 *ucp = NULL;
138 uc = uclass_find(id);
139 if (!uc)
140 return uclass_add(id, ucp);
141 *ucp = uc;
142
143 return 0;
144}
145
Heiko Schocher54c5d082014-05-22 12:43:05 +0200146int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
Simon Glass6494d702014-02-26 15:59:18 -0700147{
148 struct uclass *uc;
Heiko Schocher54c5d082014-05-22 12:43:05 +0200149 struct udevice *dev;
Simon Glass6494d702014-02-26 15:59:18 -0700150 int ret;
151
152 *devp = NULL;
153 ret = uclass_get(id, &uc);
154 if (ret)
155 return ret;
Simon Glass2fda14a2015-07-31 09:31:19 -0600156 if (list_empty(&uc->dev_head))
157 return -ENODEV;
Simon Glass6494d702014-02-26 15:59:18 -0700158
159 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
160 if (!index--) {
161 *devp = dev;
162 return 0;
163 }
164 }
165
166 return -ENODEV;
167}
168
Przemyslaw Marczakc1d6f912015-04-15 13:07:17 +0200169int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
170{
171 struct uclass *uc;
172 int ret;
173
174 *devp = NULL;
175 ret = uclass_get(id, &uc);
176 if (ret)
177 return ret;
178 if (list_empty(&uc->dev_head))
179 return 0;
180
181 *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
182
183 return 0;
184}
185
186int uclass_find_next_device(struct udevice **devp)
187{
188 struct udevice *dev = *devp;
189
190 *devp = NULL;
191 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
192 return 0;
193
194 *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
195
196 return 0;
197}
198
Przemyslaw Marczake0735a42015-04-15 13:07:22 +0200199int uclass_find_device_by_name(enum uclass_id id, const char *name,
200 struct udevice **devp)
201{
202 struct uclass *uc;
203 struct udevice *dev;
204 int ret;
205
206 *devp = NULL;
207 if (!name)
208 return -EINVAL;
209 ret = uclass_get(id, &uc);
210 if (ret)
211 return ret;
212
213 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
214 if (!strncmp(dev->name, name, strlen(name))) {
215 *devp = dev;
216 return 0;
217 }
218 }
219
220 return -ENODEV;
221}
222
Simon Glass5a66a8f2014-07-23 06:55:12 -0600223int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq,
224 bool find_req_seq, struct udevice **devp)
225{
226 struct uclass *uc;
227 struct udevice *dev;
228 int ret;
229
230 *devp = NULL;
231 debug("%s: %d %d\n", __func__, find_req_seq, seq_or_req_seq);
232 if (seq_or_req_seq == -1)
233 return -ENODEV;
234 ret = uclass_get(id, &uc);
235 if (ret)
236 return ret;
237
238 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
239 debug(" - %d %d\n", dev->req_seq, dev->seq);
240 if ((find_req_seq ? dev->req_seq : dev->seq) ==
241 seq_or_req_seq) {
242 *devp = dev;
243 debug(" - found\n");
244 return 0;
245 }
246 }
247 debug(" - not found\n");
248
249 return -ENODEV;
250}
251
Simon Glassf4cdead2014-07-23 06:55:14 -0600252static int uclass_find_device_by_of_offset(enum uclass_id id, int node,
253 struct udevice **devp)
254{
255 struct uclass *uc;
256 struct udevice *dev;
257 int ret;
258
259 *devp = NULL;
260 if (node < 0)
261 return -ENODEV;
262 ret = uclass_get(id, &uc);
263 if (ret)
264 return ret;
265
266 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
267 if (dev->of_offset == node) {
268 *devp = dev;
269 return 0;
270 }
271 }
272
273 return -ENODEV;
274}
275
Simon Glassd82ba4c2015-07-02 18:15:38 -0600276static int uclass_find_device_by_phandle(enum uclass_id id,
277 struct udevice *parent,
278 const char *name,
279 struct udevice **devp)
280{
281 struct udevice *dev;
282 struct uclass *uc;
283 int find_phandle;
284 int ret;
285
286 *devp = NULL;
287 find_phandle = fdtdec_get_int(gd->fdt_blob, parent->of_offset, name,
288 -1);
289 if (find_phandle <= 0)
290 return -ENOENT;
291 ret = uclass_get(id, &uc);
292 if (ret)
293 return ret;
294
295 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
296 uint phandle = fdt_get_phandle(gd->fdt_blob, dev->of_offset);
297
298 if (phandle == find_phandle) {
299 *devp = dev;
300 return 0;
301 }
302 }
303
304 return -ENODEV;
305}
306
Przemyslaw Marczak794d5212015-04-20 13:32:32 +0200307int uclass_get_device_tail(struct udevice *dev, int ret,
Simon Glass9ca296a2014-07-23 06:55:08 -0600308 struct udevice **devp)
Simon Glass6494d702014-02-26 15:59:18 -0700309{
Simon Glass6494d702014-02-26 15:59:18 -0700310 if (ret)
311 return ret;
312
Simon Glassf66529f2015-04-24 22:33:07 -0600313 assert(dev);
Simon Glass6494d702014-02-26 15:59:18 -0700314 ret = device_probe(dev);
315 if (ret)
316 return ret;
317
318 *devp = dev;
319
320 return 0;
321}
322
Simon Glass9ca296a2014-07-23 06:55:08 -0600323int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
324{
325 struct udevice *dev;
326 int ret;
327
328 *devp = NULL;
329 ret = uclass_find_device(id, index, &dev);
330 return uclass_get_device_tail(dev, ret, devp);
331}
332
Przemyslaw Marczakb7af1a22015-04-15 13:07:23 +0200333int uclass_get_device_by_name(enum uclass_id id, const char *name,
334 struct udevice **devp)
335{
336 struct udevice *dev;
337 int ret;
338
339 *devp = NULL;
340 ret = uclass_find_device_by_name(id, name, &dev);
341 return uclass_get_device_tail(dev, ret, devp);
342}
343
Simon Glass5a66a8f2014-07-23 06:55:12 -0600344int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
345{
346 struct udevice *dev;
347 int ret;
348
349 *devp = NULL;
350 ret = uclass_find_device_by_seq(id, seq, false, &dev);
351 if (ret == -ENODEV) {
352 /*
353 * We didn't find it in probed devices. See if there is one
354 * that will request this seq if probed.
355 */
356 ret = uclass_find_device_by_seq(id, seq, true, &dev);
357 }
358 return uclass_get_device_tail(dev, ret, devp);
359}
360
Simon Glassf4cdead2014-07-23 06:55:14 -0600361int uclass_get_device_by_of_offset(enum uclass_id id, int node,
362 struct udevice **devp)
363{
364 struct udevice *dev;
365 int ret;
366
367 *devp = NULL;
368 ret = uclass_find_device_by_of_offset(id, node, &dev);
369 return uclass_get_device_tail(dev, ret, devp);
370}
371
Simon Glassd82ba4c2015-07-02 18:15:38 -0600372int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
373 const char *name, struct udevice **devp)
374{
375 struct udevice *dev;
376 int ret;
377
378 *devp = NULL;
379 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
380 return uclass_get_device_tail(dev, ret, devp);
381}
382
Heiko Schocher54c5d082014-05-22 12:43:05 +0200383int uclass_first_device(enum uclass_id id, struct udevice **devp)
Simon Glass6494d702014-02-26 15:59:18 -0700384{
Heiko Schocher54c5d082014-05-22 12:43:05 +0200385 struct udevice *dev;
Simon Glass6494d702014-02-26 15:59:18 -0700386 int ret;
387
388 *devp = NULL;
Przemyslaw Marczakc1d6f912015-04-15 13:07:17 +0200389 ret = uclass_find_first_device(id, &dev);
Simon Glassf66529f2015-04-24 22:33:07 -0600390 if (!dev)
391 return 0;
Przemyslaw Marczakc1d6f912015-04-15 13:07:17 +0200392 return uclass_get_device_tail(dev, ret, devp);
Simon Glass6494d702014-02-26 15:59:18 -0700393}
394
Heiko Schocher54c5d082014-05-22 12:43:05 +0200395int uclass_next_device(struct udevice **devp)
Simon Glass6494d702014-02-26 15:59:18 -0700396{
Heiko Schocher54c5d082014-05-22 12:43:05 +0200397 struct udevice *dev = *devp;
Simon Glass6494d702014-02-26 15:59:18 -0700398 int ret;
399
400 *devp = NULL;
Przemyslaw Marczakc1d6f912015-04-15 13:07:17 +0200401 ret = uclass_find_next_device(&dev);
Simon Glassf66529f2015-04-24 22:33:07 -0600402 if (!dev)
403 return 0;
Przemyslaw Marczakc1d6f912015-04-15 13:07:17 +0200404 return uclass_get_device_tail(dev, ret, devp);
Simon Glass6494d702014-02-26 15:59:18 -0700405}
406
Heiko Schocher54c5d082014-05-22 12:43:05 +0200407int uclass_bind_device(struct udevice *dev)
Simon Glass6494d702014-02-26 15:59:18 -0700408{
409 struct uclass *uc;
410 int ret;
411
412 uc = dev->uclass;
Simon Glass6494d702014-02-26 15:59:18 -0700413 list_add_tail(&dev->uclass_node, &uc->dev_head);
414
Simon Glass081f2fc2015-01-25 08:27:08 -0700415 if (dev->parent) {
416 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
417
418 if (uc_drv->child_post_bind) {
419 ret = uc_drv->child_post_bind(dev);
420 if (ret)
421 goto err;
422 }
423 }
Simon Glass6494d702014-02-26 15:59:18 -0700424 if (uc->uc_drv->post_bind) {
425 ret = uc->uc_drv->post_bind(dev);
Simon Glass081f2fc2015-01-25 08:27:08 -0700426 if (ret)
427 goto err;
Simon Glass6494d702014-02-26 15:59:18 -0700428 }
429
430 return 0;
Simon Glass081f2fc2015-01-25 08:27:08 -0700431err:
432 /* There is no need to undo the parent's post_bind call */
433 list_del(&dev->uclass_node);
434
435 return ret;
Simon Glass6494d702014-02-26 15:59:18 -0700436}
437
Simon Glass7f9875e2015-02-27 22:06:31 -0700438#ifdef CONFIG_DM_DEVICE_REMOVE
Heiko Schocher54c5d082014-05-22 12:43:05 +0200439int uclass_unbind_device(struct udevice *dev)
Simon Glass6494d702014-02-26 15:59:18 -0700440{
441 struct uclass *uc;
442 int ret;
443
444 uc = dev->uclass;
445 if (uc->uc_drv->pre_unbind) {
446 ret = uc->uc_drv->pre_unbind(dev);
447 if (ret)
448 return ret;
449 }
450
451 list_del(&dev->uclass_node);
452 return 0;
453}
Simon Glass7f9875e2015-02-27 22:06:31 -0700454#endif
Simon Glass6494d702014-02-26 15:59:18 -0700455
Simon Glass5a66a8f2014-07-23 06:55:12 -0600456int uclass_resolve_seq(struct udevice *dev)
457{
458 struct udevice *dup;
459 int seq;
460 int ret;
461
462 assert(dev->seq == -1);
463 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq,
464 false, &dup);
465 if (!ret) {
466 dm_warn("Device '%s': seq %d is in use by '%s'\n",
467 dev->name, dev->req_seq, dup->name);
468 } else if (ret == -ENODEV) {
469 /* Our requested sequence number is available */
470 if (dev->req_seq != -1)
471 return dev->req_seq;
472 } else {
473 return ret;
474 }
475
476 for (seq = 0; seq < DM_MAX_SEQ; seq++) {
477 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq,
478 false, &dup);
479 if (ret == -ENODEV)
480 break;
481 if (ret)
482 return ret;
483 }
484 return seq;
485}
486
Simon Glass02c07b32015-03-05 12:25:22 -0700487int uclass_pre_probe_device(struct udevice *dev)
Simon Glass83c7e432015-01-25 08:27:10 -0700488{
489 struct uclass_driver *uc_drv;
Simon Glass02c07b32015-03-05 12:25:22 -0700490 int ret;
491
492 uc_drv = dev->uclass->uc_drv;
493 if (uc_drv->pre_probe) {
494 ret = uc_drv->pre_probe(dev);
495 if (ret)
496 return ret;
497 }
Simon Glass83c7e432015-01-25 08:27:10 -0700498
499 if (!dev->parent)
500 return 0;
501 uc_drv = dev->parent->uclass->uc_drv;
502 if (uc_drv->child_pre_probe)
503 return uc_drv->child_pre_probe(dev);
504
505 return 0;
506}
507
Heiko Schocher54c5d082014-05-22 12:43:05 +0200508int uclass_post_probe_device(struct udevice *dev)
Simon Glass6494d702014-02-26 15:59:18 -0700509{
510 struct uclass_driver *uc_drv = dev->uclass->uc_drv;
511
512 if (uc_drv->post_probe)
513 return uc_drv->post_probe(dev);
514
515 return 0;
516}
517
Simon Glass7f9875e2015-02-27 22:06:31 -0700518#ifdef CONFIG_DM_DEVICE_REMOVE
Heiko Schocher54c5d082014-05-22 12:43:05 +0200519int uclass_pre_remove_device(struct udevice *dev)
Simon Glass6494d702014-02-26 15:59:18 -0700520{
521 struct uclass_driver *uc_drv;
522 struct uclass *uc;
523 int ret;
524
525 uc = dev->uclass;
526 uc_drv = uc->uc_drv;
527 if (uc->uc_drv->pre_remove) {
528 ret = uc->uc_drv->pre_remove(dev);
529 if (ret)
530 return ret;
531 }
532 if (uc_drv->per_device_auto_alloc_size) {
533 free(dev->uclass_priv);
534 dev->uclass_priv = NULL;
535 }
Simon Glass5a66a8f2014-07-23 06:55:12 -0600536 dev->seq = -1;
Simon Glass6494d702014-02-26 15:59:18 -0700537
538 return 0;
539}
Simon Glass7f9875e2015-02-27 22:06:31 -0700540#endif