2 * abituguru.c Copyright (c) 2005-2006 Hans de Goede <hdegoede@redhat.com>
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!
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/module.h>
27 #include <linux/sched.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/jiffies.h>
31 #include <linux/mutex.h>
32 #include <linux/err.h>
33 #include <linux/delay.h>
34 #include <linux/platform_device.h>
35 #include <linux/hwmon.h>
36 #include <linux/hwmon-sysfs.h>
37 #include <linux/dmi.h>
41 #define ABIT_UGURU_ALARM_BANK 0x20 /* 1x 3 bytes */
42 #define ABIT_UGURU_SENSOR_BANK1 0x21 /* 16x volt and temp */
43 #define ABIT_UGURU_FAN_PWM 0x24 /* 3x 5 bytes */
44 #define ABIT_UGURU_SENSOR_BANK2 0x26 /* fans */
45 /* max nr of sensors in bank1, a bank1 sensor can be in, temp or nc */
46 #define ABIT_UGURU_MAX_BANK1_SENSORS 16
48 * Warning if you increase one of the 2 MAX defines below to 10 or higher you
49 * should adjust the belonging _NAMES_LENGTH macro for the 2 digit number!
51 /* max nr of sensors in bank2, currently mb's with max 6 fans are known */
52 #define ABIT_UGURU_MAX_BANK2_SENSORS 6
53 /* max nr of pwm outputs, currently mb's with max 5 pwm outputs are known */
54 #define ABIT_UGURU_MAX_PWMS 5
55 /* uGuru sensor bank 1 flags */ /* Alarm if: */
56 #define ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE 0x01 /* temp over warn */
57 #define ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE 0x02 /* volt over max */
58 #define ABIT_UGURU_VOLT_LOW_ALARM_ENABLE 0x04 /* volt under min */
59 #define ABIT_UGURU_TEMP_HIGH_ALARM_FLAG 0x10 /* temp is over warn */
60 #define ABIT_UGURU_VOLT_HIGH_ALARM_FLAG 0x20 /* volt is over max */
61 #define ABIT_UGURU_VOLT_LOW_ALARM_FLAG 0x40 /* volt is under min */
62 /* uGuru sensor bank 2 flags */ /* Alarm if: */
63 #define ABIT_UGURU_FAN_LOW_ALARM_ENABLE 0x01 /* fan under min */
64 /* uGuru sensor bank common flags */
65 #define ABIT_UGURU_BEEP_ENABLE 0x08 /* beep if alarm */
66 #define ABIT_UGURU_SHUTDOWN_ENABLE 0x80 /* shutdown if alarm */
67 /* uGuru fan PWM (speed control) flags */
68 #define ABIT_UGURU_FAN_PWM_ENABLE 0x80 /* enable speed control */
69 /* Values used for conversion */
70 #define ABIT_UGURU_FAN_MAX 15300 /* RPM */
71 /* Bank1 sensor types */
72 #define ABIT_UGURU_IN_SENSOR 0
73 #define ABIT_UGURU_TEMP_SENSOR 1
74 #define ABIT_UGURU_NC 2
76 * In many cases we need to wait for the uGuru to reach a certain status, most
77 * of the time it will reach this status within 30 - 90 ISA reads, and thus we
78 * can best busy wait. This define gives the total amount of reads to try.
80 #define ABIT_UGURU_WAIT_TIMEOUT 125
82 * However sometimes older versions of the uGuru seem to be distracted and they
83 * do not respond for a long time. To handle this we sleep before each of the
84 * last ABIT_UGURU_WAIT_TIMEOUT_SLEEP tries.
86 #define ABIT_UGURU_WAIT_TIMEOUT_SLEEP 5
88 * Normally all expected status in abituguru_ready, are reported after the
89 * first read, but sometimes not and we need to poll.
91 #define ABIT_UGURU_READY_TIMEOUT 5
92 /* Maximum 3 retries on timedout reads/writes, delay 200 ms before retrying */
93 #define ABIT_UGURU_MAX_RETRIES 3
94 #define ABIT_UGURU_RETRY_DELAY (HZ/5)
95 /* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is an error */
96 #define ABIT_UGURU_MAX_TIMEOUTS 2
98 #define ABIT_UGURU_NAME "abituguru"
99 #define ABIT_UGURU_DEBUG(level, format, arg...) \
100 if (level <= verbose) \
101 printk(KERN_DEBUG ABIT_UGURU_NAME ": " format , ## arg)
102 /* Macros to help calculate the sysfs_names array length */
104 * sum of strlen of: in??_input\0, in??_{min,max}\0, in??_{min,max}_alarm\0,
105 * in??_{min,max}_alarm_enable\0, in??_beep\0, in??_shutdown\0
107 #define ABITUGURU_IN_NAMES_LENGTH (11 + 2 * 9 + 2 * 15 + 2 * 22 + 10 + 14)
109 * sum of strlen of: temp??_input\0, temp??_max\0, temp??_crit\0,
110 * temp??_alarm\0, temp??_alarm_enable\0, temp??_beep\0, temp??_shutdown\0
112 #define ABITUGURU_TEMP_NAMES_LENGTH (13 + 11 + 12 + 13 + 20 + 12 + 16)
114 * sum of strlen of: fan?_input\0, fan?_min\0, fan?_alarm\0,
115 * fan?_alarm_enable\0, fan?_beep\0, fan?_shutdown\0
117 #define ABITUGURU_FAN_NAMES_LENGTH (11 + 9 + 11 + 18 + 10 + 14)
119 * sum of strlen of: pwm?_enable\0, pwm?_auto_channels_temp\0,
120 * pwm?_auto_point{1,2}_pwm\0, pwm?_auto_point{1,2}_temp\0
122 #define ABITUGURU_PWM_NAMES_LENGTH (12 + 24 + 2 * 21 + 2 * 22)
123 /* IN_NAMES_LENGTH > TEMP_NAMES_LENGTH so assume all bank1 sensors are in */
124 #define ABITUGURU_SYSFS_NAMES_LENGTH ( \
125 ABIT_UGURU_MAX_BANK1_SENSORS * ABITUGURU_IN_NAMES_LENGTH + \
126 ABIT_UGURU_MAX_BANK2_SENSORS * ABITUGURU_FAN_NAMES_LENGTH + \
127 ABIT_UGURU_MAX_PWMS * ABITUGURU_PWM_NAMES_LENGTH)
130 * All the macros below are named identical to the oguru and oguru2 programs
131 * reverse engineered by Olle Sandberg, hence the names might not be 100%
132 * logical. I could come up with better names, but I prefer keeping the names
133 * identical so that this driver can be compared with his work more easily.
135 /* Two i/o-ports are used by uGuru */
136 #define ABIT_UGURU_BASE 0x00E0
137 /* Used to tell uGuru what to read and to read the actual data */
138 #define ABIT_UGURU_CMD 0x00
139 /* Mostly used to check if uGuru is busy */
140 #define ABIT_UGURU_DATA 0x04
141 #define ABIT_UGURU_REGION_LENGTH 5
143 #define ABIT_UGURU_STATUS_WRITE 0x00 /* Ready to be written */
144 #define ABIT_UGURU_STATUS_READ 0x01 /* Ready to be read */
145 #define ABIT_UGURU_STATUS_INPUT 0x08 /* More input */
146 #define ABIT_UGURU_STATUS_READY 0x09 /* Ready to be written */
149 /* in (Volt) sensors go up to 3494 mV, temp to 255000 millidegrees Celsius */
150 static const int abituguru_bank1_max_value[2] = { 3494, 255000 };
152 * Min / Max allowed values for sensor2 (fan) alarm threshold, these values
153 * correspond to 300-3000 RPM
155 static const u8 abituguru_bank2_min_threshold = 5;
156 static const u8 abituguru_bank2_max_threshold = 50;
158 * Register 0 is a bitfield, 1 and 2 are pwm settings (255 = 100%), 3 and 4
159 * are temperature trip points.
161 static const int abituguru_pwm_settings_multiplier[5] = { 0, 1, 1, 1000, 1000 };
163 * Min / Max allowed values for pwm_settings. Note: pwm1 (CPU fan) is a
164 * special case the minium allowed pwm% setting for this is 30% (77) on
165 * some MB's this special case is handled in the code!
167 static const u8 abituguru_pwm_min[5] = { 0, 170, 170, 25, 25 };
168 static const u8 abituguru_pwm_max[5] = { 0, 255, 255, 75, 75 };
171 /* Insmod parameters */
173 module_param(force, bool, 0);
174 MODULE_PARM_DESC(force, "Set to one to force detection.");
175 static int bank1_types[ABIT_UGURU_MAX_BANK1_SENSORS] = { -1, -1, -1, -1, -1,
176 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
177 module_param_array(bank1_types, int, NULL, 0);
178 MODULE_PARM_DESC(bank1_types, "Bank1 sensortype autodetection override:\n"
183 static int fan_sensors;
184 module_param(fan_sensors, int, 0);
185 MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru "
188 module_param(pwms, int, 0);
189 MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru "
192 /* Default verbose is 2, since this driver is still in the testing phase */
193 static int verbose = 2;
194 module_param(verbose, int, 0644);
195 MODULE_PARM_DESC(verbose, "How verbose should the driver be? (0-3):\n"
197 " 1 + verbose error reporting\n"
198 " 2 + sensors type probing info\n"
199 " 3 + retryable error reporting");
203 * For the Abit uGuru, we need to keep some data in memory.
204 * The structure is dynamically allocated, at the same time when a new
205 * abituguru device is allocated.
207 struct abituguru_data {
208 struct device *hwmon_dev; /* hwmon registered device */
209 struct mutex update_lock; /* protect access to data and uGuru */
210 unsigned long last_updated; /* In jiffies */
211 unsigned short addr; /* uguru base address */
212 char uguru_ready; /* is the uguru in ready state? */
213 unsigned char update_timeouts; /*
214 * number of update timeouts since last
219 * The sysfs attr and their names are generated automatically, for bank1
220 * we cannot use a predefined array because we don't know beforehand
221 * of a sensor is a volt or a temp sensor, for bank2 and the pwms its
222 * easier todo things the same way. For in sensors we have 9 (temp 7)
223 * sysfs entries per sensor, for bank2 and pwms 6.
225 struct sensor_device_attribute_2 sysfs_attr[
226 ABIT_UGURU_MAX_BANK1_SENSORS * 9 +
227 ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6];
228 /* Buffer to store the dynamically generated sysfs names */
229 char sysfs_names[ABITUGURU_SYSFS_NAMES_LENGTH];
232 /* number of and addresses of [0] in, [1] temp sensors */
234 u8 bank1_address[2][ABIT_UGURU_MAX_BANK1_SENSORS];
235 u8 bank1_value[ABIT_UGURU_MAX_BANK1_SENSORS];
237 * This array holds 3 entries per sensor for the bank 1 sensor settings
238 * (flags, min, max for voltage / flags, warn, shutdown for temp).
240 u8 bank1_settings[ABIT_UGURU_MAX_BANK1_SENSORS][3];
242 * Maximum value for each sensor used for scaling in mV/millidegrees
245 int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS];
247 /* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */
248 u8 bank2_sensors; /* actual number of bank2 sensors found */
249 u8 bank2_value[ABIT_UGURU_MAX_BANK2_SENSORS];
250 u8 bank2_settings[ABIT_UGURU_MAX_BANK2_SENSORS][2]; /* flags, min */
252 /* Alarms 2 bytes for bank1, 1 byte for bank2 */
255 /* Fan PWM (speed control) 5 bytes per PWM */
256 u8 pwms; /* actual number of pwms found */
257 u8 pwm_settings[ABIT_UGURU_MAX_PWMS][5];
260 static const char *never_happen = "This should never happen.";
261 static const char *report_this =
262 "Please report this to the abituguru maintainer (see MAINTAINERS)";
264 /* wait till the uguru is in the specified state */
265 static int abituguru_wait(struct abituguru_data *data, u8 state)
267 int timeout = ABIT_UGURU_WAIT_TIMEOUT;
269 while (inb_p(data->addr + ABIT_UGURU_DATA) != state) {
274 * sleep a bit before our last few tries, see the comment on
275 * this where ABIT_UGURU_WAIT_TIMEOUT_SLEEP is defined.
277 if (timeout <= ABIT_UGURU_WAIT_TIMEOUT_SLEEP)
283 /* Put the uguru in ready for input state */
284 static int abituguru_ready(struct abituguru_data *data)
286 int timeout = ABIT_UGURU_READY_TIMEOUT;
288 if (data->uguru_ready)
291 /* Reset? / Prepare for next read/write cycle */
292 outb(0x00, data->addr + ABIT_UGURU_DATA);
294 /* Wait till the uguru is ready */
295 if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) {
297 "timeout exceeded waiting for ready state\n");
301 /* Cmd port MUST be read now and should contain 0xAC */
302 while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
306 "CMD reg does not hold 0xAC after ready command\n");
313 * After this the ABIT_UGURU_DATA port should contain
314 * ABIT_UGURU_STATUS_INPUT
316 timeout = ABIT_UGURU_READY_TIMEOUT;
317 while (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT) {
321 "state != more input after ready command\n");
327 data->uguru_ready = 1;
332 * Send the bank and then sensor address to the uGuru for the next read/write
333 * cycle. This function gets called as the first part of a read/write by
334 * abituguru_read and abituguru_write. This function should never be
335 * called by any other function.
337 static int abituguru_send_address(struct abituguru_data *data,
338 u8 bank_addr, u8 sensor_addr, int retries)
341 * assume the caller does error handling itself if it has not requested
342 * any retries, and thus be quiet.
344 int report_errors = retries;
348 * Make sure the uguru is ready and then send the bank address,
349 * after this the uguru is no longer "ready".
351 if (abituguru_ready(data) != 0)
353 outb(bank_addr, data->addr + ABIT_UGURU_DATA);
354 data->uguru_ready = 0;
357 * Wait till the uguru is ABIT_UGURU_STATUS_INPUT state again
358 * and send the sensor addr
360 if (abituguru_wait(data, ABIT_UGURU_STATUS_INPUT)) {
362 ABIT_UGURU_DEBUG(3, "timeout exceeded "
363 "waiting for more input state, %d "
364 "tries remaining\n", retries);
365 set_current_state(TASK_UNINTERRUPTIBLE);
366 schedule_timeout(ABIT_UGURU_RETRY_DELAY);
371 ABIT_UGURU_DEBUG(1, "timeout exceeded "
372 "waiting for more input state "
373 "(bank: %d)\n", (int)bank_addr);
376 outb(sensor_addr, data->addr + ABIT_UGURU_CMD);
382 * Read count bytes from sensor sensor_addr in bank bank_addr and store the
383 * result in buf, retry the send address part of the read retries times.
385 static int abituguru_read(struct abituguru_data *data,
386 u8 bank_addr, u8 sensor_addr, u8 *buf, int count, int retries)
390 /* Send the address */
391 i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
395 /* And read the data */
396 for (i = 0; i < count; i++) {
397 if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
398 ABIT_UGURU_DEBUG(retries ? 1 : 3,
399 "timeout exceeded waiting for "
400 "read state (bank: %d, sensor: %d)\n",
401 (int)bank_addr, (int)sensor_addr);
404 buf[i] = inb(data->addr + ABIT_UGURU_CMD);
407 /* Last put the chip back in ready state */
408 abituguru_ready(data);
414 * Write count bytes from buf to sensor sensor_addr in bank bank_addr, the send
415 * address part of the write is always retried ABIT_UGURU_MAX_RETRIES times.
417 static int abituguru_write(struct abituguru_data *data,
418 u8 bank_addr, u8 sensor_addr, u8 *buf, int count)
421 * We use the ready timeout as we have to wait for 0xAC just like the
424 int i, timeout = ABIT_UGURU_READY_TIMEOUT;
426 /* Send the address */
427 i = abituguru_send_address(data, bank_addr, sensor_addr,
428 ABIT_UGURU_MAX_RETRIES);
432 /* And write the data */
433 for (i = 0; i < count; i++) {
434 if (abituguru_wait(data, ABIT_UGURU_STATUS_WRITE)) {
435 ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
436 "write state (bank: %d, sensor: %d)\n",
437 (int)bank_addr, (int)sensor_addr);
440 outb(buf[i], data->addr + ABIT_UGURU_CMD);
444 * Now we need to wait till the chip is ready to be read again,
445 * so that we can read 0xAC as confirmation that our write has
448 if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
449 ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for read state "
450 "after write (bank: %d, sensor: %d)\n", (int)bank_addr,
455 /* Cmd port MUST be read now and should contain 0xAC */
456 while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
459 ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after "
460 "write (bank: %d, sensor: %d)\n",
461 (int)bank_addr, (int)sensor_addr);
467 /* Last put the chip back in ready state */
468 abituguru_ready(data);
474 * Detect sensor type. Temp and Volt sensors are enabled with
475 * different masks and will ignore enable masks not meant for them.
476 * This enables us to test what kind of sensor we're dealing with.
477 * By setting the alarm thresholds so that we will always get an
478 * alarm for sensor type X and then enabling the sensor as sensor type
479 * X, if we then get an alarm it is a sensor of type X.
482 abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
485 u8 val, test_flag, buf[3];
486 int i, ret = -ENODEV; /* error is the most common used retval :| */
488 /* If overriden by the user return the user selected type */
489 if (bank1_types[sensor_addr] >= ABIT_UGURU_IN_SENSOR &&
490 bank1_types[sensor_addr] <= ABIT_UGURU_NC) {
491 ABIT_UGURU_DEBUG(2, "assuming sensor type %d for bank1 sensor "
492 "%d because of \"bank1_types\" module param\n",
493 bank1_types[sensor_addr], (int)sensor_addr);
494 return bank1_types[sensor_addr];
497 /* First read the sensor and the current settings */
498 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, sensor_addr, &val,
499 1, ABIT_UGURU_MAX_RETRIES) != 1)
502 /* Test val is sane / usable for sensor type detection. */
503 if ((val < 10u) || (val > 250u)) {
504 pr_warn("bank1-sensor: %d reading (%d) too close to limits, "
505 "unable to determine sensor type, skipping sensor\n",
506 (int)sensor_addr, (int)val);
508 * assume no sensor is there for sensors for which we can't
509 * determine the sensor type because their reading is too close
510 * to their limits, this usually means no sensor is there.
512 return ABIT_UGURU_NC;
515 ABIT_UGURU_DEBUG(2, "testing bank1 sensor %d\n", (int)sensor_addr);
517 * Volt sensor test, enable volt low alarm, set min value ridicously
518 * high, or vica versa if the reading is very high. If its a volt
519 * sensor this should always give us an alarm.
522 buf[0] = ABIT_UGURU_VOLT_LOW_ALARM_ENABLE;
525 test_flag = ABIT_UGURU_VOLT_LOW_ALARM_FLAG;
527 buf[0] = ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE;
530 test_flag = ABIT_UGURU_VOLT_HIGH_ALARM_FLAG;
533 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
535 goto abituguru_detect_bank1_sensor_type_exit;
537 * Now we need 20 ms to give the uguru time to read the sensors
538 * and raise a voltage alarm
540 set_current_state(TASK_UNINTERRUPTIBLE);
541 schedule_timeout(HZ/50);
542 /* Check for alarm and check the alarm is a volt low alarm. */
543 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
544 ABIT_UGURU_MAX_RETRIES) != 3)
545 goto abituguru_detect_bank1_sensor_type_exit;
546 if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
547 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
549 ABIT_UGURU_MAX_RETRIES) != 3)
550 goto abituguru_detect_bank1_sensor_type_exit;
551 if (buf[0] & test_flag) {
552 ABIT_UGURU_DEBUG(2, " found volt sensor\n");
553 ret = ABIT_UGURU_IN_SENSOR;
554 goto abituguru_detect_bank1_sensor_type_exit;
556 ABIT_UGURU_DEBUG(2, " alarm raised during volt "
557 "sensor test, but volt range flag not set\n");
559 ABIT_UGURU_DEBUG(2, " alarm not raised during volt sensor "
563 * Temp sensor test, enable sensor as a temp sensor, set beep value
564 * ridicously low (but not too low, otherwise uguru ignores it).
565 * If its a temp sensor this should always give us an alarm.
567 buf[0] = ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE;
570 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
572 goto abituguru_detect_bank1_sensor_type_exit;
574 * Now we need 50 ms to give the uguru time to read the sensors
575 * and raise a temp alarm
577 set_current_state(TASK_UNINTERRUPTIBLE);
578 schedule_timeout(HZ/20);
579 /* Check for alarm and check the alarm is a temp high alarm. */
580 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
581 ABIT_UGURU_MAX_RETRIES) != 3)
582 goto abituguru_detect_bank1_sensor_type_exit;
583 if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
584 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
586 ABIT_UGURU_MAX_RETRIES) != 3)
587 goto abituguru_detect_bank1_sensor_type_exit;
588 if (buf[0] & ABIT_UGURU_TEMP_HIGH_ALARM_FLAG) {
589 ABIT_UGURU_DEBUG(2, " found temp sensor\n");
590 ret = ABIT_UGURU_TEMP_SENSOR;
591 goto abituguru_detect_bank1_sensor_type_exit;
593 ABIT_UGURU_DEBUG(2, " alarm raised during temp "
594 "sensor test, but temp high flag not set\n");
596 ABIT_UGURU_DEBUG(2, " alarm not raised during temp sensor "
600 abituguru_detect_bank1_sensor_type_exit:
602 * Restore original settings, failing here is really BAD, it has been
603 * reported that some BIOS-es hang when entering the uGuru menu with
604 * invalid settings present in the uGuru, so we try this 3 times.
606 for (i = 0; i < 3; i++)
607 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
608 sensor_addr, data->bank1_settings[sensor_addr],
612 pr_err("Fatal error could not restore original settings. %s %s\n",
613 never_happen, report_this);
620 * These functions try to find out how many sensors there are in bank2 and how
621 * many pwms there are. The purpose of this is to make sure that we don't give
622 * the user the possibility to change settings for non-existent sensors / pwm.
623 * The uGuru will happily read / write whatever memory happens to be after the
624 * memory storing the PWM settings when reading/writing to a PWM which is not
625 * there. Notice even if we detect a PWM which doesn't exist we normally won't
626 * write to it, unless the user tries to change the settings.
628 * Although the uGuru allows reading (settings) from non existing bank2
629 * sensors, my version of the uGuru does seem to stop writing to them, the
630 * write function above aborts in this case with:
631 * "CMD reg does not hold 0xAC after write"
633 * Notice these 2 tests are non destructive iow read-only tests, otherwise
634 * they would defeat their purpose. Although for the bank2_sensors detection a
635 * read/write test would be feasible because of the reaction above, I've
636 * however opted to stay on the safe side.
639 abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
643 if (fan_sensors > 0 && fan_sensors <= ABIT_UGURU_MAX_BANK2_SENSORS) {
644 data->bank2_sensors = fan_sensors;
645 ABIT_UGURU_DEBUG(2, "assuming %d fan sensors because of "
646 "\"fan_sensors\" module param\n",
647 (int)data->bank2_sensors);
651 ABIT_UGURU_DEBUG(2, "detecting number of fan sensors\n");
652 for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
654 * 0x89 are the known used bits:
655 * -0x80 enable shutdown
658 * All other bits should be 0, but on some motherboards
659 * 0x40 (bit 6) is also high for some of the fans??
661 if (data->bank2_settings[i][0] & ~0xC9) {
662 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
663 "to be a fan sensor: settings[0] = %02X\n",
664 i, (unsigned int)data->bank2_settings[i][0]);
668 /* check if the threshold is within the allowed range */
669 if (data->bank2_settings[i][1] <
670 abituguru_bank2_min_threshold) {
671 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
672 "to be a fan sensor: the threshold (%d) is "
673 "below the minimum (%d)\n", i,
674 (int)data->bank2_settings[i][1],
675 (int)abituguru_bank2_min_threshold);
678 if (data->bank2_settings[i][1] >
679 abituguru_bank2_max_threshold) {
680 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
681 "to be a fan sensor: the threshold (%d) is "
682 "above the maximum (%d)\n", i,
683 (int)data->bank2_settings[i][1],
684 (int)abituguru_bank2_max_threshold);
689 data->bank2_sensors = i;
690 ABIT_UGURU_DEBUG(2, " found: %d fan sensors\n",
691 (int)data->bank2_sensors);
695 abituguru_detect_no_pwms(struct abituguru_data *data)
699 if (pwms > 0 && pwms <= ABIT_UGURU_MAX_PWMS) {
701 ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
702 "\"pwms\" module param\n", (int)data->pwms);
706 ABIT_UGURU_DEBUG(2, "detecting number of PWM outputs\n");
707 for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
709 * 0x80 is the enable bit and the low
710 * nibble is which temp sensor to use,
711 * the other bits should be 0
713 if (data->pwm_settings[i][0] & ~0x8F) {
714 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
715 "to be a pwm channel: settings[0] = %02X\n",
716 i, (unsigned int)data->pwm_settings[i][0]);
721 * the low nibble must correspond to one of the temp sensors
724 for (j = 0; j < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR];
726 if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][j] ==
727 (data->pwm_settings[i][0] & 0x0F))
730 if (j == data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
731 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
732 "to be a pwm channel: %d is not a valid temp "
733 "sensor address\n", i,
734 data->pwm_settings[i][0] & 0x0F);
738 /* check if all other settings are within the allowed range */
739 for (j = 1; j < 5; j++) {
741 /* special case pwm1 min pwm% */
742 if ((i == 0) && ((j == 1) || (j == 2)))
745 min = abituguru_pwm_min[j];
746 if (data->pwm_settings[i][j] < min) {
747 ABIT_UGURU_DEBUG(2, " pwm channel %d does "
748 "not seem to be a pwm channel: "
749 "setting %d (%d) is below the minimum "
750 "value (%d)\n", i, j,
751 (int)data->pwm_settings[i][j],
753 goto abituguru_detect_no_pwms_exit;
755 if (data->pwm_settings[i][j] > abituguru_pwm_max[j]) {
756 ABIT_UGURU_DEBUG(2, " pwm channel %d does "
757 "not seem to be a pwm channel: "
758 "setting %d (%d) is above the maximum "
759 "value (%d)\n", i, j,
760 (int)data->pwm_settings[i][j],
761 (int)abituguru_pwm_max[j]);
762 goto abituguru_detect_no_pwms_exit;
766 /* check that min temp < max temp and min pwm < max pwm */
767 if (data->pwm_settings[i][1] >= data->pwm_settings[i][2]) {
768 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
769 "to be a pwm channel: min pwm (%d) >= "
771 (int)data->pwm_settings[i][1],
772 (int)data->pwm_settings[i][2]);
775 if (data->pwm_settings[i][3] >= data->pwm_settings[i][4]) {
776 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
777 "to be a pwm channel: min temp (%d) >= "
778 "max temp (%d)\n", i,
779 (int)data->pwm_settings[i][3],
780 (int)data->pwm_settings[i][4]);
785 abituguru_detect_no_pwms_exit:
787 ABIT_UGURU_DEBUG(2, " found: %d PWM outputs\n", (int)data->pwms);
791 * Following are the sysfs callback functions. These functions expect:
792 * sensor_device_attribute_2->index: sensor address/offset in the bank
793 * sensor_device_attribute_2->nr: register offset, bitmask or NA.
795 static struct abituguru_data *abituguru_update_device(struct device *dev);
797 static ssize_t show_bank1_value(struct device *dev,
798 struct device_attribute *devattr, char *buf)
800 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
801 struct abituguru_data *data = abituguru_update_device(dev);
804 return sprintf(buf, "%d\n", (data->bank1_value[attr->index] *
805 data->bank1_max_value[attr->index] + 128) / 255);
808 static ssize_t show_bank1_setting(struct device *dev,
809 struct device_attribute *devattr, char *buf)
811 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
812 struct abituguru_data *data = dev_get_drvdata(dev);
813 return sprintf(buf, "%d\n",
814 (data->bank1_settings[attr->index][attr->nr] *
815 data->bank1_max_value[attr->index] + 128) / 255);
818 static ssize_t show_bank2_value(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);
825 return sprintf(buf, "%d\n", (data->bank2_value[attr->index] *
826 ABIT_UGURU_FAN_MAX + 128) / 255);
829 static ssize_t show_bank2_setting(struct device *dev,
830 struct device_attribute *devattr, char *buf)
832 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
833 struct abituguru_data *data = dev_get_drvdata(dev);
834 return sprintf(buf, "%d\n",
835 (data->bank2_settings[attr->index][attr->nr] *
836 ABIT_UGURU_FAN_MAX + 128) / 255);
839 static ssize_t store_bank1_setting(struct device *dev, struct device_attribute
840 *devattr, const char *buf, size_t count)
842 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
843 struct abituguru_data *data = dev_get_drvdata(dev);
847 ret = kstrtoul(buf, 10, &val);
852 val = (val * 255 + data->bank1_max_value[attr->index] / 2) /
853 data->bank1_max_value[attr->index];
857 mutex_lock(&data->update_lock);
858 if (data->bank1_settings[attr->index][attr->nr] != val) {
859 u8 orig_val = data->bank1_settings[attr->index][attr->nr];
860 data->bank1_settings[attr->index][attr->nr] = val;
861 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
862 attr->index, data->bank1_settings[attr->index],
864 data->bank1_settings[attr->index][attr->nr] = orig_val;
868 mutex_unlock(&data->update_lock);
872 static ssize_t store_bank2_setting(struct device *dev, struct device_attribute
873 *devattr, const char *buf, size_t count)
875 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
876 struct abituguru_data *data = dev_get_drvdata(dev);
880 ret = kstrtoul(buf, 10, &val);
885 val = (val * 255 + ABIT_UGURU_FAN_MAX / 2) / ABIT_UGURU_FAN_MAX;
887 /* this check can be done before taking the lock */
888 if (val < abituguru_bank2_min_threshold ||
889 val > abituguru_bank2_max_threshold)
892 mutex_lock(&data->update_lock);
893 if (data->bank2_settings[attr->index][attr->nr] != val) {
894 u8 orig_val = data->bank2_settings[attr->index][attr->nr];
895 data->bank2_settings[attr->index][attr->nr] = val;
896 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK2 + 2,
897 attr->index, data->bank2_settings[attr->index],
899 data->bank2_settings[attr->index][attr->nr] = orig_val;
903 mutex_unlock(&data->update_lock);
907 static ssize_t show_bank1_alarm(struct device *dev,
908 struct device_attribute *devattr, char *buf)
910 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
911 struct abituguru_data *data = abituguru_update_device(dev);
915 * See if the alarm bit for this sensor is set, and if the
916 * alarm matches the type of alarm we're looking for (for volt
917 * it can be either low or high). The type is stored in a few
918 * readonly bits in the settings part of the relevant sensor.
919 * The bitmask of the type is passed to us in attr->nr.
921 if ((data->alarms[attr->index / 8] & (0x01 << (attr->index % 8))) &&
922 (data->bank1_settings[attr->index][0] & attr->nr))
923 return sprintf(buf, "1\n");
925 return sprintf(buf, "0\n");
928 static ssize_t show_bank2_alarm(struct device *dev,
929 struct device_attribute *devattr, char *buf)
931 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
932 struct abituguru_data *data = abituguru_update_device(dev);
935 if (data->alarms[2] & (0x01 << attr->index))
936 return sprintf(buf, "1\n");
938 return sprintf(buf, "0\n");
941 static ssize_t show_bank1_mask(struct device *dev,
942 struct device_attribute *devattr, char *buf)
944 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
945 struct abituguru_data *data = dev_get_drvdata(dev);
946 if (data->bank1_settings[attr->index][0] & attr->nr)
947 return sprintf(buf, "1\n");
949 return sprintf(buf, "0\n");
952 static ssize_t show_bank2_mask(struct device *dev,
953 struct device_attribute *devattr, char *buf)
955 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
956 struct abituguru_data *data = dev_get_drvdata(dev);
957 if (data->bank2_settings[attr->index][0] & attr->nr)
958 return sprintf(buf, "1\n");
960 return sprintf(buf, "0\n");
963 static ssize_t store_bank1_mask(struct device *dev,
964 struct device_attribute *devattr, const char *buf, size_t count)
966 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
967 struct abituguru_data *data = dev_get_drvdata(dev);
972 ret = kstrtoul(buf, 10, &mask);
977 mutex_lock(&data->update_lock);
978 orig_val = data->bank1_settings[attr->index][0];
981 data->bank1_settings[attr->index][0] |= attr->nr;
983 data->bank1_settings[attr->index][0] &= ~attr->nr;
985 if ((data->bank1_settings[attr->index][0] != orig_val) &&
986 (abituguru_write(data,
987 ABIT_UGURU_SENSOR_BANK1 + 2, attr->index,
988 data->bank1_settings[attr->index], 3) < 1)) {
989 data->bank1_settings[attr->index][0] = orig_val;
992 mutex_unlock(&data->update_lock);
996 static ssize_t store_bank2_mask(struct device *dev,
997 struct device_attribute *devattr, const char *buf, size_t count)
999 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1000 struct abituguru_data *data = dev_get_drvdata(dev);
1005 ret = kstrtoul(buf, 10, &mask);
1010 mutex_lock(&data->update_lock);
1011 orig_val = data->bank2_settings[attr->index][0];
1014 data->bank2_settings[attr->index][0] |= attr->nr;
1016 data->bank2_settings[attr->index][0] &= ~attr->nr;
1018 if ((data->bank2_settings[attr->index][0] != orig_val) &&
1019 (abituguru_write(data,
1020 ABIT_UGURU_SENSOR_BANK2 + 2, attr->index,
1021 data->bank2_settings[attr->index], 2) < 1)) {
1022 data->bank2_settings[attr->index][0] = orig_val;
1025 mutex_unlock(&data->update_lock);
1029 /* Fan PWM (speed control) */
1030 static ssize_t show_pwm_setting(struct device *dev,
1031 struct device_attribute *devattr, char *buf)
1033 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1034 struct abituguru_data *data = dev_get_drvdata(dev);
1035 return sprintf(buf, "%d\n", data->pwm_settings[attr->index][attr->nr] *
1036 abituguru_pwm_settings_multiplier[attr->nr]);
1039 static ssize_t store_pwm_setting(struct device *dev, struct device_attribute
1040 *devattr, const char *buf, size_t count)
1042 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1043 struct abituguru_data *data = dev_get_drvdata(dev);
1048 ret = kstrtoul(buf, 10, &val);
1053 val = (val + abituguru_pwm_settings_multiplier[attr->nr] / 2) /
1054 abituguru_pwm_settings_multiplier[attr->nr];
1056 /* special case pwm1 min pwm% */
1057 if ((attr->index == 0) && ((attr->nr == 1) || (attr->nr == 2)))
1060 min = abituguru_pwm_min[attr->nr];
1062 /* this check can be done before taking the lock */
1063 if (val < min || val > abituguru_pwm_max[attr->nr])
1066 mutex_lock(&data->update_lock);
1067 /* this check needs to be done after taking the lock */
1068 if ((attr->nr & 1) &&
1069 (val >= data->pwm_settings[attr->index][attr->nr + 1]))
1071 else if (!(attr->nr & 1) &&
1072 (val <= data->pwm_settings[attr->index][attr->nr - 1]))
1074 else if (data->pwm_settings[attr->index][attr->nr] != val) {
1075 u8 orig_val = data->pwm_settings[attr->index][attr->nr];
1076 data->pwm_settings[attr->index][attr->nr] = val;
1077 if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1078 attr->index, data->pwm_settings[attr->index],
1080 data->pwm_settings[attr->index][attr->nr] =
1085 mutex_unlock(&data->update_lock);
1089 static ssize_t show_pwm_sensor(struct device *dev,
1090 struct device_attribute *devattr, char *buf)
1092 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1093 struct abituguru_data *data = dev_get_drvdata(dev);
1096 * We need to walk to the temp sensor addresses to find what
1097 * the userspace id of the configured temp sensor is.
1099 for (i = 0; i < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]; i++)
1100 if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][i] ==
1101 (data->pwm_settings[attr->index][0] & 0x0F))
1102 return sprintf(buf, "%d\n", i+1);
1107 static ssize_t store_pwm_sensor(struct device *dev, struct device_attribute
1108 *devattr, const char *buf, size_t count)
1110 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1111 struct abituguru_data *data = dev_get_drvdata(dev);
1117 ret = kstrtoul(buf, 10, &val);
1121 if (val == 0 || val > data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR])
1126 mutex_lock(&data->update_lock);
1127 orig_val = data->pwm_settings[attr->index][0];
1128 address = data->bank1_address[ABIT_UGURU_TEMP_SENSOR][val];
1129 data->pwm_settings[attr->index][0] &= 0xF0;
1130 data->pwm_settings[attr->index][0] |= address;
1131 if (data->pwm_settings[attr->index][0] != orig_val) {
1132 if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1, attr->index,
1133 data->pwm_settings[attr->index], 5) < 1) {
1134 data->pwm_settings[attr->index][0] = orig_val;
1138 mutex_unlock(&data->update_lock);
1142 static ssize_t show_pwm_enable(struct device *dev,
1143 struct device_attribute *devattr, char *buf)
1145 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1146 struct abituguru_data *data = dev_get_drvdata(dev);
1148 if (data->pwm_settings[attr->index][0] & ABIT_UGURU_FAN_PWM_ENABLE)
1150 return sprintf(buf, "%d\n", res);
1153 static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1154 *devattr, const char *buf, size_t count)
1156 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1157 struct abituguru_data *data = dev_get_drvdata(dev);
1160 unsigned long user_val;
1162 ret = kstrtoul(buf, 10, &user_val);
1167 mutex_lock(&data->update_lock);
1168 orig_val = data->pwm_settings[attr->index][0];
1171 data->pwm_settings[attr->index][0] &=
1172 ~ABIT_UGURU_FAN_PWM_ENABLE;
1175 data->pwm_settings[attr->index][0] |= ABIT_UGURU_FAN_PWM_ENABLE;
1180 if ((data->pwm_settings[attr->index][0] != orig_val) &&
1181 (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1182 attr->index, data->pwm_settings[attr->index],
1184 data->pwm_settings[attr->index][0] = orig_val;
1187 mutex_unlock(&data->update_lock);
1191 static ssize_t show_name(struct device *dev,
1192 struct device_attribute *devattr, char *buf)
1194 return sprintf(buf, "%s\n", ABIT_UGURU_NAME);
1197 /* Sysfs attr templates, the real entries are generated automatically. */
1199 struct sensor_device_attribute_2 abituguru_sysfs_bank1_templ[2][9] = {
1201 SENSOR_ATTR_2(in%d_input, 0444, show_bank1_value, NULL, 0, 0),
1202 SENSOR_ATTR_2(in%d_min, 0644, show_bank1_setting,
1203 store_bank1_setting, 1, 0),
1204 SENSOR_ATTR_2(in%d_min_alarm, 0444, show_bank1_alarm, NULL,
1205 ABIT_UGURU_VOLT_LOW_ALARM_FLAG, 0),
1206 SENSOR_ATTR_2(in%d_max, 0644, show_bank1_setting,
1207 store_bank1_setting, 2, 0),
1208 SENSOR_ATTR_2(in%d_max_alarm, 0444, show_bank1_alarm, NULL,
1209 ABIT_UGURU_VOLT_HIGH_ALARM_FLAG, 0),
1210 SENSOR_ATTR_2(in%d_beep, 0644, show_bank1_mask,
1211 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1212 SENSOR_ATTR_2(in%d_shutdown, 0644, show_bank1_mask,
1213 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1214 SENSOR_ATTR_2(in%d_min_alarm_enable, 0644, show_bank1_mask,
1215 store_bank1_mask, ABIT_UGURU_VOLT_LOW_ALARM_ENABLE, 0),
1216 SENSOR_ATTR_2(in%d_max_alarm_enable, 0644, show_bank1_mask,
1217 store_bank1_mask, ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE, 0),
1219 SENSOR_ATTR_2(temp%d_input, 0444, show_bank1_value, NULL, 0, 0),
1220 SENSOR_ATTR_2(temp%d_alarm, 0444, show_bank1_alarm, NULL,
1221 ABIT_UGURU_TEMP_HIGH_ALARM_FLAG, 0),
1222 SENSOR_ATTR_2(temp%d_max, 0644, show_bank1_setting,
1223 store_bank1_setting, 1, 0),
1224 SENSOR_ATTR_2(temp%d_crit, 0644, show_bank1_setting,
1225 store_bank1_setting, 2, 0),
1226 SENSOR_ATTR_2(temp%d_beep, 0644, show_bank1_mask,
1227 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1228 SENSOR_ATTR_2(temp%d_shutdown, 0644, show_bank1_mask,
1229 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1230 SENSOR_ATTR_2(temp%d_alarm_enable, 0644, show_bank1_mask,
1231 store_bank1_mask, ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE, 0),
1235 static const struct sensor_device_attribute_2 abituguru_sysfs_fan_templ[6] = {
1236 SENSOR_ATTR_2(fan%d_input, 0444, show_bank2_value, NULL, 0, 0),
1237 SENSOR_ATTR_2(fan%d_alarm, 0444, show_bank2_alarm, NULL, 0, 0),
1238 SENSOR_ATTR_2(fan%d_min, 0644, show_bank2_setting,
1239 store_bank2_setting, 1, 0),
1240 SENSOR_ATTR_2(fan%d_beep, 0644, show_bank2_mask,
1241 store_bank2_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1242 SENSOR_ATTR_2(fan%d_shutdown, 0644, show_bank2_mask,
1243 store_bank2_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1244 SENSOR_ATTR_2(fan%d_alarm_enable, 0644, show_bank2_mask,
1245 store_bank2_mask, ABIT_UGURU_FAN_LOW_ALARM_ENABLE, 0),
1248 static const struct sensor_device_attribute_2 abituguru_sysfs_pwm_templ[6] = {
1249 SENSOR_ATTR_2(pwm%d_enable, 0644, show_pwm_enable,
1250 store_pwm_enable, 0, 0),
1251 SENSOR_ATTR_2(pwm%d_auto_channels_temp, 0644, show_pwm_sensor,
1252 store_pwm_sensor, 0, 0),
1253 SENSOR_ATTR_2(pwm%d_auto_point1_pwm, 0644, show_pwm_setting,
1254 store_pwm_setting, 1, 0),
1255 SENSOR_ATTR_2(pwm%d_auto_point2_pwm, 0644, show_pwm_setting,
1256 store_pwm_setting, 2, 0),
1257 SENSOR_ATTR_2(pwm%d_auto_point1_temp, 0644, show_pwm_setting,
1258 store_pwm_setting, 3, 0),
1259 SENSOR_ATTR_2(pwm%d_auto_point2_temp, 0644, show_pwm_setting,
1260 store_pwm_setting, 4, 0),
1263 static struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
1264 SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
1267 static int abituguru_probe(struct platform_device *pdev)
1269 struct abituguru_data *data;
1270 int i, j, used, sysfs_names_free, sysfs_attr_i, res = -ENODEV;
1271 char *sysfs_filename;
1274 * El weirdo probe order, to keep the sysfs order identical to the
1275 * BIOS and window-appliction listing order.
1277 const u8 probe_order[ABIT_UGURU_MAX_BANK1_SENSORS] = {
1278 0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E, 0x02,
1279 0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
1281 data = devm_kzalloc(&pdev->dev, sizeof(struct abituguru_data),
1286 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
1287 mutex_init(&data->update_lock);
1288 platform_set_drvdata(pdev, data);
1290 /* See if the uGuru is ready */
1291 if (inb_p(data->addr + ABIT_UGURU_DATA) == ABIT_UGURU_STATUS_INPUT)
1292 data->uguru_ready = 1;
1295 * Completely read the uGuru this has 2 purposes:
1296 * - testread / see if one really is there.
1297 * - make an in memory copy of all the uguru settings for future use.
1299 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1300 data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3)
1301 goto abituguru_probe_error;
1303 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1304 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, i,
1305 &data->bank1_value[i], 1,
1306 ABIT_UGURU_MAX_RETRIES) != 1)
1307 goto abituguru_probe_error;
1308 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1+1, i,
1309 data->bank1_settings[i], 3,
1310 ABIT_UGURU_MAX_RETRIES) != 3)
1311 goto abituguru_probe_error;
1314 * Note: We don't know how many bank2 sensors / pwms there really are,
1315 * but in order to "detect" this we need to read the maximum amount
1316 * anyways. If we read sensors/pwms not there we'll just read crap
1317 * this can't hurt. We need the detection because we don't want
1318 * unwanted writes, which will hurt!
1320 for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
1321 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
1322 &data->bank2_value[i], 1,
1323 ABIT_UGURU_MAX_RETRIES) != 1)
1324 goto abituguru_probe_error;
1325 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2+1, i,
1326 data->bank2_settings[i], 2,
1327 ABIT_UGURU_MAX_RETRIES) != 2)
1328 goto abituguru_probe_error;
1330 for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
1331 if (abituguru_read(data, ABIT_UGURU_FAN_PWM, i,
1332 data->pwm_settings[i], 5,
1333 ABIT_UGURU_MAX_RETRIES) != 5)
1334 goto abituguru_probe_error;
1336 data->last_updated = jiffies;
1338 /* Detect sensor types and fill the sysfs attr for bank1 */
1340 sysfs_filename = data->sysfs_names;
1341 sysfs_names_free = ABITUGURU_SYSFS_NAMES_LENGTH;
1342 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1343 res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
1345 goto abituguru_probe_error;
1346 if (res == ABIT_UGURU_NC)
1349 /* res 1 (temp) sensors have 7 sysfs entries, 0 (in) 9 */
1350 for (j = 0; j < (res ? 7 : 9); j++) {
1351 used = snprintf(sysfs_filename, sysfs_names_free,
1352 abituguru_sysfs_bank1_templ[res][j].dev_attr.
1353 attr.name, data->bank1_sensors[res] + res)
1355 data->sysfs_attr[sysfs_attr_i] =
1356 abituguru_sysfs_bank1_templ[res][j];
1357 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1359 data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
1360 sysfs_filename += used;
1361 sysfs_names_free -= used;
1364 data->bank1_max_value[probe_order[i]] =
1365 abituguru_bank1_max_value[res];
1366 data->bank1_address[res][data->bank1_sensors[res]] =
1368 data->bank1_sensors[res]++;
1370 /* Detect number of sensors and fill the sysfs attr for bank2 (fans) */
1371 abituguru_detect_no_bank2_sensors(data);
1372 for (i = 0; i < data->bank2_sensors; i++) {
1373 for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_fan_templ); j++) {
1374 used = snprintf(sysfs_filename, sysfs_names_free,
1375 abituguru_sysfs_fan_templ[j].dev_attr.attr.name,
1377 data->sysfs_attr[sysfs_attr_i] =
1378 abituguru_sysfs_fan_templ[j];
1379 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1381 data->sysfs_attr[sysfs_attr_i].index = i;
1382 sysfs_filename += used;
1383 sysfs_names_free -= used;
1387 /* Detect number of sensors and fill the sysfs attr for pwms */
1388 abituguru_detect_no_pwms(data);
1389 for (i = 0; i < data->pwms; i++) {
1390 for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_pwm_templ); j++) {
1391 used = snprintf(sysfs_filename, sysfs_names_free,
1392 abituguru_sysfs_pwm_templ[j].dev_attr.attr.name,
1394 data->sysfs_attr[sysfs_attr_i] =
1395 abituguru_sysfs_pwm_templ[j];
1396 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1398 data->sysfs_attr[sysfs_attr_i].index = i;
1399 sysfs_filename += used;
1400 sysfs_names_free -= used;
1404 /* Fail safe check, this should never happen! */
1405 if (sysfs_names_free < 0) {
1406 pr_err("Fatal error ran out of space for sysfs attr names. %s %s",
1407 never_happen, report_this);
1408 res = -ENAMETOOLONG;
1409 goto abituguru_probe_error;
1411 pr_info("found Abit uGuru\n");
1413 /* Register sysfs hooks */
1414 for (i = 0; i < sysfs_attr_i; i++)
1415 if (device_create_file(&pdev->dev,
1416 &data->sysfs_attr[i].dev_attr))
1417 goto abituguru_probe_error;
1418 for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1419 if (device_create_file(&pdev->dev,
1420 &abituguru_sysfs_attr[i].dev_attr))
1421 goto abituguru_probe_error;
1423 data->hwmon_dev = hwmon_device_register(&pdev->dev);
1424 if (!IS_ERR(data->hwmon_dev))
1425 return 0; /* success */
1427 res = PTR_ERR(data->hwmon_dev);
1428 abituguru_probe_error:
1429 for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
1430 device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1431 for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1432 device_remove_file(&pdev->dev,
1433 &abituguru_sysfs_attr[i].dev_attr);
1437 static int abituguru_remove(struct platform_device *pdev)
1440 struct abituguru_data *data = platform_get_drvdata(pdev);
1442 hwmon_device_unregister(data->hwmon_dev);
1443 for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
1444 device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1445 for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1446 device_remove_file(&pdev->dev,
1447 &abituguru_sysfs_attr[i].dev_attr);
1452 static struct abituguru_data *abituguru_update_device(struct device *dev)
1455 struct abituguru_data *data = dev_get_drvdata(dev);
1456 /* fake a complete successful read if no update necessary. */
1459 mutex_lock(&data->update_lock);
1460 if (time_after(jiffies, data->last_updated + HZ)) {
1462 err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1463 data->alarms, 3, 0);
1466 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
1467 err = abituguru_read(data, ABIT_UGURU_SENSOR_BANK1,
1468 i, &data->bank1_value[i], 1, 0);
1471 err = abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
1472 i, data->bank1_settings[i], 3, 0);
1476 for (i = 0; i < data->bank2_sensors; i++) {
1477 err = abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
1478 &data->bank2_value[i], 1, 0);
1484 data->update_timeouts = 0;
1486 /* handle timeout condition */
1487 if (!success && (err == -EBUSY || err >= 0)) {
1488 /* No overflow please */
1489 if (data->update_timeouts < 255u)
1490 data->update_timeouts++;
1491 if (data->update_timeouts <= ABIT_UGURU_MAX_TIMEOUTS) {
1492 ABIT_UGURU_DEBUG(3, "timeout exceeded, will "
1493 "try again next update\n");
1494 /* Just a timeout, fake a successful read */
1497 ABIT_UGURU_DEBUG(1, "timeout exceeded %d "
1498 "times waiting for more input state\n",
1499 (int)data->update_timeouts);
1501 /* On success set last_updated */
1503 data->last_updated = jiffies;
1505 mutex_unlock(&data->update_lock);
1513 #ifdef CONFIG_PM_SLEEP
1514 static int abituguru_suspend(struct device *dev)
1516 struct abituguru_data *data = dev_get_drvdata(dev);
1518 * make sure all communications with the uguru are done and no new
1521 mutex_lock(&data->update_lock);
1525 static int abituguru_resume(struct device *dev)
1527 struct abituguru_data *data = dev_get_drvdata(dev);
1528 /* See if the uGuru is still ready */
1529 if (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT)
1530 data->uguru_ready = 0;
1531 mutex_unlock(&data->update_lock);
1535 static SIMPLE_DEV_PM_OPS(abituguru_pm, abituguru_suspend, abituguru_resume);
1536 #define ABIT_UGURU_PM &abituguru_pm
1538 #define ABIT_UGURU_PM NULL
1539 #endif /* CONFIG_PM */
1541 static struct platform_driver abituguru_driver = {
1543 .owner = THIS_MODULE,
1544 .name = ABIT_UGURU_NAME,
1545 .pm = ABIT_UGURU_PM,
1547 .probe = abituguru_probe,
1548 .remove = abituguru_remove,
1551 static int __init abituguru_detect(void)
1554 * See if there is an uguru there. After a reboot uGuru will hold 0x00
1555 * at DATA and 0xAC, when this driver has already been loaded once
1556 * DATA will hold 0x08. For most uGuru's CMD will hold 0xAC in either
1557 * scenario but some will hold 0x00.
1558 * Some uGuru's initially hold 0x09 at DATA and will only hold 0x08
1559 * after reading CMD first, so CMD must be read first!
1561 u8 cmd_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_CMD);
1562 u8 data_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_DATA);
1563 if (((data_val == 0x00) || (data_val == 0x08)) &&
1564 ((cmd_val == 0x00) || (cmd_val == 0xAC)))
1565 return ABIT_UGURU_BASE;
1567 ABIT_UGURU_DEBUG(2, "no Abit uGuru found, data = 0x%02X, cmd = "
1568 "0x%02X\n", (unsigned int)data_val, (unsigned int)cmd_val);
1571 pr_info("Assuming Abit uGuru is present because of \"force\" parameter\n");
1572 return ABIT_UGURU_BASE;
1575 /* No uGuru found */
1579 static struct platform_device *abituguru_pdev;
1581 static int __init abituguru_init(void)
1584 struct resource res = { .flags = IORESOURCE_IO };
1585 const char *board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1587 /* safety check, refuse to load on non Abit motherboards */
1588 if (!force && (!board_vendor ||
1589 strcmp(board_vendor, "http://www.abit.com.tw/")))
1592 address = abituguru_detect();
1596 err = platform_driver_register(&abituguru_driver);
1600 abituguru_pdev = platform_device_alloc(ABIT_UGURU_NAME, address);
1601 if (!abituguru_pdev) {
1602 pr_err("Device allocation failed\n");
1604 goto exit_driver_unregister;
1607 res.start = address;
1608 res.end = address + ABIT_UGURU_REGION_LENGTH - 1;
1609 res.name = ABIT_UGURU_NAME;
1611 err = platform_device_add_resources(abituguru_pdev, &res, 1);
1613 pr_err("Device resource addition failed (%d)\n", err);
1614 goto exit_device_put;
1617 err = platform_device_add(abituguru_pdev);
1619 pr_err("Device addition failed (%d)\n", err);
1620 goto exit_device_put;
1626 platform_device_put(abituguru_pdev);
1627 exit_driver_unregister:
1628 platform_driver_unregister(&abituguru_driver);
1633 static void __exit abituguru_exit(void)
1635 platform_device_unregister(abituguru_pdev);
1636 platform_driver_unregister(&abituguru_driver);
1639 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
1640 MODULE_DESCRIPTION("Abit uGuru Sensor device");
1641 MODULE_LICENSE("GPL");
1643 module_init(abituguru_init);
1644 module_exit(abituguru_exit);