vserver 1.9.5.x5
[linux-2.6.git] / drivers / i2c / chips / lm63.c
1 /*
2  * lm63.c - driver for the National Semiconductor LM63 temperature sensor
3  *          with integrated fan control
4  * Copyright (C) 2004  Jean Delvare <khali@linux-fr.org>
5  * Based on the lm90 driver.
6  *
7  * The LM63 is a sensor chip made by National Semiconductor. It measures
8  * two temperatures (its own and one external one) and the speed of one
9  * fan, those speed it can additionally control. Complete datasheet can be
10  * obtained from National's website at:
11  *   http://www.national.com/pf/LM/LM63.html
12  *
13  * The LM63 is basically an LM86 with fan speed monitoring and control
14  * capabilities added. It misses some of the LM86 features though:
15  *  - No low limit for local temperature.
16  *  - No critical limit for local temperature.
17  *  - Critical limit for remote temperature can be changed only once. We
18  *    will consider that the critical limit is read-only.
19  *
20  * The datasheet isn't very clear about what the tachometer reading is.
21  * I had a explanation from National Semiconductor though. The two lower
22  * bits of the read value have to be masked out. The value is still 16 bit
23  * in width.
24  *
25  * This program is free software; you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation; either version 2 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program; if not, write to the Free Software
37  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38  */
39
40 #include <linux/config.h>
41 #include <linux/module.h>
42 #include <linux/init.h>
43 #include <linux/slab.h>
44 #include <linux/i2c.h>
45 #include <linux/i2c-sensor.h>
46
47 /*
48  * Addresses to scan
49  * Address is fully defined internally and cannot be changed.
50  */
51
52 static unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END };
53 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
54
55 /*
56  * Insmod parameters
57  */
58
59 SENSORS_INSMOD_1(lm63);
60
61 /*
62  * The LM63 registers
63  */
64
65 #define LM63_REG_CONFIG1                0x03
66 #define LM63_REG_CONFIG2                0xBF
67 #define LM63_REG_CONFIG_FAN             0x4A
68
69 #define LM63_REG_TACH_COUNT_MSB         0x47
70 #define LM63_REG_TACH_COUNT_LSB         0x46
71 #define LM63_REG_TACH_LIMIT_MSB         0x49
72 #define LM63_REG_TACH_LIMIT_LSB         0x48
73
74 #define LM63_REG_PWM_VALUE              0x4C
75 #define LM63_REG_PWM_FREQ               0x4D
76
77 #define LM63_REG_LOCAL_TEMP             0x00
78 #define LM63_REG_LOCAL_HIGH             0x05
79
80 #define LM63_REG_REMOTE_TEMP_MSB        0x01
81 #define LM63_REG_REMOTE_TEMP_LSB        0x10
82 #define LM63_REG_REMOTE_OFFSET_MSB      0x11
83 #define LM63_REG_REMOTE_OFFSET_LSB      0x12
84 #define LM63_REG_REMOTE_HIGH_MSB        0x07
85 #define LM63_REG_REMOTE_HIGH_LSB        0x13
86 #define LM63_REG_REMOTE_LOW_MSB         0x08
87 #define LM63_REG_REMOTE_LOW_LSB         0x14
88 #define LM63_REG_REMOTE_TCRIT           0x19
89 #define LM63_REG_REMOTE_TCRIT_HYST      0x21
90
91 #define LM63_REG_ALERT_STATUS           0x02
92 #define LM63_REG_ALERT_MASK             0x16
93
94 #define LM63_REG_MAN_ID                 0xFE
95 #define LM63_REG_CHIP_ID                0xFF
96
97 /*
98  * Conversions and various macros
99  * For tachometer counts, the LM63 uses 16-bit values.
100  * For local temperature and high limit, remote critical limit and hysteresis
101  * value, it uses signed 8-bit values with LSB = 1 degree Celcius.
102  * For remote temperature, low and high limits, it uses signed 11-bit values
103  * with LSB = 0.125 degree Celcius, left-justified in 16-bit registers.
104  */
105
106 #define FAN_FROM_REG(reg)       ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
107                                  5400000 / (reg))
108 #define FAN_TO_REG(val)         ((val) <= 82 ? 0xFFFC : \
109                                  (5400000 / (val)) & 0xFFFC)
110 #define TEMP8_FROM_REG(reg)     ((reg) * 1000)
111 #define TEMP8_TO_REG(val)       ((val) <= -128000 ? -128 : \
112                                  (val) >= 127000 ? 127 : \
113                                  (val) < 0 ? ((val) - 500) / 1000 : \
114                                  ((val) + 500) / 1000)
115 #define TEMP11_FROM_REG(reg)    ((reg) / 32 * 125)
116 #define TEMP11_TO_REG(val)      ((val) <= -128000 ? 0x8000 : \
117                                  (val) >= 127875 ? 0x7FE0 : \
118                                  (val) < 0 ? ((val) - 62) / 125 * 32 : \
119                                  ((val) + 62) / 125 * 32)
120 #define HYST_TO_REG(val)        ((val) <= 0 ? 0 : \
121                                  (val) >= 127000 ? 127 : \
122                                  ((val) + 500) / 1000)
123
124 /*
125  * Functions declaration
126  */
127
128 static int lm63_attach_adapter(struct i2c_adapter *adapter);
129 static int lm63_detach_client(struct i2c_client *client);
130
131 static struct lm63_data *lm63_update_device(struct device *dev);
132
133 static int lm63_detect(struct i2c_adapter *adapter, int address, int kind);
134 static void lm63_init_client(struct i2c_client *client);
135
136 /*
137  * Driver data (common to all clients)
138  */
139
140 static struct i2c_driver lm63_driver = {
141         .owner          = THIS_MODULE,
142         .name           = "lm63",
143         .flags          = I2C_DF_NOTIFY,
144         .attach_adapter = lm63_attach_adapter,
145         .detach_client  = lm63_detach_client,
146 };
147
148 /*
149  * Client data (each client gets its own)
150  */
151
152 struct lm63_data {
153         struct i2c_client client;
154         struct semaphore update_lock;
155         char valid; /* zero until following fields are valid */
156         unsigned long last_updated; /* in jiffies */
157
158         /* registers values */
159         u8 config, config_fan;
160         u16 fan1_input;
161         u16 fan1_low;
162         u8 pwm1_freq;
163         u8 pwm1_value;
164         s8 temp1_input;
165         s8 temp1_high;
166         s16 temp2_input;
167         s16 temp2_high;
168         s16 temp2_low;
169         s8 temp2_crit;
170         u8 temp2_crit_hyst;
171         u8 alarms;
172 };
173
174 /*
175  * Sysfs callback functions and files
176  */
177
178 #define show_fan(value) \
179 static ssize_t show_##value(struct device *dev, char *buf) \
180 { \
181         struct lm63_data *data = lm63_update_device(dev); \
182         return sprintf(buf, "%d\n", FAN_FROM_REG(data->value)); \
183 }
184 show_fan(fan1_input);
185 show_fan(fan1_low);
186
187 static ssize_t set_fan1_low(struct device *dev, const char *buf,
188         size_t count)
189 {
190         struct i2c_client *client = to_i2c_client(dev);
191         struct lm63_data *data = i2c_get_clientdata(client);
192         unsigned long val = simple_strtoul(buf, NULL, 10);
193         data->fan1_low = FAN_TO_REG(val);
194         i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
195                                   data->fan1_low & 0xFF);
196         i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
197                                   data->fan1_low >> 8);
198         return count;
199 }
200
201 static ssize_t show_pwm1(struct device *dev, char *buf)
202 {
203         struct lm63_data *data = lm63_update_device(dev);
204         return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ?
205                        255 : (data->pwm1_value * 255 + data->pwm1_freq) /
206                        (2 * data->pwm1_freq));
207 }
208
209 static ssize_t set_pwm1(struct device *dev, const char *buf, size_t count)
210 {
211         struct i2c_client *client = to_i2c_client(dev);
212         struct lm63_data *data = i2c_get_clientdata(client);
213         unsigned long val;
214         
215         if (!(data->config_fan & 0x20)) /* register is read-only */
216                 return -EPERM;
217
218         val = simple_strtoul(buf, NULL, 10);
219         data->pwm1_value = val <= 0 ? 0 :
220                            val >= 255 ? 2 * data->pwm1_freq :
221                            (val * data->pwm1_freq * 2 + 127) / 255;
222         i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value);
223         return count;
224 }
225
226 static ssize_t show_pwm1_enable(struct device *dev, char *buf)
227 {
228         struct lm63_data *data = lm63_update_device(dev);
229         return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
230 }
231
232 #define show_temp8(value) \
233 static ssize_t show_##value(struct device *dev, char *buf) \
234 { \
235         struct lm63_data *data = lm63_update_device(dev); \
236         return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->value)); \
237 }
238 #define show_temp11(value) \
239 static ssize_t show_##value(struct device *dev, char *buf) \
240 { \
241         struct lm63_data *data = lm63_update_device(dev); \
242         return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->value)); \
243 }
244 show_temp8(temp1_input);
245 show_temp8(temp1_high);
246 show_temp11(temp2_input);
247 show_temp11(temp2_high);
248 show_temp11(temp2_low);
249 show_temp8(temp2_crit);
250
251 #define set_temp8(value, reg) \
252 static ssize_t set_##value(struct device *dev, const char *buf, \
253         size_t count) \
254 { \
255         struct i2c_client *client = to_i2c_client(dev); \
256         struct lm63_data *data = i2c_get_clientdata(client); \
257         long val = simple_strtol(buf, NULL, 10); \
258         data->value = TEMP8_TO_REG(val); \
259         i2c_smbus_write_byte_data(client, reg, data->value); \
260         return count; \
261 }
262 #define set_temp11(value, reg_msb, reg_lsb) \
263 static ssize_t set_##value(struct device *dev, const char *buf, \
264         size_t count) \
265 { \
266         struct i2c_client *client = to_i2c_client(dev); \
267         struct lm63_data *data = i2c_get_clientdata(client); \
268         long val = simple_strtol(buf, NULL, 10); \
269         data->value = TEMP11_TO_REG(val); \
270         i2c_smbus_write_byte_data(client, reg_msb, data->value >> 8); \
271         i2c_smbus_write_byte_data(client, reg_lsb, data->value & 0xff); \
272         return count; \
273 }
274 set_temp8(temp1_high, LM63_REG_LOCAL_HIGH);
275 set_temp11(temp2_high, LM63_REG_REMOTE_HIGH_MSB, LM63_REG_REMOTE_HIGH_LSB);
276 set_temp11(temp2_low, LM63_REG_REMOTE_LOW_MSB, LM63_REG_REMOTE_LOW_LSB);
277
278 /* Hysteresis register holds a relative value, while we want to present
279    an absolute to user-space */
280 static ssize_t show_temp2_crit_hyst(struct device *dev, char *buf)
281 {
282         struct lm63_data *data = lm63_update_device(dev);
283         return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp2_crit)
284                        - TEMP8_FROM_REG(data->temp2_crit_hyst));
285 }
286
287 /* And now the other way around, user-space provides an absolute
288    hysteresis value and we have to store a relative one */
289 static ssize_t set_temp2_crit_hyst(struct device *dev, const char *buf,
290         size_t count)
291 {
292         struct i2c_client *client = to_i2c_client(dev);
293         struct lm63_data *data = i2c_get_clientdata(client);
294         int hyst = TEMP8_FROM_REG(data->temp2_crit) -
295                    simple_strtol(buf, NULL, 10);
296         i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
297                                   HYST_TO_REG(hyst));
298         return count;
299 }
300
301 static ssize_t show_alarms(struct device *dev, char *buf)
302 {
303         struct lm63_data *data = lm63_update_device(dev);
304         return sprintf(buf, "%u\n", data->alarms);
305 }
306
307 static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan1_input, NULL);
308 static DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan1_low,
309         set_fan1_low);
310
311 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1);
312 static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
313
314 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1_input, NULL);
315 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp1_high,
316         set_temp1_high);
317
318 static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp2_input, NULL);
319 static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp2_low,
320         set_temp2_low);
321 static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp2_high,
322         set_temp2_high);
323 static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp2_crit, NULL);
324 static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
325         set_temp2_crit_hyst);
326
327 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
328
329 /*
330  * Real code
331  */
332
333 static int lm63_attach_adapter(struct i2c_adapter *adapter)
334 {
335         if (!(adapter->class & I2C_CLASS_HWMON))
336                 return 0;
337         return i2c_detect(adapter, &addr_data, lm63_detect);
338 }
339
340 /*
341  * The following function does more than just detection. If detection
342  * succeeds, it also registers the new chip.
343  */
344 static int lm63_detect(struct i2c_adapter *adapter, int address, int kind)
345 {
346         struct i2c_client *new_client;
347         struct lm63_data *data;
348         int err = 0;
349
350         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
351                 goto exit;
352
353         if (!(data = kmalloc(sizeof(struct lm63_data), GFP_KERNEL))) {
354                 err = -ENOMEM;
355                 goto exit;
356         }
357         memset(data, 0, sizeof(struct lm63_data));
358
359         /* The common I2C client data is placed right before the
360            LM63-specific data. */
361         new_client = &data->client;
362         i2c_set_clientdata(new_client, data);
363         new_client->addr = address;
364         new_client->adapter = adapter;
365         new_client->driver = &lm63_driver;
366         new_client->flags = 0;
367
368         /* Default to an LM63 if forced */
369         if (kind == 0)
370                 kind = lm63;
371
372         if (kind < 0) { /* must identify */
373                 u8 man_id, chip_id, reg_config1, reg_config2;
374                 u8 reg_alert_status, reg_alert_mask;
375
376                 man_id = i2c_smbus_read_byte_data(new_client,
377                          LM63_REG_MAN_ID);
378                 chip_id = i2c_smbus_read_byte_data(new_client,
379                           LM63_REG_CHIP_ID);
380                 reg_config1 = i2c_smbus_read_byte_data(new_client,
381                               LM63_REG_CONFIG1);
382                 reg_config2 = i2c_smbus_read_byte_data(new_client,
383                               LM63_REG_CONFIG2);
384                 reg_alert_status = i2c_smbus_read_byte_data(new_client,
385                                    LM63_REG_ALERT_STATUS);
386                 reg_alert_mask = i2c_smbus_read_byte_data(new_client,
387                                  LM63_REG_ALERT_MASK);
388
389                 if (man_id == 0x01 /* National Semiconductor */
390                  && chip_id == 0x41 /* LM63 */
391                  && (reg_config1 & 0x18) == 0x00
392                  && (reg_config2 & 0xF8) == 0x00
393                  && (reg_alert_status & 0x20) == 0x00
394                  && (reg_alert_mask & 0xA4) == 0xA4) {
395                         kind = lm63;
396                 } else { /* failed */
397                         dev_dbg(&adapter->dev, "Unsupported chip "
398                                 "(man_id=0x%02X, chip_id=0x%02X).\n",
399                                 man_id, chip_id);
400                         goto exit_free;
401                 }
402         }
403
404         strlcpy(new_client->name, "lm63", I2C_NAME_SIZE);
405         data->valid = 0;
406         init_MUTEX(&data->update_lock);
407
408         /* Tell the I2C layer a new client has arrived */
409         if ((err = i2c_attach_client(new_client)))
410                 goto exit_free;
411
412         /* Initialize the LM63 chip */
413         lm63_init_client(new_client);
414
415         /* Register sysfs hooks */
416         if (data->config & 0x04) { /* tachometer enabled */
417                 device_create_file(&new_client->dev, &dev_attr_fan1_input);
418                 device_create_file(&new_client->dev, &dev_attr_fan1_min);
419         }
420         device_create_file(&new_client->dev, &dev_attr_pwm1);
421         device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
422         device_create_file(&new_client->dev, &dev_attr_temp1_input);
423         device_create_file(&new_client->dev, &dev_attr_temp2_input);
424         device_create_file(&new_client->dev, &dev_attr_temp2_min);
425         device_create_file(&new_client->dev, &dev_attr_temp1_max);
426         device_create_file(&new_client->dev, &dev_attr_temp2_max);
427         device_create_file(&new_client->dev, &dev_attr_temp2_crit);
428         device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst);
429         device_create_file(&new_client->dev, &dev_attr_alarms);
430
431         return 0;
432
433 exit_free:
434         kfree(data);
435 exit:
436         return err;
437 }
438
439 /* Idealy we shouldn't have to initialize anything, since the BIOS
440    should have taken care of everything */
441 static void lm63_init_client(struct i2c_client *client)
442 {
443         struct lm63_data *data = i2c_get_clientdata(client);
444
445         data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
446         data->config_fan = i2c_smbus_read_byte_data(client,
447                                                     LM63_REG_CONFIG_FAN);
448
449         /* Start converting if needed */
450         if (data->config & 0x40) { /* standby */
451                 dev_dbg(&client->dev, "Switching to operational mode");
452                 data->config &= 0xA7;
453                 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
454                                           data->config);
455         }
456
457         /* We may need pwm1_freq before ever updating the client data */
458         data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
459         if (data->pwm1_freq == 0)
460                 data->pwm1_freq = 1;
461
462         /* Show some debug info about the LM63 configuration */
463         dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
464                 (data->config & 0x04) ? "tachometer input" :
465                 "alert output");
466         dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
467                 (data->config_fan & 0x08) ? "1.4" : "360",
468                 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
469         dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
470                 (data->config_fan & 0x10) ? "low" : "high",
471                 (data->config_fan & 0x20) ? "manual" : "auto");
472 }
473
474 static int lm63_detach_client(struct i2c_client *client)
475 {
476         int err;
477
478         if ((err = i2c_detach_client(client))) {
479                 dev_err(&client->dev, "Client deregistration failed, "
480                         "client not detached\n");
481                 return err;
482         }
483
484         kfree(i2c_get_clientdata(client));
485         return 0;
486 }
487
488 static struct lm63_data *lm63_update_device(struct device *dev)
489 {
490         struct i2c_client *client = to_i2c_client(dev);
491         struct lm63_data *data = i2c_get_clientdata(client);
492
493         down(&data->update_lock);
494
495         if ((jiffies - data->last_updated > HZ) ||
496             (jiffies < data->last_updated) ||
497             !data->valid) {
498                 if (data->config & 0x04) { /* tachometer enabled  */
499                         /* order matters for fan1_input */
500                         data->fan1_input = i2c_smbus_read_byte_data(client,
501                                            LM63_REG_TACH_COUNT_LSB) & 0xFC;
502                         data->fan1_input |= i2c_smbus_read_byte_data(client,
503                                             LM63_REG_TACH_COUNT_MSB) << 8;
504                         data->fan1_low = (i2c_smbus_read_byte_data(client,
505                                           LM63_REG_TACH_LIMIT_LSB) & 0xFC)
506                                        | (i2c_smbus_read_byte_data(client,
507                                           LM63_REG_TACH_LIMIT_MSB) << 8);
508                 }
509
510                 data->pwm1_freq = i2c_smbus_read_byte_data(client,
511                                   LM63_REG_PWM_FREQ);
512                 if (data->pwm1_freq == 0)
513                         data->pwm1_freq = 1;
514                 data->pwm1_value = i2c_smbus_read_byte_data(client,
515                                    LM63_REG_PWM_VALUE);
516
517                 data->temp1_input = i2c_smbus_read_byte_data(client,
518                                     LM63_REG_LOCAL_TEMP);
519                 data->temp1_high = i2c_smbus_read_byte_data(client,
520                                    LM63_REG_LOCAL_HIGH);
521
522                 /* order matters for temp2_input */
523                 data->temp2_input = i2c_smbus_read_byte_data(client,
524                                     LM63_REG_REMOTE_TEMP_MSB) << 8;
525                 data->temp2_input |= i2c_smbus_read_byte_data(client,
526                                      LM63_REG_REMOTE_TEMP_LSB);
527                 data->temp2_high = (i2c_smbus_read_byte_data(client,
528                                    LM63_REG_REMOTE_HIGH_MSB) << 8)
529                                  | i2c_smbus_read_byte_data(client,
530                                    LM63_REG_REMOTE_HIGH_LSB);
531                 data->temp2_low = (i2c_smbus_read_byte_data(client,
532                                   LM63_REG_REMOTE_LOW_MSB) << 8)
533                                 | i2c_smbus_read_byte_data(client,
534                                   LM63_REG_REMOTE_LOW_LSB);
535                 data->temp2_crit = i2c_smbus_read_byte_data(client,
536                                    LM63_REG_REMOTE_TCRIT);
537                 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
538                                         LM63_REG_REMOTE_TCRIT_HYST);
539
540                 data->alarms = i2c_smbus_read_byte_data(client,
541                                LM63_REG_ALERT_STATUS) & 0x7F;
542
543                 data->last_updated = jiffies;
544                 data->valid = 1;
545         }
546
547         up(&data->update_lock);
548
549         return data;
550 }
551
552 static int __init sensors_lm63_init(void)
553 {
554         return i2c_add_driver(&lm63_driver);
555 }
556
557 static void __exit sensors_lm63_exit(void)
558 {
559         i2c_del_driver(&lm63_driver);
560 }
561
562 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
563 MODULE_DESCRIPTION("LM63 driver");
564 MODULE_LICENSE("GPL");
565
566 module_init(sensors_lm63_init);
567 module_exit(sensors_lm63_exit);