]> Git Repo - J-linux.git/blob - drivers/pwm/core.c
pwm: Manage owner assignment implicitly for drivers
[J-linux.git] / drivers / pwm / core.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Generic pwmlib implementation
4  *
5  * Copyright (C) 2011 Sascha Hauer <[email protected]>
6  * Copyright (C) 2011-2012 Avionic Design GmbH
7  */
8
9 #include <linux/acpi.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/pwm.h>
13 #include <linux/list.h>
14 #include <linux/mutex.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/device.h>
18 #include <linux/debugfs.h>
19 #include <linux/seq_file.h>
20
21 #include <dt-bindings/pwm/pwm.h>
22
23 #define CREATE_TRACE_POINTS
24 #include <trace/events/pwm.h>
25
26 #define MAX_PWMS 1024
27
28 static DEFINE_MUTEX(pwm_lookup_lock);
29 static LIST_HEAD(pwm_lookup_list);
30
31 /* protects access to pwm_chips and allocated_pwms */
32 static DEFINE_MUTEX(pwm_lock);
33
34 static LIST_HEAD(pwm_chips);
35 static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
36
37 /* Called with pwm_lock held */
38 static int alloc_pwms(unsigned int count)
39 {
40         unsigned int start;
41
42         start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, 0,
43                                            count, 0);
44
45         if (start + count > MAX_PWMS)
46                 return -ENOSPC;
47
48         bitmap_set(allocated_pwms, start, count);
49
50         return start;
51 }
52
53 /* Called with pwm_lock held */
54 static void free_pwms(struct pwm_chip *chip)
55 {
56         bitmap_clear(allocated_pwms, chip->base, chip->npwm);
57
58         kfree(chip->pwms);
59         chip->pwms = NULL;
60 }
61
62 static struct pwm_chip *pwmchip_find_by_name(const char *name)
63 {
64         struct pwm_chip *chip;
65
66         if (!name)
67                 return NULL;
68
69         mutex_lock(&pwm_lock);
70
71         list_for_each_entry(chip, &pwm_chips, list) {
72                 const char *chip_name = dev_name(chip->dev);
73
74                 if (chip_name && strcmp(chip_name, name) == 0) {
75                         mutex_unlock(&pwm_lock);
76                         return chip;
77                 }
78         }
79
80         mutex_unlock(&pwm_lock);
81
82         return NULL;
83 }
84
85 static int pwm_device_request(struct pwm_device *pwm, const char *label)
86 {
87         int err;
88
89         if (test_bit(PWMF_REQUESTED, &pwm->flags))
90                 return -EBUSY;
91
92         if (!try_module_get(pwm->chip->owner))
93                 return -ENODEV;
94
95         if (pwm->chip->ops->request) {
96                 err = pwm->chip->ops->request(pwm->chip, pwm);
97                 if (err) {
98                         module_put(pwm->chip->owner);
99                         return err;
100                 }
101         }
102
103         if (pwm->chip->ops->get_state) {
104                 /*
105                  * Zero-initialize state because most drivers are unaware of
106                  * .usage_power. The other members of state are supposed to be
107                  * set by lowlevel drivers. We still initialize the whole
108                  * structure for simplicity even though this might paper over
109                  * faulty implementations of .get_state().
110                  */
111                 struct pwm_state state = { 0, };
112
113                 err = pwm->chip->ops->get_state(pwm->chip, pwm, &state);
114                 trace_pwm_get(pwm, &state, err);
115
116                 if (!err)
117                         pwm->state = state;
118
119                 if (IS_ENABLED(CONFIG_PWM_DEBUG))
120                         pwm->last = pwm->state;
121         }
122
123         set_bit(PWMF_REQUESTED, &pwm->flags);
124         pwm->label = label;
125
126         return 0;
127 }
128
129 struct pwm_device *
130 of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args)
131 {
132         struct pwm_device *pwm;
133
134         if (chip->of_pwm_n_cells < 2)
135                 return ERR_PTR(-EINVAL);
136
137         /* flags in the third cell are optional */
138         if (args->args_count < 2)
139                 return ERR_PTR(-EINVAL);
140
141         if (args->args[0] >= chip->npwm)
142                 return ERR_PTR(-EINVAL);
143
144         pwm = pwm_request_from_chip(chip, args->args[0], NULL);
145         if (IS_ERR(pwm))
146                 return pwm;
147
148         pwm->args.period = args->args[1];
149         pwm->args.polarity = PWM_POLARITY_NORMAL;
150
151         if (chip->of_pwm_n_cells >= 3) {
152                 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
153                         pwm->args.polarity = PWM_POLARITY_INVERSED;
154         }
155
156         return pwm;
157 }
158 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
159
160 struct pwm_device *
161 of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args)
162 {
163         struct pwm_device *pwm;
164
165         if (chip->of_pwm_n_cells < 1)
166                 return ERR_PTR(-EINVAL);
167
168         /* validate that one cell is specified, optionally with flags */
169         if (args->args_count != 1 && args->args_count != 2)
170                 return ERR_PTR(-EINVAL);
171
172         pwm = pwm_request_from_chip(chip, 0, NULL);
173         if (IS_ERR(pwm))
174                 return pwm;
175
176         pwm->args.period = args->args[0];
177         pwm->args.polarity = PWM_POLARITY_NORMAL;
178
179         if (args->args_count == 2 && args->args[2] & PWM_POLARITY_INVERTED)
180                 pwm->args.polarity = PWM_POLARITY_INVERSED;
181
182         return pwm;
183 }
184 EXPORT_SYMBOL_GPL(of_pwm_single_xlate);
185
186 static void of_pwmchip_add(struct pwm_chip *chip)
187 {
188         if (!chip->dev || !chip->dev->of_node)
189                 return;
190
191         if (!chip->of_xlate) {
192                 u32 pwm_cells;
193
194                 if (of_property_read_u32(chip->dev->of_node, "#pwm-cells",
195                                          &pwm_cells))
196                         pwm_cells = 2;
197
198                 chip->of_xlate = of_pwm_xlate_with_flags;
199                 chip->of_pwm_n_cells = pwm_cells;
200         }
201
202         of_node_get(chip->dev->of_node);
203 }
204
205 static void of_pwmchip_remove(struct pwm_chip *chip)
206 {
207         if (chip->dev)
208                 of_node_put(chip->dev->of_node);
209 }
210
211 /**
212  * pwm_set_chip_data() - set private chip data for a PWM
213  * @pwm: PWM device
214  * @data: pointer to chip-specific data
215  *
216  * Returns: 0 on success or a negative error code on failure.
217  */
218 int pwm_set_chip_data(struct pwm_device *pwm, void *data)
219 {
220         if (!pwm)
221                 return -EINVAL;
222
223         pwm->chip_data = data;
224
225         return 0;
226 }
227 EXPORT_SYMBOL_GPL(pwm_set_chip_data);
228
229 /**
230  * pwm_get_chip_data() - get private chip data for a PWM
231  * @pwm: PWM device
232  *
233  * Returns: A pointer to the chip-private data for the PWM device.
234  */
235 void *pwm_get_chip_data(struct pwm_device *pwm)
236 {
237         return pwm ? pwm->chip_data : NULL;
238 }
239 EXPORT_SYMBOL_GPL(pwm_get_chip_data);
240
241 static bool pwm_ops_check(const struct pwm_chip *chip)
242 {
243         const struct pwm_ops *ops = chip->ops;
244
245         if (!ops->apply)
246                 return false;
247
248         if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
249                 dev_warn(chip->dev,
250                          "Please implement the .get_state() callback\n");
251
252         return true;
253 }
254
255 /**
256  * __pwmchip_add() - register a new PWM chip
257  * @chip: the PWM chip to add
258  * @owner: reference to the module providing the chip.
259  *
260  * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
261  * pwmchip_add wrapper to do this right.
262  *
263  * Returns: 0 on success or a negative error code on failure.
264  */
265 int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
266 {
267         struct pwm_device *pwm;
268         unsigned int i;
269         int ret;
270
271         if (!chip || !chip->dev || !chip->ops || !chip->npwm)
272                 return -EINVAL;
273
274         if (!pwm_ops_check(chip))
275                 return -EINVAL;
276
277         chip->owner = owner;
278
279         chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL);
280         if (!chip->pwms)
281                 return -ENOMEM;
282
283         mutex_lock(&pwm_lock);
284
285         ret = alloc_pwms(chip->npwm);
286         if (ret < 0) {
287                 mutex_unlock(&pwm_lock);
288                 kfree(chip->pwms);
289                 return ret;
290         }
291
292         chip->base = ret;
293
294         for (i = 0; i < chip->npwm; i++) {
295                 pwm = &chip->pwms[i];
296
297                 pwm->chip = chip;
298                 pwm->pwm = chip->base + i;
299                 pwm->hwpwm = i;
300         }
301
302         list_add(&chip->list, &pwm_chips);
303
304         mutex_unlock(&pwm_lock);
305
306         if (IS_ENABLED(CONFIG_OF))
307                 of_pwmchip_add(chip);
308
309         pwmchip_sysfs_export(chip);
310
311         return 0;
312 }
313 EXPORT_SYMBOL_GPL(__pwmchip_add);
314
315 /**
316  * pwmchip_remove() - remove a PWM chip
317  * @chip: the PWM chip to remove
318  *
319  * Removes a PWM chip.
320  */
321 void pwmchip_remove(struct pwm_chip *chip)
322 {
323         pwmchip_sysfs_unexport(chip);
324
325         if (IS_ENABLED(CONFIG_OF))
326                 of_pwmchip_remove(chip);
327
328         mutex_lock(&pwm_lock);
329
330         list_del_init(&chip->list);
331
332         free_pwms(chip);
333
334         mutex_unlock(&pwm_lock);
335 }
336 EXPORT_SYMBOL_GPL(pwmchip_remove);
337
338 static void devm_pwmchip_remove(void *data)
339 {
340         struct pwm_chip *chip = data;
341
342         pwmchip_remove(chip);
343 }
344
345 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner)
346 {
347         int ret;
348
349         ret = __pwmchip_add(chip, owner);
350         if (ret)
351                 return ret;
352
353         return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
354 }
355 EXPORT_SYMBOL_GPL(__devm_pwmchip_add);
356
357 /**
358  * pwm_request_from_chip() - request a PWM device relative to a PWM chip
359  * @chip: PWM chip
360  * @index: per-chip index of the PWM to request
361  * @label: a literal description string of this PWM
362  *
363  * Returns: A pointer to the PWM device at the given index of the given PWM
364  * chip. A negative error code is returned if the index is not valid for the
365  * specified PWM chip or if the PWM device cannot be requested.
366  */
367 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
368                                          unsigned int index,
369                                          const char *label)
370 {
371         struct pwm_device *pwm;
372         int err;
373
374         if (!chip || index >= chip->npwm)
375                 return ERR_PTR(-EINVAL);
376
377         mutex_lock(&pwm_lock);
378         pwm = &chip->pwms[index];
379
380         err = pwm_device_request(pwm, label);
381         if (err < 0)
382                 pwm = ERR_PTR(err);
383
384         mutex_unlock(&pwm_lock);
385         return pwm;
386 }
387 EXPORT_SYMBOL_GPL(pwm_request_from_chip);
388
389 static void pwm_apply_state_debug(struct pwm_device *pwm,
390                                   const struct pwm_state *state)
391 {
392         struct pwm_state *last = &pwm->last;
393         struct pwm_chip *chip = pwm->chip;
394         struct pwm_state s1 = { 0 }, s2 = { 0 };
395         int err;
396
397         if (!IS_ENABLED(CONFIG_PWM_DEBUG))
398                 return;
399
400         /* No reasonable diagnosis possible without .get_state() */
401         if (!chip->ops->get_state)
402                 return;
403
404         /*
405          * *state was just applied. Read out the hardware state and do some
406          * checks.
407          */
408
409         err = chip->ops->get_state(chip, pwm, &s1);
410         trace_pwm_get(pwm, &s1, err);
411         if (err)
412                 /* If that failed there isn't much to debug */
413                 return;
414
415         /*
416          * The lowlevel driver either ignored .polarity (which is a bug) or as
417          * best effort inverted .polarity and fixed .duty_cycle respectively.
418          * Undo this inversion and fixup for further tests.
419          */
420         if (s1.enabled && s1.polarity != state->polarity) {
421                 s2.polarity = state->polarity;
422                 s2.duty_cycle = s1.period - s1.duty_cycle;
423                 s2.period = s1.period;
424                 s2.enabled = s1.enabled;
425         } else {
426                 s2 = s1;
427         }
428
429         if (s2.polarity != state->polarity &&
430             state->duty_cycle < state->period)
431                 dev_warn(chip->dev, ".apply ignored .polarity\n");
432
433         if (state->enabled &&
434             last->polarity == state->polarity &&
435             last->period > s2.period &&
436             last->period <= state->period)
437                 dev_warn(chip->dev,
438                          ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
439                          state->period, s2.period, last->period);
440
441         if (state->enabled && state->period < s2.period)
442                 dev_warn(chip->dev,
443                          ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
444                          state->period, s2.period);
445
446         if (state->enabled &&
447             last->polarity == state->polarity &&
448             last->period == s2.period &&
449             last->duty_cycle > s2.duty_cycle &&
450             last->duty_cycle <= state->duty_cycle)
451                 dev_warn(chip->dev,
452                          ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
453                          state->duty_cycle, state->period,
454                          s2.duty_cycle, s2.period,
455                          last->duty_cycle, last->period);
456
457         if (state->enabled && state->duty_cycle < s2.duty_cycle)
458                 dev_warn(chip->dev,
459                          ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
460                          state->duty_cycle, state->period,
461                          s2.duty_cycle, s2.period);
462
463         if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
464                 dev_warn(chip->dev,
465                          "requested disabled, but yielded enabled with duty > 0\n");
466
467         /* reapply the state that the driver reported being configured. */
468         err = chip->ops->apply(chip, pwm, &s1);
469         trace_pwm_apply(pwm, &s1, err);
470         if (err) {
471                 *last = s1;
472                 dev_err(chip->dev, "failed to reapply current setting\n");
473                 return;
474         }
475
476         *last = (struct pwm_state){ 0 };
477         err = chip->ops->get_state(chip, pwm, last);
478         trace_pwm_get(pwm, last, err);
479         if (err)
480                 return;
481
482         /* reapplication of the current state should give an exact match */
483         if (s1.enabled != last->enabled ||
484             s1.polarity != last->polarity ||
485             (s1.enabled && s1.period != last->period) ||
486             (s1.enabled && s1.duty_cycle != last->duty_cycle)) {
487                 dev_err(chip->dev,
488                         ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
489                         s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
490                         last->enabled, last->polarity, last->duty_cycle,
491                         last->period);
492         }
493 }
494
495 /**
496  * pwm_apply_state() - atomically apply a new state to a PWM device
497  * @pwm: PWM device
498  * @state: new state to apply
499  */
500 int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
501 {
502         struct pwm_chip *chip;
503         int err;
504
505         /*
506          * Some lowlevel driver's implementations of .apply() make use of
507          * mutexes, also with some drivers only returning when the new
508          * configuration is active calling pwm_apply_state() from atomic context
509          * is a bad idea. So make it explicit that calling this function might
510          * sleep.
511          */
512         might_sleep();
513
514         if (!pwm || !state || !state->period ||
515             state->duty_cycle > state->period)
516                 return -EINVAL;
517
518         chip = pwm->chip;
519
520         if (state->period == pwm->state.period &&
521             state->duty_cycle == pwm->state.duty_cycle &&
522             state->polarity == pwm->state.polarity &&
523             state->enabled == pwm->state.enabled &&
524             state->usage_power == pwm->state.usage_power)
525                 return 0;
526
527         err = chip->ops->apply(chip, pwm, state);
528         trace_pwm_apply(pwm, state, err);
529         if (err)
530                 return err;
531
532         pwm->state = *state;
533
534         /*
535          * only do this after pwm->state was applied as some
536          * implementations of .get_state depend on this
537          */
538         pwm_apply_state_debug(pwm, state);
539
540         return 0;
541 }
542 EXPORT_SYMBOL_GPL(pwm_apply_state);
543
544 /**
545  * pwm_capture() - capture and report a PWM signal
546  * @pwm: PWM device
547  * @result: structure to fill with capture result
548  * @timeout: time to wait, in milliseconds, before giving up on capture
549  *
550  * Returns: 0 on success or a negative error code on failure.
551  */
552 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
553                 unsigned long timeout)
554 {
555         int err;
556
557         if (!pwm || !pwm->chip->ops)
558                 return -EINVAL;
559
560         if (!pwm->chip->ops->capture)
561                 return -ENOSYS;
562
563         mutex_lock(&pwm_lock);
564         err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
565         mutex_unlock(&pwm_lock);
566
567         return err;
568 }
569 EXPORT_SYMBOL_GPL(pwm_capture);
570
571 /**
572  * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
573  * @pwm: PWM device
574  *
575  * This function will adjust the PWM config to the PWM arguments provided
576  * by the DT or PWM lookup table. This is particularly useful to adapt
577  * the bootloader config to the Linux one.
578  */
579 int pwm_adjust_config(struct pwm_device *pwm)
580 {
581         struct pwm_state state;
582         struct pwm_args pargs;
583
584         pwm_get_args(pwm, &pargs);
585         pwm_get_state(pwm, &state);
586
587         /*
588          * If the current period is zero it means that either the PWM driver
589          * does not support initial state retrieval or the PWM has not yet
590          * been configured.
591          *
592          * In either case, we setup the new period and polarity, and assign a
593          * duty cycle of 0.
594          */
595         if (!state.period) {
596                 state.duty_cycle = 0;
597                 state.period = pargs.period;
598                 state.polarity = pargs.polarity;
599
600                 return pwm_apply_state(pwm, &state);
601         }
602
603         /*
604          * Adjust the PWM duty cycle/period based on the period value provided
605          * in PWM args.
606          */
607         if (pargs.period != state.period) {
608                 u64 dutycycle = (u64)state.duty_cycle * pargs.period;
609
610                 do_div(dutycycle, state.period);
611                 state.duty_cycle = dutycycle;
612                 state.period = pargs.period;
613         }
614
615         /*
616          * If the polarity changed, we should also change the duty cycle.
617          */
618         if (pargs.polarity != state.polarity) {
619                 state.polarity = pargs.polarity;
620                 state.duty_cycle = state.period - state.duty_cycle;
621         }
622
623         return pwm_apply_state(pwm, &state);
624 }
625 EXPORT_SYMBOL_GPL(pwm_adjust_config);
626
627 static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
628 {
629         struct pwm_chip *chip;
630
631         mutex_lock(&pwm_lock);
632
633         list_for_each_entry(chip, &pwm_chips, list)
634                 if (chip->dev && device_match_fwnode(chip->dev, fwnode)) {
635                         mutex_unlock(&pwm_lock);
636                         return chip;
637                 }
638
639         mutex_unlock(&pwm_lock);
640
641         return ERR_PTR(-EPROBE_DEFER);
642 }
643
644 static struct device_link *pwm_device_link_add(struct device *dev,
645                                                struct pwm_device *pwm)
646 {
647         struct device_link *dl;
648
649         if (!dev) {
650                 /*
651                  * No device for the PWM consumer has been provided. It may
652                  * impact the PM sequence ordering: the PWM supplier may get
653                  * suspended before the consumer.
654                  */
655                 dev_warn(pwm->chip->dev,
656                          "No consumer device specified to create a link to\n");
657                 return NULL;
658         }
659
660         dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
661         if (!dl) {
662                 dev_err(dev, "failed to create device link to %s\n",
663                         dev_name(pwm->chip->dev));
664                 return ERR_PTR(-EINVAL);
665         }
666
667         return dl;
668 }
669
670 /**
671  * of_pwm_get() - request a PWM via the PWM framework
672  * @dev: device for PWM consumer
673  * @np: device node to get the PWM from
674  * @con_id: consumer name
675  *
676  * Returns the PWM device parsed from the phandle and index specified in the
677  * "pwms" property of a device tree node or a negative error-code on failure.
678  * Values parsed from the device tree are stored in the returned PWM device
679  * object.
680  *
681  * If con_id is NULL, the first PWM device listed in the "pwms" property will
682  * be requested. Otherwise the "pwm-names" property is used to do a reverse
683  * lookup of the PWM index. This also means that the "pwm-names" property
684  * becomes mandatory for devices that look up the PWM device via the con_id
685  * parameter.
686  *
687  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
688  * error code on failure.
689  */
690 static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
691                                      const char *con_id)
692 {
693         struct pwm_device *pwm = NULL;
694         struct of_phandle_args args;
695         struct device_link *dl;
696         struct pwm_chip *chip;
697         int index = 0;
698         int err;
699
700         if (con_id) {
701                 index = of_property_match_string(np, "pwm-names", con_id);
702                 if (index < 0)
703                         return ERR_PTR(index);
704         }
705
706         err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
707                                          &args);
708         if (err) {
709                 pr_err("%s(): can't parse \"pwms\" property\n", __func__);
710                 return ERR_PTR(err);
711         }
712
713         chip = fwnode_to_pwmchip(of_fwnode_handle(args.np));
714         if (IS_ERR(chip)) {
715                 if (PTR_ERR(chip) != -EPROBE_DEFER)
716                         pr_err("%s(): PWM chip not found\n", __func__);
717
718                 pwm = ERR_CAST(chip);
719                 goto put;
720         }
721
722         pwm = chip->of_xlate(chip, &args);
723         if (IS_ERR(pwm))
724                 goto put;
725
726         dl = pwm_device_link_add(dev, pwm);
727         if (IS_ERR(dl)) {
728                 /* of_xlate ended up calling pwm_request_from_chip() */
729                 pwm_put(pwm);
730                 pwm = ERR_CAST(dl);
731                 goto put;
732         }
733
734         /*
735          * If a consumer name was not given, try to look it up from the
736          * "pwm-names" property if it exists. Otherwise use the name of
737          * the user device node.
738          */
739         if (!con_id) {
740                 err = of_property_read_string_index(np, "pwm-names", index,
741                                                     &con_id);
742                 if (err < 0)
743                         con_id = np->name;
744         }
745
746         pwm->label = con_id;
747
748 put:
749         of_node_put(args.np);
750
751         return pwm;
752 }
753
754 /**
755  * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
756  * @fwnode: firmware node to get the "pwms" property from
757  *
758  * Returns the PWM device parsed from the fwnode and index specified in the
759  * "pwms" property or a negative error-code on failure.
760  * Values parsed from the device tree are stored in the returned PWM device
761  * object.
762  *
763  * This is analogous to of_pwm_get() except con_id is not yet supported.
764  * ACPI entries must look like
765  * Package () {"pwms", Package ()
766  *     { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
767  *
768  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
769  * error code on failure.
770  */
771 static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode)
772 {
773         struct pwm_device *pwm;
774         struct fwnode_reference_args args;
775         struct pwm_chip *chip;
776         int ret;
777
778         memset(&args, 0, sizeof(args));
779
780         ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
781         if (ret < 0)
782                 return ERR_PTR(ret);
783
784         if (args.nargs < 2)
785                 return ERR_PTR(-EPROTO);
786
787         chip = fwnode_to_pwmchip(args.fwnode);
788         if (IS_ERR(chip))
789                 return ERR_CAST(chip);
790
791         pwm = pwm_request_from_chip(chip, args.args[0], NULL);
792         if (IS_ERR(pwm))
793                 return pwm;
794
795         pwm->args.period = args.args[1];
796         pwm->args.polarity = PWM_POLARITY_NORMAL;
797
798         if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
799                 pwm->args.polarity = PWM_POLARITY_INVERSED;
800
801         return pwm;
802 }
803
804 /**
805  * pwm_add_table() - register PWM device consumers
806  * @table: array of consumers to register
807  * @num: number of consumers in table
808  */
809 void pwm_add_table(struct pwm_lookup *table, size_t num)
810 {
811         mutex_lock(&pwm_lookup_lock);
812
813         while (num--) {
814                 list_add_tail(&table->list, &pwm_lookup_list);
815                 table++;
816         }
817
818         mutex_unlock(&pwm_lookup_lock);
819 }
820
821 /**
822  * pwm_remove_table() - unregister PWM device consumers
823  * @table: array of consumers to unregister
824  * @num: number of consumers in table
825  */
826 void pwm_remove_table(struct pwm_lookup *table, size_t num)
827 {
828         mutex_lock(&pwm_lookup_lock);
829
830         while (num--) {
831                 list_del(&table->list);
832                 table++;
833         }
834
835         mutex_unlock(&pwm_lookup_lock);
836 }
837
838 /**
839  * pwm_get() - look up and request a PWM device
840  * @dev: device for PWM consumer
841  * @con_id: consumer name
842  *
843  * Lookup is first attempted using DT. If the device was not instantiated from
844  * a device tree, a PWM chip and a relative index is looked up via a table
845  * supplied by board setup code (see pwm_add_table()).
846  *
847  * Once a PWM chip has been found the specified PWM device will be requested
848  * and is ready to be used.
849  *
850  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
851  * error code on failure.
852  */
853 struct pwm_device *pwm_get(struct device *dev, const char *con_id)
854 {
855         const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
856         const char *dev_id = dev ? dev_name(dev) : NULL;
857         struct pwm_device *pwm;
858         struct pwm_chip *chip;
859         struct device_link *dl;
860         unsigned int best = 0;
861         struct pwm_lookup *p, *chosen = NULL;
862         unsigned int match;
863         int err;
864
865         /* look up via DT first */
866         if (is_of_node(fwnode))
867                 return of_pwm_get(dev, to_of_node(fwnode), con_id);
868
869         /* then lookup via ACPI */
870         if (is_acpi_node(fwnode)) {
871                 pwm = acpi_pwm_get(fwnode);
872                 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
873                         return pwm;
874         }
875
876         /*
877          * We look up the provider in the static table typically provided by
878          * board setup code. We first try to lookup the consumer device by
879          * name. If the consumer device was passed in as NULL or if no match
880          * was found, we try to find the consumer by directly looking it up
881          * by name.
882          *
883          * If a match is found, the provider PWM chip is looked up by name
884          * and a PWM device is requested using the PWM device per-chip index.
885          *
886          * The lookup algorithm was shamelessly taken from the clock
887          * framework:
888          *
889          * We do slightly fuzzy matching here:
890          *  An entry with a NULL ID is assumed to be a wildcard.
891          *  If an entry has a device ID, it must match
892          *  If an entry has a connection ID, it must match
893          * Then we take the most specific entry - with the following order
894          * of precedence: dev+con > dev only > con only.
895          */
896         mutex_lock(&pwm_lookup_lock);
897
898         list_for_each_entry(p, &pwm_lookup_list, list) {
899                 match = 0;
900
901                 if (p->dev_id) {
902                         if (!dev_id || strcmp(p->dev_id, dev_id))
903                                 continue;
904
905                         match += 2;
906                 }
907
908                 if (p->con_id) {
909                         if (!con_id || strcmp(p->con_id, con_id))
910                                 continue;
911
912                         match += 1;
913                 }
914
915                 if (match > best) {
916                         chosen = p;
917
918                         if (match != 3)
919                                 best = match;
920                         else
921                                 break;
922                 }
923         }
924
925         mutex_unlock(&pwm_lookup_lock);
926
927         if (!chosen)
928                 return ERR_PTR(-ENODEV);
929
930         chip = pwmchip_find_by_name(chosen->provider);
931
932         /*
933          * If the lookup entry specifies a module, load the module and retry
934          * the PWM chip lookup. This can be used to work around driver load
935          * ordering issues if driver's can't be made to properly support the
936          * deferred probe mechanism.
937          */
938         if (!chip && chosen->module) {
939                 err = request_module(chosen->module);
940                 if (err == 0)
941                         chip = pwmchip_find_by_name(chosen->provider);
942         }
943
944         if (!chip)
945                 return ERR_PTR(-EPROBE_DEFER);
946
947         pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
948         if (IS_ERR(pwm))
949                 return pwm;
950
951         dl = pwm_device_link_add(dev, pwm);
952         if (IS_ERR(dl)) {
953                 pwm_put(pwm);
954                 return ERR_CAST(dl);
955         }
956
957         pwm->args.period = chosen->period;
958         pwm->args.polarity = chosen->polarity;
959
960         return pwm;
961 }
962 EXPORT_SYMBOL_GPL(pwm_get);
963
964 /**
965  * pwm_put() - release a PWM device
966  * @pwm: PWM device
967  */
968 void pwm_put(struct pwm_device *pwm)
969 {
970         if (!pwm)
971                 return;
972
973         mutex_lock(&pwm_lock);
974
975         if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
976                 pr_warn("PWM device already freed\n");
977                 goto out;
978         }
979
980         if (pwm->chip->ops->free)
981                 pwm->chip->ops->free(pwm->chip, pwm);
982
983         pwm_set_chip_data(pwm, NULL);
984         pwm->label = NULL;
985
986         module_put(pwm->chip->owner);
987 out:
988         mutex_unlock(&pwm_lock);
989 }
990 EXPORT_SYMBOL_GPL(pwm_put);
991
992 static void devm_pwm_release(void *pwm)
993 {
994         pwm_put(pwm);
995 }
996
997 /**
998  * devm_pwm_get() - resource managed pwm_get()
999  * @dev: device for PWM consumer
1000  * @con_id: consumer name
1001  *
1002  * This function performs like pwm_get() but the acquired PWM device will
1003  * automatically be released on driver detach.
1004  *
1005  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1006  * error code on failure.
1007  */
1008 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
1009 {
1010         struct pwm_device *pwm;
1011         int ret;
1012
1013         pwm = pwm_get(dev, con_id);
1014         if (IS_ERR(pwm))
1015                 return pwm;
1016
1017         ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1018         if (ret)
1019                 return ERR_PTR(ret);
1020
1021         return pwm;
1022 }
1023 EXPORT_SYMBOL_GPL(devm_pwm_get);
1024
1025 /**
1026  * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1027  * @dev: device for PWM consumer
1028  * @fwnode: firmware node to get the PWM from
1029  * @con_id: consumer name
1030  *
1031  * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1032  * acpi_pwm_get() for a detailed description.
1033  *
1034  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1035  * error code on failure.
1036  */
1037 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
1038                                        struct fwnode_handle *fwnode,
1039                                        const char *con_id)
1040 {
1041         struct pwm_device *pwm = ERR_PTR(-ENODEV);
1042         int ret;
1043
1044         if (is_of_node(fwnode))
1045                 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
1046         else if (is_acpi_node(fwnode))
1047                 pwm = acpi_pwm_get(fwnode);
1048         if (IS_ERR(pwm))
1049                 return pwm;
1050
1051         ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1052         if (ret)
1053                 return ERR_PTR(ret);
1054
1055         return pwm;
1056 }
1057 EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
1058
1059 #ifdef CONFIG_DEBUG_FS
1060 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
1061 {
1062         unsigned int i;
1063
1064         for (i = 0; i < chip->npwm; i++) {
1065                 struct pwm_device *pwm = &chip->pwms[i];
1066                 struct pwm_state state;
1067
1068                 pwm_get_state(pwm, &state);
1069
1070                 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
1071
1072                 if (test_bit(PWMF_REQUESTED, &pwm->flags))
1073                         seq_puts(s, " requested");
1074
1075                 if (state.enabled)
1076                         seq_puts(s, " enabled");
1077
1078                 seq_printf(s, " period: %llu ns", state.period);
1079                 seq_printf(s, " duty: %llu ns", state.duty_cycle);
1080                 seq_printf(s, " polarity: %s",
1081                            state.polarity ? "inverse" : "normal");
1082
1083                 if (state.usage_power)
1084                         seq_puts(s, " usage_power");
1085
1086                 seq_puts(s, "\n");
1087         }
1088 }
1089
1090 static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
1091 {
1092         mutex_lock(&pwm_lock);
1093         s->private = "";
1094
1095         return seq_list_start(&pwm_chips, *pos);
1096 }
1097
1098 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
1099 {
1100         s->private = "\n";
1101
1102         return seq_list_next(v, &pwm_chips, pos);
1103 }
1104
1105 static void pwm_seq_stop(struct seq_file *s, void *v)
1106 {
1107         mutex_unlock(&pwm_lock);
1108 }
1109
1110 static int pwm_seq_show(struct seq_file *s, void *v)
1111 {
1112         struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
1113
1114         seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
1115                    chip->dev->bus ? chip->dev->bus->name : "no-bus",
1116                    dev_name(chip->dev), chip->npwm,
1117                    (chip->npwm != 1) ? "s" : "");
1118
1119         pwm_dbg_show(chip, s);
1120
1121         return 0;
1122 }
1123
1124 static const struct seq_operations pwm_debugfs_sops = {
1125         .start = pwm_seq_start,
1126         .next = pwm_seq_next,
1127         .stop = pwm_seq_stop,
1128         .show = pwm_seq_show,
1129 };
1130
1131 DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
1132
1133 static int __init pwm_debugfs_init(void)
1134 {
1135         debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops);
1136
1137         return 0;
1138 }
1139 subsys_initcall(pwm_debugfs_init);
1140 #endif /* CONFIG_DEBUG_FS */
This page took 0.090557 seconds and 4 git commands to generate.