18c2ecf20Sopenharmony_ci=================================== 28c2ecf20Sopenharmony_ciRegulator Consumer Driver Interface 38c2ecf20Sopenharmony_ci=================================== 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ciThis text describes the regulator interface for consumer device drivers. 68c2ecf20Sopenharmony_ciPlease see overview.txt for a description of the terms used in this text. 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci1. Consumer Regulator Access (static & dynamic drivers) 108c2ecf20Sopenharmony_ci======================================================= 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ciA consumer driver can get access to its supply regulator by calling :: 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci regulator = regulator_get(dev, "Vcc"); 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ciThe consumer passes in its struct device pointer and power supply ID. The core 178c2ecf20Sopenharmony_cithen finds the correct regulator by consulting a machine specific lookup table. 188c2ecf20Sopenharmony_ciIf the lookup is successful then this call will return a pointer to the struct 198c2ecf20Sopenharmony_ciregulator that supplies this consumer. 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ciTo release the regulator the consumer driver should call :: 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci regulator_put(regulator); 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ciConsumers can be supplied by more than one regulator e.g. codec consumer with 268c2ecf20Sopenharmony_cianalog and digital supplies :: 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci digital = regulator_get(dev, "Vcc"); /* digital core */ 298c2ecf20Sopenharmony_ci analog = regulator_get(dev, "Avdd"); /* analog */ 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ciThe regulator access functions regulator_get() and regulator_put() will 328c2ecf20Sopenharmony_ciusually be called in your device drivers probe() and remove() respectively. 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci2. Regulator Output Enable & Disable (static & dynamic drivers) 368c2ecf20Sopenharmony_ci=============================================================== 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ciA consumer can enable its power supply by calling:: 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci int regulator_enable(regulator); 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ciNOTE: 448c2ecf20Sopenharmony_ci The supply may already be enabled before regulator_enabled() is called. 458c2ecf20Sopenharmony_ci This may happen if the consumer shares the regulator or the regulator has been 468c2ecf20Sopenharmony_ci previously enabled by bootloader or kernel board initialization code. 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ciA consumer can determine if a regulator is enabled by calling:: 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci int regulator_is_enabled(regulator); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ciThis will return > zero when the regulator is enabled. 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ciA consumer can disable its supply when no longer needed by calling:: 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci int regulator_disable(regulator); 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ciNOTE: 608c2ecf20Sopenharmony_ci This may not disable the supply if it's shared with other consumers. The 618c2ecf20Sopenharmony_ci regulator will only be disabled when the enabled reference count is zero. 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ciFinally, a regulator can be forcefully disabled in the case of an emergency:: 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci int regulator_force_disable(regulator); 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ciNOTE: 688c2ecf20Sopenharmony_ci this will immediately and forcefully shutdown the regulator output. All 698c2ecf20Sopenharmony_ci consumers will be powered off. 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci3. Regulator Voltage Control & Status (dynamic drivers) 738c2ecf20Sopenharmony_ci======================================================= 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ciSome consumer drivers need to be able to dynamically change their supply 768c2ecf20Sopenharmony_civoltage to match system operating points. e.g. CPUfreq drivers can scale 778c2ecf20Sopenharmony_civoltage along with frequency to save power, SD drivers may need to select the 788c2ecf20Sopenharmony_cicorrect card voltage, etc. 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ciConsumers can control their supply voltage by calling:: 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci int regulator_set_voltage(regulator, min_uV, max_uV); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ciWhere min_uV and max_uV are the minimum and maximum acceptable voltages in 858c2ecf20Sopenharmony_cimicrovolts. 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ciNOTE: this can be called when the regulator is enabled or disabled. If called 888c2ecf20Sopenharmony_ciwhen enabled, then the voltage changes instantly, otherwise the voltage 898c2ecf20Sopenharmony_ciconfiguration changes and the voltage is physically set when the regulator is 908c2ecf20Sopenharmony_cinext enabled. 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ciThe regulators configured voltage output can be found by calling:: 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci int regulator_get_voltage(regulator); 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ciNOTE: 978c2ecf20Sopenharmony_ci get_voltage() will return the configured output voltage whether the 988c2ecf20Sopenharmony_ci regulator is enabled or disabled and should NOT be used to determine regulator 998c2ecf20Sopenharmony_ci output state. However this can be used in conjunction with is_enabled() to 1008c2ecf20Sopenharmony_ci determine the regulator physical output voltage. 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci4. Regulator Current Limit Control & Status (dynamic drivers) 1048c2ecf20Sopenharmony_ci============================================================= 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ciSome consumer drivers need to be able to dynamically change their supply 1078c2ecf20Sopenharmony_cicurrent limit to match system operating points. e.g. LCD backlight driver can 1088c2ecf20Sopenharmony_cichange the current limit to vary the backlight brightness, USB drivers may want 1098c2ecf20Sopenharmony_cito set the limit to 500mA when supplying power. 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ciConsumers can control their supply current limit by calling:: 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci int regulator_set_current_limit(regulator, min_uA, max_uA); 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ciWhere min_uA and max_uA are the minimum and maximum acceptable current limit in 1168c2ecf20Sopenharmony_cimicroamps. 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ciNOTE: 1198c2ecf20Sopenharmony_ci this can be called when the regulator is enabled or disabled. If called 1208c2ecf20Sopenharmony_ci when enabled, then the current limit changes instantly, otherwise the current 1218c2ecf20Sopenharmony_ci limit configuration changes and the current limit is physically set when the 1228c2ecf20Sopenharmony_ci regulator is next enabled. 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ciA regulators current limit can be found by calling:: 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci int regulator_get_current_limit(regulator); 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ciNOTE: 1298c2ecf20Sopenharmony_ci get_current_limit() will return the current limit whether the regulator 1308c2ecf20Sopenharmony_ci is enabled or disabled and should not be used to determine regulator current 1318c2ecf20Sopenharmony_ci load. 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci5. Regulator Operating Mode Control & Status (dynamic drivers) 1358c2ecf20Sopenharmony_ci============================================================== 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ciSome consumers can further save system power by changing the operating mode of 1388c2ecf20Sopenharmony_citheir supply regulator to be more efficient when the consumers operating state 1398c2ecf20Sopenharmony_cichanges. e.g. consumer driver is idle and subsequently draws less current 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ciRegulator operating mode can be changed indirectly or directly. 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ciIndirect operating mode control. 1448c2ecf20Sopenharmony_ci-------------------------------- 1458c2ecf20Sopenharmony_ciConsumer drivers can request a change in their supply regulator operating mode 1468c2ecf20Sopenharmony_ciby calling:: 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci int regulator_set_load(struct regulator *regulator, int load_uA); 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ciThis will cause the core to recalculate the total load on the regulator (based 1518c2ecf20Sopenharmony_cion all its consumers) and change operating mode (if necessary and permitted) 1528c2ecf20Sopenharmony_cito best match the current operating load. 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ciThe load_uA value can be determined from the consumer's datasheet. e.g. most 1558c2ecf20Sopenharmony_cidatasheets have tables showing the maximum current consumed in certain 1568c2ecf20Sopenharmony_cisituations. 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ciMost consumers will use indirect operating mode control since they have no 1598c2ecf20Sopenharmony_ciknowledge of the regulator or whether the regulator is shared with other 1608c2ecf20Sopenharmony_ciconsumers. 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ciDirect operating mode control. 1638c2ecf20Sopenharmony_ci------------------------------ 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ciBespoke or tightly coupled drivers may want to directly control regulator 1668c2ecf20Sopenharmony_cioperating mode depending on their operating point. This can be achieved by 1678c2ecf20Sopenharmony_cicalling:: 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci int regulator_set_mode(struct regulator *regulator, unsigned int mode); 1708c2ecf20Sopenharmony_ci unsigned int regulator_get_mode(struct regulator *regulator); 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ciDirect mode will only be used by consumers that *know* about the regulator and 1738c2ecf20Sopenharmony_ciare not sharing the regulator with other consumers. 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci6. Regulator Events 1778c2ecf20Sopenharmony_ci=================== 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ciRegulators can notify consumers of external events. Events could be received by 1808c2ecf20Sopenharmony_ciconsumers under regulator stress or failure conditions. 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ciConsumers can register interest in regulator events by calling:: 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci int regulator_register_notifier(struct regulator *regulator, 1858c2ecf20Sopenharmony_ci struct notifier_block *nb); 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ciConsumers can unregister interest by calling:: 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci int regulator_unregister_notifier(struct regulator *regulator, 1908c2ecf20Sopenharmony_ci struct notifier_block *nb); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ciRegulators use the kernel notifier framework to send event to their interested 1938c2ecf20Sopenharmony_ciconsumers. 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci7. Regulator Direct Register Access 1968c2ecf20Sopenharmony_ci=================================== 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ciSome kinds of power management hardware or firmware are designed such that 1998c2ecf20Sopenharmony_cithey need to do low-level hardware access to regulators, with no involvement 2008c2ecf20Sopenharmony_cifrom the kernel. Examples of such devices are: 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci- clocksource with a voltage-controlled oscillator and control logic to change 2038c2ecf20Sopenharmony_ci the supply voltage over I2C to achieve a desired output clock rate 2048c2ecf20Sopenharmony_ci- thermal management firmware that can issue an arbitrary I2C transaction to 2058c2ecf20Sopenharmony_ci perform system poweroff during overtemperature conditions 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ciTo set up such a device/firmware, various parameters like I2C address of the 2088c2ecf20Sopenharmony_ciregulator, addresses of various regulator registers etc. need to be configured 2098c2ecf20Sopenharmony_cito it. The regulator framework provides the following helpers for querying 2108c2ecf20Sopenharmony_cithese details. 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ciBus-specific details, like I2C addresses or transfer rates are handled by the 2138c2ecf20Sopenharmony_ciregmap framework. To get the regulator's regmap (if supported), use:: 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci struct regmap *regulator_get_regmap(struct regulator *regulator); 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ciTo obtain the hardware register offset and bitmask for the regulator's voltage 2188c2ecf20Sopenharmony_ciselector register, use:: 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci int regulator_get_hardware_vsel_register(struct regulator *regulator, 2218c2ecf20Sopenharmony_ci unsigned *vsel_reg, 2228c2ecf20Sopenharmony_ci unsigned *vsel_mask); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ciTo convert a regulator framework voltage selector code (used by 2258c2ecf20Sopenharmony_ciregulator_list_voltage) to a hardware-specific voltage selector that can be 2268c2ecf20Sopenharmony_cidirectly written to the voltage selector register, use:: 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci int regulator_list_hardware_vsel(struct regulator *regulator, 2298c2ecf20Sopenharmony_ci unsigned selector); 230