dm: core: Introduce dev_read_alias_highest_id()
[oweals/u-boot.git] / drivers / board / sandbox.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2018
4  * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <board.h>
10
11 #include "sandbox.h"
12
13 struct board_sandbox_priv {
14         bool called_detect;
15         int test_i1;
16         int test_i2;
17 };
18
19 char vacation_spots[][64] = {"R'lyeh", "Dreamlands", "Plateau of Leng",
20                              "Carcosa", "Yuggoth", "The Nameless City"};
21
22 int board_sandbox_detect(struct udevice *dev)
23 {
24         struct board_sandbox_priv *priv = dev_get_priv(dev);
25
26         priv->called_detect = true;
27         priv->test_i2 = 100;
28
29         return 0;
30 }
31
32 int board_sandbox_get_bool(struct udevice *dev, int id, bool *val)
33 {
34         struct board_sandbox_priv *priv = dev_get_priv(dev);
35
36         switch (id) {
37         case BOOL_CALLED_DETECT:
38                 /* Checks if the dectect method has been called */
39                 *val = priv->called_detect;
40                 return 0;
41         }
42
43         return -ENOENT;
44 }
45
46 int board_sandbox_get_int(struct udevice *dev, int id, int *val)
47 {
48         struct board_sandbox_priv *priv = dev_get_priv(dev);
49
50         switch (id) {
51         case INT_TEST1:
52                 *val = priv->test_i1;
53                 /* Increments with every call */
54                 priv->test_i1++;
55                 return 0;
56         case INT_TEST2:
57                 *val = priv->test_i2;
58                 /* Decrements with every call */
59                 priv->test_i2--;
60                 return 0;
61         }
62
63         return -ENOENT;
64 }
65
66 int board_sandbox_get_str(struct udevice *dev, int id, size_t size, char *val)
67 {
68         struct board_sandbox_priv *priv = dev_get_priv(dev);
69         int i1 = priv->test_i1;
70         int i2 = priv->test_i2;
71         int index = (i1 * i2) % ARRAY_SIZE(vacation_spots);
72
73         switch (id) {
74         case STR_VACATIONSPOT:
75                 /* Picks a vacation spot depending on i1 and i2 */
76                 snprintf(val, size, vacation_spots[index]);
77                 return 0;
78         }
79
80         return -ENOENT;
81 }
82
83 static const struct udevice_id board_sandbox_ids[] = {
84         { .compatible = "sandbox,board_sandbox" },
85         { /* sentinel */ }
86 };
87
88 static const struct board_ops board_sandbox_ops = {
89         .detect = board_sandbox_detect,
90         .get_bool = board_sandbox_get_bool,
91         .get_int = board_sandbox_get_int,
92         .get_str = board_sandbox_get_str,
93 };
94
95 int board_sandbox_probe(struct udevice *dev)
96 {
97         return 0;
98 }
99
100 U_BOOT_DRIVER(board_sandbox) = {
101         .name           = "board_sandbox",
102         .id             = UCLASS_BOARD,
103         .of_match       = board_sandbox_ids,
104         .ops            = &board_sandbox_ops,
105         .priv_auto_alloc_size = sizeof(struct board_sandbox_priv),
106         .probe          = board_sandbox_probe,
107 };