Home | History | Annotate | Line # | Download | only in fdt
fdt_regulator.c revision 1.2.2.3
      1  1.2.2.3  skrll /* $NetBSD: fdt_regulator.c,v 1.2.2.3 2017/08/28 17:52:02 skrll Exp $ */
      2  1.2.2.2  skrll 
      3  1.2.2.2  skrll /*-
      4  1.2.2.2  skrll  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.2.2.2  skrll  * All rights reserved.
      6  1.2.2.2  skrll  *
      7  1.2.2.2  skrll  * Redistribution and use in source and binary forms, with or without
      8  1.2.2.2  skrll  * modification, are permitted provided that the following conditions
      9  1.2.2.2  skrll  * are met:
     10  1.2.2.2  skrll  * 1. Redistributions of source code must retain the above copyright
     11  1.2.2.2  skrll  *    notice, this list of conditions and the following disclaimer.
     12  1.2.2.2  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2.2.2  skrll  *    notice, this list of conditions and the following disclaimer in the
     14  1.2.2.2  skrll  *    documentation and/or other materials provided with the distribution.
     15  1.2.2.2  skrll  *
     16  1.2.2.2  skrll  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.2.2.2  skrll  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.2.2.2  skrll  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.2.2.2  skrll  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.2.2.2  skrll  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.2.2.2  skrll  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.2.2.2  skrll  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.2.2.2  skrll  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.2.2.2  skrll  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.2.2.2  skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.2.2.2  skrll  * SUCH DAMAGE.
     27  1.2.2.2  skrll  */
     28  1.2.2.2  skrll 
     29  1.2.2.2  skrll #include <sys/cdefs.h>
     30  1.2.2.3  skrll __KERNEL_RCSID(0, "$NetBSD: fdt_regulator.c,v 1.2.2.3 2017/08/28 17:52:02 skrll Exp $");
     31  1.2.2.2  skrll 
     32  1.2.2.2  skrll #include <sys/param.h>
     33  1.2.2.2  skrll #include <sys/bus.h>
     34  1.2.2.2  skrll #include <sys/kmem.h>
     35  1.2.2.2  skrll 
     36  1.2.2.2  skrll #include <libfdt.h>
     37  1.2.2.2  skrll #include <dev/fdt/fdtvar.h>
     38  1.2.2.2  skrll 
     39  1.2.2.2  skrll struct fdtbus_regulator_controller {
     40  1.2.2.2  skrll 	device_t rc_dev;
     41  1.2.2.2  skrll 	int rc_phandle;
     42  1.2.2.2  skrll 	const struct fdtbus_regulator_controller_func *rc_funcs;
     43  1.2.2.2  skrll 
     44  1.2.2.2  skrll 	struct fdtbus_regulator_controller *rc_next;
     45  1.2.2.2  skrll };
     46  1.2.2.2  skrll 
     47  1.2.2.2  skrll static struct fdtbus_regulator_controller *fdtbus_rc = NULL;
     48  1.2.2.2  skrll 
     49  1.2.2.2  skrll int
     50  1.2.2.2  skrll fdtbus_register_regulator_controller(device_t dev, int phandle,
     51  1.2.2.2  skrll     const struct fdtbus_regulator_controller_func *funcs)
     52  1.2.2.2  skrll {
     53  1.2.2.2  skrll 	struct fdtbus_regulator_controller *rc;
     54  1.2.2.2  skrll 
     55  1.2.2.2  skrll 	rc = kmem_alloc(sizeof(*rc), KM_SLEEP);
     56  1.2.2.2  skrll 	rc->rc_dev = dev;
     57  1.2.2.2  skrll 	rc->rc_phandle = phandle;
     58  1.2.2.2  skrll 	rc->rc_funcs = funcs;
     59  1.2.2.2  skrll 
     60  1.2.2.2  skrll 	rc->rc_next = fdtbus_rc;
     61  1.2.2.2  skrll 	fdtbus_rc = rc;
     62  1.2.2.2  skrll 
     63  1.2.2.2  skrll 	return 0;
     64  1.2.2.2  skrll }
     65  1.2.2.2  skrll 
     66  1.2.2.2  skrll static struct fdtbus_regulator_controller *
     67  1.2.2.2  skrll fdtbus_get_regulator_controller(int phandle)
     68  1.2.2.2  skrll {
     69  1.2.2.2  skrll 	struct fdtbus_regulator_controller *rc;
     70  1.2.2.2  skrll 
     71  1.2.2.2  skrll 	for (rc = fdtbus_rc; rc; rc = rc->rc_next) {
     72  1.2.2.2  skrll 		if (rc->rc_phandle == phandle) {
     73  1.2.2.2  skrll 			return rc;
     74  1.2.2.2  skrll 		}
     75  1.2.2.2  skrll 	}
     76  1.2.2.2  skrll 
     77  1.2.2.2  skrll 	return NULL;
     78  1.2.2.2  skrll }
     79  1.2.2.2  skrll 
     80  1.2.2.2  skrll struct fdtbus_regulator *
     81  1.2.2.2  skrll fdtbus_regulator_acquire(int phandle, const char *prop)
     82  1.2.2.2  skrll {
     83  1.2.2.2  skrll 	struct fdtbus_regulator_controller *rc;
     84  1.2.2.2  skrll 	struct fdtbus_regulator *reg;
     85  1.2.2.2  skrll 	int regulator_phandle;
     86  1.2.2.2  skrll 	int error;
     87  1.2.2.2  skrll 
     88  1.2.2.2  skrll 	regulator_phandle = fdtbus_get_phandle(phandle, prop);
     89  1.2.2.2  skrll 	if (regulator_phandle == -1) {
     90  1.2.2.2  skrll 		return NULL;
     91  1.2.2.2  skrll 	}
     92  1.2.2.2  skrll 
     93  1.2.2.2  skrll 	rc = fdtbus_get_regulator_controller(regulator_phandle);
     94  1.2.2.2  skrll 	if (rc == NULL) {
     95  1.2.2.2  skrll 		return NULL;
     96  1.2.2.2  skrll 	}
     97  1.2.2.2  skrll 
     98  1.2.2.2  skrll 	error = rc->rc_funcs->acquire(rc->rc_dev);
     99  1.2.2.2  skrll 	if (error) {
    100  1.2.2.2  skrll 		return NULL;
    101  1.2.2.2  skrll 	}
    102  1.2.2.2  skrll 
    103  1.2.2.2  skrll 	reg = kmem_alloc(sizeof(*reg), KM_SLEEP);
    104  1.2.2.2  skrll 	reg->reg_rc = rc;
    105  1.2.2.2  skrll 
    106  1.2.2.2  skrll 	return reg;
    107  1.2.2.2  skrll }
    108  1.2.2.2  skrll 
    109  1.2.2.2  skrll void
    110  1.2.2.2  skrll fdtbus_regulator_release(struct fdtbus_regulator *reg)
    111  1.2.2.2  skrll {
    112  1.2.2.2  skrll 	struct fdtbus_regulator_controller *rc = reg->reg_rc;
    113  1.2.2.2  skrll 
    114  1.2.2.2  skrll 	rc->rc_funcs->release(rc->rc_dev);
    115  1.2.2.2  skrll 
    116  1.2.2.2  skrll 	kmem_free(reg, sizeof(*reg));
    117  1.2.2.2  skrll }
    118  1.2.2.2  skrll 
    119  1.2.2.2  skrll int
    120  1.2.2.2  skrll fdtbus_regulator_enable(struct fdtbus_regulator *reg)
    121  1.2.2.2  skrll {
    122  1.2.2.2  skrll 	struct fdtbus_regulator_controller *rc = reg->reg_rc;
    123  1.2.2.2  skrll 
    124  1.2.2.2  skrll 	return rc->rc_funcs->enable(rc->rc_dev, true);
    125  1.2.2.2  skrll }
    126  1.2.2.2  skrll 
    127  1.2.2.2  skrll int
    128  1.2.2.2  skrll fdtbus_regulator_disable(struct fdtbus_regulator *reg)
    129  1.2.2.2  skrll {
    130  1.2.2.2  skrll 	struct fdtbus_regulator_controller *rc = reg->reg_rc;
    131  1.2.2.2  skrll 
    132  1.2.2.2  skrll 	return rc->rc_funcs->enable(rc->rc_dev, false);
    133  1.2.2.2  skrll }
    134  1.2.2.3  skrll 
    135  1.2.2.3  skrll int
    136  1.2.2.3  skrll fdtbus_regulator_set_voltage(struct fdtbus_regulator *reg, u_int min_uvol,
    137  1.2.2.3  skrll     u_int max_uvol)
    138  1.2.2.3  skrll {
    139  1.2.2.3  skrll 	struct fdtbus_regulator_controller *rc = reg->reg_rc;
    140  1.2.2.3  skrll 
    141  1.2.2.3  skrll 	if (rc->rc_funcs->set_voltage == NULL)
    142  1.2.2.3  skrll 		return EINVAL;
    143  1.2.2.3  skrll 
    144  1.2.2.3  skrll 	return rc->rc_funcs->set_voltage(rc->rc_dev, min_uvol, max_uvol);
    145  1.2.2.3  skrll }
    146  1.2.2.3  skrll 
    147  1.2.2.3  skrll int
    148  1.2.2.3  skrll fdtbus_regulator_get_voltage(struct fdtbus_regulator *reg, u_int *puvol)
    149  1.2.2.3  skrll {
    150  1.2.2.3  skrll 	struct fdtbus_regulator_controller *rc = reg->reg_rc;
    151  1.2.2.3  skrll 
    152  1.2.2.3  skrll 	if (rc->rc_funcs->set_voltage == NULL)
    153  1.2.2.3  skrll 		return EINVAL;
    154  1.2.2.3  skrll 
    155  1.2.2.3  skrll 	return rc->rc_funcs->get_voltage(rc->rc_dev, puvol);
    156  1.2.2.3  skrll }
    157