18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * via686a.c - Part of lm_sensors, Linux kernel modules
48c2ecf20Sopenharmony_ci *	       for hardware monitoring
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (c) 1998 - 2002  Frodo Looijaard <frodol@dds.nl>,
78c2ecf20Sopenharmony_ci *			      Kyösti Mälkki <kmalkki@cc.hut.fi>,
88c2ecf20Sopenharmony_ci *			      Mark Studebaker <mdsxyz123@yahoo.com>,
98c2ecf20Sopenharmony_ci *			      and Bob Dougherty <bobd@stanford.edu>
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * (Some conversion-factor data were contributed by Jonathan Teh Soon Yew
128c2ecf20Sopenharmony_ci * <j.teh@iname.com> and Alex van Kaam <darkside@chello.nl>.)
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci * Supports the Via VT82C686A, VT82C686B south bridges.
178c2ecf20Sopenharmony_ci * Reports all as a 686A.
188c2ecf20Sopenharmony_ci * Warning - only supports a single device.
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <linux/module.h>
248c2ecf20Sopenharmony_ci#include <linux/slab.h>
258c2ecf20Sopenharmony_ci#include <linux/pci.h>
268c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
278c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
288c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
298c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
308c2ecf20Sopenharmony_ci#include <linux/err.h>
318c2ecf20Sopenharmony_ci#include <linux/init.h>
328c2ecf20Sopenharmony_ci#include <linux/mutex.h>
338c2ecf20Sopenharmony_ci#include <linux/sysfs.h>
348c2ecf20Sopenharmony_ci#include <linux/acpi.h>
358c2ecf20Sopenharmony_ci#include <linux/io.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/*
388c2ecf20Sopenharmony_ci * If force_addr is set to anything different from 0, we forcibly enable
398c2ecf20Sopenharmony_ci * the device at the given address.
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_cistatic unsigned short force_addr;
428c2ecf20Sopenharmony_cimodule_param(force_addr, ushort, 0);
438c2ecf20Sopenharmony_ciMODULE_PARM_DESC(force_addr,
448c2ecf20Sopenharmony_ci		 "Initialize the base address of the sensors");
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic struct platform_device *pdev;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/*
498c2ecf20Sopenharmony_ci * The Via 686a southbridge has a LM78-like chip integrated on the same IC.
508c2ecf20Sopenharmony_ci * This driver is a customized copy of lm78.c
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/* Many VIA686A constants specified below */
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* Length of ISA address segment */
568c2ecf20Sopenharmony_ci#define VIA686A_EXTENT		0x80
578c2ecf20Sopenharmony_ci#define VIA686A_BASE_REG	0x70
588c2ecf20Sopenharmony_ci#define VIA686A_ENABLE_REG	0x74
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/* The VIA686A registers */
618c2ecf20Sopenharmony_ci/* ins numbered 0-4 */
628c2ecf20Sopenharmony_ci#define VIA686A_REG_IN_MAX(nr)	(0x2b + ((nr) * 2))
638c2ecf20Sopenharmony_ci#define VIA686A_REG_IN_MIN(nr)	(0x2c + ((nr) * 2))
648c2ecf20Sopenharmony_ci#define VIA686A_REG_IN(nr)	(0x22 + (nr))
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/* fans numbered 1-2 */
678c2ecf20Sopenharmony_ci#define VIA686A_REG_FAN_MIN(nr)	(0x3a + (nr))
688c2ecf20Sopenharmony_ci#define VIA686A_REG_FAN(nr)	(0x28 + (nr))
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/* temps numbered 1-3 */
718c2ecf20Sopenharmony_cistatic const u8 VIA686A_REG_TEMP[]	= { 0x20, 0x21, 0x1f };
728c2ecf20Sopenharmony_cistatic const u8 VIA686A_REG_TEMP_OVER[]	= { 0x39, 0x3d, 0x1d };
738c2ecf20Sopenharmony_cistatic const u8 VIA686A_REG_TEMP_HYST[]	= { 0x3a, 0x3e, 0x1e };
748c2ecf20Sopenharmony_ci/* bits 7-6 */
758c2ecf20Sopenharmony_ci#define VIA686A_REG_TEMP_LOW1	0x4b
768c2ecf20Sopenharmony_ci/* 2 = bits 5-4, 3 = bits 7-6 */
778c2ecf20Sopenharmony_ci#define VIA686A_REG_TEMP_LOW23	0x49
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define VIA686A_REG_ALARM1	0x41
808c2ecf20Sopenharmony_ci#define VIA686A_REG_ALARM2	0x42
818c2ecf20Sopenharmony_ci#define VIA686A_REG_FANDIV	0x47
828c2ecf20Sopenharmony_ci#define VIA686A_REG_CONFIG	0x40
838c2ecf20Sopenharmony_ci/*
848c2ecf20Sopenharmony_ci * The following register sets temp interrupt mode (bits 1-0 for temp1,
858c2ecf20Sopenharmony_ci * 3-2 for temp2, 5-4 for temp3).  Modes are:
868c2ecf20Sopenharmony_ci * 00 interrupt stays as long as value is out-of-range
878c2ecf20Sopenharmony_ci * 01 interrupt is cleared once register is read (default)
888c2ecf20Sopenharmony_ci * 10 comparator mode- like 00, but ignores hysteresis
898c2ecf20Sopenharmony_ci * 11 same as 00
908c2ecf20Sopenharmony_ci */
918c2ecf20Sopenharmony_ci#define VIA686A_REG_TEMP_MODE		0x4b
928c2ecf20Sopenharmony_ci/* We'll just assume that you want to set all 3 simultaneously: */
938c2ecf20Sopenharmony_ci#define VIA686A_TEMP_MODE_MASK		0x3F
948c2ecf20Sopenharmony_ci#define VIA686A_TEMP_MODE_CONTINUOUS	0x00
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/*
978c2ecf20Sopenharmony_ci * Conversions. Limit checking is only done on the TO_REG
988c2ecf20Sopenharmony_ci * variants.
998c2ecf20Sopenharmony_ci *
1008c2ecf20Sopenharmony_ci ******** VOLTAGE CONVERSIONS (Bob Dougherty) ********
1018c2ecf20Sopenharmony_ci * From HWMon.cpp (Copyright 1998-2000 Jonathan Teh Soon Yew):
1028c2ecf20Sopenharmony_ci * voltagefactor[0]=1.25/2628; (2628/1.25=2102.4)   // Vccp
1038c2ecf20Sopenharmony_ci * voltagefactor[1]=1.25/2628; (2628/1.25=2102.4)   // +2.5V
1048c2ecf20Sopenharmony_ci * voltagefactor[2]=1.67/2628; (2628/1.67=1573.7)   // +3.3V
1058c2ecf20Sopenharmony_ci * voltagefactor[3]=2.6/2628;  (2628/2.60=1010.8)   // +5V
1068c2ecf20Sopenharmony_ci * voltagefactor[4]=6.3/2628;  (2628/6.30=417.14)   // +12V
1078c2ecf20Sopenharmony_ci * in[i]=(data[i+2]*25.0+133)*voltagefactor[i];
1088c2ecf20Sopenharmony_ci * That is:
1098c2ecf20Sopenharmony_ci * volts = (25*regVal+133)*factor
1108c2ecf20Sopenharmony_ci * regVal = (volts/factor-133)/25
1118c2ecf20Sopenharmony_ci * (These conversions were contributed by Jonathan Teh Soon Yew
1128c2ecf20Sopenharmony_ci * <j.teh@iname.com>)
1138c2ecf20Sopenharmony_ci */
1148c2ecf20Sopenharmony_cistatic inline u8 IN_TO_REG(long val, int in_num)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	/*
1178c2ecf20Sopenharmony_ci	 * To avoid floating point, we multiply constants by 10 (100 for +12V).
1188c2ecf20Sopenharmony_ci	 * Rounding is done (120500 is actually 133000 - 12500).
1198c2ecf20Sopenharmony_ci	 * Remember that val is expressed in 0.001V/bit, which is why we divide
1208c2ecf20Sopenharmony_ci	 * by an additional 10000 (100000 for +12V): 1000 for val and 10 (100)
1218c2ecf20Sopenharmony_ci	 * for the constants.
1228c2ecf20Sopenharmony_ci	 */
1238c2ecf20Sopenharmony_ci	if (in_num <= 1)
1248c2ecf20Sopenharmony_ci		return (u8) clamp_val((val * 21024 - 1205000) / 250000, 0, 255);
1258c2ecf20Sopenharmony_ci	else if (in_num == 2)
1268c2ecf20Sopenharmony_ci		return (u8) clamp_val((val * 15737 - 1205000) / 250000, 0, 255);
1278c2ecf20Sopenharmony_ci	else if (in_num == 3)
1288c2ecf20Sopenharmony_ci		return (u8) clamp_val((val * 10108 - 1205000) / 250000, 0, 255);
1298c2ecf20Sopenharmony_ci	else
1308c2ecf20Sopenharmony_ci		return (u8) clamp_val((val * 41714 - 12050000) / 2500000, 0,
1318c2ecf20Sopenharmony_ci				      255);
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic inline long IN_FROM_REG(u8 val, int in_num)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	/*
1378c2ecf20Sopenharmony_ci	 * To avoid floating point, we multiply constants by 10 (100 for +12V).
1388c2ecf20Sopenharmony_ci	 * We also multiply them by 1000 because we want 0.001V/bit for the
1398c2ecf20Sopenharmony_ci	 * output value. Rounding is done.
1408c2ecf20Sopenharmony_ci	 */
1418c2ecf20Sopenharmony_ci	if (in_num <= 1)
1428c2ecf20Sopenharmony_ci		return (long) ((250000 * val + 1330000 + 21024 / 2) / 21024);
1438c2ecf20Sopenharmony_ci	else if (in_num == 2)
1448c2ecf20Sopenharmony_ci		return (long) ((250000 * val + 1330000 + 15737 / 2) / 15737);
1458c2ecf20Sopenharmony_ci	else if (in_num == 3)
1468c2ecf20Sopenharmony_ci		return (long) ((250000 * val + 1330000 + 10108 / 2) / 10108);
1478c2ecf20Sopenharmony_ci	else
1488c2ecf20Sopenharmony_ci		return (long) ((2500000 * val + 13300000 + 41714 / 2) / 41714);
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci/********* FAN RPM CONVERSIONS ********/
1528c2ecf20Sopenharmony_ci/*
1538c2ecf20Sopenharmony_ci * Higher register values = slower fans (the fan's strobe gates a counter).
1548c2ecf20Sopenharmony_ci * But this chip saturates back at 0, not at 255 like all the other chips.
1558c2ecf20Sopenharmony_ci * So, 0 means 0 RPM
1568c2ecf20Sopenharmony_ci */
1578c2ecf20Sopenharmony_cistatic inline u8 FAN_TO_REG(long rpm, int div)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	if (rpm == 0)
1608c2ecf20Sopenharmony_ci		return 0;
1618c2ecf20Sopenharmony_ci	rpm = clamp_val(rpm, 1, 1000000);
1628c2ecf20Sopenharmony_ci	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 255);
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : (val) == 255 ? 0 : 1350000 / \
1668c2ecf20Sopenharmony_ci				((val) * (div)))
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/******** TEMP CONVERSIONS (Bob Dougherty) *********/
1698c2ecf20Sopenharmony_ci/*
1708c2ecf20Sopenharmony_ci * linear fits from HWMon.cpp (Copyright 1998-2000 Jonathan Teh Soon Yew)
1718c2ecf20Sopenharmony_ci *	if(temp<169)
1728c2ecf20Sopenharmony_ci *		return double(temp)*0.427-32.08;
1738c2ecf20Sopenharmony_ci *	else if(temp>=169 && temp<=202)
1748c2ecf20Sopenharmony_ci *		return double(temp)*0.582-58.16;
1758c2ecf20Sopenharmony_ci *	else
1768c2ecf20Sopenharmony_ci *		return double(temp)*0.924-127.33;
1778c2ecf20Sopenharmony_ci *
1788c2ecf20Sopenharmony_ci * A fifth-order polynomial fits the unofficial data (provided by Alex van
1798c2ecf20Sopenharmony_ci * Kaam <darkside@chello.nl>) a bit better.  It also give more reasonable
1808c2ecf20Sopenharmony_ci * numbers on my machine (ie. they agree with what my BIOS tells me).
1818c2ecf20Sopenharmony_ci * Here's the fifth-order fit to the 8-bit data:
1828c2ecf20Sopenharmony_ci * temp = 1.625093e-10*val^5 - 1.001632e-07*val^4 + 2.457653e-05*val^3 -
1838c2ecf20Sopenharmony_ci *	2.967619e-03*val^2 + 2.175144e-01*val - 7.090067e+0.
1848c2ecf20Sopenharmony_ci *
1858c2ecf20Sopenharmony_ci * (2000-10-25- RFD: thanks to Uwe Andersen <uandersen@mayah.com> for
1868c2ecf20Sopenharmony_ci * finding my typos in this formula!)
1878c2ecf20Sopenharmony_ci *
1888c2ecf20Sopenharmony_ci * Alas, none of the elegant function-fit solutions will work because we
1898c2ecf20Sopenharmony_ci * aren't allowed to use floating point in the kernel and doing it with
1908c2ecf20Sopenharmony_ci * integers doesn't provide enough precision.  So we'll do boring old
1918c2ecf20Sopenharmony_ci * look-up table stuff.  The unofficial data (see below) have effectively
1928c2ecf20Sopenharmony_ci * 7-bit resolution (they are rounded to the nearest degree).  I'm assuming
1938c2ecf20Sopenharmony_ci * that the transfer function of the device is monotonic and smooth, so a
1948c2ecf20Sopenharmony_ci * smooth function fit to the data will allow us to get better precision.
1958c2ecf20Sopenharmony_ci * I used the 5th-order poly fit described above and solved for
1968c2ecf20Sopenharmony_ci * VIA register values 0-255.  I *10 before rounding, so we get tenth-degree
1978c2ecf20Sopenharmony_ci * precision.  (I could have done all 1024 values for our 10-bit readings,
1988c2ecf20Sopenharmony_ci * but the function is very linear in the useful range (0-80 deg C), so
1998c2ecf20Sopenharmony_ci * we'll just use linear interpolation for 10-bit readings.)  So, temp_lut
2008c2ecf20Sopenharmony_ci * is the temp at via register values 0-255:
2018c2ecf20Sopenharmony_ci */
2028c2ecf20Sopenharmony_cistatic const s16 temp_lut[] = {
2038c2ecf20Sopenharmony_ci	-709, -688, -667, -646, -627, -607, -589, -570, -553, -536, -519,
2048c2ecf20Sopenharmony_ci	-503, -487, -471, -456, -442, -428, -414, -400, -387, -375,
2058c2ecf20Sopenharmony_ci	-362, -350, -339, -327, -316, -305, -295, -285, -275, -265,
2068c2ecf20Sopenharmony_ci	-255, -246, -237, -229, -220, -212, -204, -196, -188, -180,
2078c2ecf20Sopenharmony_ci	-173, -166, -159, -152, -145, -139, -132, -126, -120, -114,
2088c2ecf20Sopenharmony_ci	-108, -102, -96, -91, -85, -80, -74, -69, -64, -59, -54, -49,
2098c2ecf20Sopenharmony_ci	-44, -39, -34, -29, -25, -20, -15, -11, -6, -2, 3, 7, 12, 16,
2108c2ecf20Sopenharmony_ci	20, 25, 29, 33, 37, 42, 46, 50, 54, 59, 63, 67, 71, 75, 79, 84,
2118c2ecf20Sopenharmony_ci	88, 92, 96, 100, 104, 109, 113, 117, 121, 125, 130, 134, 138,
2128c2ecf20Sopenharmony_ci	142, 146, 151, 155, 159, 163, 168, 172, 176, 181, 185, 189,
2138c2ecf20Sopenharmony_ci	193, 198, 202, 206, 211, 215, 219, 224, 228, 232, 237, 241,
2148c2ecf20Sopenharmony_ci	245, 250, 254, 259, 263, 267, 272, 276, 281, 285, 290, 294,
2158c2ecf20Sopenharmony_ci	299, 303, 307, 312, 316, 321, 325, 330, 334, 339, 344, 348,
2168c2ecf20Sopenharmony_ci	353, 357, 362, 366, 371, 376, 380, 385, 390, 395, 399, 404,
2178c2ecf20Sopenharmony_ci	409, 414, 419, 423, 428, 433, 438, 443, 449, 454, 459, 464,
2188c2ecf20Sopenharmony_ci	469, 475, 480, 486, 491, 497, 502, 508, 514, 520, 526, 532,
2198c2ecf20Sopenharmony_ci	538, 544, 551, 557, 564, 571, 578, 584, 592, 599, 606, 614,
2208c2ecf20Sopenharmony_ci	621, 629, 637, 645, 654, 662, 671, 680, 689, 698, 708, 718,
2218c2ecf20Sopenharmony_ci	728, 738, 749, 759, 770, 782, 793, 805, 818, 830, 843, 856,
2228c2ecf20Sopenharmony_ci	870, 883, 898, 912, 927, 943, 958, 975, 991, 1008, 1026, 1044,
2238c2ecf20Sopenharmony_ci	1062, 1081, 1101, 1121, 1141, 1162, 1184, 1206, 1229, 1252,
2248c2ecf20Sopenharmony_ci	1276, 1301, 1326, 1352, 1378, 1406, 1434, 1462
2258c2ecf20Sopenharmony_ci};
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci/*
2288c2ecf20Sopenharmony_ci * the original LUT values from Alex van Kaam <darkside@chello.nl>
2298c2ecf20Sopenharmony_ci * (for via register values 12-240):
2308c2ecf20Sopenharmony_ci * {-50,-49,-47,-45,-43,-41,-39,-38,-37,-35,-34,-33,-32,-31,
2318c2ecf20Sopenharmony_ci * -30,-29,-28,-27,-26,-25,-24,-24,-23,-22,-21,-20,-20,-19,-18,-17,-17,-16,-15,
2328c2ecf20Sopenharmony_ci * -15,-14,-14,-13,-12,-12,-11,-11,-10,-9,-9,-8,-8,-7,-7,-6,-6,-5,-5,-4,-4,-3,
2338c2ecf20Sopenharmony_ci * -3,-2,-2,-1,-1,0,0,1,1,1,3,3,3,4,4,4,5,5,5,6,6,7,7,8,8,9,9,9,10,10,11,11,12,
2348c2ecf20Sopenharmony_ci * 12,12,13,13,13,14,14,15,15,16,16,16,17,17,18,18,19,19,20,20,21,21,21,22,22,
2358c2ecf20Sopenharmony_ci * 22,23,23,24,24,25,25,26,26,26,27,27,27,28,28,29,29,30,30,30,31,31,32,32,33,
2368c2ecf20Sopenharmony_ci * 33,34,34,35,35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,43,43,44,44,45,
2378c2ecf20Sopenharmony_ci * 45,46,46,47,48,48,49,49,50,51,51,52,52,53,53,54,55,55,56,57,57,58,59,59,60,
2388c2ecf20Sopenharmony_ci * 61,62,62,63,64,65,66,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,83,84,
2398c2ecf20Sopenharmony_ci * 85,86,88,89,91,92,94,96,97,99,101,103,105,107,109,110};
2408c2ecf20Sopenharmony_ci *
2418c2ecf20Sopenharmony_ci *
2428c2ecf20Sopenharmony_ci * Here's the reverse LUT.  I got it by doing a 6-th order poly fit (needed
2438c2ecf20Sopenharmony_ci * an extra term for a good fit to these inverse data!) and then
2448c2ecf20Sopenharmony_ci * solving for each temp value from -50 to 110 (the useable range for
2458c2ecf20Sopenharmony_ci * this chip).  Here's the fit:
2468c2ecf20Sopenharmony_ci * viaRegVal = -1.160370e-10*val^6 +3.193693e-08*val^5 - 1.464447e-06*val^4
2478c2ecf20Sopenharmony_ci * - 2.525453e-04*val^3 + 1.424593e-02*val^2 + 2.148941e+00*val +7.275808e+01)
2488c2ecf20Sopenharmony_ci * Note that n=161:
2498c2ecf20Sopenharmony_ci */
2508c2ecf20Sopenharmony_cistatic const u8 via_lut[] = {
2518c2ecf20Sopenharmony_ci	12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18, 19, 20, 20, 21, 22, 23,
2528c2ecf20Sopenharmony_ci	23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 39, 40,
2538c2ecf20Sopenharmony_ci	41, 43, 45, 46, 48, 49, 51, 53, 55, 57, 59, 60, 62, 64, 66,
2548c2ecf20Sopenharmony_ci	69, 71, 73, 75, 77, 79, 82, 84, 86, 88, 91, 93, 95, 98, 100,
2558c2ecf20Sopenharmony_ci	103, 105, 107, 110, 112, 115, 117, 119, 122, 124, 126, 129,
2568c2ecf20Sopenharmony_ci	131, 134, 136, 138, 140, 143, 145, 147, 150, 152, 154, 156,
2578c2ecf20Sopenharmony_ci	158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180,
2588c2ecf20Sopenharmony_ci	182, 183, 185, 187, 188, 190, 192, 193, 195, 196, 198, 199,
2598c2ecf20Sopenharmony_ci	200, 202, 203, 205, 206, 207, 208, 209, 210, 211, 212, 213,
2608c2ecf20Sopenharmony_ci	214, 215, 216, 217, 218, 219, 220, 221, 222, 222, 223, 224,
2618c2ecf20Sopenharmony_ci	225, 226, 226, 227, 228, 228, 229, 230, 230, 231, 232, 232,
2628c2ecf20Sopenharmony_ci	233, 233, 234, 235, 235, 236, 236, 237, 237, 238, 238, 239,
2638c2ecf20Sopenharmony_ci	239, 240
2648c2ecf20Sopenharmony_ci};
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci/*
2678c2ecf20Sopenharmony_ci * Converting temps to (8-bit) hyst and over registers
2688c2ecf20Sopenharmony_ci * No interpolation here.
2698c2ecf20Sopenharmony_ci * The +50 is because the temps start at -50
2708c2ecf20Sopenharmony_ci */
2718c2ecf20Sopenharmony_cistatic inline u8 TEMP_TO_REG(long val)
2728c2ecf20Sopenharmony_ci{
2738c2ecf20Sopenharmony_ci	return via_lut[val <= -50000 ? 0 : val >= 110000 ? 160 :
2748c2ecf20Sopenharmony_ci		      (val < 0 ? val - 500 : val + 500) / 1000 + 50];
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci/* for 8-bit temperature hyst and over registers */
2788c2ecf20Sopenharmony_ci#define TEMP_FROM_REG(val)	((long)temp_lut[val] * 100)
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci/* for 10-bit temperature readings */
2818c2ecf20Sopenharmony_cistatic inline long TEMP_FROM_REG10(u16 val)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	u16 eight_bits = val >> 2;
2848c2ecf20Sopenharmony_ci	u16 two_bits = val & 3;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	/* no interpolation for these */
2878c2ecf20Sopenharmony_ci	if (two_bits == 0 || eight_bits == 255)
2888c2ecf20Sopenharmony_ci		return TEMP_FROM_REG(eight_bits);
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	/* do some linear interpolation */
2918c2ecf20Sopenharmony_ci	return (temp_lut[eight_bits] * (4 - two_bits) +
2928c2ecf20Sopenharmony_ci		temp_lut[eight_bits + 1] * two_bits) * 25;
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci#define DIV_FROM_REG(val) (1 << (val))
2968c2ecf20Sopenharmony_ci#define DIV_TO_REG(val) ((val) == 8 ? 3 : (val) == 4 ? 2 : (val) == 1 ? 0 : 1)
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci/*
2998c2ecf20Sopenharmony_ci * For each registered chip, we need to keep some data in memory.
3008c2ecf20Sopenharmony_ci * The structure is dynamically allocated.
3018c2ecf20Sopenharmony_ci */
3028c2ecf20Sopenharmony_cistruct via686a_data {
3038c2ecf20Sopenharmony_ci	unsigned short addr;
3048c2ecf20Sopenharmony_ci	const char *name;
3058c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
3068c2ecf20Sopenharmony_ci	struct mutex update_lock;
3078c2ecf20Sopenharmony_ci	char valid;		/* !=0 if following fields are valid */
3088c2ecf20Sopenharmony_ci	unsigned long last_updated;	/* In jiffies */
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	u8 in[5];		/* Register value */
3118c2ecf20Sopenharmony_ci	u8 in_max[5];		/* Register value */
3128c2ecf20Sopenharmony_ci	u8 in_min[5];		/* Register value */
3138c2ecf20Sopenharmony_ci	u8 fan[2];		/* Register value */
3148c2ecf20Sopenharmony_ci	u8 fan_min[2];		/* Register value */
3158c2ecf20Sopenharmony_ci	u16 temp[3];		/* Register value 10 bit */
3168c2ecf20Sopenharmony_ci	u8 temp_over[3];	/* Register value */
3178c2ecf20Sopenharmony_ci	u8 temp_hyst[3];	/* Register value */
3188c2ecf20Sopenharmony_ci	u8 fan_div[2];		/* Register encoding, shifted right */
3198c2ecf20Sopenharmony_ci	u16 alarms;		/* Register encoding, combined */
3208c2ecf20Sopenharmony_ci};
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic struct pci_dev *s_bridge;	/* pointer to the (only) via686a */
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic int via686a_probe(struct platform_device *pdev);
3258c2ecf20Sopenharmony_cistatic int via686a_remove(struct platform_device *pdev);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_cistatic inline int via686a_read_value(struct via686a_data *data, u8 reg)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	return inb_p(data->addr + reg);
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_cistatic inline void via686a_write_value(struct via686a_data *data, u8 reg,
3338c2ecf20Sopenharmony_ci				       u8 value)
3348c2ecf20Sopenharmony_ci{
3358c2ecf20Sopenharmony_ci	outb_p(value, data->addr + reg);
3368c2ecf20Sopenharmony_ci}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_cistatic struct via686a_data *via686a_update_device(struct device *dev);
3398c2ecf20Sopenharmony_cistatic void via686a_init_device(struct via686a_data *data);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci/* following are the sysfs callback functions */
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci/* 7 voltage sensors */
3448c2ecf20Sopenharmony_cistatic ssize_t in_show(struct device *dev, struct device_attribute *da,
3458c2ecf20Sopenharmony_ci		       char *buf) {
3468c2ecf20Sopenharmony_ci	struct via686a_data *data = via686a_update_device(dev);
3478c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
3488c2ecf20Sopenharmony_ci	int nr = attr->index;
3498c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", IN_FROM_REG(data->in[nr], nr));
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_cistatic ssize_t in_min_show(struct device *dev, struct device_attribute *da,
3538c2ecf20Sopenharmony_ci			   char *buf) {
3548c2ecf20Sopenharmony_ci	struct via686a_data *data = via686a_update_device(dev);
3558c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
3568c2ecf20Sopenharmony_ci	int nr = attr->index;
3578c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", IN_FROM_REG(data->in_min[nr], nr));
3588c2ecf20Sopenharmony_ci}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cistatic ssize_t in_max_show(struct device *dev, struct device_attribute *da,
3618c2ecf20Sopenharmony_ci			   char *buf) {
3628c2ecf20Sopenharmony_ci	struct via686a_data *data = via686a_update_device(dev);
3638c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
3648c2ecf20Sopenharmony_ci	int nr = attr->index;
3658c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", IN_FROM_REG(data->in_max[nr], nr));
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_cistatic ssize_t in_min_store(struct device *dev, struct device_attribute *da,
3698c2ecf20Sopenharmony_ci			    const char *buf, size_t count) {
3708c2ecf20Sopenharmony_ci	struct via686a_data *data = dev_get_drvdata(dev);
3718c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
3728c2ecf20Sopenharmony_ci	int nr = attr->index;
3738c2ecf20Sopenharmony_ci	unsigned long val;
3748c2ecf20Sopenharmony_ci	int err;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
3778c2ecf20Sopenharmony_ci	if (err)
3788c2ecf20Sopenharmony_ci		return err;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3818c2ecf20Sopenharmony_ci	data->in_min[nr] = IN_TO_REG(val, nr);
3828c2ecf20Sopenharmony_ci	via686a_write_value(data, VIA686A_REG_IN_MIN(nr),
3838c2ecf20Sopenharmony_ci			data->in_min[nr]);
3848c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3858c2ecf20Sopenharmony_ci	return count;
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_cistatic ssize_t in_max_store(struct device *dev, struct device_attribute *da,
3888c2ecf20Sopenharmony_ci			    const char *buf, size_t count) {
3898c2ecf20Sopenharmony_ci	struct via686a_data *data = dev_get_drvdata(dev);
3908c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
3918c2ecf20Sopenharmony_ci	int nr = attr->index;
3928c2ecf20Sopenharmony_ci	unsigned long val;
3938c2ecf20Sopenharmony_ci	int err;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
3968c2ecf20Sopenharmony_ci	if (err)
3978c2ecf20Sopenharmony_ci		return err;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
4008c2ecf20Sopenharmony_ci	data->in_max[nr] = IN_TO_REG(val, nr);
4018c2ecf20Sopenharmony_ci	via686a_write_value(data, VIA686A_REG_IN_MAX(nr),
4028c2ecf20Sopenharmony_ci			data->in_max[nr]);
4038c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
4048c2ecf20Sopenharmony_ci	return count;
4058c2ecf20Sopenharmony_ci}
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
4088c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
4098c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
4108c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
4118c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
4128c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
4138c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
4148c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
4158c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
4168c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
4178c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
4188c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
4198c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
4208c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
4218c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci/* 3 temperatures */
4248c2ecf20Sopenharmony_cistatic ssize_t temp_show(struct device *dev, struct device_attribute *da,
4258c2ecf20Sopenharmony_ci			 char *buf) {
4268c2ecf20Sopenharmony_ci	struct via686a_data *data = via686a_update_device(dev);
4278c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
4288c2ecf20Sopenharmony_ci	int nr = attr->index;
4298c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", TEMP_FROM_REG10(data->temp[nr]));
4308c2ecf20Sopenharmony_ci}
4318c2ecf20Sopenharmony_cistatic ssize_t temp_over_show(struct device *dev, struct device_attribute *da,
4328c2ecf20Sopenharmony_ci			      char *buf) {
4338c2ecf20Sopenharmony_ci	struct via686a_data *data = via686a_update_device(dev);
4348c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
4358c2ecf20Sopenharmony_ci	int nr = attr->index;
4368c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp_over[nr]));
4378c2ecf20Sopenharmony_ci}
4388c2ecf20Sopenharmony_cistatic ssize_t temp_hyst_show(struct device *dev, struct device_attribute *da,
4398c2ecf20Sopenharmony_ci			      char *buf) {
4408c2ecf20Sopenharmony_ci	struct via686a_data *data = via686a_update_device(dev);
4418c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
4428c2ecf20Sopenharmony_ci	int nr = attr->index;
4438c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp_hyst[nr]));
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_cistatic ssize_t temp_over_store(struct device *dev,
4468c2ecf20Sopenharmony_ci			       struct device_attribute *da, const char *buf,
4478c2ecf20Sopenharmony_ci			       size_t count) {
4488c2ecf20Sopenharmony_ci	struct via686a_data *data = dev_get_drvdata(dev);
4498c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
4508c2ecf20Sopenharmony_ci	int nr = attr->index;
4518c2ecf20Sopenharmony_ci	long val;
4528c2ecf20Sopenharmony_ci	int err;
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
4558c2ecf20Sopenharmony_ci	if (err)
4568c2ecf20Sopenharmony_ci		return err;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
4598c2ecf20Sopenharmony_ci	data->temp_over[nr] = TEMP_TO_REG(val);
4608c2ecf20Sopenharmony_ci	via686a_write_value(data, VIA686A_REG_TEMP_OVER[nr],
4618c2ecf20Sopenharmony_ci			    data->temp_over[nr]);
4628c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
4638c2ecf20Sopenharmony_ci	return count;
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_cistatic ssize_t temp_hyst_store(struct device *dev,
4668c2ecf20Sopenharmony_ci			       struct device_attribute *da, const char *buf,
4678c2ecf20Sopenharmony_ci			       size_t count) {
4688c2ecf20Sopenharmony_ci	struct via686a_data *data = dev_get_drvdata(dev);
4698c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
4708c2ecf20Sopenharmony_ci	int nr = attr->index;
4718c2ecf20Sopenharmony_ci	long val;
4728c2ecf20Sopenharmony_ci	int err;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
4758c2ecf20Sopenharmony_ci	if (err)
4768c2ecf20Sopenharmony_ci		return err;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
4798c2ecf20Sopenharmony_ci	data->temp_hyst[nr] = TEMP_TO_REG(val);
4808c2ecf20Sopenharmony_ci	via686a_write_value(data, VIA686A_REG_TEMP_HYST[nr],
4818c2ecf20Sopenharmony_ci			    data->temp_hyst[nr]);
4828c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
4838c2ecf20Sopenharmony_ci	return count;
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
4878c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max, temp_over, 0);
4888c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp_hyst, 0);
4898c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
4908c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_max, temp_over, 1);
4918c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_max_hyst, temp_hyst, 1);
4928c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
4938c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_max, temp_over, 2);
4948c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_max_hyst, temp_hyst, 2);
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci/* 2 Fans */
4978c2ecf20Sopenharmony_cistatic ssize_t fan_show(struct device *dev, struct device_attribute *da,
4988c2ecf20Sopenharmony_ci			char *buf) {
4998c2ecf20Sopenharmony_ci	struct via686a_data *data = via686a_update_device(dev);
5008c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
5018c2ecf20Sopenharmony_ci	int nr = attr->index;
5028c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
5038c2ecf20Sopenharmony_ci				DIV_FROM_REG(data->fan_div[nr])));
5048c2ecf20Sopenharmony_ci}
5058c2ecf20Sopenharmony_cistatic ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
5068c2ecf20Sopenharmony_ci			    char *buf) {
5078c2ecf20Sopenharmony_ci	struct via686a_data *data = via686a_update_device(dev);
5088c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
5098c2ecf20Sopenharmony_ci	int nr = attr->index;
5108c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n",
5118c2ecf20Sopenharmony_ci		FAN_FROM_REG(data->fan_min[nr],
5128c2ecf20Sopenharmony_ci			     DIV_FROM_REG(data->fan_div[nr])));
5138c2ecf20Sopenharmony_ci}
5148c2ecf20Sopenharmony_cistatic ssize_t fan_div_show(struct device *dev, struct device_attribute *da,
5158c2ecf20Sopenharmony_ci			    char *buf) {
5168c2ecf20Sopenharmony_ci	struct via686a_data *data = via686a_update_device(dev);
5178c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
5188c2ecf20Sopenharmony_ci	int nr = attr->index;
5198c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
5208c2ecf20Sopenharmony_ci}
5218c2ecf20Sopenharmony_cistatic ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
5228c2ecf20Sopenharmony_ci			     const char *buf, size_t count) {
5238c2ecf20Sopenharmony_ci	struct via686a_data *data = dev_get_drvdata(dev);
5248c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
5258c2ecf20Sopenharmony_ci	int nr = attr->index;
5268c2ecf20Sopenharmony_ci	unsigned long val;
5278c2ecf20Sopenharmony_ci	int err;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
5308c2ecf20Sopenharmony_ci	if (err)
5318c2ecf20Sopenharmony_ci		return err;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
5348c2ecf20Sopenharmony_ci	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
5358c2ecf20Sopenharmony_ci	via686a_write_value(data, VIA686A_REG_FAN_MIN(nr+1), data->fan_min[nr]);
5368c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
5378c2ecf20Sopenharmony_ci	return count;
5388c2ecf20Sopenharmony_ci}
5398c2ecf20Sopenharmony_cistatic ssize_t fan_div_store(struct device *dev, struct device_attribute *da,
5408c2ecf20Sopenharmony_ci			     const char *buf, size_t count) {
5418c2ecf20Sopenharmony_ci	struct via686a_data *data = dev_get_drvdata(dev);
5428c2ecf20Sopenharmony_ci	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
5438c2ecf20Sopenharmony_ci	int nr = attr->index;
5448c2ecf20Sopenharmony_ci	int old;
5458c2ecf20Sopenharmony_ci	unsigned long val;
5468c2ecf20Sopenharmony_ci	int err;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
5498c2ecf20Sopenharmony_ci	if (err)
5508c2ecf20Sopenharmony_ci		return err;
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
5538c2ecf20Sopenharmony_ci	old = via686a_read_value(data, VIA686A_REG_FANDIV);
5548c2ecf20Sopenharmony_ci	data->fan_div[nr] = DIV_TO_REG(val);
5558c2ecf20Sopenharmony_ci	old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
5568c2ecf20Sopenharmony_ci	via686a_write_value(data, VIA686A_REG_FANDIV, old);
5578c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
5588c2ecf20Sopenharmony_ci	return count;
5598c2ecf20Sopenharmony_ci}
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
5628c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
5638c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
5648c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
5658c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
5668c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci/* Alarms */
5698c2ecf20Sopenharmony_cistatic ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
5708c2ecf20Sopenharmony_ci			   char *buf)
5718c2ecf20Sopenharmony_ci{
5728c2ecf20Sopenharmony_ci	struct via686a_data *data = via686a_update_device(dev);
5738c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", data->alarms);
5748c2ecf20Sopenharmony_ci}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(alarms);
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_cistatic ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
5798c2ecf20Sopenharmony_ci			  char *buf)
5808c2ecf20Sopenharmony_ci{
5818c2ecf20Sopenharmony_ci	int bitnr = to_sensor_dev_attr(attr)->index;
5828c2ecf20Sopenharmony_ci	struct via686a_data *data = via686a_update_device(dev);
5838c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
5848c2ecf20Sopenharmony_ci}
5858c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
5868c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
5878c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
5888c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
5898c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
5908c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
5918c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 11);
5928c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 15);
5938c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
5948c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_cistatic ssize_t name_show(struct device *dev, struct device_attribute
5978c2ecf20Sopenharmony_ci			 *devattr, char *buf)
5988c2ecf20Sopenharmony_ci{
5998c2ecf20Sopenharmony_ci	struct via686a_data *data = dev_get_drvdata(dev);
6008c2ecf20Sopenharmony_ci	return sprintf(buf, "%s\n", data->name);
6018c2ecf20Sopenharmony_ci}
6028c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(name);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_cistatic struct attribute *via686a_attributes[] = {
6058c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_input.dev_attr.attr,
6068c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_input.dev_attr.attr,
6078c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_input.dev_attr.attr,
6088c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_input.dev_attr.attr,
6098c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_input.dev_attr.attr,
6108c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_min.dev_attr.attr,
6118c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_min.dev_attr.attr,
6128c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_min.dev_attr.attr,
6138c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_min.dev_attr.attr,
6148c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_min.dev_attr.attr,
6158c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_max.dev_attr.attr,
6168c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_max.dev_attr.attr,
6178c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_max.dev_attr.attr,
6188c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_max.dev_attr.attr,
6198c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_max.dev_attr.attr,
6208c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_alarm.dev_attr.attr,
6218c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_alarm.dev_attr.attr,
6228c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_alarm.dev_attr.attr,
6238c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_alarm.dev_attr.attr,
6248c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_alarm.dev_attr.attr,
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
6278c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_input.dev_attr.attr,
6288c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_input.dev_attr.attr,
6298c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max.dev_attr.attr,
6308c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max.dev_attr.attr,
6318c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_max.dev_attr.attr,
6328c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
6338c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
6348c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
6358c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
6368c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
6378c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_input.dev_attr.attr,
6408c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_input.dev_attr.attr,
6418c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_min.dev_attr.attr,
6428c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_min.dev_attr.attr,
6438c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_div.dev_attr.attr,
6448c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_div.dev_attr.attr,
6458c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
6468c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	&dev_attr_alarms.attr,
6498c2ecf20Sopenharmony_ci	&dev_attr_name.attr,
6508c2ecf20Sopenharmony_ci	NULL
6518c2ecf20Sopenharmony_ci};
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_cistatic const struct attribute_group via686a_group = {
6548c2ecf20Sopenharmony_ci	.attrs = via686a_attributes,
6558c2ecf20Sopenharmony_ci};
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_cistatic struct platform_driver via686a_driver = {
6588c2ecf20Sopenharmony_ci	.driver = {
6598c2ecf20Sopenharmony_ci		.name	= "via686a",
6608c2ecf20Sopenharmony_ci	},
6618c2ecf20Sopenharmony_ci	.probe		= via686a_probe,
6628c2ecf20Sopenharmony_ci	.remove		= via686a_remove,
6638c2ecf20Sopenharmony_ci};
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci/* This is called when the module is loaded */
6668c2ecf20Sopenharmony_cistatic int via686a_probe(struct platform_device *pdev)
6678c2ecf20Sopenharmony_ci{
6688c2ecf20Sopenharmony_ci	struct via686a_data *data;
6698c2ecf20Sopenharmony_ci	struct resource *res;
6708c2ecf20Sopenharmony_ci	int err;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	/* Reserve the ISA region */
6738c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
6748c2ecf20Sopenharmony_ci	if (!devm_request_region(&pdev->dev, res->start, VIA686A_EXTENT,
6758c2ecf20Sopenharmony_ci				 via686a_driver.driver.name)) {
6768c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n",
6778c2ecf20Sopenharmony_ci			(unsigned long)res->start, (unsigned long)res->end);
6788c2ecf20Sopenharmony_ci		return -ENODEV;
6798c2ecf20Sopenharmony_ci	}
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	data = devm_kzalloc(&pdev->dev, sizeof(struct via686a_data),
6828c2ecf20Sopenharmony_ci			    GFP_KERNEL);
6838c2ecf20Sopenharmony_ci	if (!data)
6848c2ecf20Sopenharmony_ci		return -ENOMEM;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, data);
6878c2ecf20Sopenharmony_ci	data->addr = res->start;
6888c2ecf20Sopenharmony_ci	data->name = "via686a";
6898c2ecf20Sopenharmony_ci	mutex_init(&data->update_lock);
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	/* Initialize the VIA686A chip */
6928c2ecf20Sopenharmony_ci	via686a_init_device(data);
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	/* Register sysfs hooks */
6958c2ecf20Sopenharmony_ci	err = sysfs_create_group(&pdev->dev.kobj, &via686a_group);
6968c2ecf20Sopenharmony_ci	if (err)
6978c2ecf20Sopenharmony_ci		return err;
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	data->hwmon_dev = hwmon_device_register(&pdev->dev);
7008c2ecf20Sopenharmony_ci	if (IS_ERR(data->hwmon_dev)) {
7018c2ecf20Sopenharmony_ci		err = PTR_ERR(data->hwmon_dev);
7028c2ecf20Sopenharmony_ci		goto exit_remove_files;
7038c2ecf20Sopenharmony_ci	}
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	return 0;
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ciexit_remove_files:
7088c2ecf20Sopenharmony_ci	sysfs_remove_group(&pdev->dev.kobj, &via686a_group);
7098c2ecf20Sopenharmony_ci	return err;
7108c2ecf20Sopenharmony_ci}
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_cistatic int via686a_remove(struct platform_device *pdev)
7138c2ecf20Sopenharmony_ci{
7148c2ecf20Sopenharmony_ci	struct via686a_data *data = platform_get_drvdata(pdev);
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	hwmon_device_unregister(data->hwmon_dev);
7178c2ecf20Sopenharmony_ci	sysfs_remove_group(&pdev->dev.kobj, &via686a_group);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	return 0;
7208c2ecf20Sopenharmony_ci}
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_cistatic void via686a_update_fan_div(struct via686a_data *data)
7238c2ecf20Sopenharmony_ci{
7248c2ecf20Sopenharmony_ci	int reg = via686a_read_value(data, VIA686A_REG_FANDIV);
7258c2ecf20Sopenharmony_ci	data->fan_div[0] = (reg >> 4) & 0x03;
7268c2ecf20Sopenharmony_ci	data->fan_div[1] = reg >> 6;
7278c2ecf20Sopenharmony_ci}
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_cistatic void via686a_init_device(struct via686a_data *data)
7308c2ecf20Sopenharmony_ci{
7318c2ecf20Sopenharmony_ci	u8 reg;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	/* Start monitoring */
7348c2ecf20Sopenharmony_ci	reg = via686a_read_value(data, VIA686A_REG_CONFIG);
7358c2ecf20Sopenharmony_ci	via686a_write_value(data, VIA686A_REG_CONFIG, (reg | 0x01) & 0x7F);
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	/* Configure temp interrupt mode for continuous-interrupt operation */
7388c2ecf20Sopenharmony_ci	reg = via686a_read_value(data, VIA686A_REG_TEMP_MODE);
7398c2ecf20Sopenharmony_ci	via686a_write_value(data, VIA686A_REG_TEMP_MODE,
7408c2ecf20Sopenharmony_ci			    (reg & ~VIA686A_TEMP_MODE_MASK)
7418c2ecf20Sopenharmony_ci			    | VIA686A_TEMP_MODE_CONTINUOUS);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	/* Pre-read fan clock divisor values */
7448c2ecf20Sopenharmony_ci	via686a_update_fan_div(data);
7458c2ecf20Sopenharmony_ci}
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_cistatic struct via686a_data *via686a_update_device(struct device *dev)
7488c2ecf20Sopenharmony_ci{
7498c2ecf20Sopenharmony_ci	struct via686a_data *data = dev_get_drvdata(dev);
7508c2ecf20Sopenharmony_ci	int i;
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
7558c2ecf20Sopenharmony_ci	    || !data->valid) {
7568c2ecf20Sopenharmony_ci		for (i = 0; i <= 4; i++) {
7578c2ecf20Sopenharmony_ci			data->in[i] =
7588c2ecf20Sopenharmony_ci			    via686a_read_value(data, VIA686A_REG_IN(i));
7598c2ecf20Sopenharmony_ci			data->in_min[i] = via686a_read_value(data,
7608c2ecf20Sopenharmony_ci							     VIA686A_REG_IN_MIN
7618c2ecf20Sopenharmony_ci							     (i));
7628c2ecf20Sopenharmony_ci			data->in_max[i] =
7638c2ecf20Sopenharmony_ci			    via686a_read_value(data, VIA686A_REG_IN_MAX(i));
7648c2ecf20Sopenharmony_ci		}
7658c2ecf20Sopenharmony_ci		for (i = 1; i <= 2; i++) {
7668c2ecf20Sopenharmony_ci			data->fan[i - 1] =
7678c2ecf20Sopenharmony_ci			    via686a_read_value(data, VIA686A_REG_FAN(i));
7688c2ecf20Sopenharmony_ci			data->fan_min[i - 1] = via686a_read_value(data,
7698c2ecf20Sopenharmony_ci						     VIA686A_REG_FAN_MIN(i));
7708c2ecf20Sopenharmony_ci		}
7718c2ecf20Sopenharmony_ci		for (i = 0; i <= 2; i++) {
7728c2ecf20Sopenharmony_ci			data->temp[i] = via686a_read_value(data,
7738c2ecf20Sopenharmony_ci						 VIA686A_REG_TEMP[i]) << 2;
7748c2ecf20Sopenharmony_ci			data->temp_over[i] =
7758c2ecf20Sopenharmony_ci			    via686a_read_value(data,
7768c2ecf20Sopenharmony_ci					       VIA686A_REG_TEMP_OVER[i]);
7778c2ecf20Sopenharmony_ci			data->temp_hyst[i] =
7788c2ecf20Sopenharmony_ci			    via686a_read_value(data,
7798c2ecf20Sopenharmony_ci					       VIA686A_REG_TEMP_HYST[i]);
7808c2ecf20Sopenharmony_ci		}
7818c2ecf20Sopenharmony_ci		/*
7828c2ecf20Sopenharmony_ci		 * add in lower 2 bits
7838c2ecf20Sopenharmony_ci		 * temp1 uses bits 7-6 of VIA686A_REG_TEMP_LOW1
7848c2ecf20Sopenharmony_ci		 * temp2 uses bits 5-4 of VIA686A_REG_TEMP_LOW23
7858c2ecf20Sopenharmony_ci		 * temp3 uses bits 7-6 of VIA686A_REG_TEMP_LOW23
7868c2ecf20Sopenharmony_ci		 */
7878c2ecf20Sopenharmony_ci		data->temp[0] |= (via686a_read_value(data,
7888c2ecf20Sopenharmony_ci						     VIA686A_REG_TEMP_LOW1)
7898c2ecf20Sopenharmony_ci				  & 0xc0) >> 6;
7908c2ecf20Sopenharmony_ci		data->temp[1] |=
7918c2ecf20Sopenharmony_ci		    (via686a_read_value(data, VIA686A_REG_TEMP_LOW23) &
7928c2ecf20Sopenharmony_ci		     0x30) >> 4;
7938c2ecf20Sopenharmony_ci		data->temp[2] |=
7948c2ecf20Sopenharmony_ci		    (via686a_read_value(data, VIA686A_REG_TEMP_LOW23) &
7958c2ecf20Sopenharmony_ci		     0xc0) >> 6;
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci		via686a_update_fan_div(data);
7988c2ecf20Sopenharmony_ci		data->alarms =
7998c2ecf20Sopenharmony_ci		    via686a_read_value(data,
8008c2ecf20Sopenharmony_ci				       VIA686A_REG_ALARM1) |
8018c2ecf20Sopenharmony_ci		    (via686a_read_value(data, VIA686A_REG_ALARM2) << 8);
8028c2ecf20Sopenharmony_ci		data->last_updated = jiffies;
8038c2ecf20Sopenharmony_ci		data->valid = 1;
8048c2ecf20Sopenharmony_ci	}
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	return data;
8098c2ecf20Sopenharmony_ci}
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_cistatic const struct pci_device_id via686a_pci_ids[] = {
8128c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_4) },
8138c2ecf20Sopenharmony_ci	{ }
8148c2ecf20Sopenharmony_ci};
8158c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, via686a_pci_ids);
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_cistatic int via686a_device_add(unsigned short address)
8188c2ecf20Sopenharmony_ci{
8198c2ecf20Sopenharmony_ci	struct resource res = {
8208c2ecf20Sopenharmony_ci		.start	= address,
8218c2ecf20Sopenharmony_ci		.end	= address + VIA686A_EXTENT - 1,
8228c2ecf20Sopenharmony_ci		.name	= "via686a",
8238c2ecf20Sopenharmony_ci		.flags	= IORESOURCE_IO,
8248c2ecf20Sopenharmony_ci	};
8258c2ecf20Sopenharmony_ci	int err;
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	err = acpi_check_resource_conflict(&res);
8288c2ecf20Sopenharmony_ci	if (err)
8298c2ecf20Sopenharmony_ci		goto exit;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	pdev = platform_device_alloc("via686a", address);
8328c2ecf20Sopenharmony_ci	if (!pdev) {
8338c2ecf20Sopenharmony_ci		err = -ENOMEM;
8348c2ecf20Sopenharmony_ci		pr_err("Device allocation failed\n");
8358c2ecf20Sopenharmony_ci		goto exit;
8368c2ecf20Sopenharmony_ci	}
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	err = platform_device_add_resources(pdev, &res, 1);
8398c2ecf20Sopenharmony_ci	if (err) {
8408c2ecf20Sopenharmony_ci		pr_err("Device resource addition failed (%d)\n", err);
8418c2ecf20Sopenharmony_ci		goto exit_device_put;
8428c2ecf20Sopenharmony_ci	}
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	err = platform_device_add(pdev);
8458c2ecf20Sopenharmony_ci	if (err) {
8468c2ecf20Sopenharmony_ci		pr_err("Device addition failed (%d)\n", err);
8478c2ecf20Sopenharmony_ci		goto exit_device_put;
8488c2ecf20Sopenharmony_ci	}
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	return 0;
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ciexit_device_put:
8538c2ecf20Sopenharmony_ci	platform_device_put(pdev);
8548c2ecf20Sopenharmony_ciexit:
8558c2ecf20Sopenharmony_ci	return err;
8568c2ecf20Sopenharmony_ci}
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_cistatic int via686a_pci_probe(struct pci_dev *dev,
8598c2ecf20Sopenharmony_ci				       const struct pci_device_id *id)
8608c2ecf20Sopenharmony_ci{
8618c2ecf20Sopenharmony_ci	u16 address, val;
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	if (force_addr) {
8648c2ecf20Sopenharmony_ci		address = force_addr & ~(VIA686A_EXTENT - 1);
8658c2ecf20Sopenharmony_ci		dev_warn(&dev->dev, "Forcing ISA address 0x%x\n", address);
8668c2ecf20Sopenharmony_ci		if (PCIBIOS_SUCCESSFUL !=
8678c2ecf20Sopenharmony_ci		    pci_write_config_word(dev, VIA686A_BASE_REG, address | 1))
8688c2ecf20Sopenharmony_ci			return -ENODEV;
8698c2ecf20Sopenharmony_ci	}
8708c2ecf20Sopenharmony_ci	if (PCIBIOS_SUCCESSFUL !=
8718c2ecf20Sopenharmony_ci	    pci_read_config_word(dev, VIA686A_BASE_REG, &val))
8728c2ecf20Sopenharmony_ci		return -ENODEV;
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	address = val & ~(VIA686A_EXTENT - 1);
8758c2ecf20Sopenharmony_ci	if (address == 0) {
8768c2ecf20Sopenharmony_ci		dev_err(&dev->dev,
8778c2ecf20Sopenharmony_ci			"base address not set - upgrade BIOS or use force_addr=0xaddr\n");
8788c2ecf20Sopenharmony_ci		return -ENODEV;
8798c2ecf20Sopenharmony_ci	}
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	if (PCIBIOS_SUCCESSFUL !=
8828c2ecf20Sopenharmony_ci	    pci_read_config_word(dev, VIA686A_ENABLE_REG, &val))
8838c2ecf20Sopenharmony_ci		return -ENODEV;
8848c2ecf20Sopenharmony_ci	if (!(val & 0x0001)) {
8858c2ecf20Sopenharmony_ci		if (!force_addr) {
8868c2ecf20Sopenharmony_ci			dev_warn(&dev->dev,
8878c2ecf20Sopenharmony_ci				 "Sensors disabled, enable with force_addr=0x%x\n",
8888c2ecf20Sopenharmony_ci				 address);
8898c2ecf20Sopenharmony_ci			return -ENODEV;
8908c2ecf20Sopenharmony_ci		}
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci		dev_warn(&dev->dev, "Enabling sensors\n");
8938c2ecf20Sopenharmony_ci		if (PCIBIOS_SUCCESSFUL !=
8948c2ecf20Sopenharmony_ci		    pci_write_config_word(dev, VIA686A_ENABLE_REG,
8958c2ecf20Sopenharmony_ci					  val | 0x0001))
8968c2ecf20Sopenharmony_ci			return -ENODEV;
8978c2ecf20Sopenharmony_ci	}
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	if (platform_driver_register(&via686a_driver))
9008c2ecf20Sopenharmony_ci		goto exit;
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci	/* Sets global pdev as a side effect */
9038c2ecf20Sopenharmony_ci	if (via686a_device_add(address))
9048c2ecf20Sopenharmony_ci		goto exit_unregister;
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	/*
9078c2ecf20Sopenharmony_ci	 * Always return failure here.  This is to allow other drivers to bind
9088c2ecf20Sopenharmony_ci	 * to this pci device.  We don't really want to have control over the
9098c2ecf20Sopenharmony_ci	 * pci device, we only wanted to read as few register values from it.
9108c2ecf20Sopenharmony_ci	 */
9118c2ecf20Sopenharmony_ci	s_bridge = pci_dev_get(dev);
9128c2ecf20Sopenharmony_ci	return -ENODEV;
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ciexit_unregister:
9158c2ecf20Sopenharmony_ci	platform_driver_unregister(&via686a_driver);
9168c2ecf20Sopenharmony_ciexit:
9178c2ecf20Sopenharmony_ci	return -ENODEV;
9188c2ecf20Sopenharmony_ci}
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_cistatic struct pci_driver via686a_pci_driver = {
9218c2ecf20Sopenharmony_ci	.name		= "via686a",
9228c2ecf20Sopenharmony_ci	.id_table	= via686a_pci_ids,
9238c2ecf20Sopenharmony_ci	.probe		= via686a_pci_probe,
9248c2ecf20Sopenharmony_ci};
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_cistatic int __init sm_via686a_init(void)
9278c2ecf20Sopenharmony_ci{
9288c2ecf20Sopenharmony_ci	return pci_register_driver(&via686a_pci_driver);
9298c2ecf20Sopenharmony_ci}
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_cistatic void __exit sm_via686a_exit(void)
9328c2ecf20Sopenharmony_ci{
9338c2ecf20Sopenharmony_ci	pci_unregister_driver(&via686a_pci_driver);
9348c2ecf20Sopenharmony_ci	if (s_bridge != NULL) {
9358c2ecf20Sopenharmony_ci		platform_device_unregister(pdev);
9368c2ecf20Sopenharmony_ci		platform_driver_unregister(&via686a_driver);
9378c2ecf20Sopenharmony_ci		pci_dev_put(s_bridge);
9388c2ecf20Sopenharmony_ci		s_bridge = NULL;
9398c2ecf20Sopenharmony_ci	}
9408c2ecf20Sopenharmony_ci}
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ciMODULE_AUTHOR("Kyösti Mälkki <kmalkki@cc.hut.fi>, "
9438c2ecf20Sopenharmony_ci	      "Mark Studebaker <mdsxyz123@yahoo.com> "
9448c2ecf20Sopenharmony_ci	      "and Bob Dougherty <bobd@stanford.edu>");
9458c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("VIA 686A Sensor device");
9468c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_cimodule_init(sm_via686a_init);
9498c2ecf20Sopenharmony_cimodule_exit(sm_via686a_exit);
950