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