blob: ec683eabd7ac77af840bc6569acacfb36ee7a120 [file] [log] [blame]
Juerg Haefliger94319962007-06-09 10:11:16 -04001/*
Juerg Haefligerea694432010-05-27 19:59:01 +02002 * dme1737.c - Driver for the SMSC DME1737, Asus A8000, SMSC SCH311x, SCH5027,
3 * and SCH5127 Super-I/O chips integrated hardware monitoring
4 * features.
5 * Copyright (c) 2007, 2008, 2009, 2010 Juerg Haefliger <juergh@gmail.com>
Juerg Haefliger94319962007-06-09 10:11:16 -04006 *
Juerg Haefligere95c2372007-10-07 21:27:35 -07007 * This driver is an I2C/ISA hybrid, meaning that it uses the I2C bus to access
Juerg Haefliger549edb82008-08-06 22:41:03 +02008 * the chip registers if a DME1737, A8000, or SCH5027 is found and the ISA bus
Juerg Haefligerea694432010-05-27 19:59:01 +02009 * if a SCH311x or SCH5127 chip is found. Both types of chips have very
10 * similar hardware monitoring capabilities but differ in the way they can be
11 * accessed.
Juerg Haefliger94319962007-06-09 10:11:16 -040012 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
Joe Perches9c6e13b2010-10-20 06:51:32 +000028#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29
Juerg Haefliger94319962007-06-09 10:11:16 -040030#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/slab.h>
33#include <linux/jiffies.h>
34#include <linux/i2c.h>
Juerg Haefligere95c2372007-10-07 21:27:35 -070035#include <linux/platform_device.h>
Juerg Haefliger94319962007-06-09 10:11:16 -040036#include <linux/hwmon.h>
37#include <linux/hwmon-sysfs.h>
38#include <linux/hwmon-vid.h>
39#include <linux/err.h>
40#include <linux/mutex.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010041#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020042#include <linux/io.h>
Juerg Haefliger94319962007-06-09 10:11:16 -040043
Juerg Haefligere95c2372007-10-07 21:27:35 -070044/* ISA device, if found */
45static struct platform_device *pdev;
46
Juerg Haefliger94319962007-06-09 10:11:16 -040047/* Module load parameters */
48static int force_start;
49module_param(force_start, bool, 0);
50MODULE_PARM_DESC(force_start, "Force the chip to start monitoring inputs");
51
Jean Delvare67b671b2007-12-06 23:13:42 +010052static unsigned short force_id;
53module_param(force_id, ushort, 0);
54MODULE_PARM_DESC(force_id, "Override the detected device ID");
55
Juerg Haefliger92430b62008-04-03 21:34:19 -070056static int probe_all_addr;
57module_param(probe_all_addr, bool, 0);
58MODULE_PARM_DESC(probe_all_addr, "Include probing of non-standard LPC "
59 "addresses");
60
Juerg Haefliger94319962007-06-09 10:11:16 -040061/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050062static const unsigned short normal_i2c[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END};
Juerg Haefliger94319962007-06-09 10:11:16 -040063
Juerg Haefligerea694432010-05-27 19:59:01 +020064enum chips { dme1737, sch5027, sch311x, sch5127 };
Juerg Haefliger94319962007-06-09 10:11:16 -040065
66/* ---------------------------------------------------------------------
67 * Registers
68 *
69 * The sensors are defined as follows:
70 *
71 * Voltages Temperatures
72 * -------- ------------
73 * in0 +5VTR (+5V stdby) temp1 Remote diode 1
74 * in1 Vccp (proc core) temp2 Internal temp
75 * in2 VCC (internal +3.3V) temp3 Remote diode 2
76 * in3 +5V
77 * in4 +12V
78 * in5 VTR (+3.3V stby)
79 * in6 Vbat
Juerg Haefligerd4b94e12011-01-12 21:55:13 +010080 * in7 Vtrip (sch5127 only)
Juerg Haefliger94319962007-06-09 10:11:16 -040081 *
82 * --------------------------------------------------------------------- */
83
Juerg Haefligerd4b94e12011-01-12 21:55:13 +010084/* Voltages (in) numbered 0-7 (ix) */
85#define DME1737_REG_IN(ix) ((ix) < 5 ? 0x20 + (ix) : \
86 (ix) < 7 ? 0x94 + (ix) : \
87 0x1f)
Juerg Haefliger94319962007-06-09 10:11:16 -040088#define DME1737_REG_IN_MIN(ix) ((ix) < 5 ? 0x44 + (ix) * 2 \
89 : 0x91 + (ix) * 2)
90#define DME1737_REG_IN_MAX(ix) ((ix) < 5 ? 0x45 + (ix) * 2 \
91 : 0x92 + (ix) * 2)
92
93/* Temperatures (temp) numbered 0-2 (ix) */
94#define DME1737_REG_TEMP(ix) (0x25 + (ix))
95#define DME1737_REG_TEMP_MIN(ix) (0x4e + (ix) * 2)
96#define DME1737_REG_TEMP_MAX(ix) (0x4f + (ix) * 2)
97#define DME1737_REG_TEMP_OFFSET(ix) ((ix) == 0 ? 0x1f \
98 : 0x1c + (ix))
99
100/* Voltage and temperature LSBs
101 * The LSBs (4 bits each) are stored in 5 registers with the following layouts:
102 * IN_TEMP_LSB(0) = [in5, in6]
103 * IN_TEMP_LSB(1) = [temp3, temp1]
104 * IN_TEMP_LSB(2) = [in4, temp2]
105 * IN_TEMP_LSB(3) = [in3, in0]
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100106 * IN_TEMP_LSB(4) = [in2, in1]
107 * IN_TEMP_LSB(5) = [res, in7] */
Juerg Haefliger94319962007-06-09 10:11:16 -0400108#define DME1737_REG_IN_TEMP_LSB(ix) (0x84 + (ix))
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100109static const u8 DME1737_REG_IN_LSB[] = {3, 4, 4, 3, 2, 0, 0, 5};
110static const u8 DME1737_REG_IN_LSB_SHL[] = {4, 4, 0, 0, 0, 0, 4, 4};
Juerg Haefliger94319962007-06-09 10:11:16 -0400111static const u8 DME1737_REG_TEMP_LSB[] = {1, 2, 1};
112static const u8 DME1737_REG_TEMP_LSB_SHL[] = {4, 4, 0};
113
114/* Fans numbered 0-5 (ix) */
115#define DME1737_REG_FAN(ix) ((ix) < 4 ? 0x28 + (ix) * 2 \
116 : 0xa1 + (ix) * 2)
117#define DME1737_REG_FAN_MIN(ix) ((ix) < 4 ? 0x54 + (ix) * 2 \
118 : 0xa5 + (ix) * 2)
119#define DME1737_REG_FAN_OPT(ix) ((ix) < 4 ? 0x90 + (ix) \
120 : 0xb2 + (ix))
121#define DME1737_REG_FAN_MAX(ix) (0xb4 + (ix)) /* only for fan[4-5] */
122
123/* PWMs numbered 0-2, 4-5 (ix) */
124#define DME1737_REG_PWM(ix) ((ix) < 3 ? 0x30 + (ix) \
125 : 0xa1 + (ix))
126#define DME1737_REG_PWM_CONFIG(ix) (0x5c + (ix)) /* only for pwm[0-2] */
127#define DME1737_REG_PWM_MIN(ix) (0x64 + (ix)) /* only for pwm[0-2] */
128#define DME1737_REG_PWM_FREQ(ix) ((ix) < 3 ? 0x5f + (ix) \
129 : 0xa3 + (ix))
130/* The layout of the ramp rate registers is different from the other pwm
131 * registers. The bits for the 3 PWMs are stored in 2 registers:
132 * PWM_RR(0) = [OFF3, OFF2, OFF1, RES, RR1E, RR1-2, RR1-1, RR1-0]
133 * PWM_RR(1) = [RR2E, RR2-2, RR2-1, RR2-0, RR3E, RR3-2, RR3-1, RR3-0] */
134#define DME1737_REG_PWM_RR(ix) (0x62 + (ix)) /* only for pwm[0-2] */
135
136/* Thermal zones 0-2 */
137#define DME1737_REG_ZONE_LOW(ix) (0x67 + (ix))
138#define DME1737_REG_ZONE_ABS(ix) (0x6a + (ix))
139/* The layout of the hysteresis registers is different from the other zone
140 * registers. The bits for the 3 zones are stored in 2 registers:
141 * ZONE_HYST(0) = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
142 * ZONE_HYST(1) = [H3-3, H3-2, H3-1, H3-0, RES, RES, RES, RES] */
143#define DME1737_REG_ZONE_HYST(ix) (0x6d + (ix))
144
145/* Alarm registers and bit mapping
146 * The 3 8-bit alarm registers will be concatenated to a single 32-bit
147 * alarm value [0, ALARM3, ALARM2, ALARM1]. */
148#define DME1737_REG_ALARM1 0x41
149#define DME1737_REG_ALARM2 0x42
150#define DME1737_REG_ALARM3 0x83
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100151static const u8 DME1737_BIT_ALARM_IN[] = {0, 1, 2, 3, 8, 16, 17, 18};
Juerg Haefliger94319962007-06-09 10:11:16 -0400152static const u8 DME1737_BIT_ALARM_TEMP[] = {4, 5, 6};
153static const u8 DME1737_BIT_ALARM_FAN[] = {10, 11, 12, 13, 22, 23};
154
155/* Miscellaneous registers */
Juerg Haefligere95c2372007-10-07 21:27:35 -0700156#define DME1737_REG_DEVICE 0x3d
Juerg Haefliger94319962007-06-09 10:11:16 -0400157#define DME1737_REG_COMPANY 0x3e
158#define DME1737_REG_VERSTEP 0x3f
159#define DME1737_REG_CONFIG 0x40
160#define DME1737_REG_CONFIG2 0x7f
161#define DME1737_REG_VID 0x43
162#define DME1737_REG_TACH_PWM 0x81
163
164/* ---------------------------------------------------------------------
165 * Misc defines
166 * --------------------------------------------------------------------- */
167
168/* Chip identification */
169#define DME1737_COMPANY_SMSC 0x5c
170#define DME1737_VERSTEP 0x88
171#define DME1737_VERSTEP_MASK 0xf8
Juerg Haefligere95c2372007-10-07 21:27:35 -0700172#define SCH311X_DEVICE 0x8c
Juerg Haefliger549edb82008-08-06 22:41:03 +0200173#define SCH5027_VERSTEP 0x69
Juerg Haefligerea694432010-05-27 19:59:01 +0200174#define SCH5127_DEVICE 0x8e
175
176/* Device ID values (global configuration register index 0x20) */
177#define DME1737_ID_1 0x77
178#define DME1737_ID_2 0x78
179#define SCH3112_ID 0x7c
180#define SCH3114_ID 0x7d
181#define SCH3116_ID 0x7f
182#define SCH5027_ID 0x89
183#define SCH5127_ID 0x86
Juerg Haefligere95c2372007-10-07 21:27:35 -0700184
185/* Length of ISA address segment */
186#define DME1737_EXTENT 2
Juerg Haefliger94319962007-06-09 10:11:16 -0400187
Juerg Haefligerea694432010-05-27 19:59:01 +0200188/* chip-dependent features */
189#define HAS_TEMP_OFFSET (1 << 0) /* bit 0 */
190#define HAS_VID (1 << 1) /* bit 1 */
191#define HAS_ZONE3 (1 << 2) /* bit 2 */
192#define HAS_ZONE_HYST (1 << 3) /* bit 3 */
193#define HAS_PWM_MIN (1 << 4) /* bit 4 */
194#define HAS_FAN(ix) (1 << ((ix) + 5)) /* bits 5-10 */
195#define HAS_PWM(ix) (1 << ((ix) + 11)) /* bits 11-16 */
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100196#define HAS_IN7 (1 << 17) /* bit 17 */
Juerg Haefligerea694432010-05-27 19:59:01 +0200197
Juerg Haefliger94319962007-06-09 10:11:16 -0400198/* ---------------------------------------------------------------------
199 * Data structures and manipulation thereof
200 * --------------------------------------------------------------------- */
201
202struct dme1737_data {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200203 struct i2c_client *client; /* for I2C devices only */
Tony Jones1beeffe2007-08-20 13:46:20 -0700204 struct device *hwmon_dev;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200205 const char *name;
206 unsigned int addr; /* for ISA devices only */
Juerg Haefliger94319962007-06-09 10:11:16 -0400207
208 struct mutex update_lock;
209 int valid; /* !=0 if following fields are valid */
210 unsigned long last_update; /* in jiffies */
211 unsigned long last_vbat; /* in jiffies */
Juerg Haefligerf994fb22008-03-25 21:49:15 -0700212 enum chips type;
Juerg Haefliger549edb82008-08-06 22:41:03 +0200213 const int *in_nominal; /* pointer to IN_NOMINAL array */
Juerg Haefliger94319962007-06-09 10:11:16 -0400214
215 u8 vid;
216 u8 pwm_rr_en;
Juerg Haefligerea694432010-05-27 19:59:01 +0200217 u32 has_features;
Juerg Haefliger94319962007-06-09 10:11:16 -0400218
219 /* Register values */
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100220 u16 in[8];
221 u8 in_min[8];
222 u8 in_max[8];
Juerg Haefliger94319962007-06-09 10:11:16 -0400223 s16 temp[3];
224 s8 temp_min[3];
225 s8 temp_max[3];
226 s8 temp_offset[3];
227 u8 config;
228 u8 config2;
229 u8 vrm;
230 u16 fan[6];
231 u16 fan_min[6];
232 u8 fan_max[2];
233 u8 fan_opt[6];
234 u8 pwm[6];
235 u8 pwm_min[3];
236 u8 pwm_config[3];
237 u8 pwm_acz[3];
238 u8 pwm_freq[6];
239 u8 pwm_rr[2];
240 u8 zone_low[3];
241 u8 zone_abs[3];
242 u8 zone_hyst[2];
243 u32 alarms;
244};
245
246/* Nominal voltage values */
Juerg Haefligerf994fb22008-03-25 21:49:15 -0700247static const int IN_NOMINAL_DME1737[] = {5000, 2250, 3300, 5000, 12000, 3300,
248 3300};
249static const int IN_NOMINAL_SCH311x[] = {2500, 1500, 3300, 5000, 12000, 3300,
250 3300};
Juerg Haefliger549edb82008-08-06 22:41:03 +0200251static const int IN_NOMINAL_SCH5027[] = {5000, 2250, 3300, 1125, 1125, 3300,
252 3300};
Juerg Haefligerea694432010-05-27 19:59:01 +0200253static const int IN_NOMINAL_SCH5127[] = {2500, 2250, 3300, 1125, 1125, 3300,
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100254 3300, 1500};
Juerg Haefliger549edb82008-08-06 22:41:03 +0200255#define IN_NOMINAL(type) ((type) == sch311x ? IN_NOMINAL_SCH311x : \
256 (type) == sch5027 ? IN_NOMINAL_SCH5027 : \
Juerg Haefligerea694432010-05-27 19:59:01 +0200257 (type) == sch5127 ? IN_NOMINAL_SCH5127 : \
Juerg Haefliger549edb82008-08-06 22:41:03 +0200258 IN_NOMINAL_DME1737)
Juerg Haefliger94319962007-06-09 10:11:16 -0400259
260/* Voltage input
261 * Voltage inputs have 16 bits resolution, limit values have 8 bits
262 * resolution. */
Juerg Haefliger549edb82008-08-06 22:41:03 +0200263static inline int IN_FROM_REG(int reg, int nominal, int res)
Juerg Haefliger94319962007-06-09 10:11:16 -0400264{
Juerg Haefliger549edb82008-08-06 22:41:03 +0200265 return (reg * nominal + (3 << (res - 3))) / (3 << (res - 2));
Juerg Haefliger94319962007-06-09 10:11:16 -0400266}
267
Juerg Haefliger549edb82008-08-06 22:41:03 +0200268static inline int IN_TO_REG(int val, int nominal)
Juerg Haefliger94319962007-06-09 10:11:16 -0400269{
Juerg Haefliger549edb82008-08-06 22:41:03 +0200270 return SENSORS_LIMIT((val * 192 + nominal / 2) / nominal, 0, 255);
Juerg Haefliger94319962007-06-09 10:11:16 -0400271}
272
273/* Temperature input
274 * The register values represent temperatures in 2's complement notation from
275 * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit
276 * values have 8 bits resolution. */
277static inline int TEMP_FROM_REG(int reg, int res)
278{
279 return (reg * 1000) >> (res - 8);
280}
281
282static inline int TEMP_TO_REG(int val)
283{
284 return SENSORS_LIMIT((val < 0 ? val - 500 : val + 500) / 1000,
285 -128, 127);
286}
287
288/* Temperature range */
289static const int TEMP_RANGE[] = {2000, 2500, 3333, 4000, 5000, 6666, 8000,
290 10000, 13333, 16000, 20000, 26666, 32000,
291 40000, 53333, 80000};
292
293static inline int TEMP_RANGE_FROM_REG(int reg)
294{
295 return TEMP_RANGE[(reg >> 4) & 0x0f];
296}
297
298static int TEMP_RANGE_TO_REG(int val, int reg)
299{
300 int i;
301
302 for (i = 15; i > 0; i--) {
303 if (val > (TEMP_RANGE[i] + TEMP_RANGE[i - 1] + 1) / 2) {
304 break;
305 }
306 }
307
308 return (reg & 0x0f) | (i << 4);
309}
310
311/* Temperature hysteresis
312 * Register layout:
313 * reg[0] = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
314 * reg[1] = [H3-3, H3-2, H3-1, H3-0, xxxx, xxxx, xxxx, xxxx] */
315static inline int TEMP_HYST_FROM_REG(int reg, int ix)
316{
317 return (((ix == 1) ? reg : reg >> 4) & 0x0f) * 1000;
318}
319
320static inline int TEMP_HYST_TO_REG(int val, int ix, int reg)
321{
322 int hyst = SENSORS_LIMIT((val + 500) / 1000, 0, 15);
323
324 return (ix == 1) ? (reg & 0xf0) | hyst : (reg & 0x0f) | (hyst << 4);
325}
326
327/* Fan input RPM */
328static inline int FAN_FROM_REG(int reg, int tpc)
329{
Juerg Haefligerff8421f2008-01-27 16:39:46 -0800330 if (tpc) {
331 return tpc * reg;
332 } else {
333 return (reg == 0 || reg == 0xffff) ? 0 : 90000 * 60 / reg;
334 }
Juerg Haefliger94319962007-06-09 10:11:16 -0400335}
336
337static inline int FAN_TO_REG(int val, int tpc)
338{
Juerg Haefligerff8421f2008-01-27 16:39:46 -0800339 if (tpc) {
340 return SENSORS_LIMIT(val / tpc, 0, 0xffff);
341 } else {
342 return (val <= 0) ? 0xffff :
343 SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe);
344 }
Juerg Haefliger94319962007-06-09 10:11:16 -0400345}
346
347/* Fan TPC (tach pulse count)
348 * Converts a register value to a TPC multiplier or returns 0 if the tachometer
349 * is configured in legacy (non-tpc) mode */
350static inline int FAN_TPC_FROM_REG(int reg)
351{
352 return (reg & 0x20) ? 0 : 60 >> (reg & 0x03);
353}
354
355/* Fan type
356 * The type of a fan is expressed in number of pulses-per-revolution that it
357 * emits */
358static inline int FAN_TYPE_FROM_REG(int reg)
359{
360 int edge = (reg >> 1) & 0x03;
361
362 return (edge > 0) ? 1 << (edge - 1) : 0;
363}
364
365static inline int FAN_TYPE_TO_REG(int val, int reg)
366{
367 int edge = (val == 4) ? 3 : val;
368
369 return (reg & 0xf9) | (edge << 1);
370}
371
372/* Fan max RPM */
373static const int FAN_MAX[] = {0x54, 0x38, 0x2a, 0x21, 0x1c, 0x18, 0x15, 0x12,
374 0x11, 0x0f, 0x0e};
375
376static int FAN_MAX_FROM_REG(int reg)
377{
378 int i;
379
380 for (i = 10; i > 0; i--) {
381 if (reg == FAN_MAX[i]) {
382 break;
383 }
384 }
385
386 return 1000 + i * 500;
387}
388
389static int FAN_MAX_TO_REG(int val)
390{
391 int i;
392
393 for (i = 10; i > 0; i--) {
394 if (val > (1000 + (i - 1) * 500)) {
395 break;
396 }
397 }
398
399 return FAN_MAX[i];
400}
401
402/* PWM enable
403 * Register to enable mapping:
404 * 000: 2 fan on zone 1 auto
405 * 001: 2 fan on zone 2 auto
406 * 010: 2 fan on zone 3 auto
407 * 011: 0 fan full on
408 * 100: -1 fan disabled
409 * 101: 2 fan on hottest of zones 2,3 auto
410 * 110: 2 fan on hottest of zones 1,2,3 auto
411 * 111: 1 fan in manual mode */
412static inline int PWM_EN_FROM_REG(int reg)
413{
414 static const int en[] = {2, 2, 2, 0, -1, 2, 2, 1};
415
416 return en[(reg >> 5) & 0x07];
417}
418
419static inline int PWM_EN_TO_REG(int val, int reg)
420{
421 int en = (val == 1) ? 7 : 3;
422
423 return (reg & 0x1f) | ((en & 0x07) << 5);
424}
425
426/* PWM auto channels zone
427 * Register to auto channels zone mapping (ACZ is a bitfield with bit x
428 * corresponding to zone x+1):
429 * 000: 001 fan on zone 1 auto
430 * 001: 010 fan on zone 2 auto
431 * 010: 100 fan on zone 3 auto
432 * 011: 000 fan full on
433 * 100: 000 fan disabled
434 * 101: 110 fan on hottest of zones 2,3 auto
435 * 110: 111 fan on hottest of zones 1,2,3 auto
436 * 111: 000 fan in manual mode */
437static inline int PWM_ACZ_FROM_REG(int reg)
438{
439 static const int acz[] = {1, 2, 4, 0, 0, 6, 7, 0};
440
441 return acz[(reg >> 5) & 0x07];
442}
443
444static inline int PWM_ACZ_TO_REG(int val, int reg)
445{
446 int acz = (val == 4) ? 2 : val - 1;
447
448 return (reg & 0x1f) | ((acz & 0x07) << 5);
449}
450
451/* PWM frequency */
452static const int PWM_FREQ[] = {11, 15, 22, 29, 35, 44, 59, 88,
453 15000, 20000, 30000, 25000, 0, 0, 0, 0};
454
455static inline int PWM_FREQ_FROM_REG(int reg)
456{
457 return PWM_FREQ[reg & 0x0f];
458}
459
460static int PWM_FREQ_TO_REG(int val, int reg)
461{
462 int i;
463
464 /* the first two cases are special - stupid chip design! */
465 if (val > 27500) {
466 i = 10;
467 } else if (val > 22500) {
468 i = 11;
469 } else {
470 for (i = 9; i > 0; i--) {
471 if (val > (PWM_FREQ[i] + PWM_FREQ[i - 1] + 1) / 2) {
472 break;
473 }
474 }
475 }
476
477 return (reg & 0xf0) | i;
478}
479
480/* PWM ramp rate
481 * Register layout:
482 * reg[0] = [OFF3, OFF2, OFF1, RES, RR1-E, RR1-2, RR1-1, RR1-0]
483 * reg[1] = [RR2-E, RR2-2, RR2-1, RR2-0, RR3-E, RR3-2, RR3-1, RR3-0] */
484static const u8 PWM_RR[] = {206, 104, 69, 41, 26, 18, 10, 5};
485
486static inline int PWM_RR_FROM_REG(int reg, int ix)
487{
488 int rr = (ix == 1) ? reg >> 4 : reg;
489
490 return (rr & 0x08) ? PWM_RR[rr & 0x07] : 0;
491}
492
493static int PWM_RR_TO_REG(int val, int ix, int reg)
494{
495 int i;
496
497 for (i = 0; i < 7; i++) {
498 if (val > (PWM_RR[i] + PWM_RR[i + 1] + 1) / 2) {
499 break;
500 }
501 }
502
503 return (ix == 1) ? (reg & 0x8f) | (i << 4) : (reg & 0xf8) | i;
504}
505
506/* PWM ramp rate enable */
507static inline int PWM_RR_EN_FROM_REG(int reg, int ix)
508{
509 return PWM_RR_FROM_REG(reg, ix) ? 1 : 0;
510}
511
512static inline int PWM_RR_EN_TO_REG(int val, int ix, int reg)
513{
514 int en = (ix == 1) ? 0x80 : 0x08;
515
516 return val ? reg | en : reg & ~en;
517}
518
519/* PWM min/off
520 * The PWM min/off bits are part of the PMW ramp rate register 0 (see above for
521 * the register layout). */
522static inline int PWM_OFF_FROM_REG(int reg, int ix)
523{
524 return (reg >> (ix + 5)) & 0x01;
525}
526
527static inline int PWM_OFF_TO_REG(int val, int ix, int reg)
528{
529 return (reg & ~(1 << (ix + 5))) | ((val & 0x01) << (ix + 5));
530}
531
532/* ---------------------------------------------------------------------
533 * Device I/O access
Juerg Haefligere95c2372007-10-07 21:27:35 -0700534 *
535 * ISA access is performed through an index/data register pair and needs to
536 * be protected by a mutex during runtime (not required for initialization).
537 * We use data->update_lock for this and need to ensure that we acquire it
538 * before calling dme1737_read or dme1737_write.
Juerg Haefliger94319962007-06-09 10:11:16 -0400539 * --------------------------------------------------------------------- */
540
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200541static u8 dme1737_read(const struct dme1737_data *data, u8 reg)
Juerg Haefliger94319962007-06-09 10:11:16 -0400542{
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200543 struct i2c_client *client = data->client;
Juerg Haefligere95c2372007-10-07 21:27:35 -0700544 s32 val;
Juerg Haefliger94319962007-06-09 10:11:16 -0400545
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200546 if (client) { /* I2C device */
Juerg Haefligere95c2372007-10-07 21:27:35 -0700547 val = i2c_smbus_read_byte_data(client, reg);
548
549 if (val < 0) {
550 dev_warn(&client->dev, "Read from register "
551 "0x%02x failed! Please report to the driver "
552 "maintainer.\n", reg);
553 }
554 } else { /* ISA device */
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200555 outb(reg, data->addr);
556 val = inb(data->addr + 1);
Juerg Haefliger94319962007-06-09 10:11:16 -0400557 }
558
559 return val;
560}
561
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200562static s32 dme1737_write(const struct dme1737_data *data, u8 reg, u8 val)
Juerg Haefliger94319962007-06-09 10:11:16 -0400563{
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200564 struct i2c_client *client = data->client;
Juerg Haefligere95c2372007-10-07 21:27:35 -0700565 s32 res = 0;
Juerg Haefliger94319962007-06-09 10:11:16 -0400566
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200567 if (client) { /* I2C device */
Juerg Haefligere95c2372007-10-07 21:27:35 -0700568 res = i2c_smbus_write_byte_data(client, reg, val);
569
570 if (res < 0) {
571 dev_warn(&client->dev, "Write to register "
572 "0x%02x failed! Please report to the driver "
573 "maintainer.\n", reg);
574 }
575 } else { /* ISA device */
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200576 outb(reg, data->addr);
577 outb(val, data->addr + 1);
Juerg Haefliger94319962007-06-09 10:11:16 -0400578 }
579
580 return res;
581}
582
583static struct dme1737_data *dme1737_update_device(struct device *dev)
584{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700585 struct dme1737_data *data = dev_get_drvdata(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -0400586 int ix;
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100587 u8 lsb[6];
Juerg Haefliger94319962007-06-09 10:11:16 -0400588
589 mutex_lock(&data->update_lock);
590
591 /* Enable a Vbat monitoring cycle every 10 mins */
592 if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200593 dme1737_write(data, DME1737_REG_CONFIG, dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400594 DME1737_REG_CONFIG) | 0x10);
595 data->last_vbat = jiffies;
596 }
597
598 /* Sample register contents every 1 sec */
599 if (time_after(jiffies, data->last_update + HZ) || !data->valid) {
Juerg Haefligerea694432010-05-27 19:59:01 +0200600 if (data->has_features & HAS_VID) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200601 data->vid = dme1737_read(data, DME1737_REG_VID) &
Juerg Haefliger549edb82008-08-06 22:41:03 +0200602 0x3f;
603 }
Juerg Haefliger94319962007-06-09 10:11:16 -0400604
605 /* In (voltage) registers */
606 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
607 /* Voltage inputs are stored as 16 bit values even
608 * though they have only 12 bits resolution. This is
609 * to make it consistent with the temp inputs. */
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100610 if (ix == 7 && !(data->has_features & HAS_IN7)) {
611 continue;
612 }
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200613 data->in[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400614 DME1737_REG_IN(ix)) << 8;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200615 data->in_min[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400616 DME1737_REG_IN_MIN(ix));
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200617 data->in_max[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400618 DME1737_REG_IN_MAX(ix));
619 }
620
621 /* Temp registers */
622 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
623 /* Temp inputs are stored as 16 bit values even
624 * though they have only 12 bits resolution. This is
625 * to take advantage of implicit conversions between
626 * register values (2's complement) and temp values
627 * (signed decimal). */
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200628 data->temp[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400629 DME1737_REG_TEMP(ix)) << 8;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200630 data->temp_min[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400631 DME1737_REG_TEMP_MIN(ix));
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200632 data->temp_max[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400633 DME1737_REG_TEMP_MAX(ix));
Juerg Haefligerea694432010-05-27 19:59:01 +0200634 if (data->has_features & HAS_TEMP_OFFSET) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200635 data->temp_offset[ix] = dme1737_read(data,
Juerg Haefliger549edb82008-08-06 22:41:03 +0200636 DME1737_REG_TEMP_OFFSET(ix));
637 }
Juerg Haefliger94319962007-06-09 10:11:16 -0400638 }
639
640 /* In and temp LSB registers
641 * The LSBs are latched when the MSBs are read, so the order in
642 * which the registers are read (MSB first, then LSB) is
643 * important! */
644 for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) {
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100645 if (ix == 5 && !(data->has_features & HAS_IN7)) {
646 continue;
647 }
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200648 lsb[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400649 DME1737_REG_IN_TEMP_LSB(ix));
650 }
651 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100652 if (ix == 7 && !(data->has_features & HAS_IN7)) {
653 continue;
654 }
Juerg Haefliger94319962007-06-09 10:11:16 -0400655 data->in[ix] |= (lsb[DME1737_REG_IN_LSB[ix]] <<
656 DME1737_REG_IN_LSB_SHL[ix]) & 0xf0;
657 }
658 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
659 data->temp[ix] |= (lsb[DME1737_REG_TEMP_LSB[ix]] <<
660 DME1737_REG_TEMP_LSB_SHL[ix]) & 0xf0;
661 }
662
663 /* Fan registers */
664 for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
665 /* Skip reading registers if optional fans are not
666 * present */
Juerg Haefligerea694432010-05-27 19:59:01 +0200667 if (!(data->has_features & HAS_FAN(ix))) {
Juerg Haefliger94319962007-06-09 10:11:16 -0400668 continue;
669 }
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200670 data->fan[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400671 DME1737_REG_FAN(ix));
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200672 data->fan[ix] |= dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400673 DME1737_REG_FAN(ix) + 1) << 8;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200674 data->fan_min[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400675 DME1737_REG_FAN_MIN(ix));
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200676 data->fan_min[ix] |= dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400677 DME1737_REG_FAN_MIN(ix) + 1) << 8;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200678 data->fan_opt[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400679 DME1737_REG_FAN_OPT(ix));
680 /* fan_max exists only for fan[5-6] */
681 if (ix > 3) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200682 data->fan_max[ix - 4] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400683 DME1737_REG_FAN_MAX(ix));
684 }
685 }
686
687 /* PWM registers */
688 for (ix = 0; ix < ARRAY_SIZE(data->pwm); ix++) {
689 /* Skip reading registers if optional PWMs are not
690 * present */
Juerg Haefligerea694432010-05-27 19:59:01 +0200691 if (!(data->has_features & HAS_PWM(ix))) {
Juerg Haefliger94319962007-06-09 10:11:16 -0400692 continue;
693 }
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200694 data->pwm[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400695 DME1737_REG_PWM(ix));
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200696 data->pwm_freq[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400697 DME1737_REG_PWM_FREQ(ix));
698 /* pwm_config and pwm_min exist only for pwm[1-3] */
699 if (ix < 3) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200700 data->pwm_config[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400701 DME1737_REG_PWM_CONFIG(ix));
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200702 data->pwm_min[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400703 DME1737_REG_PWM_MIN(ix));
704 }
705 }
706 for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200707 data->pwm_rr[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400708 DME1737_REG_PWM_RR(ix));
709 }
710
711 /* Thermal zone registers */
712 for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) {
Juerg Haefligerea694432010-05-27 19:59:01 +0200713 /* Skip reading registers if zone3 is not present */
714 if ((ix == 2) && !(data->has_features & HAS_ZONE3)) {
715 continue;
716 }
717 /* sch5127 zone2 registers are special */
718 if ((ix == 1) && (data->type == sch5127)) {
719 data->zone_low[1] = dme1737_read(data,
720 DME1737_REG_ZONE_LOW(2));
721 data->zone_abs[1] = dme1737_read(data,
722 DME1737_REG_ZONE_ABS(2));
723 } else {
724 data->zone_low[ix] = dme1737_read(data,
725 DME1737_REG_ZONE_LOW(ix));
726 data->zone_abs[ix] = dme1737_read(data,
727 DME1737_REG_ZONE_ABS(ix));
728 }
Juerg Haefliger94319962007-06-09 10:11:16 -0400729 }
Juerg Haefligerea694432010-05-27 19:59:01 +0200730 if (data->has_features & HAS_ZONE_HYST) {
Juerg Haefliger549edb82008-08-06 22:41:03 +0200731 for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200732 data->zone_hyst[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400733 DME1737_REG_ZONE_HYST(ix));
Juerg Haefliger549edb82008-08-06 22:41:03 +0200734 }
Juerg Haefliger94319962007-06-09 10:11:16 -0400735 }
736
737 /* Alarm registers */
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200738 data->alarms = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400739 DME1737_REG_ALARM1);
740 /* Bit 7 tells us if the other alarm registers are non-zero and
741 * therefore also need to be read */
742 if (data->alarms & 0x80) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200743 data->alarms |= dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400744 DME1737_REG_ALARM2) << 8;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200745 data->alarms |= dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400746 DME1737_REG_ALARM3) << 16;
747 }
748
Juerg Haefligere95c2372007-10-07 21:27:35 -0700749 /* The ISA chips require explicit clearing of alarm bits.
750 * Don't worry, an alarm will come back if the condition
751 * that causes it still exists */
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200752 if (!data->client) {
Juerg Haefligere95c2372007-10-07 21:27:35 -0700753 if (data->alarms & 0xff0000) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200754 dme1737_write(data, DME1737_REG_ALARM3,
Juerg Haefligere95c2372007-10-07 21:27:35 -0700755 0xff);
756 }
757 if (data->alarms & 0xff00) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200758 dme1737_write(data, DME1737_REG_ALARM2,
Juerg Haefligere95c2372007-10-07 21:27:35 -0700759 0xff);
760 }
761 if (data->alarms & 0xff) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200762 dme1737_write(data, DME1737_REG_ALARM1,
Juerg Haefligere95c2372007-10-07 21:27:35 -0700763 0xff);
764 }
765 }
766
Juerg Haefliger94319962007-06-09 10:11:16 -0400767 data->last_update = jiffies;
768 data->valid = 1;
769 }
770
771 mutex_unlock(&data->update_lock);
772
773 return data;
774}
775
776/* ---------------------------------------------------------------------
777 * Voltage sysfs attributes
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100778 * ix = [0-7]
Juerg Haefliger94319962007-06-09 10:11:16 -0400779 * --------------------------------------------------------------------- */
780
781#define SYS_IN_INPUT 0
782#define SYS_IN_MIN 1
783#define SYS_IN_MAX 2
784#define SYS_IN_ALARM 3
785
786static ssize_t show_in(struct device *dev, struct device_attribute *attr,
787 char *buf)
788{
789 struct dme1737_data *data = dme1737_update_device(dev);
790 struct sensor_device_attribute_2
791 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
792 int ix = sensor_attr_2->index;
793 int fn = sensor_attr_2->nr;
794 int res;
795
796 switch (fn) {
797 case SYS_IN_INPUT:
Juerg Haefliger549edb82008-08-06 22:41:03 +0200798 res = IN_FROM_REG(data->in[ix], data->in_nominal[ix], 16);
Juerg Haefliger94319962007-06-09 10:11:16 -0400799 break;
800 case SYS_IN_MIN:
Juerg Haefliger549edb82008-08-06 22:41:03 +0200801 res = IN_FROM_REG(data->in_min[ix], data->in_nominal[ix], 8);
Juerg Haefliger94319962007-06-09 10:11:16 -0400802 break;
803 case SYS_IN_MAX:
Juerg Haefliger549edb82008-08-06 22:41:03 +0200804 res = IN_FROM_REG(data->in_max[ix], data->in_nominal[ix], 8);
Juerg Haefliger94319962007-06-09 10:11:16 -0400805 break;
806 case SYS_IN_ALARM:
807 res = (data->alarms >> DME1737_BIT_ALARM_IN[ix]) & 0x01;
808 break;
809 default:
810 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700811 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400812 }
813
814 return sprintf(buf, "%d\n", res);
815}
816
817static ssize_t set_in(struct device *dev, struct device_attribute *attr,
818 const char *buf, size_t count)
819{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700820 struct dme1737_data *data = dev_get_drvdata(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -0400821 struct sensor_device_attribute_2
822 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
823 int ix = sensor_attr_2->index;
824 int fn = sensor_attr_2->nr;
825 long val = simple_strtol(buf, NULL, 10);
826
827 mutex_lock(&data->update_lock);
828 switch (fn) {
829 case SYS_IN_MIN:
Juerg Haefliger549edb82008-08-06 22:41:03 +0200830 data->in_min[ix] = IN_TO_REG(val, data->in_nominal[ix]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200831 dme1737_write(data, DME1737_REG_IN_MIN(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -0400832 data->in_min[ix]);
833 break;
834 case SYS_IN_MAX:
Juerg Haefliger549edb82008-08-06 22:41:03 +0200835 data->in_max[ix] = IN_TO_REG(val, data->in_nominal[ix]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200836 dme1737_write(data, DME1737_REG_IN_MAX(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -0400837 data->in_max[ix]);
838 break;
839 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700840 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400841 }
842 mutex_unlock(&data->update_lock);
843
844 return count;
845}
846
847/* ---------------------------------------------------------------------
848 * Temperature sysfs attributes
849 * ix = [0-2]
850 * --------------------------------------------------------------------- */
851
852#define SYS_TEMP_INPUT 0
853#define SYS_TEMP_MIN 1
854#define SYS_TEMP_MAX 2
855#define SYS_TEMP_OFFSET 3
856#define SYS_TEMP_ALARM 4
857#define SYS_TEMP_FAULT 5
858
859static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
860 char *buf)
861{
862 struct dme1737_data *data = dme1737_update_device(dev);
863 struct sensor_device_attribute_2
864 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
865 int ix = sensor_attr_2->index;
866 int fn = sensor_attr_2->nr;
867 int res;
868
869 switch (fn) {
870 case SYS_TEMP_INPUT:
871 res = TEMP_FROM_REG(data->temp[ix], 16);
872 break;
873 case SYS_TEMP_MIN:
874 res = TEMP_FROM_REG(data->temp_min[ix], 8);
875 break;
876 case SYS_TEMP_MAX:
877 res = TEMP_FROM_REG(data->temp_max[ix], 8);
878 break;
879 case SYS_TEMP_OFFSET:
880 res = TEMP_FROM_REG(data->temp_offset[ix], 8);
881 break;
882 case SYS_TEMP_ALARM:
883 res = (data->alarms >> DME1737_BIT_ALARM_TEMP[ix]) & 0x01;
884 break;
885 case SYS_TEMP_FAULT:
Juerg Haefligerc0f31402007-07-20 14:16:47 -0700886 res = (((u16)data->temp[ix] & 0xff00) == 0x8000);
Juerg Haefliger94319962007-06-09 10:11:16 -0400887 break;
888 default:
889 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700890 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400891 }
892
893 return sprintf(buf, "%d\n", res);
894}
895
896static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
897 const char *buf, size_t count)
898{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700899 struct dme1737_data *data = dev_get_drvdata(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -0400900 struct sensor_device_attribute_2
901 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
902 int ix = sensor_attr_2->index;
903 int fn = sensor_attr_2->nr;
904 long val = simple_strtol(buf, NULL, 10);
905
906 mutex_lock(&data->update_lock);
907 switch (fn) {
908 case SYS_TEMP_MIN:
909 data->temp_min[ix] = TEMP_TO_REG(val);
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200910 dme1737_write(data, DME1737_REG_TEMP_MIN(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -0400911 data->temp_min[ix]);
912 break;
913 case SYS_TEMP_MAX:
914 data->temp_max[ix] = TEMP_TO_REG(val);
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200915 dme1737_write(data, DME1737_REG_TEMP_MAX(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -0400916 data->temp_max[ix]);
917 break;
918 case SYS_TEMP_OFFSET:
919 data->temp_offset[ix] = TEMP_TO_REG(val);
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200920 dme1737_write(data, DME1737_REG_TEMP_OFFSET(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -0400921 data->temp_offset[ix]);
922 break;
923 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700924 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400925 }
926 mutex_unlock(&data->update_lock);
927
928 return count;
929}
930
931/* ---------------------------------------------------------------------
932 * Zone sysfs attributes
933 * ix = [0-2]
934 * --------------------------------------------------------------------- */
935
936#define SYS_ZONE_AUTO_CHANNELS_TEMP 0
937#define SYS_ZONE_AUTO_POINT1_TEMP_HYST 1
938#define SYS_ZONE_AUTO_POINT1_TEMP 2
939#define SYS_ZONE_AUTO_POINT2_TEMP 3
940#define SYS_ZONE_AUTO_POINT3_TEMP 4
941
942static ssize_t show_zone(struct device *dev, struct device_attribute *attr,
943 char *buf)
944{
945 struct dme1737_data *data = dme1737_update_device(dev);
946 struct sensor_device_attribute_2
947 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
948 int ix = sensor_attr_2->index;
949 int fn = sensor_attr_2->nr;
950 int res;
951
952 switch (fn) {
953 case SYS_ZONE_AUTO_CHANNELS_TEMP:
954 /* check config2 for non-standard temp-to-zone mapping */
955 if ((ix == 1) && (data->config2 & 0x02)) {
956 res = 4;
957 } else {
958 res = 1 << ix;
959 }
960 break;
961 case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
962 res = TEMP_FROM_REG(data->zone_low[ix], 8) -
963 TEMP_HYST_FROM_REG(data->zone_hyst[ix == 2], ix);
964 break;
965 case SYS_ZONE_AUTO_POINT1_TEMP:
966 res = TEMP_FROM_REG(data->zone_low[ix], 8);
967 break;
968 case SYS_ZONE_AUTO_POINT2_TEMP:
969 /* pwm_freq holds the temp range bits in the upper nibble */
970 res = TEMP_FROM_REG(data->zone_low[ix], 8) +
971 TEMP_RANGE_FROM_REG(data->pwm_freq[ix]);
972 break;
973 case SYS_ZONE_AUTO_POINT3_TEMP:
974 res = TEMP_FROM_REG(data->zone_abs[ix], 8);
975 break;
976 default:
977 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700978 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400979 }
980
981 return sprintf(buf, "%d\n", res);
982}
983
984static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
985 const char *buf, size_t count)
986{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700987 struct dme1737_data *data = dev_get_drvdata(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -0400988 struct sensor_device_attribute_2
989 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
990 int ix = sensor_attr_2->index;
991 int fn = sensor_attr_2->nr;
992 long val = simple_strtol(buf, NULL, 10);
993
994 mutex_lock(&data->update_lock);
995 switch (fn) {
996 case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
997 /* Refresh the cache */
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200998 data->zone_low[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400999 DME1737_REG_ZONE_LOW(ix));
1000 /* Modify the temp hyst value */
1001 data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG(
1002 TEMP_FROM_REG(data->zone_low[ix], 8) -
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001003 val, ix, dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001004 DME1737_REG_ZONE_HYST(ix == 2)));
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001005 dme1737_write(data, DME1737_REG_ZONE_HYST(ix == 2),
Juerg Haefliger94319962007-06-09 10:11:16 -04001006 data->zone_hyst[ix == 2]);
1007 break;
1008 case SYS_ZONE_AUTO_POINT1_TEMP:
1009 data->zone_low[ix] = TEMP_TO_REG(val);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001010 dme1737_write(data, DME1737_REG_ZONE_LOW(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001011 data->zone_low[ix]);
1012 break;
1013 case SYS_ZONE_AUTO_POINT2_TEMP:
1014 /* Refresh the cache */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001015 data->zone_low[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001016 DME1737_REG_ZONE_LOW(ix));
1017 /* Modify the temp range value (which is stored in the upper
1018 * nibble of the pwm_freq register) */
1019 data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val -
1020 TEMP_FROM_REG(data->zone_low[ix], 8),
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001021 dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001022 DME1737_REG_PWM_FREQ(ix)));
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001023 dme1737_write(data, DME1737_REG_PWM_FREQ(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001024 data->pwm_freq[ix]);
1025 break;
1026 case SYS_ZONE_AUTO_POINT3_TEMP:
1027 data->zone_abs[ix] = TEMP_TO_REG(val);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001028 dme1737_write(data, DME1737_REG_ZONE_ABS(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001029 data->zone_abs[ix]);
1030 break;
1031 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001032 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001033 }
1034 mutex_unlock(&data->update_lock);
1035
1036 return count;
1037}
1038
1039/* ---------------------------------------------------------------------
1040 * Fan sysfs attributes
1041 * ix = [0-5]
1042 * --------------------------------------------------------------------- */
1043
1044#define SYS_FAN_INPUT 0
1045#define SYS_FAN_MIN 1
1046#define SYS_FAN_MAX 2
1047#define SYS_FAN_ALARM 3
1048#define SYS_FAN_TYPE 4
1049
1050static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
1051 char *buf)
1052{
1053 struct dme1737_data *data = dme1737_update_device(dev);
1054 struct sensor_device_attribute_2
1055 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1056 int ix = sensor_attr_2->index;
1057 int fn = sensor_attr_2->nr;
1058 int res;
1059
1060 switch (fn) {
1061 case SYS_FAN_INPUT:
1062 res = FAN_FROM_REG(data->fan[ix],
1063 ix < 4 ? 0 :
1064 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1065 break;
1066 case SYS_FAN_MIN:
1067 res = FAN_FROM_REG(data->fan_min[ix],
1068 ix < 4 ? 0 :
1069 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1070 break;
1071 case SYS_FAN_MAX:
1072 /* only valid for fan[5-6] */
1073 res = FAN_MAX_FROM_REG(data->fan_max[ix - 4]);
1074 break;
1075 case SYS_FAN_ALARM:
1076 res = (data->alarms >> DME1737_BIT_ALARM_FAN[ix]) & 0x01;
1077 break;
1078 case SYS_FAN_TYPE:
1079 /* only valid for fan[1-4] */
1080 res = FAN_TYPE_FROM_REG(data->fan_opt[ix]);
1081 break;
1082 default:
1083 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001084 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001085 }
1086
1087 return sprintf(buf, "%d\n", res);
1088}
1089
1090static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1091 const char *buf, size_t count)
1092{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001093 struct dme1737_data *data = dev_get_drvdata(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04001094 struct sensor_device_attribute_2
1095 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1096 int ix = sensor_attr_2->index;
1097 int fn = sensor_attr_2->nr;
1098 long val = simple_strtol(buf, NULL, 10);
1099
1100 mutex_lock(&data->update_lock);
1101 switch (fn) {
1102 case SYS_FAN_MIN:
1103 if (ix < 4) {
1104 data->fan_min[ix] = FAN_TO_REG(val, 0);
1105 } else {
1106 /* Refresh the cache */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001107 data->fan_opt[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001108 DME1737_REG_FAN_OPT(ix));
1109 /* Modify the fan min value */
1110 data->fan_min[ix] = FAN_TO_REG(val,
1111 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1112 }
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001113 dme1737_write(data, DME1737_REG_FAN_MIN(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001114 data->fan_min[ix] & 0xff);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001115 dme1737_write(data, DME1737_REG_FAN_MIN(ix) + 1,
Juerg Haefliger94319962007-06-09 10:11:16 -04001116 data->fan_min[ix] >> 8);
1117 break;
1118 case SYS_FAN_MAX:
1119 /* Only valid for fan[5-6] */
1120 data->fan_max[ix - 4] = FAN_MAX_TO_REG(val);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001121 dme1737_write(data, DME1737_REG_FAN_MAX(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001122 data->fan_max[ix - 4]);
1123 break;
1124 case SYS_FAN_TYPE:
1125 /* Only valid for fan[1-4] */
1126 if (!(val == 1 || val == 2 || val == 4)) {
1127 count = -EINVAL;
Juerg Haefligere95c2372007-10-07 21:27:35 -07001128 dev_warn(dev, "Fan type value %ld not "
Juerg Haefliger94319962007-06-09 10:11:16 -04001129 "supported. Choose one of 1, 2, or 4.\n",
1130 val);
1131 goto exit;
1132 }
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001133 data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001134 DME1737_REG_FAN_OPT(ix)));
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001135 dme1737_write(data, DME1737_REG_FAN_OPT(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001136 data->fan_opt[ix]);
1137 break;
1138 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001139 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001140 }
1141exit:
1142 mutex_unlock(&data->update_lock);
1143
1144 return count;
1145}
1146
1147/* ---------------------------------------------------------------------
1148 * PWM sysfs attributes
1149 * ix = [0-4]
1150 * --------------------------------------------------------------------- */
1151
1152#define SYS_PWM 0
1153#define SYS_PWM_FREQ 1
1154#define SYS_PWM_ENABLE 2
1155#define SYS_PWM_RAMP_RATE 3
1156#define SYS_PWM_AUTO_CHANNELS_ZONE 4
1157#define SYS_PWM_AUTO_PWM_MIN 5
1158#define SYS_PWM_AUTO_POINT1_PWM 6
1159#define SYS_PWM_AUTO_POINT2_PWM 7
1160
1161static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1162 char *buf)
1163{
1164 struct dme1737_data *data = dme1737_update_device(dev);
1165 struct sensor_device_attribute_2
1166 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1167 int ix = sensor_attr_2->index;
1168 int fn = sensor_attr_2->nr;
1169 int res;
1170
1171 switch (fn) {
1172 case SYS_PWM:
1173 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 0) {
1174 res = 255;
1175 } else {
1176 res = data->pwm[ix];
1177 }
1178 break;
1179 case SYS_PWM_FREQ:
1180 res = PWM_FREQ_FROM_REG(data->pwm_freq[ix]);
1181 break;
1182 case SYS_PWM_ENABLE:
Roel Kluin912e8372009-09-22 16:43:41 -07001183 if (ix >= 3) {
Juerg Haefliger94319962007-06-09 10:11:16 -04001184 res = 1; /* pwm[5-6] hard-wired to manual mode */
1185 } else {
1186 res = PWM_EN_FROM_REG(data->pwm_config[ix]);
1187 }
1188 break;
1189 case SYS_PWM_RAMP_RATE:
1190 /* Only valid for pwm[1-3] */
1191 res = PWM_RR_FROM_REG(data->pwm_rr[ix > 0], ix);
1192 break;
1193 case SYS_PWM_AUTO_CHANNELS_ZONE:
1194 /* Only valid for pwm[1-3] */
1195 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1196 res = PWM_ACZ_FROM_REG(data->pwm_config[ix]);
1197 } else {
1198 res = data->pwm_acz[ix];
1199 }
1200 break;
1201 case SYS_PWM_AUTO_PWM_MIN:
1202 /* Only valid for pwm[1-3] */
1203 if (PWM_OFF_FROM_REG(data->pwm_rr[0], ix)) {
1204 res = data->pwm_min[ix];
1205 } else {
1206 res = 0;
1207 }
1208 break;
1209 case SYS_PWM_AUTO_POINT1_PWM:
1210 /* Only valid for pwm[1-3] */
1211 res = data->pwm_min[ix];
1212 break;
1213 case SYS_PWM_AUTO_POINT2_PWM:
1214 /* Only valid for pwm[1-3] */
1215 res = 255; /* hard-wired */
1216 break;
1217 default:
1218 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001219 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001220 }
1221
1222 return sprintf(buf, "%d\n", res);
1223}
1224
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001225static struct attribute *dme1737_pwm_chmod_attr[];
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001226static void dme1737_chmod_file(struct device*, struct attribute*, mode_t);
Juerg Haefliger94319962007-06-09 10:11:16 -04001227
1228static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1229 const char *buf, size_t count)
1230{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001231 struct dme1737_data *data = dev_get_drvdata(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04001232 struct sensor_device_attribute_2
1233 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1234 int ix = sensor_attr_2->index;
1235 int fn = sensor_attr_2->nr;
1236 long val = simple_strtol(buf, NULL, 10);
1237
1238 mutex_lock(&data->update_lock);
1239 switch (fn) {
1240 case SYS_PWM:
1241 data->pwm[ix] = SENSORS_LIMIT(val, 0, 255);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001242 dme1737_write(data, DME1737_REG_PWM(ix), data->pwm[ix]);
Juerg Haefliger94319962007-06-09 10:11:16 -04001243 break;
1244 case SYS_PWM_FREQ:
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001245 data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001246 DME1737_REG_PWM_FREQ(ix)));
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001247 dme1737_write(data, DME1737_REG_PWM_FREQ(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001248 data->pwm_freq[ix]);
1249 break;
1250 case SYS_PWM_ENABLE:
1251 /* Only valid for pwm[1-3] */
1252 if (val < 0 || val > 2) {
1253 count = -EINVAL;
Juerg Haefligere95c2372007-10-07 21:27:35 -07001254 dev_warn(dev, "PWM enable %ld not "
Juerg Haefliger94319962007-06-09 10:11:16 -04001255 "supported. Choose one of 0, 1, or 2.\n",
1256 val);
1257 goto exit;
1258 }
1259 /* Refresh the cache */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001260 data->pwm_config[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001261 DME1737_REG_PWM_CONFIG(ix));
1262 if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) {
1263 /* Bail out if no change */
1264 goto exit;
1265 }
1266 /* Do some housekeeping if we are currently in auto mode */
1267 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1268 /* Save the current zone channel assignment */
1269 data->pwm_acz[ix] = PWM_ACZ_FROM_REG(
1270 data->pwm_config[ix]);
1271 /* Save the current ramp rate state and disable it */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001272 data->pwm_rr[ix > 0] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001273 DME1737_REG_PWM_RR(ix > 0));
1274 data->pwm_rr_en &= ~(1 << ix);
1275 if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) {
1276 data->pwm_rr_en |= (1 << ix);
1277 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix,
1278 data->pwm_rr[ix > 0]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001279 dme1737_write(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001280 DME1737_REG_PWM_RR(ix > 0),
1281 data->pwm_rr[ix > 0]);
1282 }
1283 }
1284 /* Set the new PWM mode */
1285 switch (val) {
1286 case 0:
1287 /* Change permissions of pwm[ix] to read-only */
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001288 dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
Juerg Haefliger94319962007-06-09 10:11:16 -04001289 S_IRUGO);
1290 /* Turn fan fully on */
1291 data->pwm_config[ix] = PWM_EN_TO_REG(0,
1292 data->pwm_config[ix]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001293 dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001294 data->pwm_config[ix]);
1295 break;
1296 case 1:
1297 /* Turn on manual mode */
1298 data->pwm_config[ix] = PWM_EN_TO_REG(1,
1299 data->pwm_config[ix]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001300 dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001301 data->pwm_config[ix]);
1302 /* Change permissions of pwm[ix] to read-writeable */
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001303 dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
Juerg Haefliger94319962007-06-09 10:11:16 -04001304 S_IRUGO | S_IWUSR);
1305 break;
1306 case 2:
1307 /* Change permissions of pwm[ix] to read-only */
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001308 dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
Juerg Haefliger94319962007-06-09 10:11:16 -04001309 S_IRUGO);
1310 /* Turn on auto mode using the saved zone channel
1311 * assignment */
1312 data->pwm_config[ix] = PWM_ACZ_TO_REG(
1313 data->pwm_acz[ix],
1314 data->pwm_config[ix]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001315 dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001316 data->pwm_config[ix]);
1317 /* Enable PWM ramp rate if previously enabled */
1318 if (data->pwm_rr_en & (1 << ix)) {
1319 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix,
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001320 dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001321 DME1737_REG_PWM_RR(ix > 0)));
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001322 dme1737_write(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001323 DME1737_REG_PWM_RR(ix > 0),
1324 data->pwm_rr[ix > 0]);
1325 }
1326 break;
1327 }
1328 break;
1329 case SYS_PWM_RAMP_RATE:
1330 /* Only valid for pwm[1-3] */
1331 /* Refresh the cache */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001332 data->pwm_config[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001333 DME1737_REG_PWM_CONFIG(ix));
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001334 data->pwm_rr[ix > 0] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001335 DME1737_REG_PWM_RR(ix > 0));
1336 /* Set the ramp rate value */
1337 if (val > 0) {
1338 data->pwm_rr[ix > 0] = PWM_RR_TO_REG(val, ix,
1339 data->pwm_rr[ix > 0]);
1340 }
1341 /* Enable/disable the feature only if the associated PWM
1342 * output is in automatic mode. */
1343 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1344 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix,
1345 data->pwm_rr[ix > 0]);
1346 }
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001347 dme1737_write(data, DME1737_REG_PWM_RR(ix > 0),
Juerg Haefliger94319962007-06-09 10:11:16 -04001348 data->pwm_rr[ix > 0]);
1349 break;
1350 case SYS_PWM_AUTO_CHANNELS_ZONE:
1351 /* Only valid for pwm[1-3] */
1352 if (!(val == 1 || val == 2 || val == 4 ||
1353 val == 6 || val == 7)) {
1354 count = -EINVAL;
Juerg Haefligere95c2372007-10-07 21:27:35 -07001355 dev_warn(dev, "PWM auto channels zone %ld "
Juerg Haefliger94319962007-06-09 10:11:16 -04001356 "not supported. Choose one of 1, 2, 4, 6, "
1357 "or 7.\n", val);
1358 goto exit;
1359 }
1360 /* Refresh the cache */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001361 data->pwm_config[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001362 DME1737_REG_PWM_CONFIG(ix));
1363 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1364 /* PWM is already in auto mode so update the temp
1365 * channel assignment */
1366 data->pwm_config[ix] = PWM_ACZ_TO_REG(val,
1367 data->pwm_config[ix]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001368 dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001369 data->pwm_config[ix]);
1370 } else {
1371 /* PWM is not in auto mode so we save the temp
1372 * channel assignment for later use */
1373 data->pwm_acz[ix] = val;
1374 }
1375 break;
1376 case SYS_PWM_AUTO_PWM_MIN:
1377 /* Only valid for pwm[1-3] */
1378 /* Refresh the cache */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001379 data->pwm_min[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001380 DME1737_REG_PWM_MIN(ix));
1381 /* There are only 2 values supported for the auto_pwm_min
1382 * value: 0 or auto_point1_pwm. So if the temperature drops
1383 * below the auto_point1_temp_hyst value, the fan either turns
1384 * off or runs at auto_point1_pwm duty-cycle. */
1385 if (val > ((data->pwm_min[ix] + 1) / 2)) {
1386 data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix,
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001387 dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001388 DME1737_REG_PWM_RR(0)));
Juerg Haefliger94319962007-06-09 10:11:16 -04001389 } else {
1390 data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix,
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001391 dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001392 DME1737_REG_PWM_RR(0)));
Juerg Haefliger94319962007-06-09 10:11:16 -04001393 }
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001394 dme1737_write(data, DME1737_REG_PWM_RR(0),
Juerg Haefliger94319962007-06-09 10:11:16 -04001395 data->pwm_rr[0]);
1396 break;
1397 case SYS_PWM_AUTO_POINT1_PWM:
1398 /* Only valid for pwm[1-3] */
1399 data->pwm_min[ix] = SENSORS_LIMIT(val, 0, 255);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001400 dme1737_write(data, DME1737_REG_PWM_MIN(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001401 data->pwm_min[ix]);
1402 break;
1403 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001404 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001405 }
1406exit:
1407 mutex_unlock(&data->update_lock);
1408
1409 return count;
1410}
1411
1412/* ---------------------------------------------------------------------
1413 * Miscellaneous sysfs attributes
1414 * --------------------------------------------------------------------- */
1415
1416static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
1417 char *buf)
1418{
1419 struct i2c_client *client = to_i2c_client(dev);
1420 struct dme1737_data *data = i2c_get_clientdata(client);
1421
1422 return sprintf(buf, "%d\n", data->vrm);
1423}
1424
1425static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
1426 const char *buf, size_t count)
1427{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001428 struct dme1737_data *data = dev_get_drvdata(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04001429 long val = simple_strtol(buf, NULL, 10);
1430
1431 data->vrm = val;
1432 return count;
1433}
1434
1435static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
1436 char *buf)
1437{
1438 struct dme1737_data *data = dme1737_update_device(dev);
1439
1440 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1441}
1442
Juerg Haefligere95c2372007-10-07 21:27:35 -07001443static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1444 char *buf)
1445{
1446 struct dme1737_data *data = dev_get_drvdata(dev);
1447
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001448 return sprintf(buf, "%s\n", data->name);
Juerg Haefligere95c2372007-10-07 21:27:35 -07001449}
1450
Juerg Haefliger94319962007-06-09 10:11:16 -04001451/* ---------------------------------------------------------------------
1452 * Sysfs device attribute defines and structs
1453 * --------------------------------------------------------------------- */
1454
Juerg Haefligerd4b94e12011-01-12 21:55:13 +01001455/* Voltages 0-7 */
Juerg Haefliger94319962007-06-09 10:11:16 -04001456
1457#define SENSOR_DEVICE_ATTR_IN(ix) \
1458static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001459 show_in, NULL, SYS_IN_INPUT, ix); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001460static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001461 show_in, set_in, SYS_IN_MIN, ix); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001462static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001463 show_in, set_in, SYS_IN_MAX, ix); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001464static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001465 show_in, NULL, SYS_IN_ALARM, ix)
Juerg Haefliger94319962007-06-09 10:11:16 -04001466
1467SENSOR_DEVICE_ATTR_IN(0);
1468SENSOR_DEVICE_ATTR_IN(1);
1469SENSOR_DEVICE_ATTR_IN(2);
1470SENSOR_DEVICE_ATTR_IN(3);
1471SENSOR_DEVICE_ATTR_IN(4);
1472SENSOR_DEVICE_ATTR_IN(5);
1473SENSOR_DEVICE_ATTR_IN(6);
Juerg Haefligerd4b94e12011-01-12 21:55:13 +01001474SENSOR_DEVICE_ATTR_IN(7);
Juerg Haefliger94319962007-06-09 10:11:16 -04001475
1476/* Temperatures 1-3 */
1477
1478#define SENSOR_DEVICE_ATTR_TEMP(ix) \
1479static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001480 show_temp, NULL, SYS_TEMP_INPUT, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001481static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001482 show_temp, set_temp, SYS_TEMP_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001483static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001484 show_temp, set_temp, SYS_TEMP_MAX, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001485static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001486 show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001487static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001488 show_temp, NULL, SYS_TEMP_ALARM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001489static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001490 show_temp, NULL, SYS_TEMP_FAULT, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001491
1492SENSOR_DEVICE_ATTR_TEMP(1);
1493SENSOR_DEVICE_ATTR_TEMP(2);
1494SENSOR_DEVICE_ATTR_TEMP(3);
1495
1496/* Zones 1-3 */
1497
1498#define SENSOR_DEVICE_ATTR_ZONE(ix) \
1499static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001500 show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001501static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001502 show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001503static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001504 show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001505static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001506 show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001507static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001508 show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001509
1510SENSOR_DEVICE_ATTR_ZONE(1);
1511SENSOR_DEVICE_ATTR_ZONE(2);
1512SENSOR_DEVICE_ATTR_ZONE(3);
1513
1514/* Fans 1-4 */
1515
1516#define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \
1517static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001518 show_fan, NULL, SYS_FAN_INPUT, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001519static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001520 show_fan, set_fan, SYS_FAN_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001521static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001522 show_fan, NULL, SYS_FAN_ALARM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001523static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001524 show_fan, set_fan, SYS_FAN_TYPE, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001525
1526SENSOR_DEVICE_ATTR_FAN_1TO4(1);
1527SENSOR_DEVICE_ATTR_FAN_1TO4(2);
1528SENSOR_DEVICE_ATTR_FAN_1TO4(3);
1529SENSOR_DEVICE_ATTR_FAN_1TO4(4);
1530
1531/* Fans 5-6 */
1532
1533#define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \
1534static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001535 show_fan, NULL, SYS_FAN_INPUT, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001536static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001537 show_fan, set_fan, SYS_FAN_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001538static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001539 show_fan, NULL, SYS_FAN_ALARM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001540static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001541 show_fan, set_fan, SYS_FAN_MAX, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001542
1543SENSOR_DEVICE_ATTR_FAN_5TO6(5);
1544SENSOR_DEVICE_ATTR_FAN_5TO6(6);
1545
1546/* PWMs 1-3 */
1547
1548#define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \
1549static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001550 show_pwm, set_pwm, SYS_PWM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001551static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001552 show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001553static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001554 show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001555static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001556 show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001557static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001558 show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001559static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001560 show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001561static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001562 show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001563static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001564 show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001565
1566SENSOR_DEVICE_ATTR_PWM_1TO3(1);
1567SENSOR_DEVICE_ATTR_PWM_1TO3(2);
1568SENSOR_DEVICE_ATTR_PWM_1TO3(3);
1569
1570/* PWMs 5-6 */
1571
1572#define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \
Juerg Haefliger9b257712008-03-25 21:49:02 -07001573static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001574 show_pwm, set_pwm, SYS_PWM, ix-1); \
Juerg Haefliger9b257712008-03-25 21:49:02 -07001575static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001576 show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001577static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001578 show_pwm, NULL, SYS_PWM_ENABLE, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001579
1580SENSOR_DEVICE_ATTR_PWM_5TO6(5);
1581SENSOR_DEVICE_ATTR_PWM_5TO6(6);
1582
1583/* Misc */
1584
1585static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
1586static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
Juerg Haefligere95c2372007-10-07 21:27:35 -07001587static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); /* for ISA devices */
Juerg Haefliger94319962007-06-09 10:11:16 -04001588
Juerg Haefliger94319962007-06-09 10:11:16 -04001589/* This struct holds all the attributes that are always present and need to be
1590 * created unconditionally. The attributes that need modification of their
1591 * permissions are created read-only and write permissions are added or removed
1592 * on the fly when required */
1593static struct attribute *dme1737_attr[] ={
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001594 /* Voltages */
Juerg Haefliger9b257712008-03-25 21:49:02 -07001595 &sensor_dev_attr_in0_input.dev_attr.attr,
1596 &sensor_dev_attr_in0_min.dev_attr.attr,
1597 &sensor_dev_attr_in0_max.dev_attr.attr,
1598 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1599 &sensor_dev_attr_in1_input.dev_attr.attr,
1600 &sensor_dev_attr_in1_min.dev_attr.attr,
1601 &sensor_dev_attr_in1_max.dev_attr.attr,
1602 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1603 &sensor_dev_attr_in2_input.dev_attr.attr,
1604 &sensor_dev_attr_in2_min.dev_attr.attr,
1605 &sensor_dev_attr_in2_max.dev_attr.attr,
1606 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1607 &sensor_dev_attr_in3_input.dev_attr.attr,
1608 &sensor_dev_attr_in3_min.dev_attr.attr,
1609 &sensor_dev_attr_in3_max.dev_attr.attr,
1610 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1611 &sensor_dev_attr_in4_input.dev_attr.attr,
1612 &sensor_dev_attr_in4_min.dev_attr.attr,
1613 &sensor_dev_attr_in4_max.dev_attr.attr,
1614 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1615 &sensor_dev_attr_in5_input.dev_attr.attr,
1616 &sensor_dev_attr_in5_min.dev_attr.attr,
1617 &sensor_dev_attr_in5_max.dev_attr.attr,
1618 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1619 &sensor_dev_attr_in6_input.dev_attr.attr,
1620 &sensor_dev_attr_in6_min.dev_attr.attr,
1621 &sensor_dev_attr_in6_max.dev_attr.attr,
1622 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001623 /* Temperatures */
Juerg Haefliger9b257712008-03-25 21:49:02 -07001624 &sensor_dev_attr_temp1_input.dev_attr.attr,
1625 &sensor_dev_attr_temp1_min.dev_attr.attr,
1626 &sensor_dev_attr_temp1_max.dev_attr.attr,
1627 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1628 &sensor_dev_attr_temp1_fault.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001629 &sensor_dev_attr_temp2_input.dev_attr.attr,
1630 &sensor_dev_attr_temp2_min.dev_attr.attr,
1631 &sensor_dev_attr_temp2_max.dev_attr.attr,
1632 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1633 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001634 &sensor_dev_attr_temp3_input.dev_attr.attr,
1635 &sensor_dev_attr_temp3_min.dev_attr.attr,
1636 &sensor_dev_attr_temp3_max.dev_attr.attr,
1637 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1638 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001639 /* Zones */
Juerg Haefliger9b257712008-03-25 21:49:02 -07001640 &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
1641 &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
1642 &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
1643 &sensor_dev_attr_zone1_auto_channels_temp.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001644 &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
1645 &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
1646 &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
1647 &sensor_dev_attr_zone2_auto_channels_temp.dev_attr.attr,
Juerg Haefliger549edb82008-08-06 22:41:03 +02001648 NULL
1649};
1650
1651static const struct attribute_group dme1737_group = {
1652 .attrs = dme1737_attr,
1653};
1654
Juerg Haefligerea694432010-05-27 19:59:01 +02001655/* The following struct holds temp offset attributes, which are not available
1656 * in all chips. The following chips support them:
1657 * DME1737, SCH311x */
1658static struct attribute *dme1737_temp_offset_attr[] = {
Juerg Haefliger549edb82008-08-06 22:41:03 +02001659 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1660 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1661 &sensor_dev_attr_temp3_offset.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001662 NULL
1663};
1664
Juerg Haefligerea694432010-05-27 19:59:01 +02001665static const struct attribute_group dme1737_temp_offset_group = {
1666 .attrs = dme1737_temp_offset_attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001667};
1668
Juerg Haefligerea694432010-05-27 19:59:01 +02001669/* The following struct holds VID related attributes, which are not available
1670 * in all chips. The following chips support them:
1671 * DME1737 */
Jean Delvare9d091442009-10-24 13:28:46 +02001672static struct attribute *dme1737_vid_attr[] = {
1673 &dev_attr_vrm.attr,
1674 &dev_attr_cpu0_vid.attr,
1675 NULL
1676};
1677
1678static const struct attribute_group dme1737_vid_group = {
1679 .attrs = dme1737_vid_attr,
1680};
1681
Juerg Haefligerea694432010-05-27 19:59:01 +02001682/* The following struct holds temp zone 3 related attributes, which are not
1683 * available in all chips. The following chips support them:
1684 * DME1737, SCH311x, SCH5027 */
1685static struct attribute *dme1737_zone3_attr[] = {
1686 &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
1687 &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
1688 &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
1689 &sensor_dev_attr_zone3_auto_channels_temp.dev_attr.attr,
1690 NULL
1691};
1692
1693static const struct attribute_group dme1737_zone3_group = {
1694 .attrs = dme1737_zone3_attr,
1695};
1696
1697
1698/* The following struct holds temp zone hysteresis related attributes, which
1699 * are not available in all chips. The following chips support them:
1700 * DME1737, SCH311x */
1701static struct attribute *dme1737_zone_hyst_attr[] = {
1702 &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
1703 &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
1704 &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
1705 NULL
1706};
1707
1708static const struct attribute_group dme1737_zone_hyst_group = {
1709 .attrs = dme1737_zone_hyst_attr,
1710};
1711
Juerg Haefligerd4b94e12011-01-12 21:55:13 +01001712/* The following struct holds voltage in7 related attributes, which
1713 * are not available in all chips. The following chips support them:
1714 * SCH5127 */
1715static struct attribute *dme1737_in7_attr[] = {
1716 &sensor_dev_attr_in7_input.dev_attr.attr,
1717 &sensor_dev_attr_in7_min.dev_attr.attr,
1718 &sensor_dev_attr_in7_max.dev_attr.attr,
1719 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1720 NULL
1721};
1722
1723static const struct attribute_group dme1737_in7_group = {
1724 .attrs = dme1737_in7_attr,
1725};
1726
Juerg Haefliger94319962007-06-09 10:11:16 -04001727/* The following structs hold the PWM attributes, some of which are optional.
1728 * Their creation depends on the chip configuration which is determined during
1729 * module load. */
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001730static struct attribute *dme1737_pwm1_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001731 &sensor_dev_attr_pwm1.dev_attr.attr,
1732 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1733 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1734 &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
1735 &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001736 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1737 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001738 NULL
1739};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001740static struct attribute *dme1737_pwm2_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001741 &sensor_dev_attr_pwm2.dev_attr.attr,
1742 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1743 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1744 &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
1745 &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001746 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1747 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001748 NULL
1749};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001750static struct attribute *dme1737_pwm3_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001751 &sensor_dev_attr_pwm3.dev_attr.attr,
1752 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1753 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1754 &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
1755 &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001756 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1757 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001758 NULL
1759};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001760static struct attribute *dme1737_pwm5_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001761 &sensor_dev_attr_pwm5.dev_attr.attr,
1762 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
1763 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001764 NULL
1765};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001766static struct attribute *dme1737_pwm6_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001767 &sensor_dev_attr_pwm6.dev_attr.attr,
1768 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
1769 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001770 NULL
1771};
1772
1773static const struct attribute_group dme1737_pwm_group[] = {
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001774 { .attrs = dme1737_pwm1_attr },
1775 { .attrs = dme1737_pwm2_attr },
1776 { .attrs = dme1737_pwm3_attr },
Juerg Haefliger94319962007-06-09 10:11:16 -04001777 { .attrs = NULL },
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001778 { .attrs = dme1737_pwm5_attr },
1779 { .attrs = dme1737_pwm6_attr },
Juerg Haefliger94319962007-06-09 10:11:16 -04001780};
1781
Juerg Haefligerea694432010-05-27 19:59:01 +02001782/* The following struct holds auto PWM min attributes, which are not available
1783 * in all chips. Their creation depends on the chip type which is determined
Juerg Haefliger549edb82008-08-06 22:41:03 +02001784 * during module load. */
Juerg Haefligerea694432010-05-27 19:59:01 +02001785static struct attribute *dme1737_auto_pwm_min_attr[] = {
Juerg Haefliger549edb82008-08-06 22:41:03 +02001786 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1787 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1788 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1789};
1790
Juerg Haefliger94319962007-06-09 10:11:16 -04001791/* The following structs hold the fan attributes, some of which are optional.
1792 * Their creation depends on the chip configuration which is determined during
1793 * module load. */
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001794static struct attribute *dme1737_fan1_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001795 &sensor_dev_attr_fan1_input.dev_attr.attr,
1796 &sensor_dev_attr_fan1_min.dev_attr.attr,
1797 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1798 &sensor_dev_attr_fan1_type.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001799 NULL
1800};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001801static struct attribute *dme1737_fan2_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001802 &sensor_dev_attr_fan2_input.dev_attr.attr,
1803 &sensor_dev_attr_fan2_min.dev_attr.attr,
1804 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1805 &sensor_dev_attr_fan2_type.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001806 NULL
1807};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001808static struct attribute *dme1737_fan3_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001809 &sensor_dev_attr_fan3_input.dev_attr.attr,
1810 &sensor_dev_attr_fan3_min.dev_attr.attr,
1811 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1812 &sensor_dev_attr_fan3_type.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001813 NULL
1814};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001815static struct attribute *dme1737_fan4_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001816 &sensor_dev_attr_fan4_input.dev_attr.attr,
1817 &sensor_dev_attr_fan4_min.dev_attr.attr,
1818 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1819 &sensor_dev_attr_fan4_type.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001820 NULL
1821};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001822static struct attribute *dme1737_fan5_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001823 &sensor_dev_attr_fan5_input.dev_attr.attr,
1824 &sensor_dev_attr_fan5_min.dev_attr.attr,
1825 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1826 &sensor_dev_attr_fan5_max.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001827 NULL
1828};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001829static struct attribute *dme1737_fan6_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001830 &sensor_dev_attr_fan6_input.dev_attr.attr,
1831 &sensor_dev_attr_fan6_min.dev_attr.attr,
1832 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1833 &sensor_dev_attr_fan6_max.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001834 NULL
1835};
1836
1837static const struct attribute_group dme1737_fan_group[] = {
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001838 { .attrs = dme1737_fan1_attr },
1839 { .attrs = dme1737_fan2_attr },
1840 { .attrs = dme1737_fan3_attr },
1841 { .attrs = dme1737_fan4_attr },
1842 { .attrs = dme1737_fan5_attr },
1843 { .attrs = dme1737_fan6_attr },
Juerg Haefliger94319962007-06-09 10:11:16 -04001844};
1845
Juerg Haefliger549edb82008-08-06 22:41:03 +02001846/* The permissions of the following zone attributes are changed to read-
Juerg Haefliger94319962007-06-09 10:11:16 -04001847 * writeable if the chip is *not* locked. Otherwise they stay read-only. */
Juerg Haefliger549edb82008-08-06 22:41:03 +02001848static struct attribute *dme1737_zone_chmod_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001849 &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
1850 &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
1851 &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001852 &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
1853 &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
1854 &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
Juerg Haefligerea694432010-05-27 19:59:01 +02001855 NULL
1856};
1857
1858static const struct attribute_group dme1737_zone_chmod_group = {
1859 .attrs = dme1737_zone_chmod_attr,
1860};
1861
1862
1863/* The permissions of the following zone 3 attributes are changed to read-
1864 * writeable if the chip is *not* locked. Otherwise they stay read-only. */
1865static struct attribute *dme1737_zone3_chmod_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001866 &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
1867 &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
1868 &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001869 NULL
1870};
1871
Juerg Haefligerea694432010-05-27 19:59:01 +02001872static const struct attribute_group dme1737_zone3_chmod_group = {
1873 .attrs = dme1737_zone3_chmod_attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001874};
1875
1876/* The permissions of the following PWM attributes are changed to read-
1877 * writeable if the chip is *not* locked and the respective PWM is available.
1878 * Otherwise they stay read-only. */
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001879static struct attribute *dme1737_pwm1_chmod_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001880 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1881 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1882 &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
1883 &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001884 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001885 NULL
1886};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001887static struct attribute *dme1737_pwm2_chmod_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001888 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1889 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1890 &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
1891 &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001892 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001893 NULL
1894};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001895static struct attribute *dme1737_pwm3_chmod_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001896 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1897 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1898 &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
1899 &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001900 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001901 NULL
1902};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001903static struct attribute *dme1737_pwm5_chmod_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001904 &sensor_dev_attr_pwm5.dev_attr.attr,
1905 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001906 NULL
1907};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001908static struct attribute *dme1737_pwm6_chmod_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001909 &sensor_dev_attr_pwm6.dev_attr.attr,
1910 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001911 NULL
1912};
1913
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001914static const struct attribute_group dme1737_pwm_chmod_group[] = {
1915 { .attrs = dme1737_pwm1_chmod_attr },
1916 { .attrs = dme1737_pwm2_chmod_attr },
1917 { .attrs = dme1737_pwm3_chmod_attr },
Juerg Haefliger94319962007-06-09 10:11:16 -04001918 { .attrs = NULL },
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001919 { .attrs = dme1737_pwm5_chmod_attr },
1920 { .attrs = dme1737_pwm6_chmod_attr },
Juerg Haefliger94319962007-06-09 10:11:16 -04001921};
1922
1923/* Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the
1924 * chip is not locked. Otherwise they are read-only. */
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001925static struct attribute *dme1737_pwm_chmod_attr[] = {
Juerg Haefliger94319962007-06-09 10:11:16 -04001926 &sensor_dev_attr_pwm1.dev_attr.attr,
1927 &sensor_dev_attr_pwm2.dev_attr.attr,
1928 &sensor_dev_attr_pwm3.dev_attr.attr,
1929};
1930
1931/* ---------------------------------------------------------------------
1932 * Super-IO functions
1933 * --------------------------------------------------------------------- */
1934
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001935static inline void dme1737_sio_enter(int sio_cip)
1936{
1937 outb(0x55, sio_cip);
1938}
1939
1940static inline void dme1737_sio_exit(int sio_cip)
1941{
1942 outb(0xaa, sio_cip);
1943}
1944
Juerg Haefliger94319962007-06-09 10:11:16 -04001945static inline int dme1737_sio_inb(int sio_cip, int reg)
1946{
1947 outb(reg, sio_cip);
1948 return inb(sio_cip + 1);
1949}
1950
1951static inline void dme1737_sio_outb(int sio_cip, int reg, int val)
1952{
1953 outb(reg, sio_cip);
1954 outb(val, sio_cip + 1);
1955}
1956
Juerg Haefliger94319962007-06-09 10:11:16 -04001957/* ---------------------------------------------------------------------
Juerg Haefligere95c2372007-10-07 21:27:35 -07001958 * Device initialization
Juerg Haefliger94319962007-06-09 10:11:16 -04001959 * --------------------------------------------------------------------- */
1960
Juerg Haefliger67e2f322007-10-01 21:20:28 -07001961static int dme1737_i2c_get_features(int, struct dme1737_data*);
Juerg Haefliger94319962007-06-09 10:11:16 -04001962
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001963static void dme1737_chmod_file(struct device *dev,
Juerg Haefliger94319962007-06-09 10:11:16 -04001964 struct attribute *attr, mode_t mode)
Juerg Haefliger94319962007-06-09 10:11:16 -04001965{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001966 if (sysfs_chmod_file(&dev->kobj, attr, mode)) {
1967 dev_warn(dev, "Failed to change permissions of %s.\n",
Juerg Haefliger94319962007-06-09 10:11:16 -04001968 attr->name);
1969 }
1970}
1971
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001972static void dme1737_chmod_group(struct device *dev,
Juerg Haefliger94319962007-06-09 10:11:16 -04001973 const struct attribute_group *group,
1974 mode_t mode)
1975{
1976 struct attribute **attr;
1977
1978 for (attr = group->attrs; *attr; attr++) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001979 dme1737_chmod_file(dev, *attr, mode);
Juerg Haefliger94319962007-06-09 10:11:16 -04001980 }
1981}
1982
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001983static void dme1737_remove_files(struct device *dev)
Juerg Haefliger94319962007-06-09 10:11:16 -04001984{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001985 struct dme1737_data *data = dev_get_drvdata(dev);
1986 int ix;
1987
1988 for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
Juerg Haefligerea694432010-05-27 19:59:01 +02001989 if (data->has_features & HAS_FAN(ix)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001990 sysfs_remove_group(&dev->kobj,
1991 &dme1737_fan_group[ix]);
1992 }
1993 }
1994
1995 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
Juerg Haefligerea694432010-05-27 19:59:01 +02001996 if (data->has_features & HAS_PWM(ix)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001997 sysfs_remove_group(&dev->kobj,
1998 &dme1737_pwm_group[ix]);
Juerg Haefligerea694432010-05-27 19:59:01 +02001999 if ((data->has_features & HAS_PWM_MIN) && ix < 3) {
Juerg Haefliger549edb82008-08-06 22:41:03 +02002000 sysfs_remove_file(&dev->kobj,
Juerg Haefligerea694432010-05-27 19:59:01 +02002001 dme1737_auto_pwm_min_attr[ix]);
Juerg Haefliger549edb82008-08-06 22:41:03 +02002002 }
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002003 }
2004 }
2005
Juerg Haefligerea694432010-05-27 19:59:01 +02002006 if (data->has_features & HAS_TEMP_OFFSET) {
2007 sysfs_remove_group(&dev->kobj, &dme1737_temp_offset_group);
Juerg Haefliger549edb82008-08-06 22:41:03 +02002008 }
Juerg Haefligerea694432010-05-27 19:59:01 +02002009 if (data->has_features & HAS_VID) {
Jean Delvare9d091442009-10-24 13:28:46 +02002010 sysfs_remove_group(&dev->kobj, &dme1737_vid_group);
2011 }
Juerg Haefligerea694432010-05-27 19:59:01 +02002012 if (data->has_features & HAS_ZONE3) {
2013 sysfs_remove_group(&dev->kobj, &dme1737_zone3_group);
2014 }
2015 if (data->has_features & HAS_ZONE_HYST) {
2016 sysfs_remove_group(&dev->kobj, &dme1737_zone_hyst_group);
2017 }
Juerg Haefligerd4b94e12011-01-12 21:55:13 +01002018 if (data->has_features & HAS_IN7) {
2019 sysfs_remove_group(&dev->kobj, &dme1737_in7_group);
2020 }
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002021 sysfs_remove_group(&dev->kobj, &dme1737_group);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002022
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002023 if (!data->client) {
Juerg Haefligere95c2372007-10-07 21:27:35 -07002024 sysfs_remove_file(&dev->kobj, &dev_attr_name.attr);
2025 }
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002026}
2027
2028static int dme1737_create_files(struct device *dev)
2029{
2030 struct dme1737_data *data = dev_get_drvdata(dev);
2031 int err, ix;
2032
Juerg Haefligere95c2372007-10-07 21:27:35 -07002033 /* Create a name attribute for ISA devices */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002034 if (!data->client &&
Juerg Haefligere95c2372007-10-07 21:27:35 -07002035 (err = sysfs_create_file(&dev->kobj, &dev_attr_name.attr))) {
2036 goto exit;
2037 }
2038
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002039 /* Create standard sysfs attributes */
2040 if ((err = sysfs_create_group(&dev->kobj, &dme1737_group))) {
Juerg Haefligere95c2372007-10-07 21:27:35 -07002041 goto exit_remove;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002042 }
2043
Juerg Haefligerea694432010-05-27 19:59:01 +02002044 /* Create chip-dependent sysfs attributes */
2045 if ((data->has_features & HAS_TEMP_OFFSET) &&
Juerg Haefliger549edb82008-08-06 22:41:03 +02002046 (err = sysfs_create_group(&dev->kobj,
Juerg Haefligerea694432010-05-27 19:59:01 +02002047 &dme1737_temp_offset_group))) {
Juerg Haefliger549edb82008-08-06 22:41:03 +02002048 goto exit_remove;
2049 }
Juerg Haefligerea694432010-05-27 19:59:01 +02002050 if ((data->has_features & HAS_VID) &&
Jean Delvare9d091442009-10-24 13:28:46 +02002051 (err = sysfs_create_group(&dev->kobj,
2052 &dme1737_vid_group))) {
2053 goto exit_remove;
2054 }
Juerg Haefligerea694432010-05-27 19:59:01 +02002055 if ((data->has_features & HAS_ZONE3) &&
2056 (err = sysfs_create_group(&dev->kobj,
2057 &dme1737_zone3_group))) {
2058 goto exit_remove;
2059 }
2060 if ((data->has_features & HAS_ZONE_HYST) &&
2061 (err = sysfs_create_group(&dev->kobj,
2062 &dme1737_zone_hyst_group))) {
2063 goto exit_remove;
2064 }
Juerg Haefligerd4b94e12011-01-12 21:55:13 +01002065 if (data->has_features & HAS_IN7) {
2066 err = sysfs_create_group(&dev->kobj, &dme1737_in7_group);
2067 if (err) {
2068 goto exit_remove;
2069 }
2070 }
Jean Delvare9d091442009-10-24 13:28:46 +02002071
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002072 /* Create fan sysfs attributes */
2073 for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
Juerg Haefligerea694432010-05-27 19:59:01 +02002074 if (data->has_features & HAS_FAN(ix)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002075 if ((err = sysfs_create_group(&dev->kobj,
2076 &dme1737_fan_group[ix]))) {
2077 goto exit_remove;
2078 }
2079 }
2080 }
2081
2082 /* Create PWM sysfs attributes */
2083 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
Juerg Haefligerea694432010-05-27 19:59:01 +02002084 if (data->has_features & HAS_PWM(ix)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002085 if ((err = sysfs_create_group(&dev->kobj,
2086 &dme1737_pwm_group[ix]))) {
2087 goto exit_remove;
2088 }
Juerg Haefligerea694432010-05-27 19:59:01 +02002089 if ((data->has_features & HAS_PWM_MIN) && ix < 3 &&
Juerg Haefliger549edb82008-08-06 22:41:03 +02002090 (err = sysfs_create_file(&dev->kobj,
Juerg Haefligerea694432010-05-27 19:59:01 +02002091 dme1737_auto_pwm_min_attr[ix]))) {
Juerg Haefliger549edb82008-08-06 22:41:03 +02002092 goto exit_remove;
2093 }
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002094 }
2095 }
2096
2097 /* Inform if the device is locked. Otherwise change the permissions of
2098 * selected attributes from read-only to read-writeable. */
2099 if (data->config & 0x02) {
2100 dev_info(dev, "Device is locked. Some attributes "
2101 "will be read-only.\n");
2102 } else {
Juerg Haefliger549edb82008-08-06 22:41:03 +02002103 /* Change permissions of zone sysfs attributes */
2104 dme1737_chmod_group(dev, &dme1737_zone_chmod_group,
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002105 S_IRUGO | S_IWUSR);
2106
Juerg Haefligerea694432010-05-27 19:59:01 +02002107 /* Change permissions of chip-dependent sysfs attributes */
2108 if (data->has_features & HAS_TEMP_OFFSET) {
2109 dme1737_chmod_group(dev, &dme1737_temp_offset_group,
2110 S_IRUGO | S_IWUSR);
2111 }
2112 if (data->has_features & HAS_ZONE3) {
2113 dme1737_chmod_group(dev, &dme1737_zone3_chmod_group,
2114 S_IRUGO | S_IWUSR);
2115 }
2116 if (data->has_features & HAS_ZONE_HYST) {
2117 dme1737_chmod_group(dev, &dme1737_zone_hyst_group,
Juerg Haefliger549edb82008-08-06 22:41:03 +02002118 S_IRUGO | S_IWUSR);
2119 }
2120
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02002121 /* Change permissions of PWM sysfs attributes */
2122 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_chmod_group); ix++) {
Juerg Haefligerea694432010-05-27 19:59:01 +02002123 if (data->has_features & HAS_PWM(ix)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002124 dme1737_chmod_group(dev,
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02002125 &dme1737_pwm_chmod_group[ix],
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002126 S_IRUGO | S_IWUSR);
Juerg Haefligerea694432010-05-27 19:59:01 +02002127 if ((data->has_features & HAS_PWM_MIN) &&
2128 ix < 3) {
Juerg Haefliger549edb82008-08-06 22:41:03 +02002129 dme1737_chmod_file(dev,
Juerg Haefligerea694432010-05-27 19:59:01 +02002130 dme1737_auto_pwm_min_attr[ix],
Juerg Haefliger549edb82008-08-06 22:41:03 +02002131 S_IRUGO | S_IWUSR);
2132 }
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002133 }
2134 }
2135
2136 /* Change permissions of pwm[1-3] if in manual mode */
2137 for (ix = 0; ix < 3; ix++) {
Juerg Haefligerea694432010-05-27 19:59:01 +02002138 if ((data->has_features & HAS_PWM(ix)) &&
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002139 (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) {
2140 dme1737_chmod_file(dev,
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02002141 dme1737_pwm_chmod_attr[ix],
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002142 S_IRUGO | S_IWUSR);
2143 }
2144 }
2145 }
2146
2147 return 0;
2148
2149exit_remove:
2150 dme1737_remove_files(dev);
2151exit:
2152 return err;
2153}
2154
2155static int dme1737_init_device(struct device *dev)
2156{
2157 struct dme1737_data *data = dev_get_drvdata(dev);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002158 struct i2c_client *client = data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -04002159 int ix;
2160 u8 reg;
2161
Juerg Haefliger549edb82008-08-06 22:41:03 +02002162 /* Point to the right nominal voltages array */
2163 data->in_nominal = IN_NOMINAL(data->type);
2164
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002165 data->config = dme1737_read(data, DME1737_REG_CONFIG);
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002166 /* Inform if part is not monitoring/started */
2167 if (!(data->config & 0x01)) {
2168 if (!force_start) {
2169 dev_err(dev, "Device is not monitoring. "
2170 "Use the force_start load parameter to "
2171 "override.\n");
2172 return -EFAULT;
2173 }
Juerg Haefliger94319962007-06-09 10:11:16 -04002174
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002175 /* Force monitoring */
2176 data->config |= 0x01;
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002177 dme1737_write(data, DME1737_REG_CONFIG, data->config);
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002178 }
Juerg Haefliger94319962007-06-09 10:11:16 -04002179 /* Inform if part is not ready */
2180 if (!(data->config & 0x04)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002181 dev_err(dev, "Device is not ready.\n");
Juerg Haefliger94319962007-06-09 10:11:16 -04002182 return -EFAULT;
2183 }
2184
Juerg Haefligerea694432010-05-27 19:59:01 +02002185 /* Determine which optional fan and pwm features are enabled (only
2186 * valid for I2C devices) */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002187 if (client) { /* I2C chip */
2188 data->config2 = dme1737_read(data, DME1737_REG_CONFIG2);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002189 /* Check if optional fan3 input is enabled */
2190 if (data->config2 & 0x04) {
Juerg Haefligerea694432010-05-27 19:59:01 +02002191 data->has_features |= HAS_FAN(2);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002192 }
2193
2194 /* Fan4 and pwm3 are only available if the client's I2C address
2195 * is the default 0x2e. Otherwise the I/Os associated with
2196 * these functions are used for addr enable/select. */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002197 if (client->addr == 0x2e) {
Juerg Haefligerea694432010-05-27 19:59:01 +02002198 data->has_features |= HAS_FAN(3) | HAS_PWM(2);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002199 }
2200
2201 /* Determine which of the optional fan[5-6] and pwm[5-6]
2202 * features are enabled. For this, we need to query the runtime
2203 * registers through the Super-IO LPC interface. Try both
2204 * config ports 0x2e and 0x4e. */
2205 if (dme1737_i2c_get_features(0x2e, data) &&
2206 dme1737_i2c_get_features(0x4e, data)) {
2207 dev_warn(dev, "Failed to query Super-IO for optional "
2208 "features.\n");
2209 }
Juerg Haefliger94319962007-06-09 10:11:16 -04002210 }
2211
Juerg Haefligerea694432010-05-27 19:59:01 +02002212 /* Fan[1-2] and pwm[1-2] are present in all chips */
2213 data->has_features |= HAS_FAN(0) | HAS_FAN(1) | HAS_PWM(0) | HAS_PWM(1);
2214
2215 /* Chip-dependent features */
2216 switch (data->type) {
2217 case dme1737:
2218 data->has_features |= HAS_TEMP_OFFSET | HAS_VID | HAS_ZONE3 |
2219 HAS_ZONE_HYST | HAS_PWM_MIN;
2220 break;
2221 case sch311x:
2222 data->has_features |= HAS_TEMP_OFFSET | HAS_ZONE3 |
2223 HAS_ZONE_HYST | HAS_PWM_MIN | HAS_FAN(2) | HAS_PWM(2);
2224 break;
2225 case sch5027:
2226 data->has_features |= HAS_ZONE3;
2227 break;
2228 case sch5127:
Juerg Haefligerd4b94e12011-01-12 21:55:13 +01002229 data->has_features |= HAS_FAN(2) | HAS_PWM(2) | HAS_IN7;
Juerg Haefligerea694432010-05-27 19:59:01 +02002230 break;
2231 default:
2232 break;
2233 }
Juerg Haefliger94319962007-06-09 10:11:16 -04002234
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002235 dev_info(dev, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, "
Juerg Haefliger94319962007-06-09 10:11:16 -04002236 "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n",
Juerg Haefligerea694432010-05-27 19:59:01 +02002237 (data->has_features & HAS_PWM(2)) ? "yes" : "no",
2238 (data->has_features & HAS_PWM(4)) ? "yes" : "no",
2239 (data->has_features & HAS_PWM(5)) ? "yes" : "no",
2240 (data->has_features & HAS_FAN(2)) ? "yes" : "no",
2241 (data->has_features & HAS_FAN(3)) ? "yes" : "no",
2242 (data->has_features & HAS_FAN(4)) ? "yes" : "no",
2243 (data->has_features & HAS_FAN(5)) ? "yes" : "no");
Juerg Haefliger94319962007-06-09 10:11:16 -04002244
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002245 reg = dme1737_read(data, DME1737_REG_TACH_PWM);
Juerg Haefliger94319962007-06-09 10:11:16 -04002246 /* Inform if fan-to-pwm mapping differs from the default */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002247 if (client && reg != 0xa4) { /* I2C chip */
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002248 dev_warn(dev, "Non-standard fan to pwm mapping: "
Juerg Haefliger94319962007-06-09 10:11:16 -04002249 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, "
2250 "fan4->pwm%d. Please report to the driver "
2251 "maintainer.\n",
2252 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
2253 ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002254 } else if (!client && reg != 0x24) { /* ISA chip */
Juerg Haefligere95c2372007-10-07 21:27:35 -07002255 dev_warn(dev, "Non-standard fan to pwm mapping: "
2256 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. "
2257 "Please report to the driver maintainer.\n",
2258 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
2259 ((reg >> 4) & 0x03) + 1);
Juerg Haefliger94319962007-06-09 10:11:16 -04002260 }
2261
2262 /* Switch pwm[1-3] to manual mode if they are currently disabled and
2263 * set the duty-cycles to 0% (which is identical to the PWMs being
2264 * disabled). */
2265 if (!(data->config & 0x02)) {
2266 for (ix = 0; ix < 3; ix++) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002267 data->pwm_config[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04002268 DME1737_REG_PWM_CONFIG(ix));
Juerg Haefligerea694432010-05-27 19:59:01 +02002269 if ((data->has_features & HAS_PWM(ix)) &&
Juerg Haefliger94319962007-06-09 10:11:16 -04002270 (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002271 dev_info(dev, "Switching pwm%d to "
Juerg Haefliger94319962007-06-09 10:11:16 -04002272 "manual mode.\n", ix + 1);
2273 data->pwm_config[ix] = PWM_EN_TO_REG(1,
2274 data->pwm_config[ix]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002275 dme1737_write(data, DME1737_REG_PWM(ix), 0);
2276 dme1737_write(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04002277 DME1737_REG_PWM_CONFIG(ix),
2278 data->pwm_config[ix]);
2279 }
2280 }
2281 }
2282
2283 /* Initialize the default PWM auto channels zone (acz) assignments */
2284 data->pwm_acz[0] = 1; /* pwm1 -> zone1 */
2285 data->pwm_acz[1] = 2; /* pwm2 -> zone2 */
2286 data->pwm_acz[2] = 4; /* pwm3 -> zone3 */
2287
2288 /* Set VRM */
Juerg Haefligerea694432010-05-27 19:59:01 +02002289 if (data->has_features & HAS_VID) {
Juerg Haefliger549edb82008-08-06 22:41:03 +02002290 data->vrm = vid_which_vrm();
2291 }
Juerg Haefliger94319962007-06-09 10:11:16 -04002292
2293 return 0;
2294}
2295
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002296/* ---------------------------------------------------------------------
2297 * I2C device detection and registration
2298 * --------------------------------------------------------------------- */
2299
2300static struct i2c_driver dme1737_i2c_driver;
2301
2302static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data)
2303{
Juerg Haefliger94319962007-06-09 10:11:16 -04002304 int err = 0, reg;
2305 u16 addr;
2306
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002307 dme1737_sio_enter(sio_cip);
Juerg Haefliger94319962007-06-09 10:11:16 -04002308
2309 /* Check device ID
Juerg Haefligerea694432010-05-27 19:59:01 +02002310 * We currently know about two kinds of DME1737 and SCH5027. */
Juerg Haefliger345a2222008-01-26 08:54:24 -08002311 reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
Juerg Haefligerea694432010-05-27 19:59:01 +02002312 if (!(reg == DME1737_ID_1 || reg == DME1737_ID_2 ||
2313 reg == SCH5027_ID)) {
Juerg Haefliger94319962007-06-09 10:11:16 -04002314 err = -ENODEV;
2315 goto exit;
2316 }
2317
2318 /* Select logical device A (runtime registers) */
2319 dme1737_sio_outb(sio_cip, 0x07, 0x0a);
2320
2321 /* Get the base address of the runtime registers */
2322 if (!(addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
2323 dme1737_sio_inb(sio_cip, 0x61))) {
2324 err = -ENODEV;
2325 goto exit;
2326 }
2327
2328 /* Read the runtime registers to determine which optional features
2329 * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set
2330 * to '10' if the respective feature is enabled. */
2331 if ((inb(addr + 0x43) & 0x0c) == 0x08) { /* fan6 */
Juerg Haefligerea694432010-05-27 19:59:01 +02002332 data->has_features |= HAS_FAN(5);
Juerg Haefliger94319962007-06-09 10:11:16 -04002333 }
2334 if ((inb(addr + 0x44) & 0x0c) == 0x08) { /* pwm6 */
Juerg Haefligerea694432010-05-27 19:59:01 +02002335 data->has_features |= HAS_PWM(5);
Juerg Haefliger94319962007-06-09 10:11:16 -04002336 }
2337 if ((inb(addr + 0x45) & 0x0c) == 0x08) { /* fan5 */
Juerg Haefligerea694432010-05-27 19:59:01 +02002338 data->has_features |= HAS_FAN(4);
Juerg Haefliger94319962007-06-09 10:11:16 -04002339 }
2340 if ((inb(addr + 0x46) & 0x0c) == 0x08) { /* pwm5 */
Juerg Haefligerea694432010-05-27 19:59:01 +02002341 data->has_features |= HAS_PWM(4);
Juerg Haefliger94319962007-06-09 10:11:16 -04002342 }
2343
2344exit:
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002345 dme1737_sio_exit(sio_cip);
Juerg Haefliger94319962007-06-09 10:11:16 -04002346
2347 return err;
2348}
2349
Jean Delvare67a37302008-10-14 17:30:04 +02002350/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01002351static int dme1737_i2c_detect(struct i2c_client *client,
Jean Delvare67a37302008-10-14 17:30:04 +02002352 struct i2c_board_info *info)
Juerg Haefliger94319962007-06-09 10:11:16 -04002353{
Jean Delvare67a37302008-10-14 17:30:04 +02002354 struct i2c_adapter *adapter = client->adapter;
2355 struct device *dev = &adapter->dev;
Juerg Haefliger94319962007-06-09 10:11:16 -04002356 u8 company, verstep = 0;
Juerg Haefliger94319962007-06-09 10:11:16 -04002357 const char *name;
2358
2359 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Jean Delvare67a37302008-10-14 17:30:04 +02002360 return -ENODEV;
Juerg Haefliger94319962007-06-09 10:11:16 -04002361 }
2362
Jean Delvare52df6442009-12-09 20:35:57 +01002363 company = i2c_smbus_read_byte_data(client, DME1737_REG_COMPANY);
2364 verstep = i2c_smbus_read_byte_data(client, DME1737_REG_VERSTEP);
Juerg Haefliger94319962007-06-09 10:11:16 -04002365
Jean Delvare52df6442009-12-09 20:35:57 +01002366 if (company == DME1737_COMPANY_SMSC &&
2367 verstep == SCH5027_VERSTEP) {
Juerg Haefliger549edb82008-08-06 22:41:03 +02002368 name = "sch5027";
Jean Delvare52df6442009-12-09 20:35:57 +01002369 } else if (company == DME1737_COMPANY_SMSC &&
2370 (verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP) {
Juerg Haefliger549edb82008-08-06 22:41:03 +02002371 name = "dme1737";
Jean Delvare52df6442009-12-09 20:35:57 +01002372 } else {
2373 return -ENODEV;
Juerg Haefliger549edb82008-08-06 22:41:03 +02002374 }
Juerg Haefliger94319962007-06-09 10:11:16 -04002375
Juerg Haefliger549edb82008-08-06 22:41:03 +02002376 dev_info(dev, "Found a %s chip at 0x%02x (rev 0x%02x).\n",
Jean Delvare52df6442009-12-09 20:35:57 +01002377 verstep == SCH5027_VERSTEP ? "SCH5027" : "DME1737",
2378 client->addr, verstep);
Jean Delvare67a37302008-10-14 17:30:04 +02002379 strlcpy(info->type, name, I2C_NAME_SIZE);
2380
2381 return 0;
2382}
2383
2384static int dme1737_i2c_probe(struct i2c_client *client,
2385 const struct i2c_device_id *id)
2386{
2387 struct dme1737_data *data;
2388 struct device *dev = &client->dev;
2389 int err;
2390
2391 data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL);
2392 if (!data) {
2393 err = -ENOMEM;
2394 goto exit;
2395 }
2396
2397 i2c_set_clientdata(client, data);
2398 data->type = id->driver_data;
2399 data->client = client;
2400 data->name = client->name;
2401 mutex_init(&data->update_lock);
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002402
Juerg Haefliger94319962007-06-09 10:11:16 -04002403 /* Initialize the DME1737 chip */
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002404 if ((err = dme1737_init_device(dev))) {
2405 dev_err(dev, "Failed to initialize device.\n");
Jean Delvare67a37302008-10-14 17:30:04 +02002406 goto exit_kfree;
Juerg Haefliger94319962007-06-09 10:11:16 -04002407 }
2408
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002409 /* Create sysfs files */
2410 if ((err = dme1737_create_files(dev))) {
2411 dev_err(dev, "Failed to create sysfs files.\n");
Jean Delvare67a37302008-10-14 17:30:04 +02002412 goto exit_kfree;
Juerg Haefliger94319962007-06-09 10:11:16 -04002413 }
2414
2415 /* Register device */
Mark M. Hoffman62ee3e12007-10-10 22:32:50 -04002416 data->hwmon_dev = hwmon_device_register(dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07002417 if (IS_ERR(data->hwmon_dev)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002418 dev_err(dev, "Failed to register device.\n");
Tony Jones1beeffe2007-08-20 13:46:20 -07002419 err = PTR_ERR(data->hwmon_dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04002420 goto exit_remove;
2421 }
2422
Juerg Haefliger94319962007-06-09 10:11:16 -04002423 return 0;
2424
2425exit_remove:
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002426 dme1737_remove_files(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04002427exit_kfree:
2428 kfree(data);
2429exit:
2430 return err;
2431}
2432
Jean Delvare67a37302008-10-14 17:30:04 +02002433static int dme1737_i2c_remove(struct i2c_client *client)
Juerg Haefliger94319962007-06-09 10:11:16 -04002434{
2435 struct dme1737_data *data = i2c_get_clientdata(client);
Juerg Haefliger94319962007-06-09 10:11:16 -04002436
Tony Jones1beeffe2007-08-20 13:46:20 -07002437 hwmon_device_unregister(data->hwmon_dev);
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002438 dme1737_remove_files(&client->dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04002439
Juerg Haefliger94319962007-06-09 10:11:16 -04002440 kfree(data);
2441 return 0;
2442}
2443
Jean Delvare67a37302008-10-14 17:30:04 +02002444static const struct i2c_device_id dme1737_id[] = {
2445 { "dme1737", dme1737 },
2446 { "sch5027", sch5027 },
2447 { }
2448};
2449MODULE_DEVICE_TABLE(i2c, dme1737_id);
2450
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002451static struct i2c_driver dme1737_i2c_driver = {
Jean Delvare67a37302008-10-14 17:30:04 +02002452 .class = I2C_CLASS_HWMON,
Juerg Haefliger94319962007-06-09 10:11:16 -04002453 .driver = {
2454 .name = "dme1737",
2455 },
Jean Delvare67a37302008-10-14 17:30:04 +02002456 .probe = dme1737_i2c_probe,
2457 .remove = dme1737_i2c_remove,
2458 .id_table = dme1737_id,
2459 .detect = dme1737_i2c_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01002460 .address_list = normal_i2c,
Juerg Haefliger94319962007-06-09 10:11:16 -04002461};
2462
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002463/* ---------------------------------------------------------------------
Juerg Haefligere95c2372007-10-07 21:27:35 -07002464 * ISA device detection and registration
2465 * --------------------------------------------------------------------- */
2466
2467static int __init dme1737_isa_detect(int sio_cip, unsigned short *addr)
2468{
2469 int err = 0, reg;
2470 unsigned short base_addr;
2471
2472 dme1737_sio_enter(sio_cip);
2473
2474 /* Check device ID
Juerg Haefligerea694432010-05-27 19:59:01 +02002475 * We currently know about SCH3112, SCH3114, SCH3116, and SCH5127 */
Jean Delvare67b671b2007-12-06 23:13:42 +01002476 reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
Juerg Haefligerea694432010-05-27 19:59:01 +02002477 if (!(reg == SCH3112_ID || reg == SCH3114_ID || reg == SCH3116_ID ||
2478 reg == SCH5127_ID)) {
Juerg Haefligere95c2372007-10-07 21:27:35 -07002479 err = -ENODEV;
2480 goto exit;
2481 }
2482
2483 /* Select logical device A (runtime registers) */
2484 dme1737_sio_outb(sio_cip, 0x07, 0x0a);
2485
2486 /* Get the base address of the runtime registers */
2487 if (!(base_addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
2488 dme1737_sio_inb(sio_cip, 0x61))) {
Joe Perches9c6e13b2010-10-20 06:51:32 +00002489 pr_err("Base address not set\n");
Juerg Haefligere95c2372007-10-07 21:27:35 -07002490 err = -ENODEV;
2491 goto exit;
2492 }
2493
2494 /* Access to the hwmon registers is through an index/data register
2495 * pair located at offset 0x70/0x71. */
2496 *addr = base_addr + 0x70;
2497
2498exit:
2499 dme1737_sio_exit(sio_cip);
2500 return err;
2501}
2502
2503static int __init dme1737_isa_device_add(unsigned short addr)
2504{
2505 struct resource res = {
2506 .start = addr,
2507 .end = addr + DME1737_EXTENT - 1,
2508 .name = "dme1737",
2509 .flags = IORESOURCE_IO,
2510 };
2511 int err;
2512
Jean Delvareb9acb642009-01-07 16:37:35 +01002513 err = acpi_check_resource_conflict(&res);
2514 if (err)
2515 goto exit;
2516
Juerg Haefligere95c2372007-10-07 21:27:35 -07002517 if (!(pdev = platform_device_alloc("dme1737", addr))) {
Joe Perches9c6e13b2010-10-20 06:51:32 +00002518 pr_err("Failed to allocate device\n");
Juerg Haefligere95c2372007-10-07 21:27:35 -07002519 err = -ENOMEM;
2520 goto exit;
2521 }
2522
2523 if ((err = platform_device_add_resources(pdev, &res, 1))) {
Joe Perches9c6e13b2010-10-20 06:51:32 +00002524 pr_err("Failed to add device resource (err = %d)\n", err);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002525 goto exit_device_put;
2526 }
2527
2528 if ((err = platform_device_add(pdev))) {
Joe Perches9c6e13b2010-10-20 06:51:32 +00002529 pr_err("Failed to add device (err = %d)\n", err);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002530 goto exit_device_put;
2531 }
2532
2533 return 0;
2534
2535exit_device_put:
2536 platform_device_put(pdev);
2537 pdev = NULL;
2538exit:
2539 return err;
2540}
2541
2542static int __devinit dme1737_isa_probe(struct platform_device *pdev)
2543{
2544 u8 company, device;
2545 struct resource *res;
Juerg Haefligere95c2372007-10-07 21:27:35 -07002546 struct dme1737_data *data;
2547 struct device *dev = &pdev->dev;
2548 int err;
2549
2550 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2551 if (!request_region(res->start, DME1737_EXTENT, "dme1737")) {
2552 dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
2553 (unsigned short)res->start,
2554 (unsigned short)res->start + DME1737_EXTENT - 1);
2555 err = -EBUSY;
2556 goto exit;
2557 }
2558
2559 if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
2560 err = -ENOMEM;
2561 goto exit_release_region;
2562 }
2563
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002564 data->addr = res->start;
Juerg Haefligere95c2372007-10-07 21:27:35 -07002565 platform_set_drvdata(pdev, data);
2566
Juerg Haefliger55d68d72008-08-06 22:41:03 +02002567 /* Skip chip detection if module is loaded with force_id parameter */
Juerg Haefligerea694432010-05-27 19:59:01 +02002568 switch (force_id) {
2569 case SCH3112_ID:
2570 case SCH3114_ID:
2571 case SCH3116_ID:
2572 data->type = sch311x;
2573 break;
2574 case SCH5127_ID:
2575 data->type = sch5127;
2576 break;
2577 default:
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002578 company = dme1737_read(data, DME1737_REG_COMPANY);
2579 device = dme1737_read(data, DME1737_REG_DEVICE);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002580
Juerg Haefligerea694432010-05-27 19:59:01 +02002581 if ((company == DME1737_COMPANY_SMSC) &&
2582 (device == SCH311X_DEVICE)) {
2583 data->type = sch311x;
2584 } else if ((company == DME1737_COMPANY_SMSC) &&
2585 (device == SCH5127_DEVICE)) {
2586 data->type = sch5127;
2587 } else {
Juerg Haefliger55d68d72008-08-06 22:41:03 +02002588 err = -ENODEV;
2589 goto exit_kfree;
2590 }
Juerg Haefligere95c2372007-10-07 21:27:35 -07002591 }
2592
Juerg Haefligerea694432010-05-27 19:59:01 +02002593 if (data->type == sch5127) {
2594 data->name = "sch5127";
2595 } else {
2596 data->name = "sch311x";
2597 }
2598
2599 /* Initialize the mutex */
Juerg Haefligere95c2372007-10-07 21:27:35 -07002600 mutex_init(&data->update_lock);
2601
Juerg Haefligerea694432010-05-27 19:59:01 +02002602 dev_info(dev, "Found a %s chip at 0x%04x\n",
2603 data->type == sch5127 ? "SCH5127" : "SCH311x", data->addr);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002604
2605 /* Initialize the chip */
2606 if ((err = dme1737_init_device(dev))) {
2607 dev_err(dev, "Failed to initialize device.\n");
2608 goto exit_kfree;
2609 }
2610
2611 /* Create sysfs files */
2612 if ((err = dme1737_create_files(dev))) {
2613 dev_err(dev, "Failed to create sysfs files.\n");
2614 goto exit_kfree;
2615 }
2616
2617 /* Register device */
2618 data->hwmon_dev = hwmon_device_register(dev);
2619 if (IS_ERR(data->hwmon_dev)) {
2620 dev_err(dev, "Failed to register device.\n");
2621 err = PTR_ERR(data->hwmon_dev);
2622 goto exit_remove_files;
2623 }
2624
2625 return 0;
2626
2627exit_remove_files:
2628 dme1737_remove_files(dev);
2629exit_kfree:
2630 platform_set_drvdata(pdev, NULL);
2631 kfree(data);
2632exit_release_region:
2633 release_region(res->start, DME1737_EXTENT);
2634exit:
2635 return err;
2636}
2637
2638static int __devexit dme1737_isa_remove(struct platform_device *pdev)
2639{
2640 struct dme1737_data *data = platform_get_drvdata(pdev);
2641
2642 hwmon_device_unregister(data->hwmon_dev);
2643 dme1737_remove_files(&pdev->dev);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002644 release_region(data->addr, DME1737_EXTENT);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002645 platform_set_drvdata(pdev, NULL);
2646 kfree(data);
2647
2648 return 0;
2649}
2650
2651static struct platform_driver dme1737_isa_driver = {
2652 .driver = {
2653 .owner = THIS_MODULE,
2654 .name = "dme1737",
2655 },
2656 .probe = dme1737_isa_probe,
2657 .remove = __devexit_p(dme1737_isa_remove),
2658};
2659
2660/* ---------------------------------------------------------------------
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002661 * Module initialization and cleanup
2662 * --------------------------------------------------------------------- */
2663
Juerg Haefliger94319962007-06-09 10:11:16 -04002664static int __init dme1737_init(void)
2665{
Juerg Haefligere95c2372007-10-07 21:27:35 -07002666 int err;
2667 unsigned short addr;
2668
2669 if ((err = i2c_add_driver(&dme1737_i2c_driver))) {
2670 goto exit;
2671 }
2672
2673 if (dme1737_isa_detect(0x2e, &addr) &&
Juerg Haefliger92430b62008-04-03 21:34:19 -07002674 dme1737_isa_detect(0x4e, &addr) &&
2675 (!probe_all_addr ||
2676 (dme1737_isa_detect(0x162e, &addr) &&
2677 dme1737_isa_detect(0x164e, &addr)))) {
Juerg Haefligere95c2372007-10-07 21:27:35 -07002678 /* Return 0 if we didn't find an ISA device */
2679 return 0;
2680 }
2681
2682 if ((err = platform_driver_register(&dme1737_isa_driver))) {
2683 goto exit_del_i2c_driver;
2684 }
2685
2686 /* Sets global pdev as a side effect */
2687 if ((err = dme1737_isa_device_add(addr))) {
2688 goto exit_del_isa_driver;
2689 }
2690
2691 return 0;
2692
2693exit_del_isa_driver:
2694 platform_driver_unregister(&dme1737_isa_driver);
2695exit_del_i2c_driver:
2696 i2c_del_driver(&dme1737_i2c_driver);
2697exit:
2698 return err;
Juerg Haefliger94319962007-06-09 10:11:16 -04002699}
2700
2701static void __exit dme1737_exit(void)
2702{
Juerg Haefligere95c2372007-10-07 21:27:35 -07002703 if (pdev) {
2704 platform_device_unregister(pdev);
2705 platform_driver_unregister(&dme1737_isa_driver);
2706 }
2707
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002708 i2c_del_driver(&dme1737_i2c_driver);
Juerg Haefliger94319962007-06-09 10:11:16 -04002709}
2710
2711MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
2712MODULE_DESCRIPTION("DME1737 sensors");
2713MODULE_LICENSE("GPL");
2714
2715module_init(dme1737_init);
2716module_exit(dme1737_exit);