Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
twl4030-pwrbutton.c
Go to the documentation of this file.
1 
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/errno.h>
27 #include <linux/input.h>
28 #include <linux/interrupt.h>
29 #include <linux/platform_device.h>
30 #include <linux/i2c/twl.h>
31 
32 #define PWR_PWRON_IRQ (1 << 0)
33 
34 #define STS_HW_CONDITIONS 0xf
35 
36 static irqreturn_t powerbutton_irq(int irq, void *_pwr)
37 {
38  struct input_dev *pwr = _pwr;
39  int err;
40  u8 value;
41 
44  if (!err) {
45  pm_wakeup_event(pwr->dev.parent, 0);
46  input_report_key(pwr, KEY_POWER, value & PWR_PWRON_IRQ);
47  input_sync(pwr);
48  } else {
49  dev_err(pwr->dev.parent, "twl4030: i2c error %d while reading"
50  " TWL4030 PM_MASTER STS_HW_CONDITIONS register\n", err);
51  }
52 
53  return IRQ_HANDLED;
54 }
55 
56 static int __init twl4030_pwrbutton_probe(struct platform_device *pdev)
57 {
58  struct input_dev *pwr;
59  int irq = platform_get_irq(pdev, 0);
60  int err;
61 
62  pwr = input_allocate_device();
63  if (!pwr) {
64  dev_dbg(&pdev->dev, "Can't allocate power button\n");
65  return -ENOMEM;
66  }
67 
68  pwr->evbit[0] = BIT_MASK(EV_KEY);
69  pwr->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
70  pwr->name = "twl4030_pwrbutton";
71  pwr->phys = "twl4030_pwrbutton/input0";
72  pwr->dev.parent = &pdev->dev;
73 
74  err = request_threaded_irq(irq, NULL, powerbutton_irq,
76  "twl4030_pwrbutton", pwr);
77  if (err < 0) {
78  dev_dbg(&pdev->dev, "Can't get IRQ for pwrbutton: %d\n", err);
79  goto free_input_dev;
80  }
81 
82  err = input_register_device(pwr);
83  if (err) {
84  dev_dbg(&pdev->dev, "Can't register power button: %d\n", err);
85  goto free_irq;
86  }
87 
88  platform_set_drvdata(pdev, pwr);
89 
90  return 0;
91 
92 free_irq:
93  free_irq(irq, pwr);
94 free_input_dev:
95  input_free_device(pwr);
96  return err;
97 }
98 
99 static int __exit twl4030_pwrbutton_remove(struct platform_device *pdev)
100 {
101  struct input_dev *pwr = platform_get_drvdata(pdev);
102  int irq = platform_get_irq(pdev, 0);
103 
104  free_irq(irq, pwr);
105  input_unregister_device(pwr);
106 
107  return 0;
108 }
109 
110 static struct platform_driver twl4030_pwrbutton_driver = {
111  .remove = __exit_p(twl4030_pwrbutton_remove),
112  .driver = {
113  .name = "twl4030_pwrbutton",
114  .owner = THIS_MODULE,
115  },
116 };
117 
118 static int __init twl4030_pwrbutton_init(void)
119 {
120  return platform_driver_probe(&twl4030_pwrbutton_driver,
121  twl4030_pwrbutton_probe);
122 }
123 module_init(twl4030_pwrbutton_init);
124 
125 static void __exit twl4030_pwrbutton_exit(void)
126 {
127  platform_driver_unregister(&twl4030_pwrbutton_driver);
128 }
129 module_exit(twl4030_pwrbutton_exit);
130 
131 MODULE_ALIAS("platform:twl4030_pwrbutton");
132 MODULE_DESCRIPTION("Triton2 Power Button");
133 MODULE_LICENSE("GPL");
134 MODULE_AUTHOR("Peter De Schrijver <[email protected]>");
135 MODULE_AUTHOR("Felipe Balbi <[email protected]>");
136