kernel:input: remotectl remove ir key struct to dts and handle ir irq not on cpu0
[firefly-linux-kernel-4.4.55.git] / drivers / input / remotectl / rockchip_pwm_remotectl.c
1 #include <linux/clk.h>
2 #include <linux/io.h>
3 #include <linux/module.h>
4 #include <linux/of.h>
5 #include <linux/platform_device.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/interrupt.h>
9 #include <linux/platform_device.h>
10 #include <linux/input.h>
11 #include <linux/workqueue.h>
12 #include <linux/wakelock.h>
13 #include <linux/slab.h>
14 #include "rockchip_pwm_remotectl.h"
15
16
17
18 /*sys/module/rk_pwm_remotectl/parameters,
19 modify code_print to change the value*/
20
21 static int rk_remote_print_code;
22 module_param_named(code_print, rk_remote_print_code, int, 0644);
23 #define DBG_CODE(args...) \
24         do { \
25                 if (rk_remote_print_code) { \
26                         pr_info(args); \
27                 } \
28         } while (0)
29
30 static int rk_remote_pwm_dbg_level;
31 module_param_named(dbg_level, rk_remote_pwm_dbg_level, int, 0644);
32 #define DBG(args...) \
33         do { \
34                 if (rk_remote_pwm_dbg_level) { \
35                         pr_info(args); \
36                 } \
37         } while (0)
38
39
40 struct rkxx_remote_key_table {
41         int scancode;
42         int keycode;
43 };
44
45 struct rkxx_remotectl_button {
46         int usercode;
47         int nbuttons;
48         struct rkxx_remote_key_table key_table[MAX_NUM_KEYS];
49 };
50
51 struct rkxx_remotectl_drvdata {
52         void __iomem *base;
53         int state;
54         int nbuttons;
55         int result;
56         int scandata;
57         int count;
58         int keynum;
59         int maxkeybdnum;
60         int keycode;
61         int press;
62         int pre_press;
63         int period;
64         int irq;
65         int remote_pwm_id;
66         int handle_cpu_id;
67         int wakeup;
68         struct input_dev *input;
69         struct timer_list timer;
70         struct tasklet_struct remote_tasklet;
71         struct wake_lock remotectl_wake_lock;
72 };
73
74 static struct rkxx_remotectl_button *remotectl_button; 
75
76
77 static int remotectl_keybd_num_lookup(struct rkxx_remotectl_drvdata *ddata)
78 {
79         int i;
80         int num;
81
82         num = ddata->maxkeybdnum;
83         for (i = 0; i < num; i++) {
84                 if (remotectl_button[i].usercode == (ddata->scandata&0xFFFF)) {
85                         ddata->keynum = i;
86                         return 1;
87                 }
88         }
89         return 0;
90 }
91
92
93 static int remotectl_keycode_lookup(struct rkxx_remotectl_drvdata *ddata)
94 {
95         int i;
96         unsigned char keydata = (unsigned char)((ddata->scandata >> 8) & 0xff);
97
98         for (i = 0; i < remotectl_button[ddata->keynum].nbuttons; i++) {
99                 if (remotectl_button[ddata->keynum].key_table[i].scancode ==
100                     keydata) {
101                         ddata->keycode =
102                         remotectl_button[ddata->keynum].key_table[i].keycode;
103                         return 1;
104                 }
105         }
106         return 0;
107 }
108
109
110 static int rk_remotectl_parse_ir_keys(struct platform_device *pdev)
111 {
112         struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
113         struct device_node *node = pdev->dev.of_node;
114         struct device_node *child_node;
115         u32 loop;
116         u32 ret;
117         u32 len;
118         u32 boardnum;
119         
120         boardnum = 0;
121         for_each_child_of_node(node, child_node) {
122                 if(of_property_read_u32(child_node, "rockchip,usercode",
123                          &remotectl_button[boardnum].usercode)) {
124                         dev_err(&pdev->dev, "Missing usercode property in the DTS.\n");
125                         ret = -1;
126                         return ret;
127                 }
128                 DBG("remotectl_button[0].usercode=0x%x\n",
129                                 remotectl_button[boardnum].usercode);
130                 of_get_property(child_node, "rockchip,key_table", &len);
131                 len /= sizeof(u32);
132                 DBG("len=0x%x\n",len);
133                 remotectl_button[boardnum].nbuttons = len/2;
134                 if(of_property_read_u32_array(child_node, "rockchip,key_table", 
135                          (u32 *)remotectl_button[boardnum].key_table, len)) {
136                         dev_err(&pdev->dev, "Missing key_table property in the DTS.\n");
137                         ret = -1;
138                         return ret;
139                 }
140                 for (loop=0; loop<(len/2); loop++) {
141                         DBG("board[%d].scanCode[%d]=0x%x\n", boardnum, loop, 
142                                         remotectl_button[boardnum].key_table[loop].scancode);
143                         DBG("board[%d].keyCode[%d]=%d\n", boardnum, loop, 
144                                         remotectl_button[boardnum].key_table[loop].keycode);
145                 }
146                 boardnum++;
147                 if (boardnum > ddata->maxkeybdnum)
148                         break;
149         }
150         DBG("keybdNum=0x%x\n",boardnum);
151         return 0;
152 }
153
154
155
156 static void rk_pwm_remotectl_do_something(unsigned long  data)
157 {
158         struct rkxx_remotectl_drvdata *ddata;
159
160         ddata = (struct rkxx_remotectl_drvdata *)data;
161         switch (ddata->state) {
162         case RMC_IDLE: {
163                 ;
164                 break;
165         }
166         case RMC_PRELOAD: {
167                 mod_timer(&ddata->timer, jiffies + msecs_to_jiffies(130));
168                 if ((RK_PWM_TIME_PRE_MIN < ddata->period) &&
169                     (ddata->period < RK_PWM_TIME_PRE_MAX)) {
170                         ddata->scandata = 0;
171                         ddata->count = 0;
172                         ddata->state = RMC_USERCODE;
173                 } else {
174                         ddata->state = RMC_PRELOAD;
175                 }
176                 break;
177         }
178         case RMC_USERCODE: {
179                 if ((RK_PWM_TIME_BIT1_MIN < ddata->period) &&
180                     (ddata->period < RK_PWM_TIME_BIT1_MAX))
181                         ddata->scandata |= (0x01 << ddata->count);
182                 ddata->count++;
183                 if (ddata->count == 0x10) {
184                         DBG_CODE("USERCODE=0x%x\n", ddata->scandata);
185                         if (remotectl_keybd_num_lookup(ddata)) {
186                                 ddata->state = RMC_GETDATA;
187                                 ddata->scandata = 0;
188                                 ddata->count = 0;
189                         } else {
190                                 ddata->state = RMC_PRELOAD;
191                         }
192                 }
193         }
194         break;
195         case RMC_GETDATA: {
196                 if ((RK_PWM_TIME_BIT1_MIN < ddata->period) &&
197                     (ddata->period < RK_PWM_TIME_BIT1_MAX))
198                         ddata->scandata |= (0x01<<ddata->count);
199                 ddata->count++;
200                 if (ddata->count < 0x10)
201                         return;
202                 DBG_CODE("RMC_GETDATA=%x\n", (ddata->scandata>>8));
203                 if ((ddata->scandata&0x0ff) ==
204                     ((~ddata->scandata >> 8) & 0x0ff)) {
205                         if (remotectl_keycode_lookup(ddata)) {
206                                 ddata->press = 1;
207                                 input_event(ddata->input, EV_KEY,
208                                             ddata->keycode, 1);
209                                 input_sync(ddata->input);
210                                 ddata->state = RMC_SEQUENCE;
211                         } else {
212                                 ddata->state = RMC_PRELOAD;
213                         }
214                 } else {
215                         ddata->state = RMC_PRELOAD;
216                 }
217         }
218         break;
219         case RMC_SEQUENCE:{
220                 DBG("S=%d\n", ddata->period);
221                 if ((RK_PWM_TIME_RPT_MIN < ddata->period) &&
222                     (ddata->period < RK_PWM_TIME_RPT_MAX)) {
223                         DBG("S1\n");
224                         mod_timer(&ddata->timer, jiffies
225                                   + msecs_to_jiffies(130));
226                 } else if ((RK_PWM_TIME_SEQ1_MIN < ddata->period) &&
227                            (ddata->period < RK_PWM_TIME_SEQ1_MAX)) {
228                         DBG("S2\n");
229                         mod_timer(&ddata->timer, jiffies
230                                   + msecs_to_jiffies(130));
231                 } else if ((RK_PWM_TIME_SEQ2_MIN < ddata->period) &&
232                           (ddata->period < RK_PWM_TIME_SEQ2_MAX)) {
233                         DBG("S3\n");
234                         mod_timer(&ddata->timer, jiffies
235                                   + msecs_to_jiffies(130));
236                 } else {
237                         DBG("S4\n");
238                         input_event(ddata->input, EV_KEY,
239                                     ddata->keycode, 0);
240                         input_sync(ddata->input);
241                         ddata->state = RMC_PRELOAD;
242                         ddata->press = 0;
243                 }
244         }
245         break;
246         default:
247         break;
248         }
249 }
250
251 static void rk_pwm_remotectl_timer(unsigned long _data)
252 {
253         struct rkxx_remotectl_drvdata *ddata;
254
255         ddata =  (struct rkxx_remotectl_drvdata *)_data;
256         if (ddata->press != ddata->pre_press) {
257                 ddata->pre_press = 0;
258                 ddata->press = 0;
259                 input_event(ddata->input, EV_KEY, ddata->keycode, 0);
260                 input_sync(ddata->input);
261         }
262         ddata->state = RMC_PRELOAD;
263 }
264
265
266 static irqreturn_t rockchip_pwm_irq(int irq, void *dev_id)
267 {
268         struct rkxx_remotectl_drvdata *ddata;
269         int val;
270
271         ddata = (struct rkxx_remotectl_drvdata *)dev_id;
272         switch (ddata->remote_pwm_id) {
273         case 0: {
274                 val = readl_relaxed(ddata->base + PWM0_REG_INTSTS);
275                 if (val & PWM_CH0_INT) {
276                         if ((val & PWM_CH0_POL) == 0) {
277                                 val = readl_relaxed(ddata->base + PWM_REG_HPR);
278                                 ddata->period = val;
279                                 tasklet_hi_schedule(&ddata->remote_tasklet);
280                                 DBG("hpr=0x%x\n", val);
281                         } else {
282                                 val = readl_relaxed(ddata->base + PWM_REG_LPR);
283                                 DBG("lpr=0x%x\n", val);
284                         }
285                         writel_relaxed(PWM_CH0_INT, ddata->base + PWM0_REG_INTSTS);
286                         if (ddata->state == RMC_PRELOAD)
287                                 wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
288                         return IRQ_HANDLED;
289                 }
290         }
291         break;
292         case 1: {
293                 val = readl_relaxed(ddata->base + PWM1_REG_INTSTS);
294                 if (val & PWM_CH1_INT) {
295                         if ((val & PWM_CH1_POL) == 0) {
296                                 val = readl_relaxed(ddata->base + PWM_REG_HPR);
297                                 ddata->period = val;
298                                 tasklet_hi_schedule(&ddata->remote_tasklet);
299                                 DBG("hpr=0x%x\n", val);
300                         } else {
301                                 val = readl_relaxed(ddata->base + PWM_REG_LPR);
302                                 DBG("lpr=0x%x\n", val);
303                         }
304                         writel_relaxed(PWM_CH1_INT, ddata->base + PWM1_REG_INTSTS);
305                         if (ddata->state == RMC_PRELOAD)
306                                 wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
307                         return IRQ_HANDLED;
308                 }
309         }
310         break;
311         case 2: {
312                 val = readl_relaxed(ddata->base + PWM2_REG_INTSTS);
313                 if (val & PWM_CH2_INT) {
314                         if ((val & PWM_CH2_POL) == 0) {
315                                 val = readl_relaxed(ddata->base + PWM_REG_HPR);
316                                 ddata->period = val;
317                                 tasklet_hi_schedule(&ddata->remote_tasklet);
318                                 DBG("hpr=0x%x\n", val);
319                         } else {
320                                 val = readl_relaxed(ddata->base + PWM_REG_LPR);
321                                 DBG("lpr=0x%x\n", val);
322                         }
323                         writel_relaxed(PWM_CH2_INT, ddata->base + PWM2_REG_INTSTS);
324                         if (ddata->state == RMC_PRELOAD)
325                                 wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
326                         return IRQ_HANDLED;
327                 }
328         }
329         break;
330         case 3: {
331                 val = readl_relaxed(ddata->base + PWM3_REG_INTSTS);
332                 if (val & PWM_CH3_INT) {
333                         if ((val & PWM_CH3_POL) == 0) {
334                                 val = readl_relaxed(ddata->base + PWM_REG_HPR);
335                                 ddata->period = val;
336                                 tasklet_hi_schedule(&ddata->remote_tasklet);
337                                 DBG("hpr=0x%x\n", val);
338                         } else {
339                                 val = readl_relaxed(ddata->base + PWM_REG_LPR);
340                                 DBG("lpr=0x%x\n", val);
341                         }
342                         writel_relaxed(PWM_CH3_INT, ddata->base + PWM3_REG_INTSTS);
343                         if (ddata->state == RMC_PRELOAD)
344                                 wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
345                         return IRQ_HANDLED;
346                 }
347         }
348         break;
349         default:        
350         break;
351         }               
352         return IRQ_NONE;
353 }
354
355 static int rk_pwm_remotectl_hw_init(struct rkxx_remotectl_drvdata *ddata)
356 {
357         int val;
358
359         val = readl_relaxed(ddata->base + PWM_REG_CTRL);
360         val = (val & 0xFFFFFFFE) | PWM_DISABLE;
361         writel_relaxed(val, ddata->base + PWM_REG_CTRL);
362         val = readl_relaxed(ddata->base + PWM_REG_CTRL);
363         val = (val & 0xFFFFFFF9) | PWM_MODE_CAPTURE;
364         writel_relaxed(val, ddata->base + PWM_REG_CTRL);
365         val = readl_relaxed(ddata->base + PWM_REG_CTRL);
366         val = (val & 0xFF008DFF) | 0x00646200;
367         writel_relaxed(val, ddata->base + PWM_REG_CTRL);
368         switch (ddata->remote_pwm_id) {
369         case 0: {
370                 val = readl_relaxed(ddata->base + PWM0_REG_INT_EN);
371                 val = (val & 0xFFFFFFFE) | PWM_CH0_INT_ENABLE;
372                 writel_relaxed(val, ddata->base + PWM0_REG_INT_EN);
373         }
374         break;
375         case 1: {
376                 val = readl_relaxed(ddata->base + PWM1_REG_INT_EN);
377                 val = (val & 0xFFFFFFFD) | PWM_CH1_INT_ENABLE;
378                 writel_relaxed(val, ddata->base + PWM1_REG_INT_EN);
379         }
380         break;
381         case 2: {
382                 val = readl_relaxed(ddata->base + PWM2_REG_INT_EN);
383                 val = (val & 0xFFFFFFFB) | PWM_CH2_INT_ENABLE;
384                 writel_relaxed(val, ddata->base + PWM2_REG_INT_EN);
385         }
386         break;
387         case 3: {
388                 val = readl_relaxed(ddata->base + PWM3_REG_INT_EN);
389                 val = (val & 0xFFFFFFF7) | PWM_CH3_INT_ENABLE;
390                 writel_relaxed(val, ddata->base + PWM3_REG_INT_EN);
391         }
392         break;
393         default:        
394         break;
395         }               
396         val = readl_relaxed(ddata->base + PWM_REG_CTRL);
397         val = (val & 0xFFFFFFFE) | PWM_ENABLE;
398         writel_relaxed(val, ddata->base + PWM_REG_CTRL);
399         return 0;
400 }
401
402
403
404 static int rk_pwm_probe(struct platform_device *pdev)
405 {
406         struct rkxx_remotectl_drvdata *ddata;
407         struct device_node *np = pdev->dev.of_node;
408         struct device_node *child_node;
409         struct resource *r;
410         struct input_dev *input;
411         struct clk *clk;
412         struct cpumask cpumask;
413         int num;
414         int irq;
415         int ret;
416         int i, j;
417         int cpu_id;
418         int pwm_id;
419         
420         pr_err(".. rk pwm remotectl v1.1 init\n");
421         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
422         if (!r) {
423                 dev_err(&pdev->dev, "no memory resources defined\n");
424                 return -ENODEV;
425         }
426         ddata = devm_kzalloc(&pdev->dev, sizeof(struct rkxx_remotectl_drvdata),
427                              GFP_KERNEL);
428         if (!ddata) {
429                 dev_err(&pdev->dev, "failed to allocate memory\n");
430                 return -ENOMEM;
431         }
432         ddata->state = RMC_PRELOAD;
433         ddata->base = devm_ioremap_resource(&pdev->dev, r);
434         if (IS_ERR(ddata->base))
435                 return PTR_ERR(ddata->base);
436         clk = devm_clk_get(&pdev->dev, "pclk_pwm");
437         if (IS_ERR(clk))
438                 return PTR_ERR(clk);
439         platform_set_drvdata(pdev, ddata);
440         num = 0;
441         for_each_child_of_node(np, child_node){
442                 if(of_device_is_compatible(child_node, "rockchip,ir_key"))
443                         num++;
444         }
445         if (num == 0) {
446                 pr_err("remotectl: no ir keyboard add in dts!!\n");
447                 return -1;
448         }
449         ddata->maxkeybdnum = num;
450         remotectl_button = kmalloc(
451                                         num*sizeof(struct rkxx_remotectl_button),
452                                         GFP_KERNEL);
453         if (!remotectl_button) {
454                 pr_err("failed to malloc remote button memory\n");
455                 return -ENOMEM;
456         }
457         input = input_allocate_device();
458         input->name = pdev->name;
459         input->phys = "gpio-keys/remotectl";
460         input->dev.parent = &pdev->dev;
461         input->id.bustype = BUS_HOST;
462         input->id.vendor = 0x0001;
463         input->id.product = 0x0001;
464         input->id.version = 0x0100;
465         ddata->input = input;
466         ddata->input = input;
467         wake_lock_init(&ddata->remotectl_wake_lock,
468                        WAKE_LOCK_SUSPEND, "rk29_pwm_remote");
469         ret = clk_prepare_enable(clk);
470         if (ret)
471                 return ret;
472         irq = platform_get_irq(pdev, 0);
473         if (ret < 0) {
474                 dev_err(&pdev->dev, "cannot find IRQ\n");
475                 return ret;
476         }
477         ddata->irq = irq;
478         ddata->wakeup = 1;
479         of_property_read_u32(np, "remote_pwm_id", &pwm_id);
480         ddata->remote_pwm_id = pwm_id;
481         DBG("remotectl: remote pwm id=0x%x\n", pwm_id);
482         of_property_read_u32(np, "handle_cpu_id", &cpu_id);
483         ddata->handle_cpu_id = cpu_id;
484         DBG("remotectl: handle cpu id=0x%x\n", cpu_id);
485         rk_remotectl_parse_ir_keys(pdev);
486         tasklet_init(&ddata->remote_tasklet, rk_pwm_remotectl_do_something,
487                      (unsigned long)ddata);
488         for (j = 0; j < num; j++) {
489                 DBG("remotectl probe j = 0x%x\n", j);
490                 for (i = 0; i < remotectl_button[j].nbuttons; i++) {
491                         unsigned int type = EV_KEY;
492
493                         input_set_capability(input, type, remotectl_button[j].
494                                              key_table[i].keycode);
495                 }
496         }
497         ret = input_register_device(input);
498         if (ret)
499                 pr_err("remotectl: register input device err, ret: %d\n", ret);
500         input_set_capability(input, EV_KEY, KEY_WAKEUP);
501         device_init_wakeup(&pdev->dev, 1);
502         ret = devm_request_irq(&pdev->dev, irq, rockchip_pwm_irq,
503                                0, "rk_pwm_irq", ddata);
504         if (ret) {
505                 dev_err(&pdev->dev, "cannot claim IRQ %d\n", irq);
506                 return ret;
507         }
508         enable_irq_wake(irq);
509         setup_timer(&ddata->timer, rk_pwm_remotectl_timer,
510                     (unsigned long)ddata);
511         mod_timer(&ddata->timer, jiffies + msecs_to_jiffies(1000));
512         cpumask_clear(&cpumask);
513         cpumask_set_cpu(cpu_id, &cpumask); 
514         irq_set_affinity(irq, &cpumask); 
515         rk_pwm_remotectl_hw_init(ddata);
516         return ret;
517 }
518
519 static int rk_pwm_remove(struct platform_device *pdev)
520 {
521         return 0;
522 }
523
524 #ifdef CONFIG_PM
525 static int remotectl_suspend(struct device *dev)
526 {
527         int cpu = 0;
528         struct cpumask cpumask;
529         struct platform_device *pdev = to_platform_device(dev);
530         struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
531         
532         cpumask_clear(&cpumask);
533         cpumask_set_cpu(cpu, &cpumask);
534         irq_set_affinity(ddata->irq, &cpumask);
535         return 0;
536 }
537
538
539 static int remotectl_resume(struct device *dev)
540 {
541         struct cpumask cpumask;
542         struct platform_device *pdev = to_platform_device(dev);
543         struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
544         
545         cpumask_clear(&cpumask);
546         cpumask_set_cpu(ddata->handle_cpu_id, &cpumask); 
547         irq_set_affinity(ddata->irq, &cpumask); 
548         return 0;
549 }
550
551 static const struct dev_pm_ops remotectl_pm_ops = {
552         .suspend = remotectl_suspend,
553         .resume = remotectl_resume,
554 };
555 #endif
556
557 static const struct of_device_id rk_pwm_of_match[] = {
558         { .compatible =  "rockchip,remotectl-pwm"},
559         { }
560 };
561
562 MODULE_DEVICE_TABLE(of, rk_pwm_of_match);
563
564 static struct platform_driver rk_pwm_driver = {
565         .driver = {
566                 .name = "remotectl-pwm",
567                 .of_match_table = rk_pwm_of_match,
568 #ifdef CONFIG_PM
569         .pm     = &remotectl_pm_ops,
570 #endif  
571         },
572         .probe = rk_pwm_probe,
573         .remove = rk_pwm_remove,
574 };
575
576 module_platform_driver(rk_pwm_driver);
577
578 MODULE_LICENSE("GPL");