Back to home page

OSCL-LXR

 
 

    


0001 ======================================
0002 Pulse Width Modulation (PWM) interface
0003 ======================================
0004 
0005 This provides an overview about the Linux PWM interface
0006 
0007 PWMs are commonly used for controlling LEDs, fans or vibrators in
0008 cell phones. PWMs with a fixed purpose have no need implementing
0009 the Linux PWM API (although they could). However, PWMs are often
0010 found as discrete devices on SoCs which have no fixed purpose. It's
0011 up to the board designer to connect them to LEDs or fans. To provide
0012 this kind of flexibility the generic PWM API exists.
0013 
0014 Identifying PWMs
0015 ----------------
0016 
0017 Users of the legacy PWM API use unique IDs to refer to PWM devices.
0018 
0019 Instead of referring to a PWM device via its unique ID, board setup code
0020 should instead register a static mapping that can be used to match PWM
0021 consumers to providers, as given in the following example::
0022 
0023         static struct pwm_lookup board_pwm_lookup[] = {
0024                 PWM_LOOKUP("tegra-pwm", 0, "pwm-backlight", NULL,
0025                            50000, PWM_POLARITY_NORMAL),
0026         };
0027 
0028         static void __init board_init(void)
0029         {
0030                 ...
0031                 pwm_add_table(board_pwm_lookup, ARRAY_SIZE(board_pwm_lookup));
0032                 ...
0033         }
0034 
0035 Using PWMs
0036 ----------
0037 
0038 Legacy users can request a PWM device using pwm_request() and free it
0039 after usage with pwm_free().
0040 
0041 New users should use the pwm_get() function and pass to it the consumer
0042 device or a consumer name. pwm_put() is used to free the PWM device. Managed
0043 variants of the getter, devm_pwm_get(), devm_of_pwm_get(),
0044 devm_fwnode_pwm_get(), also exist.
0045 
0046 After being requested, a PWM has to be configured using::
0047 
0048         int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
0049 
0050 This API controls both the PWM period/duty_cycle config and the
0051 enable/disable state.
0052 
0053 As a consumer, don't rely on the output's state for a disabled PWM. If it's
0054 easily possible, drivers are supposed to emit the inactive state, but some
0055 drivers cannot. If you rely on getting the inactive state, use .duty_cycle=0,
0056 .enabled=true.
0057 
0058 There is also a usage_power setting: If set, the PWM driver is only required to
0059 maintain the power output but has more freedom regarding signal form.
0060 If supported by the driver, the signal can be optimized, for example to improve
0061 EMI by phase shifting the individual channels of a chip.
0062 
0063 The pwm_config(), pwm_enable() and pwm_disable() functions are just wrappers
0064 around pwm_apply_state() and should not be used if the user wants to change
0065 several parameter at once. For example, if you see pwm_config() and
0066 pwm_{enable,disable}() calls in the same function, this probably means you
0067 should switch to pwm_apply_state().
0068 
0069 The PWM user API also allows one to query the PWM state that was passed to the
0070 last invocation of pwm_apply_state() using pwm_get_state(). Note this is
0071 different to what the driver has actually implemented if the request cannot be
0072 satisfied exactly with the hardware in use. There is currently no way for
0073 consumers to get the actually implemented settings.
0074 
0075 In addition to the PWM state, the PWM API also exposes PWM arguments, which
0076 are the reference PWM config one should use on this PWM.
0077 PWM arguments are usually platform-specific and allows the PWM user to only
0078 care about dutycycle relatively to the full period (like, duty = 50% of the
0079 period). struct pwm_args contains 2 fields (period and polarity) and should
0080 be used to set the initial PWM config (usually done in the probe function
0081 of the PWM user). PWM arguments are retrieved with pwm_get_args().
0082 
0083 All consumers should really be reconfiguring the PWM upon resume as
0084 appropriate. This is the only way to ensure that everything is resumed in
0085 the proper order.
0086 
0087 Using PWMs with the sysfs interface
0088 -----------------------------------
0089 
0090 If CONFIG_SYSFS is enabled in your kernel configuration a simple sysfs
0091 interface is provided to use the PWMs from userspace. It is exposed at
0092 /sys/class/pwm/. Each probed PWM controller/chip will be exported as
0093 pwmchipN, where N is the base of the PWM chip. Inside the directory you
0094 will find:
0095 
0096   npwm
0097     The number of PWM channels this chip supports (read-only).
0098 
0099   export
0100     Exports a PWM channel for use with sysfs (write-only).
0101 
0102   unexport
0103    Unexports a PWM channel from sysfs (write-only).
0104 
0105 The PWM channels are numbered using a per-chip index from 0 to npwm-1.
0106 
0107 When a PWM channel is exported a pwmX directory will be created in the
0108 pwmchipN directory it is associated with, where X is the number of the
0109 channel that was exported. The following properties will then be available:
0110 
0111   period
0112     The total period of the PWM signal (read/write).
0113     Value is in nanoseconds and is the sum of the active and inactive
0114     time of the PWM.
0115 
0116   duty_cycle
0117     The active time of the PWM signal (read/write).
0118     Value is in nanoseconds and must be less than the period.
0119 
0120   polarity
0121     Changes the polarity of the PWM signal (read/write).
0122     Writes to this property only work if the PWM chip supports changing
0123     the polarity. The polarity can only be changed if the PWM is not
0124     enabled. Value is the string "normal" or "inversed".
0125 
0126   enable
0127     Enable/disable the PWM signal (read/write).
0128 
0129         - 0 - disabled
0130         - 1 - enabled
0131 
0132 Implementing a PWM driver
0133 -------------------------
0134 
0135 Currently there are two ways to implement pwm drivers. Traditionally
0136 there only has been the barebone API meaning that each driver has
0137 to implement the pwm_*() functions itself. This means that it's impossible
0138 to have multiple PWM drivers in the system. For this reason it's mandatory
0139 for new drivers to use the generic PWM framework.
0140 
0141 A new PWM controller/chip can be added using pwmchip_add() and removed
0142 again with pwmchip_remove(). pwmchip_add() takes a filled in struct
0143 pwm_chip as argument which provides a description of the PWM chip, the
0144 number of PWM devices provided by the chip and the chip-specific
0145 implementation of the supported PWM operations to the framework.
0146 
0147 When implementing polarity support in a PWM driver, make sure to respect the
0148 signal conventions in the PWM framework. By definition, normal polarity
0149 characterizes a signal starts high for the duration of the duty cycle and
0150 goes low for the remainder of the period. Conversely, a signal with inversed
0151 polarity starts low for the duration of the duty cycle and goes high for the
0152 remainder of the period.
0153 
0154 Drivers are encouraged to implement ->apply() instead of the legacy
0155 ->enable(), ->disable() and ->config() methods. Doing that should provide
0156 atomicity in the PWM config workflow, which is required when the PWM controls
0157 a critical device (like a regulator).
0158 
0159 The implementation of ->get_state() (a method used to retrieve initial PWM
0160 state) is also encouraged for the same reason: letting the PWM user know
0161 about the current PWM state would allow him to avoid glitches.
0162 
0163 Drivers should not implement any power management. In other words,
0164 consumers should implement it as described in the "Using PWMs" section.
0165 
0166 Locking
0167 -------
0168 
0169 The PWM core list manipulations are protected by a mutex, so pwm_request()
0170 and pwm_free() may not be called from an atomic context. Currently the
0171 PWM core does not enforce any locking to pwm_enable(), pwm_disable() and
0172 pwm_config(), so the calling context is currently driver specific. This
0173 is an issue derived from the former barebone API and should be fixed soon.
0174 
0175 Helpers
0176 -------
0177 
0178 Currently a PWM can only be configured with period_ns and duty_ns. For several
0179 use cases freq_hz and duty_percent might be better. Instead of calculating
0180 this in your driver please consider adding appropriate helpers to the framework.