Adding Polarity in GPIO config

When requesting output gpio pin, the daemon would request non-active as default value, therefore we need a polarity for output pin.

For the input/event pin, the daemon just set 0 as default value.

ref: x86-power-control https://github.com/openbmc/x86-power-control/blob/4d684117da08fd48c61655bb9afbc0510be42917/src/power_control.cpp#L2976
PiperOrigin-RevId: 825951846
Change-Id: I9256a671cb387be6d2f6ae5ee5a534fdde6e2ae3
diff --git a/tlbmc/gpio_config.proto b/tlbmc/gpio_config.proto
index 5bf2048..eabd8f5 100644
--- a/tlbmc/gpio_config.proto
+++ b/tlbmc/gpio_config.proto
@@ -19,6 +19,14 @@
   GPIO_EVENT_BOTH = 3;
 }
 
+enum GpioPolarity {
+  // For GPIO input/output, we can set the GPIO line to be active high or active
+  // low.
+  GPIO_POLARITY_UNKNOWN = 0;
+  GPIO_POLARITY_ACTIVE_HIGH = 1;
+  GPIO_POLARITY_ACTIVE_LOW = 2;
+}
+
 message GpioEventConfig {
   GpioEventType event_type = 1;
   // If true, the event callback will be triggered only once. Otherwise, the
@@ -48,6 +56,10 @@
     GpioDirection gpio_direction = 4;
     GpioEventConfig gpio_event_config = 5;
   }
+
+  // The polarity of the GPIO line. This is used to set the polarity of the GPIO
+  // line. The output pin is set to the opposite polarity in the beginning.
+  GpioPolarity gpio_polarity = 6;
 }
 
 message GpioConfigs {
diff --git a/tlbmc/hal/gpio/gpio.cc b/tlbmc/hal/gpio/gpio.cc
index dbbba02..8fbe259 100644
--- a/tlbmc/hal/gpio/gpio.cc
+++ b/tlbmc/hal/gpio/gpio.cc
@@ -113,8 +113,23 @@
                             internal::ParseGpioRequestType(config));
   request_config.request_type = request_type;
 
-  if (gpiod_line_request(line, &request_config, /*flags=*/0) < 0) {
-    return absl::InternalError("Failed to request GPIO line");
+  if (config.request_type_case() == GpioConfig::kGpioDirection &&
+      config.gpio_direction() == GPIO_DIRECTION_OUTPUT) {
+    if (config.gpio_polarity() == GPIO_POLARITY_UNKNOWN) {
+      return absl::InvalidArgumentError(absl::StrCat(
+          "Output GPIO line ", config.line_name(), " has unknown polarity"));
+    }
+    // The default value of the output GPIO line is the opposite of the
+    // polarity.
+    int default_val =
+        (config.gpio_polarity() == GPIO_POLARITY_ACTIVE_LOW) ? 1 : 0;
+    if (gpiod_line_request(line, &request_config, default_val) < 0) {
+      return absl::InternalError("Failed to request GPIO line");
+    }
+  } else {
+    if (gpiod_line_request(line, &request_config, /*default_val=*/0) < 0) {
+      return absl::InternalError("Failed to request GPIO line");
+    }
   }
 
   int fd = -1;