blob: 7de0ded4ccc382ed3b90b16c302176be980eb59b [file] [log] [blame]
Haojian Zhuang37345742010-05-22 00:57:26 -07001/**
2 * max8925_onkey.c - MAX8925 ONKEY driver
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>
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;
38 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;
Haojian Zhuang2d95ae32010-09-08 09:44:35 -040049 int ret, event;
Haojian Zhuang37345742010-05-22 00:57:26 -070050
Haojian Zhuang2d95ae32010-09-08 09:44:35 -040051 ret = max8925_reg_read(info->i2c, MAX8925_ON_OFF_STATUS);
52 if (ret & SW_INPUT)
53 event = 1;
54 else
55 event = 0;
56 input_report_key(info->idev, KEY_POWER, event);
Haojian Zhuang37345742010-05-22 00:57:26 -070057 input_sync(info->idev);
58
Haojian Zhuang2d95ae32010-09-08 09:44:35 -040059 dev_dbg(info->dev, "onkey event:%d\n", event);
60
Haojian Zhuang37345742010-05-22 00:57:26 -070061 /* Enable hardreset to halt if system isn't shutdown on time */
62 max8925_set_bits(info->i2c, MAX8925_SYSENSEL,
63 HARDRESET_EN, HARDRESET_EN);
64
65 return IRQ_HANDLED;
66}
67
68static int __devinit max8925_onkey_probe(struct platform_device *pdev)
69{
70 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
71 struct max8925_onkey_info *info;
Haojian Zhuang2d95ae32010-09-08 09:44:35 -040072 int irq[2], error;
73
74 irq[0] = platform_get_irq(pdev, 0);
75 if (irq[0] < 0) {
76 dev_err(&pdev->dev, "No IRQ resource!\n");
77 return -EINVAL;
78 }
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 Zhuang37345742010-05-22 00:57:26 -070084
85 info = kzalloc(sizeof(struct max8925_onkey_info), GFP_KERNEL);
86 if (!info)
87 return -ENOMEM;
88
89 info->i2c = chip->i2c;
Haojian Zhuang2d95ae32010-09-08 09:44:35 -040090 info->dev = &pdev->dev;
91 irq[0] += chip->irq_base;
92 irq[1] += chip->irq_base;
93
94 error = request_threaded_irq(irq[0], NULL, max8925_onkey_handler,
95 IRQF_ONESHOT, "onkey-down", info);
96 if (error < 0) {
97 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
98 irq[0], error);
99 goto out;
100 }
101 error = request_threaded_irq(irq[1], NULL, max8925_onkey_handler,
102 IRQF_ONESHOT, "onkey-up", info);
103 if (error < 0) {
104 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
105 irq[1], error);
106 goto out_irq;
107 }
Haojian Zhuang37345742010-05-22 00:57:26 -0700108
109 info->idev = input_allocate_device();
110 if (!info->idev) {
111 dev_err(chip->dev, "Failed to allocate input dev\n");
112 error = -ENOMEM;
113 goto out_input;
114 }
115
116 info->idev->name = "max8925_on";
117 info->idev->phys = "max8925_on/input0";
118 info->idev->id.bustype = BUS_I2C;
119 info->idev->dev.parent = &pdev->dev;
Haojian Zhuang2d95ae32010-09-08 09:44:35 -0400120 info->irq[0] = irq[0];
121 info->irq[1] = irq[1];
Haojian Zhuang37345742010-05-22 00:57:26 -0700122 info->idev->evbit[0] = BIT_MASK(EV_KEY);
123 info->idev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
124
Haojian Zhuang37345742010-05-22 00:57:26 -0700125
126 error = input_register_device(info->idev);
127 if (error) {
128 dev_err(chip->dev, "Can't register input device: %d\n", error);
Haojian Zhuang2d95ae32010-09-08 09:44:35 -0400129 goto out_reg;
Haojian Zhuang37345742010-05-22 00:57:26 -0700130 }
131
132 platform_set_drvdata(pdev, info);
133
134 return 0;
135
Haojian Zhuang2d95ae32010-09-08 09:44:35 -0400136out_reg:
Haojian Zhuang37345742010-05-22 00:57:26 -0700137 input_free_device(info->idev);
138out_input:
Haojian Zhuang2d95ae32010-09-08 09:44:35 -0400139 free_irq(info->irq[1], info);
140out_irq:
141 free_irq(info->irq[0], info);
142out:
Haojian Zhuang37345742010-05-22 00:57:26 -0700143 kfree(info);
144 return error;
145}
146
147static int __devexit max8925_onkey_remove(struct platform_device *pdev)
148{
149 struct max8925_onkey_info *info = platform_get_drvdata(pdev);
150
Haojian Zhuang2d95ae32010-09-08 09:44:35 -0400151 free_irq(info->irq[0], info);
152 free_irq(info->irq[1], info);
Haojian Zhuang37345742010-05-22 00:57:26 -0700153 input_unregister_device(info->idev);
154 kfree(info);
155
156 platform_set_drvdata(pdev, NULL);
157
158 return 0;
159}
160
161static struct platform_driver max8925_onkey_driver = {
162 .driver = {
163 .name = "max8925-onkey",
164 .owner = THIS_MODULE,
165 },
166 .probe = max8925_onkey_probe,
167 .remove = __devexit_p(max8925_onkey_remove),
168};
169
170static int __init max8925_onkey_init(void)
171{
172 return platform_driver_register(&max8925_onkey_driver);
173}
174module_init(max8925_onkey_init);
175
176static void __exit max8925_onkey_exit(void)
177{
178 platform_driver_unregister(&max8925_onkey_driver);
179}
180module_exit(max8925_onkey_exit);
181
182MODULE_DESCRIPTION("Maxim MAX8925 ONKEY driver");
183MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
184MODULE_LICENSE("GPL");