Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 1 | /** |
Dmitry Torokhov | 104a5f3 | 2012-03-06 09:10:21 -0800 | [diff] [blame] | 2 | * MAX8925 ONKEY driver |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2009 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/max8925.h> |
| 28 | #include <linux/slab.h> |
Himangi Saraogi | 04115e4 | 2014-05-29 00:21:56 -0700 | [diff] [blame^] | 29 | #include <linux/device.h> |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 30 | |
Haojian Zhuang | 2d95ae3 | 2010-09-08 09:44:35 -0400 | [diff] [blame] | 31 | #define SW_INPUT (1 << 7) /* 0/1 -- up/down */ |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 32 | #define HARDRESET_EN (1 << 7) |
| 33 | #define PWREN_EN (1 << 7) |
| 34 | |
| 35 | struct max8925_onkey_info { |
| 36 | struct input_dev *idev; |
| 37 | struct i2c_client *i2c; |
Haojian Zhuang | 2d95ae3 | 2010-09-08 09:44:35 -0400 | [diff] [blame] | 38 | struct device *dev; |
Dmitry Torokhov | 104a5f3 | 2012-03-06 09:10:21 -0800 | [diff] [blame] | 39 | unsigned int irq[2]; |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | /* |
Haojian Zhuang | 2d95ae3 | 2010-09-08 09:44:35 -0400 | [diff] [blame] | 43 | * MAX8925 gives us an interrupt when ONKEY is pressed or released. |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 44 | * max8925_set_bits() operates I2C bus and may sleep. So implement |
| 45 | * it in thread IRQ handler. |
| 46 | */ |
| 47 | static irqreturn_t max8925_onkey_handler(int irq, void *data) |
| 48 | { |
| 49 | struct max8925_onkey_info *info = data; |
Dmitry Torokhov | 104a5f3 | 2012-03-06 09:10:21 -0800 | [diff] [blame] | 50 | int state; |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 51 | |
Dmitry Torokhov | 104a5f3 | 2012-03-06 09:10:21 -0800 | [diff] [blame] | 52 | state = max8925_reg_read(info->i2c, MAX8925_ON_OFF_STATUS); |
| 53 | |
| 54 | input_report_key(info->idev, KEY_POWER, state & SW_INPUT); |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 55 | input_sync(info->idev); |
| 56 | |
Dmitry Torokhov | 104a5f3 | 2012-03-06 09:10:21 -0800 | [diff] [blame] | 57 | dev_dbg(info->dev, "onkey state:%d\n", state); |
Haojian Zhuang | 2d95ae3 | 2010-09-08 09:44:35 -0400 | [diff] [blame] | 58 | |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 59 | /* Enable hardreset to halt if system isn't shutdown on time */ |
| 60 | max8925_set_bits(info->i2c, MAX8925_SYSENSEL, |
| 61 | HARDRESET_EN, HARDRESET_EN); |
| 62 | |
| 63 | return IRQ_HANDLED; |
| 64 | } |
| 65 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 66 | static int max8925_onkey_probe(struct platform_device *pdev) |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 67 | { |
| 68 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); |
| 69 | struct max8925_onkey_info *info; |
Dmitry Torokhov | 104a5f3 | 2012-03-06 09:10:21 -0800 | [diff] [blame] | 70 | struct input_dev *input; |
Haojian Zhuang | 2d95ae3 | 2010-09-08 09:44:35 -0400 | [diff] [blame] | 71 | int irq[2], error; |
| 72 | |
| 73 | irq[0] = platform_get_irq(pdev, 0); |
| 74 | if (irq[0] < 0) { |
| 75 | dev_err(&pdev->dev, "No IRQ resource!\n"); |
| 76 | return -EINVAL; |
| 77 | } |
Dmitry Torokhov | 104a5f3 | 2012-03-06 09:10:21 -0800 | [diff] [blame] | 78 | |
Haojian Zhuang | 2d95ae3 | 2010-09-08 09:44:35 -0400 | [diff] [blame] | 79 | irq[1] = platform_get_irq(pdev, 1); |
| 80 | if (irq[1] < 0) { |
| 81 | dev_err(&pdev->dev, "No IRQ resource!\n"); |
| 82 | return -EINVAL; |
| 83 | } |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 84 | |
Himangi Saraogi | 04115e4 | 2014-05-29 00:21:56 -0700 | [diff] [blame^] | 85 | info = devm_kzalloc(&pdev->dev, sizeof(struct max8925_onkey_info), |
| 86 | GFP_KERNEL); |
| 87 | if (!info) |
| 88 | return -ENOMEM; |
| 89 | |
| 90 | input = devm_input_allocate_device(&pdev->dev); |
| 91 | if (!input) |
| 92 | return -ENOMEM; |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 93 | |
Dmitry Torokhov | 104a5f3 | 2012-03-06 09:10:21 -0800 | [diff] [blame] | 94 | info->idev = input; |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 95 | info->i2c = chip->i2c; |
Haojian Zhuang | 2d95ae3 | 2010-09-08 09:44:35 -0400 | [diff] [blame] | 96 | info->dev = &pdev->dev; |
Dmitry Torokhov | 104a5f3 | 2012-03-06 09:10:21 -0800 | [diff] [blame] | 97 | info->irq[0] = irq[0]; |
| 98 | info->irq[1] = irq[1]; |
| 99 | |
| 100 | input->name = "max8925_on"; |
| 101 | input->phys = "max8925_on/input0"; |
| 102 | input->id.bustype = BUS_I2C; |
| 103 | input->dev.parent = &pdev->dev; |
| 104 | input_set_capability(input, EV_KEY, KEY_POWER); |
| 105 | |
Himangi Saraogi | 04115e4 | 2014-05-29 00:21:56 -0700 | [diff] [blame^] | 106 | error = devm_request_threaded_irq(&pdev->dev, irq[0], NULL, |
| 107 | max8925_onkey_handler, IRQF_ONESHOT, |
| 108 | "onkey-down", info); |
Haojian Zhuang | 2d95ae3 | 2010-09-08 09:44:35 -0400 | [diff] [blame] | 109 | if (error < 0) { |
| 110 | dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", |
| 111 | irq[0], error); |
Himangi Saraogi | 04115e4 | 2014-05-29 00:21:56 -0700 | [diff] [blame^] | 112 | return error; |
Haojian Zhuang | 2d95ae3 | 2010-09-08 09:44:35 -0400 | [diff] [blame] | 113 | } |
Dmitry Torokhov | 104a5f3 | 2012-03-06 09:10:21 -0800 | [diff] [blame] | 114 | |
Himangi Saraogi | 04115e4 | 2014-05-29 00:21:56 -0700 | [diff] [blame^] | 115 | error = devm_request_threaded_irq(&pdev->dev, irq[1], NULL, |
| 116 | max8925_onkey_handler, IRQF_ONESHOT, |
| 117 | "onkey-up", info); |
Haojian Zhuang | 2d95ae3 | 2010-09-08 09:44:35 -0400 | [diff] [blame] | 118 | if (error < 0) { |
| 119 | dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", |
| 120 | irq[1], error); |
Himangi Saraogi | 04115e4 | 2014-05-29 00:21:56 -0700 | [diff] [blame^] | 121 | return error; |
Haojian Zhuang | 2d95ae3 | 2010-09-08 09:44:35 -0400 | [diff] [blame] | 122 | } |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 123 | |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 124 | error = input_register_device(info->idev); |
| 125 | if (error) { |
| 126 | dev_err(chip->dev, "Can't register input device: %d\n", error); |
Himangi Saraogi | 04115e4 | 2014-05-29 00:21:56 -0700 | [diff] [blame^] | 127 | return error; |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | platform_set_drvdata(pdev, info); |
Dmitry Torokhov | 104a5f3 | 2012-03-06 09:10:21 -0800 | [diff] [blame] | 131 | device_init_wakeup(&pdev->dev, 1); |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 132 | |
| 133 | return 0; |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Kevin Liu | adab30d | 2012-03-05 22:24:54 -0800 | [diff] [blame] | 136 | #ifdef CONFIG_PM_SLEEP |
| 137 | static int max8925_onkey_suspend(struct device *dev) |
| 138 | { |
| 139 | struct platform_device *pdev = to_platform_device(dev); |
| 140 | struct max8925_onkey_info *info = platform_get_drvdata(pdev); |
| 141 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); |
| 142 | |
| 143 | if (device_may_wakeup(dev)) { |
Dmitry Torokhov | 104a5f3 | 2012-03-06 09:10:21 -0800 | [diff] [blame] | 144 | chip->wakeup_flag |= 1 << info->irq[0]; |
| 145 | chip->wakeup_flag |= 1 << info->irq[1]; |
Kevin Liu | adab30d | 2012-03-05 22:24:54 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static int max8925_onkey_resume(struct device *dev) |
| 152 | { |
| 153 | struct platform_device *pdev = to_platform_device(dev); |
| 154 | struct max8925_onkey_info *info = platform_get_drvdata(pdev); |
| 155 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); |
| 156 | |
| 157 | if (device_may_wakeup(dev)) { |
Dmitry Torokhov | 104a5f3 | 2012-03-06 09:10:21 -0800 | [diff] [blame] | 158 | chip->wakeup_flag &= ~(1 << info->irq[0]); |
| 159 | chip->wakeup_flag &= ~(1 << info->irq[1]); |
Kevin Liu | adab30d | 2012-03-05 22:24:54 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | #endif |
| 165 | |
| 166 | static SIMPLE_DEV_PM_OPS(max8925_onkey_pm_ops, max8925_onkey_suspend, max8925_onkey_resume); |
| 167 | |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 168 | static struct platform_driver max8925_onkey_driver = { |
| 169 | .driver = { |
| 170 | .name = "max8925-onkey", |
| 171 | .owner = THIS_MODULE, |
Kevin Liu | adab30d | 2012-03-05 22:24:54 -0800 | [diff] [blame] | 172 | .pm = &max8925_onkey_pm_ops, |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 173 | }, |
| 174 | .probe = max8925_onkey_probe, |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 175 | }; |
JJ Ding | 840a746 | 2011-11-29 11:08:40 -0800 | [diff] [blame] | 176 | module_platform_driver(max8925_onkey_driver); |
Haojian Zhuang | 3734574 | 2010-05-22 00:57:26 -0700 | [diff] [blame] | 177 | |
| 178 | MODULE_DESCRIPTION("Maxim MAX8925 ONKEY driver"); |
| 179 | MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>"); |
| 180 | MODULE_LICENSE("GPL"); |