xref: /kernel/linux/linux-5.10/drivers/hwmon/lm87.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * lm87.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2000       Frodo Looijaard <frodol@dds.nl>
68c2ecf20Sopenharmony_ci *                          Philip Edelbrock <phil@netroedge.com>
78c2ecf20Sopenharmony_ci *                          Stephen Rousset <stephen.rousset@rocketlogix.com>
88c2ecf20Sopenharmony_ci *                          Dan Eaton <dan.eaton@rocketlogix.com>
98c2ecf20Sopenharmony_ci * Copyright (C) 2004-2008  Jean Delvare <jdelvare@suse.de>
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Original port to Linux 2.6 by Jeff Oliver.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * The LM87 is a sensor chip made by National Semiconductor. It monitors up
148c2ecf20Sopenharmony_ci * to 8 voltages (including its own power source), up to three temperatures
158c2ecf20Sopenharmony_ci * (its own plus up to two external ones) and up to two fans. The default
168c2ecf20Sopenharmony_ci * configuration is 6 voltages, two temperatures and two fans (see below).
178c2ecf20Sopenharmony_ci * Voltages are scaled internally with ratios such that the nominal value of
188c2ecf20Sopenharmony_ci * each voltage correspond to a register value of 192 (which means a
198c2ecf20Sopenharmony_ci * resolution of about 0.5% of the nominal value). Temperature values are
208c2ecf20Sopenharmony_ci * reported with a 1 deg resolution and a 3-4 deg accuracy. Complete
218c2ecf20Sopenharmony_ci * datasheet can be obtained from National's website at:
228c2ecf20Sopenharmony_ci *   http://www.national.com/pf/LM/LM87.html
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * Some functions share pins, so not all functions are available at the same
258c2ecf20Sopenharmony_ci * time. Which are depends on the hardware setup. This driver normally
268c2ecf20Sopenharmony_ci * assumes that firmware configured the chip correctly. Where this is not
278c2ecf20Sopenharmony_ci * the case, platform code must set the I2C client's platform_data to point
288c2ecf20Sopenharmony_ci * to a u8 value to be written to the channel register.
298c2ecf20Sopenharmony_ci * For reference, here is the list of exclusive functions:
308c2ecf20Sopenharmony_ci *  - in0+in5 (default) or temp3
318c2ecf20Sopenharmony_ci *  - fan1 (default) or in6
328c2ecf20Sopenharmony_ci *  - fan2 (default) or in7
338c2ecf20Sopenharmony_ci *  - VID lines (default) or IRQ lines (not handled by this driver)
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * The LM87 additionally features an analog output, supposedly usable to
368c2ecf20Sopenharmony_ci * control the speed of a fan. All new chips use pulse width modulation
378c2ecf20Sopenharmony_ci * instead. The LM87 is the only hardware monitoring chipset I know of
388c2ecf20Sopenharmony_ci * which uses amplitude modulation. Be careful when using this feature.
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci * This driver also supports the ADM1024, a sensor chip made by Analog
418c2ecf20Sopenharmony_ci * Devices. That chip is fully compatible with the LM87. Complete
428c2ecf20Sopenharmony_ci * datasheet can be obtained from Analog's website at:
438c2ecf20Sopenharmony_ci *   https://www.analog.com/en/prod/0,2877,ADM1024,00.html
448c2ecf20Sopenharmony_ci */
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#include <linux/module.h>
478c2ecf20Sopenharmony_ci#include <linux/init.h>
488c2ecf20Sopenharmony_ci#include <linux/slab.h>
498c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
508c2ecf20Sopenharmony_ci#include <linux/i2c.h>
518c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
528c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
538c2ecf20Sopenharmony_ci#include <linux/hwmon-vid.h>
548c2ecf20Sopenharmony_ci#include <linux/err.h>
558c2ecf20Sopenharmony_ci#include <linux/mutex.h>
568c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/*
598c2ecf20Sopenharmony_ci * Addresses to scan
608c2ecf20Sopenharmony_ci * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
618c2ecf20Sopenharmony_ci */
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/*
668c2ecf20Sopenharmony_ci * The LM87 registers
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/* nr in 0..5 */
708c2ecf20Sopenharmony_ci#define LM87_REG_IN(nr)			(0x20 + (nr))
718c2ecf20Sopenharmony_ci#define LM87_REG_IN_MAX(nr)		(0x2B + (nr) * 2)
728c2ecf20Sopenharmony_ci#define LM87_REG_IN_MIN(nr)		(0x2C + (nr) * 2)
738c2ecf20Sopenharmony_ci/* nr in 0..1 */
748c2ecf20Sopenharmony_ci#define LM87_REG_AIN(nr)		(0x28 + (nr))
758c2ecf20Sopenharmony_ci#define LM87_REG_AIN_MIN(nr)		(0x1A + (nr))
768c2ecf20Sopenharmony_ci#define LM87_REG_AIN_MAX(nr)		(0x3B + (nr))
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
798c2ecf20Sopenharmony_cistatic u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
808c2ecf20Sopenharmony_cistatic u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define LM87_REG_TEMP_HW_INT_LOCK	0x13
838c2ecf20Sopenharmony_ci#define LM87_REG_TEMP_HW_EXT_LOCK	0x14
848c2ecf20Sopenharmony_ci#define LM87_REG_TEMP_HW_INT		0x17
858c2ecf20Sopenharmony_ci#define LM87_REG_TEMP_HW_EXT		0x18
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/* nr in 0..1 */
888c2ecf20Sopenharmony_ci#define LM87_REG_FAN(nr)		(0x28 + (nr))
898c2ecf20Sopenharmony_ci#define LM87_REG_FAN_MIN(nr)		(0x3B + (nr))
908c2ecf20Sopenharmony_ci#define LM87_REG_AOUT			0x19
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci#define LM87_REG_CONFIG			0x40
938c2ecf20Sopenharmony_ci#define LM87_REG_CHANNEL_MODE		0x16
948c2ecf20Sopenharmony_ci#define LM87_REG_VID_FAN_DIV		0x47
958c2ecf20Sopenharmony_ci#define LM87_REG_VID4			0x49
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci#define LM87_REG_ALARMS1		0x41
988c2ecf20Sopenharmony_ci#define LM87_REG_ALARMS2		0x42
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci#define LM87_REG_COMPANY_ID		0x3E
1018c2ecf20Sopenharmony_ci#define LM87_REG_REVISION		0x3F
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/*
1048c2ecf20Sopenharmony_ci * Conversions and various macros
1058c2ecf20Sopenharmony_ci * The LM87 uses signed 8-bit values for temperatures.
1068c2ecf20Sopenharmony_ci */
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci#define IN_FROM_REG(reg, scale)	(((reg) * (scale) + 96) / 192)
1098c2ecf20Sopenharmony_ci#define IN_TO_REG(val, scale)	((val) <= 0 ? 0 : \
1108c2ecf20Sopenharmony_ci				 (val) >= (scale) * 255 / 192 ? 255 : \
1118c2ecf20Sopenharmony_ci				 ((val) * 192 + (scale) / 2) / (scale))
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci#define TEMP_FROM_REG(reg)	((reg) * 1000)
1148c2ecf20Sopenharmony_ci#define TEMP_TO_REG(val)	((val) <= -127500 ? -128 : \
1158c2ecf20Sopenharmony_ci				 (val) >= 126500 ? 127 : \
1168c2ecf20Sopenharmony_ci				 (((val) < 0 ? (val) - 500 : \
1178c2ecf20Sopenharmony_ci				   (val) + 500) / 1000))
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci#define FAN_FROM_REG(reg, div)	((reg) == 255 || (reg) == 0 ? 0 : \
1208c2ecf20Sopenharmony_ci				 (1350000 + (reg)*(div) / 2) / ((reg) * (div)))
1218c2ecf20Sopenharmony_ci#define FAN_TO_REG(val, div)	((val) * (div) * 255 <= 1350000 ? 255 : \
1228c2ecf20Sopenharmony_ci				 (1350000 + (val)*(div) / 2) / ((val) * (div)))
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci#define FAN_DIV_FROM_REG(reg)	(1 << (reg))
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci/* analog out is 9.80mV/LSB */
1278c2ecf20Sopenharmony_ci#define AOUT_FROM_REG(reg)	(((reg) * 98 + 5) / 10)
1288c2ecf20Sopenharmony_ci#define AOUT_TO_REG(val)	((val) <= 0 ? 0 : \
1298c2ecf20Sopenharmony_ci				 (val) >= 2500 ? 255 : \
1308c2ecf20Sopenharmony_ci				 ((val) * 10 + 49) / 98)
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/* nr in 0..1 */
1338c2ecf20Sopenharmony_ci#define CHAN_NO_FAN(nr)		(1 << (nr))
1348c2ecf20Sopenharmony_ci#define CHAN_TEMP3		(1 << 2)
1358c2ecf20Sopenharmony_ci#define CHAN_VCC_5V		(1 << 3)
1368c2ecf20Sopenharmony_ci#define CHAN_NO_VID		(1 << 7)
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci/*
1398c2ecf20Sopenharmony_ci * Client data (each client gets its own)
1408c2ecf20Sopenharmony_ci */
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistruct lm87_data {
1438c2ecf20Sopenharmony_ci	struct mutex update_lock;
1448c2ecf20Sopenharmony_ci	char valid; /* zero until following fields are valid */
1458c2ecf20Sopenharmony_ci	unsigned long last_updated; /* In jiffies */
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	u8 channel;		/* register value */
1488c2ecf20Sopenharmony_ci	u8 config;		/* original register value */
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	u8 in[8];		/* register value */
1518c2ecf20Sopenharmony_ci	u8 in_max[8];		/* register value */
1528c2ecf20Sopenharmony_ci	u8 in_min[8];		/* register value */
1538c2ecf20Sopenharmony_ci	u16 in_scale[8];
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	s8 temp[3];		/* register value */
1568c2ecf20Sopenharmony_ci	s8 temp_high[3];	/* register value */
1578c2ecf20Sopenharmony_ci	s8 temp_low[3];		/* register value */
1588c2ecf20Sopenharmony_ci	s8 temp_crit_int;	/* min of two register values */
1598c2ecf20Sopenharmony_ci	s8 temp_crit_ext;	/* min of two register values */
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	u8 fan[2];		/* register value */
1628c2ecf20Sopenharmony_ci	u8 fan_min[2];		/* register value */
1638c2ecf20Sopenharmony_ci	u8 fan_div[2];		/* register value, shifted right */
1648c2ecf20Sopenharmony_ci	u8 aout;		/* register value */
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	u16 alarms;		/* register values, combined */
1678c2ecf20Sopenharmony_ci	u8 vid;			/* register values, combined */
1688c2ecf20Sopenharmony_ci	u8 vrm;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	const struct attribute_group *attr_groups[6];
1718c2ecf20Sopenharmony_ci};
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic inline int lm87_read_value(struct i2c_client *client, u8 reg)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	return i2c_smbus_read_byte_data(client, reg);
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	return i2c_smbus_write_byte_data(client, reg, value);
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic struct lm87_data *lm87_update_device(struct device *dev)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	struct i2c_client *client = dev_get_drvdata(dev);
1868c2ecf20Sopenharmony_ci	struct lm87_data *data = i2c_get_clientdata(client);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
1918c2ecf20Sopenharmony_ci		int i, j;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "Updating data.\n");
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci		i = (data->channel & CHAN_TEMP3) ? 1 : 0;
1968c2ecf20Sopenharmony_ci		j = (data->channel & CHAN_TEMP3) ? 5 : 6;
1978c2ecf20Sopenharmony_ci		for (; i < j; i++) {
1988c2ecf20Sopenharmony_ci			data->in[i] = lm87_read_value(client,
1998c2ecf20Sopenharmony_ci				      LM87_REG_IN(i));
2008c2ecf20Sopenharmony_ci			data->in_min[i] = lm87_read_value(client,
2018c2ecf20Sopenharmony_ci					  LM87_REG_IN_MIN(i));
2028c2ecf20Sopenharmony_ci			data->in_max[i] = lm87_read_value(client,
2038c2ecf20Sopenharmony_ci					  LM87_REG_IN_MAX(i));
2048c2ecf20Sopenharmony_ci		}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci		for (i = 0; i < 2; i++) {
2078c2ecf20Sopenharmony_ci			if (data->channel & CHAN_NO_FAN(i)) {
2088c2ecf20Sopenharmony_ci				data->in[6+i] = lm87_read_value(client,
2098c2ecf20Sopenharmony_ci						LM87_REG_AIN(i));
2108c2ecf20Sopenharmony_ci				data->in_max[6+i] = lm87_read_value(client,
2118c2ecf20Sopenharmony_ci						    LM87_REG_AIN_MAX(i));
2128c2ecf20Sopenharmony_ci				data->in_min[6+i] = lm87_read_value(client,
2138c2ecf20Sopenharmony_ci						    LM87_REG_AIN_MIN(i));
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci			} else {
2168c2ecf20Sopenharmony_ci				data->fan[i] = lm87_read_value(client,
2178c2ecf20Sopenharmony_ci					       LM87_REG_FAN(i));
2188c2ecf20Sopenharmony_ci				data->fan_min[i] = lm87_read_value(client,
2198c2ecf20Sopenharmony_ci						   LM87_REG_FAN_MIN(i));
2208c2ecf20Sopenharmony_ci			}
2218c2ecf20Sopenharmony_ci		}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci		j = (data->channel & CHAN_TEMP3) ? 3 : 2;
2248c2ecf20Sopenharmony_ci		for (i = 0 ; i < j; i++) {
2258c2ecf20Sopenharmony_ci			data->temp[i] = lm87_read_value(client,
2268c2ecf20Sopenharmony_ci					LM87_REG_TEMP[i]);
2278c2ecf20Sopenharmony_ci			data->temp_high[i] = lm87_read_value(client,
2288c2ecf20Sopenharmony_ci					     LM87_REG_TEMP_HIGH[i]);
2298c2ecf20Sopenharmony_ci			data->temp_low[i] = lm87_read_value(client,
2308c2ecf20Sopenharmony_ci					    LM87_REG_TEMP_LOW[i]);
2318c2ecf20Sopenharmony_ci		}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci		i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
2348c2ecf20Sopenharmony_ci		j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
2358c2ecf20Sopenharmony_ci		data->temp_crit_int = min(i, j);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci		i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
2388c2ecf20Sopenharmony_ci		j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
2398c2ecf20Sopenharmony_ci		data->temp_crit_ext = min(i, j);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci		i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
2428c2ecf20Sopenharmony_ci		data->fan_div[0] = (i >> 4) & 0x03;
2438c2ecf20Sopenharmony_ci		data->fan_div[1] = (i >> 6) & 0x03;
2448c2ecf20Sopenharmony_ci		data->vid = (i & 0x0F)
2458c2ecf20Sopenharmony_ci			  | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
2468c2ecf20Sopenharmony_ci			     << 4;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci		data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
2498c2ecf20Sopenharmony_ci			     | (lm87_read_value(client, LM87_REG_ALARMS2)
2508c2ecf20Sopenharmony_ci				<< 8);
2518c2ecf20Sopenharmony_ci		data->aout = lm87_read_value(client, LM87_REG_AOUT);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci		data->last_updated = jiffies;
2548c2ecf20Sopenharmony_ci		data->valid = 1;
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	return data;
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci/*
2638c2ecf20Sopenharmony_ci * Sysfs stuff
2648c2ecf20Sopenharmony_ci */
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic ssize_t in_input_show(struct device *dev,
2678c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	struct lm87_data *data = lm87_update_device(dev);
2708c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[nr],
2738c2ecf20Sopenharmony_ci		       data->in_scale[nr]));
2748c2ecf20Sopenharmony_ci}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
2778c2ecf20Sopenharmony_ci			   char *buf)
2788c2ecf20Sopenharmony_ci{
2798c2ecf20Sopenharmony_ci	struct lm87_data *data = lm87_update_device(dev);
2808c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[nr],
2838c2ecf20Sopenharmony_ci		       data->in_scale[nr]));
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
2878c2ecf20Sopenharmony_ci			   char *buf)
2888c2ecf20Sopenharmony_ci{
2898c2ecf20Sopenharmony_ci	struct lm87_data *data = lm87_update_device(dev);
2908c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[nr],
2938c2ecf20Sopenharmony_ci		       data->in_scale[nr]));
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
2978c2ecf20Sopenharmony_ci			    const char *buf, size_t count)
2988c2ecf20Sopenharmony_ci{
2998c2ecf20Sopenharmony_ci	struct i2c_client *client = dev_get_drvdata(dev);
3008c2ecf20Sopenharmony_ci	struct lm87_data *data = i2c_get_clientdata(client);
3018c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
3028c2ecf20Sopenharmony_ci	long val;
3038c2ecf20Sopenharmony_ci	int err;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
3068c2ecf20Sopenharmony_ci	if (err)
3078c2ecf20Sopenharmony_ci		return err;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3108c2ecf20Sopenharmony_ci	data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
3118c2ecf20Sopenharmony_ci	lm87_write_value(client, nr < 6 ? LM87_REG_IN_MIN(nr) :
3128c2ecf20Sopenharmony_ci			 LM87_REG_AIN_MIN(nr - 6), data->in_min[nr]);
3138c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3148c2ecf20Sopenharmony_ci	return count;
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
3188c2ecf20Sopenharmony_ci			    const char *buf, size_t count)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	struct i2c_client *client = dev_get_drvdata(dev);
3218c2ecf20Sopenharmony_ci	struct lm87_data *data = i2c_get_clientdata(client);
3228c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
3238c2ecf20Sopenharmony_ci	long val;
3248c2ecf20Sopenharmony_ci	int err;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
3278c2ecf20Sopenharmony_ci	if (err)
3288c2ecf20Sopenharmony_ci		return err;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3318c2ecf20Sopenharmony_ci	data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
3328c2ecf20Sopenharmony_ci	lm87_write_value(client, nr < 6 ? LM87_REG_IN_MAX(nr) :
3338c2ecf20Sopenharmony_ci			 LM87_REG_AIN_MAX(nr - 6), data->in_max[nr]);
3348c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3358c2ecf20Sopenharmony_ci	return count;
3368c2ecf20Sopenharmony_ci}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_input, in_input, 0);
3398c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
3408c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
3418c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_input, in_input, 1);
3428c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
3438c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
3448c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_input, in_input, 2);
3458c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
3468c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
3478c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_input, in_input, 3);
3488c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
3498c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
3508c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_input, in_input, 4);
3518c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
3528c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
3538c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in5_input, in_input, 5);
3548c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
3558c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
3568c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in6_input, in_input, 6);
3578c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 6);
3588c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 6);
3598c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in7_input, in_input, 7);
3608c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in7_min, in_min, 7);
3618c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in7_max, in_max, 7);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic ssize_t temp_input_show(struct device *dev,
3648c2ecf20Sopenharmony_ci			       struct device_attribute *attr, char *buf)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	struct lm87_data *data = lm87_update_device(dev);
3678c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
3708c2ecf20Sopenharmony_ci}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_cistatic ssize_t temp_low_show(struct device *dev,
3738c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	struct lm87_data *data = lm87_update_device(dev);
3768c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n",
3798c2ecf20Sopenharmony_ci		       TEMP_FROM_REG(data->temp_low[nr]));
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_cistatic ssize_t temp_high_show(struct device *dev,
3838c2ecf20Sopenharmony_ci			      struct device_attribute *attr, char *buf)
3848c2ecf20Sopenharmony_ci{
3858c2ecf20Sopenharmony_ci	struct lm87_data *data = lm87_update_device(dev);
3868c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n",
3898c2ecf20Sopenharmony_ci		       TEMP_FROM_REG(data->temp_high[nr]));
3908c2ecf20Sopenharmony_ci}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_cistatic ssize_t temp_low_store(struct device *dev,
3938c2ecf20Sopenharmony_ci			      struct device_attribute *attr, const char *buf,
3948c2ecf20Sopenharmony_ci			      size_t count)
3958c2ecf20Sopenharmony_ci{
3968c2ecf20Sopenharmony_ci	struct i2c_client *client = dev_get_drvdata(dev);
3978c2ecf20Sopenharmony_ci	struct lm87_data *data = i2c_get_clientdata(client);
3988c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
3998c2ecf20Sopenharmony_ci	long val;
4008c2ecf20Sopenharmony_ci	int err;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
4038c2ecf20Sopenharmony_ci	if (err)
4048c2ecf20Sopenharmony_ci		return err;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
4078c2ecf20Sopenharmony_ci	data->temp_low[nr] = TEMP_TO_REG(val);
4088c2ecf20Sopenharmony_ci	lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
4098c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
4108c2ecf20Sopenharmony_ci	return count;
4118c2ecf20Sopenharmony_ci}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_cistatic ssize_t temp_high_store(struct device *dev,
4148c2ecf20Sopenharmony_ci			       struct device_attribute *attr, const char *buf,
4158c2ecf20Sopenharmony_ci			       size_t count)
4168c2ecf20Sopenharmony_ci{
4178c2ecf20Sopenharmony_ci	struct i2c_client *client = dev_get_drvdata(dev);
4188c2ecf20Sopenharmony_ci	struct lm87_data *data = i2c_get_clientdata(client);
4198c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
4208c2ecf20Sopenharmony_ci	long val;
4218c2ecf20Sopenharmony_ci	int err;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
4248c2ecf20Sopenharmony_ci	if (err)
4258c2ecf20Sopenharmony_ci		return err;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
4288c2ecf20Sopenharmony_ci	data->temp_high[nr] = TEMP_TO_REG(val);
4298c2ecf20Sopenharmony_ci	lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
4308c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
4318c2ecf20Sopenharmony_ci	return count;
4328c2ecf20Sopenharmony_ci}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
4358c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_min, temp_low, 0);
4368c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max, temp_high, 0);
4378c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1);
4388c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_min, temp_low, 1);
4398c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_max, temp_high, 1);
4408c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_input, temp_input, 2);
4418c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_min, temp_low, 2);
4428c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_max, temp_high, 2);
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_cistatic ssize_t temp1_crit_show(struct device *dev,
4458c2ecf20Sopenharmony_ci			       struct device_attribute *attr, char *buf)
4468c2ecf20Sopenharmony_ci{
4478c2ecf20Sopenharmony_ci	struct lm87_data *data = lm87_update_device(dev);
4488c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
4498c2ecf20Sopenharmony_ci}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_cistatic ssize_t temp2_crit_show(struct device *dev,
4528c2ecf20Sopenharmony_ci			       struct device_attribute *attr, char *buf)
4538c2ecf20Sopenharmony_ci{
4548c2ecf20Sopenharmony_ci	struct lm87_data *data = lm87_update_device(dev);
4558c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
4568c2ecf20Sopenharmony_ci}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(temp1_crit);
4598c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(temp2_crit);
4608c2ecf20Sopenharmony_cistatic DEVICE_ATTR(temp3_crit, 0444, temp2_crit_show, NULL);
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_cistatic ssize_t fan_input_show(struct device *dev,
4638c2ecf20Sopenharmony_ci			      struct device_attribute *attr, char *buf)
4648c2ecf20Sopenharmony_ci{
4658c2ecf20Sopenharmony_ci	struct lm87_data *data = lm87_update_device(dev);
4668c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
4698c2ecf20Sopenharmony_ci		       FAN_DIV_FROM_REG(data->fan_div[nr])));
4708c2ecf20Sopenharmony_ci}
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_cistatic ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
4738c2ecf20Sopenharmony_ci			    char *buf)
4748c2ecf20Sopenharmony_ci{
4758c2ecf20Sopenharmony_ci	struct lm87_data *data = lm87_update_device(dev);
4768c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
4798c2ecf20Sopenharmony_ci		       FAN_DIV_FROM_REG(data->fan_div[nr])));
4808c2ecf20Sopenharmony_ci}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_cistatic ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
4838c2ecf20Sopenharmony_ci			    char *buf)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	struct lm87_data *data = lm87_update_device(dev);
4868c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n",
4898c2ecf20Sopenharmony_ci		       FAN_DIV_FROM_REG(data->fan_div[nr]));
4908c2ecf20Sopenharmony_ci}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_cistatic ssize_t fan_min_store(struct device *dev,
4938c2ecf20Sopenharmony_ci			     struct device_attribute *attr, const char *buf,
4948c2ecf20Sopenharmony_ci			     size_t count)
4958c2ecf20Sopenharmony_ci{
4968c2ecf20Sopenharmony_ci	struct i2c_client *client = dev_get_drvdata(dev);
4978c2ecf20Sopenharmony_ci	struct lm87_data *data = i2c_get_clientdata(client);
4988c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
4998c2ecf20Sopenharmony_ci	long val;
5008c2ecf20Sopenharmony_ci	int err;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
5038c2ecf20Sopenharmony_ci	if (err)
5048c2ecf20Sopenharmony_ci		return err;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
5078c2ecf20Sopenharmony_ci	data->fan_min[nr] = FAN_TO_REG(val,
5088c2ecf20Sopenharmony_ci			    FAN_DIV_FROM_REG(data->fan_div[nr]));
5098c2ecf20Sopenharmony_ci	lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
5108c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
5118c2ecf20Sopenharmony_ci	return count;
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci/*
5158c2ecf20Sopenharmony_ci * Note: we save and restore the fan minimum here, because its value is
5168c2ecf20Sopenharmony_ci * determined in part by the fan clock divider.  This follows the principle
5178c2ecf20Sopenharmony_ci * of least surprise; the user doesn't expect the fan minimum to change just
5188c2ecf20Sopenharmony_ci * because the divider changed.
5198c2ecf20Sopenharmony_ci */
5208c2ecf20Sopenharmony_cistatic ssize_t fan_div_store(struct device *dev,
5218c2ecf20Sopenharmony_ci			     struct device_attribute *attr, const char *buf,
5228c2ecf20Sopenharmony_ci			     size_t count)
5238c2ecf20Sopenharmony_ci{
5248c2ecf20Sopenharmony_ci	struct i2c_client *client = dev_get_drvdata(dev);
5258c2ecf20Sopenharmony_ci	struct lm87_data *data = i2c_get_clientdata(client);
5268c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
5278c2ecf20Sopenharmony_ci	long val;
5288c2ecf20Sopenharmony_ci	int err;
5298c2ecf20Sopenharmony_ci	unsigned long min;
5308c2ecf20Sopenharmony_ci	u8 reg;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
5338c2ecf20Sopenharmony_ci	if (err)
5348c2ecf20Sopenharmony_ci		return err;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
5378c2ecf20Sopenharmony_ci	min = FAN_FROM_REG(data->fan_min[nr],
5388c2ecf20Sopenharmony_ci			   FAN_DIV_FROM_REG(data->fan_div[nr]));
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	switch (val) {
5418c2ecf20Sopenharmony_ci	case 1:
5428c2ecf20Sopenharmony_ci		data->fan_div[nr] = 0;
5438c2ecf20Sopenharmony_ci		break;
5448c2ecf20Sopenharmony_ci	case 2:
5458c2ecf20Sopenharmony_ci		data->fan_div[nr] = 1;
5468c2ecf20Sopenharmony_ci		break;
5478c2ecf20Sopenharmony_ci	case 4:
5488c2ecf20Sopenharmony_ci		data->fan_div[nr] = 2;
5498c2ecf20Sopenharmony_ci		break;
5508c2ecf20Sopenharmony_ci	case 8:
5518c2ecf20Sopenharmony_ci		data->fan_div[nr] = 3;
5528c2ecf20Sopenharmony_ci		break;
5538c2ecf20Sopenharmony_ci	default:
5548c2ecf20Sopenharmony_ci		mutex_unlock(&data->update_lock);
5558c2ecf20Sopenharmony_ci		return -EINVAL;
5568c2ecf20Sopenharmony_ci	}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
5598c2ecf20Sopenharmony_ci	switch (nr) {
5608c2ecf20Sopenharmony_ci	case 0:
5618c2ecf20Sopenharmony_ci	    reg = (reg & 0xCF) | (data->fan_div[0] << 4);
5628c2ecf20Sopenharmony_ci	    break;
5638c2ecf20Sopenharmony_ci	case 1:
5648c2ecf20Sopenharmony_ci	    reg = (reg & 0x3F) | (data->fan_div[1] << 6);
5658c2ecf20Sopenharmony_ci	    break;
5668c2ecf20Sopenharmony_ci	}
5678c2ecf20Sopenharmony_ci	lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	data->fan_min[nr] = FAN_TO_REG(min, val);
5708c2ecf20Sopenharmony_ci	lm87_write_value(client, LM87_REG_FAN_MIN(nr),
5718c2ecf20Sopenharmony_ci			 data->fan_min[nr]);
5728c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	return count;
5758c2ecf20Sopenharmony_ci}
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
5788c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
5798c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
5808c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
5818c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
5828c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_cistatic ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
5858c2ecf20Sopenharmony_ci			   char *buf)
5868c2ecf20Sopenharmony_ci{
5878c2ecf20Sopenharmony_ci	struct lm87_data *data = lm87_update_device(dev);
5888c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->alarms);
5898c2ecf20Sopenharmony_ci}
5908c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(alarms);
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_cistatic ssize_t cpu0_vid_show(struct device *dev,
5938c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
5948c2ecf20Sopenharmony_ci{
5958c2ecf20Sopenharmony_ci	struct lm87_data *data = lm87_update_device(dev);
5968c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
5978c2ecf20Sopenharmony_ci}
5988c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(cpu0_vid);
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_cistatic ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
6018c2ecf20Sopenharmony_ci			char *buf)
6028c2ecf20Sopenharmony_ci{
6038c2ecf20Sopenharmony_ci	struct lm87_data *data = dev_get_drvdata(dev);
6048c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->vrm);
6058c2ecf20Sopenharmony_ci}
6068c2ecf20Sopenharmony_cistatic ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
6078c2ecf20Sopenharmony_ci			 const char *buf, size_t count)
6088c2ecf20Sopenharmony_ci{
6098c2ecf20Sopenharmony_ci	struct lm87_data *data = dev_get_drvdata(dev);
6108c2ecf20Sopenharmony_ci	unsigned long val;
6118c2ecf20Sopenharmony_ci	int err;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
6148c2ecf20Sopenharmony_ci	if (err)
6158c2ecf20Sopenharmony_ci		return err;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	if (val > 255)
6188c2ecf20Sopenharmony_ci		return -EINVAL;
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	data->vrm = val;
6218c2ecf20Sopenharmony_ci	return count;
6228c2ecf20Sopenharmony_ci}
6238c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(vrm);
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_cistatic ssize_t aout_output_show(struct device *dev,
6268c2ecf20Sopenharmony_ci				struct device_attribute *attr, char *buf)
6278c2ecf20Sopenharmony_ci{
6288c2ecf20Sopenharmony_ci	struct lm87_data *data = lm87_update_device(dev);
6298c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
6308c2ecf20Sopenharmony_ci}
6318c2ecf20Sopenharmony_cistatic ssize_t aout_output_store(struct device *dev,
6328c2ecf20Sopenharmony_ci				 struct device_attribute *attr,
6338c2ecf20Sopenharmony_ci				 const char *buf, size_t count)
6348c2ecf20Sopenharmony_ci{
6358c2ecf20Sopenharmony_ci	struct i2c_client *client = dev_get_drvdata(dev);
6368c2ecf20Sopenharmony_ci	struct lm87_data *data = i2c_get_clientdata(client);
6378c2ecf20Sopenharmony_ci	long val;
6388c2ecf20Sopenharmony_ci	int err;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
6418c2ecf20Sopenharmony_ci	if (err)
6428c2ecf20Sopenharmony_ci		return err;
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
6458c2ecf20Sopenharmony_ci	data->aout = AOUT_TO_REG(val);
6468c2ecf20Sopenharmony_ci	lm87_write_value(client, LM87_REG_AOUT, data->aout);
6478c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
6488c2ecf20Sopenharmony_ci	return count;
6498c2ecf20Sopenharmony_ci}
6508c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(aout_output);
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_cistatic ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
6538c2ecf20Sopenharmony_ci			  char *buf)
6548c2ecf20Sopenharmony_ci{
6558c2ecf20Sopenharmony_ci	struct lm87_data *data = lm87_update_device(dev);
6568c2ecf20Sopenharmony_ci	int bitnr = to_sensor_dev_attr(attr)->index;
6578c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
6588c2ecf20Sopenharmony_ci}
6598c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
6608c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
6618c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
6628c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
6638c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
6648c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
6658c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 6);
6668c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in7_alarm, alarm, 7);
6678c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
6688c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 5);
6698c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 5);
6708c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
6718c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
6728c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 14);
6738c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 15);
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci/*
6768c2ecf20Sopenharmony_ci * Real code
6778c2ecf20Sopenharmony_ci */
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_cistatic struct attribute *lm87_attributes[] = {
6808c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_input.dev_attr.attr,
6818c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_min.dev_attr.attr,
6828c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_max.dev_attr.attr,
6838c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_alarm.dev_attr.attr,
6848c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_input.dev_attr.attr,
6858c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_min.dev_attr.attr,
6868c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_max.dev_attr.attr,
6878c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_alarm.dev_attr.attr,
6888c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_input.dev_attr.attr,
6898c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_min.dev_attr.attr,
6908c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_max.dev_attr.attr,
6918c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_alarm.dev_attr.attr,
6928c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_input.dev_attr.attr,
6938c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_min.dev_attr.attr,
6948c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_max.dev_attr.attr,
6958c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_alarm.dev_attr.attr,
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
6988c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max.dev_attr.attr,
6998c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_min.dev_attr.attr,
7008c2ecf20Sopenharmony_ci	&dev_attr_temp1_crit.attr,
7018c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
7028c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_input.dev_attr.attr,
7038c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max.dev_attr.attr,
7048c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_min.dev_attr.attr,
7058c2ecf20Sopenharmony_ci	&dev_attr_temp2_crit.attr,
7068c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
7078c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_fault.dev_attr.attr,
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	&dev_attr_alarms.attr,
7108c2ecf20Sopenharmony_ci	&dev_attr_aout_output.attr,
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	NULL
7138c2ecf20Sopenharmony_ci};
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_cistatic const struct attribute_group lm87_group = {
7168c2ecf20Sopenharmony_ci	.attrs = lm87_attributes,
7178c2ecf20Sopenharmony_ci};
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_cistatic struct attribute *lm87_attributes_in6[] = {
7208c2ecf20Sopenharmony_ci	&sensor_dev_attr_in6_input.dev_attr.attr,
7218c2ecf20Sopenharmony_ci	&sensor_dev_attr_in6_min.dev_attr.attr,
7228c2ecf20Sopenharmony_ci	&sensor_dev_attr_in6_max.dev_attr.attr,
7238c2ecf20Sopenharmony_ci	&sensor_dev_attr_in6_alarm.dev_attr.attr,
7248c2ecf20Sopenharmony_ci	NULL
7258c2ecf20Sopenharmony_ci};
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_cistatic const struct attribute_group lm87_group_in6 = {
7288c2ecf20Sopenharmony_ci	.attrs = lm87_attributes_in6,
7298c2ecf20Sopenharmony_ci};
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_cistatic struct attribute *lm87_attributes_fan1[] = {
7328c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_input.dev_attr.attr,
7338c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_min.dev_attr.attr,
7348c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_div.dev_attr.attr,
7358c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
7368c2ecf20Sopenharmony_ci	NULL
7378c2ecf20Sopenharmony_ci};
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_cistatic const struct attribute_group lm87_group_fan1 = {
7408c2ecf20Sopenharmony_ci	.attrs = lm87_attributes_fan1,
7418c2ecf20Sopenharmony_ci};
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_cistatic struct attribute *lm87_attributes_in7[] = {
7448c2ecf20Sopenharmony_ci	&sensor_dev_attr_in7_input.dev_attr.attr,
7458c2ecf20Sopenharmony_ci	&sensor_dev_attr_in7_min.dev_attr.attr,
7468c2ecf20Sopenharmony_ci	&sensor_dev_attr_in7_max.dev_attr.attr,
7478c2ecf20Sopenharmony_ci	&sensor_dev_attr_in7_alarm.dev_attr.attr,
7488c2ecf20Sopenharmony_ci	NULL
7498c2ecf20Sopenharmony_ci};
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_cistatic const struct attribute_group lm87_group_in7 = {
7528c2ecf20Sopenharmony_ci	.attrs = lm87_attributes_in7,
7538c2ecf20Sopenharmony_ci};
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_cistatic struct attribute *lm87_attributes_fan2[] = {
7568c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_input.dev_attr.attr,
7578c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_min.dev_attr.attr,
7588c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_div.dev_attr.attr,
7598c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
7608c2ecf20Sopenharmony_ci	NULL
7618c2ecf20Sopenharmony_ci};
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_cistatic const struct attribute_group lm87_group_fan2 = {
7648c2ecf20Sopenharmony_ci	.attrs = lm87_attributes_fan2,
7658c2ecf20Sopenharmony_ci};
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_cistatic struct attribute *lm87_attributes_temp3[] = {
7688c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_input.dev_attr.attr,
7698c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_max.dev_attr.attr,
7708c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_min.dev_attr.attr,
7718c2ecf20Sopenharmony_ci	&dev_attr_temp3_crit.attr,
7728c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
7738c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_fault.dev_attr.attr,
7748c2ecf20Sopenharmony_ci	NULL
7758c2ecf20Sopenharmony_ci};
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_cistatic const struct attribute_group lm87_group_temp3 = {
7788c2ecf20Sopenharmony_ci	.attrs = lm87_attributes_temp3,
7798c2ecf20Sopenharmony_ci};
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_cistatic struct attribute *lm87_attributes_in0_5[] = {
7828c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_input.dev_attr.attr,
7838c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_min.dev_attr.attr,
7848c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_max.dev_attr.attr,
7858c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_alarm.dev_attr.attr,
7868c2ecf20Sopenharmony_ci	&sensor_dev_attr_in5_input.dev_attr.attr,
7878c2ecf20Sopenharmony_ci	&sensor_dev_attr_in5_min.dev_attr.attr,
7888c2ecf20Sopenharmony_ci	&sensor_dev_attr_in5_max.dev_attr.attr,
7898c2ecf20Sopenharmony_ci	&sensor_dev_attr_in5_alarm.dev_attr.attr,
7908c2ecf20Sopenharmony_ci	NULL
7918c2ecf20Sopenharmony_ci};
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_cistatic const struct attribute_group lm87_group_in0_5 = {
7948c2ecf20Sopenharmony_ci	.attrs = lm87_attributes_in0_5,
7958c2ecf20Sopenharmony_ci};
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_cistatic struct attribute *lm87_attributes_vid[] = {
7988c2ecf20Sopenharmony_ci	&dev_attr_cpu0_vid.attr,
7998c2ecf20Sopenharmony_ci	&dev_attr_vrm.attr,
8008c2ecf20Sopenharmony_ci	NULL
8018c2ecf20Sopenharmony_ci};
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_cistatic const struct attribute_group lm87_group_vid = {
8048c2ecf20Sopenharmony_ci	.attrs = lm87_attributes_vid,
8058c2ecf20Sopenharmony_ci};
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci/* Return 0 if detection is successful, -ENODEV otherwise */
8088c2ecf20Sopenharmony_cistatic int lm87_detect(struct i2c_client *client, struct i2c_board_info *info)
8098c2ecf20Sopenharmony_ci{
8108c2ecf20Sopenharmony_ci	struct i2c_adapter *adapter = client->adapter;
8118c2ecf20Sopenharmony_ci	const char *name;
8128c2ecf20Sopenharmony_ci	u8 cid, rev;
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
8158c2ecf20Sopenharmony_ci		return -ENODEV;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	if (lm87_read_value(client, LM87_REG_CONFIG) & 0x80)
8188c2ecf20Sopenharmony_ci		return -ENODEV;
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	/* Now, we do the remaining detection. */
8218c2ecf20Sopenharmony_ci	cid = lm87_read_value(client, LM87_REG_COMPANY_ID);
8228c2ecf20Sopenharmony_ci	rev = lm87_read_value(client, LM87_REG_REVISION);
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	if (cid == 0x02			/* National Semiconductor */
8258c2ecf20Sopenharmony_ci	 && (rev >= 0x01 && rev <= 0x08))
8268c2ecf20Sopenharmony_ci		name = "lm87";
8278c2ecf20Sopenharmony_ci	else if (cid == 0x41		/* Analog Devices */
8288c2ecf20Sopenharmony_ci	      && (rev & 0xf0) == 0x10)
8298c2ecf20Sopenharmony_ci		name = "adm1024";
8308c2ecf20Sopenharmony_ci	else {
8318c2ecf20Sopenharmony_ci		dev_dbg(&adapter->dev, "LM87 detection failed at 0x%02x\n",
8328c2ecf20Sopenharmony_ci			client->addr);
8338c2ecf20Sopenharmony_ci		return -ENODEV;
8348c2ecf20Sopenharmony_ci	}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	strlcpy(info->type, name, I2C_NAME_SIZE);
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	return 0;
8398c2ecf20Sopenharmony_ci}
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_cistatic void lm87_restore_config(void *arg)
8428c2ecf20Sopenharmony_ci{
8438c2ecf20Sopenharmony_ci	struct i2c_client *client = arg;
8448c2ecf20Sopenharmony_ci	struct lm87_data *data = i2c_get_clientdata(client);
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	lm87_write_value(client, LM87_REG_CONFIG, data->config);
8478c2ecf20Sopenharmony_ci}
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_cistatic int lm87_init_client(struct i2c_client *client)
8508c2ecf20Sopenharmony_ci{
8518c2ecf20Sopenharmony_ci	struct lm87_data *data = i2c_get_clientdata(client);
8528c2ecf20Sopenharmony_ci	int rc;
8538c2ecf20Sopenharmony_ci	struct device_node *of_node = client->dev.of_node;
8548c2ecf20Sopenharmony_ci	u8 val = 0;
8558c2ecf20Sopenharmony_ci	struct regulator *vcc = NULL;
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	if (of_node) {
8588c2ecf20Sopenharmony_ci		if (of_property_read_bool(of_node, "has-temp3"))
8598c2ecf20Sopenharmony_ci			val |= CHAN_TEMP3;
8608c2ecf20Sopenharmony_ci		if (of_property_read_bool(of_node, "has-in6"))
8618c2ecf20Sopenharmony_ci			val |= CHAN_NO_FAN(0);
8628c2ecf20Sopenharmony_ci		if (of_property_read_bool(of_node, "has-in7"))
8638c2ecf20Sopenharmony_ci			val |= CHAN_NO_FAN(1);
8648c2ecf20Sopenharmony_ci		vcc = devm_regulator_get_optional(&client->dev, "vcc");
8658c2ecf20Sopenharmony_ci		if (!IS_ERR(vcc)) {
8668c2ecf20Sopenharmony_ci			if (regulator_get_voltage(vcc) == 5000000)
8678c2ecf20Sopenharmony_ci				val |= CHAN_VCC_5V;
8688c2ecf20Sopenharmony_ci		}
8698c2ecf20Sopenharmony_ci		data->channel = val;
8708c2ecf20Sopenharmony_ci		lm87_write_value(client,
8718c2ecf20Sopenharmony_ci				LM87_REG_CHANNEL_MODE, data->channel);
8728c2ecf20Sopenharmony_ci	} else if (dev_get_platdata(&client->dev)) {
8738c2ecf20Sopenharmony_ci		data->channel = *(u8 *)dev_get_platdata(&client->dev);
8748c2ecf20Sopenharmony_ci		lm87_write_value(client,
8758c2ecf20Sopenharmony_ci				 LM87_REG_CHANNEL_MODE, data->channel);
8768c2ecf20Sopenharmony_ci	} else {
8778c2ecf20Sopenharmony_ci		data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
8788c2ecf20Sopenharmony_ci	}
8798c2ecf20Sopenharmony_ci	data->config = lm87_read_value(client, LM87_REG_CONFIG) & 0x6F;
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	rc = devm_add_action(&client->dev, lm87_restore_config, client);
8828c2ecf20Sopenharmony_ci	if (rc)
8838c2ecf20Sopenharmony_ci		return rc;
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	if (!(data->config & 0x01)) {
8868c2ecf20Sopenharmony_ci		int i;
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci		/* Limits are left uninitialized after power-up */
8898c2ecf20Sopenharmony_ci		for (i = 1; i < 6; i++) {
8908c2ecf20Sopenharmony_ci			lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
8918c2ecf20Sopenharmony_ci			lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
8928c2ecf20Sopenharmony_ci		}
8938c2ecf20Sopenharmony_ci		for (i = 0; i < 2; i++) {
8948c2ecf20Sopenharmony_ci			lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
8958c2ecf20Sopenharmony_ci			lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
8968c2ecf20Sopenharmony_ci			lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
8978c2ecf20Sopenharmony_ci			lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
8988c2ecf20Sopenharmony_ci		}
8998c2ecf20Sopenharmony_ci		if (data->channel & CHAN_TEMP3) {
9008c2ecf20Sopenharmony_ci			lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
9018c2ecf20Sopenharmony_ci			lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
9028c2ecf20Sopenharmony_ci		} else {
9038c2ecf20Sopenharmony_ci			lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
9048c2ecf20Sopenharmony_ci			lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
9058c2ecf20Sopenharmony_ci		}
9068c2ecf20Sopenharmony_ci	}
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	/* Make sure Start is set and INT#_Clear is clear */
9098c2ecf20Sopenharmony_ci	if ((data->config & 0x09) != 0x01)
9108c2ecf20Sopenharmony_ci		lm87_write_value(client, LM87_REG_CONFIG,
9118c2ecf20Sopenharmony_ci				 (data->config & 0x77) | 0x01);
9128c2ecf20Sopenharmony_ci	return 0;
9138c2ecf20Sopenharmony_ci}
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_cistatic int lm87_probe(struct i2c_client *client)
9168c2ecf20Sopenharmony_ci{
9178c2ecf20Sopenharmony_ci	struct lm87_data *data;
9188c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
9198c2ecf20Sopenharmony_ci	int err;
9208c2ecf20Sopenharmony_ci	unsigned int group_tail = 0;
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci	data = devm_kzalloc(&client->dev, sizeof(struct lm87_data), GFP_KERNEL);
9238c2ecf20Sopenharmony_ci	if (!data)
9248c2ecf20Sopenharmony_ci		return -ENOMEM;
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, data);
9278c2ecf20Sopenharmony_ci	mutex_init(&data->update_lock);
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	/* Initialize the LM87 chip */
9308c2ecf20Sopenharmony_ci	err = lm87_init_client(client);
9318c2ecf20Sopenharmony_ci	if (err)
9328c2ecf20Sopenharmony_ci		return err;
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	data->in_scale[0] = 2500;
9358c2ecf20Sopenharmony_ci	data->in_scale[1] = 2700;
9368c2ecf20Sopenharmony_ci	data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
9378c2ecf20Sopenharmony_ci	data->in_scale[3] = 5000;
9388c2ecf20Sopenharmony_ci	data->in_scale[4] = 12000;
9398c2ecf20Sopenharmony_ci	data->in_scale[5] = 2700;
9408c2ecf20Sopenharmony_ci	data->in_scale[6] = 1875;
9418c2ecf20Sopenharmony_ci	data->in_scale[7] = 1875;
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci	/*
9448c2ecf20Sopenharmony_ci	 * Construct the list of attributes, the list depends on the
9458c2ecf20Sopenharmony_ci	 * configuration of the chip
9468c2ecf20Sopenharmony_ci	 */
9478c2ecf20Sopenharmony_ci	data->attr_groups[group_tail++] = &lm87_group;
9488c2ecf20Sopenharmony_ci	if (data->channel & CHAN_NO_FAN(0))
9498c2ecf20Sopenharmony_ci		data->attr_groups[group_tail++] = &lm87_group_in6;
9508c2ecf20Sopenharmony_ci	else
9518c2ecf20Sopenharmony_ci		data->attr_groups[group_tail++] = &lm87_group_fan1;
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci	if (data->channel & CHAN_NO_FAN(1))
9548c2ecf20Sopenharmony_ci		data->attr_groups[group_tail++] = &lm87_group_in7;
9558c2ecf20Sopenharmony_ci	else
9568c2ecf20Sopenharmony_ci		data->attr_groups[group_tail++] = &lm87_group_fan2;
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	if (data->channel & CHAN_TEMP3)
9598c2ecf20Sopenharmony_ci		data->attr_groups[group_tail++] = &lm87_group_temp3;
9608c2ecf20Sopenharmony_ci	else
9618c2ecf20Sopenharmony_ci		data->attr_groups[group_tail++] = &lm87_group_in0_5;
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci	if (!(data->channel & CHAN_NO_VID)) {
9648c2ecf20Sopenharmony_ci		data->vrm = vid_which_vrm();
9658c2ecf20Sopenharmony_ci		data->attr_groups[group_tail++] = &lm87_group_vid;
9668c2ecf20Sopenharmony_ci	}
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	hwmon_dev = devm_hwmon_device_register_with_groups(
9698c2ecf20Sopenharmony_ci	    &client->dev, client->name, client, data->attr_groups);
9708c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(hwmon_dev);
9718c2ecf20Sopenharmony_ci}
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci/*
9748c2ecf20Sopenharmony_ci * Driver data (common to all clients)
9758c2ecf20Sopenharmony_ci */
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_cistatic const struct i2c_device_id lm87_id[] = {
9788c2ecf20Sopenharmony_ci	{ "lm87", 0 },
9798c2ecf20Sopenharmony_ci	{ "adm1024", 0 },
9808c2ecf20Sopenharmony_ci	{ }
9818c2ecf20Sopenharmony_ci};
9828c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, lm87_id);
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_cistatic const struct of_device_id lm87_of_match[] = {
9858c2ecf20Sopenharmony_ci	{ .compatible = "ti,lm87" },
9868c2ecf20Sopenharmony_ci	{ .compatible = "adi,adm1024" },
9878c2ecf20Sopenharmony_ci	{ },
9888c2ecf20Sopenharmony_ci};
9898c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, lm87_of_match);
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_cistatic struct i2c_driver lm87_driver = {
9928c2ecf20Sopenharmony_ci	.class		= I2C_CLASS_HWMON,
9938c2ecf20Sopenharmony_ci	.driver = {
9948c2ecf20Sopenharmony_ci		.name	= "lm87",
9958c2ecf20Sopenharmony_ci		.of_match_table = lm87_of_match,
9968c2ecf20Sopenharmony_ci	},
9978c2ecf20Sopenharmony_ci	.probe_new	= lm87_probe,
9988c2ecf20Sopenharmony_ci	.id_table	= lm87_id,
9998c2ecf20Sopenharmony_ci	.detect		= lm87_detect,
10008c2ecf20Sopenharmony_ci	.address_list	= normal_i2c,
10018c2ecf20Sopenharmony_ci};
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_cimodule_i2c_driver(lm87_driver);
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jean Delvare <jdelvare@suse.de> and others");
10068c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("LM87 driver");
10078c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1008