Lines Matching +full:on +full:- +full:chip

1 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Copyright (C) 2011-2012 Avionic Design GmbH
12 #include <linux/radix-tree.h>
21 #include <dt-bindings/pwm/pwm.h>
48 return -ENOSPC; in alloc_pwms()
53 static void free_pwms(struct pwm_chip *chip) in free_pwms() argument
57 for (i = 0; i < chip->npwm; i++) { in free_pwms()
58 struct pwm_device *pwm = &chip->pwms[i]; in free_pwms()
60 radix_tree_delete(&pwm_tree, pwm->pwm); in free_pwms()
63 bitmap_clear(allocated_pwms, chip->base, chip->npwm); in free_pwms()
65 kfree(chip->pwms); in free_pwms()
66 chip->pwms = NULL; in free_pwms()
71 struct pwm_chip *chip; in pwmchip_find_by_name() local
78 list_for_each_entry(chip, &pwm_chips, list) { in pwmchip_find_by_name()
79 const char *chip_name = dev_name(chip->dev); in pwmchip_find_by_name()
83 return chip; in pwmchip_find_by_name()
96 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_device_request()
97 return -EBUSY; in pwm_device_request()
99 if (!try_module_get(pwm->chip->ops->owner)) in pwm_device_request()
100 return -ENODEV; in pwm_device_request()
102 if (pwm->chip->ops->request) { in pwm_device_request()
103 err = pwm->chip->ops->request(pwm->chip, pwm); in pwm_device_request()
105 module_put(pwm->chip->ops->owner); in pwm_device_request()
110 if (pwm->chip->ops->get_state) { in pwm_device_request()
111 pwm->chip->ops->get_state(pwm->chip, pwm, &pwm->state); in pwm_device_request()
112 trace_pwm_get(pwm, &pwm->state); in pwm_device_request()
115 pwm->last = pwm->state; in pwm_device_request()
118 set_bit(PWMF_REQUESTED, &pwm->flags); in pwm_device_request()
119 pwm->label = label; in pwm_device_request()
129 if (pc->of_pwm_n_cells < 2) in of_pwm_xlate_with_flags()
130 return ERR_PTR(-EINVAL); in of_pwm_xlate_with_flags()
133 if (args->args_count < 2) in of_pwm_xlate_with_flags()
134 return ERR_PTR(-EINVAL); in of_pwm_xlate_with_flags()
136 if (args->args[0] >= pc->npwm) in of_pwm_xlate_with_flags()
137 return ERR_PTR(-EINVAL); in of_pwm_xlate_with_flags()
139 pwm = pwm_request_from_chip(pc, args->args[0], NULL); in of_pwm_xlate_with_flags()
143 pwm->args.period = args->args[1]; in of_pwm_xlate_with_flags()
144 pwm->args.polarity = PWM_POLARITY_NORMAL; in of_pwm_xlate_with_flags()
146 if (pc->of_pwm_n_cells >= 3) { in of_pwm_xlate_with_flags()
147 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) in of_pwm_xlate_with_flags()
148 pwm->args.polarity = PWM_POLARITY_INVERSED; in of_pwm_xlate_with_flags()
155 static void of_pwmchip_add(struct pwm_chip *chip) in of_pwmchip_add() argument
157 if (!chip->dev || !chip->dev->of_node) in of_pwmchip_add()
160 if (!chip->of_xlate) { in of_pwmchip_add()
163 if (of_property_read_u32(chip->dev->of_node, "#pwm-cells", in of_pwmchip_add()
167 chip->of_xlate = of_pwm_xlate_with_flags; in of_pwmchip_add()
168 chip->of_pwm_n_cells = pwm_cells; in of_pwmchip_add()
171 of_node_get(chip->dev->of_node); in of_pwmchip_add()
174 static void of_pwmchip_remove(struct pwm_chip *chip) in of_pwmchip_remove() argument
176 if (chip->dev) in of_pwmchip_remove()
177 of_node_put(chip->dev->of_node); in of_pwmchip_remove()
181 * pwm_set_chip_data() - set private chip data for a PWM
183 * @data: pointer to chip-specific data
185 * Returns: 0 on success or a negative error code on failure.
190 return -EINVAL; in pwm_set_chip_data()
192 pwm->chip_data = data; in pwm_set_chip_data()
199 * pwm_get_chip_data() - get private chip data for a PWM
202 * Returns: A pointer to the chip-private data for the PWM device.
206 return pwm ? pwm->chip_data : NULL; in pwm_get_chip_data()
210 static bool pwm_ops_check(const struct pwm_chip *chip) in pwm_ops_check() argument
213 const struct pwm_ops *ops = chip->ops; in pwm_ops_check()
215 /* driver supports legacy, non-atomic operation */ in pwm_ops_check()
216 if (ops->config && ops->enable && ops->disable) { in pwm_ops_check()
218 dev_warn(chip->dev, in pwm_ops_check()
224 if (!ops->apply) in pwm_ops_check()
227 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) in pwm_ops_check()
228 dev_warn(chip->dev, in pwm_ops_check()
235 * pwmchip_add() - register a new PWM chip
236 * @chip: the PWM chip to add
238 * Register a new PWM chip.
240 * Returns: 0 on success or a negative error code on failure.
242 int pwmchip_add(struct pwm_chip *chip) in pwmchip_add() argument
248 if (!chip || !chip->dev || !chip->ops || !chip->npwm) in pwmchip_add()
249 return -EINVAL; in pwmchip_add()
251 if (!pwm_ops_check(chip)) in pwmchip_add()
252 return -EINVAL; in pwmchip_add()
256 ret = alloc_pwms(chip->npwm); in pwmchip_add()
260 chip->base = ret; in pwmchip_add()
262 chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL); in pwmchip_add()
263 if (!chip->pwms) { in pwmchip_add()
264 ret = -ENOMEM; in pwmchip_add()
268 for (i = 0; i < chip->npwm; i++) { in pwmchip_add()
269 pwm = &chip->pwms[i]; in pwmchip_add()
271 pwm->chip = chip; in pwmchip_add()
272 pwm->pwm = chip->base + i; in pwmchip_add()
273 pwm->hwpwm = i; in pwmchip_add()
275 radix_tree_insert(&pwm_tree, pwm->pwm, pwm); in pwmchip_add()
278 bitmap_set(allocated_pwms, chip->base, chip->npwm); in pwmchip_add()
280 INIT_LIST_HEAD(&chip->list); in pwmchip_add()
281 list_add(&chip->list, &pwm_chips); in pwmchip_add()
286 of_pwmchip_add(chip); in pwmchip_add()
292 pwmchip_sysfs_export(chip); in pwmchip_add()
299 * pwmchip_remove() - remove a PWM chip
300 * @chip: the PWM chip to remove
302 * Removes a PWM chip. This function may return busy if the PWM chip provides
305 * Returns: 0 on success or a negative error code on failure.
307 void pwmchip_remove(struct pwm_chip *chip) in pwmchip_remove() argument
309 pwmchip_sysfs_unexport(chip); in pwmchip_remove()
313 list_del_init(&chip->list); in pwmchip_remove()
316 of_pwmchip_remove(chip); in pwmchip_remove()
318 free_pwms(chip); in pwmchip_remove()
326 struct pwm_chip *chip = data; in devm_pwmchip_remove() local
328 pwmchip_remove(chip); in devm_pwmchip_remove()
331 int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip) in devm_pwmchip_add() argument
335 ret = pwmchip_add(chip); in devm_pwmchip_add()
339 return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip); in devm_pwmchip_add()
344 * pwm_request() - request a PWM device
350 * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on
359 return ERR_PTR(-EINVAL); in pwm_request()
365 dev = ERR_PTR(-EPROBE_DEFER); in pwm_request()
381 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
382 * @chip: PWM chip
383 * @index: per-chip index of the PWM to request
387 * chip. A negative error code is returned if the index is not valid for the
388 * specified PWM chip or if the PWM device cannot be requested.
390 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, in pwm_request_from_chip() argument
397 if (!chip || index >= chip->npwm) in pwm_request_from_chip()
398 return ERR_PTR(-EINVAL); in pwm_request_from_chip()
401 pwm = &chip->pwms[index]; in pwm_request_from_chip()
413 * pwm_free() - free a PWM device
427 struct pwm_state *last = &pwm->last; in pwm_apply_state_debug()
428 struct pwm_chip *chip = pwm->chip; in pwm_apply_state_debug() local
436 if (!chip->ops->get_state) in pwm_apply_state_debug()
444 chip->ops->get_state(chip, pwm, &s1); in pwm_apply_state_debug()
452 if (s1.enabled && s1.polarity != state->polarity) { in pwm_apply_state_debug()
453 s2.polarity = state->polarity; in pwm_apply_state_debug()
454 s2.duty_cycle = s1.period - s1.duty_cycle; in pwm_apply_state_debug()
461 if (s2.polarity != state->polarity && in pwm_apply_state_debug()
462 state->duty_cycle < state->period) in pwm_apply_state_debug()
463 dev_warn(chip->dev, ".apply ignored .polarity\n"); in pwm_apply_state_debug()
465 if (state->enabled && in pwm_apply_state_debug()
466 last->polarity == state->polarity && in pwm_apply_state_debug()
467 last->period > s2.period && in pwm_apply_state_debug()
468 last->period <= state->period) in pwm_apply_state_debug()
469 dev_warn(chip->dev, in pwm_apply_state_debug()
471 state->period, s2.period, last->period); in pwm_apply_state_debug()
473 if (state->enabled && state->period < s2.period) in pwm_apply_state_debug()
474 dev_warn(chip->dev, in pwm_apply_state_debug()
476 state->period, s2.period); in pwm_apply_state_debug()
478 if (state->enabled && in pwm_apply_state_debug()
479 last->polarity == state->polarity && in pwm_apply_state_debug()
480 last->period == s2.period && in pwm_apply_state_debug()
481 last->duty_cycle > s2.duty_cycle && in pwm_apply_state_debug()
482 last->duty_cycle <= state->duty_cycle) in pwm_apply_state_debug()
483 dev_warn(chip->dev, in pwm_apply_state_debug()
485 state->duty_cycle, state->period, in pwm_apply_state_debug()
487 last->duty_cycle, last->period); in pwm_apply_state_debug()
489 if (state->enabled && state->duty_cycle < s2.duty_cycle) in pwm_apply_state_debug()
490 dev_warn(chip->dev, in pwm_apply_state_debug()
492 state->duty_cycle, state->period, in pwm_apply_state_debug()
495 if (!state->enabled && s2.enabled && s2.duty_cycle > 0) in pwm_apply_state_debug()
496 dev_warn(chip->dev, in pwm_apply_state_debug()
500 err = chip->ops->apply(chip, pwm, &s1); in pwm_apply_state_debug()
503 dev_err(chip->dev, "failed to reapply current setting\n"); in pwm_apply_state_debug()
509 chip->ops->get_state(chip, pwm, last); in pwm_apply_state_debug()
513 if (s1.enabled != last->enabled || in pwm_apply_state_debug()
514 s1.polarity != last->polarity || in pwm_apply_state_debug()
515 (s1.enabled && s1.period != last->period) || in pwm_apply_state_debug()
516 (s1.enabled && s1.duty_cycle != last->duty_cycle)) { in pwm_apply_state_debug()
517 dev_err(chip->dev, in pwm_apply_state_debug()
518 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n", in pwm_apply_state_debug()
520 last->enabled, last->polarity, last->duty_cycle, in pwm_apply_state_debug()
521 last->period); in pwm_apply_state_debug()
526 * pwm_apply_state() - atomically apply a new state to a PWM device
532 struct pwm_chip *chip; in pwm_apply_state() local
535 if (!pwm || !state || !state->period || in pwm_apply_state()
536 state->duty_cycle > state->period) in pwm_apply_state()
537 return -EINVAL; in pwm_apply_state()
539 chip = pwm->chip; in pwm_apply_state()
541 if (state->period == pwm->state.period && in pwm_apply_state()
542 state->duty_cycle == pwm->state.duty_cycle && in pwm_apply_state()
543 state->polarity == pwm->state.polarity && in pwm_apply_state()
544 state->enabled == pwm->state.enabled && in pwm_apply_state()
545 state->usage_power == pwm->state.usage_power) in pwm_apply_state()
548 if (chip->ops->apply) { in pwm_apply_state()
549 err = chip->ops->apply(chip, pwm, state); in pwm_apply_state()
555 pwm->state = *state; in pwm_apply_state()
558 * only do this after pwm->state was applied as some in pwm_apply_state()
559 * implementations of .get_state depend on this in pwm_apply_state()
566 if (state->polarity != pwm->state.polarity) { in pwm_apply_state()
567 if (!chip->ops->set_polarity) in pwm_apply_state()
568 return -EINVAL; in pwm_apply_state()
573 * ->apply(). in pwm_apply_state()
575 if (pwm->state.enabled) { in pwm_apply_state()
576 chip->ops->disable(chip, pwm); in pwm_apply_state()
577 pwm->state.enabled = false; in pwm_apply_state()
580 err = chip->ops->set_polarity(chip, pwm, in pwm_apply_state()
581 state->polarity); in pwm_apply_state()
585 pwm->state.polarity = state->polarity; in pwm_apply_state()
588 if (state->period != pwm->state.period || in pwm_apply_state()
589 state->duty_cycle != pwm->state.duty_cycle) { in pwm_apply_state()
590 err = chip->ops->config(pwm->chip, pwm, in pwm_apply_state()
591 state->duty_cycle, in pwm_apply_state()
592 state->period); in pwm_apply_state()
596 pwm->state.duty_cycle = state->duty_cycle; in pwm_apply_state()
597 pwm->state.period = state->period; in pwm_apply_state()
600 if (state->enabled != pwm->state.enabled) { in pwm_apply_state()
601 if (state->enabled) { in pwm_apply_state()
602 err = chip->ops->enable(chip, pwm); in pwm_apply_state()
606 chip->ops->disable(chip, pwm); in pwm_apply_state()
609 pwm->state.enabled = state->enabled; in pwm_apply_state()
618 * pwm_capture() - capture and report a PWM signal
621 * @timeout: time to wait, in milliseconds, before giving up on capture
623 * Returns: 0 on success or a negative error code on failure.
630 if (!pwm || !pwm->chip->ops) in pwm_capture()
631 return -EINVAL; in pwm_capture()
633 if (!pwm->chip->ops->capture) in pwm_capture()
634 return -ENOSYS; in pwm_capture()
637 err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout); in pwm_capture()
645 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
677 * Adjust the PWM duty cycle/period based on the period value provided in pwm_adjust_config()
693 state.duty_cycle = state.period - state.duty_cycle; in pwm_adjust_config()
702 struct pwm_chip *chip; in fwnode_to_pwmchip() local
706 list_for_each_entry(chip, &pwm_chips, list) in fwnode_to_pwmchip()
707 if (chip->dev && dev_fwnode(chip->dev) == fwnode) { in fwnode_to_pwmchip()
709 return chip; in fwnode_to_pwmchip()
714 return ERR_PTR(-EPROBE_DEFER); in fwnode_to_pwmchip()
728 dev_warn(pwm->chip->dev, in pwm_device_link_add()
733 dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER); in pwm_device_link_add()
736 dev_name(pwm->chip->dev)); in pwm_device_link_add()
737 return ERR_PTR(-EINVAL); in pwm_device_link_add()
744 * of_pwm_get() - request a PWM via the PWM framework
750 * "pwms" property of a device tree node or a negative error-code on failure.
755 * be requested. Otherwise the "pwm-names" property is used to do a reverse
756 * lookup of the PWM index. This also means that the "pwm-names" property
760 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
761 * error code on failure.
774 index = of_property_match_string(np, "pwm-names", con_id); in of_pwm_get()
779 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, in of_pwm_get()
788 if (PTR_ERR(pc) != -EPROBE_DEFER) in of_pwm_get()
789 pr_err("%s(): PWM chip not found\n", __func__); in of_pwm_get()
795 pwm = pc->of_xlate(pc, &args); in of_pwm_get()
809 * "pwm-names" property if it exists. Otherwise use the name of in of_pwm_get()
813 err = of_property_read_string_index(np, "pwm-names", index, in of_pwm_get()
816 con_id = np->name; in of_pwm_get()
819 pwm->label = con_id; in of_pwm_get()
829 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
833 * "pwms" property or a negative error-code on failure.
842 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
843 * error code on failure.
849 struct pwm_chip *chip; in acpi_pwm_get() local
859 return ERR_PTR(-EPROTO); in acpi_pwm_get()
861 chip = fwnode_to_pwmchip(args.fwnode); in acpi_pwm_get()
862 if (IS_ERR(chip)) in acpi_pwm_get()
863 return ERR_CAST(chip); in acpi_pwm_get()
865 pwm = pwm_request_from_chip(chip, args.args[0], NULL); in acpi_pwm_get()
869 pwm->args.period = args.args[1]; in acpi_pwm_get()
870 pwm->args.polarity = PWM_POLARITY_NORMAL; in acpi_pwm_get()
873 pwm->args.polarity = PWM_POLARITY_INVERSED; in acpi_pwm_get()
879 * pwm_add_table() - register PWM device consumers
887 while (num--) { in pwm_add_table()
888 list_add_tail(&table->list, &pwm_lookup_list); in pwm_add_table()
896 * pwm_remove_table() - unregister PWM device consumers
904 while (num--) { in pwm_remove_table()
905 list_del(&table->list); in pwm_remove_table()
913 * pwm_get() - look up and request a PWM device
918 * a device tree, a PWM chip and a relative index is looked up via a table
921 * Once a PWM chip has been found the specified PWM device will be requested
924 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
925 * error code on failure.
932 struct pwm_chip *chip; in pwm_get() local
946 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT) in pwm_get()
957 * If a match is found, the provider PWM chip is looked up by name in pwm_get()
958 * and a PWM device is requested using the PWM device per-chip index. in pwm_get()
967 * Then we take the most specific entry - with the following order in pwm_get()
975 if (p->dev_id) { in pwm_get()
976 if (!dev_id || strcmp(p->dev_id, dev_id)) in pwm_get()
982 if (p->con_id) { in pwm_get()
983 if (!con_id || strcmp(p->con_id, con_id)) in pwm_get()
1002 return ERR_PTR(-ENODEV); in pwm_get()
1004 chip = pwmchip_find_by_name(chosen->provider); in pwm_get()
1008 * the PWM chip lookup. This can be used to work around driver load in pwm_get()
1012 if (!chip && chosen->module) { in pwm_get()
1013 err = request_module(chosen->module); in pwm_get()
1015 chip = pwmchip_find_by_name(chosen->provider); in pwm_get()
1018 if (!chip) in pwm_get()
1019 return ERR_PTR(-EPROBE_DEFER); in pwm_get()
1021 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); in pwm_get()
1031 pwm->args.period = chosen->period; in pwm_get()
1032 pwm->args.polarity = chosen->polarity; in pwm_get()
1039 * pwm_put() - release a PWM device
1049 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { in pwm_put()
1054 if (pwm->chip->ops->free) in pwm_put()
1055 pwm->chip->ops->free(pwm->chip, pwm); in pwm_put()
1058 pwm->label = NULL; in pwm_put()
1060 module_put(pwm->chip->ops->owner); in pwm_put()
1072 * devm_pwm_get() - resource managed pwm_get()
1077 * automatically be released on driver detach.
1079 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1080 * error code on failure.
1100 * devm_of_pwm_get() - resource managed of_pwm_get()
1106 * automatically be released on driver detach.
1108 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1109 * error code on failure.
1130 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1138 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1139 * error code on failure.
1145 struct pwm_device *pwm = ERR_PTR(-ENODEV); in devm_fwnode_pwm_get()
1164 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s) in pwm_dbg_show() argument
1168 for (i = 0; i < chip->npwm; i++) { in pwm_dbg_show()
1169 struct pwm_device *pwm = &chip->pwms[i]; in pwm_dbg_show()
1174 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); in pwm_dbg_show()
1176 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_dbg_show()
1197 s->private = ""; in pwm_seq_start()
1204 s->private = "\n"; in pwm_seq_next()
1216 struct pwm_chip *chip = list_entry(v, struct pwm_chip, list); in pwm_seq_show() local
1218 seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private, in pwm_seq_show()
1219 chip->dev->bus ? chip->dev->bus->name : "no-bus", in pwm_seq_show()
1220 dev_name(chip->dev), chip->npwm, in pwm_seq_show()
1221 (chip->npwm != 1) ? "s" : ""); in pwm_seq_show()
1223 pwm_dbg_show(chip, s); in pwm_seq_show()