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