root/lm-sensors/trunk/kernel/chips/smsc47m1.c @ 2867

Revision 2867, 14.2 KB (checked in by khali, 8 years ago)

Drop unused client id.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    smsc47m1.c - Part of lm_sensors, Linux kernel modules
3                for hardware monitoring
4               
5    Copyright (c) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
22#include <linux/module.h>
23#include <linux/slab.h>
24#include <linux/ioport.h>
25#include <linux/i2c.h>
26#include <linux/i2c-proc.h>
27#include <linux/init.h>
28#include <asm/io.h>
29#include "version.h"
30
31static int force_addr = 0;
32MODULE_PARM(force_addr, "i");
33MODULE_PARM_DESC(force_addr,
34                 "Initialize the base address of the sensors");
35
36static unsigned short normal_i2c[] = { SENSORS_I2C_END };
37static unsigned short normal_i2c_range[] = { SENSORS_I2C_END };
38static unsigned int normal_isa[] = { 0x0000, SENSORS_ISA_END };
39static unsigned int normal_isa_range[] = { SENSORS_ISA_END };
40
41SENSORS_INSMOD_1(smsc47m1);
42
43/* modified from kernel/include/traps.c */
44#define REG     0x2e    /* The register to read/write */
45#define DEV     0x07    /* Register: Logical device select */
46#define VAL     0x2f    /* The value to read/write */
47#define PME     0x0a    /* The device with the fan registers in it */
48#define DEVID   0x20    /* Register: Device ID */
49
50static inline void
51superio_outb(int reg, int val)
52{
53        outb(reg, REG);
54        outb(val, VAL);
55}
56
57static inline int
58superio_inb(int reg)
59{
60        outb(reg, REG);
61        return inb(VAL);
62}
63
64static inline void
65superio_select(void)
66{
67        outb(DEV, REG);
68        outb(PME, VAL);
69}
70
71static inline void
72superio_enter(void)
73{
74        outb(0x55, REG);
75}
76
77static inline void
78superio_exit(void)
79{
80        outb(0xAA, REG);
81}
82
83/*
84 * SMSC LPC47M10x (device id 0x59), LPC47M14x (device id 0x5F) and
85 * LPC47B27x (device id 0x51) have fan control.
86 * The 47M15x and 47M192 chips "with hardware monitoring block"
87 * can do much more besides (device id 0x60).
88 */
89#define SMSC_DEVID_MATCH(id) ((id) == 0x51 || (id) == 0x59 || (id) == 0x5F || (id) == 0x60)
90
91#define SMSC_ACT_REG 0x30
92#define SMSC_BASE_REG 0x60
93
94#define SMSC_EXTENT 0x80
95
96#define SMSC47M1_REG_ALARM1 0x04
97#define SMSC47M1_REG_TPIN2 0x33
98#define SMSC47M1_REG_TPIN1 0x34
99#define SMSC47M1_REG_PPIN(nr) (0x37 - (nr))
100#define SMSC47M1_REG_PWM(nr) (0x55 + (nr))
101#define SMSC47M1_REG_FANDIV 0x58
102#define SMSC47M1_REG_FAN(nr) (0x58 + (nr))
103#define SMSC47M1_REG_FAN_MIN(nr) (0x5a + (nr))
104
105static inline u8 MIN_TO_REG(long rpm, int div)
106{
107        if (rpm == 0)
108                return 0;
109        rpm = SENSORS_LIMIT(rpm, 1, 1000000);
110        return SENSORS_LIMIT(192 - ((983040 + rpm * div / 2) / (rpm * div)),
111                             0, 191);
112}
113
114#define MIN_FROM_REG(val,div) ((val)>=192?0: \
115                                983040/((192-(val))*(div)))
116#define FAN_FROM_REG(val,div,preload) ((val)==0?-1:(val)==255?0: \
117                                983040/(((val)-preload)*(div)))
118#define DIV_FROM_REG(val) (1 << (val))
119#define DIV_TO_REG(val) ((val)==8?3:(val)==4?2:(val)==1?0:1)
120/* reg is 6 middle bits; /proc is 8 bits */
121#define PWM_FROM_REG(val) (((val) << 1) & 0xfc)
122#define PWM_TO_REG(val)   (((SENSORS_LIMIT((val), 0, 255)) >> 1) & 0x7e)
123
124struct smsc47m1_data {
125        struct i2c_client client;
126        struct semaphore lock;
127        int sysctl_id;
128
129        struct semaphore update_lock;
130        char valid;             /* !=0 if following fields are valid */
131        unsigned long last_updated;     /* In jiffies */
132
133        u8 fan[2];              /* Register value */
134        u8 fan_min[2];          /* Register value */
135        u8 fan_div[2];          /* Register encoding, shifted right */
136        u8 alarms;              /* Register encoding */
137        u8 pwm[2];              /* Register value (bit 7 is enable) */
138};
139
140
141static int smsc47m1_attach_adapter(struct i2c_adapter *adapter);
142static int smsc47m1_detect(struct i2c_adapter *adapter, int address,
143                          unsigned short flags, int kind);
144static int smsc47m1_detach_client(struct i2c_client *client);
145
146static int smsc47m1_read_value(struct i2c_client *client, u8 register);
147static int smsc47m1_write_value(struct i2c_client *client, u8 register,
148                               u8 value);
149static void smsc47m1_update_client(struct i2c_client *client);
150static void smsc47m1_init_client(struct i2c_client *client);
151static int smsc47m1_find(int *address);
152
153
154static void smsc47m1_fan(struct i2c_client *client, int operation,
155                        int ctl_name, int *nrels_mag, long *results);
156static void smsc47m1_alarms(struct i2c_client *client, int operation,
157                           int ctl_name, int *nrels_mag, long *results);
158static void smsc47m1_fan_div(struct i2c_client *client, int operation,
159                            int ctl_name, int *nrels_mag, long *results);
160static void smsc47m1_pwm(struct i2c_client *client, int operation,
161                        int ctl_name, int *nrels_mag, long *results);
162
163static struct i2c_driver smsc47m1_driver = {
164        .name           = "SMSC 47M1xx fan monitor",
165        .id             = I2C_DRIVERID_SMSC47M1,
166        .flags          = I2C_DF_NOTIFY,
167        .attach_adapter = smsc47m1_attach_adapter,
168        .detach_client  = smsc47m1_detach_client,
169};
170
171/* -- SENSORS SYSCTL START -- */
172#define SMSC47M1_SYSCTL_FAN1 1101   /* Rotations/min */
173#define SMSC47M1_SYSCTL_FAN2 1102
174#define SMSC47M1_SYSCTL_PWM1 1401
175#define SMSC47M1_SYSCTL_PWM2 1402
176#define SMSC47M1_SYSCTL_FAN_DIV 2000        /* 1, 2, 4 or 8 */
177#define SMSC47M1_SYSCTL_ALARMS 2004    /* bitvector */
178
179#define SMSC47M1_ALARM_FAN1 0x0001
180#define SMSC47M1_ALARM_FAN2 0x0002
181
182/* -- SENSORS SYSCTL END -- */
183
184static ctl_table smsc47m1_dir_table_template[] = {
185        {SMSC47M1_SYSCTL_FAN1, "fan1", NULL, 0, 0644, NULL, &i2c_proc_real,
186         &i2c_sysctl_real, NULL, &smsc47m1_fan},
187        {SMSC47M1_SYSCTL_FAN2, "fan2", NULL, 0, 0644, NULL, &i2c_proc_real,
188         &i2c_sysctl_real, NULL, &smsc47m1_fan},
189        {SMSC47M1_SYSCTL_FAN_DIV, "fan_div", NULL, 0, 0644, NULL, &i2c_proc_real,
190         &i2c_sysctl_real, NULL, &smsc47m1_fan_div},
191        {SMSC47M1_SYSCTL_ALARMS, "alarms", NULL, 0, 0444, NULL, &i2c_proc_real,
192         &i2c_sysctl_real, NULL, &smsc47m1_alarms},
193        {SMSC47M1_SYSCTL_PWM1, "pwm1", NULL, 0, 0644, NULL, &i2c_proc_real,
194         &i2c_sysctl_real, NULL, &smsc47m1_pwm},
195        {SMSC47M1_SYSCTL_PWM2, "pwm2", NULL, 0, 0644, NULL, &i2c_proc_real,
196         &i2c_sysctl_real, NULL, &smsc47m1_pwm},
197        {0}
198};
199
200static int smsc47m1_attach_adapter(struct i2c_adapter *adapter)
201{
202        return i2c_detect(adapter, &addr_data, smsc47m1_detect);
203}
204
205static int smsc47m1_find(int *address)
206{
207        u16 val;
208
209        superio_enter();
210        val= superio_inb(DEVID);
211        if (!SMSC_DEVID_MATCH(val)) {
212                superio_exit();
213                return -ENODEV;
214        }
215
216        superio_select();
217        val = (superio_inb(SMSC_BASE_REG) << 8) |
218               superio_inb(SMSC_BASE_REG + 1);
219        *address = val & ~(SMSC_EXTENT - 1);
220        if (*address == 0 && force_addr == 0) {
221                printk("smsc47m1.o: base address not set - use force_addr=0xaddr\n");
222                superio_exit();
223                return -ENODEV;
224        }
225        if (force_addr)
226                *address = force_addr;  /* so detect will get called */
227
228        superio_exit();
229        return 0;
230}
231
232int smsc47m1_detect(struct i2c_adapter *adapter, int address,
233                   unsigned short flags, int kind)
234{
235        int i;
236        struct i2c_client *new_client;
237        struct smsc47m1_data *data;
238        int err = 0;
239        const char *type_name = "smsc47m1";
240        const char *client_name = "47M1xx chip";
241
242        if (!i2c_is_isa_adapter(adapter)) {
243                return 0;
244        }
245
246        if(force_addr)
247                address = force_addr & ~(SMSC_EXTENT - 1);
248        if (check_region(address, SMSC_EXTENT)) {
249                printk("smsc47m1.o: region 0x%x already in use!\n", address);
250                return -ENODEV;
251        }
252        if(force_addr) {
253                printk("smsc47m1.o: forcing ISA address 0x%04X\n", address);
254                superio_enter();
255                superio_select();
256                superio_outb(SMSC_BASE_REG, address >> 8);
257                superio_outb(SMSC_BASE_REG+1, address & 0xff);
258                superio_exit();
259        }
260
261        if (!(data = kmalloc(sizeof(struct smsc47m1_data), GFP_KERNEL))) {
262                return -ENOMEM;
263        }
264
265        new_client = &data->client;
266        new_client->addr = address;
267        init_MUTEX(&data->lock);
268        new_client->data = data;
269        new_client->adapter = adapter;
270        new_client->driver = &smsc47m1_driver;
271        new_client->flags = 0;
272
273        request_region(address, SMSC_EXTENT, "smsc47m1-fans");
274        strcpy(new_client->name, client_name);
275        data->valid = 0;
276        init_MUTEX(&data->update_lock);
277
278        if ((err = i2c_attach_client(new_client)))
279                goto ERROR3;
280
281        if ((i = i2c_register_entry((struct i2c_client *) new_client,
282                                        type_name,
283                                        smsc47m1_dir_table_template,
284                                        THIS_MODULE)) < 0) {
285                err = i;
286                goto ERROR4;
287        }
288        data->sysctl_id = i;
289
290        smsc47m1_init_client(new_client);
291        return 0;
292
293      ERROR4:
294        i2c_detach_client(new_client);
295      ERROR3:
296        release_region(address, SMSC_EXTENT);
297        kfree(data);
298        return err;
299}
300
301static int smsc47m1_detach_client(struct i2c_client *client)
302{
303        int err;
304
305        i2c_deregister_entry(((struct smsc47m1_data *) (client->data))->
306                                 sysctl_id);
307
308        if ((err = i2c_detach_client(client))) {
309                printk
310                    ("smsc47m1.o: Client deregistration failed, client not detached.\n");
311                return err;
312        }
313
314        release_region(client->addr, SMSC_EXTENT);
315        kfree(client->data);
316
317        return 0;
318}
319
320static int smsc47m1_read_value(struct i2c_client *client, u8 reg)
321{
322        int res;
323
324        down(&(((struct smsc47m1_data *) (client->data))->lock));
325        res = inb_p(client->addr + reg);
326        up(&(((struct smsc47m1_data *) (client->data))->lock));
327        return res;
328}
329
330static int smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value)
331{
332        down(&(((struct smsc47m1_data *) (client->data))->lock));
333        outb_p(value, client->addr + reg);
334        up(&(((struct smsc47m1_data *) (client->data))->lock));
335        return 0;
336}
337
338static void smsc47m1_init_client(struct i2c_client *client)
339{
340        /* configure pins for tach function */
341        smsc47m1_write_value(client, SMSC47M1_REG_TPIN1, 0x05);
342        smsc47m1_write_value(client, SMSC47M1_REG_TPIN2, 0x05);
343}
344
345static void smsc47m1_update_client(struct i2c_client *client)
346{
347        struct smsc47m1_data *data = client->data;
348        int i;
349
350        down(&data->update_lock);
351
352        if ((jiffies - data->last_updated > HZ + HZ / 2) ||
353            (jiffies < data->last_updated) || !data->valid) {
354                for (i = 1; i <= 2; i++) {
355                        data->fan[i - 1] =
356                            smsc47m1_read_value(client, SMSC47M1_REG_FAN(i));
357                        data->fan_min[i - 1] =
358                            smsc47m1_read_value(client, SMSC47M1_REG_FAN_MIN(i));
359                        data->pwm[i - 1] =
360                            smsc47m1_read_value(client, SMSC47M1_REG_PWM(i));
361                }
362
363                i = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV);
364                data->fan_div[0] = (i >> 4) & 0x03;
365                data->fan_div[1] = i >> 6;
366                data->alarms =
367                        smsc47m1_read_value(client, SMSC47M1_REG_ALARM1) >> 6;
368                if(data->alarms)
369                        smsc47m1_write_value(client, SMSC47M1_REG_ALARM1, 0xc0);
370                data->last_updated = jiffies;
371                data->valid = 1;
372        }
373
374        up(&data->update_lock);
375}
376
377
378void smsc47m1_fan(struct i2c_client *client, int operation, int ctl_name,
379                 int *nrels_mag, long *results)
380{
381        struct smsc47m1_data *data = client->data;
382        int nr = ctl_name - SMSC47M1_SYSCTL_FAN1 + 1;
383
384        if (operation == SENSORS_PROC_REAL_INFO)
385                *nrels_mag = 0;
386        else if (operation == SENSORS_PROC_REAL_READ) {
387                smsc47m1_update_client(client);
388                results[0] = MIN_FROM_REG(data->fan_min[nr - 1],
389                                          DIV_FROM_REG(data->fan_div[nr - 1]));
390                results[1] = FAN_FROM_REG(data->fan[nr - 1],
391                                          DIV_FROM_REG(data->fan_div[nr - 1]),
392                                          data->fan_min[nr - 1]);
393                *nrels_mag = 2;
394        } else if (operation == SENSORS_PROC_REAL_WRITE) {
395                if (*nrels_mag >= 1) {
396                        data->fan_min[nr - 1] = MIN_TO_REG(results[0],
397                                                           DIV_FROM_REG
398                                                           (data->
399                                                            fan_div[nr-1]));
400                        smsc47m1_write_value(client, SMSC47M1_REG_FAN_MIN(nr),
401                                            data->fan_min[nr - 1]);
402                }
403        }
404}
405
406
407void smsc47m1_alarms(struct i2c_client *client, int operation, int ctl_name,
408                    int *nrels_mag, long *results)
409{
410        struct smsc47m1_data *data = client->data;
411        if (operation == SENSORS_PROC_REAL_INFO)
412                *nrels_mag = 0;
413        else if (operation == SENSORS_PROC_REAL_READ) {
414                smsc47m1_update_client(client);
415                results[0] = data->alarms;
416                *nrels_mag = 1;
417        }
418}
419
420void smsc47m1_fan_div(struct i2c_client *client, int operation,
421                     int ctl_name, int *nrels_mag, long *results)
422{
423        struct smsc47m1_data *data = client->data;
424        int old;
425
426        if (operation == SENSORS_PROC_REAL_INFO)
427                *nrels_mag = 0;
428        else if (operation == SENSORS_PROC_REAL_READ) {
429                smsc47m1_update_client(client);
430                results[0] = DIV_FROM_REG(data->fan_div[0]);
431                results[1] = DIV_FROM_REG(data->fan_div[1]);
432                *nrels_mag = 2;
433        } else if (operation == SENSORS_PROC_REAL_WRITE) {
434                old = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV);
435                if (*nrels_mag >= 2) {
436                        data->fan_div[1] = DIV_TO_REG(results[1]);
437                        old = (old & 0x3f) | (data->fan_div[1] << 6);
438                }
439                if (*nrels_mag >= 1) {
440                        data->fan_div[0] = DIV_TO_REG(results[0]);
441                        old = (old & 0xcf) | (data->fan_div[0] << 4);
442                        smsc47m1_write_value(client, SMSC47M1_REG_FANDIV, old);
443                }
444        }
445}
446
447void smsc47m1_pwm(struct i2c_client *client, int operation, int ctl_name,
448                 int *nrels_mag, long *results)
449{
450        struct smsc47m1_data *data = client->data;
451        int nr = 1 + ctl_name - SMSC47M1_SYSCTL_PWM1;
452
453        if (operation == SENSORS_PROC_REAL_INFO)
454                *nrels_mag = 0;
455        else if (operation == SENSORS_PROC_REAL_READ) {
456                smsc47m1_update_client(client);
457                results[0] = PWM_FROM_REG(data->pwm[nr - 1]);
458                results[1] = data->pwm[nr - 1] & 0x01;
459                *nrels_mag = 2;
460        } else if (operation == SENSORS_PROC_REAL_WRITE) {
461                if (*nrels_mag >= 1) {
462                        data->pwm[nr - 1] &= 0x81;
463                        data->pwm[nr - 1] |= PWM_TO_REG(results[0]);
464                        if (*nrels_mag >= 2) {
465                                if(results[1] && (data->pwm[nr-1] & 0x01)) {
466                                        /* enable PWM */
467/* hope BIOS did it already
468                                        smsc47m1_write_value(client,
469                                                  SMSC47M1_REG_PPIN(nr), 0x04);
470*/
471                                        data->pwm[nr - 1] &= 0xfe;
472                                } else if((!results[1]) && (!(data->pwm[nr-1] & 0x01))) {
473                                        /* disable PWM */
474                                        data->pwm[nr - 1] |= 0x01;
475                                }
476                        }
477                        smsc47m1_write_value(client, SMSC47M1_REG_PWM(nr),
478                                             data->pwm[nr - 1]);
479                }
480        }
481}
482
483static int __init sm_smsc47m1_init(void)
484{
485        int addr;
486
487        printk("smsc47m1.o version %s (%s)\n", LM_VERSION, LM_DATE);
488
489        if (smsc47m1_find(&addr)) {
490                printk("smsc47m1.o: SMSC 47M1xx not detected, module not inserted.\n");
491                return -ENODEV;
492        }
493        normal_isa[0] = addr;
494
495        return i2c_add_driver(&smsc47m1_driver);
496}
497
498static void __exit sm_smsc47m1_exit(void)
499{
500        i2c_del_driver(&smsc47m1_driver);
501}
502
503
504
505MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
506MODULE_DESCRIPTION("SMSC 47M1xx Fan sensors");
507MODULE_LICENSE("GPL");
508
509module_init(sm_smsc47m1_init);
510module_exit(sm_smsc47m1_exit);
Note: See TracBrowser for help on using the browser.