-
-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathrockchip_pwm_remotectl.c
More file actions
1098 lines (993 loc) · 28.8 KB
/
Copy pathrockchip_pwm_remotectl.c
File metadata and controls
1098 lines (993 loc) · 28.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/irqdesc.h>
#include <linux/irqdomain.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/wakelock.h>
#include <linux/slab.h>
#include <linux/rockchip/rockchip_sip.h>
#include "rockchip_pwm_remotectl.h"
/*
* sys/module/rk_pwm_remotectl/parameters,
* modify code_print to change the value
*/
static int rk_remote_print_code;
module_param_named(code_print, rk_remote_print_code, int, 0644);
#define DBG_CODE(args...) \
do { \
if (rk_remote_print_code) { \
pr_info(args); \
} \
} while (0)
static int rk_remote_pwm_dbg_level;
module_param_named(dbg_level, rk_remote_pwm_dbg_level, int, 0644);
#define DBG(args...) \
do { \
if (rk_remote_pwm_dbg_level) { \
pr_info(args); \
} \
} while (0)
struct rkxx_remote_key_table {
int scancode;
int keycode;
};
struct rkxx_remotectl_button {
int usercode;
int nbuttons;
struct rkxx_remote_key_table key_table[MAX_NUM_KEYS];
};
struct rk_remote_pwm_regs {
unsigned long ctrl;
unsigned long version;
unsigned long enable;
unsigned long clk_ctrl;
unsigned long offset;
unsigned long rpt;
unsigned long hpr;
unsigned long lpr;
unsigned long intsts;
unsigned long int_en;
unsigned long pwrmatch_arbiter;
unsigned long pwrmatch_ctrl;
unsigned long pwrmatch_lpre;
unsigned long pwrmatch_hpre;
unsigned long pwrmatch_ld;
unsigned long pwrmatch_hd_zero;
unsigned long pwrmatch_hd_one;
unsigned long pwrmatch_value0;
unsigned long pwrcapture_value;
};
struct rk_remote_pwm_funcs {
irqreturn_t (*irq_handler)(int irq, void *data);
void (*int_ctrl)(struct platform_device *pdev, int work_mode);
int (*init_hw)(struct platform_device *pdev);
};
struct rk_remote_pwm_data {
struct rk_remote_pwm_regs regs;
struct rk_remote_pwm_funcs funcs;
unsigned int prescaler;
u32 enable_conf;
int pwm_version;
int pwrmactch_key_max;
};
struct rkxx_remotectl_drvdata {
void __iomem *base;
int pwm_version;
int state;
int nbuttons;
int scandata;
int count;
int keynum;
int maxkeybdnum;
int keycode;
int press;
int pre_press;
int irq;
int remote_pwm_id;
int handle_cpu_id;
int wakeup;
int support_psci;
int pwm_pwrkey_capture;
unsigned long period;
unsigned long temp_period;
int pwm_freq_nstime;
int pwrkey_wakeup;
struct input_dev *input;
struct timer_list timer;
struct tasklet_struct remote_tasklet;
struct wake_lock remotectl_wake_lock;
const struct rk_remote_pwm_data *pwm_data;
};
static irqreturn_t rockchip_pwm_irq(int irq, void *dev_id);
static irqreturn_t rockchip_pwm_irq_v4(int irq, void *dev_id);
static int rk_pwm_remotectl_hw_init(struct platform_device *pdev);
static int rk_pwm_remotectl_hw_init_v4(struct platform_device *pdev);
static void rk_pwm_int_ctrl(struct platform_device *pdev, int work_mode);
static void rk_pwm_int_ctrl_v4(struct platform_device *pdev, int work_mode);
static struct rkxx_remotectl_button *remotectl_button;
struct rk_remote_pwm_data pwm_data_v1 = {
.regs = {
.version = 0x5c,
.ctrl = 0x0c,
},
.prescaler = 1,
.pwm_version = 1,
.funcs = {
.irq_handler = rockchip_pwm_irq,
.int_ctrl = rk_pwm_int_ctrl,
.init_hw = rk_pwm_remotectl_hw_init,
},
};
struct rk_remote_pwm_data pwm_data_v4 = {
.regs = {
.version = 0x0,
.enable = 0x4,
.clk_ctrl = 0x8,
.ctrl = 0xc,
.hpr = 0x2c,
.lpr = 0x30,
.intsts = 0x70,
.int_en = 0x74,
.pwrmatch_arbiter = 0x100,
.pwrmatch_ctrl = 0x104,
.pwrmatch_lpre = 0x108,
.pwrmatch_hpre = 0x10c,
.pwrmatch_ld = 0x110,
.pwrmatch_hd_zero = 0x114,
.pwrmatch_hd_one = 0x118,
.pwrmatch_value0 = 0x11c,
.pwrcapture_value = 0x15c,
},
.pwm_version = 4,
.funcs = {
.irq_handler = rockchip_pwm_irq_v4,
.int_ctrl = rk_pwm_int_ctrl_v4,
.init_hw = rk_pwm_remotectl_hw_init_v4,
},
};
static int remotectl_keybd_num_lookup(struct rkxx_remotectl_drvdata *ddata)
{
int i;
int num;
num = ddata->maxkeybdnum;
for (i = 0; i < num; i++) {
if (remotectl_button[i].usercode == (ddata->scandata&0xFFFF)) {
ddata->keynum = i;
return 1;
}
}
return 0;
}
static int remotectl_keycode_lookup(struct rkxx_remotectl_drvdata *ddata)
{
int i;
unsigned char keydata = (unsigned char)((ddata->scandata >> 8) & 0xff);
for (i = 0; i < remotectl_button[ddata->keynum].nbuttons; i++) {
if (remotectl_button[ddata->keynum].key_table[i].scancode ==
keydata) {
ddata->keycode =
remotectl_button[ddata->keynum].key_table[i].keycode;
return 1;
}
}
return 0;
}
static int rk_remotectl_get_irkeybd_count(struct platform_device *pdev)
{
struct device_node *node = pdev->dev.of_node;
struct device_node *child_node;
int boardnum;
int temp_usercode;
boardnum = 0;
for_each_child_of_node(node, child_node) {
if (of_property_read_u32(child_node, "rockchip,usercode",
&temp_usercode)) {
DBG("get keybd num error.\n");
} else {
boardnum++;
}
}
DBG("get keybd num = 0x%x.\n", boardnum);
return boardnum;
}
static int rk_remotectl_parse_ir_keys(struct platform_device *pdev)
{
struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
struct device_node *node = pdev->dev.of_node;
struct device_node *child_node;
int loop;
int ret;
int len;
int boardnum;
boardnum = 0;
for_each_child_of_node(node, child_node) {
if (of_property_read_u32(child_node, "rockchip,usercode",
&remotectl_button[boardnum].usercode)) {
dev_err(&pdev->dev, "Missing usercode in the DTS.\n");
ret = -1;
return ret;
}
DBG("remotectl_button[0].usercode=0x%x\n",
remotectl_button[boardnum].usercode);
of_get_property(child_node, "rockchip,key_table", &len);
len /= sizeof(u32);
DBG("len=0x%x\n", len);
remotectl_button[boardnum].nbuttons = len/2;
if (of_property_read_u32_array(child_node, "rockchip,key_table",
(u32 *)remotectl_button[boardnum].key_table, len)) {
dev_err(&pdev->dev, "Missing key_table in the DTS.\n");
ret = -1;
return ret;
}
for (loop = 0; loop < (len/2); loop++) {
DBG("board[%d].scanCode[%d]=0x%x\n", boardnum, loop,
remotectl_button[boardnum].key_table[loop].scancode);
DBG("board[%d].keyCode[%d]=%d\n", boardnum, loop,
remotectl_button[boardnum].key_table[loop].keycode);
}
boardnum++;
if (boardnum > ddata->maxkeybdnum)
break;
}
DBG("keybdNum=0x%x\n", boardnum);
return 0;
}
static void rk_pwm_remotectl_do_something(unsigned long data)
{
struct rkxx_remotectl_drvdata *ddata;
ddata = (struct rkxx_remotectl_drvdata *)data;
switch (ddata->state) {
case RMC_IDLE: {
;
break;
}
case RMC_PRELOAD: {
mod_timer(&ddata->timer, jiffies + msecs_to_jiffies(140));
if ((ddata->period > RK_PWM_TIME_PRE_MIN) &&
(ddata->period < RK_PWM_TIME_PRE_MAX)) {
ddata->scandata = 0;
ddata->count = 0;
ddata->state = RMC_USERCODE;
} else {
ddata->state = RMC_PRELOAD;
}
break;
}
case RMC_USERCODE: {
if ((ddata->period > RK_PWM_TIME_BIT1_MIN) &&
(ddata->period < RK_PWM_TIME_BIT1_MAX))
ddata->scandata |= (0x01 << ddata->count);
ddata->count++;
if (ddata->count == 0x10) {
DBG_CODE("USERCODE=0x%x\n", ddata->scandata);
if (remotectl_keybd_num_lookup(ddata)) {
ddata->state = RMC_GETDATA;
ddata->scandata = 0;
ddata->count = 0;
} else {
if (rk_remote_print_code) {
ddata->state = RMC_GETDATA;
ddata->scandata = 0;
ddata->count = 0;
} else
ddata->state = RMC_PRELOAD;
}
}
}
break;
case RMC_GETDATA: {
if ((ddata->period > RK_PWM_TIME_BIT1_MIN) &&
(ddata->period < RK_PWM_TIME_BIT1_MAX))
ddata->scandata |= (0x01<<ddata->count);
ddata->count++;
if (ddata->count < 0x10)
return;
DBG_CODE("RMC_GETDATA=%x\n", (ddata->scandata>>8));
if ((ddata->scandata&0x0ff) ==
((~ddata->scandata >> 8) & 0x0ff)) {
if (remotectl_keycode_lookup(ddata)) {
ddata->press = 1;
input_event(ddata->input, EV_KEY,
ddata->keycode, 1);
input_sync(ddata->input);
ddata->state = RMC_SEQUENCE;
} else {
ddata->state = RMC_PRELOAD;
}
} else {
ddata->state = RMC_PRELOAD;
}
}
break;
case RMC_SEQUENCE:{
DBG("S=%ld\n", ddata->period);
if ((ddata->period > RK_PWM_TIME_RPT_MIN) &&
(ddata->period < RK_PWM_TIME_RPT_MAX)) {
DBG("S1\n");
mod_timer(&ddata->timer, jiffies
+ msecs_to_jiffies(130));
} else if ((ddata->period > RK_PWM_TIME_SEQ1_MIN) &&
(ddata->period < RK_PWM_TIME_SEQ1_MAX)) {
DBG("S2\n");
mod_timer(&ddata->timer, jiffies
+ msecs_to_jiffies(130));
} else if ((ddata->period > RK_PWM_TIME_SEQ2_MIN) &&
(ddata->period < RK_PWM_TIME_SEQ2_MAX)) {
DBG("S3\n");
mod_timer(&ddata->timer, jiffies
+ msecs_to_jiffies(130));
} else {
DBG("S4\n");
input_event(ddata->input, EV_KEY,
ddata->keycode, 0);
input_sync(ddata->input);
ddata->state = RMC_PRELOAD;
ddata->press = 0;
}
}
break;
default:
break;
}
}
static void rk_pwm_remotectl_timer(struct timer_list *t)
{
struct rkxx_remotectl_drvdata *ddata = from_timer(ddata, t, timer);
if (ddata->press != ddata->pre_press) {
ddata->pre_press = 0;
ddata->press = 0;
input_event(ddata->input, EV_KEY, ddata->keycode, 0);
input_sync(ddata->input);
}
ddata->state = RMC_PRELOAD;
}
static irqreturn_t rockchip_pwm_pwrirq(int irq, void *dev_id)
{
struct rkxx_remotectl_drvdata *ddata = dev_id;
int val;
unsigned int id = ddata->remote_pwm_id;
if (id > 3)
return IRQ_NONE;
val = readl_relaxed(ddata->base + PWM_REG_INTSTS(id));
if (val & PWM_PWR_KEY_INT) {
DBG("pwr=0x%x\n", readl_relaxed(ddata->base + PWM_PWRCAPTURE_VALUE(id)));
writel_relaxed(PWM_PWR_KEY_INT, ddata->base + PWM_REG_INTSTS(id));
ddata->pwrkey_wakeup = 1;
return IRQ_HANDLED;
}
return IRQ_NONE;
}
static irqreturn_t rockchip_pwm_irq(int irq, void *dev_id)
{
struct rkxx_remotectl_drvdata *ddata = dev_id;
int val;
int temp_hpr;
int temp_lpr;
int temp_period;
unsigned int id = ddata->remote_pwm_id;
if (id > 3)
return IRQ_NONE;
val = readl_relaxed(ddata->base + PWM_REG_INTSTS(id));
if ((val & PWM_CH_INT(id)) == 0)
return IRQ_NONE;
if ((val & PWM_CH_POL(id)) == 0) {
temp_hpr = readl_relaxed(ddata->base + PWM_REG_HPR);
writel_relaxed(0, ddata->base + PWM_REG_HPR);
temp_lpr = readl_relaxed(ddata->base + PWM_REG_LPR);
writel_relaxed(0, ddata->base + PWM_REG_LPR);
DBG("hpr=%d\n", temp_hpr);
DBG("lpr=%d\n", temp_lpr);
temp_period = ddata->pwm_freq_nstime * temp_lpr / 1000;
if (temp_period > RK_PWM_TIME_BIT0_MIN) {
ddata->period = ddata->temp_period
+ ddata->pwm_freq_nstime * temp_hpr / 1000;
tasklet_hi_schedule(&ddata->remote_tasklet);
ddata->temp_period = 0;
DBG("period+ =%ld\n", ddata->period);
} else {
ddata->temp_period += ddata->pwm_freq_nstime
* (temp_hpr + temp_lpr) / 1000;
}
}
writel_relaxed(PWM_CH_INT(id), ddata->base + PWM_REG_INTSTS(id));
if (ddata->state == RMC_PRELOAD)
wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
return IRQ_HANDLED;
}
static irqreturn_t rockchip_pwm_irq_v4(int irq, void *dev_id)
{
struct rkxx_remotectl_drvdata *ddata = dev_id;
int tmp;
int val;
irqreturn_t ret = IRQ_NONE;
val = readl_relaxed(ddata->base + PWM_REG_INTSTS_V4);
if (val & CAP_LPR_INT) {
writel_relaxed(CAP_LPR_INT, ddata->base + PWM_REG_INTSTS_V4);
tmp = readl_relaxed(ddata->base + PWM_REG_HPR_V4);
ddata->period = ddata->pwm_freq_nstime * tmp / 1000;
DBG("period = %ld\n", ddata->period);
tasklet_hi_schedule(&ddata->remote_tasklet);
ret = IRQ_HANDLED;
} else if (val & CAP_HPR_INT) {
writel_relaxed(CAP_HPR_INT, ddata->base + PWM_REG_INTSTS_V4);
tmp = readl_relaxed(ddata->base + PWM_REG_LPR_V4);
DBG("lpr = %d\n", tmp);
ret = IRQ_HANDLED;
}
if (val & PWR_INT) {
writel_relaxed(PWR_INT, ddata->base + PWM_REG_INTSTS_V4);
tmp = readl_relaxed(ddata->base + PWM_REG_CAPTURE_VALUE_V4);
DBG("##### cap_val = 0x%0x\n", tmp);
ddata->pwrkey_wakeup = 1;
ret = IRQ_HANDLED;
}
if (ddata->state == RMC_PRELOAD)
wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
return ret;
}
static int rk_pwmkey_convert_val(int min, int max, int freq_nstime)
{
int val, min_temp, max_temp;
min_temp = min * 1000 / freq_nstime;
max_temp = max * 1000 / freq_nstime;
val = (max_temp & 0xffff) << 16 | (min_temp & 0xffff);
return val;
}
static int rockchip_pwm_set_pwrmatch(struct platform_device *pdev)
{
struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
unsigned int pwm_id = ddata->remote_pwm_id;
int version;
int pwr_irq;
int val;
int ret;
version = readl_relaxed(ddata->base + RK_PWM_VERSION_ID(pwm_id));
DBG("remote pwm version is 0x%x\n", version);
if (((version >> 24) & 0xFF) < 2) {
dev_info(&pdev->dev, "pwm version is less v2.0\n");
ret = -EINVAL;
goto end;
}
pwr_irq = platform_get_irq(pdev, 1);
if (pwr_irq < 0) {
dev_err(&pdev->dev, "cannot find PWR IRQ\n");
ret = pwr_irq;
goto end;
}
ret = devm_request_irq(&pdev->dev, pwr_irq, rockchip_pwm_pwrirq,
IRQF_NO_SUSPEND, "rk_pwm_pwr_irq", ddata);
if (ret) {
dev_err(&pdev->dev, "cannot claim PWR_IRQ!!!\n");
goto end;
}
val = readl_relaxed(ddata->base + PWM_REG_INT_EN(pwm_id));
val = (val & 0xFFFFFF7F) | PWM_PWR_INT_ENABLE;
writel_relaxed(val, ddata->base + PWM_REG_INT_EN(pwm_id));
val = CH3_PWRKEY_ENABLE;
writel_relaxed(val, ddata->base + PWM_REG_PWRMATCH_CTRL(pwm_id));
val = readl_relaxed(ddata->base + PWM_REG_CTRL);
val = (val & 0xFFFFFFFE) | PWM_ENABLE;
writel_relaxed(val, ddata->base + PWM_REG_CTRL);
return 0;
end:
return ret;
}
static int rockchip_pwm_set_pwrmatch_v4(struct rkxx_remotectl_drvdata *pd)
{
int version;
int channel_id;
int val;
version = readl_relaxed(pd->base + PWM_REG_VERSION_V4);
DBG("remote pwm version is 0x%x\n", version);
channel_id = (version & CHANNLE_INDEX_MASK) >> CHANNLE_INDEX_SHIFT;
val = BIT(channel_id) << PWRMATCH_READ_LOCK_SHIFT |
BIT(channel_id) << PWRMATCH_GRANT_SHIFT;
writel_relaxed(val, pd->base + PWM_REG_MATCH_ARBITER_V4);
writel_relaxed(PWRKEY_EN(true),
pd->base + PWM_REG_MATCH_CTRL_V4);
return 0;
}
static void rockchip_pwrmatch_set_nec_param(struct rkxx_remotectl_drvdata *pd)
{
unsigned int pwm_id = pd->remote_pwm_id;
void __iomem *reg;
int freq_nstime;
int val;
freq_nstime = pd->pwm_freq_nstime;
//preloader low min:8000us, max:10000us
val = rk_pwmkey_convert_val(RK_PWM_TIME_PRE_MIN_LOW,
RK_PWM_TIME_PRE_MAX_LOW, freq_nstime);
if (pd->pwm_data->pwm_version < 4)
reg = pd->base + PWM_REG_PWRMATCH_LPRE(pwm_id);
else
reg = pd->base + PWM_REG_MATCH_LPRE_V4;
writel_relaxed(val, reg);
//preloader higt min:4000us, max:5000us
val = rk_pwmkey_convert_val(RK_PWM_TIME_PRE_MIN,
RK_PWM_TIME_PRE_MAX, freq_nstime);
if (pd->pwm_data->pwm_version < 4)
reg = pd->base + PWM_REG_PWRMATCH_HPRE(pwm_id);
else
reg = pd->base + PWM_REG_MATCH_HPRE_V4;
writel_relaxed(val, reg);
//logic 0/1 low min:480us, max 700us
val = rk_pwmkey_convert_val(RK_PWM_TIME_BIT_MIN_LOW,
RK_PWM_TIME_BIT_MAX_LOW, freq_nstime);
if (pd->pwm_data->pwm_version < 4)
reg = pd->base + PWM_REG_PWRMATCH_LD(pwm_id);
else
reg = pd->base + PWM_REG_MATCH_LD_V4;
writel_relaxed(val, reg);
//logic 0 higt min:480us, max 700us
val = rk_pwmkey_convert_val(RK_PWM_TIME_BIT0_MIN,
RK_PWM_TIME_BIT0_MAX, freq_nstime);
if (pd->pwm_data->pwm_version < 4)
reg = pd->base + PWM_REG_PWRMATCH_HD_ZERO(pwm_id);
else
reg = pd->base + PWM_REG_MATCH_HD_ZERO_V4;
writel_relaxed(val, reg);
//logic 1 higt min:1300us, max 2000us
val = rk_pwmkey_convert_val(RK_PWM_TIME_BIT1_MIN,
RK_PWM_TIME_BIT1_MAX, freq_nstime);
if (pd->pwm_data->pwm_version < 4)
reg = pd->base + PWM_REG_PWRMATCH_HD_ONE(pwm_id);
else
reg = pd->base + PWM_REG_MATCH_HD_ONE_V4;
writel_relaxed(val, reg);
}
static void rockchip_pwrmatch_set_pwrkey(struct rkxx_remotectl_drvdata *pd)
{
unsigned int pwm_id = pd->remote_pwm_id;
void __iomem *reg;
int i, j;
int num = 0, num_max;
if (pd->pwm_data->pwm_version < 4) {
reg = pd->base + PWM_PWRMATCH_VALUE(pwm_id);
num_max = PWM_PWR_KEY_CAPURURE_MAX;
} else {
reg = pd->base + PWM_REG_CAPTURE_VALUE0_V4;
num_max = PWM_PWR_KEY_CAPURURE_MAX_V4;
}
for (j = 0; j < pd->maxkeybdnum; j++) {
for (i = 0; i < remotectl_button[j].nbuttons; i++) {
int scancode, usercode, pwrkey;
if (remotectl_button[j].key_table[i].keycode != KEY_POWER)
continue;
usercode = remotectl_button[j].usercode & 0xffff;
scancode = remotectl_button[j].key_table[i].scancode & 0xff;
DBG("usercode=%x, key=%x\n", usercode, scancode);
pwrkey = usercode;
pwrkey |= (scancode << 24) | ((~scancode & 0xff) << 16);
DBG("pwrkey = %x\n", pwrkey);
writel_relaxed(pwrkey, reg + num * 4);
num++;
if (num >= num_max)
break;
}
}
}
static int rk_pwm_pwrkey_wakeup_init(struct platform_device *pdev)
{
struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
int ret = -1;
ddata->pwm_pwrkey_capture = 0;
if (ddata->pwm_data->pwm_version < 4) {
ret = rockchip_pwm_set_pwrmatch(pdev);
if (ret < 0)
goto end;
} else {
rockchip_pwm_set_pwrmatch_v4(ddata);
}
rockchip_pwrmatch_set_nec_param(ddata);
rockchip_pwrmatch_set_pwrkey(ddata);
ddata->pwm_pwrkey_capture = 1;
end:
return ret;
}
static void rk_pwm_int_ctrl(struct platform_device *pdev, int work_mode)
{
struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
int val;
int pwm_id = ddata->remote_pwm_id;
if (pwm_id > 3)
return;
val = readl_relaxed(ddata->base + PWM_REG_INT_EN(pwm_id));
if (work_mode) {
val |= PWM_CH_INT_ENABLE(pwm_id);
DBG("pwm int enabled, value is 0x%x\n", val);
writel_relaxed(val, ddata->base + PWM_REG_INT_EN(pwm_id));
} else {
val &= ~PWM_CH_INT_ENABLE(pwm_id);
DBG("pwm int disabled, value is 0x%x\n", val);
}
writel_relaxed(val, ddata->base + PWM_REG_INT_EN(pwm_id));
}
static void rk_pwm_int_ctrl_v4(struct platform_device *pdev, int work_mode)
{
struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
int val;
if (work_mode) {
val = CAP_LPR_INT_EN(true) | CAP_HPR_INT_EN(true) | PWR_INT_EN(false);
writel_relaxed(val, ddata->base + PWM_REG_INT_EN_V4);
} else {
val = CAP_LPR_INT_EN(false) | CAP_HPR_INT_EN(false) | PWR_INT_EN(true);
writel_relaxed(val, ddata->base + PWM_REG_INT_EN_V4);
}
}
static int rk_pwm_remotectl_hw_init(struct platform_device *pdev)
{
int val;
struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
//1. disabled pwm
val = readl_relaxed(ddata->base + PWM_REG_CTRL);
val = (val & 0xFFFFFFFE) | PWM_DISABLE;
writel_relaxed(val, ddata->base + PWM_REG_CTRL);
//2. capture mode
val = readl_relaxed(ddata->base + PWM_REG_CTRL);
val = (val & 0xFFFFFFF9) | PWM_MODE_CAPTURE;
writel_relaxed(val, ddata->base + PWM_REG_CTRL);
//set clk div, clk div to 64
val = readl_relaxed(ddata->base + PWM_REG_CTRL);
val = (val & 0xFF0001FF) | PWM_DIV64;
writel_relaxed(val, ddata->base + PWM_REG_CTRL);
//4. enabled pwm int
ddata->pwm_data->funcs.int_ctrl(pdev, IR_WORK_MODE);
//5. enabled pwm
val = readl_relaxed(ddata->base + PWM_REG_CTRL);
val = (val & 0xFFFFFFFE) | PWM_ENABLE;
writel_relaxed(val, ddata->base + PWM_REG_CTRL);
return 0;
}
static int rk_pwm_remotectl_hw_init_v4(struct platform_device *pdev)
{
int val;
struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
//1. disabled pwm
val = PWM_EN(false) | PWM_CLK_EN(false);
writel_relaxed(val, ddata->base + PWM_REG_ENABLE_V4);
//2. capture mode
val = PWM_MODE(CAPTURE_MODE);
writel_relaxed(val, ddata->base + PWM_REG_CTRL_V4);
//3. set clk div, clk div to 64
val = CLK_SCALE(32);
writel_relaxed(val, ddata->base + PWM_REG_CLK_CTRL_V4);
//4. enabled pwm int
ddata->pwm_data->funcs.int_ctrl(pdev, IR_WORK_MODE);
//5. enabled pwm
val = PWM_EN(true) | PWM_CLK_EN(true);
writel_relaxed(val, ddata->base + PWM_REG_ENABLE_V4);
return 0;
}
static int rk_pwm_sip_wakeup_init(struct platform_device *pdev)
{
struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
struct device_node *np = pdev->dev.of_node;
struct irq_desc *desc;
int support_psci = 0;
int irq;
int hwirq;
int i, j;
int num;
int pwm_id;
int ret = -1;
if (of_property_read_u32(np, "remote_support_psci", &support_psci)) {
dev_info(&pdev->dev, "PWM Missing psci property in the DT.\n");
goto end;
}
DBG("support_psci=0x%x\n", support_psci);
if (!support_psci)
goto end;
irq = ddata->irq;
desc = irq_to_desc(irq);
if (!desc || !desc->irq_data.domain)
goto end;
hwirq = desc->irq_data.hwirq;
ret = sip_smc_remotectl_config(REMOTECTL_SET_IRQ, hwirq);
if (ret) {
dev_err(&pdev->dev, "set irq err, set support_psci to 0 !!\n");
/*
* if atf doesn't support, return probe success to abandon atf
* function and still use kernel pwm parse function
*/
goto end;
}
pwm_id = ddata->remote_pwm_id;
num = ddata->maxkeybdnum;
sip_smc_remotectl_config(REMOTECTL_SET_PWM_CH, pwm_id);
for (j = 0; j < num; j++) {
for (i = 0; i < remotectl_button[j].nbuttons; i++) {
int scancode, pwrkey;
if (remotectl_button[j].key_table[i].keycode
!= KEY_POWER)
continue;
scancode = remotectl_button[j].key_table[i].scancode;
DBG("usercode=%x, key=%x\n",
remotectl_button[j].usercode, scancode);
pwrkey = (remotectl_button[j].usercode & 0xffff) << 16;
pwrkey |= (scancode & 0xff) << 8;
DBG("deliver: key=%x\n", pwrkey);
sip_smc_remotectl_config(REMOTECTL_SET_PWRKEY,
pwrkey);
}
}
sip_smc_remotectl_config(REMOTECTL_ENABLE, 1);
ddata->support_psci = support_psci;
DBG("rk pwm sip init end!\n");
return 0;
end:
dev_info(&pdev->dev, "pwm sip wakeup config error!!\n");
ddata->support_psci = 0;
return ret;
}
static inline void rk_pwm_wakeup(struct input_dev *input)
{
input_event(input, EV_KEY, KEY_POWER, 1);
input_event(input, EV_KEY, KEY_POWER, 0);
input_sync(input);
}
static const struct of_device_id rk_pwm_of_match[] = {
{ .compatible = "rockchip,remotectl-pwm", .data = &pwm_data_v1},
{ .compatible = "rockchip,remotectl-pwm-v4", .data = &pwm_data_v4},
{ }
};
MODULE_DEVICE_TABLE(of, rk_pwm_of_match);
static int rk_pwm_probe(struct platform_device *pdev)
{
struct rkxx_remotectl_drvdata *ddata;
const struct of_device_id *id;
struct device_node *np = pdev->dev.of_node;
struct resource *r;
struct input_dev *input;
struct clk *clk;
struct clk *p_clk;
struct cpumask cpumask;
int num;
int irq;
int ret;
int i, j;
int cpu_id;
int pwm_id;
int pwm_freq;
int count;
pr_err(".. rk pwm remotectl v2.0 init\n");
id = of_match_device(rk_pwm_of_match, &pdev->dev);
if (!id)
return -EINVAL;
r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!r) {
dev_err(&pdev->dev, "no memory resources defined\n");
return -ENODEV;
}
ddata = devm_kzalloc(&pdev->dev, sizeof(struct rkxx_remotectl_drvdata),
GFP_KERNEL);
if (!ddata) {
return -ENOMEM;
}
ddata->pwm_data = id->data;
ddata->state = RMC_PRELOAD;
ddata->temp_period = 0;
ddata->base = devm_ioremap_resource(&pdev->dev, r);
if (IS_ERR(ddata->base))
return PTR_ERR(ddata->base);
count = of_property_count_strings(np, "clock-names");
if (count == 2) {
clk = devm_clk_get(&pdev->dev, "pwm");
p_clk = devm_clk_get(&pdev->dev, "pclk");
} else {
clk = devm_clk_get(&pdev->dev, NULL);
p_clk = clk;
}
if (IS_ERR(clk)) {
ret = PTR_ERR(clk);
if (ret != -EPROBE_DEFER)
dev_err(&pdev->dev, "Can't get bus clk: %d\n", ret);
return ret;
}
if (IS_ERR(p_clk)) {
ret = PTR_ERR(p_clk);
if (ret != -EPROBE_DEFER)
dev_err(&pdev->dev, "Can't get periph clk: %d\n", ret);
return ret;
}
ret = clk_prepare_enable(clk);
if (ret) {
dev_err(&pdev->dev, "Can't enable bus clk: %d\n", ret);
return ret;
}
ret = clk_prepare_enable(p_clk);
if (ret) {
dev_err(&pdev->dev, "Can't enable bus periph clk: %d\n", ret);
goto error_clk;
}
platform_set_drvdata(pdev, ddata);
num = rk_remotectl_get_irkeybd_count(pdev);
if (num == 0) {
pr_err("remotectl: no ir keyboard add in dts!!\n");
ret = -EINVAL;
goto error_pclk;
}
ddata->maxkeybdnum = num;
remotectl_button = devm_kzalloc(&pdev->dev,
num * sizeof(struct rkxx_remotectl_button),
GFP_KERNEL);
if (!remotectl_button) {
pr_err("failed to malloc remote button memory\n");
ret = -ENOMEM;
goto error_pclk;
}
input = devm_input_allocate_device(&pdev->dev);
if (!input) {
pr_err("failed to allocate input device\n");
ret = -ENOMEM;
goto error_pclk;
}
input->name = pdev->name;
input->phys = "gpio-keys/remotectl";
input->dev.parent = &pdev->dev;
input->id.bustype = BUS_HOST;
input->id.vendor = 0x524b;
input->id.product = 0x0006;
input->id.version = 0x0100;
ddata->input = input;
/*
* On PWM v4 (RK3588), channel 3 has a dedicated capture IRQ at
* index 1, separate from the group IRQ at index 0 that is shared
* with the other channels in the same PWM controller. Prefer the
* dedicated one so we don't collide with the pwm-rockchip driver
* bound to another channel (which claims the group IRQ without
* IRQF_SHARED on v4). Fall back to index 0 for older PWM v1-v3.
*/
irq = platform_get_irq(pdev, 1);
if (irq < 0)
irq = platform_get_irq(pdev, 0);
if (irq < 0) {
dev_err(&pdev->dev, "cannot find IRQ\n");
goto error_pclk;
}
ddata->irq = irq;
ddata->wakeup = 1;
if (ddata->pwm_data->pwm_version < 4) {
of_property_read_u32(np, "remote_pwm_id", &pwm_id);
pwm_id %= 4;
ddata->remote_pwm_id = pwm_id;
if (pwm_id > 3) {
dev_err(&pdev->dev, "pwm id error\n");
goto error_pclk;
}
DBG("remotectl: remote pwm id=0x%x\n", pwm_id);
}
of_property_read_u32(np, "handle_cpu_id", &cpu_id);
ddata->handle_cpu_id = cpu_id;
DBG("remotectl: handle cpu id=0x%x\n", cpu_id);
rk_remotectl_parse_ir_keys(pdev);
tasklet_init(&ddata->remote_tasklet, rk_pwm_remotectl_do_something,
(unsigned long)ddata);
for (j = 0; j < num; j++) {
DBG("remotectl probe j = 0x%x\n", j);
for (i = 0; i < remotectl_button[j].nbuttons; i++) {
int keycode;
keycode = remotectl_button[j].key_table[i].keycode;
input_set_capability(input, EV_KEY, keycode);
}
}
ret = input_register_device(input);
if (ret)
dev_err(&pdev->dev, "register input device err, ret=%d\n", ret);
input_set_capability(input, EV_KEY, KEY_WAKEUP);
device_init_wakeup(&pdev->dev, 1);
enable_irq_wake(irq);
timer_setup(&ddata->timer, rk_pwm_remotectl_timer, 0);
wake_lock_init(&ddata->remotectl_wake_lock,
WAKE_LOCK_SUSPEND, "rockchip_pwm_remote");
cpumask_clear(&cpumask);
cpumask_set_cpu(cpu_id, &cpumask);
irq_set_affinity_hint(irq, &cpumask);
ret = devm_request_irq(&pdev->dev, irq, ddata->pwm_data->funcs.irq_handler,
IRQF_NO_SUSPEND, "rk_pwm_irq", ddata);
if (ret) {
dev_err(&pdev->dev, "cannot claim IRQ %d\n", irq);
goto error_irq;
}
pwm_freq = clk_get_rate(clk) / 64;
ddata->pwm_freq_nstime = 1000000000 / pwm_freq;
ddata->pwm_data->funcs.init_hw(pdev);
ret = rk_pwm_pwrkey_wakeup_init(pdev);
if (!ret) {