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