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