[PATCH 3/3] hw/sensor: max31785 : update the tach input based on the tach margin percentage

Maheswara Kurapati posted 3 patches 3 years, 7 months ago
Maintainers: Paolo Bonzini <pbonzini@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Eduardo Habkost <eduardo@habkost.net>
[PATCH 3/3] hw/sensor: max31785 : update the tach input based on the tach margin percentage
Posted by Maheswara Kurapati 3 years, 7 months ago
Update the tach input based on the percentage of tach target.  The tach margin
could be a +ve or -ve margin of the target tach rpm.

Signed-off-by: Maheswara Kurapati <quic_mkurapat@quicinc.com>
---
 hw/sensor/max31785.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/hw/sensor/max31785.c b/hw/sensor/max31785.c
index 1cb31c2e82..c37b154130 100644
--- a/hw/sensor/max31785.c
+++ b/hw/sensor/max31785.c
@@ -71,7 +71,7 @@
 /* FAN_CONFIG_1_2 */
 #define MAX31785_MFR_FAN_CONFIG                0xF1
 #define MAX31785_FAN_CONFIG_ENABLE             BIT(7)
-#define MAX31785_FAN_CONFIG_RPM_PWM            BIT(6)
+#define MAX31785_FAN_CONFIG_FAN_MODE_RPM       BIT(6)
 #define MAX31785_FAN_CONFIG_PULSE(pulse)       (pulse << 4)
 #define MAX31785_DEFAULT_FAN_CONFIG_1_2(pulse)                                 \
     (MAX31785_FAN_CONFIG_ENABLE | MAX31785_FAN_CONFIG_PULSE(pulse))
@@ -316,6 +316,8 @@ static int max31785_write_data(PMBusDevice *pmdev, const uint8_t *buf,
                                uint8_t len)
 {
     MAX31785State *s = MAX31785(pmdev);
+    int16_t   tach_margin = 0;
+
     if (len == 0) {
         qemu_log_mask(LOG_GUEST_ERROR, "%s: writing empty data\n", __func__);
         return -1;
@@ -342,9 +344,23 @@ static int max31785_write_data(PMBusDevice *pmdev, const uint8_t *buf,
     case PMBUS_FAN_COMMAND_1:
         if (pmdev->page <= MAX31785_MAX_FAN_PAGE) {
             pmdev->pages[pmdev->page].fan_command_1 = pmbus_receive16(pmdev);
-            pmdev->pages[pmdev->page].read_fan_speed_1 =
-                ((MAX31785_DEFAULT_FAN_SPEED / MAX31785_DEFAULT_FAN_MAX_PWM) *
-                pmdev->pages[pmdev->page].fan_command_1);
+            if ((pmdev->pages[pmdev->page].fan_config_1_2 &
+                 MAX31785_FAN_CONFIG_FAN_MODE_RPM)
+                  == MAX31785_FAN_CONFIG_FAN_MODE_RPM) {
+                /* calculate the tach margin (+ve or -ve) */
+                tach_margin = (int16_t)pmdev->pages[pmdev->page].fan_command_1 *
+                        ((float)s->tach_margin_percent[pmdev->page] / 100.0);
+
+                /* set the tach */
+                pmdev->pages[pmdev->page].read_fan_speed_1 =
+                 (uint16_t)(pmdev->pages[pmdev->page].fan_command_1 +
+                            tach_margin);
+            } else {
+                /* pwm mode */
+                pmdev->pages[pmdev->page].read_fan_speed_1 =
+                  ((MAX31785_DEFAULT_FAN_SPEED / MAX31785_DEFAULT_FAN_MAX_PWM) *
+                   pmdev->pages[pmdev->page].fan_command_1);
+            }
         }
         break;
 
-- 
2.25.1