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

Revision 2784, 14.3 KB (checked in by khali, 9 years ago)

Restore controlling_mod argument to i2c_register_entry(). This
is needed to properly lock chip drivers in memory while anyone uses their
/proc entries. This also brings back compatibility with the 2.4 Linux
kernel.

  • 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 int smsc47m1_id = 0;
164
165static struct i2c_driver smsc47m1_driver = {
166        .name           = "SMSC 47M1xx fan monitor",
167        .id             = I2C_DRIVERID_SMSC47M1,
168        .flags          = I2C_DF_NOTIFY,
169        .attach_adapter = smsc47m1_attach_adapter,
170        .detach_client  = smsc47m1_detach_client,
171};
172
173/* -- SENSORS SYSCTL START -- */
174#define SMSC47M1_SYSCTL_FAN1 1101   /* Rotations/min */
175#define SMSC47M1_SYSCTL_FAN2 1102
176#define SMSC47M1_SYSCTL_PWM1 1401
177#define SMSC47M1_SYSCTL_PWM2 1402
178#define SMSC47M1_SYSCTL_FAN_DIV 2000        /* 1, 2, 4 or 8 */
179#define SMSC47M1_SYSCTL_ALARMS 2004    /* bitvector */
180
181#define SMSC47M1_ALARM_FAN1 0x0001
182#define SMSC47M1_ALARM_FAN2 0x0002
183
184/* -- SENSORS SYSCTL END -- */
185
186static ctl_table smsc47m1_dir_table_template[] = {
187        {SMSC47M1_SYSCTL_FAN1, "fan1", NULL, 0, 0644, NULL, &i2c_proc_real,
188         &i2c_sysctl_real, NULL, &smsc47m1_fan},
189        {SMSC47M1_SYSCTL_FAN2, "fan2", NULL, 0, 0644, NULL, &i2c_proc_real,
190         &i2c_sysctl_real, NULL, &smsc47m1_fan},
191        {SMSC47M1_SYSCTL_FAN_DIV, "fan_div", NULL, 0, 0644, NULL, &i2c_proc_real,
192         &i2c_sysctl_real, NULL, &smsc47m1_fan_div},
193        {SMSC47M1_SYSCTL_ALARMS, "alarms", NULL, 0, 0444, NULL, &i2c_proc_real,
194         &i2c_sysctl_real, NULL, &smsc47m1_alarms},
195        {SMSC47M1_SYSCTL_PWM1, "pwm1", NULL, 0, 0644, NULL, &i2c_proc_real,
196         &i2c_sysctl_real, NULL, &smsc47m1_pwm},
197        {SMSC47M1_SYSCTL_PWM2, "pwm2", NULL, 0, 0644, NULL, &i2c_proc_real,
198         &i2c_sysctl_real, NULL, &smsc47m1_pwm},
199        {0}
200};
201
202static int smsc47m1_attach_adapter(struct i2c_adapter *adapter)
203{
204        return i2c_detect(adapter, &addr_data, smsc47m1_detect);
205}
206
207static int smsc47m1_find(int *address)
208{
209        u16 val;
210
211        superio_enter();
212        val= superio_inb(DEVID);
213        if (!SMSC_DEVID_MATCH(val)) {
214                superio_exit();
215                return -ENODEV;
216        }
217
218        superio_select();
219        val = (superio_inb(SMSC_BASE_REG) << 8) |
220               superio_inb(SMSC_BASE_REG + 1);
221        *address = val & ~(SMSC_EXTENT - 1);
222        if (*address == 0 && force_addr == 0) {
223                printk("smsc47m1.o: base address not set - use force_addr=0xaddr\n");
224                superio_exit();
225                return -ENODEV;
226        }
227        if (force_addr)
228                *address = force_addr;  /* so detect will get called */
229
230        superio_exit();
231        return 0;
232}
233
234int smsc47m1_detect(struct i2c_adapter *adapter, int address,
235                   unsigned short flags, int kind)
236{
237        int i;
238        struct i2c_client *new_client;
239        struct smsc47m1_data *data;
240        int err = 0;
241        const char *type_name = "smsc47m1";
242        const char *client_name = "47M1xx chip";
243
244        if (!i2c_is_isa_adapter(adapter)) {
245                return 0;
246        }
247
248        if(force_addr)
249                address = force_addr & ~(SMSC_EXTENT - 1);
250        if (check_region(address, SMSC_EXTENT)) {
251                printk("smsc47m1.o: region 0x%x already in use!\n", address);
252                return -ENODEV;
253        }
254        if(force_addr) {
255                printk("smsc47m1.o: forcing ISA address 0x%04X\n", address);
256                superio_enter();
257                superio_select();
258                superio_outb(SMSC_BASE_REG, address >> 8);
259                superio_outb(SMSC_BASE_REG+1, address & 0xff);
260                superio_exit();
261        }
262
263        if (!(data = kmalloc(sizeof(struct smsc47m1_data), GFP_KERNEL))) {
264                return -ENOMEM;
265        }
266
267        new_client = &data->client;
268        new_client->addr = address;
269        init_MUTEX(&data->lock);
270        new_client->data = data;
271        new_client->adapter = adapter;
272        new_client->driver = &smsc47m1_driver;
273        new_client->flags = 0;
274
275        request_region(address, SMSC_EXTENT, "smsc47m1-fans");
276        strcpy(new_client->name, client_name);
277
278        new_client->id = smsc47m1_id++;
279        data->valid = 0;
280        init_MUTEX(&data->update_lock);
281
282        if ((err = i2c_attach_client(new_client)))
283                goto ERROR3;
284
285        if ((i = i2c_register_entry((struct i2c_client *) new_client,
286                                        type_name,
287                                        smsc47m1_dir_table_template,
288                                        THIS_MODULE)) < 0) {
289                err = i;
290                goto ERROR4;
291        }
292        data->sysctl_id = i;
293
294        smsc47m1_init_client(new_client);
295        return 0;
296
297      ERROR4:
298        i2c_detach_client(new_client);
299      ERROR3:
300        release_region(address, SMSC_EXTENT);
301        kfree(data);
302        return err;
303}
304
305static int smsc47m1_detach_client(struct i2c_client *client)
306{
307        int err;
308
309        i2c_deregister_entry(((struct smsc47m1_data *) (client->data))->
310                                 sysctl_id);
311
312        if ((err = i2c_detach_client(client))) {
313                printk
314                    ("smsc47m1.o: Client deregistration failed, client not detached.\n");
315                return err;
316        }
317
318        release_region(client->addr, SMSC_EXTENT);
319        kfree(client->data);
320
321        return 0;
322}
323
324static int smsc47m1_read_value(struct i2c_client *client, u8 reg)
325{
326        int res;
327
328        down(&(((struct smsc47m1_data *) (client->data))->lock));
329        res = inb_p(client->addr + reg);
330        up(&(((struct smsc47m1_data *) (client->data))->lock));
331        return res;
332}
333
334static int smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value)
335{
336        down(&(((struct smsc47m1_data *) (client->data))->lock));
337        outb_p(value, client->addr + reg);
338        up(&(((struct smsc47m1_data *) (client->data))->lock));
339        return 0;
340}
341
342static void smsc47m1_init_client(struct i2c_client *client)
343{
344        /* configure pins for tach function */
345        smsc47m1_write_value(client, SMSC47M1_REG_TPIN1, 0x05);
346        smsc47m1_write_value(client, SMSC47M1_REG_TPIN2, 0x05);
347}
348
349static void smsc47m1_update_client(struct i2c_client *client)
350{
351        struct smsc47m1_data *data = client->data;
352        int i;
353
354        down(&data->update_lock);
355
356        if ((jiffies - data->last_updated > HZ + HZ / 2) ||
357            (jiffies < data->last_updated) || !data->valid) {
358                for (i = 1; i <= 2; i++) {
359                        data->fan[i - 1] =
360                            smsc47m1_read_value(client, SMSC47M1_REG_FAN(i));
361                        data->fan_min[i - 1] =
362                            smsc47m1_read_value(client, SMSC47M1_REG_FAN_MIN(i));
363                        data->pwm[i - 1] =
364                            smsc47m1_read_value(client, SMSC47M1_REG_PWM(i));
365                }
366
367                i = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV);
368                data->fan_div[0] = (i >> 4) & 0x03;
369                data->fan_div[1] = i >> 6;
370                data->alarms =
371                        smsc47m1_read_value(client, SMSC47M1_REG_ALARM1) >> 6;
372                if(data->alarms)
373                        smsc47m1_write_value(client, SMSC47M1_REG_ALARM1, 0xc0);
374                data->last_updated = jiffies;
375                data->valid = 1;
376        }
377
378        up(&data->update_lock);
379}
380
381
382void smsc47m1_fan(struct i2c_client *client, int operation, int ctl_name,
383                 int *nrels_mag, long *results)
384{
385        struct smsc47m1_data *data = client->data;
386        int nr = ctl_name - SMSC47M1_SYSCTL_FAN1 + 1;
387
388        if (operation == SENSORS_PROC_REAL_INFO)
389                *nrels_mag = 0;
390        else if (operation == SENSORS_PROC_REAL_READ) {
391                smsc47m1_update_client(client);
392                results[0] = MIN_FROM_REG(data->fan_min[nr - 1],
393                                          DIV_FROM_REG(data->fan_div[nr - 1]));
394                results[1] = FAN_FROM_REG(data->fan[nr - 1],
395                                          DIV_FROM_REG(data->fan_div[nr - 1]),
396                                          data->fan_min[nr - 1]);
397                *nrels_mag = 2;
398        } else if (operation == SENSORS_PROC_REAL_WRITE) {
399                if (*nrels_mag >= 1) {
400                        data->fan_min[nr - 1] = MIN_TO_REG(results[0],
401                                                           DIV_FROM_REG
402                                                           (data->
403                                                            fan_div[nr-1]));
404                        smsc47m1_write_value(client, SMSC47M1_REG_FAN_MIN(nr),
405                                            data->fan_min[nr - 1]);
406                }
407        }
408}
409
410
411void smsc47m1_alarms(struct i2c_client *client, int operation, int ctl_name,
412                    int *nrels_mag, long *results)
413{
414        struct smsc47m1_data *data = client->data;
415        if (operation == SENSORS_PROC_REAL_INFO)
416                *nrels_mag = 0;
417        else if (operation == SENSORS_PROC_REAL_READ) {
418                smsc47m1_update_client(client);
419                results[0] = data->alarms;
420                *nrels_mag = 1;
421        }
422}
423
424void smsc47m1_fan_div(struct i2c_client *client, int operation,
425                     int ctl_name, int *nrels_mag, long *results)
426{
427        struct smsc47m1_data *data = client->data;
428        int old;
429
430        if (operation == SENSORS_PROC_REAL_INFO)
431                *nrels_mag = 0;
432        else if (operation == SENSORS_PROC_REAL_READ) {
433                smsc47m1_update_client(client);
434                results[0] = DIV_FROM_REG(data->fan_div[0]);
435                results[1] = DIV_FROM_REG(data->fan_div[1]);
436                *nrels_mag = 2;
437        } else if (operation == SENSORS_PROC_REAL_WRITE) {
438                old = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV);
439                if (*nrels_mag >= 2) {
440                        data->fan_div[1] = DIV_TO_REG(results[1]);
441                        old = (old & 0x3f) | (data->fan_div[1] << 6);
442                }
443                if (*nrels_mag >= 1) {
444                        data->fan_div[0] = DIV_TO_REG(results[0]);
445                        old = (old & 0xcf) | (data->fan_div[0] << 4);
446                        smsc47m1_write_value(client, SMSC47M1_REG_FANDIV, old);
447                }
448        }
449}
450
451void smsc47m1_pwm(struct i2c_client *client, int operation, int ctl_name,
452                 int *nrels_mag, long *results)
453{
454        struct smsc47m1_data *data = client->data;
455        int nr = 1 + ctl_name - SMSC47M1_SYSCTL_PWM1;
456
457        if (operation == SENSORS_PROC_REAL_INFO)
458                *nrels_mag = 0;
459        else if (operation == SENSORS_PROC_REAL_READ) {
460                smsc47m1_update_client(client);
461                results[0] = PWM_FROM_REG(data->pwm[nr - 1]);
462                results[1] = data->pwm[nr - 1] & 0x01;
463                *nrels_mag = 2;
464        } else if (operation == SENSORS_PROC_REAL_WRITE) {
465                if (*nrels_mag >= 1) {
466                        data->pwm[nr - 1] &= 0x81;
467                        data->pwm[nr - 1] |= PWM_TO_REG(results[0]);
468                        if (*nrels_mag >= 2) {
469                                if(results[1] && (data->pwm[nr-1] & 0x01)) {
470                                        /* enable PWM */
471/* hope BIOS did it already
472                                        smsc47m1_write_value(client,
473                                                  SMSC47M1_REG_PPIN(nr), 0x04);
474*/
475                                        data->pwm[nr - 1] &= 0xfe;
476                                } else if((!results[1]) && (!(data->pwm[nr-1] & 0x01))) {
477                                        /* disable PWM */
478                                        data->pwm[nr - 1] |= 0x01;
479                                }
480                        }
481                        smsc47m1_write_value(client, SMSC47M1_REG_PWM(nr),
482                                             data->pwm[nr - 1]);
483                }
484        }
485}
486
487static int __init sm_smsc47m1_init(void)
488{
489        int addr;
490
491        printk("smsc47m1.o version %s (%s)\n", LM_VERSION, LM_DATE);
492
493        if (smsc47m1_find(&addr)) {
494                printk("smsc47m1.o: SMSC 47M1xx not detected, module not inserted.\n");
495                return -ENODEV;
496        }
497        normal_isa[0] = addr;
498
499        return i2c_add_driver(&smsc47m1_driver);
500}
501
502static void __exit sm_smsc47m1_exit(void)
503{
504        i2c_del_driver(&smsc47m1_driver);
505}
506
507
508
509MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
510MODULE_DESCRIPTION("SMSC 47M1xx Fan sensors");
511MODULE_LICENSE("GPL");
512
513module_init(sm_smsc47m1_init);
514module_exit(sm_smsc47m1_exit);
Note: See TracBrowser for help on using the browser.