| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 1 | /* |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 2 | * Power button driver for Intel MID platforms. |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 3 | * |
| 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 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 20 | #include <linux/init.h> |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 21 | #include <linux/input.h> |
| Andy Shevchenko | 7591b9f | 2017-01-19 18:39:48 +0200 | [diff] [blame^] | 22 | #include <linux/interrupt.h> |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 23 | #include <linux/mfd/intel_msic.h> |
| Andy Shevchenko | 7591b9f | 2017-01-19 18:39:48 +0200 | [diff] [blame^] | 24 | #include <linux/module.h> |
| 25 | #include <linux/platform_device.h> |
| Sudeep Holla | daea5a6 | 2015-09-21 16:47:01 +0100 | [diff] [blame] | 26 | #include <linux/pm_wakeirq.h> |
| Andy Shevchenko | 7591b9f | 2017-01-19 18:39:48 +0200 | [diff] [blame^] | 27 | #include <linux/slab.h> |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 28 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 29 | #include <asm/cpu_device_id.h> |
| 30 | #include <asm/intel-family.h> |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 31 | #include <asm/intel_scu_ipc.h> |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 32 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 33 | #define DRIVER_NAME "msic_power_btn" |
| 34 | |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 35 | #define MSIC_PB_LEVEL (1 << 3) /* 1 - release, 0 - press */ |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 36 | |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 37 | /* |
| 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 Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 43 | /* 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 Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 52 | struct mid_pb_ddata { |
| 53 | struct device *dev; |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 54 | void __iomem *reg; |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 55 | int irq; |
| 56 | struct input_dev *input; |
| 57 | int (*pbstat)(struct mid_pb_ddata *ddata, int *value); |
| Andy Shevchenko | 4b819c6 | 2017-01-19 18:39:44 +0200 | [diff] [blame] | 58 | int (*ack)(struct mid_pb_ddata *ddata); |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 59 | int (*setup)(struct mid_pb_ddata *ddata); |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static int mfld_pbstat(struct mid_pb_ddata *ddata, int *value) |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 63 | { |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 64 | struct input_dev *input = ddata->input; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 65 | int ret; |
| 66 | u8 pbstat; |
| 67 | |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 68 | ret = intel_msic_reg_read(INTEL_MSIC_PBSTATUS, &pbstat); |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 69 | if (ret) |
| 70 | return ret; |
| 71 | |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 72 | dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat); |
| 73 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 74 | *value = !(pbstat & MSIC_PB_LEVEL); |
| 75 | return 0; |
| 76 | } |
| 77 | |
| Andy Shevchenko | 4b819c6 | 2017-01-19 18:39:44 +0200 | [diff] [blame] | 78 | static 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 Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 93 | static 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 | |
| 106 | static int mrfld_ack(struct mid_pb_ddata *ddata) |
| 107 | { |
| 108 | return intel_scu_ipc_update_register(BCOVE_IRQLVL1MSK, 0, MSIC_PWRBTNM); |
| 109 | } |
| 110 | |
| 111 | static 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 Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 124 | static 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 Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 132 | if (ret < 0) { |
| Andy Shevchenko | fdde1a82 | 2017-01-19 18:39:47 +0200 | [diff] [blame] | 133 | dev_err(input->dev.parent, |
| 134 | "Read error %d while reading MSIC_PB_STATUS\n", ret); |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 135 | } else { |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 136 | input_event(input, EV_KEY, KEY_POWER, value); |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 137 | input_sync(input); |
| 138 | } |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 139 | |
| Andy Shevchenko | 553e9c1 | 2017-01-19 18:39:45 +0200 | [diff] [blame] | 140 | ddata->ack(ddata); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 141 | return IRQ_HANDLED; |
| 142 | } |
| 143 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 144 | static struct mid_pb_ddata mfld_ddata = { |
| 145 | .pbstat = mfld_pbstat, |
| Andy Shevchenko | 4b819c6 | 2017-01-19 18:39:44 +0200 | [diff] [blame] | 146 | .ack = mfld_ack, |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 147 | }; |
| 148 | |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 149 | static struct mid_pb_ddata mrfld_ddata = { |
| 150 | .pbstat = mrfld_pbstat, |
| 151 | .ack = mrfld_ack, |
| 152 | .setup = mrfld_setup, |
| 153 | }; |
| 154 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 155 | #define ICPU(model, ddata) \ |
| 156 | { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (kernel_ulong_t)&ddata } |
| 157 | |
| 158 | static const struct x86_cpu_id mid_pb_cpu_ids[] = { |
| 159 | ICPU(INTEL_FAM6_ATOM_PENWELL, mfld_ddata), |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 160 | ICPU(INTEL_FAM6_ATOM_MERRIFIELD, mrfld_ddata), |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 161 | {} |
| 162 | }; |
| 163 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 164 | static int mid_pb_probe(struct platform_device *pdev) |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 165 | { |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 166 | const struct x86_cpu_id *id; |
| 167 | struct mid_pb_ddata *ddata; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 168 | struct input_dev *input; |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 169 | int irq = platform_get_irq(pdev, 0); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 170 | int error; |
| 171 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 172 | id = x86_match_cpu(mid_pb_cpu_ids); |
| 173 | if (!id) |
| 174 | return -ENODEV; |
| 175 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 176 | if (irq < 0) |
| 177 | return -EINVAL; |
| 178 | |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 179 | input = devm_input_allocate_device(&pdev->dev); |
| Joe Perches | b222cca | 2013-10-23 12:14:52 -0700 | [diff] [blame] | 180 | if (!input) |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 181 | return -ENOMEM; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 182 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 183 | 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 Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 190 | 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 Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 198 | if (ddata->setup) { |
| 199 | error = ddata->setup(ddata); |
| 200 | if (error) |
| 201 | return error; |
| 202 | } |
| 203 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 204 | error = devm_request_threaded_irq(&pdev->dev, irq, NULL, mid_pb_isr, |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 205 | IRQF_ONESHOT, DRIVER_NAME, ddata); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 206 | if (error) { |
| Andy Shevchenko | fdde1a82 | 2017-01-19 18:39:47 +0200 | [diff] [blame] | 207 | dev_err(&pdev->dev, |
| 208 | "Unable to request irq %d for MID power button\n", irq); |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 209 | return error; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | error = input_register_device(input); |
| 213 | if (error) { |
| Andy Shevchenko | fdde1a82 | 2017-01-19 18:39:47 +0200 | [diff] [blame] | 214 | dev_err(&pdev->dev, |
| 215 | "Unable to register input dev, error %d\n", error); |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 216 | return error; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 217 | } |
| 218 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 219 | platform_set_drvdata(pdev, ddata); |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 220 | |
| Andy Shevchenko | 4b819c6 | 2017-01-19 18:39:44 +0200 | [diff] [blame] | 221 | error = ddata->ack(ddata); |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 222 | if (error) { |
| Andy Shevchenko | fdde1a82 | 2017-01-19 18:39:47 +0200 | [diff] [blame] | 223 | dev_err(&pdev->dev, |
| 224 | "Unable to clear power button interrupt, error: %d\n", |
| 225 | error); |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 226 | return error; |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 229 | device_init_wakeup(&pdev->dev, true); |
| 230 | dev_pm_set_wake_irq(&pdev->dev, irq); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 231 | |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 232 | return 0; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 233 | } |
| 234 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 235 | static int mid_pb_remove(struct platform_device *pdev) |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 236 | { |
| Sudeep Holla | daea5a6 | 2015-09-21 16:47:01 +0100 | [diff] [blame] | 237 | dev_pm_clear_wake_irq(&pdev->dev); |
| 238 | device_init_wakeup(&pdev->dev, false); |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 239 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 240 | return 0; |
| 241 | } |
| 242 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 243 | static struct platform_driver mid_pb_driver = { |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 244 | .driver = { |
| 245 | .name = DRIVER_NAME, |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 246 | }, |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 247 | .probe = mid_pb_probe, |
| 248 | .remove = mid_pb_remove, |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 249 | }; |
| 250 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 251 | module_platform_driver(mid_pb_driver); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 252 | |
| 253 | MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>"); |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 254 | MODULE_DESCRIPTION("Intel MID Power Button Driver"); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 255 | MODULE_LICENSE("GPL v2"); |
| 256 | MODULE_ALIAS("platform:" DRIVER_NAME); |