18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * w83627hf.c - Part of lm_sensors, Linux kernel modules for hardware
48c2ecf20Sopenharmony_ci *		monitoring
58c2ecf20Sopenharmony_ci * Copyright (c) 1998 - 2003  Frodo Looijaard <frodol@dds.nl>,
68c2ecf20Sopenharmony_ci *			      Philip Edelbrock <phil@netroedge.com>,
78c2ecf20Sopenharmony_ci *			      and Mark Studebaker <mdsxyz123@yahoo.com>
88c2ecf20Sopenharmony_ci * Ported to 2.6 by Bernhard C. Schrenk <clemy@clemy.org>
98c2ecf20Sopenharmony_ci * Copyright (c) 2007 - 1012  Jean Delvare <jdelvare@suse.de>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci * Supports following chips:
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * Chip		#vin	#fanin	#pwm	#temp	wchipid	vendid	i2c	ISA
168c2ecf20Sopenharmony_ci * w83627hf	9	3	2	3	0x20	0x5ca3	no	yes(LPC)
178c2ecf20Sopenharmony_ci * w83627thf	7	3	3	3	0x90	0x5ca3	no	yes(LPC)
188c2ecf20Sopenharmony_ci * w83637hf	7	3	3	3	0x80	0x5ca3	no	yes(LPC)
198c2ecf20Sopenharmony_ci * w83687thf	7	3	3	3	0x90	0x5ca3	no	yes(LPC)
208c2ecf20Sopenharmony_ci * w83697hf	8	2	2	2	0x60	0x5ca3	no	yes(LPC)
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * For other winbond chips, and for i2c support in the above chips,
238c2ecf20Sopenharmony_ci * use w83781d.c.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * Note: automatic ("cruise") fan control for 697, 637 & 627thf not
268c2ecf20Sopenharmony_ci * supported yet.
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <linux/module.h>
328c2ecf20Sopenharmony_ci#include <linux/init.h>
338c2ecf20Sopenharmony_ci#include <linux/slab.h>
348c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
358c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
368c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
378c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
388c2ecf20Sopenharmony_ci#include <linux/hwmon-vid.h>
398c2ecf20Sopenharmony_ci#include <linux/err.h>
408c2ecf20Sopenharmony_ci#include <linux/mutex.h>
418c2ecf20Sopenharmony_ci#include <linux/ioport.h>
428c2ecf20Sopenharmony_ci#include <linux/acpi.h>
438c2ecf20Sopenharmony_ci#include <linux/io.h>
448c2ecf20Sopenharmony_ci#include "lm75.h"
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic struct platform_device *pdev;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define DRVNAME "w83627hf"
498c2ecf20Sopenharmony_cienum chips { w83627hf, w83627thf, w83697hf, w83637hf, w83687thf };
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistruct w83627hf_sio_data {
528c2ecf20Sopenharmony_ci	enum chips type;
538c2ecf20Sopenharmony_ci	int sioaddr;
548c2ecf20Sopenharmony_ci};
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic u8 force_i2c = 0x1f;
578c2ecf20Sopenharmony_cimodule_param(force_i2c, byte, 0);
588c2ecf20Sopenharmony_ciMODULE_PARM_DESC(force_i2c,
598c2ecf20Sopenharmony_ci		 "Initialize the i2c address of the sensors");
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic bool init = 1;
628c2ecf20Sopenharmony_cimodule_param(init, bool, 0);
638c2ecf20Sopenharmony_ciMODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic unsigned short force_id;
668c2ecf20Sopenharmony_cimodule_param(force_id, ushort, 0);
678c2ecf20Sopenharmony_ciMODULE_PARM_DESC(force_id, "Override the detected device ID");
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/* modified from kernel/include/traps.c */
708c2ecf20Sopenharmony_ci#define DEV			0x07 /* Register: Logical device select */
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/* logical device numbers for superio_select (below) */
738c2ecf20Sopenharmony_ci#define W83627HF_LD_FDC		0x00
748c2ecf20Sopenharmony_ci#define W83627HF_LD_PRT		0x01
758c2ecf20Sopenharmony_ci#define W83627HF_LD_UART1	0x02
768c2ecf20Sopenharmony_ci#define W83627HF_LD_UART2	0x03
778c2ecf20Sopenharmony_ci#define W83627HF_LD_KBC		0x05
788c2ecf20Sopenharmony_ci#define W83627HF_LD_CIR		0x06 /* w83627hf only */
798c2ecf20Sopenharmony_ci#define W83627HF_LD_GAME	0x07
808c2ecf20Sopenharmony_ci#define W83627HF_LD_MIDI	0x07
818c2ecf20Sopenharmony_ci#define W83627HF_LD_GPIO1	0x07
828c2ecf20Sopenharmony_ci#define W83627HF_LD_GPIO5	0x07 /* w83627thf only */
838c2ecf20Sopenharmony_ci#define W83627HF_LD_GPIO2	0x08
848c2ecf20Sopenharmony_ci#define W83627HF_LD_GPIO3	0x09
858c2ecf20Sopenharmony_ci#define W83627HF_LD_GPIO4	0x09 /* w83627thf only */
868c2ecf20Sopenharmony_ci#define W83627HF_LD_ACPI	0x0a
878c2ecf20Sopenharmony_ci#define W83627HF_LD_HWM		0x0b
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define DEVID			0x20 /* Register: Device ID */
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci#define W83627THF_GPIO5_EN	0x30 /* w83627thf only */
928c2ecf20Sopenharmony_ci#define W83627THF_GPIO5_IOSR	0xf3 /* w83627thf only */
938c2ecf20Sopenharmony_ci#define W83627THF_GPIO5_DR	0xf4 /* w83627thf only */
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#define W83687THF_VID_EN	0x29 /* w83687thf only */
968c2ecf20Sopenharmony_ci#define W83687THF_VID_CFG	0xF0 /* w83687thf only */
978c2ecf20Sopenharmony_ci#define W83687THF_VID_DATA	0xF1 /* w83687thf only */
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic inline void
1008c2ecf20Sopenharmony_cisuperio_outb(struct w83627hf_sio_data *sio, int reg, int val)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	outb(reg, sio->sioaddr);
1038c2ecf20Sopenharmony_ci	outb(val, sio->sioaddr + 1);
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic inline int
1078c2ecf20Sopenharmony_cisuperio_inb(struct w83627hf_sio_data *sio, int reg)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	outb(reg, sio->sioaddr);
1108c2ecf20Sopenharmony_ci	return inb(sio->sioaddr + 1);
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic inline void
1148c2ecf20Sopenharmony_cisuperio_select(struct w83627hf_sio_data *sio, int ld)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	outb(DEV, sio->sioaddr);
1178c2ecf20Sopenharmony_ci	outb(ld,  sio->sioaddr + 1);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic inline int
1218c2ecf20Sopenharmony_cisuperio_enter(struct w83627hf_sio_data *sio)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	if (!request_muxed_region(sio->sioaddr, 2, DRVNAME))
1248c2ecf20Sopenharmony_ci		return -EBUSY;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	outb(0x87, sio->sioaddr);
1278c2ecf20Sopenharmony_ci	outb(0x87, sio->sioaddr);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	return 0;
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic inline void
1338c2ecf20Sopenharmony_cisuperio_exit(struct w83627hf_sio_data *sio)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	outb(0xAA, sio->sioaddr);
1368c2ecf20Sopenharmony_ci	release_region(sio->sioaddr, 2);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci#define W627_DEVID 0x52
1408c2ecf20Sopenharmony_ci#define W627THF_DEVID 0x82
1418c2ecf20Sopenharmony_ci#define W697_DEVID 0x60
1428c2ecf20Sopenharmony_ci#define W637_DEVID 0x70
1438c2ecf20Sopenharmony_ci#define W687THF_DEVID 0x85
1448c2ecf20Sopenharmony_ci#define WINB_ACT_REG 0x30
1458c2ecf20Sopenharmony_ci#define WINB_BASE_REG 0x60
1468c2ecf20Sopenharmony_ci/* Constants specified below */
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci/* Alignment of the base address */
1498c2ecf20Sopenharmony_ci#define WINB_ALIGNMENT		~7
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci/* Offset & size of I/O region we are interested in */
1528c2ecf20Sopenharmony_ci#define WINB_REGION_OFFSET	5
1538c2ecf20Sopenharmony_ci#define WINB_REGION_SIZE	2
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/* Where are the sensors address/data registers relative to the region offset */
1568c2ecf20Sopenharmony_ci#define W83781D_ADDR_REG_OFFSET 0
1578c2ecf20Sopenharmony_ci#define W83781D_DATA_REG_OFFSET 1
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/* The W83781D registers */
1608c2ecf20Sopenharmony_ci/* The W83782D registers for nr=7,8 are in bank 5 */
1618c2ecf20Sopenharmony_ci#define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
1628c2ecf20Sopenharmony_ci					   (0x554 + (((nr) - 7) * 2)))
1638c2ecf20Sopenharmony_ci#define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
1648c2ecf20Sopenharmony_ci					   (0x555 + (((nr) - 7) * 2)))
1658c2ecf20Sopenharmony_ci#define W83781D_REG_IN(nr)     ((nr < 7) ? (0x20 + (nr)) : \
1668c2ecf20Sopenharmony_ci					   (0x550 + (nr) - 7))
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/* nr:0-2 for fans:1-3 */
1698c2ecf20Sopenharmony_ci#define W83627HF_REG_FAN_MIN(nr)	(0x3b + (nr))
1708c2ecf20Sopenharmony_ci#define W83627HF_REG_FAN(nr)		(0x28 + (nr))
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci#define W83627HF_REG_TEMP2_CONFIG 0x152
1738c2ecf20Sopenharmony_ci#define W83627HF_REG_TEMP3_CONFIG 0x252
1748c2ecf20Sopenharmony_ci/* these are zero-based, unlike config constants above */
1758c2ecf20Sopenharmony_cistatic const u16 w83627hf_reg_temp[]		= { 0x27, 0x150, 0x250 };
1768c2ecf20Sopenharmony_cistatic const u16 w83627hf_reg_temp_hyst[]	= { 0x3A, 0x153, 0x253 };
1778c2ecf20Sopenharmony_cistatic const u16 w83627hf_reg_temp_over[]	= { 0x39, 0x155, 0x255 };
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci#define W83781D_REG_BANK 0x4E
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci#define W83781D_REG_CONFIG 0x40
1828c2ecf20Sopenharmony_ci#define W83781D_REG_ALARM1 0x459
1838c2ecf20Sopenharmony_ci#define W83781D_REG_ALARM2 0x45A
1848c2ecf20Sopenharmony_ci#define W83781D_REG_ALARM3 0x45B
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci#define W83781D_REG_BEEP_CONFIG 0x4D
1878c2ecf20Sopenharmony_ci#define W83781D_REG_BEEP_INTS1 0x56
1888c2ecf20Sopenharmony_ci#define W83781D_REG_BEEP_INTS2 0x57
1898c2ecf20Sopenharmony_ci#define W83781D_REG_BEEP_INTS3 0x453
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci#define W83781D_REG_VID_FANDIV 0x47
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci#define W83781D_REG_CHIPID 0x49
1948c2ecf20Sopenharmony_ci#define W83781D_REG_WCHIPID 0x58
1958c2ecf20Sopenharmony_ci#define W83781D_REG_CHIPMAN 0x4F
1968c2ecf20Sopenharmony_ci#define W83781D_REG_PIN 0x4B
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci#define W83781D_REG_VBAT 0x5D
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci#define W83627HF_REG_PWM1 0x5A
2018c2ecf20Sopenharmony_ci#define W83627HF_REG_PWM2 0x5B
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic const u8 W83627THF_REG_PWM_ENABLE[] = {
2048c2ecf20Sopenharmony_ci	0x04,		/* FAN 1 mode */
2058c2ecf20Sopenharmony_ci	0x04,		/* FAN 2 mode */
2068c2ecf20Sopenharmony_ci	0x12,		/* FAN AUX mode */
2078c2ecf20Sopenharmony_ci};
2088c2ecf20Sopenharmony_cistatic const u8 W83627THF_PWM_ENABLE_SHIFT[] = { 2, 4, 1 };
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci#define W83627THF_REG_PWM1		0x01	/* 697HF/637HF/687THF too */
2118c2ecf20Sopenharmony_ci#define W83627THF_REG_PWM2		0x03	/* 697HF/637HF/687THF too */
2128c2ecf20Sopenharmony_ci#define W83627THF_REG_PWM3		0x11	/* 637HF/687THF too */
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci#define W83627THF_REG_VRM_OVT_CFG 	0x18	/* 637HF/687THF too */
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cistatic const u8 regpwm_627hf[] = { W83627HF_REG_PWM1, W83627HF_REG_PWM2 };
2178c2ecf20Sopenharmony_cistatic const u8 regpwm[] = { W83627THF_REG_PWM1, W83627THF_REG_PWM2,
2188c2ecf20Sopenharmony_ci                             W83627THF_REG_PWM3 };
2198c2ecf20Sopenharmony_ci#define W836X7HF_REG_PWM(type, nr) (((type) == w83627hf) ? \
2208c2ecf20Sopenharmony_ci				    regpwm_627hf[nr] : regpwm[nr])
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci#define W83627HF_REG_PWM_FREQ		0x5C	/* Only for the 627HF */
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci#define W83637HF_REG_PWM_FREQ1		0x00	/* 697HF/687THF too */
2258c2ecf20Sopenharmony_ci#define W83637HF_REG_PWM_FREQ2		0x02	/* 697HF/687THF too */
2268c2ecf20Sopenharmony_ci#define W83637HF_REG_PWM_FREQ3		0x10	/* 687THF too */
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic const u8 W83637HF_REG_PWM_FREQ[] = { W83637HF_REG_PWM_FREQ1,
2298c2ecf20Sopenharmony_ci					W83637HF_REG_PWM_FREQ2,
2308c2ecf20Sopenharmony_ci					W83637HF_REG_PWM_FREQ3 };
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci#define W83627HF_BASE_PWM_FREQ	46870
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci#define W83781D_REG_I2C_ADDR 0x48
2358c2ecf20Sopenharmony_ci#define W83781D_REG_I2C_SUBADDR 0x4A
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci/* Sensor selection */
2388c2ecf20Sopenharmony_ci#define W83781D_REG_SCFG1 0x5D
2398c2ecf20Sopenharmony_cistatic const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
2408c2ecf20Sopenharmony_ci#define W83781D_REG_SCFG2 0x59
2418c2ecf20Sopenharmony_cistatic const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
2428c2ecf20Sopenharmony_ci#define W83781D_DEFAULT_BETA 3435
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci/*
2458c2ecf20Sopenharmony_ci * Conversions. Limit checking is only done on the TO_REG
2468c2ecf20Sopenharmony_ci * variants. Note that you should be a bit careful with which arguments
2478c2ecf20Sopenharmony_ci * these macros are called: arguments may be evaluated more than once.
2488c2ecf20Sopenharmony_ci * Fixing this is just not worth it.
2498c2ecf20Sopenharmony_ci */
2508c2ecf20Sopenharmony_ci#define IN_TO_REG(val)  (clamp_val((((val) + 8) / 16), 0, 255))
2518c2ecf20Sopenharmony_ci#define IN_FROM_REG(val) ((val) * 16)
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic inline u8 FAN_TO_REG(long rpm, int div)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	if (rpm == 0)
2568c2ecf20Sopenharmony_ci		return 255;
2578c2ecf20Sopenharmony_ci	rpm = clamp_val(rpm, 1, 1000000);
2588c2ecf20Sopenharmony_ci	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci#define TEMP_MIN (-128000)
2628c2ecf20Sopenharmony_ci#define TEMP_MAX ( 127000)
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci/*
2658c2ecf20Sopenharmony_ci * TEMP: 0.001C/bit (-128C to +127C)
2668c2ecf20Sopenharmony_ci * REG: 1C/bit, two's complement
2678c2ecf20Sopenharmony_ci */
2688c2ecf20Sopenharmony_cistatic u8 TEMP_TO_REG(long temp)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	int ntemp = clamp_val(temp, TEMP_MIN, TEMP_MAX);
2718c2ecf20Sopenharmony_ci	ntemp += (ntemp < 0 ? -500 : 500);
2728c2ecf20Sopenharmony_ci	return (u8)(ntemp / 1000);
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cistatic int TEMP_FROM_REG(u8 reg)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci        return (s8)reg * 1000;
2788c2ecf20Sopenharmony_ci}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci#define PWM_TO_REG(val) (clamp_val((val), 0, 255))
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic inline unsigned long pwm_freq_from_reg_627hf(u8 reg)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	unsigned long freq;
2878c2ecf20Sopenharmony_ci	freq = W83627HF_BASE_PWM_FREQ >> reg;
2888c2ecf20Sopenharmony_ci	return freq;
2898c2ecf20Sopenharmony_ci}
2908c2ecf20Sopenharmony_cistatic inline u8 pwm_freq_to_reg_627hf(unsigned long val)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	u8 i;
2938c2ecf20Sopenharmony_ci	/*
2948c2ecf20Sopenharmony_ci	 * Only 5 dividers (1 2 4 8 16)
2958c2ecf20Sopenharmony_ci	 * Search for the nearest available frequency
2968c2ecf20Sopenharmony_ci	 */
2978c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
2988c2ecf20Sopenharmony_ci		if (val > (((W83627HF_BASE_PWM_FREQ >> i) +
2998c2ecf20Sopenharmony_ci			    (W83627HF_BASE_PWM_FREQ >> (i+1))) / 2))
3008c2ecf20Sopenharmony_ci			break;
3018c2ecf20Sopenharmony_ci	}
3028c2ecf20Sopenharmony_ci	return i;
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cistatic inline unsigned long pwm_freq_from_reg(u8 reg)
3068c2ecf20Sopenharmony_ci{
3078c2ecf20Sopenharmony_ci	/* Clock bit 8 -> 180 kHz or 24 MHz */
3088c2ecf20Sopenharmony_ci	unsigned long clock = (reg & 0x80) ? 180000UL : 24000000UL;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	reg &= 0x7f;
3118c2ecf20Sopenharmony_ci	/* This should not happen but anyway... */
3128c2ecf20Sopenharmony_ci	if (reg == 0)
3138c2ecf20Sopenharmony_ci		reg++;
3148c2ecf20Sopenharmony_ci	return clock / (reg << 8);
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_cistatic inline u8 pwm_freq_to_reg(unsigned long val)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	/* Minimum divider value is 0x01 and maximum is 0x7F */
3198c2ecf20Sopenharmony_ci	if (val >= 93750)	/* The highest we can do */
3208c2ecf20Sopenharmony_ci		return 0x01;
3218c2ecf20Sopenharmony_ci	if (val >= 720)	/* Use 24 MHz clock */
3228c2ecf20Sopenharmony_ci		return 24000000UL / (val << 8);
3238c2ecf20Sopenharmony_ci	if (val < 6)		/* The lowest we can do */
3248c2ecf20Sopenharmony_ci		return 0xFF;
3258c2ecf20Sopenharmony_ci	else			/* Use 180 kHz clock */
3268c2ecf20Sopenharmony_ci		return 0x80 | (180000UL / (val << 8));
3278c2ecf20Sopenharmony_ci}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci#define BEEP_MASK_FROM_REG(val)		((val) & 0xff7fff)
3308c2ecf20Sopenharmony_ci#define BEEP_MASK_TO_REG(val)		((val) & 0xff7fff)
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci#define DIV_FROM_REG(val) (1 << (val))
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic inline u8 DIV_TO_REG(long val)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	int i;
3378c2ecf20Sopenharmony_ci	val = clamp_val(val, 1, 128) >> 1;
3388c2ecf20Sopenharmony_ci	for (i = 0; i < 7; i++) {
3398c2ecf20Sopenharmony_ci		if (val == 0)
3408c2ecf20Sopenharmony_ci			break;
3418c2ecf20Sopenharmony_ci		val >>= 1;
3428c2ecf20Sopenharmony_ci	}
3438c2ecf20Sopenharmony_ci	return (u8)i;
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci/*
3478c2ecf20Sopenharmony_ci * For each registered chip, we need to keep some data in memory.
3488c2ecf20Sopenharmony_ci * The structure is dynamically allocated.
3498c2ecf20Sopenharmony_ci */
3508c2ecf20Sopenharmony_cistruct w83627hf_data {
3518c2ecf20Sopenharmony_ci	unsigned short addr;
3528c2ecf20Sopenharmony_ci	const char *name;
3538c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
3548c2ecf20Sopenharmony_ci	struct mutex lock;
3558c2ecf20Sopenharmony_ci	enum chips type;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	struct mutex update_lock;
3588c2ecf20Sopenharmony_ci	char valid;		/* !=0 if following fields are valid */
3598c2ecf20Sopenharmony_ci	unsigned long last_updated;	/* In jiffies */
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	u8 in[9];		/* Register value */
3628c2ecf20Sopenharmony_ci	u8 in_max[9];		/* Register value */
3638c2ecf20Sopenharmony_ci	u8 in_min[9];		/* Register value */
3648c2ecf20Sopenharmony_ci	u8 fan[3];		/* Register value */
3658c2ecf20Sopenharmony_ci	u8 fan_min[3];		/* Register value */
3668c2ecf20Sopenharmony_ci	u16 temp[3];		/* Register value */
3678c2ecf20Sopenharmony_ci	u16 temp_max[3];	/* Register value */
3688c2ecf20Sopenharmony_ci	u16 temp_max_hyst[3];	/* Register value */
3698c2ecf20Sopenharmony_ci	u8 fan_div[3];		/* Register encoding, shifted right */
3708c2ecf20Sopenharmony_ci	u8 vid;			/* Register encoding, combined */
3718c2ecf20Sopenharmony_ci	u32 alarms;		/* Register encoding, combined */
3728c2ecf20Sopenharmony_ci	u32 beep_mask;		/* Register encoding, combined */
3738c2ecf20Sopenharmony_ci	u8 pwm[3];		/* Register value */
3748c2ecf20Sopenharmony_ci	u8 pwm_enable[3];	/* 1 = manual
3758c2ecf20Sopenharmony_ci				 * 2 = thermal cruise (also called SmartFan I)
3768c2ecf20Sopenharmony_ci				 * 3 = fan speed cruise
3778c2ecf20Sopenharmony_ci				 */
3788c2ecf20Sopenharmony_ci	u8 pwm_freq[3];		/* Register value */
3798c2ecf20Sopenharmony_ci	u16 sens[3];		/* 1 = pentium diode; 2 = 3904 diode;
3808c2ecf20Sopenharmony_ci				 * 4 = thermistor
3818c2ecf20Sopenharmony_ci				 */
3828c2ecf20Sopenharmony_ci	u8 vrm;
3838c2ecf20Sopenharmony_ci	u8 vrm_ovt;		/* Register value, 627THF/637HF/687THF only */
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
3868c2ecf20Sopenharmony_ci	/* Remember extra register values over suspend/resume */
3878c2ecf20Sopenharmony_ci	u8 scfg1;
3888c2ecf20Sopenharmony_ci	u8 scfg2;
3898c2ecf20Sopenharmony_ci#endif
3908c2ecf20Sopenharmony_ci};
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_cistatic int w83627hf_probe(struct platform_device *pdev);
3938c2ecf20Sopenharmony_cistatic int w83627hf_remove(struct platform_device *pdev);
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_cistatic int w83627hf_read_value(struct w83627hf_data *data, u16 reg);
3968c2ecf20Sopenharmony_cistatic int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value);
3978c2ecf20Sopenharmony_cistatic void w83627hf_update_fan_div(struct w83627hf_data *data);
3988c2ecf20Sopenharmony_cistatic struct w83627hf_data *w83627hf_update_device(struct device *dev);
3998c2ecf20Sopenharmony_cistatic void w83627hf_init_device(struct platform_device *pdev);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
4028c2ecf20Sopenharmony_cistatic int w83627hf_suspend(struct device *dev)
4038c2ecf20Sopenharmony_ci{
4048c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
4078c2ecf20Sopenharmony_ci	data->scfg1 = w83627hf_read_value(data, W83781D_REG_SCFG1);
4088c2ecf20Sopenharmony_ci	data->scfg2 = w83627hf_read_value(data, W83781D_REG_SCFG2);
4098c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	return 0;
4128c2ecf20Sopenharmony_ci}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cistatic int w83627hf_resume(struct device *dev)
4158c2ecf20Sopenharmony_ci{
4168c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
4178c2ecf20Sopenharmony_ci	int i, num_temps = (data->type == w83697hf) ? 2 : 3;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	/* Restore limits */
4208c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
4218c2ecf20Sopenharmony_ci	for (i = 0; i <= 8; i++) {
4228c2ecf20Sopenharmony_ci		/* skip missing sensors */
4238c2ecf20Sopenharmony_ci		if (((data->type == w83697hf) && (i == 1)) ||
4248c2ecf20Sopenharmony_ci		    ((data->type != w83627hf && data->type != w83697hf)
4258c2ecf20Sopenharmony_ci		    && (i == 5 || i == 6)))
4268c2ecf20Sopenharmony_ci			continue;
4278c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83781D_REG_IN_MAX(i),
4288c2ecf20Sopenharmony_ci				     data->in_max[i]);
4298c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83781D_REG_IN_MIN(i),
4308c2ecf20Sopenharmony_ci				     data->in_min[i]);
4318c2ecf20Sopenharmony_ci	}
4328c2ecf20Sopenharmony_ci	for (i = 0; i <= 2; i++)
4338c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83627HF_REG_FAN_MIN(i),
4348c2ecf20Sopenharmony_ci				     data->fan_min[i]);
4358c2ecf20Sopenharmony_ci	for (i = 0; i < num_temps; i++) {
4368c2ecf20Sopenharmony_ci		w83627hf_write_value(data, w83627hf_reg_temp_over[i],
4378c2ecf20Sopenharmony_ci				     data->temp_max[i]);
4388c2ecf20Sopenharmony_ci		w83627hf_write_value(data, w83627hf_reg_temp_hyst[i],
4398c2ecf20Sopenharmony_ci				     data->temp_max_hyst[i]);
4408c2ecf20Sopenharmony_ci	}
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	/* Fixup BIOS bugs */
4438c2ecf20Sopenharmony_ci	if (data->type == w83627thf || data->type == w83637hf ||
4448c2ecf20Sopenharmony_ci	    data->type == w83687thf)
4458c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83627THF_REG_VRM_OVT_CFG,
4468c2ecf20Sopenharmony_ci				     data->vrm_ovt);
4478c2ecf20Sopenharmony_ci	w83627hf_write_value(data, W83781D_REG_SCFG1, data->scfg1);
4488c2ecf20Sopenharmony_ci	w83627hf_write_value(data, W83781D_REG_SCFG2, data->scfg2);
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	/* Force re-reading all values */
4518c2ecf20Sopenharmony_ci	data->valid = 0;
4528c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	return 0;
4558c2ecf20Sopenharmony_ci}
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_cistatic const struct dev_pm_ops w83627hf_dev_pm_ops = {
4588c2ecf20Sopenharmony_ci	.suspend = w83627hf_suspend,
4598c2ecf20Sopenharmony_ci	.resume = w83627hf_resume,
4608c2ecf20Sopenharmony_ci};
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci#define W83627HF_DEV_PM_OPS	(&w83627hf_dev_pm_ops)
4638c2ecf20Sopenharmony_ci#else
4648c2ecf20Sopenharmony_ci#define W83627HF_DEV_PM_OPS	NULL
4658c2ecf20Sopenharmony_ci#endif /* CONFIG_PM */
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_cistatic struct platform_driver w83627hf_driver = {
4688c2ecf20Sopenharmony_ci	.driver = {
4698c2ecf20Sopenharmony_ci		.name	= DRVNAME,
4708c2ecf20Sopenharmony_ci		.pm	= W83627HF_DEV_PM_OPS,
4718c2ecf20Sopenharmony_ci	},
4728c2ecf20Sopenharmony_ci	.probe		= w83627hf_probe,
4738c2ecf20Sopenharmony_ci	.remove		= w83627hf_remove,
4748c2ecf20Sopenharmony_ci};
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_cistatic ssize_t
4778c2ecf20Sopenharmony_ciin_input_show(struct device *dev, struct device_attribute *devattr, char *buf)
4788c2ecf20Sopenharmony_ci{
4798c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
4808c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
4818c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in[nr]));
4828c2ecf20Sopenharmony_ci}
4838c2ecf20Sopenharmony_cistatic ssize_t
4848c2ecf20Sopenharmony_ciin_min_show(struct device *dev, struct device_attribute *devattr, char *buf)
4858c2ecf20Sopenharmony_ci{
4868c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
4878c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
4888c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_min[nr]));
4898c2ecf20Sopenharmony_ci}
4908c2ecf20Sopenharmony_cistatic ssize_t
4918c2ecf20Sopenharmony_ciin_max_show(struct device *dev, struct device_attribute *devattr, char *buf)
4928c2ecf20Sopenharmony_ci{
4938c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
4948c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
4958c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_max[nr]));
4968c2ecf20Sopenharmony_ci}
4978c2ecf20Sopenharmony_cistatic ssize_t
4988c2ecf20Sopenharmony_ciin_min_store(struct device *dev, struct device_attribute *devattr,
4998c2ecf20Sopenharmony_ci	     const char *buf, size_t count)
5008c2ecf20Sopenharmony_ci{
5018c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
5028c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
5038c2ecf20Sopenharmony_ci	long val;
5048c2ecf20Sopenharmony_ci	int err;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
5078c2ecf20Sopenharmony_ci	if (err)
5088c2ecf20Sopenharmony_ci		return err;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
5118c2ecf20Sopenharmony_ci	data->in_min[nr] = IN_TO_REG(val);
5128c2ecf20Sopenharmony_ci	w83627hf_write_value(data, W83781D_REG_IN_MIN(nr), data->in_min[nr]);
5138c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
5148c2ecf20Sopenharmony_ci	return count;
5158c2ecf20Sopenharmony_ci}
5168c2ecf20Sopenharmony_cistatic ssize_t
5178c2ecf20Sopenharmony_ciin_max_store(struct device *dev, struct device_attribute *devattr,
5188c2ecf20Sopenharmony_ci	     const char *buf, size_t count)
5198c2ecf20Sopenharmony_ci{
5208c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
5218c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
5228c2ecf20Sopenharmony_ci	long val;
5238c2ecf20Sopenharmony_ci	int err;
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
5268c2ecf20Sopenharmony_ci	if (err)
5278c2ecf20Sopenharmony_ci		return err;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
5308c2ecf20Sopenharmony_ci	data->in_max[nr] = IN_TO_REG(val);
5318c2ecf20Sopenharmony_ci	w83627hf_write_value(data, W83781D_REG_IN_MAX(nr), data->in_max[nr]);
5328c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
5338c2ecf20Sopenharmony_ci	return count;
5348c2ecf20Sopenharmony_ci}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_input, in_input, 1);
5378c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
5388c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
5398c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_input, in_input, 2);
5408c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
5418c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
5428c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_input, in_input, 3);
5438c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
5448c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
5458c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_input, in_input, 4);
5468c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
5478c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
5488c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in5_input, in_input, 5);
5498c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
5508c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
5518c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in6_input, in_input, 6);
5528c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 6);
5538c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 6);
5548c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in7_input, in_input, 7);
5558c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in7_min, in_min, 7);
5568c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in7_max, in_max, 7);
5578c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in8_input, in_input, 8);
5588c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in8_min, in_min, 8);
5598c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in8_max, in_max, 8);
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci/* use a different set of functions for in0 */
5628c2ecf20Sopenharmony_cistatic ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
5638c2ecf20Sopenharmony_ci{
5648c2ecf20Sopenharmony_ci	long in0;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	if ((data->vrm_ovt & 0x01) &&
5678c2ecf20Sopenharmony_ci		(w83627thf == data->type || w83637hf == data->type
5688c2ecf20Sopenharmony_ci		 || w83687thf == data->type))
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci		/* use VRM9 calculation */
5718c2ecf20Sopenharmony_ci		in0 = (long)((reg * 488 + 70000 + 50) / 100);
5728c2ecf20Sopenharmony_ci	else
5738c2ecf20Sopenharmony_ci		/* use VRM8 (standard) calculation */
5748c2ecf20Sopenharmony_ci		in0 = (long)IN_FROM_REG(reg);
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	return sprintf(buf,"%ld\n", in0);
5778c2ecf20Sopenharmony_ci}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_cistatic ssize_t in0_input_show(struct device *dev,
5808c2ecf20Sopenharmony_ci			      struct device_attribute *attr, char *buf)
5818c2ecf20Sopenharmony_ci{
5828c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
5838c2ecf20Sopenharmony_ci	return show_in_0(data, buf, data->in[0]);
5848c2ecf20Sopenharmony_ci}
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_cistatic ssize_t in0_min_show(struct device *dev, struct device_attribute *attr,
5878c2ecf20Sopenharmony_ci			    char *buf)
5888c2ecf20Sopenharmony_ci{
5898c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
5908c2ecf20Sopenharmony_ci	return show_in_0(data, buf, data->in_min[0]);
5918c2ecf20Sopenharmony_ci}
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_cistatic ssize_t in0_max_show(struct device *dev, struct device_attribute *attr,
5948c2ecf20Sopenharmony_ci			    char *buf)
5958c2ecf20Sopenharmony_ci{
5968c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
5978c2ecf20Sopenharmony_ci	return show_in_0(data, buf, data->in_max[0]);
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_cistatic ssize_t in0_min_store(struct device *dev,
6018c2ecf20Sopenharmony_ci			     struct device_attribute *attr, const char *buf,
6028c2ecf20Sopenharmony_ci			     size_t count)
6038c2ecf20Sopenharmony_ci{
6048c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
6058c2ecf20Sopenharmony_ci	unsigned long val;
6068c2ecf20Sopenharmony_ci	int err;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
6098c2ecf20Sopenharmony_ci	if (err)
6108c2ecf20Sopenharmony_ci		return err;
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	if ((data->vrm_ovt & 0x01) &&
6158c2ecf20Sopenharmony_ci		(w83627thf == data->type || w83637hf == data->type
6168c2ecf20Sopenharmony_ci		 || w83687thf == data->type))
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci		/* use VRM9 calculation */
6198c2ecf20Sopenharmony_ci		data->in_min[0] =
6208c2ecf20Sopenharmony_ci			clamp_val(((val * 100) - 70000 + 244) / 488, 0, 255);
6218c2ecf20Sopenharmony_ci	else
6228c2ecf20Sopenharmony_ci		/* use VRM8 (standard) calculation */
6238c2ecf20Sopenharmony_ci		data->in_min[0] = IN_TO_REG(val);
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	w83627hf_write_value(data, W83781D_REG_IN_MIN(0), data->in_min[0]);
6268c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
6278c2ecf20Sopenharmony_ci	return count;
6288c2ecf20Sopenharmony_ci}
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_cistatic ssize_t in0_max_store(struct device *dev,
6318c2ecf20Sopenharmony_ci			     struct device_attribute *attr, const char *buf,
6328c2ecf20Sopenharmony_ci			     size_t count)
6338c2ecf20Sopenharmony_ci{
6348c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
6358c2ecf20Sopenharmony_ci	unsigned long val;
6368c2ecf20Sopenharmony_ci	int err;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
6398c2ecf20Sopenharmony_ci	if (err)
6408c2ecf20Sopenharmony_ci		return err;
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	if ((data->vrm_ovt & 0x01) &&
6458c2ecf20Sopenharmony_ci		(w83627thf == data->type || w83637hf == data->type
6468c2ecf20Sopenharmony_ci		 || w83687thf == data->type))
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci		/* use VRM9 calculation */
6498c2ecf20Sopenharmony_ci		data->in_max[0] =
6508c2ecf20Sopenharmony_ci			clamp_val(((val * 100) - 70000 + 244) / 488, 0, 255);
6518c2ecf20Sopenharmony_ci	else
6528c2ecf20Sopenharmony_ci		/* use VRM8 (standard) calculation */
6538c2ecf20Sopenharmony_ci		data->in_max[0] = IN_TO_REG(val);
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	w83627hf_write_value(data, W83781D_REG_IN_MAX(0), data->in_max[0]);
6568c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
6578c2ecf20Sopenharmony_ci	return count;
6588c2ecf20Sopenharmony_ci}
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(in0_input);
6618c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(in0_min);
6628c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(in0_max);
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_cistatic ssize_t
6658c2ecf20Sopenharmony_cifan_input_show(struct device *dev, struct device_attribute *devattr,
6668c2ecf20Sopenharmony_ci	       char *buf)
6678c2ecf20Sopenharmony_ci{
6688c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
6698c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
6708c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan[nr],
6718c2ecf20Sopenharmony_ci				(long)DIV_FROM_REG(data->fan_div[nr])));
6728c2ecf20Sopenharmony_ci}
6738c2ecf20Sopenharmony_cistatic ssize_t
6748c2ecf20Sopenharmony_cifan_min_show(struct device *dev, struct device_attribute *devattr, char *buf)
6758c2ecf20Sopenharmony_ci{
6768c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
6778c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
6788c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan_min[nr],
6798c2ecf20Sopenharmony_ci				(long)DIV_FROM_REG(data->fan_div[nr])));
6808c2ecf20Sopenharmony_ci}
6818c2ecf20Sopenharmony_cistatic ssize_t
6828c2ecf20Sopenharmony_cifan_min_store(struct device *dev, struct device_attribute *devattr,
6838c2ecf20Sopenharmony_ci	      const char *buf, size_t count)
6848c2ecf20Sopenharmony_ci{
6858c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
6868c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
6878c2ecf20Sopenharmony_ci	unsigned long val;
6888c2ecf20Sopenharmony_ci	int err;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
6918c2ecf20Sopenharmony_ci	if (err)
6928c2ecf20Sopenharmony_ci		return err;
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
6958c2ecf20Sopenharmony_ci	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
6968c2ecf20Sopenharmony_ci	w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr),
6978c2ecf20Sopenharmony_ci			     data->fan_min[nr]);
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
7008c2ecf20Sopenharmony_ci	return count;
7018c2ecf20Sopenharmony_ci}
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
7048c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
7058c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
7068c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
7078c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan3_input, fan_input, 2);
7088c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_cistatic ssize_t
7118c2ecf20Sopenharmony_citemp_show(struct device *dev, struct device_attribute *devattr, char *buf)
7128c2ecf20Sopenharmony_ci{
7138c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
7148c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	u16 tmp = data->temp[nr];
7178c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
7188c2ecf20Sopenharmony_ci					  : (long) TEMP_FROM_REG(tmp));
7198c2ecf20Sopenharmony_ci}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_cistatic ssize_t
7228c2ecf20Sopenharmony_citemp_max_show(struct device *dev, struct device_attribute *devattr, char *buf)
7238c2ecf20Sopenharmony_ci{
7248c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
7258c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	u16 tmp = data->temp_max[nr];
7288c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
7298c2ecf20Sopenharmony_ci					  : (long) TEMP_FROM_REG(tmp));
7308c2ecf20Sopenharmony_ci}
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_cistatic ssize_t
7338c2ecf20Sopenharmony_citemp_max_hyst_show(struct device *dev, struct device_attribute *devattr,
7348c2ecf20Sopenharmony_ci		   char *buf)
7358c2ecf20Sopenharmony_ci{
7368c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
7378c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	u16 tmp = data->temp_max_hyst[nr];
7408c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
7418c2ecf20Sopenharmony_ci					  : (long) TEMP_FROM_REG(tmp));
7428c2ecf20Sopenharmony_ci}
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_cistatic ssize_t
7458c2ecf20Sopenharmony_citemp_max_store(struct device *dev, struct device_attribute *devattr,
7468c2ecf20Sopenharmony_ci	       const char *buf, size_t count)
7478c2ecf20Sopenharmony_ci{
7488c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
7498c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
7508c2ecf20Sopenharmony_ci	u16 tmp;
7518c2ecf20Sopenharmony_ci	long val;
7528c2ecf20Sopenharmony_ci	int err;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
7558c2ecf20Sopenharmony_ci	if (err)
7568c2ecf20Sopenharmony_ci		return err;
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
7598c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
7608c2ecf20Sopenharmony_ci	data->temp_max[nr] = tmp;
7618c2ecf20Sopenharmony_ci	w83627hf_write_value(data, w83627hf_reg_temp_over[nr], tmp);
7628c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
7638c2ecf20Sopenharmony_ci	return count;
7648c2ecf20Sopenharmony_ci}
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_cistatic ssize_t
7678c2ecf20Sopenharmony_citemp_max_hyst_store(struct device *dev, struct device_attribute *devattr,
7688c2ecf20Sopenharmony_ci		    const char *buf, size_t count)
7698c2ecf20Sopenharmony_ci{
7708c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
7718c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
7728c2ecf20Sopenharmony_ci	u16 tmp;
7738c2ecf20Sopenharmony_ci	long val;
7748c2ecf20Sopenharmony_ci	int err;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	err = kstrtol(buf, 10, &val);
7778c2ecf20Sopenharmony_ci	if (err)
7788c2ecf20Sopenharmony_ci		return err;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
7818c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
7828c2ecf20Sopenharmony_ci	data->temp_max_hyst[nr] = tmp;
7838c2ecf20Sopenharmony_ci	w83627hf_write_value(data, w83627hf_reg_temp_hyst[nr], tmp);
7848c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
7858c2ecf20Sopenharmony_ci	return count;
7868c2ecf20Sopenharmony_ci}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
7898c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
7908c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp_max_hyst, 0);
7918c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
7928c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
7938c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_max_hyst, temp_max_hyst, 1);
7948c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
7958c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
7968c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_max_hyst, temp_max_hyst, 2);
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_cistatic ssize_t
7998c2ecf20Sopenharmony_cicpu0_vid_show(struct device *dev, struct device_attribute *attr, char *buf)
8008c2ecf20Sopenharmony_ci{
8018c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
8028c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
8038c2ecf20Sopenharmony_ci}
8048c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(cpu0_vid);
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_cistatic ssize_t
8078c2ecf20Sopenharmony_civrm_show(struct device *dev, struct device_attribute *attr, char *buf)
8088c2ecf20Sopenharmony_ci{
8098c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
8108c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", (long) data->vrm);
8118c2ecf20Sopenharmony_ci}
8128c2ecf20Sopenharmony_cistatic ssize_t
8138c2ecf20Sopenharmony_civrm_store(struct device *dev, struct device_attribute *attr, const char *buf,
8148c2ecf20Sopenharmony_ci	  size_t count)
8158c2ecf20Sopenharmony_ci{
8168c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
8178c2ecf20Sopenharmony_ci	unsigned long val;
8188c2ecf20Sopenharmony_ci	int err;
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
8218c2ecf20Sopenharmony_ci	if (err)
8228c2ecf20Sopenharmony_ci		return err;
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	if (val > 255)
8258c2ecf20Sopenharmony_ci		return -EINVAL;
8268c2ecf20Sopenharmony_ci	data->vrm = val;
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	return count;
8298c2ecf20Sopenharmony_ci}
8308c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(vrm);
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_cistatic ssize_t
8338c2ecf20Sopenharmony_cialarms_show(struct device *dev, struct device_attribute *attr, char *buf)
8348c2ecf20Sopenharmony_ci{
8358c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
8368c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", (long) data->alarms);
8378c2ecf20Sopenharmony_ci}
8388c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(alarms);
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_cistatic ssize_t
8418c2ecf20Sopenharmony_cialarm_show(struct device *dev, struct device_attribute *attr, char *buf)
8428c2ecf20Sopenharmony_ci{
8438c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
8448c2ecf20Sopenharmony_ci	int bitnr = to_sensor_dev_attr(attr)->index;
8458c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
8468c2ecf20Sopenharmony_ci}
8478c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
8488c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
8498c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
8508c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
8518c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
8528c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
8538c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 10);
8548c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in7_alarm, alarm, 16);
8558c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in8_alarm, alarm, 17);
8568c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
8578c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
8588c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, 11);
8598c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
8608c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 5);
8618c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 13);
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_cistatic ssize_t
8648c2ecf20Sopenharmony_cibeep_mask_show(struct device *dev, struct device_attribute *attr, char *buf)
8658c2ecf20Sopenharmony_ci{
8668c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
8678c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n",
8688c2ecf20Sopenharmony_ci		      (long)BEEP_MASK_FROM_REG(data->beep_mask));
8698c2ecf20Sopenharmony_ci}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_cistatic ssize_t
8728c2ecf20Sopenharmony_cibeep_mask_store(struct device *dev, struct device_attribute *attr,
8738c2ecf20Sopenharmony_ci		const char *buf, size_t count)
8748c2ecf20Sopenharmony_ci{
8758c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
8768c2ecf20Sopenharmony_ci	unsigned long val;
8778c2ecf20Sopenharmony_ci	int err;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
8808c2ecf20Sopenharmony_ci	if (err)
8818c2ecf20Sopenharmony_ci		return err;
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	/* preserve beep enable */
8868c2ecf20Sopenharmony_ci	data->beep_mask = (data->beep_mask & 0x8000)
8878c2ecf20Sopenharmony_ci			| BEEP_MASK_TO_REG(val);
8888c2ecf20Sopenharmony_ci	w83627hf_write_value(data, W83781D_REG_BEEP_INTS1,
8898c2ecf20Sopenharmony_ci			    data->beep_mask & 0xff);
8908c2ecf20Sopenharmony_ci	w83627hf_write_value(data, W83781D_REG_BEEP_INTS3,
8918c2ecf20Sopenharmony_ci			    ((data->beep_mask) >> 16) & 0xff);
8928c2ecf20Sopenharmony_ci	w83627hf_write_value(data, W83781D_REG_BEEP_INTS2,
8938c2ecf20Sopenharmony_ci			    (data->beep_mask >> 8) & 0xff);
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
8968c2ecf20Sopenharmony_ci	return count;
8978c2ecf20Sopenharmony_ci}
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(beep_mask);
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_cistatic ssize_t
9028c2ecf20Sopenharmony_cibeep_show(struct device *dev, struct device_attribute *attr, char *buf)
9038c2ecf20Sopenharmony_ci{
9048c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
9058c2ecf20Sopenharmony_ci	int bitnr = to_sensor_dev_attr(attr)->index;
9068c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
9078c2ecf20Sopenharmony_ci}
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_cistatic ssize_t
9108c2ecf20Sopenharmony_cibeep_store(struct device *dev, struct device_attribute *attr, const char *buf,
9118c2ecf20Sopenharmony_ci	   size_t count)
9128c2ecf20Sopenharmony_ci{
9138c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
9148c2ecf20Sopenharmony_ci	int bitnr = to_sensor_dev_attr(attr)->index;
9158c2ecf20Sopenharmony_ci	u8 reg;
9168c2ecf20Sopenharmony_ci	unsigned long bit;
9178c2ecf20Sopenharmony_ci	int err;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &bit);
9208c2ecf20Sopenharmony_ci	if (err)
9218c2ecf20Sopenharmony_ci		return err;
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	if (bit & ~1)
9248c2ecf20Sopenharmony_ci		return -EINVAL;
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
9278c2ecf20Sopenharmony_ci	if (bit)
9288c2ecf20Sopenharmony_ci		data->beep_mask |= (1 << bitnr);
9298c2ecf20Sopenharmony_ci	else
9308c2ecf20Sopenharmony_ci		data->beep_mask &= ~(1 << bitnr);
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	if (bitnr < 8) {
9338c2ecf20Sopenharmony_ci		reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS1);
9348c2ecf20Sopenharmony_ci		if (bit)
9358c2ecf20Sopenharmony_ci			reg |= (1 << bitnr);
9368c2ecf20Sopenharmony_ci		else
9378c2ecf20Sopenharmony_ci			reg &= ~(1 << bitnr);
9388c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83781D_REG_BEEP_INTS1, reg);
9398c2ecf20Sopenharmony_ci	} else if (bitnr < 16) {
9408c2ecf20Sopenharmony_ci		reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
9418c2ecf20Sopenharmony_ci		if (bit)
9428c2ecf20Sopenharmony_ci			reg |= (1 << (bitnr - 8));
9438c2ecf20Sopenharmony_ci		else
9448c2ecf20Sopenharmony_ci			reg &= ~(1 << (bitnr - 8));
9458c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83781D_REG_BEEP_INTS2, reg);
9468c2ecf20Sopenharmony_ci	} else {
9478c2ecf20Sopenharmony_ci		reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS3);
9488c2ecf20Sopenharmony_ci		if (bit)
9498c2ecf20Sopenharmony_ci			reg |= (1 << (bitnr - 16));
9508c2ecf20Sopenharmony_ci		else
9518c2ecf20Sopenharmony_ci			reg &= ~(1 << (bitnr - 16));
9528c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83781D_REG_BEEP_INTS3, reg);
9538c2ecf20Sopenharmony_ci	}
9548c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci	return count;
9578c2ecf20Sopenharmony_ci}
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in0_beep, beep, 0);
9608c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in1_beep, beep, 1);
9618c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in2_beep, beep, 2);
9628c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in3_beep, beep, 3);
9638c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in4_beep, beep, 8);
9648c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in5_beep, beep, 9);
9658c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in6_beep, beep, 10);
9668c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in7_beep, beep, 16);
9678c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(in8_beep, beep, 17);
9688c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_beep, beep, 6);
9698c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_beep, beep, 7);
9708c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan3_beep, beep, 11);
9718c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_beep, beep, 4);
9728c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_beep, beep, 5);
9738c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_beep, beep, 13);
9748c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(beep_enable, beep, 15);
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_cistatic ssize_t
9778c2ecf20Sopenharmony_cifan_div_show(struct device *dev, struct device_attribute *devattr, char *buf)
9788c2ecf20Sopenharmony_ci{
9798c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
9808c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
9818c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n",
9828c2ecf20Sopenharmony_ci		       (long) DIV_FROM_REG(data->fan_div[nr]));
9838c2ecf20Sopenharmony_ci}
9848c2ecf20Sopenharmony_ci/*
9858c2ecf20Sopenharmony_ci * Note: we save and restore the fan minimum here, because its value is
9868c2ecf20Sopenharmony_ci * determined in part by the fan divisor.  This follows the principle of
9878c2ecf20Sopenharmony_ci * least surprise; the user doesn't expect the fan minimum to change just
9888c2ecf20Sopenharmony_ci * because the divisor changed.
9898c2ecf20Sopenharmony_ci */
9908c2ecf20Sopenharmony_cistatic ssize_t
9918c2ecf20Sopenharmony_cifan_div_store(struct device *dev, struct device_attribute *devattr,
9928c2ecf20Sopenharmony_ci	      const char *buf, size_t count)
9938c2ecf20Sopenharmony_ci{
9948c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
9958c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
9968c2ecf20Sopenharmony_ci	unsigned long min;
9978c2ecf20Sopenharmony_ci	u8 reg;
9988c2ecf20Sopenharmony_ci	unsigned long val;
9998c2ecf20Sopenharmony_ci	int err;
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
10028c2ecf20Sopenharmony_ci	if (err)
10038c2ecf20Sopenharmony_ci		return err;
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	/* Save fan_min */
10088c2ecf20Sopenharmony_ci	min = FAN_FROM_REG(data->fan_min[nr],
10098c2ecf20Sopenharmony_ci			   DIV_FROM_REG(data->fan_div[nr]));
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ci	data->fan_div[nr] = DIV_TO_REG(val);
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci	reg = (w83627hf_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
10148c2ecf20Sopenharmony_ci	       & (nr==0 ? 0xcf : 0x3f))
10158c2ecf20Sopenharmony_ci	    | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
10168c2ecf20Sopenharmony_ci	w83627hf_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	reg = (w83627hf_read_value(data, W83781D_REG_VBAT)
10198c2ecf20Sopenharmony_ci	       & ~(1 << (5 + nr)))
10208c2ecf20Sopenharmony_ci	    | ((data->fan_div[nr] & 0x04) << (3 + nr));
10218c2ecf20Sopenharmony_ci	w83627hf_write_value(data, W83781D_REG_VBAT, reg);
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	/* Restore fan_min */
10248c2ecf20Sopenharmony_ci	data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
10258c2ecf20Sopenharmony_ci	w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr), data->fan_min[nr]);
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
10288c2ecf20Sopenharmony_ci	return count;
10298c2ecf20Sopenharmony_ci}
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
10328c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
10338c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(fan3_div, fan_div, 2);
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_cistatic ssize_t
10368c2ecf20Sopenharmony_cipwm_show(struct device *dev, struct device_attribute *devattr, char *buf)
10378c2ecf20Sopenharmony_ci{
10388c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
10398c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
10408c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", (long) data->pwm[nr]);
10418c2ecf20Sopenharmony_ci}
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_cistatic ssize_t
10448c2ecf20Sopenharmony_cipwm_store(struct device *dev, struct device_attribute *devattr,
10458c2ecf20Sopenharmony_ci	  const char *buf, size_t count)
10468c2ecf20Sopenharmony_ci{
10478c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
10488c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
10498c2ecf20Sopenharmony_ci	unsigned long val;
10508c2ecf20Sopenharmony_ci	int err;
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
10538c2ecf20Sopenharmony_ci	if (err)
10548c2ecf20Sopenharmony_ci		return err;
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci	if (data->type == w83627thf) {
10598c2ecf20Sopenharmony_ci		/* bits 0-3 are reserved  in 627THF */
10608c2ecf20Sopenharmony_ci		data->pwm[nr] = PWM_TO_REG(val) & 0xf0;
10618c2ecf20Sopenharmony_ci		w83627hf_write_value(data,
10628c2ecf20Sopenharmony_ci				     W836X7HF_REG_PWM(data->type, nr),
10638c2ecf20Sopenharmony_ci				     data->pwm[nr] |
10648c2ecf20Sopenharmony_ci				     (w83627hf_read_value(data,
10658c2ecf20Sopenharmony_ci				     W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
10668c2ecf20Sopenharmony_ci	} else {
10678c2ecf20Sopenharmony_ci		data->pwm[nr] = PWM_TO_REG(val);
10688c2ecf20Sopenharmony_ci		w83627hf_write_value(data,
10698c2ecf20Sopenharmony_ci				     W836X7HF_REG_PWM(data->type, nr),
10708c2ecf20Sopenharmony_ci				     data->pwm[nr]);
10718c2ecf20Sopenharmony_ci	}
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
10748c2ecf20Sopenharmony_ci	return count;
10758c2ecf20Sopenharmony_ci}
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
10788c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
10798c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_cistatic ssize_t
10828c2ecf20Sopenharmony_cipwm_enable_show(struct device *dev, struct device_attribute *devattr,
10838c2ecf20Sopenharmony_ci		char *buf)
10848c2ecf20Sopenharmony_ci{
10858c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
10868c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
10878c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->pwm_enable[nr]);
10888c2ecf20Sopenharmony_ci}
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_cistatic ssize_t
10918c2ecf20Sopenharmony_cipwm_enable_store(struct device *dev, struct device_attribute *devattr,
10928c2ecf20Sopenharmony_ci		 const char *buf, size_t count)
10938c2ecf20Sopenharmony_ci{
10948c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
10958c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
10968c2ecf20Sopenharmony_ci	u8 reg;
10978c2ecf20Sopenharmony_ci	unsigned long val;
10988c2ecf20Sopenharmony_ci	int err;
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
11018c2ecf20Sopenharmony_ci	if (err)
11028c2ecf20Sopenharmony_ci		return err;
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	if (!val || val > 3)	/* modes 1, 2 and 3 are supported */
11058c2ecf20Sopenharmony_ci		return -EINVAL;
11068c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
11078c2ecf20Sopenharmony_ci	data->pwm_enable[nr] = val;
11088c2ecf20Sopenharmony_ci	reg = w83627hf_read_value(data, W83627THF_REG_PWM_ENABLE[nr]);
11098c2ecf20Sopenharmony_ci	reg &= ~(0x03 << W83627THF_PWM_ENABLE_SHIFT[nr]);
11108c2ecf20Sopenharmony_ci	reg |= (val - 1) << W83627THF_PWM_ENABLE_SHIFT[nr];
11118c2ecf20Sopenharmony_ci	w83627hf_write_value(data, W83627THF_REG_PWM_ENABLE[nr], reg);
11128c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
11138c2ecf20Sopenharmony_ci	return count;
11148c2ecf20Sopenharmony_ci}
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0);
11178c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1);
11188c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_enable, 2);
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_cistatic ssize_t
11218c2ecf20Sopenharmony_cipwm_freq_show(struct device *dev, struct device_attribute *devattr, char *buf)
11228c2ecf20Sopenharmony_ci{
11238c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
11248c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
11258c2ecf20Sopenharmony_ci	if (data->type == w83627hf)
11268c2ecf20Sopenharmony_ci		return sprintf(buf, "%ld\n",
11278c2ecf20Sopenharmony_ci			pwm_freq_from_reg_627hf(data->pwm_freq[nr]));
11288c2ecf20Sopenharmony_ci	else
11298c2ecf20Sopenharmony_ci		return sprintf(buf, "%ld\n",
11308c2ecf20Sopenharmony_ci			pwm_freq_from_reg(data->pwm_freq[nr]));
11318c2ecf20Sopenharmony_ci}
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_cistatic ssize_t
11348c2ecf20Sopenharmony_cipwm_freq_store(struct device *dev, struct device_attribute *devattr,
11358c2ecf20Sopenharmony_ci	       const char *buf, size_t count)
11368c2ecf20Sopenharmony_ci{
11378c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
11388c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
11398c2ecf20Sopenharmony_ci	static const u8 mask[]={0xF8, 0x8F};
11408c2ecf20Sopenharmony_ci	unsigned long val;
11418c2ecf20Sopenharmony_ci	int err;
11428c2ecf20Sopenharmony_ci
11438c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
11448c2ecf20Sopenharmony_ci	if (err)
11458c2ecf20Sopenharmony_ci		return err;
11468c2ecf20Sopenharmony_ci
11478c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci	if (data->type == w83627hf) {
11508c2ecf20Sopenharmony_ci		data->pwm_freq[nr] = pwm_freq_to_reg_627hf(val);
11518c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83627HF_REG_PWM_FREQ,
11528c2ecf20Sopenharmony_ci				(data->pwm_freq[nr] << (nr*4)) |
11538c2ecf20Sopenharmony_ci				(w83627hf_read_value(data,
11548c2ecf20Sopenharmony_ci				W83627HF_REG_PWM_FREQ) & mask[nr]));
11558c2ecf20Sopenharmony_ci	} else {
11568c2ecf20Sopenharmony_ci		data->pwm_freq[nr] = pwm_freq_to_reg(val);
11578c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83637HF_REG_PWM_FREQ[nr],
11588c2ecf20Sopenharmony_ci				data->pwm_freq[nr]);
11598c2ecf20Sopenharmony_ci	}
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
11628c2ecf20Sopenharmony_ci	return count;
11638c2ecf20Sopenharmony_ci}
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm1_freq, pwm_freq, 0);
11668c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm2_freq, pwm_freq, 1);
11678c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(pwm3_freq, pwm_freq, 2);
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_cistatic ssize_t
11708c2ecf20Sopenharmony_citemp_type_show(struct device *dev, struct device_attribute *devattr,
11718c2ecf20Sopenharmony_ci	       char *buf)
11728c2ecf20Sopenharmony_ci{
11738c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
11748c2ecf20Sopenharmony_ci	struct w83627hf_data *data = w83627hf_update_device(dev);
11758c2ecf20Sopenharmony_ci	return sprintf(buf, "%ld\n", (long) data->sens[nr]);
11768c2ecf20Sopenharmony_ci}
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_cistatic ssize_t
11798c2ecf20Sopenharmony_citemp_type_store(struct device *dev, struct device_attribute *devattr,
11808c2ecf20Sopenharmony_ci		const char *buf, size_t count)
11818c2ecf20Sopenharmony_ci{
11828c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(devattr)->index;
11838c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
11848c2ecf20Sopenharmony_ci	unsigned long val;
11858c2ecf20Sopenharmony_ci	u32 tmp;
11868c2ecf20Sopenharmony_ci	int err;
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
11898c2ecf20Sopenharmony_ci	if (err)
11908c2ecf20Sopenharmony_ci		return err;
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_ci	switch (val) {
11958c2ecf20Sopenharmony_ci	case 1:		/* PII/Celeron diode */
11968c2ecf20Sopenharmony_ci		tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
11978c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83781D_REG_SCFG1,
11988c2ecf20Sopenharmony_ci				    tmp | BIT_SCFG1[nr]);
11998c2ecf20Sopenharmony_ci		tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
12008c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83781D_REG_SCFG2,
12018c2ecf20Sopenharmony_ci				    tmp | BIT_SCFG2[nr]);
12028c2ecf20Sopenharmony_ci		data->sens[nr] = val;
12038c2ecf20Sopenharmony_ci		break;
12048c2ecf20Sopenharmony_ci	case 2:		/* 3904 */
12058c2ecf20Sopenharmony_ci		tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
12068c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83781D_REG_SCFG1,
12078c2ecf20Sopenharmony_ci				    tmp | BIT_SCFG1[nr]);
12088c2ecf20Sopenharmony_ci		tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
12098c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83781D_REG_SCFG2,
12108c2ecf20Sopenharmony_ci				    tmp & ~BIT_SCFG2[nr]);
12118c2ecf20Sopenharmony_ci		data->sens[nr] = val;
12128c2ecf20Sopenharmony_ci		break;
12138c2ecf20Sopenharmony_ci	case W83781D_DEFAULT_BETA:
12148c2ecf20Sopenharmony_ci		dev_warn(dev, "Sensor type %d is deprecated, please use 4 "
12158c2ecf20Sopenharmony_ci			 "instead\n", W83781D_DEFAULT_BETA);
12168c2ecf20Sopenharmony_ci		fallthrough;
12178c2ecf20Sopenharmony_ci	case 4:		/* thermistor */
12188c2ecf20Sopenharmony_ci		tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
12198c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83781D_REG_SCFG1,
12208c2ecf20Sopenharmony_ci				    tmp & ~BIT_SCFG1[nr]);
12218c2ecf20Sopenharmony_ci		data->sens[nr] = val;
12228c2ecf20Sopenharmony_ci		break;
12238c2ecf20Sopenharmony_ci	default:
12248c2ecf20Sopenharmony_ci		dev_err(dev,
12258c2ecf20Sopenharmony_ci		       "Invalid sensor type %ld; must be 1, 2, or 4\n",
12268c2ecf20Sopenharmony_ci		       (long) val);
12278c2ecf20Sopenharmony_ci		break;
12288c2ecf20Sopenharmony_ci	}
12298c2ecf20Sopenharmony_ci
12308c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
12318c2ecf20Sopenharmony_ci	return count;
12328c2ecf20Sopenharmony_ci}
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp1_type, temp_type, 0);
12358c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp2_type, temp_type, 1);
12368c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RW(temp3_type, temp_type, 2);
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_cistatic ssize_t
12398c2ecf20Sopenharmony_ciname_show(struct device *dev, struct device_attribute *devattr, char *buf)
12408c2ecf20Sopenharmony_ci{
12418c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci	return sprintf(buf, "%s\n", data->name);
12448c2ecf20Sopenharmony_ci}
12458c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(name);
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_cistatic int __init w83627hf_find(int sioaddr, unsigned short *addr,
12488c2ecf20Sopenharmony_ci				struct w83627hf_sio_data *sio_data)
12498c2ecf20Sopenharmony_ci{
12508c2ecf20Sopenharmony_ci	int err;
12518c2ecf20Sopenharmony_ci	u16 val;
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	static __initconst char *const names[] = {
12548c2ecf20Sopenharmony_ci		"W83627HF",
12558c2ecf20Sopenharmony_ci		"W83627THF",
12568c2ecf20Sopenharmony_ci		"W83697HF",
12578c2ecf20Sopenharmony_ci		"W83637HF",
12588c2ecf20Sopenharmony_ci		"W83687THF",
12598c2ecf20Sopenharmony_ci	};
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_ci	sio_data->sioaddr = sioaddr;
12628c2ecf20Sopenharmony_ci	err = superio_enter(sio_data);
12638c2ecf20Sopenharmony_ci	if (err)
12648c2ecf20Sopenharmony_ci		return err;
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci	err = -ENODEV;
12678c2ecf20Sopenharmony_ci	val = force_id ? force_id : superio_inb(sio_data, DEVID);
12688c2ecf20Sopenharmony_ci	switch (val) {
12698c2ecf20Sopenharmony_ci	case W627_DEVID:
12708c2ecf20Sopenharmony_ci		sio_data->type = w83627hf;
12718c2ecf20Sopenharmony_ci		break;
12728c2ecf20Sopenharmony_ci	case W627THF_DEVID:
12738c2ecf20Sopenharmony_ci		sio_data->type = w83627thf;
12748c2ecf20Sopenharmony_ci		break;
12758c2ecf20Sopenharmony_ci	case W697_DEVID:
12768c2ecf20Sopenharmony_ci		sio_data->type = w83697hf;
12778c2ecf20Sopenharmony_ci		break;
12788c2ecf20Sopenharmony_ci	case W637_DEVID:
12798c2ecf20Sopenharmony_ci		sio_data->type = w83637hf;
12808c2ecf20Sopenharmony_ci		break;
12818c2ecf20Sopenharmony_ci	case W687THF_DEVID:
12828c2ecf20Sopenharmony_ci		sio_data->type = w83687thf;
12838c2ecf20Sopenharmony_ci		break;
12848c2ecf20Sopenharmony_ci	case 0xff:	/* No device at all */
12858c2ecf20Sopenharmony_ci		goto exit;
12868c2ecf20Sopenharmony_ci	default:
12878c2ecf20Sopenharmony_ci		pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%02x)\n", val);
12888c2ecf20Sopenharmony_ci		goto exit;
12898c2ecf20Sopenharmony_ci	}
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci	superio_select(sio_data, W83627HF_LD_HWM);
12928c2ecf20Sopenharmony_ci	val = (superio_inb(sio_data, WINB_BASE_REG) << 8) |
12938c2ecf20Sopenharmony_ci	       superio_inb(sio_data, WINB_BASE_REG + 1);
12948c2ecf20Sopenharmony_ci	*addr = val & WINB_ALIGNMENT;
12958c2ecf20Sopenharmony_ci	if (*addr == 0) {
12968c2ecf20Sopenharmony_ci		pr_warn("Base address not set, skipping\n");
12978c2ecf20Sopenharmony_ci		goto exit;
12988c2ecf20Sopenharmony_ci	}
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci	val = superio_inb(sio_data, WINB_ACT_REG);
13018c2ecf20Sopenharmony_ci	if (!(val & 0x01)) {
13028c2ecf20Sopenharmony_ci		pr_warn("Enabling HWM logical device\n");
13038c2ecf20Sopenharmony_ci		superio_outb(sio_data, WINB_ACT_REG, val | 0x01);
13048c2ecf20Sopenharmony_ci	}
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ci	err = 0;
13078c2ecf20Sopenharmony_ci	pr_info(DRVNAME ": Found %s chip at %#x\n",
13088c2ecf20Sopenharmony_ci		names[sio_data->type], *addr);
13098c2ecf20Sopenharmony_ci
13108c2ecf20Sopenharmony_ci exit:
13118c2ecf20Sopenharmony_ci	superio_exit(sio_data);
13128c2ecf20Sopenharmony_ci	return err;
13138c2ecf20Sopenharmony_ci}
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_ci#define VIN_UNIT_ATTRS(_X_)	\
13168c2ecf20Sopenharmony_ci	&sensor_dev_attr_in##_X_##_input.dev_attr.attr,		\
13178c2ecf20Sopenharmony_ci	&sensor_dev_attr_in##_X_##_min.dev_attr.attr,		\
13188c2ecf20Sopenharmony_ci	&sensor_dev_attr_in##_X_##_max.dev_attr.attr,		\
13198c2ecf20Sopenharmony_ci	&sensor_dev_attr_in##_X_##_alarm.dev_attr.attr,		\
13208c2ecf20Sopenharmony_ci	&sensor_dev_attr_in##_X_##_beep.dev_attr.attr
13218c2ecf20Sopenharmony_ci
13228c2ecf20Sopenharmony_ci#define FAN_UNIT_ATTRS(_X_)	\
13238c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan##_X_##_input.dev_attr.attr,	\
13248c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan##_X_##_min.dev_attr.attr,		\
13258c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan##_X_##_div.dev_attr.attr,		\
13268c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan##_X_##_alarm.dev_attr.attr,	\
13278c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan##_X_##_beep.dev_attr.attr
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_ci#define TEMP_UNIT_ATTRS(_X_)	\
13308c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp##_X_##_input.dev_attr.attr,	\
13318c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp##_X_##_max.dev_attr.attr,		\
13328c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp##_X_##_max_hyst.dev_attr.attr,	\
13338c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp##_X_##_type.dev_attr.attr,	\
13348c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp##_X_##_alarm.dev_attr.attr,	\
13358c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp##_X_##_beep.dev_attr.attr
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_cistatic struct attribute *w83627hf_attributes[] = {
13388c2ecf20Sopenharmony_ci	&dev_attr_in0_input.attr,
13398c2ecf20Sopenharmony_ci	&dev_attr_in0_min.attr,
13408c2ecf20Sopenharmony_ci	&dev_attr_in0_max.attr,
13418c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_alarm.dev_attr.attr,
13428c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_beep.dev_attr.attr,
13438c2ecf20Sopenharmony_ci	VIN_UNIT_ATTRS(2),
13448c2ecf20Sopenharmony_ci	VIN_UNIT_ATTRS(3),
13458c2ecf20Sopenharmony_ci	VIN_UNIT_ATTRS(4),
13468c2ecf20Sopenharmony_ci	VIN_UNIT_ATTRS(7),
13478c2ecf20Sopenharmony_ci	VIN_UNIT_ATTRS(8),
13488c2ecf20Sopenharmony_ci
13498c2ecf20Sopenharmony_ci	FAN_UNIT_ATTRS(1),
13508c2ecf20Sopenharmony_ci	FAN_UNIT_ATTRS(2),
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ci	TEMP_UNIT_ATTRS(1),
13538c2ecf20Sopenharmony_ci	TEMP_UNIT_ATTRS(2),
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_ci	&dev_attr_alarms.attr,
13568c2ecf20Sopenharmony_ci	&sensor_dev_attr_beep_enable.dev_attr.attr,
13578c2ecf20Sopenharmony_ci	&dev_attr_beep_mask.attr,
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1.dev_attr.attr,
13608c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2.dev_attr.attr,
13618c2ecf20Sopenharmony_ci	&dev_attr_name.attr,
13628c2ecf20Sopenharmony_ci	NULL
13638c2ecf20Sopenharmony_ci};
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_cistatic const struct attribute_group w83627hf_group = {
13668c2ecf20Sopenharmony_ci	.attrs = w83627hf_attributes,
13678c2ecf20Sopenharmony_ci};
13688c2ecf20Sopenharmony_ci
13698c2ecf20Sopenharmony_cistatic struct attribute *w83627hf_attributes_opt[] = {
13708c2ecf20Sopenharmony_ci	VIN_UNIT_ATTRS(1),
13718c2ecf20Sopenharmony_ci	VIN_UNIT_ATTRS(5),
13728c2ecf20Sopenharmony_ci	VIN_UNIT_ATTRS(6),
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	FAN_UNIT_ATTRS(3),
13758c2ecf20Sopenharmony_ci	TEMP_UNIT_ATTRS(3),
13768c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3.dev_attr.attr,
13778c2ecf20Sopenharmony_ci
13788c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_freq.dev_attr.attr,
13798c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2_freq.dev_attr.attr,
13808c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3_freq.dev_attr.attr,
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
13838c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
13848c2ecf20Sopenharmony_ci	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
13858c2ecf20Sopenharmony_ci
13868c2ecf20Sopenharmony_ci	NULL
13878c2ecf20Sopenharmony_ci};
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_cistatic const struct attribute_group w83627hf_group_opt = {
13908c2ecf20Sopenharmony_ci	.attrs = w83627hf_attributes_opt,
13918c2ecf20Sopenharmony_ci};
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_cistatic int w83627hf_probe(struct platform_device *pdev)
13948c2ecf20Sopenharmony_ci{
13958c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
13968c2ecf20Sopenharmony_ci	struct w83627hf_sio_data *sio_data = dev_get_platdata(dev);
13978c2ecf20Sopenharmony_ci	struct w83627hf_data *data;
13988c2ecf20Sopenharmony_ci	struct resource *res;
13998c2ecf20Sopenharmony_ci	int err, i;
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci	static const char *names[] = {
14028c2ecf20Sopenharmony_ci		"w83627hf",
14038c2ecf20Sopenharmony_ci		"w83627thf",
14048c2ecf20Sopenharmony_ci		"w83697hf",
14058c2ecf20Sopenharmony_ci		"w83637hf",
14068c2ecf20Sopenharmony_ci		"w83687thf",
14078c2ecf20Sopenharmony_ci	};
14088c2ecf20Sopenharmony_ci
14098c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
14108c2ecf20Sopenharmony_ci	if (!devm_request_region(dev, res->start, WINB_REGION_SIZE, DRVNAME)) {
14118c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
14128c2ecf20Sopenharmony_ci			(unsigned long)res->start,
14138c2ecf20Sopenharmony_ci			(unsigned long)(res->start + WINB_REGION_SIZE - 1));
14148c2ecf20Sopenharmony_ci		return -EBUSY;
14158c2ecf20Sopenharmony_ci	}
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_ci	data = devm_kzalloc(dev, sizeof(struct w83627hf_data), GFP_KERNEL);
14188c2ecf20Sopenharmony_ci	if (!data)
14198c2ecf20Sopenharmony_ci		return -ENOMEM;
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_ci	data->addr = res->start;
14228c2ecf20Sopenharmony_ci	data->type = sio_data->type;
14238c2ecf20Sopenharmony_ci	data->name = names[sio_data->type];
14248c2ecf20Sopenharmony_ci	mutex_init(&data->lock);
14258c2ecf20Sopenharmony_ci	mutex_init(&data->update_lock);
14268c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, data);
14278c2ecf20Sopenharmony_ci
14288c2ecf20Sopenharmony_ci	/* Initialize the chip */
14298c2ecf20Sopenharmony_ci	w83627hf_init_device(pdev);
14308c2ecf20Sopenharmony_ci
14318c2ecf20Sopenharmony_ci	/* A few vars need to be filled upon startup */
14328c2ecf20Sopenharmony_ci	for (i = 0; i <= 2; i++)
14338c2ecf20Sopenharmony_ci		data->fan_min[i] = w83627hf_read_value(
14348c2ecf20Sopenharmony_ci					data, W83627HF_REG_FAN_MIN(i));
14358c2ecf20Sopenharmony_ci	w83627hf_update_fan_div(data);
14368c2ecf20Sopenharmony_ci
14378c2ecf20Sopenharmony_ci	/* Register common device attributes */
14388c2ecf20Sopenharmony_ci	err = sysfs_create_group(&dev->kobj, &w83627hf_group);
14398c2ecf20Sopenharmony_ci	if (err)
14408c2ecf20Sopenharmony_ci		return err;
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci	/* Register chip-specific device attributes */
14438c2ecf20Sopenharmony_ci	if (data->type == w83627hf || data->type == w83697hf)
14448c2ecf20Sopenharmony_ci		if ((err = device_create_file(dev,
14458c2ecf20Sopenharmony_ci				&sensor_dev_attr_in5_input.dev_attr))
14468c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14478c2ecf20Sopenharmony_ci				&sensor_dev_attr_in5_min.dev_attr))
14488c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14498c2ecf20Sopenharmony_ci				&sensor_dev_attr_in5_max.dev_attr))
14508c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14518c2ecf20Sopenharmony_ci				&sensor_dev_attr_in5_alarm.dev_attr))
14528c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14538c2ecf20Sopenharmony_ci				&sensor_dev_attr_in5_beep.dev_attr))
14548c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14558c2ecf20Sopenharmony_ci				&sensor_dev_attr_in6_input.dev_attr))
14568c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14578c2ecf20Sopenharmony_ci				&sensor_dev_attr_in6_min.dev_attr))
14588c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14598c2ecf20Sopenharmony_ci				&sensor_dev_attr_in6_max.dev_attr))
14608c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14618c2ecf20Sopenharmony_ci				&sensor_dev_attr_in6_alarm.dev_attr))
14628c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14638c2ecf20Sopenharmony_ci				&sensor_dev_attr_in6_beep.dev_attr))
14648c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14658c2ecf20Sopenharmony_ci				&sensor_dev_attr_pwm1_freq.dev_attr))
14668c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14678c2ecf20Sopenharmony_ci				&sensor_dev_attr_pwm2_freq.dev_attr)))
14688c2ecf20Sopenharmony_ci			goto error;
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci	if (data->type != w83697hf)
14718c2ecf20Sopenharmony_ci		if ((err = device_create_file(dev,
14728c2ecf20Sopenharmony_ci				&sensor_dev_attr_in1_input.dev_attr))
14738c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14748c2ecf20Sopenharmony_ci				&sensor_dev_attr_in1_min.dev_attr))
14758c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14768c2ecf20Sopenharmony_ci				&sensor_dev_attr_in1_max.dev_attr))
14778c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14788c2ecf20Sopenharmony_ci				&sensor_dev_attr_in1_alarm.dev_attr))
14798c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14808c2ecf20Sopenharmony_ci				&sensor_dev_attr_in1_beep.dev_attr))
14818c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14828c2ecf20Sopenharmony_ci				&sensor_dev_attr_fan3_input.dev_attr))
14838c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14848c2ecf20Sopenharmony_ci				&sensor_dev_attr_fan3_min.dev_attr))
14858c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14868c2ecf20Sopenharmony_ci				&sensor_dev_attr_fan3_div.dev_attr))
14878c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14888c2ecf20Sopenharmony_ci				&sensor_dev_attr_fan3_alarm.dev_attr))
14898c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14908c2ecf20Sopenharmony_ci				&sensor_dev_attr_fan3_beep.dev_attr))
14918c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14928c2ecf20Sopenharmony_ci				&sensor_dev_attr_temp3_input.dev_attr))
14938c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14948c2ecf20Sopenharmony_ci				&sensor_dev_attr_temp3_max.dev_attr))
14958c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14968c2ecf20Sopenharmony_ci				&sensor_dev_attr_temp3_max_hyst.dev_attr))
14978c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
14988c2ecf20Sopenharmony_ci				&sensor_dev_attr_temp3_alarm.dev_attr))
14998c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
15008c2ecf20Sopenharmony_ci				&sensor_dev_attr_temp3_beep.dev_attr))
15018c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
15028c2ecf20Sopenharmony_ci				&sensor_dev_attr_temp3_type.dev_attr)))
15038c2ecf20Sopenharmony_ci			goto error;
15048c2ecf20Sopenharmony_ci
15058c2ecf20Sopenharmony_ci	if (data->type != w83697hf && data->vid != 0xff) {
15068c2ecf20Sopenharmony_ci		/* Convert VID to voltage based on VRM */
15078c2ecf20Sopenharmony_ci		data->vrm = vid_which_vrm();
15088c2ecf20Sopenharmony_ci
15098c2ecf20Sopenharmony_ci		if ((err = device_create_file(dev, &dev_attr_cpu0_vid))
15108c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev, &dev_attr_vrm)))
15118c2ecf20Sopenharmony_ci			goto error;
15128c2ecf20Sopenharmony_ci	}
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_ci	if (data->type == w83627thf || data->type == w83637hf
15158c2ecf20Sopenharmony_ci	    || data->type == w83687thf) {
15168c2ecf20Sopenharmony_ci		err = device_create_file(dev, &sensor_dev_attr_pwm3.dev_attr);
15178c2ecf20Sopenharmony_ci		if (err)
15188c2ecf20Sopenharmony_ci			goto error;
15198c2ecf20Sopenharmony_ci	}
15208c2ecf20Sopenharmony_ci
15218c2ecf20Sopenharmony_ci	if (data->type == w83637hf || data->type == w83687thf)
15228c2ecf20Sopenharmony_ci		if ((err = device_create_file(dev,
15238c2ecf20Sopenharmony_ci				&sensor_dev_attr_pwm1_freq.dev_attr))
15248c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
15258c2ecf20Sopenharmony_ci				&sensor_dev_attr_pwm2_freq.dev_attr))
15268c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
15278c2ecf20Sopenharmony_ci				&sensor_dev_attr_pwm3_freq.dev_attr)))
15288c2ecf20Sopenharmony_ci			goto error;
15298c2ecf20Sopenharmony_ci
15308c2ecf20Sopenharmony_ci	if (data->type != w83627hf)
15318c2ecf20Sopenharmony_ci		if ((err = device_create_file(dev,
15328c2ecf20Sopenharmony_ci				&sensor_dev_attr_pwm1_enable.dev_attr))
15338c2ecf20Sopenharmony_ci		 || (err = device_create_file(dev,
15348c2ecf20Sopenharmony_ci				&sensor_dev_attr_pwm2_enable.dev_attr)))
15358c2ecf20Sopenharmony_ci			goto error;
15368c2ecf20Sopenharmony_ci
15378c2ecf20Sopenharmony_ci	if (data->type == w83627thf || data->type == w83637hf
15388c2ecf20Sopenharmony_ci	    || data->type == w83687thf) {
15398c2ecf20Sopenharmony_ci		err = device_create_file(dev,
15408c2ecf20Sopenharmony_ci					 &sensor_dev_attr_pwm3_enable.dev_attr);
15418c2ecf20Sopenharmony_ci		if (err)
15428c2ecf20Sopenharmony_ci			goto error;
15438c2ecf20Sopenharmony_ci	}
15448c2ecf20Sopenharmony_ci
15458c2ecf20Sopenharmony_ci	data->hwmon_dev = hwmon_device_register(dev);
15468c2ecf20Sopenharmony_ci	if (IS_ERR(data->hwmon_dev)) {
15478c2ecf20Sopenharmony_ci		err = PTR_ERR(data->hwmon_dev);
15488c2ecf20Sopenharmony_ci		goto error;
15498c2ecf20Sopenharmony_ci	}
15508c2ecf20Sopenharmony_ci
15518c2ecf20Sopenharmony_ci	return 0;
15528c2ecf20Sopenharmony_ci
15538c2ecf20Sopenharmony_ci error:
15548c2ecf20Sopenharmony_ci	sysfs_remove_group(&dev->kobj, &w83627hf_group);
15558c2ecf20Sopenharmony_ci	sysfs_remove_group(&dev->kobj, &w83627hf_group_opt);
15568c2ecf20Sopenharmony_ci	return err;
15578c2ecf20Sopenharmony_ci}
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_cistatic int w83627hf_remove(struct platform_device *pdev)
15608c2ecf20Sopenharmony_ci{
15618c2ecf20Sopenharmony_ci	struct w83627hf_data *data = platform_get_drvdata(pdev);
15628c2ecf20Sopenharmony_ci
15638c2ecf20Sopenharmony_ci	hwmon_device_unregister(data->hwmon_dev);
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_ci	sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group);
15668c2ecf20Sopenharmony_ci	sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group_opt);
15678c2ecf20Sopenharmony_ci
15688c2ecf20Sopenharmony_ci	return 0;
15698c2ecf20Sopenharmony_ci}
15708c2ecf20Sopenharmony_ci
15718c2ecf20Sopenharmony_ci/* Registers 0x50-0x5f are banked */
15728c2ecf20Sopenharmony_cistatic inline void w83627hf_set_bank(struct w83627hf_data *data, u16 reg)
15738c2ecf20Sopenharmony_ci{
15748c2ecf20Sopenharmony_ci	if ((reg & 0x00f0) == 0x50) {
15758c2ecf20Sopenharmony_ci		outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
15768c2ecf20Sopenharmony_ci		outb_p(reg >> 8, data->addr + W83781D_DATA_REG_OFFSET);
15778c2ecf20Sopenharmony_ci	}
15788c2ecf20Sopenharmony_ci}
15798c2ecf20Sopenharmony_ci
15808c2ecf20Sopenharmony_ci/* Not strictly necessary, but play it safe for now */
15818c2ecf20Sopenharmony_cistatic inline void w83627hf_reset_bank(struct w83627hf_data *data, u16 reg)
15828c2ecf20Sopenharmony_ci{
15838c2ecf20Sopenharmony_ci	if (reg & 0xff00) {
15848c2ecf20Sopenharmony_ci		outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
15858c2ecf20Sopenharmony_ci		outb_p(0, data->addr + W83781D_DATA_REG_OFFSET);
15868c2ecf20Sopenharmony_ci	}
15878c2ecf20Sopenharmony_ci}
15888c2ecf20Sopenharmony_ci
15898c2ecf20Sopenharmony_cistatic int w83627hf_read_value(struct w83627hf_data *data, u16 reg)
15908c2ecf20Sopenharmony_ci{
15918c2ecf20Sopenharmony_ci	int res, word_sized;
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_ci	mutex_lock(&data->lock);
15948c2ecf20Sopenharmony_ci	word_sized = (((reg & 0xff00) == 0x100)
15958c2ecf20Sopenharmony_ci		   || ((reg & 0xff00) == 0x200))
15968c2ecf20Sopenharmony_ci		  && (((reg & 0x00ff) == 0x50)
15978c2ecf20Sopenharmony_ci		   || ((reg & 0x00ff) == 0x53)
15988c2ecf20Sopenharmony_ci		   || ((reg & 0x00ff) == 0x55));
15998c2ecf20Sopenharmony_ci	w83627hf_set_bank(data, reg);
16008c2ecf20Sopenharmony_ci	outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
16018c2ecf20Sopenharmony_ci	res = inb_p(data->addr + W83781D_DATA_REG_OFFSET);
16028c2ecf20Sopenharmony_ci	if (word_sized) {
16038c2ecf20Sopenharmony_ci		outb_p((reg & 0xff) + 1,
16048c2ecf20Sopenharmony_ci		       data->addr + W83781D_ADDR_REG_OFFSET);
16058c2ecf20Sopenharmony_ci		res =
16068c2ecf20Sopenharmony_ci		    (res << 8) + inb_p(data->addr +
16078c2ecf20Sopenharmony_ci				       W83781D_DATA_REG_OFFSET);
16088c2ecf20Sopenharmony_ci	}
16098c2ecf20Sopenharmony_ci	w83627hf_reset_bank(data, reg);
16108c2ecf20Sopenharmony_ci	mutex_unlock(&data->lock);
16118c2ecf20Sopenharmony_ci	return res;
16128c2ecf20Sopenharmony_ci}
16138c2ecf20Sopenharmony_ci
16148c2ecf20Sopenharmony_cistatic int w83627thf_read_gpio5(struct platform_device *pdev)
16158c2ecf20Sopenharmony_ci{
16168c2ecf20Sopenharmony_ci	struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
16178c2ecf20Sopenharmony_ci	int res = 0xff, sel;
16188c2ecf20Sopenharmony_ci
16198c2ecf20Sopenharmony_ci	if (superio_enter(sio_data)) {
16208c2ecf20Sopenharmony_ci		/*
16218c2ecf20Sopenharmony_ci		 * Some other driver reserved the address space for itself.
16228c2ecf20Sopenharmony_ci		 * We don't want to fail driver instantiation because of that,
16238c2ecf20Sopenharmony_ci		 * so display a warning and keep going.
16248c2ecf20Sopenharmony_ci		 */
16258c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev,
16268c2ecf20Sopenharmony_ci			 "Can not read VID data: Failed to enable SuperIO access\n");
16278c2ecf20Sopenharmony_ci		return res;
16288c2ecf20Sopenharmony_ci	}
16298c2ecf20Sopenharmony_ci
16308c2ecf20Sopenharmony_ci	superio_select(sio_data, W83627HF_LD_GPIO5);
16318c2ecf20Sopenharmony_ci
16328c2ecf20Sopenharmony_ci	res = 0xff;
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_ci	/* Make sure these GPIO pins are enabled */
16358c2ecf20Sopenharmony_ci	if (!(superio_inb(sio_data, W83627THF_GPIO5_EN) & (1<<3))) {
16368c2ecf20Sopenharmony_ci		dev_dbg(&pdev->dev, "GPIO5 disabled, no VID function\n");
16378c2ecf20Sopenharmony_ci		goto exit;
16388c2ecf20Sopenharmony_ci	}
16398c2ecf20Sopenharmony_ci
16408c2ecf20Sopenharmony_ci	/*
16418c2ecf20Sopenharmony_ci	 * Make sure the pins are configured for input
16428c2ecf20Sopenharmony_ci	 * There must be at least five (VRM 9), and possibly 6 (VRM 10)
16438c2ecf20Sopenharmony_ci	 */
16448c2ecf20Sopenharmony_ci	sel = superio_inb(sio_data, W83627THF_GPIO5_IOSR) & 0x3f;
16458c2ecf20Sopenharmony_ci	if ((sel & 0x1f) != 0x1f) {
16468c2ecf20Sopenharmony_ci		dev_dbg(&pdev->dev, "GPIO5 not configured for VID "
16478c2ecf20Sopenharmony_ci			"function\n");
16488c2ecf20Sopenharmony_ci		goto exit;
16498c2ecf20Sopenharmony_ci	}
16508c2ecf20Sopenharmony_ci
16518c2ecf20Sopenharmony_ci	dev_info(&pdev->dev, "Reading VID from GPIO5\n");
16528c2ecf20Sopenharmony_ci	res = superio_inb(sio_data, W83627THF_GPIO5_DR) & sel;
16538c2ecf20Sopenharmony_ci
16548c2ecf20Sopenharmony_ciexit:
16558c2ecf20Sopenharmony_ci	superio_exit(sio_data);
16568c2ecf20Sopenharmony_ci	return res;
16578c2ecf20Sopenharmony_ci}
16588c2ecf20Sopenharmony_ci
16598c2ecf20Sopenharmony_cistatic int w83687thf_read_vid(struct platform_device *pdev)
16608c2ecf20Sopenharmony_ci{
16618c2ecf20Sopenharmony_ci	struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
16628c2ecf20Sopenharmony_ci	int res = 0xff;
16638c2ecf20Sopenharmony_ci
16648c2ecf20Sopenharmony_ci	if (superio_enter(sio_data)) {
16658c2ecf20Sopenharmony_ci		/*
16668c2ecf20Sopenharmony_ci		 * Some other driver reserved the address space for itself.
16678c2ecf20Sopenharmony_ci		 * We don't want to fail driver instantiation because of that,
16688c2ecf20Sopenharmony_ci		 * so display a warning and keep going.
16698c2ecf20Sopenharmony_ci		 */
16708c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev,
16718c2ecf20Sopenharmony_ci			 "Can not read VID data: Failed to enable SuperIO access\n");
16728c2ecf20Sopenharmony_ci		return res;
16738c2ecf20Sopenharmony_ci	}
16748c2ecf20Sopenharmony_ci
16758c2ecf20Sopenharmony_ci	superio_select(sio_data, W83627HF_LD_HWM);
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_ci	/* Make sure these GPIO pins are enabled */
16788c2ecf20Sopenharmony_ci	if (!(superio_inb(sio_data, W83687THF_VID_EN) & (1 << 2))) {
16798c2ecf20Sopenharmony_ci		dev_dbg(&pdev->dev, "VID disabled, no VID function\n");
16808c2ecf20Sopenharmony_ci		goto exit;
16818c2ecf20Sopenharmony_ci	}
16828c2ecf20Sopenharmony_ci
16838c2ecf20Sopenharmony_ci	/* Make sure the pins are configured for input */
16848c2ecf20Sopenharmony_ci	if (!(superio_inb(sio_data, W83687THF_VID_CFG) & (1 << 4))) {
16858c2ecf20Sopenharmony_ci		dev_dbg(&pdev->dev, "VID configured as output, "
16868c2ecf20Sopenharmony_ci			"no VID function\n");
16878c2ecf20Sopenharmony_ci		goto exit;
16888c2ecf20Sopenharmony_ci	}
16898c2ecf20Sopenharmony_ci
16908c2ecf20Sopenharmony_ci	res = superio_inb(sio_data, W83687THF_VID_DATA) & 0x3f;
16918c2ecf20Sopenharmony_ci
16928c2ecf20Sopenharmony_ciexit:
16938c2ecf20Sopenharmony_ci	superio_exit(sio_data);
16948c2ecf20Sopenharmony_ci	return res;
16958c2ecf20Sopenharmony_ci}
16968c2ecf20Sopenharmony_ci
16978c2ecf20Sopenharmony_cistatic int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value)
16988c2ecf20Sopenharmony_ci{
16998c2ecf20Sopenharmony_ci	int word_sized;
17008c2ecf20Sopenharmony_ci
17018c2ecf20Sopenharmony_ci	mutex_lock(&data->lock);
17028c2ecf20Sopenharmony_ci	word_sized = (((reg & 0xff00) == 0x100)
17038c2ecf20Sopenharmony_ci		   || ((reg & 0xff00) == 0x200))
17048c2ecf20Sopenharmony_ci		  && (((reg & 0x00ff) == 0x53)
17058c2ecf20Sopenharmony_ci		   || ((reg & 0x00ff) == 0x55));
17068c2ecf20Sopenharmony_ci	w83627hf_set_bank(data, reg);
17078c2ecf20Sopenharmony_ci	outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
17088c2ecf20Sopenharmony_ci	if (word_sized) {
17098c2ecf20Sopenharmony_ci		outb_p(value >> 8,
17108c2ecf20Sopenharmony_ci		       data->addr + W83781D_DATA_REG_OFFSET);
17118c2ecf20Sopenharmony_ci		outb_p((reg & 0xff) + 1,
17128c2ecf20Sopenharmony_ci		       data->addr + W83781D_ADDR_REG_OFFSET);
17138c2ecf20Sopenharmony_ci	}
17148c2ecf20Sopenharmony_ci	outb_p(value & 0xff,
17158c2ecf20Sopenharmony_ci	       data->addr + W83781D_DATA_REG_OFFSET);
17168c2ecf20Sopenharmony_ci	w83627hf_reset_bank(data, reg);
17178c2ecf20Sopenharmony_ci	mutex_unlock(&data->lock);
17188c2ecf20Sopenharmony_ci	return 0;
17198c2ecf20Sopenharmony_ci}
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_cistatic void w83627hf_init_device(struct platform_device *pdev)
17228c2ecf20Sopenharmony_ci{
17238c2ecf20Sopenharmony_ci	struct w83627hf_data *data = platform_get_drvdata(pdev);
17248c2ecf20Sopenharmony_ci	int i;
17258c2ecf20Sopenharmony_ci	enum chips type = data->type;
17268c2ecf20Sopenharmony_ci	u8 tmp;
17278c2ecf20Sopenharmony_ci
17288c2ecf20Sopenharmony_ci	/* Minimize conflicts with other winbond i2c-only clients...  */
17298c2ecf20Sopenharmony_ci	/* disable i2c subclients... how to disable main i2c client?? */
17308c2ecf20Sopenharmony_ci	/* force i2c address to relatively uncommon address */
17318c2ecf20Sopenharmony_ci	if (type == w83627hf) {
17328c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83781D_REG_I2C_SUBADDR, 0x89);
17338c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83781D_REG_I2C_ADDR, force_i2c);
17348c2ecf20Sopenharmony_ci	}
17358c2ecf20Sopenharmony_ci
17368c2ecf20Sopenharmony_ci	/* Read VID only once */
17378c2ecf20Sopenharmony_ci	if (type == w83627hf || type == w83637hf) {
17388c2ecf20Sopenharmony_ci		int lo = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
17398c2ecf20Sopenharmony_ci		int hi = w83627hf_read_value(data, W83781D_REG_CHIPID);
17408c2ecf20Sopenharmony_ci		data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
17418c2ecf20Sopenharmony_ci	} else if (type == w83627thf) {
17428c2ecf20Sopenharmony_ci		data->vid = w83627thf_read_gpio5(pdev);
17438c2ecf20Sopenharmony_ci	} else if (type == w83687thf) {
17448c2ecf20Sopenharmony_ci		data->vid = w83687thf_read_vid(pdev);
17458c2ecf20Sopenharmony_ci	}
17468c2ecf20Sopenharmony_ci
17478c2ecf20Sopenharmony_ci	/* Read VRM & OVT Config only once */
17488c2ecf20Sopenharmony_ci	if (type == w83627thf || type == w83637hf || type == w83687thf) {
17498c2ecf20Sopenharmony_ci		data->vrm_ovt =
17508c2ecf20Sopenharmony_ci			w83627hf_read_value(data, W83627THF_REG_VRM_OVT_CFG);
17518c2ecf20Sopenharmony_ci	}
17528c2ecf20Sopenharmony_ci
17538c2ecf20Sopenharmony_ci	tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
17548c2ecf20Sopenharmony_ci	for (i = 1; i <= 3; i++) {
17558c2ecf20Sopenharmony_ci		if (!(tmp & BIT_SCFG1[i - 1])) {
17568c2ecf20Sopenharmony_ci			data->sens[i - 1] = 4;
17578c2ecf20Sopenharmony_ci		} else {
17588c2ecf20Sopenharmony_ci			if (w83627hf_read_value
17598c2ecf20Sopenharmony_ci			    (data,
17608c2ecf20Sopenharmony_ci			     W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
17618c2ecf20Sopenharmony_ci				data->sens[i - 1] = 1;
17628c2ecf20Sopenharmony_ci			else
17638c2ecf20Sopenharmony_ci				data->sens[i - 1] = 2;
17648c2ecf20Sopenharmony_ci		}
17658c2ecf20Sopenharmony_ci		if ((type == w83697hf) && (i == 2))
17668c2ecf20Sopenharmony_ci			break;
17678c2ecf20Sopenharmony_ci	}
17688c2ecf20Sopenharmony_ci
17698c2ecf20Sopenharmony_ci	if(init) {
17708c2ecf20Sopenharmony_ci		/* Enable temp2 */
17718c2ecf20Sopenharmony_ci		tmp = w83627hf_read_value(data, W83627HF_REG_TEMP2_CONFIG);
17728c2ecf20Sopenharmony_ci		if (tmp & 0x01) {
17738c2ecf20Sopenharmony_ci			dev_warn(&pdev->dev, "Enabling temp2, readings "
17748c2ecf20Sopenharmony_ci				 "might not make sense\n");
17758c2ecf20Sopenharmony_ci			w83627hf_write_value(data, W83627HF_REG_TEMP2_CONFIG,
17768c2ecf20Sopenharmony_ci				tmp & 0xfe);
17778c2ecf20Sopenharmony_ci		}
17788c2ecf20Sopenharmony_ci
17798c2ecf20Sopenharmony_ci		/* Enable temp3 */
17808c2ecf20Sopenharmony_ci		if (type != w83697hf) {
17818c2ecf20Sopenharmony_ci			tmp = w83627hf_read_value(data,
17828c2ecf20Sopenharmony_ci				W83627HF_REG_TEMP3_CONFIG);
17838c2ecf20Sopenharmony_ci			if (tmp & 0x01) {
17848c2ecf20Sopenharmony_ci				dev_warn(&pdev->dev, "Enabling temp3, "
17858c2ecf20Sopenharmony_ci					 "readings might not make sense\n");
17868c2ecf20Sopenharmony_ci				w83627hf_write_value(data,
17878c2ecf20Sopenharmony_ci					W83627HF_REG_TEMP3_CONFIG, tmp & 0xfe);
17888c2ecf20Sopenharmony_ci			}
17898c2ecf20Sopenharmony_ci		}
17908c2ecf20Sopenharmony_ci	}
17918c2ecf20Sopenharmony_ci
17928c2ecf20Sopenharmony_ci	/* Start monitoring */
17938c2ecf20Sopenharmony_ci	w83627hf_write_value(data, W83781D_REG_CONFIG,
17948c2ecf20Sopenharmony_ci			    (w83627hf_read_value(data,
17958c2ecf20Sopenharmony_ci						W83781D_REG_CONFIG) & 0xf7)
17968c2ecf20Sopenharmony_ci			    | 0x01);
17978c2ecf20Sopenharmony_ci
17988c2ecf20Sopenharmony_ci	/* Enable VBAT monitoring if needed */
17998c2ecf20Sopenharmony_ci	tmp = w83627hf_read_value(data, W83781D_REG_VBAT);
18008c2ecf20Sopenharmony_ci	if (!(tmp & 0x01))
18018c2ecf20Sopenharmony_ci		w83627hf_write_value(data, W83781D_REG_VBAT, tmp | 0x01);
18028c2ecf20Sopenharmony_ci}
18038c2ecf20Sopenharmony_ci
18048c2ecf20Sopenharmony_cistatic void w83627hf_update_fan_div(struct w83627hf_data *data)
18058c2ecf20Sopenharmony_ci{
18068c2ecf20Sopenharmony_ci	int reg;
18078c2ecf20Sopenharmony_ci
18088c2ecf20Sopenharmony_ci	reg = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
18098c2ecf20Sopenharmony_ci	data->fan_div[0] = (reg >> 4) & 0x03;
18108c2ecf20Sopenharmony_ci	data->fan_div[1] = (reg >> 6) & 0x03;
18118c2ecf20Sopenharmony_ci	if (data->type != w83697hf) {
18128c2ecf20Sopenharmony_ci		data->fan_div[2] = (w83627hf_read_value(data,
18138c2ecf20Sopenharmony_ci				       W83781D_REG_PIN) >> 6) & 0x03;
18148c2ecf20Sopenharmony_ci	}
18158c2ecf20Sopenharmony_ci	reg = w83627hf_read_value(data, W83781D_REG_VBAT);
18168c2ecf20Sopenharmony_ci	data->fan_div[0] |= (reg >> 3) & 0x04;
18178c2ecf20Sopenharmony_ci	data->fan_div[1] |= (reg >> 4) & 0x04;
18188c2ecf20Sopenharmony_ci	if (data->type != w83697hf)
18198c2ecf20Sopenharmony_ci		data->fan_div[2] |= (reg >> 5) & 0x04;
18208c2ecf20Sopenharmony_ci}
18218c2ecf20Sopenharmony_ci
18228c2ecf20Sopenharmony_cistatic struct w83627hf_data *w83627hf_update_device(struct device *dev)
18238c2ecf20Sopenharmony_ci{
18248c2ecf20Sopenharmony_ci	struct w83627hf_data *data = dev_get_drvdata(dev);
18258c2ecf20Sopenharmony_ci	int i, num_temps = (data->type == w83697hf) ? 2 : 3;
18268c2ecf20Sopenharmony_ci	int num_pwms = (data->type == w83697hf) ? 2 : 3;
18278c2ecf20Sopenharmony_ci
18288c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
18298c2ecf20Sopenharmony_ci
18308c2ecf20Sopenharmony_ci	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
18318c2ecf20Sopenharmony_ci	    || !data->valid) {
18328c2ecf20Sopenharmony_ci		for (i = 0; i <= 8; i++) {
18338c2ecf20Sopenharmony_ci			/* skip missing sensors */
18348c2ecf20Sopenharmony_ci			if (((data->type == w83697hf) && (i == 1)) ||
18358c2ecf20Sopenharmony_ci			    ((data->type != w83627hf && data->type != w83697hf)
18368c2ecf20Sopenharmony_ci			    && (i == 5 || i == 6)))
18378c2ecf20Sopenharmony_ci				continue;
18388c2ecf20Sopenharmony_ci			data->in[i] =
18398c2ecf20Sopenharmony_ci			    w83627hf_read_value(data, W83781D_REG_IN(i));
18408c2ecf20Sopenharmony_ci			data->in_min[i] =
18418c2ecf20Sopenharmony_ci			    w83627hf_read_value(data,
18428c2ecf20Sopenharmony_ci					       W83781D_REG_IN_MIN(i));
18438c2ecf20Sopenharmony_ci			data->in_max[i] =
18448c2ecf20Sopenharmony_ci			    w83627hf_read_value(data,
18458c2ecf20Sopenharmony_ci					       W83781D_REG_IN_MAX(i));
18468c2ecf20Sopenharmony_ci		}
18478c2ecf20Sopenharmony_ci		for (i = 0; i <= 2; i++) {
18488c2ecf20Sopenharmony_ci			data->fan[i] =
18498c2ecf20Sopenharmony_ci			    w83627hf_read_value(data, W83627HF_REG_FAN(i));
18508c2ecf20Sopenharmony_ci			data->fan_min[i] =
18518c2ecf20Sopenharmony_ci			    w83627hf_read_value(data,
18528c2ecf20Sopenharmony_ci					       W83627HF_REG_FAN_MIN(i));
18538c2ecf20Sopenharmony_ci		}
18548c2ecf20Sopenharmony_ci		for (i = 0; i <= 2; i++) {
18558c2ecf20Sopenharmony_ci			u8 tmp = w83627hf_read_value(data,
18568c2ecf20Sopenharmony_ci				W836X7HF_REG_PWM(data->type, i));
18578c2ecf20Sopenharmony_ci 			/* bits 0-3 are reserved  in 627THF */
18588c2ecf20Sopenharmony_ci 			if (data->type == w83627thf)
18598c2ecf20Sopenharmony_ci				tmp &= 0xf0;
18608c2ecf20Sopenharmony_ci			data->pwm[i] = tmp;
18618c2ecf20Sopenharmony_ci			if (i == 1 &&
18628c2ecf20Sopenharmony_ci			    (data->type == w83627hf || data->type == w83697hf))
18638c2ecf20Sopenharmony_ci				break;
18648c2ecf20Sopenharmony_ci		}
18658c2ecf20Sopenharmony_ci		if (data->type == w83627hf) {
18668c2ecf20Sopenharmony_ci				u8 tmp = w83627hf_read_value(data,
18678c2ecf20Sopenharmony_ci						W83627HF_REG_PWM_FREQ);
18688c2ecf20Sopenharmony_ci				data->pwm_freq[0] = tmp & 0x07;
18698c2ecf20Sopenharmony_ci				data->pwm_freq[1] = (tmp >> 4) & 0x07;
18708c2ecf20Sopenharmony_ci		} else if (data->type != w83627thf) {
18718c2ecf20Sopenharmony_ci			for (i = 1; i <= 3; i++) {
18728c2ecf20Sopenharmony_ci				data->pwm_freq[i - 1] =
18738c2ecf20Sopenharmony_ci					w83627hf_read_value(data,
18748c2ecf20Sopenharmony_ci						W83637HF_REG_PWM_FREQ[i - 1]);
18758c2ecf20Sopenharmony_ci				if (i == 2 && (data->type == w83697hf))
18768c2ecf20Sopenharmony_ci					break;
18778c2ecf20Sopenharmony_ci			}
18788c2ecf20Sopenharmony_ci		}
18798c2ecf20Sopenharmony_ci		if (data->type != w83627hf) {
18808c2ecf20Sopenharmony_ci			for (i = 0; i < num_pwms; i++) {
18818c2ecf20Sopenharmony_ci				u8 tmp = w83627hf_read_value(data,
18828c2ecf20Sopenharmony_ci					W83627THF_REG_PWM_ENABLE[i]);
18838c2ecf20Sopenharmony_ci				data->pwm_enable[i] =
18848c2ecf20Sopenharmony_ci					((tmp >> W83627THF_PWM_ENABLE_SHIFT[i])
18858c2ecf20Sopenharmony_ci					& 0x03) + 1;
18868c2ecf20Sopenharmony_ci			}
18878c2ecf20Sopenharmony_ci		}
18888c2ecf20Sopenharmony_ci		for (i = 0; i < num_temps; i++) {
18898c2ecf20Sopenharmony_ci			data->temp[i] = w83627hf_read_value(
18908c2ecf20Sopenharmony_ci						data, w83627hf_reg_temp[i]);
18918c2ecf20Sopenharmony_ci			data->temp_max[i] = w83627hf_read_value(
18928c2ecf20Sopenharmony_ci						data, w83627hf_reg_temp_over[i]);
18938c2ecf20Sopenharmony_ci			data->temp_max_hyst[i] = w83627hf_read_value(
18948c2ecf20Sopenharmony_ci						data, w83627hf_reg_temp_hyst[i]);
18958c2ecf20Sopenharmony_ci		}
18968c2ecf20Sopenharmony_ci
18978c2ecf20Sopenharmony_ci		w83627hf_update_fan_div(data);
18988c2ecf20Sopenharmony_ci
18998c2ecf20Sopenharmony_ci		data->alarms =
19008c2ecf20Sopenharmony_ci		    w83627hf_read_value(data, W83781D_REG_ALARM1) |
19018c2ecf20Sopenharmony_ci		    (w83627hf_read_value(data, W83781D_REG_ALARM2) << 8) |
19028c2ecf20Sopenharmony_ci		    (w83627hf_read_value(data, W83781D_REG_ALARM3) << 16);
19038c2ecf20Sopenharmony_ci		i = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
19048c2ecf20Sopenharmony_ci		data->beep_mask = (i << 8) |
19058c2ecf20Sopenharmony_ci		    w83627hf_read_value(data, W83781D_REG_BEEP_INTS1) |
19068c2ecf20Sopenharmony_ci		    w83627hf_read_value(data, W83781D_REG_BEEP_INTS3) << 16;
19078c2ecf20Sopenharmony_ci		data->last_updated = jiffies;
19088c2ecf20Sopenharmony_ci		data->valid = 1;
19098c2ecf20Sopenharmony_ci	}
19108c2ecf20Sopenharmony_ci
19118c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
19128c2ecf20Sopenharmony_ci
19138c2ecf20Sopenharmony_ci	return data;
19148c2ecf20Sopenharmony_ci}
19158c2ecf20Sopenharmony_ci
19168c2ecf20Sopenharmony_cistatic int __init w83627hf_device_add(unsigned short address,
19178c2ecf20Sopenharmony_ci				      const struct w83627hf_sio_data *sio_data)
19188c2ecf20Sopenharmony_ci{
19198c2ecf20Sopenharmony_ci	struct resource res = {
19208c2ecf20Sopenharmony_ci		.start	= address + WINB_REGION_OFFSET,
19218c2ecf20Sopenharmony_ci		.end	= address + WINB_REGION_OFFSET + WINB_REGION_SIZE - 1,
19228c2ecf20Sopenharmony_ci		.name	= DRVNAME,
19238c2ecf20Sopenharmony_ci		.flags	= IORESOURCE_IO,
19248c2ecf20Sopenharmony_ci	};
19258c2ecf20Sopenharmony_ci	int err;
19268c2ecf20Sopenharmony_ci
19278c2ecf20Sopenharmony_ci	err = acpi_check_resource_conflict(&res);
19288c2ecf20Sopenharmony_ci	if (err)
19298c2ecf20Sopenharmony_ci		goto exit;
19308c2ecf20Sopenharmony_ci
19318c2ecf20Sopenharmony_ci	pdev = platform_device_alloc(DRVNAME, address);
19328c2ecf20Sopenharmony_ci	if (!pdev) {
19338c2ecf20Sopenharmony_ci		err = -ENOMEM;
19348c2ecf20Sopenharmony_ci		pr_err("Device allocation failed\n");
19358c2ecf20Sopenharmony_ci		goto exit;
19368c2ecf20Sopenharmony_ci	}
19378c2ecf20Sopenharmony_ci
19388c2ecf20Sopenharmony_ci	err = platform_device_add_resources(pdev, &res, 1);
19398c2ecf20Sopenharmony_ci	if (err) {
19408c2ecf20Sopenharmony_ci		pr_err("Device resource addition failed (%d)\n", err);
19418c2ecf20Sopenharmony_ci		goto exit_device_put;
19428c2ecf20Sopenharmony_ci	}
19438c2ecf20Sopenharmony_ci
19448c2ecf20Sopenharmony_ci	err = platform_device_add_data(pdev, sio_data,
19458c2ecf20Sopenharmony_ci				       sizeof(struct w83627hf_sio_data));
19468c2ecf20Sopenharmony_ci	if (err) {
19478c2ecf20Sopenharmony_ci		pr_err("Platform data allocation failed\n");
19488c2ecf20Sopenharmony_ci		goto exit_device_put;
19498c2ecf20Sopenharmony_ci	}
19508c2ecf20Sopenharmony_ci
19518c2ecf20Sopenharmony_ci	err = platform_device_add(pdev);
19528c2ecf20Sopenharmony_ci	if (err) {
19538c2ecf20Sopenharmony_ci		pr_err("Device addition failed (%d)\n", err);
19548c2ecf20Sopenharmony_ci		goto exit_device_put;
19558c2ecf20Sopenharmony_ci	}
19568c2ecf20Sopenharmony_ci
19578c2ecf20Sopenharmony_ci	return 0;
19588c2ecf20Sopenharmony_ci
19598c2ecf20Sopenharmony_ciexit_device_put:
19608c2ecf20Sopenharmony_ci	platform_device_put(pdev);
19618c2ecf20Sopenharmony_ciexit:
19628c2ecf20Sopenharmony_ci	return err;
19638c2ecf20Sopenharmony_ci}
19648c2ecf20Sopenharmony_ci
19658c2ecf20Sopenharmony_cistatic int __init sensors_w83627hf_init(void)
19668c2ecf20Sopenharmony_ci{
19678c2ecf20Sopenharmony_ci	int err;
19688c2ecf20Sopenharmony_ci	unsigned short address;
19698c2ecf20Sopenharmony_ci	struct w83627hf_sio_data sio_data;
19708c2ecf20Sopenharmony_ci
19718c2ecf20Sopenharmony_ci	if (w83627hf_find(0x2e, &address, &sio_data)
19728c2ecf20Sopenharmony_ci	 && w83627hf_find(0x4e, &address, &sio_data))
19738c2ecf20Sopenharmony_ci		return -ENODEV;
19748c2ecf20Sopenharmony_ci
19758c2ecf20Sopenharmony_ci	err = platform_driver_register(&w83627hf_driver);
19768c2ecf20Sopenharmony_ci	if (err)
19778c2ecf20Sopenharmony_ci		goto exit;
19788c2ecf20Sopenharmony_ci
19798c2ecf20Sopenharmony_ci	/* Sets global pdev as a side effect */
19808c2ecf20Sopenharmony_ci	err = w83627hf_device_add(address, &sio_data);
19818c2ecf20Sopenharmony_ci	if (err)
19828c2ecf20Sopenharmony_ci		goto exit_driver;
19838c2ecf20Sopenharmony_ci
19848c2ecf20Sopenharmony_ci	return 0;
19858c2ecf20Sopenharmony_ci
19868c2ecf20Sopenharmony_ciexit_driver:
19878c2ecf20Sopenharmony_ci	platform_driver_unregister(&w83627hf_driver);
19888c2ecf20Sopenharmony_ciexit:
19898c2ecf20Sopenharmony_ci	return err;
19908c2ecf20Sopenharmony_ci}
19918c2ecf20Sopenharmony_ci
19928c2ecf20Sopenharmony_cistatic void __exit sensors_w83627hf_exit(void)
19938c2ecf20Sopenharmony_ci{
19948c2ecf20Sopenharmony_ci	platform_device_unregister(pdev);
19958c2ecf20Sopenharmony_ci	platform_driver_unregister(&w83627hf_driver);
19968c2ecf20Sopenharmony_ci}
19978c2ecf20Sopenharmony_ci
19988c2ecf20Sopenharmony_ciMODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
19998c2ecf20Sopenharmony_ci	      "Philip Edelbrock <phil@netroedge.com>, "
20008c2ecf20Sopenharmony_ci	      "and Mark Studebaker <mdsxyz123@yahoo.com>");
20018c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("W83627HF driver");
20028c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
20038c2ecf20Sopenharmony_ci
20048c2ecf20Sopenharmony_cimodule_init(sensors_w83627hf_init);
20058c2ecf20Sopenharmony_cimodule_exit(sensors_w83627hf_exit);
2006