1818#include <linux/module.h>
1919#include <linux/mutex.h>
2020#include <linux/of_device.h>
21+ #include <linux/of_irq.h>
2122#include <linux/platform_device.h>
2223#include <linux/pm_runtime.h>
2324#include <linux/spinlock.h>
6566
6667#define RZT2H_MAX_SAFETY_PORTS 12
6768
69+ #define RZT2H_INTERRUPTS_START 16
70+ #define RZT2H_INTERRUPTS_NUM 17
71+
6872struct rzt2h_pinctrl_data {
6973 unsigned int n_port_pins ;
7074 const u8 * port_pin_configs ;
@@ -80,9 +84,11 @@ struct rzt2h_pinctrl {
8084 struct device * dev ;
8185 struct gpio_chip gpio_chip ;
8286 struct pinctrl_gpio_range gpio_range ;
87+ DECLARE_BITMAP (used_irqs , RZT2H_INTERRUPTS_NUM );
8388 spinlock_t lock ; /* lock read/write registers */
8489 struct mutex mutex ; /* serialize adding groups and functions */
8590 bool safety_port_enabled ;
91+ atomic_t wakeup_path ;
8692};
8793
8894#define RZT2H_GET_BASE (pctrl , port ) \
@@ -644,14 +650,185 @@ static const char * const rzt2h_gpio_names[] = {
644650 "P35_0" , "P35_1" , "P35_2" , "P35_3" , "P35_4" , "P35_5" , "P35_6" , "P35_7" ,
645651};
646652
653+ /*
654+ * Interrupts 0-15 are for INTCPUn, which are not exposed externally.
655+ * Interrupts 16-31 are for IRQn. SEI is 32.
656+ * This table matches the information found in User Manual's Section
657+ * 17.5, Multiplexed Pin Configurations, Tables 17.5 to 17.40, on the
658+ * Interrupt rows.
659+ * RZ/N2H has the same GPIO to IRQ mapping, except for the pins which
660+ * are not present.
661+ */
662+ static const u8 rzt2h_gpio_irq_map [] = {
663+ 32 , 16 , 17 , 18 , 19 , 0 , 20 , 21 ,
664+ 22 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
665+ 23 , 24 , 25 , 26 , 27 , 0 , 0 , 0 ,
666+ 0 , 0 , 28 , 29 , 30 , 31 , 0 , 0 ,
667+ 0 , 0 , 0 , 0 , 0 , 32 , 16 , 17 ,
668+ 18 , 19 , 20 , 21 , 22 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, 0 , 28 , 29 , 30 , 31 , 0 ,
697+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 30 ,
698+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
699+ };
700+
701+ static void rzt2h_gpio_irq_disable (struct irq_data * d )
702+ {
703+ struct gpio_chip * gc = irq_data_get_irq_chip_data (d );
704+ unsigned int hwirq = irqd_to_hwirq (d );
705+
706+ irq_chip_disable_parent (d );
707+ gpiochip_disable_irq (gc , hwirq );
708+ }
709+
710+ static void rzt2h_gpio_irq_enable (struct irq_data * d )
711+ {
712+ struct gpio_chip * gc = irq_data_get_irq_chip_data (d );
713+ unsigned int hwirq = irqd_to_hwirq (d );
714+
715+ gpiochip_enable_irq (gc , hwirq );
716+ irq_chip_enable_parent (d );
717+ }
718+
719+ static int rzt2h_gpio_irq_set_wake (struct irq_data * d , unsigned int on )
720+ {
721+ struct gpio_chip * gc = irq_data_get_irq_chip_data (d );
722+ struct rzt2h_pinctrl * pctrl = container_of (gc , struct rzt2h_pinctrl , gpio_chip );
723+ int ret ;
724+
725+ ret = irq_chip_set_wake_parent (d , on );
726+ if (ret )
727+ return ret ;
728+
729+ /*
730+ * If any of the IRQs are in use, put the entire pin controller on the
731+ * device wakeup path.
732+ */
733+ if (on )
734+ atomic_inc (& pctrl -> wakeup_path );
735+ else
736+ atomic_dec (& pctrl -> wakeup_path );
737+
738+ return 0 ;
739+ }
740+
741+ static const struct irq_chip rzt2h_gpio_irqchip = {
742+ .name = "rzt2h-gpio" ,
743+ .irq_disable = rzt2h_gpio_irq_disable ,
744+ .irq_enable = rzt2h_gpio_irq_enable ,
745+ .irq_mask = irq_chip_mask_parent ,
746+ .irq_unmask = irq_chip_unmask_parent ,
747+ .irq_set_type = irq_chip_set_type_parent ,
748+ .irq_set_wake = rzt2h_gpio_irq_set_wake ,
749+ .irq_eoi = irq_chip_eoi_parent ,
750+ .irq_set_affinity = irq_chip_set_affinity_parent ,
751+ .flags = IRQCHIP_IMMUTABLE ,
752+ GPIOCHIP_IRQ_RESOURCE_HELPERS ,
753+ };
754+
755+ static int rzt2h_gpio_child_to_parent_hwirq (struct gpio_chip * gc ,
756+ unsigned int child ,
757+ unsigned int child_type ,
758+ unsigned int * parent ,
759+ unsigned int * parent_type )
760+ {
761+ struct rzt2h_pinctrl * pctrl = gpiochip_get_data (gc );
762+ u8 port = RZT2H_PIN_ID_TO_PORT (child );
763+ u8 pin = RZT2H_PIN_ID_TO_PIN (child );
764+ u8 parent_irq ;
765+
766+ parent_irq = rzt2h_gpio_irq_map [child ];
767+ if (parent_irq < RZT2H_INTERRUPTS_START )
768+ return - EINVAL ;
769+
770+ if (test_and_set_bit (parent_irq - RZT2H_INTERRUPTS_START ,
771+ pctrl -> used_irqs ))
772+ return - EBUSY ;
773+
774+ rzt2h_pinctrl_set_pfc_mode (pctrl , port , pin , PFC_FUNC_INTERRUPT );
775+
776+ * parent = parent_irq ;
777+ * parent_type = child_type ;
778+
779+ return 0 ;
780+ }
781+
782+ static void rzt2h_gpio_irq_domain_free (struct irq_domain * domain , unsigned int virq ,
783+ unsigned int nr_irqs )
784+ {
785+ struct irq_data * d = irq_domain_get_irq_data (domain , virq );
786+ struct gpio_chip * gc = irq_data_get_irq_chip_data (d );
787+ struct rzt2h_pinctrl * pctrl = container_of (gc , struct rzt2h_pinctrl , gpio_chip );
788+ irq_hw_number_t hwirq = irqd_to_hwirq (d );
789+ u8 port = RZT2H_PIN_ID_TO_PORT (hwirq );
790+ u8 pin = RZT2H_PIN_ID_TO_PIN (hwirq );
791+
792+ if (test_and_clear_bit (hwirq - RZT2H_INTERRUPTS_START , pctrl -> used_irqs ))
793+ rzt2h_pinctrl_set_gpio_en (pctrl , port , pin , false);
794+
795+ irq_domain_free_irqs_common (domain , virq , nr_irqs );
796+ }
797+
798+ static void rzt2h_gpio_init_irq_valid_mask (struct gpio_chip * gc ,
799+ unsigned long * valid_mask ,
800+ unsigned int ngpios )
801+ {
802+ struct rzt2h_pinctrl * pctrl = gpiochip_get_data (gc );
803+ unsigned int offset ;
804+
805+ for (offset = 0 ; offset < ngpios ; offset ++ ) {
806+ if (!rzt2h_gpio_irq_map [offset ] || rzt2h_validate_pin (pctrl , offset ))
807+ clear_bit (offset , valid_mask );
808+ }
809+ }
810+
647811static int rzt2h_gpio_register (struct rzt2h_pinctrl * pctrl )
648812{
649813 struct pinctrl_gpio_range * range = & pctrl -> gpio_range ;
650814 struct gpio_chip * chip = & pctrl -> gpio_chip ;
815+ struct device_node * np = pctrl -> dev -> of_node ;
816+ struct irq_domain * parent_domain ;
651817 struct device * dev = pctrl -> dev ;
652818 struct of_phandle_args of_args ;
819+ struct device_node * parent_np ;
820+ struct gpio_irq_chip * girq ;
653821 int ret ;
654822
823+ parent_np = of_irq_find_parent (np );
824+ if (!parent_np )
825+ return - ENXIO ;
826+
827+ parent_domain = irq_find_host (parent_np );
828+ of_node_put (parent_np );
829+ if (!parent_domain )
830+ return - EPROBE_DEFER ;
831+
655832 ret = of_parse_phandle_with_fixed_args (dev -> of_node , "gpio-ranges" , 3 , 0 , & of_args );
656833 if (ret )
657834 return dev_err_probe (dev , ret , "Unable to parse gpio-ranges\n" );
@@ -675,6 +852,17 @@ static int rzt2h_gpio_register(struct rzt2h_pinctrl *pctrl)
675852 chip -> set = rzt2h_gpio_set ;
676853 chip -> label = dev_name (dev );
677854
855+ if (of_property_present (np , "interrupt-controller" )) {
856+ girq = & chip -> irq ;
857+ gpio_irq_chip_set_chip (girq , & rzt2h_gpio_irqchip );
858+ girq -> fwnode = dev_fwnode (pctrl -> dev );
859+ girq -> parent_domain = parent_domain ;
860+ girq -> child_to_parent_hwirq = rzt2h_gpio_child_to_parent_hwirq ;
861+ girq -> populate_parent_alloc_arg = gpiochip_populate_parent_fwspec_twocell ;
862+ girq -> child_irq_domain_ops .free = rzt2h_gpio_irq_domain_free ;
863+ girq -> init_valid_mask = rzt2h_gpio_init_irq_valid_mask ;
864+ }
865+
678866 range -> id = 0 ;
679867 range -> pin_base = 0 ;
680868 range -> base = 0 ;
@@ -819,10 +1007,25 @@ static const struct of_device_id rzt2h_pinctrl_of_table[] = {
8191007 { /* sentinel */ }
8201008};
8211009
1010+ static int rzt2h_pinctrl_suspend_noirq (struct device * dev )
1011+ {
1012+ struct rzt2h_pinctrl * pctrl = dev_get_drvdata (dev );
1013+
1014+ if (atomic_read (& pctrl -> wakeup_path ))
1015+ device_set_wakeup_path (dev );
1016+
1017+ return 0 ;
1018+ }
1019+
1020+ static const struct dev_pm_ops rzt2h_pinctrl_pm_ops = {
1021+ NOIRQ_SYSTEM_SLEEP_PM_OPS (rzt2h_pinctrl_suspend_noirq , NULL )
1022+ };
1023+
8221024static struct platform_driver rzt2h_pinctrl_driver = {
8231025 .driver = {
8241026 .name = DRV_NAME ,
8251027 .of_match_table = of_match_ptr (rzt2h_pinctrl_of_table ),
1028+ .pm = pm_sleep_ptr (& rzt2h_pinctrl_pm_ops ),
8261029 .suppress_bind_attrs = true,
8271030 },
8281031 .probe = rzt2h_pinctrl_probe ,
0 commit comments