blob: 4e319eb9e19d9f75a67d7c2123cc3bf5550c86ad [file] [log] [blame]
Joonyoung Shim0fffed22010-07-21 00:45:10 -07001/*
2 * Samsung keypad driver
3 *
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
6 * Author: Donghwa Lee <dh09.lee@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/err.h>
Joonyoung Shim0fffed22010-07-21 00:45:10 -070017#include <linux/input.h>
18#include <linux/interrupt.h>
19#include <linux/io.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
Mark Brown48c98b12011-12-29 09:58:16 -080022#include <linux/pm.h>
23#include <linux/pm_runtime.h>
Joonyoung Shim0fffed22010-07-21 00:45:10 -070024#include <linux/slab.h>
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +090025#include <linux/of.h>
Joonyoung Shim0fffed22010-07-21 00:45:10 -070026#include <linux/sched.h>
Dmitry Torokhov8d964a22011-11-07 23:59:41 -080027#include <linux/input/samsung-keypad.h>
Joonyoung Shim0fffed22010-07-21 00:45:10 -070028
29#define SAMSUNG_KEYIFCON 0x00
30#define SAMSUNG_KEYIFSTSCLR 0x04
31#define SAMSUNG_KEYIFCOL 0x08
32#define SAMSUNG_KEYIFROW 0x0c
33#define SAMSUNG_KEYIFFC 0x10
34
35/* SAMSUNG_KEYIFCON */
36#define SAMSUNG_KEYIFCON_INT_F_EN (1 << 0)
37#define SAMSUNG_KEYIFCON_INT_R_EN (1 << 1)
38#define SAMSUNG_KEYIFCON_DF_EN (1 << 2)
39#define SAMSUNG_KEYIFCON_FC_EN (1 << 3)
40#define SAMSUNG_KEYIFCON_WAKEUPEN (1 << 4)
41
42/* SAMSUNG_KEYIFSTSCLR */
43#define SAMSUNG_KEYIFSTSCLR_P_INT_MASK (0xff << 0)
44#define SAMSUNG_KEYIFSTSCLR_R_INT_MASK (0xff << 8)
45#define SAMSUNG_KEYIFSTSCLR_R_INT_OFFSET 8
46#define S5PV210_KEYIFSTSCLR_P_INT_MASK (0x3fff << 0)
47#define S5PV210_KEYIFSTSCLR_R_INT_MASK (0x3fff << 16)
48#define S5PV210_KEYIFSTSCLR_R_INT_OFFSET 16
49
50/* SAMSUNG_KEYIFCOL */
51#define SAMSUNG_KEYIFCOL_MASK (0xff << 0)
52#define S5PV210_KEYIFCOLEN_MASK (0xff << 8)
53
54/* SAMSUNG_KEYIFROW */
55#define SAMSUNG_KEYIFROW_MASK (0xff << 0)
56#define S5PV210_KEYIFROW_MASK (0x3fff << 0)
57
58/* SAMSUNG_KEYIFFC */
59#define SAMSUNG_KEYIFFC_MASK (0x3ff << 0)
60
61enum samsung_keypad_type {
62 KEYPAD_TYPE_SAMSUNG,
63 KEYPAD_TYPE_S5PV210,
64};
65
66struct samsung_keypad {
67 struct input_dev *input_dev;
Mark Brown48c98b12011-12-29 09:58:16 -080068 struct platform_device *pdev;
Joonyoung Shim0fffed22010-07-21 00:45:10 -070069 struct clk *clk;
70 void __iomem *base;
71 wait_queue_head_t wait;
72 bool stopped;
Mark Brown48c98b12011-12-29 09:58:16 -080073 bool wake_enabled;
Joonyoung Shim0fffed22010-07-21 00:45:10 -070074 int irq;
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +090075 enum samsung_keypad_type type;
Joonyoung Shim0fffed22010-07-21 00:45:10 -070076 unsigned int row_shift;
77 unsigned int rows;
78 unsigned int cols;
79 unsigned int row_state[SAMSUNG_MAX_COLS];
80 unsigned short keycodes[];
81};
82
Joonyoung Shim0fffed22010-07-21 00:45:10 -070083static void samsung_keypad_scan(struct samsung_keypad *keypad,
84 unsigned int *row_state)
85{
Joonyoung Shim0fffed22010-07-21 00:45:10 -070086 unsigned int col;
87 unsigned int val;
88
89 for (col = 0; col < keypad->cols; col++) {
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +090090 if (keypad->type == KEYPAD_TYPE_S5PV210) {
Joonyoung Shim0fffed22010-07-21 00:45:10 -070091 val = S5PV210_KEYIFCOLEN_MASK;
92 val &= ~(1 << col) << 8;
93 } else {
94 val = SAMSUNG_KEYIFCOL_MASK;
95 val &= ~(1 << col);
96 }
97
98 writel(val, keypad->base + SAMSUNG_KEYIFCOL);
99 mdelay(1);
100
101 val = readl(keypad->base + SAMSUNG_KEYIFROW);
102 row_state[col] = ~val & ((1 << keypad->rows) - 1);
103 }
104
105 /* KEYIFCOL reg clear */
106 writel(0, keypad->base + SAMSUNG_KEYIFCOL);
107}
108
109static bool samsung_keypad_report(struct samsung_keypad *keypad,
110 unsigned int *row_state)
111{
112 struct input_dev *input_dev = keypad->input_dev;
113 unsigned int changed;
114 unsigned int pressed;
115 unsigned int key_down = 0;
116 unsigned int val;
117 unsigned int col, row;
118
119 for (col = 0; col < keypad->cols; col++) {
120 changed = row_state[col] ^ keypad->row_state[col];
121 key_down |= row_state[col];
122 if (!changed)
123 continue;
124
125 for (row = 0; row < keypad->rows; row++) {
126 if (!(changed & (1 << row)))
127 continue;
128
129 pressed = row_state[col] & (1 << row);
130
131 dev_dbg(&keypad->input_dev->dev,
132 "key %s, row: %d, col: %d\n",
133 pressed ? "pressed" : "released", row, col);
134
135 val = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
136
137 input_event(input_dev, EV_MSC, MSC_SCAN, val);
138 input_report_key(input_dev,
139 keypad->keycodes[val], pressed);
140 }
141 input_sync(keypad->input_dev);
142 }
143
144 memcpy(keypad->row_state, row_state, sizeof(keypad->row_state));
145
146 return key_down;
147}
148
149static irqreturn_t samsung_keypad_irq(int irq, void *dev_id)
150{
151 struct samsung_keypad *keypad = dev_id;
152 unsigned int row_state[SAMSUNG_MAX_COLS];
153 unsigned int val;
154 bool key_down;
155
Mark Brown48c98b12011-12-29 09:58:16 -0800156 pm_runtime_get_sync(&keypad->pdev->dev);
157
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700158 do {
159 val = readl(keypad->base + SAMSUNG_KEYIFSTSCLR);
160 /* Clear interrupt. */
161 writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR);
162
163 samsung_keypad_scan(keypad, row_state);
164
165 key_down = samsung_keypad_report(keypad, row_state);
166 if (key_down)
167 wait_event_timeout(keypad->wait, keypad->stopped,
168 msecs_to_jiffies(50));
169
170 } while (key_down && !keypad->stopped);
171
Mark Brown69898e52012-01-22 23:27:54 -0800172 pm_runtime_put(&keypad->pdev->dev);
Mark Brown48c98b12011-12-29 09:58:16 -0800173
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700174 return IRQ_HANDLED;
175}
176
177static void samsung_keypad_start(struct samsung_keypad *keypad)
178{
179 unsigned int val;
180
Mark Brown48c98b12011-12-29 09:58:16 -0800181 pm_runtime_get_sync(&keypad->pdev->dev);
182
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700183 /* Tell IRQ thread that it may poll the device. */
184 keypad->stopped = false;
185
186 clk_enable(keypad->clk);
187
188 /* Enable interrupt bits. */
189 val = readl(keypad->base + SAMSUNG_KEYIFCON);
190 val |= SAMSUNG_KEYIFCON_INT_F_EN | SAMSUNG_KEYIFCON_INT_R_EN;
191 writel(val, keypad->base + SAMSUNG_KEYIFCON);
192
193 /* KEYIFCOL reg clear. */
194 writel(0, keypad->base + SAMSUNG_KEYIFCOL);
Mark Brown48c98b12011-12-29 09:58:16 -0800195
Mark Brown69898e52012-01-22 23:27:54 -0800196 pm_runtime_put(&keypad->pdev->dev);
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700197}
198
199static void samsung_keypad_stop(struct samsung_keypad *keypad)
200{
201 unsigned int val;
202
Mark Brown48c98b12011-12-29 09:58:16 -0800203 pm_runtime_get_sync(&keypad->pdev->dev);
204
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700205 /* Signal IRQ thread to stop polling and disable the handler. */
206 keypad->stopped = true;
207 wake_up(&keypad->wait);
208 disable_irq(keypad->irq);
209
210 /* Clear interrupt. */
211 writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR);
212
213 /* Disable interrupt bits. */
214 val = readl(keypad->base + SAMSUNG_KEYIFCON);
215 val &= ~(SAMSUNG_KEYIFCON_INT_F_EN | SAMSUNG_KEYIFCON_INT_R_EN);
216 writel(val, keypad->base + SAMSUNG_KEYIFCON);
217
218 clk_disable(keypad->clk);
219
220 /*
221 * Now that chip should not generate interrupts we can safely
222 * re-enable the handler.
223 */
224 enable_irq(keypad->irq);
Mark Brown48c98b12011-12-29 09:58:16 -0800225
Mark Brown69898e52012-01-22 23:27:54 -0800226 pm_runtime_put(&keypad->pdev->dev);
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700227}
228
229static int samsung_keypad_open(struct input_dev *input_dev)
230{
231 struct samsung_keypad *keypad = input_get_drvdata(input_dev);
232
233 samsung_keypad_start(keypad);
234
235 return 0;
236}
237
238static void samsung_keypad_close(struct input_dev *input_dev)
239{
240 struct samsung_keypad *keypad = input_get_drvdata(input_dev);
241
242 samsung_keypad_stop(keypad);
243}
244
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900245#ifdef CONFIG_OF
Dmitry Torokhov670d2072012-11-03 12:16:09 -0700246static struct samsung_keypad_platdata *
247samsung_keypad_parse_dt(struct device *dev)
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900248{
249 struct samsung_keypad_platdata *pdata;
250 struct matrix_keymap_data *keymap_data;
251 uint32_t *keymap, num_rows = 0, num_cols = 0;
252 struct device_node *np = dev->of_node, *key_np;
Tobias Klauser31b95bf2012-08-21 22:04:46 -0700253 unsigned int key_count;
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900254
Dmitry Torokhov670d2072012-11-03 12:16:09 -0700255 if (!np) {
256 dev_err(dev, "missing device tree data\n");
257 return ERR_PTR(-EINVAL);
258 }
259
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900260 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
261 if (!pdata) {
262 dev_err(dev, "could not allocate memory for platform data\n");
Dmitry Torokhov670d2072012-11-03 12:16:09 -0700263 return ERR_PTR(-ENOMEM);
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900264 }
265
266 of_property_read_u32(np, "samsung,keypad-num-rows", &num_rows);
267 of_property_read_u32(np, "samsung,keypad-num-columns", &num_cols);
268 if (!num_rows || !num_cols) {
269 dev_err(dev, "number of keypad rows/columns not specified\n");
Dmitry Torokhov670d2072012-11-03 12:16:09 -0700270 return ERR_PTR(-EINVAL);
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900271 }
272 pdata->rows = num_rows;
273 pdata->cols = num_cols;
274
275 keymap_data = devm_kzalloc(dev, sizeof(*keymap_data), GFP_KERNEL);
276 if (!keymap_data) {
277 dev_err(dev, "could not allocate memory for keymap data\n");
Dmitry Torokhov670d2072012-11-03 12:16:09 -0700278 return ERR_PTR(-ENOMEM);
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900279 }
280 pdata->keymap_data = keymap_data;
281
Tobias Klauser31b95bf2012-08-21 22:04:46 -0700282 key_count = of_get_child_count(np);
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900283 keymap_data->keymap_size = key_count;
284 keymap = devm_kzalloc(dev, sizeof(uint32_t) * key_count, GFP_KERNEL);
285 if (!keymap) {
286 dev_err(dev, "could not allocate memory for keymap\n");
Dmitry Torokhov670d2072012-11-03 12:16:09 -0700287 return ERR_PTR(-ENOMEM);
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900288 }
289 keymap_data->keymap = keymap;
290
291 for_each_child_of_node(np, key_np) {
292 u32 row, col, key_code;
293 of_property_read_u32(key_np, "keypad,row", &row);
294 of_property_read_u32(key_np, "keypad,column", &col);
295 of_property_read_u32(key_np, "linux,code", &key_code);
296 *keymap++ = KEY(row, col, key_code);
297 }
298
299 if (of_get_property(np, "linux,input-no-autorepeat", NULL))
300 pdata->no_autorepeat = true;
Dmitry Torokhov670d2072012-11-03 12:16:09 -0700301
Dmitry Torokhov43b7be32015-07-17 17:04:10 -0700302 pdata->wakeup = of_property_read_bool(np, "wakeup-source") ||
303 /* legacy name */
304 of_property_read_bool(np, "linux,input-wakeup");
305
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900306
307 return pdata;
308}
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900309#else
Dmitry Torokhov670d2072012-11-03 12:16:09 -0700310static struct samsung_keypad_platdata *
311samsung_keypad_parse_dt(struct device *dev)
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900312{
Dmitry Torokhov670d2072012-11-03 12:16:09 -0700313 dev_err(dev, "no platform data defined\n");
314
315 return ERR_PTR(-EINVAL);
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900316}
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900317#endif
318
Bill Pemberton5298cc42012-11-23 21:38:25 -0800319static int samsung_keypad_probe(struct platform_device *pdev)
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700320{
321 const struct samsung_keypad_platdata *pdata;
322 const struct matrix_keymap_data *keymap_data;
323 struct samsung_keypad *keypad;
324 struct resource *res;
325 struct input_dev *input_dev;
326 unsigned int row_shift;
327 unsigned int keymap_size;
328 int error;
329
Dmitry Torokhov670d2072012-11-03 12:16:09 -0700330 pdata = dev_get_platdata(&pdev->dev);
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700331 if (!pdata) {
Dmitry Torokhov670d2072012-11-03 12:16:09 -0700332 pdata = samsung_keypad_parse_dt(&pdev->dev);
333 if (IS_ERR(pdata))
334 return PTR_ERR(pdata);
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700335 }
336
337 keymap_data = pdata->keymap_data;
338 if (!keymap_data) {
339 dev_err(&pdev->dev, "no keymap data defined\n");
340 return -EINVAL;
341 }
342
343 if (!pdata->rows || pdata->rows > SAMSUNG_MAX_ROWS)
344 return -EINVAL;
345
346 if (!pdata->cols || pdata->cols > SAMSUNG_MAX_COLS)
347 return -EINVAL;
348
349 /* initialize the gpio */
350 if (pdata->cfg_gpio)
351 pdata->cfg_gpio(pdata->rows, pdata->cols);
352
353 row_shift = get_count_order(pdata->cols);
354 keymap_size = (pdata->rows << row_shift) * sizeof(keypad->keycodes[0]);
355
Sachin Kamata57da342012-11-27 22:34:38 -0800356 keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad) + keymap_size,
357 GFP_KERNEL);
358 input_dev = devm_input_allocate_device(&pdev->dev);
359 if (!keypad || !input_dev)
360 return -ENOMEM;
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700361
362 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sachin Kamata57da342012-11-27 22:34:38 -0800363 if (!res)
364 return -ENODEV;
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700365
Sachin Kamata57da342012-11-27 22:34:38 -0800366 keypad->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
367 if (!keypad->base)
368 return -EBUSY;
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700369
Sachin Kamata57da342012-11-27 22:34:38 -0800370 keypad->clk = devm_clk_get(&pdev->dev, "keypad");
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700371 if (IS_ERR(keypad->clk)) {
372 dev_err(&pdev->dev, "failed to get keypad clk\n");
Sachin Kamata57da342012-11-27 22:34:38 -0800373 return PTR_ERR(keypad->clk);
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700374 }
375
Thomas Abrahamaba828c2012-10-04 00:03:49 -0700376 error = clk_prepare(keypad->clk);
377 if (error) {
378 dev_err(&pdev->dev, "keypad clock prepare failed\n");
Sachin Kamata57da342012-11-27 22:34:38 -0800379 return error;
Thomas Abrahamaba828c2012-10-04 00:03:49 -0700380 }
381
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700382 keypad->input_dev = input_dev;
Mark Brown48c98b12011-12-29 09:58:16 -0800383 keypad->pdev = pdev;
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700384 keypad->row_shift = row_shift;
385 keypad->rows = pdata->rows;
386 keypad->cols = pdata->cols;
Mark Brown48c98b12011-12-29 09:58:16 -0800387 keypad->stopped = true;
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700388 init_waitqueue_head(&keypad->wait);
389
Thomas Abraham41cc2aa2013-06-25 09:20:08 -0700390 if (pdev->dev.of_node)
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900391 keypad->type = of_device_is_compatible(pdev->dev.of_node,
392 "samsung,s5pv210-keypad");
Thomas Abraham41cc2aa2013-06-25 09:20:08 -0700393 else
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900394 keypad->type = platform_get_device_id(pdev)->driver_data;
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900395
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700396 input_dev->name = pdev->name;
397 input_dev->id.bustype = BUS_HOST;
398 input_dev->dev.parent = &pdev->dev;
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700399
400 input_dev->open = samsung_keypad_open;
401 input_dev->close = samsung_keypad_close;
402
Dmitry Torokhov19328112012-05-10 22:37:08 -0700403 error = matrix_keypad_build_keymap(keymap_data, NULL,
404 pdata->rows, pdata->cols,
405 keypad->keycodes, input_dev);
406 if (error) {
407 dev_err(&pdev->dev, "failed to build keymap\n");
Thomas Abrahamaba828c2012-10-04 00:03:49 -0700408 goto err_unprepare_clk;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700409 }
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700410
411 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
Dmitry Torokhov19328112012-05-10 22:37:08 -0700412 if (!pdata->no_autorepeat)
413 __set_bit(EV_REP, input_dev->evbit);
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700414
Dmitry Torokhov19328112012-05-10 22:37:08 -0700415 input_set_drvdata(input_dev, keypad);
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700416
417 keypad->irq = platform_get_irq(pdev, 0);
418 if (keypad->irq < 0) {
419 error = keypad->irq;
Sachin Kamata57da342012-11-27 22:34:38 -0800420 goto err_unprepare_clk;
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700421 }
422
Sachin Kamata57da342012-11-27 22:34:38 -0800423 error = devm_request_threaded_irq(&pdev->dev, keypad->irq, NULL,
424 samsung_keypad_irq, IRQF_ONESHOT,
425 dev_name(&pdev->dev), keypad);
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700426 if (error) {
427 dev_err(&pdev->dev, "failed to register keypad interrupt\n");
Sachin Kamata57da342012-11-27 22:34:38 -0800428 goto err_unprepare_clk;
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700429 }
430
Mark Brown48c98b12011-12-29 09:58:16 -0800431 device_init_wakeup(&pdev->dev, pdata->wakeup);
432 platform_set_drvdata(pdev, keypad);
433 pm_runtime_enable(&pdev->dev);
434
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700435 error = input_register_device(keypad->input_dev);
436 if (error)
Sachin Kamata57da342012-11-27 22:34:38 -0800437 goto err_disable_runtime_pm;
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700438
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900439 if (pdev->dev.of_node) {
440 devm_kfree(&pdev->dev, (void *)pdata->keymap_data->keymap);
441 devm_kfree(&pdev->dev, (void *)pdata->keymap_data);
442 devm_kfree(&pdev->dev, (void *)pdata);
443 }
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700444 return 0;
445
Sachin Kamata57da342012-11-27 22:34:38 -0800446err_disable_runtime_pm:
Mark Brown48c98b12011-12-29 09:58:16 -0800447 pm_runtime_disable(&pdev->dev);
448 device_init_wakeup(&pdev->dev, 0);
Thomas Abrahamaba828c2012-10-04 00:03:49 -0700449err_unprepare_clk:
450 clk_unprepare(keypad->clk);
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700451 return error;
452}
453
Bill Pembertone2619cf2012-11-23 21:50:47 -0800454static int samsung_keypad_remove(struct platform_device *pdev)
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700455{
456 struct samsung_keypad *keypad = platform_get_drvdata(pdev);
457
Mark Brown48c98b12011-12-29 09:58:16 -0800458 pm_runtime_disable(&pdev->dev);
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700459 device_init_wakeup(&pdev->dev, 0);
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700460
461 input_unregister_device(keypad->input_dev);
462
Thomas Abrahamaba828c2012-10-04 00:03:49 -0700463 clk_unprepare(keypad->clk);
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700464
465 return 0;
466}
467
Rafael J. Wysockidc5686e2014-12-04 01:09:12 +0100468#ifdef CONFIG_PM
Mark Brown48c98b12011-12-29 09:58:16 -0800469static int samsung_keypad_runtime_suspend(struct device *dev)
470{
471 struct platform_device *pdev = to_platform_device(dev);
472 struct samsung_keypad *keypad = platform_get_drvdata(pdev);
473 unsigned int val;
474 int error;
475
476 if (keypad->stopped)
477 return 0;
478
479 /* This may fail on some SoCs due to lack of controller support */
480 error = enable_irq_wake(keypad->irq);
481 if (!error)
482 keypad->wake_enabled = true;
483
484 val = readl(keypad->base + SAMSUNG_KEYIFCON);
485 val |= SAMSUNG_KEYIFCON_WAKEUPEN;
486 writel(val, keypad->base + SAMSUNG_KEYIFCON);
487
488 clk_disable(keypad->clk);
489
490 return 0;
491}
492
493static int samsung_keypad_runtime_resume(struct device *dev)
494{
495 struct platform_device *pdev = to_platform_device(dev);
496 struct samsung_keypad *keypad = platform_get_drvdata(pdev);
497 unsigned int val;
498
499 if (keypad->stopped)
500 return 0;
501
502 clk_enable(keypad->clk);
503
504 val = readl(keypad->base + SAMSUNG_KEYIFCON);
505 val &= ~SAMSUNG_KEYIFCON_WAKEUPEN;
506 writel(val, keypad->base + SAMSUNG_KEYIFCON);
507
508 if (keypad->wake_enabled)
509 disable_irq_wake(keypad->irq);
510
511 return 0;
512}
513#endif
514
Dmitry Torokhov400bf292011-11-07 23:59:35 -0800515#ifdef CONFIG_PM_SLEEP
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700516static void samsung_keypad_toggle_wakeup(struct samsung_keypad *keypad,
517 bool enable)
518{
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700519 unsigned int val;
520
521 clk_enable(keypad->clk);
522
523 val = readl(keypad->base + SAMSUNG_KEYIFCON);
524 if (enable) {
525 val |= SAMSUNG_KEYIFCON_WAKEUPEN;
Mark Brown48c98b12011-12-29 09:58:16 -0800526 if (device_may_wakeup(&keypad->pdev->dev))
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700527 enable_irq_wake(keypad->irq);
528 } else {
529 val &= ~SAMSUNG_KEYIFCON_WAKEUPEN;
Mark Brown48c98b12011-12-29 09:58:16 -0800530 if (device_may_wakeup(&keypad->pdev->dev))
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700531 disable_irq_wake(keypad->irq);
532 }
533 writel(val, keypad->base + SAMSUNG_KEYIFCON);
534
535 clk_disable(keypad->clk);
536}
537
538static int samsung_keypad_suspend(struct device *dev)
539{
540 struct platform_device *pdev = to_platform_device(dev);
541 struct samsung_keypad *keypad = platform_get_drvdata(pdev);
542 struct input_dev *input_dev = keypad->input_dev;
543
544 mutex_lock(&input_dev->mutex);
545
546 if (input_dev->users)
547 samsung_keypad_stop(keypad);
548
549 samsung_keypad_toggle_wakeup(keypad, true);
550
551 mutex_unlock(&input_dev->mutex);
552
553 return 0;
554}
555
556static int samsung_keypad_resume(struct device *dev)
557{
558 struct platform_device *pdev = to_platform_device(dev);
559 struct samsung_keypad *keypad = platform_get_drvdata(pdev);
560 struct input_dev *input_dev = keypad->input_dev;
561
562 mutex_lock(&input_dev->mutex);
563
564 samsung_keypad_toggle_wakeup(keypad, false);
565
566 if (input_dev->users)
567 samsung_keypad_start(keypad);
568
569 mutex_unlock(&input_dev->mutex);
570
571 return 0;
572}
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700573#endif
574
Mark Brown48c98b12011-12-29 09:58:16 -0800575static const struct dev_pm_ops samsung_keypad_pm_ops = {
576 SET_SYSTEM_SLEEP_PM_OPS(samsung_keypad_suspend, samsung_keypad_resume)
577 SET_RUNTIME_PM_OPS(samsung_keypad_runtime_suspend,
578 samsung_keypad_runtime_resume, NULL)
579};
Dmitry Torokhov400bf292011-11-07 23:59:35 -0800580
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900581#ifdef CONFIG_OF
582static const struct of_device_id samsung_keypad_dt_match[] = {
583 { .compatible = "samsung,s3c6410-keypad" },
584 { .compatible = "samsung,s5pv210-keypad" },
585 {},
586};
587MODULE_DEVICE_TABLE(of, samsung_keypad_dt_match);
Thomas Abrahamb3d6ac32011-11-02 19:37:22 +0900588#endif
589
Krzysztof Kozlowski8550b072015-05-06 16:54:56 -0700590static const struct platform_device_id samsung_keypad_driver_ids[] = {
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700591 {
592 .name = "samsung-keypad",
593 .driver_data = KEYPAD_TYPE_SAMSUNG,
594 }, {
595 .name = "s5pv210-keypad",
596 .driver_data = KEYPAD_TYPE_S5PV210,
597 },
598 { },
599};
600MODULE_DEVICE_TABLE(platform, samsung_keypad_driver_ids);
601
602static struct platform_driver samsung_keypad_driver = {
603 .probe = samsung_keypad_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800604 .remove = samsung_keypad_remove,
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700605 .driver = {
606 .name = "samsung-keypad",
Tobias Klauserf4d46612012-08-21 22:04:32 -0700607 .of_match_table = of_match_ptr(samsung_keypad_dt_match),
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700608 .pm = &samsung_keypad_pm_ops,
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700609 },
610 .id_table = samsung_keypad_driver_ids,
611};
JJ Ding5146c842011-11-29 11:08:39 -0800612module_platform_driver(samsung_keypad_driver);
Joonyoung Shim0fffed22010-07-21 00:45:10 -0700613
614MODULE_DESCRIPTION("Samsung keypad driver");
615MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
616MODULE_AUTHOR("Donghwa Lee <dh09.lee@samsung.com>");
617MODULE_LICENSE("GPL");