This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / i2c / chips / lm77.c
1 /*
2     lm77.c - Part of lm_sensors, Linux kernel modules for hardware
3              monitoring
4
5     Copyright (c) 2004  Andras BALI <drewie@freemail.hu>
6
7     Heavily based on lm75.c by Frodo Looijaard <frodol@dds.nl>.  The LM77
8     is a temperature sensor and thermal window comparator with 0.5 deg
9     resolution made by National Semiconductor.  Complete datasheet can be
10     obtained at their site:
11        http://www.national.com/pf/LM/LM77.html
12
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU General Public License for more details.
22
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/i2c.h>
33 #include <linux/i2c-sensor.h>
34
35
36 /* Addresses to scan */
37 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
38 static unsigned short normal_i2c_range[] = { 0x48, 0x4b, I2C_CLIENT_END };
39 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
40 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
41
42 /* Insmod parameters */
43 SENSORS_INSMOD_1(lm77);
44
45 /* The LM77 registers */
46 #define LM77_REG_TEMP           0x00
47 #define LM77_REG_CONF           0x01
48 #define LM77_REG_TEMP_HYST      0x02
49 #define LM77_REG_TEMP_CRIT      0x03
50 #define LM77_REG_TEMP_MIN       0x04
51 #define LM77_REG_TEMP_MAX       0x05
52
53 /* Each client has this additional data */
54 struct lm77_data {
55         struct i2c_client       client;
56         struct semaphore        update_lock;
57         char                    valid;
58         unsigned long           last_updated;   /* In jiffies */
59         int                     temp_input;     /* Temperatures */
60         int                     temp_crit;
61         int                     temp_min;
62         int                     temp_max;
63         int                     temp_hyst;
64         u8                      alarms;
65 };
66
67 static int lm77_attach_adapter(struct i2c_adapter *adapter);
68 static int lm77_detect(struct i2c_adapter *adapter, int address, int kind);
69 static void lm77_init_client(struct i2c_client *client);
70 static int lm77_detach_client(struct i2c_client *client);
71 static u16 lm77_read_value(struct i2c_client *client, u8 reg);
72 static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value);
73
74 static struct lm77_data *lm77_update_device(struct device *dev);
75
76
77 /* This is the driver that will be inserted */
78 static struct i2c_driver lm77_driver = {
79         .owner          = THIS_MODULE,
80         .name           = "lm77",
81         .flags          = I2C_DF_NOTIFY,
82         .attach_adapter = lm77_attach_adapter,
83         .detach_client  = lm77_detach_client,
84 };
85
86 static int lm77_id = 0;
87
88 /* straight from the datasheet */
89 #define LM77_TEMP_MIN (-55000)
90 #define LM77_TEMP_MAX 125000
91
92 /* In the temperature registers, the low 3 bits are not part of the
93    temperature values; they are the status bits. */
94 static inline u16 LM77_TEMP_TO_REG(int temp)
95 {
96         int ntemp = SENSORS_LIMIT(temp, LM77_TEMP_MIN, LM77_TEMP_MAX);
97         return (u16)((ntemp / 500) * 8);
98 }
99
100 static inline int LM77_TEMP_FROM_REG(u16 reg)
101 {
102         return ((int)reg / 8) * 500;
103 }
104
105 /* sysfs stuff */
106
107 /* read routines for temperature limits */
108 #define show(value)     \
109 static ssize_t show_##value(struct device *dev, char *buf)      \
110 {                                                               \
111         struct lm77_data *data = lm77_update_device(dev);       \
112         return sprintf(buf, "%d\n", data->value);               \
113 }
114
115 show(temp_input);
116 show(temp_crit);
117 show(temp_min);
118 show(temp_max);
119 show(alarms);
120
121 /* read routines for hysteresis values */
122 static ssize_t show_temp_crit_hyst(struct device *dev, char *buf)
123 {
124         struct lm77_data *data = lm77_update_device(dev);
125         return sprintf(buf, "%d\n", data->temp_crit - data->temp_hyst);
126 }
127 static ssize_t show_temp_min_hyst(struct device *dev, char *buf)
128 {
129         struct lm77_data *data = lm77_update_device(dev);
130         return sprintf(buf, "%d\n", data->temp_min + data->temp_hyst);
131 }
132 static ssize_t show_temp_max_hyst(struct device *dev, char *buf)
133 {
134         struct lm77_data *data = lm77_update_device(dev);
135         return sprintf(buf, "%d\n", data->temp_max - data->temp_hyst);
136 }
137
138 /* write routines */
139 #define set(value, reg) \
140 static ssize_t set_##value(struct device *dev, const char *buf, size_t count)   \
141 {                                                                               \
142         struct i2c_client *client = to_i2c_client(dev);                         \
143         struct lm77_data *data = i2c_get_clientdata(client);                    \
144         data->value = simple_strtoul(buf, NULL, 10);                            \
145         lm77_write_value(client, reg, LM77_TEMP_TO_REG(data->value));           \
146         return count;                                                           \
147 }
148
149 set(temp_min, LM77_REG_TEMP_MIN);
150 set(temp_max, LM77_REG_TEMP_MAX);
151
152 /* hysteresis is stored as a relative value on the chip, so it has to be
153    converted first */
154 static ssize_t set_temp_crit_hyst(struct device *dev, const char *buf, size_t count)
155 {
156         struct i2c_client *client = to_i2c_client(dev);
157         struct lm77_data *data = i2c_get_clientdata(client);
158         data->temp_hyst = data->temp_crit - simple_strtoul(buf, NULL, 10);
159         lm77_write_value(client, LM77_REG_TEMP_HYST,
160                          LM77_TEMP_TO_REG(data->temp_hyst));
161         return count;
162 }
163
164 /* preserve hysteresis when setting T_crit */
165 static ssize_t set_temp_crit(struct device *dev, const char *buf, size_t count)
166 {
167         struct i2c_client *client = to_i2c_client(dev);
168         struct lm77_data *data = i2c_get_clientdata(client);
169         int oldcrithyst = data->temp_crit - data->temp_hyst;
170         data->temp_crit = simple_strtoul(buf, NULL, 10);
171         data->temp_hyst = data->temp_crit - oldcrithyst;
172         lm77_write_value(client, LM77_REG_TEMP_CRIT,
173                          LM77_TEMP_TO_REG(data->temp_crit));
174         lm77_write_value(client, LM77_REG_TEMP_HYST,
175                          LM77_TEMP_TO_REG(data->temp_hyst));
176         return count;
177 }
178
179 static DEVICE_ATTR(temp1_input, S_IRUGO,
180                    show_temp_input, NULL);
181 static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
182                    show_temp_crit, set_temp_crit);
183 static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
184                    show_temp_min, set_temp_min);
185 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
186                    show_temp_max, set_temp_max);
187
188 static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO,
189                    show_temp_crit_hyst, set_temp_crit_hyst);
190 static DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
191                    show_temp_min_hyst, NULL);
192 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO,
193                    show_temp_max_hyst, NULL);
194
195 static DEVICE_ATTR(alarms, S_IRUGO,
196                    show_alarms, NULL);
197
198 static int lm77_attach_adapter(struct i2c_adapter *adapter)
199 {
200         if (!(adapter->class & I2C_CLASS_HWMON))
201                 return 0;
202         return i2c_detect(adapter, &addr_data, lm77_detect);
203 }
204
205 /* This function is called by i2c_detect */
206 static int lm77_detect(struct i2c_adapter *adapter, int address, int kind)
207 {
208         struct i2c_client *new_client;
209         struct lm77_data *data;
210         int err = 0;
211         const char *name = "";
212
213         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
214                                      I2C_FUNC_SMBUS_WORD_DATA))
215                 goto exit;
216
217         /* OK. For now, we presume we have a valid client. We now create the
218            client structure, even though we cannot fill it completely yet.
219            But it allows us to access lm77_{read,write}_value. */
220         if (!(data = kmalloc(sizeof(struct lm77_data), GFP_KERNEL))) {
221                 err = -ENOMEM;
222                 goto exit;
223         }
224         memset(data, 0, sizeof(struct lm77_data));
225
226         new_client = &data->client;
227         i2c_set_clientdata(new_client, data);
228         new_client->addr = address;
229         new_client->adapter = adapter;
230         new_client->driver = &lm77_driver;
231         new_client->flags = 0;
232
233         /* Here comes the remaining detection.  Since the LM77 has no
234            register dedicated to identification, we have to rely on the
235            following tricks:
236
237            1. the high 4 bits represent the sign and thus they should
238               always be the same
239            2. the high 3 bits are unused in the configuration register
240            3. addresses 0x06 and 0x07 return the last read value
241            4. registers cycling over 8-address boundaries
242
243            Word-sized registers are high-byte first. */
244         if (kind < 0) {
245                 int i, cur, conf, hyst, crit, min, max;
246
247                 /* addresses cycling */
248                 cur = i2c_smbus_read_word_data(new_client, 0);
249                 conf = i2c_smbus_read_byte_data(new_client, 1);
250                 hyst = i2c_smbus_read_word_data(new_client, 2);
251                 crit = i2c_smbus_read_word_data(new_client, 3);
252                 min = i2c_smbus_read_word_data(new_client, 4);
253                 max = i2c_smbus_read_word_data(new_client, 5);
254                 for (i = 8; i <= 0xff; i += 8)
255                         if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
256                             || i2c_smbus_read_word_data(new_client, i + 2) != hyst
257                             || i2c_smbus_read_word_data(new_client, i + 3) != crit
258                             || i2c_smbus_read_word_data(new_client, i + 4) != min
259                             || i2c_smbus_read_word_data(new_client, i + 5) != max)
260                                 goto exit_free;
261
262                 /* sign bits */
263                 if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
264                     || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0)
265                     || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
266                     || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
267                     || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
268                         goto exit_free;
269
270                 /* unused bits */
271                 if (conf & 0xe0)
272                         goto exit_free;
273
274                 /* 0x06 and 0x07 return the last read value */
275                 cur = i2c_smbus_read_word_data(new_client, 0);
276                 if (i2c_smbus_read_word_data(new_client, 6) != cur
277                     || i2c_smbus_read_word_data(new_client, 7) != cur)
278                         goto exit_free;
279                 hyst = i2c_smbus_read_word_data(new_client, 2);
280                 if (i2c_smbus_read_word_data(new_client, 6) != hyst
281                     || i2c_smbus_read_word_data(new_client, 7) != hyst)
282                         goto exit_free;
283                 min = i2c_smbus_read_word_data(new_client, 4);
284                 if (i2c_smbus_read_word_data(new_client, 6) != min
285                     || i2c_smbus_read_word_data(new_client, 7) != min)
286                         goto exit_free;
287
288         }
289
290         /* Determine the chip type - only one kind supported! */
291         if (kind <= 0)
292                 kind = lm77;
293
294         if (kind == lm77) {
295                 name = "lm77";
296         }
297
298         /* Fill in the remaining client fields and put it into the global list */
299         strlcpy(new_client->name, name, I2C_NAME_SIZE);
300
301         new_client->id = lm77_id++;
302         data->valid = 0;
303         init_MUTEX(&data->update_lock);
304
305         /* Tell the I2C layer a new client has arrived */
306         if ((err = i2c_attach_client(new_client)))
307                 goto exit_free;
308
309         /* Initialize the LM77 chip */
310         lm77_init_client(new_client);
311
312         /* Register sysfs hooks */
313         device_create_file(&new_client->dev, &dev_attr_temp1_input);
314         device_create_file(&new_client->dev, &dev_attr_temp1_crit);
315         device_create_file(&new_client->dev, &dev_attr_temp1_min);
316         device_create_file(&new_client->dev, &dev_attr_temp1_max);
317         device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
318         device_create_file(&new_client->dev, &dev_attr_temp1_min_hyst);
319         device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
320         device_create_file(&new_client->dev, &dev_attr_alarms);
321         return 0;
322
323 exit_free:
324         kfree(data);
325 exit:
326         return err;
327 }
328
329 static int lm77_detach_client(struct i2c_client *client)
330 {
331         i2c_detach_client(client);
332         kfree(i2c_get_clientdata(client));
333         return 0;
334 }
335
336 /* All registers are word-sized, except for the configuration register.
337    The LM77 uses the high-byte first convention. */
338 static u16 lm77_read_value(struct i2c_client *client, u8 reg)
339 {
340         if (reg == LM77_REG_CONF)
341                 return i2c_smbus_read_byte_data(client, reg);
342         else
343                 return swab16(i2c_smbus_read_word_data(client, reg));
344 }
345
346 static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value)
347 {
348         if (reg == LM77_REG_CONF)
349                 return i2c_smbus_write_byte_data(client, reg, value);
350         else
351                 return i2c_smbus_write_word_data(client, reg, swab16(value));
352 }
353
354 static void lm77_init_client(struct i2c_client *client)
355 {
356         /* Initialize the LM77 chip - turn off shutdown mode */
357         int conf = lm77_read_value(client, LM77_REG_CONF);
358         if (conf & 1)
359                 lm77_write_value(client, LM77_REG_CONF, conf & 0xfe);
360 }
361
362 static struct lm77_data *lm77_update_device(struct device *dev)
363 {
364         struct i2c_client *client = to_i2c_client(dev);
365         struct lm77_data *data = i2c_get_clientdata(client);
366
367         down(&data->update_lock);
368
369         if ((jiffies - data->last_updated > HZ + HZ / 2) ||
370             (jiffies < data->last_updated) || !data->valid) {
371                 dev_dbg(&client->dev, "Starting lm77 update\n");
372                 data->temp_input =
373                         LM77_TEMP_FROM_REG(lm77_read_value(client,
374                                                            LM77_REG_TEMP));
375                 data->temp_hyst =
376                         LM77_TEMP_FROM_REG(lm77_read_value(client,
377                                                            LM77_REG_TEMP_HYST));
378                 data->temp_crit =
379                         LM77_TEMP_FROM_REG(lm77_read_value(client,
380                                                            LM77_REG_TEMP_CRIT));
381                 data->temp_min =
382                         LM77_TEMP_FROM_REG(lm77_read_value(client,
383                                                            LM77_REG_TEMP_MIN));
384                 data->temp_max =
385                         LM77_TEMP_FROM_REG(lm77_read_value(client,
386                                                            LM77_REG_TEMP_MAX));
387                 data->alarms =
388                         lm77_read_value(client, LM77_REG_TEMP) & 0x0007;
389                 data->last_updated = jiffies;
390                 data->valid = 1;
391         }
392
393         up(&data->update_lock);
394
395         return data;
396 }
397
398 static int __init sensors_lm77_init(void)
399 {
400         return i2c_add_driver(&lm77_driver);
401 }
402
403 static void __exit sensors_lm77_exit(void)
404 {
405         i2c_del_driver(&lm77_driver);
406 }
407
408 MODULE_AUTHOR("Andras BALI <drewie@freemail.hu>");
409 MODULE_DESCRIPTION("LM77 driver");
410 MODULE_LICENSE("GPL");
411
412 module_init(sensors_lm77_init);
413 module_exit(sensors_lm77_exit);