Commit | Line | Data |
---|---|---|
c82ee6d3 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
0c2498f1 SH |
2 | /* |
3 | * Generic pwmlib implementation | |
4 | * | |
5 | * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de> | |
f051c466 | 6 | * Copyright (C) 2011-2012 Avionic Design GmbH |
0c2498f1 SH |
7 | */ |
8 | ||
4a6ef8e3 | 9 | #include <linux/acpi.h> |
0c2498f1 | 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 | 28 | static DEFINE_MUTEX(pwm_lock); |
e51b156b | 29 | |
54c86dd2 | 30 | static DEFINE_IDR(pwm_chips); |
f051c466 | 31 | |
62928315 UKK |
32 | static 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 | */ | |
143 | static 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 | */ | |
183 | int 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 | 210 | EXPORT_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 | */ | |
218 | int 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 | 225 | EXPORT_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 | */ | |
235 | int 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 | 281 | EXPORT_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 |
291 | int 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 | } | |
308 | EXPORT_SYMBOL_GPL(pwm_capture); | |
c8135b51 | 309 | |
62928315 UKK |
310 | static 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 | 334 | static 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 | |
4c56b143 UKK |
346 | if (!get_device(&chip->dev)) { |
347 | err = -ENODEV; | |
348 | goto err_get_device; | |
349 | } | |
350 | ||
62928315 UKK |
351 | if (ops->request) { |
352 | err = ops->request(chip, pwm); | |
353 | if (err) { | |
4c56b143 UKK |
354 | put_device(&chip->dev); |
355 | err_get_device: | |
62928315 UKK |
356 | module_put(chip->owner); |
357 | return err; | |
358 | } | |
359 | } | |
54c86dd2 | 360 | |
62928315 UKK |
361 | if (ops->get_state) { |
362 | /* | |
363 | * Zero-initialize state because most drivers are unaware of | |
364 | * .usage_power. The other members of state are supposed to be | |
365 | * set by lowlevel drivers. We still initialize the whole | |
366 | * structure for simplicity even though this might paper over | |
367 | * faulty implementations of .get_state(). | |
368 | */ | |
369 | struct pwm_state state = { 0, }; | |
0c2498f1 | 370 | |
62928315 UKK |
371 | err = ops->get_state(chip, pwm, &state); |
372 | trace_pwm_get(pwm, &state, err); | |
bcda91bf | 373 | |
62928315 UKK |
374 | if (!err) |
375 | pwm->state = state; | |
bcda91bf | 376 | |
62928315 UKK |
377 | if (IS_ENABLED(CONFIG_PWM_DEBUG)) |
378 | pwm->last = pwm->state; | |
379 | } | |
bcda91bf | 380 | |
62928315 UKK |
381 | set_bit(PWMF_REQUESTED, &pwm->flags); |
382 | pwm->label = label; | |
bcda91bf | 383 | |
62928315 | 384 | return 0; |
bcda91bf | 385 | } |
bcda91bf | 386 | |
f051c466 TR |
387 | /** |
388 | * pwm_request_from_chip() - request a PWM device relative to a PWM chip | |
389 | * @chip: PWM chip | |
390 | * @index: per-chip index of the PWM to request | |
391 | * @label: a literal description string of this PWM | |
392 | * | |
04883802 TR |
393 | * Returns: A pointer to the PWM device at the given index of the given PWM |
394 | * chip. A negative error code is returned if the index is not valid for the | |
395 | * specified PWM chip or if the PWM device cannot be requested. | |
f051c466 TR |
396 | */ |
397 | struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, | |
398 | unsigned int index, | |
399 | const char *label) | |
400 | { | |
401 | struct pwm_device *pwm; | |
402 | int err; | |
0c2498f1 | 403 | |
f051c466 TR |
404 | if (!chip || index >= chip->npwm) |
405 | return ERR_PTR(-EINVAL); | |
0c2498f1 | 406 | |
f051c466 TR |
407 | mutex_lock(&pwm_lock); |
408 | pwm = &chip->pwms[index]; | |
0c2498f1 | 409 | |
f051c466 TR |
410 | err = pwm_device_request(pwm, label); |
411 | if (err < 0) | |
412 | pwm = ERR_PTR(err); | |
413 | ||
414 | mutex_unlock(&pwm_lock); | |
0c2498f1 SH |
415 | return pwm; |
416 | } | |
f051c466 | 417 | EXPORT_SYMBOL_GPL(pwm_request_from_chip); |
0c2498f1 | 418 | |
3ad1f3a3 | 419 | |
62928315 UKK |
420 | struct pwm_device * |
421 | of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args) | |
422 | { | |
423 | struct pwm_device *pwm; | |
3ad1f3a3 | 424 | |
62928315 UKK |
425 | /* period in the second cell and flags in the third cell are optional */ |
426 | if (args->args_count < 1) | |
427 | return ERR_PTR(-EINVAL); | |
3ad1f3a3 | 428 | |
62928315 UKK |
429 | pwm = pwm_request_from_chip(chip, args->args[0], NULL); |
430 | if (IS_ERR(pwm)) | |
431 | return pwm; | |
76abbdde | 432 | |
62928315 UKK |
433 | if (args->args_count > 1) |
434 | pwm->args.period = args->args[1]; | |
f051c466 | 435 | |
62928315 UKK |
436 | pwm->args.polarity = PWM_POLARITY_NORMAL; |
437 | if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) | |
438 | pwm->args.polarity = PWM_POLARITY_INVERSED; | |
fc3c5512 | 439 | |
62928315 UKK |
440 | return pwm; |
441 | } | |
442 | EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags); | |
76abbdde | 443 | |
62928315 UKK |
444 | struct pwm_device * |
445 | of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args) | |
446 | { | |
447 | struct pwm_device *pwm; | |
5ec803ed | 448 | |
62928315 UKK |
449 | pwm = pwm_request_from_chip(chip, 0, NULL); |
450 | if (IS_ERR(pwm)) | |
451 | return pwm; | |
5ec803ed | 452 | |
73dfe970 | 453 | if (args->args_count > 0) |
62928315 | 454 | pwm->args.period = args->args[0]; |
0aa0869c | 455 | |
62928315 UKK |
456 | pwm->args.polarity = PWM_POLARITY_NORMAL; |
457 | if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED) | |
458 | pwm->args.polarity = PWM_POLARITY_INVERSED; | |
459 | ||
460 | return pwm; | |
0aa0869c | 461 | } |
62928315 | 462 | EXPORT_SYMBOL_GPL(of_pwm_single_xlate); |
7170d3be | 463 | |
e9cc807f UKK |
464 | struct pwm_export { |
465 | struct device pwm_dev; | |
466 | struct pwm_device *pwm; | |
467 | struct mutex lock; | |
468 | struct pwm_state suspend; | |
469 | }; | |
470 | ||
471 | static inline struct pwm_chip *pwmchip_from_dev(struct device *pwmchip_dev) | |
472 | { | |
4c56b143 | 473 | return container_of(pwmchip_dev, struct pwm_chip, dev); |
e9cc807f UKK |
474 | } |
475 | ||
476 | static inline struct pwm_export *pwmexport_from_dev(struct device *pwm_dev) | |
477 | { | |
478 | return container_of(pwm_dev, struct pwm_export, pwm_dev); | |
479 | } | |
480 | ||
481 | static inline struct pwm_device *pwm_from_dev(struct device *pwm_dev) | |
482 | { | |
483 | struct pwm_export *export = pwmexport_from_dev(pwm_dev); | |
484 | ||
485 | return export->pwm; | |
486 | } | |
487 | ||
488 | static ssize_t period_show(struct device *pwm_dev, | |
489 | struct device_attribute *attr, | |
490 | char *buf) | |
491 | { | |
492 | const struct pwm_device *pwm = pwm_from_dev(pwm_dev); | |
493 | struct pwm_state state; | |
494 | ||
495 | pwm_get_state(pwm, &state); | |
496 | ||
497 | return sysfs_emit(buf, "%llu\n", state.period); | |
498 | } | |
499 | ||
500 | static ssize_t period_store(struct device *pwm_dev, | |
501 | struct device_attribute *attr, | |
502 | const char *buf, size_t size) | |
503 | { | |
504 | struct pwm_export *export = pwmexport_from_dev(pwm_dev); | |
505 | struct pwm_device *pwm = export->pwm; | |
506 | struct pwm_state state; | |
507 | u64 val; | |
508 | int ret; | |
509 | ||
510 | ret = kstrtou64(buf, 0, &val); | |
511 | if (ret) | |
512 | return ret; | |
513 | ||
514 | mutex_lock(&export->lock); | |
515 | pwm_get_state(pwm, &state); | |
516 | state.period = val; | |
517 | ret = pwm_apply_might_sleep(pwm, &state); | |
518 | mutex_unlock(&export->lock); | |
519 | ||
520 | return ret ? : size; | |
521 | } | |
522 | ||
523 | static ssize_t duty_cycle_show(struct device *pwm_dev, | |
524 | struct device_attribute *attr, | |
525 | char *buf) | |
526 | { | |
527 | const struct pwm_device *pwm = pwm_from_dev(pwm_dev); | |
528 | struct pwm_state state; | |
529 | ||
530 | pwm_get_state(pwm, &state); | |
531 | ||
532 | return sysfs_emit(buf, "%llu\n", state.duty_cycle); | |
533 | } | |
534 | ||
535 | static ssize_t duty_cycle_store(struct device *pwm_dev, | |
536 | struct device_attribute *attr, | |
537 | const char *buf, size_t size) | |
538 | { | |
539 | struct pwm_export *export = pwmexport_from_dev(pwm_dev); | |
540 | struct pwm_device *pwm = export->pwm; | |
541 | struct pwm_state state; | |
542 | u64 val; | |
543 | int ret; | |
544 | ||
545 | ret = kstrtou64(buf, 0, &val); | |
546 | if (ret) | |
547 | return ret; | |
548 | ||
549 | mutex_lock(&export->lock); | |
550 | pwm_get_state(pwm, &state); | |
551 | state.duty_cycle = val; | |
552 | ret = pwm_apply_might_sleep(pwm, &state); | |
553 | mutex_unlock(&export->lock); | |
554 | ||
555 | return ret ? : size; | |
556 | } | |
557 | ||
558 | static ssize_t enable_show(struct device *pwm_dev, | |
559 | struct device_attribute *attr, | |
560 | char *buf) | |
561 | { | |
562 | const struct pwm_device *pwm = pwm_from_dev(pwm_dev); | |
563 | struct pwm_state state; | |
564 | ||
565 | pwm_get_state(pwm, &state); | |
566 | ||
567 | return sysfs_emit(buf, "%d\n", state.enabled); | |
568 | } | |
569 | ||
570 | static ssize_t enable_store(struct device *pwm_dev, | |
571 | struct device_attribute *attr, | |
572 | const char *buf, size_t size) | |
573 | { | |
574 | struct pwm_export *export = pwmexport_from_dev(pwm_dev); | |
575 | struct pwm_device *pwm = export->pwm; | |
576 | struct pwm_state state; | |
577 | int val, ret; | |
578 | ||
579 | ret = kstrtoint(buf, 0, &val); | |
580 | if (ret) | |
581 | return ret; | |
582 | ||
583 | mutex_lock(&export->lock); | |
584 | ||
585 | pwm_get_state(pwm, &state); | |
586 | ||
587 | switch (val) { | |
588 | case 0: | |
589 | state.enabled = false; | |
590 | break; | |
591 | case 1: | |
592 | state.enabled = true; | |
593 | break; | |
594 | default: | |
595 | ret = -EINVAL; | |
596 | goto unlock; | |
597 | } | |
598 | ||
599 | ret = pwm_apply_might_sleep(pwm, &state); | |
600 | ||
601 | unlock: | |
602 | mutex_unlock(&export->lock); | |
603 | return ret ? : size; | |
604 | } | |
605 | ||
606 | static ssize_t polarity_show(struct device *pwm_dev, | |
607 | struct device_attribute *attr, | |
608 | char *buf) | |
609 | { | |
610 | const struct pwm_device *pwm = pwm_from_dev(pwm_dev); | |
611 | const char *polarity = "unknown"; | |
612 | struct pwm_state state; | |
613 | ||
614 | pwm_get_state(pwm, &state); | |
615 | ||
616 | switch (state.polarity) { | |
617 | case PWM_POLARITY_NORMAL: | |
618 | polarity = "normal"; | |
619 | break; | |
620 | ||
621 | case PWM_POLARITY_INVERSED: | |
622 | polarity = "inversed"; | |
623 | break; | |
624 | } | |
625 | ||
626 | return sysfs_emit(buf, "%s\n", polarity); | |
627 | } | |
628 | ||
629 | static ssize_t polarity_store(struct device *pwm_dev, | |
630 | struct device_attribute *attr, | |
631 | const char *buf, size_t size) | |
632 | { | |
633 | struct pwm_export *export = pwmexport_from_dev(pwm_dev); | |
634 | struct pwm_device *pwm = export->pwm; | |
635 | enum pwm_polarity polarity; | |
636 | struct pwm_state state; | |
637 | int ret; | |
638 | ||
639 | if (sysfs_streq(buf, "normal")) | |
640 | polarity = PWM_POLARITY_NORMAL; | |
641 | else if (sysfs_streq(buf, "inversed")) | |
642 | polarity = PWM_POLARITY_INVERSED; | |
643 | else | |
644 | return -EINVAL; | |
645 | ||
646 | mutex_lock(&export->lock); | |
647 | pwm_get_state(pwm, &state); | |
648 | state.polarity = polarity; | |
649 | ret = pwm_apply_might_sleep(pwm, &state); | |
650 | mutex_unlock(&export->lock); | |
651 | ||
652 | return ret ? : size; | |
653 | } | |
654 | ||
655 | static ssize_t capture_show(struct device *pwm_dev, | |
656 | struct device_attribute *attr, | |
657 | char *buf) | |
658 | { | |
659 | struct pwm_device *pwm = pwm_from_dev(pwm_dev); | |
660 | struct pwm_capture result; | |
661 | int ret; | |
662 | ||
663 | ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ)); | |
664 | if (ret) | |
665 | return ret; | |
666 | ||
667 | return sysfs_emit(buf, "%u %u\n", result.period, result.duty_cycle); | |
668 | } | |
669 | ||
670 | static DEVICE_ATTR_RW(period); | |
671 | static DEVICE_ATTR_RW(duty_cycle); | |
672 | static DEVICE_ATTR_RW(enable); | |
673 | static DEVICE_ATTR_RW(polarity); | |
674 | static DEVICE_ATTR_RO(capture); | |
675 | ||
676 | static struct attribute *pwm_attrs[] = { | |
677 | &dev_attr_period.attr, | |
678 | &dev_attr_duty_cycle.attr, | |
679 | &dev_attr_enable.attr, | |
680 | &dev_attr_polarity.attr, | |
681 | &dev_attr_capture.attr, | |
682 | NULL | |
683 | }; | |
684 | ATTRIBUTE_GROUPS(pwm); | |
685 | ||
686 | static void pwm_export_release(struct device *pwm_dev) | |
687 | { | |
688 | struct pwm_export *export = pwmexport_from_dev(pwm_dev); | |
689 | ||
690 | kfree(export); | |
691 | } | |
692 | ||
693 | static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm) | |
694 | { | |
695 | struct pwm_export *export; | |
696 | char *pwm_prop[2]; | |
697 | int ret; | |
698 | ||
699 | if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags)) | |
700 | return -EBUSY; | |
701 | ||
702 | export = kzalloc(sizeof(*export), GFP_KERNEL); | |
703 | if (!export) { | |
704 | clear_bit(PWMF_EXPORTED, &pwm->flags); | |
705 | return -ENOMEM; | |
706 | } | |
707 | ||
708 | export->pwm = pwm; | |
709 | mutex_init(&export->lock); | |
710 | ||
711 | export->pwm_dev.release = pwm_export_release; | |
712 | export->pwm_dev.parent = pwmchip_dev; | |
713 | export->pwm_dev.devt = MKDEV(0, 0); | |
714 | export->pwm_dev.groups = pwm_groups; | |
715 | dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm); | |
716 | ||
717 | ret = device_register(&export->pwm_dev); | |
718 | if (ret) { | |
719 | clear_bit(PWMF_EXPORTED, &pwm->flags); | |
720 | put_device(&export->pwm_dev); | |
721 | export = NULL; | |
722 | return ret; | |
723 | } | |
724 | pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm); | |
725 | pwm_prop[1] = NULL; | |
726 | kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop); | |
727 | kfree(pwm_prop[0]); | |
728 | ||
729 | return 0; | |
730 | } | |
731 | ||
732 | static int pwm_unexport_match(struct device *pwm_dev, void *data) | |
733 | { | |
734 | return pwm_from_dev(pwm_dev) == data; | |
735 | } | |
736 | ||
737 | static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm) | |
738 | { | |
739 | struct device *pwm_dev; | |
740 | char *pwm_prop[2]; | |
741 | ||
742 | if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags)) | |
743 | return -ENODEV; | |
744 | ||
745 | pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match); | |
746 | if (!pwm_dev) | |
747 | return -ENODEV; | |
748 | ||
749 | pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm); | |
750 | pwm_prop[1] = NULL; | |
751 | kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop); | |
752 | kfree(pwm_prop[0]); | |
753 | ||
754 | /* for device_find_child() */ | |
755 | put_device(pwm_dev); | |
756 | device_unregister(pwm_dev); | |
757 | pwm_put(pwm); | |
758 | ||
759 | return 0; | |
760 | } | |
761 | ||
762 | static ssize_t export_store(struct device *pwmchip_dev, | |
763 | struct device_attribute *attr, | |
764 | const char *buf, size_t len) | |
765 | { | |
766 | struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); | |
767 | struct pwm_device *pwm; | |
768 | unsigned int hwpwm; | |
769 | int ret; | |
770 | ||
771 | ret = kstrtouint(buf, 0, &hwpwm); | |
772 | if (ret < 0) | |
773 | return ret; | |
774 | ||
775 | if (hwpwm >= chip->npwm) | |
776 | return -ENODEV; | |
777 | ||
778 | pwm = pwm_request_from_chip(chip, hwpwm, "sysfs"); | |
779 | if (IS_ERR(pwm)) | |
780 | return PTR_ERR(pwm); | |
781 | ||
782 | ret = pwm_export_child(pwmchip_dev, pwm); | |
783 | if (ret < 0) | |
784 | pwm_put(pwm); | |
785 | ||
786 | return ret ? : len; | |
787 | } | |
788 | static DEVICE_ATTR_WO(export); | |
789 | ||
790 | static ssize_t unexport_store(struct device *pwmchip_dev, | |
791 | struct device_attribute *attr, | |
792 | const char *buf, size_t len) | |
793 | { | |
794 | struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); | |
795 | unsigned int hwpwm; | |
796 | int ret; | |
797 | ||
798 | ret = kstrtouint(buf, 0, &hwpwm); | |
799 | if (ret < 0) | |
800 | return ret; | |
801 | ||
802 | if (hwpwm >= chip->npwm) | |
803 | return -ENODEV; | |
804 | ||
805 | ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]); | |
806 | ||
807 | return ret ? : len; | |
808 | } | |
809 | static DEVICE_ATTR_WO(unexport); | |
810 | ||
811 | static ssize_t npwm_show(struct device *pwmchip_dev, struct device_attribute *attr, | |
812 | char *buf) | |
813 | { | |
814 | const struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); | |
815 | ||
816 | return sysfs_emit(buf, "%u\n", chip->npwm); | |
817 | } | |
818 | static DEVICE_ATTR_RO(npwm); | |
819 | ||
820 | static struct attribute *pwm_chip_attrs[] = { | |
821 | &dev_attr_export.attr, | |
822 | &dev_attr_unexport.attr, | |
823 | &dev_attr_npwm.attr, | |
824 | NULL, | |
825 | }; | |
826 | ATTRIBUTE_GROUPS(pwm_chip); | |
827 | ||
828 | /* takes export->lock on success */ | |
829 | static struct pwm_export *pwm_class_get_state(struct device *pwmchip_dev, | |
830 | struct pwm_device *pwm, | |
831 | struct pwm_state *state) | |
832 | { | |
833 | struct device *pwm_dev; | |
834 | struct pwm_export *export; | |
835 | ||
836 | if (!test_bit(PWMF_EXPORTED, &pwm->flags)) | |
837 | return NULL; | |
838 | ||
839 | pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match); | |
840 | if (!pwm_dev) | |
841 | return NULL; | |
842 | ||
843 | export = pwmexport_from_dev(pwm_dev); | |
844 | put_device(pwm_dev); /* for device_find_child() */ | |
845 | ||
846 | mutex_lock(&export->lock); | |
847 | pwm_get_state(pwm, state); | |
848 | ||
849 | return export; | |
850 | } | |
851 | ||
852 | static int pwm_class_apply_state(struct pwm_export *export, | |
853 | struct pwm_device *pwm, | |
854 | struct pwm_state *state) | |
855 | { | |
856 | int ret = pwm_apply_might_sleep(pwm, state); | |
857 | ||
858 | /* release lock taken in pwm_class_get_state */ | |
859 | mutex_unlock(&export->lock); | |
860 | ||
861 | return ret; | |
862 | } | |
863 | ||
864 | static int pwm_class_resume_npwm(struct device *pwmchip_dev, unsigned int npwm) | |
865 | { | |
866 | struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); | |
867 | unsigned int i; | |
868 | int ret = 0; | |
869 | ||
870 | for (i = 0; i < npwm; i++) { | |
871 | struct pwm_device *pwm = &chip->pwms[i]; | |
872 | struct pwm_state state; | |
873 | struct pwm_export *export; | |
874 | ||
875 | export = pwm_class_get_state(pwmchip_dev, pwm, &state); | |
876 | if (!export) | |
877 | continue; | |
878 | ||
879 | /* If pwmchip was not enabled before suspend, do nothing. */ | |
880 | if (!export->suspend.enabled) { | |
881 | /* release lock taken in pwm_class_get_state */ | |
882 | mutex_unlock(&export->lock); | |
883 | continue; | |
884 | } | |
885 | ||
886 | state.enabled = export->suspend.enabled; | |
887 | ret = pwm_class_apply_state(export, pwm, &state); | |
888 | if (ret < 0) | |
889 | break; | |
890 | } | |
891 | ||
892 | return ret; | |
893 | } | |
894 | ||
895 | static int pwm_class_suspend(struct device *pwmchip_dev) | |
896 | { | |
897 | struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); | |
898 | unsigned int i; | |
899 | int ret = 0; | |
900 | ||
901 | for (i = 0; i < chip->npwm; i++) { | |
902 | struct pwm_device *pwm = &chip->pwms[i]; | |
903 | struct pwm_state state; | |
904 | struct pwm_export *export; | |
905 | ||
906 | export = pwm_class_get_state(pwmchip_dev, pwm, &state); | |
907 | if (!export) | |
908 | continue; | |
909 | ||
910 | /* | |
911 | * If pwmchip was not enabled before suspend, save | |
912 | * state for resume time and do nothing else. | |
913 | */ | |
914 | export->suspend = state; | |
915 | if (!state.enabled) { | |
916 | /* release lock taken in pwm_class_get_state */ | |
917 | mutex_unlock(&export->lock); | |
918 | continue; | |
919 | } | |
920 | ||
921 | state.enabled = false; | |
922 | ret = pwm_class_apply_state(export, pwm, &state); | |
923 | if (ret < 0) { | |
924 | /* | |
925 | * roll back the PWM devices that were disabled by | |
926 | * this suspend function. | |
927 | */ | |
928 | pwm_class_resume_npwm(pwmchip_dev, i); | |
929 | break; | |
930 | } | |
931 | } | |
932 | ||
933 | return ret; | |
934 | } | |
935 | ||
936 | static int pwm_class_resume(struct device *pwmchip_dev) | |
937 | { | |
938 | struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); | |
939 | ||
940 | return pwm_class_resume_npwm(pwmchip_dev, chip->npwm); | |
941 | } | |
942 | ||
943 | static DEFINE_SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume); | |
944 | ||
945 | static struct class pwm_class = { | |
946 | .name = "pwm", | |
947 | .dev_groups = pwm_chip_groups, | |
948 | .pm = pm_sleep_ptr(&pwm_class_pm_ops), | |
949 | }; | |
950 | ||
e9cc807f UKK |
951 | static void pwmchip_sysfs_unexport(struct pwm_chip *chip) |
952 | { | |
e9cc807f UKK |
953 | unsigned int i; |
954 | ||
e9cc807f UKK |
955 | for (i = 0; i < chip->npwm; i++) { |
956 | struct pwm_device *pwm = &chip->pwms[i]; | |
957 | ||
958 | if (test_bit(PWMF_EXPORTED, &pwm->flags)) | |
4c56b143 | 959 | pwm_unexport_child(&chip->dev, pwm); |
e9cc807f | 960 | } |
e9cc807f UKK |
961 | } |
962 | ||
024913db UKK |
963 | #define PWMCHIP_ALIGN ARCH_DMA_MINALIGN |
964 | ||
965 | static void *pwmchip_priv(struct pwm_chip *chip) | |
966 | { | |
ee37bf50 | 967 | return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN); |
024913db UKK |
968 | } |
969 | ||
970 | /* This is the counterpart to pwmchip_alloc() */ | |
971 | void pwmchip_put(struct pwm_chip *chip) | |
972 | { | |
4c56b143 | 973 | put_device(&chip->dev); |
024913db UKK |
974 | } |
975 | EXPORT_SYMBOL_GPL(pwmchip_put); | |
976 | ||
4c56b143 UKK |
977 | static void pwmchip_release(struct device *pwmchip_dev) |
978 | { | |
979 | struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev); | |
980 | ||
981 | kfree(chip); | |
982 | } | |
983 | ||
024913db UKK |
984 | struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv) |
985 | { | |
986 | struct pwm_chip *chip; | |
4c56b143 | 987 | struct device *pwmchip_dev; |
024913db | 988 | size_t alloc_size; |
ee37bf50 | 989 | unsigned int i; |
024913db | 990 | |
ee37bf50 UKK |
991 | alloc_size = size_add(ALIGN(struct_size(chip, pwms, npwm), PWMCHIP_ALIGN), |
992 | sizeof_priv); | |
024913db UKK |
993 | |
994 | chip = kzalloc(alloc_size, GFP_KERNEL); | |
995 | if (!chip) | |
996 | return ERR_PTR(-ENOMEM); | |
997 | ||
024913db | 998 | chip->npwm = npwm; |
05947224 | 999 | chip->uses_pwmchip_alloc = true; |
024913db | 1000 | |
4c56b143 UKK |
1001 | pwmchip_dev = &chip->dev; |
1002 | device_initialize(pwmchip_dev); | |
1003 | pwmchip_dev->class = &pwm_class; | |
1004 | pwmchip_dev->parent = parent; | |
1005 | pwmchip_dev->release = pwmchip_release; | |
1006 | ||
024913db UKK |
1007 | pwmchip_set_drvdata(chip, pwmchip_priv(chip)); |
1008 | ||
ee37bf50 UKK |
1009 | for (i = 0; i < chip->npwm; i++) { |
1010 | struct pwm_device *pwm = &chip->pwms[i]; | |
1011 | pwm->chip = chip; | |
1012 | pwm->hwpwm = i; | |
1013 | } | |
1014 | ||
024913db UKK |
1015 | return chip; |
1016 | } | |
1017 | EXPORT_SYMBOL_GPL(pwmchip_alloc); | |
1018 | ||
1019 | static void devm_pwmchip_put(void *data) | |
1020 | { | |
1021 | struct pwm_chip *chip = data; | |
1022 | ||
1023 | pwmchip_put(chip); | |
1024 | } | |
1025 | ||
1026 | struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv) | |
1027 | { | |
1028 | struct pwm_chip *chip; | |
1029 | int ret; | |
1030 | ||
1031 | chip = pwmchip_alloc(parent, npwm, sizeof_priv); | |
1032 | if (IS_ERR(chip)) | |
1033 | return chip; | |
1034 | ||
1035 | ret = devm_add_action_or_reset(parent, devm_pwmchip_put, chip); | |
1036 | if (ret) | |
1037 | return ERR_PTR(ret); | |
1038 | ||
1039 | return chip; | |
1040 | } | |
1041 | EXPORT_SYMBOL_GPL(devm_pwmchip_alloc); | |
1042 | ||
62928315 | 1043 | static void of_pwmchip_add(struct pwm_chip *chip) |
7170d3be | 1044 | { |
4e59267c | 1045 | if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node) |
62928315 | 1046 | return; |
7170d3be | 1047 | |
62928315 UKK |
1048 | if (!chip->of_xlate) |
1049 | chip->of_xlate = of_pwm_xlate_with_flags; | |
7170d3be | 1050 | |
4e59267c | 1051 | of_node_get(pwmchip_parent(chip)->of_node); |
62928315 | 1052 | } |
7170d3be | 1053 | |
62928315 UKK |
1054 | static void of_pwmchip_remove(struct pwm_chip *chip) |
1055 | { | |
4e59267c UKK |
1056 | if (pwmchip_parent(chip)) |
1057 | of_node_put(pwmchip_parent(chip)->of_node); | |
7170d3be | 1058 | } |
0aa0869c | 1059 | |
62928315 | 1060 | static bool pwm_ops_check(const struct pwm_chip *chip) |
7170d3be | 1061 | { |
62928315 | 1062 | const struct pwm_ops *ops = chip->ops; |
7170d3be | 1063 | |
62928315 UKK |
1064 | if (!ops->apply) |
1065 | return false; | |
1066 | ||
1067 | if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) | |
4e59267c | 1068 | dev_warn(pwmchip_parent(chip), |
62928315 UKK |
1069 | "Please implement the .get_state() callback\n"); |
1070 | ||
1071 | return true; | |
7170d3be | 1072 | } |
7170d3be | 1073 | |
3a3d1a4e | 1074 | /** |
62928315 UKK |
1075 | * __pwmchip_add() - register a new PWM chip |
1076 | * @chip: the PWM chip to add | |
1077 | * @owner: reference to the module providing the chip. | |
1078 | * | |
1079 | * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the | |
1080 | * pwmchip_add wrapper to do this right. | |
3a3d1a4e LJ |
1081 | * |
1082 | * Returns: 0 on success or a negative error code on failure. | |
1083 | */ | |
62928315 | 1084 | int __pwmchip_add(struct pwm_chip *chip, struct module *owner) |
3a3d1a4e | 1085 | { |
62928315 | 1086 | int ret; |
3a3d1a4e | 1087 | |
4e59267c | 1088 | if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm) |
3a3d1a4e LJ |
1089 | return -EINVAL; |
1090 | ||
05947224 UKK |
1091 | /* |
1092 | * a struct pwm_chip must be allocated using (devm_)pwmchip_alloc, | |
1093 | * otherwise the embedded struct device might disappear too early | |
1094 | * resulting in memory corruption. | |
1095 | * Catch drivers that were not converted appropriately. | |
1096 | */ | |
1097 | if (!chip->uses_pwmchip_alloc) | |
1098 | return -EINVAL; | |
1099 | ||
62928315 UKK |
1100 | if (!pwm_ops_check(chip)) |
1101 | return -EINVAL; | |
1102 | ||
1103 | chip->owner = owner; | |
1104 | ||
3a3d1a4e | 1105 | mutex_lock(&pwm_lock); |
62928315 UKK |
1106 | |
1107 | ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL); | |
4c56b143 UKK |
1108 | if (ret < 0) |
1109 | goto err_idr_alloc; | |
62928315 UKK |
1110 | |
1111 | chip->id = ret; | |
1112 | ||
4c56b143 | 1113 | dev_set_name(&chip->dev, "pwmchip%u", chip->id); |
3a3d1a4e | 1114 | |
62928315 UKK |
1115 | if (IS_ENABLED(CONFIG_OF)) |
1116 | of_pwmchip_add(chip); | |
1117 | ||
4c56b143 UKK |
1118 | ret = device_add(&chip->dev); |
1119 | if (ret) | |
1120 | goto err_device_add; | |
1121 | ||
1122 | mutex_unlock(&pwm_lock); | |
62928315 UKK |
1123 | |
1124 | return 0; | |
4c56b143 UKK |
1125 | |
1126 | err_device_add: | |
1127 | if (IS_ENABLED(CONFIG_OF)) | |
1128 | of_pwmchip_remove(chip); | |
1129 | ||
1130 | idr_remove(&pwm_chips, chip->id); | |
1131 | err_idr_alloc: | |
1132 | ||
1133 | mutex_unlock(&pwm_lock); | |
1134 | ||
1135 | return ret; | |
3a3d1a4e | 1136 | } |
62928315 | 1137 | EXPORT_SYMBOL_GPL(__pwmchip_add); |
3a3d1a4e | 1138 | |
0c2498f1 | 1139 | /** |
62928315 UKK |
1140 | * pwmchip_remove() - remove a PWM chip |
1141 | * @chip: the PWM chip to remove | |
04883802 | 1142 | * |
62928315 | 1143 | * Removes a PWM chip. |
0c2498f1 | 1144 | */ |
62928315 | 1145 | void pwmchip_remove(struct pwm_chip *chip) |
0c2498f1 | 1146 | { |
62928315 | 1147 | pwmchip_sysfs_unexport(chip); |
d1cd2142 | 1148 | |
62928315 UKK |
1149 | if (IS_ENABLED(CONFIG_OF)) |
1150 | of_pwmchip_remove(chip); | |
d1cd2142 | 1151 | |
62928315 | 1152 | mutex_lock(&pwm_lock); |
0c2498f1 | 1153 | |
62928315 | 1154 | idr_remove(&pwm_chips, chip->id); |
0c2498f1 | 1155 | |
62928315 | 1156 | mutex_unlock(&pwm_lock); |
4c56b143 UKK |
1157 | |
1158 | device_del(&chip->dev); | |
0c2498f1 | 1159 | } |
62928315 | 1160 | EXPORT_SYMBOL_GPL(pwmchip_remove); |
62099abf | 1161 | |
62928315 | 1162 | static void devm_pwmchip_remove(void *data) |
7299ab70 | 1163 | { |
62928315 | 1164 | struct pwm_chip *chip = data; |
7299ab70 | 1165 | |
62928315 UKK |
1166 | pwmchip_remove(chip); |
1167 | } | |
7299ab70 | 1168 | |
62928315 UKK |
1169 | int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner) |
1170 | { | |
1171 | int ret; | |
7299ab70 | 1172 | |
62928315 UKK |
1173 | ret = __pwmchip_add(chip, owner); |
1174 | if (ret) | |
1175 | return ret; | |
7299ab70 | 1176 | |
62928315 | 1177 | return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip); |
7299ab70 | 1178 | } |
62928315 | 1179 | EXPORT_SYMBOL_GPL(__devm_pwmchip_add); |
7299ab70 | 1180 | |
b2c200e3 FG |
1181 | static struct device_link *pwm_device_link_add(struct device *dev, |
1182 | struct pwm_device *pwm) | |
1183 | { | |
1184 | struct device_link *dl; | |
1185 | ||
1186 | if (!dev) { | |
1187 | /* | |
1188 | * No device for the PWM consumer has been provided. It may | |
1189 | * impact the PM sequence ordering: the PWM supplier may get | |
1190 | * suspended before the consumer. | |
1191 | */ | |
4e59267c | 1192 | dev_warn(pwmchip_parent(pwm->chip), |
b2c200e3 FG |
1193 | "No consumer device specified to create a link to\n"); |
1194 | return NULL; | |
1195 | } | |
1196 | ||
4e59267c | 1197 | dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER); |
b2c200e3 FG |
1198 | if (!dl) { |
1199 | dev_err(dev, "failed to create device link to %s\n", | |
4e59267c | 1200 | dev_name(pwmchip_parent(pwm->chip))); |
b2c200e3 FG |
1201 | return ERR_PTR(-EINVAL); |
1202 | } | |
1203 | ||
1204 | return dl; | |
1205 | } | |
1206 | ||
62928315 UKK |
1207 | static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode) |
1208 | { | |
1209 | struct pwm_chip *chip; | |
1210 | unsigned long id, tmp; | |
1211 | ||
1212 | mutex_lock(&pwm_lock); | |
1213 | ||
1214 | idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) | |
4e59267c | 1215 | if (pwmchip_parent(chip) && device_match_fwnode(pwmchip_parent(chip), fwnode)) { |
62928315 UKK |
1216 | mutex_unlock(&pwm_lock); |
1217 | return chip; | |
1218 | } | |
1219 | ||
1220 | mutex_unlock(&pwm_lock); | |
1221 | ||
1222 | return ERR_PTR(-EPROBE_DEFER); | |
1223 | } | |
1224 | ||
7299ab70 | 1225 | /** |
8eb96127 | 1226 | * of_pwm_get() - request a PWM via the PWM framework |
b2c200e3 | 1227 | * @dev: device for PWM consumer |
7299ab70 TR |
1228 | * @np: device node to get the PWM from |
1229 | * @con_id: consumer name | |
1230 | * | |
1231 | * Returns the PWM device parsed from the phandle and index specified in the | |
1232 | * "pwms" property of a device tree node or a negative error-code on failure. | |
1233 | * Values parsed from the device tree are stored in the returned PWM device | |
1234 | * object. | |
1235 | * | |
1236 | * If con_id is NULL, the first PWM device listed in the "pwms" property will | |
1237 | * be requested. Otherwise the "pwm-names" property is used to do a reverse | |
1238 | * lookup of the PWM index. This also means that the "pwm-names" property | |
1239 | * becomes mandatory for devices that look up the PWM device via the con_id | |
1240 | * parameter. | |
04883802 TR |
1241 | * |
1242 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded | |
1243 | * error code on failure. | |
7299ab70 | 1244 | */ |
b5ae0ad5 AS |
1245 | static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np, |
1246 | const char *con_id) | |
7299ab70 TR |
1247 | { |
1248 | struct pwm_device *pwm = NULL; | |
1249 | struct of_phandle_args args; | |
b2c200e3 | 1250 | struct device_link *dl; |
b4f78ff7 | 1251 | struct pwm_chip *chip; |
7299ab70 TR |
1252 | int index = 0; |
1253 | int err; | |
1254 | ||
1255 | if (con_id) { | |
1256 | index = of_property_match_string(np, "pwm-names", con_id); | |
1257 | if (index < 0) | |
1258 | return ERR_PTR(index); | |
1259 | } | |
1260 | ||
1261 | err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, | |
1262 | &args); | |
1263 | if (err) { | |
f2dafc09 | 1264 | pr_err("%s(): can't parse \"pwms\" property\n", __func__); |
7299ab70 TR |
1265 | return ERR_PTR(err); |
1266 | } | |
1267 | ||
b4f78ff7 UKK |
1268 | chip = fwnode_to_pwmchip(of_fwnode_handle(args.np)); |
1269 | if (IS_ERR(chip)) { | |
1270 | if (PTR_ERR(chip) != -EPROBE_DEFER) | |
93c292ef JB |
1271 | pr_err("%s(): PWM chip not found\n", __func__); |
1272 | ||
b4f78ff7 | 1273 | pwm = ERR_CAST(chip); |
7299ab70 TR |
1274 | goto put; |
1275 | } | |
1276 | ||
b4f78ff7 | 1277 | pwm = chip->of_xlate(chip, &args); |
7299ab70 TR |
1278 | if (IS_ERR(pwm)) |
1279 | goto put; | |
1280 | ||
b2c200e3 FG |
1281 | dl = pwm_device_link_add(dev, pwm); |
1282 | if (IS_ERR(dl)) { | |
1283 | /* of_xlate ended up calling pwm_request_from_chip() */ | |
0af4d704 | 1284 | pwm_put(pwm); |
b2c200e3 FG |
1285 | pwm = ERR_CAST(dl); |
1286 | goto put; | |
1287 | } | |
1288 | ||
7299ab70 TR |
1289 | /* |
1290 | * If a consumer name was not given, try to look it up from the | |
1291 | * "pwm-names" property if it exists. Otherwise use the name of | |
1292 | * the user device node. | |
1293 | */ | |
1294 | if (!con_id) { | |
1295 | err = of_property_read_string_index(np, "pwm-names", index, | |
1296 | &con_id); | |
1297 | if (err < 0) | |
1298 | con_id = np->name; | |
1299 | } | |
1300 | ||
1301 | pwm->label = con_id; | |
1302 | ||
1303 | put: | |
1304 | of_node_put(args.np); | |
1305 | ||
1306 | return pwm; | |
1307 | } | |
1308 | ||
4a6ef8e3 NV |
1309 | /** |
1310 | * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI | |
e625fb70 | 1311 | * @fwnode: firmware node to get the "pwms" property from |
4a6ef8e3 NV |
1312 | * |
1313 | * Returns the PWM device parsed from the fwnode and index specified in the | |
1314 | * "pwms" property or a negative error-code on failure. | |
1315 | * Values parsed from the device tree are stored in the returned PWM device | |
1316 | * object. | |
1317 | * | |
1318 | * This is analogous to of_pwm_get() except con_id is not yet supported. | |
1319 | * ACPI entries must look like | |
1320 | * Package () {"pwms", Package () | |
1321 | * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}} | |
1322 | * | |
1323 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded | |
1324 | * error code on failure. | |
1325 | */ | |
e625fb70 | 1326 | static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode) |
4a6ef8e3 | 1327 | { |
bebedf2b | 1328 | struct pwm_device *pwm; |
4a6ef8e3 | 1329 | struct fwnode_reference_args args; |
4a6ef8e3 NV |
1330 | struct pwm_chip *chip; |
1331 | int ret; | |
1332 | ||
1333 | memset(&args, 0, sizeof(args)); | |
1334 | ||
1335 | ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args); | |
1336 | if (ret < 0) | |
1337 | return ERR_PTR(ret); | |
1338 | ||
4a6ef8e3 NV |
1339 | if (args.nargs < 2) |
1340 | return ERR_PTR(-EPROTO); | |
1341 | ||
e5c38ba9 | 1342 | chip = fwnode_to_pwmchip(args.fwnode); |
4a6ef8e3 NV |
1343 | if (IS_ERR(chip)) |
1344 | return ERR_CAST(chip); | |
1345 | ||
1346 | pwm = pwm_request_from_chip(chip, args.args[0], NULL); | |
1347 | if (IS_ERR(pwm)) | |
1348 | return pwm; | |
1349 | ||
1350 | pwm->args.period = args.args[1]; | |
1351 | pwm->args.polarity = PWM_POLARITY_NORMAL; | |
1352 | ||
1353 | if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED) | |
1354 | pwm->args.polarity = PWM_POLARITY_INVERSED; | |
4a6ef8e3 NV |
1355 | |
1356 | return pwm; | |
1357 | } | |
1358 | ||
62928315 UKK |
1359 | static DEFINE_MUTEX(pwm_lookup_lock); |
1360 | static LIST_HEAD(pwm_lookup_list); | |
1361 | ||
8138d2dd TR |
1362 | /** |
1363 | * pwm_add_table() - register PWM device consumers | |
1364 | * @table: array of consumers to register | |
1365 | * @num: number of consumers in table | |
1366 | */ | |
c264f111 | 1367 | void pwm_add_table(struct pwm_lookup *table, size_t num) |
8138d2dd TR |
1368 | { |
1369 | mutex_lock(&pwm_lookup_lock); | |
1370 | ||
1371 | while (num--) { | |
1372 | list_add_tail(&table->list, &pwm_lookup_list); | |
1373 | table++; | |
1374 | } | |
1375 | ||
1376 | mutex_unlock(&pwm_lookup_lock); | |
1377 | } | |
1378 | ||
efb0de55 SK |
1379 | /** |
1380 | * pwm_remove_table() - unregister PWM device consumers | |
1381 | * @table: array of consumers to unregister | |
1382 | * @num: number of consumers in table | |
1383 | */ | |
1384 | void pwm_remove_table(struct pwm_lookup *table, size_t num) | |
1385 | { | |
1386 | mutex_lock(&pwm_lookup_lock); | |
1387 | ||
1388 | while (num--) { | |
1389 | list_del(&table->list); | |
1390 | table++; | |
1391 | } | |
1392 | ||
1393 | mutex_unlock(&pwm_lookup_lock); | |
1394 | } | |
1395 | ||
8138d2dd TR |
1396 | /** |
1397 | * pwm_get() - look up and request a PWM device | |
1398 | * @dev: device for PWM consumer | |
1399 | * @con_id: consumer name | |
1400 | * | |
7299ab70 TR |
1401 | * Lookup is first attempted using DT. If the device was not instantiated from |
1402 | * a device tree, a PWM chip and a relative index is looked up via a table | |
1403 | * supplied by board setup code (see pwm_add_table()). | |
8138d2dd TR |
1404 | * |
1405 | * Once a PWM chip has been found the specified PWM device will be requested | |
1406 | * and is ready to be used. | |
04883802 TR |
1407 | * |
1408 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded | |
1409 | * error code on failure. | |
8138d2dd TR |
1410 | */ |
1411 | struct pwm_device *pwm_get(struct device *dev, const char *con_id) | |
1412 | { | |
e625fb70 | 1413 | const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL; |
e50d3523 | 1414 | const char *dev_id = dev ? dev_name(dev) : NULL; |
69efb343 HG |
1415 | struct pwm_device *pwm; |
1416 | struct pwm_chip *chip; | |
b2c200e3 | 1417 | struct device_link *dl; |
8138d2dd | 1418 | unsigned int best = 0; |
70145f87 | 1419 | struct pwm_lookup *p, *chosen = NULL; |
8138d2dd | 1420 | unsigned int match; |
b526a314 | 1421 | int err; |
8138d2dd | 1422 | |
7299ab70 | 1423 | /* look up via DT first */ |
e625fb70 AS |
1424 | if (is_of_node(fwnode)) |
1425 | return of_pwm_get(dev, to_of_node(fwnode), con_id); | |
7299ab70 | 1426 | |
4a6ef8e3 | 1427 | /* then lookup via ACPI */ |
e625fb70 AS |
1428 | if (is_acpi_node(fwnode)) { |
1429 | pwm = acpi_pwm_get(fwnode); | |
6cf9481b HG |
1430 | if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT) |
1431 | return pwm; | |
1432 | } | |
7299ab70 | 1433 | |
8138d2dd TR |
1434 | /* |
1435 | * We look up the provider in the static table typically provided by | |
1436 | * board setup code. We first try to lookup the consumer device by | |
1437 | * name. If the consumer device was passed in as NULL or if no match | |
1438 | * was found, we try to find the consumer by directly looking it up | |
1439 | * by name. | |
1440 | * | |
1441 | * If a match is found, the provider PWM chip is looked up by name | |
1442 | * and a PWM device is requested using the PWM device per-chip index. | |
1443 | * | |
1444 | * The lookup algorithm was shamelessly taken from the clock | |
1445 | * framework: | |
1446 | * | |
1447 | * We do slightly fuzzy matching here: | |
1448 | * An entry with a NULL ID is assumed to be a wildcard. | |
1449 | * If an entry has a device ID, it must match | |
1450 | * If an entry has a connection ID, it must match | |
1451 | * Then we take the most specific entry - with the following order | |
1452 | * of precedence: dev+con > dev only > con only. | |
1453 | */ | |
1454 | mutex_lock(&pwm_lookup_lock); | |
1455 | ||
1456 | list_for_each_entry(p, &pwm_lookup_list, list) { | |
1457 | match = 0; | |
1458 | ||
1459 | if (p->dev_id) { | |
1460 | if (!dev_id || strcmp(p->dev_id, dev_id)) | |
1461 | continue; | |
1462 | ||
1463 | match += 2; | |
1464 | } | |
1465 | ||
1466 | if (p->con_id) { | |
1467 | if (!con_id || strcmp(p->con_id, con_id)) | |
1468 | continue; | |
1469 | ||
1470 | match += 1; | |
1471 | } | |
1472 | ||
1473 | if (match > best) { | |
70145f87 | 1474 | chosen = p; |
8138d2dd TR |
1475 | |
1476 | if (match != 3) | |
1477 | best = match; | |
1478 | else | |
1479 | break; | |
1480 | } | |
1481 | } | |
1482 | ||
69efb343 HG |
1483 | mutex_unlock(&pwm_lookup_lock); |
1484 | ||
1485 | if (!chosen) | |
1486 | return ERR_PTR(-ENODEV); | |
3796ce1d | 1487 | |
70145f87 | 1488 | chip = pwmchip_find_by_name(chosen->provider); |
b526a314 HG |
1489 | |
1490 | /* | |
1491 | * If the lookup entry specifies a module, load the module and retry | |
1492 | * the PWM chip lookup. This can be used to work around driver load | |
1493 | * ordering issues if driver's can't be made to properly support the | |
1494 | * deferred probe mechanism. | |
1495 | */ | |
1496 | if (!chip && chosen->module) { | |
1497 | err = request_module(chosen->module); | |
1498 | if (err == 0) | |
1499 | chip = pwmchip_find_by_name(chosen->provider); | |
1500 | } | |
1501 | ||
70145f87 | 1502 | if (!chip) |
69efb343 | 1503 | return ERR_PTR(-EPROBE_DEFER); |
3796ce1d | 1504 | |
70145f87 GU |
1505 | pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); |
1506 | if (IS_ERR(pwm)) | |
69efb343 | 1507 | return pwm; |
8138d2dd | 1508 | |
b2c200e3 FG |
1509 | dl = pwm_device_link_add(dev, pwm); |
1510 | if (IS_ERR(dl)) { | |
0af4d704 | 1511 | pwm_put(pwm); |
b2c200e3 FG |
1512 | return ERR_CAST(dl); |
1513 | } | |
1514 | ||
fbd45a12 BB |
1515 | pwm->args.period = chosen->period; |
1516 | pwm->args.polarity = chosen->polarity; | |
1517 | ||
8138d2dd TR |
1518 | return pwm; |
1519 | } | |
1520 | EXPORT_SYMBOL_GPL(pwm_get); | |
1521 | ||
1522 | /** | |
1523 | * pwm_put() - release a PWM device | |
1524 | * @pwm: PWM device | |
1525 | */ | |
1526 | void pwm_put(struct pwm_device *pwm) | |
1527 | { | |
4c56b143 UKK |
1528 | struct pwm_chip *chip = pwm->chip; |
1529 | ||
8138d2dd TR |
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 | ||
4c56b143 | 1540 | if (chip->ops->free) |
8138d2dd TR |
1541 | pwm->chip->ops->free(pwm->chip, pwm); |
1542 | ||
1543 | pwm->label = NULL; | |
1544 | ||
4c56b143 UKK |
1545 | put_device(&chip->dev); |
1546 | ||
1547 | module_put(chip->owner); | |
8138d2dd TR |
1548 | out: |
1549 | mutex_unlock(&pwm_lock); | |
1550 | } | |
1551 | EXPORT_SYMBOL_GPL(pwm_put); | |
1552 | ||
9ae241d0 | 1553 | static void devm_pwm_release(void *pwm) |
6354316d | 1554 | { |
9ae241d0 | 1555 | pwm_put(pwm); |
6354316d AC |
1556 | } |
1557 | ||
1558 | /** | |
1559 | * devm_pwm_get() - resource managed pwm_get() | |
1560 | * @dev: device for PWM consumer | |
1561 | * @con_id: consumer name | |
1562 | * | |
1563 | * This function performs like pwm_get() but the acquired PWM device will | |
1564 | * automatically be released on driver detach. | |
04883802 TR |
1565 | * |
1566 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded | |
1567 | * error code on failure. | |
6354316d AC |
1568 | */ |
1569 | struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id) | |
1570 | { | |
9ae241d0 AS |
1571 | struct pwm_device *pwm; |
1572 | int ret; | |
6354316d AC |
1573 | |
1574 | pwm = pwm_get(dev, con_id); | |
9ae241d0 AS |
1575 | if (IS_ERR(pwm)) |
1576 | return pwm; | |
1577 | ||
1578 | ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); | |
1579 | if (ret) | |
1580 | return ERR_PTR(ret); | |
6354316d AC |
1581 | |
1582 | return pwm; | |
1583 | } | |
1584 | EXPORT_SYMBOL_GPL(devm_pwm_get); | |
1585 | ||
4a6ef8e3 NV |
1586 | /** |
1587 | * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node | |
1588 | * @dev: device for PWM consumer | |
1589 | * @fwnode: firmware node to get the PWM from | |
1590 | * @con_id: consumer name | |
1591 | * | |
1592 | * Returns the PWM device parsed from the firmware node. See of_pwm_get() and | |
1593 | * acpi_pwm_get() for a detailed description. | |
1594 | * | |
1595 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded | |
1596 | * error code on failure. | |
1597 | */ | |
1598 | struct pwm_device *devm_fwnode_pwm_get(struct device *dev, | |
1599 | struct fwnode_handle *fwnode, | |
1600 | const char *con_id) | |
1601 | { | |
9ae241d0 AS |
1602 | struct pwm_device *pwm = ERR_PTR(-ENODEV); |
1603 | int ret; | |
4a6ef8e3 NV |
1604 | |
1605 | if (is_of_node(fwnode)) | |
1606 | pwm = of_pwm_get(dev, to_of_node(fwnode), con_id); | |
1607 | else if (is_acpi_node(fwnode)) | |
1608 | pwm = acpi_pwm_get(fwnode); | |
9ae241d0 AS |
1609 | if (IS_ERR(pwm)) |
1610 | return pwm; | |
4a6ef8e3 | 1611 | |
9ae241d0 AS |
1612 | ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); |
1613 | if (ret) | |
1614 | return ERR_PTR(ret); | |
4a6ef8e3 NV |
1615 | |
1616 | return pwm; | |
1617 | } | |
1618 | EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get); | |
1619 | ||
62099abf TR |
1620 | static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s) |
1621 | { | |
1622 | unsigned int i; | |
1623 | ||
1624 | for (i = 0; i < chip->npwm; i++) { | |
1625 | struct pwm_device *pwm = &chip->pwms[i]; | |
39100cee BB |
1626 | struct pwm_state state; |
1627 | ||
1628 | pwm_get_state(pwm, &state); | |
62099abf TR |
1629 | |
1630 | seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); | |
1631 | ||
1632 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) | |
adcba1e3 | 1633 | seq_puts(s, " requested"); |
62099abf | 1634 | |
39100cee | 1635 | if (state.enabled) |
adcba1e3 | 1636 | seq_puts(s, " enabled"); |
62099abf | 1637 | |
a9d887dc GDS |
1638 | seq_printf(s, " period: %llu ns", state.period); |
1639 | seq_printf(s, " duty: %llu ns", state.duty_cycle); | |
23e3523f HS |
1640 | seq_printf(s, " polarity: %s", |
1641 | state.polarity ? "inverse" : "normal"); | |
1642 | ||
9e40ee18 CG |
1643 | if (state.usage_power) |
1644 | seq_puts(s, " usage_power"); | |
1645 | ||
adcba1e3 | 1646 | seq_puts(s, "\n"); |
62099abf TR |
1647 | } |
1648 | } | |
1649 | ||
1650 | static void *pwm_seq_start(struct seq_file *s, loff_t *pos) | |
1651 | { | |
54c86dd2 UKK |
1652 | unsigned long id = *pos; |
1653 | void *ret; | |
1654 | ||
62099abf TR |
1655 | mutex_lock(&pwm_lock); |
1656 | s->private = ""; | |
1657 | ||
54c86dd2 UKK |
1658 | ret = idr_get_next_ul(&pwm_chips, &id); |
1659 | *pos = id; | |
1660 | return ret; | |
62099abf TR |
1661 | } |
1662 | ||
1663 | static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
1664 | { | |
54c86dd2 UKK |
1665 | unsigned long id = *pos + 1; |
1666 | void *ret; | |
1667 | ||
62099abf TR |
1668 | s->private = "\n"; |
1669 | ||
54c86dd2 UKK |
1670 | ret = idr_get_next_ul(&pwm_chips, &id); |
1671 | *pos = id; | |
1672 | return ret; | |
62099abf TR |
1673 | } |
1674 | ||
1675 | static void pwm_seq_stop(struct seq_file *s, void *v) | |
1676 | { | |
1677 | mutex_unlock(&pwm_lock); | |
1678 | } | |
1679 | ||
1680 | static int pwm_seq_show(struct seq_file *s, void *v) | |
1681 | { | |
54c86dd2 | 1682 | struct pwm_chip *chip = v; |
62099abf | 1683 | |
0360a487 UKK |
1684 | seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n", |
1685 | (char *)s->private, chip->id, | |
4e59267c UKK |
1686 | pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus", |
1687 | dev_name(pwmchip_parent(chip)), chip->npwm, | |
62099abf TR |
1688 | (chip->npwm != 1) ? "s" : ""); |
1689 | ||
cc2d2247 | 1690 | pwm_dbg_show(chip, s); |
62099abf TR |
1691 | |
1692 | return 0; | |
1693 | } | |
1694 | ||
f339e79b | 1695 | static const struct seq_operations pwm_debugfs_sops = { |
62099abf TR |
1696 | .start = pwm_seq_start, |
1697 | .next = pwm_seq_next, | |
1698 | .stop = pwm_seq_stop, | |
1699 | .show = pwm_seq_show, | |
1700 | }; | |
1701 | ||
f339e79b | 1702 | DEFINE_SEQ_ATTRIBUTE(pwm_debugfs); |
62099abf | 1703 | |
e9cc807f | 1704 | static int __init pwm_init(void) |
62099abf | 1705 | { |
e9cc807f UKK |
1706 | if (IS_ENABLED(CONFIG_DEBUG_FS)) |
1707 | debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops); | |
62099abf | 1708 | |
e9cc807f | 1709 | return class_register(&pwm_class); |
62099abf | 1710 | } |
e9cc807f | 1711 | subsys_initcall(pwm_init); |