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 MAX_IDENTICAL_BOARD_VARIATIONS 3
59 /* Moniker for the ACPI global lock (':' is not allowed in ASL identifiers) */
60 #define ACPI_GLOBAL_LOCK_PSEUDO_PATH ":GLOBAL_LOCK"
72 #define MAKE_SENSOR_ADDRESS(size, bank, index) { \
73 .value = (size << 16) + (bank << 8) + index \
76 static u32 hwmon_attributes[hwmon_max] = {
77 [hwmon_chip] = HWMON_C_REGISTER_TZ,
78 [hwmon_temp] = HWMON_T_INPUT | HWMON_T_LABEL,
79 [hwmon_in] = HWMON_I_INPUT | HWMON_I_LABEL,
80 [hwmon_curr] = HWMON_C_INPUT | HWMON_C_LABEL,
81 [hwmon_fan] = HWMON_F_INPUT | HWMON_F_LABEL,
84 struct ec_sensor_info {
85 char label[SENSOR_LABEL_LEN];
86 enum hwmon_sensor_types type;
90 #define EC_SENSOR(sensor_label, sensor_type, size, bank, index) { \
91 .label = sensor_label, .type = sensor_type, \
92 .addr = MAKE_SENSOR_ADDRESS(size, bank, index), \
96 /* chipset temperature [℃] */
97 ec_sensor_temp_chipset,
98 /* CPU temperature [℃] */
100 /* motherboard temperature [℃] */
102 /* "T_Sensor" temperature sensor reading [℃] */
103 ec_sensor_temp_t_sensor,
104 /* VRM temperature [℃] */
106 /* CPU Core voltage [mV] */
107 ec_sensor_in_cpu_core,
108 /* CPU_Opt fan [RPM] */
109 ec_sensor_fan_cpu_opt,
110 /* VRM heat sink fan [RPM] */
111 ec_sensor_fan_vrm_hs,
112 /* Chipset fan [RPM] */
113 ec_sensor_fan_chipset,
114 /* Water flow sensor reading [RPM] */
115 ec_sensor_fan_water_flow,
116 /* CPU current [A] */
118 /* "Water_In" temperature sensor reading [℃] */
119 ec_sensor_temp_water_in,
120 /* "Water_Out" temperature sensor reading [℃] */
121 ec_sensor_temp_water_out,
124 #define SENSOR_TEMP_CHIPSET BIT(ec_sensor_temp_chipset)
125 #define SENSOR_TEMP_CPU BIT(ec_sensor_temp_cpu)
126 #define SENSOR_TEMP_MB BIT(ec_sensor_temp_mb)
127 #define SENSOR_TEMP_T_SENSOR BIT(ec_sensor_temp_t_sensor)
128 #define SENSOR_TEMP_VRM BIT(ec_sensor_temp_vrm)
129 #define SENSOR_IN_CPU_CORE BIT(ec_sensor_in_cpu_core)
130 #define SENSOR_FAN_CPU_OPT BIT(ec_sensor_fan_cpu_opt)
131 #define SENSOR_FAN_VRM_HS BIT(ec_sensor_fan_vrm_hs)
132 #define SENSOR_FAN_CHIPSET BIT(ec_sensor_fan_chipset)
133 #define SENSOR_FAN_WATER_FLOW BIT(ec_sensor_fan_water_flow)
134 #define SENSOR_CURR_CPU BIT(ec_sensor_curr_cpu)
135 #define SENSOR_TEMP_WATER_IN BIT(ec_sensor_temp_water_in)
136 #define SENSOR_TEMP_WATER_OUT BIT(ec_sensor_temp_water_out)
140 family_amd_400_series,
141 family_amd_500_series,
144 /* All the known sensors for ASUS EC controllers */
145 static const struct ec_sensor_info sensors_family_amd_400[] = {
146 [ec_sensor_temp_chipset] =
147 EC_SENSOR("Chipset", hwmon_temp, 1, 0x00, 0x3a),
148 [ec_sensor_temp_cpu] =
149 EC_SENSOR("CPU", hwmon_temp, 1, 0x00, 0x3b),
150 [ec_sensor_temp_mb] =
151 EC_SENSOR("Motherboard", hwmon_temp, 1, 0x00, 0x3c),
152 [ec_sensor_temp_t_sensor] =
153 EC_SENSOR("T_Sensor", hwmon_temp, 1, 0x00, 0x3d),
154 [ec_sensor_temp_vrm] =
155 EC_SENSOR("VRM", hwmon_temp, 1, 0x00, 0x3e),
156 [ec_sensor_in_cpu_core] =
157 EC_SENSOR("CPU Core", hwmon_in, 2, 0x00, 0xa2),
158 [ec_sensor_fan_cpu_opt] =
159 EC_SENSOR("CPU_Opt", hwmon_fan, 2, 0x00, 0xbc),
160 [ec_sensor_fan_vrm_hs] =
161 EC_SENSOR("VRM HS", hwmon_fan, 2, 0x00, 0xb2),
162 [ec_sensor_fan_chipset] =
163 /* no chipset fans in this generation */
164 EC_SENSOR("Chipset", hwmon_fan, 0, 0x00, 0x00),
165 [ec_sensor_fan_water_flow] =
166 EC_SENSOR("Water_Flow", hwmon_fan, 2, 0x00, 0xb4),
167 [ec_sensor_curr_cpu] =
168 EC_SENSOR("CPU", hwmon_curr, 1, 0x00, 0xf4),
169 [ec_sensor_temp_water_in] =
170 EC_SENSOR("Water_In", hwmon_temp, 1, 0x01, 0x0d),
171 [ec_sensor_temp_water_out] =
172 EC_SENSOR("Water_Out", hwmon_temp, 1, 0x01, 0x0b),
175 static const struct ec_sensor_info sensors_family_amd_500[] = {
176 [ec_sensor_temp_chipset] =
177 EC_SENSOR("Chipset", hwmon_temp, 1, 0x00, 0x3a),
178 [ec_sensor_temp_cpu] = EC_SENSOR("CPU", hwmon_temp, 1, 0x00, 0x3b),
179 [ec_sensor_temp_mb] =
180 EC_SENSOR("Motherboard", hwmon_temp, 1, 0x00, 0x3c),
181 [ec_sensor_temp_t_sensor] =
182 EC_SENSOR("T_Sensor", hwmon_temp, 1, 0x00, 0x3d),
183 [ec_sensor_temp_vrm] = EC_SENSOR("VRM", hwmon_temp, 1, 0x00, 0x3e),
184 [ec_sensor_in_cpu_core] =
185 EC_SENSOR("CPU Core", hwmon_in, 2, 0x00, 0xa2),
186 [ec_sensor_fan_cpu_opt] =
187 EC_SENSOR("CPU_Opt", hwmon_fan, 2, 0x00, 0xb0),
188 [ec_sensor_fan_vrm_hs] = EC_SENSOR("VRM HS", hwmon_fan, 2, 0x00, 0xb2),
189 [ec_sensor_fan_chipset] =
190 EC_SENSOR("Chipset", hwmon_fan, 2, 0x00, 0xb4),
191 [ec_sensor_fan_water_flow] =
192 EC_SENSOR("Water_Flow", hwmon_fan, 2, 0x00, 0xbc),
193 [ec_sensor_curr_cpu] = EC_SENSOR("CPU", hwmon_curr, 1, 0x00, 0xf4),
194 [ec_sensor_temp_water_in] =
195 EC_SENSOR("Water_In", hwmon_temp, 1, 0x01, 0x00),
196 [ec_sensor_temp_water_out] =
197 EC_SENSOR("Water_Out", hwmon_temp, 1, 0x01, 0x01),
200 /* Shortcuts for common combinations */
201 #define SENSOR_SET_TEMP_CHIPSET_CPU_MB \
202 (SENSOR_TEMP_CHIPSET | SENSOR_TEMP_CPU | SENSOR_TEMP_MB)
203 #define SENSOR_SET_TEMP_WATER (SENSOR_TEMP_WATER_IN | SENSOR_TEMP_WATER_OUT)
205 struct ec_board_info {
206 const char *board_names[MAX_IDENTICAL_BOARD_VARIATIONS];
207 unsigned long sensors;
209 * Defines which mutex to use for guarding access to the state and the
210 * hardware. Can be either a full path to an AML mutex or the
211 * pseudo-path ACPI_GLOBAL_LOCK_PSEUDO_PATH to use the global ACPI lock,
212 * or left empty to use a regular mutex object, in which case access to
213 * the hardware is not guarded.
215 const char *mutex_path;
216 enum board_family family;
219 static const struct ec_board_info board_info[] = {
221 .board_names = {"PRIME X470-PRO"},
222 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
223 SENSOR_TEMP_T_SENSOR | SENSOR_TEMP_VRM |
225 SENSOR_CURR_CPU | SENSOR_IN_CPU_CORE,
226 .mutex_path = ACPI_GLOBAL_LOCK_PSEUDO_PATH,
227 .family = family_amd_400_series,
230 .board_names = {"PRIME X570-PRO"},
231 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB | SENSOR_TEMP_VRM |
232 SENSOR_TEMP_T_SENSOR | SENSOR_FAN_CHIPSET,
233 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
234 .family = family_amd_500_series,
237 .board_names = {"ProArt X570-CREATOR WIFI"},
238 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB | SENSOR_TEMP_VRM |
239 SENSOR_TEMP_T_SENSOR | SENSOR_FAN_CPU_OPT |
240 SENSOR_CURR_CPU | SENSOR_IN_CPU_CORE,
243 .board_names = {"Pro WS X570-ACE"},
244 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB | SENSOR_TEMP_VRM |
245 SENSOR_TEMP_T_SENSOR | SENSOR_FAN_CHIPSET |
246 SENSOR_CURR_CPU | SENSOR_IN_CPU_CORE,
247 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
248 .family = family_amd_500_series,
251 .board_names = {"ROG CROSSHAIR VIII DARK HERO"},
252 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
253 SENSOR_TEMP_T_SENSOR |
254 SENSOR_TEMP_VRM | SENSOR_SET_TEMP_WATER |
255 SENSOR_FAN_CPU_OPT | SENSOR_FAN_WATER_FLOW |
256 SENSOR_CURR_CPU | SENSOR_IN_CPU_CORE,
257 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
258 .family = family_amd_500_series,
262 "ROG CROSSHAIR VIII FORMULA",
263 "ROG CROSSHAIR VIII HERO",
264 "ROG CROSSHAIR VIII HERO (WI-FI)",
266 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
267 SENSOR_TEMP_T_SENSOR |
268 SENSOR_TEMP_VRM | SENSOR_SET_TEMP_WATER |
269 SENSOR_FAN_CPU_OPT | SENSOR_FAN_CHIPSET |
270 SENSOR_FAN_WATER_FLOW | SENSOR_CURR_CPU |
272 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
273 .family = family_amd_500_series,
276 .board_names = {"ROG CROSSHAIR VIII IMPACT"},
277 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
278 SENSOR_TEMP_T_SENSOR | SENSOR_TEMP_VRM |
279 SENSOR_FAN_CHIPSET | SENSOR_CURR_CPU |
281 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
282 .family = family_amd_500_series,
285 .board_names = {"ROG STRIX B550-E GAMING"},
286 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
287 SENSOR_TEMP_T_SENSOR | SENSOR_TEMP_VRM |
289 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
290 .family = family_amd_500_series,
293 .board_names = {"ROG STRIX B550-I GAMING"},
294 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
295 SENSOR_TEMP_T_SENSOR | SENSOR_TEMP_VRM |
296 SENSOR_FAN_VRM_HS | SENSOR_CURR_CPU |
298 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
299 .family = family_amd_500_series,
302 .board_names = {"ROG STRIX X570-E GAMING"},
303 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
304 SENSOR_TEMP_T_SENSOR | SENSOR_TEMP_VRM |
305 SENSOR_FAN_CHIPSET | 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 WIFI II"},
312 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
313 SENSOR_TEMP_T_SENSOR | SENSOR_CURR_CPU |
315 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
316 .family = family_amd_500_series,
319 .board_names = {"ROG STRIX X570-F GAMING"},
320 .sensors = SENSOR_SET_TEMP_CHIPSET_CPU_MB |
321 SENSOR_TEMP_T_SENSOR | SENSOR_FAN_CHIPSET,
322 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
323 .family = family_amd_500_series,
326 .board_names = {"ROG STRIX X570-I GAMING"},
327 .sensors = SENSOR_TEMP_T_SENSOR | SENSOR_FAN_VRM_HS |
328 SENSOR_FAN_CHIPSET | SENSOR_CURR_CPU |
330 .mutex_path = ASUS_HW_ACCESS_MUTEX_ASMX,
331 .family = family_amd_500_series,
337 unsigned int info_index;
344 /* global lock handle */
347 bool (*lock)(struct lock_data *data);
348 bool (*unlock)(struct lock_data *data);
352 * The next function pairs implement options for locking access to the
355 static bool lock_via_acpi_mutex(struct lock_data *data)
358 * ASUS DSDT does not specify that access to the EC has to be guarded,
359 * but firmware does access it via ACPI
361 return ACPI_SUCCESS(acpi_acquire_mutex(data->mutex.aml,
362 NULL, ACPI_LOCK_DELAY_MS));
365 static bool unlock_acpi_mutex(struct lock_data *data)
367 return ACPI_SUCCESS(acpi_release_mutex(data->mutex.aml, NULL));
370 static bool lock_via_global_acpi_lock(struct lock_data *data)
372 return ACPI_SUCCESS(acpi_acquire_global_lock(ACPI_LOCK_DELAY_MS,
376 static bool unlock_global_acpi_lock(struct lock_data *data)
378 return ACPI_SUCCESS(acpi_release_global_lock(data->mutex.glk));
381 struct ec_sensors_data {
382 const struct ec_board_info *board_info;
383 const struct ec_sensor_info *sensors_info;
384 struct ec_sensor *sensors;
385 /* EC registers to read from */
388 /* sorted list of unique register banks */
389 u8 banks[ASUS_EC_MAX_BANK + 1];
391 unsigned long last_updated;
392 struct lock_data lock_data;
393 /* number of board EC sensors */
396 * number of EC registers to read
397 * (sensor might span more than 1 register)
400 /* number of unique register banks */
404 static u8 register_bank(u16 reg)
409 static u8 register_index(u16 reg)
414 static bool is_sensor_data_signed(const struct ec_sensor_info *si)
417 * guessed from WMI functions in DSDT code for boards
418 * of the X470 generation
420 return si->type == hwmon_temp;
423 static const struct ec_sensor_info *
424 get_sensor_info(const struct ec_sensors_data *state, int index)
426 return state->sensors_info + state->sensors[index].info_index;
429 static int find_ec_sensor_index(const struct ec_sensors_data *ec,
430 enum hwmon_sensor_types type, int channel)
434 for (i = 0; i < ec->nr_sensors; i++) {
435 if (get_sensor_info(ec, i)->type == type) {
444 static int __init bank_compare(const void *a, const void *b)
446 return *((const s8 *)a) - *((const s8 *)b);
449 static void __init setup_sensor_data(struct ec_sensors_data *ec)
451 struct ec_sensor *s = ec->sensors;
457 ec->nr_registers = 0;
459 for_each_set_bit(i, &ec->board_info->sensors,
460 BITS_PER_TYPE(ec->board_info->sensors)) {
464 ec->sensors_info[s->info_index].addr.components.size;
466 bank = ec->sensors_info[s->info_index].addr.components.bank;
467 for (j = 0; j < ec->nr_banks; j++) {
468 if (ec->banks[j] == bank) {
474 ec->banks[ec->nr_banks++] = bank;
478 sort(ec->banks, ec->nr_banks, 1, bank_compare, NULL);
481 static void __init fill_ec_registers(struct ec_sensors_data *ec)
483 const struct ec_sensor_info *si;
484 unsigned int i, j, register_idx = 0;
486 for (i = 0; i < ec->nr_sensors; ++i) {
487 si = get_sensor_info(ec, i);
488 for (j = 0; j < si->addr.components.size; ++j, ++register_idx) {
489 ec->registers[register_idx] =
490 (si->addr.components.bank << 8) +
491 si->addr.components.index + j;
496 static int __init setup_lock_data(struct device *dev)
498 const char *mutex_path;
500 struct ec_sensors_data *state = dev_get_drvdata(dev);
502 mutex_path = mutex_path_override ?
503 mutex_path_override : state->board_info->mutex_path;
505 if (!mutex_path || !strlen(mutex_path)) {
506 dev_err(dev, "Hardware access guard mutex name is empty");
509 if (!strcmp(mutex_path, ACPI_GLOBAL_LOCK_PSEUDO_PATH)) {
510 state->lock_data.mutex.glk = 0;
511 state->lock_data.lock = lock_via_global_acpi_lock;
512 state->lock_data.unlock = unlock_global_acpi_lock;
514 status = acpi_get_handle(NULL, (acpi_string)mutex_path,
515 &state->lock_data.mutex.aml);
516 if (ACPI_FAILURE(status)) {
518 "Failed to get hardware access guard AML mutex '%s': error %d",
522 state->lock_data.lock = lock_via_acpi_mutex;
523 state->lock_data.unlock = unlock_acpi_mutex;
528 static int asus_ec_bank_switch(u8 bank, u8 *old)
533 status = ec_read(ASUS_EC_BANK_REGISTER, old);
535 if (status || (old && (*old == bank)))
537 return ec_write(ASUS_EC_BANK_REGISTER, bank);
540 static int asus_ec_block_read(const struct device *dev,
541 struct ec_sensors_data *ec)
543 int ireg, ibank, status;
544 u8 bank, reg_bank, prev_bank;
547 status = asus_ec_bank_switch(bank, &prev_bank);
549 dev_warn(dev, "EC bank switch failed");
554 /* oops... somebody else is working with the EC too */
556 "Concurrent access to the ACPI EC detected.\nRace condition possible.");
559 /* read registers minimizing bank switches. */
560 for (ibank = 0; ibank < ec->nr_banks; ibank++) {
561 if (bank != ec->banks[ibank]) {
562 bank = ec->banks[ibank];
563 if (asus_ec_bank_switch(bank, NULL)) {
564 dev_warn(dev, "EC bank switch to %d failed",
569 for (ireg = 0; ireg < ec->nr_registers; ireg++) {
570 reg_bank = register_bank(ec->registers[ireg]);
571 if (reg_bank < bank) {
574 ec_read(register_index(ec->registers[ireg]),
575 ec->read_buffer + ireg);
579 status = asus_ec_bank_switch(prev_bank, NULL);
583 static inline s32 get_sensor_value(const struct ec_sensor_info *si, u8 *data)
585 if (is_sensor_data_signed(si)) {
586 switch (si->addr.components.size) {
590 return (s16)get_unaligned_be16(data);
592 return (s32)get_unaligned_be32(data);
597 switch (si->addr.components.size) {
601 return get_unaligned_be16(data);
603 return get_unaligned_be32(data);
610 static void update_sensor_values(struct ec_sensors_data *ec, u8 *data)
612 const struct ec_sensor_info *si;
613 struct ec_sensor *s, *sensor_end;
615 sensor_end = ec->sensors + ec->nr_sensors;
616 for (s = ec->sensors; s != sensor_end; s++) {
617 si = ec->sensors_info + s->info_index;
618 s->cached_value = get_sensor_value(si, data);
619 data += si->addr.components.size;
623 static int update_ec_sensors(const struct device *dev,
624 struct ec_sensors_data *ec)
628 if (!ec->lock_data.lock(&ec->lock_data)) {
629 dev_warn(dev, "Failed to acquire mutex");
633 status = asus_ec_block_read(dev, ec);
636 update_sensor_values(ec, ec->read_buffer);
639 if (!ec->lock_data.unlock(&ec->lock_data))
640 dev_err(dev, "Failed to release mutex");
645 static long scale_sensor_value(s32 value, int data_type)
650 return value * MILLI;
656 static int get_cached_value_or_update(const struct device *dev,
658 struct ec_sensors_data *state, s32 *value)
660 if (time_after(jiffies, state->last_updated + HZ)) {
661 if (update_ec_sensors(dev, state)) {
662 dev_err(dev, "update_ec_sensors() failure\n");
666 state->last_updated = jiffies;
669 *value = state->sensors[sensor_index].cached_value;
674 * Now follow the functions that implement the hwmon interface
677 static int asus_ec_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
678 u32 attr, int channel, long *val)
683 struct ec_sensors_data *state = dev_get_drvdata(dev);
684 int sidx = find_ec_sensor_index(state, type, channel);
690 ret = get_cached_value_or_update(dev, sidx, state, &value);
692 *val = scale_sensor_value(value,
693 get_sensor_info(state, sidx)->type);
699 static int asus_ec_hwmon_read_string(struct device *dev,
700 enum hwmon_sensor_types type, u32 attr,
701 int channel, const char **str)
703 struct ec_sensors_data *state = dev_get_drvdata(dev);
704 int sensor_index = find_ec_sensor_index(state, type, channel);
705 *str = get_sensor_info(state, sensor_index)->label;
710 static umode_t asus_ec_hwmon_is_visible(const void *drvdata,
711 enum hwmon_sensor_types type, u32 attr,
714 const struct ec_sensors_data *state = drvdata;
716 return find_ec_sensor_index(state, type, channel) >= 0 ? S_IRUGO : 0;
720 asus_ec_hwmon_add_chan_info(struct hwmon_channel_info *asus_ec_hwmon_chan,
721 struct device *dev, int num,
722 enum hwmon_sensor_types type, u32 config)
725 u32 *cfg = devm_kcalloc(dev, num + 1, sizeof(*cfg), GFP_KERNEL);
730 asus_ec_hwmon_chan->type = type;
731 asus_ec_hwmon_chan->config = cfg;
732 for (i = 0; i < num; i++, cfg++)
738 static const struct hwmon_ops asus_ec_hwmon_ops = {
739 .is_visible = asus_ec_hwmon_is_visible,
740 .read = asus_ec_hwmon_read,
741 .read_string = asus_ec_hwmon_read_string,
744 static struct hwmon_chip_info asus_ec_chip_info = {
745 .ops = &asus_ec_hwmon_ops,
748 static const struct ec_board_info * __init get_board_info(void)
750 const char *dmi_board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
751 const char *dmi_board_name = dmi_get_system_info(DMI_BOARD_NAME);
752 const struct ec_board_info *board;
754 if (!dmi_board_vendor || !dmi_board_name ||
755 strcasecmp(dmi_board_vendor, "ASUSTeK COMPUTER INC."))
758 for (board = board_info; board->sensors; board++) {
759 if (match_string(board->board_names,
760 MAX_IDENTICAL_BOARD_VARIATIONS,
761 dmi_board_name) >= 0)
768 static int __init asus_ec_probe(struct platform_device *pdev)
770 const struct hwmon_channel_info **ptr_asus_ec_ci;
771 int nr_count[hwmon_max] = { 0 }, nr_types = 0;
772 struct hwmon_channel_info *asus_ec_hwmon_chan;
773 const struct ec_board_info *pboard_info;
774 const struct hwmon_chip_info *chip_info;
775 struct device *dev = &pdev->dev;
776 struct ec_sensors_data *ec_data;
777 const struct ec_sensor_info *si;
778 enum hwmon_sensor_types type;
779 struct device *hwdev;
783 pboard_info = get_board_info();
787 ec_data = devm_kzalloc(dev, sizeof(struct ec_sensors_data),
792 dev_set_drvdata(dev, ec_data);
793 ec_data->board_info = pboard_info;
795 switch (ec_data->board_info->family) {
796 case family_amd_400_series:
797 ec_data->sensors_info = sensors_family_amd_400;
799 case family_amd_500_series:
800 ec_data->sensors_info = sensors_family_amd_500;
803 dev_err(dev, "Unknown board family: %d",
804 ec_data->board_info->family);
808 ec_data->nr_sensors = hweight_long(ec_data->board_info->sensors);
809 ec_data->sensors = devm_kcalloc(dev, ec_data->nr_sensors,
810 sizeof(struct ec_sensor), GFP_KERNEL);
812 status = setup_lock_data(dev);
814 dev_err(dev, "Failed to setup state/EC locking: %d", status);
818 setup_sensor_data(ec_data);
819 ec_data->registers = devm_kcalloc(dev, ec_data->nr_registers,
820 sizeof(u16), GFP_KERNEL);
821 ec_data->read_buffer = devm_kcalloc(dev, ec_data->nr_registers,
822 sizeof(u8), GFP_KERNEL);
824 if (!ec_data->registers || !ec_data->read_buffer)
827 fill_ec_registers(ec_data);
829 for (i = 0; i < ec_data->nr_sensors; ++i) {
830 si = get_sensor_info(ec_data, i);
831 if (!nr_count[si->type])
833 ++nr_count[si->type];
836 if (nr_count[hwmon_temp])
837 nr_count[hwmon_chip]++, nr_types++;
839 asus_ec_hwmon_chan = devm_kcalloc(
840 dev, nr_types, sizeof(*asus_ec_hwmon_chan), GFP_KERNEL);
841 if (!asus_ec_hwmon_chan)
844 ptr_asus_ec_ci = devm_kcalloc(dev, nr_types + 1,
845 sizeof(*ptr_asus_ec_ci), GFP_KERNEL);
849 asus_ec_chip_info.info = ptr_asus_ec_ci;
850 chip_info = &asus_ec_chip_info;
852 for (type = 0; type < hwmon_max; ++type) {
856 asus_ec_hwmon_add_chan_info(asus_ec_hwmon_chan, dev,
857 nr_count[type], type,
858 hwmon_attributes[type]);
859 *ptr_asus_ec_ci++ = asus_ec_hwmon_chan++;
862 dev_info(dev, "board has %d EC sensors that span %d registers",
863 ec_data->nr_sensors, ec_data->nr_registers);
865 hwdev = devm_hwmon_device_register_with_info(dev, "asusec",
866 ec_data, chip_info, NULL);
868 return PTR_ERR_OR_ZERO(hwdev);
872 static const struct acpi_device_id acpi_ec_ids[] = {
873 /* Embedded Controller Device */
878 static struct platform_driver asus_ec_sensors_platform_driver = {
880 .name = "asus-ec-sensors",
881 .acpi_match_table = acpi_ec_ids,
885 MODULE_DEVICE_TABLE(acpi, acpi_ec_ids);
887 * we use module_platform_driver_probe() rather than module_platform_driver()
888 * because the probe function (and its dependants) are marked with __init, which
889 * means we can't put it into the .probe member of the platform_driver struct
890 * above, and we can't mark the asus_ec_sensors_platform_driver object as __init
891 * because the object is referenced from the module exit code.
893 module_platform_driver_probe(asus_ec_sensors_platform_driver, asus_ec_probe);
895 module_param_named(mutex_path, mutex_path_override, charp, 0);
896 MODULE_PARM_DESC(mutex_path,
897 "Override ACPI mutex path used to guard access to hardware");
899 MODULE_AUTHOR("Eugene Shalygin <eugene.shalygin@gmail.com>");
901 "HWMON driver for sensors accessible via ACPI EC in ASUS motherboards");
902 MODULE_LICENSE("GPL");