xref: /linux/drivers/pwm/core.c (revision 0007fa1292a24ef23d662ec7afe178088ab7766d)
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 
34 static void pwm_apply_debug(struct pwm_device *pwm,
35 			    const struct pwm_state *state)
36 {
37 	struct pwm_state *last = &pwm->last;
38 	struct pwm_chip *chip = pwm->chip;
39 	struct pwm_state s1 = { 0 }, s2 = { 0 };
40 	int err;
41 
42 	if (!IS_ENABLED(CONFIG_PWM_DEBUG))
43 		return;
44 
45 	/* No reasonable diagnosis possible without .get_state() */
46 	if (!chip->ops->get_state)
47 		return;
48 
49 	/*
50 	 * *state was just applied. Read out the hardware state and do some
51 	 * checks.
52 	 */
53 
54 	err = chip->ops->get_state(chip, pwm, &s1);
55 	trace_pwm_get(pwm, &s1, err);
56 	if (err)
57 		/* If that failed there isn't much to debug */
58 		return;
59 
60 	/*
61 	 * The lowlevel driver either ignored .polarity (which is a bug) or as
62 	 * best effort inverted .polarity and fixed .duty_cycle respectively.
63 	 * Undo this inversion and fixup for further tests.
64 	 */
65 	if (s1.enabled && s1.polarity != state->polarity) {
66 		s2.polarity = state->polarity;
67 		s2.duty_cycle = s1.period - s1.duty_cycle;
68 		s2.period = s1.period;
69 		s2.enabled = s1.enabled;
70 	} else {
71 		s2 = s1;
72 	}
73 
74 	if (s2.polarity != state->polarity &&
75 	    state->duty_cycle < state->period)
76 		dev_warn(pwmchip_parent(chip), ".apply ignored .polarity\n");
77 
78 	if (state->enabled &&
79 	    last->polarity == state->polarity &&
80 	    last->period > s2.period &&
81 	    last->period <= state->period)
82 		dev_warn(pwmchip_parent(chip),
83 			 ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
84 			 state->period, s2.period, last->period);
85 
86 	if (state->enabled && state->period < s2.period)
87 		dev_warn(pwmchip_parent(chip),
88 			 ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
89 			 state->period, s2.period);
90 
91 	if (state->enabled &&
92 	    last->polarity == state->polarity &&
93 	    last->period == s2.period &&
94 	    last->duty_cycle > s2.duty_cycle &&
95 	    last->duty_cycle <= state->duty_cycle)
96 		dev_warn(pwmchip_parent(chip),
97 			 ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
98 			 state->duty_cycle, state->period,
99 			 s2.duty_cycle, s2.period,
100 			 last->duty_cycle, last->period);
101 
102 	if (state->enabled && state->duty_cycle < s2.duty_cycle)
103 		dev_warn(pwmchip_parent(chip),
104 			 ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
105 			 state->duty_cycle, state->period,
106 			 s2.duty_cycle, s2.period);
107 
108 	if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
109 		dev_warn(pwmchip_parent(chip),
110 			 "requested disabled, but yielded enabled with duty > 0\n");
111 
112 	/* reapply the state that the driver reported being configured. */
113 	err = chip->ops->apply(chip, pwm, &s1);
114 	trace_pwm_apply(pwm, &s1, err);
115 	if (err) {
116 		*last = s1;
117 		dev_err(pwmchip_parent(chip), "failed to reapply current setting\n");
118 		return;
119 	}
120 
121 	*last = (struct pwm_state){ 0 };
122 	err = chip->ops->get_state(chip, pwm, last);
123 	trace_pwm_get(pwm, last, err);
124 	if (err)
125 		return;
126 
127 	/* reapplication of the current state should give an exact match */
128 	if (s1.enabled != last->enabled ||
129 	    s1.polarity != last->polarity ||
130 	    (s1.enabled && s1.period != last->period) ||
131 	    (s1.enabled && s1.duty_cycle != last->duty_cycle)) {
132 		dev_err(pwmchip_parent(chip),
133 			".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
134 			s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
135 			last->enabled, last->polarity, last->duty_cycle,
136 			last->period);
137 	}
138 }
139 
140 /**
141  * __pwm_apply() - atomically apply a new state to a PWM device
142  * @pwm: PWM device
143  * @state: new state to apply
144  */
145 static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
146 {
147 	struct pwm_chip *chip;
148 	int err;
149 
150 	if (!pwm || !state || !state->period ||
151 	    state->duty_cycle > state->period)
152 		return -EINVAL;
153 
154 	chip = pwm->chip;
155 
156 	if (state->period == pwm->state.period &&
157 	    state->duty_cycle == pwm->state.duty_cycle &&
158 	    state->polarity == pwm->state.polarity &&
159 	    state->enabled == pwm->state.enabled &&
160 	    state->usage_power == pwm->state.usage_power)
161 		return 0;
162 
163 	err = chip->ops->apply(chip, pwm, state);
164 	trace_pwm_apply(pwm, state, err);
165 	if (err)
166 		return err;
167 
168 	pwm->state = *state;
169 
170 	/*
171 	 * only do this after pwm->state was applied as some
172 	 * implementations of .get_state depend on this
173 	 */
174 	pwm_apply_debug(pwm, state);
175 
176 	return 0;
177 }
178 
179 /**
180  * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
181  * Cannot be used in atomic context.
182  * @pwm: PWM device
183  * @state: new state to apply
184  */
185 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
186 {
187 	int err;
188 
189 	/*
190 	 * Some lowlevel driver's implementations of .apply() make use of
191 	 * mutexes, also with some drivers only returning when the new
192 	 * configuration is active calling pwm_apply_might_sleep() from atomic context
193 	 * is a bad idea. So make it explicit that calling this function might
194 	 * sleep.
195 	 */
196 	might_sleep();
197 
198 	if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm->chip->atomic) {
199 		/*
200 		 * Catch any drivers that have been marked as atomic but
201 		 * that will sleep anyway.
202 		 */
203 		non_block_start();
204 		err = __pwm_apply(pwm, state);
205 		non_block_end();
206 	} else {
207 		err = __pwm_apply(pwm, state);
208 	}
209 
210 	return err;
211 }
212 EXPORT_SYMBOL_GPL(pwm_apply_might_sleep);
213 
214 /**
215  * pwm_apply_atomic() - apply a new state to a PWM device from atomic context
216  * Not all PWM devices support this function, check with pwm_might_sleep().
217  * @pwm: PWM device
218  * @state: new state to apply
219  */
220 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state)
221 {
222 	WARN_ONCE(!pwm->chip->atomic,
223 		  "sleeping PWM driver used in atomic context\n");
224 
225 	return __pwm_apply(pwm, state);
226 }
227 EXPORT_SYMBOL_GPL(pwm_apply_atomic);
228 
229 /**
230  * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
231  * @pwm: PWM device
232  *
233  * This function will adjust the PWM config to the PWM arguments provided
234  * by the DT or PWM lookup table. This is particularly useful to adapt
235  * the bootloader config to the Linux one.
236  */
237 int pwm_adjust_config(struct pwm_device *pwm)
238 {
239 	struct pwm_state state;
240 	struct pwm_args pargs;
241 
242 	pwm_get_args(pwm, &pargs);
243 	pwm_get_state(pwm, &state);
244 
245 	/*
246 	 * If the current period is zero it means that either the PWM driver
247 	 * does not support initial state retrieval or the PWM has not yet
248 	 * been configured.
249 	 *
250 	 * In either case, we setup the new period and polarity, and assign a
251 	 * duty cycle of 0.
252 	 */
253 	if (!state.period) {
254 		state.duty_cycle = 0;
255 		state.period = pargs.period;
256 		state.polarity = pargs.polarity;
257 
258 		return pwm_apply_might_sleep(pwm, &state);
259 	}
260 
261 	/*
262 	 * Adjust the PWM duty cycle/period based on the period value provided
263 	 * in PWM args.
264 	 */
265 	if (pargs.period != state.period) {
266 		u64 dutycycle = (u64)state.duty_cycle * pargs.period;
267 
268 		do_div(dutycycle, state.period);
269 		state.duty_cycle = dutycycle;
270 		state.period = pargs.period;
271 	}
272 
273 	/*
274 	 * If the polarity changed, we should also change the duty cycle.
275 	 */
276 	if (pargs.polarity != state.polarity) {
277 		state.polarity = pargs.polarity;
278 		state.duty_cycle = state.period - state.duty_cycle;
279 	}
280 
281 	return pwm_apply_might_sleep(pwm, &state);
282 }
283 EXPORT_SYMBOL_GPL(pwm_adjust_config);
284 
285 /**
286  * pwm_capture() - capture and report a PWM signal
287  * @pwm: PWM device
288  * @result: structure to fill with capture result
289  * @timeout: time to wait, in milliseconds, before giving up on capture
290  *
291  * Returns: 0 on success or a negative error code on failure.
292  */
293 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
294 		unsigned long timeout)
295 {
296 	if (!pwm || !pwm->chip->ops)
297 		return -EINVAL;
298 
299 	if (!pwm->chip->ops->capture)
300 		return -ENOSYS;
301 
302 	guard(mutex)(&pwm_lock);
303 
304 	return pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
305 }
306 EXPORT_SYMBOL_GPL(pwm_capture);
307 
308 static struct pwm_chip *pwmchip_find_by_name(const char *name)
309 {
310 	struct pwm_chip *chip;
311 	unsigned long id, tmp;
312 
313 	if (!name)
314 		return NULL;
315 
316 	guard(mutex)(&pwm_lock);
317 
318 	idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) {
319 		const char *chip_name = dev_name(pwmchip_parent(chip));
320 
321 		if (chip_name && strcmp(chip_name, name) == 0)
322 			return chip;
323 	}
324 
325 	return NULL;
326 }
327 
328 static int pwm_device_request(struct pwm_device *pwm, const char *label)
329 {
330 	int err;
331 	struct pwm_chip *chip = pwm->chip;
332 	const struct pwm_ops *ops = chip->ops;
333 
334 	if (test_bit(PWMF_REQUESTED, &pwm->flags))
335 		return -EBUSY;
336 
337 	if (!try_module_get(chip->owner))
338 		return -ENODEV;
339 
340 	if (!get_device(&chip->dev)) {
341 		err = -ENODEV;
342 		goto err_get_device;
343 	}
344 
345 	if (ops->request) {
346 		err = ops->request(chip, pwm);
347 		if (err) {
348 			put_device(&chip->dev);
349 err_get_device:
350 			module_put(chip->owner);
351 			return err;
352 		}
353 	}
354 
355 	if (ops->get_state) {
356 		/*
357 		 * Zero-initialize state because most drivers are unaware of
358 		 * .usage_power. The other members of state are supposed to be
359 		 * set by lowlevel drivers. We still initialize the whole
360 		 * structure for simplicity even though this might paper over
361 		 * faulty implementations of .get_state().
362 		 */
363 		struct pwm_state state = { 0, };
364 
365 		err = ops->get_state(chip, pwm, &state);
366 		trace_pwm_get(pwm, &state, err);
367 
368 		if (!err)
369 			pwm->state = state;
370 
371 		if (IS_ENABLED(CONFIG_PWM_DEBUG))
372 			pwm->last = pwm->state;
373 	}
374 
375 	set_bit(PWMF_REQUESTED, &pwm->flags);
376 	pwm->label = label;
377 
378 	return 0;
379 }
380 
381 /**
382  * pwm_request_from_chip() - request a PWM device relative to a PWM chip
383  * @chip: PWM chip
384  * @index: per-chip index of the PWM to request
385  * @label: a literal description string of this PWM
386  *
387  * Returns: A pointer to the PWM device at the given index of the given PWM
388  * chip. A negative error code is returned if the index is not valid for the
389  * specified PWM chip or if the PWM device cannot be requested.
390  */
391 static struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
392 						unsigned int index,
393 						const char *label)
394 {
395 	struct pwm_device *pwm;
396 	int err;
397 
398 	if (!chip || index >= chip->npwm)
399 		return ERR_PTR(-EINVAL);
400 
401 	guard(mutex)(&pwm_lock);
402 
403 	pwm = &chip->pwms[index];
404 
405 	err = pwm_device_request(pwm, label);
406 	if (err < 0)
407 		return ERR_PTR(err);
408 
409 	return pwm;
410 }
411 
412 struct pwm_device *
413 of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args)
414 {
415 	struct pwm_device *pwm;
416 
417 	/* period in the second cell and flags in the third cell are optional */
418 	if (args->args_count < 1)
419 		return ERR_PTR(-EINVAL);
420 
421 	pwm = pwm_request_from_chip(chip, args->args[0], NULL);
422 	if (IS_ERR(pwm))
423 		return pwm;
424 
425 	if (args->args_count > 1)
426 		pwm->args.period = args->args[1];
427 
428 	pwm->args.polarity = PWM_POLARITY_NORMAL;
429 	if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
430 		pwm->args.polarity = PWM_POLARITY_INVERSED;
431 
432 	return pwm;
433 }
434 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
435 
436 struct pwm_device *
437 of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args)
438 {
439 	struct pwm_device *pwm;
440 
441 	pwm = pwm_request_from_chip(chip, 0, NULL);
442 	if (IS_ERR(pwm))
443 		return pwm;
444 
445 	if (args->args_count > 0)
446 		pwm->args.period = args->args[0];
447 
448 	pwm->args.polarity = PWM_POLARITY_NORMAL;
449 	if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED)
450 		pwm->args.polarity = PWM_POLARITY_INVERSED;
451 
452 	return pwm;
453 }
454 EXPORT_SYMBOL_GPL(of_pwm_single_xlate);
455 
456 struct pwm_export {
457 	struct device pwm_dev;
458 	struct pwm_device *pwm;
459 	struct mutex lock;
460 	struct pwm_state suspend;
461 };
462 
463 static inline struct pwm_chip *pwmchip_from_dev(struct device *pwmchip_dev)
464 {
465 	return container_of(pwmchip_dev, struct pwm_chip, dev);
466 }
467 
468 static inline struct pwm_export *pwmexport_from_dev(struct device *pwm_dev)
469 {
470 	return container_of(pwm_dev, struct pwm_export, pwm_dev);
471 }
472 
473 static inline struct pwm_device *pwm_from_dev(struct device *pwm_dev)
474 {
475 	struct pwm_export *export = pwmexport_from_dev(pwm_dev);
476 
477 	return export->pwm;
478 }
479 
480 static ssize_t period_show(struct device *pwm_dev,
481 			   struct device_attribute *attr,
482 			   char *buf)
483 {
484 	const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
485 	struct pwm_state state;
486 
487 	pwm_get_state(pwm, &state);
488 
489 	return sysfs_emit(buf, "%llu\n", state.period);
490 }
491 
492 static ssize_t period_store(struct device *pwm_dev,
493 			    struct device_attribute *attr,
494 			    const char *buf, size_t size)
495 {
496 	struct pwm_export *export = pwmexport_from_dev(pwm_dev);
497 	struct pwm_device *pwm = export->pwm;
498 	struct pwm_state state;
499 	u64 val;
500 	int ret;
501 
502 	ret = kstrtou64(buf, 0, &val);
503 	if (ret)
504 		return ret;
505 
506 	guard(mutex)(&export->lock);
507 
508 	pwm_get_state(pwm, &state);
509 	state.period = val;
510 	ret = pwm_apply_might_sleep(pwm, &state);
511 
512 	return ret ? : size;
513 }
514 
515 static ssize_t duty_cycle_show(struct device *pwm_dev,
516 			       struct device_attribute *attr,
517 			       char *buf)
518 {
519 	const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
520 	struct pwm_state state;
521 
522 	pwm_get_state(pwm, &state);
523 
524 	return sysfs_emit(buf, "%llu\n", state.duty_cycle);
525 }
526 
527 static ssize_t duty_cycle_store(struct device *pwm_dev,
528 				struct device_attribute *attr,
529 				const char *buf, size_t size)
530 {
531 	struct pwm_export *export = pwmexport_from_dev(pwm_dev);
532 	struct pwm_device *pwm = export->pwm;
533 	struct pwm_state state;
534 	u64 val;
535 	int ret;
536 
537 	ret = kstrtou64(buf, 0, &val);
538 	if (ret)
539 		return ret;
540 
541 	guard(mutex)(&export->lock);
542 
543 	pwm_get_state(pwm, &state);
544 	state.duty_cycle = val;
545 	ret = pwm_apply_might_sleep(pwm, &state);
546 
547 	return ret ? : size;
548 }
549 
550 static ssize_t enable_show(struct device *pwm_dev,
551 			   struct device_attribute *attr,
552 			   char *buf)
553 {
554 	const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
555 	struct pwm_state state;
556 
557 	pwm_get_state(pwm, &state);
558 
559 	return sysfs_emit(buf, "%d\n", state.enabled);
560 }
561 
562 static ssize_t enable_store(struct device *pwm_dev,
563 			    struct device_attribute *attr,
564 			    const char *buf, size_t size)
565 {
566 	struct pwm_export *export = pwmexport_from_dev(pwm_dev);
567 	struct pwm_device *pwm = export->pwm;
568 	struct pwm_state state;
569 	int val, ret;
570 
571 	ret = kstrtoint(buf, 0, &val);
572 	if (ret)
573 		return ret;
574 
575 	guard(mutex)(&export->lock);
576 
577 	pwm_get_state(pwm, &state);
578 
579 	switch (val) {
580 	case 0:
581 		state.enabled = false;
582 		break;
583 	case 1:
584 		state.enabled = true;
585 		break;
586 	default:
587 		return -EINVAL;
588 	}
589 
590 	ret = pwm_apply_might_sleep(pwm, &state);
591 
592 	return ret ? : size;
593 }
594 
595 static ssize_t polarity_show(struct device *pwm_dev,
596 			     struct device_attribute *attr,
597 			     char *buf)
598 {
599 	const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
600 	const char *polarity = "unknown";
601 	struct pwm_state state;
602 
603 	pwm_get_state(pwm, &state);
604 
605 	switch (state.polarity) {
606 	case PWM_POLARITY_NORMAL:
607 		polarity = "normal";
608 		break;
609 
610 	case PWM_POLARITY_INVERSED:
611 		polarity = "inversed";
612 		break;
613 	}
614 
615 	return sysfs_emit(buf, "%s\n", polarity);
616 }
617 
618 static ssize_t polarity_store(struct device *pwm_dev,
619 			      struct device_attribute *attr,
620 			      const char *buf, size_t size)
621 {
622 	struct pwm_export *export = pwmexport_from_dev(pwm_dev);
623 	struct pwm_device *pwm = export->pwm;
624 	enum pwm_polarity polarity;
625 	struct pwm_state state;
626 	int ret;
627 
628 	if (sysfs_streq(buf, "normal"))
629 		polarity = PWM_POLARITY_NORMAL;
630 	else if (sysfs_streq(buf, "inversed"))
631 		polarity = PWM_POLARITY_INVERSED;
632 	else
633 		return -EINVAL;
634 
635 	guard(mutex)(&export->lock);
636 
637 	pwm_get_state(pwm, &state);
638 	state.polarity = polarity;
639 	ret = pwm_apply_might_sleep(pwm, &state);
640 
641 	return ret ? : size;
642 }
643 
644 static ssize_t capture_show(struct device *pwm_dev,
645 			    struct device_attribute *attr,
646 			    char *buf)
647 {
648 	struct pwm_device *pwm = pwm_from_dev(pwm_dev);
649 	struct pwm_capture result;
650 	int ret;
651 
652 	ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ));
653 	if (ret)
654 		return ret;
655 
656 	return sysfs_emit(buf, "%u %u\n", result.period, result.duty_cycle);
657 }
658 
659 static DEVICE_ATTR_RW(period);
660 static DEVICE_ATTR_RW(duty_cycle);
661 static DEVICE_ATTR_RW(enable);
662 static DEVICE_ATTR_RW(polarity);
663 static DEVICE_ATTR_RO(capture);
664 
665 static struct attribute *pwm_attrs[] = {
666 	&dev_attr_period.attr,
667 	&dev_attr_duty_cycle.attr,
668 	&dev_attr_enable.attr,
669 	&dev_attr_polarity.attr,
670 	&dev_attr_capture.attr,
671 	NULL
672 };
673 ATTRIBUTE_GROUPS(pwm);
674 
675 static void pwm_export_release(struct device *pwm_dev)
676 {
677 	struct pwm_export *export = pwmexport_from_dev(pwm_dev);
678 
679 	kfree(export);
680 }
681 
682 static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm)
683 {
684 	struct pwm_export *export;
685 	char *pwm_prop[2];
686 	int ret;
687 
688 	if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
689 		return -EBUSY;
690 
691 	export = kzalloc(sizeof(*export), GFP_KERNEL);
692 	if (!export) {
693 		clear_bit(PWMF_EXPORTED, &pwm->flags);
694 		return -ENOMEM;
695 	}
696 
697 	export->pwm = pwm;
698 	mutex_init(&export->lock);
699 
700 	export->pwm_dev.release = pwm_export_release;
701 	export->pwm_dev.parent = pwmchip_dev;
702 	export->pwm_dev.devt = MKDEV(0, 0);
703 	export->pwm_dev.groups = pwm_groups;
704 	dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm);
705 
706 	ret = device_register(&export->pwm_dev);
707 	if (ret) {
708 		clear_bit(PWMF_EXPORTED, &pwm->flags);
709 		put_device(&export->pwm_dev);
710 		export = NULL;
711 		return ret;
712 	}
713 	pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm);
714 	pwm_prop[1] = NULL;
715 	kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
716 	kfree(pwm_prop[0]);
717 
718 	return 0;
719 }
720 
721 static int pwm_unexport_match(struct device *pwm_dev, void *data)
722 {
723 	return pwm_from_dev(pwm_dev) == data;
724 }
725 
726 static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm)
727 {
728 	struct device *pwm_dev;
729 	char *pwm_prop[2];
730 
731 	if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
732 		return -ENODEV;
733 
734 	pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
735 	if (!pwm_dev)
736 		return -ENODEV;
737 
738 	pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm);
739 	pwm_prop[1] = NULL;
740 	kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
741 	kfree(pwm_prop[0]);
742 
743 	/* for device_find_child() */
744 	put_device(pwm_dev);
745 	device_unregister(pwm_dev);
746 	pwm_put(pwm);
747 
748 	return 0;
749 }
750 
751 static ssize_t export_store(struct device *pwmchip_dev,
752 			    struct device_attribute *attr,
753 			    const char *buf, size_t len)
754 {
755 	struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
756 	struct pwm_device *pwm;
757 	unsigned int hwpwm;
758 	int ret;
759 
760 	ret = kstrtouint(buf, 0, &hwpwm);
761 	if (ret < 0)
762 		return ret;
763 
764 	if (hwpwm >= chip->npwm)
765 		return -ENODEV;
766 
767 	pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
768 	if (IS_ERR(pwm))
769 		return PTR_ERR(pwm);
770 
771 	ret = pwm_export_child(pwmchip_dev, pwm);
772 	if (ret < 0)
773 		pwm_put(pwm);
774 
775 	return ret ? : len;
776 }
777 static DEVICE_ATTR_WO(export);
778 
779 static ssize_t unexport_store(struct device *pwmchip_dev,
780 			      struct device_attribute *attr,
781 			      const char *buf, size_t len)
782 {
783 	struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
784 	unsigned int hwpwm;
785 	int ret;
786 
787 	ret = kstrtouint(buf, 0, &hwpwm);
788 	if (ret < 0)
789 		return ret;
790 
791 	if (hwpwm >= chip->npwm)
792 		return -ENODEV;
793 
794 	ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]);
795 
796 	return ret ? : len;
797 }
798 static DEVICE_ATTR_WO(unexport);
799 
800 static ssize_t npwm_show(struct device *pwmchip_dev, struct device_attribute *attr,
801 			 char *buf)
802 {
803 	const struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
804 
805 	return sysfs_emit(buf, "%u\n", chip->npwm);
806 }
807 static DEVICE_ATTR_RO(npwm);
808 
809 static struct attribute *pwm_chip_attrs[] = {
810 	&dev_attr_export.attr,
811 	&dev_attr_unexport.attr,
812 	&dev_attr_npwm.attr,
813 	NULL,
814 };
815 ATTRIBUTE_GROUPS(pwm_chip);
816 
817 /* takes export->lock on success */
818 static struct pwm_export *pwm_class_get_state(struct device *pwmchip_dev,
819 					      struct pwm_device *pwm,
820 					      struct pwm_state *state)
821 {
822 	struct device *pwm_dev;
823 	struct pwm_export *export;
824 
825 	if (!test_bit(PWMF_EXPORTED, &pwm->flags))
826 		return NULL;
827 
828 	pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
829 	if (!pwm_dev)
830 		return NULL;
831 
832 	export = pwmexport_from_dev(pwm_dev);
833 	put_device(pwm_dev);	/* for device_find_child() */
834 
835 	mutex_lock(&export->lock);
836 	pwm_get_state(pwm, state);
837 
838 	return export;
839 }
840 
841 static int pwm_class_apply_state(struct pwm_export *export,
842 				 struct pwm_device *pwm,
843 				 struct pwm_state *state)
844 {
845 	int ret = pwm_apply_might_sleep(pwm, state);
846 
847 	/* release lock taken in pwm_class_get_state */
848 	mutex_unlock(&export->lock);
849 
850 	return ret;
851 }
852 
853 static int pwm_class_resume_npwm(struct device *pwmchip_dev, unsigned int npwm)
854 {
855 	struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
856 	unsigned int i;
857 	int ret = 0;
858 
859 	for (i = 0; i < npwm; i++) {
860 		struct pwm_device *pwm = &chip->pwms[i];
861 		struct pwm_state state;
862 		struct pwm_export *export;
863 
864 		export = pwm_class_get_state(pwmchip_dev, pwm, &state);
865 		if (!export)
866 			continue;
867 
868 		/* If pwmchip was not enabled before suspend, do nothing. */
869 		if (!export->suspend.enabled) {
870 			/* release lock taken in pwm_class_get_state */
871 			mutex_unlock(&export->lock);
872 			continue;
873 		}
874 
875 		state.enabled = export->suspend.enabled;
876 		ret = pwm_class_apply_state(export, pwm, &state);
877 		if (ret < 0)
878 			break;
879 	}
880 
881 	return ret;
882 }
883 
884 static int pwm_class_suspend(struct device *pwmchip_dev)
885 {
886 	struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
887 	unsigned int i;
888 	int ret = 0;
889 
890 	for (i = 0; i < chip->npwm; i++) {
891 		struct pwm_device *pwm = &chip->pwms[i];
892 		struct pwm_state state;
893 		struct pwm_export *export;
894 
895 		export = pwm_class_get_state(pwmchip_dev, pwm, &state);
896 		if (!export)
897 			continue;
898 
899 		/*
900 		 * If pwmchip was not enabled before suspend, save
901 		 * state for resume time and do nothing else.
902 		 */
903 		export->suspend = state;
904 		if (!state.enabled) {
905 			/* release lock taken in pwm_class_get_state */
906 			mutex_unlock(&export->lock);
907 			continue;
908 		}
909 
910 		state.enabled = false;
911 		ret = pwm_class_apply_state(export, pwm, &state);
912 		if (ret < 0) {
913 			/*
914 			 * roll back the PWM devices that were disabled by
915 			 * this suspend function.
916 			 */
917 			pwm_class_resume_npwm(pwmchip_dev, i);
918 			break;
919 		}
920 	}
921 
922 	return ret;
923 }
924 
925 static int pwm_class_resume(struct device *pwmchip_dev)
926 {
927 	struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
928 
929 	return pwm_class_resume_npwm(pwmchip_dev, chip->npwm);
930 }
931 
932 static DEFINE_SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume);
933 
934 static struct class pwm_class = {
935 	.name = "pwm",
936 	.dev_groups = pwm_chip_groups,
937 	.pm = pm_sleep_ptr(&pwm_class_pm_ops),
938 };
939 
940 static void pwmchip_sysfs_unexport(struct pwm_chip *chip)
941 {
942 	unsigned int i;
943 
944 	for (i = 0; i < chip->npwm; i++) {
945 		struct pwm_device *pwm = &chip->pwms[i];
946 
947 		if (test_bit(PWMF_EXPORTED, &pwm->flags))
948 			pwm_unexport_child(&chip->dev, pwm);
949 	}
950 }
951 
952 #define PWMCHIP_ALIGN ARCH_DMA_MINALIGN
953 
954 static void *pwmchip_priv(struct pwm_chip *chip)
955 {
956 	return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN);
957 }
958 
959 /* This is the counterpart to pwmchip_alloc() */
960 void pwmchip_put(struct pwm_chip *chip)
961 {
962 	put_device(&chip->dev);
963 }
964 EXPORT_SYMBOL_GPL(pwmchip_put);
965 
966 static void pwmchip_release(struct device *pwmchip_dev)
967 {
968 	struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
969 
970 	kfree(chip);
971 }
972 
973 struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
974 {
975 	struct pwm_chip *chip;
976 	struct device *pwmchip_dev;
977 	size_t alloc_size;
978 	unsigned int i;
979 
980 	alloc_size = size_add(ALIGN(struct_size(chip, pwms, npwm), PWMCHIP_ALIGN),
981 			      sizeof_priv);
982 
983 	chip = kzalloc(alloc_size, GFP_KERNEL);
984 	if (!chip)
985 		return ERR_PTR(-ENOMEM);
986 
987 	chip->npwm = npwm;
988 	chip->uses_pwmchip_alloc = true;
989 
990 	pwmchip_dev = &chip->dev;
991 	device_initialize(pwmchip_dev);
992 	pwmchip_dev->class = &pwm_class;
993 	pwmchip_dev->parent = parent;
994 	pwmchip_dev->release = pwmchip_release;
995 
996 	pwmchip_set_drvdata(chip, pwmchip_priv(chip));
997 
998 	for (i = 0; i < chip->npwm; i++) {
999 		struct pwm_device *pwm = &chip->pwms[i];
1000 		pwm->chip = chip;
1001 		pwm->hwpwm = i;
1002 	}
1003 
1004 	return chip;
1005 }
1006 EXPORT_SYMBOL_GPL(pwmchip_alloc);
1007 
1008 static void devm_pwmchip_put(void *data)
1009 {
1010 	struct pwm_chip *chip = data;
1011 
1012 	pwmchip_put(chip);
1013 }
1014 
1015 struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
1016 {
1017 	struct pwm_chip *chip;
1018 	int ret;
1019 
1020 	chip = pwmchip_alloc(parent, npwm, sizeof_priv);
1021 	if (IS_ERR(chip))
1022 		return chip;
1023 
1024 	ret = devm_add_action_or_reset(parent, devm_pwmchip_put, chip);
1025 	if (ret)
1026 		return ERR_PTR(ret);
1027 
1028 	return chip;
1029 }
1030 EXPORT_SYMBOL_GPL(devm_pwmchip_alloc);
1031 
1032 static void of_pwmchip_add(struct pwm_chip *chip)
1033 {
1034 	if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node)
1035 		return;
1036 
1037 	if (!chip->of_xlate)
1038 		chip->of_xlate = of_pwm_xlate_with_flags;
1039 
1040 	of_node_get(pwmchip_parent(chip)->of_node);
1041 }
1042 
1043 static void of_pwmchip_remove(struct pwm_chip *chip)
1044 {
1045 	if (pwmchip_parent(chip))
1046 		of_node_put(pwmchip_parent(chip)->of_node);
1047 }
1048 
1049 static bool pwm_ops_check(const struct pwm_chip *chip)
1050 {
1051 	const struct pwm_ops *ops = chip->ops;
1052 
1053 	if (!ops->apply)
1054 		return false;
1055 
1056 	if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
1057 		dev_warn(pwmchip_parent(chip),
1058 			 "Please implement the .get_state() callback\n");
1059 
1060 	return true;
1061 }
1062 
1063 /**
1064  * __pwmchip_add() - register a new PWM chip
1065  * @chip: the PWM chip to add
1066  * @owner: reference to the module providing the chip.
1067  *
1068  * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
1069  * pwmchip_add wrapper to do this right.
1070  *
1071  * Returns: 0 on success or a negative error code on failure.
1072  */
1073 int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
1074 {
1075 	int ret;
1076 
1077 	if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm)
1078 		return -EINVAL;
1079 
1080 	/*
1081 	 * a struct pwm_chip must be allocated using (devm_)pwmchip_alloc,
1082 	 * otherwise the embedded struct device might disappear too early
1083 	 * resulting in memory corruption.
1084 	 * Catch drivers that were not converted appropriately.
1085 	 */
1086 	if (!chip->uses_pwmchip_alloc)
1087 		return -EINVAL;
1088 
1089 	if (!pwm_ops_check(chip))
1090 		return -EINVAL;
1091 
1092 	chip->owner = owner;
1093 
1094 	guard(mutex)(&pwm_lock);
1095 
1096 	ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL);
1097 	if (ret < 0)
1098 		return ret;
1099 
1100 	chip->id = ret;
1101 
1102 	dev_set_name(&chip->dev, "pwmchip%u", chip->id);
1103 
1104 	if (IS_ENABLED(CONFIG_OF))
1105 		of_pwmchip_add(chip);
1106 
1107 	ret = device_add(&chip->dev);
1108 	if (ret)
1109 		goto err_device_add;
1110 
1111 	return 0;
1112 
1113 err_device_add:
1114 	if (IS_ENABLED(CONFIG_OF))
1115 		of_pwmchip_remove(chip);
1116 
1117 	idr_remove(&pwm_chips, chip->id);
1118 
1119 	return ret;
1120 }
1121 EXPORT_SYMBOL_GPL(__pwmchip_add);
1122 
1123 /**
1124  * pwmchip_remove() - remove a PWM chip
1125  * @chip: the PWM chip to remove
1126  *
1127  * Removes a PWM chip.
1128  */
1129 void pwmchip_remove(struct pwm_chip *chip)
1130 {
1131 	pwmchip_sysfs_unexport(chip);
1132 
1133 	if (IS_ENABLED(CONFIG_OF))
1134 		of_pwmchip_remove(chip);
1135 
1136 	scoped_guard(mutex, &pwm_lock)
1137 		idr_remove(&pwm_chips, chip->id);
1138 
1139 	device_del(&chip->dev);
1140 }
1141 EXPORT_SYMBOL_GPL(pwmchip_remove);
1142 
1143 static void devm_pwmchip_remove(void *data)
1144 {
1145 	struct pwm_chip *chip = data;
1146 
1147 	pwmchip_remove(chip);
1148 }
1149 
1150 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner)
1151 {
1152 	int ret;
1153 
1154 	ret = __pwmchip_add(chip, owner);
1155 	if (ret)
1156 		return ret;
1157 
1158 	return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
1159 }
1160 EXPORT_SYMBOL_GPL(__devm_pwmchip_add);
1161 
1162 static struct device_link *pwm_device_link_add(struct device *dev,
1163 					       struct pwm_device *pwm)
1164 {
1165 	struct device_link *dl;
1166 
1167 	if (!dev) {
1168 		/*
1169 		 * No device for the PWM consumer has been provided. It may
1170 		 * impact the PM sequence ordering: the PWM supplier may get
1171 		 * suspended before the consumer.
1172 		 */
1173 		dev_warn(pwmchip_parent(pwm->chip),
1174 			 "No consumer device specified to create a link to\n");
1175 		return NULL;
1176 	}
1177 
1178 	dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER);
1179 	if (!dl) {
1180 		dev_err(dev, "failed to create device link to %s\n",
1181 			dev_name(pwmchip_parent(pwm->chip)));
1182 		return ERR_PTR(-EINVAL);
1183 	}
1184 
1185 	return dl;
1186 }
1187 
1188 static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
1189 {
1190 	struct pwm_chip *chip;
1191 	unsigned long id, tmp;
1192 
1193 	guard(mutex)(&pwm_lock);
1194 
1195 	idr_for_each_entry_ul(&pwm_chips, chip, tmp, id)
1196 		if (pwmchip_parent(chip) && device_match_fwnode(pwmchip_parent(chip), fwnode))
1197 			return chip;
1198 
1199 	return ERR_PTR(-EPROBE_DEFER);
1200 }
1201 
1202 /**
1203  * of_pwm_get() - request a PWM via the PWM framework
1204  * @dev: device for PWM consumer
1205  * @np: device node to get the PWM from
1206  * @con_id: consumer name
1207  *
1208  * Returns the PWM device parsed from the phandle and index specified in the
1209  * "pwms" property of a device tree node or a negative error-code on failure.
1210  * Values parsed from the device tree are stored in the returned PWM device
1211  * object.
1212  *
1213  * If con_id is NULL, the first PWM device listed in the "pwms" property will
1214  * be requested. Otherwise the "pwm-names" property is used to do a reverse
1215  * lookup of the PWM index. This also means that the "pwm-names" property
1216  * becomes mandatory for devices that look up the PWM device via the con_id
1217  * parameter.
1218  *
1219  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1220  * error code on failure.
1221  */
1222 static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
1223 				     const char *con_id)
1224 {
1225 	struct pwm_device *pwm = NULL;
1226 	struct of_phandle_args args;
1227 	struct device_link *dl;
1228 	struct pwm_chip *chip;
1229 	int index = 0;
1230 	int err;
1231 
1232 	if (con_id) {
1233 		index = of_property_match_string(np, "pwm-names", con_id);
1234 		if (index < 0)
1235 			return ERR_PTR(index);
1236 	}
1237 
1238 	err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
1239 					 &args);
1240 	if (err) {
1241 		pr_err("%s(): can't parse \"pwms\" property\n", __func__);
1242 		return ERR_PTR(err);
1243 	}
1244 
1245 	chip = fwnode_to_pwmchip(of_fwnode_handle(args.np));
1246 	if (IS_ERR(chip)) {
1247 		if (PTR_ERR(chip) != -EPROBE_DEFER)
1248 			pr_err("%s(): PWM chip not found\n", __func__);
1249 
1250 		pwm = ERR_CAST(chip);
1251 		goto put;
1252 	}
1253 
1254 	pwm = chip->of_xlate(chip, &args);
1255 	if (IS_ERR(pwm))
1256 		goto put;
1257 
1258 	dl = pwm_device_link_add(dev, pwm);
1259 	if (IS_ERR(dl)) {
1260 		/* of_xlate ended up calling pwm_request_from_chip() */
1261 		pwm_put(pwm);
1262 		pwm = ERR_CAST(dl);
1263 		goto put;
1264 	}
1265 
1266 	/*
1267 	 * If a consumer name was not given, try to look it up from the
1268 	 * "pwm-names" property if it exists. Otherwise use the name of
1269 	 * the user device node.
1270 	 */
1271 	if (!con_id) {
1272 		err = of_property_read_string_index(np, "pwm-names", index,
1273 						    &con_id);
1274 		if (err < 0)
1275 			con_id = np->name;
1276 	}
1277 
1278 	pwm->label = con_id;
1279 
1280 put:
1281 	of_node_put(args.np);
1282 
1283 	return pwm;
1284 }
1285 
1286 /**
1287  * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
1288  * @fwnode: firmware node to get the "pwms" property from
1289  *
1290  * Returns the PWM device parsed from the fwnode and index specified in the
1291  * "pwms" property or a negative error-code on failure.
1292  * Values parsed from the device tree are stored in the returned PWM device
1293  * object.
1294  *
1295  * This is analogous to of_pwm_get() except con_id is not yet supported.
1296  * ACPI entries must look like
1297  * Package () {"pwms", Package ()
1298  *     { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
1299  *
1300  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1301  * error code on failure.
1302  */
1303 static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode)
1304 {
1305 	struct pwm_device *pwm;
1306 	struct fwnode_reference_args args;
1307 	struct pwm_chip *chip;
1308 	int ret;
1309 
1310 	memset(&args, 0, sizeof(args));
1311 
1312 	ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
1313 	if (ret < 0)
1314 		return ERR_PTR(ret);
1315 
1316 	if (args.nargs < 2)
1317 		return ERR_PTR(-EPROTO);
1318 
1319 	chip = fwnode_to_pwmchip(args.fwnode);
1320 	if (IS_ERR(chip))
1321 		return ERR_CAST(chip);
1322 
1323 	pwm = pwm_request_from_chip(chip, args.args[0], NULL);
1324 	if (IS_ERR(pwm))
1325 		return pwm;
1326 
1327 	pwm->args.period = args.args[1];
1328 	pwm->args.polarity = PWM_POLARITY_NORMAL;
1329 
1330 	if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
1331 		pwm->args.polarity = PWM_POLARITY_INVERSED;
1332 
1333 	return pwm;
1334 }
1335 
1336 static DEFINE_MUTEX(pwm_lookup_lock);
1337 static LIST_HEAD(pwm_lookup_list);
1338 
1339 /**
1340  * pwm_add_table() - register PWM device consumers
1341  * @table: array of consumers to register
1342  * @num: number of consumers in table
1343  */
1344 void pwm_add_table(struct pwm_lookup *table, size_t num)
1345 {
1346 	guard(mutex)(&pwm_lookup_lock);
1347 
1348 	while (num--) {
1349 		list_add_tail(&table->list, &pwm_lookup_list);
1350 		table++;
1351 	}
1352 }
1353 
1354 /**
1355  * pwm_remove_table() - unregister PWM device consumers
1356  * @table: array of consumers to unregister
1357  * @num: number of consumers in table
1358  */
1359 void pwm_remove_table(struct pwm_lookup *table, size_t num)
1360 {
1361 	guard(mutex)(&pwm_lookup_lock);
1362 
1363 	while (num--) {
1364 		list_del(&table->list);
1365 		table++;
1366 	}
1367 }
1368 
1369 /**
1370  * pwm_get() - look up and request a PWM device
1371  * @dev: device for PWM consumer
1372  * @con_id: consumer name
1373  *
1374  * Lookup is first attempted using DT. If the device was not instantiated from
1375  * a device tree, a PWM chip and a relative index is looked up via a table
1376  * supplied by board setup code (see pwm_add_table()).
1377  *
1378  * Once a PWM chip has been found the specified PWM device will be requested
1379  * and is ready to be used.
1380  *
1381  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1382  * error code on failure.
1383  */
1384 struct pwm_device *pwm_get(struct device *dev, const char *con_id)
1385 {
1386 	const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
1387 	const char *dev_id = dev ? dev_name(dev) : NULL;
1388 	struct pwm_device *pwm;
1389 	struct pwm_chip *chip;
1390 	struct device_link *dl;
1391 	unsigned int best = 0;
1392 	struct pwm_lookup *p, *chosen = NULL;
1393 	unsigned int match;
1394 	int err;
1395 
1396 	/* look up via DT first */
1397 	if (is_of_node(fwnode))
1398 		return of_pwm_get(dev, to_of_node(fwnode), con_id);
1399 
1400 	/* then lookup via ACPI */
1401 	if (is_acpi_node(fwnode)) {
1402 		pwm = acpi_pwm_get(fwnode);
1403 		if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
1404 			return pwm;
1405 	}
1406 
1407 	/*
1408 	 * We look up the provider in the static table typically provided by
1409 	 * board setup code. We first try to lookup the consumer device by
1410 	 * name. If the consumer device was passed in as NULL or if no match
1411 	 * was found, we try to find the consumer by directly looking it up
1412 	 * by name.
1413 	 *
1414 	 * If a match is found, the provider PWM chip is looked up by name
1415 	 * and a PWM device is requested using the PWM device per-chip index.
1416 	 *
1417 	 * The lookup algorithm was shamelessly taken from the clock
1418 	 * framework:
1419 	 *
1420 	 * We do slightly fuzzy matching here:
1421 	 *  An entry with a NULL ID is assumed to be a wildcard.
1422 	 *  If an entry has a device ID, it must match
1423 	 *  If an entry has a connection ID, it must match
1424 	 * Then we take the most specific entry - with the following order
1425 	 * of precedence: dev+con > dev only > con only.
1426 	 */
1427 	scoped_guard(mutex, &pwm_lookup_lock)
1428 		list_for_each_entry(p, &pwm_lookup_list, list) {
1429 			match = 0;
1430 
1431 			if (p->dev_id) {
1432 				if (!dev_id || strcmp(p->dev_id, dev_id))
1433 					continue;
1434 
1435 				match += 2;
1436 			}
1437 
1438 			if (p->con_id) {
1439 				if (!con_id || strcmp(p->con_id, con_id))
1440 					continue;
1441 
1442 				match += 1;
1443 			}
1444 
1445 			if (match > best) {
1446 				chosen = p;
1447 
1448 				if (match != 3)
1449 					best = match;
1450 				else
1451 					break;
1452 			}
1453 		}
1454 
1455 	if (!chosen)
1456 		return ERR_PTR(-ENODEV);
1457 
1458 	chip = pwmchip_find_by_name(chosen->provider);
1459 
1460 	/*
1461 	 * If the lookup entry specifies a module, load the module and retry
1462 	 * the PWM chip lookup. This can be used to work around driver load
1463 	 * ordering issues if driver's can't be made to properly support the
1464 	 * deferred probe mechanism.
1465 	 */
1466 	if (!chip && chosen->module) {
1467 		err = request_module(chosen->module);
1468 		if (err == 0)
1469 			chip = pwmchip_find_by_name(chosen->provider);
1470 	}
1471 
1472 	if (!chip)
1473 		return ERR_PTR(-EPROBE_DEFER);
1474 
1475 	pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
1476 	if (IS_ERR(pwm))
1477 		return pwm;
1478 
1479 	dl = pwm_device_link_add(dev, pwm);
1480 	if (IS_ERR(dl)) {
1481 		pwm_put(pwm);
1482 		return ERR_CAST(dl);
1483 	}
1484 
1485 	pwm->args.period = chosen->period;
1486 	pwm->args.polarity = chosen->polarity;
1487 
1488 	return pwm;
1489 }
1490 EXPORT_SYMBOL_GPL(pwm_get);
1491 
1492 /**
1493  * pwm_put() - release a PWM device
1494  * @pwm: PWM device
1495  */
1496 void pwm_put(struct pwm_device *pwm)
1497 {
1498 	struct pwm_chip *chip;
1499 
1500 	if (!pwm)
1501 		return;
1502 
1503 	chip = pwm->chip;
1504 
1505 	guard(mutex)(&pwm_lock);
1506 
1507 	if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
1508 		pr_warn("PWM device already freed\n");
1509 		return;
1510 	}
1511 
1512 	if (chip->ops->free)
1513 		pwm->chip->ops->free(pwm->chip, pwm);
1514 
1515 	pwm->label = NULL;
1516 
1517 	put_device(&chip->dev);
1518 
1519 	module_put(chip->owner);
1520 }
1521 EXPORT_SYMBOL_GPL(pwm_put);
1522 
1523 static void devm_pwm_release(void *pwm)
1524 {
1525 	pwm_put(pwm);
1526 }
1527 
1528 /**
1529  * devm_pwm_get() - resource managed pwm_get()
1530  * @dev: device for PWM consumer
1531  * @con_id: consumer name
1532  *
1533  * This function performs like pwm_get() but the acquired PWM device will
1534  * automatically be released on driver detach.
1535  *
1536  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1537  * error code on failure.
1538  */
1539 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
1540 {
1541 	struct pwm_device *pwm;
1542 	int ret;
1543 
1544 	pwm = pwm_get(dev, con_id);
1545 	if (IS_ERR(pwm))
1546 		return pwm;
1547 
1548 	ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1549 	if (ret)
1550 		return ERR_PTR(ret);
1551 
1552 	return pwm;
1553 }
1554 EXPORT_SYMBOL_GPL(devm_pwm_get);
1555 
1556 /**
1557  * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1558  * @dev: device for PWM consumer
1559  * @fwnode: firmware node to get the PWM from
1560  * @con_id: consumer name
1561  *
1562  * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1563  * acpi_pwm_get() for a detailed description.
1564  *
1565  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1566  * error code on failure.
1567  */
1568 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
1569 				       struct fwnode_handle *fwnode,
1570 				       const char *con_id)
1571 {
1572 	struct pwm_device *pwm = ERR_PTR(-ENODEV);
1573 	int ret;
1574 
1575 	if (is_of_node(fwnode))
1576 		pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
1577 	else if (is_acpi_node(fwnode))
1578 		pwm = acpi_pwm_get(fwnode);
1579 	if (IS_ERR(pwm))
1580 		return pwm;
1581 
1582 	ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1583 	if (ret)
1584 		return ERR_PTR(ret);
1585 
1586 	return pwm;
1587 }
1588 EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
1589 
1590 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
1591 {
1592 	unsigned int i;
1593 
1594 	for (i = 0; i < chip->npwm; i++) {
1595 		struct pwm_device *pwm = &chip->pwms[i];
1596 		struct pwm_state state;
1597 
1598 		pwm_get_state(pwm, &state);
1599 
1600 		seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
1601 
1602 		if (test_bit(PWMF_REQUESTED, &pwm->flags))
1603 			seq_puts(s, " requested");
1604 
1605 		if (state.enabled)
1606 			seq_puts(s, " enabled");
1607 
1608 		seq_printf(s, " period: %llu ns", state.period);
1609 		seq_printf(s, " duty: %llu ns", state.duty_cycle);
1610 		seq_printf(s, " polarity: %s",
1611 			   state.polarity ? "inverse" : "normal");
1612 
1613 		if (state.usage_power)
1614 			seq_puts(s, " usage_power");
1615 
1616 		seq_puts(s, "\n");
1617 	}
1618 }
1619 
1620 static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
1621 {
1622 	unsigned long id = *pos;
1623 	void *ret;
1624 
1625 	mutex_lock(&pwm_lock);
1626 	s->private = "";
1627 
1628 	ret = idr_get_next_ul(&pwm_chips, &id);
1629 	*pos = id;
1630 	return ret;
1631 }
1632 
1633 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
1634 {
1635 	unsigned long id = *pos + 1;
1636 	void *ret;
1637 
1638 	s->private = "\n";
1639 
1640 	ret = idr_get_next_ul(&pwm_chips, &id);
1641 	*pos = id;
1642 	return ret;
1643 }
1644 
1645 static void pwm_seq_stop(struct seq_file *s, void *v)
1646 {
1647 	mutex_unlock(&pwm_lock);
1648 }
1649 
1650 static int pwm_seq_show(struct seq_file *s, void *v)
1651 {
1652 	struct pwm_chip *chip = v;
1653 
1654 	seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n",
1655 		   (char *)s->private, chip->id,
1656 		   pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus",
1657 		   dev_name(pwmchip_parent(chip)), chip->npwm,
1658 		   (chip->npwm != 1) ? "s" : "");
1659 
1660 	pwm_dbg_show(chip, s);
1661 
1662 	return 0;
1663 }
1664 
1665 static const struct seq_operations pwm_debugfs_sops = {
1666 	.start = pwm_seq_start,
1667 	.next = pwm_seq_next,
1668 	.stop = pwm_seq_stop,
1669 	.show = pwm_seq_show,
1670 };
1671 
1672 DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
1673 
1674 static int __init pwm_init(void)
1675 {
1676 	int ret;
1677 
1678 	ret = class_register(&pwm_class);
1679 	if (ret) {
1680 		pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret));
1681 		return ret;
1682 	}
1683 
1684 	if (IS_ENABLED(CONFIG_DEBUG_FS))
1685 		debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops);
1686 
1687 	return 0;
1688 }
1689 subsys_initcall(pwm_init);
1690