1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+ 26a1c7cefSSimon Glass /* 36a1c7cefSSimon Glass * Copyright (C) 2015 Google, Inc 46a1c7cefSSimon Glass */ 56a1c7cefSSimon Glass 66a1c7cefSSimon Glass #include <common.h> 76a1c7cefSSimon Glass #include <dm.h> 8135aa950SStephen Warren #include <asm/clk.h> 96a1c7cefSSimon Glass #include <dm/test.h> 106a1c7cefSSimon Glass #include <linux/err.h> 116a1c7cefSSimon Glass #include <test/ut.h> 126a1c7cefSSimon Glass 13135aa950SStephen Warren static int dm_test_clk(struct unit_test_state *uts) 146a1c7cefSSimon Glass { 15135aa950SStephen Warren struct udevice *dev_fixed, *dev_clk, *dev_test; 166a1c7cefSSimon Glass ulong rate; 176a1c7cefSSimon Glass 18135aa950SStephen Warren ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-fixed", 19135aa950SStephen Warren &dev_fixed)); 206a1c7cefSSimon Glass 21135aa950SStephen Warren ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-sbox", 22135aa950SStephen Warren &dev_clk)); 23135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); 24135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); 25135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI)); 26135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C)); 276a1c7cefSSimon Glass 28135aa950SStephen Warren ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test", 29135aa950SStephen Warren &dev_test)); 30135aa950SStephen Warren ut_assertok(sandbox_clk_test_get(dev_test)); 316a1c7cefSSimon Glass 32135aa950SStephen Warren ut_asserteq(1234, 33135aa950SStephen Warren sandbox_clk_test_get_rate(dev_test, 34135aa950SStephen Warren SANDBOX_CLK_TEST_ID_FIXED)); 35135aa950SStephen Warren ut_asserteq(0, sandbox_clk_test_get_rate(dev_test, 36135aa950SStephen Warren SANDBOX_CLK_TEST_ID_SPI)); 37135aa950SStephen Warren ut_asserteq(0, sandbox_clk_test_get_rate(dev_test, 38135aa950SStephen Warren SANDBOX_CLK_TEST_ID_I2C)); 396a1c7cefSSimon Glass 40135aa950SStephen Warren rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED, 41135aa950SStephen Warren 12345); 42135aa950SStephen Warren ut_assert(IS_ERR_VALUE(rate)); 43135aa950SStephen Warren rate = sandbox_clk_test_get_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED); 446a1c7cefSSimon Glass ut_asserteq(1234, rate); 456a1c7cefSSimon Glass 46135aa950SStephen Warren ut_asserteq(0, sandbox_clk_test_set_rate(dev_test, 47135aa950SStephen Warren SANDBOX_CLK_TEST_ID_SPI, 48135aa950SStephen Warren 1000)); 49135aa950SStephen Warren ut_asserteq(0, sandbox_clk_test_set_rate(dev_test, 50135aa950SStephen Warren SANDBOX_CLK_TEST_ID_I2C, 51135aa950SStephen Warren 2000)); 52135aa950SStephen Warren 53135aa950SStephen Warren ut_asserteq(1000, sandbox_clk_test_get_rate(dev_test, 54135aa950SStephen Warren SANDBOX_CLK_TEST_ID_SPI)); 55135aa950SStephen Warren ut_asserteq(2000, sandbox_clk_test_get_rate(dev_test, 56135aa950SStephen Warren SANDBOX_CLK_TEST_ID_I2C)); 57135aa950SStephen Warren 58135aa950SStephen Warren ut_asserteq(1000, sandbox_clk_test_set_rate(dev_test, 59135aa950SStephen Warren SANDBOX_CLK_TEST_ID_SPI, 60135aa950SStephen Warren 10000)); 61135aa950SStephen Warren ut_asserteq(2000, sandbox_clk_test_set_rate(dev_test, 62135aa950SStephen Warren SANDBOX_CLK_TEST_ID_I2C, 63135aa950SStephen Warren 20000)); 64135aa950SStephen Warren 65135aa950SStephen Warren rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_SPI, 0); 66135aa950SStephen Warren ut_assert(IS_ERR_VALUE(rate)); 67135aa950SStephen Warren rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_I2C, 0); 68135aa950SStephen Warren ut_assert(IS_ERR_VALUE(rate)); 69135aa950SStephen Warren 70135aa950SStephen Warren ut_asserteq(10000, sandbox_clk_test_get_rate(dev_test, 71135aa950SStephen Warren SANDBOX_CLK_TEST_ID_SPI)); 72135aa950SStephen Warren ut_asserteq(20000, sandbox_clk_test_get_rate(dev_test, 73135aa950SStephen Warren SANDBOX_CLK_TEST_ID_I2C)); 74135aa950SStephen Warren 75135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); 76135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); 77135aa950SStephen Warren ut_asserteq(10000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI)); 78135aa950SStephen Warren ut_asserteq(20000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C)); 79135aa950SStephen Warren 80135aa950SStephen Warren ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_SPI)); 81135aa950SStephen Warren ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); 82135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); 83135aa950SStephen Warren 84135aa950SStephen Warren ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_I2C)); 85135aa950SStephen Warren ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); 86135aa950SStephen Warren ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); 87135aa950SStephen Warren 88135aa950SStephen Warren ut_assertok(sandbox_clk_test_disable(dev_test, 89135aa950SStephen Warren SANDBOX_CLK_TEST_ID_SPI)); 90135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); 91135aa950SStephen Warren ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); 92135aa950SStephen Warren 93135aa950SStephen Warren ut_assertok(sandbox_clk_test_disable(dev_test, 94135aa950SStephen Warren SANDBOX_CLK_TEST_ID_I2C)); 95135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); 96135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); 97135aa950SStephen Warren 98135aa950SStephen Warren ut_assertok(sandbox_clk_test_free(dev_test)); 996a1c7cefSSimon Glass 1006a1c7cefSSimon Glass return 0; 1016a1c7cefSSimon Glass } 102135aa950SStephen Warren DM_TEST(dm_test_clk, DM_TESTF_SCAN_FDT); 10365388d0dSNeil Armstrong 10465388d0dSNeil Armstrong static int dm_test_clk_bulk(struct unit_test_state *uts) 10565388d0dSNeil Armstrong { 10665388d0dSNeil Armstrong struct udevice *dev_clk, *dev_test; 10765388d0dSNeil Armstrong 10865388d0dSNeil Armstrong ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-sbox", 10965388d0dSNeil Armstrong &dev_clk)); 11065388d0dSNeil Armstrong ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test", 11165388d0dSNeil Armstrong &dev_test)); 11265388d0dSNeil Armstrong ut_assertok(sandbox_clk_test_get_bulk(dev_test)); 11365388d0dSNeil Armstrong 11465388d0dSNeil Armstrong ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); 11565388d0dSNeil Armstrong ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); 11665388d0dSNeil Armstrong 11765388d0dSNeil Armstrong /* Fixed clock does not support enable, thus should not fail */ 11865388d0dSNeil Armstrong ut_assertok(sandbox_clk_test_enable_bulk(dev_test)); 11965388d0dSNeil Armstrong ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); 12065388d0dSNeil Armstrong ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); 12165388d0dSNeil Armstrong 12265388d0dSNeil Armstrong /* Fixed clock does not support disable, thus should not fail */ 12365388d0dSNeil Armstrong ut_assertok(sandbox_clk_test_disable_bulk(dev_test)); 12465388d0dSNeil Armstrong ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); 12565388d0dSNeil Armstrong ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); 12665388d0dSNeil Armstrong 12765388d0dSNeil Armstrong /* Fixed clock does not support enable, thus should not fail */ 12865388d0dSNeil Armstrong ut_assertok(sandbox_clk_test_enable_bulk(dev_test)); 12965388d0dSNeil Armstrong ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); 13065388d0dSNeil Armstrong ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); 13165388d0dSNeil Armstrong 13265388d0dSNeil Armstrong /* Fixed clock does not support disable, thus should not fail */ 13365388d0dSNeil Armstrong ut_assertok(sandbox_clk_test_release_bulk(dev_test)); 13465388d0dSNeil Armstrong ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI)); 13565388d0dSNeil Armstrong ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C)); 13665388d0dSNeil Armstrong 13765388d0dSNeil Armstrong return 0; 13865388d0dSNeil Armstrong } 13965388d0dSNeil Armstrong DM_TEST(dm_test_clk_bulk, DM_TESTF_SCAN_FDT); 140