Skip to contentSkip to conversation

DS1621 support for kernel 2.6 (new patch)

DISCUSSION Aurelien Jarno 2 participants 4 messages May 19, 2005

lore
Share & tools
lore
#0 lore raw

Hi all,

In the latest patch I sent, which adds support for DS1621 to the 2.6 kernels, there is a line which contains a call to the printk function. I used it for debugging purpose and I forgot to remove it. This line can be safely removed. Please find attached the new patch which fixes that.

Aurelien

Signature · 503 lines
  .''`.  Aurelien Jarno	              GPG: 1024D/F1BCDB73
 : :' :  Debian GNU/Linux developer | Electrical Engineering Student 
 `. `'   [email protected]         | [email protected]
   `-    people.debian.org/~aurel32 | www.aurel32.net
-------------- next part --------------
diff -urN linux-2.6.3.orig/drivers/i2c/chips/ds1621.c linux-2.6.3/drivers/i2c/chips/ds1621.c
--- linux-2.6.3.orig/drivers/i2c/chips/ds1621.c	1970-01-01 01:00:00.000000000 +0100
+++ linux-2.6.3/drivers/i2c/chips/ds1621.c	2004-03-05 23:17:50.000000000 +0100
@@ -0,0 +1,462 @@
+/*
+    ds1621.c - Part of lm_sensors, Linux kernel modules for hardware
+             monitoring
+    Christian W. Zuckschwerdt  <[email protected]>  2000-11-23
+    based on lm75.c by Frodo Looijaard <[email protected]>
+
+    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.
+*/
+
+/* Supports DS1621. See doc/chips/ds1621 for details */
+
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/i2c.h>
+#include <linux/i2c-sensor.h>
+#include <linux/init.h>
+
+/* Addresses to scan */
+static unsigned short normal_i2c[] = { I2C_CLIENT_END };
+static unsigned short normal_i2c_range[] = { 0x48, 0x4f, I2C_CLIENT_END };
+static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
+static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
+
+/* Insmod parameters */
+SENSORS_INSMOD_1(ds1621);
+
+/* Many DS1621 constants specified below */
+
+/* Config register used for detection         */
+/*  7    6    5    4    3    2    1    0      */
+/* |Done|THF |TLF |NVB | 1  | 0  |POL |1SHOT| */
+#define DS1621_REG_CONFIG_MASK 		0x0C
+#define DS1621_REG_CONFIG_VAL 		0x08
+#define DS1621_REG_CONFIG_POLARITY 	0x02
+#define DS1621_REG_CONFIG_1SHOT 	0x01
+#define DS1621_REG_CONFIG_DONE 		0x80
+
+/* Note: the done bit is always unset if continuous conversion is in progress.
+         We need to stop the continuous conversion or switch to single shot
+         before this bit becomes available!
+ */
+
+/* The DS1621 registers */
+#define DS1621_REG_TEMP 	0xAA /* word, RO */
+#define DS1621_REG_TEMP_OVER 	0xA1 /* word, RW */
+#define DS1621_REG_TEMP_HYST 	0xA2 /* word, RW -- it's a low temp trigger */
+#define DS1621_REG_CONF 	0xAC /* byte, RW */
+#define DS1621_REG_TEMP_COUNTER 0xA8 /* byte, RO */
+#define DS1621_REG_TEMP_SLOPE 	0xA9 /* byte, RO */
+#define DS1621_COM_START 	0xEE /* 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. Rounding and limit checking is only done on the TO_REG
+   variants. Note that you should be a bit careful with which arguments
+   these macros are called: arguments may be evaluated more than once.
+   Fixing this is just not worth it. */
+#define TEMP_FROM_REG(val) ((((val & 0x7fff) >> 7) * 500) | \
+                            ((val & 0x8000)?-256:0))
+#define TEMP_TO_REG(val)   (SENSORS_LIMIT((val<0 ? (0x200+((val)/500))<<7 : \
+                                          (((val) + 2) / 5) << 7),0,0xffff))
+#define ALARMS_FROM_REG(val) (!(!((val) & \
+                              (DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))))
+#define ITEMP_FROM_REG(val) (((((val & 0x7fff) >> 8)) | \
+                            ((val & 0x8000)?-256:0)) * 100)
+
+/* Each client has this additional data */
+struct ds1621_data {
+	struct semaphore update_lock;
+	char valid;		/* !=0 if following fields are valid */
+	unsigned long last_updated;	/* In jiffies */
+
+	u16 temp, temp_over, temp_hyst;	/* Register values, word */
+	u8 conf;			/* Register encoding, combined */
+
+	char enable;	/* !=0 if we're expected to restart the conversion */
+	u8 temp_int, temp_counter, temp_slope;	/* Register values, byte */
+};
+
+static int ds1621_attach_adapter(struct i2c_adapter *adapter);
+static int ds1621_detect(struct i2c_adapter *adapter, int address,
+			 int kind);
+static void ds1621_init_client(struct i2c_client *client);
+static int ds1621_detach_client(struct i2c_client *client);
+static void ds1621_update_client(struct i2c_client *client);
+
+/* This is the driver that will be inserted */
+static struct i2c_driver ds1621_driver = {
+	.owner		= THIS_MODULE,
+	.name		= "ds1621",
+	.id		= I2C_DRIVERID_DS1621,
+	.flags		= I2C_DF_NOTIFY,
+	.attach_adapter	= ds1621_attach_adapter,
+	.detach_client	= ds1621_detach_client,
+};
+
+static int ds1621_id = 0;
+
+static u16 swap_bytes(u16 val)
+{
+	return (val >> 8) | (val << 8);
+}
+
+/* All registers are word-sized, except for the configuration register.
+   DS1621 uses a high-byte first convention, which is exactly opposite to
+   the usual practice. */
+static int ds1621_read_value(struct i2c_client *client, u8 reg)
+{
+	if ((reg = DS1621_REG_CONF) || (reg = DS1621_REG_TEMP_COUNTER)
+	    || (reg = DS1621_REG_TEMP_SLOPE))
+		return i2c_smbus_read_byte_data(client, reg);
+	else
+		return swap_bytes(i2c_smbus_read_word_data(client, reg));
+}
+
+/* All registers are word-sized, except for the configuration register.
+   DS1621 uses a high-byte first convention, which is exactly opposite to
+   the usual practice. */
+static int ds1621_write_value(struct i2c_client *client, u8 reg, u16 value)
+{
+	if ( (reg = DS1621_COM_START) || (reg = DS1621_COM_STOP) )
+		return i2c_smbus_write_byte(client, reg);
+	else
+	if ((reg = DS1621_REG_CONF) || (reg = DS1621_REG_TEMP_COUNTER)
+	    || (reg = DS1621_REG_TEMP_SLOPE))
+		return i2c_smbus_write_byte_data(client, reg, value);
+	else
+		return i2c_smbus_write_word_data(client, reg,
+						 swap_bytes(value));
+}
+
+static void ds1621_init_client(struct i2c_client *client)
+{
+	struct ds1621_data *data = i2c_get_clientdata(client);
+	int reg = ds1621_read_value(client, DS1621_REG_CONF);
+	/* switch to continous conversion mode */
+	if(reg & DS1621_REG_CONFIG_1SHOT)
+		ds1621_write_value(client, DS1621_REG_CONF, 
+				   reg & ~ DS1621_REG_CONFIG_1SHOT);
+	/* start conversion */
+	ds1621_write_value(client, DS1621_COM_START, 0);
+	data->enable = 1;
+}
+
+#define show(value)     \
+static ssize_t show_##value(struct device *dev, char *buf)		\
+{									\
+        struct i2c_client *client = to_i2c_client(dev);			\
+	struct ds1621_data *data = i2c_get_clientdata(client);		\
+	ds1621_update_client(client);					\
+	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value));	\
+}
+
+show(temp);
+show(temp_hyst);
+show(temp_over);
+
+#define set_temp(suffix, value, reg) \
+static ssize_t set_temp_##suffix(struct device *dev, const char *buf,	\
+		        size_t count)					\
+{									\
+	struct i2c_client *client = to_i2c_client(dev); 		\
+	struct ds1621_data *data = i2c_get_clientdata(client); 		\
+	data->value = TEMP_TO_REG(simple_strtoul(buf, NULL, 10)); 	\
+	ds1621_write_value(client, reg, data->value); 			\
+	return count; 							\
+}
+
+set_temp(hyst, temp_hyst, DS1621_REG_TEMP_HYST);
+set_temp(over, temp_over, DS1621_REG_TEMP_OVER);
+
+static ssize_t show_polarity(struct device *dev, char *buf)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+        struct ds1621_data *data = i2c_get_clientdata(client);
+	ds1621_update_client(client);
+	return sprintf(buf, "%d\n", !(!(data->conf & DS1621_REG_CONFIG_POLARITY)));
+}
+
+static ssize_t set_polarity(struct device *dev, const char *buf,
+			    size_t count)					
+{									
+	struct i2c_client *client = to_i2c_client(dev);	
+	struct ds1621_data *data = i2c_get_clientdata(client); 	
+	ds1621_update_client(client);
+	if (simple_strtoul(buf, NULL, 10)) 
+		data->conf |= DS1621_REG_CONFIG_POLARITY;
+	else
+		data->conf &= ~DS1621_REG_CONFIG_POLARITY;
+	ds1621_write_value(client, DS1621_REG_CONF, data->conf);
+	return count;
+}
+
+static ssize_t show_continuous(struct device *dev, char *buf)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+        struct ds1621_data *data = i2c_get_clientdata(client);
+	ds1621_update_client(client);
+	return sprintf(buf, "%d\n", !(data->conf & DS1621_REG_CONFIG_1SHOT));
+}
+
+static ssize_t set_continuous(struct device *dev, const char *buf,
+			      size_t count)					
+{		 							
+	struct i2c_client *client = to_i2c_client(dev);	
+	struct ds1621_data *data = i2c_get_clientdata(client); 	
+	ds1621_update_client(client);
+	if (simple_strtoul(buf, NULL, 10)) 
+		data->conf &= ~DS1621_REG_CONFIG_1SHOT;
+	else
+		data->conf |= DS1621_REG_CONFIG_1SHOT;
+	ds1621_write_value(client, DS1621_REG_CONF, data->conf);
+	if (data->enable)
+		ds1621_write_value(client, DS1621_COM_START, 0);
+	return count;
+}
+
+static ssize_t show_enable(struct device *dev, char *buf)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+        struct ds1621_data *data = i2c_get_clientdata(client);
+	ds1621_update_client(client);
+	return sprintf(buf, "%d\n", !(data->conf & DS1621_REG_CONFIG_DONE));
+}
+
+static ssize_t set_enable(struct device *dev, const char *buf,
+			  size_t count)					
+{									
+	/* If you really screw up your chip (like I did) this is */
+	/* sometimes needed to (re)start the continous conversion */
+	/* there is no data to read so this might hang your SMBus! */
+
+	struct i2c_client *client = to_i2c_client(dev);	
+	struct ds1621_data *data = i2c_get_clientdata(client); 	
+	if (simple_strtoul(buf, NULL, 10)) {
+		ds1621_write_value(client, DS1621_COM_START, 0);
+		data->enable = 1;
+	} else {
+		ds1621_write_value(client, DS1621_COM_STOP, 0);
+		data->enable = 0;
+	}
+	return count;
+}
+
+
+static ssize_t show_alarms(struct device *dev, char *buf)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+        struct ds1621_data *data = i2c_get_clientdata(client);
+	ds1621_update_client(client);
+	return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf));
+}
+
+static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
+static DEVICE_ATTR(continuous, S_IWUSR | S_IRUGO, show_continuous, set_continuous);
+static DEVICE_ATTR(enable, S_IWUSR | S_IRUGO, show_enable, set_enable);
+static DEVICE_ATTR(temp_input1, S_IRUGO , show_temp, NULL);
+static DEVICE_ATTR(temp_hyst1, S_IWUSR | S_IRUGO , show_temp_hyst, set_temp_hyst);
+static DEVICE_ATTR(temp_max1, S_IWUSR | S_IRUGO, show_temp_over, set_temp_over);
+static DEVICE_ATTR(polarity, S_IWUSR | S_IRUGO, show_polarity, set_polarity);
+
+
+static int ds1621_attach_adapter(struct i2c_adapter *adapter)
+{
+	return i2c_detect(adapter, &addr_data, ds1621_detect);
+}
+
+/* This function is called by i2c_detect */
+int ds1621_detect(struct i2c_adapter *adapter, int address,
+		  int kind)
+{
+	int conf;
+	struct i2c_client *new_client;
+	struct ds1621_data *data;
+	int err = 0;
+	const char *client_name = "";
+
+	/* Make sure we aren't probing the ISA bus!! This is just a safety check
+	   at this moment; i2c_detect really won't call us. */
+	if (i2c_is_isa_adapter(adapter)) {
+		dev_dbg(&adapter->dev, 
+		        "ds1621.o: ds1621_detect called for an ISA bus adapter?!?\n");
+		return 0;
+	}
+
+	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
+				     I2C_FUNC_SMBUS_WORD_DATA))
+		    goto ERROR0;
+
+	/* OK. For now, we presume we have a valid client. We now create the
+	   client structure, even though we cannot fill it completely yet.
+	   But it allows us to access ds1621_{read,write}_value. */
+	if (!(new_client = kmalloc(sizeof(struct i2c_client) +
+				   sizeof(struct ds1621_data),
+				   GFP_KERNEL))) {
+		err = -ENOMEM;
+		goto ERROR1;
+	}
+        memset(new_client, 0, sizeof(struct i2c_client) +
+	       sizeof(struct ds1621_data));
+	
+	data = (struct ds1621_data *) (new_client + 1);
+	i2c_set_clientdata(new_client, data);
+	new_client->addr = address;
+	new_client->adapter = adapter;
+	new_client->driver = &ds1621_driver;
+	new_client->flags = 0;
+
+
+	/* Now, we do the remaining detection. It is lousy. */
+	if (kind < 0) {
+		conf = i2c_smbus_read_byte_data(new_client,
+						DS1621_REG_CONF);
+		if ((conf & DS1621_REG_CONFIG_MASK)
+		    != DS1621_REG_CONFIG_VAL)
+			goto ERROR2;
+	}
+
+	/* Determine the chip type - only one kind supported! */
+	if (kind <= 0)
+		kind = ds1621;
+
+	if (kind = ds1621) {
+		client_name = "ds1621";
+	} else {
+		dev_dbg(&adapter->dev,
+		        "ds1621.o: Internal error: unknown kind (%d)?!?",
+		        kind);
+		goto ERROR2;
+	}
+
+	/* Fill in remaining client fields and put it into the global list */
+	strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
+
+	new_client->id = ds1621_id++;
+	data->valid = 0;
+	init_MUTEX(&data->update_lock);
+
+	/* Tell the I2C layer a new client has arrived */
+	if ((err = i2c_attach_client(new_client)))
+		goto ERROR2;
+
+	/* Initialize the DS1621 chip */
+	ds1621_init_client(new_client);
+
+	/* Register sysfs hooks */
+        device_create_file(&new_client->dev, &dev_attr_alarms);
+        device_create_file(&new_client->dev, &dev_attr_continuous);
+        device_create_file(&new_client->dev, &dev_attr_enable);
+        device_create_file(&new_client->dev, &dev_attr_temp_input1);
+        device_create_file(&new_client->dev, &dev_attr_temp_hyst1);
+        device_create_file(&new_client->dev, &dev_attr_temp_max1);
+        device_create_file(&new_client->dev, &dev_attr_polarity);
+	
+	return 0;
+
+/* OK, this is not exactly good programming practice, usually. But it is
+   very code-efficient in this case. */
+      ERROR2:
+	kfree(new_client);
+      ERROR1:
+      ERROR0:
+	return err;
+}
+
+static int ds1621_detach_client(struct i2c_client *client)
+{
+	int err;
+
+	if ((err = i2c_detach_client(client))) {
+		dev_err(&client->dev,
+		        "ds1621.o: Client deregistration failed, client not detached.\n");
+		return err;
+	}
+
+	kfree(client);
+
+	return 0;
+}
+
+static void ds1621_update_client(struct i2c_client *client)
+{
+	struct ds1621_data *data = i2c_get_clientdata(client);
+	u8 new_conf;
+
+	down(&data->update_lock);
+
+	if ((jiffies - data->last_updated > HZ + HZ / 2) ||
+	    (jiffies < data->last_updated) || !data->valid) {
+
+		dev_dbg(&client->dev, "Starting ds1621 update\n");
+
+		data->conf = ds1621_read_value(client, DS1621_REG_CONF);
+
+		data->temp = ds1621_read_value(client, DS1621_REG_TEMP);
+		
+		data->temp_over = ds1621_read_value(client,
+		                                    DS1621_REG_TEMP_OVER);
+		data->temp_hyst = ds1621_read_value(client,
+						    DS1621_REG_TEMP_HYST);
+
+		/* wait for the DONE bit before reading extended values */
+		if (data->conf & DS1621_REG_CONFIG_DONE) {
+			data->temp_counter = ds1621_read_value(client,
+						     DS1621_REG_TEMP_COUNTER);
+			data->temp_slope = ds1621_read_value(client,
+						     DS1621_REG_TEMP_SLOPE);
+			data->temp_int = ITEMP_FROM_REG(data->temp);
+			/* restart the conversion */
+			if (data->enable)
+				ds1621_write_value(client, DS1621_COM_START, 0);
+		}
+
+		/* reset alarms if neccessary */
+		new_conf = data->conf;
+		if (data->temp < data->temp_over)
+			new_conf &= ~DS1621_ALARM_TEMP_HIGH;
+		if (data->temp > data->temp_hyst)
+			new_conf &= ~DS1621_ALARM_TEMP_LOW;
+		if (data->conf != new_conf)
+			ds1621_write_value(client, DS1621_REG_CONF,
+					   new_conf);
+
+		data->last_updated = jiffies;
+		data->valid = 1;
+	}
+
+	up(&data->update_lock);
+}
+
+static int __init ds1621_init(void)
+{
+	return i2c_add_driver(&ds1621_driver);
+}
+
+static void __exit ds1621_exit(void)
+{
+	i2c_del_driver(&ds1621_driver);
+}
+
+
+MODULE_AUTHOR("Christian W. Zuckschwerdt <[email protected]>");
+MODULE_DESCRIPTION("DS1621 driver");
+MODULE_LICENSE("GPL");
+
+module_init(ds1621_init);
+module_exit(ds1621_exit);
diff -urN linux-2.6.3.orig/drivers/i2c/chips/Kconfig linux-2.6.3/drivers/i2c/chips/Kconfig
--- linux-2.6.3.orig/drivers/i2c/chips/Kconfig	2004-02-18 04:58:10.000000000 +0100
+++ linux-2.6.3/drivers/i2c/chips/Kconfig	2004-03-05 23:18:06.000000000 +0100
@@ -33,6 +33,17 @@
 	  This driver can also be built as a module.  If so, the module
 	  will be called asb100.
 
+config SENSORS_DS1621
+      	tristate "Dallas Semiconductor DS1621 and DS1625"
+	depends on I2C && EXPERIMENTAL
+	select I2C_SENSOR
+	help
+	  If you say yes here you get support for Dallas Semiconductor
+	  DS1621 and DS1625 sensor chips. 
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called ds1621.
+
 config SENSORS_EEPROM
 	tristate "EEPROM (DIMM) reader"
 	depends on I2C && EXPERIMENTAL
diff -urN linux-2.6.3.orig/drivers/i2c/chips/Makefile linux-2.6.3/drivers/i2c/chips/Makefile
--- linux-2.6.3.orig/drivers/i2c/chips/Makefile	2004-02-18 05:00:01.000000000 +0100
+++ linux-2.6.3/drivers/i2c/chips/Makefile	2004-03-05 23:17:54.000000000 +0100
@@ -7,6 +7,7 @@
 obj-$(CONFIG_SENSORS_W83781D)	+= w83781d.o
 
 obj-$(CONFIG_SENSORS_ADM1021)	+= adm1021.o
+obj-$(CONFIG_SENSORS_DS1621)	+= ds1621.o
 obj-$(CONFIG_SENSORS_EEPROM)	+= eeprom.o
 obj-$(CONFIG_SENSORS_FSCHER)	+= fscher.o
 obj-$(CONFIG_SENSORS_GL518SM)	+= gl518sm.o
#1 lore raw

Hi Aurelien,

Sorry for the delay. See my comments inline.

+/* Supports DS1621. See doc/chips/ds1621 for details */

That comment doesn't make sense here, since doc/chips/ds1621 is not part of the Linux 2.6 tree.

+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/i2c.h>
+#include <linux/i2c-sensor.h>
+#include <linux/init.h>

See lm83.c (for example) for the prefered include order). Also, standard debugging stuff is missing here (see lm83.c too).

+/* Addresses to scan */
+static unsigned short normal_i2c[] = { I2C_CLIENT_END }; +static unsigned short normal_i2c_range[] = { 0x48, 0x4f, I2C_CLIENT_END }; +static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END }; +static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END }; +
+/* Insmod parameters */
+SENSORS_INSMOD_1(ds1621);
+
+/* Many DS1621 constants specified below */
+
+/* Config register used for detection */
+/* 7 6 5 4 3 2 1 0 */
+/* |Done|THF |TLF |NVB | 1 | 0 |POL |1SHOT| */

Please merge these comments into a single one.

+#define DS1621_REG_CONFIG_MASK 0x0C
+#define DS1621_REG_CONFIG_VAL 0x08
+#define DS1621_REG_CONFIG_POLARITY 0x02
+#define DS1621_REG_CONFIG_1SHOT 0x01
+#define DS1621_REG_CONFIG_DONE 0x80

Please use tabs and *only* tabs for alignements. In your patch, all constants seem to have a trailing white space before aligning tabs.

+#define TEMP_FROM_REG(val) ((((val & 0x7fff) >> 7) * 500) | \
+ ((val & 0x8000)?-256:0))
+#define TEMP_TO_REG(val) (SENSORS_LIMIT((val<0 ? (0x200+((val)/500))<<7 : \
+ (((val) + 2) / 5) << 7),0,0xffff))

I think that the defines in lm75.h would do the same, more cleanly.

+#define ALARMS_FROM_REG(val) (!(!((val) & \
+ (DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))))

The double negation makes this macro hardly readble. Care to reformulate?

+#define ITEMP_FROM_REG(val) (((((val & 0x7fff) >> 8)) | \
+ ((val & 0x8000)?-256:0)) * 100)

This one looks wrong. I'd say *1000, not *100. Anyway, this macro is used only to feed data->temp_int, which is never used as far as I can see. Looks like all this stuff is useless...Same for temp_counter and temp_slope?

As a more general advice, I'd suggest that you simplify the driver as much as possible. The DS1621 seems to be a really simple chip and I can hardly believe that the driver would be so big.

For example, I wonder why the "continuous" parameter should be configurable. All drivers work in continuous mode by default. Polarity should be left unchanged. Chip should not need to be restarted. The "I screw up the chip once" comment is a developper issue. Regular users have no way (hopefully) to screw up anything, so this parameter is likely to be useless.

I'll have to read the DS1621 (and possibly DS1625) datasheet to make sure I understand correctly how the chip(s) work, but I expect us to be able to simplify the driver much.

Thanks for you work. Please tell me how you do feel about my comments and suggestion, so that we can go on with this port.

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
#2 lore raw
In reply to Aurelien Jarno
Quoted 10 lines
Quoted 6 lines
(quoted diff) +2 -0
+#define ALARMS_FROM_REG(val) (!(!((val) & \
+                              (DS1621_ALARM_TEMP_HIGH |

DS1621_ALARM_TEMP_LOW))))

The double negation makes this macro hardly readble. Care to reformulate?

The double negation is used to convert a number equal to zero or different to zero to a number equal to 0 or 1.

Quoted 8 lines

I don't have a lot of idea to reformulate it, I used:

#define ALARMS_FROM_REG(val) (((val) & \
			      (DS1621_ALARM_TEMP_HIGH |
			       DS621_ALARM_TEMP_LOW)) & \
			       1)

Maybe you have a better idea.

The problem is, I don't see what you are trying to do. The original driver has:

#define ALARMS_FROM_REG(val) ((val) & \
                              (DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))

And this is exactly what you are supposed to have. Why did you want to change it in the first place?

Quoted 7 lines

In fact making the chip to work in non continous mode (starting conversion explicitly), give the possibility to get high resolution temperature (resolution of 0.01?C) instead of standard resolution (0.5?C). In the driver there was a start of 1 shot conversion support, but no routines to get high resolution temperatures.

I removed all routines concerning the continous parameter.

I prefer it that way. That "extra resolution" feature sounded bloated to me. Who needs such a resolution after all?

Quoted 6 lines
Quoted 2 lines

Polarity
should be left unchanged.

I not really agree with that. The polarity parameter doesn't control the polarity of the alarms, but rather the polarity of the output pin. On my circuit, this pin activates a relay, and the polarity would be different if you want to connect an heater or an air conditionner!

I usually object that BIOSes are supposed to set things up for you, but it of course doesn't apply in this case.

Don't you think it should be a module parameter instead? I don't think that's the kind of setting you want to change at runtime. I'd suggest that you leave the polarity unchanged by default, and force it to 0 or 1 on user request.

If you don't like the idea, then we have to find a correct name for the sysfs file. Whenever possible, sysfs names should express precisely what they represent.

On the patch itself:

Quoted 9 lines

/* The DS1621 registers */
#define DS1621_REG_TEMP 0xAA /* word, RO */
#define DS1621_REG_TEMP_OVER 0xA1 /* word, RW */
#define DS1621_REG_TEMP_HYST 0xA2 /* word, RW -- it's a low temp trigger */
#define DS1621_REG_CONF 0xAC /* byte, RW */
#define DS1621_REG_TEMP_COUNTER 0xA8 /* byte, RO */
#define DS1621_REG_TEMP_SLOPE 0xA9 /* byte, RO */
#define DS1621_COM_START 0xEE /* no data */
#define DS1621_COM_STOP 0x22 /* no data */

DS1621_REG_TEMP_COUNTER, DS1621_REG_TEMP_SLOPE and DS1621_COM_STOP are unused. Removing them completely would simplify ds1621_read_value and ds1621_write_value much.

Quoted 4 lines

static u16 swap_bytes(u16 val)

{
	return (val >> 8) | (val << 8);
}

This should be inlined in lm75.h IMHO, but let's leave this for later.

Quoted 17 lines

/* All registers are word-sized, except for the configuration register.
DS1621 uses a high-byte first convention, which is exactly opposite to the usual practice. */
static int ds1621_read_value(struct i2c_client *client, u8 reg)

{
	if ((reg = DS1621_REG_CONF) || (reg = DS1621_REG_TEMP_COUNTER)
	    || (reg = DS1621_REG_TEMP_SLOPE))
		return i2c_smbus_read_byte_data(client, reg);
	else
		return swap_bytes(i2c_smbus_read_word_data(client, reg));
}

/* All registers are word-sized, except for the configuration register.
DS1621 uses a high-byte first convention, which is exactly opposite to the usual practice. */
static int ds1621_write_value(struct i2c_client *client, u8 reg, u16 value)

Quoted 11 lines
{
	if ( (reg = DS1621_COM_START) || (reg = DS1621_COM_STOP) )
		return i2c_smbus_write_byte(client, reg);
	else
	if ((reg = DS1621_REG_CONF) || (reg = DS1621_REG_TEMP_COUNTER)
	    || (reg = DS1621_REG_TEMP_SLOPE))
		return i2c_smbus_write_byte_data(client, reg, value);
	else
		return i2c_smbus_write_word_data(client, reg,
						 swap_bytes(value));
}

The comments don't match the code, but it doesn't matter much if you get rid of DS1621_REG_TEMP_COUNTER and DS1621_REG_TEMP_SLOPE completely.

I don't think I would include DS1621_COM_START in ds1621_write_value, since it's used only once and isn't a real write. But maybe that's just me.

Quoted 2 lines

static DEVICE_ATTR(temp_hyst1, S_IWUSR | S_IRUGO , show_temp_hyst, set_temp_hyst); static DEVICE_ATTR(temp_max1, S_IWUSR | S_IRUGO, show_temp_over, set_temp_over);

BTW, is it an hysteresis or a min limit? After a quick look at the DS1621 datasheet, I'm not absolutely sure. It looks like an hysteresis for the output but a min for the alarm flags. Doesn't make much sense to me. Any chance you can clarify the situation?

Also, we just changed the sysfs naming conversions. Please see Documentation/i2c/sysfs-interface in 2.6.4-mm1 or after applying this page on top of 2.6.4:
http://delvare.nerim.net/i2c/linux-2.6/linux-2.6.4-i2c.patch.gz

Baically, the idea is that temp_max1 becomes temp1_max, etc...

Quoted 7 lines
	/* Make sure we aren't probing the ISA bus!! This is just a safety check
	   at this moment; i2c_detect really won't call us. */
	if (i2c_is_isa_adapter(adapter)) {
		dev_dbg(&adapter->dev, 
		        "ds1621.o: ds1621_detect called for an ISA bus adapter?!?\n");
		return 0;
	}

This block isn't needed. The functionality check below it does the same as a side effect.

Quoted 2 lines

memset(new_client, 0, sizeof(struct i2c_client) +
sizeof(struct ds1621_data));

Bad indentation.

Quoted 4 lines
	/* Now, we do the remaining detection. It is lousy. */
	if (kind < 0) {
		conf = i2c_smbus_read_byte_data(new_client,
						DS1621_REG_CONF);

Don't you use ds1621_read_value here? (This is a true question, I don't know if this was not done on purpose in the first place.)

Quoted 4 lines
		if ((conf & DS1621_REG_CONFIG_MASK)
		    != DS1621_REG_CONFIG_VAL)
			goto ERROR2;
	}

The detection could be improved by reading temperatures (the three of them) and check that the 7 lower bits are clear. This is how things are done in sensors-detect.

Quoted 8 lines
	if (kind = ds1621) {
		client_name = "ds1621";
	} else {
		dev_dbg(&adapter->dev,
		        "ds1621.o: Internal error: unknown kind (%d)?!?",
		        kind);
		goto ERROR2;
	}

The "else" block is useless. Several drivers used to do that, but I've just submitted a patch that fixes this.

Quoted 2 lines
	/* Fill in remaining client fields and put it into the global list */
	strlcpy(new_client->name, client_name, I2C_NAME_SIZE);

You could even hardcode "ds1621" here and save some bytes. It's unlikely that such on old driver will be extended to support an additional chip.

Quoted 7 lines

/* OK, this is not exactly good programming practice, usually. But it is
very code-efficient in this case. */

      ERROR2:
	kfree(new_client);
      ERROR1:
      ERROR0:
	return err;

Merge ERROR0 and ERROR1, and rename labels to exit_free and exit.

A few more things:

1* Don't hesitate to credit yourself for porting the driver.

2* Please provide a patch against 2.6.4-mm1 (or 2.6.4 + the patch mentioned above). There were changes made to Makefile and Kconfig, your previous patch wouldn't have applied cleanly.

3* Do you know if there are differences between the DS1621 and the DS1625? The driver is supposed to support both.

Thanks.

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
#3 lore raw
In reply to Aurelien Jarno
Quoted 12 lines · Jean Delvare
Quoted 11 lines

The double negation is used to convert a number equal to zero or different to zero to a number equal to 0 or 1.

I don't have a lot of idea to reformulate it, I used:

#define ALARMS_FROM_REG(val) (((val) & \
			      (DS1621_ALARM_TEMP_HIGH |
			       DS621_ALARM_TEMP_LOW)) & \
			       1)

Maybe you have a better idea.

Quoted 8 lines

The problem is, I don't see what you are trying to do. The original driver has:

#define ALARMS_FROM_REG(val) ((val) & \
                              (DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))

And this is exactly what you are supposed to have. Why did you want to change it in the first place?

In the old driver, the output of ALARMS_FROM_REG was not binary (0 or 1), but could take four values, 0, 32, 64 or 96. That was what I was trying to change. I recognize that the formulations I used are not easily readable. In the new version, I let the old formulation and used the following line in show_alarms():
return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf) != 0);

Quoted 10 lines
Quoted 6 lines
Quoted 2 lines

Polarity
should be left unchanged.

I not really agree with that. The polarity parameter doesn't control the polarity of the alarms, but rather the polarity of the output pin. On my circuit, this pin activates a relay, and the polarity would be different if you want to connect an heater or an air conditionner!

I usually object that BIOSes are supposed to set things up for you, but it of course doesn't apply in this case.

Quoted 8 lines

Don't you think it should be a module parameter instead? I don't think that's the kind of setting you want to change at runtime. I'd suggest that you leave the polarity unchanged by default, and force it to 0 or 1 on user request.

If you don't like the idea, then we have to find a correct name for the sysfs file. Whenever possible, sysfs names should express precisely what they represent.

I added the polarity parameter to the module.

Quoted 12 lines

On the patch itself:

Quoted 9 lines

/* The DS1621 registers */
#define DS1621_REG_TEMP 0xAA /* word, RO */
#define DS1621_REG_TEMP_OVER 0xA1 /* word, RW */
#define DS1621_REG_TEMP_HYST 0xA2 /* word, RW -- it's a low temp trigger */
#define DS1621_REG_CONF 0xAC /* byte, RW */
#define DS1621_REG_TEMP_COUNTER 0xA8 /* byte, RO */
#define DS1621_REG_TEMP_SLOPE 0xA9 /* byte, RO */
#define DS1621_COM_START 0xEE /* no data */
#define DS1621_COM_STOP 0x22 /* no data */

Quoted 3 lines

DS1621_REG_TEMP_COUNTER, DS1621_REG_TEMP_SLOPE and DS1621_COM_STOP are unused. Removing them completely would simplify ds1621_read_value and ds1621_write_value much.

Done.

Quoted 28 lines
Quoted 16 lines

/* All registers are word-sized, except for the configuration register.
DS1621 uses a high-byte first convention, which is exactly opposite to the usual practice. */
static int ds1621_read_value(struct i2c_client *client, u8 reg)

{
	if ((reg = DS1621_REG_CONF) || (reg = DS1621_REG_TEMP_COUNTER)
	    || (reg = DS1621_REG_TEMP_SLOPE))
		return i2c_smbus_read_byte_data(client, reg);
	else
		return swap_bytes(i2c_smbus_read_word_data(client, reg));
}

/* All registers are word-sized, except for the configuration register.
DS1621 uses a high-byte first convention, which is exactly opposite to the usual practice. */
static int ds1621_write_value(struct i2c_client *client, u8 reg, u16 value)

Quoted 11 lines
{
	if ( (reg = DS1621_COM_START) || (reg = DS1621_COM_STOP) )
		return i2c_smbus_write_byte(client, reg);
	else
	if ((reg = DS1621_REG_CONF) || (reg = DS1621_REG_TEMP_COUNTER)
	    || (reg = DS1621_REG_TEMP_SLOPE))
		return i2c_smbus_write_byte_data(client, reg, value);
	else
		return i2c_smbus_write_word_data(client, reg,
						 swap_bytes(value));
}
Quoted 6 lines

The comments don't match the code, but it doesn't matter much if you get rid of DS1621_REG_TEMP_COUNTER and DS1621_REG_TEMP_SLOPE completely.

I don't think I would include DS1621_COM_START in ds1621_write_value, since it's used only once and isn't a real write. But maybe that's just me.

Done.

Quoted 7 lines
Quoted 2 lines

static DEVICE_ATTR(temp_hyst1, S_IWUSR | S_IRUGO , show_temp_hyst, set_temp_hyst); static DEVICE_ATTR(temp_max1, S_IWUSR | S_IRUGO, show_temp_over, set_temp_over);

BTW, is it an hysteresis or a min limit? After a quick look at the DS1621 datasheet, I'm not absolutely sure. It looks like an hysteresis for the output but a min for the alarm flags. Doesn't make much sense to me. Any chance you can clarify the situation?

After reading the datasheet and making some tests, I have understood how it functions. It a little bit complicated:

- There is in fact two registers, one for the minimum temperature, and one for the maximum temperature. A soft alarm is generated when the temperature is outside the range defined by this two temperatures.
- Concerning the output pin, one of the two register, depending on the choosen polarity, is used as an hysterisis value. So it could be temp_min and temp_hyst, or temp_hyst and temp_max.

I have made the modifications to call the two temperatures temp1_min and temp1_max. Maybe that should also be changed in the driver for 2.4 kernels and in libsensors.

Quoted 6 lines

Also, we just changed the sysfs naming conversions. Please see Documentation/i2c/sysfs-interface in 2.6.4-mm1 or after applying this page on top of 2.6.4:
http://delvare.nerim.net/i2c/linux-2.6/linux-2.6.4-i2c.patch.gz

Baically, the idea is that temp_max1 becomes temp1_max, etc...

Done.

Quoted 10 lines
Quoted 7 lines
	/* Make sure we aren't probing the ISA bus!! This is just a safety check
	   at this moment; i2c_detect really won't call us. */
	if (i2c_is_isa_adapter(adapter)) {
		dev_dbg(&adapter->dev, 
		        "ds1621.o: ds1621_detect called for an ISA bus adapter?!?\n");
		return 0;
	}

This block isn't needed. The functionality check below it does the same as a side effect.

Done.

Quoted 4 lines
Quoted 3 lines

memset(new_client, 0, sizeof(struct i2c_client) +
sizeof(struct ds1621_data));

Bad indentation.

Fixed.

Quoted 7 lines
Quoted 4 lines
	/* Now, we do the remaining detection. It is lousy. */
	if (kind < 0) {
		conf = i2c_smbus_read_byte_data(new_client,
						DS1621_REG_CONF);

Don't you use ds1621_read_value here? (This is a true question, I don't know if this was not done on purpose in the first place.)

Fixed.

Quoted 8 lines
Quoted 4 lines
		if ((conf & DS1621_REG_CONFIG_MASK)
		    != DS1621_REG_CONFIG_VAL)
			goto ERROR2;
	}

The detection could be improved by reading temperatures (the three of them) and check that the 7 lower bits are clear. This is how things are done in sensors-detect.

Done.

Quoted 11 lines
Quoted 8 lines
	if (kind = ds1621) {
		client_name = "ds1621";
	} else {
		dev_dbg(&adapter->dev,
		        "ds1621.o: Internal error: unknown kind (%d)?!?",
		        kind);
		goto ERROR2;
	}

The "else" block is useless. Several drivers used to do that, but I've just submitted a patch that fixes this.

Fixed.

Quoted 5 lines
Quoted 2 lines
	/* Fill in remaining client fields and put it into the global list */
	strlcpy(new_client->name, client_name, I2C_NAME_SIZE);

You could even hardcode "ds1621" here and save some bytes. It's unlikely that such on old driver will be extended to support an additional chip.

Done.

Quoted 9 lines
Quoted 7 lines

/* OK, this is not exactly good programming practice, usually. But it is
very code-efficient in this case. */

      ERROR2:
	kfree(new_client);
      ERROR1:
      ERROR0:
	return err;

Merge ERROR0 and ERROR1, and rename labels to exit_free and exit.

Done.

Quoted 3 lines

A few more things:

1* Don't hesitate to credit yourself for porting the driver.

Done.

Quoted 3 lines

2* Please provide a patch against 2.6.4-mm1 (or 2.6.4 + the patch mentioned above). There were changes made to Makefile and Kconfig, your previous patch wouldn't have applied cleanly.

Please find it attached.

Quoted 2 lines

3* Do you know if there are differences between the DS1621 and the DS1625? The driver is supposed to support both.

Basically the DS1621 is an evolution of the DS1625. It is pin compatible and has the following improvements:
* more accuracy
* possibility to read internal registers to compute a high resolution temperature
* wider supply voltage (2.7 to 5.5V)

Signature · 385 lines
  .''`.  Aurelien Jarno	              GPG: 1024D/F1BCDB73
 : :' :  Debian GNU/Linux developer | Electrical Engineering Student 
 `. `'   [email protected]         | [email protected]
   `-    people.debian.org/~aurel32 | www.aurel32.net
-------------- next part --------------
diff -urN linux-2.6.4.orig/drivers/i2c/chips/ds1621.c linux-2.6.4/drivers/i2c/chips/ds1621.c
--- linux-2.6.4.orig/drivers/i2c/chips/ds1621.c	1970-01-01 01:00:00.000000000 +0100
+++ linux-2.6.4/drivers/i2c/chips/ds1621.c	2004-03-14 12:40:42.000000000 +0100
@@ -0,0 +1,344 @@
+/*
+    ds1621.c - Part of lm_sensors, Linux kernel modules for hardware
+             monitoring
+    Christian W. Zuckschwerdt  <[email protected]>  2000-11-23
+    based on lm75.c by Frodo Looijaard <[email protected]>
+    Ported to Linux 2.6 by Aurelien Jarno <[email protected]> with 
+    the help of Jean Delvare <[email protected]>
+
+    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/i2c.h>
+#include <linux/i2c-sensor.h>
+#include "lm75.h"
+
+/* Addresses to scan */
+static unsigned short normal_i2c[] = { I2C_CLIENT_END };
+static unsigned short normal_i2c_range[] = { 0x48, 0x4f, I2C_CLIENT_END };
+static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
+static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
+
+/* Insmod parameters */
+SENSORS_INSMOD_1(ds1621);
+static int polarity = 1;
+MODULE_PARM(polarity, "i");
+MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low (default)");
+
+/* Many DS1621 constants specified below */
+/* Config register used for detection         */
+/*  7    6    5    4    3    2    1    0      */
+/* |Done|THF |TLF |NVB | 1  | 0  |POL |1SHOT| */
+#define DS1621_REG_CONFIG_MASK		0x0C
+#define DS1621_REG_CONFIG_VAL		0x08
+#define DS1621_REG_CONFIG_POLARITY	0x02
+#define DS1621_REG_CONFIG_1SHOT		0x01
+#define DS1621_REG_CONFIG_DONE		0x80
+
+/* The DS1621 registers */
+#define DS1621_REG_TEMP			0xAA /* word, RO */
+#define DS1621_REG_TEMP_MIN		0xA1 /* word, RW */
+#define DS1621_REG_TEMP_MAX		0xA2 /* word, RW */
+#define DS1621_REG_CONF			0xAC /* byte, RW */
+#define DS1621_COM_START		0xEE /* no data */
+
+/* The DS1621 configuration register */
+#define DS1621_ALARM_TEMP_HIGH		0x40
+#define DS1621_ALARM_TEMP_LOW		0x20
+
+/* Conversions. Rounding and limit checking is only done on the TO_REG
+   variants. Note that you should be a bit careful with which arguments
+   these macros are called: arguments may be evaluated more than once.
+   Fixing this is just not worth it. */
+#define ALARMS_FROM_REG(val) ((val) & \
+                              (DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))
+
+/* Each client has this additional data */
+struct ds1621_data {
+	struct semaphore update_lock;
+	char valid;			/* !=0 if following fields are valid */
+	unsigned long last_updated;	/* In jiffies */
+
+	u16 temp, temp_min, temp_max;	/* Register values, word */
+	u8 conf;			/* Register encoding, combined */
+};
+
+static int ds1621_attach_adapter(struct i2c_adapter *adapter);
+static int ds1621_detect(struct i2c_adapter *adapter, int address,
+			 int kind);
+static void ds1621_init_client(struct i2c_client *client);
+static int ds1621_detach_client(struct i2c_client *client);
+static void ds1621_update_client(struct i2c_client *client);
+
+/* This is the driver that will be inserted */
+static struct i2c_driver ds1621_driver = {
+	.owner		= THIS_MODULE,
+	.name		= "ds1621",
+	.id		= I2C_DRIVERID_DS1621,
+	.flags		= I2C_DF_NOTIFY,
+	.attach_adapter	= ds1621_attach_adapter,
+	.detach_client	= ds1621_detach_client,
+};
+
+static int ds1621_id = 0;
+
+static u16 swap_bytes(u16 val)
+{
+	return (val >> 8) | (val << 8);
+}
+
+/* All registers are word-sized, except for the configuration register.
+   DS1621 uses a high-byte first convention, which is exactly opposite to
+   the usual practice. */
+static int ds1621_read_value(struct i2c_client *client, u8 reg)
+{
+	if (reg = DS1621_REG_CONF)
+		return i2c_smbus_read_byte_data(client, reg);
+	else
+		return swap_bytes(i2c_smbus_read_word_data(client, reg));
+}
+
+/* All registers are word-sized, except for the configuration register.
+   DS1621 uses a high-byte first convention, which is exactly opposite to
+   the usual practice. */
+static int ds1621_write_value(struct i2c_client *client, u8 reg, u16 value)
+{
+	if (reg = DS1621_REG_CONF)
+		return i2c_smbus_write_byte_data(client, reg, value);
+	else
+		return i2c_smbus_write_word_data(client, reg,
+						 swap_bytes(value));
+}
+
+static void ds1621_init_client(struct i2c_client *client)
+{
+	int reg = ds1621_read_value(client, DS1621_REG_CONF);
+	/* switch to continous conversion mode */
+	reg &= ~ DS1621_REG_CONFIG_1SHOT;
+
+	/* setup output polarity */
+	if (polarity)
+		reg |= DS1621_REG_CONFIG_POLARITY;
+	else
+		reg &= ~DS1621_REG_CONFIG_POLARITY;
+	
+	ds1621_write_value(client, DS1621_REG_CONF, reg);
+	
+	/* start conversion */
+	i2c_smbus_write_byte(client, DS1621_COM_START);
+}
+
+#define show(value)     \
+static ssize_t show_##value(struct device *dev, char *buf)		\
+{									\
+        struct i2c_client *client = to_i2c_client(dev);			\
+	struct ds1621_data *data = i2c_get_clientdata(client);		\
+	ds1621_update_client(client);					\
+	return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value));	\
+}
+
+show(temp);
+show(temp_min);
+show(temp_max);
+
+#define set_temp(suffix, value, reg) \
+static ssize_t set_temp_##suffix(struct device *dev, const char *buf,	\
+		        size_t count)					\
+{									\
+	struct i2c_client *client = to_i2c_client(dev);			\
+	struct ds1621_data *data = i2c_get_clientdata(client);		\
+	data->value = LM75_TEMP_TO_REG(simple_strtoul(buf, NULL, 10));	\
+	ds1621_write_value(client, reg, data->value);			\
+	return count;							\
+}
+
+set_temp(min, temp_min, DS1621_REG_TEMP_MIN);
+set_temp(max, temp_max, DS1621_REG_TEMP_MAX);
+
+static ssize_t show_alarms(struct device *dev, char *buf)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+        struct ds1621_data *data = i2c_get_clientdata(client);
+	ds1621_update_client(client);
+	return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf) != 0);
+}
+
+static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
+static DEVICE_ATTR(temp1_input, S_IRUGO , show_temp, NULL);
+static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO , show_temp_min, set_temp_min);
+static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
+
+
+static int ds1621_attach_adapter(struct i2c_adapter *adapter)
+{
+	return i2c_detect(adapter, &addr_data, ds1621_detect);
+}
+
+/* This function is called by i2c_detect */
+int ds1621_detect(struct i2c_adapter *adapter, int address,
+		  int kind)
+{
+	int conf, temp;
+	struct i2c_client *new_client;
+	struct ds1621_data *data;
+	int err = 0;
+
+	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
+				     I2C_FUNC_SMBUS_WORD_DATA))
+		    goto exit;
+
+	/* OK. For now, we presume we have a valid client. We now create the
+	   client structure, even though we cannot fill it completely yet.
+	   But it allows us to access ds1621_{read,write}_value. */
+	if (!(new_client = kmalloc(sizeof(struct i2c_client) +
+				   sizeof(struct ds1621_data),
+				   GFP_KERNEL))) {
+		err = -ENOMEM;
+		goto exit;
+	}
+        memset(new_client, 0, sizeof(struct i2c_client) +
+	       sizeof(struct ds1621_data));
+	
+	data = (struct ds1621_data *) (new_client + 1);
+	i2c_set_clientdata(new_client, data);
+	new_client->addr = address;
+	new_client->adapter = adapter;
+	new_client->driver = &ds1621_driver;
+	new_client->flags = 0;
+
+
+	/* Now, we do the remaining detection. It is lousy. */
+	if (kind < 0) {
+		conf = ds1621_read_value(new_client, DS1621_REG_CONF);
+		if ((conf & DS1621_REG_CONFIG_MASK) != DS1621_REG_CONFIG_VAL)
+			goto exit_free;
+		temp = ds1621_read_value(new_client, DS1621_REG_TEMP);
+		if (temp & 0x007f)
+			goto exit_free;
+		temp = ds1621_read_value(new_client, DS1621_REG_TEMP_MIN);
+		if (temp & 0x007f)
+			goto exit_free;
+		temp = ds1621_read_value(new_client, DS1621_REG_TEMP_MAX);
+		if (temp & 0x007f)
+			goto exit_free;
+	}
+
+	/* Determine the chip type - only one kind supported! */
+	if (kind <= 0)
+		kind = ds1621;
+
+	/* Fill in remaining client fields and put it into the global list */
+	strlcpy(new_client->name, "ds1621", I2C_NAME_SIZE);
+
+	new_client->id = ds1621_id++;
+	data->valid = 0;
+	init_MUTEX(&data->update_lock);
+
+	/* Tell the I2C layer a new client has arrived */
+	if ((err = i2c_attach_client(new_client)))
+		goto exit_free;
+
+	/* Initialize the DS1621 chip */
+	ds1621_init_client(new_client);
+
+	/* Register sysfs hooks */
+        device_create_file(&new_client->dev, &dev_attr_alarms);
+        device_create_file(&new_client->dev, &dev_attr_temp1_input);
+        device_create_file(&new_client->dev, &dev_attr_temp1_min);
+        device_create_file(&new_client->dev, &dev_attr_temp1_max);
+	
+	return 0;
+
+/* OK, this is not exactly good programming practice, usually. But it is
+   very code-efficient in this case. */
+      exit_free:
+	kfree(new_client);
+      exit:
+	return err;
+}
+
+static int ds1621_detach_client(struct i2c_client *client)
+{
+	int err;
+
+	if ((err = i2c_detach_client(client))) {
+		dev_err(&client->dev,
+		        "ds1621.o: Client deregistration failed, client not detached.\n");
+		return err;
+	}
+
+	kfree(client);
+
+	return 0;
+}
+
+static void ds1621_update_client(struct i2c_client *client)
+{
+	struct ds1621_data *data = i2c_get_clientdata(client);
+	u8 new_conf;
+
+	down(&data->update_lock);
+
+	if ((jiffies - data->last_updated > HZ + HZ / 2) ||
+	    (jiffies < data->last_updated) || !data->valid) {
+
+		dev_dbg(&client->dev, "Starting ds1621 update\n");
+
+		data->conf = ds1621_read_value(client, DS1621_REG_CONF);
+
+		data->temp = ds1621_read_value(client, DS1621_REG_TEMP);
+		
+		data->temp_min = ds1621_read_value(client,
+		                                    DS1621_REG_TEMP_MIN);
+		data->temp_max = ds1621_read_value(client,
+						    DS1621_REG_TEMP_MAX);
+
+		/* reset alarms if neccessary */
+		new_conf = data->conf;
+		if (data->temp < data->temp_min)
+			new_conf &= ~DS1621_ALARM_TEMP_LOW;
+		if (data->temp > data->temp_max)
+			new_conf &= ~DS1621_ALARM_TEMP_HIGH;
+		if (data->conf != new_conf)
+			ds1621_write_value(client, DS1621_REG_CONF,
+					   new_conf);
+
+		data->last_updated = jiffies;
+		data->valid = 1;
+	}
+
+	up(&data->update_lock);
+}
+
+static int __init ds1621_init(void)
+{
+	return i2c_add_driver(&ds1621_driver);
+}
+
+static void __exit ds1621_exit(void)
+{
+	i2c_del_driver(&ds1621_driver);
+}
+
+
+MODULE_AUTHOR("Christian W. Zuckschwerdt <[email protected]>");
+MODULE_DESCRIPTION("DS1621 driver");
+MODULE_LICENSE("GPL");
+
+module_init(ds1621_init);
+module_exit(ds1621_exit);
diff -urN linux-2.6.4.orig/drivers/i2c/chips/Kconfig linux-2.6.4/drivers/i2c/chips/Kconfig
--- linux-2.6.4.orig/drivers/i2c/chips/Kconfig	2004-03-14 12:01:40.000000000 +0100
+++ linux-2.6.4/drivers/i2c/chips/Kconfig	2004-03-14 12:40:31.000000000 +0100
@@ -186,6 +186,17 @@
 menu "Other I2C Chip support"
 	depends on I2C
 
+config SENSORS_DS1621
+      	tristate "Dallas Semiconductor DS1621 and DS1625"
+	depends on I2C && EXPERIMENTAL
+	select I2C_SENSOR
+	help
+	  If you say yes here you get support for Dallas Semiconductor
+	  DS1621 and DS1625 sensor chips. 
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called ds1621.
+
 config SENSORS_EEPROM
 	tristate "EEPROM reader"
 	depends on I2C && EXPERIMENTAL
diff -urN linux-2.6.4.orig/drivers/i2c/chips/Makefile linux-2.6.4/drivers/i2c/chips/Makefile
--- linux-2.6.4.orig/drivers/i2c/chips/Makefile	2004-03-14 12:01:40.000000000 +0100
+++ linux-2.6.4/drivers/i2c/chips/Makefile	2004-03-14 12:40:31.000000000 +0100
@@ -8,6 +8,7 @@
 obj-$(CONFIG_SENSORS_W83781D)	+= w83781d.o
 
 obj-$(CONFIG_SENSORS_ADM1021)	+= adm1021.o
+obj-$(CONFIG_SENSORS_DS1621)	+= ds1621.o
 obj-$(CONFIG_SENSORS_EEPROM)	+= eeprom.o
 obj-$(CONFIG_SENSORS_FSCHER)	+= fscher.o
 obj-$(CONFIG_SENSORS_GL518SM)	+= gl518sm.o