blob: 9b718cf237eeabd60bf6ed6d7fbda10edecbad5a [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>
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020031#include <asm/intel_scu_ipc.h>
Andy Shevchenko18934ec2017-01-19 18:39:43 +020032
Hong Liu8eec8a12011-02-07 14:45:55 -050033#define DRIVER_NAME "msic_power_btn"
34
Ameya Palandeb9e06692011-04-06 17:44:37 +030035#define MSIC_PB_LEVEL (1 << 3) /* 1 - release, 0 - press */
Hong Liu8eec8a12011-02-07 14:45:55 -050036
Michael Demeter77145672012-01-26 17:40:27 +000037/*
38 * MSIC document ti_datasheet defines the 1st bit reg 0x21 is used to mask
39 * power button interrupt
40 */
41#define MSIC_PWRBTNM (1 << 0)
42
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020043/* Intel Tangier */
44#define MRFLD_PBSTAT_ADDR 0xfffff61a
45#define MRFLD_PB_LEVEL (1 << 4) /* 1 - release, 0 - press */
46
47/* Basin Cove PMIC */
48#define BCOVE_PBIRQ 0x02
49#define BCOVE_IRQLVL1MSK 0x0c
50#define BCOVE_PBIRQMASK 0x0d
51
Andy Shevchenko18934ec2017-01-19 18:39:43 +020052struct mid_pb_ddata {
53 struct device *dev;
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020054 void __iomem *reg;
Andy Shevchenko18934ec2017-01-19 18:39:43 +020055 int irq;
56 struct input_dev *input;
57 int (*pbstat)(struct mid_pb_ddata *ddata, int *value);
Andy Shevchenko4b819c62017-01-19 18:39:44 +020058 int (*ack)(struct mid_pb_ddata *ddata);
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020059 int (*setup)(struct mid_pb_ddata *ddata);
Andy Shevchenko18934ec2017-01-19 18:39:43 +020060};
61
62static int mfld_pbstat(struct mid_pb_ddata *ddata, int *value)
Hong Liu8eec8a12011-02-07 14:45:55 -050063{
Andy Shevchenko18934ec2017-01-19 18:39:43 +020064 struct input_dev *input = ddata->input;
Hong Liu8eec8a12011-02-07 14:45:55 -050065 int ret;
66 u8 pbstat;
67
Michael Demeter77145672012-01-26 17:40:27 +000068 ret = intel_msic_reg_read(INTEL_MSIC_PBSTATUS, &pbstat);
Andy Shevchenko18934ec2017-01-19 18:39:43 +020069 if (ret)
70 return ret;
71
Michael Demeter77145672012-01-26 17:40:27 +000072 dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat);
73
Andy Shevchenko18934ec2017-01-19 18:39:43 +020074 *value = !(pbstat & MSIC_PB_LEVEL);
75 return 0;
76}
77
Andy Shevchenko4b819c62017-01-19 18:39:44 +020078static int mfld_ack(struct mid_pb_ddata *ddata)
79{
80 /*
81 * SCU firmware might send power button interrupts to IA core before
82 * kernel boots and doesn't get EOI from IA core. The first bit of
83 * MSIC reg 0x21 is kept masked, and SCU firmware doesn't send new
84 * power interrupt to Android kernel. Unmask the bit when probing
85 * power button in kernel.
86 * There is a very narrow race between irq handler and power button
87 * initialization. The race happens rarely. So we needn't worry
88 * about it.
89 */
90 return intel_msic_reg_update(INTEL_MSIC_IRQLVL1MSK, 0, MSIC_PWRBTNM);
91}
92
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020093static int mrfld_pbstat(struct mid_pb_ddata *ddata, int *value)
94{
95 struct input_dev *input = ddata->input;
96 u8 pbstat;
97
98 pbstat = readb(ddata->reg);
99
100 dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat);
101
102 *value = !(pbstat & MRFLD_PB_LEVEL);
103 return 0;
104}
105
106static int mrfld_ack(struct mid_pb_ddata *ddata)
107{
108 return intel_scu_ipc_update_register(BCOVE_IRQLVL1MSK, 0, MSIC_PWRBTNM);
109}
110
111static int mrfld_setup(struct mid_pb_ddata *ddata)
112{
113 ddata->reg = devm_ioremap_nocache(ddata->dev, MRFLD_PBSTAT_ADDR, 1);
114 if (!ddata->reg)
115 return -ENOMEM;
116
117 /* Unmask the PBIRQ and MPBIRQ on Tangier */
118 intel_scu_ipc_update_register(BCOVE_PBIRQ, 0, MSIC_PWRBTNM);
119 intel_scu_ipc_update_register(BCOVE_PBIRQMASK, 0, MSIC_PWRBTNM);
120
121 return 0;
122}
123
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200124static irqreturn_t mid_pb_isr(int irq, void *dev_id)
125{
126 struct mid_pb_ddata *ddata = dev_id;
127 struct input_dev *input = ddata->input;
128 int value;
129 int ret;
130
131 ret = ddata->pbstat(ddata, &value);
Ameya Palandeb9e06692011-04-06 17:44:37 +0300132 if (ret < 0) {
Andy Shevchenkofdde1a822017-01-19 18:39:47 +0200133 dev_err(input->dev.parent,
134 "Read error %d while reading MSIC_PB_STATUS\n", ret);
Ameya Palandeb9e06692011-04-06 17:44:37 +0300135 } else {
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200136 input_event(input, EV_KEY, KEY_POWER, value);
Ameya Palandeb9e06692011-04-06 17:44:37 +0300137 input_sync(input);
138 }
Hong Liu8eec8a12011-02-07 14:45:55 -0500139
Andy Shevchenko553e9c12017-01-19 18:39:45 +0200140 ddata->ack(ddata);
Hong Liu8eec8a12011-02-07 14:45:55 -0500141 return IRQ_HANDLED;
142}
143
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200144static struct mid_pb_ddata mfld_ddata = {
145 .pbstat = mfld_pbstat,
Andy Shevchenko4b819c62017-01-19 18:39:44 +0200146 .ack = mfld_ack,
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200147};
148
Andy Shevchenko6a0f9982017-01-19 18:39:46 +0200149static struct mid_pb_ddata mrfld_ddata = {
150 .pbstat = mrfld_pbstat,
151 .ack = mrfld_ack,
152 .setup = mrfld_setup,
153};
154
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200155#define ICPU(model, ddata) \
156 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (kernel_ulong_t)&ddata }
157
158static const struct x86_cpu_id mid_pb_cpu_ids[] = {
159 ICPU(INTEL_FAM6_ATOM_PENWELL, mfld_ddata),
Andy Shevchenko6a0f9982017-01-19 18:39:46 +0200160 ICPU(INTEL_FAM6_ATOM_MERRIFIELD, mrfld_ddata),
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200161 {}
162};
163
Andy Shevchenko48b44522017-01-19 18:39:42 +0200164static int mid_pb_probe(struct platform_device *pdev)
Hong Liu8eec8a12011-02-07 14:45:55 -0500165{
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200166 const struct x86_cpu_id *id;
167 struct mid_pb_ddata *ddata;
Hong Liu8eec8a12011-02-07 14:45:55 -0500168 struct input_dev *input;
Ameya Palandeb9e06692011-04-06 17:44:37 +0300169 int irq = platform_get_irq(pdev, 0);
Hong Liu8eec8a12011-02-07 14:45:55 -0500170 int error;
171
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200172 id = x86_match_cpu(mid_pb_cpu_ids);
173 if (!id)
174 return -ENODEV;
175
Hong Liu8eec8a12011-02-07 14:45:55 -0500176 if (irq < 0)
177 return -EINVAL;
178
Andy Shevchenko07d90892017-01-19 18:39:41 +0200179 input = devm_input_allocate_device(&pdev->dev);
Joe Perchesb222cca2013-10-23 12:14:52 -0700180 if (!input)
Ameya Palandeb9e06692011-04-06 17:44:37 +0300181 return -ENOMEM;
Hong Liu8eec8a12011-02-07 14:45:55 -0500182
Hong Liu8eec8a12011-02-07 14:45:55 -0500183 input->name = pdev->name;
184 input->phys = "power-button/input0";
185 input->id.bustype = BUS_HOST;
186 input->dev.parent = &pdev->dev;
187
188 input_set_capability(input, EV_KEY, KEY_POWER);
189
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200190 ddata = (struct mid_pb_ddata *)id->driver_data;
191 if (!ddata)
192 return -ENODATA;
193
194 ddata->dev = &pdev->dev;
195 ddata->irq = irq;
196 ddata->input = input;
197
Andy Shevchenko6a0f9982017-01-19 18:39:46 +0200198 if (ddata->setup) {
199 error = ddata->setup(ddata);
200 if (error)
201 return error;
202 }
203
Andy Shevchenko48b44522017-01-19 18:39:42 +0200204 error = devm_request_threaded_irq(&pdev->dev, irq, NULL, mid_pb_isr,
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200205 IRQF_ONESHOT, DRIVER_NAME, ddata);
Hong Liu8eec8a12011-02-07 14:45:55 -0500206 if (error) {
Andy Shevchenkofdde1a822017-01-19 18:39:47 +0200207 dev_err(&pdev->dev,
208 "Unable to request irq %d for MID power button\n", irq);
Andy Shevchenko07d90892017-01-19 18:39:41 +0200209 return error;
Hong Liu8eec8a12011-02-07 14:45:55 -0500210 }
211
212 error = input_register_device(input);
213 if (error) {
Andy Shevchenkofdde1a822017-01-19 18:39:47 +0200214 dev_err(&pdev->dev,
215 "Unable to register input dev, error %d\n", error);
Andy Shevchenko07d90892017-01-19 18:39:41 +0200216 return error;
Hong Liu8eec8a12011-02-07 14:45:55 -0500217 }
218
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200219 platform_set_drvdata(pdev, ddata);
Michael Demeter77145672012-01-26 17:40:27 +0000220
Andy Shevchenko4b819c62017-01-19 18:39:44 +0200221 error = ddata->ack(ddata);
Michael Demeter77145672012-01-26 17:40:27 +0000222 if (error) {
Andy Shevchenkofdde1a822017-01-19 18:39:47 +0200223 dev_err(&pdev->dev,
224 "Unable to clear power button interrupt, error: %d\n",
225 error);
Andy Shevchenko07d90892017-01-19 18:39:41 +0200226 return error;
Michael Demeter77145672012-01-26 17:40:27 +0000227 }
228
Andy Shevchenko07d90892017-01-19 18:39:41 +0200229 device_init_wakeup(&pdev->dev, true);
230 dev_pm_set_wake_irq(&pdev->dev, irq);
Hong Liu8eec8a12011-02-07 14:45:55 -0500231
Andy Shevchenko07d90892017-01-19 18:39:41 +0200232 return 0;
Hong Liu8eec8a12011-02-07 14:45:55 -0500233}
234
Andy Shevchenko48b44522017-01-19 18:39:42 +0200235static int mid_pb_remove(struct platform_device *pdev)
Hong Liu8eec8a12011-02-07 14:45:55 -0500236{
Sudeep Holladaea5a62015-09-21 16:47:01 +0100237 dev_pm_clear_wake_irq(&pdev->dev);
238 device_init_wakeup(&pdev->dev, false);
Ameya Palandeb9e06692011-04-06 17:44:37 +0300239
Hong Liu8eec8a12011-02-07 14:45:55 -0500240 return 0;
241}
242
Andy Shevchenko48b44522017-01-19 18:39:42 +0200243static struct platform_driver mid_pb_driver = {
Hong Liu8eec8a12011-02-07 14:45:55 -0500244 .driver = {
245 .name = DRIVER_NAME,
Hong Liu8eec8a12011-02-07 14:45:55 -0500246 },
Andy Shevchenko48b44522017-01-19 18:39:42 +0200247 .probe = mid_pb_probe,
248 .remove = mid_pb_remove,
Hong Liu8eec8a12011-02-07 14:45:55 -0500249};
250
Andy Shevchenko48b44522017-01-19 18:39:42 +0200251module_platform_driver(mid_pb_driver);
Hong Liu8eec8a12011-02-07 14:45:55 -0500252
253MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>");
Andy Shevchenko48b44522017-01-19 18:39:42 +0200254MODULE_DESCRIPTION("Intel MID Power Button Driver");
Hong Liu8eec8a12011-02-07 14:45:55 -0500255MODULE_LICENSE("GPL v2");
256MODULE_ALIAS("platform:" DRIVER_NAME);