blob: ec8edd2a8cbda29565db35d03a17d90f75660d7c [file] [log] [blame]
Juerg Haefliger94319962007-06-09 10:11:16 -04001/*
2 * dme1737.c - driver for the SMSC DME1737 and Asus A8000 Super-I/O chips
3 * integrated hardware monitoring features.
4 * Copyright (c) 2007 Juerg Haefliger <juergh@gmail.com>
5 *
6 * This driver is based on the LM85 driver. The hardware monitoring
7 * capabilities of the DME1737 are very similar to the LM85 with some
8 * additional features. Even though the DME1737 is a Super-I/O chip, the
9 * hardware monitoring registers are only accessible via SMBus.
10 *
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>
31#include <linux/hwmon.h>
32#include <linux/hwmon-sysfs.h>
33#include <linux/hwmon-vid.h>
34#include <linux/err.h>
35#include <linux/mutex.h>
36#include <asm/io.h>
37
38/* Module load parameters */
39static int force_start;
40module_param(force_start, bool, 0);
41MODULE_PARM_DESC(force_start, "Force the chip to start monitoring inputs");
42
43/* Addresses to scan */
44static unsigned short normal_i2c[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END};
45
46/* Insmod parameters */
47I2C_CLIENT_INSMOD_1(dme1737);
48
49/* ---------------------------------------------------------------------
50 * Registers
51 *
52 * The sensors are defined as follows:
53 *
54 * Voltages Temperatures
55 * -------- ------------
56 * in0 +5VTR (+5V stdby) temp1 Remote diode 1
57 * in1 Vccp (proc core) temp2 Internal temp
58 * in2 VCC (internal +3.3V) temp3 Remote diode 2
59 * in3 +5V
60 * in4 +12V
61 * in5 VTR (+3.3V stby)
62 * in6 Vbat
63 *
64 * --------------------------------------------------------------------- */
65
66/* Voltages (in) numbered 0-6 (ix) */
67#define DME1737_REG_IN(ix) ((ix) < 5 ? 0x20 + (ix) \
68 : 0x94 + (ix))
69#define DME1737_REG_IN_MIN(ix) ((ix) < 5 ? 0x44 + (ix) * 2 \
70 : 0x91 + (ix) * 2)
71#define DME1737_REG_IN_MAX(ix) ((ix) < 5 ? 0x45 + (ix) * 2 \
72 : 0x92 + (ix) * 2)
73
74/* Temperatures (temp) numbered 0-2 (ix) */
75#define DME1737_REG_TEMP(ix) (0x25 + (ix))
76#define DME1737_REG_TEMP_MIN(ix) (0x4e + (ix) * 2)
77#define DME1737_REG_TEMP_MAX(ix) (0x4f + (ix) * 2)
78#define DME1737_REG_TEMP_OFFSET(ix) ((ix) == 0 ? 0x1f \
79 : 0x1c + (ix))
80
81/* Voltage and temperature LSBs
82 * The LSBs (4 bits each) are stored in 5 registers with the following layouts:
83 * IN_TEMP_LSB(0) = [in5, in6]
84 * IN_TEMP_LSB(1) = [temp3, temp1]
85 * IN_TEMP_LSB(2) = [in4, temp2]
86 * IN_TEMP_LSB(3) = [in3, in0]
87 * IN_TEMP_LSB(4) = [in2, in1] */
88#define DME1737_REG_IN_TEMP_LSB(ix) (0x84 + (ix))
89static const u8 DME1737_REG_IN_LSB[] = {3, 4, 4, 3, 2, 0, 0};
90static const u8 DME1737_REG_IN_LSB_SHL[] = {4, 4, 0, 0, 0, 0, 4};
91static const u8 DME1737_REG_TEMP_LSB[] = {1, 2, 1};
92static const u8 DME1737_REG_TEMP_LSB_SHL[] = {4, 4, 0};
93
94/* Fans numbered 0-5 (ix) */
95#define DME1737_REG_FAN(ix) ((ix) < 4 ? 0x28 + (ix) * 2 \
96 : 0xa1 + (ix) * 2)
97#define DME1737_REG_FAN_MIN(ix) ((ix) < 4 ? 0x54 + (ix) * 2 \
98 : 0xa5 + (ix) * 2)
99#define DME1737_REG_FAN_OPT(ix) ((ix) < 4 ? 0x90 + (ix) \
100 : 0xb2 + (ix))
101#define DME1737_REG_FAN_MAX(ix) (0xb4 + (ix)) /* only for fan[4-5] */
102
103/* PWMs numbered 0-2, 4-5 (ix) */
104#define DME1737_REG_PWM(ix) ((ix) < 3 ? 0x30 + (ix) \
105 : 0xa1 + (ix))
106#define DME1737_REG_PWM_CONFIG(ix) (0x5c + (ix)) /* only for pwm[0-2] */
107#define DME1737_REG_PWM_MIN(ix) (0x64 + (ix)) /* only for pwm[0-2] */
108#define DME1737_REG_PWM_FREQ(ix) ((ix) < 3 ? 0x5f + (ix) \
109 : 0xa3 + (ix))
110/* The layout of the ramp rate registers is different from the other pwm
111 * registers. The bits for the 3 PWMs are stored in 2 registers:
112 * PWM_RR(0) = [OFF3, OFF2, OFF1, RES, RR1E, RR1-2, RR1-1, RR1-0]
113 * PWM_RR(1) = [RR2E, RR2-2, RR2-1, RR2-0, RR3E, RR3-2, RR3-1, RR3-0] */
114#define DME1737_REG_PWM_RR(ix) (0x62 + (ix)) /* only for pwm[0-2] */
115
116/* Thermal zones 0-2 */
117#define DME1737_REG_ZONE_LOW(ix) (0x67 + (ix))
118#define DME1737_REG_ZONE_ABS(ix) (0x6a + (ix))
119/* The layout of the hysteresis registers is different from the other zone
120 * registers. The bits for the 3 zones are stored in 2 registers:
121 * ZONE_HYST(0) = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
122 * ZONE_HYST(1) = [H3-3, H3-2, H3-1, H3-0, RES, RES, RES, RES] */
123#define DME1737_REG_ZONE_HYST(ix) (0x6d + (ix))
124
125/* Alarm registers and bit mapping
126 * The 3 8-bit alarm registers will be concatenated to a single 32-bit
127 * alarm value [0, ALARM3, ALARM2, ALARM1]. */
128#define DME1737_REG_ALARM1 0x41
129#define DME1737_REG_ALARM2 0x42
130#define DME1737_REG_ALARM3 0x83
131static const u8 DME1737_BIT_ALARM_IN[] = {0, 1, 2, 3, 8, 16, 17};
132static const u8 DME1737_BIT_ALARM_TEMP[] = {4, 5, 6};
133static const u8 DME1737_BIT_ALARM_FAN[] = {10, 11, 12, 13, 22, 23};
134
135/* Miscellaneous registers */
136#define DME1737_REG_COMPANY 0x3e
137#define DME1737_REG_VERSTEP 0x3f
138#define DME1737_REG_CONFIG 0x40
139#define DME1737_REG_CONFIG2 0x7f
140#define DME1737_REG_VID 0x43
141#define DME1737_REG_TACH_PWM 0x81
142
143/* ---------------------------------------------------------------------
144 * Misc defines
145 * --------------------------------------------------------------------- */
146
147/* Chip identification */
148#define DME1737_COMPANY_SMSC 0x5c
149#define DME1737_VERSTEP 0x88
150#define DME1737_VERSTEP_MASK 0xf8
151
152/* ---------------------------------------------------------------------
153 * Data structures and manipulation thereof
154 * --------------------------------------------------------------------- */
155
156struct dme1737_data {
157 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700158 struct device *hwmon_dev;
Juerg Haefliger94319962007-06-09 10:11:16 -0400159
160 struct mutex update_lock;
161 int valid; /* !=0 if following fields are valid */
162 unsigned long last_update; /* in jiffies */
163 unsigned long last_vbat; /* in jiffies */
164
165 u8 vid;
166 u8 pwm_rr_en;
167 u8 has_pwm;
168 u8 has_fan;
169
170 /* Register values */
171 u16 in[7];
172 u8 in_min[7];
173 u8 in_max[7];
174 s16 temp[3];
175 s8 temp_min[3];
176 s8 temp_max[3];
177 s8 temp_offset[3];
178 u8 config;
179 u8 config2;
180 u8 vrm;
181 u16 fan[6];
182 u16 fan_min[6];
183 u8 fan_max[2];
184 u8 fan_opt[6];
185 u8 pwm[6];
186 u8 pwm_min[3];
187 u8 pwm_config[3];
188 u8 pwm_acz[3];
189 u8 pwm_freq[6];
190 u8 pwm_rr[2];
191 u8 zone_low[3];
192 u8 zone_abs[3];
193 u8 zone_hyst[2];
194 u32 alarms;
195};
196
197/* Nominal voltage values */
198static const int IN_NOMINAL[] = {5000, 2250, 3300, 5000, 12000, 3300, 3300};
199
200/* Voltage input
201 * Voltage inputs have 16 bits resolution, limit values have 8 bits
202 * resolution. */
203static inline int IN_FROM_REG(int reg, int ix, int res)
204{
205 return (reg * IN_NOMINAL[ix] + (3 << (res - 3))) / (3 << (res - 2));
206}
207
208static inline int IN_TO_REG(int val, int ix)
209{
210 return SENSORS_LIMIT((val * 192 + IN_NOMINAL[ix] / 2) /
211 IN_NOMINAL[ix], 0, 255);
212}
213
214/* Temperature input
215 * The register values represent temperatures in 2's complement notation from
216 * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit
217 * values have 8 bits resolution. */
218static inline int TEMP_FROM_REG(int reg, int res)
219{
220 return (reg * 1000) >> (res - 8);
221}
222
223static inline int TEMP_TO_REG(int val)
224{
225 return SENSORS_LIMIT((val < 0 ? val - 500 : val + 500) / 1000,
226 -128, 127);
227}
228
229/* Temperature range */
230static const int TEMP_RANGE[] = {2000, 2500, 3333, 4000, 5000, 6666, 8000,
231 10000, 13333, 16000, 20000, 26666, 32000,
232 40000, 53333, 80000};
233
234static inline int TEMP_RANGE_FROM_REG(int reg)
235{
236 return TEMP_RANGE[(reg >> 4) & 0x0f];
237}
238
239static int TEMP_RANGE_TO_REG(int val, int reg)
240{
241 int i;
242
243 for (i = 15; i > 0; i--) {
244 if (val > (TEMP_RANGE[i] + TEMP_RANGE[i - 1] + 1) / 2) {
245 break;
246 }
247 }
248
249 return (reg & 0x0f) | (i << 4);
250}
251
252/* Temperature hysteresis
253 * Register layout:
254 * reg[0] = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
255 * reg[1] = [H3-3, H3-2, H3-1, H3-0, xxxx, xxxx, xxxx, xxxx] */
256static inline int TEMP_HYST_FROM_REG(int reg, int ix)
257{
258 return (((ix == 1) ? reg : reg >> 4) & 0x0f) * 1000;
259}
260
261static inline int TEMP_HYST_TO_REG(int val, int ix, int reg)
262{
263 int hyst = SENSORS_LIMIT((val + 500) / 1000, 0, 15);
264
265 return (ix == 1) ? (reg & 0xf0) | hyst : (reg & 0x0f) | (hyst << 4);
266}
267
268/* Fan input RPM */
269static inline int FAN_FROM_REG(int reg, int tpc)
270{
271 return (reg == 0 || reg == 0xffff) ? 0 :
272 (tpc == 0) ? 90000 * 60 / reg : tpc * reg;
273}
274
275static inline int FAN_TO_REG(int val, int tpc)
276{
277 return SENSORS_LIMIT((tpc == 0) ? 90000 * 60 / val : val / tpc,
278 0, 0xffff);
279}
280
281/* Fan TPC (tach pulse count)
282 * Converts a register value to a TPC multiplier or returns 0 if the tachometer
283 * is configured in legacy (non-tpc) mode */
284static inline int FAN_TPC_FROM_REG(int reg)
285{
286 return (reg & 0x20) ? 0 : 60 >> (reg & 0x03);
287}
288
289/* Fan type
290 * The type of a fan is expressed in number of pulses-per-revolution that it
291 * emits */
292static inline int FAN_TYPE_FROM_REG(int reg)
293{
294 int edge = (reg >> 1) & 0x03;
295
296 return (edge > 0) ? 1 << (edge - 1) : 0;
297}
298
299static inline int FAN_TYPE_TO_REG(int val, int reg)
300{
301 int edge = (val == 4) ? 3 : val;
302
303 return (reg & 0xf9) | (edge << 1);
304}
305
306/* Fan max RPM */
307static const int FAN_MAX[] = {0x54, 0x38, 0x2a, 0x21, 0x1c, 0x18, 0x15, 0x12,
308 0x11, 0x0f, 0x0e};
309
310static int FAN_MAX_FROM_REG(int reg)
311{
312 int i;
313
314 for (i = 10; i > 0; i--) {
315 if (reg == FAN_MAX[i]) {
316 break;
317 }
318 }
319
320 return 1000 + i * 500;
321}
322
323static int FAN_MAX_TO_REG(int val)
324{
325 int i;
326
327 for (i = 10; i > 0; i--) {
328 if (val > (1000 + (i - 1) * 500)) {
329 break;
330 }
331 }
332
333 return FAN_MAX[i];
334}
335
336/* PWM enable
337 * Register to enable mapping:
338 * 000: 2 fan on zone 1 auto
339 * 001: 2 fan on zone 2 auto
340 * 010: 2 fan on zone 3 auto
341 * 011: 0 fan full on
342 * 100: -1 fan disabled
343 * 101: 2 fan on hottest of zones 2,3 auto
344 * 110: 2 fan on hottest of zones 1,2,3 auto
345 * 111: 1 fan in manual mode */
346static inline int PWM_EN_FROM_REG(int reg)
347{
348 static const int en[] = {2, 2, 2, 0, -1, 2, 2, 1};
349
350 return en[(reg >> 5) & 0x07];
351}
352
353static inline int PWM_EN_TO_REG(int val, int reg)
354{
355 int en = (val == 1) ? 7 : 3;
356
357 return (reg & 0x1f) | ((en & 0x07) << 5);
358}
359
360/* PWM auto channels zone
361 * Register to auto channels zone mapping (ACZ is a bitfield with bit x
362 * corresponding to zone x+1):
363 * 000: 001 fan on zone 1 auto
364 * 001: 010 fan on zone 2 auto
365 * 010: 100 fan on zone 3 auto
366 * 011: 000 fan full on
367 * 100: 000 fan disabled
368 * 101: 110 fan on hottest of zones 2,3 auto
369 * 110: 111 fan on hottest of zones 1,2,3 auto
370 * 111: 000 fan in manual mode */
371static inline int PWM_ACZ_FROM_REG(int reg)
372{
373 static const int acz[] = {1, 2, 4, 0, 0, 6, 7, 0};
374
375 return acz[(reg >> 5) & 0x07];
376}
377
378static inline int PWM_ACZ_TO_REG(int val, int reg)
379{
380 int acz = (val == 4) ? 2 : val - 1;
381
382 return (reg & 0x1f) | ((acz & 0x07) << 5);
383}
384
385/* PWM frequency */
386static const int PWM_FREQ[] = {11, 15, 22, 29, 35, 44, 59, 88,
387 15000, 20000, 30000, 25000, 0, 0, 0, 0};
388
389static inline int PWM_FREQ_FROM_REG(int reg)
390{
391 return PWM_FREQ[reg & 0x0f];
392}
393
394static int PWM_FREQ_TO_REG(int val, int reg)
395{
396 int i;
397
398 /* the first two cases are special - stupid chip design! */
399 if (val > 27500) {
400 i = 10;
401 } else if (val > 22500) {
402 i = 11;
403 } else {
404 for (i = 9; i > 0; i--) {
405 if (val > (PWM_FREQ[i] + PWM_FREQ[i - 1] + 1) / 2) {
406 break;
407 }
408 }
409 }
410
411 return (reg & 0xf0) | i;
412}
413
414/* PWM ramp rate
415 * Register layout:
416 * reg[0] = [OFF3, OFF2, OFF1, RES, RR1-E, RR1-2, RR1-1, RR1-0]
417 * reg[1] = [RR2-E, RR2-2, RR2-1, RR2-0, RR3-E, RR3-2, RR3-1, RR3-0] */
418static const u8 PWM_RR[] = {206, 104, 69, 41, 26, 18, 10, 5};
419
420static inline int PWM_RR_FROM_REG(int reg, int ix)
421{
422 int rr = (ix == 1) ? reg >> 4 : reg;
423
424 return (rr & 0x08) ? PWM_RR[rr & 0x07] : 0;
425}
426
427static int PWM_RR_TO_REG(int val, int ix, int reg)
428{
429 int i;
430
431 for (i = 0; i < 7; i++) {
432 if (val > (PWM_RR[i] + PWM_RR[i + 1] + 1) / 2) {
433 break;
434 }
435 }
436
437 return (ix == 1) ? (reg & 0x8f) | (i << 4) : (reg & 0xf8) | i;
438}
439
440/* PWM ramp rate enable */
441static inline int PWM_RR_EN_FROM_REG(int reg, int ix)
442{
443 return PWM_RR_FROM_REG(reg, ix) ? 1 : 0;
444}
445
446static inline int PWM_RR_EN_TO_REG(int val, int ix, int reg)
447{
448 int en = (ix == 1) ? 0x80 : 0x08;
449
450 return val ? reg | en : reg & ~en;
451}
452
453/* PWM min/off
454 * The PWM min/off bits are part of the PMW ramp rate register 0 (see above for
455 * the register layout). */
456static inline int PWM_OFF_FROM_REG(int reg, int ix)
457{
458 return (reg >> (ix + 5)) & 0x01;
459}
460
461static inline int PWM_OFF_TO_REG(int val, int ix, int reg)
462{
463 return (reg & ~(1 << (ix + 5))) | ((val & 0x01) << (ix + 5));
464}
465
466/* ---------------------------------------------------------------------
467 * Device I/O access
468 * --------------------------------------------------------------------- */
469
470static u8 dme1737_read(struct i2c_client *client, u8 reg)
471{
472 s32 val = i2c_smbus_read_byte_data(client, reg);
473
474 if (val < 0) {
475 dev_warn(&client->dev, "Read from register 0x%02x failed! "
476 "Please report to the driver maintainer.\n", reg);
477 }
478
479 return val;
480}
481
482static s32 dme1737_write(struct i2c_client *client, u8 reg, u8 value)
483{
484 s32 res = i2c_smbus_write_byte_data(client, reg, value);
485
486 if (res < 0) {
487 dev_warn(&client->dev, "Write to register 0x%02x failed! "
488 "Please report to the driver maintainer.\n", reg);
489 }
490
491 return res;
492}
493
494static struct dme1737_data *dme1737_update_device(struct device *dev)
495{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700496 struct dme1737_data *data = dev_get_drvdata(dev);
497 struct i2c_client *client = &data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -0400498 int ix;
499 u8 lsb[5];
500
501 mutex_lock(&data->update_lock);
502
503 /* Enable a Vbat monitoring cycle every 10 mins */
504 if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) {
505 dme1737_write(client, DME1737_REG_CONFIG, dme1737_read(client,
506 DME1737_REG_CONFIG) | 0x10);
507 data->last_vbat = jiffies;
508 }
509
510 /* Sample register contents every 1 sec */
511 if (time_after(jiffies, data->last_update + HZ) || !data->valid) {
512 data->vid = dme1737_read(client, DME1737_REG_VID) & 0x3f;
513
514 /* In (voltage) registers */
515 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
516 /* Voltage inputs are stored as 16 bit values even
517 * though they have only 12 bits resolution. This is
518 * to make it consistent with the temp inputs. */
519 data->in[ix] = dme1737_read(client,
520 DME1737_REG_IN(ix)) << 8;
521 data->in_min[ix] = dme1737_read(client,
522 DME1737_REG_IN_MIN(ix));
523 data->in_max[ix] = dme1737_read(client,
524 DME1737_REG_IN_MAX(ix));
525 }
526
527 /* Temp registers */
528 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
529 /* Temp inputs are stored as 16 bit values even
530 * though they have only 12 bits resolution. This is
531 * to take advantage of implicit conversions between
532 * register values (2's complement) and temp values
533 * (signed decimal). */
534 data->temp[ix] = dme1737_read(client,
535 DME1737_REG_TEMP(ix)) << 8;
536 data->temp_min[ix] = dme1737_read(client,
537 DME1737_REG_TEMP_MIN(ix));
538 data->temp_max[ix] = dme1737_read(client,
539 DME1737_REG_TEMP_MAX(ix));
540 data->temp_offset[ix] = dme1737_read(client,
541 DME1737_REG_TEMP_OFFSET(ix));
542 }
543
544 /* In and temp LSB registers
545 * The LSBs are latched when the MSBs are read, so the order in
546 * which the registers are read (MSB first, then LSB) is
547 * important! */
548 for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) {
549 lsb[ix] = dme1737_read(client,
550 DME1737_REG_IN_TEMP_LSB(ix));
551 }
552 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
553 data->in[ix] |= (lsb[DME1737_REG_IN_LSB[ix]] <<
554 DME1737_REG_IN_LSB_SHL[ix]) & 0xf0;
555 }
556 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
557 data->temp[ix] |= (lsb[DME1737_REG_TEMP_LSB[ix]] <<
558 DME1737_REG_TEMP_LSB_SHL[ix]) & 0xf0;
559 }
560
561 /* Fan registers */
562 for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
563 /* Skip reading registers if optional fans are not
564 * present */
565 if (!(data->has_fan & (1 << ix))) {
566 continue;
567 }
568 data->fan[ix] = dme1737_read(client,
569 DME1737_REG_FAN(ix));
570 data->fan[ix] |= dme1737_read(client,
571 DME1737_REG_FAN(ix) + 1) << 8;
572 data->fan_min[ix] = dme1737_read(client,
573 DME1737_REG_FAN_MIN(ix));
574 data->fan_min[ix] |= dme1737_read(client,
575 DME1737_REG_FAN_MIN(ix) + 1) << 8;
576 data->fan_opt[ix] = dme1737_read(client,
577 DME1737_REG_FAN_OPT(ix));
578 /* fan_max exists only for fan[5-6] */
579 if (ix > 3) {
580 data->fan_max[ix - 4] = dme1737_read(client,
581 DME1737_REG_FAN_MAX(ix));
582 }
583 }
584
585 /* PWM registers */
586 for (ix = 0; ix < ARRAY_SIZE(data->pwm); ix++) {
587 /* Skip reading registers if optional PWMs are not
588 * present */
589 if (!(data->has_pwm & (1 << ix))) {
590 continue;
591 }
592 data->pwm[ix] = dme1737_read(client,
593 DME1737_REG_PWM(ix));
594 data->pwm_freq[ix] = dme1737_read(client,
595 DME1737_REG_PWM_FREQ(ix));
596 /* pwm_config and pwm_min exist only for pwm[1-3] */
597 if (ix < 3) {
598 data->pwm_config[ix] = dme1737_read(client,
599 DME1737_REG_PWM_CONFIG(ix));
600 data->pwm_min[ix] = dme1737_read(client,
601 DME1737_REG_PWM_MIN(ix));
602 }
603 }
604 for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) {
605 data->pwm_rr[ix] = dme1737_read(client,
606 DME1737_REG_PWM_RR(ix));
607 }
608
609 /* Thermal zone registers */
610 for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) {
611 data->zone_low[ix] = dme1737_read(client,
612 DME1737_REG_ZONE_LOW(ix));
613 data->zone_abs[ix] = dme1737_read(client,
614 DME1737_REG_ZONE_ABS(ix));
615 }
616 for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) {
617 data->zone_hyst[ix] = dme1737_read(client,
618 DME1737_REG_ZONE_HYST(ix));
619 }
620
621 /* Alarm registers */
622 data->alarms = dme1737_read(client,
623 DME1737_REG_ALARM1);
624 /* Bit 7 tells us if the other alarm registers are non-zero and
625 * therefore also need to be read */
626 if (data->alarms & 0x80) {
627 data->alarms |= dme1737_read(client,
628 DME1737_REG_ALARM2) << 8;
629 data->alarms |= dme1737_read(client,
630 DME1737_REG_ALARM3) << 16;
631 }
632
633 data->last_update = jiffies;
634 data->valid = 1;
635 }
636
637 mutex_unlock(&data->update_lock);
638
639 return data;
640}
641
642/* ---------------------------------------------------------------------
643 * Voltage sysfs attributes
644 * ix = [0-5]
645 * --------------------------------------------------------------------- */
646
647#define SYS_IN_INPUT 0
648#define SYS_IN_MIN 1
649#define SYS_IN_MAX 2
650#define SYS_IN_ALARM 3
651
652static ssize_t show_in(struct device *dev, struct device_attribute *attr,
653 char *buf)
654{
655 struct dme1737_data *data = dme1737_update_device(dev);
656 struct sensor_device_attribute_2
657 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
658 int ix = sensor_attr_2->index;
659 int fn = sensor_attr_2->nr;
660 int res;
661
662 switch (fn) {
663 case SYS_IN_INPUT:
664 res = IN_FROM_REG(data->in[ix], ix, 16);
665 break;
666 case SYS_IN_MIN:
667 res = IN_FROM_REG(data->in_min[ix], ix, 8);
668 break;
669 case SYS_IN_MAX:
670 res = IN_FROM_REG(data->in_max[ix], ix, 8);
671 break;
672 case SYS_IN_ALARM:
673 res = (data->alarms >> DME1737_BIT_ALARM_IN[ix]) & 0x01;
674 break;
675 default:
676 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700677 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400678 }
679
680 return sprintf(buf, "%d\n", res);
681}
682
683static ssize_t set_in(struct device *dev, struct device_attribute *attr,
684 const char *buf, size_t count)
685{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700686 struct dme1737_data *data = dev_get_drvdata(dev);
687 struct i2c_client *client = &data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -0400688 struct sensor_device_attribute_2
689 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
690 int ix = sensor_attr_2->index;
691 int fn = sensor_attr_2->nr;
692 long val = simple_strtol(buf, NULL, 10);
693
694 mutex_lock(&data->update_lock);
695 switch (fn) {
696 case SYS_IN_MIN:
697 data->in_min[ix] = IN_TO_REG(val, ix);
698 dme1737_write(client, DME1737_REG_IN_MIN(ix),
699 data->in_min[ix]);
700 break;
701 case SYS_IN_MAX:
702 data->in_max[ix] = IN_TO_REG(val, ix);
703 dme1737_write(client, DME1737_REG_IN_MAX(ix),
704 data->in_max[ix]);
705 break;
706 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700707 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400708 }
709 mutex_unlock(&data->update_lock);
710
711 return count;
712}
713
714/* ---------------------------------------------------------------------
715 * Temperature sysfs attributes
716 * ix = [0-2]
717 * --------------------------------------------------------------------- */
718
719#define SYS_TEMP_INPUT 0
720#define SYS_TEMP_MIN 1
721#define SYS_TEMP_MAX 2
722#define SYS_TEMP_OFFSET 3
723#define SYS_TEMP_ALARM 4
724#define SYS_TEMP_FAULT 5
725
726static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
727 char *buf)
728{
729 struct dme1737_data *data = dme1737_update_device(dev);
730 struct sensor_device_attribute_2
731 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
732 int ix = sensor_attr_2->index;
733 int fn = sensor_attr_2->nr;
734 int res;
735
736 switch (fn) {
737 case SYS_TEMP_INPUT:
738 res = TEMP_FROM_REG(data->temp[ix], 16);
739 break;
740 case SYS_TEMP_MIN:
741 res = TEMP_FROM_REG(data->temp_min[ix], 8);
742 break;
743 case SYS_TEMP_MAX:
744 res = TEMP_FROM_REG(data->temp_max[ix], 8);
745 break;
746 case SYS_TEMP_OFFSET:
747 res = TEMP_FROM_REG(data->temp_offset[ix], 8);
748 break;
749 case SYS_TEMP_ALARM:
750 res = (data->alarms >> DME1737_BIT_ALARM_TEMP[ix]) & 0x01;
751 break;
752 case SYS_TEMP_FAULT:
Juerg Haefligerc0f31402007-07-20 14:16:47 -0700753 res = (((u16)data->temp[ix] & 0xff00) == 0x8000);
Juerg Haefliger94319962007-06-09 10:11:16 -0400754 break;
755 default:
756 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700757 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400758 }
759
760 return sprintf(buf, "%d\n", res);
761}
762
763static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
764 const char *buf, size_t count)
765{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700766 struct dme1737_data *data = dev_get_drvdata(dev);
767 struct i2c_client *client = &data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -0400768 struct sensor_device_attribute_2
769 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
770 int ix = sensor_attr_2->index;
771 int fn = sensor_attr_2->nr;
772 long val = simple_strtol(buf, NULL, 10);
773
774 mutex_lock(&data->update_lock);
775 switch (fn) {
776 case SYS_TEMP_MIN:
777 data->temp_min[ix] = TEMP_TO_REG(val);
778 dme1737_write(client, DME1737_REG_TEMP_MIN(ix),
779 data->temp_min[ix]);
780 break;
781 case SYS_TEMP_MAX:
782 data->temp_max[ix] = TEMP_TO_REG(val);
783 dme1737_write(client, DME1737_REG_TEMP_MAX(ix),
784 data->temp_max[ix]);
785 break;
786 case SYS_TEMP_OFFSET:
787 data->temp_offset[ix] = TEMP_TO_REG(val);
788 dme1737_write(client, DME1737_REG_TEMP_OFFSET(ix),
789 data->temp_offset[ix]);
790 break;
791 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700792 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400793 }
794 mutex_unlock(&data->update_lock);
795
796 return count;
797}
798
799/* ---------------------------------------------------------------------
800 * Zone sysfs attributes
801 * ix = [0-2]
802 * --------------------------------------------------------------------- */
803
804#define SYS_ZONE_AUTO_CHANNELS_TEMP 0
805#define SYS_ZONE_AUTO_POINT1_TEMP_HYST 1
806#define SYS_ZONE_AUTO_POINT1_TEMP 2
807#define SYS_ZONE_AUTO_POINT2_TEMP 3
808#define SYS_ZONE_AUTO_POINT3_TEMP 4
809
810static ssize_t show_zone(struct device *dev, struct device_attribute *attr,
811 char *buf)
812{
813 struct dme1737_data *data = dme1737_update_device(dev);
814 struct sensor_device_attribute_2
815 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
816 int ix = sensor_attr_2->index;
817 int fn = sensor_attr_2->nr;
818 int res;
819
820 switch (fn) {
821 case SYS_ZONE_AUTO_CHANNELS_TEMP:
822 /* check config2 for non-standard temp-to-zone mapping */
823 if ((ix == 1) && (data->config2 & 0x02)) {
824 res = 4;
825 } else {
826 res = 1 << ix;
827 }
828 break;
829 case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
830 res = TEMP_FROM_REG(data->zone_low[ix], 8) -
831 TEMP_HYST_FROM_REG(data->zone_hyst[ix == 2], ix);
832 break;
833 case SYS_ZONE_AUTO_POINT1_TEMP:
834 res = TEMP_FROM_REG(data->zone_low[ix], 8);
835 break;
836 case SYS_ZONE_AUTO_POINT2_TEMP:
837 /* pwm_freq holds the temp range bits in the upper nibble */
838 res = TEMP_FROM_REG(data->zone_low[ix], 8) +
839 TEMP_RANGE_FROM_REG(data->pwm_freq[ix]);
840 break;
841 case SYS_ZONE_AUTO_POINT3_TEMP:
842 res = TEMP_FROM_REG(data->zone_abs[ix], 8);
843 break;
844 default:
845 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700846 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400847 }
848
849 return sprintf(buf, "%d\n", res);
850}
851
852static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
853 const char *buf, size_t count)
854{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700855 struct dme1737_data *data = dev_get_drvdata(dev);
856 struct i2c_client *client = &data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -0400857 struct sensor_device_attribute_2
858 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
859 int ix = sensor_attr_2->index;
860 int fn = sensor_attr_2->nr;
861 long val = simple_strtol(buf, NULL, 10);
862
863 mutex_lock(&data->update_lock);
864 switch (fn) {
865 case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
866 /* Refresh the cache */
867 data->zone_low[ix] = dme1737_read(client,
868 DME1737_REG_ZONE_LOW(ix));
869 /* Modify the temp hyst value */
870 data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG(
871 TEMP_FROM_REG(data->zone_low[ix], 8) -
872 val, ix, dme1737_read(client,
873 DME1737_REG_ZONE_HYST(ix == 2)));
874 dme1737_write(client, DME1737_REG_ZONE_HYST(ix == 2),
875 data->zone_hyst[ix == 2]);
876 break;
877 case SYS_ZONE_AUTO_POINT1_TEMP:
878 data->zone_low[ix] = TEMP_TO_REG(val);
879 dme1737_write(client, DME1737_REG_ZONE_LOW(ix),
880 data->zone_low[ix]);
881 break;
882 case SYS_ZONE_AUTO_POINT2_TEMP:
883 /* Refresh the cache */
884 data->zone_low[ix] = dme1737_read(client,
885 DME1737_REG_ZONE_LOW(ix));
886 /* Modify the temp range value (which is stored in the upper
887 * nibble of the pwm_freq register) */
888 data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val -
889 TEMP_FROM_REG(data->zone_low[ix], 8),
890 dme1737_read(client,
891 DME1737_REG_PWM_FREQ(ix)));
892 dme1737_write(client, DME1737_REG_PWM_FREQ(ix),
893 data->pwm_freq[ix]);
894 break;
895 case SYS_ZONE_AUTO_POINT3_TEMP:
896 data->zone_abs[ix] = TEMP_TO_REG(val);
897 dme1737_write(client, DME1737_REG_ZONE_ABS(ix),
898 data->zone_abs[ix]);
899 break;
900 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700901 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400902 }
903 mutex_unlock(&data->update_lock);
904
905 return count;
906}
907
908/* ---------------------------------------------------------------------
909 * Fan sysfs attributes
910 * ix = [0-5]
911 * --------------------------------------------------------------------- */
912
913#define SYS_FAN_INPUT 0
914#define SYS_FAN_MIN 1
915#define SYS_FAN_MAX 2
916#define SYS_FAN_ALARM 3
917#define SYS_FAN_TYPE 4
918
919static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
920 char *buf)
921{
922 struct dme1737_data *data = dme1737_update_device(dev);
923 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 int res;
928
929 switch (fn) {
930 case SYS_FAN_INPUT:
931 res = FAN_FROM_REG(data->fan[ix],
932 ix < 4 ? 0 :
933 FAN_TPC_FROM_REG(data->fan_opt[ix]));
934 break;
935 case SYS_FAN_MIN:
936 res = FAN_FROM_REG(data->fan_min[ix],
937 ix < 4 ? 0 :
938 FAN_TPC_FROM_REG(data->fan_opt[ix]));
939 break;
940 case SYS_FAN_MAX:
941 /* only valid for fan[5-6] */
942 res = FAN_MAX_FROM_REG(data->fan_max[ix - 4]);
943 break;
944 case SYS_FAN_ALARM:
945 res = (data->alarms >> DME1737_BIT_ALARM_FAN[ix]) & 0x01;
946 break;
947 case SYS_FAN_TYPE:
948 /* only valid for fan[1-4] */
949 res = FAN_TYPE_FROM_REG(data->fan_opt[ix]);
950 break;
951 default:
952 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700953 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400954 }
955
956 return sprintf(buf, "%d\n", res);
957}
958
959static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
960 const char *buf, size_t count)
961{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700962 struct dme1737_data *data = dev_get_drvdata(dev);
963 struct i2c_client *client = &data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -0400964 struct sensor_device_attribute_2
965 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
966 int ix = sensor_attr_2->index;
967 int fn = sensor_attr_2->nr;
968 long val = simple_strtol(buf, NULL, 10);
969
970 mutex_lock(&data->update_lock);
971 switch (fn) {
972 case SYS_FAN_MIN:
973 if (ix < 4) {
974 data->fan_min[ix] = FAN_TO_REG(val, 0);
975 } else {
976 /* Refresh the cache */
977 data->fan_opt[ix] = dme1737_read(client,
978 DME1737_REG_FAN_OPT(ix));
979 /* Modify the fan min value */
980 data->fan_min[ix] = FAN_TO_REG(val,
981 FAN_TPC_FROM_REG(data->fan_opt[ix]));
982 }
983 dme1737_write(client, DME1737_REG_FAN_MIN(ix),
984 data->fan_min[ix] & 0xff);
985 dme1737_write(client, DME1737_REG_FAN_MIN(ix) + 1,
986 data->fan_min[ix] >> 8);
987 break;
988 case SYS_FAN_MAX:
989 /* Only valid for fan[5-6] */
990 data->fan_max[ix - 4] = FAN_MAX_TO_REG(val);
991 dme1737_write(client, DME1737_REG_FAN_MAX(ix),
992 data->fan_max[ix - 4]);
993 break;
994 case SYS_FAN_TYPE:
995 /* Only valid for fan[1-4] */
996 if (!(val == 1 || val == 2 || val == 4)) {
997 count = -EINVAL;
998 dev_warn(&client->dev, "Fan type value %ld not "
999 "supported. Choose one of 1, 2, or 4.\n",
1000 val);
1001 goto exit;
1002 }
1003 data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(client,
1004 DME1737_REG_FAN_OPT(ix)));
1005 dme1737_write(client, DME1737_REG_FAN_OPT(ix),
1006 data->fan_opt[ix]);
1007 break;
1008 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001009 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001010 }
1011exit:
1012 mutex_unlock(&data->update_lock);
1013
1014 return count;
1015}
1016
1017/* ---------------------------------------------------------------------
1018 * PWM sysfs attributes
1019 * ix = [0-4]
1020 * --------------------------------------------------------------------- */
1021
1022#define SYS_PWM 0
1023#define SYS_PWM_FREQ 1
1024#define SYS_PWM_ENABLE 2
1025#define SYS_PWM_RAMP_RATE 3
1026#define SYS_PWM_AUTO_CHANNELS_ZONE 4
1027#define SYS_PWM_AUTO_PWM_MIN 5
1028#define SYS_PWM_AUTO_POINT1_PWM 6
1029#define SYS_PWM_AUTO_POINT2_PWM 7
1030
1031static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1032 char *buf)
1033{
1034 struct dme1737_data *data = dme1737_update_device(dev);
1035 struct sensor_device_attribute_2
1036 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1037 int ix = sensor_attr_2->index;
1038 int fn = sensor_attr_2->nr;
1039 int res;
1040
1041 switch (fn) {
1042 case SYS_PWM:
1043 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 0) {
1044 res = 255;
1045 } else {
1046 res = data->pwm[ix];
1047 }
1048 break;
1049 case SYS_PWM_FREQ:
1050 res = PWM_FREQ_FROM_REG(data->pwm_freq[ix]);
1051 break;
1052 case SYS_PWM_ENABLE:
1053 if (ix > 3) {
1054 res = 1; /* pwm[5-6] hard-wired to manual mode */
1055 } else {
1056 res = PWM_EN_FROM_REG(data->pwm_config[ix]);
1057 }
1058 break;
1059 case SYS_PWM_RAMP_RATE:
1060 /* Only valid for pwm[1-3] */
1061 res = PWM_RR_FROM_REG(data->pwm_rr[ix > 0], ix);
1062 break;
1063 case SYS_PWM_AUTO_CHANNELS_ZONE:
1064 /* Only valid for pwm[1-3] */
1065 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1066 res = PWM_ACZ_FROM_REG(data->pwm_config[ix]);
1067 } else {
1068 res = data->pwm_acz[ix];
1069 }
1070 break;
1071 case SYS_PWM_AUTO_PWM_MIN:
1072 /* Only valid for pwm[1-3] */
1073 if (PWM_OFF_FROM_REG(data->pwm_rr[0], ix)) {
1074 res = data->pwm_min[ix];
1075 } else {
1076 res = 0;
1077 }
1078 break;
1079 case SYS_PWM_AUTO_POINT1_PWM:
1080 /* Only valid for pwm[1-3] */
1081 res = data->pwm_min[ix];
1082 break;
1083 case SYS_PWM_AUTO_POINT2_PWM:
1084 /* Only valid for pwm[1-3] */
1085 res = 255; /* hard-wired */
1086 break;
1087 default:
1088 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001089 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001090 }
1091
1092 return sprintf(buf, "%d\n", res);
1093}
1094
1095static struct attribute *dme1737_attr_pwm[];
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001096static void dme1737_chmod_file(struct device*, struct attribute*, mode_t);
Juerg Haefliger94319962007-06-09 10:11:16 -04001097
1098static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1099 const char *buf, size_t count)
1100{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001101 struct dme1737_data *data = dev_get_drvdata(dev);
1102 struct i2c_client *client = &data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -04001103 struct sensor_device_attribute_2
1104 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1105 int ix = sensor_attr_2->index;
1106 int fn = sensor_attr_2->nr;
1107 long val = simple_strtol(buf, NULL, 10);
1108
1109 mutex_lock(&data->update_lock);
1110 switch (fn) {
1111 case SYS_PWM:
1112 data->pwm[ix] = SENSORS_LIMIT(val, 0, 255);
1113 dme1737_write(client, DME1737_REG_PWM(ix), data->pwm[ix]);
1114 break;
1115 case SYS_PWM_FREQ:
1116 data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(client,
1117 DME1737_REG_PWM_FREQ(ix)));
1118 dme1737_write(client, DME1737_REG_PWM_FREQ(ix),
1119 data->pwm_freq[ix]);
1120 break;
1121 case SYS_PWM_ENABLE:
1122 /* Only valid for pwm[1-3] */
1123 if (val < 0 || val > 2) {
1124 count = -EINVAL;
1125 dev_warn(&client->dev, "PWM enable %ld not "
1126 "supported. Choose one of 0, 1, or 2.\n",
1127 val);
1128 goto exit;
1129 }
1130 /* Refresh the cache */
1131 data->pwm_config[ix] = dme1737_read(client,
1132 DME1737_REG_PWM_CONFIG(ix));
1133 if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) {
1134 /* Bail out if no change */
1135 goto exit;
1136 }
1137 /* Do some housekeeping if we are currently in auto mode */
1138 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1139 /* Save the current zone channel assignment */
1140 data->pwm_acz[ix] = PWM_ACZ_FROM_REG(
1141 data->pwm_config[ix]);
1142 /* Save the current ramp rate state and disable it */
1143 data->pwm_rr[ix > 0] = dme1737_read(client,
1144 DME1737_REG_PWM_RR(ix > 0));
1145 data->pwm_rr_en &= ~(1 << ix);
1146 if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) {
1147 data->pwm_rr_en |= (1 << ix);
1148 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix,
1149 data->pwm_rr[ix > 0]);
1150 dme1737_write(client,
1151 DME1737_REG_PWM_RR(ix > 0),
1152 data->pwm_rr[ix > 0]);
1153 }
1154 }
1155 /* Set the new PWM mode */
1156 switch (val) {
1157 case 0:
1158 /* Change permissions of pwm[ix] to read-only */
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001159 dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
Juerg Haefliger94319962007-06-09 10:11:16 -04001160 S_IRUGO);
1161 /* Turn fan fully on */
1162 data->pwm_config[ix] = PWM_EN_TO_REG(0,
1163 data->pwm_config[ix]);
1164 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1165 data->pwm_config[ix]);
1166 break;
1167 case 1:
1168 /* Turn on manual mode */
1169 data->pwm_config[ix] = PWM_EN_TO_REG(1,
1170 data->pwm_config[ix]);
1171 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1172 data->pwm_config[ix]);
1173 /* Change permissions of pwm[ix] to read-writeable */
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001174 dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
Juerg Haefliger94319962007-06-09 10:11:16 -04001175 S_IRUGO | S_IWUSR);
1176 break;
1177 case 2:
1178 /* Change permissions of pwm[ix] to read-only */
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001179 dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
Juerg Haefliger94319962007-06-09 10:11:16 -04001180 S_IRUGO);
1181 /* Turn on auto mode using the saved zone channel
1182 * assignment */
1183 data->pwm_config[ix] = PWM_ACZ_TO_REG(
1184 data->pwm_acz[ix],
1185 data->pwm_config[ix]);
1186 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1187 data->pwm_config[ix]);
1188 /* Enable PWM ramp rate if previously enabled */
1189 if (data->pwm_rr_en & (1 << ix)) {
1190 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix,
1191 dme1737_read(client,
1192 DME1737_REG_PWM_RR(ix > 0)));
1193 dme1737_write(client,
1194 DME1737_REG_PWM_RR(ix > 0),
1195 data->pwm_rr[ix > 0]);
1196 }
1197 break;
1198 }
1199 break;
1200 case SYS_PWM_RAMP_RATE:
1201 /* Only valid for pwm[1-3] */
1202 /* Refresh the cache */
1203 data->pwm_config[ix] = dme1737_read(client,
1204 DME1737_REG_PWM_CONFIG(ix));
1205 data->pwm_rr[ix > 0] = dme1737_read(client,
1206 DME1737_REG_PWM_RR(ix > 0));
1207 /* Set the ramp rate value */
1208 if (val > 0) {
1209 data->pwm_rr[ix > 0] = PWM_RR_TO_REG(val, ix,
1210 data->pwm_rr[ix > 0]);
1211 }
1212 /* Enable/disable the feature only if the associated PWM
1213 * output is in automatic mode. */
1214 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1215 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix,
1216 data->pwm_rr[ix > 0]);
1217 }
1218 dme1737_write(client, DME1737_REG_PWM_RR(ix > 0),
1219 data->pwm_rr[ix > 0]);
1220 break;
1221 case SYS_PWM_AUTO_CHANNELS_ZONE:
1222 /* Only valid for pwm[1-3] */
1223 if (!(val == 1 || val == 2 || val == 4 ||
1224 val == 6 || val == 7)) {
1225 count = -EINVAL;
1226 dev_warn(&client->dev, "PWM auto channels zone %ld "
1227 "not supported. Choose one of 1, 2, 4, 6, "
1228 "or 7.\n", val);
1229 goto exit;
1230 }
1231 /* Refresh the cache */
1232 data->pwm_config[ix] = dme1737_read(client,
1233 DME1737_REG_PWM_CONFIG(ix));
1234 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1235 /* PWM is already in auto mode so update the temp
1236 * channel assignment */
1237 data->pwm_config[ix] = PWM_ACZ_TO_REG(val,
1238 data->pwm_config[ix]);
1239 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1240 data->pwm_config[ix]);
1241 } else {
1242 /* PWM is not in auto mode so we save the temp
1243 * channel assignment for later use */
1244 data->pwm_acz[ix] = val;
1245 }
1246 break;
1247 case SYS_PWM_AUTO_PWM_MIN:
1248 /* Only valid for pwm[1-3] */
1249 /* Refresh the cache */
1250 data->pwm_min[ix] = dme1737_read(client,
1251 DME1737_REG_PWM_MIN(ix));
1252 /* There are only 2 values supported for the auto_pwm_min
1253 * value: 0 or auto_point1_pwm. So if the temperature drops
1254 * below the auto_point1_temp_hyst value, the fan either turns
1255 * off or runs at auto_point1_pwm duty-cycle. */
1256 if (val > ((data->pwm_min[ix] + 1) / 2)) {
1257 data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix,
1258 dme1737_read(client,
1259 DME1737_REG_PWM_RR(0)));
Juerg Haefliger94319962007-06-09 10:11:16 -04001260 } else {
1261 data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix,
1262 dme1737_read(client,
1263 DME1737_REG_PWM_RR(0)));
Juerg Haefliger94319962007-06-09 10:11:16 -04001264 }
1265 dme1737_write(client, DME1737_REG_PWM_RR(0),
1266 data->pwm_rr[0]);
1267 break;
1268 case SYS_PWM_AUTO_POINT1_PWM:
1269 /* Only valid for pwm[1-3] */
1270 data->pwm_min[ix] = SENSORS_LIMIT(val, 0, 255);
1271 dme1737_write(client, DME1737_REG_PWM_MIN(ix),
1272 data->pwm_min[ix]);
1273 break;
1274 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001275 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001276 }
1277exit:
1278 mutex_unlock(&data->update_lock);
1279
1280 return count;
1281}
1282
1283/* ---------------------------------------------------------------------
1284 * Miscellaneous sysfs attributes
1285 * --------------------------------------------------------------------- */
1286
1287static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
1288 char *buf)
1289{
1290 struct i2c_client *client = to_i2c_client(dev);
1291 struct dme1737_data *data = i2c_get_clientdata(client);
1292
1293 return sprintf(buf, "%d\n", data->vrm);
1294}
1295
1296static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
1297 const char *buf, size_t count)
1298{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001299 struct dme1737_data *data = dev_get_drvdata(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04001300 long val = simple_strtol(buf, NULL, 10);
1301
1302 data->vrm = val;
1303 return count;
1304}
1305
1306static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
1307 char *buf)
1308{
1309 struct dme1737_data *data = dme1737_update_device(dev);
1310
1311 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1312}
1313
1314/* ---------------------------------------------------------------------
1315 * Sysfs device attribute defines and structs
1316 * --------------------------------------------------------------------- */
1317
1318/* Voltages 0-6 */
1319
1320#define SENSOR_DEVICE_ATTR_IN(ix) \
1321static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001322 show_in, NULL, SYS_IN_INPUT, ix); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001323static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001324 show_in, set_in, SYS_IN_MIN, ix); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001325static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001326 show_in, set_in, SYS_IN_MAX, ix); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001327static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001328 show_in, NULL, SYS_IN_ALARM, ix)
Juerg Haefliger94319962007-06-09 10:11:16 -04001329
1330SENSOR_DEVICE_ATTR_IN(0);
1331SENSOR_DEVICE_ATTR_IN(1);
1332SENSOR_DEVICE_ATTR_IN(2);
1333SENSOR_DEVICE_ATTR_IN(3);
1334SENSOR_DEVICE_ATTR_IN(4);
1335SENSOR_DEVICE_ATTR_IN(5);
1336SENSOR_DEVICE_ATTR_IN(6);
1337
1338/* Temperatures 1-3 */
1339
1340#define SENSOR_DEVICE_ATTR_TEMP(ix) \
1341static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001342 show_temp, NULL, SYS_TEMP_INPUT, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001343static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001344 show_temp, set_temp, SYS_TEMP_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001345static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001346 show_temp, set_temp, SYS_TEMP_MAX, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001347static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001348 show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001349static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001350 show_temp, NULL, SYS_TEMP_ALARM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001351static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001352 show_temp, NULL, SYS_TEMP_FAULT, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001353
1354SENSOR_DEVICE_ATTR_TEMP(1);
1355SENSOR_DEVICE_ATTR_TEMP(2);
1356SENSOR_DEVICE_ATTR_TEMP(3);
1357
1358/* Zones 1-3 */
1359
1360#define SENSOR_DEVICE_ATTR_ZONE(ix) \
1361static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001362 show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001363static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001364 show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001365static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001366 show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001367static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001368 show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001369static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001370 show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001371
1372SENSOR_DEVICE_ATTR_ZONE(1);
1373SENSOR_DEVICE_ATTR_ZONE(2);
1374SENSOR_DEVICE_ATTR_ZONE(3);
1375
1376/* Fans 1-4 */
1377
1378#define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \
1379static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001380 show_fan, NULL, SYS_FAN_INPUT, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001381static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001382 show_fan, set_fan, SYS_FAN_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001383static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001384 show_fan, NULL, SYS_FAN_ALARM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001385static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001386 show_fan, set_fan, SYS_FAN_TYPE, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001387
1388SENSOR_DEVICE_ATTR_FAN_1TO4(1);
1389SENSOR_DEVICE_ATTR_FAN_1TO4(2);
1390SENSOR_DEVICE_ATTR_FAN_1TO4(3);
1391SENSOR_DEVICE_ATTR_FAN_1TO4(4);
1392
1393/* Fans 5-6 */
1394
1395#define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \
1396static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001397 show_fan, NULL, SYS_FAN_INPUT, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001398static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001399 show_fan, set_fan, SYS_FAN_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001400static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001401 show_fan, NULL, SYS_FAN_ALARM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001402static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001403 show_fan, set_fan, SYS_FAN_MAX, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001404
1405SENSOR_DEVICE_ATTR_FAN_5TO6(5);
1406SENSOR_DEVICE_ATTR_FAN_5TO6(6);
1407
1408/* PWMs 1-3 */
1409
1410#define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \
1411static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001412 show_pwm, set_pwm, SYS_PWM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001413static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001414 show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001415static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001416 show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001417static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001418 show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001419static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001420 show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001421static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001422 show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001423static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001424 show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001425static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001426 show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001427
1428SENSOR_DEVICE_ATTR_PWM_1TO3(1);
1429SENSOR_DEVICE_ATTR_PWM_1TO3(2);
1430SENSOR_DEVICE_ATTR_PWM_1TO3(3);
1431
1432/* PWMs 5-6 */
1433
1434#define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \
1435static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001436 show_pwm, set_pwm, SYS_PWM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001437static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001438 show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001439static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001440 show_pwm, NULL, SYS_PWM_ENABLE, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001441
1442SENSOR_DEVICE_ATTR_PWM_5TO6(5);
1443SENSOR_DEVICE_ATTR_PWM_5TO6(6);
1444
1445/* Misc */
1446
1447static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
1448static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1449
1450#define SENSOR_DEV_ATTR_IN(ix) \
1451&sensor_dev_attr_in##ix##_input.dev_attr.attr, \
1452&sensor_dev_attr_in##ix##_min.dev_attr.attr, \
1453&sensor_dev_attr_in##ix##_max.dev_attr.attr, \
1454&sensor_dev_attr_in##ix##_alarm.dev_attr.attr
1455
1456/* These attributes are read-writeable only if the chip is *not* locked */
1457#define SENSOR_DEV_ATTR_TEMP_LOCK(ix) \
1458&sensor_dev_attr_temp##ix##_offset.dev_attr.attr
1459
1460#define SENSOR_DEV_ATTR_TEMP(ix) \
1461SENSOR_DEV_ATTR_TEMP_LOCK(ix), \
1462&sensor_dev_attr_temp##ix##_input.dev_attr.attr, \
1463&sensor_dev_attr_temp##ix##_min.dev_attr.attr, \
1464&sensor_dev_attr_temp##ix##_max.dev_attr.attr, \
1465&sensor_dev_attr_temp##ix##_alarm.dev_attr.attr, \
1466&sensor_dev_attr_temp##ix##_fault.dev_attr.attr
1467
1468/* These attributes are read-writeable only if the chip is *not* locked */
1469#define SENSOR_DEV_ATTR_ZONE_LOCK(ix) \
1470&sensor_dev_attr_zone##ix##_auto_point1_temp_hyst.dev_attr.attr, \
1471&sensor_dev_attr_zone##ix##_auto_point1_temp.dev_attr.attr, \
1472&sensor_dev_attr_zone##ix##_auto_point2_temp.dev_attr.attr, \
1473&sensor_dev_attr_zone##ix##_auto_point3_temp.dev_attr.attr
1474
1475#define SENSOR_DEV_ATTR_ZONE(ix) \
1476SENSOR_DEV_ATTR_ZONE_LOCK(ix), \
1477&sensor_dev_attr_zone##ix##_auto_channels_temp.dev_attr.attr
1478
1479#define SENSOR_DEV_ATTR_FAN_1TO4(ix) \
1480&sensor_dev_attr_fan##ix##_input.dev_attr.attr, \
1481&sensor_dev_attr_fan##ix##_min.dev_attr.attr, \
1482&sensor_dev_attr_fan##ix##_alarm.dev_attr.attr, \
1483&sensor_dev_attr_fan##ix##_type.dev_attr.attr
1484
1485#define SENSOR_DEV_ATTR_FAN_5TO6(ix) \
1486&sensor_dev_attr_fan##ix##_input.dev_attr.attr, \
1487&sensor_dev_attr_fan##ix##_min.dev_attr.attr, \
1488&sensor_dev_attr_fan##ix##_alarm.dev_attr.attr, \
1489&sensor_dev_attr_fan##ix##_max.dev_attr.attr
1490
1491/* These attributes are read-writeable only if the chip is *not* locked */
1492#define SENSOR_DEV_ATTR_PWM_1TO3_LOCK(ix) \
1493&sensor_dev_attr_pwm##ix##_freq.dev_attr.attr, \
1494&sensor_dev_attr_pwm##ix##_enable.dev_attr.attr, \
1495&sensor_dev_attr_pwm##ix##_ramp_rate.dev_attr.attr, \
1496&sensor_dev_attr_pwm##ix##_auto_channels_zone.dev_attr.attr, \
1497&sensor_dev_attr_pwm##ix##_auto_pwm_min.dev_attr.attr, \
1498&sensor_dev_attr_pwm##ix##_auto_point1_pwm.dev_attr.attr
1499
1500#define SENSOR_DEV_ATTR_PWM_1TO3(ix) \
1501SENSOR_DEV_ATTR_PWM_1TO3_LOCK(ix), \
1502&sensor_dev_attr_pwm##ix.dev_attr.attr, \
1503&sensor_dev_attr_pwm##ix##_auto_point2_pwm.dev_attr.attr
1504
1505/* These attributes are read-writeable only if the chip is *not* locked */
1506#define SENSOR_DEV_ATTR_PWM_5TO6_LOCK(ix) \
1507&sensor_dev_attr_pwm##ix.dev_attr.attr, \
1508&sensor_dev_attr_pwm##ix##_freq.dev_attr.attr
1509
1510#define SENSOR_DEV_ATTR_PWM_5TO6(ix) \
1511SENSOR_DEV_ATTR_PWM_5TO6_LOCK(ix), \
1512&sensor_dev_attr_pwm##ix##_enable.dev_attr.attr
1513
1514/* This struct holds all the attributes that are always present and need to be
1515 * created unconditionally. The attributes that need modification of their
1516 * permissions are created read-only and write permissions are added or removed
1517 * on the fly when required */
1518static struct attribute *dme1737_attr[] ={
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001519 /* Voltages */
1520 SENSOR_DEV_ATTR_IN(0),
1521 SENSOR_DEV_ATTR_IN(1),
1522 SENSOR_DEV_ATTR_IN(2),
1523 SENSOR_DEV_ATTR_IN(3),
1524 SENSOR_DEV_ATTR_IN(4),
1525 SENSOR_DEV_ATTR_IN(5),
1526 SENSOR_DEV_ATTR_IN(6),
1527 /* Temperatures */
1528 SENSOR_DEV_ATTR_TEMP(1),
1529 SENSOR_DEV_ATTR_TEMP(2),
1530 SENSOR_DEV_ATTR_TEMP(3),
1531 /* Zones */
1532 SENSOR_DEV_ATTR_ZONE(1),
1533 SENSOR_DEV_ATTR_ZONE(2),
1534 SENSOR_DEV_ATTR_ZONE(3),
1535 /* Misc */
1536 &dev_attr_vrm.attr,
1537 &dev_attr_cpu0_vid.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001538 NULL
1539};
1540
1541static const struct attribute_group dme1737_group = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001542 .attrs = dme1737_attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001543};
1544
1545/* The following structs hold the PWM attributes, some of which are optional.
1546 * Their creation depends on the chip configuration which is determined during
1547 * module load. */
1548static struct attribute *dme1737_attr_pwm1[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001549 SENSOR_DEV_ATTR_PWM_1TO3(1),
Juerg Haefliger94319962007-06-09 10:11:16 -04001550 NULL
1551};
1552static struct attribute *dme1737_attr_pwm2[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001553 SENSOR_DEV_ATTR_PWM_1TO3(2),
Juerg Haefliger94319962007-06-09 10:11:16 -04001554 NULL
1555};
1556static struct attribute *dme1737_attr_pwm3[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001557 SENSOR_DEV_ATTR_PWM_1TO3(3),
Juerg Haefliger94319962007-06-09 10:11:16 -04001558 NULL
1559};
1560static struct attribute *dme1737_attr_pwm5[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001561 SENSOR_DEV_ATTR_PWM_5TO6(5),
Juerg Haefliger94319962007-06-09 10:11:16 -04001562 NULL
1563};
1564static struct attribute *dme1737_attr_pwm6[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001565 SENSOR_DEV_ATTR_PWM_5TO6(6),
Juerg Haefliger94319962007-06-09 10:11:16 -04001566 NULL
1567};
1568
1569static const struct attribute_group dme1737_pwm_group[] = {
1570 { .attrs = dme1737_attr_pwm1 },
1571 { .attrs = dme1737_attr_pwm2 },
1572 { .attrs = dme1737_attr_pwm3 },
1573 { .attrs = NULL },
1574 { .attrs = dme1737_attr_pwm5 },
1575 { .attrs = dme1737_attr_pwm6 },
1576};
1577
1578/* The following structs hold the fan attributes, some of which are optional.
1579 * Their creation depends on the chip configuration which is determined during
1580 * module load. */
1581static struct attribute *dme1737_attr_fan1[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001582 SENSOR_DEV_ATTR_FAN_1TO4(1),
Juerg Haefliger94319962007-06-09 10:11:16 -04001583 NULL
1584};
1585static struct attribute *dme1737_attr_fan2[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001586 SENSOR_DEV_ATTR_FAN_1TO4(2),
Juerg Haefliger94319962007-06-09 10:11:16 -04001587 NULL
1588};
1589static struct attribute *dme1737_attr_fan3[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001590 SENSOR_DEV_ATTR_FAN_1TO4(3),
Juerg Haefliger94319962007-06-09 10:11:16 -04001591 NULL
1592};
1593static struct attribute *dme1737_attr_fan4[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001594 SENSOR_DEV_ATTR_FAN_1TO4(4),
Juerg Haefliger94319962007-06-09 10:11:16 -04001595 NULL
1596};
1597static struct attribute *dme1737_attr_fan5[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001598 SENSOR_DEV_ATTR_FAN_5TO6(5),
Juerg Haefliger94319962007-06-09 10:11:16 -04001599 NULL
1600};
1601static struct attribute *dme1737_attr_fan6[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001602 SENSOR_DEV_ATTR_FAN_5TO6(6),
Juerg Haefliger94319962007-06-09 10:11:16 -04001603 NULL
1604};
1605
1606static const struct attribute_group dme1737_fan_group[] = {
1607 { .attrs = dme1737_attr_fan1 },
1608 { .attrs = dme1737_attr_fan2 },
1609 { .attrs = dme1737_attr_fan3 },
1610 { .attrs = dme1737_attr_fan4 },
1611 { .attrs = dme1737_attr_fan5 },
1612 { .attrs = dme1737_attr_fan6 },
1613};
1614
1615/* The permissions of all of the following attributes are changed to read-
1616 * writeable if the chip is *not* locked. Otherwise they stay read-only. */
1617static struct attribute *dme1737_attr_lock[] = {
1618 /* Temperatures */
1619 SENSOR_DEV_ATTR_TEMP_LOCK(1),
1620 SENSOR_DEV_ATTR_TEMP_LOCK(2),
1621 SENSOR_DEV_ATTR_TEMP_LOCK(3),
1622 /* Zones */
1623 SENSOR_DEV_ATTR_ZONE_LOCK(1),
1624 SENSOR_DEV_ATTR_ZONE_LOCK(2),
1625 SENSOR_DEV_ATTR_ZONE_LOCK(3),
1626 NULL
1627};
1628
1629static const struct attribute_group dme1737_lock_group = {
1630 .attrs = dme1737_attr_lock,
1631};
1632
1633/* The permissions of the following PWM attributes are changed to read-
1634 * writeable if the chip is *not* locked and the respective PWM is available.
1635 * Otherwise they stay read-only. */
1636static struct attribute *dme1737_attr_pwm1_lock[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001637 SENSOR_DEV_ATTR_PWM_1TO3_LOCK(1),
Juerg Haefliger94319962007-06-09 10:11:16 -04001638 NULL
1639};
1640static struct attribute *dme1737_attr_pwm2_lock[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001641 SENSOR_DEV_ATTR_PWM_1TO3_LOCK(2),
Juerg Haefliger94319962007-06-09 10:11:16 -04001642 NULL
1643};
1644static struct attribute *dme1737_attr_pwm3_lock[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001645 SENSOR_DEV_ATTR_PWM_1TO3_LOCK(3),
Juerg Haefliger94319962007-06-09 10:11:16 -04001646 NULL
1647};
1648static struct attribute *dme1737_attr_pwm5_lock[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001649 SENSOR_DEV_ATTR_PWM_5TO6_LOCK(5),
Juerg Haefliger94319962007-06-09 10:11:16 -04001650 NULL
1651};
1652static struct attribute *dme1737_attr_pwm6_lock[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001653 SENSOR_DEV_ATTR_PWM_5TO6_LOCK(6),
Juerg Haefliger94319962007-06-09 10:11:16 -04001654 NULL
1655};
1656
1657static const struct attribute_group dme1737_pwm_lock_group[] = {
1658 { .attrs = dme1737_attr_pwm1_lock },
1659 { .attrs = dme1737_attr_pwm2_lock },
1660 { .attrs = dme1737_attr_pwm3_lock },
1661 { .attrs = NULL },
1662 { .attrs = dme1737_attr_pwm5_lock },
1663 { .attrs = dme1737_attr_pwm6_lock },
1664};
1665
1666/* Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the
1667 * chip is not locked. Otherwise they are read-only. */
1668static struct attribute *dme1737_attr_pwm[] = {
1669 &sensor_dev_attr_pwm1.dev_attr.attr,
1670 &sensor_dev_attr_pwm2.dev_attr.attr,
1671 &sensor_dev_attr_pwm3.dev_attr.attr,
1672};
1673
1674/* ---------------------------------------------------------------------
1675 * Super-IO functions
1676 * --------------------------------------------------------------------- */
1677
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001678static inline void dme1737_sio_enter(int sio_cip)
1679{
1680 outb(0x55, sio_cip);
1681}
1682
1683static inline void dme1737_sio_exit(int sio_cip)
1684{
1685 outb(0xaa, sio_cip);
1686}
1687
Juerg Haefliger94319962007-06-09 10:11:16 -04001688static inline int dme1737_sio_inb(int sio_cip, int reg)
1689{
1690 outb(reg, sio_cip);
1691 return inb(sio_cip + 1);
1692}
1693
1694static inline void dme1737_sio_outb(int sio_cip, int reg, int val)
1695{
1696 outb(reg, sio_cip);
1697 outb(val, sio_cip + 1);
1698}
1699
Juerg Haefliger94319962007-06-09 10:11:16 -04001700/* ---------------------------------------------------------------------
1701 * Device detection, registration and initialization
1702 * --------------------------------------------------------------------- */
1703
Juerg Haefliger67e2f322007-10-01 21:20:28 -07001704static int dme1737_i2c_get_features(int, struct dme1737_data*);
Juerg Haefliger94319962007-06-09 10:11:16 -04001705
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001706static void dme1737_chmod_file(struct device *dev,
Juerg Haefliger94319962007-06-09 10:11:16 -04001707 struct attribute *attr, mode_t mode)
Juerg Haefliger94319962007-06-09 10:11:16 -04001708{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001709 if (sysfs_chmod_file(&dev->kobj, attr, mode)) {
1710 dev_warn(dev, "Failed to change permissions of %s.\n",
Juerg Haefliger94319962007-06-09 10:11:16 -04001711 attr->name);
1712 }
1713}
1714
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001715static void dme1737_chmod_group(struct device *dev,
Juerg Haefliger94319962007-06-09 10:11:16 -04001716 const struct attribute_group *group,
1717 mode_t mode)
1718{
1719 struct attribute **attr;
1720
1721 for (attr = group->attrs; *attr; attr++) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001722 dme1737_chmod_file(dev, *attr, mode);
Juerg Haefliger94319962007-06-09 10:11:16 -04001723 }
1724}
1725
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001726static void dme1737_remove_files(struct device *dev)
Juerg Haefliger94319962007-06-09 10:11:16 -04001727{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001728 struct dme1737_data *data = dev_get_drvdata(dev);
1729 int ix;
1730
1731 for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
1732 if (data->has_fan & (1 << ix)) {
1733 sysfs_remove_group(&dev->kobj,
1734 &dme1737_fan_group[ix]);
1735 }
1736 }
1737
1738 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
1739 if (data->has_pwm & (1 << ix)) {
1740 sysfs_remove_group(&dev->kobj,
1741 &dme1737_pwm_group[ix]);
1742 }
1743 }
1744
1745 sysfs_remove_group(&dev->kobj, &dme1737_group);
1746}
1747
1748static int dme1737_create_files(struct device *dev)
1749{
1750 struct dme1737_data *data = dev_get_drvdata(dev);
1751 int err, ix;
1752
1753 /* Create standard sysfs attributes */
1754 if ((err = sysfs_create_group(&dev->kobj, &dme1737_group))) {
1755 goto exit;
1756 }
1757
1758 /* Create fan sysfs attributes */
1759 for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
1760 if (data->has_fan & (1 << ix)) {
1761 if ((err = sysfs_create_group(&dev->kobj,
1762 &dme1737_fan_group[ix]))) {
1763 goto exit_remove;
1764 }
1765 }
1766 }
1767
1768 /* Create PWM sysfs attributes */
1769 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
1770 if (data->has_pwm & (1 << ix)) {
1771 if ((err = sysfs_create_group(&dev->kobj,
1772 &dme1737_pwm_group[ix]))) {
1773 goto exit_remove;
1774 }
1775 }
1776 }
1777
1778 /* Inform if the device is locked. Otherwise change the permissions of
1779 * selected attributes from read-only to read-writeable. */
1780 if (data->config & 0x02) {
1781 dev_info(dev, "Device is locked. Some attributes "
1782 "will be read-only.\n");
1783 } else {
1784 /* Change permissions of standard attributes */
1785 dme1737_chmod_group(dev, &dme1737_lock_group,
1786 S_IRUGO | S_IWUSR);
1787
1788 /* Change permissions of PWM attributes */
1789 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_lock_group); ix++) {
1790 if (data->has_pwm & (1 << ix)) {
1791 dme1737_chmod_group(dev,
1792 &dme1737_pwm_lock_group[ix],
1793 S_IRUGO | S_IWUSR);
1794 }
1795 }
1796
1797 /* Change permissions of pwm[1-3] if in manual mode */
1798 for (ix = 0; ix < 3; ix++) {
1799 if ((data->has_pwm & (1 << ix)) &&
1800 (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) {
1801 dme1737_chmod_file(dev,
1802 dme1737_attr_pwm[ix],
1803 S_IRUGO | S_IWUSR);
1804 }
1805 }
1806 }
1807
1808 return 0;
1809
1810exit_remove:
1811 dme1737_remove_files(dev);
1812exit:
1813 return err;
1814}
1815
1816static int dme1737_init_device(struct device *dev)
1817{
1818 struct dme1737_data *data = dev_get_drvdata(dev);
1819 struct i2c_client *client = &data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -04001820 int ix;
1821 u8 reg;
1822
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001823 data->config = dme1737_read(client, DME1737_REG_CONFIG);
1824 /* Inform if part is not monitoring/started */
1825 if (!(data->config & 0x01)) {
1826 if (!force_start) {
1827 dev_err(dev, "Device is not monitoring. "
1828 "Use the force_start load parameter to "
1829 "override.\n");
1830 return -EFAULT;
1831 }
Juerg Haefliger94319962007-06-09 10:11:16 -04001832
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001833 /* Force monitoring */
1834 data->config |= 0x01;
1835 dme1737_write(client, DME1737_REG_CONFIG, data->config);
1836 }
Juerg Haefliger94319962007-06-09 10:11:16 -04001837 /* Inform if part is not ready */
1838 if (!(data->config & 0x04)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001839 dev_err(dev, "Device is not ready.\n");
Juerg Haefliger94319962007-06-09 10:11:16 -04001840 return -EFAULT;
1841 }
1842
1843 data->config2 = dme1737_read(client, DME1737_REG_CONFIG2);
1844 /* Check if optional fan3 input is enabled */
1845 if (data->config2 & 0x04) {
1846 data->has_fan |= (1 << 2);
1847 }
1848
1849 /* Fan4 and pwm3 are only available if the client's I2C address
1850 * is the default 0x2e. Otherwise the I/Os associated with these
1851 * functions are used for addr enable/select. */
1852 if (client->addr == 0x2e) {
1853 data->has_fan |= (1 << 3);
1854 data->has_pwm |= (1 << 2);
1855 }
1856
1857 /* Determine if the optional fan[5-6] and/or pwm[5-6] are enabled.
1858 * For this, we need to query the runtime registers through the
1859 * Super-IO LPC interface. Try both config ports 0x2e and 0x4e. */
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001860 if (dme1737_i2c_get_features(0x2e, data) &&
1861 dme1737_i2c_get_features(0x4e, data)) {
1862 dev_warn(dev, "Failed to query Super-IO for optional "
Juerg Haefliger94319962007-06-09 10:11:16 -04001863 "features.\n");
1864 }
1865
1866 /* Fan1, fan2, pwm1, and pwm2 are always present */
1867 data->has_fan |= 0x03;
1868 data->has_pwm |= 0x03;
1869
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001870 dev_info(dev, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, "
Juerg Haefliger94319962007-06-09 10:11:16 -04001871 "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n",
1872 (data->has_pwm & (1 << 2)) ? "yes" : "no",
1873 (data->has_pwm & (1 << 4)) ? "yes" : "no",
1874 (data->has_pwm & (1 << 5)) ? "yes" : "no",
1875 (data->has_fan & (1 << 2)) ? "yes" : "no",
1876 (data->has_fan & (1 << 3)) ? "yes" : "no",
1877 (data->has_fan & (1 << 4)) ? "yes" : "no",
1878 (data->has_fan & (1 << 5)) ? "yes" : "no");
1879
1880 reg = dme1737_read(client, DME1737_REG_TACH_PWM);
1881 /* Inform if fan-to-pwm mapping differs from the default */
1882 if (reg != 0xa4) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001883 dev_warn(dev, "Non-standard fan to pwm mapping: "
Juerg Haefliger94319962007-06-09 10:11:16 -04001884 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, "
1885 "fan4->pwm%d. Please report to the driver "
1886 "maintainer.\n",
1887 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
1888 ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1);
1889 }
1890
1891 /* Switch pwm[1-3] to manual mode if they are currently disabled and
1892 * set the duty-cycles to 0% (which is identical to the PWMs being
1893 * disabled). */
1894 if (!(data->config & 0x02)) {
1895 for (ix = 0; ix < 3; ix++) {
1896 data->pwm_config[ix] = dme1737_read(client,
1897 DME1737_REG_PWM_CONFIG(ix));
1898 if ((data->has_pwm & (1 << ix)) &&
1899 (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001900 dev_info(dev, "Switching pwm%d to "
Juerg Haefliger94319962007-06-09 10:11:16 -04001901 "manual mode.\n", ix + 1);
1902 data->pwm_config[ix] = PWM_EN_TO_REG(1,
1903 data->pwm_config[ix]);
1904 dme1737_write(client, DME1737_REG_PWM(ix), 0);
1905 dme1737_write(client,
1906 DME1737_REG_PWM_CONFIG(ix),
1907 data->pwm_config[ix]);
1908 }
1909 }
1910 }
1911
1912 /* Initialize the default PWM auto channels zone (acz) assignments */
1913 data->pwm_acz[0] = 1; /* pwm1 -> zone1 */
1914 data->pwm_acz[1] = 2; /* pwm2 -> zone2 */
1915 data->pwm_acz[2] = 4; /* pwm3 -> zone3 */
1916
1917 /* Set VRM */
1918 data->vrm = vid_which_vrm();
1919
1920 return 0;
1921}
1922
Juerg Haefliger67e2f322007-10-01 21:20:28 -07001923/* ---------------------------------------------------------------------
1924 * I2C device detection and registration
1925 * --------------------------------------------------------------------- */
1926
1927static struct i2c_driver dme1737_i2c_driver;
1928
1929static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data)
1930{
Juerg Haefliger94319962007-06-09 10:11:16 -04001931 int err = 0, reg;
1932 u16 addr;
1933
Juerg Haefliger67e2f322007-10-01 21:20:28 -07001934 dme1737_sio_enter(sio_cip);
Juerg Haefliger94319962007-06-09 10:11:16 -04001935
1936 /* Check device ID
1937 * The DME1737 can return either 0x78 or 0x77 as its device ID. */
1938 reg = dme1737_sio_inb(sio_cip, 0x20);
1939 if (!(reg == 0x77 || reg == 0x78)) {
1940 err = -ENODEV;
1941 goto exit;
1942 }
1943
1944 /* Select logical device A (runtime registers) */
1945 dme1737_sio_outb(sio_cip, 0x07, 0x0a);
1946
1947 /* Get the base address of the runtime registers */
1948 if (!(addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
1949 dme1737_sio_inb(sio_cip, 0x61))) {
1950 err = -ENODEV;
1951 goto exit;
1952 }
1953
1954 /* Read the runtime registers to determine which optional features
1955 * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set
1956 * to '10' if the respective feature is enabled. */
1957 if ((inb(addr + 0x43) & 0x0c) == 0x08) { /* fan6 */
1958 data->has_fan |= (1 << 5);
1959 }
1960 if ((inb(addr + 0x44) & 0x0c) == 0x08) { /* pwm6 */
1961 data->has_pwm |= (1 << 5);
1962 }
1963 if ((inb(addr + 0x45) & 0x0c) == 0x08) { /* fan5 */
1964 data->has_fan |= (1 << 4);
1965 }
1966 if ((inb(addr + 0x46) & 0x0c) == 0x08) { /* pwm5 */
1967 data->has_pwm |= (1 << 4);
1968 }
1969
1970exit:
Juerg Haefliger67e2f322007-10-01 21:20:28 -07001971 dme1737_sio_exit(sio_cip);
Juerg Haefliger94319962007-06-09 10:11:16 -04001972
1973 return err;
1974}
1975
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001976static int dme1737_i2c_detect(struct i2c_adapter *adapter, int address,
1977 int kind)
Juerg Haefliger94319962007-06-09 10:11:16 -04001978{
1979 u8 company, verstep = 0;
1980 struct i2c_client *client;
1981 struct dme1737_data *data;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001982 struct device *dev;
1983 int err = 0;
Juerg Haefliger94319962007-06-09 10:11:16 -04001984 const char *name;
1985
1986 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1987 goto exit;
1988 }
1989
1990 if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
1991 err = -ENOMEM;
1992 goto exit;
1993 }
1994
1995 client = &data->client;
1996 i2c_set_clientdata(client, data);
1997 client->addr = address;
1998 client->adapter = adapter;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001999 client->driver = &dme1737_i2c_driver;
2000 dev = &client->dev;
Juerg Haefliger94319962007-06-09 10:11:16 -04002001
2002 /* A negative kind means that the driver was loaded with no force
2003 * parameter (default), so we must identify the chip. */
2004 if (kind < 0) {
2005 company = dme1737_read(client, DME1737_REG_COMPANY);
2006 verstep = dme1737_read(client, DME1737_REG_VERSTEP);
2007
2008 if (!((company == DME1737_COMPANY_SMSC) &&
2009 ((verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP))) {
2010 err = -ENODEV;
2011 goto exit_kfree;
2012 }
2013 }
2014
2015 kind = dme1737;
2016 name = "dme1737";
2017
2018 /* Fill in the remaining client fields and put it into the global
2019 * list */
2020 strlcpy(client->name, name, I2C_NAME_SIZE);
2021 mutex_init(&data->update_lock);
2022
2023 /* Tell the I2C layer a new client has arrived */
2024 if ((err = i2c_attach_client(client))) {
2025 goto exit_kfree;
2026 }
2027
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002028 dev_info(dev, "Found a DME1737 chip at 0x%02x (rev 0x%02x).\n",
2029 client->addr, verstep);
2030
Juerg Haefliger94319962007-06-09 10:11:16 -04002031 /* Initialize the DME1737 chip */
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002032 if ((err = dme1737_init_device(dev))) {
2033 dev_err(dev, "Failed to initialize device.\n");
Juerg Haefliger94319962007-06-09 10:11:16 -04002034 goto exit_detach;
2035 }
2036
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002037 /* Create sysfs files */
2038 if ((err = dme1737_create_files(dev))) {
2039 dev_err(dev, "Failed to create sysfs files.\n");
2040 goto exit_detach;
Juerg Haefliger94319962007-06-09 10:11:16 -04002041 }
2042
2043 /* Register device */
Mark M. Hoffman62ee3e12007-10-10 22:32:50 -04002044 data->hwmon_dev = hwmon_device_register(dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07002045 if (IS_ERR(data->hwmon_dev)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002046 dev_err(dev, "Failed to register device.\n");
Tony Jones1beeffe2007-08-20 13:46:20 -07002047 err = PTR_ERR(data->hwmon_dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04002048 goto exit_remove;
2049 }
2050
Juerg Haefliger94319962007-06-09 10:11:16 -04002051 return 0;
2052
2053exit_remove:
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002054 dme1737_remove_files(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04002055exit_detach:
2056 i2c_detach_client(client);
2057exit_kfree:
2058 kfree(data);
2059exit:
2060 return err;
2061}
2062
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002063static int dme1737_i2c_attach_adapter(struct i2c_adapter *adapter)
Juerg Haefliger94319962007-06-09 10:11:16 -04002064{
2065 if (!(adapter->class & I2C_CLASS_HWMON)) {
2066 return 0;
2067 }
2068
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002069 return i2c_probe(adapter, &addr_data, dme1737_i2c_detect);
Juerg Haefliger94319962007-06-09 10:11:16 -04002070}
2071
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002072static int dme1737_i2c_detach_client(struct i2c_client *client)
Juerg Haefliger94319962007-06-09 10:11:16 -04002073{
2074 struct dme1737_data *data = i2c_get_clientdata(client);
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002075 int err;
Juerg Haefliger94319962007-06-09 10:11:16 -04002076
Tony Jones1beeffe2007-08-20 13:46:20 -07002077 hwmon_device_unregister(data->hwmon_dev);
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002078 dme1737_remove_files(&client->dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04002079
2080 if ((err = i2c_detach_client(client))) {
2081 return err;
2082 }
2083
2084 kfree(data);
2085 return 0;
2086}
2087
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002088static struct i2c_driver dme1737_i2c_driver = {
Juerg Haefliger94319962007-06-09 10:11:16 -04002089 .driver = {
2090 .name = "dme1737",
2091 },
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002092 .attach_adapter = dme1737_i2c_attach_adapter,
2093 .detach_client = dme1737_i2c_detach_client,
Juerg Haefliger94319962007-06-09 10:11:16 -04002094};
2095
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002096/* ---------------------------------------------------------------------
2097 * Module initialization and cleanup
2098 * --------------------------------------------------------------------- */
2099
Juerg Haefliger94319962007-06-09 10:11:16 -04002100static int __init dme1737_init(void)
2101{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002102 return i2c_add_driver(&dme1737_i2c_driver);
Juerg Haefliger94319962007-06-09 10:11:16 -04002103}
2104
2105static void __exit dme1737_exit(void)
2106{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002107 i2c_del_driver(&dme1737_i2c_driver);
Juerg Haefliger94319962007-06-09 10:11:16 -04002108}
2109
2110MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
2111MODULE_DESCRIPTION("DME1737 sensors");
2112MODULE_LICENSE("GPL");
2113
2114module_init(dme1737_init);
2115module_exit(dme1737_exit);