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