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