1212#include <linux/mfd/stm32-timers.h>
1313#include <linux/module.h>
1414#include <linux/of.h>
15+ #include <linux/pinctrl/consumer.h>
1516#include <linux/platform_device.h>
1617#include <linux/pwm.h>
1718
1819#define CCMR_CHANNEL_SHIFT 8
1920#define CCMR_CHANNEL_MASK 0xFF
2021#define MAX_BREAKINPUT 2
2122
23+ struct stm32_breakinput {
24+ u32 index ;
25+ u32 level ;
26+ u32 filter ;
27+ };
28+
2229struct stm32_pwm {
2330 struct pwm_chip chip ;
2431 struct mutex lock ; /* protect pwm config/enable */
2532 struct clk * clk ;
2633 struct regmap * regmap ;
2734 u32 max_arr ;
2835 bool have_complementary_output ;
36+ struct stm32_breakinput breakinputs [MAX_BREAKINPUT ];
37+ unsigned int num_breakinputs ;
2938 u32 capture [4 ] ____cacheline_aligned ; /* DMA'able buffer */
3039};
3140
32- struct stm32_breakinput {
33- u32 index ;
34- u32 level ;
35- u32 filter ;
36- };
37-
3841static inline struct stm32_pwm * to_stm32_pwm_dev (struct pwm_chip * chip )
3942{
4043 return container_of (chip , struct stm32_pwm , chip );
@@ -488,22 +491,19 @@ static const struct pwm_ops stm32pwm_ops = {
488491};
489492
490493static int stm32_pwm_set_breakinput (struct stm32_pwm * priv ,
491- int index , int level , int filter )
494+ const struct stm32_breakinput * bi )
492495{
493- u32 bke = (index == 0 ) ? TIM_BDTR_BKE : TIM_BDTR_BK2E ;
494- int shift = (index == 0 ) ? TIM_BDTR_BKF_SHIFT : TIM_BDTR_BK2F_SHIFT ;
495- u32 mask = (index == 0 ) ? TIM_BDTR_BKE | TIM_BDTR_BKP | TIM_BDTR_BKF
496- : TIM_BDTR_BK2E | TIM_BDTR_BK2P | TIM_BDTR_BK2F ;
497- u32 bdtr = bke ;
496+ u32 shift = TIM_BDTR_BKF_SHIFT (bi -> index );
497+ u32 bke = TIM_BDTR_BKE (bi -> index );
498+ u32 bkp = TIM_BDTR_BKP (bi -> index );
499+ u32 bkf = TIM_BDTR_BKF (bi -> index );
500+ u32 mask = bkf | bkp | bke ;
501+ u32 bdtr ;
498502
499- /*
500- * The both bits could be set since only one will be wrote
501- * due to mask value.
502- */
503- if (level )
504- bdtr |= TIM_BDTR_BKP | TIM_BDTR_BK2P ;
503+ bdtr = (bi -> filter & TIM_BDTR_BKF_MASK ) << shift | bke ;
505504
506- bdtr |= (filter & TIM_BDTR_BKF_MASK ) << shift ;
505+ if (bi -> level )
506+ bdtr |= bkp ;
507507
508508 regmap_update_bits (priv -> regmap , TIM_BDTR , mask , bdtr );
509509
@@ -512,11 +512,25 @@ static int stm32_pwm_set_breakinput(struct stm32_pwm *priv,
512512 return (bdtr & bke ) ? 0 : - EINVAL ;
513513}
514514
515- static int stm32_pwm_apply_breakinputs (struct stm32_pwm * priv ,
515+ static int stm32_pwm_apply_breakinputs (struct stm32_pwm * priv )
516+ {
517+ unsigned int i ;
518+ int ret ;
519+
520+ for (i = 0 ; i < priv -> num_breakinputs ; i ++ ) {
521+ ret = stm32_pwm_set_breakinput (priv , & priv -> breakinputs [i ]);
522+ if (ret < 0 )
523+ return ret ;
524+ }
525+
526+ return 0 ;
527+ }
528+
529+ static int stm32_pwm_probe_breakinputs (struct stm32_pwm * priv ,
516530 struct device_node * np )
517531{
518- struct stm32_breakinput breakinput [ MAX_BREAKINPUT ] ;
519- int nb , ret , i , array_size ;
532+ int nb , ret , array_size ;
533+ unsigned int i ;
520534
521535 nb = of_property_count_elems_of_size (np , "st,breakinput" ,
522536 sizeof (struct stm32_breakinput ));
@@ -531,20 +545,21 @@ static int stm32_pwm_apply_breakinputs(struct stm32_pwm *priv,
531545 if (nb > MAX_BREAKINPUT )
532546 return - EINVAL ;
533547
548+ priv -> num_breakinputs = nb ;
534549 array_size = nb * sizeof (struct stm32_breakinput ) / sizeof (u32 );
535550 ret = of_property_read_u32_array (np , "st,breakinput" ,
536- (u32 * )breakinput , array_size );
551+ (u32 * )priv -> breakinputs , array_size );
537552 if (ret )
538553 return ret ;
539554
540- for (i = 0 ; i < nb && ! ret ; i ++ ) {
541- ret = stm32_pwm_set_breakinput (priv ,
542- breakinput [i ].index ,
543- breakinput [i ].level ,
544- breakinput [ i ]. filter ) ;
555+ for (i = 0 ; i < priv -> num_breakinputs ; i ++ ) {
556+ if (priv -> breakinputs [ i ]. index > 1 ||
557+ priv -> breakinputs [i ].level > 1 ||
558+ priv -> breakinputs [i ].filter > 15 )
559+ return - EINVAL ;
545560 }
546561
547- return ret ;
562+ return stm32_pwm_apply_breakinputs ( priv ) ;
548563}
549564
550565static void stm32_pwm_detect_complementary (struct stm32_pwm * priv )
@@ -614,7 +629,7 @@ static int stm32_pwm_probe(struct platform_device *pdev)
614629 if (!priv -> regmap || !priv -> clk )
615630 return - EINVAL ;
616631
617- ret = stm32_pwm_apply_breakinputs (priv , np );
632+ ret = stm32_pwm_probe_breakinputs (priv , np );
618633 if (ret )
619634 return ret ;
620635
@@ -647,6 +662,42 @@ static int stm32_pwm_remove(struct platform_device *pdev)
647662 return 0 ;
648663}
649664
665+ static int __maybe_unused stm32_pwm_suspend (struct device * dev )
666+ {
667+ struct stm32_pwm * priv = dev_get_drvdata (dev );
668+ unsigned int i ;
669+ u32 ccer , mask ;
670+
671+ /* Look for active channels */
672+ ccer = active_channels (priv );
673+
674+ for (i = 0 ; i < priv -> chip .npwm ; i ++ ) {
675+ mask = TIM_CCER_CC1E << (i * 4 );
676+ if (ccer & mask ) {
677+ dev_err (dev , "PWM %u still in use by consumer %s\n" ,
678+ i , priv -> chip .pwms [i ].label );
679+ return - EBUSY ;
680+ }
681+ }
682+
683+ return pinctrl_pm_select_sleep_state (dev );
684+ }
685+
686+ static int __maybe_unused stm32_pwm_resume (struct device * dev )
687+ {
688+ struct stm32_pwm * priv = dev_get_drvdata (dev );
689+ int ret ;
690+
691+ ret = pinctrl_pm_select_default_state (dev );
692+ if (ret )
693+ return ret ;
694+
695+ /* restore breakinput registers that may have been lost in low power */
696+ return stm32_pwm_apply_breakinputs (priv );
697+ }
698+
699+ static SIMPLE_DEV_PM_OPS (stm32_pwm_pm_ops , stm32_pwm_suspend , stm32_pwm_resume ) ;
700+
650701static const struct of_device_id stm32_pwm_of_match [] = {
651702 { .compatible = "st,stm32-pwm" , },
652703 { /* end node */ },
@@ -659,6 +710,7 @@ static struct platform_driver stm32_pwm_driver = {
659710 .driver = {
660711 .name = "stm32-pwm" ,
661712 .of_match_table = stm32_pwm_of_match ,
713+ .pm = & stm32_pwm_pm_ops ,
662714 },
663715};
664716module_platform_driver (stm32_pwm_driver );
0 commit comments