1 /*- 2 * Copyright 2016 Michal Meloun <mmel@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #ifndef _DEV_REGULATOR_H_ 28 #define _DEV_REGULATOR_H_ 29 30 #include "opt_platform.h" 31 32 #include <sys/kobj.h> 33 #include <sys/sysctl.h> 34 #ifdef FDT 35 #include <dev/ofw/ofw_bus.h> 36 #endif 37 #include "regnode_if.h" 38 39 SYSCTL_DECL(_hw_regulator); 40 41 #define REGULATOR_FLAGS_STATIC 0x00000001 /* Static strings */ 42 #define REGULATOR_FLAGS_NOT_DISABLE 0x00000002 /* Cannot be disabled */ 43 44 #define REGULATOR_STATUS_ENABLED 0x00000001 45 #define REGULATOR_STATUS_OVERCURRENT 0x00000002 46 47 typedef struct regulator *regulator_t; 48 49 /* Standard regulator parameters. */ 50 struct regnode_std_param { 51 int min_uvolt; /* In uV */ 52 int max_uvolt; /* In uV */ 53 int min_uamp; /* In uA */ 54 int max_uamp; /* In uA */ 55 int ramp_delay; /* In uV/usec */ 56 int enable_delay; /* In usec */ 57 bool boot_on; /* Is enabled on boot */ 58 bool always_on; /* Must be enabled */ 59 int enable_active_high; 60 }; 61 62 /* Initialization parameters. */ 63 struct regnode_init_def { 64 char *name; /* Regulator name */ 65 char *parent_name; /* Name of parent regulator */ 66 struct regnode_std_param std_param; /* Standard parameters */ 67 intptr_t id; /* Regulator ID */ 68 int flags; /* Flags */ 69 #ifdef FDT 70 phandle_t ofw_node; /* OFW node of regulator */ 71 #endif 72 }; 73 74 struct regulator_range { 75 int min_uvolt; 76 int step_uvolt; 77 uint8_t min_sel; 78 uint8_t max_sel; 79 }; 80 81 #define REG_RANGE_INIT(_min_sel, _max_sel, _min_uvolt, _step_uvolt) { \ 82 .min_sel = _min_sel, \ 83 .max_sel = _max_sel, \ 84 .min_uvolt = _min_uvolt, \ 85 .step_uvolt = _step_uvolt, \ 86 } 87 88 /* 89 * Shorthands for constructing method tables. 90 */ 91 #define REGNODEMETHOD KOBJMETHOD 92 #define REGNODEMETHOD_END KOBJMETHOD_END 93 #define regnode_method_t kobj_method_t 94 #define regnode_class_t kobj_class_t 95 DECLARE_CLASS(regnode_class); 96 97 /* Providers interface. */ 98 struct regnode *regnode_create(device_t pdev, regnode_class_t regnode_class, 99 struct regnode_init_def *def); 100 struct regnode *regnode_register(struct regnode *regnode); 101 const char *regnode_get_name(struct regnode *regnode); 102 const char *regnode_get_parent_name(struct regnode *regnode); 103 struct regnode *regnode_get_parent(struct regnode *regnode); 104 int regnode_get_flags(struct regnode *regnode); 105 void *regnode_get_softc(struct regnode *regnode); 106 device_t regnode_get_device(struct regnode *regnode); 107 struct regnode_std_param *regnode_get_stdparam(struct regnode *regnode); 108 void regnode_topo_unlock(void); 109 void regnode_topo_xlock(void); 110 void regnode_topo_slock(void); 111 112 int regnode_enable(struct regnode *regnode); 113 int regnode_disable(struct regnode *regnode); 114 int regnode_stop(struct regnode *regnode, int depth); 115 int regnode_status(struct regnode *regnode, int *status); 116 int regnode_get_voltage(struct regnode *regnode, int *uvolt); 117 int regnode_set_voltage(struct regnode *regnode, int min_uvolt, int max_uvolt); 118 int regnode_set_constraint(struct regnode *regnode); 119 120 /* Standard method that aren't default */ 121 int regnode_method_check_voltage(struct regnode *regnode, int uvolt); 122 123 #ifdef FDT 124 phandle_t regnode_get_ofw_node(struct regnode *regnode); 125 #endif 126 127 /* Consumers interface. */ 128 int regulator_get_by_name(device_t cdev, const char *name, 129 regulator_t *regulator); 130 int regulator_get_by_id(device_t cdev, device_t pdev, intptr_t id, 131 regulator_t *regulator); 132 int regulator_release(regulator_t regulator); 133 const char *regulator_get_name(regulator_t regulator); 134 int regulator_enable(regulator_t reg); 135 int regulator_disable(regulator_t reg); 136 int regulator_stop(regulator_t reg); 137 int regulator_status(regulator_t reg, int *status); 138 int regulator_get_voltage(regulator_t reg, int *uvolt); 139 int regulator_set_voltage(regulator_t reg, int min_uvolt, int max_uvolt); 140 int regulator_check_voltage(regulator_t reg, int uvolt); 141 142 #ifdef FDT 143 int regulator_get_by_ofw_property(device_t dev, phandle_t node, char *name, 144 regulator_t *reg); 145 int regulator_parse_ofw_stdparam(device_t dev, phandle_t node, 146 struct regnode_init_def *def); 147 #endif 148 149 /* Utility functions */ 150 int regulator_range_volt_to_sel8(struct regulator_range *ranges, int nranges, 151 int min_uvolt, int max_uvolt, uint8_t *out_sel); 152 int regulator_range_sel8_to_volt(struct regulator_range *ranges, int nranges, 153 uint8_t sel, int *volt); 154 155 #endif /* _DEV_REGULATOR_H_ */ 156