[NETFILTER]: Kconfig: improve conntrack selection
[linux-2.6.22.y-op.git] / drivers / hwmon / abituguru.c
blobe5cb0fdab9b15d8529274531a8879a31dd2cb0a0
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 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/delay.h>
30 #include <linux/platform_device.h>
31 #include <linux/hwmon.h>
32 #include <linux/hwmon-sysfs.h>
33 #include <asm/io.h>
35 /* Banks */
36 #define ABIT_UGURU_ALARM_BANK 0x20 /* 1x 3 bytes */
37 #define ABIT_UGURU_SENSOR_BANK1 0x21 /* 16x volt and temp */
38 #define ABIT_UGURU_FAN_PWM 0x24 /* 3x 5 bytes */
39 #define ABIT_UGURU_SENSOR_BANK2 0x26 /* fans */
40 /* max nr of sensors in bank1, a bank1 sensor can be in, temp or nc */
41 #define ABIT_UGURU_MAX_BANK1_SENSORS 16
42 /* Warning if you increase one of the 2 MAX defines below to 10 or higher you
43 should adjust the belonging _NAMES_LENGTH macro for the 2 digit number! */
44 /* max nr of sensors in bank2, currently mb's with max 6 fans are known */
45 #define ABIT_UGURU_MAX_BANK2_SENSORS 6
46 /* max nr of pwm outputs, currently mb's with max 5 pwm outputs are known */
47 #define ABIT_UGURU_MAX_PWMS 5
48 /* uGuru sensor bank 1 flags */ /* Alarm if: */
49 #define ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE 0x01 /* temp over warn */
50 #define ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE 0x02 /* volt over max */
51 #define ABIT_UGURU_VOLT_LOW_ALARM_ENABLE 0x04 /* volt under min */
52 #define ABIT_UGURU_TEMP_HIGH_ALARM_FLAG 0x10 /* temp is over warn */
53 #define ABIT_UGURU_VOLT_HIGH_ALARM_FLAG 0x20 /* volt is over max */
54 #define ABIT_UGURU_VOLT_LOW_ALARM_FLAG 0x40 /* volt is under min */
55 /* uGuru sensor bank 2 flags */ /* Alarm if: */
56 #define ABIT_UGURU_FAN_LOW_ALARM_ENABLE 0x01 /* fan under min */
57 /* uGuru sensor bank common flags */
58 #define ABIT_UGURU_BEEP_ENABLE 0x08 /* beep if alarm */
59 #define ABIT_UGURU_SHUTDOWN_ENABLE 0x80 /* shutdown if alarm */
60 /* uGuru fan PWM (speed control) flags */
61 #define ABIT_UGURU_FAN_PWM_ENABLE 0x80 /* enable speed control */
62 /* Values used for conversion */
63 #define ABIT_UGURU_FAN_MAX 15300 /* RPM */
64 /* Bank1 sensor types */
65 #define ABIT_UGURU_IN_SENSOR 0
66 #define ABIT_UGURU_TEMP_SENSOR 1
67 #define ABIT_UGURU_NC 2
68 /* In many cases we need to wait for the uGuru to reach a certain status, most
69 of the time it will reach this status within 30 - 90 ISA reads, and thus we
70 can best busy wait. This define gives the total amount of reads to try. */
71 #define ABIT_UGURU_WAIT_TIMEOUT 125
72 /* However sometimes older versions of the uGuru seem to be distracted and they
73 do not respond for a long time. To handle this we sleep before each of the
74 last ABIT_UGURU_WAIT_TIMEOUT_SLEEP tries. */
75 #define ABIT_UGURU_WAIT_TIMEOUT_SLEEP 5
76 /* Normally all expected status in abituguru_ready, are reported after the
77 first read, but sometimes not and we need to poll. */
78 #define ABIT_UGURU_READY_TIMEOUT 5
79 /* Maximum 3 retries on timedout reads/writes, delay 200 ms before retrying */
80 #define ABIT_UGURU_MAX_RETRIES 3
81 #define ABIT_UGURU_RETRY_DELAY (HZ/5)
82 /* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is an error */
83 #define ABIT_UGURU_MAX_TIMEOUTS 2
84 /* utility macros */
85 #define ABIT_UGURU_NAME "abituguru"
86 #define ABIT_UGURU_DEBUG(level, format, arg...) \
87 if (level <= verbose) \
88 printk(KERN_DEBUG ABIT_UGURU_NAME ": " format , ## arg)
89 /* Macros to help calculate the sysfs_names array length */
90 /* sum of strlen of: in??_input\0, in??_{min,max}\0, in??_{min,max}_alarm\0,
91 in??_{min,max}_alarm_enable\0, in??_beep\0, in??_shutdown\0 */
92 #define ABITUGURU_IN_NAMES_LENGTH (11 + 2 * 9 + 2 * 15 + 2 * 22 + 10 + 14)
93 /* sum of strlen of: temp??_input\0, temp??_max\0, temp??_crit\0,
94 temp??_alarm\0, temp??_alarm_enable\0, temp??_beep\0, temp??_shutdown\0 */
95 #define ABITUGURU_TEMP_NAMES_LENGTH (13 + 11 + 12 + 13 + 20 + 12 + 16)
96 /* sum of strlen of: fan?_input\0, fan?_min\0, fan?_alarm\0,
97 fan?_alarm_enable\0, fan?_beep\0, fan?_shutdown\0 */
98 #define ABITUGURU_FAN_NAMES_LENGTH (11 + 9 + 11 + 18 + 10 + 14)
99 /* sum of strlen of: pwm?_enable\0, pwm?_auto_channels_temp\0,
100 pwm?_auto_point{1,2}_pwm\0, pwm?_auto_point{1,2}_temp\0 */
101 #define ABITUGURU_PWM_NAMES_LENGTH (12 + 24 + 2 * 21 + 2 * 22)
102 /* IN_NAMES_LENGTH > TEMP_NAMES_LENGTH so assume all bank1 sensors are in */
103 #define ABITUGURU_SYSFS_NAMES_LENGTH ( \
104 ABIT_UGURU_MAX_BANK1_SENSORS * ABITUGURU_IN_NAMES_LENGTH + \
105 ABIT_UGURU_MAX_BANK2_SENSORS * ABITUGURU_FAN_NAMES_LENGTH + \
106 ABIT_UGURU_MAX_PWMS * ABITUGURU_PWM_NAMES_LENGTH)
108 /* All the macros below are named identical to the oguru and oguru2 programs
109 reverse engineered by Olle Sandberg, hence the names might not be 100%
110 logical. I could come up with better names, but I prefer keeping the names
111 identical so that this driver can be compared with his work more easily. */
112 /* Two i/o-ports are used by uGuru */
113 #define ABIT_UGURU_BASE 0x00E0
114 /* Used to tell uGuru what to read and to read the actual data */
115 #define ABIT_UGURU_CMD 0x00
116 /* Mostly used to check if uGuru is busy */
117 #define ABIT_UGURU_DATA 0x04
118 #define ABIT_UGURU_REGION_LENGTH 5
119 /* uGuru status' */
120 #define ABIT_UGURU_STATUS_WRITE 0x00 /* Ready to be written */
121 #define ABIT_UGURU_STATUS_READ 0x01 /* Ready to be read */
122 #define ABIT_UGURU_STATUS_INPUT 0x08 /* More input */
123 #define ABIT_UGURU_STATUS_READY 0x09 /* Ready to be written */
125 /* Constants */
126 /* in (Volt) sensors go up to 3494 mV, temp to 255000 millidegrees Celsius */
127 static const int abituguru_bank1_max_value[2] = { 3494, 255000 };
128 /* Min / Max allowed values for sensor2 (fan) alarm threshold, these values
129 correspond to 300-3000 RPM */
130 static const u8 abituguru_bank2_min_threshold = 5;
131 static const u8 abituguru_bank2_max_threshold = 50;
132 /* Register 0 is a bitfield, 1 and 2 are pwm settings (255 = 100%), 3 and 4
133 are temperature trip points. */
134 static const int abituguru_pwm_settings_multiplier[5] = { 0, 1, 1, 1000, 1000 };
135 /* Min / Max allowed values for pwm_settings. Note: pwm1 (CPU fan) is a
136 special case the minium allowed pwm% setting for this is 30% (77) on
137 some MB's this special case is handled in the code! */
138 static const u8 abituguru_pwm_min[5] = { 0, 170, 170, 25, 25 };
139 static const u8 abituguru_pwm_max[5] = { 0, 255, 255, 75, 75 };
142 /* Insmod parameters */
143 static int force;
144 module_param(force, bool, 0);
145 MODULE_PARM_DESC(force, "Set to one to force detection.");
146 static int bank1_types[ABIT_UGURU_MAX_BANK1_SENSORS] = { -1, -1, -1, -1, -1,
147 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
148 module_param_array(bank1_types, int, NULL, 0);
149 MODULE_PARM_DESC(bank1_types, "Bank1 sensortype autodetection override:\n"
150 " -1 autodetect\n"
151 " 0 volt sensor\n"
152 " 1 temp sensor\n"
153 " 2 not connected");
154 static int fan_sensors;
155 module_param(fan_sensors, int, 0);
156 MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru "
157 "(0 = autodetect)");
158 static int pwms;
159 module_param(pwms, int, 0);
160 MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru "
161 "(0 = autodetect)");
163 /* Default verbose is 2, since this driver is still in the testing phase */
164 static int verbose = 2;
165 module_param(verbose, int, 0644);
166 MODULE_PARM_DESC(verbose, "How verbose should the driver be? (0-3):\n"
167 " 0 normal output\n"
168 " 1 + verbose error reporting\n"
169 " 2 + sensors type probing info\n"
170 " 3 + retryable error reporting");
173 /* For the Abit uGuru, we need to keep some data in memory.
174 The structure is dynamically allocated, at the same time when a new
175 abituguru device is allocated. */
176 struct abituguru_data {
177 struct class_device *class_dev; /* hwmon registered device */
178 struct mutex update_lock; /* protect access to data and uGuru */
179 unsigned long last_updated; /* In jiffies */
180 unsigned short addr; /* uguru base address */
181 char uguru_ready; /* is the uguru in ready state? */
182 unsigned char update_timeouts; /* number of update timeouts since last
183 successful update */
185 /* The sysfs attr and their names are generated automatically, for bank1
186 we cannot use a predefined array because we don't know beforehand
187 of a sensor is a volt or a temp sensor, for bank2 and the pwms its
188 easier todo things the same way. For in sensors we have 9 (temp 7)
189 sysfs entries per sensor, for bank2 and pwms 6. */
190 struct sensor_device_attribute_2 sysfs_attr[
191 ABIT_UGURU_MAX_BANK1_SENSORS * 9 +
192 ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6];
193 /* Buffer to store the dynamically generated sysfs names */
194 char sysfs_names[ABITUGURU_SYSFS_NAMES_LENGTH];
196 /* Bank 1 data */
197 /* number of and addresses of [0] in, [1] temp sensors */
198 u8 bank1_sensors[2];
199 u8 bank1_address[2][ABIT_UGURU_MAX_BANK1_SENSORS];
200 u8 bank1_value[ABIT_UGURU_MAX_BANK1_SENSORS];
201 /* This array holds 3 entries per sensor for the bank 1 sensor settings
202 (flags, min, max for voltage / flags, warn, shutdown for temp). */
203 u8 bank1_settings[ABIT_UGURU_MAX_BANK1_SENSORS][3];
204 /* Maximum value for each sensor used for scaling in mV/millidegrees
205 Celsius. */
206 int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS];
208 /* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */
209 u8 bank2_sensors; /* actual number of bank2 sensors found */
210 u8 bank2_value[ABIT_UGURU_MAX_BANK2_SENSORS];
211 u8 bank2_settings[ABIT_UGURU_MAX_BANK2_SENSORS][2]; /* flags, min */
213 /* Alarms 2 bytes for bank1, 1 byte for bank2 */
214 u8 alarms[3];
216 /* Fan PWM (speed control) 5 bytes per PWM */
217 u8 pwms; /* actual number of pwms found */
218 u8 pwm_settings[ABIT_UGURU_MAX_PWMS][5];
221 /* wait till the uguru is in the specified state */
222 static int abituguru_wait(struct abituguru_data *data, u8 state)
224 int timeout = ABIT_UGURU_WAIT_TIMEOUT;
226 while (inb_p(data->addr + ABIT_UGURU_DATA) != state) {
227 timeout--;
228 if (timeout == 0)
229 return -EBUSY;
230 /* sleep a bit before our last few tries, see the comment on
231 this where ABIT_UGURU_WAIT_TIMEOUT_SLEEP is defined. */
232 if (timeout <= ABIT_UGURU_WAIT_TIMEOUT_SLEEP)
233 msleep(0);
235 return 0;
238 /* Put the uguru in ready for input state */
239 static int abituguru_ready(struct abituguru_data *data)
241 int timeout = ABIT_UGURU_READY_TIMEOUT;
243 if (data->uguru_ready)
244 return 0;
246 /* Reset? / Prepare for next read/write cycle */
247 outb(0x00, data->addr + ABIT_UGURU_DATA);
249 /* Wait till the uguru is ready */
250 if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) {
251 ABIT_UGURU_DEBUG(1,
252 "timeout exceeded waiting for ready state\n");
253 return -EIO;
256 /* Cmd port MUST be read now and should contain 0xAC */
257 while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
258 timeout--;
259 if (timeout == 0) {
260 ABIT_UGURU_DEBUG(1,
261 "CMD reg does not hold 0xAC after ready command\n");
262 return -EIO;
264 msleep(0);
267 /* After this the ABIT_UGURU_DATA port should contain
268 ABIT_UGURU_STATUS_INPUT */
269 timeout = ABIT_UGURU_READY_TIMEOUT;
270 while (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT) {
271 timeout--;
272 if (timeout == 0) {
273 ABIT_UGURU_DEBUG(1,
274 "state != more input after ready command\n");
275 return -EIO;
277 msleep(0);
280 data->uguru_ready = 1;
281 return 0;
284 /* Send the bank and then sensor address to the uGuru for the next read/write
285 cycle. This function gets called as the first part of a read/write by
286 abituguru_read and abituguru_write. This function should never be
287 called by any other function. */
288 static int abituguru_send_address(struct abituguru_data *data,
289 u8 bank_addr, u8 sensor_addr, int retries)
291 /* assume the caller does error handling itself if it has not requested
292 any retries, and thus be quiet. */
293 int report_errors = retries;
295 for (;;) {
296 /* Make sure the uguru is ready and then send the bank address,
297 after this the uguru is no longer "ready". */
298 if (abituguru_ready(data) != 0)
299 return -EIO;
300 outb(bank_addr, data->addr + ABIT_UGURU_DATA);
301 data->uguru_ready = 0;
303 /* Wait till the uguru is ABIT_UGURU_STATUS_INPUT state again
304 and send the sensor addr */
305 if (abituguru_wait(data, ABIT_UGURU_STATUS_INPUT)) {
306 if (retries) {
307 ABIT_UGURU_DEBUG(3, "timeout exceeded "
308 "waiting for more input state, %d "
309 "tries remaining\n", retries);
310 set_current_state(TASK_UNINTERRUPTIBLE);
311 schedule_timeout(ABIT_UGURU_RETRY_DELAY);
312 retries--;
313 continue;
315 if (report_errors)
316 ABIT_UGURU_DEBUG(1, "timeout exceeded "
317 "waiting for more input state "
318 "(bank: %d)\n", (int)bank_addr);
319 return -EBUSY;
321 outb(sensor_addr, data->addr + ABIT_UGURU_CMD);
322 return 0;
326 /* Read count bytes from sensor sensor_addr in bank bank_addr and store the
327 result in buf, retry the send address part of the read retries times. */
328 static int abituguru_read(struct abituguru_data *data,
329 u8 bank_addr, u8 sensor_addr, u8 *buf, int count, int retries)
331 int i;
333 /* Send the address */
334 i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
335 if (i)
336 return i;
338 /* And read the data */
339 for (i = 0; i < count; i++) {
340 if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
341 ABIT_UGURU_DEBUG(retries ? 1 : 3,
342 "timeout exceeded waiting for "
343 "read state (bank: %d, sensor: %d)\n",
344 (int)bank_addr, (int)sensor_addr);
345 break;
347 buf[i] = inb(data->addr + ABIT_UGURU_CMD);
350 /* Last put the chip back in ready state */
351 abituguru_ready(data);
353 return i;
356 /* Write count bytes from buf to sensor sensor_addr in bank bank_addr, the send
357 address part of the write is always retried ABIT_UGURU_MAX_RETRIES times. */
358 static int abituguru_write(struct abituguru_data *data,
359 u8 bank_addr, u8 sensor_addr, u8 *buf, int count)
361 /* We use the ready timeout as we have to wait for 0xAC just like the
362 ready function */
363 int i, timeout = ABIT_UGURU_READY_TIMEOUT;
365 /* Send the address */
366 i = abituguru_send_address(data, bank_addr, sensor_addr,
367 ABIT_UGURU_MAX_RETRIES);
368 if (i)
369 return i;
371 /* And write the data */
372 for (i = 0; i < count; i++) {
373 if (abituguru_wait(data, ABIT_UGURU_STATUS_WRITE)) {
374 ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
375 "write state (bank: %d, sensor: %d)\n",
376 (int)bank_addr, (int)sensor_addr);
377 break;
379 outb(buf[i], data->addr + ABIT_UGURU_CMD);
382 /* Now we need to wait till the chip is ready to be read again,
383 so that we can read 0xAC as confirmation that our write has
384 succeeded. */
385 if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
386 ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for read state "
387 "after write (bank: %d, sensor: %d)\n", (int)bank_addr,
388 (int)sensor_addr);
389 return -EIO;
392 /* Cmd port MUST be read now and should contain 0xAC */
393 while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
394 timeout--;
395 if (timeout == 0) {
396 ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after "
397 "write (bank: %d, sensor: %d)\n",
398 (int)bank_addr, (int)sensor_addr);
399 return -EIO;
401 msleep(0);
404 /* Last put the chip back in ready state */
405 abituguru_ready(data);
407 return i;
410 /* Detect sensor type. Temp and Volt sensors are enabled with
411 different masks and will ignore enable masks not meant for them.
412 This enables us to test what kind of sensor we're dealing with.
413 By setting the alarm thresholds so that we will always get an
414 alarm for sensor type X and then enabling the sensor as sensor type
415 X, if we then get an alarm it is a sensor of type X. */
416 static int __devinit
417 abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
418 u8 sensor_addr)
420 u8 val, buf[3];
421 int i, ret = -ENODEV; /* error is the most common used retval :| */
423 /* If overriden by the user return the user selected type */
424 if (bank1_types[sensor_addr] >= ABIT_UGURU_IN_SENSOR &&
425 bank1_types[sensor_addr] <= ABIT_UGURU_NC) {
426 ABIT_UGURU_DEBUG(2, "assuming sensor type %d for bank1 sensor "
427 "%d because of \"bank1_types\" module param\n",
428 bank1_types[sensor_addr], (int)sensor_addr);
429 return bank1_types[sensor_addr];
432 /* First read the sensor and the current settings */
433 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, sensor_addr, &val,
434 1, ABIT_UGURU_MAX_RETRIES) != 1)
435 return -ENODEV;
437 /* Test val is sane / usable for sensor type detection. */
438 if ((val < 10u) || (val > 240u)) {
439 printk(KERN_WARNING ABIT_UGURU_NAME
440 ": bank1-sensor: %d reading (%d) too close to limits, "
441 "unable to determine sensor type, skipping sensor\n",
442 (int)sensor_addr, (int)val);
443 /* assume no sensor is there for sensors for which we can't
444 determine the sensor type because their reading is too close
445 to their limits, this usually means no sensor is there. */
446 return ABIT_UGURU_NC;
449 ABIT_UGURU_DEBUG(2, "testing bank1 sensor %d\n", (int)sensor_addr);
450 /* Volt sensor test, enable volt low alarm, set min value ridicously
451 high. If its a volt sensor this should always give us an alarm. */
452 buf[0] = ABIT_UGURU_VOLT_LOW_ALARM_ENABLE;
453 buf[1] = 245;
454 buf[2] = 250;
455 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
456 buf, 3) != 3)
457 goto abituguru_detect_bank1_sensor_type_exit;
458 /* Now we need 20 ms to give the uguru time to read the sensors
459 and raise a voltage alarm */
460 set_current_state(TASK_UNINTERRUPTIBLE);
461 schedule_timeout(HZ/50);
462 /* Check for alarm and check the alarm is a volt low alarm. */
463 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
464 ABIT_UGURU_MAX_RETRIES) != 3)
465 goto abituguru_detect_bank1_sensor_type_exit;
466 if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
467 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
468 sensor_addr, buf, 3,
469 ABIT_UGURU_MAX_RETRIES) != 3)
470 goto abituguru_detect_bank1_sensor_type_exit;
471 if (buf[0] & ABIT_UGURU_VOLT_LOW_ALARM_FLAG) {
472 ABIT_UGURU_DEBUG(2, " found volt sensor\n");
473 ret = ABIT_UGURU_IN_SENSOR;
474 goto abituguru_detect_bank1_sensor_type_exit;
475 } else
476 ABIT_UGURU_DEBUG(2, " alarm raised during volt "
477 "sensor test, but volt low flag not set\n");
478 } else
479 ABIT_UGURU_DEBUG(2, " alarm not raised during volt sensor "
480 "test\n");
482 /* Temp sensor test, enable sensor as a temp sensor, set beep value
483 ridicously low (but not too low, otherwise uguru ignores it).
484 If its a temp sensor this should always give us an alarm. */
485 buf[0] = ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE;
486 buf[1] = 5;
487 buf[2] = 10;
488 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
489 buf, 3) != 3)
490 goto abituguru_detect_bank1_sensor_type_exit;
491 /* Now we need 50 ms to give the uguru time to read the sensors
492 and raise a temp alarm */
493 set_current_state(TASK_UNINTERRUPTIBLE);
494 schedule_timeout(HZ/20);
495 /* Check for alarm and check the alarm is a temp high alarm. */
496 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
497 ABIT_UGURU_MAX_RETRIES) != 3)
498 goto abituguru_detect_bank1_sensor_type_exit;
499 if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
500 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
501 sensor_addr, buf, 3,
502 ABIT_UGURU_MAX_RETRIES) != 3)
503 goto abituguru_detect_bank1_sensor_type_exit;
504 if (buf[0] & ABIT_UGURU_TEMP_HIGH_ALARM_FLAG) {
505 ABIT_UGURU_DEBUG(2, " found temp sensor\n");
506 ret = ABIT_UGURU_TEMP_SENSOR;
507 goto abituguru_detect_bank1_sensor_type_exit;
508 } else
509 ABIT_UGURU_DEBUG(2, " alarm raised during temp "
510 "sensor test, but temp high flag not set\n");
511 } else
512 ABIT_UGURU_DEBUG(2, " alarm not raised during temp sensor "
513 "test\n");
515 ret = ABIT_UGURU_NC;
516 abituguru_detect_bank1_sensor_type_exit:
517 /* Restore original settings, failing here is really BAD, it has been
518 reported that some BIOS-es hang when entering the uGuru menu with
519 invalid settings present in the uGuru, so we try this 3 times. */
520 for (i = 0; i < 3; i++)
521 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
522 sensor_addr, data->bank1_settings[sensor_addr],
523 3) == 3)
524 break;
525 if (i == 3) {
526 printk(KERN_ERR ABIT_UGURU_NAME
527 ": Fatal error could not restore original settings. "
528 "This should never happen please report this to the "
529 "abituguru maintainer (see MAINTAINERS)\n");
530 return -ENODEV;
532 return ret;
535 /* These functions try to find out how many sensors there are in bank2 and how
536 many pwms there are. The purpose of this is to make sure that we don't give
537 the user the possibility to change settings for non-existent sensors / pwm.
538 The uGuru will happily read / write whatever memory happens to be after the
539 memory storing the PWM settings when reading/writing to a PWM which is not
540 there. Notice even if we detect a PWM which doesn't exist we normally won't
541 write to it, unless the user tries to change the settings.
543 Although the uGuru allows reading (settings) from non existing bank2
544 sensors, my version of the uGuru does seem to stop writing to them, the
545 write function above aborts in this case with:
546 "CMD reg does not hold 0xAC after write"
548 Notice these 2 tests are non destructive iow read-only tests, otherwise
549 they would defeat their purpose. Although for the bank2_sensors detection a
550 read/write test would be feasible because of the reaction above, I've
551 however opted to stay on the safe side. */
552 static void __devinit
553 abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
555 int i;
557 if (fan_sensors > 0 && fan_sensors <= ABIT_UGURU_MAX_BANK2_SENSORS) {
558 data->bank2_sensors = fan_sensors;
559 ABIT_UGURU_DEBUG(2, "assuming %d fan sensors because of "
560 "\"fan_sensors\" module param\n",
561 (int)data->bank2_sensors);
562 return;
565 ABIT_UGURU_DEBUG(2, "detecting number of fan sensors\n");
566 for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
567 /* 0x89 are the known used bits:
568 -0x80 enable shutdown
569 -0x08 enable beep
570 -0x01 enable alarm
571 All other bits should be 0, but on some motherboards
572 0x40 (bit 6) is also high for some of the fans?? */
573 if (data->bank2_settings[i][0] & ~0xC9) {
574 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
575 "to be a fan sensor: settings[0] = %02X\n",
576 i, (unsigned int)data->bank2_settings[i][0]);
577 break;
580 /* check if the threshold is within the allowed range */
581 if (data->bank2_settings[i][1] <
582 abituguru_bank2_min_threshold) {
583 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
584 "to be a fan sensor: the threshold (%d) is "
585 "below the minimum (%d)\n", i,
586 (int)data->bank2_settings[i][1],
587 (int)abituguru_bank2_min_threshold);
588 break;
590 if (data->bank2_settings[i][1] >
591 abituguru_bank2_max_threshold) {
592 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
593 "to be a fan sensor: the threshold (%d) is "
594 "above the maximum (%d)\n", i,
595 (int)data->bank2_settings[i][1],
596 (int)abituguru_bank2_max_threshold);
597 break;
601 data->bank2_sensors = i;
602 ABIT_UGURU_DEBUG(2, " found: %d fan sensors\n",
603 (int)data->bank2_sensors);
606 static void __devinit
607 abituguru_detect_no_pwms(struct abituguru_data *data)
609 int i, j;
611 if (pwms > 0 && pwms <= ABIT_UGURU_MAX_PWMS) {
612 data->pwms = pwms;
613 ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
614 "\"pwms\" module param\n", (int)data->pwms);
615 return;
618 ABIT_UGURU_DEBUG(2, "detecting number of PWM outputs\n");
619 for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
620 /* 0x80 is the enable bit and the low
621 nibble is which temp sensor to use,
622 the other bits should be 0 */
623 if (data->pwm_settings[i][0] & ~0x8F) {
624 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
625 "to be a pwm channel: settings[0] = %02X\n",
626 i, (unsigned int)data->pwm_settings[i][0]);
627 break;
630 /* the low nibble must correspond to one of the temp sensors
631 we've found */
632 for (j = 0; j < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR];
633 j++) {
634 if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][j] ==
635 (data->pwm_settings[i][0] & 0x0F))
636 break;
638 if (j == data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
639 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
640 "to be a pwm channel: %d is not a valid temp "
641 "sensor address\n", i,
642 data->pwm_settings[i][0] & 0x0F);
643 break;
646 /* check if all other settings are within the allowed range */
647 for (j = 1; j < 5; j++) {
648 u8 min;
649 /* special case pwm1 min pwm% */
650 if ((i == 0) && ((j == 1) || (j == 2)))
651 min = 77;
652 else
653 min = abituguru_pwm_min[j];
654 if (data->pwm_settings[i][j] < min) {
655 ABIT_UGURU_DEBUG(2, " pwm channel %d does "
656 "not seem to be a pwm channel: "
657 "setting %d (%d) is below the minimum "
658 "value (%d)\n", i, j,
659 (int)data->pwm_settings[i][j],
660 (int)min);
661 goto abituguru_detect_no_pwms_exit;
663 if (data->pwm_settings[i][j] > abituguru_pwm_max[j]) {
664 ABIT_UGURU_DEBUG(2, " pwm channel %d does "
665 "not seem to be a pwm channel: "
666 "setting %d (%d) is above the maximum "
667 "value (%d)\n", i, j,
668 (int)data->pwm_settings[i][j],
669 (int)abituguru_pwm_max[j]);
670 goto abituguru_detect_no_pwms_exit;
674 /* check that min temp < max temp and min pwm < max pwm */
675 if (data->pwm_settings[i][1] >= data->pwm_settings[i][2]) {
676 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
677 "to be a pwm channel: min pwm (%d) >= "
678 "max pwm (%d)\n", i,
679 (int)data->pwm_settings[i][1],
680 (int)data->pwm_settings[i][2]);
681 break;
683 if (data->pwm_settings[i][3] >= data->pwm_settings[i][4]) {
684 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
685 "to be a pwm channel: min temp (%d) >= "
686 "max temp (%d)\n", i,
687 (int)data->pwm_settings[i][3],
688 (int)data->pwm_settings[i][4]);
689 break;
693 abituguru_detect_no_pwms_exit:
694 data->pwms = i;
695 ABIT_UGURU_DEBUG(2, " found: %d PWM outputs\n", (int)data->pwms);
698 /* Following are the sysfs callback functions. These functions expect:
699 sensor_device_attribute_2->index: sensor address/offset in the bank
700 sensor_device_attribute_2->nr: register offset, bitmask or NA. */
701 static struct abituguru_data *abituguru_update_device(struct device *dev);
703 static ssize_t show_bank1_value(struct device *dev,
704 struct device_attribute *devattr, char *buf)
706 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
707 struct abituguru_data *data = abituguru_update_device(dev);
708 if (!data)
709 return -EIO;
710 return sprintf(buf, "%d\n", (data->bank1_value[attr->index] *
711 data->bank1_max_value[attr->index] + 128) / 255);
714 static ssize_t show_bank1_setting(struct device *dev,
715 struct device_attribute *devattr, char *buf)
717 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
718 struct abituguru_data *data = dev_get_drvdata(dev);
719 return sprintf(buf, "%d\n",
720 (data->bank1_settings[attr->index][attr->nr] *
721 data->bank1_max_value[attr->index] + 128) / 255);
724 static ssize_t show_bank2_value(struct device *dev,
725 struct device_attribute *devattr, char *buf)
727 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
728 struct abituguru_data *data = abituguru_update_device(dev);
729 if (!data)
730 return -EIO;
731 return sprintf(buf, "%d\n", (data->bank2_value[attr->index] *
732 ABIT_UGURU_FAN_MAX + 128) / 255);
735 static ssize_t show_bank2_setting(struct device *dev,
736 struct device_attribute *devattr, char *buf)
738 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
739 struct abituguru_data *data = dev_get_drvdata(dev);
740 return sprintf(buf, "%d\n",
741 (data->bank2_settings[attr->index][attr->nr] *
742 ABIT_UGURU_FAN_MAX + 128) / 255);
745 static ssize_t store_bank1_setting(struct device *dev, struct device_attribute
746 *devattr, const char *buf, size_t count)
748 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
749 struct abituguru_data *data = dev_get_drvdata(dev);
750 u8 val = (simple_strtoul(buf, NULL, 10) * 255 +
751 data->bank1_max_value[attr->index]/2) /
752 data->bank1_max_value[attr->index];
753 ssize_t ret = count;
755 mutex_lock(&data->update_lock);
756 if (data->bank1_settings[attr->index][attr->nr] != val) {
757 u8 orig_val = data->bank1_settings[attr->index][attr->nr];
758 data->bank1_settings[attr->index][attr->nr] = val;
759 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
760 attr->index, data->bank1_settings[attr->index],
761 3) <= attr->nr) {
762 data->bank1_settings[attr->index][attr->nr] = orig_val;
763 ret = -EIO;
766 mutex_unlock(&data->update_lock);
767 return ret;
770 static ssize_t store_bank2_setting(struct device *dev, struct device_attribute
771 *devattr, const char *buf, size_t count)
773 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
774 struct abituguru_data *data = dev_get_drvdata(dev);
775 u8 val = (simple_strtoul(buf, NULL, 10)*255 + ABIT_UGURU_FAN_MAX/2) /
776 ABIT_UGURU_FAN_MAX;
777 ssize_t ret = count;
779 /* this check can be done before taking the lock */
780 if ((val < abituguru_bank2_min_threshold) ||
781 (val > abituguru_bank2_max_threshold))
782 return -EINVAL;
784 mutex_lock(&data->update_lock);
785 if (data->bank2_settings[attr->index][attr->nr] != val) {
786 u8 orig_val = data->bank2_settings[attr->index][attr->nr];
787 data->bank2_settings[attr->index][attr->nr] = val;
788 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK2 + 2,
789 attr->index, data->bank2_settings[attr->index],
790 2) <= attr->nr) {
791 data->bank2_settings[attr->index][attr->nr] = orig_val;
792 ret = -EIO;
795 mutex_unlock(&data->update_lock);
796 return ret;
799 static ssize_t show_bank1_alarm(struct device *dev,
800 struct device_attribute *devattr, char *buf)
802 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
803 struct abituguru_data *data = abituguru_update_device(dev);
804 if (!data)
805 return -EIO;
806 /* See if the alarm bit for this sensor is set, and if the
807 alarm matches the type of alarm we're looking for (for volt
808 it can be either low or high). The type is stored in a few
809 readonly bits in the settings part of the relevant sensor.
810 The bitmask of the type is passed to us in attr->nr. */
811 if ((data->alarms[attr->index / 8] & (0x01 << (attr->index % 8))) &&
812 (data->bank1_settings[attr->index][0] & attr->nr))
813 return sprintf(buf, "1\n");
814 else
815 return sprintf(buf, "0\n");
818 static ssize_t show_bank2_alarm(struct device *dev,
819 struct device_attribute *devattr, char *buf)
821 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
822 struct abituguru_data *data = abituguru_update_device(dev);
823 if (!data)
824 return -EIO;
825 if (data->alarms[2] & (0x01 << attr->index))
826 return sprintf(buf, "1\n");
827 else
828 return sprintf(buf, "0\n");
831 static ssize_t show_bank1_mask(struct device *dev,
832 struct device_attribute *devattr, char *buf)
834 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
835 struct abituguru_data *data = dev_get_drvdata(dev);
836 if (data->bank1_settings[attr->index][0] & attr->nr)
837 return sprintf(buf, "1\n");
838 else
839 return sprintf(buf, "0\n");
842 static ssize_t show_bank2_mask(struct device *dev,
843 struct device_attribute *devattr, char *buf)
845 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
846 struct abituguru_data *data = dev_get_drvdata(dev);
847 if (data->bank2_settings[attr->index][0] & attr->nr)
848 return sprintf(buf, "1\n");
849 else
850 return sprintf(buf, "0\n");
853 static ssize_t store_bank1_mask(struct device *dev,
854 struct device_attribute *devattr, const char *buf, size_t count)
856 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
857 struct abituguru_data *data = dev_get_drvdata(dev);
858 int mask = simple_strtoul(buf, NULL, 10);
859 ssize_t ret = count;
860 u8 orig_val;
862 mutex_lock(&data->update_lock);
863 orig_val = data->bank1_settings[attr->index][0];
865 if (mask)
866 data->bank1_settings[attr->index][0] |= attr->nr;
867 else
868 data->bank1_settings[attr->index][0] &= ~attr->nr;
870 if ((data->bank1_settings[attr->index][0] != orig_val) &&
871 (abituguru_write(data,
872 ABIT_UGURU_SENSOR_BANK1 + 2, attr->index,
873 data->bank1_settings[attr->index], 3) < 1)) {
874 data->bank1_settings[attr->index][0] = orig_val;
875 ret = -EIO;
877 mutex_unlock(&data->update_lock);
878 return ret;
881 static ssize_t store_bank2_mask(struct device *dev,
882 struct device_attribute *devattr, const char *buf, size_t count)
884 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
885 struct abituguru_data *data = dev_get_drvdata(dev);
886 int mask = simple_strtoul(buf, NULL, 10);
887 ssize_t ret = count;
888 u8 orig_val;
890 mutex_lock(&data->update_lock);
891 orig_val = data->bank2_settings[attr->index][0];
893 if (mask)
894 data->bank2_settings[attr->index][0] |= attr->nr;
895 else
896 data->bank2_settings[attr->index][0] &= ~attr->nr;
898 if ((data->bank2_settings[attr->index][0] != orig_val) &&
899 (abituguru_write(data,
900 ABIT_UGURU_SENSOR_BANK2 + 2, attr->index,
901 data->bank2_settings[attr->index], 2) < 1)) {
902 data->bank2_settings[attr->index][0] = orig_val;
903 ret = -EIO;
905 mutex_unlock(&data->update_lock);
906 return ret;
909 /* Fan PWM (speed control) */
910 static ssize_t show_pwm_setting(struct device *dev,
911 struct device_attribute *devattr, char *buf)
913 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
914 struct abituguru_data *data = dev_get_drvdata(dev);
915 return sprintf(buf, "%d\n", data->pwm_settings[attr->index][attr->nr] *
916 abituguru_pwm_settings_multiplier[attr->nr]);
919 static ssize_t store_pwm_setting(struct device *dev, struct device_attribute
920 *devattr, const char *buf, size_t count)
922 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
923 struct abituguru_data *data = dev_get_drvdata(dev);
924 u8 min, val = (simple_strtoul(buf, NULL, 10) +
925 abituguru_pwm_settings_multiplier[attr->nr]/2) /
926 abituguru_pwm_settings_multiplier[attr->nr];
927 ssize_t ret = count;
929 /* special case pwm1 min pwm% */
930 if ((attr->index == 0) && ((attr->nr == 1) || (attr->nr == 2)))
931 min = 77;
932 else
933 min = abituguru_pwm_min[attr->nr];
935 /* this check can be done before taking the lock */
936 if ((val < min) || (val > abituguru_pwm_max[attr->nr]))
937 return -EINVAL;
939 mutex_lock(&data->update_lock);
940 /* this check needs to be done after taking the lock */
941 if ((attr->nr & 1) &&
942 (val >= data->pwm_settings[attr->index][attr->nr + 1]))
943 ret = -EINVAL;
944 else if (!(attr->nr & 1) &&
945 (val <= data->pwm_settings[attr->index][attr->nr - 1]))
946 ret = -EINVAL;
947 else if (data->pwm_settings[attr->index][attr->nr] != val) {
948 u8 orig_val = data->pwm_settings[attr->index][attr->nr];
949 data->pwm_settings[attr->index][attr->nr] = val;
950 if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
951 attr->index, data->pwm_settings[attr->index],
952 5) <= attr->nr) {
953 data->pwm_settings[attr->index][attr->nr] =
954 orig_val;
955 ret = -EIO;
958 mutex_unlock(&data->update_lock);
959 return ret;
962 static ssize_t show_pwm_sensor(struct device *dev,
963 struct device_attribute *devattr, char *buf)
965 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
966 struct abituguru_data *data = dev_get_drvdata(dev);
967 int i;
968 /* We need to walk to the temp sensor addresses to find what
969 the userspace id of the configured temp sensor is. */
970 for (i = 0; i < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]; i++)
971 if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][i] ==
972 (data->pwm_settings[attr->index][0] & 0x0F))
973 return sprintf(buf, "%d\n", i+1);
975 return -ENXIO;
978 static ssize_t store_pwm_sensor(struct device *dev, struct device_attribute
979 *devattr, const char *buf, size_t count)
981 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
982 struct abituguru_data *data = dev_get_drvdata(dev);
983 unsigned long val = simple_strtoul(buf, NULL, 10) - 1;
984 ssize_t ret = count;
986 mutex_lock(&data->update_lock);
987 if (val < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
988 u8 orig_val = data->pwm_settings[attr->index][0];
989 u8 address = data->bank1_address[ABIT_UGURU_TEMP_SENSOR][val];
990 data->pwm_settings[attr->index][0] &= 0xF0;
991 data->pwm_settings[attr->index][0] |= address;
992 if (data->pwm_settings[attr->index][0] != orig_val) {
993 if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
994 attr->index,
995 data->pwm_settings[attr->index],
996 5) < 1) {
997 data->pwm_settings[attr->index][0] = orig_val;
998 ret = -EIO;
1002 else
1003 ret = -EINVAL;
1004 mutex_unlock(&data->update_lock);
1005 return ret;
1008 static ssize_t show_pwm_enable(struct device *dev,
1009 struct device_attribute *devattr, char *buf)
1011 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1012 struct abituguru_data *data = dev_get_drvdata(dev);
1013 int res = 0;
1014 if (data->pwm_settings[attr->index][0] & ABIT_UGURU_FAN_PWM_ENABLE)
1015 res = 2;
1016 return sprintf(buf, "%d\n", res);
1019 static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1020 *devattr, const char *buf, size_t count)
1022 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1023 struct abituguru_data *data = dev_get_drvdata(dev);
1024 u8 orig_val, user_val = simple_strtoul(buf, NULL, 10);
1025 ssize_t ret = count;
1027 mutex_lock(&data->update_lock);
1028 orig_val = data->pwm_settings[attr->index][0];
1029 switch (user_val) {
1030 case 0:
1031 data->pwm_settings[attr->index][0] &=
1032 ~ABIT_UGURU_FAN_PWM_ENABLE;
1033 break;
1034 case 2:
1035 data->pwm_settings[attr->index][0] |=
1036 ABIT_UGURU_FAN_PWM_ENABLE;
1037 break;
1038 default:
1039 ret = -EINVAL;
1041 if ((data->pwm_settings[attr->index][0] != orig_val) &&
1042 (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1043 attr->index, data->pwm_settings[attr->index],
1044 5) < 1)) {
1045 data->pwm_settings[attr->index][0] = orig_val;
1046 ret = -EIO;
1048 mutex_unlock(&data->update_lock);
1049 return ret;
1052 static ssize_t show_name(struct device *dev,
1053 struct device_attribute *devattr, char *buf)
1055 return sprintf(buf, "%s\n", ABIT_UGURU_NAME);
1058 /* Sysfs attr templates, the real entries are generated automatically. */
1059 static const
1060 struct sensor_device_attribute_2 abituguru_sysfs_bank1_templ[2][9] = {
1062 SENSOR_ATTR_2(in%d_input, 0444, show_bank1_value, NULL, 0, 0),
1063 SENSOR_ATTR_2(in%d_min, 0644, show_bank1_setting,
1064 store_bank1_setting, 1, 0),
1065 SENSOR_ATTR_2(in%d_min_alarm, 0444, show_bank1_alarm, NULL,
1066 ABIT_UGURU_VOLT_LOW_ALARM_FLAG, 0),
1067 SENSOR_ATTR_2(in%d_max, 0644, show_bank1_setting,
1068 store_bank1_setting, 2, 0),
1069 SENSOR_ATTR_2(in%d_max_alarm, 0444, show_bank1_alarm, NULL,
1070 ABIT_UGURU_VOLT_HIGH_ALARM_FLAG, 0),
1071 SENSOR_ATTR_2(in%d_beep, 0644, show_bank1_mask,
1072 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1073 SENSOR_ATTR_2(in%d_shutdown, 0644, show_bank1_mask,
1074 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1075 SENSOR_ATTR_2(in%d_min_alarm_enable, 0644, show_bank1_mask,
1076 store_bank1_mask, ABIT_UGURU_VOLT_LOW_ALARM_ENABLE, 0),
1077 SENSOR_ATTR_2(in%d_max_alarm_enable, 0644, show_bank1_mask,
1078 store_bank1_mask, ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE, 0),
1079 }, {
1080 SENSOR_ATTR_2(temp%d_input, 0444, show_bank1_value, NULL, 0, 0),
1081 SENSOR_ATTR_2(temp%d_alarm, 0444, show_bank1_alarm, NULL,
1082 ABIT_UGURU_TEMP_HIGH_ALARM_FLAG, 0),
1083 SENSOR_ATTR_2(temp%d_max, 0644, show_bank1_setting,
1084 store_bank1_setting, 1, 0),
1085 SENSOR_ATTR_2(temp%d_crit, 0644, show_bank1_setting,
1086 store_bank1_setting, 2, 0),
1087 SENSOR_ATTR_2(temp%d_beep, 0644, show_bank1_mask,
1088 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1089 SENSOR_ATTR_2(temp%d_shutdown, 0644, show_bank1_mask,
1090 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1091 SENSOR_ATTR_2(temp%d_alarm_enable, 0644, show_bank1_mask,
1092 store_bank1_mask, ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE, 0),
1096 static const struct sensor_device_attribute_2 abituguru_sysfs_fan_templ[6] = {
1097 SENSOR_ATTR_2(fan%d_input, 0444, show_bank2_value, NULL, 0, 0),
1098 SENSOR_ATTR_2(fan%d_alarm, 0444, show_bank2_alarm, NULL, 0, 0),
1099 SENSOR_ATTR_2(fan%d_min, 0644, show_bank2_setting,
1100 store_bank2_setting, 1, 0),
1101 SENSOR_ATTR_2(fan%d_beep, 0644, show_bank2_mask,
1102 store_bank2_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1103 SENSOR_ATTR_2(fan%d_shutdown, 0644, show_bank2_mask,
1104 store_bank2_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1105 SENSOR_ATTR_2(fan%d_alarm_enable, 0644, show_bank2_mask,
1106 store_bank2_mask, ABIT_UGURU_FAN_LOW_ALARM_ENABLE, 0),
1109 static const struct sensor_device_attribute_2 abituguru_sysfs_pwm_templ[6] = {
1110 SENSOR_ATTR_2(pwm%d_enable, 0644, show_pwm_enable,
1111 store_pwm_enable, 0, 0),
1112 SENSOR_ATTR_2(pwm%d_auto_channels_temp, 0644, show_pwm_sensor,
1113 store_pwm_sensor, 0, 0),
1114 SENSOR_ATTR_2(pwm%d_auto_point1_pwm, 0644, show_pwm_setting,
1115 store_pwm_setting, 1, 0),
1116 SENSOR_ATTR_2(pwm%d_auto_point2_pwm, 0644, show_pwm_setting,
1117 store_pwm_setting, 2, 0),
1118 SENSOR_ATTR_2(pwm%d_auto_point1_temp, 0644, show_pwm_setting,
1119 store_pwm_setting, 3, 0),
1120 SENSOR_ATTR_2(pwm%d_auto_point2_temp, 0644, show_pwm_setting,
1121 store_pwm_setting, 4, 0),
1124 static struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
1125 SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
1128 static int __devinit abituguru_probe(struct platform_device *pdev)
1130 struct abituguru_data *data;
1131 int i, j, used, sysfs_names_free, sysfs_attr_i, res = -ENODEV;
1132 char *sysfs_filename;
1134 /* El weirdo probe order, to keep the sysfs order identical to the
1135 BIOS and window-appliction listing order. */
1136 const u8 probe_order[ABIT_UGURU_MAX_BANK1_SENSORS] = {
1137 0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E, 0x02,
1138 0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
1140 if (!(data = kzalloc(sizeof(struct abituguru_data), GFP_KERNEL)))
1141 return -ENOMEM;
1143 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
1144 mutex_init(&data->update_lock);
1145 platform_set_drvdata(pdev, data);
1147 /* See if the uGuru is ready */
1148 if (inb_p(data->addr + ABIT_UGURU_DATA) == ABIT_UGURU_STATUS_INPUT)
1149 data->uguru_ready = 1;
1151 /* Completely read the uGuru this has 2 purposes:
1152 - testread / see if one really is there.
1153 - make an in memory copy of all the uguru settings for future use. */
1154 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1155 data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3)
1156 goto abituguru_probe_error;
1158 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1159 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, i,
1160 &data->bank1_value[i], 1,
1161 ABIT_UGURU_MAX_RETRIES) != 1)
1162 goto abituguru_probe_error;
1163 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1+1, i,
1164 data->bank1_settings[i], 3,
1165 ABIT_UGURU_MAX_RETRIES) != 3)
1166 goto abituguru_probe_error;
1168 /* Note: We don't know how many bank2 sensors / pwms there really are,
1169 but in order to "detect" this we need to read the maximum amount
1170 anyways. If we read sensors/pwms not there we'll just read crap
1171 this can't hurt. We need the detection because we don't want
1172 unwanted writes, which will hurt! */
1173 for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
1174 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
1175 &data->bank2_value[i], 1,
1176 ABIT_UGURU_MAX_RETRIES) != 1)
1177 goto abituguru_probe_error;
1178 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2+1, i,
1179 data->bank2_settings[i], 2,
1180 ABIT_UGURU_MAX_RETRIES) != 2)
1181 goto abituguru_probe_error;
1183 for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
1184 if (abituguru_read(data, ABIT_UGURU_FAN_PWM, i,
1185 data->pwm_settings[i], 5,
1186 ABIT_UGURU_MAX_RETRIES) != 5)
1187 goto abituguru_probe_error;
1189 data->last_updated = jiffies;
1191 /* Detect sensor types and fill the sysfs attr for bank1 */
1192 sysfs_attr_i = 0;
1193 sysfs_filename = data->sysfs_names;
1194 sysfs_names_free = ABITUGURU_SYSFS_NAMES_LENGTH;
1195 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1196 res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
1197 if (res < 0)
1198 goto abituguru_probe_error;
1199 if (res == ABIT_UGURU_NC)
1200 continue;
1202 /* res 1 (temp) sensors have 7 sysfs entries, 0 (in) 9 */
1203 for (j = 0; j < (res ? 7 : 9); j++) {
1204 used = snprintf(sysfs_filename, sysfs_names_free,
1205 abituguru_sysfs_bank1_templ[res][j].dev_attr.
1206 attr.name, data->bank1_sensors[res] + res)
1207 + 1;
1208 data->sysfs_attr[sysfs_attr_i] =
1209 abituguru_sysfs_bank1_templ[res][j];
1210 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1211 sysfs_filename;
1212 data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
1213 sysfs_filename += used;
1214 sysfs_names_free -= used;
1215 sysfs_attr_i++;
1217 data->bank1_max_value[probe_order[i]] =
1218 abituguru_bank1_max_value[res];
1219 data->bank1_address[res][data->bank1_sensors[res]] =
1220 probe_order[i];
1221 data->bank1_sensors[res]++;
1223 /* Detect number of sensors and fill the sysfs attr for bank2 (fans) */
1224 abituguru_detect_no_bank2_sensors(data);
1225 for (i = 0; i < data->bank2_sensors; i++) {
1226 for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_fan_templ); j++) {
1227 used = snprintf(sysfs_filename, sysfs_names_free,
1228 abituguru_sysfs_fan_templ[j].dev_attr.attr.name,
1229 i + 1) + 1;
1230 data->sysfs_attr[sysfs_attr_i] =
1231 abituguru_sysfs_fan_templ[j];
1232 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1233 sysfs_filename;
1234 data->sysfs_attr[sysfs_attr_i].index = i;
1235 sysfs_filename += used;
1236 sysfs_names_free -= used;
1237 sysfs_attr_i++;
1240 /* Detect number of sensors and fill the sysfs attr for pwms */
1241 abituguru_detect_no_pwms(data);
1242 for (i = 0; i < data->pwms; i++) {
1243 for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_pwm_templ); j++) {
1244 used = snprintf(sysfs_filename, sysfs_names_free,
1245 abituguru_sysfs_pwm_templ[j].dev_attr.attr.name,
1246 i + 1) + 1;
1247 data->sysfs_attr[sysfs_attr_i] =
1248 abituguru_sysfs_pwm_templ[j];
1249 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1250 sysfs_filename;
1251 data->sysfs_attr[sysfs_attr_i].index = i;
1252 sysfs_filename += used;
1253 sysfs_names_free -= used;
1254 sysfs_attr_i++;
1257 /* Fail safe check, this should never happen! */
1258 if (sysfs_names_free < 0) {
1259 printk(KERN_ERR ABIT_UGURU_NAME ": Fatal error ran out of "
1260 "space for sysfs attr names. This should never "
1261 "happen please report to the abituguru maintainer "
1262 "(see MAINTAINERS)\n");
1263 res = -ENAMETOOLONG;
1264 goto abituguru_probe_error;
1266 printk(KERN_INFO ABIT_UGURU_NAME ": found Abit uGuru\n");
1268 /* Register sysfs hooks */
1269 data->class_dev = hwmon_device_register(&pdev->dev);
1270 if (IS_ERR(data->class_dev)) {
1271 res = PTR_ERR(data->class_dev);
1272 goto abituguru_probe_error;
1274 for (i = 0; i < sysfs_attr_i; i++)
1275 device_create_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1276 for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1277 device_create_file(&pdev->dev,
1278 &abituguru_sysfs_attr[i].dev_attr);
1280 return 0;
1282 abituguru_probe_error:
1283 kfree(data);
1284 return res;
1287 static int __devexit abituguru_remove(struct platform_device *pdev)
1289 struct abituguru_data *data = platform_get_drvdata(pdev);
1291 platform_set_drvdata(pdev, NULL);
1292 hwmon_device_unregister(data->class_dev);
1293 kfree(data);
1295 return 0;
1298 static struct abituguru_data *abituguru_update_device(struct device *dev)
1300 int i, err;
1301 struct abituguru_data *data = dev_get_drvdata(dev);
1302 /* fake a complete successful read if no update necessary. */
1303 char success = 1;
1305 mutex_lock(&data->update_lock);
1306 if (time_after(jiffies, data->last_updated + HZ)) {
1307 success = 0;
1308 if ((err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1309 data->alarms, 3, 0)) != 3)
1310 goto LEAVE_UPDATE;
1311 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1312 if ((err = abituguru_read(data,
1313 ABIT_UGURU_SENSOR_BANK1, i,
1314 &data->bank1_value[i], 1, 0)) != 1)
1315 goto LEAVE_UPDATE;
1316 if ((err = abituguru_read(data,
1317 ABIT_UGURU_SENSOR_BANK1 + 1, i,
1318 data->bank1_settings[i], 3, 0)) != 3)
1319 goto LEAVE_UPDATE;
1321 for (i = 0; i < data->bank2_sensors; i++)
1322 if ((err = abituguru_read(data,
1323 ABIT_UGURU_SENSOR_BANK2, i,
1324 &data->bank2_value[i], 1, 0)) != 1)
1325 goto LEAVE_UPDATE;
1326 /* success! */
1327 success = 1;
1328 data->update_timeouts = 0;
1329 LEAVE_UPDATE:
1330 /* handle timeout condition */
1331 if (!success && (err == -EBUSY || err >= 0)) {
1332 /* No overflow please */
1333 if (data->update_timeouts < 255u)
1334 data->update_timeouts++;
1335 if (data->update_timeouts <= ABIT_UGURU_MAX_TIMEOUTS) {
1336 ABIT_UGURU_DEBUG(3, "timeout exceeded, will "
1337 "try again next update\n");
1338 /* Just a timeout, fake a successful read */
1339 success = 1;
1340 } else
1341 ABIT_UGURU_DEBUG(1, "timeout exceeded %d "
1342 "times waiting for more input state\n",
1343 (int)data->update_timeouts);
1345 /* On success set last_updated */
1346 if (success)
1347 data->last_updated = jiffies;
1349 mutex_unlock(&data->update_lock);
1351 if (success)
1352 return data;
1353 else
1354 return NULL;
1357 #ifdef CONFIG_PM
1358 static int abituguru_suspend(struct platform_device *pdev, pm_message_t state)
1360 struct abituguru_data *data = platform_get_drvdata(pdev);
1361 /* make sure all communications with the uguru are done and no new
1362 ones are started */
1363 mutex_lock(&data->update_lock);
1364 return 0;
1367 static int abituguru_resume(struct platform_device *pdev)
1369 struct abituguru_data *data = platform_get_drvdata(pdev);
1370 /* See if the uGuru is still ready */
1371 if (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT)
1372 data->uguru_ready = 0;
1373 mutex_unlock(&data->update_lock);
1374 return 0;
1376 #else
1377 #define abituguru_suspend NULL
1378 #define abituguru_resume NULL
1379 #endif /* CONFIG_PM */
1381 static struct platform_driver abituguru_driver = {
1382 .driver = {
1383 .owner = THIS_MODULE,
1384 .name = ABIT_UGURU_NAME,
1386 .probe = abituguru_probe,
1387 .remove = __devexit_p(abituguru_remove),
1388 .suspend = abituguru_suspend,
1389 .resume = abituguru_resume,
1392 static int __init abituguru_detect(void)
1394 /* See if there is an uguru there. After a reboot uGuru will hold 0x00
1395 at DATA and 0xAC, when this driver has already been loaded once
1396 DATA will hold 0x08. For most uGuru's CMD will hold 0xAC in either
1397 scenario but some will hold 0x00.
1398 Some uGuru's initally hold 0x09 at DATA and will only hold 0x08
1399 after reading CMD first, so CMD must be read first! */
1400 u8 cmd_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_CMD);
1401 u8 data_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_DATA);
1402 if (((data_val == 0x00) || (data_val == 0x08)) &&
1403 ((cmd_val == 0x00) || (cmd_val == 0xAC)))
1404 return ABIT_UGURU_BASE;
1406 ABIT_UGURU_DEBUG(2, "no Abit uGuru found, data = 0x%02X, cmd = "
1407 "0x%02X\n", (unsigned int)data_val, (unsigned int)cmd_val);
1409 if (force) {
1410 printk(KERN_INFO ABIT_UGURU_NAME ": Assuming Abit uGuru is "
1411 "present because of \"force\" parameter\n");
1412 return ABIT_UGURU_BASE;
1415 /* No uGuru found */
1416 return -ENODEV;
1419 static struct platform_device *abituguru_pdev;
1421 static int __init abituguru_init(void)
1423 int address, err;
1424 struct resource res = { .flags = IORESOURCE_IO };
1426 address = abituguru_detect();
1427 if (address < 0)
1428 return address;
1430 err = platform_driver_register(&abituguru_driver);
1431 if (err)
1432 goto exit;
1434 abituguru_pdev = platform_device_alloc(ABIT_UGURU_NAME, address);
1435 if (!abituguru_pdev) {
1436 printk(KERN_ERR ABIT_UGURU_NAME
1437 ": Device allocation failed\n");
1438 err = -ENOMEM;
1439 goto exit_driver_unregister;
1442 res.start = address;
1443 res.end = address + ABIT_UGURU_REGION_LENGTH - 1;
1444 res.name = ABIT_UGURU_NAME;
1446 err = platform_device_add_resources(abituguru_pdev, &res, 1);
1447 if (err) {
1448 printk(KERN_ERR ABIT_UGURU_NAME
1449 ": Device resource addition failed (%d)\n", err);
1450 goto exit_device_put;
1453 err = platform_device_add(abituguru_pdev);
1454 if (err) {
1455 printk(KERN_ERR ABIT_UGURU_NAME
1456 ": Device addition failed (%d)\n", err);
1457 goto exit_device_put;
1460 return 0;
1462 exit_device_put:
1463 platform_device_put(abituguru_pdev);
1464 exit_driver_unregister:
1465 platform_driver_unregister(&abituguru_driver);
1466 exit:
1467 return err;
1470 static void __exit abituguru_exit(void)
1472 platform_device_unregister(abituguru_pdev);
1473 platform_driver_unregister(&abituguru_driver);
1476 MODULE_AUTHOR("Hans de Goede <j.w.r.degoede@hhs.nl>");
1477 MODULE_DESCRIPTION("Abit uGuru Sensor device");
1478 MODULE_LICENSE("GPL");
1480 module_init(abituguru_init);
1481 module_exit(abituguru_exit);