// SPDX-License-Identifier: GPL-2.0-or-later
/*
* asb100.c - Part of lm_sensors, Linux kernel modules for hardware
* monitoring
*
* Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
*
* (derived from w83781d.c)
*
* Copyright (C) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
* Philip Edelbrock <phil@netroedge.com>, and
* Mark Studebaker <mdsxyz123@yahoo.com>
*/
/*
* This driver supports the hardware sensor chips: Asus ASB100 and
* ASB100-A "BACH".
*
* ASB100-A supports pwm1, while plain ASB100 does not. There is no known
* way for the driver to tell which one is there.
*
* Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
* asb100 7 3 1 4 0x31 0x0694 yes no
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/hwmon-vid.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/mutex.h>
#include "lm75.h"
/* I2C addresses to scan */
static const unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
static unsigned short force_subclients[4];
module_param_array(force_subclients, short, NULL, 0);
MODULE_PARM_DESC(force_subclients,
"List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
/* Voltage IN registers 0-6 */
#define ASB100_REG_IN(nr) (0x20 + (nr))
#define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2))
#define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2))
/* FAN IN registers 1-3 */
#define ASB100_REG_FAN(nr) (0x28 + (nr))
#define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
/* TEMPERATURE registers 1-4 */
static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17};
static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18};
static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};
#define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
#define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
#define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
#define ASB100_REG_TEMP2_CONFIG 0x0152
#define ASB100_REG_TEMP3_CONFIG 0x0252
#define ASB100_REG_CONFIG 0x40
#define ASB100_REG_ALARM1 0x41
#define ASB100_REG_ALARM2 0x42
#define ASB100_REG_SMIM1 0x43
#define ASB100_REG_SMIM2 0x44
#define ASB100_REG_VID_FANDIV 0x47
#define ASB100_REG_I2C_ADDR 0x48
#define ASB100_REG_CHIPID 0x49
#define ASB100_REG_I2C_SUBADDR 0x4a
#define ASB100_REG_PIN 0x4b
#define ASB100_REG_IRQ 0x4c
#define ASB100_REG_BANK 0x4e
#define ASB100_REG_CHIPMAN 0x4f
#define ASB100_REG_WCHIPID 0x58
/* bit 7 -> enable, bits 0-3 -> duty cycle */
#define ASB100_REG_PWM1 0x59
/*
* CONVERSIONS
* Rounding and limit checking is only done on the TO_REG variants.
*/
/* These constants are a guess, consistent w/ w83781d */
#define ASB100_IN_MIN 0
#define ASB100_IN_MAX 4080
/*
* IN: 1/1000 V (0V to 4.08V)
* REG: 16mV/bit
*/
static u8 IN_TO_REG(unsigned val)
{
unsigned nval = clamp_val(val, ASB100_IN_MIN, ASB100_IN_MAX);
return (nval + 8) / 16;
}
static unsigned IN_FROM_REG(u8 reg)
{
return reg * 16;
}
static u8 FAN_TO_REG(long rpm, int div)
{
if (rpm == -1)
return 0;
if (rpm == 0)
return 255;
rpm = clamp_val(rpm, 1, 1000000);
return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
}
static int FAN_FROM_REG(u8 val, int div)
{
return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
}
/* These constants are a guess, consistent w/ w83781d */
#define ASB100_TEMP_MIN -128000
#define ASB100_TEMP_MAX 127000
/*
* TEMP: 0.001C/bit (-128C to +127C)
* REG: 1C/bit, two's complement
*/
static u8 TEMP_TO_REG(long temp)
{
int ntemp = clamp_val(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
ntemp += (ntemp < 0 ? -500 : 500);
return (u8)(ntemp / 1000);
}
static int TEMP_FROM_REG(u8 reg)
{
return (s8)reg * 1000;
}
/*
* PWM: 0 - 255 per sensors documentation
* REG: (6.25% duty cycle per bit)
*/
static u8 ASB100_PWM_TO_REG(int pwm)
{
pwm = clamp_val(pwm, 0, 255);
return (u8)(pwm / 16);
}
static int ASB100_PWM_FROM_REG(u8 reg)
{
return reg * 16;
}
#define DIV_FROM_REG(val) (1 << (val))
/*
* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
* REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
*/
static u8 DIV_TO_REG(long val)
{
return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
}
/*
* For each registered client, we need to keep some data in memory. That
* data is pointed to by client->data. The structure itself is
* dynamically allocated, at the same time the client itself is allocated.
*/
struct asb100_data {
struct device *hwmon_dev;
struct mutex lock;
struct mutex update_lock;
unsigned long last_updated; /* In jiffies */
/* array of 2 pointers to subclients */
struct i2c_client *lm75[2];
char valid; /* !=0 if following fields are valid */
u8 in[7]; /* Register value */
u8 in_max[7]; /* Register value */
u8 in_min[7]; /* Register value */
u8 fan[3]; /* Register