GPIO abstraction for power and host control

In libopenbmc_intf/power_gpio.c there are GPIO abstractions for power
related GPIOs.
Host control related GPIOs need to do the same abstraction.

The changes include:
1. Add host control related GPIOs in gpio_configs and update
system_manager.py
2. Update control_host_obj.c to use the abstracted GPIOs
3. Update GPIO_CONFIGS for all machines and add host control related
GPIOs

power_gpio will be renamed to gpio_configs in next commit;

Resolves openbmc/openbmc#814

Change-Id: I7832065d495d7d64c5df2f5b177005b97e68900a
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/libopenbmc_intf/power_gpio.c b/libopenbmc_intf/power_gpio.c
index 1e7d9cd..a3fcc36 100644
--- a/libopenbmc_intf/power_gpio.c
+++ b/libopenbmc_intf/power_gpio.c
@@ -22,13 +22,12 @@
 #include <string.h>
 #include <glib.h>
 
-gboolean read_power_gpio(GDBusConnection *connection, PowerGpio *power_gpio)
+gboolean read_gpios(GDBusConnection *connection, GpioConfigs *gpios)
 {
 	GDBusProxy *proxy;
 	GError *error = NULL;
 	GVariant *value;
-	gchar *power_good_in_name;
-	gchar *latch_out_name;
+
 	GVariantIter *power_up_outs_iter;
 	GVariantIter *reset_outs_iter;
 	GVariantIter *pci_reset_outs_iter;
@@ -39,6 +38,10 @@
 	gboolean reset_out_polarity;
 	gboolean pci_reset_out_polarity;
 	gboolean pci_reset_out_hold;
+
+	GVariantIter *optionals_iter;
+	gchar *optional_name;
+	gboolean optional_polarity;
 	int i;
 
 	proxy = g_dbus_proxy_new_sync(connection,
@@ -56,66 +59,74 @@
 	}
 
 	value = g_dbus_proxy_call_sync(proxy,
-			"getPowerConfiguration",
+			"getGpioConfiguration",
 			NULL,
 			G_DBUS_CALL_FLAGS_NONE,
 			-1,
 			NULL,
 			&error);
 	if(error != NULL) {
-		fprintf(stderr, "Power GPIO: call to getPowerConfiguration failed: %s\n",
+		fprintf(stderr, "Power GPIO: call to getGpioConfiguration failed: %s\n",
 				error->message);
 		g_error_free(error);
 		return FALSE;
 	}
 
 	g_assert(value != NULL);
-	memset(power_gpio, 0, sizeof(*power_gpio));
-	g_variant_get(value, "(&s&sa(sb)a(sb)a(sbb))", &power_good_in_name, &latch_out_name,
-			&power_up_outs_iter, &reset_outs_iter, &pci_reset_outs_iter);
+	memset(gpios, 0, sizeof(*gpios));
+	g_variant_get(
+		value, "(&s&sa(sb)a(sb)a(sbb)&s&s&s&sa(sb))",
+		&gpios->power_gpio.power_good_in.name, &gpios->power_gpio.latch_out.name,
+		&power_up_outs_iter, &reset_outs_iter, &pci_reset_outs_iter,
+		&gpios->hostctl_gpio.fsi_data.name, &gpios->hostctl_gpio.fsi_clk.name,
+		&gpios->hostctl_gpio.fsi_enable.name, &gpios->hostctl_gpio.cronus_sel.name,
+		&optionals_iter);
 
-	g_print("Power GPIO latch output %s\n", latch_out_name);
-	if(*latch_out_name != '\0') {  /* latch is optional */
-		power_gpio->latch_out.name = strdup(latch_out_name);
+	g_print("Power GPIO latch output: %s\n", gpios->power_gpio.latch_out.name);
+	if(*gpios->power_gpio.latch_out.name != '\0') {  /* latch is optional */
+		gpios->power_gpio.latch_out.name = strdup(gpios->power_gpio.latch_out.name);
 	}
-	g_print("Power GPIO power good input %s\n", power_good_in_name);
-	power_gpio->power_good_in.name = g_strdup(power_good_in_name);
-	power_gpio->num_power_up_outs = g_variant_iter_n_children(
+	else {
+		gpios->power_gpio.latch_out.name = NULL;
+	}
+	g_print("Power GPIO power good input: %s\n", gpios->power_gpio.power_good_in.name);
+	gpios->power_gpio.power_good_in.name = g_strdup(gpios->power_gpio.power_good_in.name);
+	gpios->power_gpio.num_power_up_outs = g_variant_iter_n_children(
 			power_up_outs_iter);
-	g_print("Power GPIO %d power_up outputs\n",
-			power_gpio->num_power_up_outs);
-	power_gpio->power_up_outs = g_malloc0_n(power_gpio->num_power_up_outs,
+	g_print("Power GPIO %zu power_up outputs\n",
+			gpios->power_gpio.num_power_up_outs);
+	gpios->power_gpio.power_up_outs = g_malloc0_n(gpios->power_gpio.num_power_up_outs,
 			sizeof(GPIO));
-	power_gpio->power_up_pols = g_malloc0_n(power_gpio->num_power_up_outs,
+	gpios->power_gpio.power_up_pols = g_malloc0_n(gpios->power_gpio.num_power_up_outs,
 			sizeof(gboolean));
 	for(i = 0; g_variant_iter_next(power_up_outs_iter, "(&sb)",
 				&power_up_out_name, &power_up_polarity);
 			i++) {
 		g_print("Power GPIO power_up[%d] = %s active %s\n", i,
 				power_up_out_name, power_up_polarity ? "HIGH" : "LOW");
-		power_gpio->power_up_outs[i].name = g_strdup(power_up_out_name);
-		power_gpio->power_up_pols[i] = power_up_polarity;
+		gpios->power_gpio.power_up_outs[i].name = g_strdup(power_up_out_name);
+		gpios->power_gpio.power_up_pols[i] = power_up_polarity;
 	}
-	power_gpio->num_reset_outs = g_variant_iter_n_children(reset_outs_iter);
-	g_print("Power GPIO %d reset outputs\n", power_gpio->num_reset_outs);
-	power_gpio->reset_outs = g_malloc0_n(power_gpio->num_reset_outs, sizeof(GPIO));
-	power_gpio->reset_pols = g_malloc0_n(power_gpio->num_reset_outs,
+	gpios->power_gpio.num_reset_outs = g_variant_iter_n_children(reset_outs_iter);
+	g_print("Power GPIO %zu reset outputs\n", gpios->power_gpio.num_reset_outs);
+	gpios->power_gpio.reset_outs = g_malloc0_n(gpios->power_gpio.num_reset_outs, sizeof(GPIO));
+	gpios->power_gpio.reset_pols = g_malloc0_n(gpios->power_gpio.num_reset_outs,
 			sizeof(gboolean));
 	for(i = 0; g_variant_iter_next(reset_outs_iter, "(&sb)", &reset_out_name,
 				&reset_out_polarity); i++) {
 		g_print("Power GPIO reset[%d] = %s active %s\n", i, reset_out_name,
 				reset_out_polarity ? "HIGH" : "LOW");
-		power_gpio->reset_outs[i].name = g_strdup(reset_out_name);
-		power_gpio->reset_pols[i] = reset_out_polarity;
+		gpios->power_gpio.reset_outs[i].name = g_strdup(reset_out_name);
+		gpios->power_gpio.reset_pols[i] = reset_out_polarity;
 	}
 
-	power_gpio->num_pci_reset_outs = g_variant_iter_n_children(pci_reset_outs_iter);
-	g_print("Power GPIO %d pci reset outputs\n", power_gpio->num_pci_reset_outs);
-	power_gpio->pci_reset_outs = g_malloc0_n(power_gpio->num_pci_reset_outs,
+	gpios->power_gpio.num_pci_reset_outs = g_variant_iter_n_children(pci_reset_outs_iter);
+	g_print("Power GPIO %d pci reset outputs\n", gpios->power_gpio.num_pci_reset_outs);
+	gpios->power_gpio.pci_reset_outs = g_malloc0_n(gpios->power_gpio.num_pci_reset_outs,
 			sizeof(GPIO));
-	power_gpio->pci_reset_pols = g_malloc0_n(power_gpio->num_pci_reset_outs,
+	gpios->power_gpio.pci_reset_pols = g_malloc0_n(gpios->power_gpio.num_pci_reset_outs,
 			sizeof(gboolean));
-	power_gpio->pci_reset_holds = g_malloc0_n(power_gpio->num_pci_reset_outs,
+	gpios->power_gpio.pci_reset_holds = g_malloc0_n(gpios->power_gpio.num_pci_reset_outs,
 			sizeof(gboolean));
 	for(i = 0; g_variant_iter_next(pci_reset_outs_iter, "(&sbb)", &pci_reset_out_name,
 				&pci_reset_out_polarity, &pci_reset_out_hold); i++) {
@@ -123,37 +134,69 @@
 				pci_reset_out_name,
 				pci_reset_out_polarity ? "HIGH" : "LOW",
 				pci_reset_out_hold ? "Yes" : "No");
-		power_gpio->pci_reset_outs[i].name = g_strdup(pci_reset_out_name);
-		power_gpio->pci_reset_pols[i] = pci_reset_out_polarity;
-		power_gpio->pci_reset_holds[i] = pci_reset_out_hold;
+		gpios->power_gpio.pci_reset_outs[i].name = g_strdup(pci_reset_out_name);
+		gpios->power_gpio.pci_reset_pols[i] = pci_reset_out_polarity;
+		gpios->power_gpio.pci_reset_holds[i] = pci_reset_out_hold;
+	}
+
+
+	g_print("FSI DATA GPIO: %s\n", gpios->hostctl_gpio.fsi_data.name);
+	gpios->hostctl_gpio.fsi_data.name = strdup(gpios->hostctl_gpio.fsi_data.name);
+
+	g_print("FSI CLK GPIO: %s\n", gpios->hostctl_gpio.fsi_clk.name);
+	gpios->hostctl_gpio.fsi_clk.name = strdup(gpios->hostctl_gpio.fsi_clk.name);
+
+	g_print("FSI ENABLE GPIO: %s\n", gpios->hostctl_gpio.fsi_enable.name);
+	gpios->hostctl_gpio.fsi_enable.name = strdup(gpios->hostctl_gpio.fsi_enable.name);
+
+	g_print("CRONUS SEL GPIO: %s\n", gpios->hostctl_gpio.cronus_sel.name);
+	gpios->hostctl_gpio.cronus_sel.name = strdup(gpios->hostctl_gpio.cronus_sel.name);
+
+	gpios->hostctl_gpio.num_optionals = g_variant_iter_n_children(optionals_iter);
+	g_print("Hostctl GPIO optionals: %zu\n", gpios->hostctl_gpio.num_optionals);
+	gpios->hostctl_gpio.optionals = g_malloc0_n(gpios->hostctl_gpio.num_optionals, sizeof(GPIO));
+	gpios->hostctl_gpio.optional_pols = g_malloc0_n(gpios->hostctl_gpio.num_optionals, sizeof(gboolean));
+	for (i = 0; g_variant_iter_next(optionals_iter, "(&sb)", &optional_name, &optional_polarity); ++i) {
+		g_print("Hostctl optional GPIO[%d] = %s active %s\n", i, optional_name, optional_polarity ? "HIGH" : "LOW");
+		gpios->hostctl_gpio.optionals[i].name = g_strdup(optional_name);
+		gpios->hostctl_gpio.optional_pols[i] = optional_polarity;
 	}
 
 	g_variant_iter_free(power_up_outs_iter);
 	g_variant_iter_free(reset_outs_iter);
 	g_variant_iter_free(pci_reset_outs_iter);
+	g_variant_iter_free(optionals_iter);
 	g_variant_unref(value);
 
 	return TRUE;
 }
 
-void free_power_gpio(PowerGpio *power_gpio) {
+void free_gpios(GpioConfigs *gpios) {
 	int i;
-	g_free(power_gpio->latch_out.name);
-	g_free(power_gpio->power_good_in.name);
-	for(i = 0; i < power_gpio->num_power_up_outs; i++) {
-		g_free(power_gpio->power_up_outs[i].name);
+	g_free(gpios->power_gpio.latch_out.name);
+	g_free(gpios->power_gpio.power_good_in.name);
+	for(i = 0; i < gpios->power_gpio.num_power_up_outs; i++) {
+		g_free(gpios->power_gpio.power_up_outs[i].name);
 	}
-	g_free(power_gpio->power_up_outs);
-	g_free(power_gpio->power_up_pols);
-	for(i = 0; i < power_gpio->num_reset_outs; i++) {
-		g_free(power_gpio->reset_outs[i].name);
+	g_free(gpios->power_gpio.power_up_outs);
+	g_free(gpios->power_gpio.power_up_pols);
+	for(i = 0; i < gpios->power_gpio.num_reset_outs; i++) {
+		g_free(gpios->power_gpio.reset_outs[i].name);
 	}
-	g_free(power_gpio->reset_outs);
-	g_free(power_gpio->reset_pols);
-	for(i = 0; i < power_gpio->num_pci_reset_outs; i++) {
-		g_free(power_gpio->pci_reset_outs[i].name);
+	g_free(gpios->power_gpio.reset_outs);
+	g_free(gpios->power_gpio.reset_pols);
+	for(i = 0; i < gpios->power_gpio.num_pci_reset_outs; i++) {
+		g_free(gpios->power_gpio.pci_reset_outs[i].name);
 	}
-	g_free(power_gpio->pci_reset_outs);
-	g_free(power_gpio->pci_reset_pols);
-	g_free(power_gpio->pci_reset_holds);
+	g_free(gpios->power_gpio.pci_reset_outs);
+	g_free(gpios->power_gpio.pci_reset_pols);
+	g_free(gpios->power_gpio.pci_reset_holds);
+
+	g_free(gpios->hostctl_gpio.fsi_data.name);
+	g_free(gpios->hostctl_gpio.fsi_clk.name);
+	g_free(gpios->hostctl_gpio.fsi_enable.name);
+	g_free(gpios->hostctl_gpio.cronus_sel.name);
+	for (i = 0; i < gpios->hostctl_gpio.num_optionals; ++i) {
+		g_free(gpios->hostctl_gpio.optionals[i].name);
+	}
 }
diff --git a/libopenbmc_intf/power_gpio.h b/libopenbmc_intf/power_gpio.h
index e3714d0..ab01fe5 100644
--- a/libopenbmc_intf/power_gpio.h
+++ b/libopenbmc_intf/power_gpio.h
@@ -15,8 +15,8 @@
  * limitations under the License.
  */
 
-#ifndef __POWER_GPIO_H__
-#define __POWER_GPIO_H__
+#ifndef __GPIO_CONFIGS_H__
+#define __GPIO_CONFIGS_H__
 
 #include <stddef.h>
 #include <glib.h>
@@ -44,9 +44,24 @@
 	gboolean *pci_reset_holds;
 } PowerGpio;
 
-/* Read system configuration for power GPIOs. */
-gboolean read_power_gpio(GDBusConnection *connection, PowerGpio *power_gpio);
+typedef struct HostctlGpio {
+	GPIO fsi_data;
+	GPIO fsi_clk;
+	GPIO fsi_enable;
+	GPIO cronus_sel;
+	size_t num_optionals;
+	GPIO* optionals;
+	gboolean* optional_pols;
+} HostctlGpio;
+
+typedef struct GpioConfigs {
+	PowerGpio power_gpio;
+	HostctlGpio hostctl_gpio;
+} GpioConfigs;
+
+/* Read system configuration for GPIOs. */
+gboolean read_gpios(GDBusConnection *connection, GpioConfigs *gpios);
 /* Frees internal buffers. Does not free parameter. Does not close GPIOs. */
-void free_power_gpio(PowerGpio *power_gpio);
+void free_gpios(GpioConfigs *gpios);
 
 #endif