18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * adm1021.c - Part of lm_sensors, Linux kernel modules for hardware
48c2ecf20Sopenharmony_ci *	       monitoring
58c2ecf20Sopenharmony_ci * Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl> and
68c2ecf20Sopenharmony_ci *			     Philip Edelbrock <phil@netroedge.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
138c2ecf20Sopenharmony_ci#include <linux/i2c.h>
148c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
158c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
168c2ecf20Sopenharmony_ci#include <linux/err.h>
178c2ecf20Sopenharmony_ci#include <linux/mutex.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* Addresses to scan */
218c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = {
228c2ecf20Sopenharmony_ci	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cienum chips {
258c2ecf20Sopenharmony_ci	adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066 };
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* adm1021 constants specified below */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* The adm1021 registers */
308c2ecf20Sopenharmony_ci/* Read-only */
318c2ecf20Sopenharmony_ci/* For nr in 0-1 */
328c2ecf20Sopenharmony_ci#define ADM1021_REG_TEMP(nr)		(nr)
338c2ecf20Sopenharmony_ci#define ADM1021_REG_STATUS		0x02
348c2ecf20Sopenharmony_ci/* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */
358c2ecf20Sopenharmony_ci#define ADM1021_REG_MAN_ID		0xFE
368c2ecf20Sopenharmony_ci/* ADM1021 = 0x0X, ADM1023 = 0x3X */
378c2ecf20Sopenharmony_ci#define ADM1021_REG_DEV_ID		0xFF
388c2ecf20Sopenharmony_ci/* These use different addresses for reading/writing */
398c2ecf20Sopenharmony_ci#define ADM1021_REG_CONFIG_R		0x03
408c2ecf20Sopenharmony_ci#define ADM1021_REG_CONFIG_W		0x09
418c2ecf20Sopenharmony_ci#define ADM1021_REG_CONV_RATE_R		0x04
428c2ecf20Sopenharmony_ci#define ADM1021_REG_CONV_RATE_W		0x0A
438c2ecf20Sopenharmony_ci/* These are for the ADM1023's additional precision on the remote temp sensor */
448c2ecf20Sopenharmony_ci#define ADM1023_REG_REM_TEMP_PREC	0x10
458c2ecf20Sopenharmony_ci#define ADM1023_REG_REM_OFFSET		0x11
468c2ecf20Sopenharmony_ci#define ADM1023_REG_REM_OFFSET_PREC	0x12
478c2ecf20Sopenharmony_ci#define ADM1023_REG_REM_TOS_PREC	0x13
488c2ecf20Sopenharmony_ci#define ADM1023_REG_REM_THYST_PREC	0x14
498c2ecf20Sopenharmony_ci/* limits */
508c2ecf20Sopenharmony_ci/* For nr in 0-1 */
518c2ecf20Sopenharmony_ci#define ADM1021_REG_TOS_R(nr)		(0x05 + 2 * (nr))
528c2ecf20Sopenharmony_ci#define ADM1021_REG_TOS_W(nr)		(0x0B + 2 * (nr))
538c2ecf20Sopenharmony_ci#define ADM1021_REG_THYST_R(nr)		(0x06 + 2 * (nr))
548c2ecf20Sopenharmony_ci#define ADM1021_REG_THYST_W(nr)		(0x0C + 2 * (nr))
558c2ecf20Sopenharmony_ci/* write-only */
568c2ecf20Sopenharmony_ci#define ADM1021_REG_ONESHOT		0x0F
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* Initial values */
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/*
618c2ecf20Sopenharmony_ci * Note: Even though I left the low and high limits named os and hyst,
628c2ecf20Sopenharmony_ci * they don't quite work like a thermostat the way the LM75 does.  I.e.,
638c2ecf20Sopenharmony_ci * a lower temp than THYST actually triggers an alarm instead of
648c2ecf20Sopenharmony_ci * clearing it.  Weird, ey?   --Phil
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/* Each client has this additional data */
688c2ecf20Sopenharmony_cistruct adm1021_data {
698c2ecf20Sopenharmony_ci	struct i2c_client *client;
708c2ecf20Sopenharmony_ci	enum chips type;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	const struct attribute_group *groups[3];
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	struct mutex update_lock;
758c2ecf20Sopenharmony_ci	char valid;		/* !=0 if following fields are valid */
768c2ecf20Sopenharmony_ci	char low_power;		/* !=0 if device in low power mode */
778c2ecf20Sopenharmony_ci	unsigned long last_updated;	/* In jiffies */
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	int temp_max[2];		/* Register values */
808c2ecf20Sopenharmony_ci	int temp_min[2];
818c2ecf20Sopenharmony_ci	int temp[2];
828c2ecf20Sopenharmony_ci	u8 alarms;
838c2ecf20Sopenharmony_ci	/* Special values for ADM1023 only */
848c2ecf20Sopenharmony_ci	u8 remote_temp_offset;
858c2ecf20Sopenharmony_ci	u8 remote_temp_offset_prec;
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
898c2ecf20Sopenharmony_cistatic bool read_only;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic struct adm1021_data *adm1021_update_device(struct device *dev)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	struct adm1021_data *data = dev_get_drvdata(dev);
948c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
998c2ecf20Sopenharmony_ci	    || !data->valid) {
1008c2ecf20Sopenharmony_ci		int i;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci		dev_dbg(dev, "Starting adm1021 update\n");
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci		for (i = 0; i < 2; i++) {
1058c2ecf20Sopenharmony_ci			data->temp[i] = 1000 *
1068c2ecf20Sopenharmony_ci				(s8) i2c_smbus_read_byte_data(
1078c2ecf20Sopenharmony_ci					client, ADM1021_REG_TEMP(i));
1088c2ecf20Sopenharmony_ci			data->temp_max[i] = 1000 *
1098c2ecf20Sopenharmony_ci				(s8) i2c_smbus_read_byte_data(
1108c2ecf20Sopenharmony_ci					client, ADM1021_REG_TOS_R(i));
1118c2ecf20Sopenharmony_ci			if (data->type != lm84) {
1128c2ecf20Sopenharmony_ci				data->temp_min[i] = 1000 *
1138c2ecf20Sopenharmony_ci				  (s8) i2c_smbus_read_byte_data(client,
1148c2ecf20Sopenharmony_ci							ADM1021_REG_THYST_R(i));
1158c2ecf20Sopenharmony_ci			}
1168c2ecf20Sopenharmony_ci		}
1178c2ecf20Sopenharmony_ci		data->alarms = i2c_smbus_read_byte_data(client,
1188c2ecf20Sopenharmony_ci						ADM1021_REG_STATUS) & 0x7c;
1198c2ecf20Sopenharmony_ci		if (data->type == adm1023) {
1208c2ecf20Sopenharmony_ci			/*
1218c2ecf20Sopenharmony_ci			 * The ADM1023 provides 3 extra bits of precision for
1228c2ecf20Sopenharmony_ci			 * the remote sensor in extra registers.
1238c2ecf20Sopenharmony_ci			 */
1248c2ecf20Sopenharmony_ci			data->temp[1] += 125 * (i2c_smbus_read_byte_data(
1258c2ecf20Sopenharmony_ci				client, ADM1023_REG_REM_TEMP_PREC) >> 5);
1268c2ecf20Sopenharmony_ci			data->temp_max[1] += 125 * (i2c_smbus_read_byte_data(
1278c2ecf20Sopenharmony_ci				client, ADM1023_REG_REM_TOS_PREC) >> 5);
1288c2ecf20Sopenharmony_ci			data->temp_min[1] += 125 * (i2c_smbus_read_byte_data(
1298c2ecf20Sopenharmony_ci				client, ADM1023_REG_REM_THYST_PREC) >> 5);
1308c2ecf20Sopenharmony_ci			data->remote_temp_offset =
1318c2ecf20Sopenharmony_ci				i2c_smbus_read_byte_data(client,
1328c2ecf20Sopenharmony_ci						ADM1023_REG_REM_OFFSET);
1338c2ecf20Sopenharmony_ci			data->remote_temp_offset_prec =
1348c2ecf20Sopenharmony_ci				i2c_smbus_read_byte_data(client,
1358c2ecf20Sopenharmony_ci						ADM1023_REG_REM_OFFSET_PREC);
1368c2ecf20Sopenharmony_ci		}
1378c2ecf20Sopenharmony_ci		data->last_updated = jiffies;
1388c2ecf20Sopenharmony_ci		data->valid = 1;
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	return data;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
1478c2ecf20Sopenharmony_ci			 char *buf)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(devattr)->index;
1508c2ecf20Sopenharmony_ci	struct adm1021_data *data = adm1021_update_device(dev);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->temp[index]);
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic ssize_t temp_max_show(struct device *dev,
1568c2ecf20Sopenharmony_ci			     struct device_attribute *devattr, char *buf)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(devattr)->index;
1598c2ecf20Sopenharmony_ci	struct adm1021_data *data = adm1021_update_device(dev);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->temp_max[index]);
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic ssize_t temp_min_show(struct device *dev,
1658c2ecf20Sopenharmony_ci			     struct device_attribute *devattr, char *buf)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(devattr)->index;
1688c2ecf20Sopenharmony_ci	struct adm1021_data *data = adm1021_update_device(dev);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->temp_min[index]);
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
1748c2ecf20Sopenharmony_ci			  char *buf)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(attr)->index;
1778c2ecf20Sopenharmony_ci	struct adm1021_data *data = adm1021_update_device(dev);
1788c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic ssize_t alarms_show(struct device *dev,
1828c2ecf20Sopenharmony_ci			   struct device_attribute *attr,
1838c2ecf20Sopenharmony_ci			   char *buf)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	struct adm1021_data *data = adm1021_update_device(dev);
1868c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", data->alarms);
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic ssize_t temp_max_store(struct device *dev,
1908c2ecf20Sopenharmony_ci			      struct device_attribute *devattr,
1918c2ecf20Sopenharmony_ci			      const char *buf, size_t count)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(devattr)->index;
1948c2ecf20Sopenharmony_ci	struct adm1021_data *data = dev_get_drvdata(dev);
1958c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
1968c2ecf20Sopenharmony_ci	long temp;
1978c2ecf20Sopenharmony_ci	int reg_val, err;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &temp);
2008c2ecf20Sopenharmony_ci	if (err)
2018c2ecf20Sopenharmony_ci		return err;
2028c2ecf20Sopenharmony_ci	temp /= 1000;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2058c2ecf20Sopenharmony_ci	reg_val = clamp_val(temp, -128, 127);
2068c2ecf20Sopenharmony_ci	data->temp_max[index] = reg_val * 1000;
2078c2ecf20Sopenharmony_ci	if (!read_only)
2088c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
2098c2ecf20Sopenharmony_ci					  reg_val);
2108c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return count;
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic ssize_t temp_min_store(struct device *dev,
2168c2ecf20Sopenharmony_ci			      struct device_attribute *devattr,
2178c2ecf20Sopenharmony_ci			      const char *buf, size_t count)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	int index = to_sensor_dev_attr(devattr)->index;
2208c2ecf20Sopenharmony_ci	struct adm1021_data *data = dev_get_drvdata(dev);
2218c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2228c2ecf20Sopenharmony_ci	long temp;
2238c2ecf20Sopenharmony_ci	int reg_val, err;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &temp);
2268c2ecf20Sopenharmony_ci	if (err)
2278c2ecf20Sopenharmony_ci		return err;
2288c2ecf20Sopenharmony_ci	temp /= 1000;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2318c2ecf20Sopenharmony_ci	reg_val = clamp_val(temp, -128, 127);
2328c2ecf20Sopenharmony_ci	data->temp_min[index] = reg_val * 1000;
2338c2ecf20Sopenharmony_ci	if (!read_only)
2348c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
2358c2ecf20Sopenharmony_ci					  reg_val);
2368c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	return count;
2398c2ecf20Sopenharmony_ci}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic ssize_t low_power_show(struct device *dev,
2428c2ecf20Sopenharmony_ci			      struct device_attribute *devattr, char *buf)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	struct adm1021_data *data = adm1021_update_device(dev);
2458c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->low_power);
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic ssize_t low_power_store(struct device *dev,
2498c2ecf20Sopenharmony_ci			       struct device_attribute *devattr,
2508c2ecf20Sopenharmony_ci			       const char *buf, size_t count)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	struct adm1021_data *data = dev_get_drvdata(dev);
2538c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2548c2ecf20Sopenharmony_ci	char low_power;
2558c2ecf20Sopenharmony_ci	unsigned long val;
2568c2ecf20Sopenharmony_ci	int err;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
2598c2ecf20Sopenharmony_ci	if (err)
2608c2ecf20Sopenharmony_ci		return err;
2618c2ecf20Sopenharmony_ci	low_power = val != 0;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2648c2ecf20Sopenharmony_ci	if (low_power != data->low_power) {
2658c2ecf20Sopenharmony_ci		int config = i2c_smbus_read_byte_data(
2668c2ecf20Sopenharmony_ci			client, ADM1021_REG_CONFIG_R);
2678c2ecf20Sopenharmony_ci		data->low_power = low_power;
2688c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
2698c2ecf20Sopenharmony_ci			(config & 0xBF) | (low_power << 6));
2708c2ecf20Sopenharmony_ci	}
2718c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	return count;
2748c2ecf20Sopenharmony_ci}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
2788c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
2798c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
2808c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
2818c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
2828c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
2838c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 6);
2848c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 5);
2858c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 4);
2868c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, alarm, 3);
2878c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 2);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(alarms);
2908c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(low_power);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic struct attribute *adm1021_attributes[] = {
2938c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max.dev_attr.attr,
2948c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
2958c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max.dev_attr.attr,
2968c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_input.dev_attr.attr,
2978c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
2988c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
2998c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_fault.dev_attr.attr,
3008c2ecf20Sopenharmony_ci	&dev_attr_alarms.attr,
3018c2ecf20Sopenharmony_ci	&dev_attr_low_power.attr,
3028c2ecf20Sopenharmony_ci	NULL
3038c2ecf20Sopenharmony_ci};
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cistatic const struct attribute_group adm1021_group = {
3068c2ecf20Sopenharmony_ci	.attrs = adm1021_attributes,
3078c2ecf20Sopenharmony_ci};
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_cistatic struct attribute *adm1021_min_attributes[] = {
3108c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min.dev_attr.attr,
3118c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_min.dev_attr.attr,
3128c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
3138c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
3148c2ecf20Sopenharmony_ci	NULL
3158c2ecf20Sopenharmony_ci};
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic const struct attribute_group adm1021_min_group = {
3188c2ecf20Sopenharmony_ci	.attrs = adm1021_min_attributes,
3198c2ecf20Sopenharmony_ci};
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci/* Return 0 if detection is successful, -ENODEV otherwise */
3228c2ecf20Sopenharmony_cistatic int adm1021_detect(struct i2c_client *client,
3238c2ecf20Sopenharmony_ci			  struct i2c_board_info *info)
3248c2ecf20Sopenharmony_ci{
3258c2ecf20Sopenharmony_ci	struct i2c_adapter *adapter = client->adapter;
3268c2ecf20Sopenharmony_ci	const char *type_name;
3278c2ecf20Sopenharmony_ci	int conv_rate, status, config, man_id, dev_id;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
3308c2ecf20Sopenharmony_ci		pr_debug("detect failed, smbus byte data not supported!\n");
3318c2ecf20Sopenharmony_ci		return -ENODEV;
3328c2ecf20Sopenharmony_ci	}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
3358c2ecf20Sopenharmony_ci	conv_rate = i2c_smbus_read_byte_data(client,
3368c2ecf20Sopenharmony_ci					     ADM1021_REG_CONV_RATE_R);
3378c2ecf20Sopenharmony_ci	config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	/* Check unused bits */
3408c2ecf20Sopenharmony_ci	if ((status & 0x03) || (config & 0x3F) || (conv_rate & 0xF8)) {
3418c2ecf20Sopenharmony_ci		pr_debug("detect failed, chip not detected!\n");
3428c2ecf20Sopenharmony_ci		return -ENODEV;
3438c2ecf20Sopenharmony_ci	}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	/* Determine the chip type. */
3468c2ecf20Sopenharmony_ci	man_id = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
3478c2ecf20Sopenharmony_ci	dev_id = i2c_smbus_read_byte_data(client, ADM1021_REG_DEV_ID);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	if (man_id < 0 || dev_id < 0)
3508c2ecf20Sopenharmony_ci		return -ENODEV;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	if (man_id == 0x4d && dev_id == 0x01)
3538c2ecf20Sopenharmony_ci		type_name = "max1617a";
3548c2ecf20Sopenharmony_ci	else if (man_id == 0x41) {
3558c2ecf20Sopenharmony_ci		if ((dev_id & 0xF0) == 0x30)
3568c2ecf20Sopenharmony_ci			type_name = "adm1023";
3578c2ecf20Sopenharmony_ci		else if ((dev_id & 0xF0) == 0x00)
3588c2ecf20Sopenharmony_ci			type_name = "adm1021";
3598c2ecf20Sopenharmony_ci		else
3608c2ecf20Sopenharmony_ci			return -ENODEV;
3618c2ecf20Sopenharmony_ci	} else if (man_id == 0x49)
3628c2ecf20Sopenharmony_ci		type_name = "thmc10";
3638c2ecf20Sopenharmony_ci	else if (man_id == 0x23)
3648c2ecf20Sopenharmony_ci		type_name = "gl523sm";
3658c2ecf20Sopenharmony_ci	else if (man_id == 0x54)
3668c2ecf20Sopenharmony_ci		type_name = "mc1066";
3678c2ecf20Sopenharmony_ci	else {
3688c2ecf20Sopenharmony_ci		int lte, rte, lhi, rhi, llo, rlo;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci		/* extra checks for LM84 and MAX1617 to avoid misdetections */
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci		llo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(0));
3738c2ecf20Sopenharmony_ci		rlo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(1));
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci		/* fail if any of the additional register reads failed */
3768c2ecf20Sopenharmony_ci		if (llo < 0 || rlo < 0)
3778c2ecf20Sopenharmony_ci			return -ENODEV;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci		lte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(0));
3808c2ecf20Sopenharmony_ci		rte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(1));
3818c2ecf20Sopenharmony_ci		lhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(0));
3828c2ecf20Sopenharmony_ci		rhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(1));
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci		/*
3858c2ecf20Sopenharmony_ci		 * Fail for negative temperatures and negative high limits.
3868c2ecf20Sopenharmony_ci		 * This check also catches read errors on the tested registers.
3878c2ecf20Sopenharmony_ci		 */
3888c2ecf20Sopenharmony_ci		if ((s8)lte < 0 || (s8)rte < 0 || (s8)lhi < 0 || (s8)rhi < 0)
3898c2ecf20Sopenharmony_ci			return -ENODEV;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci		/* fail if all registers hold the same value */
3928c2ecf20Sopenharmony_ci		if (lte == rte && lte == lhi && lte == rhi && lte == llo
3938c2ecf20Sopenharmony_ci		    && lte == rlo)
3948c2ecf20Sopenharmony_ci			return -ENODEV;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci		/*
3978c2ecf20Sopenharmony_ci		 * LM84 Mfr ID is in a different place,
3988c2ecf20Sopenharmony_ci		 * and it has more unused bits.
3998c2ecf20Sopenharmony_ci		 */
4008c2ecf20Sopenharmony_ci		if (conv_rate == 0x00
4018c2ecf20Sopenharmony_ci		    && (config & 0x7F) == 0x00
4028c2ecf20Sopenharmony_ci		    && (status & 0xAB) == 0x00) {
4038c2ecf20Sopenharmony_ci			type_name = "lm84";
4048c2ecf20Sopenharmony_ci		} else {
4058c2ecf20Sopenharmony_ci			/* fail if low limits are larger than high limits */
4068c2ecf20Sopenharmony_ci			if ((s8)llo > lhi || (s8)rlo > rhi)
4078c2ecf20Sopenharmony_ci				return -ENODEV;
4088c2ecf20Sopenharmony_ci			type_name = "max1617";
4098c2ecf20Sopenharmony_ci		}
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	pr_debug("Detected chip %s at adapter %d, address 0x%02x.\n",
4138c2ecf20Sopenharmony_ci		 type_name, i2c_adapter_id(adapter), client->addr);
4148c2ecf20Sopenharmony_ci	strlcpy(info->type, type_name, I2C_NAME_SIZE);
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	return 0;
4178c2ecf20Sopenharmony_ci}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cistatic void adm1021_init_client(struct i2c_client *client)
4208c2ecf20Sopenharmony_ci{
4218c2ecf20Sopenharmony_ci	/* Enable ADC and disable suspend mode */
4228c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
4238c2ecf20Sopenharmony_ci		i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
4248c2ecf20Sopenharmony_ci	/* Set Conversion rate to 1/sec (this can be tinkered with) */
4258c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
4268c2ecf20Sopenharmony_ci}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_cistatic const struct i2c_device_id adm1021_id[];
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_cistatic int adm1021_probe(struct i2c_client *client)
4318c2ecf20Sopenharmony_ci{
4328c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
4338c2ecf20Sopenharmony_ci	struct adm1021_data *data;
4348c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	data = devm_kzalloc(dev, sizeof(struct adm1021_data), GFP_KERNEL);
4378c2ecf20Sopenharmony_ci	if (!data)
4388c2ecf20Sopenharmony_ci		return -ENOMEM;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	data->client = client;
4418c2ecf20Sopenharmony_ci	data->type = i2c_match_id(adm1021_id, client)->driver_data;
4428c2ecf20Sopenharmony_ci	mutex_init(&data->update_lock);
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	/* Initialize the ADM1021 chip */
4458c2ecf20Sopenharmony_ci	if (data->type != lm84 && !read_only)
4468c2ecf20Sopenharmony_ci		adm1021_init_client(client);
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	data->groups[0] = &adm1021_group;
4498c2ecf20Sopenharmony_ci	if (data->type != lm84)
4508c2ecf20Sopenharmony_ci		data->groups[1] = &adm1021_min_group;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
4538c2ecf20Sopenharmony_ci							   data, data->groups);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(hwmon_dev);
4568c2ecf20Sopenharmony_ci}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_cistatic const struct i2c_device_id adm1021_id[] = {
4598c2ecf20Sopenharmony_ci	{ "adm1021", adm1021 },
4608c2ecf20Sopenharmony_ci	{ "adm1023", adm1023 },
4618c2ecf20Sopenharmony_ci	{ "max1617", max1617 },
4628c2ecf20Sopenharmony_ci	{ "max1617a", max1617a },
4638c2ecf20Sopenharmony_ci	{ "thmc10", thmc10 },
4648c2ecf20Sopenharmony_ci	{ "lm84", lm84 },
4658c2ecf20Sopenharmony_ci	{ "gl523sm", gl523sm },
4668c2ecf20Sopenharmony_ci	{ "mc1066", mc1066 },
4678c2ecf20Sopenharmony_ci	{ }
4688c2ecf20Sopenharmony_ci};
4698c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, adm1021_id);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_cistatic struct i2c_driver adm1021_driver = {
4728c2ecf20Sopenharmony_ci	.class		= I2C_CLASS_HWMON,
4738c2ecf20Sopenharmony_ci	.driver = {
4748c2ecf20Sopenharmony_ci		.name	= "adm1021",
4758c2ecf20Sopenharmony_ci	},
4768c2ecf20Sopenharmony_ci	.probe_new	= adm1021_probe,
4778c2ecf20Sopenharmony_ci	.id_table	= adm1021_id,
4788c2ecf20Sopenharmony_ci	.detect		= adm1021_detect,
4798c2ecf20Sopenharmony_ci	.address_list	= normal_i2c,
4808c2ecf20Sopenharmony_ci};
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_cimodule_i2c_driver(adm1021_driver);
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ciMODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
4858c2ecf20Sopenharmony_ci		"Philip Edelbrock <phil@netroedge.com>");
4868c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("adm1021 driver");
4878c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_cimodule_param(read_only, bool, 0);
4908c2ecf20Sopenharmony_ciMODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
491