| 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 | |
| 29 | #define DRIVER_NAME "msic_power_btn" |
| 30 | |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 31 | #define MSIC_PB_LEVEL (1 << 3) /* 1 - release, 0 - press */ |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 32 | |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 33 | /* |
| 34 | * MSIC document ti_datasheet defines the 1st bit reg 0x21 is used to mask |
| 35 | * power button interrupt |
| 36 | */ |
| 37 | #define MSIC_PWRBTNM (1 << 0) |
| 38 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame^] | 39 | static irqreturn_t mid_pb_isr(int irq, void *dev_id) |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 40 | { |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 41 | struct input_dev *input = dev_id; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 42 | int ret; |
| 43 | u8 pbstat; |
| 44 | |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 45 | ret = intel_msic_reg_read(INTEL_MSIC_PBSTATUS, &pbstat); |
| 46 | dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat); |
| 47 | |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 48 | if (ret < 0) { |
| 49 | dev_err(input->dev.parent, "Read error %d while reading" |
| 50 | " MSIC_PB_STATUS\n", ret); |
| 51 | } else { |
| 52 | input_event(input, EV_KEY, KEY_POWER, |
| 53 | !(pbstat & MSIC_PB_LEVEL)); |
| 54 | input_sync(input); |
| 55 | } |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 56 | |
| 57 | return IRQ_HANDLED; |
| 58 | } |
| 59 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame^] | 60 | static int mid_pb_probe(struct platform_device *pdev) |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 61 | { |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 62 | struct input_dev *input; |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 63 | int irq = platform_get_irq(pdev, 0); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 64 | int error; |
| 65 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 66 | if (irq < 0) |
| 67 | return -EINVAL; |
| 68 | |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 69 | input = devm_input_allocate_device(&pdev->dev); |
| Joe Perches | b222cca | 2013-10-23 12:14:52 -0700 | [diff] [blame] | 70 | if (!input) |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 71 | return -ENOMEM; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 72 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 73 | input->name = pdev->name; |
| 74 | input->phys = "power-button/input0"; |
| 75 | input->id.bustype = BUS_HOST; |
| 76 | input->dev.parent = &pdev->dev; |
| 77 | |
| 78 | input_set_capability(input, EV_KEY, KEY_POWER); |
| 79 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame^] | 80 | error = devm_request_threaded_irq(&pdev->dev, irq, NULL, mid_pb_isr, |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 81 | IRQF_ONESHOT, DRIVER_NAME, input); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 82 | if (error) { |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame^] | 83 | dev_err(&pdev->dev, "Unable to request irq %d for MID power" |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 84 | "button\n", irq); |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 85 | return error; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | error = input_register_device(input); |
| 89 | if (error) { |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 90 | dev_err(&pdev->dev, "Unable to register input dev, error " |
| 91 | "%d\n", error); |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 92 | return error; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 95 | platform_set_drvdata(pdev, input); |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * SCU firmware might send power button interrupts to IA core before |
| 99 | * kernel boots and doesn't get EOI from IA core. The first bit of |
| 100 | * MSIC reg 0x21 is kept masked, and SCU firmware doesn't send new |
| 101 | * power interrupt to Android kernel. Unmask the bit when probing |
| 102 | * power button in kernel. |
| 103 | * There is a very narrow race between irq handler and power button |
| 104 | * initialization. The race happens rarely. So we needn't worry |
| 105 | * about it. |
| 106 | */ |
| 107 | error = intel_msic_reg_update(INTEL_MSIC_IRQLVL1MSK, 0, MSIC_PWRBTNM); |
| 108 | if (error) { |
| 109 | dev_err(&pdev->dev, "Unable to clear power button interrupt, " |
| 110 | "error: %d\n", error); |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 111 | return error; |
| Michael Demeter | 7714567 | 2012-01-26 17:40:27 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 114 | device_init_wakeup(&pdev->dev, true); |
| 115 | dev_pm_set_wake_irq(&pdev->dev, irq); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 116 | |
| Andy Shevchenko | 07d9089 | 2017-01-19 18:39:41 +0200 | [diff] [blame] | 117 | return 0; |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 118 | } |
| 119 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame^] | 120 | static int mid_pb_remove(struct platform_device *pdev) |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 121 | { |
| Sudeep Holla | daea5a6 | 2015-09-21 16:47:01 +0100 | [diff] [blame] | 122 | dev_pm_clear_wake_irq(&pdev->dev); |
| 123 | device_init_wakeup(&pdev->dev, false); |
| Ameya Palande | b9e0669 | 2011-04-06 17:44:37 +0300 | [diff] [blame] | 124 | |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame^] | 128 | static struct platform_driver mid_pb_driver = { |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 129 | .driver = { |
| 130 | .name = DRIVER_NAME, |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 131 | }, |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame^] | 132 | .probe = mid_pb_probe, |
| 133 | .remove = mid_pb_remove, |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 134 | }; |
| 135 | |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame^] | 136 | module_platform_driver(mid_pb_driver); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 137 | |
| 138 | MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>"); |
| Andy Shevchenko | 48b4452 | 2017-01-19 18:39:42 +0200 | [diff] [blame^] | 139 | MODULE_DESCRIPTION("Intel MID Power Button Driver"); |
| Hong Liu | 8eec8a1 | 2011-02-07 14:45:55 -0500 | [diff] [blame] | 140 | MODULE_LICENSE("GPL v2"); |
| 141 | MODULE_ALIAS("platform:" DRIVER_NAME); |