16
16
#include <linux/module.h>
17
17
#include <linux/of_device.h>
18
18
#include <linux/of_platform.h>
19
+ #include <linux/pm_runtime.h>
19
20
#include <linux/slab.h>
20
21
21
22
/* Register Offset Definitions */
@@ -278,6 +279,39 @@ static void xgpio_save_regs(struct xgpio_instance *chip)
278
279
chip -> gpio_dir [1 ]);
279
280
}
280
281
282
+ static int xgpio_request (struct gpio_chip * chip , unsigned int offset )
283
+ {
284
+ int ret ;
285
+
286
+ ret = pm_runtime_get_sync (chip -> parent );
287
+ /*
288
+ * If the device is already active pm_runtime_get() will return 1 on
289
+ * success, but gpio_request still needs to return 0.
290
+ */
291
+ return ret < 0 ? ret : 0 ;
292
+ }
293
+
294
+ static void xgpio_free (struct gpio_chip * chip , unsigned int offset )
295
+ {
296
+ pm_runtime_put (chip -> parent );
297
+ }
298
+
299
+ static int __maybe_unused xgpio_suspend (struct device * dev )
300
+ {
301
+ struct xgpio_instance * gpio = dev_get_drvdata (dev );
302
+ struct irq_data * data = irq_get_irq_data (gpio -> irq );
303
+
304
+ if (!data ) {
305
+ dev_err (dev , "irq_get_irq_data() failed\n" );
306
+ return - EINVAL ;
307
+ }
308
+
309
+ if (!irqd_is_wakeup_set (data ))
310
+ return pm_runtime_force_suspend (dev );
311
+
312
+ return 0 ;
313
+ }
314
+
281
315
/**
282
316
* xgpio_remove - Remove method for the GPIO device.
283
317
* @pdev: pointer to the platform device
@@ -290,6 +324,9 @@ static int xgpio_remove(struct platform_device *pdev)
290
324
{
291
325
struct xgpio_instance * gpio = platform_get_drvdata (pdev );
292
326
327
+ pm_runtime_get_sync (& pdev -> dev );
328
+ pm_runtime_put_noidle (& pdev -> dev );
329
+ pm_runtime_disable (& pdev -> dev );
293
330
clk_disable_unprepare (gpio -> clk );
294
331
295
332
return 0 ;
@@ -305,6 +342,46 @@ static void xgpio_irq_ack(struct irq_data *irq_data)
305
342
{
306
343
}
307
344
345
+ static int __maybe_unused xgpio_resume (struct device * dev )
346
+ {
347
+ struct xgpio_instance * gpio = dev_get_drvdata (dev );
348
+ struct irq_data * data = irq_get_irq_data (gpio -> irq );
349
+
350
+ if (!data ) {
351
+ dev_err (dev , "irq_get_irq_data() failed\n" );
352
+ return - EINVAL ;
353
+ }
354
+
355
+ if (!irqd_is_wakeup_set (data ))
356
+ return pm_runtime_force_resume (dev );
357
+
358
+ return 0 ;
359
+ }
360
+
361
+ static int __maybe_unused xgpio_runtime_suspend (struct device * dev )
362
+ {
363
+ struct platform_device * pdev = to_platform_device (dev );
364
+ struct xgpio_instance * gpio = platform_get_drvdata (pdev );
365
+
366
+ clk_disable (gpio -> clk );
367
+
368
+ return 0 ;
369
+ }
370
+
371
+ static int __maybe_unused xgpio_runtime_resume (struct device * dev )
372
+ {
373
+ struct platform_device * pdev = to_platform_device (dev );
374
+ struct xgpio_instance * gpio = platform_get_drvdata (pdev );
375
+
376
+ return clk_enable (gpio -> clk );
377
+ }
378
+
379
+ static const struct dev_pm_ops xgpio_dev_pm_ops = {
380
+ SET_SYSTEM_SLEEP_PM_OPS (xgpio_suspend , xgpio_resume )
381
+ SET_RUNTIME_PM_OPS (xgpio_runtime_suspend ,
382
+ xgpio_runtime_resume , NULL )
383
+ };
384
+
308
385
/**
309
386
* xgpio_irq_mask - Write the specified signal of the GPIO device.
310
387
* @irq_data: per IRQ and chip data passed down to chip functions
@@ -546,6 +623,8 @@ static int xgpio_probe(struct platform_device *pdev)
546
623
chip -> gc .of_gpio_n_cells = cells ;
547
624
chip -> gc .get = xgpio_get ;
548
625
chip -> gc .set = xgpio_set ;
626
+ chip -> gc .request = xgpio_request ;
627
+ chip -> gc .free = xgpio_free ;
549
628
chip -> gc .set_multiple = xgpio_set_multiple ;
550
629
551
630
chip -> gc .label = dev_name (& pdev -> dev );
@@ -565,6 +644,9 @@ static int xgpio_probe(struct platform_device *pdev)
565
644
dev_err (& pdev -> dev , "Failed to prepare clk\n" );
566
645
return status ;
567
646
}
647
+ pm_runtime_get_noresume (& pdev -> dev );
648
+ pm_runtime_set_active (& pdev -> dev );
649
+ pm_runtime_enable (& pdev -> dev );
568
650
569
651
xgpio_save_regs (chip );
570
652
@@ -595,7 +677,7 @@ static int xgpio_probe(struct platform_device *pdev)
595
677
GFP_KERNEL );
596
678
if (!girq -> parents ) {
597
679
status = - ENOMEM ;
598
- goto err_unprepare_clk ;
680
+ goto err_pm_put ;
599
681
}
600
682
girq -> parents [0 ] = chip -> irq ;
601
683
girq -> default_type = IRQ_TYPE_NONE ;
@@ -605,12 +687,15 @@ static int xgpio_probe(struct platform_device *pdev)
605
687
status = devm_gpiochip_add_data (& pdev -> dev , & chip -> gc , chip );
606
688
if (status ) {
607
689
dev_err (& pdev -> dev , "failed to add GPIO chip\n" );
608
- goto err_unprepare_clk ;
690
+ goto err_pm_put ;
609
691
}
610
692
693
+ pm_runtime_put (& pdev -> dev );
611
694
return 0 ;
612
695
613
- err_unprepare_clk :
696
+ err_pm_put :
697
+ pm_runtime_disable (& pdev -> dev );
698
+ pm_runtime_put_noidle (& pdev -> dev );
614
699
clk_disable_unprepare (chip -> clk );
615
700
return status ;
616
701
}
@@ -628,6 +713,7 @@ static struct platform_driver xgpio_plat_driver = {
628
713
.driver = {
629
714
.name = "gpio-xilinx" ,
630
715
.of_match_table = xgpio_of_match ,
716
+ .pm = & xgpio_dev_pm_ops ,
631
717
},
632
718
};
633
719
0 commit comments