^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * abituguru.c Copyright (c) 2005-2006 Hans de Goede <hdegoede@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * This driver supports the sensor part of the first and second revision of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * the custom Abit uGuru chip found on Abit uGuru motherboards. Note: because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * of lack of specs the CPU/RAM voltage & frequency control is not supported!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/dmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /* Banks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define ABIT_UGURU_ALARM_BANK 0x20 /* 1x 3 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define ABIT_UGURU_SENSOR_BANK1 0x21 /* 16x volt and temp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define ABIT_UGURU_FAN_PWM 0x24 /* 3x 5 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define ABIT_UGURU_SENSOR_BANK2 0x26 /* fans */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* max nr of sensors in bank1, a bank1 sensor can be in, temp or nc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define ABIT_UGURU_MAX_BANK1_SENSORS 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * Warning if you increase one of the 2 MAX defines below to 10 or higher you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * should adjust the belonging _NAMES_LENGTH macro for the 2 digit number!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /* max nr of sensors in bank2, currently mb's with max 6 fans are known */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define ABIT_UGURU_MAX_BANK2_SENSORS 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* max nr of pwm outputs, currently mb's with max 5 pwm outputs are known */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define ABIT_UGURU_MAX_PWMS 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* uGuru sensor bank 1 flags */ /* Alarm if: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE 0x01 /* temp over warn */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE 0x02 /* volt over max */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define ABIT_UGURU_VOLT_LOW_ALARM_ENABLE 0x04 /* volt under min */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define ABIT_UGURU_TEMP_HIGH_ALARM_FLAG 0x10 /* temp is over warn */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define ABIT_UGURU_VOLT_HIGH_ALARM_FLAG 0x20 /* volt is over max */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define ABIT_UGURU_VOLT_LOW_ALARM_FLAG 0x40 /* volt is under min */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /* uGuru sensor bank 2 flags */ /* Alarm if: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define ABIT_UGURU_FAN_LOW_ALARM_ENABLE 0x01 /* fan under min */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* uGuru sensor bank common flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define ABIT_UGURU_BEEP_ENABLE 0x08 /* beep if alarm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define ABIT_UGURU_SHUTDOWN_ENABLE 0x80 /* shutdown if alarm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* uGuru fan PWM (speed control) flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define ABIT_UGURU_FAN_PWM_ENABLE 0x80 /* enable speed control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /* Values used for conversion */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define ABIT_UGURU_FAN_MAX 15300 /* RPM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* Bank1 sensor types */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define ABIT_UGURU_IN_SENSOR 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define ABIT_UGURU_TEMP_SENSOR 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define ABIT_UGURU_NC 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * In many cases we need to wait for the uGuru to reach a certain status, most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * of the time it will reach this status within 30 - 90 ISA reads, and thus we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * can best busy wait. This define gives the total amount of reads to try.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define ABIT_UGURU_WAIT_TIMEOUT 125
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * However sometimes older versions of the uGuru seem to be distracted and they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * do not respond for a long time. To handle this we sleep before each of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * last ABIT_UGURU_WAIT_TIMEOUT_SLEEP tries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define ABIT_UGURU_WAIT_TIMEOUT_SLEEP 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * Normally all expected status in abituguru_ready, are reported after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * first read, but sometimes not and we need to poll.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define ABIT_UGURU_READY_TIMEOUT 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /* Maximum 3 retries on timedout reads/writes, delay 200 ms before retrying */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define ABIT_UGURU_MAX_RETRIES 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define ABIT_UGURU_RETRY_DELAY (HZ/5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is an error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define ABIT_UGURU_MAX_TIMEOUTS 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* utility macros */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #define ABIT_UGURU_NAME "abituguru"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #define ABIT_UGURU_DEBUG(level, format, arg...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (level <= verbose) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) pr_debug(format , ## arg); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* Macros to help calculate the sysfs_names array length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * sum of strlen of: in??_input\0, in??_{min,max}\0, in??_{min,max}_alarm\0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * in??_{min,max}_alarm_enable\0, in??_beep\0, in??_shutdown\0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #define ABITUGURU_IN_NAMES_LENGTH (11 + 2 * 9 + 2 * 15 + 2 * 22 + 10 + 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * sum of strlen of: temp??_input\0, temp??_max\0, temp??_crit\0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * temp??_alarm\0, temp??_alarm_enable\0, temp??_beep\0, temp??_shutdown\0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #define ABITUGURU_TEMP_NAMES_LENGTH (13 + 11 + 12 + 13 + 20 + 12 + 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * sum of strlen of: fan?_input\0, fan?_min\0, fan?_alarm\0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * fan?_alarm_enable\0, fan?_beep\0, fan?_shutdown\0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define ABITUGURU_FAN_NAMES_LENGTH (11 + 9 + 11 + 18 + 10 + 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * sum of strlen of: pwm?_enable\0, pwm?_auto_channels_temp\0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * pwm?_auto_point{1,2}_pwm\0, pwm?_auto_point{1,2}_temp\0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #define ABITUGURU_PWM_NAMES_LENGTH (12 + 24 + 2 * 21 + 2 * 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* IN_NAMES_LENGTH > TEMP_NAMES_LENGTH so assume all bank1 sensors are in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #define ABITUGURU_SYSFS_NAMES_LENGTH ( \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) ABIT_UGURU_MAX_BANK1_SENSORS * ABITUGURU_IN_NAMES_LENGTH + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) ABIT_UGURU_MAX_BANK2_SENSORS * ABITUGURU_FAN_NAMES_LENGTH + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ABIT_UGURU_MAX_PWMS * ABITUGURU_PWM_NAMES_LENGTH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * All the macros below are named identical to the oguru and oguru2 programs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * reverse engineered by Olle Sandberg, hence the names might not be 100%
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * logical. I could come up with better names, but I prefer keeping the names
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * identical so that this driver can be compared with his work more easily.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /* Two i/o-ports are used by uGuru */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #define ABIT_UGURU_BASE 0x00E0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /* Used to tell uGuru what to read and to read the actual data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) #define ABIT_UGURU_CMD 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /* Mostly used to check if uGuru is busy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #define ABIT_UGURU_DATA 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #define ABIT_UGURU_REGION_LENGTH 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* uGuru status' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #define ABIT_UGURU_STATUS_WRITE 0x00 /* Ready to be written */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #define ABIT_UGURU_STATUS_READ 0x01 /* Ready to be read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #define ABIT_UGURU_STATUS_INPUT 0x08 /* More input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) #define ABIT_UGURU_STATUS_READY 0x09 /* Ready to be written */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* Constants */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* in (Volt) sensors go up to 3494 mV, temp to 255000 millidegrees Celsius */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static const int abituguru_bank1_max_value[2] = { 3494, 255000 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * Min / Max allowed values for sensor2 (fan) alarm threshold, these values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * correspond to 300-3000 RPM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static const u8 abituguru_bank2_min_threshold = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static const u8 abituguru_bank2_max_threshold = 50;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * Register 0 is a bitfield, 1 and 2 are pwm settings (255 = 100%), 3 and 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * are temperature trip points.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static const int abituguru_pwm_settings_multiplier[5] = { 0, 1, 1, 1000, 1000 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * Min / Max allowed values for pwm_settings. Note: pwm1 (CPU fan) is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * special case the minimum allowed pwm% setting for this is 30% (77) on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * some MB's this special case is handled in the code!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static const u8 abituguru_pwm_min[5] = { 0, 170, 170, 25, 25 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static const u8 abituguru_pwm_max[5] = { 0, 255, 255, 75, 75 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /* Insmod parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static bool force;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) module_param(force, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) MODULE_PARM_DESC(force, "Set to one to force detection.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static int bank1_types[ABIT_UGURU_MAX_BANK1_SENSORS] = { -1, -1, -1, -1, -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) module_param_array(bank1_types, int, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) MODULE_PARM_DESC(bank1_types, "Bank1 sensortype autodetection override:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) " -1 autodetect\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) " 0 volt sensor\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) " 1 temp sensor\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) " 2 not connected");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static int fan_sensors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) module_param(fan_sensors, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) "(0 = autodetect)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static int pwms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) module_param(pwms, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) "(0 = autodetect)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* Default verbose is 2, since this driver is still in the testing phase */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static int verbose = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) module_param(verbose, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) MODULE_PARM_DESC(verbose, "How verbose should the driver be? (0-3):\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) " 0 normal output\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) " 1 + verbose error reporting\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) " 2 + sensors type probing info\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) " 3 + retryable error reporting");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * For the Abit uGuru, we need to keep some data in memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * The structure is dynamically allocated, at the same time when a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * abituguru device is allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct abituguru_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct device *hwmon_dev; /* hwmon registered device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct mutex update_lock; /* protect access to data and uGuru */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) unsigned long last_updated; /* In jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) unsigned short addr; /* uguru base address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) char uguru_ready; /* is the uguru in ready state? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) unsigned char update_timeouts; /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * number of update timeouts since last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * successful update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * The sysfs attr and their names are generated automatically, for bank1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * we cannot use a predefined array because we don't know beforehand
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * of a sensor is a volt or a temp sensor, for bank2 and the pwms its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * easier todo things the same way. For in sensors we have 9 (temp 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * sysfs entries per sensor, for bank2 and pwms 6.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct sensor_device_attribute_2 sysfs_attr[
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) ABIT_UGURU_MAX_BANK1_SENSORS * 9 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /* Buffer to store the dynamically generated sysfs names */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) char sysfs_names[ABITUGURU_SYSFS_NAMES_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /* Bank 1 data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /* number of and addresses of [0] in, [1] temp sensors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) u8 bank1_sensors[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) u8 bank1_address[2][ABIT_UGURU_MAX_BANK1_SENSORS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) u8 bank1_value[ABIT_UGURU_MAX_BANK1_SENSORS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * This array holds 3 entries per sensor for the bank 1 sensor settings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * (flags, min, max for voltage / flags, warn, shutdown for temp).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) u8 bank1_settings[ABIT_UGURU_MAX_BANK1_SENSORS][3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * Maximum value for each sensor used for scaling in mV/millidegrees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * Celsius.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) u8 bank2_sensors; /* actual number of bank2 sensors found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) u8 bank2_value[ABIT_UGURU_MAX_BANK2_SENSORS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) u8 bank2_settings[ABIT_UGURU_MAX_BANK2_SENSORS][2]; /* flags, min */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /* Alarms 2 bytes for bank1, 1 byte for bank2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) u8 alarms[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) /* Fan PWM (speed control) 5 bytes per PWM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) u8 pwms; /* actual number of pwms found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) u8 pwm_settings[ABIT_UGURU_MAX_PWMS][5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static const char *never_happen = "This should never happen.";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static const char *report_this =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) "Please report this to the abituguru maintainer (see MAINTAINERS)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) /* wait till the uguru is in the specified state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static int abituguru_wait(struct abituguru_data *data, u8 state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int timeout = ABIT_UGURU_WAIT_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) while (inb_p(data->addr + ABIT_UGURU_DATA) != state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) timeout--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (timeout == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * sleep a bit before our last few tries, see the comment on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * this where ABIT_UGURU_WAIT_TIMEOUT_SLEEP is defined.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (timeout <= ABIT_UGURU_WAIT_TIMEOUT_SLEEP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) msleep(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /* Put the uguru in ready for input state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static int abituguru_ready(struct abituguru_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int timeout = ABIT_UGURU_READY_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (data->uguru_ready)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /* Reset? / Prepare for next read/write cycle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) outb(0x00, data->addr + ABIT_UGURU_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /* Wait till the uguru is ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) ABIT_UGURU_DEBUG(1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) "timeout exceeded waiting for ready state\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /* Cmd port MUST be read now and should contain 0xAC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) timeout--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (timeout == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) ABIT_UGURU_DEBUG(1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) "CMD reg does not hold 0xAC after ready command\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) msleep(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * After this the ABIT_UGURU_DATA port should contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * ABIT_UGURU_STATUS_INPUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) timeout = ABIT_UGURU_READY_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) while (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) timeout--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (timeout == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) ABIT_UGURU_DEBUG(1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) "state != more input after ready command\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) msleep(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) data->uguru_ready = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * Send the bank and then sensor address to the uGuru for the next read/write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * cycle. This function gets called as the first part of a read/write by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * abituguru_read and abituguru_write. This function should never be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * called by any other function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static int abituguru_send_address(struct abituguru_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) u8 bank_addr, u8 sensor_addr, int retries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * assume the caller does error handling itself if it has not requested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * any retries, and thus be quiet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) int report_errors = retries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * Make sure the uguru is ready and then send the bank address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * after this the uguru is no longer "ready".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (abituguru_ready(data) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) outb(bank_addr, data->addr + ABIT_UGURU_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) data->uguru_ready = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * Wait till the uguru is ABIT_UGURU_STATUS_INPUT state again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * and send the sensor addr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (abituguru_wait(data, ABIT_UGURU_STATUS_INPUT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (retries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) ABIT_UGURU_DEBUG(3, "timeout exceeded "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) "waiting for more input state, %d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) "tries remaining\n", retries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) set_current_state(TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) schedule_timeout(ABIT_UGURU_RETRY_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) retries--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (report_errors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) ABIT_UGURU_DEBUG(1, "timeout exceeded "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) "waiting for more input state "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) "(bank: %d)\n", (int)bank_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) outb(sensor_addr, data->addr + ABIT_UGURU_CMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * Read count bytes from sensor sensor_addr in bank bank_addr and store the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * result in buf, retry the send address part of the read retries times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) static int abituguru_read(struct abituguru_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) u8 bank_addr, u8 sensor_addr, u8 *buf, int count, int retries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /* Send the address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /* And read the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) ABIT_UGURU_DEBUG(retries ? 1 : 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) "timeout exceeded waiting for "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) "read state (bank: %d, sensor: %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) (int)bank_addr, (int)sensor_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) buf[i] = inb(data->addr + ABIT_UGURU_CMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) /* Last put the chip back in ready state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) abituguru_ready(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * Write count bytes from buf to sensor sensor_addr in bank bank_addr, the send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * address part of the write is always retried ABIT_UGURU_MAX_RETRIES times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static int abituguru_write(struct abituguru_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) u8 bank_addr, u8 sensor_addr, u8 *buf, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * We use the ready timeout as we have to wait for 0xAC just like the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * ready function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) int i, timeout = ABIT_UGURU_READY_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /* Send the address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) i = abituguru_send_address(data, bank_addr, sensor_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) ABIT_UGURU_MAX_RETRIES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) /* And write the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (abituguru_wait(data, ABIT_UGURU_STATUS_WRITE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) "write state (bank: %d, sensor: %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) (int)bank_addr, (int)sensor_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) outb(buf[i], data->addr + ABIT_UGURU_CMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * Now we need to wait till the chip is ready to be read again,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * so that we can read 0xAC as confirmation that our write has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) * succeeded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for read state "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) "after write (bank: %d, sensor: %d)\n", (int)bank_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) (int)sensor_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) /* Cmd port MUST be read now and should contain 0xAC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) timeout--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (timeout == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) "write (bank: %d, sensor: %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) (int)bank_addr, (int)sensor_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) msleep(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) /* Last put the chip back in ready state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) abituguru_ready(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * Detect sensor type. Temp and Volt sensors are enabled with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * different masks and will ignore enable masks not meant for them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * This enables us to test what kind of sensor we're dealing with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * By setting the alarm thresholds so that we will always get an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * alarm for sensor type X and then enabling the sensor as sensor type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * X, if we then get an alarm it is a sensor of type X.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) u8 sensor_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) u8 val, test_flag, buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) int i, ret = -ENODEV; /* error is the most common used retval :| */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) /* If overriden by the user return the user selected type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (bank1_types[sensor_addr] >= ABIT_UGURU_IN_SENSOR &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) bank1_types[sensor_addr] <= ABIT_UGURU_NC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) ABIT_UGURU_DEBUG(2, "assuming sensor type %d for bank1 sensor "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) "%d because of \"bank1_types\" module param\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) bank1_types[sensor_addr], (int)sensor_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) return bank1_types[sensor_addr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) /* First read the sensor and the current settings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, sensor_addr, &val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 1, ABIT_UGURU_MAX_RETRIES) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) /* Test val is sane / usable for sensor type detection. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if ((val < 10u) || (val > 250u)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) pr_warn("bank1-sensor: %d reading (%d) too close to limits, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) "unable to determine sensor type, skipping sensor\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) (int)sensor_addr, (int)val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) * assume no sensor is there for sensors for which we can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) * determine the sensor type because their reading is too close
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) * to their limits, this usually means no sensor is there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) return ABIT_UGURU_NC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) ABIT_UGURU_DEBUG(2, "testing bank1 sensor %d\n", (int)sensor_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * Volt sensor test, enable volt low alarm, set min value ridiculously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * high, or vica versa if the reading is very high. If its a volt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * sensor this should always give us an alarm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (val <= 240u) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) buf[0] = ABIT_UGURU_VOLT_LOW_ALARM_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) buf[1] = 245;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) buf[2] = 250;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) test_flag = ABIT_UGURU_VOLT_LOW_ALARM_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) buf[0] = ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) buf[1] = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) buf[2] = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) test_flag = ABIT_UGURU_VOLT_HIGH_ALARM_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) buf, 3) != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) goto abituguru_detect_bank1_sensor_type_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) * Now we need 20 ms to give the uguru time to read the sensors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * and raise a voltage alarm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) set_current_state(TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) schedule_timeout(HZ/50);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) /* Check for alarm and check the alarm is a volt low alarm. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) ABIT_UGURU_MAX_RETRIES) != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) goto abituguru_detect_bank1_sensor_type_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) sensor_addr, buf, 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) ABIT_UGURU_MAX_RETRIES) != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) goto abituguru_detect_bank1_sensor_type_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) if (buf[0] & test_flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) ABIT_UGURU_DEBUG(2, " found volt sensor\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) ret = ABIT_UGURU_IN_SENSOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) goto abituguru_detect_bank1_sensor_type_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) ABIT_UGURU_DEBUG(2, " alarm raised during volt "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) "sensor test, but volt range flag not set\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) ABIT_UGURU_DEBUG(2, " alarm not raised during volt sensor "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) "test\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * Temp sensor test, enable sensor as a temp sensor, set beep value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * ridiculously low (but not too low, otherwise uguru ignores it).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * If its a temp sensor this should always give us an alarm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) buf[0] = ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) buf[1] = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) buf[2] = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) buf, 3) != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) goto abituguru_detect_bank1_sensor_type_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) * Now we need 50 ms to give the uguru time to read the sensors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) * and raise a temp alarm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) set_current_state(TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) schedule_timeout(HZ/20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) /* Check for alarm and check the alarm is a temp high alarm. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) ABIT_UGURU_MAX_RETRIES) != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) goto abituguru_detect_bank1_sensor_type_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) sensor_addr, buf, 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) ABIT_UGURU_MAX_RETRIES) != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) goto abituguru_detect_bank1_sensor_type_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (buf[0] & ABIT_UGURU_TEMP_HIGH_ALARM_FLAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) ABIT_UGURU_DEBUG(2, " found temp sensor\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) ret = ABIT_UGURU_TEMP_SENSOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) goto abituguru_detect_bank1_sensor_type_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) ABIT_UGURU_DEBUG(2, " alarm raised during temp "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) "sensor test, but temp high flag not set\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) ABIT_UGURU_DEBUG(2, " alarm not raised during temp sensor "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) "test\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) ret = ABIT_UGURU_NC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) abituguru_detect_bank1_sensor_type_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) * Restore original settings, failing here is really BAD, it has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) * reported that some BIOS-es hang when entering the uGuru menu with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) * invalid settings present in the uGuru, so we try this 3 times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) for (i = 0; i < 3; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) sensor_addr, data->bank1_settings[sensor_addr],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 3) == 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (i == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) pr_err("Fatal error could not restore original settings. %s %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) never_happen, report_this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) * These functions try to find out how many sensors there are in bank2 and how
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * many pwms there are. The purpose of this is to make sure that we don't give
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) * the user the possibility to change settings for non-existent sensors / pwm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) * The uGuru will happily read / write whatever memory happens to be after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) * memory storing the PWM settings when reading/writing to a PWM which is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) * there. Notice even if we detect a PWM which doesn't exist we normally won't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) * write to it, unless the user tries to change the settings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) * Although the uGuru allows reading (settings) from non existing bank2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) * sensors, my version of the uGuru does seem to stop writing to them, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) * write function above aborts in this case with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) * "CMD reg does not hold 0xAC after write"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * Notice these 2 tests are non destructive iow read-only tests, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) * they would defeat their purpose. Although for the bank2_sensors detection a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) * read/write test would be feasible because of the reaction above, I've
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) * however opted to stay on the safe side.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) if (fan_sensors > 0 && fan_sensors <= ABIT_UGURU_MAX_BANK2_SENSORS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) data->bank2_sensors = fan_sensors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) ABIT_UGURU_DEBUG(2, "assuming %d fan sensors because of "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) "\"fan_sensors\" module param\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) (int)data->bank2_sensors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) ABIT_UGURU_DEBUG(2, "detecting number of fan sensors\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) * 0x89 are the known used bits:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) * -0x80 enable shutdown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) * -0x08 enable beep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) * -0x01 enable alarm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) * All other bits should be 0, but on some motherboards
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) * 0x40 (bit 6) is also high for some of the fans??
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (data->bank2_settings[i][0] & ~0xC9) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) "to be a fan sensor: settings[0] = %02X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) i, (unsigned int)data->bank2_settings[i][0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) /* check if the threshold is within the allowed range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if (data->bank2_settings[i][1] <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) abituguru_bank2_min_threshold) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) "to be a fan sensor: the threshold (%d) is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) "below the minimum (%d)\n", i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) (int)data->bank2_settings[i][1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) (int)abituguru_bank2_min_threshold);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (data->bank2_settings[i][1] >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) abituguru_bank2_max_threshold) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) "to be a fan sensor: the threshold (%d) is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) "above the maximum (%d)\n", i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) (int)data->bank2_settings[i][1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) (int)abituguru_bank2_max_threshold);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) data->bank2_sensors = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) ABIT_UGURU_DEBUG(2, " found: %d fan sensors\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) (int)data->bank2_sensors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) abituguru_detect_no_pwms(struct abituguru_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) if (pwms > 0 && pwms <= ABIT_UGURU_MAX_PWMS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) data->pwms = pwms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) "\"pwms\" module param\n", (int)data->pwms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) ABIT_UGURU_DEBUG(2, "detecting number of PWM outputs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) * 0x80 is the enable bit and the low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) * nibble is which temp sensor to use,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) * the other bits should be 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) if (data->pwm_settings[i][0] & ~0x8F) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) "to be a pwm channel: settings[0] = %02X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) i, (unsigned int)data->pwm_settings[i][0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) * the low nibble must correspond to one of the temp sensors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) * we've found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) for (j = 0; j < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][j] ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) (data->pwm_settings[i][0] & 0x0F))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if (j == data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) "to be a pwm channel: %d is not a valid temp "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) "sensor address\n", i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) data->pwm_settings[i][0] & 0x0F);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) /* check if all other settings are within the allowed range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) for (j = 1; j < 5; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) u8 min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) /* special case pwm1 min pwm% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if ((i == 0) && ((j == 1) || (j == 2)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) min = 77;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) min = abituguru_pwm_min[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) if (data->pwm_settings[i][j] < min) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) ABIT_UGURU_DEBUG(2, " pwm channel %d does "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) "not seem to be a pwm channel: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) "setting %d (%d) is below the minimum "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) "value (%d)\n", i, j,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) (int)data->pwm_settings[i][j],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) (int)min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) goto abituguru_detect_no_pwms_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (data->pwm_settings[i][j] > abituguru_pwm_max[j]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) ABIT_UGURU_DEBUG(2, " pwm channel %d does "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) "not seem to be a pwm channel: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) "setting %d (%d) is above the maximum "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) "value (%d)\n", i, j,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) (int)data->pwm_settings[i][j],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) (int)abituguru_pwm_max[j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) goto abituguru_detect_no_pwms_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) /* check that min temp < max temp and min pwm < max pwm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) if (data->pwm_settings[i][1] >= data->pwm_settings[i][2]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) "to be a pwm channel: min pwm (%d) >= "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) "max pwm (%d)\n", i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) (int)data->pwm_settings[i][1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) (int)data->pwm_settings[i][2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) if (data->pwm_settings[i][3] >= data->pwm_settings[i][4]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) "to be a pwm channel: min temp (%d) >= "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) "max temp (%d)\n", i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) (int)data->pwm_settings[i][3],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) (int)data->pwm_settings[i][4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) abituguru_detect_no_pwms_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) data->pwms = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) ABIT_UGURU_DEBUG(2, " found: %d PWM outputs\n", (int)data->pwms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) * Following are the sysfs callback functions. These functions expect:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) * sensor_device_attribute_2->index: sensor address/offset in the bank
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) * sensor_device_attribute_2->nr: register offset, bitmask or NA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) static struct abituguru_data *abituguru_update_device(struct device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) static ssize_t show_bank1_value(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) struct abituguru_data *data = abituguru_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) return sprintf(buf, "%d\n", (data->bank1_value[attr->index] *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) data->bank1_max_value[attr->index] + 128) / 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) static ssize_t show_bank1_setting(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) return sprintf(buf, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) (data->bank1_settings[attr->index][attr->nr] *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) data->bank1_max_value[attr->index] + 128) / 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) static ssize_t show_bank2_value(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) struct abituguru_data *data = abituguru_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) return sprintf(buf, "%d\n", (data->bank2_value[attr->index] *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) ABIT_UGURU_FAN_MAX + 128) / 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) static ssize_t show_bank2_setting(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) return sprintf(buf, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) (data->bank2_settings[attr->index][attr->nr] *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) ABIT_UGURU_FAN_MAX + 128) / 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) static ssize_t store_bank1_setting(struct device *dev, struct device_attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) *devattr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) ret = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) val = (val * 255 + data->bank1_max_value[attr->index] / 2) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) data->bank1_max_value[attr->index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) if (val > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) if (data->bank1_settings[attr->index][attr->nr] != val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) u8 orig_val = data->bank1_settings[attr->index][attr->nr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) data->bank1_settings[attr->index][attr->nr] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) attr->index, data->bank1_settings[attr->index],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 3) <= attr->nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) data->bank1_settings[attr->index][attr->nr] = orig_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) static ssize_t store_bank2_setting(struct device *dev, struct device_attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) *devattr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) ret = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) val = (val * 255 + ABIT_UGURU_FAN_MAX / 2) / ABIT_UGURU_FAN_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) /* this check can be done before taking the lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) if (val < abituguru_bank2_min_threshold ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) val > abituguru_bank2_max_threshold)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) if (data->bank2_settings[attr->index][attr->nr] != val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) u8 orig_val = data->bank2_settings[attr->index][attr->nr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) data->bank2_settings[attr->index][attr->nr] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK2 + 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) attr->index, data->bank2_settings[attr->index],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 2) <= attr->nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) data->bank2_settings[attr->index][attr->nr] = orig_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) static ssize_t show_bank1_alarm(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) struct abituguru_data *data = abituguru_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) * See if the alarm bit for this sensor is set, and if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) * alarm matches the type of alarm we're looking for (for volt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) * it can be either low or high). The type is stored in a few
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) * readonly bits in the settings part of the relevant sensor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) * The bitmask of the type is passed to us in attr->nr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) if ((data->alarms[attr->index / 8] & (0x01 << (attr->index % 8))) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) (data->bank1_settings[attr->index][0] & attr->nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) return sprintf(buf, "1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) return sprintf(buf, "0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) static ssize_t show_bank2_alarm(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) struct abituguru_data *data = abituguru_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) if (data->alarms[2] & (0x01 << attr->index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) return sprintf(buf, "1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) return sprintf(buf, "0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) static ssize_t show_bank1_mask(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) if (data->bank1_settings[attr->index][0] & attr->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) return sprintf(buf, "1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) return sprintf(buf, "0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) static ssize_t show_bank2_mask(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) if (data->bank2_settings[attr->index][0] & attr->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) return sprintf(buf, "1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) return sprintf(buf, "0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) static ssize_t store_bank1_mask(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) struct device_attribute *devattr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) u8 orig_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) unsigned long mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) ret = kstrtoul(buf, 10, &mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) orig_val = data->bank1_settings[attr->index][0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) if (mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) data->bank1_settings[attr->index][0] |= attr->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) data->bank1_settings[attr->index][0] &= ~attr->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) if ((data->bank1_settings[attr->index][0] != orig_val) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) (abituguru_write(data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) ABIT_UGURU_SENSOR_BANK1 + 2, attr->index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) data->bank1_settings[attr->index], 3) < 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) data->bank1_settings[attr->index][0] = orig_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) static ssize_t store_bank2_mask(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) struct device_attribute *devattr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) u8 orig_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) unsigned long mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) ret = kstrtoul(buf, 10, &mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) orig_val = data->bank2_settings[attr->index][0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) if (mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) data->bank2_settings[attr->index][0] |= attr->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) data->bank2_settings[attr->index][0] &= ~attr->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) if ((data->bank2_settings[attr->index][0] != orig_val) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) (abituguru_write(data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) ABIT_UGURU_SENSOR_BANK2 + 2, attr->index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) data->bank2_settings[attr->index], 2) < 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) data->bank2_settings[attr->index][0] = orig_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) /* Fan PWM (speed control) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) static ssize_t show_pwm_setting(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) return sprintf(buf, "%d\n", data->pwm_settings[attr->index][attr->nr] *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) abituguru_pwm_settings_multiplier[attr->nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) static ssize_t store_pwm_setting(struct device *dev, struct device_attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) *devattr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) u8 min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) ret = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) val = (val + abituguru_pwm_settings_multiplier[attr->nr] / 2) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) abituguru_pwm_settings_multiplier[attr->nr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) /* special case pwm1 min pwm% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) if ((attr->index == 0) && ((attr->nr == 1) || (attr->nr == 2)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) min = 77;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) min = abituguru_pwm_min[attr->nr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) /* this check can be done before taking the lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) if (val < min || val > abituguru_pwm_max[attr->nr])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) /* this check needs to be done after taking the lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) if ((attr->nr & 1) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) (val >= data->pwm_settings[attr->index][attr->nr + 1]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) else if (!(attr->nr & 1) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) (val <= data->pwm_settings[attr->index][attr->nr - 1]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) else if (data->pwm_settings[attr->index][attr->nr] != val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) u8 orig_val = data->pwm_settings[attr->index][attr->nr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) data->pwm_settings[attr->index][attr->nr] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) attr->index, data->pwm_settings[attr->index],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 5) <= attr->nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) data->pwm_settings[attr->index][attr->nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) orig_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) static ssize_t show_pwm_sensor(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) * We need to walk to the temp sensor addresses to find what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) * the userspace id of the configured temp sensor is.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) for (i = 0; i < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][i] ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) (data->pwm_settings[attr->index][0] & 0x0F))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) return sprintf(buf, "%d\n", i+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) static ssize_t store_pwm_sensor(struct device *dev, struct device_attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) *devattr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) u8 orig_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) u8 address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) ret = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) if (val == 0 || val > data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) val -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) orig_val = data->pwm_settings[attr->index][0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) address = data->bank1_address[ABIT_UGURU_TEMP_SENSOR][val];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) data->pwm_settings[attr->index][0] &= 0xF0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) data->pwm_settings[attr->index][0] |= address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) if (data->pwm_settings[attr->index][0] != orig_val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1, attr->index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) data->pwm_settings[attr->index], 5) < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) data->pwm_settings[attr->index][0] = orig_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) static ssize_t show_pwm_enable(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) if (data->pwm_settings[attr->index][0] & ABIT_UGURU_FAN_PWM_ENABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) res = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) return sprintf(buf, "%d\n", res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) *devattr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) u8 orig_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) unsigned long user_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) ret = kstrtoul(buf, 10, &user_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) orig_val = data->pwm_settings[attr->index][0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) switch (user_val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) data->pwm_settings[attr->index][0] &=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) ~ABIT_UGURU_FAN_PWM_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) data->pwm_settings[attr->index][0] |= ABIT_UGURU_FAN_PWM_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) if ((data->pwm_settings[attr->index][0] != orig_val) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) attr->index, data->pwm_settings[attr->index],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 5) < 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) data->pwm_settings[attr->index][0] = orig_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) static ssize_t show_name(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) return sprintf(buf, "%s\n", ABIT_UGURU_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) /* Sysfs attr templates, the real entries are generated automatically. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) static const
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) struct sensor_device_attribute_2 abituguru_sysfs_bank1_templ[2][9] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) SENSOR_ATTR_2(in%d_input, 0444, show_bank1_value, NULL, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) SENSOR_ATTR_2(in%d_min, 0644, show_bank1_setting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) store_bank1_setting, 1, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) SENSOR_ATTR_2(in%d_min_alarm, 0444, show_bank1_alarm, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) ABIT_UGURU_VOLT_LOW_ALARM_FLAG, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) SENSOR_ATTR_2(in%d_max, 0644, show_bank1_setting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) store_bank1_setting, 2, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) SENSOR_ATTR_2(in%d_max_alarm, 0444, show_bank1_alarm, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) ABIT_UGURU_VOLT_HIGH_ALARM_FLAG, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) SENSOR_ATTR_2(in%d_beep, 0644, show_bank1_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) SENSOR_ATTR_2(in%d_shutdown, 0644, show_bank1_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) SENSOR_ATTR_2(in%d_min_alarm_enable, 0644, show_bank1_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) store_bank1_mask, ABIT_UGURU_VOLT_LOW_ALARM_ENABLE, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) SENSOR_ATTR_2(in%d_max_alarm_enable, 0644, show_bank1_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) store_bank1_mask, ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) SENSOR_ATTR_2(temp%d_input, 0444, show_bank1_value, NULL, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) SENSOR_ATTR_2(temp%d_alarm, 0444, show_bank1_alarm, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) ABIT_UGURU_TEMP_HIGH_ALARM_FLAG, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) SENSOR_ATTR_2(temp%d_max, 0644, show_bank1_setting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) store_bank1_setting, 1, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) SENSOR_ATTR_2(temp%d_crit, 0644, show_bank1_setting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) store_bank1_setting, 2, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) SENSOR_ATTR_2(temp%d_beep, 0644, show_bank1_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) SENSOR_ATTR_2(temp%d_shutdown, 0644, show_bank1_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) SENSOR_ATTR_2(temp%d_alarm_enable, 0644, show_bank1_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) store_bank1_mask, ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) static const struct sensor_device_attribute_2 abituguru_sysfs_fan_templ[6] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) SENSOR_ATTR_2(fan%d_input, 0444, show_bank2_value, NULL, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) SENSOR_ATTR_2(fan%d_alarm, 0444, show_bank2_alarm, NULL, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) SENSOR_ATTR_2(fan%d_min, 0644, show_bank2_setting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) store_bank2_setting, 1, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) SENSOR_ATTR_2(fan%d_beep, 0644, show_bank2_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) store_bank2_mask, ABIT_UGURU_BEEP_ENABLE, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) SENSOR_ATTR_2(fan%d_shutdown, 0644, show_bank2_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) store_bank2_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) SENSOR_ATTR_2(fan%d_alarm_enable, 0644, show_bank2_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) store_bank2_mask, ABIT_UGURU_FAN_LOW_ALARM_ENABLE, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) static const struct sensor_device_attribute_2 abituguru_sysfs_pwm_templ[6] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) SENSOR_ATTR_2(pwm%d_enable, 0644, show_pwm_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) store_pwm_enable, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) SENSOR_ATTR_2(pwm%d_auto_channels_temp, 0644, show_pwm_sensor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) store_pwm_sensor, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) SENSOR_ATTR_2(pwm%d_auto_point1_pwm, 0644, show_pwm_setting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) store_pwm_setting, 1, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) SENSOR_ATTR_2(pwm%d_auto_point2_pwm, 0644, show_pwm_setting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) store_pwm_setting, 2, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) SENSOR_ATTR_2(pwm%d_auto_point1_temp, 0644, show_pwm_setting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) store_pwm_setting, 3, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) SENSOR_ATTR_2(pwm%d_auto_point2_temp, 0644, show_pwm_setting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) store_pwm_setting, 4, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) static struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) static int abituguru_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) struct abituguru_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) int i, j, used, sysfs_names_free, sysfs_attr_i, res = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) char *sysfs_filename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) * El weirdo probe order, to keep the sysfs order identical to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) * BIOS and window-appliction listing order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) static const u8 probe_order[ABIT_UGURU_MAX_BANK1_SENSORS] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E, 0x02,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) data = devm_kzalloc(&pdev->dev, sizeof(struct abituguru_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) platform_set_drvdata(pdev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) /* See if the uGuru is ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) if (inb_p(data->addr + ABIT_UGURU_DATA) == ABIT_UGURU_STATUS_INPUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) data->uguru_ready = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) * Completely read the uGuru this has 2 purposes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) * - testread / see if one really is there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) * - make an in memory copy of all the uguru settings for future use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) goto abituguru_probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) &data->bank1_value[i], 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) ABIT_UGURU_MAX_RETRIES) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) goto abituguru_probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1+1, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) data->bank1_settings[i], 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) ABIT_UGURU_MAX_RETRIES) != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) goto abituguru_probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) * Note: We don't know how many bank2 sensors / pwms there really are,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) * but in order to "detect" this we need to read the maximum amount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) * anyways. If we read sensors/pwms not there we'll just read crap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) * this can't hurt. We need the detection because we don't want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) * unwanted writes, which will hurt!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) &data->bank2_value[i], 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) ABIT_UGURU_MAX_RETRIES) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) goto abituguru_probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2+1, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) data->bank2_settings[i], 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) ABIT_UGURU_MAX_RETRIES) != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) goto abituguru_probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) if (abituguru_read(data, ABIT_UGURU_FAN_PWM, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) data->pwm_settings[i], 5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) ABIT_UGURU_MAX_RETRIES) != 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) goto abituguru_probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) /* Detect sensor types and fill the sysfs attr for bank1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) sysfs_attr_i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) sysfs_filename = data->sysfs_names;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) sysfs_names_free = ABITUGURU_SYSFS_NAMES_LENGTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) goto abituguru_probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) if (res == ABIT_UGURU_NC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) /* res 1 (temp) sensors have 7 sysfs entries, 0 (in) 9 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) for (j = 0; j < (res ? 7 : 9); j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) used = snprintf(sysfs_filename, sysfs_names_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) abituguru_sysfs_bank1_templ[res][j].dev_attr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) attr.name, data->bank1_sensors[res] + res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) data->sysfs_attr[sysfs_attr_i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) abituguru_sysfs_bank1_templ[res][j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) sysfs_filename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) sysfs_filename += used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) sysfs_names_free -= used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) sysfs_attr_i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) data->bank1_max_value[probe_order[i]] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) abituguru_bank1_max_value[res];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) data->bank1_address[res][data->bank1_sensors[res]] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) probe_order[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) data->bank1_sensors[res]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) /* Detect number of sensors and fill the sysfs attr for bank2 (fans) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) abituguru_detect_no_bank2_sensors(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) for (i = 0; i < data->bank2_sensors; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_fan_templ); j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) used = snprintf(sysfs_filename, sysfs_names_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) abituguru_sysfs_fan_templ[j].dev_attr.attr.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) i + 1) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) data->sysfs_attr[sysfs_attr_i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) abituguru_sysfs_fan_templ[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) sysfs_filename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) data->sysfs_attr[sysfs_attr_i].index = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) sysfs_filename += used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) sysfs_names_free -= used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) sysfs_attr_i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) /* Detect number of sensors and fill the sysfs attr for pwms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) abituguru_detect_no_pwms(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) for (i = 0; i < data->pwms; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_pwm_templ); j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) used = snprintf(sysfs_filename, sysfs_names_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) abituguru_sysfs_pwm_templ[j].dev_attr.attr.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) i + 1) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) data->sysfs_attr[sysfs_attr_i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) abituguru_sysfs_pwm_templ[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) sysfs_filename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) data->sysfs_attr[sysfs_attr_i].index = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) sysfs_filename += used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) sysfs_names_free -= used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) sysfs_attr_i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) /* Fail safe check, this should never happen! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) if (sysfs_names_free < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) pr_err("Fatal error ran out of space for sysfs attr names. %s %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) never_happen, report_this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) res = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) goto abituguru_probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) pr_info("found Abit uGuru\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) /* Register sysfs hooks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) for (i = 0; i < sysfs_attr_i; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) res = device_create_file(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) &data->sysfs_attr[i].dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) goto abituguru_probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) res = device_create_file(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) &abituguru_sysfs_attr[i].dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) goto abituguru_probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) data->hwmon_dev = hwmon_device_register(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) if (!IS_ERR(data->hwmon_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) return 0; /* success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) res = PTR_ERR(data->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) abituguru_probe_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) device_remove_file(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) &abituguru_sysfs_attr[i].dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) static int abituguru_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) struct abituguru_data *data = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) hwmon_device_unregister(data->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) device_remove_file(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) &abituguru_sysfs_attr[i].dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) static struct abituguru_data *abituguru_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) /* fake a complete successful read if no update necessary. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) char success = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) if (time_after(jiffies, data->last_updated + HZ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) success = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) data->alarms, 3, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) if (err != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) goto LEAVE_UPDATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) err = abituguru_read(data, ABIT_UGURU_SENSOR_BANK1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) i, &data->bank1_value[i], 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) if (err != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) goto LEAVE_UPDATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) err = abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) i, data->bank1_settings[i], 3, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) if (err != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) goto LEAVE_UPDATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) for (i = 0; i < data->bank2_sensors; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) err = abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) &data->bank2_value[i], 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) if (err != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) goto LEAVE_UPDATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) /* success! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) success = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) data->update_timeouts = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) LEAVE_UPDATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) /* handle timeout condition */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) if (!success && (err == -EBUSY || err >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) /* No overflow please */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) if (data->update_timeouts < 255u)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) data->update_timeouts++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) if (data->update_timeouts <= ABIT_UGURU_MAX_TIMEOUTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) ABIT_UGURU_DEBUG(3, "timeout exceeded, will "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) "try again next update\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) /* Just a timeout, fake a successful read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) success = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) ABIT_UGURU_DEBUG(1, "timeout exceeded %d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) "times waiting for more input state\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) (int)data->update_timeouts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) /* On success set last_updated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) if (success)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) if (success)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) static int abituguru_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) * make sure all communications with the uguru are done and no new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) * ones are started
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) static int abituguru_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) struct abituguru_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) /* See if the uGuru is still ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) if (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) data->uguru_ready = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) static SIMPLE_DEV_PM_OPS(abituguru_pm, abituguru_suspend, abituguru_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) #define ABIT_UGURU_PM (&abituguru_pm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) #define ABIT_UGURU_PM NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) #endif /* CONFIG_PM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) static struct platform_driver abituguru_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) .name = ABIT_UGURU_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) .pm = ABIT_UGURU_PM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) .probe = abituguru_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) .remove = abituguru_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) static int __init abituguru_detect(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) * See if there is an uguru there. After a reboot uGuru will hold 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) * at DATA and 0xAC, when this driver has already been loaded once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) * DATA will hold 0x08. For most uGuru's CMD will hold 0xAC in either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) * scenario but some will hold 0x00.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) * Some uGuru's initially hold 0x09 at DATA and will only hold 0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) * after reading CMD first, so CMD must be read first!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) u8 cmd_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_CMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) u8 data_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) if (((data_val == 0x00) || (data_val == 0x08)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) ((cmd_val == 0x00) || (cmd_val == 0xAC)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) return ABIT_UGURU_BASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) ABIT_UGURU_DEBUG(2, "no Abit uGuru found, data = 0x%02X, cmd = "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) "0x%02X\n", (unsigned int)data_val, (unsigned int)cmd_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) if (force) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) pr_info("Assuming Abit uGuru is present because of \"force\" parameter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) return ABIT_UGURU_BASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) /* No uGuru found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) static struct platform_device *abituguru_pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) static int __init abituguru_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) int address, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) struct resource res = { .flags = IORESOURCE_IO };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) const char *board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) /* safety check, refuse to load on non Abit motherboards */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) if (!force && (!board_vendor ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) strcmp(board_vendor, "http://www.abit.com.tw/")))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) address = abituguru_detect();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) if (address < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) return address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) err = platform_driver_register(&abituguru_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) abituguru_pdev = platform_device_alloc(ABIT_UGURU_NAME, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) if (!abituguru_pdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) pr_err("Device allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) goto exit_driver_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) res.start = address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) res.end = address + ABIT_UGURU_REGION_LENGTH - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) res.name = ABIT_UGURU_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) err = platform_device_add_resources(abituguru_pdev, &res, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) pr_err("Device resource addition failed (%d)\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) goto exit_device_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) err = platform_device_add(abituguru_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) pr_err("Device addition failed (%d)\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) goto exit_device_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) exit_device_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) platform_device_put(abituguru_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) exit_driver_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) platform_driver_unregister(&abituguru_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) static void __exit abituguru_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) platform_device_unregister(abituguru_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) platform_driver_unregister(&abituguru_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) MODULE_DESCRIPTION("Abit uGuru Sensor device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) module_init(abituguru_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) module_exit(abituguru_exit);