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