blob: 5a3d41fbdb3c65eb4a20932af5f800e3ca6fd450 [file] [log] [blame]
Juerg Haefliger94319962007-06-09 10:11:16 -04001/*
Juerg Haefligere95c2372007-10-07 21:27:35 -07002 * dme1737.c - Driver for the SMSC DME1737, Asus A8000, and SMSC SCH311x
3 * Super-I/O chips integrated hardware monitoring features.
Juerg Haefliger94319962007-06-09 10:11:16 -04004 * Copyright (c) 2007 Juerg Haefliger <juergh@gmail.com>
5 *
Juerg Haefligere95c2372007-10-07 21:27:35 -07006 * This driver is an I2C/ISA hybrid, meaning that it uses the I2C bus to access
7 * the chip registers if a DME1737 (or A8000) is found and the ISA bus if a
8 * SCH311x chip is found. Both types of chips have very similar hardware
9 * monitoring capabilities but differ in the way they can be accessed.
Juerg Haefliger94319962007-06-09 10:11:16 -040010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/jiffies.h>
30#include <linux/i2c.h>
Juerg Haefligere95c2372007-10-07 21:27:35 -070031#include <linux/platform_device.h>
Juerg Haefliger94319962007-06-09 10:11:16 -040032#include <linux/hwmon.h>
33#include <linux/hwmon-sysfs.h>
34#include <linux/hwmon-vid.h>
35#include <linux/err.h>
36#include <linux/mutex.h>
37#include <asm/io.h>
38
Juerg Haefligere95c2372007-10-07 21:27:35 -070039/* ISA device, if found */
40static struct platform_device *pdev;
41
Juerg Haefliger94319962007-06-09 10:11:16 -040042/* Module load parameters */
43static int force_start;
44module_param(force_start, bool, 0);
45MODULE_PARM_DESC(force_start, "Force the chip to start monitoring inputs");
46
Jean Delvare67b671b2007-12-06 23:13:42 +010047static unsigned short force_id;
48module_param(force_id, ushort, 0);
49MODULE_PARM_DESC(force_id, "Override the detected device ID");
50
Juerg Haefliger92430b62008-04-03 21:34:19 -070051static int probe_all_addr;
52module_param(probe_all_addr, bool, 0);
53MODULE_PARM_DESC(probe_all_addr, "Include probing of non-standard LPC "
54 "addresses");
55
Juerg Haefliger94319962007-06-09 10:11:16 -040056/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050057static const unsigned short normal_i2c[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END};
Juerg Haefliger94319962007-06-09 10:11:16 -040058
59/* Insmod parameters */
60I2C_CLIENT_INSMOD_1(dme1737);
61
62/* ---------------------------------------------------------------------
63 * Registers
64 *
65 * The sensors are defined as follows:
66 *
67 * Voltages Temperatures
68 * -------- ------------
69 * in0 +5VTR (+5V stdby) temp1 Remote diode 1
70 * in1 Vccp (proc core) temp2 Internal temp
71 * in2 VCC (internal +3.3V) temp3 Remote diode 2
72 * in3 +5V
73 * in4 +12V
74 * in5 VTR (+3.3V stby)
75 * in6 Vbat
76 *
77 * --------------------------------------------------------------------- */
78
79/* Voltages (in) numbered 0-6 (ix) */
80#define DME1737_REG_IN(ix) ((ix) < 5 ? 0x20 + (ix) \
81 : 0x94 + (ix))
82#define DME1737_REG_IN_MIN(ix) ((ix) < 5 ? 0x44 + (ix) * 2 \
83 : 0x91 + (ix) * 2)
84#define DME1737_REG_IN_MAX(ix) ((ix) < 5 ? 0x45 + (ix) * 2 \
85 : 0x92 + (ix) * 2)
86
87/* Temperatures (temp) numbered 0-2 (ix) */
88#define DME1737_REG_TEMP(ix) (0x25 + (ix))
89#define DME1737_REG_TEMP_MIN(ix) (0x4e + (ix) * 2)
90#define DME1737_REG_TEMP_MAX(ix) (0x4f + (ix) * 2)
91#define DME1737_REG_TEMP_OFFSET(ix) ((ix) == 0 ? 0x1f \
92 : 0x1c + (ix))
93
94/* Voltage and temperature LSBs
95 * The LSBs (4 bits each) are stored in 5 registers with the following layouts:
96 * IN_TEMP_LSB(0) = [in5, in6]
97 * IN_TEMP_LSB(1) = [temp3, temp1]
98 * IN_TEMP_LSB(2) = [in4, temp2]
99 * IN_TEMP_LSB(3) = [in3, in0]
100 * IN_TEMP_LSB(4) = [in2, in1] */
101#define DME1737_REG_IN_TEMP_LSB(ix) (0x84 + (ix))
102static const u8 DME1737_REG_IN_LSB[] = {3, 4, 4, 3, 2, 0, 0};
103static const u8 DME1737_REG_IN_LSB_SHL[] = {4, 4, 0, 0, 0, 0, 4};
104static const u8 DME1737_REG_TEMP_LSB[] = {1, 2, 1};
105static const u8 DME1737_REG_TEMP_LSB_SHL[] = {4, 4, 0};
106
107/* Fans numbered 0-5 (ix) */
108#define DME1737_REG_FAN(ix) ((ix) < 4 ? 0x28 + (ix) * 2 \
109 : 0xa1 + (ix) * 2)
110#define DME1737_REG_FAN_MIN(ix) ((ix) < 4 ? 0x54 + (ix) * 2 \
111 : 0xa5 + (ix) * 2)
112#define DME1737_REG_FAN_OPT(ix) ((ix) < 4 ? 0x90 + (ix) \
113 : 0xb2 + (ix))
114#define DME1737_REG_FAN_MAX(ix) (0xb4 + (ix)) /* only for fan[4-5] */
115
116/* PWMs numbered 0-2, 4-5 (ix) */
117#define DME1737_REG_PWM(ix) ((ix) < 3 ? 0x30 + (ix) \
118 : 0xa1 + (ix))
119#define DME1737_REG_PWM_CONFIG(ix) (0x5c + (ix)) /* only for pwm[0-2] */
120#define DME1737_REG_PWM_MIN(ix) (0x64 + (ix)) /* only for pwm[0-2] */
121#define DME1737_REG_PWM_FREQ(ix) ((ix) < 3 ? 0x5f + (ix) \
122 : 0xa3 + (ix))
123/* The layout of the ramp rate registers is different from the other pwm
124 * registers. The bits for the 3 PWMs are stored in 2 registers:
125 * PWM_RR(0) = [OFF3, OFF2, OFF1, RES, RR1E, RR1-2, RR1-1, RR1-0]
126 * PWM_RR(1) = [RR2E, RR2-2, RR2-1, RR2-0, RR3E, RR3-2, RR3-1, RR3-0] */
127#define DME1737_REG_PWM_RR(ix) (0x62 + (ix)) /* only for pwm[0-2] */
128
129/* Thermal zones 0-2 */
130#define DME1737_REG_ZONE_LOW(ix) (0x67 + (ix))
131#define DME1737_REG_ZONE_ABS(ix) (0x6a + (ix))
132/* The layout of the hysteresis registers is different from the other zone
133 * registers. The bits for the 3 zones are stored in 2 registers:
134 * ZONE_HYST(0) = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
135 * ZONE_HYST(1) = [H3-3, H3-2, H3-1, H3-0, RES, RES, RES, RES] */
136#define DME1737_REG_ZONE_HYST(ix) (0x6d + (ix))
137
138/* Alarm registers and bit mapping
139 * The 3 8-bit alarm registers will be concatenated to a single 32-bit
140 * alarm value [0, ALARM3, ALARM2, ALARM1]. */
141#define DME1737_REG_ALARM1 0x41
142#define DME1737_REG_ALARM2 0x42
143#define DME1737_REG_ALARM3 0x83
144static const u8 DME1737_BIT_ALARM_IN[] = {0, 1, 2, 3, 8, 16, 17};
145static const u8 DME1737_BIT_ALARM_TEMP[] = {4, 5, 6};
146static const u8 DME1737_BIT_ALARM_FAN[] = {10, 11, 12, 13, 22, 23};
147
148/* Miscellaneous registers */
Juerg Haefligere95c2372007-10-07 21:27:35 -0700149#define DME1737_REG_DEVICE 0x3d
Juerg Haefliger94319962007-06-09 10:11:16 -0400150#define DME1737_REG_COMPANY 0x3e
151#define DME1737_REG_VERSTEP 0x3f
152#define DME1737_REG_CONFIG 0x40
153#define DME1737_REG_CONFIG2 0x7f
154#define DME1737_REG_VID 0x43
155#define DME1737_REG_TACH_PWM 0x81
156
157/* ---------------------------------------------------------------------
158 * Misc defines
159 * --------------------------------------------------------------------- */
160
161/* Chip identification */
162#define DME1737_COMPANY_SMSC 0x5c
163#define DME1737_VERSTEP 0x88
164#define DME1737_VERSTEP_MASK 0xf8
Juerg Haefligere95c2372007-10-07 21:27:35 -0700165#define SCH311X_DEVICE 0x8c
166
167/* Length of ISA address segment */
168#define DME1737_EXTENT 2
Juerg Haefliger94319962007-06-09 10:11:16 -0400169
170/* ---------------------------------------------------------------------
171 * Data structures and manipulation thereof
172 * --------------------------------------------------------------------- */
173
Juerg Haefligere95c2372007-10-07 21:27:35 -0700174/* For ISA chips, we abuse the i2c_client addr and name fields. We also use
175 the driver field to differentiate between I2C and ISA chips. */
Juerg Haefliger94319962007-06-09 10:11:16 -0400176struct dme1737_data {
177 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700178 struct device *hwmon_dev;
Juerg Haefliger94319962007-06-09 10:11:16 -0400179
180 struct mutex update_lock;
181 int valid; /* !=0 if following fields are valid */
182 unsigned long last_update; /* in jiffies */
183 unsigned long last_vbat; /* in jiffies */
184
185 u8 vid;
186 u8 pwm_rr_en;
187 u8 has_pwm;
188 u8 has_fan;
189
190 /* Register values */
191 u16 in[7];
192 u8 in_min[7];
193 u8 in_max[7];
194 s16 temp[3];
195 s8 temp_min[3];
196 s8 temp_max[3];
197 s8 temp_offset[3];
198 u8 config;
199 u8 config2;
200 u8 vrm;
201 u16 fan[6];
202 u16 fan_min[6];
203 u8 fan_max[2];
204 u8 fan_opt[6];
205 u8 pwm[6];
206 u8 pwm_min[3];
207 u8 pwm_config[3];
208 u8 pwm_acz[3];
209 u8 pwm_freq[6];
210 u8 pwm_rr[2];
211 u8 zone_low[3];
212 u8 zone_abs[3];
213 u8 zone_hyst[2];
214 u32 alarms;
215};
216
217/* Nominal voltage values */
218static const int IN_NOMINAL[] = {5000, 2250, 3300, 5000, 12000, 3300, 3300};
219
220/* Voltage input
221 * Voltage inputs have 16 bits resolution, limit values have 8 bits
222 * resolution. */
223static inline int IN_FROM_REG(int reg, int ix, int res)
224{
225 return (reg * IN_NOMINAL[ix] + (3 << (res - 3))) / (3 << (res - 2));
226}
227
228static inline int IN_TO_REG(int val, int ix)
229{
230 return SENSORS_LIMIT((val * 192 + IN_NOMINAL[ix] / 2) /
231 IN_NOMINAL[ix], 0, 255);
232}
233
234/* Temperature input
235 * The register values represent temperatures in 2's complement notation from
236 * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit
237 * values have 8 bits resolution. */
238static inline int TEMP_FROM_REG(int reg, int res)
239{
240 return (reg * 1000) >> (res - 8);
241}
242
243static inline int TEMP_TO_REG(int val)
244{
245 return SENSORS_LIMIT((val < 0 ? val - 500 : val + 500) / 1000,
246 -128, 127);
247}
248
249/* Temperature range */
250static const int TEMP_RANGE[] = {2000, 2500, 3333, 4000, 5000, 6666, 8000,
251 10000, 13333, 16000, 20000, 26666, 32000,
252 40000, 53333, 80000};
253
254static inline int TEMP_RANGE_FROM_REG(int reg)
255{
256 return TEMP_RANGE[(reg >> 4) & 0x0f];
257}
258
259static int TEMP_RANGE_TO_REG(int val, int reg)
260{
261 int i;
262
263 for (i = 15; i > 0; i--) {
264 if (val > (TEMP_RANGE[i] + TEMP_RANGE[i - 1] + 1) / 2) {
265 break;
266 }
267 }
268
269 return (reg & 0x0f) | (i << 4);
270}
271
272/* Temperature hysteresis
273 * Register layout:
274 * reg[0] = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
275 * reg[1] = [H3-3, H3-2, H3-1, H3-0, xxxx, xxxx, xxxx, xxxx] */
276static inline int TEMP_HYST_FROM_REG(int reg, int ix)
277{
278 return (((ix == 1) ? reg : reg >> 4) & 0x0f) * 1000;
279}
280
281static inline int TEMP_HYST_TO_REG(int val, int ix, int reg)
282{
283 int hyst = SENSORS_LIMIT((val + 500) / 1000, 0, 15);
284
285 return (ix == 1) ? (reg & 0xf0) | hyst : (reg & 0x0f) | (hyst << 4);
286}
287
288/* Fan input RPM */
289static inline int FAN_FROM_REG(int reg, int tpc)
290{
Juerg Haefligerff8421f2008-01-27 16:39:46 -0800291 if (tpc) {
292 return tpc * reg;
293 } else {
294 return (reg == 0 || reg == 0xffff) ? 0 : 90000 * 60 / reg;
295 }
Juerg Haefliger94319962007-06-09 10:11:16 -0400296}
297
298static inline int FAN_TO_REG(int val, int tpc)
299{
Juerg Haefligerff8421f2008-01-27 16:39:46 -0800300 if (tpc) {
301 return SENSORS_LIMIT(val / tpc, 0, 0xffff);
302 } else {
303 return (val <= 0) ? 0xffff :
304 SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe);
305 }
Juerg Haefliger94319962007-06-09 10:11:16 -0400306}
307
308/* Fan TPC (tach pulse count)
309 * Converts a register value to a TPC multiplier or returns 0 if the tachometer
310 * is configured in legacy (non-tpc) mode */
311static inline int FAN_TPC_FROM_REG(int reg)
312{
313 return (reg & 0x20) ? 0 : 60 >> (reg & 0x03);
314}
315
316/* Fan type
317 * The type of a fan is expressed in number of pulses-per-revolution that it
318 * emits */
319static inline int FAN_TYPE_FROM_REG(int reg)
320{
321 int edge = (reg >> 1) & 0x03;
322
323 return (edge > 0) ? 1 << (edge - 1) : 0;
324}
325
326static inline int FAN_TYPE_TO_REG(int val, int reg)
327{
328 int edge = (val == 4) ? 3 : val;
329
330 return (reg & 0xf9) | (edge << 1);
331}
332
333/* Fan max RPM */
334static const int FAN_MAX[] = {0x54, 0x38, 0x2a, 0x21, 0x1c, 0x18, 0x15, 0x12,
335 0x11, 0x0f, 0x0e};
336
337static int FAN_MAX_FROM_REG(int reg)
338{
339 int i;
340
341 for (i = 10; i > 0; i--) {
342 if (reg == FAN_MAX[i]) {
343 break;
344 }
345 }
346
347 return 1000 + i * 500;
348}
349
350static int FAN_MAX_TO_REG(int val)
351{
352 int i;
353
354 for (i = 10; i > 0; i--) {
355 if (val > (1000 + (i - 1) * 500)) {
356 break;
357 }
358 }
359
360 return FAN_MAX[i];
361}
362
363/* PWM enable
364 * Register to enable mapping:
365 * 000: 2 fan on zone 1 auto
366 * 001: 2 fan on zone 2 auto
367 * 010: 2 fan on zone 3 auto
368 * 011: 0 fan full on
369 * 100: -1 fan disabled
370 * 101: 2 fan on hottest of zones 2,3 auto
371 * 110: 2 fan on hottest of zones 1,2,3 auto
372 * 111: 1 fan in manual mode */
373static inline int PWM_EN_FROM_REG(int reg)
374{
375 static const int en[] = {2, 2, 2, 0, -1, 2, 2, 1};
376
377 return en[(reg >> 5) & 0x07];
378}
379
380static inline int PWM_EN_TO_REG(int val, int reg)
381{
382 int en = (val == 1) ? 7 : 3;
383
384 return (reg & 0x1f) | ((en & 0x07) << 5);
385}
386
387/* PWM auto channels zone
388 * Register to auto channels zone mapping (ACZ is a bitfield with bit x
389 * corresponding to zone x+1):
390 * 000: 001 fan on zone 1 auto
391 * 001: 010 fan on zone 2 auto
392 * 010: 100 fan on zone 3 auto
393 * 011: 000 fan full on
394 * 100: 000 fan disabled
395 * 101: 110 fan on hottest of zones 2,3 auto
396 * 110: 111 fan on hottest of zones 1,2,3 auto
397 * 111: 000 fan in manual mode */
398static inline int PWM_ACZ_FROM_REG(int reg)
399{
400 static const int acz[] = {1, 2, 4, 0, 0, 6, 7, 0};
401
402 return acz[(reg >> 5) & 0x07];
403}
404
405static inline int PWM_ACZ_TO_REG(int val, int reg)
406{
407 int acz = (val == 4) ? 2 : val - 1;
408
409 return (reg & 0x1f) | ((acz & 0x07) << 5);
410}
411
412/* PWM frequency */
413static const int PWM_FREQ[] = {11, 15, 22, 29, 35, 44, 59, 88,
414 15000, 20000, 30000, 25000, 0, 0, 0, 0};
415
416static inline int PWM_FREQ_FROM_REG(int reg)
417{
418 return PWM_FREQ[reg & 0x0f];
419}
420
421static int PWM_FREQ_TO_REG(int val, int reg)
422{
423 int i;
424
425 /* the first two cases are special - stupid chip design! */
426 if (val > 27500) {
427 i = 10;
428 } else if (val > 22500) {
429 i = 11;
430 } else {
431 for (i = 9; i > 0; i--) {
432 if (val > (PWM_FREQ[i] + PWM_FREQ[i - 1] + 1) / 2) {
433 break;
434 }
435 }
436 }
437
438 return (reg & 0xf0) | i;
439}
440
441/* PWM ramp rate
442 * Register layout:
443 * reg[0] = [OFF3, OFF2, OFF1, RES, RR1-E, RR1-2, RR1-1, RR1-0]
444 * reg[1] = [RR2-E, RR2-2, RR2-1, RR2-0, RR3-E, RR3-2, RR3-1, RR3-0] */
445static const u8 PWM_RR[] = {206, 104, 69, 41, 26, 18, 10, 5};
446
447static inline int PWM_RR_FROM_REG(int reg, int ix)
448{
449 int rr = (ix == 1) ? reg >> 4 : reg;
450
451 return (rr & 0x08) ? PWM_RR[rr & 0x07] : 0;
452}
453
454static int PWM_RR_TO_REG(int val, int ix, int reg)
455{
456 int i;
457
458 for (i = 0; i < 7; i++) {
459 if (val > (PWM_RR[i] + PWM_RR[i + 1] + 1) / 2) {
460 break;
461 }
462 }
463
464 return (ix == 1) ? (reg & 0x8f) | (i << 4) : (reg & 0xf8) | i;
465}
466
467/* PWM ramp rate enable */
468static inline int PWM_RR_EN_FROM_REG(int reg, int ix)
469{
470 return PWM_RR_FROM_REG(reg, ix) ? 1 : 0;
471}
472
473static inline int PWM_RR_EN_TO_REG(int val, int ix, int reg)
474{
475 int en = (ix == 1) ? 0x80 : 0x08;
476
477 return val ? reg | en : reg & ~en;
478}
479
480/* PWM min/off
481 * The PWM min/off bits are part of the PMW ramp rate register 0 (see above for
482 * the register layout). */
483static inline int PWM_OFF_FROM_REG(int reg, int ix)
484{
485 return (reg >> (ix + 5)) & 0x01;
486}
487
488static inline int PWM_OFF_TO_REG(int val, int ix, int reg)
489{
490 return (reg & ~(1 << (ix + 5))) | ((val & 0x01) << (ix + 5));
491}
492
493/* ---------------------------------------------------------------------
494 * Device I/O access
Juerg Haefligere95c2372007-10-07 21:27:35 -0700495 *
496 * ISA access is performed through an index/data register pair and needs to
497 * be protected by a mutex during runtime (not required for initialization).
498 * We use data->update_lock for this and need to ensure that we acquire it
499 * before calling dme1737_read or dme1737_write.
Juerg Haefliger94319962007-06-09 10:11:16 -0400500 * --------------------------------------------------------------------- */
501
502static u8 dme1737_read(struct i2c_client *client, u8 reg)
503{
Juerg Haefligere95c2372007-10-07 21:27:35 -0700504 s32 val;
Juerg Haefliger94319962007-06-09 10:11:16 -0400505
Juerg Haefligere95c2372007-10-07 21:27:35 -0700506 if (client->driver) { /* I2C device */
507 val = i2c_smbus_read_byte_data(client, reg);
508
509 if (val < 0) {
510 dev_warn(&client->dev, "Read from register "
511 "0x%02x failed! Please report to the driver "
512 "maintainer.\n", reg);
513 }
514 } else { /* ISA device */
515 outb(reg, client->addr);
516 val = inb(client->addr + 1);
Juerg Haefliger94319962007-06-09 10:11:16 -0400517 }
518
519 return val;
520}
521
Juerg Haefligere95c2372007-10-07 21:27:35 -0700522static s32 dme1737_write(struct i2c_client *client, u8 reg, u8 val)
Juerg Haefliger94319962007-06-09 10:11:16 -0400523{
Juerg Haefligere95c2372007-10-07 21:27:35 -0700524 s32 res = 0;
Juerg Haefliger94319962007-06-09 10:11:16 -0400525
Juerg Haefligere95c2372007-10-07 21:27:35 -0700526 if (client->driver) { /* I2C device */
527 res = i2c_smbus_write_byte_data(client, reg, val);
528
529 if (res < 0) {
530 dev_warn(&client->dev, "Write to register "
531 "0x%02x failed! Please report to the driver "
532 "maintainer.\n", reg);
533 }
534 } else { /* ISA device */
535 outb(reg, client->addr);
536 outb(val, client->addr + 1);
Juerg Haefliger94319962007-06-09 10:11:16 -0400537 }
538
539 return res;
540}
541
542static struct dme1737_data *dme1737_update_device(struct device *dev)
543{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700544 struct dme1737_data *data = dev_get_drvdata(dev);
545 struct i2c_client *client = &data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -0400546 int ix;
547 u8 lsb[5];
548
549 mutex_lock(&data->update_lock);
550
551 /* Enable a Vbat monitoring cycle every 10 mins */
552 if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) {
553 dme1737_write(client, DME1737_REG_CONFIG, dme1737_read(client,
554 DME1737_REG_CONFIG) | 0x10);
555 data->last_vbat = jiffies;
556 }
557
558 /* Sample register contents every 1 sec */
559 if (time_after(jiffies, data->last_update + HZ) || !data->valid) {
560 data->vid = dme1737_read(client, DME1737_REG_VID) & 0x3f;
561
562 /* In (voltage) registers */
563 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
564 /* Voltage inputs are stored as 16 bit values even
565 * though they have only 12 bits resolution. This is
566 * to make it consistent with the temp inputs. */
567 data->in[ix] = dme1737_read(client,
568 DME1737_REG_IN(ix)) << 8;
569 data->in_min[ix] = dme1737_read(client,
570 DME1737_REG_IN_MIN(ix));
571 data->in_max[ix] = dme1737_read(client,
572 DME1737_REG_IN_MAX(ix));
573 }
574
575 /* Temp registers */
576 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
577 /* Temp inputs are stored as 16 bit values even
578 * though they have only 12 bits resolution. This is
579 * to take advantage of implicit conversions between
580 * register values (2's complement) and temp values
581 * (signed decimal). */
582 data->temp[ix] = dme1737_read(client,
583 DME1737_REG_TEMP(ix)) << 8;
584 data->temp_min[ix] = dme1737_read(client,
585 DME1737_REG_TEMP_MIN(ix));
586 data->temp_max[ix] = dme1737_read(client,
587 DME1737_REG_TEMP_MAX(ix));
588 data->temp_offset[ix] = dme1737_read(client,
589 DME1737_REG_TEMP_OFFSET(ix));
590 }
591
592 /* In and temp LSB registers
593 * The LSBs are latched when the MSBs are read, so the order in
594 * which the registers are read (MSB first, then LSB) is
595 * important! */
596 for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) {
597 lsb[ix] = dme1737_read(client,
598 DME1737_REG_IN_TEMP_LSB(ix));
599 }
600 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
601 data->in[ix] |= (lsb[DME1737_REG_IN_LSB[ix]] <<
602 DME1737_REG_IN_LSB_SHL[ix]) & 0xf0;
603 }
604 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
605 data->temp[ix] |= (lsb[DME1737_REG_TEMP_LSB[ix]] <<
606 DME1737_REG_TEMP_LSB_SHL[ix]) & 0xf0;
607 }
608
609 /* Fan registers */
610 for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
611 /* Skip reading registers if optional fans are not
612 * present */
613 if (!(data->has_fan & (1 << ix))) {
614 continue;
615 }
616 data->fan[ix] = dme1737_read(client,
617 DME1737_REG_FAN(ix));
618 data->fan[ix] |= dme1737_read(client,
619 DME1737_REG_FAN(ix) + 1) << 8;
620 data->fan_min[ix] = dme1737_read(client,
621 DME1737_REG_FAN_MIN(ix));
622 data->fan_min[ix] |= dme1737_read(client,
623 DME1737_REG_FAN_MIN(ix) + 1) << 8;
624 data->fan_opt[ix] = dme1737_read(client,
625 DME1737_REG_FAN_OPT(ix));
626 /* fan_max exists only for fan[5-6] */
627 if (ix > 3) {
628 data->fan_max[ix - 4] = dme1737_read(client,
629 DME1737_REG_FAN_MAX(ix));
630 }
631 }
632
633 /* PWM registers */
634 for (ix = 0; ix < ARRAY_SIZE(data->pwm); ix++) {
635 /* Skip reading registers if optional PWMs are not
636 * present */
637 if (!(data->has_pwm & (1 << ix))) {
638 continue;
639 }
640 data->pwm[ix] = dme1737_read(client,
641 DME1737_REG_PWM(ix));
642 data->pwm_freq[ix] = dme1737_read(client,
643 DME1737_REG_PWM_FREQ(ix));
644 /* pwm_config and pwm_min exist only for pwm[1-3] */
645 if (ix < 3) {
646 data->pwm_config[ix] = dme1737_read(client,
647 DME1737_REG_PWM_CONFIG(ix));
648 data->pwm_min[ix] = dme1737_read(client,
649 DME1737_REG_PWM_MIN(ix));
650 }
651 }
652 for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) {
653 data->pwm_rr[ix] = dme1737_read(client,
654 DME1737_REG_PWM_RR(ix));
655 }
656
657 /* Thermal zone registers */
658 for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) {
659 data->zone_low[ix] = dme1737_read(client,
660 DME1737_REG_ZONE_LOW(ix));
661 data->zone_abs[ix] = dme1737_read(client,
662 DME1737_REG_ZONE_ABS(ix));
663 }
664 for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) {
665 data->zone_hyst[ix] = dme1737_read(client,
666 DME1737_REG_ZONE_HYST(ix));
667 }
668
669 /* Alarm registers */
670 data->alarms = dme1737_read(client,
671 DME1737_REG_ALARM1);
672 /* Bit 7 tells us if the other alarm registers are non-zero and
673 * therefore also need to be read */
674 if (data->alarms & 0x80) {
675 data->alarms |= dme1737_read(client,
676 DME1737_REG_ALARM2) << 8;
677 data->alarms |= dme1737_read(client,
678 DME1737_REG_ALARM3) << 16;
679 }
680
Juerg Haefligere95c2372007-10-07 21:27:35 -0700681 /* The ISA chips require explicit clearing of alarm bits.
682 * Don't worry, an alarm will come back if the condition
683 * that causes it still exists */
684 if (!client->driver) {
685 if (data->alarms & 0xff0000) {
686 dme1737_write(client, DME1737_REG_ALARM3,
687 0xff);
688 }
689 if (data->alarms & 0xff00) {
690 dme1737_write(client, DME1737_REG_ALARM2,
691 0xff);
692 }
693 if (data->alarms & 0xff) {
694 dme1737_write(client, DME1737_REG_ALARM1,
695 0xff);
696 }
697 }
698
Juerg Haefliger94319962007-06-09 10:11:16 -0400699 data->last_update = jiffies;
700 data->valid = 1;
701 }
702
703 mutex_unlock(&data->update_lock);
704
705 return data;
706}
707
708/* ---------------------------------------------------------------------
709 * Voltage sysfs attributes
710 * ix = [0-5]
711 * --------------------------------------------------------------------- */
712
713#define SYS_IN_INPUT 0
714#define SYS_IN_MIN 1
715#define SYS_IN_MAX 2
716#define SYS_IN_ALARM 3
717
718static ssize_t show_in(struct device *dev, struct device_attribute *attr,
719 char *buf)
720{
721 struct dme1737_data *data = dme1737_update_device(dev);
722 struct sensor_device_attribute_2
723 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
724 int ix = sensor_attr_2->index;
725 int fn = sensor_attr_2->nr;
726 int res;
727
728 switch (fn) {
729 case SYS_IN_INPUT:
730 res = IN_FROM_REG(data->in[ix], ix, 16);
731 break;
732 case SYS_IN_MIN:
733 res = IN_FROM_REG(data->in_min[ix], ix, 8);
734 break;
735 case SYS_IN_MAX:
736 res = IN_FROM_REG(data->in_max[ix], ix, 8);
737 break;
738 case SYS_IN_ALARM:
739 res = (data->alarms >> DME1737_BIT_ALARM_IN[ix]) & 0x01;
740 break;
741 default:
742 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700743 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400744 }
745
746 return sprintf(buf, "%d\n", res);
747}
748
749static ssize_t set_in(struct device *dev, struct device_attribute *attr,
750 const char *buf, size_t count)
751{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700752 struct dme1737_data *data = dev_get_drvdata(dev);
753 struct i2c_client *client = &data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -0400754 struct sensor_device_attribute_2
755 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
756 int ix = sensor_attr_2->index;
757 int fn = sensor_attr_2->nr;
758 long val = simple_strtol(buf, NULL, 10);
759
760 mutex_lock(&data->update_lock);
761 switch (fn) {
762 case SYS_IN_MIN:
763 data->in_min[ix] = IN_TO_REG(val, ix);
764 dme1737_write(client, DME1737_REG_IN_MIN(ix),
765 data->in_min[ix]);
766 break;
767 case SYS_IN_MAX:
768 data->in_max[ix] = IN_TO_REG(val, ix);
769 dme1737_write(client, DME1737_REG_IN_MAX(ix),
770 data->in_max[ix]);
771 break;
772 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700773 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400774 }
775 mutex_unlock(&data->update_lock);
776
777 return count;
778}
779
780/* ---------------------------------------------------------------------
781 * Temperature sysfs attributes
782 * ix = [0-2]
783 * --------------------------------------------------------------------- */
784
785#define SYS_TEMP_INPUT 0
786#define SYS_TEMP_MIN 1
787#define SYS_TEMP_MAX 2
788#define SYS_TEMP_OFFSET 3
789#define SYS_TEMP_ALARM 4
790#define SYS_TEMP_FAULT 5
791
792static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
793 char *buf)
794{
795 struct dme1737_data *data = dme1737_update_device(dev);
796 struct sensor_device_attribute_2
797 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
798 int ix = sensor_attr_2->index;
799 int fn = sensor_attr_2->nr;
800 int res;
801
802 switch (fn) {
803 case SYS_TEMP_INPUT:
804 res = TEMP_FROM_REG(data->temp[ix], 16);
805 break;
806 case SYS_TEMP_MIN:
807 res = TEMP_FROM_REG(data->temp_min[ix], 8);
808 break;
809 case SYS_TEMP_MAX:
810 res = TEMP_FROM_REG(data->temp_max[ix], 8);
811 break;
812 case SYS_TEMP_OFFSET:
813 res = TEMP_FROM_REG(data->temp_offset[ix], 8);
814 break;
815 case SYS_TEMP_ALARM:
816 res = (data->alarms >> DME1737_BIT_ALARM_TEMP[ix]) & 0x01;
817 break;
818 case SYS_TEMP_FAULT:
Juerg Haefligerc0f31402007-07-20 14:16:47 -0700819 res = (((u16)data->temp[ix] & 0xff00) == 0x8000);
Juerg Haefliger94319962007-06-09 10:11:16 -0400820 break;
821 default:
822 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700823 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400824 }
825
826 return sprintf(buf, "%d\n", res);
827}
828
829static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
830 const char *buf, size_t count)
831{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700832 struct dme1737_data *data = dev_get_drvdata(dev);
833 struct i2c_client *client = &data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -0400834 struct sensor_device_attribute_2
835 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
836 int ix = sensor_attr_2->index;
837 int fn = sensor_attr_2->nr;
838 long val = simple_strtol(buf, NULL, 10);
839
840 mutex_lock(&data->update_lock);
841 switch (fn) {
842 case SYS_TEMP_MIN:
843 data->temp_min[ix] = TEMP_TO_REG(val);
844 dme1737_write(client, DME1737_REG_TEMP_MIN(ix),
845 data->temp_min[ix]);
846 break;
847 case SYS_TEMP_MAX:
848 data->temp_max[ix] = TEMP_TO_REG(val);
849 dme1737_write(client, DME1737_REG_TEMP_MAX(ix),
850 data->temp_max[ix]);
851 break;
852 case SYS_TEMP_OFFSET:
853 data->temp_offset[ix] = TEMP_TO_REG(val);
854 dme1737_write(client, DME1737_REG_TEMP_OFFSET(ix),
855 data->temp_offset[ix]);
856 break;
857 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700858 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400859 }
860 mutex_unlock(&data->update_lock);
861
862 return count;
863}
864
865/* ---------------------------------------------------------------------
866 * Zone sysfs attributes
867 * ix = [0-2]
868 * --------------------------------------------------------------------- */
869
870#define SYS_ZONE_AUTO_CHANNELS_TEMP 0
871#define SYS_ZONE_AUTO_POINT1_TEMP_HYST 1
872#define SYS_ZONE_AUTO_POINT1_TEMP 2
873#define SYS_ZONE_AUTO_POINT2_TEMP 3
874#define SYS_ZONE_AUTO_POINT3_TEMP 4
875
876static ssize_t show_zone(struct device *dev, struct device_attribute *attr,
877 char *buf)
878{
879 struct dme1737_data *data = dme1737_update_device(dev);
880 struct sensor_device_attribute_2
881 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
882 int ix = sensor_attr_2->index;
883 int fn = sensor_attr_2->nr;
884 int res;
885
886 switch (fn) {
887 case SYS_ZONE_AUTO_CHANNELS_TEMP:
888 /* check config2 for non-standard temp-to-zone mapping */
889 if ((ix == 1) && (data->config2 & 0x02)) {
890 res = 4;
891 } else {
892 res = 1 << ix;
893 }
894 break;
895 case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
896 res = TEMP_FROM_REG(data->zone_low[ix], 8) -
897 TEMP_HYST_FROM_REG(data->zone_hyst[ix == 2], ix);
898 break;
899 case SYS_ZONE_AUTO_POINT1_TEMP:
900 res = TEMP_FROM_REG(data->zone_low[ix], 8);
901 break;
902 case SYS_ZONE_AUTO_POINT2_TEMP:
903 /* pwm_freq holds the temp range bits in the upper nibble */
904 res = TEMP_FROM_REG(data->zone_low[ix], 8) +
905 TEMP_RANGE_FROM_REG(data->pwm_freq[ix]);
906 break;
907 case SYS_ZONE_AUTO_POINT3_TEMP:
908 res = TEMP_FROM_REG(data->zone_abs[ix], 8);
909 break;
910 default:
911 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700912 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400913 }
914
915 return sprintf(buf, "%d\n", res);
916}
917
918static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
919 const char *buf, size_t count)
920{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700921 struct dme1737_data *data = dev_get_drvdata(dev);
922 struct i2c_client *client = &data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -0400923 struct sensor_device_attribute_2
924 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
925 int ix = sensor_attr_2->index;
926 int fn = sensor_attr_2->nr;
927 long val = simple_strtol(buf, NULL, 10);
928
929 mutex_lock(&data->update_lock);
930 switch (fn) {
931 case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
932 /* Refresh the cache */
933 data->zone_low[ix] = dme1737_read(client,
934 DME1737_REG_ZONE_LOW(ix));
935 /* Modify the temp hyst value */
936 data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG(
937 TEMP_FROM_REG(data->zone_low[ix], 8) -
938 val, ix, dme1737_read(client,
939 DME1737_REG_ZONE_HYST(ix == 2)));
940 dme1737_write(client, DME1737_REG_ZONE_HYST(ix == 2),
941 data->zone_hyst[ix == 2]);
942 break;
943 case SYS_ZONE_AUTO_POINT1_TEMP:
944 data->zone_low[ix] = TEMP_TO_REG(val);
945 dme1737_write(client, DME1737_REG_ZONE_LOW(ix),
946 data->zone_low[ix]);
947 break;
948 case SYS_ZONE_AUTO_POINT2_TEMP:
949 /* Refresh the cache */
950 data->zone_low[ix] = dme1737_read(client,
951 DME1737_REG_ZONE_LOW(ix));
952 /* Modify the temp range value (which is stored in the upper
953 * nibble of the pwm_freq register) */
954 data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val -
955 TEMP_FROM_REG(data->zone_low[ix], 8),
956 dme1737_read(client,
957 DME1737_REG_PWM_FREQ(ix)));
958 dme1737_write(client, DME1737_REG_PWM_FREQ(ix),
959 data->pwm_freq[ix]);
960 break;
961 case SYS_ZONE_AUTO_POINT3_TEMP:
962 data->zone_abs[ix] = TEMP_TO_REG(val);
963 dme1737_write(client, DME1737_REG_ZONE_ABS(ix),
964 data->zone_abs[ix]);
965 break;
966 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700967 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400968 }
969 mutex_unlock(&data->update_lock);
970
971 return count;
972}
973
974/* ---------------------------------------------------------------------
975 * Fan sysfs attributes
976 * ix = [0-5]
977 * --------------------------------------------------------------------- */
978
979#define SYS_FAN_INPUT 0
980#define SYS_FAN_MIN 1
981#define SYS_FAN_MAX 2
982#define SYS_FAN_ALARM 3
983#define SYS_FAN_TYPE 4
984
985static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
986 char *buf)
987{
988 struct dme1737_data *data = dme1737_update_device(dev);
989 struct sensor_device_attribute_2
990 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
991 int ix = sensor_attr_2->index;
992 int fn = sensor_attr_2->nr;
993 int res;
994
995 switch (fn) {
996 case SYS_FAN_INPUT:
997 res = FAN_FROM_REG(data->fan[ix],
998 ix < 4 ? 0 :
999 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1000 break;
1001 case SYS_FAN_MIN:
1002 res = FAN_FROM_REG(data->fan_min[ix],
1003 ix < 4 ? 0 :
1004 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1005 break;
1006 case SYS_FAN_MAX:
1007 /* only valid for fan[5-6] */
1008 res = FAN_MAX_FROM_REG(data->fan_max[ix - 4]);
1009 break;
1010 case SYS_FAN_ALARM:
1011 res = (data->alarms >> DME1737_BIT_ALARM_FAN[ix]) & 0x01;
1012 break;
1013 case SYS_FAN_TYPE:
1014 /* only valid for fan[1-4] */
1015 res = FAN_TYPE_FROM_REG(data->fan_opt[ix]);
1016 break;
1017 default:
1018 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001019 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001020 }
1021
1022 return sprintf(buf, "%d\n", res);
1023}
1024
1025static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1026 const char *buf, size_t count)
1027{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001028 struct dme1737_data *data = dev_get_drvdata(dev);
1029 struct i2c_client *client = &data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -04001030 struct sensor_device_attribute_2
1031 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1032 int ix = sensor_attr_2->index;
1033 int fn = sensor_attr_2->nr;
1034 long val = simple_strtol(buf, NULL, 10);
1035
1036 mutex_lock(&data->update_lock);
1037 switch (fn) {
1038 case SYS_FAN_MIN:
1039 if (ix < 4) {
1040 data->fan_min[ix] = FAN_TO_REG(val, 0);
1041 } else {
1042 /* Refresh the cache */
1043 data->fan_opt[ix] = dme1737_read(client,
1044 DME1737_REG_FAN_OPT(ix));
1045 /* Modify the fan min value */
1046 data->fan_min[ix] = FAN_TO_REG(val,
1047 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1048 }
1049 dme1737_write(client, DME1737_REG_FAN_MIN(ix),
1050 data->fan_min[ix] & 0xff);
1051 dme1737_write(client, DME1737_REG_FAN_MIN(ix) + 1,
1052 data->fan_min[ix] >> 8);
1053 break;
1054 case SYS_FAN_MAX:
1055 /* Only valid for fan[5-6] */
1056 data->fan_max[ix - 4] = FAN_MAX_TO_REG(val);
1057 dme1737_write(client, DME1737_REG_FAN_MAX(ix),
1058 data->fan_max[ix - 4]);
1059 break;
1060 case SYS_FAN_TYPE:
1061 /* Only valid for fan[1-4] */
1062 if (!(val == 1 || val == 2 || val == 4)) {
1063 count = -EINVAL;
Juerg Haefligere95c2372007-10-07 21:27:35 -07001064 dev_warn(dev, "Fan type value %ld not "
Juerg Haefliger94319962007-06-09 10:11:16 -04001065 "supported. Choose one of 1, 2, or 4.\n",
1066 val);
1067 goto exit;
1068 }
1069 data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(client,
1070 DME1737_REG_FAN_OPT(ix)));
1071 dme1737_write(client, DME1737_REG_FAN_OPT(ix),
1072 data->fan_opt[ix]);
1073 break;
1074 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001075 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001076 }
1077exit:
1078 mutex_unlock(&data->update_lock);
1079
1080 return count;
1081}
1082
1083/* ---------------------------------------------------------------------
1084 * PWM sysfs attributes
1085 * ix = [0-4]
1086 * --------------------------------------------------------------------- */
1087
1088#define SYS_PWM 0
1089#define SYS_PWM_FREQ 1
1090#define SYS_PWM_ENABLE 2
1091#define SYS_PWM_RAMP_RATE 3
1092#define SYS_PWM_AUTO_CHANNELS_ZONE 4
1093#define SYS_PWM_AUTO_PWM_MIN 5
1094#define SYS_PWM_AUTO_POINT1_PWM 6
1095#define SYS_PWM_AUTO_POINT2_PWM 7
1096
1097static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1098 char *buf)
1099{
1100 struct dme1737_data *data = dme1737_update_device(dev);
1101 struct sensor_device_attribute_2
1102 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1103 int ix = sensor_attr_2->index;
1104 int fn = sensor_attr_2->nr;
1105 int res;
1106
1107 switch (fn) {
1108 case SYS_PWM:
1109 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 0) {
1110 res = 255;
1111 } else {
1112 res = data->pwm[ix];
1113 }
1114 break;
1115 case SYS_PWM_FREQ:
1116 res = PWM_FREQ_FROM_REG(data->pwm_freq[ix]);
1117 break;
1118 case SYS_PWM_ENABLE:
1119 if (ix > 3) {
1120 res = 1; /* pwm[5-6] hard-wired to manual mode */
1121 } else {
1122 res = PWM_EN_FROM_REG(data->pwm_config[ix]);
1123 }
1124 break;
1125 case SYS_PWM_RAMP_RATE:
1126 /* Only valid for pwm[1-3] */
1127 res = PWM_RR_FROM_REG(data->pwm_rr[ix > 0], ix);
1128 break;
1129 case SYS_PWM_AUTO_CHANNELS_ZONE:
1130 /* Only valid for pwm[1-3] */
1131 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1132 res = PWM_ACZ_FROM_REG(data->pwm_config[ix]);
1133 } else {
1134 res = data->pwm_acz[ix];
1135 }
1136 break;
1137 case SYS_PWM_AUTO_PWM_MIN:
1138 /* Only valid for pwm[1-3] */
1139 if (PWM_OFF_FROM_REG(data->pwm_rr[0], ix)) {
1140 res = data->pwm_min[ix];
1141 } else {
1142 res = 0;
1143 }
1144 break;
1145 case SYS_PWM_AUTO_POINT1_PWM:
1146 /* Only valid for pwm[1-3] */
1147 res = data->pwm_min[ix];
1148 break;
1149 case SYS_PWM_AUTO_POINT2_PWM:
1150 /* Only valid for pwm[1-3] */
1151 res = 255; /* hard-wired */
1152 break;
1153 default:
1154 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001155 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001156 }
1157
1158 return sprintf(buf, "%d\n", res);
1159}
1160
1161static struct attribute *dme1737_attr_pwm[];
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001162static void dme1737_chmod_file(struct device*, struct attribute*, mode_t);
Juerg Haefliger94319962007-06-09 10:11:16 -04001163
1164static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1165 const char *buf, size_t count)
1166{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001167 struct dme1737_data *data = dev_get_drvdata(dev);
1168 struct i2c_client *client = &data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -04001169 struct sensor_device_attribute_2
1170 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1171 int ix = sensor_attr_2->index;
1172 int fn = sensor_attr_2->nr;
1173 long val = simple_strtol(buf, NULL, 10);
1174
1175 mutex_lock(&data->update_lock);
1176 switch (fn) {
1177 case SYS_PWM:
1178 data->pwm[ix] = SENSORS_LIMIT(val, 0, 255);
1179 dme1737_write(client, DME1737_REG_PWM(ix), data->pwm[ix]);
1180 break;
1181 case SYS_PWM_FREQ:
1182 data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(client,
1183 DME1737_REG_PWM_FREQ(ix)));
1184 dme1737_write(client, DME1737_REG_PWM_FREQ(ix),
1185 data->pwm_freq[ix]);
1186 break;
1187 case SYS_PWM_ENABLE:
1188 /* Only valid for pwm[1-3] */
1189 if (val < 0 || val > 2) {
1190 count = -EINVAL;
Juerg Haefligere95c2372007-10-07 21:27:35 -07001191 dev_warn(dev, "PWM enable %ld not "
Juerg Haefliger94319962007-06-09 10:11:16 -04001192 "supported. Choose one of 0, 1, or 2.\n",
1193 val);
1194 goto exit;
1195 }
1196 /* Refresh the cache */
1197 data->pwm_config[ix] = dme1737_read(client,
1198 DME1737_REG_PWM_CONFIG(ix));
1199 if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) {
1200 /* Bail out if no change */
1201 goto exit;
1202 }
1203 /* Do some housekeeping if we are currently in auto mode */
1204 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1205 /* Save the current zone channel assignment */
1206 data->pwm_acz[ix] = PWM_ACZ_FROM_REG(
1207 data->pwm_config[ix]);
1208 /* Save the current ramp rate state and disable it */
1209 data->pwm_rr[ix > 0] = dme1737_read(client,
1210 DME1737_REG_PWM_RR(ix > 0));
1211 data->pwm_rr_en &= ~(1 << ix);
1212 if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) {
1213 data->pwm_rr_en |= (1 << ix);
1214 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix,
1215 data->pwm_rr[ix > 0]);
1216 dme1737_write(client,
1217 DME1737_REG_PWM_RR(ix > 0),
1218 data->pwm_rr[ix > 0]);
1219 }
1220 }
1221 /* Set the new PWM mode */
1222 switch (val) {
1223 case 0:
1224 /* Change permissions of pwm[ix] to read-only */
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001225 dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
Juerg Haefliger94319962007-06-09 10:11:16 -04001226 S_IRUGO);
1227 /* Turn fan fully on */
1228 data->pwm_config[ix] = PWM_EN_TO_REG(0,
1229 data->pwm_config[ix]);
1230 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1231 data->pwm_config[ix]);
1232 break;
1233 case 1:
1234 /* Turn on manual mode */
1235 data->pwm_config[ix] = PWM_EN_TO_REG(1,
1236 data->pwm_config[ix]);
1237 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1238 data->pwm_config[ix]);
1239 /* Change permissions of pwm[ix] to read-writeable */
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001240 dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
Juerg Haefliger94319962007-06-09 10:11:16 -04001241 S_IRUGO | S_IWUSR);
1242 break;
1243 case 2:
1244 /* Change permissions of pwm[ix] to read-only */
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001245 dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
Juerg Haefliger94319962007-06-09 10:11:16 -04001246 S_IRUGO);
1247 /* Turn on auto mode using the saved zone channel
1248 * assignment */
1249 data->pwm_config[ix] = PWM_ACZ_TO_REG(
1250 data->pwm_acz[ix],
1251 data->pwm_config[ix]);
1252 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1253 data->pwm_config[ix]);
1254 /* Enable PWM ramp rate if previously enabled */
1255 if (data->pwm_rr_en & (1 << ix)) {
1256 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix,
1257 dme1737_read(client,
1258 DME1737_REG_PWM_RR(ix > 0)));
1259 dme1737_write(client,
1260 DME1737_REG_PWM_RR(ix > 0),
1261 data->pwm_rr[ix > 0]);
1262 }
1263 break;
1264 }
1265 break;
1266 case SYS_PWM_RAMP_RATE:
1267 /* Only valid for pwm[1-3] */
1268 /* Refresh the cache */
1269 data->pwm_config[ix] = dme1737_read(client,
1270 DME1737_REG_PWM_CONFIG(ix));
1271 data->pwm_rr[ix > 0] = dme1737_read(client,
1272 DME1737_REG_PWM_RR(ix > 0));
1273 /* Set the ramp rate value */
1274 if (val > 0) {
1275 data->pwm_rr[ix > 0] = PWM_RR_TO_REG(val, ix,
1276 data->pwm_rr[ix > 0]);
1277 }
1278 /* Enable/disable the feature only if the associated PWM
1279 * output is in automatic mode. */
1280 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1281 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix,
1282 data->pwm_rr[ix > 0]);
1283 }
1284 dme1737_write(client, DME1737_REG_PWM_RR(ix > 0),
1285 data->pwm_rr[ix > 0]);
1286 break;
1287 case SYS_PWM_AUTO_CHANNELS_ZONE:
1288 /* Only valid for pwm[1-3] */
1289 if (!(val == 1 || val == 2 || val == 4 ||
1290 val == 6 || val == 7)) {
1291 count = -EINVAL;
Juerg Haefligere95c2372007-10-07 21:27:35 -07001292 dev_warn(dev, "PWM auto channels zone %ld "
Juerg Haefliger94319962007-06-09 10:11:16 -04001293 "not supported. Choose one of 1, 2, 4, 6, "
1294 "or 7.\n", val);
1295 goto exit;
1296 }
1297 /* Refresh the cache */
1298 data->pwm_config[ix] = dme1737_read(client,
1299 DME1737_REG_PWM_CONFIG(ix));
1300 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1301 /* PWM is already in auto mode so update the temp
1302 * channel assignment */
1303 data->pwm_config[ix] = PWM_ACZ_TO_REG(val,
1304 data->pwm_config[ix]);
1305 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1306 data->pwm_config[ix]);
1307 } else {
1308 /* PWM is not in auto mode so we save the temp
1309 * channel assignment for later use */
1310 data->pwm_acz[ix] = val;
1311 }
1312 break;
1313 case SYS_PWM_AUTO_PWM_MIN:
1314 /* Only valid for pwm[1-3] */
1315 /* Refresh the cache */
1316 data->pwm_min[ix] = dme1737_read(client,
1317 DME1737_REG_PWM_MIN(ix));
1318 /* There are only 2 values supported for the auto_pwm_min
1319 * value: 0 or auto_point1_pwm. So if the temperature drops
1320 * below the auto_point1_temp_hyst value, the fan either turns
1321 * off or runs at auto_point1_pwm duty-cycle. */
1322 if (val > ((data->pwm_min[ix] + 1) / 2)) {
1323 data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix,
1324 dme1737_read(client,
1325 DME1737_REG_PWM_RR(0)));
Juerg Haefliger94319962007-06-09 10:11:16 -04001326 } else {
1327 data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix,
1328 dme1737_read(client,
1329 DME1737_REG_PWM_RR(0)));
Juerg Haefliger94319962007-06-09 10:11:16 -04001330 }
1331 dme1737_write(client, DME1737_REG_PWM_RR(0),
1332 data->pwm_rr[0]);
1333 break;
1334 case SYS_PWM_AUTO_POINT1_PWM:
1335 /* Only valid for pwm[1-3] */
1336 data->pwm_min[ix] = SENSORS_LIMIT(val, 0, 255);
1337 dme1737_write(client, DME1737_REG_PWM_MIN(ix),
1338 data->pwm_min[ix]);
1339 break;
1340 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001341 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001342 }
1343exit:
1344 mutex_unlock(&data->update_lock);
1345
1346 return count;
1347}
1348
1349/* ---------------------------------------------------------------------
1350 * Miscellaneous sysfs attributes
1351 * --------------------------------------------------------------------- */
1352
1353static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
1354 char *buf)
1355{
1356 struct i2c_client *client = to_i2c_client(dev);
1357 struct dme1737_data *data = i2c_get_clientdata(client);
1358
1359 return sprintf(buf, "%d\n", data->vrm);
1360}
1361
1362static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
1363 const char *buf, size_t count)
1364{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001365 struct dme1737_data *data = dev_get_drvdata(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04001366 long val = simple_strtol(buf, NULL, 10);
1367
1368 data->vrm = val;
1369 return count;
1370}
1371
1372static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
1373 char *buf)
1374{
1375 struct dme1737_data *data = dme1737_update_device(dev);
1376
1377 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1378}
1379
Juerg Haefligere95c2372007-10-07 21:27:35 -07001380static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1381 char *buf)
1382{
1383 struct dme1737_data *data = dev_get_drvdata(dev);
1384
1385 return sprintf(buf, "%s\n", data->client.name);
1386}
1387
Juerg Haefliger94319962007-06-09 10:11:16 -04001388/* ---------------------------------------------------------------------
1389 * Sysfs device attribute defines and structs
1390 * --------------------------------------------------------------------- */
1391
1392/* Voltages 0-6 */
1393
1394#define SENSOR_DEVICE_ATTR_IN(ix) \
1395static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001396 show_in, NULL, SYS_IN_INPUT, ix); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001397static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001398 show_in, set_in, SYS_IN_MIN, ix); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001399static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001400 show_in, set_in, SYS_IN_MAX, ix); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001401static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001402 show_in, NULL, SYS_IN_ALARM, ix)
Juerg Haefliger94319962007-06-09 10:11:16 -04001403
1404SENSOR_DEVICE_ATTR_IN(0);
1405SENSOR_DEVICE_ATTR_IN(1);
1406SENSOR_DEVICE_ATTR_IN(2);
1407SENSOR_DEVICE_ATTR_IN(3);
1408SENSOR_DEVICE_ATTR_IN(4);
1409SENSOR_DEVICE_ATTR_IN(5);
1410SENSOR_DEVICE_ATTR_IN(6);
1411
1412/* Temperatures 1-3 */
1413
1414#define SENSOR_DEVICE_ATTR_TEMP(ix) \
1415static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001416 show_temp, NULL, SYS_TEMP_INPUT, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001417static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001418 show_temp, set_temp, SYS_TEMP_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001419static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001420 show_temp, set_temp, SYS_TEMP_MAX, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001421static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001422 show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001423static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001424 show_temp, NULL, SYS_TEMP_ALARM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001425static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001426 show_temp, NULL, SYS_TEMP_FAULT, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001427
1428SENSOR_DEVICE_ATTR_TEMP(1);
1429SENSOR_DEVICE_ATTR_TEMP(2);
1430SENSOR_DEVICE_ATTR_TEMP(3);
1431
1432/* Zones 1-3 */
1433
1434#define SENSOR_DEVICE_ATTR_ZONE(ix) \
1435static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001436 show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001437static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001438 show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001439static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001440 show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001441static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001442 show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001443static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001444 show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001445
1446SENSOR_DEVICE_ATTR_ZONE(1);
1447SENSOR_DEVICE_ATTR_ZONE(2);
1448SENSOR_DEVICE_ATTR_ZONE(3);
1449
1450/* Fans 1-4 */
1451
1452#define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \
1453static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001454 show_fan, NULL, SYS_FAN_INPUT, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001455static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001456 show_fan, set_fan, SYS_FAN_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001457static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001458 show_fan, NULL, SYS_FAN_ALARM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001459static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001460 show_fan, set_fan, SYS_FAN_TYPE, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001461
1462SENSOR_DEVICE_ATTR_FAN_1TO4(1);
1463SENSOR_DEVICE_ATTR_FAN_1TO4(2);
1464SENSOR_DEVICE_ATTR_FAN_1TO4(3);
1465SENSOR_DEVICE_ATTR_FAN_1TO4(4);
1466
1467/* Fans 5-6 */
1468
1469#define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \
1470static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001471 show_fan, NULL, SYS_FAN_INPUT, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001472static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001473 show_fan, set_fan, SYS_FAN_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001474static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001475 show_fan, NULL, SYS_FAN_ALARM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001476static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001477 show_fan, set_fan, SYS_FAN_MAX, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001478
1479SENSOR_DEVICE_ATTR_FAN_5TO6(5);
1480SENSOR_DEVICE_ATTR_FAN_5TO6(6);
1481
1482/* PWMs 1-3 */
1483
1484#define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \
1485static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001486 show_pwm, set_pwm, SYS_PWM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001487static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001488 show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001489static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001490 show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001491static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001492 show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001493static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001494 show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001495static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001496 show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001497static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001498 show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001499static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001500 show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001501
1502SENSOR_DEVICE_ATTR_PWM_1TO3(1);
1503SENSOR_DEVICE_ATTR_PWM_1TO3(2);
1504SENSOR_DEVICE_ATTR_PWM_1TO3(3);
1505
1506/* PWMs 5-6 */
1507
1508#define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \
Juerg Haefliger9b257712008-03-25 21:49:02 -07001509static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001510 show_pwm, set_pwm, SYS_PWM, ix-1); \
Juerg Haefliger9b257712008-03-25 21:49:02 -07001511static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001512 show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001513static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001514 show_pwm, NULL, SYS_PWM_ENABLE, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001515
1516SENSOR_DEVICE_ATTR_PWM_5TO6(5);
1517SENSOR_DEVICE_ATTR_PWM_5TO6(6);
1518
1519/* Misc */
1520
1521static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
1522static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
Juerg Haefligere95c2372007-10-07 21:27:35 -07001523static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); /* for ISA devices */
Juerg Haefliger94319962007-06-09 10:11:16 -04001524
Juerg Haefliger94319962007-06-09 10:11:16 -04001525/* This struct holds all the attributes that are always present and need to be
1526 * created unconditionally. The attributes that need modification of their
1527 * permissions are created read-only and write permissions are added or removed
1528 * on the fly when required */
1529static struct attribute *dme1737_attr[] ={
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001530 /* Voltages */
Juerg Haefliger9b257712008-03-25 21:49:02 -07001531 &sensor_dev_attr_in0_input.dev_attr.attr,
1532 &sensor_dev_attr_in0_min.dev_attr.attr,
1533 &sensor_dev_attr_in0_max.dev_attr.attr,
1534 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1535 &sensor_dev_attr_in1_input.dev_attr.attr,
1536 &sensor_dev_attr_in1_min.dev_attr.attr,
1537 &sensor_dev_attr_in1_max.dev_attr.attr,
1538 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1539 &sensor_dev_attr_in2_input.dev_attr.attr,
1540 &sensor_dev_attr_in2_min.dev_attr.attr,
1541 &sensor_dev_attr_in2_max.dev_attr.attr,
1542 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1543 &sensor_dev_attr_in3_input.dev_attr.attr,
1544 &sensor_dev_attr_in3_min.dev_attr.attr,
1545 &sensor_dev_attr_in3_max.dev_attr.attr,
1546 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1547 &sensor_dev_attr_in4_input.dev_attr.attr,
1548 &sensor_dev_attr_in4_min.dev_attr.attr,
1549 &sensor_dev_attr_in4_max.dev_attr.attr,
1550 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1551 &sensor_dev_attr_in5_input.dev_attr.attr,
1552 &sensor_dev_attr_in5_min.dev_attr.attr,
1553 &sensor_dev_attr_in5_max.dev_attr.attr,
1554 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1555 &sensor_dev_attr_in6_input.dev_attr.attr,
1556 &sensor_dev_attr_in6_min.dev_attr.attr,
1557 &sensor_dev_attr_in6_max.dev_attr.attr,
1558 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001559 /* Temperatures */
Juerg Haefliger9b257712008-03-25 21:49:02 -07001560 &sensor_dev_attr_temp1_input.dev_attr.attr,
1561 &sensor_dev_attr_temp1_min.dev_attr.attr,
1562 &sensor_dev_attr_temp1_max.dev_attr.attr,
1563 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1564 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1565 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1566 &sensor_dev_attr_temp2_input.dev_attr.attr,
1567 &sensor_dev_attr_temp2_min.dev_attr.attr,
1568 &sensor_dev_attr_temp2_max.dev_attr.attr,
1569 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1570 &sensor_dev_attr_temp2_fault.dev_attr.attr,
1571 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1572 &sensor_dev_attr_temp3_input.dev_attr.attr,
1573 &sensor_dev_attr_temp3_min.dev_attr.attr,
1574 &sensor_dev_attr_temp3_max.dev_attr.attr,
1575 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1576 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1577 &sensor_dev_attr_temp3_offset.dev_attr.attr,
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001578 /* Zones */
Juerg Haefliger9b257712008-03-25 21:49:02 -07001579 &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
1580 &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
1581 &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
1582 &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
1583 &sensor_dev_attr_zone1_auto_channels_temp.dev_attr.attr,
1584 &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
1585 &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
1586 &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
1587 &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
1588 &sensor_dev_attr_zone2_auto_channels_temp.dev_attr.attr,
1589 &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
1590 &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
1591 &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
1592 &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
1593 &sensor_dev_attr_zone3_auto_channels_temp.dev_attr.attr,
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001594 /* Misc */
1595 &dev_attr_vrm.attr,
1596 &dev_attr_cpu0_vid.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001597 NULL
1598};
1599
1600static const struct attribute_group dme1737_group = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001601 .attrs = dme1737_attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001602};
1603
1604/* The following structs hold the PWM attributes, some of which are optional.
1605 * Their creation depends on the chip configuration which is determined during
1606 * module load. */
1607static struct attribute *dme1737_attr_pwm1[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001608 &sensor_dev_attr_pwm1.dev_attr.attr,
1609 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1610 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1611 &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
1612 &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
1613 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1614 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1615 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001616 NULL
1617};
1618static struct attribute *dme1737_attr_pwm2[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001619 &sensor_dev_attr_pwm2.dev_attr.attr,
1620 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1621 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1622 &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
1623 &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
1624 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1625 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1626 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001627 NULL
1628};
1629static struct attribute *dme1737_attr_pwm3[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001630 &sensor_dev_attr_pwm3.dev_attr.attr,
1631 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1632 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1633 &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
1634 &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
1635 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1636 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1637 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001638 NULL
1639};
1640static struct attribute *dme1737_attr_pwm5[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001641 &sensor_dev_attr_pwm5.dev_attr.attr,
1642 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
1643 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001644 NULL
1645};
1646static struct attribute *dme1737_attr_pwm6[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001647 &sensor_dev_attr_pwm6.dev_attr.attr,
1648 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
1649 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001650 NULL
1651};
1652
1653static const struct attribute_group dme1737_pwm_group[] = {
1654 { .attrs = dme1737_attr_pwm1 },
1655 { .attrs = dme1737_attr_pwm2 },
1656 { .attrs = dme1737_attr_pwm3 },
1657 { .attrs = NULL },
1658 { .attrs = dme1737_attr_pwm5 },
1659 { .attrs = dme1737_attr_pwm6 },
1660};
1661
1662/* The following structs hold the fan attributes, some of which are optional.
1663 * Their creation depends on the chip configuration which is determined during
1664 * module load. */
1665static struct attribute *dme1737_attr_fan1[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001666 &sensor_dev_attr_fan1_input.dev_attr.attr,
1667 &sensor_dev_attr_fan1_min.dev_attr.attr,
1668 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1669 &sensor_dev_attr_fan1_type.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001670 NULL
1671};
1672static struct attribute *dme1737_attr_fan2[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001673 &sensor_dev_attr_fan2_input.dev_attr.attr,
1674 &sensor_dev_attr_fan2_min.dev_attr.attr,
1675 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1676 &sensor_dev_attr_fan2_type.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001677 NULL
1678};
1679static struct attribute *dme1737_attr_fan3[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001680 &sensor_dev_attr_fan3_input.dev_attr.attr,
1681 &sensor_dev_attr_fan3_min.dev_attr.attr,
1682 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1683 &sensor_dev_attr_fan3_type.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001684 NULL
1685};
1686static struct attribute *dme1737_attr_fan4[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001687 &sensor_dev_attr_fan4_input.dev_attr.attr,
1688 &sensor_dev_attr_fan4_min.dev_attr.attr,
1689 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1690 &sensor_dev_attr_fan4_type.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001691 NULL
1692};
1693static struct attribute *dme1737_attr_fan5[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001694 &sensor_dev_attr_fan5_input.dev_attr.attr,
1695 &sensor_dev_attr_fan5_min.dev_attr.attr,
1696 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1697 &sensor_dev_attr_fan5_max.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001698 NULL
1699};
1700static struct attribute *dme1737_attr_fan6[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001701 &sensor_dev_attr_fan6_input.dev_attr.attr,
1702 &sensor_dev_attr_fan6_min.dev_attr.attr,
1703 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1704 &sensor_dev_attr_fan6_max.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001705 NULL
1706};
1707
1708static const struct attribute_group dme1737_fan_group[] = {
1709 { .attrs = dme1737_attr_fan1 },
1710 { .attrs = dme1737_attr_fan2 },
1711 { .attrs = dme1737_attr_fan3 },
1712 { .attrs = dme1737_attr_fan4 },
1713 { .attrs = dme1737_attr_fan5 },
1714 { .attrs = dme1737_attr_fan6 },
1715};
1716
1717/* The permissions of all of the following attributes are changed to read-
1718 * writeable if the chip is *not* locked. Otherwise they stay read-only. */
1719static struct attribute *dme1737_attr_lock[] = {
1720 /* Temperatures */
Juerg Haefliger9b257712008-03-25 21:49:02 -07001721 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1722 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1723 &sensor_dev_attr_temp3_offset.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001724 /* Zones */
Juerg Haefliger9b257712008-03-25 21:49:02 -07001725 &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
1726 &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
1727 &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
1728 &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
1729 &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
1730 &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
1731 &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
1732 &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
1733 &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
1734 &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
1735 &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
1736 &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001737 NULL
1738};
1739
1740static const struct attribute_group dme1737_lock_group = {
1741 .attrs = dme1737_attr_lock,
1742};
1743
1744/* The permissions of the following PWM attributes are changed to read-
1745 * writeable if the chip is *not* locked and the respective PWM is available.
1746 * Otherwise they stay read-only. */
1747static struct attribute *dme1737_attr_pwm1_lock[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001748 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1749 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1750 &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
1751 &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
1752 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1753 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001754 NULL
1755};
1756static struct attribute *dme1737_attr_pwm2_lock[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001757 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1758 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1759 &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
1760 &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
1761 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1762 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001763 NULL
1764};
1765static struct attribute *dme1737_attr_pwm3_lock[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001766 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1767 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1768 &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
1769 &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
1770 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1771 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001772 NULL
1773};
1774static struct attribute *dme1737_attr_pwm5_lock[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001775 &sensor_dev_attr_pwm5.dev_attr.attr,
1776 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001777 NULL
1778};
1779static struct attribute *dme1737_attr_pwm6_lock[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001780 &sensor_dev_attr_pwm6.dev_attr.attr,
1781 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001782 NULL
1783};
1784
1785static const struct attribute_group dme1737_pwm_lock_group[] = {
1786 { .attrs = dme1737_attr_pwm1_lock },
1787 { .attrs = dme1737_attr_pwm2_lock },
1788 { .attrs = dme1737_attr_pwm3_lock },
1789 { .attrs = NULL },
1790 { .attrs = dme1737_attr_pwm5_lock },
1791 { .attrs = dme1737_attr_pwm6_lock },
1792};
1793
1794/* Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the
1795 * chip is not locked. Otherwise they are read-only. */
1796static struct attribute *dme1737_attr_pwm[] = {
1797 &sensor_dev_attr_pwm1.dev_attr.attr,
1798 &sensor_dev_attr_pwm2.dev_attr.attr,
1799 &sensor_dev_attr_pwm3.dev_attr.attr,
1800};
1801
1802/* ---------------------------------------------------------------------
1803 * Super-IO functions
1804 * --------------------------------------------------------------------- */
1805
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001806static inline void dme1737_sio_enter(int sio_cip)
1807{
1808 outb(0x55, sio_cip);
1809}
1810
1811static inline void dme1737_sio_exit(int sio_cip)
1812{
1813 outb(0xaa, sio_cip);
1814}
1815
Juerg Haefliger94319962007-06-09 10:11:16 -04001816static inline int dme1737_sio_inb(int sio_cip, int reg)
1817{
1818 outb(reg, sio_cip);
1819 return inb(sio_cip + 1);
1820}
1821
1822static inline void dme1737_sio_outb(int sio_cip, int reg, int val)
1823{
1824 outb(reg, sio_cip);
1825 outb(val, sio_cip + 1);
1826}
1827
Juerg Haefliger94319962007-06-09 10:11:16 -04001828/* ---------------------------------------------------------------------
Juerg Haefligere95c2372007-10-07 21:27:35 -07001829 * Device initialization
Juerg Haefliger94319962007-06-09 10:11:16 -04001830 * --------------------------------------------------------------------- */
1831
Juerg Haefliger67e2f322007-10-01 21:20:28 -07001832static int dme1737_i2c_get_features(int, struct dme1737_data*);
Juerg Haefliger94319962007-06-09 10:11:16 -04001833
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001834static void dme1737_chmod_file(struct device *dev,
Juerg Haefliger94319962007-06-09 10:11:16 -04001835 struct attribute *attr, mode_t mode)
Juerg Haefliger94319962007-06-09 10:11:16 -04001836{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001837 if (sysfs_chmod_file(&dev->kobj, attr, mode)) {
1838 dev_warn(dev, "Failed to change permissions of %s.\n",
Juerg Haefliger94319962007-06-09 10:11:16 -04001839 attr->name);
1840 }
1841}
1842
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001843static void dme1737_chmod_group(struct device *dev,
Juerg Haefliger94319962007-06-09 10:11:16 -04001844 const struct attribute_group *group,
1845 mode_t mode)
1846{
1847 struct attribute **attr;
1848
1849 for (attr = group->attrs; *attr; attr++) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001850 dme1737_chmod_file(dev, *attr, mode);
Juerg Haefliger94319962007-06-09 10:11:16 -04001851 }
1852}
1853
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001854static void dme1737_remove_files(struct device *dev)
Juerg Haefliger94319962007-06-09 10:11:16 -04001855{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001856 struct dme1737_data *data = dev_get_drvdata(dev);
1857 int ix;
1858
1859 for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
1860 if (data->has_fan & (1 << ix)) {
1861 sysfs_remove_group(&dev->kobj,
1862 &dme1737_fan_group[ix]);
1863 }
1864 }
1865
1866 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
1867 if (data->has_pwm & (1 << ix)) {
1868 sysfs_remove_group(&dev->kobj,
1869 &dme1737_pwm_group[ix]);
1870 }
1871 }
1872
1873 sysfs_remove_group(&dev->kobj, &dme1737_group);
Juerg Haefligere95c2372007-10-07 21:27:35 -07001874
1875 if (!data->client.driver) {
1876 sysfs_remove_file(&dev->kobj, &dev_attr_name.attr);
1877 }
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001878}
1879
1880static int dme1737_create_files(struct device *dev)
1881{
1882 struct dme1737_data *data = dev_get_drvdata(dev);
1883 int err, ix;
1884
Juerg Haefligere95c2372007-10-07 21:27:35 -07001885 /* Create a name attribute for ISA devices */
1886 if (!data->client.driver &&
1887 (err = sysfs_create_file(&dev->kobj, &dev_attr_name.attr))) {
1888 goto exit;
1889 }
1890
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001891 /* Create standard sysfs attributes */
1892 if ((err = sysfs_create_group(&dev->kobj, &dme1737_group))) {
Juerg Haefligere95c2372007-10-07 21:27:35 -07001893 goto exit_remove;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001894 }
1895
1896 /* Create fan sysfs attributes */
1897 for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
1898 if (data->has_fan & (1 << ix)) {
1899 if ((err = sysfs_create_group(&dev->kobj,
1900 &dme1737_fan_group[ix]))) {
1901 goto exit_remove;
1902 }
1903 }
1904 }
1905
1906 /* Create PWM sysfs attributes */
1907 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
1908 if (data->has_pwm & (1 << ix)) {
1909 if ((err = sysfs_create_group(&dev->kobj,
1910 &dme1737_pwm_group[ix]))) {
1911 goto exit_remove;
1912 }
1913 }
1914 }
1915
1916 /* Inform if the device is locked. Otherwise change the permissions of
1917 * selected attributes from read-only to read-writeable. */
1918 if (data->config & 0x02) {
1919 dev_info(dev, "Device is locked. Some attributes "
1920 "will be read-only.\n");
1921 } else {
1922 /* Change permissions of standard attributes */
1923 dme1737_chmod_group(dev, &dme1737_lock_group,
1924 S_IRUGO | S_IWUSR);
1925
1926 /* Change permissions of PWM attributes */
1927 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_lock_group); ix++) {
1928 if (data->has_pwm & (1 << ix)) {
1929 dme1737_chmod_group(dev,
1930 &dme1737_pwm_lock_group[ix],
1931 S_IRUGO | S_IWUSR);
1932 }
1933 }
1934
1935 /* Change permissions of pwm[1-3] if in manual mode */
1936 for (ix = 0; ix < 3; ix++) {
1937 if ((data->has_pwm & (1 << ix)) &&
1938 (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) {
1939 dme1737_chmod_file(dev,
1940 dme1737_attr_pwm[ix],
1941 S_IRUGO | S_IWUSR);
1942 }
1943 }
1944 }
1945
1946 return 0;
1947
1948exit_remove:
1949 dme1737_remove_files(dev);
1950exit:
1951 return err;
1952}
1953
1954static int dme1737_init_device(struct device *dev)
1955{
1956 struct dme1737_data *data = dev_get_drvdata(dev);
1957 struct i2c_client *client = &data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -04001958 int ix;
1959 u8 reg;
1960
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001961 data->config = dme1737_read(client, DME1737_REG_CONFIG);
1962 /* Inform if part is not monitoring/started */
1963 if (!(data->config & 0x01)) {
1964 if (!force_start) {
1965 dev_err(dev, "Device is not monitoring. "
1966 "Use the force_start load parameter to "
1967 "override.\n");
1968 return -EFAULT;
1969 }
Juerg Haefliger94319962007-06-09 10:11:16 -04001970
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001971 /* Force monitoring */
1972 data->config |= 0x01;
1973 dme1737_write(client, DME1737_REG_CONFIG, data->config);
1974 }
Juerg Haefliger94319962007-06-09 10:11:16 -04001975 /* Inform if part is not ready */
1976 if (!(data->config & 0x04)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001977 dev_err(dev, "Device is not ready.\n");
Juerg Haefliger94319962007-06-09 10:11:16 -04001978 return -EFAULT;
1979 }
1980
Juerg Haefligere95c2372007-10-07 21:27:35 -07001981 /* Determine which optional fan and pwm features are enabled/present */
1982 if (client->driver) { /* I2C chip */
1983 data->config2 = dme1737_read(client, DME1737_REG_CONFIG2);
1984 /* Check if optional fan3 input is enabled */
1985 if (data->config2 & 0x04) {
1986 data->has_fan |= (1 << 2);
1987 }
1988
1989 /* Fan4 and pwm3 are only available if the client's I2C address
1990 * is the default 0x2e. Otherwise the I/Os associated with
1991 * these functions are used for addr enable/select. */
1992 if (data->client.addr == 0x2e) {
1993 data->has_fan |= (1 << 3);
1994 data->has_pwm |= (1 << 2);
1995 }
1996
1997 /* Determine which of the optional fan[5-6] and pwm[5-6]
1998 * features are enabled. For this, we need to query the runtime
1999 * registers through the Super-IO LPC interface. Try both
2000 * config ports 0x2e and 0x4e. */
2001 if (dme1737_i2c_get_features(0x2e, data) &&
2002 dme1737_i2c_get_features(0x4e, data)) {
2003 dev_warn(dev, "Failed to query Super-IO for optional "
2004 "features.\n");
2005 }
2006 } else { /* ISA chip */
2007 /* Fan3 and pwm3 are always available. Fan[4-5] and pwm[5-6]
2008 * don't exist in the ISA chip. */
Juerg Haefliger94319962007-06-09 10:11:16 -04002009 data->has_fan |= (1 << 2);
Juerg Haefliger94319962007-06-09 10:11:16 -04002010 data->has_pwm |= (1 << 2);
2011 }
2012
Juerg Haefliger94319962007-06-09 10:11:16 -04002013 /* Fan1, fan2, pwm1, and pwm2 are always present */
2014 data->has_fan |= 0x03;
2015 data->has_pwm |= 0x03;
2016
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002017 dev_info(dev, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, "
Juerg Haefliger94319962007-06-09 10:11:16 -04002018 "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n",
2019 (data->has_pwm & (1 << 2)) ? "yes" : "no",
2020 (data->has_pwm & (1 << 4)) ? "yes" : "no",
2021 (data->has_pwm & (1 << 5)) ? "yes" : "no",
2022 (data->has_fan & (1 << 2)) ? "yes" : "no",
2023 (data->has_fan & (1 << 3)) ? "yes" : "no",
2024 (data->has_fan & (1 << 4)) ? "yes" : "no",
2025 (data->has_fan & (1 << 5)) ? "yes" : "no");
2026
2027 reg = dme1737_read(client, DME1737_REG_TACH_PWM);
2028 /* Inform if fan-to-pwm mapping differs from the default */
Juerg Haefligere95c2372007-10-07 21:27:35 -07002029 if (client->driver && reg != 0xa4) { /* I2C chip */
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002030 dev_warn(dev, "Non-standard fan to pwm mapping: "
Juerg Haefliger94319962007-06-09 10:11:16 -04002031 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, "
2032 "fan4->pwm%d. Please report to the driver "
2033 "maintainer.\n",
2034 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
2035 ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002036 } else if (!client->driver && reg != 0x24) { /* ISA chip */
2037 dev_warn(dev, "Non-standard fan to pwm mapping: "
2038 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. "
2039 "Please report to the driver maintainer.\n",
2040 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
2041 ((reg >> 4) & 0x03) + 1);
Juerg Haefliger94319962007-06-09 10:11:16 -04002042 }
2043
2044 /* Switch pwm[1-3] to manual mode if they are currently disabled and
2045 * set the duty-cycles to 0% (which is identical to the PWMs being
2046 * disabled). */
2047 if (!(data->config & 0x02)) {
2048 for (ix = 0; ix < 3; ix++) {
2049 data->pwm_config[ix] = dme1737_read(client,
2050 DME1737_REG_PWM_CONFIG(ix));
2051 if ((data->has_pwm & (1 << ix)) &&
2052 (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002053 dev_info(dev, "Switching pwm%d to "
Juerg Haefliger94319962007-06-09 10:11:16 -04002054 "manual mode.\n", ix + 1);
2055 data->pwm_config[ix] = PWM_EN_TO_REG(1,
2056 data->pwm_config[ix]);
2057 dme1737_write(client, DME1737_REG_PWM(ix), 0);
2058 dme1737_write(client,
2059 DME1737_REG_PWM_CONFIG(ix),
2060 data->pwm_config[ix]);
2061 }
2062 }
2063 }
2064
2065 /* Initialize the default PWM auto channels zone (acz) assignments */
2066 data->pwm_acz[0] = 1; /* pwm1 -> zone1 */
2067 data->pwm_acz[1] = 2; /* pwm2 -> zone2 */
2068 data->pwm_acz[2] = 4; /* pwm3 -> zone3 */
2069
2070 /* Set VRM */
2071 data->vrm = vid_which_vrm();
2072
2073 return 0;
2074}
2075
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002076/* ---------------------------------------------------------------------
2077 * I2C device detection and registration
2078 * --------------------------------------------------------------------- */
2079
2080static struct i2c_driver dme1737_i2c_driver;
2081
2082static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data)
2083{
Juerg Haefliger94319962007-06-09 10:11:16 -04002084 int err = 0, reg;
2085 u16 addr;
2086
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002087 dme1737_sio_enter(sio_cip);
Juerg Haefliger94319962007-06-09 10:11:16 -04002088
2089 /* Check device ID
2090 * The DME1737 can return either 0x78 or 0x77 as its device ID. */
Juerg Haefliger345a2222008-01-26 08:54:24 -08002091 reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
Juerg Haefliger94319962007-06-09 10:11:16 -04002092 if (!(reg == 0x77 || reg == 0x78)) {
2093 err = -ENODEV;
2094 goto exit;
2095 }
2096
2097 /* Select logical device A (runtime registers) */
2098 dme1737_sio_outb(sio_cip, 0x07, 0x0a);
2099
2100 /* Get the base address of the runtime registers */
2101 if (!(addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
2102 dme1737_sio_inb(sio_cip, 0x61))) {
2103 err = -ENODEV;
2104 goto exit;
2105 }
2106
2107 /* Read the runtime registers to determine which optional features
2108 * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set
2109 * to '10' if the respective feature is enabled. */
2110 if ((inb(addr + 0x43) & 0x0c) == 0x08) { /* fan6 */
2111 data->has_fan |= (1 << 5);
2112 }
2113 if ((inb(addr + 0x44) & 0x0c) == 0x08) { /* pwm6 */
2114 data->has_pwm |= (1 << 5);
2115 }
2116 if ((inb(addr + 0x45) & 0x0c) == 0x08) { /* fan5 */
2117 data->has_fan |= (1 << 4);
2118 }
2119 if ((inb(addr + 0x46) & 0x0c) == 0x08) { /* pwm5 */
2120 data->has_pwm |= (1 << 4);
2121 }
2122
2123exit:
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002124 dme1737_sio_exit(sio_cip);
Juerg Haefliger94319962007-06-09 10:11:16 -04002125
2126 return err;
2127}
2128
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002129static int dme1737_i2c_detect(struct i2c_adapter *adapter, int address,
2130 int kind)
Juerg Haefliger94319962007-06-09 10:11:16 -04002131{
2132 u8 company, verstep = 0;
2133 struct i2c_client *client;
2134 struct dme1737_data *data;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002135 struct device *dev;
2136 int err = 0;
Juerg Haefliger94319962007-06-09 10:11:16 -04002137 const char *name;
2138
2139 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
2140 goto exit;
2141 }
2142
2143 if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
2144 err = -ENOMEM;
2145 goto exit;
2146 }
2147
2148 client = &data->client;
2149 i2c_set_clientdata(client, data);
2150 client->addr = address;
2151 client->adapter = adapter;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002152 client->driver = &dme1737_i2c_driver;
2153 dev = &client->dev;
Juerg Haefliger94319962007-06-09 10:11:16 -04002154
2155 /* A negative kind means that the driver was loaded with no force
2156 * parameter (default), so we must identify the chip. */
2157 if (kind < 0) {
2158 company = dme1737_read(client, DME1737_REG_COMPANY);
2159 verstep = dme1737_read(client, DME1737_REG_VERSTEP);
2160
2161 if (!((company == DME1737_COMPANY_SMSC) &&
2162 ((verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP))) {
2163 err = -ENODEV;
2164 goto exit_kfree;
2165 }
2166 }
2167
2168 kind = dme1737;
2169 name = "dme1737";
2170
2171 /* Fill in the remaining client fields and put it into the global
2172 * list */
2173 strlcpy(client->name, name, I2C_NAME_SIZE);
2174 mutex_init(&data->update_lock);
2175
2176 /* Tell the I2C layer a new client has arrived */
2177 if ((err = i2c_attach_client(client))) {
2178 goto exit_kfree;
2179 }
2180
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002181 dev_info(dev, "Found a DME1737 chip at 0x%02x (rev 0x%02x).\n",
2182 client->addr, verstep);
2183
Juerg Haefliger94319962007-06-09 10:11:16 -04002184 /* Initialize the DME1737 chip */
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002185 if ((err = dme1737_init_device(dev))) {
2186 dev_err(dev, "Failed to initialize device.\n");
Juerg Haefliger94319962007-06-09 10:11:16 -04002187 goto exit_detach;
2188 }
2189
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002190 /* Create sysfs files */
2191 if ((err = dme1737_create_files(dev))) {
2192 dev_err(dev, "Failed to create sysfs files.\n");
2193 goto exit_detach;
Juerg Haefliger94319962007-06-09 10:11:16 -04002194 }
2195
2196 /* Register device */
Mark M. Hoffman62ee3e12007-10-10 22:32:50 -04002197 data->hwmon_dev = hwmon_device_register(dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07002198 if (IS_ERR(data->hwmon_dev)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002199 dev_err(dev, "Failed to register device.\n");
Tony Jones1beeffe2007-08-20 13:46:20 -07002200 err = PTR_ERR(data->hwmon_dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04002201 goto exit_remove;
2202 }
2203
Juerg Haefliger94319962007-06-09 10:11:16 -04002204 return 0;
2205
2206exit_remove:
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002207 dme1737_remove_files(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04002208exit_detach:
2209 i2c_detach_client(client);
2210exit_kfree:
2211 kfree(data);
2212exit:
2213 return err;
2214}
2215
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002216static int dme1737_i2c_attach_adapter(struct i2c_adapter *adapter)
Juerg Haefliger94319962007-06-09 10:11:16 -04002217{
2218 if (!(adapter->class & I2C_CLASS_HWMON)) {
2219 return 0;
2220 }
2221
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002222 return i2c_probe(adapter, &addr_data, dme1737_i2c_detect);
Juerg Haefliger94319962007-06-09 10:11:16 -04002223}
2224
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002225static int dme1737_i2c_detach_client(struct i2c_client *client)
Juerg Haefliger94319962007-06-09 10:11:16 -04002226{
2227 struct dme1737_data *data = i2c_get_clientdata(client);
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002228 int err;
Juerg Haefliger94319962007-06-09 10:11:16 -04002229
Tony Jones1beeffe2007-08-20 13:46:20 -07002230 hwmon_device_unregister(data->hwmon_dev);
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002231 dme1737_remove_files(&client->dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04002232
2233 if ((err = i2c_detach_client(client))) {
2234 return err;
2235 }
2236
2237 kfree(data);
2238 return 0;
2239}
2240
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002241static struct i2c_driver dme1737_i2c_driver = {
Juerg Haefliger94319962007-06-09 10:11:16 -04002242 .driver = {
2243 .name = "dme1737",
2244 },
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002245 .attach_adapter = dme1737_i2c_attach_adapter,
2246 .detach_client = dme1737_i2c_detach_client,
Juerg Haefliger94319962007-06-09 10:11:16 -04002247};
2248
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002249/* ---------------------------------------------------------------------
Juerg Haefligere95c2372007-10-07 21:27:35 -07002250 * ISA device detection and registration
2251 * --------------------------------------------------------------------- */
2252
2253static int __init dme1737_isa_detect(int sio_cip, unsigned short *addr)
2254{
2255 int err = 0, reg;
2256 unsigned short base_addr;
2257
2258 dme1737_sio_enter(sio_cip);
2259
2260 /* Check device ID
2261 * We currently know about SCH3112 (0x7c), SCH3114 (0x7d), and
2262 * SCH3116 (0x7f). */
Jean Delvare67b671b2007-12-06 23:13:42 +01002263 reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002264 if (!(reg == 0x7c || reg == 0x7d || reg == 0x7f)) {
2265 err = -ENODEV;
2266 goto exit;
2267 }
2268
2269 /* Select logical device A (runtime registers) */
2270 dme1737_sio_outb(sio_cip, 0x07, 0x0a);
2271
2272 /* Get the base address of the runtime registers */
2273 if (!(base_addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
2274 dme1737_sio_inb(sio_cip, 0x61))) {
2275 printk(KERN_ERR "dme1737: Base address not set.\n");
2276 err = -ENODEV;
2277 goto exit;
2278 }
2279
2280 /* Access to the hwmon registers is through an index/data register
2281 * pair located at offset 0x70/0x71. */
2282 *addr = base_addr + 0x70;
2283
2284exit:
2285 dme1737_sio_exit(sio_cip);
2286 return err;
2287}
2288
2289static int __init dme1737_isa_device_add(unsigned short addr)
2290{
2291 struct resource res = {
2292 .start = addr,
2293 .end = addr + DME1737_EXTENT - 1,
2294 .name = "dme1737",
2295 .flags = IORESOURCE_IO,
2296 };
2297 int err;
2298
2299 if (!(pdev = platform_device_alloc("dme1737", addr))) {
2300 printk(KERN_ERR "dme1737: Failed to allocate device.\n");
2301 err = -ENOMEM;
2302 goto exit;
2303 }
2304
2305 if ((err = platform_device_add_resources(pdev, &res, 1))) {
2306 printk(KERN_ERR "dme1737: Failed to add device resource "
2307 "(err = %d).\n", err);
2308 goto exit_device_put;
2309 }
2310
2311 if ((err = platform_device_add(pdev))) {
2312 printk(KERN_ERR "dme1737: Failed to add device (err = %d).\n",
2313 err);
2314 goto exit_device_put;
2315 }
2316
2317 return 0;
2318
2319exit_device_put:
2320 platform_device_put(pdev);
2321 pdev = NULL;
2322exit:
2323 return err;
2324}
2325
2326static int __devinit dme1737_isa_probe(struct platform_device *pdev)
2327{
2328 u8 company, device;
2329 struct resource *res;
2330 struct i2c_client *client;
2331 struct dme1737_data *data;
2332 struct device *dev = &pdev->dev;
2333 int err;
2334
2335 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2336 if (!request_region(res->start, DME1737_EXTENT, "dme1737")) {
2337 dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
2338 (unsigned short)res->start,
2339 (unsigned short)res->start + DME1737_EXTENT - 1);
2340 err = -EBUSY;
2341 goto exit;
2342 }
2343
2344 if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
2345 err = -ENOMEM;
2346 goto exit_release_region;
2347 }
2348
2349 client = &data->client;
2350 i2c_set_clientdata(client, data);
2351 client->addr = res->start;
2352 platform_set_drvdata(pdev, data);
2353
2354 company = dme1737_read(client, DME1737_REG_COMPANY);
2355 device = dme1737_read(client, DME1737_REG_DEVICE);
2356
2357 if (!((company == DME1737_COMPANY_SMSC) &&
2358 (device == SCH311X_DEVICE))) {
2359 err = -ENODEV;
2360 goto exit_kfree;
2361 }
2362
2363 /* Fill in the remaining client fields and initialize the mutex */
2364 strlcpy(client->name, "sch311x", I2C_NAME_SIZE);
2365 mutex_init(&data->update_lock);
2366
2367 dev_info(dev, "Found a SCH311x chip at 0x%04x\n", client->addr);
2368
2369 /* Initialize the chip */
2370 if ((err = dme1737_init_device(dev))) {
2371 dev_err(dev, "Failed to initialize device.\n");
2372 goto exit_kfree;
2373 }
2374
2375 /* Create sysfs files */
2376 if ((err = dme1737_create_files(dev))) {
2377 dev_err(dev, "Failed to create sysfs files.\n");
2378 goto exit_kfree;
2379 }
2380
2381 /* Register device */
2382 data->hwmon_dev = hwmon_device_register(dev);
2383 if (IS_ERR(data->hwmon_dev)) {
2384 dev_err(dev, "Failed to register device.\n");
2385 err = PTR_ERR(data->hwmon_dev);
2386 goto exit_remove_files;
2387 }
2388
2389 return 0;
2390
2391exit_remove_files:
2392 dme1737_remove_files(dev);
2393exit_kfree:
2394 platform_set_drvdata(pdev, NULL);
2395 kfree(data);
2396exit_release_region:
2397 release_region(res->start, DME1737_EXTENT);
2398exit:
2399 return err;
2400}
2401
2402static int __devexit dme1737_isa_remove(struct platform_device *pdev)
2403{
2404 struct dme1737_data *data = platform_get_drvdata(pdev);
2405
2406 hwmon_device_unregister(data->hwmon_dev);
2407 dme1737_remove_files(&pdev->dev);
2408 release_region(data->client.addr, DME1737_EXTENT);
2409 platform_set_drvdata(pdev, NULL);
2410 kfree(data);
2411
2412 return 0;
2413}
2414
2415static struct platform_driver dme1737_isa_driver = {
2416 .driver = {
2417 .owner = THIS_MODULE,
2418 .name = "dme1737",
2419 },
2420 .probe = dme1737_isa_probe,
2421 .remove = __devexit_p(dme1737_isa_remove),
2422};
2423
2424/* ---------------------------------------------------------------------
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002425 * Module initialization and cleanup
2426 * --------------------------------------------------------------------- */
2427
Juerg Haefliger94319962007-06-09 10:11:16 -04002428static int __init dme1737_init(void)
2429{
Juerg Haefligere95c2372007-10-07 21:27:35 -07002430 int err;
2431 unsigned short addr;
2432
2433 if ((err = i2c_add_driver(&dme1737_i2c_driver))) {
2434 goto exit;
2435 }
2436
2437 if (dme1737_isa_detect(0x2e, &addr) &&
Juerg Haefliger92430b62008-04-03 21:34:19 -07002438 dme1737_isa_detect(0x4e, &addr) &&
2439 (!probe_all_addr ||
2440 (dme1737_isa_detect(0x162e, &addr) &&
2441 dme1737_isa_detect(0x164e, &addr)))) {
Juerg Haefligere95c2372007-10-07 21:27:35 -07002442 /* Return 0 if we didn't find an ISA device */
2443 return 0;
2444 }
2445
2446 if ((err = platform_driver_register(&dme1737_isa_driver))) {
2447 goto exit_del_i2c_driver;
2448 }
2449
2450 /* Sets global pdev as a side effect */
2451 if ((err = dme1737_isa_device_add(addr))) {
2452 goto exit_del_isa_driver;
2453 }
2454
2455 return 0;
2456
2457exit_del_isa_driver:
2458 platform_driver_unregister(&dme1737_isa_driver);
2459exit_del_i2c_driver:
2460 i2c_del_driver(&dme1737_i2c_driver);
2461exit:
2462 return err;
Juerg Haefliger94319962007-06-09 10:11:16 -04002463}
2464
2465static void __exit dme1737_exit(void)
2466{
Juerg Haefligere95c2372007-10-07 21:27:35 -07002467 if (pdev) {
2468 platform_device_unregister(pdev);
2469 platform_driver_unregister(&dme1737_isa_driver);
2470 }
2471
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002472 i2c_del_driver(&dme1737_i2c_driver);
Juerg Haefliger94319962007-06-09 10:11:16 -04002473}
2474
2475MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
2476MODULE_DESCRIPTION("DME1737 sensors");
2477MODULE_LICENSE("GPL");
2478
2479module_init(dme1737_init);
2480module_exit(dme1737_exit);