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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * w83l786ng.c - Linux kernel driver for hardware monitoring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (c) 2007 Kevin Lo <kevlo@kevlo.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Supports following chips:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Chip		#vin	#fanin	#pwm	#temp	wchipid	vendid	i2c	ISA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * w83l786ng	3	2	2	2	0x7b	0x5ca3	yes	no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/hwmon-vid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /* Addresses to scan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static const unsigned short normal_i2c[] = { 0x2e, 0x2f, I2C_CLIENT_END };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) /* Insmod parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static bool reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) module_param(reset, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define W83L786NG_REG_IN_MIN(nr)	(0x2C + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define W83L786NG_REG_IN_MAX(nr)	(0x2B + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define W83L786NG_REG_IN(nr)		((nr) + 0x20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define W83L786NG_REG_FAN(nr)		((nr) + 0x28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define W83L786NG_REG_FAN_MIN(nr)	((nr) + 0x3B)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define W83L786NG_REG_CONFIG		0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define W83L786NG_REG_ALARM1		0x41
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define W83L786NG_REG_ALARM2		0x42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define W83L786NG_REG_GPIO_EN		0x47
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define W83L786NG_REG_MAN_ID2		0x4C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define W83L786NG_REG_MAN_ID1		0x4D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define W83L786NG_REG_CHIP_ID		0x4E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define W83L786NG_REG_DIODE		0x53
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define W83L786NG_REG_FAN_DIV		0x54
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define W83L786NG_REG_FAN_CFG		0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define W83L786NG_REG_TOLERANCE		0x8D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static const u8 W83L786NG_REG_TEMP[2][3] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	{ 0x25,		/* TEMP 0 in DataSheet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	  0x35,		/* TEMP 0 Over in DataSheet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	  0x36 },	/* TEMP 0 Hyst in DataSheet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	{ 0x26,		/* TEMP 1 in DataSheet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	  0x37,		/* TEMP 1 Over in DataSheet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	  0x38 }	/* TEMP 1 Hyst in DataSheet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static const u8 W83L786NG_PWM_MODE_SHIFT[] = {6, 7};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static const u8 W83L786NG_PWM_ENABLE_SHIFT[] = {2, 4};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) /* FAN Duty Cycle, be used to control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static const u8 W83L786NG_REG_PWM[] = {0x81, 0x87};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static inline u8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) FAN_TO_REG(long rpm, int div)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (rpm == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		return 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	rpm = clamp_val(rpm, 1, 1000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #define FAN_FROM_REG(val, div)	((val) == 0   ? -1 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 				((val) == 255 ? 0 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 				1350000 / ((val) * (div))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) /* for temp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) #define TEMP_TO_REG(val)	(clamp_val(((val) < 0 ? (val) + 0x100 * 1000 \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 						      : (val)) / 1000, 0, 0xff))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) #define TEMP_FROM_REG(val)	(((val) & 0x80 ? \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 				  (val) - 0x100 : (val)) * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * The analog voltage inputs have 8mV LSB. Since the sysfs output is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * in mV as would be measured on the chip input pin, need to just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * multiply/divide by 8 to translate from/to register values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) #define IN_TO_REG(val)		(clamp_val((((val) + 4) / 8), 0, 255))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) #define IN_FROM_REG(val)	((val) * 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) #define DIV_FROM_REG(val)	(1 << (val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static inline u8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) DIV_TO_REG(long val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	val = clamp_val(val, 1, 128) >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	for (i = 0; i < 7; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		if (val == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		val >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	return (u8)i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct w83l786ng_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	char valid;			/* !=0 if following fields are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	unsigned long last_updated;	/* In jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	unsigned long last_nonvolatile;	/* In jiffies, last time we update the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 					 * nonvolatile registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	u8 in[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	u8 in_max[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	u8 in_min[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	u8 fan[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	u8 fan_div[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	u8 fan_min[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	u8 temp_type[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	u8 temp[2][3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	u8 pwm[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	u8 pwm_mode[2];	/* 0->DC variable voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			 * 1->PWM variable duty cycle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	u8 pwm_enable[2]; /* 1->manual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			   * 2->thermal cruise (also called SmartFan I) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	u8 tolerance[2];
^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) static u8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) w83l786ng_read_value(struct i2c_client *client, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return i2c_smbus_read_byte_data(client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) w83l786ng_write_value(struct i2c_client *client, u8 reg, u8 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return i2c_smbus_write_byte_data(client, reg, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static struct w83l786ng_data *w83l786ng_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct w83l786ng_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	u8 reg_tmp, pwmcfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	    || !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		dev_dbg(&client->dev, "Updating w83l786ng data.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		/* Update the voltages measured value and limits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		for (i = 0; i < 3; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			data->in[i] = w83l786ng_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			    W83L786NG_REG_IN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			data->in_min[i] = w83l786ng_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			    W83L786NG_REG_IN_MIN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			data->in_max[i] = w83l786ng_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			    W83L786NG_REG_IN_MAX(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		/* Update the fan counts and limits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			data->fan[i] = w83l786ng_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			    W83L786NG_REG_FAN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			data->fan_min[i] = w83l786ng_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			    W83L786NG_REG_FAN_MIN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		/* Update the fan divisor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		data->fan_div[0] = reg_tmp & 0x07;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		data->fan_div[1] = (reg_tmp >> 4) & 0x07;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		pwmcfg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			data->pwm_mode[i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			    ((pwmcfg >> W83L786NG_PWM_MODE_SHIFT[i]) & 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			    ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			data->pwm_enable[i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			    ((pwmcfg >> W83L786NG_PWM_ENABLE_SHIFT[i]) & 3) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			data->pwm[i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			    (w83l786ng_read_value(client, W83L786NG_REG_PWM[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			     & 0x0f) * 0x11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		/* Update the temperature sensors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			for (j = 0; j < 3; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 				data->temp[i][j] = w83l786ng_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 				    W83L786NG_REG_TEMP[i][j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			}
^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) 		/* Update Smart Fan I/II tolerance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_TOLERANCE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		data->tolerance[0] = reg_tmp & 0x0f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		data->tolerance[1] = (reg_tmp >> 4) & 0x0f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		data->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /* following are the sysfs callback functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) #define show_in_reg(reg) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static ssize_t \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) show_##reg(struct device *dev, struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	   char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	int nr = to_sensor_dev_attr(attr)->index; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	struct w83l786ng_data *data = w83l786ng_update_device(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) show_in_reg(in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) show_in_reg(in_min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) show_in_reg(in_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) #define store_in_reg(REG, reg) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static ssize_t \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) store_in_##reg(struct device *dev, struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	       const char *buf, size_t count) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	int nr = to_sensor_dev_attr(attr)->index; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	struct w83l786ng_data *data = dev_get_drvdata(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	struct i2c_client *client = data->client; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	unsigned long val; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	int err = kstrtoul(buf, 10, &val); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	if (err) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		return err; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	mutex_lock(&data->update_lock); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	data->in_##reg[nr] = IN_TO_REG(val); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	w83l786ng_write_value(client, W83L786NG_REG_IN_##REG(nr), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			      data->in_##reg[nr]); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	mutex_unlock(&data->update_lock); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	return count; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) store_in_reg(MIN, min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) store_in_reg(MAX, max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static struct sensor_device_attribute sda_in_input[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static struct sensor_device_attribute sda_in_min[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static struct sensor_device_attribute sda_in_max[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) #define show_fan_reg(reg) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			  char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	int nr = to_sensor_dev_attr(attr)->index; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	struct w83l786ng_data *data = w83l786ng_update_device(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return sprintf(buf, "%d\n", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		FAN_FROM_REG(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) show_fan_reg(fan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) show_fan_reg(fan_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) store_fan_min(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	      const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	struct w83l786ng_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			      data->fan_min[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) show_fan_div(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	struct w83l786ng_data *data = w83l786ng_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	return sprintf(buf, "%u\n", DIV_FROM_REG(data->fan_div[nr]));
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)  * Note: we save and restore the fan minimum here, because its value is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)  * determined in part by the fan divisor.  This follows the principle of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)  * least surprise; the user doesn't expect the fan minimum to change just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)  * because the divisor changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) store_fan_div(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	      const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	struct w83l786ng_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	unsigned long min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	u8 tmp_fan_div;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	u8 fan_div_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	u8 keep_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	u8 new_shift = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	/* Save fan_min */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	data->fan_div[nr] = DIV_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	switch (nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		keep_mask = 0xf8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		new_shift = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		keep_mask = 0x8f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		new_shift = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	fan_div_reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 					   & keep_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	tmp_fan_div = (data->fan_div[nr] << new_shift) & ~keep_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	w83l786ng_write_value(client, W83L786NG_REG_FAN_DIV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			      fan_div_reg | tmp_fan_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	/* Restore fan_min */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 			      data->fan_min[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) static struct sensor_device_attribute sda_fan_input[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static struct sensor_device_attribute sda_fan_min[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		    store_fan_min, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		    store_fan_min, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) static struct sensor_device_attribute sda_fan_div[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	SENSOR_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		    store_fan_div, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	SENSOR_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		    store_fan_div, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /* read/write the temperature, includes measured value and limits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) show_temp(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	struct sensor_device_attribute_2 *sensor_attr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	    to_sensor_dev_attr_2(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	int nr = sensor_attr->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	int index = sensor_attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	struct w83l786ng_data *data = w83l786ng_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) store_temp(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	   const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	struct sensor_device_attribute_2 *sensor_attr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	    to_sensor_dev_attr_2(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	int nr = sensor_attr->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	int index = sensor_attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	struct w83l786ng_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	data->temp[nr][index] = TEMP_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	w83l786ng_write_value(client, W83L786NG_REG_TEMP[nr][index],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 			      data->temp[nr][index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) static struct sensor_device_attribute_2 sda_temp_input[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) static struct sensor_device_attribute_2 sda_temp_max[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		      show_temp, store_temp, 0, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		      show_temp, store_temp, 1, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		      show_temp, store_temp, 0, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		      show_temp, store_temp, 1, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) #define show_pwm_reg(reg) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 			  char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	struct w83l786ng_data *data = w83l786ng_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) 	return sprintf(buf, "%d\n", data->reg[nr]); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) show_pwm_reg(pwm_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) show_pwm_reg(pwm_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) show_pwm_reg(pwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) store_pwm_mode(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	       const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	struct w83l786ng_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	if (val > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	data->pwm_mode[nr] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	reg &= ~(1 << W83L786NG_PWM_MODE_SHIFT[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	if (!val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		reg |= 1 << W83L786NG_PWM_MODE_SHIFT[nr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) store_pwm(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	  const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	struct w83l786ng_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	val = clamp_val(val, 0, 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	val = DIV_ROUND_CLOSEST(val, 0x11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	data->pwm[nr] = val * 0x11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	val |= w83l786ng_read_value(client, W83L786NG_REG_PWM[nr]) & 0xf0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	w83l786ng_write_value(client, W83L786NG_REG_PWM[nr], val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) store_pwm_enable(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		 const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	struct w83l786ng_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	if (!val || val > 2)  /* only modes 1 and 2 are supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	data->pwm_enable[nr] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	reg &= ~(0x03 << W83L786NG_PWM_ENABLE_SHIFT[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	reg |= (val - 1) << W83L786NG_PWM_ENABLE_SHIFT[nr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) static struct sensor_device_attribute sda_pwm[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) static struct sensor_device_attribute sda_pwm_mode[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		    store_pwm_mode, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		    store_pwm_mode, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) static struct sensor_device_attribute sda_pwm_enable[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		    store_pwm_enable, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		    store_pwm_enable, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) /* For Smart Fan I/Thermal Cruise and Smart Fan II */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) show_tolerance(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	struct w83l786ng_data *data = w83l786ng_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	return sprintf(buf, "%ld\n", (long)data->tolerance[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) store_tolerance(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	struct w83l786ng_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	u8 tol_tmp, tol_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	tol_mask = w83l786ng_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	    W83L786NG_REG_TOLERANCE) & ((nr == 1) ? 0x0f : 0xf0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	tol_tmp = clamp_val(val, 0, 15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	tol_tmp &= 0x0f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	data->tolerance[nr] = tol_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	if (nr == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		tol_tmp <<= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	w83l786ng_write_value(client, W83L786NG_REG_TOLERANCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 			      tol_mask | tol_tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	return count;
^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) static struct sensor_device_attribute sda_tolerance[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		    show_tolerance, store_tolerance, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		    show_tolerance, store_tolerance, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) #define IN_UNIT_ATTRS(X)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	&sda_in_input[X].dev_attr.attr,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	&sda_in_min[X].dev_attr.attr,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	&sda_in_max[X].dev_attr.attr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) #define FAN_UNIT_ATTRS(X)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	&sda_fan_input[X].dev_attr.attr,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	&sda_fan_min[X].dev_attr.attr,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	&sda_fan_div[X].dev_attr.attr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) #define TEMP_UNIT_ATTRS(X)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	&sda_temp_input[X].dev_attr.attr,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	&sda_temp_max[X].dev_attr.attr,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	&sda_temp_max_hyst[X].dev_attr.attr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) #define PWM_UNIT_ATTRS(X)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	&sda_pwm[X].dev_attr.attr,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	&sda_pwm_mode[X].dev_attr.attr,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	&sda_pwm_enable[X].dev_attr.attr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) #define TOLERANCE_UNIT_ATTRS(X)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	&sda_tolerance[X].dev_attr.attr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) static struct attribute *w83l786ng_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	IN_UNIT_ATTRS(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	IN_UNIT_ATTRS(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	IN_UNIT_ATTRS(2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	FAN_UNIT_ATTRS(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	FAN_UNIT_ATTRS(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	TEMP_UNIT_ATTRS(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	TEMP_UNIT_ATTRS(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	PWM_UNIT_ATTRS(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	PWM_UNIT_ATTRS(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	TOLERANCE_UNIT_ATTRS(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	TOLERANCE_UNIT_ATTRS(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) ATTRIBUTE_GROUPS(w83l786ng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) w83l786ng_detect(struct i2c_client *client, struct i2c_board_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	u16 man_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	u8 chip_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	/* Detection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	if ((w83l786ng_read_value(client, W83L786NG_REG_CONFIG) & 0x80)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 		dev_dbg(&adapter->dev, "W83L786NG detection failed at 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 			client->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	/* Identification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	man_id = (w83l786ng_read_value(client, W83L786NG_REG_MAN_ID1) << 8) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		 w83l786ng_read_value(client, W83L786NG_REG_MAN_ID2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	chip_id = w83l786ng_read_value(client, W83L786NG_REG_CHIP_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	if (man_id != 0x5CA3 ||		/* Winbond */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	    chip_id != 0x80) {		/* W83L786NG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		dev_dbg(&adapter->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 			"Unsupported chip (man_id=0x%04X, chip_id=0x%02X)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 			man_id, chip_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	strlcpy(info->type, "w83l786ng", I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) static void w83l786ng_init_client(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	u8 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	if (reset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 		w83l786ng_write_value(client, W83L786NG_REG_CONFIG, 0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	/* Start monitoring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	tmp = w83l786ng_read_value(client, W83L786NG_REG_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	if (!(tmp & 0x01))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		w83l786ng_write_value(client, W83L786NG_REG_CONFIG, tmp | 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) w83l786ng_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	struct w83l786ng_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	u8 reg_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	data = devm_kzalloc(dev, sizeof(struct w83l786ng_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	/* Initialize the chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	w83l786ng_init_client(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	/* A few vars need to be filled upon startup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 		data->fan_min[i] = w83l786ng_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		    W83L786NG_REG_FAN_MIN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	/* Update the fan divisor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	data->fan_div[0] = reg_tmp & 0x07;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	data->fan_div[1] = (reg_tmp >> 4) & 0x07;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 							   data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 							   w83l786ng_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) static const struct i2c_device_id w83l786ng_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	{ "w83l786ng", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) MODULE_DEVICE_TABLE(i2c, w83l786ng_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) static struct i2c_driver w83l786ng_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	.class		= I2C_CLASS_HWMON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 		   .name = "w83l786ng",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	.probe_new	= w83l786ng_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	.id_table	= w83l786ng_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	.detect		= w83l786ng_detect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	.address_list	= normal_i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) module_i2c_driver(w83l786ng_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) MODULE_AUTHOR("Kevin Lo");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) MODULE_DESCRIPTION("w83l786ng driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) MODULE_LICENSE("GPL");