18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * smsc47m192.c - Support for hardware monitoring block of
48c2ecf20Sopenharmony_ci *		  SMSC LPC47M192 and compatible Super I/O chips
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2006  Hartmut Rick <linux@rick.claranet.de>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Derived from lm78.c and other chip drivers.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
158c2ecf20Sopenharmony_ci#include <linux/i2c.h>
168c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
178c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
188c2ecf20Sopenharmony_ci#include <linux/hwmon-vid.h>
198c2ecf20Sopenharmony_ci#include <linux/err.h>
208c2ecf20Sopenharmony_ci#include <linux/sysfs.h>
218c2ecf20Sopenharmony_ci#include <linux/mutex.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* Addresses to scan */
248c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* SMSC47M192 registers */
278c2ecf20Sopenharmony_ci#define SMSC47M192_REG_IN(nr)		((nr) < 6 ? (0x20 + (nr)) : \
288c2ecf20Sopenharmony_ci					(0x50 + (nr) - 6))
298c2ecf20Sopenharmony_ci#define SMSC47M192_REG_IN_MAX(nr)	((nr) < 6 ? (0x2b + (nr) * 2) : \
308c2ecf20Sopenharmony_ci					(0x54 + (((nr) - 6) * 2)))
318c2ecf20Sopenharmony_ci#define SMSC47M192_REG_IN_MIN(nr)	((nr) < 6 ? (0x2c + (nr) * 2) : \
328c2ecf20Sopenharmony_ci					(0x55 + (((nr) - 6) * 2)))
338c2ecf20Sopenharmony_cistatic u8 SMSC47M192_REG_TEMP[3] =	{ 0x27, 0x26, 0x52 };
348c2ecf20Sopenharmony_cistatic u8 SMSC47M192_REG_TEMP_MAX[3] =	{ 0x39, 0x37, 0x58 };
358c2ecf20Sopenharmony_cistatic u8 SMSC47M192_REG_TEMP_MIN[3] =	{ 0x3A, 0x38, 0x59 };
368c2ecf20Sopenharmony_ci#define SMSC47M192_REG_TEMP_OFFSET(nr)	((nr) == 2 ? 0x1e : 0x1f)
378c2ecf20Sopenharmony_ci#define SMSC47M192_REG_ALARM1		0x41
388c2ecf20Sopenharmony_ci#define SMSC47M192_REG_ALARM2		0x42
398c2ecf20Sopenharmony_ci#define SMSC47M192_REG_VID		0x47
408c2ecf20Sopenharmony_ci#define SMSC47M192_REG_VID4		0x49
418c2ecf20Sopenharmony_ci#define SMSC47M192_REG_CONFIG		0x40
428c2ecf20Sopenharmony_ci#define SMSC47M192_REG_SFR		0x4f
438c2ecf20Sopenharmony_ci#define SMSC47M192_REG_COMPANY_ID	0x3e
448c2ecf20Sopenharmony_ci#define SMSC47M192_REG_VERSION		0x3f
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* generalised scaling with integer rounding */
478c2ecf20Sopenharmony_cistatic inline int SCALE(long val, int mul, int div)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	if (val < 0)
508c2ecf20Sopenharmony_ci		return (val * mul - div / 2) / div;
518c2ecf20Sopenharmony_ci	else
528c2ecf20Sopenharmony_ci		return (val * mul + div / 2) / div;
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* Conversions */
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/* smsc47m192 internally scales voltage measurements */
588c2ecf20Sopenharmony_cistatic const u16 nom_mv[] = { 2500, 2250, 3300, 5000, 12000, 3300, 1500, 1800 };
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic inline unsigned int IN_FROM_REG(u8 reg, int n)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	return SCALE(reg, nom_mv[n], 192);
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic inline u8 IN_TO_REG(unsigned long val, int n)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	val = clamp_val(val, 0, nom_mv[n] * 255 / 192);
688c2ecf20Sopenharmony_ci	return SCALE(val, 192, nom_mv[n]);
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/*
728c2ecf20Sopenharmony_ci * TEMP: 0.001 degC units (-128C to +127C)
738c2ecf20Sopenharmony_ci * REG: 1C/bit, two's complement
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_cistatic inline s8 TEMP_TO_REG(long val)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	return SCALE(clamp_val(val, -128000, 127000), 1, 1000);
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic inline int TEMP_FROM_REG(s8 val)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	return val * 1000;
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistruct smsc47m192_data {
868c2ecf20Sopenharmony_ci	struct i2c_client *client;
878c2ecf20Sopenharmony_ci	const struct attribute_group *groups[3];
888c2ecf20Sopenharmony_ci	struct mutex update_lock;
898c2ecf20Sopenharmony_ci	char valid;		/* !=0 if following fields are valid */
908c2ecf20Sopenharmony_ci	unsigned long last_updated;	/* In jiffies */
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	u8 in[8];		/* Register value */
938c2ecf20Sopenharmony_ci	u8 in_max[8];		/* Register value */
948c2ecf20Sopenharmony_ci	u8 in_min[8];		/* Register value */
958c2ecf20Sopenharmony_ci	s8 temp[3];		/* Register value */
968c2ecf20Sopenharmony_ci	s8 temp_max[3];		/* Register value */
978c2ecf20Sopenharmony_ci	s8 temp_min[3];		/* Register value */
988c2ecf20Sopenharmony_ci	s8 temp_offset[3];	/* Register value */
998c2ecf20Sopenharmony_ci	u16 alarms;		/* Register encoding, combined */
1008c2ecf20Sopenharmony_ci	u8 vid;			/* Register encoding, combined */
1018c2ecf20Sopenharmony_ci	u8 vrm;
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic struct smsc47m192_data *smsc47m192_update_device(struct device *dev)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = dev_get_drvdata(dev);
1078c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
1088c2ecf20Sopenharmony_ci	int i, config;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1138c2ecf20Sopenharmony_ci	 || !data->valid) {
1148c2ecf20Sopenharmony_ci		u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "Starting smsc47m192 update\n");
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci		for (i = 0; i <= 7; i++) {
1198c2ecf20Sopenharmony_ci			data->in[i] = i2c_smbus_read_byte_data(client,
1208c2ecf20Sopenharmony_ci						SMSC47M192_REG_IN(i));
1218c2ecf20Sopenharmony_ci			data->in_min[i] = i2c_smbus_read_byte_data(client,
1228c2ecf20Sopenharmony_ci						SMSC47M192_REG_IN_MIN(i));
1238c2ecf20Sopenharmony_ci			data->in_max[i] = i2c_smbus_read_byte_data(client,
1248c2ecf20Sopenharmony_ci						SMSC47M192_REG_IN_MAX(i));
1258c2ecf20Sopenharmony_ci		}
1268c2ecf20Sopenharmony_ci		for (i = 0; i < 3; i++) {
1278c2ecf20Sopenharmony_ci			data->temp[i] = i2c_smbus_read_byte_data(client,
1288c2ecf20Sopenharmony_ci						SMSC47M192_REG_TEMP[i]);
1298c2ecf20Sopenharmony_ci			data->temp_max[i] = i2c_smbus_read_byte_data(client,
1308c2ecf20Sopenharmony_ci						SMSC47M192_REG_TEMP_MAX[i]);
1318c2ecf20Sopenharmony_ci			data->temp_min[i] = i2c_smbus_read_byte_data(client,
1328c2ecf20Sopenharmony_ci						SMSC47M192_REG_TEMP_MIN[i]);
1338c2ecf20Sopenharmony_ci		}
1348c2ecf20Sopenharmony_ci		for (i = 1; i < 3; i++)
1358c2ecf20Sopenharmony_ci			data->temp_offset[i] = i2c_smbus_read_byte_data(client,
1368c2ecf20Sopenharmony_ci						SMSC47M192_REG_TEMP_OFFSET(i));
1378c2ecf20Sopenharmony_ci		/*
1388c2ecf20Sopenharmony_ci		 * first offset is temp_offset[0] if SFR bit 4 is set,
1398c2ecf20Sopenharmony_ci		 * temp_offset[1] otherwise
1408c2ecf20Sopenharmony_ci		 */
1418c2ecf20Sopenharmony_ci		if (sfr & 0x10) {
1428c2ecf20Sopenharmony_ci			data->temp_offset[0] = data->temp_offset[1];
1438c2ecf20Sopenharmony_ci			data->temp_offset[1] = 0;
1448c2ecf20Sopenharmony_ci		} else
1458c2ecf20Sopenharmony_ci			data->temp_offset[0] = 0;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci		data->vid = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VID)
1488c2ecf20Sopenharmony_ci			    & 0x0f;
1498c2ecf20Sopenharmony_ci		config = i2c_smbus_read_byte_data(client,
1508c2ecf20Sopenharmony_ci						  SMSC47M192_REG_CONFIG);
1518c2ecf20Sopenharmony_ci		if (config & 0x20)
1528c2ecf20Sopenharmony_ci			data->vid |= (i2c_smbus_read_byte_data(client,
1538c2ecf20Sopenharmony_ci					SMSC47M192_REG_VID4) & 0x01) << 4;
1548c2ecf20Sopenharmony_ci		data->alarms = i2c_smbus_read_byte_data(client,
1558c2ecf20Sopenharmony_ci						SMSC47M192_REG_ALARM1) |
1568c2ecf20Sopenharmony_ci			       (i2c_smbus_read_byte_data(client,
1578c2ecf20Sopenharmony_ci						SMSC47M192_REG_ALARM2) << 8);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci		data->last_updated = jiffies;
1608c2ecf20Sopenharmony_ci		data->valid = 1;
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	return data;
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/* Voltages */
1698c2ecf20Sopenharmony_cistatic ssize_t in_show(struct device *dev, struct device_attribute *attr,
1708c2ecf20Sopenharmony_ci		       char *buf)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1738c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
1748c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = smsc47m192_update_device(dev);
1758c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr], nr));
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
1798c2ecf20Sopenharmony_ci			   char *buf)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1828c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
1838c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = smsc47m192_update_device(dev);
1848c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr], nr));
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
1888c2ecf20Sopenharmony_ci			   char *buf)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1918c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
1928c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = smsc47m192_update_device(dev);
1938c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr], nr));
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
1978c2ecf20Sopenharmony_ci			    const char *buf, size_t count)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2008c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
2018c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = dev_get_drvdata(dev);
2028c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2038c2ecf20Sopenharmony_ci	unsigned long val;
2048c2ecf20Sopenharmony_ci	int err;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
2078c2ecf20Sopenharmony_ci	if (err)
2088c2ecf20Sopenharmony_ci		return err;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2118c2ecf20Sopenharmony_ci	data->in_min[nr] = IN_TO_REG(val, nr);
2128c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MIN(nr),
2138c2ecf20Sopenharmony_ci							data->in_min[nr]);
2148c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2158c2ecf20Sopenharmony_ci	return count;
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
2198c2ecf20Sopenharmony_ci			    const char *buf, size_t count)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2228c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
2238c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = dev_get_drvdata(dev);
2248c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
2258c2ecf20Sopenharmony_ci	unsigned long val;
2268c2ecf20Sopenharmony_ci	int err;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
2298c2ecf20Sopenharmony_ci	if (err)
2308c2ecf20Sopenharmony_ci		return err;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2338c2ecf20Sopenharmony_ci	data->in_max[nr] = IN_TO_REG(val, nr);
2348c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MAX(nr),
2358c2ecf20Sopenharmony_ci							data->in_max[nr]);
2368c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2378c2ecf20Sopenharmony_ci	return count;
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
2418c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
2428c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
2438c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
2448c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
2458c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
2468c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
2478c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
2488c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
2498c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
2508c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
2518c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
2528c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
2538c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
2548c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
2558c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in5_input, in, 5);
2568c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
2578c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
2588c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in6_input, in, 6);
2598c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 6);
2608c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 6);
2618c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in7_input, in, 7);
2628c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in7_min, in_min, 7);
2638c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in7_max, in_max, 7);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci/* Temperatures */
2668c2ecf20Sopenharmony_cistatic ssize_t temp_show(struct device *dev, struct device_attribute *attr,
2678c2ecf20Sopenharmony_ci			 char *buf)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2708c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
2718c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = smsc47m192_update_device(dev);
2728c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cistatic ssize_t temp_min_show(struct device *dev,
2768c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2798c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
2808c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = smsc47m192_update_device(dev);
2818c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic ssize_t temp_max_show(struct device *dev,
2858c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
2868c2ecf20Sopenharmony_ci{
2878c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2888c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
2898c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = smsc47m192_update_device(dev);
2908c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic ssize_t temp_min_store(struct device *dev,
2948c2ecf20Sopenharmony_ci			      struct device_attribute *attr, const char *buf,
2958c2ecf20Sopenharmony_ci			      size_t count)
2968c2ecf20Sopenharmony_ci{
2978c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2988c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
2998c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = dev_get_drvdata(dev);
3008c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
3018c2ecf20Sopenharmony_ci	long val;
3028c2ecf20Sopenharmony_ci	int err;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
3058c2ecf20Sopenharmony_ci	if (err)
3068c2ecf20Sopenharmony_ci		return err;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3098c2ecf20Sopenharmony_ci	data->temp_min[nr] = TEMP_TO_REG(val);
3108c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MIN[nr],
3118c2ecf20Sopenharmony_ci						data->temp_min[nr]);
3128c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3138c2ecf20Sopenharmony_ci	return count;
3148c2ecf20Sopenharmony_ci}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_cistatic ssize_t temp_max_store(struct device *dev,
3178c2ecf20Sopenharmony_ci			      struct device_attribute *attr, const char *buf,
3188c2ecf20Sopenharmony_ci			      size_t count)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
3218c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
3228c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = dev_get_drvdata(dev);
3238c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
3248c2ecf20Sopenharmony_ci	long val;
3258c2ecf20Sopenharmony_ci	int err;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
3288c2ecf20Sopenharmony_ci	if (err)
3298c2ecf20Sopenharmony_ci		return err;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3328c2ecf20Sopenharmony_ci	data->temp_max[nr] = TEMP_TO_REG(val);
3338c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MAX[nr],
3348c2ecf20Sopenharmony_ci						data->temp_max[nr]);
3358c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3368c2ecf20Sopenharmony_ci	return count;
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic ssize_t temp_offset_show(struct device *dev,
3408c2ecf20Sopenharmony_ci				struct device_attribute *attr, char *buf)
3418c2ecf20Sopenharmony_ci{
3428c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
3438c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
3448c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = smsc47m192_update_device(dev);
3458c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cistatic ssize_t temp_offset_store(struct device *dev,
3498c2ecf20Sopenharmony_ci				 struct device_attribute *attr,
3508c2ecf20Sopenharmony_ci				 const char *buf, size_t count)
3518c2ecf20Sopenharmony_ci{
3528c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
3538c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
3548c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = dev_get_drvdata(dev);
3558c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
3568c2ecf20Sopenharmony_ci	u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
3578c2ecf20Sopenharmony_ci	long val;
3588c2ecf20Sopenharmony_ci	int err;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
3618c2ecf20Sopenharmony_ci	if (err)
3628c2ecf20Sopenharmony_ci		return err;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3658c2ecf20Sopenharmony_ci	data->temp_offset[nr] = TEMP_TO_REG(val);
3668c2ecf20Sopenharmony_ci	if (nr > 1)
3678c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client,
3688c2ecf20Sopenharmony_ci			SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
3698c2ecf20Sopenharmony_ci	else if (data->temp_offset[nr] != 0) {
3708c2ecf20Sopenharmony_ci		/*
3718c2ecf20Sopenharmony_ci		 * offset[0] and offset[1] share the same register,
3728c2ecf20Sopenharmony_ci		 * SFR bit 4 activates offset[0]
3738c2ecf20Sopenharmony_ci		 */
3748c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
3758c2ecf20Sopenharmony_ci					(sfr & 0xef) | (nr == 0 ? 0x10 : 0));
3768c2ecf20Sopenharmony_ci		data->temp_offset[1-nr] = 0;
3778c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client,
3788c2ecf20Sopenharmony_ci			SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
3798c2ecf20Sopenharmony_ci	} else if ((sfr & 0x10) == (nr == 0 ? 0x10 : 0))
3808c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client,
3818c2ecf20Sopenharmony_ci					SMSC47M192_REG_TEMP_OFFSET(nr), 0);
3828c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3838c2ecf20Sopenharmony_ci	return count;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
3878c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
3888c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
3898c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_offset, temp_offset, 0);
3908c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
3918c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
3928c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
3938c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_offset, temp_offset, 1);
3948c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
3958c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
3968c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
3978c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_offset, temp_offset, 2);
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci/* VID */
4008c2ecf20Sopenharmony_cistatic ssize_t cpu0_vid_show(struct device *dev,
4018c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = smsc47m192_update_device(dev);
4048c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
4058c2ecf20Sopenharmony_ci}
4068c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(cpu0_vid);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_cistatic ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
4098c2ecf20Sopenharmony_ci		char *buf)
4108c2ecf20Sopenharmony_ci{
4118c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = dev_get_drvdata(dev);
4128c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->vrm);
4138c2ecf20Sopenharmony_ci}
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_cistatic ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
4168c2ecf20Sopenharmony_ci			 const char *buf, size_t count)
4178c2ecf20Sopenharmony_ci{
4188c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = dev_get_drvdata(dev);
4198c2ecf20Sopenharmony_ci	unsigned long val;
4208c2ecf20Sopenharmony_ci	int err;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
4238c2ecf20Sopenharmony_ci	if (err)
4248c2ecf20Sopenharmony_ci		return err;
4258c2ecf20Sopenharmony_ci	if (val > 255)
4268c2ecf20Sopenharmony_ci		return -EINVAL;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	data->vrm = val;
4298c2ecf20Sopenharmony_ci	return count;
4308c2ecf20Sopenharmony_ci}
4318c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(vrm);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci/* Alarms */
4348c2ecf20Sopenharmony_cistatic ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
4358c2ecf20Sopenharmony_ci			  char *buf)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
4388c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
4398c2ecf20Sopenharmony_ci	struct smsc47m192_data *data = smsc47m192_update_device(dev);
4408c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", (data->alarms & nr) ? 1 : 0);
4418c2ecf20Sopenharmony_ci}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 0x0010);
4448c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 0x0020);
4458c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 0x0040);
4468c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 0x4000);
4478c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 0x8000);
4488c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0x0001);
4498c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 0x0002);
4508c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 0x0004);
4518c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 0x0008);
4528c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 0x0100);
4538c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 0x0200);
4548c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 0x0400);
4558c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in7_alarm, alarm, 0x0800);
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_cistatic struct attribute *smsc47m192_attributes[] = {
4588c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_input.dev_attr.attr,
4598c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_min.dev_attr.attr,
4608c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_max.dev_attr.attr,
4618c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_alarm.dev_attr.attr,
4628c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_input.dev_attr.attr,
4638c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_min.dev_attr.attr,
4648c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_max.dev_attr.attr,
4658c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_alarm.dev_attr.attr,
4668c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_input.dev_attr.attr,
4678c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_min.dev_attr.attr,
4688c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_max.dev_attr.attr,
4698c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_alarm.dev_attr.attr,
4708c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_input.dev_attr.attr,
4718c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_min.dev_attr.attr,
4728c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_max.dev_attr.attr,
4738c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_alarm.dev_attr.attr,
4748c2ecf20Sopenharmony_ci	&sensor_dev_attr_in5_input.dev_attr.attr,
4758c2ecf20Sopenharmony_ci	&sensor_dev_attr_in5_min.dev_attr.attr,
4768c2ecf20Sopenharmony_ci	&sensor_dev_attr_in5_max.dev_attr.attr,
4778c2ecf20Sopenharmony_ci	&sensor_dev_attr_in5_alarm.dev_attr.attr,
4788c2ecf20Sopenharmony_ci	&sensor_dev_attr_in6_input.dev_attr.attr,
4798c2ecf20Sopenharmony_ci	&sensor_dev_attr_in6_min.dev_attr.attr,
4808c2ecf20Sopenharmony_ci	&sensor_dev_attr_in6_max.dev_attr.attr,
4818c2ecf20Sopenharmony_ci	&sensor_dev_attr_in6_alarm.dev_attr.attr,
4828c2ecf20Sopenharmony_ci	&sensor_dev_attr_in7_input.dev_attr.attr,
4838c2ecf20Sopenharmony_ci	&sensor_dev_attr_in7_min.dev_attr.attr,
4848c2ecf20Sopenharmony_ci	&sensor_dev_attr_in7_max.dev_attr.attr,
4858c2ecf20Sopenharmony_ci	&sensor_dev_attr_in7_alarm.dev_attr.attr,
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
4888c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max.dev_attr.attr,
4898c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min.dev_attr.attr,
4908c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_offset.dev_attr.attr,
4918c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
4928c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_input.dev_attr.attr,
4938c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max.dev_attr.attr,
4948c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_min.dev_attr.attr,
4958c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_offset.dev_attr.attr,
4968c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
4978c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_fault.dev_attr.attr,
4988c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_input.dev_attr.attr,
4998c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_max.dev_attr.attr,
5008c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_min.dev_attr.attr,
5018c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_offset.dev_attr.attr,
5028c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
5038c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_fault.dev_attr.attr,
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	&dev_attr_cpu0_vid.attr,
5068c2ecf20Sopenharmony_ci	&dev_attr_vrm.attr,
5078c2ecf20Sopenharmony_ci	NULL
5088c2ecf20Sopenharmony_ci};
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_cistatic const struct attribute_group smsc47m192_group = {
5118c2ecf20Sopenharmony_ci	.attrs = smsc47m192_attributes,
5128c2ecf20Sopenharmony_ci};
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_cistatic struct attribute *smsc47m192_attributes_in4[] = {
5158c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_input.dev_attr.attr,
5168c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_min.dev_attr.attr,
5178c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_max.dev_attr.attr,
5188c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_alarm.dev_attr.attr,
5198c2ecf20Sopenharmony_ci	NULL
5208c2ecf20Sopenharmony_ci};
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_cistatic const struct attribute_group smsc47m192_group_in4 = {
5238c2ecf20Sopenharmony_ci	.attrs = smsc47m192_attributes_in4,
5248c2ecf20Sopenharmony_ci};
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_cistatic void smsc47m192_init_client(struct i2c_client *client)
5278c2ecf20Sopenharmony_ci{
5288c2ecf20Sopenharmony_ci	int i;
5298c2ecf20Sopenharmony_ci	u8 config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
5308c2ecf20Sopenharmony_ci	u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	/* select cycle mode (pause 1 sec between updates) */
5338c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
5348c2ecf20Sopenharmony_ci						(sfr & 0xfd) | 0x02);
5358c2ecf20Sopenharmony_ci	if (!(config & 0x01)) {
5368c2ecf20Sopenharmony_ci		/* initialize alarm limits */
5378c2ecf20Sopenharmony_ci		for (i = 0; i < 8; i++) {
5388c2ecf20Sopenharmony_ci			i2c_smbus_write_byte_data(client,
5398c2ecf20Sopenharmony_ci				SMSC47M192_REG_IN_MIN(i), 0);
5408c2ecf20Sopenharmony_ci			i2c_smbus_write_byte_data(client,
5418c2ecf20Sopenharmony_ci				SMSC47M192_REG_IN_MAX(i), 0xff);
5428c2ecf20Sopenharmony_ci		}
5438c2ecf20Sopenharmony_ci		for (i = 0; i < 3; i++) {
5448c2ecf20Sopenharmony_ci			i2c_smbus_write_byte_data(client,
5458c2ecf20Sopenharmony_ci				SMSC47M192_REG_TEMP_MIN[i], 0x80);
5468c2ecf20Sopenharmony_ci			i2c_smbus_write_byte_data(client,
5478c2ecf20Sopenharmony_ci				SMSC47M192_REG_TEMP_MAX[i], 0x7f);
5488c2ecf20Sopenharmony_ci		}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci		/* start monitoring */
5518c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, SMSC47M192_REG_CONFIG,
5528c2ecf20Sopenharmony_ci						(config & 0xf7) | 0x01);
5538c2ecf20Sopenharmony_ci	}
5548c2ecf20Sopenharmony_ci}
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci/* Return 0 if detection is successful, -ENODEV otherwise */
5578c2ecf20Sopenharmony_cistatic int smsc47m192_detect(struct i2c_client *client,
5588c2ecf20Sopenharmony_ci			     struct i2c_board_info *info)
5598c2ecf20Sopenharmony_ci{
5608c2ecf20Sopenharmony_ci	struct i2c_adapter *adapter = client->adapter;
5618c2ecf20Sopenharmony_ci	int version;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
5648c2ecf20Sopenharmony_ci		return -ENODEV;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	/* Detection criteria from sensors_detect script */
5678c2ecf20Sopenharmony_ci	version = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VERSION);
5688c2ecf20Sopenharmony_ci	if (i2c_smbus_read_byte_data(client,
5698c2ecf20Sopenharmony_ci				SMSC47M192_REG_COMPANY_ID) == 0x55
5708c2ecf20Sopenharmony_ci	 && (version & 0xf0) == 0x20
5718c2ecf20Sopenharmony_ci	 && (i2c_smbus_read_byte_data(client,
5728c2ecf20Sopenharmony_ci				SMSC47M192_REG_VID) & 0x70) == 0x00
5738c2ecf20Sopenharmony_ci	 && (i2c_smbus_read_byte_data(client,
5748c2ecf20Sopenharmony_ci				SMSC47M192_REG_VID4) & 0xfe) == 0x80) {
5758c2ecf20Sopenharmony_ci		dev_info(&adapter->dev,
5768c2ecf20Sopenharmony_ci			 "found SMSC47M192 or compatible, "
5778c2ecf20Sopenharmony_ci			 "version 2, stepping A%d\n", version & 0x0f);
5788c2ecf20Sopenharmony_ci	} else {
5798c2ecf20Sopenharmony_ci		dev_dbg(&adapter->dev,
5808c2ecf20Sopenharmony_ci			"SMSC47M192 detection failed at 0x%02x\n",
5818c2ecf20Sopenharmony_ci			client->addr);
5828c2ecf20Sopenharmony_ci		return -ENODEV;
5838c2ecf20Sopenharmony_ci	}
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	strlcpy(info->type, "smsc47m192", I2C_NAME_SIZE);
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	return 0;
5888c2ecf20Sopenharmony_ci}
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_cistatic int smsc47m192_probe(struct i2c_client *client)
5918c2ecf20Sopenharmony_ci{
5928c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
5938c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
5948c2ecf20Sopenharmony_ci	struct smsc47m192_data *data;
5958c2ecf20Sopenharmony_ci	int config;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	data = devm_kzalloc(dev, sizeof(struct smsc47m192_data), GFP_KERNEL);
5988c2ecf20Sopenharmony_ci	if (!data)
5998c2ecf20Sopenharmony_ci		return -ENOMEM;
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	data->client = client;
6028c2ecf20Sopenharmony_ci	data->vrm = vid_which_vrm();
6038c2ecf20Sopenharmony_ci	mutex_init(&data->update_lock);
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	/* Initialize the SMSC47M192 chip */
6068c2ecf20Sopenharmony_ci	smsc47m192_init_client(client);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	/* sysfs hooks */
6098c2ecf20Sopenharmony_ci	data->groups[0] = &smsc47m192_group;
6108c2ecf20Sopenharmony_ci	/* Pin 110 is either in4 (+12V) or VID4 */
6118c2ecf20Sopenharmony_ci	config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
6128c2ecf20Sopenharmony_ci	if (!(config & 0x20))
6138c2ecf20Sopenharmony_ci		data->groups[1] = &smsc47m192_group_in4;
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
6168c2ecf20Sopenharmony_ci							   data, data->groups);
6178c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(hwmon_dev);
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_cistatic const struct i2c_device_id smsc47m192_id[] = {
6218c2ecf20Sopenharmony_ci	{ "smsc47m192", 0 },
6228c2ecf20Sopenharmony_ci	{ }
6238c2ecf20Sopenharmony_ci};
6248c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, smsc47m192_id);
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_cistatic struct i2c_driver smsc47m192_driver = {
6278c2ecf20Sopenharmony_ci	.class		= I2C_CLASS_HWMON,
6288c2ecf20Sopenharmony_ci	.driver = {
6298c2ecf20Sopenharmony_ci		.name	= "smsc47m192",
6308c2ecf20Sopenharmony_ci	},
6318c2ecf20Sopenharmony_ci	.probe_new	= smsc47m192_probe,
6328c2ecf20Sopenharmony_ci	.id_table	= smsc47m192_id,
6338c2ecf20Sopenharmony_ci	.detect		= smsc47m192_detect,
6348c2ecf20Sopenharmony_ci	.address_list	= normal_i2c,
6358c2ecf20Sopenharmony_ci};
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_cimodule_i2c_driver(smsc47m192_driver);
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ciMODULE_AUTHOR("Hartmut Rick <linux@rick.claranet.de>");
6408c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SMSC47M192 driver");
6418c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
642