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