blob: ac02a0b8bef37b660862091c4569bd4271ff4c3d [file] [log] [blame]
Hong Liu8eec8a12011-02-07 14:45:55 -05001/*
Andy Shevchenko48b44522017-01-19 18:39:42 +02002 * Power button driver for Intel MID platforms.
Hong Liu8eec8a12011-02-07 14:45:55 -05003 *
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>
Sudeep Holladaea5a62015-09-21 16:47:01 +010027#include <linux/pm_wakeirq.h>
Hong Liu8eec8a12011-02-07 14:45:55 -050028
Andy Shevchenko18934ec2017-01-19 18:39:43 +020029#include <asm/cpu_device_id.h>
30#include <asm/intel-family.h>
31
Hong Liu8eec8a12011-02-07 14:45:55 -050032#define DRIVER_NAME "msic_power_btn"
33
Ameya Palandeb9e06692011-04-06 17:44:37 +030034#define MSIC_PB_LEVEL (1 << 3) /* 1 - release, 0 - press */
Hong Liu8eec8a12011-02-07 14:45:55 -050035
Michael Demeter77145672012-01-26 17:40:27 +000036/*
37 * MSIC document ti_datasheet defines the 1st bit reg 0x21 is used to mask
38 * power button interrupt
39 */
40#define MSIC_PWRBTNM (1 << 0)
41
Andy Shevchenko18934ec2017-01-19 18:39:43 +020042struct mid_pb_ddata {
43 struct device *dev;
44 int irq;
45 struct input_dev *input;
46 int (*pbstat)(struct mid_pb_ddata *ddata, int *value);
Andy Shevchenko4b819c62017-01-19 18:39:44 +020047 int (*ack)(struct mid_pb_ddata *ddata);
Andy Shevchenko18934ec2017-01-19 18:39:43 +020048};
49
50static int mfld_pbstat(struct mid_pb_ddata *ddata, int *value)
Hong Liu8eec8a12011-02-07 14:45:55 -050051{
Andy Shevchenko18934ec2017-01-19 18:39:43 +020052 struct input_dev *input = ddata->input;
Hong Liu8eec8a12011-02-07 14:45:55 -050053 int ret;
54 u8 pbstat;
55
Michael Demeter77145672012-01-26 17:40:27 +000056 ret = intel_msic_reg_read(INTEL_MSIC_PBSTATUS, &pbstat);
Andy Shevchenko18934ec2017-01-19 18:39:43 +020057 if (ret)
58 return ret;
59
Michael Demeter77145672012-01-26 17:40:27 +000060 dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat);
61
Andy Shevchenko18934ec2017-01-19 18:39:43 +020062 *value = !(pbstat & MSIC_PB_LEVEL);
63 return 0;
64}
65
Andy Shevchenko4b819c62017-01-19 18:39:44 +020066static int mfld_ack(struct mid_pb_ddata *ddata)
67{
68 /*
69 * SCU firmware might send power button interrupts to IA core before
70 * kernel boots and doesn't get EOI from IA core. The first bit of
71 * MSIC reg 0x21 is kept masked, and SCU firmware doesn't send new
72 * power interrupt to Android kernel. Unmask the bit when probing
73 * power button in kernel.
74 * There is a very narrow race between irq handler and power button
75 * initialization. The race happens rarely. So we needn't worry
76 * about it.
77 */
78 return intel_msic_reg_update(INTEL_MSIC_IRQLVL1MSK, 0, MSIC_PWRBTNM);
79}
80
Andy Shevchenko18934ec2017-01-19 18:39:43 +020081static irqreturn_t mid_pb_isr(int irq, void *dev_id)
82{
83 struct mid_pb_ddata *ddata = dev_id;
84 struct input_dev *input = ddata->input;
85 int value;
86 int ret;
87
88 ret = ddata->pbstat(ddata, &value);
Ameya Palandeb9e06692011-04-06 17:44:37 +030089 if (ret < 0) {
90 dev_err(input->dev.parent, "Read error %d while reading"
91 " MSIC_PB_STATUS\n", ret);
92 } else {
Andy Shevchenko18934ec2017-01-19 18:39:43 +020093 input_event(input, EV_KEY, KEY_POWER, value);
Ameya Palandeb9e06692011-04-06 17:44:37 +030094 input_sync(input);
95 }
Hong Liu8eec8a12011-02-07 14:45:55 -050096
Andy Shevchenko553e9c12017-01-19 18:39:45 +020097 ddata->ack(ddata);
Hong Liu8eec8a12011-02-07 14:45:55 -050098 return IRQ_HANDLED;
99}
100
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200101static struct mid_pb_ddata mfld_ddata = {
102 .pbstat = mfld_pbstat,
Andy Shevchenko4b819c62017-01-19 18:39:44 +0200103 .ack = mfld_ack,
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200104};
105
106#define ICPU(model, ddata) \
107 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (kernel_ulong_t)&ddata }
108
109static const struct x86_cpu_id mid_pb_cpu_ids[] = {
110 ICPU(INTEL_FAM6_ATOM_PENWELL, mfld_ddata),
111 {}
112};
113
Andy Shevchenko48b44522017-01-19 18:39:42 +0200114static int mid_pb_probe(struct platform_device *pdev)
Hong Liu8eec8a12011-02-07 14:45:55 -0500115{
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200116 const struct x86_cpu_id *id;
117 struct mid_pb_ddata *ddata;
Hong Liu8eec8a12011-02-07 14:45:55 -0500118 struct input_dev *input;
Ameya Palandeb9e06692011-04-06 17:44:37 +0300119 int irq = platform_get_irq(pdev, 0);
Hong Liu8eec8a12011-02-07 14:45:55 -0500120 int error;
121
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200122 id = x86_match_cpu(mid_pb_cpu_ids);
123 if (!id)
124 return -ENODEV;
125
Hong Liu8eec8a12011-02-07 14:45:55 -0500126 if (irq < 0)
127 return -EINVAL;
128
Andy Shevchenko07d90892017-01-19 18:39:41 +0200129 input = devm_input_allocate_device(&pdev->dev);
Joe Perchesb222cca2013-10-23 12:14:52 -0700130 if (!input)
Ameya Palandeb9e06692011-04-06 17:44:37 +0300131 return -ENOMEM;
Hong Liu8eec8a12011-02-07 14:45:55 -0500132
Hong Liu8eec8a12011-02-07 14:45:55 -0500133 input->name = pdev->name;
134 input->phys = "power-button/input0";
135 input->id.bustype = BUS_HOST;
136 input->dev.parent = &pdev->dev;
137
138 input_set_capability(input, EV_KEY, KEY_POWER);
139
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200140 ddata = (struct mid_pb_ddata *)id->driver_data;
141 if (!ddata)
142 return -ENODATA;
143
144 ddata->dev = &pdev->dev;
145 ddata->irq = irq;
146 ddata->input = input;
147
Andy Shevchenko48b44522017-01-19 18:39:42 +0200148 error = devm_request_threaded_irq(&pdev->dev, irq, NULL, mid_pb_isr,
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200149 IRQF_ONESHOT, DRIVER_NAME, ddata);
Hong Liu8eec8a12011-02-07 14:45:55 -0500150 if (error) {
Andy Shevchenko48b44522017-01-19 18:39:42 +0200151 dev_err(&pdev->dev, "Unable to request irq %d for MID power"
Ameya Palandeb9e06692011-04-06 17:44:37 +0300152 "button\n", irq);
Andy Shevchenko07d90892017-01-19 18:39:41 +0200153 return error;
Hong Liu8eec8a12011-02-07 14:45:55 -0500154 }
155
156 error = input_register_device(input);
157 if (error) {
Ameya Palandeb9e06692011-04-06 17:44:37 +0300158 dev_err(&pdev->dev, "Unable to register input dev, error "
159 "%d\n", error);
Andy Shevchenko07d90892017-01-19 18:39:41 +0200160 return error;
Hong Liu8eec8a12011-02-07 14:45:55 -0500161 }
162
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200163 platform_set_drvdata(pdev, ddata);
Michael Demeter77145672012-01-26 17:40:27 +0000164
Andy Shevchenko4b819c62017-01-19 18:39:44 +0200165 error = ddata->ack(ddata);
Michael Demeter77145672012-01-26 17:40:27 +0000166 if (error) {
167 dev_err(&pdev->dev, "Unable to clear power button interrupt, "
168 "error: %d\n", error);
Andy Shevchenko07d90892017-01-19 18:39:41 +0200169 return error;
Michael Demeter77145672012-01-26 17:40:27 +0000170 }
171
Andy Shevchenko07d90892017-01-19 18:39:41 +0200172 device_init_wakeup(&pdev->dev, true);
173 dev_pm_set_wake_irq(&pdev->dev, irq);
Hong Liu8eec8a12011-02-07 14:45:55 -0500174
Andy Shevchenko07d90892017-01-19 18:39:41 +0200175 return 0;
Hong Liu8eec8a12011-02-07 14:45:55 -0500176}
177
Andy Shevchenko48b44522017-01-19 18:39:42 +0200178static int mid_pb_remove(struct platform_device *pdev)
Hong Liu8eec8a12011-02-07 14:45:55 -0500179{
Sudeep Holladaea5a62015-09-21 16:47:01 +0100180 dev_pm_clear_wake_irq(&pdev->dev);
181 device_init_wakeup(&pdev->dev, false);
Ameya Palandeb9e06692011-04-06 17:44:37 +0300182
Hong Liu8eec8a12011-02-07 14:45:55 -0500183 return 0;
184}
185
Andy Shevchenko48b44522017-01-19 18:39:42 +0200186static struct platform_driver mid_pb_driver = {
Hong Liu8eec8a12011-02-07 14:45:55 -0500187 .driver = {
188 .name = DRIVER_NAME,
Hong Liu8eec8a12011-02-07 14:45:55 -0500189 },
Andy Shevchenko48b44522017-01-19 18:39:42 +0200190 .probe = mid_pb_probe,
191 .remove = mid_pb_remove,
Hong Liu8eec8a12011-02-07 14:45:55 -0500192};
193
Andy Shevchenko48b44522017-01-19 18:39:42 +0200194module_platform_driver(mid_pb_driver);
Hong Liu8eec8a12011-02-07 14:45:55 -0500195
196MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>");
Andy Shevchenko48b44522017-01-19 18:39:42 +0200197MODULE_DESCRIPTION("Intel MID Power Button Driver");
Hong Liu8eec8a12011-02-07 14:45:55 -0500198MODULE_LICENSE("GPL v2");
199MODULE_ALIAS("platform:" DRIVER_NAME);