servo: introduce query for pd control on servo

This change adds the ability to query servo.py directly whether the
servo setup on the other end supports pd charging and control. This is
part of the effort to in the bug to shore up the autotest code to be
more capabilities oriented.

It also now removes that duplicate logic from the
server/cros/power/servo_v4_charge_utils.py, as well as some other small
maintenance on that code (reordering to get v4 role after checking v4 is
in use, deleting unused INA calls)

BUG=chromium:1009616
TEST=test_that --autotest_dir . $IP power_ServoChargeStress.3loops -b atlas

/.../results-1-power_ServoChargeStress.3loops   [  PASSED  ]

TEST=// remove charger cable
     test_that --autotest_dir . $IP power_ServoChargeStress.3loops -b atlas

... TestNAError: Servo setup does not support PD control. Check logs for
details.
// from logs
servo:1175| It appears that no charger is plugged into servo v4. Charger
port voltage: 5mv

Change-Id: Id2d7ae0723d110892c90ebbebd76c4f9b9dcf6ce
Signed-off-by: Ruben Rodriguez Buchillon <coconutruben@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/1832587
Reviewed-by: Puthikorn Voravootivat <puthik@chromium.org>
diff --git a/server/cros/servo/servo.py b/server/cros/servo/servo.py
index 295bf3e..5f82cb3 100644
--- a/server/cros/servo/servo.py
+++ b/server/cros/servo/servo.py
@@ -26,6 +26,12 @@
 # Regex to match XMLRPC errors due to a servod control not existing.
 NO_CONTROL_RE = re.compile(r'No control named (\w*\.?\w*)')
 
+
+# The minimum voltage on the charger port on servo v4 that is expected. This is
+# to query whether a charger is plugged into servo v4 and thus pd control
+# capabilities can be used.
+V4_CHG_ATTACHED_MIN_VOLTAGE_MV = 4400
+
 class ControlUnavailableError(error.TestFail):
     """Custom error class to indicate a control is unavailable on servod."""
     pass
@@ -1289,6 +1295,26 @@
             logging.debug('Not a servo v4, unable to set role to %s.', role)
 
 
+    def supports_built_in_pd_control(self):
+        """Return whether the servo type supports pd charging and control."""
+        servo_type = self.get('servo_type')
+        if 'servo_v4' not in servo_type:
+            # Only servo v4 supports this feature.
+            logging.info('%r type does not support pd control.', servo_type)
+            return False
+        # On servo v4, it still needs ot be the type-c version.
+        if not self.get('servo_v4_type') == 'type-c':
+            logging.info('PD controls require a type-c servo v4.')
+            return False
+        # Lastly, one cannot really do anything without a plugged in charger.
+        chg_port_mv = self.get('ppchg5_mv')
+        if chg_port_mv < V4_CHG_ATTACHED_MIN_VOLTAGE_MV:
+            logging.warn('It appears that no charger is plugged into servo v4. '
+                         'Charger port voltage: %dmV', chg_port_mv)
+            return False
+        logging.info('Charger port voltage: %dmV', chg_port_mv)
+        return True
+
     def set_servo_v4_dts_mode(self, state):
         """Set servo v4 dts mode to off or on.