summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/pwm_led_driver
diff options
context:
space:
mode:
Diffstat (limited to 'zephyr/test/drivers/pwm_led_driver')
-rw-r--r--zephyr/test/drivers/pwm_led_driver/CMakeLists.txt10
-rw-r--r--zephyr/test/drivers/pwm_led_driver/led_pins.dts108
-rw-r--r--zephyr/test/drivers/pwm_led_driver/prj.conf6
-rw-r--r--zephyr/test/drivers/pwm_led_driver/src/led.c91
4 files changed, 215 insertions, 0 deletions
diff --git a/zephyr/test/drivers/pwm_led_driver/CMakeLists.txt b/zephyr/test/drivers/pwm_led_driver/CMakeLists.txt
new file mode 100644
index 0000000000..3e4f48724d
--- /dev/null
+++ b/zephyr/test/drivers/pwm_led_driver/CMakeLists.txt
@@ -0,0 +1,10 @@
+# Copyright 2022 The ChromiumOS Authors.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# Add include paths
+target_include_directories(app PRIVATE
+ "${PLATFORM_EC}/zephyr/shim/src/led_driver")
+
+# Add source files
+target_sources(app PRIVATE src/led.c)
diff --git a/zephyr/test/drivers/pwm_led_driver/led_pins.dts b/zephyr/test/drivers/pwm_led_driver/led_pins.dts
new file mode 100644
index 0000000000..5455a21bef
--- /dev/null
+++ b/zephyr/test/drivers/pwm_led_driver/led_pins.dts
@@ -0,0 +1,108 @@
+/* Copyright 2022 The ChromiumOS Authors.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <zephyr/dt-bindings/pwm/pwm.h>
+
+/ {
+ pwms {
+ #address-cells = <1>;
+ #size-cells = <1>;
+
+ pwm_blue_left: pwm@0 {
+ compatible = "cros,pwm-mock";
+ reg = <0 1>;
+ #pwm-cells = <2>;
+ status = "okay";
+ };
+ pwm_white_left: pwm@1 {
+ compatible = "cros,pwm-mock";
+ reg = <1 1>;
+ #pwm-cells = <2>;
+ status = "okay";
+ };
+ pwm_amber_right: pwm@2 {
+ compatible = "cros,pwm-mock";
+ reg = <2 1>;
+ #pwm-cells = <2>;
+ status = "okay";
+ };
+ pwm_white_right: pwm@3 {
+ compatible = "cros,pwm-mock";
+ reg = <3 1>;
+ #pwm-cells = <2>;
+ status = "okay";
+ };
+ };
+
+ pwmleds {
+ compatible = "cros-ec,pwm-pin-config";
+
+ pwmled_blue_left: pwmled_b_left {
+ #led-pin-cells = <1>;
+ pwms = <&pwm_blue_left 0 1000000>;
+ };
+ pwmled_white_left: pwmled_w_left {
+ #led-pin-cells = <1>;
+ pwms = <&pwm_white_left 0 1000000>;
+ };
+ pwmled_amber_right: pwmled_y_right {
+ #led-pin-cells = <1>;
+ pwms = <&pwm_amber_right 0 1000000>;
+ };
+ pwmled_white_right: pwmled_w_right {
+ #led-pin-cells = <1>;
+ pwms = <&pwm_white_right 0 1000000>;
+ };
+ };
+
+ pwm-led-pins {
+ compatible = "cros-ec,pwm-led-pins";
+
+ color_off_left: color-off-left {
+ led-color = "LED_OFF";
+ led-id = "EC_LED_ID_LEFT_LED";
+ led-pins = <&pwmled_blue_left 0>,
+ <&pwmled_white_left 0>;
+ };
+ color_off_right: color-off-right {
+ led-color = "LED_OFF";
+ led-id = "EC_LED_ID_RIGHT_LED";
+ led-pins = <&pwmled_amber_right 0>,
+ <&pwmled_white_right 0>;
+ };
+ color_blue_left: color-blue-left {
+ led-color = "LED_BLUE";
+ led-id = "EC_LED_ID_LEFT_LED";
+ br-color = "EC_LED_COLOR_BLUE";
+ led-pins = <&pwmled_blue_left 100>,
+ <&pwmled_white_left 0>;
+ };
+ color_amber_right: color-amber-right {
+ led-color = "LED_AMBER";
+ led-id = "EC_LED_ID_RIGHT_LED";
+ br-color = "EC_LED_COLOR_AMBER";
+ led-pins = <&pwmled_amber_right 100>,
+ <&pwmled_white_right 0>;
+ };
+ color_white_left: color-white-left {
+ led-color = "LED_WHITE";
+ led-id = "EC_LED_ID_LEFT_LED";
+ br-color = "EC_LED_COLOR_WHITE";
+ led-pins = <&pwmled_blue_left 0>,
+ <&pwmled_white_left 100>;
+ };
+ color_white_right: color-white-right {
+ led-color = "LED_WHITE";
+ led-id = "EC_LED_ID_RIGHT_LED";
+ br-color = "EC_LED_COLOR_WHITE";
+ led-pins = <&pwmled_amber_right 0>,
+ <&pwmled_white_right 100>;
+ };
+ };
+
+ led-colors {
+ compatible = "cros-ec,led-policy";
+ };
+};
diff --git a/zephyr/test/drivers/pwm_led_driver/prj.conf b/zephyr/test/drivers/pwm_led_driver/prj.conf
new file mode 100644
index 0000000000..9556721725
--- /dev/null
+++ b/zephyr/test/drivers/pwm_led_driver/prj.conf
@@ -0,0 +1,6 @@
+# Copyright 2022 The ChromiumOS Authors.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+CONFIG_PLATFORM_EC_LED_DT=y
+CONFIG_PWM_MOCK=y
diff --git a/zephyr/test/drivers/pwm_led_driver/src/led.c b/zephyr/test/drivers/pwm_led_driver/src/led.c
new file mode 100644
index 0000000000..95a183cebe
--- /dev/null
+++ b/zephyr/test/drivers/pwm_led_driver/src/led.c
@@ -0,0 +1,91 @@
+/* Copyright 2022 The ChromiumOS Authors.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <zephyr/ztest.h>
+#include <zephyr/ztest_assert.h>
+
+#include <zephyr/device.h>
+#include <zephyr/drivers/pwm.h>
+
+#include "ec_commands.h"
+#include "led.h"
+#include "led_common.h"
+#include "test/drivers/test_state.h"
+#include "test/drivers/utils.h"
+
+ZTEST_SUITE(pwm_led_driver, drivers_predicate_post_main, NULL, NULL, NULL,
+ NULL);
+
+ZTEST(pwm_led_driver, test_led_set_brightness)
+{
+ const uint8_t brightness_off[EC_LED_COLOR_COUNT] = {};
+ const uint8_t brightness_white[EC_LED_COLOR_COUNT] = {
+ [EC_LED_COLOR_WHITE] = 1
+ };
+ const uint8_t brightness_amber[EC_LED_COLOR_COUNT] = {
+ [EC_LED_COLOR_AMBER] = 1
+ };
+ const struct device *pwm_blue_left =
+ DEVICE_DT_GET(DT_NODELABEL(pwm_blue_left));
+ const struct device *pwm_white_left =
+ DEVICE_DT_GET(DT_NODELABEL(pwm_white_left));
+ const struct device *pwm_amber_right =
+ DEVICE_DT_GET(DT_NODELABEL(pwm_amber_right));
+ const struct device *pwm_white_right =
+ DEVICE_DT_GET(DT_NODELABEL(pwm_white_right));
+
+ /* Turn off all LEDs */
+ led_set_brightness(EC_LED_ID_LEFT_LED, brightness_off);
+ led_set_brightness(EC_LED_ID_RIGHT_LED, brightness_off);
+ zassert_equal(pwm_mock_get_duty(pwm_blue_left, 0), 0, NULL);
+ zassert_equal(pwm_mock_get_duty(pwm_white_left, 0), 0, NULL);
+ zassert_equal(pwm_mock_get_duty(pwm_amber_right, 0), 0, NULL);
+ zassert_equal(pwm_mock_get_duty(pwm_white_right, 0), 0, NULL);
+
+ /* Call led_set_color(LED_WHITE, LEFT_LED) */
+ led_set_brightness(EC_LED_ID_LEFT_LED, brightness_white);
+ zassert_equal(pwm_mock_get_duty(pwm_blue_left, 0), 0, NULL);
+ zassert_equal(pwm_mock_get_duty(pwm_white_left, 0), 100, NULL);
+ zassert_equal(pwm_mock_get_duty(pwm_amber_right, 0), 0, NULL);
+ zassert_equal(pwm_mock_get_duty(pwm_white_right, 0), 0, NULL);
+
+ /* Unsupporte, call led_set_color(LED_OFF, LEFT_LED) */
+ led_set_brightness(EC_LED_ID_LEFT_LED, brightness_amber);
+ zassert_equal(pwm_mock_get_duty(pwm_blue_left, 0), 0, NULL);
+ zassert_equal(pwm_mock_get_duty(pwm_white_left, 0), 0, NULL);
+ zassert_equal(pwm_mock_get_duty(pwm_amber_right, 0), 0, NULL);
+ zassert_equal(pwm_mock_get_duty(pwm_white_right, 0), 0, NULL);
+
+ /* Call led_set_color(AMBER, RIGHT_LED) */
+ led_set_brightness(EC_LED_ID_RIGHT_LED, brightness_amber);
+ zassert_equal(pwm_mock_get_duty(pwm_blue_left, 0), 0, NULL);
+ zassert_equal(pwm_mock_get_duty(pwm_white_left, 0), 0, NULL);
+ zassert_equal(pwm_mock_get_duty(pwm_amber_right, 0), 100, NULL);
+ zassert_equal(pwm_mock_get_duty(pwm_white_right, 0), 0, NULL);
+}
+
+ZTEST(pwm_led_driver, test_led_get_brightness)
+{
+ uint8_t brightness[EC_LED_COLOR_COUNT];
+ const uint8_t expected_left[EC_LED_COLOR_COUNT] = {
+ [EC_LED_COLOR_BLUE] = 100,
+ [EC_LED_COLOR_WHITE] = 100,
+ };
+ const uint8_t expected_right[EC_LED_COLOR_COUNT] = {
+ [EC_LED_COLOR_WHITE] = 100,
+ [EC_LED_COLOR_AMBER] = 100,
+ };
+
+ /* Verify LED colors defined in device tree are reflected in the
+ * brightness array.
+ */
+ memset(brightness, 255, sizeof(brightness));
+ led_get_brightness_range(EC_LED_ID_LEFT_LED, brightness);
+ zassert_mem_equal(brightness, expected_left, sizeof(brightness), NULL);
+
+ memset(brightness, 255, sizeof(brightness));
+ led_get_brightness_range(EC_LED_ID_RIGHT_LED, brightness);
+ zassert_mem_equal(brightness, expected_right, sizeof(brightness), NULL);
+}