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