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