gpio: add function to export gpios

Many platforms don't have gpio signals available to userspace
in gpio class. This function adds support to export gpios.

Signed-off-by: Sanjay Singh Rawat <sanjay.rawat@linaro.org>
diff --git a/gpio.c b/gpio.c
index 7cc16f7..4006a5a 100644
--- a/gpio.c
+++ b/gpio.c
@@ -314,11 +314,49 @@
 	.change = gpio_change,
 };
 
+void export_gpios(void)
+{
+	FILE *fgpio, *fgpio_export;
+	int ret = 0, gpio[256], num = 0;
+	char *line;
+	ssize_t read, len;
+
+	fgpio = fopen("/sys/kernel/debug/gpio", "r");
+	if (!fgpio) {
+		printf("failed to read debugfs gpio file\n");
+		ret = -1;
+		goto out;
+	}
+
+	fgpio_export = fopen("/sys/class/gpio/export", "w");
+	if (!fgpio_export) {
+		printf("failed to write open gpio-export file\n");
+		ret = -1;
+		goto out;
+	}
+
+	/* export the gpios */
+	while (read = getline(&line, &len, fgpio) != -1) {
+		char *str;
+
+		if (strstr(line, "gpio-")) {
+			str = strtok(line, " ");
+			sscanf(str, "gpio-%d", &gpio[num]);
+			fprintf(fgpio_export, "%d", gpio[num]);
+			num++;
+		}
+	}
+out:
+	return;
+}
+
 /*
  * Initialize the gpio framework
  */
 int gpio_init(void)
 {
+	export_gpios();
+
 	gpio_tree = tree_load(SYSFS_GPIO, gpio_filter_cb, false);
 	if (!gpio_tree)
 		return -1;