Jean-Jacques Hiblot | a94b697 | 2020-10-16 16:16:34 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Nishanth Menon | eaa39c6 | 2023-11-01 15:56:03 -0500 | [diff] [blame] | 3 | * Copyright (C) 2020 Texas Instruments Incorporated - https://www.ti.com/ |
Jean-Jacques Hiblot | a94b697 | 2020-10-16 16:16:34 +0530 | [diff] [blame] | 4 | * Pratyush Yadav <p.yadav@ti.com> |
| 5 | */ |
Jean-Jacques Hiblot | a94b697 | 2020-10-16 16:16:34 +0530 | [diff] [blame] | 6 | #include <dm.h> |
| 7 | #include <mux.h> |
| 8 | #include <mux-internal.h> |
| 9 | #include <dm/test.h> |
| 10 | #include <test/ut.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Jean-Jacques Hiblot | a94b697 | 2020-10-16 16:16:34 +0530 | [diff] [blame] | 12 | |
| 13 | struct mux_emul_priv { |
| 14 | u32 state; |
| 15 | }; |
| 16 | |
| 17 | static 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 | |
| 25 | static 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 | |
| 48 | static const struct mux_control_ops mux_emul_ops = { |
| 49 | .set = mux_emul_set, |
| 50 | }; |
| 51 | |
| 52 | static const struct udevice_id mux_emul_of_match[] = { |
| 53 | { .compatible = "mux-emul" }, |
| 54 | { /* sentinel */ }, |
| 55 | }; |
| 56 | |
| 57 | U_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, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 63 | .priv_auto = sizeof(struct mux_emul_priv), |
Jean-Jacques Hiblot | a94b697 | 2020-10-16 16:16:34 +0530 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | static 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 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 82 | DM_TEST(dm_test_mux_emul_default_state, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Jean-Jacques Hiblot | a94b697 | 2020-10-16 16:16:34 +0530 | [diff] [blame] | 83 | |
| 84 | static 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 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 105 | DM_TEST(dm_test_mux_emul_select_deselect, UTF_SCAN_PDATA | UTF_SCAN_FDT); |