-
Notifications
You must be signed in to change notification settings - Fork 54.7k
/
Copy pathideapad-laptop.c
2347 lines (1902 loc) · 57.6 KB
/
ideapad-laptop.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-or-later
/*
* ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
*
* Copyright © 2010 Intel Corporation
* Copyright © 2010 David Woodhouse <dwmw2@infradead.org>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/acpi.h>
#include <linux/backlight.h>
#include <linux/bitfield.h>
#include <linux/bitops.h>
#include <linux/bug.h>
#include <linux/cleanup.h>
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/dmi.h>
#include <linux/i8042.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/input/sparse-keymap.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/platform_profile.h>
#include <linux/rfkill.h>
#include <linux/seq_file.h>
#include <linux/sysfs.h>
#include <linux/types.h>
#include <linux/wmi.h>
#include "ideapad-laptop.h"
#include <acpi/video.h>
#include <dt-bindings/leds/common.h>
#define IDEAPAD_RFKILL_DEV_NUM 3
enum {
CFG_CAP_BT_BIT = 16,
CFG_CAP_3G_BIT = 17,
CFG_CAP_WIFI_BIT = 18,
CFG_CAP_CAM_BIT = 19,
/*
* These are OnScreenDisplay support bits that can be useful to determine
* whether a hotkey exists/should show OSD. But they aren't particularly
* meaningful since they were introduced later, i.e. 2010 IdeaPads
* don't have these, but they still have had OSD for hotkeys.
*/
CFG_OSD_NUMLK_BIT = 27,
CFG_OSD_CAPSLK_BIT = 28,
CFG_OSD_MICMUTE_BIT = 29,
CFG_OSD_TOUCHPAD_BIT = 30,
CFG_OSD_CAM_BIT = 31,
};
enum {
GBMD_CONSERVATION_STATE_BIT = 5,
};
enum {
SBMC_CONSERVATION_ON = 3,
SBMC_CONSERVATION_OFF = 5,
};
enum {
HALS_KBD_BL_SUPPORT_BIT = 4,
HALS_KBD_BL_STATE_BIT = 5,
HALS_USB_CHARGING_SUPPORT_BIT = 6,
HALS_USB_CHARGING_STATE_BIT = 7,
HALS_FNLOCK_SUPPORT_BIT = 9,
HALS_FNLOCK_STATE_BIT = 10,
HALS_HOTKEYS_PRIMARY_BIT = 11,
};
enum {
SALS_KBD_BL_ON = 0x8,
SALS_KBD_BL_OFF = 0x9,
SALS_USB_CHARGING_ON = 0xa,
SALS_USB_CHARGING_OFF = 0xb,
SALS_FNLOCK_ON = 0xe,
SALS_FNLOCK_OFF = 0xf,
};
enum {
VPCCMD_R_VPC1 = 0x10,
VPCCMD_R_BL_MAX,
VPCCMD_R_BL,
VPCCMD_W_BL,
VPCCMD_R_WIFI,
VPCCMD_W_WIFI,
VPCCMD_R_BT,
VPCCMD_W_BT,
VPCCMD_R_BL_POWER,
VPCCMD_R_NOVO,
VPCCMD_R_VPC2,
VPCCMD_R_TOUCHPAD,
VPCCMD_W_TOUCHPAD,
VPCCMD_R_CAMERA,
VPCCMD_W_CAMERA,
VPCCMD_R_3G,
VPCCMD_W_3G,
VPCCMD_R_ODD, /* 0x21 */
VPCCMD_W_FAN,
VPCCMD_R_RF,
VPCCMD_W_RF,
VPCCMD_W_YMC = 0x2A,
VPCCMD_R_FAN = 0x2B,
VPCCMD_R_SPECIAL_BUTTONS = 0x31,
VPCCMD_W_BL_POWER = 0x33,
};
/*
* These correspond to the number of supported states - 1
* Future keyboard types may need a new system, if there's a collision
* KBD_BL_TRISTATE_AUTO has no way to report or set the auto state
* so it effectively has 3 states, but needs to handle 4
*/
enum {
KBD_BL_STANDARD = 1,
KBD_BL_TRISTATE = 2,
KBD_BL_TRISTATE_AUTO = 3,
};
#define KBD_BL_QUERY_TYPE 0x1
#define KBD_BL_TRISTATE_TYPE 0x5
#define KBD_BL_TRISTATE_AUTO_TYPE 0x7
#define KBD_BL_COMMAND_GET 0x2
#define KBD_BL_COMMAND_SET 0x3
#define KBD_BL_COMMAND_TYPE GENMASK(7, 4)
#define KBD_BL_GET_BRIGHTNESS GENMASK(15, 1)
#define KBD_BL_SET_BRIGHTNESS GENMASK(19, 16)
#define KBD_BL_KBLC_CHANGED_EVENT 12
struct ideapad_dytc_priv {
enum platform_profile_option current_profile;
struct platform_profile_handler pprof;
struct mutex mutex; /* protects the DYTC interface */
struct ideapad_private *priv;
};
struct ideapad_rfk_priv {
int dev;
struct ideapad_private *priv;
};
struct ideapad_private {
struct acpi_device *adev;
struct mutex vpc_mutex; /* protects the VPC calls */
struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
struct platform_device *platform_device;
struct input_dev *inputdev;
struct backlight_device *blightdev;
struct ideapad_dytc_priv *dytc;
struct dentry *debug;
unsigned long cfg;
unsigned long r_touchpad_val;
struct {
bool conservation_mode : 1;
bool dytc : 1;
bool fan_mode : 1;
bool fn_lock : 1;
bool set_fn_lock_led : 1;
bool hw_rfkill_switch : 1;
bool kbd_bl : 1;
bool touchpad_ctrl_via_ec : 1;
bool ctrl_ps2_aux_port : 1;
bool usb_charging : 1;
bool ymc_ec_trigger : 1;
} features;
struct {
bool initialized;
int type;
struct led_classdev led;
unsigned int last_brightness;
} kbd_bl;
struct {
bool initialized;
struct led_classdev led;
unsigned int last_brightness;
} fn_lock;
};
static bool no_bt_rfkill;
module_param(no_bt_rfkill, bool, 0444);
MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
static bool allow_v4_dytc;
module_param(allow_v4_dytc, bool, 0444);
MODULE_PARM_DESC(allow_v4_dytc,
"Enable DYTC version 4 platform-profile support. "
"If you need this please report this to: platform-driver-x86@vger.kernel.org");
static bool hw_rfkill_switch;
module_param(hw_rfkill_switch, bool, 0444);
MODULE_PARM_DESC(hw_rfkill_switch,
"Enable rfkill support for laptops with a hw on/off wifi switch/slider. "
"If you need this please report this to: platform-driver-x86@vger.kernel.org");
static bool set_fn_lock_led;
module_param(set_fn_lock_led, bool, 0444);
MODULE_PARM_DESC(set_fn_lock_led,
"Enable driver based updates of the fn-lock LED on fn-lock changes. "
"If you need this please report this to: platform-driver-x86@vger.kernel.org");
static bool ctrl_ps2_aux_port;
module_param(ctrl_ps2_aux_port, bool, 0444);
MODULE_PARM_DESC(ctrl_ps2_aux_port,
"Enable driver based PS/2 aux port en-/dis-abling on touchpad on/off toggle. "
"If you need this please report this to: platform-driver-x86@vger.kernel.org");
static bool touchpad_ctrl_via_ec;
module_param(touchpad_ctrl_via_ec, bool, 0444);
MODULE_PARM_DESC(touchpad_ctrl_via_ec,
"Enable registering a 'touchpad' sysfs-attribute which can be used to manually "
"tell the EC to enable/disable the touchpad. This may not work on all models.");
static bool ymc_ec_trigger __read_mostly;
module_param(ymc_ec_trigger, bool, 0444);
MODULE_PARM_DESC(ymc_ec_trigger,
"Enable EC triggering work-around to force emitting tablet mode events. "
"If you need this please report this to: platform-driver-x86@vger.kernel.org");
/*
* shared data
*/
static struct ideapad_private *ideapad_shared;
static DEFINE_MUTEX(ideapad_shared_mutex);
static int ideapad_shared_init(struct ideapad_private *priv)
{
int ret;
guard(mutex)(&ideapad_shared_mutex);
if (!ideapad_shared) {
ideapad_shared = priv;
ret = 0;
} else {
dev_warn(&priv->adev->dev, "found multiple platform devices\n");
ret = -EINVAL;
}
return ret;
}
static void ideapad_shared_exit(struct ideapad_private *priv)
{
guard(mutex)(&ideapad_shared_mutex);
if (ideapad_shared == priv)
ideapad_shared = NULL;
}
/*
* ACPI Helpers
*/
#define IDEAPAD_EC_TIMEOUT 200 /* in ms */
static int eval_int(acpi_handle handle, const char *name, unsigned long *res)
{
unsigned long long result;
acpi_status status;
status = acpi_evaluate_integer(handle, (char *)name, NULL, &result);
if (ACPI_FAILURE(status))
return -EIO;
*res = result;
return 0;
}
static int eval_int_with_arg(acpi_handle handle, const char *name, unsigned long arg,
unsigned long *res)
{
struct acpi_object_list params;
unsigned long long result;
union acpi_object in_obj;
acpi_status status;
params.count = 1;
params.pointer = &in_obj;
in_obj.type = ACPI_TYPE_INTEGER;
in_obj.integer.value = arg;
status = acpi_evaluate_integer(handle, (char *)name, ¶ms, &result);
if (ACPI_FAILURE(status))
return -EIO;
if (res)
*res = result;
return 0;
}
static int exec_simple_method(acpi_handle handle, const char *name, unsigned long arg)
{
acpi_status status = acpi_execute_simple_method(handle, (char *)name, arg);
return ACPI_FAILURE(status) ? -EIO : 0;
}
static int eval_gbmd(acpi_handle handle, unsigned long *res)
{
return eval_int(handle, "GBMD", res);
}
static int exec_sbmc(acpi_handle handle, unsigned long arg)
{
return exec_simple_method(handle, "SBMC", arg);
}
static int eval_hals(acpi_handle handle, unsigned long *res)
{
return eval_int(handle, "HALS", res);
}
static int exec_sals(acpi_handle handle, unsigned long arg)
{
return exec_simple_method(handle, "SALS", arg);
}
static int exec_kblc(acpi_handle handle, unsigned long arg)
{
return exec_simple_method(handle, "KBLC", arg);
}
static int eval_kblc(acpi_handle handle, unsigned long cmd, unsigned long *res)
{
return eval_int_with_arg(handle, "KBLC", cmd, res);
}
static int eval_dytc(acpi_handle handle, unsigned long cmd, unsigned long *res)
{
return eval_int_with_arg(handle, "DYTC", cmd, res);
}
static int eval_vpcr(acpi_handle handle, unsigned long cmd, unsigned long *res)
{
return eval_int_with_arg(handle, "VPCR", cmd, res);
}
static int eval_vpcw(acpi_handle handle, unsigned long cmd, unsigned long data)
{
struct acpi_object_list params;
union acpi_object in_obj[2];
acpi_status status;
params.count = 2;
params.pointer = in_obj;
in_obj[0].type = ACPI_TYPE_INTEGER;
in_obj[0].integer.value = cmd;
in_obj[1].type = ACPI_TYPE_INTEGER;
in_obj[1].integer.value = data;
status = acpi_evaluate_object(handle, "VPCW", ¶ms, NULL);
if (ACPI_FAILURE(status))
return -EIO;
return 0;
}
static int read_ec_data(acpi_handle handle, unsigned long cmd, unsigned long *data)
{
unsigned long end_jiffies, val;
int err;
err = eval_vpcw(handle, 1, cmd);
if (err)
return err;
end_jiffies = jiffies + msecs_to_jiffies(IDEAPAD_EC_TIMEOUT) + 1;
while (time_before(jiffies, end_jiffies)) {
schedule();
err = eval_vpcr(handle, 1, &val);
if (err)
return err;
if (val == 0)
return eval_vpcr(handle, 0, data);
}
acpi_handle_err(handle, "timeout in %s\n", __func__);
return -ETIMEDOUT;
}
static int write_ec_cmd(acpi_handle handle, unsigned long cmd, unsigned long data)
{
unsigned long end_jiffies, val;
int err;
err = eval_vpcw(handle, 0, data);
if (err)
return err;
err = eval_vpcw(handle, 1, cmd);
if (err)
return err;
end_jiffies = jiffies + msecs_to_jiffies(IDEAPAD_EC_TIMEOUT) + 1;
while (time_before(jiffies, end_jiffies)) {
schedule();
err = eval_vpcr(handle, 1, &val);
if (err)
return err;
if (val == 0)
return 0;
}
acpi_handle_err(handle, "timeout in %s\n", __func__);
return -ETIMEDOUT;
}
/*
* debugfs
*/
static int debugfs_status_show(struct seq_file *s, void *data)
{
struct ideapad_private *priv = s->private;
unsigned long value;
guard(mutex)(&priv->vpc_mutex);
if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
seq_printf(s, "Backlight max: %lu\n", value);
if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
seq_printf(s, "Backlight now: %lu\n", value);
if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
seq_printf(s, "BL power value: %s (%lu)\n", value ? "on" : "off", value);
seq_puts(s, "=====================\n");
if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
seq_printf(s, "Radio status: %s (%lu)\n", value ? "on" : "off", value);
if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
seq_printf(s, "Wifi status: %s (%lu)\n", value ? "on" : "off", value);
if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
seq_printf(s, "BT status: %s (%lu)\n", value ? "on" : "off", value);
if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
seq_printf(s, "3G status: %s (%lu)\n", value ? "on" : "off", value);
seq_puts(s, "=====================\n");
if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
seq_printf(s, "Touchpad status: %s (%lu)\n", value ? "on" : "off", value);
if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
seq_printf(s, "Camera status: %s (%lu)\n", value ? "on" : "off", value);
seq_puts(s, "=====================\n");
if (!eval_gbmd(priv->adev->handle, &value))
seq_printf(s, "GBMD: %#010lx\n", value);
if (!eval_hals(priv->adev->handle, &value))
seq_printf(s, "HALS: %#010lx\n", value);
return 0;
}
DEFINE_SHOW_ATTRIBUTE(debugfs_status);
static int debugfs_cfg_show(struct seq_file *s, void *data)
{
struct ideapad_private *priv = s->private;
seq_printf(s, "_CFG: %#010lx\n\n", priv->cfg);
seq_puts(s, "Capabilities:");
if (test_bit(CFG_CAP_BT_BIT, &priv->cfg))
seq_puts(s, " bluetooth");
if (test_bit(CFG_CAP_3G_BIT, &priv->cfg))
seq_puts(s, " 3G");
if (test_bit(CFG_CAP_WIFI_BIT, &priv->cfg))
seq_puts(s, " wifi");
if (test_bit(CFG_CAP_CAM_BIT, &priv->cfg))
seq_puts(s, " camera");
seq_puts(s, "\n");
seq_puts(s, "OSD support:");
if (test_bit(CFG_OSD_NUMLK_BIT, &priv->cfg))
seq_puts(s, " num-lock");
if (test_bit(CFG_OSD_CAPSLK_BIT, &priv->cfg))
seq_puts(s, " caps-lock");
if (test_bit(CFG_OSD_MICMUTE_BIT, &priv->cfg))
seq_puts(s, " mic-mute");
if (test_bit(CFG_OSD_TOUCHPAD_BIT, &priv->cfg))
seq_puts(s, " touchpad");
if (test_bit(CFG_OSD_CAM_BIT, &priv->cfg))
seq_puts(s, " camera");
seq_puts(s, "\n");
seq_puts(s, "Graphics: ");
switch (priv->cfg & 0x700) {
case 0x100:
seq_puts(s, "Intel");
break;
case 0x200:
seq_puts(s, "ATI");
break;
case 0x300:
seq_puts(s, "Nvidia");
break;
case 0x400:
seq_puts(s, "Intel and ATI");
break;
case 0x500:
seq_puts(s, "Intel and Nvidia");
break;
}
seq_puts(s, "\n");
return 0;
}
DEFINE_SHOW_ATTRIBUTE(debugfs_cfg);
static void ideapad_debugfs_init(struct ideapad_private *priv)
{
struct dentry *dir;
dir = debugfs_create_dir("ideapad", NULL);
priv->debug = dir;
debugfs_create_file("cfg", 0444, dir, priv, &debugfs_cfg_fops);
debugfs_create_file("status", 0444, dir, priv, &debugfs_status_fops);
}
static void ideapad_debugfs_exit(struct ideapad_private *priv)
{
debugfs_remove_recursive(priv->debug);
priv->debug = NULL;
}
/*
* sysfs
*/
static ssize_t camera_power_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct ideapad_private *priv = dev_get_drvdata(dev);
unsigned long result = 0;
int err;
scoped_guard(mutex, &priv->vpc_mutex) {
err = read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result);
if (err)
return err;
}
return sysfs_emit(buf, "%d\n", !!result);
}
static ssize_t camera_power_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct ideapad_private *priv = dev_get_drvdata(dev);
bool state;
int err;
err = kstrtobool(buf, &state);
if (err)
return err;
scoped_guard(mutex, &priv->vpc_mutex) {
err = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
if (err)
return err;
}
return count;
}
static DEVICE_ATTR_RW(camera_power);
static ssize_t conservation_mode_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct ideapad_private *priv = dev_get_drvdata(dev);
unsigned long result;
int err;
err = eval_gbmd(priv->adev->handle, &result);
if (err)
return err;
return sysfs_emit(buf, "%d\n", !!test_bit(GBMD_CONSERVATION_STATE_BIT, &result));
}
static ssize_t conservation_mode_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct ideapad_private *priv = dev_get_drvdata(dev);
bool state;
int err;
err = kstrtobool(buf, &state);
if (err)
return err;
err = exec_sbmc(priv->adev->handle, state ? SBMC_CONSERVATION_ON : SBMC_CONSERVATION_OFF);
if (err)
return err;
return count;
}
static DEVICE_ATTR_RW(conservation_mode);
static ssize_t fan_mode_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct ideapad_private *priv = dev_get_drvdata(dev);
unsigned long result = 0;
int err;
scoped_guard(mutex, &priv->vpc_mutex) {
err = read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result);
if (err)
return err;
}
return sysfs_emit(buf, "%lu\n", result);
}
static ssize_t fan_mode_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct ideapad_private *priv = dev_get_drvdata(dev);
unsigned int state;
int err;
err = kstrtouint(buf, 0, &state);
if (err)
return err;
if (state > 4 || state == 3)
return -EINVAL;
scoped_guard(mutex, &priv->vpc_mutex) {
err = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
if (err)
return err;
}
return count;
}
static DEVICE_ATTR_RW(fan_mode);
static int ideapad_fn_lock_get(struct ideapad_private *priv)
{
unsigned long hals;
int err;
err = eval_hals(priv->adev->handle, &hals);
if (err)
return err;
return !!test_bit(HALS_FNLOCK_STATE_BIT, &hals);
}
static int ideapad_fn_lock_set(struct ideapad_private *priv, bool state)
{
return exec_sals(priv->adev->handle,
state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
}
static void ideapad_fn_lock_led_notify(struct ideapad_private *priv, int brightness)
{
if (!priv->fn_lock.initialized)
return;
if (brightness == priv->fn_lock.last_brightness)
return;
priv->fn_lock.last_brightness = brightness;
led_classdev_notify_brightness_hw_changed(&priv->fn_lock.led, brightness);
}
static ssize_t fn_lock_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct ideapad_private *priv = dev_get_drvdata(dev);
int brightness;
brightness = ideapad_fn_lock_get(priv);
if (brightness < 0)
return brightness;
return sysfs_emit(buf, "%d\n", brightness);
}
static ssize_t fn_lock_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct ideapad_private *priv = dev_get_drvdata(dev);
bool state;
int err;
err = kstrtobool(buf, &state);
if (err)
return err;
err = ideapad_fn_lock_set(priv, state);
if (err)
return err;
ideapad_fn_lock_led_notify(priv, state);
return count;
}
static DEVICE_ATTR_RW(fn_lock);
static ssize_t touchpad_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct ideapad_private *priv = dev_get_drvdata(dev);
unsigned long result = 0;
int err;
scoped_guard(mutex, &priv->vpc_mutex) {
err = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result);
if (err)
return err;
}
priv->r_touchpad_val = result;
return sysfs_emit(buf, "%d\n", !!result);
}
static ssize_t touchpad_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct ideapad_private *priv = dev_get_drvdata(dev);
bool state;
int err;
err = kstrtobool(buf, &state);
if (err)
return err;
scoped_guard(mutex, &priv->vpc_mutex) {
err = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
if (err)
return err;
}
priv->r_touchpad_val = state;
return count;
}
static DEVICE_ATTR_RW(touchpad);
static ssize_t usb_charging_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct ideapad_private *priv = dev_get_drvdata(dev);
unsigned long hals;
int err;
err = eval_hals(priv->adev->handle, &hals);
if (err)
return err;
return sysfs_emit(buf, "%d\n", !!test_bit(HALS_USB_CHARGING_STATE_BIT, &hals));
}
static ssize_t usb_charging_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct ideapad_private *priv = dev_get_drvdata(dev);
bool state;
int err;
err = kstrtobool(buf, &state);
if (err)
return err;
err = exec_sals(priv->adev->handle, state ? SALS_USB_CHARGING_ON : SALS_USB_CHARGING_OFF);
if (err)
return err;
return count;
}
static DEVICE_ATTR_RW(usb_charging);
static struct attribute *ideapad_attributes[] = {
&dev_attr_camera_power.attr,
&dev_attr_conservation_mode.attr,
&dev_attr_fan_mode.attr,
&dev_attr_fn_lock.attr,
&dev_attr_touchpad.attr,
&dev_attr_usb_charging.attr,
NULL
};
static umode_t ideapad_is_visible(struct kobject *kobj,
struct attribute *attr,
int idx)
{
struct device *dev = kobj_to_dev(kobj);
struct ideapad_private *priv = dev_get_drvdata(dev);
bool supported = true;
if (attr == &dev_attr_camera_power.attr)
supported = test_bit(CFG_CAP_CAM_BIT, &priv->cfg);
else if (attr == &dev_attr_conservation_mode.attr)
supported = priv->features.conservation_mode;
else if (attr == &dev_attr_fan_mode.attr)
supported = priv->features.fan_mode;
else if (attr == &dev_attr_fn_lock.attr)
supported = priv->features.fn_lock;
else if (attr == &dev_attr_touchpad.attr)
supported = priv->features.touchpad_ctrl_via_ec;
else if (attr == &dev_attr_usb_charging.attr)
supported = priv->features.usb_charging;
return supported ? attr->mode : 0;
}
static const struct attribute_group ideapad_attribute_group = {
.is_visible = ideapad_is_visible,
.attrs = ideapad_attributes
};
/*
* DYTC Platform profile
*/
#define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
#define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
#define DYTC_CMD_GET 2 /* To get current IC function and mode */
#define DYTC_CMD_RESET 0x1ff /* To reset back to default */
#define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
#define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
#define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
#define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
#define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
#define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
#define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
#define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
#define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
#define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
#define DYTC_FUNCTION_MMC 11 /* Function = 11, desk mode */
#define DYTC_MODE_PERFORM 2 /* High power mode aka performance */
#define DYTC_MODE_LOW_POWER 3 /* Low power mode aka quiet */
#define DYTC_MODE_BALANCE 0xF /* Default mode aka balanced */
#define DYTC_SET_COMMAND(function, mode, on) \
(DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
(mode) << DYTC_SET_MODE_BIT | \
(on) << DYTC_SET_VALID_BIT)
#define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0)
#define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1)
static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
{
switch (dytcmode) {
case DYTC_MODE_LOW_POWER:
*profile = PLATFORM_PROFILE_LOW_POWER;
break;
case DYTC_MODE_BALANCE:
*profile = PLATFORM_PROFILE_BALANCED;
break;
case DYTC_MODE_PERFORM:
*profile = PLATFORM_PROFILE_PERFORMANCE;
break;
default: /* Unknown mode */
return -EINVAL;
}
return 0;
}
static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
{
switch (profile) {
case PLATFORM_PROFILE_LOW_POWER:
*perfmode = DYTC_MODE_LOW_POWER;
break;
case PLATFORM_PROFILE_BALANCED:
*perfmode = DYTC_MODE_BALANCE;
break;
case PLATFORM_PROFILE_PERFORMANCE:
*perfmode = DYTC_MODE_PERFORM;
break;
default: /* Unknown profile */
return -EOPNOTSUPP;
}
return 0;
}
/*
* dytc_profile_get: Function to register with platform_profile
* handler. Returns current platform profile.
*/
static int dytc_profile_get(struct platform_profile_handler *pprof,
enum platform_profile_option *profile)
{
struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
*profile = dytc->current_profile;
return 0;
}
/*
* Helper function - check if we are in CQL mode and if we are
* - disable CQL,
* - run the command
* - enable CQL
* If not in CQL mode, just run the command
*/
static int dytc_cql_command(struct ideapad_private *priv, unsigned long cmd,
unsigned long *output)
{
int err, cmd_err, cur_funcmode;
/* Determine if we are in CQL mode. This alters the commands we do */
err = eval_dytc(priv->adev->handle, DYTC_CMD_GET, output);
if (err)
return err;
cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
/* Check if we're OK to return immediately */
if (cmd == DYTC_CMD_GET && cur_funcmode != DYTC_FUNCTION_CQL)
return 0;
if (cur_funcmode == DYTC_FUNCTION_CQL) {
err = eval_dytc(priv->adev->handle, DYTC_DISABLE_CQL, NULL);
if (err)
return err;
}
cmd_err = eval_dytc(priv->adev->handle, cmd, output);
/* Check return condition after we've restored CQL state */
if (cur_funcmode == DYTC_FUNCTION_CQL) {
err = eval_dytc(priv->adev->handle, DYTC_ENABLE_CQL, NULL);
if (err)
return err;
}
return cmd_err;
}
/*
* dytc_profile_set: Function to register with platform_profile
* handler. Sets current platform profile.
*/
static int dytc_profile_set(struct platform_profile_handler *pprof,
enum platform_profile_option profile)
{
struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
struct ideapad_private *priv = dytc->priv;
unsigned long output;
int err;
scoped_guard(mutex_intr, &dytc->mutex) {
if (profile == PLATFORM_PROFILE_BALANCED) {
/* To get back to balanced mode we just issue a reset command */
err = eval_dytc(priv->adev->handle, DYTC_CMD_RESET, NULL);