blob: f9179b2585a90e5b80f0715da13dbfefdec1cece [file] [log] [blame]
Haojian Zhuang37345742010-05-22 00:57:26 -07001/**
Dmitry Torokhov104a5f32012-03-06 09:10:21 -08002 * MAX8925 ONKEY driver
Haojian Zhuang37345742010-05-22 00:57:26 -07003 *
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>
29
Haojian Zhuang2d95ae32010-09-08 09:44:35 -040030#define SW_INPUT (1 << 7) /* 0/1 -- up/down */
Haojian Zhuang37345742010-05-22 00:57:26 -070031#define HARDRESET_EN (1 << 7)
32#define PWREN_EN (1 << 7)
33
34struct max8925_onkey_info {
35 struct input_dev *idev;
36 struct i2c_client *i2c;
Haojian Zhuang2d95ae32010-09-08 09:44:35 -040037 struct device *dev;
Dmitry Torokhov104a5f32012-03-06 09:10:21 -080038 unsigned int irq[2];
Haojian Zhuang37345742010-05-22 00:57:26 -070039};
40
41/*
Haojian Zhuang2d95ae32010-09-08 09:44:35 -040042 * MAX8925 gives us an interrupt when ONKEY is pressed or released.
Haojian Zhuang37345742010-05-22 00:57:26 -070043 * max8925_set_bits() operates I2C bus and may sleep. So implement
44 * it in thread IRQ handler.
45 */
46static irqreturn_t max8925_onkey_handler(int irq, void *data)
47{
48 struct max8925_onkey_info *info = data;
Dmitry Torokhov104a5f32012-03-06 09:10:21 -080049 int state;
Haojian Zhuang37345742010-05-22 00:57:26 -070050
Dmitry Torokhov104a5f32012-03-06 09:10:21 -080051 state = max8925_reg_read(info->i2c, MAX8925_ON_OFF_STATUS);
52
53 input_report_key(info->idev, KEY_POWER, state & SW_INPUT);
Haojian Zhuang37345742010-05-22 00:57:26 -070054 input_sync(info->idev);
55
Dmitry Torokhov104a5f32012-03-06 09:10:21 -080056 dev_dbg(info->dev, "onkey state:%d\n", state);
Haojian Zhuang2d95ae32010-09-08 09:44:35 -040057
Haojian Zhuang37345742010-05-22 00:57:26 -070058 /* Enable hardreset to halt if system isn't shutdown on time */
59 max8925_set_bits(info->i2c, MAX8925_SYSENSEL,
60 HARDRESET_EN, HARDRESET_EN);
61
62 return IRQ_HANDLED;
63}
64
Bill Pemberton5298cc42012-11-23 21:38:25 -080065static int max8925_onkey_probe(struct platform_device *pdev)
Haojian Zhuang37345742010-05-22 00:57:26 -070066{
67 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
68 struct max8925_onkey_info *info;
Dmitry Torokhov104a5f32012-03-06 09:10:21 -080069 struct input_dev *input;
Haojian Zhuang2d95ae32010-09-08 09:44:35 -040070 int irq[2], error;
71
72 irq[0] = platform_get_irq(pdev, 0);
73 if (irq[0] < 0) {
74 dev_err(&pdev->dev, "No IRQ resource!\n");
75 return -EINVAL;
76 }
Dmitry Torokhov104a5f32012-03-06 09:10:21 -080077
Haojian Zhuang2d95ae32010-09-08 09:44:35 -040078 irq[1] = platform_get_irq(pdev, 1);
79 if (irq[1] < 0) {
80 dev_err(&pdev->dev, "No IRQ resource!\n");
81 return -EINVAL;
82 }
Haojian Zhuang37345742010-05-22 00:57:26 -070083
84 info = kzalloc(sizeof(struct max8925_onkey_info), GFP_KERNEL);
Dmitry Torokhov104a5f32012-03-06 09:10:21 -080085 input = input_allocate_device();
86 if (!info || !input) {
87 error = -ENOMEM;
88 goto err_free_mem;
89 }
Haojian Zhuang37345742010-05-22 00:57:26 -070090
Dmitry Torokhov104a5f32012-03-06 09:10:21 -080091 info->idev = input;
Haojian Zhuang37345742010-05-22 00:57:26 -070092 info->i2c = chip->i2c;
Haojian Zhuang2d95ae32010-09-08 09:44:35 -040093 info->dev = &pdev->dev;
Dmitry Torokhov104a5f32012-03-06 09:10:21 -080094 info->irq[0] = irq[0];
95 info->irq[1] = irq[1];
96
97 input->name = "max8925_on";
98 input->phys = "max8925_on/input0";
99 input->id.bustype = BUS_I2C;
100 input->dev.parent = &pdev->dev;
101 input_set_capability(input, EV_KEY, KEY_POWER);
102
Haojian Zhuang2d95ae32010-09-08 09:44:35 -0400103 error = request_threaded_irq(irq[0], NULL, max8925_onkey_handler,
104 IRQF_ONESHOT, "onkey-down", info);
105 if (error < 0) {
106 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
107 irq[0], error);
Dmitry Torokhov104a5f32012-03-06 09:10:21 -0800108 goto err_free_mem;
Haojian Zhuang2d95ae32010-09-08 09:44:35 -0400109 }
Dmitry Torokhov104a5f32012-03-06 09:10:21 -0800110
Haojian Zhuang2d95ae32010-09-08 09:44:35 -0400111 error = request_threaded_irq(irq[1], NULL, max8925_onkey_handler,
112 IRQF_ONESHOT, "onkey-up", info);
113 if (error < 0) {
114 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
115 irq[1], error);
Dmitry Torokhov104a5f32012-03-06 09:10:21 -0800116 goto err_free_irq0;
Haojian Zhuang2d95ae32010-09-08 09:44:35 -0400117 }
Haojian Zhuang37345742010-05-22 00:57:26 -0700118
Haojian Zhuang37345742010-05-22 00:57:26 -0700119 error = input_register_device(info->idev);
120 if (error) {
121 dev_err(chip->dev, "Can't register input device: %d\n", error);
Dmitry Torokhov104a5f32012-03-06 09:10:21 -0800122 goto err_free_irq1;
Haojian Zhuang37345742010-05-22 00:57:26 -0700123 }
124
125 platform_set_drvdata(pdev, info);
Dmitry Torokhov104a5f32012-03-06 09:10:21 -0800126 device_init_wakeup(&pdev->dev, 1);
Haojian Zhuang37345742010-05-22 00:57:26 -0700127
128 return 0;
129
Dmitry Torokhov104a5f32012-03-06 09:10:21 -0800130err_free_irq1:
131 free_irq(irq[1], info);
132err_free_irq0:
133 free_irq(irq[0], info);
134err_free_mem:
135 input_free_device(input);
Haojian Zhuang37345742010-05-22 00:57:26 -0700136 kfree(info);
Dmitry Torokhov104a5f32012-03-06 09:10:21 -0800137
Haojian Zhuang37345742010-05-22 00:57:26 -0700138 return error;
139}
140
Bill Pembertone2619cf2012-11-23 21:50:47 -0800141static int max8925_onkey_remove(struct platform_device *pdev)
Haojian Zhuang37345742010-05-22 00:57:26 -0700142{
143 struct max8925_onkey_info *info = platform_get_drvdata(pdev);
Dmitry Torokhov104a5f32012-03-06 09:10:21 -0800144 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
Haojian Zhuang37345742010-05-22 00:57:26 -0700145
Dmitry Torokhov104a5f32012-03-06 09:10:21 -0800146 free_irq(info->irq[0] + chip->irq_base, info);
147 free_irq(info->irq[1] + chip->irq_base, info);
Haojian Zhuang37345742010-05-22 00:57:26 -0700148 input_unregister_device(info->idev);
149 kfree(info);
150
151 platform_set_drvdata(pdev, NULL);
152
153 return 0;
154}
155
Kevin Liuadab30d2012-03-05 22:24:54 -0800156#ifdef CONFIG_PM_SLEEP
157static int max8925_onkey_suspend(struct device *dev)
158{
159 struct platform_device *pdev = to_platform_device(dev);
160 struct max8925_onkey_info *info = platform_get_drvdata(pdev);
161 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
162
163 if (device_may_wakeup(dev)) {
Dmitry Torokhov104a5f32012-03-06 09:10:21 -0800164 chip->wakeup_flag |= 1 << info->irq[0];
165 chip->wakeup_flag |= 1 << info->irq[1];
Kevin Liuadab30d2012-03-05 22:24:54 -0800166 }
167
168 return 0;
169}
170
171static int max8925_onkey_resume(struct device *dev)
172{
173 struct platform_device *pdev = to_platform_device(dev);
174 struct max8925_onkey_info *info = platform_get_drvdata(pdev);
175 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
176
177 if (device_may_wakeup(dev)) {
Dmitry Torokhov104a5f32012-03-06 09:10:21 -0800178 chip->wakeup_flag &= ~(1 << info->irq[0]);
179 chip->wakeup_flag &= ~(1 << info->irq[1]);
Kevin Liuadab30d2012-03-05 22:24:54 -0800180 }
181
182 return 0;
183}
184#endif
185
186static SIMPLE_DEV_PM_OPS(max8925_onkey_pm_ops, max8925_onkey_suspend, max8925_onkey_resume);
187
Haojian Zhuang37345742010-05-22 00:57:26 -0700188static struct platform_driver max8925_onkey_driver = {
189 .driver = {
190 .name = "max8925-onkey",
191 .owner = THIS_MODULE,
Kevin Liuadab30d2012-03-05 22:24:54 -0800192 .pm = &max8925_onkey_pm_ops,
Haojian Zhuang37345742010-05-22 00:57:26 -0700193 },
194 .probe = max8925_onkey_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800195 .remove = max8925_onkey_remove,
Haojian Zhuang37345742010-05-22 00:57:26 -0700196};
JJ Ding840a7462011-11-29 11:08:40 -0800197module_platform_driver(max8925_onkey_driver);
Haojian Zhuang37345742010-05-22 00:57:26 -0700198
199MODULE_DESCRIPTION("Maxim MAX8925 ONKEY driver");
200MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
201MODULE_LICENSE("GPL");