blob: 0770688f79b62c8b303ca08698a084cea1a98ede [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>
Al Virof6a57032006-10-18 01:47:25 -040024#include <linux/sched.h>
Hans de Goedef2b84bb2006-06-04 20:22:24 +020025#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/jiffies.h>
28#include <linux/mutex.h>
29#include <linux/err.h>
Hans de Goedefaf9b612006-08-25 10:24:20 +020030#include <linux/delay.h>
Hans de Goedef2b84bb2006-06-04 20:22:24 +020031#include <linux/platform_device.h>
32#include <linux/hwmon.h>
33#include <linux/hwmon-sysfs.h>
34#include <asm/io.h>
35
36/* Banks */
37#define ABIT_UGURU_ALARM_BANK 0x20 /* 1x 3 bytes */
38#define ABIT_UGURU_SENSOR_BANK1 0x21 /* 16x volt and temp */
39#define ABIT_UGURU_FAN_PWM 0x24 /* 3x 5 bytes */
40#define ABIT_UGURU_SENSOR_BANK2 0x26 /* fans */
Hans de Goedea2392e02006-06-04 20:23:01 +020041/* max nr of sensors in bank1, a bank1 sensor can be in, temp or nc */
42#define ABIT_UGURU_MAX_BANK1_SENSORS 16
43/* Warning if you increase one of the 2 MAX defines below to 10 or higher you
44 should adjust the belonging _NAMES_LENGTH macro for the 2 digit number! */
Hans de Goedef2b84bb2006-06-04 20:22:24 +020045/* max nr of sensors in bank2, currently mb's with max 6 fans are known */
46#define ABIT_UGURU_MAX_BANK2_SENSORS 6
47/* max nr of pwm outputs, currently mb's with max 5 pwm outputs are known */
48#define ABIT_UGURU_MAX_PWMS 5
49/* uGuru sensor bank 1 flags */ /* Alarm if: */
50#define ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE 0x01 /* temp over warn */
51#define ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE 0x02 /* volt over max */
52#define ABIT_UGURU_VOLT_LOW_ALARM_ENABLE 0x04 /* volt under min */
53#define ABIT_UGURU_TEMP_HIGH_ALARM_FLAG 0x10 /* temp is over warn */
54#define ABIT_UGURU_VOLT_HIGH_ALARM_FLAG 0x20 /* volt is over max */
55#define ABIT_UGURU_VOLT_LOW_ALARM_FLAG 0x40 /* volt is under min */
56/* uGuru sensor bank 2 flags */ /* Alarm if: */
57#define ABIT_UGURU_FAN_LOW_ALARM_ENABLE 0x01 /* fan under min */
58/* uGuru sensor bank common flags */
59#define ABIT_UGURU_BEEP_ENABLE 0x08 /* beep if alarm */
60#define ABIT_UGURU_SHUTDOWN_ENABLE 0x80 /* shutdown if alarm */
61/* uGuru fan PWM (speed control) flags */
62#define ABIT_UGURU_FAN_PWM_ENABLE 0x80 /* enable speed control */
63/* Values used for conversion */
64#define ABIT_UGURU_FAN_MAX 15300 /* RPM */
65/* Bank1 sensor types */
66#define ABIT_UGURU_IN_SENSOR 0
67#define ABIT_UGURU_TEMP_SENSOR 1
68#define ABIT_UGURU_NC 2
Hans de Goedefaf9b612006-08-25 10:24:20 +020069/* In many cases we need to wait for the uGuru to reach a certain status, most
70 of the time it will reach this status within 30 - 90 ISA reads, and thus we
71 can best busy wait. This define gives the total amount of reads to try. */
72#define ABIT_UGURU_WAIT_TIMEOUT 125
73/* However sometimes older versions of the uGuru seem to be distracted and they
74 do not respond for a long time. To handle this we sleep before each of the
75 last ABIT_UGURU_WAIT_TIMEOUT_SLEEP tries. */
76#define ABIT_UGURU_WAIT_TIMEOUT_SLEEP 5
Hans de Goedef2b84bb2006-06-04 20:22:24 +020077/* Normally all expected status in abituguru_ready, are reported after the
Hans de Goedefaf9b612006-08-25 10:24:20 +020078 first read, but sometimes not and we need to poll. */
79#define ABIT_UGURU_READY_TIMEOUT 5
Hans de Goedef2b84bb2006-06-04 20:22:24 +020080/* Maximum 3 retries on timedout reads/writes, delay 200 ms before retrying */
81#define ABIT_UGURU_MAX_RETRIES 3
82#define ABIT_UGURU_RETRY_DELAY (HZ/5)
Hans de Goedea2392e02006-06-04 20:23:01 +020083/* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is an error */
Hans de Goedef2b84bb2006-06-04 20:22:24 +020084#define ABIT_UGURU_MAX_TIMEOUTS 2
Hans de Goedea2392e02006-06-04 20:23:01 +020085/* utility macros */
86#define ABIT_UGURU_NAME "abituguru"
87#define ABIT_UGURU_DEBUG(level, format, arg...) \
88 if (level <= verbose) \
89 printk(KERN_DEBUG ABIT_UGURU_NAME ": " format , ## arg)
90/* Macros to help calculate the sysfs_names array length */
91/* sum of strlen of: in??_input\0, in??_{min,max}\0, in??_{min,max}_alarm\0,
92 in??_{min,max}_alarm_enable\0, in??_beep\0, in??_shutdown\0 */
93#define ABITUGURU_IN_NAMES_LENGTH (11 + 2 * 9 + 2 * 15 + 2 * 22 + 10 + 14)
94/* sum of strlen of: temp??_input\0, temp??_max\0, temp??_crit\0,
95 temp??_alarm\0, temp??_alarm_enable\0, temp??_beep\0, temp??_shutdown\0 */
96#define ABITUGURU_TEMP_NAMES_LENGTH (13 + 11 + 12 + 13 + 20 + 12 + 16)
97/* sum of strlen of: fan?_input\0, fan?_min\0, fan?_alarm\0,
98 fan?_alarm_enable\0, fan?_beep\0, fan?_shutdown\0 */
99#define ABITUGURU_FAN_NAMES_LENGTH (11 + 9 + 11 + 18 + 10 + 14)
100/* sum of strlen of: pwm?_enable\0, pwm?_auto_channels_temp\0,
101 pwm?_auto_point{1,2}_pwm\0, pwm?_auto_point{1,2}_temp\0 */
102#define ABITUGURU_PWM_NAMES_LENGTH (12 + 24 + 2 * 21 + 2 * 22)
103/* IN_NAMES_LENGTH > TEMP_NAMES_LENGTH so assume all bank1 sensors are in */
104#define ABITUGURU_SYSFS_NAMES_LENGTH ( \
105 ABIT_UGURU_MAX_BANK1_SENSORS * ABITUGURU_IN_NAMES_LENGTH + \
106 ABIT_UGURU_MAX_BANK2_SENSORS * ABITUGURU_FAN_NAMES_LENGTH + \
107 ABIT_UGURU_MAX_PWMS * ABITUGURU_PWM_NAMES_LENGTH)
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200108
Hans de Goedea2392e02006-06-04 20:23:01 +0200109/* All the macros below are named identical to the oguru and oguru2 programs
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200110 reverse engineered by Olle Sandberg, hence the names might not be 100%
111 logical. I could come up with better names, but I prefer keeping the names
112 identical so that this driver can be compared with his work more easily. */
113/* Two i/o-ports are used by uGuru */
114#define ABIT_UGURU_BASE 0x00E0
115/* Used to tell uGuru what to read and to read the actual data */
116#define ABIT_UGURU_CMD 0x00
117/* Mostly used to check if uGuru is busy */
118#define ABIT_UGURU_DATA 0x04
119#define ABIT_UGURU_REGION_LENGTH 5
120/* uGuru status' */
121#define ABIT_UGURU_STATUS_WRITE 0x00 /* Ready to be written */
122#define ABIT_UGURU_STATUS_READ 0x01 /* Ready to be read */
123#define ABIT_UGURU_STATUS_INPUT 0x08 /* More input */
124#define ABIT_UGURU_STATUS_READY 0x09 /* Ready to be written */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200125
126/* Constants */
127/* in (Volt) sensors go up to 3494 mV, temp to 255000 millidegrees Celsius */
128static const int abituguru_bank1_max_value[2] = { 3494, 255000 };
129/* Min / Max allowed values for sensor2 (fan) alarm threshold, these values
130 correspond to 300-3000 RPM */
131static const u8 abituguru_bank2_min_threshold = 5;
132static const u8 abituguru_bank2_max_threshold = 50;
133/* Register 0 is a bitfield, 1 and 2 are pwm settings (255 = 100%), 3 and 4
134 are temperature trip points. */
135static const int abituguru_pwm_settings_multiplier[5] = { 0, 1, 1, 1000, 1000 };
136/* Min / Max allowed values for pwm_settings. Note: pwm1 (CPU fan) is a
137 special case the minium allowed pwm% setting for this is 30% (77) on
138 some MB's this special case is handled in the code! */
139static const u8 abituguru_pwm_min[5] = { 0, 170, 170, 25, 25 };
140static const u8 abituguru_pwm_max[5] = { 0, 255, 255, 75, 75 };
141
142
143/* Insmod parameters */
144static int force;
145module_param(force, bool, 0);
146MODULE_PARM_DESC(force, "Set to one to force detection.");
Hans de Goede9b2ad122006-07-05 18:07:49 +0200147static int bank1_types[ABIT_UGURU_MAX_BANK1_SENSORS] = { -1, -1, -1, -1, -1,
148 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
149module_param_array(bank1_types, int, NULL, 0);
150MODULE_PARM_DESC(bank1_types, "Bank1 sensortype autodetection override:\n"
151 " -1 autodetect\n"
152 " 0 volt sensor\n"
153 " 1 temp sensor\n"
154 " 2 not connected");
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200155static int fan_sensors;
156module_param(fan_sensors, int, 0);
157MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru "
158 "(0 = autodetect)");
159static int pwms;
160module_param(pwms, int, 0);
161MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru "
162 "(0 = autodetect)");
163
164/* Default verbose is 2, since this driver is still in the testing phase */
165static int verbose = 2;
166module_param(verbose, int, 0644);
167MODULE_PARM_DESC(verbose, "How verbose should the driver be? (0-3):\n"
168 " 0 normal output\n"
169 " 1 + verbose error reporting\n"
170 " 2 + sensors type probing info\n"
171 " 3 + retryable error reporting");
172
173
174/* For the Abit uGuru, we need to keep some data in memory.
175 The structure is dynamically allocated, at the same time when a new
176 abituguru device is allocated. */
177struct abituguru_data {
178 struct class_device *class_dev; /* hwmon registered device */
179 struct mutex update_lock; /* protect access to data and uGuru */
180 unsigned long last_updated; /* In jiffies */
181 unsigned short addr; /* uguru base address */
182 char uguru_ready; /* is the uguru in ready state? */
183 unsigned char update_timeouts; /* number of update timeouts since last
184 successful update */
185
186 /* The sysfs attr and their names are generated automatically, for bank1
187 we cannot use a predefined array because we don't know beforehand
188 of a sensor is a volt or a temp sensor, for bank2 and the pwms its
189 easier todo things the same way. For in sensors we have 9 (temp 7)
190 sysfs entries per sensor, for bank2 and pwms 6. */
Hans de Goedea2392e02006-06-04 20:23:01 +0200191 struct sensor_device_attribute_2 sysfs_attr[
192 ABIT_UGURU_MAX_BANK1_SENSORS * 9 +
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200193 ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6];
Hans de Goedea2392e02006-06-04 20:23:01 +0200194 /* Buffer to store the dynamically generated sysfs names */
195 char sysfs_names[ABITUGURU_SYSFS_NAMES_LENGTH];
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200196
197 /* Bank 1 data */
Hans de Goedea2392e02006-06-04 20:23:01 +0200198 /* number of and addresses of [0] in, [1] temp sensors */
199 u8 bank1_sensors[2];
200 u8 bank1_address[2][ABIT_UGURU_MAX_BANK1_SENSORS];
201 u8 bank1_value[ABIT_UGURU_MAX_BANK1_SENSORS];
202 /* This array holds 3 entries per sensor for the bank 1 sensor settings
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200203 (flags, min, max for voltage / flags, warn, shutdown for temp). */
Hans de Goedea2392e02006-06-04 20:23:01 +0200204 u8 bank1_settings[ABIT_UGURU_MAX_BANK1_SENSORS][3];
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200205 /* Maximum value for each sensor used for scaling in mV/millidegrees
206 Celsius. */
Hans de Goedea2392e02006-06-04 20:23:01 +0200207 int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS];
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200208
209 /* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */
210 u8 bank2_sensors; /* actual number of bank2 sensors found */
211 u8 bank2_value[ABIT_UGURU_MAX_BANK2_SENSORS];
212 u8 bank2_settings[ABIT_UGURU_MAX_BANK2_SENSORS][2]; /* flags, min */
213
214 /* Alarms 2 bytes for bank1, 1 byte for bank2 */
215 u8 alarms[3];
216
217 /* Fan PWM (speed control) 5 bytes per PWM */
218 u8 pwms; /* actual number of pwms found */
219 u8 pwm_settings[ABIT_UGURU_MAX_PWMS][5];
220};
221
222/* wait till the uguru is in the specified state */
223static int abituguru_wait(struct abituguru_data *data, u8 state)
224{
225 int timeout = ABIT_UGURU_WAIT_TIMEOUT;
226
227 while (inb_p(data->addr + ABIT_UGURU_DATA) != state) {
228 timeout--;
229 if (timeout == 0)
230 return -EBUSY;
Hans de Goedefaf9b612006-08-25 10:24:20 +0200231 /* sleep a bit before our last few tries, see the comment on
232 this where ABIT_UGURU_WAIT_TIMEOUT_SLEEP is defined. */
233 if (timeout <= ABIT_UGURU_WAIT_TIMEOUT_SLEEP)
234 msleep(0);
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200235 }
236 return 0;
237}
238
239/* Put the uguru in ready for input state */
240static int abituguru_ready(struct abituguru_data *data)
241{
242 int timeout = ABIT_UGURU_READY_TIMEOUT;
243
244 if (data->uguru_ready)
245 return 0;
246
247 /* Reset? / Prepare for next read/write cycle */
248 outb(0x00, data->addr + ABIT_UGURU_DATA);
249
250 /* Wait till the uguru is ready */
251 if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) {
252 ABIT_UGURU_DEBUG(1,
253 "timeout exceeded waiting for ready state\n");
254 return -EIO;
255 }
256
257 /* Cmd port MUST be read now and should contain 0xAC */
258 while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
259 timeout--;
260 if (timeout == 0) {
261 ABIT_UGURU_DEBUG(1,
262 "CMD reg does not hold 0xAC after ready command\n");
263 return -EIO;
264 }
Hans de Goedefaf9b612006-08-25 10:24:20 +0200265 msleep(0);
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200266 }
267
268 /* After this the ABIT_UGURU_DATA port should contain
269 ABIT_UGURU_STATUS_INPUT */
270 timeout = ABIT_UGURU_READY_TIMEOUT;
271 while (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT) {
272 timeout--;
273 if (timeout == 0) {
274 ABIT_UGURU_DEBUG(1,
275 "state != more input after ready command\n");
276 return -EIO;
277 }
Hans de Goedefaf9b612006-08-25 10:24:20 +0200278 msleep(0);
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200279 }
280
281 data->uguru_ready = 1;
282 return 0;
283}
284
285/* Send the bank and then sensor address to the uGuru for the next read/write
286 cycle. This function gets called as the first part of a read/write by
287 abituguru_read and abituguru_write. This function should never be
288 called by any other function. */
289static int abituguru_send_address(struct abituguru_data *data,
290 u8 bank_addr, u8 sensor_addr, int retries)
291{
292 /* assume the caller does error handling itself if it has not requested
293 any retries, and thus be quiet. */
294 int report_errors = retries;
295
296 for (;;) {
297 /* Make sure the uguru is ready and then send the bank address,
298 after this the uguru is no longer "ready". */
299 if (abituguru_ready(data) != 0)
300 return -EIO;
301 outb(bank_addr, data->addr + ABIT_UGURU_DATA);
302 data->uguru_ready = 0;
303
304 /* Wait till the uguru is ABIT_UGURU_STATUS_INPUT state again
305 and send the sensor addr */
306 if (abituguru_wait(data, ABIT_UGURU_STATUS_INPUT)) {
307 if (retries) {
308 ABIT_UGURU_DEBUG(3, "timeout exceeded "
309 "waiting for more input state, %d "
310 "tries remaining\n", retries);
311 set_current_state(TASK_UNINTERRUPTIBLE);
312 schedule_timeout(ABIT_UGURU_RETRY_DELAY);
313 retries--;
314 continue;
315 }
316 if (report_errors)
317 ABIT_UGURU_DEBUG(1, "timeout exceeded "
318 "waiting for more input state "
319 "(bank: %d)\n", (int)bank_addr);
320 return -EBUSY;
321 }
322 outb(sensor_addr, data->addr + ABIT_UGURU_CMD);
323 return 0;
324 }
325}
326
327/* Read count bytes from sensor sensor_addr in bank bank_addr and store the
328 result in buf, retry the send address part of the read retries times. */
329static int abituguru_read(struct abituguru_data *data,
330 u8 bank_addr, u8 sensor_addr, u8 *buf, int count, int retries)
331{
332 int i;
333
334 /* Send the address */
335 i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
336 if (i)
337 return i;
338
339 /* And read the data */
340 for (i = 0; i < count; i++) {
341 if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
Hans de Goedefaf9b612006-08-25 10:24:20 +0200342 ABIT_UGURU_DEBUG(retries ? 1 : 3,
343 "timeout exceeded waiting for "
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200344 "read state (bank: %d, sensor: %d)\n",
345 (int)bank_addr, (int)sensor_addr);
346 break;
347 }
348 buf[i] = inb(data->addr + ABIT_UGURU_CMD);
349 }
350
351 /* Last put the chip back in ready state */
352 abituguru_ready(data);
353
354 return i;
355}
356
357/* Write count bytes from buf to sensor sensor_addr in bank bank_addr, the send
358 address part of the write is always retried ABIT_UGURU_MAX_RETRIES times. */
359static int abituguru_write(struct abituguru_data *data,
360 u8 bank_addr, u8 sensor_addr, u8 *buf, int count)
361{
Hans de Goedefaf9b612006-08-25 10:24:20 +0200362 /* We use the ready timeout as we have to wait for 0xAC just like the
363 ready function */
364 int i, timeout = ABIT_UGURU_READY_TIMEOUT;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200365
366 /* Send the address */
367 i = abituguru_send_address(data, bank_addr, sensor_addr,
368 ABIT_UGURU_MAX_RETRIES);
369 if (i)
370 return i;
371
372 /* And write the data */
373 for (i = 0; i < count; i++) {
374 if (abituguru_wait(data, ABIT_UGURU_STATUS_WRITE)) {
375 ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
376 "write state (bank: %d, sensor: %d)\n",
377 (int)bank_addr, (int)sensor_addr);
378 break;
379 }
380 outb(buf[i], data->addr + ABIT_UGURU_CMD);
381 }
382
383 /* Now we need to wait till the chip is ready to be read again,
Hans de Goedefaf9b612006-08-25 10:24:20 +0200384 so that we can read 0xAC as confirmation that our write has
385 succeeded. */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200386 if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
387 ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for read state "
388 "after write (bank: %d, sensor: %d)\n", (int)bank_addr,
389 (int)sensor_addr);
390 return -EIO;
391 }
392
393 /* Cmd port MUST be read now and should contain 0xAC */
Hans de Goedefaf9b612006-08-25 10:24:20 +0200394 while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
395 timeout--;
396 if (timeout == 0) {
397 ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after "
398 "write (bank: %d, sensor: %d)\n",
399 (int)bank_addr, (int)sensor_addr);
400 return -EIO;
401 }
402 msleep(0);
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200403 }
404
405 /* Last put the chip back in ready state */
406 abituguru_ready(data);
407
408 return i;
409}
410
411/* Detect sensor type. Temp and Volt sensors are enabled with
412 different masks and will ignore enable masks not meant for them.
413 This enables us to test what kind of sensor we're dealing with.
414 By setting the alarm thresholds so that we will always get an
415 alarm for sensor type X and then enabling the sensor as sensor type
416 X, if we then get an alarm it is a sensor of type X. */
417static int __devinit
418abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
419 u8 sensor_addr)
420{
Hans de Goedee432dc82007-06-18 22:59:34 +0200421 u8 val, test_flag, buf[3];
Hans de Goedefaf9b612006-08-25 10:24:20 +0200422 int i, ret = -ENODEV; /* error is the most common used retval :| */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200423
Hans de Goede9b2ad122006-07-05 18:07:49 +0200424 /* If overriden by the user return the user selected type */
425 if (bank1_types[sensor_addr] >= ABIT_UGURU_IN_SENSOR &&
426 bank1_types[sensor_addr] <= ABIT_UGURU_NC) {
427 ABIT_UGURU_DEBUG(2, "assuming sensor type %d for bank1 sensor "
428 "%d because of \"bank1_types\" module param\n",
429 bank1_types[sensor_addr], (int)sensor_addr);
430 return bank1_types[sensor_addr];
431 }
432
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200433 /* First read the sensor and the current settings */
434 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, sensor_addr, &val,
435 1, ABIT_UGURU_MAX_RETRIES) != 1)
Hans de Goedea2392e02006-06-04 20:23:01 +0200436 return -ENODEV;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200437
438 /* Test val is sane / usable for sensor type detection. */
Hans de Goedee432dc82007-06-18 22:59:34 +0200439 if ((val < 10u) || (val > 250u)) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200440 printk(KERN_WARNING ABIT_UGURU_NAME
441 ": bank1-sensor: %d reading (%d) too close to limits, "
442 "unable to determine sensor type, skipping sensor\n",
443 (int)sensor_addr, (int)val);
444 /* assume no sensor is there for sensors for which we can't
445 determine the sensor type because their reading is too close
446 to their limits, this usually means no sensor is there. */
447 return ABIT_UGURU_NC;
448 }
449
450 ABIT_UGURU_DEBUG(2, "testing bank1 sensor %d\n", (int)sensor_addr);
451 /* Volt sensor test, enable volt low alarm, set min value ridicously
Hans de Goedee432dc82007-06-18 22:59:34 +0200452 high, or vica versa if the reading is very high. If its a volt
453 sensor this should always give us an alarm. */
454 if (val <= 240u) {
455 buf[0] = ABIT_UGURU_VOLT_LOW_ALARM_ENABLE;
456 buf[1] = 245;
457 buf[2] = 250;
458 test_flag = ABIT_UGURU_VOLT_LOW_ALARM_FLAG;
459 } else {
460 buf[0] = ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE;
461 buf[1] = 5;
462 buf[2] = 10;
463 test_flag = ABIT_UGURU_VOLT_HIGH_ALARM_FLAG;
464 }
465
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200466 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
467 buf, 3) != 3)
Hans de Goedefaf9b612006-08-25 10:24:20 +0200468 goto abituguru_detect_bank1_sensor_type_exit;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200469 /* Now we need 20 ms to give the uguru time to read the sensors
470 and raise a voltage alarm */
471 set_current_state(TASK_UNINTERRUPTIBLE);
472 schedule_timeout(HZ/50);
473 /* Check for alarm and check the alarm is a volt low alarm. */
474 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
475 ABIT_UGURU_MAX_RETRIES) != 3)
Hans de Goedefaf9b612006-08-25 10:24:20 +0200476 goto abituguru_detect_bank1_sensor_type_exit;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200477 if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
478 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
479 sensor_addr, buf, 3,
480 ABIT_UGURU_MAX_RETRIES) != 3)
Hans de Goedefaf9b612006-08-25 10:24:20 +0200481 goto abituguru_detect_bank1_sensor_type_exit;
Hans de Goedee432dc82007-06-18 22:59:34 +0200482 if (buf[0] & test_flag) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200483 ABIT_UGURU_DEBUG(2, " found volt sensor\n");
Hans de Goedefaf9b612006-08-25 10:24:20 +0200484 ret = ABIT_UGURU_IN_SENSOR;
485 goto abituguru_detect_bank1_sensor_type_exit;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200486 } else
487 ABIT_UGURU_DEBUG(2, " alarm raised during volt "
Hans de Goedee432dc82007-06-18 22:59:34 +0200488 "sensor test, but volt range flag not set\n");
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200489 } else
490 ABIT_UGURU_DEBUG(2, " alarm not raised during volt sensor "
491 "test\n");
492
493 /* Temp sensor test, enable sensor as a temp sensor, set beep value
494 ridicously low (but not too low, otherwise uguru ignores it).
495 If its a temp sensor this should always give us an alarm. */
496 buf[0] = ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE;
497 buf[1] = 5;
498 buf[2] = 10;
499 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
500 buf, 3) != 3)
Hans de Goedefaf9b612006-08-25 10:24:20 +0200501 goto abituguru_detect_bank1_sensor_type_exit;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200502 /* Now we need 50 ms to give the uguru time to read the sensors
503 and raise a temp alarm */
504 set_current_state(TASK_UNINTERRUPTIBLE);
505 schedule_timeout(HZ/20);
506 /* Check for alarm and check the alarm is a temp high alarm. */
507 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
508 ABIT_UGURU_MAX_RETRIES) != 3)
Hans de Goedefaf9b612006-08-25 10:24:20 +0200509 goto abituguru_detect_bank1_sensor_type_exit;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200510 if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
511 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
512 sensor_addr, buf, 3,
513 ABIT_UGURU_MAX_RETRIES) != 3)
Hans de Goedefaf9b612006-08-25 10:24:20 +0200514 goto abituguru_detect_bank1_sensor_type_exit;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200515 if (buf[0] & ABIT_UGURU_TEMP_HIGH_ALARM_FLAG) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200516 ABIT_UGURU_DEBUG(2, " found temp sensor\n");
Hans de Goedefaf9b612006-08-25 10:24:20 +0200517 ret = ABIT_UGURU_TEMP_SENSOR;
518 goto abituguru_detect_bank1_sensor_type_exit;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200519 } else
520 ABIT_UGURU_DEBUG(2, " alarm raised during temp "
521 "sensor test, but temp high flag not set\n");
522 } else
523 ABIT_UGURU_DEBUG(2, " alarm not raised during temp sensor "
524 "test\n");
525
Hans de Goedefaf9b612006-08-25 10:24:20 +0200526 ret = ABIT_UGURU_NC;
527abituguru_detect_bank1_sensor_type_exit:
528 /* Restore original settings, failing here is really BAD, it has been
529 reported that some BIOS-es hang when entering the uGuru menu with
530 invalid settings present in the uGuru, so we try this 3 times. */
531 for (i = 0; i < 3; i++)
532 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
533 sensor_addr, data->bank1_settings[sensor_addr],
534 3) == 3)
535 break;
536 if (i == 3) {
537 printk(KERN_ERR ABIT_UGURU_NAME
538 ": Fatal error could not restore original settings. "
539 "This should never happen please report this to the "
540 "abituguru maintainer (see MAINTAINERS)\n");
Hans de Goedea2392e02006-06-04 20:23:01 +0200541 return -ENODEV;
Hans de Goedefaf9b612006-08-25 10:24:20 +0200542 }
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200543 return ret;
544}
545
546/* These functions try to find out how many sensors there are in bank2 and how
547 many pwms there are. The purpose of this is to make sure that we don't give
548 the user the possibility to change settings for non-existent sensors / pwm.
549 The uGuru will happily read / write whatever memory happens to be after the
550 memory storing the PWM settings when reading/writing to a PWM which is not
551 there. Notice even if we detect a PWM which doesn't exist we normally won't
552 write to it, unless the user tries to change the settings.
553
554 Although the uGuru allows reading (settings) from non existing bank2
555 sensors, my version of the uGuru does seem to stop writing to them, the
556 write function above aborts in this case with:
557 "CMD reg does not hold 0xAC after write"
558
559 Notice these 2 tests are non destructive iow read-only tests, otherwise
560 they would defeat their purpose. Although for the bank2_sensors detection a
561 read/write test would be feasible because of the reaction above, I've
562 however opted to stay on the safe side. */
563static void __devinit
564abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
565{
566 int i;
567
Hans de Goede9b2ad122006-07-05 18:07:49 +0200568 if (fan_sensors > 0 && fan_sensors <= ABIT_UGURU_MAX_BANK2_SENSORS) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200569 data->bank2_sensors = fan_sensors;
570 ABIT_UGURU_DEBUG(2, "assuming %d fan sensors because of "
571 "\"fan_sensors\" module param\n",
572 (int)data->bank2_sensors);
573 return;
574 }
575
576 ABIT_UGURU_DEBUG(2, "detecting number of fan sensors\n");
577 for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
578 /* 0x89 are the known used bits:
579 -0x80 enable shutdown
580 -0x08 enable beep
581 -0x01 enable alarm
582 All other bits should be 0, but on some motherboards
Hans de Goedeb7c06602006-06-04 20:24:11 +0200583 0x40 (bit 6) is also high for some of the fans?? */
584 if (data->bank2_settings[i][0] & ~0xC9) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200585 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
586 "to be a fan sensor: settings[0] = %02X\n",
587 i, (unsigned int)data->bank2_settings[i][0]);
588 break;
589 }
590
591 /* check if the threshold is within the allowed range */
592 if (data->bank2_settings[i][1] <
593 abituguru_bank2_min_threshold) {
594 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
595 "to be a fan sensor: the threshold (%d) is "
596 "below the minimum (%d)\n", i,
597 (int)data->bank2_settings[i][1],
598 (int)abituguru_bank2_min_threshold);
599 break;
600 }
601 if (data->bank2_settings[i][1] >
602 abituguru_bank2_max_threshold) {
603 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
604 "to be a fan sensor: the threshold (%d) is "
605 "above the maximum (%d)\n", i,
606 (int)data->bank2_settings[i][1],
607 (int)abituguru_bank2_max_threshold);
608 break;
609 }
610 }
611
612 data->bank2_sensors = i;
613 ABIT_UGURU_DEBUG(2, " found: %d fan sensors\n",
614 (int)data->bank2_sensors);
615}
616
617static void __devinit
618abituguru_detect_no_pwms(struct abituguru_data *data)
619{
620 int i, j;
621
Hans de Goede9b2ad122006-07-05 18:07:49 +0200622 if (pwms > 0 && pwms <= ABIT_UGURU_MAX_PWMS) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200623 data->pwms = pwms;
624 ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
625 "\"pwms\" module param\n", (int)data->pwms);
626 return;
627 }
628
629 ABIT_UGURU_DEBUG(2, "detecting number of PWM outputs\n");
630 for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
631 /* 0x80 is the enable bit and the low
632 nibble is which temp sensor to use,
633 the other bits should be 0 */
634 if (data->pwm_settings[i][0] & ~0x8F) {
635 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
636 "to be a pwm channel: settings[0] = %02X\n",
637 i, (unsigned int)data->pwm_settings[i][0]);
638 break;
639 }
640
641 /* the low nibble must correspond to one of the temp sensors
642 we've found */
643 for (j = 0; j < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR];
644 j++) {
645 if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][j] ==
646 (data->pwm_settings[i][0] & 0x0F))
647 break;
648 }
649 if (j == data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
650 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
651 "to be a pwm channel: %d is not a valid temp "
652 "sensor address\n", i,
653 data->pwm_settings[i][0] & 0x0F);
654 break;
655 }
656
657 /* check if all other settings are within the allowed range */
658 for (j = 1; j < 5; j++) {
659 u8 min;
660 /* special case pwm1 min pwm% */
661 if ((i == 0) && ((j == 1) || (j == 2)))
662 min = 77;
663 else
664 min = abituguru_pwm_min[j];
665 if (data->pwm_settings[i][j] < min) {
666 ABIT_UGURU_DEBUG(2, " pwm channel %d does "
667 "not seem to be a pwm channel: "
668 "setting %d (%d) is below the minimum "
669 "value (%d)\n", i, j,
670 (int)data->pwm_settings[i][j],
671 (int)min);
672 goto abituguru_detect_no_pwms_exit;
673 }
674 if (data->pwm_settings[i][j] > abituguru_pwm_max[j]) {
675 ABIT_UGURU_DEBUG(2, " pwm channel %d does "
676 "not seem to be a pwm channel: "
677 "setting %d (%d) is above the maximum "
678 "value (%d)\n", i, j,
679 (int)data->pwm_settings[i][j],
680 (int)abituguru_pwm_max[j]);
681 goto abituguru_detect_no_pwms_exit;
682 }
683 }
684
685 /* check that min temp < max temp and min pwm < max pwm */
686 if (data->pwm_settings[i][1] >= data->pwm_settings[i][2]) {
687 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
688 "to be a pwm channel: min pwm (%d) >= "
689 "max pwm (%d)\n", i,
690 (int)data->pwm_settings[i][1],
691 (int)data->pwm_settings[i][2]);
692 break;
693 }
694 if (data->pwm_settings[i][3] >= data->pwm_settings[i][4]) {
695 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
696 "to be a pwm channel: min temp (%d) >= "
697 "max temp (%d)\n", i,
698 (int)data->pwm_settings[i][3],
699 (int)data->pwm_settings[i][4]);
700 break;
701 }
702 }
703
704abituguru_detect_no_pwms_exit:
705 data->pwms = i;
706 ABIT_UGURU_DEBUG(2, " found: %d PWM outputs\n", (int)data->pwms);
707}
708
709/* Following are the sysfs callback functions. These functions expect:
710 sensor_device_attribute_2->index: sensor address/offset in the bank
711 sensor_device_attribute_2->nr: register offset, bitmask or NA. */
712static struct abituguru_data *abituguru_update_device(struct device *dev);
713
714static ssize_t show_bank1_value(struct device *dev,
715 struct device_attribute *devattr, char *buf)
716{
717 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
718 struct abituguru_data *data = abituguru_update_device(dev);
719 if (!data)
720 return -EIO;
721 return sprintf(buf, "%d\n", (data->bank1_value[attr->index] *
722 data->bank1_max_value[attr->index] + 128) / 255);
723}
724
725static ssize_t show_bank1_setting(struct device *dev,
726 struct device_attribute *devattr, char *buf)
727{
728 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
729 struct abituguru_data *data = dev_get_drvdata(dev);
730 return sprintf(buf, "%d\n",
731 (data->bank1_settings[attr->index][attr->nr] *
732 data->bank1_max_value[attr->index] + 128) / 255);
733}
734
735static ssize_t show_bank2_value(struct device *dev,
736 struct device_attribute *devattr, char *buf)
737{
738 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
739 struct abituguru_data *data = abituguru_update_device(dev);
740 if (!data)
741 return -EIO;
742 return sprintf(buf, "%d\n", (data->bank2_value[attr->index] *
743 ABIT_UGURU_FAN_MAX + 128) / 255);
744}
745
746static ssize_t show_bank2_setting(struct device *dev,
747 struct device_attribute *devattr, char *buf)
748{
749 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
750 struct abituguru_data *data = dev_get_drvdata(dev);
751 return sprintf(buf, "%d\n",
752 (data->bank2_settings[attr->index][attr->nr] *
753 ABIT_UGURU_FAN_MAX + 128) / 255);
754}
755
756static ssize_t store_bank1_setting(struct device *dev, struct device_attribute
757 *devattr, const char *buf, size_t count)
758{
759 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
760 struct abituguru_data *data = dev_get_drvdata(dev);
761 u8 val = (simple_strtoul(buf, NULL, 10) * 255 +
762 data->bank1_max_value[attr->index]/2) /
763 data->bank1_max_value[attr->index];
764 ssize_t ret = count;
765
766 mutex_lock(&data->update_lock);
767 if (data->bank1_settings[attr->index][attr->nr] != val) {
768 u8 orig_val = data->bank1_settings[attr->index][attr->nr];
769 data->bank1_settings[attr->index][attr->nr] = val;
770 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
771 attr->index, data->bank1_settings[attr->index],
772 3) <= attr->nr) {
773 data->bank1_settings[attr->index][attr->nr] = orig_val;
774 ret = -EIO;
775 }
776 }
777 mutex_unlock(&data->update_lock);
778 return ret;
779}
780
781static ssize_t store_bank2_setting(struct device *dev, struct device_attribute
782 *devattr, const char *buf, size_t count)
783{
784 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
785 struct abituguru_data *data = dev_get_drvdata(dev);
786 u8 val = (simple_strtoul(buf, NULL, 10)*255 + ABIT_UGURU_FAN_MAX/2) /
787 ABIT_UGURU_FAN_MAX;
788 ssize_t ret = count;
789
790 /* this check can be done before taking the lock */
791 if ((val < abituguru_bank2_min_threshold) ||
792 (val > abituguru_bank2_max_threshold))
793 return -EINVAL;
794
795 mutex_lock(&data->update_lock);
796 if (data->bank2_settings[attr->index][attr->nr] != val) {
797 u8 orig_val = data->bank2_settings[attr->index][attr->nr];
798 data->bank2_settings[attr->index][attr->nr] = val;
799 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK2 + 2,
800 attr->index, data->bank2_settings[attr->index],
801 2) <= attr->nr) {
802 data->bank2_settings[attr->index][attr->nr] = orig_val;
803 ret = -EIO;
804 }
805 }
806 mutex_unlock(&data->update_lock);
807 return ret;
808}
809
810static ssize_t show_bank1_alarm(struct device *dev,
811 struct device_attribute *devattr, char *buf)
812{
813 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
814 struct abituguru_data *data = abituguru_update_device(dev);
815 if (!data)
816 return -EIO;
817 /* See if the alarm bit for this sensor is set, and if the
818 alarm matches the type of alarm we're looking for (for volt
819 it can be either low or high). The type is stored in a few
820 readonly bits in the settings part of the relevant sensor.
821 The bitmask of the type is passed to us in attr->nr. */
822 if ((data->alarms[attr->index / 8] & (0x01 << (attr->index % 8))) &&
823 (data->bank1_settings[attr->index][0] & attr->nr))
824 return sprintf(buf, "1\n");
825 else
826 return sprintf(buf, "0\n");
827}
828
829static ssize_t show_bank2_alarm(struct device *dev,
830 struct device_attribute *devattr, char *buf)
831{
832 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
833 struct abituguru_data *data = abituguru_update_device(dev);
834 if (!data)
835 return -EIO;
836 if (data->alarms[2] & (0x01 << attr->index))
837 return sprintf(buf, "1\n");
838 else
839 return sprintf(buf, "0\n");
840}
841
842static ssize_t show_bank1_mask(struct device *dev,
843 struct device_attribute *devattr, char *buf)
844{
845 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
846 struct abituguru_data *data = dev_get_drvdata(dev);
847 if (data->bank1_settings[attr->index][0] & attr->nr)
848 return sprintf(buf, "1\n");
849 else
850 return sprintf(buf, "0\n");
851}
852
853static ssize_t show_bank2_mask(struct device *dev,
854 struct device_attribute *devattr, char *buf)
855{
856 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
857 struct abituguru_data *data = dev_get_drvdata(dev);
858 if (data->bank2_settings[attr->index][0] & attr->nr)
859 return sprintf(buf, "1\n");
860 else
861 return sprintf(buf, "0\n");
862}
863
864static ssize_t store_bank1_mask(struct device *dev,
865 struct device_attribute *devattr, const char *buf, size_t count)
866{
867 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
868 struct abituguru_data *data = dev_get_drvdata(dev);
869 int mask = simple_strtoul(buf, NULL, 10);
870 ssize_t ret = count;
871 u8 orig_val;
872
873 mutex_lock(&data->update_lock);
874 orig_val = data->bank1_settings[attr->index][0];
875
876 if (mask)
877 data->bank1_settings[attr->index][0] |= attr->nr;
878 else
879 data->bank1_settings[attr->index][0] &= ~attr->nr;
880
881 if ((data->bank1_settings[attr->index][0] != orig_val) &&
882 (abituguru_write(data,
883 ABIT_UGURU_SENSOR_BANK1 + 2, attr->index,
884 data->bank1_settings[attr->index], 3) < 1)) {
885 data->bank1_settings[attr->index][0] = orig_val;
886 ret = -EIO;
887 }
888 mutex_unlock(&data->update_lock);
889 return ret;
890}
891
892static ssize_t store_bank2_mask(struct device *dev,
893 struct device_attribute *devattr, const char *buf, size_t count)
894{
895 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
896 struct abituguru_data *data = dev_get_drvdata(dev);
897 int mask = simple_strtoul(buf, NULL, 10);
898 ssize_t ret = count;
899 u8 orig_val;
900
901 mutex_lock(&data->update_lock);
902 orig_val = data->bank2_settings[attr->index][0];
903
904 if (mask)
905 data->bank2_settings[attr->index][0] |= attr->nr;
906 else
907 data->bank2_settings[attr->index][0] &= ~attr->nr;
908
909 if ((data->bank2_settings[attr->index][0] != orig_val) &&
910 (abituguru_write(data,
911 ABIT_UGURU_SENSOR_BANK2 + 2, attr->index,
912 data->bank2_settings[attr->index], 2) < 1)) {
913 data->bank2_settings[attr->index][0] = orig_val;
914 ret = -EIO;
915 }
916 mutex_unlock(&data->update_lock);
917 return ret;
918}
919
920/* Fan PWM (speed control) */
921static ssize_t show_pwm_setting(struct device *dev,
922 struct device_attribute *devattr, char *buf)
923{
924 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
925 struct abituguru_data *data = dev_get_drvdata(dev);
926 return sprintf(buf, "%d\n", data->pwm_settings[attr->index][attr->nr] *
927 abituguru_pwm_settings_multiplier[attr->nr]);
928}
929
930static ssize_t store_pwm_setting(struct device *dev, struct device_attribute
931 *devattr, const char *buf, size_t count)
932{
933 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
934 struct abituguru_data *data = dev_get_drvdata(dev);
935 u8 min, val = (simple_strtoul(buf, NULL, 10) +
936 abituguru_pwm_settings_multiplier[attr->nr]/2) /
937 abituguru_pwm_settings_multiplier[attr->nr];
938 ssize_t ret = count;
939
940 /* special case pwm1 min pwm% */
941 if ((attr->index == 0) && ((attr->nr == 1) || (attr->nr == 2)))
942 min = 77;
943 else
944 min = abituguru_pwm_min[attr->nr];
945
946 /* this check can be done before taking the lock */
947 if ((val < min) || (val > abituguru_pwm_max[attr->nr]))
948 return -EINVAL;
949
950 mutex_lock(&data->update_lock);
951 /* this check needs to be done after taking the lock */
952 if ((attr->nr & 1) &&
953 (val >= data->pwm_settings[attr->index][attr->nr + 1]))
954 ret = -EINVAL;
955 else if (!(attr->nr & 1) &&
956 (val <= data->pwm_settings[attr->index][attr->nr - 1]))
957 ret = -EINVAL;
958 else if (data->pwm_settings[attr->index][attr->nr] != val) {
959 u8 orig_val = data->pwm_settings[attr->index][attr->nr];
960 data->pwm_settings[attr->index][attr->nr] = val;
961 if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
962 attr->index, data->pwm_settings[attr->index],
963 5) <= attr->nr) {
964 data->pwm_settings[attr->index][attr->nr] =
965 orig_val;
966 ret = -EIO;
967 }
968 }
969 mutex_unlock(&data->update_lock);
970 return ret;
971}
972
973static ssize_t show_pwm_sensor(struct device *dev,
974 struct device_attribute *devattr, char *buf)
975{
976 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
977 struct abituguru_data *data = dev_get_drvdata(dev);
978 int i;
979 /* We need to walk to the temp sensor addresses to find what
980 the userspace id of the configured temp sensor is. */
981 for (i = 0; i < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]; i++)
982 if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][i] ==
983 (data->pwm_settings[attr->index][0] & 0x0F))
984 return sprintf(buf, "%d\n", i+1);
985
986 return -ENXIO;
987}
988
989static ssize_t store_pwm_sensor(struct device *dev, struct device_attribute
990 *devattr, const char *buf, size_t count)
991{
992 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
993 struct abituguru_data *data = dev_get_drvdata(dev);
994 unsigned long val = simple_strtoul(buf, NULL, 10) - 1;
995 ssize_t ret = count;
996
997 mutex_lock(&data->update_lock);
998 if (val < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
999 u8 orig_val = data->pwm_settings[attr->index][0];
1000 u8 address = data->bank1_address[ABIT_UGURU_TEMP_SENSOR][val];
1001 data->pwm_settings[attr->index][0] &= 0xF0;
1002 data->pwm_settings[attr->index][0] |= address;
1003 if (data->pwm_settings[attr->index][0] != orig_val) {
1004 if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1005 attr->index,
1006 data->pwm_settings[attr->index],
1007 5) < 1) {
1008 data->pwm_settings[attr->index][0] = orig_val;
1009 ret = -EIO;
1010 }
1011 }
1012 }
1013 else
1014 ret = -EINVAL;
1015 mutex_unlock(&data->update_lock);
1016 return ret;
1017}
1018
1019static ssize_t show_pwm_enable(struct device *dev,
1020 struct device_attribute *devattr, char *buf)
1021{
1022 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1023 struct abituguru_data *data = dev_get_drvdata(dev);
1024 int res = 0;
1025 if (data->pwm_settings[attr->index][0] & ABIT_UGURU_FAN_PWM_ENABLE)
1026 res = 2;
1027 return sprintf(buf, "%d\n", res);
1028}
1029
1030static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1031 *devattr, const char *buf, size_t count)
1032{
1033 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1034 struct abituguru_data *data = dev_get_drvdata(dev);
1035 u8 orig_val, user_val = simple_strtoul(buf, NULL, 10);
1036 ssize_t ret = count;
1037
1038 mutex_lock(&data->update_lock);
1039 orig_val = data->pwm_settings[attr->index][0];
1040 switch (user_val) {
1041 case 0:
1042 data->pwm_settings[attr->index][0] &=
1043 ~ABIT_UGURU_FAN_PWM_ENABLE;
1044 break;
1045 case 2:
1046 data->pwm_settings[attr->index][0] |=
1047 ABIT_UGURU_FAN_PWM_ENABLE;
1048 break;
1049 default:
1050 ret = -EINVAL;
1051 }
1052 if ((data->pwm_settings[attr->index][0] != orig_val) &&
1053 (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1054 attr->index, data->pwm_settings[attr->index],
1055 5) < 1)) {
1056 data->pwm_settings[attr->index][0] = orig_val;
1057 ret = -EIO;
1058 }
1059 mutex_unlock(&data->update_lock);
1060 return ret;
1061}
1062
1063static ssize_t show_name(struct device *dev,
1064 struct device_attribute *devattr, char *buf)
1065{
1066 return sprintf(buf, "%s\n", ABIT_UGURU_NAME);
1067}
1068
1069/* Sysfs attr templates, the real entries are generated automatically. */
1070static const
1071struct sensor_device_attribute_2 abituguru_sysfs_bank1_templ[2][9] = {
1072 {
1073 SENSOR_ATTR_2(in%d_input, 0444, show_bank1_value, NULL, 0, 0),
1074 SENSOR_ATTR_2(in%d_min, 0644, show_bank1_setting,
1075 store_bank1_setting, 1, 0),
1076 SENSOR_ATTR_2(in%d_min_alarm, 0444, show_bank1_alarm, NULL,
1077 ABIT_UGURU_VOLT_LOW_ALARM_FLAG, 0),
1078 SENSOR_ATTR_2(in%d_max, 0644, show_bank1_setting,
1079 store_bank1_setting, 2, 0),
1080 SENSOR_ATTR_2(in%d_max_alarm, 0444, show_bank1_alarm, NULL,
1081 ABIT_UGURU_VOLT_HIGH_ALARM_FLAG, 0),
1082 SENSOR_ATTR_2(in%d_beep, 0644, show_bank1_mask,
1083 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1084 SENSOR_ATTR_2(in%d_shutdown, 0644, show_bank1_mask,
1085 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1086 SENSOR_ATTR_2(in%d_min_alarm_enable, 0644, show_bank1_mask,
1087 store_bank1_mask, ABIT_UGURU_VOLT_LOW_ALARM_ENABLE, 0),
1088 SENSOR_ATTR_2(in%d_max_alarm_enable, 0644, show_bank1_mask,
1089 store_bank1_mask, ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE, 0),
1090 }, {
1091 SENSOR_ATTR_2(temp%d_input, 0444, show_bank1_value, NULL, 0, 0),
1092 SENSOR_ATTR_2(temp%d_alarm, 0444, show_bank1_alarm, NULL,
1093 ABIT_UGURU_TEMP_HIGH_ALARM_FLAG, 0),
1094 SENSOR_ATTR_2(temp%d_max, 0644, show_bank1_setting,
1095 store_bank1_setting, 1, 0),
1096 SENSOR_ATTR_2(temp%d_crit, 0644, show_bank1_setting,
1097 store_bank1_setting, 2, 0),
1098 SENSOR_ATTR_2(temp%d_beep, 0644, show_bank1_mask,
1099 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1100 SENSOR_ATTR_2(temp%d_shutdown, 0644, show_bank1_mask,
1101 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1102 SENSOR_ATTR_2(temp%d_alarm_enable, 0644, show_bank1_mask,
1103 store_bank1_mask, ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE, 0),
1104 }
1105};
1106
1107static const struct sensor_device_attribute_2 abituguru_sysfs_fan_templ[6] = {
1108 SENSOR_ATTR_2(fan%d_input, 0444, show_bank2_value, NULL, 0, 0),
1109 SENSOR_ATTR_2(fan%d_alarm, 0444, show_bank2_alarm, NULL, 0, 0),
1110 SENSOR_ATTR_2(fan%d_min, 0644, show_bank2_setting,
1111 store_bank2_setting, 1, 0),
1112 SENSOR_ATTR_2(fan%d_beep, 0644, show_bank2_mask,
1113 store_bank2_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1114 SENSOR_ATTR_2(fan%d_shutdown, 0644, show_bank2_mask,
1115 store_bank2_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1116 SENSOR_ATTR_2(fan%d_alarm_enable, 0644, show_bank2_mask,
1117 store_bank2_mask, ABIT_UGURU_FAN_LOW_ALARM_ENABLE, 0),
1118};
1119
1120static const struct sensor_device_attribute_2 abituguru_sysfs_pwm_templ[6] = {
1121 SENSOR_ATTR_2(pwm%d_enable, 0644, show_pwm_enable,
1122 store_pwm_enable, 0, 0),
1123 SENSOR_ATTR_2(pwm%d_auto_channels_temp, 0644, show_pwm_sensor,
1124 store_pwm_sensor, 0, 0),
1125 SENSOR_ATTR_2(pwm%d_auto_point1_pwm, 0644, show_pwm_setting,
1126 store_pwm_setting, 1, 0),
1127 SENSOR_ATTR_2(pwm%d_auto_point2_pwm, 0644, show_pwm_setting,
1128 store_pwm_setting, 2, 0),
1129 SENSOR_ATTR_2(pwm%d_auto_point1_temp, 0644, show_pwm_setting,
1130 store_pwm_setting, 3, 0),
1131 SENSOR_ATTR_2(pwm%d_auto_point2_temp, 0644, show_pwm_setting,
1132 store_pwm_setting, 4, 0),
1133};
1134
Hans de Goedea2392e02006-06-04 20:23:01 +02001135static struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001136 SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
1137};
1138
1139static int __devinit abituguru_probe(struct platform_device *pdev)
1140{
1141 struct abituguru_data *data;
Hans de Goedea2392e02006-06-04 20:23:01 +02001142 int i, j, used, sysfs_names_free, sysfs_attr_i, res = -ENODEV;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001143 char *sysfs_filename;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001144
1145 /* El weirdo probe order, to keep the sysfs order identical to the
1146 BIOS and window-appliction listing order. */
Hans de Goedea2392e02006-06-04 20:23:01 +02001147 const u8 probe_order[ABIT_UGURU_MAX_BANK1_SENSORS] = {
1148 0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E, 0x02,
1149 0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001150
1151 if (!(data = kzalloc(sizeof(struct abituguru_data), GFP_KERNEL)))
1152 return -ENOMEM;
1153
1154 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
1155 mutex_init(&data->update_lock);
1156 platform_set_drvdata(pdev, data);
1157
1158 /* See if the uGuru is ready */
1159 if (inb_p(data->addr + ABIT_UGURU_DATA) == ABIT_UGURU_STATUS_INPUT)
1160 data->uguru_ready = 1;
1161
1162 /* Completely read the uGuru this has 2 purposes:
1163 - testread / see if one really is there.
1164 - make an in memory copy of all the uguru settings for future use. */
1165 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
Hans de Goedea2392e02006-06-04 20:23:01 +02001166 data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3)
1167 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001168
Hans de Goedea2392e02006-06-04 20:23:01 +02001169 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001170 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, i,
1171 &data->bank1_value[i], 1,
Hans de Goedea2392e02006-06-04 20:23:01 +02001172 ABIT_UGURU_MAX_RETRIES) != 1)
1173 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001174 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1+1, i,
1175 data->bank1_settings[i], 3,
Hans de Goedea2392e02006-06-04 20:23:01 +02001176 ABIT_UGURU_MAX_RETRIES) != 3)
1177 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001178 }
1179 /* Note: We don't know how many bank2 sensors / pwms there really are,
1180 but in order to "detect" this we need to read the maximum amount
1181 anyways. If we read sensors/pwms not there we'll just read crap
1182 this can't hurt. We need the detection because we don't want
1183 unwanted writes, which will hurt! */
1184 for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
1185 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
1186 &data->bank2_value[i], 1,
Hans de Goedea2392e02006-06-04 20:23:01 +02001187 ABIT_UGURU_MAX_RETRIES) != 1)
1188 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001189 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2+1, i,
1190 data->bank2_settings[i], 2,
Hans de Goedea2392e02006-06-04 20:23:01 +02001191 ABIT_UGURU_MAX_RETRIES) != 2)
1192 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001193 }
1194 for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
1195 if (abituguru_read(data, ABIT_UGURU_FAN_PWM, i,
1196 data->pwm_settings[i], 5,
Hans de Goedea2392e02006-06-04 20:23:01 +02001197 ABIT_UGURU_MAX_RETRIES) != 5)
1198 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001199 }
1200 data->last_updated = jiffies;
1201
1202 /* Detect sensor types and fill the sysfs attr for bank1 */
Hans de Goedea2392e02006-06-04 20:23:01 +02001203 sysfs_attr_i = 0;
1204 sysfs_filename = data->sysfs_names;
1205 sysfs_names_free = ABITUGURU_SYSFS_NAMES_LENGTH;
1206 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001207 res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
Hans de Goedea2392e02006-06-04 20:23:01 +02001208 if (res < 0)
1209 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001210 if (res == ABIT_UGURU_NC)
1211 continue;
1212
Hans de Goedea2392e02006-06-04 20:23:01 +02001213 /* res 1 (temp) sensors have 7 sysfs entries, 0 (in) 9 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001214 for (j = 0; j < (res ? 7 : 9); j++) {
Hans de Goedea2392e02006-06-04 20:23:01 +02001215 used = snprintf(sysfs_filename, sysfs_names_free,
1216 abituguru_sysfs_bank1_templ[res][j].dev_attr.
1217 attr.name, data->bank1_sensors[res] + res)
1218 + 1;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001219 data->sysfs_attr[sysfs_attr_i] =
1220 abituguru_sysfs_bank1_templ[res][j];
1221 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1222 sysfs_filename;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001223 data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
Hans de Goedea2392e02006-06-04 20:23:01 +02001224 sysfs_filename += used;
1225 sysfs_names_free -= used;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001226 sysfs_attr_i++;
1227 }
1228 data->bank1_max_value[probe_order[i]] =
1229 abituguru_bank1_max_value[res];
1230 data->bank1_address[res][data->bank1_sensors[res]] =
1231 probe_order[i];
1232 data->bank1_sensors[res]++;
1233 }
1234 /* Detect number of sensors and fill the sysfs attr for bank2 (fans) */
1235 abituguru_detect_no_bank2_sensors(data);
1236 for (i = 0; i < data->bank2_sensors; i++) {
Hans de Goedea2392e02006-06-04 20:23:01 +02001237 for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_fan_templ); j++) {
1238 used = snprintf(sysfs_filename, sysfs_names_free,
1239 abituguru_sysfs_fan_templ[j].dev_attr.attr.name,
1240 i + 1) + 1;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001241 data->sysfs_attr[sysfs_attr_i] =
1242 abituguru_sysfs_fan_templ[j];
1243 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1244 sysfs_filename;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001245 data->sysfs_attr[sysfs_attr_i].index = i;
Hans de Goedea2392e02006-06-04 20:23:01 +02001246 sysfs_filename += used;
1247 sysfs_names_free -= used;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001248 sysfs_attr_i++;
1249 }
1250 }
1251 /* Detect number of sensors and fill the sysfs attr for pwms */
1252 abituguru_detect_no_pwms(data);
1253 for (i = 0; i < data->pwms; i++) {
Hans de Goedea2392e02006-06-04 20:23:01 +02001254 for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_pwm_templ); j++) {
1255 used = snprintf(sysfs_filename, sysfs_names_free,
1256 abituguru_sysfs_pwm_templ[j].dev_attr.attr.name,
1257 i + 1) + 1;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001258 data->sysfs_attr[sysfs_attr_i] =
1259 abituguru_sysfs_pwm_templ[j];
1260 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1261 sysfs_filename;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001262 data->sysfs_attr[sysfs_attr_i].index = i;
Hans de Goedea2392e02006-06-04 20:23:01 +02001263 sysfs_filename += used;
1264 sysfs_names_free -= used;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001265 sysfs_attr_i++;
1266 }
1267 }
Hans de Goedea2392e02006-06-04 20:23:01 +02001268 /* Fail safe check, this should never happen! */
1269 if (sysfs_names_free < 0) {
1270 printk(KERN_ERR ABIT_UGURU_NAME ": Fatal error ran out of "
1271 "space for sysfs attr names. This should never "
1272 "happen please report to the abituguru maintainer "
1273 "(see MAINTAINERS)\n");
1274 res = -ENAMETOOLONG;
1275 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001276 }
1277 printk(KERN_INFO ABIT_UGURU_NAME ": found Abit uGuru\n");
1278
1279 /* Register sysfs hooks */
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001280 for (i = 0; i < sysfs_attr_i; i++)
Hans de Goedebc8f0a22007-02-14 21:15:05 +01001281 if (device_create_file(&pdev->dev,
1282 &data->sysfs_attr[i].dev_attr))
1283 goto abituguru_probe_error;
Hans de Goedea2392e02006-06-04 20:23:01 +02001284 for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
Hans de Goedebc8f0a22007-02-14 21:15:05 +01001285 if (device_create_file(&pdev->dev,
1286 &abituguru_sysfs_attr[i].dev_attr))
1287 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001288
Hans de Goedebc8f0a22007-02-14 21:15:05 +01001289 data->class_dev = hwmon_device_register(&pdev->dev);
1290 if (!IS_ERR(data->class_dev))
1291 return 0; /* success */
Hans de Goedea2392e02006-06-04 20:23:01 +02001292
Hans de Goedebc8f0a22007-02-14 21:15:05 +01001293 res = PTR_ERR(data->class_dev);
Hans de Goedea2392e02006-06-04 20:23:01 +02001294abituguru_probe_error:
Hans de Goedebc8f0a22007-02-14 21:15:05 +01001295 for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
1296 device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1297 for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1298 device_remove_file(&pdev->dev,
1299 &abituguru_sysfs_attr[i].dev_attr);
Jean Delvare04a62172007-06-12 13:57:19 +02001300 platform_set_drvdata(pdev, NULL);
Hans de Goedea2392e02006-06-04 20:23:01 +02001301 kfree(data);
1302 return res;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001303}
1304
1305static int __devexit abituguru_remove(struct platform_device *pdev)
1306{
Hans de Goedebc8f0a22007-02-14 21:15:05 +01001307 int i;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001308 struct abituguru_data *data = platform_get_drvdata(pdev);
1309
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001310 hwmon_device_unregister(data->class_dev);
Hans de Goedebc8f0a22007-02-14 21:15:05 +01001311 for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
1312 device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1313 for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1314 device_remove_file(&pdev->dev,
1315 &abituguru_sysfs_attr[i].dev_attr);
Jean Delvare04a62172007-06-12 13:57:19 +02001316 platform_set_drvdata(pdev, NULL);
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001317 kfree(data);
1318
1319 return 0;
1320}
1321
1322static struct abituguru_data *abituguru_update_device(struct device *dev)
1323{
1324 int i, err;
1325 struct abituguru_data *data = dev_get_drvdata(dev);
1326 /* fake a complete successful read if no update necessary. */
1327 char success = 1;
1328
1329 mutex_lock(&data->update_lock);
1330 if (time_after(jiffies, data->last_updated + HZ)) {
1331 success = 0;
1332 if ((err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1333 data->alarms, 3, 0)) != 3)
1334 goto LEAVE_UPDATE;
Hans de Goedea2392e02006-06-04 20:23:01 +02001335 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001336 if ((err = abituguru_read(data,
1337 ABIT_UGURU_SENSOR_BANK1, i,
1338 &data->bank1_value[i], 1, 0)) != 1)
1339 goto LEAVE_UPDATE;
1340 if ((err = abituguru_read(data,
1341 ABIT_UGURU_SENSOR_BANK1 + 1, i,
1342 data->bank1_settings[i], 3, 0)) != 3)
1343 goto LEAVE_UPDATE;
1344 }
1345 for (i = 0; i < data->bank2_sensors; i++)
1346 if ((err = abituguru_read(data,
1347 ABIT_UGURU_SENSOR_BANK2, i,
1348 &data->bank2_value[i], 1, 0)) != 1)
1349 goto LEAVE_UPDATE;
1350 /* success! */
1351 success = 1;
1352 data->update_timeouts = 0;
1353LEAVE_UPDATE:
1354 /* handle timeout condition */
Hans de Goedefaf9b612006-08-25 10:24:20 +02001355 if (!success && (err == -EBUSY || err >= 0)) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001356 /* No overflow please */
1357 if (data->update_timeouts < 255u)
1358 data->update_timeouts++;
1359 if (data->update_timeouts <= ABIT_UGURU_MAX_TIMEOUTS) {
1360 ABIT_UGURU_DEBUG(3, "timeout exceeded, will "
1361 "try again next update\n");
1362 /* Just a timeout, fake a successful read */
1363 success = 1;
1364 } else
1365 ABIT_UGURU_DEBUG(1, "timeout exceeded %d "
1366 "times waiting for more input state\n",
1367 (int)data->update_timeouts);
1368 }
1369 /* On success set last_updated */
1370 if (success)
1371 data->last_updated = jiffies;
1372 }
1373 mutex_unlock(&data->update_lock);
1374
1375 if (success)
1376 return data;
1377 else
1378 return NULL;
1379}
1380
Hans de Goede360b9ab2006-08-28 14:42:24 +02001381#ifdef CONFIG_PM
1382static int abituguru_suspend(struct platform_device *pdev, pm_message_t state)
1383{
1384 struct abituguru_data *data = platform_get_drvdata(pdev);
1385 /* make sure all communications with the uguru are done and no new
1386 ones are started */
1387 mutex_lock(&data->update_lock);
1388 return 0;
1389}
1390
1391static int abituguru_resume(struct platform_device *pdev)
1392{
1393 struct abituguru_data *data = platform_get_drvdata(pdev);
1394 /* See if the uGuru is still ready */
1395 if (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT)
1396 data->uguru_ready = 0;
1397 mutex_unlock(&data->update_lock);
1398 return 0;
1399}
1400#else
1401#define abituguru_suspend NULL
1402#define abituguru_resume NULL
1403#endif /* CONFIG_PM */
1404
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001405static struct platform_driver abituguru_driver = {
1406 .driver = {
1407 .owner = THIS_MODULE,
1408 .name = ABIT_UGURU_NAME,
1409 },
Hans de Goede360b9ab2006-08-28 14:42:24 +02001410 .probe = abituguru_probe,
1411 .remove = __devexit_p(abituguru_remove),
1412 .suspend = abituguru_suspend,
1413 .resume = abituguru_resume,
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001414};
1415
1416static int __init abituguru_detect(void)
1417{
1418 /* See if there is an uguru there. After a reboot uGuru will hold 0x00
1419 at DATA and 0xAC, when this driver has already been loaded once
1420 DATA will hold 0x08. For most uGuru's CMD will hold 0xAC in either
1421 scenario but some will hold 0x00.
1422 Some uGuru's initally hold 0x09 at DATA and will only hold 0x08
1423 after reading CMD first, so CMD must be read first! */
1424 u8 cmd_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_CMD);
1425 u8 data_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_DATA);
1426 if (((data_val == 0x00) || (data_val == 0x08)) &&
1427 ((cmd_val == 0x00) || (cmd_val == 0xAC)))
1428 return ABIT_UGURU_BASE;
1429
1430 ABIT_UGURU_DEBUG(2, "no Abit uGuru found, data = 0x%02X, cmd = "
1431 "0x%02X\n", (unsigned int)data_val, (unsigned int)cmd_val);
1432
1433 if (force) {
1434 printk(KERN_INFO ABIT_UGURU_NAME ": Assuming Abit uGuru is "
1435 "present because of \"force\" parameter\n");
1436 return ABIT_UGURU_BASE;
1437 }
1438
1439 /* No uGuru found */
1440 return -ENODEV;
1441}
1442
1443static struct platform_device *abituguru_pdev;
1444
1445static int __init abituguru_init(void)
1446{
1447 int address, err;
1448 struct resource res = { .flags = IORESOURCE_IO };
1449
1450 address = abituguru_detect();
1451 if (address < 0)
1452 return address;
1453
1454 err = platform_driver_register(&abituguru_driver);
1455 if (err)
1456 goto exit;
1457
1458 abituguru_pdev = platform_device_alloc(ABIT_UGURU_NAME, address);
1459 if (!abituguru_pdev) {
1460 printk(KERN_ERR ABIT_UGURU_NAME
1461 ": Device allocation failed\n");
1462 err = -ENOMEM;
1463 goto exit_driver_unregister;
1464 }
1465
1466 res.start = address;
1467 res.end = address + ABIT_UGURU_REGION_LENGTH - 1;
1468 res.name = ABIT_UGURU_NAME;
1469
1470 err = platform_device_add_resources(abituguru_pdev, &res, 1);
1471 if (err) {
1472 printk(KERN_ERR ABIT_UGURU_NAME
1473 ": Device resource addition failed (%d)\n", err);
1474 goto exit_device_put;
1475 }
1476
1477 err = platform_device_add(abituguru_pdev);
1478 if (err) {
1479 printk(KERN_ERR ABIT_UGURU_NAME
1480 ": Device addition failed (%d)\n", err);
1481 goto exit_device_put;
1482 }
1483
1484 return 0;
1485
1486exit_device_put:
1487 platform_device_put(abituguru_pdev);
1488exit_driver_unregister:
1489 platform_driver_unregister(&abituguru_driver);
1490exit:
1491 return err;
1492}
1493
1494static void __exit abituguru_exit(void)
1495{
1496 platform_device_unregister(abituguru_pdev);
1497 platform_driver_unregister(&abituguru_driver);
1498}
1499
1500MODULE_AUTHOR("Hans de Goede <j.w.r.degoede@hhs.nl>");
1501MODULE_DESCRIPTION("Abit uGuru Sensor device");
1502MODULE_LICENSE("GPL");
1503
1504module_init(abituguru_init);
1505module_exit(abituguru_exit);