1010#include <linux/gpio/driver.h>
1111#include <linux/init.h>
1212#include <linux/interrupt.h>
13+ #include <linux/spinlock.h>
1314#include <linux/io.h>
1415#include <linux/module.h>
1516#include <linux/platform_device.h>
2122/* Maximum banks */
2223#define ZYNQ_GPIO_MAX_BANK 4
2324#define ZYNQMP_GPIO_MAX_BANK 6
25+ #define VERSAL_GPIO_MAX_BANK 4
26+ #define PMC_GPIO_MAX_BANK 5
27+ #define VERSAL_UNUSED_BANKS 2
2428
2529#define ZYNQ_GPIO_BANK0_NGPIO 32
2630#define ZYNQ_GPIO_BANK1_NGPIO 22
9599/* set to differentiate zynq from zynqmp, 0=zynqmp, 1=zynq */
96100#define ZYNQ_GPIO_QUIRK_IS_ZYNQ BIT(0)
97101#define GPIO_QUIRK_DATA_RO_BUG BIT(1)
102+ #define GPIO_QUIRK_VERSAL BIT(2)
98103
99104struct gpio_regs {
100105 u32 datamsw [ZYNQMP_GPIO_MAX_BANK ];
@@ -116,6 +121,7 @@ struct gpio_regs {
116121 * @irq: interrupt for the GPIO device
117122 * @p_data: pointer to platform data
118123 * @context: context registers
124+ * @dirlock: lock used for direction in/out synchronization
119125 */
120126struct zynq_gpio {
121127 struct gpio_chip chip ;
@@ -124,6 +130,7 @@ struct zynq_gpio {
124130 int irq ;
125131 const struct zynq_platform_data * p_data ;
126132 struct gpio_regs context ;
133+ spinlock_t dirlock ; /* lock */
127134};
128135
129136/**
@@ -196,6 +203,8 @@ static inline void zynq_gpio_get_bank_pin(unsigned int pin_num,
196203 gpio -> p_data -> bank_min [bank ];
197204 return ;
198205 }
206+ if (gpio -> p_data -> quirks & GPIO_QUIRK_VERSAL )
207+ bank = bank + VERSAL_UNUSED_BANKS ;
199208 }
200209
201210 /* default */
@@ -297,6 +306,7 @@ static int zynq_gpio_dir_in(struct gpio_chip *chip, unsigned int pin)
297306{
298307 u32 reg ;
299308 unsigned int bank_num , bank_pin_num ;
309+ unsigned long flags ;
300310 struct zynq_gpio * gpio = gpiochip_get_data (chip );
301311
302312 zynq_gpio_get_bank_pin (pin , & bank_num , & bank_pin_num , gpio );
@@ -310,9 +320,11 @@ static int zynq_gpio_dir_in(struct gpio_chip *chip, unsigned int pin)
310320 return - EINVAL ;
311321
312322 /* clear the bit in direction mode reg to set the pin as input */
323+ spin_lock_irqsave (& gpio -> dirlock , flags );
313324 reg = readl_relaxed (gpio -> base_addr + ZYNQ_GPIO_DIRM_OFFSET (bank_num ));
314325 reg &= ~BIT (bank_pin_num );
315326 writel_relaxed (reg , gpio -> base_addr + ZYNQ_GPIO_DIRM_OFFSET (bank_num ));
327+ spin_unlock_irqrestore (& gpio -> dirlock , flags );
316328
317329 return 0 ;
318330}
@@ -334,11 +346,13 @@ static int zynq_gpio_dir_out(struct gpio_chip *chip, unsigned int pin,
334346{
335347 u32 reg ;
336348 unsigned int bank_num , bank_pin_num ;
349+ unsigned long flags ;
337350 struct zynq_gpio * gpio = gpiochip_get_data (chip );
338351
339352 zynq_gpio_get_bank_pin (pin , & bank_num , & bank_pin_num , gpio );
340353
341354 /* set the GPIO pin as output */
355+ spin_lock_irqsave (& gpio -> dirlock , flags );
342356 reg = readl_relaxed (gpio -> base_addr + ZYNQ_GPIO_DIRM_OFFSET (bank_num ));
343357 reg |= BIT (bank_pin_num );
344358 writel_relaxed (reg , gpio -> base_addr + ZYNQ_GPIO_DIRM_OFFSET (bank_num ));
@@ -347,6 +361,7 @@ static int zynq_gpio_dir_out(struct gpio_chip *chip, unsigned int pin,
347361 reg = readl_relaxed (gpio -> base_addr + ZYNQ_GPIO_OUTEN_OFFSET (bank_num ));
348362 reg |= BIT (bank_pin_num );
349363 writel_relaxed (reg , gpio -> base_addr + ZYNQ_GPIO_OUTEN_OFFSET (bank_num ));
364+ spin_unlock_irqrestore (& gpio -> dirlock , flags );
350365
351366 /* set the state of the pin */
352367 zynq_gpio_set_value (chip , pin , state );
@@ -647,6 +662,8 @@ static void zynq_gpio_irqhandler(struct irq_desc *desc)
647662 int_enb = readl_relaxed (gpio -> base_addr +
648663 ZYNQ_GPIO_INTMASK_OFFSET (bank_num ));
649664 zynq_gpio_handle_bank_irq (gpio , bank_num , int_sts & ~int_enb );
665+ if (gpio -> p_data -> quirks & GPIO_QUIRK_VERSAL )
666+ bank_num = bank_num + VERSAL_UNUSED_BANKS ;
650667 }
651668
652669 chained_irq_exit (irqchip , desc );
@@ -676,6 +693,8 @@ static void zynq_gpio_save_context(struct zynq_gpio *gpio)
676693 gpio -> context .int_any [bank_num ] =
677694 readl_relaxed (gpio -> base_addr +
678695 ZYNQ_GPIO_INTANY_OFFSET (bank_num ));
696+ if (gpio -> p_data -> quirks & GPIO_QUIRK_VERSAL )
697+ bank_num = bank_num + VERSAL_UNUSED_BANKS ;
679698 }
680699}
681700
@@ -707,6 +726,8 @@ static void zynq_gpio_restore_context(struct zynq_gpio *gpio)
707726 writel_relaxed (~(gpio -> context .int_en [bank_num ]),
708727 gpio -> base_addr +
709728 ZYNQ_GPIO_INTEN_OFFSET (bank_num ));
729+ if (gpio -> p_data -> quirks & GPIO_QUIRK_VERSAL )
730+ bank_num = bank_num + VERSAL_UNUSED_BANKS ;
710731 }
711732}
712733
@@ -715,6 +736,9 @@ static int __maybe_unused zynq_gpio_suspend(struct device *dev)
715736 struct zynq_gpio * gpio = dev_get_drvdata (dev );
716737 struct irq_data * data = irq_get_irq_data (gpio -> irq );
717738
739+ if (!device_may_wakeup (dev ))
740+ disable_irq (gpio -> irq );
741+
718742 if (!irqd_is_wakeup_set (data )) {
719743 zynq_gpio_save_context (gpio );
720744 return pm_runtime_force_suspend (dev );
@@ -729,6 +753,9 @@ static int __maybe_unused zynq_gpio_resume(struct device *dev)
729753 struct irq_data * data = irq_get_irq_data (gpio -> irq );
730754 int ret ;
731755
756+ if (!device_may_wakeup (dev ))
757+ enable_irq (gpio -> irq );
758+
732759 if (!irqd_is_wakeup_set (data )) {
733760 ret = pm_runtime_force_resume (dev );
734761 zynq_gpio_restore_context (gpio );
@@ -778,6 +805,31 @@ static const struct dev_pm_ops zynq_gpio_dev_pm_ops = {
778805 zynq_gpio_runtime_resume , NULL )
779806};
780807
808+ static const struct zynq_platform_data versal_gpio_def = {
809+ .label = "versal_gpio" ,
810+ .quirks = GPIO_QUIRK_VERSAL ,
811+ .ngpio = 58 ,
812+ .max_bank = VERSAL_GPIO_MAX_BANK ,
813+ .bank_min [0 ] = 0 ,
814+ .bank_max [0 ] = 25 , /* 0 to 25 are connected to MIOs (26 pins) */
815+ .bank_min [3 ] = 26 ,
816+ .bank_max [3 ] = 57 , /* Bank 3 is connected to FMIOs (32 pins) */
817+ };
818+
819+ static const struct zynq_platform_data pmc_gpio_def = {
820+ .label = "pmc_gpio" ,
821+ .ngpio = 116 ,
822+ .max_bank = PMC_GPIO_MAX_BANK ,
823+ .bank_min [0 ] = 0 ,
824+ .bank_max [0 ] = 25 , /* 0 to 25 are connected to MIOs (26 pins) */
825+ .bank_min [1 ] = 26 ,
826+ .bank_max [1 ] = 51 , /* Bank 1 are connected to MIOs (26 pins) */
827+ .bank_min [3 ] = 52 ,
828+ .bank_max [3 ] = 83 , /* Bank 3 is connected to EMIOs (32 pins) */
829+ .bank_min [4 ] = 84 ,
830+ .bank_max [4 ] = 115 , /* Bank 4 is connected to EMIOs (32 pins) */
831+ };
832+
781833static const struct zynq_platform_data zynqmp_gpio_def = {
782834 .label = "zynqmp_gpio" ,
783835 .quirks = GPIO_QUIRK_DATA_RO_BUG ,
@@ -815,6 +867,8 @@ static const struct zynq_platform_data zynq_gpio_def = {
815867static const struct of_device_id zynq_gpio_of_match [] = {
816868 { .compatible = "xlnx,zynq-gpio-1.0" , .data = & zynq_gpio_def },
817869 { .compatible = "xlnx,zynqmp-gpio-1.0" , .data = & zynqmp_gpio_def },
870+ { .compatible = "xlnx,versal-gpio-1.0" , .data = & versal_gpio_def },
871+ { .compatible = "xlnx,pmc-gpio-1.0" , .data = & pmc_gpio_def },
818872 { /* end of table */ }
819873};
820874MODULE_DEVICE_TABLE (of , zynq_gpio_of_match );
@@ -876,7 +930,8 @@ static int zynq_gpio_probe(struct platform_device *pdev)
876930 /* Retrieve GPIO clock */
877931 gpio -> clk = devm_clk_get (& pdev -> dev , NULL );
878932 if (IS_ERR (gpio -> clk )) {
879- dev_err (& pdev -> dev , "input clock not found.\n" );
933+ if (PTR_ERR (gpio -> clk ) != - EPROBE_DEFER )
934+ dev_err (& pdev -> dev , "input clock not found.\n" );
880935 return PTR_ERR (gpio -> clk );
881936 }
882937 ret = clk_prepare_enable (gpio -> clk );
@@ -885,16 +940,21 @@ static int zynq_gpio_probe(struct platform_device *pdev)
885940 return ret ;
886941 }
887942
943+ spin_lock_init (& gpio -> dirlock );
944+
888945 pm_runtime_set_active (& pdev -> dev );
889946 pm_runtime_enable (& pdev -> dev );
890947 ret = pm_runtime_get_sync (& pdev -> dev );
891948 if (ret < 0 )
892949 goto err_pm_dis ;
893950
894951 /* disable interrupts for all banks */
895- for (bank_num = 0 ; bank_num < gpio -> p_data -> max_bank ; bank_num ++ )
952+ for (bank_num = 0 ; bank_num < gpio -> p_data -> max_bank ; bank_num ++ ) {
896953 writel_relaxed (ZYNQ_GPIO_IXR_DISABLE_ALL , gpio -> base_addr +
897954 ZYNQ_GPIO_INTDIS_OFFSET (bank_num ));
955+ if (gpio -> p_data -> quirks & GPIO_QUIRK_VERSAL )
956+ bank_num = bank_num + VERSAL_UNUSED_BANKS ;
957+ }
898958
899959 /* Set up the GPIO irqchip */
900960 girq = & chip -> irq ;
@@ -919,6 +979,8 @@ static int zynq_gpio_probe(struct platform_device *pdev)
919979 goto err_pm_put ;
920980 }
921981
982+ irq_set_status_flags (gpio -> irq , IRQ_DISABLE_UNLAZY );
983+ device_init_wakeup (& pdev -> dev , 1 );
922984 pm_runtime_put (& pdev -> dev );
923985
924986 return 0 ;
0 commit comments