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