Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mateusz Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com> |
Mateusz Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
Mateusz Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 6 | #include <fdtdec.h> |
| 7 | #include <dm.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 8 | #include <malloc.h> |
Mateusz Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 9 | #include <dm/device.h> |
| 10 | #include <dm/root.h> |
| 11 | #include <dm/test.h> |
| 12 | #include <dm/util.h> |
| 13 | #include <power/pmic.h> |
| 14 | #include <spmi/spmi.h> |
| 15 | #include <asm/gpio.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 16 | #include <test/test.h> |
Mateusz Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 17 | #include <test/ut.h> |
| 18 | |
Yuepeng Xing | 71cc021 | 2022-12-02 14:23:07 +0800 | [diff] [blame] | 19 | /* Test if bus children got probed properly*/ |
Mateusz Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 20 | static int dm_test_spmi_probe(struct unit_test_state *uts) |
| 21 | { |
| 22 | const char *name = "spmi@0"; |
| 23 | struct udevice *bus, *dev; |
| 24 | |
| 25 | ut_assertok(uclass_get_device(UCLASS_SPMI, 0, &bus)); |
| 26 | |
| 27 | /* Check bus name */ |
| 28 | ut_asserteq_str(name, bus->name); |
| 29 | |
| 30 | /* Check that it has some devices */ |
| 31 | ut_asserteq(device_has_children(bus), true); |
| 32 | |
| 33 | ut_assertok(device_find_first_child(bus, &dev)); |
| 34 | |
| 35 | /* There should be at least one child */ |
| 36 | ut_assertnonnull(dev); |
| 37 | |
| 38 | /* Check that only PMICs are connected to the bus */ |
| 39 | while (dev) { |
| 40 | ut_asserteq(device_get_uclass_id(dev), UCLASS_PMIC); |
| 41 | device_find_next_child(&dev); |
| 42 | } |
| 43 | |
| 44 | return 0; |
| 45 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 46 | DM_TEST(dm_test_spmi_probe, UT_TESTF_SCAN_FDT); |
Mateusz Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 47 | |
| 48 | /* Test if it's possible to read bus directly and indirectly */ |
| 49 | static int dm_test_spmi_access(struct unit_test_state *uts) |
| 50 | { |
| 51 | const char *pmic_name = "pm8916@0"; |
| 52 | struct udevice *bus, *pmic; |
| 53 | |
| 54 | ut_assertok(uclass_get_device(UCLASS_SPMI, 0, &bus)); |
| 55 | |
| 56 | ut_assertok(device_get_child(bus, 0, &pmic)); |
| 57 | |
| 58 | /* Sanity check if it's proper PMIC */ |
| 59 | ut_asserteq_str(pmic_name, pmic->name); |
| 60 | |
| 61 | /* Read PMIC ID reg using SPMI bus - it assumes it has slaveID == 0*/ |
| 62 | ut_asserteq(spmi_reg_read(bus, 0, 0xC0, 0x4), 0x10); |
| 63 | ut_asserteq(spmi_reg_read(bus, 0, 0xC0, 0x5), 0x5); |
| 64 | |
| 65 | /* Read ID reg via pmic interface */ |
| 66 | ut_asserteq(pmic_reg_read(pmic, 0xC004), 0x10); |
| 67 | ut_asserteq(pmic_reg_read(pmic, 0xC005), 0x5); |
| 68 | |
| 69 | return 0; |
| 70 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 71 | DM_TEST(dm_test_spmi_access, UT_TESTF_SCAN_FDT); |
Mateusz Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 72 | |
| 73 | |
| 74 | /* Test if it's possible to access GPIO that should be in pmic */ |
| 75 | static int dm_test_spmi_access_peripheral(struct unit_test_state *uts) |
| 76 | { |
| 77 | struct udevice *dev; |
| 78 | unsigned int offset, gpio; |
| 79 | const char *name; |
| 80 | int offset_count; |
| 81 | |
| 82 | /* Get second pin of PMIC GPIO */ |
Caleb Connolly | 1edc45f | 2024-01-08 15:30:51 +0000 | [diff] [blame] | 83 | ut_assertok(gpio_lookup_name("pmic1", &dev, &offset, &gpio)); |
Mateusz Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 84 | |
| 85 | /* Check if PMIC is parent */ |
| 86 | ut_asserteq(device_get_uclass_id(dev->parent), UCLASS_PMIC); |
| 87 | |
| 88 | /* This should be second gpio */ |
| 89 | ut_asserteq(1, offset); |
| 90 | |
| 91 | name = gpio_get_bank_info(dev, &offset_count); |
| 92 | |
| 93 | /* Check bank name */ |
Caleb Connolly | 1edc45f | 2024-01-08 15:30:51 +0000 | [diff] [blame] | 94 | ut_asserteq_str("pmic", name); |
Mateusz Kulikowski | c7e4fbb | 2016-03-31 23:12:28 +0200 | [diff] [blame] | 95 | /* Check pin count */ |
| 96 | ut_asserteq(4, offset_count); |
| 97 | |
| 98 | ut_assertok(gpio_request(gpio, "testing")); |
| 99 | |
| 100 | /* Try to set/clear gpio */ |
| 101 | ut_assertok(gpio_direction_output(gpio, 0)); |
| 102 | ut_asserteq(gpio_get_value(gpio), 0); |
| 103 | ut_assertok(gpio_direction_output(gpio, 1)); |
| 104 | ut_asserteq(gpio_get_value(gpio), 1); |
| 105 | ut_assertok(gpio_direction_input(gpio)); |
| 106 | ut_asserteq(gpio_get_value(gpio), 1); |
| 107 | |
| 108 | ut_assertok(gpio_free(gpio)); |
| 109 | |
| 110 | return 0; |
| 111 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 112 | DM_TEST(dm_test_spmi_access_peripheral, UT_TESTF_SCAN_FDT); |