Input: sc_keypad: remove event log
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / input / keyboard / sc_keypad.c
1 /*
2  * Copyright (C) 2012 Spreadtrum Communications Inc.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/io.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/fs.h>
21 #include <linux/interrupt.h>
22 #include <linux/irq.h>
23 #include <linux/sched.h>
24 #include <linux/pm.h>
25 #include <linux/sysctl.h>
26 #include <linux/proc_fs.h>
27 #include <linux/gpio.h>
28 #include <linux/delay.h>
29 #include <linux/platform_device.h>
30 #include <linux/input.h>
31 #include <linux/input/matrix_keypad.h>
32 #include <linux/sysrq.h>
33 #include <linux/sched.h>
34 #include <linux/of_device.h>
35 #include <linux/of_address.h>
36 #include <linux/of_gpio.h>
37
38 #include <soc/sprd/globalregs.h>
39 #include <soc/sprd/hardware.h>
40 #include <soc/sprd/board.h>
41 #include <soc/sprd/gpio.h>
42 #include <soc/sprd/adi.h>
43 #include <soc/sprd/kpd.h>
44 #include <soc/sprd/sci.h>
45 #include <soc/sprd/sci_glb_regs.h>
46 #include <linux/wakelock.h>
47 #if CONFIG_SEC_DEBUG
48 #include <soc/sprd/sec_debug.h>
49 #endif
50
51 #include <linux/input-hook.h>
52
53 #define WAKELOCK_TIME   HZ/10
54
55 #define DEBUG_KEYPAD    0
56
57 #ifndef CONFIG_OF
58 #define KPD_REG_BASE                (SPRD_KPD_BASE)
59 #else
60 static unsigned long KPD_REG_BASE;
61 #endif
62
63 #define KPD_CTRL                        (KPD_REG_BASE + 0x00)
64 #define KPD_EN                                          (0x01 << 0)
65 #define KPD_SLEEP_EN                            (0x01 << 1)
66 #define KPD_LONG_KEY_EN                         (0x01 << 2)
67
68 //v0
69 #define KPDCTL_ROW_MSK_V0                  (0x3f << 18) /* enable rows 2 - 7 */
70 #define KPDCTL_COL_MSK_V0                  (0x3f << 10) /* enable cols 2 - 7 */
71
72 //v1
73 #define KPDCTL_ROW_MSK_V1                  (0xff << 16) /* enable rows 0 - 7 */
74 #define KPDCTL_COL_MSK_V1                  (0xff << 8)  /* enable cols 0 - 7 */
75
76 #define KPD_INT_EN                      (KPD_REG_BASE + 0x04)
77 #define KPD_INT_RAW_STATUS          (KPD_REG_BASE + 0x08)
78 #define KPD_INT_MASK_STATUS             (KPD_REG_BASE + 0x0C)
79 #define KPD_INT_ALL                 (0xfff)
80 #define KPD_INT_DOWNUP              (0x0ff)
81 #define KPD_INT_LONG                            (0xf00)
82 #define KPD_PRESS_INT0              (1 << 0)
83 #define KPD_PRESS_INT1              (1 << 1)
84 #define KPD_PRESS_INT2              (1 << 2)
85 #define KPD_PRESS_INT3              (1 << 3)
86 #define KPD_RELEASE_INT0            (1 << 4)
87 #define KPD_RELEASE_INT1            (1 << 5)
88 #define KPD_RELEASE_INT2            (1 << 6)
89 #define KPD_RELEASE_INT3            (1 << 7)
90 #define KPD_LONG_KEY_INT0           (1 << 8)
91 #define KPD_LONG_KEY_INT1           (1 << 9)
92 #define KPD_LONG_KEY_INT2           (1 << 10)
93 #define KPD_LONG_KEY_INT3           (1 << 11)
94
95 #define KPD_INT_CLR                     (KPD_REG_BASE + 0x10)
96 #define KPD_POLARITY                    (KPD_REG_BASE + 0x18)
97 #define KPD_CFG_ROW_POLARITY            (0xff)
98 #define KPD_CFG_COL_POLARITY            (0xFF00)
99 #define CFG_ROW_POLARITY            (KPD_CFG_ROW_POLARITY & 0x00FF)
100 #define CFG_COL_POLARITY            (KPD_CFG_COL_POLARITY & 0xFF00)
101
102 #define KPD_DEBOUNCE_CNT                (KPD_REG_BASE + 0x1C)
103 #define KPD_LONG_KEY_CNT                (KPD_REG_BASE + 0x20)
104
105 #define KPD_SLEEP_CNT                   (KPD_REG_BASE + 0x24)
106 #define KPD_SLEEP_CNT_VALUE(_X_MS_)     (_X_MS_ * 32.768 -1)
107 #define KPD_CLK_DIV_CNT                 (KPD_REG_BASE + 0x28)
108
109 #define KPD_KEY_STATUS                  (KPD_REG_BASE + 0x2C)
110 #define KPD_INT0_COL(_X_)       (((_X_)>> 0) & 0x7)
111 #define KPD_INT0_ROW(_X_)       (((_X_)>> 4) & 0x7)
112 #define KPD_INT0_DOWN(_X_)      (((_X_)>> 7) & 0x1)
113 #define KPD_INT1_COL(_X_)       (((_X_)>> 8) & 0x7)
114 #define KPD_INT1_ROW(_X_)       (((_X_)>> 12) & 0x7)
115 #define KPD_INT1_DOWN(_X_)      (((_X_)>> 15) & 0x1)
116 #define KPD_INT2_COL(_X_)       (((_X_)>> 16) & 0x7)
117 #define KPD_INT2_ROW(_X_)       (((_X_)>> 20) & 0x7)
118 #define KPD_INT2_DOWN(_X_)      (((_X_)>> 23) & 0x1)
119 #define KPD_INT3_COL(_X_)       (((_X_)>> 24) & 0x7)
120 #define KPD_INT3_ROW(_X_)       (((_X_)>> 28) & 0x7)
121 #define KPD_INT3_DOWN(_X_)      (((_X_)>> 31) & 0x1)
122
123 #define KPD_SLEEP_STATUS                (KPD_REG_BASE + 0x0030)
124 #define KPD_DEBUG_STATUS1               (KPD_REG_BASE + 0x0034)
125 #define KPD_DEBUG_STATUS2               (KPD_REG_BASE + 0x0038)
126
127 #ifndef CONFIG_OF
128 #define PB_INT                  EIC_KEY_POWER
129 #else
130 static int PB_INT;
131 #endif
132
133 #if defined(CONFIG_ARCH_SC8825)
134 static __devinit void __keypad_enable(void)
135 {
136         sci_glb_set(REG_GLB_SOFT_RST, BIT_KPD_RST);
137         mdelay(2);
138         sci_glb_clr(REG_GLB_SOFT_RST, BIT_KPD_RST);
139         sci_glb_set(REG_GLB_GEN0, BIT_KPD_EB | BIT_RTC_KPD_EB);
140 }
141 static __devexit void __keypad_disable(void)
142 {
143         sci_glb_clr(REG_GLB_GEN0, BIT_KPD_EB | BIT_RTC_KPD_EB);
144 }
145
146 static int __keypad_controller_ver(void)
147 {
148         return 0;
149 }
150
151 #elif defined(CONFIG_ARCH_SCX35)
152 static void __keypad_enable(void)
153 {
154         sci_glb_set(REG_AON_APB_APB_RST0, BIT_KPD_SOFT_RST);
155         mdelay(2);
156         sci_glb_clr(REG_AON_APB_APB_RST0, BIT_KPD_SOFT_RST);
157         sci_glb_set(REG_AON_APB_APB_EB0, BIT_KPD_EB);
158         sci_glb_set(REG_AON_APB_APB_RTC_EB, BIT_KPD_RTC_EB);
159 }
160
161 static void __keypad_disable(void)
162 {
163         sci_glb_clr(REG_AON_APB_APB_EB0, BIT_KPD_EB);
164         sci_glb_clr(REG_AON_APB_APB_RTC_EB, BIT_KPD_RTC_EB);
165 }
166 static int __keypad_controller_ver(void)
167 {
168         return 1;
169 }
170
171 #else
172 #error "Pls fill the low level enable function"
173 #endif
174
175 struct sci_keypad_t {
176         struct input_dev *input_dev;
177         int irq;
178         int rows;
179         int cols;
180         unsigned int keyup_test_jiffies;
181         int powerkey_state;
182         struct wake_lock         key_wake_lock;
183 };
184
185 /* sys fs  */
186 extern struct class *sec_class;
187 struct device *key_dev, *powerkey_dev;
188 EXPORT_SYMBOL(key_dev);
189 EXPORT_SYMBOL(powerkey_dev);
190 extern uint8_t vol_down_keys_pressed;
191 uint8_t keys_pressed;
192 static ssize_t key_show(struct device *dev, struct device_attribute *attr, char *buf);
193 static ssize_t powerkey_show(struct device *dev, struct device_attribute *attr, char *buf);
194 static ssize_t store_reset_enable(struct device *dev, struct device_attribute
195                 *devattr, const char *buf, size_t count);
196 static ssize_t show_reset_enable(struct device *dev, struct device_attribute *attr, char *buf);
197
198 static DEVICE_ATTR(sec_key_pressed, S_IRUGO, key_show, NULL);
199 static DEVICE_ATTR(sec_powerkey_pressed, S_IRUGO, powerkey_show, NULL);
200 static DEVICE_ATTR(reset_enable, S_IRUGO | S_IWUSR | S_IWGRP, show_reset_enable, store_reset_enable);
201
202 static struct attribute *sec_powerkey_attributes[] = {
203         &dev_attr_sec_powerkey_pressed.attr,
204         &dev_attr_reset_enable.attr,
205         NULL,
206 };
207
208 static struct attribute_group sec_powerkey_attr_group = {
209         .attrs  = sec_powerkey_attributes,
210 };
211
212 static ssize_t key_show(struct device *dev, struct device_attribute *attr, char *buf)
213 {
214         uint8_t default_press = 0x80;
215         int is_key_checked = 0;
216         uint8_t keys_pressed;
217
218         keys_pressed = __raw_readl(KPD_KEY_STATUS);
219         if ((keys_pressed & default_press) | vol_down_keys_pressed)
220                 is_key_checked = sprintf(buf, "%s\n", "PRESS");
221         else
222                 is_key_checked = sprintf(buf, "%s\n", "RELEASE");
223
224         return is_key_checked;
225 }
226 static ssize_t powerkey_show(struct device *dev, struct device_attribute *attr, char *buf)
227 {
228         struct sci_keypad_t *sci_kpd = dev_get_drvdata(powerkey_dev);
229         int is_key_checked = 0;
230
231         is_key_checked = sprintf(buf, "%s\n",
232                 sci_kpd->powerkey_state ? "PRESS" : "RELEASE");
233
234         return is_key_checked;
235 }
236
237 static ssize_t store_reset_enable(struct device *dev, struct device_attribute
238                 *devattr, const char *buf, size_t count)
239 {
240         int ret, value;
241
242         ret = kstrtoul(buf, 10, &value);
243         if (ret) {
244                 pr_err("%s: failed to get buf value\n", __func__);
245                 return -EINVAL;
246         }
247
248         if(value)
249                 sci_adi_set(ANA_REG_GLB_SWRST_CTRL, BIT_KEY2_7S_RST_EN);
250         else
251                 sci_adi_clr(ANA_REG_GLB_SWRST_CTRL, BIT_KEY2_7S_RST_EN);
252
253         pr_info("%s: %sabled one key reset.\n", __func__, value ? "en":"dis");
254
255         return count;
256 }
257
258 static ssize_t show_reset_enable(struct device *dev, struct device_attribute *attr, char *buf)
259 {
260         int reg_val;
261
262         reg_val = sci_adi_read(ANA_REG_GLB_SWRST_CTRL);
263         reg_val &= BIT_KEY2_7S_RST_EN;
264
265         pr_info("%s: %sabled one key reset.\n", __func__, reg_val ? "en":"dis");
266
267         return sprintf(buf, "%d\n", !!reg_val);
268 }
269
270 /* sys fs */
271
272 #if     DEBUG_KEYPAD
273 static void dump_keypad_register(void)
274 {
275 #define INT_MASK_STS                (SPRD_INTC1_BASE + 0x0000)
276 #define INT_RAW_STS                 (SPRD_INTC1_BASE + 0x0004)
277 #define INT_EN                      (SPRD_INTC1_BASE + 0x0008)
278 #define INT_DIS                     (SPRD_INTC1_BASE + 0x000C)
279
280         printk("\nREG_INT_MASK_STS = 0x%08x\n", __raw_readl(INT_MASK_STS));
281         printk("REG_INT_RAW_STS = 0x%08x\n", __raw_readl(INT_RAW_STS));
282         printk("REG_INT_EN = 0x%08x\n", __raw_readl(INT_EN));
283         printk("REG_INT_DIS = 0x%08x\n", __raw_readl(INT_DIS));
284         printk("REG_KPD_CTRL = 0x%08x\n", __raw_readl(KPD_CTRL));
285         printk("REG_KPD_INT_EN = 0x%08x\n", __raw_readl(KPD_INT_EN));
286         printk("REG_KPD_INT_RAW_STATUS = 0x%08x\n",
287                __raw_readl(KPD_INT_RAW_STATUS));
288         printk("REG_KPD_INT_MASK_STATUS = 0x%08x\n",
289                __raw_readl(KPD_INT_MASK_STATUS));
290         printk("REG_KPD_INT_CLR = 0x%08x\n", __raw_readl(KPD_INT_CLR));
291         printk("REG_KPD_POLARITY = 0x%08x\n", __raw_readl(KPD_POLARITY));
292         printk("REG_KPD_DEBOUNCE_CNT = 0x%08x\n",
293                __raw_readl(KPD_DEBOUNCE_CNT));
294         printk("REG_KPD_LONG_KEY_CNT = 0x%08x\n",
295                __raw_readl(KPD_LONG_KEY_CNT));
296         printk("REG_KPD_SLEEP_CNT = 0x%08x\n", __raw_readl(KPD_SLEEP_CNT));
297         printk("REG_KPD_CLK_DIV_CNT = 0x%08x\n", __raw_readl(KPD_CLK_DIV_CNT));
298         printk("REG_KPD_KEY_STATUS = 0x%08x\n", __raw_readl(KPD_KEY_STATUS));
299         printk("REG_KPD_SLEEP_STATUS = 0x%08x\n",
300                __raw_readl(KPD_SLEEP_STATUS));
301 }
302 #else
303 static void dump_keypad_register(void)
304 {
305 }
306 #endif
307
308 static int sci_keypad_check_validity(unsigned short key)
309 {
310         if ((key == KEY_VOLUMEUP) || (key == KEY_VOLUMEDOWN) ||\
311                 (key == KEY_MENU) || (key == KEY_POWER))
312                 return 0;
313         else
314                 return -1;
315 }
316
317 static void sci_keypad_report_event(struct sci_keypad_t *sci_kpd, int col, int row, int state)
318 {
319         unsigned short *keycodes = sci_kpd->input_dev->keycode;
320         unsigned int row_shift = get_count_order(sci_kpd->cols);
321         unsigned short key;
322
323         key = keycodes[MATRIX_SCAN_CODE(row, col, row_shift)];
324
325         if (sci_keypad_check_validity(key))
326                 return;
327
328         if (key == KEY_MENU)
329                 wake_lock_timeout
330                         (&sci_kpd->key_wake_lock, WAKELOCK_TIME);
331
332         input_report_key(sci_kpd->input_dev, key, state);
333         input_sync(sci_kpd->input_dev);
334         sec_debug_check_crash_key(key, state);
335
336         return;
337 }
338
339 static irqreturn_t sci_keypad_isr(int irq, void *dev_id)
340 {
341         unsigned short key = 0;
342         unsigned long value;
343         struct sci_keypad_t *sci_kpd = dev_id;
344         unsigned long int_status = __raw_readl(KPD_INT_MASK_STATUS);
345         unsigned long key_status = __raw_readl(KPD_KEY_STATUS);
346         unsigned short *keycodes = sci_kpd->input_dev->keycode;
347         unsigned int row_shift = get_count_order(sci_kpd->cols);
348         int col, row;
349
350         value = __raw_readl(KPD_INT_CLR);
351         value |= KPD_INT_ALL;
352         __raw_writel(value, KPD_INT_CLR);
353
354         if ((int_status & KPD_PRESS_INT0)) {
355                 col = KPD_INT0_COL(key_status);
356                 row = KPD_INT0_ROW(key_status);
357                 sci_keypad_report_event(dev_id, col, row, true);
358         }
359         if (int_status & KPD_RELEASE_INT0) {
360                 col = KPD_INT0_COL(key_status);
361                 row = KPD_INT0_ROW(key_status);
362                 key = keycodes[MATRIX_SCAN_CODE(row, col, row_shift)];
363                 sci_keypad_report_event(dev_id, col, row, false);
364         }
365
366         if ((int_status & KPD_PRESS_INT1)) {
367                 col = KPD_INT1_COL(key_status);
368                 row = KPD_INT1_ROW(key_status);
369                 key = keycodes[MATRIX_SCAN_CODE(row, col, row_shift)];
370                 sci_keypad_report_event(dev_id, col, row, true);
371         }
372         if (int_status & KPD_RELEASE_INT1) {
373                 col = KPD_INT1_COL(key_status);
374                 row = KPD_INT1_ROW(key_status);
375                 key = keycodes[MATRIX_SCAN_CODE(row, col, row_shift)];
376                 sci_keypad_report_event(dev_id, col, row, false);
377         }
378
379         if ((int_status & KPD_PRESS_INT2)) {
380                 col = KPD_INT2_COL(key_status);
381                 row = KPD_INT2_ROW(key_status);
382                 key = keycodes[MATRIX_SCAN_CODE(row, col, row_shift)];
383                 sci_keypad_report_event(dev_id, col, row, true);
384         }
385         if (int_status & KPD_RELEASE_INT2) {
386                 col = KPD_INT2_COL(key_status);
387                 row = KPD_INT2_ROW(key_status);
388                 key = keycodes[MATRIX_SCAN_CODE(row, col, row_shift)];
389                 sci_keypad_report_event(dev_id, col, row, false);
390         }
391
392         if (int_status & KPD_PRESS_INT3) {
393                 col = KPD_INT3_COL(key_status);
394                 row = KPD_INT3_ROW(key_status);
395                 key = keycodes[MATRIX_SCAN_CODE(row, col, row_shift)];
396                 sci_keypad_report_event(dev_id, col, row, true);
397         }
398         if (int_status & KPD_RELEASE_INT3) {
399                 col = KPD_INT3_COL(key_status);
400                 row = KPD_INT3_ROW(key_status);
401                 key = keycodes[MATRIX_SCAN_CODE(row, col, row_shift)];
402                 sci_keypad_report_event(dev_id, col, row, false);
403         }
404
405         return IRQ_HANDLED;
406 }
407
408 static irqreturn_t sci_powerkey_isr(int irq, void *dev_id)
409 {                               //TODO: if usign gpio(eic), need add row , cols to platform data.
410         static unsigned long last_value = 1;
411         unsigned short key = KEY_POWER;
412         unsigned long value = !(gpio_get_value(PB_INT));
413         struct sci_keypad_t *sci_kpd = dev_id;
414
415         wake_lock_timeout(&sci_kpd->key_wake_lock, WAKELOCK_TIME);
416
417         if (last_value == value) {
418                 /* seems an event is missing, just report it */
419                 input_report_key(sci_kpd->input_dev, key, last_value);
420                 input_sync(sci_kpd->input_dev);
421         }
422
423         if (value) {
424                 /* Release : low level */
425 #ifdef HOOK_POWER_KEY
426                 input_report_key_hook(sci_kpd->input_dev, key, 0);
427 #endif
428                 sci_kpd->powerkey_state = 0;
429                 input_report_key(sci_kpd->input_dev, key, 0);
430                 input_sync(sci_kpd->input_dev);
431                 irq_set_irq_type(irq, IRQF_TRIGGER_HIGH);
432         } else {
433                 /* Press : high level */
434 #ifdef HOOK_POWER_KEY
435                 input_report_key_hook(sci_kpd->input_dev, key, 1);
436 #endif
437                 sci_kpd->powerkey_state = 1;
438                 input_report_key(sci_kpd->input_dev, key, 1);
439                 input_sync(sci_kpd->input_dev);
440                 irq_set_irq_type(irq, IRQF_TRIGGER_LOW);
441         }
442
443 #if CONFIG_SEC_DEBUG
444         sec_debug_check_crash_key(key, value);
445 #endif
446         last_value = value;
447
448         return IRQ_HANDLED;
449 }
450 #ifdef CONFIG_OF
451 static struct sci_keypad_platdata *sci_keypad_parse_dt(
452                 struct device *dev)
453 {
454         struct sci_keypad_platform_data *pdata;
455         struct device_node *np = dev->of_node, *key_np;
456         uint32_t num_rows, num_cols;
457         uint32_t rows_choose_hw, cols_choose_hw;
458         uint32_t debounce_time;
459         struct matrix_keymap_data *keymap_data;
460         uint32_t *keymap, key_count;
461         struct resource res;
462         int ret;
463
464         ret = of_address_to_resource(np, 0, &res);
465         if(ret < 0){
466                 dev_err(dev, "no reg of property specified\n");
467                 return NULL;
468         }
469         KPD_REG_BASE = (unsigned long)ioremap_nocache(res.start,
470                         resource_size(&res));
471         if(!KPD_REG_BASE)
472                 BUG();
473         PB_INT = of_get_gpio(np, 0);
474         if(PB_INT < 0){
475                 dev_err(dev, "no gpios of property specified\n");
476                 return NULL;
477         }
478         pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
479         if (!pdata) {
480                 dev_err(dev, "could not allocate memory for platform data\n");
481                 return NULL;
482         }
483         ret = of_property_read_u32(np, "sprd,keypad-num-rows", &num_rows);
484         if(ret){
485                 dev_err(dev, "no sprd,keypad-num-rows of property specified\n");
486                 goto fail;
487         }
488         pdata->rows_number = num_rows;
489
490         ret = of_property_read_u32(np, "sprd,keypad-num-columns", &num_cols);
491         if(ret){
492                 dev_err(dev, "no sprd,keypad-num-columns of property specified\n");
493                 goto fail;
494         }
495         pdata->cols_number = num_cols;
496
497         ret = of_property_read_u32(np, "sprd,debounce_time", &debounce_time);
498         if(ret){
499                 debounce_time = 0;
500         }
501         pdata->debounce_time = debounce_time;
502
503         if (of_get_property(np, "linux,input-no-autorepeat", NULL))
504                 pdata->repeat = false;
505         if(of_get_property(np, "sprd,support_long_key", NULL))
506                 pdata->support_long_key = false;
507         if (of_get_property(np, "linux,input-wakeup", NULL))
508                 pdata->wakeup = true;
509
510
511         ret = of_property_read_u32(np, "sprd,keypad-rows-choose-hw", &rows_choose_hw);
512         if(ret){
513                 dev_err(dev, "no sprd,keypad-rows-choose-hw of property specified\n");
514                 goto fail;
515         }
516         pdata->rows_choose_hw = rows_choose_hw;
517
518         ret = of_property_read_u32(np, "sprd,keypad-cols-choose-hw", &cols_choose_hw);
519         if(ret){
520                 dev_err(dev, "no sprd,keypad-cols-choose-hw of property specified\n");
521                 goto fail;
522         }
523         pdata->cols_choose_hw = cols_choose_hw;
524
525         keymap_data = kzalloc(sizeof(*keymap_data), GFP_KERNEL);
526         if (!keymap_data) {
527                 dev_err(dev, "could not allocate memory for keymap_data\n");
528                 goto fail;
529         }
530         pdata->keymap_data = keymap_data;
531
532         key_count = of_get_child_count(np);
533         keymap_data->keymap_size = key_count;
534         keymap = kzalloc(sizeof(uint32_t) * key_count, GFP_KERNEL);
535         if (!keymap) {
536                 dev_err(dev, "could not allocate memory for keymap\n");
537                 goto fail_keymap;
538         }
539         keymap_data->keymap = keymap;
540
541         for_each_child_of_node(np, key_np) {
542                 u32 row, col, key_code;
543                 ret = of_property_read_u32(key_np, "keypad,row", &row);
544                 if(ret)
545                         goto fail_parse_keymap;
546                 ret = of_property_read_u32(key_np, "keypad,column", &col);
547                 if(ret)
548                         goto fail_parse_keymap;
549                 ret = of_property_read_u32(key_np, "linux,code", &key_code);
550                 if(ret)
551                         goto fail_parse_keymap;
552                 *keymap++ = KEY(row, col, key_code);
553                 pr_info("sci_keypad_parse_dt: %d, %d, %d\n",row, col, key_code);
554         }
555
556
557         return pdata;
558
559 fail_parse_keymap:
560         dev_err(dev, "failed parsing keymap\n");
561         kfree(keymap);
562         keymap_data->keymap = NULL;
563 fail_keymap:
564         kfree(keymap_data);
565         pdata->keymap_data = NULL;
566 fail:
567         kfree(pdata);
568         return NULL;
569 }
570 #else
571 static struct sci_keypad_platdata *sci_keypad_parse_dt(
572                 struct device *dev)
573 {
574         return NULL;
575 }
576 #endif
577
578 /*****************/
579 static int sprd_eic_button_state(void)
580 {
581         int button_state = 0;
582         int gpio_value = 0;
583
584         gpio_value = !!gpio_get_value(PB_INT);
585
586         //if(1 == button->active_low) {
587         if(0) {
588                 if(0 == gpio_value)
589                         button_state = 1;
590                 else
591                         button_state = 0;
592         } else {
593                 if(1 == gpio_value)
594                         button_state = 1;
595                 else
596                         button_state = 0;
597         }
598
599         printk("GPIO_%d=%d, button_state=%d\n", PB_INT, gpio_value, button_state);
600
601         return button_state; //0==released, 1==pressed
602 }
603
604 static ssize_t sprd_eic_button_show(struct kobject *kobj, struct kobj_attribute *attr, char *buff)
605 {
606         int status = 0;
607
608         status = sprd_eic_button_state();
609         printk("button status = %d (0 == released, 1 == pressed)\n", status);
610         return sprintf(buff, "%d\n", status);
611 }
612
613 static struct kobject *sprd_eic_button_kobj = NULL;
614 static struct kobj_attribute sprd_eic_button_attr =
615         __ATTR(status, 0644, sprd_eic_button_show, NULL);
616
617 static int sprd_eic_button_sysfs_init(void)
618 {
619         int ret = -1;
620
621         sprd_eic_button_kobj = kobject_create_and_add("sprd_eic_button", kernel_kobj);
622         if (sprd_eic_button_kobj == NULL) {
623                 ret = -ENOMEM;
624                 printk("register sysfs failed. ret = %d\n", ret);
625                 return ret;
626         }
627
628         ret = sysfs_create_file(sprd_eic_button_kobj, &sprd_eic_button_attr.attr);
629         if (ret) {
630                 printk("create sysfs failed. ret = %d\n", ret);
631                 return ret;
632         }
633
634         printk("sprd_eic_button_sysfs_init success\n");
635         return ret;
636 }
637 /*****************/
638
639 static int sci_keypad_probe(struct platform_device *pdev)
640 {
641         struct sci_keypad_t *sci_kpd;
642         struct input_dev *input_dev;
643         struct sci_keypad_platform_data *pdata = pdev->dev.platform_data;
644         int error;
645         unsigned long value;
646         unsigned int row_shift, keycodemax;
647         struct device_node *np = pdev->dev.of_node;
648
649         if (pdev->dev.of_node && !pdata){
650                 pdata = sci_keypad_parse_dt(&pdev->dev);
651                 if(pdata)
652                         pdev->dev.platform_data = pdata;
653         }
654
655         if (!pdata) {
656                 printk(KERN_WARNING "sci_keypad_probe get platform_data NULL\n");
657                 error = -EINVAL;
658                 goto out0;
659         }
660         row_shift = get_count_order(pdata->cols_number);
661         keycodemax = pdata->rows_number << row_shift;
662
663         sci_kpd = kzalloc(sizeof(struct sci_keypad_t) +
664                           keycodemax * sizeof(unsigned short), GFP_KERNEL);
665         input_dev = input_allocate_device();
666
667         if (!sci_kpd || !input_dev) {
668                 error = -ENOMEM;
669                 goto out1;
670         }
671         platform_set_drvdata(pdev, sci_kpd);
672
673         wake_lock_init(&sci_kpd->key_wake_lock,
674                         WAKE_LOCK_SUSPEND, "power-keys_wake_lock");
675
676         sci_kpd->input_dev = input_dev;
677         sci_kpd->rows = pdata->rows_number;
678         sci_kpd->cols = pdata->cols_number;
679
680         __keypad_enable();
681
682         __raw_writel(KPD_INT_ALL, KPD_INT_CLR);
683         __raw_writel(CFG_ROW_POLARITY | CFG_COL_POLARITY, KPD_POLARITY);
684         __raw_writel(1, KPD_CLK_DIV_CNT);
685         __raw_writel(0xc, KPD_LONG_KEY_CNT);
686         __raw_writel(0x5, KPD_DEBOUNCE_CNT);
687
688         sci_kpd->irq = platform_get_irq(pdev, 0);
689         if (sci_kpd->irq < 0) {
690                 error = -ENODEV;
691                 dev_err(&pdev->dev, "Get irq number error,Keypad Module\n");
692                 goto out2;
693         }
694         error =
695             request_irq(sci_kpd->irq, sci_keypad_isr, IRQF_NO_SUSPEND, "sci-keypad", sci_kpd);
696         if (error) {
697                 dev_err(&pdev->dev, "unable to claim irq %d\n", sci_kpd->irq);
698                 goto out2;
699         }
700 #ifndef CONFIG_OF
701         input_dev->name = pdev->name;
702 #else
703         input_dev->name = "sci-keypad";
704 #endif
705         input_dev->phys = "sci-key/input0";
706         input_dev->dev.parent = &pdev->dev;
707         input_set_drvdata(input_dev, sci_kpd);
708
709         input_dev->id.bustype = BUS_HOST;
710         input_dev->id.vendor = 0x0001;
711         input_dev->id.product = 0x0001;
712         input_dev->id.version = 0x0100;
713
714         input_dev->keycode = &sci_kpd[1];
715         input_dev->keycodesize = sizeof(unsigned short);
716         input_dev->keycodemax = keycodemax;
717
718         matrix_keypad_build_keymap(pdata->keymap_data, NULL, pdata->rows_number, pdata->cols_number,
719                                    input_dev->keycode, input_dev);
720
721         /* there are keys from hw other than keypad controller */
722         __set_bit(KEY_POWER, input_dev->keybit);
723         __set_bit(EV_KEY, input_dev->evbit);
724         if (pdata->repeat)
725                 __set_bit(EV_REP, input_dev->evbit);
726
727         error = input_register_device(input_dev);
728         if (error) {
729                 dev_err(&pdev->dev, "unable to register input device\n");
730                 goto out3;
731         }
732         device_init_wakeup(&pdev->dev, 1);
733
734         value = KPD_INT_DOWNUP;
735         if (pdata->support_long_key)
736                 value |= KPD_INT_LONG;
737         __raw_writel(value, KPD_INT_EN);
738         value = KPD_SLEEP_CNT_VALUE(1000);
739         __raw_writel(value, KPD_SLEEP_CNT);
740
741         if (__keypad_controller_ver() == 0) {
742                 if ((pdata->rows_choose_hw & ~KPDCTL_ROW_MSK_V0)
743                     || (pdata->cols_choose_hw & ~KPDCTL_COL_MSK_V0)) {
744                         pr_warn("Error rows_choose_hw Or cols_choose_hw\n");
745                 } else {
746                         pdata->rows_choose_hw &= KPDCTL_ROW_MSK_V0;
747                         pdata->cols_choose_hw &= KPDCTL_COL_MSK_V0;
748                 }
749         } else if (__keypad_controller_ver() == 1) {
750                 if ((pdata->rows_choose_hw & ~KPDCTL_ROW_MSK_V1)
751                     || (pdata->cols_choose_hw & ~KPDCTL_COL_MSK_V1)) {
752                         pr_warn("Error rows_choose_hw\n");
753                 } else {
754                         pdata->rows_choose_hw &= KPDCTL_ROW_MSK_V1;
755                         pdata->cols_choose_hw &= KPDCTL_COL_MSK_V1;
756                 }
757         } else {
758                 pr_warn
759                     ("This driver don't support this keypad controller version Now\n");
760         }
761         value =
762             KPD_EN | KPD_SLEEP_EN | pdata->
763             rows_choose_hw | pdata->cols_choose_hw;
764         if (pdata->support_long_key)
765                 value |= KPD_LONG_KEY_EN;
766         __raw_writel(value, KPD_CTRL);
767
768         gpio_request(PB_INT, "powerkey");
769         gpio_direction_input(PB_INT);
770         error = request_irq(gpio_to_irq(PB_INT), sci_powerkey_isr,
771                             IRQF_TRIGGER_HIGH | IRQF_NO_SUSPEND,
772                             "powerkey", sci_kpd);
773         if (error) {
774                 dev_err(&pdev->dev, "unable to claim irq %d\n",
775                         gpio_to_irq(PB_INT));
776                 goto out3;
777         }
778         /* sys fs */
779         key_dev = device_create(sec_class, NULL, 0, "%s", "sec_key");
780         if (device_create_file(key_dev, &dev_attr_sec_key_pressed) < 0)
781                 pr_err("Failed to create device file(%s)!\n", dev_attr_sec_key_pressed.attr.name);
782
783         powerkey_dev = device_create(sec_class, NULL, 0, NULL, "sec_powerkey");
784         if (sysfs_create_group(&powerkey_dev->kobj, &sec_powerkey_attr_group))
785                 pr_err("Failed to create sysfs group(%s)!\n", "sec_powerkey");
786         dev_set_drvdata(powerkey_dev, sci_kpd);
787
788         /* sys fs */
789         dump_keypad_register();
790         sprd_eic_button_sysfs_init();
791         return 0;
792
793         free_irq(gpio_to_irq(PB_INT), pdev);
794 out3:
795         input_free_device(input_dev);
796         free_irq(sci_kpd->irq, pdev);
797 out2:
798         platform_set_drvdata(pdev, NULL);
799 out1:
800         kfree(sci_kpd);
801 out0:
802         return error;
803 }
804
805 static int sci_keypad_remove(struct
806                                        platform_device
807                                        *pdev)
808 {
809         unsigned long value;
810         struct sci_keypad_t *sci_kpd = platform_get_drvdata(pdev);
811         struct sci_keypad_platform_data *pdata = pdev->dev.platform_data;
812         /* disable sci keypad controller */
813         __raw_writel(KPD_INT_ALL, KPD_INT_CLR);
814         value = __raw_readl(KPD_CTRL);
815         value &= ~(1 << 0);
816         __raw_writel(value, KPD_CTRL);
817
818         __keypad_disable();
819         wake_lock_destroy(&sci_kpd->key_wake_lock);
820         free_irq(sci_kpd->irq, pdev);
821         input_unregister_device(sci_kpd->input_dev);
822         kfree(sci_kpd);
823         platform_set_drvdata(pdev, NULL);
824         if(pdev->dev.of_node){
825                 kfree(pdata->keymap_data->keymap);
826                 kfree(pdata->keymap_data);
827                 kfree(pdata);
828         }
829         return 0;
830 }
831
832 #ifdef CONFIG_PM
833 static int sci_keypad_suspend(struct platform_device *dev, pm_message_t state)
834 {
835         return 0;
836 }
837
838 static int sci_keypad_resume(struct platform_device *dev)
839 {
840        struct sci_keypad_platform_data *pdata = dev->dev.platform_data;
841         unsigned long value;
842
843        __keypad_enable();
844         __raw_writel(KPD_INT_ALL, KPD_INT_CLR);
845         __raw_writel(CFG_ROW_POLARITY | CFG_COL_POLARITY, KPD_POLARITY);
846         __raw_writel(1, KPD_CLK_DIV_CNT);
847         __raw_writel(0xc, KPD_LONG_KEY_CNT);
848         __raw_writel(0x5, KPD_DEBOUNCE_CNT);
849
850         value = KPD_INT_DOWNUP;
851         if (pdata->support_long_key)
852                 value |= KPD_INT_LONG;
853         __raw_writel(value, KPD_INT_EN);
854         value = KPD_SLEEP_CNT_VALUE(1000);
855         __raw_writel(value, KPD_SLEEP_CNT);
856
857         value =
858             KPD_EN | KPD_SLEEP_EN | pdata->
859             rows_choose_hw | pdata->cols_choose_hw;
860         if (pdata->support_long_key)
861                 value |= KPD_LONG_KEY_EN;
862         __raw_writel(value, KPD_CTRL);
863
864         return 0;
865 }
866 #else
867 #define sci_keypad_suspend      NULL
868 #define sci_keypad_resume       NULL
869 #endif
870
871 static struct of_device_id keypad_match_table[] = {
872         { .compatible = "sprd,sci-keypad", },
873         { },
874 };
875 struct platform_driver sci_keypad_driver = {
876         .probe = sci_keypad_probe,
877         .remove = sci_keypad_remove,
878         .suspend = sci_keypad_suspend,
879         .resume = sci_keypad_resume,
880         .driver = {
881                    .name = "sci-keypad",.owner = THIS_MODULE,
882                    .of_match_table = keypad_match_table,
883                    },
884 };
885
886 static int __init sci_keypad_init(void)
887 {
888 #ifdef HOOK_POWER_KEY
889         input_hook_init();
890 #endif
891         return platform_driver_register(&sci_keypad_driver);
892 }
893
894 static void __exit sci_keypad_exit(void)
895 {
896 #ifdef HOOK_POWER_KEY
897         input_hook_exit();
898 #endif
899         platform_driver_unregister(&sci_keypad_driver);
900 }
901
902 module_init(sci_keypad_init);
903 module_exit(sci_keypad_exit);
904 MODULE_LICENSE("GPL");
905 MODULE_AUTHOR("spreadtrum.com");
906 MODULE_DESCRIPTION("Keypad driver for spreadtrum:questions contact steve zhan");
907 MODULE_ALIAS("platform:sci-keypad");