Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / ds1621.c
blob8f182b381f2f9f70c8e83e6a9203720665f3c01e
1 /*
2 ds1621.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Christian W. Zuckschwerdt <zany@triq.net> 2000-11-23
5 based on lm75.c by Frodo Looijaard <frodol@dds.nl>
6 Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
7 the help of Jean Delvare <khali@linux-fr.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
33 #include <linux/sysfs.h>
34 #include "lm75.h"
36 /* Addresses to scan */
37 <<<<<<< HEAD:drivers/hwmon/ds1621.c
38 static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
39 =======
40 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
41 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/ds1621.c
42 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
44 /* Insmod parameters */
45 I2C_CLIENT_INSMOD_1(ds1621);
46 static int polarity = -1;
47 module_param(polarity, int, 0);
48 MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low");
50 /* Many DS1621 constants specified below */
51 /* Config register used for detection */
52 /* 7 6 5 4 3 2 1 0 */
53 /* |Done|THF |TLF |NVB | X | X |POL |1SHOT| */
54 #define DS1621_REG_CONFIG_NVB 0x10
55 #define DS1621_REG_CONFIG_POLARITY 0x02
56 #define DS1621_REG_CONFIG_1SHOT 0x01
57 #define DS1621_REG_CONFIG_DONE 0x80
59 /* The DS1621 registers */
60 static const u8 DS1621_REG_TEMP[3] = {
61 0xAA, /* input, word, RO */
62 0xA2, /* min, word, RW */
63 0xA1, /* max, word, RW */
65 #define DS1621_REG_CONF 0xAC /* byte, RW */
66 #define DS1621_COM_START 0xEE /* no data */
67 #define DS1621_COM_STOP 0x22 /* no data */
69 /* The DS1621 configuration register */
70 #define DS1621_ALARM_TEMP_HIGH 0x40
71 #define DS1621_ALARM_TEMP_LOW 0x20
73 /* Conversions */
74 #define ALARMS_FROM_REG(val) ((val) & \
75 (DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))
77 /* Each client has this additional data */
78 struct ds1621_data {
79 struct i2c_client client;
80 struct device *hwmon_dev;
81 struct mutex update_lock;
82 char valid; /* !=0 if following fields are valid */
83 unsigned long last_updated; /* In jiffies */
85 u16 temp[3]; /* Register values, word */
86 u8 conf; /* Register encoding, combined */
89 static int ds1621_attach_adapter(struct i2c_adapter *adapter);
90 static int ds1621_detect(struct i2c_adapter *adapter, int address,
91 int kind);
92 static void ds1621_init_client(struct i2c_client *client);
93 static int ds1621_detach_client(struct i2c_client *client);
94 static struct ds1621_data *ds1621_update_client(struct device *dev);
96 /* This is the driver that will be inserted */
97 static struct i2c_driver ds1621_driver = {
98 .driver = {
99 .name = "ds1621",
101 .attach_adapter = ds1621_attach_adapter,
102 .detach_client = ds1621_detach_client,
105 /* All registers are word-sized, except for the configuration register.
106 DS1621 uses a high-byte first convention, which is exactly opposite to
107 the SMBus standard. */
108 static int ds1621_read_value(struct i2c_client *client, u8 reg)
110 if (reg == DS1621_REG_CONF)
111 return i2c_smbus_read_byte_data(client, reg);
112 else
113 return swab16(i2c_smbus_read_word_data(client, reg));
116 static int ds1621_write_value(struct i2c_client *client, u8 reg, u16 value)
118 if (reg == DS1621_REG_CONF)
119 return i2c_smbus_write_byte_data(client, reg, value);
120 else
121 return i2c_smbus_write_word_data(client, reg, swab16(value));
124 static void ds1621_init_client(struct i2c_client *client)
126 int reg = ds1621_read_value(client, DS1621_REG_CONF);
127 /* switch to continuous conversion mode */
128 reg &= ~ DS1621_REG_CONFIG_1SHOT;
130 /* setup output polarity */
131 if (polarity == 0)
132 reg &= ~DS1621_REG_CONFIG_POLARITY;
133 else if (polarity == 1)
134 reg |= DS1621_REG_CONFIG_POLARITY;
136 ds1621_write_value(client, DS1621_REG_CONF, reg);
138 /* start conversion */
139 i2c_smbus_write_byte(client, DS1621_COM_START);
142 static ssize_t show_temp(struct device *dev, struct device_attribute *da,
143 char *buf)
145 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
146 struct ds1621_data *data = ds1621_update_client(dev);
147 return sprintf(buf, "%d\n",
148 LM75_TEMP_FROM_REG(data->temp[attr->index]));
151 static ssize_t set_temp(struct device *dev, struct device_attribute *da,
152 const char *buf, size_t count)
154 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
155 struct i2c_client *client = to_i2c_client(dev);
156 struct ds1621_data *data = ds1621_update_client(dev);
157 u16 val = LM75_TEMP_TO_REG(simple_strtol(buf, NULL, 10));
159 mutex_lock(&data->update_lock);
160 data->temp[attr->index] = val;
161 ds1621_write_value(client, DS1621_REG_TEMP[attr->index],
162 data->temp[attr->index]);
163 mutex_unlock(&data->update_lock);
164 return count;
167 static ssize_t show_alarms(struct device *dev, struct device_attribute *da,
168 char *buf)
170 struct ds1621_data *data = ds1621_update_client(dev);
171 return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf));
174 static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
175 char *buf)
177 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
178 struct ds1621_data *data = ds1621_update_client(dev);
179 return sprintf(buf, "%d\n", !!(data->conf & attr->index));
182 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
183 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
184 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp, set_temp, 1);
185 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, set_temp, 2);
186 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
187 DS1621_ALARM_TEMP_LOW);
188 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
189 DS1621_ALARM_TEMP_HIGH);
191 static struct attribute *ds1621_attributes[] = {
192 &sensor_dev_attr_temp1_input.dev_attr.attr,
193 &sensor_dev_attr_temp1_min.dev_attr.attr,
194 &sensor_dev_attr_temp1_max.dev_attr.attr,
195 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
196 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
197 &dev_attr_alarms.attr,
198 NULL
201 static const struct attribute_group ds1621_group = {
202 .attrs = ds1621_attributes,
206 static int ds1621_attach_adapter(struct i2c_adapter *adapter)
208 if (!(adapter->class & I2C_CLASS_HWMON))
209 return 0;
210 return i2c_probe(adapter, &addr_data, ds1621_detect);
213 /* This function is called by i2c_probe */
214 static int ds1621_detect(struct i2c_adapter *adapter, int address,
215 int kind)
217 int conf, temp;
218 struct i2c_client *client;
219 struct ds1621_data *data;
220 int i, err = 0;
222 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
223 | I2C_FUNC_SMBUS_WORD_DATA
224 | I2C_FUNC_SMBUS_WRITE_BYTE))
225 goto exit;
227 /* OK. For now, we presume we have a valid client. We now create the
228 client structure, even though we cannot fill it completely yet.
229 But it allows us to access ds1621_{read,write}_value. */
230 if (!(data = kzalloc(sizeof(struct ds1621_data), GFP_KERNEL))) {
231 err = -ENOMEM;
232 goto exit;
235 client = &data->client;
236 i2c_set_clientdata(client, data);
237 client->addr = address;
238 client->adapter = adapter;
239 client->driver = &ds1621_driver;
241 /* Now, we do the remaining detection. It is lousy. */
242 if (kind < 0) {
243 /* The NVB bit should be low if no EEPROM write has been
244 requested during the latest 10ms, which is highly
245 improbable in our case. */
246 conf = ds1621_read_value(client, DS1621_REG_CONF);
247 if (conf & DS1621_REG_CONFIG_NVB)
248 goto exit_free;
249 /* The 7 lowest bits of a temperature should always be 0. */
250 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
251 temp = ds1621_read_value(client, DS1621_REG_TEMP[i]);
252 if (temp & 0x007f)
253 goto exit_free;
257 /* Fill in remaining client fields and put it into the global list */
258 strlcpy(client->name, "ds1621", I2C_NAME_SIZE);
259 mutex_init(&data->update_lock);
261 /* Tell the I2C layer a new client has arrived */
262 if ((err = i2c_attach_client(client)))
263 goto exit_free;
265 /* Initialize the DS1621 chip */
266 ds1621_init_client(client);
268 /* Register sysfs hooks */
269 if ((err = sysfs_create_group(&client->dev.kobj, &ds1621_group)))
270 goto exit_detach;
272 data->hwmon_dev = hwmon_device_register(&client->dev);
273 if (IS_ERR(data->hwmon_dev)) {
274 err = PTR_ERR(data->hwmon_dev);
275 goto exit_remove_files;
278 return 0;
280 exit_remove_files:
281 sysfs_remove_group(&client->dev.kobj, &ds1621_group);
282 exit_detach:
283 i2c_detach_client(client);
284 exit_free:
285 kfree(data);
286 exit:
287 return err;
290 static int ds1621_detach_client(struct i2c_client *client)
292 struct ds1621_data *data = i2c_get_clientdata(client);
293 int err;
295 hwmon_device_unregister(data->hwmon_dev);
296 sysfs_remove_group(&client->dev.kobj, &ds1621_group);
298 if ((err = i2c_detach_client(client)))
299 return err;
301 kfree(data);
303 return 0;
307 static struct ds1621_data *ds1621_update_client(struct device *dev)
309 struct i2c_client *client = to_i2c_client(dev);
310 struct ds1621_data *data = i2c_get_clientdata(client);
311 u8 new_conf;
313 mutex_lock(&data->update_lock);
315 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
316 || !data->valid) {
317 int i;
319 dev_dbg(&client->dev, "Starting ds1621 update\n");
321 data->conf = ds1621_read_value(client, DS1621_REG_CONF);
323 for (i = 0; i < ARRAY_SIZE(data->temp); i++)
324 data->temp[i] = ds1621_read_value(client,
325 DS1621_REG_TEMP[i]);
327 /* reset alarms if necessary */
328 new_conf = data->conf;
329 if (data->temp[0] > data->temp[1]) /* input > min */
330 new_conf &= ~DS1621_ALARM_TEMP_LOW;
331 if (data->temp[0] < data->temp[2]) /* input < max */
332 new_conf &= ~DS1621_ALARM_TEMP_HIGH;
333 if (data->conf != new_conf)
334 ds1621_write_value(client, DS1621_REG_CONF,
335 new_conf);
337 data->last_updated = jiffies;
338 data->valid = 1;
341 mutex_unlock(&data->update_lock);
343 return data;
346 static int __init ds1621_init(void)
348 return i2c_add_driver(&ds1621_driver);
351 static void __exit ds1621_exit(void)
353 i2c_del_driver(&ds1621_driver);
357 MODULE_AUTHOR("Christian W. Zuckschwerdt <zany@triq.net>");
358 MODULE_DESCRIPTION("DS1621 driver");
359 MODULE_LICENSE("GPL");
361 module_init(ds1621_init);
362 module_exit(ds1621_exit);