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