Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Przemyslaw Marczak | dca9450 | 2015-05-13 13:38:33 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Tests for the driver model pmic API |
| 4 | * |
| 5 | * Copyright (c) 2015 Samsung Electronics |
| 6 | * Przemyslaw Marczak <p.marczak@samsung.com> |
Przemyslaw Marczak | dca9450 | 2015-05-13 13:38:33 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <errno.h> |
| 11 | #include <dm.h> |
| 12 | #include <fdtdec.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 13 | #include <fsl_pmic.h> |
Przemyslaw Marczak | dca9450 | 2015-05-13 13:38:33 +0200 | [diff] [blame] | 14 | #include <malloc.h> |
| 15 | #include <dm/device-internal.h> |
| 16 | #include <dm/root.h> |
Przemyslaw Marczak | dca9450 | 2015-05-13 13:38:33 +0200 | [diff] [blame] | 17 | #include <dm/test.h> |
| 18 | #include <dm/uclass-internal.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 19 | #include <dm/util.h> |
Przemyslaw Marczak | dca9450 | 2015-05-13 13:38:33 +0200 | [diff] [blame] | 20 | #include <power/pmic.h> |
| 21 | #include <power/sandbox_pmic.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 22 | #include <test/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 23 | #include <test/ut.h> |
Przemyslaw Marczak | dca9450 | 2015-05-13 13:38:33 +0200 | [diff] [blame] | 24 | |
Przemyslaw Marczak | dca9450 | 2015-05-13 13:38:33 +0200 | [diff] [blame] | 25 | /* Test PMIC get method */ |
Lukasz Majewski | d0eb49e | 2018-05-15 16:26:42 +0200 | [diff] [blame] | 26 | |
| 27 | static inline int power_pmic_get(struct unit_test_state *uts, char *name) |
Przemyslaw Marczak | dca9450 | 2015-05-13 13:38:33 +0200 | [diff] [blame] | 28 | { |
Przemyslaw Marczak | dca9450 | 2015-05-13 13:38:33 +0200 | [diff] [blame] | 29 | struct udevice *dev; |
| 30 | |
| 31 | ut_assertok(pmic_get(name, &dev)); |
| 32 | ut_assertnonnull(dev); |
| 33 | |
| 34 | /* Check PMIC's name */ |
| 35 | ut_asserteq_str(name, dev->name); |
| 36 | |
| 37 | return 0; |
| 38 | } |
Lukasz Majewski | d0eb49e | 2018-05-15 16:26:42 +0200 | [diff] [blame] | 39 | |
| 40 | /* Test PMIC get method */ |
| 41 | static int dm_test_power_pmic_get(struct unit_test_state *uts) |
| 42 | { |
| 43 | power_pmic_get(uts, "sandbox_pmic"); |
| 44 | |
| 45 | return 0; |
| 46 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 47 | DM_TEST(dm_test_power_pmic_get, UT_TESTF_SCAN_FDT); |
Przemyslaw Marczak | dca9450 | 2015-05-13 13:38:33 +0200 | [diff] [blame] | 48 | |
Lukasz Majewski | 7b21a5a | 2018-05-15 16:26:43 +0200 | [diff] [blame] | 49 | /* PMIC get method - MC34708 - for 3 bytes transmission */ |
| 50 | static int dm_test_power_pmic_mc34708_get(struct unit_test_state *uts) |
| 51 | { |
| 52 | power_pmic_get(uts, "pmic@41"); |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 57 | DM_TEST(dm_test_power_pmic_mc34708_get, UT_TESTF_SCAN_FDT); |
Lukasz Majewski | 7b21a5a | 2018-05-15 16:26:43 +0200 | [diff] [blame] | 58 | |
Przemyslaw Marczak | dca9450 | 2015-05-13 13:38:33 +0200 | [diff] [blame] | 59 | /* Test PMIC I/O */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 60 | static int dm_test_power_pmic_io(struct unit_test_state *uts) |
Przemyslaw Marczak | dca9450 | 2015-05-13 13:38:33 +0200 | [diff] [blame] | 61 | { |
| 62 | const char *name = "sandbox_pmic"; |
| 63 | uint8_t out_buffer, in_buffer; |
| 64 | struct udevice *dev; |
| 65 | int reg_count, i; |
| 66 | |
| 67 | ut_assertok(pmic_get(name, &dev)); |
| 68 | |
| 69 | reg_count = pmic_reg_count(dev); |
| 70 | ut_asserteq(reg_count, SANDBOX_PMIC_REG_COUNT); |
| 71 | |
| 72 | /* |
| 73 | * Test PMIC I/O - write and read a loop counter. |
| 74 | * usually we can't write to all PMIC's registers in the real hardware, |
| 75 | * but we can to the sandbox pmic. |
| 76 | */ |
| 77 | for (i = 0; i < reg_count; i++) { |
| 78 | out_buffer = i; |
| 79 | ut_assertok(pmic_write(dev, i, &out_buffer, 1)); |
| 80 | ut_assertok(pmic_read(dev, i, &in_buffer, 1)); |
| 81 | ut_asserteq(out_buffer, in_buffer); |
| 82 | } |
| 83 | |
| 84 | return 0; |
| 85 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 86 | DM_TEST(dm_test_power_pmic_io, UT_TESTF_SCAN_FDT); |
Lukasz Majewski | 7b21a5a | 2018-05-15 16:26:43 +0200 | [diff] [blame] | 87 | |
| 88 | #define MC34708_PMIC_REG_COUNT 64 |
| 89 | #define MC34708_PMIC_TEST_VAL 0x125534 |
| 90 | static int dm_test_power_pmic_mc34708_regs_check(struct unit_test_state *uts) |
| 91 | { |
| 92 | struct udevice *dev; |
| 93 | int reg_count; |
| 94 | |
| 95 | ut_assertok(pmic_get("pmic@41", &dev)); |
| 96 | |
| 97 | /* Check number of PMIC registers */ |
| 98 | reg_count = pmic_reg_count(dev); |
| 99 | ut_asserteq(reg_count, MC34708_PMIC_REG_COUNT); |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 104 | DM_TEST(dm_test_power_pmic_mc34708_regs_check, UT_TESTF_SCAN_FDT); |
Lukasz Majewski | 7b21a5a | 2018-05-15 16:26:43 +0200 | [diff] [blame] | 105 | |
| 106 | static int dm_test_power_pmic_mc34708_rw_val(struct unit_test_state *uts) |
| 107 | { |
| 108 | struct udevice *dev; |
| 109 | int val; |
| 110 | |
| 111 | ut_assertok(pmic_get("pmic@41", &dev)); |
| 112 | |
| 113 | /* Check if single 3 byte read is successful */ |
| 114 | val = pmic_reg_read(dev, REG_POWER_CTL2); |
| 115 | ut_asserteq(val, 0x422100); |
| 116 | |
| 117 | /* Check if RW works */ |
| 118 | val = 0; |
| 119 | ut_assertok(pmic_reg_write(dev, REG_RTC_TIME, val)); |
| 120 | ut_assertok(pmic_reg_write(dev, REG_RTC_TIME, MC34708_PMIC_TEST_VAL)); |
| 121 | val = pmic_reg_read(dev, REG_RTC_TIME); |
| 122 | ut_asserteq(val, MC34708_PMIC_TEST_VAL); |
| 123 | |
| 124 | pmic_clrsetbits(dev, REG_POWER_CTL2, 0x3 << 8, 1 << 9); |
| 125 | val = pmic_reg_read(dev, REG_POWER_CTL2); |
| 126 | ut_asserteq(val, (0x422100 & ~(0x3 << 8)) | (1 << 9)); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 131 | DM_TEST(dm_test_power_pmic_mc34708_rw_val, UT_TESTF_SCAN_FDT); |