| 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/input.h> |
| Andy Shevchenko | 7591b9f | 2017-01-19 18:39:48 +0200 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 21 | #include <linux/mfd/intel_msic.h> |
| Andy Shevchenko | 7591b9f | 2017-01-19 18:39:48 +0200 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/platform_device.h> |
| Sudeep Holla | daea5a6 | 2015-09-21 16:47:01 +0100 | [diff] [blame] | 24 | #include <linux/pm_wakeirq.h> |
| Andy Shevchenko | 7591b9f | 2017-01-19 18:39:48 +0200 | [diff] [blame] | 25 | #include <linux/slab.h> |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 26 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 27 | #include <asm/cpu_device_id.h> |
| 28 | #include <asm/intel-family.h> |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 29 | #include <asm/intel_scu_ipc.h> |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 30 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 31 | #define DRIVER_NAME "msic_power_btn" |
| 32 | |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 33 | #define MSIC_PB_LEVEL (1 << 3) /* 1 - release, 0 - press */ |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 34 | |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 35 | /* |
| 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 Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 41 | /* Intel Tangier */ |
| Andy Shevchenko | b30f3f8 | 2017-02-02 19:54:26 +0200 | [diff] [blame] | 42 | #define BCOVE_PB_LEVEL (1 << 4) /* 1 - release, 0 - press */ |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 43 | |
| 44 | /* Basin Cove PMIC */ |
| 45 | #define BCOVE_PBIRQ 0x02 |
| 46 | #define BCOVE_IRQLVL1MSK 0x0c |
| 47 | #define BCOVE_PBIRQMASK 0x0d |
| Andy Shevchenko | b30f3f8 | 2017-02-02 19:54:26 +0200 | [diff] [blame] | 48 | #define BCOVE_PBSTATUS 0x27 |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 49 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 50 | struct mid_pb_ddata { |
| 51 | struct device *dev; |
| 52 | int irq; |
| 53 | struct input_dev *input; |
| Andy Shevchenko | ca45ba0 | 2017-02-02 19:54:28 +0200 | [diff] [blame] | 54 | unsigned short mirqlvl1_addr; |
| Andy Shevchenko | b30f3f8 | 2017-02-02 19:54:26 +0200 | [diff] [blame] | 55 | unsigned short pbstat_addr; |
| 56 | u8 pbstat_mask; |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 57 | int (*setup)(struct mid_pb_ddata *ddata); |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
| Andy Shevchenko | b30f3f8 | 2017-02-02 19:54:26 +0200 | [diff] [blame] | 60 | static int mid_pbstat(struct mid_pb_ddata *ddata, int *value) |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 61 | { |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 62 | struct input_dev *input = ddata->input; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 63 | int ret; |
| 64 | u8 pbstat; |
| 65 | |
| Andy Shevchenko | 25b4a38 | 2017-02-08 19:03:19 +0200 | [diff] [blame] | 66 | ret = intel_scu_ipc_ioread8(ddata->pbstat_addr, &pbstat); |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 67 | if (ret) |
| 68 | return ret; |
| 69 | |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 70 | dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat); |
| 71 | |
| Andy Shevchenko | b30f3f8 | 2017-02-02 19:54:26 +0200 | [diff] [blame] | 72 | *value = !(pbstat & ddata->pbstat_mask); |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| Andy Shevchenko | ca45ba0 | 2017-02-02 19:54:28 +0200 | [diff] [blame] | 76 | static int mid_irq_ack(struct mid_pb_ddata *ddata) |
| Andy Shevchenko | 4b819c6 | 2017-01-19 18:39:44 +0200 | [diff] [blame] | 77 | { |
| Andy Shevchenko | 25b4a38 | 2017-02-08 19:03:19 +0200 | [diff] [blame] | 78 | return intel_scu_ipc_update_register(ddata->mirqlvl1_addr, 0, MSIC_PWRBTNM); |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static int mrfld_setup(struct mid_pb_ddata *ddata) |
| 82 | { |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 83 | /* 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 Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 90 | static 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 Shevchenko | b30f3f8 | 2017-02-02 19:54:26 +0200 | [diff] [blame] | 94 | int value = 0; |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 95 | int ret; |
| 96 | |
| Andy Shevchenko | b30f3f8 | 2017-02-02 19:54:26 +0200 | [diff] [blame] | 97 | ret = mid_pbstat(ddata, &value); |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 98 | if (ret < 0) { |
| Andy Shevchenko | fdde1a82 | 2017-01-19 18:39:47 +0200 | [diff] [blame] | 99 | dev_err(input->dev.parent, |
| 100 | "Read error %d while reading MSIC_PB_STATUS\n", ret); |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 101 | } else { |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 102 | input_event(input, EV_KEY, KEY_POWER, value); |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 103 | input_sync(input); |
| 104 | } |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 105 | |
| Andy Shevchenko | ca45ba0 | 2017-02-02 19:54:28 +0200 | [diff] [blame] | 106 | mid_irq_ack(ddata); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 107 | return IRQ_HANDLED; |
| 108 | } |
| 109 | |
| Bhumika Goyal | c94a8ff | 2017-08-11 19:50:00 +0530 | [diff] [blame] | 110 | static const struct mid_pb_ddata mfld_ddata = { |
| Andy Shevchenko | ca45ba0 | 2017-02-02 19:54:28 +0200 | [diff] [blame] | 111 | .mirqlvl1_addr = INTEL_MSIC_IRQLVL1MSK, |
| Andy Shevchenko | b30f3f8 | 2017-02-02 19:54:26 +0200 | [diff] [blame] | 112 | .pbstat_addr = INTEL_MSIC_PBSTATUS, |
| 113 | .pbstat_mask = MSIC_PB_LEVEL, |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 114 | }; |
| 115 | |
| Bhumika Goyal | c94a8ff | 2017-08-11 19:50:00 +0530 | [diff] [blame] | 116 | static const struct mid_pb_ddata mrfld_ddata = { |
| Andy Shevchenko | ca45ba0 | 2017-02-02 19:54:28 +0200 | [diff] [blame] | 117 | .mirqlvl1_addr = BCOVE_IRQLVL1MSK, |
| Andy Shevchenko | b30f3f8 | 2017-02-02 19:54:26 +0200 | [diff] [blame] | 118 | .pbstat_addr = BCOVE_PBSTATUS, |
| 119 | .pbstat_mask = BCOVE_PB_LEVEL, |
| Andy Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 120 | .setup = mrfld_setup, |
| 121 | }; |
| 122 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 123 | static const struct x86_cpu_id mid_pb_cpu_ids[] = { |
| Andy Shevchenko | a8b60e4 | 2018-08-31 11:34:31 +0300 | [diff] [blame] | 124 | INTEL_CPU_FAM6(ATOM_PENWELL, mfld_ddata), |
| 125 | INTEL_CPU_FAM6(ATOM_MERRIFIELD, mrfld_ddata), |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 126 | {} |
| 127 | }; |
| 128 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 129 | static int mid_pb_probe(struct platform_device *pdev) |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 130 | { |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 131 | const struct x86_cpu_id *id; |
| 132 | struct mid_pb_ddata *ddata; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 133 | struct input_dev *input; |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 134 | int irq = platform_get_irq(pdev, 0); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 135 | int error; |
| 136 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 137 | id = x86_match_cpu(mid_pb_cpu_ids); |
| 138 | if (!id) |
| 139 | return -ENODEV; |
| 140 | |
| Gustavo A. R. Silva | fe4e8d0 | 2017-08-09 11:00:54 -0500 | [diff] [blame] | 141 | if (irq < 0) { |
| 142 | dev_err(&pdev->dev, "Failed to get IRQ: %d\n", irq); |
| 143 | return irq; |
| 144 | } |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 145 | |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 146 | input = devm_input_allocate_device(&pdev->dev); |
| Joe Perches | b222cca | 2013-10-23 12:14:52 -0700 | [diff] [blame] | 147 | if (!input) |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 148 | return -ENOMEM; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 149 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 150 | 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 Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 157 | 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 Shevchenko | 6a0f998 | 2017-01-19 18:39:46 +0200 | [diff] [blame] | 165 | if (ddata->setup) { |
| 166 | error = ddata->setup(ddata); |
| 167 | if (error) |
| 168 | return error; |
| 169 | } |
| 170 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 171 | error = devm_request_threaded_irq(&pdev->dev, irq, NULL, mid_pb_isr, |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 172 | IRQF_ONESHOT, DRIVER_NAME, ddata); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 173 | if (error) { |
| Andy Shevchenko | fdde1a82 | 2017-01-19 18:39:47 +0200 | [diff] [blame] | 174 | dev_err(&pdev->dev, |
| 175 | "Unable to request irq %d for MID power button\n", irq); |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 176 | return error; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | error = input_register_device(input); |
| 180 | if (error) { |
| Andy Shevchenko | fdde1a82 | 2017-01-19 18:39:47 +0200 | [diff] [blame] | 181 | dev_err(&pdev->dev, |
| 182 | "Unable to register input dev, error %d\n", error); |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 183 | return error; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| Andy Shevchenko | 18934ec | 2017-01-19 18:39:43 +0200 | [diff] [blame] | 186 | platform_set_drvdata(pdev, ddata); |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 187 | |
| Andy Shevchenko | 5cb44ee | 2017-02-02 19:54:27 +0200 | [diff] [blame] | 188 | /* |
| 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 Shevchenko | ca45ba0 | 2017-02-02 19:54:28 +0200 | [diff] [blame] | 198 | error = mid_irq_ack(ddata); |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 199 | if (error) { |
| Andy Shevchenko | fdde1a82 | 2017-01-19 18:39:47 +0200 | [diff] [blame] | 200 | dev_err(&pdev->dev, |
| 201 | "Unable to clear power button interrupt, error: %d\n", |
| 202 | error); |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 203 | return error; |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 206 | device_init_wakeup(&pdev->dev, true); |
| 207 | dev_pm_set_wake_irq(&pdev->dev, irq); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 208 | |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 209 | return 0; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 210 | } |
| 211 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 212 | static int mid_pb_remove(struct platform_device *pdev) |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 213 | { |
| Sudeep Holla | daea5a6 | 2015-09-21 16:47:01 +0100 | [diff] [blame] | 214 | dev_pm_clear_wake_irq(&pdev->dev); |
| 215 | device_init_wakeup(&pdev->dev, false); |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 216 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 220 | static struct platform_driver mid_pb_driver = { |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 221 | .driver = { |
| 222 | .name = DRIVER_NAME, |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 223 | }, |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 224 | .probe = mid_pb_probe, |
| 225 | .remove = mid_pb_remove, |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 226 | }; |
| 227 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 228 | module_platform_driver(mid_pb_driver); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 229 | |
| 230 | MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>"); |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame] | 231 | MODULE_DESCRIPTION("Intel MID Power Button Driver"); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 232 | MODULE_LICENSE("GPL v2"); |
| 233 | MODULE_ALIAS("platform:" DRIVER_NAME); |