2019-05-27 14:55:06 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* lm92 - Hardware monitoring driver
|
2014-01-30 03:40:08 +08:00
|
|
|
* Copyright (C) 2005-2008 Jean Delvare <jdelvare@suse.de>
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Based on the lm90 driver, with some ideas taken from the lm_sensors
|
|
|
|
* lm92 driver as well.
|
|
|
|
*
|
|
|
|
* The LM92 is a sensor chip made by National Semiconductor. It reports
|
|
|
|
* its own temperature with a 0.0625 deg resolution and a 0.33 deg
|
|
|
|
* accuracy. Complete datasheet can be obtained from National's website
|
|
|
|
* at:
|
|
|
|
* http://www.national.com/pf/LM/LM92.html
|
|
|
|
*
|
|
|
|
* This driver also supports the MAX6635 sensor chip made by Maxim.
|
|
|
|
* This chip is compatible with the LM92, but has a lesser accuracy
|
|
|
|
* (1.0 deg). Complete datasheet can be obtained from Maxim's website
|
|
|
|
* at:
|
|
|
|
* http://www.maxim-ic.com/quick_view2.cfm/qv_pk/3074
|
|
|
|
*
|
|
|
|
* Since the LM92 was the first chipset supported by this driver, most
|
|
|
|
* comments will refer to this chipset, but are actually general and
|
|
|
|
* concern all supported chipsets, unless mentioned otherwise.
|
|
|
|
*
|
|
|
|
* Support could easily be added for the National Semiconductor LM76
|
|
|
|
* and Maxim MAX6633 and MAX6634 chips, which are mostly compatible
|
|
|
|
* with the LM92.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/i2c.h>
|
2005-07-16 09:39:18 +08:00
|
|
|
#include <linux/hwmon.h>
|
2008-01-06 22:25:52 +08:00
|
|
|
#include <linux/hwmon-sysfs.h>
|
2005-07-16 09:39:18 +08:00
|
|
|
#include <linux/err.h>
|
2006-01-19 06:19:26 +08:00
|
|
|
#include <linux/mutex.h>
|
2012-10-10 21:25:56 +08:00
|
|
|
#include <linux/jiffies.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-15 13:04:18 +08:00
|
|
|
/*
|
|
|
|
* The LM92 and MAX6635 have 2 two-state pins for address selection,
|
|
|
|
* resulting in 4 possible addresses.
|
|
|
|
*/
|
2008-02-18 11:28:03 +08:00
|
|
|
static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
|
|
|
|
I2C_CLIENT_END };
|
2018-03-22 22:37:44 +08:00
|
|
|
enum chips { lm92, max6635 };
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* The LM92 registers */
|
|
|
|
#define LM92_REG_CONFIG 0x01 /* 8-bit, RW */
|
|
|
|
#define LM92_REG_TEMP 0x00 /* 16-bit, RO */
|
|
|
|
#define LM92_REG_TEMP_HYST 0x02 /* 16-bit, RW */
|
|
|
|
#define LM92_REG_TEMP_CRIT 0x03 /* 16-bit, RW */
|
|
|
|
#define LM92_REG_TEMP_LOW 0x04 /* 16-bit, RW */
|
|
|
|
#define LM92_REG_TEMP_HIGH 0x05 /* 16-bit, RW */
|
|
|
|
#define LM92_REG_MAN_ID 0x07 /* 16-bit, RO, LM92 only */
|
|
|
|
|
2012-01-15 13:04:18 +08:00
|
|
|
/*
|
|
|
|
* The LM92 uses signed 13-bit values with LSB = 0.0625 degree Celsius,
|
|
|
|
* left-justified in 16-bit registers. No rounding is done, with such
|
|
|
|
* a resolution it's just not worth it. Note that the MAX6635 doesn't
|
|
|
|
* make use of the 4 lower bits for limits (i.e. effective resolution
|
|
|
|
* for limits is 1 degree Celsius).
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
static inline int TEMP_FROM_REG(s16 reg)
|
|
|
|
{
|
|
|
|
return reg / 8 * 625 / 10;
|
|
|
|
}
|
|
|
|
|
2014-08-05 10:08:31 +08:00
|
|
|
static inline s16 TEMP_TO_REG(long val)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2014-08-05 10:08:31 +08:00
|
|
|
val = clamp_val(val, -60000, 160000);
|
2005-04-17 06:20:36 +08:00
|
|
|
return val * 10 / 625 * 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Alarm flags are stored in the 3 LSB of the temperature register */
|
|
|
|
static inline u8 ALARMS_FROM_REG(s16 reg)
|
|
|
|
{
|
|
|
|
return reg & 0x0007;
|
|
|
|
}
|
|
|
|
|
2014-04-13 04:07:08 +08:00
|
|
|
enum temp_index {
|
|
|
|
t_input,
|
|
|
|
t_crit,
|
|
|
|
t_min,
|
|
|
|
t_max,
|
|
|
|
t_hyst,
|
|
|
|
t_num_regs
|
|
|
|
};
|
|
|
|
|
|
|
|
static const u8 regs[t_num_regs] = {
|
|
|
|
[t_input] = LM92_REG_TEMP,
|
|
|
|
[t_crit] = LM92_REG_TEMP_CRIT,
|
|
|
|
[t_min] = LM92_REG_TEMP_LOW,
|
|
|
|
[t_max] = LM92_REG_TEMP_HIGH,
|
|
|
|
[t_hyst] = LM92_REG_TEMP_HYST,
|
|
|
|
};
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Client data (each client gets its own) */
|
|
|
|
struct lm92_data {
|
2014-04-13 04:17:16 +08:00
|
|
|
struct i2c_client *client;
|
2006-01-19 06:19:26 +08:00
|
|
|
struct mutex update_lock;
|
2005-04-17 06:20:36 +08:00
|
|
|
char valid; /* zero until following fields are valid */
|
|
|
|
unsigned long last_updated; /* in jiffies */
|
|
|
|
|
|
|
|
/* registers values */
|
2014-04-13 04:07:08 +08:00
|
|
|
s16 temp[t_num_regs]; /* index with enum temp_index */
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sysfs attributes and callback functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct lm92_data *lm92_update_device(struct device *dev)
|
|
|
|
{
|
2014-04-13 04:17:16 +08:00
|
|
|
struct lm92_data *data = dev_get_drvdata(dev);
|
|
|
|
struct i2c_client *client = data->client;
|
2014-04-13 04:07:08 +08:00
|
|
|
int i;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-19 06:19:26 +08:00
|
|
|
mutex_lock(&data->update_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-09-16 22:45:21 +08:00
|
|
|
if (time_after(jiffies, data->last_updated + HZ) ||
|
|
|
|
!data->valid) {
|
2005-04-17 06:20:36 +08:00
|
|
|
dev_dbg(&client->dev, "Updating lm92 data\n");
|
2014-04-13 04:07:08 +08:00
|
|
|
for (i = 0; i < t_num_regs; i++) {
|
|
|
|
data->temp[i] =
|
|
|
|
i2c_smbus_read_word_swapped(client, regs[i]);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
data->last_updated = jiffies;
|
|
|
|
data->valid = 1;
|
|
|
|
}
|
|
|
|
|
2006-01-19 06:19:26 +08:00
|
|
|
mutex_unlock(&data->update_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2018-12-11 06:02:13 +08:00
|
|
|
static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
|
2014-04-13 04:07:08 +08:00
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
|
|
|
struct lm92_data *data = lm92_update_device(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2018-12-11 06:02:13 +08:00
|
|
|
static ssize_t temp_store(struct device *dev,
|
|
|
|
struct device_attribute *devattr, const char *buf,
|
|
|
|
size_t count)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2014-04-13 04:07:08 +08:00
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
2014-04-13 04:17:16 +08:00
|
|
|
struct lm92_data *data = dev_get_drvdata(dev);
|
|
|
|
struct i2c_client *client = data->client;
|
2014-04-13 04:07:08 +08:00
|
|
|
int nr = attr->index;
|
|
|
|
long val;
|
|
|
|
int err;
|
2018-09-16 22:45:21 +08:00
|
|
|
|
2014-04-13 04:07:08 +08:00
|
|
|
err = kstrtol(buf, 10, &val);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
mutex_lock(&data->update_lock);
|
|
|
|
data->temp[nr] = TEMP_TO_REG(val);
|
|
|
|
i2c_smbus_write_word_swapped(client, regs[nr], data->temp[nr]);
|
|
|
|
mutex_unlock(&data->update_lock);
|
|
|
|
return count;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2014-04-13 04:07:08 +08:00
|
|
|
|
2018-12-11 06:02:13 +08:00
|
|
|
static ssize_t temp_hyst_show(struct device *dev,
|
2014-04-13 04:07:08 +08:00
|
|
|
struct device_attribute *devattr, char *buf)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2014-04-13 04:07:08 +08:00
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
2005-04-17 06:20:36 +08:00
|
|
|
struct lm92_data *data = lm92_update_device(dev);
|
2018-09-16 22:45:21 +08:00
|
|
|
|
2014-04-13 04:07:08 +08:00
|
|
|
return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index])
|
|
|
|
- TEMP_FROM_REG(data->temp[t_hyst]));
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2014-04-13 04:07:08 +08:00
|
|
|
|
2016-12-22 20:04:54 +08:00
|
|
|
static ssize_t temp1_min_hyst_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct lm92_data *data = lm92_update_device(dev);
|
2018-09-16 22:45:21 +08:00
|
|
|
|
2014-04-13 04:07:08 +08:00
|
|
|
return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[t_min])
|
|
|
|
+ TEMP_FROM_REG(data->temp[t_hyst]));
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2018-12-11 06:02:13 +08:00
|
|
|
static ssize_t temp_hyst_store(struct device *dev,
|
|
|
|
struct device_attribute *devattr,
|
|
|
|
const char *buf, size_t count)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2014-04-13 04:07:08 +08:00
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
|
2014-04-13 04:17:16 +08:00
|
|
|
struct lm92_data *data = dev_get_drvdata(dev);
|
|
|
|
struct i2c_client *client = data->client;
|
2012-01-15 13:04:18 +08:00
|
|
|
long val;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = kstrtol(buf, 10, &val);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-08-05 10:08:31 +08:00
|
|
|
val = clamp_val(val, -120000, 220000);
|
2006-01-19 06:19:26 +08:00
|
|
|
mutex_lock(&data->update_lock);
|
2018-09-16 22:45:21 +08:00
|
|
|
data->temp[t_hyst] =
|
2014-08-05 10:08:31 +08:00
|
|
|
TEMP_TO_REG(TEMP_FROM_REG(data->temp[attr->index]) - val);
|
2011-11-04 19:00:47 +08:00
|
|
|
i2c_smbus_write_word_swapped(client, LM92_REG_TEMP_HYST,
|
2014-08-05 10:08:31 +08:00
|
|
|
data->temp[t_hyst]);
|
2006-01-19 06:19:26 +08:00
|
|
|
mutex_unlock(&data->update_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2016-12-22 20:04:54 +08:00
|
|
|
static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
|
2012-01-15 13:04:18 +08:00
|
|
|
char *buf)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct lm92_data *data = lm92_update_device(dev);
|
2018-09-16 22:45:21 +08:00
|
|
|
|
2014-04-13 04:07:08 +08:00
|
|
|
return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->temp[t_input]));
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2018-12-11 06:02:13 +08:00
|
|
|
static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
|
2008-01-06 22:25:52 +08:00
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
int bitnr = to_sensor_dev_attr(attr)->index;
|
|
|
|
struct lm92_data *data = lm92_update_device(dev);
|
2014-04-13 04:07:08 +08:00
|
|
|
return sprintf(buf, "%d\n", (data->temp[t_input] >> bitnr) & 1);
|
2008-01-06 22:25:52 +08:00
|
|
|
}
|
|
|
|
|
2018-12-11 06:02:13 +08:00
|
|
|
static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, t_input);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, t_crit);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, temp_hyst, t_crit);
|
|
|
|
static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, t_min);
|
2016-12-22 20:04:54 +08:00
|
|
|
static DEVICE_ATTR_RO(temp1_min_hyst);
|
2018-12-11 06:02:13 +08:00
|
|
|
static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, t_max);
|
|
|
|
static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, temp_hyst, t_max);
|
2016-12-22 20:04:54 +08:00
|
|
|
static DEVICE_ATTR_RO(alarms);
|
2018-12-11 06:02:13 +08:00
|
|
|
static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 2);
|
|
|
|
static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 0);
|
|
|
|
static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 1);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Detection and registration
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void lm92_init_client(struct i2c_client *client)
|
|
|
|
{
|
|
|
|
u8 config;
|
|
|
|
|
|
|
|
/* Start the conversions if needed */
|
|
|
|
config = i2c_smbus_read_byte_data(client, LM92_REG_CONFIG);
|
|
|
|
if (config & 0x01)
|
|
|
|
i2c_smbus_write_byte_data(client, LM92_REG_CONFIG,
|
|
|
|
config & 0xFE);
|
|
|
|
}
|
|
|
|
|
2014-04-13 04:17:16 +08:00
|
|
|
static struct attribute *lm92_attrs[] = {
|
2014-04-13 04:07:08 +08:00
|
|
|
&sensor_dev_attr_temp1_input.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_temp1_crit.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_temp1_min.dev_attr.attr,
|
2006-09-25 03:14:35 +08:00
|
|
|
&dev_attr_temp1_min_hyst.attr,
|
2014-04-13 04:07:08 +08:00
|
|
|
&sensor_dev_attr_temp1_max.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
|
2006-09-25 03:14:35 +08:00
|
|
|
&dev_attr_alarms.attr,
|
2008-01-06 22:25:52 +08:00
|
|
|
&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
|
2006-09-25 03:14:35 +08:00
|
|
|
NULL
|
|
|
|
};
|
2014-04-13 04:17:16 +08:00
|
|
|
ATTRIBUTE_GROUPS(lm92);
|
2006-09-25 03:14:35 +08:00
|
|
|
|
2008-07-17 01:30:15 +08:00
|
|
|
/* Return 0 if detection is successful, -ENODEV otherwise */
|
2009-12-15 04:17:23 +08:00
|
|
|
static int lm92_detect(struct i2c_client *new_client,
|
2008-07-17 01:30:15 +08:00
|
|
|
struct i2c_board_info *info)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-07-17 01:30:15 +08:00
|
|
|
struct i2c_adapter *adapter = new_client->adapter;
|
2009-12-10 03:35:57 +08:00
|
|
|
u8 config;
|
|
|
|
u16 man_id;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
|
|
|
|
| I2C_FUNC_SMBUS_WORD_DATA))
|
2008-07-17 01:30:15 +08:00
|
|
|
return -ENODEV;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-12-10 03:35:57 +08:00
|
|
|
config = i2c_smbus_read_byte_data(new_client, LM92_REG_CONFIG);
|
|
|
|
man_id = i2c_smbus_read_word_data(new_client, LM92_REG_MAN_ID);
|
|
|
|
|
|
|
|
if ((config & 0xe0) == 0x00 && man_id == 0x0180)
|
|
|
|
pr_info("lm92: Found National Semiconductor LM92 chip\n");
|
|
|
|
else
|
|
|
|
return -ENODEV;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-07-17 01:30:15 +08:00
|
|
|
strlcpy(info->type, "lm92", I2C_NAME_SIZE);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-14 00:02:22 +08:00
|
|
|
static int lm92_probe(struct i2c_client *new_client)
|
2008-07-17 01:30:15 +08:00
|
|
|
{
|
2014-04-13 04:17:16 +08:00
|
|
|
struct device *hwmon_dev;
|
2008-07-17 01:30:15 +08:00
|
|
|
struct lm92_data *data;
|
|
|
|
|
2012-06-03 00:58:11 +08:00
|
|
|
data = devm_kzalloc(&new_client->dev, sizeof(struct lm92_data),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
2008-07-17 01:30:15 +08:00
|
|
|
|
2014-04-13 04:17:16 +08:00
|
|
|
data->client = new_client;
|
2006-01-19 06:19:26 +08:00
|
|
|
mutex_init(&data->update_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Initialize the chipset */
|
|
|
|
lm92_init_client(new_client);
|
|
|
|
|
2014-04-13 04:17:16 +08:00
|
|
|
hwmon_dev = devm_hwmon_device_register_with_groups(&new_client->dev,
|
|
|
|
new_client->name,
|
|
|
|
data, lm92_groups);
|
|
|
|
return PTR_ERR_OR_ZERO(hwmon_dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Module and driver stuff
|
|
|
|
*/
|
|
|
|
|
2008-07-17 01:30:15 +08:00
|
|
|
static const struct i2c_device_id lm92_id[] = {
|
2018-03-22 22:37:44 +08:00
|
|
|
{ "lm92", lm92 },
|
|
|
|
{ "max6635", max6635 },
|
2008-07-17 01:30:15 +08:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(i2c, lm92_id);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static struct i2c_driver lm92_driver = {
|
2008-07-17 01:30:15 +08:00
|
|
|
.class = I2C_CLASS_HWMON,
|
2005-11-27 03:37:41 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "lm92",
|
|
|
|
},
|
2020-08-14 00:02:22 +08:00
|
|
|
.probe_new = lm92_probe,
|
2008-07-17 01:30:15 +08:00
|
|
|
.id_table = lm92_id,
|
|
|
|
.detect = lm92_detect,
|
2009-12-15 04:17:25 +08:00
|
|
|
.address_list = normal_i2c,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2012-01-20 15:38:18 +08:00
|
|
|
module_i2c_driver(lm92_driver);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-01-30 03:40:08 +08:00
|
|
|
MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
|
2005-04-17 06:20:36 +08:00
|
|
|
MODULE_DESCRIPTION("LM92/MAX6635 driver");
|
|
|
|
MODULE_LICENSE("GPL");
|