99#include <linux/clk.h>
1010#include <linux/gpio/driver.h>
1111#include <linux/io.h>
12+ #include <linux/interrupt.h>
1213#include <linux/module.h>
1314#include <linux/of_device.h>
15+ #include <linux/of_irq.h>
1416#include <linux/pinctrl/pinconf-generic.h>
1517#include <linux/pinctrl/pinconf.h>
1618#include <linux/pinctrl/pinctrl.h>
8991#define PIN (n ) (0x0800 + 0x10 + (n))
9092#define IOLH (n ) (0x1000 + (n) * 8)
9193#define IEN (n ) (0x1800 + (n) * 8)
94+ #define ISEL (n ) (0x2c80 + (n) * 8)
9295#define PWPR (0x3014)
9396#define SD_CH (n ) (0x3000 + (n) * 4)
9497#define QSPI (0x3008)
112115#define RZG2L_PIN_ID_TO_PORT_OFFSET (id ) (RZG2L_PIN_ID_TO_PORT(id) + 0x10)
113116#define RZG2L_PIN_ID_TO_PIN (id ) ((id) % RZG2L_PINS_PER_PORT)
114117
118+ #define RZG2L_TINT_MAX_INTERRUPT 32
119+ #define RZG2L_TINT_IRQ_START_INDEX 9
120+ #define RZG2L_PACK_HWIRQ (t , i ) (((t) << 16) | (i))
121+
115122struct rzg2l_dedicated_configs {
116123 const char * name ;
117124 u32 config ;
@@ -137,6 +144,9 @@ struct rzg2l_pinctrl {
137144
138145 struct gpio_chip gpio_chip ;
139146 struct pinctrl_gpio_range gpio_range ;
147+ DECLARE_BITMAP (tint_slot , RZG2L_TINT_MAX_INTERRUPT );
148+ spinlock_t bitmap_lock ;
149+ unsigned int hwirq [RZG2L_TINT_MAX_INTERRUPT ];
140150
141151 spinlock_t lock ;
142152};
@@ -883,8 +893,14 @@ static int rzg2l_gpio_get(struct gpio_chip *chip, unsigned int offset)
883893
884894static void rzg2l_gpio_free (struct gpio_chip * chip , unsigned int offset )
885895{
896+ unsigned int virq ;
897+
886898 pinctrl_gpio_free (chip -> base + offset );
887899
900+ virq = irq_find_mapping (chip -> irq .domain , offset );
901+ if (virq )
902+ irq_dispose_mapping (virq );
903+
888904 /*
889905 * Set the GPIO as an input to ensure that the next GPIO request won't
890906 * drive the GPIO pin as an output.
@@ -1104,14 +1120,221 @@ static struct {
11041120 }
11051121};
11061122
1123+ static int rzg2l_gpio_get_gpioint (unsigned int virq )
1124+ {
1125+ unsigned int gpioint ;
1126+ unsigned int i ;
1127+ u32 port , bit ;
1128+
1129+ port = virq / 8 ;
1130+ bit = virq % 8 ;
1131+
1132+ if (port >= ARRAY_SIZE (rzg2l_gpio_configs ) ||
1133+ bit >= RZG2L_GPIO_PORT_GET_PINCNT (rzg2l_gpio_configs [port ]))
1134+ return - EINVAL ;
1135+
1136+ gpioint = bit ;
1137+ for (i = 0 ; i < port ; i ++ )
1138+ gpioint += RZG2L_GPIO_PORT_GET_PINCNT (rzg2l_gpio_configs [i ]);
1139+
1140+ return gpioint ;
1141+ }
1142+
1143+ static void rzg2l_gpio_irq_disable (struct irq_data * d )
1144+ {
1145+ struct gpio_chip * gc = irq_data_get_irq_chip_data (d );
1146+ struct rzg2l_pinctrl * pctrl = container_of (gc , struct rzg2l_pinctrl , gpio_chip );
1147+ unsigned int hwirq = irqd_to_hwirq (d );
1148+ unsigned long flags ;
1149+ void __iomem * addr ;
1150+ u32 port ;
1151+ u8 bit ;
1152+
1153+ port = RZG2L_PIN_ID_TO_PORT (hwirq );
1154+ bit = RZG2L_PIN_ID_TO_PIN (hwirq );
1155+
1156+ addr = pctrl -> base + ISEL (port );
1157+ if (bit >= 4 ) {
1158+ bit -= 4 ;
1159+ addr += 4 ;
1160+ }
1161+
1162+ spin_lock_irqsave (& pctrl -> lock , flags );
1163+ writel (readl (addr ) & ~BIT (bit * 8 ), addr );
1164+ spin_unlock_irqrestore (& pctrl -> lock , flags );
1165+
1166+ gpiochip_disable_irq (gc , hwirq );
1167+ irq_chip_disable_parent (d );
1168+ }
1169+
1170+ static void rzg2l_gpio_irq_enable (struct irq_data * d )
1171+ {
1172+ struct gpio_chip * gc = irq_data_get_irq_chip_data (d );
1173+ struct rzg2l_pinctrl * pctrl = container_of (gc , struct rzg2l_pinctrl , gpio_chip );
1174+ unsigned int hwirq = irqd_to_hwirq (d );
1175+ unsigned long flags ;
1176+ void __iomem * addr ;
1177+ u32 port ;
1178+ u8 bit ;
1179+
1180+ gpiochip_enable_irq (gc , hwirq );
1181+
1182+ port = RZG2L_PIN_ID_TO_PORT (hwirq );
1183+ bit = RZG2L_PIN_ID_TO_PIN (hwirq );
1184+
1185+ addr = pctrl -> base + ISEL (port );
1186+ if (bit >= 4 ) {
1187+ bit -= 4 ;
1188+ addr += 4 ;
1189+ }
1190+
1191+ spin_lock_irqsave (& pctrl -> lock , flags );
1192+ writel (readl (addr ) | BIT (bit * 8 ), addr );
1193+ spin_unlock_irqrestore (& pctrl -> lock , flags );
1194+
1195+ irq_chip_enable_parent (d );
1196+ }
1197+
1198+ static int rzg2l_gpio_irq_set_type (struct irq_data * d , unsigned int type )
1199+ {
1200+ return irq_chip_set_type_parent (d , type );
1201+ }
1202+
1203+ static void rzg2l_gpio_irqc_eoi (struct irq_data * d )
1204+ {
1205+ irq_chip_eoi_parent (d );
1206+ }
1207+
1208+ static void rzg2l_gpio_irq_print_chip (struct irq_data * data , struct seq_file * p )
1209+ {
1210+ struct gpio_chip * gc = irq_data_get_irq_chip_data (data );
1211+
1212+ seq_printf (p , dev_name (gc -> parent ));
1213+ }
1214+
1215+ static const struct irq_chip rzg2l_gpio_irqchip = {
1216+ .name = "rzg2l-gpio" ,
1217+ .irq_disable = rzg2l_gpio_irq_disable ,
1218+ .irq_enable = rzg2l_gpio_irq_enable ,
1219+ .irq_mask = irq_chip_mask_parent ,
1220+ .irq_unmask = irq_chip_unmask_parent ,
1221+ .irq_set_type = rzg2l_gpio_irq_set_type ,
1222+ .irq_eoi = rzg2l_gpio_irqc_eoi ,
1223+ .irq_print_chip = rzg2l_gpio_irq_print_chip ,
1224+ .flags = IRQCHIP_IMMUTABLE ,
1225+ GPIOCHIP_IRQ_RESOURCE_HELPERS ,
1226+ };
1227+
1228+ static int rzg2l_gpio_child_to_parent_hwirq (struct gpio_chip * gc ,
1229+ unsigned int child ,
1230+ unsigned int child_type ,
1231+ unsigned int * parent ,
1232+ unsigned int * parent_type )
1233+ {
1234+ struct rzg2l_pinctrl * pctrl = gpiochip_get_data (gc );
1235+ unsigned long flags ;
1236+ int gpioint , irq ;
1237+
1238+ gpioint = rzg2l_gpio_get_gpioint (child );
1239+ if (gpioint < 0 )
1240+ return gpioint ;
1241+
1242+ spin_lock_irqsave (& pctrl -> bitmap_lock , flags );
1243+ irq = bitmap_find_free_region (pctrl -> tint_slot , RZG2L_TINT_MAX_INTERRUPT , get_order (1 ));
1244+ spin_unlock_irqrestore (& pctrl -> bitmap_lock , flags );
1245+ if (irq < 0 )
1246+ return - ENOSPC ;
1247+ pctrl -> hwirq [irq ] = child ;
1248+ irq += RZG2L_TINT_IRQ_START_INDEX ;
1249+
1250+ /* All these interrupts are level high in the CPU */
1251+ * parent_type = IRQ_TYPE_LEVEL_HIGH ;
1252+ * parent = RZG2L_PACK_HWIRQ (gpioint , irq );
1253+ return 0 ;
1254+ }
1255+
1256+ static int rzg2l_gpio_populate_parent_fwspec (struct gpio_chip * chip ,
1257+ union gpio_irq_fwspec * gfwspec ,
1258+ unsigned int parent_hwirq ,
1259+ unsigned int parent_type )
1260+ {
1261+ struct irq_fwspec * fwspec = & gfwspec -> fwspec ;
1262+
1263+ fwspec -> fwnode = chip -> irq .parent_domain -> fwnode ;
1264+ fwspec -> param_count = 2 ;
1265+ fwspec -> param [0 ] = parent_hwirq ;
1266+ fwspec -> param [1 ] = parent_type ;
1267+
1268+ return 0 ;
1269+ }
1270+
1271+ static void rzg2l_gpio_irq_domain_free (struct irq_domain * domain , unsigned int virq ,
1272+ unsigned int nr_irqs )
1273+ {
1274+ struct irq_data * d ;
1275+
1276+ d = irq_domain_get_irq_data (domain , virq );
1277+ if (d ) {
1278+ struct gpio_chip * gc = irq_data_get_irq_chip_data (d );
1279+ struct rzg2l_pinctrl * pctrl = container_of (gc , struct rzg2l_pinctrl , gpio_chip );
1280+ irq_hw_number_t hwirq = irqd_to_hwirq (d );
1281+ unsigned long flags ;
1282+ unsigned int i ;
1283+
1284+ for (i = 0 ; i < RZG2L_TINT_MAX_INTERRUPT ; i ++ ) {
1285+ if (pctrl -> hwirq [i ] == hwirq ) {
1286+ spin_lock_irqsave (& pctrl -> bitmap_lock , flags );
1287+ bitmap_release_region (pctrl -> tint_slot , i , get_order (1 ));
1288+ spin_unlock_irqrestore (& pctrl -> bitmap_lock , flags );
1289+ pctrl -> hwirq [i ] = 0 ;
1290+ break ;
1291+ }
1292+ }
1293+ }
1294+ irq_domain_free_irqs_common (domain , virq , nr_irqs );
1295+ }
1296+
1297+ static void rzg2l_init_irq_valid_mask (struct gpio_chip * gc ,
1298+ unsigned long * valid_mask ,
1299+ unsigned int ngpios )
1300+ {
1301+ struct rzg2l_pinctrl * pctrl = gpiochip_get_data (gc );
1302+ struct gpio_chip * chip = & pctrl -> gpio_chip ;
1303+ unsigned int offset ;
1304+
1305+ /* Forbid unused lines to be mapped as IRQs */
1306+ for (offset = 0 ; offset < chip -> ngpio ; offset ++ ) {
1307+ u32 port , bit ;
1308+
1309+ port = offset / 8 ;
1310+ bit = offset % 8 ;
1311+
1312+ if (port >= ARRAY_SIZE (rzg2l_gpio_configs ) ||
1313+ bit >= RZG2L_GPIO_PORT_GET_PINCNT (rzg2l_gpio_configs [port ]))
1314+ clear_bit (offset , valid_mask );
1315+ }
1316+ }
1317+
11071318static int rzg2l_gpio_register (struct rzg2l_pinctrl * pctrl )
11081319{
11091320 struct device_node * np = pctrl -> dev -> of_node ;
11101321 struct gpio_chip * chip = & pctrl -> gpio_chip ;
11111322 const char * name = dev_name (pctrl -> dev );
1323+ struct irq_domain * parent_domain ;
11121324 struct of_phandle_args of_args ;
1325+ struct device_node * parent_np ;
1326+ struct gpio_irq_chip * girq ;
11131327 int ret ;
11141328
1329+ parent_np = of_irq_find_parent (np );
1330+ if (!parent_np )
1331+ return - ENXIO ;
1332+
1333+ parent_domain = irq_find_host (parent_np );
1334+ of_node_put (parent_np );
1335+ if (!parent_domain )
1336+ return - EPROBE_DEFER ;
1337+
11151338 ret = of_parse_phandle_with_fixed_args (np , "gpio-ranges" , 3 , 0 , & of_args );
11161339 if (ret ) {
11171340 dev_err (pctrl -> dev , "Unable to parse gpio-ranges\n" );
@@ -1138,6 +1361,15 @@ static int rzg2l_gpio_register(struct rzg2l_pinctrl *pctrl)
11381361 chip -> base = -1 ;
11391362 chip -> ngpio = of_args .args [2 ];
11401363
1364+ girq = & chip -> irq ;
1365+ gpio_irq_chip_set_chip (girq , & rzg2l_gpio_irqchip );
1366+ girq -> fwnode = of_node_to_fwnode (np );
1367+ girq -> parent_domain = parent_domain ;
1368+ girq -> child_to_parent_hwirq = rzg2l_gpio_child_to_parent_hwirq ;
1369+ girq -> populate_parent_alloc_arg = rzg2l_gpio_populate_parent_fwspec ;
1370+ girq -> child_irq_domain_ops .free = rzg2l_gpio_irq_domain_free ;
1371+ girq -> init_valid_mask = rzg2l_init_irq_valid_mask ;
1372+
11411373 pctrl -> gpio_range .id = 0 ;
11421374 pctrl -> gpio_range .pin_base = 0 ;
11431375 pctrl -> gpio_range .base = 0 ;
@@ -1253,6 +1485,7 @@ static int rzg2l_pinctrl_probe(struct platform_device *pdev)
12531485 }
12541486
12551487 spin_lock_init (& pctrl -> lock );
1488+ spin_lock_init (& pctrl -> bitmap_lock );
12561489
12571490 platform_set_drvdata (pdev , pctrl );
12581491
0 commit comments