18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * vt8231.c - Part of lm_sensors, Linux kernel modules
48c2ecf20Sopenharmony_ci *	      for hardware monitoring
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (c) 2005 Roger Lucas <vt8231@hiddenengine.co.uk>
78c2ecf20Sopenharmony_ci * Copyright (c) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
88c2ecf20Sopenharmony_ci *		      Aaron M. Marsh <amarsh@sdf.lonestar.org>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci * Supports VIA VT8231 South Bridge embedded sensors
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/module.h>
188c2ecf20Sopenharmony_ci#include <linux/init.h>
198c2ecf20Sopenharmony_ci#include <linux/slab.h>
208c2ecf20Sopenharmony_ci#include <linux/pci.h>
218c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
228c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
238c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
248c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
258c2ecf20Sopenharmony_ci#include <linux/hwmon-vid.h>
268c2ecf20Sopenharmony_ci#include <linux/err.h>
278c2ecf20Sopenharmony_ci#include <linux/mutex.h>
288c2ecf20Sopenharmony_ci#include <linux/acpi.h>
298c2ecf20Sopenharmony_ci#include <linux/io.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic int force_addr;
328c2ecf20Sopenharmony_cimodule_param(force_addr, int, 0);
338c2ecf20Sopenharmony_ciMODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic struct platform_device *pdev;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define VT8231_EXTENT 0x80
388c2ecf20Sopenharmony_ci#define VT8231_BASE_REG 0x70
398c2ecf20Sopenharmony_ci#define VT8231_ENABLE_REG 0x74
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/*
428c2ecf20Sopenharmony_ci * The VT8231 registers
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * The reset value for the input channel configuration is used (Reg 0x4A=0x07)
458c2ecf20Sopenharmony_ci * which sets the selected inputs marked with '*' below if multiple options are
468c2ecf20Sopenharmony_ci * possible:
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci *		    Voltage Mode	  Temperature Mode
498c2ecf20Sopenharmony_ci *	Sensor	      Linux Id	      Linux Id	      VIA Id
508c2ecf20Sopenharmony_ci *	--------      --------	      --------	      ------
518c2ecf20Sopenharmony_ci *	CPU Diode	N/A		temp1		0
528c2ecf20Sopenharmony_ci *	UIC1		in0		temp2 *		1
538c2ecf20Sopenharmony_ci *	UIC2		in1 *		temp3		2
548c2ecf20Sopenharmony_ci *	UIC3		in2 *		temp4		3
558c2ecf20Sopenharmony_ci *	UIC4		in3 *		temp5		4
568c2ecf20Sopenharmony_ci *	UIC5		in4 *		temp6		5
578c2ecf20Sopenharmony_ci *	3.3V		in5		N/A
588c2ecf20Sopenharmony_ci *
598c2ecf20Sopenharmony_ci * Note that the BIOS may set the configuration register to a different value
608c2ecf20Sopenharmony_ci * to match the motherboard configuration.
618c2ecf20Sopenharmony_ci */
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/* fans numbered 0-1 */
648c2ecf20Sopenharmony_ci#define VT8231_REG_FAN_MIN(nr)	(0x3b + (nr))
658c2ecf20Sopenharmony_ci#define VT8231_REG_FAN(nr)	(0x29 + (nr))
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/* Voltage inputs numbered 0-5 */
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic const u8 regvolt[]    = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
708c2ecf20Sopenharmony_cistatic const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
718c2ecf20Sopenharmony_cistatic const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/*
748c2ecf20Sopenharmony_ci * Temperatures are numbered 1-6 according to the Linux kernel specification.
758c2ecf20Sopenharmony_ci *
768c2ecf20Sopenharmony_ci * In the VIA datasheet, however, the temperatures are numbered from zero.
778c2ecf20Sopenharmony_ci * Since it is important that this driver can easily be compared to the VIA
788c2ecf20Sopenharmony_ci * datasheet, we will use the VIA numbering within this driver and map the
798c2ecf20Sopenharmony_ci * kernel sysfs device name to the VIA number in the sysfs callback.
808c2ecf20Sopenharmony_ci */
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define VT8231_REG_TEMP_LOW01	0x49
838c2ecf20Sopenharmony_ci#define VT8231_REG_TEMP_LOW25	0x4d
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic const u8 regtemp[]    = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
868c2ecf20Sopenharmony_cistatic const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
878c2ecf20Sopenharmony_cistatic const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define TEMP_FROM_REG(reg)		(((253 * 4 - (reg)) * 550 + 105) / 210)
908c2ecf20Sopenharmony_ci#define TEMP_MAXMIN_FROM_REG(reg)	(((253 - (reg)) * 2200 + 105) / 210)
918c2ecf20Sopenharmony_ci#define TEMP_MAXMIN_TO_REG(val)		(253 - ((val) * 210 + 1100) / 2200)
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci#define VT8231_REG_CONFIG 0x40
948c2ecf20Sopenharmony_ci#define VT8231_REG_ALARM1 0x41
958c2ecf20Sopenharmony_ci#define VT8231_REG_ALARM2 0x42
968c2ecf20Sopenharmony_ci#define VT8231_REG_FANDIV 0x47
978c2ecf20Sopenharmony_ci#define VT8231_REG_UCH_CONFIG 0x4a
988c2ecf20Sopenharmony_ci#define VT8231_REG_TEMP1_CONFIG 0x4b
998c2ecf20Sopenharmony_ci#define VT8231_REG_TEMP2_CONFIG 0x4c
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/*
1028c2ecf20Sopenharmony_ci * temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
1038c2ecf20Sopenharmony_ci * numbering
1048c2ecf20Sopenharmony_ci */
1058c2ecf20Sopenharmony_ci#define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \
1068c2ecf20Sopenharmony_ci			      ((ch_config) >> ((i)+1)) & 0x01)
1078c2ecf20Sopenharmony_ci/* voltages 0-5 */
1088c2ecf20Sopenharmony_ci#define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \
1098c2ecf20Sopenharmony_ci			      !(((ch_config) >> ((i)+2)) & 0x01))
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci#define DIV_FROM_REG(val) (1 << (val))
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/*
1148c2ecf20Sopenharmony_ci * NB  The values returned here are NOT temperatures.  The calibration curves
1158c2ecf20Sopenharmony_ci *     for the thermistor curves are board-specific and must go in the
1168c2ecf20Sopenharmony_ci *     sensors.conf file.  Temperature sensors are actually ten bits, but the
1178c2ecf20Sopenharmony_ci *     VIA datasheet only considers the 8 MSBs obtained from the regtemp[]
1188c2ecf20Sopenharmony_ci *     register.  The temperature value returned should have a magnitude of 3,
1198c2ecf20Sopenharmony_ci *     so we use the VIA scaling as the "true" scaling and use the remaining 2
1208c2ecf20Sopenharmony_ci *     LSBs as fractional precision.
1218c2ecf20Sopenharmony_ci *
1228c2ecf20Sopenharmony_ci *     All the on-chip hardware temperature comparisons for the alarms are only
1238c2ecf20Sopenharmony_ci *     8-bits wide, and compare against the 8 MSBs of the temperature.  The bits
1248c2ecf20Sopenharmony_ci *     in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are
1258c2ecf20Sopenharmony_ci *     ignored.
1268c2ecf20Sopenharmony_ci */
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/*
1298c2ecf20Sopenharmony_ci ****** FAN RPM CONVERSIONS ********
1308c2ecf20Sopenharmony_ci * This chip saturates back at 0, not at 255 like many the other chips.
1318c2ecf20Sopenharmony_ci * So, 0 means 0 RPM
1328c2ecf20Sopenharmony_ci */
1338c2ecf20Sopenharmony_cistatic inline u8 FAN_TO_REG(long rpm, int div)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	if (rpm <= 0 || rpm > 1310720)
1368c2ecf20Sopenharmony_ci		return 0;
1378c2ecf20Sopenharmony_ci	return clamp_val(1310720 / (rpm * div), 1, 255);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistruct vt8231_data {
1438c2ecf20Sopenharmony_ci	unsigned short addr;
1448c2ecf20Sopenharmony_ci	const char *name;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	struct mutex update_lock;
1478c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
1488c2ecf20Sopenharmony_ci	char valid;		/* !=0 if following fields are valid */
1498c2ecf20Sopenharmony_ci	unsigned long last_updated;	/* In jiffies */
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	u8 in[6];		/* Register value */
1528c2ecf20Sopenharmony_ci	u8 in_max[6];		/* Register value */
1538c2ecf20Sopenharmony_ci	u8 in_min[6];		/* Register value */
1548c2ecf20Sopenharmony_ci	u16 temp[6];		/* Register value 10 bit, right aligned */
1558c2ecf20Sopenharmony_ci	u8 temp_max[6];		/* Register value */
1568c2ecf20Sopenharmony_ci	u8 temp_min[6];		/* Register value */
1578c2ecf20Sopenharmony_ci	u8 fan[2];		/* Register value */
1588c2ecf20Sopenharmony_ci	u8 fan_min[2];		/* Register value */
1598c2ecf20Sopenharmony_ci	u8 fan_div[2];		/* Register encoding, shifted right */
1608c2ecf20Sopenharmony_ci	u16 alarms;		/* Register encoding */
1618c2ecf20Sopenharmony_ci	u8 uch_config;
1628c2ecf20Sopenharmony_ci};
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic struct pci_dev *s_bridge;
1658c2ecf20Sopenharmony_cistatic int vt8231_probe(struct platform_device *pdev);
1668c2ecf20Sopenharmony_cistatic int vt8231_remove(struct platform_device *pdev);
1678c2ecf20Sopenharmony_cistatic struct vt8231_data *vt8231_update_device(struct device *dev);
1688c2ecf20Sopenharmony_cistatic void vt8231_init_device(struct vt8231_data *data);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic inline int vt8231_read_value(struct vt8231_data *data, u8 reg)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	return inb_p(data->addr + reg);
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic inline void vt8231_write_value(struct vt8231_data *data, u8 reg,
1768c2ecf20Sopenharmony_ci					u8 value)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	outb_p(value, data->addr + reg);
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci/* following are the sysfs callback functions */
1828c2ecf20Sopenharmony_cistatic ssize_t in_show(struct device *dev, struct device_attribute *attr,
1838c2ecf20Sopenharmony_ci		       char *buf)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1868c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
1878c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958);
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
1938c2ecf20Sopenharmony_ci			   char *buf)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1968c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
1978c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958);
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
2038c2ecf20Sopenharmony_ci			   char *buf)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2068c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
2078c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958));
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
2138c2ecf20Sopenharmony_ci			    const char *buf, size_t count)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2168c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
2178c2ecf20Sopenharmony_ci	struct vt8231_data *data = dev_get_drvdata(dev);
2188c2ecf20Sopenharmony_ci	unsigned long val;
2198c2ecf20Sopenharmony_ci	int err;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
2228c2ecf20Sopenharmony_ci	if (err)
2238c2ecf20Sopenharmony_ci		return err;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2268c2ecf20Sopenharmony_ci	data->in_min[nr] = clamp_val(((val * 958) / 10000) + 3, 0, 255);
2278c2ecf20Sopenharmony_ci	vt8231_write_value(data, regvoltmin[nr], data->in_min[nr]);
2288c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2298c2ecf20Sopenharmony_ci	return count;
2308c2ecf20Sopenharmony_ci}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistatic ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
2338c2ecf20Sopenharmony_ci			    const char *buf, size_t count)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2368c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
2378c2ecf20Sopenharmony_ci	struct vt8231_data *data = dev_get_drvdata(dev);
2388c2ecf20Sopenharmony_ci	unsigned long val;
2398c2ecf20Sopenharmony_ci	int err;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
2428c2ecf20Sopenharmony_ci	if (err)
2438c2ecf20Sopenharmony_ci		return err;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2468c2ecf20Sopenharmony_ci	data->in_max[nr] = clamp_val(((val * 958) / 10000) + 3, 0, 255);
2478c2ecf20Sopenharmony_ci	vt8231_write_value(data, regvoltmax[nr], data->in_max[nr]);
2488c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2498c2ecf20Sopenharmony_ci	return count;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci/* Special case for input 5 as this has 3.3V scaling built into the chip */
2538c2ecf20Sopenharmony_cistatic ssize_t in5_input_show(struct device *dev,
2548c2ecf20Sopenharmony_ci			      struct device_attribute *attr, char *buf)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n",
2598c2ecf20Sopenharmony_ci		(((data->in[5] - 3) * 10000 * 54) / (958 * 34)));
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_cistatic ssize_t in5_min_show(struct device *dev, struct device_attribute *attr,
2638c2ecf20Sopenharmony_ci		char *buf)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n",
2688c2ecf20Sopenharmony_ci		(((data->in_min[5] - 3) * 10000 * 54) / (958 * 34)));
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_cistatic ssize_t in5_max_show(struct device *dev, struct device_attribute *attr,
2728c2ecf20Sopenharmony_ci		char *buf)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n",
2778c2ecf20Sopenharmony_ci		(((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
2788c2ecf20Sopenharmony_ci}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_cistatic ssize_t in5_min_store(struct device *dev,
2818c2ecf20Sopenharmony_ci			     struct device_attribute *attr, const char *buf,
2828c2ecf20Sopenharmony_ci			     size_t count)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	struct vt8231_data *data = dev_get_drvdata(dev);
2858c2ecf20Sopenharmony_ci	unsigned long val;
2868c2ecf20Sopenharmony_ci	int err;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
2898c2ecf20Sopenharmony_ci	if (err)
2908c2ecf20Sopenharmony_ci		return err;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2938c2ecf20Sopenharmony_ci	data->in_min[5] = clamp_val(((val * 958 * 34) / (10000 * 54)) + 3,
2948c2ecf20Sopenharmony_ci				    0, 255);
2958c2ecf20Sopenharmony_ci	vt8231_write_value(data, regvoltmin[5], data->in_min[5]);
2968c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2978c2ecf20Sopenharmony_ci	return count;
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic ssize_t in5_max_store(struct device *dev,
3018c2ecf20Sopenharmony_ci			     struct device_attribute *attr, const char *buf,
3028c2ecf20Sopenharmony_ci			     size_t count)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	struct vt8231_data *data = dev_get_drvdata(dev);
3058c2ecf20Sopenharmony_ci	unsigned long val;
3068c2ecf20Sopenharmony_ci	int err;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
3098c2ecf20Sopenharmony_ci	if (err)
3108c2ecf20Sopenharmony_ci		return err;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3138c2ecf20Sopenharmony_ci	data->in_max[5] = clamp_val(((val * 958 * 34) / (10000 * 54)) + 3,
3148c2ecf20Sopenharmony_ci				    0, 255);
3158c2ecf20Sopenharmony_ci	vt8231_write_value(data, regvoltmax[5], data->in_max[5]);
3168c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3178c2ecf20Sopenharmony_ci	return count;
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
3218c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
3228c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
3238c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
3248c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
3258c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
3268c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
3278c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
3288c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
3298c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
3308c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
3318c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
3328c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
3338c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
3348c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(in5_input);
3378c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(in5_min);
3388c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(in5_max);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci/* Temperatures */
3418c2ecf20Sopenharmony_cistatic ssize_t temp1_input_show(struct device *dev,
3428c2ecf20Sopenharmony_ci				struct device_attribute *attr, char *buf)
3438c2ecf20Sopenharmony_ci{
3448c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
3458c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->temp[0] * 250);
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cistatic ssize_t temp1_max_show(struct device *dev, struct device_attribute *attr,
3498c2ecf20Sopenharmony_ci		char *buf)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
3528c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_cistatic ssize_t temp1_max_hyst_show(struct device *dev,
3568c2ecf20Sopenharmony_ci				   struct device_attribute *attr, char *buf)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
3598c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_cistatic ssize_t temp1_max_store(struct device *dev,
3638c2ecf20Sopenharmony_ci			       struct device_attribute *attr, const char *buf,
3648c2ecf20Sopenharmony_ci			       size_t count)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	struct vt8231_data *data = dev_get_drvdata(dev);
3678c2ecf20Sopenharmony_ci	long val;
3688c2ecf20Sopenharmony_ci	int err;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
3718c2ecf20Sopenharmony_ci	if (err)
3728c2ecf20Sopenharmony_ci		return err;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3758c2ecf20Sopenharmony_ci	data->temp_max[0] = clamp_val((val + 500) / 1000, 0, 255);
3768c2ecf20Sopenharmony_ci	vt8231_write_value(data, regtempmax[0], data->temp_max[0]);
3778c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3788c2ecf20Sopenharmony_ci	return count;
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_cistatic ssize_t temp1_max_hyst_store(struct device *dev,
3818c2ecf20Sopenharmony_ci				    struct device_attribute *attr,
3828c2ecf20Sopenharmony_ci				    const char *buf, size_t count)
3838c2ecf20Sopenharmony_ci{
3848c2ecf20Sopenharmony_ci	struct vt8231_data *data = dev_get_drvdata(dev);
3858c2ecf20Sopenharmony_ci	long val;
3868c2ecf20Sopenharmony_ci	int err;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
3898c2ecf20Sopenharmony_ci	if (err)
3908c2ecf20Sopenharmony_ci		return err;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3938c2ecf20Sopenharmony_ci	data->temp_min[0] = clamp_val((val + 500) / 1000, 0, 255);
3948c2ecf20Sopenharmony_ci	vt8231_write_value(data, regtempmin[0], data->temp_min[0]);
3958c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3968c2ecf20Sopenharmony_ci	return count;
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_cistatic ssize_t temp_show(struct device *dev, struct device_attribute *attr,
4008c2ecf20Sopenharmony_ci			 char *buf)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
4038c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
4048c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
4058c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
4068c2ecf20Sopenharmony_ci}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_cistatic ssize_t temp_max_show(struct device *dev,
4098c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
4108c2ecf20Sopenharmony_ci{
4118c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
4128c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
4138c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
4148c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr]));
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_cistatic ssize_t temp_min_show(struct device *dev,
4188c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
4198c2ecf20Sopenharmony_ci{
4208c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
4218c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
4228c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
4238c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr]));
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_cistatic ssize_t temp_max_store(struct device *dev,
4278c2ecf20Sopenharmony_ci			      struct device_attribute *attr, const char *buf,
4288c2ecf20Sopenharmony_ci			      size_t count)
4298c2ecf20Sopenharmony_ci{
4308c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
4318c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
4328c2ecf20Sopenharmony_ci	struct vt8231_data *data = dev_get_drvdata(dev);
4338c2ecf20Sopenharmony_ci	long val;
4348c2ecf20Sopenharmony_ci	int err;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
4378c2ecf20Sopenharmony_ci	if (err)
4388c2ecf20Sopenharmony_ci		return err;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
4418c2ecf20Sopenharmony_ci	data->temp_max[nr] = clamp_val(TEMP_MAXMIN_TO_REG(val), 0, 255);
4428c2ecf20Sopenharmony_ci	vt8231_write_value(data, regtempmax[nr], data->temp_max[nr]);
4438c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
4448c2ecf20Sopenharmony_ci	return count;
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_cistatic ssize_t temp_min_store(struct device *dev,
4478c2ecf20Sopenharmony_ci			      struct device_attribute *attr, const char *buf,
4488c2ecf20Sopenharmony_ci			      size_t count)
4498c2ecf20Sopenharmony_ci{
4508c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
4518c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
4528c2ecf20Sopenharmony_ci	struct vt8231_data *data = dev_get_drvdata(dev);
4538c2ecf20Sopenharmony_ci	long val;
4548c2ecf20Sopenharmony_ci	int err;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
4578c2ecf20Sopenharmony_ci	if (err)
4588c2ecf20Sopenharmony_ci		return err;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
4618c2ecf20Sopenharmony_ci	data->temp_min[nr] = clamp_val(TEMP_MAXMIN_TO_REG(val), 0, 255);
4628c2ecf20Sopenharmony_ci	vt8231_write_value(data, regtempmin[nr], data->temp_min[nr]);
4638c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
4648c2ecf20Sopenharmony_ci	return count;
4658c2ecf20Sopenharmony_ci}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci/*
4688c2ecf20Sopenharmony_ci * Note that these map the Linux temperature sensor numbering (1-6) to the VIA
4698c2ecf20Sopenharmony_ci * temperature sensor numbering (0-5)
4708c2ecf20Sopenharmony_ci */
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(temp1_input);
4738c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(temp1_max);
4748c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(temp1_max_hyst);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
4778c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
4788c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_max_hyst, temp_min, 1);
4798c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
4808c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
4818c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_max_hyst, temp_min, 2);
4828c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
4838c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
4848c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp4_max_hyst, temp_min, 3);
4858c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
4868c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp5_max, temp_max, 4);
4878c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp5_max_hyst, temp_min, 4);
4888c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp6_input, temp, 5);
4898c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp6_max, temp_max, 5);
4908c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp6_max_hyst, temp_min, 5);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci/* Fans */
4938c2ecf20Sopenharmony_cistatic ssize_t fan_show(struct device *dev, struct device_attribute *attr,
4948c2ecf20Sopenharmony_ci			char *buf)
4958c2ecf20Sopenharmony_ci{
4968c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
4978c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
4988c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
4998c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
5008c2ecf20Sopenharmony_ci				DIV_FROM_REG(data->fan_div[nr])));
5018c2ecf20Sopenharmony_ci}
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_cistatic ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
5048c2ecf20Sopenharmony_ci			    char *buf)
5058c2ecf20Sopenharmony_ci{
5068c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
5078c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
5088c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
5098c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
5108c2ecf20Sopenharmony_ci			DIV_FROM_REG(data->fan_div[nr])));
5118c2ecf20Sopenharmony_ci}
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_cistatic ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
5148c2ecf20Sopenharmony_ci			    char *buf)
5158c2ecf20Sopenharmony_ci{
5168c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
5178c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
5188c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
5198c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
5208c2ecf20Sopenharmony_ci}
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_cistatic ssize_t fan_min_store(struct device *dev,
5238c2ecf20Sopenharmony_ci			     struct device_attribute *attr, const char *buf,
5248c2ecf20Sopenharmony_ci			     size_t count)
5258c2ecf20Sopenharmony_ci{
5268c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
5278c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
5288c2ecf20Sopenharmony_ci	struct vt8231_data *data = dev_get_drvdata(dev);
5298c2ecf20Sopenharmony_ci	unsigned long val;
5308c2ecf20Sopenharmony_ci	int err;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
5338c2ecf20Sopenharmony_ci	if (err)
5348c2ecf20Sopenharmony_ci		return err;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
5378c2ecf20Sopenharmony_ci	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
5388c2ecf20Sopenharmony_ci	vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
5398c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
5408c2ecf20Sopenharmony_ci	return count;
5418c2ecf20Sopenharmony_ci}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_cistatic ssize_t fan_div_store(struct device *dev,
5448c2ecf20Sopenharmony_ci			     struct device_attribute *attr, const char *buf,
5458c2ecf20Sopenharmony_ci			     size_t count)
5468c2ecf20Sopenharmony_ci{
5478c2ecf20Sopenharmony_ci	struct vt8231_data *data = dev_get_drvdata(dev);
5488c2ecf20Sopenharmony_ci	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
5498c2ecf20Sopenharmony_ci	unsigned long val;
5508c2ecf20Sopenharmony_ci	int nr = sensor_attr->index;
5518c2ecf20Sopenharmony_ci	int old = vt8231_read_value(data, VT8231_REG_FANDIV);
5528c2ecf20Sopenharmony_ci	long min = FAN_FROM_REG(data->fan_min[nr],
5538c2ecf20Sopenharmony_ci				 DIV_FROM_REG(data->fan_div[nr]));
5548c2ecf20Sopenharmony_ci	int err;
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
5578c2ecf20Sopenharmony_ci	if (err)
5588c2ecf20Sopenharmony_ci		return err;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
5618c2ecf20Sopenharmony_ci	switch (val) {
5628c2ecf20Sopenharmony_ci	case 1:
5638c2ecf20Sopenharmony_ci		data->fan_div[nr] = 0;
5648c2ecf20Sopenharmony_ci		break;
5658c2ecf20Sopenharmony_ci	case 2:
5668c2ecf20Sopenharmony_ci		data->fan_div[nr] = 1;
5678c2ecf20Sopenharmony_ci		break;
5688c2ecf20Sopenharmony_ci	case 4:
5698c2ecf20Sopenharmony_ci		data->fan_div[nr] = 2;
5708c2ecf20Sopenharmony_ci		break;
5718c2ecf20Sopenharmony_ci	case 8:
5728c2ecf20Sopenharmony_ci		data->fan_div[nr] = 3;
5738c2ecf20Sopenharmony_ci		break;
5748c2ecf20Sopenharmony_ci	default:
5758c2ecf20Sopenharmony_ci		dev_err(dev,
5768c2ecf20Sopenharmony_ci			"fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
5778c2ecf20Sopenharmony_ci			val);
5788c2ecf20Sopenharmony_ci		mutex_unlock(&data->update_lock);
5798c2ecf20Sopenharmony_ci		return -EINVAL;
5808c2ecf20Sopenharmony_ci	}
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	/* Correct the fan minimum speed */
5838c2ecf20Sopenharmony_ci	data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
5848c2ecf20Sopenharmony_ci	vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
5878c2ecf20Sopenharmony_ci	vt8231_write_value(data, VT8231_REG_FANDIV, old);
5888c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
5898c2ecf20Sopenharmony_ci	return count;
5908c2ecf20Sopenharmony_ci}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
5938c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
5948c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
5958c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
5968c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
5978c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci/* Alarms */
6008c2ecf20Sopenharmony_cistatic ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
6018c2ecf20Sopenharmony_ci			   char *buf)
6028c2ecf20Sopenharmony_ci{
6038c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
6048c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->alarms);
6058c2ecf20Sopenharmony_ci}
6068c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(alarms);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_cistatic ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
6098c2ecf20Sopenharmony_ci			  char *buf)
6108c2ecf20Sopenharmony_ci{
6118c2ecf20Sopenharmony_ci	int bitnr = to_sensor_dev_attr(attr)->index;
6128c2ecf20Sopenharmony_ci	struct vt8231_data *data = vt8231_update_device(dev);
6138c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
6148c2ecf20Sopenharmony_ci}
6158c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
6168c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 11);
6178c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 0);
6188c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp4_alarm, alarm, 1);
6198c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp5_alarm, alarm, 3);
6208c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp6_alarm, alarm, 8);
6218c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 11);
6228c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 0);
6238c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 1);
6248c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
6258c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
6268c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 2);
6278c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
6288c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_cistatic ssize_t name_show(struct device *dev, struct device_attribute
6318c2ecf20Sopenharmony_ci			 *devattr, char *buf)
6328c2ecf20Sopenharmony_ci{
6338c2ecf20Sopenharmony_ci	struct vt8231_data *data = dev_get_drvdata(dev);
6348c2ecf20Sopenharmony_ci	return sprintf(buf, "%s\n", data->name);
6358c2ecf20Sopenharmony_ci}
6368c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(name);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_cistatic struct attribute *vt8231_attributes_temps[6][5] = {
6398c2ecf20Sopenharmony_ci	{
6408c2ecf20Sopenharmony_ci		&dev_attr_temp1_input.attr,
6418c2ecf20Sopenharmony_ci		&dev_attr_temp1_max_hyst.attr,
6428c2ecf20Sopenharmony_ci		&dev_attr_temp1_max.attr,
6438c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp1_alarm.dev_attr.attr,
6448c2ecf20Sopenharmony_ci		NULL
6458c2ecf20Sopenharmony_ci	}, {
6468c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp2_input.dev_attr.attr,
6478c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
6488c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp2_max.dev_attr.attr,
6498c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp2_alarm.dev_attr.attr,
6508c2ecf20Sopenharmony_ci		NULL
6518c2ecf20Sopenharmony_ci	}, {
6528c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp3_input.dev_attr.attr,
6538c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
6548c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp3_max.dev_attr.attr,
6558c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp3_alarm.dev_attr.attr,
6568c2ecf20Sopenharmony_ci		NULL
6578c2ecf20Sopenharmony_ci	}, {
6588c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp4_input.dev_attr.attr,
6598c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
6608c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp4_max.dev_attr.attr,
6618c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp4_alarm.dev_attr.attr,
6628c2ecf20Sopenharmony_ci		NULL
6638c2ecf20Sopenharmony_ci	}, {
6648c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp5_input.dev_attr.attr,
6658c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
6668c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp5_max.dev_attr.attr,
6678c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp5_alarm.dev_attr.attr,
6688c2ecf20Sopenharmony_ci		NULL
6698c2ecf20Sopenharmony_ci	}, {
6708c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp6_input.dev_attr.attr,
6718c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp6_max_hyst.dev_attr.attr,
6728c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp6_max.dev_attr.attr,
6738c2ecf20Sopenharmony_ci		&sensor_dev_attr_temp6_alarm.dev_attr.attr,
6748c2ecf20Sopenharmony_ci		NULL
6758c2ecf20Sopenharmony_ci	}
6768c2ecf20Sopenharmony_ci};
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_cistatic const struct attribute_group vt8231_group_temps[6] = {
6798c2ecf20Sopenharmony_ci	{ .attrs = vt8231_attributes_temps[0] },
6808c2ecf20Sopenharmony_ci	{ .attrs = vt8231_attributes_temps[1] },
6818c2ecf20Sopenharmony_ci	{ .attrs = vt8231_attributes_temps[2] },
6828c2ecf20Sopenharmony_ci	{ .attrs = vt8231_attributes_temps[3] },
6838c2ecf20Sopenharmony_ci	{ .attrs = vt8231_attributes_temps[4] },
6848c2ecf20Sopenharmony_ci	{ .attrs = vt8231_attributes_temps[5] },
6858c2ecf20Sopenharmony_ci};
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_cistatic struct attribute *vt8231_attributes_volts[6][5] = {
6888c2ecf20Sopenharmony_ci	{
6898c2ecf20Sopenharmony_ci		&sensor_dev_attr_in0_input.dev_attr.attr,
6908c2ecf20Sopenharmony_ci		&sensor_dev_attr_in0_min.dev_attr.attr,
6918c2ecf20Sopenharmony_ci		&sensor_dev_attr_in0_max.dev_attr.attr,
6928c2ecf20Sopenharmony_ci		&sensor_dev_attr_in0_alarm.dev_attr.attr,
6938c2ecf20Sopenharmony_ci		NULL
6948c2ecf20Sopenharmony_ci	}, {
6958c2ecf20Sopenharmony_ci		&sensor_dev_attr_in1_input.dev_attr.attr,
6968c2ecf20Sopenharmony_ci		&sensor_dev_attr_in1_min.dev_attr.attr,
6978c2ecf20Sopenharmony_ci		&sensor_dev_attr_in1_max.dev_attr.attr,
6988c2ecf20Sopenharmony_ci		&sensor_dev_attr_in1_alarm.dev_attr.attr,
6998c2ecf20Sopenharmony_ci		NULL
7008c2ecf20Sopenharmony_ci	}, {
7018c2ecf20Sopenharmony_ci		&sensor_dev_attr_in2_input.dev_attr.attr,
7028c2ecf20Sopenharmony_ci		&sensor_dev_attr_in2_min.dev_attr.attr,
7038c2ecf20Sopenharmony_ci		&sensor_dev_attr_in2_max.dev_attr.attr,
7048c2ecf20Sopenharmony_ci		&sensor_dev_attr_in2_alarm.dev_attr.attr,
7058c2ecf20Sopenharmony_ci		NULL
7068c2ecf20Sopenharmony_ci	}, {
7078c2ecf20Sopenharmony_ci		&sensor_dev_attr_in3_input.dev_attr.attr,
7088c2ecf20Sopenharmony_ci		&sensor_dev_attr_in3_min.dev_attr.attr,
7098c2ecf20Sopenharmony_ci		&sensor_dev_attr_in3_max.dev_attr.attr,
7108c2ecf20Sopenharmony_ci		&sensor_dev_attr_in3_alarm.dev_attr.attr,
7118c2ecf20Sopenharmony_ci		NULL
7128c2ecf20Sopenharmony_ci	}, {
7138c2ecf20Sopenharmony_ci		&sensor_dev_attr_in4_input.dev_attr.attr,
7148c2ecf20Sopenharmony_ci		&sensor_dev_attr_in4_min.dev_attr.attr,
7158c2ecf20Sopenharmony_ci		&sensor_dev_attr_in4_max.dev_attr.attr,
7168c2ecf20Sopenharmony_ci		&sensor_dev_attr_in4_alarm.dev_attr.attr,
7178c2ecf20Sopenharmony_ci		NULL
7188c2ecf20Sopenharmony_ci	}, {
7198c2ecf20Sopenharmony_ci		&dev_attr_in5_input.attr,
7208c2ecf20Sopenharmony_ci		&dev_attr_in5_min.attr,
7218c2ecf20Sopenharmony_ci		&dev_attr_in5_max.attr,
7228c2ecf20Sopenharmony_ci		&sensor_dev_attr_in5_alarm.dev_attr.attr,
7238c2ecf20Sopenharmony_ci		NULL
7248c2ecf20Sopenharmony_ci	}
7258c2ecf20Sopenharmony_ci};
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_cistatic const struct attribute_group vt8231_group_volts[6] = {
7288c2ecf20Sopenharmony_ci	{ .attrs = vt8231_attributes_volts[0] },
7298c2ecf20Sopenharmony_ci	{ .attrs = vt8231_attributes_volts[1] },
7308c2ecf20Sopenharmony_ci	{ .attrs = vt8231_attributes_volts[2] },
7318c2ecf20Sopenharmony_ci	{ .attrs = vt8231_attributes_volts[3] },
7328c2ecf20Sopenharmony_ci	{ .attrs = vt8231_attributes_volts[4] },
7338c2ecf20Sopenharmony_ci	{ .attrs = vt8231_attributes_volts[5] },
7348c2ecf20Sopenharmony_ci};
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_cistatic struct attribute *vt8231_attributes[] = {
7378c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_input.dev_attr.attr,
7388c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_input.dev_attr.attr,
7398c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_min.dev_attr.attr,
7408c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_min.dev_attr.attr,
7418c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_div.dev_attr.attr,
7428c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_div.dev_attr.attr,
7438c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
7448c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
7458c2ecf20Sopenharmony_ci	&dev_attr_alarms.attr,
7468c2ecf20Sopenharmony_ci	&dev_attr_name.attr,
7478c2ecf20Sopenharmony_ci	NULL
7488c2ecf20Sopenharmony_ci};
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_cistatic const struct attribute_group vt8231_group = {
7518c2ecf20Sopenharmony_ci	.attrs = vt8231_attributes,
7528c2ecf20Sopenharmony_ci};
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_cistatic struct platform_driver vt8231_driver = {
7558c2ecf20Sopenharmony_ci	.driver = {
7568c2ecf20Sopenharmony_ci		.name	= "vt8231",
7578c2ecf20Sopenharmony_ci	},
7588c2ecf20Sopenharmony_ci	.probe	= vt8231_probe,
7598c2ecf20Sopenharmony_ci	.remove	= vt8231_remove,
7608c2ecf20Sopenharmony_ci};
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_cistatic const struct pci_device_id vt8231_pci_ids[] = {
7638c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
7648c2ecf20Sopenharmony_ci	{ 0, }
7658c2ecf20Sopenharmony_ci};
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_cistatic int vt8231_pci_probe(struct pci_dev *dev,
7708c2ecf20Sopenharmony_ci				      const struct pci_device_id *id);
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_cistatic struct pci_driver vt8231_pci_driver = {
7738c2ecf20Sopenharmony_ci	.name		= "vt8231",
7748c2ecf20Sopenharmony_ci	.id_table	= vt8231_pci_ids,
7758c2ecf20Sopenharmony_ci	.probe		= vt8231_pci_probe,
7768c2ecf20Sopenharmony_ci};
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_cistatic int vt8231_probe(struct platform_device *pdev)
7798c2ecf20Sopenharmony_ci{
7808c2ecf20Sopenharmony_ci	struct resource *res;
7818c2ecf20Sopenharmony_ci	struct vt8231_data *data;
7828c2ecf20Sopenharmony_ci	int err = 0, i;
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	/* Reserve the ISA region */
7858c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
7868c2ecf20Sopenharmony_ci	if (!devm_request_region(&pdev->dev, res->start, VT8231_EXTENT,
7878c2ecf20Sopenharmony_ci				 vt8231_driver.driver.name)) {
7888c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n",
7898c2ecf20Sopenharmony_ci			(unsigned long)res->start, (unsigned long)res->end);
7908c2ecf20Sopenharmony_ci		return -ENODEV;
7918c2ecf20Sopenharmony_ci	}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	data = devm_kzalloc(&pdev->dev, sizeof(struct vt8231_data), GFP_KERNEL);
7948c2ecf20Sopenharmony_ci	if (!data)
7958c2ecf20Sopenharmony_ci		return -ENOMEM;
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, data);
7988c2ecf20Sopenharmony_ci	data->addr = res->start;
7998c2ecf20Sopenharmony_ci	data->name = "vt8231";
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	mutex_init(&data->update_lock);
8028c2ecf20Sopenharmony_ci	vt8231_init_device(data);
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	/* Register sysfs hooks */
8058c2ecf20Sopenharmony_ci	err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group);
8068c2ecf20Sopenharmony_ci	if (err)
8078c2ecf20Sopenharmony_ci		return err;
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	/* Must update device information to find out the config field */
8108c2ecf20Sopenharmony_ci	data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG);
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) {
8138c2ecf20Sopenharmony_ci		if (ISTEMP(i, data->uch_config)) {
8148c2ecf20Sopenharmony_ci			err = sysfs_create_group(&pdev->dev.kobj,
8158c2ecf20Sopenharmony_ci						 &vt8231_group_temps[i]);
8168c2ecf20Sopenharmony_ci			if (err)
8178c2ecf20Sopenharmony_ci				goto exit_remove_files;
8188c2ecf20Sopenharmony_ci		}
8198c2ecf20Sopenharmony_ci	}
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) {
8228c2ecf20Sopenharmony_ci		if (ISVOLT(i, data->uch_config)) {
8238c2ecf20Sopenharmony_ci			err = sysfs_create_group(&pdev->dev.kobj,
8248c2ecf20Sopenharmony_ci						 &vt8231_group_volts[i]);
8258c2ecf20Sopenharmony_ci			if (err)
8268c2ecf20Sopenharmony_ci				goto exit_remove_files;
8278c2ecf20Sopenharmony_ci		}
8288c2ecf20Sopenharmony_ci	}
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	data->hwmon_dev = hwmon_device_register(&pdev->dev);
8318c2ecf20Sopenharmony_ci	if (IS_ERR(data->hwmon_dev)) {
8328c2ecf20Sopenharmony_ci		err = PTR_ERR(data->hwmon_dev);
8338c2ecf20Sopenharmony_ci		goto exit_remove_files;
8348c2ecf20Sopenharmony_ci	}
8358c2ecf20Sopenharmony_ci	return 0;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ciexit_remove_files:
8388c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
8398c2ecf20Sopenharmony_ci		sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
8428c2ecf20Sopenharmony_ci		sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
8458c2ecf20Sopenharmony_ci	return err;
8468c2ecf20Sopenharmony_ci}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_cistatic int vt8231_remove(struct platform_device *pdev)
8498c2ecf20Sopenharmony_ci{
8508c2ecf20Sopenharmony_ci	struct vt8231_data *data = platform_get_drvdata(pdev);
8518c2ecf20Sopenharmony_ci	int i;
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	hwmon_device_unregister(data->hwmon_dev);
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
8568c2ecf20Sopenharmony_ci		sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
8598c2ecf20Sopenharmony_ci		sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	return 0;
8648c2ecf20Sopenharmony_ci}
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_cistatic void vt8231_init_device(struct vt8231_data *data)
8678c2ecf20Sopenharmony_ci{
8688c2ecf20Sopenharmony_ci	vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0);
8698c2ecf20Sopenharmony_ci	vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0);
8708c2ecf20Sopenharmony_ci}
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_cistatic struct vt8231_data *vt8231_update_device(struct device *dev)
8738c2ecf20Sopenharmony_ci{
8748c2ecf20Sopenharmony_ci	struct vt8231_data *data = dev_get_drvdata(dev);
8758c2ecf20Sopenharmony_ci	int i;
8768c2ecf20Sopenharmony_ci	u16 low;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
8818c2ecf20Sopenharmony_ci	    || !data->valid) {
8828c2ecf20Sopenharmony_ci		for (i = 0; i < 6; i++) {
8838c2ecf20Sopenharmony_ci			if (ISVOLT(i, data->uch_config)) {
8848c2ecf20Sopenharmony_ci				data->in[i] = vt8231_read_value(data,
8858c2ecf20Sopenharmony_ci						regvolt[i]);
8868c2ecf20Sopenharmony_ci				data->in_min[i] = vt8231_read_value(data,
8878c2ecf20Sopenharmony_ci						regvoltmin[i]);
8888c2ecf20Sopenharmony_ci				data->in_max[i] = vt8231_read_value(data,
8898c2ecf20Sopenharmony_ci						regvoltmax[i]);
8908c2ecf20Sopenharmony_ci			}
8918c2ecf20Sopenharmony_ci		}
8928c2ecf20Sopenharmony_ci		for (i = 0; i < 2; i++) {
8938c2ecf20Sopenharmony_ci			data->fan[i] = vt8231_read_value(data,
8948c2ecf20Sopenharmony_ci						VT8231_REG_FAN(i));
8958c2ecf20Sopenharmony_ci			data->fan_min[i] = vt8231_read_value(data,
8968c2ecf20Sopenharmony_ci						VT8231_REG_FAN_MIN(i));
8978c2ecf20Sopenharmony_ci		}
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci		low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01);
9008c2ecf20Sopenharmony_ci		low = (low >> 6) | ((low & 0x30) >> 2)
9018c2ecf20Sopenharmony_ci		    | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4);
9028c2ecf20Sopenharmony_ci		for (i = 0; i < 6; i++) {
9038c2ecf20Sopenharmony_ci			if (ISTEMP(i, data->uch_config)) {
9048c2ecf20Sopenharmony_ci				data->temp[i] = (vt8231_read_value(data,
9058c2ecf20Sopenharmony_ci						       regtemp[i]) << 2)
9068c2ecf20Sopenharmony_ci						| ((low >> (2 * i)) & 0x03);
9078c2ecf20Sopenharmony_ci				data->temp_max[i] = vt8231_read_value(data,
9088c2ecf20Sopenharmony_ci						      regtempmax[i]);
9098c2ecf20Sopenharmony_ci				data->temp_min[i] = vt8231_read_value(data,
9108c2ecf20Sopenharmony_ci						      regtempmin[i]);
9118c2ecf20Sopenharmony_ci			}
9128c2ecf20Sopenharmony_ci		}
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci		i = vt8231_read_value(data, VT8231_REG_FANDIV);
9158c2ecf20Sopenharmony_ci		data->fan_div[0] = (i >> 4) & 0x03;
9168c2ecf20Sopenharmony_ci		data->fan_div[1] = i >> 6;
9178c2ecf20Sopenharmony_ci		data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) |
9188c2ecf20Sopenharmony_ci			(vt8231_read_value(data, VT8231_REG_ALARM2) << 8);
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci		/* Set alarm flags correctly */
9218c2ecf20Sopenharmony_ci		if (!data->fan[0] && data->fan_min[0])
9228c2ecf20Sopenharmony_ci			data->alarms |= 0x40;
9238c2ecf20Sopenharmony_ci		else if (data->fan[0] && !data->fan_min[0])
9248c2ecf20Sopenharmony_ci			data->alarms &= ~0x40;
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci		if (!data->fan[1] && data->fan_min[1])
9278c2ecf20Sopenharmony_ci			data->alarms |= 0x80;
9288c2ecf20Sopenharmony_ci		else if (data->fan[1] && !data->fan_min[1])
9298c2ecf20Sopenharmony_ci			data->alarms &= ~0x80;
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci		data->last_updated = jiffies;
9328c2ecf20Sopenharmony_ci		data->valid = 1;
9338c2ecf20Sopenharmony_ci	}
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	return data;
9388c2ecf20Sopenharmony_ci}
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_cistatic int vt8231_device_add(unsigned short address)
9418c2ecf20Sopenharmony_ci{
9428c2ecf20Sopenharmony_ci	struct resource res = {
9438c2ecf20Sopenharmony_ci		.start	= address,
9448c2ecf20Sopenharmony_ci		.end	= address + VT8231_EXTENT - 1,
9458c2ecf20Sopenharmony_ci		.name	= "vt8231",
9468c2ecf20Sopenharmony_ci		.flags	= IORESOURCE_IO,
9478c2ecf20Sopenharmony_ci	};
9488c2ecf20Sopenharmony_ci	int err;
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci	err = acpi_check_resource_conflict(&res);
9518c2ecf20Sopenharmony_ci	if (err)
9528c2ecf20Sopenharmony_ci		goto exit;
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci	pdev = platform_device_alloc("vt8231", address);
9558c2ecf20Sopenharmony_ci	if (!pdev) {
9568c2ecf20Sopenharmony_ci		err = -ENOMEM;
9578c2ecf20Sopenharmony_ci		pr_err("Device allocation failed\n");
9588c2ecf20Sopenharmony_ci		goto exit;
9598c2ecf20Sopenharmony_ci	}
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci	err = platform_device_add_resources(pdev, &res, 1);
9628c2ecf20Sopenharmony_ci	if (err) {
9638c2ecf20Sopenharmony_ci		pr_err("Device resource addition failed (%d)\n", err);
9648c2ecf20Sopenharmony_ci		goto exit_device_put;
9658c2ecf20Sopenharmony_ci	}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	err = platform_device_add(pdev);
9688c2ecf20Sopenharmony_ci	if (err) {
9698c2ecf20Sopenharmony_ci		pr_err("Device addition failed (%d)\n", err);
9708c2ecf20Sopenharmony_ci		goto exit_device_put;
9718c2ecf20Sopenharmony_ci	}
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci	return 0;
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ciexit_device_put:
9768c2ecf20Sopenharmony_ci	platform_device_put(pdev);
9778c2ecf20Sopenharmony_ciexit:
9788c2ecf20Sopenharmony_ci	return err;
9798c2ecf20Sopenharmony_ci}
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_cistatic int vt8231_pci_probe(struct pci_dev *dev,
9828c2ecf20Sopenharmony_ci				const struct pci_device_id *id)
9838c2ecf20Sopenharmony_ci{
9848c2ecf20Sopenharmony_ci	u16 address, val;
9858c2ecf20Sopenharmony_ci	if (force_addr) {
9868c2ecf20Sopenharmony_ci		address = force_addr & 0xff00;
9878c2ecf20Sopenharmony_ci		dev_warn(&dev->dev, "Forcing ISA address 0x%x\n",
9888c2ecf20Sopenharmony_ci			 address);
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci		if (PCIBIOS_SUCCESSFUL !=
9918c2ecf20Sopenharmony_ci		    pci_write_config_word(dev, VT8231_BASE_REG, address | 1))
9928c2ecf20Sopenharmony_ci			return -ENODEV;
9938c2ecf20Sopenharmony_ci	}
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci	pci_read_config_word(dev, VT8231_BASE_REG, &val);
9968c2ecf20Sopenharmony_ci	if (val == (u16)~0)
9978c2ecf20Sopenharmony_ci		return -ENODEV;
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	address = val & ~(VT8231_EXTENT - 1);
10008c2ecf20Sopenharmony_ci	if (address == 0) {
10018c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "base address not set - upgrade BIOS or use force_addr=0xaddr\n");
10028c2ecf20Sopenharmony_ci		return -ENODEV;
10038c2ecf20Sopenharmony_ci	}
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	pci_read_config_word(dev, VT8231_ENABLE_REG, &val);
10068c2ecf20Sopenharmony_ci	if (val == (u16)~0)
10078c2ecf20Sopenharmony_ci		return -ENODEV;
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci	if (!(val & 0x0001)) {
10108c2ecf20Sopenharmony_ci		dev_warn(&dev->dev, "enabling sensors\n");
10118c2ecf20Sopenharmony_ci		if (PCIBIOS_SUCCESSFUL !=
10128c2ecf20Sopenharmony_ci			pci_write_config_word(dev, VT8231_ENABLE_REG,
10138c2ecf20Sopenharmony_ci							val | 0x0001))
10148c2ecf20Sopenharmony_ci			return -ENODEV;
10158c2ecf20Sopenharmony_ci	}
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_ci	if (platform_driver_register(&vt8231_driver))
10188c2ecf20Sopenharmony_ci		goto exit;
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_ci	/* Sets global pdev as a side effect */
10218c2ecf20Sopenharmony_ci	if (vt8231_device_add(address))
10228c2ecf20Sopenharmony_ci		goto exit_unregister;
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	/*
10258c2ecf20Sopenharmony_ci	 * Always return failure here.  This is to allow other drivers to bind
10268c2ecf20Sopenharmony_ci	 * to this pci device.  We don't really want to have control over the
10278c2ecf20Sopenharmony_ci	 * pci device, we only wanted to read as few register values from it.
10288c2ecf20Sopenharmony_ci	 */
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci	/*
10318c2ecf20Sopenharmony_ci	 * We do, however, mark ourselves as using the PCI device to stop it
10328c2ecf20Sopenharmony_ci	 * getting unloaded.
10338c2ecf20Sopenharmony_ci	 */
10348c2ecf20Sopenharmony_ci	s_bridge = pci_dev_get(dev);
10358c2ecf20Sopenharmony_ci	return -ENODEV;
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ciexit_unregister:
10388c2ecf20Sopenharmony_ci	platform_driver_unregister(&vt8231_driver);
10398c2ecf20Sopenharmony_ciexit:
10408c2ecf20Sopenharmony_ci	return -ENODEV;
10418c2ecf20Sopenharmony_ci}
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_cistatic int __init sm_vt8231_init(void)
10448c2ecf20Sopenharmony_ci{
10458c2ecf20Sopenharmony_ci	return pci_register_driver(&vt8231_pci_driver);
10468c2ecf20Sopenharmony_ci}
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_cistatic void __exit sm_vt8231_exit(void)
10498c2ecf20Sopenharmony_ci{
10508c2ecf20Sopenharmony_ci	pci_unregister_driver(&vt8231_pci_driver);
10518c2ecf20Sopenharmony_ci	if (s_bridge != NULL) {
10528c2ecf20Sopenharmony_ci		platform_device_unregister(pdev);
10538c2ecf20Sopenharmony_ci		platform_driver_unregister(&vt8231_driver);
10548c2ecf20Sopenharmony_ci		pci_dev_put(s_bridge);
10558c2ecf20Sopenharmony_ci		s_bridge = NULL;
10568c2ecf20Sopenharmony_ci	}
10578c2ecf20Sopenharmony_ci}
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ciMODULE_AUTHOR("Roger Lucas <vt8231@hiddenengine.co.uk>");
10608c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("VT8231 sensors");
10618c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_cimodule_init(sm_vt8231_init);
10648c2ecf20Sopenharmony_cimodule_exit(sm_vt8231_exit);
1065