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)  * adm1025.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2000       Chen-Yuan Wu <gwu@esoft.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2003-2009  Jean Delvare <jdelvare@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * The ADM1025 is a sensor chip made by Analog Devices. It reports up to 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * voltages (including its own power source) and up to two temperatures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * (its own plus up to one external one). Voltages are scaled internally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * (which is not the common way) with ratios such that the nominal value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * of each voltage correspond to a register value of 192 (which means a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * resolution of about 0.5% of the nominal value). Temperature values are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * reported with a 1 deg resolution and a 3 deg accuracy. Complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * datasheet can be obtained from Analog's website at:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *   https://www.onsemi.com/PowerSolutions/product.do?id=ADM1025
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * This driver also supports the ADM1025A, which differs from the ADM1025
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * only in that it has "open-drain VID inputs while the ADM1025 has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * on-chip 100k pull-ups on the VID inputs". It doesn't make any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * difference for us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * This driver also supports the NE1619, a sensor chip made by Philips.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * That chip is similar to the ADM1025A, with a few differences. The only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * difference that matters to us is that the NE1619 has only two possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * addresses while the ADM1025A has a third one. Complete datasheet can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * obtained from Philips's website at:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *   http://www.semiconductors.philips.com/pip/NE1619DS.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * Since the ADM1025 was the first chipset supported by this driver, most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * comments will refer to this chipset, but are actually general and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * concern all supported chipsets, unless mentioned otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #include <linux/hwmon-vid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * Addresses to scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * ADM1025 and ADM1025A have three possible addresses: 0x2c, 0x2d and 0x2e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * NE1619 has two possible addresses: 0x2c and 0x2d.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) enum chips { adm1025, ne1619 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * The ADM1025 registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define ADM1025_REG_MAN_ID		0x3E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #define ADM1025_REG_CHIP_ID		0x3F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #define ADM1025_REG_CONFIG		0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #define ADM1025_REG_STATUS1		0x41
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define ADM1025_REG_STATUS2		0x42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #define ADM1025_REG_IN(nr)		(0x20 + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #define ADM1025_REG_IN_MAX(nr)		(0x2B + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #define ADM1025_REG_IN_MIN(nr)		(0x2C + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #define ADM1025_REG_TEMP(nr)		(0x26 + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #define ADM1025_REG_TEMP_HIGH(nr)	(0x37 + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #define ADM1025_REG_TEMP_LOW(nr)	(0x38 + (nr) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define ADM1025_REG_VID			0x47
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #define ADM1025_REG_VID4		0x49
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * Conversions and various macros
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * The ADM1025 uses signed 8-bit values for temperatures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static const int in_scale[6] = { 2500, 2250, 3300, 5000, 12000, 3300 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) #define IN_FROM_REG(reg, scale)	(((reg) * (scale) + 96) / 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) #define IN_TO_REG(val, scale)	((val) <= 0 ? 0 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 				 (val) >= (scale) * 255 / 192 ? 255 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 				 ((val) * 192 + (scale) / 2) / (scale))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) #define TEMP_FROM_REG(reg)	((reg) * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) #define TEMP_TO_REG(val)	((val) <= -127500 ? -128 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 				 (val) >= 126500 ? 127 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 				 (((val) < 0 ? (val) - 500 : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 				   (val) + 500) / 1000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * Client data (each client gets its own)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) struct adm1025_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	const struct attribute_group *groups[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	char valid; /* zero until following fields are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	unsigned long last_updated; /* in jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	u8 in[6];		/* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	u8 in_max[6];		/* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	u8 in_min[6];		/* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	s8 temp[2];		/* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	s8 temp_min[2];		/* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	s8 temp_max[2];		/* register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	u16 alarms;		/* register values, combined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	u8 vid;			/* register values, combined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	u8 vrm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static struct adm1025_data *adm1025_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct adm1025_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		dev_dbg(&client->dev, "Updating data.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		for (i = 0; i < 6; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			data->in[i] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 				      ADM1025_REG_IN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			data->in_min[i] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 					  ADM1025_REG_IN_MIN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			data->in_max[i] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 					  ADM1025_REG_IN_MAX(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			data->temp[i] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 					ADM1025_REG_TEMP(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			data->temp_min[i] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 					    ADM1025_REG_TEMP_LOW(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			data->temp_max[i] = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 					    ADM1025_REG_TEMP_HIGH(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		data->alarms = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			       ADM1025_REG_STATUS1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			     | (i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 				ADM1025_REG_STATUS2) << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		data->vid = (i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			     ADM1025_REG_VID) & 0x0f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			  | ((i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			      ADM1025_REG_VID4) & 0x01) << 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		data->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  * Sysfs stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) in_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	int index = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	struct adm1025_data *data = adm1025_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[index],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		       in_scale[index]));
^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) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) in_min_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	int index = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	struct adm1025_data *data = adm1025_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[index],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		       in_scale[index]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) in_max_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	int index = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct adm1025_data *data = adm1025_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[index],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		       in_scale[index]));
^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 ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) temp_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	int index = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	struct adm1025_data *data = adm1025_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[index]));
^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) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) temp_min_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	int index = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	struct adm1025_data *data = adm1025_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[index]));
^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) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) temp_max_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	int index = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	struct adm1025_data *data = adm1025_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[index]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			    const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	int index = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	struct adm1025_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	data->in_min[index] = IN_TO_REG(val, in_scale[index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MIN(index),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 				  data->in_min[index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			    const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	int index = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	struct adm1025_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^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_max[index] = IN_TO_REG(val, in_scale[index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MAX(index),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 				  data->in_max[index]);
^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) static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static SENSOR_DEVICE_ATTR_RO(in5_input, in, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static ssize_t temp_min_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			      struct device_attribute *attr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			      size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	int index = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	struct adm1025_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	data->temp_min[index] = TEMP_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_LOW(index),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 				  data->temp_min[index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static ssize_t temp_max_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			      struct device_attribute *attr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			      size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	int index = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	struct adm1025_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	err = kstrtol(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	data->temp_max[index] = TEMP_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_HIGH(index),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 				  data->temp_max[index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) alarms_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	struct adm1025_data *data = adm1025_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	return sprintf(buf, "%u\n", data->alarms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static DEVICE_ATTR_RO(alarms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) alarm_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	int bitnr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	struct adm1025_data *data = adm1025_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static SENSOR_DEVICE_ATTR_RO(temp1_fault, alarm, 14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) cpu0_vid_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	struct adm1025_data *data = adm1025_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static DEVICE_ATTR_RO(cpu0_vid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) vrm_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	struct adm1025_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	return sprintf(buf, "%u\n", data->vrm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			 const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	struct adm1025_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	if (val > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	data->vrm = val;
^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) static DEVICE_ATTR_RW(vrm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)  * Real code
^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 attribute *adm1025_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	&sensor_dev_attr_in0_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	&sensor_dev_attr_in1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	&sensor_dev_attr_in2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	&sensor_dev_attr_in3_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	&sensor_dev_attr_in5_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	&sensor_dev_attr_in0_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	&sensor_dev_attr_in1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	&sensor_dev_attr_in2_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	&sensor_dev_attr_in3_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	&sensor_dev_attr_in5_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	&sensor_dev_attr_in0_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	&sensor_dev_attr_in1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	&sensor_dev_attr_in2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	&sensor_dev_attr_in3_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	&sensor_dev_attr_in5_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	&sensor_dev_attr_temp2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	&sensor_dev_attr_temp1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	&sensor_dev_attr_temp2_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	&sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	&sensor_dev_attr_temp2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	&dev_attr_alarms.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	&dev_attr_cpu0_vid.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	&dev_attr_vrm.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) static const struct attribute_group adm1025_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	.attrs = adm1025_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static struct attribute *adm1025_attributes_in4[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	&sensor_dev_attr_in4_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	&sensor_dev_attr_in4_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	&sensor_dev_attr_in4_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) static const struct attribute_group adm1025_group_in4 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	.attrs = adm1025_attributes_in4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) /* Return 0 if detection is successful, -ENODEV otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) static int adm1025_detect(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			  struct i2c_board_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	u8 man_id, chip_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	/* Check for unused bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	if ((i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG) & 0x80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	 || (i2c_smbus_read_byte_data(client, ADM1025_REG_STATUS1) & 0xC0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	 || (i2c_smbus_read_byte_data(client, ADM1025_REG_STATUS2) & 0xBC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		dev_dbg(&adapter->dev, "ADM1025 detection failed at 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			client->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	/* Identification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	chip_id = i2c_smbus_read_byte_data(client, ADM1025_REG_CHIP_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	if ((chip_id & 0xF0) != 0x20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	man_id = i2c_smbus_read_byte_data(client, ADM1025_REG_MAN_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	if (man_id == 0x41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		name = "adm1025";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	else if (man_id == 0xA1 && client->addr != 0x2E)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		name = "ne1619";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	strlcpy(info->type, name, I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) static void adm1025_init_client(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	struct adm1025_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	data->vrm = vid_which_vrm();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	 * Set high limits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	 * Usually we avoid setting limits on driver init, but it happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	 * that the ADM1025 comes with stupid default limits (all registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	 * set to 0). In case the chip has not gone through any limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	 * setting yet, we better set the high limits to the max so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	 * no alarm triggers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	for (i = 0; i < 6; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		reg = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 					       ADM1025_REG_IN_MAX(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		if (reg == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 						  ADM1025_REG_IN_MAX(i),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 						  0xFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		reg = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 					       ADM1025_REG_TEMP_HIGH(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		if (reg == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 			i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 						  ADM1025_REG_TEMP_HIGH(i),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 						  0x7F);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	 * Start the conversions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	reg = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	if (!(reg & 0x01))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		i2c_smbus_write_byte_data(client, ADM1025_REG_CONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 					  (reg&0x7E)|0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static int adm1025_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	struct adm1025_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	u8 config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	data = devm_kzalloc(dev, sizeof(struct adm1025_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	i2c_set_clientdata(client, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	/* Initialize the ADM1025 chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	adm1025_init_client(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	/* sysfs hooks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	data->groups[0] = &adm1025_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	/* Pin 11 is either in4 (+12V) or VID4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	config = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	if (!(config & 0x20))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		data->groups[1] = &adm1025_group_in4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 							   data, data->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	return PTR_ERR_OR_ZERO(hwmon_dev);
^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 const struct i2c_device_id adm1025_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	{ "adm1025", adm1025 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	{ "ne1619", ne1619 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) MODULE_DEVICE_TABLE(i2c, adm1025_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) static struct i2c_driver adm1025_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	.class		= I2C_CLASS_HWMON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		.name	= "adm1025",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	.probe_new	= adm1025_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	.id_table	= adm1025_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	.detect		= adm1025_detect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	.address_list	= normal_i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) module_i2c_driver(adm1025_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) MODULE_DESCRIPTION("ADM1025 driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) MODULE_LICENSE("GPL");