| 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 | * |
| Andy Shevchenko | 1cfd3ba | 2017-01-19 18:39:49 +0200 | [diff] [blame^] | 4 | * 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 Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 8 | * |
| 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 Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 17 | */ |
| 18 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 19 | #include <linux/init.h> |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 20 | #include <linux/input.h> |
| Andy Shevchenko | 7591b9f | 2017-01-19 18:39:48 +0200 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 22 | #include <linux/mfd/intel_msic.h> |
| Andy Shevchenko | 7591b9f | 2017-01-19 18:39:48 +0200 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/platform_device.h> |
| Sudeep Holla | daea5a6 | 2015-09-21 16:47:01 +0100 | [diff] [blame] | 25 | #include <linux/pm_wakeirq.h> |
| Andy Shevchenko | 7591b9f | 2017-01-19 18:39:48 +0200 | [diff] [blame] | 26 | #include <linux/slab.h> |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 27 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 28 | #include <asm/cpu_device_id.h> |
| 29 | #include <asm/intel-family.h> |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 30 | #include <asm/intel_scu_ipc.h> |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 31 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 32 | #define DRIVER_NAME "msic_power_btn" |
| 33 | |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 34 | #define MSIC_PB_LEVEL (1 << 3) /* 1 - release, 0 - press */ |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 35 | |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 36 | /* |
| 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 Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 42 | /* Intel Tangier */ |
| 43 | #define MRFLD_PBSTAT_ADDR 0xfffff61a |
| 44 | #define MRFLD_PB_LEVEL (1 << 4) /* 1 - release, 0 - press */ |
| 45 | |
| 46 | /* Basin Cove PMIC */ |
| 47 | #define BCOVE_PBIRQ 0x02 |
| 48 | #define BCOVE_IRQLVL1MSK 0x0c |
| 49 | #define BCOVE_PBIRQMASK 0x0d |
| 50 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 51 | struct mid_pb_ddata { |
| 52 | struct device *dev; |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 53 | void __iomem *reg; |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 54 | int irq; |
| 55 | struct input_dev *input; |
| 56 | int (*pbstat)(struct mid_pb_ddata *ddata, int *value); |
| Andy Shevchenko | 4b819c6 | 2017-01-19 18:39:44 +0200 | [diff] [blame] | 57 | int (*ack)(struct mid_pb_ddata *ddata); |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 58 | int (*setup)(struct mid_pb_ddata *ddata); |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static int mfld_pbstat(struct mid_pb_ddata *ddata, int *value) |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 62 | { |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 63 | struct input_dev *input = ddata->input; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 64 | int ret; |
| 65 | u8 pbstat; |
| 66 | |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 67 | ret = intel_msic_reg_read(INTEL_MSIC_PBSTATUS, &pbstat); |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 68 | if (ret) |
| 69 | return ret; |
| 70 | |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 71 | dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat); |
| 72 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 73 | *value = !(pbstat & MSIC_PB_LEVEL); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| Andy Shevchenko | 4b819c6 | 2017-01-19 18:39:44 +0200 | [diff] [blame] | 77 | static int mfld_ack(struct mid_pb_ddata *ddata) |
| 78 | { |
| 79 | /* |
| 80 | * SCU firmware might send power button interrupts to IA core before |
| 81 | * kernel boots and doesn't get EOI from IA core. The first bit of |
| 82 | * MSIC reg 0x21 is kept masked, and SCU firmware doesn't send new |
| 83 | * power interrupt to Android kernel. Unmask the bit when probing |
| 84 | * power button in kernel. |
| 85 | * There is a very narrow race between irq handler and power button |
| 86 | * initialization. The race happens rarely. So we needn't worry |
| 87 | * about it. |
| 88 | */ |
| 89 | return intel_msic_reg_update(INTEL_MSIC_IRQLVL1MSK, 0, MSIC_PWRBTNM); |
| 90 | } |
| 91 | |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 92 | static int mrfld_pbstat(struct mid_pb_ddata *ddata, int *value) |
| 93 | { |
| 94 | struct input_dev *input = ddata->input; |
| 95 | u8 pbstat; |
| 96 | |
| 97 | pbstat = readb(ddata->reg); |
| 98 | |
| 99 | dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat); |
| 100 | |
| 101 | *value = !(pbstat & MRFLD_PB_LEVEL); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int mrfld_ack(struct mid_pb_ddata *ddata) |
| 106 | { |
| 107 | return intel_scu_ipc_update_register(BCOVE_IRQLVL1MSK, 0, MSIC_PWRBTNM); |
| 108 | } |
| 109 | |
| 110 | static int mrfld_setup(struct mid_pb_ddata *ddata) |
| 111 | { |
| 112 | ddata->reg = devm_ioremap_nocache(ddata->dev, MRFLD_PBSTAT_ADDR, 1); |
| 113 | if (!ddata->reg) |
| 114 | return -ENOMEM; |
| 115 | |
| 116 | /* Unmask the PBIRQ and MPBIRQ on Tangier */ |
| 117 | intel_scu_ipc_update_register(BCOVE_PBIRQ, 0, MSIC_PWRBTNM); |
| 118 | intel_scu_ipc_update_register(BCOVE_PBIRQMASK, 0, MSIC_PWRBTNM); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 123 | static irqreturn_t mid_pb_isr(int irq, void *dev_id) |
| 124 | { |
| 125 | struct mid_pb_ddata *ddata = dev_id; |
| 126 | struct input_dev *input = ddata->input; |
| 127 | int value; |
| 128 | int ret; |
| 129 | |
| 130 | ret = ddata->pbstat(ddata, &value); |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 131 | if (ret < 0) { |
| Andy Shevchenko | fdde1a82 | 2017-01-19 18:39:47 +0200 | [diff] [blame] | 132 | dev_err(input->dev.parent, |
| 133 | "Read error %d while reading MSIC_PB_STATUS\n", ret); |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 134 | } else { |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 135 | input_event(input, EV_KEY, KEY_POWER, value); |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 136 | input_sync(input); |
| 137 | } |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 138 | |
| Andy Shevchenko | 553e9c1 | 2017-01-19 18:39:45 +0200 | [diff] [blame] | 139 | ddata->ack(ddata); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 140 | return IRQ_HANDLED; |
| 141 | } |
| 142 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 143 | static struct mid_pb_ddata mfld_ddata = { |
| 144 | .pbstat = mfld_pbstat, |
| Andy Shevchenko | 4b819c6 | 2017-01-19 18:39:44 +0200 | [diff] [blame] | 145 | .ack = mfld_ack, |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 146 | }; |
| 147 | |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 148 | static struct mid_pb_ddata mrfld_ddata = { |
| 149 | .pbstat = mrfld_pbstat, |
| 150 | .ack = mrfld_ack, |
| 151 | .setup = mrfld_setup, |
| 152 | }; |
| 153 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 154 | #define ICPU(model, ddata) \ |
| 155 | { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (kernel_ulong_t)&ddata } |
| 156 | |
| 157 | static const struct x86_cpu_id mid_pb_cpu_ids[] = { |
| 158 | ICPU(INTEL_FAM6_ATOM_PENWELL, mfld_ddata), |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 159 | ICPU(INTEL_FAM6_ATOM_MERRIFIELD, mrfld_ddata), |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 160 | {} |
| 161 | }; |
| 162 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 163 | static int mid_pb_probe(struct platform_device *pdev) |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 164 | { |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 165 | const struct x86_cpu_id *id; |
| 166 | struct mid_pb_ddata *ddata; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 167 | struct input_dev *input; |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 168 | int irq = platform_get_irq(pdev, 0); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 169 | int error; |
| 170 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 171 | id = x86_match_cpu(mid_pb_cpu_ids); |
| 172 | if (!id) |
| 173 | return -ENODEV; |
| 174 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 175 | if (irq < 0) |
| 176 | return -EINVAL; |
| 177 | |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 178 | input = devm_input_allocate_device(&pdev->dev); |
| Joe Perches | b222cca | 2013-10-23 12:14:52 -0700 | [diff] [blame] | 179 | if (!input) |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 180 | return -ENOMEM; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 181 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 182 | input->name = pdev->name; |
| 183 | input->phys = "power-button/input0"; |
| 184 | input->id.bustype = BUS_HOST; |
| 185 | input->dev.parent = &pdev->dev; |
| 186 | |
| 187 | input_set_capability(input, EV_KEY, KEY_POWER); |
| 188 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 189 | ddata = (struct mid_pb_ddata *)id->driver_data; |
| 190 | if (!ddata) |
| 191 | return -ENODATA; |
| 192 | |
| 193 | ddata->dev = &pdev->dev; |
| 194 | ddata->irq = irq; |
| 195 | ddata->input = input; |
| 196 | |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 197 | if (ddata->setup) { |
| 198 | error = ddata->setup(ddata); |
| 199 | if (error) |
| 200 | return error; |
| 201 | } |
| 202 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 203 | error = devm_request_threaded_irq(&pdev->dev, irq, NULL, mid_pb_isr, |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 204 | IRQF_ONESHOT, DRIVER_NAME, ddata); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 205 | if (error) { |
| Andy Shevchenko | fdde1a82 | 2017-01-19 18:39:47 +0200 | [diff] [blame] | 206 | dev_err(&pdev->dev, |
| 207 | "Unable to request irq %d for MID power button\n", irq); |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 208 | return error; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | error = input_register_device(input); |
| 212 | if (error) { |
| Andy Shevchenko | fdde1a82 | 2017-01-19 18:39:47 +0200 | [diff] [blame] | 213 | dev_err(&pdev->dev, |
| 214 | "Unable to register input dev, error %d\n", error); |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 215 | return error; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 216 | } |
| 217 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 218 | platform_set_drvdata(pdev, ddata); |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 219 | |
| Andy Shevchenko | 4b819c6 | 2017-01-19 18:39:44 +0200 | [diff] [blame] | 220 | error = ddata->ack(ddata); |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 221 | if (error) { |
| Andy Shevchenko | fdde1a82 | 2017-01-19 18:39:47 +0200 | [diff] [blame] | 222 | dev_err(&pdev->dev, |
| 223 | "Unable to clear power button interrupt, error: %d\n", |
| 224 | error); |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 225 | return error; |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 228 | device_init_wakeup(&pdev->dev, true); |
| 229 | dev_pm_set_wake_irq(&pdev->dev, irq); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 230 | |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 231 | return 0; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 232 | } |
| 233 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 234 | static int mid_pb_remove(struct platform_device *pdev) |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 235 | { |
| Sudeep Holla | daea5a6 | 2015-09-21 16:47:01 +0100 | [diff] [blame] | 236 | dev_pm_clear_wake_irq(&pdev->dev); |
| 237 | device_init_wakeup(&pdev->dev, false); |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 238 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 242 | static struct platform_driver mid_pb_driver = { |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 243 | .driver = { |
| 244 | .name = DRIVER_NAME, |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 245 | }, |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 246 | .probe = mid_pb_probe, |
| 247 | .remove = mid_pb_remove, |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 248 | }; |
| 249 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 250 | module_platform_driver(mid_pb_driver); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 251 | |
| 252 | MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>"); |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 253 | MODULE_DESCRIPTION("Intel MID Power Button Driver"); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 254 | MODULE_LICENSE("GPL v2"); |
| 255 | MODULE_ALIAS("platform:" DRIVER_NAME); |