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