18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * asb100.c - Part of lm_sensors, Linux kernel modules for hardware
48c2ecf20Sopenharmony_ci *	      monitoring
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * (derived from w83781d.c)
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Copyright (C) 1998 - 2003  Frodo Looijaard <frodol@dds.nl>,
118c2ecf20Sopenharmony_ci *			      Philip Edelbrock <phil@netroedge.com>, and
128c2ecf20Sopenharmony_ci *			      Mark Studebaker <mdsxyz123@yahoo.com>
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci * This driver supports the hardware sensor chips: Asus ASB100 and
178c2ecf20Sopenharmony_ci * ASB100-A "BACH".
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * ASB100-A supports pwm1, while plain ASB100 does not.  There is no known
208c2ecf20Sopenharmony_ci * way for the driver to tell which one is there.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * Chip		#vin	#fanin	#pwm	#temp	wchipid	vendid	i2c	ISA
238c2ecf20Sopenharmony_ci * asb100	7	3	1	4	0x31	0x0694	yes	no
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <linux/module.h>
298c2ecf20Sopenharmony_ci#include <linux/slab.h>
308c2ecf20Sopenharmony_ci#include <linux/i2c.h>
318c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
328c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h>
338c2ecf20Sopenharmony_ci#include <linux/hwmon-vid.h>
348c2ecf20Sopenharmony_ci#include <linux/err.h>
358c2ecf20Sopenharmony_ci#include <linux/init.h>
368c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
378c2ecf20Sopenharmony_ci#include <linux/mutex.h>
388c2ecf20Sopenharmony_ci#include "lm75.h"
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/* I2C addresses to scan */
418c2ecf20Sopenharmony_cistatic const unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic unsigned short force_subclients[4];
448c2ecf20Sopenharmony_cimodule_param_array(force_subclients, short, NULL, 0);
458c2ecf20Sopenharmony_ciMODULE_PARM_DESC(force_subclients,
468c2ecf20Sopenharmony_ci	"List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/* Voltage IN registers 0-6 */
498c2ecf20Sopenharmony_ci#define ASB100_REG_IN(nr)	(0x20 + (nr))
508c2ecf20Sopenharmony_ci#define ASB100_REG_IN_MAX(nr)	(0x2b + (nr * 2))
518c2ecf20Sopenharmony_ci#define ASB100_REG_IN_MIN(nr)	(0x2c + (nr * 2))
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/* FAN IN registers 1-3 */
548c2ecf20Sopenharmony_ci#define ASB100_REG_FAN(nr)	(0x28 + (nr))
558c2ecf20Sopenharmony_ci#define ASB100_REG_FAN_MIN(nr)	(0x3b + (nr))
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/* TEMPERATURE registers 1-4 */
588c2ecf20Sopenharmony_cistatic const u16 asb100_reg_temp[]	= {0, 0x27, 0x150, 0x250, 0x17};
598c2ecf20Sopenharmony_cistatic const u16 asb100_reg_temp_max[]	= {0, 0x39, 0x155, 0x255, 0x18};
608c2ecf20Sopenharmony_cistatic const u16 asb100_reg_temp_hyst[]	= {0, 0x3a, 0x153, 0x253, 0x19};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
638c2ecf20Sopenharmony_ci#define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
648c2ecf20Sopenharmony_ci#define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define ASB100_REG_TEMP2_CONFIG	0x0152
678c2ecf20Sopenharmony_ci#define ASB100_REG_TEMP3_CONFIG	0x0252
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#define ASB100_REG_CONFIG	0x40
718c2ecf20Sopenharmony_ci#define ASB100_REG_ALARM1	0x41
728c2ecf20Sopenharmony_ci#define ASB100_REG_ALARM2	0x42
738c2ecf20Sopenharmony_ci#define ASB100_REG_SMIM1	0x43
748c2ecf20Sopenharmony_ci#define ASB100_REG_SMIM2	0x44
758c2ecf20Sopenharmony_ci#define ASB100_REG_VID_FANDIV	0x47
768c2ecf20Sopenharmony_ci#define ASB100_REG_I2C_ADDR	0x48
778c2ecf20Sopenharmony_ci#define ASB100_REG_CHIPID	0x49
788c2ecf20Sopenharmony_ci#define ASB100_REG_I2C_SUBADDR	0x4a
798c2ecf20Sopenharmony_ci#define ASB100_REG_PIN		0x4b
808c2ecf20Sopenharmony_ci#define ASB100_REG_IRQ		0x4c
818c2ecf20Sopenharmony_ci#define ASB100_REG_BANK		0x4e
828c2ecf20Sopenharmony_ci#define ASB100_REG_CHIPMAN	0x4f
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define ASB100_REG_WCHIPID	0x58
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/* bit 7 -> enable, bits 0-3 -> duty cycle */
878c2ecf20Sopenharmony_ci#define ASB100_REG_PWM1		0x59
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/*
908c2ecf20Sopenharmony_ci * CONVERSIONS
918c2ecf20Sopenharmony_ci * Rounding and limit checking is only done on the TO_REG variants.
928c2ecf20Sopenharmony_ci */
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/* These constants are a guess, consistent w/ w83781d */
958c2ecf20Sopenharmony_ci#define ASB100_IN_MIN		0
968c2ecf20Sopenharmony_ci#define ASB100_IN_MAX		4080
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/*
998c2ecf20Sopenharmony_ci * IN: 1/1000 V (0V to 4.08V)
1008c2ecf20Sopenharmony_ci * REG: 16mV/bit
1018c2ecf20Sopenharmony_ci */
1028c2ecf20Sopenharmony_cistatic u8 IN_TO_REG(unsigned val)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	unsigned nval = clamp_val(val, ASB100_IN_MIN, ASB100_IN_MAX);
1058c2ecf20Sopenharmony_ci	return (nval + 8) / 16;
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic unsigned IN_FROM_REG(u8 reg)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	return reg * 16;
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic u8 FAN_TO_REG(long rpm, int div)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	if (rpm == -1)
1168c2ecf20Sopenharmony_ci		return 0;
1178c2ecf20Sopenharmony_ci	if (rpm == 0)
1188c2ecf20Sopenharmony_ci		return 255;
1198c2ecf20Sopenharmony_ci	rpm = clamp_val(rpm, 1, 1000000);
1208c2ecf20Sopenharmony_ci	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic int FAN_FROM_REG(u8 val, int div)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/* These constants are a guess, consistent w/ w83781d */
1298c2ecf20Sopenharmony_ci#define ASB100_TEMP_MIN		-128000
1308c2ecf20Sopenharmony_ci#define ASB100_TEMP_MAX		127000
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/*
1338c2ecf20Sopenharmony_ci * TEMP: 0.001C/bit (-128C to +127C)
1348c2ecf20Sopenharmony_ci * REG: 1C/bit, two's complement
1358c2ecf20Sopenharmony_ci */
1368c2ecf20Sopenharmony_cistatic u8 TEMP_TO_REG(long temp)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	int ntemp = clamp_val(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
1398c2ecf20Sopenharmony_ci	ntemp += (ntemp < 0 ? -500 : 500);
1408c2ecf20Sopenharmony_ci	return (u8)(ntemp / 1000);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic int TEMP_FROM_REG(u8 reg)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	return (s8)reg * 1000;
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci/*
1498c2ecf20Sopenharmony_ci * PWM: 0 - 255 per sensors documentation
1508c2ecf20Sopenharmony_ci * REG: (6.25% duty cycle per bit)
1518c2ecf20Sopenharmony_ci */
1528c2ecf20Sopenharmony_cistatic u8 ASB100_PWM_TO_REG(int pwm)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	pwm = clamp_val(pwm, 0, 255);
1558c2ecf20Sopenharmony_ci	return (u8)(pwm / 16);
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic int ASB100_PWM_FROM_REG(u8 reg)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	return reg * 16;
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci#define DIV_FROM_REG(val) (1 << (val))
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci/*
1668c2ecf20Sopenharmony_ci * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
1678c2ecf20Sopenharmony_ci * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
1688c2ecf20Sopenharmony_ci */
1698c2ecf20Sopenharmony_cistatic u8 DIV_TO_REG(long val)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci/*
1758c2ecf20Sopenharmony_ci * For each registered client, we need to keep some data in memory. That
1768c2ecf20Sopenharmony_ci * data is pointed to by client->data. The structure itself is
1778c2ecf20Sopenharmony_ci * dynamically allocated, at the same time the client itself is allocated.
1788c2ecf20Sopenharmony_ci */
1798c2ecf20Sopenharmony_cistruct asb100_data {
1808c2ecf20Sopenharmony_ci	struct device *hwmon_dev;
1818c2ecf20Sopenharmony_ci	struct mutex lock;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	struct mutex update_lock;
1848c2ecf20Sopenharmony_ci	unsigned long last_updated;	/* In jiffies */
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	/* array of 2 pointers to subclients */
1878c2ecf20Sopenharmony_ci	struct i2c_client *lm75[2];
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	char valid;		/* !=0 if following fields are valid */
1908c2ecf20Sopenharmony_ci	u8 in[7];		/* Register value */
1918c2ecf20Sopenharmony_ci	u8 in_max[7];		/* Register value */
1928c2ecf20Sopenharmony_ci	u8 in_min[7];		/* Register value */
1938c2ecf20Sopenharmony_ci	u8 fan[3];		/* Register value */
1948c2ecf20Sopenharmony_ci	u8 fan_min[3];		/* Register value */
1958c2ecf20Sopenharmony_ci	u16 temp[4];		/* Register value (0 and 3 are u8 only) */
1968c2ecf20Sopenharmony_ci	u16 temp_max[4];	/* Register value (0 and 3 are u8 only) */
1978c2ecf20Sopenharmony_ci	u16 temp_hyst[4];	/* Register value (0 and 3 are u8 only) */
1988c2ecf20Sopenharmony_ci	u8 fan_div[3];		/* Register encoding, right justified */
1998c2ecf20Sopenharmony_ci	u8 pwm;			/* Register encoding */
2008c2ecf20Sopenharmony_ci	u8 vid;			/* Register encoding, combined */
2018c2ecf20Sopenharmony_ci	u32 alarms;		/* Register encoding, combined */
2028c2ecf20Sopenharmony_ci	u8 vrm;
2038c2ecf20Sopenharmony_ci};
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistatic int asb100_read_value(struct i2c_client *client, u16 reg);
2068c2ecf20Sopenharmony_cistatic void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic int asb100_probe(struct i2c_client *client);
2098c2ecf20Sopenharmony_cistatic int asb100_detect(struct i2c_client *client,
2108c2ecf20Sopenharmony_ci			 struct i2c_board_info *info);
2118c2ecf20Sopenharmony_cistatic int asb100_remove(struct i2c_client *client);
2128c2ecf20Sopenharmony_cistatic struct asb100_data *asb100_update_device(struct device *dev);
2138c2ecf20Sopenharmony_cistatic void asb100_init_client(struct i2c_client *client);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic const struct i2c_device_id asb100_id[] = {
2168c2ecf20Sopenharmony_ci	{ "asb100", 0 },
2178c2ecf20Sopenharmony_ci	{ }
2188c2ecf20Sopenharmony_ci};
2198c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, asb100_id);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_cistatic struct i2c_driver asb100_driver = {
2228c2ecf20Sopenharmony_ci	.class		= I2C_CLASS_HWMON,
2238c2ecf20Sopenharmony_ci	.driver = {
2248c2ecf20Sopenharmony_ci		.name	= "asb100",
2258c2ecf20Sopenharmony_ci	},
2268c2ecf20Sopenharmony_ci	.probe_new	= asb100_probe,
2278c2ecf20Sopenharmony_ci	.remove		= asb100_remove,
2288c2ecf20Sopenharmony_ci	.id_table	= asb100_id,
2298c2ecf20Sopenharmony_ci	.detect		= asb100_detect,
2308c2ecf20Sopenharmony_ci	.address_list	= normal_i2c,
2318c2ecf20Sopenharmony_ci};
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci/* 7 Voltages */
2348c2ecf20Sopenharmony_ci#define show_in_reg(reg) \
2358c2ecf20Sopenharmony_cistatic ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
2368c2ecf20Sopenharmony_ci		char *buf) \
2378c2ecf20Sopenharmony_ci{ \
2388c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index; \
2398c2ecf20Sopenharmony_ci	struct asb100_data *data = asb100_update_device(dev); \
2408c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cishow_in_reg(in)
2448c2ecf20Sopenharmony_cishow_in_reg(in_min)
2458c2ecf20Sopenharmony_cishow_in_reg(in_max)
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci#define set_in_reg(REG, reg) \
2488c2ecf20Sopenharmony_cistatic ssize_t set_in_##reg(struct device *dev, struct device_attribute *attr, \
2498c2ecf20Sopenharmony_ci		const char *buf, size_t count) \
2508c2ecf20Sopenharmony_ci{ \
2518c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index; \
2528c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev); \
2538c2ecf20Sopenharmony_ci	struct asb100_data *data = i2c_get_clientdata(client); \
2548c2ecf20Sopenharmony_ci	unsigned long val; \
2558c2ecf20Sopenharmony_ci	int err = kstrtoul(buf, 10, &val); \
2568c2ecf20Sopenharmony_ci	if (err) \
2578c2ecf20Sopenharmony_ci		return err; \
2588c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock); \
2598c2ecf20Sopenharmony_ci	data->in_##reg[nr] = IN_TO_REG(val); \
2608c2ecf20Sopenharmony_ci	asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
2618c2ecf20Sopenharmony_ci		data->in_##reg[nr]); \
2628c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock); \
2638c2ecf20Sopenharmony_ci	return count; \
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ciset_in_reg(MIN, min)
2678c2ecf20Sopenharmony_ciset_in_reg(MAX, max)
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci#define sysfs_in(offset) \
2708c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
2718c2ecf20Sopenharmony_ci		show_in, NULL, offset); \
2728c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
2738c2ecf20Sopenharmony_ci		show_in_min, set_in_min, offset); \
2748c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
2758c2ecf20Sopenharmony_ci		show_in_max, set_in_max, offset)
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cisysfs_in(0);
2788c2ecf20Sopenharmony_cisysfs_in(1);
2798c2ecf20Sopenharmony_cisysfs_in(2);
2808c2ecf20Sopenharmony_cisysfs_in(3);
2818c2ecf20Sopenharmony_cisysfs_in(4);
2828c2ecf20Sopenharmony_cisysfs_in(5);
2838c2ecf20Sopenharmony_cisysfs_in(6);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci/* 3 Fans */
2868c2ecf20Sopenharmony_cistatic ssize_t show_fan(struct device *dev, struct device_attribute *attr,
2878c2ecf20Sopenharmony_ci		char *buf)
2888c2ecf20Sopenharmony_ci{
2898c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
2908c2ecf20Sopenharmony_ci	struct asb100_data *data = asb100_update_device(dev);
2918c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
2928c2ecf20Sopenharmony_ci		DIV_FROM_REG(data->fan_div[nr])));
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_cistatic ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
2968c2ecf20Sopenharmony_ci		char *buf)
2978c2ecf20Sopenharmony_ci{
2988c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
2998c2ecf20Sopenharmony_ci	struct asb100_data *data = asb100_update_device(dev);
3008c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
3018c2ecf20Sopenharmony_ci		DIV_FROM_REG(data->fan_div[nr])));
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
3058c2ecf20Sopenharmony_ci		char *buf)
3068c2ecf20Sopenharmony_ci{
3078c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
3088c2ecf20Sopenharmony_ci	struct asb100_data *data = asb100_update_device(dev);
3098c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
3108c2ecf20Sopenharmony_ci}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_cistatic ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
3138c2ecf20Sopenharmony_ci		const char *buf, size_t count)
3148c2ecf20Sopenharmony_ci{
3158c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
3168c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
3178c2ecf20Sopenharmony_ci	struct asb100_data *data = i2c_get_clientdata(client);
3188c2ecf20Sopenharmony_ci	unsigned long val;
3198c2ecf20Sopenharmony_ci	int err;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
3228c2ecf20Sopenharmony_ci	if (err)
3238c2ecf20Sopenharmony_ci		return err;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3268c2ecf20Sopenharmony_ci	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
3278c2ecf20Sopenharmony_ci	asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
3288c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3298c2ecf20Sopenharmony_ci	return count;
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci/*
3338c2ecf20Sopenharmony_ci * Note: we save and restore the fan minimum here, because its value is
3348c2ecf20Sopenharmony_ci * determined in part by the fan divisor.  This follows the principle of
3358c2ecf20Sopenharmony_ci * least surprise; the user doesn't expect the fan minimum to change just
3368c2ecf20Sopenharmony_ci * because the divisor changed.
3378c2ecf20Sopenharmony_ci */
3388c2ecf20Sopenharmony_cistatic ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
3398c2ecf20Sopenharmony_ci		const char *buf, size_t count)
3408c2ecf20Sopenharmony_ci{
3418c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index;
3428c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
3438c2ecf20Sopenharmony_ci	struct asb100_data *data = i2c_get_clientdata(client);
3448c2ecf20Sopenharmony_ci	unsigned long min;
3458c2ecf20Sopenharmony_ci	int reg;
3468c2ecf20Sopenharmony_ci	unsigned long val;
3478c2ecf20Sopenharmony_ci	int err;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
3508c2ecf20Sopenharmony_ci	if (err)
3518c2ecf20Sopenharmony_ci		return err;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	min = FAN_FROM_REG(data->fan_min[nr],
3568c2ecf20Sopenharmony_ci			DIV_FROM_REG(data->fan_div[nr]));
3578c2ecf20Sopenharmony_ci	data->fan_div[nr] = DIV_TO_REG(val);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	switch (nr) {
3608c2ecf20Sopenharmony_ci	case 0:	/* fan 1 */
3618c2ecf20Sopenharmony_ci		reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
3628c2ecf20Sopenharmony_ci		reg = (reg & 0xcf) | (data->fan_div[0] << 4);
3638c2ecf20Sopenharmony_ci		asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
3648c2ecf20Sopenharmony_ci		break;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	case 1:	/* fan 2 */
3678c2ecf20Sopenharmony_ci		reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
3688c2ecf20Sopenharmony_ci		reg = (reg & 0x3f) | (data->fan_div[1] << 6);
3698c2ecf20Sopenharmony_ci		asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
3708c2ecf20Sopenharmony_ci		break;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	case 2:	/* fan 3 */
3738c2ecf20Sopenharmony_ci		reg = asb100_read_value(client, ASB100_REG_PIN);
3748c2ecf20Sopenharmony_ci		reg = (reg & 0x3f) | (data->fan_div[2] << 6);
3758c2ecf20Sopenharmony_ci		asb100_write_value(client, ASB100_REG_PIN, reg);
3768c2ecf20Sopenharmony_ci		break;
3778c2ecf20Sopenharmony_ci	}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	data->fan_min[nr] =
3808c2ecf20Sopenharmony_ci		FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
3818c2ecf20Sopenharmony_ci	asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	return count;
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci#define sysfs_fan(offset) \
3898c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
3908c2ecf20Sopenharmony_ci		show_fan, NULL, offset - 1); \
3918c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
3928c2ecf20Sopenharmony_ci		show_fan_min, set_fan_min, offset - 1); \
3938c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
3948c2ecf20Sopenharmony_ci		show_fan_div, set_fan_div, offset - 1)
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_cisysfs_fan(1);
3978c2ecf20Sopenharmony_cisysfs_fan(2);
3988c2ecf20Sopenharmony_cisysfs_fan(3);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci/* 4 Temp. Sensors */
4018c2ecf20Sopenharmony_cistatic int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	int ret = 0;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	switch (nr) {
4068c2ecf20Sopenharmony_ci	case 1: case 2:
4078c2ecf20Sopenharmony_ci		ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
4088c2ecf20Sopenharmony_ci		break;
4098c2ecf20Sopenharmony_ci	case 0: case 3: default:
4108c2ecf20Sopenharmony_ci		ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
4118c2ecf20Sopenharmony_ci		break;
4128c2ecf20Sopenharmony_ci	}
4138c2ecf20Sopenharmony_ci	return ret;
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci#define show_temp_reg(reg) \
4178c2ecf20Sopenharmony_cistatic ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
4188c2ecf20Sopenharmony_ci		char *buf) \
4198c2ecf20Sopenharmony_ci{ \
4208c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index; \
4218c2ecf20Sopenharmony_ci	struct asb100_data *data = asb100_update_device(dev); \
4228c2ecf20Sopenharmony_ci	return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
4238c2ecf20Sopenharmony_ci}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_cishow_temp_reg(temp);
4268c2ecf20Sopenharmony_cishow_temp_reg(temp_max);
4278c2ecf20Sopenharmony_cishow_temp_reg(temp_hyst);
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci#define set_temp_reg(REG, reg) \
4308c2ecf20Sopenharmony_cistatic ssize_t set_##reg(struct device *dev, struct device_attribute *attr, \
4318c2ecf20Sopenharmony_ci		const char *buf, size_t count) \
4328c2ecf20Sopenharmony_ci{ \
4338c2ecf20Sopenharmony_ci	int nr = to_sensor_dev_attr(attr)->index; \
4348c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev); \
4358c2ecf20Sopenharmony_ci	struct asb100_data *data = i2c_get_clientdata(client); \
4368c2ecf20Sopenharmony_ci	long val; \
4378c2ecf20Sopenharmony_ci	int err = kstrtol(buf, 10, &val); \
4388c2ecf20Sopenharmony_ci	if (err) \
4398c2ecf20Sopenharmony_ci		return err; \
4408c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock); \
4418c2ecf20Sopenharmony_ci	switch (nr) { \
4428c2ecf20Sopenharmony_ci	case 1: case 2: \
4438c2ecf20Sopenharmony_ci		data->reg[nr] = LM75_TEMP_TO_REG(val); \
4448c2ecf20Sopenharmony_ci		break; \
4458c2ecf20Sopenharmony_ci	case 0: case 3: default: \
4468c2ecf20Sopenharmony_ci		data->reg[nr] = TEMP_TO_REG(val); \
4478c2ecf20Sopenharmony_ci		break; \
4488c2ecf20Sopenharmony_ci	} \
4498c2ecf20Sopenharmony_ci	asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
4508c2ecf20Sopenharmony_ci			data->reg[nr]); \
4518c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock); \
4528c2ecf20Sopenharmony_ci	return count; \
4538c2ecf20Sopenharmony_ci}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ciset_temp_reg(MAX, temp_max);
4568c2ecf20Sopenharmony_ciset_temp_reg(HYST, temp_hyst);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci#define sysfs_temp(num) \
4598c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp##num##_input, S_IRUGO, \
4608c2ecf20Sopenharmony_ci		show_temp, NULL, num - 1); \
4618c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
4628c2ecf20Sopenharmony_ci		show_temp_max, set_temp_max, num - 1); \
4638c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
4648c2ecf20Sopenharmony_ci		show_temp_hyst, set_temp_hyst, num - 1)
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cisysfs_temp(1);
4678c2ecf20Sopenharmony_cisysfs_temp(2);
4688c2ecf20Sopenharmony_cisysfs_temp(3);
4698c2ecf20Sopenharmony_cisysfs_temp(4);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci/* VID */
4728c2ecf20Sopenharmony_cistatic ssize_t cpu0_vid_show(struct device *dev,
4738c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
4748c2ecf20Sopenharmony_ci{
4758c2ecf20Sopenharmony_ci	struct asb100_data *data = asb100_update_device(dev);
4768c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
4778c2ecf20Sopenharmony_ci}
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(cpu0_vid);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci/* VRM */
4828c2ecf20Sopenharmony_cistatic ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
4838c2ecf20Sopenharmony_ci		char *buf)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	struct asb100_data *data = dev_get_drvdata(dev);
4868c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", data->vrm);
4878c2ecf20Sopenharmony_ci}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_cistatic ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
4908c2ecf20Sopenharmony_ci			 const char *buf, size_t count)
4918c2ecf20Sopenharmony_ci{
4928c2ecf20Sopenharmony_ci	struct asb100_data *data = dev_get_drvdata(dev);
4938c2ecf20Sopenharmony_ci	unsigned long val;
4948c2ecf20Sopenharmony_ci	int err;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
4978c2ecf20Sopenharmony_ci	if (err)
4988c2ecf20Sopenharmony_ci		return err;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	if (val > 255)
5018c2ecf20Sopenharmony_ci		return -EINVAL;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	data->vrm = val;
5048c2ecf20Sopenharmony_ci	return count;
5058c2ecf20Sopenharmony_ci}
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci/* Alarms */
5088c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(vrm);
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_cistatic ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
5118c2ecf20Sopenharmony_ci		char *buf)
5128c2ecf20Sopenharmony_ci{
5138c2ecf20Sopenharmony_ci	struct asb100_data *data = asb100_update_device(dev);
5148c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", data->alarms);
5158c2ecf20Sopenharmony_ci}
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(alarms);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cistatic ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
5208c2ecf20Sopenharmony_ci		char *buf)
5218c2ecf20Sopenharmony_ci{
5228c2ecf20Sopenharmony_ci	int bitnr = to_sensor_dev_attr(attr)->index;
5238c2ecf20Sopenharmony_ci	struct asb100_data *data = asb100_update_device(dev);
5248c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
5278c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
5288c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
5298c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
5308c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
5318c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
5328c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
5338c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
5348c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
5358c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
5368c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci/* 1 PWM */
5398c2ecf20Sopenharmony_cistatic ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
5408c2ecf20Sopenharmony_ci		char *buf)
5418c2ecf20Sopenharmony_ci{
5428c2ecf20Sopenharmony_ci	struct asb100_data *data = asb100_update_device(dev);
5438c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
5448c2ecf20Sopenharmony_ci}
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_cistatic ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
5478c2ecf20Sopenharmony_ci			  const char *buf, size_t count)
5488c2ecf20Sopenharmony_ci{
5498c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
5508c2ecf20Sopenharmony_ci	struct asb100_data *data = i2c_get_clientdata(client);
5518c2ecf20Sopenharmony_ci	unsigned long val;
5528c2ecf20Sopenharmony_ci	int err;
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
5558c2ecf20Sopenharmony_ci	if (err)
5568c2ecf20Sopenharmony_ci		return err;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
5598c2ecf20Sopenharmony_ci	data->pwm &= 0x80; /* keep the enable bit */
5608c2ecf20Sopenharmony_ci	data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
5618c2ecf20Sopenharmony_ci	asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
5628c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
5638c2ecf20Sopenharmony_ci	return count;
5648c2ecf20Sopenharmony_ci}
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_cistatic ssize_t pwm1_enable_show(struct device *dev,
5678c2ecf20Sopenharmony_ci		struct device_attribute *attr, char *buf)
5688c2ecf20Sopenharmony_ci{
5698c2ecf20Sopenharmony_ci	struct asb100_data *data = asb100_update_device(dev);
5708c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
5718c2ecf20Sopenharmony_ci}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_cistatic ssize_t pwm1_enable_store(struct device *dev,
5748c2ecf20Sopenharmony_ci				 struct device_attribute *attr,
5758c2ecf20Sopenharmony_ci				 const char *buf, size_t count)
5768c2ecf20Sopenharmony_ci{
5778c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
5788c2ecf20Sopenharmony_ci	struct asb100_data *data = i2c_get_clientdata(client);
5798c2ecf20Sopenharmony_ci	unsigned long val;
5808c2ecf20Sopenharmony_ci	int err;
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	err = kstrtoul(buf, 10, &val);
5838c2ecf20Sopenharmony_ci	if (err)
5848c2ecf20Sopenharmony_ci		return err;
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
5878c2ecf20Sopenharmony_ci	data->pwm &= 0x0f; /* keep the duty cycle bits */
5888c2ecf20Sopenharmony_ci	data->pwm |= (val ? 0x80 : 0x00);
5898c2ecf20Sopenharmony_ci	asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
5908c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
5918c2ecf20Sopenharmony_ci	return count;
5928c2ecf20Sopenharmony_ci}
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(pwm1);
5958c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(pwm1_enable);
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_cistatic struct attribute *asb100_attributes[] = {
5988c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_input.dev_attr.attr,
5998c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_min.dev_attr.attr,
6008c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_max.dev_attr.attr,
6018c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_input.dev_attr.attr,
6028c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_min.dev_attr.attr,
6038c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_max.dev_attr.attr,
6048c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_input.dev_attr.attr,
6058c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_min.dev_attr.attr,
6068c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_max.dev_attr.attr,
6078c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_input.dev_attr.attr,
6088c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_min.dev_attr.attr,
6098c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_max.dev_attr.attr,
6108c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_input.dev_attr.attr,
6118c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_min.dev_attr.attr,
6128c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_max.dev_attr.attr,
6138c2ecf20Sopenharmony_ci	&sensor_dev_attr_in5_input.dev_attr.attr,
6148c2ecf20Sopenharmony_ci	&sensor_dev_attr_in5_min.dev_attr.attr,
6158c2ecf20Sopenharmony_ci	&sensor_dev_attr_in5_max.dev_attr.attr,
6168c2ecf20Sopenharmony_ci	&sensor_dev_attr_in6_input.dev_attr.attr,
6178c2ecf20Sopenharmony_ci	&sensor_dev_attr_in6_min.dev_attr.attr,
6188c2ecf20Sopenharmony_ci	&sensor_dev_attr_in6_max.dev_attr.attr,
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_input.dev_attr.attr,
6218c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_min.dev_attr.attr,
6228c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_div.dev_attr.attr,
6238c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_input.dev_attr.attr,
6248c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_min.dev_attr.attr,
6258c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_div.dev_attr.attr,
6268c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan3_input.dev_attr.attr,
6278c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan3_min.dev_attr.attr,
6288c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan3_div.dev_attr.attr,
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_input.dev_attr.attr,
6318c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max.dev_attr.attr,
6328c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
6338c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_input.dev_attr.attr,
6348c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max.dev_attr.attr,
6358c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
6368c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_input.dev_attr.attr,
6378c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_max.dev_attr.attr,
6388c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
6398c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_input.dev_attr.attr,
6408c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_max.dev_attr.attr,
6418c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	&sensor_dev_attr_in0_alarm.dev_attr.attr,
6448c2ecf20Sopenharmony_ci	&sensor_dev_attr_in1_alarm.dev_attr.attr,
6458c2ecf20Sopenharmony_ci	&sensor_dev_attr_in2_alarm.dev_attr.attr,
6468c2ecf20Sopenharmony_ci	&sensor_dev_attr_in3_alarm.dev_attr.attr,
6478c2ecf20Sopenharmony_ci	&sensor_dev_attr_in4_alarm.dev_attr.attr,
6488c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
6498c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
6508c2ecf20Sopenharmony_ci	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
6518c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
6528c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
6538c2ecf20Sopenharmony_ci	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	&dev_attr_cpu0_vid.attr,
6568c2ecf20Sopenharmony_ci	&dev_attr_vrm.attr,
6578c2ecf20Sopenharmony_ci	&dev_attr_alarms.attr,
6588c2ecf20Sopenharmony_ci	&dev_attr_pwm1.attr,
6598c2ecf20Sopenharmony_ci	&dev_attr_pwm1_enable.attr,
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	NULL
6628c2ecf20Sopenharmony_ci};
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_cistatic const struct attribute_group asb100_group = {
6658c2ecf20Sopenharmony_ci	.attrs = asb100_attributes,
6668c2ecf20Sopenharmony_ci};
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_cistatic int asb100_detect_subclients(struct i2c_client *client)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	int i, id, err;
6718c2ecf20Sopenharmony_ci	int address = client->addr;
6728c2ecf20Sopenharmony_ci	unsigned short sc_addr[2];
6738c2ecf20Sopenharmony_ci	struct asb100_data *data = i2c_get_clientdata(client);
6748c2ecf20Sopenharmony_ci	struct i2c_adapter *adapter = client->adapter;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	id = i2c_adapter_id(adapter);
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	if (force_subclients[0] == id && force_subclients[1] == address) {
6798c2ecf20Sopenharmony_ci		for (i = 2; i <= 3; i++) {
6808c2ecf20Sopenharmony_ci			if (force_subclients[i] < 0x48 ||
6818c2ecf20Sopenharmony_ci			    force_subclients[i] > 0x4f) {
6828c2ecf20Sopenharmony_ci				dev_err(&client->dev,
6838c2ecf20Sopenharmony_ci					"invalid subclient address %d; must be 0x48-0x4f\n",
6848c2ecf20Sopenharmony_ci					force_subclients[i]);
6858c2ecf20Sopenharmony_ci				err = -ENODEV;
6868c2ecf20Sopenharmony_ci				goto ERROR_SC_2;
6878c2ecf20Sopenharmony_ci			}
6888c2ecf20Sopenharmony_ci		}
6898c2ecf20Sopenharmony_ci		asb100_write_value(client, ASB100_REG_I2C_SUBADDR,
6908c2ecf20Sopenharmony_ci					(force_subclients[2] & 0x07) |
6918c2ecf20Sopenharmony_ci					((force_subclients[3] & 0x07) << 4));
6928c2ecf20Sopenharmony_ci		sc_addr[0] = force_subclients[2];
6938c2ecf20Sopenharmony_ci		sc_addr[1] = force_subclients[3];
6948c2ecf20Sopenharmony_ci	} else {
6958c2ecf20Sopenharmony_ci		int val = asb100_read_value(client, ASB100_REG_I2C_SUBADDR);
6968c2ecf20Sopenharmony_ci		sc_addr[0] = 0x48 + (val & 0x07);
6978c2ecf20Sopenharmony_ci		sc_addr[1] = 0x48 + ((val >> 4) & 0x07);
6988c2ecf20Sopenharmony_ci	}
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	if (sc_addr[0] == sc_addr[1]) {
7018c2ecf20Sopenharmony_ci		dev_err(&client->dev,
7028c2ecf20Sopenharmony_ci			"duplicate addresses 0x%x for subclients\n",
7038c2ecf20Sopenharmony_ci			sc_addr[0]);
7048c2ecf20Sopenharmony_ci		err = -ENODEV;
7058c2ecf20Sopenharmony_ci		goto ERROR_SC_2;
7068c2ecf20Sopenharmony_ci	}
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	data->lm75[0] = i2c_new_dummy_device(adapter, sc_addr[0]);
7098c2ecf20Sopenharmony_ci	if (IS_ERR(data->lm75[0])) {
7108c2ecf20Sopenharmony_ci		dev_err(&client->dev,
7118c2ecf20Sopenharmony_ci			"subclient %d registration at address 0x%x failed.\n",
7128c2ecf20Sopenharmony_ci			1, sc_addr[0]);
7138c2ecf20Sopenharmony_ci		err = PTR_ERR(data->lm75[0]);
7148c2ecf20Sopenharmony_ci		goto ERROR_SC_2;
7158c2ecf20Sopenharmony_ci	}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	data->lm75[1] = i2c_new_dummy_device(adapter, sc_addr[1]);
7188c2ecf20Sopenharmony_ci	if (IS_ERR(data->lm75[1])) {
7198c2ecf20Sopenharmony_ci		dev_err(&client->dev,
7208c2ecf20Sopenharmony_ci			"subclient %d registration at address 0x%x failed.\n",
7218c2ecf20Sopenharmony_ci			2, sc_addr[1]);
7228c2ecf20Sopenharmony_ci		err = PTR_ERR(data->lm75[1]);
7238c2ecf20Sopenharmony_ci		goto ERROR_SC_3;
7248c2ecf20Sopenharmony_ci	}
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	return 0;
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci/* Undo inits in case of errors */
7298c2ecf20Sopenharmony_ciERROR_SC_3:
7308c2ecf20Sopenharmony_ci	i2c_unregister_device(data->lm75[0]);
7318c2ecf20Sopenharmony_ciERROR_SC_2:
7328c2ecf20Sopenharmony_ci	return err;
7338c2ecf20Sopenharmony_ci}
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci/* Return 0 if detection is successful, -ENODEV otherwise */
7368c2ecf20Sopenharmony_cistatic int asb100_detect(struct i2c_client *client,
7378c2ecf20Sopenharmony_ci			 struct i2c_board_info *info)
7388c2ecf20Sopenharmony_ci{
7398c2ecf20Sopenharmony_ci	struct i2c_adapter *adapter = client->adapter;
7408c2ecf20Sopenharmony_ci	int val1, val2;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
7438c2ecf20Sopenharmony_ci		pr_debug("detect failed, smbus byte data not supported!\n");
7448c2ecf20Sopenharmony_ci		return -ENODEV;
7458c2ecf20Sopenharmony_ci	}
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	val1 = i2c_smbus_read_byte_data(client, ASB100_REG_BANK);
7488c2ecf20Sopenharmony_ci	val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN);
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	/* If we're in bank 0 */
7518c2ecf20Sopenharmony_ci	if ((!(val1 & 0x07)) &&
7528c2ecf20Sopenharmony_ci			/* Check for ASB100 ID (low byte) */
7538c2ecf20Sopenharmony_ci			(((!(val1 & 0x80)) && (val2 != 0x94)) ||
7548c2ecf20Sopenharmony_ci			/* Check for ASB100 ID (high byte ) */
7558c2ecf20Sopenharmony_ci			((val1 & 0x80) && (val2 != 0x06)))) {
7568c2ecf20Sopenharmony_ci		pr_debug("detect failed, bad chip id 0x%02x!\n", val2);
7578c2ecf20Sopenharmony_ci		return -ENODEV;
7588c2ecf20Sopenharmony_ci	}
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	/* Put it now into bank 0 and Vendor ID High Byte */
7618c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, ASB100_REG_BANK,
7628c2ecf20Sopenharmony_ci		(i2c_smbus_read_byte_data(client, ASB100_REG_BANK) & 0x78)
7638c2ecf20Sopenharmony_ci		| 0x80);
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	/* Determine the chip type. */
7668c2ecf20Sopenharmony_ci	val1 = i2c_smbus_read_byte_data(client, ASB100_REG_WCHIPID);
7678c2ecf20Sopenharmony_ci	val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN);
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	if (val1 != 0x31 || val2 != 0x06)
7708c2ecf20Sopenharmony_ci		return -ENODEV;
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	strlcpy(info->type, "asb100", I2C_NAME_SIZE);
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	return 0;
7758c2ecf20Sopenharmony_ci}
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_cistatic int asb100_probe(struct i2c_client *client)
7788c2ecf20Sopenharmony_ci{
7798c2ecf20Sopenharmony_ci	int err;
7808c2ecf20Sopenharmony_ci	struct asb100_data *data;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	data = devm_kzalloc(&client->dev, sizeof(struct asb100_data),
7838c2ecf20Sopenharmony_ci			    GFP_KERNEL);
7848c2ecf20Sopenharmony_ci	if (!data)
7858c2ecf20Sopenharmony_ci		return -ENOMEM;
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, data);
7888c2ecf20Sopenharmony_ci	mutex_init(&data->lock);
7898c2ecf20Sopenharmony_ci	mutex_init(&data->update_lock);
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	/* Attach secondary lm75 clients */
7928c2ecf20Sopenharmony_ci	err = asb100_detect_subclients(client);
7938c2ecf20Sopenharmony_ci	if (err)
7948c2ecf20Sopenharmony_ci		return err;
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	/* Initialize the chip */
7978c2ecf20Sopenharmony_ci	asb100_init_client(client);
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	/* A few vars need to be filled upon startup */
8008c2ecf20Sopenharmony_ci	data->fan_min[0] = asb100_read_value(client, ASB100_REG_FAN_MIN(0));
8018c2ecf20Sopenharmony_ci	data->fan_min[1] = asb100_read_value(client, ASB100_REG_FAN_MIN(1));
8028c2ecf20Sopenharmony_ci	data->fan_min[2] = asb100_read_value(client, ASB100_REG_FAN_MIN(2));
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	/* Register sysfs hooks */
8058c2ecf20Sopenharmony_ci	err = sysfs_create_group(&client->dev.kobj, &asb100_group);
8068c2ecf20Sopenharmony_ci	if (err)
8078c2ecf20Sopenharmony_ci		goto ERROR3;
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	data->hwmon_dev = hwmon_device_register(&client->dev);
8108c2ecf20Sopenharmony_ci	if (IS_ERR(data->hwmon_dev)) {
8118c2ecf20Sopenharmony_ci		err = PTR_ERR(data->hwmon_dev);
8128c2ecf20Sopenharmony_ci		goto ERROR4;
8138c2ecf20Sopenharmony_ci	}
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	return 0;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ciERROR4:
8188c2ecf20Sopenharmony_ci	sysfs_remove_group(&client->dev.kobj, &asb100_group);
8198c2ecf20Sopenharmony_ciERROR3:
8208c2ecf20Sopenharmony_ci	i2c_unregister_device(data->lm75[1]);
8218c2ecf20Sopenharmony_ci	i2c_unregister_device(data->lm75[0]);
8228c2ecf20Sopenharmony_ci	return err;
8238c2ecf20Sopenharmony_ci}
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_cistatic int asb100_remove(struct i2c_client *client)
8268c2ecf20Sopenharmony_ci{
8278c2ecf20Sopenharmony_ci	struct asb100_data *data = i2c_get_clientdata(client);
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	hwmon_device_unregister(data->hwmon_dev);
8308c2ecf20Sopenharmony_ci	sysfs_remove_group(&client->dev.kobj, &asb100_group);
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	i2c_unregister_device(data->lm75[1]);
8338c2ecf20Sopenharmony_ci	i2c_unregister_device(data->lm75[0]);
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	return 0;
8368c2ecf20Sopenharmony_ci}
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci/*
8398c2ecf20Sopenharmony_ci * The SMBus locks itself, usually, but nothing may access the chip between
8408c2ecf20Sopenharmony_ci * bank switches.
8418c2ecf20Sopenharmony_ci */
8428c2ecf20Sopenharmony_cistatic int asb100_read_value(struct i2c_client *client, u16 reg)
8438c2ecf20Sopenharmony_ci{
8448c2ecf20Sopenharmony_ci	struct asb100_data *data = i2c_get_clientdata(client);
8458c2ecf20Sopenharmony_ci	struct i2c_client *cl;
8468c2ecf20Sopenharmony_ci	int res, bank;
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	mutex_lock(&data->lock);
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	bank = (reg >> 8) & 0x0f;
8518c2ecf20Sopenharmony_ci	if (bank > 2)
8528c2ecf20Sopenharmony_ci		/* switch banks */
8538c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	if (bank == 0 || bank > 2) {
8568c2ecf20Sopenharmony_ci		res = i2c_smbus_read_byte_data(client, reg & 0xff);
8578c2ecf20Sopenharmony_ci	} else {
8588c2ecf20Sopenharmony_ci		/* switch to subclient */
8598c2ecf20Sopenharmony_ci		cl = data->lm75[bank - 1];
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci		/* convert from ISA to LM75 I2C addresses */
8628c2ecf20Sopenharmony_ci		switch (reg & 0xff) {
8638c2ecf20Sopenharmony_ci		case 0x50: /* TEMP */
8648c2ecf20Sopenharmony_ci			res = i2c_smbus_read_word_swapped(cl, 0);
8658c2ecf20Sopenharmony_ci			break;
8668c2ecf20Sopenharmony_ci		case 0x52: /* CONFIG */
8678c2ecf20Sopenharmony_ci			res = i2c_smbus_read_byte_data(cl, 1);
8688c2ecf20Sopenharmony_ci			break;
8698c2ecf20Sopenharmony_ci		case 0x53: /* HYST */
8708c2ecf20Sopenharmony_ci			res = i2c_smbus_read_word_swapped(cl, 2);
8718c2ecf20Sopenharmony_ci			break;
8728c2ecf20Sopenharmony_ci		case 0x55: /* MAX */
8738c2ecf20Sopenharmony_ci		default:
8748c2ecf20Sopenharmony_ci			res = i2c_smbus_read_word_swapped(cl, 3);
8758c2ecf20Sopenharmony_ci			break;
8768c2ecf20Sopenharmony_ci		}
8778c2ecf20Sopenharmony_ci	}
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	if (bank > 2)
8808c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	mutex_unlock(&data->lock);
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	return res;
8858c2ecf20Sopenharmony_ci}
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_cistatic void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
8888c2ecf20Sopenharmony_ci{
8898c2ecf20Sopenharmony_ci	struct asb100_data *data = i2c_get_clientdata(client);
8908c2ecf20Sopenharmony_ci	struct i2c_client *cl;
8918c2ecf20Sopenharmony_ci	int bank;
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	mutex_lock(&data->lock);
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	bank = (reg >> 8) & 0x0f;
8968c2ecf20Sopenharmony_ci	if (bank > 2)
8978c2ecf20Sopenharmony_ci		/* switch banks */
8988c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	if (bank == 0 || bank > 2) {
9018c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
9028c2ecf20Sopenharmony_ci	} else {
9038c2ecf20Sopenharmony_ci		/* switch to subclient */
9048c2ecf20Sopenharmony_ci		cl = data->lm75[bank - 1];
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci		/* convert from ISA to LM75 I2C addresses */
9078c2ecf20Sopenharmony_ci		switch (reg & 0xff) {
9088c2ecf20Sopenharmony_ci		case 0x52: /* CONFIG */
9098c2ecf20Sopenharmony_ci			i2c_smbus_write_byte_data(cl, 1, value & 0xff);
9108c2ecf20Sopenharmony_ci			break;
9118c2ecf20Sopenharmony_ci		case 0x53: /* HYST */
9128c2ecf20Sopenharmony_ci			i2c_smbus_write_word_swapped(cl, 2, value);
9138c2ecf20Sopenharmony_ci			break;
9148c2ecf20Sopenharmony_ci		case 0x55: /* MAX */
9158c2ecf20Sopenharmony_ci			i2c_smbus_write_word_swapped(cl, 3, value);
9168c2ecf20Sopenharmony_ci			break;
9178c2ecf20Sopenharmony_ci		}
9188c2ecf20Sopenharmony_ci	}
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	if (bank > 2)
9218c2ecf20Sopenharmony_ci		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	mutex_unlock(&data->lock);
9248c2ecf20Sopenharmony_ci}
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_cistatic void asb100_init_client(struct i2c_client *client)
9278c2ecf20Sopenharmony_ci{
9288c2ecf20Sopenharmony_ci	struct asb100_data *data = i2c_get_clientdata(client);
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci	data->vrm = vid_which_vrm();
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	/* Start monitoring */
9338c2ecf20Sopenharmony_ci	asb100_write_value(client, ASB100_REG_CONFIG,
9348c2ecf20Sopenharmony_ci		(asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
9358c2ecf20Sopenharmony_ci}
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_cistatic struct asb100_data *asb100_update_device(struct device *dev)
9388c2ecf20Sopenharmony_ci{
9398c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
9408c2ecf20Sopenharmony_ci	struct asb100_data *data = i2c_get_clientdata(client);
9418c2ecf20Sopenharmony_ci	int i;
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
9468c2ecf20Sopenharmony_ci		|| !data->valid) {
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "starting device update...\n");
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci		/* 7 voltage inputs */
9518c2ecf20Sopenharmony_ci		for (i = 0; i < 7; i++) {
9528c2ecf20Sopenharmony_ci			data->in[i] = asb100_read_value(client,
9538c2ecf20Sopenharmony_ci				ASB100_REG_IN(i));
9548c2ecf20Sopenharmony_ci			data->in_min[i] = asb100_read_value(client,
9558c2ecf20Sopenharmony_ci				ASB100_REG_IN_MIN(i));
9568c2ecf20Sopenharmony_ci			data->in_max[i] = asb100_read_value(client,
9578c2ecf20Sopenharmony_ci				ASB100_REG_IN_MAX(i));
9588c2ecf20Sopenharmony_ci		}
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci		/* 3 fan inputs */
9618c2ecf20Sopenharmony_ci		for (i = 0; i < 3; i++) {
9628c2ecf20Sopenharmony_ci			data->fan[i] = asb100_read_value(client,
9638c2ecf20Sopenharmony_ci					ASB100_REG_FAN(i));
9648c2ecf20Sopenharmony_ci			data->fan_min[i] = asb100_read_value(client,
9658c2ecf20Sopenharmony_ci					ASB100_REG_FAN_MIN(i));
9668c2ecf20Sopenharmony_ci		}
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci		/* 4 temperature inputs */
9698c2ecf20Sopenharmony_ci		for (i = 1; i <= 4; i++) {
9708c2ecf20Sopenharmony_ci			data->temp[i-1] = asb100_read_value(client,
9718c2ecf20Sopenharmony_ci					ASB100_REG_TEMP(i));
9728c2ecf20Sopenharmony_ci			data->temp_max[i-1] = asb100_read_value(client,
9738c2ecf20Sopenharmony_ci					ASB100_REG_TEMP_MAX(i));
9748c2ecf20Sopenharmony_ci			data->temp_hyst[i-1] = asb100_read_value(client,
9758c2ecf20Sopenharmony_ci					ASB100_REG_TEMP_HYST(i));
9768c2ecf20Sopenharmony_ci		}
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci		/* VID and fan divisors */
9798c2ecf20Sopenharmony_ci		i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
9808c2ecf20Sopenharmony_ci		data->vid = i & 0x0f;
9818c2ecf20Sopenharmony_ci		data->vid |= (asb100_read_value(client,
9828c2ecf20Sopenharmony_ci				ASB100_REG_CHIPID) & 0x01) << 4;
9838c2ecf20Sopenharmony_ci		data->fan_div[0] = (i >> 4) & 0x03;
9848c2ecf20Sopenharmony_ci		data->fan_div[1] = (i >> 6) & 0x03;
9858c2ecf20Sopenharmony_ci		data->fan_div[2] = (asb100_read_value(client,
9868c2ecf20Sopenharmony_ci				ASB100_REG_PIN) >> 6) & 0x03;
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci		/* PWM */
9898c2ecf20Sopenharmony_ci		data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci		/* alarms */
9928c2ecf20Sopenharmony_ci		data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
9938c2ecf20Sopenharmony_ci			(asb100_read_value(client, ASB100_REG_ALARM2) << 8);
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci		data->last_updated = jiffies;
9968c2ecf20Sopenharmony_ci		data->valid = 1;
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "... device update complete\n");
9998c2ecf20Sopenharmony_ci	}
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	return data;
10048c2ecf20Sopenharmony_ci}
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_cimodule_i2c_driver(asb100_driver);
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ciMODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
10098c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ASB100 Bach driver");
10108c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1011