162306a36Sopenharmony_ci===================================
262306a36Sopenharmony_ciRegulator Consumer Driver Interface
362306a36Sopenharmony_ci===================================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ciThis text describes the regulator interface for consumer device drivers.
662306a36Sopenharmony_ciPlease see overview.txt for a description of the terms used in this text.
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci1. Consumer Regulator Access (static & dynamic drivers)
1062306a36Sopenharmony_ci=======================================================
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ciA consumer driver can get access to its supply regulator by calling ::
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci	regulator = regulator_get(dev, "Vcc");
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ciThe consumer passes in its struct device pointer and power supply ID. The core
1762306a36Sopenharmony_cithen finds the correct regulator by consulting a machine specific lookup table.
1862306a36Sopenharmony_ciIf the lookup is successful then this call will return a pointer to the struct
1962306a36Sopenharmony_ciregulator that supplies this consumer.
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ciTo release the regulator the consumer driver should call ::
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci	regulator_put(regulator);
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciConsumers can be supplied by more than one regulator e.g. codec consumer with
2662306a36Sopenharmony_cianalog and digital supplies ::
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci	digital = regulator_get(dev, "Vcc");  /* digital core */
2962306a36Sopenharmony_ci	analog = regulator_get(dev, "Avdd");  /* analog */
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ciThe regulator access functions regulator_get() and regulator_put() will
3262306a36Sopenharmony_ciusually be called in your device drivers probe() and remove() respectively.
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci2. Regulator Output Enable & Disable (static & dynamic drivers)
3662306a36Sopenharmony_ci===============================================================
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ciA consumer can enable its power supply by calling::
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	int regulator_enable(regulator);
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ciNOTE:
4462306a36Sopenharmony_ci  The supply may already be enabled before regulator_enable() is called.
4562306a36Sopenharmony_ci  This may happen if the consumer shares the regulator or the regulator has been
4662306a36Sopenharmony_ci  previously enabled by bootloader or kernel board initialization code.
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ciA consumer can determine if a regulator is enabled by calling::
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	int regulator_is_enabled(regulator);
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciThis will return > zero when the regulator is enabled.
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ciA consumer can disable its supply when no longer needed by calling::
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	int regulator_disable(regulator);
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ciNOTE:
6062306a36Sopenharmony_ci  This may not disable the supply if it's shared with other consumers. The
6162306a36Sopenharmony_ci  regulator will only be disabled when the enabled reference count is zero.
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ciFinally, a regulator can be forcefully disabled in the case of an emergency::
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	int regulator_force_disable(regulator);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ciNOTE:
6862306a36Sopenharmony_ci  this will immediately and forcefully shutdown the regulator output. All
6962306a36Sopenharmony_ci  consumers will be powered off.
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci3. Regulator Voltage Control & Status (dynamic drivers)
7362306a36Sopenharmony_ci=======================================================
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ciSome consumer drivers need to be able to dynamically change their supply
7662306a36Sopenharmony_civoltage to match system operating points. e.g. CPUfreq drivers can scale
7762306a36Sopenharmony_civoltage along with frequency to save power, SD drivers may need to select the
7862306a36Sopenharmony_cicorrect card voltage, etc.
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ciConsumers can control their supply voltage by calling::
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	int regulator_set_voltage(regulator, min_uV, max_uV);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciWhere min_uV and max_uV are the minimum and maximum acceptable voltages in
8562306a36Sopenharmony_cimicrovolts.
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciNOTE: this can be called when the regulator is enabled or disabled. If called
8862306a36Sopenharmony_ciwhen enabled, then the voltage changes instantly, otherwise the voltage
8962306a36Sopenharmony_ciconfiguration changes and the voltage is physically set when the regulator is
9062306a36Sopenharmony_cinext enabled.
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ciThe regulators configured voltage output can be found by calling::
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	int regulator_get_voltage(regulator);
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ciNOTE:
9762306a36Sopenharmony_ci  get_voltage() will return the configured output voltage whether the
9862306a36Sopenharmony_ci  regulator is enabled or disabled and should NOT be used to determine regulator
9962306a36Sopenharmony_ci  output state. However this can be used in conjunction with is_enabled() to
10062306a36Sopenharmony_ci  determine the regulator physical output voltage.
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci4. Regulator Current Limit Control & Status (dynamic drivers)
10462306a36Sopenharmony_ci=============================================================
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ciSome consumer drivers need to be able to dynamically change their supply
10762306a36Sopenharmony_cicurrent limit to match system operating points. e.g. LCD backlight driver can
10862306a36Sopenharmony_cichange the current limit to vary the backlight brightness, USB drivers may want
10962306a36Sopenharmony_cito set the limit to 500mA when supplying power.
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ciConsumers can control their supply current limit by calling::
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	int regulator_set_current_limit(regulator, min_uA, max_uA);
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ciWhere min_uA and max_uA are the minimum and maximum acceptable current limit in
11662306a36Sopenharmony_cimicroamps.
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ciNOTE:
11962306a36Sopenharmony_ci  this can be called when the regulator is enabled or disabled. If called
12062306a36Sopenharmony_ci  when enabled, then the current limit changes instantly, otherwise the current
12162306a36Sopenharmony_ci  limit configuration changes and the current limit is physically set when the
12262306a36Sopenharmony_ci  regulator is next enabled.
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ciA regulators current limit can be found by calling::
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	int regulator_get_current_limit(regulator);
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ciNOTE:
12962306a36Sopenharmony_ci  get_current_limit() will return the current limit whether the regulator
13062306a36Sopenharmony_ci  is enabled or disabled and should not be used to determine regulator current
13162306a36Sopenharmony_ci  load.
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci5. Regulator Operating Mode Control & Status (dynamic drivers)
13562306a36Sopenharmony_ci==============================================================
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ciSome consumers can further save system power by changing the operating mode of
13862306a36Sopenharmony_citheir supply regulator to be more efficient when the consumers operating state
13962306a36Sopenharmony_cichanges. e.g. consumer driver is idle and subsequently draws less current
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ciRegulator operating mode can be changed indirectly or directly.
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ciIndirect operating mode control.
14462306a36Sopenharmony_ci--------------------------------
14562306a36Sopenharmony_ciConsumer drivers can request a change in their supply regulator operating mode
14662306a36Sopenharmony_ciby calling::
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	int regulator_set_load(struct regulator *regulator, int load_uA);
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ciThis will cause the core to recalculate the total load on the regulator (based
15162306a36Sopenharmony_cion all its consumers) and change operating mode (if necessary and permitted)
15262306a36Sopenharmony_cito best match the current operating load.
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ciThe load_uA value can be determined from the consumer's datasheet. e.g. most
15562306a36Sopenharmony_cidatasheets have tables showing the maximum current consumed in certain
15662306a36Sopenharmony_cisituations.
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ciMost consumers will use indirect operating mode control since they have no
15962306a36Sopenharmony_ciknowledge of the regulator or whether the regulator is shared with other
16062306a36Sopenharmony_ciconsumers.
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ciDirect operating mode control.
16362306a36Sopenharmony_ci------------------------------
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ciBespoke or tightly coupled drivers may want to directly control regulator
16662306a36Sopenharmony_cioperating mode depending on their operating point. This can be achieved by
16762306a36Sopenharmony_cicalling::
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	int regulator_set_mode(struct regulator *regulator, unsigned int mode);
17062306a36Sopenharmony_ci	unsigned int regulator_get_mode(struct regulator *regulator);
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ciDirect mode will only be used by consumers that *know* about the regulator and
17362306a36Sopenharmony_ciare not sharing the regulator with other consumers.
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci6. Regulator Events
17762306a36Sopenharmony_ci===================
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ciRegulators can notify consumers of external events. Events could be received by
18062306a36Sopenharmony_ciconsumers under regulator stress or failure conditions.
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ciConsumers can register interest in regulator events by calling::
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	int regulator_register_notifier(struct regulator *regulator,
18562306a36Sopenharmony_ci					struct notifier_block *nb);
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ciConsumers can unregister interest by calling::
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	int regulator_unregister_notifier(struct regulator *regulator,
19062306a36Sopenharmony_ci					  struct notifier_block *nb);
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ciRegulators use the kernel notifier framework to send event to their interested
19362306a36Sopenharmony_ciconsumers.
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci7. Regulator Direct Register Access
19662306a36Sopenharmony_ci===================================
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ciSome kinds of power management hardware or firmware are designed such that
19962306a36Sopenharmony_cithey need to do low-level hardware access to regulators, with no involvement
20062306a36Sopenharmony_cifrom the kernel. Examples of such devices are:
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci- clocksource with a voltage-controlled oscillator and control logic to change
20362306a36Sopenharmony_ci  the supply voltage over I2C to achieve a desired output clock rate
20462306a36Sopenharmony_ci- thermal management firmware that can issue an arbitrary I2C transaction to
20562306a36Sopenharmony_ci  perform system poweroff during overtemperature conditions
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ciTo set up such a device/firmware, various parameters like I2C address of the
20862306a36Sopenharmony_ciregulator, addresses of various regulator registers etc. need to be configured
20962306a36Sopenharmony_cito it. The regulator framework provides the following helpers for querying
21062306a36Sopenharmony_cithese details.
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ciBus-specific details, like I2C addresses or transfer rates are handled by the
21362306a36Sopenharmony_ciregmap framework. To get the regulator's regmap (if supported), use::
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	struct regmap *regulator_get_regmap(struct regulator *regulator);
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ciTo obtain the hardware register offset and bitmask for the regulator's voltage
21862306a36Sopenharmony_ciselector register, use::
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	int regulator_get_hardware_vsel_register(struct regulator *regulator,
22162306a36Sopenharmony_ci						 unsigned *vsel_reg,
22262306a36Sopenharmony_ci						 unsigned *vsel_mask);
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ciTo convert a regulator framework voltage selector code (used by
22562306a36Sopenharmony_ciregulator_list_voltage) to a hardware-specific voltage selector that can be
22662306a36Sopenharmony_cidirectly written to the voltage selector register, use::
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci	int regulator_list_hardware_vsel(struct regulator *regulator,
22962306a36Sopenharmony_ci					 unsigned selector);
230