File Index
Files and directories
Sign in
FILE

pwm.h

File preview with syntax highlighting.

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