Loading...
1/**
2 * 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
30#define SW_INPUT (1 << 7) /* 0/1 -- up/down */
31#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;
37 struct device *dev;
38 unsigned int irq[2];
39};
40
41/*
42 * MAX8925 gives us an interrupt when ONKEY is pressed or released.
43 * 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;
49 int state;
50
51 state = max8925_reg_read(info->i2c, MAX8925_ON_OFF_STATUS);
52
53 input_report_key(info->idev, KEY_POWER, state & SW_INPUT);
54 input_sync(info->idev);
55
56 dev_dbg(info->dev, "onkey state:%d\n", state);
57
58 /* 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
65static int max8925_onkey_probe(struct platform_device *pdev)
66{
67 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
68 struct max8925_onkey_info *info;
69 struct input_dev *input;
70 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 }
77
78 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 }
83
84 info = kzalloc(sizeof(struct max8925_onkey_info), GFP_KERNEL);
85 input = input_allocate_device();
86 if (!info || !input) {
87 error = -ENOMEM;
88 goto err_free_mem;
89 }
90
91 info->idev = input;
92 info->i2c = chip->i2c;
93 info->dev = &pdev->dev;
94 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
103 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);
108 goto err_free_mem;
109 }
110
111 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);
116 goto err_free_irq0;
117 }
118
119 error = input_register_device(info->idev);
120 if (error) {
121 dev_err(chip->dev, "Can't register input device: %d\n", error);
122 goto err_free_irq1;
123 }
124
125 platform_set_drvdata(pdev, info);
126 device_init_wakeup(&pdev->dev, 1);
127
128 return 0;
129
130err_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);
136 kfree(info);
137
138 return error;
139}
140
141static int max8925_onkey_remove(struct platform_device *pdev)
142{
143 struct max8925_onkey_info *info = platform_get_drvdata(pdev);
144 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
145
146 free_irq(info->irq[0] + chip->irq_base, info);
147 free_irq(info->irq[1] + chip->irq_base, info);
148 input_unregister_device(info->idev);
149 kfree(info);
150
151 return 0;
152}
153
154#ifdef CONFIG_PM_SLEEP
155static int max8925_onkey_suspend(struct device *dev)
156{
157 struct platform_device *pdev = to_platform_device(dev);
158 struct max8925_onkey_info *info = platform_get_drvdata(pdev);
159 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
160
161 if (device_may_wakeup(dev)) {
162 chip->wakeup_flag |= 1 << info->irq[0];
163 chip->wakeup_flag |= 1 << info->irq[1];
164 }
165
166 return 0;
167}
168
169static int max8925_onkey_resume(struct device *dev)
170{
171 struct platform_device *pdev = to_platform_device(dev);
172 struct max8925_onkey_info *info = platform_get_drvdata(pdev);
173 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
174
175 if (device_may_wakeup(dev)) {
176 chip->wakeup_flag &= ~(1 << info->irq[0]);
177 chip->wakeup_flag &= ~(1 << info->irq[1]);
178 }
179
180 return 0;
181}
182#endif
183
184static SIMPLE_DEV_PM_OPS(max8925_onkey_pm_ops, max8925_onkey_suspend, max8925_onkey_resume);
185
186static struct platform_driver max8925_onkey_driver = {
187 .driver = {
188 .name = "max8925-onkey",
189 .owner = THIS_MODULE,
190 .pm = &max8925_onkey_pm_ops,
191 },
192 .probe = max8925_onkey_probe,
193 .remove = max8925_onkey_remove,
194};
195module_platform_driver(max8925_onkey_driver);
196
197MODULE_DESCRIPTION("Maxim MAX8925 ONKEY driver");
198MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
199MODULE_LICENSE("GPL");
1/*
2 * 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#include <linux/device.h>
30
31#define SW_INPUT (1 << 7) /* 0/1 -- up/down */
32#define HARDRESET_EN (1 << 7)
33#define PWREN_EN (1 << 7)
34
35struct max8925_onkey_info {
36 struct input_dev *idev;
37 struct i2c_client *i2c;
38 struct device *dev;
39 unsigned int irq[2];
40};
41
42/*
43 * MAX8925 gives us an interrupt when ONKEY is pressed or released.
44 * max8925_set_bits() operates I2C bus and may sleep. So implement
45 * it in thread IRQ handler.
46 */
47static irqreturn_t max8925_onkey_handler(int irq, void *data)
48{
49 struct max8925_onkey_info *info = data;
50 int state;
51
52 state = max8925_reg_read(info->i2c, MAX8925_ON_OFF_STATUS);
53
54 input_report_key(info->idev, KEY_POWER, state & SW_INPUT);
55 input_sync(info->idev);
56
57 dev_dbg(info->dev, "onkey state:%d\n", state);
58
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
66static int max8925_onkey_probe(struct platform_device *pdev)
67{
68 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
69 struct max8925_onkey_info *info;
70 struct input_dev *input;
71 int irq[2], error;
72
73 irq[0] = platform_get_irq(pdev, 0);
74 if (irq[0] < 0)
75 return -EINVAL;
76
77 irq[1] = platform_get_irq(pdev, 1);
78 if (irq[1] < 0)
79 return -EINVAL;
80
81 info = devm_kzalloc(&pdev->dev, sizeof(struct max8925_onkey_info),
82 GFP_KERNEL);
83 if (!info)
84 return -ENOMEM;
85
86 input = devm_input_allocate_device(&pdev->dev);
87 if (!input)
88 return -ENOMEM;
89
90 info->idev = input;
91 info->i2c = chip->i2c;
92 info->dev = &pdev->dev;
93 info->irq[0] = irq[0];
94 info->irq[1] = irq[1];
95
96 input->name = "max8925_on";
97 input->phys = "max8925_on/input0";
98 input->id.bustype = BUS_I2C;
99 input->dev.parent = &pdev->dev;
100 input_set_capability(input, EV_KEY, KEY_POWER);
101
102 error = devm_request_threaded_irq(&pdev->dev, irq[0], NULL,
103 max8925_onkey_handler, IRQF_ONESHOT,
104 "onkey-down", info);
105 if (error < 0) {
106 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
107 irq[0], error);
108 return error;
109 }
110
111 error = devm_request_threaded_irq(&pdev->dev, irq[1], NULL,
112 max8925_onkey_handler, IRQF_ONESHOT,
113 "onkey-up", info);
114 if (error < 0) {
115 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
116 irq[1], error);
117 return error;
118 }
119
120 error = input_register_device(info->idev);
121 if (error) {
122 dev_err(chip->dev, "Can't register input device: %d\n", error);
123 return error;
124 }
125
126 platform_set_drvdata(pdev, info);
127 device_init_wakeup(&pdev->dev, 1);
128
129 return 0;
130}
131
132static int max8925_onkey_suspend(struct device *dev)
133{
134 struct platform_device *pdev = to_platform_device(dev);
135 struct max8925_onkey_info *info = platform_get_drvdata(pdev);
136 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
137
138 if (device_may_wakeup(dev)) {
139 chip->wakeup_flag |= 1 << info->irq[0];
140 chip->wakeup_flag |= 1 << info->irq[1];
141 }
142
143 return 0;
144}
145
146static int max8925_onkey_resume(struct device *dev)
147{
148 struct platform_device *pdev = to_platform_device(dev);
149 struct max8925_onkey_info *info = platform_get_drvdata(pdev);
150 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
151
152 if (device_may_wakeup(dev)) {
153 chip->wakeup_flag &= ~(1 << info->irq[0]);
154 chip->wakeup_flag &= ~(1 << info->irq[1]);
155 }
156
157 return 0;
158}
159
160static DEFINE_SIMPLE_DEV_PM_OPS(max8925_onkey_pm_ops,
161 max8925_onkey_suspend, max8925_onkey_resume);
162
163static struct platform_driver max8925_onkey_driver = {
164 .driver = {
165 .name = "max8925-onkey",
166 .pm = pm_sleep_ptr(&max8925_onkey_pm_ops),
167 },
168 .probe = max8925_onkey_probe,
169};
170module_platform_driver(max8925_onkey_driver);
171
172MODULE_DESCRIPTION("Maxim MAX8925 ONKEY driver");
173MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
174MODULE_LICENSE("GPL");