-
Notifications
You must be signed in to change notification settings - Fork 5
/
ayaneo-platform.c
1083 lines (937 loc) · 35.7 KB
/
ayaneo-platform.c
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+
/*
* Platform driver for AYANEO x86 Handhelds that exposes RGB LED
* control via a sysfs led_class_multicolor interface.
*
* Copyright (C) 2023-2024 Derek J. Clark <derekjohn.clark@gmail.com>
* Copyright (C) 2023-2024 JELOS <https://github.com/JustEnoughLinuxOS>
* Copyright (C) 2024 Sebastian Kranz <https://github.com/Lightwars>
* Copyright (C) 2024 SytheZN <https://github.com/SytheZN>
* Derived from original reverse engineering work by Maya Matuszczyk
* <https://github.com/Maccraft123/ayaled>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/acpi.h>
#include <linux/delay.h>
#include <linux/dmi.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/led-class-multicolor.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/pm.h>
#include <linux/platform_device.h>
#include <linux/processor.h>
/* Handle ACPI lock mechanism */
static u32 ayaneo_mutex;
#define ACPI_LOCK_DELAY_MS 500
static bool lock_global_acpi_lock(void)
{
return ACPI_SUCCESS(acpi_acquire_global_lock(ACPI_LOCK_DELAY_MS, &ayaneo_mutex));
}
static bool unlock_global_acpi_lock(void)
{
return ACPI_SUCCESS(acpi_release_global_lock(ayaneo_mutex));
}
/* Common ec ram port data */
#define AYANEO_ADDR_PORT 0x4e
#define AYANEO_DATA_PORT 0x4f
#define AYANEO_HIGH_BYTE 0xd1
/* RGB LED EC Ram Registers */
/*
#define AYANEO_LED_MC_L_Q1_R 0xb3
#define AYANEO_LED_MC_L_Q1_G 0xb4
#define AYANEO_LED_MC_L_Q1_B 0xb5
#define AYANEO_LED_MC_L_Q2_R 0xb6
#define AYANEO_LED_MC_L_Q2_G 0xb7
#define AYANEO_LED_MC_L_Q2_B 0xb8
#define AYANEO_LED_MC_L_Q3_R 0xb9
#define AYANEO_LED_MC_L_Q3_G 0xba
#define AYANEO_LED_MC_L_Q3_B 0xbb
#define AYANEO_LED_MC_L_Q4_R 0xbc
#define AYANEO_LED_MC_L_Q4_G 0xbd
#define AYANEO_LED_MC_L_Q4_B 0xbe
#define AYANEO_LED_MC_R_Q1_R 0x73
#define AYANEO_LED_MC_R_Q1_G 0x74
#define AYANEO_LED_MC_R_Q1_B 0x75
#define AYANEO_LED_MC_R_Q2_R 0x76
#define AYANEO_LED_MC_R_Q2_G 0x77
#define AYANEO_LED_MC_R_Q2_B 0x78
#define AYANEO_LED_MC_R_Q3_R 0x79
#define AYANEO_LED_MC_R_Q3_G 0x7a
#define AYANEO_LED_MC_R_Q3_B 0x7b
#define AYANEO_LED_MC_R_Q4_R 0x7c
#define AYANEO_LED_MC_R_Q4_G 0x7d
#define AYANEO_LED_MC_R_Q4_B 0x7e
*/
#define AYANEO_LED_MC_ADDR_L 0xb0
#define AYANEO_LED_MC_ADDR_R 0x70
#define AYANEO_LED_MC_ADDR_CLOSE_1 0x86
#define AYANEO_LED_MC_ADDR_CLOSE_2 0xc6
#define AYANEO_LED_MC_MODE_ADDR 0x87
#define AYANEO_LED_MC_MODE_HOLD 0xa5
#define AYANEO_LED_MC_MODE_RELEASE 0x00
/* Schema:
#
# 0x6d - LED PWM control (0x03)
#
# 0xb1 - Support for 4 zones and RGB color
#
# RGB colors:
#
# 1 - Red
# 2 - Green
# 3 - Blue
#
# Zones:
#
# Right (2), Down (5), Left (8) , Up (11)
#
# Note: Set 0xb1 to 02 for off.
#
# 0xb2 - Sets brightness, requires b1 to be set at the same time.
#
# 00-ff - brightness from 0-255. Not noticeable to me above 128.
#
# 0xbf - Set expected mode
#
# 0x10 - Enable
# 0xe2 - Tint (+ Red for Purple, + Green for Teal)
# 0xe3-0xe5 - Tint + blink (unused)
#
# 0xff - Close channel
*/
/* EC Controlled RGB registers */
#define AYANEO_LED_PWM_CONTROL 0x6d
#define AYANEO_LED_POS 0xb1
#define AYANEO_LED_BRIGHTNESS 0xb2
#define AYANEO_LED_MODE_REG 0xbf
#define AYANEO_LED_CMD_ENABLE_ADDR 0x02
#define AYANEO_LED_CMD_ENABLE_ON 0xb1
#define AYANEO_LED_CMD_ENABLE_OFF 0x31
#define AYANEO_LED_CMD_ENABLE_RESET 0xc0
#define AYANEO_LED_CMD_PATTERN_ADDR 0x0f
#define AYANEO_LED_CMD_PATTERN_OFF 0x00
#define AYANEO_LED_CMD_FADE_ADDR 0x10
#define AYANEO_LED_CMD_FADE_OFF 0x00
#define AYANEO_LED_CMD_WATCHDOG_ADDR 0x15
#define AYANEO_LED_CMD_WATCHDOG_ON 0x07
#define AYANEO_LED_CMD_ANIM_1_ADDR 0x11 /* Animation step 1 */
#define AYANEO_LED_CMD_ANIM_2_ADDR 0x12 /* Animation step 2 */
#define AYANEO_LED_CMD_ANIM_3_ADDR 0x13 /* Animation step 3 */
#define AYANEO_LED_CMD_ANIM_4_ADDR 0x14 /* Animation step 4 */
#define AYANEO_LED_CMD_ANIM_STATIC 0x05
/* RGB Mode values */
#define AYANEO_LED_MODE_RELEASE 0x00 /* close channel, release control */
#define AYANEO_LED_MODE_WRITE 0x10 /* Default write mode */
#define AYANEO_LED_MODE_HOLD 0xfe /* close channel, hold control */
#define AYANEO_LED_GROUP_LEFT 0x01
#define AYANEO_LED_GROUP_RIGHT 0x02
#define AYANEO_LED_GROUP_LEFT_RIGHT 0x03
#define AYANEO_LED_GROUP_BUTTON 0x04
#define AYANEO_LED_WRITE_DELAY_LEGACY_MS 2
#define AYANEO_LED_WRITE_DELAY_MS 1
#define AYANEO_LED_WRITER_DELAY_RANGE_US 10000, 20000
#define AYANEO_LED_SUSPEND_RESUME_DELAY_MS 100
enum ayaneo_model {
air = 1,
air_1s,
air_1s_limited,
air_plus,
air_plus_mendo,
air_pro,
ayaneo_2,
ayaneo_2s,
geek,
geek_1s,
kun,
slide,
};
static enum ayaneo_model model;
enum AYANEO_LED_SUSPEND_MODE {
AYANEO_LED_SUSPEND_MODE_OEM,
AYANEO_LED_SUSPEND_MODE_KEEP,
AYANEO_LED_SUSPEND_MODE_OFF
};
static const char * const AYANEO_LED_SUSPEND_MODE_TEXT[] = {
[AYANEO_LED_SUSPEND_MODE_OEM] = "oem",
[AYANEO_LED_SUSPEND_MODE_KEEP] ="keep",
[AYANEO_LED_SUSPEND_MODE_OFF] = "off"
};
static enum AYANEO_LED_SUSPEND_MODE suspend_mode;
static const struct dmi_system_id dmi_table[] = {
{
.matches = {
DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
DMI_EXACT_MATCH(DMI_BOARD_NAME, "AIR"),
},
.driver_data = (void *)air,
},
{
.matches = {
DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
DMI_EXACT_MATCH(DMI_BOARD_NAME, "AIR 1S"),
},
.driver_data = (void *)air_1s,
},
{
.matches = {
DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
DMI_EXACT_MATCH(DMI_BOARD_NAME, "AIR 1S Limited"),
},
.driver_data = (void *)air_1s_limited,
},
{
.matches = {
DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
DMI_EXACT_MATCH(DMI_BOARD_NAME, "AB05-AMD"),
},
.driver_data = (void *)air_plus,
},
{
.matches = {
DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
DMI_EXACT_MATCH(DMI_BOARD_NAME, "AB05-Mendocino"),
},
.driver_data = (void *)air_plus_mendo,
},
{
.matches = {
DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
DMI_EXACT_MATCH(DMI_BOARD_NAME, "AIR Pro"),
},
.driver_data = (void *)air_pro,
},
{
.matches = {
DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
DMI_EXACT_MATCH(DMI_BOARD_NAME, "AYANEO 2"),
},
.driver_data = (void *)ayaneo_2,
},
{
.matches = {
DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
DMI_EXACT_MATCH(DMI_BOARD_NAME, "AYANEO 2S"),
},
.driver_data = (void *)ayaneo_2s,
},
{
.matches = {
DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
DMI_EXACT_MATCH(DMI_BOARD_NAME, "GEEK"),
},
.driver_data = (void *)geek,
},
{
.matches = {
DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
DMI_EXACT_MATCH(DMI_BOARD_NAME, "GEEK 1S"),
},
.driver_data = (void *)geek_1s,
},
{
.matches = {
DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
DMI_EXACT_MATCH(DMI_BOARD_NAME, "AYANEO KUN"),
},
.driver_data = (void *)kun,
},
{
.matches = {
DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
DMI_EXACT_MATCH(DMI_BOARD_NAME, "AS01"),
},
.driver_data = (void *)slide,
},
{},
};
static int ec_write_ram(u8 index, u8 val)
{
int ret;
if (!lock_global_acpi_lock())
return -EBUSY;
outb(0x2e, AYANEO_ADDR_PORT);
outb(0x11, AYANEO_DATA_PORT);
outb(0x2f, AYANEO_ADDR_PORT);
outb(AYANEO_HIGH_BYTE, AYANEO_DATA_PORT);
outb(0x2e, AYANEO_ADDR_PORT);
outb(0x10, AYANEO_DATA_PORT);
outb(0x2f, AYANEO_ADDR_PORT);
outb(index, AYANEO_DATA_PORT);
outb(0x2e, AYANEO_ADDR_PORT);
outb(0x12, AYANEO_DATA_PORT);
outb(0x2f, AYANEO_ADDR_PORT);
outb(val, AYANEO_DATA_PORT);
if (!unlock_global_acpi_lock())
return -EBUSY;
return ret;
}
/* Function Summary
# AyaNeo devices can be largely divided into 2 groups; modern and legacy.
# - Legacy devices use a microcontroller either embedded into or controlled via
# the system's ACPI controller.
# - Modern devices use a dedicated microcontroller and communicate via shared
# memory.
#
# The control scheme is largely shared between the 2 device types and many of
# the command values are shared.
#
# ayaneo_led_mc_set / ayaneo_led_mc_legacy_set
# Sets the value of a single address or subpixel
#
# ayaneo_led_mc_release / ayaneo_led_mc_legacy_release
# Releases control of the LEDs back to the microcontroller.
# This function is abstracted by ayaneo_led_mc_release_control.
#
# ayaneo_led_mc_hold / ayaneo_led_mc_legacy_hold
# Takes and holds control of the LEDs from the microcontroller.
# This function is abstracted by ayaneo_led_mc_take_control.
#
# ayaneo_led_mc_intensity / ayaneo_led_mc_legacy_intensity
# Sets the values of all of the LEDs in the zones of a given group.
#
# ayaneo_led_mc_off / ayaneo_led_mc_legacy_off
# Instructs the microcontroller to disable output for the given group.
#
# ayaneo_led_mc_on / ayaneo_led_mc_legacy_on
# Instructs the microcontroller to enable output for the given group.
#
# ayaneo_led_mc_reset / ayaneo_led_mc_legacy_reset
# Reverts all of the microcontroller internal registers to power on
# defaults.
*/
/* Dedicated microcontroller methods */
static void ayaneo_led_mc_set(u8 group, u8 pos, u8 brightness)
{
u8 led_offset;
u8 close_cmd;
if (group < 2)
{
led_offset = AYANEO_LED_MC_ADDR_L;
close_cmd = AYANEO_LED_MC_ADDR_CLOSE_2;
}
else
{
led_offset = AYANEO_LED_MC_ADDR_R;
close_cmd = AYANEO_LED_MC_ADDR_CLOSE_1;
}
ec_write_ram(led_offset + pos, brightness);
ec_write_ram(close_cmd, 0x01);
mdelay(AYANEO_LED_WRITE_DELAY_MS);
}
static void ayaneo_led_mc_release(void)
{
ec_write_ram(AYANEO_LED_MC_MODE_ADDR, AYANEO_LED_MC_MODE_RELEASE);
}
static void ayaneo_led_mc_hold(void)
{
ec_write_ram(AYANEO_LED_MC_MODE_ADDR, AYANEO_LED_MC_MODE_HOLD);
ayaneo_led_mc_set(AYANEO_LED_GROUP_LEFT_RIGHT, 0x00, 0x00);
}
static void ayaneo_led_mc_intensity(u8 group, u8 *color, u8 zones[])
{
int zone;
for (zone = 0; zone < 4; zone++) {
ayaneo_led_mc_set(group, zones[zone], color[0]);
ayaneo_led_mc_set(group, zones[zone] + 1, color[1]);
ayaneo_led_mc_set(group, zones[zone] + 2, color[2]);
}
// note: omit for aya flip when implemented, causes unexpected behavior
ayaneo_led_mc_set(AYANEO_LED_GROUP_LEFT_RIGHT, 0x00, 0x00);
}
static void ayaneo_led_mc_off(void)
{
ayaneo_led_mc_set(AYANEO_LED_GROUP_LEFT,
AYANEO_LED_CMD_ENABLE_ADDR, AYANEO_LED_CMD_ENABLE_OFF);
ayaneo_led_mc_set(AYANEO_LED_GROUP_RIGHT,
AYANEO_LED_CMD_ENABLE_ADDR, AYANEO_LED_CMD_ENABLE_OFF);
// note: omit for aya flip when implemented, causes unexpected behavior
ayaneo_led_mc_set(AYANEO_LED_GROUP_LEFT_RIGHT, 0x00, 0x00);
}
static void ayaneo_led_mc_on(void)
{
ayaneo_led_mc_set(AYANEO_LED_GROUP_LEFT,
AYANEO_LED_CMD_ENABLE_ADDR, AYANEO_LED_CMD_ENABLE_ON);
ayaneo_led_mc_set(AYANEO_LED_GROUP_RIGHT,
AYANEO_LED_CMD_ENABLE_ADDR, AYANEO_LED_CMD_ENABLE_ON);
ayaneo_led_mc_set(AYANEO_LED_GROUP_LEFT,
AYANEO_LED_CMD_PATTERN_ADDR, AYANEO_LED_CMD_PATTERN_OFF);
ayaneo_led_mc_set(AYANEO_LED_GROUP_RIGHT,
AYANEO_LED_CMD_PATTERN_ADDR, AYANEO_LED_CMD_PATTERN_OFF);
ayaneo_led_mc_set(AYANEO_LED_GROUP_LEFT,
AYANEO_LED_CMD_FADE_ADDR, AYANEO_LED_CMD_FADE_OFF);
ayaneo_led_mc_set(AYANEO_LED_GROUP_RIGHT,
AYANEO_LED_CMD_FADE_ADDR, AYANEO_LED_CMD_FADE_OFF);
// Set static color animation
ayaneo_led_mc_set(AYANEO_LED_GROUP_LEFT,
AYANEO_LED_CMD_ANIM_1_ADDR, AYANEO_LED_CMD_ANIM_STATIC);
ayaneo_led_mc_set(AYANEO_LED_GROUP_RIGHT,
AYANEO_LED_CMD_ANIM_1_ADDR, AYANEO_LED_CMD_ANIM_STATIC);
ayaneo_led_mc_set(AYANEO_LED_GROUP_LEFT,
AYANEO_LED_CMD_ANIM_2_ADDR, AYANEO_LED_CMD_ANIM_STATIC);
ayaneo_led_mc_set(AYANEO_LED_GROUP_RIGHT,
AYANEO_LED_CMD_ANIM_2_ADDR, AYANEO_LED_CMD_ANIM_STATIC);
ayaneo_led_mc_set(AYANEO_LED_GROUP_LEFT,
AYANEO_LED_CMD_ANIM_3_ADDR, AYANEO_LED_CMD_ANIM_STATIC);
ayaneo_led_mc_set(AYANEO_LED_GROUP_RIGHT,
AYANEO_LED_CMD_ANIM_3_ADDR, AYANEO_LED_CMD_ANIM_STATIC);
ayaneo_led_mc_set(AYANEO_LED_GROUP_LEFT,
AYANEO_LED_CMD_ANIM_4_ADDR, AYANEO_LED_CMD_ANIM_STATIC);
ayaneo_led_mc_set(AYANEO_LED_GROUP_RIGHT,
AYANEO_LED_CMD_ANIM_4_ADDR, AYANEO_LED_CMD_ANIM_STATIC);
ayaneo_led_mc_set(AYANEO_LED_GROUP_LEFT,
AYANEO_LED_CMD_WATCHDOG_ADDR, AYANEO_LED_CMD_WATCHDOG_ON);
ayaneo_led_mc_set(AYANEO_LED_GROUP_RIGHT,
AYANEO_LED_CMD_WATCHDOG_ADDR, AYANEO_LED_CMD_WATCHDOG_ON);
// note: omit for aya flip when implemented, causes unexpected behavior
ayaneo_led_mc_set(AYANEO_LED_GROUP_LEFT_RIGHT, 0x00, 0x00);
}
static void ayaneo_led_mc_reset(void)
{
ayaneo_led_mc_set(AYANEO_LED_GROUP_LEFT,
AYANEO_LED_CMD_ENABLE_ADDR, AYANEO_LED_CMD_ENABLE_RESET);
ayaneo_led_mc_set(AYANEO_LED_GROUP_RIGHT,
AYANEO_LED_CMD_ENABLE_ADDR, AYANEO_LED_CMD_ENABLE_RESET);
// note: omit for aya flip when implemented, causes unexpected behavior
ayaneo_led_mc_set(AYANEO_LED_GROUP_LEFT_RIGHT, 0x00, 0x00);
}
/* ACPI controller methods */
static void ayaneo_led_mc_legacy_set(u8 group, u8 pos, u8 brightness)
{
if (!lock_global_acpi_lock())
return;
ec_write(AYANEO_LED_PWM_CONTROL, group);
ec_write(AYANEO_LED_POS, pos);
ec_write(AYANEO_LED_BRIGHTNESS, brightness);
ec_write(AYANEO_LED_MODE_REG, AYANEO_LED_MODE_WRITE);
if (!unlock_global_acpi_lock())
return;
mdelay(AYANEO_LED_WRITE_DELAY_LEGACY_MS);
if (!lock_global_acpi_lock())
return;
ec_write(AYANEO_LED_MODE_REG, AYANEO_LED_MODE_HOLD);
if (!unlock_global_acpi_lock())
return;
}
static void ayaneo_led_mc_legacy_release(void)
{
if (!lock_global_acpi_lock())
return;
ec_write(AYANEO_LED_MODE_REG, AYANEO_LED_MODE_RELEASE);
if (!unlock_global_acpi_lock())
return;
}
static void ayaneo_led_mc_legacy_hold(void)
{
if (!lock_global_acpi_lock())
return;
ec_write(AYANEO_LED_MODE_REG, AYANEO_LED_MODE_HOLD);
if (!unlock_global_acpi_lock())
return;
}
static void ayaneo_led_mc_legacy_intensity_single(u8 group, u8 *color, u8 zone)
{
ayaneo_led_mc_legacy_set(group, zone, color[0]);
ayaneo_led_mc_legacy_set(group, zone + 1, color[1]);
ayaneo_led_mc_legacy_set(group, zone + 2, color[2]);
}
static void ayaneo_led_mc_legacy_intensity(u8 group, u8 *color, u8 zones[])
{
int zone;
for (zone = 0; zone < 4; zone++) {
ayaneo_led_mc_legacy_intensity_single(group, color, zones[zone]);
}
// note: omit for aya flip when implemented, causes unexpected behavior
ayaneo_led_mc_legacy_set(AYANEO_LED_GROUP_LEFT_RIGHT, 0x00, 0x00);
}
/* KUN doesn't use consistant zone mapping for RGB, adjust */
static void ayaneo_led_mc_legacy_intensity_kun(u8 group, u8 *color)
{
u8 zone;
u8 remap_color[3];
if (group == AYANEO_LED_GROUP_BUTTON)
{
zone = 12;
remap_color[0] = color[2];
remap_color[1] = color[0];
remap_color[2] = color[1];
ayaneo_led_mc_legacy_intensity_single(AYANEO_LED_GROUP_BUTTON, remap_color, zone);
ayaneo_led_mc_legacy_set(AYANEO_LED_GROUP_LEFT_RIGHT, 0x00, 0x00);
return;
}
zone = 3;
remap_color[0] = color[1];
remap_color[1] = color[0];
remap_color[2] = color[2];
ayaneo_led_mc_legacy_intensity_single(group, remap_color, zone);
zone = 6;
remap_color[0] = color[1];
remap_color[1] = color[2];
remap_color[2] = color[0];
ayaneo_led_mc_legacy_intensity_single(group, remap_color, zone);
zone = 9;
remap_color[0] = color[2];
remap_color[1] = color[0];
remap_color[2] = color[1];
ayaneo_led_mc_legacy_intensity_single(group, remap_color, zone);
zone = 12;
remap_color[0] = color[2];
remap_color[1] = color[1];
remap_color[2] = color[0];
ayaneo_led_mc_legacy_intensity_single(group, remap_color, zone);
ayaneo_led_mc_legacy_set(AYANEO_LED_GROUP_LEFT_RIGHT, 0x00, 0x00);
}
static void ayaneo_led_mc_legacy_off(void)
{
ayaneo_led_mc_legacy_set(AYANEO_LED_GROUP_LEFT,
AYANEO_LED_CMD_ENABLE_ADDR, AYANEO_LED_CMD_ENABLE_OFF);
ayaneo_led_mc_legacy_set(AYANEO_LED_GROUP_RIGHT,
AYANEO_LED_CMD_ENABLE_ADDR, AYANEO_LED_CMD_ENABLE_OFF);
// note: omit for aya flip when implemented, causes unexpected behavior
ayaneo_led_mc_legacy_set(AYANEO_LED_GROUP_LEFT_RIGHT, 0x00, 0x00);
}
static void ayaneo_led_mc_legacy_on(void)
{
ayaneo_led_mc_legacy_set(AYANEO_LED_GROUP_LEFT,
AYANEO_LED_CMD_ENABLE_ADDR, AYANEO_LED_CMD_ENABLE_ON);
ayaneo_led_mc_legacy_set(AYANEO_LED_GROUP_RIGHT,
AYANEO_LED_CMD_ENABLE_ADDR, AYANEO_LED_CMD_ENABLE_ON);
// note: omit for aya flip when implemented, causes unexpected behavior
ayaneo_led_mc_legacy_set(AYANEO_LED_GROUP_LEFT_RIGHT, 0x00, 0x00);
}
static void ayaneo_led_mc_legacy_reset(void)
{
ayaneo_led_mc_legacy_set(AYANEO_LED_GROUP_LEFT,
AYANEO_LED_CMD_ENABLE_ADDR, AYANEO_LED_CMD_ENABLE_RESET);
ayaneo_led_mc_legacy_set(AYANEO_LED_GROUP_RIGHT,
AYANEO_LED_CMD_ENABLE_ADDR, AYANEO_LED_CMD_ENABLE_RESET);
// note: omit for aya flip when implemented, causes unexpected behavior
ayaneo_led_mc_legacy_set(AYANEO_LED_GROUP_LEFT_RIGHT, 0x00, 0x00);
}
/* Device command abstractions */
static void ayaneo_led_mc_take_control(void)
{
switch (model) {
case air:
case air_1s:
case air_1s_limited:
case air_pro:
case air_plus_mendo:
case geek:
case geek_1s:
case ayaneo_2:
case ayaneo_2s:
case kun:
ayaneo_led_mc_legacy_hold();
ayaneo_led_mc_legacy_reset();
ayaneo_led_mc_legacy_off();
break;
case air_plus:
case slide:
ayaneo_led_mc_hold();
ayaneo_led_mc_reset();
ayaneo_led_mc_off();
break;
default:
break;
}
}
static void ayaneo_led_mc_release_control(void)
{
switch (model) {
case air:
case air_1s:
case air_1s_limited:
case air_pro:
case air_plus_mendo:
case geek:
case geek_1s:
case ayaneo_2:
case ayaneo_2s:
case kun:
ayaneo_led_mc_legacy_reset();
ayaneo_led_mc_legacy_release();
break;
case air_plus:
case slide:
ayaneo_led_mc_reset();
ayaneo_led_mc_release();
break;
default:
break;
}
}
/* Threaded writes:
# The writer thread's job is to push updates to the physical LEDs as fast as
# possible while allowing updates to the LED multi_intensity/brightness sysfs
# attributes to return quickly.
#
# During multi_intensity/brightness set, the ayaneo_led_mc_update_color array
# is updated with the target color and ayaneo_led_mc_update_required is
# incremented by 1.
#
# When the writer thread begins its next loop, it copies the current values of
# ayaneo_led_mc_update_required, and ayaneo_led_mc_update_color, after which
# the new color is pushed to the microcontroller. After the color has been
# pushed the writer thread subtracts the starting value from
# ayaneo_led_mc_update_required. If any updates were pushed to
# ayaneo_led_mc_update_required during the writes then the following iteration
# will immediately begin writing the new colors to the microcontroller,
# otherwise it'll sleep for a short while.
#
# Updates to ayaneo_led_mc_update_required and ayaneo_led_mc_update_color are
# syncronised by ayaneo_led_mc_update_lock to prevent a race condition between
# the writer thread and the brightness set function.
#
# During suspend kthread_stop is called which causes the writer thread to
# terminate after its current iteration. The writer thread is restarted during
# resume to allow updates to continue.
*/
static struct task_struct *ayaneo_led_mc_writer_thread;
static int ayaneo_led_mc_update_required;
static u8 ayaneo_led_mc_update_color[3];
DEFINE_RWLOCK(ayaneo_led_mc_update_lock);
static void ayaneo_led_mc_scale_color(u8 *color, u8 max_value)
{
for (int i = 0; i < 3; i++)
{
int c_color = (int)color[i] * (int)max_value / 255;
// prevents left-right discrepancy when brightness/color are low
if (c_color == 0 && color[i] > 0)
c_color = 1;
color[i] = (u8)c_color;
}
}
static void ayaneo_led_mc_brightness_apply(u8 *color)
{
u8 color_l[3];
u8 color_r[3];
u8 color_b[3];
u8 zones[4] = {3, 6, 9, 12};
for (int i = 0; i < 3; i++)
{
color_l[i] = color[i];
color_r[i] = color[i];
color_b[i] = color[i];
}
ayaneo_led_mc_scale_color(color_l, 192);
ayaneo_led_mc_scale_color(color_r, 192);
ayaneo_led_mc_scale_color(color_b, 192);
switch (model) {
case air:
case air_pro:
case air_1s:
ayaneo_led_mc_scale_color(color_l, 69);
ayaneo_led_mc_legacy_on();
ayaneo_led_mc_legacy_intensity(AYANEO_LED_GROUP_LEFT, color_l, zones);
ayaneo_led_mc_legacy_intensity(AYANEO_LED_GROUP_RIGHT, color_r, zones);
break;
case air_1s_limited:
ayaneo_led_mc_scale_color(color_r, 204);
ayaneo_led_mc_legacy_on();
ayaneo_led_mc_legacy_intensity(AYANEO_LED_GROUP_LEFT, color_l, zones);
ayaneo_led_mc_legacy_intensity(AYANEO_LED_GROUP_RIGHT, color_r, zones);
break;
case geek:
case geek_1s:
case ayaneo_2:
case ayaneo_2s:
ayaneo_led_mc_legacy_on();
ayaneo_led_mc_legacy_intensity(AYANEO_LED_GROUP_LEFT, color_l, zones);
ayaneo_led_mc_legacy_intensity(AYANEO_LED_GROUP_RIGHT, color_r, zones);
break;
case air_plus_mendo:
ayaneo_led_mc_scale_color(color_l, 64);
ayaneo_led_mc_scale_color(color_r, 32);
ayaneo_led_mc_legacy_on();
ayaneo_led_mc_legacy_intensity(AYANEO_LED_GROUP_LEFT, color_l, zones);
ayaneo_led_mc_legacy_intensity(AYANEO_LED_GROUP_RIGHT, color_r, zones);
break;
case air_plus:
ayaneo_led_mc_scale_color(color_l, 64);
ayaneo_led_mc_scale_color(color_r, 32);
ayaneo_led_mc_on();
ayaneo_led_mc_intensity(AYANEO_LED_GROUP_LEFT, color_l, zones);
ayaneo_led_mc_intensity(AYANEO_LED_GROUP_RIGHT, color_r, zones);
break;
case slide:
ayaneo_led_mc_on();
ayaneo_led_mc_intensity(AYANEO_LED_GROUP_LEFT, color_l, zones);
ayaneo_led_mc_intensity(AYANEO_LED_GROUP_RIGHT, color_r, zones);
break;
case kun:
ayaneo_led_mc_legacy_on();
ayaneo_led_mc_legacy_intensity_kun(AYANEO_LED_GROUP_LEFT, color_l);
ayaneo_led_mc_legacy_intensity_kun(AYANEO_LED_GROUP_RIGHT, color_r);
ayaneo_led_mc_legacy_intensity_kun(AYANEO_LED_GROUP_BUTTON, color_b);
break;
default:
break;
}
}
int ayaneo_led_mc_writer(void *pv);
int ayaneo_led_mc_writer(void *pv)
{
pr_info("Writer thread started.\n");
int count;
u8 color[3];
while (!kthread_should_stop())
{
read_lock(&ayaneo_led_mc_update_lock);
count = ayaneo_led_mc_update_required;
if (count)
{
color[0] = ayaneo_led_mc_update_color[0];
color[1] = ayaneo_led_mc_update_color[1];
color[2] = ayaneo_led_mc_update_color[2];
}
read_unlock(&ayaneo_led_mc_update_lock);
if (count)
{
ayaneo_led_mc_brightness_apply(color);
write_lock(&ayaneo_led_mc_update_lock);
ayaneo_led_mc_update_required -= count;
write_unlock(&ayaneo_led_mc_update_lock);
}
else
usleep_range(AYANEO_LED_WRITER_DELAY_RANGE_US);
}
pr_info("Writer thread stopped.\n");
return 0;
}
/* RGB LED Logic */
static void ayaneo_led_mc_brightness_set(struct led_classdev *led_cdev,
enum led_brightness brightness)
{
struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
int val;
int i;
struct mc_subled s_led;
if (brightness < 0 || brightness > 255)
return;
led_cdev->brightness = brightness;
write_lock(&ayaneo_led_mc_update_lock);
for (i = 0; i < mc_cdev->num_colors; i++) {
s_led = mc_cdev->subled_info[i];
if (s_led.intensity < 0 || s_led.intensity > 255)
return;
val = brightness * s_led.intensity / led_cdev->max_brightness;
ayaneo_led_mc_update_color[s_led.channel] = val;
}
ayaneo_led_mc_update_required++;
write_unlock(&ayaneo_led_mc_update_lock);
};
static enum led_brightness ayaneo_led_mc_brightness_get(struct led_classdev *led_cdev)
{
return led_cdev->brightness;
};
/* Suspend Mode
# Multiple modes of operation are supported during suspend:
#
# OEM: Retains the default behavior of the device by returning control to the
# microcontroller. On most devices the LEDs will flash periodically, and
# will turn red when charging.
# Off: The LEDs are turned off during suspend, and control of the LEDs is
# retained by the driver. On most devices, charging will not turn on the
# LEDs. During resume, the last color set is restored.
# Keep: The color currently set on the LEDs remains unchanged, and control of
# the LEDs is retained by the driver. On most devices, charging will not
# change the color of the LEDs.
*/
static ssize_t suspend_mode_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
bool active;
ssize_t count = 0;
int i;
for (i = 0; i < ARRAY_SIZE(AYANEO_LED_SUSPEND_MODE_TEXT); i++) {
active = i == suspend_mode;
if (active) {
count += sysfs_emit_at(buf, count, "[%s] ",
AYANEO_LED_SUSPEND_MODE_TEXT[i]);
}
else {
count += sysfs_emit_at(buf, count, "%s ",
AYANEO_LED_SUSPEND_MODE_TEXT[i]);
}
}
if (count)
buf[count - 1] = '\n';
return count;
}
static ssize_t suspend_mode_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
int res = sysfs_match_string(AYANEO_LED_SUSPEND_MODE_TEXT, buf);
if (res < 0)
return -EINVAL;
suspend_mode = res;
return count;
}
static DEVICE_ATTR_RW(suspend_mode);
static struct attribute *ayaneo_led_mc_attrs[] = {
NULL,
NULL,
};
static void suspend_mode_register_attr(void)
{
switch (model) {
case air:
case air_1s:
case air_1s_limited:
case air_pro:
case air_plus_mendo:
case geek:
case geek_1s:
case ayaneo_2:
case ayaneo_2s:
case kun:
ayaneo_led_mc_attrs[0] = &dev_attr_suspend_mode.attr;
break;
case air_plus:
case slide:
ayaneo_led_mc_attrs[0] = &dev_attr_suspend_mode.attr;
break;
default:
break;
}
}
ATTRIBUTE_GROUPS(ayaneo_led_mc);
struct mc_subled ayaneo_led_mc_subled_info[] = {
{
.color_index = LED_COLOR_ID_RED,
.brightness = 0,
.intensity = 0,
.channel = 0,
},
{
.color_index = LED_COLOR_ID_GREEN,
.brightness = 0,
.intensity = 0,
.channel = 1,
},
{
.color_index = LED_COLOR_ID_BLUE,
.brightness = 0,
.intensity = 0,
.channel = 2,
},
};
struct led_classdev_mc ayaneo_led_mc = {
.led_cdev = {
.name = "multicolor:chassis",
.brightness = 0,
.max_brightness = 255,
.brightness_set = ayaneo_led_mc_brightness_set,
.brightness_get = ayaneo_led_mc_brightness_get,
},
.num_colors = ARRAY_SIZE(ayaneo_led_mc_subled_info),
.subled_info = ayaneo_led_mc_subled_info,
};
static int ayaneo_platform_resume(struct platform_device *pdev)
{
ayaneo_led_mc_take_control();
write_lock(&ayaneo_led_mc_update_lock);
ayaneo_led_mc_update_required++;
write_unlock(&ayaneo_led_mc_update_lock);
// Allow the MCU to sync with the new state.
msleep(AYANEO_LED_SUSPEND_RESUME_DELAY_MS);
ayaneo_led_mc_writer_thread = kthread_run(ayaneo_led_mc_writer,
NULL,
"ayaneo-platform led writer");
return 0;
}
static int ayaneo_platform_suspend(struct platform_device *pdev, pm_message_t state)
{
kthread_stop(ayaneo_led_mc_writer_thread);
switch (suspend_mode)
{
case AYANEO_LED_SUSPEND_MODE_OEM:
ayaneo_led_mc_release_control();
break;
case AYANEO_LED_SUSPEND_MODE_KEEP:
// Nothing to do.
break;
case AYANEO_LED_SUSPEND_MODE_OFF:
ayaneo_led_mc_take_control();
break;
default:
break;
}
// Allow the MCU to sync with the new state.
msleep(AYANEO_LED_SUSPEND_RESUME_DELAY_MS);
return 0;
}
static int ayaneo_platform_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;