blob: d79fbf924b136823987011664d385e1bdceb927d [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 *
Andy Shevchenko1cfd3ba2017-01-19 18:39:49 +02004 * Copyright (C) 2010,2017 Intel Corp
5 *
6 * Author: Hong Liu <hong.liu@intel.com>
7 * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Hong Liu8eec8a12011-02-07 14:45:55 -05008 *
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.
12 *
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.
Hong Liu8eec8a12011-02-07 14:45:55 -050017 */
18
Hong Liu8eec8a12011-02-07 14:45:55 -050019#include <linux/init.h>
Hong Liu8eec8a12011-02-07 14:45:55 -050020#include <linux/input.h>
Andy Shevchenko7591b9f2017-01-19 18:39:48 +020021#include <linux/interrupt.h>
Michael Demeter77145672012-01-26 17:40:27 +000022#include <linux/mfd/intel_msic.h>
Andy Shevchenko7591b9f2017-01-19 18:39:48 +020023#include <linux/module.h>
24#include <linux/platform_device.h>
Sudeep Holladaea5a62015-09-21 16:47:01 +010025#include <linux/pm_wakeirq.h>
Andy Shevchenko7591b9f2017-01-19 18:39:48 +020026#include <linux/slab.h>
Hong Liu8eec8a12011-02-07 14:45:55 -050027
Andy Shevchenko18934ec2017-01-19 18:39:43 +020028#include <asm/cpu_device_id.h>
29#include <asm/intel-family.h>
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020030#include <asm/intel_scu_ipc.h>
Andy Shevchenko18934ec2017-01-19 18:39:43 +020031
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 Shevchenko6a0f9982017-01-19 18:39:46 +020042/* Intel Tangier */
Andy Shevchenkob30f3f82017-02-02 19:54:26 +020043#define BCOVE_PB_LEVEL (1 << 4) /* 1 - release, 0 - press */
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020044
45/* Basin Cove PMIC */
46#define BCOVE_PBIRQ 0x02
47#define BCOVE_IRQLVL1MSK 0x0c
48#define BCOVE_PBIRQMASK 0x0d
Andy Shevchenkob30f3f82017-02-02 19:54:26 +020049#define BCOVE_PBSTATUS 0x27
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020050
Andy Shevchenko18934ec2017-01-19 18:39:43 +020051struct mid_pb_ddata {
52 struct device *dev;
53 int irq;
54 struct input_dev *input;
Andy Shevchenkoca45ba02017-02-02 19:54:28 +020055 unsigned short mirqlvl1_addr;
Andy Shevchenkob30f3f82017-02-02 19:54:26 +020056 unsigned short pbstat_addr;
57 u8 pbstat_mask;
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020058 int (*setup)(struct mid_pb_ddata *ddata);
Andy Shevchenko18934ec2017-01-19 18:39:43 +020059};
60
Andy Shevchenkob30f3f82017-02-02 19:54:26 +020061static int mid_pbstat(struct mid_pb_ddata *ddata, int *value)
Hong Liu8eec8a12011-02-07 14:45:55 -050062{
Andy Shevchenko18934ec2017-01-19 18:39:43 +020063 struct input_dev *input = ddata->input;
Hong Liu8eec8a12011-02-07 14:45:55 -050064 int ret;
65 u8 pbstat;
66
Andy Shevchenko25b4a382017-02-08 19:03:19 +020067 ret = intel_scu_ipc_ioread8(ddata->pbstat_addr, &pbstat);
Andy Shevchenko18934ec2017-01-19 18:39:43 +020068 if (ret)
69 return ret;
70
Michael Demeter77145672012-01-26 17:40:27 +000071 dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat);
72
Andy Shevchenkob30f3f82017-02-02 19:54:26 +020073 *value = !(pbstat & ddata->pbstat_mask);
Andy Shevchenko18934ec2017-01-19 18:39:43 +020074 return 0;
75}
76
Andy Shevchenkoca45ba02017-02-02 19:54:28 +020077static int mid_irq_ack(struct mid_pb_ddata *ddata)
Andy Shevchenko4b819c62017-01-19 18:39:44 +020078{
Andy Shevchenko25b4a382017-02-08 19:03:19 +020079 return intel_scu_ipc_update_register(ddata->mirqlvl1_addr, 0, MSIC_PWRBTNM);
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020080}
81
82static int mrfld_setup(struct mid_pb_ddata *ddata)
83{
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020084 /* 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);
87
88 return 0;
89}
90
Andy Shevchenko18934ec2017-01-19 18:39:43 +020091static irqreturn_t mid_pb_isr(int irq, void *dev_id)
92{
93 struct mid_pb_ddata *ddata = dev_id;
94 struct input_dev *input = ddata->input;
Andy Shevchenkob30f3f82017-02-02 19:54:26 +020095 int value = 0;
Andy Shevchenko18934ec2017-01-19 18:39:43 +020096 int ret;
97
Andy Shevchenkob30f3f82017-02-02 19:54:26 +020098 ret = mid_pbstat(ddata, &value);
Ameya Palandeb9e06692011-04-06 17:44:37 +030099 if (ret < 0) {
Andy Shevchenkofdde1a822017-01-19 18:39:47 +0200100 dev_err(input->dev.parent,
101 "Read error %d while reading MSIC_PB_STATUS\n", ret);
Ameya Palandeb9e06692011-04-06 17:44:37 +0300102 } else {
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200103 input_event(input, EV_KEY, KEY_POWER, value);
Ameya Palandeb9e06692011-04-06 17:44:37 +0300104 input_sync(input);
105 }
Hong Liu8eec8a12011-02-07 14:45:55 -0500106
Andy Shevchenkoca45ba02017-02-02 19:54:28 +0200107 mid_irq_ack(ddata);
Hong Liu8eec8a12011-02-07 14:45:55 -0500108 return IRQ_HANDLED;
109}
110
Bhumika Goyalc94a8ff2017-08-11 19:50:00 +0530111static const struct mid_pb_ddata mfld_ddata = {
Andy Shevchenkoca45ba02017-02-02 19:54:28 +0200112 .mirqlvl1_addr = INTEL_MSIC_IRQLVL1MSK,
Andy Shevchenkob30f3f82017-02-02 19:54:26 +0200113 .pbstat_addr = INTEL_MSIC_PBSTATUS,
114 .pbstat_mask = MSIC_PB_LEVEL,
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200115};
116
Bhumika Goyalc94a8ff2017-08-11 19:50:00 +0530117static const struct mid_pb_ddata mrfld_ddata = {
Andy Shevchenkoca45ba02017-02-02 19:54:28 +0200118 .mirqlvl1_addr = BCOVE_IRQLVL1MSK,
Andy Shevchenkob30f3f82017-02-02 19:54:26 +0200119 .pbstat_addr = BCOVE_PBSTATUS,
120 .pbstat_mask = BCOVE_PB_LEVEL,
Andy Shevchenko6a0f9982017-01-19 18:39:46 +0200121 .setup = mrfld_setup,
122};
123
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200124#define ICPU(model, ddata) \
125 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (kernel_ulong_t)&ddata }
126
127static const struct x86_cpu_id mid_pb_cpu_ids[] = {
128 ICPU(INTEL_FAM6_ATOM_PENWELL, mfld_ddata),
Andy Shevchenko6a0f9982017-01-19 18:39:46 +0200129 ICPU(INTEL_FAM6_ATOM_MERRIFIELD, mrfld_ddata),
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200130 {}
131};
132
Andy Shevchenko48b44522017-01-19 18:39:42 +0200133static int mid_pb_probe(struct platform_device *pdev)
Hong Liu8eec8a12011-02-07 14:45:55 -0500134{
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200135 const struct x86_cpu_id *id;
136 struct mid_pb_ddata *ddata;
Hong Liu8eec8a12011-02-07 14:45:55 -0500137 struct input_dev *input;
Ameya Palandeb9e06692011-04-06 17:44:37 +0300138 int irq = platform_get_irq(pdev, 0);
Hong Liu8eec8a12011-02-07 14:45:55 -0500139 int error;
140
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200141 id = x86_match_cpu(mid_pb_cpu_ids);
142 if (!id)
143 return -ENODEV;
144
Gustavo A. R. Silvafe4e8d02017-08-09 11:00:54 -0500145 if (irq < 0) {
146 dev_err(&pdev->dev, "Failed to get IRQ: %d\n", irq);
147 return irq;
148 }
Hong Liu8eec8a12011-02-07 14:45:55 -0500149
Andy Shevchenko07d90892017-01-19 18:39:41 +0200150 input = devm_input_allocate_device(&pdev->dev);
Joe Perchesb222cca2013-10-23 12:14:52 -0700151 if (!input)
Ameya Palandeb9e06692011-04-06 17:44:37 +0300152 return -ENOMEM;
Hong Liu8eec8a12011-02-07 14:45:55 -0500153
Hong Liu8eec8a12011-02-07 14:45:55 -0500154 input->name = pdev->name;
155 input->phys = "power-button/input0";
156 input->id.bustype = BUS_HOST;
157 input->dev.parent = &pdev->dev;
158
159 input_set_capability(input, EV_KEY, KEY_POWER);
160
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200161 ddata = (struct mid_pb_ddata *)id->driver_data;
162 if (!ddata)
163 return -ENODATA;
164
165 ddata->dev = &pdev->dev;
166 ddata->irq = irq;
167 ddata->input = input;
168
Andy Shevchenko6a0f9982017-01-19 18:39:46 +0200169 if (ddata->setup) {
170 error = ddata->setup(ddata);
171 if (error)
172 return error;
173 }
174
Andy Shevchenko48b44522017-01-19 18:39:42 +0200175 error = devm_request_threaded_irq(&pdev->dev, irq, NULL, mid_pb_isr,
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200176 IRQF_ONESHOT, DRIVER_NAME, ddata);
Hong Liu8eec8a12011-02-07 14:45:55 -0500177 if (error) {
Andy Shevchenkofdde1a822017-01-19 18:39:47 +0200178 dev_err(&pdev->dev,
179 "Unable to request irq %d for MID power button\n", irq);
Andy Shevchenko07d90892017-01-19 18:39:41 +0200180 return error;
Hong Liu8eec8a12011-02-07 14:45:55 -0500181 }
182
183 error = input_register_device(input);
184 if (error) {
Andy Shevchenkofdde1a822017-01-19 18:39:47 +0200185 dev_err(&pdev->dev,
186 "Unable to register input dev, error %d\n", error);
Andy Shevchenko07d90892017-01-19 18:39:41 +0200187 return error;
Hong Liu8eec8a12011-02-07 14:45:55 -0500188 }
189
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200190 platform_set_drvdata(pdev, ddata);
Michael Demeter77145672012-01-26 17:40:27 +0000191
Andy Shevchenko5cb44ee2017-02-02 19:54:27 +0200192 /*
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
200 * about it.
201 */
Andy Shevchenkoca45ba02017-02-02 19:54:28 +0200202 error = mid_irq_ack(ddata);
Michael Demeter77145672012-01-26 17:40:27 +0000203 if (error) {
Andy Shevchenkofdde1a822017-01-19 18:39:47 +0200204 dev_err(&pdev->dev,
205 "Unable to clear power button interrupt, error: %d\n",
206 error);
Andy Shevchenko07d90892017-01-19 18:39:41 +0200207 return error;
Michael Demeter77145672012-01-26 17:40:27 +0000208 }
209
Andy Shevchenko07d90892017-01-19 18:39:41 +0200210 device_init_wakeup(&pdev->dev, true);
211 dev_pm_set_wake_irq(&pdev->dev, irq);
Hong Liu8eec8a12011-02-07 14:45:55 -0500212
Andy Shevchenko07d90892017-01-19 18:39:41 +0200213 return 0;
Hong Liu8eec8a12011-02-07 14:45:55 -0500214}
215
Andy Shevchenko48b44522017-01-19 18:39:42 +0200216static int mid_pb_remove(struct platform_device *pdev)
Hong Liu8eec8a12011-02-07 14:45:55 -0500217{
Sudeep Holladaea5a62015-09-21 16:47:01 +0100218 dev_pm_clear_wake_irq(&pdev->dev);
219 device_init_wakeup(&pdev->dev, false);
Ameya Palandeb9e06692011-04-06 17:44:37 +0300220
Hong Liu8eec8a12011-02-07 14:45:55 -0500221 return 0;
222}
223
Andy Shevchenko48b44522017-01-19 18:39:42 +0200224static struct platform_driver mid_pb_driver = {
Hong Liu8eec8a12011-02-07 14:45:55 -0500225 .driver = {
226 .name = DRIVER_NAME,
Hong Liu8eec8a12011-02-07 14:45:55 -0500227 },
Andy Shevchenko48b44522017-01-19 18:39:42 +0200228 .probe = mid_pb_probe,
229 .remove = mid_pb_remove,
Hong Liu8eec8a12011-02-07 14:45:55 -0500230};
231
Andy Shevchenko48b44522017-01-19 18:39:42 +0200232module_platform_driver(mid_pb_driver);
Hong Liu8eec8a12011-02-07 14:45:55 -0500233
234MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>");
Andy Shevchenko48b44522017-01-19 18:39:42 +0200235MODULE_DESCRIPTION("Intel MID Power Button Driver");
Hong Liu8eec8a12011-02-07 14:45:55 -0500236MODULE_LICENSE("GPL v2");
237MODULE_ALIAS("platform:" DRIVER_NAME);