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