Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __LINUX_PWM_H
3#define __LINUX_PWM_H
4
5#include <linux/err.h>
6#include <linux/mutex.h>
7#include <linux/of.h>
8
9struct pwm_chip;
10
11/**
12 * enum pwm_polarity - polarity of a PWM signal
13 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
14 * cycle, followed by a low signal for the remainder of the pulse
15 * period
16 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
17 * cycle, followed by a high signal for the remainder of the pulse
18 * period
19 */
20enum pwm_polarity {
21 PWM_POLARITY_NORMAL,
22 PWM_POLARITY_INVERSED,
23};
24
25/**
26 * struct pwm_args - board-dependent PWM arguments
27 * @period: reference period
28 * @polarity: reference polarity
29 *
30 * This structure describes board-dependent arguments attached to a PWM
31 * device. These arguments are usually retrieved from the PWM lookup table or
32 * device tree.
33 *
34 * Do not confuse this with the PWM state: PWM arguments represent the initial
35 * configuration that users want to use on this PWM device rather than the
36 * current PWM hardware state.
37 */
38struct pwm_args {
39 u64 period;
40 enum pwm_polarity polarity;
41};
42
43enum {
44 PWMF_REQUESTED = 0,
45 PWMF_EXPORTED = 1,
46};
47
48/*
49 * struct pwm_state - state of a PWM channel
50 * @period: PWM period (in nanoseconds)
51 * @duty_cycle: PWM duty cycle (in nanoseconds)
52 * @polarity: PWM polarity
53 * @enabled: PWM enabled status
54 * @usage_power: If set, the PWM driver is only required to maintain the power
55 * output but has more freedom regarding signal form.
56 * If supported, the signal can be optimized, for example to
57 * improve EMI by phase shifting individual channels.
58 */
59struct pwm_state {
60 u64 period;
61 u64 duty_cycle;
62 enum pwm_polarity polarity;
63 bool enabled;
64 bool usage_power;
65};
66
67/**
68 * struct pwm_device - PWM channel object
69 * @label: name of the PWM device
70 * @flags: flags associated with the PWM device
71 * @hwpwm: per-chip relative index of the PWM device
72 * @chip: PWM chip providing this PWM device
73 * @args: PWM arguments
74 * @state: last applied state
75 * @last: last implemented state (for PWM_DEBUG)
76 */
77struct pwm_device {
78 const char *label;
79 unsigned long flags;
80 unsigned int hwpwm;
81 struct pwm_chip *chip;
82
83 struct pwm_args args;
84 struct pwm_state state;
85 struct pwm_state last;
86};
87
88/**
89 * pwm_get_state() - retrieve the current PWM state
90 * @pwm: PWM device
91 * @state: state to fill with the current PWM state
92 *
93 * The returned PWM state represents the state that was applied by a previous call to
94 * pwm_apply_might_sleep(). Drivers may have to slightly tweak that state before programming it to
95 * hardware. If pwm_apply_might_sleep() was never called, this returns either the current hardware
96 * state (if supported) or the default settings.
97 */
98static inline void pwm_get_state(const struct pwm_device *pwm,
99 struct pwm_state *state)
100{
101 *state = pwm->state;
102}
103
104static inline bool pwm_is_enabled(const struct pwm_device *pwm)
105{
106 struct pwm_state state;
107
108 pwm_get_state(pwm, &state);
109
110 return state.enabled;
111}
112
113static inline u64 pwm_get_period(const struct pwm_device *pwm)
114{
115 struct pwm_state state;
116
117 pwm_get_state(pwm, &state);
118
119 return state.period;
120}
121
122static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
123{
124 struct pwm_state state;
125
126 pwm_get_state(pwm, &state);
127
128 return state.duty_cycle;
129}
130
131static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
132{
133 struct pwm_state state;
134
135 pwm_get_state(pwm, &state);
136
137 return state.polarity;
138}
139
140static inline void pwm_get_args(const struct pwm_device *pwm,
141 struct pwm_args *args)
142{
143 *args = pwm->args;
144}
145
146/**
147 * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep()
148 * @pwm: PWM device
149 * @state: state to fill with the prepared PWM state
150 *
151 * This functions prepares a state that can later be tweaked and applied
152 * to the PWM device with pwm_apply_might_sleep(). This is a convenient function
153 * that first retrieves the current PWM state and the replaces the period
154 * and polarity fields with the reference values defined in pwm->args.
155 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
156 * fields according to your needs before calling pwm_apply_might_sleep().
157 *
158 * ->duty_cycle is initially set to zero to avoid cases where the current
159 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
160 * an error if the user calls pwm_apply_might_sleep() without adjusting ->duty_cycle
161 * first.
162 */
163static inline void pwm_init_state(const struct pwm_device *pwm,
164 struct pwm_state *state)
165{
166 struct pwm_args args;
167
168 /* First get the current state. */
169 pwm_get_state(pwm, state);
170
171 /* Then fill it with the reference config */
172 pwm_get_args(pwm, &args);
173
174 state->period = args.period;
175 state->polarity = args.polarity;
176 state->duty_cycle = 0;
177 state->usage_power = false;
178}
179
180/**
181 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
182 * @state: PWM state to extract the duty cycle from
183 * @scale: target scale of the relative duty cycle
184 *
185 * This functions converts the absolute duty cycle stored in @state (expressed
186 * in nanosecond) into a value relative to the period.
187 *
188 * For example if you want to get the duty_cycle expressed in percent, call:
189 *
190 * pwm_get_state(pwm, &state);
191 * duty = pwm_get_relative_duty_cycle(&state, 100);
192 */
193static inline unsigned int
194pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
195{
196 if (!state->period)
197 return 0;
198
199 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
200 state->period);
201}
202
203/**
204 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
205 * @state: PWM state to fill
206 * @duty_cycle: relative duty cycle value
207 * @scale: scale in which @duty_cycle is expressed
208 *
209 * This functions converts a relative into an absolute duty cycle (expressed
210 * in nanoseconds), and puts the result in state->duty_cycle.
211 *
212 * For example if you want to configure a 50% duty cycle, call:
213 *
214 * pwm_init_state(pwm, &state);
215 * pwm_set_relative_duty_cycle(&state, 50, 100);
216 * pwm_apply_might_sleep(pwm, &state);
217 *
218 * This functions returns -EINVAL if @duty_cycle and/or @scale are
219 * inconsistent (@scale == 0 or @duty_cycle > @scale).
220 */
221static inline int
222pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
223 unsigned int scale)
224{
225 if (!scale || duty_cycle > scale)
226 return -EINVAL;
227
228 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
229 state->period,
230 scale);
231
232 return 0;
233}
234
235/**
236 * struct pwm_capture - PWM capture data
237 * @period: period of the PWM signal (in nanoseconds)
238 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
239 */
240struct pwm_capture {
241 unsigned int period;
242 unsigned int duty_cycle;
243};
244
245/**
246 * struct pwm_ops - PWM controller operations
247 * @request: optional hook for requesting a PWM
248 * @free: optional hook for freeing a PWM
249 * @capture: capture and report PWM signal
250 * @apply: atomically apply a new PWM config
251 * @get_state: get the current PWM state. This function is only
252 * called once per PWM device when the PWM chip is
253 * registered.
254 */
255struct pwm_ops {
256 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
257 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
258 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
259 struct pwm_capture *result, unsigned long timeout);
260 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
261 const struct pwm_state *state);
262 int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
263 struct pwm_state *state);
264};
265
266/**
267 * struct pwm_chip - abstract a PWM controller
268 * @dev: device providing the PWMs
269 * @ops: callbacks for this PWM controller
270 * @owner: module providing this chip
271 * @id: unique number of this PWM chip
272 * @npwm: number of PWMs controlled by this chip
273 * @of_xlate: request a PWM device given a device tree PWM specifier
274 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
275 * @atomic: can the driver's ->apply() be called in atomic context
276 * @pwms: array of PWM devices allocated by the framework
277 */
278struct pwm_chip {
279 struct device *dev;
280 const struct pwm_ops *ops;
281 struct module *owner;
282 unsigned int id;
283 unsigned int npwm;
284
285 struct pwm_device * (*of_xlate)(struct pwm_chip *chip,
286 const struct of_phandle_args *args);
287 unsigned int of_pwm_n_cells;
288 bool atomic;
289
290 /* only used internally by the PWM framework */
291 struct pwm_device *pwms;
292};
293
294#if IS_ENABLED(CONFIG_PWM)
295/* PWM user APIs */
296int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state);
297int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state);
298int pwm_adjust_config(struct pwm_device *pwm);
299
300/**
301 * pwm_config() - change a PWM device configuration
302 * @pwm: PWM device
303 * @duty_ns: "on" time (in nanoseconds)
304 * @period_ns: duration (in nanoseconds) of one cycle
305 *
306 * Returns: 0 on success or a negative error code on failure.
307 */
308static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
309 int period_ns)
310{
311 struct pwm_state state;
312
313 if (!pwm)
314 return -EINVAL;
315
316 if (duty_ns < 0 || period_ns < 0)
317 return -EINVAL;
318
319 pwm_get_state(pwm, &state);
320 if (state.duty_cycle == duty_ns && state.period == period_ns)
321 return 0;
322
323 state.duty_cycle = duty_ns;
324 state.period = period_ns;
325 return pwm_apply_might_sleep(pwm, &state);
326}
327
328/**
329 * pwm_enable() - start a PWM output toggling
330 * @pwm: PWM device
331 *
332 * Returns: 0 on success or a negative error code on failure.
333 */
334static inline int pwm_enable(struct pwm_device *pwm)
335{
336 struct pwm_state state;
337
338 if (!pwm)
339 return -EINVAL;
340
341 pwm_get_state(pwm, &state);
342 if (state.enabled)
343 return 0;
344
345 state.enabled = true;
346 return pwm_apply_might_sleep(pwm, &state);
347}
348
349/**
350 * pwm_disable() - stop a PWM output toggling
351 * @pwm: PWM device
352 */
353static inline void pwm_disable(struct pwm_device *pwm)
354{
355 struct pwm_state state;
356
357 if (!pwm)
358 return;
359
360 pwm_get_state(pwm, &state);
361 if (!state.enabled)
362 return;
363
364 state.enabled = false;
365 pwm_apply_might_sleep(pwm, &state);
366}
367
368/**
369 * pwm_might_sleep() - is pwm_apply_atomic() supported?
370 * @pwm: PWM device
371 *
372 * Returns: false if pwm_apply_atomic() can be called from atomic context.
373 */
374static inline bool pwm_might_sleep(struct pwm_device *pwm)
375{
376 return !pwm->chip->atomic;
377}
378
379/* PWM provider APIs */
380int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
381 unsigned long timeout);
382
383int __pwmchip_add(struct pwm_chip *chip, struct module *owner);
384#define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE)
385void pwmchip_remove(struct pwm_chip *chip);
386
387int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner);
388#define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE)
389
390struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
391 unsigned int index,
392 const char *label);
393
394struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip,
395 const struct of_phandle_args *args);
396struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip,
397 const struct of_phandle_args *args);
398
399struct pwm_device *pwm_get(struct device *dev, const char *con_id);
400void pwm_put(struct pwm_device *pwm);
401
402struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
403struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
404 struct fwnode_handle *fwnode,
405 const char *con_id);
406#else
407static inline bool pwm_might_sleep(struct pwm_device *pwm)
408{
409 return true;
410}
411
412static inline int pwm_apply_might_sleep(struct pwm_device *pwm,
413 const struct pwm_state *state)
414{
415 might_sleep();
416 return -EOPNOTSUPP;
417}
418
419static inline int pwm_apply_atomic(struct pwm_device *pwm,
420 const struct pwm_state *state)
421{
422 return -EOPNOTSUPP;
423}
424
425static inline int pwm_adjust_config(struct pwm_device *pwm)
426{
427 return -EOPNOTSUPP;
428}
429
430static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
431 int period_ns)
432{
433 might_sleep();
434 return -EINVAL;
435}
436
437static inline int pwm_enable(struct pwm_device *pwm)
438{
439 might_sleep();
440 return -EINVAL;
441}
442
443static inline void pwm_disable(struct pwm_device *pwm)
444{
445 might_sleep();
446}
447
448static inline int pwm_capture(struct pwm_device *pwm,
449 struct pwm_capture *result,
450 unsigned long timeout)
451{
452 return -EINVAL;
453}
454
455static inline int pwmchip_add(struct pwm_chip *chip)
456{
457 return -EINVAL;
458}
459
460static inline int pwmchip_remove(struct pwm_chip *chip)
461{
462 return -EINVAL;
463}
464
465static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
466{
467 return -EINVAL;
468}
469
470static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
471 unsigned int index,
472 const char *label)
473{
474 might_sleep();
475 return ERR_PTR(-ENODEV);
476}
477
478static inline struct pwm_device *pwm_get(struct device *dev,
479 const char *consumer)
480{
481 might_sleep();
482 return ERR_PTR(-ENODEV);
483}
484
485static inline void pwm_put(struct pwm_device *pwm)
486{
487 might_sleep();
488}
489
490static inline struct pwm_device *devm_pwm_get(struct device *dev,
491 const char *consumer)
492{
493 might_sleep();
494 return ERR_PTR(-ENODEV);
495}
496
497static inline struct pwm_device *
498devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
499 const char *con_id)
500{
501 might_sleep();
502 return ERR_PTR(-ENODEV);
503}
504#endif
505
506static inline void pwm_apply_args(struct pwm_device *pwm)
507{
508 struct pwm_state state = { };
509
510 /*
511 * PWM users calling pwm_apply_args() expect to have a fresh config
512 * where the polarity and period are set according to pwm_args info.
513 * The problem is, polarity can only be changed when the PWM is
514 * disabled.
515 *
516 * PWM drivers supporting hardware readout may declare the PWM device
517 * as enabled, and prevent polarity setting, which changes from the
518 * existing behavior, where all PWM devices are declared as disabled
519 * at startup (even if they are actually enabled), thus authorizing
520 * polarity setting.
521 *
522 * To fulfill this requirement, we apply a new state which disables
523 * the PWM device and set the reference period and polarity config.
524 *
525 * Note that PWM users requiring a smooth handover between the
526 * bootloader and the kernel (like critical regulators controlled by
527 * PWM devices) will have to switch to the atomic API and avoid calling
528 * pwm_apply_args().
529 */
530
531 state.enabled = false;
532 state.polarity = pwm->args.polarity;
533 state.period = pwm->args.period;
534 state.usage_power = false;
535
536 pwm_apply_might_sleep(pwm, &state);
537}
538
539/* only for backwards-compatibility, new code should not use this */
540static inline int pwm_apply_state(struct pwm_device *pwm,
541 const struct pwm_state *state)
542{
543 return pwm_apply_might_sleep(pwm, state);
544}
545
546struct pwm_lookup {
547 struct list_head list;
548 const char *provider;
549 unsigned int index;
550 const char *dev_id;
551 const char *con_id;
552 unsigned int period;
553 enum pwm_polarity polarity;
554 const char *module; /* optional, may be NULL */
555};
556
557#define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
558 _period, _polarity, _module) \
559 { \
560 .provider = _provider, \
561 .index = _index, \
562 .dev_id = _dev_id, \
563 .con_id = _con_id, \
564 .period = _period, \
565 .polarity = _polarity, \
566 .module = _module, \
567 }
568
569#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
570 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
571 _polarity, NULL)
572
573#if IS_ENABLED(CONFIG_PWM)
574void pwm_add_table(struct pwm_lookup *table, size_t num);
575void pwm_remove_table(struct pwm_lookup *table, size_t num);
576#else
577static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
578{
579}
580
581static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
582{
583}
584#endif
585
586#ifdef CONFIG_PWM_SYSFS
587void pwmchip_sysfs_export(struct pwm_chip *chip);
588void pwmchip_sysfs_unexport(struct pwm_chip *chip);
589#else
590static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
591{
592}
593
594static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
595{
596}
597#endif /* CONFIG_PWM_SYSFS */
598
599#endif /* __LINUX_PWM_H */