]> git.itanic.dy.fi Git - linux-stable/blob - drivers/hwmon/asus-ec-sensors.c
19d3ca71b360927b9963e1d41b57cf74cbeb601b
[linux-stable] / drivers / hwmon / asus-ec-sensors.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * HWMON driver for ASUS motherboards that publish some sensor values
4  * via the embedded controller registers.
5  *
6  * Copyright (C) 2021 Eugene Shalygin <eugene.shalygin@gmail.com>
7
8  * EC provides:
9  * - Chipset temperature
10  * - CPU temperature
11  * - Motherboard temperature
12  * - T_Sensor temperature
13  * - VRM temperature
14  * - Water In temperature
15  * - Water Out temperature
16  * - CPU Optional fan RPM
17  * - Chipset fan RPM
18  * - VRM Heat Sink fan RPM
19  * - Water Flow fan RPM
20  * - CPU current
21  * - CPU core voltage
22  */
23
24 #include <linux/acpi.h>
25 #include <linux/bitops.h>
26 #include <linux/dev_printk.h>
27 #include <linux/dmi.h>
28 #include <linux/hwmon.h>
29 #include <linux/init.h>
30 #include <linux/jiffies.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/platform_device.h>
34 #include <linux/sort.h>
35 #include <linux/units.h>
36
37 #include <asm/unaligned.h>
38
39 static char *mutex_path_override;
40
41 /* Writing to this EC register switches EC bank */
42 #define ASUS_EC_BANK_REGISTER   0xff
43 #define SENSOR_LABEL_LEN        16
44
45 /*
46  * Arbitrary set max. allowed bank number. Required for sorting banks and
47  * currently is overkill with just 2 banks used at max, but for the sake
48  * of alignment let's set it to a higher value.
49  */
50 #define ASUS_EC_MAX_BANK        3
51
52 #define ACPI_LOCK_DELAY_MS      500
53
54 /* ACPI mutex for locking access to the EC for the firmware */
55 #define ASUS_HW_ACCESS_MUTEX_ASMX       "\\AMW0.ASMX"
56
57 #define ASUS_HW_ACCESS_MUTEX_RMTW_ASMX  "\\RMTW.ASMX"
58
59 #define MAX_IDENTICAL_BOARD_VARIATIONS  3
60
61 /* Moniker for the ACPI global lock (':' is not allowed in ASL identifiers) */
62 #define ACPI_GLOBAL_LOCK_PSEUDO_PATH    ":GLOBAL_LOCK"
63
64 typedef union {
65         u32 value;
66         struct {
67                 u8 index;
68                 u8 bank;
69                 u8 size;
70                 u8 dummy;
71         } components;
72 } sensor_address;
73
74 #define MAKE_SENSOR_ADDRESS(size, bank, index) {                               \
75                 .value = (size << 16) + (bank << 8) + index                    \
76         }
77
78 static u32 hwmon_attributes[hwmon_max] = {
79         [hwmon_chip] = HWMON_C_REGISTER_TZ,
80         [hwmon_temp] = HWMON_T_INPUT | HWMON_T_LABEL,
81         [hwmon_in] = HWMON_I_INPUT | HWMON_I_LABEL,
82         [hwmon_curr] = HWMON_C_INPUT | HWMON_C_LABEL,
83         [hwmon_fan] = HWMON_F_INPUT | HWMON_F_LABEL,
84 };
85
86 struct ec_sensor_info {
87         char label[SENSOR_LABEL_LEN];
88         enum hwmon_sensor_types type;
89         sensor_address addr;
90 };
91
92 #define EC_SENSOR(sensor_label, sensor_type, size, bank, index) {              \
93                 .label = sensor_label, .type = sensor_type,                    \
94                 .addr = MAKE_SENSOR_ADDRESS(size, bank, index),                \
95         }
96
97 enum ec_sensors {
98         /* chipset temperature [℃] */
99         ec_sensor_temp_chipset,
100         /* CPU temperature [℃] */
101         ec_sensor_temp_cpu,
102         /* motherboard temperature [℃] */
103         ec_sensor_temp_mb,
104         /* "T_Sensor" temperature sensor reading [℃] */
105         ec_sensor_temp_t_sensor,
106         /* VRM temperature [℃] */
107         ec_sensor_temp_vrm,
108         /* CPU Core voltage [mV] */
109         ec_sensor_in_cpu_core,
110         /* CPU_Opt fan [RPM] */
111         ec_sensor_fan_cpu_opt,
112         /* VRM heat sink fan [RPM] */
113         ec_sensor_fan_vrm_hs,
114         /* Chipset fan [RPM] */
115         ec_sensor_fan_chipset,
116         /* Water flow sensor reading [RPM] */
117         ec_sensor_fan_water_flow,
118         /* CPU current [A] */
119         ec_sensor_curr_cpu,
120         /* "Water_In" temperature sensor reading [℃] */
121         ec_sensor_temp_water_in,
122         /* "Water_Out" temperature sensor reading [℃] */
123         ec_sensor_temp_water_out,
124 };
125
126 #define SENSOR_TEMP_CHIPSET BIT(ec_sensor_temp_chipset)
127 #define SENSOR_TEMP_CPU BIT(ec_sensor_temp_cpu)
128 #define SENSOR_TEMP_MB BIT(ec_sensor_temp_mb)
129 #define SENSOR_TEMP_T_SENSOR BIT(ec_sensor_temp_t_sensor)
130 #define SENSOR_TEMP_VRM BIT(ec_sensor_temp_vrm)
131 #define SENSOR_IN_CPU_CORE BIT(ec_sensor_in_cpu_core)
132 #define SENSOR_FAN_CPU_OPT BIT(ec_sensor_fan_cpu_opt)
133 #define SENSOR_FAN_VRM_HS BIT(ec_sensor_fan_vrm_hs)
134 #define SENSOR_FAN_CHIPSET BIT(ec_sensor_fan_chipset)
135 #define SENSOR_FAN_WATER_FLOW BIT(ec_sensor_fan_water_flow)
136 #define SENSOR_CURR_CPU BIT(ec_sensor_curr_cpu)
137 #define SENSOR_TEMP_WATER_IN BIT(ec_sensor_temp_water_in)
138 #define SENSOR_TEMP_WATER_OUT BIT(ec_sensor_temp_water_out)
139
140 enum board_family {
141         family_unknown,
142         family_amd_400_series,
143         family_amd_500_series,
144         family_intel_600_series
145 };
146
147 /* All the known sensors for ASUS EC controllers */
148 static const struct ec_sensor_info sensors_family_amd_400[] = {
149         [ec_sensor_temp_chipset] =
150                 EC_SENSOR("Chipset", hwmon_temp, 1, 0x00, 0x3a),
151         [ec_sensor_temp_cpu] =
152                 EC_SENSOR("CPU", hwmon_temp, 1, 0x00, 0x3b),
153         [ec_sensor_temp_mb] =
154                 EC_SENSOR("Motherboard", hwmon_temp, 1, 0x00, 0x3c),
155         [ec_sensor_temp_t_sensor] =
156                 EC_SENSOR("T_Sensor", hwmon_temp, 1, 0x00, 0x3d),
157         [ec_sensor_temp_vrm] =
158                 EC_SENSOR("VRM", hwmon_temp, 1, 0x00, 0x3e),
159         [ec_sensor_in_cpu_core] =
160                 EC_SENSOR("CPU Core", hwmon_in, 2, 0x00, 0xa2),
161         [ec_sensor_fan_cpu_opt] =
162                 EC_SENSOR("CPU_Opt", hwmon_fan, 2, 0x00, 0xbc),
163         [ec_sensor_fan_vrm_hs] =
164                 EC_SENSOR("VRM HS", hwmon_fan, 2, 0x00, 0xb2),
165         [ec_sensor_fan_chipset] =
166                 /* no chipset fans in this generation */
167                 EC_SENSOR("Chipset", hwmon_fan, 0, 0x00, 0x00),
168         [ec_sensor_fan_water_flow] =
169                 EC_SENSOR("Water_Flow", hwmon_fan, 2, 0x00, 0xb4),
170         [ec_sensor_curr_cpu] =
171                 EC_SENSOR("CPU", hwmon_curr, 1, 0x00, 0xf4),
172         [ec_sensor_temp_water_in] =
173                 EC_SENSOR("Water_In", hwmon_temp, 1, 0x01, 0x0d),
174         [ec_sensor_temp_water_out] =
175                 EC_SENSOR("Water_Out", hwmon_temp, 1, 0x01, 0x0b),
176 };
177
178 static const struct ec_sensor_info sensors_family_amd_500[] = {
179         [ec_sensor_temp_chipset] =
180                 EC_SENSOR("Chipset", hwmon_temp, 1, 0x00, 0x3a),
181         [ec_sensor_temp_cpu] = EC_SENSOR("CPU", hwmon_temp, 1, 0x00, 0x3b),
182         [ec_sensor_temp_mb] =
183                 EC_SENSOR("Motherboard", hwmon_temp, 1, 0x00, 0x3c),
184         [ec_sensor_temp_t_sensor] =
185                 EC_SENSOR("T_Sensor", hwmon_temp, 1, 0x00, 0x3d),
186         [ec_sensor_temp_vrm] = EC_SENSOR("VRM", hwmon_temp, 1, 0x00, 0x3e),
187         [ec_sensor_in_cpu_core] =
188                 EC_SENSOR("CPU Core", hwmon_in, 2, 0x00, 0xa2),
189         [ec_sensor_fan_cpu_opt] =
190                 EC_SENSOR("CPU_Opt", hwmon_fan, 2, 0x00, 0xb0),
191         [ec_sensor_fan_vrm_hs] = EC_SENSOR("VRM HS", hwmon_fan, 2, 0x00, 0xb2),
192         [ec_sensor_fan_chipset] =
193                 EC_SENSOR("Chipset", hwmon_fan, 2, 0x00, 0xb4),
194         [ec_sensor_fan_water_flow] =
195                 EC_SENSOR("Water_Flow", hwmon_fan, 2, 0x00, 0xbc),
196         [ec_sensor_curr_cpu] = EC_SENSOR("CPU", hwmon_curr, 1, 0x00, 0xf4),
197         [ec_sensor_temp_water_in] =
198                 EC_SENSOR("Water_In", hwmon_temp, 1, 0x01, 0x00),
199         [ec_sensor_temp_water_out] =
200                 EC_SENSOR("Water_Out", hwmon_temp, 1, 0x01, 0x01),
201 };
202
203 static const struct ec_sensor_info sensors_family_intel_600[] = {
204         [ec_sensor_temp_t_sensor] =
205                 EC_SENSOR("T_Sensor", hwmon_temp, 1, 0x00, 0x3d),
206         [ec_sensor_temp_vrm] = EC_SENSOR("VRM", hwmon_temp, 1, 0x00, 0x3e),
207 };
208
209 /* Shortcuts for common combinations */
210 #define SENSOR_SET_TEMP_CHIPSET_CPU_MB                                         \
211         (SENSOR_TEMP_CHIPSET | SENSOR_TEMP_CPU | SENSOR_TEMP_MB)
212 #define SENSOR_SET_TEMP_WATER (SENSOR_TEMP_WATER_IN | SENSOR_TEMP_WATER_OUT)
213
214 struct ec_board_info {
215         const char *board_names[MAX_IDENTICAL_BOARD_VARIATIONS];
216         unsigned long sensors;
217         /*
218          * Defines which mutex to use for guarding access to the state and the
219          * hardware. Can be either a full path to an AML mutex or the
220          * pseudo-path ACPI_GLOBAL_LOCK_PSEUDO_PATH to use the global ACPI lock,
221          * or left empty to use a regular mutex object, in which case access to
222          * the hardware is not guarded.
223          */
224         const char *mutex_path;
225         enum board_family family;
226 };
227
228 static const struct ec_board_info board_info[] = {
229         {
230                 .board_names = {"PRIME X470-PRO"},
231                 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
232                         SENSOR_TEMP_T_SENSOR | SENSOR_TEMP_VRM |
233                         SENSOR_FAN_CPU_OPT |
234                         SENSOR_CURR_CPU | SENSOR_IN_CPU_CORE,
235                 .mutex_path = ACPI_GLOBAL_LOCK_PSEUDO_PATH,
236                 .family = family_amd_400_series,
237         },
238         {
239                 .board_names = {"PRIME X570-PRO"},
240                 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB | SENSOR_TEMP_VRM |
241                         SENSOR_TEMP_T_SENSOR | SENSOR_FAN_CHIPSET,
242                 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
243                 .family = family_amd_500_series,
244         },
245         {
246                 .board_names = {"ProArt X570-CREATOR WIFI"},
247                 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB | SENSOR_TEMP_VRM |
248                         SENSOR_TEMP_T_SENSOR | SENSOR_FAN_CPU_OPT |
249                         SENSOR_CURR_CPU | SENSOR_IN_CPU_CORE,
250         },
251         {
252                 .board_names = {"Pro WS X570-ACE"},
253                 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB | SENSOR_TEMP_VRM |
254                         SENSOR_TEMP_T_SENSOR | SENSOR_FAN_CHIPSET |
255                         SENSOR_CURR_CPU | SENSOR_IN_CPU_CORE,
256                 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
257                 .family = family_amd_500_series,
258         },
259         {
260                 .board_names = {"ROG CROSSHAIR VIII DARK HERO"},
261                 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
262                         SENSOR_TEMP_T_SENSOR |
263                         SENSOR_TEMP_VRM | SENSOR_SET_TEMP_WATER |
264                         SENSOR_FAN_CPU_OPT | SENSOR_FAN_WATER_FLOW |
265                         SENSOR_CURR_CPU | SENSOR_IN_CPU_CORE,
266                 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
267                 .family = family_amd_500_series,
268         },
269         {
270                 .board_names = {
271                         "ROG CROSSHAIR VIII FORMULA",
272                         "ROG CROSSHAIR VIII HERO",
273                         "ROG CROSSHAIR VIII HERO (WI-FI)",
274                 },
275                 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
276                         SENSOR_TEMP_T_SENSOR |
277                         SENSOR_TEMP_VRM | SENSOR_SET_TEMP_WATER |
278                         SENSOR_FAN_CPU_OPT | SENSOR_FAN_CHIPSET |
279                         SENSOR_FAN_WATER_FLOW | SENSOR_CURR_CPU |
280                         SENSOR_IN_CPU_CORE,
281                 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
282                 .family = family_amd_500_series,
283         },
284         {
285                 .board_names = {"ROG CROSSHAIR VIII IMPACT"},
286                 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
287                         SENSOR_TEMP_T_SENSOR | SENSOR_TEMP_VRM |
288                         SENSOR_FAN_CHIPSET | SENSOR_CURR_CPU |
289                         SENSOR_IN_CPU_CORE,
290                 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
291                 .family = family_amd_500_series,
292         },
293         {
294                 .board_names = {"ROG STRIX B550-E GAMING"},
295                 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
296                         SENSOR_TEMP_T_SENSOR | SENSOR_TEMP_VRM |
297                         SENSOR_FAN_CPU_OPT,
298                 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
299                 .family = family_amd_500_series,
300         },
301         {
302                 .board_names = {"ROG STRIX B550-I GAMING"},
303                 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
304                         SENSOR_TEMP_T_SENSOR | SENSOR_TEMP_VRM |
305                         SENSOR_FAN_VRM_HS | SENSOR_CURR_CPU |
306                         SENSOR_IN_CPU_CORE,
307                 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
308                 .family = family_amd_500_series,
309         },
310         {
311                 .board_names = {"ROG STRIX X570-E GAMING"},
312                 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
313                         SENSOR_TEMP_T_SENSOR | SENSOR_TEMP_VRM |
314                         SENSOR_FAN_CHIPSET | SENSOR_CURR_CPU |
315                         SENSOR_IN_CPU_CORE,
316                 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
317                 .family = family_amd_500_series,
318         },
319         {
320                 .board_names = {"ROG STRIX X570-E GAMING WIFI II"},
321                 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
322                         SENSOR_TEMP_T_SENSOR | SENSOR_CURR_CPU |
323                         SENSOR_IN_CPU_CORE,
324                 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
325                 .family = family_amd_500_series,
326         },
327         {
328                 .board_names = {"ROG STRIX X570-F GAMING"},
329                 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
330                         SENSOR_TEMP_T_SENSOR | SENSOR_FAN_CHIPSET,
331                 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
332                 .family = family_amd_500_series,
333         },
334         {
335                 .board_names = {"ROG STRIX X570-I GAMING"},
336                 .sensors = SENSOR_TEMP_T_SENSOR | SENSOR_FAN_VRM_HS |
337                         SENSOR_FAN_CHIPSET | SENSOR_CURR_CPU |
338                         SENSOR_IN_CPU_CORE,
339                 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
340                 .family = family_amd_500_series,
341         },
342         {
343                 .board_names = {"ROG STRIX Z690-A GAMING WIFI D4"},
344                 .sensors = SENSOR_TEMP_T_SENSOR | SENSOR_TEMP_VRM,
345                 .mutex_path = ASUS_HW_ACCESS_MUTEX_RMTW_ASMX,
346                 .family = family_intel_600_series,
347         },
348         {}
349 };
350
351 struct ec_sensor {
352         unsigned int info_index;
353         s32 cached_value;
354 };
355
356 struct lock_data {
357         union {
358                 acpi_handle aml;
359                 /* global lock handle */
360                 u32 glk;
361         } mutex;
362         bool (*lock)(struct lock_data *data);
363         bool (*unlock)(struct lock_data *data);
364 };
365
366 /*
367  * The next function pairs implement options for locking access to the
368  * state and the EC
369  */
370 static bool lock_via_acpi_mutex(struct lock_data *data)
371 {
372         /*
373          * ASUS DSDT does not specify that access to the EC has to be guarded,
374          * but firmware does access it via ACPI
375          */
376         return ACPI_SUCCESS(acpi_acquire_mutex(data->mutex.aml,
377                                                NULL, ACPI_LOCK_DELAY_MS));
378 }
379
380 static bool unlock_acpi_mutex(struct lock_data *data)
381 {
382         return ACPI_SUCCESS(acpi_release_mutex(data->mutex.aml, NULL));
383 }
384
385 static bool lock_via_global_acpi_lock(struct lock_data *data)
386 {
387         return ACPI_SUCCESS(acpi_acquire_global_lock(ACPI_LOCK_DELAY_MS,
388                                                      &data->mutex.glk));
389 }
390
391 static bool unlock_global_acpi_lock(struct lock_data *data)
392 {
393         return ACPI_SUCCESS(acpi_release_global_lock(data->mutex.glk));
394 }
395
396 struct ec_sensors_data {
397         const struct ec_board_info *board_info;
398         const struct ec_sensor_info *sensors_info;
399         struct ec_sensor *sensors;
400         /* EC registers to read from */
401         u16 *registers;
402         u8 *read_buffer;
403         /* sorted list of unique register banks */
404         u8 banks[ASUS_EC_MAX_BANK + 1];
405         /* in jiffies */
406         unsigned long last_updated;
407         struct lock_data lock_data;
408         /* number of board EC sensors */
409         u8 nr_sensors;
410         /*
411          * number of EC registers to read
412          * (sensor might span more than 1 register)
413          */
414         u8 nr_registers;
415         /* number of unique register banks */
416         u8 nr_banks;
417 };
418
419 static u8 register_bank(u16 reg)
420 {
421         return reg >> 8;
422 }
423
424 static u8 register_index(u16 reg)
425 {
426         return reg & 0x00ff;
427 }
428
429 static bool is_sensor_data_signed(const struct ec_sensor_info *si)
430 {
431         /*
432          * guessed from WMI functions in DSDT code for boards
433          * of the X470 generation
434          */
435         return si->type == hwmon_temp;
436 }
437
438 static const struct ec_sensor_info *
439 get_sensor_info(const struct ec_sensors_data *state, int index)
440 {
441         return state->sensors_info + state->sensors[index].info_index;
442 }
443
444 static int find_ec_sensor_index(const struct ec_sensors_data *ec,
445                                 enum hwmon_sensor_types type, int channel)
446 {
447         unsigned int i;
448
449         for (i = 0; i < ec->nr_sensors; i++) {
450                 if (get_sensor_info(ec, i)->type == type) {
451                         if (channel == 0)
452                                 return i;
453                         channel--;
454                 }
455         }
456         return -ENOENT;
457 }
458
459 static int __init bank_compare(const void *a, const void *b)
460 {
461         return *((const s8 *)a) - *((const s8 *)b);
462 }
463
464 static void __init setup_sensor_data(struct ec_sensors_data *ec)
465 {
466         struct ec_sensor *s = ec->sensors;
467         bool bank_found;
468         int i, j;
469         u8 bank;
470
471         ec->nr_banks = 0;
472         ec->nr_registers = 0;
473
474         for_each_set_bit(i, &ec->board_info->sensors,
475                          BITS_PER_TYPE(ec->board_info->sensors)) {
476                 s->info_index = i;
477                 s->cached_value = 0;
478                 ec->nr_registers +=
479                         ec->sensors_info[s->info_index].addr.components.size;
480                 bank_found = false;
481                 bank = ec->sensors_info[s->info_index].addr.components.bank;
482                 for (j = 0; j < ec->nr_banks; j++) {
483                         if (ec->banks[j] == bank) {
484                                 bank_found = true;
485                                 break;
486                         }
487                 }
488                 if (!bank_found) {
489                         ec->banks[ec->nr_banks++] = bank;
490                 }
491                 s++;
492         }
493         sort(ec->banks, ec->nr_banks, 1, bank_compare, NULL);
494 }
495
496 static void __init fill_ec_registers(struct ec_sensors_data *ec)
497 {
498         const struct ec_sensor_info *si;
499         unsigned int i, j, register_idx = 0;
500
501         for (i = 0; i < ec->nr_sensors; ++i) {
502                 si = get_sensor_info(ec, i);
503                 for (j = 0; j < si->addr.components.size; ++j, ++register_idx) {
504                         ec->registers[register_idx] =
505                                 (si->addr.components.bank << 8) +
506                                 si->addr.components.index + j;
507                 }
508         }
509 }
510
511 static int __init setup_lock_data(struct device *dev)
512 {
513         const char *mutex_path;
514         int status;
515         struct ec_sensors_data *state = dev_get_drvdata(dev);
516
517         mutex_path = mutex_path_override ?
518                 mutex_path_override : state->board_info->mutex_path;
519
520         if (!mutex_path || !strlen(mutex_path)) {
521                 dev_err(dev, "Hardware access guard mutex name is empty");
522                 return -EINVAL;
523         }
524         if (!strcmp(mutex_path, ACPI_GLOBAL_LOCK_PSEUDO_PATH)) {
525                 state->lock_data.mutex.glk = 0;
526                 state->lock_data.lock = lock_via_global_acpi_lock;
527                 state->lock_data.unlock = unlock_global_acpi_lock;
528         } else {
529                 status = acpi_get_handle(NULL, (acpi_string)mutex_path,
530                                          &state->lock_data.mutex.aml);
531                 if (ACPI_FAILURE(status)) {
532                         dev_err(dev,
533                                 "Failed to get hardware access guard AML mutex '%s': error %d",
534                                 mutex_path, status);
535                         return -ENOENT;
536                 }
537                 state->lock_data.lock = lock_via_acpi_mutex;
538                 state->lock_data.unlock = unlock_acpi_mutex;
539         }
540         return 0;
541 }
542
543 static int asus_ec_bank_switch(u8 bank, u8 *old)
544 {
545         int status = 0;
546
547         if (old) {
548                 status = ec_read(ASUS_EC_BANK_REGISTER, old);
549         }
550         if (status || (old && (*old == bank)))
551                 return status;
552         return ec_write(ASUS_EC_BANK_REGISTER, bank);
553 }
554
555 static int asus_ec_block_read(const struct device *dev,
556                               struct ec_sensors_data *ec)
557 {
558         int ireg, ibank, status;
559         u8 bank, reg_bank, prev_bank;
560
561         bank = 0;
562         status = asus_ec_bank_switch(bank, &prev_bank);
563         if (status) {
564                 dev_warn(dev, "EC bank switch failed");
565                 return status;
566         }
567
568         if (prev_bank) {
569                 /* oops... somebody else is working with the EC too */
570                 dev_warn(dev,
571                         "Concurrent access to the ACPI EC detected.\nRace condition possible.");
572         }
573
574         /* read registers minimizing bank switches. */
575         for (ibank = 0; ibank < ec->nr_banks; ibank++) {
576                 if (bank != ec->banks[ibank]) {
577                         bank = ec->banks[ibank];
578                         if (asus_ec_bank_switch(bank, NULL)) {
579                                 dev_warn(dev, "EC bank switch to %d failed",
580                                          bank);
581                                 break;
582                         }
583                 }
584                 for (ireg = 0; ireg < ec->nr_registers; ireg++) {
585                         reg_bank = register_bank(ec->registers[ireg]);
586                         if (reg_bank < bank) {
587                                 continue;
588                         }
589                         ec_read(register_index(ec->registers[ireg]),
590                                 ec->read_buffer + ireg);
591                 }
592         }
593
594         status = asus_ec_bank_switch(prev_bank, NULL);
595         return status;
596 }
597
598 static inline s32 get_sensor_value(const struct ec_sensor_info *si, u8 *data)
599 {
600         if (is_sensor_data_signed(si)) {
601                 switch (si->addr.components.size) {
602                 case 1:
603                         return (s8)*data;
604                 case 2:
605                         return (s16)get_unaligned_be16(data);
606                 case 4:
607                         return (s32)get_unaligned_be32(data);
608                 default:
609                         return 0;
610                 }
611         } else {
612                 switch (si->addr.components.size) {
613                 case 1:
614                         return *data;
615                 case 2:
616                         return get_unaligned_be16(data);
617                 case 4:
618                         return get_unaligned_be32(data);
619                 default:
620                         return 0;
621                 }
622         }
623 }
624
625 static void update_sensor_values(struct ec_sensors_data *ec, u8 *data)
626 {
627         const struct ec_sensor_info *si;
628         struct ec_sensor *s, *sensor_end;
629
630         sensor_end = ec->sensors + ec->nr_sensors;
631         for (s = ec->sensors; s != sensor_end; s++) {
632                 si = ec->sensors_info + s->info_index;
633                 s->cached_value = get_sensor_value(si, data);
634                 data += si->addr.components.size;
635         }
636 }
637
638 static int update_ec_sensors(const struct device *dev,
639                              struct ec_sensors_data *ec)
640 {
641         int status;
642
643         if (!ec->lock_data.lock(&ec->lock_data)) {
644                 dev_warn(dev, "Failed to acquire mutex");
645                 return -EBUSY;
646         }
647
648         status = asus_ec_block_read(dev, ec);
649
650         if (!status) {
651                 update_sensor_values(ec, ec->read_buffer);
652         }
653
654         if (!ec->lock_data.unlock(&ec->lock_data))
655                 dev_err(dev, "Failed to release mutex");
656
657         return status;
658 }
659
660 static long scale_sensor_value(s32 value, int data_type)
661 {
662         switch (data_type) {
663         case hwmon_curr:
664         case hwmon_temp:
665                 return value * MILLI;
666         default:
667                 return value;
668         }
669 }
670
671 static int get_cached_value_or_update(const struct device *dev,
672                                       int sensor_index,
673                                       struct ec_sensors_data *state, s32 *value)
674 {
675         if (time_after(jiffies, state->last_updated + HZ)) {
676                 if (update_ec_sensors(dev, state)) {
677                         dev_err(dev, "update_ec_sensors() failure\n");
678                         return -EIO;
679                 }
680
681                 state->last_updated = jiffies;
682         }
683
684         *value = state->sensors[sensor_index].cached_value;
685         return 0;
686 }
687
688 /*
689  * Now follow the functions that implement the hwmon interface
690  */
691
692 static int asus_ec_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
693                               u32 attr, int channel, long *val)
694 {
695         int ret;
696         s32 value = 0;
697
698         struct ec_sensors_data *state = dev_get_drvdata(dev);
699         int sidx = find_ec_sensor_index(state, type, channel);
700
701         if (sidx < 0) {
702                 return sidx;
703         }
704
705         ret = get_cached_value_or_update(dev, sidx, state, &value);
706         if (!ret) {
707                 *val = scale_sensor_value(value,
708                                           get_sensor_info(state, sidx)->type);
709         }
710
711         return ret;
712 }
713
714 static int asus_ec_hwmon_read_string(struct device *dev,
715                                      enum hwmon_sensor_types type, u32 attr,
716                                      int channel, const char **str)
717 {
718         struct ec_sensors_data *state = dev_get_drvdata(dev);
719         int sensor_index = find_ec_sensor_index(state, type, channel);
720         *str = get_sensor_info(state, sensor_index)->label;
721
722         return 0;
723 }
724
725 static umode_t asus_ec_hwmon_is_visible(const void *drvdata,
726                                         enum hwmon_sensor_types type, u32 attr,
727                                         int channel)
728 {
729         const struct ec_sensors_data *state = drvdata;
730
731         return find_ec_sensor_index(state, type, channel) >= 0 ? S_IRUGO : 0;
732 }
733
734 static int __init
735 asus_ec_hwmon_add_chan_info(struct hwmon_channel_info *asus_ec_hwmon_chan,
736                              struct device *dev, int num,
737                              enum hwmon_sensor_types type, u32 config)
738 {
739         int i;
740         u32 *cfg = devm_kcalloc(dev, num + 1, sizeof(*cfg), GFP_KERNEL);
741
742         if (!cfg)
743                 return -ENOMEM;
744
745         asus_ec_hwmon_chan->type = type;
746         asus_ec_hwmon_chan->config = cfg;
747         for (i = 0; i < num; i++, cfg++)
748                 *cfg = config;
749
750         return 0;
751 }
752
753 static const struct hwmon_ops asus_ec_hwmon_ops = {
754         .is_visible = asus_ec_hwmon_is_visible,
755         .read = asus_ec_hwmon_read,
756         .read_string = asus_ec_hwmon_read_string,
757 };
758
759 static struct hwmon_chip_info asus_ec_chip_info = {
760         .ops = &asus_ec_hwmon_ops,
761 };
762
763 static const struct ec_board_info * __init get_board_info(void)
764 {
765         const char *dmi_board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
766         const char *dmi_board_name = dmi_get_system_info(DMI_BOARD_NAME);
767         const struct ec_board_info *board;
768
769         if (!dmi_board_vendor || !dmi_board_name ||
770             strcasecmp(dmi_board_vendor, "ASUSTeK COMPUTER INC."))
771                 return NULL;
772
773         for (board = board_info; board->sensors; board++) {
774                 if (match_string(board->board_names,
775                                  MAX_IDENTICAL_BOARD_VARIATIONS,
776                                  dmi_board_name) >= 0)
777                         return board;
778         }
779
780         return NULL;
781 }
782
783 static int __init asus_ec_probe(struct platform_device *pdev)
784 {
785         const struct hwmon_channel_info **ptr_asus_ec_ci;
786         int nr_count[hwmon_max] = { 0 }, nr_types = 0;
787         struct hwmon_channel_info *asus_ec_hwmon_chan;
788         const struct ec_board_info *pboard_info;
789         const struct hwmon_chip_info *chip_info;
790         struct device *dev = &pdev->dev;
791         struct ec_sensors_data *ec_data;
792         const struct ec_sensor_info *si;
793         enum hwmon_sensor_types type;
794         struct device *hwdev;
795         unsigned int i;
796         int status;
797
798         pboard_info = get_board_info();
799         if (!pboard_info)
800                 return -ENODEV;
801
802         ec_data = devm_kzalloc(dev, sizeof(struct ec_sensors_data),
803                                GFP_KERNEL);
804         if (!ec_data)
805                 return -ENOMEM;
806
807         dev_set_drvdata(dev, ec_data);
808         ec_data->board_info = pboard_info;
809
810         switch (ec_data->board_info->family) {
811         case family_amd_400_series:
812                 ec_data->sensors_info = sensors_family_amd_400;
813                 break;
814         case family_amd_500_series:
815                 ec_data->sensors_info = sensors_family_amd_500;
816                 break;
817         case family_intel_600_series:
818                 ec_data->sensors_info = sensors_family_intel_600;
819                 break;
820         default:
821                 dev_err(dev, "Unknown board family: %d",
822                         ec_data->board_info->family);
823                 return -EINVAL;
824         }
825
826         ec_data->nr_sensors = hweight_long(ec_data->board_info->sensors);
827         ec_data->sensors = devm_kcalloc(dev, ec_data->nr_sensors,
828                                         sizeof(struct ec_sensor), GFP_KERNEL);
829
830         status = setup_lock_data(dev);
831         if (status) {
832                 dev_err(dev, "Failed to setup state/EC locking: %d", status);
833                 return status;
834         }
835
836         setup_sensor_data(ec_data);
837         ec_data->registers = devm_kcalloc(dev, ec_data->nr_registers,
838                                           sizeof(u16), GFP_KERNEL);
839         ec_data->read_buffer = devm_kcalloc(dev, ec_data->nr_registers,
840                                             sizeof(u8), GFP_KERNEL);
841
842         if (!ec_data->registers || !ec_data->read_buffer)
843                 return -ENOMEM;
844
845         fill_ec_registers(ec_data);
846
847         for (i = 0; i < ec_data->nr_sensors; ++i) {
848                 si = get_sensor_info(ec_data, i);
849                 if (!nr_count[si->type])
850                         ++nr_types;
851                 ++nr_count[si->type];
852         }
853
854         if (nr_count[hwmon_temp])
855                 nr_count[hwmon_chip]++, nr_types++;
856
857         asus_ec_hwmon_chan = devm_kcalloc(
858                 dev, nr_types, sizeof(*asus_ec_hwmon_chan), GFP_KERNEL);
859         if (!asus_ec_hwmon_chan)
860                 return -ENOMEM;
861
862         ptr_asus_ec_ci = devm_kcalloc(dev, nr_types + 1,
863                                        sizeof(*ptr_asus_ec_ci), GFP_KERNEL);
864         if (!ptr_asus_ec_ci)
865                 return -ENOMEM;
866
867         asus_ec_chip_info.info = ptr_asus_ec_ci;
868         chip_info = &asus_ec_chip_info;
869
870         for (type = 0; type < hwmon_max; ++type) {
871                 if (!nr_count[type])
872                         continue;
873
874                 asus_ec_hwmon_add_chan_info(asus_ec_hwmon_chan, dev,
875                                              nr_count[type], type,
876                                              hwmon_attributes[type]);
877                 *ptr_asus_ec_ci++ = asus_ec_hwmon_chan++;
878         }
879
880         dev_info(dev, "board has %d EC sensors that span %d registers",
881                  ec_data->nr_sensors, ec_data->nr_registers);
882
883         hwdev = devm_hwmon_device_register_with_info(dev, "asusec",
884                                                      ec_data, chip_info, NULL);
885
886         return PTR_ERR_OR_ZERO(hwdev);
887 }
888
889
890 static const struct acpi_device_id acpi_ec_ids[] = {
891         /* Embedded Controller Device */
892         { "PNP0C09", 0 },
893         {}
894 };
895
896 static struct platform_driver asus_ec_sensors_platform_driver = {
897         .driver = {
898                 .name   = "asus-ec-sensors",
899                 .acpi_match_table = acpi_ec_ids,
900         },
901 };
902
903 MODULE_DEVICE_TABLE(acpi, acpi_ec_ids);
904 /*
905  * we use module_platform_driver_probe() rather than module_platform_driver()
906  * because the probe function (and its dependants) are marked with __init, which
907  * means we can't put it into the .probe member of the platform_driver struct
908  * above, and we can't mark the asus_ec_sensors_platform_driver object as __init
909  * because the object is referenced from the module exit code.
910  */
911 module_platform_driver_probe(asus_ec_sensors_platform_driver, asus_ec_probe);
912
913 module_param_named(mutex_path, mutex_path_override, charp, 0);
914 MODULE_PARM_DESC(mutex_path,
915                  "Override ACPI mutex path used to guard access to hardware");
916
917 MODULE_AUTHOR("Eugene Shalygin <eugene.shalygin@gmail.com>");
918 MODULE_DESCRIPTION(
919         "HWMON driver for sensors accessible via ACPI EC in ASUS motherboards");
920 MODULE_LICENSE("GPL");