]>
Commit | Line | Data |
---|---|---|
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 | 19 | #include <linux/interrupt.h> |
76e2c68f | 20 | #include <linux/pm.h> |
0baf81ba KK |
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 | * MAX7359 registers | |
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 | * Configuration register bits | |
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 | * Autosleep register values (ms) | |
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 | ||
59 | struct max7359_keypad { | |
60 | /* matrix key code map */ | |
61 | unsigned short keycodes[MAX7359_MAX_KEY_NUM]; | |
62 | ||
0baf81ba KK |
63 | struct input_dev *input_dev; |
64 | struct i2c_client *client; | |
0baf81ba KK |
65 | }; |
66 | ||
67 | static 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 | ||
77 | static 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 | ||
44ca397b DT |
87 | /* runs in an IRQ thread -- can (and will!) sleep */ |
88 | static irqreturn_t max7359_interrupt(int irq, void *dev_id) | |
0baf81ba | 89 | { |
44ca397b | 90 | struct max7359_keypad *keypad = dev_id; |
0baf81ba KK |
91 | struct input_dev *input_dev = keypad->input_dev; |
92 | int val, row, col, release, code; | |
93 | ||
94 | val = max7359_read_reg(keypad->client, MAX7359_REG_KEYFIFO); | |
95 | row = val & 0x7; | |
96 | col = (val >> 3) & 0x7; | |
97 | release = val & 0x40; | |
98 | ||
99 | code = MATRIX_SCAN_CODE(row, col, MAX7359_ROW_SHIFT); | |
100 | ||
44ca397b DT |
101 | dev_dbg(&keypad->client->dev, |
102 | "key[%d:%d] %s\n", row, col, release ? "release" : "press"); | |
103 | ||
0baf81ba KK |
104 | input_event(input_dev, EV_MSC, MSC_SCAN, code); |
105 | input_report_key(input_dev, keypad->keycodes[code], !release); | |
106 | input_sync(input_dev); | |
107 | ||
0baf81ba KK |
108 | return IRQ_HANDLED; |
109 | } | |
110 | ||
111 | /* | |
112 | * Let MAX7359 fall into a deep sleep: | |
113 | * If no keys are pressed, enter sleep mode for 8192 ms. And if any | |
114 | * key is pressed, the MAX7359 returns to normal operating mode. | |
115 | */ | |
116 | static inline void max7359_fall_deepsleep(struct i2c_client *client) | |
117 | { | |
118 | max7359_write_reg(client, MAX7359_REG_SLEEP, MAX7359_AUTOSLEEP_8192); | |
119 | } | |
120 | ||
121 | /* | |
122 | * Let MAX7359 take a catnap: | |
123 | * Autosleep just for 256 ms. | |
124 | */ | |
125 | static inline void max7359_take_catnap(struct i2c_client *client) | |
126 | { | |
127 | max7359_write_reg(client, MAX7359_REG_SLEEP, MAX7359_AUTOSLEEP_256); | |
128 | } | |
129 | ||
130 | static int max7359_open(struct input_dev *dev) | |
131 | { | |
132 | struct max7359_keypad *keypad = input_get_drvdata(dev); | |
133 | ||
134 | max7359_take_catnap(keypad->client); | |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
139 | static void max7359_close(struct input_dev *dev) | |
140 | { | |
141 | struct max7359_keypad *keypad = input_get_drvdata(dev); | |
142 | ||
143 | max7359_fall_deepsleep(keypad->client); | |
144 | } | |
145 | ||
146 | static void max7359_initialize(struct i2c_client *client) | |
147 | { | |
148 | max7359_write_reg(client, MAX7359_REG_CONFIG, | |
0baf81ba KK |
149 | MAX7359_CFG_KEY_RELEASE | /* Key release enable */ |
150 | MAX7359_CFG_WAKEUP); /* Key press wakeup enable */ | |
151 | ||
152 | /* Full key-scan functionality */ | |
153 | max7359_write_reg(client, MAX7359_REG_DEBOUNCE, 0x1F); | |
154 | ||
155 | /* nINT asserts every debounce cycles */ | |
156 | max7359_write_reg(client, MAX7359_REG_INTERRUPT, 0x01); | |
157 | ||
158 | max7359_fall_deepsleep(client); | |
159 | } | |
160 | ||
5298cc4c | 161 | static int max7359_probe(struct i2c_client *client, |
0baf81ba KK |
162 | const struct i2c_device_id *id) |
163 | { | |
c838cb3d JH |
164 | const struct matrix_keymap_data *keymap_data = |
165 | dev_get_platdata(&client->dev); | |
0baf81ba KK |
166 | struct max7359_keypad *keypad; |
167 | struct input_dev *input_dev; | |
168 | int ret; | |
169 | int error; | |
170 | ||
171 | if (!client->irq) { | |
172 | dev_err(&client->dev, "The irq number should not be zero\n"); | |
173 | return -EINVAL; | |
174 | } | |
175 | ||
176 | /* Detect MAX7359: The initial Keys FIFO value is '0x3F' */ | |
177 | ret = max7359_read_reg(client, MAX7359_REG_KEYFIFO); | |
178 | if (ret < 0) { | |
179 | dev_err(&client->dev, "failed to detect device\n"); | |
180 | return -ENODEV; | |
181 | } | |
182 | ||
183 | dev_dbg(&client->dev, "keys FIFO is 0x%02x\n", ret); | |
184 | ||
997502f8 HS |
185 | keypad = devm_kzalloc(&client->dev, sizeof(struct max7359_keypad), |
186 | GFP_KERNEL); | |
187 | if (!keypad) { | |
0baf81ba | 188 | dev_err(&client->dev, "failed to allocate memory\n"); |
997502f8 HS |
189 | return -ENOMEM; |
190 | } | |
191 | ||
192 | input_dev = devm_input_allocate_device(&client->dev); | |
193 | if (!input_dev) { | |
194 | dev_err(&client->dev, "failed to allocate input device\n"); | |
195 | return -ENOMEM; | |
0baf81ba KK |
196 | } |
197 | ||
198 | keypad->client = client; | |
199 | keypad->input_dev = input_dev; | |
0baf81ba KK |
200 | |
201 | input_dev->name = client->name; | |
202 | input_dev->id.bustype = BUS_I2C; | |
203 | input_dev->open = max7359_open; | |
204 | input_dev->close = max7359_close; | |
205 | input_dev->dev.parent = &client->dev; | |
206 | ||
207 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP); | |
208 | input_dev->keycodesize = sizeof(keypad->keycodes[0]); | |
209 | input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes); | |
210 | input_dev->keycode = keypad->keycodes; | |
211 | ||
212 | input_set_capability(input_dev, EV_MSC, MSC_SCAN); | |
213 | input_set_drvdata(input_dev, keypad); | |
214 | ||
68aeee98 ED |
215 | error = matrix_keypad_build_keymap(keymap_data, NULL, |
216 | MAX7359_MAX_KEY_ROWS, | |
217 | MAX7359_MAX_KEY_COLS, | |
218 | keypad->keycodes, | |
219 | input_dev); | |
220 | if (error) { | |
221 | dev_err(&client->dev, "failed to build keymap\n"); | |
222 | return error; | |
223 | } | |
0baf81ba | 224 | |
997502f8 HS |
225 | error = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
226 | max7359_interrupt, | |
227 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, | |
228 | client->name, keypad); | |
0baf81ba KK |
229 | if (error) { |
230 | dev_err(&client->dev, "failed to register interrupt\n"); | |
997502f8 | 231 | return error; |
0baf81ba KK |
232 | } |
233 | ||
234 | /* Register the input device */ | |
235 | error = input_register_device(input_dev); | |
236 | if (error) { | |
237 | dev_err(&client->dev, "failed to register input device\n"); | |
997502f8 | 238 | return error; |
0baf81ba KK |
239 | } |
240 | ||
241 | /* Initialize MAX7359 */ | |
242 | max7359_initialize(client); | |
243 | ||
0baf81ba KK |
244 | device_init_wakeup(&client->dev, 1); |
245 | ||
0baf81ba KK |
246 | return 0; |
247 | } | |
248 | ||
1bc75457 | 249 | #ifdef CONFIG_PM_SLEEP |
76e2c68f | 250 | static int max7359_suspend(struct device *dev) |
0baf81ba | 251 | { |
76e2c68f MB |
252 | struct i2c_client *client = to_i2c_client(dev); |
253 | ||
0baf81ba KK |
254 | max7359_fall_deepsleep(client); |
255 | ||
256 | if (device_may_wakeup(&client->dev)) | |
44ca397b | 257 | enable_irq_wake(client->irq); |
0baf81ba KK |
258 | |
259 | return 0; | |
260 | } | |
261 | ||
76e2c68f | 262 | static int max7359_resume(struct device *dev) |
0baf81ba | 263 | { |
76e2c68f MB |
264 | struct i2c_client *client = to_i2c_client(dev); |
265 | ||
0baf81ba | 266 | if (device_may_wakeup(&client->dev)) |
44ca397b | 267 | disable_irq_wake(client->irq); |
0baf81ba KK |
268 | |
269 | /* Restore the default setting */ | |
270 | max7359_take_catnap(client); | |
271 | ||
272 | return 0; | |
273 | } | |
0baf81ba KK |
274 | #endif |
275 | ||
76e2c68f MB |
276 | static SIMPLE_DEV_PM_OPS(max7359_pm, max7359_suspend, max7359_resume); |
277 | ||
0baf81ba KK |
278 | static const struct i2c_device_id max7359_ids[] = { |
279 | { "max7359", 0 }, | |
280 | { } | |
281 | }; | |
282 | MODULE_DEVICE_TABLE(i2c, max7359_ids); | |
283 | ||
284 | static struct i2c_driver max7359_i2c_driver = { | |
285 | .driver = { | |
286 | .name = "max7359", | |
76e2c68f | 287 | .pm = &max7359_pm, |
0baf81ba KK |
288 | }, |
289 | .probe = max7359_probe, | |
0baf81ba KK |
290 | .id_table = max7359_ids, |
291 | }; | |
292 | ||
1b92c1cf | 293 | module_i2c_driver(max7359_i2c_driver); |
0baf81ba KK |
294 | |
295 | MODULE_AUTHOR("Kim Kyuwon <q1.kim@samsung.com>"); | |
296 | MODULE_DESCRIPTION("MAX7359 Key Switch Controller Driver"); | |
297 | MODULE_LICENSE("GPL v2"); |