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