xref: /OK3568_Linux_fs/kernel/Documentation/driver-api/pwm.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun======================================
2*4882a593SmuzhiyunPulse Width Modulation (PWM) interface
3*4882a593Smuzhiyun======================================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunThis provides an overview about the Linux PWM interface
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunPWMs are commonly used for controlling LEDs, fans or vibrators in
8*4882a593Smuzhiyuncell phones. PWMs with a fixed purpose have no need implementing
9*4882a593Smuzhiyunthe Linux PWM API (although they could). However, PWMs are often
10*4882a593Smuzhiyunfound as discrete devices on SoCs which have no fixed purpose. It's
11*4882a593Smuzhiyunup to the board designer to connect them to LEDs or fans. To provide
12*4882a593Smuzhiyunthis kind of flexibility the generic PWM API exists.
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunIdentifying PWMs
15*4882a593Smuzhiyun----------------
16*4882a593Smuzhiyun
17*4882a593SmuzhiyunUsers of the legacy PWM API use unique IDs to refer to PWM devices.
18*4882a593Smuzhiyun
19*4882a593SmuzhiyunInstead of referring to a PWM device via its unique ID, board setup code
20*4882a593Smuzhiyunshould instead register a static mapping that can be used to match PWM
21*4882a593Smuzhiyunconsumers to providers, as given in the following example::
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun	static struct pwm_lookup board_pwm_lookup[] = {
24*4882a593Smuzhiyun		PWM_LOOKUP("tegra-pwm", 0, "pwm-backlight", NULL,
25*4882a593Smuzhiyun			   50000, PWM_POLARITY_NORMAL),
26*4882a593Smuzhiyun	};
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun	static void __init board_init(void)
29*4882a593Smuzhiyun	{
30*4882a593Smuzhiyun		...
31*4882a593Smuzhiyun		pwm_add_table(board_pwm_lookup, ARRAY_SIZE(board_pwm_lookup));
32*4882a593Smuzhiyun		...
33*4882a593Smuzhiyun	}
34*4882a593Smuzhiyun
35*4882a593SmuzhiyunUsing PWMs
36*4882a593Smuzhiyun----------
37*4882a593Smuzhiyun
38*4882a593SmuzhiyunLegacy users can request a PWM device using pwm_request() and free it
39*4882a593Smuzhiyunafter usage with pwm_free().
40*4882a593Smuzhiyun
41*4882a593SmuzhiyunNew users should use the pwm_get() function and pass to it the consumer
42*4882a593Smuzhiyundevice or a consumer name. pwm_put() is used to free the PWM device. Managed
43*4882a593Smuzhiyunvariants of these functions, devm_pwm_get() and devm_pwm_put(), also exist.
44*4882a593Smuzhiyun
45*4882a593SmuzhiyunAfter being requested, a PWM has to be configured using::
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun	int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
48*4882a593Smuzhiyun
49*4882a593SmuzhiyunThis API controls both the PWM period/duty_cycle config and the
50*4882a593Smuzhiyunenable/disable state.
51*4882a593Smuzhiyun
52*4882a593SmuzhiyunThe pwm_config(), pwm_enable() and pwm_disable() functions are just wrappers
53*4882a593Smuzhiyunaround pwm_apply_state() and should not be used if the user wants to change
54*4882a593Smuzhiyunseveral parameter at once. For example, if you see pwm_config() and
55*4882a593Smuzhiyunpwm_{enable,disable}() calls in the same function, this probably means you
56*4882a593Smuzhiyunshould switch to pwm_apply_state().
57*4882a593Smuzhiyun
58*4882a593SmuzhiyunThe PWM user API also allows one to query the PWM state with pwm_get_state().
59*4882a593Smuzhiyun
60*4882a593SmuzhiyunIn addition to the PWM state, the PWM API also exposes PWM arguments, which
61*4882a593Smuzhiyunare the reference PWM config one should use on this PWM.
62*4882a593SmuzhiyunPWM arguments are usually platform-specific and allows the PWM user to only
63*4882a593Smuzhiyuncare about dutycycle relatively to the full period (like, duty = 50% of the
64*4882a593Smuzhiyunperiod). struct pwm_args contains 2 fields (period and polarity) and should
65*4882a593Smuzhiyunbe used to set the initial PWM config (usually done in the probe function
66*4882a593Smuzhiyunof the PWM user). PWM arguments are retrieved with pwm_get_args().
67*4882a593Smuzhiyun
68*4882a593SmuzhiyunAll consumers should really be reconfiguring the PWM upon resume as
69*4882a593Smuzhiyunappropriate. This is the only way to ensure that everything is resumed in
70*4882a593Smuzhiyunthe proper order.
71*4882a593Smuzhiyun
72*4882a593SmuzhiyunUsing PWMs with the sysfs interface
73*4882a593Smuzhiyun-----------------------------------
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunIf CONFIG_SYSFS is enabled in your kernel configuration a simple sysfs
76*4882a593Smuzhiyuninterface is provided to use the PWMs from userspace. It is exposed at
77*4882a593Smuzhiyun/sys/class/pwm/. Each probed PWM controller/chip will be exported as
78*4882a593SmuzhiyunpwmchipN, where N is the base of the PWM chip. Inside the directory you
79*4882a593Smuzhiyunwill find:
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun  npwm
82*4882a593Smuzhiyun    The number of PWM channels this chip supports (read-only).
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun  export
85*4882a593Smuzhiyun    Exports a PWM channel for use with sysfs (write-only).
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun  unexport
88*4882a593Smuzhiyun   Unexports a PWM channel from sysfs (write-only).
89*4882a593Smuzhiyun
90*4882a593SmuzhiyunThe PWM channels are numbered using a per-chip index from 0 to npwm-1.
91*4882a593Smuzhiyun
92*4882a593SmuzhiyunWhen a PWM channel is exported a pwmX directory will be created in the
93*4882a593SmuzhiyunpwmchipN directory it is associated with, where X is the number of the
94*4882a593Smuzhiyunchannel that was exported. The following properties will then be available:
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun  period
97*4882a593Smuzhiyun    The total period of the PWM signal (read/write).
98*4882a593Smuzhiyun    Value is in nanoseconds and is the sum of the active and inactive
99*4882a593Smuzhiyun    time of the PWM.
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun  duty_cycle
102*4882a593Smuzhiyun    The active time of the PWM signal (read/write).
103*4882a593Smuzhiyun    Value is in nanoseconds and must be less than the period.
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun  polarity
106*4882a593Smuzhiyun    Changes the polarity of the PWM signal (read/write).
107*4882a593Smuzhiyun    Writes to this property only work if the PWM chip supports changing
108*4882a593Smuzhiyun    the polarity. The polarity can only be changed if the PWM is not
109*4882a593Smuzhiyun    enabled. Value is the string "normal" or "inversed".
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun  enable
112*4882a593Smuzhiyun    Enable/disable the PWM signal (read/write).
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun	- 0 - disabled
115*4882a593Smuzhiyun	- 1 - enabled
116*4882a593Smuzhiyun
117*4882a593SmuzhiyunImplementing a PWM driver
118*4882a593Smuzhiyun-------------------------
119*4882a593Smuzhiyun
120*4882a593SmuzhiyunCurrently there are two ways to implement pwm drivers. Traditionally
121*4882a593Smuzhiyunthere only has been the barebone API meaning that each driver has
122*4882a593Smuzhiyunto implement the pwm_*() functions itself. This means that it's impossible
123*4882a593Smuzhiyunto have multiple PWM drivers in the system. For this reason it's mandatory
124*4882a593Smuzhiyunfor new drivers to use the generic PWM framework.
125*4882a593Smuzhiyun
126*4882a593SmuzhiyunA new PWM controller/chip can be added using pwmchip_add() and removed
127*4882a593Smuzhiyunagain with pwmchip_remove(). pwmchip_add() takes a filled in struct
128*4882a593Smuzhiyunpwm_chip as argument which provides a description of the PWM chip, the
129*4882a593Smuzhiyunnumber of PWM devices provided by the chip and the chip-specific
130*4882a593Smuzhiyunimplementation of the supported PWM operations to the framework.
131*4882a593Smuzhiyun
132*4882a593SmuzhiyunWhen implementing polarity support in a PWM driver, make sure to respect the
133*4882a593Smuzhiyunsignal conventions in the PWM framework. By definition, normal polarity
134*4882a593Smuzhiyuncharacterizes a signal starts high for the duration of the duty cycle and
135*4882a593Smuzhiyungoes low for the remainder of the period. Conversely, a signal with inversed
136*4882a593Smuzhiyunpolarity starts low for the duration of the duty cycle and goes high for the
137*4882a593Smuzhiyunremainder of the period.
138*4882a593Smuzhiyun
139*4882a593SmuzhiyunDrivers are encouraged to implement ->apply() instead of the legacy
140*4882a593Smuzhiyun->enable(), ->disable() and ->config() methods. Doing that should provide
141*4882a593Smuzhiyunatomicity in the PWM config workflow, which is required when the PWM controls
142*4882a593Smuzhiyuna critical device (like a regulator).
143*4882a593Smuzhiyun
144*4882a593SmuzhiyunThe implementation of ->get_state() (a method used to retrieve initial PWM
145*4882a593Smuzhiyunstate) is also encouraged for the same reason: letting the PWM user know
146*4882a593Smuzhiyunabout the current PWM state would allow him to avoid glitches.
147*4882a593Smuzhiyun
148*4882a593SmuzhiyunDrivers should not implement any power management. In other words,
149*4882a593Smuzhiyunconsumers should implement it as described in the "Using PWMs" section.
150*4882a593Smuzhiyun
151*4882a593SmuzhiyunLocking
152*4882a593Smuzhiyun-------
153*4882a593Smuzhiyun
154*4882a593SmuzhiyunThe PWM core list manipulations are protected by a mutex, so pwm_request()
155*4882a593Smuzhiyunand pwm_free() may not be called from an atomic context. Currently the
156*4882a593SmuzhiyunPWM core does not enforce any locking to pwm_enable(), pwm_disable() and
157*4882a593Smuzhiyunpwm_config(), so the calling context is currently driver specific. This
158*4882a593Smuzhiyunis an issue derived from the former barebone API and should be fixed soon.
159*4882a593Smuzhiyun
160*4882a593SmuzhiyunHelpers
161*4882a593Smuzhiyun-------
162*4882a593Smuzhiyun
163*4882a593SmuzhiyunCurrently a PWM can only be configured with period_ns and duty_ns. For several
164*4882a593Smuzhiyunuse cases freq_hz and duty_percent might be better. Instead of calculating
165*4882a593Smuzhiyunthis in your driver please consider adding appropriate helpers to the framework.
166