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