Fix display error on screen

The previous patch was reverted and is replaced by this one.

Instead of dancing around with global flag telling if a subsys succeed
or not and write a error, let's try to initialize the subsystem each
time a display is requested and output an error occurs.

That makes the code a bit nicer and give the opportunity to fix the
problem at the system level (eg. mount debugfs) without restarting
powerdebug.

Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
diff --git a/gpio.c b/gpio.c
index 7024347..d7df0e3 100644
--- a/gpio.c
+++ b/gpio.c
@@ -262,8 +262,76 @@
 	return ret;
 }
 
+void export_free_gpios(void)
+{
+	FILE *fgpio, *fgpio_export;
+	int i, gpio_max = 0;
+	char *line = NULL;
+	ssize_t nrread;
+	size_t len = 0;
+
+	fgpio = fopen("/sys/kernel/debug/gpio", "r");
+	if (!fgpio) {
+		printf("failed to read debugfs gpio file\n");
+		return;
+	}
+
+	fgpio_export = fopen("/sys/class/gpio/export", "w");
+	if (!fgpio_export) {
+		printf("failed to write open gpio-export file\n");
+		goto out;
+	}
+
+	/* export the gpios */
+	while ((nrread = getline(&line, &len, fgpio)) != -1) {
+		if (strstr(line, "GPIOs"))
+			sscanf(line, "%*[^-]-%d", &gpio_max);
+	}
+
+	printf("log: total gpios = %d\n", gpio_max);
+	for (i = 0 ; i <= gpio_max ; i++) {
+		char command[50] = "";
+
+		sprintf(command, "echo %d > /sys/class/gpio/export", i);
+		if (system(command) < 0)
+			printf("error: failed to export gpio-%d\n", i);
+	}
+
+	free(line);
+
+	if (fgpio)
+		fclose(fgpio);
+out:
+	if (fgpio_export)
+		fclose(fgpio_export);
+
+	return;
+}
+
+static int gpio_load_info(void)
+{
+	if (gpio_tree)
+		return 0;
+
+	export_free_gpios();
+
+	gpio_tree = tree_load(SYSFS_GPIO, gpio_filter_cb, false);
+	if (!gpio_tree)
+		return -1;
+
+	if (fill_gpio_tree())
+		return -1;
+
+	return 0;
+}
+
 static int gpio_display(bool refresh)
 {
+	if (gpio_load_info()) {
+                display_print_error(GPIO, 0, "Failed to read gpio info");
+                return 0; /* we don't want this to be a critical error */
+	}
+
 	if (refresh && read_gpio_info(gpio_tree))
 		return -1;
 
@@ -320,52 +388,6 @@
 	.change = gpio_change,
 };
 
-void export_free_gpios(void)
-{
-	FILE *fgpio, *fgpio_export;
-	int i, gpio_max = 0;
-	char *line = NULL;
-	ssize_t nrread;
-	size_t len = 0;
-
-	fgpio = fopen("/sys/kernel/debug/gpio", "r");
-	if (!fgpio) {
-		printf("failed to read debugfs gpio file\n");
-		return;
-	}
-
-	fgpio_export = fopen("/sys/class/gpio/export", "w");
-	if (!fgpio_export) {
-		printf("failed to write open gpio-export file\n");
-		goto out;
-	}
-
-	/* export the gpios */
-	while ((nrread = getline(&line, &len, fgpio)) != -1) {
-		if (strstr(line, "GPIOs"))
-			sscanf(line, "%*[^-]-%d", &gpio_max);
-	}
-
-	printf("log: total gpios = %d\n", gpio_max);
-	for (i = 0 ; i <= gpio_max ; i++) {
-		char command[50] = "";
-
-		sprintf(command, "echo %d > /sys/class/gpio/export", i);
-		if (system(command) < 0)
-			printf("error: failed to export gpio-%d\n", i);
-	}
-
-	free(line);
-
-	if (fgpio)
-		fclose(fgpio);
-out:
-	if (fgpio_export)
-		fclose(fgpio_export);
-
-	return;
-}
-
 /*
  * Initialize the gpio framework
  */
@@ -374,14 +396,5 @@
 	if (!(options->flags & GPIO_OPTION))
 		return 0;
 
-	export_free_gpios();
-
-	gpio_tree = tree_load(SYSFS_GPIO, gpio_filter_cb, false);
-	if (!gpio_tree)
-		return -1;
-
-	if (fill_gpio_tree())
-		return -1;
-
 	return display_register(GPIO, &gpio_ops);
 }