Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / lm75.c
blob5d5dd7739294d25b08f91913639aab1e710cba9b
1 /*
2 lm75.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/i2c.h>
26 #include <linux/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
28 #include <linux/err.h>
29 #include <linux/mutex.h>
30 #include "lm75.h"
33 /* Addresses to scan */
34 <<<<<<< HEAD:drivers/hwmon/lm75.c
35 static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
36 =======
37 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
38 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/lm75.c
39 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
41 /* Insmod parameters */
42 I2C_CLIENT_INSMOD_1(lm75);
44 /* Many LM75 constants specified below */
46 /* The LM75 registers */
47 #define LM75_REG_CONF 0x01
48 static const u8 LM75_REG_TEMP[3] = {
49 0x00, /* input */
50 0x03, /* max */
51 0x02, /* hyst */
54 /* Each client has this additional data */
55 struct lm75_data {
56 struct i2c_client client;
57 struct device *hwmon_dev;
58 struct mutex update_lock;
59 char valid; /* !=0 if following fields are valid */
60 unsigned long last_updated; /* In jiffies */
61 u16 temp[3]; /* Register values,
62 0 = input
63 1 = max
64 2 = hyst */
67 static int lm75_attach_adapter(struct i2c_adapter *adapter);
68 static int lm75_detect(struct i2c_adapter *adapter, int address, int kind);
69 static void lm75_init_client(struct i2c_client *client);
70 static int lm75_detach_client(struct i2c_client *client);
71 static int lm75_read_value(struct i2c_client *client, u8 reg);
72 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
73 static struct lm75_data *lm75_update_device(struct device *dev);
76 /* This is the driver that will be inserted */
77 static struct i2c_driver lm75_driver = {
78 .driver = {
79 .name = "lm75",
81 .attach_adapter = lm75_attach_adapter,
82 .detach_client = lm75_detach_client,
85 static ssize_t show_temp(struct device *dev, struct device_attribute *da,
86 char *buf)
88 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
89 struct lm75_data *data = lm75_update_device(dev);
90 return sprintf(buf, "%d\n",
91 LM75_TEMP_FROM_REG(data->temp[attr->index]));
94 static ssize_t set_temp(struct device *dev, struct device_attribute *da,
95 const char *buf, size_t count)
97 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
98 struct i2c_client *client = to_i2c_client(dev);
99 struct lm75_data *data = i2c_get_clientdata(client);
100 int nr = attr->index;
101 long temp = simple_strtol(buf, NULL, 10);
103 mutex_lock(&data->update_lock);
104 data->temp[nr] = LM75_TEMP_TO_REG(temp);
105 lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]);
106 mutex_unlock(&data->update_lock);
107 return count;
110 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
111 show_temp, set_temp, 1);
112 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
113 show_temp, set_temp, 2);
114 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
116 static int lm75_attach_adapter(struct i2c_adapter *adapter)
118 if (!(adapter->class & I2C_CLASS_HWMON))
119 return 0;
120 return i2c_probe(adapter, &addr_data, lm75_detect);
123 static struct attribute *lm75_attributes[] = {
124 &sensor_dev_attr_temp1_input.dev_attr.attr,
125 &sensor_dev_attr_temp1_max.dev_attr.attr,
126 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
128 NULL
131 static const struct attribute_group lm75_group = {
132 .attrs = lm75_attributes,
135 /* This function is called by i2c_probe */
136 static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
138 int i;
139 struct i2c_client *new_client;
140 struct lm75_data *data;
141 int err = 0;
142 const char *name = "";
144 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
145 I2C_FUNC_SMBUS_WORD_DATA))
146 goto exit;
148 /* OK. For now, we presume we have a valid client. We now create the
149 client structure, even though we cannot fill it completely yet.
150 But it allows us to access lm75_{read,write}_value. */
151 if (!(data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL))) {
152 err = -ENOMEM;
153 goto exit;
156 new_client = &data->client;
157 i2c_set_clientdata(new_client, data);
158 new_client->addr = address;
159 new_client->adapter = adapter;
160 new_client->driver = &lm75_driver;
161 new_client->flags = 0;
163 /* Now, we do the remaining detection. There is no identification-
164 dedicated register so we have to rely on several tricks:
165 unused bits, registers cycling over 8-address boundaries,
166 addresses 0x04-0x07 returning the last read value.
167 The cycling+unused addresses combination is not tested,
168 since it would significantly slow the detection down and would
169 hardly add any value. */
170 if (kind < 0) {
171 int cur, conf, hyst, os;
173 /* Unused addresses */
174 cur = i2c_smbus_read_word_data(new_client, 0);
175 conf = i2c_smbus_read_byte_data(new_client, 1);
176 hyst = i2c_smbus_read_word_data(new_client, 2);
177 if (i2c_smbus_read_word_data(new_client, 4) != hyst
178 || i2c_smbus_read_word_data(new_client, 5) != hyst
179 || i2c_smbus_read_word_data(new_client, 6) != hyst
180 || i2c_smbus_read_word_data(new_client, 7) != hyst)
181 goto exit_free;
182 os = i2c_smbus_read_word_data(new_client, 3);
183 if (i2c_smbus_read_word_data(new_client, 4) != os
184 || i2c_smbus_read_word_data(new_client, 5) != os
185 || i2c_smbus_read_word_data(new_client, 6) != os
186 || i2c_smbus_read_word_data(new_client, 7) != os)
187 goto exit_free;
189 /* Unused bits */
190 if (conf & 0xe0)
191 goto exit_free;
193 /* Addresses cycling */
194 for (i = 8; i < 0xff; i += 8)
195 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
196 || i2c_smbus_read_word_data(new_client, i + 2) != hyst
197 || i2c_smbus_read_word_data(new_client, i + 3) != os)
198 goto exit_free;
201 /* Determine the chip type - only one kind supported! */
202 if (kind <= 0)
203 kind = lm75;
205 if (kind == lm75) {
206 name = "lm75";
209 /* Fill in the remaining client fields and put it into the global list */
210 strlcpy(new_client->name, name, I2C_NAME_SIZE);
211 data->valid = 0;
212 mutex_init(&data->update_lock);
214 /* Tell the I2C layer a new client has arrived */
215 if ((err = i2c_attach_client(new_client)))
216 goto exit_free;
218 /* Initialize the LM75 chip */
219 lm75_init_client(new_client);
221 /* Register sysfs hooks */
222 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm75_group)))
223 goto exit_detach;
225 data->hwmon_dev = hwmon_device_register(&new_client->dev);
226 if (IS_ERR(data->hwmon_dev)) {
227 err = PTR_ERR(data->hwmon_dev);
228 goto exit_remove;
231 return 0;
233 exit_remove:
234 sysfs_remove_group(&new_client->dev.kobj, &lm75_group);
235 exit_detach:
236 i2c_detach_client(new_client);
237 exit_free:
238 kfree(data);
239 exit:
240 return err;
243 static int lm75_detach_client(struct i2c_client *client)
245 struct lm75_data *data = i2c_get_clientdata(client);
246 hwmon_device_unregister(data->hwmon_dev);
247 sysfs_remove_group(&client->dev.kobj, &lm75_group);
248 i2c_detach_client(client);
249 kfree(data);
250 return 0;
253 /* All registers are word-sized, except for the configuration register.
254 LM75 uses a high-byte first convention, which is exactly opposite to
255 the usual practice. */
256 static int lm75_read_value(struct i2c_client *client, u8 reg)
258 if (reg == LM75_REG_CONF)
259 return i2c_smbus_read_byte_data(client, reg);
260 else
261 return swab16(i2c_smbus_read_word_data(client, reg));
264 /* All registers are word-sized, except for the configuration register.
265 LM75 uses a high-byte first convention, which is exactly opposite to
266 the usual practice. */
267 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
269 if (reg == LM75_REG_CONF)
270 return i2c_smbus_write_byte_data(client, reg, value);
271 else
272 return i2c_smbus_write_word_data(client, reg, swab16(value));
275 static void lm75_init_client(struct i2c_client *client)
277 int reg;
279 /* Enable if in shutdown mode */
280 reg = lm75_read_value(client, LM75_REG_CONF);
281 if (reg >= 0 && (reg & 0x01))
282 lm75_write_value(client, LM75_REG_CONF, reg & 0xfe);
285 static struct lm75_data *lm75_update_device(struct device *dev)
287 struct i2c_client *client = to_i2c_client(dev);
288 struct lm75_data *data = i2c_get_clientdata(client);
290 mutex_lock(&data->update_lock);
292 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
293 || !data->valid) {
294 int i;
295 dev_dbg(&client->dev, "Starting lm75 update\n");
297 for (i = 0; i < ARRAY_SIZE(data->temp); i++)
298 data->temp[i] = lm75_read_value(client,
299 LM75_REG_TEMP[i]);
300 data->last_updated = jiffies;
301 data->valid = 1;
304 mutex_unlock(&data->update_lock);
306 return data;
309 static int __init sensors_lm75_init(void)
311 return i2c_add_driver(&lm75_driver);
314 static void __exit sensors_lm75_exit(void)
316 i2c_del_driver(&lm75_driver);
319 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
320 MODULE_DESCRIPTION("LM75 driver");
321 MODULE_LICENSE("GPL");
323 module_init(sensors_lm75_init);
324 module_exit(sensors_lm75_exit);