Merge tag 'iommu-updates-v6.4' of git://git.kernel.org/pub/scm/linux/kernel/git/joro...
[linux-2.6-block.git] / include / linux / pwm.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1a189b97
RK
2#ifndef __LINUX_PWM_H
3#define __LINUX_PWM_H
4
0bcf168b 5#include <linux/err.h>
d1cd2142 6#include <linux/mutex.h>
7299ab70
TR
7#include <linux/of.h>
8
0c2498f1
SH
9struct pwm_chip;
10
0aa0869c
PA
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 */
20enum pwm_polarity {
21 PWM_POLARITY_NORMAL,
22 PWM_POLARITY_INVERSED,
23};
24
e39c0df1
BB
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 */
38struct pwm_args {
a9d887dc 39 u64 period;
e39c0df1
BB
40 enum pwm_polarity polarity;
41};
42
f051c466
TR
43enum {
44 PWMF_REQUESTED = 1 << 0,
09a7e4a3 45 PWMF_EXPORTED = 1 << 1,
f051c466
TR
46};
47
43a276b0
BB
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
09a7e4a3 53 * @enabled: PWM enabled status
9e40ee18
CG
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.
43a276b0
BB
58 */
59struct pwm_state {
a9d887dc
GDS
60 u64 period;
61 u64 duty_cycle;
43a276b0 62 enum pwm_polarity polarity;
09a7e4a3 63 bool enabled;
9e40ee18 64 bool usage_power;
43a276b0
BB
65};
66
04883802
TR
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
e39c0df1 75 * @args: PWM arguments
3ad1f3a3
UKK
76 * @state: last applied state
77 * @last: last implemented state (for PWM_DEBUG)
04883802 78 */
f051c466 79struct pwm_device {
6bc7064a
TR
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
e39c0df1 87 struct pwm_args args;
43a276b0 88 struct pwm_state state;
3ad1f3a3 89 struct pwm_state last;
f051c466
TR
90};
91
43a276b0
BB
92/**
93 * pwm_get_state() - retrieve the current PWM state
94 * @pwm: PWM device
95 * @state: state to fill with the current PWM state
1a7a6e80
UKK
96 *
97 * The returned PWM state represents the state that was applied by a previous call to
98 * pwm_apply_state(). Drivers may have to slightly tweak that state before programming it to
99 * hardware. If pwm_apply_state() was never called, this returns either the current hardware
100 * state (if supported) or the default settings.
43a276b0
BB
101 */
102static inline void pwm_get_state(const struct pwm_device *pwm,
103 struct pwm_state *state)
104{
105 *state = pwm->state;
106}
107
5c31252c
BB
108static inline bool pwm_is_enabled(const struct pwm_device *pwm)
109{
09a7e4a3
BB
110 struct pwm_state state;
111
112 pwm_get_state(pwm, &state);
113
114 return state.enabled;
5c31252c
BB
115}
116
a9d887dc 117static inline void pwm_set_period(struct pwm_device *pwm, u64 period)
f051c466
TR
118{
119 if (pwm)
43a276b0 120 pwm->state.period = period;
f051c466
TR
121}
122
a9d887dc 123static inline u64 pwm_get_period(const struct pwm_device *pwm)
f051c466 124{
43a276b0
BB
125 struct pwm_state state;
126
127 pwm_get_state(pwm, &state);
128
129 return state.period;
f051c466
TR
130}
131
76abbdde
HS
132static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
133{
134 if (pwm)
43a276b0 135 pwm->state.duty_cycle = duty;
76abbdde
HS
136}
137
a9d887dc 138static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
76abbdde 139{
43a276b0
BB
140 struct pwm_state state;
141
142 pwm_get_state(pwm, &state);
143
144 return state.duty_cycle;
76abbdde
HS
145}
146
011e7631
BB
147static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
148{
43a276b0
BB
149 struct pwm_state state;
150
151 pwm_get_state(pwm, &state);
152
153 return state.polarity;
011e7631
BB
154}
155
e39c0df1
BB
156static inline void pwm_get_args(const struct pwm_device *pwm,
157 struct pwm_args *args)
158{
159 *args = pwm->args;
160}
161
a6a0dbbc
BB
162/**
163 * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
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_state(). 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_state().
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_state() without adjusting ->duty_cycle
177 * first.
178 */
179static 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;
9e40ee18 193 state->usage_power = false;
a6a0dbbc
BB
194}
195
f6f3bddf
BB
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 */
209static inline unsigned int
210pwm_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_state(pwm, &state);
233 *
234 * This functions returns -EINVAL if @duty_cycle and/or @scale are
235 * inconsistent (@scale == 0 or @duty_cycle > @scale).
236 */
237static inline int
238pwm_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
ef2e35d9
UKK
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 */
256struct pwm_capture {
257 unsigned int period;
258 unsigned int duty_cycle;
259};
260
0c2498f1
SH
261/**
262 * struct pwm_ops - PWM controller operations
263 * @request: optional hook for requesting a PWM
264 * @free: optional hook for freeing a PWM
3a3d1a4e 265 * @capture: capture and report PWM signal
27938fd8 266 * @apply: atomically apply a new PWM config
15fa8a43
BB
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.
0c2498f1
SH
270 * @owner: helps prevent removal of modules exporting active PWMs
271 */
272struct pwm_ops {
6bc7064a
TR
273 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
274 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
3a3d1a4e
LJ
275 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
276 struct pwm_capture *result, unsigned long timeout);
5ec803ed 277 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
71523d18 278 const struct pwm_state *state);
6c452cff
UKK
279 int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
280 struct pwm_state *state);
6bc7064a 281 struct module *owner;
0c2498f1
SH
282};
283
284/**
f051c466
TR
285 * struct pwm_chip - abstract a PWM controller
286 * @dev: device providing the PWMs
f051c466
TR
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
04883802
TR
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
5d0a4c11
UKK
292 * @list: list node for internal use
293 * @pwms: array of PWM devices allocated by the framework
0c2498f1
SH
294 */
295struct pwm_chip {
6bc7064a 296 struct device *dev;
6bc7064a
TR
297 const struct pwm_ops *ops;
298 int base;
299 unsigned int npwm;
300
6bc7064a
TR
301 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
302 const struct of_phandle_args *args);
303 unsigned int of_pwm_n_cells;
5d0a4c11
UKK
304
305 /* only used internally by the PWM framework */
306 struct list_head list;
307 struct pwm_device *pwms;
0c2498f1
SH
308};
309
0bcf168b 310#if IS_ENABLED(CONFIG_PWM)
5ec803ed
BB
311/* PWM user APIs */
312struct pwm_device *pwm_request(int pwm_id, const char *label);
313void pwm_free(struct pwm_device *pwm);
71523d18 314int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state);
5ec803ed
BB
315int pwm_adjust_config(struct pwm_device *pwm);
316
317/**
318 * pwm_config() - change a PWM device configuration
319 * @pwm: PWM device
320 * @duty_ns: "on" time (in nanoseconds)
321 * @period_ns: duration (in nanoseconds) of one cycle
322 *
323 * Returns: 0 on success or a negative error code on failure.
324 */
325static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
326 int period_ns)
327{
328 struct pwm_state state;
329
330 if (!pwm)
331 return -EINVAL;
332
ef2bf499
BN
333 if (duty_ns < 0 || period_ns < 0)
334 return -EINVAL;
335
5ec803ed
BB
336 pwm_get_state(pwm, &state);
337 if (state.duty_cycle == duty_ns && state.period == period_ns)
338 return 0;
339
340 state.duty_cycle = duty_ns;
341 state.period = period_ns;
342 return pwm_apply_state(pwm, &state);
343}
344
5ec803ed
BB
345/**
346 * pwm_enable() - start a PWM output toggling
347 * @pwm: PWM device
348 *
349 * Returns: 0 on success or a negative error code on failure.
350 */
351static inline int pwm_enable(struct pwm_device *pwm)
352{
353 struct pwm_state state;
354
355 if (!pwm)
356 return -EINVAL;
357
358 pwm_get_state(pwm, &state);
359 if (state.enabled)
360 return 0;
361
362 state.enabled = true;
363 return pwm_apply_state(pwm, &state);
364}
365
366/**
367 * pwm_disable() - stop a PWM output toggling
368 * @pwm: PWM device
369 */
370static inline void pwm_disable(struct pwm_device *pwm)
371{
372 struct pwm_state state;
373
374 if (!pwm)
375 return;
376
377 pwm_get_state(pwm, &state);
378 if (!state.enabled)
379 return;
380
381 state.enabled = false;
382 pwm_apply_state(pwm, &state);
383}
384
5ec803ed 385/* PWM provider APIs */
3a3d1a4e
LJ
386int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
387 unsigned long timeout);
f051c466
TR
388int pwm_set_chip_data(struct pwm_device *pwm, void *data);
389void *pwm_get_chip_data(struct pwm_device *pwm);
390
0c2498f1 391int pwmchip_add(struct pwm_chip *chip);
8083f58d 392void pwmchip_remove(struct pwm_chip *chip);
bcda91bf
UKK
393
394int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip);
395
f051c466
TR
396struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
397 unsigned int index,
398 const char *label);
8138d2dd 399
83af2402
PA
400struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
401 const struct of_phandle_args *args);
3ab7b6ac
BA
402struct pwm_device *of_pwm_single_xlate(struct pwm_chip *pc,
403 const struct of_phandle_args *args);
83af2402 404
d4c0c470 405struct pwm_device *pwm_get(struct device *dev, const char *con_id);
8138d2dd
TR
406void pwm_put(struct pwm_device *pwm);
407
d4c0c470 408struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
4a6ef8e3
NV
409struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
410 struct fwnode_handle *fwnode,
411 const char *con_id);
0bcf168b 412#else
5ec803ed
BB
413static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
414{
27d9a4d6 415 might_sleep();
5ec803ed
BB
416 return ERR_PTR(-ENODEV);
417}
418
419static inline void pwm_free(struct pwm_device *pwm)
420{
27d9a4d6 421 might_sleep();
5ec803ed
BB
422}
423
424static inline int pwm_apply_state(struct pwm_device *pwm,
425 const struct pwm_state *state)
426{
4ad91a22 427 might_sleep();
5ec803ed
BB
428 return -ENOTSUPP;
429}
430
431static inline int pwm_adjust_config(struct pwm_device *pwm)
432{
433 return -ENOTSUPP;
434}
435
436static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
437 int period_ns)
438{
4ad91a22 439 might_sleep();
5ec803ed
BB
440 return -EINVAL;
441}
442
5ec803ed
BB
443static inline int pwm_enable(struct pwm_device *pwm)
444{
4ad91a22 445 might_sleep();
5ec803ed
BB
446 return -EINVAL;
447}
448
449static inline void pwm_disable(struct pwm_device *pwm)
450{
4ad91a22 451 might_sleep();
5ec803ed
BB
452}
453
b3c650ad
GU
454static inline int pwm_capture(struct pwm_device *pwm,
455 struct pwm_capture *result,
456 unsigned long timeout)
457{
458 return -EINVAL;
459}
460
0bcf168b
TB
461static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
462{
463 return -EINVAL;
464}
465
466static inline void *pwm_get_chip_data(struct pwm_device *pwm)
467{
468 return NULL;
469}
470
471static inline int pwmchip_add(struct pwm_chip *chip)
472{
473 return -EINVAL;
474}
475
476static inline int pwmchip_remove(struct pwm_chip *chip)
477{
478 return -EINVAL;
479}
480
88da4e81
AS
481static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
482{
483 return -EINVAL;
484}
485
0bcf168b
TB
486static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
487 unsigned int index,
488 const char *label)
489{
27d9a4d6 490 might_sleep();
0bcf168b
TB
491 return ERR_PTR(-ENODEV);
492}
493
494static inline struct pwm_device *pwm_get(struct device *dev,
495 const char *consumer)
496{
27d9a4d6 497 might_sleep();
0bcf168b
TB
498 return ERR_PTR(-ENODEV);
499}
500
501static inline void pwm_put(struct pwm_device *pwm)
502{
27d9a4d6 503 might_sleep();
0bcf168b
TB
504}
505
506static inline struct pwm_device *devm_pwm_get(struct device *dev,
507 const char *consumer)
508{
27d9a4d6 509 might_sleep();
0bcf168b
TB
510 return ERR_PTR(-ENODEV);
511}
512
4a6ef8e3
NV
513static inline struct pwm_device *
514devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
515 const char *con_id)
516{
27d9a4d6 517 might_sleep();
4a6ef8e3
NV
518 return ERR_PTR(-ENODEV);
519}
0bcf168b 520#endif
6354316d 521
5ec803ed
BB
522static inline void pwm_apply_args(struct pwm_device *pwm)
523{
33cdcee0
BB
524 struct pwm_state state = { };
525
5ec803ed
BB
526 /*
527 * PWM users calling pwm_apply_args() expect to have a fresh config
528 * where the polarity and period are set according to pwm_args info.
529 * The problem is, polarity can only be changed when the PWM is
530 * disabled.
531 *
532 * PWM drivers supporting hardware readout may declare the PWM device
533 * as enabled, and prevent polarity setting, which changes from the
534 * existing behavior, where all PWM devices are declared as disabled
535 * at startup (even if they are actually enabled), thus authorizing
536 * polarity setting.
537 *
33cdcee0
BB
538 * To fulfill this requirement, we apply a new state which disables
539 * the PWM device and set the reference period and polarity config.
5ec803ed
BB
540 *
541 * Note that PWM users requiring a smooth handover between the
542 * bootloader and the kernel (like critical regulators controlled by
543 * PWM devices) will have to switch to the atomic API and avoid calling
544 * pwm_apply_args().
545 */
33cdcee0
BB
546
547 state.enabled = false;
548 state.polarity = pwm->args.polarity;
549 state.period = pwm->args.period;
9e40ee18 550 state.usage_power = false;
33cdcee0
BB
551
552 pwm_apply_state(pwm, &state);
5ec803ed
BB
553}
554
8138d2dd
TR
555struct pwm_lookup {
556 struct list_head list;
557 const char *provider;
558 unsigned int index;
559 const char *dev_id;
560 const char *con_id;
3796ce1d
AB
561 unsigned int period;
562 enum pwm_polarity polarity;
b526a314 563 const char *module; /* optional, may be NULL */
8138d2dd
TR
564};
565
b526a314
HG
566#define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
567 _period, _polarity, _module) \
568 { \
569 .provider = _provider, \
570 .index = _index, \
571 .dev_id = _dev_id, \
572 .con_id = _con_id, \
573 .period = _period, \
574 .polarity = _polarity, \
575 .module = _module, \
8138d2dd
TR
576 }
577
b526a314
HG
578#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
579 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
580 _polarity, NULL)
581
0bcf168b 582#if IS_ENABLED(CONFIG_PWM)
8138d2dd 583void pwm_add_table(struct pwm_lookup *table, size_t num);
efb0de55 584void pwm_remove_table(struct pwm_lookup *table, size_t num);
0bcf168b
TB
585#else
586static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
587{
588}
efb0de55
SK
589
590static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
591{
592}
0c2498f1
SH
593#endif
594
76abbdde
HS
595#ifdef CONFIG_PWM_SYSFS
596void pwmchip_sysfs_export(struct pwm_chip *chip);
597void pwmchip_sysfs_unexport(struct pwm_chip *chip);
598#else
599static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
600{
601}
602
603static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
604{
605}
606#endif /* CONFIG_PWM_SYSFS */
607
5243ef8b 608#endif /* __LINUX_PWM_H */