xref: /netbsd-src/sys/dev/fdt/fdt_power.c (revision 233c5d85efd0721c11837e7294d260f3a2bd8f2f)
1*233c5d85Sjmcneill /* $NetBSD: fdt_power.c,v 1.2 2018/06/30 20:34:43 jmcneill Exp $ */
26ba64aa7Sjmcneill 
36ba64aa7Sjmcneill /*-
46ba64aa7Sjmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
56ba64aa7Sjmcneill  * All rights reserved.
66ba64aa7Sjmcneill  *
76ba64aa7Sjmcneill  * Redistribution and use in source and binary forms, with or without
86ba64aa7Sjmcneill  * modification, are permitted provided that the following conditions
96ba64aa7Sjmcneill  * are met:
106ba64aa7Sjmcneill  * 1. Redistributions of source code must retain the above copyright
116ba64aa7Sjmcneill  *    notice, this list of conditions and the following disclaimer.
126ba64aa7Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
136ba64aa7Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
146ba64aa7Sjmcneill  *    documentation and/or other materials provided with the distribution.
156ba64aa7Sjmcneill  *
166ba64aa7Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
176ba64aa7Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
186ba64aa7Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
196ba64aa7Sjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
206ba64aa7Sjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
216ba64aa7Sjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
226ba64aa7Sjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
236ba64aa7Sjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
246ba64aa7Sjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
256ba64aa7Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
266ba64aa7Sjmcneill  * SUCH DAMAGE.
276ba64aa7Sjmcneill  */
286ba64aa7Sjmcneill 
296ba64aa7Sjmcneill #include <sys/cdefs.h>
30*233c5d85Sjmcneill __KERNEL_RCSID(0, "$NetBSD: fdt_power.c,v 1.2 2018/06/30 20:34:43 jmcneill Exp $");
316ba64aa7Sjmcneill 
326ba64aa7Sjmcneill #include <sys/param.h>
336ba64aa7Sjmcneill #include <sys/bus.h>
346ba64aa7Sjmcneill #include <sys/kmem.h>
35*233c5d85Sjmcneill #include <sys/queue.h>
366ba64aa7Sjmcneill 
376ba64aa7Sjmcneill #include <libfdt.h>
386ba64aa7Sjmcneill #include <dev/fdt/fdtvar.h>
396ba64aa7Sjmcneill 
406ba64aa7Sjmcneill struct fdtbus_power_controller {
416ba64aa7Sjmcneill 	device_t power_dev;
426ba64aa7Sjmcneill 	int power_phandle;
436ba64aa7Sjmcneill 	const struct fdtbus_power_controller_func *power_funcs;
446ba64aa7Sjmcneill 
45*233c5d85Sjmcneill 	LIST_ENTRY(fdtbus_power_controller) power_next;
466ba64aa7Sjmcneill };
476ba64aa7Sjmcneill 
48*233c5d85Sjmcneill static LIST_HEAD(, fdtbus_power_controller) fdtbus_power_controllers =
49*233c5d85Sjmcneill     LIST_HEAD_INITIALIZER(fdtbus_power_controllers);
506ba64aa7Sjmcneill 
516ba64aa7Sjmcneill int
fdtbus_register_power_controller(device_t dev,int phandle,const struct fdtbus_power_controller_func * funcs)526ba64aa7Sjmcneill fdtbus_register_power_controller(device_t dev, int phandle,
536ba64aa7Sjmcneill     const struct fdtbus_power_controller_func *funcs)
546ba64aa7Sjmcneill {
556ba64aa7Sjmcneill 	struct fdtbus_power_controller *power;
566ba64aa7Sjmcneill 
576ba64aa7Sjmcneill 	power = kmem_alloc(sizeof(*power), KM_SLEEP);
586ba64aa7Sjmcneill 	power->power_dev = dev;
596ba64aa7Sjmcneill 	power->power_phandle = phandle;
606ba64aa7Sjmcneill 	power->power_funcs = funcs;
616ba64aa7Sjmcneill 
62*233c5d85Sjmcneill 	LIST_INSERT_HEAD(&fdtbus_power_controllers, power, power_next);
636ba64aa7Sjmcneill 
646ba64aa7Sjmcneill 	return 0;
656ba64aa7Sjmcneill }
666ba64aa7Sjmcneill 
676ba64aa7Sjmcneill void
fdtbus_power_reset(void)686ba64aa7Sjmcneill fdtbus_power_reset(void)
696ba64aa7Sjmcneill {
706ba64aa7Sjmcneill 	struct fdtbus_power_controller *power;
716ba64aa7Sjmcneill 
72*233c5d85Sjmcneill 	LIST_FOREACH(power, &fdtbus_power_controllers, power_next) {
736ba64aa7Sjmcneill 		if (power->power_funcs->reset)
746ba64aa7Sjmcneill 			power->power_funcs->reset(power->power_dev);
756ba64aa7Sjmcneill 	}
766ba64aa7Sjmcneill }
776ba64aa7Sjmcneill 
786ba64aa7Sjmcneill void
fdtbus_power_poweroff(void)796ba64aa7Sjmcneill fdtbus_power_poweroff(void)
806ba64aa7Sjmcneill {
816ba64aa7Sjmcneill 	struct fdtbus_power_controller *power;
826ba64aa7Sjmcneill 
83*233c5d85Sjmcneill 	LIST_FOREACH(power, &fdtbus_power_controllers, power_next) {
846ba64aa7Sjmcneill 		if (power->power_funcs->poweroff)
856ba64aa7Sjmcneill 			power->power_funcs->poweroff(power->power_dev);
866ba64aa7Sjmcneill 	}
876ba64aa7Sjmcneill }
88