1#ifndef __LINUX_PWM_H
2#define __LINUX_PWM_H
3
4#include <linux/err.h>
5#include <linux/mutex.h>
6#include <linux/of.h>
7
8struct pwm_capture;
9struct seq_file;
10
11struct pwm_chip;
12
13
14
15
16
17
18
19
20
21
22enum pwm_polarity {
23 PWM_POLARITY_NORMAL,
24 PWM_POLARITY_INVERSED,
25};
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40struct pwm_args {
41 unsigned int period;
42 enum pwm_polarity polarity;
43};
44
45enum {
46 PWMF_REQUESTED = 1 << 0,
47 PWMF_EXPORTED = 1 << 1,
48};
49
50
51
52
53
54
55
56
57struct pwm_state {
58 unsigned int period;
59 unsigned int duty_cycle;
60 enum pwm_polarity polarity;
61 bool enabled;
62};
63
64
65
66
67
68
69
70
71
72
73
74
75struct pwm_device {
76 const char *label;
77 unsigned long flags;
78 unsigned int hwpwm;
79 unsigned int pwm;
80 struct pwm_chip *chip;
81 void *chip_data;
82
83 struct pwm_args args;
84 struct pwm_state state;
85};
86
87
88
89
90
91
92static inline void pwm_get_state(const struct pwm_device *pwm,
93 struct pwm_state *state)
94{
95 *state = pwm->state;
96}
97
98static inline bool pwm_is_enabled(const struct pwm_device *pwm)
99{
100 struct pwm_state state;
101
102 pwm_get_state(pwm, &state);
103
104 return state.enabled;
105}
106
107static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
108{
109 if (pwm)
110 pwm->state.period = period;
111}
112
113static inline unsigned int 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
122static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
123{
124 if (pwm)
125 pwm->state.duty_cycle = duty;
126}
127
128static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
129{
130 struct pwm_state state;
131
132 pwm_get_state(pwm, &state);
133
134 return state.duty_cycle;
135}
136
137static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
138{
139 struct pwm_state state;
140
141 pwm_get_state(pwm, &state);
142
143 return state.polarity;
144}
145
146static inline void pwm_get_args(const struct pwm_device *pwm,
147 struct pwm_args *args)
148{
149 *args = pwm->args;
150}
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169static inline void pwm_init_state(const struct pwm_device *pwm,
170 struct pwm_state *state)
171{
172 struct pwm_args args;
173
174
175 pwm_get_state(pwm, state);
176
177
178 pwm_get_args(pwm, &args);
179
180 state->period = args.period;
181 state->polarity = args.polarity;
182 state->duty_cycle = 0;
183}
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198static inline unsigned int
199pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
200{
201 if (!state->period)
202 return 0;
203
204 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
205 state->period);
206}
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226static inline int
227pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
228 unsigned int scale)
229{
230 if (!scale || duty_cycle > scale)
231 return -EINVAL;
232
233 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
234 state->period,
235 scale);
236
237 return 0;
238}
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259struct pwm_ops {
260 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
261 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
262 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
263 int duty_ns, int period_ns);
264 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
265 enum pwm_polarity polarity);
266 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
267 struct pwm_capture *result, unsigned long timeout);
268 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
269 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
270 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
271 struct pwm_state *state);
272 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
273 struct pwm_state *state);
274#ifdef CONFIG_DEBUG_FS
275 void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
276#endif
277 struct module *owner;
278};
279
280
281
282
283
284
285
286
287
288
289
290
291struct pwm_chip {
292 struct device *dev;
293 struct list_head list;
294 const struct pwm_ops *ops;
295 int base;
296 unsigned int npwm;
297
298 struct pwm_device *pwms;
299
300 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
301 const struct of_phandle_args *args);
302 unsigned int of_pwm_n_cells;
303};
304
305
306
307
308
309
310struct pwm_capture {
311 unsigned int period;
312 unsigned int duty_cycle;
313};
314
315#if IS_ENABLED(CONFIG_PWM)
316
317struct pwm_device *pwm_request(int pwm_id, const char *label);
318void pwm_free(struct pwm_device *pwm);
319int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
320int pwm_adjust_config(struct pwm_device *pwm);
321
322
323
324
325
326
327
328
329
330static 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_state(pwm, &state);
348}
349
350
351
352
353
354
355
356
357
358
359
360static inline int pwm_set_polarity(struct pwm_device *pwm,
361 enum pwm_polarity polarity)
362{
363 struct pwm_state state;
364
365 if (!pwm)
366 return -EINVAL;
367
368 pwm_get_state(pwm, &state);
369 if (state.polarity == polarity)
370 return 0;
371
372
373
374
375
376
377
378
379 if (state.enabled)
380 return -EBUSY;
381
382 state.polarity = polarity;
383 return pwm_apply_state(pwm, &state);
384}
385
386
387
388
389
390
391
392static inline int pwm_enable(struct pwm_device *pwm)
393{
394 struct pwm_state state;
395
396 if (!pwm)
397 return -EINVAL;
398
399 pwm_get_state(pwm, &state);
400 if (state.enabled)
401 return 0;
402
403 state.enabled = true;
404 return pwm_apply_state(pwm, &state);
405}
406
407
408
409
410
411static inline void pwm_disable(struct pwm_device *pwm)
412{
413 struct pwm_state state;
414
415 if (!pwm)
416 return;
417
418 pwm_get_state(pwm, &state);
419 if (!state.enabled)
420 return;
421
422 state.enabled = false;
423 pwm_apply_state(pwm, &state);
424}
425
426
427int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
428 unsigned long timeout);
429int pwm_set_chip_data(struct pwm_device *pwm, void *data);
430void *pwm_get_chip_data(struct pwm_device *pwm);
431
432int pwmchip_add_with_polarity(struct pwm_chip *chip,
433 enum pwm_polarity polarity);
434int pwmchip_add(struct pwm_chip *chip);
435int pwmchip_remove(struct pwm_chip *chip);
436struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
437 unsigned int index,
438 const char *label);
439
440struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
441 const struct of_phandle_args *args);
442
443struct pwm_device *pwm_get(struct device *dev, const char *con_id);
444struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
445void pwm_put(struct pwm_device *pwm);
446
447struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
448struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
449 const char *con_id);
450void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
451#else
452static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
453{
454 return ERR_PTR(-ENODEV);
455}
456
457static inline void pwm_free(struct pwm_device *pwm)
458{
459}
460
461static inline int pwm_apply_state(struct pwm_device *pwm,
462 const struct pwm_state *state)
463{
464 return -ENOTSUPP;
465}
466
467static inline int pwm_adjust_config(struct pwm_device *pwm)
468{
469 return -ENOTSUPP;
470}
471
472static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
473 int period_ns)
474{
475 return -EINVAL;
476}
477
478static inline int pwm_capture(struct pwm_device *pwm,
479 struct pwm_capture *result,
480 unsigned long timeout)
481{
482 return -EINVAL;
483}
484
485static inline int pwm_set_polarity(struct pwm_device *pwm,
486 enum pwm_polarity polarity)
487{
488 return -ENOTSUPP;
489}
490
491static inline int pwm_enable(struct pwm_device *pwm)
492{
493 return -EINVAL;
494}
495
496static inline void pwm_disable(struct pwm_device *pwm)
497{
498}
499
500static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
501{
502 return -EINVAL;
503}
504
505static inline void *pwm_get_chip_data(struct pwm_device *pwm)
506{
507 return NULL;
508}
509
510static inline int pwmchip_add(struct pwm_chip *chip)
511{
512 return -EINVAL;
513}
514
515static inline int pwmchip_add_inversed(struct pwm_chip *chip)
516{
517 return -EINVAL;
518}
519
520static inline int pwmchip_remove(struct pwm_chip *chip)
521{
522 return -EINVAL;
523}
524
525static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
526 unsigned int index,
527 const char *label)
528{
529 return ERR_PTR(-ENODEV);
530}
531
532static inline struct pwm_device *pwm_get(struct device *dev,
533 const char *consumer)
534{
535 return ERR_PTR(-ENODEV);
536}
537
538static inline struct pwm_device *of_pwm_get(struct device_node *np,
539 const char *con_id)
540{
541 return ERR_PTR(-ENODEV);
542}
543
544static inline void pwm_put(struct pwm_device *pwm)
545{
546}
547
548static inline struct pwm_device *devm_pwm_get(struct device *dev,
549 const char *consumer)
550{
551 return ERR_PTR(-ENODEV);
552}
553
554static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
555 struct device_node *np,
556 const char *con_id)
557{
558 return ERR_PTR(-ENODEV);
559}
560
561static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
562{
563}
564#endif
565
566static inline void pwm_apply_args(struct pwm_device *pwm)
567{
568 struct pwm_state state = { };
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591 state.enabled = false;
592 state.polarity = pwm->args.polarity;
593 state.period = pwm->args.period;
594
595 pwm_apply_state(pwm, &state);
596}
597
598struct pwm_lookup {
599 struct list_head list;
600 const char *provider;
601 unsigned int index;
602 const char *dev_id;
603 const char *con_id;
604 unsigned int period;
605 enum pwm_polarity polarity;
606 const char *module;
607};
608
609#define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
610 _period, _polarity, _module) \
611 { \
612 .provider = _provider, \
613 .index = _index, \
614 .dev_id = _dev_id, \
615 .con_id = _con_id, \
616 .period = _period, \
617 .polarity = _polarity, \
618 .module = _module, \
619 }
620
621#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
622 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
623 _polarity, NULL)
624
625#if IS_ENABLED(CONFIG_PWM)
626void pwm_add_table(struct pwm_lookup *table, size_t num);
627void pwm_remove_table(struct pwm_lookup *table, size_t num);
628#else
629static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
630{
631}
632
633static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
634{
635}
636#endif
637
638#ifdef CONFIG_PWM_SYSFS
639void pwmchip_sysfs_export(struct pwm_chip *chip);
640void pwmchip_sysfs_unexport(struct pwm_chip *chip);
641void pwmchip_sysfs_unexport_children(struct pwm_chip *chip);
642#else
643static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
644{
645}
646
647static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
648{
649}
650
651static inline void pwmchip_sysfs_unexport_children(struct pwm_chip *chip)
652{
653}
654#endif
655
656#endif
657