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 bank2, currently mb's with max 6 fans are known */
40 #define ABIT_UGURU_MAX_BANK2_SENSORS 6
41 /* max nr of pwm outputs, currently mb's with max 5 pwm outputs are known */
42 #define ABIT_UGURU_MAX_PWMS 5
43 /* uGuru sensor bank 1 flags */ /* Alarm if: */
44 #define ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE 0x01 /* temp over warn */
45 #define ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE 0x02 /* volt over max */
46 #define ABIT_UGURU_VOLT_LOW_ALARM_ENABLE 0x04 /* volt under min */
47 #define ABIT_UGURU_TEMP_HIGH_ALARM_FLAG 0x10 /* temp is over warn */
48 #define ABIT_UGURU_VOLT_HIGH_ALARM_FLAG 0x20 /* volt is over max */
49 #define ABIT_UGURU_VOLT_LOW_ALARM_FLAG 0x40 /* volt is under min */
50 /* uGuru sensor bank 2 flags */ /* Alarm if: */
51 #define ABIT_UGURU_FAN_LOW_ALARM_ENABLE 0x01 /* fan under min */
52 /* uGuru sensor bank common flags */
53 #define ABIT_UGURU_BEEP_ENABLE 0x08 /* beep if alarm */
54 #define ABIT_UGURU_SHUTDOWN_ENABLE 0x80 /* shutdown if alarm */
55 /* uGuru fan PWM (speed control) flags */
56 #define ABIT_UGURU_FAN_PWM_ENABLE 0x80 /* enable speed control */
57 /* Values used for conversion */
58 #define ABIT_UGURU_FAN_MAX 15300 /* RPM */
59 /* Bank1 sensor types */
60 #define ABIT_UGURU_IN_SENSOR 0
61 #define ABIT_UGURU_TEMP_SENSOR 1
62 #define ABIT_UGURU_NC 2
63 /* Timeouts / Retries, if these turn out to need a lot of fiddling we could
64 convert them to params. */
65 /* 250 was determined by trial and error, 200 works most of the time, but not
66 always. I assume this is cpu-speed independent, since the ISA-bus and not
67 the CPU should be the bottleneck. Note that 250 sometimes is still not
68 enough (only reported on AN7 mb) this is handled by a higher layer. */
69 #define ABIT_UGURU_WAIT_TIMEOUT 250
70 /* Normally all expected status in abituguru_ready, are reported after the
71 first read, but sometimes not and we need to poll, 5 polls was not enough
73 #define ABIT_UGURU_READY_TIMEOUT 50
74 /* Maximum 3 retries on timedout reads/writes, delay 200 ms before retrying */
75 #define ABIT_UGURU_MAX_RETRIES 3
76 #define ABIT_UGURU_RETRY_DELAY (HZ/5)
77 /* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is a error */
78 #define ABIT_UGURU_MAX_TIMEOUTS 2
80 /* All the variables below are named identical to the oguru and oguru2 programs
81 reverse engineered by Olle Sandberg, hence the names might not be 100%
82 logical. I could come up with better names, but I prefer keeping the names
83 identical so that this driver can be compared with his work more easily. */
84 /* Two i/o-ports are used by uGuru */
85 #define ABIT_UGURU_BASE 0x00E0
86 /* Used to tell uGuru what to read and to read the actual data */
87 #define ABIT_UGURU_CMD 0x00
88 /* Mostly used to check if uGuru is busy */
89 #define ABIT_UGURU_DATA 0x04
90 #define ABIT_UGURU_REGION_LENGTH 5
92 #define ABIT_UGURU_STATUS_WRITE 0x00 /* Ready to be written */
93 #define ABIT_UGURU_STATUS_READ 0x01 /* Ready to be read */
94 #define ABIT_UGURU_STATUS_INPUT 0x08 /* More input */
95 #define ABIT_UGURU_STATUS_READY 0x09 /* Ready to be written */
97 #define ABIT_UGURU_NAME "abituguru"
98 #define ABIT_UGURU_DEBUG(level, format, arg...) \
99 if (level <= verbose) \
100 printk(KERN_DEBUG ABIT_UGURU_NAME ": " format , ## arg)
103 /* in (Volt) sensors go up to 3494 mV, temp to 255000 millidegrees Celsius */
104 static const int abituguru_bank1_max_value[2] = { 3494, 255000 };
105 /* Min / Max allowed values for sensor2 (fan) alarm threshold, these values
106 correspond to 300-3000 RPM */
107 static const u8 abituguru_bank2_min_threshold = 5;
108 static const u8 abituguru_bank2_max_threshold = 50;
109 /* Register 0 is a bitfield, 1 and 2 are pwm settings (255 = 100%), 3 and 4
110 are temperature trip points. */
111 static const int abituguru_pwm_settings_multiplier[5] = { 0, 1, 1, 1000, 1000 };
112 /* Min / Max allowed values for pwm_settings. Note: pwm1 (CPU fan) is a
113 special case the minium allowed pwm% setting for this is 30% (77) on
114 some MB's this special case is handled in the code! */
115 static const u8 abituguru_pwm_min[5] = { 0, 170, 170, 25, 25 };
116 static const u8 abituguru_pwm_max[5] = { 0, 255, 255, 75, 75 };
119 /* Insmod parameters */
121 module_param(force, bool, 0);
122 MODULE_PARM_DESC(force, "Set to one to force detection.");
123 static int fan_sensors;
124 module_param(fan_sensors, int, 0);
125 MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru "
128 module_param(pwms, int, 0);
129 MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru "
132 /* Default verbose is 2, since this driver is still in the testing phase */
133 static int verbose = 2;
134 module_param(verbose, int, 0644);
135 MODULE_PARM_DESC(verbose, "How verbose should the driver be? (0-3):\n"
137 " 1 + verbose error reporting\n"
138 " 2 + sensors type probing info\n"
139 " 3 + retryable error reporting");
142 /* For the Abit uGuru, we need to keep some data in memory.
143 The structure is dynamically allocated, at the same time when a new
144 abituguru device is allocated. */
145 struct abituguru_data {
146 struct class_device *class_dev; /* hwmon registered device */
147 struct mutex update_lock; /* protect access to data and uGuru */
148 unsigned long last_updated; /* In jiffies */
149 unsigned short addr; /* uguru base address */
150 char uguru_ready; /* is the uguru in ready state? */
151 unsigned char update_timeouts; /* number of update timeouts since last
154 /* The sysfs attr and their names are generated automatically, for bank1
155 we cannot use a predefined array because we don't know beforehand
156 of a sensor is a volt or a temp sensor, for bank2 and the pwms its
157 easier todo things the same way. For in sensors we have 9 (temp 7)
158 sysfs entries per sensor, for bank2 and pwms 6. */
159 struct sensor_device_attribute_2 sysfs_attr[16 * 9 +
160 ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6];
161 /* Buffer to store the dynamically generated sysfs names, we need 2120
162 bytes for bank1 (worst case scenario of 16 in sensors), 444 bytes
163 for fan1-6 and 738 bytes for pwm1-6 + some room to spare in case I
165 char bank1_names[3400];
168 u8 bank1_sensors[2]; /* number of [0] in, [1] temp sensors */
169 u8 bank1_address[2][16];/* addresses of [0] in, [1] temp sensors */
171 /* This array holds 16 x 3 entries for all the bank 1 sensor settings
172 (flags, min, max for voltage / flags, warn, shutdown for temp). */
173 u8 bank1_settings[16][3];
174 /* Maximum value for each sensor used for scaling in mV/millidegrees
176 int bank1_max_value[16];
178 /* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */
179 u8 bank2_sensors; /* actual number of bank2 sensors found */
180 u8 bank2_value[ABIT_UGURU_MAX_BANK2_SENSORS];
181 u8 bank2_settings[ABIT_UGURU_MAX_BANK2_SENSORS][2]; /* flags, min */
183 /* Alarms 2 bytes for bank1, 1 byte for bank2 */
186 /* Fan PWM (speed control) 5 bytes per PWM */
187 u8 pwms; /* actual number of pwms found */
188 u8 pwm_settings[ABIT_UGURU_MAX_PWMS][5];
191 /* wait till the uguru is in the specified state */
192 static int abituguru_wait(struct abituguru_data *data, u8 state)
194 int timeout = ABIT_UGURU_WAIT_TIMEOUT;
196 while (inb_p(data->addr + ABIT_UGURU_DATA) != state) {
204 /* Put the uguru in ready for input state */
205 static int abituguru_ready(struct abituguru_data *data)
207 int timeout = ABIT_UGURU_READY_TIMEOUT;
209 if (data->uguru_ready)
212 /* Reset? / Prepare for next read/write cycle */
213 outb(0x00, data->addr + ABIT_UGURU_DATA);
215 /* Wait till the uguru is ready */
216 if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) {
218 "timeout exceeded waiting for ready state\n");
222 /* Cmd port MUST be read now and should contain 0xAC */
223 while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
227 "CMD reg does not hold 0xAC after ready command\n");
232 /* After this the ABIT_UGURU_DATA port should contain
233 ABIT_UGURU_STATUS_INPUT */
234 timeout = ABIT_UGURU_READY_TIMEOUT;
235 while (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT) {
239 "state != more input after ready command\n");
244 data->uguru_ready = 1;
248 /* Send the bank and then sensor address to the uGuru for the next read/write
249 cycle. This function gets called as the first part of a read/write by
250 abituguru_read and abituguru_write. This function should never be
251 called by any other function. */
252 static int abituguru_send_address(struct abituguru_data *data,
253 u8 bank_addr, u8 sensor_addr, int retries)
255 /* assume the caller does error handling itself if it has not requested
256 any retries, and thus be quiet. */
257 int report_errors = retries;
260 /* Make sure the uguru is ready and then send the bank address,
261 after this the uguru is no longer "ready". */
262 if (abituguru_ready(data) != 0)
264 outb(bank_addr, data->addr + ABIT_UGURU_DATA);
265 data->uguru_ready = 0;
267 /* Wait till the uguru is ABIT_UGURU_STATUS_INPUT state again
268 and send the sensor addr */
269 if (abituguru_wait(data, ABIT_UGURU_STATUS_INPUT)) {
271 ABIT_UGURU_DEBUG(3, "timeout exceeded "
272 "waiting for more input state, %d "
273 "tries remaining\n", retries);
274 set_current_state(TASK_UNINTERRUPTIBLE);
275 schedule_timeout(ABIT_UGURU_RETRY_DELAY);
280 ABIT_UGURU_DEBUG(1, "timeout exceeded "
281 "waiting for more input state "
282 "(bank: %d)\n", (int)bank_addr);
285 outb(sensor_addr, data->addr + ABIT_UGURU_CMD);
290 /* Read count bytes from sensor sensor_addr in bank bank_addr and store the
291 result in buf, retry the send address part of the read retries times. */
292 static int abituguru_read(struct abituguru_data *data,
293 u8 bank_addr, u8 sensor_addr, u8 *buf, int count, int retries)
297 /* Send the address */
298 i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
302 /* And read the data */
303 for (i = 0; i < count; i++) {
304 if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
305 ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
306 "read state (bank: %d, sensor: %d)\n",
307 (int)bank_addr, (int)sensor_addr);
310 buf[i] = inb(data->addr + ABIT_UGURU_CMD);
313 /* Last put the chip back in ready state */
314 abituguru_ready(data);
319 /* Write count bytes from buf to sensor sensor_addr in bank bank_addr, the send
320 address part of the write is always retried ABIT_UGURU_MAX_RETRIES times. */
321 static int abituguru_write(struct abituguru_data *data,
322 u8 bank_addr, u8 sensor_addr, u8 *buf, int count)
326 /* Send the address */
327 i = abituguru_send_address(data, bank_addr, sensor_addr,
328 ABIT_UGURU_MAX_RETRIES);
332 /* And write the data */
333 for (i = 0; i < count; i++) {
334 if (abituguru_wait(data, ABIT_UGURU_STATUS_WRITE)) {
335 ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
336 "write state (bank: %d, sensor: %d)\n",
337 (int)bank_addr, (int)sensor_addr);
340 outb(buf[i], data->addr + ABIT_UGURU_CMD);
343 /* Now we need to wait till the chip is ready to be read again,
345 if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
346 ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for read state "
347 "after write (bank: %d, sensor: %d)\n", (int)bank_addr,
352 /* Cmd port MUST be read now and should contain 0xAC */
353 if (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
354 ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after write "
355 "(bank: %d, sensor: %d)\n", (int)bank_addr,
360 /* Last put the chip back in ready state */
361 abituguru_ready(data);
366 /* Detect sensor type. Temp and Volt sensors are enabled with
367 different masks and will ignore enable masks not meant for them.
368 This enables us to test what kind of sensor we're dealing with.
369 By setting the alarm thresholds so that we will always get an
370 alarm for sensor type X and then enabling the sensor as sensor type
371 X, if we then get an alarm it is a sensor of type X. */
373 abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
377 int ret = ABIT_UGURU_NC;
379 /* First read the sensor and the current settings */
380 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, sensor_addr, &val,
381 1, ABIT_UGURU_MAX_RETRIES) != 1)
384 /* Test val is sane / usable for sensor type detection. */
385 if ((val < 10u) || (val > 240u)) {
386 printk(KERN_WARNING ABIT_UGURU_NAME
387 ": bank1-sensor: %d reading (%d) too close to limits, "
388 "unable to determine sensor type, skipping sensor\n",
389 (int)sensor_addr, (int)val);
390 /* assume no sensor is there for sensors for which we can't
391 determine the sensor type because their reading is too close
392 to their limits, this usually means no sensor is there. */
393 return ABIT_UGURU_NC;
396 ABIT_UGURU_DEBUG(2, "testing bank1 sensor %d\n", (int)sensor_addr);
397 /* Volt sensor test, enable volt low alarm, set min value ridicously
398 high. If its a volt sensor this should always give us an alarm. */
399 buf[0] = ABIT_UGURU_VOLT_LOW_ALARM_ENABLE;
402 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
405 /* Now we need 20 ms to give the uguru time to read the sensors
406 and raise a voltage alarm */
407 set_current_state(TASK_UNINTERRUPTIBLE);
408 schedule_timeout(HZ/50);
409 /* Check for alarm and check the alarm is a volt low alarm. */
410 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
411 ABIT_UGURU_MAX_RETRIES) != 3)
413 if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
414 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
416 ABIT_UGURU_MAX_RETRIES) != 3)
418 if (buf[0] & ABIT_UGURU_VOLT_LOW_ALARM_FLAG) {
419 /* Restore original settings */
420 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
422 data->bank1_settings[sensor_addr],
425 ABIT_UGURU_DEBUG(2, " found volt sensor\n");
426 return ABIT_UGURU_IN_SENSOR;
428 ABIT_UGURU_DEBUG(2, " alarm raised during volt "
429 "sensor test, but volt low flag not set\n");
431 ABIT_UGURU_DEBUG(2, " alarm not raised during volt sensor "
434 /* Temp sensor test, enable sensor as a temp sensor, set beep value
435 ridicously low (but not too low, otherwise uguru ignores it).
436 If its a temp sensor this should always give us an alarm. */
437 buf[0] = ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE;
440 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
443 /* Now we need 50 ms to give the uguru time to read the sensors
444 and raise a temp alarm */
445 set_current_state(TASK_UNINTERRUPTIBLE);
446 schedule_timeout(HZ/20);
447 /* Check for alarm and check the alarm is a temp high 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_TEMP_HIGH_ALARM_FLAG) {
457 ret = ABIT_UGURU_TEMP_SENSOR;
458 ABIT_UGURU_DEBUG(2, " found temp sensor\n");
460 ABIT_UGURU_DEBUG(2, " alarm raised during temp "
461 "sensor test, but temp high flag not set\n");
463 ABIT_UGURU_DEBUG(2, " alarm not raised during temp sensor "
466 /* Restore original settings */
467 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
468 data->bank1_settings[sensor_addr], 3) != 3)
474 /* These functions try to find out how many sensors there are in bank2 and how
475 many pwms there are. The purpose of this is to make sure that we don't give
476 the user the possibility to change settings for non-existent sensors / pwm.
477 The uGuru will happily read / write whatever memory happens to be after the
478 memory storing the PWM settings when reading/writing to a PWM which is not
479 there. Notice even if we detect a PWM which doesn't exist we normally won't
480 write to it, unless the user tries to change the settings.
482 Although the uGuru allows reading (settings) from non existing bank2
483 sensors, my version of the uGuru does seem to stop writing to them, the
484 write function above aborts in this case with:
485 "CMD reg does not hold 0xAC after write"
487 Notice these 2 tests are non destructive iow read-only tests, otherwise
488 they would defeat their purpose. Although for the bank2_sensors detection a
489 read/write test would be feasible because of the reaction above, I've
490 however opted to stay on the safe side. */
491 static void __devinit
492 abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
497 data->bank2_sensors = fan_sensors;
498 ABIT_UGURU_DEBUG(2, "assuming %d fan sensors because of "
499 "\"fan_sensors\" module param\n",
500 (int)data->bank2_sensors);
504 ABIT_UGURU_DEBUG(2, "detecting number of fan sensors\n");
505 for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
506 /* 0x89 are the known used bits:
507 -0x80 enable shutdown
510 All other bits should be 0, but on some motherboards
511 0x40 (bit 6) is also high, at least for fan1 */
512 if ((!i && (data->bank2_settings[i][0] & ~0xC9)) ||
513 (i && (data->bank2_settings[i][0] & ~0x89))) {
514 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
515 "to be a fan sensor: settings[0] = %02X\n",
516 i, (unsigned int)data->bank2_settings[i][0]);
520 /* check if the threshold is within the allowed range */
521 if (data->bank2_settings[i][1] <
522 abituguru_bank2_min_threshold) {
523 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
524 "to be a fan sensor: the threshold (%d) is "
525 "below the minimum (%d)\n", i,
526 (int)data->bank2_settings[i][1],
527 (int)abituguru_bank2_min_threshold);
530 if (data->bank2_settings[i][1] >
531 abituguru_bank2_max_threshold) {
532 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
533 "to be a fan sensor: the threshold (%d) is "
534 "above the maximum (%d)\n", i,
535 (int)data->bank2_settings[i][1],
536 (int)abituguru_bank2_max_threshold);
541 data->bank2_sensors = i;
542 ABIT_UGURU_DEBUG(2, " found: %d fan sensors\n",
543 (int)data->bank2_sensors);
546 static void __devinit
547 abituguru_detect_no_pwms(struct abituguru_data *data)
553 ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
554 "\"pwms\" module param\n", (int)data->pwms);
558 ABIT_UGURU_DEBUG(2, "detecting number of PWM outputs\n");
559 for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
560 /* 0x80 is the enable bit and the low
561 nibble is which temp sensor to use,
562 the other bits should be 0 */
563 if (data->pwm_settings[i][0] & ~0x8F) {
564 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
565 "to be a pwm channel: settings[0] = %02X\n",
566 i, (unsigned int)data->pwm_settings[i][0]);
570 /* the low nibble must correspond to one of the temp sensors
572 for (j = 0; j < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR];
574 if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][j] ==
575 (data->pwm_settings[i][0] & 0x0F))
578 if (j == data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
579 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
580 "to be a pwm channel: %d is not a valid temp "
581 "sensor address\n", i,
582 data->pwm_settings[i][0] & 0x0F);
586 /* check if all other settings are within the allowed range */
587 for (j = 1; j < 5; j++) {
589 /* special case pwm1 min pwm% */
590 if ((i == 0) && ((j == 1) || (j == 2)))
593 min = abituguru_pwm_min[j];
594 if (data->pwm_settings[i][j] < min) {
595 ABIT_UGURU_DEBUG(2, " pwm channel %d does "
596 "not seem to be a pwm channel: "
597 "setting %d (%d) is below the minimum "
598 "value (%d)\n", i, j,
599 (int)data->pwm_settings[i][j],
601 goto abituguru_detect_no_pwms_exit;
603 if (data->pwm_settings[i][j] > abituguru_pwm_max[j]) {
604 ABIT_UGURU_DEBUG(2, " pwm channel %d does "
605 "not seem to be a pwm channel: "
606 "setting %d (%d) is above the maximum "
607 "value (%d)\n", i, j,
608 (int)data->pwm_settings[i][j],
609 (int)abituguru_pwm_max[j]);
610 goto abituguru_detect_no_pwms_exit;
614 /* check that min temp < max temp and min pwm < max pwm */
615 if (data->pwm_settings[i][1] >= data->pwm_settings[i][2]) {
616 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
617 "to be a pwm channel: min pwm (%d) >= "
619 (int)data->pwm_settings[i][1],
620 (int)data->pwm_settings[i][2]);
623 if (data->pwm_settings[i][3] >= data->pwm_settings[i][4]) {
624 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
625 "to be a pwm channel: min temp (%d) >= "
626 "max temp (%d)\n", i,
627 (int)data->pwm_settings[i][3],
628 (int)data->pwm_settings[i][4]);
633 abituguru_detect_no_pwms_exit:
635 ABIT_UGURU_DEBUG(2, " found: %d PWM outputs\n", (int)data->pwms);
638 /* Following are the sysfs callback functions. These functions expect:
639 sensor_device_attribute_2->index: sensor address/offset in the bank
640 sensor_device_attribute_2->nr: register offset, bitmask or NA. */
641 static struct abituguru_data *abituguru_update_device(struct device *dev);
643 static ssize_t show_bank1_value(struct device *dev,
644 struct device_attribute *devattr, char *buf)
646 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
647 struct abituguru_data *data = abituguru_update_device(dev);
650 return sprintf(buf, "%d\n", (data->bank1_value[attr->index] *
651 data->bank1_max_value[attr->index] + 128) / 255);
654 static ssize_t show_bank1_setting(struct device *dev,
655 struct device_attribute *devattr, char *buf)
657 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
658 struct abituguru_data *data = dev_get_drvdata(dev);
659 return sprintf(buf, "%d\n",
660 (data->bank1_settings[attr->index][attr->nr] *
661 data->bank1_max_value[attr->index] + 128) / 255);
664 static ssize_t show_bank2_value(struct device *dev,
665 struct device_attribute *devattr, char *buf)
667 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
668 struct abituguru_data *data = abituguru_update_device(dev);
671 return sprintf(buf, "%d\n", (data->bank2_value[attr->index] *
672 ABIT_UGURU_FAN_MAX + 128) / 255);
675 static ssize_t show_bank2_setting(struct device *dev,
676 struct device_attribute *devattr, char *buf)
678 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
679 struct abituguru_data *data = dev_get_drvdata(dev);
680 return sprintf(buf, "%d\n",
681 (data->bank2_settings[attr->index][attr->nr] *
682 ABIT_UGURU_FAN_MAX + 128) / 255);
685 static ssize_t store_bank1_setting(struct device *dev, struct device_attribute
686 *devattr, const char *buf, size_t count)
688 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
689 struct abituguru_data *data = dev_get_drvdata(dev);
690 u8 val = (simple_strtoul(buf, NULL, 10) * 255 +
691 data->bank1_max_value[attr->index]/2) /
692 data->bank1_max_value[attr->index];
695 mutex_lock(&data->update_lock);
696 if (data->bank1_settings[attr->index][attr->nr] != val) {
697 u8 orig_val = data->bank1_settings[attr->index][attr->nr];
698 data->bank1_settings[attr->index][attr->nr] = val;
699 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
700 attr->index, data->bank1_settings[attr->index],
702 data->bank1_settings[attr->index][attr->nr] = orig_val;
706 mutex_unlock(&data->update_lock);
710 static ssize_t store_bank2_setting(struct device *dev, struct device_attribute
711 *devattr, const char *buf, size_t count)
713 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
714 struct abituguru_data *data = dev_get_drvdata(dev);
715 u8 val = (simple_strtoul(buf, NULL, 10)*255 + ABIT_UGURU_FAN_MAX/2) /
719 /* this check can be done before taking the lock */
720 if ((val < abituguru_bank2_min_threshold) ||
721 (val > abituguru_bank2_max_threshold))
724 mutex_lock(&data->update_lock);
725 if (data->bank2_settings[attr->index][attr->nr] != val) {
726 u8 orig_val = data->bank2_settings[attr->index][attr->nr];
727 data->bank2_settings[attr->index][attr->nr] = val;
728 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK2 + 2,
729 attr->index, data->bank2_settings[attr->index],
731 data->bank2_settings[attr->index][attr->nr] = orig_val;
735 mutex_unlock(&data->update_lock);
739 static ssize_t show_bank1_alarm(struct device *dev,
740 struct device_attribute *devattr, char *buf)
742 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
743 struct abituguru_data *data = abituguru_update_device(dev);
746 /* See if the alarm bit for this sensor is set, and if the
747 alarm matches the type of alarm we're looking for (for volt
748 it can be either low or high). The type is stored in a few
749 readonly bits in the settings part of the relevant sensor.
750 The bitmask of the type is passed to us in attr->nr. */
751 if ((data->alarms[attr->index / 8] & (0x01 << (attr->index % 8))) &&
752 (data->bank1_settings[attr->index][0] & attr->nr))
753 return sprintf(buf, "1\n");
755 return sprintf(buf, "0\n");
758 static ssize_t show_bank2_alarm(struct device *dev,
759 struct device_attribute *devattr, char *buf)
761 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
762 struct abituguru_data *data = abituguru_update_device(dev);
765 if (data->alarms[2] & (0x01 << attr->index))
766 return sprintf(buf, "1\n");
768 return sprintf(buf, "0\n");
771 static ssize_t show_bank1_mask(struct device *dev,
772 struct device_attribute *devattr, char *buf)
774 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
775 struct abituguru_data *data = dev_get_drvdata(dev);
776 if (data->bank1_settings[attr->index][0] & attr->nr)
777 return sprintf(buf, "1\n");
779 return sprintf(buf, "0\n");
782 static ssize_t show_bank2_mask(struct device *dev,
783 struct device_attribute *devattr, char *buf)
785 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
786 struct abituguru_data *data = dev_get_drvdata(dev);
787 if (data->bank2_settings[attr->index][0] & attr->nr)
788 return sprintf(buf, "1\n");
790 return sprintf(buf, "0\n");
793 static ssize_t store_bank1_mask(struct device *dev,
794 struct device_attribute *devattr, const char *buf, size_t count)
796 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
797 struct abituguru_data *data = dev_get_drvdata(dev);
798 int mask = simple_strtoul(buf, NULL, 10);
802 mutex_lock(&data->update_lock);
803 orig_val = data->bank1_settings[attr->index][0];
806 data->bank1_settings[attr->index][0] |= attr->nr;
808 data->bank1_settings[attr->index][0] &= ~attr->nr;
810 if ((data->bank1_settings[attr->index][0] != orig_val) &&
811 (abituguru_write(data,
812 ABIT_UGURU_SENSOR_BANK1 + 2, attr->index,
813 data->bank1_settings[attr->index], 3) < 1)) {
814 data->bank1_settings[attr->index][0] = orig_val;
817 mutex_unlock(&data->update_lock);
821 static ssize_t store_bank2_mask(struct device *dev,
822 struct device_attribute *devattr, const char *buf, size_t count)
824 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
825 struct abituguru_data *data = dev_get_drvdata(dev);
826 int mask = simple_strtoul(buf, NULL, 10);
830 mutex_lock(&data->update_lock);
831 orig_val = data->bank2_settings[attr->index][0];
834 data->bank2_settings[attr->index][0] |= attr->nr;
836 data->bank2_settings[attr->index][0] &= ~attr->nr;
838 if ((data->bank2_settings[attr->index][0] != orig_val) &&
839 (abituguru_write(data,
840 ABIT_UGURU_SENSOR_BANK2 + 2, attr->index,
841 data->bank2_settings[attr->index], 2) < 1)) {
842 data->bank2_settings[attr->index][0] = orig_val;
845 mutex_unlock(&data->update_lock);
849 /* Fan PWM (speed control) */
850 static ssize_t show_pwm_setting(struct device *dev,
851 struct device_attribute *devattr, char *buf)
853 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
854 struct abituguru_data *data = dev_get_drvdata(dev);
855 return sprintf(buf, "%d\n", data->pwm_settings[attr->index][attr->nr] *
856 abituguru_pwm_settings_multiplier[attr->nr]);
859 static ssize_t store_pwm_setting(struct device *dev, struct device_attribute
860 *devattr, const char *buf, size_t count)
862 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
863 struct abituguru_data *data = dev_get_drvdata(dev);
864 u8 min, val = (simple_strtoul(buf, NULL, 10) +
865 abituguru_pwm_settings_multiplier[attr->nr]/2) /
866 abituguru_pwm_settings_multiplier[attr->nr];
869 /* special case pwm1 min pwm% */
870 if ((attr->index == 0) && ((attr->nr == 1) || (attr->nr == 2)))
873 min = abituguru_pwm_min[attr->nr];
875 /* this check can be done before taking the lock */
876 if ((val < min) || (val > abituguru_pwm_max[attr->nr]))
879 mutex_lock(&data->update_lock);
880 /* this check needs to be done after taking the lock */
881 if ((attr->nr & 1) &&
882 (val >= data->pwm_settings[attr->index][attr->nr + 1]))
884 else if (!(attr->nr & 1) &&
885 (val <= data->pwm_settings[attr->index][attr->nr - 1]))
887 else if (data->pwm_settings[attr->index][attr->nr] != val) {
888 u8 orig_val = data->pwm_settings[attr->index][attr->nr];
889 data->pwm_settings[attr->index][attr->nr] = val;
890 if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
891 attr->index, data->pwm_settings[attr->index],
893 data->pwm_settings[attr->index][attr->nr] =
898 mutex_unlock(&data->update_lock);
902 static ssize_t show_pwm_sensor(struct device *dev,
903 struct device_attribute *devattr, char *buf)
905 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
906 struct abituguru_data *data = dev_get_drvdata(dev);
908 /* We need to walk to the temp sensor addresses to find what
909 the userspace id of the configured temp sensor is. */
910 for (i = 0; i < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]; i++)
911 if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][i] ==
912 (data->pwm_settings[attr->index][0] & 0x0F))
913 return sprintf(buf, "%d\n", i+1);
918 static ssize_t store_pwm_sensor(struct device *dev, struct device_attribute
919 *devattr, const char *buf, size_t count)
921 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
922 struct abituguru_data *data = dev_get_drvdata(dev);
923 unsigned long val = simple_strtoul(buf, NULL, 10) - 1;
926 mutex_lock(&data->update_lock);
927 if (val < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
928 u8 orig_val = data->pwm_settings[attr->index][0];
929 u8 address = data->bank1_address[ABIT_UGURU_TEMP_SENSOR][val];
930 data->pwm_settings[attr->index][0] &= 0xF0;
931 data->pwm_settings[attr->index][0] |= address;
932 if (data->pwm_settings[attr->index][0] != orig_val) {
933 if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
935 data->pwm_settings[attr->index],
937 data->pwm_settings[attr->index][0] = orig_val;
944 mutex_unlock(&data->update_lock);
948 static ssize_t show_pwm_enable(struct device *dev,
949 struct device_attribute *devattr, char *buf)
951 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
952 struct abituguru_data *data = dev_get_drvdata(dev);
954 if (data->pwm_settings[attr->index][0] & ABIT_UGURU_FAN_PWM_ENABLE)
956 return sprintf(buf, "%d\n", res);
959 static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
960 *devattr, const char *buf, size_t count)
962 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
963 struct abituguru_data *data = dev_get_drvdata(dev);
964 u8 orig_val, user_val = simple_strtoul(buf, NULL, 10);
967 mutex_lock(&data->update_lock);
968 orig_val = data->pwm_settings[attr->index][0];
971 data->pwm_settings[attr->index][0] &=
972 ~ABIT_UGURU_FAN_PWM_ENABLE;
975 data->pwm_settings[attr->index][0] |=
976 ABIT_UGURU_FAN_PWM_ENABLE;
981 if ((data->pwm_settings[attr->index][0] != orig_val) &&
982 (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
983 attr->index, data->pwm_settings[attr->index],
985 data->pwm_settings[attr->index][0] = orig_val;
988 mutex_unlock(&data->update_lock);
992 static ssize_t show_name(struct device *dev,
993 struct device_attribute *devattr, char *buf)
995 return sprintf(buf, "%s\n", ABIT_UGURU_NAME);
998 /* Sysfs attr templates, the real entries are generated automatically. */
1000 struct sensor_device_attribute_2 abituguru_sysfs_bank1_templ[2][9] = {
1002 SENSOR_ATTR_2(in%d_input, 0444, show_bank1_value, NULL, 0, 0),
1003 SENSOR_ATTR_2(in%d_min, 0644, show_bank1_setting,
1004 store_bank1_setting, 1, 0),
1005 SENSOR_ATTR_2(in%d_min_alarm, 0444, show_bank1_alarm, NULL,
1006 ABIT_UGURU_VOLT_LOW_ALARM_FLAG, 0),
1007 SENSOR_ATTR_2(in%d_max, 0644, show_bank1_setting,
1008 store_bank1_setting, 2, 0),
1009 SENSOR_ATTR_2(in%d_max_alarm, 0444, show_bank1_alarm, NULL,
1010 ABIT_UGURU_VOLT_HIGH_ALARM_FLAG, 0),
1011 SENSOR_ATTR_2(in%d_beep, 0644, show_bank1_mask,
1012 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1013 SENSOR_ATTR_2(in%d_shutdown, 0644, show_bank1_mask,
1014 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1015 SENSOR_ATTR_2(in%d_min_alarm_enable, 0644, show_bank1_mask,
1016 store_bank1_mask, ABIT_UGURU_VOLT_LOW_ALARM_ENABLE, 0),
1017 SENSOR_ATTR_2(in%d_max_alarm_enable, 0644, show_bank1_mask,
1018 store_bank1_mask, ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE, 0),
1020 SENSOR_ATTR_2(temp%d_input, 0444, show_bank1_value, NULL, 0, 0),
1021 SENSOR_ATTR_2(temp%d_alarm, 0444, show_bank1_alarm, NULL,
1022 ABIT_UGURU_TEMP_HIGH_ALARM_FLAG, 0),
1023 SENSOR_ATTR_2(temp%d_max, 0644, show_bank1_setting,
1024 store_bank1_setting, 1, 0),
1025 SENSOR_ATTR_2(temp%d_crit, 0644, show_bank1_setting,
1026 store_bank1_setting, 2, 0),
1027 SENSOR_ATTR_2(temp%d_beep, 0644, show_bank1_mask,
1028 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1029 SENSOR_ATTR_2(temp%d_shutdown, 0644, show_bank1_mask,
1030 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1031 SENSOR_ATTR_2(temp%d_alarm_enable, 0644, show_bank1_mask,
1032 store_bank1_mask, ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE, 0),
1036 static const struct sensor_device_attribute_2 abituguru_sysfs_fan_templ[6] = {
1037 SENSOR_ATTR_2(fan%d_input, 0444, show_bank2_value, NULL, 0, 0),
1038 SENSOR_ATTR_2(fan%d_alarm, 0444, show_bank2_alarm, NULL, 0, 0),
1039 SENSOR_ATTR_2(fan%d_min, 0644, show_bank2_setting,
1040 store_bank2_setting, 1, 0),
1041 SENSOR_ATTR_2(fan%d_beep, 0644, show_bank2_mask,
1042 store_bank2_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1043 SENSOR_ATTR_2(fan%d_shutdown, 0644, show_bank2_mask,
1044 store_bank2_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1045 SENSOR_ATTR_2(fan%d_alarm_enable, 0644, show_bank2_mask,
1046 store_bank2_mask, ABIT_UGURU_FAN_LOW_ALARM_ENABLE, 0),
1049 static const struct sensor_device_attribute_2 abituguru_sysfs_pwm_templ[6] = {
1050 SENSOR_ATTR_2(pwm%d_enable, 0644, show_pwm_enable,
1051 store_pwm_enable, 0, 0),
1052 SENSOR_ATTR_2(pwm%d_auto_channels_temp, 0644, show_pwm_sensor,
1053 store_pwm_sensor, 0, 0),
1054 SENSOR_ATTR_2(pwm%d_auto_point1_pwm, 0644, show_pwm_setting,
1055 store_pwm_setting, 1, 0),
1056 SENSOR_ATTR_2(pwm%d_auto_point2_pwm, 0644, show_pwm_setting,
1057 store_pwm_setting, 2, 0),
1058 SENSOR_ATTR_2(pwm%d_auto_point1_temp, 0644, show_pwm_setting,
1059 store_pwm_setting, 3, 0),
1060 SENSOR_ATTR_2(pwm%d_auto_point2_temp, 0644, show_pwm_setting,
1061 store_pwm_setting, 4, 0),
1064 static const struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
1065 SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
1068 static int __devinit abituguru_probe(struct platform_device *pdev)
1070 struct abituguru_data *data;
1072 char *sysfs_filename;
1073 int sysfs_attr_i = 0;
1075 /* El weirdo probe order, to keep the sysfs order identical to the
1076 BIOS and window-appliction listing order. */
1077 const u8 probe_order[16] = { 0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E,
1078 0x02, 0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
1080 if (!(data = kzalloc(sizeof(struct abituguru_data), GFP_KERNEL)))
1083 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
1084 mutex_init(&data->update_lock);
1085 platform_set_drvdata(pdev, data);
1087 /* See if the uGuru is ready */
1088 if (inb_p(data->addr + ABIT_UGURU_DATA) == ABIT_UGURU_STATUS_INPUT)
1089 data->uguru_ready = 1;
1091 /* Completely read the uGuru this has 2 purposes:
1092 - testread / see if one really is there.
1093 - make an in memory copy of all the uguru settings for future use. */
1094 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1095 data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3) {
1100 for (i = 0; i < 16; i++) {
1101 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, i,
1102 &data->bank1_value[i], 1,
1103 ABIT_UGURU_MAX_RETRIES) != 1) {
1107 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1+1, i,
1108 data->bank1_settings[i], 3,
1109 ABIT_UGURU_MAX_RETRIES) != 3) {
1114 /* Note: We don't know how many bank2 sensors / pwms there really are,
1115 but in order to "detect" this we need to read the maximum amount
1116 anyways. If we read sensors/pwms not there we'll just read crap
1117 this can't hurt. We need the detection because we don't want
1118 unwanted writes, which will hurt! */
1119 for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
1120 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
1121 &data->bank2_value[i], 1,
1122 ABIT_UGURU_MAX_RETRIES) != 1) {
1126 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2+1, i,
1127 data->bank2_settings[i], 2,
1128 ABIT_UGURU_MAX_RETRIES) != 2) {
1133 for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
1134 if (abituguru_read(data, ABIT_UGURU_FAN_PWM, i,
1135 data->pwm_settings[i], 5,
1136 ABIT_UGURU_MAX_RETRIES) != 5) {
1141 data->last_updated = jiffies;
1143 /* Detect sensor types and fill the sysfs attr for bank1 */
1144 sysfs_filename = data->bank1_names;
1145 for (i = 0; i < 16; i++) {
1146 res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
1151 if (res == ABIT_UGURU_NC)
1154 for (j = 0; j < (res ? 7 : 9); j++) {
1155 const char *name_templ = abituguru_sysfs_bank1_templ[
1156 res][j].dev_attr.attr.name;
1157 data->sysfs_attr[sysfs_attr_i] =
1158 abituguru_sysfs_bank1_templ[res][j];
1159 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1161 sysfs_filename += sprintf(sysfs_filename, name_templ,
1162 data->bank1_sensors[res] + res) + 1;
1163 data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
1166 data->bank1_max_value[probe_order[i]] =
1167 abituguru_bank1_max_value[res];
1168 data->bank1_address[res][data->bank1_sensors[res]] =
1170 data->bank1_sensors[res]++;
1172 /* Detect number of sensors and fill the sysfs attr for bank2 (fans) */
1173 abituguru_detect_no_bank2_sensors(data);
1174 for (i = 0; i < data->bank2_sensors; i++) {
1175 for (j = 0; j < 6; j++) {
1176 const char *name_templ = abituguru_sysfs_fan_templ[j].
1178 data->sysfs_attr[sysfs_attr_i] =
1179 abituguru_sysfs_fan_templ[j];
1180 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1182 sysfs_filename += sprintf(sysfs_filename, name_templ,
1184 data->sysfs_attr[sysfs_attr_i].index = i;
1188 /* Detect number of sensors and fill the sysfs attr for pwms */
1189 abituguru_detect_no_pwms(data);
1190 for (i = 0; i < data->pwms; i++) {
1191 for (j = 0; j < 6; j++) {
1192 const char *name_templ = abituguru_sysfs_pwm_templ[j].
1194 data->sysfs_attr[sysfs_attr_i] =
1195 abituguru_sysfs_pwm_templ[j];
1196 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1198 sysfs_filename += sprintf(sysfs_filename, name_templ,
1200 data->sysfs_attr[sysfs_attr_i].index = i;
1204 /* Last add any "generic" entries to sysfs */
1205 for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++) {
1206 data->sysfs_attr[sysfs_attr_i] = abituguru_sysfs_attr[i];
1209 printk(KERN_INFO ABIT_UGURU_NAME ": found Abit uGuru\n");
1211 /* Register sysfs hooks */
1212 data->class_dev = hwmon_device_register(&pdev->dev);
1213 if (IS_ERR(data->class_dev)) {
1215 return PTR_ERR(data->class_dev);
1217 for (i = 0; i < sysfs_attr_i; i++)
1218 device_create_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1223 static int __devexit abituguru_remove(struct platform_device *pdev)
1225 struct abituguru_data *data = platform_get_drvdata(pdev);
1227 platform_set_drvdata(pdev, NULL);
1228 hwmon_device_unregister(data->class_dev);
1234 static struct abituguru_data *abituguru_update_device(struct device *dev)
1237 struct abituguru_data *data = dev_get_drvdata(dev);
1238 /* fake a complete successful read if no update necessary. */
1241 mutex_lock(&data->update_lock);
1242 if (time_after(jiffies, data->last_updated + HZ)) {
1244 if ((err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1245 data->alarms, 3, 0)) != 3)
1247 for (i = 0; i < 16; i++) {
1248 if ((err = abituguru_read(data,
1249 ABIT_UGURU_SENSOR_BANK1, i,
1250 &data->bank1_value[i], 1, 0)) != 1)
1252 if ((err = abituguru_read(data,
1253 ABIT_UGURU_SENSOR_BANK1 + 1, i,
1254 data->bank1_settings[i], 3, 0)) != 3)
1257 for (i = 0; i < data->bank2_sensors; i++)
1258 if ((err = abituguru_read(data,
1259 ABIT_UGURU_SENSOR_BANK2, i,
1260 &data->bank2_value[i], 1, 0)) != 1)
1264 data->update_timeouts = 0;
1266 /* handle timeout condition */
1267 if (err == -EBUSY) {
1268 /* No overflow please */
1269 if (data->update_timeouts < 255u)
1270 data->update_timeouts++;
1271 if (data->update_timeouts <= ABIT_UGURU_MAX_TIMEOUTS) {
1272 ABIT_UGURU_DEBUG(3, "timeout exceeded, will "
1273 "try again next update\n");
1274 /* Just a timeout, fake a successful read */
1277 ABIT_UGURU_DEBUG(1, "timeout exceeded %d "
1278 "times waiting for more input state\n",
1279 (int)data->update_timeouts);
1281 /* On success set last_updated */
1283 data->last_updated = jiffies;
1285 mutex_unlock(&data->update_lock);
1293 static struct platform_driver abituguru_driver = {
1295 .owner = THIS_MODULE,
1296 .name = ABIT_UGURU_NAME,
1298 .probe = abituguru_probe,
1299 .remove = __devexit_p(abituguru_remove),
1302 static int __init abituguru_detect(void)
1304 /* See if there is an uguru there. After a reboot uGuru will hold 0x00
1305 at DATA and 0xAC, when this driver has already been loaded once
1306 DATA will hold 0x08. For most uGuru's CMD will hold 0xAC in either
1307 scenario but some will hold 0x00.
1308 Some uGuru's initally hold 0x09 at DATA and will only hold 0x08
1309 after reading CMD first, so CMD must be read first! */
1310 u8 cmd_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_CMD);
1311 u8 data_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_DATA);
1312 if (((data_val == 0x00) || (data_val == 0x08)) &&
1313 ((cmd_val == 0x00) || (cmd_val == 0xAC)))
1314 return ABIT_UGURU_BASE;
1316 ABIT_UGURU_DEBUG(2, "no Abit uGuru found, data = 0x%02X, cmd = "
1317 "0x%02X\n", (unsigned int)data_val, (unsigned int)cmd_val);
1320 printk(KERN_INFO ABIT_UGURU_NAME ": Assuming Abit uGuru is "
1321 "present because of \"force\" parameter\n");
1322 return ABIT_UGURU_BASE;
1325 /* No uGuru found */
1329 static struct platform_device *abituguru_pdev;
1331 static int __init abituguru_init(void)
1334 struct resource res = { .flags = IORESOURCE_IO };
1336 address = abituguru_detect();
1340 err = platform_driver_register(&abituguru_driver);
1344 abituguru_pdev = platform_device_alloc(ABIT_UGURU_NAME, address);
1345 if (!abituguru_pdev) {
1346 printk(KERN_ERR ABIT_UGURU_NAME
1347 ": Device allocation failed\n");
1349 goto exit_driver_unregister;
1352 res.start = address;
1353 res.end = address + ABIT_UGURU_REGION_LENGTH - 1;
1354 res.name = ABIT_UGURU_NAME;
1356 err = platform_device_add_resources(abituguru_pdev, &res, 1);
1358 printk(KERN_ERR ABIT_UGURU_NAME
1359 ": Device resource addition failed (%d)\n", err);
1360 goto exit_device_put;
1363 err = platform_device_add(abituguru_pdev);
1365 printk(KERN_ERR ABIT_UGURU_NAME
1366 ": Device addition failed (%d)\n", err);
1367 goto exit_device_put;
1373 platform_device_put(abituguru_pdev);
1374 exit_driver_unregister:
1375 platform_driver_unregister(&abituguru_driver);
1380 static void __exit abituguru_exit(void)
1382 platform_device_unregister(abituguru_pdev);
1383 platform_driver_unregister(&abituguru_driver);
1386 MODULE_AUTHOR("Hans de Goede <j.w.r.degoede@hhs.nl>");
1387 MODULE_DESCRIPTION("Abit uGuru Sensor device");
1388 MODULE_LICENSE("GPL");
1390 module_init(abituguru_init);
1391 module_exit(abituguru_exit);