blob: 141fd4d90831752133f26ff92cb40b4b3ee1da05 [file] [log] [blame]
Jean-Jacques Hiblota94b6972020-10-16 16:16:34 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/
4 * Pratyush Yadav <p.yadav@ti.com>
5 */
6#include <common.h>
7#include <dm.h>
8#include <mux.h>
9#include <mux-internal.h>
10#include <dm/test.h>
11#include <test/ut.h>
12
13struct mux_emul_priv {
14 u32 state;
15};
16
17static int mux_emul_set(struct mux_control *mux, int state)
18{
19 struct mux_emul_priv *priv = dev_get_priv(mux->dev);
20
21 priv->state = state;
22 return 0;
23}
24
25static int mux_emul_probe(struct udevice *dev)
26{
27 struct mux_chip *mux_chip = dev_get_uclass_priv(dev);
28 struct mux_control *mux;
29 u32 idle_state;
30 int ret;
31
32 ret = mux_alloc_controllers(dev, 1);
33 if (ret < 0)
34 return ret;
35
36 mux = &mux_chip->mux[0];
37
38 ret = dev_read_u32(dev, "idle-state", &idle_state);
39 if (ret)
40 return ret;
41
42 mux->idle_state = idle_state;
43 mux->states = 0x100000;
44
45 return 0;
46}
47
48static const struct mux_control_ops mux_emul_ops = {
49 .set = mux_emul_set,
50};
51
52static const struct udevice_id mux_emul_of_match[] = {
53 { .compatible = "mux-emul" },
54 { /* sentinel */ },
55};
56
57U_BOOT_DRIVER(emul_mux) = {
58 .name = "mux-emul",
59 .id = UCLASS_MUX,
60 .of_match = mux_emul_of_match,
61 .ops = &mux_emul_ops,
62 .probe = mux_emul_probe,
63 .priv_auto_alloc_size = sizeof(struct mux_emul_priv),
64};
65
66static int dm_test_mux_emul_default_state(struct unit_test_state *uts)
67{
68 struct udevice *dev;
69 struct mux_control *mux;
70 struct mux_emul_priv *priv;
71
72 ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
73 &dev));
74 ut_assertok(mux_control_get(dev, "mux4", &mux));
75
76 priv = dev_get_priv(mux->dev);
77
78 ut_asserteq(0xabcd, priv->state);
79
80 return 0;
81}
82DM_TEST(dm_test_mux_emul_default_state, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
83
84static int dm_test_mux_emul_select_deselect(struct unit_test_state *uts)
85{
86 struct udevice *dev;
87 struct mux_control *mux;
88 struct mux_emul_priv *priv;
89
90 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
91 ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
92 &dev));
93 ut_assertok(mux_control_get(dev, "mux4", &mux));
94
95 priv = dev_get_priv(mux->dev);
96
97 ut_assertok(mux_control_select(mux, 0x1234));
98 ut_asserteq(priv->state, 0x1234);
99
100 ut_assertok(mux_control_deselect(mux));
101 ut_asserteq(priv->state, 0xabcd);
102
103 return 0;
104}
105DM_TEST(dm_test_mux_emul_select_deselect, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);