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