Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * asb100.c - Part of lm_sensors, Linux kernel modules for hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *	      monitoring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * (derived from w83781d.c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * Copyright (C) 1998 - 2003  Frodo Looijaard <frodol@dds.nl>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *			      Philip Edelbrock <phil@netroedge.com>, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *			      Mark Studebaker <mdsxyz123@yahoo.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  * This driver supports the hardware sensor chips: Asus ASB100 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  * ASB100-A "BACH".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  * ASB100-A supports pwm1, while plain ASB100 does not.  There is no known
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  * way for the driver to tell which one is there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  * Chip		#vin	#fanin	#pwm	#temp	wchipid	vendid	i2c	ISA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  * asb100	7	3	1	4	0x31	0x0694	yes	no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/hwmon-vid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include "lm75.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) /* I2C addresses to scan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) static const unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) static unsigned short force_subclients[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) module_param_array(force_subclients, short, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) MODULE_PARM_DESC(force_subclients,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	"List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) /* Voltage IN registers 0-6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #define ASB100_REG_IN(nr)	(0x20 + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #define ASB100_REG_IN_MAX(nr)	(0x2b + (nr * 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #define ASB100_REG_IN_MIN(nr)	(0x2c + (nr * 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) /* FAN IN registers 1-3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #define ASB100_REG_FAN(nr)	(0x28 + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #define ASB100_REG_FAN_MIN(nr)	(0x3b + (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) /* TEMPERATURE registers 1-4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) static const u16 asb100_reg_temp[]	= {0, 0x27, 0x150, 0x250, 0x17};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) static const u16 asb100_reg_temp_max[]	= {0, 0x39, 0x155, 0x255, 0x18};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) static const u16 asb100_reg_temp_hyst[]	= {0, 0x3a, 0x153, 0x253, 0x19};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) #define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #define ASB100_REG_TEMP2_CONFIG	0x0152
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) #define ASB100_REG_TEMP3_CONFIG	0x0252
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) #define ASB100_REG_CONFIG	0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) #define ASB100_REG_ALARM1	0x41
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) #define ASB100_REG_ALARM2	0x42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) #define ASB100_REG_SMIM1	0x43
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #define ASB100_REG_SMIM2	0x44
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #define ASB100_REG_VID_FANDIV	0x47
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) #define ASB100_REG_I2C_ADDR	0x48
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) #define ASB100_REG_CHIPID	0x49
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #define ASB100_REG_I2C_SUBADDR	0x4a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) #define ASB100_REG_PIN		0x4b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) #define ASB100_REG_IRQ		0x4c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) #define ASB100_REG_BANK		0x4e
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) #define ASB100_REG_CHIPMAN	0x4f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) #define ASB100_REG_WCHIPID	0x58
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) /* bit 7 -> enable, bits 0-3 -> duty cycle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) #define ASB100_REG_PWM1		0x59
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  * CONVERSIONS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * Rounding and limit checking is only done on the TO_REG variants.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) /* These constants are a guess, consistent w/ w83781d */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) #define ASB100_IN_MIN		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) #define ASB100_IN_MAX		4080
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  * IN: 1/1000 V (0V to 4.08V)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100)  * REG: 16mV/bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) static u8 IN_TO_REG(unsigned val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	unsigned nval = clamp_val(val, ASB100_IN_MIN, ASB100_IN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	return (nval + 8) / 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) static unsigned IN_FROM_REG(u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	return reg * 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) static u8 FAN_TO_REG(long rpm, int div)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	if (rpm == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	if (rpm == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		return 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	rpm = clamp_val(rpm, 1, 1000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) static int FAN_FROM_REG(u8 val, int div)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) /* These constants are a guess, consistent w/ w83781d */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) #define ASB100_TEMP_MIN		-128000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) #define ASB100_TEMP_MAX		127000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133)  * TEMP: 0.001C/bit (-128C to +127C)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134)  * REG: 1C/bit, two's complement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) static u8 TEMP_TO_REG(long temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	int ntemp = clamp_val(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	ntemp += (ntemp < 0 ? -500 : 500);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	return (u8)(ntemp / 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) static int TEMP_FROM_REG(u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	return (s8)reg * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149)  * PWM: 0 - 255 per sensors documentation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150)  * REG: (6.25% duty cycle per bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) static u8 ASB100_PWM_TO_REG(int pwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	pwm = clamp_val(pwm, 0, 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	return (u8)(pwm / 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) static int ASB100_PWM_FROM_REG(u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	return reg * 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) #define DIV_FROM_REG(val) (1 << (val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) static u8 DIV_TO_REG(long val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175)  * For each registered client, we need to keep some data in memory. That
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176)  * data is pointed to by client->data. The structure itself is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177)  * dynamically allocated, at the same time the client itself is allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) struct asb100_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	unsigned long last_updated;	/* In jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	/* array of 2 pointers to subclients */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	struct i2c_client *lm75[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	char valid;		/* !=0 if following fields are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	u8 in[7];		/* Register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	u8 in_max[7];		/* Register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	u8 in_min[7];		/* Register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	u8 fan[3];		/* Register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	u8 fan_min[3];		/* Register value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	u16 temp[4];		/* Register value (0 and 3 are u8 only) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	u16 temp_max[4];	/* Register value (0 and 3 are u8 only) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	u16 temp_hyst[4];	/* Register value (0 and 3 are u8 only) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	u8 fan_div[3];		/* Register encoding, right justified */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	u8 pwm;			/* Register encoding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	u8 vid;			/* Register encoding, combined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	u32 alarms;		/* Register encoding, combined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	u8 vrm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) static int asb100_read_value(struct i2c_client *client, u16 reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) static int asb100_probe(struct i2c_client *client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) static int asb100_detect(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 			 struct i2c_board_info *info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) static int asb100_remove(struct i2c_client *client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) static struct asb100_data *asb100_update_device(struct device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) static void asb100_init_client(struct i2c_client *client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) static const struct i2c_device_id asb100_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	{ "asb100", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) MODULE_DEVICE_TABLE(i2c, asb100_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) static struct i2c_driver asb100_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	.class		= I2C_CLASS_HWMON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		.name	= "asb100",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	.probe_new	= asb100_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	.remove		= asb100_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	.id_table	= asb100_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	.detect		= asb100_detect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	.address_list	= normal_i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) /* 7 Voltages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) #define show_in_reg(reg) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	int nr = to_sensor_dev_attr(attr)->index; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	struct asb100_data *data = asb100_update_device(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) show_in_reg(in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) show_in_reg(in_min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) show_in_reg(in_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) #define set_in_reg(REG, reg) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) static ssize_t set_in_##reg(struct device *dev, struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		const char *buf, size_t count) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	int nr = to_sensor_dev_attr(attr)->index; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	struct i2c_client *client = to_i2c_client(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	struct asb100_data *data = i2c_get_clientdata(client); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	unsigned long val; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	int err = kstrtoul(buf, 10, &val); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	if (err) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		return err; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	mutex_lock(&data->update_lock); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	data->in_##reg[nr] = IN_TO_REG(val); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		data->in_##reg[nr]); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	mutex_unlock(&data->update_lock); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	return count; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) set_in_reg(MIN, min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) set_in_reg(MAX, max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) #define sysfs_in(offset) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		show_in, NULL, offset); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		show_in_min, set_in_min, offset); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		show_in_max, set_in_max, offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) sysfs_in(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) sysfs_in(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) sysfs_in(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) sysfs_in(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) sysfs_in(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) sysfs_in(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) sysfs_in(6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) /* 3 Fans */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	struct asb100_data *data = asb100_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		DIV_FROM_REG(data->fan_div[nr])));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	struct asb100_data *data = asb100_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 		DIV_FROM_REG(data->fan_div[nr])));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	struct asb100_data *data = asb100_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	struct asb100_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333)  * Note: we save and restore the fan minimum here, because its value is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334)  * determined in part by the fan divisor.  This follows the principle of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335)  * least surprise; the user doesn't expect the fan minimum to change just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336)  * because the divisor changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	int nr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	struct asb100_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	unsigned long min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	int reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	min = FAN_FROM_REG(data->fan_min[nr],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 			DIV_FROM_REG(data->fan_div[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	data->fan_div[nr] = DIV_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	switch (nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	case 0:	/* fan 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		reg = (reg & 0xcf) | (data->fan_div[0] << 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	case 1:	/* fan 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		reg = (reg & 0x3f) | (data->fan_div[1] << 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	case 2:	/* fan 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		reg = asb100_read_value(client, ASB100_REG_PIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		reg = (reg & 0x3f) | (data->fan_div[2] << 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		asb100_write_value(client, ASB100_REG_PIN, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	data->fan_min[nr] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) #define sysfs_fan(offset) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		show_fan, NULL, offset - 1); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		show_fan_min, set_fan_min, offset - 1); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		show_fan_div, set_fan_div, offset - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) sysfs_fan(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) sysfs_fan(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) sysfs_fan(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) /* 4 Temp. Sensors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	switch (nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	case 1: case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	case 0: case 3: default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) #define show_temp_reg(reg) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	int nr = to_sensor_dev_attr(attr)->index; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	struct asb100_data *data = asb100_update_device(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) show_temp_reg(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) show_temp_reg(temp_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) show_temp_reg(temp_hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) #define set_temp_reg(REG, reg) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) static ssize_t set_##reg(struct device *dev, struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		const char *buf, size_t count) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	int nr = to_sensor_dev_attr(attr)->index; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	struct i2c_client *client = to_i2c_client(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	struct asb100_data *data = i2c_get_clientdata(client); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	long val; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	int err = kstrtol(buf, 10, &val); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	if (err) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		return err; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	mutex_lock(&data->update_lock); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	switch (nr) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	case 1: case 2: \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		data->reg[nr] = LM75_TEMP_TO_REG(val); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		break; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	case 0: case 3: default: \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		data->reg[nr] = TEMP_TO_REG(val); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		break; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	} \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 			data->reg[nr]); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	mutex_unlock(&data->update_lock); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	return count; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) set_temp_reg(MAX, temp_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) set_temp_reg(HYST, temp_hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) #define sysfs_temp(num) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) static SENSOR_DEVICE_ATTR(temp##num##_input, S_IRUGO, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		show_temp, NULL, num - 1); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) static SENSOR_DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		show_temp_max, set_temp_max, num - 1); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) static SENSOR_DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		show_temp_hyst, set_temp_hyst, num - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) sysfs_temp(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) sysfs_temp(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) sysfs_temp(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) sysfs_temp(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) /* VID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) static ssize_t cpu0_vid_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 			     struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	struct asb100_data *data = asb100_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) static DEVICE_ATTR_RO(cpu0_vid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) /* VRM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	struct asb100_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	return sprintf(buf, "%d\n", data->vrm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 			 const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	struct asb100_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	if (val > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	data->vrm = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) /* Alarms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) static DEVICE_ATTR_RW(vrm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	struct asb100_data *data = asb100_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	return sprintf(buf, "%u\n", data->alarms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) static DEVICE_ATTR_RO(alarms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	int bitnr = to_sensor_dev_attr(attr)->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	struct asb100_data *data = asb100_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) /* 1 PWM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	struct asb100_data *data = asb100_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 			  const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	struct asb100_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	data->pwm &= 0x80; /* keep the enable bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) static ssize_t pwm1_enable_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	struct asb100_data *data = asb100_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) static ssize_t pwm1_enable_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 				 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 				 const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	struct asb100_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	err = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	data->pwm &= 0x0f; /* keep the duty cycle bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	data->pwm |= (val ? 0x80 : 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) static DEVICE_ATTR_RW(pwm1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) static DEVICE_ATTR_RW(pwm1_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) static struct attribute *asb100_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	&sensor_dev_attr_in0_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	&sensor_dev_attr_in0_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	&sensor_dev_attr_in0_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	&sensor_dev_attr_in1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	&sensor_dev_attr_in1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	&sensor_dev_attr_in1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	&sensor_dev_attr_in2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	&sensor_dev_attr_in2_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	&sensor_dev_attr_in2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	&sensor_dev_attr_in3_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	&sensor_dev_attr_in3_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	&sensor_dev_attr_in3_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	&sensor_dev_attr_in4_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	&sensor_dev_attr_in4_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	&sensor_dev_attr_in4_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	&sensor_dev_attr_in5_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	&sensor_dev_attr_in5_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	&sensor_dev_attr_in5_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	&sensor_dev_attr_in6_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	&sensor_dev_attr_in6_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	&sensor_dev_attr_in6_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	&sensor_dev_attr_fan1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	&sensor_dev_attr_fan1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	&sensor_dev_attr_fan1_div.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	&sensor_dev_attr_fan2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	&sensor_dev_attr_fan2_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	&sensor_dev_attr_fan2_div.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	&sensor_dev_attr_fan3_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	&sensor_dev_attr_fan3_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	&sensor_dev_attr_fan3_div.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	&sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	&sensor_dev_attr_temp2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	&sensor_dev_attr_temp2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	&sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	&sensor_dev_attr_temp3_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	&sensor_dev_attr_temp3_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	&sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	&sensor_dev_attr_temp4_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	&sensor_dev_attr_temp4_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	&sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	&dev_attr_cpu0_vid.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	&dev_attr_vrm.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	&dev_attr_alarms.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	&dev_attr_pwm1.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	&dev_attr_pwm1_enable.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) static const struct attribute_group asb100_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	.attrs = asb100_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) static int asb100_detect_subclients(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	int i, id, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	int address = client->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	unsigned short sc_addr[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	struct asb100_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	id = i2c_adapter_id(adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	if (force_subclients[0] == id && force_subclients[1] == address) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		for (i = 2; i <= 3; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 			if (force_subclients[i] < 0x48 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 			    force_subclients[i] > 0x4f) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 				dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 					"invalid subclient address %d; must be 0x48-0x4f\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 					force_subclients[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 				err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 				goto ERROR_SC_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		asb100_write_value(client, ASB100_REG_I2C_SUBADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 					(force_subclients[2] & 0x07) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 					((force_subclients[3] & 0x07) << 4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		sc_addr[0] = force_subclients[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		sc_addr[1] = force_subclients[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		int val = asb100_read_value(client, ASB100_REG_I2C_SUBADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		sc_addr[0] = 0x48 + (val & 0x07);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		sc_addr[1] = 0x48 + ((val >> 4) & 0x07);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	if (sc_addr[0] == sc_addr[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 			"duplicate addresses 0x%x for subclients\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 			sc_addr[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		goto ERROR_SC_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	data->lm75[0] = i2c_new_dummy_device(adapter, sc_addr[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	if (IS_ERR(data->lm75[0])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 			"subclient %d registration at address 0x%x failed.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 			1, sc_addr[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		err = PTR_ERR(data->lm75[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		goto ERROR_SC_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	data->lm75[1] = i2c_new_dummy_device(adapter, sc_addr[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	if (IS_ERR(data->lm75[1])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 			"subclient %d registration at address 0x%x failed.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 			2, sc_addr[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		err = PTR_ERR(data->lm75[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		goto ERROR_SC_3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) /* Undo inits in case of errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) ERROR_SC_3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	i2c_unregister_device(data->lm75[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) ERROR_SC_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) /* Return 0 if detection is successful, -ENODEV otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) static int asb100_detect(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 			 struct i2c_board_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	int val1, val2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		pr_debug("detect failed, smbus byte data not supported!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	val1 = i2c_smbus_read_byte_data(client, ASB100_REG_BANK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	/* If we're in bank 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	if ((!(val1 & 0x07)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			/* Check for ASB100 ID (low byte) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			(((!(val1 & 0x80)) && (val2 != 0x94)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 			/* Check for ASB100 ID (high byte ) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 			((val1 & 0x80) && (val2 != 0x06)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		pr_debug("detect failed, bad chip id 0x%02x!\n", val2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	/* Put it now into bank 0 and Vendor ID High Byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	i2c_smbus_write_byte_data(client, ASB100_REG_BANK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		(i2c_smbus_read_byte_data(client, ASB100_REG_BANK) & 0x78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		| 0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	/* Determine the chip type. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	val1 = i2c_smbus_read_byte_data(client, ASB100_REG_WCHIPID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	if (val1 != 0x31 || val2 != 0x06)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	strlcpy(info->type, "asb100", I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) static int asb100_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	struct asb100_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	data = devm_kzalloc(&client->dev, sizeof(struct asb100_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 			    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	i2c_set_clientdata(client, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	mutex_init(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	/* Attach secondary lm75 clients */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	err = asb100_detect_subclients(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	/* Initialize the chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	asb100_init_client(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	/* A few vars need to be filled upon startup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	data->fan_min[0] = asb100_read_value(client, ASB100_REG_FAN_MIN(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	data->fan_min[1] = asb100_read_value(client, ASB100_REG_FAN_MIN(1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	data->fan_min[2] = asb100_read_value(client, ASB100_REG_FAN_MIN(2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	/* Register sysfs hooks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	err = sysfs_create_group(&client->dev.kobj, &asb100_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		goto ERROR3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	data->hwmon_dev = hwmon_device_register(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	if (IS_ERR(data->hwmon_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		err = PTR_ERR(data->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		goto ERROR4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) ERROR4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	sysfs_remove_group(&client->dev.kobj, &asb100_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) ERROR3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	i2c_unregister_device(data->lm75[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	i2c_unregister_device(data->lm75[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) static int asb100_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	struct asb100_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	hwmon_device_unregister(data->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	sysfs_remove_group(&client->dev.kobj, &asb100_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	i2c_unregister_device(data->lm75[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	i2c_unregister_device(data->lm75[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839)  * The SMBus locks itself, usually, but nothing may access the chip between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840)  * bank switches.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) static int asb100_read_value(struct i2c_client *client, u16 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	struct asb100_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	struct i2c_client *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	int res, bank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	bank = (reg >> 8) & 0x0f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	if (bank > 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		/* switch banks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	if (bank == 0 || bank > 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		res = i2c_smbus_read_byte_data(client, reg & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		/* switch to subclient */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		cl = data->lm75[bank - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		/* convert from ISA to LM75 I2C addresses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		switch (reg & 0xff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		case 0x50: /* TEMP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 			res = i2c_smbus_read_word_swapped(cl, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		case 0x52: /* CONFIG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 			res = i2c_smbus_read_byte_data(cl, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		case 0x53: /* HYST */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 			res = i2c_smbus_read_word_swapped(cl, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		case 0x55: /* MAX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 			res = i2c_smbus_read_word_swapped(cl, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	if (bank > 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	struct asb100_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	struct i2c_client *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	int bank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	bank = (reg >> 8) & 0x0f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	if (bank > 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		/* switch banks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	if (bank == 0 || bank > 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		/* switch to subclient */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		cl = data->lm75[bank - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		/* convert from ISA to LM75 I2C addresses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		switch (reg & 0xff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		case 0x52: /* CONFIG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 			i2c_smbus_write_byte_data(cl, 1, value & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		case 0x53: /* HYST */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 			i2c_smbus_write_word_swapped(cl, 2, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		case 0x55: /* MAX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 			i2c_smbus_write_word_swapped(cl, 3, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	if (bank > 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) static void asb100_init_client(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	struct asb100_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	data->vrm = vid_which_vrm();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	/* Start monitoring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	asb100_write_value(client, ASB100_REG_CONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		(asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) static struct asb100_data *asb100_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	struct asb100_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		|| !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		dev_dbg(&client->dev, "starting device update...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		/* 7 voltage inputs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		for (i = 0; i < 7; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			data->in[i] = asb100_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 				ASB100_REG_IN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 			data->in_min[i] = asb100_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 				ASB100_REG_IN_MIN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 			data->in_max[i] = asb100_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 				ASB100_REG_IN_MAX(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		/* 3 fan inputs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		for (i = 0; i < 3; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 			data->fan[i] = asb100_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 					ASB100_REG_FAN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			data->fan_min[i] = asb100_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 					ASB100_REG_FAN_MIN(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		/* 4 temperature inputs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		for (i = 1; i <= 4; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			data->temp[i-1] = asb100_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 					ASB100_REG_TEMP(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 			data->temp_max[i-1] = asb100_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 					ASB100_REG_TEMP_MAX(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			data->temp_hyst[i-1] = asb100_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 					ASB100_REG_TEMP_HYST(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		/* VID and fan divisors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		data->vid = i & 0x0f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		data->vid |= (asb100_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 				ASB100_REG_CHIPID) & 0x01) << 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		data->fan_div[0] = (i >> 4) & 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		data->fan_div[1] = (i >> 6) & 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		data->fan_div[2] = (asb100_read_value(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 				ASB100_REG_PIN) >> 6) & 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		/* PWM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		/* alarms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 			(asb100_read_value(client, ASB100_REG_ALARM2) << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		data->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		dev_dbg(&client->dev, "... device update complete\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) module_i2c_driver(asb100_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) MODULE_DESCRIPTION("ASB100 Bach driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) MODULE_LICENSE("GPL");