pwm: Move contents of sysfs.c into core.c
[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{
990 return (void *)chip + ALIGN(sizeof(*chip), PWMCHIP_ALIGN);
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;
1004
1005 alloc_size = size_add(ALIGN(sizeof(*chip), PWMCHIP_ALIGN), sizeof_priv);
1006
1007 chip = kzalloc(alloc_size, GFP_KERNEL);
1008 if (!chip)
1009 return ERR_PTR(-ENOMEM);
1010
1011 chip->dev = parent;
1012 chip->npwm = npwm;
05947224 1013 chip->uses_pwmchip_alloc = true;
024913db
UKK
1014
1015 pwmchip_set_drvdata(chip, pwmchip_priv(chip));
1016
1017 return chip;
1018}
1019EXPORT_SYMBOL_GPL(pwmchip_alloc);
1020
1021static void devm_pwmchip_put(void *data)
1022{
1023 struct pwm_chip *chip = data;
1024
1025 pwmchip_put(chip);
1026}
1027
1028struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
1029{
1030 struct pwm_chip *chip;
1031 int ret;
1032
1033 chip = pwmchip_alloc(parent, npwm, sizeof_priv);
1034 if (IS_ERR(chip))
1035 return chip;
1036
1037 ret = devm_add_action_or_reset(parent, devm_pwmchip_put, chip);
1038 if (ret)
1039 return ERR_PTR(ret);
1040
1041 return chip;
1042}
1043EXPORT_SYMBOL_GPL(devm_pwmchip_alloc);
1044
62928315 1045static void of_pwmchip_add(struct pwm_chip *chip)
7170d3be 1046{
4e59267c 1047 if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node)
62928315 1048 return;
7170d3be 1049
62928315
UKK
1050 if (!chip->of_xlate)
1051 chip->of_xlate = of_pwm_xlate_with_flags;
7170d3be 1052
4e59267c 1053 of_node_get(pwmchip_parent(chip)->of_node);
62928315 1054}
7170d3be 1055
62928315
UKK
1056static void of_pwmchip_remove(struct pwm_chip *chip)
1057{
4e59267c
UKK
1058 if (pwmchip_parent(chip))
1059 of_node_put(pwmchip_parent(chip)->of_node);
7170d3be 1060}
0aa0869c 1061
62928315 1062static bool pwm_ops_check(const struct pwm_chip *chip)
7170d3be 1063{
62928315 1064 const struct pwm_ops *ops = chip->ops;
7170d3be 1065
62928315
UKK
1066 if (!ops->apply)
1067 return false;
1068
1069 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
4e59267c 1070 dev_warn(pwmchip_parent(chip),
62928315
UKK
1071 "Please implement the .get_state() callback\n");
1072
1073 return true;
7170d3be 1074}
7170d3be 1075
3a3d1a4e 1076/**
62928315
UKK
1077 * __pwmchip_add() - register a new PWM chip
1078 * @chip: the PWM chip to add
1079 * @owner: reference to the module providing the chip.
1080 *
1081 * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
1082 * pwmchip_add wrapper to do this right.
3a3d1a4e
LJ
1083 *
1084 * Returns: 0 on success or a negative error code on failure.
1085 */
62928315 1086int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
3a3d1a4e 1087{
62928315
UKK
1088 unsigned int i;
1089 int ret;
3a3d1a4e 1090
4e59267c 1091 if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm)
3a3d1a4e
LJ
1092 return -EINVAL;
1093
05947224
UKK
1094 /*
1095 * a struct pwm_chip must be allocated using (devm_)pwmchip_alloc,
1096 * otherwise the embedded struct device might disappear too early
1097 * resulting in memory corruption.
1098 * Catch drivers that were not converted appropriately.
1099 */
1100 if (!chip->uses_pwmchip_alloc)
1101 return -EINVAL;
1102
62928315
UKK
1103 if (!pwm_ops_check(chip))
1104 return -EINVAL;
1105
1106 chip->owner = owner;
1107
1108 chip->pwms = kcalloc(chip->npwm, sizeof(*chip->pwms), GFP_KERNEL);
1109 if (!chip->pwms)
1110 return -ENOMEM;
3a3d1a4e
LJ
1111
1112 mutex_lock(&pwm_lock);
62928315
UKK
1113
1114 ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL);
1115 if (ret < 0) {
1116 mutex_unlock(&pwm_lock);
1117 kfree(chip->pwms);
1118 return ret;
1119 }
1120
1121 chip->id = ret;
1122
1123 for (i = 0; i < chip->npwm; i++) {
1124 struct pwm_device *pwm = &chip->pwms[i];
1125
1126 pwm->chip = chip;
1127 pwm->hwpwm = i;
1128 }
1129
3a3d1a4e
LJ
1130 mutex_unlock(&pwm_lock);
1131
62928315
UKK
1132 if (IS_ENABLED(CONFIG_OF))
1133 of_pwmchip_add(chip);
1134
1135 pwmchip_sysfs_export(chip);
1136
1137 return 0;
3a3d1a4e 1138}
62928315 1139EXPORT_SYMBOL_GPL(__pwmchip_add);
3a3d1a4e 1140
0c2498f1 1141/**
62928315
UKK
1142 * pwmchip_remove() - remove a PWM chip
1143 * @chip: the PWM chip to remove
04883802 1144 *
62928315 1145 * Removes a PWM chip.
0c2498f1 1146 */
62928315 1147void pwmchip_remove(struct pwm_chip *chip)
0c2498f1 1148{
62928315 1149 pwmchip_sysfs_unexport(chip);
d1cd2142 1150
62928315
UKK
1151 if (IS_ENABLED(CONFIG_OF))
1152 of_pwmchip_remove(chip);
d1cd2142 1153
62928315 1154 mutex_lock(&pwm_lock);
0c2498f1 1155
62928315 1156 idr_remove(&pwm_chips, chip->id);
0c2498f1 1157
62928315 1158 mutex_unlock(&pwm_lock);
5ec803ed 1159
62928315 1160 kfree(chip->pwms);
0c2498f1 1161}
62928315 1162EXPORT_SYMBOL_GPL(pwmchip_remove);
62099abf 1163
62928315 1164static void devm_pwmchip_remove(void *data)
7299ab70 1165{
62928315 1166 struct pwm_chip *chip = data;
7299ab70 1167
62928315
UKK
1168 pwmchip_remove(chip);
1169}
7299ab70 1170
62928315
UKK
1171int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner)
1172{
1173 int ret;
7299ab70 1174
62928315
UKK
1175 ret = __pwmchip_add(chip, owner);
1176 if (ret)
1177 return ret;
7299ab70 1178
62928315 1179 return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
7299ab70 1180}
62928315 1181EXPORT_SYMBOL_GPL(__devm_pwmchip_add);
7299ab70 1182
b2c200e3
FG
1183static struct device_link *pwm_device_link_add(struct device *dev,
1184 struct pwm_device *pwm)
1185{
1186 struct device_link *dl;
1187
1188 if (!dev) {
1189 /*
1190 * No device for the PWM consumer has been provided. It may
1191 * impact the PM sequence ordering: the PWM supplier may get
1192 * suspended before the consumer.
1193 */
4e59267c 1194 dev_warn(pwmchip_parent(pwm->chip),
b2c200e3
FG
1195 "No consumer device specified to create a link to\n");
1196 return NULL;
1197 }
1198
4e59267c 1199 dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER);
b2c200e3
FG
1200 if (!dl) {
1201 dev_err(dev, "failed to create device link to %s\n",
4e59267c 1202 dev_name(pwmchip_parent(pwm->chip)));
b2c200e3
FG
1203 return ERR_PTR(-EINVAL);
1204 }
1205
1206 return dl;
1207}
1208
62928315
UKK
1209static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
1210{
1211 struct pwm_chip *chip;
1212 unsigned long id, tmp;
1213
1214 mutex_lock(&pwm_lock);
1215
1216 idr_for_each_entry_ul(&pwm_chips, chip, tmp, id)
4e59267c 1217 if (pwmchip_parent(chip) && device_match_fwnode(pwmchip_parent(chip), fwnode)) {
62928315
UKK
1218 mutex_unlock(&pwm_lock);
1219 return chip;
1220 }
1221
1222 mutex_unlock(&pwm_lock);
1223
1224 return ERR_PTR(-EPROBE_DEFER);
1225}
1226
7299ab70 1227/**
8eb96127 1228 * of_pwm_get() - request a PWM via the PWM framework
b2c200e3 1229 * @dev: device for PWM consumer
7299ab70
TR
1230 * @np: device node to get the PWM from
1231 * @con_id: consumer name
1232 *
1233 * Returns the PWM device parsed from the phandle and index specified in the
1234 * "pwms" property of a device tree node or a negative error-code on failure.
1235 * Values parsed from the device tree are stored in the returned PWM device
1236 * object.
1237 *
1238 * If con_id is NULL, the first PWM device listed in the "pwms" property will
1239 * be requested. Otherwise the "pwm-names" property is used to do a reverse
1240 * lookup of the PWM index. This also means that the "pwm-names" property
1241 * becomes mandatory for devices that look up the PWM device via the con_id
1242 * parameter.
04883802
TR
1243 *
1244 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1245 * error code on failure.
7299ab70 1246 */
b5ae0ad5
AS
1247static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
1248 const char *con_id)
7299ab70
TR
1249{
1250 struct pwm_device *pwm = NULL;
1251 struct of_phandle_args args;
b2c200e3 1252 struct device_link *dl;
b4f78ff7 1253 struct pwm_chip *chip;
7299ab70
TR
1254 int index = 0;
1255 int err;
1256
1257 if (con_id) {
1258 index = of_property_match_string(np, "pwm-names", con_id);
1259 if (index < 0)
1260 return ERR_PTR(index);
1261 }
1262
1263 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
1264 &args);
1265 if (err) {
f2dafc09 1266 pr_err("%s(): can't parse \"pwms\" property\n", __func__);
7299ab70
TR
1267 return ERR_PTR(err);
1268 }
1269
b4f78ff7
UKK
1270 chip = fwnode_to_pwmchip(of_fwnode_handle(args.np));
1271 if (IS_ERR(chip)) {
1272 if (PTR_ERR(chip) != -EPROBE_DEFER)
93c292ef
JB
1273 pr_err("%s(): PWM chip not found\n", __func__);
1274
b4f78ff7 1275 pwm = ERR_CAST(chip);
7299ab70
TR
1276 goto put;
1277 }
1278
b4f78ff7 1279 pwm = chip->of_xlate(chip, &args);
7299ab70
TR
1280 if (IS_ERR(pwm))
1281 goto put;
1282
b2c200e3
FG
1283 dl = pwm_device_link_add(dev, pwm);
1284 if (IS_ERR(dl)) {
1285 /* of_xlate ended up calling pwm_request_from_chip() */
0af4d704 1286 pwm_put(pwm);
b2c200e3
FG
1287 pwm = ERR_CAST(dl);
1288 goto put;
1289 }
1290
7299ab70
TR
1291 /*
1292 * If a consumer name was not given, try to look it up from the
1293 * "pwm-names" property if it exists. Otherwise use the name of
1294 * the user device node.
1295 */
1296 if (!con_id) {
1297 err = of_property_read_string_index(np, "pwm-names", index,
1298 &con_id);
1299 if (err < 0)
1300 con_id = np->name;
1301 }
1302
1303 pwm->label = con_id;
1304
1305put:
1306 of_node_put(args.np);
1307
1308 return pwm;
1309}
1310
4a6ef8e3
NV
1311/**
1312 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
e625fb70 1313 * @fwnode: firmware node to get the "pwms" property from
4a6ef8e3
NV
1314 *
1315 * Returns the PWM device parsed from the fwnode and index specified in the
1316 * "pwms" property or a negative error-code on failure.
1317 * Values parsed from the device tree are stored in the returned PWM device
1318 * object.
1319 *
1320 * This is analogous to of_pwm_get() except con_id is not yet supported.
1321 * ACPI entries must look like
1322 * Package () {"pwms", Package ()
1323 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
1324 *
1325 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1326 * error code on failure.
1327 */
e625fb70 1328static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode)
4a6ef8e3 1329{
bebedf2b 1330 struct pwm_device *pwm;
4a6ef8e3 1331 struct fwnode_reference_args args;
4a6ef8e3
NV
1332 struct pwm_chip *chip;
1333 int ret;
1334
1335 memset(&args, 0, sizeof(args));
1336
1337 ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
1338 if (ret < 0)
1339 return ERR_PTR(ret);
1340
4a6ef8e3
NV
1341 if (args.nargs < 2)
1342 return ERR_PTR(-EPROTO);
1343
e5c38ba9 1344 chip = fwnode_to_pwmchip(args.fwnode);
4a6ef8e3
NV
1345 if (IS_ERR(chip))
1346 return ERR_CAST(chip);
1347
1348 pwm = pwm_request_from_chip(chip, args.args[0], NULL);
1349 if (IS_ERR(pwm))
1350 return pwm;
1351
1352 pwm->args.period = args.args[1];
1353 pwm->args.polarity = PWM_POLARITY_NORMAL;
1354
1355 if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
1356 pwm->args.polarity = PWM_POLARITY_INVERSED;
4a6ef8e3
NV
1357
1358 return pwm;
1359}
1360
62928315
UKK
1361static DEFINE_MUTEX(pwm_lookup_lock);
1362static LIST_HEAD(pwm_lookup_list);
1363
8138d2dd
TR
1364/**
1365 * pwm_add_table() - register PWM device consumers
1366 * @table: array of consumers to register
1367 * @num: number of consumers in table
1368 */
c264f111 1369void pwm_add_table(struct pwm_lookup *table, size_t num)
8138d2dd
TR
1370{
1371 mutex_lock(&pwm_lookup_lock);
1372
1373 while (num--) {
1374 list_add_tail(&table->list, &pwm_lookup_list);
1375 table++;
1376 }
1377
1378 mutex_unlock(&pwm_lookup_lock);
1379}
1380
efb0de55
SK
1381/**
1382 * pwm_remove_table() - unregister PWM device consumers
1383 * @table: array of consumers to unregister
1384 * @num: number of consumers in table
1385 */
1386void pwm_remove_table(struct pwm_lookup *table, size_t num)
1387{
1388 mutex_lock(&pwm_lookup_lock);
1389
1390 while (num--) {
1391 list_del(&table->list);
1392 table++;
1393 }
1394
1395 mutex_unlock(&pwm_lookup_lock);
1396}
1397
8138d2dd
TR
1398/**
1399 * pwm_get() - look up and request a PWM device
1400 * @dev: device for PWM consumer
1401 * @con_id: consumer name
1402 *
7299ab70
TR
1403 * Lookup is first attempted using DT. If the device was not instantiated from
1404 * a device tree, a PWM chip and a relative index is looked up via a table
1405 * supplied by board setup code (see pwm_add_table()).
8138d2dd
TR
1406 *
1407 * Once a PWM chip has been found the specified PWM device will be requested
1408 * and is ready to be used.
04883802
TR
1409 *
1410 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1411 * error code on failure.
8138d2dd
TR
1412 */
1413struct pwm_device *pwm_get(struct device *dev, const char *con_id)
1414{
e625fb70 1415 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
e50d3523 1416 const char *dev_id = dev ? dev_name(dev) : NULL;
69efb343
HG
1417 struct pwm_device *pwm;
1418 struct pwm_chip *chip;
b2c200e3 1419 struct device_link *dl;
8138d2dd 1420 unsigned int best = 0;
70145f87 1421 struct pwm_lookup *p, *chosen = NULL;
8138d2dd 1422 unsigned int match;
b526a314 1423 int err;
8138d2dd 1424
7299ab70 1425 /* look up via DT first */
e625fb70
AS
1426 if (is_of_node(fwnode))
1427 return of_pwm_get(dev, to_of_node(fwnode), con_id);
7299ab70 1428
4a6ef8e3 1429 /* then lookup via ACPI */
e625fb70
AS
1430 if (is_acpi_node(fwnode)) {
1431 pwm = acpi_pwm_get(fwnode);
6cf9481b
HG
1432 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
1433 return pwm;
1434 }
7299ab70 1435
8138d2dd
TR
1436 /*
1437 * We look up the provider in the static table typically provided by
1438 * board setup code. We first try to lookup the consumer device by
1439 * name. If the consumer device was passed in as NULL or if no match
1440 * was found, we try to find the consumer by directly looking it up
1441 * by name.
1442 *
1443 * If a match is found, the provider PWM chip is looked up by name
1444 * and a PWM device is requested using the PWM device per-chip index.
1445 *
1446 * The lookup algorithm was shamelessly taken from the clock
1447 * framework:
1448 *
1449 * We do slightly fuzzy matching here:
1450 * An entry with a NULL ID is assumed to be a wildcard.
1451 * If an entry has a device ID, it must match
1452 * If an entry has a connection ID, it must match
1453 * Then we take the most specific entry - with the following order
1454 * of precedence: dev+con > dev only > con only.
1455 */
1456 mutex_lock(&pwm_lookup_lock);
1457
1458 list_for_each_entry(p, &pwm_lookup_list, list) {
1459 match = 0;
1460
1461 if (p->dev_id) {
1462 if (!dev_id || strcmp(p->dev_id, dev_id))
1463 continue;
1464
1465 match += 2;
1466 }
1467
1468 if (p->con_id) {
1469 if (!con_id || strcmp(p->con_id, con_id))
1470 continue;
1471
1472 match += 1;
1473 }
1474
1475 if (match > best) {
70145f87 1476 chosen = p;
8138d2dd
TR
1477
1478 if (match != 3)
1479 best = match;
1480 else
1481 break;
1482 }
1483 }
1484
69efb343
HG
1485 mutex_unlock(&pwm_lookup_lock);
1486
1487 if (!chosen)
1488 return ERR_PTR(-ENODEV);
3796ce1d 1489
70145f87 1490 chip = pwmchip_find_by_name(chosen->provider);
b526a314
HG
1491
1492 /*
1493 * If the lookup entry specifies a module, load the module and retry
1494 * the PWM chip lookup. This can be used to work around driver load
1495 * ordering issues if driver's can't be made to properly support the
1496 * deferred probe mechanism.
1497 */
1498 if (!chip && chosen->module) {
1499 err = request_module(chosen->module);
1500 if (err == 0)
1501 chip = pwmchip_find_by_name(chosen->provider);
1502 }
1503
70145f87 1504 if (!chip)
69efb343 1505 return ERR_PTR(-EPROBE_DEFER);
3796ce1d 1506
70145f87
GU
1507 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
1508 if (IS_ERR(pwm))
69efb343 1509 return pwm;
8138d2dd 1510
b2c200e3
FG
1511 dl = pwm_device_link_add(dev, pwm);
1512 if (IS_ERR(dl)) {
0af4d704 1513 pwm_put(pwm);
b2c200e3
FG
1514 return ERR_CAST(dl);
1515 }
1516
fbd45a12
BB
1517 pwm->args.period = chosen->period;
1518 pwm->args.polarity = chosen->polarity;
1519
8138d2dd
TR
1520 return pwm;
1521}
1522EXPORT_SYMBOL_GPL(pwm_get);
1523
1524/**
1525 * pwm_put() - release a PWM device
1526 * @pwm: PWM device
1527 */
1528void pwm_put(struct pwm_device *pwm)
1529{
1530 if (!pwm)
1531 return;
1532
1533 mutex_lock(&pwm_lock);
1534
1535 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
e50d3523 1536 pr_warn("PWM device already freed\n");
8138d2dd
TR
1537 goto out;
1538 }
1539
1540 if (pwm->chip->ops->free)
1541 pwm->chip->ops->free(pwm->chip, pwm);
1542
1543 pwm->label = NULL;
1544
384461ab 1545 module_put(pwm->chip->owner);
8138d2dd
TR
1546out:
1547 mutex_unlock(&pwm_lock);
1548}
1549EXPORT_SYMBOL_GPL(pwm_put);
1550
9ae241d0 1551static void devm_pwm_release(void *pwm)
6354316d 1552{
9ae241d0 1553 pwm_put(pwm);
6354316d
AC
1554}
1555
1556/**
1557 * devm_pwm_get() - resource managed pwm_get()
1558 * @dev: device for PWM consumer
1559 * @con_id: consumer name
1560 *
1561 * This function performs like pwm_get() but the acquired PWM device will
1562 * automatically be released on driver detach.
04883802
TR
1563 *
1564 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1565 * error code on failure.
6354316d
AC
1566 */
1567struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
1568{
9ae241d0
AS
1569 struct pwm_device *pwm;
1570 int ret;
6354316d
AC
1571
1572 pwm = pwm_get(dev, con_id);
9ae241d0
AS
1573 if (IS_ERR(pwm))
1574 return pwm;
1575
1576 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1577 if (ret)
1578 return ERR_PTR(ret);
6354316d
AC
1579
1580 return pwm;
1581}
1582EXPORT_SYMBOL_GPL(devm_pwm_get);
1583
4a6ef8e3
NV
1584/**
1585 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1586 * @dev: device for PWM consumer
1587 * @fwnode: firmware node to get the PWM from
1588 * @con_id: consumer name
1589 *
1590 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1591 * acpi_pwm_get() for a detailed description.
1592 *
1593 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1594 * error code on failure.
1595 */
1596struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
1597 struct fwnode_handle *fwnode,
1598 const char *con_id)
1599{
9ae241d0
AS
1600 struct pwm_device *pwm = ERR_PTR(-ENODEV);
1601 int ret;
4a6ef8e3
NV
1602
1603 if (is_of_node(fwnode))
1604 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
1605 else if (is_acpi_node(fwnode))
1606 pwm = acpi_pwm_get(fwnode);
9ae241d0
AS
1607 if (IS_ERR(pwm))
1608 return pwm;
4a6ef8e3 1609
9ae241d0
AS
1610 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1611 if (ret)
1612 return ERR_PTR(ret);
4a6ef8e3
NV
1613
1614 return pwm;
1615}
1616EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
1617
62099abf
TR
1618static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
1619{
1620 unsigned int i;
1621
1622 for (i = 0; i < chip->npwm; i++) {
1623 struct pwm_device *pwm = &chip->pwms[i];
39100cee
BB
1624 struct pwm_state state;
1625
1626 pwm_get_state(pwm, &state);
62099abf
TR
1627
1628 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
1629
1630 if (test_bit(PWMF_REQUESTED, &pwm->flags))
adcba1e3 1631 seq_puts(s, " requested");
62099abf 1632
39100cee 1633 if (state.enabled)
adcba1e3 1634 seq_puts(s, " enabled");
62099abf 1635
a9d887dc
GDS
1636 seq_printf(s, " period: %llu ns", state.period);
1637 seq_printf(s, " duty: %llu ns", state.duty_cycle);
23e3523f
HS
1638 seq_printf(s, " polarity: %s",
1639 state.polarity ? "inverse" : "normal");
1640
9e40ee18
CG
1641 if (state.usage_power)
1642 seq_puts(s, " usage_power");
1643
adcba1e3 1644 seq_puts(s, "\n");
62099abf
TR
1645 }
1646}
1647
1648static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
1649{
54c86dd2
UKK
1650 unsigned long id = *pos;
1651 void *ret;
1652
62099abf
TR
1653 mutex_lock(&pwm_lock);
1654 s->private = "";
1655
54c86dd2
UKK
1656 ret = idr_get_next_ul(&pwm_chips, &id);
1657 *pos = id;
1658 return ret;
62099abf
TR
1659}
1660
1661static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
1662{
54c86dd2
UKK
1663 unsigned long id = *pos + 1;
1664 void *ret;
1665
62099abf
TR
1666 s->private = "\n";
1667
54c86dd2
UKK
1668 ret = idr_get_next_ul(&pwm_chips, &id);
1669 *pos = id;
1670 return ret;
62099abf
TR
1671}
1672
1673static void pwm_seq_stop(struct seq_file *s, void *v)
1674{
1675 mutex_unlock(&pwm_lock);
1676}
1677
1678static int pwm_seq_show(struct seq_file *s, void *v)
1679{
54c86dd2 1680 struct pwm_chip *chip = v;
62099abf 1681
0360a487
UKK
1682 seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n",
1683 (char *)s->private, chip->id,
4e59267c
UKK
1684 pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus",
1685 dev_name(pwmchip_parent(chip)), chip->npwm,
62099abf
TR
1686 (chip->npwm != 1) ? "s" : "");
1687
cc2d2247 1688 pwm_dbg_show(chip, s);
62099abf
TR
1689
1690 return 0;
1691}
1692
f339e79b 1693static const struct seq_operations pwm_debugfs_sops = {
62099abf
TR
1694 .start = pwm_seq_start,
1695 .next = pwm_seq_next,
1696 .stop = pwm_seq_stop,
1697 .show = pwm_seq_show,
1698};
1699
f339e79b 1700DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
62099abf 1701
e9cc807f 1702static int __init pwm_init(void)
62099abf 1703{
e9cc807f
UKK
1704 if (IS_ENABLED(CONFIG_DEBUG_FS))
1705 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops);
62099abf 1706
e9cc807f 1707 return class_register(&pwm_class);
62099abf 1708}
e9cc807f 1709subsys_initcall(pwm_init);