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