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