]>
Commit | Line | Data |
---|---|---|
8eec8a11 HL |
1 | /* |
2 | * Power button driver for Medfield. | |
3 | * | |
4 | * Copyright (C) 2010 Intel Corp | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; version 2 of the License. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
18 | */ | |
19 | ||
20 | #include <linux/module.h> | |
21 | #include <linux/init.h> | |
22 | #include <linux/interrupt.h> | |
23 | #include <linux/slab.h> | |
24 | #include <linux/platform_device.h> | |
25 | #include <linux/input.h> | |
7714567c | 26 | #include <linux/mfd/intel_msic.h> |
daea5a65 | 27 | #include <linux/pm_wakeirq.h> |
8eec8a11 HL |
28 | |
29 | #define DRIVER_NAME "msic_power_btn" | |
30 | ||
b9e06694 | 31 | #define MSIC_PB_LEVEL (1 << 3) /* 1 - release, 0 - press */ |
8eec8a11 | 32 | |
7714567c MD |
33 | /* |
34 | * MSIC document ti_datasheet defines the 1st bit reg 0x21 is used to mask | |
35 | * power button interrupt | |
36 | */ | |
37 | #define MSIC_PWRBTNM (1 << 0) | |
38 | ||
8eec8a11 HL |
39 | static irqreturn_t mfld_pb_isr(int irq, void *dev_id) |
40 | { | |
b9e06694 | 41 | struct input_dev *input = dev_id; |
8eec8a11 HL |
42 | int ret; |
43 | u8 pbstat; | |
44 | ||
7714567c MD |
45 | ret = intel_msic_reg_read(INTEL_MSIC_PBSTATUS, &pbstat); |
46 | dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat); | |
47 | ||
b9e06694 AP |
48 | if (ret < 0) { |
49 | dev_err(input->dev.parent, "Read error %d while reading" | |
50 | " MSIC_PB_STATUS\n", ret); | |
51 | } else { | |
52 | input_event(input, EV_KEY, KEY_POWER, | |
53 | !(pbstat & MSIC_PB_LEVEL)); | |
54 | input_sync(input); | |
55 | } | |
8eec8a11 HL |
56 | |
57 | return IRQ_HANDLED; | |
58 | } | |
59 | ||
b859f159 | 60 | static int mfld_pb_probe(struct platform_device *pdev) |
8eec8a11 | 61 | { |
8eec8a11 | 62 | struct input_dev *input; |
b9e06694 | 63 | int irq = platform_get_irq(pdev, 0); |
8eec8a11 HL |
64 | int error; |
65 | ||
8eec8a11 HL |
66 | if (irq < 0) |
67 | return -EINVAL; | |
68 | ||
8eec8a11 | 69 | input = input_allocate_device(); |
b222cca6 | 70 | if (!input) |
b9e06694 | 71 | return -ENOMEM; |
8eec8a11 | 72 | |
8eec8a11 HL |
73 | input->name = pdev->name; |
74 | input->phys = "power-button/input0"; | |
75 | input->id.bustype = BUS_HOST; | |
76 | input->dev.parent = &pdev->dev; | |
77 | ||
78 | input_set_capability(input, EV_KEY, KEY_POWER); | |
79 | ||
5a00b6c2 | 80 | error = request_threaded_irq(irq, NULL, mfld_pb_isr, IRQF_ONESHOT, |
daea5a65 | 81 | DRIVER_NAME, input); |
8eec8a11 | 82 | if (error) { |
b9e06694 AP |
83 | dev_err(&pdev->dev, "Unable to request irq %d for mfld power" |
84 | "button\n", irq); | |
85 | goto err_free_input; | |
8eec8a11 HL |
86 | } |
87 | ||
daea5a65 SH |
88 | device_init_wakeup(&pdev->dev, true); |
89 | dev_pm_set_wake_irq(&pdev->dev, irq); | |
90 | ||
8eec8a11 HL |
91 | error = input_register_device(input); |
92 | if (error) { | |
b9e06694 AP |
93 | dev_err(&pdev->dev, "Unable to register input dev, error " |
94 | "%d\n", error); | |
8eec8a11 HL |
95 | goto err_free_irq; |
96 | } | |
97 | ||
b9e06694 | 98 | platform_set_drvdata(pdev, input); |
7714567c MD |
99 | |
100 | /* | |
101 | * SCU firmware might send power button interrupts to IA core before | |
102 | * kernel boots and doesn't get EOI from IA core. The first bit of | |
103 | * MSIC reg 0x21 is kept masked, and SCU firmware doesn't send new | |
104 | * power interrupt to Android kernel. Unmask the bit when probing | |
105 | * power button in kernel. | |
106 | * There is a very narrow race between irq handler and power button | |
107 | * initialization. The race happens rarely. So we needn't worry | |
108 | * about it. | |
109 | */ | |
110 | error = intel_msic_reg_update(INTEL_MSIC_IRQLVL1MSK, 0, MSIC_PWRBTNM); | |
111 | if (error) { | |
112 | dev_err(&pdev->dev, "Unable to clear power button interrupt, " | |
113 | "error: %d\n", error); | |
114 | goto err_free_irq; | |
115 | } | |
116 | ||
8eec8a11 HL |
117 | return 0; |
118 | ||
119 | err_free_irq: | |
b9e06694 AP |
120 | free_irq(irq, input); |
121 | err_free_input: | |
8eec8a11 | 122 | input_free_device(input); |
8eec8a11 HL |
123 | return error; |
124 | } | |
125 | ||
b859f159 | 126 | static int mfld_pb_remove(struct platform_device *pdev) |
8eec8a11 | 127 | { |
b9e06694 AP |
128 | struct input_dev *input = platform_get_drvdata(pdev); |
129 | int irq = platform_get_irq(pdev, 0); | |
8eec8a11 | 130 | |
daea5a65 SH |
131 | dev_pm_clear_wake_irq(&pdev->dev); |
132 | device_init_wakeup(&pdev->dev, false); | |
b9e06694 AP |
133 | free_irq(irq, input); |
134 | input_unregister_device(input); | |
b9e06694 | 135 | |
8eec8a11 HL |
136 | return 0; |
137 | } | |
138 | ||
139 | static struct platform_driver mfld_pb_driver = { | |
140 | .driver = { | |
141 | .name = DRIVER_NAME, | |
8eec8a11 HL |
142 | }, |
143 | .probe = mfld_pb_probe, | |
b859f159 | 144 | .remove = mfld_pb_remove, |
8eec8a11 HL |
145 | }; |
146 | ||
73d99a22 | 147 | module_platform_driver(mfld_pb_driver); |
8eec8a11 HL |
148 | |
149 | MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>"); | |
150 | MODULE_DESCRIPTION("Intel Medfield Power Button Driver"); | |
151 | MODULE_LICENSE("GPL v2"); | |
152 | MODULE_ALIAS("platform:" DRIVER_NAME); |