22 #include <linux/module.h>
25 #include <linux/list.h>
28 #include <linux/slab.h>
29 #include <linux/device.h>
47 static int alloc_pwms(
int pwm,
unsigned int count)
49 unsigned int from = 0;
61 if (pwm >= 0 && start != pwm)
74 for (i = 0; i < chip->
npwm; i++) {
85 static struct pwm_chip *pwmchip_find_by_name(
const char *
name)
95 const char *chip_name = dev_name(chip->
dev);
97 if (chip_name &&
strcmp(chip_name, name) == 0) {
108 static int pwm_device_request(
struct pwm_device *pwm,
const char *
label)
115 if (!try_module_get(pwm->
chip->ops->owner))
118 if (pwm->
chip->ops->request) {
119 err = pwm->
chip->ops->request(pwm->
chip, pwm);
121 module_put(pwm->
chip->ops->owner);
147 pwm_set_period(pwm, args->
args[1]);
152 static void of_pwmchip_add(
struct pwm_chip *chip)
154 if (!chip->
dev || !chip->
dev->of_node)
158 chip->
of_xlate = of_pwm_simple_xlate;
162 of_node_get(chip->
dev->of_node);
165 static void of_pwmchip_remove(
struct pwm_chip *chip)
167 if (chip->
dev && chip->
dev->of_node)
168 of_node_put(chip->
dev->of_node);
208 if (!chip || !chip->
dev || !chip->
ops || !chip->
ops->config ||
209 !chip->
ops->enable || !chip->
ops->disable)
214 ret = alloc_pwms(chip->
base, chip->
npwm);
226 for (i = 0; i < chip->
npwm; i++) {
227 pwm = &chip->
pwms[
i];
238 INIT_LIST_HEAD(&chip->
list);
239 list_add(&chip->
list, &pwm_chips);
244 of_pwmchip_add(chip);
266 for (i = 0; i < chip->
npwm; i++) {
275 list_del_init(&chip->
list);
278 of_pwmchip_remove(chip);
305 dev = pwm_to_device(pwm);
311 err = pwm_device_request(dev, label);
339 if (!chip || index >= chip->
npwm)
345 err = pwm_device_request(pwm, label);
374 if (!pwm || duty_ns < 0 || period_ns <= 0 || duty_ns > period_ns)
377 return pwm->
chip->ops->config(pwm->
chip, pwm, duty_ns, period_ns);
390 if (!pwm || !pwm->
chip->ops)
393 if (!pwm->
chip->ops->set_polarity)
399 return pwm->
chip->ops->set_polarity(pwm->
chip, pwm, polarity);
410 return pwm->
chip->ops->enable(pwm->
chip, pwm);
423 pwm->
chip->ops->disable(pwm->
chip, pwm);
434 if (chip->
dev && chip->
dev->of_node == np) {
472 return ERR_PTR(index);
478 pr_debug(
"%s(): can't parse \"pwms\" property\n", __func__);
482 pc = of_node_to_pwmchip(args.
np);
484 pr_debug(
"%s(): PWM chip not found\n", __func__);
515 of_node_put(args.
np);
552 const char *
dev_id = dev ? dev_name(dev) :
NULL;
554 unsigned int index = 0;
555 unsigned int best = 0;
561 return of_pwm_request(dev->
of_node, con_id);
603 chip = pwmchip_find_by_name(p->
provider);
634 pr_warn(
"PWM device already freed\n");
638 if (pwm->
chip->ops->free)
639 pwm->
chip->ops->free(pwm->
chip, pwm);
643 module_put(pwm->
chip->ops->owner);
649 static void devm_pwm_release(
struct device *
dev,
void *
res)
682 static int devm_pwm_match(
struct device *dev,
void *res,
void *
data)
707 #ifdef CONFIG_DEBUG_FS
712 for (i = 0; i < chip->
npwm; i++) {
727 static void *pwm_seq_start(
struct seq_file *s, loff_t *
pos)
735 static void *pwm_seq_next(
struct seq_file *s,
void *
v, loff_t *
pos)
742 static void pwm_seq_stop(
struct seq_file *s,
void *
v)
747 static int pwm_seq_show(
struct seq_file *s,
void *
v)
752 chip->
dev->bus ? chip->
dev->bus->name :
"no-bus",
753 dev_name(chip->
dev), chip->
npwm,
754 (chip->
npwm != 1) ?
"s" :
"");
756 if (chip->
ops->dbg_show)
757 chip->
ops->dbg_show(chip, s);
759 pwm_dbg_show(chip, s);
765 .
start = pwm_seq_start,
766 .next = pwm_seq_next,
767 .stop = pwm_seq_stop,
768 .show = pwm_seq_show,
773 return seq_open(file, &pwm_seq_ops);
778 .open = pwm_seq_open,
784 static int __init pwm_debugfs_init(
void)