162306a36Sopenharmony_ci======================================
262306a36Sopenharmony_ciPulse Width Modulation (PWM) interface
362306a36Sopenharmony_ci======================================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ciThis provides an overview about the Linux PWM interface
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciPWMs are commonly used for controlling LEDs, fans or vibrators in
862306a36Sopenharmony_cicell phones. PWMs with a fixed purpose have no need implementing
962306a36Sopenharmony_cithe Linux PWM API (although they could). However, PWMs are often
1062306a36Sopenharmony_cifound as discrete devices on SoCs which have no fixed purpose. It's
1162306a36Sopenharmony_ciup to the board designer to connect them to LEDs or fans. To provide
1262306a36Sopenharmony_cithis kind of flexibility the generic PWM API exists.
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ciIdentifying PWMs
1562306a36Sopenharmony_ci----------------
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ciUsers of the legacy PWM API use unique IDs to refer to PWM devices.
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ciInstead of referring to a PWM device via its unique ID, board setup code
2062306a36Sopenharmony_cishould instead register a static mapping that can be used to match PWM
2162306a36Sopenharmony_ciconsumers to providers, as given in the following example::
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci	static struct pwm_lookup board_pwm_lookup[] = {
2462306a36Sopenharmony_ci		PWM_LOOKUP("tegra-pwm", 0, "pwm-backlight", NULL,
2562306a36Sopenharmony_ci			   50000, PWM_POLARITY_NORMAL),
2662306a36Sopenharmony_ci	};
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci	static void __init board_init(void)
2962306a36Sopenharmony_ci	{
3062306a36Sopenharmony_ci		...
3162306a36Sopenharmony_ci		pwm_add_table(board_pwm_lookup, ARRAY_SIZE(board_pwm_lookup));
3262306a36Sopenharmony_ci		...
3362306a36Sopenharmony_ci	}
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ciUsing PWMs
3662306a36Sopenharmony_ci----------
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ciConsumers use the pwm_get() function and pass to it the consumer device or a
3962306a36Sopenharmony_ciconsumer name. pwm_put() is used to free the PWM device. Managed variants of
4062306a36Sopenharmony_cithe getter, devm_pwm_get() and devm_fwnode_pwm_get(), also exist.
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ciAfter being requested, a PWM has to be configured using::
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ciThis API controls both the PWM period/duty_cycle config and the
4762306a36Sopenharmony_cienable/disable state.
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ciAs a consumer, don't rely on the output's state for a disabled PWM. If it's
5062306a36Sopenharmony_cieasily possible, drivers are supposed to emit the inactive state, but some
5162306a36Sopenharmony_cidrivers cannot. If you rely on getting the inactive state, use .duty_cycle=0,
5262306a36Sopenharmony_ci.enabled=true.
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ciThere is also a usage_power setting: If set, the PWM driver is only required to
5562306a36Sopenharmony_cimaintain the power output but has more freedom regarding signal form.
5662306a36Sopenharmony_ciIf supported by the driver, the signal can be optimized, for example to improve
5762306a36Sopenharmony_ciEMI by phase shifting the individual channels of a chip.
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ciThe pwm_config(), pwm_enable() and pwm_disable() functions are just wrappers
6062306a36Sopenharmony_ciaround pwm_apply_state() and should not be used if the user wants to change
6162306a36Sopenharmony_ciseveral parameter at once. For example, if you see pwm_config() and
6262306a36Sopenharmony_cipwm_{enable,disable}() calls in the same function, this probably means you
6362306a36Sopenharmony_cishould switch to pwm_apply_state().
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ciThe PWM user API also allows one to query the PWM state that was passed to the
6662306a36Sopenharmony_cilast invocation of pwm_apply_state() using pwm_get_state(). Note this is
6762306a36Sopenharmony_cidifferent to what the driver has actually implemented if the request cannot be
6862306a36Sopenharmony_cisatisfied exactly with the hardware in use. There is currently no way for
6962306a36Sopenharmony_ciconsumers to get the actually implemented settings.
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ciIn addition to the PWM state, the PWM API also exposes PWM arguments, which
7262306a36Sopenharmony_ciare the reference PWM config one should use on this PWM.
7362306a36Sopenharmony_ciPWM arguments are usually platform-specific and allows the PWM user to only
7462306a36Sopenharmony_cicare about dutycycle relatively to the full period (like, duty = 50% of the
7562306a36Sopenharmony_ciperiod). struct pwm_args contains 2 fields (period and polarity) and should
7662306a36Sopenharmony_cibe used to set the initial PWM config (usually done in the probe function
7762306a36Sopenharmony_ciof the PWM user). PWM arguments are retrieved with pwm_get_args().
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciAll consumers should really be reconfiguring the PWM upon resume as
8062306a36Sopenharmony_ciappropriate. This is the only way to ensure that everything is resumed in
8162306a36Sopenharmony_cithe proper order.
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ciUsing PWMs with the sysfs interface
8462306a36Sopenharmony_ci-----------------------------------
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ciIf CONFIG_SYSFS is enabled in your kernel configuration a simple sysfs
8762306a36Sopenharmony_ciinterface is provided to use the PWMs from userspace. It is exposed at
8862306a36Sopenharmony_ci/sys/class/pwm/. Each probed PWM controller/chip will be exported as
8962306a36Sopenharmony_cipwmchipN, where N is the base of the PWM chip. Inside the directory you
9062306a36Sopenharmony_ciwill find:
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci  npwm
9362306a36Sopenharmony_ci    The number of PWM channels this chip supports (read-only).
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci  export
9662306a36Sopenharmony_ci    Exports a PWM channel for use with sysfs (write-only).
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci  unexport
9962306a36Sopenharmony_ci   Unexports a PWM channel from sysfs (write-only).
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ciThe PWM channels are numbered using a per-chip index from 0 to npwm-1.
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ciWhen a PWM channel is exported a pwmX directory will be created in the
10462306a36Sopenharmony_cipwmchipN directory it is associated with, where X is the number of the
10562306a36Sopenharmony_cichannel that was exported. The following properties will then be available:
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci  period
10862306a36Sopenharmony_ci    The total period of the PWM signal (read/write).
10962306a36Sopenharmony_ci    Value is in nanoseconds and is the sum of the active and inactive
11062306a36Sopenharmony_ci    time of the PWM.
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci  duty_cycle
11362306a36Sopenharmony_ci    The active time of the PWM signal (read/write).
11462306a36Sopenharmony_ci    Value is in nanoseconds and must be less than the period.
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci  polarity
11762306a36Sopenharmony_ci    Changes the polarity of the PWM signal (read/write).
11862306a36Sopenharmony_ci    Writes to this property only work if the PWM chip supports changing
11962306a36Sopenharmony_ci    the polarity. The polarity can only be changed if the PWM is not
12062306a36Sopenharmony_ci    enabled. Value is the string "normal" or "inversed".
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci  enable
12362306a36Sopenharmony_ci    Enable/disable the PWM signal (read/write).
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	- 0 - disabled
12662306a36Sopenharmony_ci	- 1 - enabled
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ciImplementing a PWM driver
12962306a36Sopenharmony_ci-------------------------
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ciCurrently there are two ways to implement pwm drivers. Traditionally
13262306a36Sopenharmony_cithere only has been the barebone API meaning that each driver has
13362306a36Sopenharmony_cito implement the pwm_*() functions itself. This means that it's impossible
13462306a36Sopenharmony_cito have multiple PWM drivers in the system. For this reason it's mandatory
13562306a36Sopenharmony_cifor new drivers to use the generic PWM framework.
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ciA new PWM controller/chip can be added using pwmchip_add() and removed
13862306a36Sopenharmony_ciagain with pwmchip_remove(). pwmchip_add() takes a filled in struct
13962306a36Sopenharmony_cipwm_chip as argument which provides a description of the PWM chip, the
14062306a36Sopenharmony_cinumber of PWM devices provided by the chip and the chip-specific
14162306a36Sopenharmony_ciimplementation of the supported PWM operations to the framework.
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ciWhen implementing polarity support in a PWM driver, make sure to respect the
14462306a36Sopenharmony_cisignal conventions in the PWM framework. By definition, normal polarity
14562306a36Sopenharmony_cicharacterizes a signal starts high for the duration of the duty cycle and
14662306a36Sopenharmony_cigoes low for the remainder of the period. Conversely, a signal with inversed
14762306a36Sopenharmony_cipolarity starts low for the duration of the duty cycle and goes high for the
14862306a36Sopenharmony_ciremainder of the period.
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ciDrivers are encouraged to implement ->apply() instead of the legacy
15162306a36Sopenharmony_ci->enable(), ->disable() and ->config() methods. Doing that should provide
15262306a36Sopenharmony_ciatomicity in the PWM config workflow, which is required when the PWM controls
15362306a36Sopenharmony_cia critical device (like a regulator).
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ciThe implementation of ->get_state() (a method used to retrieve initial PWM
15662306a36Sopenharmony_cistate) is also encouraged for the same reason: letting the PWM user know
15762306a36Sopenharmony_ciabout the current PWM state would allow him to avoid glitches.
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ciDrivers should not implement any power management. In other words,
16062306a36Sopenharmony_ciconsumers should implement it as described in the "Using PWMs" section.
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ciLocking
16362306a36Sopenharmony_ci-------
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ciThe PWM core list manipulations are protected by a mutex, so pwm_get()
16662306a36Sopenharmony_ciand pwm_put() may not be called from an atomic context. Currently the
16762306a36Sopenharmony_ciPWM core does not enforce any locking to pwm_enable(), pwm_disable() and
16862306a36Sopenharmony_cipwm_config(), so the calling context is currently driver specific. This
16962306a36Sopenharmony_ciis an issue derived from the former barebone API and should be fixed soon.
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ciHelpers
17262306a36Sopenharmony_ci-------
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ciCurrently a PWM can only be configured with period_ns and duty_ns. For several
17562306a36Sopenharmony_ciuse cases freq_hz and duty_percent might be better. Instead of calculating
17662306a36Sopenharmony_cithis in your driver please consider adding appropriate helpers to the framework.
177