Blame view

drivers/hwmon/abituguru.c 52 KB
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1
  /*
93d0cc588   Hans de Goede   hwmon: (abituguru...
2
      abituguru.c Copyright (c) 2005-2006 Hans de Goede <hdegoede@redhat.com>
f2b84bbce   Hans de Goede   [PATCH] abituguru...
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  
      This program is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published by
      the Free Software Foundation; either version 2 of the License, or
      (at your option) any later version.
  
      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.
  
      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
  /*
3faa1ffb4   Hans de Goede   hwmon: Add suppor...
19
20
21
      This driver supports the sensor part of the first and second revision of
      the custom Abit uGuru chip found on Abit uGuru motherboards. Note: because
      of lack of specs the CPU/RAM voltage & frequency control is not supported!
f2b84bbce   Hans de Goede   [PATCH] abituguru...
22
  */
28ebfa13f   Joe Perches   hwmon: (abituguru...
23
24
  
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
f2b84bbce   Hans de Goede   [PATCH] abituguru...
25
  #include <linux/module.h>
f6a570333   Al Viro   [PATCH] severing ...
26
  #include <linux/sched.h>
f2b84bbce   Hans de Goede   [PATCH] abituguru...
27
28
29
30
31
  #include <linux/init.h>
  #include <linux/slab.h>
  #include <linux/jiffies.h>
  #include <linux/mutex.h>
  #include <linux/err.h>
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
32
  #include <linux/delay.h>
f2b84bbce   Hans de Goede   [PATCH] abituguru...
33
34
35
  #include <linux/platform_device.h>
  #include <linux/hwmon.h>
  #include <linux/hwmon-sysfs.h>
c182f5bbf   Hans de Goede   hwmon: refuse to ...
36
  #include <linux/dmi.h>
6055fae8a   H Hartley Sweeten   hwmon: Include <l...
37
  #include <linux/io.h>
f2b84bbce   Hans de Goede   [PATCH] abituguru...
38
39
40
41
42
43
  
  /* Banks */
  #define ABIT_UGURU_ALARM_BANK			0x20 /* 1x 3 bytes */
  #define ABIT_UGURU_SENSOR_BANK1			0x21 /* 16x volt and temp */
  #define ABIT_UGURU_FAN_PWM			0x24 /* 3x 5 bytes */
  #define ABIT_UGURU_SENSOR_BANK2			0x26 /* fans */
a2392e0b9   Hans de Goede   [PATCH] abituguru...
44
45
46
47
  /* max nr of sensors in bank1, a bank1 sensor can be in, temp or nc */
  #define ABIT_UGURU_MAX_BANK1_SENSORS		16
  /* Warning if you increase one of the 2 MAX defines below to 10 or higher you
     should adjust the belonging _NAMES_LENGTH macro for the 2 digit number! */
f2b84bbce   Hans de Goede   [PATCH] abituguru...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  /* max nr of sensors in bank2, currently mb's with max 6 fans are known */
  #define ABIT_UGURU_MAX_BANK2_SENSORS		6
  /* max nr of pwm outputs, currently mb's with max 5 pwm outputs are known */
  #define ABIT_UGURU_MAX_PWMS			5
  /* uGuru sensor bank 1 flags */			     /* Alarm if: */
  #define ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE	0x01 /*  temp over warn */
  #define ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE	0x02 /*  volt over max */
  #define ABIT_UGURU_VOLT_LOW_ALARM_ENABLE	0x04 /*  volt under min */
  #define ABIT_UGURU_TEMP_HIGH_ALARM_FLAG		0x10 /* temp is over warn */
  #define ABIT_UGURU_VOLT_HIGH_ALARM_FLAG		0x20 /* volt is over max */
  #define ABIT_UGURU_VOLT_LOW_ALARM_FLAG		0x40 /* volt is under min */
  /* uGuru sensor bank 2 flags */			     /* Alarm if: */
  #define ABIT_UGURU_FAN_LOW_ALARM_ENABLE		0x01 /*   fan under min */
  /* uGuru sensor bank common flags */
  #define ABIT_UGURU_BEEP_ENABLE			0x08 /* beep if alarm */
  #define ABIT_UGURU_SHUTDOWN_ENABLE		0x80 /* shutdown if alarm */
  /* uGuru fan PWM (speed control) flags */
  #define ABIT_UGURU_FAN_PWM_ENABLE		0x80 /* enable speed control */
  /* Values used for conversion */
  #define ABIT_UGURU_FAN_MAX			15300 /* RPM */
  /* Bank1 sensor types */
  #define ABIT_UGURU_IN_SENSOR			0
  #define ABIT_UGURU_TEMP_SENSOR			1
  #define ABIT_UGURU_NC				2
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
72
73
74
75
76
77
78
79
  /* In many cases we need to wait for the uGuru to reach a certain status, most
     of the time it will reach this status within 30 - 90 ISA reads, and thus we
     can best busy wait. This define gives the total amount of reads to try. */
  #define ABIT_UGURU_WAIT_TIMEOUT			125
  /* However sometimes older versions of the uGuru seem to be distracted and they
     do not respond for a long time. To handle this we sleep before each of the
     last ABIT_UGURU_WAIT_TIMEOUT_SLEEP tries. */
  #define ABIT_UGURU_WAIT_TIMEOUT_SLEEP		5
f2b84bbce   Hans de Goede   [PATCH] abituguru...
80
  /* Normally all expected status in abituguru_ready, are reported after the
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
81
82
     first read, but sometimes not and we need to poll. */
  #define ABIT_UGURU_READY_TIMEOUT		5
f2b84bbce   Hans de Goede   [PATCH] abituguru...
83
84
85
  /* Maximum 3 retries on timedout reads/writes, delay 200 ms before retrying */
  #define ABIT_UGURU_MAX_RETRIES			3
  #define ABIT_UGURU_RETRY_DELAY			(HZ/5)
a2392e0b9   Hans de Goede   [PATCH] abituguru...
86
  /* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is an error */
f2b84bbce   Hans de Goede   [PATCH] abituguru...
87
  #define ABIT_UGURU_MAX_TIMEOUTS			2
a2392e0b9   Hans de Goede   [PATCH] abituguru...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  /* utility macros */
  #define ABIT_UGURU_NAME				"abituguru"
  #define ABIT_UGURU_DEBUG(level, format, arg...)				\
  	if (level <= verbose)						\
  		printk(KERN_DEBUG ABIT_UGURU_NAME ": "	format , ## arg)
  /* Macros to help calculate the sysfs_names array length */
  /* sum of strlen of: in??_input\0, in??_{min,max}\0, in??_{min,max}_alarm\0,
     in??_{min,max}_alarm_enable\0, in??_beep\0, in??_shutdown\0 */
  #define ABITUGURU_IN_NAMES_LENGTH	(11 + 2 * 9 + 2 * 15 + 2 * 22 + 10 + 14)
  /* sum of strlen of: temp??_input\0, temp??_max\0, temp??_crit\0,
     temp??_alarm\0, temp??_alarm_enable\0, temp??_beep\0, temp??_shutdown\0 */
  #define ABITUGURU_TEMP_NAMES_LENGTH	(13 + 11 + 12 + 13 + 20 + 12 + 16)
  /* sum of strlen of: fan?_input\0, fan?_min\0, fan?_alarm\0,
     fan?_alarm_enable\0, fan?_beep\0, fan?_shutdown\0 */
  #define ABITUGURU_FAN_NAMES_LENGTH	(11 + 9 + 11 + 18 + 10 + 14)
  /* sum of strlen of: pwm?_enable\0, pwm?_auto_channels_temp\0,
     pwm?_auto_point{1,2}_pwm\0, pwm?_auto_point{1,2}_temp\0 */
  #define ABITUGURU_PWM_NAMES_LENGTH	(12 + 24 + 2 * 21 + 2 * 22)
  /* IN_NAMES_LENGTH > TEMP_NAMES_LENGTH so assume all bank1 sensors are in */
  #define ABITUGURU_SYSFS_NAMES_LENGTH	( \
  	ABIT_UGURU_MAX_BANK1_SENSORS * ABITUGURU_IN_NAMES_LENGTH + \
  	ABIT_UGURU_MAX_BANK2_SENSORS * ABITUGURU_FAN_NAMES_LENGTH + \
  	ABIT_UGURU_MAX_PWMS * ABITUGURU_PWM_NAMES_LENGTH)
  
  /* All the macros below are named identical to the oguru and oguru2 programs
f2b84bbce   Hans de Goede   [PATCH] abituguru...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
     reverse engineered by Olle Sandberg, hence the names might not be 100%
     logical. I could come up with better names, but I prefer keeping the names
     identical so that this driver can be compared with his work more easily. */
  /* Two i/o-ports are used by uGuru */
  #define ABIT_UGURU_BASE				0x00E0
  /* Used to tell uGuru what to read and to read the actual data */
  #define ABIT_UGURU_CMD				0x00
  /* Mostly used to check if uGuru is busy */
  #define ABIT_UGURU_DATA				0x04
  #define ABIT_UGURU_REGION_LENGTH		5
  /* uGuru status' */
  #define ABIT_UGURU_STATUS_WRITE			0x00 /* Ready to be written */
  #define ABIT_UGURU_STATUS_READ			0x01 /* Ready to be read */
  #define ABIT_UGURU_STATUS_INPUT			0x08 /* More input */
  #define ABIT_UGURU_STATUS_READY			0x09 /* Ready to be written */
f2b84bbce   Hans de Goede   [PATCH] abituguru...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  
  /* Constants */
  /* in (Volt) sensors go up to 3494 mV, temp to 255000 millidegrees Celsius */
  static const int abituguru_bank1_max_value[2] = { 3494, 255000 };
  /* Min / Max allowed values for sensor2 (fan) alarm threshold, these values
     correspond to 300-3000 RPM */
  static const u8 abituguru_bank2_min_threshold = 5;
  static const u8 abituguru_bank2_max_threshold = 50;
  /* Register 0 is a bitfield, 1 and 2 are pwm settings (255 = 100%), 3 and 4
     are temperature trip points. */
  static const int abituguru_pwm_settings_multiplier[5] = { 0, 1, 1, 1000, 1000 };
  /* Min / Max allowed values for pwm_settings. Note: pwm1 (CPU fan) is a
     special case the minium allowed pwm% setting for this is 30% (77) on
     some MB's this special case is handled in the code! */
  static const u8 abituguru_pwm_min[5] = { 0, 170, 170, 25, 25 };
  static const u8 abituguru_pwm_max[5] = { 0, 255, 255, 75, 75 };
  
  
  /* Insmod parameters */
90ab5ee94   Rusty Russell   module_param: mak...
147
  static bool force;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
148
149
  module_param(force, bool, 0);
  MODULE_PARM_DESC(force, "Set to one to force detection.");
9b2ad1298   Hans de Goede   [PATCH] hwmon: Fi...
150
151
152
153
154
155
156
157
158
159
160
161
  static int bank1_types[ABIT_UGURU_MAX_BANK1_SENSORS] = { -1, -1, -1, -1, -1,
  	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
  module_param_array(bank1_types, int, NULL, 0);
  MODULE_PARM_DESC(bank1_types, "Bank1 sensortype autodetection override:
  "
  	"   -1 autodetect
  "
  	"    0 volt sensor
  "
  	"    1 temp sensor
  "
  	"    2 not connected");
f2b84bbce   Hans de Goede   [PATCH] abituguru...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  static int fan_sensors;
  module_param(fan_sensors, int, 0);
  MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru "
  	"(0 = autodetect)");
  static int pwms;
  module_param(pwms, int, 0);
  MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru "
  	"(0 = autodetect)");
  
  /* Default verbose is 2, since this driver is still in the testing phase */
  static int verbose = 2;
  module_param(verbose, int, 0644);
  MODULE_PARM_DESC(verbose, "How verbose should the driver be? (0-3):
  "
  	"   0 normal output
  "
  	"   1 + verbose error reporting
  "
  	"   2 + sensors type probing info
  "
  	"   3 + retryable error reporting");
  
  
  /* For the Abit uGuru, we need to keep some data in memory.
     The structure is dynamically allocated, at the same time when a new
     abituguru device is allocated. */
  struct abituguru_data {
1beeffe43   Tony Jones   hwmon: Convert fr...
189
  	struct device *hwmon_dev;	/* hwmon registered device */
f2b84bbce   Hans de Goede   [PATCH] abituguru...
190
191
192
193
194
195
196
197
198
199
200
201
  	struct mutex update_lock;	/* protect access to data and uGuru */
  	unsigned long last_updated;	/* In jiffies */
  	unsigned short addr;		/* uguru base address */
  	char uguru_ready;		/* is the uguru in ready state? */
  	unsigned char update_timeouts;	/* number of update timeouts since last
  					   successful update */
  
  	/* The sysfs attr and their names are generated automatically, for bank1
  	   we cannot use a predefined array because we don't know beforehand
  	   of a sensor is a volt or a temp sensor, for bank2 and the pwms its
  	   easier todo things the same way.  For in sensors we have 9 (temp 7)
  	   sysfs entries per sensor, for bank2 and pwms 6. */
a2392e0b9   Hans de Goede   [PATCH] abituguru...
202
203
  	struct sensor_device_attribute_2 sysfs_attr[
  		ABIT_UGURU_MAX_BANK1_SENSORS * 9 +
f2b84bbce   Hans de Goede   [PATCH] abituguru...
204
  		ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6];
a2392e0b9   Hans de Goede   [PATCH] abituguru...
205
206
  	/* Buffer to store the dynamically generated sysfs names */
  	char sysfs_names[ABITUGURU_SYSFS_NAMES_LENGTH];
f2b84bbce   Hans de Goede   [PATCH] abituguru...
207
208
  
  	/* Bank 1 data */
a2392e0b9   Hans de Goede   [PATCH] abituguru...
209
210
211
212
213
  	/* number of and addresses of [0] in, [1] temp sensors */
  	u8 bank1_sensors[2];
  	u8 bank1_address[2][ABIT_UGURU_MAX_BANK1_SENSORS];
  	u8 bank1_value[ABIT_UGURU_MAX_BANK1_SENSORS];
  	/* This array holds 3 entries per sensor for the bank 1 sensor settings
f2b84bbce   Hans de Goede   [PATCH] abituguru...
214
  	   (flags, min, max for voltage / flags, warn, shutdown for temp). */
a2392e0b9   Hans de Goede   [PATCH] abituguru...
215
  	u8 bank1_settings[ABIT_UGURU_MAX_BANK1_SENSORS][3];
f2b84bbce   Hans de Goede   [PATCH] abituguru...
216
217
  	/* Maximum value for each sensor used for scaling in mV/millidegrees
  	   Celsius. */
a2392e0b9   Hans de Goede   [PATCH] abituguru...
218
  	int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS];
f2b84bbce   Hans de Goede   [PATCH] abituguru...
219
220
221
222
223
224
225
226
227
228
229
230
231
  
  	/* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */
  	u8 bank2_sensors; /* actual number of bank2 sensors found */
  	u8 bank2_value[ABIT_UGURU_MAX_BANK2_SENSORS];
  	u8 bank2_settings[ABIT_UGURU_MAX_BANK2_SENSORS][2]; /* flags, min */
  
  	/* Alarms 2 bytes for bank1, 1 byte for bank2 */
  	u8 alarms[3];
  
  	/* Fan PWM (speed control) 5 bytes per PWM */
  	u8 pwms; /* actual number of pwms found */
  	u8 pwm_settings[ABIT_UGURU_MAX_PWMS][5];
  };
28ebfa13f   Joe Perches   hwmon: (abituguru...
232
233
234
  static const char *never_happen = "This should never happen.";
  static const char *report_this =
  	"Please report this to the abituguru maintainer (see MAINTAINERS)";
f2b84bbce   Hans de Goede   [PATCH] abituguru...
235
236
237
238
239
240
241
242
243
  /* wait till the uguru is in the specified state */
  static int abituguru_wait(struct abituguru_data *data, u8 state)
  {
  	int timeout = ABIT_UGURU_WAIT_TIMEOUT;
  
  	while (inb_p(data->addr + ABIT_UGURU_DATA) != state) {
  		timeout--;
  		if (timeout == 0)
  			return -EBUSY;
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
244
245
246
247
  		/* sleep a bit before our last few tries, see the comment on
  		   this where ABIT_UGURU_WAIT_TIMEOUT_SLEEP is defined. */
  		if (timeout <= ABIT_UGURU_WAIT_TIMEOUT_SLEEP)
  			msleep(0);
f2b84bbce   Hans de Goede   [PATCH] abituguru...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  	}
  	return 0;
  }
  
  /* Put the uguru in ready for input state */
  static int abituguru_ready(struct abituguru_data *data)
  {
  	int timeout = ABIT_UGURU_READY_TIMEOUT;
  
  	if (data->uguru_ready)
  		return 0;
  
  	/* Reset? / Prepare for next read/write cycle */
  	outb(0x00, data->addr + ABIT_UGURU_DATA);
  
  	/* Wait till the uguru is ready */
  	if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) {
  		ABIT_UGURU_DEBUG(1,
  			"timeout exceeded waiting for ready state
  ");
  		return -EIO;
  	}
  
  	/* Cmd port MUST be read now and should contain 0xAC */
  	while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
  		timeout--;
  		if (timeout == 0) {
  			ABIT_UGURU_DEBUG(1,
  			   "CMD reg does not hold 0xAC after ready command
  ");
  			return -EIO;
  		}
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
280
  		msleep(0);
f2b84bbce   Hans de Goede   [PATCH] abituguru...
281
282
283
284
285
286
287
288
289
290
291
292
293
  	}
  
  	/* After this the ABIT_UGURU_DATA port should contain
  	   ABIT_UGURU_STATUS_INPUT */
  	timeout = ABIT_UGURU_READY_TIMEOUT;
  	while (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT) {
  		timeout--;
  		if (timeout == 0) {
  			ABIT_UGURU_DEBUG(1,
  				"state != more input after ready command
  ");
  			return -EIO;
  		}
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
294
  		msleep(0);
f2b84bbce   Hans de Goede   [PATCH] abituguru...
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
  	}
  
  	data->uguru_ready = 1;
  	return 0;
  }
  
  /* Send the bank and then sensor address to the uGuru for the next read/write
     cycle. This function gets called as the first part of a read/write by
     abituguru_read and abituguru_write. This function should never be
     called by any other function. */
  static int abituguru_send_address(struct abituguru_data *data,
  	u8 bank_addr, u8 sensor_addr, int retries)
  {
  	/* assume the caller does error handling itself if it has not requested
  	   any retries, and thus be quiet. */
  	int report_errors = retries;
  
  	for (;;) {
  		/* Make sure the uguru is ready and then send the bank address,
  		   after this the uguru is no longer "ready". */
  		if (abituguru_ready(data) != 0)
  			return -EIO;
  		outb(bank_addr, data->addr + ABIT_UGURU_DATA);
  		data->uguru_ready = 0;
  
  		/* Wait till the uguru is ABIT_UGURU_STATUS_INPUT state again
  		   and send the sensor addr */
  		if (abituguru_wait(data, ABIT_UGURU_STATUS_INPUT)) {
  			if (retries) {
  				ABIT_UGURU_DEBUG(3, "timeout exceeded "
  					"waiting for more input state, %d "
  					"tries remaining
  ", retries);
  				set_current_state(TASK_UNINTERRUPTIBLE);
  				schedule_timeout(ABIT_UGURU_RETRY_DELAY);
  				retries--;
  				continue;
  			}
  			if (report_errors)
  				ABIT_UGURU_DEBUG(1, "timeout exceeded "
  					"waiting for more input state "
  					"(bank: %d)
  ", (int)bank_addr);
  			return -EBUSY;
  		}
  		outb(sensor_addr, data->addr + ABIT_UGURU_CMD);
  		return 0;
  	}
  }
  
  /* Read count bytes from sensor sensor_addr in bank bank_addr and store the
     result in buf, retry the send address part of the read retries times. */
  static int abituguru_read(struct abituguru_data *data,
  	u8 bank_addr, u8 sensor_addr, u8 *buf, int count, int retries)
  {
  	int i;
  
  	/* Send the address */
  	i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
  	if (i)
  		return i;
  
  	/* And read the data */
  	for (i = 0; i < count; i++) {
  		if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
360
361
  			ABIT_UGURU_DEBUG(retries ? 1 : 3,
  				"timeout exceeded waiting for "
f2b84bbce   Hans de Goede   [PATCH] abituguru...
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
  				"read state (bank: %d, sensor: %d)
  ",
  				(int)bank_addr, (int)sensor_addr);
  			break;
  		}
  		buf[i] = inb(data->addr + ABIT_UGURU_CMD);
  	}
  
  	/* Last put the chip back in ready state */
  	abituguru_ready(data);
  
  	return i;
  }
  
  /* Write count bytes from buf to sensor sensor_addr in bank bank_addr, the send
     address part of the write is always retried ABIT_UGURU_MAX_RETRIES times. */
  static int abituguru_write(struct abituguru_data *data,
  	u8 bank_addr, u8 sensor_addr, u8 *buf, int count)
  {
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
381
382
383
  	/* We use the ready timeout as we have to wait for 0xAC just like the
  	   ready function */
  	int i, timeout = ABIT_UGURU_READY_TIMEOUT;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
  
  	/* Send the address */
  	i = abituguru_send_address(data, bank_addr, sensor_addr,
  		ABIT_UGURU_MAX_RETRIES);
  	if (i)
  		return i;
  
  	/* And write the data */
  	for (i = 0; i < count; i++) {
  		if (abituguru_wait(data, ABIT_UGURU_STATUS_WRITE)) {
  			ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
  				"write state (bank: %d, sensor: %d)
  ",
  				(int)bank_addr, (int)sensor_addr);
  			break;
  		}
  		outb(buf[i], data->addr + ABIT_UGURU_CMD);
  	}
  
  	/* Now we need to wait till the chip is ready to be read again,
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
404
405
  	   so that we can read 0xAC as confirmation that our write has
  	   succeeded. */
f2b84bbce   Hans de Goede   [PATCH] abituguru...
406
407
408
409
410
411
412
413
414
  	if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
  		ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for read state "
  			"after write (bank: %d, sensor: %d)
  ", (int)bank_addr,
  			(int)sensor_addr);
  		return -EIO;
  	}
  
  	/* Cmd port MUST be read now and should contain 0xAC */
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
415
416
417
418
419
420
421
422
423
424
  	while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
  		timeout--;
  		if (timeout == 0) {
  			ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after "
  				"write (bank: %d, sensor: %d)
  ",
  				(int)bank_addr, (int)sensor_addr);
  			return -EIO;
  		}
  		msleep(0);
f2b84bbce   Hans de Goede   [PATCH] abituguru...
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
  	}
  
  	/* Last put the chip back in ready state */
  	abituguru_ready(data);
  
  	return i;
  }
  
  /* Detect sensor type. Temp and Volt sensors are enabled with
     different masks and will ignore enable masks not meant for them.
     This enables us to test what kind of sensor we're dealing with.
     By setting the alarm thresholds so that we will always get an
     alarm for sensor type X and then enabling the sensor as sensor type
     X, if we then get an alarm it is a sensor of type X. */
  static int __devinit
  abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
  				   u8 sensor_addr)
  {
e432dc811   Hans de Goede   hwmon: fix detect...
443
  	u8 val, test_flag, buf[3];
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
444
  	int i, ret = -ENODEV; /* error is the most common used retval :| */
f2b84bbce   Hans de Goede   [PATCH] abituguru...
445

9b2ad1298   Hans de Goede   [PATCH] hwmon: Fi...
446
447
448
449
450
451
452
453
454
  	/* If overriden by the user return the user selected type */
  	if (bank1_types[sensor_addr] >= ABIT_UGURU_IN_SENSOR &&
  			bank1_types[sensor_addr] <= ABIT_UGURU_NC) {
  		ABIT_UGURU_DEBUG(2, "assuming sensor type %d for bank1 sensor "
  			"%d because of \"bank1_types\" module param
  ",
  			bank1_types[sensor_addr], (int)sensor_addr);
  		return bank1_types[sensor_addr];
  	}
f2b84bbce   Hans de Goede   [PATCH] abituguru...
455
456
457
  	/* First read the sensor and the current settings */
  	if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, sensor_addr, &val,
  			1, ABIT_UGURU_MAX_RETRIES) != 1)
a2392e0b9   Hans de Goede   [PATCH] abituguru...
458
  		return -ENODEV;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
459
460
  
  	/* Test val is sane / usable for sensor type detection. */
e432dc811   Hans de Goede   hwmon: fix detect...
461
  	if ((val < 10u) || (val > 250u)) {
28ebfa13f   Joe Perches   hwmon: (abituguru...
462
  		pr_warn("bank1-sensor: %d reading (%d) too close to limits, "
f2b84bbce   Hans de Goede   [PATCH] abituguru...
463
464
465
466
467
468
469
470
471
472
473
474
  			"unable to determine sensor type, skipping sensor
  ",
  			(int)sensor_addr, (int)val);
  		/* assume no sensor is there for sensors for which we can't
  		   determine the sensor type because their reading is too close
  		   to their limits, this usually means no sensor is there. */
  		return ABIT_UGURU_NC;
  	}
  
  	ABIT_UGURU_DEBUG(2, "testing bank1 sensor %d
  ", (int)sensor_addr);
  	/* Volt sensor test, enable volt low alarm, set min value ridicously
e432dc811   Hans de Goede   hwmon: fix detect...
475
476
477
478
479
480
481
482
483
484
485
486
487
  	   high, or vica versa if the reading is very high. If its a volt
  	   sensor this should always give us an alarm. */
  	if (val <= 240u) {
  		buf[0] = ABIT_UGURU_VOLT_LOW_ALARM_ENABLE;
  		buf[1] = 245;
  		buf[2] = 250;
  		test_flag = ABIT_UGURU_VOLT_LOW_ALARM_FLAG;
  	} else {
  		buf[0] = ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE;
  		buf[1] = 5;
  		buf[2] = 10;
  		test_flag = ABIT_UGURU_VOLT_HIGH_ALARM_FLAG;
  	}
f2b84bbce   Hans de Goede   [PATCH] abituguru...
488
489
  	if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
  			buf, 3) != 3)
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
490
  		goto abituguru_detect_bank1_sensor_type_exit;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
491
492
493
494
495
496
497
  	/* Now we need 20 ms to give the uguru time to read the sensors
  	   and raise a voltage alarm */
  	set_current_state(TASK_UNINTERRUPTIBLE);
  	schedule_timeout(HZ/50);
  	/* Check for alarm and check the alarm is a volt low alarm. */
  	if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
  			ABIT_UGURU_MAX_RETRIES) != 3)
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
498
  		goto abituguru_detect_bank1_sensor_type_exit;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
499
500
501
502
  	if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
  		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
  				sensor_addr, buf, 3,
  				ABIT_UGURU_MAX_RETRIES) != 3)
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
503
  			goto abituguru_detect_bank1_sensor_type_exit;
e432dc811   Hans de Goede   hwmon: fix detect...
504
  		if (buf[0] & test_flag) {
f2b84bbce   Hans de Goede   [PATCH] abituguru...
505
506
  			ABIT_UGURU_DEBUG(2, "  found volt sensor
  ");
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
507
508
  			ret = ABIT_UGURU_IN_SENSOR;
  			goto abituguru_detect_bank1_sensor_type_exit;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
509
510
  		} else
  			ABIT_UGURU_DEBUG(2, "  alarm raised during volt "
e432dc811   Hans de Goede   hwmon: fix detect...
511
512
  				"sensor test, but volt range flag not set
  ");
f2b84bbce   Hans de Goede   [PATCH] abituguru...
513
514
515
516
517
518
519
520
521
522
523
524
525
  	} else
  		ABIT_UGURU_DEBUG(2, "  alarm not raised during volt sensor "
  			"test
  ");
  
  	/* Temp sensor test, enable sensor as a temp sensor, set beep value
  	   ridicously low (but not too low, otherwise uguru ignores it).
  	   If its a temp sensor this should always give us an alarm. */
  	buf[0] = ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE;
  	buf[1] = 5;
  	buf[2] = 10;
  	if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
  			buf, 3) != 3)
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
526
  		goto abituguru_detect_bank1_sensor_type_exit;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
527
528
529
530
531
532
533
  	/* Now we need 50 ms to give the uguru time to read the sensors
  	   and raise a temp alarm */
  	set_current_state(TASK_UNINTERRUPTIBLE);
  	schedule_timeout(HZ/20);
  	/* Check for alarm and check the alarm is a temp high alarm. */
  	if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
  			ABIT_UGURU_MAX_RETRIES) != 3)
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
534
  		goto abituguru_detect_bank1_sensor_type_exit;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
535
536
537
538
  	if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
  		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
  				sensor_addr, buf, 3,
  				ABIT_UGURU_MAX_RETRIES) != 3)
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
539
  			goto abituguru_detect_bank1_sensor_type_exit;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
540
  		if (buf[0] & ABIT_UGURU_TEMP_HIGH_ALARM_FLAG) {
f2b84bbce   Hans de Goede   [PATCH] abituguru...
541
542
  			ABIT_UGURU_DEBUG(2, "  found temp sensor
  ");
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
543
544
  			ret = ABIT_UGURU_TEMP_SENSOR;
  			goto abituguru_detect_bank1_sensor_type_exit;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
545
546
547
548
549
550
551
552
  		} else
  			ABIT_UGURU_DEBUG(2, "  alarm raised during temp "
  				"sensor test, but temp high flag not set
  ");
  	} else
  		ABIT_UGURU_DEBUG(2, "  alarm not raised during temp sensor "
  			"test
  ");
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
553
554
555
556
557
558
559
560
561
562
563
  	ret = ABIT_UGURU_NC;
  abituguru_detect_bank1_sensor_type_exit:
  	/* Restore original settings, failing here is really BAD, it has been
  	   reported that some BIOS-es hang when entering the uGuru menu with
  	   invalid settings present in the uGuru, so we try this 3 times. */
  	for (i = 0; i < 3; i++)
  		if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
  				sensor_addr, data->bank1_settings[sensor_addr],
  				3) == 3)
  			break;
  	if (i == 3) {
28ebfa13f   Joe Perches   hwmon: (abituguru...
564
565
566
  		pr_err("Fatal error could not restore original settings. %s %s
  ",
  		       never_happen, report_this);
a2392e0b9   Hans de Goede   [PATCH] abituguru...
567
  		return -ENODEV;
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
568
  	}
f2b84bbce   Hans de Goede   [PATCH] abituguru...
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
  	return ret;
  }
  
  /* These functions try to find out how many sensors there are in bank2 and how
     many pwms there are. The purpose of this is to make sure that we don't give
     the user the possibility to change settings for non-existent sensors / pwm.
     The uGuru will happily read / write whatever memory happens to be after the
     memory storing the PWM settings when reading/writing to a PWM which is not
     there. Notice even if we detect a PWM which doesn't exist we normally won't
     write to it, unless the user tries to change the settings.
  
     Although the uGuru allows reading (settings) from non existing bank2
     sensors, my version of the uGuru does seem to stop writing to them, the
     write function above aborts in this case with:
     "CMD reg does not hold 0xAC after write"
  
     Notice these 2 tests are non destructive iow read-only tests, otherwise
     they would defeat their purpose. Although for the bank2_sensors detection a
     read/write test would be feasible because of the reaction above, I've
     however opted to stay on the safe side. */
  static void __devinit
  abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
  {
  	int i;
9b2ad1298   Hans de Goede   [PATCH] hwmon: Fi...
593
  	if (fan_sensors > 0 && fan_sensors <= ABIT_UGURU_MAX_BANK2_SENSORS) {
f2b84bbce   Hans de Goede   [PATCH] abituguru...
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
  		data->bank2_sensors = fan_sensors;
  		ABIT_UGURU_DEBUG(2, "assuming %d fan sensors because of "
  			"\"fan_sensors\" module param
  ",
  			(int)data->bank2_sensors);
  		return;
  	}
  
  	ABIT_UGURU_DEBUG(2, "detecting number of fan sensors
  ");
  	for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
  		/* 0x89 are the known used bits:
  		   -0x80 enable shutdown
  		   -0x08 enable beep
  		   -0x01 enable alarm
  		   All other bits should be 0, but on some motherboards
b7c066044   Hans de Goede   [PATCH] abituguru...
610
611
  		   0x40 (bit 6) is also high for some of the fans?? */
  		if (data->bank2_settings[i][0] & ~0xC9) {
f2b84bbce   Hans de Goede   [PATCH] abituguru...
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
  			ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
  				"to be a fan sensor: settings[0] = %02X
  ",
  				i, (unsigned int)data->bank2_settings[i][0]);
  			break;
  		}
  
  		/* check if the threshold is within the allowed range */
  		if (data->bank2_settings[i][1] <
  				abituguru_bank2_min_threshold) {
  			ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
  				"to be a fan sensor: the threshold (%d) is "
  				"below the minimum (%d)
  ", i,
  				(int)data->bank2_settings[i][1],
  				(int)abituguru_bank2_min_threshold);
  			break;
  		}
  		if (data->bank2_settings[i][1] >
  				abituguru_bank2_max_threshold) {
  			ABIT_UGURU_DEBUG(2, "  bank2 sensor %d does not seem "
  				"to be a fan sensor: the threshold (%d) is "
  				"above the maximum (%d)
  ", i,
  				(int)data->bank2_settings[i][1],
  				(int)abituguru_bank2_max_threshold);
  			break;
  		}
  	}
  
  	data->bank2_sensors = i;
  	ABIT_UGURU_DEBUG(2, " found: %d fan sensors
  ",
  		(int)data->bank2_sensors);
  }
  
  static void __devinit
  abituguru_detect_no_pwms(struct abituguru_data *data)
  {
  	int i, j;
9b2ad1298   Hans de Goede   [PATCH] hwmon: Fi...
652
  	if (pwms > 0 && pwms <= ABIT_UGURU_MAX_PWMS) {
f2b84bbce   Hans de Goede   [PATCH] abituguru...
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
  		data->pwms = pwms;
  		ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
  			"\"pwms\" module param
  ", (int)data->pwms);
  		return;
  	}
  
  	ABIT_UGURU_DEBUG(2, "detecting number of PWM outputs
  ");
  	for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
  		/* 0x80 is the enable bit and the low
  		   nibble is which temp sensor to use,
  		   the other bits should be 0 */
  		if (data->pwm_settings[i][0] & ~0x8F) {
  			ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
  				"to be a pwm channel: settings[0] = %02X
  ",
  				i, (unsigned int)data->pwm_settings[i][0]);
  			break;
  		}
  
  		/* the low nibble must correspond to one of the temp sensors
  		   we've found */
  		for (j = 0; j < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR];
  				j++) {
  			if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][j] ==
  					(data->pwm_settings[i][0] & 0x0F))
  				break;
  		}
  		if (j == data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
  			ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
  				"to be a pwm channel: %d is not a valid temp "
  				"sensor address
  ", i,
  				data->pwm_settings[i][0] & 0x0F);
  			break;
  		}
  
  		/* check if all other settings are within the allowed range */
  		for (j = 1; j < 5; j++) {
  			u8 min;
  			/* special case pwm1 min pwm% */
  			if ((i == 0) && ((j == 1) || (j == 2)))
  				min = 77;
  			else
  				min = abituguru_pwm_min[j];
  			if (data->pwm_settings[i][j] < min) {
  				ABIT_UGURU_DEBUG(2, "  pwm channel %d does "
  					"not seem to be a pwm channel: "
  					"setting %d (%d) is below the minimum "
  					"value (%d)
  ", i, j,
  					(int)data->pwm_settings[i][j],
  					(int)min);
  				goto abituguru_detect_no_pwms_exit;
  			}
  			if (data->pwm_settings[i][j] > abituguru_pwm_max[j]) {
  				ABIT_UGURU_DEBUG(2, "  pwm channel %d does "
  					"not seem to be a pwm channel: "
  					"setting %d (%d) is above the maximum "
  					"value (%d)
  ", i, j,
  					(int)data->pwm_settings[i][j],
  					(int)abituguru_pwm_max[j]);
  				goto abituguru_detect_no_pwms_exit;
  			}
  		}
  
  		/* check that min temp < max temp and min pwm < max pwm */
  		if (data->pwm_settings[i][1] >= data->pwm_settings[i][2]) {
  			ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
  				"to be a pwm channel: min pwm (%d) >= "
  				"max pwm (%d)
  ", i,
  				(int)data->pwm_settings[i][1],
  				(int)data->pwm_settings[i][2]);
  			break;
  		}
  		if (data->pwm_settings[i][3] >= data->pwm_settings[i][4]) {
  			ABIT_UGURU_DEBUG(2, "  pwm channel %d does not seem "
  				"to be a pwm channel: min temp (%d) >= "
  				"max temp (%d)
  ", i,
  				(int)data->pwm_settings[i][3],
  				(int)data->pwm_settings[i][4]);
  			break;
  		}
  	}
  
  abituguru_detect_no_pwms_exit:
  	data->pwms = i;
  	ABIT_UGURU_DEBUG(2, " found: %d PWM outputs
  ", (int)data->pwms);
  }
  
  /* Following are the sysfs callback functions. These functions expect:
     sensor_device_attribute_2->index:   sensor address/offset in the bank
     sensor_device_attribute_2->nr:      register offset, bitmask or NA. */
  static struct abituguru_data *abituguru_update_device(struct device *dev);
  
  static ssize_t show_bank1_value(struct device *dev,
  	struct device_attribute *devattr, char *buf)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = abituguru_update_device(dev);
  	if (!data)
  		return -EIO;
  	return sprintf(buf, "%d
  ", (data->bank1_value[attr->index] *
  		data->bank1_max_value[attr->index] + 128) / 255);
  }
  
  static ssize_t show_bank1_setting(struct device *dev,
  	struct device_attribute *devattr, char *buf)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = dev_get_drvdata(dev);
  	return sprintf(buf, "%d
  ",
  		(data->bank1_settings[attr->index][attr->nr] *
  		data->bank1_max_value[attr->index] + 128) / 255);
  }
  
  static ssize_t show_bank2_value(struct device *dev,
  	struct device_attribute *devattr, char *buf)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = abituguru_update_device(dev);
  	if (!data)
  		return -EIO;
  	return sprintf(buf, "%d
  ", (data->bank2_value[attr->index] *
  		ABIT_UGURU_FAN_MAX + 128) / 255);
  }
  
  static ssize_t show_bank2_setting(struct device *dev,
  	struct device_attribute *devattr, char *buf)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = dev_get_drvdata(dev);
  	return sprintf(buf, "%d
  ",
  		(data->bank2_settings[attr->index][attr->nr] *
  		ABIT_UGURU_FAN_MAX + 128) / 255);
  }
  
  static ssize_t store_bank1_setting(struct device *dev, struct device_attribute
  	*devattr, const char *buf, size_t count)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = dev_get_drvdata(dev);
  	u8 val = (simple_strtoul(buf, NULL, 10) * 255 +
  		data->bank1_max_value[attr->index]/2) /
  		data->bank1_max_value[attr->index];
  	ssize_t ret = count;
  
  	mutex_lock(&data->update_lock);
  	if (data->bank1_settings[attr->index][attr->nr] != val) {
  		u8 orig_val = data->bank1_settings[attr->index][attr->nr];
  		data->bank1_settings[attr->index][attr->nr] = val;
  		if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
  				attr->index, data->bank1_settings[attr->index],
  				3) <= attr->nr) {
  			data->bank1_settings[attr->index][attr->nr] = orig_val;
  			ret = -EIO;
  		}
  	}
  	mutex_unlock(&data->update_lock);
  	return ret;
  }
  
  static ssize_t store_bank2_setting(struct device *dev, struct device_attribute
  	*devattr, const char *buf, size_t count)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = dev_get_drvdata(dev);
  	u8 val = (simple_strtoul(buf, NULL, 10)*255 + ABIT_UGURU_FAN_MAX/2) /
  		ABIT_UGURU_FAN_MAX;
  	ssize_t ret = count;
  
  	/* this check can be done before taking the lock */
  	if ((val < abituguru_bank2_min_threshold) ||
  			(val > abituguru_bank2_max_threshold))
  		return -EINVAL;
  
  	mutex_lock(&data->update_lock);
  	if (data->bank2_settings[attr->index][attr->nr] != val) {
  		u8 orig_val = data->bank2_settings[attr->index][attr->nr];
  		data->bank2_settings[attr->index][attr->nr] = val;
  		if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK2 + 2,
  				attr->index, data->bank2_settings[attr->index],
  				2) <= attr->nr) {
  			data->bank2_settings[attr->index][attr->nr] = orig_val;
  			ret = -EIO;
  		}
  	}
  	mutex_unlock(&data->update_lock);
  	return ret;
  }
  
  static ssize_t show_bank1_alarm(struct device *dev,
  	struct device_attribute *devattr, char *buf)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = abituguru_update_device(dev);
  	if (!data)
  		return -EIO;
  	/* See if the alarm bit for this sensor is set, and if the
  	   alarm matches the type of alarm we're looking for (for volt
  	   it can be either low or high). The type is stored in a few
  	   readonly bits in the settings part of the relevant sensor.
  	   The bitmask of the type is passed to us in attr->nr. */
  	if ((data->alarms[attr->index / 8] & (0x01 << (attr->index % 8))) &&
  			(data->bank1_settings[attr->index][0] & attr->nr))
  		return sprintf(buf, "1
  ");
  	else
  		return sprintf(buf, "0
  ");
  }
  
  static ssize_t show_bank2_alarm(struct device *dev,
  	struct device_attribute *devattr, char *buf)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = abituguru_update_device(dev);
  	if (!data)
  		return -EIO;
  	if (data->alarms[2] & (0x01 << attr->index))
  		return sprintf(buf, "1
  ");
  	else
  		return sprintf(buf, "0
  ");
  }
  
  static ssize_t show_bank1_mask(struct device *dev,
  	struct device_attribute *devattr, char *buf)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = dev_get_drvdata(dev);
  	if (data->bank1_settings[attr->index][0] & attr->nr)
  		return sprintf(buf, "1
  ");
  	else
  		return sprintf(buf, "0
  ");
  }
  
  static ssize_t show_bank2_mask(struct device *dev,
  	struct device_attribute *devattr, char *buf)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = dev_get_drvdata(dev);
  	if (data->bank2_settings[attr->index][0] & attr->nr)
  		return sprintf(buf, "1
  ");
  	else
  		return sprintf(buf, "0
  ");
  }
  
  static ssize_t store_bank1_mask(struct device *dev,
  	struct device_attribute *devattr, const char *buf, size_t count)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = dev_get_drvdata(dev);
  	int mask = simple_strtoul(buf, NULL, 10);
  	ssize_t ret = count;
  	u8 orig_val;
  
  	mutex_lock(&data->update_lock);
  	orig_val = data->bank1_settings[attr->index][0];
  
  	if (mask)
  		data->bank1_settings[attr->index][0] |= attr->nr;
  	else
  		data->bank1_settings[attr->index][0] &= ~attr->nr;
  
  	if ((data->bank1_settings[attr->index][0] != orig_val) &&
  			(abituguru_write(data,
  			ABIT_UGURU_SENSOR_BANK1 + 2, attr->index,
  			data->bank1_settings[attr->index], 3) < 1)) {
  		data->bank1_settings[attr->index][0] = orig_val;
  		ret = -EIO;
  	}
  	mutex_unlock(&data->update_lock);
  	return ret;
  }
  
  static ssize_t store_bank2_mask(struct device *dev,
  	struct device_attribute *devattr, const char *buf, size_t count)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = dev_get_drvdata(dev);
  	int mask = simple_strtoul(buf, NULL, 10);
  	ssize_t ret = count;
  	u8 orig_val;
  
  	mutex_lock(&data->update_lock);
  	orig_val = data->bank2_settings[attr->index][0];
  
  	if (mask)
  		data->bank2_settings[attr->index][0] |= attr->nr;
  	else
  		data->bank2_settings[attr->index][0] &= ~attr->nr;
  
  	if ((data->bank2_settings[attr->index][0] != orig_val) &&
  			(abituguru_write(data,
  			ABIT_UGURU_SENSOR_BANK2 + 2, attr->index,
  			data->bank2_settings[attr->index], 2) < 1)) {
  		data->bank2_settings[attr->index][0] = orig_val;
  		ret = -EIO;
  	}
  	mutex_unlock(&data->update_lock);
  	return ret;
  }
  
  /* Fan PWM (speed control) */
  static ssize_t show_pwm_setting(struct device *dev,
  	struct device_attribute *devattr, char *buf)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = dev_get_drvdata(dev);
  	return sprintf(buf, "%d
  ", data->pwm_settings[attr->index][attr->nr] *
  		abituguru_pwm_settings_multiplier[attr->nr]);
  }
  
  static ssize_t store_pwm_setting(struct device *dev, struct device_attribute
  	*devattr, const char *buf, size_t count)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = dev_get_drvdata(dev);
  	u8 min, val = (simple_strtoul(buf, NULL, 10) +
  		abituguru_pwm_settings_multiplier[attr->nr]/2) /
  		abituguru_pwm_settings_multiplier[attr->nr];
  	ssize_t ret = count;
  
  	/* special case pwm1 min pwm% */
  	if ((attr->index == 0) && ((attr->nr == 1) || (attr->nr == 2)))
  		min = 77;
  	else
  		min = abituguru_pwm_min[attr->nr];
  
  	/* this check can be done before taking the lock */
  	if ((val < min) || (val > abituguru_pwm_max[attr->nr]))
  		return -EINVAL;
  
  	mutex_lock(&data->update_lock);
  	/* this check needs to be done after taking the lock */
  	if ((attr->nr & 1) &&
  			(val >= data->pwm_settings[attr->index][attr->nr + 1]))
  		ret = -EINVAL;
  	else if (!(attr->nr & 1) &&
  			(val <= data->pwm_settings[attr->index][attr->nr - 1]))
  		ret = -EINVAL;
  	else if (data->pwm_settings[attr->index][attr->nr] != val) {
  		u8 orig_val = data->pwm_settings[attr->index][attr->nr];
  		data->pwm_settings[attr->index][attr->nr] = val;
  		if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
  				attr->index, data->pwm_settings[attr->index],
  				5) <= attr->nr) {
  			data->pwm_settings[attr->index][attr->nr] =
  				orig_val;
  			ret = -EIO;
  		}
  	}
  	mutex_unlock(&data->update_lock);
  	return ret;
  }
  
  static ssize_t show_pwm_sensor(struct device *dev,
  	struct device_attribute *devattr, char *buf)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = dev_get_drvdata(dev);
  	int i;
  	/* We need to walk to the temp sensor addresses to find what
  	   the userspace id of the configured temp sensor is. */
  	for (i = 0; i < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]; i++)
  		if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][i] ==
  				(data->pwm_settings[attr->index][0] & 0x0F))
  			return sprintf(buf, "%d
  ", i+1);
  
  	return -ENXIO;
  }
  
  static ssize_t store_pwm_sensor(struct device *dev, struct device_attribute
  	*devattr, const char *buf, size_t count)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = dev_get_drvdata(dev);
  	unsigned long val = simple_strtoul(buf, NULL, 10) - 1;
  	ssize_t ret = count;
  
  	mutex_lock(&data->update_lock);
  	if (val < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
  		u8 orig_val = data->pwm_settings[attr->index][0];
  		u8 address = data->bank1_address[ABIT_UGURU_TEMP_SENSOR][val];
  		data->pwm_settings[attr->index][0] &= 0xF0;
  		data->pwm_settings[attr->index][0] |= address;
  		if (data->pwm_settings[attr->index][0] != orig_val) {
  			if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
  					attr->index,
  					data->pwm_settings[attr->index],
  					5) < 1) {
  				data->pwm_settings[attr->index][0] = orig_val;
  				ret = -EIO;
  			}
  		}
  	}
  	else
  		ret = -EINVAL;
  	mutex_unlock(&data->update_lock);
  	return ret;
  }
  
  static ssize_t show_pwm_enable(struct device *dev,
  	struct device_attribute *devattr, char *buf)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = dev_get_drvdata(dev);
  	int res = 0;
  	if (data->pwm_settings[attr->index][0] & ABIT_UGURU_FAN_PWM_ENABLE)
  		res = 2;
  	return sprintf(buf, "%d
  ", res);
  }
  
  static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
  	*devattr, const char *buf, size_t count)
  {
  	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  	struct abituguru_data *data = dev_get_drvdata(dev);
  	u8 orig_val, user_val = simple_strtoul(buf, NULL, 10);
  	ssize_t ret = count;
  
  	mutex_lock(&data->update_lock);
  	orig_val = data->pwm_settings[attr->index][0];
  	switch (user_val) {
  		case 0:
  			data->pwm_settings[attr->index][0] &=
  				~ABIT_UGURU_FAN_PWM_ENABLE;
  			break;
  		case 2:
  			data->pwm_settings[attr->index][0] |=
  				ABIT_UGURU_FAN_PWM_ENABLE;
  			break;
  		default:
  			ret = -EINVAL;
  	}
  	if ((data->pwm_settings[attr->index][0] != orig_val) &&
  			(abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
  			attr->index, data->pwm_settings[attr->index],
  			5) < 1)) {
  		data->pwm_settings[attr->index][0] = orig_val;
  		ret = -EIO;
  	}
  	mutex_unlock(&data->update_lock);
  	return ret;
  }
  
  static ssize_t show_name(struct device *dev,
  	struct device_attribute *devattr, char *buf)
  {
  	return sprintf(buf, "%s
  ", ABIT_UGURU_NAME);
  }
  
  /* Sysfs attr templates, the real entries are generated automatically. */
  static const
  struct sensor_device_attribute_2 abituguru_sysfs_bank1_templ[2][9] = {
  	{
  	SENSOR_ATTR_2(in%d_input, 0444, show_bank1_value, NULL, 0, 0),
  	SENSOR_ATTR_2(in%d_min, 0644, show_bank1_setting,
  		store_bank1_setting, 1, 0),
  	SENSOR_ATTR_2(in%d_min_alarm, 0444, show_bank1_alarm, NULL,
  		ABIT_UGURU_VOLT_LOW_ALARM_FLAG, 0),
  	SENSOR_ATTR_2(in%d_max, 0644, show_bank1_setting,
  		store_bank1_setting, 2, 0),
  	SENSOR_ATTR_2(in%d_max_alarm, 0444, show_bank1_alarm, NULL,
  		ABIT_UGURU_VOLT_HIGH_ALARM_FLAG, 0),
  	SENSOR_ATTR_2(in%d_beep, 0644, show_bank1_mask,
  		store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
  	SENSOR_ATTR_2(in%d_shutdown, 0644, show_bank1_mask,
  		store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
  	SENSOR_ATTR_2(in%d_min_alarm_enable, 0644, show_bank1_mask,
  		store_bank1_mask, ABIT_UGURU_VOLT_LOW_ALARM_ENABLE, 0),
  	SENSOR_ATTR_2(in%d_max_alarm_enable, 0644, show_bank1_mask,
  		store_bank1_mask, ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE, 0),
  	}, {
  	SENSOR_ATTR_2(temp%d_input, 0444, show_bank1_value, NULL, 0, 0),
  	SENSOR_ATTR_2(temp%d_alarm, 0444, show_bank1_alarm, NULL,
  		ABIT_UGURU_TEMP_HIGH_ALARM_FLAG, 0),
  	SENSOR_ATTR_2(temp%d_max, 0644, show_bank1_setting,
  		store_bank1_setting, 1, 0),
  	SENSOR_ATTR_2(temp%d_crit, 0644, show_bank1_setting,
  		store_bank1_setting, 2, 0),
  	SENSOR_ATTR_2(temp%d_beep, 0644, show_bank1_mask,
  		store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
  	SENSOR_ATTR_2(temp%d_shutdown, 0644, show_bank1_mask,
  		store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
  	SENSOR_ATTR_2(temp%d_alarm_enable, 0644, show_bank1_mask,
  		store_bank1_mask, ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE, 0),
  	}
  };
  
  static const struct sensor_device_attribute_2 abituguru_sysfs_fan_templ[6] = {
  	SENSOR_ATTR_2(fan%d_input, 0444, show_bank2_value, NULL, 0, 0),
  	SENSOR_ATTR_2(fan%d_alarm, 0444, show_bank2_alarm, NULL, 0, 0),
  	SENSOR_ATTR_2(fan%d_min, 0644, show_bank2_setting,
  		store_bank2_setting, 1, 0),
  	SENSOR_ATTR_2(fan%d_beep, 0644, show_bank2_mask,
  		store_bank2_mask, ABIT_UGURU_BEEP_ENABLE, 0),
  	SENSOR_ATTR_2(fan%d_shutdown, 0644, show_bank2_mask,
  		store_bank2_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
  	SENSOR_ATTR_2(fan%d_alarm_enable, 0644, show_bank2_mask,
  		store_bank2_mask, ABIT_UGURU_FAN_LOW_ALARM_ENABLE, 0),
  };
  
  static const struct sensor_device_attribute_2 abituguru_sysfs_pwm_templ[6] = {
  	SENSOR_ATTR_2(pwm%d_enable, 0644, show_pwm_enable,
  		store_pwm_enable, 0, 0),
  	SENSOR_ATTR_2(pwm%d_auto_channels_temp, 0644, show_pwm_sensor,
  		store_pwm_sensor, 0, 0),
  	SENSOR_ATTR_2(pwm%d_auto_point1_pwm, 0644, show_pwm_setting,
  		store_pwm_setting, 1, 0),
  	SENSOR_ATTR_2(pwm%d_auto_point2_pwm, 0644, show_pwm_setting,
  		store_pwm_setting, 2, 0),
  	SENSOR_ATTR_2(pwm%d_auto_point1_temp, 0644, show_pwm_setting,
  		store_pwm_setting, 3, 0),
  	SENSOR_ATTR_2(pwm%d_auto_point2_temp, 0644, show_pwm_setting,
  		store_pwm_setting, 4, 0),
  };
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1189
  static struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1190
1191
1192
1193
1194
1195
  	SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
  };
  
  static int __devinit abituguru_probe(struct platform_device *pdev)
  {
  	struct abituguru_data *data;
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1196
  	int i, j, used, sysfs_names_free, sysfs_attr_i, res = -ENODEV;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1197
  	char *sysfs_filename;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1198
1199
1200
  
  	/* El weirdo probe order, to keep the sysfs order identical to the
  	   BIOS and window-appliction listing order. */
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1201
1202
1203
  	const u8 probe_order[ABIT_UGURU_MAX_BANK1_SENSORS] = {
  		0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E, 0x02,
  		0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
  
  	if (!(data = kzalloc(sizeof(struct abituguru_data), GFP_KERNEL)))
  		return -ENOMEM;
  
  	data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
  	mutex_init(&data->update_lock);
  	platform_set_drvdata(pdev, data);
  
  	/* See if the uGuru is ready */
  	if (inb_p(data->addr + ABIT_UGURU_DATA) == ABIT_UGURU_STATUS_INPUT)
  		data->uguru_ready = 1;
  
  	/* Completely read the uGuru this has 2 purposes:
  	   - testread / see if one really is there.
  	   - make an in memory copy of all the uguru settings for future use. */
  	if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1220
1221
  			data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3)
  		goto abituguru_probe_error;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1222

a2392e0b9   Hans de Goede   [PATCH] abituguru...
1223
  	for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1224
1225
  		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, i,
  				&data->bank1_value[i], 1,
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1226
1227
  				ABIT_UGURU_MAX_RETRIES) != 1)
  			goto abituguru_probe_error;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1228
1229
  		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1+1, i,
  				data->bank1_settings[i], 3,
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1230
1231
  				ABIT_UGURU_MAX_RETRIES) != 3)
  			goto abituguru_probe_error;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1232
1233
1234
1235
1236
1237
1238
1239
1240
  	}
  	/* Note: We don't know how many bank2 sensors / pwms there really are,
  	   but in order to "detect" this we need to read the maximum amount
  	   anyways. If we read sensors/pwms not there we'll just read crap
  	   this can't hurt. We need the detection because we don't want
  	   unwanted writes, which will hurt! */
  	for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
  		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
  				&data->bank2_value[i], 1,
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1241
1242
  				ABIT_UGURU_MAX_RETRIES) != 1)
  			goto abituguru_probe_error;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1243
1244
  		if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2+1, i,
  				data->bank2_settings[i], 2,
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1245
1246
  				ABIT_UGURU_MAX_RETRIES) != 2)
  			goto abituguru_probe_error;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1247
1248
1249
1250
  	}
  	for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
  		if (abituguru_read(data, ABIT_UGURU_FAN_PWM, i,
  				data->pwm_settings[i], 5,
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1251
1252
  				ABIT_UGURU_MAX_RETRIES) != 5)
  			goto abituguru_probe_error;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1253
1254
1255
1256
  	}
  	data->last_updated = jiffies;
  
  	/* Detect sensor types and fill the sysfs attr for bank1 */
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1257
1258
1259
1260
  	sysfs_attr_i = 0;
  	sysfs_filename = data->sysfs_names;
  	sysfs_names_free = ABITUGURU_SYSFS_NAMES_LENGTH;
  	for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1261
  		res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1262
1263
  		if (res < 0)
  			goto abituguru_probe_error;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1264
1265
  		if (res == ABIT_UGURU_NC)
  			continue;
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1266
  		/* res 1 (temp) sensors have 7 sysfs entries, 0 (in) 9 */
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1267
  		for (j = 0; j < (res ? 7 : 9); j++) {
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1268
1269
1270
1271
  			used = snprintf(sysfs_filename, sysfs_names_free,
  				abituguru_sysfs_bank1_templ[res][j].dev_attr.
  				attr.name, data->bank1_sensors[res] + res)
  				+ 1;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1272
1273
1274
1275
  			data->sysfs_attr[sysfs_attr_i] =
  				abituguru_sysfs_bank1_templ[res][j];
  			data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
  				sysfs_filename;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1276
  			data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1277
1278
  			sysfs_filename += used;
  			sysfs_names_free -= used;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
  			sysfs_attr_i++;
  		}
  		data->bank1_max_value[probe_order[i]] =
  			abituguru_bank1_max_value[res];
  		data->bank1_address[res][data->bank1_sensors[res]] =
  			probe_order[i];
  		data->bank1_sensors[res]++;
  	}
  	/* Detect number of sensors and fill the sysfs attr for bank2 (fans) */
  	abituguru_detect_no_bank2_sensors(data);
  	for (i = 0; i < data->bank2_sensors; i++) {
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1290
1291
1292
1293
  		for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_fan_templ); j++) {
  			used = snprintf(sysfs_filename, sysfs_names_free,
  				abituguru_sysfs_fan_templ[j].dev_attr.attr.name,
  				i + 1) + 1;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1294
1295
1296
1297
  			data->sysfs_attr[sysfs_attr_i] =
  				abituguru_sysfs_fan_templ[j];
  			data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
  				sysfs_filename;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1298
  			data->sysfs_attr[sysfs_attr_i].index = i;
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1299
1300
  			sysfs_filename += used;
  			sysfs_names_free -= used;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1301
1302
1303
1304
1305
1306
  			sysfs_attr_i++;
  		}
  	}
  	/* Detect number of sensors and fill the sysfs attr for pwms */
  	abituguru_detect_no_pwms(data);
  	for (i = 0; i < data->pwms; i++) {
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1307
1308
1309
1310
  		for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_pwm_templ); j++) {
  			used = snprintf(sysfs_filename, sysfs_names_free,
  				abituguru_sysfs_pwm_templ[j].dev_attr.attr.name,
  				i + 1) + 1;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1311
1312
1313
1314
  			data->sysfs_attr[sysfs_attr_i] =
  				abituguru_sysfs_pwm_templ[j];
  			data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
  				sysfs_filename;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1315
  			data->sysfs_attr[sysfs_attr_i].index = i;
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1316
1317
  			sysfs_filename += used;
  			sysfs_names_free -= used;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1318
1319
1320
  			sysfs_attr_i++;
  		}
  	}
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1321
1322
  	/* Fail safe check, this should never happen! */
  	if (sysfs_names_free < 0) {
28ebfa13f   Joe Perches   hwmon: (abituguru...
1323
1324
  		pr_err("Fatal error ran out of space for sysfs attr names. %s %s",
  		       never_happen, report_this);
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1325
1326
  		res = -ENAMETOOLONG;
  		goto abituguru_probe_error;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1327
  	}
28ebfa13f   Joe Perches   hwmon: (abituguru...
1328
1329
  	pr_info("found Abit uGuru
  ");
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1330
1331
  
  	/* Register sysfs hooks */
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1332
  	for (i = 0; i < sysfs_attr_i; i++)
bc8f0a268   Hans de Goede   hwmon/abituguru: ...
1333
1334
1335
  		if (device_create_file(&pdev->dev,
  				&data->sysfs_attr[i].dev_attr))
  			goto abituguru_probe_error;
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1336
  	for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
bc8f0a268   Hans de Goede   hwmon/abituguru: ...
1337
1338
1339
  		if (device_create_file(&pdev->dev,
  				&abituguru_sysfs_attr[i].dev_attr))
  			goto abituguru_probe_error;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1340

1beeffe43   Tony Jones   hwmon: Convert fr...
1341
1342
  	data->hwmon_dev = hwmon_device_register(&pdev->dev);
  	if (!IS_ERR(data->hwmon_dev))
bc8f0a268   Hans de Goede   hwmon/abituguru: ...
1343
  		return 0; /* success */
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1344

1beeffe43   Tony Jones   hwmon: Convert fr...
1345
  	res = PTR_ERR(data->hwmon_dev);
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1346
  abituguru_probe_error:
bc8f0a268   Hans de Goede   hwmon/abituguru: ...
1347
1348
1349
1350
1351
  	for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
  		device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
  	for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
  		device_remove_file(&pdev->dev,
  			&abituguru_sysfs_attr[i].dev_attr);
04a6217df   Jean Delvare   hwmon: Fix a pote...
1352
  	platform_set_drvdata(pdev, NULL);
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1353
1354
  	kfree(data);
  	return res;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1355
1356
1357
1358
  }
  
  static int __devexit abituguru_remove(struct platform_device *pdev)
  {
bc8f0a268   Hans de Goede   hwmon/abituguru: ...
1359
  	int i;
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1360
  	struct abituguru_data *data = platform_get_drvdata(pdev);
1beeffe43   Tony Jones   hwmon: Convert fr...
1361
  	hwmon_device_unregister(data->hwmon_dev);
bc8f0a268   Hans de Goede   hwmon/abituguru: ...
1362
1363
1364
1365
1366
  	for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
  		device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
  	for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
  		device_remove_file(&pdev->dev,
  			&abituguru_sysfs_attr[i].dev_attr);
04a6217df   Jean Delvare   hwmon: Fix a pote...
1367
  	platform_set_drvdata(pdev, NULL);
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
  	kfree(data);
  
  	return 0;
  }
  
  static struct abituguru_data *abituguru_update_device(struct device *dev)
  {
  	int i, err;
  	struct abituguru_data *data = dev_get_drvdata(dev);
  	/* fake a complete successful read if no update necessary. */
  	char success = 1;
  
  	mutex_lock(&data->update_lock);
  	if (time_after(jiffies, data->last_updated + HZ)) {
  		success = 0;
  		if ((err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
  				data->alarms, 3, 0)) != 3)
  			goto LEAVE_UPDATE;
a2392e0b9   Hans de Goede   [PATCH] abituguru...
1386
  		for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
  			if ((err = abituguru_read(data,
  					ABIT_UGURU_SENSOR_BANK1, i,
  					&data->bank1_value[i], 1, 0)) != 1)
  				goto LEAVE_UPDATE;
  			if ((err = abituguru_read(data,
  					ABIT_UGURU_SENSOR_BANK1 + 1, i,
  					data->bank1_settings[i], 3, 0)) != 3)
  				goto LEAVE_UPDATE;
  		}
  		for (i = 0; i < data->bank2_sensors; i++)
  			if ((err = abituguru_read(data,
  					ABIT_UGURU_SENSOR_BANK2, i,
  					&data->bank2_value[i], 1, 0)) != 1)
  				goto LEAVE_UPDATE;
  		/* success! */
  		success = 1;
  		data->update_timeouts = 0;
  LEAVE_UPDATE:
  		/* handle timeout condition */
faf9b6163   Hans de Goede   [PATCH] hwmon: ab...
1406
  		if (!success && (err == -EBUSY || err >= 0)) {
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
  			/* No overflow please */
  			if (data->update_timeouts < 255u)
  				data->update_timeouts++;
  			if (data->update_timeouts <= ABIT_UGURU_MAX_TIMEOUTS) {
  				ABIT_UGURU_DEBUG(3, "timeout exceeded, will "
  					"try again next update
  ");
  				/* Just a timeout, fake a successful read */
  				success = 1;
  			} else
  				ABIT_UGURU_DEBUG(1, "timeout exceeded %d "
  					"times waiting for more input state
  ",
  					(int)data->update_timeouts);
  		}
  		/* On success set last_updated */
  		if (success)
  			data->last_updated = jiffies;
  	}
  	mutex_unlock(&data->update_lock);
  
  	if (success)
  		return data;
  	else
  		return NULL;
  }
360b9ab22   Hans de Goede   abituguru: Add su...
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
  #ifdef CONFIG_PM
  static int abituguru_suspend(struct platform_device *pdev, pm_message_t state)
  {
  	struct abituguru_data *data = platform_get_drvdata(pdev);
  	/* make sure all communications with the uguru are done and no new
  	   ones are started */
  	mutex_lock(&data->update_lock);
  	return 0;
  }
  
  static int abituguru_resume(struct platform_device *pdev)
  {
  	struct abituguru_data *data = platform_get_drvdata(pdev);
  	/* See if the uGuru is still ready */
  	if (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT)
  		data->uguru_ready = 0;
  	mutex_unlock(&data->update_lock);
  	return 0;
  }
  #else
  #define abituguru_suspend	NULL
  #define abituguru_resume	NULL
  #endif /* CONFIG_PM */
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1456
1457
1458
1459
1460
  static struct platform_driver abituguru_driver = {
  	.driver = {
  		.owner	= THIS_MODULE,
  		.name	= ABIT_UGURU_NAME,
  	},
360b9ab22   Hans de Goede   abituguru: Add su...
1461
1462
1463
1464
  	.probe		= abituguru_probe,
  	.remove		= __devexit_p(abituguru_remove),
  	.suspend	= abituguru_suspend,
  	.resume		= abituguru_resume,
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1465
1466
1467
1468
1469
1470
1471
1472
  };
  
  static int __init abituguru_detect(void)
  {
  	/* See if there is an uguru there. After a reboot uGuru will hold 0x00
  	   at DATA and 0xAC, when this driver has already been loaded once
  	   DATA will hold 0x08. For most uGuru's CMD will hold 0xAC in either
  	   scenario but some will hold 0x00.
25985edce   Lucas De Marchi   Fix common misspe...
1473
  	   Some uGuru's initially hold 0x09 at DATA and will only hold 0x08
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
  	   after reading CMD first, so CMD must be read first! */
  	u8 cmd_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_CMD);
  	u8 data_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_DATA);
  	if (((data_val == 0x00) || (data_val == 0x08)) &&
  	    ((cmd_val == 0x00) || (cmd_val == 0xAC)))
  		return ABIT_UGURU_BASE;
  
  	ABIT_UGURU_DEBUG(2, "no Abit uGuru found, data = 0x%02X, cmd = "
  		"0x%02X
  ", (unsigned int)data_val, (unsigned int)cmd_val);
  
  	if (force) {
28ebfa13f   Joe Perches   hwmon: (abituguru...
1486
1487
  		pr_info("Assuming Abit uGuru is present because of \"force\" parameter
  ");
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
  		return ABIT_UGURU_BASE;
  	}
  
  	/* No uGuru found */
  	return -ENODEV;
  }
  
  static struct platform_device *abituguru_pdev;
  
  static int __init abituguru_init(void)
  {
  	int address, err;
  	struct resource res = { .flags = IORESOURCE_IO };
1855256c4   Jeff Garzik   drivers/firmware:...
1501
  	const char *board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
c182f5bbf   Hans de Goede   hwmon: refuse to ...
1502
1503
1504
1505
1506
  
  	/* safety check, refuse to load on non Abit motherboards */
  	if (!force && (!board_vendor ||
  			strcmp(board_vendor, "http://www.abit.com.tw/")))
  		return -ENODEV;
c182f5bbf   Hans de Goede   hwmon: refuse to ...
1507

f2b84bbce   Hans de Goede   [PATCH] abituguru...
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
  	address = abituguru_detect();
  	if (address < 0)
  		return address;
  
  	err = platform_driver_register(&abituguru_driver);
  	if (err)
  		goto exit;
  
  	abituguru_pdev = platform_device_alloc(ABIT_UGURU_NAME, address);
  	if (!abituguru_pdev) {
28ebfa13f   Joe Perches   hwmon: (abituguru...
1518
1519
  		pr_err("Device allocation failed
  ");
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
  		err = -ENOMEM;
  		goto exit_driver_unregister;
  	}
  
  	res.start = address;
  	res.end = address + ABIT_UGURU_REGION_LENGTH - 1;
  	res.name = ABIT_UGURU_NAME;
  
  	err = platform_device_add_resources(abituguru_pdev, &res, 1);
  	if (err) {
28ebfa13f   Joe Perches   hwmon: (abituguru...
1530
1531
  		pr_err("Device resource addition failed (%d)
  ", err);
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1532
1533
1534
1535
1536
  		goto exit_device_put;
  	}
  
  	err = platform_device_add(abituguru_pdev);
  	if (err) {
28ebfa13f   Joe Perches   hwmon: (abituguru...
1537
1538
  		pr_err("Device addition failed (%d)
  ", err);
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
  		goto exit_device_put;
  	}
  
  	return 0;
  
  exit_device_put:
  	platform_device_put(abituguru_pdev);
  exit_driver_unregister:
  	platform_driver_unregister(&abituguru_driver);
  exit:
  	return err;
  }
  
  static void __exit abituguru_exit(void)
  {
  	platform_device_unregister(abituguru_pdev);
  	platform_driver_unregister(&abituguru_driver);
  }
93d0cc588   Hans de Goede   hwmon: (abituguru...
1557
  MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
f2b84bbce   Hans de Goede   [PATCH] abituguru...
1558
1559
1560
1561
1562
  MODULE_DESCRIPTION("Abit uGuru Sensor device");
  MODULE_LICENSE("GPL");
  
  module_init(abituguru_init);
  module_exit(abituguru_exit);