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