Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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)  * lm63.c - driver for the National Semiconductor LM63 temperature sensor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *          with integrated fan control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2004-2008  Jean Delvare <jdelvare@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Based on the lm90 driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * The LM63 is a sensor chip made by National Semiconductor. It measures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * two temperatures (its own and one external one) and the speed of one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * fan, those speed it can additionally control. Complete datasheet can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * obtained from National's website at:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *   http://www.national.com/pf/LM/LM63.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  * The LM63 is basically an LM86 with fan speed monitoring and control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * capabilities added. It misses some of the LM86 features though:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *  - No low limit for local temperature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  *  - No critical limit for local temperature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *  - Critical limit for remote temperature can be changed only once. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *    will consider that the critical limit is read-only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  * The datasheet isn't very clear about what the tachometer reading is.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  * I had a explanation from National Semiconductor though. The two lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  * bits of the read value have to be masked out. The value is still 16 bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  * in width.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * Addresses to scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * Address is fully defined internally and cannot be changed except for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * LM64 which has one pin dedicated to address selection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  * LM63 and LM96163 have address 0x4c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * LM64 can have address 0x18 or 0x4e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  * The LM63 registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #define LM63_REG_CONFIG1		0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #define LM63_REG_CONVRATE		0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #define LM63_REG_CONFIG2		0xBF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #define LM63_REG_CONFIG_FAN		0x4A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #define LM63_REG_TACH_COUNT_MSB		0x47
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) #define LM63_REG_TACH_COUNT_LSB		0x46
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #define LM63_REG_TACH_LIMIT_MSB		0x49
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #define LM63_REG_TACH_LIMIT_LSB		0x48
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #define LM63_REG_PWM_VALUE		0x4C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define LM63_REG_PWM_FREQ		0x4D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #define LM63_REG_LUT_TEMP_HYST		0x4F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) #define LM63_REG_LUT_TEMP(nr)		(0x50 + 2 * (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) #define LM63_REG_LUT_PWM(nr)		(0x51 + 2 * (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) #define LM63_REG_LOCAL_TEMP		0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) #define LM63_REG_LOCAL_HIGH		0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) #define LM63_REG_REMOTE_TEMP_MSB	0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #define LM63_REG_REMOTE_TEMP_LSB	0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #define LM63_REG_REMOTE_OFFSET_MSB	0x11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) #define LM63_REG_REMOTE_OFFSET_LSB	0x12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) #define LM63_REG_REMOTE_HIGH_MSB	0x07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #define LM63_REG_REMOTE_HIGH_LSB	0x13
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) #define LM63_REG_REMOTE_LOW_MSB		0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) #define LM63_REG_REMOTE_LOW_LSB		0x14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) #define LM63_REG_REMOTE_TCRIT		0x19
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) #define LM63_REG_REMOTE_TCRIT_HYST	0x21
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) #define LM63_REG_ALERT_STATUS		0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) #define LM63_REG_ALERT_MASK		0x16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) #define LM63_REG_MAN_ID			0xFE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #define LM63_REG_CHIP_ID		0xFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) #define LM96163_REG_TRUTHERM		0x30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) #define LM96163_REG_REMOTE_TEMP_U_MSB	0x31
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) #define LM96163_REG_REMOTE_TEMP_U_LSB	0x32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) #define LM96163_REG_CONFIG_ENHANCED	0x45
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) #define LM63_MAX_CONVRATE		9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) #define LM63_MAX_CONVRATE_HZ		32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) #define LM96163_MAX_CONVRATE_HZ		26
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  * Conversions and various macros
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  * For tachometer counts, the LM63 uses 16-bit values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  * For local temperature and high limit, remote critical limit and hysteresis
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104)  * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105)  * For remote temperature, low and high limits, it uses signed 11-bit values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107)  * For LM64 the actual remote diode temperature is 16 degree Celsius higher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108)  * than the register reading. Remote temperature setpoints have to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109)  * adapted accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) #define FAN_FROM_REG(reg)	((reg) == 0xFFFC || (reg) == 0 ? 0 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 				 5400000 / (reg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) #define FAN_TO_REG(val)		((val) <= 82 ? 0xFFFC : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 				 (5400000 / (val)) & 0xFFFC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) #define TEMP8_FROM_REG(reg)	((reg) * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) #define TEMP8_TO_REG(val)	DIV_ROUND_CLOSEST(clamp_val((val), -128000, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 							    127000), 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) #define TEMP8U_TO_REG(val)	DIV_ROUND_CLOSEST(clamp_val((val), 0, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 							    255000), 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) #define TEMP11_FROM_REG(reg)	((reg) / 32 * 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) #define TEMP11_TO_REG(val)	(DIV_ROUND_CLOSEST(clamp_val((val), -128000, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 							     127875), 125) * 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) #define TEMP11U_TO_REG(val)	(DIV_ROUND_CLOSEST(clamp_val((val), 0, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 							     255875), 125) * 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) #define HYST_TO_REG(val)	DIV_ROUND_CLOSEST(clamp_val((val), 0, 127000), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 						  1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) #define UPDATE_INTERVAL(max, rate) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 			((1000 << (LM63_MAX_CONVRATE - (rate))) / (max))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) enum chips { lm63, lm64, lm96163 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135)  * Client data (each client gets its own)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) struct lm63_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	const struct attribute_group *groups[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	char valid; /* zero until following fields are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	char lut_valid; /* zero until lut fields are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	unsigned long last_updated; /* in jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	unsigned long lut_last_updated; /* in jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	enum chips kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	int temp2_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	int update_interval;	/* in milliseconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	int max_convrate_hz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	int lut_size;		/* 8 or 12 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	/* registers values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	u8 config, config_fan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	u16 fan[2];	/* 0: input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 			   1: low limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	u8 pwm1_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	u8 pwm1[13];	/* 0: current output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 			   1-12: lookup table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	s8 temp8[15];	/* 0: local input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 			   1: local high limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 			   2: remote critical limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 			   3-14: lookup table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	s16 temp11[4];	/* 0: remote input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 			   1: remote low limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 			   2: remote high limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 			   3: remote offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	u16 temp11u;	/* remote input (unsigned) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	u8 temp2_crit_hyst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	u8 lut_temp_hyst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	u8 alarms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	bool pwm_highres;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	bool lut_temp_highres;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	bool remote_unsigned; /* true if unsigned remote upper limits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	bool trutherm;
^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 temp8_from_reg(struct lm63_data *data, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	if (data->remote_unsigned)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		return TEMP8_FROM_REG((u8)data->temp8[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	return TEMP8_FROM_REG(data->temp8[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) static inline int lut_temp_from_reg(struct lm63_data *data, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	return data->temp8[nr] * (data->lut_temp_highres ? 500 : 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) static inline int lut_temp_to_reg(struct lm63_data *data, long val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	val -= data->temp2_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	if (data->lut_temp_highres)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		return DIV_ROUND_CLOSEST(clamp_val(val, 0, 127500), 500);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		return DIV_ROUND_CLOSEST(clamp_val(val, 0, 127000), 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200)  * Update the lookup table register cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201)  * client->update_lock must be held when calling this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) static void lm63_update_lut(struct lm63_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	if (time_after(jiffies, data->lut_last_updated + 5 * HZ) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	    !data->lut_valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		for (i = 0; i < data->lut_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 			data->pwm1[1 + i] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 					    LM63_REG_LUT_PWM(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 			data->temp8[3 + i] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 					     LM63_REG_LUT_TEMP(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		data->lut_temp_hyst = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 				      LM63_REG_LUT_TEMP_HYST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 		data->lut_last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		data->lut_valid = 1;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) static struct lm63_data *lm63_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	struct lm63_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	unsigned long next_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	next_update = data->last_updated +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		      msecs_to_jiffies(data->update_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	if (time_after(jiffies, next_update) || !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		if (data->config & 0x04) { /* tachometer enabled  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 			/* order matters for fan1_input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 			data->fan[0] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 				       LM63_REG_TACH_COUNT_LSB) & 0xFC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 			data->fan[0] |= i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 					LM63_REG_TACH_COUNT_MSB) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 			data->fan[1] = (i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 					LM63_REG_TACH_LIMIT_LSB) & 0xFC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 				     | (i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 					LM63_REG_TACH_LIMIT_MSB) << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		data->pwm1_freq = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 				  LM63_REG_PWM_FREQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		if (data->pwm1_freq == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 			data->pwm1_freq = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		data->pwm1[0] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 				LM63_REG_PWM_VALUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		data->temp8[0] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 				 LM63_REG_LOCAL_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		data->temp8[1] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 				 LM63_REG_LOCAL_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		/* order matters for temp2_input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 		data->temp11[0] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 				  LM63_REG_REMOTE_TEMP_MSB) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 		data->temp11[0] |= i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 				   LM63_REG_REMOTE_TEMP_LSB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		data->temp11[1] = (i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 				  LM63_REG_REMOTE_LOW_MSB) << 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 				| i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 				  LM63_REG_REMOTE_LOW_LSB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		data->temp11[2] = (i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 				  LM63_REG_REMOTE_HIGH_MSB) << 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 				| i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 				  LM63_REG_REMOTE_HIGH_LSB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		data->temp11[3] = (i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 				  LM63_REG_REMOTE_OFFSET_MSB) << 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 				| i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 				  LM63_REG_REMOTE_OFFSET_LSB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		if (data->kind == lm96163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 			data->temp11u = (i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 					LM96163_REG_REMOTE_TEMP_U_MSB) << 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 				      | i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 					LM96163_REG_REMOTE_TEMP_U_LSB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		data->temp8[2] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 				 LM63_REG_REMOTE_TCRIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 					LM63_REG_REMOTE_TCRIT_HYST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		data->alarms = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 			       LM63_REG_ALERT_STATUS) & 0x7F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		data->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	lm63_update_lut(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	return data;
^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)  * Trip points in the lookup table should be in ascending order for both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304)  * temperatures and PWM output values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) static int lm63_lut_looks_bad(struct device *dev, struct lm63_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	lm63_update_lut(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	for (i = 1; i < data->lut_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		if (data->pwm1[1 + i - 1] > data->pwm1[1 + i]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		 || data->temp8[3 + i - 1] > data->temp8[3 + i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 			dev_warn(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 				 "Lookup table doesn't look sane (check entries %d and %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 				 i, i + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 			break;
^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) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	return i == data->lut_size ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328)  * Sysfs callback functions and files
^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) static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 			char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	struct lm63_data *data = lm63_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 		       const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	struct lm63_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	data->fan[1] = FAN_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 				  data->fan[1] & 0xFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 				  data->fan[1] >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) static ssize_t show_pwm1(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 			 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	struct lm63_data *data = lm63_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	int nr = attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	int pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	if (data->pwm_highres)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		pwm = data->pwm1[nr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		pwm = data->pwm1[nr] >= 2 * data->pwm1_freq ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		       255 : (data->pwm1[nr] * 255 + data->pwm1_freq) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		       (2 * data->pwm1_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	return sprintf(buf, "%d\n", pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) static ssize_t set_pwm1(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 			const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	struct lm63_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	int nr = attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	if (!(data->config_fan & 0x20)) /* register is read-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	reg = nr ? LM63_REG_LUT_PWM(nr - 1) : LM63_REG_PWM_VALUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	val = clamp_val(val, 0, 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	data->pwm1[nr] = data->pwm_highres ? val :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 			(val * data->pwm1_freq * 2 + 127) / 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	i2c_smbus_write_byte_data(client, reg, data->pwm1[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) static ssize_t pwm1_enable_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 				struct device_attribute *dummy, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	struct lm63_data *data = lm63_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) static ssize_t pwm1_enable_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 				 struct device_attribute *dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 				 const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	struct lm63_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	if (val < 1 || val > 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	 * Only let the user switch to automatic mode if the lookup table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	 * looks sane.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	if (val == 2 && lm63_lut_looks_bad(dev, data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	data->config_fan = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 						    LM63_REG_CONFIG_FAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	if (val == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		data->config_fan |= 0x20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		data->config_fan &= ~0x20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	i2c_smbus_write_byte_data(client, LM63_REG_CONFIG_FAN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 				  data->config_fan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) }
^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)  * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452)  * For remote sensor registers temp2_offset has to be considered,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453)  * for local sensor it must not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454)  * So we need separate 8bit accessors for local and remote sensor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) static ssize_t show_local_temp8(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 				struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 				char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	struct lm63_data *data = lm63_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) static ssize_t show_remote_temp8(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 				 struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 				 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	struct lm63_data *data = lm63_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		       + data->temp2_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) static ssize_t show_lut_temp(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 			      struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 			      char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	struct lm63_data *data = lm63_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		       + data->temp2_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 			 const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	struct lm63_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	int nr = attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	int temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	switch (nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		reg = LM63_REG_REMOTE_TCRIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		if (data->remote_unsigned)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 			temp = TEMP8U_TO_REG(val - data->temp2_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 			temp = TEMP8_TO_REG(val - data->temp2_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		reg = LM63_REG_LOCAL_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		temp = TEMP8_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	default:	/* lookup table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		reg = LM63_REG_LUT_TEMP(nr - 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		temp = lut_temp_to_reg(data, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	data->temp8[nr] = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	i2c_smbus_write_byte_data(client, reg, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 			   char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	struct lm63_data *data = lm63_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	int nr = attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	int temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	if (!nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		 * Use unsigned temperature unless its value is zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		 * If it is zero, use signed temperature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		if (data->temp11u)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 			temp = TEMP11_FROM_REG(data->temp11u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 			temp = TEMP11_FROM_REG(data->temp11[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		if (data->remote_unsigned && nr == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 			temp = TEMP11_FROM_REG((u16)data->temp11[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 			temp = TEMP11_FROM_REG(data->temp11[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	return sprintf(buf, "%d\n", temp + data->temp2_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 			  const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	static const u8 reg[6] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		LM63_REG_REMOTE_LOW_MSB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		LM63_REG_REMOTE_LOW_LSB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		LM63_REG_REMOTE_HIGH_MSB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		LM63_REG_REMOTE_HIGH_LSB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		LM63_REG_REMOTE_OFFSET_MSB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		LM63_REG_REMOTE_OFFSET_LSB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	struct lm63_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	int nr = attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	if (data->remote_unsigned && nr == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 				  data->temp11[nr] >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 				  data->temp11[nr] & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588)  * Hysteresis register holds a relative value, while we want to present
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589)  * an absolute to user-space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) static ssize_t temp2_crit_hyst_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 				    struct device_attribute *dummy, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	struct lm63_data *data = lm63_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	return sprintf(buf, "%d\n", temp8_from_reg(data, 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		       + data->temp2_offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		       - TEMP8_FROM_REG(data->temp2_crit_hyst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) static ssize_t show_lut_temp_hyst(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 				  struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	struct lm63_data *data = lm63_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		       + data->temp2_offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		       - TEMP8_FROM_REG(data->lut_temp_hyst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612)  * And now the other way around, user-space provides an absolute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613)  * hysteresis value and we have to store a relative one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) static ssize_t temp2_crit_hyst_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 				     struct device_attribute *dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 				     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	struct lm63_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	long hyst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	hyst = temp8_from_reg(data, 2) + data->temp2_offset - val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 				  HYST_TO_REG(hyst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638)  * Set conversion rate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639)  * client->update_lock must be held when calling this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) static void lm63_set_convrate(struct lm63_data *data, unsigned int interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	unsigned int update_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	/* Shift calculations to avoid rounding errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	interval <<= 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	/* find the nearest update rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	  / data->max_convrate_hz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		if (interval >= update_interval * 3 / 4)
^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) 	i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) static ssize_t update_interval_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 				    struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	struct lm63_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	return sprintf(buf, "%u\n", data->update_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) static ssize_t update_interval_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 				     struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 				     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	struct lm63_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	lm63_set_convrate(data, clamp_val(val, 0, 100000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) static ssize_t temp2_type_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 			       struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	struct lm63_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	return sprintf(buf, data->trutherm ? "1\n" : "2\n");
^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) static ssize_t temp2_type_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 				struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 				const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	struct lm63_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	ret = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	if (val != 1 && val != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	data->trutherm = val == 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	reg = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM) & ~0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	i2c_smbus_write_byte_data(client, LM96163_REG_TRUTHERM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 				  reg | (data->trutherm ? 0x02 : 0x00));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	data->valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) static ssize_t alarms_show(struct device *dev, struct device_attribute *dummy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 			   char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	struct lm63_data *data = lm63_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	return sprintf(buf, "%u\n", data->alarms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 			  char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	struct lm63_data *data = lm63_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	int bitnr = attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	set_fan, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) static DEVICE_ATTR_RW(pwm1_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	show_pwm1, set_pwm1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	show_lut_temp, set_temp8, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp_hyst, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	show_lut_temp_hyst, NULL, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	show_pwm1, set_pwm1, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	show_lut_temp, set_temp8, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp_hyst, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	show_lut_temp_hyst, NULL, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	show_pwm1, set_pwm1, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	show_lut_temp, set_temp8, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp_hyst, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	show_lut_temp_hyst, NULL, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) static SENSOR_DEVICE_ATTR(pwm1_auto_point4_pwm, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	show_pwm1, set_pwm1, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	show_lut_temp, set_temp8, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp_hyst, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	show_lut_temp_hyst, NULL, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) static SENSOR_DEVICE_ATTR(pwm1_auto_point5_pwm, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	show_pwm1, set_pwm1, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	show_lut_temp, set_temp8, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp_hyst, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	show_lut_temp_hyst, NULL, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) static SENSOR_DEVICE_ATTR(pwm1_auto_point6_pwm, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	show_pwm1, set_pwm1, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	show_lut_temp, set_temp8, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp_hyst, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	show_lut_temp_hyst, NULL, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) static SENSOR_DEVICE_ATTR(pwm1_auto_point7_pwm, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	show_pwm1, set_pwm1, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	show_lut_temp, set_temp8, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp_hyst, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	show_lut_temp_hyst, NULL, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) static SENSOR_DEVICE_ATTR(pwm1_auto_point8_pwm, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	show_pwm1, set_pwm1, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	show_lut_temp, set_temp8, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp_hyst, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	show_lut_temp_hyst, NULL, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) static SENSOR_DEVICE_ATTR(pwm1_auto_point9_pwm, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	show_pwm1, set_pwm1, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	show_lut_temp, set_temp8, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp_hyst, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	show_lut_temp_hyst, NULL, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) static SENSOR_DEVICE_ATTR(pwm1_auto_point10_pwm, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	show_pwm1, set_pwm1, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	show_lut_temp, set_temp8, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp_hyst, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	show_lut_temp_hyst, NULL, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) static SENSOR_DEVICE_ATTR(pwm1_auto_point11_pwm, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	show_pwm1, set_pwm1, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	show_lut_temp, set_temp8, 13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp_hyst, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	show_lut_temp_hyst, NULL, 13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) static SENSOR_DEVICE_ATTR(pwm1_auto_point12_pwm, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	show_pwm1, set_pwm1, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	show_lut_temp, set_temp8, 14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp_hyst, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	show_lut_temp_hyst, NULL, 14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	set_temp8, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	set_temp11, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	set_temp11, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	set_temp11, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	set_temp8, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) static DEVICE_ATTR_RW(temp2_crit_hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) static DEVICE_ATTR_RW(temp2_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) /* Individual alarm files */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) /* Raw alarm file for compatibility */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) static DEVICE_ATTR_RO(alarms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) static DEVICE_ATTR_RW(update_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) static struct attribute *lm63_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	&sensor_dev_attr_pwm1.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	&dev_attr_pwm1_enable.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	&sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	&sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	&sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	&sensor_dev_attr_pwm1_auto_point2_temp_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	&sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	&sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	&sensor_dev_attr_pwm1_auto_point3_temp_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	&sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	&sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	&sensor_dev_attr_pwm1_auto_point4_temp_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	&sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	&sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	&sensor_dev_attr_pwm1_auto_point5_temp_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	&sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	&sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	&sensor_dev_attr_pwm1_auto_point6_temp_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	&sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	&sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	&sensor_dev_attr_pwm1_auto_point7_temp_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	&sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	&sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	&sensor_dev_attr_pwm1_auto_point8_temp_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	&sensor_dev_attr_temp2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	&sensor_dev_attr_temp2_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	&sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	&sensor_dev_attr_temp2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	&sensor_dev_attr_temp2_offset.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	&dev_attr_temp2_crit_hyst.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	&dev_attr_alarms.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	&dev_attr_update_interval.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) static struct attribute *lm63_attributes_temp2_type[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	&dev_attr_temp2_type.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) static const struct attribute_group lm63_group_temp2_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	.attrs = lm63_attributes_temp2_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) static struct attribute *lm63_attributes_extra_lut[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	&sensor_dev_attr_pwm1_auto_point9_pwm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	&sensor_dev_attr_pwm1_auto_point9_temp.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	&sensor_dev_attr_pwm1_auto_point9_temp_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	&sensor_dev_attr_pwm1_auto_point10_pwm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	&sensor_dev_attr_pwm1_auto_point10_temp.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	&sensor_dev_attr_pwm1_auto_point10_temp_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	&sensor_dev_attr_pwm1_auto_point11_pwm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	&sensor_dev_attr_pwm1_auto_point11_temp.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	&sensor_dev_attr_pwm1_auto_point11_temp_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	&sensor_dev_attr_pwm1_auto_point12_pwm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	&sensor_dev_attr_pwm1_auto_point12_temp.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	&sensor_dev_attr_pwm1_auto_point12_temp_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) static const struct attribute_group lm63_group_extra_lut = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	.attrs = lm63_attributes_extra_lut,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925)  * On LM63, temp2_crit can be set only once, which should be job
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926)  * of the bootloader.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927)  * On LM64, temp2_crit can always be set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928)  * On LM96163, temp2_crit can be set if bit 1 of the configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929)  * register is true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) static umode_t lm63_attribute_mode(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 				   struct attribute *attr, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	struct device *dev = container_of(kobj, struct device, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	struct lm63_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	    && (data->kind == lm64 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		(data->kind == lm96163 && (data->config & 0x02))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		return attr->mode | S_IWUSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	return attr->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) static const struct attribute_group lm63_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	.is_visible = lm63_attribute_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	.attrs = lm63_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) static struct attribute *lm63_attributes_fan1[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	&sensor_dev_attr_fan1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	&sensor_dev_attr_fan1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	&sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) static const struct attribute_group lm63_group_fan1 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	.attrs = lm63_attributes_fan1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963)  * Real code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) /* Return 0 if detection is successful, -ENODEV otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) static int lm63_detect(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		       struct i2c_board_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	u8 man_id, chip_id, reg_config1, reg_config2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	u8 reg_alert_status, reg_alert_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	int address = client->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	man_id = i2c_smbus_read_byte_data(client, LM63_REG_MAN_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	chip_id = i2c_smbus_read_byte_data(client, LM63_REG_CHIP_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	reg_config1 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	reg_config2 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	reg_alert_status = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			   LM63_REG_ALERT_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	reg_alert_mask = i2c_smbus_read_byte_data(client, LM63_REG_ALERT_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	if (man_id != 0x01 /* National Semiconductor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	 || (reg_config1 & 0x18) != 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	 || (reg_config2 & 0xF8) != 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	 || (reg_alert_status & 0x20) != 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	 || (reg_alert_mask & 0xA4) != 0xA4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		dev_dbg(&adapter->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 			"Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 			man_id, chip_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	if (chip_id == 0x41 && address == 0x4c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		strlcpy(info->type, "lm63", I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		strlcpy(info->type, "lm64", I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	else if (chip_id == 0x49 && address == 0x4c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		strlcpy(info->type, "lm96163", I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)  * Ideally we shouldn't have to initialize anything, since the BIOS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)  * should have taken care of everything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) static void lm63_init_client(struct lm63_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	u8 convrate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	data->config_fan = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 						    LM63_REG_CONFIG_FAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	/* Start converting if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	if (data->config & 0x40) { /* standby */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		dev_dbg(dev, "Switching to operational mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		data->config &= 0xA7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 					  data->config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	/* Tachometer is always enabled on LM64 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	if (data->kind == lm64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		data->config |= 0x04;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	/* We may need pwm1_freq before ever updating the client data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	if (data->pwm1_freq == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		data->pwm1_freq = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	switch (data->kind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	case lm63:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	case lm64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		data->max_convrate_hz = LM63_MAX_CONVRATE_HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		data->lut_size = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	case lm96163:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		data->lut_size = 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		data->trutherm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		  = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 					     LM96163_REG_TRUTHERM) & 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	if (unlikely(convrate > LM63_MAX_CONVRATE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		convrate = LM63_MAX_CONVRATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 						convrate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	 * For LM96163, check if high resolution PWM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	 * and unsigned temperature format is enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	if (data->kind == lm96163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		u8 config_enhanced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		  = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 					     LM96163_REG_CONFIG_ENHANCED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		if (config_enhanced & 0x20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 			data->lut_temp_highres = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		if ((config_enhanced & 0x10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		    && !(data->config_fan & 0x08) && data->pwm1_freq == 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			data->pwm_highres = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 		if (config_enhanced & 0x08)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 			data->remote_unsigned = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	/* Show some debug info about the LM63 configuration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	if (data->kind == lm63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		dev_dbg(dev, "Alert/tach pin configured for %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 			(data->config & 0x04) ? "tachometer input" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 			"alert output");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	dev_dbg(dev, "PWM clock %s kHz, output frequency %u Hz\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		(data->config_fan & 0x08) ? "1.4" : "360",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	dev_dbg(dev, "PWM output active %s, %s mode\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		(data->config_fan & 0x10) ? "low" : "high",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		(data->config_fan & 0x20) ? "manual" : "auto");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) static const struct i2c_device_id lm63_id[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) static int lm63_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	struct lm63_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	int groups = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	data = devm_kzalloc(dev, sizeof(struct lm63_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	/* Set the device type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	if (client->dev.of_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		data->kind = (enum chips)of_device_get_match_data(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		data->kind = i2c_match_id(lm63_id, client)->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	if (data->kind == lm64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		data->temp2_offset = 16000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	/* Initialize chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	lm63_init_client(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	/* Register sysfs hooks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	data->groups[groups++] = &lm63_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	if (data->config & 0x04)	/* tachometer enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		data->groups[groups++] = &lm63_group_fan1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	if (data->kind == lm96163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		data->groups[groups++] = &lm63_group_temp2_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		data->groups[groups++] = &lm63_group_extra_lut;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 							   data, data->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	return PTR_ERR_OR_ZERO(hwmon_dev);
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133)  * Driver data (common to all clients)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) static const struct i2c_device_id lm63_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	{ "lm63", lm63 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	{ "lm64", lm64 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	{ "lm96163", lm96163 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) MODULE_DEVICE_TABLE(i2c, lm63_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) static const struct of_device_id __maybe_unused lm63_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		.compatible = "national,lm63",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		.data = (void *)lm63
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		.compatible = "national,lm64",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		.data = (void *)lm64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		.compatible = "national,lm96163",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		.data = (void *)lm96163
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) MODULE_DEVICE_TABLE(of, lm63_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) static struct i2c_driver lm63_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	.class		= I2C_CLASS_HWMON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		.name	= "lm63",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		.of_match_table = of_match_ptr(lm63_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	.probe_new	= lm63_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	.id_table	= lm63_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	.detect		= lm63_detect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	.address_list	= normal_i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) module_i2c_driver(lm63_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) MODULE_DESCRIPTION("LM63 driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) MODULE_LICENSE("GPL");