The diff below adds a simple "regulator" API, Regulators are devices
that apply voltage and/or current to subsystems to power them on.
Examples are applying voltage to an SD card, powering a USB bus,
turning on an Ethernet PHY, scaling the CPU voltage.  Regulators come
in many flavours.  The simples ones are simple gpio pins on which you
drive a voltage.  More complex ones are typically special power
control chips.

This diff only adds support for the "regulator-fixed" type.  This is a
simple gpio-based regulator that can only apply a fixed voltage.  It
doesn't need a driver of its own as it interacts with the hardware
through the gpio and pinctrl APIs.

In the future, when we need to add support for more complex types that
need their own driver, those drivers will register themselves in a
similar way to gpio controllers and pinctrl devices.  Ten we'll also
have to implement functions to set specific voltages and such.

ok?


Index: dev/ofw/ofw_regulator.c
===================================================================
RCS file: dev/ofw/ofw_regulator.c
diff -N dev/ofw/ofw_regulator.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ dev/ofw/ofw_regulator.c     12 Aug 2016 20:50:19 -0000
@@ -0,0 +1,75 @@
+/*     $OpenBSD$       */
+/*
+ * Copyright (c) 2016 Mark Kettenis
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/systm.h>
+
+#include <dev/ofw/openfirm.h>
+#include <dev/ofw/ofw_gpio.h>
+#include <dev/ofw/ofw_pinctrl.h>
+#include <dev/ofw/ofw_regulator.h>
+
+int
+regulator_set(uint32_t phandle, int enable)
+{
+       uint32_t gpio[3];
+       uint32_t startup_delay;
+       int active;
+       int node;
+
+       node = OF_getnodebyphandle(phandle);
+       if (node == 0)
+               return -1;
+
+       if (!OF_is_compatible(node, "regulator-fixed"))
+               return -1;
+
+       pinctrl_byname(node, "default");
+
+       if (OF_getproplen(node, "enable-active-high") == 0)
+               active = 1;
+       else
+               active = 0;
+
+       if (OF_getpropintarray(node, "gpio", gpio,
+           sizeof(gpio)) != sizeof(gpio))
+               return -1;
+
+       gpio_controller_config_pin(gpio, GPIO_CONFIG_OUTPUT);
+       if (enable)
+               gpio_controller_set_pin(gpio, active);
+       else
+               gpio_controller_set_pin(gpio, !active);
+
+       startup_delay = OF_getpropint(node, "startup-delay-us", 0);
+       if (enable && startup_delay > 0)
+               delay(startup_delay);
+
+       return 0;
+}
+
+int
+regulator_enable(uint32_t phandle)
+{
+       return regulator_set(phandle, 1);
+}
+
+int
+regulator_disable(uint32_t phandle)
+{
+       return regulator_set(phandle, 0);
+}
Index: dev/ofw/ofw_regulator.h
===================================================================
RCS file: dev/ofw/ofw_regulator.h
diff -N dev/ofw/ofw_regulator.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ dev/ofw/ofw_regulator.h     12 Aug 2016 20:50:19 -0000
@@ -0,0 +1,24 @@
+/*     $OpenBSD$       */
+/*
+ * Copyright (c) 2016 Mark Kettenis
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _DEV_OFW_REGULATOR_H_
+#define _DEV_OFW_REGULATOR_H_
+
+int    regulator_enable(uint32_t);
+int    regulator_disable(uint32_t);
+
+#endif /* _DEV_OFW_GPIO_H_ */

Reply via email to