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