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