24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/module.h>
27 #include <linux/sched.h>
29 #include <linux/slab.h>
41 #define ABIT_UGURU_ALARM_BANK 0x20
42 #define ABIT_UGURU_SENSOR_BANK1 0x21
43 #define ABIT_UGURU_FAN_PWM 0x24
44 #define ABIT_UGURU_SENSOR_BANK2 0x26
46 #define ABIT_UGURU_MAX_BANK1_SENSORS 16
52 #define ABIT_UGURU_MAX_BANK2_SENSORS 6
54 #define ABIT_UGURU_MAX_PWMS 5
56 #define ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE 0x01
57 #define ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE 0x02
58 #define ABIT_UGURU_VOLT_LOW_ALARM_ENABLE 0x04
59 #define ABIT_UGURU_TEMP_HIGH_ALARM_FLAG 0x10
60 #define ABIT_UGURU_VOLT_HIGH_ALARM_FLAG 0x20
61 #define ABIT_UGURU_VOLT_LOW_ALARM_FLAG 0x40
63 #define ABIT_UGURU_FAN_LOW_ALARM_ENABLE 0x01
65 #define ABIT_UGURU_BEEP_ENABLE 0x08
66 #define ABIT_UGURU_SHUTDOWN_ENABLE 0x80
68 #define ABIT_UGURU_FAN_PWM_ENABLE 0x80
70 #define ABIT_UGURU_FAN_MAX 15300
72 #define ABIT_UGURU_IN_SENSOR 0
73 #define ABIT_UGURU_TEMP_SENSOR 1
74 #define ABIT_UGURU_NC 2
80 #define ABIT_UGURU_WAIT_TIMEOUT 125
86 #define ABIT_UGURU_WAIT_TIMEOUT_SLEEP 5
91 #define ABIT_UGURU_READY_TIMEOUT 5
93 #define ABIT_UGURU_MAX_RETRIES 3
94 #define ABIT_UGURU_RETRY_DELAY (HZ/5)
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)
107 #define ABITUGURU_IN_NAMES_LENGTH (11 + 2 * 9 + 2 * 15 + 2 * 22 + 10 + 14)
112 #define ABITUGURU_TEMP_NAMES_LENGTH (13 + 11 + 12 + 13 + 20 + 12 + 16)
117 #define ABITUGURU_FAN_NAMES_LENGTH (11 + 9 + 11 + 18 + 10 + 14)
122 #define ABITUGURU_PWM_NAMES_LENGTH (12 + 24 + 2 * 21 + 2 * 22)
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)
136 #define ABIT_UGURU_BASE 0x00E0
138 #define ABIT_UGURU_CMD 0x00
140 #define ABIT_UGURU_DATA 0x04
141 #define ABIT_UGURU_REGION_LENGTH 5
143 #define ABIT_UGURU_STATUS_WRITE 0x00
144 #define ABIT_UGURU_STATUS_READ 0x01
145 #define ABIT_UGURU_STATUS_INPUT 0x08
146 #define ABIT_UGURU_STATUS_READY 0x09
150 static const int abituguru_bank1_max_value[2] = { 3494, 255000 };
155 static const u8 abituguru_bank2_min_threshold = 5;
156 static const u8 abituguru_bank2_max_threshold = 50;
161 static const int abituguru_pwm_settings_multiplier[5] = { 0, 1, 1, 1000, 1000 };
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 };
176 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
183 static int fan_sensors;
197 " 1 + verbose error reporting\n"
198 " 2 + sensors type probing info\n"
199 " 3 + retryable error reporting");
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)";
297 "timeout exceeded waiting for ready state\n");
306 "CMD reg does not hold 0xAC after ready command\n");
321 "state != more input after ready command\n");
351 if (abituguru_ready(data) != 0)
363 "waiting for more input state, %d "
364 "tries remaining\n", retries);
372 "waiting for more input state "
373 "(bank: %d)\n", (
int)bank_addr);
391 i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
396 for (i = 0; i <
count; i++) {
399 "timeout exceeded waiting for "
400 "read state (bank: %d, sensor: %d)\n",
401 (
int)bank_addr, (
int)sensor_addr);
408 abituguru_ready(data);
418 u8 bank_addr,
u8 sensor_addr,
u8 *buf,
int count)
427 i = abituguru_send_address(data, bank_addr, sensor_addr,
433 for (i = 0; i <
count; i++) {
436 "write state (bank: %d, sensor: %d)\n",
437 (
int)bank_addr, (
int)sensor_addr);
450 "after write (bank: %d, sensor: %d)\n", (
int)bank_addr,
460 "write (bank: %d, sensor: %d)\n",
461 (
int)bank_addr, (
int)sensor_addr);
468 abituguru_ready(data);
492 "%d because of \"bank1_types\" module param\n",
493 bank1_types[sensor_addr], (
int)sensor_addr);
494 return bank1_types[sensor_addr];
503 if ((val < 10
u) || (val > 250
u)) {
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);
535 goto abituguru_detect_bank1_sensor_type_exit;
545 goto abituguru_detect_bank1_sensor_type_exit;
546 if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
550 goto abituguru_detect_bank1_sensor_type_exit;
551 if (buf[0] & test_flag) {
554 goto abituguru_detect_bank1_sensor_type_exit;
557 "sensor test, but volt range flag not set\n");
572 goto abituguru_detect_bank1_sensor_type_exit;
582 goto abituguru_detect_bank1_sensor_type_exit;
583 if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
587 goto abituguru_detect_bank1_sensor_type_exit;
591 goto abituguru_detect_bank1_sensor_type_exit;
594 "sensor test, but temp high flag not set\n");
600 abituguru_detect_bank1_sensor_type_exit:
606 for (i = 0; i < 3; i++)
612 pr_err(
"Fatal error could not restore original settings. %s %s\n",
613 never_happen, report_this);
646 "\"fan_sensors\" module param\n",
663 "to be a fan sensor: settings[0] = %02X\n",
670 abituguru_bank2_min_threshold) {
672 "to be a fan sensor: the threshold (%d) is "
673 "below the minimum (%d)\n", i,
675 (
int)abituguru_bank2_min_threshold);
679 abituguru_bank2_max_threshold) {
681 "to be a fan sensor: the threshold (%d) is "
682 "above the maximum (%d)\n", i,
684 (
int)abituguru_bank2_max_threshold);
702 "\"pwms\" module param\n", (
int)data->
pwms);
715 "to be a pwm channel: settings[0] = %02X\n",
732 "to be a pwm channel: %d is not a valid temp "
733 "sensor address\n", i,
739 for (j = 1; j < 5; j++) {
742 if ((i == 0) && ((j == 1) || (j == 2)))
745 min = abituguru_pwm_min[
j];
748 "not seem to be a pwm channel: "
749 "setting %d (%d) is below the minimum "
750 "value (%d)\n", i, j,
753 goto abituguru_detect_no_pwms_exit;
757 "not seem to be a pwm channel: "
758 "setting %d (%d) is above the maximum "
759 "value (%d)\n", i, j,
761 (
int)abituguru_pwm_max[j]);
762 goto abituguru_detect_no_pwms_exit;
769 "to be a pwm channel: min pwm (%d) >= "
777 "to be a pwm channel: min temp (%d) >= "
778 "max temp (%d)\n", i,
785 abituguru_detect_no_pwms_exit:
840 *devattr,
const char *buf,
size_t count)
847 ret = kstrtoul(buf, 10, &val);
873 *devattr,
const char *buf,
size_t count)
880 ret = kstrtoul(buf, 10, &val);
888 if (val < abituguru_bank2_min_threshold ||
889 val > abituguru_bank2_max_threshold)
972 ret = kstrtoul(buf, 10, &mask);
986 (abituguru_write(data,
1005 ret = kstrtoul(buf, 10, &mask);
1019 (abituguru_write(data,
1036 abituguru_pwm_settings_multiplier[attr->
nr]);
1040 *devattr,
const char *buf,
size_t count)
1048 ret = kstrtoul(buf, 10, &val);
1053 val = (val + abituguru_pwm_settings_multiplier[attr->
nr] / 2) /
1054 abituguru_pwm_settings_multiplier[attr->
nr];
1057 if ((attr->
index == 0) && ((attr->
nr == 1) || (attr->
nr == 2)))
1060 min = abituguru_pwm_min[attr->
nr];
1063 if (val < min || val > abituguru_pwm_max[attr->
nr])
1068 if ((attr->
nr & 1) &&
1071 else if (!(attr->
nr & 1) &&
1102 return sprintf(buf,
"%d\n", i+1);
1108 *devattr,
const char *buf,
size_t count)
1117 ret = kstrtoul(buf, 10, &val);
1150 return sprintf(buf,
"%d\n", res);
1154 *devattr,
const char *buf,
size_t count)
1160 unsigned long user_val;
1162 ret = kstrtoul(buf, 10, &user_val);
1203 store_bank1_setting, 1, 0),
1207 store_bank1_setting, 2, 0),
1223 store_bank1_setting, 1, 0),
1225 store_bank1_setting, 2, 0),
1239 store_bank2_setting, 1, 0),
1250 store_pwm_enable, 0, 0),
1252 store_pwm_sensor, 0, 0),
1254 store_pwm_setting, 1, 0),
1256 store_pwm_setting, 2, 0),
1258 store_pwm_setting, 3, 0),
1260 store_pwm_setting, 4, 0),
1270 int i,
j,
used, sysfs_names_free, sysfs_attr_i, res = -
ENODEV;
1271 char *sysfs_filename;
1278 0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E, 0x02,
1279 0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
1288 platform_set_drvdata(pdev, data);
1301 goto abituguru_probe_error;
1307 goto abituguru_probe_error;
1311 goto abituguru_probe_error;
1324 goto abituguru_probe_error;
1328 goto abituguru_probe_error;
1334 goto abituguru_probe_error;
1343 res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
1345 goto abituguru_probe_error;
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.
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;
1365 abituguru_bank1_max_value[
res];
1371 abituguru_detect_no_bank2_sensors(data);
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,
1378 abituguru_sysfs_fan_templ[
j];
1379 data->
sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1382 sysfs_filename +=
used;
1383 sysfs_names_free -=
used;
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,
1395 abituguru_sysfs_pwm_templ[
j];
1396 data->
sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1399 sysfs_filename +=
used;
1400 sysfs_names_free -=
used;
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);
1409 goto abituguru_probe_error;
1411 pr_info(
"found Abit uGuru\n");
1414 for (i = 0; i < sysfs_attr_i; i++)
1417 goto abituguru_probe_error;
1418 for (i = 0; i <
ARRAY_SIZE(abituguru_sysfs_attr); i++)
1420 &abituguru_sysfs_attr[i].
dev_attr))
1421 goto abituguru_probe_error;
1428 abituguru_probe_error:
1429 for (i = 0; data->
sysfs_attr[
i].dev_attr.attr.name; i++)
1431 for (i = 0; i <
ARRAY_SIZE(abituguru_sysfs_attr); i++)
1433 &abituguru_sysfs_attr[i].
dev_attr);
1443 for (i = 0; data->
sysfs_attr[
i].dev_attr.attr.name; i++)
1445 for (i = 0; i <
ARRAY_SIZE(abituguru_sysfs_attr); i++)
1447 &abituguru_sysfs_attr[i].
dev_attr);
1487 if (!success && (err == -
EBUSY || err >= 0)) {
1493 "try again next update\n");
1498 "times waiting for more input state\n",
1513 #ifdef CONFIG_PM_SLEEP
1514 static int abituguru_suspend(
struct device *dev)
1525 static int abituguru_resume(
struct device *dev)
1536 #define ABIT_UGURU_PM &abituguru_pm
1538 #define ABIT_UGURU_PM NULL
1547 .probe = abituguru_probe,
1551 static int __init abituguru_detect(
void)
1563 if (((data_val == 0x00) || (data_val == 0x08)) &&
1564 ((cmd_val == 0x00) || (cmd_val == 0xAC)))
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");
1581 static int __init abituguru_init(
void)
1588 if (!
force && (!board_vendor ||
1589 strcmp(board_vendor,
"http://www.abit.com.tw/")))
1592 address = abituguru_detect();
1601 if (!abituguru_pdev) {
1602 pr_err(
"Device allocation failed\n");
1604 goto exit_driver_unregister;
1613 pr_err(
"Device resource addition failed (%d)\n", err);
1614 goto exit_device_put;
1619 pr_err(
"Device addition failed (%d)\n", err);
1620 goto exit_device_put;
1627 exit_driver_unregister:
1633 static void __exit abituguru_exit(
void)