9
9
#include <linux/clk.h>
10
10
#include <linux/gpio/driver.h>
11
11
#include <linux/io.h>
12
+ #include <linux/interrupt.h>
12
13
#include <linux/module.h>
13
14
#include <linux/of_device.h>
15
+ #include <linux/of_irq.h>
14
16
#include <linux/pinctrl/pinconf-generic.h>
15
17
#include <linux/pinctrl/pinconf.h>
16
18
#include <linux/pinctrl/pinctrl.h>
89
91
#define PIN (n ) (0x0800 + 0x10 + (n))
90
92
#define IOLH (n ) (0x1000 + (n) * 8)
91
93
#define IEN (n ) (0x1800 + (n) * 8)
94
+ #define ISEL (n ) (0x2c80 + (n) * 8)
92
95
#define PWPR (0x3014)
93
96
#define SD_CH (n ) (0x3000 + (n) * 4)
94
97
#define QSPI (0x3008)
112
115
#define RZG2L_PIN_ID_TO_PORT_OFFSET (id ) (RZG2L_PIN_ID_TO_PORT(id) + 0x10)
113
116
#define RZG2L_PIN_ID_TO_PIN (id ) ((id) % RZG2L_PINS_PER_PORT)
114
117
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
+
115
122
struct rzg2l_dedicated_configs {
116
123
const char * name ;
117
124
u32 config ;
@@ -137,6 +144,9 @@ struct rzg2l_pinctrl {
137
144
138
145
struct gpio_chip gpio_chip ;
139
146
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 ];
140
150
141
151
spinlock_t lock ;
142
152
};
@@ -883,8 +893,14 @@ static int rzg2l_gpio_get(struct gpio_chip *chip, unsigned int offset)
883
893
884
894
static void rzg2l_gpio_free (struct gpio_chip * chip , unsigned int offset )
885
895
{
896
+ unsigned int virq ;
897
+
886
898
pinctrl_gpio_free (chip -> base + offset );
887
899
900
+ virq = irq_find_mapping (chip -> irq .domain , offset );
901
+ if (virq )
902
+ irq_dispose_mapping (virq );
903
+
888
904
/*
889
905
* Set the GPIO as an input to ensure that the next GPIO request won't
890
906
* drive the GPIO pin as an output.
@@ -1104,14 +1120,221 @@ static struct {
1104
1120
}
1105
1121
};
1106
1122
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
+
1107
1318
static int rzg2l_gpio_register (struct rzg2l_pinctrl * pctrl )
1108
1319
{
1109
1320
struct device_node * np = pctrl -> dev -> of_node ;
1110
1321
struct gpio_chip * chip = & pctrl -> gpio_chip ;
1111
1322
const char * name = dev_name (pctrl -> dev );
1323
+ struct irq_domain * parent_domain ;
1112
1324
struct of_phandle_args of_args ;
1325
+ struct device_node * parent_np ;
1326
+ struct gpio_irq_chip * girq ;
1113
1327
int ret ;
1114
1328
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
+
1115
1338
ret = of_parse_phandle_with_fixed_args (np , "gpio-ranges" , 3 , 0 , & of_args );
1116
1339
if (ret ) {
1117
1340
dev_err (pctrl -> dev , "Unable to parse gpio-ranges\n" );
@@ -1138,6 +1361,15 @@ static int rzg2l_gpio_register(struct rzg2l_pinctrl *pctrl)
1138
1361
chip -> base = -1 ;
1139
1362
chip -> ngpio = of_args .args [2 ];
1140
1363
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
+
1141
1373
pctrl -> gpio_range .id = 0 ;
1142
1374
pctrl -> gpio_range .pin_base = 0 ;
1143
1375
pctrl -> gpio_range .base = 0 ;
@@ -1253,6 +1485,7 @@ static int rzg2l_pinctrl_probe(struct platform_device *pdev)
1253
1485
}
1254
1486
1255
1487
spin_lock_init (& pctrl -> lock );
1488
+ spin_lock_init (& pctrl -> bitmap_lock );
1256
1489
1257
1490
platform_set_drvdata (pdev , pctrl );
1258
1491
0 commit comments