blob: 596ac9f3e89d0166e104bb3ba417fbf92ab68909 [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
97 return IRQ_HANDLED;
98}
99
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200100static struct mid_pb_ddata mfld_ddata = {
101 .pbstat = mfld_pbstat,
Andy Shevchenko4b819c62017-01-19 18:39:44 +0200102 .ack = mfld_ack,
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200103};
104
105#define ICPU(model, ddata) \
106 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (kernel_ulong_t)&ddata }
107
108static const struct x86_cpu_id mid_pb_cpu_ids[] = {
109 ICPU(INTEL_FAM6_ATOM_PENWELL, mfld_ddata),
110 {}
111};
112
Andy Shevchenko48b44522017-01-19 18:39:42 +0200113static int mid_pb_probe(struct platform_device *pdev)
Hong Liu8eec8a12011-02-07 14:45:55 -0500114{
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200115 const struct x86_cpu_id *id;
116 struct mid_pb_ddata *ddata;
Hong Liu8eec8a12011-02-07 14:45:55 -0500117 struct input_dev *input;
Ameya Palandeb9e06692011-04-06 17:44:37 +0300118 int irq = platform_get_irq(pdev, 0);
Hong Liu8eec8a12011-02-07 14:45:55 -0500119 int error;
120
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200121 id = x86_match_cpu(mid_pb_cpu_ids);
122 if (!id)
123 return -ENODEV;
124
Hong Liu8eec8a12011-02-07 14:45:55 -0500125 if (irq < 0)
126 return -EINVAL;
127
Andy Shevchenko07d90892017-01-19 18:39:41 +0200128 input = devm_input_allocate_device(&pdev->dev);
Joe Perchesb222cca2013-10-23 12:14:52 -0700129 if (!input)
Ameya Palandeb9e06692011-04-06 17:44:37 +0300130 return -ENOMEM;
Hong Liu8eec8a12011-02-07 14:45:55 -0500131
Hong Liu8eec8a12011-02-07 14:45:55 -0500132 input->name = pdev->name;
133 input->phys = "power-button/input0";
134 input->id.bustype = BUS_HOST;
135 input->dev.parent = &pdev->dev;
136
137 input_set_capability(input, EV_KEY, KEY_POWER);
138
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200139 ddata = (struct mid_pb_ddata *)id->driver_data;
140 if (!ddata)
141 return -ENODATA;
142
143 ddata->dev = &pdev->dev;
144 ddata->irq = irq;
145 ddata->input = input;
146
Andy Shevchenko48b44522017-01-19 18:39:42 +0200147 error = devm_request_threaded_irq(&pdev->dev, irq, NULL, mid_pb_isr,
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200148 IRQF_ONESHOT, DRIVER_NAME, ddata);
Hong Liu8eec8a12011-02-07 14:45:55 -0500149 if (error) {
Andy Shevchenko48b44522017-01-19 18:39:42 +0200150 dev_err(&pdev->dev, "Unable to request irq %d for MID power"
Ameya Palandeb9e06692011-04-06 17:44:37 +0300151 "button\n", irq);
Andy Shevchenko07d90892017-01-19 18:39:41 +0200152 return error;
Hong Liu8eec8a12011-02-07 14:45:55 -0500153 }
154
155 error = input_register_device(input);
156 if (error) {
Ameya Palandeb9e06692011-04-06 17:44:37 +0300157 dev_err(&pdev->dev, "Unable to register input dev, error "
158 "%d\n", error);
Andy Shevchenko07d90892017-01-19 18:39:41 +0200159 return error;
Hong Liu8eec8a12011-02-07 14:45:55 -0500160 }
161
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200162 platform_set_drvdata(pdev, ddata);
Michael Demeter77145672012-01-26 17:40:27 +0000163
Andy Shevchenko4b819c62017-01-19 18:39:44 +0200164 error = ddata->ack(ddata);
Michael Demeter77145672012-01-26 17:40:27 +0000165 if (error) {
166 dev_err(&pdev->dev, "Unable to clear power button interrupt, "
167 "error: %d\n", error);
Andy Shevchenko07d90892017-01-19 18:39:41 +0200168 return error;
Michael Demeter77145672012-01-26 17:40:27 +0000169 }
170
Andy Shevchenko07d90892017-01-19 18:39:41 +0200171 device_init_wakeup(&pdev->dev, true);
172 dev_pm_set_wake_irq(&pdev->dev, irq);
Hong Liu8eec8a12011-02-07 14:45:55 -0500173
Andy Shevchenko07d90892017-01-19 18:39:41 +0200174 return 0;
Hong Liu8eec8a12011-02-07 14:45:55 -0500175}
176
Andy Shevchenko48b44522017-01-19 18:39:42 +0200177static int mid_pb_remove(struct platform_device *pdev)
Hong Liu8eec8a12011-02-07 14:45:55 -0500178{
Sudeep Holladaea5a62015-09-21 16:47:01 +0100179 dev_pm_clear_wake_irq(&pdev->dev);
180 device_init_wakeup(&pdev->dev, false);
Ameya Palandeb9e06692011-04-06 17:44:37 +0300181
Hong Liu8eec8a12011-02-07 14:45:55 -0500182 return 0;
183}
184
Andy Shevchenko48b44522017-01-19 18:39:42 +0200185static struct platform_driver mid_pb_driver = {
Hong Liu8eec8a12011-02-07 14:45:55 -0500186 .driver = {
187 .name = DRIVER_NAME,
Hong Liu8eec8a12011-02-07 14:45:55 -0500188 },
Andy Shevchenko48b44522017-01-19 18:39:42 +0200189 .probe = mid_pb_probe,
190 .remove = mid_pb_remove,
Hong Liu8eec8a12011-02-07 14:45:55 -0500191};
192
Andy Shevchenko48b44522017-01-19 18:39:42 +0200193module_platform_driver(mid_pb_driver);
Hong Liu8eec8a12011-02-07 14:45:55 -0500194
195MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>");
Andy Shevchenko48b44522017-01-19 18:39:42 +0200196MODULE_DESCRIPTION("Intel MID Power Button Driver");
Hong Liu8eec8a12011-02-07 14:45:55 -0500197MODULE_LICENSE("GPL v2");
198MODULE_ALIAS("platform:" DRIVER_NAME);