blob: 9c8a6bab822866ccab860a04fd276979bd69a115 [file] [log] [blame]
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001/*
Guenter Roeck1bd385d2012-01-14 12:30:52 -08002 * abituguru.c Copyright (c) 2005-2006 Hans de Goede <hdegoede@redhat.com>
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 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +020018/*
Guenter Roeck1bd385d2012-01-14 12:30:52 -080019 * 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!
22 */
Joe Perches28ebfa12010-10-20 06:51:25 +000023
24#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
Hans de Goedef2b84bb2006-06-04 20:22:24 +020026#include <linux/module.h>
Al Virof6a57032006-10-18 01:47:25 -040027#include <linux/sched.h>
Hans de Goedef2b84bb2006-06-04 20:22:24 +020028#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/jiffies.h>
31#include <linux/mutex.h>
32#include <linux/err.h>
Hans de Goedefaf9b612006-08-25 10:24:20 +020033#include <linux/delay.h>
Hans de Goedef2b84bb2006-06-04 20:22:24 +020034#include <linux/platform_device.h>
35#include <linux/hwmon.h>
36#include <linux/hwmon-sysfs.h>
Hans de Goedec182f5b2007-07-10 17:09:57 +020037#include <linux/dmi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020038#include <linux/io.h>
Hans de Goedef2b84bb2006-06-04 20:22:24 +020039
40/* Banks */
41#define ABIT_UGURU_ALARM_BANK 0x20 /* 1x 3 bytes */
42#define ABIT_UGURU_SENSOR_BANK1 0x21 /* 16x volt and temp */
43#define ABIT_UGURU_FAN_PWM 0x24 /* 3x 5 bytes */
44#define ABIT_UGURU_SENSOR_BANK2 0x26 /* fans */
Hans de Goedea2392e02006-06-04 20:23:01 +020045/* max nr of sensors in bank1, a bank1 sensor can be in, temp or nc */
46#define ABIT_UGURU_MAX_BANK1_SENSORS 16
Guenter Roeck1bd385d2012-01-14 12:30:52 -080047/*
48 * Warning if you increase one of the 2 MAX defines below to 10 or higher you
49 * should adjust the belonging _NAMES_LENGTH macro for the 2 digit number!
50 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +020051/* max nr of sensors in bank2, currently mb's with max 6 fans are known */
52#define ABIT_UGURU_MAX_BANK2_SENSORS 6
53/* max nr of pwm outputs, currently mb's with max 5 pwm outputs are known */
54#define ABIT_UGURU_MAX_PWMS 5
55/* uGuru sensor bank 1 flags */ /* Alarm if: */
56#define ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE 0x01 /* temp over warn */
57#define ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE 0x02 /* volt over max */
58#define ABIT_UGURU_VOLT_LOW_ALARM_ENABLE 0x04 /* volt under min */
59#define ABIT_UGURU_TEMP_HIGH_ALARM_FLAG 0x10 /* temp is over warn */
60#define ABIT_UGURU_VOLT_HIGH_ALARM_FLAG 0x20 /* volt is over max */
61#define ABIT_UGURU_VOLT_LOW_ALARM_FLAG 0x40 /* volt is under min */
62/* uGuru sensor bank 2 flags */ /* Alarm if: */
63#define ABIT_UGURU_FAN_LOW_ALARM_ENABLE 0x01 /* fan under min */
64/* uGuru sensor bank common flags */
65#define ABIT_UGURU_BEEP_ENABLE 0x08 /* beep if alarm */
66#define ABIT_UGURU_SHUTDOWN_ENABLE 0x80 /* shutdown if alarm */
67/* uGuru fan PWM (speed control) flags */
68#define ABIT_UGURU_FAN_PWM_ENABLE 0x80 /* enable speed control */
69/* Values used for conversion */
70#define ABIT_UGURU_FAN_MAX 15300 /* RPM */
71/* Bank1 sensor types */
72#define ABIT_UGURU_IN_SENSOR 0
73#define ABIT_UGURU_TEMP_SENSOR 1
74#define ABIT_UGURU_NC 2
Guenter Roeck1bd385d2012-01-14 12:30:52 -080075/*
76 * In many cases we need to wait for the uGuru to reach a certain status, most
77 * of the time it will reach this status within 30 - 90 ISA reads, and thus we
78 * can best busy wait. This define gives the total amount of reads to try.
79 */
Hans de Goedefaf9b612006-08-25 10:24:20 +020080#define ABIT_UGURU_WAIT_TIMEOUT 125
Guenter Roeck1bd385d2012-01-14 12:30:52 -080081/*
82 * However sometimes older versions of the uGuru seem to be distracted and they
83 * do not respond for a long time. To handle this we sleep before each of the
84 * last ABIT_UGURU_WAIT_TIMEOUT_SLEEP tries.
85 */
Hans de Goedefaf9b612006-08-25 10:24:20 +020086#define ABIT_UGURU_WAIT_TIMEOUT_SLEEP 5
Guenter Roeck1bd385d2012-01-14 12:30:52 -080087/*
88 * Normally all expected status in abituguru_ready, are reported after the
89 * first read, but sometimes not and we need to poll.
90 */
Hans de Goedefaf9b612006-08-25 10:24:20 +020091#define ABIT_UGURU_READY_TIMEOUT 5
Hans de Goedef2b84bb2006-06-04 20:22:24 +020092/* Maximum 3 retries on timedout reads/writes, delay 200 ms before retrying */
93#define ABIT_UGURU_MAX_RETRIES 3
94#define ABIT_UGURU_RETRY_DELAY (HZ/5)
Hans de Goedea2392e02006-06-04 20:23:01 +020095/* Maximum 2 timeouts in abituguru_update_device, iow 3 in a row is an error */
Hans de Goedef2b84bb2006-06-04 20:22:24 +020096#define ABIT_UGURU_MAX_TIMEOUTS 2
Hans de Goedea2392e02006-06-04 20:23:01 +020097/* utility macros */
98#define ABIT_UGURU_NAME "abituguru"
Guenter Roeck24f9c532013-01-10 05:54:40 -080099#define ABIT_UGURU_DEBUG(level, format, arg...) \
100 do { \
101 if (level <= verbose) \
102 pr_debug(format , ## arg); \
103 } while (0)
104
Hans de Goedea2392e02006-06-04 20:23:01 +0200105/* Macros to help calculate the sysfs_names array length */
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800106/*
107 * sum of strlen of: in??_input\0, in??_{min,max}\0, in??_{min,max}_alarm\0,
108 * in??_{min,max}_alarm_enable\0, in??_beep\0, in??_shutdown\0
109 */
Hans de Goedea2392e02006-06-04 20:23:01 +0200110#define ABITUGURU_IN_NAMES_LENGTH (11 + 2 * 9 + 2 * 15 + 2 * 22 + 10 + 14)
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800111/*
112 * sum of strlen of: temp??_input\0, temp??_max\0, temp??_crit\0,
113 * temp??_alarm\0, temp??_alarm_enable\0, temp??_beep\0, temp??_shutdown\0
114 */
Hans de Goedea2392e02006-06-04 20:23:01 +0200115#define ABITUGURU_TEMP_NAMES_LENGTH (13 + 11 + 12 + 13 + 20 + 12 + 16)
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800116/*
117 * sum of strlen of: fan?_input\0, fan?_min\0, fan?_alarm\0,
118 * fan?_alarm_enable\0, fan?_beep\0, fan?_shutdown\0
119 */
Hans de Goedea2392e02006-06-04 20:23:01 +0200120#define ABITUGURU_FAN_NAMES_LENGTH (11 + 9 + 11 + 18 + 10 + 14)
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800121/*
122 * sum of strlen of: pwm?_enable\0, pwm?_auto_channels_temp\0,
123 * pwm?_auto_point{1,2}_pwm\0, pwm?_auto_point{1,2}_temp\0
124 */
Hans de Goedea2392e02006-06-04 20:23:01 +0200125#define ABITUGURU_PWM_NAMES_LENGTH (12 + 24 + 2 * 21 + 2 * 22)
126/* IN_NAMES_LENGTH > TEMP_NAMES_LENGTH so assume all bank1 sensors are in */
127#define ABITUGURU_SYSFS_NAMES_LENGTH ( \
128 ABIT_UGURU_MAX_BANK1_SENSORS * ABITUGURU_IN_NAMES_LENGTH + \
129 ABIT_UGURU_MAX_BANK2_SENSORS * ABITUGURU_FAN_NAMES_LENGTH + \
130 ABIT_UGURU_MAX_PWMS * ABITUGURU_PWM_NAMES_LENGTH)
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200131
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800132/*
133 * All the macros below are named identical to the oguru and oguru2 programs
134 * reverse engineered by Olle Sandberg, hence the names might not be 100%
135 * logical. I could come up with better names, but I prefer keeping the names
136 * identical so that this driver can be compared with his work more easily.
137 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200138/* Two i/o-ports are used by uGuru */
139#define ABIT_UGURU_BASE 0x00E0
140/* Used to tell uGuru what to read and to read the actual data */
141#define ABIT_UGURU_CMD 0x00
142/* Mostly used to check if uGuru is busy */
143#define ABIT_UGURU_DATA 0x04
144#define ABIT_UGURU_REGION_LENGTH 5
145/* uGuru status' */
146#define ABIT_UGURU_STATUS_WRITE 0x00 /* Ready to be written */
147#define ABIT_UGURU_STATUS_READ 0x01 /* Ready to be read */
148#define ABIT_UGURU_STATUS_INPUT 0x08 /* More input */
149#define ABIT_UGURU_STATUS_READY 0x09 /* Ready to be written */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200150
151/* Constants */
152/* in (Volt) sensors go up to 3494 mV, temp to 255000 millidegrees Celsius */
153static const int abituguru_bank1_max_value[2] = { 3494, 255000 };
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800154/*
155 * Min / Max allowed values for sensor2 (fan) alarm threshold, these values
156 * correspond to 300-3000 RPM
157 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200158static const u8 abituguru_bank2_min_threshold = 5;
159static const u8 abituguru_bank2_max_threshold = 50;
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800160/*
161 * Register 0 is a bitfield, 1 and 2 are pwm settings (255 = 100%), 3 and 4
162 * are temperature trip points.
163 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200164static const int abituguru_pwm_settings_multiplier[5] = { 0, 1, 1, 1000, 1000 };
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800165/*
166 * Min / Max allowed values for pwm_settings. Note: pwm1 (CPU fan) is a
LABBE Corentin84fb0292013-09-27 14:36:04 +0200167 * special case the minimum allowed pwm% setting for this is 30% (77) on
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800168 * some MB's this special case is handled in the code!
169 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200170static const u8 abituguru_pwm_min[5] = { 0, 170, 170, 25, 25 };
171static const u8 abituguru_pwm_max[5] = { 0, 255, 255, 75, 75 };
172
173
174/* Insmod parameters */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030175static bool force;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200176module_param(force, bool, 0);
177MODULE_PARM_DESC(force, "Set to one to force detection.");
Hans de Goede9b2ad122006-07-05 18:07:49 +0200178static int bank1_types[ABIT_UGURU_MAX_BANK1_SENSORS] = { -1, -1, -1, -1, -1,
179 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
180module_param_array(bank1_types, int, NULL, 0);
181MODULE_PARM_DESC(bank1_types, "Bank1 sensortype autodetection override:\n"
182 " -1 autodetect\n"
183 " 0 volt sensor\n"
184 " 1 temp sensor\n"
185 " 2 not connected");
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200186static int fan_sensors;
187module_param(fan_sensors, int, 0);
188MODULE_PARM_DESC(fan_sensors, "Number of fan sensors on the uGuru "
189 "(0 = autodetect)");
190static int pwms;
191module_param(pwms, int, 0);
192MODULE_PARM_DESC(pwms, "Number of PWMs on the uGuru "
193 "(0 = autodetect)");
194
195/* Default verbose is 2, since this driver is still in the testing phase */
196static int verbose = 2;
197module_param(verbose, int, 0644);
198MODULE_PARM_DESC(verbose, "How verbose should the driver be? (0-3):\n"
199 " 0 normal output\n"
200 " 1 + verbose error reporting\n"
201 " 2 + sensors type probing info\n"
202 " 3 + retryable error reporting");
203
204
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800205/*
206 * For the Abit uGuru, we need to keep some data in memory.
207 * The structure is dynamically allocated, at the same time when a new
208 * abituguru device is allocated.
209 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200210struct abituguru_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700211 struct device *hwmon_dev; /* hwmon registered device */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200212 struct mutex update_lock; /* protect access to data and uGuru */
213 unsigned long last_updated; /* In jiffies */
214 unsigned short addr; /* uguru base address */
215 char uguru_ready; /* is the uguru in ready state? */
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800216 unsigned char update_timeouts; /*
217 * number of update timeouts since last
218 * successful update
219 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200220
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800221 /*
222 * The sysfs attr and their names are generated automatically, for bank1
223 * we cannot use a predefined array because we don't know beforehand
224 * of a sensor is a volt or a temp sensor, for bank2 and the pwms its
225 * easier todo things the same way. For in sensors we have 9 (temp 7)
226 * sysfs entries per sensor, for bank2 and pwms 6.
227 */
Hans de Goedea2392e02006-06-04 20:23:01 +0200228 struct sensor_device_attribute_2 sysfs_attr[
229 ABIT_UGURU_MAX_BANK1_SENSORS * 9 +
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200230 ABIT_UGURU_MAX_BANK2_SENSORS * 6 + ABIT_UGURU_MAX_PWMS * 6];
Hans de Goedea2392e02006-06-04 20:23:01 +0200231 /* Buffer to store the dynamically generated sysfs names */
232 char sysfs_names[ABITUGURU_SYSFS_NAMES_LENGTH];
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200233
234 /* Bank 1 data */
Hans de Goedea2392e02006-06-04 20:23:01 +0200235 /* number of and addresses of [0] in, [1] temp sensors */
236 u8 bank1_sensors[2];
237 u8 bank1_address[2][ABIT_UGURU_MAX_BANK1_SENSORS];
238 u8 bank1_value[ABIT_UGURU_MAX_BANK1_SENSORS];
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800239 /*
240 * This array holds 3 entries per sensor for the bank 1 sensor settings
241 * (flags, min, max for voltage / flags, warn, shutdown for temp).
242 */
Hans de Goedea2392e02006-06-04 20:23:01 +0200243 u8 bank1_settings[ABIT_UGURU_MAX_BANK1_SENSORS][3];
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800244 /*
245 * Maximum value for each sensor used for scaling in mV/millidegrees
246 * Celsius.
247 */
Hans de Goedea2392e02006-06-04 20:23:01 +0200248 int bank1_max_value[ABIT_UGURU_MAX_BANK1_SENSORS];
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200249
250 /* Bank 2 data, ABIT_UGURU_MAX_BANK2_SENSORS entries for bank2 */
251 u8 bank2_sensors; /* actual number of bank2 sensors found */
252 u8 bank2_value[ABIT_UGURU_MAX_BANK2_SENSORS];
253 u8 bank2_settings[ABIT_UGURU_MAX_BANK2_SENSORS][2]; /* flags, min */
254
255 /* Alarms 2 bytes for bank1, 1 byte for bank2 */
256 u8 alarms[3];
257
258 /* Fan PWM (speed control) 5 bytes per PWM */
259 u8 pwms; /* actual number of pwms found */
260 u8 pwm_settings[ABIT_UGURU_MAX_PWMS][5];
261};
262
Joe Perches28ebfa12010-10-20 06:51:25 +0000263static const char *never_happen = "This should never happen.";
264static const char *report_this =
265 "Please report this to the abituguru maintainer (see MAINTAINERS)";
266
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200267/* wait till the uguru is in the specified state */
268static int abituguru_wait(struct abituguru_data *data, u8 state)
269{
270 int timeout = ABIT_UGURU_WAIT_TIMEOUT;
271
272 while (inb_p(data->addr + ABIT_UGURU_DATA) != state) {
273 timeout--;
274 if (timeout == 0)
275 return -EBUSY;
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800276 /*
277 * sleep a bit before our last few tries, see the comment on
278 * this where ABIT_UGURU_WAIT_TIMEOUT_SLEEP is defined.
279 */
Hans de Goedefaf9b612006-08-25 10:24:20 +0200280 if (timeout <= ABIT_UGURU_WAIT_TIMEOUT_SLEEP)
281 msleep(0);
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200282 }
283 return 0;
284}
285
286/* Put the uguru in ready for input state */
287static int abituguru_ready(struct abituguru_data *data)
288{
289 int timeout = ABIT_UGURU_READY_TIMEOUT;
290
291 if (data->uguru_ready)
292 return 0;
293
294 /* Reset? / Prepare for next read/write cycle */
295 outb(0x00, data->addr + ABIT_UGURU_DATA);
296
297 /* Wait till the uguru is ready */
298 if (abituguru_wait(data, ABIT_UGURU_STATUS_READY)) {
299 ABIT_UGURU_DEBUG(1,
300 "timeout exceeded waiting for ready state\n");
301 return -EIO;
302 }
303
304 /* Cmd port MUST be read now and should contain 0xAC */
305 while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
306 timeout--;
307 if (timeout == 0) {
308 ABIT_UGURU_DEBUG(1,
309 "CMD reg does not hold 0xAC after ready command\n");
310 return -EIO;
311 }
Hans de Goedefaf9b612006-08-25 10:24:20 +0200312 msleep(0);
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200313 }
314
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800315 /*
316 * After this the ABIT_UGURU_DATA port should contain
317 * ABIT_UGURU_STATUS_INPUT
318 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200319 timeout = ABIT_UGURU_READY_TIMEOUT;
320 while (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT) {
321 timeout--;
322 if (timeout == 0) {
323 ABIT_UGURU_DEBUG(1,
324 "state != more input after ready command\n");
325 return -EIO;
326 }
Hans de Goedefaf9b612006-08-25 10:24:20 +0200327 msleep(0);
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200328 }
329
330 data->uguru_ready = 1;
331 return 0;
332}
333
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800334/*
335 * Send the bank and then sensor address to the uGuru for the next read/write
336 * cycle. This function gets called as the first part of a read/write by
337 * abituguru_read and abituguru_write. This function should never be
338 * called by any other function.
339 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200340static int abituguru_send_address(struct abituguru_data *data,
341 u8 bank_addr, u8 sensor_addr, int retries)
342{
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800343 /*
344 * assume the caller does error handling itself if it has not requested
345 * any retries, and thus be quiet.
346 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200347 int report_errors = retries;
348
349 for (;;) {
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800350 /*
351 * Make sure the uguru is ready and then send the bank address,
352 * after this the uguru is no longer "ready".
353 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200354 if (abituguru_ready(data) != 0)
355 return -EIO;
356 outb(bank_addr, data->addr + ABIT_UGURU_DATA);
357 data->uguru_ready = 0;
358
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800359 /*
360 * Wait till the uguru is ABIT_UGURU_STATUS_INPUT state again
361 * and send the sensor addr
362 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200363 if (abituguru_wait(data, ABIT_UGURU_STATUS_INPUT)) {
364 if (retries) {
365 ABIT_UGURU_DEBUG(3, "timeout exceeded "
366 "waiting for more input state, %d "
367 "tries remaining\n", retries);
368 set_current_state(TASK_UNINTERRUPTIBLE);
369 schedule_timeout(ABIT_UGURU_RETRY_DELAY);
370 retries--;
371 continue;
372 }
373 if (report_errors)
374 ABIT_UGURU_DEBUG(1, "timeout exceeded "
375 "waiting for more input state "
376 "(bank: %d)\n", (int)bank_addr);
377 return -EBUSY;
378 }
379 outb(sensor_addr, data->addr + ABIT_UGURU_CMD);
380 return 0;
381 }
382}
383
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800384/*
385 * Read count bytes from sensor sensor_addr in bank bank_addr and store the
386 * result in buf, retry the send address part of the read retries times.
387 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200388static int abituguru_read(struct abituguru_data *data,
389 u8 bank_addr, u8 sensor_addr, u8 *buf, int count, int retries)
390{
391 int i;
392
393 /* Send the address */
394 i = abituguru_send_address(data, bank_addr, sensor_addr, retries);
395 if (i)
396 return i;
397
398 /* And read the data */
399 for (i = 0; i < count; i++) {
400 if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
Hans de Goedefaf9b612006-08-25 10:24:20 +0200401 ABIT_UGURU_DEBUG(retries ? 1 : 3,
402 "timeout exceeded waiting for "
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200403 "read state (bank: %d, sensor: %d)\n",
404 (int)bank_addr, (int)sensor_addr);
405 break;
406 }
407 buf[i] = inb(data->addr + ABIT_UGURU_CMD);
408 }
409
410 /* Last put the chip back in ready state */
411 abituguru_ready(data);
412
413 return i;
414}
415
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800416/*
417 * Write count bytes from buf to sensor sensor_addr in bank bank_addr, the send
418 * address part of the write is always retried ABIT_UGURU_MAX_RETRIES times.
419 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200420static int abituguru_write(struct abituguru_data *data,
421 u8 bank_addr, u8 sensor_addr, u8 *buf, int count)
422{
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800423 /*
424 * We use the ready timeout as we have to wait for 0xAC just like the
425 * ready function
426 */
Hans de Goedefaf9b612006-08-25 10:24:20 +0200427 int i, timeout = ABIT_UGURU_READY_TIMEOUT;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200428
429 /* Send the address */
430 i = abituguru_send_address(data, bank_addr, sensor_addr,
431 ABIT_UGURU_MAX_RETRIES);
432 if (i)
433 return i;
434
435 /* And write the data */
436 for (i = 0; i < count; i++) {
437 if (abituguru_wait(data, ABIT_UGURU_STATUS_WRITE)) {
438 ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for "
439 "write state (bank: %d, sensor: %d)\n",
440 (int)bank_addr, (int)sensor_addr);
441 break;
442 }
443 outb(buf[i], data->addr + ABIT_UGURU_CMD);
444 }
445
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800446 /*
447 * Now we need to wait till the chip is ready to be read again,
448 * so that we can read 0xAC as confirmation that our write has
449 * succeeded.
450 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200451 if (abituguru_wait(data, ABIT_UGURU_STATUS_READ)) {
452 ABIT_UGURU_DEBUG(1, "timeout exceeded waiting for read state "
453 "after write (bank: %d, sensor: %d)\n", (int)bank_addr,
454 (int)sensor_addr);
455 return -EIO;
456 }
457
458 /* Cmd port MUST be read now and should contain 0xAC */
Hans de Goedefaf9b612006-08-25 10:24:20 +0200459 while (inb_p(data->addr + ABIT_UGURU_CMD) != 0xAC) {
460 timeout--;
461 if (timeout == 0) {
462 ABIT_UGURU_DEBUG(1, "CMD reg does not hold 0xAC after "
463 "write (bank: %d, sensor: %d)\n",
464 (int)bank_addr, (int)sensor_addr);
465 return -EIO;
466 }
467 msleep(0);
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200468 }
469
470 /* Last put the chip back in ready state */
471 abituguru_ready(data);
472
473 return i;
474}
475
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800476/*
477 * Detect sensor type. Temp and Volt sensors are enabled with
478 * different masks and will ignore enable masks not meant for them.
479 * This enables us to test what kind of sensor we're dealing with.
480 * By setting the alarm thresholds so that we will always get an
481 * alarm for sensor type X and then enabling the sensor as sensor type
482 * X, if we then get an alarm it is a sensor of type X.
483 */
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500484static int
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200485abituguru_detect_bank1_sensor_type(struct abituguru_data *data,
486 u8 sensor_addr)
487{
Hans de Goedee432dc82007-06-18 22:59:34 +0200488 u8 val, test_flag, buf[3];
Hans de Goedefaf9b612006-08-25 10:24:20 +0200489 int i, ret = -ENODEV; /* error is the most common used retval :| */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200490
Hans de Goede9b2ad122006-07-05 18:07:49 +0200491 /* If overriden by the user return the user selected type */
492 if (bank1_types[sensor_addr] >= ABIT_UGURU_IN_SENSOR &&
493 bank1_types[sensor_addr] <= ABIT_UGURU_NC) {
494 ABIT_UGURU_DEBUG(2, "assuming sensor type %d for bank1 sensor "
495 "%d because of \"bank1_types\" module param\n",
496 bank1_types[sensor_addr], (int)sensor_addr);
497 return bank1_types[sensor_addr];
498 }
499
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200500 /* First read the sensor and the current settings */
501 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, sensor_addr, &val,
502 1, ABIT_UGURU_MAX_RETRIES) != 1)
Hans de Goedea2392e02006-06-04 20:23:01 +0200503 return -ENODEV;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200504
505 /* Test val is sane / usable for sensor type detection. */
Hans de Goedee432dc82007-06-18 22:59:34 +0200506 if ((val < 10u) || (val > 250u)) {
Joe Perches28ebfa12010-10-20 06:51:25 +0000507 pr_warn("bank1-sensor: %d reading (%d) too close to limits, "
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200508 "unable to determine sensor type, skipping sensor\n",
509 (int)sensor_addr, (int)val);
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800510 /*
511 * assume no sensor is there for sensors for which we can't
512 * determine the sensor type because their reading is too close
513 * to their limits, this usually means no sensor is there.
514 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200515 return ABIT_UGURU_NC;
516 }
517
518 ABIT_UGURU_DEBUG(2, "testing bank1 sensor %d\n", (int)sensor_addr);
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800519 /*
LABBE Corentin84fb0292013-09-27 14:36:04 +0200520 * Volt sensor test, enable volt low alarm, set min value ridiculously
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800521 * high, or vica versa if the reading is very high. If its a volt
522 * sensor this should always give us an alarm.
523 */
Hans de Goedee432dc82007-06-18 22:59:34 +0200524 if (val <= 240u) {
525 buf[0] = ABIT_UGURU_VOLT_LOW_ALARM_ENABLE;
526 buf[1] = 245;
527 buf[2] = 250;
528 test_flag = ABIT_UGURU_VOLT_LOW_ALARM_FLAG;
529 } else {
530 buf[0] = ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE;
531 buf[1] = 5;
532 buf[2] = 10;
533 test_flag = ABIT_UGURU_VOLT_HIGH_ALARM_FLAG;
534 }
535
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200536 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
537 buf, 3) != 3)
Hans de Goedefaf9b612006-08-25 10:24:20 +0200538 goto abituguru_detect_bank1_sensor_type_exit;
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800539 /*
540 * Now we need 20 ms to give the uguru time to read the sensors
541 * and raise a voltage alarm
542 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200543 set_current_state(TASK_UNINTERRUPTIBLE);
544 schedule_timeout(HZ/50);
545 /* Check for alarm and check the alarm is a volt low alarm. */
546 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
547 ABIT_UGURU_MAX_RETRIES) != 3)
Hans de Goedefaf9b612006-08-25 10:24:20 +0200548 goto abituguru_detect_bank1_sensor_type_exit;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200549 if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
550 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
551 sensor_addr, buf, 3,
552 ABIT_UGURU_MAX_RETRIES) != 3)
Hans de Goedefaf9b612006-08-25 10:24:20 +0200553 goto abituguru_detect_bank1_sensor_type_exit;
Hans de Goedee432dc82007-06-18 22:59:34 +0200554 if (buf[0] & test_flag) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200555 ABIT_UGURU_DEBUG(2, " found volt sensor\n");
Hans de Goedefaf9b612006-08-25 10:24:20 +0200556 ret = ABIT_UGURU_IN_SENSOR;
557 goto abituguru_detect_bank1_sensor_type_exit;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200558 } else
559 ABIT_UGURU_DEBUG(2, " alarm raised during volt "
Hans de Goedee432dc82007-06-18 22:59:34 +0200560 "sensor test, but volt range flag not set\n");
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200561 } else
562 ABIT_UGURU_DEBUG(2, " alarm not raised during volt sensor "
563 "test\n");
564
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800565 /*
566 * Temp sensor test, enable sensor as a temp sensor, set beep value
LABBE Corentin84fb0292013-09-27 14:36:04 +0200567 * ridiculously low (but not too low, otherwise uguru ignores it).
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800568 * If its a temp sensor this should always give us an alarm.
569 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200570 buf[0] = ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE;
571 buf[1] = 5;
572 buf[2] = 10;
573 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2, sensor_addr,
574 buf, 3) != 3)
Hans de Goedefaf9b612006-08-25 10:24:20 +0200575 goto abituguru_detect_bank1_sensor_type_exit;
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800576 /*
577 * Now we need 50 ms to give the uguru time to read the sensors
578 * and raise a temp alarm
579 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200580 set_current_state(TASK_UNINTERRUPTIBLE);
581 schedule_timeout(HZ/20);
582 /* Check for alarm and check the alarm is a temp high alarm. */
583 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0, buf, 3,
584 ABIT_UGURU_MAX_RETRIES) != 3)
Hans de Goedefaf9b612006-08-25 10:24:20 +0200585 goto abituguru_detect_bank1_sensor_type_exit;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200586 if (buf[sensor_addr/8] & (0x01 << (sensor_addr % 8))) {
587 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
588 sensor_addr, buf, 3,
589 ABIT_UGURU_MAX_RETRIES) != 3)
Hans de Goedefaf9b612006-08-25 10:24:20 +0200590 goto abituguru_detect_bank1_sensor_type_exit;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200591 if (buf[0] & ABIT_UGURU_TEMP_HIGH_ALARM_FLAG) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200592 ABIT_UGURU_DEBUG(2, " found temp sensor\n");
Hans de Goedefaf9b612006-08-25 10:24:20 +0200593 ret = ABIT_UGURU_TEMP_SENSOR;
594 goto abituguru_detect_bank1_sensor_type_exit;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200595 } else
596 ABIT_UGURU_DEBUG(2, " alarm raised during temp "
597 "sensor test, but temp high flag not set\n");
598 } else
599 ABIT_UGURU_DEBUG(2, " alarm not raised during temp sensor "
600 "test\n");
601
Hans de Goedefaf9b612006-08-25 10:24:20 +0200602 ret = ABIT_UGURU_NC;
603abituguru_detect_bank1_sensor_type_exit:
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800604 /*
605 * Restore original settings, failing here is really BAD, it has been
606 * reported that some BIOS-es hang when entering the uGuru menu with
607 * invalid settings present in the uGuru, so we try this 3 times.
608 */
Hans de Goedefaf9b612006-08-25 10:24:20 +0200609 for (i = 0; i < 3; i++)
610 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
611 sensor_addr, data->bank1_settings[sensor_addr],
612 3) == 3)
613 break;
614 if (i == 3) {
Joe Perches28ebfa12010-10-20 06:51:25 +0000615 pr_err("Fatal error could not restore original settings. %s %s\n",
616 never_happen, report_this);
Hans de Goedea2392e02006-06-04 20:23:01 +0200617 return -ENODEV;
Hans de Goedefaf9b612006-08-25 10:24:20 +0200618 }
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200619 return ret;
620}
621
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800622/*
623 * These functions try to find out how many sensors there are in bank2 and how
624 * many pwms there are. The purpose of this is to make sure that we don't give
625 * the user the possibility to change settings for non-existent sensors / pwm.
626 * The uGuru will happily read / write whatever memory happens to be after the
627 * memory storing the PWM settings when reading/writing to a PWM which is not
628 * there. Notice even if we detect a PWM which doesn't exist we normally won't
629 * write to it, unless the user tries to change the settings.
630 *
631 * Although the uGuru allows reading (settings) from non existing bank2
632 * sensors, my version of the uGuru does seem to stop writing to them, the
633 * write function above aborts in this case with:
634 * "CMD reg does not hold 0xAC after write"
635 *
636 * Notice these 2 tests are non destructive iow read-only tests, otherwise
637 * they would defeat their purpose. Although for the bank2_sensors detection a
638 * read/write test would be feasible because of the reaction above, I've
639 * however opted to stay on the safe side.
640 */
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500641static void
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200642abituguru_detect_no_bank2_sensors(struct abituguru_data *data)
643{
644 int i;
645
Hans de Goede9b2ad122006-07-05 18:07:49 +0200646 if (fan_sensors > 0 && fan_sensors <= ABIT_UGURU_MAX_BANK2_SENSORS) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200647 data->bank2_sensors = fan_sensors;
648 ABIT_UGURU_DEBUG(2, "assuming %d fan sensors because of "
649 "\"fan_sensors\" module param\n",
650 (int)data->bank2_sensors);
651 return;
652 }
653
654 ABIT_UGURU_DEBUG(2, "detecting number of fan sensors\n");
655 for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800656 /*
657 * 0x89 are the known used bits:
658 * -0x80 enable shutdown
659 * -0x08 enable beep
660 * -0x01 enable alarm
661 * All other bits should be 0, but on some motherboards
662 * 0x40 (bit 6) is also high for some of the fans??
663 */
Hans de Goedeb7c06602006-06-04 20:24:11 +0200664 if (data->bank2_settings[i][0] & ~0xC9) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200665 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
666 "to be a fan sensor: settings[0] = %02X\n",
667 i, (unsigned int)data->bank2_settings[i][0]);
668 break;
669 }
670
671 /* check if the threshold is within the allowed range */
672 if (data->bank2_settings[i][1] <
673 abituguru_bank2_min_threshold) {
674 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
675 "to be a fan sensor: the threshold (%d) is "
676 "below the minimum (%d)\n", i,
677 (int)data->bank2_settings[i][1],
678 (int)abituguru_bank2_min_threshold);
679 break;
680 }
681 if (data->bank2_settings[i][1] >
682 abituguru_bank2_max_threshold) {
683 ABIT_UGURU_DEBUG(2, " bank2 sensor %d does not seem "
684 "to be a fan sensor: the threshold (%d) is "
685 "above the maximum (%d)\n", i,
686 (int)data->bank2_settings[i][1],
687 (int)abituguru_bank2_max_threshold);
688 break;
689 }
690 }
691
692 data->bank2_sensors = i;
693 ABIT_UGURU_DEBUG(2, " found: %d fan sensors\n",
694 (int)data->bank2_sensors);
695}
696
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500697static void
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200698abituguru_detect_no_pwms(struct abituguru_data *data)
699{
700 int i, j;
701
Hans de Goede9b2ad122006-07-05 18:07:49 +0200702 if (pwms > 0 && pwms <= ABIT_UGURU_MAX_PWMS) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200703 data->pwms = pwms;
704 ABIT_UGURU_DEBUG(2, "assuming %d PWM outputs because of "
705 "\"pwms\" module param\n", (int)data->pwms);
706 return;
707 }
708
709 ABIT_UGURU_DEBUG(2, "detecting number of PWM outputs\n");
710 for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800711 /*
712 * 0x80 is the enable bit and the low
713 * nibble is which temp sensor to use,
714 * the other bits should be 0
715 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200716 if (data->pwm_settings[i][0] & ~0x8F) {
717 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
718 "to be a pwm channel: settings[0] = %02X\n",
719 i, (unsigned int)data->pwm_settings[i][0]);
720 break;
721 }
722
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800723 /*
724 * the low nibble must correspond to one of the temp sensors
725 * we've found
726 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200727 for (j = 0; j < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR];
728 j++) {
729 if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][j] ==
730 (data->pwm_settings[i][0] & 0x0F))
731 break;
732 }
733 if (j == data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]) {
734 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
735 "to be a pwm channel: %d is not a valid temp "
736 "sensor address\n", i,
737 data->pwm_settings[i][0] & 0x0F);
738 break;
739 }
740
741 /* check if all other settings are within the allowed range */
742 for (j = 1; j < 5; j++) {
743 u8 min;
744 /* special case pwm1 min pwm% */
745 if ((i == 0) && ((j == 1) || (j == 2)))
746 min = 77;
747 else
748 min = abituguru_pwm_min[j];
749 if (data->pwm_settings[i][j] < min) {
750 ABIT_UGURU_DEBUG(2, " pwm channel %d does "
751 "not seem to be a pwm channel: "
752 "setting %d (%d) is below the minimum "
753 "value (%d)\n", i, j,
754 (int)data->pwm_settings[i][j],
755 (int)min);
756 goto abituguru_detect_no_pwms_exit;
757 }
758 if (data->pwm_settings[i][j] > abituguru_pwm_max[j]) {
759 ABIT_UGURU_DEBUG(2, " pwm channel %d does "
760 "not seem to be a pwm channel: "
761 "setting %d (%d) is above the maximum "
762 "value (%d)\n", i, j,
763 (int)data->pwm_settings[i][j],
764 (int)abituguru_pwm_max[j]);
765 goto abituguru_detect_no_pwms_exit;
766 }
767 }
768
769 /* check that min temp < max temp and min pwm < max pwm */
770 if (data->pwm_settings[i][1] >= data->pwm_settings[i][2]) {
771 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
772 "to be a pwm channel: min pwm (%d) >= "
773 "max pwm (%d)\n", i,
774 (int)data->pwm_settings[i][1],
775 (int)data->pwm_settings[i][2]);
776 break;
777 }
778 if (data->pwm_settings[i][3] >= data->pwm_settings[i][4]) {
779 ABIT_UGURU_DEBUG(2, " pwm channel %d does not seem "
780 "to be a pwm channel: min temp (%d) >= "
781 "max temp (%d)\n", i,
782 (int)data->pwm_settings[i][3],
783 (int)data->pwm_settings[i][4]);
784 break;
785 }
786 }
787
788abituguru_detect_no_pwms_exit:
789 data->pwms = i;
790 ABIT_UGURU_DEBUG(2, " found: %d PWM outputs\n", (int)data->pwms);
791}
792
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800793/*
794 * Following are the sysfs callback functions. These functions expect:
795 * sensor_device_attribute_2->index: sensor address/offset in the bank
796 * sensor_device_attribute_2->nr: register offset, bitmask or NA.
797 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200798static struct abituguru_data *abituguru_update_device(struct device *dev);
799
800static ssize_t show_bank1_value(struct device *dev,
801 struct device_attribute *devattr, char *buf)
802{
803 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
804 struct abituguru_data *data = abituguru_update_device(dev);
805 if (!data)
806 return -EIO;
807 return sprintf(buf, "%d\n", (data->bank1_value[attr->index] *
808 data->bank1_max_value[attr->index] + 128) / 255);
809}
810
811static ssize_t show_bank1_setting(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 = dev_get_drvdata(dev);
816 return sprintf(buf, "%d\n",
817 (data->bank1_settings[attr->index][attr->nr] *
818 data->bank1_max_value[attr->index] + 128) / 255);
819}
820
821static ssize_t show_bank2_value(struct device *dev,
822 struct device_attribute *devattr, char *buf)
823{
824 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
825 struct abituguru_data *data = abituguru_update_device(dev);
826 if (!data)
827 return -EIO;
828 return sprintf(buf, "%d\n", (data->bank2_value[attr->index] *
829 ABIT_UGURU_FAN_MAX + 128) / 255);
830}
831
832static ssize_t show_bank2_setting(struct device *dev,
833 struct device_attribute *devattr, char *buf)
834{
835 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
836 struct abituguru_data *data = dev_get_drvdata(dev);
837 return sprintf(buf, "%d\n",
838 (data->bank2_settings[attr->index][attr->nr] *
839 ABIT_UGURU_FAN_MAX + 128) / 255);
840}
841
842static ssize_t store_bank1_setting(struct device *dev, struct device_attribute
843 *devattr, const char *buf, size_t count)
844{
845 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
846 struct abituguru_data *data = dev_get_drvdata(dev);
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800847 unsigned long val;
848 ssize_t ret;
849
850 ret = kstrtoul(buf, 10, &val);
851 if (ret)
852 return ret;
853
854 ret = count;
855 val = (val * 255 + data->bank1_max_value[attr->index] / 2) /
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200856 data->bank1_max_value[attr->index];
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800857 if (val > 255)
858 return -EINVAL;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200859
860 mutex_lock(&data->update_lock);
861 if (data->bank1_settings[attr->index][attr->nr] != val) {
862 u8 orig_val = data->bank1_settings[attr->index][attr->nr];
863 data->bank1_settings[attr->index][attr->nr] = val;
864 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK1 + 2,
865 attr->index, data->bank1_settings[attr->index],
866 3) <= attr->nr) {
867 data->bank1_settings[attr->index][attr->nr] = orig_val;
868 ret = -EIO;
869 }
870 }
871 mutex_unlock(&data->update_lock);
872 return ret;
873}
874
875static ssize_t store_bank2_setting(struct device *dev, struct device_attribute
876 *devattr, const char *buf, size_t count)
877{
878 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
879 struct abituguru_data *data = dev_get_drvdata(dev);
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800880 unsigned long val;
881 ssize_t ret;
882
883 ret = kstrtoul(buf, 10, &val);
884 if (ret)
885 return ret;
886
887 ret = count;
888 val = (val * 255 + ABIT_UGURU_FAN_MAX / 2) / ABIT_UGURU_FAN_MAX;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200889
890 /* this check can be done before taking the lock */
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800891 if (val < abituguru_bank2_min_threshold ||
892 val > abituguru_bank2_max_threshold)
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200893 return -EINVAL;
894
895 mutex_lock(&data->update_lock);
896 if (data->bank2_settings[attr->index][attr->nr] != val) {
897 u8 orig_val = data->bank2_settings[attr->index][attr->nr];
898 data->bank2_settings[attr->index][attr->nr] = val;
899 if (abituguru_write(data, ABIT_UGURU_SENSOR_BANK2 + 2,
900 attr->index, data->bank2_settings[attr->index],
901 2) <= attr->nr) {
902 data->bank2_settings[attr->index][attr->nr] = orig_val;
903 ret = -EIO;
904 }
905 }
906 mutex_unlock(&data->update_lock);
907 return ret;
908}
909
910static ssize_t show_bank1_alarm(struct device *dev,
911 struct device_attribute *devattr, char *buf)
912{
913 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
914 struct abituguru_data *data = abituguru_update_device(dev);
915 if (!data)
916 return -EIO;
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800917 /*
918 * See if the alarm bit for this sensor is set, and if the
919 * alarm matches the type of alarm we're looking for (for volt
920 * it can be either low or high). The type is stored in a few
921 * readonly bits in the settings part of the relevant sensor.
922 * The bitmask of the type is passed to us in attr->nr.
923 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200924 if ((data->alarms[attr->index / 8] & (0x01 << (attr->index % 8))) &&
925 (data->bank1_settings[attr->index][0] & attr->nr))
926 return sprintf(buf, "1\n");
927 else
928 return sprintf(buf, "0\n");
929}
930
931static ssize_t show_bank2_alarm(struct device *dev,
932 struct device_attribute *devattr, char *buf)
933{
934 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
935 struct abituguru_data *data = abituguru_update_device(dev);
936 if (!data)
937 return -EIO;
938 if (data->alarms[2] & (0x01 << attr->index))
939 return sprintf(buf, "1\n");
940 else
941 return sprintf(buf, "0\n");
942}
943
944static ssize_t show_bank1_mask(struct device *dev,
945 struct device_attribute *devattr, char *buf)
946{
947 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
948 struct abituguru_data *data = dev_get_drvdata(dev);
949 if (data->bank1_settings[attr->index][0] & attr->nr)
950 return sprintf(buf, "1\n");
951 else
952 return sprintf(buf, "0\n");
953}
954
955static ssize_t show_bank2_mask(struct device *dev,
956 struct device_attribute *devattr, char *buf)
957{
958 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
959 struct abituguru_data *data = dev_get_drvdata(dev);
960 if (data->bank2_settings[attr->index][0] & attr->nr)
961 return sprintf(buf, "1\n");
962 else
963 return sprintf(buf, "0\n");
964}
965
966static ssize_t store_bank1_mask(struct device *dev,
967 struct device_attribute *devattr, const char *buf, size_t count)
968{
969 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
970 struct abituguru_data *data = dev_get_drvdata(dev);
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800971 ssize_t ret;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200972 u8 orig_val;
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800973 unsigned long mask;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200974
Guenter Roeck1bd385d2012-01-14 12:30:52 -0800975 ret = kstrtoul(buf, 10, &mask);
976 if (ret)
977 return ret;
978
979 ret = count;
Hans de Goedef2b84bb2006-06-04 20:22:24 +0200980 mutex_lock(&data->update_lock);
981 orig_val = data->bank1_settings[attr->index][0];
982
983 if (mask)
984 data->bank1_settings[attr->index][0] |= attr->nr;
985 else
986 data->bank1_settings[attr->index][0] &= ~attr->nr;
987
988 if ((data->bank1_settings[attr->index][0] != orig_val) &&
989 (abituguru_write(data,
990 ABIT_UGURU_SENSOR_BANK1 + 2, attr->index,
991 data->bank1_settings[attr->index], 3) < 1)) {
992 data->bank1_settings[attr->index][0] = orig_val;
993 ret = -EIO;
994 }
995 mutex_unlock(&data->update_lock);
996 return ret;
997}
998
999static ssize_t store_bank2_mask(struct device *dev,
1000 struct device_attribute *devattr, const char *buf, size_t count)
1001{
1002 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1003 struct abituguru_data *data = dev_get_drvdata(dev);
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001004 ssize_t ret;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001005 u8 orig_val;
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001006 unsigned long mask;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001007
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001008 ret = kstrtoul(buf, 10, &mask);
1009 if (ret)
1010 return ret;
1011
1012 ret = count;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001013 mutex_lock(&data->update_lock);
1014 orig_val = data->bank2_settings[attr->index][0];
1015
1016 if (mask)
1017 data->bank2_settings[attr->index][0] |= attr->nr;
1018 else
1019 data->bank2_settings[attr->index][0] &= ~attr->nr;
1020
1021 if ((data->bank2_settings[attr->index][0] != orig_val) &&
1022 (abituguru_write(data,
1023 ABIT_UGURU_SENSOR_BANK2 + 2, attr->index,
1024 data->bank2_settings[attr->index], 2) < 1)) {
1025 data->bank2_settings[attr->index][0] = orig_val;
1026 ret = -EIO;
1027 }
1028 mutex_unlock(&data->update_lock);
1029 return ret;
1030}
1031
1032/* Fan PWM (speed control) */
1033static ssize_t show_pwm_setting(struct device *dev,
1034 struct device_attribute *devattr, char *buf)
1035{
1036 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1037 struct abituguru_data *data = dev_get_drvdata(dev);
1038 return sprintf(buf, "%d\n", data->pwm_settings[attr->index][attr->nr] *
1039 abituguru_pwm_settings_multiplier[attr->nr]);
1040}
1041
1042static ssize_t store_pwm_setting(struct device *dev, struct device_attribute
1043 *devattr, const char *buf, size_t count)
1044{
1045 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1046 struct abituguru_data *data = dev_get_drvdata(dev);
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001047 u8 min;
1048 unsigned long val;
1049 ssize_t ret;
1050
1051 ret = kstrtoul(buf, 10, &val);
1052 if (ret)
1053 return ret;
1054
1055 ret = count;
1056 val = (val + abituguru_pwm_settings_multiplier[attr->nr] / 2) /
1057 abituguru_pwm_settings_multiplier[attr->nr];
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001058
1059 /* special case pwm1 min pwm% */
1060 if ((attr->index == 0) && ((attr->nr == 1) || (attr->nr == 2)))
1061 min = 77;
1062 else
1063 min = abituguru_pwm_min[attr->nr];
1064
1065 /* this check can be done before taking the lock */
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001066 if (val < min || val > abituguru_pwm_max[attr->nr])
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001067 return -EINVAL;
1068
1069 mutex_lock(&data->update_lock);
1070 /* this check needs to be done after taking the lock */
1071 if ((attr->nr & 1) &&
1072 (val >= data->pwm_settings[attr->index][attr->nr + 1]))
1073 ret = -EINVAL;
1074 else if (!(attr->nr & 1) &&
1075 (val <= data->pwm_settings[attr->index][attr->nr - 1]))
1076 ret = -EINVAL;
1077 else if (data->pwm_settings[attr->index][attr->nr] != val) {
1078 u8 orig_val = data->pwm_settings[attr->index][attr->nr];
1079 data->pwm_settings[attr->index][attr->nr] = val;
1080 if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1081 attr->index, data->pwm_settings[attr->index],
1082 5) <= attr->nr) {
1083 data->pwm_settings[attr->index][attr->nr] =
1084 orig_val;
1085 ret = -EIO;
1086 }
1087 }
1088 mutex_unlock(&data->update_lock);
1089 return ret;
1090}
1091
1092static ssize_t show_pwm_sensor(struct device *dev,
1093 struct device_attribute *devattr, char *buf)
1094{
1095 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1096 struct abituguru_data *data = dev_get_drvdata(dev);
1097 int i;
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001098 /*
1099 * We need to walk to the temp sensor addresses to find what
1100 * the userspace id of the configured temp sensor is.
1101 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001102 for (i = 0; i < data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR]; i++)
1103 if (data->bank1_address[ABIT_UGURU_TEMP_SENSOR][i] ==
1104 (data->pwm_settings[attr->index][0] & 0x0F))
1105 return sprintf(buf, "%d\n", i+1);
1106
1107 return -ENXIO;
1108}
1109
1110static ssize_t store_pwm_sensor(struct device *dev, struct device_attribute
1111 *devattr, const char *buf, size_t count)
1112{
1113 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1114 struct abituguru_data *data = dev_get_drvdata(dev);
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001115 ssize_t ret;
1116 unsigned long val;
1117 u8 orig_val;
1118 u8 address;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001119
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001120 ret = kstrtoul(buf, 10, &val);
1121 if (ret)
1122 return ret;
1123
1124 if (val == 0 || val > data->bank1_sensors[ABIT_UGURU_TEMP_SENSOR])
1125 return -EINVAL;
1126
1127 val -= 1;
1128 ret = count;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001129 mutex_lock(&data->update_lock);
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001130 orig_val = data->pwm_settings[attr->index][0];
1131 address = data->bank1_address[ABIT_UGURU_TEMP_SENSOR][val];
1132 data->pwm_settings[attr->index][0] &= 0xF0;
1133 data->pwm_settings[attr->index][0] |= address;
1134 if (data->pwm_settings[attr->index][0] != orig_val) {
1135 if (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1, attr->index,
1136 data->pwm_settings[attr->index], 5) < 1) {
1137 data->pwm_settings[attr->index][0] = orig_val;
1138 ret = -EIO;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001139 }
1140 }
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001141 mutex_unlock(&data->update_lock);
1142 return ret;
1143}
1144
1145static ssize_t show_pwm_enable(struct device *dev,
1146 struct device_attribute *devattr, char *buf)
1147{
1148 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1149 struct abituguru_data *data = dev_get_drvdata(dev);
1150 int res = 0;
1151 if (data->pwm_settings[attr->index][0] & ABIT_UGURU_FAN_PWM_ENABLE)
1152 res = 2;
1153 return sprintf(buf, "%d\n", res);
1154}
1155
1156static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1157 *devattr, const char *buf, size_t count)
1158{
1159 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
1160 struct abituguru_data *data = dev_get_drvdata(dev);
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001161 u8 orig_val;
1162 ssize_t ret;
1163 unsigned long user_val;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001164
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001165 ret = kstrtoul(buf, 10, &user_val);
1166 if (ret)
1167 return ret;
1168
1169 ret = count;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001170 mutex_lock(&data->update_lock);
1171 orig_val = data->pwm_settings[attr->index][0];
1172 switch (user_val) {
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001173 case 0:
1174 data->pwm_settings[attr->index][0] &=
1175 ~ABIT_UGURU_FAN_PWM_ENABLE;
1176 break;
1177 case 2:
1178 data->pwm_settings[attr->index][0] |= ABIT_UGURU_FAN_PWM_ENABLE;
1179 break;
1180 default:
1181 ret = -EINVAL;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001182 }
1183 if ((data->pwm_settings[attr->index][0] != orig_val) &&
1184 (abituguru_write(data, ABIT_UGURU_FAN_PWM + 1,
1185 attr->index, data->pwm_settings[attr->index],
1186 5) < 1)) {
1187 data->pwm_settings[attr->index][0] = orig_val;
1188 ret = -EIO;
1189 }
1190 mutex_unlock(&data->update_lock);
1191 return ret;
1192}
1193
1194static ssize_t show_name(struct device *dev,
1195 struct device_attribute *devattr, char *buf)
1196{
1197 return sprintf(buf, "%s\n", ABIT_UGURU_NAME);
1198}
1199
1200/* Sysfs attr templates, the real entries are generated automatically. */
1201static const
1202struct sensor_device_attribute_2 abituguru_sysfs_bank1_templ[2][9] = {
1203 {
1204 SENSOR_ATTR_2(in%d_input, 0444, show_bank1_value, NULL, 0, 0),
1205 SENSOR_ATTR_2(in%d_min, 0644, show_bank1_setting,
1206 store_bank1_setting, 1, 0),
1207 SENSOR_ATTR_2(in%d_min_alarm, 0444, show_bank1_alarm, NULL,
1208 ABIT_UGURU_VOLT_LOW_ALARM_FLAG, 0),
1209 SENSOR_ATTR_2(in%d_max, 0644, show_bank1_setting,
1210 store_bank1_setting, 2, 0),
1211 SENSOR_ATTR_2(in%d_max_alarm, 0444, show_bank1_alarm, NULL,
1212 ABIT_UGURU_VOLT_HIGH_ALARM_FLAG, 0),
1213 SENSOR_ATTR_2(in%d_beep, 0644, show_bank1_mask,
1214 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1215 SENSOR_ATTR_2(in%d_shutdown, 0644, show_bank1_mask,
1216 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1217 SENSOR_ATTR_2(in%d_min_alarm_enable, 0644, show_bank1_mask,
1218 store_bank1_mask, ABIT_UGURU_VOLT_LOW_ALARM_ENABLE, 0),
1219 SENSOR_ATTR_2(in%d_max_alarm_enable, 0644, show_bank1_mask,
1220 store_bank1_mask, ABIT_UGURU_VOLT_HIGH_ALARM_ENABLE, 0),
1221 }, {
1222 SENSOR_ATTR_2(temp%d_input, 0444, show_bank1_value, NULL, 0, 0),
1223 SENSOR_ATTR_2(temp%d_alarm, 0444, show_bank1_alarm, NULL,
1224 ABIT_UGURU_TEMP_HIGH_ALARM_FLAG, 0),
1225 SENSOR_ATTR_2(temp%d_max, 0644, show_bank1_setting,
1226 store_bank1_setting, 1, 0),
1227 SENSOR_ATTR_2(temp%d_crit, 0644, show_bank1_setting,
1228 store_bank1_setting, 2, 0),
1229 SENSOR_ATTR_2(temp%d_beep, 0644, show_bank1_mask,
1230 store_bank1_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1231 SENSOR_ATTR_2(temp%d_shutdown, 0644, show_bank1_mask,
1232 store_bank1_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1233 SENSOR_ATTR_2(temp%d_alarm_enable, 0644, show_bank1_mask,
1234 store_bank1_mask, ABIT_UGURU_TEMP_HIGH_ALARM_ENABLE, 0),
1235 }
1236};
1237
1238static const struct sensor_device_attribute_2 abituguru_sysfs_fan_templ[6] = {
1239 SENSOR_ATTR_2(fan%d_input, 0444, show_bank2_value, NULL, 0, 0),
1240 SENSOR_ATTR_2(fan%d_alarm, 0444, show_bank2_alarm, NULL, 0, 0),
1241 SENSOR_ATTR_2(fan%d_min, 0644, show_bank2_setting,
1242 store_bank2_setting, 1, 0),
1243 SENSOR_ATTR_2(fan%d_beep, 0644, show_bank2_mask,
1244 store_bank2_mask, ABIT_UGURU_BEEP_ENABLE, 0),
1245 SENSOR_ATTR_2(fan%d_shutdown, 0644, show_bank2_mask,
1246 store_bank2_mask, ABIT_UGURU_SHUTDOWN_ENABLE, 0),
1247 SENSOR_ATTR_2(fan%d_alarm_enable, 0644, show_bank2_mask,
1248 store_bank2_mask, ABIT_UGURU_FAN_LOW_ALARM_ENABLE, 0),
1249};
1250
1251static const struct sensor_device_attribute_2 abituguru_sysfs_pwm_templ[6] = {
1252 SENSOR_ATTR_2(pwm%d_enable, 0644, show_pwm_enable,
1253 store_pwm_enable, 0, 0),
1254 SENSOR_ATTR_2(pwm%d_auto_channels_temp, 0644, show_pwm_sensor,
1255 store_pwm_sensor, 0, 0),
1256 SENSOR_ATTR_2(pwm%d_auto_point1_pwm, 0644, show_pwm_setting,
1257 store_pwm_setting, 1, 0),
1258 SENSOR_ATTR_2(pwm%d_auto_point2_pwm, 0644, show_pwm_setting,
1259 store_pwm_setting, 2, 0),
1260 SENSOR_ATTR_2(pwm%d_auto_point1_temp, 0644, show_pwm_setting,
1261 store_pwm_setting, 3, 0),
1262 SENSOR_ATTR_2(pwm%d_auto_point2_temp, 0644, show_pwm_setting,
1263 store_pwm_setting, 4, 0),
1264};
1265
Hans de Goedea2392e02006-06-04 20:23:01 +02001266static struct sensor_device_attribute_2 abituguru_sysfs_attr[] = {
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001267 SENSOR_ATTR_2(name, 0444, show_name, NULL, 0, 0),
1268};
1269
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001270static int abituguru_probe(struct platform_device *pdev)
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001271{
1272 struct abituguru_data *data;
Hans de Goedea2392e02006-06-04 20:23:01 +02001273 int i, j, used, sysfs_names_free, sysfs_attr_i, res = -ENODEV;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001274 char *sysfs_filename;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001275
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001276 /*
1277 * El weirdo probe order, to keep the sysfs order identical to the
1278 * BIOS and window-appliction listing order.
1279 */
Hans de Goedea2392e02006-06-04 20:23:01 +02001280 const u8 probe_order[ABIT_UGURU_MAX_BANK1_SENSORS] = {
1281 0x00, 0x01, 0x03, 0x04, 0x0A, 0x08, 0x0E, 0x02,
1282 0x09, 0x06, 0x05, 0x0B, 0x0F, 0x0D, 0x07, 0x0C };
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001283
Guenter Roeckd8f30ad2012-06-02 09:57:56 -07001284 data = devm_kzalloc(&pdev->dev, sizeof(struct abituguru_data),
1285 GFP_KERNEL);
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001286 if (!data)
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001287 return -ENOMEM;
1288
1289 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
1290 mutex_init(&data->update_lock);
1291 platform_set_drvdata(pdev, data);
1292
1293 /* See if the uGuru is ready */
1294 if (inb_p(data->addr + ABIT_UGURU_DATA) == ABIT_UGURU_STATUS_INPUT)
1295 data->uguru_ready = 1;
1296
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001297 /*
1298 * Completely read the uGuru this has 2 purposes:
1299 * - testread / see if one really is there.
1300 * - make an in memory copy of all the uguru settings for future use.
1301 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001302 if (abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
Hans de Goedea2392e02006-06-04 20:23:01 +02001303 data->alarms, 3, ABIT_UGURU_MAX_RETRIES) != 3)
1304 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001305
Hans de Goedea2392e02006-06-04 20:23:01 +02001306 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001307 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1, i,
1308 &data->bank1_value[i], 1,
Hans de Goedea2392e02006-06-04 20:23:01 +02001309 ABIT_UGURU_MAX_RETRIES) != 1)
1310 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001311 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK1+1, i,
1312 data->bank1_settings[i], 3,
Hans de Goedea2392e02006-06-04 20:23:01 +02001313 ABIT_UGURU_MAX_RETRIES) != 3)
1314 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001315 }
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001316 /*
1317 * Note: We don't know how many bank2 sensors / pwms there really are,
1318 * but in order to "detect" this we need to read the maximum amount
1319 * anyways. If we read sensors/pwms not there we'll just read crap
1320 * this can't hurt. We need the detection because we don't want
1321 * unwanted writes, which will hurt!
1322 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001323 for (i = 0; i < ABIT_UGURU_MAX_BANK2_SENSORS; i++) {
1324 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
1325 &data->bank2_value[i], 1,
Hans de Goedea2392e02006-06-04 20:23:01 +02001326 ABIT_UGURU_MAX_RETRIES) != 1)
1327 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001328 if (abituguru_read(data, ABIT_UGURU_SENSOR_BANK2+1, i,
1329 data->bank2_settings[i], 2,
Hans de Goedea2392e02006-06-04 20:23:01 +02001330 ABIT_UGURU_MAX_RETRIES) != 2)
1331 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001332 }
1333 for (i = 0; i < ABIT_UGURU_MAX_PWMS; i++) {
1334 if (abituguru_read(data, ABIT_UGURU_FAN_PWM, i,
1335 data->pwm_settings[i], 5,
Hans de Goedea2392e02006-06-04 20:23:01 +02001336 ABIT_UGURU_MAX_RETRIES) != 5)
1337 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001338 }
1339 data->last_updated = jiffies;
1340
1341 /* Detect sensor types and fill the sysfs attr for bank1 */
Hans de Goedea2392e02006-06-04 20:23:01 +02001342 sysfs_attr_i = 0;
1343 sysfs_filename = data->sysfs_names;
1344 sysfs_names_free = ABITUGURU_SYSFS_NAMES_LENGTH;
1345 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001346 res = abituguru_detect_bank1_sensor_type(data, probe_order[i]);
Hans de Goedea2392e02006-06-04 20:23:01 +02001347 if (res < 0)
1348 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001349 if (res == ABIT_UGURU_NC)
1350 continue;
1351
Hans de Goedea2392e02006-06-04 20:23:01 +02001352 /* res 1 (temp) sensors have 7 sysfs entries, 0 (in) 9 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001353 for (j = 0; j < (res ? 7 : 9); j++) {
Hans de Goedea2392e02006-06-04 20:23:01 +02001354 used = snprintf(sysfs_filename, sysfs_names_free,
1355 abituguru_sysfs_bank1_templ[res][j].dev_attr.
1356 attr.name, data->bank1_sensors[res] + res)
1357 + 1;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001358 data->sysfs_attr[sysfs_attr_i] =
1359 abituguru_sysfs_bank1_templ[res][j];
1360 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1361 sysfs_filename;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001362 data->sysfs_attr[sysfs_attr_i].index = probe_order[i];
Hans de Goedea2392e02006-06-04 20:23:01 +02001363 sysfs_filename += used;
1364 sysfs_names_free -= used;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001365 sysfs_attr_i++;
1366 }
1367 data->bank1_max_value[probe_order[i]] =
1368 abituguru_bank1_max_value[res];
1369 data->bank1_address[res][data->bank1_sensors[res]] =
1370 probe_order[i];
1371 data->bank1_sensors[res]++;
1372 }
1373 /* Detect number of sensors and fill the sysfs attr for bank2 (fans) */
1374 abituguru_detect_no_bank2_sensors(data);
1375 for (i = 0; i < data->bank2_sensors; i++) {
Hans de Goedea2392e02006-06-04 20:23:01 +02001376 for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_fan_templ); j++) {
1377 used = snprintf(sysfs_filename, sysfs_names_free,
1378 abituguru_sysfs_fan_templ[j].dev_attr.attr.name,
1379 i + 1) + 1;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001380 data->sysfs_attr[sysfs_attr_i] =
1381 abituguru_sysfs_fan_templ[j];
1382 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1383 sysfs_filename;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001384 data->sysfs_attr[sysfs_attr_i].index = i;
Hans de Goedea2392e02006-06-04 20:23:01 +02001385 sysfs_filename += used;
1386 sysfs_names_free -= used;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001387 sysfs_attr_i++;
1388 }
1389 }
1390 /* Detect number of sensors and fill the sysfs attr for pwms */
1391 abituguru_detect_no_pwms(data);
1392 for (i = 0; i < data->pwms; i++) {
Hans de Goedea2392e02006-06-04 20:23:01 +02001393 for (j = 0; j < ARRAY_SIZE(abituguru_sysfs_pwm_templ); j++) {
1394 used = snprintf(sysfs_filename, sysfs_names_free,
1395 abituguru_sysfs_pwm_templ[j].dev_attr.attr.name,
1396 i + 1) + 1;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001397 data->sysfs_attr[sysfs_attr_i] =
1398 abituguru_sysfs_pwm_templ[j];
1399 data->sysfs_attr[sysfs_attr_i].dev_attr.attr.name =
1400 sysfs_filename;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001401 data->sysfs_attr[sysfs_attr_i].index = i;
Hans de Goedea2392e02006-06-04 20:23:01 +02001402 sysfs_filename += used;
1403 sysfs_names_free -= used;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001404 sysfs_attr_i++;
1405 }
1406 }
Hans de Goedea2392e02006-06-04 20:23:01 +02001407 /* Fail safe check, this should never happen! */
1408 if (sysfs_names_free < 0) {
Joe Perches28ebfa12010-10-20 06:51:25 +00001409 pr_err("Fatal error ran out of space for sysfs attr names. %s %s",
1410 never_happen, report_this);
Hans de Goedea2392e02006-06-04 20:23:01 +02001411 res = -ENAMETOOLONG;
1412 goto abituguru_probe_error;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001413 }
Joe Perches28ebfa12010-10-20 06:51:25 +00001414 pr_info("found Abit uGuru\n");
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001415
1416 /* Register sysfs hooks */
Wei Yongjunecacb0b2013-05-13 14:01:43 +08001417 for (i = 0; i < sysfs_attr_i; i++) {
1418 res = device_create_file(&pdev->dev,
1419 &data->sysfs_attr[i].dev_attr);
1420 if (res)
Hans de Goedebc8f0a22007-02-14 21:15:05 +01001421 goto abituguru_probe_error;
Wei Yongjunecacb0b2013-05-13 14:01:43 +08001422 }
1423 for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++) {
1424 res = device_create_file(&pdev->dev,
1425 &abituguru_sysfs_attr[i].dev_attr);
1426 if (res)
Hans de Goedebc8f0a22007-02-14 21:15:05 +01001427 goto abituguru_probe_error;
Wei Yongjunecacb0b2013-05-13 14:01:43 +08001428 }
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001429
Tony Jones1beeffe2007-08-20 13:46:20 -07001430 data->hwmon_dev = hwmon_device_register(&pdev->dev);
1431 if (!IS_ERR(data->hwmon_dev))
Hans de Goedebc8f0a22007-02-14 21:15:05 +01001432 return 0; /* success */
Hans de Goedea2392e02006-06-04 20:23:01 +02001433
Tony Jones1beeffe2007-08-20 13:46:20 -07001434 res = PTR_ERR(data->hwmon_dev);
Hans de Goedea2392e02006-06-04 20:23:01 +02001435abituguru_probe_error:
Hans de Goedebc8f0a22007-02-14 21:15:05 +01001436 for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
1437 device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1438 for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1439 device_remove_file(&pdev->dev,
1440 &abituguru_sysfs_attr[i].dev_attr);
Hans de Goedea2392e02006-06-04 20:23:01 +02001441 return res;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001442}
1443
Bill Pemberton281dfd02012-11-19 13:25:51 -05001444static int abituguru_remove(struct platform_device *pdev)
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001445{
Hans de Goedebc8f0a22007-02-14 21:15:05 +01001446 int i;
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001447 struct abituguru_data *data = platform_get_drvdata(pdev);
1448
Tony Jones1beeffe2007-08-20 13:46:20 -07001449 hwmon_device_unregister(data->hwmon_dev);
Hans de Goedebc8f0a22007-02-14 21:15:05 +01001450 for (i = 0; data->sysfs_attr[i].dev_attr.attr.name; i++)
1451 device_remove_file(&pdev->dev, &data->sysfs_attr[i].dev_attr);
1452 for (i = 0; i < ARRAY_SIZE(abituguru_sysfs_attr); i++)
1453 device_remove_file(&pdev->dev,
1454 &abituguru_sysfs_attr[i].dev_attr);
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001455
1456 return 0;
1457}
1458
1459static struct abituguru_data *abituguru_update_device(struct device *dev)
1460{
1461 int i, err;
1462 struct abituguru_data *data = dev_get_drvdata(dev);
1463 /* fake a complete successful read if no update necessary. */
1464 char success = 1;
1465
1466 mutex_lock(&data->update_lock);
1467 if (time_after(jiffies, data->last_updated + HZ)) {
1468 success = 0;
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001469 err = abituguru_read(data, ABIT_UGURU_ALARM_BANK, 0,
1470 data->alarms, 3, 0);
1471 if (err != 3)
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001472 goto LEAVE_UPDATE;
Hans de Goedea2392e02006-06-04 20:23:01 +02001473 for (i = 0; i < ABIT_UGURU_MAX_BANK1_SENSORS; i++) {
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001474 err = abituguru_read(data, ABIT_UGURU_SENSOR_BANK1,
1475 i, &data->bank1_value[i], 1, 0);
1476 if (err != 1)
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001477 goto LEAVE_UPDATE;
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001478 err = abituguru_read(data, ABIT_UGURU_SENSOR_BANK1 + 1,
1479 i, data->bank1_settings[i], 3, 0);
1480 if (err != 3)
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001481 goto LEAVE_UPDATE;
1482 }
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001483 for (i = 0; i < data->bank2_sensors; i++) {
1484 err = abituguru_read(data, ABIT_UGURU_SENSOR_BANK2, i,
1485 &data->bank2_value[i], 1, 0);
1486 if (err != 1)
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001487 goto LEAVE_UPDATE;
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001488 }
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001489 /* success! */
1490 success = 1;
1491 data->update_timeouts = 0;
1492LEAVE_UPDATE:
1493 /* handle timeout condition */
Hans de Goedefaf9b612006-08-25 10:24:20 +02001494 if (!success && (err == -EBUSY || err >= 0)) {
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001495 /* No overflow please */
1496 if (data->update_timeouts < 255u)
1497 data->update_timeouts++;
1498 if (data->update_timeouts <= ABIT_UGURU_MAX_TIMEOUTS) {
1499 ABIT_UGURU_DEBUG(3, "timeout exceeded, will "
1500 "try again next update\n");
1501 /* Just a timeout, fake a successful read */
1502 success = 1;
1503 } else
1504 ABIT_UGURU_DEBUG(1, "timeout exceeded %d "
1505 "times waiting for more input state\n",
1506 (int)data->update_timeouts);
1507 }
1508 /* On success set last_updated */
1509 if (success)
1510 data->last_updated = jiffies;
1511 }
1512 mutex_unlock(&data->update_lock);
1513
1514 if (success)
1515 return data;
1516 else
1517 return NULL;
1518}
1519
Rafael J. Wysocki4f0928c2012-07-07 23:59:12 +02001520#ifdef CONFIG_PM_SLEEP
1521static int abituguru_suspend(struct device *dev)
Hans de Goede360b9ab2006-08-28 14:42:24 +02001522{
Rafael J. Wysocki4f0928c2012-07-07 23:59:12 +02001523 struct abituguru_data *data = dev_get_drvdata(dev);
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001524 /*
1525 * make sure all communications with the uguru are done and no new
1526 * ones are started
1527 */
Hans de Goede360b9ab2006-08-28 14:42:24 +02001528 mutex_lock(&data->update_lock);
1529 return 0;
1530}
1531
Rafael J. Wysocki4f0928c2012-07-07 23:59:12 +02001532static int abituguru_resume(struct device *dev)
Hans de Goede360b9ab2006-08-28 14:42:24 +02001533{
Rafael J. Wysocki4f0928c2012-07-07 23:59:12 +02001534 struct abituguru_data *data = dev_get_drvdata(dev);
Hans de Goede360b9ab2006-08-28 14:42:24 +02001535 /* See if the uGuru is still ready */
1536 if (inb_p(data->addr + ABIT_UGURU_DATA) != ABIT_UGURU_STATUS_INPUT)
1537 data->uguru_ready = 0;
1538 mutex_unlock(&data->update_lock);
1539 return 0;
1540}
Rafael J. Wysocki4f0928c2012-07-07 23:59:12 +02001541
1542static SIMPLE_DEV_PM_OPS(abituguru_pm, abituguru_suspend, abituguru_resume);
Guenter Roeck24f9c532013-01-10 05:54:40 -08001543#define ABIT_UGURU_PM (&abituguru_pm)
Hans de Goede360b9ab2006-08-28 14:42:24 +02001544#else
Rafael J. Wysocki4f0928c2012-07-07 23:59:12 +02001545#define ABIT_UGURU_PM NULL
Hans de Goede360b9ab2006-08-28 14:42:24 +02001546#endif /* CONFIG_PM */
1547
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001548static struct platform_driver abituguru_driver = {
1549 .driver = {
1550 .owner = THIS_MODULE,
1551 .name = ABIT_UGURU_NAME,
Rafael J. Wysocki4f0928c2012-07-07 23:59:12 +02001552 .pm = ABIT_UGURU_PM,
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001553 },
Hans de Goede360b9ab2006-08-28 14:42:24 +02001554 .probe = abituguru_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -05001555 .remove = abituguru_remove,
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001556};
1557
1558static int __init abituguru_detect(void)
1559{
Guenter Roeck1bd385d2012-01-14 12:30:52 -08001560 /*
1561 * See if there is an uguru there. After a reboot uGuru will hold 0x00
1562 * at DATA and 0xAC, when this driver has already been loaded once
1563 * DATA will hold 0x08. For most uGuru's CMD will hold 0xAC in either
1564 * scenario but some will hold 0x00.
1565 * Some uGuru's initially hold 0x09 at DATA and will only hold 0x08
1566 * after reading CMD first, so CMD must be read first!
1567 */
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001568 u8 cmd_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_CMD);
1569 u8 data_val = inb_p(ABIT_UGURU_BASE + ABIT_UGURU_DATA);
1570 if (((data_val == 0x00) || (data_val == 0x08)) &&
1571 ((cmd_val == 0x00) || (cmd_val == 0xAC)))
1572 return ABIT_UGURU_BASE;
1573
1574 ABIT_UGURU_DEBUG(2, "no Abit uGuru found, data = 0x%02X, cmd = "
1575 "0x%02X\n", (unsigned int)data_val, (unsigned int)cmd_val);
1576
1577 if (force) {
Joe Perches28ebfa12010-10-20 06:51:25 +00001578 pr_info("Assuming Abit uGuru is present because of \"force\" parameter\n");
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001579 return ABIT_UGURU_BASE;
1580 }
1581
1582 /* No uGuru found */
1583 return -ENODEV;
1584}
1585
1586static struct platform_device *abituguru_pdev;
1587
1588static int __init abituguru_init(void)
1589{
1590 int address, err;
1591 struct resource res = { .flags = IORESOURCE_IO };
Jeff Garzik18552562007-10-03 15:15:40 -04001592 const char *board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
Hans de Goedec182f5b2007-07-10 17:09:57 +02001593
1594 /* safety check, refuse to load on non Abit motherboards */
1595 if (!force && (!board_vendor ||
1596 strcmp(board_vendor, "http://www.abit.com.tw/")))
1597 return -ENODEV;
Hans de Goedec182f5b2007-07-10 17:09:57 +02001598
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001599 address = abituguru_detect();
1600 if (address < 0)
1601 return address;
1602
1603 err = platform_driver_register(&abituguru_driver);
1604 if (err)
1605 goto exit;
1606
1607 abituguru_pdev = platform_device_alloc(ABIT_UGURU_NAME, address);
1608 if (!abituguru_pdev) {
Joe Perches28ebfa12010-10-20 06:51:25 +00001609 pr_err("Device allocation failed\n");
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001610 err = -ENOMEM;
1611 goto exit_driver_unregister;
1612 }
1613
1614 res.start = address;
1615 res.end = address + ABIT_UGURU_REGION_LENGTH - 1;
1616 res.name = ABIT_UGURU_NAME;
1617
1618 err = platform_device_add_resources(abituguru_pdev, &res, 1);
1619 if (err) {
Joe Perches28ebfa12010-10-20 06:51:25 +00001620 pr_err("Device resource addition failed (%d)\n", err);
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001621 goto exit_device_put;
1622 }
1623
1624 err = platform_device_add(abituguru_pdev);
1625 if (err) {
Joe Perches28ebfa12010-10-20 06:51:25 +00001626 pr_err("Device addition failed (%d)\n", err);
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001627 goto exit_device_put;
1628 }
1629
1630 return 0;
1631
1632exit_device_put:
1633 platform_device_put(abituguru_pdev);
1634exit_driver_unregister:
1635 platform_driver_unregister(&abituguru_driver);
1636exit:
1637 return err;
1638}
1639
1640static void __exit abituguru_exit(void)
1641{
1642 platform_device_unregister(abituguru_pdev);
1643 platform_driver_unregister(&abituguru_driver);
1644}
1645
Hans de Goede93d0cc52011-03-21 17:59:36 +01001646MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
Hans de Goedef2b84bb2006-06-04 20:22:24 +02001647MODULE_DESCRIPTION("Abit uGuru Sensor device");
1648MODULE_LICENSE("GPL");
1649
1650module_init(abituguru_init);
1651module_exit(abituguru_exit);