forked from pharra/linux-surface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacpi.patch
1320 lines (1319 loc) · 34 KB
/
acpi.patch
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
diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
index 566644bb496a..081105a9cfca 100644
--- a/drivers/platform/x86/Kconfig
+++ b/drivers/platform/x86/Kconfig
@@ -1158,6 +1158,15 @@ config SURFACE_3_BUTTON
---help---
This driver handles the power/home/volume buttons on the Microsoft Surface 3 tablet.
+config ACPI_SURFACE
+ tristate "Microsoft Surface Extras"
+ depends on ACPI
+ depends on ACPI_WMI
+ depends on INPUT
+ ---help---
+ This driver adds support for access to certain system events
+ on Microsoft Surface devices.
+
config INTEL_PUNIT_IPC
tristate "Intel P-Unit IPC Driver"
---help---
diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
index 2ba6cb795338..8fd5b93bb20d 100644
--- a/drivers/platform/x86/Makefile
+++ b/drivers/platform/x86/Makefile
@@ -81,6 +81,9 @@ obj-$(CONFIG_INTEL_PMC_IPC) += intel_pmc_ipc.o
obj-$(CONFIG_SILEAD_DMI) += silead_dmi.o
obj-$(CONFIG_SURFACE_PRO3_BUTTON) += surfacepro3_button.o
obj-$(CONFIG_SURFACE_3_BUTTON) += surface3_button.o
+obj-$(CONFIG_ACPI_SURFACE) += surface_acpi.o
+obj-$(CONFIG_ACPI_SURFACE) += surface_i2c.o
+obj-$(CONFIG_ACPI_SURFACE) += surface_platform.o
obj-$(CONFIG_INTEL_PUNIT_IPC) += intel_punit_ipc.o
obj-$(CONFIG_INTEL_BXTWC_PMIC_TMU) += intel_bxtwc_tmu.o
obj-$(CONFIG_INTEL_TELEMETRY) += intel_telemetry_core.o \
diff --git a/drivers/platform/x86/surface_acpi.c b/drivers/platform/x86/surface_acpi.c
new file mode 100644
index 000000000000..c969bda99464
--- /dev/null
+++ b/drivers/platform/x86/surface_acpi.c
@@ -0,0 +1,481 @@
+/*
+ * surface_acpi.c - Microsoft Surface ACPI Driver
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * The full GNU General Public License is included in this distribution in
+ * the file called "COPYING".
+ */
+
+#define SURFACE_ACPI_VERSION "0.1"
+#define SURFACE_GEN_VERSION 0x08
+#define PROC_SURFACE "surface"
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/types.h>
+#include <linux/acpi.h>
+#include <linux/power_supply.h>
+#include <linux/thermal.h>
+#include <linux/dmi.h>
+#include <linux/seq_file.h>
+#include <acpi/acpi_bus.h>
+#include <acpi/acpi_drivers.h>
+
+#include "surface_acpi.h"
+
+#define SUR_METHOD_DSM "_DSM"
+#define SUR_METHOD_REG "_REG"
+#define SUR_METHOD_STA "_STA"
+#define SUR_METHOD_INI "_INI"
+#define SUR_METHOD_CRS "_CRS"
+
+#define SUR_QUERY_DEVICE 0x00
+#define SUR_SET_DVER 0x01
+#define SUR_GET_BOARD_REVID 0x02
+#define SUR_BAT1_STATE_CHANGE 0x03
+#define SUR_BAT1_INFO_CHANGE 0x04
+#define SUR_PSU_STATE_CHANGE 0x05
+#define SUR_PSU_INFO_CHANGE 0x06
+#define SUR_BAT2_STATE_CHANGE 0x07
+#define SUR_BAT2_INFO_CHANGE 0x08
+#define SUR_SENSOR_TRIP_POINT 0x09
+
+#define REG_AVAILABLE 0x01
+#define REG_INIT 0x09
+
+static char SURFACE_EVENT_GUID[] = "93b666c5-70c6-469f-a215-3d487c91ab3c";
+static char SUR_SAN_RQST[] = "\\_SB._SAN.RQST";
+static char SUR_SAN_RQSX[] = "\\_SB._SAN.RQSX";
+
+struct surface_acpi_dev {
+ acpi_handle handle;
+ acpi_handle rqst_handle;
+ acpi_handle rqsx_handle;
+
+ struct acpi_device *san_dev;
+ struct acpi_device *ssh_dev;
+ struct acpi_device *bat1_dev;
+ struct acpi_device *bat2_dev;
+ struct acpi_device *psu_dev;
+
+ unsigned int bat1_attached:1;
+ unsigned int bat2_attached:1;
+ unsigned int psu_registered:1;
+};
+
+static struct surface_acpi_dev *surface_acpi;
+
+static struct proc_dir_entry *surface_proc_dir;
+
+static acpi_status surface_acpi_check_status(struct acpi_device *dev)
+{
+ unsigned long long value;
+ acpi_status status;
+
+ if (acpi_has_method(dev->handle, SUR_METHOD_STA)) {
+ status = acpi_evaluate_integer(dev->handle,
+ SUR_METHOD_STA, NULL, &value);
+
+ if (ACPI_FAILURE(status)) {
+ pr_err("surface_acpi: ACPI event failure status %s\n",
+ acpi_format_exception(status));
+ return AE_ERROR;
+ }
+ }
+ else
+ return AE_NOT_FOUND;
+
+ return AE_OK;
+}
+
+static acpi_status surface_acpi_san_reg(void)
+{
+ union acpi_object in_objs[2], out_objs[1];
+ struct acpi_object_list params;
+ struct acpi_buffer results;
+ acpi_status status;
+
+ params.count = ARRAY_SIZE(in_objs);
+ params.pointer = in_objs;
+ in_objs[0].type = ACPI_TYPE_INTEGER;
+ in_objs[0].integer.value = REG_INIT;
+ in_objs[1].type = ACPI_TYPE_INTEGER;
+ in_objs[1].integer.value = REG_AVAILABLE;
+ results.length = sizeof(out_objs);
+ results.pointer = out_objs;
+
+ if (acpi_has_method(surface_acpi->handle, SUR_METHOD_REG)) {
+ status = acpi_evaluate_object(surface_acpi->handle,
+ SUR_METHOD_REG, ¶ms, &results);
+
+ if (ACPI_FAILURE(status)) {
+ pr_err("surface_acpi: ACPI event failure status %s\n",
+ acpi_format_exception(status));
+ return AE_ERROR;
+ }
+ }
+ else
+ return AE_NOT_FOUND;
+
+ return AE_OK;
+}
+
+acpi_status surface_acpi_event_handler(u32 event)
+{
+ union acpi_object in_objs[4], out_objs[5];
+ struct acpi_object_list params;
+ struct acpi_buffer results;
+ acpi_status status;
+
+ params.count = ARRAY_SIZE(in_objs);
+ params.pointer = in_objs;
+ in_objs[0].type = ACPI_TYPE_BUFFER;
+ in_objs[0].buffer.length = sizeof(SURFACE_EVENT_GUID);
+ in_objs[0].buffer.pointer = SURFACE_EVENT_GUID;
+ in_objs[1].type = ACPI_TYPE_INTEGER;
+ in_objs[1].integer.value = SUR_QUERY_DEVICE;
+ in_objs[2].type = ACPI_TYPE_INTEGER;
+ in_objs[2].integer.value = event;
+ in_objs[3].type = ACPI_TYPE_PACKAGE;
+ in_objs[3].package.count = 0;
+ in_objs[3].package.elements = SURFACE_GEN_VERSION;
+ results.length = sizeof(out_objs);
+ results.pointer = out_objs;
+
+ if (acpi_has_method(surface_acpi->handle, SUR_METHOD_DSM)) {
+ status = acpi_evaluate_object(surface_acpi->handle,
+ SUR_METHOD_DSM, ¶ms, &results);
+
+ if (ACPI_FAILURE(status)) {
+ pr_err("surface_acpi: ACPI event failure status %s\n",
+ acpi_format_exception(status));
+ return AE_ERROR;
+ }
+ }
+ else
+ return AE_NOT_FOUND;
+
+ return AE_OK;
+}
+EXPORT_SYMBOL(surface_acpi_event_handler);
+
+static void surface_acpi_san_load(void)
+{
+ acpi_status ret;
+
+ ret = surface_acpi_event_handler(SUR_SET_DVER);
+ if (ACPI_FAILURE(ret))
+ pr_err("surface_acpi: Error setting Driver Version\n");
+
+ ret = surface_acpi_event_handler(SUR_SENSOR_TRIP_POINT);
+ if (ACPI_FAILURE(ret))
+ pr_err("surface_acpi: Error setting Sensor Trip Point\n");
+
+ ret = surface_acpi_event_handler(SUR_BAT1_INFO_CHANGE);
+ if (ACPI_FAILURE(ret))
+ pr_err("surface_acpi: Error attaching BAT1\n");
+ else
+ surface_acpi->bat1_attached = 1;
+
+ ret = surface_acpi_event_handler(SUR_BAT2_INFO_CHANGE);
+ if (ACPI_FAILURE(ret))
+ pr_err("surface_acpi: Error attaching BAT2\n");
+ else
+ surface_acpi->bat2_attached = 1;
+
+ ret = surface_acpi_event_handler(SUR_PSU_INFO_CHANGE);
+ if (ACPI_FAILURE(ret))
+ pr_err("surface_acpi: Error registering PSU\n");
+ else
+ surface_acpi->psu_registered = 1;
+}
+
+static acpi_status surface_acpi_ssh_initialize(void)
+{
+ acpi_status status;
+
+ if (acpi_has_method(surface_acpi->ssh_dev->handle, SUR_METHOD_INI)) {
+ status = acpi_evaluate_object(surface_acpi->ssh_dev->handle,
+ SUR_METHOD_INI, NULL, NULL);
+
+ if (ACPI_FAILURE(status)) {
+ pr_err("surface_acpi: ACPI event failure status %s\n",
+ acpi_format_exception(status));
+ return AE_ERROR;
+ }
+ }
+ else
+ return AE_NOT_FOUND;
+
+ return AE_OK;
+}
+
+static int bat1_proc_show(struct seq_file *m, void *v)
+{
+ seq_printf(m, "attached: %d\n", surface_acpi->bat1_attached);
+ return 0;
+}
+
+static int bat1_proc_open(struct inode *inode, struct file *file)
+{
+ return single_open(file, bat1_proc_show, PDE_DATA(inode));
+}
+
+static const struct file_operations bat1_proc_fops = {
+ .owner = THIS_MODULE,
+ .open = bat1_proc_open,
+ .read = seq_read,
+ .llseek = seq_lseek,
+ .release = single_release,
+};
+
+static int bat2_proc_show(struct seq_file *m, void *v)
+{
+ seq_printf(m, "attached: %d\n", surface_acpi->bat2_attached);
+ return 0;
+}
+
+static int bat2_proc_open(struct inode *inode, struct file *file)
+{
+ return single_open(file, bat2_proc_show, PDE_DATA(inode));
+}
+
+static const struct file_operations bat2_proc_fops = {
+ .owner = THIS_MODULE,
+ .open = bat2_proc_open,
+ .read = seq_read,
+ .llseek = seq_lseek,
+ .release = single_release,
+};
+
+static int psu_proc_show(struct seq_file *m, void *v)
+{
+ seq_printf(m, "registered: %d\n", surface_acpi->psu_registered);
+ return 0;
+}
+
+static int psu_proc_open(struct inode *inode, struct file *file)
+{
+ return single_open(file, psu_proc_show, PDE_DATA(inode));
+}
+
+static const struct file_operations psu_proc_fops = {
+ .owner = THIS_MODULE,
+ .open = psu_proc_open,
+ .read = seq_read,
+ .llseek = seq_lseek,
+ .release = single_release,
+};
+
+static int version_proc_show(struct seq_file *m, void *v)
+{
+ seq_printf(m, "driver: %s\n", SURFACE_ACPI_VERSION);
+ return 0;
+}
+
+static int version_proc_open(struct inode *inode, struct file *file)
+{
+ return single_open(file, version_proc_show, PDE_DATA(inode));
+}
+
+static const struct file_operations version_proc_fops = {
+ .owner = THIS_MODULE,
+ .open = version_proc_open,
+ .read = seq_read,
+ .llseek = seq_lseek,
+ .release = single_release,
+};
+
+static void create_surface_proc_entries(void)
+{
+ proc_create_data("BAT1", 0, surface_proc_dir,
+ &bat1_proc_fops, surface_acpi->bat1_attached);
+ proc_create_data("BAT2", 0, surface_proc_dir,
+ &bat2_proc_fops, surface_acpi->bat2_attached);
+ proc_create_data("ADP1", 0, surface_proc_dir,
+ &psu_proc_fops, surface_acpi->psu_registered);
+ proc_create_data("version", 0, surface_proc_dir,
+ &version_proc_fops, SURFACE_ACPI_VERSION);
+}
+
+static void remove_surface_proc_entries(void)
+{
+ remove_proc_entry("BAT1", surface_proc_dir);
+ remove_proc_entry("BAT2", surface_proc_dir);
+ remove_proc_entry("ADP1", surface_proc_dir);
+ remove_proc_entry("version", surface_proc_dir);
+}
+
+static void surface_acpi_notify(struct acpi_device *dev, u32 event)
+{
+ pr_info("surface_acpi: Event received %x\n", event);
+}
+
+static void surface_acpi_register_rqst_handler(void)
+{
+ acpi_status status;
+
+ status = acpi_get_handle(NULL, SUR_SAN_RQST, &surface_acpi->rqst_handle);
+ if (ACPI_FAILURE(status)) {
+ pr_err("surface_acpi: ACPI event failure status %s\n",
+ acpi_format_exception(status));
+ }
+}
+
+static void surface_acpi_register_rqsx_handler(void)
+{
+ acpi_status status;
+
+ status = acpi_get_handle(NULL, SUR_SAN_RQSX, &surface_acpi->rqsx_handle);
+ if (ACPI_FAILURE(status)) {
+ pr_err("surface_acpi: ACPI event failure status %s\n",
+ acpi_format_exception(status));
+ }
+}
+
+static acpi_status surface_acpi_walk_callback(acpi_handle handle, u32 level,
+ void *context, void **return_value)
+{
+ struct acpi_device_info *info;
+
+ if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
+ pr_warn("method: name: %4.4s, args %X\n",
+ (char *)&info->name, info->param_count);
+
+ kfree(info);
+ }
+
+ return AE_OK;
+}
+
+static void surface_acpi_walk_namespace(struct acpi_device *dev)
+{
+ acpi_status status;
+
+ status = acpi_walk_namespace(ACPI_TYPE_METHOD,
+ dev->handle, 1, surface_acpi_walk_callback,
+ NULL, NULL, NULL);
+ if (ACPI_FAILURE(status))
+ pr_warn("surface_acpi: Unable to walk acpi resources\n");
+}
+
+static int surface_acpi_add(struct acpi_device *dev)
+{
+ if (!surface_acpi)
+ {
+ surface_acpi = kzalloc(sizeof(*surface_acpi), GFP_KERNEL);
+ if (!surface_acpi)
+ return AE_NO_MEMORY;
+ }
+
+ if (acpi_has_method(dev->handle, SUR_METHOD_DSM))
+ {
+ pr_info("surface_acpi: Attaching device MSHW0091\n");
+
+ surface_acpi->san_dev = dev;
+ surface_acpi->handle = dev->handle;
+
+ surface_acpi_walk_namespace(surface_acpi->san_dev);
+ surface_acpi_check_status(surface_acpi->san_dev);
+
+ surface_acpi_register_rqst_handler();
+ surface_acpi_register_rqsx_handler();
+
+ surface_acpi_san_reg();
+ surface_acpi_san_load();
+
+ create_surface_proc_entries();
+ }
+ else if (acpi_has_method(dev->handle, SUR_METHOD_CRS))
+ {
+ pr_info("surface_acpi: Attaching device MSHW0084\n");
+
+ surface_acpi->ssh_dev = dev;
+
+ surface_acpi_walk_namespace(surface_acpi->ssh_dev);
+ surface_acpi_check_status(surface_acpi->ssh_dev);
+
+ surface_acpi_ssh_initialize();
+ //surface_acpi_ssh_load();
+ }
+ else
+ {
+ pr_info("surface_acpi: Attaching device\n");
+ }
+
+ device_init_wakeup(&dev->dev, true);
+
+ return AE_OK;
+}
+
+static int surface_acpi_remove(struct acpi_device *dev)
+{
+ remove_surface_proc_entries();
+
+ return AE_OK;
+}
+
+static const struct acpi_device_id surface_device_ids[] = {
+ {"MSHW0084", 0},
+ {"MSHW0091", 0},
+ {"", 0},
+};
+MODULE_DEVICE_TABLE(acpi, surface_device_ids);
+
+static struct acpi_driver surface_acpi_driver = {
+ .name = "surface_acpi",
+ .owner = THIS_MODULE,
+ .ids = surface_device_ids,
+ .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
+ .ops = {
+ .add = surface_acpi_add,
+ .remove = surface_acpi_remove,
+ .notify = surface_acpi_notify,
+ },
+};
+
+static int __init surface_acpi_init(void)
+{
+ int ret;
+
+ pr_info("surface_acpi: Microsoft Surface ACPI Driver version %s\n",
+ SURFACE_ACPI_VERSION);
+
+ surface_proc_dir = proc_mkdir(PROC_SURFACE, acpi_root_dir);
+ if (!surface_proc_dir) {
+ pr_err("surface_acpi: Unable to create proc dir " PROC_SURFACE "\n");
+ return -ENODEV;
+ }
+
+ ret = acpi_bus_register_driver(&surface_acpi_driver);
+ if (ret) {
+ pr_err("surface_acpi: Failed to register ACPI driver: %d\n", ret);
+ remove_proc_entry(PROC_SURFACE, acpi_root_dir);
+ }
+
+ return ret;
+}
+
+static void __exit surface_acpi_exit(void)
+{
+ acpi_bus_unregister_driver(&surface_acpi_driver);
+ if (surface_proc_dir)
+ remove_proc_entry(PROC_SURFACE, acpi_root_dir);
+}
+
+module_init(surface_acpi_init);
+module_exit(surface_acpi_exit);
+
+MODULE_AUTHOR("Jake Day");
+MODULE_DESCRIPTION("Microsoft Surface ACPI Driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/platform/x86/surface_acpi.h b/drivers/platform/x86/surface_acpi.h
new file mode 100644
index 000000000000..5b6627c4d6f1
--- /dev/null
+++ b/drivers/platform/x86/surface_acpi.h
@@ -0,0 +1,18 @@
+/*
+ * surface_acpi.h - Microsoft Surface ACPI Driver
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * The full GNU General Public License is included in this distribution in
+ * the file called "COPYING".
+ */
+
+acpi_status surface_acpi_event_handler(u32 event);
diff --git a/drivers/platform/x86/surface_i2c.c b/drivers/platform/x86/surface_i2c.c
new file mode 100644
index 000000000000..fb2cf0cae72f
--- /dev/null
+++ b/drivers/platform/x86/surface_i2c.c
@@ -0,0 +1,696 @@
+/*
+ * surface_i2c.c - Microsoft Surface I2C Driver
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * The full GNU General Public License is included in this distribution in
+ * the file called "COPYING".
+ */
+
+#include <linux/kernel.h>
+#include <linux/i2c.h>
+#include <linux/slab.h>
+#include <linux/acpi.h>
+#include <linux/module.h>
+#include <linux/uuid.h>
+#include <linux/kthread.h>
+#include <linux/freezer.h>
+#include <asm/unaligned.h>
+
+#include "surface_acpi.h"
+
+#define POLL_INTERVAL (HZ * 2)
+
+struct surface_i2c_data {
+ struct i2c_client *adp1;
+ struct i2c_client *bat0;
+ unsigned short notify_version;
+ struct task_struct *poll_task;
+ bool kthread_running;
+ bool charging;
+ bool bat_charging;
+ u8 trip_point;
+ s32 full_capacity;
+};
+
+struct surface_i2c_lookup {
+ struct surface_i2c_data *cdata;
+ unsigned int n;
+ unsigned int index;
+ int addr;
+};
+
+struct surface_i2c_handler_data {
+ struct acpi_connection_info info;
+ struct i2c_client *client;
+};
+
+struct bix {
+ u32 revision;
+ u32 power_unit;
+ u32 design_capacity;
+ u32 last_full_charg_capacity;
+ u32 battery_technology;
+ u32 design_voltage;
+ u32 design_capacity_of_warning;
+ u32 design_capacity_of_low;
+ u32 cycle_count;
+ u32 measurement_accuracy;
+ u32 max_sampling_time;
+ u32 min_sampling_time;
+ u32 max_average_interval;
+ u32 min_average_interval;
+ u32 battery_capacity_granularity_1;
+ u32 battery_capacity_granularity_2;
+ char model[10];
+ char serial[10];
+ char type[10];
+ char OEM[10];
+} __packed;
+
+struct bst {
+ u32 battery_state;
+ s32 battery_present_rate;
+ u32 battery_remaining_capacity;
+ u32 battery_present_voltage;
+} __packed;
+
+struct gsb_command {
+ u8 arg0;
+ u8 arg1;
+ u8 arg2;
+} __packed;
+
+struct gsb_buffer {
+ u8 status;
+ u8 len;
+ u8 ret;
+ union {
+ struct gsb_command cmd;
+ struct bst bst;
+ struct bix bix;
+ } __packed;
+} __packed;
+
+#define ACPI_BATTERY_STATE_DISCHARGING 0x1
+#define ACPI_BATTERY_STATE_CHARGING 0x2
+#define ACPI_BATTERY_STATE_CRITICAL 0x4
+
+#define surface_i2c_CMD_DEST_BAT0 0x01
+#define surface_i2c_CMD_DEST_ADP1 0x03
+
+#define surface_i2c_CMD_BAT0_STA 0x01
+#define surface_i2c_CMD_BAT0_BIX 0x02
+#define surface_i2c_CMD_BAT0_BCT 0x03
+#define surface_i2c_CMD_BAT0_BTM 0x04
+#define surface_i2c_CMD_BAT0_BST 0x05
+#define surface_i2c_CMD_BAT0_BTP 0x06
+#define surface_i2c_CMD_ADP1_PSR 0x07
+#define surface_i2c_CMD_BAT0_PSOC 0x09
+#define surface_i2c_CMD_BAT0_PMAX 0x0A
+#define surface_i2c_CMD_BAT0_PSRC 0x0B
+#define surface_i2c_CMD_BAT0_CHGI 0x0C
+#define surface_i2c_CMD_BAT0_ARTG 0x0D
+
+#define surface_i2c_NOTIFY_GET_VERSION 0x00
+#define surface_i2c_NOTIFY_ADP1 0x01
+#define surface_i2c_NOTIFY_BAT0_BST 0x02
+#define surface_i2c_NOTIFY_BAT0_BIX 0x05
+
+#define surface_i2c_ADP1_REG_PSR 0x03
+
+#define surface_i2c_BAT0_REG_CAPACITY 0x0c
+#define surface_i2c_BAT0_REG_FULL_CHG_CAPACITY 0x0e
+#define surface_i2c_BAT0_REG_DESIGN_CAPACITY 0x40
+#define surface_i2c_BAT0_REG_VOLTAGE 0x08
+#define surface_i2c_BAT0_REG_RATE 0x14
+#define surface_i2c_BAT0_REG_OEM 0x45
+#define surface_i2c_BAT0_REG_TYPE 0x4e
+#define surface_i2c_BAT0_REG_SERIAL_NO 0x56
+#define surface_i2c_BAT0_REG_CYCLE_CNT 0x6e
+
+#define surface_i2c_EV_2_5 0x1ff
+
+static int surface_i2c_read_block(struct i2c_client *client, u8 reg, u8 *buf,
+ int len)
+{
+ int status, i;
+
+ for (i = 0; i < len; i++) {
+ status = i2c_smbus_read_byte_data(client, reg + i);
+ if (status < 0) {
+ buf[i] = 0xff;
+ continue;
+ }
+
+ buf[i] = (u8)status;
+ }
+
+ return 0;
+}
+
+static int
+surface_i2c_notify(struct surface_i2c_data *cdata, u8 arg1, u8 arg2,
+ unsigned int *ret_value)
+{
+ /*static const guid_t surface_i2c_guid =
+ GUID_INIT(0x93b666c5, 0x70c6, 0x469f,
+ 0xa2, 0x15, 0x3d, 0x48, 0x7c, 0x91, 0xab, 0x3c);*/
+
+ struct acpi_device *adev;
+ acpi_handle handle;
+ acpi_status status;
+
+ handle = ACPI_HANDLE(&cdata->adp1->dev);
+ if (!handle || acpi_bus_get_device(handle, &adev))
+ return -ENODEV;
+
+ *ret_value = 0;
+
+ status = surface_acpi_event_handler(arg2);
+ if (ACPI_FAILURE(status)) {
+ pr_err("surface_i2c: ACPI event failure status %s\n",
+ acpi_format_exception(status));
+ }
+
+ return 0;
+}
+
+static const struct bix default_bix = {
+ .revision = 0x00,
+ .power_unit = 0x00,
+ .design_capacity = 0x1734,
+ .last_full_charg_capacity = 0x1734,
+ .battery_technology = 0x01,
+ .design_voltage = 0x1d92,
+ .design_capacity_of_warning = 0xc8,
+ .design_capacity_of_low = 0xc8,
+ .battery_capacity_granularity_1 = 0x45,
+ .battery_capacity_granularity_2 = 0x11,
+ .cycle_count = 0x01,
+ .measurement_accuracy = 0x00015F90,
+ .max_sampling_time = 0x03E8,
+ .min_sampling_time = 0x03E8,
+ .max_average_interval = 0x03E8,
+ .min_average_interval = 0x03E8,
+ .model = "PNP0C0A",
+ .serial = "1234567890",
+ .type = "SDS-BAT",
+ .OEM = "MICROSOFT",
+};
+
+static int surface_i2c_bix(struct surface_i2c_data *cdata, struct bix *bix)
+{
+ struct i2c_client *client = cdata->bat0;
+ int ret;
+ char buf[10];
+
+ *bix = default_bix;
+
+ /* get design capacity */
+ ret = i2c_smbus_read_word_data(client, surface_i2c_BAT0_REG_DESIGN_CAPACITY);
+ if (ret < 0) {
+ dev_err(&client->dev, "Error reading design capacity: %d\n", ret);
+ return ret;
+ }
+ bix->design_capacity = le16_to_cpu(ret);
+
+ /* get last full charge capacity */
+ ret = i2c_smbus_read_word_data(client, surface_i2c_BAT0_REG_FULL_CHG_CAPACITY);
+ if (ret < 0) {
+ dev_err(&client->dev, "Error reading last full charge capacity: %d\n", ret);
+ return ret;
+ }
+ bix->last_full_charg_capacity = le16_to_cpu(ret);
+
+ /* get serial number */
+ ret = surface_i2c_read_block(client, surface_i2c_BAT0_REG_SERIAL_NO,
+ buf, 10);
+ if (ret) {
+ dev_err(&client->dev, "Error reading serial no: %d\n", ret);
+ return ret;
+ }
+ memcpy(bix->serial, buf + 7, 3);
+ memcpy(bix->serial + 3, buf, 6);
+ bix->serial[9] = '\0';
+
+ /* get cycle count */
+ ret = i2c_smbus_read_word_data(client, surface_i2c_BAT0_REG_CYCLE_CNT);
+ if (ret < 0) {
+ dev_err(&client->dev, "Error reading cycle count: %d\n", ret);
+ return ret;
+ }
+ bix->cycle_count = le16_to_cpu(ret);
+
+ /* get OEM name */
+ ret = surface_i2c_read_block(client, surface_i2c_BAT0_REG_OEM, buf, 4);
+ if (ret) {
+ dev_err(&client->dev, "Error reading cycle count: %d\n", ret);
+ return ret;
+ }
+ memcpy(bix->OEM, buf, 3);
+ bix->OEM[4] = '\0';
+
+ return 0;
+}
+
+static int surface_i2c_bst(struct surface_i2c_data *cdata, struct bst *bst)
+{
+ struct i2c_client *client = cdata->bat0;
+ int rate, capacity, voltage, state;
+ s16 tmp;
+
+ rate = i2c_smbus_read_word_data(client, surface_i2c_BAT0_REG_RATE);
+ if (rate < 0)
+ return rate;
+
+ capacity = i2c_smbus_read_word_data(client, surface_i2c_BAT0_REG_CAPACITY);
+ if (capacity < 0)
+ return capacity;
+
+ voltage = i2c_smbus_read_word_data(client, surface_i2c_BAT0_REG_VOLTAGE);
+ if (voltage < 0)
+ return voltage;
+
+ tmp = le16_to_cpu(rate);
+ bst->battery_present_rate = abs((s32)tmp);
+
+ state = 0;
+ if ((s32) tmp > 0)
+ state |= ACPI_BATTERY_STATE_CHARGING;
+ else if ((s32) tmp < 0)
+ state |= ACPI_BATTERY_STATE_DISCHARGING;
+ bst->battery_state = state;
+
+ bst->battery_remaining_capacity = le16_to_cpu(capacity);
+ bst->battery_present_voltage = le16_to_cpu(voltage);
+
+ return 0;
+}
+
+static int surface_i2c_adp_psr(struct surface_i2c_data *cdata)
+{
+ struct i2c_client *client = cdata->adp1;
+ int ret;
+
+ ret = i2c_smbus_read_byte_data(client, surface_i2c_ADP1_REG_PSR);
+ if (ret < 0)
+ return ret;
+
+ return ret;
+}
+
+static int surface_i2c_isr(struct surface_i2c_data *cdata)
+{
+ struct bst bst;
+ struct bix bix;
+ int ret;
+ bool status, bat_status;
+
+ ret = surface_i2c_adp_psr(cdata);
+ if (ret < 0)
+ return ret;
+
+ status = ret;
+
+ if (status != cdata->charging)
+ surface_i2c_notify(cdata, cdata->notify_version,
+ surface_i2c_NOTIFY_ADP1, &ret);
+
+ cdata->charging = status;
+
+ ret = surface_i2c_bst(cdata, &bst);
+ if (ret < 0)
+ return ret;
+
+ bat_status = bst.battery_state;
+
+ if (bat_status != cdata->bat_charging)
+ surface_i2c_notify(cdata, cdata->notify_version,
+ surface_i2c_NOTIFY_BAT0_BST, &ret);
+
+ cdata->bat_charging = bat_status;
+
+ ret = surface_i2c_bix(cdata, &bix);
+ if (ret < 0)
+ return ret;
+ if (bix.last_full_charg_capacity != cdata->full_capacity)
+ surface_i2c_notify(cdata, cdata->notify_version,
+ surface_i2c_NOTIFY_BAT0_BIX, &ret);
+
+ cdata->full_capacity = bix.last_full_charg_capacity;
+
+ return 0;
+}
+
+static int surface_i2c_poll_task(void *data)
+{
+ struct surface_i2c_data *cdata = data;
+ int ret = 0;
+
+ cdata->kthread_running = true;
+
+ set_freezable();
+
+ while (!kthread_should_stop()) {
+ schedule_timeout_interruptible(POLL_INTERVAL);
+ try_to_freeze();
+ ret = surface_i2c_isr(data);
+ if (ret)
+ goto out;
+ }
+
+out:
+ cdata->kthread_running = false;
+ return ret;
+}
+
+static acpi_status
+surface_i2c_space_handler(u32 function, acpi_physical_address command,
+ u32 bits, u64 *value64,
+ void *handler_context, void *region_context)
+{
+ struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
+ struct surface_i2c_handler_data *data = handler_context;
+ struct acpi_connection_info *info = &data->info;
+ struct acpi_resource_i2c_serialbus *sb;
+ struct i2c_client *client = data->client;
+ struct surface_i2c_data *cdata = i2c_get_clientdata(client);
+ struct acpi_resource *ares;
+ u32 accessor_type = function >> 16;
+ acpi_status ret;
+ int status = 1;
+
+ ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
+ if (ACPI_FAILURE(ret))
+ return ret;
+
+ if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
+ ret = AE_BAD_PARAMETER;
+ goto err;
+ }
+
+ sb = &ares->data.i2c_serial_bus;
+ if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
+ ret = AE_BAD_PARAMETER;
+ goto err;
+ }
+
+ if (accessor_type != ACPI_GSB_ACCESS_ATTRIB_RAW_PROCESS) {
+ ret = AE_BAD_PARAMETER;
+ goto err;
+ }
+
+ if (gsb->cmd.arg0 == surface_i2c_CMD_DEST_ADP1 &&
+ gsb->cmd.arg1 == surface_i2c_CMD_ADP1_PSR) {
+ ret = surface_i2c_adp_psr(cdata);
+ if (ret >= 0) {
+ status = ret;
+ ret = 0;
+ }
+ goto out;
+ }
+
+ if (gsb->cmd.arg0 != surface_i2c_CMD_DEST_BAT0) {
+ ret = AE_BAD_PARAMETER;
+ goto err;
+ }
+
+ switch (gsb->cmd.arg1) {
+ case surface_i2c_CMD_BAT0_STA:
+ status = 1;
+ ret = 0;
+ break;
+ case surface_i2c_CMD_BAT0_BIX:
+ status = 1;
+ ret = surface_i2c_bix(cdata, &gsb->bix);
+ break;
+ case surface_i2c_CMD_BAT0_BTP:
+ status = 1;
+ ret = 0;
+ cdata->trip_point = gsb->cmd.arg2;
+ break;
+ case surface_i2c_CMD_BAT0_BST:
+ status = 1;
+ ret = surface_i2c_bst(cdata, &gsb->bst);
+ break;
+ default:
+ pr_info("command(0x%02x) is not supported.\n", gsb->cmd.arg1);
+ ret = AE_BAD_PARAMETER;
+ goto err;
+ }