18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * adm1025.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2000       Chen-Yuan Wu <gwu@esoft.com>
68c2ecf20Sopenharmony_ci * Copyright (C) 2003-2009  Jean Delvare <jdelvare@suse.de>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * The ADM1025 is a sensor chip made by Analog Devices. It reports up to 6
98c2ecf20Sopenharmony_ci * voltages (including its own power source) and up to two temperatures
108c2ecf20Sopenharmony_ci * (its own plus up to one external one). Voltages are scaled internally
118c2ecf20Sopenharmony_ci * (which is not the common way) with ratios such that the nominal value
128c2ecf20Sopenharmony_ci * of each voltage correspond to a register value of 192 (which means a
138c2ecf20Sopenharmony_ci * resolution of about 0.5% of the nominal value). Temperature values are
148c2ecf20Sopenharmony_ci * reported with a 1 deg resolution and a 3 deg accuracy. Complete
158c2ecf20Sopenharmony_ci * datasheet can be obtained from Analog's website at:
168c2ecf20Sopenharmony_ci *   https://www.onsemi.com/PowerSolutions/product.do?id=ADM1025
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * This driver also supports the ADM1025A, which differs from the ADM1025
198c2ecf20Sopenharmony_ci * only in that it has "open-drain VID inputs while the ADM1025 has
208c2ecf20Sopenharmony_ci * on-chip 100k pull-ups on the VID inputs". It doesn't make any
218c2ecf20Sopenharmony_ci * difference for us.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * This driver also supports the NE1619, a sensor chip made by Philips.
248c2ecf20Sopenharmony_ci * That chip is similar to the ADM1025A, with a few differences. The only
258c2ecf20Sopenharmony_ci * difference that matters to us is that the NE1619 has only two possible
268c2ecf20Sopenharmony_ci * addresses while the ADM1025A has a third one. Complete datasheet can be
278c2ecf20Sopenharmony_ci * obtained from Philips's website at:
288c2ecf20Sopenharmony_ci *   http://www.semiconductors.philips.com/pip/NE1619DS.html
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * Since the ADM1025 was the first chipset supported by this driver, most
318c2ecf20Sopenharmony_ci * comments will refer to this chipset, but are actually general and
328c2ecf20Sopenharmony_ci * concern all supported chipsets, unless mentioned otherwise.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <linux/module.h>
368c2ecf20Sopenharmony_ci#include <linux/init.h>
378c2ecf20Sopenharmony_ci#include <linux/slab.h>
388c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
398c2ecf20Sopenharmony_ci#include <linux/i2c.h>
408c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
418c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
428c2ecf20Sopenharmony_ci#include <linux/hwmon-vid.h>
438c2ecf20Sopenharmony_ci#include <linux/err.h>
448c2ecf20Sopenharmony_ci#include <linux/mutex.h>
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/*
478c2ecf20Sopenharmony_ci * Addresses to scan
488c2ecf20Sopenharmony_ci * ADM1025 and ADM1025A have three possible addresses: 0x2c, 0x2d and 0x2e.
498c2ecf20Sopenharmony_ci * NE1619 has two possible addresses: 0x2c and 0x2d.
508c2ecf20Sopenharmony_ci */
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cienum chips { adm1025, ne1619 };
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/*
578c2ecf20Sopenharmony_ci * The ADM1025 registers
588c2ecf20Sopenharmony_ci */
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define ADM1025_REG_MAN_ID		0x3E
618c2ecf20Sopenharmony_ci#define ADM1025_REG_CHIP_ID		0x3F
628c2ecf20Sopenharmony_ci#define ADM1025_REG_CONFIG		0x40
638c2ecf20Sopenharmony_ci#define ADM1025_REG_STATUS1		0x41
648c2ecf20Sopenharmony_ci#define ADM1025_REG_STATUS2		0x42
658c2ecf20Sopenharmony_ci#define ADM1025_REG_IN(nr)		(0x20 + (nr))
668c2ecf20Sopenharmony_ci#define ADM1025_REG_IN_MAX(nr)		(0x2B + (nr) * 2)
678c2ecf20Sopenharmony_ci#define ADM1025_REG_IN_MIN(nr)		(0x2C + (nr) * 2)
688c2ecf20Sopenharmony_ci#define ADM1025_REG_TEMP(nr)		(0x26 + (nr))
698c2ecf20Sopenharmony_ci#define ADM1025_REG_TEMP_HIGH(nr)	(0x37 + (nr) * 2)
708c2ecf20Sopenharmony_ci#define ADM1025_REG_TEMP_LOW(nr)	(0x38 + (nr) * 2)
718c2ecf20Sopenharmony_ci#define ADM1025_REG_VID			0x47
728c2ecf20Sopenharmony_ci#define ADM1025_REG_VID4		0x49
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/*
758c2ecf20Sopenharmony_ci * Conversions and various macros
768c2ecf20Sopenharmony_ci * The ADM1025 uses signed 8-bit values for temperatures.
778c2ecf20Sopenharmony_ci */
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic const int in_scale[6] = { 2500, 2250, 3300, 5000, 12000, 3300 };
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci#define IN_FROM_REG(reg, scale)	(((reg) * (scale) + 96) / 192)
828c2ecf20Sopenharmony_ci#define IN_TO_REG(val, scale)	((val) <= 0 ? 0 : \
838c2ecf20Sopenharmony_ci				 (val) >= (scale) * 255 / 192 ? 255 : \
848c2ecf20Sopenharmony_ci				 ((val) * 192 + (scale) / 2) / (scale))
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci#define TEMP_FROM_REG(reg)	((reg) * 1000)
878c2ecf20Sopenharmony_ci#define TEMP_TO_REG(val)	((val) <= -127500 ? -128 : \
888c2ecf20Sopenharmony_ci				 (val) >= 126500 ? 127 : \
898c2ecf20Sopenharmony_ci				 (((val) < 0 ? (val) - 500 : \
908c2ecf20Sopenharmony_ci				   (val) + 500) / 1000))
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/*
938c2ecf20Sopenharmony_ci * Client data (each client gets its own)
948c2ecf20Sopenharmony_ci */
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistruct adm1025_data {
978c2ecf20Sopenharmony_ci	struct i2c_client *client;
988c2ecf20Sopenharmony_ci	const struct attribute_group *groups[3];
998c2ecf20Sopenharmony_ci	struct mutex update_lock;
1008c2ecf20Sopenharmony_ci	char valid; /* zero until following fields are valid */
1018c2ecf20Sopenharmony_ci	unsigned long last_updated; /* in jiffies */
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	u8 in[6];		/* register value */
1048c2ecf20Sopenharmony_ci	u8 in_max[6];		/* register value */
1058c2ecf20Sopenharmony_ci	u8 in_min[6];		/* register value */
1068c2ecf20Sopenharmony_ci	s8 temp[2];		/* register value */
1078c2ecf20Sopenharmony_ci	s8 temp_min[2];		/* register value */
1088c2ecf20Sopenharmony_ci	s8 temp_max[2];		/* register value */
1098c2ecf20Sopenharmony_ci	u16 alarms;		/* register values, combined */
1108c2ecf20Sopenharmony_ci	u8 vid;			/* register values, combined */
1118c2ecf20Sopenharmony_ci	u8 vrm;
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic struct adm1025_data *adm1025_update_device(struct device *dev)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	struct adm1025_data *data = dev_get_drvdata(dev);
1178c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1228c2ecf20Sopenharmony_ci		int i;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "Updating data.\n");
1258c2ecf20Sopenharmony_ci		for (i = 0; i < 6; i++) {
1268c2ecf20Sopenharmony_ci			data->in[i] = i2c_smbus_read_byte_data(client,
1278c2ecf20Sopenharmony_ci				      ADM1025_REG_IN(i));
1288c2ecf20Sopenharmony_ci			data->in_min[i] = i2c_smbus_read_byte_data(client,
1298c2ecf20Sopenharmony_ci					  ADM1025_REG_IN_MIN(i));
1308c2ecf20Sopenharmony_ci			data->in_max[i] = i2c_smbus_read_byte_data(client,
1318c2ecf20Sopenharmony_ci					  ADM1025_REG_IN_MAX(i));
1328c2ecf20Sopenharmony_ci		}
1338c2ecf20Sopenharmony_ci		for (i = 0; i < 2; i++) {
1348c2ecf20Sopenharmony_ci			data->temp[i] = i2c_smbus_read_byte_data(client,
1358c2ecf20Sopenharmony_ci					ADM1025_REG_TEMP(i));
1368c2ecf20Sopenharmony_ci			data->temp_min[i] = i2c_smbus_read_byte_data(client,
1378c2ecf20Sopenharmony_ci					    ADM1025_REG_TEMP_LOW(i));
1388c2ecf20Sopenharmony_ci			data->temp_max[i] = i2c_smbus_read_byte_data(client,
1398c2ecf20Sopenharmony_ci					    ADM1025_REG_TEMP_HIGH(i));
1408c2ecf20Sopenharmony_ci		}
1418c2ecf20Sopenharmony_ci		data->alarms = i2c_smbus_read_byte_data(client,
1428c2ecf20Sopenharmony_ci			       ADM1025_REG_STATUS1)
1438c2ecf20Sopenharmony_ci			     | (i2c_smbus_read_byte_data(client,
1448c2ecf20Sopenharmony_ci				ADM1025_REG_STATUS2) << 8);
1458c2ecf20Sopenharmony_ci		data->vid = (i2c_smbus_read_byte_data(client,
1468c2ecf20Sopenharmony_ci			     ADM1025_REG_VID) & 0x0f)
1478c2ecf20Sopenharmony_ci			  | ((i2c_smbus_read_byte_data(client,
1488c2ecf20Sopenharmony_ci			      ADM1025_REG_VID4) & 0x01) << 4);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci		data->last_updated = jiffies;
1518c2ecf20Sopenharmony_ci		data->valid = 1;
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	return data;
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/*
1608c2ecf20Sopenharmony_ci * Sysfs stuff
1618c2ecf20Sopenharmony_ci */
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistatic ssize_t
1648c2ecf20Sopenharmony_ciin_show(struct device *dev, struct device_attribute *attr, char *buf)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(attr)->index;
1678c2ecf20Sopenharmony_ci	struct adm1025_data *data = adm1025_update_device(dev);
1688c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[index],
1698c2ecf20Sopenharmony_ci		       in_scale[index]));
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic ssize_t
1738c2ecf20Sopenharmony_ciin_min_show(struct device *dev, struct device_attribute *attr, char *buf)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(attr)->index;
1768c2ecf20Sopenharmony_ci	struct adm1025_data *data = adm1025_update_device(dev);
1778c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[index],
1788c2ecf20Sopenharmony_ci		       in_scale[index]));
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic ssize_t
1828c2ecf20Sopenharmony_ciin_max_show(struct device *dev, struct device_attribute *attr, char *buf)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(attr)->index;
1858c2ecf20Sopenharmony_ci	struct adm1025_data *data = adm1025_update_device(dev);
1868c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[index],
1878c2ecf20Sopenharmony_ci		       in_scale[index]));
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic ssize_t
1918c2ecf20Sopenharmony_citemp_show(struct device *dev, struct device_attribute *attr, char *buf)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(attr)->index;
1948c2ecf20Sopenharmony_ci	struct adm1025_data *data = adm1025_update_device(dev);
1958c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[index]));
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic ssize_t
1998c2ecf20Sopenharmony_citemp_min_show(struct device *dev, struct device_attribute *attr, char *buf)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(attr)->index;
2028c2ecf20Sopenharmony_ci	struct adm1025_data *data = adm1025_update_device(dev);
2038c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[index]));
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic ssize_t
2078c2ecf20Sopenharmony_citemp_max_show(struct device *dev, struct device_attribute *attr, char *buf)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(attr)->index;
2108c2ecf20Sopenharmony_ci	struct adm1025_data *data = adm1025_update_device(dev);
2118c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[index]));
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
2158c2ecf20Sopenharmony_ci			    const char *buf, size_t count)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(attr)->index;
2188c2ecf20Sopenharmony_ci	struct adm1025_data *data = dev_get_drvdata(dev);
2198c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2208c2ecf20Sopenharmony_ci	long val;
2218c2ecf20Sopenharmony_ci	int err;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
2248c2ecf20Sopenharmony_ci	if (err)
2258c2ecf20Sopenharmony_ci		return err;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2288c2ecf20Sopenharmony_ci	data->in_min[index] = IN_TO_REG(val, in_scale[index]);
2298c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MIN(index),
2308c2ecf20Sopenharmony_ci				  data->in_min[index]);
2318c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2328c2ecf20Sopenharmony_ci	return count;
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
2368c2ecf20Sopenharmony_ci			    const char *buf, size_t count)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(attr)->index;
2398c2ecf20Sopenharmony_ci	struct adm1025_data *data = dev_get_drvdata(dev);
2408c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2418c2ecf20Sopenharmony_ci	long val;
2428c2ecf20Sopenharmony_ci	int err;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
2458c2ecf20Sopenharmony_ci	if (err)
2468c2ecf20Sopenharmony_ci		return err;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2498c2ecf20Sopenharmony_ci	data->in_max[index] = IN_TO_REG(val, in_scale[index]);
2508c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MAX(index),
2518c2ecf20Sopenharmony_ci				  data->in_max[index]);
2528c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2538c2ecf20Sopenharmony_ci	return count;
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
2578c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
2588c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
2598c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
2608c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
2618c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
2628c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
2638c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
2648c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
2658c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
2668c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
2678c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
2688c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
2698c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
2708c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
2718c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in5_input, in, 5);
2728c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
2738c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cistatic ssize_t temp_min_store(struct device *dev,
2768c2ecf20Sopenharmony_ci			      struct device_attribute *attr, const char *buf,
2778c2ecf20Sopenharmony_ci			      size_t count)
2788c2ecf20Sopenharmony_ci{
2798c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(attr)->index;
2808c2ecf20Sopenharmony_ci	struct adm1025_data *data = dev_get_drvdata(dev);
2818c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2828c2ecf20Sopenharmony_ci	long val;
2838c2ecf20Sopenharmony_ci	int err;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
2868c2ecf20Sopenharmony_ci	if (err)
2878c2ecf20Sopenharmony_ci		return err;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2908c2ecf20Sopenharmony_ci	data->temp_min[index] = TEMP_TO_REG(val);
2918c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_LOW(index),
2928c2ecf20Sopenharmony_ci				  data->temp_min[index]);
2938c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2948c2ecf20Sopenharmony_ci	return count;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_cistatic ssize_t temp_max_store(struct device *dev,
2988c2ecf20Sopenharmony_ci			      struct device_attribute *attr, const char *buf,
2998c2ecf20Sopenharmony_ci			      size_t count)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(attr)->index;
3028c2ecf20Sopenharmony_ci	struct adm1025_data *data = dev_get_drvdata(dev);
3038c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
3048c2ecf20Sopenharmony_ci	long val;
3058c2ecf20Sopenharmony_ci	int err;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
3088c2ecf20Sopenharmony_ci	if (err)
3098c2ecf20Sopenharmony_ci		return err;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3128c2ecf20Sopenharmony_ci	data->temp_max[index] = TEMP_TO_REG(val);
3138c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_HIGH(index),
3148c2ecf20Sopenharmony_ci				  data->temp_max[index]);
3158c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3168c2ecf20Sopenharmony_ci	return count;
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
3208c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
3218c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
3228c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
3238c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
3248c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_cistatic ssize_t
3278c2ecf20Sopenharmony_cialarms_show(struct device *dev, struct device_attribute *attr, char *buf)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	struct adm1025_data *data = adm1025_update_device(dev);
3308c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", data->alarms);
3318c2ecf20Sopenharmony_ci}
3328c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(alarms);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic ssize_t
3358c2ecf20Sopenharmony_cialarm_show(struct device *dev, struct device_attribute *attr, char *buf)
3368c2ecf20Sopenharmony_ci{
3378c2ecf20Sopenharmony_ci	int bitnr = to_sensor_dev_attr(attr)->index;
3388c2ecf20Sopenharmony_ci	struct adm1025_data *data = adm1025_update_device(dev);
3398c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
3408c2ecf20Sopenharmony_ci}
3418c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
3428c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
3438c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
3448c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
3458c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
3468c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
3478c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 5);
3488c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 4);
3498c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_fault, alarm, 14);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cistatic ssize_t
3528c2ecf20Sopenharmony_cicpu0_vid_show(struct device *dev, struct device_attribute *attr, char *buf)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	struct adm1025_data *data = adm1025_update_device(dev);
3558c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
3568c2ecf20Sopenharmony_ci}
3578c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(cpu0_vid);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_cistatic ssize_t
3608c2ecf20Sopenharmony_civrm_show(struct device *dev, struct device_attribute *attr, char *buf)
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	struct adm1025_data *data = dev_get_drvdata(dev);
3638c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", data->vrm);
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_cistatic ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
3668c2ecf20Sopenharmony_ci			 const char *buf, size_t count)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	struct adm1025_data *data = dev_get_drvdata(dev);
3698c2ecf20Sopenharmony_ci	unsigned long val;
3708c2ecf20Sopenharmony_ci	int err;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
3738c2ecf20Sopenharmony_ci	if (err)
3748c2ecf20Sopenharmony_ci		return err;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	if (val > 255)
3778c2ecf20Sopenharmony_ci		return -EINVAL;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	data->vrm = val;
3808c2ecf20Sopenharmony_ci	return count;
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(vrm);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci/*
3858c2ecf20Sopenharmony_ci * Real code
3868c2ecf20Sopenharmony_ci */
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cistatic struct attribute *adm1025_attributes[] = {
3898c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_input.dev_attr.attr,
3908c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_input.dev_attr.attr,
3918c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_input.dev_attr.attr,
3928c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_input.dev_attr.attr,
3938c2ecf20Sopenharmony_ci	&sensor_dev_attr_in5_input.dev_attr.attr,
3948c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_min.dev_attr.attr,
3958c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_min.dev_attr.attr,
3968c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_min.dev_attr.attr,
3978c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_min.dev_attr.attr,
3988c2ecf20Sopenharmony_ci	&sensor_dev_attr_in5_min.dev_attr.attr,
3998c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_max.dev_attr.attr,
4008c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_max.dev_attr.attr,
4018c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_max.dev_attr.attr,
4028c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_max.dev_attr.attr,
4038c2ecf20Sopenharmony_ci	&sensor_dev_attr_in5_max.dev_attr.attr,
4048c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_alarm.dev_attr.attr,
4058c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_alarm.dev_attr.attr,
4068c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_alarm.dev_attr.attr,
4078c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_alarm.dev_attr.attr,
4088c2ecf20Sopenharmony_ci	&sensor_dev_attr_in5_alarm.dev_attr.attr,
4098c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
4108c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_input.dev_attr.attr,
4118c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min.dev_attr.attr,
4128c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_min.dev_attr.attr,
4138c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max.dev_attr.attr,
4148c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max.dev_attr.attr,
4158c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
4168c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
4178c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_fault.dev_attr.attr,
4188c2ecf20Sopenharmony_ci	&dev_attr_alarms.attr,
4198c2ecf20Sopenharmony_ci	&dev_attr_cpu0_vid.attr,
4208c2ecf20Sopenharmony_ci	&dev_attr_vrm.attr,
4218c2ecf20Sopenharmony_ci	NULL
4228c2ecf20Sopenharmony_ci};
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic const struct attribute_group adm1025_group = {
4258c2ecf20Sopenharmony_ci	.attrs = adm1025_attributes,
4268c2ecf20Sopenharmony_ci};
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_cistatic struct attribute *adm1025_attributes_in4[] = {
4298c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_input.dev_attr.attr,
4308c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_min.dev_attr.attr,
4318c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_max.dev_attr.attr,
4328c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_alarm.dev_attr.attr,
4338c2ecf20Sopenharmony_ci	NULL
4348c2ecf20Sopenharmony_ci};
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_cistatic const struct attribute_group adm1025_group_in4 = {
4378c2ecf20Sopenharmony_ci	.attrs = adm1025_attributes_in4,
4388c2ecf20Sopenharmony_ci};
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci/* Return 0 if detection is successful, -ENODEV otherwise */
4418c2ecf20Sopenharmony_cistatic int adm1025_detect(struct i2c_client *client,
4428c2ecf20Sopenharmony_ci			  struct i2c_board_info *info)
4438c2ecf20Sopenharmony_ci{
4448c2ecf20Sopenharmony_ci	struct i2c_adapter *adapter = client->adapter;
4458c2ecf20Sopenharmony_ci	const char *name;
4468c2ecf20Sopenharmony_ci	u8 man_id, chip_id;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
4498c2ecf20Sopenharmony_ci		return -ENODEV;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	/* Check for unused bits */
4528c2ecf20Sopenharmony_ci	if ((i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG) & 0x80)
4538c2ecf20Sopenharmony_ci	 || (i2c_smbus_read_byte_data(client, ADM1025_REG_STATUS1) & 0xC0)
4548c2ecf20Sopenharmony_ci	 || (i2c_smbus_read_byte_data(client, ADM1025_REG_STATUS2) & 0xBC)) {
4558c2ecf20Sopenharmony_ci		dev_dbg(&adapter->dev, "ADM1025 detection failed at 0x%02x\n",
4568c2ecf20Sopenharmony_ci			client->addr);
4578c2ecf20Sopenharmony_ci		return -ENODEV;
4588c2ecf20Sopenharmony_ci	}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	/* Identification */
4618c2ecf20Sopenharmony_ci	chip_id = i2c_smbus_read_byte_data(client, ADM1025_REG_CHIP_ID);
4628c2ecf20Sopenharmony_ci	if ((chip_id & 0xF0) != 0x20)
4638c2ecf20Sopenharmony_ci		return -ENODEV;
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	man_id = i2c_smbus_read_byte_data(client, ADM1025_REG_MAN_ID);
4668c2ecf20Sopenharmony_ci	if (man_id == 0x41)
4678c2ecf20Sopenharmony_ci		name = "adm1025";
4688c2ecf20Sopenharmony_ci	else if (man_id == 0xA1 && client->addr != 0x2E)
4698c2ecf20Sopenharmony_ci		name = "ne1619";
4708c2ecf20Sopenharmony_ci	else
4718c2ecf20Sopenharmony_ci		return -ENODEV;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	strlcpy(info->type, name, I2C_NAME_SIZE);
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	return 0;
4768c2ecf20Sopenharmony_ci}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_cistatic void adm1025_init_client(struct i2c_client *client)
4798c2ecf20Sopenharmony_ci{
4808c2ecf20Sopenharmony_ci	u8 reg;
4818c2ecf20Sopenharmony_ci	struct adm1025_data *data = i2c_get_clientdata(client);
4828c2ecf20Sopenharmony_ci	int i;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	data->vrm = vid_which_vrm();
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	/*
4878c2ecf20Sopenharmony_ci	 * Set high limits
4888c2ecf20Sopenharmony_ci	 * Usually we avoid setting limits on driver init, but it happens
4898c2ecf20Sopenharmony_ci	 * that the ADM1025 comes with stupid default limits (all registers
4908c2ecf20Sopenharmony_ci	 * set to 0). In case the chip has not gone through any limit
4918c2ecf20Sopenharmony_ci	 * setting yet, we better set the high limits to the max so that
4928c2ecf20Sopenharmony_ci	 * no alarm triggers.
4938c2ecf20Sopenharmony_ci	 */
4948c2ecf20Sopenharmony_ci	for (i = 0; i < 6; i++) {
4958c2ecf20Sopenharmony_ci		reg = i2c_smbus_read_byte_data(client,
4968c2ecf20Sopenharmony_ci					       ADM1025_REG_IN_MAX(i));
4978c2ecf20Sopenharmony_ci		if (reg == 0)
4988c2ecf20Sopenharmony_ci			i2c_smbus_write_byte_data(client,
4998c2ecf20Sopenharmony_ci						  ADM1025_REG_IN_MAX(i),
5008c2ecf20Sopenharmony_ci						  0xFF);
5018c2ecf20Sopenharmony_ci	}
5028c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
5038c2ecf20Sopenharmony_ci		reg = i2c_smbus_read_byte_data(client,
5048c2ecf20Sopenharmony_ci					       ADM1025_REG_TEMP_HIGH(i));
5058c2ecf20Sopenharmony_ci		if (reg == 0)
5068c2ecf20Sopenharmony_ci			i2c_smbus_write_byte_data(client,
5078c2ecf20Sopenharmony_ci						  ADM1025_REG_TEMP_HIGH(i),
5088c2ecf20Sopenharmony_ci						  0x7F);
5098c2ecf20Sopenharmony_ci	}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	/*
5128c2ecf20Sopenharmony_ci	 * Start the conversions
5138c2ecf20Sopenharmony_ci	 */
5148c2ecf20Sopenharmony_ci	reg = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG);
5158c2ecf20Sopenharmony_ci	if (!(reg & 0x01))
5168c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, ADM1025_REG_CONFIG,
5178c2ecf20Sopenharmony_ci					  (reg&0x7E)|0x01);
5188c2ecf20Sopenharmony_ci}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_cistatic int adm1025_probe(struct i2c_client *client)
5218c2ecf20Sopenharmony_ci{
5228c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
5238c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
5248c2ecf20Sopenharmony_ci	struct adm1025_data *data;
5258c2ecf20Sopenharmony_ci	u8 config;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	data = devm_kzalloc(dev, sizeof(struct adm1025_data), GFP_KERNEL);
5288c2ecf20Sopenharmony_ci	if (!data)
5298c2ecf20Sopenharmony_ci		return -ENOMEM;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, data);
5328c2ecf20Sopenharmony_ci	data->client = client;
5338c2ecf20Sopenharmony_ci	mutex_init(&data->update_lock);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	/* Initialize the ADM1025 chip */
5368c2ecf20Sopenharmony_ci	adm1025_init_client(client);
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	/* sysfs hooks */
5398c2ecf20Sopenharmony_ci	data->groups[0] = &adm1025_group;
5408c2ecf20Sopenharmony_ci	/* Pin 11 is either in4 (+12V) or VID4 */
5418c2ecf20Sopenharmony_ci	config = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG);
5428c2ecf20Sopenharmony_ci	if (!(config & 0x20))
5438c2ecf20Sopenharmony_ci		data->groups[1] = &adm1025_group_in4;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
5468c2ecf20Sopenharmony_ci							   data, data->groups);
5478c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(hwmon_dev);
5488c2ecf20Sopenharmony_ci}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_cistatic const struct i2c_device_id adm1025_id[] = {
5518c2ecf20Sopenharmony_ci	{ "adm1025", adm1025 },
5528c2ecf20Sopenharmony_ci	{ "ne1619", ne1619 },
5538c2ecf20Sopenharmony_ci	{ }
5548c2ecf20Sopenharmony_ci};
5558c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, adm1025_id);
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_cistatic struct i2c_driver adm1025_driver = {
5588c2ecf20Sopenharmony_ci	.class		= I2C_CLASS_HWMON,
5598c2ecf20Sopenharmony_ci	.driver = {
5608c2ecf20Sopenharmony_ci		.name	= "adm1025",
5618c2ecf20Sopenharmony_ci	},
5628c2ecf20Sopenharmony_ci	.probe_new	= adm1025_probe,
5638c2ecf20Sopenharmony_ci	.id_table	= adm1025_id,
5648c2ecf20Sopenharmony_ci	.detect		= adm1025_detect,
5658c2ecf20Sopenharmony_ci	.address_list	= normal_i2c,
5668c2ecf20Sopenharmony_ci};
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_cimodule_i2c_driver(adm1025_driver);
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
5718c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ADM1025 driver");
5728c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
573