Release 4.7 drivers/hwmon/ds1621.c
/*
* ds1621.c - Part of lm_sensors, Linux kernel modules for hardware
* monitoring
* Christian W. Zuckschwerdt <zany@triq.net> 2000-11-23
* based on lm75.c by Frodo Looijaard <frodol@dds.nl>
* Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
* the help of Jean Delvare <jdelvare@suse.de>
*
* The DS1621 device is a digital temperature/thermometer with 9-bit
* resolution, a thermal alarm output (Tout), and user-defined minimum
* and maximum temperature thresholds (TH and TL).
*
* The DS1625, DS1631, DS1721, and DS1731 are pin compatible with the DS1621
* and similar in operation, with slight variations as noted in the device
* datasheets (please refer to www.maximintegrated.com for specific
* device information).
*
* Since the DS1621 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.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.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/sysfs.h>
#include <linux/kernel.h>
/* Supported devices */
enum chips { ds1621, ds1625, ds1631, ds1721, ds1731 };
/* Insmod parameters */
static int polarity = -1;
module_param(polarity, int, 0);
MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low");
/*
* The Configuration/Status register
*
* - DS1621:
* 7 6 5 4 3 2 1 0
* |Done|THF |TLF |NVB | X | X |POL |1SHOT|
*
* - DS1625:
* 7 6 5 4 3 2 1 0
* |Done|THF |TLF |NVB | 1 | 0 |POL |1SHOT|
*
* - DS1631, DS1731:
* 7 6 5 4 3 2 1 0
* |Done|THF |TLF |NVB | R1 | R0 |POL |1SHOT|
*
* - DS1721:
* 7 6 5 4 3 2 1 0
* |Done| X | X | U | R1 | R0 |POL |1SHOT|
*
* Where:
* - 'X' is Reserved
* - 'U' is Undefined
*/
#define DS1621_REG_CONFIG_NVB 0x10
#define DS1621_REG_CONFIG_RESOL 0x0C
#define DS1621_REG_CONFIG_POLARITY 0x02
#define DS1621_REG_CONFIG_1SHOT 0x01
#define DS1621_REG_CONFIG_DONE 0x80
#define DS1621_REG_CONFIG_RESOL_SHIFT 2
/* ds1721 conversion rates: {C/LSB, time(ms), resolution bit setting} */
static const unsigned short ds1721_convrates[] = {
94, /* 9-bits (0.5, 93.75, RES[0..1] = 0 */
188, /* 10-bits (0.25, 187.5, RES[0..1] = 1 */
375, /* 11-bits (0.125, 375, RES[0..1] = 2 */
750, /* 12-bits (0.0625, 750, RES[0..1] = 3 */
};
#define DS1621_CONVERSION_MAX 750
#define DS1625_CONVERSION_MAX 500
#define DS1621_TEMP_MAX 125000
#define DS1621_TEMP_MIN (-55000)
/* The DS1621 temperature registers */
static const u8 DS1621_REG_TEMP[3] = {
0xAA, /* input, word, RO */
0xA2, /* min, word, RW */
0xA1, /* max, word, RW */
};
#define DS1621_REG_CONF 0xAC
/* byte, RW */
#define DS1621_COM_START 0xEE
/* no data */
#define DS1721_COM_START 0x51
/* no data */
#define DS1621_COM_STOP 0x22
/* no data */
/* The DS1621 configuration register */
#define DS1621_ALARM_TEMP_HIGH 0x40
#define DS1621_ALARM_TEMP_LOW 0x20
/* Conversions */
#define ALARMS_FROM_REG(val) ((val) & \
(DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))
/* Each client has this additional data */
struct ds1621_data {
struct i2c_client *client;
struct mutex update_lock;
char valid; /* !=0 if following fields are valid */
unsigned long last_updated; /* In jiffies */
enum chips kind; /* device type */
u16 temp[3]; /* Register values, word */
u8 conf; /* Register encoding, combined */
u8 zbits; /* Resolution encoded as number of
* zero bits */
u16 update_interval; /* Conversion rate in milliseconds */
};
static inline int DS1621_TEMP_FROM_REG(u16 reg)
{
return DIV_ROUND_CLOSEST(((s16)reg / 16) * 625, 10);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
robert coulson | robert coulson | 27 | 100.00% | 1 | 100.00% |
| Total | 27 | 100.00% | 1 | 100.00% |
/*
* TEMP: 0.001C/bit (-55C to +125C)
* REG:
* - 1621, 1625: 0.5C/bit, 7 zero-bits
* - 1631, 1721, 1731: 0.0625C/bit, 4 zero-bits
*/
static inline u16 DS1621_TEMP_TO_REG(long temp, u8 zbits)
{
temp = clamp_val(temp, DS1621_TEMP_MIN, DS1621_TEMP_MAX);
temp = DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits;
return temp;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
robert coulson | robert coulson | 29 | 60.42% | 1 | 50.00% |
guenter roeck | guenter roeck | 19 | 39.58% | 1 | 50.00% |
| Total | 48 | 100.00% | 2 | 100.00% |
static void ds1621_init_client(struct ds1621_data *data,
struct i2c_client *client)
{
u8 conf, new_conf, sreg, resol;
new_conf = conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
/* switch to continuous conversion mode */
new_conf &= ~DS1621_REG_CONFIG_1SHOT;
/* setup output polarity */
if (polarity == 0)
new_conf &= ~DS1621_REG_CONFIG_POLARITY;
else if (polarity == 1)
new_conf |= DS1621_REG_CONFIG_POLARITY;
if (conf != new_conf)
i2c_smbus_write_byte_data(client, DS1621_REG_CONF, new_conf);
switch (data->kind) {
case ds1625:
data->update_interval = DS1625_CONVERSION_MAX;
data->zbits = 7;
sreg = DS1621_COM_START;
break;
case ds1631:
case ds1721:
case ds1731:
resol = (new_conf & DS1621_REG_CONFIG_RESOL) >>
DS1621_REG_CONFIG_RESOL_SHIFT;
data->update_interval = ds1721_convrates[resol];
data->zbits = 7 - resol;
sreg = DS1721_COM_START;
break;
default:
data->update_interval = DS1621_CONVERSION_MAX;
data->zbits = 7;
sreg = DS1621_COM_START;
break;
}
/* start conversion */
i2c_smbus_write_byte(client, sreg);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
robert coulson | robert coulson | 73 | 41.71% | 3 | 33.33% |
aurelien jarno | aurelien jarno | 55 | 31.43% | 1 | 11.11% |
guenter roeck | guenter roeck | 25 | 14.29% | 2 | 22.22% |
jean delvare | jean delvare | 21 | 12.00% | 2 | 22.22% |
steven cole | steven cole | 1 | 0.57% | 1 | 11.11% |
| Total | 175 | 100.00% | 9 | 100.00% |
static struct ds1621_data *ds1621_update_client(struct device *dev)
{
struct ds1621_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;
u8 new_conf;
mutex_lock(&data->update_lock);
if (time_after(jiffies, data->last_updated + data->update_interval) ||
!data->valid) {
int i;
dev_dbg(&client->dev, "Starting ds1621 update\n");
data->conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
for (i = 0; i < ARRAY_SIZE(data->temp); i++)
data->temp[i] = i2c_smbus_read_word_swapped(client,
DS1621_REG_TEMP[i]);
/* reset alarms if necessary */
new_conf = data->conf;
if (data->temp[0] > data->temp[1]) /* input > min */
new_conf &= ~DS1621_ALARM_TEMP_LOW;
if (data->temp[0] < data->temp[2]) /* input < max */
new_conf &= ~DS1621_ALARM_TEMP_HIGH;
if (data->conf != new_conf)
i2c_smbus_write_byte_data(client, DS1621_REG_CONF,
new_conf);
data->last_updated = jiffies;
data->valid = 1;
}
mutex_unlock(&data->update_lock);
return data;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
jean delvare | jean delvare | 197 | 91.63% | 4 | 57.14% |
aurelien jarno | aurelien jarno | 8 | 3.72% | 1 | 14.29% |
guenter roeck | guenter roeck | 7 | 3.26% | 1 | 14.29% |
robert coulson | robert coulson | 3 | 1.40% | 1 | 14.29% |
| Total | 215 | 100.00% | 7 | 100.00% |
static ssize_t show_temp(struct device *dev, struct device_attribute *da,
char *buf)
{
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
struct ds1621_data *data = ds1621_update_client(dev);
return sprintf(buf, "%d\n",
DS1621_TEMP_FROM_REG(data->temp[attr->index]));
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
aurelien jarno | aurelien jarno | 37 | 61.67% | 1 | 20.00% |
jean delvare | jean delvare | 18 | 30.00% | 2 | 40.00% |
yani ioannou | yani ioannou | 4 | 6.67% | 1 | 20.00% |
robert coulson | robert coulson | 1 | 1.67% | 1 | 20.00% |
| Total | 60 | 100.00% | 5 | 100.00% |
static ssize_t set_temp(struct device *dev, struct device_attribute *da,
const char *buf, size_t count)
{
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
struct ds1621_data *data = dev_get_drvdata(dev);
long val;
int err;
err = kstrtol(buf, 10, &val);
if (err)
return err;
mutex_lock(&data->update_lock);
data->temp[attr->index] = DS1621_TEMP_TO_REG(val, data->zbits);
i2c_smbus_write_word_swapped(data->client, DS1621_REG_TEMP[attr->index],
data->temp[attr->index]);
mutex_unlock(&data->update_lock);
return count;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
jean delvare | jean delvare | 97 | 75.19% | 3 | 37.50% |
guenter roeck | guenter roeck | 29 | 22.48% | 3 | 37.50% |
aurelien jarno | aurelien jarno | 2 | 1.55% | 1 | 12.50% |
robert coulson | robert coulson | 1 | 0.78% | 1 | 12.50% |
| Total | 129 | 100.00% | 8 | 100.00% |
static ssize_t show_alarms(struct device *dev, struct device_attribute *da,
char *buf)
{
struct ds1621_data *data = ds1621_update_client(dev);
return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf));
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
jean delvare | jean delvare | 45 | 100.00% | 1 | 100.00% |
| Total | 45 | 100.00% | 1 | 100.00% |
static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
char *buf)
{
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
struct ds1621_data *data = ds1621_update_client(dev);
return sprintf(buf, "%d\n", !!(data->conf & attr->index));
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
jean delvare | jean delvare | 60 | 100.00% | 1 | 100.00% |
| Total | 60 | 100.00% | 1 | 100.00% |
static ssize_t show_convrate(struct device *dev, struct device_attribute *da,
char *buf)
{
struct ds1621_data *data = dev_get_drvdata(dev);
return scnprintf(buf, PAGE_SIZE, "%hu\n", data->update_interval);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
robert coulson | robert coulson | 42 | 95.45% | 1 | 50.00% |
guenter roeck | guenter roeck | 2 | 4.55% | 1 | 50.00% |
| Total | 44 | 100.00% | 2 | 100.00% |
static ssize_t set_convrate(struct device *dev, struct device_attribute *da,
const char *buf, size_t count)
{
struct ds1621_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;
unsigned long convrate;
s32 err;
int resol = 0;
err = kstrtoul(buf, 10, &convrate);
if (err)
return err;
/* Convert rate into resolution bits */
while (resol < (ARRAY_SIZE(ds1721_convrates) - 1) &&
convrate > ds1721_convrates[resol])
resol++;
mutex_lock(&data->update_lock);
data->conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
data->conf &= ~DS1621_REG_CONFIG_RESOL;
data->conf |= (resol << DS1621_REG_CONFIG_RESOL_SHIFT);
i2c_smbus_write_byte_data(client, DS1621_REG_CONF, data->conf);
data->update_interval = ds1721_convrates[resol];
data->zbits = 7 - resol;
mutex_unlock(&data->update_lock);
return count;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
robert coulson | robert coulson | 166 | 95.95% | 2 | 66.67% |
guenter roeck | guenter roeck | 7 | 4.05% | 1 | 33.33% |
| Total | 173 | 100.00% | 3 | 100.00% |
static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_convrate,
set_convrate);
static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp, set_temp, 1);
static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, set_temp, 2);
static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
DS1621_ALARM_TEMP_LOW);
static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
DS1621_ALARM_TEMP_HIGH);
static struct attribute *ds1621_attributes[] = {
&sensor_dev_attr_temp1_input.dev_attr.attr,
&sensor_dev_attr_temp1_min.dev_attr.attr,
&sensor_dev_attr_temp1_max.dev_attr.attr,
&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
&dev_attr_alarms.attr,
&dev_attr_update_interval.attr,
NULL
};
static umode_t ds1621_attribute_visible(struct kobject *kobj,
struct attribute *attr, int index)
{
struct device *dev = container_of(kobj, struct device, kobj);
struct ds1621_data *data = dev_get_drvdata(dev);
if (attr == &dev_attr_update_interval.attr)
if (data->kind == ds1621 || data->kind == ds1625)
/* shhh, we're hiding update_interval */
return 0;
return attr->mode;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
robert coulson | robert coulson | 74 | 97.37% | 2 | 66.67% |
guenter roeck | guenter roeck | 2 | 2.63% | 1 | 33.33% |
| Total | 76 | 100.00% | 3 | 100.00% |
static const struct attribute_group ds1621_group = {
.attrs = ds1621_attributes,
.is_visible = ds1621_attribute_visible
};
__ATTRIBUTE_GROUPS(ds1621);
static int ds1621_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct ds1621_data *data;
struct device *hwmon_dev;
data = devm_kzalloc(&client->dev, sizeof(struct ds1621_data),
GFP_KERNEL);
if (!data)
return -ENOMEM;
mutex_init(&data->update_lock);
data->kind = id->driver_data;
data->client = client;
/* Initialize the DS1621 chip */
ds1621_init_client(data, client);
hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
client->name, data,
ds1621_groups);
return PTR_ERR_OR_ZERO(hwmon_dev);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
jean delvare | jean delvare | 54 | 50.00% | 3 | 27.27% |
guenter roeck | guenter roeck | 28 | 25.93% | 3 | 27.27% |
aurelien jarno | aurelien jarno | 11 | 10.19% | 1 | 9.09% |
robert coulson | robert coulson | 8 | 7.41% | 1 | 9.09% |
mark m. hoffman | mark m. hoffman | 5 | 4.63% | 1 | 9.09% |
fengguang wu | fengguang wu | 1 | 0.93% | 1 | 9.09% |
tony jones | tony jones | 1 | 0.93% | 1 | 9.09% |
| Total | 108 | 100.00% | 11 | 100.00% |
static const struct i2c_device_id ds1621_id[] = {
{ "ds1621", ds1621 },
{ "ds1625", ds1625 },
{ "ds1631", ds1631 },
{ "ds1721", ds1721 },
{ "ds1731", ds1731 },
{ }
};
MODULE_DEVICE_TABLE(i2c, ds1621_id);
/* This is the driver that will be inserted */
static struct i2c_driver ds1621_driver = {
.class = I2C_CLASS_HWMON,
.driver = {
.name = "ds1621",
},
.probe = ds1621_probe,
.id_table = ds1621_id,
};
module_i2c_driver(ds1621_driver);
MODULE_AUTHOR("Christian W. Zuckschwerdt <zany@triq.net>");
MODULE_DESCRIPTION("DS1621 driver");
MODULE_LICENSE("GPL");
Overall Contributors
| Person | Tokens | Prop | Commits | CommitProp |
jean delvare | jean delvare | 694 | 41.21% | 10 | 32.26% |
robert coulson | robert coulson | 548 | 32.54% | 5 | 16.13% |
aurelien jarno | aurelien jarno | 277 | 16.45% | 2 | 6.45% |
guenter roeck | guenter roeck | 132 | 7.84% | 5 | 16.13% |
mark m. hoffman | mark m. hoffman | 14 | 0.83% | 1 | 3.23% |
ingo molnar | ingo molnar | 4 | 0.24% | 1 | 3.23% |
greg kroah-hartman | greg kroah-hartman | 4 | 0.24% | 1 | 3.23% |
yani ioannou | yani ioannou | 4 | 0.24% | 1 | 3.23% |
alexey dobriyan | alexey dobriyan | 3 | 0.18% | 1 | 3.23% |
fengguang wu | fengguang wu | 1 | 0.06% | 1 | 3.23% |
tony jones | tony jones | 1 | 0.06% | 1 | 3.23% |
steven cole | steven cole | 1 | 0.06% | 1 | 3.23% |
axel lin | axel lin | 1 | 0.06% | 1 | 3.23% |
| Total | 1684 | 100.00% | 31 | 100.00% |
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.