diff options
author | Fabio Baltieri <fabiobaltieri@google.com> | 2022-03-01 13:23:45 +0000 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2022-03-14 15:56:52 +0000 |
commit | 69955023ac1806613e740632642571bfaa50ea65 (patch) | |
tree | 1e93dcdee6edb7924747cd37d25bc540d8a37a6b | |
parent | cf85710446f841b339905f25891e8f7fb734d083 (diff) | |
download | chrome-ec-69955023ac1806613e740632642571bfaa50ea65.tar.gz |
zephyr: pwm: de-shim the fan driver PWM APIs
Refactor the fan driver to drop the shimmed PWM code and use the Zephyr
PWM APIs directly. Drop the corresponding dts entries, leaving few
pwm.dts files empty, will clear those at a later stage.
BRANCH=none
BUG=b:217741090
TEST=zmake testall
TEST=build and run on volteer, play with faninfo and fanset
Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
Change-Id: I7eac734938c3b30768544a60af52b37d39a164fc
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3497452
Reviewed-by: Tristan Honscheid <honscheid@google.com>
Reviewed-by: Keith Short <keithshort@chromium.org>
-rw-r--r-- | zephyr/Kconfig.temperature | 1 | ||||
-rw-r--r-- | zephyr/boards/riscv/it8xxx2_evb/it8xxx2_evb.dts | 7 | ||||
-rw-r--r-- | zephyr/dts/bindings/fan/named-fans.yaml | 8 | ||||
-rw-r--r-- | zephyr/projects/brya/brya/fan.dts | 8 | ||||
-rw-r--r-- | zephyr/projects/brya/brya/pwm.dts | 11 | ||||
-rw-r--r-- | zephyr/projects/npcx_evb/npcx7/fan.dts | 8 | ||||
-rw-r--r-- | zephyr/projects/npcx_evb/npcx7/pwm.dts | 11 | ||||
-rw-r--r-- | zephyr/projects/npcx_evb/npcx9/fan.dts | 8 | ||||
-rw-r--r-- | zephyr/projects/npcx_evb/npcx9/pwm.dts | 11 | ||||
-rw-r--r-- | zephyr/projects/skyrim/fan.dts | 8 | ||||
-rw-r--r-- | zephyr/projects/skyrim/pwm.dts | 8 | ||||
-rw-r--r-- | zephyr/projects/volteer/volteer/fan.dts | 8 | ||||
-rw-r--r-- | zephyr/projects/volteer/volteer/pwm.dts | 11 | ||||
-rw-r--r-- | zephyr/shim/src/fan.c | 97 |
14 files changed, 105 insertions, 100 deletions
diff --git a/zephyr/Kconfig.temperature b/zephyr/Kconfig.temperature index a3ff8f12cf..69a6b71d39 100644 --- a/zephyr/Kconfig.temperature +++ b/zephyr/Kconfig.temperature @@ -79,7 +79,6 @@ endif # PLATFORM_EC_TEMP_SENSOR_FIRST_READ_DELAY config PLATFORM_EC_FAN bool "Fan support" - depends on PLATFORM_EC_PWM help Enables support for fans. Allows periodic thermal task to automatically set the fan speed (control temperature). diff --git a/zephyr/boards/riscv/it8xxx2_evb/it8xxx2_evb.dts b/zephyr/boards/riscv/it8xxx2_evb/it8xxx2_evb.dts index 3e675c61eb..c060a842f3 100644 --- a/zephyr/boards/riscv/it8xxx2_evb/it8xxx2_evb.dts +++ b/zephyr/boards/riscv/it8xxx2_evb/it8xxx2_evb.dts @@ -154,10 +154,6 @@ */ frequency = <324>; }; - pwm_fan: fan { - pwms = <&pwm7 PWM_CHANNEL_7 PWM_POLARITY_NORMAL>; - frequency = <30000>; - }; }; named-fans { @@ -165,7 +161,8 @@ fan_0 { label = "FAN_0"; - pwm = <&pwm_fan>; + pwms = <&pwm7 PWM_CHANNEL_7 PWM_POLARITY_NORMAL>; + pwm-frequency = <30000>; tach = <&tach0>; rpm_min = <1500>; rpm_start = <1500>; diff --git a/zephyr/dts/bindings/fan/named-fans.yaml b/zephyr/dts/bindings/fan/named-fans.yaml index 0595a20f54..019b7dba5a 100644 --- a/zephyr/dts/bindings/fan/named-fans.yaml +++ b/zephyr/dts/bindings/fan/named-fans.yaml @@ -30,11 +30,15 @@ child-binding: type: int description: Maximum fan speed (in RPM) - pwm: - type: phandle + pwms: + type: phandle-array required: true description: PWM channel to control the fan + pwm-frequency: + type: int + description: + PWM frequency in Hz. tach: type: phandle required: false diff --git a/zephyr/projects/brya/brya/fan.dts b/zephyr/projects/brya/brya/fan.dts index cccd6315bf..a0323b171b 100644 --- a/zephyr/projects/brya/brya/fan.dts +++ b/zephyr/projects/brya/brya/fan.dts @@ -9,7 +9,8 @@ fan_0 { label = "FAN_0"; - pwm = <&pwm_fan>; + pwms = <&pwm5 0 PWM_POLARITY_NORMAL>; + pwm-frequency = <1000>; rpm_min = <2200>; rpm_start = <2200>; rpm_max = <4200>; @@ -27,3 +28,8 @@ sample-clk = <NPCX_TACH_FREQ_LFCLK>; /* Use LFCLK as sampling clock */ pulses-per-round = <2>; /* number of pulses per round of encoder */ }; + +&pwm5 { + status = "okay"; + drive-open-drain; +}; diff --git a/zephyr/projects/brya/brya/pwm.dts b/zephyr/projects/brya/brya/pwm.dts index 0e457d2e8b..4e212f6d0b 100644 --- a/zephyr/projects/brya/brya/pwm.dts +++ b/zephyr/projects/brya/brya/pwm.dts @@ -6,16 +6,5 @@ / { named-pwms { compatible = "named-pwms"; - - pwm_fan: fan { - pwms = <&pwm5 0 PWM_POLARITY_NORMAL>; - frequency = <1000>; - }; }; }; - -/* Fan control */ -&pwm5 { - status = "okay"; - drive-open-drain; -}; diff --git a/zephyr/projects/npcx_evb/npcx7/fan.dts b/zephyr/projects/npcx_evb/npcx7/fan.dts index de2852d73a..e4eba80663 100644 --- a/zephyr/projects/npcx_evb/npcx7/fan.dts +++ b/zephyr/projects/npcx_evb/npcx7/fan.dts @@ -9,7 +9,8 @@ fan_0 { label = "FAN_0"; - pwm = <&pwm_fan>; + pwms = <&pwm0 0 PWM_POLARITY_NORMAL>; + pwm-frequency = <25000>; rpm_min = <1000>; rpm_start = <1000>; rpm_max = <5200>; @@ -27,3 +28,8 @@ sample-clk = <NPCX_TACH_FREQ_LFCLK>; /* Use LFCLK as sampling clock */ pulses-per-round = <2>; /* number of pulses per round of encoder */ }; + +&pwm0 { + status = "okay"; + drive-open-drain; +}; diff --git a/zephyr/projects/npcx_evb/npcx7/pwm.dts b/zephyr/projects/npcx_evb/npcx7/pwm.dts index b898dee131..96da83ca32 100644 --- a/zephyr/projects/npcx_evb/npcx7/pwm.dts +++ b/zephyr/projects/npcx_evb/npcx7/pwm.dts @@ -6,16 +6,5 @@ / { named-pwms { compatible = "named-pwms"; - - pwm_fan: fan { - pwms = <&pwm0 0 PWM_POLARITY_NORMAL>; - frequency = <25000>; - }; }; }; - -/* fan */ -&pwm0 { - status = "okay"; - drive-open-drain; -}; diff --git a/zephyr/projects/npcx_evb/npcx9/fan.dts b/zephyr/projects/npcx_evb/npcx9/fan.dts index de2852d73a..e4eba80663 100644 --- a/zephyr/projects/npcx_evb/npcx9/fan.dts +++ b/zephyr/projects/npcx_evb/npcx9/fan.dts @@ -9,7 +9,8 @@ fan_0 { label = "FAN_0"; - pwm = <&pwm_fan>; + pwms = <&pwm0 0 PWM_POLARITY_NORMAL>; + pwm-frequency = <25000>; rpm_min = <1000>; rpm_start = <1000>; rpm_max = <5200>; @@ -27,3 +28,8 @@ sample-clk = <NPCX_TACH_FREQ_LFCLK>; /* Use LFCLK as sampling clock */ pulses-per-round = <2>; /* number of pulses per round of encoder */ }; + +&pwm0 { + status = "okay"; + drive-open-drain; +}; diff --git a/zephyr/projects/npcx_evb/npcx9/pwm.dts b/zephyr/projects/npcx_evb/npcx9/pwm.dts index b898dee131..96da83ca32 100644 --- a/zephyr/projects/npcx_evb/npcx9/pwm.dts +++ b/zephyr/projects/npcx_evb/npcx9/pwm.dts @@ -6,16 +6,5 @@ / { named-pwms { compatible = "named-pwms"; - - pwm_fan: fan { - pwms = <&pwm0 0 PWM_POLARITY_NORMAL>; - frequency = <25000>; - }; }; }; - -/* fan */ -&pwm0 { - status = "okay"; - drive-open-drain; -}; diff --git a/zephyr/projects/skyrim/fan.dts b/zephyr/projects/skyrim/fan.dts index 7ab15229e1..0964b69f99 100644 --- a/zephyr/projects/skyrim/fan.dts +++ b/zephyr/projects/skyrim/fan.dts @@ -9,7 +9,8 @@ fan_0 { label = "FAN_0"; - pwm = <&pwm_fan>; + pwms = <&pwm0 0 PWM_POLARITY_NORMAL>; + pwm-frequency = <25000>; rpm_min = <1000>; rpm_start = <1000>; rpm_max = <6500>; @@ -27,3 +28,8 @@ sample-clk = <NPCX_TACH_FREQ_LFCLK>; /* Use LFCLK as sampling clock */ pulses-per-round = <2>; /* number of pulses per round of encoder */ }; + +&pwm0 { + status = "okay"; + drive-open-drain; +}; diff --git a/zephyr/projects/skyrim/pwm.dts b/zephyr/projects/skyrim/pwm.dts index 81fa42d980..4c52b04859 100644 --- a/zephyr/projects/skyrim/pwm.dts +++ b/zephyr/projects/skyrim/pwm.dts @@ -7,10 +7,6 @@ named-pwms { compatible = "named-pwms"; - pwm_fan: ec_fan_pwm { - pwms = <&pwm0 0 PWM_POLARITY_NORMAL>; - frequency = <25000>; - }; led_charge: ec_pwm_led_chrg_l { pwms = <&pwm2 0 PWM_POLARITY_NORMAL>; frequency = <100>; @@ -23,10 +19,6 @@ }; /* Fan control */ -&pwm0 { - status = "okay"; - drive-open-drain; -}; /* Amber charging LED */ &pwm2 { diff --git a/zephyr/projects/volteer/volteer/fan.dts b/zephyr/projects/volteer/volteer/fan.dts index e8fcc7194c..9544747ad8 100644 --- a/zephyr/projects/volteer/volteer/fan.dts +++ b/zephyr/projects/volteer/volteer/fan.dts @@ -9,7 +9,8 @@ fan_0 { label = "FAN_0"; - pwm = <&pwm_fan>; + pwms = <&pwm5 0 PWM_POLARITY_NORMAL>; + pwm-frequency = <25000>; rpm_min = <1900>; rpm_start = <1900>; rpm_max = <5900>; @@ -27,3 +28,8 @@ sample-clk = <NPCX_TACH_FREQ_LFCLK>; /* Use LFCLK as sampling clock */ pulses-per-round = <2>; /* number of pulses per round of encoder */ }; + +&pwm5 { + status = "okay"; + drive-open-drain; +}; diff --git a/zephyr/projects/volteer/volteer/pwm.dts b/zephyr/projects/volteer/volteer/pwm.dts index 22825ca5b8..96da83ca32 100644 --- a/zephyr/projects/volteer/volteer/pwm.dts +++ b/zephyr/projects/volteer/volteer/pwm.dts @@ -6,16 +6,5 @@ / { named-pwms { compatible = "named-pwms"; - - pwm_fan: fan_pwm { - pwms = <&pwm5 0 PWM_POLARITY_NORMAL>; - frequency = <25000>; - }; }; }; - -/* Fan control */ -&pwm5 { - status = "okay"; - drive-open-drain; -}; diff --git a/zephyr/shim/src/fan.c b/zephyr/shim/src/fan.c index cc05a48e29..da127b6662 100644 --- a/zephyr/shim/src/fan.c +++ b/zephyr/shim/src/fan.c @@ -6,17 +6,17 @@ #define DT_DRV_COMPAT named_fans #include <drivers/gpio.h> +#include <drivers/pwm.h> #include <drivers/sensor.h> #include <logging/log.h> #include <sys/util_macro.h> #include "fan.h" -#include "pwm.h" -#include "pwm/pwm.h" -#include "system.h" -#include "math_util.h" -#include "hooks.h" #include "gpio_signal.h" +#include "hooks.h" +#include "math_util.h" +#include "system.h" +#include "util.h" LOG_MODULE_REGISTER(fan_shim, LOG_LEVEL_ERR); @@ -51,22 +51,21 @@ BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 1, .rpm = &node_id##_rpm, \ }, -#define FAN_CONTROL_INST(node_id) \ - [node_id] = { \ - .pwm_id = PWM_CHANNEL(DT_PHANDLE(node_id, pwm)), \ +#define FAN_CONTROL_INST(node_id) \ + [node_id] = { \ + .pwm = DEVICE_DT_GET(DT_PWMS_CTLR(node_id)), \ + .channel = DT_PWMS_CHANNEL(node_id), \ + .flags = DT_PWMS_FLAGS(node_id), \ + .period_us = (USEC_PER_SEC/DT_PROP(node_id, pwm_frequency)), \ + .tach = DEVICE_DT_GET(DT_PHANDLE(node_id, tach)), \ }, DT_INST_FOREACH_CHILD(0, FAN_CONFIGS) -const struct fan_t fans[] = { +const struct fan_t fans[FAN_CH_COUNT] = { DT_INST_FOREACH_CHILD(0, FAN_INST) }; -#define TACHO_DEV_INIT(node_id) { \ - fan_control[node_id].tach = \ - DEVICE_DT_GET(DT_PHANDLE(node_id, tach)); \ - } - /* Rpm deviation (Unit:percent) */ #ifndef RPM_DEVIATION #define RPM_DEVIATION 7 @@ -97,19 +96,57 @@ struct fan_status_t { unsigned int flags; /* Automatic fan status */ enum fan_status auto_status; + /* Current PWM duty cycle percentage */ + int pwm_percent; + /* Whether the PWM channel is enabled */ + bool pwm_enabled; }; -/* Data structure to define tachometer. */ +/* Data structure to define PWM and tachometer. */ struct fan_control_t { + const struct device *pwm; + uint32_t channel; + pwm_flags_t flags; + uint32_t period_us; + const struct device *tach; - enum pwm_channel pwm_id; }; static struct fan_status_t fan_status[FAN_CH_COUNT]; -static struct fan_control_t fan_control[] = { +static const struct fan_control_t fan_control[FAN_CH_COUNT] = { DT_INST_FOREACH_CHILD(0, FAN_CONTROL_INST) }; +static void fan_pwm_update(int ch) +{ + const struct fan_control_t *ctrl = &fan_control[ch]; + struct fan_status_t *status = &fan_status[ch]; + uint32_t pulse_us; + int ret; + + if (!device_is_ready(ctrl->pwm)) { + LOG_ERR("PWM device %s not ready", ctrl->pwm->name); + return; + } + + if (status->pwm_enabled) { + pulse_us = DIV_ROUND_NEAREST( + ctrl->period_us * status->pwm_percent, 100); + } else { + pulse_us = 0; + } + + LOG_DBG("FAN PWM %s set percent (%d), pulse %d", ctrl->pwm->name, + status->pwm_percent, pulse_us); + + ret = pwm_pin_set_usec(ctrl->pwm, ctrl->channel, ctrl->period_us, + pulse_us, ctrl->flags); + if (ret) { + LOG_ERR("pwm_pin_set_usec() failed %s (%d)", + ctrl->pwm->name, ret); + } +} + /** * Get fan rpm value * @@ -293,10 +330,7 @@ DECLARE_HOOK(HOOK_TICK, fan_tick_func, HOOK_PRIO_DEFAULT); int fan_get_duty(int ch) { - enum pwm_channel pwm_id = fan_control[ch].pwm_id; - - /* Return percent */ - return pwm_get_duty(pwm_id); + return fan_status[ch].pwm_percent; } int fan_get_rpm_mode(int ch) @@ -326,30 +360,24 @@ int fan_get_rpm_actual(int ch) int fan_get_enabled(int ch) { - enum pwm_channel pwm_id = fan_control[ch].pwm_id; - - return pwm_get_enabled(pwm_id); + return fan_status[ch].pwm_enabled; } void fan_set_enabled(int ch, int enabled) { - enum pwm_channel pwm_id = fan_control[ch].pwm_id; - if (!enabled) { fan_status[ch].auto_status = FAN_STATUS_STOPPED; } - pwm_enable(pwm_id, enabled); + fan_status[ch].pwm_enabled = enabled; + + fan_pwm_update(ch); } void fan_channel_setup(int ch, unsigned int flags) { struct fan_status_t *status = fan_status + ch; - if (flags & FAN_USE_RPM_MODE) { - DT_INST_FOREACH_CHILD(0, TACHO_DEV_INIT) - } - status->flags = flags; /* Set default fan states */ status->current_fan_mode = FAN_DUTY; @@ -358,8 +386,6 @@ void fan_channel_setup(int ch, unsigned int flags) void fan_set_duty(int ch, int percent) { - enum pwm_channel pwm_id = fan_control[ch].pwm_id; - /* duty is zero */ if (!percent) { fan_status[ch].auto_status = FAN_STATUS_STOPPED; @@ -370,8 +396,9 @@ void fan_set_duty(int ch, int percent) disable_sleep(SLEEP_MASK_FAN); } - /* Set the duty cycle of PWM */ - pwm_set_duty(pwm_id, percent); + fan_status[ch].pwm_percent = percent; + + fan_pwm_update(ch); } int fan_get_rpm_target(int ch) |