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

Revision 585, 24.5 KB (checked in by frodo, 14 years ago)

Preparations for better 10-bit address support

Updated for the changes in i2c_smbus_* functions

  • 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 semaphore *) (client->data));
482    down(& (((struct sis5595_data *) (client->data)) -> lock));
483    outb_p(reg,client->addr + SIS5595_ADDR_REG_OFFSET);
484    res = inb_p(client->addr + SIS5595_DATA_REG_OFFSET);
485    up( & (((struct sis5595_data *) (client->data)) -> lock));
486    return res;
487}
488
489/* The SMBus locks itself, but ISA access muse be locked explicitely!
490   There are some ugly typecasts here, but the good new is - they should
491   nowhere else be necessary! */
492int sis5595_write_value(struct i2c_client *client, u8 reg, u8 value)
493{
494    down(& (((struct sis5595_data *) (client->data)) -> lock));
495    outb_p(reg,client->addr + SIS5595_ADDR_REG_OFFSET);
496    outb_p(value,client->addr + SIS5595_DATA_REG_OFFSET);
497    up( & (((struct sis5595_data *) (client->data)) -> lock));
498    return 0;
499}
500
501/* Called when we have found a new SIS5595. It should set limits, etc. */
502void sis5595_init_client(struct i2c_client *client)
503{
504  /* Reset all except Watchdog values and last conversion values
505     This sets fan-divs to 2, among others */
506  sis5595_write_value(client,SIS5595_REG_CONFIG,0x80);
507
508  sis5595_write_value(client,SIS5595_REG_IN_MIN(0),IN_TO_REG(SIS5595_INIT_IN_MIN_0));
509  sis5595_write_value(client,SIS5595_REG_IN_MAX(0),IN_TO_REG(SIS5595_INIT_IN_MAX_0));
510  sis5595_write_value(client,SIS5595_REG_IN_MIN(1),IN_TO_REG(SIS5595_INIT_IN_MIN_1));
511  sis5595_write_value(client,SIS5595_REG_IN_MAX(1),IN_TO_REG(SIS5595_INIT_IN_MAX_1));
512  sis5595_write_value(client,SIS5595_REG_IN_MIN(2),IN_TO_REG(SIS5595_INIT_IN_MIN_2));
513  sis5595_write_value(client,SIS5595_REG_IN_MAX(2),IN_TO_REG(SIS5595_INIT_IN_MAX_2));
514  sis5595_write_value(client,SIS5595_REG_IN_MIN(3),IN_TO_REG(SIS5595_INIT_IN_MIN_3));
515  sis5595_write_value(client,SIS5595_REG_IN_MAX(3),IN_TO_REG(SIS5595_INIT_IN_MAX_3));
516  sis5595_write_value(client,SIS5595_REG_FAN_MIN(1),
517                   FAN_TO_REG(SIS5595_INIT_FAN_MIN_1,2));
518  sis5595_write_value(client,SIS5595_REG_FAN_MIN(2),
519                   FAN_TO_REG(SIS5595_INIT_FAN_MIN_2,2));
520  sis5595_write_value(client,SIS5595_REG_TEMP_OVER,TEMP_TO_REG(SIS5595_INIT_TEMP_OVER));
521  sis5595_write_value(client,SIS5595_REG_TEMP_HYST,TEMP_TO_REG(SIS5595_INIT_TEMP_HYST));
522
523  /* Start monitoring */
524  sis5595_write_value(client,SIS5595_REG_CONFIG,
525                   (sis5595_read_value(client,SIS5595_REG_CONFIG) & 0xf7) | 0x01);
526 
527}
528
529void sis5595_update_client(struct i2c_client *client)
530{
531  struct sis5595_data *data = client->data;
532  int i;
533
534  down(&data->update_lock);
535
536  if ((jiffies - data->last_updated > HZ+HZ/2 ) ||
537      (jiffies < data->last_updated) || ! data->valid) {
538
539#ifdef DEBUG
540    printk("Starting sis5595 update\n");
541#endif
542    for (i = 0; i <= 3; i++) {
543      data->in[i]     = sis5595_read_value(client,SIS5595_REG_IN(i));
544      data->in_min[i] = sis5595_read_value(client,SIS5595_REG_IN_MIN(i));
545      data->in_max[i] = sis5595_read_value(client,SIS5595_REG_IN_MAX(i));
546    }
547    for (i = 1; i <= 2; i++) {
548      data->fan[i-1] = sis5595_read_value(client,SIS5595_REG_FAN(i));
549      data->fan_min[i-1] = sis5595_read_value(client,SIS5595_REG_FAN_MIN(i));
550    }
551    data->temp = sis5595_read_value(client,SIS5595_REG_TEMP);
552    data->temp_over = sis5595_read_value(client,SIS5595_REG_TEMP_OVER);
553    data->temp_hyst = sis5595_read_value(client,SIS5595_REG_TEMP_HYST);
554    i = sis5595_read_value(client,SIS5595_REG_FANDIV);
555    data->fan_div[0] = (i >> 4) & 0x03;
556    data->fan_div[1] = i >> 6;
557    data->alarms = sis5595_read_value(client,SIS5595_REG_ALARM1);
558    data->last_updated = jiffies;
559    data->valid = 1;
560  }
561
562  up(&data->update_lock);
563}
564
565
566/* The next few functions are the call-back functions of the /proc/sys and
567   sysctl files. Which function is used is defined in the ctl_table in
568   the extra1 field.
569   Each function must return the magnitude (power of 10 to divide the date
570   with) if it is called with operation==SENSORS_PROC_REAL_INFO. It must
571   put a maximum of *nrels elements in results reflecting the data of this
572   file, and set *nrels to the number it actually put in it, if operation==
573   SENSORS_PROC_REAL_READ. Finally, it must get upto *nrels elements from
574   results and write them to the chip, if operations==SENSORS_PROC_REAL_WRITE.
575   Note that on SENSORS_PROC_REAL_READ, I do not check whether results is
576   large enough (by checking the incoming value of *nrels). This is not very
577   good practice, but as long as you put less than about 5 values in results,
578   you can assume it is large enough. */
579void sis5595_in(struct i2c_client *client, int operation, int ctl_name, 
580             int *nrels_mag, long *results)
581{
582  struct sis5595_data *data = client->data;
583  int nr = ctl_name - SIS5595_SYSCTL_IN0;
584
585  if (operation == SENSORS_PROC_REAL_INFO)
586    *nrels_mag = 2;
587  else if (operation == SENSORS_PROC_REAL_READ) {
588    sis5595_update_client(client);
589    results[0] = IN_FROM_REG(data->in_min[nr]);
590    results[1] = IN_FROM_REG(data->in_max[nr]);
591    results[2] = IN_FROM_REG(data->in[nr]);
592    *nrels_mag = 3;
593  } else if (operation == SENSORS_PROC_REAL_WRITE) {
594      if (*nrels_mag >= 1) {
595        data->in_min[nr] = IN_TO_REG(results[0]);
596        sis5595_write_value(client,SIS5595_REG_IN_MIN(nr),data->in_min[nr]);
597      }
598      if (*nrels_mag >= 2) {
599        data->in_max[nr] = IN_TO_REG(results[1]);
600        sis5595_write_value(client,SIS5595_REG_IN_MAX(nr),data->in_max[nr]);
601      }
602  }
603}
604
605void sis5595_fan(struct i2c_client *client, int operation, int ctl_name,
606              int *nrels_mag, long *results)
607{
608  struct sis5595_data *data = client->data;
609  int nr = ctl_name - SIS5595_SYSCTL_FAN1 + 1;
610
611  if (operation == SENSORS_PROC_REAL_INFO)
612    *nrels_mag = 0;
613  else if (operation == SENSORS_PROC_REAL_READ) {
614    sis5595_update_client(client);
615    results[0] = FAN_FROM_REG(data->fan_min[nr-1],
616                 DIV_FROM_REG(data->fan_div[nr-1]));
617    results[1] = FAN_FROM_REG(data->fan[nr-1],
618                 DIV_FROM_REG(data->fan_div[nr-1]));
619    *nrels_mag = 2;
620  } else if (operation == SENSORS_PROC_REAL_WRITE) {
621    if (*nrels_mag >= 1) {
622      data->fan_min[nr-1] = FAN_TO_REG(results[0],
623                            DIV_FROM_REG(data->fan_div[nr-1]));
624      sis5595_write_value(client,SIS5595_REG_FAN_MIN(nr),data->fan_min[nr-1]);
625    }
626  }
627}
628
629
630void sis5595_temp(struct i2c_client *client, int operation, int ctl_name,
631               int *nrels_mag, long *results)
632{
633  struct sis5595_data *data = client->data;
634  if (operation == SENSORS_PROC_REAL_INFO)
635    *nrels_mag = 1;
636  else if (operation == SENSORS_PROC_REAL_READ) {
637    sis5595_update_client(client);
638    results[0] = TEMP_FROM_REG(data->temp_over);
639    results[1] = TEMP_FROM_REG(data->temp_hyst);
640    results[2] = TEMP_FROM_REG(data->temp);
641    *nrels_mag = 3;
642  } else if (operation == SENSORS_PROC_REAL_WRITE) {
643    if (*nrels_mag >= 1) {
644      data->temp_over = TEMP_TO_REG(results[0]);
645      sis5595_write_value(client,SIS5595_REG_TEMP_OVER,data->temp_over);
646    }
647    if (*nrels_mag >= 2) {
648      data->temp_hyst = TEMP_TO_REG(results[1]);
649      sis5595_write_value(client,SIS5595_REG_TEMP_HYST,data->temp_hyst);
650    }
651  }
652}
653
654void sis5595_alarms(struct i2c_client *client, int operation, int ctl_name,
655                 int *nrels_mag, long *results)
656{
657  struct sis5595_data *data = client->data;
658  if (operation == SENSORS_PROC_REAL_INFO)
659    *nrels_mag = 0;
660  else if (operation == SENSORS_PROC_REAL_READ) {
661    sis5595_update_client(client);
662    results[0] = ALARMS_FROM_REG(data->alarms);
663    *nrels_mag = 1;
664  }
665}
666
667void sis5595_fan_div(struct i2c_client *client, int operation, int ctl_name,
668                  int *nrels_mag, long *results)
669{
670  struct sis5595_data *data = client->data;
671  int old;
672
673  if (operation == SENSORS_PROC_REAL_INFO)
674    *nrels_mag = 0;
675  else if (operation == SENSORS_PROC_REAL_READ) {
676    sis5595_update_client(client);
677    results[0] = DIV_FROM_REG(data->fan_div[0]);
678    results[1] = DIV_FROM_REG(data->fan_div[1]);
679    *nrels_mag = 2;
680  } else if (operation == SENSORS_PROC_REAL_WRITE) {
681    old = sis5595_read_value(client,SIS5595_REG_FANDIV);
682    if (*nrels_mag >= 2) {
683      data->fan_div[1] = DIV_TO_REG(results[1]);
684      old = (old & 0x3f) | (data->fan_div[1] << 6);
685    }
686    if (*nrels_mag >= 1) {
687      data->fan_div[0] = DIV_TO_REG(results[0]);
688      old = (old & 0xcf) | (data->fan_div[0] << 4);
689      sis5595_write_value(client,SIS5595_REG_FANDIV,old);
690    }
691  }
692}
693
694int __init sensors_sis5595_init(void)
695{
696  int res,addr;
697
698  printk("sis5595.o version %s (%s)\n",LM_VERSION,LM_DATE);
699  sis5595_initialized = 0;
700
701  if (sis5595_find_sis(&addr)) {
702    normal_isa[0] = SENSORS_ISA_END;
703    printk("sis5595.o: Warning: No SIS5595 southbridge found!\n");
704  } else
705    normal_isa[0] = addr;
706
707  if ((res =i2c_add_driver(&sis5595_driver))) {
708    printk("sis5595.o: Driver registration failed, module not inserted.\n");
709    sis5595_cleanup();
710    return res;
711  }
712  sis5595_initialized ++;
713  return 0;
714}
715
716int __init sis5595_cleanup(void)
717{
718  int res;
719
720  if (sis5595_initialized >= 1) {
721    if ((res = i2c_del_driver(&sis5595_driver))) {
722      printk("sis5595.o: Driver deregistration failed, module not removed.\n");
723      return res;
724    }
725    sis5595_initialized --;
726  }
727  return 0;
728}
729
730EXPORT_NO_SYMBOLS;
731
732#ifdef MODULE
733
734MODULE_AUTHOR("Kyösti Mälkki <kmalkki@cc.hut.fi>");
735MODULE_DESCRIPTION("SiS 5595 Sensor device");
736
737int init_module(void)
738{
739  return sensors_sis5595_init();
740}
741
742int cleanup_module(void)
743{
744  return sis5595_cleanup();
745}
746
747#endif /* MODULE */
748
Note: See TracBrowser for help on using the browser.