1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/module.h>
17#include <linux/i2c.h>
18#include <linux/slab.h>
19#include <linux/interrupt.h>
20#include <linux/pm.h>
21#include <linux/input.h>
22#include <linux/input/matrix_keypad.h>
23
24#define MAX7359_MAX_KEY_ROWS 8
25#define MAX7359_MAX_KEY_COLS 8
26#define MAX7359_MAX_KEY_NUM (MAX7359_MAX_KEY_ROWS * MAX7359_MAX_KEY_COLS)
27#define MAX7359_ROW_SHIFT 3
28
29
30
31
32#define MAX7359_REG_KEYFIFO 0x00
33#define MAX7359_REG_CONFIG 0x01
34#define MAX7359_REG_DEBOUNCE 0x02
35#define MAX7359_REG_INTERRUPT 0x03
36#define MAX7359_REG_PORTS 0x04
37#define MAX7359_REG_KEYREP 0x05
38#define MAX7359_REG_SLEEP 0x06
39
40
41
42
43#define MAX7359_CFG_SLEEP (1 << 7)
44#define MAX7359_CFG_INTERRUPT (1 << 5)
45#define MAX7359_CFG_KEY_RELEASE (1 << 3)
46#define MAX7359_CFG_WAKEUP (1 << 1)
47#define MAX7359_CFG_TIMEOUT (1 << 0)
48
49
50
51
52#define MAX7359_AUTOSLEEP_8192 0x01
53#define MAX7359_AUTOSLEEP_4096 0x02
54#define MAX7359_AUTOSLEEP_2048 0x03
55#define MAX7359_AUTOSLEEP_1024 0x04
56#define MAX7359_AUTOSLEEP_512 0x05
57#define MAX7359_AUTOSLEEP_256 0x06
58
59struct max7359_keypad {
60
61 unsigned short keycodes[MAX7359_MAX_KEY_NUM];
62
63 struct input_dev *input_dev;
64 struct i2c_client *client;
65};
66
67static int max7359_write_reg(struct i2c_client *client, u8 reg, u8 val)
68{
69 int ret = i2c_smbus_write_byte_data(client, reg, val);
70
71 if (ret < 0)
72 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
73 __func__, reg, val, ret);
74 return ret;
75}
76
77static int max7359_read_reg(struct i2c_client *client, int reg)
78{
79 int ret = i2c_smbus_read_byte_data(client, reg);
80
81 if (ret < 0)
82 dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
83 __func__, reg, ret);
84 return ret;
85}
86
87static void max7359_build_keycode(struct max7359_keypad *keypad,
88 const struct matrix_keymap_data *keymap_data)
89{
90 struct input_dev *input_dev = keypad->input_dev;
91 int i;
92
93 for (i = 0; i < keymap_data->keymap_size; i++) {
94 unsigned int key = keymap_data->keymap[i];
95 unsigned int row = KEY_ROW(key);
96 unsigned int col = KEY_COL(key);
97 unsigned int scancode = MATRIX_SCAN_CODE(row, col,
98 MAX7359_ROW_SHIFT);
99 unsigned short keycode = KEY_VAL(key);
100
101 keypad->keycodes[scancode] = keycode;
102 __set_bit(keycode, input_dev->keybit);
103 }
104 __clear_bit(KEY_RESERVED, input_dev->keybit);
105}
106
107
108static irqreturn_t max7359_interrupt(int irq, void *dev_id)
109{
110 struct max7359_keypad *keypad = dev_id;
111 struct input_dev *input_dev = keypad->input_dev;
112 int val, row, col, release, code;
113
114 val = max7359_read_reg(keypad->client, MAX7359_REG_KEYFIFO);
115 row = val & 0x7;
116 col = (val >> 3) & 0x7;
117 release = val & 0x40;
118
119 code = MATRIX_SCAN_CODE(row, col, MAX7359_ROW_SHIFT);
120
121 dev_dbg(&keypad->client->dev,
122 "key[%d:%d] %s\n", row, col, release ? "release" : "press");
123
124 input_event(input_dev, EV_MSC, MSC_SCAN, code);
125 input_report_key(input_dev, keypad->keycodes[code], !release);
126 input_sync(input_dev);
127
128 return IRQ_HANDLED;
129}
130
131
132
133
134
135
136static inline void max7359_fall_deepsleep(struct i2c_client *client)
137{
138 max7359_write_reg(client, MAX7359_REG_SLEEP, MAX7359_AUTOSLEEP_8192);
139}
140
141
142
143
144
145static inline void max7359_take_catnap(struct i2c_client *client)
146{
147 max7359_write_reg(client, MAX7359_REG_SLEEP, MAX7359_AUTOSLEEP_256);
148}
149
150static int max7359_open(struct input_dev *dev)
151{
152 struct max7359_keypad *keypad = input_get_drvdata(dev);
153
154 max7359_take_catnap(keypad->client);
155
156 return 0;
157}
158
159static void max7359_close(struct input_dev *dev)
160{
161 struct max7359_keypad *keypad = input_get_drvdata(dev);
162
163 max7359_fall_deepsleep(keypad->client);
164}
165
166static void max7359_initialize(struct i2c_client *client)
167{
168 max7359_write_reg(client, MAX7359_REG_CONFIG,
169 MAX7359_CFG_INTERRUPT |
170 MAX7359_CFG_KEY_RELEASE |
171 MAX7359_CFG_WAKEUP);
172
173
174 max7359_write_reg(client, MAX7359_REG_DEBOUNCE, 0x1F);
175
176
177 max7359_write_reg(client, MAX7359_REG_INTERRUPT, 0x01);
178
179 max7359_fall_deepsleep(client);
180}
181
182static int __devinit max7359_probe(struct i2c_client *client,
183 const struct i2c_device_id *id)
184{
185 const struct matrix_keymap_data *keymap_data = client->dev.platform_data;
186 struct max7359_keypad *keypad;
187 struct input_dev *input_dev;
188 int ret;
189 int error;
190
191 if (!client->irq) {
192 dev_err(&client->dev, "The irq number should not be zero\n");
193 return -EINVAL;
194 }
195
196
197 ret = max7359_read_reg(client, MAX7359_REG_KEYFIFO);
198 if (ret < 0) {
199 dev_err(&client->dev, "failed to detect device\n");
200 return -ENODEV;
201 }
202
203 dev_dbg(&client->dev, "keys FIFO is 0x%02x\n", ret);
204
205 keypad = kzalloc(sizeof(struct max7359_keypad), GFP_KERNEL);
206 input_dev = input_allocate_device();
207 if (!keypad || !input_dev) {
208 dev_err(&client->dev, "failed to allocate memory\n");
209 error = -ENOMEM;
210 goto failed_free_mem;
211 }
212
213 keypad->client = client;
214 keypad->input_dev = input_dev;
215
216 input_dev->name = client->name;
217 input_dev->id.bustype = BUS_I2C;
218 input_dev->open = max7359_open;
219 input_dev->close = max7359_close;
220 input_dev->dev.parent = &client->dev;
221
222 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
223 input_dev->keycodesize = sizeof(keypad->keycodes[0]);
224 input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
225 input_dev->keycode = keypad->keycodes;
226
227 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
228 input_set_drvdata(input_dev, keypad);
229
230 max7359_build_keycode(keypad, keymap_data);
231
232 error = request_threaded_irq(client->irq, NULL, max7359_interrupt,
233 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
234 client->name, keypad);
235 if (error) {
236 dev_err(&client->dev, "failed to register interrupt\n");
237 goto failed_free_mem;
238 }
239
240
241 error = input_register_device(input_dev);
242 if (error) {
243 dev_err(&client->dev, "failed to register input device\n");
244 goto failed_free_irq;
245 }
246
247
248 max7359_initialize(client);
249
250 i2c_set_clientdata(client, keypad);
251 device_init_wakeup(&client->dev, 1);
252
253 return 0;
254
255failed_free_irq:
256 free_irq(client->irq, keypad);
257failed_free_mem:
258 input_free_device(input_dev);
259 kfree(keypad);
260 return error;
261}
262
263static int __devexit max7359_remove(struct i2c_client *client)
264{
265 struct max7359_keypad *keypad = i2c_get_clientdata(client);
266
267 free_irq(client->irq, keypad);
268 input_unregister_device(keypad->input_dev);
269 kfree(keypad);
270
271 return 0;
272}
273
274#ifdef CONFIG_PM
275static int max7359_suspend(struct device *dev)
276{
277 struct i2c_client *client = to_i2c_client(dev);
278
279 max7359_fall_deepsleep(client);
280
281 if (device_may_wakeup(&client->dev))
282 enable_irq_wake(client->irq);
283
284 return 0;
285}
286
287static int max7359_resume(struct device *dev)
288{
289 struct i2c_client *client = to_i2c_client(dev);
290
291 if (device_may_wakeup(&client->dev))
292 disable_irq_wake(client->irq);
293
294
295 max7359_take_catnap(client);
296
297 return 0;
298}
299#endif
300
301static SIMPLE_DEV_PM_OPS(max7359_pm, max7359_suspend, max7359_resume);
302
303static const struct i2c_device_id max7359_ids[] = {
304 { "max7359", 0 },
305 { }
306};
307MODULE_DEVICE_TABLE(i2c, max7359_ids);
308
309static struct i2c_driver max7359_i2c_driver = {
310 .driver = {
311 .name = "max7359",
312 .pm = &max7359_pm,
313 },
314 .probe = max7359_probe,
315 .remove = __devexit_p(max7359_remove),
316 .id_table = max7359_ids,
317};
318
319static int __init max7359_init(void)
320{
321 return i2c_add_driver(&max7359_i2c_driver);
322}
323module_init(max7359_init);
324
325static void __exit max7359_exit(void)
326{
327 i2c_del_driver(&max7359_i2c_driver);
328}
329module_exit(max7359_exit);
330
331MODULE_AUTHOR("Kim Kyuwon <q1.kim@samsung.com>");
332MODULE_DESCRIPTION("MAX7359 Key Switch Controller Driver");
333MODULE_LICENSE("GPL v2");
334