]> Git Repo - J-linux.git/blob - drivers/pwm/core.c
Merge tag '6.13-rc-part1-SMB3-client-fixes' of git://git.samba.org/sfrench/cifs-2.6
[J-linux.git] / drivers / pwm / core.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Generic pwmlib implementation
4  *
5  * Copyright (C) 2011 Sascha Hauer <[email protected]>
6  * Copyright (C) 2011-2012 Avionic Design GmbH
7  */
8
9 #define DEFAULT_SYMBOL_NAMESPACE PWM
10
11 #include <linux/acpi.h>
12 #include <linux/module.h>
13 #include <linux/idr.h>
14 #include <linux/of.h>
15 #include <linux/pwm.h>
16 #include <linux/list.h>
17 #include <linux/mutex.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/device.h>
21 #include <linux/debugfs.h>
22 #include <linux/seq_file.h>
23
24 #include <dt-bindings/pwm/pwm.h>
25
26 #define CREATE_TRACE_POINTS
27 #include <trace/events/pwm.h>
28
29 /* protects access to pwm_chips */
30 static DEFINE_MUTEX(pwm_lock);
31
32 static DEFINE_IDR(pwm_chips);
33
34 static void pwmchip_lock(struct pwm_chip *chip)
35 {
36         if (chip->atomic)
37                 spin_lock(&chip->atomic_lock);
38         else
39                 mutex_lock(&chip->nonatomic_lock);
40 }
41
42 static void pwmchip_unlock(struct pwm_chip *chip)
43 {
44         if (chip->atomic)
45                 spin_unlock(&chip->atomic_lock);
46         else
47                 mutex_unlock(&chip->nonatomic_lock);
48 }
49
50 DEFINE_GUARD(pwmchip, struct pwm_chip *, pwmchip_lock(_T), pwmchip_unlock(_T))
51
52 static bool pwm_wf_valid(const struct pwm_waveform *wf)
53 {
54         /*
55          * For now restrict waveforms to period_length_ns <= S64_MAX to provide
56          * some space for future extensions. One possibility is to simplify
57          * representing waveforms with inverted polarity using negative values
58          * somehow.
59          */
60         if (wf->period_length_ns > S64_MAX)
61                 return false;
62
63         if (wf->duty_length_ns > wf->period_length_ns)
64                 return false;
65
66         /*
67          * .duty_offset_ns is supposed to be smaller than .period_length_ns, apart
68          * from the corner case .duty_offset_ns == 0 && .period_length_ns == 0.
69          */
70         if (wf->duty_offset_ns && wf->duty_offset_ns >= wf->period_length_ns)
71                 return false;
72
73         return true;
74 }
75
76 static void pwm_wf2state(const struct pwm_waveform *wf, struct pwm_state *state)
77 {
78         if (wf->period_length_ns) {
79                 if (wf->duty_length_ns + wf->duty_offset_ns < wf->period_length_ns)
80                         *state = (struct pwm_state){
81                                 .enabled = true,
82                                 .polarity = PWM_POLARITY_NORMAL,
83                                 .period = wf->period_length_ns,
84                                 .duty_cycle = wf->duty_length_ns,
85                         };
86                 else
87                         *state = (struct pwm_state){
88                                 .enabled = true,
89                                 .polarity = PWM_POLARITY_INVERSED,
90                                 .period = wf->period_length_ns,
91                                 .duty_cycle = wf->period_length_ns - wf->duty_length_ns,
92                         };
93         } else {
94                 *state = (struct pwm_state){
95                         .enabled = false,
96                 };
97         }
98 }
99
100 static void pwm_state2wf(const struct pwm_state *state, struct pwm_waveform *wf)
101 {
102         if (state->enabled) {
103                 if (state->polarity == PWM_POLARITY_NORMAL)
104                         *wf = (struct pwm_waveform){
105                                 .period_length_ns = state->period,
106                                 .duty_length_ns = state->duty_cycle,
107                                 .duty_offset_ns = 0,
108                         };
109                 else
110                         *wf = (struct pwm_waveform){
111                                 .period_length_ns = state->period,
112                                 .duty_length_ns = state->period - state->duty_cycle,
113                                 .duty_offset_ns = state->duty_cycle,
114                         };
115         } else {
116                 *wf = (struct pwm_waveform){
117                         .period_length_ns = 0,
118                 };
119         }
120 }
121
122 static int pwmwfcmp(const struct pwm_waveform *a, const struct pwm_waveform *b)
123 {
124         if (a->period_length_ns > b->period_length_ns)
125                 return 1;
126
127         if (a->period_length_ns < b->period_length_ns)
128                 return -1;
129
130         if (a->duty_length_ns > b->duty_length_ns)
131                 return 1;
132
133         if (a->duty_length_ns < b->duty_length_ns)
134                 return -1;
135
136         if (a->duty_offset_ns > b->duty_offset_ns)
137                 return 1;
138
139         if (a->duty_offset_ns < b->duty_offset_ns)
140                 return -1;
141
142         return 0;
143 }
144
145 static bool pwm_check_rounding(const struct pwm_waveform *wf,
146                                const struct pwm_waveform *wf_rounded)
147 {
148         if (!wf->period_length_ns)
149                 return true;
150
151         if (wf->period_length_ns < wf_rounded->period_length_ns)
152                 return false;
153
154         if (wf->duty_length_ns < wf_rounded->duty_length_ns)
155                 return false;
156
157         if (wf->duty_offset_ns < wf_rounded->duty_offset_ns)
158                 return false;
159
160         return true;
161 }
162
163 static int __pwm_round_waveform_tohw(struct pwm_chip *chip, struct pwm_device *pwm,
164                                      const struct pwm_waveform *wf, void *wfhw)
165 {
166         const struct pwm_ops *ops = chip->ops;
167         int ret;
168
169         ret = ops->round_waveform_tohw(chip, pwm, wf, wfhw);
170         trace_pwm_round_waveform_tohw(pwm, wf, wfhw, ret);
171
172         return ret;
173 }
174
175 static int __pwm_round_waveform_fromhw(struct pwm_chip *chip, struct pwm_device *pwm,
176                                        const void *wfhw, struct pwm_waveform *wf)
177 {
178         const struct pwm_ops *ops = chip->ops;
179         int ret;
180
181         ret = ops->round_waveform_fromhw(chip, pwm, wfhw, wf);
182         trace_pwm_round_waveform_fromhw(pwm, wfhw, wf, ret);
183
184         return ret;
185 }
186
187 static int __pwm_read_waveform(struct pwm_chip *chip, struct pwm_device *pwm, void *wfhw)
188 {
189         const struct pwm_ops *ops = chip->ops;
190         int ret;
191
192         ret = ops->read_waveform(chip, pwm, wfhw);
193         trace_pwm_read_waveform(pwm, wfhw, ret);
194
195         return ret;
196 }
197
198 static int __pwm_write_waveform(struct pwm_chip *chip, struct pwm_device *pwm, const void *wfhw)
199 {
200         const struct pwm_ops *ops = chip->ops;
201         int ret;
202
203         ret = ops->write_waveform(chip, pwm, wfhw);
204         trace_pwm_write_waveform(pwm, wfhw, ret);
205
206         return ret;
207 }
208
209 #define WFHWSIZE 20
210
211 /**
212  * pwm_round_waveform_might_sleep - Query hardware capabilities
213  * Cannot be used in atomic context.
214  * @pwm: PWM device
215  * @wf: waveform to round and output parameter
216  *
217  * Typically a given waveform cannot be implemented exactly by hardware, e.g.
218  * because hardware only supports coarse period resolution or no duty_offset.
219  * This function returns the actually implemented waveform if you pass wf to
220  * pwm_set_waveform_might_sleep now.
221  *
222  * Note however that the world doesn't stop turning when you call it, so when
223  * doing
224  *
225  *      pwm_round_waveform_might_sleep(mypwm, &wf);
226  *      pwm_set_waveform_might_sleep(mypwm, &wf, true);
227  *
228  * the latter might fail, e.g. because an input clock changed its rate between
229  * these two calls and the waveform determined by
230  * pwm_round_waveform_might_sleep() cannot be implemented any more.
231  *
232  * Returns 0 on success, 1 if there is no valid hardware configuration matching
233  * the input waveform under the PWM rounding rules or a negative errno.
234  */
235 int pwm_round_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf)
236 {
237         struct pwm_chip *chip = pwm->chip;
238         const struct pwm_ops *ops = chip->ops;
239         struct pwm_waveform wf_req = *wf;
240         char wfhw[WFHWSIZE];
241         int ret_tohw, ret_fromhw;
242
243         BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
244
245         if (!pwm_wf_valid(wf))
246                 return -EINVAL;
247
248         guard(pwmchip)(chip);
249
250         if (!chip->operational)
251                 return -ENODEV;
252
253         ret_tohw = __pwm_round_waveform_tohw(chip, pwm, wf, wfhw);
254         if (ret_tohw < 0)
255                 return ret_tohw;
256
257         if (IS_ENABLED(CONFIG_PWM_DEBUG) && ret_tohw > 1)
258                 dev_err(&chip->dev, "Unexpected return value from __pwm_round_waveform_tohw: requested %llu/%llu [+%llu], return value %d\n",
259                         wf_req.duty_length_ns, wf_req.period_length_ns, wf_req.duty_offset_ns, ret_tohw);
260
261         ret_fromhw = __pwm_round_waveform_fromhw(chip, pwm, wfhw, wf);
262         if (ret_fromhw < 0)
263                 return ret_fromhw;
264
265         if (IS_ENABLED(CONFIG_PWM_DEBUG) && ret_fromhw > 0)
266                 dev_err(&chip->dev, "Unexpected return value from __pwm_round_waveform_fromhw: requested %llu/%llu [+%llu], return value %d\n",
267                         wf_req.duty_length_ns, wf_req.period_length_ns, wf_req.duty_offset_ns, ret_tohw);
268
269         if (IS_ENABLED(CONFIG_PWM_DEBUG) &&
270             ret_tohw == 0 && !pwm_check_rounding(&wf_req, wf))
271                 dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
272                         wf_req.duty_length_ns, wf_req.period_length_ns, wf_req.duty_offset_ns,
273                         wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns);
274
275         return ret_tohw;
276 }
277 EXPORT_SYMBOL_GPL(pwm_round_waveform_might_sleep);
278
279 /**
280  * pwm_get_waveform_might_sleep - Query hardware about current configuration
281  * Cannot be used in atomic context.
282  * @pwm: PWM device
283  * @wf: output parameter
284  *
285  * Stores the current configuration of the PWM in @wf. Note this is the
286  * equivalent of pwm_get_state_hw() (and not pwm_get_state()) for pwm_waveform.
287  */
288 int pwm_get_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf)
289 {
290         struct pwm_chip *chip = pwm->chip;
291         const struct pwm_ops *ops = chip->ops;
292         char wfhw[WFHWSIZE];
293         int err;
294
295         BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
296
297         guard(pwmchip)(chip);
298
299         if (!chip->operational)
300                 return -ENODEV;
301
302         err = __pwm_read_waveform(chip, pwm, &wfhw);
303         if (err)
304                 return err;
305
306         return __pwm_round_waveform_fromhw(chip, pwm, &wfhw, wf);
307 }
308 EXPORT_SYMBOL_GPL(pwm_get_waveform_might_sleep);
309
310 /* Called with the pwmchip lock held */
311 static int __pwm_set_waveform(struct pwm_device *pwm,
312                               const struct pwm_waveform *wf,
313                               bool exact)
314 {
315         struct pwm_chip *chip = pwm->chip;
316         const struct pwm_ops *ops = chip->ops;
317         char wfhw[WFHWSIZE];
318         struct pwm_waveform wf_rounded;
319         int err;
320
321         BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
322
323         if (!pwm_wf_valid(wf))
324                 return -EINVAL;
325
326         err = __pwm_round_waveform_tohw(chip, pwm, wf, &wfhw);
327         if (err)
328                 return err;
329
330         if ((IS_ENABLED(CONFIG_PWM_DEBUG) || exact) && wf->period_length_ns) {
331                 err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
332                 if (err)
333                         return err;
334
335                 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !pwm_check_rounding(wf, &wf_rounded))
336                         dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
337                                 wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns,
338                                 wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns);
339
340                 if (exact && pwmwfcmp(wf, &wf_rounded)) {
341                         dev_dbg(&chip->dev, "Requested no rounding, but %llu/%llu [+%llu] -> %llu/%llu [+%llu]\n",
342                                 wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns,
343                                 wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns);
344
345                         return 1;
346                 }
347         }
348
349         err = __pwm_write_waveform(chip, pwm, &wfhw);
350         if (err)
351                 return err;
352
353         /* update .state */
354         pwm_wf2state(wf, &pwm->state);
355
356         if (IS_ENABLED(CONFIG_PWM_DEBUG) && ops->read_waveform && wf->period_length_ns) {
357                 struct pwm_waveform wf_set;
358
359                 err = __pwm_read_waveform(chip, pwm, &wfhw);
360                 if (err)
361                         /* maybe ignore? */
362                         return err;
363
364                 err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_set);
365                 if (err)
366                         /* maybe ignore? */
367                         return err;
368
369                 if (pwmwfcmp(&wf_set, &wf_rounded) != 0)
370                         dev_err(&chip->dev,
371                                 "Unexpected setting: requested %llu/%llu [+%llu], expected %llu/%llu [+%llu], set %llu/%llu [+%llu]\n",
372                                 wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns,
373                                 wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns,
374                                 wf_set.duty_length_ns, wf_set.period_length_ns, wf_set.duty_offset_ns);
375         }
376         return 0;
377 }
378
379 /**
380  * pwm_set_waveform_might_sleep - Apply a new waveform
381  * Cannot be used in atomic context.
382  * @pwm: PWM device
383  * @wf: The waveform to apply
384  * @exact: If true no rounding is allowed
385  *
386  * Typically a requested waveform cannot be implemented exactly, e.g. because
387  * you requested .period_length_ns = 100 ns, but the hardware can only set
388  * periods that are a multiple of 8.5 ns. With that hardware passing exact =
389  * true results in pwm_set_waveform_might_sleep() failing and returning 1. If
390  * exact = false you get a period of 93.5 ns (i.e. the biggest period not bigger
391  * than the requested value).
392  * Note that even with exact = true, some rounding by less than 1 is
393  * possible/needed. In the above example requesting .period_length_ns = 94 and
394  * exact = true, you get the hardware configured with period = 93.5 ns.
395  */
396 int pwm_set_waveform_might_sleep(struct pwm_device *pwm,
397                                  const struct pwm_waveform *wf, bool exact)
398 {
399         struct pwm_chip *chip = pwm->chip;
400         int err;
401
402         might_sleep();
403
404         guard(pwmchip)(chip);
405
406         if (!chip->operational)
407                 return -ENODEV;
408
409         if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) {
410                 /*
411                  * Catch any drivers that have been marked as atomic but
412                  * that will sleep anyway.
413                  */
414                 non_block_start();
415                 err = __pwm_set_waveform(pwm, wf, exact);
416                 non_block_end();
417         } else {
418                 err = __pwm_set_waveform(pwm, wf, exact);
419         }
420
421         return err;
422 }
423 EXPORT_SYMBOL_GPL(pwm_set_waveform_might_sleep);
424
425 static void pwm_apply_debug(struct pwm_device *pwm,
426                             const struct pwm_state *state)
427 {
428         struct pwm_state *last = &pwm->last;
429         struct pwm_chip *chip = pwm->chip;
430         struct pwm_state s1 = { 0 }, s2 = { 0 };
431         int err;
432
433         if (!IS_ENABLED(CONFIG_PWM_DEBUG))
434                 return;
435
436         /* No reasonable diagnosis possible without .get_state() */
437         if (!chip->ops->get_state)
438                 return;
439
440         /*
441          * *state was just applied. Read out the hardware state and do some
442          * checks.
443          */
444
445         err = chip->ops->get_state(chip, pwm, &s1);
446         trace_pwm_get(pwm, &s1, err);
447         if (err)
448                 /* If that failed there isn't much to debug */
449                 return;
450
451         /*
452          * The lowlevel driver either ignored .polarity (which is a bug) or as
453          * best effort inverted .polarity and fixed .duty_cycle respectively.
454          * Undo this inversion and fixup for further tests.
455          */
456         if (s1.enabled && s1.polarity != state->polarity) {
457                 s2.polarity = state->polarity;
458                 s2.duty_cycle = s1.period - s1.duty_cycle;
459                 s2.period = s1.period;
460                 s2.enabled = s1.enabled;
461         } else {
462                 s2 = s1;
463         }
464
465         if (s2.polarity != state->polarity &&
466             state->duty_cycle < state->period)
467                 dev_warn(pwmchip_parent(chip), ".apply ignored .polarity\n");
468
469         if (state->enabled && s2.enabled &&
470             last->polarity == state->polarity &&
471             last->period > s2.period &&
472             last->period <= state->period)
473                 dev_warn(pwmchip_parent(chip),
474                          ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
475                          state->period, s2.period, last->period);
476
477         /*
478          * Rounding period up is fine only if duty_cycle is 0 then, because a
479          * flat line doesn't have a characteristic period.
480          */
481         if (state->enabled && s2.enabled && state->period < s2.period && s2.duty_cycle)
482                 dev_warn(pwmchip_parent(chip),
483                          ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
484                          state->period, s2.period);
485
486         if (state->enabled &&
487             last->polarity == state->polarity &&
488             last->period == s2.period &&
489             last->duty_cycle > s2.duty_cycle &&
490             last->duty_cycle <= state->duty_cycle)
491                 dev_warn(pwmchip_parent(chip),
492                          ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
493                          state->duty_cycle, state->period,
494                          s2.duty_cycle, s2.period,
495                          last->duty_cycle, last->period);
496
497         if (state->enabled && s2.enabled && state->duty_cycle < s2.duty_cycle)
498                 dev_warn(pwmchip_parent(chip),
499                          ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
500                          state->duty_cycle, state->period,
501                          s2.duty_cycle, s2.period);
502
503         if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
504                 dev_warn(pwmchip_parent(chip),
505                          "requested disabled, but yielded enabled with duty > 0\n");
506
507         /* reapply the state that the driver reported being configured. */
508         err = chip->ops->apply(chip, pwm, &s1);
509         trace_pwm_apply(pwm, &s1, err);
510         if (err) {
511                 *last = s1;
512                 dev_err(pwmchip_parent(chip), "failed to reapply current setting\n");
513                 return;
514         }
515
516         *last = (struct pwm_state){ 0 };
517         err = chip->ops->get_state(chip, pwm, last);
518         trace_pwm_get(pwm, last, err);
519         if (err)
520                 return;
521
522         /* reapplication of the current state should give an exact match */
523         if (s1.enabled != last->enabled ||
524             s1.polarity != last->polarity ||
525             (s1.enabled && s1.period != last->period) ||
526             (s1.enabled && s1.duty_cycle != last->duty_cycle)) {
527                 dev_err(pwmchip_parent(chip),
528                         ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
529                         s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
530                         last->enabled, last->polarity, last->duty_cycle,
531                         last->period);
532         }
533 }
534
535 static bool pwm_state_valid(const struct pwm_state *state)
536 {
537         /*
538          * For a disabled state all other state description is irrelevant and
539          * and supposed to be ignored. So also ignore any strange values and
540          * consider the state ok.
541          */
542         if (state->enabled)
543                 return true;
544
545         if (!state->period)
546                 return false;
547
548         if (state->duty_cycle > state->period)
549                 return false;
550
551         return true;
552 }
553
554 /**
555  * __pwm_apply() - atomically apply a new state to a PWM device
556  * @pwm: PWM device
557  * @state: new state to apply
558  */
559 static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
560 {
561         struct pwm_chip *chip;
562         const struct pwm_ops *ops;
563         int err;
564
565         if (!pwm || !state)
566                 return -EINVAL;
567
568         if (!pwm_state_valid(state)) {
569                 /*
570                  * Allow to transition from one invalid state to another.
571                  * This ensures that you can e.g. change the polarity while
572                  * the period is zero. (This happens on stm32 when the hardware
573                  * is in its poweron default state.) This greatly simplifies
574                  * working with the sysfs API where you can only change one
575                  * parameter at a time.
576                  */
577                 if (!pwm_state_valid(&pwm->state)) {
578                         pwm->state = *state;
579                         return 0;
580                 }
581
582                 return -EINVAL;
583         }
584
585         chip = pwm->chip;
586         ops = chip->ops;
587
588         if (state->period == pwm->state.period &&
589             state->duty_cycle == pwm->state.duty_cycle &&
590             state->polarity == pwm->state.polarity &&
591             state->enabled == pwm->state.enabled &&
592             state->usage_power == pwm->state.usage_power)
593                 return 0;
594
595         if (ops->write_waveform) {
596                 struct pwm_waveform wf;
597                 char wfhw[WFHWSIZE];
598
599                 BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
600
601                 pwm_state2wf(state, &wf);
602
603                 /*
604                  * The rounding is wrong here for states with inverted polarity.
605                  * While .apply() rounds down duty_cycle (which represents the
606                  * time from the start of the period to the inner edge),
607                  * .round_waveform_tohw() rounds down the time the PWM is high.
608                  * Can be fixed if the need arises, until reported otherwise
609                  * let's assume that consumers don't care.
610                  */
611
612                 err = __pwm_round_waveform_tohw(chip, pwm, &wf, &wfhw);
613                 if (err) {
614                         if (err > 0)
615                                 /*
616                                  * This signals an invalid request, typically
617                                  * the requested period (or duty_offset) is
618                                  * smaller than possible with the hardware.
619                                  */
620                                 return -EINVAL;
621
622                         return err;
623                 }
624
625                 if (IS_ENABLED(CONFIG_PWM_DEBUG)) {
626                         struct pwm_waveform wf_rounded;
627
628                         err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
629                         if (err)
630                                 return err;
631
632                         if (!pwm_check_rounding(&wf, &wf_rounded))
633                                 dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
634                                         wf.duty_length_ns, wf.period_length_ns, wf.duty_offset_ns,
635                                         wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns);
636                 }
637
638                 err = __pwm_write_waveform(chip, pwm, &wfhw);
639                 if (err)
640                         return err;
641
642                 pwm->state = *state;
643
644         } else {
645                 err = ops->apply(chip, pwm, state);
646                 trace_pwm_apply(pwm, state, err);
647                 if (err)
648                         return err;
649
650                 pwm->state = *state;
651
652                 /*
653                  * only do this after pwm->state was applied as some
654                  * implementations of .get_state() depend on this
655                  */
656                 pwm_apply_debug(pwm, state);
657         }
658
659         return 0;
660 }
661
662 /**
663  * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
664  * Cannot be used in atomic context.
665  * @pwm: PWM device
666  * @state: new state to apply
667  */
668 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
669 {
670         int err;
671         struct pwm_chip *chip = pwm->chip;
672
673         /*
674          * Some lowlevel driver's implementations of .apply() make use of
675          * mutexes, also with some drivers only returning when the new
676          * configuration is active calling pwm_apply_might_sleep() from atomic context
677          * is a bad idea. So make it explicit that calling this function might
678          * sleep.
679          */
680         might_sleep();
681
682         guard(pwmchip)(chip);
683
684         if (!chip->operational)
685                 return -ENODEV;
686
687         if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) {
688                 /*
689                  * Catch any drivers that have been marked as atomic but
690                  * that will sleep anyway.
691                  */
692                 non_block_start();
693                 err = __pwm_apply(pwm, state);
694                 non_block_end();
695         } else {
696                 err = __pwm_apply(pwm, state);
697         }
698
699         return err;
700 }
701 EXPORT_SYMBOL_GPL(pwm_apply_might_sleep);
702
703 /**
704  * pwm_apply_atomic() - apply a new state to a PWM device from atomic context
705  * Not all PWM devices support this function, check with pwm_might_sleep().
706  * @pwm: PWM device
707  * @state: new state to apply
708  */
709 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state)
710 {
711         struct pwm_chip *chip = pwm->chip;
712
713         WARN_ONCE(!chip->atomic,
714                   "sleeping PWM driver used in atomic context\n");
715
716         guard(pwmchip)(chip);
717
718         if (!chip->operational)
719                 return -ENODEV;
720
721         return __pwm_apply(pwm, state);
722 }
723 EXPORT_SYMBOL_GPL(pwm_apply_atomic);
724
725 /**
726  * pwm_get_state_hw() - get the current PWM state from hardware
727  * @pwm: PWM device
728  * @state: state to fill with the current PWM state
729  *
730  * Similar to pwm_get_state() but reads the current PWM state from hardware
731  * instead of the requested state.
732  *
733  * Returns: 0 on success or a negative error code on failure.
734  * Context: May sleep.
735  */
736 int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state)
737 {
738         struct pwm_chip *chip = pwm->chip;
739         const struct pwm_ops *ops = chip->ops;
740         int ret = -EOPNOTSUPP;
741
742         might_sleep();
743
744         guard(pwmchip)(chip);
745
746         if (!chip->operational)
747                 return -ENODEV;
748
749         if (ops->read_waveform) {
750                 char wfhw[WFHWSIZE];
751                 struct pwm_waveform wf;
752
753                 BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
754
755                 ret = __pwm_read_waveform(chip, pwm, &wfhw);
756                 if (ret)
757                         return ret;
758
759                 ret = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf);
760                 if (ret)
761                         return ret;
762
763                 pwm_wf2state(&wf, state);
764
765         } else if (ops->get_state) {
766                 ret = ops->get_state(chip, pwm, state);
767                 trace_pwm_get(pwm, state, ret);
768         }
769
770         return ret;
771 }
772 EXPORT_SYMBOL_GPL(pwm_get_state_hw);
773
774 /**
775  * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
776  * @pwm: PWM device
777  *
778  * This function will adjust the PWM config to the PWM arguments provided
779  * by the DT or PWM lookup table. This is particularly useful to adapt
780  * the bootloader config to the Linux one.
781  */
782 int pwm_adjust_config(struct pwm_device *pwm)
783 {
784         struct pwm_state state;
785         struct pwm_args pargs;
786
787         pwm_get_args(pwm, &pargs);
788         pwm_get_state(pwm, &state);
789
790         /*
791          * If the current period is zero it means that either the PWM driver
792          * does not support initial state retrieval or the PWM has not yet
793          * been configured.
794          *
795          * In either case, we setup the new period and polarity, and assign a
796          * duty cycle of 0.
797          */
798         if (!state.period) {
799                 state.duty_cycle = 0;
800                 state.period = pargs.period;
801                 state.polarity = pargs.polarity;
802
803                 return pwm_apply_might_sleep(pwm, &state);
804         }
805
806         /*
807          * Adjust the PWM duty cycle/period based on the period value provided
808          * in PWM args.
809          */
810         if (pargs.period != state.period) {
811                 u64 dutycycle = (u64)state.duty_cycle * pargs.period;
812
813                 do_div(dutycycle, state.period);
814                 state.duty_cycle = dutycycle;
815                 state.period = pargs.period;
816         }
817
818         /*
819          * If the polarity changed, we should also change the duty cycle.
820          */
821         if (pargs.polarity != state.polarity) {
822                 state.polarity = pargs.polarity;
823                 state.duty_cycle = state.period - state.duty_cycle;
824         }
825
826         return pwm_apply_might_sleep(pwm, &state);
827 }
828 EXPORT_SYMBOL_GPL(pwm_adjust_config);
829
830 /**
831  * pwm_capture() - capture and report a PWM signal
832  * @pwm: PWM device
833  * @result: structure to fill with capture result
834  * @timeout: time to wait, in milliseconds, before giving up on capture
835  *
836  * Returns: 0 on success or a negative error code on failure.
837  */
838 static int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
839                        unsigned long timeout)
840 {
841         struct pwm_chip *chip = pwm->chip;
842         const struct pwm_ops *ops = chip->ops;
843
844         if (!ops->capture)
845                 return -ENOSYS;
846
847         /*
848          * Holding the pwm_lock is probably not needed. If you use pwm_capture()
849          * and you're interested to speed it up, please convince yourself it's
850          * really not needed, test and then suggest a patch on the mailing list.
851          */
852         guard(mutex)(&pwm_lock);
853
854         guard(pwmchip)(chip);
855
856         if (!chip->operational)
857                 return -ENODEV;
858
859         return ops->capture(chip, pwm, result, timeout);
860 }
861
862 static struct pwm_chip *pwmchip_find_by_name(const char *name)
863 {
864         struct pwm_chip *chip;
865         unsigned long id, tmp;
866
867         if (!name)
868                 return NULL;
869
870         guard(mutex)(&pwm_lock);
871
872         idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) {
873                 if (device_match_name(pwmchip_parent(chip), name))
874                         return chip;
875         }
876
877         return NULL;
878 }
879
880 static int pwm_device_request(struct pwm_device *pwm, const char *label)
881 {
882         int err;
883         struct pwm_chip *chip = pwm->chip;
884         const struct pwm_ops *ops = chip->ops;
885
886         if (test_bit(PWMF_REQUESTED, &pwm->flags))
887                 return -EBUSY;
888
889         /*
890          * This function is called while holding pwm_lock. As .operational only
891          * changes while holding this lock, checking it here without holding the
892          * chip lock is fine.
893          */
894         if (!chip->operational)
895                 return -ENODEV;
896
897         if (!try_module_get(chip->owner))
898                 return -ENODEV;
899
900         if (!get_device(&chip->dev)) {
901                 err = -ENODEV;
902                 goto err_get_device;
903         }
904
905         if (ops->request) {
906                 err = ops->request(chip, pwm);
907                 if (err) {
908                         put_device(&chip->dev);
909 err_get_device:
910                         module_put(chip->owner);
911                         return err;
912                 }
913         }
914
915         if (ops->read_waveform || ops->get_state) {
916                 /*
917                  * Zero-initialize state because most drivers are unaware of
918                  * .usage_power. The other members of state are supposed to be
919                  * set by lowlevel drivers. We still initialize the whole
920                  * structure for simplicity even though this might paper over
921                  * faulty implementations of .get_state().
922                  */
923                 struct pwm_state state = { 0, };
924
925                 err = pwm_get_state_hw(pwm, &state);
926                 if (!err)
927                         pwm->state = state;
928
929                 if (IS_ENABLED(CONFIG_PWM_DEBUG))
930                         pwm->last = pwm->state;
931         }
932
933         set_bit(PWMF_REQUESTED, &pwm->flags);
934         pwm->label = label;
935
936         return 0;
937 }
938
939 /**
940  * pwm_request_from_chip() - request a PWM device relative to a PWM chip
941  * @chip: PWM chip
942  * @index: per-chip index of the PWM to request
943  * @label: a literal description string of this PWM
944  *
945  * Returns: A pointer to the PWM device at the given index of the given PWM
946  * chip. A negative error code is returned if the index is not valid for the
947  * specified PWM chip or if the PWM device cannot be requested.
948  */
949 static struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
950                                                 unsigned int index,
951                                                 const char *label)
952 {
953         struct pwm_device *pwm;
954         int err;
955
956         if (!chip || index >= chip->npwm)
957                 return ERR_PTR(-EINVAL);
958
959         guard(mutex)(&pwm_lock);
960
961         pwm = &chip->pwms[index];
962
963         err = pwm_device_request(pwm, label);
964         if (err < 0)
965                 return ERR_PTR(err);
966
967         return pwm;
968 }
969
970 struct pwm_device *
971 of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args)
972 {
973         struct pwm_device *pwm;
974
975         /* period in the second cell and flags in the third cell are optional */
976         if (args->args_count < 1)
977                 return ERR_PTR(-EINVAL);
978
979         pwm = pwm_request_from_chip(chip, args->args[0], NULL);
980         if (IS_ERR(pwm))
981                 return pwm;
982
983         if (args->args_count > 1)
984                 pwm->args.period = args->args[1];
985
986         pwm->args.polarity = PWM_POLARITY_NORMAL;
987         if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
988                 pwm->args.polarity = PWM_POLARITY_INVERSED;
989
990         return pwm;
991 }
992 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
993
994 struct pwm_device *
995 of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args)
996 {
997         struct pwm_device *pwm;
998
999         pwm = pwm_request_from_chip(chip, 0, NULL);
1000         if (IS_ERR(pwm))
1001                 return pwm;
1002
1003         if (args->args_count > 0)
1004                 pwm->args.period = args->args[0];
1005
1006         pwm->args.polarity = PWM_POLARITY_NORMAL;
1007         if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED)
1008                 pwm->args.polarity = PWM_POLARITY_INVERSED;
1009
1010         return pwm;
1011 }
1012 EXPORT_SYMBOL_GPL(of_pwm_single_xlate);
1013
1014 struct pwm_export {
1015         struct device pwm_dev;
1016         struct pwm_device *pwm;
1017         struct mutex lock;
1018         struct pwm_state suspend;
1019 };
1020
1021 static inline struct pwm_chip *pwmchip_from_dev(struct device *pwmchip_dev)
1022 {
1023         return container_of(pwmchip_dev, struct pwm_chip, dev);
1024 }
1025
1026 static inline struct pwm_export *pwmexport_from_dev(struct device *pwm_dev)
1027 {
1028         return container_of(pwm_dev, struct pwm_export, pwm_dev);
1029 }
1030
1031 static inline struct pwm_device *pwm_from_dev(struct device *pwm_dev)
1032 {
1033         struct pwm_export *export = pwmexport_from_dev(pwm_dev);
1034
1035         return export->pwm;
1036 }
1037
1038 static ssize_t period_show(struct device *pwm_dev,
1039                            struct device_attribute *attr,
1040                            char *buf)
1041 {
1042         const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
1043         struct pwm_state state;
1044
1045         pwm_get_state(pwm, &state);
1046
1047         return sysfs_emit(buf, "%llu\n", state.period);
1048 }
1049
1050 static ssize_t period_store(struct device *pwm_dev,
1051                             struct device_attribute *attr,
1052                             const char *buf, size_t size)
1053 {
1054         struct pwm_export *export = pwmexport_from_dev(pwm_dev);
1055         struct pwm_device *pwm = export->pwm;
1056         struct pwm_state state;
1057         u64 val;
1058         int ret;
1059
1060         ret = kstrtou64(buf, 0, &val);
1061         if (ret)
1062                 return ret;
1063
1064         guard(mutex)(&export->lock);
1065
1066         pwm_get_state(pwm, &state);
1067         state.period = val;
1068         ret = pwm_apply_might_sleep(pwm, &state);
1069
1070         return ret ? : size;
1071 }
1072
1073 static ssize_t duty_cycle_show(struct device *pwm_dev,
1074                                struct device_attribute *attr,
1075                                char *buf)
1076 {
1077         const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
1078         struct pwm_state state;
1079
1080         pwm_get_state(pwm, &state);
1081
1082         return sysfs_emit(buf, "%llu\n", state.duty_cycle);
1083 }
1084
1085 static ssize_t duty_cycle_store(struct device *pwm_dev,
1086                                 struct device_attribute *attr,
1087                                 const char *buf, size_t size)
1088 {
1089         struct pwm_export *export = pwmexport_from_dev(pwm_dev);
1090         struct pwm_device *pwm = export->pwm;
1091         struct pwm_state state;
1092         u64 val;
1093         int ret;
1094
1095         ret = kstrtou64(buf, 0, &val);
1096         if (ret)
1097                 return ret;
1098
1099         guard(mutex)(&export->lock);
1100
1101         pwm_get_state(pwm, &state);
1102         state.duty_cycle = val;
1103         ret = pwm_apply_might_sleep(pwm, &state);
1104
1105         return ret ? : size;
1106 }
1107
1108 static ssize_t enable_show(struct device *pwm_dev,
1109                            struct device_attribute *attr,
1110                            char *buf)
1111 {
1112         const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
1113         struct pwm_state state;
1114
1115         pwm_get_state(pwm, &state);
1116
1117         return sysfs_emit(buf, "%d\n", state.enabled);
1118 }
1119
1120 static ssize_t enable_store(struct device *pwm_dev,
1121                             struct device_attribute *attr,
1122                             const char *buf, size_t size)
1123 {
1124         struct pwm_export *export = pwmexport_from_dev(pwm_dev);
1125         struct pwm_device *pwm = export->pwm;
1126         struct pwm_state state;
1127         int val, ret;
1128
1129         ret = kstrtoint(buf, 0, &val);
1130         if (ret)
1131                 return ret;
1132
1133         guard(mutex)(&export->lock);
1134
1135         pwm_get_state(pwm, &state);
1136
1137         switch (val) {
1138         case 0:
1139                 state.enabled = false;
1140                 break;
1141         case 1:
1142                 state.enabled = true;
1143                 break;
1144         default:
1145                 return -EINVAL;
1146         }
1147
1148         ret = pwm_apply_might_sleep(pwm, &state);
1149
1150         return ret ? : size;
1151 }
1152
1153 static ssize_t polarity_show(struct device *pwm_dev,
1154                              struct device_attribute *attr,
1155                              char *buf)
1156 {
1157         const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
1158         const char *polarity = "unknown";
1159         struct pwm_state state;
1160
1161         pwm_get_state(pwm, &state);
1162
1163         switch (state.polarity) {
1164         case PWM_POLARITY_NORMAL:
1165                 polarity = "normal";
1166                 break;
1167
1168         case PWM_POLARITY_INVERSED:
1169                 polarity = "inversed";
1170                 break;
1171         }
1172
1173         return sysfs_emit(buf, "%s\n", polarity);
1174 }
1175
1176 static ssize_t polarity_store(struct device *pwm_dev,
1177                               struct device_attribute *attr,
1178                               const char *buf, size_t size)
1179 {
1180         struct pwm_export *export = pwmexport_from_dev(pwm_dev);
1181         struct pwm_device *pwm = export->pwm;
1182         enum pwm_polarity polarity;
1183         struct pwm_state state;
1184         int ret;
1185
1186         if (sysfs_streq(buf, "normal"))
1187                 polarity = PWM_POLARITY_NORMAL;
1188         else if (sysfs_streq(buf, "inversed"))
1189                 polarity = PWM_POLARITY_INVERSED;
1190         else
1191                 return -EINVAL;
1192
1193         guard(mutex)(&export->lock);
1194
1195         pwm_get_state(pwm, &state);
1196         state.polarity = polarity;
1197         ret = pwm_apply_might_sleep(pwm, &state);
1198
1199         return ret ? : size;
1200 }
1201
1202 static ssize_t capture_show(struct device *pwm_dev,
1203                             struct device_attribute *attr,
1204                             char *buf)
1205 {
1206         struct pwm_device *pwm = pwm_from_dev(pwm_dev);
1207         struct pwm_capture result;
1208         int ret;
1209
1210         ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ));
1211         if (ret)
1212                 return ret;
1213
1214         return sysfs_emit(buf, "%u %u\n", result.period, result.duty_cycle);
1215 }
1216
1217 static DEVICE_ATTR_RW(period);
1218 static DEVICE_ATTR_RW(duty_cycle);
1219 static DEVICE_ATTR_RW(enable);
1220 static DEVICE_ATTR_RW(polarity);
1221 static DEVICE_ATTR_RO(capture);
1222
1223 static struct attribute *pwm_attrs[] = {
1224         &dev_attr_period.attr,
1225         &dev_attr_duty_cycle.attr,
1226         &dev_attr_enable.attr,
1227         &dev_attr_polarity.attr,
1228         &dev_attr_capture.attr,
1229         NULL
1230 };
1231 ATTRIBUTE_GROUPS(pwm);
1232
1233 static void pwm_export_release(struct device *pwm_dev)
1234 {
1235         struct pwm_export *export = pwmexport_from_dev(pwm_dev);
1236
1237         kfree(export);
1238 }
1239
1240 static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm)
1241 {
1242         struct pwm_export *export;
1243         char *pwm_prop[2];
1244         int ret;
1245
1246         if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
1247                 return -EBUSY;
1248
1249         export = kzalloc(sizeof(*export), GFP_KERNEL);
1250         if (!export) {
1251                 clear_bit(PWMF_EXPORTED, &pwm->flags);
1252                 return -ENOMEM;
1253         }
1254
1255         export->pwm = pwm;
1256         mutex_init(&export->lock);
1257
1258         export->pwm_dev.release = pwm_export_release;
1259         export->pwm_dev.parent = pwmchip_dev;
1260         export->pwm_dev.devt = MKDEV(0, 0);
1261         export->pwm_dev.groups = pwm_groups;
1262         dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm);
1263
1264         ret = device_register(&export->pwm_dev);
1265         if (ret) {
1266                 clear_bit(PWMF_EXPORTED, &pwm->flags);
1267                 put_device(&export->pwm_dev);
1268                 export = NULL;
1269                 return ret;
1270         }
1271         pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm);
1272         pwm_prop[1] = NULL;
1273         kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
1274         kfree(pwm_prop[0]);
1275
1276         return 0;
1277 }
1278
1279 static int pwm_unexport_match(struct device *pwm_dev, void *data)
1280 {
1281         return pwm_from_dev(pwm_dev) == data;
1282 }
1283
1284 static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm)
1285 {
1286         struct device *pwm_dev;
1287         char *pwm_prop[2];
1288
1289         if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
1290                 return -ENODEV;
1291
1292         pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
1293         if (!pwm_dev)
1294                 return -ENODEV;
1295
1296         pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm);
1297         pwm_prop[1] = NULL;
1298         kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
1299         kfree(pwm_prop[0]);
1300
1301         /* for device_find_child() */
1302         put_device(pwm_dev);
1303         device_unregister(pwm_dev);
1304         pwm_put(pwm);
1305
1306         return 0;
1307 }
1308
1309 static ssize_t export_store(struct device *pwmchip_dev,
1310                             struct device_attribute *attr,
1311                             const char *buf, size_t len)
1312 {
1313         struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
1314         struct pwm_device *pwm;
1315         unsigned int hwpwm;
1316         int ret;
1317
1318         ret = kstrtouint(buf, 0, &hwpwm);
1319         if (ret < 0)
1320                 return ret;
1321
1322         if (hwpwm >= chip->npwm)
1323                 return -ENODEV;
1324
1325         pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
1326         if (IS_ERR(pwm))
1327                 return PTR_ERR(pwm);
1328
1329         ret = pwm_export_child(pwmchip_dev, pwm);
1330         if (ret < 0)
1331                 pwm_put(pwm);
1332
1333         return ret ? : len;
1334 }
1335 static DEVICE_ATTR_WO(export);
1336
1337 static ssize_t unexport_store(struct device *pwmchip_dev,
1338                               struct device_attribute *attr,
1339                               const char *buf, size_t len)
1340 {
1341         struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
1342         unsigned int hwpwm;
1343         int ret;
1344
1345         ret = kstrtouint(buf, 0, &hwpwm);
1346         if (ret < 0)
1347                 return ret;
1348
1349         if (hwpwm >= chip->npwm)
1350                 return -ENODEV;
1351
1352         ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]);
1353
1354         return ret ? : len;
1355 }
1356 static DEVICE_ATTR_WO(unexport);
1357
1358 static ssize_t npwm_show(struct device *pwmchip_dev, struct device_attribute *attr,
1359                          char *buf)
1360 {
1361         const struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
1362
1363         return sysfs_emit(buf, "%u\n", chip->npwm);
1364 }
1365 static DEVICE_ATTR_RO(npwm);
1366
1367 static struct attribute *pwm_chip_attrs[] = {
1368         &dev_attr_export.attr,
1369         &dev_attr_unexport.attr,
1370         &dev_attr_npwm.attr,
1371         NULL,
1372 };
1373 ATTRIBUTE_GROUPS(pwm_chip);
1374
1375 /* takes export->lock on success */
1376 static struct pwm_export *pwm_class_get_state(struct device *pwmchip_dev,
1377                                               struct pwm_device *pwm,
1378                                               struct pwm_state *state)
1379 {
1380         struct device *pwm_dev;
1381         struct pwm_export *export;
1382
1383         if (!test_bit(PWMF_EXPORTED, &pwm->flags))
1384                 return NULL;
1385
1386         pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
1387         if (!pwm_dev)
1388                 return NULL;
1389
1390         export = pwmexport_from_dev(pwm_dev);
1391         put_device(pwm_dev);    /* for device_find_child() */
1392
1393         mutex_lock(&export->lock);
1394         pwm_get_state(pwm, state);
1395
1396         return export;
1397 }
1398
1399 static int pwm_class_apply_state(struct pwm_export *export,
1400                                  struct pwm_device *pwm,
1401                                  struct pwm_state *state)
1402 {
1403         int ret = pwm_apply_might_sleep(pwm, state);
1404
1405         /* release lock taken in pwm_class_get_state */
1406         mutex_unlock(&export->lock);
1407
1408         return ret;
1409 }
1410
1411 static int pwm_class_resume_npwm(struct device *pwmchip_dev, unsigned int npwm)
1412 {
1413         struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
1414         unsigned int i;
1415         int ret = 0;
1416
1417         for (i = 0; i < npwm; i++) {
1418                 struct pwm_device *pwm = &chip->pwms[i];
1419                 struct pwm_state state;
1420                 struct pwm_export *export;
1421
1422                 export = pwm_class_get_state(pwmchip_dev, pwm, &state);
1423                 if (!export)
1424                         continue;
1425
1426                 /* If pwmchip was not enabled before suspend, do nothing. */
1427                 if (!export->suspend.enabled) {
1428                         /* release lock taken in pwm_class_get_state */
1429                         mutex_unlock(&export->lock);
1430                         continue;
1431                 }
1432
1433                 state.enabled = export->suspend.enabled;
1434                 ret = pwm_class_apply_state(export, pwm, &state);
1435                 if (ret < 0)
1436                         break;
1437         }
1438
1439         return ret;
1440 }
1441
1442 static int pwm_class_suspend(struct device *pwmchip_dev)
1443 {
1444         struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
1445         unsigned int i;
1446         int ret = 0;
1447
1448         for (i = 0; i < chip->npwm; i++) {
1449                 struct pwm_device *pwm = &chip->pwms[i];
1450                 struct pwm_state state;
1451                 struct pwm_export *export;
1452
1453                 export = pwm_class_get_state(pwmchip_dev, pwm, &state);
1454                 if (!export)
1455                         continue;
1456
1457                 /*
1458                  * If pwmchip was not enabled before suspend, save
1459                  * state for resume time and do nothing else.
1460                  */
1461                 export->suspend = state;
1462                 if (!state.enabled) {
1463                         /* release lock taken in pwm_class_get_state */
1464                         mutex_unlock(&export->lock);
1465                         continue;
1466                 }
1467
1468                 state.enabled = false;
1469                 ret = pwm_class_apply_state(export, pwm, &state);
1470                 if (ret < 0) {
1471                         /*
1472                          * roll back the PWM devices that were disabled by
1473                          * this suspend function.
1474                          */
1475                         pwm_class_resume_npwm(pwmchip_dev, i);
1476                         break;
1477                 }
1478         }
1479
1480         return ret;
1481 }
1482
1483 static int pwm_class_resume(struct device *pwmchip_dev)
1484 {
1485         struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
1486
1487         return pwm_class_resume_npwm(pwmchip_dev, chip->npwm);
1488 }
1489
1490 static DEFINE_SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume);
1491
1492 static struct class pwm_class = {
1493         .name = "pwm",
1494         .dev_groups = pwm_chip_groups,
1495         .pm = pm_sleep_ptr(&pwm_class_pm_ops),
1496 };
1497
1498 static void pwmchip_sysfs_unexport(struct pwm_chip *chip)
1499 {
1500         unsigned int i;
1501
1502         for (i = 0; i < chip->npwm; i++) {
1503                 struct pwm_device *pwm = &chip->pwms[i];
1504
1505                 if (test_bit(PWMF_EXPORTED, &pwm->flags))
1506                         pwm_unexport_child(&chip->dev, pwm);
1507         }
1508 }
1509
1510 #define PWMCHIP_ALIGN ARCH_DMA_MINALIGN
1511
1512 static void *pwmchip_priv(struct pwm_chip *chip)
1513 {
1514         return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN);
1515 }
1516
1517 /* This is the counterpart to pwmchip_alloc() */
1518 void pwmchip_put(struct pwm_chip *chip)
1519 {
1520         put_device(&chip->dev);
1521 }
1522 EXPORT_SYMBOL_GPL(pwmchip_put);
1523
1524 static void pwmchip_release(struct device *pwmchip_dev)
1525 {
1526         struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
1527
1528         kfree(chip);
1529 }
1530
1531 struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
1532 {
1533         struct pwm_chip *chip;
1534         struct device *pwmchip_dev;
1535         size_t alloc_size;
1536         unsigned int i;
1537
1538         alloc_size = size_add(ALIGN(struct_size(chip, pwms, npwm), PWMCHIP_ALIGN),
1539                               sizeof_priv);
1540
1541         chip = kzalloc(alloc_size, GFP_KERNEL);
1542         if (!chip)
1543                 return ERR_PTR(-ENOMEM);
1544
1545         chip->npwm = npwm;
1546         chip->uses_pwmchip_alloc = true;
1547         chip->operational = false;
1548
1549         pwmchip_dev = &chip->dev;
1550         device_initialize(pwmchip_dev);
1551         pwmchip_dev->class = &pwm_class;
1552         pwmchip_dev->parent = parent;
1553         pwmchip_dev->release = pwmchip_release;
1554
1555         pwmchip_set_drvdata(chip, pwmchip_priv(chip));
1556
1557         for (i = 0; i < chip->npwm; i++) {
1558                 struct pwm_device *pwm = &chip->pwms[i];
1559                 pwm->chip = chip;
1560                 pwm->hwpwm = i;
1561         }
1562
1563         return chip;
1564 }
1565 EXPORT_SYMBOL_GPL(pwmchip_alloc);
1566
1567 static void devm_pwmchip_put(void *data)
1568 {
1569         struct pwm_chip *chip = data;
1570
1571         pwmchip_put(chip);
1572 }
1573
1574 struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
1575 {
1576         struct pwm_chip *chip;
1577         int ret;
1578
1579         chip = pwmchip_alloc(parent, npwm, sizeof_priv);
1580         if (IS_ERR(chip))
1581                 return chip;
1582
1583         ret = devm_add_action_or_reset(parent, devm_pwmchip_put, chip);
1584         if (ret)
1585                 return ERR_PTR(ret);
1586
1587         return chip;
1588 }
1589 EXPORT_SYMBOL_GPL(devm_pwmchip_alloc);
1590
1591 static void of_pwmchip_add(struct pwm_chip *chip)
1592 {
1593         if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node)
1594                 return;
1595
1596         if (!chip->of_xlate)
1597                 chip->of_xlate = of_pwm_xlate_with_flags;
1598
1599         of_node_get(pwmchip_parent(chip)->of_node);
1600 }
1601
1602 static void of_pwmchip_remove(struct pwm_chip *chip)
1603 {
1604         if (pwmchip_parent(chip))
1605                 of_node_put(pwmchip_parent(chip)->of_node);
1606 }
1607
1608 static bool pwm_ops_check(const struct pwm_chip *chip)
1609 {
1610         const struct pwm_ops *ops = chip->ops;
1611
1612         if (ops->write_waveform) {
1613                 if (!ops->round_waveform_tohw ||
1614                     !ops->round_waveform_fromhw ||
1615                     !ops->write_waveform)
1616                         return false;
1617
1618                 if (WFHWSIZE < ops->sizeof_wfhw) {
1619                         dev_warn(pwmchip_parent(chip), "WFHWSIZE < %zu\n", ops->sizeof_wfhw);
1620                         return false;
1621                 }
1622         } else {
1623                 if (!ops->apply)
1624                         return false;
1625
1626                 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
1627                         dev_warn(pwmchip_parent(chip),
1628                                  "Please implement the .get_state() callback\n");
1629         }
1630
1631         return true;
1632 }
1633
1634 static struct device_link *pwm_device_link_add(struct device *dev,
1635                                                struct pwm_device *pwm)
1636 {
1637         struct device_link *dl;
1638
1639         if (!dev) {
1640                 /*
1641                  * No device for the PWM consumer has been provided. It may
1642                  * impact the PM sequence ordering: the PWM supplier may get
1643                  * suspended before the consumer.
1644                  */
1645                 dev_warn(pwmchip_parent(pwm->chip),
1646                          "No consumer device specified to create a link to\n");
1647                 return NULL;
1648         }
1649
1650         dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER);
1651         if (!dl) {
1652                 dev_err(dev, "failed to create device link to %s\n",
1653                         dev_name(pwmchip_parent(pwm->chip)));
1654                 return ERR_PTR(-EINVAL);
1655         }
1656
1657         return dl;
1658 }
1659
1660 static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
1661 {
1662         struct pwm_chip *chip;
1663         unsigned long id, tmp;
1664
1665         guard(mutex)(&pwm_lock);
1666
1667         idr_for_each_entry_ul(&pwm_chips, chip, tmp, id)
1668                 if (pwmchip_parent(chip) && device_match_fwnode(pwmchip_parent(chip), fwnode))
1669                         return chip;
1670
1671         return ERR_PTR(-EPROBE_DEFER);
1672 }
1673
1674 /**
1675  * of_pwm_get() - request a PWM via the PWM framework
1676  * @dev: device for PWM consumer
1677  * @np: device node to get the PWM from
1678  * @con_id: consumer name
1679  *
1680  * Returns the PWM device parsed from the phandle and index specified in the
1681  * "pwms" property of a device tree node or a negative error-code on failure.
1682  * Values parsed from the device tree are stored in the returned PWM device
1683  * object.
1684  *
1685  * If con_id is NULL, the first PWM device listed in the "pwms" property will
1686  * be requested. Otherwise the "pwm-names" property is used to do a reverse
1687  * lookup of the PWM index. This also means that the "pwm-names" property
1688  * becomes mandatory for devices that look up the PWM device via the con_id
1689  * parameter.
1690  *
1691  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1692  * error code on failure.
1693  */
1694 static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
1695                                      const char *con_id)
1696 {
1697         struct pwm_device *pwm = NULL;
1698         struct of_phandle_args args;
1699         struct device_link *dl;
1700         struct pwm_chip *chip;
1701         int index = 0;
1702         int err;
1703
1704         if (con_id) {
1705                 index = of_property_match_string(np, "pwm-names", con_id);
1706                 if (index < 0)
1707                         return ERR_PTR(index);
1708         }
1709
1710         err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
1711                                          &args);
1712         if (err) {
1713                 pr_err("%s(): can't parse \"pwms\" property\n", __func__);
1714                 return ERR_PTR(err);
1715         }
1716
1717         chip = fwnode_to_pwmchip(of_fwnode_handle(args.np));
1718         if (IS_ERR(chip)) {
1719                 if (PTR_ERR(chip) != -EPROBE_DEFER)
1720                         pr_err("%s(): PWM chip not found\n", __func__);
1721
1722                 pwm = ERR_CAST(chip);
1723                 goto put;
1724         }
1725
1726         pwm = chip->of_xlate(chip, &args);
1727         if (IS_ERR(pwm))
1728                 goto put;
1729
1730         dl = pwm_device_link_add(dev, pwm);
1731         if (IS_ERR(dl)) {
1732                 /* of_xlate ended up calling pwm_request_from_chip() */
1733                 pwm_put(pwm);
1734                 pwm = ERR_CAST(dl);
1735                 goto put;
1736         }
1737
1738         /*
1739          * If a consumer name was not given, try to look it up from the
1740          * "pwm-names" property if it exists. Otherwise use the name of
1741          * the user device node.
1742          */
1743         if (!con_id) {
1744                 err = of_property_read_string_index(np, "pwm-names", index,
1745                                                     &con_id);
1746                 if (err < 0)
1747                         con_id = np->name;
1748         }
1749
1750         pwm->label = con_id;
1751
1752 put:
1753         of_node_put(args.np);
1754
1755         return pwm;
1756 }
1757
1758 /**
1759  * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
1760  * @fwnode: firmware node to get the "pwms" property from
1761  *
1762  * Returns the PWM device parsed from the fwnode and index specified in the
1763  * "pwms" property or a negative error-code on failure.
1764  * Values parsed from the device tree are stored in the returned PWM device
1765  * object.
1766  *
1767  * This is analogous to of_pwm_get() except con_id is not yet supported.
1768  * ACPI entries must look like
1769  * Package () {"pwms", Package ()
1770  *     { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
1771  *
1772  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1773  * error code on failure.
1774  */
1775 static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode)
1776 {
1777         struct pwm_device *pwm;
1778         struct fwnode_reference_args args;
1779         struct pwm_chip *chip;
1780         int ret;
1781
1782         memset(&args, 0, sizeof(args));
1783
1784         ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
1785         if (ret < 0)
1786                 return ERR_PTR(ret);
1787
1788         if (args.nargs < 2)
1789                 return ERR_PTR(-EPROTO);
1790
1791         chip = fwnode_to_pwmchip(args.fwnode);
1792         if (IS_ERR(chip))
1793                 return ERR_CAST(chip);
1794
1795         pwm = pwm_request_from_chip(chip, args.args[0], NULL);
1796         if (IS_ERR(pwm))
1797                 return pwm;
1798
1799         pwm->args.period = args.args[1];
1800         pwm->args.polarity = PWM_POLARITY_NORMAL;
1801
1802         if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
1803                 pwm->args.polarity = PWM_POLARITY_INVERSED;
1804
1805         return pwm;
1806 }
1807
1808 static DEFINE_MUTEX(pwm_lookup_lock);
1809 static LIST_HEAD(pwm_lookup_list);
1810
1811 /**
1812  * pwm_get() - look up and request a PWM device
1813  * @dev: device for PWM consumer
1814  * @con_id: consumer name
1815  *
1816  * Lookup is first attempted using DT. If the device was not instantiated from
1817  * a device tree, a PWM chip and a relative index is looked up via a table
1818  * supplied by board setup code (see pwm_add_table()).
1819  *
1820  * Once a PWM chip has been found the specified PWM device will be requested
1821  * and is ready to be used.
1822  *
1823  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1824  * error code on failure.
1825  */
1826 struct pwm_device *pwm_get(struct device *dev, const char *con_id)
1827 {
1828         const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
1829         const char *dev_id = dev ? dev_name(dev) : NULL;
1830         struct pwm_device *pwm;
1831         struct pwm_chip *chip;
1832         struct device_link *dl;
1833         unsigned int best = 0;
1834         struct pwm_lookup *p, *chosen = NULL;
1835         unsigned int match;
1836         int err;
1837
1838         /* look up via DT first */
1839         if (is_of_node(fwnode))
1840                 return of_pwm_get(dev, to_of_node(fwnode), con_id);
1841
1842         /* then lookup via ACPI */
1843         if (is_acpi_node(fwnode)) {
1844                 pwm = acpi_pwm_get(fwnode);
1845                 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
1846                         return pwm;
1847         }
1848
1849         /*
1850          * We look up the provider in the static table typically provided by
1851          * board setup code. We first try to lookup the consumer device by
1852          * name. If the consumer device was passed in as NULL or if no match
1853          * was found, we try to find the consumer by directly looking it up
1854          * by name.
1855          *
1856          * If a match is found, the provider PWM chip is looked up by name
1857          * and a PWM device is requested using the PWM device per-chip index.
1858          *
1859          * The lookup algorithm was shamelessly taken from the clock
1860          * framework:
1861          *
1862          * We do slightly fuzzy matching here:
1863          *  An entry with a NULL ID is assumed to be a wildcard.
1864          *  If an entry has a device ID, it must match
1865          *  If an entry has a connection ID, it must match
1866          * Then we take the most specific entry - with the following order
1867          * of precedence: dev+con > dev only > con only.
1868          */
1869         scoped_guard(mutex, &pwm_lookup_lock)
1870                 list_for_each_entry(p, &pwm_lookup_list, list) {
1871                         match = 0;
1872
1873                         if (p->dev_id) {
1874                                 if (!dev_id || strcmp(p->dev_id, dev_id))
1875                                         continue;
1876
1877                                 match += 2;
1878                         }
1879
1880                         if (p->con_id) {
1881                                 if (!con_id || strcmp(p->con_id, con_id))
1882                                         continue;
1883
1884                                 match += 1;
1885                         }
1886
1887                         if (match > best) {
1888                                 chosen = p;
1889
1890                                 if (match != 3)
1891                                         best = match;
1892                                 else
1893                                         break;
1894                         }
1895                 }
1896
1897         if (!chosen)
1898                 return ERR_PTR(-ENODEV);
1899
1900         chip = pwmchip_find_by_name(chosen->provider);
1901
1902         /*
1903          * If the lookup entry specifies a module, load the module and retry
1904          * the PWM chip lookup. This can be used to work around driver load
1905          * ordering issues if driver's can't be made to properly support the
1906          * deferred probe mechanism.
1907          */
1908         if (!chip && chosen->module) {
1909                 err = request_module(chosen->module);
1910                 if (err == 0)
1911                         chip = pwmchip_find_by_name(chosen->provider);
1912         }
1913
1914         if (!chip)
1915                 return ERR_PTR(-EPROBE_DEFER);
1916
1917         pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
1918         if (IS_ERR(pwm))
1919                 return pwm;
1920
1921         dl = pwm_device_link_add(dev, pwm);
1922         if (IS_ERR(dl)) {
1923                 pwm_put(pwm);
1924                 return ERR_CAST(dl);
1925         }
1926
1927         pwm->args.period = chosen->period;
1928         pwm->args.polarity = chosen->polarity;
1929
1930         return pwm;
1931 }
1932 EXPORT_SYMBOL_GPL(pwm_get);
1933
1934 /**
1935  * pwm_put() - release a PWM device
1936  * @pwm: PWM device
1937  */
1938 void pwm_put(struct pwm_device *pwm)
1939 {
1940         struct pwm_chip *chip;
1941
1942         if (!pwm)
1943                 return;
1944
1945         chip = pwm->chip;
1946
1947         guard(mutex)(&pwm_lock);
1948
1949         /*
1950          * Trigger a warning if a consumer called pwm_put() twice.
1951          * If the chip isn't operational, PWMF_REQUESTED was already cleared in
1952          * pwmchip_remove(). So don't warn in this case.
1953          */
1954         if (chip->operational && !test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
1955                 pr_warn("PWM device already freed\n");
1956                 return;
1957         }
1958
1959         if (chip->operational && chip->ops->free)
1960                 pwm->chip->ops->free(pwm->chip, pwm);
1961
1962         pwm->label = NULL;
1963
1964         put_device(&chip->dev);
1965
1966         module_put(chip->owner);
1967 }
1968 EXPORT_SYMBOL_GPL(pwm_put);
1969
1970 static void devm_pwm_release(void *pwm)
1971 {
1972         pwm_put(pwm);
1973 }
1974
1975 /**
1976  * devm_pwm_get() - resource managed pwm_get()
1977  * @dev: device for PWM consumer
1978  * @con_id: consumer name
1979  *
1980  * This function performs like pwm_get() but the acquired PWM device will
1981  * automatically be released on driver detach.
1982  *
1983  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1984  * error code on failure.
1985  */
1986 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
1987 {
1988         struct pwm_device *pwm;
1989         int ret;
1990
1991         pwm = pwm_get(dev, con_id);
1992         if (IS_ERR(pwm))
1993                 return pwm;
1994
1995         ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1996         if (ret)
1997                 return ERR_PTR(ret);
1998
1999         return pwm;
2000 }
2001 EXPORT_SYMBOL_GPL(devm_pwm_get);
2002
2003 /**
2004  * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
2005  * @dev: device for PWM consumer
2006  * @fwnode: firmware node to get the PWM from
2007  * @con_id: consumer name
2008  *
2009  * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
2010  * acpi_pwm_get() for a detailed description.
2011  *
2012  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
2013  * error code on failure.
2014  */
2015 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
2016                                        struct fwnode_handle *fwnode,
2017                                        const char *con_id)
2018 {
2019         struct pwm_device *pwm = ERR_PTR(-ENODEV);
2020         int ret;
2021
2022         if (is_of_node(fwnode))
2023                 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
2024         else if (is_acpi_node(fwnode))
2025                 pwm = acpi_pwm_get(fwnode);
2026         if (IS_ERR(pwm))
2027                 return pwm;
2028
2029         ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
2030         if (ret)
2031                 return ERR_PTR(ret);
2032
2033         return pwm;
2034 }
2035 EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
2036
2037 /**
2038  * __pwmchip_add() - register a new PWM chip
2039  * @chip: the PWM chip to add
2040  * @owner: reference to the module providing the chip.
2041  *
2042  * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
2043  * pwmchip_add wrapper to do this right.
2044  *
2045  * Returns: 0 on success or a negative error code on failure.
2046  */
2047 int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
2048 {
2049         int ret;
2050
2051         if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm)
2052                 return -EINVAL;
2053
2054         /*
2055          * a struct pwm_chip must be allocated using (devm_)pwmchip_alloc,
2056          * otherwise the embedded struct device might disappear too early
2057          * resulting in memory corruption.
2058          * Catch drivers that were not converted appropriately.
2059          */
2060         if (!chip->uses_pwmchip_alloc)
2061                 return -EINVAL;
2062
2063         if (!pwm_ops_check(chip))
2064                 return -EINVAL;
2065
2066         chip->owner = owner;
2067
2068         if (chip->atomic)
2069                 spin_lock_init(&chip->atomic_lock);
2070         else
2071                 mutex_init(&chip->nonatomic_lock);
2072
2073         guard(mutex)(&pwm_lock);
2074
2075         ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL);
2076         if (ret < 0)
2077                 return ret;
2078
2079         chip->id = ret;
2080
2081         dev_set_name(&chip->dev, "pwmchip%u", chip->id);
2082
2083         if (IS_ENABLED(CONFIG_OF))
2084                 of_pwmchip_add(chip);
2085
2086         scoped_guard(pwmchip, chip)
2087                 chip->operational = true;
2088
2089         ret = device_add(&chip->dev);
2090         if (ret)
2091                 goto err_device_add;
2092
2093         return 0;
2094
2095 err_device_add:
2096         scoped_guard(pwmchip, chip)
2097                 chip->operational = false;
2098
2099         if (IS_ENABLED(CONFIG_OF))
2100                 of_pwmchip_remove(chip);
2101
2102         idr_remove(&pwm_chips, chip->id);
2103
2104         return ret;
2105 }
2106 EXPORT_SYMBOL_GPL(__pwmchip_add);
2107
2108 /**
2109  * pwmchip_remove() - remove a PWM chip
2110  * @chip: the PWM chip to remove
2111  *
2112  * Removes a PWM chip.
2113  */
2114 void pwmchip_remove(struct pwm_chip *chip)
2115 {
2116         pwmchip_sysfs_unexport(chip);
2117
2118         scoped_guard(mutex, &pwm_lock) {
2119                 unsigned int i;
2120
2121                 scoped_guard(pwmchip, chip)
2122                         chip->operational = false;
2123
2124                 for (i = 0; i < chip->npwm; ++i) {
2125                         struct pwm_device *pwm = &chip->pwms[i];
2126
2127                         if (test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
2128                                 dev_warn(&chip->dev, "Freeing requested PWM #%u\n", i);
2129                                 if (pwm->chip->ops->free)
2130                                         pwm->chip->ops->free(pwm->chip, pwm);
2131                         }
2132                 }
2133
2134                 if (IS_ENABLED(CONFIG_OF))
2135                         of_pwmchip_remove(chip);
2136
2137                 idr_remove(&pwm_chips, chip->id);
2138         }
2139
2140         device_del(&chip->dev);
2141 }
2142 EXPORT_SYMBOL_GPL(pwmchip_remove);
2143
2144 static void devm_pwmchip_remove(void *data)
2145 {
2146         struct pwm_chip *chip = data;
2147
2148         pwmchip_remove(chip);
2149 }
2150
2151 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner)
2152 {
2153         int ret;
2154
2155         ret = __pwmchip_add(chip, owner);
2156         if (ret)
2157                 return ret;
2158
2159         return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
2160 }
2161 EXPORT_SYMBOL_GPL(__devm_pwmchip_add);
2162
2163 /**
2164  * pwm_add_table() - register PWM device consumers
2165  * @table: array of consumers to register
2166  * @num: number of consumers in table
2167  */
2168 void pwm_add_table(struct pwm_lookup *table, size_t num)
2169 {
2170         guard(mutex)(&pwm_lookup_lock);
2171
2172         while (num--) {
2173                 list_add_tail(&table->list, &pwm_lookup_list);
2174                 table++;
2175         }
2176 }
2177
2178 /**
2179  * pwm_remove_table() - unregister PWM device consumers
2180  * @table: array of consumers to unregister
2181  * @num: number of consumers in table
2182  */
2183 void pwm_remove_table(struct pwm_lookup *table, size_t num)
2184 {
2185         guard(mutex)(&pwm_lookup_lock);
2186
2187         while (num--) {
2188                 list_del(&table->list);
2189                 table++;
2190         }
2191 }
2192
2193 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
2194 {
2195         unsigned int i;
2196
2197         for (i = 0; i < chip->npwm; i++) {
2198                 struct pwm_device *pwm = &chip->pwms[i];
2199                 struct pwm_state state;
2200
2201                 pwm_get_state(pwm, &state);
2202
2203                 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
2204
2205                 if (test_bit(PWMF_REQUESTED, &pwm->flags))
2206                         seq_puts(s, " requested");
2207
2208                 if (state.enabled)
2209                         seq_puts(s, " enabled");
2210
2211                 seq_printf(s, " period: %llu ns", state.period);
2212                 seq_printf(s, " duty: %llu ns", state.duty_cycle);
2213                 seq_printf(s, " polarity: %s",
2214                            state.polarity ? "inverse" : "normal");
2215
2216                 if (state.usage_power)
2217                         seq_puts(s, " usage_power");
2218
2219                 seq_puts(s, "\n");
2220         }
2221 }
2222
2223 static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
2224 {
2225         unsigned long id = *pos;
2226         void *ret;
2227
2228         mutex_lock(&pwm_lock);
2229         s->private = "";
2230
2231         ret = idr_get_next_ul(&pwm_chips, &id);
2232         *pos = id;
2233         return ret;
2234 }
2235
2236 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
2237 {
2238         unsigned long id = *pos + 1;
2239         void *ret;
2240
2241         s->private = "\n";
2242
2243         ret = idr_get_next_ul(&pwm_chips, &id);
2244         *pos = id;
2245         return ret;
2246 }
2247
2248 static void pwm_seq_stop(struct seq_file *s, void *v)
2249 {
2250         mutex_unlock(&pwm_lock);
2251 }
2252
2253 static int pwm_seq_show(struct seq_file *s, void *v)
2254 {
2255         struct pwm_chip *chip = v;
2256
2257         seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n",
2258                    (char *)s->private, chip->id,
2259                    pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus",
2260                    dev_name(pwmchip_parent(chip)), chip->npwm,
2261                    (chip->npwm != 1) ? "s" : "");
2262
2263         pwm_dbg_show(chip, s);
2264
2265         return 0;
2266 }
2267
2268 static const struct seq_operations pwm_debugfs_sops = {
2269         .start = pwm_seq_start,
2270         .next = pwm_seq_next,
2271         .stop = pwm_seq_stop,
2272         .show = pwm_seq_show,
2273 };
2274
2275 DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
2276
2277 static int __init pwm_init(void)
2278 {
2279         int ret;
2280
2281         ret = class_register(&pwm_class);
2282         if (ret) {
2283                 pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret));
2284                 return ret;
2285         }
2286
2287         if (IS_ENABLED(CONFIG_DEBUG_FS))
2288                 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops);
2289
2290         return 0;
2291 }
2292 subsys_initcall(pwm_init);
This page took 0.155119 seconds and 4 git commands to generate.