Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / pwm / core.c
blob275b5f399a1addbf2966de351eae74f17ec9372e
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Generic pwmlib implementation
5 * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
6 * Copyright (C) 2011-2012 Avionic Design GmbH
7 */
9 #include <linux/module.h>
10 #include <linux/pwm.h>
11 #include <linux/radix-tree.h>
12 #include <linux/list.h>
13 #include <linux/mutex.h>
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/device.h>
17 #include <linux/debugfs.h>
18 #include <linux/seq_file.h>
20 #include <dt-bindings/pwm/pwm.h>
22 #define MAX_PWMS 1024
24 static DEFINE_MUTEX(pwm_lookup_lock);
25 static LIST_HEAD(pwm_lookup_list);
26 static DEFINE_MUTEX(pwm_lock);
27 static LIST_HEAD(pwm_chips);
28 static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
29 static RADIX_TREE(pwm_tree, GFP_KERNEL);
31 static struct pwm_device *pwm_to_device(unsigned int pwm)
33 return radix_tree_lookup(&pwm_tree, pwm);
36 static int alloc_pwms(int pwm, unsigned int count)
38 unsigned int from = 0;
39 unsigned int start;
41 if (pwm >= MAX_PWMS)
42 return -EINVAL;
44 if (pwm >= 0)
45 from = pwm;
47 start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from,
48 count, 0);
50 if (pwm >= 0 && start != pwm)
51 return -EEXIST;
53 if (start + count > MAX_PWMS)
54 return -ENOSPC;
56 return start;
59 static void free_pwms(struct pwm_chip *chip)
61 unsigned int i;
63 for (i = 0; i < chip->npwm; i++) {
64 struct pwm_device *pwm = &chip->pwms[i];
66 radix_tree_delete(&pwm_tree, pwm->pwm);
69 bitmap_clear(allocated_pwms, chip->base, chip->npwm);
71 kfree(chip->pwms);
72 chip->pwms = NULL;
75 static struct pwm_chip *pwmchip_find_by_name(const char *name)
77 struct pwm_chip *chip;
79 if (!name)
80 return NULL;
82 mutex_lock(&pwm_lock);
84 list_for_each_entry(chip, &pwm_chips, list) {
85 const char *chip_name = dev_name(chip->dev);
87 if (chip_name && strcmp(chip_name, name) == 0) {
88 mutex_unlock(&pwm_lock);
89 return chip;
93 mutex_unlock(&pwm_lock);
95 return NULL;
98 static int pwm_device_request(struct pwm_device *pwm, const char *label)
100 int err;
102 if (test_bit(PWMF_REQUESTED, &pwm->flags))
103 return -EBUSY;
105 if (!try_module_get(pwm->chip->ops->owner))
106 return -ENODEV;
108 if (pwm->chip->ops->request) {
109 err = pwm->chip->ops->request(pwm->chip, pwm);
110 if (err) {
111 module_put(pwm->chip->ops->owner);
112 return err;
116 set_bit(PWMF_REQUESTED, &pwm->flags);
117 pwm->label = label;
119 return 0;
122 struct pwm_device *
123 of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
125 struct pwm_device *pwm;
127 /* check, whether the driver supports a third cell for flags */
128 if (pc->of_pwm_n_cells < 3)
129 return ERR_PTR(-EINVAL);
131 /* flags in the third cell are optional */
132 if (args->args_count < 2)
133 return ERR_PTR(-EINVAL);
135 if (args->args[0] >= pc->npwm)
136 return ERR_PTR(-EINVAL);
138 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
139 if (IS_ERR(pwm))
140 return pwm;
142 pwm->args.period = args->args[1];
143 pwm->args.polarity = PWM_POLARITY_NORMAL;
145 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
146 pwm->args.polarity = PWM_POLARITY_INVERSED;
148 return pwm;
150 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
152 static struct pwm_device *
153 of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
155 struct pwm_device *pwm;
157 /* sanity check driver support */
158 if (pc->of_pwm_n_cells < 2)
159 return ERR_PTR(-EINVAL);
161 /* all cells are required */
162 if (args->args_count != pc->of_pwm_n_cells)
163 return ERR_PTR(-EINVAL);
165 if (args->args[0] >= pc->npwm)
166 return ERR_PTR(-EINVAL);
168 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
169 if (IS_ERR(pwm))
170 return pwm;
172 pwm->args.period = args->args[1];
174 return pwm;
177 static void of_pwmchip_add(struct pwm_chip *chip)
179 if (!chip->dev || !chip->dev->of_node)
180 return;
182 if (!chip->of_xlate) {
183 chip->of_xlate = of_pwm_simple_xlate;
184 chip->of_pwm_n_cells = 2;
187 of_node_get(chip->dev->of_node);
190 static void of_pwmchip_remove(struct pwm_chip *chip)
192 if (chip->dev)
193 of_node_put(chip->dev->of_node);
197 * pwm_set_chip_data() - set private chip data for a PWM
198 * @pwm: PWM device
199 * @data: pointer to chip-specific data
201 * Returns: 0 on success or a negative error code on failure.
203 int pwm_set_chip_data(struct pwm_device *pwm, void *data)
205 if (!pwm)
206 return -EINVAL;
208 pwm->chip_data = data;
210 return 0;
212 EXPORT_SYMBOL_GPL(pwm_set_chip_data);
215 * pwm_get_chip_data() - get private chip data for a PWM
216 * @pwm: PWM device
218 * Returns: A pointer to the chip-private data for the PWM device.
220 void *pwm_get_chip_data(struct pwm_device *pwm)
222 return pwm ? pwm->chip_data : NULL;
224 EXPORT_SYMBOL_GPL(pwm_get_chip_data);
226 static bool pwm_ops_check(const struct pwm_ops *ops)
228 /* driver supports legacy, non-atomic operation */
229 if (ops->config && ops->enable && ops->disable)
230 return true;
232 /* driver supports atomic operation */
233 if (ops->apply)
234 return true;
236 return false;
240 * pwmchip_add_with_polarity() - register a new PWM chip
241 * @chip: the PWM chip to add
242 * @polarity: initial polarity of PWM channels
244 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
245 * will be used. The initial polarity for all channels is specified by the
246 * @polarity parameter.
248 * Returns: 0 on success or a negative error code on failure.
250 int pwmchip_add_with_polarity(struct pwm_chip *chip,
251 enum pwm_polarity polarity)
253 struct pwm_device *pwm;
254 unsigned int i;
255 int ret;
257 if (!chip || !chip->dev || !chip->ops || !chip->npwm)
258 return -EINVAL;
260 if (!pwm_ops_check(chip->ops))
261 return -EINVAL;
263 mutex_lock(&pwm_lock);
265 ret = alloc_pwms(chip->base, chip->npwm);
266 if (ret < 0)
267 goto out;
269 chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL);
270 if (!chip->pwms) {
271 ret = -ENOMEM;
272 goto out;
275 chip->base = ret;
277 for (i = 0; i < chip->npwm; i++) {
278 pwm = &chip->pwms[i];
280 pwm->chip = chip;
281 pwm->pwm = chip->base + i;
282 pwm->hwpwm = i;
283 pwm->state.polarity = polarity;
285 if (chip->ops->get_state)
286 chip->ops->get_state(chip, pwm, &pwm->state);
288 radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
291 bitmap_set(allocated_pwms, chip->base, chip->npwm);
293 INIT_LIST_HEAD(&chip->list);
294 list_add(&chip->list, &pwm_chips);
296 ret = 0;
298 if (IS_ENABLED(CONFIG_OF))
299 of_pwmchip_add(chip);
301 out:
302 mutex_unlock(&pwm_lock);
304 if (!ret)
305 pwmchip_sysfs_export(chip);
307 return ret;
309 EXPORT_SYMBOL_GPL(pwmchip_add_with_polarity);
312 * pwmchip_add() - register a new PWM chip
313 * @chip: the PWM chip to add
315 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
316 * will be used. The initial polarity for all channels is normal.
318 * Returns: 0 on success or a negative error code on failure.
320 int pwmchip_add(struct pwm_chip *chip)
322 return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL);
324 EXPORT_SYMBOL_GPL(pwmchip_add);
327 * pwmchip_remove() - remove a PWM chip
328 * @chip: the PWM chip to remove
330 * Removes a PWM chip. This function may return busy if the PWM chip provides
331 * a PWM device that is still requested.
333 * Returns: 0 on success or a negative error code on failure.
335 int pwmchip_remove(struct pwm_chip *chip)
337 unsigned int i;
338 int ret = 0;
340 pwmchip_sysfs_unexport(chip);
342 mutex_lock(&pwm_lock);
344 for (i = 0; i < chip->npwm; i++) {
345 struct pwm_device *pwm = &chip->pwms[i];
347 if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
348 ret = -EBUSY;
349 goto out;
353 list_del_init(&chip->list);
355 if (IS_ENABLED(CONFIG_OF))
356 of_pwmchip_remove(chip);
358 free_pwms(chip);
360 out:
361 mutex_unlock(&pwm_lock);
362 return ret;
364 EXPORT_SYMBOL_GPL(pwmchip_remove);
367 * pwm_request() - request a PWM device
368 * @pwm: global PWM device index
369 * @label: PWM device label
371 * This function is deprecated, use pwm_get() instead.
373 * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on
374 * failure.
376 struct pwm_device *pwm_request(int pwm, const char *label)
378 struct pwm_device *dev;
379 int err;
381 if (pwm < 0 || pwm >= MAX_PWMS)
382 return ERR_PTR(-EINVAL);
384 mutex_lock(&pwm_lock);
386 dev = pwm_to_device(pwm);
387 if (!dev) {
388 dev = ERR_PTR(-EPROBE_DEFER);
389 goto out;
392 err = pwm_device_request(dev, label);
393 if (err < 0)
394 dev = ERR_PTR(err);
396 out:
397 mutex_unlock(&pwm_lock);
399 return dev;
401 EXPORT_SYMBOL_GPL(pwm_request);
404 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
405 * @chip: PWM chip
406 * @index: per-chip index of the PWM to request
407 * @label: a literal description string of this PWM
409 * Returns: A pointer to the PWM device at the given index of the given PWM
410 * chip. A negative error code is returned if the index is not valid for the
411 * specified PWM chip or if the PWM device cannot be requested.
413 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
414 unsigned int index,
415 const char *label)
417 struct pwm_device *pwm;
418 int err;
420 if (!chip || index >= chip->npwm)
421 return ERR_PTR(-EINVAL);
423 mutex_lock(&pwm_lock);
424 pwm = &chip->pwms[index];
426 err = pwm_device_request(pwm, label);
427 if (err < 0)
428 pwm = ERR_PTR(err);
430 mutex_unlock(&pwm_lock);
431 return pwm;
433 EXPORT_SYMBOL_GPL(pwm_request_from_chip);
436 * pwm_free() - free a PWM device
437 * @pwm: PWM device
439 * This function is deprecated, use pwm_put() instead.
441 void pwm_free(struct pwm_device *pwm)
443 pwm_put(pwm);
445 EXPORT_SYMBOL_GPL(pwm_free);
448 * pwm_apply_state() - atomically apply a new state to a PWM device
449 * @pwm: PWM device
450 * @state: new state to apply. This can be adjusted by the PWM driver
451 * if the requested config is not achievable, for example,
452 * ->duty_cycle and ->period might be approximated.
454 int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state)
456 int err;
458 if (!pwm || !state || !state->period ||
459 state->duty_cycle > state->period)
460 return -EINVAL;
462 if (state->period == pwm->state.period &&
463 state->duty_cycle == pwm->state.duty_cycle &&
464 state->polarity == pwm->state.polarity &&
465 state->enabled == pwm->state.enabled)
466 return 0;
468 if (pwm->chip->ops->apply) {
469 err = pwm->chip->ops->apply(pwm->chip, pwm, state);
470 if (err)
471 return err;
473 pwm->state = *state;
474 } else {
476 * FIXME: restore the initial state in case of error.
478 if (state->polarity != pwm->state.polarity) {
479 if (!pwm->chip->ops->set_polarity)
480 return -ENOTSUPP;
483 * Changing the polarity of a running PWM is
484 * only allowed when the PWM driver implements
485 * ->apply().
487 if (pwm->state.enabled) {
488 pwm->chip->ops->disable(pwm->chip, pwm);
489 pwm->state.enabled = false;
492 err = pwm->chip->ops->set_polarity(pwm->chip, pwm,
493 state->polarity);
494 if (err)
495 return err;
497 pwm->state.polarity = state->polarity;
500 if (state->period != pwm->state.period ||
501 state->duty_cycle != pwm->state.duty_cycle) {
502 err = pwm->chip->ops->config(pwm->chip, pwm,
503 state->duty_cycle,
504 state->period);
505 if (err)
506 return err;
508 pwm->state.duty_cycle = state->duty_cycle;
509 pwm->state.period = state->period;
512 if (state->enabled != pwm->state.enabled) {
513 if (state->enabled) {
514 err = pwm->chip->ops->enable(pwm->chip, pwm);
515 if (err)
516 return err;
517 } else {
518 pwm->chip->ops->disable(pwm->chip, pwm);
521 pwm->state.enabled = state->enabled;
525 return 0;
527 EXPORT_SYMBOL_GPL(pwm_apply_state);
530 * pwm_capture() - capture and report a PWM signal
531 * @pwm: PWM device
532 * @result: structure to fill with capture result
533 * @timeout: time to wait, in milliseconds, before giving up on capture
535 * Returns: 0 on success or a negative error code on failure.
537 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
538 unsigned long timeout)
540 int err;
542 if (!pwm || !pwm->chip->ops)
543 return -EINVAL;
545 if (!pwm->chip->ops->capture)
546 return -ENOSYS;
548 mutex_lock(&pwm_lock);
549 err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
550 mutex_unlock(&pwm_lock);
552 return err;
554 EXPORT_SYMBOL_GPL(pwm_capture);
557 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
558 * @pwm: PWM device
560 * This function will adjust the PWM config to the PWM arguments provided
561 * by the DT or PWM lookup table. This is particularly useful to adapt
562 * the bootloader config to the Linux one.
564 int pwm_adjust_config(struct pwm_device *pwm)
566 struct pwm_state state;
567 struct pwm_args pargs;
569 pwm_get_args(pwm, &pargs);
570 pwm_get_state(pwm, &state);
573 * If the current period is zero it means that either the PWM driver
574 * does not support initial state retrieval or the PWM has not yet
575 * been configured.
577 * In either case, we setup the new period and polarity, and assign a
578 * duty cycle of 0.
580 if (!state.period) {
581 state.duty_cycle = 0;
582 state.period = pargs.period;
583 state.polarity = pargs.polarity;
585 return pwm_apply_state(pwm, &state);
589 * Adjust the PWM duty cycle/period based on the period value provided
590 * in PWM args.
592 if (pargs.period != state.period) {
593 u64 dutycycle = (u64)state.duty_cycle * pargs.period;
595 do_div(dutycycle, state.period);
596 state.duty_cycle = dutycycle;
597 state.period = pargs.period;
601 * If the polarity changed, we should also change the duty cycle.
603 if (pargs.polarity != state.polarity) {
604 state.polarity = pargs.polarity;
605 state.duty_cycle = state.period - state.duty_cycle;
608 return pwm_apply_state(pwm, &state);
610 EXPORT_SYMBOL_GPL(pwm_adjust_config);
612 static struct pwm_chip *of_node_to_pwmchip(struct device_node *np)
614 struct pwm_chip *chip;
616 mutex_lock(&pwm_lock);
618 list_for_each_entry(chip, &pwm_chips, list)
619 if (chip->dev && chip->dev->of_node == np) {
620 mutex_unlock(&pwm_lock);
621 return chip;
624 mutex_unlock(&pwm_lock);
626 return ERR_PTR(-EPROBE_DEFER);
630 * of_pwm_get() - request a PWM via the PWM framework
631 * @np: device node to get the PWM from
632 * @con_id: consumer name
634 * Returns the PWM device parsed from the phandle and index specified in the
635 * "pwms" property of a device tree node or a negative error-code on failure.
636 * Values parsed from the device tree are stored in the returned PWM device
637 * object.
639 * If con_id is NULL, the first PWM device listed in the "pwms" property will
640 * be requested. Otherwise the "pwm-names" property is used to do a reverse
641 * lookup of the PWM index. This also means that the "pwm-names" property
642 * becomes mandatory for devices that look up the PWM device via the con_id
643 * parameter.
645 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
646 * error code on failure.
648 struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id)
650 struct pwm_device *pwm = NULL;
651 struct of_phandle_args args;
652 struct pwm_chip *pc;
653 int index = 0;
654 int err;
656 if (con_id) {
657 index = of_property_match_string(np, "pwm-names", con_id);
658 if (index < 0)
659 return ERR_PTR(index);
662 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
663 &args);
664 if (err) {
665 pr_err("%s(): can't parse \"pwms\" property\n", __func__);
666 return ERR_PTR(err);
669 pc = of_node_to_pwmchip(args.np);
670 if (IS_ERR(pc)) {
671 if (PTR_ERR(pc) != -EPROBE_DEFER)
672 pr_err("%s(): PWM chip not found\n", __func__);
674 pwm = ERR_CAST(pc);
675 goto put;
678 pwm = pc->of_xlate(pc, &args);
679 if (IS_ERR(pwm))
680 goto put;
683 * If a consumer name was not given, try to look it up from the
684 * "pwm-names" property if it exists. Otherwise use the name of
685 * the user device node.
687 if (!con_id) {
688 err = of_property_read_string_index(np, "pwm-names", index,
689 &con_id);
690 if (err < 0)
691 con_id = np->name;
694 pwm->label = con_id;
696 put:
697 of_node_put(args.np);
699 return pwm;
701 EXPORT_SYMBOL_GPL(of_pwm_get);
704 * pwm_add_table() - register PWM device consumers
705 * @table: array of consumers to register
706 * @num: number of consumers in table
708 void pwm_add_table(struct pwm_lookup *table, size_t num)
710 mutex_lock(&pwm_lookup_lock);
712 while (num--) {
713 list_add_tail(&table->list, &pwm_lookup_list);
714 table++;
717 mutex_unlock(&pwm_lookup_lock);
721 * pwm_remove_table() - unregister PWM device consumers
722 * @table: array of consumers to unregister
723 * @num: number of consumers in table
725 void pwm_remove_table(struct pwm_lookup *table, size_t num)
727 mutex_lock(&pwm_lookup_lock);
729 while (num--) {
730 list_del(&table->list);
731 table++;
734 mutex_unlock(&pwm_lookup_lock);
738 * pwm_get() - look up and request a PWM device
739 * @dev: device for PWM consumer
740 * @con_id: consumer name
742 * Lookup is first attempted using DT. If the device was not instantiated from
743 * a device tree, a PWM chip and a relative index is looked up via a table
744 * supplied by board setup code (see pwm_add_table()).
746 * Once a PWM chip has been found the specified PWM device will be requested
747 * and is ready to be used.
749 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
750 * error code on failure.
752 struct pwm_device *pwm_get(struct device *dev, const char *con_id)
754 const char *dev_id = dev ? dev_name(dev) : NULL;
755 struct pwm_device *pwm;
756 struct pwm_chip *chip;
757 unsigned int best = 0;
758 struct pwm_lookup *p, *chosen = NULL;
759 unsigned int match;
760 int err;
762 /* look up via DT first */
763 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
764 return of_pwm_get(dev->of_node, con_id);
767 * We look up the provider in the static table typically provided by
768 * board setup code. We first try to lookup the consumer device by
769 * name. If the consumer device was passed in as NULL or if no match
770 * was found, we try to find the consumer by directly looking it up
771 * by name.
773 * If a match is found, the provider PWM chip is looked up by name
774 * and a PWM device is requested using the PWM device per-chip index.
776 * The lookup algorithm was shamelessly taken from the clock
777 * framework:
779 * We do slightly fuzzy matching here:
780 * An entry with a NULL ID is assumed to be a wildcard.
781 * If an entry has a device ID, it must match
782 * If an entry has a connection ID, it must match
783 * Then we take the most specific entry - with the following order
784 * of precedence: dev+con > dev only > con only.
786 mutex_lock(&pwm_lookup_lock);
788 list_for_each_entry(p, &pwm_lookup_list, list) {
789 match = 0;
791 if (p->dev_id) {
792 if (!dev_id || strcmp(p->dev_id, dev_id))
793 continue;
795 match += 2;
798 if (p->con_id) {
799 if (!con_id || strcmp(p->con_id, con_id))
800 continue;
802 match += 1;
805 if (match > best) {
806 chosen = p;
808 if (match != 3)
809 best = match;
810 else
811 break;
815 mutex_unlock(&pwm_lookup_lock);
817 if (!chosen)
818 return ERR_PTR(-ENODEV);
820 chip = pwmchip_find_by_name(chosen->provider);
823 * If the lookup entry specifies a module, load the module and retry
824 * the PWM chip lookup. This can be used to work around driver load
825 * ordering issues if driver's can't be made to properly support the
826 * deferred probe mechanism.
828 if (!chip && chosen->module) {
829 err = request_module(chosen->module);
830 if (err == 0)
831 chip = pwmchip_find_by_name(chosen->provider);
834 if (!chip)
835 return ERR_PTR(-EPROBE_DEFER);
837 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
838 if (IS_ERR(pwm))
839 return pwm;
841 pwm->args.period = chosen->period;
842 pwm->args.polarity = chosen->polarity;
844 return pwm;
846 EXPORT_SYMBOL_GPL(pwm_get);
849 * pwm_put() - release a PWM device
850 * @pwm: PWM device
852 void pwm_put(struct pwm_device *pwm)
854 if (!pwm)
855 return;
857 mutex_lock(&pwm_lock);
859 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
860 pr_warn("PWM device already freed\n");
861 goto out;
864 if (pwm->chip->ops->free)
865 pwm->chip->ops->free(pwm->chip, pwm);
867 pwm_set_chip_data(pwm, NULL);
868 pwm->label = NULL;
870 module_put(pwm->chip->ops->owner);
871 out:
872 mutex_unlock(&pwm_lock);
874 EXPORT_SYMBOL_GPL(pwm_put);
876 static void devm_pwm_release(struct device *dev, void *res)
878 pwm_put(*(struct pwm_device **)res);
882 * devm_pwm_get() - resource managed pwm_get()
883 * @dev: device for PWM consumer
884 * @con_id: consumer name
886 * This function performs like pwm_get() but the acquired PWM device will
887 * automatically be released on driver detach.
889 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
890 * error code on failure.
892 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
894 struct pwm_device **ptr, *pwm;
896 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
897 if (!ptr)
898 return ERR_PTR(-ENOMEM);
900 pwm = pwm_get(dev, con_id);
901 if (!IS_ERR(pwm)) {
902 *ptr = pwm;
903 devres_add(dev, ptr);
904 } else {
905 devres_free(ptr);
908 return pwm;
910 EXPORT_SYMBOL_GPL(devm_pwm_get);
913 * devm_of_pwm_get() - resource managed of_pwm_get()
914 * @dev: device for PWM consumer
915 * @np: device node to get the PWM from
916 * @con_id: consumer name
918 * This function performs like of_pwm_get() but the acquired PWM device will
919 * automatically be released on driver detach.
921 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
922 * error code on failure.
924 struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
925 const char *con_id)
927 struct pwm_device **ptr, *pwm;
929 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
930 if (!ptr)
931 return ERR_PTR(-ENOMEM);
933 pwm = of_pwm_get(np, con_id);
934 if (!IS_ERR(pwm)) {
935 *ptr = pwm;
936 devres_add(dev, ptr);
937 } else {
938 devres_free(ptr);
941 return pwm;
943 EXPORT_SYMBOL_GPL(devm_of_pwm_get);
945 static int devm_pwm_match(struct device *dev, void *res, void *data)
947 struct pwm_device **p = res;
949 if (WARN_ON(!p || !*p))
950 return 0;
952 return *p == data;
956 * devm_pwm_put() - resource managed pwm_put()
957 * @dev: device for PWM consumer
958 * @pwm: PWM device
960 * Release a PWM previously allocated using devm_pwm_get(). Calling this
961 * function is usually not needed because devm-allocated resources are
962 * automatically released on driver detach.
964 void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
966 WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm));
968 EXPORT_SYMBOL_GPL(devm_pwm_put);
970 #ifdef CONFIG_DEBUG_FS
971 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
973 unsigned int i;
975 for (i = 0; i < chip->npwm; i++) {
976 struct pwm_device *pwm = &chip->pwms[i];
977 struct pwm_state state;
979 pwm_get_state(pwm, &state);
981 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
983 if (test_bit(PWMF_REQUESTED, &pwm->flags))
984 seq_puts(s, " requested");
986 if (state.enabled)
987 seq_puts(s, " enabled");
989 seq_printf(s, " period: %u ns", state.period);
990 seq_printf(s, " duty: %u ns", state.duty_cycle);
991 seq_printf(s, " polarity: %s",
992 state.polarity ? "inverse" : "normal");
994 seq_puts(s, "\n");
998 static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
1000 mutex_lock(&pwm_lock);
1001 s->private = "";
1003 return seq_list_start(&pwm_chips, *pos);
1006 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
1008 s->private = "\n";
1010 return seq_list_next(v, &pwm_chips, pos);
1013 static void pwm_seq_stop(struct seq_file *s, void *v)
1015 mutex_unlock(&pwm_lock);
1018 static int pwm_seq_show(struct seq_file *s, void *v)
1020 struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
1022 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
1023 chip->dev->bus ? chip->dev->bus->name : "no-bus",
1024 dev_name(chip->dev), chip->npwm,
1025 (chip->npwm != 1) ? "s" : "");
1027 pwm_dbg_show(chip, s);
1029 return 0;
1032 static const struct seq_operations pwm_seq_ops = {
1033 .start = pwm_seq_start,
1034 .next = pwm_seq_next,
1035 .stop = pwm_seq_stop,
1036 .show = pwm_seq_show,
1039 static int pwm_seq_open(struct inode *inode, struct file *file)
1041 return seq_open(file, &pwm_seq_ops);
1044 static const struct file_operations pwm_debugfs_ops = {
1045 .owner = THIS_MODULE,
1046 .open = pwm_seq_open,
1047 .read = seq_read,
1048 .llseek = seq_lseek,
1049 .release = seq_release,
1052 static int __init pwm_debugfs_init(void)
1054 debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
1055 &pwm_debugfs_ops);
1057 return 0;
1059 subsys_initcall(pwm_debugfs_init);
1060 #endif /* CONFIG_DEBUG_FS */