blob: b8d659233c6a1e2abc8982844ccf71a0f551cc13 [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/input.h>
Andy Shevchenko7591b9f2017-01-19 18:39:48 +020020#include <linux/interrupt.h>
Michael Demeter77145672012-01-26 17:40:27 +000021#include <linux/mfd/intel_msic.h>
Andy Shevchenko7591b9f2017-01-19 18:39:48 +020022#include <linux/module.h>
23#include <linux/platform_device.h>
Sudeep Holladaea5a62015-09-21 16:47:01 +010024#include <linux/pm_wakeirq.h>
Andy Shevchenko7591b9f2017-01-19 18:39:48 +020025#include <linux/slab.h>
Hong Liu8eec8a12011-02-07 14:45:55 -050026
Andy Shevchenko18934ec2017-01-19 18:39:43 +020027#include <asm/cpu_device_id.h>
28#include <asm/intel-family.h>
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020029#include <asm/intel_scu_ipc.h>
Andy Shevchenko18934ec2017-01-19 18:39:43 +020030
Hong Liu8eec8a12011-02-07 14:45:55 -050031#define DRIVER_NAME "msic_power_btn"
32
Ameya Palandeb9e06692011-04-06 17:44:37 +030033#define MSIC_PB_LEVEL (1 << 3) /* 1 - release, 0 - press */
Hong Liu8eec8a12011-02-07 14:45:55 -050034
Michael Demeter77145672012-01-26 17:40:27 +000035/*
36 * MSIC document ti_datasheet defines the 1st bit reg 0x21 is used to mask
37 * power button interrupt
38 */
39#define MSIC_PWRBTNM (1 << 0)
40
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020041/* Intel Tangier */
Andy Shevchenkob30f3f82017-02-02 19:54:26 +020042#define BCOVE_PB_LEVEL (1 << 4) /* 1 - release, 0 - press */
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020043
44/* Basin Cove PMIC */
45#define BCOVE_PBIRQ 0x02
46#define BCOVE_IRQLVL1MSK 0x0c
47#define BCOVE_PBIRQMASK 0x0d
Andy Shevchenkob30f3f82017-02-02 19:54:26 +020048#define BCOVE_PBSTATUS 0x27
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020049
Andy Shevchenko18934ec2017-01-19 18:39:43 +020050struct mid_pb_ddata {
51 struct device *dev;
52 int irq;
53 struct input_dev *input;
Andy Shevchenkoca45ba02017-02-02 19:54:28 +020054 unsigned short mirqlvl1_addr;
Andy Shevchenkob30f3f82017-02-02 19:54:26 +020055 unsigned short pbstat_addr;
56 u8 pbstat_mask;
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020057 int (*setup)(struct mid_pb_ddata *ddata);
Andy Shevchenko18934ec2017-01-19 18:39:43 +020058};
59
Andy Shevchenkob30f3f82017-02-02 19:54:26 +020060static int mid_pbstat(struct mid_pb_ddata *ddata, int *value)
Hong Liu8eec8a12011-02-07 14:45:55 -050061{
Andy Shevchenko18934ec2017-01-19 18:39:43 +020062 struct input_dev *input = ddata->input;
Hong Liu8eec8a12011-02-07 14:45:55 -050063 int ret;
64 u8 pbstat;
65
Andy Shevchenko25b4a382017-02-08 19:03:19 +020066 ret = intel_scu_ipc_ioread8(ddata->pbstat_addr, &pbstat);
Andy Shevchenko18934ec2017-01-19 18:39:43 +020067 if (ret)
68 return ret;
69
Michael Demeter77145672012-01-26 17:40:27 +000070 dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat);
71
Andy Shevchenkob30f3f82017-02-02 19:54:26 +020072 *value = !(pbstat & ddata->pbstat_mask);
Andy Shevchenko18934ec2017-01-19 18:39:43 +020073 return 0;
74}
75
Andy Shevchenkoca45ba02017-02-02 19:54:28 +020076static int mid_irq_ack(struct mid_pb_ddata *ddata)
Andy Shevchenko4b819c62017-01-19 18:39:44 +020077{
Andy Shevchenko25b4a382017-02-08 19:03:19 +020078 return intel_scu_ipc_update_register(ddata->mirqlvl1_addr, 0, MSIC_PWRBTNM);
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020079}
80
81static int mrfld_setup(struct mid_pb_ddata *ddata)
82{
Andy Shevchenko6a0f9982017-01-19 18:39:46 +020083 /* Unmask the PBIRQ and MPBIRQ on Tangier */
84 intel_scu_ipc_update_register(BCOVE_PBIRQ, 0, MSIC_PWRBTNM);
85 intel_scu_ipc_update_register(BCOVE_PBIRQMASK, 0, MSIC_PWRBTNM);
86
87 return 0;
88}
89
Andy Shevchenko18934ec2017-01-19 18:39:43 +020090static irqreturn_t mid_pb_isr(int irq, void *dev_id)
91{
92 struct mid_pb_ddata *ddata = dev_id;
93 struct input_dev *input = ddata->input;
Andy Shevchenkob30f3f82017-02-02 19:54:26 +020094 int value = 0;
Andy Shevchenko18934ec2017-01-19 18:39:43 +020095 int ret;
96
Andy Shevchenkob30f3f82017-02-02 19:54:26 +020097 ret = mid_pbstat(ddata, &value);
Ameya Palandeb9e06692011-04-06 17:44:37 +030098 if (ret < 0) {
Andy Shevchenkofdde1a822017-01-19 18:39:47 +020099 dev_err(input->dev.parent,
100 "Read error %d while reading MSIC_PB_STATUS\n", ret);
Ameya Palandeb9e06692011-04-06 17:44:37 +0300101 } else {
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200102 input_event(input, EV_KEY, KEY_POWER, value);
Ameya Palandeb9e06692011-04-06 17:44:37 +0300103 input_sync(input);
104 }
Hong Liu8eec8a12011-02-07 14:45:55 -0500105
Andy Shevchenkoca45ba02017-02-02 19:54:28 +0200106 mid_irq_ack(ddata);
Hong Liu8eec8a12011-02-07 14:45:55 -0500107 return IRQ_HANDLED;
108}
109
Bhumika Goyalc94a8ff2017-08-11 19:50:00 +0530110static const struct mid_pb_ddata mfld_ddata = {
Andy Shevchenkoca45ba02017-02-02 19:54:28 +0200111 .mirqlvl1_addr = INTEL_MSIC_IRQLVL1MSK,
Andy Shevchenkob30f3f82017-02-02 19:54:26 +0200112 .pbstat_addr = INTEL_MSIC_PBSTATUS,
113 .pbstat_mask = MSIC_PB_LEVEL,
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200114};
115
Bhumika Goyalc94a8ff2017-08-11 19:50:00 +0530116static const struct mid_pb_ddata mrfld_ddata = {
Andy Shevchenkoca45ba02017-02-02 19:54:28 +0200117 .mirqlvl1_addr = BCOVE_IRQLVL1MSK,
Andy Shevchenkob30f3f82017-02-02 19:54:26 +0200118 .pbstat_addr = BCOVE_PBSTATUS,
119 .pbstat_mask = BCOVE_PB_LEVEL,
Andy Shevchenko6a0f9982017-01-19 18:39:46 +0200120 .setup = mrfld_setup,
121};
122
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200123static const struct x86_cpu_id mid_pb_cpu_ids[] = {
Andy Shevchenkoa8b60e42018-08-31 11:34:31 +0300124 INTEL_CPU_FAM6(ATOM_PENWELL, mfld_ddata),
125 INTEL_CPU_FAM6(ATOM_MERRIFIELD, mrfld_ddata),
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200126 {}
127};
128
Andy Shevchenko48b44522017-01-19 18:39:42 +0200129static int mid_pb_probe(struct platform_device *pdev)
Hong Liu8eec8a12011-02-07 14:45:55 -0500130{
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200131 const struct x86_cpu_id *id;
132 struct mid_pb_ddata *ddata;
Hong Liu8eec8a12011-02-07 14:45:55 -0500133 struct input_dev *input;
Ameya Palandeb9e06692011-04-06 17:44:37 +0300134 int irq = platform_get_irq(pdev, 0);
Hong Liu8eec8a12011-02-07 14:45:55 -0500135 int error;
136
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200137 id = x86_match_cpu(mid_pb_cpu_ids);
138 if (!id)
139 return -ENODEV;
140
Gustavo A. R. Silvafe4e8d02017-08-09 11:00:54 -0500141 if (irq < 0) {
142 dev_err(&pdev->dev, "Failed to get IRQ: %d\n", irq);
143 return irq;
144 }
Hong Liu8eec8a12011-02-07 14:45:55 -0500145
Andy Shevchenko07d90892017-01-19 18:39:41 +0200146 input = devm_input_allocate_device(&pdev->dev);
Joe Perchesb222cca2013-10-23 12:14:52 -0700147 if (!input)
Ameya Palandeb9e06692011-04-06 17:44:37 +0300148 return -ENOMEM;
Hong Liu8eec8a12011-02-07 14:45:55 -0500149
Hong Liu8eec8a12011-02-07 14:45:55 -0500150 input->name = pdev->name;
151 input->phys = "power-button/input0";
152 input->id.bustype = BUS_HOST;
153 input->dev.parent = &pdev->dev;
154
155 input_set_capability(input, EV_KEY, KEY_POWER);
156
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200157 ddata = (struct mid_pb_ddata *)id->driver_data;
158 if (!ddata)
159 return -ENODATA;
160
161 ddata->dev = &pdev->dev;
162 ddata->irq = irq;
163 ddata->input = input;
164
Andy Shevchenko6a0f9982017-01-19 18:39:46 +0200165 if (ddata->setup) {
166 error = ddata->setup(ddata);
167 if (error)
168 return error;
169 }
170
Andy Shevchenko48b44522017-01-19 18:39:42 +0200171 error = devm_request_threaded_irq(&pdev->dev, irq, NULL, mid_pb_isr,
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200172 IRQF_ONESHOT, DRIVER_NAME, ddata);
Hong Liu8eec8a12011-02-07 14:45:55 -0500173 if (error) {
Andy Shevchenkofdde1a822017-01-19 18:39:47 +0200174 dev_err(&pdev->dev,
175 "Unable to request irq %d for MID power button\n", irq);
Andy Shevchenko07d90892017-01-19 18:39:41 +0200176 return error;
Hong Liu8eec8a12011-02-07 14:45:55 -0500177 }
178
179 error = input_register_device(input);
180 if (error) {
Andy Shevchenkofdde1a822017-01-19 18:39:47 +0200181 dev_err(&pdev->dev,
182 "Unable to register input dev, error %d\n", error);
Andy Shevchenko07d90892017-01-19 18:39:41 +0200183 return error;
Hong Liu8eec8a12011-02-07 14:45:55 -0500184 }
185
Andy Shevchenko18934ec2017-01-19 18:39:43 +0200186 platform_set_drvdata(pdev, ddata);
Michael Demeter77145672012-01-26 17:40:27 +0000187
Andy Shevchenko5cb44ee2017-02-02 19:54:27 +0200188 /*
189 * SCU firmware might send power button interrupts to IA core before
190 * kernel boots and doesn't get EOI from IA core. The first bit of
191 * MSIC reg 0x21 is kept masked, and SCU firmware doesn't send new
192 * power interrupt to Android kernel. Unmask the bit when probing
193 * power button in kernel.
194 * There is a very narrow race between irq handler and power button
195 * initialization. The race happens rarely. So we needn't worry
196 * about it.
197 */
Andy Shevchenkoca45ba02017-02-02 19:54:28 +0200198 error = mid_irq_ack(ddata);
Michael Demeter77145672012-01-26 17:40:27 +0000199 if (error) {
Andy Shevchenkofdde1a822017-01-19 18:39:47 +0200200 dev_err(&pdev->dev,
201 "Unable to clear power button interrupt, error: %d\n",
202 error);
Andy Shevchenko07d90892017-01-19 18:39:41 +0200203 return error;
Michael Demeter77145672012-01-26 17:40:27 +0000204 }
205
Andy Shevchenko07d90892017-01-19 18:39:41 +0200206 device_init_wakeup(&pdev->dev, true);
207 dev_pm_set_wake_irq(&pdev->dev, irq);
Hong Liu8eec8a12011-02-07 14:45:55 -0500208
Andy Shevchenko07d90892017-01-19 18:39:41 +0200209 return 0;
Hong Liu8eec8a12011-02-07 14:45:55 -0500210}
211
Andy Shevchenko48b44522017-01-19 18:39:42 +0200212static int mid_pb_remove(struct platform_device *pdev)
Hong Liu8eec8a12011-02-07 14:45:55 -0500213{
Sudeep Holladaea5a62015-09-21 16:47:01 +0100214 dev_pm_clear_wake_irq(&pdev->dev);
215 device_init_wakeup(&pdev->dev, false);
Ameya Palandeb9e06692011-04-06 17:44:37 +0300216
Hong Liu8eec8a12011-02-07 14:45:55 -0500217 return 0;
218}
219
Andy Shevchenko48b44522017-01-19 18:39:42 +0200220static struct platform_driver mid_pb_driver = {
Hong Liu8eec8a12011-02-07 14:45:55 -0500221 .driver = {
222 .name = DRIVER_NAME,
Hong Liu8eec8a12011-02-07 14:45:55 -0500223 },
Andy Shevchenko48b44522017-01-19 18:39:42 +0200224 .probe = mid_pb_probe,
225 .remove = mid_pb_remove,
Hong Liu8eec8a12011-02-07 14:45:55 -0500226};
227
Andy Shevchenko48b44522017-01-19 18:39:42 +0200228module_platform_driver(mid_pb_driver);
Hong Liu8eec8a12011-02-07 14:45:55 -0500229
230MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>");
Andy Shevchenko48b44522017-01-19 18:39:42 +0200231MODULE_DESCRIPTION("Intel MID Power Button Driver");
Hong Liu8eec8a12011-02-07 14:45:55 -0500232MODULE_LICENSE("GPL v2");
233MODULE_ALIAS("platform:" DRIVER_NAME);