pwm: Ensure a struct pwm has the same lifetime as its pwm_chip
[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 10#include <linux/module.h>
54c86dd2 11#include <linux/idr.h>
0a41b0c5 12#include <linux/of.h>
0c2498f1
SH
13#include <linux/pwm.h>
14#include <linux/list.h>
15#include <linux/mutex.h>
16#include <linux/err.h>
17#include <linux/slab.h>
18#include <linux/device.h>
62099abf
TR
19#include <linux/debugfs.h>
20#include <linux/seq_file.h>
0c2498f1 21
208be769 22#include <dt-bindings/pwm/pwm.h>
0c2498f1 23
1188829a
UKK
24#define CREATE_TRACE_POINTS
25#include <trace/events/pwm.h>
26
54c86dd2 27/* protects access to pwm_chips */
0c2498f1 28static DEFINE_MUTEX(pwm_lock);
e51b156b 29
54c86dd2 30static DEFINE_IDR(pwm_chips);
f051c466 31
62928315
UKK
32static void pwm_apply_debug(struct pwm_device *pwm,
33 const struct pwm_state *state)
8138d2dd 34{
62928315
UKK
35 struct pwm_state *last = &pwm->last;
36 struct pwm_chip *chip = pwm->chip;
37 struct pwm_state s1 = { 0 }, s2 = { 0 };
38 int err;
8138d2dd 39
62928315
UKK
40 if (!IS_ENABLED(CONFIG_PWM_DEBUG))
41 return;
8138d2dd 42
62928315
UKK
43 /* No reasonable diagnosis possible without .get_state() */
44 if (!chip->ops->get_state)
45 return;
8138d2dd 46
62928315
UKK
47 /*
48 * *state was just applied. Read out the hardware state and do some
49 * checks.
50 */
8138d2dd 51
62928315
UKK
52 err = chip->ops->get_state(chip, pwm, &s1);
53 trace_pwm_get(pwm, &s1, err);
54 if (err)
55 /* If that failed there isn't much to debug */
56 return;
8138d2dd 57
62928315
UKK
58 /*
59 * The lowlevel driver either ignored .polarity (which is a bug) or as
60 * best effort inverted .polarity and fixed .duty_cycle respectively.
61 * Undo this inversion and fixup for further tests.
62 */
63 if (s1.enabled && s1.polarity != state->polarity) {
64 s2.polarity = state->polarity;
65 s2.duty_cycle = s1.period - s1.duty_cycle;
66 s2.period = s1.period;
67 s2.enabled = s1.enabled;
68 } else {
69 s2 = s1;
70 }
f051c466 71
62928315
UKK
72 if (s2.polarity != state->polarity &&
73 state->duty_cycle < state->period)
4e59267c 74 dev_warn(pwmchip_parent(chip), ".apply ignored .polarity\n");
f051c466 75
62928315
UKK
76 if (state->enabled &&
77 last->polarity == state->polarity &&
78 last->period > s2.period &&
79 last->period <= state->period)
4e59267c 80 dev_warn(pwmchip_parent(chip),
62928315
UKK
81 ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
82 state->period, s2.period, last->period);
f051c466 83
62928315 84 if (state->enabled && state->period < s2.period)
4e59267c 85 dev_warn(pwmchip_parent(chip),
62928315
UKK
86 ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
87 state->period, s2.period);
f051c466 88
62928315
UKK
89 if (state->enabled &&
90 last->polarity == state->polarity &&
91 last->period == s2.period &&
92 last->duty_cycle > s2.duty_cycle &&
93 last->duty_cycle <= state->duty_cycle)
4e59267c 94 dev_warn(pwmchip_parent(chip),
62928315
UKK
95 ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
96 state->duty_cycle, state->period,
97 s2.duty_cycle, s2.period,
98 last->duty_cycle, last->period);
c73a3107 99
62928315 100 if (state->enabled && state->duty_cycle < s2.duty_cycle)
4e59267c 101 dev_warn(pwmchip_parent(chip),
62928315
UKK
102 ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
103 state->duty_cycle, state->period,
104 s2.duty_cycle, s2.period);
c73a3107 105
62928315 106 if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
4e59267c 107 dev_warn(pwmchip_parent(chip),
62928315 108 "requested disabled, but yielded enabled with duty > 0\n");
3ad1f3a3 109
62928315
UKK
110 /* reapply the state that the driver reported being configured. */
111 err = chip->ops->apply(chip, pwm, &s1);
112 trace_pwm_apply(pwm, &s1, err);
113 if (err) {
114 *last = s1;
4e59267c 115 dev_err(pwmchip_parent(chip), "failed to reapply current setting\n");
62928315 116 return;
1188829a 117 }
cfc4c189 118
62928315
UKK
119 *last = (struct pwm_state){ 0 };
120 err = chip->ops->get_state(chip, pwm, last);
121 trace_pwm_get(pwm, last, err);
122 if (err)
123 return;
f051c466 124
62928315
UKK
125 /* reapplication of the current state should give an exact match */
126 if (s1.enabled != last->enabled ||
127 s1.polarity != last->polarity ||
128 (s1.enabled && s1.period != last->period) ||
129 (s1.enabled && s1.duty_cycle != last->duty_cycle)) {
4e59267c 130 dev_err(pwmchip_parent(chip),
62928315
UKK
131 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
132 s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
133 last->enabled, last->polarity, last->duty_cycle,
134 last->period);
135 }
f051c466
TR
136}
137
62928315
UKK
138/**
139 * __pwm_apply() - atomically apply a new state to a PWM device
140 * @pwm: PWM device
141 * @state: new state to apply
142 */
143static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
83af2402 144{
62928315
UKK
145 struct pwm_chip *chip;
146 int err;
83af2402 147
62928315
UKK
148 if (!pwm || !state || !state->period ||
149 state->duty_cycle > state->period)
150 return -EINVAL;
42883cbc 151
62928315 152 chip = pwm->chip;
83af2402 153
62928315
UKK
154 if (state->period == pwm->state.period &&
155 state->duty_cycle == pwm->state.duty_cycle &&
156 state->polarity == pwm->state.polarity &&
157 state->enabled == pwm->state.enabled &&
158 state->usage_power == pwm->state.usage_power)
159 return 0;
83af2402 160
62928315
UKK
161 err = chip->ops->apply(chip, pwm, state);
162 trace_pwm_apply(pwm, state, err);
163 if (err)
164 return err;
83af2402 165
62928315
UKK
166 pwm->state = *state;
167
168 /*
169 * only do this after pwm->state was applied as some
170 * implementations of .get_state depend on this
171 */
172 pwm_apply_debug(pwm, state);
173
174 return 0;
83af2402
PA
175}
176
62928315
UKK
177/**
178 * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
179 * Cannot be used in atomic context.
180 * @pwm: PWM device
181 * @state: new state to apply
182 */
183int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
3ab7b6ac 184{
62928315 185 int err;
3ab7b6ac 186
62928315
UKK
187 /*
188 * Some lowlevel driver's implementations of .apply() make use of
189 * mutexes, also with some drivers only returning when the new
190 * configuration is active calling pwm_apply_might_sleep() from atomic context
191 * is a bad idea. So make it explicit that calling this function might
192 * sleep.
193 */
194 might_sleep();
3ab7b6ac 195
62928315
UKK
196 if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm->chip->atomic) {
197 /*
198 * Catch any drivers that have been marked as atomic but
199 * that will sleep anyway.
200 */
201 non_block_start();
202 err = __pwm_apply(pwm, state);
203 non_block_end();
204 } else {
205 err = __pwm_apply(pwm, state);
206 }
3ab7b6ac 207
62928315 208 return err;
3ab7b6ac 209}
62928315 210EXPORT_SYMBOL_GPL(pwm_apply_might_sleep);
3ab7b6ac 211
62928315
UKK
212/**
213 * pwm_apply_atomic() - apply a new state to a PWM device from atomic context
214 * Not all PWM devices support this function, check with pwm_might_sleep().
215 * @pwm: PWM device
216 * @state: new state to apply
217 */
218int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state)
7299ab70 219{
62928315
UKK
220 WARN_ONCE(!pwm->chip->atomic,
221 "sleeping PWM driver used in atomic context\n");
7299ab70 222
62928315 223 return __pwm_apply(pwm, state);
7299ab70 224}
62928315 225EXPORT_SYMBOL_GPL(pwm_apply_atomic);
7299ab70 226
62928315
UKK
227/**
228 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
229 * @pwm: PWM device
230 *
231 * This function will adjust the PWM config to the PWM arguments provided
232 * by the DT or PWM lookup table. This is particularly useful to adapt
233 * the bootloader config to the Linux one.
234 */
235int pwm_adjust_config(struct pwm_device *pwm)
7299ab70 236{
62928315
UKK
237 struct pwm_state state;
238 struct pwm_args pargs;
7299ab70 239
62928315
UKK
240 pwm_get_args(pwm, &pargs);
241 pwm_get_state(pwm, &state);
3ad1f3a3 242
62928315
UKK
243 /*
244 * If the current period is zero it means that either the PWM driver
245 * does not support initial state retrieval or the PWM has not yet
246 * been configured.
247 *
248 * In either case, we setup the new period and polarity, and assign a
249 * duty cycle of 0.
250 */
251 if (!state.period) {
252 state.duty_cycle = 0;
253 state.period = pargs.period;
254 state.polarity = pargs.polarity;
3ad1f3a3 255
62928315
UKK
256 return pwm_apply_might_sleep(pwm, &state);
257 }
5ec803ed 258
62928315
UKK
259 /*
260 * Adjust the PWM duty cycle/period based on the period value provided
261 * in PWM args.
262 */
263 if (pargs.period != state.period) {
264 u64 dutycycle = (u64)state.duty_cycle * pargs.period;
265
266 do_div(dutycycle, state.period);
267 state.duty_cycle = dutycycle;
268 state.period = pargs.period;
269 }
270
271 /*
272 * If the polarity changed, we should also change the duty cycle.
273 */
274 if (pargs.polarity != state.polarity) {
275 state.polarity = pargs.polarity;
276 state.duty_cycle = state.period - state.duty_cycle;
277 }
278
279 return pwm_apply_might_sleep(pwm, &state);
5ec803ed 280}
62928315 281EXPORT_SYMBOL_GPL(pwm_adjust_config);
5ec803ed 282
0c2498f1 283/**
62928315
UKK
284 * pwm_capture() - capture and report a PWM signal
285 * @pwm: PWM device
286 * @result: structure to fill with capture result
287 * @timeout: time to wait, in milliseconds, before giving up on capture
04883802
TR
288 *
289 * Returns: 0 on success or a negative error code on failure.
0c2498f1 290 */
62928315
UKK
291int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
292 unsigned long timeout)
0c2498f1 293{
62928315 294 int err;
0c2498f1 295
62928315 296 if (!pwm || !pwm->chip->ops)
5ec803ed
BB
297 return -EINVAL;
298
62928315
UKK
299 if (!pwm->chip->ops->capture)
300 return -ENOSYS;
0c2498f1 301
62928315
UKK
302 mutex_lock(&pwm_lock);
303 err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
304 mutex_unlock(&pwm_lock);
384461ab 305
62928315
UKK
306 return err;
307}
308EXPORT_SYMBOL_GPL(pwm_capture);
c8135b51 309
62928315
UKK
310static struct pwm_chip *pwmchip_find_by_name(const char *name)
311{
312 struct pwm_chip *chip;
313 unsigned long id, tmp;
0c2498f1 314
62928315
UKK
315 if (!name)
316 return NULL;
f051c466 317
62928315 318 mutex_lock(&pwm_lock);
f9a8ee8c 319
62928315 320 idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) {
4e59267c 321 const char *chip_name = dev_name(pwmchip_parent(chip));
f051c466 322
62928315
UKK
323 if (chip_name && strcmp(chip_name, name) == 0) {
324 mutex_unlock(&pwm_lock);
325 return chip;
326 }
f051c466
TR
327 }
328
c8135b51 329 mutex_unlock(&pwm_lock);
f051c466 330
62928315 331 return NULL;
0c2498f1 332}
0c2498f1 333
62928315 334static int pwm_device_request(struct pwm_device *pwm, const char *label)
0c2498f1 335{
62928315
UKK
336 int err;
337 struct pwm_chip *chip = pwm->chip;
338 const struct pwm_ops *ops = chip->ops;
86eed2a1 339
62928315
UKK
340 if (test_bit(PWMF_REQUESTED, &pwm->flags))
341 return -EBUSY;
0c2498f1 342
62928315
UKK
343 if (!try_module_get(chip->owner))
344 return -ENODEV;
0c2498f1 345
62928315
UKK
346 if (ops->request) {
347 err = ops->request(chip, pwm);
348 if (err) {
349 module_put(chip->owner);
350 return err;
351 }
352 }
54c86dd2 353
62928315
UKK
354 if (ops->get_state) {
355 /*
356 * Zero-initialize state because most drivers are unaware of
357 * .usage_power. The other members of state are supposed to be
358 * set by lowlevel drivers. We still initialize the whole
359 * structure for simplicity even though this might paper over
360 * faulty implementations of .get_state().
361 */
362 struct pwm_state state = { 0, };
0c2498f1 363
62928315
UKK
364 err = ops->get_state(chip, pwm, &state);
365 trace_pwm_get(pwm, &state, err);
bcda91bf 366
62928315
UKK
367 if (!err)
368 pwm->state = state;
bcda91bf 369
62928315
UKK
370 if (IS_ENABLED(CONFIG_PWM_DEBUG))
371 pwm->last = pwm->state;
372 }
bcda91bf 373
62928315
UKK
374 set_bit(PWMF_REQUESTED, &pwm->flags);
375 pwm->label = label;
bcda91bf 376
62928315 377 return 0;
bcda91bf 378}
bcda91bf 379
f051c466
TR
380/**
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
384 * @label: a literal description string of this PWM
385 *
04883802
TR
386 * Returns: A pointer to the PWM device at the given index of the given PWM
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.
f051c466
TR
389 */
390struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
391 unsigned int index,
392 const char *label)
393{
394 struct pwm_device *pwm;
395 int err;
0c2498f1 396
f051c466
TR
397 if (!chip || index >= chip->npwm)
398 return ERR_PTR(-EINVAL);
0c2498f1 399
f051c466
TR
400 mutex_lock(&pwm_lock);
401 pwm = &chip->pwms[index];
0c2498f1 402
f051c466
TR
403 err = pwm_device_request(pwm, label);
404 if (err < 0)
405 pwm = ERR_PTR(err);
406
407 mutex_unlock(&pwm_lock);
0c2498f1
SH
408 return pwm;
409}
f051c466 410EXPORT_SYMBOL_GPL(pwm_request_from_chip);
0c2498f1 411
3ad1f3a3 412
62928315
UKK
413struct pwm_device *
414of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args)
415{
416 struct pwm_device *pwm;
3ad1f3a3 417
62928315
UKK
418 /* period in the second cell and flags in the third cell are optional */
419 if (args->args_count < 1)
420 return ERR_PTR(-EINVAL);
3ad1f3a3 421
62928315
UKK
422 pwm = pwm_request_from_chip(chip, args->args[0], NULL);
423 if (IS_ERR(pwm))
424 return pwm;
76abbdde 425
62928315
UKK
426 if (args->args_count > 1)
427 pwm->args.period = args->args[1];
f051c466 428
62928315
UKK
429 pwm->args.polarity = PWM_POLARITY_NORMAL;
430 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
431 pwm->args.polarity = PWM_POLARITY_INVERSED;
fc3c5512 432
62928315
UKK
433 return pwm;
434}
435EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
76abbdde 436
62928315
UKK
437struct pwm_device *
438of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args)
439{
440 struct pwm_device *pwm;
5ec803ed 441
62928315
UKK
442 pwm = pwm_request_from_chip(chip, 0, NULL);
443 if (IS_ERR(pwm))
444 return pwm;
5ec803ed 445
73dfe970 446 if (args->args_count > 0)
62928315 447 pwm->args.period = args->args[0];
0aa0869c 448
62928315
UKK
449 pwm->args.polarity = PWM_POLARITY_NORMAL;
450 if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED)
451 pwm->args.polarity = PWM_POLARITY_INVERSED;
452
453 return pwm;
0aa0869c 454}
62928315 455EXPORT_SYMBOL_GPL(of_pwm_single_xlate);
7170d3be 456
e9cc807f
UKK
457struct pwm_export {
458 struct device pwm_dev;
459 struct pwm_device *pwm;
460 struct mutex lock;
461 struct pwm_state suspend;
462};
463
464static inline struct pwm_chip *pwmchip_from_dev(struct device *pwmchip_dev)
465{
466 return dev_get_drvdata(pwmchip_dev);
467}
468
469static inline struct pwm_export *pwmexport_from_dev(struct device *pwm_dev)
470{
471 return container_of(pwm_dev, struct pwm_export, pwm_dev);
472}
473
474static inline struct pwm_device *pwm_from_dev(struct device *pwm_dev)
475{
476 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
477
478 return export->pwm;
479}
480
481static ssize_t period_show(struct device *pwm_dev,
482 struct device_attribute *attr,
483 char *buf)
484{
485 const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
486 struct pwm_state state;
487
488 pwm_get_state(pwm, &state);
489
490 return sysfs_emit(buf, "%llu\n", state.period);
491}
492
493static ssize_t period_store(struct device *pwm_dev,
494 struct device_attribute *attr,
495 const char *buf, size_t size)
496{
497 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
498 struct pwm_device *pwm = export->pwm;
499 struct pwm_state state;
500 u64 val;
501 int ret;
502
503 ret = kstrtou64(buf, 0, &val);
504 if (ret)
505 return ret;
506
507 mutex_lock(&export->lock);
508 pwm_get_state(pwm, &state);
509 state.period = val;
510 ret = pwm_apply_might_sleep(pwm, &state);
511 mutex_unlock(&export->lock);
512
513 return ret ? : size;
514}
515
516static ssize_t duty_cycle_show(struct device *pwm_dev,
517 struct device_attribute *attr,
518 char *buf)
519{
520 const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
521 struct pwm_state state;
522
523 pwm_get_state(pwm, &state);
524
525 return sysfs_emit(buf, "%llu\n", state.duty_cycle);
526}
527
528static ssize_t duty_cycle_store(struct device *pwm_dev,
529 struct device_attribute *attr,
530 const char *buf, size_t size)
531{
532 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
533 struct pwm_device *pwm = export->pwm;
534 struct pwm_state state;
535 u64 val;
536 int ret;
537
538 ret = kstrtou64(buf, 0, &val);
539 if (ret)
540 return ret;
541
542 mutex_lock(&export->lock);
543 pwm_get_state(pwm, &state);
544 state.duty_cycle = val;
545 ret = pwm_apply_might_sleep(pwm, &state);
546 mutex_unlock(&export->lock);
547
548 return ret ? : size;
549}
550
551static ssize_t enable_show(struct device *pwm_dev,
552 struct device_attribute *attr,
553 char *buf)
554{
555 const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
556 struct pwm_state state;
557
558 pwm_get_state(pwm, &state);
559
560 return sysfs_emit(buf, "%d\n", state.enabled);
561}
562
563static ssize_t enable_store(struct device *pwm_dev,
564 struct device_attribute *attr,
565 const char *buf, size_t size)
566{
567 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
568 struct pwm_device *pwm = export->pwm;
569 struct pwm_state state;
570 int val, ret;
571
572 ret = kstrtoint(buf, 0, &val);
573 if (ret)
574 return ret;
575
576 mutex_lock(&export->lock);
577
578 pwm_get_state(pwm, &state);
579
580 switch (val) {
581 case 0:
582 state.enabled = false;
583 break;
584 case 1:
585 state.enabled = true;
586 break;
587 default:
588 ret = -EINVAL;
589 goto unlock;
590 }
591
592 ret = pwm_apply_might_sleep(pwm, &state);
593
594unlock:
595 mutex_unlock(&export->lock);
596 return ret ? : size;
597}
598
599static ssize_t polarity_show(struct device *pwm_dev,
600 struct device_attribute *attr,
601 char *buf)
602{
603 const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
604 const char *polarity = "unknown";
605 struct pwm_state state;
606
607 pwm_get_state(pwm, &state);
608
609 switch (state.polarity) {
610 case PWM_POLARITY_NORMAL:
611 polarity = "normal";
612 break;
613
614 case PWM_POLARITY_INVERSED:
615 polarity = "inversed";
616 break;
617 }
618
619 return sysfs_emit(buf, "%s\n", polarity);
620}
621
622static ssize_t polarity_store(struct device *pwm_dev,
623 struct device_attribute *attr,
624 const char *buf, size_t size)
625{
626 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
627 struct pwm_device *pwm = export->pwm;
628 enum pwm_polarity polarity;
629 struct pwm_state state;
630 int ret;
631
632 if (sysfs_streq(buf, "normal"))
633 polarity = PWM_POLARITY_NORMAL;
634 else if (sysfs_streq(buf, "inversed"))
635 polarity = PWM_POLARITY_INVERSED;
636 else
637 return -EINVAL;
638
639 mutex_lock(&export->lock);
640 pwm_get_state(pwm, &state);
641 state.polarity = polarity;
642 ret = pwm_apply_might_sleep(pwm, &state);
643 mutex_unlock(&export->lock);
644
645 return ret ? : size;
646}
647
648static ssize_t capture_show(struct device *pwm_dev,
649 struct device_attribute *attr,
650 char *buf)
651{
652 struct pwm_device *pwm = pwm_from_dev(pwm_dev);
653 struct pwm_capture result;
654 int ret;
655
656 ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ));
657 if (ret)
658 return ret;
659
660 return sysfs_emit(buf, "%u %u\n", result.period, result.duty_cycle);
661}
662
663static DEVICE_ATTR_RW(period);
664static DEVICE_ATTR_RW(duty_cycle);
665static DEVICE_ATTR_RW(enable);
666static DEVICE_ATTR_RW(polarity);
667static DEVICE_ATTR_RO(capture);
668
669static struct attribute *pwm_attrs[] = {
670 &dev_attr_period.attr,
671 &dev_attr_duty_cycle.attr,
672 &dev_attr_enable.attr,
673 &dev_attr_polarity.attr,
674 &dev_attr_capture.attr,
675 NULL
676};
677ATTRIBUTE_GROUPS(pwm);
678
679static void pwm_export_release(struct device *pwm_dev)
680{
681 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
682
683 kfree(export);
684}
685
686static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm)
687{
688 struct pwm_export *export;
689 char *pwm_prop[2];
690 int ret;
691
692 if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
693 return -EBUSY;
694
695 export = kzalloc(sizeof(*export), GFP_KERNEL);
696 if (!export) {
697 clear_bit(PWMF_EXPORTED, &pwm->flags);
698 return -ENOMEM;
699 }
700
701 export->pwm = pwm;
702 mutex_init(&export->lock);
703
704 export->pwm_dev.release = pwm_export_release;
705 export->pwm_dev.parent = pwmchip_dev;
706 export->pwm_dev.devt = MKDEV(0, 0);
707 export->pwm_dev.groups = pwm_groups;
708 dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm);
709
710 ret = device_register(&export->pwm_dev);
711 if (ret) {
712 clear_bit(PWMF_EXPORTED, &pwm->flags);
713 put_device(&export->pwm_dev);
714 export = NULL;
715 return ret;
716 }
717 pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm);
718 pwm_prop[1] = NULL;
719 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
720 kfree(pwm_prop[0]);
721
722 return 0;
723}
724
725static int pwm_unexport_match(struct device *pwm_dev, void *data)
726{
727 return pwm_from_dev(pwm_dev) == data;
728}
729
730static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm)
731{
732 struct device *pwm_dev;
733 char *pwm_prop[2];
734
735 if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
736 return -ENODEV;
737
738 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
739 if (!pwm_dev)
740 return -ENODEV;
741
742 pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm);
743 pwm_prop[1] = NULL;
744 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
745 kfree(pwm_prop[0]);
746
747 /* for device_find_child() */
748 put_device(pwm_dev);
749 device_unregister(pwm_dev);
750 pwm_put(pwm);
751
752 return 0;
753}
754
755static ssize_t export_store(struct device *pwmchip_dev,
756 struct device_attribute *attr,
757 const char *buf, size_t len)
758{
759 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
760 struct pwm_device *pwm;
761 unsigned int hwpwm;
762 int ret;
763
764 ret = kstrtouint(buf, 0, &hwpwm);
765 if (ret < 0)
766 return ret;
767
768 if (hwpwm >= chip->npwm)
769 return -ENODEV;
770
771 pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
772 if (IS_ERR(pwm))
773 return PTR_ERR(pwm);
774
775 ret = pwm_export_child(pwmchip_dev, pwm);
776 if (ret < 0)
777 pwm_put(pwm);
778
779 return ret ? : len;
780}
781static DEVICE_ATTR_WO(export);
782
783static ssize_t unexport_store(struct device *pwmchip_dev,
784 struct device_attribute *attr,
785 const char *buf, size_t len)
786{
787 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
788 unsigned int hwpwm;
789 int ret;
790
791 ret = kstrtouint(buf, 0, &hwpwm);
792 if (ret < 0)
793 return ret;
794
795 if (hwpwm >= chip->npwm)
796 return -ENODEV;
797
798 ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]);
799
800 return ret ? : len;
801}
802static DEVICE_ATTR_WO(unexport);
803
804static ssize_t npwm_show(struct device *pwmchip_dev, struct device_attribute *attr,
805 char *buf)
806{
807 const struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
808
809 return sysfs_emit(buf, "%u\n", chip->npwm);
810}
811static DEVICE_ATTR_RO(npwm);
812
813static struct attribute *pwm_chip_attrs[] = {
814 &dev_attr_export.attr,
815 &dev_attr_unexport.attr,
816 &dev_attr_npwm.attr,
817 NULL,
818};
819ATTRIBUTE_GROUPS(pwm_chip);
820
821/* takes export->lock on success */
822static struct pwm_export *pwm_class_get_state(struct device *pwmchip_dev,
823 struct pwm_device *pwm,
824 struct pwm_state *state)
825{
826 struct device *pwm_dev;
827 struct pwm_export *export;
828
829 if (!test_bit(PWMF_EXPORTED, &pwm->flags))
830 return NULL;
831
832 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
833 if (!pwm_dev)
834 return NULL;
835
836 export = pwmexport_from_dev(pwm_dev);
837 put_device(pwm_dev); /* for device_find_child() */
838
839 mutex_lock(&export->lock);
840 pwm_get_state(pwm, state);
841
842 return export;
843}
844
845static int pwm_class_apply_state(struct pwm_export *export,
846 struct pwm_device *pwm,
847 struct pwm_state *state)
848{
849 int ret = pwm_apply_might_sleep(pwm, state);
850
851 /* release lock taken in pwm_class_get_state */
852 mutex_unlock(&export->lock);
853
854 return ret;
855}
856
857static int pwm_class_resume_npwm(struct device *pwmchip_dev, unsigned int npwm)
858{
859 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
860 unsigned int i;
861 int ret = 0;
862
863 for (i = 0; i < npwm; i++) {
864 struct pwm_device *pwm = &chip->pwms[i];
865 struct pwm_state state;
866 struct pwm_export *export;
867
868 export = pwm_class_get_state(pwmchip_dev, pwm, &state);
869 if (!export)
870 continue;
871
872 /* If pwmchip was not enabled before suspend, do nothing. */
873 if (!export->suspend.enabled) {
874 /* release lock taken in pwm_class_get_state */
875 mutex_unlock(&export->lock);
876 continue;
877 }
878
879 state.enabled = export->suspend.enabled;
880 ret = pwm_class_apply_state(export, pwm, &state);
881 if (ret < 0)
882 break;
883 }
884
885 return ret;
886}
887
888static int pwm_class_suspend(struct device *pwmchip_dev)
889{
890 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
891 unsigned int i;
892 int ret = 0;
893
894 for (i = 0; i < chip->npwm; i++) {
895 struct pwm_device *pwm = &chip->pwms[i];
896 struct pwm_state state;
897 struct pwm_export *export;
898
899 export = pwm_class_get_state(pwmchip_dev, pwm, &state);
900 if (!export)
901 continue;
902
903 /*
904 * If pwmchip was not enabled before suspend, save
905 * state for resume time and do nothing else.
906 */
907 export->suspend = state;
908 if (!state.enabled) {
909 /* release lock taken in pwm_class_get_state */
910 mutex_unlock(&export->lock);
911 continue;
912 }
913
914 state.enabled = false;
915 ret = pwm_class_apply_state(export, pwm, &state);
916 if (ret < 0) {
917 /*
918 * roll back the PWM devices that were disabled by
919 * this suspend function.
920 */
921 pwm_class_resume_npwm(pwmchip_dev, i);
922 break;
923 }
924 }
925
926 return ret;
927}
928
929static int pwm_class_resume(struct device *pwmchip_dev)
930{
931 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
932
933 return pwm_class_resume_npwm(pwmchip_dev, chip->npwm);
934}
935
936static DEFINE_SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume);
937
938static struct class pwm_class = {
939 .name = "pwm",
940 .dev_groups = pwm_chip_groups,
941 .pm = pm_sleep_ptr(&pwm_class_pm_ops),
942};
943
944static int pwmchip_sysfs_match(struct device *pwmchip_dev, const void *data)
945{
946 return pwmchip_from_dev(pwmchip_dev) == data;
947}
948
949static void pwmchip_sysfs_export(struct pwm_chip *chip)
950{
951 struct device *pwmchip_dev;
952
953 /*
954 * If device_create() fails the pwm_chip is still usable by
955 * the kernel it's just not exported.
956 */
957 pwmchip_dev = device_create(&pwm_class, pwmchip_parent(chip), MKDEV(0, 0), chip,
958 "pwmchip%d", chip->id);
959 if (IS_ERR(pwmchip_dev)) {
960 dev_warn(pwmchip_parent(chip),
961 "device_create failed for pwm_chip sysfs export\n");
962 }
963}
964
965static void pwmchip_sysfs_unexport(struct pwm_chip *chip)
966{
967 struct device *pwmchip_dev;
968 unsigned int i;
969
970 pwmchip_dev = class_find_device(&pwm_class, NULL, chip,
971 pwmchip_sysfs_match);
972 if (!pwmchip_dev)
973 return;
974
975 for (i = 0; i < chip->npwm; i++) {
976 struct pwm_device *pwm = &chip->pwms[i];
977
978 if (test_bit(PWMF_EXPORTED, &pwm->flags))
979 pwm_unexport_child(pwmchip_dev, pwm);
980 }
981
982 put_device(pwmchip_dev);
983 device_unregister(pwmchip_dev);
984}
985
024913db
UKK
986#define PWMCHIP_ALIGN ARCH_DMA_MINALIGN
987
988static void *pwmchip_priv(struct pwm_chip *chip)
989{
ee37bf50 990 return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN);
024913db
UKK
991}
992
993/* This is the counterpart to pwmchip_alloc() */
994void pwmchip_put(struct pwm_chip *chip)
995{
996 kfree(chip);
997}
998EXPORT_SYMBOL_GPL(pwmchip_put);
999
1000struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
1001{
1002 struct pwm_chip *chip;
1003 size_t alloc_size;
ee37bf50 1004 unsigned int i;
024913db 1005
ee37bf50
UKK
1006 alloc_size = size_add(ALIGN(struct_size(chip, pwms, npwm), PWMCHIP_ALIGN),
1007 sizeof_priv);
024913db
UKK
1008
1009 chip = kzalloc(alloc_size, GFP_KERNEL);
1010 if (!chip)
1011 return ERR_PTR(-ENOMEM);
1012
1013 chip->dev = parent;
1014 chip->npwm = npwm;
05947224 1015 chip->uses_pwmchip_alloc = true;
024913db
UKK
1016
1017 pwmchip_set_drvdata(chip, pwmchip_priv(chip));
1018
ee37bf50
UKK
1019 for (i = 0; i < chip->npwm; i++) {
1020 struct pwm_device *pwm = &chip->pwms[i];
1021 pwm->chip = chip;
1022 pwm->hwpwm = i;
1023 }
1024
024913db
UKK
1025 return chip;
1026}
1027EXPORT_SYMBOL_GPL(pwmchip_alloc);
1028
1029static void devm_pwmchip_put(void *data)
1030{
1031 struct pwm_chip *chip = data;
1032
1033 pwmchip_put(chip);
1034}
1035
1036struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
1037{
1038 struct pwm_chip *chip;
1039 int ret;
1040
1041 chip = pwmchip_alloc(parent, npwm, sizeof_priv);
1042 if (IS_ERR(chip))
1043 return chip;
1044
1045 ret = devm_add_action_or_reset(parent, devm_pwmchip_put, chip);
1046 if (ret)
1047 return ERR_PTR(ret);
1048
1049 return chip;
1050}
1051EXPORT_SYMBOL_GPL(devm_pwmchip_alloc);
1052
62928315 1053static void of_pwmchip_add(struct pwm_chip *chip)
7170d3be 1054{
4e59267c 1055 if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node)
62928315 1056 return;
7170d3be 1057
62928315
UKK
1058 if (!chip->of_xlate)
1059 chip->of_xlate = of_pwm_xlate_with_flags;
7170d3be 1060
4e59267c 1061 of_node_get(pwmchip_parent(chip)->of_node);
62928315 1062}
7170d3be 1063
62928315
UKK
1064static void of_pwmchip_remove(struct pwm_chip *chip)
1065{
4e59267c
UKK
1066 if (pwmchip_parent(chip))
1067 of_node_put(pwmchip_parent(chip)->of_node);
7170d3be 1068}
0aa0869c 1069
62928315 1070static bool pwm_ops_check(const struct pwm_chip *chip)
7170d3be 1071{
62928315 1072 const struct pwm_ops *ops = chip->ops;
7170d3be 1073
62928315
UKK
1074 if (!ops->apply)
1075 return false;
1076
1077 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
4e59267c 1078 dev_warn(pwmchip_parent(chip),
62928315
UKK
1079 "Please implement the .get_state() callback\n");
1080
1081 return true;
7170d3be 1082}
7170d3be 1083
3a3d1a4e 1084/**
62928315
UKK
1085 * __pwmchip_add() - register a new PWM chip
1086 * @chip: the PWM chip to add
1087 * @owner: reference to the module providing the chip.
1088 *
1089 * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
1090 * pwmchip_add wrapper to do this right.
3a3d1a4e
LJ
1091 *
1092 * Returns: 0 on success or a negative error code on failure.
1093 */
62928315 1094int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
3a3d1a4e 1095{
62928315 1096 int ret;
3a3d1a4e 1097
4e59267c 1098 if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm)
3a3d1a4e
LJ
1099 return -EINVAL;
1100
05947224
UKK
1101 /*
1102 * a struct pwm_chip must be allocated using (devm_)pwmchip_alloc,
1103 * otherwise the embedded struct device might disappear too early
1104 * resulting in memory corruption.
1105 * Catch drivers that were not converted appropriately.
1106 */
1107 if (!chip->uses_pwmchip_alloc)
1108 return -EINVAL;
1109
62928315
UKK
1110 if (!pwm_ops_check(chip))
1111 return -EINVAL;
1112
1113 chip->owner = owner;
1114
3a3d1a4e 1115 mutex_lock(&pwm_lock);
62928315
UKK
1116
1117 ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL);
1118 if (ret < 0) {
1119 mutex_unlock(&pwm_lock);
62928315
UKK
1120 return ret;
1121 }
1122
1123 chip->id = ret;
1124
3a3d1a4e
LJ
1125 mutex_unlock(&pwm_lock);
1126
62928315
UKK
1127 if (IS_ENABLED(CONFIG_OF))
1128 of_pwmchip_add(chip);
1129
1130 pwmchip_sysfs_export(chip);
1131
1132 return 0;
3a3d1a4e 1133}
62928315 1134EXPORT_SYMBOL_GPL(__pwmchip_add);
3a3d1a4e 1135
0c2498f1 1136/**
62928315
UKK
1137 * pwmchip_remove() - remove a PWM chip
1138 * @chip: the PWM chip to remove
04883802 1139 *
62928315 1140 * Removes a PWM chip.
0c2498f1 1141 */
62928315 1142void pwmchip_remove(struct pwm_chip *chip)
0c2498f1 1143{
62928315 1144 pwmchip_sysfs_unexport(chip);
d1cd2142 1145
62928315
UKK
1146 if (IS_ENABLED(CONFIG_OF))
1147 of_pwmchip_remove(chip);
d1cd2142 1148
62928315 1149 mutex_lock(&pwm_lock);
0c2498f1 1150
62928315 1151 idr_remove(&pwm_chips, chip->id);
0c2498f1 1152
62928315 1153 mutex_unlock(&pwm_lock);
0c2498f1 1154}
62928315 1155EXPORT_SYMBOL_GPL(pwmchip_remove);
62099abf 1156
62928315 1157static void devm_pwmchip_remove(void *data)
7299ab70 1158{
62928315 1159 struct pwm_chip *chip = data;
7299ab70 1160
62928315
UKK
1161 pwmchip_remove(chip);
1162}
7299ab70 1163
62928315
UKK
1164int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner)
1165{
1166 int ret;
7299ab70 1167
62928315
UKK
1168 ret = __pwmchip_add(chip, owner);
1169 if (ret)
1170 return ret;
7299ab70 1171
62928315 1172 return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
7299ab70 1173}
62928315 1174EXPORT_SYMBOL_GPL(__devm_pwmchip_add);
7299ab70 1175
b2c200e3
FG
1176static struct device_link *pwm_device_link_add(struct device *dev,
1177 struct pwm_device *pwm)
1178{
1179 struct device_link *dl;
1180
1181 if (!dev) {
1182 /*
1183 * No device for the PWM consumer has been provided. It may
1184 * impact the PM sequence ordering: the PWM supplier may get
1185 * suspended before the consumer.
1186 */
4e59267c 1187 dev_warn(pwmchip_parent(pwm->chip),
b2c200e3
FG
1188 "No consumer device specified to create a link to\n");
1189 return NULL;
1190 }
1191
4e59267c 1192 dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER);
b2c200e3
FG
1193 if (!dl) {
1194 dev_err(dev, "failed to create device link to %s\n",
4e59267c 1195 dev_name(pwmchip_parent(pwm->chip)));
b2c200e3
FG
1196 return ERR_PTR(-EINVAL);
1197 }
1198
1199 return dl;
1200}
1201
62928315
UKK
1202static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
1203{
1204 struct pwm_chip *chip;
1205 unsigned long id, tmp;
1206
1207 mutex_lock(&pwm_lock);
1208
1209 idr_for_each_entry_ul(&pwm_chips, chip, tmp, id)
4e59267c 1210 if (pwmchip_parent(chip) && device_match_fwnode(pwmchip_parent(chip), fwnode)) {
62928315
UKK
1211 mutex_unlock(&pwm_lock);
1212 return chip;
1213 }
1214
1215 mutex_unlock(&pwm_lock);
1216
1217 return ERR_PTR(-EPROBE_DEFER);
1218}
1219
7299ab70 1220/**
8eb96127 1221 * of_pwm_get() - request a PWM via the PWM framework
b2c200e3 1222 * @dev: device for PWM consumer
7299ab70
TR
1223 * @np: device node to get the PWM from
1224 * @con_id: consumer name
1225 *
1226 * Returns the PWM device parsed from the phandle and index specified in the
1227 * "pwms" property of a device tree node or a negative error-code on failure.
1228 * Values parsed from the device tree are stored in the returned PWM device
1229 * object.
1230 *
1231 * If con_id is NULL, the first PWM device listed in the "pwms" property will
1232 * be requested. Otherwise the "pwm-names" property is used to do a reverse
1233 * lookup of the PWM index. This also means that the "pwm-names" property
1234 * becomes mandatory for devices that look up the PWM device via the con_id
1235 * parameter.
04883802
TR
1236 *
1237 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1238 * error code on failure.
7299ab70 1239 */
b5ae0ad5
AS
1240static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
1241 const char *con_id)
7299ab70
TR
1242{
1243 struct pwm_device *pwm = NULL;
1244 struct of_phandle_args args;
b2c200e3 1245 struct device_link *dl;
b4f78ff7 1246 struct pwm_chip *chip;
7299ab70
TR
1247 int index = 0;
1248 int err;
1249
1250 if (con_id) {
1251 index = of_property_match_string(np, "pwm-names", con_id);
1252 if (index < 0)
1253 return ERR_PTR(index);
1254 }
1255
1256 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
1257 &args);
1258 if (err) {
f2dafc09 1259 pr_err("%s(): can't parse \"pwms\" property\n", __func__);
7299ab70
TR
1260 return ERR_PTR(err);
1261 }
1262
b4f78ff7
UKK
1263 chip = fwnode_to_pwmchip(of_fwnode_handle(args.np));
1264 if (IS_ERR(chip)) {
1265 if (PTR_ERR(chip) != -EPROBE_DEFER)
93c292ef
JB
1266 pr_err("%s(): PWM chip not found\n", __func__);
1267
b4f78ff7 1268 pwm = ERR_CAST(chip);
7299ab70
TR
1269 goto put;
1270 }
1271
b4f78ff7 1272 pwm = chip->of_xlate(chip, &args);
7299ab70
TR
1273 if (IS_ERR(pwm))
1274 goto put;
1275
b2c200e3
FG
1276 dl = pwm_device_link_add(dev, pwm);
1277 if (IS_ERR(dl)) {
1278 /* of_xlate ended up calling pwm_request_from_chip() */
0af4d704 1279 pwm_put(pwm);
b2c200e3
FG
1280 pwm = ERR_CAST(dl);
1281 goto put;
1282 }
1283
7299ab70
TR
1284 /*
1285 * If a consumer name was not given, try to look it up from the
1286 * "pwm-names" property if it exists. Otherwise use the name of
1287 * the user device node.
1288 */
1289 if (!con_id) {
1290 err = of_property_read_string_index(np, "pwm-names", index,
1291 &con_id);
1292 if (err < 0)
1293 con_id = np->name;
1294 }
1295
1296 pwm->label = con_id;
1297
1298put:
1299 of_node_put(args.np);
1300
1301 return pwm;
1302}
1303
4a6ef8e3
NV
1304/**
1305 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
e625fb70 1306 * @fwnode: firmware node to get the "pwms" property from
4a6ef8e3
NV
1307 *
1308 * Returns the PWM device parsed from the fwnode and index specified in the
1309 * "pwms" property or a negative error-code on failure.
1310 * Values parsed from the device tree are stored in the returned PWM device
1311 * object.
1312 *
1313 * This is analogous to of_pwm_get() except con_id is not yet supported.
1314 * ACPI entries must look like
1315 * Package () {"pwms", Package ()
1316 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
1317 *
1318 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1319 * error code on failure.
1320 */
e625fb70 1321static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode)
4a6ef8e3 1322{
bebedf2b 1323 struct pwm_device *pwm;
4a6ef8e3 1324 struct fwnode_reference_args args;
4a6ef8e3
NV
1325 struct pwm_chip *chip;
1326 int ret;
1327
1328 memset(&args, 0, sizeof(args));
1329
1330 ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
1331 if (ret < 0)
1332 return ERR_PTR(ret);
1333
4a6ef8e3
NV
1334 if (args.nargs < 2)
1335 return ERR_PTR(-EPROTO);
1336
e5c38ba9 1337 chip = fwnode_to_pwmchip(args.fwnode);
4a6ef8e3
NV
1338 if (IS_ERR(chip))
1339 return ERR_CAST(chip);
1340
1341 pwm = pwm_request_from_chip(chip, args.args[0], NULL);
1342 if (IS_ERR(pwm))
1343 return pwm;
1344
1345 pwm->args.period = args.args[1];
1346 pwm->args.polarity = PWM_POLARITY_NORMAL;
1347
1348 if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
1349 pwm->args.polarity = PWM_POLARITY_INVERSED;
4a6ef8e3
NV
1350
1351 return pwm;
1352}
1353
62928315
UKK
1354static DEFINE_MUTEX(pwm_lookup_lock);
1355static LIST_HEAD(pwm_lookup_list);
1356
8138d2dd
TR
1357/**
1358 * pwm_add_table() - register PWM device consumers
1359 * @table: array of consumers to register
1360 * @num: number of consumers in table
1361 */
c264f111 1362void pwm_add_table(struct pwm_lookup *table, size_t num)
8138d2dd
TR
1363{
1364 mutex_lock(&pwm_lookup_lock);
1365
1366 while (num--) {
1367 list_add_tail(&table->list, &pwm_lookup_list);
1368 table++;
1369 }
1370
1371 mutex_unlock(&pwm_lookup_lock);
1372}
1373
efb0de55
SK
1374/**
1375 * pwm_remove_table() - unregister PWM device consumers
1376 * @table: array of consumers to unregister
1377 * @num: number of consumers in table
1378 */
1379void pwm_remove_table(struct pwm_lookup *table, size_t num)
1380{
1381 mutex_lock(&pwm_lookup_lock);
1382
1383 while (num--) {
1384 list_del(&table->list);
1385 table++;
1386 }
1387
1388 mutex_unlock(&pwm_lookup_lock);
1389}
1390
8138d2dd
TR
1391/**
1392 * pwm_get() - look up and request a PWM device
1393 * @dev: device for PWM consumer
1394 * @con_id: consumer name
1395 *
7299ab70
TR
1396 * Lookup is first attempted using DT. If the device was not instantiated from
1397 * a device tree, a PWM chip and a relative index is looked up via a table
1398 * supplied by board setup code (see pwm_add_table()).
8138d2dd
TR
1399 *
1400 * Once a PWM chip has been found the specified PWM device will be requested
1401 * and is ready to be used.
04883802
TR
1402 *
1403 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1404 * error code on failure.
8138d2dd
TR
1405 */
1406struct pwm_device *pwm_get(struct device *dev, const char *con_id)
1407{
e625fb70 1408 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
e50d3523 1409 const char *dev_id = dev ? dev_name(dev) : NULL;
69efb343
HG
1410 struct pwm_device *pwm;
1411 struct pwm_chip *chip;
b2c200e3 1412 struct device_link *dl;
8138d2dd 1413 unsigned int best = 0;
70145f87 1414 struct pwm_lookup *p, *chosen = NULL;
8138d2dd 1415 unsigned int match;
b526a314 1416 int err;
8138d2dd 1417
7299ab70 1418 /* look up via DT first */
e625fb70
AS
1419 if (is_of_node(fwnode))
1420 return of_pwm_get(dev, to_of_node(fwnode), con_id);
7299ab70 1421
4a6ef8e3 1422 /* then lookup via ACPI */
e625fb70
AS
1423 if (is_acpi_node(fwnode)) {
1424 pwm = acpi_pwm_get(fwnode);
6cf9481b
HG
1425 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
1426 return pwm;
1427 }
7299ab70 1428
8138d2dd
TR
1429 /*
1430 * We look up the provider in the static table typically provided by
1431 * board setup code. We first try to lookup the consumer device by
1432 * name. If the consumer device was passed in as NULL or if no match
1433 * was found, we try to find the consumer by directly looking it up
1434 * by name.
1435 *
1436 * If a match is found, the provider PWM chip is looked up by name
1437 * and a PWM device is requested using the PWM device per-chip index.
1438 *
1439 * The lookup algorithm was shamelessly taken from the clock
1440 * framework:
1441 *
1442 * We do slightly fuzzy matching here:
1443 * An entry with a NULL ID is assumed to be a wildcard.
1444 * If an entry has a device ID, it must match
1445 * If an entry has a connection ID, it must match
1446 * Then we take the most specific entry - with the following order
1447 * of precedence: dev+con > dev only > con only.
1448 */
1449 mutex_lock(&pwm_lookup_lock);
1450
1451 list_for_each_entry(p, &pwm_lookup_list, list) {
1452 match = 0;
1453
1454 if (p->dev_id) {
1455 if (!dev_id || strcmp(p->dev_id, dev_id))
1456 continue;
1457
1458 match += 2;
1459 }
1460
1461 if (p->con_id) {
1462 if (!con_id || strcmp(p->con_id, con_id))
1463 continue;
1464
1465 match += 1;
1466 }
1467
1468 if (match > best) {
70145f87 1469 chosen = p;
8138d2dd
TR
1470
1471 if (match != 3)
1472 best = match;
1473 else
1474 break;
1475 }
1476 }
1477
69efb343
HG
1478 mutex_unlock(&pwm_lookup_lock);
1479
1480 if (!chosen)
1481 return ERR_PTR(-ENODEV);
3796ce1d 1482
70145f87 1483 chip = pwmchip_find_by_name(chosen->provider);
b526a314
HG
1484
1485 /*
1486 * If the lookup entry specifies a module, load the module and retry
1487 * the PWM chip lookup. This can be used to work around driver load
1488 * ordering issues if driver's can't be made to properly support the
1489 * deferred probe mechanism.
1490 */
1491 if (!chip && chosen->module) {
1492 err = request_module(chosen->module);
1493 if (err == 0)
1494 chip = pwmchip_find_by_name(chosen->provider);
1495 }
1496
70145f87 1497 if (!chip)
69efb343 1498 return ERR_PTR(-EPROBE_DEFER);
3796ce1d 1499
70145f87
GU
1500 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
1501 if (IS_ERR(pwm))
69efb343 1502 return pwm;
8138d2dd 1503
b2c200e3
FG
1504 dl = pwm_device_link_add(dev, pwm);
1505 if (IS_ERR(dl)) {
0af4d704 1506 pwm_put(pwm);
b2c200e3
FG
1507 return ERR_CAST(dl);
1508 }
1509
fbd45a12
BB
1510 pwm->args.period = chosen->period;
1511 pwm->args.polarity = chosen->polarity;
1512
8138d2dd
TR
1513 return pwm;
1514}
1515EXPORT_SYMBOL_GPL(pwm_get);
1516
1517/**
1518 * pwm_put() - release a PWM device
1519 * @pwm: PWM device
1520 */
1521void pwm_put(struct pwm_device *pwm)
1522{
1523 if (!pwm)
1524 return;
1525
1526 mutex_lock(&pwm_lock);
1527
1528 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
e50d3523 1529 pr_warn("PWM device already freed\n");
8138d2dd
TR
1530 goto out;
1531 }
1532
1533 if (pwm->chip->ops->free)
1534 pwm->chip->ops->free(pwm->chip, pwm);
1535
1536 pwm->label = NULL;
1537
384461ab 1538 module_put(pwm->chip->owner);
8138d2dd
TR
1539out:
1540 mutex_unlock(&pwm_lock);
1541}
1542EXPORT_SYMBOL_GPL(pwm_put);
1543
9ae241d0 1544static void devm_pwm_release(void *pwm)
6354316d 1545{
9ae241d0 1546 pwm_put(pwm);
6354316d
AC
1547}
1548
1549/**
1550 * devm_pwm_get() - resource managed pwm_get()
1551 * @dev: device for PWM consumer
1552 * @con_id: consumer name
1553 *
1554 * This function performs like pwm_get() but the acquired PWM device will
1555 * automatically be released on driver detach.
04883802
TR
1556 *
1557 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1558 * error code on failure.
6354316d
AC
1559 */
1560struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
1561{
9ae241d0
AS
1562 struct pwm_device *pwm;
1563 int ret;
6354316d
AC
1564
1565 pwm = pwm_get(dev, con_id);
9ae241d0
AS
1566 if (IS_ERR(pwm))
1567 return pwm;
1568
1569 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1570 if (ret)
1571 return ERR_PTR(ret);
6354316d
AC
1572
1573 return pwm;
1574}
1575EXPORT_SYMBOL_GPL(devm_pwm_get);
1576
4a6ef8e3
NV
1577/**
1578 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1579 * @dev: device for PWM consumer
1580 * @fwnode: firmware node to get the PWM from
1581 * @con_id: consumer name
1582 *
1583 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1584 * acpi_pwm_get() for a detailed description.
1585 *
1586 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1587 * error code on failure.
1588 */
1589struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
1590 struct fwnode_handle *fwnode,
1591 const char *con_id)
1592{
9ae241d0
AS
1593 struct pwm_device *pwm = ERR_PTR(-ENODEV);
1594 int ret;
4a6ef8e3
NV
1595
1596 if (is_of_node(fwnode))
1597 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
1598 else if (is_acpi_node(fwnode))
1599 pwm = acpi_pwm_get(fwnode);
9ae241d0
AS
1600 if (IS_ERR(pwm))
1601 return pwm;
4a6ef8e3 1602
9ae241d0
AS
1603 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1604 if (ret)
1605 return ERR_PTR(ret);
4a6ef8e3
NV
1606
1607 return pwm;
1608}
1609EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
1610
62099abf
TR
1611static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
1612{
1613 unsigned int i;
1614
1615 for (i = 0; i < chip->npwm; i++) {
1616 struct pwm_device *pwm = &chip->pwms[i];
39100cee
BB
1617 struct pwm_state state;
1618
1619 pwm_get_state(pwm, &state);
62099abf
TR
1620
1621 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
1622
1623 if (test_bit(PWMF_REQUESTED, &pwm->flags))
adcba1e3 1624 seq_puts(s, " requested");
62099abf 1625
39100cee 1626 if (state.enabled)
adcba1e3 1627 seq_puts(s, " enabled");
62099abf 1628
a9d887dc
GDS
1629 seq_printf(s, " period: %llu ns", state.period);
1630 seq_printf(s, " duty: %llu ns", state.duty_cycle);
23e3523f
HS
1631 seq_printf(s, " polarity: %s",
1632 state.polarity ? "inverse" : "normal");
1633
9e40ee18
CG
1634 if (state.usage_power)
1635 seq_puts(s, " usage_power");
1636
adcba1e3 1637 seq_puts(s, "\n");
62099abf
TR
1638 }
1639}
1640
1641static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
1642{
54c86dd2
UKK
1643 unsigned long id = *pos;
1644 void *ret;
1645
62099abf
TR
1646 mutex_lock(&pwm_lock);
1647 s->private = "";
1648
54c86dd2
UKK
1649 ret = idr_get_next_ul(&pwm_chips, &id);
1650 *pos = id;
1651 return ret;
62099abf
TR
1652}
1653
1654static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
1655{
54c86dd2
UKK
1656 unsigned long id = *pos + 1;
1657 void *ret;
1658
62099abf
TR
1659 s->private = "\n";
1660
54c86dd2
UKK
1661 ret = idr_get_next_ul(&pwm_chips, &id);
1662 *pos = id;
1663 return ret;
62099abf
TR
1664}
1665
1666static void pwm_seq_stop(struct seq_file *s, void *v)
1667{
1668 mutex_unlock(&pwm_lock);
1669}
1670
1671static int pwm_seq_show(struct seq_file *s, void *v)
1672{
54c86dd2 1673 struct pwm_chip *chip = v;
62099abf 1674
0360a487
UKK
1675 seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n",
1676 (char *)s->private, chip->id,
4e59267c
UKK
1677 pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus",
1678 dev_name(pwmchip_parent(chip)), chip->npwm,
62099abf
TR
1679 (chip->npwm != 1) ? "s" : "");
1680
cc2d2247 1681 pwm_dbg_show(chip, s);
62099abf
TR
1682
1683 return 0;
1684}
1685
f339e79b 1686static const struct seq_operations pwm_debugfs_sops = {
62099abf
TR
1687 .start = pwm_seq_start,
1688 .next = pwm_seq_next,
1689 .stop = pwm_seq_stop,
1690 .show = pwm_seq_show,
1691};
1692
f339e79b 1693DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
62099abf 1694
e9cc807f 1695static int __init pwm_init(void)
62099abf 1696{
e9cc807f
UKK
1697 if (IS_ENABLED(CONFIG_DEBUG_FS))
1698 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops);
62099abf 1699
e9cc807f 1700 return class_register(&pwm_class);
62099abf 1701}
e9cc807f 1702subsys_initcall(pwm_init);