^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) * lm87.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2000 Frodo Looijaard <frodol@dds.nl>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Philip Edelbrock <phil@netroedge.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Stephen Rousset <stephen.rousset@rocketlogix.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Dan Eaton <dan.eaton@rocketlogix.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (C) 2004-2008 Jean Delvare <jdelvare@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Original port to Linux 2.6 by Jeff Oliver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * The LM87 is a sensor chip made by National Semiconductor. It monitors up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * to 8 voltages (including its own power source), up to three temperatures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * (its own plus up to two external ones) and up to two fans. The default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * configuration is 6 voltages, two temperatures and two fans (see below).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Voltages are scaled internally with ratios such that the nominal value of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * each voltage correspond to a register value of 192 (which means a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * resolution of about 0.5% of the nominal value). Temperature values are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * reported with a 1 deg resolution and a 3-4 deg accuracy. Complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * datasheet can be obtained from National's website at:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * http://www.national.com/pf/LM/LM87.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Some functions share pins, so not all functions are available at the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * time. Which are depends on the hardware setup. This driver normally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * assumes that firmware configured the chip correctly. Where this is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * the case, platform code must set the I2C client's platform_data to point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * to a u8 value to be written to the channel register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * For reference, here is the list of exclusive functions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * - in0+in5 (default) or temp3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * - fan1 (default) or in6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * - fan2 (default) or in7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * - VID lines (default) or IRQ lines (not handled by this driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * The LM87 additionally features an analog output, supposedly usable to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * control the speed of a fan. All new chips use pulse width modulation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * instead. The LM87 is the only hardware monitoring chipset I know of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * which uses amplitude modulation. Be careful when using this feature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * This driver also supports the ADM1024, a sensor chip made by Analog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * Devices. That chip is fully compatible with the LM87. Complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * datasheet can be obtained from Analog's website at:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * https://www.analog.com/en/prod/0,2877,ADM1024,00.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/hwmon-vid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * Addresses to scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * The LM87 registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* nr in 0..5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define LM87_REG_IN(nr) (0x20 + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* nr in 0..1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define LM87_REG_AIN(nr) (0x28 + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define LM87_REG_AIN_MIN(nr) (0x1A + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define LM87_REG_AIN_MAX(nr) (0x3B + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define LM87_REG_TEMP_HW_INT_LOCK 0x13
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define LM87_REG_TEMP_HW_EXT_LOCK 0x14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #define LM87_REG_TEMP_HW_INT 0x17
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #define LM87_REG_TEMP_HW_EXT 0x18
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /* nr in 0..1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) #define LM87_REG_FAN(nr) (0x28 + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #define LM87_REG_AOUT 0x19
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #define LM87_REG_CONFIG 0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #define LM87_REG_CHANNEL_MODE 0x16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #define LM87_REG_VID_FAN_DIV 0x47
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #define LM87_REG_VID4 0x49
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #define LM87_REG_ALARMS1 0x41
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #define LM87_REG_ALARMS2 0x42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #define LM87_REG_COMPANY_ID 0x3E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #define LM87_REG_REVISION 0x3F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * Conversions and various macros
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * The LM87 uses signed 8-bit values for temperatures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #define IN_FROM_REG(reg, scale) (((reg) * (scale) + 96) / 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #define IN_TO_REG(val, scale) ((val) <= 0 ? 0 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) (val) >= (scale) * 255 / 192 ? 255 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) ((val) * 192 + (scale) / 2) / (scale))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #define TEMP_FROM_REG(reg) ((reg) * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) (val) >= 126500 ? 127 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) (((val) < 0 ? (val) - 500 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) (val) + 500) / 1000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #define FAN_FROM_REG(reg, div) ((reg) == 255 || (reg) == 0 ? 0 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) (1350000 + (reg)*(div) / 2) / ((reg) * (div)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #define FAN_TO_REG(val, div) ((val) * (div) * 255 <= 1350000 ? 255 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) (1350000 + (val)*(div) / 2) / ((val) * (div)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #define FAN_DIV_FROM_REG(reg) (1 << (reg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* analog out is 9.80mV/LSB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) #define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) #define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) (val) >= 2500 ? 255 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) ((val) * 10 + 49) / 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* nr in 0..1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #define CHAN_NO_FAN(nr) (1 << (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #define CHAN_TEMP3 (1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #define CHAN_VCC_5V (1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) #define CHAN_NO_VID (1 << 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * Client data (each client gets its own)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct lm87_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) char valid; /* zero until following fields are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) unsigned long last_updated; /* In jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) u8 channel; /* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) u8 config; /* original register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) u8 in[8]; /* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) u8 in_max[8]; /* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) u8 in_min[8]; /* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) u16 in_scale[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) s8 temp[3]; /* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) s8 temp_high[3]; /* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) s8 temp_low[3]; /* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) s8 temp_crit_int; /* min of two register values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) s8 temp_crit_ext; /* min of two register values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) u8 fan[2]; /* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) u8 fan_min[2]; /* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) u8 fan_div[2]; /* register value, shifted right */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) u8 aout; /* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) u16 alarms; /* register values, combined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) u8 vid; /* register values, combined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) u8 vrm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) const struct attribute_group *attr_groups[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static inline int lm87_read_value(struct i2c_client *client, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return i2c_smbus_read_byte_data(client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return i2c_smbus_write_byte_data(client, reg, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static struct lm87_data *lm87_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct i2c_client *client = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct lm87_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) dev_dbg(&client->dev, "Updating data.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) i = (data->channel & CHAN_TEMP3) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) j = (data->channel & CHAN_TEMP3) ? 5 : 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) for (; i < j; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) data->in[i] = lm87_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) LM87_REG_IN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) data->in_min[i] = lm87_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) LM87_REG_IN_MIN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) data->in_max[i] = lm87_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) LM87_REG_IN_MAX(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (data->channel & CHAN_NO_FAN(i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) data->in[6+i] = lm87_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) LM87_REG_AIN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) data->in_max[6+i] = lm87_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) LM87_REG_AIN_MAX(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) data->in_min[6+i] = lm87_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) LM87_REG_AIN_MIN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) data->fan[i] = lm87_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) LM87_REG_FAN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) data->fan_min[i] = lm87_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) LM87_REG_FAN_MIN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) j = (data->channel & CHAN_TEMP3) ? 3 : 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) for (i = 0 ; i < j; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) data->temp[i] = lm87_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) LM87_REG_TEMP[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) data->temp_high[i] = lm87_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) LM87_REG_TEMP_HIGH[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) data->temp_low[i] = lm87_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) LM87_REG_TEMP_LOW[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) data->temp_crit_int = min(i, j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) data->temp_crit_ext = min(i, j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) data->fan_div[0] = (i >> 4) & 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) data->fan_div[1] = (i >> 6) & 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) data->vid = (i & 0x0F)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) << 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) | (lm87_read_value(client, LM87_REG_ALARMS2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) data->aout = lm87_read_value(client, LM87_REG_AOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) data->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * Sysfs stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static ssize_t in_input_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) struct lm87_data *data = lm87_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return sprintf(buf, "%u\n", IN_FROM_REG(data->in[nr],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) data->in_scale[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct lm87_data *data = lm87_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[nr],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) data->in_scale[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct lm87_data *data = lm87_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[nr],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) data->in_scale[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct i2c_client *client = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct lm87_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) lm87_write_value(client, nr < 6 ? LM87_REG_IN_MIN(nr) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) LM87_REG_AIN_MIN(nr - 6), data->in_min[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return count;
^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) static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct i2c_client *client = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) struct lm87_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) lm87_write_value(client, nr < 6 ? LM87_REG_IN_MAX(nr) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) LM87_REG_AIN_MAX(nr - 6), data->in_max[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static SENSOR_DEVICE_ATTR_RO(in0_input, in_input, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static SENSOR_DEVICE_ATTR_RO(in1_input, in_input, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static SENSOR_DEVICE_ATTR_RO(in2_input, in_input, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static SENSOR_DEVICE_ATTR_RO(in3_input, in_input, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static SENSOR_DEVICE_ATTR_RO(in4_input, in_input, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static SENSOR_DEVICE_ATTR_RO(in5_input, in_input, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static SENSOR_DEVICE_ATTR_RO(in6_input, in_input, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) static SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static SENSOR_DEVICE_ATTR_RO(in7_input, in_input, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static SENSOR_DEVICE_ATTR_RW(in7_min, in_min, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static SENSOR_DEVICE_ATTR_RW(in7_max, in_max, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static ssize_t temp_input_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) struct lm87_data *data = lm87_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
^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) static ssize_t temp_low_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) struct lm87_data *data = lm87_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) return sprintf(buf, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) TEMP_FROM_REG(data->temp_low[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static ssize_t temp_high_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) struct lm87_data *data = lm87_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return sprintf(buf, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) TEMP_FROM_REG(data->temp_high[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) static ssize_t temp_low_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct device_attribute *attr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) struct i2c_client *client = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) struct lm87_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) data->temp_low[nr] = TEMP_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) static ssize_t temp_high_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) struct device_attribute *attr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) struct i2c_client *client = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) struct lm87_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) data->temp_high[nr] = TEMP_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) return count;
^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) static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_low, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_high, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_low, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_high, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static SENSOR_DEVICE_ATTR_RO(temp3_input, temp_input, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_low, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_high, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) static ssize_t temp1_crit_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct lm87_data *data = lm87_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) static ssize_t temp2_crit_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) struct lm87_data *data = lm87_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) static DEVICE_ATTR_RO(temp1_crit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) static DEVICE_ATTR_RO(temp2_crit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) static DEVICE_ATTR(temp3_crit, 0444, temp2_crit_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) static ssize_t fan_input_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) struct lm87_data *data = lm87_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) FAN_DIV_FROM_REG(data->fan_div[nr])));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) struct lm87_data *data = lm87_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) FAN_DIV_FROM_REG(data->fan_div[nr])));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct lm87_data *data = lm87_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) return sprintf(buf, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) FAN_DIV_FROM_REG(data->fan_div[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static ssize_t fan_min_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct device_attribute *attr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) struct i2c_client *client = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct lm87_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) data->fan_min[nr] = FAN_TO_REG(val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) FAN_DIV_FROM_REG(data->fan_div[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * Note: we save and restore the fan minimum here, because its value is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) * determined in part by the fan clock divider. This follows the principle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) * of least surprise; the user doesn't expect the fan minimum to change just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) * because the divider changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static ssize_t fan_div_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) struct device_attribute *attr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) struct i2c_client *client = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) struct lm87_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) unsigned long min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) min = FAN_FROM_REG(data->fan_min[nr],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) FAN_DIV_FROM_REG(data->fan_div[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) switch (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) data->fan_div[nr] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) data->fan_div[nr] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) data->fan_div[nr] = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) data->fan_div[nr] = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) switch (nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) reg = (reg & 0xCF) | (data->fan_div[0] << 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) reg = (reg & 0x3F) | (data->fan_div[1] << 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) data->fan_min[nr] = FAN_TO_REG(min, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) lm87_write_value(client, LM87_REG_FAN_MIN(nr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) data->fan_min[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) struct lm87_data *data = lm87_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) return sprintf(buf, "%d\n", data->alarms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) static DEVICE_ATTR_RO(alarms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) static ssize_t cpu0_vid_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) struct lm87_data *data = lm87_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) static DEVICE_ATTR_RO(cpu0_vid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) struct lm87_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) return sprintf(buf, "%d\n", data->vrm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) struct lm87_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (val > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) data->vrm = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) static DEVICE_ATTR_RW(vrm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) static ssize_t aout_output_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) struct lm87_data *data = lm87_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) static ssize_t aout_output_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) struct i2c_client *client = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) struct lm87_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) data->aout = AOUT_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) lm87_write_value(client, LM87_REG_AOUT, data->aout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) static DEVICE_ATTR_RW(aout_output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) struct lm87_data *data = lm87_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) int bitnr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) static SENSOR_DEVICE_ATTR_RO(in7_alarm, alarm, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) * Real code
^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) static struct attribute *lm87_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) &sensor_dev_attr_in1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) &sensor_dev_attr_in1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) &sensor_dev_attr_in1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) &sensor_dev_attr_in1_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) &sensor_dev_attr_in2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) &sensor_dev_attr_in2_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) &sensor_dev_attr_in2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) &sensor_dev_attr_in2_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) &sensor_dev_attr_in3_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) &sensor_dev_attr_in3_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) &sensor_dev_attr_in3_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) &sensor_dev_attr_in3_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) &sensor_dev_attr_in4_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) &sensor_dev_attr_in4_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) &sensor_dev_attr_in4_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) &sensor_dev_attr_in4_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) &sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) &sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) &sensor_dev_attr_temp1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) &dev_attr_temp1_crit.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) &sensor_dev_attr_temp1_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) &sensor_dev_attr_temp2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) &sensor_dev_attr_temp2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) &sensor_dev_attr_temp2_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) &dev_attr_temp2_crit.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) &sensor_dev_attr_temp2_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) &sensor_dev_attr_temp2_fault.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) &dev_attr_alarms.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) &dev_attr_aout_output.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) static const struct attribute_group lm87_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) .attrs = lm87_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) static struct attribute *lm87_attributes_in6[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) &sensor_dev_attr_in6_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) &sensor_dev_attr_in6_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) &sensor_dev_attr_in6_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) &sensor_dev_attr_in6_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) static const struct attribute_group lm87_group_in6 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) .attrs = lm87_attributes_in6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) static struct attribute *lm87_attributes_fan1[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) &sensor_dev_attr_fan1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) &sensor_dev_attr_fan1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) &sensor_dev_attr_fan1_div.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) &sensor_dev_attr_fan1_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) static const struct attribute_group lm87_group_fan1 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) .attrs = lm87_attributes_fan1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) static struct attribute *lm87_attributes_in7[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) &sensor_dev_attr_in7_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) &sensor_dev_attr_in7_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) &sensor_dev_attr_in7_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) &sensor_dev_attr_in7_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) static const struct attribute_group lm87_group_in7 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) .attrs = lm87_attributes_in7,
^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) static struct attribute *lm87_attributes_fan2[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) &sensor_dev_attr_fan2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) &sensor_dev_attr_fan2_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) &sensor_dev_attr_fan2_div.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) &sensor_dev_attr_fan2_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) static const struct attribute_group lm87_group_fan2 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) .attrs = lm87_attributes_fan2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) static struct attribute *lm87_attributes_temp3[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) &sensor_dev_attr_temp3_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) &sensor_dev_attr_temp3_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) &sensor_dev_attr_temp3_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) &dev_attr_temp3_crit.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) &sensor_dev_attr_temp3_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) &sensor_dev_attr_temp3_fault.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) static const struct attribute_group lm87_group_temp3 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) .attrs = lm87_attributes_temp3,
^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) static struct attribute *lm87_attributes_in0_5[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) &sensor_dev_attr_in0_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) &sensor_dev_attr_in0_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) &sensor_dev_attr_in0_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) &sensor_dev_attr_in0_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) &sensor_dev_attr_in5_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) &sensor_dev_attr_in5_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) &sensor_dev_attr_in5_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) &sensor_dev_attr_in5_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) static const struct attribute_group lm87_group_in0_5 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) .attrs = lm87_attributes_in0_5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) static struct attribute *lm87_attributes_vid[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) &dev_attr_cpu0_vid.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) &dev_attr_vrm.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) static const struct attribute_group lm87_group_vid = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) .attrs = lm87_attributes_vid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) /* Return 0 if detection is successful, -ENODEV otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) static int lm87_detect(struct i2c_client *client, struct i2c_board_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) u8 cid, rev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) if (lm87_read_value(client, LM87_REG_CONFIG) & 0x80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) /* Now, we do the remaining detection. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) cid = lm87_read_value(client, LM87_REG_COMPANY_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) rev = lm87_read_value(client, LM87_REG_REVISION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) if (cid == 0x02 /* National Semiconductor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) && (rev >= 0x01 && rev <= 0x08))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) name = "lm87";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) else if (cid == 0x41 /* Analog Devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) && (rev & 0xf0) == 0x10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) name = "adm1024";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) dev_dbg(&adapter->dev, "LM87 detection failed at 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) client->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) strlcpy(info->type, name, I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) static void lm87_restore_config(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) struct i2c_client *client = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) struct lm87_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) lm87_write_value(client, LM87_REG_CONFIG, data->config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) static int lm87_init_client(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) struct lm87_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) struct device_node *of_node = client->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) u8 val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) struct regulator *vcc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) if (of_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) if (of_property_read_bool(of_node, "has-temp3"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) val |= CHAN_TEMP3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) if (of_property_read_bool(of_node, "has-in6"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) val |= CHAN_NO_FAN(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) if (of_property_read_bool(of_node, "has-in7"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) val |= CHAN_NO_FAN(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) vcc = devm_regulator_get_optional(&client->dev, "vcc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) if (!IS_ERR(vcc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) if (regulator_get_voltage(vcc) == 5000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) val |= CHAN_VCC_5V;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) data->channel = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) lm87_write_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) LM87_REG_CHANNEL_MODE, data->channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) } else if (dev_get_platdata(&client->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) data->channel = *(u8 *)dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) lm87_write_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) LM87_REG_CHANNEL_MODE, data->channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) data->config = lm87_read_value(client, LM87_REG_CONFIG) & 0x6F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) rc = devm_add_action(&client->dev, lm87_restore_config, client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) if (!(data->config & 0x01)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) /* Limits are left uninitialized after power-up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) for (i = 1; i < 6; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) if (data->channel & CHAN_TEMP3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) /* Make sure Start is set and INT#_Clear is clear */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) if ((data->config & 0x09) != 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) lm87_write_value(client, LM87_REG_CONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) (data->config & 0x77) | 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) static int lm87_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) struct lm87_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) unsigned int group_tail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) data = devm_kzalloc(&client->dev, sizeof(struct lm87_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) i2c_set_clientdata(client, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) /* Initialize the LM87 chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) err = lm87_init_client(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) data->in_scale[0] = 2500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) data->in_scale[1] = 2700;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) data->in_scale[3] = 5000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) data->in_scale[4] = 12000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) data->in_scale[5] = 2700;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) data->in_scale[6] = 1875;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) data->in_scale[7] = 1875;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) * Construct the list of attributes, the list depends on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) * configuration of the chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) data->attr_groups[group_tail++] = &lm87_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) if (data->channel & CHAN_NO_FAN(0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) data->attr_groups[group_tail++] = &lm87_group_in6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) data->attr_groups[group_tail++] = &lm87_group_fan1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) if (data->channel & CHAN_NO_FAN(1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) data->attr_groups[group_tail++] = &lm87_group_in7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) data->attr_groups[group_tail++] = &lm87_group_fan2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) if (data->channel & CHAN_TEMP3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) data->attr_groups[group_tail++] = &lm87_group_temp3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) data->attr_groups[group_tail++] = &lm87_group_in0_5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) if (!(data->channel & CHAN_NO_VID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) data->vrm = vid_which_vrm();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) data->attr_groups[group_tail++] = &lm87_group_vid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) hwmon_dev = devm_hwmon_device_register_with_groups(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) &client->dev, client->name, client, data->attr_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) * Driver data (common to all clients)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) static const struct i2c_device_id lm87_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) { "lm87", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) { "adm1024", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) MODULE_DEVICE_TABLE(i2c, lm87_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) static const struct of_device_id lm87_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) { .compatible = "ti,lm87" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) { .compatible = "adi,adm1024" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) MODULE_DEVICE_TABLE(of, lm87_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) static struct i2c_driver lm87_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) .class = I2C_CLASS_HWMON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) .name = "lm87",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) .of_match_table = lm87_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) .probe_new = lm87_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) .id_table = lm87_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) .detect = lm87_detect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) .address_list = normal_i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) module_i2c_driver(lm87_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de> and others");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) MODULE_DESCRIPTION("LM87 driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) MODULE_LICENSE("GPL");