aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBoris Brezillon <boris.brezillon@free-electrons.com>2016-04-14 15:17:41 -0400
committerThierry Reding <thierry.reding@gmail.com>2016-05-17 08:48:03 -0400
commit5ec803edcb703fe379836f13560b79dfac79b01d (patch)
treeee69e2156e03337bd928e151a5c5a39aa44baeb2
parent15fa8a43c147213a9563903c87b29671035eb6e8 (diff)
pwm: Add core infrastructure to allow atomic updates
Add an ->apply() method to the pwm_ops struct to allow PWM drivers to implement atomic updates. This method is preferred over the ->enable(), ->disable() and ->config() methods if available. Add the pwm_apply_state() function to the PWM user API. Note that the pwm_apply_state() does not guarantee the atomicity of the update operation, it all depends on the availability and implementation of the ->apply() method. pwm_enable/disable/set_polarity/config() are now implemented as wrappers around the pwm_apply_state() function. pwm_adjust_config() is allowing smooth handover between the bootloader and the kernel. This function tries to adapt the current PWM state to the PWM arguments coming from a PWM lookup table or a DT definition without changing the duty_cycle/period proportion. Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com> [thierry.reding@gmail.com: fix a couple of typos] Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
-rw-r--r--drivers/pwm/core.c187
-rw-r--r--include/linux/pwm.h269
2 files changed, 303 insertions, 153 deletions
diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index a909c64ee863..729d457861fd 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -226,6 +226,19 @@ void *pwm_get_chip_data(struct pwm_device *pwm)
226} 226}
227EXPORT_SYMBOL_GPL(pwm_get_chip_data); 227EXPORT_SYMBOL_GPL(pwm_get_chip_data);
228 228
229static bool pwm_ops_check(const struct pwm_ops *ops)
230{
231 /* driver supports legacy, non-atomic operation */
232 if (ops->config && ops->enable && ops->disable)
233 return true;
234
235 /* driver supports atomic operation */
236 if (ops->apply)
237 return true;
238
239 return false;
240}
241
229/** 242/**
230 * pwmchip_add_with_polarity() - register a new PWM chip 243 * pwmchip_add_with_polarity() - register a new PWM chip
231 * @chip: the PWM chip to add 244 * @chip: the PWM chip to add
@@ -244,8 +257,10 @@ int pwmchip_add_with_polarity(struct pwm_chip *chip,
244 unsigned int i; 257 unsigned int i;
245 int ret; 258 int ret;
246 259
247 if (!chip || !chip->dev || !chip->ops || !chip->ops->config || 260 if (!chip || !chip->dev || !chip->ops || !chip->npwm)
248 !chip->ops->enable || !chip->ops->disable || !chip->npwm) 261 return -EINVAL;
262
263 if (!pwm_ops_check(chip->ops))
249 return -EINVAL; 264 return -EINVAL;
250 265
251 mutex_lock(&pwm_lock); 266 mutex_lock(&pwm_lock);
@@ -431,102 +446,138 @@ void pwm_free(struct pwm_device *pwm)
431EXPORT_SYMBOL_GPL(pwm_free); 446EXPORT_SYMBOL_GPL(pwm_free);
432 447
433/** 448/**
434 * pwm_config() - change a PWM device configuration 449 * pwm_apply_state() - atomically apply a new state to a PWM device
435 * @pwm: PWM device 450 * @pwm: PWM device
436 * @duty_ns: "on" time (in nanoseconds) 451 * @state: new state to apply. This can be adjusted by the PWM driver
437 * @period_ns: duration (in nanoseconds) of one cycle 452 * if the requested config is not achievable, for example,
438 * 453 * ->duty_cycle and ->period might be approximated.
439 * Returns: 0 on success or a negative error code on failure.
440 */ 454 */
441int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) 455int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state)
442{ 456{
443 int err; 457 int err;
444 458
445 if (!pwm || duty_ns < 0 || period_ns <= 0 || duty_ns > period_ns) 459 if (!pwm)
446 return -EINVAL; 460 return -EINVAL;
447 461
448 err = pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns); 462 if (!memcmp(state, &pwm->state, sizeof(*state)))
449 if (err) 463 return 0;
450 return err;
451
452 pwm->state.duty_cycle = duty_ns;
453 pwm->state.period = period_ns;
454
455 return 0;
456}
457EXPORT_SYMBOL_GPL(pwm_config);
458 464
459/** 465 if (pwm->chip->ops->apply) {
460 * pwm_set_polarity() - configure the polarity of a PWM signal 466 err = pwm->chip->ops->apply(pwm->chip, pwm, state);
461 * @pwm: PWM device 467 if (err)
462 * @polarity: new polarity of the PWM signal 468 return err;
463 *
464 * Note that the polarity cannot be configured while the PWM device is
465 * enabled.
466 *
467 * Returns: 0 on success or a negative error code on failure.
468 */
469int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity)
470{
471 int err;
472 469
473 if (!pwm || !pwm->chip->ops) 470 pwm->state = *state;
474 return -EINVAL; 471 } else {
472 /*
473 * FIXME: restore the initial state in case of error.
474 */
475 if (state->polarity != pwm->state.polarity) {
476 if (!pwm->chip->ops->set_polarity)
477 return -ENOTSUPP;
478
479 /*
480 * Changing the polarity of a running PWM is
481 * only allowed when the PWM driver implements
482 * ->apply().
483 */
484 if (pwm->state.enabled) {
485 pwm->chip->ops->disable(pwm->chip, pwm);
486 pwm->state.enabled = false;
487 }
488
489 err = pwm->chip->ops->set_polarity(pwm->chip, pwm,
490 state->polarity);
491 if (err)
492 return err;
493
494 pwm->state.polarity = state->polarity;
495 }
475 496
476 if (!pwm->chip->ops->set_polarity) 497 if (state->period != pwm->state.period ||
477 return -ENOSYS; 498 state->duty_cycle != pwm->state.duty_cycle) {
499 err = pwm->chip->ops->config(pwm->chip, pwm,
500 state->duty_cycle,
501 state->period);
502 if (err)
503 return err;
478 504
479 if (pwm_is_enabled(pwm)) 505 pwm->state.duty_cycle = state->duty_cycle;
480 return -EBUSY; 506 pwm->state.period = state->period;
507 }
481 508
482 err = pwm->chip->ops->set_polarity(pwm->chip, pwm, polarity); 509 if (state->enabled != pwm->state.enabled) {
483 if (err) 510 if (state->enabled) {
484 return err; 511 err = pwm->chip->ops->enable(pwm->chip, pwm);
512 if (err)
513 return err;
514 } else {
515 pwm->chip->ops->disable(pwm->chip, pwm);
516 }
485 517
486 pwm->state.polarity = polarity; 518 pwm->state.enabled = state->enabled;
519 }
520 }
487 521
488 return 0; 522 return 0;
489} 523}
490EXPORT_SYMBOL_GPL(pwm_set_polarity); 524EXPORT_SYMBOL_GPL(pwm_apply_state);
491 525
492/** 526/**
493 * pwm_enable() - start a PWM output toggling 527 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
494 * @pwm: PWM device 528 * @pwm: PWM device
495 * 529 *
496 * Returns: 0 on success or a negative error code on failure. 530 * This function will adjust the PWM config to the PWM arguments provided
531 * by the DT or PWM lookup table. This is particularly useful to adapt
532 * the bootloader config to the Linux one.
497 */ 533 */
498int pwm_enable(struct pwm_device *pwm) 534int pwm_adjust_config(struct pwm_device *pwm)
499{ 535{
500 int err = 0; 536 struct pwm_state state;
537 struct pwm_args pargs;
501 538
502 if (!pwm) 539 pwm_get_args(pwm, &pargs);
503 return -EINVAL; 540 pwm_get_state(pwm, &state);
541
542 /*
543 * If the current period is zero it means that either the PWM driver
544 * does not support initial state retrieval or the PWM has not yet
545 * been configured.
546 *
547 * In either case, we setup the new period and polarity, and assign a
548 * duty cycle of 0.
549 */
550 if (!state.period) {
551 state.duty_cycle = 0;
552 state.period = pargs.period;
553 state.polarity = pargs.polarity;
504 554
505 if (!pwm_is_enabled(pwm)) { 555 return pwm_apply_state(pwm, &state);
506 err = pwm->chip->ops->enable(pwm->chip, pwm);
507 if (!err)
508 pwm->state.enabled = true;
509 } 556 }
510 557
511 return err; 558 /*
512} 559 * Adjust the PWM duty cycle/period based on the period value provided
513EXPORT_SYMBOL_GPL(pwm_enable); 560 * in PWM args.
561 */
562 if (pargs.period != state.period) {
563 u64 dutycycle = (u64)state.duty_cycle * pargs.period;
514 564
515/** 565 do_div(dutycycle, state.period);
516 * pwm_disable() - stop a PWM output toggling 566 state.duty_cycle = dutycycle;
517 * @pwm: PWM device 567 state.period = pargs.period;
518 */ 568 }
519void pwm_disable(struct pwm_device *pwm)
520{
521 if (!pwm)
522 return;
523 569
524 if (pwm_is_enabled(pwm)) { 570 /*
525 pwm->chip->ops->disable(pwm->chip, pwm); 571 * If the polarity changed, we should also change the duty cycle.
526 pwm->state.enabled = false; 572 */
573 if (pargs.polarity != state.polarity) {
574 state.polarity = pargs.polarity;
575 state.duty_cycle = state.period - state.duty_cycle;
527 } 576 }
577
578 return pwm_apply_state(pwm, &state);
528} 579}
529EXPORT_SYMBOL_GPL(pwm_disable); 580EXPORT_SYMBOL_GPL(pwm_adjust_config);
530 581
531static struct pwm_chip *of_node_to_pwmchip(struct device_node *np) 582static struct pwm_chip *of_node_to_pwmchip(struct device_node *np)
532{ 583{
diff --git a/include/linux/pwm.h b/include/linux/pwm.h
index 33f8decd9f38..17018f3c066e 100644
--- a/include/linux/pwm.h
+++ b/include/linux/pwm.h
@@ -5,59 +5,7 @@
5#include <linux/mutex.h> 5#include <linux/mutex.h>
6#include <linux/of.h> 6#include <linux/of.h>
7 7
8struct pwm_device;
9struct seq_file; 8struct seq_file;
10
11#if IS_ENABLED(CONFIG_PWM)
12/*
13 * pwm_request - request a PWM device
14 */
15struct pwm_device *pwm_request(int pwm_id, const char *label);
16
17/*
18 * pwm_free - free a PWM device
19 */
20void pwm_free(struct pwm_device *pwm);
21
22/*
23 * pwm_config - change a PWM device configuration
24 */
25int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
26
27/*
28 * pwm_enable - start a PWM output toggling
29 */
30int pwm_enable(struct pwm_device *pwm);
31
32/*
33 * pwm_disable - stop a PWM output toggling
34 */
35void pwm_disable(struct pwm_device *pwm);
36#else
37static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
38{
39 return ERR_PTR(-ENODEV);
40}
41
42static inline void pwm_free(struct pwm_device *pwm)
43{
44}
45
46static inline int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
47{
48 return -EINVAL;
49}
50
51static inline int pwm_enable(struct pwm_device *pwm)
52{
53 return -EINVAL;
54}
55
56static inline void pwm_disable(struct pwm_device *pwm)
57{
58}
59#endif
60
61struct pwm_chip; 9struct pwm_chip;
62 10
63/** 11/**
@@ -184,11 +132,6 @@ static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
184 return state.duty_cycle; 132 return state.duty_cycle;
185} 133}
186 134
187/*
188 * pwm_set_polarity - configure the polarity of a PWM signal
189 */
190int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
191
192static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) 135static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
193{ 136{
194 struct pwm_state state; 137 struct pwm_state state;
@@ -204,34 +147,6 @@ static inline void pwm_get_args(const struct pwm_device *pwm,
204 *args = pwm->args; 147 *args = pwm->args;
205} 148}
206 149
207static inline void pwm_apply_args(struct pwm_device *pwm)
208{
209 /*
210 * PWM users calling pwm_apply_args() expect to have a fresh config
211 * where the polarity and period are set according to pwm_args info.
212 * The problem is, polarity can only be changed when the PWM is
213 * disabled.
214 *
215 * PWM drivers supporting hardware readout may declare the PWM device
216 * as enabled, and prevent polarity setting, which changes from the
217 * existing behavior, where all PWM devices are declared as disabled
218 * at startup (even if they are actually enabled), thus authorizing
219 * polarity setting.
220 *
221 * Instead of setting ->enabled to false, we call pwm_disable()
222 * before pwm_set_polarity() to ensure that everything is configured
223 * as expected, and the PWM is really disabled when the user request
224 * it.
225 *
226 * Note that PWM users requiring a smooth handover between the
227 * bootloader and the kernel (like critical regulators controlled by
228 * PWM devices) will have to switch to the atomic API and avoid calling
229 * pwm_apply_args().
230 */
231 pwm_disable(pwm);
232 pwm_set_polarity(pwm, pwm->args.polarity);
233}
234
235/** 150/**
236 * struct pwm_ops - PWM controller operations 151 * struct pwm_ops - PWM controller operations
237 * @request: optional hook for requesting a PWM 152 * @request: optional hook for requesting a PWM
@@ -240,6 +155,10 @@ static inline void pwm_apply_args(struct pwm_device *pwm)
240 * @set_polarity: configure the polarity of this PWM 155 * @set_polarity: configure the polarity of this PWM
241 * @enable: enable PWM output toggling 156 * @enable: enable PWM output toggling
242 * @disable: disable PWM output toggling 157 * @disable: disable PWM output toggling
158 * @apply: atomically apply a new PWM config. The state argument
159 * should be adjusted with the real hardware config (if the
160 * approximate the period or duty_cycle value, state should
161 * reflect it)
243 * @get_state: get the current PWM state. This function is only 162 * @get_state: get the current PWM state. This function is only
244 * called once per PWM device when the PWM chip is 163 * called once per PWM device when the PWM chip is
245 * registered. 164 * registered.
@@ -255,6 +174,8 @@ struct pwm_ops {
255 enum pwm_polarity polarity); 174 enum pwm_polarity polarity);
256 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm); 175 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
257 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm); 176 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
177 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
178 struct pwm_state *state);
258 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, 179 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
259 struct pwm_state *state); 180 struct pwm_state *state);
260#ifdef CONFIG_DEBUG_FS 181#ifdef CONFIG_DEBUG_FS
@@ -292,6 +213,115 @@ struct pwm_chip {
292}; 213};
293 214
294#if IS_ENABLED(CONFIG_PWM) 215#if IS_ENABLED(CONFIG_PWM)
216/* PWM user APIs */
217struct pwm_device *pwm_request(int pwm_id, const char *label);
218void pwm_free(struct pwm_device *pwm);
219int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
220int pwm_adjust_config(struct pwm_device *pwm);
221
222/**
223 * pwm_config() - change a PWM device configuration
224 * @pwm: PWM device
225 * @duty_ns: "on" time (in nanoseconds)
226 * @period_ns: duration (in nanoseconds) of one cycle
227 *
228 * Returns: 0 on success or a negative error code on failure.
229 */
230static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
231 int period_ns)
232{
233 struct pwm_state state;
234
235 if (!pwm)
236 return -EINVAL;
237
238 pwm_get_state(pwm, &state);
239 if (state.duty_cycle == duty_ns && state.period == period_ns)
240 return 0;
241
242 state.duty_cycle = duty_ns;
243 state.period = period_ns;
244 return pwm_apply_state(pwm, &state);
245}
246
247/**
248 * pwm_set_polarity() - configure the polarity of a PWM signal
249 * @pwm: PWM device
250 * @polarity: new polarity of the PWM signal
251 *
252 * Note that the polarity cannot be configured while the PWM device is
253 * enabled.
254 *
255 * Returns: 0 on success or a negative error code on failure.
256 */
257static inline int pwm_set_polarity(struct pwm_device *pwm,
258 enum pwm_polarity polarity)
259{
260 struct pwm_state state;
261
262 if (!pwm)
263 return -EINVAL;
264
265 pwm_get_state(pwm, &state);
266 if (state.polarity == polarity)
267 return 0;
268
269 /*
270 * Changing the polarity of a running PWM without adjusting the
271 * dutycycle/period value is a bit risky (can introduce glitches).
272 * Return -EBUSY in this case.
273 * Note that this is allowed when using pwm_apply_state() because
274 * the user specifies all the parameters.
275 */
276 if (state.enabled)
277 return -EBUSY;
278
279 state.polarity = polarity;
280 return pwm_apply_state(pwm, &state);
281}
282
283/**
284 * pwm_enable() - start a PWM output toggling
285 * @pwm: PWM device
286 *
287 * Returns: 0 on success or a negative error code on failure.
288 */
289static inline int pwm_enable(struct pwm_device *pwm)
290{
291 struct pwm_state state;
292
293 if (!pwm)
294 return -EINVAL;
295
296 pwm_get_state(pwm, &state);
297 if (state.enabled)
298 return 0;
299
300 state.enabled = true;
301 return pwm_apply_state(pwm, &state);
302}
303
304/**
305 * pwm_disable() - stop a PWM output toggling
306 * @pwm: PWM device
307 */
308static inline void pwm_disable(struct pwm_device *pwm)
309{
310 struct pwm_state state;
311
312 if (!pwm)
313 return;
314
315 pwm_get_state(pwm, &state);
316 if (!state.enabled)
317 return;
318
319 state.enabled = false;
320 pwm_apply_state(pwm, &state);
321}
322
323
324/* PWM provider APIs */
295int pwm_set_chip_data(struct pwm_device *pwm, void *data); 325int pwm_set_chip_data(struct pwm_device *pwm, void *data);
296void *pwm_get_chip_data(struct pwm_device *pwm); 326void *pwm_get_chip_data(struct pwm_device *pwm);
297 327
@@ -317,6 +347,47 @@ void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
317 347
318bool pwm_can_sleep(struct pwm_device *pwm); 348bool pwm_can_sleep(struct pwm_device *pwm);
319#else 349#else
350static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
351{
352 return ERR_PTR(-ENODEV);
353}
354
355static inline void pwm_free(struct pwm_device *pwm)
356{
357}
358
359static inline int pwm_apply_state(struct pwm_device *pwm,
360 const struct pwm_state *state)
361{
362 return -ENOTSUPP;
363}
364
365static inline int pwm_adjust_config(struct pwm_device *pwm)
366{
367 return -ENOTSUPP;
368}
369
370static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
371 int period_ns)
372{
373 return -EINVAL;
374}
375
376static inline int pwm_set_polarity(struct pwm_device *pwm,
377 enum pwm_polarity polarity)
378{
379 return -ENOTSUPP;
380}
381
382static inline int pwm_enable(struct pwm_device *pwm)
383{
384 return -EINVAL;
385}
386
387static inline void pwm_disable(struct pwm_device *pwm)
388{
389}
390
320static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data) 391static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
321{ 392{
322 return -EINVAL; 393 return -EINVAL;
@@ -388,6 +459,34 @@ static inline bool pwm_can_sleep(struct pwm_device *pwm)
388} 459}
389#endif 460#endif
390 461
462static inline void pwm_apply_args(struct pwm_device *pwm)
463{
464 /*
465 * PWM users calling pwm_apply_args() expect to have a fresh config
466 * where the polarity and period are set according to pwm_args info.
467 * The problem is, polarity can only be changed when the PWM is
468 * disabled.
469 *
470 * PWM drivers supporting hardware readout may declare the PWM device
471 * as enabled, and prevent polarity setting, which changes from the
472 * existing behavior, where all PWM devices are declared as disabled
473 * at startup (even if they are actually enabled), thus authorizing
474 * polarity setting.
475 *
476 * Instead of setting ->enabled to false, we call pwm_disable()
477 * before pwm_set_polarity() to ensure that everything is configured
478 * as expected, and the PWM is really disabled when the user request
479 * it.
480 *
481 * Note that PWM users requiring a smooth handover between the
482 * bootloader and the kernel (like critical regulators controlled by
483 * PWM devices) will have to switch to the atomic API and avoid calling
484 * pwm_apply_args().
485 */
486 pwm_disable(pwm);
487 pwm_set_polarity(pwm, pwm->args.polarity);
488}
489
391struct pwm_lookup { 490struct pwm_lookup {
392 struct list_head list; 491 struct list_head list;
393 const char *provider; 492 const char *provider;