2020#include <linux/io.h>
2121#include <linux/module.h>
2222#include <linux/platform_device.h>
23+ #include <linux/pm.h>
24+ #include <linux/pm_runtime.h>
2325#include <linux/slab.h>
2426#include <linux/sched.h>
2527#include <linux/input/samsung-keypad.h>
@@ -63,10 +65,12 @@ enum samsung_keypad_type {
6365
6466struct samsung_keypad {
6567 struct input_dev * input_dev ;
68+ struct platform_device * pdev ;
6669 struct clk * clk ;
6770 void __iomem * base ;
6871 wait_queue_head_t wait ;
6972 bool stopped ;
73+ bool wake_enabled ;
7074 int irq ;
7175 unsigned int row_shift ;
7276 unsigned int rows ;
@@ -158,6 +162,8 @@ static irqreturn_t samsung_keypad_irq(int irq, void *dev_id)
158162 unsigned int val ;
159163 bool key_down ;
160164
165+ pm_runtime_get_sync (& keypad -> pdev -> dev );
166+
161167 do {
162168 val = readl (keypad -> base + SAMSUNG_KEYIFSTSCLR );
163169 /* Clear interrupt. */
@@ -172,13 +178,17 @@ static irqreturn_t samsung_keypad_irq(int irq, void *dev_id)
172178
173179 } while (key_down && !keypad -> stopped );
174180
181+ pm_runtime_put_sync (& keypad -> pdev -> dev );
182+
175183 return IRQ_HANDLED ;
176184}
177185
178186static void samsung_keypad_start (struct samsung_keypad * keypad )
179187{
180188 unsigned int val ;
181189
190+ pm_runtime_get_sync (& keypad -> pdev -> dev );
191+
182192 /* Tell IRQ thread that it may poll the device. */
183193 keypad -> stopped = false;
184194
@@ -191,12 +201,16 @@ static void samsung_keypad_start(struct samsung_keypad *keypad)
191201
192202 /* KEYIFCOL reg clear. */
193203 writel (0 , keypad -> base + SAMSUNG_KEYIFCOL );
204+
205+ pm_runtime_put_sync (& keypad -> pdev -> dev );
194206}
195207
196208static void samsung_keypad_stop (struct samsung_keypad * keypad )
197209{
198210 unsigned int val ;
199211
212+ pm_runtime_get_sync (& keypad -> pdev -> dev );
213+
200214 /* Signal IRQ thread to stop polling and disable the handler. */
201215 keypad -> stopped = true;
202216 wake_up (& keypad -> wait );
@@ -217,6 +231,8 @@ static void samsung_keypad_stop(struct samsung_keypad *keypad)
217231 * re-enable the handler.
218232 */
219233 enable_irq (keypad -> irq );
234+
235+ pm_runtime_put_sync (& keypad -> pdev -> dev );
220236}
221237
222238static int samsung_keypad_open (struct input_dev * input_dev )
@@ -298,9 +314,11 @@ static int __devinit samsung_keypad_probe(struct platform_device *pdev)
298314 }
299315
300316 keypad -> input_dev = input_dev ;
317+ keypad -> pdev = pdev ;
301318 keypad -> row_shift = row_shift ;
302319 keypad -> rows = pdata -> rows ;
303320 keypad -> cols = pdata -> cols ;
321+ keypad -> stopped = true;
304322 init_waitqueue_head (& keypad -> wait );
305323
306324 input_dev -> name = pdev -> name ;
@@ -337,16 +355,21 @@ static int __devinit samsung_keypad_probe(struct platform_device *pdev)
337355 goto err_put_clk ;
338356 }
339357
358+ device_init_wakeup (& pdev -> dev , pdata -> wakeup );
359+ platform_set_drvdata (pdev , keypad );
360+ pm_runtime_enable (& pdev -> dev );
361+
340362 error = input_register_device (keypad -> input_dev );
341363 if (error )
342364 goto err_free_irq ;
343365
344- device_init_wakeup (& pdev -> dev , pdata -> wakeup );
345- platform_set_drvdata (pdev , keypad );
346366 return 0 ;
347367
348368err_free_irq :
349369 free_irq (keypad -> irq , keypad );
370+ pm_runtime_disable (& pdev -> dev );
371+ device_init_wakeup (& pdev -> dev , 0 );
372+ platform_set_drvdata (pdev , NULL );
350373err_put_clk :
351374 clk_put (keypad -> clk );
352375err_unmap_base :
@@ -362,6 +385,7 @@ static int __devexit samsung_keypad_remove(struct platform_device *pdev)
362385{
363386 struct samsung_keypad * keypad = platform_get_drvdata (pdev );
364387
388+ pm_runtime_disable (& pdev -> dev );
365389 device_init_wakeup (& pdev -> dev , 0 );
366390 platform_set_drvdata (pdev , NULL );
367391
@@ -381,23 +405,69 @@ static int __devexit samsung_keypad_remove(struct platform_device *pdev)
381405 return 0 ;
382406}
383407
408+ #ifdef CONFIG_PM_RUNTIME
409+ static int samsung_keypad_runtime_suspend (struct device * dev )
410+ {
411+ struct platform_device * pdev = to_platform_device (dev );
412+ struct samsung_keypad * keypad = platform_get_drvdata (pdev );
413+ unsigned int val ;
414+ int error ;
415+
416+ if (keypad -> stopped )
417+ return 0 ;
418+
419+ /* This may fail on some SoCs due to lack of controller support */
420+ error = enable_irq_wake (keypad -> irq );
421+ if (!error )
422+ keypad -> wake_enabled = true;
423+
424+ val = readl (keypad -> base + SAMSUNG_KEYIFCON );
425+ val |= SAMSUNG_KEYIFCON_WAKEUPEN ;
426+ writel (val , keypad -> base + SAMSUNG_KEYIFCON );
427+
428+ clk_disable (keypad -> clk );
429+
430+ return 0 ;
431+ }
432+
433+ static int samsung_keypad_runtime_resume (struct device * dev )
434+ {
435+ struct platform_device * pdev = to_platform_device (dev );
436+ struct samsung_keypad * keypad = platform_get_drvdata (pdev );
437+ unsigned int val ;
438+
439+ if (keypad -> stopped )
440+ return 0 ;
441+
442+ clk_enable (keypad -> clk );
443+
444+ val = readl (keypad -> base + SAMSUNG_KEYIFCON );
445+ val &= ~SAMSUNG_KEYIFCON_WAKEUPEN ;
446+ writel (val , keypad -> base + SAMSUNG_KEYIFCON );
447+
448+ if (keypad -> wake_enabled )
449+ disable_irq_wake (keypad -> irq );
450+
451+ return 0 ;
452+ }
453+ #endif
454+
384455#ifdef CONFIG_PM_SLEEP
385456static void samsung_keypad_toggle_wakeup (struct samsung_keypad * keypad ,
386457 bool enable )
387458{
388- struct device * dev = keypad -> input_dev -> dev .parent ;
389459 unsigned int val ;
390460
391461 clk_enable (keypad -> clk );
392462
393463 val = readl (keypad -> base + SAMSUNG_KEYIFCON );
394464 if (enable ) {
395465 val |= SAMSUNG_KEYIFCON_WAKEUPEN ;
396- if (device_may_wakeup (dev ))
466+ if (device_may_wakeup (& keypad -> pdev -> dev ))
397467 enable_irq_wake (keypad -> irq );
398468 } else {
399469 val &= ~SAMSUNG_KEYIFCON_WAKEUPEN ;
400- if (device_may_wakeup (dev ))
470+ if (device_may_wakeup (& keypad -> pdev -> dev ))
401471 disable_irq_wake (keypad -> irq );
402472 }
403473 writel (val , keypad -> base + SAMSUNG_KEYIFCON );
@@ -442,8 +512,11 @@ static int samsung_keypad_resume(struct device *dev)
442512}
443513#endif
444514
445- static SIMPLE_DEV_PM_OPS (samsung_keypad_pm_ops ,
446- samsung_keypad_suspend , samsung_keypad_resume ) ;
515+ static const struct dev_pm_ops samsung_keypad_pm_ops = {
516+ SET_SYSTEM_SLEEP_PM_OPS (samsung_keypad_suspend , samsung_keypad_resume )
517+ SET_RUNTIME_PM_OPS (samsung_keypad_runtime_suspend ,
518+ samsung_keypad_runtime_resume , NULL )
519+ };
447520
448521static struct platform_device_id samsung_keypad_driver_ids [] = {
449522 {
0 commit comments