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