Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Generic pwmlib implementation
0004  *
0005  * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
0006  * Copyright (C) 2011-2012 Avionic Design GmbH
0007  */
0008 
0009 #include <linux/acpi.h>
0010 #include <linux/module.h>
0011 #include <linux/pwm.h>
0012 #include <linux/radix-tree.h>
0013 #include <linux/list.h>
0014 #include <linux/mutex.h>
0015 #include <linux/err.h>
0016 #include <linux/slab.h>
0017 #include <linux/device.h>
0018 #include <linux/debugfs.h>
0019 #include <linux/seq_file.h>
0020 
0021 #include <dt-bindings/pwm/pwm.h>
0022 
0023 #define CREATE_TRACE_POINTS
0024 #include <trace/events/pwm.h>
0025 
0026 #define MAX_PWMS 1024
0027 
0028 static DEFINE_MUTEX(pwm_lookup_lock);
0029 static LIST_HEAD(pwm_lookup_list);
0030 static DEFINE_MUTEX(pwm_lock);
0031 static LIST_HEAD(pwm_chips);
0032 static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
0033 static RADIX_TREE(pwm_tree, GFP_KERNEL);
0034 
0035 static struct pwm_device *pwm_to_device(unsigned int pwm)
0036 {
0037     return radix_tree_lookup(&pwm_tree, pwm);
0038 }
0039 
0040 static int alloc_pwms(unsigned int count)
0041 {
0042     unsigned int start;
0043 
0044     start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, 0,
0045                        count, 0);
0046 
0047     if (start + count > MAX_PWMS)
0048         return -ENOSPC;
0049 
0050     return start;
0051 }
0052 
0053 static void free_pwms(struct pwm_chip *chip)
0054 {
0055     unsigned int i;
0056 
0057     for (i = 0; i < chip->npwm; i++) {
0058         struct pwm_device *pwm = &chip->pwms[i];
0059 
0060         radix_tree_delete(&pwm_tree, pwm->pwm);
0061     }
0062 
0063     bitmap_clear(allocated_pwms, chip->base, chip->npwm);
0064 
0065     kfree(chip->pwms);
0066     chip->pwms = NULL;
0067 }
0068 
0069 static struct pwm_chip *pwmchip_find_by_name(const char *name)
0070 {
0071     struct pwm_chip *chip;
0072 
0073     if (!name)
0074         return NULL;
0075 
0076     mutex_lock(&pwm_lock);
0077 
0078     list_for_each_entry(chip, &pwm_chips, list) {
0079         const char *chip_name = dev_name(chip->dev);
0080 
0081         if (chip_name && strcmp(chip_name, name) == 0) {
0082             mutex_unlock(&pwm_lock);
0083             return chip;
0084         }
0085     }
0086 
0087     mutex_unlock(&pwm_lock);
0088 
0089     return NULL;
0090 }
0091 
0092 static int pwm_device_request(struct pwm_device *pwm, const char *label)
0093 {
0094     int err;
0095 
0096     if (test_bit(PWMF_REQUESTED, &pwm->flags))
0097         return -EBUSY;
0098 
0099     if (!try_module_get(pwm->chip->ops->owner))
0100         return -ENODEV;
0101 
0102     if (pwm->chip->ops->request) {
0103         err = pwm->chip->ops->request(pwm->chip, pwm);
0104         if (err) {
0105             module_put(pwm->chip->ops->owner);
0106             return err;
0107         }
0108     }
0109 
0110     if (pwm->chip->ops->get_state) {
0111         pwm->chip->ops->get_state(pwm->chip, pwm, &pwm->state);
0112         trace_pwm_get(pwm, &pwm->state);
0113 
0114         if (IS_ENABLED(CONFIG_PWM_DEBUG))
0115             pwm->last = pwm->state;
0116     }
0117 
0118     set_bit(PWMF_REQUESTED, &pwm->flags);
0119     pwm->label = label;
0120 
0121     return 0;
0122 }
0123 
0124 struct pwm_device *
0125 of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
0126 {
0127     struct pwm_device *pwm;
0128 
0129     if (pc->of_pwm_n_cells < 2)
0130         return ERR_PTR(-EINVAL);
0131 
0132     /* flags in the third cell are optional */
0133     if (args->args_count < 2)
0134         return ERR_PTR(-EINVAL);
0135 
0136     if (args->args[0] >= pc->npwm)
0137         return ERR_PTR(-EINVAL);
0138 
0139     pwm = pwm_request_from_chip(pc, args->args[0], NULL);
0140     if (IS_ERR(pwm))
0141         return pwm;
0142 
0143     pwm->args.period = args->args[1];
0144     pwm->args.polarity = PWM_POLARITY_NORMAL;
0145 
0146     if (pc->of_pwm_n_cells >= 3) {
0147         if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
0148             pwm->args.polarity = PWM_POLARITY_INVERSED;
0149     }
0150 
0151     return pwm;
0152 }
0153 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
0154 
0155 struct pwm_device *
0156 of_pwm_single_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
0157 {
0158     struct pwm_device *pwm;
0159 
0160     if (pc->of_pwm_n_cells < 1)
0161         return ERR_PTR(-EINVAL);
0162 
0163     /* validate that one cell is specified, optionally with flags */
0164     if (args->args_count != 1 && args->args_count != 2)
0165         return ERR_PTR(-EINVAL);
0166 
0167     pwm = pwm_request_from_chip(pc, 0, NULL);
0168     if (IS_ERR(pwm))
0169         return pwm;
0170 
0171     pwm->args.period = args->args[0];
0172     pwm->args.polarity = PWM_POLARITY_NORMAL;
0173 
0174     if (args->args_count == 2 && args->args[2] & PWM_POLARITY_INVERTED)
0175         pwm->args.polarity = PWM_POLARITY_INVERSED;
0176 
0177     return pwm;
0178 }
0179 EXPORT_SYMBOL_GPL(of_pwm_single_xlate);
0180 
0181 static void of_pwmchip_add(struct pwm_chip *chip)
0182 {
0183     if (!chip->dev || !chip->dev->of_node)
0184         return;
0185 
0186     if (!chip->of_xlate) {
0187         u32 pwm_cells;
0188 
0189         if (of_property_read_u32(chip->dev->of_node, "#pwm-cells",
0190                      &pwm_cells))
0191             pwm_cells = 2;
0192 
0193         chip->of_xlate = of_pwm_xlate_with_flags;
0194         chip->of_pwm_n_cells = pwm_cells;
0195     }
0196 
0197     of_node_get(chip->dev->of_node);
0198 }
0199 
0200 static void of_pwmchip_remove(struct pwm_chip *chip)
0201 {
0202     if (chip->dev)
0203         of_node_put(chip->dev->of_node);
0204 }
0205 
0206 /**
0207  * pwm_set_chip_data() - set private chip data for a PWM
0208  * @pwm: PWM device
0209  * @data: pointer to chip-specific data
0210  *
0211  * Returns: 0 on success or a negative error code on failure.
0212  */
0213 int pwm_set_chip_data(struct pwm_device *pwm, void *data)
0214 {
0215     if (!pwm)
0216         return -EINVAL;
0217 
0218     pwm->chip_data = data;
0219 
0220     return 0;
0221 }
0222 EXPORT_SYMBOL_GPL(pwm_set_chip_data);
0223 
0224 /**
0225  * pwm_get_chip_data() - get private chip data for a PWM
0226  * @pwm: PWM device
0227  *
0228  * Returns: A pointer to the chip-private data for the PWM device.
0229  */
0230 void *pwm_get_chip_data(struct pwm_device *pwm)
0231 {
0232     return pwm ? pwm->chip_data : NULL;
0233 }
0234 EXPORT_SYMBOL_GPL(pwm_get_chip_data);
0235 
0236 static bool pwm_ops_check(const struct pwm_chip *chip)
0237 {
0238     const struct pwm_ops *ops = chip->ops;
0239 
0240     if (!ops->apply)
0241         return false;
0242 
0243     if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
0244         dev_warn(chip->dev,
0245              "Please implement the .get_state() callback\n");
0246 
0247     return true;
0248 }
0249 
0250 /**
0251  * pwmchip_add() - register a new PWM chip
0252  * @chip: the PWM chip to add
0253  *
0254  * Register a new PWM chip.
0255  *
0256  * Returns: 0 on success or a negative error code on failure.
0257  */
0258 int pwmchip_add(struct pwm_chip *chip)
0259 {
0260     struct pwm_device *pwm;
0261     unsigned int i;
0262     int ret;
0263 
0264     if (!chip || !chip->dev || !chip->ops || !chip->npwm)
0265         return -EINVAL;
0266 
0267     if (!pwm_ops_check(chip))
0268         return -EINVAL;
0269 
0270     mutex_lock(&pwm_lock);
0271 
0272     ret = alloc_pwms(chip->npwm);
0273     if (ret < 0)
0274         goto out;
0275 
0276     chip->base = ret;
0277 
0278     chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL);
0279     if (!chip->pwms) {
0280         ret = -ENOMEM;
0281         goto out;
0282     }
0283 
0284     for (i = 0; i < chip->npwm; i++) {
0285         pwm = &chip->pwms[i];
0286 
0287         pwm->chip = chip;
0288         pwm->pwm = chip->base + i;
0289         pwm->hwpwm = i;
0290 
0291         radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
0292     }
0293 
0294     bitmap_set(allocated_pwms, chip->base, chip->npwm);
0295 
0296     INIT_LIST_HEAD(&chip->list);
0297     list_add(&chip->list, &pwm_chips);
0298 
0299     ret = 0;
0300 
0301     if (IS_ENABLED(CONFIG_OF))
0302         of_pwmchip_add(chip);
0303 
0304 out:
0305     mutex_unlock(&pwm_lock);
0306 
0307     if (!ret)
0308         pwmchip_sysfs_export(chip);
0309 
0310     return ret;
0311 }
0312 EXPORT_SYMBOL_GPL(pwmchip_add);
0313 
0314 /**
0315  * pwmchip_remove() - remove a PWM chip
0316  * @chip: the PWM chip to remove
0317  *
0318  * Removes a PWM chip. This function may return busy if the PWM chip provides
0319  * a PWM device that is still requested.
0320  *
0321  * Returns: 0 on success or a negative error code on failure.
0322  */
0323 void pwmchip_remove(struct pwm_chip *chip)
0324 {
0325     pwmchip_sysfs_unexport(chip);
0326 
0327     mutex_lock(&pwm_lock);
0328 
0329     list_del_init(&chip->list);
0330 
0331     if (IS_ENABLED(CONFIG_OF))
0332         of_pwmchip_remove(chip);
0333 
0334     free_pwms(chip);
0335 
0336     mutex_unlock(&pwm_lock);
0337 }
0338 EXPORT_SYMBOL_GPL(pwmchip_remove);
0339 
0340 static void devm_pwmchip_remove(void *data)
0341 {
0342     struct pwm_chip *chip = data;
0343 
0344     pwmchip_remove(chip);
0345 }
0346 
0347 int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
0348 {
0349     int ret;
0350 
0351     ret = pwmchip_add(chip);
0352     if (ret)
0353         return ret;
0354 
0355     return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
0356 }
0357 EXPORT_SYMBOL_GPL(devm_pwmchip_add);
0358 
0359 /**
0360  * pwm_request() - request a PWM device
0361  * @pwm: global PWM device index
0362  * @label: PWM device label
0363  *
0364  * This function is deprecated, use pwm_get() instead.
0365  *
0366  * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on
0367  * failure.
0368  */
0369 struct pwm_device *pwm_request(int pwm, const char *label)
0370 {
0371     struct pwm_device *dev;
0372     int err;
0373 
0374     if (pwm < 0 || pwm >= MAX_PWMS)
0375         return ERR_PTR(-EINVAL);
0376 
0377     mutex_lock(&pwm_lock);
0378 
0379     dev = pwm_to_device(pwm);
0380     if (!dev) {
0381         dev = ERR_PTR(-EPROBE_DEFER);
0382         goto out;
0383     }
0384 
0385     err = pwm_device_request(dev, label);
0386     if (err < 0)
0387         dev = ERR_PTR(err);
0388 
0389 out:
0390     mutex_unlock(&pwm_lock);
0391 
0392     return dev;
0393 }
0394 EXPORT_SYMBOL_GPL(pwm_request);
0395 
0396 /**
0397  * pwm_request_from_chip() - request a PWM device relative to a PWM chip
0398  * @chip: PWM chip
0399  * @index: per-chip index of the PWM to request
0400  * @label: a literal description string of this PWM
0401  *
0402  * Returns: A pointer to the PWM device at the given index of the given PWM
0403  * chip. A negative error code is returned if the index is not valid for the
0404  * specified PWM chip or if the PWM device cannot be requested.
0405  */
0406 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
0407                      unsigned int index,
0408                      const char *label)
0409 {
0410     struct pwm_device *pwm;
0411     int err;
0412 
0413     if (!chip || index >= chip->npwm)
0414         return ERR_PTR(-EINVAL);
0415 
0416     mutex_lock(&pwm_lock);
0417     pwm = &chip->pwms[index];
0418 
0419     err = pwm_device_request(pwm, label);
0420     if (err < 0)
0421         pwm = ERR_PTR(err);
0422 
0423     mutex_unlock(&pwm_lock);
0424     return pwm;
0425 }
0426 EXPORT_SYMBOL_GPL(pwm_request_from_chip);
0427 
0428 /**
0429  * pwm_free() - free a PWM device
0430  * @pwm: PWM device
0431  *
0432  * This function is deprecated, use pwm_put() instead.
0433  */
0434 void pwm_free(struct pwm_device *pwm)
0435 {
0436     pwm_put(pwm);
0437 }
0438 EXPORT_SYMBOL_GPL(pwm_free);
0439 
0440 static void pwm_apply_state_debug(struct pwm_device *pwm,
0441                   const struct pwm_state *state)
0442 {
0443     struct pwm_state *last = &pwm->last;
0444     struct pwm_chip *chip = pwm->chip;
0445     struct pwm_state s1, s2;
0446     int err;
0447 
0448     if (!IS_ENABLED(CONFIG_PWM_DEBUG))
0449         return;
0450 
0451     /* No reasonable diagnosis possible without .get_state() */
0452     if (!chip->ops->get_state)
0453         return;
0454 
0455     /*
0456      * *state was just applied. Read out the hardware state and do some
0457      * checks.
0458      */
0459 
0460     chip->ops->get_state(chip, pwm, &s1);
0461     trace_pwm_get(pwm, &s1);
0462 
0463     /*
0464      * The lowlevel driver either ignored .polarity (which is a bug) or as
0465      * best effort inverted .polarity and fixed .duty_cycle respectively.
0466      * Undo this inversion and fixup for further tests.
0467      */
0468     if (s1.enabled && s1.polarity != state->polarity) {
0469         s2.polarity = state->polarity;
0470         s2.duty_cycle = s1.period - s1.duty_cycle;
0471         s2.period = s1.period;
0472         s2.enabled = s1.enabled;
0473     } else {
0474         s2 = s1;
0475     }
0476 
0477     if (s2.polarity != state->polarity &&
0478         state->duty_cycle < state->period)
0479         dev_warn(chip->dev, ".apply ignored .polarity\n");
0480 
0481     if (state->enabled &&
0482         last->polarity == state->polarity &&
0483         last->period > s2.period &&
0484         last->period <= state->period)
0485         dev_warn(chip->dev,
0486              ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
0487              state->period, s2.period, last->period);
0488 
0489     if (state->enabled && state->period < s2.period)
0490         dev_warn(chip->dev,
0491              ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
0492              state->period, s2.period);
0493 
0494     if (state->enabled &&
0495         last->polarity == state->polarity &&
0496         last->period == s2.period &&
0497         last->duty_cycle > s2.duty_cycle &&
0498         last->duty_cycle <= state->duty_cycle)
0499         dev_warn(chip->dev,
0500              ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
0501              state->duty_cycle, state->period,
0502              s2.duty_cycle, s2.period,
0503              last->duty_cycle, last->period);
0504 
0505     if (state->enabled && state->duty_cycle < s2.duty_cycle)
0506         dev_warn(chip->dev,
0507              ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
0508              state->duty_cycle, state->period,
0509              s2.duty_cycle, s2.period);
0510 
0511     if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
0512         dev_warn(chip->dev,
0513              "requested disabled, but yielded enabled with duty > 0\n");
0514 
0515     /* reapply the state that the driver reported being configured. */
0516     err = chip->ops->apply(chip, pwm, &s1);
0517     if (err) {
0518         *last = s1;
0519         dev_err(chip->dev, "failed to reapply current setting\n");
0520         return;
0521     }
0522 
0523     trace_pwm_apply(pwm, &s1);
0524 
0525     chip->ops->get_state(chip, pwm, last);
0526     trace_pwm_get(pwm, last);
0527 
0528     /* reapplication of the current state should give an exact match */
0529     if (s1.enabled != last->enabled ||
0530         s1.polarity != last->polarity ||
0531         (s1.enabled && s1.period != last->period) ||
0532         (s1.enabled && s1.duty_cycle != last->duty_cycle)) {
0533         dev_err(chip->dev,
0534             ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
0535             s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
0536             last->enabled, last->polarity, last->duty_cycle,
0537             last->period);
0538     }
0539 }
0540 
0541 /**
0542  * pwm_apply_state() - atomically apply a new state to a PWM device
0543  * @pwm: PWM device
0544  * @state: new state to apply
0545  */
0546 int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
0547 {
0548     struct pwm_chip *chip;
0549     int err;
0550 
0551     /*
0552      * Some lowlevel driver's implementations of .apply() make use of
0553      * mutexes, also with some drivers only returning when the new
0554      * configuration is active calling pwm_apply_state() from atomic context
0555      * is a bad idea. So make it explicit that calling this function might
0556      * sleep.
0557      */
0558     might_sleep();
0559 
0560     if (!pwm || !state || !state->period ||
0561         state->duty_cycle > state->period)
0562         return -EINVAL;
0563 
0564     chip = pwm->chip;
0565 
0566     if (state->period == pwm->state.period &&
0567         state->duty_cycle == pwm->state.duty_cycle &&
0568         state->polarity == pwm->state.polarity &&
0569         state->enabled == pwm->state.enabled &&
0570         state->usage_power == pwm->state.usage_power)
0571         return 0;
0572 
0573     err = chip->ops->apply(chip, pwm, state);
0574     if (err)
0575         return err;
0576 
0577     trace_pwm_apply(pwm, state);
0578 
0579     pwm->state = *state;
0580 
0581     /*
0582      * only do this after pwm->state was applied as some
0583      * implementations of .get_state depend on this
0584      */
0585     pwm_apply_state_debug(pwm, state);
0586 
0587     return 0;
0588 }
0589 EXPORT_SYMBOL_GPL(pwm_apply_state);
0590 
0591 /**
0592  * pwm_capture() - capture and report a PWM signal
0593  * @pwm: PWM device
0594  * @result: structure to fill with capture result
0595  * @timeout: time to wait, in milliseconds, before giving up on capture
0596  *
0597  * Returns: 0 on success or a negative error code on failure.
0598  */
0599 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
0600         unsigned long timeout)
0601 {
0602     int err;
0603 
0604     if (!pwm || !pwm->chip->ops)
0605         return -EINVAL;
0606 
0607     if (!pwm->chip->ops->capture)
0608         return -ENOSYS;
0609 
0610     mutex_lock(&pwm_lock);
0611     err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
0612     mutex_unlock(&pwm_lock);
0613 
0614     return err;
0615 }
0616 EXPORT_SYMBOL_GPL(pwm_capture);
0617 
0618 /**
0619  * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
0620  * @pwm: PWM device
0621  *
0622  * This function will adjust the PWM config to the PWM arguments provided
0623  * by the DT or PWM lookup table. This is particularly useful to adapt
0624  * the bootloader config to the Linux one.
0625  */
0626 int pwm_adjust_config(struct pwm_device *pwm)
0627 {
0628     struct pwm_state state;
0629     struct pwm_args pargs;
0630 
0631     pwm_get_args(pwm, &pargs);
0632     pwm_get_state(pwm, &state);
0633 
0634     /*
0635      * If the current period is zero it means that either the PWM driver
0636      * does not support initial state retrieval or the PWM has not yet
0637      * been configured.
0638      *
0639      * In either case, we setup the new period and polarity, and assign a
0640      * duty cycle of 0.
0641      */
0642     if (!state.period) {
0643         state.duty_cycle = 0;
0644         state.period = pargs.period;
0645         state.polarity = pargs.polarity;
0646 
0647         return pwm_apply_state(pwm, &state);
0648     }
0649 
0650     /*
0651      * Adjust the PWM duty cycle/period based on the period value provided
0652      * in PWM args.
0653      */
0654     if (pargs.period != state.period) {
0655         u64 dutycycle = (u64)state.duty_cycle * pargs.period;
0656 
0657         do_div(dutycycle, state.period);
0658         state.duty_cycle = dutycycle;
0659         state.period = pargs.period;
0660     }
0661 
0662     /*
0663      * If the polarity changed, we should also change the duty cycle.
0664      */
0665     if (pargs.polarity != state.polarity) {
0666         state.polarity = pargs.polarity;
0667         state.duty_cycle = state.period - state.duty_cycle;
0668     }
0669 
0670     return pwm_apply_state(pwm, &state);
0671 }
0672 EXPORT_SYMBOL_GPL(pwm_adjust_config);
0673 
0674 static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
0675 {
0676     struct pwm_chip *chip;
0677 
0678     mutex_lock(&pwm_lock);
0679 
0680     list_for_each_entry(chip, &pwm_chips, list)
0681         if (chip->dev && dev_fwnode(chip->dev) == fwnode) {
0682             mutex_unlock(&pwm_lock);
0683             return chip;
0684         }
0685 
0686     mutex_unlock(&pwm_lock);
0687 
0688     return ERR_PTR(-EPROBE_DEFER);
0689 }
0690 
0691 static struct device_link *pwm_device_link_add(struct device *dev,
0692                            struct pwm_device *pwm)
0693 {
0694     struct device_link *dl;
0695 
0696     if (!dev) {
0697         /*
0698          * No device for the PWM consumer has been provided. It may
0699          * impact the PM sequence ordering: the PWM supplier may get
0700          * suspended before the consumer.
0701          */
0702         dev_warn(pwm->chip->dev,
0703              "No consumer device specified to create a link to\n");
0704         return NULL;
0705     }
0706 
0707     dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
0708     if (!dl) {
0709         dev_err(dev, "failed to create device link to %s\n",
0710             dev_name(pwm->chip->dev));
0711         return ERR_PTR(-EINVAL);
0712     }
0713 
0714     return dl;
0715 }
0716 
0717 /**
0718  * of_pwm_get() - request a PWM via the PWM framework
0719  * @dev: device for PWM consumer
0720  * @np: device node to get the PWM from
0721  * @con_id: consumer name
0722  *
0723  * Returns the PWM device parsed from the phandle and index specified in the
0724  * "pwms" property of a device tree node or a negative error-code on failure.
0725  * Values parsed from the device tree are stored in the returned PWM device
0726  * object.
0727  *
0728  * If con_id is NULL, the first PWM device listed in the "pwms" property will
0729  * be requested. Otherwise the "pwm-names" property is used to do a reverse
0730  * lookup of the PWM index. This also means that the "pwm-names" property
0731  * becomes mandatory for devices that look up the PWM device via the con_id
0732  * parameter.
0733  *
0734  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
0735  * error code on failure.
0736  */
0737 struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
0738                   const char *con_id)
0739 {
0740     struct pwm_device *pwm = NULL;
0741     struct of_phandle_args args;
0742     struct device_link *dl;
0743     struct pwm_chip *pc;
0744     int index = 0;
0745     int err;
0746 
0747     if (con_id) {
0748         index = of_property_match_string(np, "pwm-names", con_id);
0749         if (index < 0)
0750             return ERR_PTR(index);
0751     }
0752 
0753     err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
0754                      &args);
0755     if (err) {
0756         pr_err("%s(): can't parse \"pwms\" property\n", __func__);
0757         return ERR_PTR(err);
0758     }
0759 
0760     pc = fwnode_to_pwmchip(of_fwnode_handle(args.np));
0761     if (IS_ERR(pc)) {
0762         if (PTR_ERR(pc) != -EPROBE_DEFER)
0763             pr_err("%s(): PWM chip not found\n", __func__);
0764 
0765         pwm = ERR_CAST(pc);
0766         goto put;
0767     }
0768 
0769     pwm = pc->of_xlate(pc, &args);
0770     if (IS_ERR(pwm))
0771         goto put;
0772 
0773     dl = pwm_device_link_add(dev, pwm);
0774     if (IS_ERR(dl)) {
0775         /* of_xlate ended up calling pwm_request_from_chip() */
0776         pwm_free(pwm);
0777         pwm = ERR_CAST(dl);
0778         goto put;
0779     }
0780 
0781     /*
0782      * If a consumer name was not given, try to look it up from the
0783      * "pwm-names" property if it exists. Otherwise use the name of
0784      * the user device node.
0785      */
0786     if (!con_id) {
0787         err = of_property_read_string_index(np, "pwm-names", index,
0788                             &con_id);
0789         if (err < 0)
0790             con_id = np->name;
0791     }
0792 
0793     pwm->label = con_id;
0794 
0795 put:
0796     of_node_put(args.np);
0797 
0798     return pwm;
0799 }
0800 EXPORT_SYMBOL_GPL(of_pwm_get);
0801 
0802 /**
0803  * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
0804  * @fwnode: firmware node to get the "pwms" property from
0805  *
0806  * Returns the PWM device parsed from the fwnode and index specified in the
0807  * "pwms" property or a negative error-code on failure.
0808  * Values parsed from the device tree are stored in the returned PWM device
0809  * object.
0810  *
0811  * This is analogous to of_pwm_get() except con_id is not yet supported.
0812  * ACPI entries must look like
0813  * Package () {"pwms", Package ()
0814  *     { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
0815  *
0816  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
0817  * error code on failure.
0818  */
0819 static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode)
0820 {
0821     struct pwm_device *pwm;
0822     struct fwnode_reference_args args;
0823     struct pwm_chip *chip;
0824     int ret;
0825 
0826     memset(&args, 0, sizeof(args));
0827 
0828     ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
0829     if (ret < 0)
0830         return ERR_PTR(ret);
0831 
0832     if (args.nargs < 2)
0833         return ERR_PTR(-EPROTO);
0834 
0835     chip = fwnode_to_pwmchip(args.fwnode);
0836     if (IS_ERR(chip))
0837         return ERR_CAST(chip);
0838 
0839     pwm = pwm_request_from_chip(chip, args.args[0], NULL);
0840     if (IS_ERR(pwm))
0841         return pwm;
0842 
0843     pwm->args.period = args.args[1];
0844     pwm->args.polarity = PWM_POLARITY_NORMAL;
0845 
0846     if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
0847         pwm->args.polarity = PWM_POLARITY_INVERSED;
0848 
0849     return pwm;
0850 }
0851 
0852 /**
0853  * pwm_add_table() - register PWM device consumers
0854  * @table: array of consumers to register
0855  * @num: number of consumers in table
0856  */
0857 void pwm_add_table(struct pwm_lookup *table, size_t num)
0858 {
0859     mutex_lock(&pwm_lookup_lock);
0860 
0861     while (num--) {
0862         list_add_tail(&table->list, &pwm_lookup_list);
0863         table++;
0864     }
0865 
0866     mutex_unlock(&pwm_lookup_lock);
0867 }
0868 
0869 /**
0870  * pwm_remove_table() - unregister PWM device consumers
0871  * @table: array of consumers to unregister
0872  * @num: number of consumers in table
0873  */
0874 void pwm_remove_table(struct pwm_lookup *table, size_t num)
0875 {
0876     mutex_lock(&pwm_lookup_lock);
0877 
0878     while (num--) {
0879         list_del(&table->list);
0880         table++;
0881     }
0882 
0883     mutex_unlock(&pwm_lookup_lock);
0884 }
0885 
0886 /**
0887  * pwm_get() - look up and request a PWM device
0888  * @dev: device for PWM consumer
0889  * @con_id: consumer name
0890  *
0891  * Lookup is first attempted using DT. If the device was not instantiated from
0892  * a device tree, a PWM chip and a relative index is looked up via a table
0893  * supplied by board setup code (see pwm_add_table()).
0894  *
0895  * Once a PWM chip has been found the specified PWM device will be requested
0896  * and is ready to be used.
0897  *
0898  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
0899  * error code on failure.
0900  */
0901 struct pwm_device *pwm_get(struct device *dev, const char *con_id)
0902 {
0903     const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
0904     const char *dev_id = dev ? dev_name(dev) : NULL;
0905     struct pwm_device *pwm;
0906     struct pwm_chip *chip;
0907     struct device_link *dl;
0908     unsigned int best = 0;
0909     struct pwm_lookup *p, *chosen = NULL;
0910     unsigned int match;
0911     int err;
0912 
0913     /* look up via DT first */
0914     if (is_of_node(fwnode))
0915         return of_pwm_get(dev, to_of_node(fwnode), con_id);
0916 
0917     /* then lookup via ACPI */
0918     if (is_acpi_node(fwnode)) {
0919         pwm = acpi_pwm_get(fwnode);
0920         if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
0921             return pwm;
0922     }
0923 
0924     /*
0925      * We look up the provider in the static table typically provided by
0926      * board setup code. We first try to lookup the consumer device by
0927      * name. If the consumer device was passed in as NULL or if no match
0928      * was found, we try to find the consumer by directly looking it up
0929      * by name.
0930      *
0931      * If a match is found, the provider PWM chip is looked up by name
0932      * and a PWM device is requested using the PWM device per-chip index.
0933      *
0934      * The lookup algorithm was shamelessly taken from the clock
0935      * framework:
0936      *
0937      * We do slightly fuzzy matching here:
0938      *  An entry with a NULL ID is assumed to be a wildcard.
0939      *  If an entry has a device ID, it must match
0940      *  If an entry has a connection ID, it must match
0941      * Then we take the most specific entry - with the following order
0942      * of precedence: dev+con > dev only > con only.
0943      */
0944     mutex_lock(&pwm_lookup_lock);
0945 
0946     list_for_each_entry(p, &pwm_lookup_list, list) {
0947         match = 0;
0948 
0949         if (p->dev_id) {
0950             if (!dev_id || strcmp(p->dev_id, dev_id))
0951                 continue;
0952 
0953             match += 2;
0954         }
0955 
0956         if (p->con_id) {
0957             if (!con_id || strcmp(p->con_id, con_id))
0958                 continue;
0959 
0960             match += 1;
0961         }
0962 
0963         if (match > best) {
0964             chosen = p;
0965 
0966             if (match != 3)
0967                 best = match;
0968             else
0969                 break;
0970         }
0971     }
0972 
0973     mutex_unlock(&pwm_lookup_lock);
0974 
0975     if (!chosen)
0976         return ERR_PTR(-ENODEV);
0977 
0978     chip = pwmchip_find_by_name(chosen->provider);
0979 
0980     /*
0981      * If the lookup entry specifies a module, load the module and retry
0982      * the PWM chip lookup. This can be used to work around driver load
0983      * ordering issues if driver's can't be made to properly support the
0984      * deferred probe mechanism.
0985      */
0986     if (!chip && chosen->module) {
0987         err = request_module(chosen->module);
0988         if (err == 0)
0989             chip = pwmchip_find_by_name(chosen->provider);
0990     }
0991 
0992     if (!chip)
0993         return ERR_PTR(-EPROBE_DEFER);
0994 
0995     pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
0996     if (IS_ERR(pwm))
0997         return pwm;
0998 
0999     dl = pwm_device_link_add(dev, pwm);
1000     if (IS_ERR(dl)) {
1001         pwm_free(pwm);
1002         return ERR_CAST(dl);
1003     }
1004 
1005     pwm->args.period = chosen->period;
1006     pwm->args.polarity = chosen->polarity;
1007 
1008     return pwm;
1009 }
1010 EXPORT_SYMBOL_GPL(pwm_get);
1011 
1012 /**
1013  * pwm_put() - release a PWM device
1014  * @pwm: PWM device
1015  */
1016 void pwm_put(struct pwm_device *pwm)
1017 {
1018     if (!pwm)
1019         return;
1020 
1021     mutex_lock(&pwm_lock);
1022 
1023     if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
1024         pr_warn("PWM device already freed\n");
1025         goto out;
1026     }
1027 
1028     if (pwm->chip->ops->free)
1029         pwm->chip->ops->free(pwm->chip, pwm);
1030 
1031     pwm_set_chip_data(pwm, NULL);
1032     pwm->label = NULL;
1033 
1034     module_put(pwm->chip->ops->owner);
1035 out:
1036     mutex_unlock(&pwm_lock);
1037 }
1038 EXPORT_SYMBOL_GPL(pwm_put);
1039 
1040 static void devm_pwm_release(void *pwm)
1041 {
1042     pwm_put(pwm);
1043 }
1044 
1045 /**
1046  * devm_pwm_get() - resource managed pwm_get()
1047  * @dev: device for PWM consumer
1048  * @con_id: consumer name
1049  *
1050  * This function performs like pwm_get() but the acquired PWM device will
1051  * automatically be released on driver detach.
1052  *
1053  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1054  * error code on failure.
1055  */
1056 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
1057 {
1058     struct pwm_device *pwm;
1059     int ret;
1060 
1061     pwm = pwm_get(dev, con_id);
1062     if (IS_ERR(pwm))
1063         return pwm;
1064 
1065     ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1066     if (ret)
1067         return ERR_PTR(ret);
1068 
1069     return pwm;
1070 }
1071 EXPORT_SYMBOL_GPL(devm_pwm_get);
1072 
1073 /**
1074  * devm_of_pwm_get() - resource managed of_pwm_get()
1075  * @dev: device for PWM consumer
1076  * @np: device node to get the PWM from
1077  * @con_id: consumer name
1078  *
1079  * This function performs like of_pwm_get() but the acquired PWM device will
1080  * automatically be released on driver detach.
1081  *
1082  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1083  * error code on failure.
1084  */
1085 struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
1086                    const char *con_id)
1087 {
1088     struct pwm_device *pwm;
1089     int ret;
1090 
1091     pwm = of_pwm_get(dev, np, con_id);
1092     if (IS_ERR(pwm))
1093         return pwm;
1094 
1095     ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1096     if (ret)
1097         return ERR_PTR(ret);
1098 
1099     return pwm;
1100 }
1101 EXPORT_SYMBOL_GPL(devm_of_pwm_get);
1102 
1103 /**
1104  * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1105  * @dev: device for PWM consumer
1106  * @fwnode: firmware node to get the PWM from
1107  * @con_id: consumer name
1108  *
1109  * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1110  * acpi_pwm_get() for a detailed description.
1111  *
1112  * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1113  * error code on failure.
1114  */
1115 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
1116                        struct fwnode_handle *fwnode,
1117                        const char *con_id)
1118 {
1119     struct pwm_device *pwm = ERR_PTR(-ENODEV);
1120     int ret;
1121 
1122     if (is_of_node(fwnode))
1123         pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
1124     else if (is_acpi_node(fwnode))
1125         pwm = acpi_pwm_get(fwnode);
1126     if (IS_ERR(pwm))
1127         return pwm;
1128 
1129     ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1130     if (ret)
1131         return ERR_PTR(ret);
1132 
1133     return pwm;
1134 }
1135 EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
1136 
1137 #ifdef CONFIG_DEBUG_FS
1138 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
1139 {
1140     unsigned int i;
1141 
1142     for (i = 0; i < chip->npwm; i++) {
1143         struct pwm_device *pwm = &chip->pwms[i];
1144         struct pwm_state state;
1145 
1146         pwm_get_state(pwm, &state);
1147 
1148         seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
1149 
1150         if (test_bit(PWMF_REQUESTED, &pwm->flags))
1151             seq_puts(s, " requested");
1152 
1153         if (state.enabled)
1154             seq_puts(s, " enabled");
1155 
1156         seq_printf(s, " period: %llu ns", state.period);
1157         seq_printf(s, " duty: %llu ns", state.duty_cycle);
1158         seq_printf(s, " polarity: %s",
1159                state.polarity ? "inverse" : "normal");
1160 
1161         if (state.usage_power)
1162             seq_puts(s, " usage_power");
1163 
1164         seq_puts(s, "\n");
1165     }
1166 }
1167 
1168 static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
1169 {
1170     mutex_lock(&pwm_lock);
1171     s->private = "";
1172 
1173     return seq_list_start(&pwm_chips, *pos);
1174 }
1175 
1176 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
1177 {
1178     s->private = "\n";
1179 
1180     return seq_list_next(v, &pwm_chips, pos);
1181 }
1182 
1183 static void pwm_seq_stop(struct seq_file *s, void *v)
1184 {
1185     mutex_unlock(&pwm_lock);
1186 }
1187 
1188 static int pwm_seq_show(struct seq_file *s, void *v)
1189 {
1190     struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
1191 
1192     seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
1193            chip->dev->bus ? chip->dev->bus->name : "no-bus",
1194            dev_name(chip->dev), chip->npwm,
1195            (chip->npwm != 1) ? "s" : "");
1196 
1197     pwm_dbg_show(chip, s);
1198 
1199     return 0;
1200 }
1201 
1202 static const struct seq_operations pwm_debugfs_sops = {
1203     .start = pwm_seq_start,
1204     .next = pwm_seq_next,
1205     .stop = pwm_seq_stop,
1206     .show = pwm_seq_show,
1207 };
1208 
1209 DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
1210 
1211 static int __init pwm_debugfs_init(void)
1212 {
1213     debugfs_create_file("pwm", S_IFREG | 0444, NULL, NULL,
1214                 &pwm_debugfs_fops);
1215 
1216     return 0;
1217 }
1218 subsys_initcall(pwm_debugfs_init);
1219 #endif /* CONFIG_DEBUG_FS */