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
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 (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
263 struct pwm_capture *result, unsigned long timeout);
264 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
265 const struct pwm_state *state);
266 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
267 struct pwm_state *state);
268 struct module *owner;
269
270
271 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
272 int duty_ns, int period_ns);
273 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
274 enum pwm_polarity polarity);
275 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
276 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
277};
278
279
280
281
282
283
284
285
286
287
288
289
290struct pwm_chip {
291 struct device *dev;
292 const struct pwm_ops *ops;
293 int base;
294 unsigned int npwm;
295
296 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
297 const struct of_phandle_args *args);
298 unsigned int of_pwm_n_cells;
299
300
301 struct list_head list;
302 struct pwm_device *pwms;
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, const 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
356static inline int pwm_enable(struct pwm_device *pwm)
357{
358 struct pwm_state state;
359
360 if (!pwm)
361 return -EINVAL;
362
363 pwm_get_state(pwm, &state);
364 if (state.enabled)
365 return 0;
366
367 state.enabled = true;
368 return pwm_apply_state(pwm, &state);
369}
370
371
372
373
374
375static inline void pwm_disable(struct pwm_device *pwm)
376{
377 struct pwm_state state;
378
379 if (!pwm)
380 return;
381
382 pwm_get_state(pwm, &state);
383 if (!state.enabled)
384 return;
385
386 state.enabled = false;
387 pwm_apply_state(pwm, &state);
388}
389
390
391int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
392 unsigned long timeout);
393int pwm_set_chip_data(struct pwm_device *pwm, void *data);
394void *pwm_get_chip_data(struct pwm_device *pwm);
395
396int pwmchip_add_with_polarity(struct pwm_chip *chip,
397 enum pwm_polarity polarity);
398int pwmchip_add(struct pwm_chip *chip);
399int pwmchip_remove(struct pwm_chip *chip);
400struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
401 unsigned int index,
402 const char *label);
403
404struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
405 const struct of_phandle_args *args);
406
407struct pwm_device *pwm_get(struct device *dev, const char *con_id);
408struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
409 const char *con_id);
410void pwm_put(struct pwm_device *pwm);
411
412struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
413struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
414 const char *con_id);
415struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
416 struct fwnode_handle *fwnode,
417 const char *con_id);
418void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
419#else
420static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
421{
422 return ERR_PTR(-ENODEV);
423}
424
425static inline void pwm_free(struct pwm_device *pwm)
426{
427}
428
429static inline int pwm_apply_state(struct pwm_device *pwm,
430 const struct pwm_state *state)
431{
432 return -ENOTSUPP;
433}
434
435static inline int pwm_adjust_config(struct pwm_device *pwm)
436{
437 return -ENOTSUPP;
438}
439
440static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
441 int period_ns)
442{
443 return -EINVAL;
444}
445
446static inline int pwm_capture(struct pwm_device *pwm,
447 struct pwm_capture *result,
448 unsigned long timeout)
449{
450 return -EINVAL;
451}
452
453static inline int pwm_enable(struct pwm_device *pwm)
454{
455 return -EINVAL;
456}
457
458static inline void pwm_disable(struct pwm_device *pwm)
459{
460}
461
462static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
463{
464 return -EINVAL;
465}
466
467static inline void *pwm_get_chip_data(struct pwm_device *pwm)
468{
469 return NULL;
470}
471
472static inline int pwmchip_add(struct pwm_chip *chip)
473{
474 return -EINVAL;
475}
476
477static inline int pwmchip_add_inversed(struct pwm_chip *chip)
478{
479 return -EINVAL;
480}
481
482static inline int pwmchip_remove(struct pwm_chip *chip)
483{
484 return -EINVAL;
485}
486
487static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
488 unsigned int index,
489 const char *label)
490{
491 return ERR_PTR(-ENODEV);
492}
493
494static inline struct pwm_device *pwm_get(struct device *dev,
495 const char *consumer)
496{
497 return ERR_PTR(-ENODEV);
498}
499
500static inline struct pwm_device *of_pwm_get(struct device *dev,
501 struct device_node *np,
502 const char *con_id)
503{
504 return ERR_PTR(-ENODEV);
505}
506
507static inline void pwm_put(struct pwm_device *pwm)
508{
509}
510
511static inline struct pwm_device *devm_pwm_get(struct device *dev,
512 const char *consumer)
513{
514 return ERR_PTR(-ENODEV);
515}
516
517static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
518 struct device_node *np,
519 const char *con_id)
520{
521 return ERR_PTR(-ENODEV);
522}
523
524static inline struct pwm_device *
525devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
526 const char *con_id)
527{
528 return ERR_PTR(-ENODEV);
529}
530
531static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
532{
533}
534#endif
535
536static inline void pwm_apply_args(struct pwm_device *pwm)
537{
538 struct pwm_state state = { };
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561 state.enabled = false;
562 state.polarity = pwm->args.polarity;
563 state.period = pwm->args.period;
564
565 pwm_apply_state(pwm, &state);
566}
567
568struct pwm_lookup {
569 struct list_head list;
570 const char *provider;
571 unsigned int index;
572 const char *dev_id;
573 const char *con_id;
574 unsigned int period;
575 enum pwm_polarity polarity;
576 const char *module;
577};
578
579#define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
580 _period, _polarity, _module) \
581 { \
582 .provider = _provider, \
583 .index = _index, \
584 .dev_id = _dev_id, \
585 .con_id = _con_id, \
586 .period = _period, \
587 .polarity = _polarity, \
588 .module = _module, \
589 }
590
591#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
592 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
593 _polarity, NULL)
594
595#if IS_ENABLED(CONFIG_PWM)
596void pwm_add_table(struct pwm_lookup *table, size_t num);
597void pwm_remove_table(struct pwm_lookup *table, size_t num);
598#else
599static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
600{
601}
602
603static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
604{
605}
606#endif
607
608#ifdef CONFIG_PWM_SYSFS
609void pwmchip_sysfs_export(struct pwm_chip *chip);
610void pwmchip_sysfs_unexport(struct pwm_chip *chip);
611#else
612static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
613{
614}
615
616static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
617{
618}
619#endif
620
621#endif
622