2019-05-27 06:55:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* max1619.c - Part of lm_sensors, Linux kernel modules for hardware
|
|
|
|
* monitoring
|
2012-10-10 13:25:56 +00:00
|
|
|
* Copyright (C) 2003-2004 Oleksij Rempel <bug-track@fisher-privat.net>
|
2014-01-29 19:40:08 +00:00
|
|
|
* Jean Delvare <jdelvare@suse.de>
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* Based on the lm90 driver. The MAX1619 is a sensor chip made by Maxim.
|
|
|
|
* It reports up to two temperatures (its own plus up to
|
|
|
|
* one external one). Complete datasheet can be
|
|
|
|
* obtained from Maxim's website at:
|
|
|
|
* http://pdfserv.maxim-ic.com/en/ds/MAX1619.pdf
|
|
|
|
*/
|
|
|
|
|
2024-07-16 03:28:58 +00:00
|
|
|
#include <linux/err.h>
|
2005-07-16 01:39:18 +00:00
|
|
|
#include <linux/hwmon.h>
|
2024-07-16 03:28:58 +00:00
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
2024-07-16 05:26:22 +00:00
|
|
|
#include <linux/regmap.h>
|
2024-07-16 16:01:27 +00:00
|
|
|
#include <linux/util_macros.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-18 03:28:03 +00:00
|
|
|
static const unsigned short normal_i2c[] = {
|
|
|
|
0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2024-07-16 05:26:22 +00:00
|
|
|
#define MAX1619_REG_LOCAL_TEMP 0x00
|
|
|
|
#define MAX1619_REG_REMOTE_TEMP 0x01
|
|
|
|
#define MAX1619_REG_STATUS 0x02
|
|
|
|
#define MAX1619_REG_CONFIG 0x03
|
|
|
|
#define MAX1619_REG_CONVRATE 0x04
|
|
|
|
#define MAX1619_REG_REMOTE_HIGH 0x07
|
|
|
|
#define MAX1619_REG_REMOTE_LOW 0x08
|
|
|
|
#define MAX1619_REG_REMOTE_CRIT 0x10
|
|
|
|
#define MAX1619_REG_REMOTE_CRIT_HYST 0x11
|
|
|
|
#define MAX1619_REG_MAN_ID 0xFE
|
|
|
|
#define MAX1619_REG_CHIP_ID 0xFF
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2024-07-16 05:26:22 +00:00
|
|
|
static int get_alarms(struct regmap *regmap)
|
|
|
|
{
|
|
|
|
static u32 regs[2] = { MAX1619_REG_STATUS, MAX1619_REG_CONFIG };
|
|
|
|
u8 regdata[2];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = regmap_multi_reg_read(regmap, regs, regdata, 2);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* OVERT status bit may be reversed */
|
|
|
|
if (!(regdata[1] & 0x20))
|
|
|
|
regdata[0] ^= 0x02;
|
|
|
|
|
|
|
|
return regdata[0] & 0x1e;
|
|
|
|
}
|
|
|
|
|
2024-07-16 15:24:28 +00:00
|
|
|
static int max1619_temp_read(struct regmap *regmap, u32 attr, int channel, long *val)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2024-07-16 15:24:28 +00:00
|
|
|
int reg = -1, alarm_bit = 0;
|
|
|
|
u32 temp;
|
|
|
|
int ret;
|
2024-07-16 05:26:22 +00:00
|
|
|
|
2024-07-16 15:24:28 +00:00
|
|
|
switch (attr) {
|
|
|
|
case hwmon_temp_input:
|
|
|
|
reg = channel ? MAX1619_REG_REMOTE_TEMP : MAX1619_REG_LOCAL_TEMP;
|
|
|
|
break;
|
|
|
|
case hwmon_temp_min:
|
|
|
|
reg = MAX1619_REG_REMOTE_LOW;
|
|
|
|
break;
|
|
|
|
case hwmon_temp_max:
|
|
|
|
reg = MAX1619_REG_REMOTE_HIGH;
|
|
|
|
break;
|
|
|
|
case hwmon_temp_crit:
|
|
|
|
reg = MAX1619_REG_REMOTE_CRIT;
|
|
|
|
break;
|
|
|
|
case hwmon_temp_crit_hyst:
|
|
|
|
reg = MAX1619_REG_REMOTE_CRIT_HYST;
|
|
|
|
break;
|
|
|
|
case hwmon_temp_min_alarm:
|
|
|
|
alarm_bit = 3;
|
|
|
|
break;
|
|
|
|
case hwmon_temp_max_alarm:
|
|
|
|
alarm_bit = 4;
|
|
|
|
break;
|
|
|
|
case hwmon_temp_crit_alarm:
|
|
|
|
alarm_bit = 1;
|
|
|
|
break;
|
|
|
|
case hwmon_temp_fault:
|
|
|
|
alarm_bit = 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
if (reg >= 0) {
|
|
|
|
ret = regmap_read(regmap, reg, &temp);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
*val = sign_extend32(temp, 7) * 1000;
|
|
|
|
} else {
|
|
|
|
ret = get_alarms(regmap);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
*val = !!(ret & BIT(alarm_bit));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2024-07-16 05:26:22 +00:00
|
|
|
|
2024-07-16 16:01:27 +00:00
|
|
|
static u16 update_intervals[] = { 16000, 8000, 4000, 2000, 1000, 500, 250, 125 };
|
|
|
|
|
2024-07-16 15:24:28 +00:00
|
|
|
static int max1619_chip_read(struct regmap *regmap, u32 attr, long *val)
|
|
|
|
{
|
2024-07-16 16:01:27 +00:00
|
|
|
int alarms, ret;
|
|
|
|
u32 regval;
|
2024-07-16 15:24:28 +00:00
|
|
|
|
|
|
|
switch (attr) {
|
2024-07-16 16:01:27 +00:00
|
|
|
case hwmon_chip_update_interval:
|
|
|
|
ret = regmap_read(regmap, MAX1619_REG_CONVRATE, ®val);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
*val = update_intervals[regval & 7];
|
|
|
|
break;
|
2024-07-16 15:24:28 +00:00
|
|
|
case hwmon_chip_alarms:
|
|
|
|
alarms = get_alarms(regmap);
|
|
|
|
if (alarms < 0)
|
|
|
|
return alarms;
|
|
|
|
*val = alarms;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 15:24:28 +00:00
|
|
|
static int max1619_read(struct device *dev, enum hwmon_sensor_types type,
|
|
|
|
u32 attr, int channel, long *val)
|
2008-01-06 14:27:59 +00:00
|
|
|
{
|
2024-07-16 05:26:22 +00:00
|
|
|
struct regmap *regmap = dev_get_drvdata(dev);
|
|
|
|
|
2024-07-16 15:24:28 +00:00
|
|
|
switch (type) {
|
|
|
|
case hwmon_chip:
|
|
|
|
return max1619_chip_read(regmap, attr, val);
|
|
|
|
case hwmon_temp:
|
|
|
|
return max1619_temp_read(regmap, attr, channel, val);
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-16 16:01:27 +00:00
|
|
|
static int max1619_chip_write(struct regmap *regmap, u32 attr, long val)
|
2024-07-16 15:24:28 +00:00
|
|
|
{
|
2024-07-16 16:01:27 +00:00
|
|
|
switch (attr) {
|
|
|
|
case hwmon_chip_update_interval:
|
|
|
|
val = find_closest_descending(val, update_intervals, ARRAY_SIZE(update_intervals));
|
|
|
|
return regmap_write(regmap, MAX1619_REG_CONVRATE, val);
|
|
|
|
default:
|
2024-07-16 15:24:28 +00:00
|
|
|
return -EOPNOTSUPP;
|
2024-07-16 16:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int max1619_temp_write(struct regmap *regmap,
|
|
|
|
u32 attr, int channel, long val)
|
|
|
|
{
|
|
|
|
int reg;
|
2024-07-16 15:24:28 +00:00
|
|
|
|
|
|
|
switch (attr) {
|
|
|
|
case hwmon_temp_min:
|
|
|
|
reg = MAX1619_REG_REMOTE_LOW;
|
|
|
|
break;
|
|
|
|
case hwmon_temp_max:
|
|
|
|
reg = MAX1619_REG_REMOTE_HIGH;
|
|
|
|
break;
|
|
|
|
case hwmon_temp_crit:
|
|
|
|
reg = MAX1619_REG_REMOTE_CRIT;
|
|
|
|
break;
|
|
|
|
case hwmon_temp_crit_hyst:
|
|
|
|
reg = MAX1619_REG_REMOTE_CRIT_HYST;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
|
|
|
|
return regmap_write(regmap, reg, val);
|
|
|
|
}
|
2024-07-16 05:26:22 +00:00
|
|
|
|
2024-07-16 16:01:27 +00:00
|
|
|
static int max1619_write(struct device *dev, enum hwmon_sensor_types type,
|
|
|
|
u32 attr, int channel, long val)
|
|
|
|
{
|
|
|
|
struct regmap *regmap = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case hwmon_chip:
|
|
|
|
return max1619_chip_write(regmap, attr, val);
|
|
|
|
case hwmon_temp:
|
|
|
|
return max1619_temp_write(regmap, attr, channel, val);
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-16 15:24:28 +00:00
|
|
|
static umode_t max1619_is_visible(const void *_data, enum hwmon_sensor_types type,
|
|
|
|
u32 attr, int channel)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case hwmon_chip:
|
|
|
|
switch (attr) {
|
2024-07-16 16:01:27 +00:00
|
|
|
case hwmon_chip_update_interval:
|
|
|
|
return 0644;
|
2024-07-16 15:24:28 +00:00
|
|
|
case hwmon_chip_alarms:
|
|
|
|
return 0444;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case hwmon_temp:
|
|
|
|
switch (attr) {
|
|
|
|
case hwmon_temp_input:
|
|
|
|
return 0444;
|
|
|
|
case hwmon_temp_min:
|
|
|
|
case hwmon_temp_max:
|
|
|
|
case hwmon_temp_crit:
|
|
|
|
case hwmon_temp_crit_hyst:
|
|
|
|
return 0644;
|
|
|
|
case hwmon_temp_min_alarm:
|
|
|
|
case hwmon_temp_max_alarm:
|
|
|
|
case hwmon_temp_crit_alarm:
|
|
|
|
case hwmon_temp_fault:
|
|
|
|
return 0444;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
2008-01-06 14:27:59 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 15:24:28 +00:00
|
|
|
static const struct hwmon_channel_info * const max1619_info[] = {
|
2024-07-16 16:01:27 +00:00
|
|
|
HWMON_CHANNEL_INFO(chip, HWMON_C_ALARMS | HWMON_C_UPDATE_INTERVAL),
|
2024-07-16 15:24:28 +00:00
|
|
|
HWMON_CHANNEL_INFO(temp,
|
|
|
|
HWMON_T_INPUT,
|
|
|
|
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
|
|
|
|
HWMON_T_CRIT | HWMON_T_CRIT_HYST |
|
|
|
|
HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
|
|
|
|
HWMON_T_CRIT_ALARM | HWMON_T_FAULT),
|
2006-09-24 19:24:46 +00:00
|
|
|
NULL
|
|
|
|
};
|
2024-07-16 15:24:28 +00:00
|
|
|
|
|
|
|
static const struct hwmon_ops max1619_hwmon_ops = {
|
|
|
|
.is_visible = max1619_is_visible,
|
|
|
|
.read = max1619_read,
|
|
|
|
.write = max1619_write,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct hwmon_chip_info max1619_chip_info = {
|
|
|
|
.ops = &max1619_hwmon_ops,
|
|
|
|
.info = max1619_info,
|
|
|
|
};
|
2006-09-24 19:24:46 +00:00
|
|
|
|
2008-07-16 17:30:15 +00:00
|
|
|
/* Return 0 if detection is successful, -ENODEV otherwise */
|
2009-12-14 20:17:23 +00:00
|
|
|
static int max1619_detect(struct i2c_client *client,
|
2008-07-16 17:30:15 +00:00
|
|
|
struct i2c_board_info *info)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2009-12-09 19:35:57 +00:00
|
|
|
struct i2c_adapter *adapter = client->adapter;
|
2024-07-26 22:29:40 +00:00
|
|
|
int regval;
|
2005-11-07 09:01:49 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
|
2008-07-16 17:30:15 +00:00
|
|
|
return -ENODEV;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2024-07-26 22:29:40 +00:00
|
|
|
regval = i2c_smbus_read_byte_data(client, MAX1619_REG_CONFIG);
|
|
|
|
if (regval < 0 || (regval & 0x03))
|
|
|
|
return -ENODEV;
|
|
|
|
regval = i2c_smbus_read_byte_data(client, MAX1619_REG_CONVRATE);
|
|
|
|
if (regval < 0 || regval > 0x07)
|
|
|
|
return -ENODEV;
|
|
|
|
regval = i2c_smbus_read_byte_data(client, MAX1619_REG_STATUS);
|
|
|
|
if (regval < 0 || (regval & 0x61))
|
2009-12-09 19:35:57 +00:00
|
|
|
return -ENODEV;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2024-07-26 22:29:40 +00:00
|
|
|
regval = i2c_smbus_read_byte_data(client, MAX1619_REG_MAN_ID);
|
|
|
|
if (regval != 0x4d)
|
|
|
|
return -ENODEV;
|
|
|
|
regval = i2c_smbus_read_byte_data(client, MAX1619_REG_CHIP_ID);
|
|
|
|
if (regval != 0x04)
|
2009-12-09 19:35:57 +00:00
|
|
|
return -ENODEV;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2022-08-18 21:00:11 +00:00
|
|
|
strscpy(info->type, "max1619", I2C_NAME_SIZE);
|
2008-07-16 17:30:15 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-07-16 05:26:22 +00:00
|
|
|
static int max1619_init_chip(struct regmap *regmap)
|
2014-04-12 17:14:33 +00:00
|
|
|
{
|
2024-07-16 05:26:22 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = regmap_write(regmap, MAX1619_REG_CONVRATE, 5); /* 2 Hz */
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Start conversions */
|
|
|
|
return regmap_clear_bits(regmap, MAX1619_REG_CONFIG, 0x40);
|
2014-04-12 17:14:33 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 05:26:22 +00:00
|
|
|
/* regmap */
|
|
|
|
|
|
|
|
static int max1619_reg_read(void *context, unsigned int reg, unsigned int *val)
|
2008-07-16 17:30:15 +00:00
|
|
|
{
|
2024-07-16 05:26:22 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = i2c_smbus_read_byte_data(context, reg);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
*val = ret;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int max1619_reg_write(void *context, unsigned int reg, unsigned int val)
|
|
|
|
{
|
|
|
|
int offset = reg < MAX1619_REG_REMOTE_CRIT ? 6 : 2;
|
|
|
|
|
|
|
|
return i2c_smbus_write_byte_data(context, reg + offset, val);
|
|
|
|
}
|
2008-07-16 17:30:15 +00:00
|
|
|
|
2024-07-16 05:26:22 +00:00
|
|
|
static bool max1619_regmap_is_volatile(struct device *dev, unsigned int reg)
|
|
|
|
{
|
|
|
|
return reg <= MAX1619_REG_STATUS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool max1619_regmap_is_writeable(struct device *dev, unsigned int reg)
|
|
|
|
{
|
|
|
|
return reg > MAX1619_REG_STATUS && reg <= MAX1619_REG_REMOTE_CRIT_HYST;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct regmap_config max1619_regmap_config = {
|
|
|
|
.reg_bits = 8,
|
|
|
|
.val_bits = 8,
|
|
|
|
.max_register = MAX1619_REG_REMOTE_CRIT_HYST,
|
|
|
|
.cache_type = REGCACHE_MAPLE,
|
|
|
|
.volatile_reg = max1619_regmap_is_volatile,
|
|
|
|
.writeable_reg = max1619_regmap_is_writeable,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct regmap_bus max1619_regmap_bus = {
|
|
|
|
.reg_write = max1619_reg_write,
|
|
|
|
.reg_read = max1619_reg_read,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int max1619_probe(struct i2c_client *client)
|
|
|
|
{
|
|
|
|
struct device *dev = &client->dev;
|
|
|
|
struct device *hwmon_dev;
|
|
|
|
struct regmap *regmap;
|
|
|
|
int ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2024-07-16 05:26:22 +00:00
|
|
|
regmap = devm_regmap_init(dev, &max1619_regmap_bus, client,
|
|
|
|
&max1619_regmap_config);
|
|
|
|
if (IS_ERR(regmap))
|
|
|
|
return PTR_ERR(regmap);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2024-07-16 05:26:22 +00:00
|
|
|
ret = max1619_init_chip(regmap);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2024-07-16 15:24:28 +00:00
|
|
|
hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
|
|
|
|
regmap, &max1619_chip_info, NULL);
|
2014-04-12 17:35:29 +00:00
|
|
|
return PTR_ERR_OR_ZERO(hwmon_dev);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-12 17:14:33 +00:00
|
|
|
static const struct i2c_device_id max1619_id[] = {
|
2024-04-30 08:56:53 +00:00
|
|
|
{ "max1619" },
|
2014-04-12 17:14:33 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(i2c, max1619_id);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2017-09-11 19:16:49 +00:00
|
|
|
#ifdef CONFIG_OF
|
|
|
|
static const struct of_device_id max1619_of_match[] = {
|
|
|
|
{ .compatible = "maxim,max1619", },
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(of, max1619_of_match);
|
|
|
|
#endif
|
|
|
|
|
2014-04-12 17:14:33 +00:00
|
|
|
static struct i2c_driver max1619_driver = {
|
|
|
|
.class = I2C_CLASS_HWMON,
|
|
|
|
.driver = {
|
|
|
|
.name = "max1619",
|
2017-09-11 19:16:49 +00:00
|
|
|
.of_match_table = of_match_ptr(max1619_of_match),
|
2014-04-12 17:14:33 +00:00
|
|
|
},
|
2023-05-05 13:17:18 +00:00
|
|
|
.probe = max1619_probe,
|
2014-04-12 17:14:33 +00:00
|
|
|
.id_table = max1619_id,
|
|
|
|
.detect = max1619_detect,
|
|
|
|
.address_list = normal_i2c,
|
|
|
|
};
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2012-01-20 07:38:18 +00:00
|
|
|
module_i2c_driver(max1619_driver);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2014-01-29 19:40:08 +00:00
|
|
|
MODULE_AUTHOR("Oleksij Rempel <bug-track@fisher-privat.net>, Jean Delvare <jdelvare@suse.de>");
|
2005-04-16 22:20:36 +00:00
|
|
|
MODULE_DESCRIPTION("MAX1619 sensor driver");
|
|
|
|
MODULE_LICENSE("GPL");
|