2 * Power button driver for Intel MID platforms.
4 * Copyright (C) 2010,2017 Intel Corp
6 * Author: Hong Liu <hong.liu@intel.com>
7 * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
19 #include <linux/init.h>
20 #include <linux/input.h>
21 #include <linux/interrupt.h>
22 #include <linux/mfd/intel_msic.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_wakeirq.h>
26 #include <linux/slab.h>
28 #include <asm/cpu_device_id.h>
29 #include <asm/intel-family.h>
30 #include <asm/intel_scu_ipc.h>
32 #define DRIVER_NAME "msic_power_btn"
34 #define MSIC_PB_LEVEL (1 << 3) /* 1 - release, 0 - press */
37 * MSIC document ti_datasheet defines the 1st bit reg 0x21 is used to mask
38 * power button interrupt
40 #define MSIC_PWRBTNM (1 << 0)
43 #define BCOVE_PB_LEVEL (1 << 4) /* 1 - release, 0 - press */
46 #define BCOVE_PBIRQ 0x02
47 #define BCOVE_IRQLVL1MSK 0x0c
48 #define BCOVE_PBIRQMASK 0x0d
49 #define BCOVE_PBSTATUS 0x27
54 struct input_dev *input;
55 unsigned short mirqlvl1_addr;
56 unsigned short pbstat_addr;
58 int (*setup)(struct mid_pb_ddata *ddata);
61 static int mid_pbstat(struct mid_pb_ddata *ddata, int *value)
63 struct input_dev *input = ddata->input;
67 ret = intel_scu_ipc_ioread8(ddata->pbstat_addr, &pbstat);
71 dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat);
73 *value = !(pbstat & ddata->pbstat_mask);
77 static int mid_irq_ack(struct mid_pb_ddata *ddata)
79 return intel_scu_ipc_update_register(ddata->mirqlvl1_addr, 0, MSIC_PWRBTNM);
82 static int mrfld_setup(struct mid_pb_ddata *ddata)
84 /* Unmask the PBIRQ and MPBIRQ on Tangier */
85 intel_scu_ipc_update_register(BCOVE_PBIRQ, 0, MSIC_PWRBTNM);
86 intel_scu_ipc_update_register(BCOVE_PBIRQMASK, 0, MSIC_PWRBTNM);
91 static irqreturn_t mid_pb_isr(int irq, void *dev_id)
93 struct mid_pb_ddata *ddata = dev_id;
94 struct input_dev *input = ddata->input;
98 ret = mid_pbstat(ddata, &value);
100 dev_err(input->dev.parent,
101 "Read error %d while reading MSIC_PB_STATUS\n", ret);
103 input_event(input, EV_KEY, KEY_POWER, value);
111 static struct mid_pb_ddata mfld_ddata = {
112 .mirqlvl1_addr = INTEL_MSIC_IRQLVL1MSK,
113 .pbstat_addr = INTEL_MSIC_PBSTATUS,
114 .pbstat_mask = MSIC_PB_LEVEL,
117 static struct mid_pb_ddata mrfld_ddata = {
118 .mirqlvl1_addr = BCOVE_IRQLVL1MSK,
119 .pbstat_addr = BCOVE_PBSTATUS,
120 .pbstat_mask = BCOVE_PB_LEVEL,
121 .setup = mrfld_setup,
124 #define ICPU(model, ddata) \
125 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (kernel_ulong_t)&ddata }
127 static const struct x86_cpu_id mid_pb_cpu_ids[] = {
128 ICPU(INTEL_FAM6_ATOM_PENWELL, mfld_ddata),
129 ICPU(INTEL_FAM6_ATOM_MERRIFIELD, mrfld_ddata),
133 static int mid_pb_probe(struct platform_device *pdev)
135 const struct x86_cpu_id *id;
136 struct mid_pb_ddata *ddata;
137 struct input_dev *input;
138 int irq = platform_get_irq(pdev, 0);
141 id = x86_match_cpu(mid_pb_cpu_ids);
146 dev_err(&pdev->dev, "Failed to get IRQ: %d\n", irq);
150 input = devm_input_allocate_device(&pdev->dev);
154 input->name = pdev->name;
155 input->phys = "power-button/input0";
156 input->id.bustype = BUS_HOST;
157 input->dev.parent = &pdev->dev;
159 input_set_capability(input, EV_KEY, KEY_POWER);
161 ddata = (struct mid_pb_ddata *)id->driver_data;
165 ddata->dev = &pdev->dev;
167 ddata->input = input;
170 error = ddata->setup(ddata);
175 error = devm_request_threaded_irq(&pdev->dev, irq, NULL, mid_pb_isr,
176 IRQF_ONESHOT, DRIVER_NAME, ddata);
179 "Unable to request irq %d for MID power button\n", irq);
183 error = input_register_device(input);
186 "Unable to register input dev, error %d\n", error);
190 platform_set_drvdata(pdev, ddata);
193 * SCU firmware might send power button interrupts to IA core before
194 * kernel boots and doesn't get EOI from IA core. The first bit of
195 * MSIC reg 0x21 is kept masked, and SCU firmware doesn't send new
196 * power interrupt to Android kernel. Unmask the bit when probing
197 * power button in kernel.
198 * There is a very narrow race between irq handler and power button
199 * initialization. The race happens rarely. So we needn't worry
202 error = mid_irq_ack(ddata);
205 "Unable to clear power button interrupt, error: %d\n",
210 device_init_wakeup(&pdev->dev, true);
211 dev_pm_set_wake_irq(&pdev->dev, irq);
216 static int mid_pb_remove(struct platform_device *pdev)
218 dev_pm_clear_wake_irq(&pdev->dev);
219 device_init_wakeup(&pdev->dev, false);
224 static struct platform_driver mid_pb_driver = {
228 .probe = mid_pb_probe,
229 .remove = mid_pb_remove,
232 module_platform_driver(mid_pb_driver);
234 MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>");
235 MODULE_DESCRIPTION("Intel MID Power Button Driver");
236 MODULE_LICENSE("GPL v2");
237 MODULE_ALIAS("platform:" DRIVER_NAME);