vserver 1.9.5.x5
[linux-2.6.git] / drivers / i2c / chips / smsc47m1.c
1 /*
2     smsc47m1.c - Part of lm_sensors, Linux kernel modules
3                  for hardware monitoring
4
5     Supports the SMSC LPC47B27x, LPC47M10x, LPC47M13x and LPC47M14x
6     Super-I/O chips.
7
8     Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
9     Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
10     Ported to Linux 2.6 by Gabriele Gorla <gorlik@yahoo.com>
11                         and Jean Delvare
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/module.h>
29 #include <linux/slab.h>
30 #include <linux/ioport.h>
31 #include <linux/i2c.h>
32 #include <linux/i2c-sensor.h>
33 #include <linux/init.h>
34 #include <asm/io.h>
35
36 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
37 /* Address is autodetected, there is no default value */
38 static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END };
39 static struct i2c_force_data forces[] = {{NULL}};
40
41 enum chips { any_chip, smsc47m1 };
42 static struct i2c_address_data addr_data = {
43         .normal_i2c             = normal_i2c,
44         .normal_isa             = normal_isa,
45         .forces                 = forces,
46 };
47
48 /* Super-I/0 registers and commands */
49
50 #define REG     0x2e    /* The register to read/write */
51 #define VAL     0x2f    /* The value to read/write */
52
53 static inline void
54 superio_outb(int reg, int val)
55 {
56         outb(reg, REG);
57         outb(val, VAL);
58 }
59
60 static inline int
61 superio_inb(int reg)
62 {
63         outb(reg, REG);
64         return inb(VAL);
65 }
66
67 /* logical device for fans is 0x0A */
68 #define superio_select() superio_outb(0x07, 0x0A)
69
70 static inline void
71 superio_enter(void)
72 {
73         outb(0x55, REG);
74 }
75
76 static inline void
77 superio_exit(void)
78 {
79         outb(0xAA, REG);
80 }
81
82 #define SUPERIO_REG_ACT         0x30
83 #define SUPERIO_REG_BASE        0x60
84 #define SUPERIO_REG_DEVID       0x20
85
86 /* Logical device registers */
87
88 #define SMSC_EXTENT             0x80
89
90 /* nr is 0 or 1 in the macros below */
91 #define SMSC47M1_REG_ALARM              0x04
92 #define SMSC47M1_REG_TPIN(nr)           (0x34 - (nr))
93 #define SMSC47M1_REG_PPIN(nr)           (0x36 - (nr))
94 #define SMSC47M1_REG_PWM(nr)            (0x56 + (nr))
95 #define SMSC47M1_REG_FANDIV             0x58
96 #define SMSC47M1_REG_FAN(nr)            (0x59 + (nr))
97 #define SMSC47M1_REG_FAN_PRELOAD(nr)    (0x5B + (nr))
98
99 #define MIN_FROM_REG(reg,div)           ((reg)>=192 ? 0 : \
100                                          983040/((192-(reg))*(div)))
101 #define FAN_FROM_REG(reg,div,preload)   ((reg)<=(preload) || (reg)==255 ? 0 : \
102                                          983040/(((reg)-(preload))*(div)))
103 #define DIV_FROM_REG(reg)               (1 << (reg))
104 #define PWM_FROM_REG(reg)               (((reg) & 0x7E) << 1)
105 #define PWM_EN_FROM_REG(reg)            ((~(reg)) & 0x01)
106 #define PWM_TO_REG(reg)                 (((reg) >> 1) & 0x7E)
107
108 struct smsc47m1_data {
109         struct i2c_client client;
110         struct semaphore lock;
111         int sysctl_id;
112
113         struct semaphore update_lock;
114         unsigned long last_updated;     /* In jiffies */
115
116         u8 fan[2];              /* Register value */
117         u8 fan_preload[2];      /* Register value */
118         u8 fan_div[2];          /* Register encoding, shifted right */
119         u8 alarms;              /* Register encoding */
120         u8 pwm[2];              /* Register value (bit 7 is enable) */
121 };
122
123
124 static int smsc47m1_attach_adapter(struct i2c_adapter *adapter);
125 static int smsc47m1_find(int *address);
126 static int smsc47m1_detect(struct i2c_adapter *adapter, int address, int kind);
127 static int smsc47m1_detach_client(struct i2c_client *client);
128
129 static int smsc47m1_read_value(struct i2c_client *client, u8 reg);
130 static void smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value);
131
132 static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
133                 int init);
134
135
136 static int smsc47m1_id;
137
138 static struct i2c_driver smsc47m1_driver = {
139         .owner          = THIS_MODULE,
140         .name           = "smsc47m1",
141         .id             = I2C_DRIVERID_SMSC47M1,
142         .flags          = I2C_DF_NOTIFY,
143         .attach_adapter = smsc47m1_attach_adapter,
144         .detach_client  = smsc47m1_detach_client,
145 };
146
147 /* nr is 0 or 1 in the callback functions below */
148
149 static ssize_t get_fan(struct device *dev, char *buf, int nr)
150 {
151         struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
152         /* This chip (stupidly) stops monitoring fan speed if PWM is
153            enabled and duty cycle is 0%. This is fine if the monitoring
154            and control concern the same fan, but troublesome if they are
155            not (which could as well happen). */
156         int rpm = (data->pwm[nr] & 0x7F) == 0x00 ? 0 :
157                   FAN_FROM_REG(data->fan[nr],
158                                DIV_FROM_REG(data->fan_div[nr]),
159                                data->fan_preload[nr]);
160         return sprintf(buf, "%d\n", rpm);
161 }
162
163 static ssize_t get_fan_min(struct device *dev, char *buf, int nr)
164 {
165         struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
166         int rpm = MIN_FROM_REG(data->fan_preload[nr],
167                                DIV_FROM_REG(data->fan_div[nr]));
168         return sprintf(buf, "%d\n", rpm);
169 }
170
171 static ssize_t get_fan_div(struct device *dev, char *buf, int nr)
172 {
173         struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
174         return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
175 }
176
177 static ssize_t get_pwm(struct device *dev, char *buf, int nr)
178 {
179         struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
180         return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
181 }
182
183 static ssize_t get_pwm_en(struct device *dev, char *buf, int nr)
184 {
185         struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
186         return sprintf(buf, "%d\n", PWM_EN_FROM_REG(data->pwm[nr]));
187 }
188
189 static ssize_t get_alarms(struct device *dev, char *buf)
190 {
191         struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
192         return sprintf(buf, "%d\n", data->alarms);
193 }
194
195 static ssize_t set_fan_min(struct device *dev, const char *buf,
196                 size_t count, int nr)
197 {
198         struct i2c_client *client = to_i2c_client(dev);
199         struct smsc47m1_data *data = i2c_get_clientdata(client);
200
201         long rpmdiv = simple_strtol(buf, NULL, 10)
202                     * DIV_FROM_REG(data->fan_div[nr]);
203
204         if (983040 > 192 * rpmdiv || 2 * rpmdiv > 983040)
205                 return -EINVAL;
206
207         data->fan_preload[nr] = 192 - ((983040 + rpmdiv / 2) / rpmdiv);
208         smsc47m1_write_value(client, SMSC47M1_REG_FAN_PRELOAD(nr),
209                              data->fan_preload[nr]);
210
211         return count;
212 }
213
214 /* Note: we save and restore the fan minimum here, because its value is
215    determined in part by the fan clock divider.  This follows the principle
216    of least suprise; the user doesn't expect the fan minimum to change just
217    because the divider changed. */
218 static ssize_t set_fan_div(struct device *dev, const char *buf,
219                 size_t count, int nr)
220 {
221         struct i2c_client *client = to_i2c_client(dev);
222         struct smsc47m1_data *data = i2c_get_clientdata(client);
223
224         long new_div = simple_strtol(buf, NULL, 10), tmp;
225         u8 old_div = DIV_FROM_REG(data->fan_div[nr]);
226
227         if (new_div == old_div) /* No change */
228                 return count;
229         switch (new_div) {
230         case 1: data->fan_div[nr] = 0; break;
231         case 2: data->fan_div[nr] = 1; break;
232         case 4: data->fan_div[nr] = 2; break;
233         case 8: data->fan_div[nr] = 3; break;
234         default: return -EINVAL;
235         }
236
237         tmp = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV) & 0x0F;
238         tmp |= (data->fan_div[0] << 4) | (data->fan_div[1] << 6);
239         smsc47m1_write_value(client, SMSC47M1_REG_FANDIV, tmp);
240
241         /* Preserve fan min */
242         tmp = 192 - (old_div * (192 - data->fan_preload[nr])
243                      + new_div / 2) / new_div;
244         data->fan_preload[nr] = SENSORS_LIMIT(tmp, 0, 191);
245         smsc47m1_write_value(client, SMSC47M1_REG_FAN_PRELOAD(nr),
246                              data->fan_preload[nr]);
247
248         return count;
249 }
250
251 static ssize_t set_pwm(struct device *dev, const char *buf,
252                 size_t count, int nr)
253 {
254         struct i2c_client *client = to_i2c_client(dev);
255         struct smsc47m1_data *data = i2c_get_clientdata(client);
256
257         long val = simple_strtol(buf, NULL, 10);
258
259         if (val < 0 || val > 255)
260                 return -EINVAL;
261
262         data->pwm[nr] &= 0x81; /* Preserve additional bits */
263         data->pwm[nr] |= PWM_TO_REG(val);
264
265         smsc47m1_write_value(client, SMSC47M1_REG_PWM(nr),
266                              data->pwm[nr]);
267         return count;
268 }
269
270 static ssize_t set_pwm_en(struct device *dev, const char *buf,
271                 size_t count, int nr)
272 {
273         struct i2c_client *client = to_i2c_client(dev);
274         struct smsc47m1_data *data = i2c_get_clientdata(client);
275
276         long val = simple_strtol(buf, NULL, 10);
277         
278         if (val != 0 && val != 1)
279                 return -EINVAL;
280
281         data->pwm[nr] &= 0xFE; /* preserve the other bits */
282         data->pwm[nr] |= !val;
283
284         smsc47m1_write_value(client, SMSC47M1_REG_PWM(nr),
285                              data->pwm[nr]);
286
287         return count;
288 }
289
290 #define fan_present(offset)                                             \
291 static ssize_t get_fan##offset (struct device *dev, char *buf)          \
292 {                                                                       \
293         return get_fan(dev, buf, offset - 1);                           \
294 }                                                                       \
295 static ssize_t get_fan##offset##_min (struct device *dev, char *buf)    \
296 {                                                                       \
297         return get_fan_min(dev, buf, offset - 1);                       \
298 }                                                                       \
299 static ssize_t set_fan##offset##_min (struct device *dev,               \
300                 const char *buf, size_t count)                          \
301 {                                                                       \
302         return set_fan_min(dev, buf, count, offset - 1);                \
303 }                                                                       \
304 static ssize_t get_fan##offset##_div (struct device *dev, char *buf)    \
305 {                                                                       \
306         return get_fan_div(dev, buf, offset - 1);                       \
307 }                                                                       \
308 static ssize_t set_fan##offset##_div (struct device *dev,               \
309                 const char *buf, size_t count)                          \
310 {                                                                       \
311         return set_fan_div(dev, buf, count, offset - 1);                \
312 }                                                                       \
313 static ssize_t get_pwm##offset (struct device *dev, char *buf)          \
314 {                                                                       \
315         return get_pwm(dev, buf, offset - 1);                           \
316 }                                                                       \
317 static ssize_t set_pwm##offset (struct device *dev,                     \
318                 const char *buf, size_t count)                          \
319 {                                                                       \
320         return set_pwm(dev, buf, count, offset - 1);                    \
321 }                                                                       \
322 static ssize_t get_pwm##offset##_en (struct device *dev, char *buf)     \
323 {                                                                       \
324         return get_pwm_en(dev, buf, offset - 1);                        \
325 }                                                                       \
326 static ssize_t set_pwm##offset##_en (struct device *dev,                \
327                 const char *buf, size_t count)                          \
328 {                                                                       \
329         return set_pwm_en(dev, buf, count, offset - 1);                 \
330 }                                                                       \
331 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, get_fan##offset,       \
332                 NULL);                                                  \
333 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,                \
334                 get_fan##offset##_min, set_fan##offset##_min);          \
335 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR,                \
336                 get_fan##offset##_div, set_fan##offset##_div);          \
337 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR,                      \
338                 get_pwm##offset, set_pwm##offset);                      \
339 static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR,             \
340                 get_pwm##offset##_en, set_pwm##offset##_en);
341
342 fan_present(1);
343 fan_present(2);
344
345 static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL);
346
347 static int smsc47m1_attach_adapter(struct i2c_adapter *adapter)
348 {
349         if (!(adapter->class & I2C_CLASS_HWMON))
350                 return 0;
351         return i2c_detect(adapter, &addr_data, smsc47m1_detect);
352 }
353
354 static int smsc47m1_find(int *address)
355 {
356         u8 val;
357
358         superio_enter();
359         val = superio_inb(SUPERIO_REG_DEVID);
360
361         /*
362          * SMSC LPC47M10x/LPC47M13x (device id 0x59), LPC47M14x (device id
363          * 0x5F) and LPC47B27x (device id 0x51) have fan control.
364          * The LPC47M15x and LPC47M192 chips "with hardware monitoring block"
365          * can do much more besides (device id 0x60, unsupported).
366          */
367         if (val == 0x51)
368                 printk(KERN_INFO "smsc47m1: Found SMSC47B27x\n");
369         else if (val == 0x59)
370                 printk(KERN_INFO "smsc47m1: Found SMSC47M10x/SMSC47M13x\n");
371         else if (val == 0x5F)
372                 printk(KERN_INFO "smsc47m1: Found SMSC47M14x\n");
373         else {
374                 superio_exit();
375                 return -ENODEV;
376         }
377
378         superio_select();
379         *address = (superio_inb(SUPERIO_REG_BASE) << 8)
380                  |  superio_inb(SUPERIO_REG_BASE + 1);
381         val = superio_inb(SUPERIO_REG_ACT);
382         if (*address == 0 || (val & 0x01) == 0) {
383                 printk(KERN_INFO "smsc47m1: Device is disabled, will not use\n");
384                 superio_exit();
385                 return -ENODEV;
386         }
387
388         superio_exit();
389         return 0;
390 }
391
392 static int smsc47m1_detect(struct i2c_adapter *adapter, int address, int kind)
393 {
394         struct i2c_client *new_client;
395         struct smsc47m1_data *data;
396         int err = 0;
397         int fan1, fan2, pwm1, pwm2;
398
399         if (!i2c_is_isa_adapter(adapter)) {
400                 return 0;
401         }
402
403         if (!request_region(address, SMSC_EXTENT, smsc47m1_driver.name)) {
404                 dev_err(&adapter->dev, "Region 0x%x already in use!\n", address);
405                 return -EBUSY;
406         }
407
408         if (!(data = kmalloc(sizeof(struct smsc47m1_data), GFP_KERNEL))) {
409                 err = -ENOMEM;
410                 goto error_release;
411         }
412         memset(data, 0x00, sizeof(struct smsc47m1_data));
413
414         new_client = &data->client;
415         i2c_set_clientdata(new_client, data);
416         new_client->addr = address;
417         init_MUTEX(&data->lock);
418         new_client->adapter = adapter;
419         new_client->driver = &smsc47m1_driver;
420         new_client->flags = 0;
421
422         strlcpy(new_client->name, "smsc47m1", I2C_NAME_SIZE);
423
424         new_client->id = smsc47m1_id++;
425         init_MUTEX(&data->update_lock);
426
427         /* If no function is properly configured, there's no point in
428            actually registering the chip. */
429         fan1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(0)) & 0x05)
430                == 0x05;
431         fan2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(1)) & 0x05)
432                == 0x05;
433         pwm1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(0)) & 0x05)
434                == 0x04;
435         pwm2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(1)) & 0x05)
436                == 0x04;
437         if (!(fan1 || fan2 || pwm1 || pwm2)) {
438                 dev_warn(&new_client->dev, "Device is not configured, will not use\n");
439                 err = -ENODEV;
440                 goto error_free;
441         }
442
443         if ((err = i2c_attach_client(new_client)))
444                 goto error_free;
445
446         /* Some values (fan min, clock dividers, pwm registers) may be
447            needed before any update is triggered, so we better read them
448            at least once here. We don't usually do it that way, but in
449            this particular case, manually reading 5 registers out of 8
450            doesn't make much sense and we're better using the existing
451            function. */
452         smsc47m1_update_device(&new_client->dev, 1);
453
454         if (fan1) {
455                 device_create_file(&new_client->dev, &dev_attr_fan1_input);
456                 device_create_file(&new_client->dev, &dev_attr_fan1_min);
457                 device_create_file(&new_client->dev, &dev_attr_fan1_div);
458         } else
459                 dev_dbg(&new_client->dev, "Fan 1 not enabled by hardware, "
460                         "skipping\n");
461
462         if (fan2) {
463                 device_create_file(&new_client->dev, &dev_attr_fan2_input);
464                 device_create_file(&new_client->dev, &dev_attr_fan2_min);
465                 device_create_file(&new_client->dev, &dev_attr_fan2_div);
466         } else
467                 dev_dbg(&new_client->dev, "Fan 2 not enabled by hardware, "
468                         "skipping\n");
469
470         if (pwm1) {
471                 device_create_file(&new_client->dev, &dev_attr_pwm1);
472                 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
473         } else
474                 dev_dbg(&new_client->dev, "PWM 1 not enabled by hardware, "
475                         "skipping\n");
476         if (pwm2) {
477                 device_create_file(&new_client->dev, &dev_attr_pwm2);
478                 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
479         } else
480                 dev_dbg(&new_client->dev, "PWM 2 not enabled by hardware, "
481                         "skipping\n");
482
483         device_create_file(&new_client->dev, &dev_attr_alarms);
484
485         return 0;
486
487 error_free:
488         kfree(new_client);
489 error_release:
490         release_region(address, SMSC_EXTENT);
491         return err;
492 }
493
494 static int smsc47m1_detach_client(struct i2c_client *client)
495 {
496         int err;
497
498         if ((err = i2c_detach_client(client))) {
499                 dev_err(&client->dev, "Client deregistration failed, "
500                         "client not detached.\n");
501                 return err;
502         }
503
504         release_region(client->addr, SMSC_EXTENT);
505         kfree(i2c_get_clientdata(client));
506
507         return 0;
508 }
509
510 static int smsc47m1_read_value(struct i2c_client *client, u8 reg)
511 {
512         int res;
513
514         down(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
515         res = inb_p(client->addr + reg);
516         up(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
517         return res;
518 }
519
520 static void smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value)
521 {
522         down(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
523         outb_p(value, client->addr + reg);
524         up(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
525 }
526
527 static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
528                 int init)
529 {
530         struct i2c_client *client = to_i2c_client(dev);
531         struct smsc47m1_data *data = i2c_get_clientdata(client);
532
533         down(&data->update_lock);
534
535         if ((jiffies - data->last_updated > HZ + HZ / 2) ||
536             (jiffies < data->last_updated) || init) {
537                 int i;
538
539                 for (i = 0; i < 2; i++) {
540                         data->fan[i] = smsc47m1_read_value(client,
541                                        SMSC47M1_REG_FAN(i));
542                         data->fan_preload[i] = smsc47m1_read_value(client,
543                                                SMSC47M1_REG_FAN_PRELOAD(i));
544                         data->pwm[i] = smsc47m1_read_value(client,
545                                        SMSC47M1_REG_PWM(i));
546                 }
547
548                 i = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV);
549                 data->fan_div[0] = (i >> 4) & 0x03;
550                 data->fan_div[1] = i >> 6;
551
552                 data->alarms = smsc47m1_read_value(client,
553                                SMSC47M1_REG_ALARM) >> 6;
554                 /* Clear alarms if needed */
555                 if (data->alarms)
556                         smsc47m1_write_value(client, SMSC47M1_REG_ALARM, 0xC0);
557
558                 data->last_updated = jiffies;
559         }
560
561         up(&data->update_lock);
562         return data;
563 }
564
565 static int __init sm_smsc47m1_init(void)
566 {
567         if (smsc47m1_find(normal_isa)) {
568                 return -ENODEV;
569         }
570
571         return i2c_add_driver(&smsc47m1_driver);
572 }
573
574 static void __exit sm_smsc47m1_exit(void)
575 {
576         i2c_del_driver(&smsc47m1_driver);
577 }
578
579 MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
580 MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver");
581 MODULE_LICENSE("GPL");
582
583 module_init(sm_smsc47m1_init);
584 module_exit(sm_smsc47m1_exit);