xref: /linux/include/linux/pwm.h (revision ed7171ff9fabc49ae6ed42fbd082a576473836fc)
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  */
102 static inline void pwm_get_state(const struct pwm_device *pwm,
103 				 struct pwm_state *state)
104 {
105 	*state = pwm->state;
106 }
107 
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 
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 
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 
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 
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  */
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
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
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 
296 static inline struct device *pwmchip_parent(const struct pwm_chip *chip)
297 {
298 	return chip->dev.parent;
299 }
300 
301 static inline void *pwmchip_get_drvdata(struct pwm_chip *chip)
302 {
303 	return dev_get_drvdata(&chip->dev);
304 }
305 
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  */
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  */
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  */
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  */
391 static inline bool pwm_might_sleep(struct pwm_device *pwm)
392 {
393 	return !pwm->chip->atomic;
394 }
395 
396 /* PWM provider APIs */
397 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
398 		unsigned long timeout);
399 
400 void pwmchip_put(struct pwm_chip *chip);
401 struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
402 struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
403 
404 int __pwmchip_add(struct pwm_chip *chip, struct module *owner);
405 #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE)
406 void pwmchip_remove(struct pwm_chip *chip);
407 
408 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner);
409 #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE)
410 
411 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip,
412 		const struct of_phandle_args *args);
413 struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip,
414 				       const struct of_phandle_args *args);
415 
416 struct pwm_device *pwm_get(struct device *dev, const char *con_id);
417 void pwm_put(struct pwm_device *pwm);
418 
419 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
420 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
421 				       struct fwnode_handle *fwnode,
422 				       const char *con_id);
423 #else
424 static inline bool pwm_might_sleep(struct pwm_device *pwm)
425 {
426 	return true;
427 }
428 
429 static inline int pwm_apply_might_sleep(struct pwm_device *pwm,
430 					const struct pwm_state *state)
431 {
432 	might_sleep();
433 	return -EOPNOTSUPP;
434 }
435 
436 static inline int pwm_apply_atomic(struct pwm_device *pwm,
437 				   const struct pwm_state *state)
438 {
439 	return -EOPNOTSUPP;
440 }
441 
442 static inline int pwm_adjust_config(struct pwm_device *pwm)
443 {
444 	return -EOPNOTSUPP;
445 }
446 
447 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
448 			     int period_ns)
449 {
450 	might_sleep();
451 	return -EINVAL;
452 }
453 
454 static inline int pwm_enable(struct pwm_device *pwm)
455 {
456 	might_sleep();
457 	return -EINVAL;
458 }
459 
460 static inline void pwm_disable(struct pwm_device *pwm)
461 {
462 	might_sleep();
463 }
464 
465 static inline int pwm_capture(struct pwm_device *pwm,
466 			      struct pwm_capture *result,
467 			      unsigned long timeout)
468 {
469 	return -EINVAL;
470 }
471 
472 static inline void pwmchip_put(struct pwm_chip *chip)
473 {
474 }
475 
476 static inline struct pwm_chip *pwmchip_alloc(struct device *parent,
477 					     unsigned int npwm,
478 					     size_t sizeof_priv)
479 {
480 	return ERR_PTR(-EINVAL);
481 }
482 
483 static inline struct pwm_chip *devm_pwmchip_alloc(struct device *parent,
484 						  unsigned int npwm,
485 						  size_t sizeof_priv)
486 {
487 	return pwmchip_alloc(parent, npwm, sizeof_priv);
488 }
489 
490 static inline int pwmchip_add(struct pwm_chip *chip)
491 {
492 	return -EINVAL;
493 }
494 
495 static inline int pwmchip_remove(struct pwm_chip *chip)
496 {
497 	return -EINVAL;
498 }
499 
500 static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
501 {
502 	return -EINVAL;
503 }
504 
505 static inline struct pwm_device *pwm_get(struct device *dev,
506 					 const char *consumer)
507 {
508 	might_sleep();
509 	return ERR_PTR(-ENODEV);
510 }
511 
512 static inline void pwm_put(struct pwm_device *pwm)
513 {
514 	might_sleep();
515 }
516 
517 static inline struct pwm_device *devm_pwm_get(struct device *dev,
518 					      const char *consumer)
519 {
520 	might_sleep();
521 	return ERR_PTR(-ENODEV);
522 }
523 
524 static inline struct pwm_device *
525 devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
526 		    const char *con_id)
527 {
528 	might_sleep();
529 	return ERR_PTR(-ENODEV);
530 }
531 #endif
532 
533 static inline void pwm_apply_args(struct pwm_device *pwm)
534 {
535 	struct pwm_state state = { };
536 
537 	/*
538 	 * PWM users calling pwm_apply_args() expect to have a fresh config
539 	 * where the polarity and period are set according to pwm_args info.
540 	 * The problem is, polarity can only be changed when the PWM is
541 	 * disabled.
542 	 *
543 	 * PWM drivers supporting hardware readout may declare the PWM device
544 	 * as enabled, and prevent polarity setting, which changes from the
545 	 * existing behavior, where all PWM devices are declared as disabled
546 	 * at startup (even if they are actually enabled), thus authorizing
547 	 * polarity setting.
548 	 *
549 	 * To fulfill this requirement, we apply a new state which disables
550 	 * the PWM device and set the reference period and polarity config.
551 	 *
552 	 * Note that PWM users requiring a smooth handover between the
553 	 * bootloader and the kernel (like critical regulators controlled by
554 	 * PWM devices) will have to switch to the atomic API and avoid calling
555 	 * pwm_apply_args().
556 	 */
557 
558 	state.enabled = false;
559 	state.polarity = pwm->args.polarity;
560 	state.period = pwm->args.period;
561 	state.usage_power = false;
562 
563 	pwm_apply_might_sleep(pwm, &state);
564 }
565 
566 struct pwm_lookup {
567 	struct list_head list;
568 	const char *provider;
569 	unsigned int index;
570 	const char *dev_id;
571 	const char *con_id;
572 	unsigned int period;
573 	enum pwm_polarity polarity;
574 	const char *module; /* optional, may be NULL */
575 };
576 
577 #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id,	\
578 			       _period, _polarity, _module)		\
579 	{								\
580 		.provider = _provider,					\
581 		.index = _index,					\
582 		.dev_id = _dev_id,					\
583 		.con_id = _con_id,					\
584 		.period = _period,					\
585 		.polarity = _polarity,					\
586 		.module = _module,					\
587 	}
588 
589 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
590 	PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
591 			       _polarity, NULL)
592 
593 #if IS_ENABLED(CONFIG_PWM)
594 void pwm_add_table(struct pwm_lookup *table, size_t num);
595 void pwm_remove_table(struct pwm_lookup *table, size_t num);
596 #else
597 static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
598 {
599 }
600 
601 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
602 {
603 }
604 #endif
605 
606 #endif /* __LINUX_PWM_H */
607