[PATCH] I2C hwmon: add hwmon sysfs class to drivers
[safe/jmp/linux-2.6] / drivers / hwmon / lm75.c
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>
5
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.
10
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.
15
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.
19 */
20
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/i2c-sensor.h>
27 #include <linux/hwmon.h>
28 #include <linux/err.h>
29 #include "lm75.h"
30
31
32 /* Addresses to scan */
33 static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
34                                         0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
35 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
36
37 /* Insmod parameters */
38 SENSORS_INSMOD_1(lm75);
39
40 /* Many LM75 constants specified below */
41
42 /* The LM75 registers */
43 #define LM75_REG_TEMP           0x00
44 #define LM75_REG_CONF           0x01
45 #define LM75_REG_TEMP_HYST      0x02
46 #define LM75_REG_TEMP_OS        0x03
47
48 /* Each client has this additional data */
49 struct lm75_data {
50         struct i2c_client       client;
51         struct class_device *class_dev;
52         struct semaphore        update_lock;
53         char                    valid;          /* !=0 if following fields are valid */
54         unsigned long           last_updated;   /* In jiffies */
55         u16                     temp_input;     /* Register values */
56         u16                     temp_max;
57         u16                     temp_hyst;
58 };
59
60 static int lm75_attach_adapter(struct i2c_adapter *adapter);
61 static int lm75_detect(struct i2c_adapter *adapter, int address, int kind);
62 static void lm75_init_client(struct i2c_client *client);
63 static int lm75_detach_client(struct i2c_client *client);
64 static int lm75_read_value(struct i2c_client *client, u8 reg);
65 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
66 static struct lm75_data *lm75_update_device(struct device *dev);
67
68
69 /* This is the driver that will be inserted */
70 static struct i2c_driver lm75_driver = {
71         .owner          = THIS_MODULE,
72         .name           = "lm75",
73         .id             = I2C_DRIVERID_LM75,
74         .flags          = I2C_DF_NOTIFY,
75         .attach_adapter = lm75_attach_adapter,
76         .detach_client  = lm75_detach_client,
77 };
78
79 #define show(value)     \
80 static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf)               \
81 {                                                                       \
82         struct lm75_data *data = lm75_update_device(dev);               \
83         return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value));   \
84 }
85 show(temp_max);
86 show(temp_hyst);
87 show(temp_input);
88
89 #define set(value, reg) \
90 static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)    \
91 {                                                               \
92         struct i2c_client *client = to_i2c_client(dev);         \
93         struct lm75_data *data = i2c_get_clientdata(client);    \
94         int temp = simple_strtoul(buf, NULL, 10);               \
95                                                                 \
96         down(&data->update_lock);                               \
97         data->value = LM75_TEMP_TO_REG(temp);                   \
98         lm75_write_value(client, reg, data->value);             \
99         up(&data->update_lock);                                 \
100         return count;                                           \
101 }
102 set(temp_max, LM75_REG_TEMP_OS);
103 set(temp_hyst, LM75_REG_TEMP_HYST);
104
105 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
106 static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
107 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
108
109 static int lm75_attach_adapter(struct i2c_adapter *adapter)
110 {
111         if (!(adapter->class & I2C_CLASS_HWMON))
112                 return 0;
113         return i2c_detect(adapter, &addr_data, lm75_detect);
114 }
115
116 /* This function is called by i2c_detect */
117 static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
118 {
119         int i;
120         struct i2c_client *new_client;
121         struct lm75_data *data;
122         int err = 0;
123         const char *name = "";
124
125         /* Make sure we aren't probing the ISA bus!! This is just a safety check
126            at this moment; i2c_detect really won't call us. */
127 #ifdef DEBUG
128         if (i2c_is_isa_adapter(adapter)) {
129                 dev_dbg(&adapter->dev,
130                         "lm75_detect called for an ISA bus adapter?!?\n");
131                 goto exit;
132         }
133 #endif
134
135         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
136                                      I2C_FUNC_SMBUS_WORD_DATA))
137                 goto exit;
138
139         /* OK. For now, we presume we have a valid client. We now create the
140            client structure, even though we cannot fill it completely yet.
141            But it allows us to access lm75_{read,write}_value. */
142         if (!(data = kmalloc(sizeof(struct lm75_data), GFP_KERNEL))) {
143                 err = -ENOMEM;
144                 goto exit;
145         }
146         memset(data, 0, sizeof(struct lm75_data));
147
148         new_client = &data->client;
149         i2c_set_clientdata(new_client, data);
150         new_client->addr = address;
151         new_client->adapter = adapter;
152         new_client->driver = &lm75_driver;
153         new_client->flags = 0;
154
155         /* Now, we do the remaining detection. There is no identification-
156            dedicated register so we have to rely on several tricks:
157            unused bits, registers cycling over 8-address boundaries,
158            addresses 0x04-0x07 returning the last read value.
159            The cycling+unused addresses combination is not tested,
160            since it would significantly slow the detection down and would
161            hardly add any value. */
162         if (kind < 0) {
163                 int cur, conf, hyst, os;
164
165                 /* Unused addresses */
166                 cur = i2c_smbus_read_word_data(new_client, 0);
167                 conf = i2c_smbus_read_byte_data(new_client, 1);
168                 hyst = i2c_smbus_read_word_data(new_client, 2);
169                 if (i2c_smbus_read_word_data(new_client, 4) != hyst
170                  || i2c_smbus_read_word_data(new_client, 5) != hyst
171                  || i2c_smbus_read_word_data(new_client, 6) != hyst
172                  || i2c_smbus_read_word_data(new_client, 7) != hyst)
173                         goto exit_free;
174                 os = i2c_smbus_read_word_data(new_client, 3);
175                 if (i2c_smbus_read_word_data(new_client, 4) != os
176                  || i2c_smbus_read_word_data(new_client, 5) != os
177                  || i2c_smbus_read_word_data(new_client, 6) != os
178                  || i2c_smbus_read_word_data(new_client, 7) != os)
179                         goto exit_free;
180
181                 /* Unused bits */
182                 if (conf & 0xe0)
183                         goto exit_free;
184
185                 /* Addresses cycling */
186                 for (i = 8; i < 0xff; i += 8)
187                         if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
188                          || i2c_smbus_read_word_data(new_client, i + 2) != hyst
189                          || i2c_smbus_read_word_data(new_client, i + 3) != os)
190                                 goto exit_free;
191         }
192
193         /* Determine the chip type - only one kind supported! */
194         if (kind <= 0)
195                 kind = lm75;
196
197         if (kind == lm75) {
198                 name = "lm75";
199         }
200
201         /* Fill in the remaining client fields and put it into the global list */
202         strlcpy(new_client->name, name, I2C_NAME_SIZE);
203         data->valid = 0;
204         init_MUTEX(&data->update_lock);
205
206         /* Tell the I2C layer a new client has arrived */
207         if ((err = i2c_attach_client(new_client)))
208                 goto exit_free;
209
210         /* Initialize the LM75 chip */
211         lm75_init_client(new_client);
212         
213         /* Register sysfs hooks */
214         data->class_dev = hwmon_device_register(&new_client->dev);
215         if (IS_ERR(data->class_dev)) {
216                 err = PTR_ERR(data->class_dev);
217                 goto exit_detach;
218         }
219
220         device_create_file(&new_client->dev, &dev_attr_temp1_max);
221         device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
222         device_create_file(&new_client->dev, &dev_attr_temp1_input);
223
224         return 0;
225
226 exit_detach:
227         i2c_detach_client(new_client);
228 exit_free:
229         kfree(data);
230 exit:
231         return err;
232 }
233
234 static int lm75_detach_client(struct i2c_client *client)
235 {
236         struct lm75_data *data = i2c_get_clientdata(client);
237         hwmon_device_unregister(data->class_dev);
238         i2c_detach_client(client);
239         kfree(data);
240         return 0;
241 }
242
243 /* All registers are word-sized, except for the configuration register.
244    LM75 uses a high-byte first convention, which is exactly opposite to
245    the usual practice. */
246 static int lm75_read_value(struct i2c_client *client, u8 reg)
247 {
248         if (reg == LM75_REG_CONF)
249                 return i2c_smbus_read_byte_data(client, reg);
250         else
251                 return swab16(i2c_smbus_read_word_data(client, reg));
252 }
253
254 /* All registers are word-sized, except for the configuration register.
255    LM75 uses a high-byte first convention, which is exactly opposite to
256    the usual practice. */
257 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
258 {
259         if (reg == LM75_REG_CONF)
260                 return i2c_smbus_write_byte_data(client, reg, value);
261         else
262                 return i2c_smbus_write_word_data(client, reg, swab16(value));
263 }
264
265 static void lm75_init_client(struct i2c_client *client)
266 {
267         int reg;
268
269         /* Enable if in shutdown mode */
270         reg = lm75_read_value(client, LM75_REG_CONF);
271         if (reg >= 0 && (reg & 0x01))
272                 lm75_write_value(client, LM75_REG_CONF, reg & 0xfe);
273 }
274
275 static struct lm75_data *lm75_update_device(struct device *dev)
276 {
277         struct i2c_client *client = to_i2c_client(dev);
278         struct lm75_data *data = i2c_get_clientdata(client);
279
280         down(&data->update_lock);
281
282         if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
283             || !data->valid) {
284                 dev_dbg(&client->dev, "Starting lm75 update\n");
285
286                 data->temp_input = lm75_read_value(client, LM75_REG_TEMP);
287                 data->temp_max = lm75_read_value(client, LM75_REG_TEMP_OS);
288                 data->temp_hyst = lm75_read_value(client, LM75_REG_TEMP_HYST);
289                 data->last_updated = jiffies;
290                 data->valid = 1;
291         }
292
293         up(&data->update_lock);
294
295         return data;
296 }
297
298 static int __init sensors_lm75_init(void)
299 {
300         return i2c_add_driver(&lm75_driver);
301 }
302
303 static void __exit sensors_lm75_exit(void)
304 {
305         i2c_del_driver(&lm75_driver);
306 }
307
308 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
309 MODULE_DESCRIPTION("LM75 driver");
310 MODULE_LICENSE("GPL");
311
312 module_init(sensors_lm75_init);
313 module_exit(sensors_lm75_exit);