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