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