Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 1 | /* |
| 2 | * 88pm860x_onkey.c - Marvell 88PM860x ONKEY driver |
| 3 | * |
| 4 | * Copyright (C) 2009-2010 Marvell International Ltd. |
| 5 | * Haojian Zhuang <haojian.zhuang@marvell.com> |
| 6 | * |
| 7 | * This file is subject to the terms and conditions of the GNU General |
| 8 | * Public License. See the file "COPYING" in the main directory of this |
| 9 | * archive for more details. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/i2c.h> |
| 25 | #include <linux/input.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/mfd/88pm860x.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Himangi Saraogi | cde51e7 | 2014-05-29 00:17:59 -0700 | [diff] [blame] | 29 | #include <linux/device.h> |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 30 | |
| 31 | #define PM8607_WAKEUP 0x0b |
| 32 | |
| 33 | #define LONG_ONKEY_EN (1 << 1) |
| 34 | #define ONKEY_STATUS (1 << 0) |
| 35 | |
| 36 | struct pm860x_onkey_info { |
| 37 | struct input_dev *idev; |
| 38 | struct pm860x_chip *chip; |
| 39 | struct i2c_client *i2c; |
| 40 | struct device *dev; |
| 41 | int irq; |
| 42 | }; |
| 43 | |
| 44 | /* 88PM860x gives us an interrupt when ONKEY is held */ |
| 45 | static irqreturn_t pm860x_onkey_handler(int irq, void *data) |
| 46 | { |
| 47 | struct pm860x_onkey_info *info = data; |
| 48 | int ret; |
| 49 | |
| 50 | ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2); |
| 51 | ret &= ONKEY_STATUS; |
| 52 | input_report_key(info->idev, KEY_POWER, ret); |
| 53 | input_sync(info->idev); |
| 54 | |
| 55 | /* Enable 8-second long onkey detection */ |
| 56 | pm860x_set_bits(info->i2c, PM8607_WAKEUP, 3, LONG_ONKEY_EN); |
| 57 | return IRQ_HANDLED; |
| 58 | } |
| 59 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 60 | static int pm860x_onkey_probe(struct platform_device *pdev) |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 61 | { |
| 62 | struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); |
| 63 | struct pm860x_onkey_info *info; |
| 64 | int irq, ret; |
| 65 | |
| 66 | irq = platform_get_irq(pdev, 0); |
| 67 | if (irq < 0) { |
| 68 | dev_err(&pdev->dev, "No IRQ resource!\n"); |
| 69 | return -EINVAL; |
| 70 | } |
| 71 | |
Himangi Saraogi | cde51e7 | 2014-05-29 00:17:59 -0700 | [diff] [blame] | 72 | info = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_onkey_info), |
| 73 | GFP_KERNEL); |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 74 | if (!info) |
| 75 | return -ENOMEM; |
| 76 | info->chip = chip; |
| 77 | info->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion; |
| 78 | info->dev = &pdev->dev; |
Haojian Zhuang | c9f560b | 2011-03-07 23:43:12 +0800 | [diff] [blame] | 79 | info->irq = irq; |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 80 | |
Himangi Saraogi | cde51e7 | 2014-05-29 00:17:59 -0700 | [diff] [blame] | 81 | info->idev = devm_input_allocate_device(&pdev->dev); |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 82 | if (!info->idev) { |
| 83 | dev_err(chip->dev, "Failed to allocate input dev\n"); |
Himangi Saraogi | cde51e7 | 2014-05-29 00:17:59 -0700 | [diff] [blame] | 84 | return -ENOMEM; |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | info->idev->name = "88pm860x_on"; |
| 88 | info->idev->phys = "88pm860x_on/input0"; |
| 89 | info->idev->id.bustype = BUS_I2C; |
| 90 | info->idev->dev.parent = &pdev->dev; |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 91 | info->idev->evbit[0] = BIT_MASK(EV_KEY); |
| 92 | info->idev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER); |
| 93 | |
| 94 | ret = input_register_device(info->idev); |
| 95 | if (ret) { |
| 96 | dev_err(chip->dev, "Can't register input device: %d\n", ret); |
Himangi Saraogi | cde51e7 | 2014-05-29 00:17:59 -0700 | [diff] [blame] | 97 | return ret; |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Himangi Saraogi | cde51e7 | 2014-05-29 00:17:59 -0700 | [diff] [blame] | 100 | ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL, |
| 101 | pm860x_onkey_handler, IRQF_ONESHOT, |
| 102 | "onkey", info); |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 103 | if (ret < 0) { |
| 104 | dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", |
| 105 | info->irq, ret); |
Himangi Saraogi | cde51e7 | 2014-05-29 00:17:59 -0700 | [diff] [blame] | 106 | return ret; |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | platform_set_drvdata(pdev, info); |
Jett.Zhou | 2853378 | 2012-02-27 15:44:20 +0100 | [diff] [blame] | 110 | device_init_wakeup(&pdev->dev, 1); |
| 111 | |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 112 | return 0; |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Jett.Zhou | 2853378 | 2012-02-27 15:44:20 +0100 | [diff] [blame] | 115 | #ifdef CONFIG_PM_SLEEP |
| 116 | static int pm860x_onkey_suspend(struct device *dev) |
| 117 | { |
| 118 | struct platform_device *pdev = to_platform_device(dev); |
| 119 | struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); |
| 120 | |
| 121 | if (device_may_wakeup(dev)) |
| 122 | chip->wakeup_flag |= 1 << PM8607_IRQ_ONKEY; |
| 123 | return 0; |
| 124 | } |
| 125 | static int pm860x_onkey_resume(struct device *dev) |
| 126 | { |
| 127 | struct platform_device *pdev = to_platform_device(dev); |
| 128 | struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); |
| 129 | |
| 130 | if (device_may_wakeup(dev)) |
| 131 | chip->wakeup_flag &= ~(1 << PM8607_IRQ_ONKEY); |
| 132 | return 0; |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | static SIMPLE_DEV_PM_OPS(pm860x_onkey_pm_ops, pm860x_onkey_suspend, pm860x_onkey_resume); |
| 137 | |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 138 | static struct platform_driver pm860x_onkey_driver = { |
| 139 | .driver = { |
| 140 | .name = "88pm860x-onkey", |
| 141 | .owner = THIS_MODULE, |
Jett.Zhou | 2853378 | 2012-02-27 15:44:20 +0100 | [diff] [blame] | 142 | .pm = &pm860x_onkey_pm_ops, |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 143 | }, |
| 144 | .probe = pm860x_onkey_probe, |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 145 | }; |
JJ Ding | 840a746 | 2011-11-29 11:08:40 -0800 | [diff] [blame] | 146 | module_platform_driver(pm860x_onkey_driver); |
Haojian Zhuang | 6985403 | 2010-02-03 15:40:59 -0500 | [diff] [blame] | 147 | |
| 148 | MODULE_DESCRIPTION("Marvell 88PM860x ONKEY driver"); |
| 149 | MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>"); |
| 150 | MODULE_LICENSE("GPL"); |