pwm: Remove pwm-twl6030 driver
[linux-block.git] / drivers / pwm / core.c
CommitLineData
0c2498f1
SH
1/*
2 * Generic pwmlib implementation
3 *
4 * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
f051c466 5 * Copyright (C) 2011-2012 Avionic Design GmbH
0c2498f1
SH
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/module.h>
23#include <linux/pwm.h>
f051c466 24#include <linux/radix-tree.h>
0c2498f1
SH
25#include <linux/list.h>
26#include <linux/mutex.h>
27#include <linux/err.h>
28#include <linux/slab.h>
29#include <linux/device.h>
62099abf
TR
30#include <linux/debugfs.h>
31#include <linux/seq_file.h>
0c2498f1 32
f051c466 33#define MAX_PWMS 1024
0c2498f1 34
83af2402
PA
35/* flags in the third cell of the DT PWM specifier */
36#define PWM_SPEC_POLARITY (1 << 0)
37
8138d2dd
TR
38static DEFINE_MUTEX(pwm_lookup_lock);
39static LIST_HEAD(pwm_lookup_list);
0c2498f1 40static DEFINE_MUTEX(pwm_lock);
f051c466
TR
41static LIST_HEAD(pwm_chips);
42static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
43static RADIX_TREE(pwm_tree, GFP_KERNEL);
0c2498f1 44
f051c466 45static struct pwm_device *pwm_to_device(unsigned int pwm)
0c2498f1 46{
f051c466
TR
47 return radix_tree_lookup(&pwm_tree, pwm);
48}
49
50static int alloc_pwms(int pwm, unsigned int count)
51{
52 unsigned int from = 0;
53 unsigned int start;
54
55 if (pwm >= MAX_PWMS)
56 return -EINVAL;
57
58 if (pwm >= 0)
59 from = pwm;
0c2498f1 60
f051c466
TR
61 start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from,
62 count, 0);
63
64 if (pwm >= 0 && start != pwm)
65 return -EEXIST;
66
67 if (start + count > MAX_PWMS)
68 return -ENOSPC;
69
70 return start;
71}
72
73static void free_pwms(struct pwm_chip *chip)
74{
75 unsigned int i;
76
77 for (i = 0; i < chip->npwm; i++) {
78 struct pwm_device *pwm = &chip->pwms[i];
79 radix_tree_delete(&pwm_tree, pwm->pwm);
0c2498f1
SH
80 }
81
f051c466
TR
82 bitmap_clear(allocated_pwms, chip->base, chip->npwm);
83
84 kfree(chip->pwms);
85 chip->pwms = NULL;
86}
87
8138d2dd
TR
88static struct pwm_chip *pwmchip_find_by_name(const char *name)
89{
90 struct pwm_chip *chip;
91
92 if (!name)
93 return NULL;
94
95 mutex_lock(&pwm_lock);
96
97 list_for_each_entry(chip, &pwm_chips, list) {
98 const char *chip_name = dev_name(chip->dev);
99
100 if (chip_name && strcmp(chip_name, name) == 0) {
101 mutex_unlock(&pwm_lock);
102 return chip;
103 }
104 }
105
106 mutex_unlock(&pwm_lock);
107
108 return NULL;
109}
110
f051c466
TR
111static int pwm_device_request(struct pwm_device *pwm, const char *label)
112{
113 int err;
114
115 if (test_bit(PWMF_REQUESTED, &pwm->flags))
116 return -EBUSY;
117
118 if (!try_module_get(pwm->chip->ops->owner))
119 return -ENODEV;
120
121 if (pwm->chip->ops->request) {
122 err = pwm->chip->ops->request(pwm->chip, pwm);
123 if (err) {
124 module_put(pwm->chip->ops->owner);
125 return err;
126 }
127 }
128
129 set_bit(PWMF_REQUESTED, &pwm->flags);
130 pwm->label = label;
131
132 return 0;
133}
134
83af2402
PA
135struct pwm_device *
136of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
137{
138 struct pwm_device *pwm;
139
140 if (pc->of_pwm_n_cells < 3)
141 return ERR_PTR(-EINVAL);
142
143 if (args->args[0] >= pc->npwm)
144 return ERR_PTR(-EINVAL);
145
146 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
147 if (IS_ERR(pwm))
148 return pwm;
149
150 pwm_set_period(pwm, args->args[1]);
151
152 if (args->args[2] & PWM_SPEC_POLARITY)
153 pwm_set_polarity(pwm, PWM_POLARITY_INVERSED);
154 else
155 pwm_set_polarity(pwm, PWM_POLARITY_NORMAL);
156
157 return pwm;
158}
159
e50d3523
SK
160static struct pwm_device *
161of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
7299ab70
TR
162{
163 struct pwm_device *pwm;
164
165 if (pc->of_pwm_n_cells < 2)
166 return ERR_PTR(-EINVAL);
167
168 if (args->args[0] >= pc->npwm)
169 return ERR_PTR(-EINVAL);
170
171 pwm = pwm_request_from_chip(pc, args->args[0], NULL);
172 if (IS_ERR(pwm))
173 return pwm;
174
175 pwm_set_period(pwm, args->args[1]);
176
177 return pwm;
178}
179
dfeb86ec 180static void of_pwmchip_add(struct pwm_chip *chip)
7299ab70
TR
181{
182 if (!chip->dev || !chip->dev->of_node)
183 return;
184
185 if (!chip->of_xlate) {
186 chip->of_xlate = of_pwm_simple_xlate;
187 chip->of_pwm_n_cells = 2;
188 }
189
190 of_node_get(chip->dev->of_node);
191}
192
dfeb86ec 193static void of_pwmchip_remove(struct pwm_chip *chip)
7299ab70
TR
194{
195 if (chip->dev && chip->dev->of_node)
196 of_node_put(chip->dev->of_node);
197}
198
f051c466
TR
199/**
200 * pwm_set_chip_data() - set private chip data for a PWM
201 * @pwm: PWM device
202 * @data: pointer to chip-specific data
203 */
204int pwm_set_chip_data(struct pwm_device *pwm, void *data)
205{
206 if (!pwm)
207 return -EINVAL;
208
209 pwm->chip_data = data;
210
211 return 0;
212}
213
214/**
215 * pwm_get_chip_data() - get private chip data for a PWM
216 * @pwm: PWM device
217 */
218void *pwm_get_chip_data(struct pwm_device *pwm)
219{
220 return pwm ? pwm->chip_data : NULL;
0c2498f1
SH
221}
222
223/**
224 * pwmchip_add() - register a new PWM chip
225 * @chip: the PWM chip to add
f051c466
TR
226 *
227 * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
228 * will be used.
0c2498f1
SH
229 */
230int pwmchip_add(struct pwm_chip *chip)
231{
232 struct pwm_device *pwm;
f051c466
TR
233 unsigned int i;
234 int ret;
0c2498f1 235
f051c466
TR
236 if (!chip || !chip->dev || !chip->ops || !chip->ops->config ||
237 !chip->ops->enable || !chip->ops->disable)
238 return -EINVAL;
0c2498f1
SH
239
240 mutex_lock(&pwm_lock);
241
f051c466
TR
242 ret = alloc_pwms(chip->base, chip->npwm);
243 if (ret < 0)
244 goto out;
245
246 chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL);
247 if (!chip->pwms) {
248 ret = -ENOMEM;
0c2498f1
SH
249 goto out;
250 }
251
f051c466
TR
252 chip->base = ret;
253
254 for (i = 0; i < chip->npwm; i++) {
255 pwm = &chip->pwms[i];
256
257 pwm->chip = chip;
258 pwm->pwm = chip->base + i;
259 pwm->hwpwm = i;
0c2498f1 260
f051c466
TR
261 radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
262 }
263
264 bitmap_set(allocated_pwms, chip->base, chip->npwm);
265
266 INIT_LIST_HEAD(&chip->list);
267 list_add(&chip->list, &pwm_chips);
0c2498f1 268
f051c466
TR
269 ret = 0;
270
7299ab70
TR
271 if (IS_ENABLED(CONFIG_OF))
272 of_pwmchip_add(chip);
273
f051c466
TR
274out:
275 mutex_unlock(&pwm_lock);
0c2498f1
SH
276 return ret;
277}
278EXPORT_SYMBOL_GPL(pwmchip_add);
279
280/**
281 * pwmchip_remove() - remove a PWM chip
282 * @chip: the PWM chip to remove
283 *
284 * Removes a PWM chip. This function may return busy if the PWM chip provides
285 * a PWM device that is still requested.
286 */
287int pwmchip_remove(struct pwm_chip *chip)
288{
f051c466 289 unsigned int i;
0c2498f1
SH
290 int ret = 0;
291
292 mutex_lock(&pwm_lock);
293
f051c466
TR
294 for (i = 0; i < chip->npwm; i++) {
295 struct pwm_device *pwm = &chip->pwms[i];
0c2498f1 296
f051c466
TR
297 if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
298 ret = -EBUSY;
299 goto out;
300 }
0c2498f1
SH
301 }
302
f051c466 303 list_del_init(&chip->list);
7299ab70
TR
304
305 if (IS_ENABLED(CONFIG_OF))
306 of_pwmchip_remove(chip);
307
f051c466 308 free_pwms(chip);
0c2498f1 309
0c2498f1
SH
310out:
311 mutex_unlock(&pwm_lock);
0c2498f1
SH
312 return ret;
313}
314EXPORT_SYMBOL_GPL(pwmchip_remove);
315
316/**
317 * pwm_request() - request a PWM device
318 * @pwm_id: global PWM device index
319 * @label: PWM device label
8138d2dd
TR
320 *
321 * This function is deprecated, use pwm_get() instead.
0c2498f1 322 */
f051c466 323struct pwm_device *pwm_request(int pwm, const char *label)
0c2498f1 324{
f051c466
TR
325 struct pwm_device *dev;
326 int err;
327
328 if (pwm < 0 || pwm >= MAX_PWMS)
329 return ERR_PTR(-EINVAL);
0c2498f1
SH
330
331 mutex_lock(&pwm_lock);
332
f051c466
TR
333 dev = pwm_to_device(pwm);
334 if (!dev) {
335 dev = ERR_PTR(-EPROBE_DEFER);
0c2498f1
SH
336 goto out;
337 }
338
f051c466
TR
339 err = pwm_device_request(dev, label);
340 if (err < 0)
341 dev = ERR_PTR(err);
0c2498f1 342
f051c466
TR
343out:
344 mutex_unlock(&pwm_lock);
0c2498f1 345
f051c466
TR
346 return dev;
347}
348EXPORT_SYMBOL_GPL(pwm_request);
0c2498f1 349
f051c466
TR
350/**
351 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
352 * @chip: PWM chip
353 * @index: per-chip index of the PWM to request
354 * @label: a literal description string of this PWM
355 *
356 * Returns the PWM at the given index of the given PWM chip. A negative error
357 * code is returned if the index is not valid for the specified PWM chip or
358 * if the PWM device cannot be requested.
359 */
360struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
361 unsigned int index,
362 const char *label)
363{
364 struct pwm_device *pwm;
365 int err;
0c2498f1 366
f051c466
TR
367 if (!chip || index >= chip->npwm)
368 return ERR_PTR(-EINVAL);
0c2498f1 369
f051c466
TR
370 mutex_lock(&pwm_lock);
371 pwm = &chip->pwms[index];
0c2498f1 372
f051c466
TR
373 err = pwm_device_request(pwm, label);
374 if (err < 0)
375 pwm = ERR_PTR(err);
376
377 mutex_unlock(&pwm_lock);
0c2498f1
SH
378 return pwm;
379}
f051c466 380EXPORT_SYMBOL_GPL(pwm_request_from_chip);
0c2498f1
SH
381
382/**
383 * pwm_free() - free a PWM device
384 * @pwm: PWM device
8138d2dd
TR
385 *
386 * This function is deprecated, use pwm_put() instead.
0c2498f1
SH
387 */
388void pwm_free(struct pwm_device *pwm)
389{
8138d2dd 390 pwm_put(pwm);
0c2498f1
SH
391}
392EXPORT_SYMBOL_GPL(pwm_free);
393
394/**
395 * pwm_config() - change a PWM device configuration
396 * @pwm: PWM device
397 * @duty_ns: "on" time (in nanoseconds)
398 * @period_ns: duration (in nanoseconds) of one cycle
399 */
400int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
401{
c2d476a9 402 if (!pwm || duty_ns < 0 || period_ns <= 0 || duty_ns > period_ns)
f051c466
TR
403 return -EINVAL;
404
405 return pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns);
0c2498f1
SH
406}
407EXPORT_SYMBOL_GPL(pwm_config);
408
0aa0869c
PA
409/**
410 * pwm_set_polarity() - configure the polarity of a PWM signal
411 * @pwm: PWM device
412 * @polarity: new polarity of the PWM signal
413 *
414 * Note that the polarity cannot be configured while the PWM device is enabled
415 */
416int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity)
417{
418 if (!pwm || !pwm->chip->ops)
419 return -EINVAL;
420
421 if (!pwm->chip->ops->set_polarity)
422 return -ENOSYS;
423
424 if (test_bit(PWMF_ENABLED, &pwm->flags))
425 return -EBUSY;
426
427 return pwm->chip->ops->set_polarity(pwm->chip, pwm, polarity);
428}
429EXPORT_SYMBOL_GPL(pwm_set_polarity);
430
0c2498f1
SH
431/**
432 * pwm_enable() - start a PWM output toggling
433 * @pwm: PWM device
434 */
435int pwm_enable(struct pwm_device *pwm)
436{
f051c466
TR
437 if (pwm && !test_and_set_bit(PWMF_ENABLED, &pwm->flags))
438 return pwm->chip->ops->enable(pwm->chip, pwm);
0c2498f1 439
f051c466 440 return pwm ? 0 : -EINVAL;
0c2498f1
SH
441}
442EXPORT_SYMBOL_GPL(pwm_enable);
443
444/**
445 * pwm_disable() - stop a PWM output toggling
446 * @pwm: PWM device
447 */
448void pwm_disable(struct pwm_device *pwm)
449{
f051c466
TR
450 if (pwm && test_and_clear_bit(PWMF_ENABLED, &pwm->flags))
451 pwm->chip->ops->disable(pwm->chip, pwm);
0c2498f1
SH
452}
453EXPORT_SYMBOL_GPL(pwm_disable);
62099abf 454
7299ab70
TR
455static struct pwm_chip *of_node_to_pwmchip(struct device_node *np)
456{
457 struct pwm_chip *chip;
458
459 mutex_lock(&pwm_lock);
460
461 list_for_each_entry(chip, &pwm_chips, list)
462 if (chip->dev && chip->dev->of_node == np) {
463 mutex_unlock(&pwm_lock);
464 return chip;
465 }
466
467 mutex_unlock(&pwm_lock);
468
469 return ERR_PTR(-EPROBE_DEFER);
470}
471
472/**
473 * of_pwm_request() - request a PWM via the PWM framework
474 * @np: device node to get the PWM from
475 * @con_id: consumer name
476 *
477 * Returns the PWM device parsed from the phandle and index specified in the
478 * "pwms" property of a device tree node or a negative error-code on failure.
479 * Values parsed from the device tree are stored in the returned PWM device
480 * object.
481 *
482 * If con_id is NULL, the first PWM device listed in the "pwms" property will
483 * be requested. Otherwise the "pwm-names" property is used to do a reverse
484 * lookup of the PWM index. This also means that the "pwm-names" property
485 * becomes mandatory for devices that look up the PWM device via the con_id
486 * parameter.
487 */
488static struct pwm_device *of_pwm_request(struct device_node *np,
489 const char *con_id)
490{
491 struct pwm_device *pwm = NULL;
492 struct of_phandle_args args;
493 struct pwm_chip *pc;
494 int index = 0;
495 int err;
496
497 if (con_id) {
498 index = of_property_match_string(np, "pwm-names", con_id);
499 if (index < 0)
500 return ERR_PTR(index);
501 }
502
503 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
504 &args);
505 if (err) {
506 pr_debug("%s(): can't parse \"pwms\" property\n", __func__);
507 return ERR_PTR(err);
508 }
509
510 pc = of_node_to_pwmchip(args.np);
511 if (IS_ERR(pc)) {
512 pr_debug("%s(): PWM chip not found\n", __func__);
513 pwm = ERR_CAST(pc);
514 goto put;
515 }
516
517 if (args.args_count != pc->of_pwm_n_cells) {
518 pr_debug("%s: wrong #pwm-cells for %s\n", np->full_name,
519 args.np->full_name);
520 pwm = ERR_PTR(-EINVAL);
521 goto put;
522 }
523
524 pwm = pc->of_xlate(pc, &args);
525 if (IS_ERR(pwm))
526 goto put;
527
528 /*
529 * If a consumer name was not given, try to look it up from the
530 * "pwm-names" property if it exists. Otherwise use the name of
531 * the user device node.
532 */
533 if (!con_id) {
534 err = of_property_read_string_index(np, "pwm-names", index,
535 &con_id);
536 if (err < 0)
537 con_id = np->name;
538 }
539
540 pwm->label = con_id;
541
542put:
543 of_node_put(args.np);
544
545 return pwm;
546}
547
8138d2dd
TR
548/**
549 * pwm_add_table() - register PWM device consumers
550 * @table: array of consumers to register
551 * @num: number of consumers in table
552 */
553void __init pwm_add_table(struct pwm_lookup *table, size_t num)
554{
555 mutex_lock(&pwm_lookup_lock);
556
557 while (num--) {
558 list_add_tail(&table->list, &pwm_lookup_list);
559 table++;
560 }
561
562 mutex_unlock(&pwm_lookup_lock);
563}
564
565/**
566 * pwm_get() - look up and request a PWM device
567 * @dev: device for PWM consumer
568 * @con_id: consumer name
569 *
7299ab70
TR
570 * Lookup is first attempted using DT. If the device was not instantiated from
571 * a device tree, a PWM chip and a relative index is looked up via a table
572 * supplied by board setup code (see pwm_add_table()).
8138d2dd
TR
573 *
574 * Once a PWM chip has been found the specified PWM device will be requested
575 * and is ready to be used.
576 */
577struct pwm_device *pwm_get(struct device *dev, const char *con_id)
578{
579 struct pwm_device *pwm = ERR_PTR(-EPROBE_DEFER);
e50d3523 580 const char *dev_id = dev ? dev_name(dev) : NULL;
8138d2dd 581 struct pwm_chip *chip = NULL;
d295b129 582 unsigned int index = 0;
8138d2dd
TR
583 unsigned int best = 0;
584 struct pwm_lookup *p;
8138d2dd
TR
585 unsigned int match;
586
7299ab70
TR
587 /* look up via DT first */
588 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
589 return of_pwm_request(dev->of_node, con_id);
590
8138d2dd
TR
591 /*
592 * We look up the provider in the static table typically provided by
593 * board setup code. We first try to lookup the consumer device by
594 * name. If the consumer device was passed in as NULL or if no match
595 * was found, we try to find the consumer by directly looking it up
596 * by name.
597 *
598 * If a match is found, the provider PWM chip is looked up by name
599 * and a PWM device is requested using the PWM device per-chip index.
600 *
601 * The lookup algorithm was shamelessly taken from the clock
602 * framework:
603 *
604 * We do slightly fuzzy matching here:
605 * An entry with a NULL ID is assumed to be a wildcard.
606 * If an entry has a device ID, it must match
607 * If an entry has a connection ID, it must match
608 * Then we take the most specific entry - with the following order
609 * of precedence: dev+con > dev only > con only.
610 */
611 mutex_lock(&pwm_lookup_lock);
612
613 list_for_each_entry(p, &pwm_lookup_list, list) {
614 match = 0;
615
616 if (p->dev_id) {
617 if (!dev_id || strcmp(p->dev_id, dev_id))
618 continue;
619
620 match += 2;
621 }
622
623 if (p->con_id) {
624 if (!con_id || strcmp(p->con_id, con_id))
625 continue;
626
627 match += 1;
628 }
629
630 if (match > best) {
631 chip = pwmchip_find_by_name(p->provider);
632 index = p->index;
633
634 if (match != 3)
635 best = match;
636 else
637 break;
638 }
639 }
640
641 if (chip)
642 pwm = pwm_request_from_chip(chip, index, con_id ?: dev_id);
643
644 mutex_unlock(&pwm_lookup_lock);
645
646 return pwm;
647}
648EXPORT_SYMBOL_GPL(pwm_get);
649
650/**
651 * pwm_put() - release a PWM device
652 * @pwm: PWM device
653 */
654void pwm_put(struct pwm_device *pwm)
655{
656 if (!pwm)
657 return;
658
659 mutex_lock(&pwm_lock);
660
661 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
e50d3523 662 pr_warn("PWM device already freed\n");
8138d2dd
TR
663 goto out;
664 }
665
666 if (pwm->chip->ops->free)
667 pwm->chip->ops->free(pwm->chip, pwm);
668
669 pwm->label = NULL;
670
671 module_put(pwm->chip->ops->owner);
672out:
673 mutex_unlock(&pwm_lock);
674}
675EXPORT_SYMBOL_GPL(pwm_put);
676
6354316d
AC
677static void devm_pwm_release(struct device *dev, void *res)
678{
679 pwm_put(*(struct pwm_device **)res);
680}
681
682/**
683 * devm_pwm_get() - resource managed pwm_get()
684 * @dev: device for PWM consumer
685 * @con_id: consumer name
686 *
687 * This function performs like pwm_get() but the acquired PWM device will
688 * automatically be released on driver detach.
689 */
690struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
691{
692 struct pwm_device **ptr, *pwm;
693
694 ptr = devres_alloc(devm_pwm_release, sizeof(**ptr), GFP_KERNEL);
695 if (!ptr)
696 return ERR_PTR(-ENOMEM);
697
698 pwm = pwm_get(dev, con_id);
699 if (!IS_ERR(pwm)) {
700 *ptr = pwm;
701 devres_add(dev, ptr);
702 } else {
703 devres_free(ptr);
704 }
705
706 return pwm;
707}
708EXPORT_SYMBOL_GPL(devm_pwm_get);
709
710static int devm_pwm_match(struct device *dev, void *res, void *data)
711{
712 struct pwm_device **p = res;
713
714 if (WARN_ON(!p || !*p))
715 return 0;
716
717 return *p == data;
718}
719
720/**
721 * devm_pwm_put() - resource managed pwm_put()
722 * @dev: device for PWM consumer
723 * @pwm: PWM device
724 *
725 * Release a PWM previously allocated using devm_pwm_get(). Calling this
726 * function is usually not needed because devm-allocated resources are
727 * automatically released on driver detach.
728 */
729void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
730{
731 WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm));
732}
733EXPORT_SYMBOL_GPL(devm_pwm_put);
734
62099abf
TR
735#ifdef CONFIG_DEBUG_FS
736static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
737{
738 unsigned int i;
739
740 for (i = 0; i < chip->npwm; i++) {
741 struct pwm_device *pwm = &chip->pwms[i];
742
743 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
744
745 if (test_bit(PWMF_REQUESTED, &pwm->flags))
746 seq_printf(s, " requested");
747
748 if (test_bit(PWMF_ENABLED, &pwm->flags))
749 seq_printf(s, " enabled");
750
751 seq_printf(s, "\n");
752 }
753}
754
755static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
756{
757 mutex_lock(&pwm_lock);
758 s->private = "";
759
760 return seq_list_start(&pwm_chips, *pos);
761}
762
763static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
764{
765 s->private = "\n";
766
767 return seq_list_next(v, &pwm_chips, pos);
768}
769
770static void pwm_seq_stop(struct seq_file *s, void *v)
771{
772 mutex_unlock(&pwm_lock);
773}
774
775static int pwm_seq_show(struct seq_file *s, void *v)
776{
777 struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
778
779 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
780 chip->dev->bus ? chip->dev->bus->name : "no-bus",
781 dev_name(chip->dev), chip->npwm,
782 (chip->npwm != 1) ? "s" : "");
783
784 if (chip->ops->dbg_show)
785 chip->ops->dbg_show(chip, s);
786 else
787 pwm_dbg_show(chip, s);
788
789 return 0;
790}
791
792static const struct seq_operations pwm_seq_ops = {
793 .start = pwm_seq_start,
794 .next = pwm_seq_next,
795 .stop = pwm_seq_stop,
796 .show = pwm_seq_show,
797};
798
799static int pwm_seq_open(struct inode *inode, struct file *file)
800{
801 return seq_open(file, &pwm_seq_ops);
802}
803
804static const struct file_operations pwm_debugfs_ops = {
805 .owner = THIS_MODULE,
806 .open = pwm_seq_open,
807 .read = seq_read,
808 .llseek = seq_lseek,
809 .release = seq_release,
810};
811
812static int __init pwm_debugfs_init(void)
813{
814 debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
815 &pwm_debugfs_ops);
816
817 return 0;
818}
819
820subsys_initcall(pwm_debugfs_init);
821#endif /* CONFIG_DEBUG_FS */