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 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"
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 "
158 module_param(pwms, int, 0);
159 MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru "
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"
167 " 1 + verbose error reporting\n"
168 " 2 + sensors type probing info\n"
169 " 3 + retryable error reporting");
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
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];
196 /* number of and addresses of [0] in, [1] temp sensors */
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
205 int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS];
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 */
212 /* Alarms 2 bytes for bank1, 1 byte for bank2 */
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];
220 /* wait till the uguru is in the specified state */
221 static int abituguru_wait(struct abituguru_data *data, u8 state)
223 int timeout = ABIT_UGURU_WAIT_TIMEOUT;
225 while (inb_p(data->addr + ABIT_UGURU_DATA) != state) {
233 /* Put the uguru in ready for input state */
234 static int abituguru_ready(struct abituguru_data *data)
236 int timeout = ABIT_UGURU_READY_TIMEOUT;
238 if (data->uguru_ready)
241 /* Reset? / Prepare for next read/write cycle */
242 outb(0x00, data->addr + ABIT_UGURU_DATA);
244 /* Wait till the uguru is ready */
245 if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) {
247 "timeout exceeded waiting for ready state\n");
251 /* Cmd port MUST be read now and should contain 0xAC */
252 while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
256 "CMD reg does not hold 0xAC after ready command\n");
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) {
268 "state != more input after ready command\n");
273 data->uguru_ready = 1;
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)
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;
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)
293 outb(bank_addr, data->addr + ABIT_UGURU_DATA);
294 data->uguru_ready = 0;
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)) {
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);
309 ABIT_UGURU_DEBUG(1, "timeout exceeded "
310 "waiting for more input state "
311 "(bank: %d)\n", (int)bank_addr);
314 outb(sensor_addr, data->addr + ABIT_UGURU_CMD);
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)
326 /* Send the address */
327 i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
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);
339 buf[i] = inb(data->addr + ABIT_UGURU_CMD);
342 /* Last put the chip back in ready state */
343 abituguru_ready(data);
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)
355 /* Send the address */
356 i = abituguru_send_address(data, bank_addr, sensor_addr,
357 ABIT_UGURU_MAX_RETRIES);
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);
369 outb(buf[i], data->addr + ABIT_UGURU_CMD);
372 /* Now we need to wait till the chip is ready to be read again,
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,
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,
389 /* Last put the chip back in ready state */
390 abituguru_ready(data);
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. */
402 abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
406 int ret = ABIT_UGURU_NC;
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];
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)
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;
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;
440 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
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)
451 if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
452 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
454 ABIT_UGURU_MAX_RETRIES) != 3)
456 if (buf[0] & ABIT_UGURU_VOLT_LOW_ALARM_FLAG) {
457 /* Restore original settings */
458 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
460 data->bank1_settings[sensor_addr],
463 ABIT_UGURU_DEBUG(2, " found volt sensor\n");
464 return ABIT_UGURU_IN_SENSOR;
466 ABIT_UGURU_DEBUG(2, " alarm raised during volt "
467 "sensor test, but volt low flag not set\n");
469 ABIT_UGURU_DEBUG(2, " alarm not raised during volt sensor "
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;
478 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
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)
489 if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
490 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
492 ABIT_UGURU_MAX_RETRIES) != 3)
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");
498 ABIT_UGURU_DEBUG(2, " alarm raised during temp "
499 "sensor test, but temp high flag not set\n");
501 ABIT_UGURU_DEBUG(2, " alarm not raised during temp sensor "
504 /* Restore original settings */
505 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
506 data->bank1_settings[sensor_addr], 3) != 3)
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.
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"
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)
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);
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
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]);
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);
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);
578 data->bank2_sensors = i;
579 ABIT_UGURU_DEBUG(2, " found: %d fan sensors\n",
580 (int)data->bank2_sensors);
583 static void __devinit
584 abituguru_detect_no_pwms(struct abituguru_data *data)
588 if (pwms > 0 && pwms <= ABIT_UGURU_MAX_PWMS) {
590 ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
591 "\"pwms\" module param\n", (int)data->pwms);
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]);
607 /* the low nibble must correspond to one of the temp sensors
609 for (j = 0; j < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR];
611 if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][j] ==
612 (data->pwm_settings[i][0] & 0x0F))
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);
623 /* check if all other settings are within the allowed range */
624 for (j = 1; j < 5; j++) {
626 /* special case pwm1 min pwm% */
627 if ((i == 0) && ((j == 1) || (j == 2)))
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],
638 goto abituguru_detect_no_pwms_exit;
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;
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) >= "
656 (int)data->pwm_settings[i][1],
657 (int)data->pwm_settings[i][2]);
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]);
670 abituguru_detect_no_pwms_exit:
672 ABIT_UGURU_DEBUG(2, " found: %d PWM outputs\n", (int)data->pwms);
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);
680 static ssize_t show_bank1_value(struct device *dev,
681 struct device_attribute *devattr, char *buf)
683 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
684 struct abituguru_data *data = abituguru_update_device(dev);
687 return sprintf(buf, "%d\n", (data->bank1_value[attr->index] *
688 data->bank1_max_value[attr->index] + 128) / 255);
691 static ssize_t show_bank1_setting(struct device *dev,
692 struct device_attribute *devattr, char *buf)
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);
701 static ssize_t show_bank2_value(struct device *dev,
702 struct device_attribute *devattr, char *buf)
704 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
705 struct abituguru_data *data = abituguru_update_device(dev);
708 return sprintf(buf, "%d\n", (data->bank2_value[attr->index] *
709 ABIT_UGURU_FAN_MAX + 128) / 255);
712 static ssize_t show_bank2_setting(struct device *dev,
713 struct device_attribute *devattr, char *buf)
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);
722 static ssize_t store_bank1_setting(struct device *dev, struct device_attribute
723 *devattr, const char *buf, size_t count)
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];
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],
739 data->bank1_settings[attr->index][attr->nr] = orig_val;
743 mutex_unlock(&data->update_lock);
747 static ssize_t store_bank2_setting(struct device *dev, struct device_attribute
748 *devattr, const char *buf, size_t count)
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) /
756 /* this check can be done before taking the lock */
757 if ((val < abituguru_bank2_min_threshold) ||
758 (val > abituguru_bank2_max_threshold))
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],
768 data->bank2_settings[attr->index][attr->nr] = orig_val;
772 mutex_unlock(&data->update_lock);
776 static ssize_t show_bank1_alarm(struct device *dev,
777 struct device_attribute *devattr, char *buf)
779 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
780 struct abituguru_data *data = abituguru_update_device(dev);
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");
792 return sprintf(buf, "0\n");
795 static ssize_t show_bank2_alarm(struct device *dev,
796 struct device_attribute *devattr, char *buf)
798 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
799 struct abituguru_data *data = abituguru_update_device(dev);
802 if (data->alarms[2] & (0x01 << attr->index))
803 return sprintf(buf, "1\n");
805 return sprintf(buf, "0\n");
808 static ssize_t show_bank1_mask(struct device *dev,
809 struct device_attribute *devattr, char *buf)
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");
816 return sprintf(buf, "0\n");
819 static ssize_t show_bank2_mask(struct device *dev,
820 struct device_attribute *devattr, char *buf)
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");
827 return sprintf(buf, "0\n");
830 static ssize_t store_bank1_mask(struct device *dev,
831 struct device_attribute *devattr, const char *buf, size_t count)
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);
839 mutex_lock(&data->update_lock);
840 orig_val = data->bank1_settings[attr->index][0];
843 data->bank1_settings[attr->index][0] |= attr->nr;
845 data->bank1_settings[attr->index][0] &= ~attr->nr;
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;
854 mutex_unlock(&data->update_lock);
858 static ssize_t store_bank2_mask(struct device *dev,
859 struct device_attribute *devattr, const char *buf, size_t count)
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);
867 mutex_lock(&data->update_lock);
868 orig_val = data->bank2_settings[attr->index][0];
871 data->bank2_settings[attr->index][0] |= attr->nr;
873 data->bank2_settings[attr->index][0] &= ~attr->nr;
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;
882 mutex_unlock(&data->update_lock);
886 /* Fan PWM (speed control) */
887 static ssize_t show_pwm_setting(struct device *dev,
888 struct device_attribute *devattr, char *buf)
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]);
896 static ssize_t store_pwm_setting(struct device *dev, struct device_attribute
897 *devattr, const char *buf, size_t count)
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];
906 /* special case pwm1 min pwm% */
907 if ((attr->index == 0) && ((attr->nr == 1) || (attr->nr == 2)))
910 min = abituguru_pwm_min[attr->nr];
912 /* this check can be done before taking the lock */
913 if ((val < min) || (val > abituguru_pwm_max[attr->nr]))
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]))
921 else if (!(attr->nr & 1) &&
922 (val <= data->pwm_settings[attr->index][attr->nr - 1]))
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],
930 data->pwm_settings[attr->index][attr->nr] =
935 mutex_unlock(&data->update_lock);
939 static ssize_t show_pwm_sensor(struct device *dev,
940 struct device_attribute *devattr, char *buf)
942 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
943 struct abituguru_data *data = dev_get_drvdata(dev);
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);
955 static ssize_t store_pwm_sensor(struct device *dev, struct device_attribute
956 *devattr, const char *buf, size_t count)
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;
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,
972 data->pwm_settings[attr->index],
974 data->pwm_settings[attr->index][0] = orig_val;
981 mutex_unlock(&data->update_lock);
985 static ssize_t show_pwm_enable(struct device *dev,
986 struct device_attribute *devattr, char *buf)
988 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
989 struct abituguru_data *data = dev_get_drvdata(dev);
991 if (data->pwm_settings[attr->index][0] & ABIT_UGURU_FAN_PWM_ENABLE)
993 return sprintf(buf, "%d\n", res);
996 static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
997 *devattr, const char *buf, size_t count)
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;
1004 mutex_lock(&data->update_lock);
1005 orig_val = data->pwm_settings[attr->index][0];
1008 data->pwm_settings[attr->index][0] &=
1009 ~ABIT_UGURU_FAN_PWM_ENABLE;
1012 data->pwm_settings[attr->index][0] |=
1013 ABIT_UGURU_FAN_PWM_ENABLE;
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],
1022 data->pwm_settings[attr->index][0] = orig_val;
1025 mutex_unlock(&data->update_lock);
1029 static ssize_t show_name(struct device *dev,
1030 struct device_attribute *devattr, char *buf)
1032 return sprintf(buf, "%s\n", ABIT_UGURU_NAME);
1035 /* Sysfs attr templates, the real entries are generated automatically. */
1037 struct sensor_device_attribute_2 abituguru_sysfs_bank1_templ[2][9] = {
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),
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),
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),
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),
1101 static struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
1102 SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
1105 static int __devinit abituguru_probe(struct platform_device *pdev)
1107 struct abituguru_data *data;
1108 int i, j, used, sysfs_names_free, sysfs_attr_i, res = -ENODEV;
1109 char *sysfs_filename;
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 };
1117 if (!(data = kzalloc(sizeof(struct abituguru_data), GFP_KERNEL)))
1120 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
1121 mutex_init(&data->update_lock);
1122 platform_set_drvdata(pdev, data);
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;
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;
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;
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;
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;
1166 data->last_updated = jiffies;
1168 /* Detect sensor types and fill the sysfs attr for bank1 */
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]);
1175 goto abituguru_probe_error;
1176 if (res == ABIT_UGURU_NC)
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)
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 =
1189 data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
1190 sysfs_filename += used;
1191 sysfs_names_free -= used;
1194 data->bank1_max_value[probe_order[i]] =
1195 abituguru_bank1_max_value[res];
1196 data->bank1_address[res][data->bank1_sensors[res]] =
1198 data->bank1_sensors[res]++;
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,
1207 data->sysfs_attr[sysfs_attr_i] =
1208 abituguru_sysfs_fan_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 /* 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,
1224 data->sysfs_attr[sysfs_attr_i] =
1225 abituguru_sysfs_pwm_templ[j];
1226 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1228 data->sysfs_attr[sysfs_attr_i].index = i;
1229 sysfs_filename += used;
1230 sysfs_names_free -= used;
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;
1243 printk(KERN_INFO ABIT_UGURU_NAME ": found Abit uGuru\n");
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;
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);
1259 abituguru_probe_error:
1264 static int __devexit abituguru_remove(struct platform_device *pdev)
1266 struct abituguru_data *data = platform_get_drvdata(pdev);
1268 platform_set_drvdata(pdev, NULL);
1269 hwmon_device_unregister(data->class_dev);
1275 static struct abituguru_data *abituguru_update_device(struct device *dev)
1278 struct abituguru_data *data = dev_get_drvdata(dev);
1279 /* fake a complete successful read if no update necessary. */
1282 mutex_lock(&data->update_lock);
1283 if (time_after(jiffies, data->last_updated + HZ)) {
1285 if ((err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1286 data->alarms, 3, 0)) != 3)
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)
1293 if ((err = abituguru_read(data,
1294 ABIT_UGURU_SENSOR_BANK1 + 1, i,
1295 data->bank1_settings[i], 3, 0)) != 3)
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)
1305 data->update_timeouts = 0;
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 */
1318 ABIT_UGURU_DEBUG(1, "timeout exceeded %d "
1319 "times waiting for more input state\n",
1320 (int)data->update_timeouts);
1322 /* On success set last_updated */
1324 data->last_updated = jiffies;
1326 mutex_unlock(&data->update_lock);
1334 static struct platform_driver abituguru_driver = {
1336 .owner = THIS_MODULE,
1337 .name = ABIT_UGURU_NAME,
1339 .probe = abituguru_probe,
1340 .remove = __devexit_p(abituguru_remove),
1343 static int __init abituguru_detect(void)
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;
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);
1361 printk(KERN_INFO ABIT_UGURU_NAME ": Assuming Abit uGuru is "
1362 "present because of \"force\" parameter\n");
1363 return ABIT_UGURU_BASE;
1366 /* No uGuru found */
1370 static struct platform_device *abituguru_pdev;
1372 static int __init abituguru_init(void)
1375 struct resource res = { .flags = IORESOURCE_IO };
1377 address = abituguru_detect();
1381 err = platform_driver_register(&abituguru_driver);
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");
1390 goto exit_driver_unregister;
1393 res.start = address;
1394 res.end = address + ABIT_UGURU_REGION_LENGTH - 1;
1395 res.name = ABIT_UGURU_NAME;
1397 err = platform_device_add_resources(abituguru_pdev, &res, 1);
1399 printk(KERN_ERR ABIT_UGURU_NAME
1400 ": Device resource addition failed (%d)\n", err);
1401 goto exit_device_put;
1404 err = platform_device_add(abituguru_pdev);
1406 printk(KERN_ERR ABIT_UGURU_NAME
1407 ": Device addition failed (%d)\n", err);
1408 goto exit_device_put;
1414 platform_device_put(abituguru_pdev);
1415 exit_driver_unregister:
1416 platform_driver_unregister(&abituguru_driver);
1421 static void __exit abituguru_exit(void)
1423 platform_device_unregister(abituguru_pdev);
1424 platform_driver_unregister(&abituguru_driver);
1427 MODULE_AUTHOR("Hans de Goede <j.w.r.degoede@hhs.nl>");
1428 MODULE_DESCRIPTION("Abit uGuru Sensor device");
1429 MODULE_LICENSE("GPL");
1431 module_init(abituguru_init);
1432 module_exit(abituguru_exit);