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