| 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 | |
| 97 | return IRQ_HANDLED; |
| 98 | } |
| 99 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 100 | static struct mid_pb_ddata mfld_ddata = { |
| 101 | .pbstat = mfld_pbstat, |
| Andy Shevchenko | 4b819c6 | 2017-01-19 18:39:44 +0200 | [diff] [blame^] | 102 | .ack = mfld_ack, |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | #define ICPU(model, ddata) \ |
| 106 | { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (kernel_ulong_t)&ddata } |
| 107 | |
| 108 | static const struct x86_cpu_id mid_pb_cpu_ids[] = { |
| 109 | ICPU(INTEL_FAM6_ATOM_PENWELL, mfld_ddata), |
| 110 | {} |
| 111 | }; |
| 112 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 113 | static int mid_pb_probe(struct platform_device *pdev) |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 114 | { |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 115 | const struct x86_cpu_id *id; |
| 116 | struct mid_pb_ddata *ddata; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 117 | struct input_dev *input; |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 118 | int irq = platform_get_irq(pdev, 0); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 119 | int error; |
| 120 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 121 | id = x86_match_cpu(mid_pb_cpu_ids); |
| 122 | if (!id) |
| 123 | return -ENODEV; |
| 124 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 125 | if (irq < 0) |
| 126 | return -EINVAL; |
| 127 | |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 128 | input = devm_input_allocate_device(&pdev->dev); |
| Joe Perches | b222cca | 2013-10-23 12:14:52 -0700 | [diff] [blame] | 129 | if (!input) |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 130 | return -ENOMEM; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 131 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 132 | input->name = pdev->name; |
| 133 | input->phys = "power-button/input0"; |
| 134 | input->id.bustype = BUS_HOST; |
| 135 | input->dev.parent = &pdev->dev; |
| 136 | |
| 137 | input_set_capability(input, EV_KEY, KEY_POWER); |
| 138 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 139 | ddata = (struct mid_pb_ddata *)id->driver_data; |
| 140 | if (!ddata) |
| 141 | return -ENODATA; |
| 142 | |
| 143 | ddata->dev = &pdev->dev; |
| 144 | ddata->irq = irq; |
| 145 | ddata->input = input; |
| 146 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 147 | error = devm_request_threaded_irq(&pdev->dev, irq, NULL, mid_pb_isr, |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 148 | IRQF_ONESHOT, DRIVER_NAME, ddata); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 149 | if (error) { |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 150 | dev_err(&pdev->dev, "Unable to request irq %d for MID power" |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 151 | "button\n", irq); |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 152 | return error; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | error = input_register_device(input); |
| 156 | if (error) { |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 157 | dev_err(&pdev->dev, "Unable to register input dev, error " |
| 158 | "%d\n", error); |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 159 | return error; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 160 | } |
| 161 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 162 | platform_set_drvdata(pdev, ddata); |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 163 | |
| Andy Shevchenko | 4b819c6 | 2017-01-19 18:39:44 +0200 | [diff] [blame^] | 164 | error = ddata->ack(ddata); |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 165 | if (error) { |
| 166 | dev_err(&pdev->dev, "Unable to clear power button interrupt, " |
| 167 | "error: %d\n", error); |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 168 | return error; |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 171 | device_init_wakeup(&pdev->dev, true); |
| 172 | dev_pm_set_wake_irq(&pdev->dev, irq); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 173 | |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 174 | return 0; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 175 | } |
| 176 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 177 | static int mid_pb_remove(struct platform_device *pdev) |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 178 | { |
| Sudeep Holla | daea5a6 | 2015-09-21 16:47:01 +0100 | [diff] [blame] | 179 | dev_pm_clear_wake_irq(&pdev->dev); |
| 180 | device_init_wakeup(&pdev->dev, false); |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 181 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 185 | static struct platform_driver mid_pb_driver = { |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 186 | .driver = { |
| 187 | .name = DRIVER_NAME, |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 188 | }, |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 189 | .probe = mid_pb_probe, |
| 190 | .remove = mid_pb_remove, |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 191 | }; |
| 192 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 193 | module_platform_driver(mid_pb_driver); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 194 | |
| 195 | MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>"); |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 196 | MODULE_DESCRIPTION("Intel MID Power Button Driver"); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 197 | MODULE_LICENSE("GPL v2"); |
| 198 | MODULE_ALIAS("platform:" DRIVER_NAME); |