2     abituguru.c Copyright (c) 2005-2006 Hans de Goede <j.w.r.degoede@hhs.nl>
 
   4     This program is free software; you can redistribute it and/or modify
 
   5     it under the terms of the GNU General Public License as published by
 
   6     the Free Software Foundation; either version 2 of the License, or
 
   7     (at your option) any later version.
 
   9     This program is distributed in the hope that it will be useful,
 
  10     but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  12     GNU General Public License for more details.
 
  14     You should have received a copy of the GNU General Public License
 
  15     along with this program; if not, write to the Free Software
 
  16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  19     This driver supports the sensor part of the custom Abit uGuru chip found
 
  20     on Abit uGuru motherboards. Note: because of lack of specs the CPU / RAM /
 
  21     etc voltage & frequency control is not supported!
 
  23 #include <linux/module.h>
 
  24 #include <linux/init.h>
 
  25 #include <linux/slab.h>
 
  26 #include <linux/jiffies.h>
 
  27 #include <linux/mutex.h>
 
  28 #include <linux/err.h>
 
  29 #include <linux/platform_device.h>
 
  30 #include <linux/hwmon.h>
 
  31 #include <linux/hwmon-sysfs.h>
 
  35 #define ABIT_UGURU_ALARM_BANK                   0x20 /* 1x 3 bytes */
 
  36 #define ABIT_UGURU_SENSOR_BANK1                 0x21 /* 16x volt and temp */
 
  37 #define ABIT_UGURU_FAN_PWM                      0x24 /* 3x 5 bytes */
 
  38 #define ABIT_UGURU_SENSOR_BANK2                 0x26 /* fans */
 
  39 /* max nr of sensors in bank1, a bank1 sensor can be in, temp or nc */
 
  40 #define ABIT_UGURU_MAX_BANK1_SENSORS            16
 
  41 /* Warning if you increase one of the 2 MAX defines below to 10 or higher you
 
  42    should adjust the belonging _NAMES_LENGTH macro for the 2 digit number! */
 
  43 /* max nr of sensors in bank2, currently mb's with max 6 fans are known */
 
  44 #define ABIT_UGURU_MAX_BANK2_SENSORS            6
 
  45 /* max nr of pwm outputs, currently mb's with max 5 pwm outputs are known */
 
  46 #define ABIT_UGURU_MAX_PWMS                     5
 
  47 /* uGuru sensor bank 1 flags */                      /* Alarm if: */
 
  48 #define ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE       0x01 /*  temp over warn */
 
  49 #define ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE       0x02 /*  volt over max */
 
  50 #define ABIT_UGURU_VOLT_LOW_ALARM_ENABLE        0x04 /*  volt under min */
 
  51 #define ABIT_UGURU_TEMP_HIGH_ALARM_FLAG         0x10 /* temp is over warn */
 
  52 #define ABIT_UGURU_VOLT_HIGH_ALARM_FLAG         0x20 /* volt is over max */
 
  53 #define ABIT_UGURU_VOLT_LOW_ALARM_FLAG          0x40 /* volt is under min */
 
  54 /* uGuru sensor bank 2 flags */                      /* Alarm if: */
 
  55 #define ABIT_UGURU_FAN_LOW_ALARM_ENABLE         0x01 /*   fan under min */
 
  56 /* uGuru sensor bank common flags */
 
  57 #define ABIT_UGURU_BEEP_ENABLE                  0x08 /* beep if alarm */
 
  58 #define ABIT_UGURU_SHUTDOWN_ENABLE              0x80 /* shutdown if alarm */
 
  59 /* uGuru fan PWM (speed control) flags */
 
  60 #define ABIT_UGURU_FAN_PWM_ENABLE               0x80 /* enable speed control */
 
  61 /* Values used for conversion */
 
  62 #define ABIT_UGURU_FAN_MAX                      15300 /* RPM */
 
  63 /* Bank1 sensor types */
 
  64 #define ABIT_UGURU_IN_SENSOR                    0
 
  65 #define ABIT_UGURU_TEMP_SENSOR                  1
 
  66 #define ABIT_UGURU_NC                           2
 
  67 /* Timeouts / Retries, if these turn out to need a lot of fiddling we could
 
  68    convert them to params. */
 
  69 /* 250 was determined by trial and error, 200 works most of the time, but not
 
  70    always. I assume this is cpu-speed independent, since the ISA-bus and not
 
  71    the CPU should be the bottleneck. Note that 250 sometimes is still not
 
  72    enough (only reported on AN7 mb) this is handled by a higher layer. */
 
  73 #define ABIT_UGURU_WAIT_TIMEOUT                 250
 
  74 /* Normally all expected status in abituguru_ready, are reported after the
 
  75    first read, but sometimes not and we need to poll, 5 polls was not enough
 
  77 #define ABIT_UGURU_READY_TIMEOUT                50
 
  78 /* Maximum 3 retries on timedout reads/writes, delay 200 ms before retrying */
 
  79 #define ABIT_UGURU_MAX_RETRIES                  3
 
  80 #define ABIT_UGURU_RETRY_DELAY                  (HZ/5)
 
  81 /* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is an error */
 
  82 #define ABIT_UGURU_MAX_TIMEOUTS                 2
 
  84 #define ABIT_UGURU_NAME                         "abituguru"
 
  85 #define ABIT_UGURU_DEBUG(level, format, arg...)                         \
 
  86         if (level <= verbose)                                           \
 
  87                 printk(KERN_DEBUG ABIT_UGURU_NAME ": "  format , ## arg)
 
  88 /* Macros to help calculate the sysfs_names array length */
 
  89 /* sum of strlen of: in??_input\0, in??_{min,max}\0, in??_{min,max}_alarm\0,
 
  90    in??_{min,max}_alarm_enable\0, in??_beep\0, in??_shutdown\0 */
 
  91 #define ABITUGURU_IN_NAMES_LENGTH       (11 + 2 * 9 + 2 * 15 + 2 * 22 + 10 + 14)
 
  92 /* sum of strlen of: temp??_input\0, temp??_max\0, temp??_crit\0,
 
  93    temp??_alarm\0, temp??_alarm_enable\0, temp??_beep\0, temp??_shutdown\0 */
 
  94 #define ABITUGURU_TEMP_NAMES_LENGTH     (13 + 11 + 12 + 13 + 20 + 12 + 16)
 
  95 /* sum of strlen of: fan?_input\0, fan?_min\0, fan?_alarm\0,
 
  96    fan?_alarm_enable\0, fan?_beep\0, fan?_shutdown\0 */
 
  97 #define ABITUGURU_FAN_NAMES_LENGTH      (11 + 9 + 11 + 18 + 10 + 14)
 
  98 /* sum of strlen of: pwm?_enable\0, pwm?_auto_channels_temp\0,
 
  99    pwm?_auto_point{1,2}_pwm\0, pwm?_auto_point{1,2}_temp\0 */
 
 100 #define ABITUGURU_PWM_NAMES_LENGTH      (12 + 24 + 2 * 21 + 2 * 22)
 
 101 /* IN_NAMES_LENGTH > TEMP_NAMES_LENGTH so assume all bank1 sensors are in */
 
 102 #define ABITUGURU_SYSFS_NAMES_LENGTH    ( \
 
 103         ABIT_UGURU_MAX_BANK1_SENSORS * ABITUGURU_IN_NAMES_LENGTH + \
 
 104         ABIT_UGURU_MAX_BANK2_SENSORS * ABITUGURU_FAN_NAMES_LENGTH + \
 
 105         ABIT_UGURU_MAX_PWMS * ABITUGURU_PWM_NAMES_LENGTH)
 
 107 /* All the macros below are named identical to the oguru and oguru2 programs
 
 108    reverse engineered by Olle Sandberg, hence the names might not be 100%
 
 109    logical. I could come up with better names, but I prefer keeping the names
 
 110    identical so that this driver can be compared with his work more easily. */
 
 111 /* Two i/o-ports are used by uGuru */
 
 112 #define ABIT_UGURU_BASE                         0x00E0
 
 113 /* Used to tell uGuru what to read and to read the actual data */
 
 114 #define ABIT_UGURU_CMD                          0x00
 
 115 /* Mostly used to check if uGuru is busy */
 
 116 #define ABIT_UGURU_DATA                         0x04
 
 117 #define ABIT_UGURU_REGION_LENGTH                5
 
 119 #define ABIT_UGURU_STATUS_WRITE                 0x00 /* Ready to be written */
 
 120 #define ABIT_UGURU_STATUS_READ                  0x01 /* Ready to be read */
 
 121 #define ABIT_UGURU_STATUS_INPUT                 0x08 /* More input */
 
 122 #define ABIT_UGURU_STATUS_READY                 0x09 /* Ready to be written */
 
 125 /* in (Volt) sensors go up to 3494 mV, temp to 255000 millidegrees Celsius */
 
 126 static const int abituguru_bank1_max_value[2] = { 3494, 255000 };
 
 127 /* Min / Max allowed values for sensor2 (fan) alarm threshold, these values
 
 128    correspond to 300-3000 RPM */
 
 129 static const u8 abituguru_bank2_min_threshold = 5;
 
 130 static const u8 abituguru_bank2_max_threshold = 50;
 
 131 /* Register 0 is a bitfield, 1 and 2 are pwm settings (255 = 100%), 3 and 4
 
 132    are temperature trip points. */
 
 133 static const int abituguru_pwm_settings_multiplier[5] = { 0, 1, 1, 1000, 1000 };
 
 134 /* Min / Max allowed values for pwm_settings. Note: pwm1 (CPU fan) is a
 
 135    special case the minium allowed pwm% setting for this is 30% (77) on
 
 136    some MB's this special case is handled in the code! */
 
 137 static const u8 abituguru_pwm_min[5] = { 0, 170, 170, 25, 25 };
 
 138 static const u8 abituguru_pwm_max[5] = { 0, 255, 255, 75, 75 };
 
 141 /* Insmod parameters */
 
 143 module_param(force, bool, 0);
 
 144 MODULE_PARM_DESC(force, "Set to one to force detection.");
 
 145 static int fan_sensors;
 
 146 module_param(fan_sensors, int, 0);
 
 147 MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru "
 
 150 module_param(pwms, int, 0);
 
 151 MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru "
 
 154 /* Default verbose is 2, since this driver is still in the testing phase */
 
 155 static int verbose = 2;
 
 156 module_param(verbose, int, 0644);
 
 157 MODULE_PARM_DESC(verbose, "How verbose should the driver be? (0-3):\n"
 
 159         "   1 + verbose error reporting\n"
 
 160         "   2 + sensors type probing info\n"
 
 161         "   3 + retryable error reporting");
 
 164 /* For the Abit uGuru, we need to keep some data in memory.
 
 165    The structure is dynamically allocated, at the same time when a new
 
 166    abituguru device is allocated. */
 
 167 struct abituguru_data {
 
 168         struct class_device *class_dev; /* hwmon registered device */
 
 169         struct mutex update_lock;       /* protect access to data and uGuru */
 
 170         unsigned long last_updated;     /* In jiffies */
 
 171         unsigned short addr;            /* uguru base address */
 
 172         char uguru_ready;               /* is the uguru in ready state? */
 
 173         unsigned char update_timeouts;  /* number of update timeouts since last
 
 176         /* The sysfs attr and their names are generated automatically, for bank1
 
 177            we cannot use a predefined array because we don't know beforehand
 
 178            of a sensor is a volt or a temp sensor, for bank2 and the pwms its
 
 179            easier todo things the same way.  For in sensors we have 9 (temp 7)
 
 180            sysfs entries per sensor, for bank2 and pwms 6. */
 
 181         struct sensor_device_attribute_2 sysfs_attr[
 
 182                 ABIT_UGURU_MAX_BANK1_SENSORS * 9 +
 
 183                 ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6];
 
 184         /* Buffer to store the dynamically generated sysfs names */
 
 185         char sysfs_names[ABITUGURU_SYSFS_NAMES_LENGTH];
 
 188         /* number of and addresses of [0] in, [1] temp sensors */
 
 190         u8 bank1_address[2][ABIT_UGURU_MAX_BANK1_SENSORS];
 
 191         u8 bank1_value[ABIT_UGURU_MAX_BANK1_SENSORS];
 
 192         /* This array holds 3 entries per sensor for the bank 1 sensor settings
 
 193            (flags, min, max for voltage / flags, warn, shutdown for temp). */
 
 194         u8 bank1_settings[ABIT_UGURU_MAX_BANK1_SENSORS][3];
 
 195         /* Maximum value for each sensor used for scaling in mV/millidegrees
 
 197         int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS];
 
 199         /* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */
 
 200         u8 bank2_sensors; /* actual number of bank2 sensors found */
 
 201         u8 bank2_value[ABIT_UGURU_MAX_BANK2_SENSORS];
 
 202         u8 bank2_settings[ABIT_UGURU_MAX_BANK2_SENSORS][2]; /* flags, min */
 
 204         /* Alarms 2 bytes for bank1, 1 byte for bank2 */
 
 207         /* Fan PWM (speed control) 5 bytes per PWM */
 
 208         u8 pwms; /* actual number of pwms found */
 
 209         u8 pwm_settings[ABIT_UGURU_MAX_PWMS][5];
 
 212 /* wait till the uguru is in the specified state */
 
 213 static int abituguru_wait(struct abituguru_data *data, u8 state)
 
 215         int timeout = ABIT_UGURU_WAIT_TIMEOUT;
 
 217         while (inb_p(data->addr + ABIT_UGURU_DATA) != state) {
 
 225 /* Put the uguru in ready for input state */
 
 226 static int abituguru_ready(struct abituguru_data *data)
 
 228         int timeout = ABIT_UGURU_READY_TIMEOUT;
 
 230         if (data->uguru_ready)
 
 233         /* Reset? / Prepare for next read/write cycle */
 
 234         outb(0x00, data->addr + ABIT_UGURU_DATA);
 
 236         /* Wait till the uguru is ready */
 
 237         if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) {
 
 239                         "timeout exceeded waiting for ready state\n");
 
 243         /* Cmd port MUST be read now and should contain 0xAC */
 
 244         while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
 
 248                            "CMD reg does not hold 0xAC after ready command\n");
 
 253         /* After this the ABIT_UGURU_DATA port should contain
 
 254            ABIT_UGURU_STATUS_INPUT */
 
 255         timeout = ABIT_UGURU_READY_TIMEOUT;
 
 256         while (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT) {
 
 260                                 "state != more input after ready command\n");
 
 265         data->uguru_ready = 1;
 
 269 /* Send the bank and then sensor address to the uGuru for the next read/write
 
 270    cycle. This function gets called as the first part of a read/write by
 
 271    abituguru_read and abituguru_write. This function should never be
 
 272    called by any other function. */
 
 273 static int abituguru_send_address(struct abituguru_data *data,
 
 274         u8 bank_addr, u8 sensor_addr, int retries)
 
 276         /* assume the caller does error handling itself if it has not requested
 
 277            any retries, and thus be quiet. */
 
 278         int report_errors = retries;
 
 281                 /* Make sure the uguru is ready and then send the bank address,
 
 282                    after this the uguru is no longer "ready". */
 
 283                 if (abituguru_ready(data) != 0)
 
 285                 outb(bank_addr, data->addr + ABIT_UGURU_DATA);
 
 286                 data->uguru_ready = 0;
 
 288                 /* Wait till the uguru is ABIT_UGURU_STATUS_INPUT state again
 
 289                    and send the sensor addr */
 
 290                 if (abituguru_wait(data, ABIT_UGURU_STATUS_INPUT)) {
 
 292                                 ABIT_UGURU_DEBUG(3, "timeout exceeded "
 
 293                                         "waiting for more input state, %d "
 
 294                                         "tries remaining\n", retries);
 
 295                                 set_current_state(TASK_UNINTERRUPTIBLE);
 
 296                                 schedule_timeout(ABIT_UGURU_RETRY_DELAY);
 
 301                                 ABIT_UGURU_DEBUG(1, "timeout exceeded "
 
 302                                         "waiting for more input state "
 
 303                                         "(bank: %d)\n", (int)bank_addr);
 
 306                 outb(sensor_addr, data->addr + ABIT_UGURU_CMD);
 
 311 /* Read count bytes from sensor sensor_addr in bank bank_addr and store the
 
 312    result in buf, retry the send address part of the read retries times. */
 
 313 static int abituguru_read(struct abituguru_data *data,
 
 314         u8 bank_addr, u8 sensor_addr, u8 *buf, int count, int retries)
 
 318         /* Send the address */
 
 319         i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
 
 323         /* And read the data */
 
 324         for (i = 0; i < count; i++) {
 
 325                 if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
 
 326                         ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
 
 327                                 "read state (bank: %d, sensor: %d)\n",
 
 328                                 (int)bank_addr, (int)sensor_addr);
 
 331                 buf[i] = inb(data->addr + ABIT_UGURU_CMD);
 
 334         /* Last put the chip back in ready state */
 
 335         abituguru_ready(data);
 
 340 /* Write count bytes from buf to sensor sensor_addr in bank bank_addr, the send
 
 341    address part of the write is always retried ABIT_UGURU_MAX_RETRIES times. */
 
 342 static int abituguru_write(struct abituguru_data *data,
 
 343         u8 bank_addr, u8 sensor_addr, u8 *buf, int count)
 
 347         /* Send the address */
 
 348         i = abituguru_send_address(data, bank_addr, sensor_addr,
 
 349                 ABIT_UGURU_MAX_RETRIES);
 
 353         /* And write the data */
 
 354         for (i = 0; i < count; i++) {
 
 355                 if (abituguru_wait(data, ABIT_UGURU_STATUS_WRITE)) {
 
 356                         ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
 
 357                                 "write state (bank: %d, sensor: %d)\n",
 
 358                                 (int)bank_addr, (int)sensor_addr);
 
 361                 outb(buf[i], data->addr + ABIT_UGURU_CMD);
 
 364         /* Now we need to wait till the chip is ready to be read again,
 
 366         if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
 
 367                 ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for read state "
 
 368                         "after write (bank: %d, sensor: %d)\n", (int)bank_addr,
 
 373         /* Cmd port MUST be read now and should contain 0xAC */
 
 374         if (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
 
 375                 ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after write "
 
 376                         "(bank: %d, sensor: %d)\n", (int)bank_addr,
 
 381         /* Last put the chip back in ready state */
 
 382         abituguru_ready(data);
 
 387 /* Detect sensor type. Temp and Volt sensors are enabled with
 
 388    different masks and will ignore enable masks not meant for them.
 
 389    This enables us to test what kind of sensor we're dealing with.
 
 390    By setting the alarm thresholds so that we will always get an
 
 391    alarm for sensor type X and then enabling the sensor as sensor type
 
 392    X, if we then get an alarm it is a sensor of type X. */
 
 394 abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
 
 398         int ret = ABIT_UGURU_NC;
 
 400         /* First read the sensor and the current settings */
 
 401         if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, sensor_addr, &val,
 
 402                         1, ABIT_UGURU_MAX_RETRIES) != 1)
 
 405         /* Test val is sane / usable for sensor type detection. */
 
 406         if ((val < 10u) || (val > 240u)) {
 
 407                 printk(KERN_WARNING ABIT_UGURU_NAME
 
 408                         ": bank1-sensor: %d reading (%d) too close to limits, "
 
 409                         "unable to determine sensor type, skipping sensor\n",
 
 410                         (int)sensor_addr, (int)val);
 
 411                 /* assume no sensor is there for sensors for which we can't
 
 412                    determine the sensor type because their reading is too close
 
 413                    to their limits, this usually means no sensor is there. */
 
 414                 return ABIT_UGURU_NC;
 
 417         ABIT_UGURU_DEBUG(2, "testing bank1 sensor %d\n", (int)sensor_addr);
 
 418         /* Volt sensor test, enable volt low alarm, set min value ridicously
 
 419            high. If its a volt sensor this should always give us an alarm. */
 
 420         buf[0] = ABIT_UGURU_VOLT_LOW_ALARM_ENABLE;
 
 423         if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
 
 426         /* Now we need 20 ms to give the uguru time to read the sensors
 
 427            and raise a voltage alarm */
 
 428         set_current_state(TASK_UNINTERRUPTIBLE);
 
 429         schedule_timeout(HZ/50);
 
 430         /* Check for alarm and check the alarm is a volt low alarm. */
 
 431         if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
 
 432                         ABIT_UGURU_MAX_RETRIES) != 3)
 
 434         if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
 
 435                 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
 
 437                                 ABIT_UGURU_MAX_RETRIES) != 3)
 
 439                 if (buf[0] & ABIT_UGURU_VOLT_LOW_ALARM_FLAG) {
 
 440                         /* Restore original settings */
 
 441                         if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
 
 443                                         data->bank1_settings[sensor_addr],
 
 446                         ABIT_UGURU_DEBUG(2, "  found volt sensor\n");
 
 447                         return ABIT_UGURU_IN_SENSOR;
 
 449                         ABIT_UGURU_DEBUG(2, "  alarm raised during volt "
 
 450                                 "sensor test, but volt low flag not set\n");
 
 452                 ABIT_UGURU_DEBUG(2, "  alarm not raised during volt sensor "
 
 455         /* Temp sensor test, enable sensor as a temp sensor, set beep value
 
 456            ridicously low (but not too low, otherwise uguru ignores it).
 
 457            If its a temp sensor this should always give us an alarm. */
 
 458         buf[0] = ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE;
 
 461         if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
 
 464         /* Now we need 50 ms to give the uguru time to read the sensors
 
 465            and raise a temp alarm */
 
 466         set_current_state(TASK_UNINTERRUPTIBLE);
 
 467         schedule_timeout(HZ/20);
 
 468         /* Check for alarm and check the alarm is a temp high alarm. */
 
 469         if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
 
 470                         ABIT_UGURU_MAX_RETRIES) != 3)
 
 472         if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
 
 473                 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
 
 475                                 ABIT_UGURU_MAX_RETRIES) != 3)
 
 477                 if (buf[0] & ABIT_UGURU_TEMP_HIGH_ALARM_FLAG) {
 
 478                         ret = ABIT_UGURU_TEMP_SENSOR;
 
 479                         ABIT_UGURU_DEBUG(2, "  found temp sensor\n");
 
 481                         ABIT_UGURU_DEBUG(2, "  alarm raised during temp "
 
 482                                 "sensor test, but temp high flag not set\n");
 
 484                 ABIT_UGURU_DEBUG(2, "  alarm not raised during temp sensor "
 
 487         /* Restore original settings */
 
 488         if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
 
 489                         data->bank1_settings[sensor_addr], 3) != 3)
 
 495 /* These functions try to find out how many sensors there are in bank2 and how
 
 496    many pwms there are. The purpose of this is to make sure that we don't give
 
 497    the user the possibility to change settings for non-existent sensors / pwm.
 
 498    The uGuru will happily read / write whatever memory happens to be after the
 
 499    memory storing the PWM settings when reading/writing to a PWM which is not
 
 500    there. Notice even if we detect a PWM which doesn't exist we normally won't
 
 501    write to it, unless the user tries to change the settings.
 
 503    Although the uGuru allows reading (settings) from non existing bank2
 
 504    sensors, my version of the uGuru does seem to stop writing to them, the
 
 505    write function above aborts in this case with:
 
 506    "CMD reg does not hold 0xAC after write"
 
 508    Notice these 2 tests are non destructive iow read-only tests, otherwise
 
 509    they would defeat their purpose. Although for the bank2_sensors detection a
 
 510    read/write test would be feasible because of the reaction above, I've
 
 511    however opted to stay on the safe side. */
 
 512 static void __devinit
 
 513 abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
 
 518                 data->bank2_sensors = fan_sensors;
 
 519                 ABIT_UGURU_DEBUG(2, "assuming %d fan sensors because of "
 
 520                         "\"fan_sensors\" module param\n",
 
 521                         (int)data->bank2_sensors);
 
 525         ABIT_UGURU_DEBUG(2, "detecting number of fan sensors\n");
 
 526         for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
 
 527                 /* 0x89 are the known used bits:
 
 528                    -0x80 enable shutdown
 
 531                    All other bits should be 0, but on some motherboards
 
 532                    0x40 (bit 6) is also high for some of the fans?? */
 
 533                 if (data->bank2_settings[i][0] & ~0xC9) {
 
 534                         ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
 
 535                                 "to be a fan sensor: settings[0] = %02X\n",
 
 536                                 i, (unsigned int)data->bank2_settings[i][0]);
 
 540                 /* check if the threshold is within the allowed range */
 
 541                 if (data->bank2_settings[i][1] <
 
 542                                 abituguru_bank2_min_threshold) {
 
 543                         ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
 
 544                                 "to be a fan sensor: the threshold (%d) is "
 
 545                                 "below the minimum (%d)\n", i,
 
 546                                 (int)data->bank2_settings[i][1],
 
 547                                 (int)abituguru_bank2_min_threshold);
 
 550                 if (data->bank2_settings[i][1] >
 
 551                                 abituguru_bank2_max_threshold) {
 
 552                         ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
 
 553                                 "to be a fan sensor: the threshold (%d) is "
 
 554                                 "above the maximum (%d)\n", i,
 
 555                                 (int)data->bank2_settings[i][1],
 
 556                                 (int)abituguru_bank2_max_threshold);
 
 561         data->bank2_sensors = i;
 
 562         ABIT_UGURU_DEBUG(2, " found: %d fan sensors\n",
 
 563                 (int)data->bank2_sensors);
 
 566 static void __devinit
 
 567 abituguru_detect_no_pwms(struct abituguru_data *data)
 
 573                 ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
 
 574                         "\"pwms\" module param\n", (int)data->pwms);
 
 578         ABIT_UGURU_DEBUG(2, "detecting number of PWM outputs\n");
 
 579         for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
 
 580                 /* 0x80 is the enable bit and the low
 
 581                    nibble is which temp sensor to use,
 
 582                    the other bits should be 0 */
 
 583                 if (data->pwm_settings[i][0] & ~0x8F) {
 
 584                         ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
 
 585                                 "to be a pwm channel: settings[0] = %02X\n",
 
 586                                 i, (unsigned int)data->pwm_settings[i][0]);
 
 590                 /* the low nibble must correspond to one of the temp sensors
 
 592                 for (j = 0; j < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR];
 
 594                         if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][j] ==
 
 595                                         (data->pwm_settings[i][0] & 0x0F))
 
 598                 if (j == data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
 
 599                         ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
 
 600                                 "to be a pwm channel: %d is not a valid temp "
 
 601                                 "sensor address\n", i,
 
 602                                 data->pwm_settings[i][0] & 0x0F);
 
 606                 /* check if all other settings are within the allowed range */
 
 607                 for (j = 1; j < 5; j++) {
 
 609                         /* special case pwm1 min pwm% */
 
 610                         if ((i == 0) && ((j == 1) || (j == 2)))
 
 613                                 min = abituguru_pwm_min[j];
 
 614                         if (data->pwm_settings[i][j] < min) {
 
 615                                 ABIT_UGURU_DEBUG(2, "  pwm channel %d does "
 
 616                                         "not seem to be a pwm channel: "
 
 617                                         "setting %d (%d) is below the minimum "
 
 618                                         "value (%d)\n", i, j,
 
 619                                         (int)data->pwm_settings[i][j],
 
 621                                 goto abituguru_detect_no_pwms_exit;
 
 623                         if (data->pwm_settings[i][j] > abituguru_pwm_max[j]) {
 
 624                                 ABIT_UGURU_DEBUG(2, "  pwm channel %d does "
 
 625                                         "not seem to be a pwm channel: "
 
 626                                         "setting %d (%d) is above the maximum "
 
 627                                         "value (%d)\n", i, j,
 
 628                                         (int)data->pwm_settings[i][j],
 
 629                                         (int)abituguru_pwm_max[j]);
 
 630                                 goto abituguru_detect_no_pwms_exit;
 
 634                 /* check that min temp < max temp and min pwm < max pwm */
 
 635                 if (data->pwm_settings[i][1] >= data->pwm_settings[i][2]) {
 
 636                         ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
 
 637                                 "to be a pwm channel: min pwm (%d) >= "
 
 639                                 (int)data->pwm_settings[i][1],
 
 640                                 (int)data->pwm_settings[i][2]);
 
 643                 if (data->pwm_settings[i][3] >= data->pwm_settings[i][4]) {
 
 644                         ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
 
 645                                 "to be a pwm channel: min temp (%d) >= "
 
 646                                 "max temp (%d)\n", i,
 
 647                                 (int)data->pwm_settings[i][3],
 
 648                                 (int)data->pwm_settings[i][4]);
 
 653 abituguru_detect_no_pwms_exit:
 
 655         ABIT_UGURU_DEBUG(2, " found: %d PWM outputs\n", (int)data->pwms);
 
 658 /* Following are the sysfs callback functions. These functions expect:
 
 659    sensor_device_attribute_2->index:   sensor address/offset in the bank
 
 660    sensor_device_attribute_2->nr:      register offset, bitmask or NA. */
 
 661 static struct abituguru_data *abituguru_update_device(struct device *dev);
 
 663 static ssize_t show_bank1_value(struct device *dev,
 
 664         struct device_attribute *devattr, char *buf)
 
 666         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 667         struct abituguru_data *data = abituguru_update_device(dev);
 
 670         return sprintf(buf, "%d\n", (data->bank1_value[attr->index] *
 
 671                 data->bank1_max_value[attr->index] + 128) / 255);
 
 674 static ssize_t show_bank1_setting(struct device *dev,
 
 675         struct device_attribute *devattr, char *buf)
 
 677         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 678         struct abituguru_data *data = dev_get_drvdata(dev);
 
 679         return sprintf(buf, "%d\n",
 
 680                 (data->bank1_settings[attr->index][attr->nr] *
 
 681                 data->bank1_max_value[attr->index] + 128) / 255);
 
 684 static ssize_t show_bank2_value(struct device *dev,
 
 685         struct device_attribute *devattr, char *buf)
 
 687         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 688         struct abituguru_data *data = abituguru_update_device(dev);
 
 691         return sprintf(buf, "%d\n", (data->bank2_value[attr->index] *
 
 692                 ABIT_UGURU_FAN_MAX + 128) / 255);
 
 695 static ssize_t show_bank2_setting(struct device *dev,
 
 696         struct device_attribute *devattr, char *buf)
 
 698         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 699         struct abituguru_data *data = dev_get_drvdata(dev);
 
 700         return sprintf(buf, "%d\n",
 
 701                 (data->bank2_settings[attr->index][attr->nr] *
 
 702                 ABIT_UGURU_FAN_MAX + 128) / 255);
 
 705 static ssize_t store_bank1_setting(struct device *dev, struct device_attribute
 
 706         *devattr, const char *buf, size_t count)
 
 708         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 709         struct abituguru_data *data = dev_get_drvdata(dev);
 
 710         u8 val = (simple_strtoul(buf, NULL, 10) * 255 +
 
 711                 data->bank1_max_value[attr->index]/2) /
 
 712                 data->bank1_max_value[attr->index];
 
 715         mutex_lock(&data->update_lock);
 
 716         if (data->bank1_settings[attr->index][attr->nr] != val) {
 
 717                 u8 orig_val = data->bank1_settings[attr->index][attr->nr];
 
 718                 data->bank1_settings[attr->index][attr->nr] = val;
 
 719                 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
 
 720                                 attr->index, data->bank1_settings[attr->index],
 
 722                         data->bank1_settings[attr->index][attr->nr] = orig_val;
 
 726         mutex_unlock(&data->update_lock);
 
 730 static ssize_t store_bank2_setting(struct device *dev, struct device_attribute
 
 731         *devattr, const char *buf, size_t count)
 
 733         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 734         struct abituguru_data *data = dev_get_drvdata(dev);
 
 735         u8 val = (simple_strtoul(buf, NULL, 10)*255 + ABIT_UGURU_FAN_MAX/2) /
 
 739         /* this check can be done before taking the lock */
 
 740         if ((val < abituguru_bank2_min_threshold) ||
 
 741                         (val > abituguru_bank2_max_threshold))
 
 744         mutex_lock(&data->update_lock);
 
 745         if (data->bank2_settings[attr->index][attr->nr] != val) {
 
 746                 u8 orig_val = data->bank2_settings[attr->index][attr->nr];
 
 747                 data->bank2_settings[attr->index][attr->nr] = val;
 
 748                 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK2 + 2,
 
 749                                 attr->index, data->bank2_settings[attr->index],
 
 751                         data->bank2_settings[attr->index][attr->nr] = orig_val;
 
 755         mutex_unlock(&data->update_lock);
 
 759 static ssize_t show_bank1_alarm(struct device *dev,
 
 760         struct device_attribute *devattr, char *buf)
 
 762         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 763         struct abituguru_data *data = abituguru_update_device(dev);
 
 766         /* See if the alarm bit for this sensor is set, and if the
 
 767            alarm matches the type of alarm we're looking for (for volt
 
 768            it can be either low or high). The type is stored in a few
 
 769            readonly bits in the settings part of the relevant sensor.
 
 770            The bitmask of the type is passed to us in attr->nr. */
 
 771         if ((data->alarms[attr->index / 8] & (0x01 << (attr->index % 8))) &&
 
 772                         (data->bank1_settings[attr->index][0] & attr->nr))
 
 773                 return sprintf(buf, "1\n");
 
 775                 return sprintf(buf, "0\n");
 
 778 static ssize_t show_bank2_alarm(struct device *dev,
 
 779         struct device_attribute *devattr, char *buf)
 
 781         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 782         struct abituguru_data *data = abituguru_update_device(dev);
 
 785         if (data->alarms[2] & (0x01 << attr->index))
 
 786                 return sprintf(buf, "1\n");
 
 788                 return sprintf(buf, "0\n");
 
 791 static ssize_t show_bank1_mask(struct device *dev,
 
 792         struct device_attribute *devattr, char *buf)
 
 794         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 795         struct abituguru_data *data = dev_get_drvdata(dev);
 
 796         if (data->bank1_settings[attr->index][0] & attr->nr)
 
 797                 return sprintf(buf, "1\n");
 
 799                 return sprintf(buf, "0\n");
 
 802 static ssize_t show_bank2_mask(struct device *dev,
 
 803         struct device_attribute *devattr, char *buf)
 
 805         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 806         struct abituguru_data *data = dev_get_drvdata(dev);
 
 807         if (data->bank2_settings[attr->index][0] & attr->nr)
 
 808                 return sprintf(buf, "1\n");
 
 810                 return sprintf(buf, "0\n");
 
 813 static ssize_t store_bank1_mask(struct device *dev,
 
 814         struct device_attribute *devattr, const char *buf, size_t count)
 
 816         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 817         struct abituguru_data *data = dev_get_drvdata(dev);
 
 818         int mask = simple_strtoul(buf, NULL, 10);
 
 822         mutex_lock(&data->update_lock);
 
 823         orig_val = data->bank1_settings[attr->index][0];
 
 826                 data->bank1_settings[attr->index][0] |= attr->nr;
 
 828                 data->bank1_settings[attr->index][0] &= ~attr->nr;
 
 830         if ((data->bank1_settings[attr->index][0] != orig_val) &&
 
 831                         (abituguru_write(data,
 
 832                         ABIT_UGURU_SENSOR_BANK1 + 2, attr->index,
 
 833                         data->bank1_settings[attr->index], 3) < 1)) {
 
 834                 data->bank1_settings[attr->index][0] = orig_val;
 
 837         mutex_unlock(&data->update_lock);
 
 841 static ssize_t store_bank2_mask(struct device *dev,
 
 842         struct device_attribute *devattr, const char *buf, size_t count)
 
 844         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 845         struct abituguru_data *data = dev_get_drvdata(dev);
 
 846         int mask = simple_strtoul(buf, NULL, 10);
 
 850         mutex_lock(&data->update_lock);
 
 851         orig_val = data->bank2_settings[attr->index][0];
 
 854                 data->bank2_settings[attr->index][0] |= attr->nr;
 
 856                 data->bank2_settings[attr->index][0] &= ~attr->nr;
 
 858         if ((data->bank2_settings[attr->index][0] != orig_val) &&
 
 859                         (abituguru_write(data,
 
 860                         ABIT_UGURU_SENSOR_BANK2 + 2, attr->index,
 
 861                         data->bank2_settings[attr->index], 2) < 1)) {
 
 862                 data->bank2_settings[attr->index][0] = orig_val;
 
 865         mutex_unlock(&data->update_lock);
 
 869 /* Fan PWM (speed control) */
 
 870 static ssize_t show_pwm_setting(struct device *dev,
 
 871         struct device_attribute *devattr, char *buf)
 
 873         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 874         struct abituguru_data *data = dev_get_drvdata(dev);
 
 875         return sprintf(buf, "%d\n", data->pwm_settings[attr->index][attr->nr] *
 
 876                 abituguru_pwm_settings_multiplier[attr->nr]);
 
 879 static ssize_t store_pwm_setting(struct device *dev, struct device_attribute
 
 880         *devattr, const char *buf, size_t count)
 
 882         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 883         struct abituguru_data *data = dev_get_drvdata(dev);
 
 884         u8 min, val = (simple_strtoul(buf, NULL, 10) +
 
 885                 abituguru_pwm_settings_multiplier[attr->nr]/2) /
 
 886                 abituguru_pwm_settings_multiplier[attr->nr];
 
 889         /* special case pwm1 min pwm% */
 
 890         if ((attr->index == 0) && ((attr->nr == 1) || (attr->nr == 2)))
 
 893                 min = abituguru_pwm_min[attr->nr];
 
 895         /* this check can be done before taking the lock */
 
 896         if ((val < min) || (val > abituguru_pwm_max[attr->nr]))
 
 899         mutex_lock(&data->update_lock);
 
 900         /* this check needs to be done after taking the lock */
 
 901         if ((attr->nr & 1) &&
 
 902                         (val >= data->pwm_settings[attr->index][attr->nr + 1]))
 
 904         else if (!(attr->nr & 1) &&
 
 905                         (val <= data->pwm_settings[attr->index][attr->nr - 1]))
 
 907         else if (data->pwm_settings[attr->index][attr->nr] != val) {
 
 908                 u8 orig_val = data->pwm_settings[attr->index][attr->nr];
 
 909                 data->pwm_settings[attr->index][attr->nr] = val;
 
 910                 if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
 
 911                                 attr->index, data->pwm_settings[attr->index],
 
 913                         data->pwm_settings[attr->index][attr->nr] =
 
 918         mutex_unlock(&data->update_lock);
 
 922 static ssize_t show_pwm_sensor(struct device *dev,
 
 923         struct device_attribute *devattr, char *buf)
 
 925         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 926         struct abituguru_data *data = dev_get_drvdata(dev);
 
 928         /* We need to walk to the temp sensor addresses to find what
 
 929            the userspace id of the configured temp sensor is. */
 
 930         for (i = 0; i < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]; i++)
 
 931                 if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][i] ==
 
 932                                 (data->pwm_settings[attr->index][0] & 0x0F))
 
 933                         return sprintf(buf, "%d\n", i+1);
 
 938 static ssize_t store_pwm_sensor(struct device *dev, struct device_attribute
 
 939         *devattr, const char *buf, size_t count)
 
 941         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 942         struct abituguru_data *data = dev_get_drvdata(dev);
 
 943         unsigned long val = simple_strtoul(buf, NULL, 10) - 1;
 
 946         mutex_lock(&data->update_lock);
 
 947         if (val < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
 
 948                 u8 orig_val = data->pwm_settings[attr->index][0];
 
 949                 u8 address = data->bank1_address[ABIT_UGURU_TEMP_SENSOR][val];
 
 950                 data->pwm_settings[attr->index][0] &= 0xF0;
 
 951                 data->pwm_settings[attr->index][0] |= address;
 
 952                 if (data->pwm_settings[attr->index][0] != orig_val) {
 
 953                         if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
 
 955                                         data->pwm_settings[attr->index],
 
 957                                 data->pwm_settings[attr->index][0] = orig_val;
 
 964         mutex_unlock(&data->update_lock);
 
 968 static ssize_t show_pwm_enable(struct device *dev,
 
 969         struct device_attribute *devattr, char *buf)
 
 971         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 972         struct abituguru_data *data = dev_get_drvdata(dev);
 
 974         if (data->pwm_settings[attr->index][0] & ABIT_UGURU_FAN_PWM_ENABLE)
 
 976         return sprintf(buf, "%d\n", res);
 
 979 static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
 
 980         *devattr, const char *buf, size_t count)
 
 982         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
 
 983         struct abituguru_data *data = dev_get_drvdata(dev);
 
 984         u8 orig_val, user_val = simple_strtoul(buf, NULL, 10);
 
 987         mutex_lock(&data->update_lock);
 
 988         orig_val = data->pwm_settings[attr->index][0];
 
 991                         data->pwm_settings[attr->index][0] &=
 
 992                                 ~ABIT_UGURU_FAN_PWM_ENABLE;
 
 995                         data->pwm_settings[attr->index][0] |=
 
 996                                 ABIT_UGURU_FAN_PWM_ENABLE;
 
1001         if ((data->pwm_settings[attr->index][0] != orig_val) &&
 
1002                         (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
 
1003                         attr->index, data->pwm_settings[attr->index],
 
1005                 data->pwm_settings[attr->index][0] = orig_val;
 
1008         mutex_unlock(&data->update_lock);
 
1012 static ssize_t show_name(struct device *dev,
 
1013         struct device_attribute *devattr, char *buf)
 
1015         return sprintf(buf, "%s\n", ABIT_UGURU_NAME);
 
1018 /* Sysfs attr templates, the real entries are generated automatically. */
 
1020 struct sensor_device_attribute_2 abituguru_sysfs_bank1_templ[2][9] = {
 
1022         SENSOR_ATTR_2(in%d_input, 0444, show_bank1_value, NULL, 0, 0),
 
1023         SENSOR_ATTR_2(in%d_min, 0644, show_bank1_setting,
 
1024                 store_bank1_setting, 1, 0),
 
1025         SENSOR_ATTR_2(in%d_min_alarm, 0444, show_bank1_alarm, NULL,
 
1026                 ABIT_UGURU_VOLT_LOW_ALARM_FLAG, 0),
 
1027         SENSOR_ATTR_2(in%d_max, 0644, show_bank1_setting,
 
1028                 store_bank1_setting, 2, 0),
 
1029         SENSOR_ATTR_2(in%d_max_alarm, 0444, show_bank1_alarm, NULL,
 
1030                 ABIT_UGURU_VOLT_HIGH_ALARM_FLAG, 0),
 
1031         SENSOR_ATTR_2(in%d_beep, 0644, show_bank1_mask,
 
1032                 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
 
1033         SENSOR_ATTR_2(in%d_shutdown, 0644, show_bank1_mask,
 
1034                 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
 
1035         SENSOR_ATTR_2(in%d_min_alarm_enable, 0644, show_bank1_mask,
 
1036                 store_bank1_mask, ABIT_UGURU_VOLT_LOW_ALARM_ENABLE, 0),
 
1037         SENSOR_ATTR_2(in%d_max_alarm_enable, 0644, show_bank1_mask,
 
1038                 store_bank1_mask, ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE, 0),
 
1040         SENSOR_ATTR_2(temp%d_input, 0444, show_bank1_value, NULL, 0, 0),
 
1041         SENSOR_ATTR_2(temp%d_alarm, 0444, show_bank1_alarm, NULL,
 
1042                 ABIT_UGURU_TEMP_HIGH_ALARM_FLAG, 0),
 
1043         SENSOR_ATTR_2(temp%d_max, 0644, show_bank1_setting,
 
1044                 store_bank1_setting, 1, 0),
 
1045         SENSOR_ATTR_2(temp%d_crit, 0644, show_bank1_setting,
 
1046                 store_bank1_setting, 2, 0),
 
1047         SENSOR_ATTR_2(temp%d_beep, 0644, show_bank1_mask,
 
1048                 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
 
1049         SENSOR_ATTR_2(temp%d_shutdown, 0644, show_bank1_mask,
 
1050                 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
 
1051         SENSOR_ATTR_2(temp%d_alarm_enable, 0644, show_bank1_mask,
 
1052                 store_bank1_mask, ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE, 0),
 
1056 static const struct sensor_device_attribute_2 abituguru_sysfs_fan_templ[6] = {
 
1057         SENSOR_ATTR_2(fan%d_input, 0444, show_bank2_value, NULL, 0, 0),
 
1058         SENSOR_ATTR_2(fan%d_alarm, 0444, show_bank2_alarm, NULL, 0, 0),
 
1059         SENSOR_ATTR_2(fan%d_min, 0644, show_bank2_setting,
 
1060                 store_bank2_setting, 1, 0),
 
1061         SENSOR_ATTR_2(fan%d_beep, 0644, show_bank2_mask,
 
1062                 store_bank2_mask, ABIT_UGURU_BEEP_ENABLE, 0),
 
1063         SENSOR_ATTR_2(fan%d_shutdown, 0644, show_bank2_mask,
 
1064                 store_bank2_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
 
1065         SENSOR_ATTR_2(fan%d_alarm_enable, 0644, show_bank2_mask,
 
1066                 store_bank2_mask, ABIT_UGURU_FAN_LOW_ALARM_ENABLE, 0),
 
1069 static const struct sensor_device_attribute_2 abituguru_sysfs_pwm_templ[6] = {
 
1070         SENSOR_ATTR_2(pwm%d_enable, 0644, show_pwm_enable,
 
1071                 store_pwm_enable, 0, 0),
 
1072         SENSOR_ATTR_2(pwm%d_auto_channels_temp, 0644, show_pwm_sensor,
 
1073                 store_pwm_sensor, 0, 0),
 
1074         SENSOR_ATTR_2(pwm%d_auto_point1_pwm, 0644, show_pwm_setting,
 
1075                 store_pwm_setting, 1, 0),
 
1076         SENSOR_ATTR_2(pwm%d_auto_point2_pwm, 0644, show_pwm_setting,
 
1077                 store_pwm_setting, 2, 0),
 
1078         SENSOR_ATTR_2(pwm%d_auto_point1_temp, 0644, show_pwm_setting,
 
1079                 store_pwm_setting, 3, 0),
 
1080         SENSOR_ATTR_2(pwm%d_auto_point2_temp, 0644, show_pwm_setting,
 
1081                 store_pwm_setting, 4, 0),
 
1084 static struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
 
1085         SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
 
1088 static int __devinit abituguru_probe(struct platform_device *pdev)
 
1090         struct abituguru_data *data;
 
1091         int i, j, used, sysfs_names_free, sysfs_attr_i, res = -ENODEV;
 
1092         char *sysfs_filename;
 
1094         /* El weirdo probe order, to keep the sysfs order identical to the
 
1095            BIOS and window-appliction listing order. */
 
1096         const u8 probe_order[ABIT_UGURU_MAX_BANK1_SENSORS] = {
 
1097                 0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E, 0x02,
 
1098                 0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
 
1100         if (!(data = kzalloc(sizeof(struct abituguru_data), GFP_KERNEL)))
 
1103         data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
 
1104         mutex_init(&data->update_lock);
 
1105         platform_set_drvdata(pdev, data);
 
1107         /* See if the uGuru is ready */
 
1108         if (inb_p(data->addr + ABIT_UGURU_DATA) == ABIT_UGURU_STATUS_INPUT)
 
1109                 data->uguru_ready = 1;
 
1111         /* Completely read the uGuru this has 2 purposes:
 
1112            - testread / see if one really is there.
 
1113            - make an in memory copy of all the uguru settings for future use. */
 
1114         if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
 
1115                         data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3)
 
1116                 goto abituguru_probe_error;
 
1118         for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
 
1119                 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, i,
 
1120                                 &data->bank1_value[i], 1,
 
1121                                 ABIT_UGURU_MAX_RETRIES) != 1)
 
1122                         goto abituguru_probe_error;
 
1123                 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1+1, i,
 
1124                                 data->bank1_settings[i], 3,
 
1125                                 ABIT_UGURU_MAX_RETRIES) != 3)
 
1126                         goto abituguru_probe_error;
 
1128         /* Note: We don't know how many bank2 sensors / pwms there really are,
 
1129            but in order to "detect" this we need to read the maximum amount
 
1130            anyways. If we read sensors/pwms not there we'll just read crap
 
1131            this can't hurt. We need the detection because we don't want
 
1132            unwanted writes, which will hurt! */
 
1133         for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
 
1134                 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
 
1135                                 &data->bank2_value[i], 1,
 
1136                                 ABIT_UGURU_MAX_RETRIES) != 1)
 
1137                         goto abituguru_probe_error;
 
1138                 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2+1, i,
 
1139                                 data->bank2_settings[i], 2,
 
1140                                 ABIT_UGURU_MAX_RETRIES) != 2)
 
1141                         goto abituguru_probe_error;
 
1143         for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
 
1144                 if (abituguru_read(data, ABIT_UGURU_FAN_PWM, i,
 
1145                                 data->pwm_settings[i], 5,
 
1146                                 ABIT_UGURU_MAX_RETRIES) != 5)
 
1147                         goto abituguru_probe_error;
 
1149         data->last_updated = jiffies;
 
1151         /* Detect sensor types and fill the sysfs attr for bank1 */
 
1153         sysfs_filename = data->sysfs_names;
 
1154         sysfs_names_free = ABITUGURU_SYSFS_NAMES_LENGTH;
 
1155         for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
 
1156                 res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
 
1158                         goto abituguru_probe_error;
 
1159                 if (res == ABIT_UGURU_NC)
 
1162                 /* res 1 (temp) sensors have 7 sysfs entries, 0 (in) 9 */
 
1163                 for (j = 0; j < (res ? 7 : 9); j++) {
 
1164                         used = snprintf(sysfs_filename, sysfs_names_free,
 
1165                                 abituguru_sysfs_bank1_templ[res][j].dev_attr.
 
1166                                 attr.name, data->bank1_sensors[res] + res)
 
1168                         data->sysfs_attr[sysfs_attr_i] =
 
1169                                 abituguru_sysfs_bank1_templ[res][j];
 
1170                         data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
 
1172                         data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
 
1173                         sysfs_filename += used;
 
1174                         sysfs_names_free -= used;
 
1177                 data->bank1_max_value[probe_order[i]] =
 
1178                         abituguru_bank1_max_value[res];
 
1179                 data->bank1_address[res][data->bank1_sensors[res]] =
 
1181                 data->bank1_sensors[res]++;
 
1183         /* Detect number of sensors and fill the sysfs attr for bank2 (fans) */
 
1184         abituguru_detect_no_bank2_sensors(data);
 
1185         for (i = 0; i < data->bank2_sensors; i++) {
 
1186                 for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_fan_templ); j++) {
 
1187                         used = snprintf(sysfs_filename, sysfs_names_free,
 
1188                                 abituguru_sysfs_fan_templ[j].dev_attr.attr.name,
 
1190                         data->sysfs_attr[sysfs_attr_i] =
 
1191                                 abituguru_sysfs_fan_templ[j];
 
1192                         data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
 
1194                         data->sysfs_attr[sysfs_attr_i].index = i;
 
1195                         sysfs_filename += used;
 
1196                         sysfs_names_free -= used;
 
1200         /* Detect number of sensors and fill the sysfs attr for pwms */
 
1201         abituguru_detect_no_pwms(data);
 
1202         for (i = 0; i < data->pwms; i++) {
 
1203                 for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_pwm_templ); j++) {
 
1204                         used = snprintf(sysfs_filename, sysfs_names_free,
 
1205                                 abituguru_sysfs_pwm_templ[j].dev_attr.attr.name,
 
1207                         data->sysfs_attr[sysfs_attr_i] =
 
1208                                 abituguru_sysfs_pwm_templ[j];
 
1209                         data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
 
1211                         data->sysfs_attr[sysfs_attr_i].index = i;
 
1212                         sysfs_filename += used;
 
1213                         sysfs_names_free -= used;
 
1217         /* Fail safe check, this should never happen! */
 
1218         if (sysfs_names_free < 0) {
 
1219                 printk(KERN_ERR ABIT_UGURU_NAME ": Fatal error ran out of "
 
1220                        "space for sysfs attr names. This should never "
 
1221                        "happen please report to the abituguru maintainer "
 
1222                        "(see MAINTAINERS)\n");
 
1223                 res = -ENAMETOOLONG;
 
1224                 goto abituguru_probe_error;
 
1226         printk(KERN_INFO ABIT_UGURU_NAME ": found Abit uGuru\n");
 
1228         /* Register sysfs hooks */
 
1229         data->class_dev = hwmon_device_register(&pdev->dev);
 
1230         if (IS_ERR(data->class_dev)) {
 
1231                 res = PTR_ERR(data->class_dev);
 
1232                 goto abituguru_probe_error;
 
1234         for (i = 0; i < sysfs_attr_i; i++)
 
1235                 device_create_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
 
1236         for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
 
1237                 device_create_file(&pdev->dev,
 
1238                         &abituguru_sysfs_attr[i].dev_attr);
 
1242 abituguru_probe_error:
 
1247 static int __devexit abituguru_remove(struct platform_device *pdev)
 
1249         struct abituguru_data *data = platform_get_drvdata(pdev);
 
1251         platform_set_drvdata(pdev, NULL);
 
1252         hwmon_device_unregister(data->class_dev);
 
1258 static struct abituguru_data *abituguru_update_device(struct device *dev)
 
1261         struct abituguru_data *data = dev_get_drvdata(dev);
 
1262         /* fake a complete successful read if no update necessary. */
 
1265         mutex_lock(&data->update_lock);
 
1266         if (time_after(jiffies, data->last_updated + HZ)) {
 
1268                 if ((err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
 
1269                                 data->alarms, 3, 0)) != 3)
 
1271                 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
 
1272                         if ((err = abituguru_read(data,
 
1273                                         ABIT_UGURU_SENSOR_BANK1, i,
 
1274                                         &data->bank1_value[i], 1, 0)) != 1)
 
1276                         if ((err = abituguru_read(data,
 
1277                                         ABIT_UGURU_SENSOR_BANK1 + 1, i,
 
1278                                         data->bank1_settings[i], 3, 0)) != 3)
 
1281                 for (i = 0; i < data->bank2_sensors; i++)
 
1282                         if ((err = abituguru_read(data,
 
1283                                         ABIT_UGURU_SENSOR_BANK2, i,
 
1284                                         &data->bank2_value[i], 1, 0)) != 1)
 
1288                 data->update_timeouts = 0;
 
1290                 /* handle timeout condition */
 
1291                 if (err == -EBUSY) {
 
1292                         /* No overflow please */
 
1293                         if (data->update_timeouts < 255u)
 
1294                                 data->update_timeouts++;
 
1295                         if (data->update_timeouts <= ABIT_UGURU_MAX_TIMEOUTS) {
 
1296                                 ABIT_UGURU_DEBUG(3, "timeout exceeded, will "
 
1297                                         "try again next update\n");
 
1298                                 /* Just a timeout, fake a successful read */
 
1301                                 ABIT_UGURU_DEBUG(1, "timeout exceeded %d "
 
1302                                         "times waiting for more input state\n",
 
1303                                         (int)data->update_timeouts);
 
1305                 /* On success set last_updated */
 
1307                         data->last_updated = jiffies;
 
1309         mutex_unlock(&data->update_lock);
 
1317 static struct platform_driver abituguru_driver = {
 
1319                 .owner  = THIS_MODULE,
 
1320                 .name   = ABIT_UGURU_NAME,
 
1322         .probe  = abituguru_probe,
 
1323         .remove = __devexit_p(abituguru_remove),
 
1326 static int __init abituguru_detect(void)
 
1328         /* See if there is an uguru there. After a reboot uGuru will hold 0x00
 
1329            at DATA and 0xAC, when this driver has already been loaded once
 
1330            DATA will hold 0x08. For most uGuru's CMD will hold 0xAC in either
 
1331            scenario but some will hold 0x00.
 
1332            Some uGuru's initally hold 0x09 at DATA and will only hold 0x08
 
1333            after reading CMD first, so CMD must be read first! */
 
1334         u8 cmd_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_CMD);
 
1335         u8 data_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_DATA);
 
1336         if (((data_val == 0x00) || (data_val == 0x08)) &&
 
1337             ((cmd_val == 0x00) || (cmd_val == 0xAC)))
 
1338                 return ABIT_UGURU_BASE;
 
1340         ABIT_UGURU_DEBUG(2, "no Abit uGuru found, data = 0x%02X, cmd = "
 
1341                 "0x%02X\n", (unsigned int)data_val, (unsigned int)cmd_val);
 
1344                 printk(KERN_INFO ABIT_UGURU_NAME ": Assuming Abit uGuru is "
 
1345                                 "present because of \"force\" parameter\n");
 
1346                 return ABIT_UGURU_BASE;
 
1349         /* No uGuru found */
 
1353 static struct platform_device *abituguru_pdev;
 
1355 static int __init abituguru_init(void)
 
1358         struct resource res = { .flags = IORESOURCE_IO };
 
1360         address = abituguru_detect();
 
1364         err = platform_driver_register(&abituguru_driver);
 
1368         abituguru_pdev = platform_device_alloc(ABIT_UGURU_NAME, address);
 
1369         if (!abituguru_pdev) {
 
1370                 printk(KERN_ERR ABIT_UGURU_NAME
 
1371                         ": Device allocation failed\n");
 
1373                 goto exit_driver_unregister;
 
1376         res.start = address;
 
1377         res.end = address + ABIT_UGURU_REGION_LENGTH - 1;
 
1378         res.name = ABIT_UGURU_NAME;
 
1380         err = platform_device_add_resources(abituguru_pdev, &res, 1);
 
1382                 printk(KERN_ERR ABIT_UGURU_NAME
 
1383                         ": Device resource addition failed (%d)\n", err);
 
1384                 goto exit_device_put;
 
1387         err = platform_device_add(abituguru_pdev);
 
1389                 printk(KERN_ERR ABIT_UGURU_NAME
 
1390                         ": Device addition failed (%d)\n", err);
 
1391                 goto exit_device_put;
 
1397         platform_device_put(abituguru_pdev);
 
1398 exit_driver_unregister:
 
1399         platform_driver_unregister(&abituguru_driver);
 
1404 static void __exit abituguru_exit(void)
 
1406         platform_device_unregister(abituguru_pdev);
 
1407         platform_driver_unregister(&abituguru_driver);
 
1410 MODULE_AUTHOR("Hans de Goede <j.w.r.degoede@hhs.nl>");
 
1411 MODULE_DESCRIPTION("Abit uGuru Sensor device");
 
1412 MODULE_LICENSE("GPL");
 
1414 module_init(abituguru_init);
 
1415 module_exit(abituguru_exit);