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