[PATCH] hwmon: Fix for first generation Abit uGuru chips
[safe/jmp/linux-2.6] / drivers / hwmon / abituguru.c
1 /*
2     abituguru.c Copyright (c) 2005-2006 Hans de Goede <j.w.r.degoede@hhs.nl>
3
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.
8
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.
13
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.
17 */
18 /*
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!
22 */
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>
32 #include <asm/io.h>
33
34 /* Banks */
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
76    50 sofar is. */
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
83 /* utility macros */
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)
106
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
118 /* uGuru status' */
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 */
123
124 /* Constants */
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 };
139
140
141 /* Insmod parameters */
142 static int force;
143 module_param(force, bool, 0);
144 MODULE_PARM_DESC(force, "Set to one to force detection.");
145 static int bank1_types[ABIT_UGURU_MAX_BANK1_SENSORS] = { -1, -1, -1, -1, -1,
146         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
147 module_param_array(bank1_types, int, NULL, 0);
148 MODULE_PARM_DESC(bank1_types, "Bank1 sensortype autodetection override:\n"
149         "   -1 autodetect\n"
150         "    0 volt sensor\n"
151         "    1 temp sensor\n"
152         "    2 not connected");
153 static int fan_sensors;
154 module_param(fan_sensors, int, 0);
155 MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru "
156         "(0 = autodetect)");
157 static int pwms;
158 module_param(pwms, int, 0);
159 MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru "
160         "(0 = autodetect)");
161
162 /* Default verbose is 2, since this driver is still in the testing phase */
163 static int verbose = 2;
164 module_param(verbose, int, 0644);
165 MODULE_PARM_DESC(verbose, "How verbose should the driver be? (0-3):\n"
166         "   0 normal output\n"
167         "   1 + verbose error reporting\n"
168         "   2 + sensors type probing info\n"
169         "   3 + retryable error reporting");
170
171
172 /* For the Abit uGuru, we need to keep some data in memory.
173    The structure is dynamically allocated, at the same time when a new
174    abituguru device is allocated. */
175 struct abituguru_data {
176         struct class_device *class_dev; /* hwmon registered device */
177         struct mutex update_lock;       /* protect access to data and uGuru */
178         unsigned long last_updated;     /* In jiffies */
179         unsigned short addr;            /* uguru base address */
180         char uguru_ready;               /* is the uguru in ready state? */
181         unsigned char update_timeouts;  /* number of update timeouts since last
182                                            successful update */
183
184         /* The sysfs attr and their names are generated automatically, for bank1
185            we cannot use a predefined array because we don't know beforehand
186            of a sensor is a volt or a temp sensor, for bank2 and the pwms its
187            easier todo things the same way.  For in sensors we have 9 (temp 7)
188            sysfs entries per sensor, for bank2 and pwms 6. */
189         struct sensor_device_attribute_2 sysfs_attr[
190                 ABIT_UGURU_MAX_BANK1_SENSORS * 9 +
191                 ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6];
192         /* Buffer to store the dynamically generated sysfs names */
193         char sysfs_names[ABITUGURU_SYSFS_NAMES_LENGTH];
194
195         /* Bank 1 data */
196         /* number of and addresses of [0] in, [1] temp sensors */
197         u8 bank1_sensors[2];
198         u8 bank1_address[2][ABIT_UGURU_MAX_BANK1_SENSORS];
199         u8 bank1_value[ABIT_UGURU_MAX_BANK1_SENSORS];
200         /* This array holds 3 entries per sensor for the bank 1 sensor settings
201            (flags, min, max for voltage / flags, warn, shutdown for temp). */
202         u8 bank1_settings[ABIT_UGURU_MAX_BANK1_SENSORS][3];
203         /* Maximum value for each sensor used for scaling in mV/millidegrees
204            Celsius. */
205         int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS];
206
207         /* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */
208         u8 bank2_sensors; /* actual number of bank2 sensors found */
209         u8 bank2_value[ABIT_UGURU_MAX_BANK2_SENSORS];
210         u8 bank2_settings[ABIT_UGURU_MAX_BANK2_SENSORS][2]; /* flags, min */
211
212         /* Alarms 2 bytes for bank1, 1 byte for bank2 */
213         u8 alarms[3];
214
215         /* Fan PWM (speed control) 5 bytes per PWM */
216         u8 pwms; /* actual number of pwms found */
217         u8 pwm_settings[ABIT_UGURU_MAX_PWMS][5];
218 };
219
220 /* wait till the uguru is in the specified state */
221 static int abituguru_wait(struct abituguru_data *data, u8 state)
222 {
223         int timeout = ABIT_UGURU_WAIT_TIMEOUT;
224
225         while (inb_p(data->addr + ABIT_UGURU_DATA) != state) {
226                 timeout--;
227                 if (timeout == 0)
228                         return -EBUSY;
229         }
230         return 0;
231 }
232
233 /* Put the uguru in ready for input state */
234 static int abituguru_ready(struct abituguru_data *data)
235 {
236         int timeout = ABIT_UGURU_READY_TIMEOUT;
237
238         if (data->uguru_ready)
239                 return 0;
240
241         /* Reset? / Prepare for next read/write cycle */
242         outb(0x00, data->addr + ABIT_UGURU_DATA);
243
244         /* Wait till the uguru is ready */
245         if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) {
246                 ABIT_UGURU_DEBUG(1,
247                         "timeout exceeded waiting for ready state\n");
248                 return -EIO;
249         }
250
251         /* Cmd port MUST be read now and should contain 0xAC */
252         while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
253                 timeout--;
254                 if (timeout == 0) {
255                         ABIT_UGURU_DEBUG(1,
256                            "CMD reg does not hold 0xAC after ready command\n");
257                         return -EIO;
258                 }
259         }
260
261         /* After this the ABIT_UGURU_DATA port should contain
262            ABIT_UGURU_STATUS_INPUT */
263         timeout = ABIT_UGURU_READY_TIMEOUT;
264         while (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT) {
265                 timeout--;
266                 if (timeout == 0) {
267                         ABIT_UGURU_DEBUG(1,
268                                 "state != more input after ready command\n");
269                         return -EIO;
270                 }
271         }
272
273         data->uguru_ready = 1;
274         return 0;
275 }
276
277 /* Send the bank and then sensor address to the uGuru for the next read/write
278    cycle. This function gets called as the first part of a read/write by
279    abituguru_read and abituguru_write. This function should never be
280    called by any other function. */
281 static int abituguru_send_address(struct abituguru_data *data,
282         u8 bank_addr, u8 sensor_addr, int retries)
283 {
284         /* assume the caller does error handling itself if it has not requested
285            any retries, and thus be quiet. */
286         int report_errors = retries;
287
288         for (;;) {
289                 /* Make sure the uguru is ready and then send the bank address,
290                    after this the uguru is no longer "ready". */
291                 if (abituguru_ready(data) != 0)
292                         return -EIO;
293                 outb(bank_addr, data->addr + ABIT_UGURU_DATA);
294                 data->uguru_ready = 0;
295
296                 /* Wait till the uguru is ABIT_UGURU_STATUS_INPUT state again
297                    and send the sensor addr */
298                 if (abituguru_wait(data, ABIT_UGURU_STATUS_INPUT)) {
299                         if (retries) {
300                                 ABIT_UGURU_DEBUG(3, "timeout exceeded "
301                                         "waiting for more input state, %d "
302                                         "tries remaining\n", retries);
303                                 set_current_state(TASK_UNINTERRUPTIBLE);
304                                 schedule_timeout(ABIT_UGURU_RETRY_DELAY);
305                                 retries--;
306                                 continue;
307                         }
308                         if (report_errors)
309                                 ABIT_UGURU_DEBUG(1, "timeout exceeded "
310                                         "waiting for more input state "
311                                         "(bank: %d)\n", (int)bank_addr);
312                         return -EBUSY;
313                 }
314                 outb(sensor_addr, data->addr + ABIT_UGURU_CMD);
315                 return 0;
316         }
317 }
318
319 /* Read count bytes from sensor sensor_addr in bank bank_addr and store the
320    result in buf, retry the send address part of the read retries times. */
321 static int abituguru_read(struct abituguru_data *data,
322         u8 bank_addr, u8 sensor_addr, u8 *buf, int count, int retries)
323 {
324         int i;
325
326         /* Send the address */
327         i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
328         if (i)
329                 return i;
330
331         /* And read the data */
332         for (i = 0; i < count; i++) {
333                 if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
334                         ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
335                                 "read state (bank: %d, sensor: %d)\n",
336                                 (int)bank_addr, (int)sensor_addr);
337                         break;
338                 }
339                 buf[i] = inb(data->addr + ABIT_UGURU_CMD);
340         }
341
342         /* Last put the chip back in ready state */
343         abituguru_ready(data);
344
345         return i;
346 }
347
348 /* Write count bytes from buf to sensor sensor_addr in bank bank_addr, the send
349    address part of the write is always retried ABIT_UGURU_MAX_RETRIES times. */
350 static int abituguru_write(struct abituguru_data *data,
351         u8 bank_addr, u8 sensor_addr, u8 *buf, int count)
352 {
353         int i;
354
355         /* Send the address */
356         i = abituguru_send_address(data, bank_addr, sensor_addr,
357                 ABIT_UGURU_MAX_RETRIES);
358         if (i)
359                 return i;
360
361         /* And write the data */
362         for (i = 0; i < count; i++) {
363                 if (abituguru_wait(data, ABIT_UGURU_STATUS_WRITE)) {
364                         ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
365                                 "write state (bank: %d, sensor: %d)\n",
366                                 (int)bank_addr, (int)sensor_addr);
367                         break;
368                 }
369                 outb(buf[i], data->addr + ABIT_UGURU_CMD);
370         }
371
372         /* Now we need to wait till the chip is ready to be read again,
373            don't ask why */
374         if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
375                 ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for read state "
376                         "after write (bank: %d, sensor: %d)\n", (int)bank_addr,
377                         (int)sensor_addr);
378                 return -EIO;
379         }
380
381         /* Cmd port MUST be read now and should contain 0xAC */
382         if (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
383                 ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after write "
384                         "(bank: %d, sensor: %d)\n", (int)bank_addr,
385                         (int)sensor_addr);
386                 return -EIO;
387         }
388
389         /* Last put the chip back in ready state */
390         abituguru_ready(data);
391
392         return i;
393 }
394
395 /* Detect sensor type. Temp and Volt sensors are enabled with
396    different masks and will ignore enable masks not meant for them.
397    This enables us to test what kind of sensor we're dealing with.
398    By setting the alarm thresholds so that we will always get an
399    alarm for sensor type X and then enabling the sensor as sensor type
400    X, if we then get an alarm it is a sensor of type X. */
401 static int __devinit
402 abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
403                                    u8 sensor_addr)
404 {
405         u8 val, buf[3];
406         int ret = ABIT_UGURU_NC;
407
408         /* If overriden by the user return the user selected type */
409         if (bank1_types[sensor_addr] >= ABIT_UGURU_IN_SENSOR &&
410                         bank1_types[sensor_addr] <= ABIT_UGURU_NC) {
411                 ABIT_UGURU_DEBUG(2, "assuming sensor type %d for bank1 sensor "
412                         "%d because of \"bank1_types\" module param\n",
413                         bank1_types[sensor_addr], (int)sensor_addr);
414                 return bank1_types[sensor_addr];
415         }
416
417         /* First read the sensor and the current settings */
418         if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, sensor_addr, &val,
419                         1, ABIT_UGURU_MAX_RETRIES) != 1)
420                 return -ENODEV;
421
422         /* Test val is sane / usable for sensor type detection. */
423         if ((val < 10u) || (val > 240u)) {
424                 printk(KERN_WARNING ABIT_UGURU_NAME
425                         ": bank1-sensor: %d reading (%d) too close to limits, "
426                         "unable to determine sensor type, skipping sensor\n",
427                         (int)sensor_addr, (int)val);
428                 /* assume no sensor is there for sensors for which we can't
429                    determine the sensor type because their reading is too close
430                    to their limits, this usually means no sensor is there. */
431                 return ABIT_UGURU_NC;
432         }
433
434         ABIT_UGURU_DEBUG(2, "testing bank1 sensor %d\n", (int)sensor_addr);
435         /* Volt sensor test, enable volt low alarm, set min value ridicously
436            high. If its a volt sensor this should always give us an alarm. */
437         buf[0] = ABIT_UGURU_VOLT_LOW_ALARM_ENABLE;
438         buf[1] = 245;
439         buf[2] = 250;
440         if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
441                         buf, 3) != 3)
442                 return -ENODEV;
443         /* Now we need 20 ms to give the uguru time to read the sensors
444            and raise a voltage alarm */
445         set_current_state(TASK_UNINTERRUPTIBLE);
446         schedule_timeout(HZ/50);
447         /* Check for alarm and check the alarm is a volt low alarm. */
448         if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
449                         ABIT_UGURU_MAX_RETRIES) != 3)
450                 return -ENODEV;
451         if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
452                 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
453                                 sensor_addr, buf, 3,
454                                 ABIT_UGURU_MAX_RETRIES) != 3)
455                         return -ENODEV;
456                 if (buf[0] & ABIT_UGURU_VOLT_LOW_ALARM_FLAG) {
457                         /* Restore original settings */
458                         if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
459                                         sensor_addr,
460                                         data->bank1_settings[sensor_addr],
461                                         3) != 3)
462                                 return -ENODEV;
463                         ABIT_UGURU_DEBUG(2, "  found volt sensor\n");
464                         return ABIT_UGURU_IN_SENSOR;
465                 } else
466                         ABIT_UGURU_DEBUG(2, "  alarm raised during volt "
467                                 "sensor test, but volt low flag not set\n");
468         } else
469                 ABIT_UGURU_DEBUG(2, "  alarm not raised during volt sensor "
470                         "test\n");
471
472         /* Temp sensor test, enable sensor as a temp sensor, set beep value
473            ridicously low (but not too low, otherwise uguru ignores it).
474            If its a temp sensor this should always give us an alarm. */
475         buf[0] = ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE;
476         buf[1] = 5;
477         buf[2] = 10;
478         if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
479                         buf, 3) != 3)
480                 return -ENODEV;
481         /* Now we need 50 ms to give the uguru time to read the sensors
482            and raise a temp alarm */
483         set_current_state(TASK_UNINTERRUPTIBLE);
484         schedule_timeout(HZ/20);
485         /* Check for alarm and check the alarm is a temp high alarm. */
486         if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
487                         ABIT_UGURU_MAX_RETRIES) != 3)
488                 return -ENODEV;
489         if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
490                 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
491                                 sensor_addr, buf, 3,
492                                 ABIT_UGURU_MAX_RETRIES) != 3)
493                         return -ENODEV;
494                 if (buf[0] & ABIT_UGURU_TEMP_HIGH_ALARM_FLAG) {
495                         ret = ABIT_UGURU_TEMP_SENSOR;
496                         ABIT_UGURU_DEBUG(2, "  found temp sensor\n");
497                 } else
498                         ABIT_UGURU_DEBUG(2, "  alarm raised during temp "
499                                 "sensor test, but temp high flag not set\n");
500         } else
501                 ABIT_UGURU_DEBUG(2, "  alarm not raised during temp sensor "
502                         "test\n");
503
504         /* Restore original settings */
505         if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
506                         data->bank1_settings[sensor_addr], 3) != 3)
507                 return -ENODEV;
508
509         return ret;
510 }
511
512 /* These functions try to find out how many sensors there are in bank2 and how
513    many pwms there are. The purpose of this is to make sure that we don't give
514    the user the possibility to change settings for non-existent sensors / pwm.
515    The uGuru will happily read / write whatever memory happens to be after the
516    memory storing the PWM settings when reading/writing to a PWM which is not
517    there. Notice even if we detect a PWM which doesn't exist we normally won't
518    write to it, unless the user tries to change the settings.
519
520    Although the uGuru allows reading (settings) from non existing bank2
521    sensors, my version of the uGuru does seem to stop writing to them, the
522    write function above aborts in this case with:
523    "CMD reg does not hold 0xAC after write"
524
525    Notice these 2 tests are non destructive iow read-only tests, otherwise
526    they would defeat their purpose. Although for the bank2_sensors detection a
527    read/write test would be feasible because of the reaction above, I've
528    however opted to stay on the safe side. */
529 static void __devinit
530 abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
531 {
532         int i;
533
534         if (fan_sensors > 0 && fan_sensors <= ABIT_UGURU_MAX_BANK2_SENSORS) {
535                 data->bank2_sensors = fan_sensors;
536                 ABIT_UGURU_DEBUG(2, "assuming %d fan sensors because of "
537                         "\"fan_sensors\" module param\n",
538                         (int)data->bank2_sensors);
539                 return;
540         }
541
542         ABIT_UGURU_DEBUG(2, "detecting number of fan sensors\n");
543         for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
544                 /* 0x89 are the known used bits:
545                    -0x80 enable shutdown
546                    -0x08 enable beep
547                    -0x01 enable alarm
548                    All other bits should be 0, but on some motherboards
549                    0x40 (bit 6) is also high for some of the fans?? */
550                 if (data->bank2_settings[i][0] & ~0xC9) {
551                         ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
552                                 "to be a fan sensor: settings[0] = %02X\n",
553                                 i, (unsigned int)data->bank2_settings[i][0]);
554                         break;
555                 }
556
557                 /* check if the threshold is within the allowed range */
558                 if (data->bank2_settings[i][1] <
559                                 abituguru_bank2_min_threshold) {
560                         ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
561                                 "to be a fan sensor: the threshold (%d) is "
562                                 "below the minimum (%d)\n", i,
563                                 (int)data->bank2_settings[i][1],
564                                 (int)abituguru_bank2_min_threshold);
565                         break;
566                 }
567                 if (data->bank2_settings[i][1] >
568                                 abituguru_bank2_max_threshold) {
569                         ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
570                                 "to be a fan sensor: the threshold (%d) is "
571                                 "above the maximum (%d)\n", i,
572                                 (int)data->bank2_settings[i][1],
573                                 (int)abituguru_bank2_max_threshold);
574                         break;
575                 }
576         }
577
578         data->bank2_sensors = i;
579         ABIT_UGURU_DEBUG(2, " found: %d fan sensors\n",
580                 (int)data->bank2_sensors);
581 }
582
583 static void __devinit
584 abituguru_detect_no_pwms(struct abituguru_data *data)
585 {
586         int i, j;
587
588         if (pwms > 0 && pwms <= ABIT_UGURU_MAX_PWMS) {
589                 data->pwms = pwms;
590                 ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
591                         "\"pwms\" module param\n", (int)data->pwms);
592                 return;
593         }
594
595         ABIT_UGURU_DEBUG(2, "detecting number of PWM outputs\n");
596         for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
597                 /* 0x80 is the enable bit and the low
598                    nibble is which temp sensor to use,
599                    the other bits should be 0 */
600                 if (data->pwm_settings[i][0] & ~0x8F) {
601                         ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
602                                 "to be a pwm channel: settings[0] = %02X\n",
603                                 i, (unsigned int)data->pwm_settings[i][0]);
604                         break;
605                 }
606
607                 /* the low nibble must correspond to one of the temp sensors
608                    we've found */
609                 for (j = 0; j < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR];
610                                 j++) {
611                         if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][j] ==
612                                         (data->pwm_settings[i][0] & 0x0F))
613                                 break;
614                 }
615                 if (j == data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
616                         ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
617                                 "to be a pwm channel: %d is not a valid temp "
618                                 "sensor address\n", i,
619                                 data->pwm_settings[i][0] & 0x0F);
620                         break;
621                 }
622
623                 /* check if all other settings are within the allowed range */
624                 for (j = 1; j < 5; j++) {
625                         u8 min;
626                         /* special case pwm1 min pwm% */
627                         if ((i == 0) && ((j == 1) || (j == 2)))
628                                 min = 77;
629                         else
630                                 min = abituguru_pwm_min[j];
631                         if (data->pwm_settings[i][j] < min) {
632                                 ABIT_UGURU_DEBUG(2, "  pwm channel %d does "
633                                         "not seem to be a pwm channel: "
634                                         "setting %d (%d) is below the minimum "
635                                         "value (%d)\n", i, j,
636                                         (int)data->pwm_settings[i][j],
637                                         (int)min);
638                                 goto abituguru_detect_no_pwms_exit;
639                         }
640                         if (data->pwm_settings[i][j] > abituguru_pwm_max[j]) {
641                                 ABIT_UGURU_DEBUG(2, "  pwm channel %d does "
642                                         "not seem to be a pwm channel: "
643                                         "setting %d (%d) is above the maximum "
644                                         "value (%d)\n", i, j,
645                                         (int)data->pwm_settings[i][j],
646                                         (int)abituguru_pwm_max[j]);
647                                 goto abituguru_detect_no_pwms_exit;
648                         }
649                 }
650
651                 /* check that min temp < max temp and min pwm < max pwm */
652                 if (data->pwm_settings[i][1] >= data->pwm_settings[i][2]) {
653                         ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
654                                 "to be a pwm channel: min pwm (%d) >= "
655                                 "max pwm (%d)\n", i,
656                                 (int)data->pwm_settings[i][1],
657                                 (int)data->pwm_settings[i][2]);
658                         break;
659                 }
660                 if (data->pwm_settings[i][3] >= data->pwm_settings[i][4]) {
661                         ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
662                                 "to be a pwm channel: min temp (%d) >= "
663                                 "max temp (%d)\n", i,
664                                 (int)data->pwm_settings[i][3],
665                                 (int)data->pwm_settings[i][4]);
666                         break;
667                 }
668         }
669
670 abituguru_detect_no_pwms_exit:
671         data->pwms = i;
672         ABIT_UGURU_DEBUG(2, " found: %d PWM outputs\n", (int)data->pwms);
673 }
674
675 /* Following are the sysfs callback functions. These functions expect:
676    sensor_device_attribute_2->index:   sensor address/offset in the bank
677    sensor_device_attribute_2->nr:      register offset, bitmask or NA. */
678 static struct abituguru_data *abituguru_update_device(struct device *dev);
679
680 static ssize_t show_bank1_value(struct device *dev,
681         struct device_attribute *devattr, char *buf)
682 {
683         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
684         struct abituguru_data *data = abituguru_update_device(dev);
685         if (!data)
686                 return -EIO;
687         return sprintf(buf, "%d\n", (data->bank1_value[attr->index] *
688                 data->bank1_max_value[attr->index] + 128) / 255);
689 }
690
691 static ssize_t show_bank1_setting(struct device *dev,
692         struct device_attribute *devattr, char *buf)
693 {
694         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
695         struct abituguru_data *data = dev_get_drvdata(dev);
696         return sprintf(buf, "%d\n",
697                 (data->bank1_settings[attr->index][attr->nr] *
698                 data->bank1_max_value[attr->index] + 128) / 255);
699 }
700
701 static ssize_t show_bank2_value(struct device *dev,
702         struct device_attribute *devattr, char *buf)
703 {
704         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
705         struct abituguru_data *data = abituguru_update_device(dev);
706         if (!data)
707                 return -EIO;
708         return sprintf(buf, "%d\n", (data->bank2_value[attr->index] *
709                 ABIT_UGURU_FAN_MAX + 128) / 255);
710 }
711
712 static ssize_t show_bank2_setting(struct device *dev,
713         struct device_attribute *devattr, char *buf)
714 {
715         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
716         struct abituguru_data *data = dev_get_drvdata(dev);
717         return sprintf(buf, "%d\n",
718                 (data->bank2_settings[attr->index][attr->nr] *
719                 ABIT_UGURU_FAN_MAX + 128) / 255);
720 }
721
722 static ssize_t store_bank1_setting(struct device *dev, struct device_attribute
723         *devattr, const char *buf, size_t count)
724 {
725         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
726         struct abituguru_data *data = dev_get_drvdata(dev);
727         u8 val = (simple_strtoul(buf, NULL, 10) * 255 +
728                 data->bank1_max_value[attr->index]/2) /
729                 data->bank1_max_value[attr->index];
730         ssize_t ret = count;
731
732         mutex_lock(&data->update_lock);
733         if (data->bank1_settings[attr->index][attr->nr] != val) {
734                 u8 orig_val = data->bank1_settings[attr->index][attr->nr];
735                 data->bank1_settings[attr->index][attr->nr] = val;
736                 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
737                                 attr->index, data->bank1_settings[attr->index],
738                                 3) <= attr->nr) {
739                         data->bank1_settings[attr->index][attr->nr] = orig_val;
740                         ret = -EIO;
741                 }
742         }
743         mutex_unlock(&data->update_lock);
744         return ret;
745 }
746
747 static ssize_t store_bank2_setting(struct device *dev, struct device_attribute
748         *devattr, const char *buf, size_t count)
749 {
750         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
751         struct abituguru_data *data = dev_get_drvdata(dev);
752         u8 val = (simple_strtoul(buf, NULL, 10)*255 + ABIT_UGURU_FAN_MAX/2) /
753                 ABIT_UGURU_FAN_MAX;
754         ssize_t ret = count;
755
756         /* this check can be done before taking the lock */
757         if ((val < abituguru_bank2_min_threshold) ||
758                         (val > abituguru_bank2_max_threshold))
759                 return -EINVAL;
760
761         mutex_lock(&data->update_lock);
762         if (data->bank2_settings[attr->index][attr->nr] != val) {
763                 u8 orig_val = data->bank2_settings[attr->index][attr->nr];
764                 data->bank2_settings[attr->index][attr->nr] = val;
765                 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK2 + 2,
766                                 attr->index, data->bank2_settings[attr->index],
767                                 2) <= attr->nr) {
768                         data->bank2_settings[attr->index][attr->nr] = orig_val;
769                         ret = -EIO;
770                 }
771         }
772         mutex_unlock(&data->update_lock);
773         return ret;
774 }
775
776 static ssize_t show_bank1_alarm(struct device *dev,
777         struct device_attribute *devattr, char *buf)
778 {
779         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
780         struct abituguru_data *data = abituguru_update_device(dev);
781         if (!data)
782                 return -EIO;
783         /* See if the alarm bit for this sensor is set, and if the
784            alarm matches the type of alarm we're looking for (for volt
785            it can be either low or high). The type is stored in a few
786            readonly bits in the settings part of the relevant sensor.
787            The bitmask of the type is passed to us in attr->nr. */
788         if ((data->alarms[attr->index / 8] & (0x01 << (attr->index % 8))) &&
789                         (data->bank1_settings[attr->index][0] & attr->nr))
790                 return sprintf(buf, "1\n");
791         else
792                 return sprintf(buf, "0\n");
793 }
794
795 static ssize_t show_bank2_alarm(struct device *dev,
796         struct device_attribute *devattr, char *buf)
797 {
798         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
799         struct abituguru_data *data = abituguru_update_device(dev);
800         if (!data)
801                 return -EIO;
802         if (data->alarms[2] & (0x01 << attr->index))
803                 return sprintf(buf, "1\n");
804         else
805                 return sprintf(buf, "0\n");
806 }
807
808 static ssize_t show_bank1_mask(struct device *dev,
809         struct device_attribute *devattr, char *buf)
810 {
811         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
812         struct abituguru_data *data = dev_get_drvdata(dev);
813         if (data->bank1_settings[attr->index][0] & attr->nr)
814                 return sprintf(buf, "1\n");
815         else
816                 return sprintf(buf, "0\n");
817 }
818
819 static ssize_t show_bank2_mask(struct device *dev,
820         struct device_attribute *devattr, char *buf)
821 {
822         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
823         struct abituguru_data *data = dev_get_drvdata(dev);
824         if (data->bank2_settings[attr->index][0] & attr->nr)
825                 return sprintf(buf, "1\n");
826         else
827                 return sprintf(buf, "0\n");
828 }
829
830 static ssize_t store_bank1_mask(struct device *dev,
831         struct device_attribute *devattr, const char *buf, size_t count)
832 {
833         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
834         struct abituguru_data *data = dev_get_drvdata(dev);
835         int mask = simple_strtoul(buf, NULL, 10);
836         ssize_t ret = count;
837         u8 orig_val;
838
839         mutex_lock(&data->update_lock);
840         orig_val = data->bank1_settings[attr->index][0];
841
842         if (mask)
843                 data->bank1_settings[attr->index][0] |= attr->nr;
844         else
845                 data->bank1_settings[attr->index][0] &= ~attr->nr;
846
847         if ((data->bank1_settings[attr->index][0] != orig_val) &&
848                         (abituguru_write(data,
849                         ABIT_UGURU_SENSOR_BANK1 + 2, attr->index,
850                         data->bank1_settings[attr->index], 3) < 1)) {
851                 data->bank1_settings[attr->index][0] = orig_val;
852                 ret = -EIO;
853         }
854         mutex_unlock(&data->update_lock);
855         return ret;
856 }
857
858 static ssize_t store_bank2_mask(struct device *dev,
859         struct device_attribute *devattr, const char *buf, size_t count)
860 {
861         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
862         struct abituguru_data *data = dev_get_drvdata(dev);
863         int mask = simple_strtoul(buf, NULL, 10);
864         ssize_t ret = count;
865         u8 orig_val;
866
867         mutex_lock(&data->update_lock);
868         orig_val = data->bank2_settings[attr->index][0];
869
870         if (mask)
871                 data->bank2_settings[attr->index][0] |= attr->nr;
872         else
873                 data->bank2_settings[attr->index][0] &= ~attr->nr;
874
875         if ((data->bank2_settings[attr->index][0] != orig_val) &&
876                         (abituguru_write(data,
877                         ABIT_UGURU_SENSOR_BANK2 + 2, attr->index,
878                         data->bank2_settings[attr->index], 2) < 1)) {
879                 data->bank2_settings[attr->index][0] = orig_val;
880                 ret = -EIO;
881         }
882         mutex_unlock(&data->update_lock);
883         return ret;
884 }
885
886 /* Fan PWM (speed control) */
887 static ssize_t show_pwm_setting(struct device *dev,
888         struct device_attribute *devattr, char *buf)
889 {
890         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
891         struct abituguru_data *data = dev_get_drvdata(dev);
892         return sprintf(buf, "%d\n", data->pwm_settings[attr->index][attr->nr] *
893                 abituguru_pwm_settings_multiplier[attr->nr]);
894 }
895
896 static ssize_t store_pwm_setting(struct device *dev, struct device_attribute
897         *devattr, const char *buf, size_t count)
898 {
899         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
900         struct abituguru_data *data = dev_get_drvdata(dev);
901         u8 min, val = (simple_strtoul(buf, NULL, 10) +
902                 abituguru_pwm_settings_multiplier[attr->nr]/2) /
903                 abituguru_pwm_settings_multiplier[attr->nr];
904         ssize_t ret = count;
905
906         /* special case pwm1 min pwm% */
907         if ((attr->index == 0) && ((attr->nr == 1) || (attr->nr == 2)))
908                 min = 77;
909         else
910                 min = abituguru_pwm_min[attr->nr];
911
912         /* this check can be done before taking the lock */
913         if ((val < min) || (val > abituguru_pwm_max[attr->nr]))
914                 return -EINVAL;
915
916         mutex_lock(&data->update_lock);
917         /* this check needs to be done after taking the lock */
918         if ((attr->nr & 1) &&
919                         (val >= data->pwm_settings[attr->index][attr->nr + 1]))
920                 ret = -EINVAL;
921         else if (!(attr->nr & 1) &&
922                         (val <= data->pwm_settings[attr->index][attr->nr - 1]))
923                 ret = -EINVAL;
924         else if (data->pwm_settings[attr->index][attr->nr] != val) {
925                 u8 orig_val = data->pwm_settings[attr->index][attr->nr];
926                 data->pwm_settings[attr->index][attr->nr] = val;
927                 if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
928                                 attr->index, data->pwm_settings[attr->index],
929                                 5) <= attr->nr) {
930                         data->pwm_settings[attr->index][attr->nr] =
931                                 orig_val;
932                         ret = -EIO;
933                 }
934         }
935         mutex_unlock(&data->update_lock);
936         return ret;
937 }
938
939 static ssize_t show_pwm_sensor(struct device *dev,
940         struct device_attribute *devattr, char *buf)
941 {
942         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
943         struct abituguru_data *data = dev_get_drvdata(dev);
944         int i;
945         /* We need to walk to the temp sensor addresses to find what
946            the userspace id of the configured temp sensor is. */
947         for (i = 0; i < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]; i++)
948                 if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][i] ==
949                                 (data->pwm_settings[attr->index][0] & 0x0F))
950                         return sprintf(buf, "%d\n", i+1);
951
952         return -ENXIO;
953 }
954
955 static ssize_t store_pwm_sensor(struct device *dev, struct device_attribute
956         *devattr, const char *buf, size_t count)
957 {
958         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
959         struct abituguru_data *data = dev_get_drvdata(dev);
960         unsigned long val = simple_strtoul(buf, NULL, 10) - 1;
961         ssize_t ret = count;
962
963         mutex_lock(&data->update_lock);
964         if (val < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
965                 u8 orig_val = data->pwm_settings[attr->index][0];
966                 u8 address = data->bank1_address[ABIT_UGURU_TEMP_SENSOR][val];
967                 data->pwm_settings[attr->index][0] &= 0xF0;
968                 data->pwm_settings[attr->index][0] |= address;
969                 if (data->pwm_settings[attr->index][0] != orig_val) {
970                         if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
971                                         attr->index,
972                                         data->pwm_settings[attr->index],
973                                         5) < 1) {
974                                 data->pwm_settings[attr->index][0] = orig_val;
975                                 ret = -EIO;
976                         }
977                 }
978         }
979         else
980                 ret = -EINVAL;
981         mutex_unlock(&data->update_lock);
982         return ret;
983 }
984
985 static ssize_t show_pwm_enable(struct device *dev,
986         struct device_attribute *devattr, char *buf)
987 {
988         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
989         struct abituguru_data *data = dev_get_drvdata(dev);
990         int res = 0;
991         if (data->pwm_settings[attr->index][0] & ABIT_UGURU_FAN_PWM_ENABLE)
992                 res = 2;
993         return sprintf(buf, "%d\n", res);
994 }
995
996 static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
997         *devattr, const char *buf, size_t count)
998 {
999         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1000         struct abituguru_data *data = dev_get_drvdata(dev);
1001         u8 orig_val, user_val = simple_strtoul(buf, NULL, 10);
1002         ssize_t ret = count;
1003
1004         mutex_lock(&data->update_lock);
1005         orig_val = data->pwm_settings[attr->index][0];
1006         switch (user_val) {
1007                 case 0:
1008                         data->pwm_settings[attr->index][0] &=
1009                                 ~ABIT_UGURU_FAN_PWM_ENABLE;
1010                         break;
1011                 case 2:
1012                         data->pwm_settings[attr->index][0] |=
1013                                 ABIT_UGURU_FAN_PWM_ENABLE;
1014                         break;
1015                 default:
1016                         ret = -EINVAL;
1017         }
1018         if ((data->pwm_settings[attr->index][0] != orig_val) &&
1019                         (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1020                         attr->index, data->pwm_settings[attr->index],
1021                         5) < 1)) {
1022                 data->pwm_settings[attr->index][0] = orig_val;
1023                 ret = -EIO;
1024         }
1025         mutex_unlock(&data->update_lock);
1026         return ret;
1027 }
1028
1029 static ssize_t show_name(struct device *dev,
1030         struct device_attribute *devattr, char *buf)
1031 {
1032         return sprintf(buf, "%s\n", ABIT_UGURU_NAME);
1033 }
1034
1035 /* Sysfs attr templates, the real entries are generated automatically. */
1036 static const
1037 struct sensor_device_attribute_2 abituguru_sysfs_bank1_templ[2][9] = {
1038         {
1039         SENSOR_ATTR_2(in%d_input, 0444, show_bank1_value, NULL, 0, 0),
1040         SENSOR_ATTR_2(in%d_min, 0644, show_bank1_setting,
1041                 store_bank1_setting, 1, 0),
1042         SENSOR_ATTR_2(in%d_min_alarm, 0444, show_bank1_alarm, NULL,
1043                 ABIT_UGURU_VOLT_LOW_ALARM_FLAG, 0),
1044         SENSOR_ATTR_2(in%d_max, 0644, show_bank1_setting,
1045                 store_bank1_setting, 2, 0),
1046         SENSOR_ATTR_2(in%d_max_alarm, 0444, show_bank1_alarm, NULL,
1047                 ABIT_UGURU_VOLT_HIGH_ALARM_FLAG, 0),
1048         SENSOR_ATTR_2(in%d_beep, 0644, show_bank1_mask,
1049                 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1050         SENSOR_ATTR_2(in%d_shutdown, 0644, show_bank1_mask,
1051                 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1052         SENSOR_ATTR_2(in%d_min_alarm_enable, 0644, show_bank1_mask,
1053                 store_bank1_mask, ABIT_UGURU_VOLT_LOW_ALARM_ENABLE, 0),
1054         SENSOR_ATTR_2(in%d_max_alarm_enable, 0644, show_bank1_mask,
1055                 store_bank1_mask, ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE, 0),
1056         }, {
1057         SENSOR_ATTR_2(temp%d_input, 0444, show_bank1_value, NULL, 0, 0),
1058         SENSOR_ATTR_2(temp%d_alarm, 0444, show_bank1_alarm, NULL,
1059                 ABIT_UGURU_TEMP_HIGH_ALARM_FLAG, 0),
1060         SENSOR_ATTR_2(temp%d_max, 0644, show_bank1_setting,
1061                 store_bank1_setting, 1, 0),
1062         SENSOR_ATTR_2(temp%d_crit, 0644, show_bank1_setting,
1063                 store_bank1_setting, 2, 0),
1064         SENSOR_ATTR_2(temp%d_beep, 0644, show_bank1_mask,
1065                 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1066         SENSOR_ATTR_2(temp%d_shutdown, 0644, show_bank1_mask,
1067                 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1068         SENSOR_ATTR_2(temp%d_alarm_enable, 0644, show_bank1_mask,
1069                 store_bank1_mask, ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE, 0),
1070         }
1071 };
1072
1073 static const struct sensor_device_attribute_2 abituguru_sysfs_fan_templ[6] = {
1074         SENSOR_ATTR_2(fan%d_input, 0444, show_bank2_value, NULL, 0, 0),
1075         SENSOR_ATTR_2(fan%d_alarm, 0444, show_bank2_alarm, NULL, 0, 0),
1076         SENSOR_ATTR_2(fan%d_min, 0644, show_bank2_setting,
1077                 store_bank2_setting, 1, 0),
1078         SENSOR_ATTR_2(fan%d_beep, 0644, show_bank2_mask,
1079                 store_bank2_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1080         SENSOR_ATTR_2(fan%d_shutdown, 0644, show_bank2_mask,
1081                 store_bank2_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1082         SENSOR_ATTR_2(fan%d_alarm_enable, 0644, show_bank2_mask,
1083                 store_bank2_mask, ABIT_UGURU_FAN_LOW_ALARM_ENABLE, 0),
1084 };
1085
1086 static const struct sensor_device_attribute_2 abituguru_sysfs_pwm_templ[6] = {
1087         SENSOR_ATTR_2(pwm%d_enable, 0644, show_pwm_enable,
1088                 store_pwm_enable, 0, 0),
1089         SENSOR_ATTR_2(pwm%d_auto_channels_temp, 0644, show_pwm_sensor,
1090                 store_pwm_sensor, 0, 0),
1091         SENSOR_ATTR_2(pwm%d_auto_point1_pwm, 0644, show_pwm_setting,
1092                 store_pwm_setting, 1, 0),
1093         SENSOR_ATTR_2(pwm%d_auto_point2_pwm, 0644, show_pwm_setting,
1094                 store_pwm_setting, 2, 0),
1095         SENSOR_ATTR_2(pwm%d_auto_point1_temp, 0644, show_pwm_setting,
1096                 store_pwm_setting, 3, 0),
1097         SENSOR_ATTR_2(pwm%d_auto_point2_temp, 0644, show_pwm_setting,
1098                 store_pwm_setting, 4, 0),
1099 };
1100
1101 static struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
1102         SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
1103 };
1104
1105 static int __devinit abituguru_probe(struct platform_device *pdev)
1106 {
1107         struct abituguru_data *data;
1108         int i, j, used, sysfs_names_free, sysfs_attr_i, res = -ENODEV;
1109         char *sysfs_filename;
1110
1111         /* El weirdo probe order, to keep the sysfs order identical to the
1112            BIOS and window-appliction listing order. */
1113         const u8 probe_order[ABIT_UGURU_MAX_BANK1_SENSORS] = {
1114                 0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E, 0x02,
1115                 0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
1116
1117         if (!(data = kzalloc(sizeof(struct abituguru_data), GFP_KERNEL)))
1118                 return -ENOMEM;
1119
1120         data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
1121         mutex_init(&data->update_lock);
1122         platform_set_drvdata(pdev, data);
1123
1124         /* See if the uGuru is ready */
1125         if (inb_p(data->addr + ABIT_UGURU_DATA) == ABIT_UGURU_STATUS_INPUT)
1126                 data->uguru_ready = 1;
1127
1128         /* Completely read the uGuru this has 2 purposes:
1129            - testread / see if one really is there.
1130            - make an in memory copy of all the uguru settings for future use. */
1131         if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1132                         data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3)
1133                 goto abituguru_probe_error;
1134
1135         for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1136                 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, i,
1137                                 &data->bank1_value[i], 1,
1138                                 ABIT_UGURU_MAX_RETRIES) != 1)
1139                         goto abituguru_probe_error;
1140                 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1+1, i,
1141                                 data->bank1_settings[i], 3,
1142                                 ABIT_UGURU_MAX_RETRIES) != 3)
1143                         goto abituguru_probe_error;
1144         }
1145         /* Note: We don't know how many bank2 sensors / pwms there really are,
1146            but in order to "detect" this we need to read the maximum amount
1147            anyways. If we read sensors/pwms not there we'll just read crap
1148            this can't hurt. We need the detection because we don't want
1149            unwanted writes, which will hurt! */
1150         for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
1151                 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
1152                                 &data->bank2_value[i], 1,
1153                                 ABIT_UGURU_MAX_RETRIES) != 1)
1154                         goto abituguru_probe_error;
1155                 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2+1, i,
1156                                 data->bank2_settings[i], 2,
1157                                 ABIT_UGURU_MAX_RETRIES) != 2)
1158                         goto abituguru_probe_error;
1159         }
1160         for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
1161                 if (abituguru_read(data, ABIT_UGURU_FAN_PWM, i,
1162                                 data->pwm_settings[i], 5,
1163                                 ABIT_UGURU_MAX_RETRIES) != 5)
1164                         goto abituguru_probe_error;
1165         }
1166         data->last_updated = jiffies;
1167
1168         /* Detect sensor types and fill the sysfs attr for bank1 */
1169         sysfs_attr_i = 0;
1170         sysfs_filename = data->sysfs_names;
1171         sysfs_names_free = ABITUGURU_SYSFS_NAMES_LENGTH;
1172         for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1173                 res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
1174                 if (res < 0)
1175                         goto abituguru_probe_error;
1176                 if (res == ABIT_UGURU_NC)
1177                         continue;
1178
1179                 /* res 1 (temp) sensors have 7 sysfs entries, 0 (in) 9 */
1180                 for (j = 0; j < (res ? 7 : 9); j++) {
1181                         used = snprintf(sysfs_filename, sysfs_names_free,
1182                                 abituguru_sysfs_bank1_templ[res][j].dev_attr.
1183                                 attr.name, data->bank1_sensors[res] + res)
1184                                 + 1;
1185                         data->sysfs_attr[sysfs_attr_i] =
1186                                 abituguru_sysfs_bank1_templ[res][j];
1187                         data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1188                                 sysfs_filename;
1189                         data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
1190                         sysfs_filename += used;
1191                         sysfs_names_free -= used;
1192                         sysfs_attr_i++;
1193                 }
1194                 data->bank1_max_value[probe_order[i]] =
1195                         abituguru_bank1_max_value[res];
1196                 data->bank1_address[res][data->bank1_sensors[res]] =
1197                         probe_order[i];
1198                 data->bank1_sensors[res]++;
1199         }
1200         /* Detect number of sensors and fill the sysfs attr for bank2 (fans) */
1201         abituguru_detect_no_bank2_sensors(data);
1202         for (i = 0; i < data->bank2_sensors; i++) {
1203                 for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_fan_templ); j++) {
1204                         used = snprintf(sysfs_filename, sysfs_names_free,
1205                                 abituguru_sysfs_fan_templ[j].dev_attr.attr.name,
1206                                 i + 1) + 1;
1207                         data->sysfs_attr[sysfs_attr_i] =
1208                                 abituguru_sysfs_fan_templ[j];
1209                         data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1210                                 sysfs_filename;
1211                         data->sysfs_attr[sysfs_attr_i].index = i;
1212                         sysfs_filename += used;
1213                         sysfs_names_free -= used;
1214                         sysfs_attr_i++;
1215                 }
1216         }
1217         /* Detect number of sensors and fill the sysfs attr for pwms */
1218         abituguru_detect_no_pwms(data);
1219         for (i = 0; i < data->pwms; i++) {
1220                 for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_pwm_templ); j++) {
1221                         used = snprintf(sysfs_filename, sysfs_names_free,
1222                                 abituguru_sysfs_pwm_templ[j].dev_attr.attr.name,
1223                                 i + 1) + 1;
1224                         data->sysfs_attr[sysfs_attr_i] =
1225                                 abituguru_sysfs_pwm_templ[j];
1226                         data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1227                                 sysfs_filename;
1228                         data->sysfs_attr[sysfs_attr_i].index = i;
1229                         sysfs_filename += used;
1230                         sysfs_names_free -= used;
1231                         sysfs_attr_i++;
1232                 }
1233         }
1234         /* Fail safe check, this should never happen! */
1235         if (sysfs_names_free < 0) {
1236                 printk(KERN_ERR ABIT_UGURU_NAME ": Fatal error ran out of "
1237                        "space for sysfs attr names. This should never "
1238                        "happen please report to the abituguru maintainer "
1239                        "(see MAINTAINERS)\n");
1240                 res = -ENAMETOOLONG;
1241                 goto abituguru_probe_error;
1242         }
1243         printk(KERN_INFO ABIT_UGURU_NAME ": found Abit uGuru\n");
1244
1245         /* Register sysfs hooks */
1246         data->class_dev = hwmon_device_register(&pdev->dev);
1247         if (IS_ERR(data->class_dev)) {
1248                 res = PTR_ERR(data->class_dev);
1249                 goto abituguru_probe_error;
1250         }
1251         for (i = 0; i < sysfs_attr_i; i++)
1252                 device_create_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1253         for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1254                 device_create_file(&pdev->dev,
1255                         &abituguru_sysfs_attr[i].dev_attr);
1256
1257         return 0;
1258
1259 abituguru_probe_error:
1260         kfree(data);
1261         return res;
1262 }
1263
1264 static int __devexit abituguru_remove(struct platform_device *pdev)
1265 {
1266         struct abituguru_data *data = platform_get_drvdata(pdev);
1267
1268         platform_set_drvdata(pdev, NULL);
1269         hwmon_device_unregister(data->class_dev);
1270         kfree(data);
1271
1272         return 0;
1273 }
1274
1275 static struct abituguru_data *abituguru_update_device(struct device *dev)
1276 {
1277         int i, err;
1278         struct abituguru_data *data = dev_get_drvdata(dev);
1279         /* fake a complete successful read if no update necessary. */
1280         char success = 1;
1281
1282         mutex_lock(&data->update_lock);
1283         if (time_after(jiffies, data->last_updated + HZ)) {
1284                 success = 0;
1285                 if ((err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1286                                 data->alarms, 3, 0)) != 3)
1287                         goto LEAVE_UPDATE;
1288                 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1289                         if ((err = abituguru_read(data,
1290                                         ABIT_UGURU_SENSOR_BANK1, i,
1291                                         &data->bank1_value[i], 1, 0)) != 1)
1292                                 goto LEAVE_UPDATE;
1293                         if ((err = abituguru_read(data,
1294                                         ABIT_UGURU_SENSOR_BANK1 + 1, i,
1295                                         data->bank1_settings[i], 3, 0)) != 3)
1296                                 goto LEAVE_UPDATE;
1297                 }
1298                 for (i = 0; i < data->bank2_sensors; i++)
1299                         if ((err = abituguru_read(data,
1300                                         ABIT_UGURU_SENSOR_BANK2, i,
1301                                         &data->bank2_value[i], 1, 0)) != 1)
1302                                 goto LEAVE_UPDATE;
1303                 /* success! */
1304                 success = 1;
1305                 data->update_timeouts = 0;
1306 LEAVE_UPDATE:
1307                 /* handle timeout condition */
1308                 if (err == -EBUSY) {
1309                         /* No overflow please */
1310                         if (data->update_timeouts < 255u)
1311                                 data->update_timeouts++;
1312                         if (data->update_timeouts <= ABIT_UGURU_MAX_TIMEOUTS) {
1313                                 ABIT_UGURU_DEBUG(3, "timeout exceeded, will "
1314                                         "try again next update\n");
1315                                 /* Just a timeout, fake a successful read */
1316                                 success = 1;
1317                         } else
1318                                 ABIT_UGURU_DEBUG(1, "timeout exceeded %d "
1319                                         "times waiting for more input state\n",
1320                                         (int)data->update_timeouts);
1321                 }
1322                 /* On success set last_updated */
1323                 if (success)
1324                         data->last_updated = jiffies;
1325         }
1326         mutex_unlock(&data->update_lock);
1327
1328         if (success)
1329                 return data;
1330         else
1331                 return NULL;
1332 }
1333
1334 static struct platform_driver abituguru_driver = {
1335         .driver = {
1336                 .owner  = THIS_MODULE,
1337                 .name   = ABIT_UGURU_NAME,
1338         },
1339         .probe  = abituguru_probe,
1340         .remove = __devexit_p(abituguru_remove),
1341 };
1342
1343 static int __init abituguru_detect(void)
1344 {
1345         /* See if there is an uguru there. After a reboot uGuru will hold 0x00
1346            at DATA and 0xAC, when this driver has already been loaded once
1347            DATA will hold 0x08. For most uGuru's CMD will hold 0xAC in either
1348            scenario but some will hold 0x00.
1349            Some uGuru's initally hold 0x09 at DATA and will only hold 0x08
1350            after reading CMD first, so CMD must be read first! */
1351         u8 cmd_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_CMD);
1352         u8 data_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_DATA);
1353         if (((data_val == 0x00) || (data_val == 0x08)) &&
1354             ((cmd_val == 0x00) || (cmd_val == 0xAC)))
1355                 return ABIT_UGURU_BASE;
1356
1357         ABIT_UGURU_DEBUG(2, "no Abit uGuru found, data = 0x%02X, cmd = "
1358                 "0x%02X\n", (unsigned int)data_val, (unsigned int)cmd_val);
1359
1360         if (force) {
1361                 printk(KERN_INFO ABIT_UGURU_NAME ": Assuming Abit uGuru is "
1362                                 "present because of \"force\" parameter\n");
1363                 return ABIT_UGURU_BASE;
1364         }
1365
1366         /* No uGuru found */
1367         return -ENODEV;
1368 }
1369
1370 static struct platform_device *abituguru_pdev;
1371
1372 static int __init abituguru_init(void)
1373 {
1374         int address, err;
1375         struct resource res = { .flags = IORESOURCE_IO };
1376
1377         address = abituguru_detect();
1378         if (address < 0)
1379                 return address;
1380
1381         err = platform_driver_register(&abituguru_driver);
1382         if (err)
1383                 goto exit;
1384
1385         abituguru_pdev = platform_device_alloc(ABIT_UGURU_NAME, address);
1386         if (!abituguru_pdev) {
1387                 printk(KERN_ERR ABIT_UGURU_NAME
1388                         ": Device allocation failed\n");
1389                 err = -ENOMEM;
1390                 goto exit_driver_unregister;
1391         }
1392
1393         res.start = address;
1394         res.end = address + ABIT_UGURU_REGION_LENGTH - 1;
1395         res.name = ABIT_UGURU_NAME;
1396
1397         err = platform_device_add_resources(abituguru_pdev, &res, 1);
1398         if (err) {
1399                 printk(KERN_ERR ABIT_UGURU_NAME
1400                         ": Device resource addition failed (%d)\n", err);
1401                 goto exit_device_put;
1402         }
1403
1404         err = platform_device_add(abituguru_pdev);
1405         if (err) {
1406                 printk(KERN_ERR ABIT_UGURU_NAME
1407                         ": Device addition failed (%d)\n", err);
1408                 goto exit_device_put;
1409         }
1410
1411         return 0;
1412
1413 exit_device_put:
1414         platform_device_put(abituguru_pdev);
1415 exit_driver_unregister:
1416         platform_driver_unregister(&abituguru_driver);
1417 exit:
1418         return err;
1419 }
1420
1421 static void __exit abituguru_exit(void)
1422 {
1423         platform_device_unregister(abituguru_pdev);
1424         platform_driver_unregister(&abituguru_driver);
1425 }
1426
1427 MODULE_AUTHOR("Hans de Goede <j.w.r.degoede@hhs.nl>");
1428 MODULE_DESCRIPTION("Abit uGuru Sensor device");
1429 MODULE_LICENSE("GPL");
1430
1431 module_init(abituguru_init);
1432 module_exit(abituguru_exit);