blob: 22606d6b2af3faeed925a00fb7488ccab36c8156 [file] [log] [blame]
Hong Liu8eec8a12011-02-07 14:45:55 -05001/*
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>
Michael Demeter77145672012-01-26 17:40:27 +000026#include <linux/mfd/intel_msic.h>
Hong Liu8eec8a12011-02-07 14:45:55 -050027
28#define DRIVER_NAME "msic_power_btn"
29
Ameya Palandeb9e06692011-04-06 17:44:37 +030030#define MSIC_PB_LEVEL (1 << 3) /* 1 - release, 0 - press */
Hong Liu8eec8a12011-02-07 14:45:55 -050031
Michael Demeter77145672012-01-26 17:40:27 +000032/*
33 * MSIC document ti_datasheet defines the 1st bit reg 0x21 is used to mask
34 * power button interrupt
35 */
36#define MSIC_PWRBTNM (1 << 0)
37
Hong Liu8eec8a12011-02-07 14:45:55 -050038static irqreturn_t mfld_pb_isr(int irq, void *dev_id)
39{
Ameya Palandeb9e06692011-04-06 17:44:37 +030040 struct input_dev *input = dev_id;
Hong Liu8eec8a12011-02-07 14:45:55 -050041 int ret;
42 u8 pbstat;
43
Michael Demeter77145672012-01-26 17:40:27 +000044 ret = intel_msic_reg_read(INTEL_MSIC_PBSTATUS, &pbstat);
45 dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat);
46
Ameya Palandeb9e06692011-04-06 17:44:37 +030047 if (ret < 0) {
48 dev_err(input->dev.parent, "Read error %d while reading"
49 " MSIC_PB_STATUS\n", ret);
50 } else {
51 input_event(input, EV_KEY, KEY_POWER,
52 !(pbstat & MSIC_PB_LEVEL));
53 input_sync(input);
54 }
Hong Liu8eec8a12011-02-07 14:45:55 -050055
56 return IRQ_HANDLED;
57}
58
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -080059static int mfld_pb_probe(struct platform_device *pdev)
Hong Liu8eec8a12011-02-07 14:45:55 -050060{
Hong Liu8eec8a12011-02-07 14:45:55 -050061 struct input_dev *input;
Ameya Palandeb9e06692011-04-06 17:44:37 +030062 int irq = platform_get_irq(pdev, 0);
Hong Liu8eec8a12011-02-07 14:45:55 -050063 int error;
64
Hong Liu8eec8a12011-02-07 14:45:55 -050065 if (irq < 0)
66 return -EINVAL;
67
Hong Liu8eec8a12011-02-07 14:45:55 -050068 input = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -070069 if (!input)
Ameya Palandeb9e06692011-04-06 17:44:37 +030070 return -ENOMEM;
Hong Liu8eec8a12011-02-07 14:45:55 -050071
Hong Liu8eec8a12011-02-07 14:45:55 -050072 input->name = pdev->name;
73 input->phys = "power-button/input0";
74 input->id.bustype = BUS_HOST;
75 input->dev.parent = &pdev->dev;
76
77 input_set_capability(input, EV_KEY, KEY_POWER);
78
Yong Wangab27a202012-05-04 14:02:44 -070079 error = request_threaded_irq(irq, NULL, mfld_pb_isr, IRQF_NO_SUSPEND,
Ameya Palandeb9e06692011-04-06 17:44:37 +030080 DRIVER_NAME, input);
Hong Liu8eec8a12011-02-07 14:45:55 -050081 if (error) {
Ameya Palandeb9e06692011-04-06 17:44:37 +030082 dev_err(&pdev->dev, "Unable to request irq %d for mfld power"
83 "button\n", irq);
84 goto err_free_input;
Hong Liu8eec8a12011-02-07 14:45:55 -050085 }
86
87 error = input_register_device(input);
88 if (error) {
Ameya Palandeb9e06692011-04-06 17:44:37 +030089 dev_err(&pdev->dev, "Unable to register input dev, error "
90 "%d\n", error);
Hong Liu8eec8a12011-02-07 14:45:55 -050091 goto err_free_irq;
92 }
93
Ameya Palandeb9e06692011-04-06 17:44:37 +030094 platform_set_drvdata(pdev, input);
Michael Demeter77145672012-01-26 17:40:27 +000095
96 /*
97 * SCU firmware might send power button interrupts to IA core before
98 * kernel boots and doesn't get EOI from IA core. The first bit of
99 * MSIC reg 0x21 is kept masked, and SCU firmware doesn't send new
100 * power interrupt to Android kernel. Unmask the bit when probing
101 * power button in kernel.
102 * There is a very narrow race between irq handler and power button
103 * initialization. The race happens rarely. So we needn't worry
104 * about it.
105 */
106 error = intel_msic_reg_update(INTEL_MSIC_IRQLVL1MSK, 0, MSIC_PWRBTNM);
107 if (error) {
108 dev_err(&pdev->dev, "Unable to clear power button interrupt, "
109 "error: %d\n", error);
110 goto err_free_irq;
111 }
112
Hong Liu8eec8a12011-02-07 14:45:55 -0500113 return 0;
114
115err_free_irq:
Ameya Palandeb9e06692011-04-06 17:44:37 +0300116 free_irq(irq, input);
117err_free_input:
Hong Liu8eec8a12011-02-07 14:45:55 -0500118 input_free_device(input);
Hong Liu8eec8a12011-02-07 14:45:55 -0500119 return error;
120}
121
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800122static int mfld_pb_remove(struct platform_device *pdev)
Hong Liu8eec8a12011-02-07 14:45:55 -0500123{
Ameya Palandeb9e06692011-04-06 17:44:37 +0300124 struct input_dev *input = platform_get_drvdata(pdev);
125 int irq = platform_get_irq(pdev, 0);
Hong Liu8eec8a12011-02-07 14:45:55 -0500126
Ameya Palandeb9e06692011-04-06 17:44:37 +0300127 free_irq(irq, input);
128 input_unregister_device(input);
Ameya Palandeb9e06692011-04-06 17:44:37 +0300129
Hong Liu8eec8a12011-02-07 14:45:55 -0500130 return 0;
131}
132
133static struct platform_driver mfld_pb_driver = {
134 .driver = {
135 .name = DRIVER_NAME,
Hong Liu8eec8a12011-02-07 14:45:55 -0500136 },
137 .probe = mfld_pb_probe,
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800138 .remove = mfld_pb_remove,
Hong Liu8eec8a12011-02-07 14:45:55 -0500139};
140
Axel Lin73d99a22011-11-26 12:14:37 +0800141module_platform_driver(mfld_pb_driver);
Hong Liu8eec8a12011-02-07 14:45:55 -0500142
143MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>");
144MODULE_DESCRIPTION("Intel Medfield Power Button Driver");
145MODULE_LICENSE("GPL v2");
146MODULE_ALIAS("platform:" DRIVER_NAME);