Module Name:    src
Committed By:   jmcneill
Date:           Sun Aug  8 15:23:42 UTC 2021

Modified Files:
        src/sys/dev/fdt: fdt_regulator.c

Log Message:
fdt: regulator: pre-allocate regulator handle

Workaround for PR# port-evbarm/54664


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/dev/fdt/fdt_regulator.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/fdt/fdt_regulator.c
diff -u src/sys/dev/fdt/fdt_regulator.c:1.8 src/sys/dev/fdt/fdt_regulator.c:1.9
--- src/sys/dev/fdt/fdt_regulator.c:1.8	Mon May 27 23:18:33 2019
+++ src/sys/dev/fdt/fdt_regulator.c	Sun Aug  8 15:23:42 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: fdt_regulator.c,v 1.8 2019/05/27 23:18:33 jmcneill Exp $ */
+/* $NetBSD: fdt_regulator.c,v 1.9 2021/08/08 15:23:42 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2015 Jared D. McNeill <jmcne...@invisible.ca>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: fdt_regulator.c,v 1.8 2019/05/27 23:18:33 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fdt_regulator.c,v 1.9 2021/08/08 15:23:42 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -37,6 +37,9 @@ __KERNEL_RCSID(0, "$NetBSD: fdt_regulato
 #include <libfdt.h>
 #include <dev/fdt/fdtvar.h>
 
+#define	REGULATOR_TO_RC(_reg)	\
+	container_of((_reg), struct fdtbus_regulator_controller, rc_reg)
+
 struct fdtbus_regulator_controller {
 	device_t rc_dev;
 	int rc_phandle;
@@ -44,6 +47,8 @@ struct fdtbus_regulator_controller {
 
 	u_int rc_enable_ramp_delay;
 
+	struct fdtbus_regulator rc_reg;	/* handle returned by acquire() */
+
 	LIST_ENTRY(fdtbus_regulator_controller) rc_next;
 };
 
@@ -60,6 +65,7 @@ fdtbus_register_regulator_controller(dev
 	rc->rc_dev = dev;
 	rc->rc_phandle = phandle;
 	rc->rc_funcs = funcs;
+	rc->rc_reg.reg_rc = rc;
 
 	of_getprop_uint32(phandle, "regulator-enable-ramp-delay", &rc->rc_enable_ramp_delay);
 
@@ -85,7 +91,6 @@ struct fdtbus_regulator *
 fdtbus_regulator_acquire(int phandle, const char *prop)
 {
 	struct fdtbus_regulator_controller *rc;
-	struct fdtbus_regulator *reg;
 	int regulator_phandle;
 	int error;
 
@@ -105,26 +110,21 @@ fdtbus_regulator_acquire(int phandle, co
 		return NULL;
 	}
 
-	reg = kmem_alloc(sizeof(*reg), KM_SLEEP);
-	reg->reg_rc = rc;
-
-	return reg;
+	return &rc->rc_reg;
 }
 
 void
 fdtbus_regulator_release(struct fdtbus_regulator *reg)
 {
-	struct fdtbus_regulator_controller *rc = reg->reg_rc;
+	struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg);
 
 	rc->rc_funcs->release(rc->rc_dev);
-
-	kmem_free(reg, sizeof(*reg));
 }
 
 int
 fdtbus_regulator_enable(struct fdtbus_regulator *reg)
 {
-	struct fdtbus_regulator_controller *rc = reg->reg_rc;
+	struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg);
 	int error;
 
 	error = rc->rc_funcs->enable(rc->rc_dev, true);
@@ -140,7 +140,7 @@ fdtbus_regulator_enable(struct fdtbus_re
 int
 fdtbus_regulator_disable(struct fdtbus_regulator *reg)
 {
-	struct fdtbus_regulator_controller *rc = reg->reg_rc;
+	struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg);
 
 	if (of_hasprop(rc->rc_phandle, "regulator-always-on"))
 		return EIO;
@@ -152,7 +152,7 @@ int
 fdtbus_regulator_set_voltage(struct fdtbus_regulator *reg, u_int min_uvol,
     u_int max_uvol)
 {
-	struct fdtbus_regulator_controller *rc = reg->reg_rc;
+	struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg);
 
 	if (rc->rc_funcs->set_voltage == NULL)
 		return EINVAL;
@@ -163,7 +163,7 @@ fdtbus_regulator_set_voltage(struct fdtb
 int
 fdtbus_regulator_get_voltage(struct fdtbus_regulator *reg, u_int *puvol)
 {
-	struct fdtbus_regulator_controller *rc = reg->reg_rc;
+	struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg);
 
 	if (rc->rc_funcs->get_voltage == NULL)
 		return EINVAL;
@@ -175,7 +175,7 @@ int
 fdtbus_regulator_supports_voltage(struct fdtbus_regulator *reg, u_int min_uvol,
     u_int max_uvol)
 {
-	struct fdtbus_regulator_controller *rc = reg->reg_rc;
+	struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg);
 	u_int uvol;
 
 	if (rc->rc_funcs->set_voltage == NULL)

Reply via email to