~funderscore blog cgit wiki get in touch
aboutsummaryrefslogtreecommitdiff
blob: 58233edc9b290d5b6ea86d77a2ec88ca7d03e6fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/
 * Pratyush Yadav <p.yadav@ti.com>
 */
#include <common.h>
#include <dm.h>
#include <mux.h>
#include <mux-internal.h>
#include <dm/test.h>
#include <test/ut.h>
#include <asm/global_data.h>

struct mux_emul_priv {
	u32 state;
};

static int mux_emul_set(struct mux_control *mux, int state)
{
	struct mux_emul_priv *priv = dev_get_priv(mux->dev);

	priv->state = state;
	return 0;
}

static int mux_emul_probe(struct udevice *dev)
{
	struct mux_chip *mux_chip = dev_get_uclass_priv(dev);
	struct mux_control *mux;
	u32 idle_state;
	int ret;

	ret = mux_alloc_controllers(dev, 1);
	if (ret < 0)
		return ret;

	mux = &mux_chip->mux[0];

	ret = dev_read_u32(dev, "idle-state", &idle_state);
	if (ret)
		return ret;

	mux->idle_state = idle_state;
	mux->states = 0x100000;

	return 0;
}

static const struct mux_control_ops mux_emul_ops = {
	.set = mux_emul_set,
};

static const struct udevice_id mux_emul_of_match[] = {
	{ .compatible = "mux-emul" },
	{ /* sentinel */ },
};

U_BOOT_DRIVER(emul_mux) = {
	.name = "mux-emul",
	.id = UCLASS_MUX,
	.of_match = mux_emul_of_match,
	.ops = &mux_emul_ops,
	.probe = mux_emul_probe,
	.priv_auto	= sizeof(struct mux_emul_priv),
};

static int dm_test_mux_emul_default_state(struct unit_test_state *uts)
{
	struct udevice *dev;
	struct mux_control *mux;
	struct mux_emul_priv *priv;

	ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
					      &dev));
	ut_assertok(mux_control_get(dev, "mux4", &mux));

	priv = dev_get_priv(mux->dev);

	ut_asserteq(0xabcd, priv->state);

	return 0;
}
DM_TEST(dm_test_mux_emul_default_state, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);

static int dm_test_mux_emul_select_deselect(struct unit_test_state *uts)
{
	struct udevice *dev;
	struct mux_control *mux;
	struct mux_emul_priv *priv;

	gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
	ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
					      &dev));
	ut_assertok(mux_control_get(dev, "mux4", &mux));

	priv = dev_get_priv(mux->dev);

	ut_assertok(mux_control_select(mux, 0x1234));
	ut_asserteq(priv->state, 0x1234);

	ut_assertok(mux_control_deselect(mux));
	ut_asserteq(priv->state, 0xabcd);

	return 0;
}
DM_TEST(dm_test_mux_emul_select_deselect, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);