2019-05-27 09:55:05 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2007-07-31 22:06:52 +04:00
|
|
|
/*
|
|
|
|
* A hwmon driver for the Analog Devices ADT7470
|
|
|
|
* Copyright (C) 2007 IBM
|
|
|
|
*
|
2013-08-27 02:42:27 +04:00
|
|
|
* Author: Darrick J. Wong <darrick.wong@oracle.com>
|
2007-07-31 22:06:52 +04:00
|
|
|
*/
|
|
|
|
|
2010-10-20 10:51:27 +04:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2007-07-31 22:06:52 +04:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/jiffies.h>
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/hwmon.h>
|
|
|
|
#include <linux/hwmon-sysfs.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/log2.h>
|
2009-01-07 01:41:34 +03:00
|
|
|
#include <linux/kthread.h>
|
2021-08-26 05:41:19 +03:00
|
|
|
#include <linux/regmap.h>
|
2022-04-07 13:13:12 +03:00
|
|
|
#include <linux/sched.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
|
|
|
#include <linux/slab.h>
|
2016-08-08 04:35:45 +03:00
|
|
|
#include <linux/util_macros.h>
|
2007-07-31 22:06:52 +04:00
|
|
|
|
|
|
|
/* Addresses to scan */
|
2008-02-18 06:28:03 +03:00
|
|
|
static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
|
2007-07-31 22:06:52 +04:00
|
|
|
|
|
|
|
/* ADT7470 registers */
|
|
|
|
#define ADT7470_REG_BASE_ADDR 0x20
|
|
|
|
#define ADT7470_REG_TEMP_BASE_ADDR 0x20
|
|
|
|
#define ADT7470_REG_TEMP_MAX_ADDR 0x29
|
|
|
|
#define ADT7470_REG_FAN_BASE_ADDR 0x2A
|
|
|
|
#define ADT7470_REG_FAN_MAX_ADDR 0x31
|
|
|
|
#define ADT7470_REG_PWM_BASE_ADDR 0x32
|
|
|
|
#define ADT7470_REG_PWM_MAX_ADDR 0x35
|
|
|
|
#define ADT7470_REG_PWM_MAX_BASE_ADDR 0x38
|
|
|
|
#define ADT7470_REG_PWM_MAX_MAX_ADDR 0x3B
|
|
|
|
#define ADT7470_REG_CFG 0x40
|
2021-08-26 05:41:19 +03:00
|
|
|
#define ADT7470_STRT_MASK 0x01
|
|
|
|
#define ADT7470_TEST_MASK 0x02
|
2007-07-31 22:06:52 +04:00
|
|
|
#define ADT7470_FSPD_MASK 0x04
|
2021-08-26 05:41:19 +03:00
|
|
|
#define ADT7470_T05_STB_MASK 0x80
|
2007-07-31 22:06:52 +04:00
|
|
|
#define ADT7470_REG_ALARM1 0x41
|
2007-12-20 01:11:25 +03:00
|
|
|
#define ADT7470_R1T_ALARM 0x01
|
|
|
|
#define ADT7470_R2T_ALARM 0x02
|
|
|
|
#define ADT7470_R3T_ALARM 0x04
|
|
|
|
#define ADT7470_R4T_ALARM 0x08
|
|
|
|
#define ADT7470_R5T_ALARM 0x10
|
|
|
|
#define ADT7470_R6T_ALARM 0x20
|
|
|
|
#define ADT7470_R7T_ALARM 0x40
|
|
|
|
#define ADT7470_OOL_ALARM 0x80
|
2007-07-31 22:06:52 +04:00
|
|
|
#define ADT7470_REG_ALARM2 0x42
|
2007-12-20 01:11:25 +03:00
|
|
|
#define ADT7470_R8T_ALARM 0x01
|
|
|
|
#define ADT7470_R9T_ALARM 0x02
|
|
|
|
#define ADT7470_R10T_ALARM 0x04
|
|
|
|
#define ADT7470_FAN1_ALARM 0x10
|
|
|
|
#define ADT7470_FAN2_ALARM 0x20
|
|
|
|
#define ADT7470_FAN3_ALARM 0x40
|
|
|
|
#define ADT7470_FAN4_ALARM 0x80
|
2007-07-31 22:06:52 +04:00
|
|
|
#define ADT7470_REG_TEMP_LIMITS_BASE_ADDR 0x44
|
|
|
|
#define ADT7470_REG_TEMP_LIMITS_MAX_ADDR 0x57
|
|
|
|
#define ADT7470_REG_FAN_MIN_BASE_ADDR 0x58
|
|
|
|
#define ADT7470_REG_FAN_MIN_MAX_ADDR 0x5F
|
|
|
|
#define ADT7470_REG_FAN_MAX_BASE_ADDR 0x60
|
|
|
|
#define ADT7470_REG_FAN_MAX_MAX_ADDR 0x67
|
|
|
|
#define ADT7470_REG_PWM_CFG_BASE_ADDR 0x68
|
|
|
|
#define ADT7470_REG_PWM12_CFG 0x68
|
|
|
|
#define ADT7470_PWM2_AUTO_MASK 0x40
|
|
|
|
#define ADT7470_PWM1_AUTO_MASK 0x80
|
2009-01-07 01:41:32 +03:00
|
|
|
#define ADT7470_PWM_AUTO_MASK 0xC0
|
2007-07-31 22:06:52 +04:00
|
|
|
#define ADT7470_REG_PWM34_CFG 0x69
|
|
|
|
#define ADT7470_PWM3_AUTO_MASK 0x40
|
|
|
|
#define ADT7470_PWM4_AUTO_MASK 0x80
|
|
|
|
#define ADT7470_REG_PWM_MIN_BASE_ADDR 0x6A
|
|
|
|
#define ADT7470_REG_PWM_MIN_MAX_ADDR 0x6D
|
|
|
|
#define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR 0x6E
|
|
|
|
#define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR 0x71
|
2016-08-08 04:35:45 +03:00
|
|
|
#define ADT7470_REG_CFG_2 0x74
|
2007-07-31 22:06:52 +04:00
|
|
|
#define ADT7470_REG_ACOUSTICS12 0x75
|
|
|
|
#define ADT7470_REG_ACOUSTICS34 0x76
|
|
|
|
#define ADT7470_REG_DEVICE 0x3D
|
|
|
|
#define ADT7470_REG_VENDOR 0x3E
|
|
|
|
#define ADT7470_REG_REVISION 0x3F
|
|
|
|
#define ADT7470_REG_ALARM1_MASK 0x72
|
|
|
|
#define ADT7470_REG_ALARM2_MASK 0x73
|
|
|
|
#define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR 0x7C
|
|
|
|
#define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR 0x7D
|
|
|
|
#define ADT7470_REG_MAX_ADDR 0x81
|
|
|
|
|
|
|
|
#define ADT7470_TEMP_COUNT 10
|
|
|
|
#define ADT7470_TEMP_REG(x) (ADT7470_REG_TEMP_BASE_ADDR + (x))
|
|
|
|
#define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
|
|
|
|
#define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
|
|
|
|
((x) * 2) + 1)
|
|
|
|
|
|
|
|
#define ADT7470_FAN_COUNT 4
|
|
|
|
#define ADT7470_REG_FAN(x) (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
|
|
|
|
#define ADT7470_REG_FAN_MIN(x) (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
|
|
|
|
#define ADT7470_REG_FAN_MAX(x) (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
|
|
|
|
|
|
|
|
#define ADT7470_PWM_COUNT 4
|
|
|
|
#define ADT7470_REG_PWM(x) (ADT7470_REG_PWM_BASE_ADDR + (x))
|
|
|
|
#define ADT7470_REG_PWM_MAX(x) (ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
|
|
|
|
#define ADT7470_REG_PWM_MIN(x) (ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
|
|
|
|
#define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
|
|
|
|
#define ADT7470_REG_PWM_CFG(x) (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
|
|
|
|
#define ADT7470_REG_PWM_AUTO_TEMP(x) (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
|
|
|
|
((x) / 2))
|
|
|
|
|
2007-12-20 01:11:25 +03:00
|
|
|
#define ALARM2(x) ((x) << 8)
|
|
|
|
|
2007-07-31 22:06:52 +04:00
|
|
|
#define ADT7470_VENDOR 0x41
|
|
|
|
#define ADT7470_DEVICE 0x70
|
|
|
|
/* datasheet only mentions a revision 2 */
|
|
|
|
#define ADT7470_REVISION 0x02
|
|
|
|
|
|
|
|
/* "all temps" according to hwmon sysfs interface spec */
|
|
|
|
#define ADT7470_PWM_ALL_TEMPS 0x3FF
|
|
|
|
|
|
|
|
/* How often do we reread sensors values? (In jiffies) */
|
|
|
|
#define SENSOR_REFRESH_INTERVAL (5 * HZ)
|
|
|
|
|
|
|
|
/* How often do we reread sensor limit values? (In jiffies) */
|
|
|
|
#define LIMIT_REFRESH_INTERVAL (60 * HZ)
|
|
|
|
|
2009-01-07 01:41:33 +03:00
|
|
|
/* Wait at least 200ms per sensor for 10 sensors */
|
|
|
|
#define TEMP_COLLECTION_TIME 2000
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2009-01-07 01:41:34 +03:00
|
|
|
/* auto update thing won't fire more than every 2s */
|
|
|
|
#define AUTO_UPDATE_INTERVAL 2000
|
|
|
|
|
2007-07-31 22:06:52 +04:00
|
|
|
/* datasheet says to divide this number by the fan reading to get fan rpm */
|
|
|
|
#define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
|
|
|
|
#define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
|
|
|
|
#define FAN_PERIOD_INVALID 65535
|
|
|
|
#define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
|
|
|
|
|
2016-08-08 04:35:45 +03:00
|
|
|
/* Config registers 1 and 2 include fields for selecting the PWM frequency */
|
|
|
|
#define ADT7470_CFG_LF 0x40
|
|
|
|
#define ADT7470_FREQ_MASK 0x70
|
|
|
|
#define ADT7470_FREQ_SHIFT 4
|
|
|
|
|
2007-07-31 22:06:52 +04:00
|
|
|
struct adt7470_data {
|
2021-08-26 05:41:19 +03:00
|
|
|
struct regmap *regmap;
|
2007-07-31 22:06:52 +04:00
|
|
|
struct mutex lock;
|
|
|
|
char sensors_valid;
|
|
|
|
char limits_valid;
|
|
|
|
unsigned long sensors_last_updated; /* In jiffies */
|
|
|
|
unsigned long limits_last_updated; /* In jiffies */
|
|
|
|
|
2009-01-07 01:41:33 +03:00
|
|
|
int num_temp_sensors; /* -1 = probe */
|
2009-01-07 01:41:34 +03:00
|
|
|
int temperatures_probed;
|
2009-01-07 01:41:33 +03:00
|
|
|
|
2007-07-31 22:06:52 +04:00
|
|
|
s8 temp[ADT7470_TEMP_COUNT];
|
|
|
|
s8 temp_min[ADT7470_TEMP_COUNT];
|
|
|
|
s8 temp_max[ADT7470_TEMP_COUNT];
|
|
|
|
u16 fan[ADT7470_FAN_COUNT];
|
|
|
|
u16 fan_min[ADT7470_FAN_COUNT];
|
|
|
|
u16 fan_max[ADT7470_FAN_COUNT];
|
2007-12-20 01:11:25 +03:00
|
|
|
u16 alarm;
|
|
|
|
u16 alarms_mask;
|
2007-07-31 22:06:52 +04:00
|
|
|
u8 force_pwm_max;
|
|
|
|
u8 pwm[ADT7470_PWM_COUNT];
|
|
|
|
u8 pwm_max[ADT7470_PWM_COUNT];
|
|
|
|
u8 pwm_automatic[ADT7470_PWM_COUNT];
|
|
|
|
u8 pwm_min[ADT7470_PWM_COUNT];
|
|
|
|
s8 pwm_tmin[ADT7470_PWM_COUNT];
|
|
|
|
u8 pwm_auto_temp[ADT7470_PWM_COUNT];
|
2009-01-07 01:41:34 +03:00
|
|
|
|
|
|
|
struct task_struct *auto_update;
|
|
|
|
unsigned int auto_update_interval;
|
2007-07-31 22:06:52 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 16-bit registers on the ADT7470 are low-byte first. The data sheet says
|
|
|
|
* that the low byte must be read before the high byte.
|
|
|
|
*/
|
2021-08-26 05:41:19 +03:00
|
|
|
static inline int adt7470_read_word_data(struct adt7470_data *data, unsigned int reg,
|
|
|
|
unsigned int *val)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
2021-08-26 05:41:19 +03:00
|
|
|
u8 regval[2];
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = regmap_bulk_read(data->regmap, reg, ®val, 2);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
*val = regval[0] | (regval[1] << 8);
|
2021-08-26 05:41:18 +03:00
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
return 0;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
static inline int adt7470_write_word_data(struct adt7470_data *data, unsigned int reg,
|
|
|
|
unsigned int val)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
2021-08-26 05:41:19 +03:00
|
|
|
u8 regval[2];
|
|
|
|
|
|
|
|
regval[0] = val & 0xFF;
|
|
|
|
regval[1] = val >> 8;
|
|
|
|
|
|
|
|
return regmap_bulk_write(data->regmap, reg, ®val, 2);
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2009-01-07 01:41:34 +03:00
|
|
|
/* Probe for temperature sensors. Assumes lock is held */
|
2021-08-26 05:41:19 +03:00
|
|
|
static int adt7470_read_temperatures(struct adt7470_data *data)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
2009-01-07 01:41:34 +03:00
|
|
|
unsigned long res;
|
2021-08-26 05:41:19 +03:00
|
|
|
unsigned int pwm_cfg[2];
|
|
|
|
int err;
|
2007-07-31 22:06:52 +04:00
|
|
|
int i;
|
2021-08-26 05:41:19 +03:00
|
|
|
u8 pwm[ADT7470_FAN_COUNT];
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2009-01-07 01:41:32 +03:00
|
|
|
/* save pwm[1-4] config register */
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_read(data->regmap, ADT7470_REG_PWM_CFG(0), &pwm_cfg[0]);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
err = regmap_read(data->regmap, ADT7470_REG_PWM_CFG(2), &pwm_cfg[1]);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2009-01-07 01:41:32 +03:00
|
|
|
|
|
|
|
/* set manual pwm to whatever it is set to now */
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_bulk_read(data->regmap, ADT7470_REG_PWM(0), &pwm[0],
|
|
|
|
ADT7470_PWM_COUNT);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2009-01-07 01:41:32 +03:00
|
|
|
|
|
|
|
/* put pwm in manual mode */
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_update_bits(data->regmap, ADT7470_REG_PWM_CFG(0),
|
|
|
|
ADT7470_PWM_AUTO_MASK, 0);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
err = regmap_update_bits(data->regmap, ADT7470_REG_PWM_CFG(2),
|
|
|
|
ADT7470_PWM_AUTO_MASK, 0);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2009-01-07 01:41:32 +03:00
|
|
|
|
|
|
|
/* write pwm control to whatever it was */
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_bulk_write(data->regmap, ADT7470_REG_PWM(0), &pwm[0],
|
|
|
|
ADT7470_PWM_COUNT);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2009-01-07 01:41:32 +03:00
|
|
|
|
2007-07-31 22:06:52 +04:00
|
|
|
/* start reading temperature sensors */
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_update_bits(data->regmap, ADT7470_REG_CFG,
|
|
|
|
ADT7470_T05_STB_MASK, ADT7470_T05_STB_MASK);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2009-01-07 01:41:33 +03:00
|
|
|
/* Delay is 200ms * number of temp sensors. */
|
2009-01-07 01:41:34 +03:00
|
|
|
res = msleep_interruptible((data->num_temp_sensors >= 0 ?
|
|
|
|
data->num_temp_sensors * 200 :
|
|
|
|
TEMP_COLLECTION_TIME));
|
2007-07-31 22:06:52 +04:00
|
|
|
|
|
|
|
/* done reading temperature sensors */
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_update_bits(data->regmap, ADT7470_REG_CFG,
|
|
|
|
ADT7470_T05_STB_MASK, 0);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2009-01-07 01:41:32 +03:00
|
|
|
/* restore pwm[1-4] config registers */
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_write(data->regmap, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
err = regmap_write(data->regmap, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (res)
|
2009-01-07 01:41:34 +03:00
|
|
|
return -EAGAIN;
|
|
|
|
|
|
|
|
/* Only count fans if we have to */
|
|
|
|
if (data->num_temp_sensors >= 0)
|
|
|
|
return 0;
|
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_bulk_read(data->regmap, ADT7470_TEMP_REG(0), &data->temp[0],
|
|
|
|
ADT7470_TEMP_COUNT);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2009-01-07 01:41:34 +03:00
|
|
|
for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
|
|
|
|
if (data->temp[i])
|
|
|
|
data->num_temp_sensors = i + 1;
|
|
|
|
}
|
|
|
|
data->temperatures_probed = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2009-01-07 01:41:34 +03:00
|
|
|
static int adt7470_update_thread(void *p)
|
|
|
|
{
|
|
|
|
struct i2c_client *client = p;
|
|
|
|
struct adt7470_data *data = i2c_get_clientdata(client);
|
|
|
|
|
|
|
|
while (!kthread_should_stop()) {
|
|
|
|
mutex_lock(&data->lock);
|
2021-08-26 05:41:19 +03:00
|
|
|
adt7470_read_temperatures(data);
|
2009-01-07 01:41:34 +03:00
|
|
|
mutex_unlock(&data->lock);
|
2016-09-09 08:19:26 +03:00
|
|
|
|
2009-01-07 01:41:34 +03:00
|
|
|
if (kthread_should_stop())
|
|
|
|
break;
|
2016-09-09 08:19:26 +03:00
|
|
|
|
2022-04-07 13:13:12 +03:00
|
|
|
schedule_timeout_interruptible(msecs_to_jiffies(data->auto_update_interval));
|
2009-01-07 01:41:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-20 01:34:22 +03:00
|
|
|
static int adt7470_update_sensors(struct adt7470_data *data)
|
2009-01-07 01:41:34 +03:00
|
|
|
{
|
2021-08-26 05:41:19 +03:00
|
|
|
unsigned int val;
|
|
|
|
int err;
|
2009-01-07 01:41:34 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!data->temperatures_probed)
|
2021-08-26 05:41:19 +03:00
|
|
|
err = adt7470_read_temperatures(data);
|
2009-01-07 01:41:34 +03:00
|
|
|
else
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_bulk_read(data->regmap, ADT7470_TEMP_REG(0), &data->temp[0],
|
|
|
|
ADT7470_TEMP_COUNT);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2009-01-07 01:41:33 +03:00
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
for (i = 0; i < ADT7470_FAN_COUNT; i++) {
|
|
|
|
err = adt7470_read_word_data(data, ADT7470_REG_FAN(i), &val);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
data->fan[i] = val;
|
|
|
|
}
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_bulk_read(data->regmap, ADT7470_REG_PWM(0), &data->pwm[0], ADT7470_PWM_COUNT);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
for (i = 0; i < ADT7470_PWM_COUNT; i++) {
|
|
|
|
unsigned int mask;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
|
|
|
if (i % 2)
|
2021-08-26 05:41:19 +03:00
|
|
|
mask = ADT7470_PWM2_AUTO_MASK;
|
2007-07-31 22:06:52 +04:00
|
|
|
else
|
2021-08-26 05:41:19 +03:00
|
|
|
mask = ADT7470_PWM1_AUTO_MASK;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_read(data->regmap, ADT7470_REG_PWM_CFG(i), &val);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
data->pwm_automatic[i] = !!(val & mask);
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_read(data->regmap, ADT7470_REG_PWM_AUTO_TEMP(i), &val);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2007-07-31 22:06:52 +04:00
|
|
|
if (!(i % 2))
|
2021-08-26 05:41:19 +03:00
|
|
|
data->pwm_auto_temp[i] = val >> 4;
|
2007-07-31 22:06:52 +04:00
|
|
|
else
|
2021-08-26 05:41:19 +03:00
|
|
|
data->pwm_auto_temp[i] = val & 0xF;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_read(data->regmap, ADT7470_REG_CFG, &val);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
data->force_pwm_max = !!(val & ADT7470_FSPD_MASK);
|
|
|
|
|
|
|
|
err = regmap_read(data->regmap, ADT7470_REG_ALARM1, &val);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
data->alarm = val;
|
|
|
|
if (data->alarm & ADT7470_OOL_ALARM) {
|
|
|
|
err = regmap_read(data->regmap, ADT7470_REG_ALARM2, &val);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
data->alarm |= ALARM2(val);
|
|
|
|
}
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
err = adt7470_read_word_data(data, ADT7470_REG_ALARM1_MASK, &val);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
data->alarms_mask = val;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2020-10-20 01:34:22 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2020-10-20 01:34:22 +03:00
|
|
|
static int adt7470_update_limits(struct adt7470_data *data)
|
|
|
|
{
|
2021-08-26 05:41:19 +03:00
|
|
|
unsigned int val;
|
|
|
|
int err;
|
2020-10-20 01:34:22 +03:00
|
|
|
int i;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
|
|
|
for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_read(data->regmap, ADT7470_TEMP_MIN_REG(i), &val);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
data->temp_min[i] = (s8)val;
|
|
|
|
err = regmap_read(data->regmap, ADT7470_TEMP_MAX_REG(i), &val);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
data->temp_max[i] = (s8)val;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ADT7470_FAN_COUNT; i++) {
|
2021-08-26 05:41:19 +03:00
|
|
|
err = adt7470_read_word_data(data, ADT7470_REG_FAN_MIN(i), &val);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
data->fan_min[i] = val;
|
|
|
|
err = adt7470_read_word_data(data, ADT7470_REG_FAN_MAX(i), &val);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
data->fan_max[i] = val;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ADT7470_PWM_COUNT; i++) {
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_read(data->regmap, ADT7470_REG_PWM_MAX(i), &val);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
data->pwm_max[i] = val;
|
|
|
|
err = regmap_read(data->regmap, ADT7470_REG_PWM_MIN(i), &val);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
data->pwm_min[i] = val;
|
|
|
|
err = regmap_read(data->regmap, ADT7470_REG_PWM_TMIN(i), &val);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
data->pwm_tmin[i] = (s8)val;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2020-10-20 01:34:22 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2020-10-20 01:34:22 +03:00
|
|
|
static struct adt7470_data *adt7470_update_device(struct device *dev)
|
|
|
|
{
|
|
|
|
struct adt7470_data *data = dev_get_drvdata(dev);
|
|
|
|
unsigned long local_jiffies = jiffies;
|
|
|
|
int need_sensors = 1;
|
|
|
|
int need_limits = 1;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure out if we need to update the shadow registers.
|
|
|
|
* Lockless means that we may occasionally report out of
|
|
|
|
* date data.
|
|
|
|
*/
|
|
|
|
if (time_before(local_jiffies, data->sensors_last_updated +
|
|
|
|
SENSOR_REFRESH_INTERVAL) &&
|
|
|
|
data->sensors_valid)
|
|
|
|
need_sensors = 0;
|
|
|
|
|
|
|
|
if (time_before(local_jiffies, data->limits_last_updated +
|
|
|
|
LIMIT_REFRESH_INTERVAL) &&
|
|
|
|
data->limits_valid)
|
|
|
|
need_limits = 0;
|
|
|
|
|
|
|
|
if (!need_sensors && !need_limits)
|
|
|
|
return data;
|
|
|
|
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
if (need_sensors) {
|
|
|
|
err = adt7470_update_sensors(data);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
data->sensors_last_updated = local_jiffies;
|
|
|
|
data->sensors_valid = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_limits) {
|
|
|
|
err = adt7470_update_limits(data);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
data->limits_last_updated = local_jiffies;
|
|
|
|
data->limits_valid = 1;
|
|
|
|
}
|
2007-07-31 22:06:52 +04:00
|
|
|
out:
|
|
|
|
mutex_unlock(&data->lock);
|
2020-10-20 01:34:22 +03:00
|
|
|
|
|
|
|
return err < 0 ? ERR_PTR(err) : data;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2016-12-22 15:04:34 +03:00
|
|
|
static ssize_t auto_update_interval_show(struct device *dev,
|
2009-01-07 01:41:34 +03:00
|
|
|
struct device_attribute *devattr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct adt7470_data *data = adt7470_update_device(dev);
|
2020-10-20 01:34:22 +03:00
|
|
|
|
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
|
|
|
|
2009-01-07 01:41:34 +03:00
|
|
|
return sprintf(buf, "%d\n", data->auto_update_interval);
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:04:34 +03:00
|
|
|
static ssize_t auto_update_interval_store(struct device *dev,
|
|
|
|
struct device_attribute *devattr,
|
|
|
|
const char *buf, size_t count)
|
2009-01-07 01:41:34 +03:00
|
|
|
{
|
2014-07-16 19:12:54 +04:00
|
|
|
struct adt7470_data *data = dev_get_drvdata(dev);
|
2009-01-07 01:41:34 +03:00
|
|
|
long temp;
|
|
|
|
|
2012-01-04 23:58:52 +04:00
|
|
|
if (kstrtol(buf, 10, &temp))
|
2009-01-07 01:41:34 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
2013-01-09 20:09:34 +04:00
|
|
|
temp = clamp_val(temp, 0, 60000);
|
2009-01-07 01:41:34 +03:00
|
|
|
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
data->auto_update_interval = temp;
|
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:04:34 +03:00
|
|
|
static ssize_t num_temp_sensors_show(struct device *dev,
|
2009-01-07 01:41:33 +03:00
|
|
|
struct device_attribute *devattr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct adt7470_data *data = adt7470_update_device(dev);
|
2020-10-20 01:34:22 +03:00
|
|
|
|
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
|
|
|
|
2009-01-07 01:41:33 +03:00
|
|
|
return sprintf(buf, "%d\n", data->num_temp_sensors);
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:04:34 +03:00
|
|
|
static ssize_t num_temp_sensors_store(struct device *dev,
|
|
|
|
struct device_attribute *devattr,
|
|
|
|
const char *buf, size_t count)
|
2009-01-07 01:41:33 +03:00
|
|
|
{
|
2014-07-16 19:12:54 +04:00
|
|
|
struct adt7470_data *data = dev_get_drvdata(dev);
|
2009-01-07 01:41:33 +03:00
|
|
|
long temp;
|
|
|
|
|
2012-01-04 23:58:52 +04:00
|
|
|
if (kstrtol(buf, 10, &temp))
|
2009-01-07 01:41:33 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
2013-01-09 20:09:34 +04:00
|
|
|
temp = clamp_val(temp, -1, 10);
|
2009-01-07 01:41:33 +03:00
|
|
|
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
data->num_temp_sensors = temp;
|
2009-01-07 01:41:34 +03:00
|
|
|
if (temp < 0)
|
|
|
|
data->temperatures_probed = 0;
|
2009-01-07 01:41:33 +03:00
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
static int adt7470_temp_read(struct device *dev, u32 attr, int channel, long *val)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
|
|
|
struct adt7470_data *data = adt7470_update_device(dev);
|
2020-10-20 01:34:22 +03:00
|
|
|
|
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
switch (attr) {
|
|
|
|
case hwmon_temp_input:
|
|
|
|
*val = 1000 * data->temp[channel];
|
|
|
|
break;
|
|
|
|
case hwmon_temp_min:
|
|
|
|
*val = 1000 * data->temp_min[channel];
|
|
|
|
break;
|
|
|
|
case hwmon_temp_max:
|
|
|
|
*val = 1000 * data->temp_max[channel];
|
|
|
|
break;
|
|
|
|
case hwmon_temp_alarm:
|
|
|
|
*val = !!(data->alarm & channel);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
return 0;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
static int adt7470_temp_write(struct device *dev, u32 attr, int channel, long val)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
2014-07-16 19:12:54 +04:00
|
|
|
struct adt7470_data *data = dev_get_drvdata(dev);
|
2021-08-26 05:41:19 +03:00
|
|
|
int err;
|
2008-11-13 00:26:57 +03:00
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
val = clamp_val(val, -128000, 127000);
|
|
|
|
val = DIV_ROUND_CLOSEST(val, 1000);
|
2020-10-20 01:34:22 +03:00
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
switch (attr) {
|
|
|
|
case hwmon_temp_min:
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
data->temp_min[channel] = val;
|
|
|
|
err = regmap_write(data->regmap, ADT7470_TEMP_MIN_REG(channel), val);
|
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
break;
|
|
|
|
case hwmon_temp_max:
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
data->temp_max[channel] = val;
|
|
|
|
err = regmap_write(data->regmap, ADT7470_TEMP_MAX_REG(channel), val);
|
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
2020-10-20 01:34:22 +03:00
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
return err;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2016-12-22 15:04:34 +03:00
|
|
|
static ssize_t alarm_mask_show(struct device *dev,
|
2021-08-26 05:41:20 +03:00
|
|
|
struct device_attribute *devattr, char *buf)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
|
|
|
struct adt7470_data *data = adt7470_update_device(dev);
|
|
|
|
|
2020-10-20 01:34:22 +03:00
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
|
|
|
|
2007-12-20 01:11:25 +03:00
|
|
|
return sprintf(buf, "%x\n", data->alarms_mask);
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2016-12-22 15:04:34 +03:00
|
|
|
static ssize_t alarm_mask_store(struct device *dev,
|
|
|
|
struct device_attribute *devattr,
|
|
|
|
const char *buf, size_t count)
|
2016-09-09 08:19:25 +03:00
|
|
|
{
|
|
|
|
struct adt7470_data *data = dev_get_drvdata(dev);
|
|
|
|
long mask;
|
2021-08-26 05:41:19 +03:00
|
|
|
int err;
|
2016-09-09 08:19:25 +03:00
|
|
|
|
|
|
|
if (kstrtoul(buf, 0, &mask))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (mask & ~0xffff)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
data->alarms_mask = mask;
|
2021-08-26 05:41:19 +03:00
|
|
|
err = adt7470_write_word_data(data, ADT7470_REG_ALARM1_MASK, mask);
|
2016-09-09 08:19:25 +03:00
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
return err < 0 ? err : count;
|
2016-09-09 08:19:25 +03:00
|
|
|
}
|
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
static int adt7470_fan_read(struct device *dev, u32 attr, int channel, long *val)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
|
|
|
struct adt7470_data *data = adt7470_update_device(dev);
|
|
|
|
|
2020-10-20 01:34:22 +03:00
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
switch (attr) {
|
|
|
|
case hwmon_fan_input:
|
|
|
|
if (FAN_DATA_VALID(data->fan[channel]))
|
|
|
|
*val = FAN_PERIOD_TO_RPM(data->fan[channel]);
|
|
|
|
else
|
|
|
|
*val = 0;
|
|
|
|
break;
|
|
|
|
case hwmon_fan_min:
|
|
|
|
if (FAN_DATA_VALID(data->fan_min[channel]))
|
|
|
|
*val = FAN_PERIOD_TO_RPM(data->fan_min[channel]);
|
|
|
|
else
|
|
|
|
*val = 0;
|
|
|
|
break;
|
|
|
|
case hwmon_fan_max:
|
|
|
|
if (FAN_DATA_VALID(data->fan_max[channel]))
|
|
|
|
*val = FAN_PERIOD_TO_RPM(data->fan_max[channel]);
|
|
|
|
else
|
|
|
|
*val = 0;
|
|
|
|
break;
|
|
|
|
case hwmon_fan_alarm:
|
|
|
|
*val = !!(data->alarm & (1 << (12 + channel)));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
2020-10-20 01:34:22 +03:00
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
return 0;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
static int adt7470_fan_write(struct device *dev, u32 attr, int channel, long val)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
2014-07-16 19:12:54 +04:00
|
|
|
struct adt7470_data *data = dev_get_drvdata(dev);
|
2021-08-26 05:41:19 +03:00
|
|
|
int err;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2022-01-21 14:55:43 +03:00
|
|
|
if (val <= 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
val = FAN_RPM_TO_PERIOD(val);
|
|
|
|
val = clamp_val(val, 1, 65534);
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
switch (attr) {
|
|
|
|
case hwmon_fan_min:
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
data->fan_min[channel] = val;
|
|
|
|
err = adt7470_write_word_data(data, ADT7470_REG_FAN_MIN(channel), val);
|
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
break;
|
|
|
|
case hwmon_fan_max:
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
data->fan_max[channel] = val;
|
|
|
|
err = adt7470_write_word_data(data, ADT7470_REG_FAN_MAX(channel), val);
|
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
2020-10-20 01:34:22 +03:00
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
return err;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2018-12-11 01:02:02 +03:00
|
|
|
static ssize_t force_pwm_max_show(struct device *dev,
|
|
|
|
struct device_attribute *devattr, char *buf)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
|
|
|
struct adt7470_data *data = adt7470_update_device(dev);
|
2020-10-20 01:34:22 +03:00
|
|
|
|
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
|
|
|
|
2007-07-31 22:06:52 +04:00
|
|
|
return sprintf(buf, "%d\n", data->force_pwm_max);
|
|
|
|
}
|
|
|
|
|
2018-12-11 01:02:02 +03:00
|
|
|
static ssize_t force_pwm_max_store(struct device *dev,
|
|
|
|
struct device_attribute *devattr,
|
|
|
|
const char *buf, size_t count)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
2014-07-16 19:12:54 +04:00
|
|
|
struct adt7470_data *data = dev_get_drvdata(dev);
|
2008-11-13 00:26:57 +03:00
|
|
|
long temp;
|
2021-08-26 05:41:19 +03:00
|
|
|
int err;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2012-01-04 23:58:52 +04:00
|
|
|
if (kstrtol(buf, 10, &temp))
|
2008-11-13 00:26:57 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
2007-07-31 22:06:52 +04:00
|
|
|
mutex_lock(&data->lock);
|
|
|
|
data->force_pwm_max = temp;
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_update_bits(data->regmap, ADT7470_REG_CFG,
|
|
|
|
ADT7470_FSPD_MASK,
|
|
|
|
temp ? ADT7470_FSPD_MASK : 0);
|
2007-07-31 22:06:52 +04:00
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
return err < 0 ? err : count;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2016-08-08 04:35:45 +03:00
|
|
|
/* These are the valid PWM frequencies to the nearest Hz */
|
|
|
|
static const int adt7470_freq_map[] = {
|
|
|
|
11, 15, 22, 29, 35, 44, 59, 88, 1400, 22500
|
|
|
|
};
|
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
static int pwm1_freq_get(struct device *dev)
|
2016-08-08 04:35:45 +03:00
|
|
|
{
|
2021-08-26 05:41:20 +03:00
|
|
|
struct adt7470_data *data = dev_get_drvdata(dev);
|
2021-08-26 05:41:19 +03:00
|
|
|
unsigned int cfg_reg_1, cfg_reg_2;
|
2016-08-08 04:35:45 +03:00
|
|
|
int index;
|
2021-08-26 05:41:19 +03:00
|
|
|
int err;
|
|
|
|
|
2016-08-08 04:35:45 +03:00
|
|
|
mutex_lock(&data->lock);
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_read(data->regmap, ADT7470_REG_CFG, &cfg_reg_1);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
err = regmap_read(data->regmap, ADT7470_REG_CFG_2, &cfg_reg_2);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
2016-08-08 04:35:45 +03:00
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
|
|
|
|
index = (cfg_reg_2 & ADT7470_FREQ_MASK) >> ADT7470_FREQ_SHIFT;
|
|
|
|
if (!(cfg_reg_1 & ADT7470_CFG_LF))
|
|
|
|
index += 8;
|
|
|
|
if (index >= ARRAY_SIZE(adt7470_freq_map))
|
|
|
|
index = ARRAY_SIZE(adt7470_freq_map) - 1;
|
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
return adt7470_freq_map[index];
|
2021-08-26 05:41:19 +03:00
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
return err;
|
2016-08-08 04:35:45 +03:00
|
|
|
}
|
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
static int adt7470_pwm_read(struct device *dev, u32 attr, int channel, long *val)
|
|
|
|
{
|
|
|
|
struct adt7470_data *data = adt7470_update_device(dev);
|
|
|
|
|
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
|
|
|
|
|
|
|
switch (attr) {
|
|
|
|
case hwmon_pwm_input:
|
|
|
|
*val = data->pwm[channel];
|
|
|
|
break;
|
|
|
|
case hwmon_pwm_enable:
|
|
|
|
*val = 1 + data->pwm_automatic[channel];
|
|
|
|
break;
|
|
|
|
case hwmon_pwm_freq:
|
|
|
|
*val = pwm1_freq_get(dev);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pwm1_freq_set(struct device *dev, long freq)
|
2016-08-08 04:35:45 +03:00
|
|
|
{
|
|
|
|
struct adt7470_data *data = dev_get_drvdata(dev);
|
2021-08-26 05:41:20 +03:00
|
|
|
unsigned int low_freq = ADT7470_CFG_LF;
|
2016-08-08 04:35:45 +03:00
|
|
|
int index;
|
2021-08-26 05:41:19 +03:00
|
|
|
int err;
|
2016-08-08 04:35:45 +03:00
|
|
|
|
|
|
|
/* Round the user value given to the closest available frequency */
|
|
|
|
index = find_closest(freq, adt7470_freq_map,
|
|
|
|
ARRAY_SIZE(adt7470_freq_map));
|
|
|
|
|
|
|
|
if (index >= 8) {
|
|
|
|
index -= 8;
|
|
|
|
low_freq = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
/* Configuration Register 1 */
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_update_bits(data->regmap, ADT7470_REG_CFG,
|
|
|
|
ADT7470_CFG_LF, low_freq);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
|
2016-08-08 04:35:45 +03:00
|
|
|
/* Configuration Register 2 */
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_update_bits(data->regmap, ADT7470_REG_CFG_2,
|
|
|
|
ADT7470_FREQ_MASK,
|
|
|
|
index << ADT7470_FREQ_SHIFT);
|
|
|
|
out:
|
2016-08-08 04:35:45 +03:00
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int adt7470_pwm_write(struct device *dev, u32 attr, int channel, long val)
|
|
|
|
{
|
|
|
|
struct adt7470_data *data = dev_get_drvdata(dev);
|
|
|
|
unsigned int pwm_auto_reg_mask;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
switch (attr) {
|
|
|
|
case hwmon_pwm_input:
|
|
|
|
val = clamp_val(val, 0, 255);
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
data->pwm[channel] = val;
|
|
|
|
err = regmap_write(data->regmap, ADT7470_REG_PWM(channel),
|
|
|
|
data->pwm[channel]);
|
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
break;
|
|
|
|
case hwmon_pwm_enable:
|
|
|
|
if (channel % 2)
|
|
|
|
pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
|
|
|
|
else
|
|
|
|
pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
|
|
|
|
|
|
|
|
if (val != 2 && val != 1)
|
|
|
|
return -EINVAL;
|
|
|
|
val--;
|
|
|
|
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
data->pwm_automatic[channel] = val;
|
|
|
|
err = regmap_update_bits(data->regmap, ADT7470_REG_PWM_CFG(channel),
|
|
|
|
pwm_auto_reg_mask,
|
|
|
|
val ? pwm_auto_reg_mask : 0);
|
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
break;
|
|
|
|
case hwmon_pwm_freq:
|
|
|
|
err = pwm1_freq_set(dev, val);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2016-08-08 04:35:45 +03:00
|
|
|
}
|
|
|
|
|
2018-12-11 01:02:02 +03:00
|
|
|
static ssize_t pwm_max_show(struct device *dev,
|
|
|
|
struct device_attribute *devattr, char *buf)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
|
|
|
struct adt7470_data *data = adt7470_update_device(dev);
|
2020-10-20 01:34:22 +03:00
|
|
|
|
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
|
|
|
|
2007-07-31 22:06:52 +04:00
|
|
|
return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
|
|
|
|
}
|
|
|
|
|
2018-12-11 01:02:02 +03:00
|
|
|
static ssize_t pwm_max_store(struct device *dev,
|
|
|
|
struct device_attribute *devattr,
|
|
|
|
const char *buf, size_t count)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
2014-07-16 19:12:54 +04:00
|
|
|
struct adt7470_data *data = dev_get_drvdata(dev);
|
2008-11-13 00:26:57 +03:00
|
|
|
long temp;
|
2021-08-26 05:41:19 +03:00
|
|
|
int err;
|
2008-11-13 00:26:57 +03:00
|
|
|
|
2012-01-04 23:58:52 +04:00
|
|
|
if (kstrtol(buf, 10, &temp))
|
2008-11-13 00:26:57 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
2013-01-09 20:09:34 +04:00
|
|
|
temp = clamp_val(temp, 0, 255);
|
2007-07-31 22:06:52 +04:00
|
|
|
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
data->pwm_max[attr->index] = temp;
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_write(data->regmap, ADT7470_REG_PWM_MAX(attr->index),
|
|
|
|
temp);
|
2007-07-31 22:06:52 +04:00
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
return err < 0 ? err : count;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2018-12-11 01:02:02 +03:00
|
|
|
static ssize_t pwm_min_show(struct device *dev,
|
|
|
|
struct device_attribute *devattr, char *buf)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
|
|
|
struct adt7470_data *data = adt7470_update_device(dev);
|
2020-10-20 01:34:22 +03:00
|
|
|
|
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
|
|
|
|
2007-07-31 22:06:52 +04:00
|
|
|
return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
|
|
|
|
}
|
|
|
|
|
2018-12-11 01:02:02 +03:00
|
|
|
static ssize_t pwm_min_store(struct device *dev,
|
|
|
|
struct device_attribute *devattr,
|
|
|
|
const char *buf, size_t count)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
2014-07-16 19:12:54 +04:00
|
|
|
struct adt7470_data *data = dev_get_drvdata(dev);
|
2008-11-13 00:26:57 +03:00
|
|
|
long temp;
|
2021-08-26 05:41:19 +03:00
|
|
|
int err;
|
2008-11-13 00:26:57 +03:00
|
|
|
|
2012-01-04 23:58:52 +04:00
|
|
|
if (kstrtol(buf, 10, &temp))
|
2008-11-13 00:26:57 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
2013-01-09 20:09:34 +04:00
|
|
|
temp = clamp_val(temp, 0, 255);
|
2007-07-31 22:06:52 +04:00
|
|
|
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
data->pwm_min[attr->index] = temp;
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_write(data->regmap, ADT7470_REG_PWM_MIN(attr->index),
|
|
|
|
temp);
|
2007-07-31 22:06:52 +04:00
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
return err < 0 ? err : count;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2018-12-11 01:02:02 +03:00
|
|
|
static ssize_t pwm_tmax_show(struct device *dev,
|
|
|
|
struct device_attribute *devattr, char *buf)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
|
|
|
struct adt7470_data *data = adt7470_update_device(dev);
|
2020-10-20 01:34:22 +03:00
|
|
|
|
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
|
|
|
|
2007-07-31 22:06:52 +04:00
|
|
|
/* the datasheet says that tmax = tmin + 20C */
|
|
|
|
return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
|
|
|
|
}
|
|
|
|
|
2018-12-11 01:02:02 +03:00
|
|
|
static ssize_t pwm_tmin_show(struct device *dev,
|
|
|
|
struct device_attribute *devattr, char *buf)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
|
|
|
struct adt7470_data *data = adt7470_update_device(dev);
|
2020-10-20 01:34:22 +03:00
|
|
|
|
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
|
|
|
|
2007-07-31 22:06:52 +04:00
|
|
|
return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
|
|
|
|
}
|
|
|
|
|
2018-12-11 01:02:02 +03:00
|
|
|
static ssize_t pwm_tmin_store(struct device *dev,
|
|
|
|
struct device_attribute *devattr,
|
|
|
|
const char *buf, size_t count)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
2014-07-16 19:12:54 +04:00
|
|
|
struct adt7470_data *data = dev_get_drvdata(dev);
|
2008-11-13 00:26:57 +03:00
|
|
|
long temp;
|
2021-08-26 05:41:19 +03:00
|
|
|
int err;
|
2008-11-13 00:26:57 +03:00
|
|
|
|
2012-01-04 23:58:52 +04:00
|
|
|
if (kstrtol(buf, 10, &temp))
|
2008-11-13 00:26:57 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
2016-12-03 22:10:34 +03:00
|
|
|
temp = clamp_val(temp, -128000, 127000);
|
2009-01-07 01:41:31 +03:00
|
|
|
temp = DIV_ROUND_CLOSEST(temp, 1000);
|
2007-07-31 22:06:52 +04:00
|
|
|
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
data->pwm_tmin[attr->index] = temp;
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_write(data->regmap, ADT7470_REG_PWM_TMIN(attr->index),
|
|
|
|
temp);
|
2007-07-31 22:06:52 +04:00
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
return err < 0 ? err : count;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2018-12-11 01:02:02 +03:00
|
|
|
static ssize_t pwm_auto_temp_show(struct device *dev,
|
|
|
|
struct device_attribute *devattr, char *buf)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
|
|
|
struct adt7470_data *data = adt7470_update_device(dev);
|
2020-10-20 01:34:22 +03:00
|
|
|
u8 ctrl;
|
|
|
|
|
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2020-10-20 01:34:22 +03:00
|
|
|
ctrl = data->pwm_auto_temp[attr->index];
|
2007-07-31 22:06:52 +04:00
|
|
|
if (ctrl)
|
|
|
|
return sprintf(buf, "%d\n", 1 << (ctrl - 1));
|
|
|
|
else
|
|
|
|
return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cvt_auto_temp(int input)
|
|
|
|
{
|
|
|
|
if (input == ADT7470_PWM_ALL_TEMPS)
|
|
|
|
return 0;
|
2007-11-06 11:21:42 +03:00
|
|
|
if (input < 1 || !is_power_of_2(input))
|
2007-07-31 22:06:52 +04:00
|
|
|
return -EINVAL;
|
|
|
|
return ilog2(input) + 1;
|
|
|
|
}
|
|
|
|
|
2018-12-11 01:02:02 +03:00
|
|
|
static ssize_t pwm_auto_temp_store(struct device *dev,
|
|
|
|
struct device_attribute *devattr,
|
|
|
|
const char *buf, size_t count)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
2014-07-16 19:12:54 +04:00
|
|
|
struct adt7470_data *data = dev_get_drvdata(dev);
|
2007-07-31 22:06:52 +04:00
|
|
|
int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
|
2021-08-26 05:41:19 +03:00
|
|
|
unsigned int mask, val;
|
2008-11-13 00:26:57 +03:00
|
|
|
long temp;
|
2021-08-26 05:41:19 +03:00
|
|
|
int err;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2012-01-04 23:58:52 +04:00
|
|
|
if (kstrtol(buf, 10, &temp))
|
2008-11-13 00:26:57 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
temp = cvt_auto_temp(temp);
|
2007-07-31 22:06:52 +04:00
|
|
|
if (temp < 0)
|
|
|
|
return temp;
|
|
|
|
|
|
|
|
mutex_lock(&data->lock);
|
|
|
|
data->pwm_automatic[attr->index] = temp;
|
|
|
|
|
|
|
|
if (!(attr->index % 2)) {
|
2021-08-26 05:41:19 +03:00
|
|
|
mask = 0xF0;
|
|
|
|
val = (temp << 4) & 0xF0;
|
2007-07-31 22:06:52 +04:00
|
|
|
} else {
|
2021-08-26 05:41:19 +03:00
|
|
|
mask = 0x0F;
|
|
|
|
val = temp & 0x0F;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_update_bits(data->regmap, pwm_auto_reg, mask, val);
|
2007-07-31 22:06:52 +04:00
|
|
|
mutex_unlock(&data->lock);
|
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
return err < 0 ? err : count;
|
2007-07-31 22:06:52 +04:00
|
|
|
}
|
|
|
|
|
2016-12-22 15:04:34 +03:00
|
|
|
static DEVICE_ATTR_RW(alarm_mask);
|
|
|
|
static DEVICE_ATTR_RW(num_temp_sensors);
|
|
|
|
static DEVICE_ATTR_RW(auto_update_interval);
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2018-12-11 01:02:02 +03:00
|
|
|
static SENSOR_DEVICE_ATTR_RW(force_pwm_max, force_pwm_max, 0);
|
|
|
|
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm_min, 0);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm_min, 1);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm_min, 2);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_pwm, pwm_min, 3);
|
|
|
|
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm_max, 0);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm_max, 1);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm_max, 2);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point2_pwm, pwm_max, 3);
|
|
|
|
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_tmin, 0);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_temp, pwm_tmin, 1);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_temp, pwm_tmin, 2);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_temp, pwm_tmin, 3);
|
|
|
|
|
|
|
|
static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_temp, pwm_tmax, 0);
|
|
|
|
static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point2_temp, pwm_tmax, 1);
|
|
|
|
static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point2_temp, pwm_tmax, 2);
|
|
|
|
static SENSOR_DEVICE_ATTR_RO(pwm4_auto_point2_temp, pwm_tmax, 3);
|
|
|
|
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm1_auto_channels_temp, pwm_auto_temp, 0);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm2_auto_channels_temp, pwm_auto_temp, 1);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm3_auto_channels_temp, pwm_auto_temp, 2);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(pwm4_auto_channels_temp, pwm_auto_temp, 3);
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2014-07-16 19:12:54 +04:00
|
|
|
static struct attribute *adt7470_attrs[] = {
|
2007-12-20 01:11:25 +03:00
|
|
|
&dev_attr_alarm_mask.attr,
|
2009-01-07 01:41:33 +03:00
|
|
|
&dev_attr_num_temp_sensors.attr,
|
2009-01-07 01:41:34 +03:00
|
|
|
&dev_attr_auto_update_interval.attr,
|
2007-07-31 22:06:52 +04:00
|
|
|
&sensor_dev_attr_force_pwm_max.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2014-07-16 19:12:54 +04:00
|
|
|
ATTRIBUTE_GROUPS(adt7470);
|
|
|
|
|
2021-08-26 05:41:20 +03:00
|
|
|
static int adt7470_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
|
|
|
|
int channel, long *val)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case hwmon_temp:
|
|
|
|
return adt7470_temp_read(dev, attr, channel, val);
|
|
|
|
case hwmon_fan:
|
|
|
|
return adt7470_fan_read(dev, attr, channel, val);
|
|
|
|
case hwmon_pwm:
|
|
|
|
return adt7470_pwm_read(dev, attr, channel, val);
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int adt7470_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
|
|
|
|
int channel, long val)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case hwmon_temp:
|
|
|
|
return adt7470_temp_write(dev, attr, channel, val);
|
|
|
|
case hwmon_fan:
|
|
|
|
return adt7470_fan_write(dev, attr, channel, val);
|
|
|
|
case hwmon_pwm:
|
|
|
|
return adt7470_pwm_write(dev, attr, channel, val);
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static umode_t adt7470_is_visible(const void *_data, enum hwmon_sensor_types type,
|
|
|
|
u32 attr, int channel)
|
|
|
|
{
|
|
|
|
umode_t mode = 0;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case hwmon_temp:
|
|
|
|
switch (attr) {
|
|
|
|
case hwmon_temp:
|
|
|
|
case hwmon_temp_alarm:
|
|
|
|
mode = 0444;
|
|
|
|
break;
|
|
|
|
case hwmon_temp_min:
|
|
|
|
case hwmon_temp_max:
|
|
|
|
mode = 0644;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case hwmon_fan:
|
|
|
|
switch (attr) {
|
|
|
|
case hwmon_fan_input:
|
|
|
|
case hwmon_fan_alarm:
|
|
|
|
mode = 0444;
|
|
|
|
break;
|
|
|
|
case hwmon_fan_min:
|
|
|
|
case hwmon_fan_max:
|
|
|
|
mode = 0644;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case hwmon_pwm:
|
|
|
|
switch (attr) {
|
|
|
|
case hwmon_pwm_input:
|
|
|
|
case hwmon_pwm_enable:
|
|
|
|
mode = 0644;
|
|
|
|
break;
|
|
|
|
case hwmon_pwm_freq:
|
|
|
|
if (channel == 0)
|
|
|
|
mode = 0644;
|
|
|
|
else
|
|
|
|
mode = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct hwmon_ops adt7470_hwmon_ops = {
|
|
|
|
.is_visible = adt7470_is_visible,
|
|
|
|
.read = adt7470_read,
|
|
|
|
.write = adt7470_write,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct hwmon_channel_info *adt7470_info[] = {
|
|
|
|
HWMON_CHANNEL_INFO(temp,
|
|
|
|
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM,
|
|
|
|
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM,
|
|
|
|
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM,
|
|
|
|
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM,
|
|
|
|
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM,
|
|
|
|
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM,
|
|
|
|
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM,
|
|
|
|
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM,
|
|
|
|
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM,
|
|
|
|
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_ALARM),
|
|
|
|
HWMON_CHANNEL_INFO(fan,
|
|
|
|
HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX | HWMON_F_DIV | HWMON_F_ALARM,
|
|
|
|
HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX | HWMON_F_DIV | HWMON_F_ALARM,
|
|
|
|
HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX | HWMON_F_DIV | HWMON_F_ALARM,
|
|
|
|
HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_MAX | HWMON_F_DIV | HWMON_F_ALARM),
|
|
|
|
HWMON_CHANNEL_INFO(pwm,
|
|
|
|
HWMON_PWM_INPUT | HWMON_PWM_ENABLE | HWMON_PWM_FREQ,
|
|
|
|
HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
|
|
|
|
HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
|
|
|
|
HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct hwmon_chip_info adt7470_chip_info = {
|
|
|
|
.ops = &adt7470_hwmon_ops,
|
|
|
|
.info = adt7470_info,
|
|
|
|
};
|
|
|
|
|
2008-07-16 21:30:10 +04:00
|
|
|
/* Return 0 if detection is successful, -ENODEV otherwise */
|
2009-12-14 23:17:23 +03:00
|
|
|
static int adt7470_detect(struct i2c_client *client,
|
2008-07-16 21:30:10 +04:00
|
|
|
struct i2c_board_info *info)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
2008-07-16 21:30:10 +04:00
|
|
|
struct i2c_adapter *adapter = client->adapter;
|
2009-12-09 22:35:57 +03:00
|
|
|
int vendor, device, revision;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
|
|
|
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
|
2008-07-16 21:30:10 +04:00
|
|
|
return -ENODEV;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2009-12-09 22:35:57 +03:00
|
|
|
vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
|
|
|
|
if (vendor != ADT7470_VENDOR)
|
|
|
|
return -ENODEV;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2009-12-09 22:35:57 +03:00
|
|
|
device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
|
|
|
|
if (device != ADT7470_DEVICE)
|
|
|
|
return -ENODEV;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2009-12-09 22:35:57 +03:00
|
|
|
revision = i2c_smbus_read_byte_data(client, ADT7470_REG_REVISION);
|
|
|
|
if (revision != ADT7470_REVISION)
|
|
|
|
return -ENODEV;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2021-08-26 05:41:18 +03:00
|
|
|
strscpy(info->type, "adt7470", I2C_NAME_SIZE);
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2008-07-16 21:30:10 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-26 05:41:19 +03:00
|
|
|
static const struct regmap_config adt7470_regmap_config = {
|
|
|
|
.reg_bits = 8,
|
|
|
|
.val_bits = 8,
|
|
|
|
.use_single_read = true,
|
|
|
|
.use_single_write = true,
|
|
|
|
};
|
2014-07-16 19:12:05 +04:00
|
|
|
|
2020-08-13 19:02:22 +03:00
|
|
|
static int adt7470_probe(struct i2c_client *client)
|
2008-07-16 21:30:10 +04:00
|
|
|
{
|
2014-07-16 19:12:54 +04:00
|
|
|
struct device *dev = &client->dev;
|
2008-07-16 21:30:10 +04:00
|
|
|
struct adt7470_data *data;
|
2014-07-16 19:12:54 +04:00
|
|
|
struct device *hwmon_dev;
|
2021-08-26 05:41:19 +03:00
|
|
|
int err;
|
2008-07-16 21:30:10 +04:00
|
|
|
|
2014-07-16 19:12:54 +04:00
|
|
|
data = devm_kzalloc(dev, sizeof(struct adt7470_data), GFP_KERNEL);
|
2012-06-02 20:58:01 +04:00
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
2008-07-16 21:30:10 +04:00
|
|
|
|
2009-01-07 01:41:33 +03:00
|
|
|
data->num_temp_sensors = -1;
|
2009-01-07 01:41:34 +03:00
|
|
|
data->auto_update_interval = AUTO_UPDATE_INTERVAL;
|
2021-08-26 05:41:19 +03:00
|
|
|
data->regmap = devm_regmap_init_i2c(client, &adt7470_regmap_config);
|
|
|
|
if (IS_ERR(data->regmap))
|
|
|
|
return PTR_ERR(data->regmap);
|
2009-01-07 01:41:33 +03:00
|
|
|
|
2008-07-16 21:30:10 +04:00
|
|
|
i2c_set_clientdata(client, data);
|
|
|
|
mutex_init(&data->lock);
|
2007-07-31 22:06:52 +04:00
|
|
|
|
|
|
|
dev_info(&client->dev, "%s chip found\n", client->name);
|
|
|
|
|
|
|
|
/* Initialize the ADT7470 chip */
|
2021-08-26 05:41:19 +03:00
|
|
|
err = regmap_update_bits(data->regmap, ADT7470_REG_CFG,
|
|
|
|
ADT7470_STRT_MASK | ADT7470_TEST_MASK,
|
|
|
|
ADT7470_STRT_MASK | ADT7470_TEST_MASK);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2007-07-31 22:06:52 +04:00
|
|
|
|
|
|
|
/* Register sysfs hooks */
|
2021-08-26 05:41:20 +03:00
|
|
|
hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data,
|
|
|
|
&adt7470_chip_info,
|
|
|
|
adt7470_groups);
|
2014-07-16 19:12:54 +04:00
|
|
|
|
|
|
|
if (IS_ERR(hwmon_dev))
|
|
|
|
return PTR_ERR(hwmon_dev);
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2013-07-04 02:04:58 +04:00
|
|
|
data->auto_update = kthread_run(adt7470_update_thread, client, "%s",
|
2014-07-16 19:12:54 +04:00
|
|
|
dev_name(hwmon_dev));
|
2021-08-26 05:41:18 +03:00
|
|
|
if (IS_ERR(data->auto_update))
|
2014-07-16 19:12:54 +04:00
|
|
|
return PTR_ERR(data->auto_update);
|
2009-01-07 01:41:34 +03:00
|
|
|
|
2007-07-31 22:06:52 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-16 21:30:10 +04:00
|
|
|
static int adt7470_remove(struct i2c_client *client)
|
2007-07-31 22:06:52 +04:00
|
|
|
{
|
|
|
|
struct adt7470_data *data = i2c_get_clientdata(client);
|
|
|
|
|
2009-01-07 01:41:34 +03:00
|
|
|
kthread_stop(data->auto_update);
|
2007-07-31 22:06:52 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-16 19:12:05 +04:00
|
|
|
static const struct i2c_device_id adt7470_id[] = {
|
|
|
|
{ "adt7470", 0 },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(i2c, adt7470_id);
|
|
|
|
|
|
|
|
static struct i2c_driver adt7470_driver = {
|
|
|
|
.class = I2C_CLASS_HWMON,
|
|
|
|
.driver = {
|
|
|
|
.name = "adt7470",
|
|
|
|
},
|
2020-08-13 19:02:22 +03:00
|
|
|
.probe_new = adt7470_probe,
|
2014-07-16 19:12:05 +04:00
|
|
|
.remove = adt7470_remove,
|
|
|
|
.id_table = adt7470_id,
|
|
|
|
.detect = adt7470_detect,
|
|
|
|
.address_list = normal_i2c,
|
|
|
|
};
|
|
|
|
|
2012-01-20 11:38:18 +04:00
|
|
|
module_i2c_driver(adt7470_driver);
|
2007-07-31 22:06:52 +04:00
|
|
|
|
2013-08-27 02:42:27 +04:00
|
|
|
MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
|
2007-07-31 22:06:52 +04:00
|
|
|
MODULE_DESCRIPTION("ADT7470 driver");
|
|
|
|
MODULE_LICENSE("GPL");
|