ds1621.c 10.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
    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 <khali@linux-fr.org>

    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>
29 30
#include <linux/hwmon.h>
#include <linux/err.h>
31
#include <linux/mutex.h>
32
#include <linux/sysfs.h>
L
Linus Torvalds 已提交
33 34 35 36 37 38 39
#include "lm75.h"

/* Addresses to scan */
static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
					0x4d, 0x4e, 0x4f, I2C_CLIENT_END };

/* Insmod parameters */
40
I2C_CLIENT_INSMOD_1(ds1621);
L
Linus Torvalds 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
static int polarity = -1;
module_param(polarity, int, 0);
MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low");

/* Many DS1621 constants specified below */
/* Config register used for detection         */
/*  7    6    5    4    3    2    1    0      */
/* |Done|THF |TLF |NVB | X  | X  |POL |1SHOT| */
#define DS1621_REG_CONFIG_NVB		0x10
#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 */
#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 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;
76
	struct class_device *class_dev;
77
	struct mutex update_lock;
L
Linus Torvalds 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	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 struct ds1621_data *ds1621_update_client(struct device *dev);

/* This is the driver that will be inserted */
static struct i2c_driver ds1621_driver = {
94 95 96
	.driver = {
		.name	= "ds1621",
	},
L
Linus Torvalds 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	.id		= I2C_DRIVERID_DS1621,
	.attach_adapter	= ds1621_attach_adapter,
	.detach_client	= ds1621_detach_client,
};

/* 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 swab16(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, swab16(value));
}

static void ds1621_init_client(struct i2c_client *client)
{
	int reg = ds1621_read_value(client, DS1621_REG_CONF);
127
	/* switch to continuous conversion mode */
L
Linus Torvalds 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	reg &= ~ DS1621_REG_CONFIG_1SHOT;

	/* setup output polarity */
	if (polarity == 0)
		reg &= ~DS1621_REG_CONFIG_POLARITY;
	else if (polarity == 1)
		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)							\
143
static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf)		\
L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151 152 153
{									\
	struct ds1621_data *data = ds1621_update_client(dev);		\
	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)					\
154
static ssize_t set_temp_##suffix(struct device *dev, struct device_attribute *attr, const char *buf,	\
L
Linus Torvalds 已提交
155 156 157 158 159 160
				 size_t count)				\
{									\
	struct i2c_client *client = to_i2c_client(dev);			\
	struct ds1621_data *data = ds1621_update_client(dev);		\
	u16 val = LM75_TEMP_TO_REG(simple_strtoul(buf, NULL, 10));	\
									\
161
	mutex_lock(&data->update_lock);					\
L
Linus Torvalds 已提交
162 163
	data->value = val;						\
	ds1621_write_value(client, reg, data->value);			\
164
	mutex_unlock(&data->update_lock);				\
L
Linus Torvalds 已提交
165 166 167 168 169 170
	return count;							\
}

set_temp(min, temp_min, DS1621_REG_TEMP_MIN);
set_temp(max, temp_max, DS1621_REG_TEMP_MAX);

171
static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
172 173 174 175 176 177 178 179 180 181
{
	struct ds1621_data *data = ds1621_update_client(dev);
	return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf));
}

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);

182 183 184 185 186 187 188 189 190 191 192 193
static struct attribute *ds1621_attributes[] = {
	&dev_attr_temp1_input.attr,
	&dev_attr_temp1_min.attr,
	&dev_attr_temp1_max.attr,
	&dev_attr_alarms.attr,
	NULL
};

static const struct attribute_group ds1621_group = {
	.attrs = ds1621_attributes,
};

L
Linus Torvalds 已提交
194 195 196

static int ds1621_attach_adapter(struct i2c_adapter *adapter)
{
197 198
	if (!(adapter->class & I2C_CLASS_HWMON))
		return 0;
199
	return i2c_probe(adapter, &addr_data, ds1621_detect);
L
Linus Torvalds 已提交
200 201
}

202
/* This function is called by i2c_probe */
203 204
static int ds1621_detect(struct i2c_adapter *adapter, int address,
			 int kind)
L
Linus Torvalds 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218
{
	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 
				     | I2C_FUNC_SMBUS_WRITE_BYTE))
		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. */
D
Deepak Saxena 已提交
219
	if (!(data = kzalloc(sizeof(struct ds1621_data), GFP_KERNEL))) {
L
Linus Torvalds 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
		err = -ENOMEM;
		goto exit;
	}
	
	new_client = &data->client;
	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) {
		/* The NVB bit should be low if no EEPROM write has been 
		   requested during the latest 10ms, which is highly 
		   improbable in our case. */
		conf = ds1621_read_value(new_client, DS1621_REG_CONF);
		if (conf & DS1621_REG_CONFIG_NVB)
			goto exit_free;
		/* The 7 lowest bits of a temperature should always be 0. */
		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);
	data->valid = 0;
259
	mutex_init(&data->update_lock);
L
Linus Torvalds 已提交
260 261 262 263 264 265 266 267 268

	/* 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 */
269 270 271
	if ((err = sysfs_create_group(&new_client->dev.kobj, &ds1621_group)))
		goto exit_detach;

272 273 274
	data->class_dev = hwmon_device_register(&new_client->dev);
	if (IS_ERR(data->class_dev)) {
		err = PTR_ERR(data->class_dev);
275
		goto exit_remove_files;
276 277
	}

L
Linus Torvalds 已提交
278 279
	return 0;

280 281
      exit_remove_files:
	sysfs_remove_group(&new_client->dev.kobj, &ds1621_group);
282 283
      exit_detach:
	i2c_detach_client(new_client);
L
Linus Torvalds 已提交
284 285 286 287 288 289 290 291
      exit_free:
	kfree(data);
      exit:
	return err;
}

static int ds1621_detach_client(struct i2c_client *client)
{
292
	struct ds1621_data *data = i2c_get_clientdata(client);
L
Linus Torvalds 已提交
293 294
	int err;

295
	hwmon_device_unregister(data->class_dev);
296
	sysfs_remove_group(&client->dev.kobj, &ds1621_group);
297

298
	if ((err = i2c_detach_client(client)))
L
Linus Torvalds 已提交
299 300
		return err;

301
	kfree(data);
L
Linus Torvalds 已提交
302 303 304 305 306 307 308 309 310 311 312

	return 0;
}


static struct ds1621_data *ds1621_update_client(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct ds1621_data *data = i2c_get_clientdata(client);
	u8 new_conf;

313
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
	    || !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);

329
		/* reset alarms if necessary */
L
Linus Torvalds 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342
		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;
	}

343
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

	return data;
}

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 <zany@triq.net>");
MODULE_DESCRIPTION("DS1621 driver");
MODULE_LICENSE("GPL");

module_init(ds1621_init);
module_exit(ds1621_exit);