1*4882a593Smuzhiyun=================================== 2*4882a593SmuzhiyunRegulator Consumer Driver Interface 3*4882a593Smuzhiyun=================================== 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunThis text describes the regulator interface for consumer device drivers. 6*4882a593SmuzhiyunPlease see overview.txt for a description of the terms used in this text. 7*4882a593Smuzhiyun 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun1. Consumer Regulator Access (static & dynamic drivers) 10*4882a593Smuzhiyun======================================================= 11*4882a593Smuzhiyun 12*4882a593SmuzhiyunA consumer driver can get access to its supply regulator by calling :: 13*4882a593Smuzhiyun 14*4882a593Smuzhiyun regulator = regulator_get(dev, "Vcc"); 15*4882a593Smuzhiyun 16*4882a593SmuzhiyunThe consumer passes in its struct device pointer and power supply ID. The core 17*4882a593Smuzhiyunthen finds the correct regulator by consulting a machine specific lookup table. 18*4882a593SmuzhiyunIf the lookup is successful then this call will return a pointer to the struct 19*4882a593Smuzhiyunregulator that supplies this consumer. 20*4882a593Smuzhiyun 21*4882a593SmuzhiyunTo release the regulator the consumer driver should call :: 22*4882a593Smuzhiyun 23*4882a593Smuzhiyun regulator_put(regulator); 24*4882a593Smuzhiyun 25*4882a593SmuzhiyunConsumers can be supplied by more than one regulator e.g. codec consumer with 26*4882a593Smuzhiyunanalog and digital supplies :: 27*4882a593Smuzhiyun 28*4882a593Smuzhiyun digital = regulator_get(dev, "Vcc"); /* digital core */ 29*4882a593Smuzhiyun analog = regulator_get(dev, "Avdd"); /* analog */ 30*4882a593Smuzhiyun 31*4882a593SmuzhiyunThe regulator access functions regulator_get() and regulator_put() will 32*4882a593Smuzhiyunusually be called in your device drivers probe() and remove() respectively. 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun 35*4882a593Smuzhiyun2. Regulator Output Enable & Disable (static & dynamic drivers) 36*4882a593Smuzhiyun=============================================================== 37*4882a593Smuzhiyun 38*4882a593Smuzhiyun 39*4882a593SmuzhiyunA consumer can enable its power supply by calling:: 40*4882a593Smuzhiyun 41*4882a593Smuzhiyun int regulator_enable(regulator); 42*4882a593Smuzhiyun 43*4882a593SmuzhiyunNOTE: 44*4882a593Smuzhiyun The supply may already be enabled before regulator_enabled() is called. 45*4882a593Smuzhiyun This may happen if the consumer shares the regulator or the regulator has been 46*4882a593Smuzhiyun previously enabled by bootloader or kernel board initialization code. 47*4882a593Smuzhiyun 48*4882a593SmuzhiyunA consumer can determine if a regulator is enabled by calling:: 49*4882a593Smuzhiyun 50*4882a593Smuzhiyun int regulator_is_enabled(regulator); 51*4882a593Smuzhiyun 52*4882a593SmuzhiyunThis will return > zero when the regulator is enabled. 53*4882a593Smuzhiyun 54*4882a593Smuzhiyun 55*4882a593SmuzhiyunA consumer can disable its supply when no longer needed by calling:: 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun int regulator_disable(regulator); 58*4882a593Smuzhiyun 59*4882a593SmuzhiyunNOTE: 60*4882a593Smuzhiyun This may not disable the supply if it's shared with other consumers. The 61*4882a593Smuzhiyun regulator will only be disabled when the enabled reference count is zero. 62*4882a593Smuzhiyun 63*4882a593SmuzhiyunFinally, a regulator can be forcefully disabled in the case of an emergency:: 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun int regulator_force_disable(regulator); 66*4882a593Smuzhiyun 67*4882a593SmuzhiyunNOTE: 68*4882a593Smuzhiyun this will immediately and forcefully shutdown the regulator output. All 69*4882a593Smuzhiyun consumers will be powered off. 70*4882a593Smuzhiyun 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun3. Regulator Voltage Control & Status (dynamic drivers) 73*4882a593Smuzhiyun======================================================= 74*4882a593Smuzhiyun 75*4882a593SmuzhiyunSome consumer drivers need to be able to dynamically change their supply 76*4882a593Smuzhiyunvoltage to match system operating points. e.g. CPUfreq drivers can scale 77*4882a593Smuzhiyunvoltage along with frequency to save power, SD drivers may need to select the 78*4882a593Smuzhiyuncorrect card voltage, etc. 79*4882a593Smuzhiyun 80*4882a593SmuzhiyunConsumers can control their supply voltage by calling:: 81*4882a593Smuzhiyun 82*4882a593Smuzhiyun int regulator_set_voltage(regulator, min_uV, max_uV); 83*4882a593Smuzhiyun 84*4882a593SmuzhiyunWhere min_uV and max_uV are the minimum and maximum acceptable voltages in 85*4882a593Smuzhiyunmicrovolts. 86*4882a593Smuzhiyun 87*4882a593SmuzhiyunNOTE: this can be called when the regulator is enabled or disabled. If called 88*4882a593Smuzhiyunwhen enabled, then the voltage changes instantly, otherwise the voltage 89*4882a593Smuzhiyunconfiguration changes and the voltage is physically set when the regulator is 90*4882a593Smuzhiyunnext enabled. 91*4882a593Smuzhiyun 92*4882a593SmuzhiyunThe regulators configured voltage output can be found by calling:: 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun int regulator_get_voltage(regulator); 95*4882a593Smuzhiyun 96*4882a593SmuzhiyunNOTE: 97*4882a593Smuzhiyun get_voltage() will return the configured output voltage whether the 98*4882a593Smuzhiyun regulator is enabled or disabled and should NOT be used to determine regulator 99*4882a593Smuzhiyun output state. However this can be used in conjunction with is_enabled() to 100*4882a593Smuzhiyun determine the regulator physical output voltage. 101*4882a593Smuzhiyun 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun4. Regulator Current Limit Control & Status (dynamic drivers) 104*4882a593Smuzhiyun============================================================= 105*4882a593Smuzhiyun 106*4882a593SmuzhiyunSome consumer drivers need to be able to dynamically change their supply 107*4882a593Smuzhiyuncurrent limit to match system operating points. e.g. LCD backlight driver can 108*4882a593Smuzhiyunchange the current limit to vary the backlight brightness, USB drivers may want 109*4882a593Smuzhiyunto set the limit to 500mA when supplying power. 110*4882a593Smuzhiyun 111*4882a593SmuzhiyunConsumers can control their supply current limit by calling:: 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun int regulator_set_current_limit(regulator, min_uA, max_uA); 114*4882a593Smuzhiyun 115*4882a593SmuzhiyunWhere min_uA and max_uA are the minimum and maximum acceptable current limit in 116*4882a593Smuzhiyunmicroamps. 117*4882a593Smuzhiyun 118*4882a593SmuzhiyunNOTE: 119*4882a593Smuzhiyun this can be called when the regulator is enabled or disabled. If called 120*4882a593Smuzhiyun when enabled, then the current limit changes instantly, otherwise the current 121*4882a593Smuzhiyun limit configuration changes and the current limit is physically set when the 122*4882a593Smuzhiyun regulator is next enabled. 123*4882a593Smuzhiyun 124*4882a593SmuzhiyunA regulators current limit can be found by calling:: 125*4882a593Smuzhiyun 126*4882a593Smuzhiyun int regulator_get_current_limit(regulator); 127*4882a593Smuzhiyun 128*4882a593SmuzhiyunNOTE: 129*4882a593Smuzhiyun get_current_limit() will return the current limit whether the regulator 130*4882a593Smuzhiyun is enabled or disabled and should not be used to determine regulator current 131*4882a593Smuzhiyun load. 132*4882a593Smuzhiyun 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun5. Regulator Operating Mode Control & Status (dynamic drivers) 135*4882a593Smuzhiyun============================================================== 136*4882a593Smuzhiyun 137*4882a593SmuzhiyunSome consumers can further save system power by changing the operating mode of 138*4882a593Smuzhiyuntheir supply regulator to be more efficient when the consumers operating state 139*4882a593Smuzhiyunchanges. e.g. consumer driver is idle and subsequently draws less current 140*4882a593Smuzhiyun 141*4882a593SmuzhiyunRegulator operating mode can be changed indirectly or directly. 142*4882a593Smuzhiyun 143*4882a593SmuzhiyunIndirect operating mode control. 144*4882a593Smuzhiyun-------------------------------- 145*4882a593SmuzhiyunConsumer drivers can request a change in their supply regulator operating mode 146*4882a593Smuzhiyunby calling:: 147*4882a593Smuzhiyun 148*4882a593Smuzhiyun int regulator_set_load(struct regulator *regulator, int load_uA); 149*4882a593Smuzhiyun 150*4882a593SmuzhiyunThis will cause the core to recalculate the total load on the regulator (based 151*4882a593Smuzhiyunon all its consumers) and change operating mode (if necessary and permitted) 152*4882a593Smuzhiyunto best match the current operating load. 153*4882a593Smuzhiyun 154*4882a593SmuzhiyunThe load_uA value can be determined from the consumer's datasheet. e.g. most 155*4882a593Smuzhiyundatasheets have tables showing the maximum current consumed in certain 156*4882a593Smuzhiyunsituations. 157*4882a593Smuzhiyun 158*4882a593SmuzhiyunMost consumers will use indirect operating mode control since they have no 159*4882a593Smuzhiyunknowledge of the regulator or whether the regulator is shared with other 160*4882a593Smuzhiyunconsumers. 161*4882a593Smuzhiyun 162*4882a593SmuzhiyunDirect operating mode control. 163*4882a593Smuzhiyun------------------------------ 164*4882a593Smuzhiyun 165*4882a593SmuzhiyunBespoke or tightly coupled drivers may want to directly control regulator 166*4882a593Smuzhiyunoperating mode depending on their operating point. This can be achieved by 167*4882a593Smuzhiyuncalling:: 168*4882a593Smuzhiyun 169*4882a593Smuzhiyun int regulator_set_mode(struct regulator *regulator, unsigned int mode); 170*4882a593Smuzhiyun unsigned int regulator_get_mode(struct regulator *regulator); 171*4882a593Smuzhiyun 172*4882a593SmuzhiyunDirect mode will only be used by consumers that *know* about the regulator and 173*4882a593Smuzhiyunare not sharing the regulator with other consumers. 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun6. Regulator Events 177*4882a593Smuzhiyun=================== 178*4882a593Smuzhiyun 179*4882a593SmuzhiyunRegulators can notify consumers of external events. Events could be received by 180*4882a593Smuzhiyunconsumers under regulator stress or failure conditions. 181*4882a593Smuzhiyun 182*4882a593SmuzhiyunConsumers can register interest in regulator events by calling:: 183*4882a593Smuzhiyun 184*4882a593Smuzhiyun int regulator_register_notifier(struct regulator *regulator, 185*4882a593Smuzhiyun struct notifier_block *nb); 186*4882a593Smuzhiyun 187*4882a593SmuzhiyunConsumers can unregister interest by calling:: 188*4882a593Smuzhiyun 189*4882a593Smuzhiyun int regulator_unregister_notifier(struct regulator *regulator, 190*4882a593Smuzhiyun struct notifier_block *nb); 191*4882a593Smuzhiyun 192*4882a593SmuzhiyunRegulators use the kernel notifier framework to send event to their interested 193*4882a593Smuzhiyunconsumers. 194*4882a593Smuzhiyun 195*4882a593Smuzhiyun7. Regulator Direct Register Access 196*4882a593Smuzhiyun=================================== 197*4882a593Smuzhiyun 198*4882a593SmuzhiyunSome kinds of power management hardware or firmware are designed such that 199*4882a593Smuzhiyunthey need to do low-level hardware access to regulators, with no involvement 200*4882a593Smuzhiyunfrom the kernel. Examples of such devices are: 201*4882a593Smuzhiyun 202*4882a593Smuzhiyun- clocksource with a voltage-controlled oscillator and control logic to change 203*4882a593Smuzhiyun the supply voltage over I2C to achieve a desired output clock rate 204*4882a593Smuzhiyun- thermal management firmware that can issue an arbitrary I2C transaction to 205*4882a593Smuzhiyun perform system poweroff during overtemperature conditions 206*4882a593Smuzhiyun 207*4882a593SmuzhiyunTo set up such a device/firmware, various parameters like I2C address of the 208*4882a593Smuzhiyunregulator, addresses of various regulator registers etc. need to be configured 209*4882a593Smuzhiyunto it. The regulator framework provides the following helpers for querying 210*4882a593Smuzhiyunthese details. 211*4882a593Smuzhiyun 212*4882a593SmuzhiyunBus-specific details, like I2C addresses or transfer rates are handled by the 213*4882a593Smuzhiyunregmap framework. To get the regulator's regmap (if supported), use:: 214*4882a593Smuzhiyun 215*4882a593Smuzhiyun struct regmap *regulator_get_regmap(struct regulator *regulator); 216*4882a593Smuzhiyun 217*4882a593SmuzhiyunTo obtain the hardware register offset and bitmask for the regulator's voltage 218*4882a593Smuzhiyunselector register, use:: 219*4882a593Smuzhiyun 220*4882a593Smuzhiyun int regulator_get_hardware_vsel_register(struct regulator *regulator, 221*4882a593Smuzhiyun unsigned *vsel_reg, 222*4882a593Smuzhiyun unsigned *vsel_mask); 223*4882a593Smuzhiyun 224*4882a593SmuzhiyunTo convert a regulator framework voltage selector code (used by 225*4882a593Smuzhiyunregulator_list_voltage) to a hardware-specific voltage selector that can be 226*4882a593Smuzhiyundirectly written to the voltage selector register, use:: 227*4882a593Smuzhiyun 228*4882a593Smuzhiyun int regulator_list_hardware_vsel(struct regulator *regulator, 229*4882a593Smuzhiyun unsigned selector); 230