blob: 58233edc9b290d5b6ea86d77a2ec88ca7d03e6fa [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Jean-Jacques Hiblota94b6972020-10-16 16:16:34 +053013
14struct mux_emul_priv {
15 u32 state;
16};
17
18static int mux_emul_set(struct mux_control *mux, int state)
19{
20 struct mux_emul_priv *priv = dev_get_priv(mux->dev);
21
22 priv->state = state;
23 return 0;
24}
25
26static int mux_emul_probe(struct udevice *dev)
27{
28 struct mux_chip *mux_chip = dev_get_uclass_priv(dev);
29 struct mux_control *mux;
30 u32 idle_state;
31 int ret;
32
33 ret = mux_alloc_controllers(dev, 1);
34 if (ret < 0)
35 return ret;
36
37 mux = &mux_chip->mux[0];
38
39 ret = dev_read_u32(dev, "idle-state", &idle_state);
40 if (ret)
41 return ret;
42
43 mux->idle_state = idle_state;
44 mux->states = 0x100000;
45
46 return 0;
47}
48
49static const struct mux_control_ops mux_emul_ops = {
50 .set = mux_emul_set,
51};
52
53static const struct udevice_id mux_emul_of_match[] = {
54 { .compatible = "mux-emul" },
55 { /* sentinel */ },
56};
57
58U_BOOT_DRIVER(emul_mux) = {
59 .name = "mux-emul",
60 .id = UCLASS_MUX,
61 .of_match = mux_emul_of_match,
62 .ops = &mux_emul_ops,
63 .probe = mux_emul_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -070064 .priv_auto = sizeof(struct mux_emul_priv),
Jean-Jacques Hiblota94b6972020-10-16 16:16:34 +053065};
66
67static int dm_test_mux_emul_default_state(struct unit_test_state *uts)
68{
69 struct udevice *dev;
70 struct mux_control *mux;
71 struct mux_emul_priv *priv;
72
73 ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
74 &dev));
75 ut_assertok(mux_control_get(dev, "mux4", &mux));
76
77 priv = dev_get_priv(mux->dev);
78
79 ut_asserteq(0xabcd, priv->state);
80
81 return 0;
82}
83DM_TEST(dm_test_mux_emul_default_state, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
84
85static int dm_test_mux_emul_select_deselect(struct unit_test_state *uts)
86{
87 struct udevice *dev;
88 struct mux_control *mux;
89 struct mux_emul_priv *priv;
90
91 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
92 ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
93 &dev));
94 ut_assertok(mux_control_get(dev, "mux4", &mux));
95
96 priv = dev_get_priv(mux->dev);
97
98 ut_assertok(mux_control_select(mux, 0x1234));
99 ut_asserteq(priv->state, 0x1234);
100
101 ut_assertok(mux_control_deselect(mux));
102 ut_asserteq(priv->state, 0xabcd);
103
104 return 0;
105}
106DM_TEST(dm_test_mux_emul_select_deselect, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);