1 // 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 #include <asm/global_data.h>
13 
14 struct mux_emul_priv {
15 	u32 state;
16 };
17 
mux_emul_set(struct mux_control * mux,int state)18 static 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 
mux_emul_probe(struct udevice * dev)26 static 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 
49 static const struct mux_control_ops mux_emul_ops = {
50 	.set = mux_emul_set,
51 };
52 
53 static const struct udevice_id mux_emul_of_match[] = {
54 	{ .compatible = "mux-emul" },
55 	{ /* sentinel */ },
56 };
57 
58 U_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,
64 	.priv_auto	= sizeof(struct mux_emul_priv),
65 };
66 
dm_test_mux_emul_default_state(struct unit_test_state * uts)67 static 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 }
83 DM_TEST(dm_test_mux_emul_default_state, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
84 
dm_test_mux_emul_select_deselect(struct unit_test_state * uts)85 static 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 }
106 DM_TEST(dm_test_mux_emul_select_deselect, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
107