making the kernel build with up to date compilers again, see https://bbs.archlinux...
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / hwmon / abituguru.c
blob4dbdb81ea3b1bfffce5d016746409ef7a3fe7f1d
1 /*
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 first and second revision of
20 the custom Abit uGuru chip found on Abit uGuru motherboards. Note: because
21 of lack of specs the CPU/RAM voltage & frequency control is not supported!
23 #include <linux/module.h>
24 #include <linux/sched.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/mutex.h>
29 #include <linux/err.h>
30 #include <linux/delay.h>
31 #include <linux/platform_device.h>
32 #include <linux/hwmon.h>
33 #include <linux/hwmon-sysfs.h>
34 #include <linux/dmi.h>
35 #include <asm/io.h>
37 /* Banks */
38 #define ABIT_UGURU_ALARM_BANK 0x20 /* 1x 3 bytes */
39 #define ABIT_UGURU_SENSOR_BANK1 0x21 /* 16x volt and temp */
40 #define ABIT_UGURU_FAN_PWM 0x24 /* 3x 5 bytes */
41 #define ABIT_UGURU_SENSOR_BANK2 0x26 /* fans */
42 /* max nr of sensors in bank1, a bank1 sensor can be in, temp or nc */
43 #define ABIT_UGURU_MAX_BANK1_SENSORS 16
44 /* Warning if you increase one of the 2 MAX defines below to 10 or higher you
45 should adjust the belonging _NAMES_LENGTH macro for the 2 digit number! */
46 /* max nr of sensors in bank2, currently mb's with max 6 fans are known */
47 #define ABIT_UGURU_MAX_BANK2_SENSORS 6
48 /* max nr of pwm outputs, currently mb's with max 5 pwm outputs are known */
49 #define ABIT_UGURU_MAX_PWMS 5
50 /* uGuru sensor bank 1 flags */ /* Alarm if: */
51 #define ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE 0x01 /* temp over warn */
52 #define ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE 0x02 /* volt over max */
53 #define ABIT_UGURU_VOLT_LOW_ALARM_ENABLE 0x04 /* volt under min */
54 #define ABIT_UGURU_TEMP_HIGH_ALARM_FLAG 0x10 /* temp is over warn */
55 #define ABIT_UGURU_VOLT_HIGH_ALARM_FLAG 0x20 /* volt is over max */
56 #define ABIT_UGURU_VOLT_LOW_ALARM_FLAG 0x40 /* volt is under min */
57 /* uGuru sensor bank 2 flags */ /* Alarm if: */
58 #define ABIT_UGURU_FAN_LOW_ALARM_ENABLE 0x01 /* fan under min */
59 /* uGuru sensor bank common flags */
60 #define ABIT_UGURU_BEEP_ENABLE 0x08 /* beep if alarm */
61 #define ABIT_UGURU_SHUTDOWN_ENABLE 0x80 /* shutdown if alarm */
62 /* uGuru fan PWM (speed control) flags */
63 #define ABIT_UGURU_FAN_PWM_ENABLE 0x80 /* enable speed control */
64 /* Values used for conversion */
65 #define ABIT_UGURU_FAN_MAX 15300 /* RPM */
66 /* Bank1 sensor types */
67 #define ABIT_UGURU_IN_SENSOR 0
68 #define ABIT_UGURU_TEMP_SENSOR 1
69 #define ABIT_UGURU_NC 2
70 /* In many cases we need to wait for the uGuru to reach a certain status, most
71 of the time it will reach this status within 30 - 90 ISA reads, and thus we
72 can best busy wait. This define gives the total amount of reads to try. */
73 #define ABIT_UGURU_WAIT_TIMEOUT 125
74 /* However sometimes older versions of the uGuru seem to be distracted and they
75 do not respond for a long time. To handle this we sleep before each of the
76 last ABIT_UGURU_WAIT_TIMEOUT_SLEEP tries. */
77 #define ABIT_UGURU_WAIT_TIMEOUT_SLEEP 5
78 /* Normally all expected status in abituguru_ready, are reported after the
79 first read, but sometimes not and we need to poll. */
80 #define ABIT_UGURU_READY_TIMEOUT 5
81 /* Maximum 3 retries on timedout reads/writes, delay 200 ms before retrying */
82 #define ABIT_UGURU_MAX_RETRIES 3
83 #define ABIT_UGURU_RETRY_DELAY (HZ/5)
84 /* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is an error */
85 #define ABIT_UGURU_MAX_TIMEOUTS 2
86 /* utility macros */
87 #define ABIT_UGURU_NAME "abituguru"
88 #define ABIT_UGURU_DEBUG(level, format, arg...) \
89 if (level <= verbose) \
90 printk(KERN_DEBUG ABIT_UGURU_NAME ": " format , ## arg)
91 /* Macros to help calculate the sysfs_names array length */
92 /* sum of strlen of: in??_input\0, in??_{min,max}\0, in??_{min,max}_alarm\0,
93 in??_{min,max}_alarm_enable\0, in??_beep\0, in??_shutdown\0 */
94 #define ABITUGURU_IN_NAMES_LENGTH (11 + 2 * 9 + 2 * 15 + 2 * 22 + 10 + 14)
95 /* sum of strlen of: temp??_input\0, temp??_max\0, temp??_crit\0,
96 temp??_alarm\0, temp??_alarm_enable\0, temp??_beep\0, temp??_shutdown\0 */
97 #define ABITUGURU_TEMP_NAMES_LENGTH (13 + 11 + 12 + 13 + 20 + 12 + 16)
98 /* sum of strlen of: fan?_input\0, fan?_min\0, fan?_alarm\0,
99 fan?_alarm_enable\0, fan?_beep\0, fan?_shutdown\0 */
100 #define ABITUGURU_FAN_NAMES_LENGTH (11 + 9 + 11 + 18 + 10 + 14)
101 /* sum of strlen of: pwm?_enable\0, pwm?_auto_channels_temp\0,
102 pwm?_auto_point{1,2}_pwm\0, pwm?_auto_point{1,2}_temp\0 */
103 #define ABITUGURU_PWM_NAMES_LENGTH (12 + 24 + 2 * 21 + 2 * 22)
104 /* IN_NAMES_LENGTH > TEMP_NAMES_LENGTH so assume all bank1 sensors are in */
105 #define ABITUGURU_SYSFS_NAMES_LENGTH ( \
106 ABIT_UGURU_MAX_BANK1_SENSORS * ABITUGURU_IN_NAMES_LENGTH + \
107 ABIT_UGURU_MAX_BANK2_SENSORS * ABITUGURU_FAN_NAMES_LENGTH + \
108 ABIT_UGURU_MAX_PWMS * ABITUGURU_PWM_NAMES_LENGTH)
110 /* All the macros below are named identical to the oguru and oguru2 programs
111 reverse engineered by Olle Sandberg, hence the names might not be 100%
112 logical. I could come up with better names, but I prefer keeping the names
113 identical so that this driver can be compared with his work more easily. */
114 /* Two i/o-ports are used by uGuru */
115 #define ABIT_UGURU_BASE 0x00E0
116 /* Used to tell uGuru what to read and to read the actual data */
117 #define ABIT_UGURU_CMD 0x00
118 /* Mostly used to check if uGuru is busy */
119 #define ABIT_UGURU_DATA 0x04
120 #define ABIT_UGURU_REGION_LENGTH 5
121 /* uGuru status' */
122 #define ABIT_UGURU_STATUS_WRITE 0x00 /* Ready to be written */
123 #define ABIT_UGURU_STATUS_READ 0x01 /* Ready to be read */
124 #define ABIT_UGURU_STATUS_INPUT 0x08 /* More input */
125 #define ABIT_UGURU_STATUS_READY 0x09 /* Ready to be written */
127 /* Constants */
128 /* in (Volt) sensors go up to 3494 mV, temp to 255000 millidegrees Celsius */
129 static const int abituguru_bank1_max_value[2] = { 3494, 255000 };
130 /* Min / Max allowed values for sensor2 (fan) alarm threshold, these values
131 correspond to 300-3000 RPM */
132 static const u8 abituguru_bank2_min_threshold = 5;
133 static const u8 abituguru_bank2_max_threshold = 50;
134 /* Register 0 is a bitfield, 1 and 2 are pwm settings (255 = 100%), 3 and 4
135 are temperature trip points. */
136 static const int abituguru_pwm_settings_multiplier[5] = { 0, 1, 1, 1000, 1000 };
137 /* Min / Max allowed values for pwm_settings. Note: pwm1 (CPU fan) is a
138 special case the minium allowed pwm% setting for this is 30% (77) on
139 some MB's this special case is handled in the code! */
140 static const u8 abituguru_pwm_min[5] = { 0, 170, 170, 25, 25 };
141 static const u8 abituguru_pwm_max[5] = { 0, 255, 255, 75, 75 };
144 /* Insmod parameters */
145 static int force;
146 module_param(force, bool, 0);
147 MODULE_PARM_DESC(force, "Set to one to force detection.");
148 static int bank1_types[ABIT_UGURU_MAX_BANK1_SENSORS] = { -1, -1, -1, -1, -1,
149 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
150 module_param_array(bank1_types, int, NULL, 0);
151 MODULE_PARM_DESC(bank1_types, "Bank1 sensortype autodetection override:\n"
152 " -1 autodetect\n"
153 " 0 volt sensor\n"
154 " 1 temp sensor\n"
155 " 2 not connected");
156 static int fan_sensors;
157 module_param(fan_sensors, int, 0);
158 MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru "
159 "(0 = autodetect)");
160 static int pwms;
161 module_param(pwms, int, 0);
162 MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru "
163 "(0 = autodetect)");
165 /* Default verbose is 2, since this driver is still in the testing phase */
166 static int verbose = 2;
167 module_param(verbose, int, 0644);
168 MODULE_PARM_DESC(verbose, "How verbose should the driver be? (0-3):\n"
169 " 0 normal output\n"
170 " 1 + verbose error reporting\n"
171 " 2 + sensors type probing info\n"
172 " 3 + retryable error reporting");
175 /* For the Abit uGuru, we need to keep some data in memory.
176 The structure is dynamically allocated, at the same time when a new
177 abituguru device is allocated. */
178 struct abituguru_data {
179 struct device *hwmon_dev; /* hwmon registered device */
180 struct mutex update_lock; /* protect access to data and uGuru */
181 unsigned long last_updated; /* In jiffies */
182 unsigned short addr; /* uguru base address */
183 char uguru_ready; /* is the uguru in ready state? */
184 unsigned char update_timeouts; /* number of update timeouts since last
185 successful update */
187 /* The sysfs attr and their names are generated automatically, for bank1
188 we cannot use a predefined array because we don't know beforehand
189 of a sensor is a volt or a temp sensor, for bank2 and the pwms its
190 easier todo things the same way. For in sensors we have 9 (temp 7)
191 sysfs entries per sensor, for bank2 and pwms 6. */
192 struct sensor_device_attribute_2 sysfs_attr[
193 ABIT_UGURU_MAX_BANK1_SENSORS * 9 +
194 ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6];
195 /* Buffer to store the dynamically generated sysfs names */
196 char sysfs_names[ABITUGURU_SYSFS_NAMES_LENGTH];
198 /* Bank 1 data */
199 /* number of and addresses of [0] in, [1] temp sensors */
200 u8 bank1_sensors[2];
201 u8 bank1_address[2][ABIT_UGURU_MAX_BANK1_SENSORS];
202 u8 bank1_value[ABIT_UGURU_MAX_BANK1_SENSORS];
203 /* This array holds 3 entries per sensor for the bank 1 sensor settings
204 (flags, min, max for voltage / flags, warn, shutdown for temp). */
205 u8 bank1_settings[ABIT_UGURU_MAX_BANK1_SENSORS][3];
206 /* Maximum value for each sensor used for scaling in mV/millidegrees
207 Celsius. */
208 int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS];
210 /* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */
211 u8 bank2_sensors; /* actual number of bank2 sensors found */
212 u8 bank2_value[ABIT_UGURU_MAX_BANK2_SENSORS];
213 u8 bank2_settings[ABIT_UGURU_MAX_BANK2_SENSORS][2]; /* flags, min */
215 /* Alarms 2 bytes for bank1, 1 byte for bank2 */
216 u8 alarms[3];
218 /* Fan PWM (speed control) 5 bytes per PWM */
219 u8 pwms; /* actual number of pwms found */
220 u8 pwm_settings[ABIT_UGURU_MAX_PWMS][5];
223 /* wait till the uguru is in the specified state */
224 static int abituguru_wait(struct abituguru_data *data, u8 state)
226 int timeout = ABIT_UGURU_WAIT_TIMEOUT;
228 while (inb_p(data->addr + ABIT_UGURU_DATA) != state) {
229 timeout--;
230 if (timeout == 0)
231 return -EBUSY;
232 /* sleep a bit before our last few tries, see the comment on
233 this where ABIT_UGURU_WAIT_TIMEOUT_SLEEP is defined. */
234 if (timeout <= ABIT_UGURU_WAIT_TIMEOUT_SLEEP)
235 msleep(0);
237 return 0;
240 /* Put the uguru in ready for input state */
241 static int abituguru_ready(struct abituguru_data *data)
243 int timeout = ABIT_UGURU_READY_TIMEOUT;
245 if (data->uguru_ready)
246 return 0;
248 /* Reset? / Prepare for next read/write cycle */
249 outb(0x00, data->addr + ABIT_UGURU_DATA);
251 /* Wait till the uguru is ready */
252 if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) {
253 ABIT_UGURU_DEBUG(1,
254 "timeout exceeded waiting for ready state\n");
255 return -EIO;
258 /* Cmd port MUST be read now and should contain 0xAC */
259 while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
260 timeout--;
261 if (timeout == 0) {
262 ABIT_UGURU_DEBUG(1,
263 "CMD reg does not hold 0xAC after ready command\n");
264 return -EIO;
266 msleep(0);
269 /* After this the ABIT_UGURU_DATA port should contain
270 ABIT_UGURU_STATUS_INPUT */
271 timeout = ABIT_UGURU_READY_TIMEOUT;
272 while (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT) {
273 timeout--;
274 if (timeout == 0) {
275 ABIT_UGURU_DEBUG(1,
276 "state != more input after ready command\n");
277 return -EIO;
279 msleep(0);
282 data->uguru_ready = 1;
283 return 0;
286 /* Send the bank and then sensor address to the uGuru for the next read/write
287 cycle. This function gets called as the first part of a read/write by
288 abituguru_read and abituguru_write. This function should never be
289 called by any other function. */
290 static int abituguru_send_address(struct abituguru_data *data,
291 u8 bank_addr, u8 sensor_addr, int retries)
293 /* assume the caller does error handling itself if it has not requested
294 any retries, and thus be quiet. */
295 int report_errors = retries;
297 for (;;) {
298 /* Make sure the uguru is ready and then send the bank address,
299 after this the uguru is no longer "ready". */
300 if (abituguru_ready(data) != 0)
301 return -EIO;
302 outb(bank_addr, data->addr + ABIT_UGURU_DATA);
303 data->uguru_ready = 0;
305 /* Wait till the uguru is ABIT_UGURU_STATUS_INPUT state again
306 and send the sensor addr */
307 if (abituguru_wait(data, ABIT_UGURU_STATUS_INPUT)) {
308 if (retries) {
309 ABIT_UGURU_DEBUG(3, "timeout exceeded "
310 "waiting for more input state, %d "
311 "tries remaining\n", retries);
312 set_current_state(TASK_UNINTERRUPTIBLE);
313 schedule_timeout(ABIT_UGURU_RETRY_DELAY);
314 retries--;
315 continue;
317 if (report_errors)
318 ABIT_UGURU_DEBUG(1, "timeout exceeded "
319 "waiting for more input state "
320 "(bank: %d)\n", (int)bank_addr);
321 return -EBUSY;
323 outb(sensor_addr, data->addr + ABIT_UGURU_CMD);
324 return 0;
328 /* Read count bytes from sensor sensor_addr in bank bank_addr and store the
329 result in buf, retry the send address part of the read retries times. */
330 static int abituguru_read(struct abituguru_data *data,
331 u8 bank_addr, u8 sensor_addr, u8 *buf, int count, int retries)
333 int i;
335 /* Send the address */
336 i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
337 if (i)
338 return i;
340 /* And read the data */
341 for (i = 0; i < count; i++) {
342 if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
343 ABIT_UGURU_DEBUG(retries ? 1 : 3,
344 "timeout exceeded waiting for "
345 "read state (bank: %d, sensor: %d)\n",
346 (int)bank_addr, (int)sensor_addr);
347 break;
349 buf[i] = inb(data->addr + ABIT_UGURU_CMD);
352 /* Last put the chip back in ready state */
353 abituguru_ready(data);
355 return i;
358 /* Write count bytes from buf to sensor sensor_addr in bank bank_addr, the send
359 address part of the write is always retried ABIT_UGURU_MAX_RETRIES times. */
360 static int abituguru_write(struct abituguru_data *data,
361 u8 bank_addr, u8 sensor_addr, u8 *buf, int count)
363 /* We use the ready timeout as we have to wait for 0xAC just like the
364 ready function */
365 int i, timeout = ABIT_UGURU_READY_TIMEOUT;
367 /* Send the address */
368 i = abituguru_send_address(data, bank_addr, sensor_addr,
369 ABIT_UGURU_MAX_RETRIES);
370 if (i)
371 return i;
373 /* And write the data */
374 for (i = 0; i < count; i++) {
375 if (abituguru_wait(data, ABIT_UGURU_STATUS_WRITE)) {
376 ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
377 "write state (bank: %d, sensor: %d)\n",
378 (int)bank_addr, (int)sensor_addr);
379 break;
381 outb(buf[i], data->addr + ABIT_UGURU_CMD);
384 /* Now we need to wait till the chip is ready to be read again,
385 so that we can read 0xAC as confirmation that our write has
386 succeeded. */
387 if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
388 ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for read state "
389 "after write (bank: %d, sensor: %d)\n", (int)bank_addr,
390 (int)sensor_addr);
391 return -EIO;
394 /* Cmd port MUST be read now and should contain 0xAC */
395 while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
396 timeout--;
397 if (timeout == 0) {
398 ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after "
399 "write (bank: %d, sensor: %d)\n",
400 (int)bank_addr, (int)sensor_addr);
401 return -EIO;
403 msleep(0);
406 /* Last put the chip back in ready state */
407 abituguru_ready(data);
409 return i;
412 /* Detect sensor type. Temp and Volt sensors are enabled with
413 different masks and will ignore enable masks not meant for them.
414 This enables us to test what kind of sensor we're dealing with.
415 By setting the alarm thresholds so that we will always get an
416 alarm for sensor type X and then enabling the sensor as sensor type
417 X, if we then get an alarm it is a sensor of type X. */
418 static int __devinit
419 abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
420 u8 sensor_addr)
422 u8 val, test_flag, buf[3];
423 int i, ret = -ENODEV; /* error is the most common used retval :| */
425 /* If overriden by the user return the user selected type */
426 if (bank1_types[sensor_addr] >= ABIT_UGURU_IN_SENSOR &&
427 bank1_types[sensor_addr] <= ABIT_UGURU_NC) {
428 ABIT_UGURU_DEBUG(2, "assuming sensor type %d for bank1 sensor "
429 "%d because of \"bank1_types\" module param\n",
430 bank1_types[sensor_addr], (int)sensor_addr);
431 return bank1_types[sensor_addr];
434 /* First read the sensor and the current settings */
435 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, sensor_addr, &val,
436 1, ABIT_UGURU_MAX_RETRIES) != 1)
437 return -ENODEV;
439 /* Test val is sane / usable for sensor type detection. */
440 if ((val < 10u) || (val > 250u)) {
441 printk(KERN_WARNING ABIT_UGURU_NAME
442 ": bank1-sensor: %d reading (%d) too close to limits, "
443 "unable to determine sensor type, skipping sensor\n",
444 (int)sensor_addr, (int)val);
445 /* assume no sensor is there for sensors for which we can't
446 determine the sensor type because their reading is too close
447 to their limits, this usually means no sensor is there. */
448 return ABIT_UGURU_NC;
451 ABIT_UGURU_DEBUG(2, "testing bank1 sensor %d\n", (int)sensor_addr);
452 /* Volt sensor test, enable volt low alarm, set min value ridicously
453 high, or vica versa if the reading is very high. If its a volt
454 sensor this should always give us an alarm. */
455 if (val <= 240u) {
456 buf[0] = ABIT_UGURU_VOLT_LOW_ALARM_ENABLE;
457 buf[1] = 245;
458 buf[2] = 250;
459 test_flag = ABIT_UGURU_VOLT_LOW_ALARM_FLAG;
460 } else {
461 buf[0] = ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE;
462 buf[1] = 5;
463 buf[2] = 10;
464 test_flag = ABIT_UGURU_VOLT_HIGH_ALARM_FLAG;
467 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
468 buf, 3) != 3)
469 goto abituguru_detect_bank1_sensor_type_exit;
470 /* Now we need 20 ms to give the uguru time to read the sensors
471 and raise a voltage alarm */
472 set_current_state(TASK_UNINTERRUPTIBLE);
473 schedule_timeout(HZ/50);
474 /* Check for alarm and check the alarm is a volt low alarm. */
475 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
476 ABIT_UGURU_MAX_RETRIES) != 3)
477 goto abituguru_detect_bank1_sensor_type_exit;
478 if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
479 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
480 sensor_addr, buf, 3,
481 ABIT_UGURU_MAX_RETRIES) != 3)
482 goto abituguru_detect_bank1_sensor_type_exit;
483 if (buf[0] & test_flag) {
484 ABIT_UGURU_DEBUG(2, " found volt sensor\n");
485 ret = ABIT_UGURU_IN_SENSOR;
486 goto abituguru_detect_bank1_sensor_type_exit;
487 } else
488 ABIT_UGURU_DEBUG(2, " alarm raised during volt "
489 "sensor test, but volt range flag not set\n");
490 } else
491 ABIT_UGURU_DEBUG(2, " alarm not raised during volt sensor "
492 "test\n");
494 /* Temp sensor test, enable sensor as a temp sensor, set beep value
495 ridicously low (but not too low, otherwise uguru ignores it).
496 If its a temp sensor this should always give us an alarm. */
497 buf[0] = ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE;
498 buf[1] = 5;
499 buf[2] = 10;
500 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
501 buf, 3) != 3)
502 goto abituguru_detect_bank1_sensor_type_exit;
503 /* Now we need 50 ms to give the uguru time to read the sensors
504 and raise a temp alarm */
505 set_current_state(TASK_UNINTERRUPTIBLE);
506 schedule_timeout(HZ/20);
507 /* Check for alarm and check the alarm is a temp high alarm. */
508 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
509 ABIT_UGURU_MAX_RETRIES) != 3)
510 goto abituguru_detect_bank1_sensor_type_exit;
511 if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
512 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
513 sensor_addr, buf, 3,
514 ABIT_UGURU_MAX_RETRIES) != 3)
515 goto abituguru_detect_bank1_sensor_type_exit;
516 if (buf[0] & ABIT_UGURU_TEMP_HIGH_ALARM_FLAG) {
517 ABIT_UGURU_DEBUG(2, " found temp sensor\n");
518 ret = ABIT_UGURU_TEMP_SENSOR;
519 goto abituguru_detect_bank1_sensor_type_exit;
520 } else
521 ABIT_UGURU_DEBUG(2, " alarm raised during temp "
522 "sensor test, but temp high flag not set\n");
523 } else
524 ABIT_UGURU_DEBUG(2, " alarm not raised during temp sensor "
525 "test\n");
527 ret = ABIT_UGURU_NC;
528 abituguru_detect_bank1_sensor_type_exit:
529 /* Restore original settings, failing here is really BAD, it has been
530 reported that some BIOS-es hang when entering the uGuru menu with
531 invalid settings present in the uGuru, so we try this 3 times. */
532 for (i = 0; i < 3; i++)
533 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
534 sensor_addr, data->bank1_settings[sensor_addr],
535 3) == 3)
536 break;
537 if (i == 3) {
538 printk(KERN_ERR ABIT_UGURU_NAME
539 ": Fatal error could not restore original settings. "
540 "This should never happen please report this to the "
541 "abituguru maintainer (see MAINTAINERS)\n");
542 return -ENODEV;
544 return ret;
547 /* These functions try to find out how many sensors there are in bank2 and how
548 many pwms there are. The purpose of this is to make sure that we don't give
549 the user the possibility to change settings for non-existent sensors / pwm.
550 The uGuru will happily read / write whatever memory happens to be after the
551 memory storing the PWM settings when reading/writing to a PWM which is not
552 there. Notice even if we detect a PWM which doesn't exist we normally won't
553 write to it, unless the user tries to change the settings.
555 Although the uGuru allows reading (settings) from non existing bank2
556 sensors, my version of the uGuru does seem to stop writing to them, the
557 write function above aborts in this case with:
558 "CMD reg does not hold 0xAC after write"
560 Notice these 2 tests are non destructive iow read-only tests, otherwise
561 they would defeat their purpose. Although for the bank2_sensors detection a
562 read/write test would be feasible because of the reaction above, I've
563 however opted to stay on the safe side. */
564 static void __devinit
565 abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
567 int i;
569 if (fan_sensors > 0 && fan_sensors <= ABIT_UGURU_MAX_BANK2_SENSORS) {
570 data->bank2_sensors = fan_sensors;
571 ABIT_UGURU_DEBUG(2, "assuming %d fan sensors because of "
572 "\"fan_sensors\" module param\n",
573 (int)data->bank2_sensors);
574 return;
577 ABIT_UGURU_DEBUG(2, "detecting number of fan sensors\n");
578 for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
579 /* 0x89 are the known used bits:
580 -0x80 enable shutdown
581 -0x08 enable beep
582 -0x01 enable alarm
583 All other bits should be 0, but on some motherboards
584 0x40 (bit 6) is also high for some of the fans?? */
585 if (data->bank2_settings[i][0] & ~0xC9) {
586 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
587 "to be a fan sensor: settings[0] = %02X\n",
588 i, (unsigned int)data->bank2_settings[i][0]);
589 break;
592 /* check if the threshold is within the allowed range */
593 if (data->bank2_settings[i][1] <
594 abituguru_bank2_min_threshold) {
595 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
596 "to be a fan sensor: the threshold (%d) is "
597 "below the minimum (%d)\n", i,
598 (int)data->bank2_settings[i][1],
599 (int)abituguru_bank2_min_threshold);
600 break;
602 if (data->bank2_settings[i][1] >
603 abituguru_bank2_max_threshold) {
604 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
605 "to be a fan sensor: the threshold (%d) is "
606 "above the maximum (%d)\n", i,
607 (int)data->bank2_settings[i][1],
608 (int)abituguru_bank2_max_threshold);
609 break;
613 data->bank2_sensors = i;
614 ABIT_UGURU_DEBUG(2, " found: %d fan sensors\n",
615 (int)data->bank2_sensors);
618 static void __devinit
619 abituguru_detect_no_pwms(struct abituguru_data *data)
621 int i, j;
623 if (pwms > 0 && pwms <= ABIT_UGURU_MAX_PWMS) {
624 data->pwms = pwms;
625 ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
626 "\"pwms\" module param\n", (int)data->pwms);
627 return;
630 ABIT_UGURU_DEBUG(2, "detecting number of PWM outputs\n");
631 for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
632 /* 0x80 is the enable bit and the low
633 nibble is which temp sensor to use,
634 the other bits should be 0 */
635 if (data->pwm_settings[i][0] & ~0x8F) {
636 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
637 "to be a pwm channel: settings[0] = %02X\n",
638 i, (unsigned int)data->pwm_settings[i][0]);
639 break;
642 /* the low nibble must correspond to one of the temp sensors
643 we've found */
644 for (j = 0; j < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR];
645 j++) {
646 if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][j] ==
647 (data->pwm_settings[i][0] & 0x0F))
648 break;
650 if (j == data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
651 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
652 "to be a pwm channel: %d is not a valid temp "
653 "sensor address\n", i,
654 data->pwm_settings[i][0] & 0x0F);
655 break;
658 /* check if all other settings are within the allowed range */
659 for (j = 1; j < 5; j++) {
660 u8 min;
661 /* special case pwm1 min pwm% */
662 if ((i == 0) && ((j == 1) || (j == 2)))
663 min = 77;
664 else
665 min = abituguru_pwm_min[j];
666 if (data->pwm_settings[i][j] < min) {
667 ABIT_UGURU_DEBUG(2, " pwm channel %d does "
668 "not seem to be a pwm channel: "
669 "setting %d (%d) is below the minimum "
670 "value (%d)\n", i, j,
671 (int)data->pwm_settings[i][j],
672 (int)min);
673 goto abituguru_detect_no_pwms_exit;
675 if (data->pwm_settings[i][j] > abituguru_pwm_max[j]) {
676 ABIT_UGURU_DEBUG(2, " pwm channel %d does "
677 "not seem to be a pwm channel: "
678 "setting %d (%d) is above the maximum "
679 "value (%d)\n", i, j,
680 (int)data->pwm_settings[i][j],
681 (int)abituguru_pwm_max[j]);
682 goto abituguru_detect_no_pwms_exit;
686 /* check that min temp < max temp and min pwm < max pwm */
687 if (data->pwm_settings[i][1] >= data->pwm_settings[i][2]) {
688 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
689 "to be a pwm channel: min pwm (%d) >= "
690 "max pwm (%d)\n", i,
691 (int)data->pwm_settings[i][1],
692 (int)data->pwm_settings[i][2]);
693 break;
695 if (data->pwm_settings[i][3] >= data->pwm_settings[i][4]) {
696 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
697 "to be a pwm channel: min temp (%d) >= "
698 "max temp (%d)\n", i,
699 (int)data->pwm_settings[i][3],
700 (int)data->pwm_settings[i][4]);
701 break;
705 abituguru_detect_no_pwms_exit:
706 data->pwms = i;
707 ABIT_UGURU_DEBUG(2, " found: %d PWM outputs\n", (int)data->pwms);
710 /* Following are the sysfs callback functions. These functions expect:
711 sensor_device_attribute_2->index: sensor address/offset in the bank
712 sensor_device_attribute_2->nr: register offset, bitmask or NA. */
713 static struct abituguru_data *abituguru_update_device(struct device *dev);
715 static ssize_t show_bank1_value(struct device *dev,
716 struct device_attribute *devattr, char *buf)
718 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
719 struct abituguru_data *data = abituguru_update_device(dev);
720 if (!data)
721 return -EIO;
722 return sprintf(buf, "%d\n", (data->bank1_value[attr->index] *
723 data->bank1_max_value[attr->index] + 128) / 255);
726 static ssize_t show_bank1_setting(struct device *dev,
727 struct device_attribute *devattr, char *buf)
729 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
730 struct abituguru_data *data = dev_get_drvdata(dev);
731 return sprintf(buf, "%d\n",
732 (data->bank1_settings[attr->index][attr->nr] *
733 data->bank1_max_value[attr->index] + 128) / 255);
736 static ssize_t show_bank2_value(struct device *dev,
737 struct device_attribute *devattr, char *buf)
739 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
740 struct abituguru_data *data = abituguru_update_device(dev);
741 if (!data)
742 return -EIO;
743 return sprintf(buf, "%d\n", (data->bank2_value[attr->index] *
744 ABIT_UGURU_FAN_MAX + 128) / 255);
747 static ssize_t show_bank2_setting(struct device *dev,
748 struct device_attribute *devattr, char *buf)
750 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
751 struct abituguru_data *data = dev_get_drvdata(dev);
752 return sprintf(buf, "%d\n",
753 (data->bank2_settings[attr->index][attr->nr] *
754 ABIT_UGURU_FAN_MAX + 128) / 255);
757 static ssize_t store_bank1_setting(struct device *dev, struct device_attribute
758 *devattr, const char *buf, size_t count)
760 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
761 struct abituguru_data *data = dev_get_drvdata(dev);
762 u8 val = (simple_strtoul(buf, NULL, 10) * 255 +
763 data->bank1_max_value[attr->index]/2) /
764 data->bank1_max_value[attr->index];
765 ssize_t ret = count;
767 mutex_lock(&data->update_lock);
768 if (data->bank1_settings[attr->index][attr->nr] != val) {
769 u8 orig_val = data->bank1_settings[attr->index][attr->nr];
770 data->bank1_settings[attr->index][attr->nr] = val;
771 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
772 attr->index, data->bank1_settings[attr->index],
773 3) <= attr->nr) {
774 data->bank1_settings[attr->index][attr->nr] = orig_val;
775 ret = -EIO;
778 mutex_unlock(&data->update_lock);
779 return ret;
782 static ssize_t store_bank2_setting(struct device *dev, struct device_attribute
783 *devattr, const char *buf, size_t count)
785 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
786 struct abituguru_data *data = dev_get_drvdata(dev);
787 u8 val = (simple_strtoul(buf, NULL, 10)*255 + ABIT_UGURU_FAN_MAX/2) /
788 ABIT_UGURU_FAN_MAX;
789 ssize_t ret = count;
791 /* this check can be done before taking the lock */
792 if ((val < abituguru_bank2_min_threshold) ||
793 (val > abituguru_bank2_max_threshold))
794 return -EINVAL;
796 mutex_lock(&data->update_lock);
797 if (data->bank2_settings[attr->index][attr->nr] != val) {
798 u8 orig_val = data->bank2_settings[attr->index][attr->nr];
799 data->bank2_settings[attr->index][attr->nr] = val;
800 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK2 + 2,
801 attr->index, data->bank2_settings[attr->index],
802 2) <= attr->nr) {
803 data->bank2_settings[attr->index][attr->nr] = orig_val;
804 ret = -EIO;
807 mutex_unlock(&data->update_lock);
808 return ret;
811 static ssize_t show_bank1_alarm(struct device *dev,
812 struct device_attribute *devattr, char *buf)
814 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
815 struct abituguru_data *data = abituguru_update_device(dev);
816 if (!data)
817 return -EIO;
818 /* See if the alarm bit for this sensor is set, and if the
819 alarm matches the type of alarm we're looking for (for volt
820 it can be either low or high). The type is stored in a few
821 readonly bits in the settings part of the relevant sensor.
822 The bitmask of the type is passed to us in attr->nr. */
823 if ((data->alarms[attr->index / 8] & (0x01 << (attr->index % 8))) &&
824 (data->bank1_settings[attr->index][0] & attr->nr))
825 return sprintf(buf, "1\n");
826 else
827 return sprintf(buf, "0\n");
830 static ssize_t show_bank2_alarm(struct device *dev,
831 struct device_attribute *devattr, char *buf)
833 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
834 struct abituguru_data *data = abituguru_update_device(dev);
835 if (!data)
836 return -EIO;
837 if (data->alarms[2] & (0x01 << attr->index))
838 return sprintf(buf, "1\n");
839 else
840 return sprintf(buf, "0\n");
843 static ssize_t show_bank1_mask(struct device *dev,
844 struct device_attribute *devattr, char *buf)
846 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
847 struct abituguru_data *data = dev_get_drvdata(dev);
848 if (data->bank1_settings[attr->index][0] & attr->nr)
849 return sprintf(buf, "1\n");
850 else
851 return sprintf(buf, "0\n");
854 static ssize_t show_bank2_mask(struct device *dev,
855 struct device_attribute *devattr, char *buf)
857 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
858 struct abituguru_data *data = dev_get_drvdata(dev);
859 if (data->bank2_settings[attr->index][0] & attr->nr)
860 return sprintf(buf, "1\n");
861 else
862 return sprintf(buf, "0\n");
865 static ssize_t store_bank1_mask(struct device *dev,
866 struct device_attribute *devattr, const char *buf, size_t count)
868 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
869 struct abituguru_data *data = dev_get_drvdata(dev);
870 int mask = simple_strtoul(buf, NULL, 10);
871 ssize_t ret = count;
872 u8 orig_val;
874 mutex_lock(&data->update_lock);
875 orig_val = data->bank1_settings[attr->index][0];
877 if (mask)
878 data->bank1_settings[attr->index][0] |= attr->nr;
879 else
880 data->bank1_settings[attr->index][0] &= ~attr->nr;
882 if ((data->bank1_settings[attr->index][0] != orig_val) &&
883 (abituguru_write(data,
884 ABIT_UGURU_SENSOR_BANK1 + 2, attr->index,
885 data->bank1_settings[attr->index], 3) < 1)) {
886 data->bank1_settings[attr->index][0] = orig_val;
887 ret = -EIO;
889 mutex_unlock(&data->update_lock);
890 return ret;
893 static ssize_t store_bank2_mask(struct device *dev,
894 struct device_attribute *devattr, const char *buf, size_t count)
896 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
897 struct abituguru_data *data = dev_get_drvdata(dev);
898 int mask = simple_strtoul(buf, NULL, 10);
899 ssize_t ret = count;
900 u8 orig_val;
902 mutex_lock(&data->update_lock);
903 orig_val = data->bank2_settings[attr->index][0];
905 if (mask)
906 data->bank2_settings[attr->index][0] |= attr->nr;
907 else
908 data->bank2_settings[attr->index][0] &= ~attr->nr;
910 if ((data->bank2_settings[attr->index][0] != orig_val) &&
911 (abituguru_write(data,
912 ABIT_UGURU_SENSOR_BANK2 + 2, attr->index,
913 data->bank2_settings[attr->index], 2) < 1)) {
914 data->bank2_settings[attr->index][0] = orig_val;
915 ret = -EIO;
917 mutex_unlock(&data->update_lock);
918 return ret;
921 /* Fan PWM (speed control) */
922 static ssize_t show_pwm_setting(struct device *dev,
923 struct device_attribute *devattr, char *buf)
925 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
926 struct abituguru_data *data = dev_get_drvdata(dev);
927 return sprintf(buf, "%d\n", data->pwm_settings[attr->index][attr->nr] *
928 abituguru_pwm_settings_multiplier[attr->nr]);
931 static ssize_t store_pwm_setting(struct device *dev, struct device_attribute
932 *devattr, const char *buf, size_t count)
934 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
935 struct abituguru_data *data = dev_get_drvdata(dev);
936 u8 min, val = (simple_strtoul(buf, NULL, 10) +
937 abituguru_pwm_settings_multiplier[attr->nr]/2) /
938 abituguru_pwm_settings_multiplier[attr->nr];
939 ssize_t ret = count;
941 /* special case pwm1 min pwm% */
942 if ((attr->index == 0) && ((attr->nr == 1) || (attr->nr == 2)))
943 min = 77;
944 else
945 min = abituguru_pwm_min[attr->nr];
947 /* this check can be done before taking the lock */
948 if ((val < min) || (val > abituguru_pwm_max[attr->nr]))
949 return -EINVAL;
951 mutex_lock(&data->update_lock);
952 /* this check needs to be done after taking the lock */
953 if ((attr->nr & 1) &&
954 (val >= data->pwm_settings[attr->index][attr->nr + 1]))
955 ret = -EINVAL;
956 else if (!(attr->nr & 1) &&
957 (val <= data->pwm_settings[attr->index][attr->nr - 1]))
958 ret = -EINVAL;
959 else if (data->pwm_settings[attr->index][attr->nr] != val) {
960 u8 orig_val = data->pwm_settings[attr->index][attr->nr];
961 data->pwm_settings[attr->index][attr->nr] = val;
962 if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
963 attr->index, data->pwm_settings[attr->index],
964 5) <= attr->nr) {
965 data->pwm_settings[attr->index][attr->nr] =
966 orig_val;
967 ret = -EIO;
970 mutex_unlock(&data->update_lock);
971 return ret;
974 static ssize_t show_pwm_sensor(struct device *dev,
975 struct device_attribute *devattr, char *buf)
977 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
978 struct abituguru_data *data = dev_get_drvdata(dev);
979 int i;
980 /* We need to walk to the temp sensor addresses to find what
981 the userspace id of the configured temp sensor is. */
982 for (i = 0; i < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]; i++)
983 if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][i] ==
984 (data->pwm_settings[attr->index][0] & 0x0F))
985 return sprintf(buf, "%d\n", i+1);
987 return -ENXIO;
990 static ssize_t store_pwm_sensor(struct device *dev, struct device_attribute
991 *devattr, const char *buf, size_t count)
993 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
994 struct abituguru_data *data = dev_get_drvdata(dev);
995 unsigned long val = simple_strtoul(buf, NULL, 10) - 1;
996 ssize_t ret = count;
998 mutex_lock(&data->update_lock);
999 if (val < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
1000 u8 orig_val = data->pwm_settings[attr->index][0];
1001 u8 address = data->bank1_address[ABIT_UGURU_TEMP_SENSOR][val];
1002 data->pwm_settings[attr->index][0] &= 0xF0;
1003 data->pwm_settings[attr->index][0] |= address;
1004 if (data->pwm_settings[attr->index][0] != orig_val) {
1005 if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1006 attr->index,
1007 data->pwm_settings[attr->index],
1008 5) < 1) {
1009 data->pwm_settings[attr->index][0] = orig_val;
1010 ret = -EIO;
1014 else
1015 ret = -EINVAL;
1016 mutex_unlock(&data->update_lock);
1017 return ret;
1020 static ssize_t show_pwm_enable(struct device *dev,
1021 struct device_attribute *devattr, char *buf)
1023 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1024 struct abituguru_data *data = dev_get_drvdata(dev);
1025 int res = 0;
1026 if (data->pwm_settings[attr->index][0] & ABIT_UGURU_FAN_PWM_ENABLE)
1027 res = 2;
1028 return sprintf(buf, "%d\n", res);
1031 static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1032 *devattr, const char *buf, size_t count)
1034 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1035 struct abituguru_data *data = dev_get_drvdata(dev);
1036 u8 orig_val, user_val = simple_strtoul(buf, NULL, 10);
1037 ssize_t ret = count;
1039 mutex_lock(&data->update_lock);
1040 orig_val = data->pwm_settings[attr->index][0];
1041 switch (user_val) {
1042 case 0:
1043 data->pwm_settings[attr->index][0] &=
1044 ~ABIT_UGURU_FAN_PWM_ENABLE;
1045 break;
1046 case 2:
1047 data->pwm_settings[attr->index][0] |=
1048 ABIT_UGURU_FAN_PWM_ENABLE;
1049 break;
1050 default:
1051 ret = -EINVAL;
1053 if ((data->pwm_settings[attr->index][0] != orig_val) &&
1054 (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1055 attr->index, data->pwm_settings[attr->index],
1056 5) < 1)) {
1057 data->pwm_settings[attr->index][0] = orig_val;
1058 ret = -EIO;
1060 mutex_unlock(&data->update_lock);
1061 return ret;
1064 static ssize_t show_name(struct device *dev,
1065 struct device_attribute *devattr, char *buf)
1067 return sprintf(buf, "%s\n", ABIT_UGURU_NAME);
1070 /* Sysfs attr templates, the real entries are generated automatically. */
1071 static const
1072 struct sensor_device_attribute_2 abituguru_sysfs_bank1_templ[2][9] = {
1074 SENSOR_ATTR_2(in%d_input, 0444, show_bank1_value, NULL, 0, 0),
1075 SENSOR_ATTR_2(in%d_min, 0644, show_bank1_setting,
1076 store_bank1_setting, 1, 0),
1077 SENSOR_ATTR_2(in%d_min_alarm, 0444, show_bank1_alarm, NULL,
1078 ABIT_UGURU_VOLT_LOW_ALARM_FLAG, 0),
1079 SENSOR_ATTR_2(in%d_max, 0644, show_bank1_setting,
1080 store_bank1_setting, 2, 0),
1081 SENSOR_ATTR_2(in%d_max_alarm, 0444, show_bank1_alarm, NULL,
1082 ABIT_UGURU_VOLT_HIGH_ALARM_FLAG, 0),
1083 SENSOR_ATTR_2(in%d_beep, 0644, show_bank1_mask,
1084 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1085 SENSOR_ATTR_2(in%d_shutdown, 0644, show_bank1_mask,
1086 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1087 SENSOR_ATTR_2(in%d_min_alarm_enable, 0644, show_bank1_mask,
1088 store_bank1_mask, ABIT_UGURU_VOLT_LOW_ALARM_ENABLE, 0),
1089 SENSOR_ATTR_2(in%d_max_alarm_enable, 0644, show_bank1_mask,
1090 store_bank1_mask, ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE, 0),
1091 }, {
1092 SENSOR_ATTR_2(temp%d_input, 0444, show_bank1_value, NULL, 0, 0),
1093 SENSOR_ATTR_2(temp%d_alarm, 0444, show_bank1_alarm, NULL,
1094 ABIT_UGURU_TEMP_HIGH_ALARM_FLAG, 0),
1095 SENSOR_ATTR_2(temp%d_max, 0644, show_bank1_setting,
1096 store_bank1_setting, 1, 0),
1097 SENSOR_ATTR_2(temp%d_crit, 0644, show_bank1_setting,
1098 store_bank1_setting, 2, 0),
1099 SENSOR_ATTR_2(temp%d_beep, 0644, show_bank1_mask,
1100 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1101 SENSOR_ATTR_2(temp%d_shutdown, 0644, show_bank1_mask,
1102 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1103 SENSOR_ATTR_2(temp%d_alarm_enable, 0644, show_bank1_mask,
1104 store_bank1_mask, ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE, 0),
1108 static const struct sensor_device_attribute_2 abituguru_sysfs_fan_templ[6] = {
1109 SENSOR_ATTR_2(fan%d_input, 0444, show_bank2_value, NULL, 0, 0),
1110 SENSOR_ATTR_2(fan%d_alarm, 0444, show_bank2_alarm, NULL, 0, 0),
1111 SENSOR_ATTR_2(fan%d_min, 0644, show_bank2_setting,
1112 store_bank2_setting, 1, 0),
1113 SENSOR_ATTR_2(fan%d_beep, 0644, show_bank2_mask,
1114 store_bank2_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1115 SENSOR_ATTR_2(fan%d_shutdown, 0644, show_bank2_mask,
1116 store_bank2_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1117 SENSOR_ATTR_2(fan%d_alarm_enable, 0644, show_bank2_mask,
1118 store_bank2_mask, ABIT_UGURU_FAN_LOW_ALARM_ENABLE, 0),
1121 static const struct sensor_device_attribute_2 abituguru_sysfs_pwm_templ[6] = {
1122 SENSOR_ATTR_2(pwm%d_enable, 0644, show_pwm_enable,
1123 store_pwm_enable, 0, 0),
1124 SENSOR_ATTR_2(pwm%d_auto_channels_temp, 0644, show_pwm_sensor,
1125 store_pwm_sensor, 0, 0),
1126 SENSOR_ATTR_2(pwm%d_auto_point1_pwm, 0644, show_pwm_setting,
1127 store_pwm_setting, 1, 0),
1128 SENSOR_ATTR_2(pwm%d_auto_point2_pwm, 0644, show_pwm_setting,
1129 store_pwm_setting, 2, 0),
1130 SENSOR_ATTR_2(pwm%d_auto_point1_temp, 0644, show_pwm_setting,
1131 store_pwm_setting, 3, 0),
1132 SENSOR_ATTR_2(pwm%d_auto_point2_temp, 0644, show_pwm_setting,
1133 store_pwm_setting, 4, 0),
1136 static struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
1137 SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
1140 static int __devinit abituguru_probe(struct platform_device *pdev)
1142 struct abituguru_data *data;
1143 int i, j, used, sysfs_names_free, sysfs_attr_i, res = -ENODEV;
1144 char *sysfs_filename;
1146 /* El weirdo probe order, to keep the sysfs order identical to the
1147 BIOS and window-appliction listing order. */
1148 const u8 probe_order[ABIT_UGURU_MAX_BANK1_SENSORS] = {
1149 0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E, 0x02,
1150 0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
1152 if (!(data = kzalloc(sizeof(struct abituguru_data), GFP_KERNEL)))
1153 return -ENOMEM;
1155 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
1156 mutex_init(&data->update_lock);
1157 platform_set_drvdata(pdev, data);
1159 /* See if the uGuru is ready */
1160 if (inb_p(data->addr + ABIT_UGURU_DATA) == ABIT_UGURU_STATUS_INPUT)
1161 data->uguru_ready = 1;
1163 /* Completely read the uGuru this has 2 purposes:
1164 - testread / see if one really is there.
1165 - make an in memory copy of all the uguru settings for future use. */
1166 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1167 data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3)
1168 goto abituguru_probe_error;
1170 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1171 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, i,
1172 &data->bank1_value[i], 1,
1173 ABIT_UGURU_MAX_RETRIES) != 1)
1174 goto abituguru_probe_error;
1175 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1+1, i,
1176 data->bank1_settings[i], 3,
1177 ABIT_UGURU_MAX_RETRIES) != 3)
1178 goto abituguru_probe_error;
1180 /* Note: We don't know how many bank2 sensors / pwms there really are,
1181 but in order to "detect" this we need to read the maximum amount
1182 anyways. If we read sensors/pwms not there we'll just read crap
1183 this can't hurt. We need the detection because we don't want
1184 unwanted writes, which will hurt! */
1185 for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
1186 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
1187 &data->bank2_value[i], 1,
1188 ABIT_UGURU_MAX_RETRIES) != 1)
1189 goto abituguru_probe_error;
1190 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2+1, i,
1191 data->bank2_settings[i], 2,
1192 ABIT_UGURU_MAX_RETRIES) != 2)
1193 goto abituguru_probe_error;
1195 for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
1196 if (abituguru_read(data, ABIT_UGURU_FAN_PWM, i,
1197 data->pwm_settings[i], 5,
1198 ABIT_UGURU_MAX_RETRIES) != 5)
1199 goto abituguru_probe_error;
1201 data->last_updated = jiffies;
1203 /* Detect sensor types and fill the sysfs attr for bank1 */
1204 sysfs_attr_i = 0;
1205 sysfs_filename = data->sysfs_names;
1206 sysfs_names_free = ABITUGURU_SYSFS_NAMES_LENGTH;
1207 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1208 res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
1209 if (res < 0)
1210 goto abituguru_probe_error;
1211 if (res == ABIT_UGURU_NC)
1212 continue;
1214 /* res 1 (temp) sensors have 7 sysfs entries, 0 (in) 9 */
1215 for (j = 0; j < (res ? 7 : 9); j++) {
1216 used = snprintf(sysfs_filename, sysfs_names_free,
1217 abituguru_sysfs_bank1_templ[res][j].dev_attr.
1218 attr.name, data->bank1_sensors[res] + res)
1219 + 1;
1220 data->sysfs_attr[sysfs_attr_i] =
1221 abituguru_sysfs_bank1_templ[res][j];
1222 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1223 sysfs_filename;
1224 data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
1225 sysfs_filename += used;
1226 sysfs_names_free -= used;
1227 sysfs_attr_i++;
1229 data->bank1_max_value[probe_order[i]] =
1230 abituguru_bank1_max_value[res];
1231 data->bank1_address[res][data->bank1_sensors[res]] =
1232 probe_order[i];
1233 data->bank1_sensors[res]++;
1235 /* Detect number of sensors and fill the sysfs attr for bank2 (fans) */
1236 abituguru_detect_no_bank2_sensors(data);
1237 for (i = 0; i < data->bank2_sensors; i++) {
1238 for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_fan_templ); j++) {
1239 used = snprintf(sysfs_filename, sysfs_names_free,
1240 abituguru_sysfs_fan_templ[j].dev_attr.attr.name,
1241 i + 1) + 1;
1242 data->sysfs_attr[sysfs_attr_i] =
1243 abituguru_sysfs_fan_templ[j];
1244 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1245 sysfs_filename;
1246 data->sysfs_attr[sysfs_attr_i].index = i;
1247 sysfs_filename += used;
1248 sysfs_names_free -= used;
1249 sysfs_attr_i++;
1252 /* Detect number of sensors and fill the sysfs attr for pwms */
1253 abituguru_detect_no_pwms(data);
1254 for (i = 0; i < data->pwms; i++) {
1255 for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_pwm_templ); j++) {
1256 used = snprintf(sysfs_filename, sysfs_names_free,
1257 abituguru_sysfs_pwm_templ[j].dev_attr.attr.name,
1258 i + 1) + 1;
1259 data->sysfs_attr[sysfs_attr_i] =
1260 abituguru_sysfs_pwm_templ[j];
1261 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1262 sysfs_filename;
1263 data->sysfs_attr[sysfs_attr_i].index = i;
1264 sysfs_filename += used;
1265 sysfs_names_free -= used;
1266 sysfs_attr_i++;
1269 /* Fail safe check, this should never happen! */
1270 if (sysfs_names_free < 0) {
1271 printk(KERN_ERR ABIT_UGURU_NAME ": Fatal error ran out of "
1272 "space for sysfs attr names. This should never "
1273 "happen please report to the abituguru maintainer "
1274 "(see MAINTAINERS)\n");
1275 res = -ENAMETOOLONG;
1276 goto abituguru_probe_error;
1278 printk(KERN_INFO ABIT_UGURU_NAME ": found Abit uGuru\n");
1280 /* Register sysfs hooks */
1281 for (i = 0; i < sysfs_attr_i; i++)
1282 if (device_create_file(&pdev->dev,
1283 &data->sysfs_attr[i].dev_attr))
1284 goto abituguru_probe_error;
1285 for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1286 if (device_create_file(&pdev->dev,
1287 &abituguru_sysfs_attr[i].dev_attr))
1288 goto abituguru_probe_error;
1290 data->hwmon_dev = hwmon_device_register(&pdev->dev);
1291 if (!IS_ERR(data->hwmon_dev))
1292 return 0; /* success */
1294 res = PTR_ERR(data->hwmon_dev);
1295 abituguru_probe_error:
1296 for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
1297 device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1298 for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1299 device_remove_file(&pdev->dev,
1300 &abituguru_sysfs_attr[i].dev_attr);
1301 platform_set_drvdata(pdev, NULL);
1302 kfree(data);
1303 return res;
1306 static int __devexit abituguru_remove(struct platform_device *pdev)
1308 int i;
1309 struct abituguru_data *data = platform_get_drvdata(pdev);
1311 hwmon_device_unregister(data->hwmon_dev);
1312 for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
1313 device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1314 for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1315 device_remove_file(&pdev->dev,
1316 &abituguru_sysfs_attr[i].dev_attr);
1317 platform_set_drvdata(pdev, NULL);
1318 kfree(data);
1320 return 0;
1323 static struct abituguru_data *abituguru_update_device(struct device *dev)
1325 int i, err;
1326 struct abituguru_data *data = dev_get_drvdata(dev);
1327 /* fake a complete successful read if no update necessary. */
1328 char success = 1;
1330 mutex_lock(&data->update_lock);
1331 if (time_after(jiffies, data->last_updated + HZ)) {
1332 success = 0;
1333 if ((err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1334 data->alarms, 3, 0)) != 3)
1335 goto LEAVE_UPDATE;
1336 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1337 if ((err = abituguru_read(data,
1338 ABIT_UGURU_SENSOR_BANK1, i,
1339 &data->bank1_value[i], 1, 0)) != 1)
1340 goto LEAVE_UPDATE;
1341 if ((err = abituguru_read(data,
1342 ABIT_UGURU_SENSOR_BANK1 + 1, i,
1343 data->bank1_settings[i], 3, 0)) != 3)
1344 goto LEAVE_UPDATE;
1346 for (i = 0; i < data->bank2_sensors; i++)
1347 if ((err = abituguru_read(data,
1348 ABIT_UGURU_SENSOR_BANK2, i,
1349 &data->bank2_value[i], 1, 0)) != 1)
1350 goto LEAVE_UPDATE;
1351 /* success! */
1352 success = 1;
1353 data->update_timeouts = 0;
1354 LEAVE_UPDATE:
1355 /* handle timeout condition */
1356 if (!success && (err == -EBUSY || err >= 0)) {
1357 /* No overflow please */
1358 if (data->update_timeouts < 255u)
1359 data->update_timeouts++;
1360 if (data->update_timeouts <= ABIT_UGURU_MAX_TIMEOUTS) {
1361 ABIT_UGURU_DEBUG(3, "timeout exceeded, will "
1362 "try again next update\n");
1363 /* Just a timeout, fake a successful read */
1364 success = 1;
1365 } else
1366 ABIT_UGURU_DEBUG(1, "timeout exceeded %d "
1367 "times waiting for more input state\n",
1368 (int)data->update_timeouts);
1370 /* On success set last_updated */
1371 if (success)
1372 data->last_updated = jiffies;
1374 mutex_unlock(&data->update_lock);
1376 if (success)
1377 return data;
1378 else
1379 return NULL;
1382 #ifdef CONFIG_PM
1383 static int abituguru_suspend(struct platform_device *pdev, pm_message_t state)
1385 struct abituguru_data *data = platform_get_drvdata(pdev);
1386 /* make sure all communications with the uguru are done and no new
1387 ones are started */
1388 mutex_lock(&data->update_lock);
1389 return 0;
1392 static int abituguru_resume(struct platform_device *pdev)
1394 struct abituguru_data *data = platform_get_drvdata(pdev);
1395 /* See if the uGuru is still ready */
1396 if (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT)
1397 data->uguru_ready = 0;
1398 mutex_unlock(&data->update_lock);
1399 return 0;
1401 #else
1402 #define abituguru_suspend NULL
1403 #define abituguru_resume NULL
1404 #endif /* CONFIG_PM */
1406 static struct platform_driver abituguru_driver = {
1407 .driver = {
1408 .owner = THIS_MODULE,
1409 .name = ABIT_UGURU_NAME,
1411 .probe = abituguru_probe,
1412 .remove = __devexit_p(abituguru_remove),
1413 .suspend = abituguru_suspend,
1414 .resume = abituguru_resume,
1417 static int __init abituguru_detect(void)
1419 /* See if there is an uguru there. After a reboot uGuru will hold 0x00
1420 at DATA and 0xAC, when this driver has already been loaded once
1421 DATA will hold 0x08. For most uGuru's CMD will hold 0xAC in either
1422 scenario but some will hold 0x00.
1423 Some uGuru's initally hold 0x09 at DATA and will only hold 0x08
1424 after reading CMD first, so CMD must be read first! */
1425 u8 cmd_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_CMD);
1426 u8 data_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_DATA);
1427 if (((data_val == 0x00) || (data_val == 0x08)) &&
1428 ((cmd_val == 0x00) || (cmd_val == 0xAC)))
1429 return ABIT_UGURU_BASE;
1431 ABIT_UGURU_DEBUG(2, "no Abit uGuru found, data = 0x%02X, cmd = "
1432 "0x%02X\n", (unsigned int)data_val, (unsigned int)cmd_val);
1434 if (force) {
1435 printk(KERN_INFO ABIT_UGURU_NAME ": Assuming Abit uGuru is "
1436 "present because of \"force\" parameter\n");
1437 return ABIT_UGURU_BASE;
1440 /* No uGuru found */
1441 return -ENODEV;
1444 static struct platform_device *abituguru_pdev;
1446 static int __init abituguru_init(void)
1448 int address, err;
1449 struct resource res = { .flags = IORESOURCE_IO };
1451 #ifdef CONFIG_DMI
1452 const char *board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1454 /* safety check, refuse to load on non Abit motherboards */
1455 if (!force && (!board_vendor ||
1456 strcmp(board_vendor, "http://www.abit.com.tw/")))
1457 return -ENODEV;
1458 #endif
1460 address = abituguru_detect();
1461 if (address < 0)
1462 return address;
1464 err = platform_driver_register(&abituguru_driver);
1465 if (err)
1466 goto exit;
1468 abituguru_pdev = platform_device_alloc(ABIT_UGURU_NAME, address);
1469 if (!abituguru_pdev) {
1470 printk(KERN_ERR ABIT_UGURU_NAME
1471 ": Device allocation failed\n");
1472 err = -ENOMEM;
1473 goto exit_driver_unregister;
1476 res.start = address;
1477 res.end = address + ABIT_UGURU_REGION_LENGTH - 1;
1478 res.name = ABIT_UGURU_NAME;
1480 err = platform_device_add_resources(abituguru_pdev, &res, 1);
1481 if (err) {
1482 printk(KERN_ERR ABIT_UGURU_NAME
1483 ": Device resource addition failed (%d)\n", err);
1484 goto exit_device_put;
1487 err = platform_device_add(abituguru_pdev);
1488 if (err) {
1489 printk(KERN_ERR ABIT_UGURU_NAME
1490 ": Device addition failed (%d)\n", err);
1491 goto exit_device_put;
1494 return 0;
1496 exit_device_put:
1497 platform_device_put(abituguru_pdev);
1498 exit_driver_unregister:
1499 platform_driver_unregister(&abituguru_driver);
1500 exit:
1501 return err;
1504 static void __exit abituguru_exit(void)
1506 platform_device_unregister(abituguru_pdev);
1507 platform_driver_unregister(&abituguru_driver);
1510 MODULE_AUTHOR("Hans de Goede <j.w.r.degoede@hhs.nl>");
1511 MODULE_DESCRIPTION("Abit uGuru Sensor device");
1512 MODULE_LICENSE("GPL");
1514 module_init(abituguru_init);
1515 module_exit(abituguru_exit);