1 // SPDX-License-Identifier: GPL-2.0+
3 * HWMON driver for ASUS motherboards that publish some sensor values
4 * via the embedded controller registers.
6 * Copyright (C) 2021 Eugene Shalygin <eugene.shalygin@gmail.com>
9 * - Chipset temperature
11 * - Motherboard temperature
12 * - T_Sensor temperature
14 * - Water In temperature
15 * - Water Out temperature
16 * - CPU Optional fan RPM
18 * - VRM Heat Sink fan RPM
19 * - Water Flow fan RPM
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>
37 #include <asm/unaligned.h>
39 static char *mutex_path_override;
41 /* Writing to this EC register switches EC bank */
42 #define ASUS_EC_BANK_REGISTER 0xff
43 #define SENSOR_LABEL_LEN 16
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.
50 #define ASUS_EC_MAX_BANK 3
52 #define ACPI_LOCK_DELAY_MS 500
54 /* ACPI mutex for locking access to the EC for the firmware */
55 #define ASUS_HW_ACCESS_MUTEX_ASMX "\\AMW0.ASMX"
57 #define ASUS_HW_ACCESS_MUTEX_RMTW_ASMX "\\RMTW.ASMX"
59 #define MAX_IDENTICAL_BOARD_VARIATIONS 3
61 /* Moniker for the ACPI global lock (':' is not allowed in ASL identifiers) */
62 #define ACPI_GLOBAL_LOCK_PSEUDO_PATH ":GLOBAL_LOCK"
74 #define MAKE_SENSOR_ADDRESS(size, bank, index) { \
75 .value = (size << 16) + (bank << 8) + index \
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,
86 struct ec_sensor_info {
87 char label[SENSOR_LABEL_LEN];
88 enum hwmon_sensor_types type;
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), \
98 /* chipset temperature [℃] */
99 ec_sensor_temp_chipset,
100 /* CPU temperature [℃] */
102 /* motherboard temperature [℃] */
104 /* "T_Sensor" temperature sensor reading [℃] */
105 ec_sensor_temp_t_sensor,
106 /* VRM temperature [℃] */
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] */
120 /* "Water_In" temperature sensor reading [℃] */
121 ec_sensor_temp_water_in,
122 /* "Water_Out" temperature sensor reading [℃] */
123 ec_sensor_temp_water_out,
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)
142 family_amd_400_series,
143 family_amd_500_series,
144 family_intel_600_series
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),
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),
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),
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)
214 struct ec_board_info {
215 const char *board_names[MAX_IDENTICAL_BOARD_VARIATIONS];
216 unsigned long sensors;
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.
224 const char *mutex_path;
225 enum board_family family;
228 static const struct ec_board_info board_info[] = {
230 .board_names = {"PRIME X470-PRO"},
231 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
232 SENSOR_TEMP_T_SENSOR | SENSOR_TEMP_VRM |
234 SENSOR_CURR_CPU | SENSOR_IN_CPU_CORE,
235 .mutex_path = ACPI_GLOBAL_LOCK_PSEUDO_PATH,
236 .family = family_amd_400_series,
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,
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,
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,
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,
271 "ROG CROSSHAIR VIII FORMULA",
272 "ROG CROSSHAIR VIII HERO",
273 "ROG CROSSHAIR VIII HERO (WI-FI)",
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 |
281 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
282 .family = family_amd_500_series,
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 |
290 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
291 .family = family_amd_500_series,
294 .board_names = {"ROG STRIX B550-E GAMING"},
295 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
296 SENSOR_TEMP_T_SENSOR | SENSOR_TEMP_VRM |
298 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
299 .family = family_amd_500_series,
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 |
307 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
308 .family = family_amd_500_series,
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 |
316 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
317 .family = family_amd_500_series,
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 |
324 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
325 .family = family_amd_500_series,
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,
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 |
339 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
340 .family = family_amd_500_series,
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,
352 unsigned int info_index;
359 /* global lock handle */
362 bool (*lock)(struct lock_data *data);
363 bool (*unlock)(struct lock_data *data);
367 * The next function pairs implement options for locking access to the
370 static bool lock_via_acpi_mutex(struct lock_data *data)
373 * ASUS DSDT does not specify that access to the EC has to be guarded,
374 * but firmware does access it via ACPI
376 return ACPI_SUCCESS(acpi_acquire_mutex(data->mutex.aml,
377 NULL, ACPI_LOCK_DELAY_MS));
380 static bool unlock_acpi_mutex(struct lock_data *data)
382 return ACPI_SUCCESS(acpi_release_mutex(data->mutex.aml, NULL));
385 static bool lock_via_global_acpi_lock(struct lock_data *data)
387 return ACPI_SUCCESS(acpi_acquire_global_lock(ACPI_LOCK_DELAY_MS,
391 static bool unlock_global_acpi_lock(struct lock_data *data)
393 return ACPI_SUCCESS(acpi_release_global_lock(data->mutex.glk));
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 */
403 /* sorted list of unique register banks */
404 u8 banks[ASUS_EC_MAX_BANK + 1];
406 unsigned long last_updated;
407 struct lock_data lock_data;
408 /* number of board EC sensors */
411 * number of EC registers to read
412 * (sensor might span more than 1 register)
415 /* number of unique register banks */
419 static u8 register_bank(u16 reg)
424 static u8 register_index(u16 reg)
429 static bool is_sensor_data_signed(const struct ec_sensor_info *si)
432 * guessed from WMI functions in DSDT code for boards
433 * of the X470 generation
435 return si->type == hwmon_temp;
438 static const struct ec_sensor_info *
439 get_sensor_info(const struct ec_sensors_data *state, int index)
441 return state->sensors_info + state->sensors[index].info_index;
444 static int find_ec_sensor_index(const struct ec_sensors_data *ec,
445 enum hwmon_sensor_types type, int channel)
449 for (i = 0; i < ec->nr_sensors; i++) {
450 if (get_sensor_info(ec, i)->type == type) {
459 static int __init bank_compare(const void *a, const void *b)
461 return *((const s8 *)a) - *((const s8 *)b);
464 static void __init setup_sensor_data(struct ec_sensors_data *ec)
466 struct ec_sensor *s = ec->sensors;
472 ec->nr_registers = 0;
474 for_each_set_bit(i, &ec->board_info->sensors,
475 BITS_PER_TYPE(ec->board_info->sensors)) {
479 ec->sensors_info[s->info_index].addr.components.size;
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) {
489 ec->banks[ec->nr_banks++] = bank;
493 sort(ec->banks, ec->nr_banks, 1, bank_compare, NULL);
496 static void __init fill_ec_registers(struct ec_sensors_data *ec)
498 const struct ec_sensor_info *si;
499 unsigned int i, j, register_idx = 0;
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;
511 static int __init setup_lock_data(struct device *dev)
513 const char *mutex_path;
515 struct ec_sensors_data *state = dev_get_drvdata(dev);
517 mutex_path = mutex_path_override ?
518 mutex_path_override : state->board_info->mutex_path;
520 if (!mutex_path || !strlen(mutex_path)) {
521 dev_err(dev, "Hardware access guard mutex name is empty");
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;
529 status = acpi_get_handle(NULL, (acpi_string)mutex_path,
530 &state->lock_data.mutex.aml);
531 if (ACPI_FAILURE(status)) {
533 "Failed to get hardware access guard AML mutex '%s': error %d",
537 state->lock_data.lock = lock_via_acpi_mutex;
538 state->lock_data.unlock = unlock_acpi_mutex;
543 static int asus_ec_bank_switch(u8 bank, u8 *old)
548 status = ec_read(ASUS_EC_BANK_REGISTER, old);
550 if (status || (old && (*old == bank)))
552 return ec_write(ASUS_EC_BANK_REGISTER, bank);
555 static int asus_ec_block_read(const struct device *dev,
556 struct ec_sensors_data *ec)
558 int ireg, ibank, status;
559 u8 bank, reg_bank, prev_bank;
562 status = asus_ec_bank_switch(bank, &prev_bank);
564 dev_warn(dev, "EC bank switch failed");
569 /* oops... somebody else is working with the EC too */
571 "Concurrent access to the ACPI EC detected.\nRace condition possible.");
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",
584 for (ireg = 0; ireg < ec->nr_registers; ireg++) {
585 reg_bank = register_bank(ec->registers[ireg]);
586 if (reg_bank < bank) {
589 ec_read(register_index(ec->registers[ireg]),
590 ec->read_buffer + ireg);
594 status = asus_ec_bank_switch(prev_bank, NULL);
598 static inline s32 get_sensor_value(const struct ec_sensor_info *si, u8 *data)
600 if (is_sensor_data_signed(si)) {
601 switch (si->addr.components.size) {
605 return (s16)get_unaligned_be16(data);
607 return (s32)get_unaligned_be32(data);
612 switch (si->addr.components.size) {
616 return get_unaligned_be16(data);
618 return get_unaligned_be32(data);
625 static void update_sensor_values(struct ec_sensors_data *ec, u8 *data)
627 const struct ec_sensor_info *si;
628 struct ec_sensor *s, *sensor_end;
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;
638 static int update_ec_sensors(const struct device *dev,
639 struct ec_sensors_data *ec)
643 if (!ec->lock_data.lock(&ec->lock_data)) {
644 dev_warn(dev, "Failed to acquire mutex");
648 status = asus_ec_block_read(dev, ec);
651 update_sensor_values(ec, ec->read_buffer);
654 if (!ec->lock_data.unlock(&ec->lock_data))
655 dev_err(dev, "Failed to release mutex");
660 static long scale_sensor_value(s32 value, int data_type)
665 return value * MILLI;
671 static int get_cached_value_or_update(const struct device *dev,
673 struct ec_sensors_data *state, s32 *value)
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");
681 state->last_updated = jiffies;
684 *value = state->sensors[sensor_index].cached_value;
689 * Now follow the functions that implement the hwmon interface
692 static int asus_ec_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
693 u32 attr, int channel, long *val)
698 struct ec_sensors_data *state = dev_get_drvdata(dev);
699 int sidx = find_ec_sensor_index(state, type, channel);
705 ret = get_cached_value_or_update(dev, sidx, state, &value);
707 *val = scale_sensor_value(value,
708 get_sensor_info(state, sidx)->type);
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)
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;
725 static umode_t asus_ec_hwmon_is_visible(const void *drvdata,
726 enum hwmon_sensor_types type, u32 attr,
729 const struct ec_sensors_data *state = drvdata;
731 return find_ec_sensor_index(state, type, channel) >= 0 ? S_IRUGO : 0;
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)
740 u32 *cfg = devm_kcalloc(dev, num + 1, sizeof(*cfg), GFP_KERNEL);
745 asus_ec_hwmon_chan->type = type;
746 asus_ec_hwmon_chan->config = cfg;
747 for (i = 0; i < num; i++, cfg++)
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,
759 static struct hwmon_chip_info asus_ec_chip_info = {
760 .ops = &asus_ec_hwmon_ops,
763 static const struct ec_board_info * __init get_board_info(void)
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;
769 if (!dmi_board_vendor || !dmi_board_name ||
770 strcasecmp(dmi_board_vendor, "ASUSTeK COMPUTER INC."))
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)
783 static int __init asus_ec_probe(struct platform_device *pdev)
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;
798 pboard_info = get_board_info();
802 ec_data = devm_kzalloc(dev, sizeof(struct ec_sensors_data),
807 dev_set_drvdata(dev, ec_data);
808 ec_data->board_info = pboard_info;
810 switch (ec_data->board_info->family) {
811 case family_amd_400_series:
812 ec_data->sensors_info = sensors_family_amd_400;
814 case family_amd_500_series:
815 ec_data->sensors_info = sensors_family_amd_500;
817 case family_intel_600_series:
818 ec_data->sensors_info = sensors_family_intel_600;
821 dev_err(dev, "Unknown board family: %d",
822 ec_data->board_info->family);
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);
830 status = setup_lock_data(dev);
832 dev_err(dev, "Failed to setup state/EC locking: %d", status);
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);
842 if (!ec_data->registers || !ec_data->read_buffer)
845 fill_ec_registers(ec_data);
847 for (i = 0; i < ec_data->nr_sensors; ++i) {
848 si = get_sensor_info(ec_data, i);
849 if (!nr_count[si->type])
851 ++nr_count[si->type];
854 if (nr_count[hwmon_temp])
855 nr_count[hwmon_chip]++, nr_types++;
857 asus_ec_hwmon_chan = devm_kcalloc(
858 dev, nr_types, sizeof(*asus_ec_hwmon_chan), GFP_KERNEL);
859 if (!asus_ec_hwmon_chan)
862 ptr_asus_ec_ci = devm_kcalloc(dev, nr_types + 1,
863 sizeof(*ptr_asus_ec_ci), GFP_KERNEL);
867 asus_ec_chip_info.info = ptr_asus_ec_ci;
868 chip_info = &asus_ec_chip_info;
870 for (type = 0; type < hwmon_max; ++type) {
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++;
880 dev_info(dev, "board has %d EC sensors that span %d registers",
881 ec_data->nr_sensors, ec_data->nr_registers);
883 hwdev = devm_hwmon_device_register_with_info(dev, "asusec",
884 ec_data, chip_info, NULL);
886 return PTR_ERR_OR_ZERO(hwdev);
890 static const struct acpi_device_id acpi_ec_ids[] = {
891 /* Embedded Controller Device */
896 static struct platform_driver asus_ec_sensors_platform_driver = {
898 .name = "asus-ec-sensors",
899 .acpi_match_table = acpi_ec_ids,
903 MODULE_DEVICE_TABLE(acpi, acpi_ec_ids);
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.
911 module_platform_driver_probe(asus_ec_sensors_platform_driver, asus_ec_probe);
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");
917 MODULE_AUTHOR("Eugene Shalygin <eugene.shalygin@gmail.com>");
919 "HWMON driver for sensors accessible via ACPI EC in ASUS motherboards");
920 MODULE_LICENSE("GPL");