88 */
99#include <linux/clk.h>
1010#include <linux/gpio/driver.h>
11+ #include <linux/hwspinlock.h>
1112#include <linux/io.h>
1213#include <linux/irq.h>
1314#include <linux/mfd/syscon.h>
5152#define gpio_range_to_bank (chip ) \
5253 container_of(chip, struct stm32_gpio_bank, range)
5354
55+ #define HWSPINLOCK_TIMEOUT 5 /* msec */
56+
5457static const char * const stm32_gpio_functions [] = {
5558 "gpio" , "af0" , "af1" ,
5659 "af2" , "af3" , "af4" ,
@@ -91,6 +94,7 @@ struct stm32_pinctrl {
9194 struct irq_domain * domain ;
9295 struct regmap * regmap ;
9396 struct regmap_field * irqmux [STM32_GPIO_PINS_PER_BANK ];
97+ struct hwspinlock * hwlock ;
9498};
9599
96100static inline int stm32_gpio_pin (int gpio )
@@ -576,14 +580,24 @@ static int stm32_pmx_get_func_groups(struct pinctrl_dev *pctldev,
576580static void stm32_pmx_set_mode (struct stm32_gpio_bank * bank ,
577581 int pin , u32 mode , u32 alt )
578582{
583+ struct stm32_pinctrl * pctl = dev_get_drvdata (bank -> gpio_chip .parent );
579584 u32 val ;
580585 int alt_shift = (pin % 8 ) * 4 ;
581586 int alt_offset = STM32_GPIO_AFRL + (pin / 8 ) * 4 ;
582587 unsigned long flags ;
588+ int err = 0 ;
583589
584590 clk_enable (bank -> clk );
585591 spin_lock_irqsave (& bank -> lock , flags );
586592
593+ if (pctl -> hwlock )
594+ err = hwspin_lock_timeout (pctl -> hwlock , HWSPINLOCK_TIMEOUT );
595+
596+ if (err ) {
597+ dev_err (pctl -> dev , "Can't get hwspinlock\n" );
598+ goto unlock ;
599+ }
600+
587601 val = readl_relaxed (bank -> base + alt_offset );
588602 val &= ~GENMASK (alt_shift + 3 , alt_shift );
589603 val |= (alt << alt_shift );
@@ -594,6 +608,10 @@ static void stm32_pmx_set_mode(struct stm32_gpio_bank *bank,
594608 val |= mode << (pin * 2 );
595609 writel_relaxed (val , bank -> base + STM32_GPIO_MODER );
596610
611+ if (pctl -> hwlock )
612+ hwspin_unlock (pctl -> hwlock );
613+
614+ unlock :
597615 spin_unlock_irqrestore (& bank -> lock , flags );
598616 clk_disable (bank -> clk );
599617}
@@ -683,17 +701,31 @@ static const struct pinmux_ops stm32_pmx_ops = {
683701static void stm32_pconf_set_driving (struct stm32_gpio_bank * bank ,
684702 unsigned offset , u32 drive )
685703{
704+ struct stm32_pinctrl * pctl = dev_get_drvdata (bank -> gpio_chip .parent );
686705 unsigned long flags ;
687706 u32 val ;
707+ int err = 0 ;
688708
689709 clk_enable (bank -> clk );
690710 spin_lock_irqsave (& bank -> lock , flags );
691711
712+ if (pctl -> hwlock )
713+ err = hwspin_lock_timeout (pctl -> hwlock , HWSPINLOCK_TIMEOUT );
714+
715+ if (err ) {
716+ dev_err (pctl -> dev , "Can't get hwspinlock\n" );
717+ goto unlock ;
718+ }
719+
692720 val = readl_relaxed (bank -> base + STM32_GPIO_TYPER );
693721 val &= ~BIT (offset );
694722 val |= drive << offset ;
695723 writel_relaxed (val , bank -> base + STM32_GPIO_TYPER );
696724
725+ if (pctl -> hwlock )
726+ hwspin_unlock (pctl -> hwlock );
727+
728+ unlock :
697729 spin_unlock_irqrestore (& bank -> lock , flags );
698730 clk_disable (bank -> clk );
699731}
@@ -719,17 +751,31 @@ static u32 stm32_pconf_get_driving(struct stm32_gpio_bank *bank,
719751static void stm32_pconf_set_speed (struct stm32_gpio_bank * bank ,
720752 unsigned offset , u32 speed )
721753{
754+ struct stm32_pinctrl * pctl = dev_get_drvdata (bank -> gpio_chip .parent );
722755 unsigned long flags ;
723756 u32 val ;
757+ int err = 0 ;
724758
725759 clk_enable (bank -> clk );
726760 spin_lock_irqsave (& bank -> lock , flags );
727761
762+ if (pctl -> hwlock )
763+ err = hwspin_lock_timeout (pctl -> hwlock , HWSPINLOCK_TIMEOUT );
764+
765+ if (err ) {
766+ dev_err (pctl -> dev , "Can't get hwspinlock\n" );
767+ goto unlock ;
768+ }
769+
728770 val = readl_relaxed (bank -> base + STM32_GPIO_SPEEDR );
729771 val &= ~GENMASK (offset * 2 + 1 , offset * 2 );
730772 val |= speed << (offset * 2 );
731773 writel_relaxed (val , bank -> base + STM32_GPIO_SPEEDR );
732774
775+ if (pctl -> hwlock )
776+ hwspin_unlock (pctl -> hwlock );
777+
778+ unlock :
733779 spin_unlock_irqrestore (& bank -> lock , flags );
734780 clk_disable (bank -> clk );
735781}
@@ -755,17 +801,31 @@ static u32 stm32_pconf_get_speed(struct stm32_gpio_bank *bank,
755801static void stm32_pconf_set_bias (struct stm32_gpio_bank * bank ,
756802 unsigned offset , u32 bias )
757803{
804+ struct stm32_pinctrl * pctl = dev_get_drvdata (bank -> gpio_chip .parent );
758805 unsigned long flags ;
759806 u32 val ;
807+ int err = 0 ;
760808
761809 clk_enable (bank -> clk );
762810 spin_lock_irqsave (& bank -> lock , flags );
763811
812+ if (pctl -> hwlock )
813+ err = hwspin_lock_timeout (pctl -> hwlock , HWSPINLOCK_TIMEOUT );
814+
815+ if (err ) {
816+ dev_err (pctl -> dev , "Can't get hwspinlock\n" );
817+ goto unlock ;
818+ }
819+
764820 val = readl_relaxed (bank -> base + STM32_GPIO_PUPDR );
765821 val &= ~GENMASK (offset * 2 + 1 , offset * 2 );
766822 val |= bias << (offset * 2 );
767823 writel_relaxed (val , bank -> base + STM32_GPIO_PUPDR );
768824
825+ if (pctl -> hwlock )
826+ hwspin_unlock (pctl -> hwlock );
827+
828+ unlock :
769829 spin_unlock_irqrestore (& bank -> lock , flags );
770830 clk_disable (bank -> clk );
771831}
@@ -1140,7 +1200,7 @@ int stm32_pctl_probe(struct platform_device *pdev)
11401200 struct device * dev = & pdev -> dev ;
11411201 struct stm32_pinctrl * pctl ;
11421202 struct pinctrl_pin_desc * pins ;
1143- int i , ret , banks = 0 ;
1203+ int i , ret , hwlock_id , banks = 0 ;
11441204
11451205 if (!np )
11461206 return - EINVAL ;
@@ -1160,6 +1220,15 @@ int stm32_pctl_probe(struct platform_device *pdev)
11601220
11611221 platform_set_drvdata (pdev , pctl );
11621222
1223+ /* hwspinlock is optional */
1224+ hwlock_id = of_hwspin_lock_get_id (pdev -> dev .of_node , 0 );
1225+ if (hwlock_id < 0 ) {
1226+ if (hwlock_id == - EPROBE_DEFER )
1227+ return hwlock_id ;
1228+ } else {
1229+ pctl -> hwlock = hwspin_lock_request_specific (hwlock_id );
1230+ }
1231+
11631232 pctl -> dev = dev ;
11641233 pctl -> match_data = match -> data ;
11651234 ret = stm32_pctrl_build_state (pdev );
0 commit comments