Commit | Line | Data |
---|---|---|
c82ee6d3 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
0c2498f1 SH |
2 | /* |
3 | * Generic pwmlib implementation | |
4 | * | |
5 | * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de> | |
f051c466 | 6 | * Copyright (C) 2011-2012 Avionic Design GmbH |
0c2498f1 SH |
7 | */ |
8 | ||
4a6ef8e3 | 9 | #include <linux/acpi.h> |
0c2498f1 SH |
10 | #include <linux/module.h> |
11 | #include <linux/pwm.h> | |
f051c466 | 12 | #include <linux/radix-tree.h> |
0c2498f1 SH |
13 | #include <linux/list.h> |
14 | #include <linux/mutex.h> | |
15 | #include <linux/err.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/device.h> | |
62099abf TR |
18 | #include <linux/debugfs.h> |
19 | #include <linux/seq_file.h> | |
0c2498f1 | 20 | |
208be769 | 21 | #include <dt-bindings/pwm/pwm.h> |
0c2498f1 | 22 | |
1188829a UKK |
23 | #define CREATE_TRACE_POINTS |
24 | #include <trace/events/pwm.h> | |
25 | ||
208be769 | 26 | #define MAX_PWMS 1024 |
83af2402 | 27 | |
8138d2dd TR |
28 | static DEFINE_MUTEX(pwm_lookup_lock); |
29 | static LIST_HEAD(pwm_lookup_list); | |
e51b156b UKK |
30 | |
31 | /* protects access to pwm_chips, allocated_pwms, and pwm_tree */ | |
0c2498f1 | 32 | static DEFINE_MUTEX(pwm_lock); |
e51b156b | 33 | |
f051c466 TR |
34 | static LIST_HEAD(pwm_chips); |
35 | static DECLARE_BITMAP(allocated_pwms, MAX_PWMS); | |
36 | static RADIX_TREE(pwm_tree, GFP_KERNEL); | |
0c2498f1 | 37 | |
f051c466 | 38 | static struct pwm_device *pwm_to_device(unsigned int pwm) |
0c2498f1 | 39 | { |
f051c466 TR |
40 | return radix_tree_lookup(&pwm_tree, pwm); |
41 | } | |
42 | ||
e51b156b | 43 | /* Called with pwm_lock held */ |
f9a8ee8c | 44 | static int alloc_pwms(unsigned int count) |
f051c466 | 45 | { |
f051c466 TR |
46 | unsigned int start; |
47 | ||
f9a8ee8c | 48 | start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, 0, |
f051c466 TR |
49 | count, 0); |
50 | ||
f051c466 TR |
51 | if (start + count > MAX_PWMS) |
52 | return -ENOSPC; | |
53 | ||
54 | return start; | |
55 | } | |
56 | ||
e51b156b | 57 | /* Called with pwm_lock held */ |
f051c466 TR |
58 | static void free_pwms(struct pwm_chip *chip) |
59 | { | |
60 | unsigned int i; | |
61 | ||
62 | for (i = 0; i < chip->npwm; i++) { | |
63 | struct pwm_device *pwm = &chip->pwms[i]; | |
83a98864 | 64 | |
f051c466 | 65 | radix_tree_delete(&pwm_tree, pwm->pwm); |
0c2498f1 SH |
66 | } |
67 | ||
f051c466 TR |
68 | bitmap_clear(allocated_pwms, chip->base, chip->npwm); |
69 | ||
70 | kfree(chip->pwms); | |
71 | chip->pwms = NULL; | |
72 | } | |
73 | ||
8138d2dd TR |
74 | static struct pwm_chip *pwmchip_find_by_name(const char *name) |
75 | { | |
76 | struct pwm_chip *chip; | |
77 | ||
78 | if (!name) | |
79 | return NULL; | |
80 | ||
81 | mutex_lock(&pwm_lock); | |
82 | ||
83 | list_for_each_entry(chip, &pwm_chips, list) { | |
84 | const char *chip_name = dev_name(chip->dev); | |
85 | ||
86 | if (chip_name && strcmp(chip_name, name) == 0) { | |
87 | mutex_unlock(&pwm_lock); | |
88 | return chip; | |
89 | } | |
90 | } | |
91 | ||
92 | mutex_unlock(&pwm_lock); | |
93 | ||
94 | return NULL; | |
95 | } | |
96 | ||
f051c466 TR |
97 | static int pwm_device_request(struct pwm_device *pwm, const char *label) |
98 | { | |
99 | int err; | |
100 | ||
101 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) | |
102 | return -EBUSY; | |
103 | ||
104 | if (!try_module_get(pwm->chip->ops->owner)) | |
105 | return -ENODEV; | |
106 | ||
107 | if (pwm->chip->ops->request) { | |
108 | err = pwm->chip->ops->request(pwm->chip, pwm); | |
109 | if (err) { | |
110 | module_put(pwm->chip->ops->owner); | |
111 | return err; | |
112 | } | |
113 | } | |
114 | ||
1188829a | 115 | if (pwm->chip->ops->get_state) { |
cfc4c189 | 116 | pwm->chip->ops->get_state(pwm->chip, pwm, &pwm->state); |
1188829a | 117 | trace_pwm_get(pwm, &pwm->state); |
3ad1f3a3 | 118 | |
f5641d05 | 119 | if (IS_ENABLED(CONFIG_PWM_DEBUG)) |
3ad1f3a3 | 120 | pwm->last = pwm->state; |
1188829a | 121 | } |
cfc4c189 | 122 | |
f051c466 TR |
123 | set_bit(PWMF_REQUESTED, &pwm->flags); |
124 | pwm->label = label; | |
125 | ||
126 | return 0; | |
127 | } | |
128 | ||
83af2402 PA |
129 | struct pwm_device * |
130 | of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args) | |
131 | { | |
132 | struct pwm_device *pwm; | |
133 | ||
cf38c978 | 134 | if (pc->of_pwm_n_cells < 2) |
83af2402 PA |
135 | return ERR_PTR(-EINVAL); |
136 | ||
42883cbc LW |
137 | /* flags in the third cell are optional */ |
138 | if (args->args_count < 2) | |
139 | return ERR_PTR(-EINVAL); | |
140 | ||
83af2402 PA |
141 | if (args->args[0] >= pc->npwm) |
142 | return ERR_PTR(-EINVAL); | |
143 | ||
144 | pwm = pwm_request_from_chip(pc, args->args[0], NULL); | |
145 | if (IS_ERR(pwm)) | |
146 | return pwm; | |
147 | ||
e39c0df1 | 148 | pwm->args.period = args->args[1]; |
42883cbc | 149 | pwm->args.polarity = PWM_POLARITY_NORMAL; |
83af2402 | 150 | |
cf38c978 UKK |
151 | if (pc->of_pwm_n_cells >= 3) { |
152 | if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) | |
153 | pwm->args.polarity = PWM_POLARITY_INVERSED; | |
154 | } | |
83af2402 PA |
155 | |
156 | return pwm; | |
157 | } | |
417328c3 | 158 | EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags); |
83af2402 | 159 | |
3ab7b6ac BA |
160 | struct pwm_device * |
161 | of_pwm_single_xlate(struct pwm_chip *pc, const struct of_phandle_args *args) | |
162 | { | |
163 | struct pwm_device *pwm; | |
164 | ||
165 | if (pc->of_pwm_n_cells < 1) | |
166 | return ERR_PTR(-EINVAL); | |
167 | ||
168 | /* validate that one cell is specified, optionally with flags */ | |
169 | if (args->args_count != 1 && args->args_count != 2) | |
170 | return ERR_PTR(-EINVAL); | |
171 | ||
172 | pwm = pwm_request_from_chip(pc, 0, NULL); | |
173 | if (IS_ERR(pwm)) | |
174 | return pwm; | |
175 | ||
176 | pwm->args.period = args->args[0]; | |
177 | pwm->args.polarity = PWM_POLARITY_NORMAL; | |
178 | ||
179 | if (args->args_count == 2 && args->args[2] & PWM_POLARITY_INVERTED) | |
180 | pwm->args.polarity = PWM_POLARITY_INVERSED; | |
181 | ||
182 | return pwm; | |
183 | } | |
184 | EXPORT_SYMBOL_GPL(of_pwm_single_xlate); | |
185 | ||
dfeb86ec | 186 | static void of_pwmchip_add(struct pwm_chip *chip) |
7299ab70 TR |
187 | { |
188 | if (!chip->dev || !chip->dev->of_node) | |
189 | return; | |
190 | ||
191 | if (!chip->of_xlate) { | |
69230cfa UKK |
192 | u32 pwm_cells; |
193 | ||
194 | if (of_property_read_u32(chip->dev->of_node, "#pwm-cells", | |
195 | &pwm_cells)) | |
196 | pwm_cells = 2; | |
197 | ||
5447e783 | 198 | chip->of_xlate = of_pwm_xlate_with_flags; |
69230cfa | 199 | chip->of_pwm_n_cells = pwm_cells; |
7299ab70 TR |
200 | } |
201 | ||
202 | of_node_get(chip->dev->of_node); | |
203 | } | |
204 | ||
dfeb86ec | 205 | static void of_pwmchip_remove(struct pwm_chip *chip) |
7299ab70 | 206 | { |
8d6cc073 | 207 | if (chip->dev) |
7299ab70 TR |
208 | of_node_put(chip->dev->of_node); |
209 | } | |
210 | ||
f051c466 TR |
211 | /** |
212 | * pwm_set_chip_data() - set private chip data for a PWM | |
213 | * @pwm: PWM device | |
214 | * @data: pointer to chip-specific data | |
04883802 TR |
215 | * |
216 | * Returns: 0 on success or a negative error code on failure. | |
f051c466 TR |
217 | */ |
218 | int pwm_set_chip_data(struct pwm_device *pwm, void *data) | |
219 | { | |
220 | if (!pwm) | |
221 | return -EINVAL; | |
222 | ||
223 | pwm->chip_data = data; | |
224 | ||
225 | return 0; | |
226 | } | |
928c4477 | 227 | EXPORT_SYMBOL_GPL(pwm_set_chip_data); |
f051c466 TR |
228 | |
229 | /** | |
230 | * pwm_get_chip_data() - get private chip data for a PWM | |
231 | * @pwm: PWM device | |
04883802 TR |
232 | * |
233 | * Returns: A pointer to the chip-private data for the PWM device. | |
f051c466 TR |
234 | */ |
235 | void *pwm_get_chip_data(struct pwm_device *pwm) | |
236 | { | |
237 | return pwm ? pwm->chip_data : NULL; | |
0c2498f1 | 238 | } |
928c4477 | 239 | EXPORT_SYMBOL_GPL(pwm_get_chip_data); |
0c2498f1 | 240 | |
3ad1f3a3 | 241 | static bool pwm_ops_check(const struct pwm_chip *chip) |
5ec803ed | 242 | { |
3ad1f3a3 UKK |
243 | const struct pwm_ops *ops = chip->ops; |
244 | ||
3ad1f3a3 UKK |
245 | if (!ops->apply) |
246 | return false; | |
247 | ||
248 | if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) | |
249 | dev_warn(chip->dev, | |
250 | "Please implement the .get_state() callback\n"); | |
5ec803ed | 251 | |
3ad1f3a3 | 252 | return true; |
5ec803ed BB |
253 | } |
254 | ||
0c2498f1 | 255 | /** |
9666cec3 | 256 | * pwmchip_add() - register a new PWM chip |
0c2498f1 | 257 | * @chip: the PWM chip to add |
f051c466 | 258 | * |
9666cec3 | 259 | * Register a new PWM chip. |
04883802 TR |
260 | * |
261 | * Returns: 0 on success or a negative error code on failure. | |
0c2498f1 | 262 | */ |
9666cec3 | 263 | int pwmchip_add(struct pwm_chip *chip) |
0c2498f1 SH |
264 | { |
265 | struct pwm_device *pwm; | |
f051c466 TR |
266 | unsigned int i; |
267 | int ret; | |
0c2498f1 | 268 | |
5ec803ed BB |
269 | if (!chip || !chip->dev || !chip->ops || !chip->npwm) |
270 | return -EINVAL; | |
271 | ||
3ad1f3a3 | 272 | if (!pwm_ops_check(chip)) |
f051c466 | 273 | return -EINVAL; |
0c2498f1 SH |
274 | |
275 | mutex_lock(&pwm_lock); | |
276 | ||
f9a8ee8c | 277 | ret = alloc_pwms(chip->npwm); |
f051c466 TR |
278 | if (ret < 0) |
279 | goto out; | |
280 | ||
f9a8ee8c UKK |
281 | chip->base = ret; |
282 | ||
2907f8ab | 283 | chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL); |
f051c466 TR |
284 | if (!chip->pwms) { |
285 | ret = -ENOMEM; | |
0c2498f1 SH |
286 | goto out; |
287 | } | |
288 | ||
f051c466 TR |
289 | for (i = 0; i < chip->npwm; i++) { |
290 | pwm = &chip->pwms[i]; | |
291 | ||
292 | pwm->chip = chip; | |
293 | pwm->pwm = chip->base + i; | |
294 | pwm->hwpwm = i; | |
0c2498f1 | 295 | |
f051c466 TR |
296 | radix_tree_insert(&pwm_tree, pwm->pwm, pwm); |
297 | } | |
298 | ||
299 | bitmap_set(allocated_pwms, chip->base, chip->npwm); | |
300 | ||
301 | INIT_LIST_HEAD(&chip->list); | |
302 | list_add(&chip->list, &pwm_chips); | |
0c2498f1 | 303 | |
f051c466 TR |
304 | ret = 0; |
305 | ||
7299ab70 TR |
306 | if (IS_ENABLED(CONFIG_OF)) |
307 | of_pwmchip_add(chip); | |
308 | ||
f051c466 TR |
309 | out: |
310 | mutex_unlock(&pwm_lock); | |
347ab948 PH |
311 | |
312 | if (!ret) | |
313 | pwmchip_sysfs_export(chip); | |
314 | ||
0c2498f1 SH |
315 | return ret; |
316 | } | |
317 | EXPORT_SYMBOL_GPL(pwmchip_add); | |
318 | ||
319 | /** | |
320 | * pwmchip_remove() - remove a PWM chip | |
321 | * @chip: the PWM chip to remove | |
322 | * | |
323 | * Removes a PWM chip. This function may return busy if the PWM chip provides | |
324 | * a PWM device that is still requested. | |
04883802 TR |
325 | * |
326 | * Returns: 0 on success or a negative error code on failure. | |
0c2498f1 | 327 | */ |
8083f58d | 328 | void pwmchip_remove(struct pwm_chip *chip) |
0c2498f1 | 329 | { |
347ab948 | 330 | pwmchip_sysfs_unexport(chip); |
0733424c | 331 | |
0c2498f1 SH |
332 | mutex_lock(&pwm_lock); |
333 | ||
f051c466 | 334 | list_del_init(&chip->list); |
7299ab70 TR |
335 | |
336 | if (IS_ENABLED(CONFIG_OF)) | |
337 | of_pwmchip_remove(chip); | |
338 | ||
f051c466 | 339 | free_pwms(chip); |
0c2498f1 | 340 | |
0c2498f1 | 341 | mutex_unlock(&pwm_lock); |
0c2498f1 SH |
342 | } |
343 | EXPORT_SYMBOL_GPL(pwmchip_remove); | |
344 | ||
bcda91bf UKK |
345 | static void devm_pwmchip_remove(void *data) |
346 | { | |
347 | struct pwm_chip *chip = data; | |
348 | ||
349 | pwmchip_remove(chip); | |
350 | } | |
351 | ||
352 | int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip) | |
353 | { | |
354 | int ret; | |
355 | ||
356 | ret = pwmchip_add(chip); | |
357 | if (ret) | |
358 | return ret; | |
359 | ||
360 | return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip); | |
361 | } | |
362 | EXPORT_SYMBOL_GPL(devm_pwmchip_add); | |
363 | ||
0c2498f1 SH |
364 | /** |
365 | * pwm_request() - request a PWM device | |
04883802 | 366 | * @pwm: global PWM device index |
0c2498f1 | 367 | * @label: PWM device label |
8138d2dd TR |
368 | * |
369 | * This function is deprecated, use pwm_get() instead. | |
04883802 TR |
370 | * |
371 | * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on | |
372 | * failure. | |
0c2498f1 | 373 | */ |
f051c466 | 374 | struct pwm_device *pwm_request(int pwm, const char *label) |
0c2498f1 | 375 | { |
f051c466 TR |
376 | struct pwm_device *dev; |
377 | int err; | |
378 | ||
379 | if (pwm < 0 || pwm >= MAX_PWMS) | |
380 | return ERR_PTR(-EINVAL); | |
0c2498f1 SH |
381 | |
382 | mutex_lock(&pwm_lock); | |
383 | ||
f051c466 TR |
384 | dev = pwm_to_device(pwm); |
385 | if (!dev) { | |
386 | dev = ERR_PTR(-EPROBE_DEFER); | |
0c2498f1 SH |
387 | goto out; |
388 | } | |
389 | ||
f051c466 TR |
390 | err = pwm_device_request(dev, label); |
391 | if (err < 0) | |
392 | dev = ERR_PTR(err); | |
0c2498f1 | 393 | |
f051c466 TR |
394 | out: |
395 | mutex_unlock(&pwm_lock); | |
0c2498f1 | 396 | |
f051c466 TR |
397 | return dev; |
398 | } | |
399 | EXPORT_SYMBOL_GPL(pwm_request); | |
0c2498f1 | 400 | |
f051c466 TR |
401 | /** |
402 | * pwm_request_from_chip() - request a PWM device relative to a PWM chip | |
403 | * @chip: PWM chip | |
404 | * @index: per-chip index of the PWM to request | |
405 | * @label: a literal description string of this PWM | |
406 | * | |
04883802 TR |
407 | * Returns: A pointer to the PWM device at the given index of the given PWM |
408 | * chip. A negative error code is returned if the index is not valid for the | |
409 | * specified PWM chip or if the PWM device cannot be requested. | |
f051c466 TR |
410 | */ |
411 | struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, | |
412 | unsigned int index, | |
413 | const char *label) | |
414 | { | |
415 | struct pwm_device *pwm; | |
416 | int err; | |
0c2498f1 | 417 | |
f051c466 TR |
418 | if (!chip || index >= chip->npwm) |
419 | return ERR_PTR(-EINVAL); | |
0c2498f1 | 420 | |
f051c466 TR |
421 | mutex_lock(&pwm_lock); |
422 | pwm = &chip->pwms[index]; | |
0c2498f1 | 423 | |
f051c466 TR |
424 | err = pwm_device_request(pwm, label); |
425 | if (err < 0) | |
426 | pwm = ERR_PTR(err); | |
427 | ||
428 | mutex_unlock(&pwm_lock); | |
0c2498f1 SH |
429 | return pwm; |
430 | } | |
f051c466 | 431 | EXPORT_SYMBOL_GPL(pwm_request_from_chip); |
0c2498f1 SH |
432 | |
433 | /** | |
434 | * pwm_free() - free a PWM device | |
435 | * @pwm: PWM device | |
8138d2dd TR |
436 | * |
437 | * This function is deprecated, use pwm_put() instead. | |
0c2498f1 SH |
438 | */ |
439 | void pwm_free(struct pwm_device *pwm) | |
440 | { | |
8138d2dd | 441 | pwm_put(pwm); |
0c2498f1 SH |
442 | } |
443 | EXPORT_SYMBOL_GPL(pwm_free); | |
444 | ||
374c1104 JY |
445 | static void pwm_apply_state_debug(struct pwm_device *pwm, |
446 | const struct pwm_state *state) | |
3ad1f3a3 UKK |
447 | { |
448 | struct pwm_state *last = &pwm->last; | |
449 | struct pwm_chip *chip = pwm->chip; | |
450 | struct pwm_state s1, s2; | |
451 | int err; | |
452 | ||
453 | if (!IS_ENABLED(CONFIG_PWM_DEBUG)) | |
454 | return; | |
455 | ||
456 | /* No reasonable diagnosis possible without .get_state() */ | |
457 | if (!chip->ops->get_state) | |
458 | return; | |
459 | ||
460 | /* | |
461 | * *state was just applied. Read out the hardware state and do some | |
462 | * checks. | |
463 | */ | |
464 | ||
465 | chip->ops->get_state(chip, pwm, &s1); | |
466 | trace_pwm_get(pwm, &s1); | |
467 | ||
468 | /* | |
469 | * The lowlevel driver either ignored .polarity (which is a bug) or as | |
470 | * best effort inverted .polarity and fixed .duty_cycle respectively. | |
471 | * Undo this inversion and fixup for further tests. | |
472 | */ | |
473 | if (s1.enabled && s1.polarity != state->polarity) { | |
474 | s2.polarity = state->polarity; | |
475 | s2.duty_cycle = s1.period - s1.duty_cycle; | |
476 | s2.period = s1.period; | |
477 | s2.enabled = s1.enabled; | |
478 | } else { | |
479 | s2 = s1; | |
480 | } | |
481 | ||
482 | if (s2.polarity != state->polarity && | |
483 | state->duty_cycle < state->period) | |
484 | dev_warn(chip->dev, ".apply ignored .polarity\n"); | |
485 | ||
486 | if (state->enabled && | |
487 | last->polarity == state->polarity && | |
488 | last->period > s2.period && | |
489 | last->period <= state->period) | |
490 | dev_warn(chip->dev, | |
a9d887dc | 491 | ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n", |
3ad1f3a3 UKK |
492 | state->period, s2.period, last->period); |
493 | ||
494 | if (state->enabled && state->period < s2.period) | |
495 | dev_warn(chip->dev, | |
a9d887dc | 496 | ".apply is supposed to round down period (requested: %llu, applied: %llu)\n", |
3ad1f3a3 UKK |
497 | state->period, s2.period); |
498 | ||
499 | if (state->enabled && | |
500 | last->polarity == state->polarity && | |
501 | last->period == s2.period && | |
502 | last->duty_cycle > s2.duty_cycle && | |
503 | last->duty_cycle <= state->duty_cycle) | |
504 | dev_warn(chip->dev, | |
a9d887dc | 505 | ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n", |
3ad1f3a3 UKK |
506 | state->duty_cycle, state->period, |
507 | s2.duty_cycle, s2.period, | |
508 | last->duty_cycle, last->period); | |
509 | ||
510 | if (state->enabled && state->duty_cycle < s2.duty_cycle) | |
511 | dev_warn(chip->dev, | |
a9d887dc | 512 | ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n", |
3ad1f3a3 UKK |
513 | state->duty_cycle, state->period, |
514 | s2.duty_cycle, s2.period); | |
515 | ||
516 | if (!state->enabled && s2.enabled && s2.duty_cycle > 0) | |
517 | dev_warn(chip->dev, | |
db539cb9 | 518 | "requested disabled, but yielded enabled with duty > 0\n"); |
3ad1f3a3 UKK |
519 | |
520 | /* reapply the state that the driver reported being configured. */ | |
521 | err = chip->ops->apply(chip, pwm, &s1); | |
522 | if (err) { | |
523 | *last = s1; | |
524 | dev_err(chip->dev, "failed to reapply current setting\n"); | |
525 | return; | |
526 | } | |
527 | ||
528 | trace_pwm_apply(pwm, &s1); | |
529 | ||
530 | chip->ops->get_state(chip, pwm, last); | |
531 | trace_pwm_get(pwm, last); | |
532 | ||
533 | /* reapplication of the current state should give an exact match */ | |
534 | if (s1.enabled != last->enabled || | |
535 | s1.polarity != last->polarity || | |
536 | (s1.enabled && s1.period != last->period) || | |
537 | (s1.enabled && s1.duty_cycle != last->duty_cycle)) { | |
538 | dev_err(chip->dev, | |
a9d887dc | 539 | ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n", |
3ad1f3a3 UKK |
540 | s1.enabled, s1.polarity, s1.duty_cycle, s1.period, |
541 | last->enabled, last->polarity, last->duty_cycle, | |
542 | last->period); | |
543 | } | |
544 | } | |
545 | ||
0c2498f1 | 546 | /** |
5ec803ed | 547 | * pwm_apply_state() - atomically apply a new state to a PWM device |
0c2498f1 | 548 | * @pwm: PWM device |
71523d18 | 549 | * @state: new state to apply |
0c2498f1 | 550 | */ |
71523d18 | 551 | int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state) |
0c2498f1 | 552 | { |
fc3c5512 | 553 | struct pwm_chip *chip; |
76abbdde HS |
554 | int err; |
555 | ||
4ad91a22 UKK |
556 | /* |
557 | * Some lowlevel driver's implementations of .apply() make use of | |
558 | * mutexes, also with some drivers only returning when the new | |
559 | * configuration is active calling pwm_apply_state() from atomic context | |
560 | * is a bad idea. So make it explicit that calling this function might | |
561 | * sleep. | |
562 | */ | |
563 | might_sleep(); | |
564 | ||
ef2bf499 BN |
565 | if (!pwm || !state || !state->period || |
566 | state->duty_cycle > state->period) | |
f051c466 TR |
567 | return -EINVAL; |
568 | ||
fc3c5512 UKK |
569 | chip = pwm->chip; |
570 | ||
309b32fb UKK |
571 | if (state->period == pwm->state.period && |
572 | state->duty_cycle == pwm->state.duty_cycle && | |
573 | state->polarity == pwm->state.polarity && | |
9e40ee18 CG |
574 | state->enabled == pwm->state.enabled && |
575 | state->usage_power == pwm->state.usage_power) | |
5ec803ed | 576 | return 0; |
76abbdde | 577 | |
0829c35d | 578 | err = chip->ops->apply(chip, pwm, state); |
77965c98 UKK |
579 | if (err) |
580 | return err; | |
5ec803ed | 581 | |
77965c98 | 582 | trace_pwm_apply(pwm, state); |
5ec803ed | 583 | |
77965c98 | 584 | pwm->state = *state; |
5ec803ed | 585 | |
77965c98 UKK |
586 | /* |
587 | * only do this after pwm->state was applied as some | |
588 | * implementations of .get_state depend on this | |
589 | */ | |
590 | pwm_apply_state_debug(pwm, state); | |
0aa0869c | 591 | |
459a25af | 592 | return 0; |
0aa0869c | 593 | } |
5ec803ed | 594 | EXPORT_SYMBOL_GPL(pwm_apply_state); |
0aa0869c | 595 | |
3a3d1a4e LJ |
596 | /** |
597 | * pwm_capture() - capture and report a PWM signal | |
598 | * @pwm: PWM device | |
599 | * @result: structure to fill with capture result | |
600 | * @timeout: time to wait, in milliseconds, before giving up on capture | |
601 | * | |
602 | * Returns: 0 on success or a negative error code on failure. | |
603 | */ | |
604 | int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, | |
605 | unsigned long timeout) | |
606 | { | |
607 | int err; | |
608 | ||
609 | if (!pwm || !pwm->chip->ops) | |
610 | return -EINVAL; | |
611 | ||
612 | if (!pwm->chip->ops->capture) | |
613 | return -ENOSYS; | |
614 | ||
615 | mutex_lock(&pwm_lock); | |
616 | err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout); | |
617 | mutex_unlock(&pwm_lock); | |
618 | ||
619 | return err; | |
620 | } | |
621 | EXPORT_SYMBOL_GPL(pwm_capture); | |
622 | ||
0c2498f1 | 623 | /** |
5ec803ed | 624 | * pwm_adjust_config() - adjust the current PWM config to the PWM arguments |
0c2498f1 | 625 | * @pwm: PWM device |
04883802 | 626 | * |
5ec803ed BB |
627 | * This function will adjust the PWM config to the PWM arguments provided |
628 | * by the DT or PWM lookup table. This is particularly useful to adapt | |
629 | * the bootloader config to the Linux one. | |
0c2498f1 | 630 | */ |
5ec803ed | 631 | int pwm_adjust_config(struct pwm_device *pwm) |
0c2498f1 | 632 | { |
5ec803ed BB |
633 | struct pwm_state state; |
634 | struct pwm_args pargs; | |
d1cd2142 | 635 | |
5ec803ed BB |
636 | pwm_get_args(pwm, &pargs); |
637 | pwm_get_state(pwm, &state); | |
d1cd2142 | 638 | |
5ec803ed BB |
639 | /* |
640 | * If the current period is zero it means that either the PWM driver | |
641 | * does not support initial state retrieval or the PWM has not yet | |
642 | * been configured. | |
643 | * | |
644 | * In either case, we setup the new period and polarity, and assign a | |
645 | * duty cycle of 0. | |
646 | */ | |
647 | if (!state.period) { | |
648 | state.duty_cycle = 0; | |
649 | state.period = pargs.period; | |
650 | state.polarity = pargs.polarity; | |
d1cd2142 | 651 | |
5ec803ed | 652 | return pwm_apply_state(pwm, &state); |
d1cd2142 JR |
653 | } |
654 | ||
5ec803ed BB |
655 | /* |
656 | * Adjust the PWM duty cycle/period based on the period value provided | |
657 | * in PWM args. | |
658 | */ | |
659 | if (pargs.period != state.period) { | |
660 | u64 dutycycle = (u64)state.duty_cycle * pargs.period; | |
0c2498f1 | 661 | |
5ec803ed BB |
662 | do_div(dutycycle, state.period); |
663 | state.duty_cycle = dutycycle; | |
664 | state.period = pargs.period; | |
665 | } | |
0c2498f1 | 666 | |
5ec803ed BB |
667 | /* |
668 | * If the polarity changed, we should also change the duty cycle. | |
669 | */ | |
670 | if (pargs.polarity != state.polarity) { | |
671 | state.polarity = pargs.polarity; | |
672 | state.duty_cycle = state.period - state.duty_cycle; | |
09a7e4a3 | 673 | } |
5ec803ed BB |
674 | |
675 | return pwm_apply_state(pwm, &state); | |
0c2498f1 | 676 | } |
5ec803ed | 677 | EXPORT_SYMBOL_GPL(pwm_adjust_config); |
62099abf | 678 | |
ca06616b | 679 | static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode) |
7299ab70 TR |
680 | { |
681 | struct pwm_chip *chip; | |
682 | ||
683 | mutex_lock(&pwm_lock); | |
684 | ||
685 | list_for_each_entry(chip, &pwm_chips, list) | |
b4319802 | 686 | if (chip->dev && device_match_fwnode(chip->dev, fwnode)) { |
7299ab70 TR |
687 | mutex_unlock(&pwm_lock); |
688 | return chip; | |
689 | } | |
690 | ||
691 | mutex_unlock(&pwm_lock); | |
692 | ||
693 | return ERR_PTR(-EPROBE_DEFER); | |
694 | } | |
695 | ||
b2c200e3 FG |
696 | static struct device_link *pwm_device_link_add(struct device *dev, |
697 | struct pwm_device *pwm) | |
698 | { | |
699 | struct device_link *dl; | |
700 | ||
701 | if (!dev) { | |
702 | /* | |
703 | * No device for the PWM consumer has been provided. It may | |
704 | * impact the PM sequence ordering: the PWM supplier may get | |
705 | * suspended before the consumer. | |
706 | */ | |
707 | dev_warn(pwm->chip->dev, | |
708 | "No consumer device specified to create a link to\n"); | |
709 | return NULL; | |
710 | } | |
711 | ||
712 | dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER); | |
713 | if (!dl) { | |
714 | dev_err(dev, "failed to create device link to %s\n", | |
715 | dev_name(pwm->chip->dev)); | |
716 | return ERR_PTR(-EINVAL); | |
717 | } | |
718 | ||
719 | return dl; | |
720 | } | |
721 | ||
7299ab70 | 722 | /** |
8eb96127 | 723 | * of_pwm_get() - request a PWM via the PWM framework |
b2c200e3 | 724 | * @dev: device for PWM consumer |
7299ab70 TR |
725 | * @np: device node to get the PWM from |
726 | * @con_id: consumer name | |
727 | * | |
728 | * Returns the PWM device parsed from the phandle and index specified in the | |
729 | * "pwms" property of a device tree node or a negative error-code on failure. | |
730 | * Values parsed from the device tree are stored in the returned PWM device | |
731 | * object. | |
732 | * | |
733 | * If con_id is NULL, the first PWM device listed in the "pwms" property will | |
734 | * be requested. Otherwise the "pwm-names" property is used to do a reverse | |
735 | * lookup of the PWM index. This also means that the "pwm-names" property | |
736 | * becomes mandatory for devices that look up the PWM device via the con_id | |
737 | * parameter. | |
04883802 TR |
738 | * |
739 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded | |
740 | * error code on failure. | |
7299ab70 | 741 | */ |
b5ae0ad5 AS |
742 | static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np, |
743 | const char *con_id) | |
7299ab70 TR |
744 | { |
745 | struct pwm_device *pwm = NULL; | |
746 | struct of_phandle_args args; | |
b2c200e3 | 747 | struct device_link *dl; |
7299ab70 TR |
748 | struct pwm_chip *pc; |
749 | int index = 0; | |
750 | int err; | |
751 | ||
752 | if (con_id) { | |
753 | index = of_property_match_string(np, "pwm-names", con_id); | |
754 | if (index < 0) | |
755 | return ERR_PTR(index); | |
756 | } | |
757 | ||
758 | err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, | |
759 | &args); | |
760 | if (err) { | |
f2dafc09 | 761 | pr_err("%s(): can't parse \"pwms\" property\n", __func__); |
7299ab70 TR |
762 | return ERR_PTR(err); |
763 | } | |
764 | ||
ca06616b | 765 | pc = fwnode_to_pwmchip(of_fwnode_handle(args.np)); |
7299ab70 | 766 | if (IS_ERR(pc)) { |
93c292ef JB |
767 | if (PTR_ERR(pc) != -EPROBE_DEFER) |
768 | pr_err("%s(): PWM chip not found\n", __func__); | |
769 | ||
7299ab70 TR |
770 | pwm = ERR_CAST(pc); |
771 | goto put; | |
772 | } | |
773 | ||
7299ab70 TR |
774 | pwm = pc->of_xlate(pc, &args); |
775 | if (IS_ERR(pwm)) | |
776 | goto put; | |
777 | ||
b2c200e3 FG |
778 | dl = pwm_device_link_add(dev, pwm); |
779 | if (IS_ERR(dl)) { | |
780 | /* of_xlate ended up calling pwm_request_from_chip() */ | |
781 | pwm_free(pwm); | |
782 | pwm = ERR_CAST(dl); | |
783 | goto put; | |
784 | } | |
785 | ||
7299ab70 TR |
786 | /* |
787 | * If a consumer name was not given, try to look it up from the | |
788 | * "pwm-names" property if it exists. Otherwise use the name of | |
789 | * the user device node. | |
790 | */ | |
791 | if (!con_id) { | |
792 | err = of_property_read_string_index(np, "pwm-names", index, | |
793 | &con_id); | |
794 | if (err < 0) | |
795 | con_id = np->name; | |
796 | } | |
797 | ||
798 | pwm->label = con_id; | |
799 | ||
800 | put: | |
801 | of_node_put(args.np); | |
802 | ||
803 | return pwm; | |
804 | } | |
805 | ||
4a6ef8e3 NV |
806 | /** |
807 | * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI | |
e625fb70 | 808 | * @fwnode: firmware node to get the "pwms" property from |
4a6ef8e3 NV |
809 | * |
810 | * Returns the PWM device parsed from the fwnode and index specified in the | |
811 | * "pwms" property or a negative error-code on failure. | |
812 | * Values parsed from the device tree are stored in the returned PWM device | |
813 | * object. | |
814 | * | |
815 | * This is analogous to of_pwm_get() except con_id is not yet supported. | |
816 | * ACPI entries must look like | |
817 | * Package () {"pwms", Package () | |
818 | * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}} | |
819 | * | |
820 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded | |
821 | * error code on failure. | |
822 | */ | |
e625fb70 | 823 | static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode) |
4a6ef8e3 | 824 | { |
bebedf2b | 825 | struct pwm_device *pwm; |
4a6ef8e3 | 826 | struct fwnode_reference_args args; |
4a6ef8e3 NV |
827 | struct pwm_chip *chip; |
828 | int ret; | |
829 | ||
830 | memset(&args, 0, sizeof(args)); | |
831 | ||
832 | ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args); | |
833 | if (ret < 0) | |
834 | return ERR_PTR(ret); | |
835 | ||
4a6ef8e3 NV |
836 | if (args.nargs < 2) |
837 | return ERR_PTR(-EPROTO); | |
838 | ||
e5c38ba9 | 839 | chip = fwnode_to_pwmchip(args.fwnode); |
4a6ef8e3 NV |
840 | if (IS_ERR(chip)) |
841 | return ERR_CAST(chip); | |
842 | ||
843 | pwm = pwm_request_from_chip(chip, args.args[0], NULL); | |
844 | if (IS_ERR(pwm)) | |
845 | return pwm; | |
846 | ||
847 | pwm->args.period = args.args[1]; | |
848 | pwm->args.polarity = PWM_POLARITY_NORMAL; | |
849 | ||
850 | if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED) | |
851 | pwm->args.polarity = PWM_POLARITY_INVERSED; | |
4a6ef8e3 NV |
852 | |
853 | return pwm; | |
854 | } | |
855 | ||
8138d2dd TR |
856 | /** |
857 | * pwm_add_table() - register PWM device consumers | |
858 | * @table: array of consumers to register | |
859 | * @num: number of consumers in table | |
860 | */ | |
c264f111 | 861 | void pwm_add_table(struct pwm_lookup *table, size_t num) |
8138d2dd TR |
862 | { |
863 | mutex_lock(&pwm_lookup_lock); | |
864 | ||
865 | while (num--) { | |
866 | list_add_tail(&table->list, &pwm_lookup_list); | |
867 | table++; | |
868 | } | |
869 | ||
870 | mutex_unlock(&pwm_lookup_lock); | |
871 | } | |
872 | ||
efb0de55 SK |
873 | /** |
874 | * pwm_remove_table() - unregister PWM device consumers | |
875 | * @table: array of consumers to unregister | |
876 | * @num: number of consumers in table | |
877 | */ | |
878 | void pwm_remove_table(struct pwm_lookup *table, size_t num) | |
879 | { | |
880 | mutex_lock(&pwm_lookup_lock); | |
881 | ||
882 | while (num--) { | |
883 | list_del(&table->list); | |
884 | table++; | |
885 | } | |
886 | ||
887 | mutex_unlock(&pwm_lookup_lock); | |
888 | } | |
889 | ||
8138d2dd TR |
890 | /** |
891 | * pwm_get() - look up and request a PWM device | |
892 | * @dev: device for PWM consumer | |
893 | * @con_id: consumer name | |
894 | * | |
7299ab70 TR |
895 | * Lookup is first attempted using DT. If the device was not instantiated from |
896 | * a device tree, a PWM chip and a relative index is looked up via a table | |
897 | * supplied by board setup code (see pwm_add_table()). | |
8138d2dd TR |
898 | * |
899 | * Once a PWM chip has been found the specified PWM device will be requested | |
900 | * and is ready to be used. | |
04883802 TR |
901 | * |
902 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded | |
903 | * error code on failure. | |
8138d2dd TR |
904 | */ |
905 | struct pwm_device *pwm_get(struct device *dev, const char *con_id) | |
906 | { | |
e625fb70 | 907 | const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL; |
e50d3523 | 908 | const char *dev_id = dev ? dev_name(dev) : NULL; |
69efb343 HG |
909 | struct pwm_device *pwm; |
910 | struct pwm_chip *chip; | |
b2c200e3 | 911 | struct device_link *dl; |
8138d2dd | 912 | unsigned int best = 0; |
70145f87 | 913 | struct pwm_lookup *p, *chosen = NULL; |
8138d2dd | 914 | unsigned int match; |
b526a314 | 915 | int err; |
8138d2dd | 916 | |
7299ab70 | 917 | /* look up via DT first */ |
e625fb70 AS |
918 | if (is_of_node(fwnode)) |
919 | return of_pwm_get(dev, to_of_node(fwnode), con_id); | |
7299ab70 | 920 | |
4a6ef8e3 | 921 | /* then lookup via ACPI */ |
e625fb70 AS |
922 | if (is_acpi_node(fwnode)) { |
923 | pwm = acpi_pwm_get(fwnode); | |
6cf9481b HG |
924 | if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT) |
925 | return pwm; | |
926 | } | |
7299ab70 | 927 | |
8138d2dd TR |
928 | /* |
929 | * We look up the provider in the static table typically provided by | |
930 | * board setup code. We first try to lookup the consumer device by | |
931 | * name. If the consumer device was passed in as NULL or if no match | |
932 | * was found, we try to find the consumer by directly looking it up | |
933 | * by name. | |
934 | * | |
935 | * If a match is found, the provider PWM chip is looked up by name | |
936 | * and a PWM device is requested using the PWM device per-chip index. | |
937 | * | |
938 | * The lookup algorithm was shamelessly taken from the clock | |
939 | * framework: | |
940 | * | |
941 | * We do slightly fuzzy matching here: | |
942 | * An entry with a NULL ID is assumed to be a wildcard. | |
943 | * If an entry has a device ID, it must match | |
944 | * If an entry has a connection ID, it must match | |
945 | * Then we take the most specific entry - with the following order | |
946 | * of precedence: dev+con > dev only > con only. | |
947 | */ | |
948 | mutex_lock(&pwm_lookup_lock); | |
949 | ||
950 | list_for_each_entry(p, &pwm_lookup_list, list) { | |
951 | match = 0; | |
952 | ||
953 | if (p->dev_id) { | |
954 | if (!dev_id || strcmp(p->dev_id, dev_id)) | |
955 | continue; | |
956 | ||
957 | match += 2; | |
958 | } | |
959 | ||
960 | if (p->con_id) { | |
961 | if (!con_id || strcmp(p->con_id, con_id)) | |
962 | continue; | |
963 | ||
964 | match += 1; | |
965 | } | |
966 | ||
967 | if (match > best) { | |
70145f87 | 968 | chosen = p; |
8138d2dd TR |
969 | |
970 | if (match != 3) | |
971 | best = match; | |
972 | else | |
973 | break; | |
974 | } | |
975 | } | |
976 | ||
69efb343 HG |
977 | mutex_unlock(&pwm_lookup_lock); |
978 | ||
979 | if (!chosen) | |
980 | return ERR_PTR(-ENODEV); | |
3796ce1d | 981 | |
70145f87 | 982 | chip = pwmchip_find_by_name(chosen->provider); |
b526a314 HG |
983 | |
984 | /* | |
985 | * If the lookup entry specifies a module, load the module and retry | |
986 | * the PWM chip lookup. This can be used to work around driver load | |
987 | * ordering issues if driver's can't be made to properly support the | |
988 | * deferred probe mechanism. | |
989 | */ | |
990 | if (!chip && chosen->module) { | |
991 | err = request_module(chosen->module); | |
992 | if (err == 0) | |
993 | chip = pwmchip_find_by_name(chosen->provider); | |
994 | } | |
995 | ||
70145f87 | 996 | if (!chip) |
69efb343 | 997 | return ERR_PTR(-EPROBE_DEFER); |
3796ce1d | 998 | |
70145f87 GU |
999 | pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); |
1000 | if (IS_ERR(pwm)) | |
69efb343 | 1001 | return pwm; |
8138d2dd | 1002 | |
b2c200e3 FG |
1003 | dl = pwm_device_link_add(dev, pwm); |
1004 | if (IS_ERR(dl)) { | |
1005 | pwm_free(pwm); | |
1006 | return ERR_CAST(dl); | |
1007 | } | |
1008 | ||
fbd45a12 BB |
1009 | pwm->args.period = chosen->period; |
1010 | pwm->args.polarity = chosen->polarity; | |
1011 | ||
8138d2dd TR |
1012 | return pwm; |
1013 | } | |
1014 | EXPORT_SYMBOL_GPL(pwm_get); | |
1015 | ||
1016 | /** | |
1017 | * pwm_put() - release a PWM device | |
1018 | * @pwm: PWM device | |
1019 | */ | |
1020 | void pwm_put(struct pwm_device *pwm) | |
1021 | { | |
1022 | if (!pwm) | |
1023 | return; | |
1024 | ||
1025 | mutex_lock(&pwm_lock); | |
1026 | ||
1027 | if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { | |
e50d3523 | 1028 | pr_warn("PWM device already freed\n"); |
8138d2dd TR |
1029 | goto out; |
1030 | } | |
1031 | ||
1032 | if (pwm->chip->ops->free) | |
1033 | pwm->chip->ops->free(pwm->chip, pwm); | |
1034 | ||
e926b12c | 1035 | pwm_set_chip_data(pwm, NULL); |
8138d2dd TR |
1036 | pwm->label = NULL; |
1037 | ||
1038 | module_put(pwm->chip->ops->owner); | |
1039 | out: | |
1040 | mutex_unlock(&pwm_lock); | |
1041 | } | |
1042 | EXPORT_SYMBOL_GPL(pwm_put); | |
1043 | ||
9ae241d0 | 1044 | static void devm_pwm_release(void *pwm) |
6354316d | 1045 | { |
9ae241d0 | 1046 | pwm_put(pwm); |
6354316d AC |
1047 | } |
1048 | ||
1049 | /** | |
1050 | * devm_pwm_get() - resource managed pwm_get() | |
1051 | * @dev: device for PWM consumer | |
1052 | * @con_id: consumer name | |
1053 | * | |
1054 | * This function performs like pwm_get() but the acquired PWM device will | |
1055 | * automatically be released on driver detach. | |
04883802 TR |
1056 | * |
1057 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded | |
1058 | * error code on failure. | |
6354316d AC |
1059 | */ |
1060 | struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id) | |
1061 | { | |
9ae241d0 AS |
1062 | struct pwm_device *pwm; |
1063 | int ret; | |
6354316d AC |
1064 | |
1065 | pwm = pwm_get(dev, con_id); | |
9ae241d0 AS |
1066 | if (IS_ERR(pwm)) |
1067 | return pwm; | |
1068 | ||
1069 | ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); | |
1070 | if (ret) | |
1071 | return ERR_PTR(ret); | |
6354316d AC |
1072 | |
1073 | return pwm; | |
1074 | } | |
1075 | EXPORT_SYMBOL_GPL(devm_pwm_get); | |
1076 | ||
4a6ef8e3 NV |
1077 | /** |
1078 | * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node | |
1079 | * @dev: device for PWM consumer | |
1080 | * @fwnode: firmware node to get the PWM from | |
1081 | * @con_id: consumer name | |
1082 | * | |
1083 | * Returns the PWM device parsed from the firmware node. See of_pwm_get() and | |
1084 | * acpi_pwm_get() for a detailed description. | |
1085 | * | |
1086 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded | |
1087 | * error code on failure. | |
1088 | */ | |
1089 | struct pwm_device *devm_fwnode_pwm_get(struct device *dev, | |
1090 | struct fwnode_handle *fwnode, | |
1091 | const char *con_id) | |
1092 | { | |
9ae241d0 AS |
1093 | struct pwm_device *pwm = ERR_PTR(-ENODEV); |
1094 | int ret; | |
4a6ef8e3 NV |
1095 | |
1096 | if (is_of_node(fwnode)) | |
1097 | pwm = of_pwm_get(dev, to_of_node(fwnode), con_id); | |
1098 | else if (is_acpi_node(fwnode)) | |
1099 | pwm = acpi_pwm_get(fwnode); | |
9ae241d0 AS |
1100 | if (IS_ERR(pwm)) |
1101 | return pwm; | |
4a6ef8e3 | 1102 | |
9ae241d0 AS |
1103 | ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); |
1104 | if (ret) | |
1105 | return ERR_PTR(ret); | |
4a6ef8e3 NV |
1106 | |
1107 | return pwm; | |
1108 | } | |
1109 | EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get); | |
1110 | ||
62099abf TR |
1111 | #ifdef CONFIG_DEBUG_FS |
1112 | static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s) | |
1113 | { | |
1114 | unsigned int i; | |
1115 | ||
1116 | for (i = 0; i < chip->npwm; i++) { | |
1117 | struct pwm_device *pwm = &chip->pwms[i]; | |
39100cee BB |
1118 | struct pwm_state state; |
1119 | ||
1120 | pwm_get_state(pwm, &state); | |
62099abf TR |
1121 | |
1122 | seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); | |
1123 | ||
1124 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) | |
adcba1e3 | 1125 | seq_puts(s, " requested"); |
62099abf | 1126 | |
39100cee | 1127 | if (state.enabled) |
adcba1e3 | 1128 | seq_puts(s, " enabled"); |
62099abf | 1129 | |
a9d887dc GDS |
1130 | seq_printf(s, " period: %llu ns", state.period); |
1131 | seq_printf(s, " duty: %llu ns", state.duty_cycle); | |
23e3523f HS |
1132 | seq_printf(s, " polarity: %s", |
1133 | state.polarity ? "inverse" : "normal"); | |
1134 | ||
9e40ee18 CG |
1135 | if (state.usage_power) |
1136 | seq_puts(s, " usage_power"); | |
1137 | ||
adcba1e3 | 1138 | seq_puts(s, "\n"); |
62099abf TR |
1139 | } |
1140 | } | |
1141 | ||
1142 | static void *pwm_seq_start(struct seq_file *s, loff_t *pos) | |
1143 | { | |
1144 | mutex_lock(&pwm_lock); | |
1145 | s->private = ""; | |
1146 | ||
1147 | return seq_list_start(&pwm_chips, *pos); | |
1148 | } | |
1149 | ||
1150 | static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
1151 | { | |
1152 | s->private = "\n"; | |
1153 | ||
1154 | return seq_list_next(v, &pwm_chips, pos); | |
1155 | } | |
1156 | ||
1157 | static void pwm_seq_stop(struct seq_file *s, void *v) | |
1158 | { | |
1159 | mutex_unlock(&pwm_lock); | |
1160 | } | |
1161 | ||
1162 | static int pwm_seq_show(struct seq_file *s, void *v) | |
1163 | { | |
1164 | struct pwm_chip *chip = list_entry(v, struct pwm_chip, list); | |
1165 | ||
1166 | seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private, | |
1167 | chip->dev->bus ? chip->dev->bus->name : "no-bus", | |
1168 | dev_name(chip->dev), chip->npwm, | |
1169 | (chip->npwm != 1) ? "s" : ""); | |
1170 | ||
cc2d2247 | 1171 | pwm_dbg_show(chip, s); |
62099abf TR |
1172 | |
1173 | return 0; | |
1174 | } | |
1175 | ||
f339e79b | 1176 | static const struct seq_operations pwm_debugfs_sops = { |
62099abf TR |
1177 | .start = pwm_seq_start, |
1178 | .next = pwm_seq_next, | |
1179 | .stop = pwm_seq_stop, | |
1180 | .show = pwm_seq_show, | |
1181 | }; | |
1182 | ||
f339e79b | 1183 | DEFINE_SEQ_ATTRIBUTE(pwm_debugfs); |
62099abf TR |
1184 | |
1185 | static int __init pwm_debugfs_init(void) | |
1186 | { | |
765edf0b | 1187 | debugfs_create_file("pwm", S_IFREG | 0444, NULL, NULL, |
f339e79b | 1188 | &pwm_debugfs_fops); |
62099abf TR |
1189 | |
1190 | return 0; | |
1191 | } | |
62099abf TR |
1192 | subsys_initcall(pwm_debugfs_init); |
1193 | #endif /* CONFIG_DEBUG_FS */ |