@@ -522,6 +522,64 @@ static void pwm_apply_state_debug(struct pwm_device *pwm,
522522 }
523523}
524524
525+ static int pwm_apply_legacy (struct pwm_chip * chip , struct pwm_device * pwm ,
526+ const struct pwm_state * state )
527+ {
528+ int err ;
529+
530+ /*
531+ * FIXME: restore the initial state in case of error.
532+ */
533+ if (state -> polarity != pwm -> state .polarity ) {
534+ if (!chip -> ops -> set_polarity )
535+ return - EINVAL ;
536+
537+ /*
538+ * Changing the polarity of a running PWM is only allowed when
539+ * the PWM driver implements ->apply().
540+ */
541+ if (pwm -> state .enabled ) {
542+ chip -> ops -> disable (chip , pwm );
543+
544+ /*
545+ * Update pwm->state already here in case
546+ * .set_polarity() or another callback depend on that.
547+ */
548+ pwm -> state .enabled = false;
549+ }
550+
551+ err = chip -> ops -> set_polarity (chip , pwm , state -> polarity );
552+ if (err )
553+ return err ;
554+
555+ pwm -> state .polarity = state -> polarity ;
556+ }
557+
558+ if (state -> period != pwm -> state .period ||
559+ state -> duty_cycle != pwm -> state .duty_cycle ) {
560+ err = chip -> ops -> config (pwm -> chip , pwm ,
561+ state -> duty_cycle ,
562+ state -> period );
563+ if (err )
564+ return err ;
565+
566+ pwm -> state .period = state -> period ;
567+ pwm -> state .duty_cycle = state -> duty_cycle ;
568+ }
569+
570+ if (state -> enabled != pwm -> state .enabled ) {
571+ if (!pwm -> state .enabled ) {
572+ err = chip -> ops -> enable (chip , pwm );
573+ if (err )
574+ return err ;
575+ } else {
576+ chip -> ops -> disable (chip , pwm );
577+ }
578+ }
579+
580+ return 0 ;
581+ }
582+
525583/**
526584 * pwm_apply_state() - atomically apply a new state to a PWM device
527585 * @pwm: PWM device
@@ -554,70 +612,22 @@ int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
554612 state -> usage_power == pwm -> state .usage_power )
555613 return 0 ;
556614
557- if (chip -> ops -> apply ) {
615+ if (chip -> ops -> apply )
558616 err = chip -> ops -> apply (chip , pwm , state );
559- if (err )
560- return err ;
561-
562- trace_pwm_apply (pwm , state );
563-
564- pwm -> state = * state ;
565-
566- /*
567- * only do this after pwm->state was applied as some
568- * implementations of .get_state depend on this
569- */
570- pwm_apply_state_debug (pwm , state );
571- } else {
572- /*
573- * FIXME: restore the initial state in case of error.
574- */
575- if (state -> polarity != pwm -> state .polarity ) {
576- if (!chip -> ops -> set_polarity )
577- return - EINVAL ;
578-
579- /*
580- * Changing the polarity of a running PWM is
581- * only allowed when the PWM driver implements
582- * ->apply().
583- */
584- if (pwm -> state .enabled ) {
585- chip -> ops -> disable (chip , pwm );
586- pwm -> state .enabled = false;
587- }
588-
589- err = chip -> ops -> set_polarity (chip , pwm ,
590- state -> polarity );
591- if (err )
592- return err ;
593-
594- pwm -> state .polarity = state -> polarity ;
595- }
596-
597- if (state -> period != pwm -> state .period ||
598- state -> duty_cycle != pwm -> state .duty_cycle ) {
599- err = chip -> ops -> config (pwm -> chip , pwm ,
600- state -> duty_cycle ,
601- state -> period );
602- if (err )
603- return err ;
617+ else
618+ err = pwm_apply_legacy (chip , pwm , state );
619+ if (err )
620+ return err ;
604621
605- pwm -> state .duty_cycle = state -> duty_cycle ;
606- pwm -> state .period = state -> period ;
607- }
622+ trace_pwm_apply (pwm , state );
608623
609- if (state -> enabled != pwm -> state .enabled ) {
610- if (state -> enabled ) {
611- err = chip -> ops -> enable (chip , pwm );
612- if (err )
613- return err ;
614- } else {
615- chip -> ops -> disable (chip , pwm );
616- }
624+ pwm -> state = * state ;
617625
618- pwm -> state .enabled = state -> enabled ;
619- }
620- }
626+ /*
627+ * only do this after pwm->state was applied as some
628+ * implementations of .get_state depend on this
629+ */
630+ pwm_apply_state_debug (pwm , state );
621631
622632 return 0 ;
623633}
0 commit comments