1 /*
2 * Copyright 2023 Cirrus Logic, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/device.h>
8 #include <zephyr/drivers/charger.h>
9 #include <zephyr/drivers/i2c.h>
10 #include <zephyr/logging/log.h>
11 #include <zephyr/sys/byteorder.h>
12 #include <zephyr/sys/util.h>
13 #include <zephyr/ztest.h>
14 #include <zephyr/ztest_assert.h>
15
16 struct sbs_charger_fixture {
17 const struct device *dev;
18 const struct charger_driver_api *api;
19 };
20
sbs_charger_setup(void)21 static void *sbs_charger_setup(void)
22 {
23 static ZTEST_DMEM struct sbs_charger_fixture fixture;
24
25 fixture.dev = DEVICE_DT_GET_ANY(sbs_sbs_charger);
26
27 k_object_access_all_grant(fixture.dev);
28
29 zassert_true(device_is_ready(fixture.dev), "Charger not found");
30
31 return &fixture;
32 }
33
ZTEST_USER_F(sbs_charger,test_get_prop_failed_returns_negative)34 ZTEST_USER_F(sbs_charger, test_get_prop_failed_returns_negative)
35 {
36 /* Grab a bogus property */
37 charger_prop_t prop = CHARGER_PROP_MAX;
38 union charger_propval val = {0};
39
40 int ret = charger_get_prop(fixture->dev, prop, &val);
41
42 zassert_equal(ret, -ENOTSUP, "Getting bad property %d has a good status.", prop);
43 }
44
ZTEST_USER_F(sbs_charger,test_get_prop_success_returns_zero)45 ZTEST_USER_F(sbs_charger, test_get_prop_success_returns_zero)
46 {
47 /* Validate what props are supported by the driver */
48 charger_prop_t prop = CHARGER_PROP_ONLINE;
49 union charger_propval val = {0};
50
51 int ret = charger_get_prop(fixture->dev, prop, &val);
52
53 zassert_equal(ret, 0, "Getting good property %d has a good status.", prop);
54 }
55
ZTEST_USER_F(sbs_charger,test_set_prop_failed_returns_negative)56 ZTEST_USER_F(sbs_charger, test_set_prop_failed_returns_negative)
57 {
58 /* Set a bogus property */
59 charger_prop_t prop = CHARGER_PROP_MAX;
60 union charger_propval val = {0};
61
62 int ret = charger_set_prop(fixture->dev, prop, &val);
63
64 zassert_equal(ret, -ENOTSUP, "Setting bad property %d has a good status.", prop);
65 }
66
ZTEST_USER_F(sbs_charger,test_charge_enable_success_returns_zero)67 ZTEST_USER_F(sbs_charger, test_charge_enable_success_returns_zero)
68 {
69 int ret = charger_charge_enable(fixture->dev, true);
70
71 zassert_equal(ret, 0, "Enabling charge has a good status.");
72 }
73
74 ZTEST_SUITE(sbs_charger, NULL, sbs_charger_setup, NULL, NULL, NULL);
75