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