test: dm: Test for default led naming
[platform/kernel/u-boot.git] / test / dm / test-driver.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013 Google, Inc
4  *
5  * (C) Copyright 2012
6  * Pavel Herrmann <morpheus.ibis@gmail.com>
7  */
8
9 #include <common.h>
10 #include <dm.h>
11 #include <errno.h>
12 #include <log.h>
13 #include <malloc.h>
14 #include <asm/io.h>
15 #include <dm/test.h>
16 #include <test/test.h>
17 #include <test/ut.h>
18
19 int dm_testdrv_op_count[DM_TEST_OP_COUNT];
20 static struct unit_test_state *uts = &global_dm_test_state;
21
22 static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
23 {
24         const struct dm_test_pdata *pdata = dev_get_platdata(dev);
25         struct dm_test_priv *priv = dev_get_priv(dev);
26
27         *pingret = pingval + pdata->ping_add;
28         priv->ping_total += *pingret;
29
30         return 0;
31 }
32
33 static const struct test_ops test_ops = {
34         .ping = testdrv_ping,
35 };
36
37 static int test_bind(struct udevice *dev)
38 {
39         /* Private data should not be allocated */
40         ut_assert(!dev_get_priv(dev));
41
42         dm_testdrv_op_count[DM_TEST_OP_BIND]++;
43         return 0;
44 }
45
46 static int test_probe(struct udevice *dev)
47 {
48         struct dm_test_priv *priv = dev_get_priv(dev);
49
50         /* Private data should be allocated */
51         ut_assert(priv);
52
53         dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
54         priv->ping_total += DM_TEST_START_TOTAL;
55         return 0;
56 }
57
58 static int test_remove(struct udevice *dev)
59 {
60         /* Private data should still be allocated */
61         ut_assert(dev_get_priv(dev));
62
63         dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
64         return 0;
65 }
66
67 static int test_unbind(struct udevice *dev)
68 {
69         /* Private data should not be allocated */
70         ut_assert(!dev->priv);
71
72         dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
73         return 0;
74 }
75
76 U_BOOT_DRIVER(test_drv) = {
77         .name   = "test_drv",
78         .id     = UCLASS_TEST,
79         .ops    = &test_ops,
80         .bind   = test_bind,
81         .probe  = test_probe,
82         .remove = test_remove,
83         .unbind = test_unbind,
84         .priv_auto_alloc_size = sizeof(struct dm_test_priv),
85 };
86
87 U_BOOT_DRIVER(test2_drv) = {
88         .name   = "test2_drv",
89         .id     = UCLASS_TEST,
90         .ops    = &test_ops,
91         .bind   = test_bind,
92         .probe  = test_probe,
93         .remove = test_remove,
94         .unbind = test_unbind,
95         .priv_auto_alloc_size = sizeof(struct dm_test_priv),
96 };
97
98 static int test_manual_drv_ping(struct udevice *dev, int pingval, int *pingret)
99 {
100         *pingret = pingval + 2;
101
102         return 0;
103 }
104
105 static const struct test_ops test_manual_ops = {
106         .ping = test_manual_drv_ping,
107 };
108
109 static int test_manual_bind(struct udevice *dev)
110 {
111         dm_testdrv_op_count[DM_TEST_OP_BIND]++;
112
113         return 0;
114 }
115
116 static int test_manual_probe(struct udevice *dev)
117 {
118         struct dm_test_state *dms = uts->priv;
119
120         dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
121         if (!dms->force_fail_alloc)
122                 dev->priv = calloc(1, sizeof(struct dm_test_priv));
123         if (!dev->priv)
124                 return -ENOMEM;
125
126         return 0;
127 }
128
129 static int test_manual_remove(struct udevice *dev)
130 {
131         dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
132         return 0;
133 }
134
135 static int test_manual_unbind(struct udevice *dev)
136 {
137         dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
138         return 0;
139 }
140
141 U_BOOT_DRIVER(test_manual_drv) = {
142         .name   = "test_manual_drv",
143         .id     = UCLASS_TEST,
144         .ops    = &test_manual_ops,
145         .bind   = test_manual_bind,
146         .probe  = test_manual_probe,
147         .remove = test_manual_remove,
148         .unbind = test_manual_unbind,
149 };
150
151 U_BOOT_DRIVER(test_pre_reloc_drv) = {
152         .name   = "test_pre_reloc_drv",
153         .id     = UCLASS_TEST,
154         .ops    = &test_manual_ops,
155         .bind   = test_manual_bind,
156         .probe  = test_manual_probe,
157         .remove = test_manual_remove,
158         .unbind = test_manual_unbind,
159         .flags  = DM_FLAG_PRE_RELOC,
160 };
161
162 U_BOOT_DRIVER(test_act_dma_drv) = {
163         .name   = "test_act_dma_drv",
164         .id     = UCLASS_TEST,
165         .ops    = &test_manual_ops,
166         .bind   = test_manual_bind,
167         .probe  = test_manual_probe,
168         .remove = test_manual_remove,
169         .unbind = test_manual_unbind,
170         .flags  = DM_FLAG_ACTIVE_DMA,
171 };