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