root/lm-sensors/trunk/kernel/chips/sis5595.c @ 632

Revision 632, 24.5 KB (checked in by kmalkki, 14 years ago)

(Kyösti)

  • SiS 5595 patches, thanks to Olaf Titz
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    sis5595.c - Part of lm_sensors, Linux kernel modules
3                for hardware monitoring
4               
5    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>,
6                        Kyösti Mälkki <kmalkki@cc.hut.fi>
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23#include <linux/module.h>
24#include <linux/malloc.h>
25#include <linux/proc_fs.h>
26#include <linux/ioport.h>
27#include <linux/sysctl.h>
28#include <linux/pci.h>
29#if LINUX_VERSION_CODE < 0x020136 /* 2.1.54 */
30#include <linux/bios32.h>
31#endif
32#include <asm/errno.h>
33#include <asm/io.h>
34#include <linux/types.h>
35#include <linux/i2c.h>
36#include "version.h"
37#include "i2c-isa.h"
38#include "sensors.h"
39#include "compat.h"
40
41#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,53)
42#include <linux/init.h>
43#else
44#define __init
45#define __initdata
46#endif
47
48
49/* Addresses to scan.
50   Note that we can't determine the ISA address until we have initialized
51   our module */
52static unsigned short normal_i2c[] = {SENSORS_I2C_END};
53static unsigned short normal_i2c_range[] = {SENSORS_I2C_END};
54static unsigned int normal_isa[] = {0x0000,SENSORS_ISA_END};
55static unsigned int normal_isa_range[] = {SENSORS_ISA_END};
56
57/* Insmod parameters */
58SENSORS_INSMOD_1(sis5595);
59
60/*
61   SiS southbridge has a LM78-like chip integrated on the same IC.
62   This driver is a customized copy of lm78.c
63*/
64
65/* Many SIS5595 constants specified below */
66
67/* Length of ISA address segment */
68#define SIS5595_EXTENT 8
69#define SIS5595_BASE_REG 0x68
70
71/* Where are the ISA address/data registers relative to the base address */
72#define SIS5595_ADDR_REG_OFFSET 5
73#define SIS5595_DATA_REG_OFFSET 6
74
75/* The SIS5595 registers */
76#define SIS5595_REG_IN_MAX(nr) (0x2b + (nr) * 2)
77#define SIS5595_REG_IN_MIN(nr) (0x2c + (nr) * 2)
78#define SIS5595_REG_IN(nr) (0x20 + (nr))
79
80#define SIS5595_REG_FAN_MIN(nr) (0x3a + (nr))
81#define SIS5595_REG_FAN(nr) (0x27 + (nr))
82
83#define SIS5595_REG_TEMP 0x27
84#define SIS5595_REG_TEMP_OVER 0x39
85#define SIS5595_REG_TEMP_HYST 0x3a
86
87#define SIS5595_REG_ALARM1 0x41
88
89#define SIS5595_REG_FANDIV 0x47
90
91#define SIS5595_REG_CONFIG 0x40
92
93/* Conversions. Rounding and limit checking is only done on the TO_REG
94   variants. Note that you should be a bit careful with which arguments
95   these macros are called: arguments may be evaluated more than once.
96   Fixing this is just not worth it. */
97
98#define IN_TO_REG(val)  (SENSORS_LIMIT((((val) * 10 + 8)/16),0,255))
99#define IN_FROM_REG(val) (((val) *  16) / 10)
100
101extern inline u8 FAN_TO_REG(long rpm, int div)
102{
103  if (rpm == 0)
104    return 255;
105  rpm = SENSORS_LIMIT(rpm,1,1000000);
106  return SENSORS_LIMIT((1350000 + rpm*div/2) / (rpm*div),1,254);
107}
108
109#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
110
111#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-5)/10):\
112                                                 ((val)+5)/10),0,255))
113#define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*10)
114
115#define ALARMS_FROM_REG(val) (val)
116
117#define DIV_FROM_REG(val) (1 << (val))
118#define DIV_TO_REG(val) ((val)==8?3:(val)==4?2:(val)==1?0:1)
119
120/* Initial limits. To keep them sane, we use the 'standard' translation as
121   specified in the SIS5595 sheet. Use the config file to set better limits. */
122#define SIS5595_INIT_IN_0 (((1200)  * 10)/38)
123#define SIS5595_INIT_IN_1 (((500)   * 100)/168)
124#define SIS5595_INIT_IN_2 330
125#define SIS5595_INIT_IN_3 250
126
127#define SIS5595_INIT_IN_PERCENTAGE 10
128
129#define SIS5595_INIT_IN_MIN_0 \
130        (SIS5595_INIT_IN_0 - SIS5595_INIT_IN_0 * SIS5595_INIT_IN_PERCENTAGE / 100)
131#define SIS5595_INIT_IN_MAX_0 \
132        (SIS5595_INIT_IN_0 + SIS5595_INIT_IN_0 * SIS5595_INIT_IN_PERCENTAGE / 100)
133#define SIS5595_INIT_IN_MIN_1 \
134        (SIS5595_INIT_IN_1 - SIS5595_INIT_IN_1 * SIS5595_INIT_IN_PERCENTAGE / 100)
135#define SIS5595_INIT_IN_MAX_1 \
136        (SIS5595_INIT_IN_1 + SIS5595_INIT_IN_1 * SIS5595_INIT_IN_PERCENTAGE / 100)
137#define SIS5595_INIT_IN_MIN_2 \
138        (SIS5595_INIT_IN_2 - SIS5595_INIT_IN_2 * SIS5595_INIT_IN_PERCENTAGE / 100)
139#define SIS5595_INIT_IN_MAX_2 \
140        (SIS5595_INIT_IN_2 + SIS5595_INIT_IN_2 * SIS5595_INIT_IN_PERCENTAGE / 100)
141#define SIS5595_INIT_IN_MIN_3 \
142        (SIS5595_INIT_IN_3 - SIS5595_INIT_IN_3 * SIS5595_INIT_IN_PERCENTAGE / 100)
143#define SIS5595_INIT_IN_MAX_3 \
144        (SIS5595_INIT_IN_3 + SIS5595_INIT_IN_3 * SIS5595_INIT_IN_PERCENTAGE / 100)
145
146#define SIS5595_INIT_FAN_MIN_1 3000
147#define SIS5595_INIT_FAN_MIN_2 3000
148
149#define SIS5595_INIT_TEMP_OVER 600
150#define SIS5595_INIT_TEMP_HYST 500
151
152#ifdef MODULE
153extern int init_module(void);
154extern int cleanup_module(void);
155#endif /* MODULE */
156
157/* This module may seem overly long and complicated. In fact, it is not so
158   bad. Quite a lot of bookkeeping is done. A real driver can often cut
159   some corners. */
160
161/* For each registered SIS5595, we need to keep some data in memory. That
162   data is pointed to by sis5595_list[NR]->data. The structure itself is
163   dynamically allocated, at the same time when a new sis5595 client is
164   allocated. */
165struct sis5595_data {
166         struct semaphore lock;
167         int sysctl_id;
168
169         struct semaphore update_lock;
170         char valid;                 /* !=0 if following fields are valid */
171         unsigned long last_updated; /* In jiffies */
172
173         u8 in[4];                   /* Register value */
174         u8 in_max[4];               /* Register value */
175         u8 in_min[4];               /* Register value */
176         u8 fan[2];                  /* Register value */
177         u8 fan_min[2];              /* Register value */
178         u8 temp;                    /* Register value */
179         u8 temp_over;               /* Register value */
180         u8 temp_hyst;               /* Register value */
181         u8 fan_div[2];              /* Register encoding, shifted right */
182         u8 alarms;                 /* Register encoding, combined */
183};
184
185
186#ifdef MODULE
187static
188#else
189extern
190#endif
191       int __init sensors_sis5595_init(void);
192static int __init sis5595_cleanup(void);
193
194static int sis5595_attach_adapter(struct i2c_adapter *adapter);
195static int sis5595_detect(struct i2c_adapter *adapter, int address, 
196                          unsigned short flags, int kind);
197static int sis5595_detach_client(struct i2c_client *client);
198static int sis5595_command(struct i2c_client *client, unsigned int cmd, 
199                        void *arg);
200static void sis5595_inc_use (struct i2c_client *client);
201static void sis5595_dec_use (struct i2c_client *client);
202
203static int sis5595_read_value(struct i2c_client *client, u8 register);
204static int sis5595_write_value(struct i2c_client *client, u8 register, u8 value);
205static void sis5595_update_client(struct i2c_client *client);
206static void sis5595_init_client(struct i2c_client *client);
207static int sis5595_find_sis(int *address);
208
209
210static void sis5595_in(struct i2c_client *client, int operation, int ctl_name,
211                    int *nrels_mag, long *results);
212static void sis5595_fan(struct i2c_client *client, int operation, int ctl_name,
213                     int *nrels_mag, long *results);
214static void sis5595_temp(struct i2c_client *client, int operation, int ctl_name,
215                      int *nrels_mag, long *results);
216static void sis5595_alarms(struct i2c_client *client, int operation, int ctl_name,
217                        int *nrels_mag, long *results);
218static void sis5595_fan_div(struct i2c_client *client, int operation, int ctl_name,
219                         int *nrels_mag, long *results);
220
221static int sis5595_id = 0;
222
223/* The driver. I choose to use type i2c_driver, as at is identical to both
224   smbus_driver and isa_driver, and clients could be of either kind */
225static struct i2c_driver sis5595_driver = {
226  /* name */            "SiS 5595",
227  /* id */              I2C_DRIVERID_SIS5595,
228  /* flags */           I2C_DF_NOTIFY,
229  /* attach_adapter */  &sis5595_attach_adapter,
230  /* detach_client */   &sis5595_detach_client,
231  /* command */         &sis5595_command,
232  /* inc_use */         &sis5595_inc_use,
233  /* dec_use */         &sis5595_dec_use
234};
235
236/* Used by sis5595_init/cleanup */
237static int __initdata sis5595_initialized = 0;
238
239/* The /proc/sys entries */
240/* These files are created for each detected SIS5595. This is just a template;
241   though at first sight, you might think we could use a statically
242   allocated list, we need some way to get back to the parent - which
243   is done through one of the 'extra' fields which are initialized
244   when a new copy is allocated. */
245static ctl_table sis5595_dir_table_template[] = {
246  { SIS5595_SYSCTL_IN0, "in0", NULL, 0, 0644, NULL, &sensors_proc_real,
247    &sensors_sysctl_real, NULL, &sis5595_in },
248  { SIS5595_SYSCTL_IN1, "in1", NULL, 0, 0644, NULL, &sensors_proc_real,
249    &sensors_sysctl_real, NULL, &sis5595_in },
250  { SIS5595_SYSCTL_IN2, "in2", NULL, 0, 0644, NULL, &sensors_proc_real,
251    &sensors_sysctl_real, NULL, &sis5595_in },
252  { SIS5595_SYSCTL_IN3, "in3", NULL, 0, 0644, NULL, &sensors_proc_real,
253    &sensors_sysctl_real, NULL, &sis5595_in },
254  { SIS5595_SYSCTL_FAN1, "fan1", NULL, 0, 0644, NULL, &sensors_proc_real,
255    &sensors_sysctl_real, NULL, &sis5595_fan },
256  { SIS5595_SYSCTL_FAN2, "fan2", NULL, 0, 0644, NULL, &sensors_proc_real,
257    &sensors_sysctl_real, NULL, &sis5595_fan },
258  { SIS5595_SYSCTL_TEMP, "temp", NULL, 0, 0644, NULL, &sensors_proc_real,
259    &sensors_sysctl_real, NULL, &sis5595_temp },
260  { SIS5595_SYSCTL_FAN_DIV, "fan_div", NULL, 0, 0644, NULL, &sensors_proc_real,
261    &sensors_sysctl_real, NULL, &sis5595_fan_div },
262  { SIS5595_SYSCTL_ALARMS, "alarms", NULL, 0, 0444, NULL, &sensors_proc_real,
263    &sensors_sysctl_real, NULL, &sis5595_alarms },
264  { 0 }
265};
266
267/* This is called when the module is loaded */
268int sis5595_attach_adapter(struct i2c_adapter *adapter)
269{
270  return sensors_detect(adapter,&addr_data,sis5595_detect);
271}
272
273/* Locate SiS bridge and correct base address for SIS5595 */
274int sis5595_find_sis(int *address)
275{
276#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,54))
277  struct pci_dev *s_bridge;
278#else
279  unsigned char SIS_bus, SIS_devfn;
280#endif
281  u16 val;
282
283  if (! pci_present())
284    return -ENODEV;
285
286#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,54))
287  if (! (s_bridge = pci_find_device(
288                   PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503, NULL)))
289               
290#else
291  if(pcibios_find_device(
292                PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503, 0,
293                &SIS_bus, &SIS_devfn))
294#endif
295    return -ENODEV;
296
297
298  if ( PCIBIOS_SUCCESSFUL !=
299        pci_read_config_word_united(s_bridge, SIS_bus, SIS_devfn, 
300                                    SIS5595_BASE_REG, &val))
301    return -ENODEV;
302               
303  *address = (val & 0xfff8);
304  return 0;
305}
306
307int sis5595_detect(struct i2c_adapter *adapter, int address, 
308                   unsigned short flags, int kind)
309{
310  int i;
311  struct i2c_client *new_client;
312  struct sis5595_data *data;
313  int err=0;
314  const char *type_name = "";
315  const char *client_name = "";
316
317  /* Make sure we are probing the ISA bus!!  */
318  if (!i2c_is_isa_adapter(adapter)) {
319    printk("sis5595.o: sis5595_detect called for an I2C bus adapter?!?\n");
320    return 0;
321  }
322
323  if (check_region(address,SIS5595_EXTENT))
324    goto ERROR0;
325
326  /* If this is the address as indicated by the SIS5595 chipset, we don't
327     do any futher probing */
328  if ((kind < 0) && (address == normal_isa[0]))
329    kind = 0;
330
331  /* Probe whether there is anything available on this address. */
332  if (kind < 0) {
333#define REALLY_SLOW_IO
334    /* We need the timeouts for at least some LM78-like chips. But only
335       if we read 'undefined' registers. */
336    i = inb_p(address + 1);
337    if (inb_p(address + 2) != i)
338      goto ERROR0;
339    if (inb_p(address + 3) != i)
340      goto ERROR0;
341    if (inb_p(address + 7) != i)
342      goto ERROR0;
343#undef REALLY_SLOW_IO
344
345    /* Let's just hope nothing breaks here */
346    i = inb_p(address + 5) & 0x7f;
347    outb_p(~i & 0x7f,address+5);
348    if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
349      outb_p(i,address+5);
350      return 0;
351    }
352  }
353
354  /* OK. For now, we presume we have a valid client. We now create the
355     client structure, even though we cannot fill it completely yet.
356     But it allows us to access sis5595_{read,write}_value. */
357
358  if (! (new_client = kmalloc(sizeof(struct i2c_client) +
359                              sizeof(struct sis5595_data),
360                              GFP_KERNEL))) {
361    err = -ENOMEM;
362    goto ERROR0;
363  }
364
365  data = (struct sis5595_data *) (new_client + 1);
366  new_client->addr = address;
367  init_MUTEX(&data->lock);
368  new_client->data = data;
369  new_client->adapter = adapter;
370  new_client->driver = &sis5595_driver;
371  new_client->flags = 0;
372
373  /* Now, we do the remaining detection. */
374
375  if (kind < 0) {
376    if (sis5595_read_value(new_client,SIS5595_REG_CONFIG) & 0x80)
377      goto ERROR1;
378  }
379
380  /* Determine the chip type. */
381  if (kind <= 0) 
382    kind = sis5595;
383
384  if (kind == sis5595) {
385    type_name = "sis5595";
386    client_name = "SIS5595 chip";
387  } else {
388#ifdef DEBUG
389    printk("sis5595.o: Internal error: unknown kind (%d)?!?",kind);
390#endif
391    goto ERROR1;
392  }
393
394  /* Reserve the ISA region */
395  request_region(address, SIS5595_EXTENT, type_name);
396
397  /* Fill in the remaining client fields and put it into the global list */
398  strcpy(new_client->name,client_name);
399
400  new_client->id = sis5595_id++;
401  data->valid = 0;
402  init_MUTEX(&data->update_lock);
403
404  /* Tell the I2C layer a new client has arrived */
405  if ((err = i2c_attach_client(new_client)))
406    goto ERROR3;
407
408  /* Register a new directory entry with module sensors */
409  if ((i = sensors_register_entry((struct i2c_client *) new_client,
410                                  type_name,
411                                  sis5595_dir_table_template)) < 0) {
412    err = i;
413    goto ERROR4;
414  }
415  data->sysctl_id = i;
416
417  /* Initialize the SIS5595 chip */
418  sis5595_init_client(new_client);
419  return 0;
420
421/* OK, this is not exactly good programming practice, usually. But it is
422   very code-efficient in this case. */
423
424ERROR4:
425  i2c_detach_client(new_client);
426ERROR3:
427  release_region(address,SIS5595_EXTENT);
428ERROR1:
429  kfree(new_client);
430ERROR0:
431  return err;
432}
433
434int sis5595_detach_client(struct i2c_client *client)
435{
436  int err;
437
438  sensors_deregister_entry(((struct sis5595_data *)(client->data))->sysctl_id);
439
440  if ((err = i2c_detach_client(client))) {
441    printk("sis5595.o: Client deregistration failed, client not detached.\n");
442    return err;
443  }
444
445  release_region(client->addr,SIS5595_EXTENT);
446  kfree(client);
447
448  return 0;
449}
450
451/* No commands defined yet */
452int sis5595_command(struct i2c_client *client, unsigned int cmd, void *arg)
453{
454  return 0;
455}
456
457/* Nothing here yet */
458void sis5595_inc_use (struct i2c_client *client)
459{
460#ifdef MODULE
461  MOD_INC_USE_COUNT;
462#endif
463}
464
465/* Nothing here yet */
466void sis5595_dec_use (struct i2c_client *client)
467{
468#ifdef MODULE
469  MOD_DEC_USE_COUNT;
470#endif
471}
472 
473
474/* The SMBus locks itself, but ISA access must be locked explicitely!
475   There are some ugly typecasts here, but the good new is - they should
476   nowhere else be necessary! */
477int sis5595_read_value(struct i2c_client *client, u8 reg)
478{
479    int res;
480   
481    down(& (((struct sis5595_data *) (client->data)) -> lock));
482    outb_p(reg,client->addr + SIS5595_ADDR_REG_OFFSET);
483    res = inb_p(client->addr + SIS5595_DATA_REG_OFFSET);
484    up( & (((struct sis5595_data *) (client->data)) -> lock));
485    return res;
486}
487
488/* The SMBus locks itself, but ISA access muse be locked explicitely!
489   There are some ugly typecasts here, but the good new is - they should
490   nowhere else be necessary! */
491int sis5595_write_value(struct i2c_client *client, u8 reg, u8 value)
492{
493    down(& (((struct sis5595_data *) (client->data)) -> lock));
494    outb_p(reg,client->addr + SIS5595_ADDR_REG_OFFSET);
495    outb_p(value,client->addr + SIS5595_DATA_REG_OFFSET);
496    up( & (((struct sis5595_data *) (client->data)) -> lock));
497    return 0;
498}
499
500/* Called when we have found a new SIS5595. It should set limits, etc. */
501void sis5595_init_client(struct i2c_client *client)
502{
503  /* Reset all except Watchdog values and last conversion values
504     This sets fan-divs to 2, among others */
505  sis5595_write_value(client,SIS5595_REG_CONFIG,0x80);
506
507  sis5595_write_value(client,SIS5595_REG_IN_MIN(0),IN_TO_REG(SIS5595_INIT_IN_MIN_0));
508  sis5595_write_value(client,SIS5595_REG_IN_MAX(0),IN_TO_REG(SIS5595_INIT_IN_MAX_0));
509  sis5595_write_value(client,SIS5595_REG_IN_MIN(1),IN_TO_REG(SIS5595_INIT_IN_MIN_1));
510  sis5595_write_value(client,SIS5595_REG_IN_MAX(1),IN_TO_REG(SIS5595_INIT_IN_MAX_1));
511  sis5595_write_value(client,SIS5595_REG_IN_MIN(2),IN_TO_REG(SIS5595_INIT_IN_MIN_2));
512  sis5595_write_value(client,SIS5595_REG_IN_MAX(2),IN_TO_REG(SIS5595_INIT_IN_MAX_2));
513  sis5595_write_value(client,SIS5595_REG_IN_MIN(3),IN_TO_REG(SIS5595_INIT_IN_MIN_3));
514  sis5595_write_value(client,SIS5595_REG_IN_MAX(3),IN_TO_REG(SIS5595_INIT_IN_MAX_3));
515  sis5595_write_value(client,SIS5595_REG_FAN_MIN(1),
516                   FAN_TO_REG(SIS5595_INIT_FAN_MIN_1,2));
517  sis5595_write_value(client,SIS5595_REG_FAN_MIN(2),
518                   FAN_TO_REG(SIS5595_INIT_FAN_MIN_2,2));
519  sis5595_write_value(client,SIS5595_REG_TEMP_OVER,TEMP_TO_REG(SIS5595_INIT_TEMP_OVER));
520  sis5595_write_value(client,SIS5595_REG_TEMP_HYST,TEMP_TO_REG(SIS5595_INIT_TEMP_HYST));
521
522  /* Start monitoring */
523  sis5595_write_value(client,SIS5595_REG_CONFIG,
524                   (sis5595_read_value(client,SIS5595_REG_CONFIG) & 0xf7) | 0x01);
525 
526}
527
528void sis5595_update_client(struct i2c_client *client)
529{
530  struct sis5595_data *data = client->data;
531  int i;
532
533  down(&data->update_lock);
534
535  if ((jiffies - data->last_updated > HZ+HZ/2 ) ||
536      (jiffies < data->last_updated) || ! data->valid) {
537
538#ifdef DEBUG
539    printk("Starting sis5595 update\n");
540#endif
541    for (i = 0; i <= 3; i++) {
542      data->in[i]     = sis5595_read_value(client,SIS5595_REG_IN(i));
543      data->in_min[i] = sis5595_read_value(client,SIS5595_REG_IN_MIN(i));
544      data->in_max[i] = sis5595_read_value(client,SIS5595_REG_IN_MAX(i));
545    }
546    for (i = 1; i <= 2; i++) {
547      data->fan[i-1] = sis5595_read_value(client,SIS5595_REG_FAN(i));
548      data->fan_min[i-1] = sis5595_read_value(client,SIS5595_REG_FAN_MIN(i));
549    }
550    data->temp = sis5595_read_value(client,SIS5595_REG_TEMP);
551    data->temp_over = sis5595_read_value(client,SIS5595_REG_TEMP_OVER);
552    data->temp_hyst = sis5595_read_value(client,SIS5595_REG_TEMP_HYST);
553    i = sis5595_read_value(client,SIS5595_REG_FANDIV);
554    data->fan_div[0] = (i >> 4) & 0x03;
555    data->fan_div[1] = i >> 6;
556    data->alarms = sis5595_read_value(client,SIS5595_REG_ALARM1);
557    data->last_updated = jiffies;
558    data->valid = 1;
559  }
560
561  up(&data->update_lock);
562}
563
564
565/* The next few functions are the call-back functions of the /proc/sys and
566   sysctl files. Which function is used is defined in the ctl_table in
567   the extra1 field.
568   Each function must return the magnitude (power of 10 to divide the date
569   with) if it is called with operation==SENSORS_PROC_REAL_INFO. It must
570   put a maximum of *nrels elements in results reflecting the data of this
571   file, and set *nrels to the number it actually put in it, if operation==
572   SENSORS_PROC_REAL_READ. Finally, it must get upto *nrels elements from
573   results and write them to the chip, if operations==SENSORS_PROC_REAL_WRITE.
574   Note that on SENSORS_PROC_REAL_READ, I do not check whether results is
575   large enough (by checking the incoming value of *nrels). This is not very
576   good practice, but as long as you put less than about 5 values in results,
577   you can assume it is large enough. */
578void sis5595_in(struct i2c_client *client, int operation, int ctl_name, 
579             int *nrels_mag, long *results)
580{
581  struct sis5595_data *data = client->data;
582  int nr = ctl_name - SIS5595_SYSCTL_IN0;
583
584  if (operation == SENSORS_PROC_REAL_INFO)
585    *nrels_mag = 2;
586  else if (operation == SENSORS_PROC_REAL_READ) {
587    sis5595_update_client(client);
588    results[0] = IN_FROM_REG(data->in_min[nr]);
589    results[1] = IN_FROM_REG(data->in_max[nr]);
590    results[2] = IN_FROM_REG(data->in[nr]);
591    *nrels_mag = 3;
592  } else if (operation == SENSORS_PROC_REAL_WRITE) {
593      if (*nrels_mag >= 1) {
594        data->in_min[nr] = IN_TO_REG(results[0]);
595        sis5595_write_value(client,SIS5595_REG_IN_MIN(nr),data->in_min[nr]);
596      }
597      if (*nrels_mag >= 2) {
598        data->in_max[nr] = IN_TO_REG(results[1]);
599        sis5595_write_value(client,SIS5595_REG_IN_MAX(nr),data->in_max[nr]);
600      }
601  }
602}
603
604void sis5595_fan(struct i2c_client *client, int operation, int ctl_name,
605              int *nrels_mag, long *results)
606{
607  struct sis5595_data *data = client->data;
608  int nr = ctl_name - SIS5595_SYSCTL_FAN1 + 1;
609
610  if (operation == SENSORS_PROC_REAL_INFO)
611    *nrels_mag = 0;
612  else if (operation == SENSORS_PROC_REAL_READ) {
613    sis5595_update_client(client);
614    results[0] = FAN_FROM_REG(data->fan_min[nr-1],
615                 DIV_FROM_REG(data->fan_div[nr-1]));
616    results[1] = FAN_FROM_REG(data->fan[nr-1],
617                 DIV_FROM_REG(data->fan_div[nr-1]));
618    *nrels_mag = 2;
619  } else if (operation == SENSORS_PROC_REAL_WRITE) {
620    if (*nrels_mag >= 1) {
621      data->fan_min[nr-1] = FAN_TO_REG(results[0],
622                            DIV_FROM_REG(data->fan_div[nr-1]));
623      sis5595_write_value(client,SIS5595_REG_FAN_MIN(nr),data->fan_min[nr-1]);
624    }
625  }
626}
627
628
629void sis5595_temp(struct i2c_client *client, int operation, int ctl_name,
630               int *nrels_mag, long *results)
631{
632  struct sis5595_data *data = client->data;
633  if (operation == SENSORS_PROC_REAL_INFO)
634    *nrels_mag = 1;
635  else if (operation == SENSORS_PROC_REAL_READ) {
636    sis5595_update_client(client);
637    results[0] = TEMP_FROM_REG(data->temp_over);
638    results[1] = TEMP_FROM_REG(data->temp_hyst);
639    results[2] = TEMP_FROM_REG(data->temp);
640    *nrels_mag = 3;
641  } else if (operation == SENSORS_PROC_REAL_WRITE) {
642    if (*nrels_mag >= 1) {
643      data->temp_over = TEMP_TO_REG(results[0]);
644      sis5595_write_value(client,SIS5595_REG_TEMP_OVER,data->temp_over);
645    }
646    if (*nrels_mag >= 2) {
647      data->temp_hyst = TEMP_TO_REG(results[1]);
648      sis5595_write_value(client,SIS5595_REG_TEMP_HYST,data->temp_hyst);
649    }
650  }
651}
652
653void sis5595_alarms(struct i2c_client *client, int operation, int ctl_name,
654                 int *nrels_mag, long *results)
655{
656  struct sis5595_data *data = client->data;
657  if (operation == SENSORS_PROC_REAL_INFO)
658    *nrels_mag = 0;
659  else if (operation == SENSORS_PROC_REAL_READ) {
660    sis5595_update_client(client);
661    results[0] = ALARMS_FROM_REG(data->alarms);
662    *nrels_mag = 1;
663  }
664}
665
666void sis5595_fan_div(struct i2c_client *client, int operation, int ctl_name,
667                  int *nrels_mag, long *results)
668{
669  struct sis5595_data *data = client->data;
670  int old;
671
672  if (operation == SENSORS_PROC_REAL_INFO)
673    *nrels_mag = 0;
674  else if (operation == SENSORS_PROC_REAL_READ) {
675    sis5595_update_client(client);
676    results[0] = DIV_FROM_REG(data->fan_div[0]);
677    results[1] = DIV_FROM_REG(data->fan_div[1]);
678    *nrels_mag = 2;
679  } else if (operation == SENSORS_PROC_REAL_WRITE) {
680    old = sis5595_read_value(client,SIS5595_REG_FANDIV);
681    if (*nrels_mag >= 2) {
682      data->fan_div[1] = DIV_TO_REG(results[1]);
683      old = (old & 0x3f) | (data->fan_div[1] << 6);
684    }
685    if (*nrels_mag >= 1) {
686      data->fan_div[0] = DIV_TO_REG(results[0]);
687      old = (old & 0xcf) | (data->fan_div[0] << 4);
688      sis5595_write_value(client,SIS5595_REG_FANDIV,old);
689    }
690  }
691}
692
693int __init sensors_sis5595_init(void)
694{
695  int res,addr;
696
697  printk("sis5595.o version %s (%s)\n",LM_VERSION,LM_DATE);
698  sis5595_initialized = 0;
699
700  if (sis5595_find_sis(&addr)) {
701    normal_isa[0] = SENSORS_ISA_END;
702    printk("sis5595.o: Warning: No SIS5595 southbridge found!\n");
703  } else
704    normal_isa[0] = addr;
705
706  if ((res =i2c_add_driver(&sis5595_driver))) {
707    printk("sis5595.o: Driver registration failed, module not inserted.\n");
708    sis5595_cleanup();
709    return res;
710  }
711  sis5595_initialized ++;
712  return 0;
713}
714
715int __init sis5595_cleanup(void)
716{
717  int res;
718
719  if (sis5595_initialized >= 1) {
720    if ((res = i2c_del_driver(&sis5595_driver))) {
721      printk("sis5595.o: Driver deregistration failed, module not removed.\n");
722      return res;
723    }
724    sis5595_initialized --;
725  }
726  return 0;
727}
728
729EXPORT_NO_SYMBOLS;
730
731#ifdef MODULE
732
733MODULE_AUTHOR("Kyösti Mälkki <kmalkki@cc.hut.fi>");
734MODULE_DESCRIPTION("SiS 5595 Sensor device");
735
736int init_module(void)
737{
738  return sensors_sis5595_init();
739}
740
741int cleanup_module(void)
742{
743  return sis5595_cleanup();
744}
745
746#endif /* MODULE */
747
Note: See TracBrowser for help on using the browser.