Merge branch 'master' of /pub/scm/linux/kernel/git/torvalds/linux-2.6
[pandora-kernel.git] / drivers / hwmon / fschmd.c
1 /* fschmd.c
2  *
3  * Copyright (C) 2007 Hans de Goede <j.w.r.degoede@hhs.nl>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 /*
21  *  Merged Fujitsu Siemens hwmon driver, supporting the Poseidon, Hermes,
22  *  Scylla, Heracles and Heimdall chips
23  *
24  *  Based on the original 2.4 fscscy, 2.6 fscpos, 2.6 fscher and 2.6
25  *  (candidate) fschmd drivers:
26  *  Copyright (C) 2006 Thilo Cestonaro
27  *                      <thilo.cestonaro.external@fujitsu-siemens.com>
28  *  Copyright (C) 2004, 2005 Stefan Ott <stefan@desire.ch>
29  *  Copyright (C) 2003, 2004 Reinhard Nissl <rnissl@gmx.de>
30  *  Copyright (c) 2001 Martin Knoblauch <mkn@teraport.de, knobi@knobisoft.de>
31  *  Copyright (C) 2000 Hermann Jung <hej@odn.de>
32  */
33
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/jiffies.h>
38 #include <linux/i2c.h>
39 #include <linux/hwmon.h>
40 #include <linux/hwmon-sysfs.h>
41 #include <linux/err.h>
42 #include <linux/mutex.h>
43 #include <linux/sysfs.h>
44 #include <linux/dmi.h>
45
46 /* Addresses to scan */
47 static const unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
48
49 /* Insmod parameters */
50 I2C_CLIENT_INSMOD_5(fscpos, fscher, fscscy, fschrc, fschmd);
51
52 /*
53  * The FSCHMD registers and other defines
54  */
55
56 /* chip identification */
57 #define FSCHMD_REG_IDENT_0              0x00
58 #define FSCHMD_REG_IDENT_1              0x01
59 #define FSCHMD_REG_IDENT_2              0x02
60 #define FSCHMD_REG_REVISION             0x03
61
62 /* global control and status */
63 #define FSCHMD_REG_EVENT_STATE          0x04
64 #define FSCHMD_REG_CONTROL              0x05
65
66 #define FSCHMD_CONTROL_ALERT_LED_MASK   0x01
67
68 /* watchdog (support to be implemented) */
69 #define FSCHMD_REG_WDOG_PRESET          0x28
70 #define FSCHMD_REG_WDOG_STATE           0x23
71 #define FSCHMD_REG_WDOG_CONTROL         0x21
72
73 /* voltages, weird order is to keep the same order as the old drivers */
74 static const u8 FSCHMD_REG_VOLT[3] = { 0x45, 0x42, 0x48 };
75
76 /* minimum pwm at which the fan is driven (pwm can by increased depending on
77    the temp. Notice that for the scy some fans share there minimum speed.
78    Also notice that with the scy the sensor order is different then with the
79    other chips, this order was in the 2.4 driver and kept for consistency. */
80 static const u8 FSCHMD_REG_FAN_MIN[5][6] = {
81         { 0x55, 0x65 },                                 /* pos */
82         { 0x55, 0x65, 0xb5 },                           /* her */
83         { 0x65, 0x65, 0x55, 0xa5, 0x55, 0xa5 },         /* scy */
84         { 0x55, 0x65, 0xa5, 0xb5 },                     /* hrc */
85         { 0x55, 0x65, 0xa5, 0xb5, 0xc5 },               /* hmd */
86 };
87
88 /* actual fan speed */
89 static const u8 FSCHMD_REG_FAN_ACT[5][6] = {
90         { 0x0e, 0x6b, 0xab },                           /* pos */
91         { 0x0e, 0x6b, 0xbb },                           /* her */
92         { 0x6b, 0x6c, 0x0e, 0xab, 0x5c, 0xbb },         /* scy */
93         { 0x0e, 0x6b, 0xab, 0xbb },                     /* hrc */
94         { 0x5b, 0x6b, 0xab, 0xbb, 0xcb },               /* hmd */
95 };
96
97 /* fan status registers */
98 static const u8 FSCHMD_REG_FAN_STATE[5][6] = {
99         { 0x0d, 0x62, 0xa2 },                           /* pos */
100         { 0x0d, 0x62, 0xb2 },                           /* her */
101         { 0x62, 0x61, 0x0d, 0xa2, 0x52, 0xb2 },         /* scy */
102         { 0x0d, 0x62, 0xa2, 0xb2 },                     /* hrc */
103         { 0x52, 0x62, 0xa2, 0xb2, 0xc2 },               /* hmd */
104 };
105
106 /* fan ripple / divider registers */
107 static const u8 FSCHMD_REG_FAN_RIPPLE[5][6] = {
108         { 0x0f, 0x6f, 0xaf },                           /* pos */
109         { 0x0f, 0x6f, 0xbf },                           /* her */
110         { 0x6f, 0x6f, 0x0f, 0xaf, 0x0f, 0xbf },         /* scy */
111         { 0x0f, 0x6f, 0xaf, 0xbf },                     /* hrc */
112         { 0x5f, 0x6f, 0xaf, 0xbf, 0xcf },               /* hmd */
113 };
114
115 static const int FSCHMD_NO_FAN_SENSORS[5] = { 3, 3, 6, 4, 5 };
116
117 /* Fan status register bitmasks */
118 #define FSCHMD_FAN_ALARM_MASK           0x04 /* called fault by FSC! */
119 #define FSCHMD_FAN_NOT_PRESENT_MASK     0x08 /* not documented */
120
121
122 /* actual temperature registers */
123 static const u8 FSCHMD_REG_TEMP_ACT[5][5] = {
124         { 0x64, 0x32, 0x35 },                           /* pos */
125         { 0x64, 0x32, 0x35 },                           /* her */
126         { 0x64, 0xD0, 0x32, 0x35 },                     /* scy */
127         { 0x64, 0x32, 0x35 },                           /* hrc */
128         { 0x70, 0x80, 0x90, 0xd0, 0xe0 },               /* hmd */
129 };
130
131 /* temperature state registers */
132 static const u8 FSCHMD_REG_TEMP_STATE[5][5] = {
133         { 0x71, 0x81, 0x91 },                           /* pos */
134         { 0x71, 0x81, 0x91 },                           /* her */
135         { 0x71, 0xd1, 0x81, 0x91 },                     /* scy */
136         { 0x71, 0x81, 0x91 },                           /* hrc */
137         { 0x71, 0x81, 0x91, 0xd1, 0xe1 },               /* hmd */
138 };
139
140 /* temperature high limit registers, FSC does not document these. Proven to be
141    there with field testing on the fscher and fschrc, already supported / used
142    in the fscscy 2.4 driver. FSC has confirmed that the fschmd has registers
143    at these addresses, but doesn't want to confirm they are the same as with
144    the fscher?? */
145 static const u8 FSCHMD_REG_TEMP_LIMIT[5][5] = {
146         { 0, 0, 0 },                                    /* pos */
147         { 0x76, 0x86, 0x96 },                           /* her */
148         { 0x76, 0xd6, 0x86, 0x96 },                     /* scy */
149         { 0x76, 0x86, 0x96 },                           /* hrc */
150         { 0x76, 0x86, 0x96, 0xd6, 0xe6 },               /* hmd */
151 };
152
153 /* These were found through experimenting with an fscher, currently they are
154    not used, but we keep them around for future reference.
155 static const u8 FSCHER_REG_TEMP_AUTOP1[] =      { 0x73, 0x83, 0x93 };
156 static const u8 FSCHER_REG_TEMP_AUTOP2[] =      { 0x75, 0x85, 0x95 }; */
157
158 static const int FSCHMD_NO_TEMP_SENSORS[5] = { 3, 3, 4, 3, 5 };
159
160 /* temp status register bitmasks */
161 #define FSCHMD_TEMP_WORKING_MASK        0x01
162 #define FSCHMD_TEMP_ALERT_MASK          0x02
163 /* there only really is an alarm if the sensor is working and alert == 1 */
164 #define FSCHMD_TEMP_ALARM_MASK \
165         (FSCHMD_TEMP_WORKING_MASK | FSCHMD_TEMP_ALERT_MASK)
166
167 /* our driver name */
168 #define FSCHMD_NAME "fschmd"
169
170 /*
171  * Functions declarations
172  */
173
174 static int fschmd_attach_adapter(struct i2c_adapter *adapter);
175 static int fschmd_detach_client(struct i2c_client *client);
176 static struct fschmd_data *fschmd_update_device(struct device *dev);
177
178 /*
179  * Driver data (common to all clients)
180  */
181
182 static struct i2c_driver fschmd_driver = {
183         .driver = {
184                 .name   = FSCHMD_NAME,
185         },
186         .attach_adapter = fschmd_attach_adapter,
187         .detach_client  = fschmd_detach_client,
188 };
189
190 /*
191  * Client data (each client gets its own)
192  */
193
194 struct fschmd_data {
195         struct i2c_client client;
196         struct device *hwmon_dev;
197         struct mutex update_lock;
198         int kind;
199         char valid; /* zero until following fields are valid */
200         unsigned long last_updated; /* in jiffies */
201
202         /* register values */
203         u8 global_control;      /* global control register */
204         u8 volt[3];             /* 12, 5, battery voltage */
205         u8 temp_act[5];         /* temperature */
206         u8 temp_status[5];      /* status of sensor */
207         u8 temp_max[5];         /* high temp limit, notice: undocumented! */
208         u8 fan_act[6];          /* fans revolutions per second */
209         u8 fan_status[6];       /* fan status */
210         u8 fan_min[6];          /* fan min value for rps */
211         u8 fan_ripple[6];       /* divider for rps */
212 };
213
214 /* Global variables to hold information read from special DMI tables, which are
215    available on FSC machines with an fscher or later chip. */
216 static int dmi_mult[3] = { 490, 200, 100 };
217 static int dmi_offset[3] = { 0, 0, 0 };
218 static int dmi_vref = -1;
219
220
221 /*
222  * Sysfs attr show / store functions
223  */
224
225 static ssize_t show_in_value(struct device *dev,
226         struct device_attribute *devattr, char *buf)
227 {
228         const int max_reading[3] = { 14200, 6600, 3300 };
229         int index = to_sensor_dev_attr(devattr)->index;
230         struct fschmd_data *data = fschmd_update_device(dev);
231
232         /* fscher / fschrc - 1 as data->kind is an array index, not a chips */
233         if (data->kind == (fscher - 1) || data->kind >= (fschrc - 1))
234                 return sprintf(buf, "%d\n", (data->volt[index] * dmi_vref *
235                         dmi_mult[index]) / 255 + dmi_offset[index]);
236         else
237                 return sprintf(buf, "%d\n", (data->volt[index] *
238                         max_reading[index] + 128) / 255);
239 }
240
241
242 #define TEMP_FROM_REG(val)      (((val) - 128) * 1000)
243
244 static ssize_t show_temp_value(struct device *dev,
245         struct device_attribute *devattr, char *buf)
246 {
247         int index = to_sensor_dev_attr(devattr)->index;
248         struct fschmd_data *data = fschmd_update_device(dev);
249
250         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_act[index]));
251 }
252
253 static ssize_t show_temp_max(struct device *dev,
254         struct device_attribute *devattr, char *buf)
255 {
256         int index = to_sensor_dev_attr(devattr)->index;
257         struct fschmd_data *data = fschmd_update_device(dev);
258
259         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[index]));
260 }
261
262 static ssize_t store_temp_max(struct device *dev, struct device_attribute
263         *devattr, const char *buf, size_t count)
264 {
265         int index = to_sensor_dev_attr(devattr)->index;
266         struct fschmd_data *data = dev_get_drvdata(dev);
267         long v = simple_strtol(buf, NULL, 10) / 1000;
268
269         v = SENSORS_LIMIT(v, -128, 127) + 128;
270
271         mutex_lock(&data->update_lock);
272         i2c_smbus_write_byte_data(&data->client,
273                 FSCHMD_REG_TEMP_LIMIT[data->kind][index], v);
274         data->temp_max[index] = v;
275         mutex_unlock(&data->update_lock);
276
277         return count;
278 }
279
280 static ssize_t show_temp_fault(struct device *dev,
281         struct device_attribute *devattr, char *buf)
282 {
283         int index = to_sensor_dev_attr(devattr)->index;
284         struct fschmd_data *data = fschmd_update_device(dev);
285
286         /* bit 0 set means sensor working ok, so no fault! */
287         if (data->temp_status[index] & FSCHMD_TEMP_WORKING_MASK)
288                 return sprintf(buf, "0\n");
289         else
290                 return sprintf(buf, "1\n");
291 }
292
293 static ssize_t show_temp_alarm(struct device *dev,
294         struct device_attribute *devattr, char *buf)
295 {
296         int index = to_sensor_dev_attr(devattr)->index;
297         struct fschmd_data *data = fschmd_update_device(dev);
298
299         if ((data->temp_status[index] & FSCHMD_TEMP_ALARM_MASK) ==
300                         FSCHMD_TEMP_ALARM_MASK)
301                 return sprintf(buf, "1\n");
302         else
303                 return sprintf(buf, "0\n");
304 }
305
306
307 #define RPM_FROM_REG(val)       ((val) * 60)
308
309 static ssize_t show_fan_value(struct device *dev,
310         struct device_attribute *devattr, char *buf)
311 {
312         int index = to_sensor_dev_attr(devattr)->index;
313         struct fschmd_data *data = fschmd_update_device(dev);
314
315         return sprintf(buf, "%u\n", RPM_FROM_REG(data->fan_act[index]));
316 }
317
318 static ssize_t show_fan_div(struct device *dev,
319         struct device_attribute *devattr, char *buf)
320 {
321         int index = to_sensor_dev_attr(devattr)->index;
322         struct fschmd_data *data = fschmd_update_device(dev);
323
324         /* bits 2..7 reserved => mask with 3 */
325         return sprintf(buf, "%d\n", 1 << (data->fan_ripple[index] & 3));
326 }
327
328 static ssize_t store_fan_div(struct device *dev, struct device_attribute
329         *devattr, const char *buf, size_t count)
330 {
331         u8 reg;
332         int index = to_sensor_dev_attr(devattr)->index;
333         struct fschmd_data *data = dev_get_drvdata(dev);
334         /* supported values: 2, 4, 8 */
335         unsigned long v = simple_strtoul(buf, NULL, 10);
336
337         switch (v) {
338         case 2: v = 1; break;
339         case 4: v = 2; break;
340         case 8: v = 3; break;
341         default:
342                 dev_err(dev, "fan_div value %lu not supported. "
343                         "Choose one of 2, 4 or 8!\n", v);
344                 return -EINVAL;
345         }
346
347         mutex_lock(&data->update_lock);
348
349         reg = i2c_smbus_read_byte_data(&data->client,
350                 FSCHMD_REG_FAN_RIPPLE[data->kind][index]);
351
352         /* bits 2..7 reserved => mask with 0x03 */
353         reg &= ~0x03;
354         reg |= v;
355
356         i2c_smbus_write_byte_data(&data->client,
357                 FSCHMD_REG_FAN_RIPPLE[data->kind][index], reg);
358
359         data->fan_ripple[index] = reg;
360
361         mutex_unlock(&data->update_lock);
362
363         return count;
364 }
365
366 static ssize_t show_fan_alarm(struct device *dev,
367         struct device_attribute *devattr, char *buf)
368 {
369         int index = to_sensor_dev_attr(devattr)->index;
370         struct fschmd_data *data = fschmd_update_device(dev);
371
372         if (data->fan_status[index] & FSCHMD_FAN_ALARM_MASK)
373                 return sprintf(buf, "1\n");
374         else
375                 return sprintf(buf, "0\n");
376 }
377
378 static ssize_t show_fan_fault(struct device *dev,
379         struct device_attribute *devattr, char *buf)
380 {
381         int index = to_sensor_dev_attr(devattr)->index;
382         struct fschmd_data *data = fschmd_update_device(dev);
383
384         if (data->fan_status[index] & FSCHMD_FAN_NOT_PRESENT_MASK)
385                 return sprintf(buf, "1\n");
386         else
387                 return sprintf(buf, "0\n");
388 }
389
390
391 static ssize_t show_pwm_auto_point1_pwm(struct device *dev,
392         struct device_attribute *devattr, char *buf)
393 {
394         int index = to_sensor_dev_attr(devattr)->index;
395         int val = fschmd_update_device(dev)->fan_min[index];
396
397         /* 0 = allow turning off, 1-255 = 50-100% */
398         if (val)
399                 val = val / 2 + 128;
400
401         return sprintf(buf, "%d\n", val);
402 }
403
404 static ssize_t store_pwm_auto_point1_pwm(struct device *dev,
405         struct device_attribute *devattr, const char *buf, size_t count)
406 {
407         int index = to_sensor_dev_attr(devattr)->index;
408         struct fschmd_data *data = dev_get_drvdata(dev);
409         unsigned long v = simple_strtoul(buf, NULL, 10);
410
411         /* register: 0 = allow turning off, 1-255 = 50-100% */
412         if (v) {
413                 v = SENSORS_LIMIT(v, 128, 255);
414                 v = (v - 128) * 2 + 1;
415         }
416
417         mutex_lock(&data->update_lock);
418
419         i2c_smbus_write_byte_data(&data->client,
420                 FSCHMD_REG_FAN_MIN[data->kind][index], v);
421         data->fan_min[index] = v;
422
423         mutex_unlock(&data->update_lock);
424
425         return count;
426 }
427
428
429 /* The FSC hwmon family has the ability to force an attached alert led to flash
430    from software, we export this as an alert_led sysfs attr */
431 static ssize_t show_alert_led(struct device *dev,
432         struct device_attribute *devattr, char *buf)
433 {
434         struct fschmd_data *data = fschmd_update_device(dev);
435
436         if (data->global_control & FSCHMD_CONTROL_ALERT_LED_MASK)
437                 return sprintf(buf, "1\n");
438         else
439                 return sprintf(buf, "0\n");
440 }
441
442 static ssize_t store_alert_led(struct device *dev,
443         struct device_attribute *devattr, const char *buf, size_t count)
444 {
445         u8 reg;
446         struct fschmd_data *data = dev_get_drvdata(dev);
447         unsigned long v = simple_strtoul(buf, NULL, 10);
448
449         mutex_lock(&data->update_lock);
450
451         reg = i2c_smbus_read_byte_data(&data->client, FSCHMD_REG_CONTROL);
452
453         if (v)
454                 reg |= FSCHMD_CONTROL_ALERT_LED_MASK;
455         else
456                 reg &= ~FSCHMD_CONTROL_ALERT_LED_MASK;
457
458         i2c_smbus_write_byte_data(&data->client, FSCHMD_REG_CONTROL, reg);
459
460         data->global_control = reg;
461
462         mutex_unlock(&data->update_lock);
463
464         return count;
465 }
466
467 static struct sensor_device_attribute fschmd_attr[] = {
468         SENSOR_ATTR(in0_input, 0444, show_in_value, NULL, 0),
469         SENSOR_ATTR(in1_input, 0444, show_in_value, NULL, 1),
470         SENSOR_ATTR(in2_input, 0444, show_in_value, NULL, 2),
471         SENSOR_ATTR(alert_led, 0644, show_alert_led, store_alert_led, 0),
472 };
473
474 static struct sensor_device_attribute fschmd_temp_attr[] = {
475         SENSOR_ATTR(temp1_input, 0444, show_temp_value, NULL, 0),
476         SENSOR_ATTR(temp1_max,   0644, show_temp_max, store_temp_max, 0),
477         SENSOR_ATTR(temp1_fault, 0444, show_temp_fault, NULL, 0),
478         SENSOR_ATTR(temp1_alarm, 0444, show_temp_alarm, NULL, 0),
479         SENSOR_ATTR(temp2_input, 0444, show_temp_value, NULL, 1),
480         SENSOR_ATTR(temp2_max,   0644, show_temp_max, store_temp_max, 1),
481         SENSOR_ATTR(temp2_fault, 0444, show_temp_fault, NULL, 1),
482         SENSOR_ATTR(temp2_alarm, 0444, show_temp_alarm, NULL, 1),
483         SENSOR_ATTR(temp3_input, 0444, show_temp_value, NULL, 2),
484         SENSOR_ATTR(temp3_max,   0644, show_temp_max, store_temp_max, 2),
485         SENSOR_ATTR(temp3_fault, 0444, show_temp_fault, NULL, 2),
486         SENSOR_ATTR(temp3_alarm, 0444, show_temp_alarm, NULL, 2),
487         SENSOR_ATTR(temp4_input, 0444, show_temp_value, NULL, 3),
488         SENSOR_ATTR(temp4_max,   0644, show_temp_max, store_temp_max, 3),
489         SENSOR_ATTR(temp4_fault, 0444, show_temp_fault, NULL, 3),
490         SENSOR_ATTR(temp4_alarm, 0444, show_temp_alarm, NULL, 3),
491         SENSOR_ATTR(temp5_input, 0444, show_temp_value, NULL, 4),
492         SENSOR_ATTR(temp5_max,   0644, show_temp_max, store_temp_max, 4),
493         SENSOR_ATTR(temp5_fault, 0444, show_temp_fault, NULL, 4),
494         SENSOR_ATTR(temp5_alarm, 0444, show_temp_alarm, NULL, 4),
495 };
496
497 static struct sensor_device_attribute fschmd_fan_attr[] = {
498         SENSOR_ATTR(fan1_input, 0444, show_fan_value, NULL, 0),
499         SENSOR_ATTR(fan1_div,   0644, show_fan_div, store_fan_div, 0),
500         SENSOR_ATTR(fan1_alarm, 0444, show_fan_alarm, NULL, 0),
501         SENSOR_ATTR(fan1_fault, 0444, show_fan_fault, NULL, 0),
502         SENSOR_ATTR(pwm1_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
503                 store_pwm_auto_point1_pwm, 0),
504         SENSOR_ATTR(fan2_input, 0444, show_fan_value, NULL, 1),
505         SENSOR_ATTR(fan2_div,   0644, show_fan_div, store_fan_div, 1),
506         SENSOR_ATTR(fan2_alarm, 0444, show_fan_alarm, NULL, 1),
507         SENSOR_ATTR(fan2_fault, 0444, show_fan_fault, NULL, 1),
508         SENSOR_ATTR(pwm2_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
509                 store_pwm_auto_point1_pwm, 1),
510         SENSOR_ATTR(fan3_input, 0444, show_fan_value, NULL, 2),
511         SENSOR_ATTR(fan3_div,   0644, show_fan_div, store_fan_div, 2),
512         SENSOR_ATTR(fan3_alarm, 0444, show_fan_alarm, NULL, 2),
513         SENSOR_ATTR(fan3_fault, 0444, show_fan_fault, NULL, 2),
514         SENSOR_ATTR(pwm3_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
515                 store_pwm_auto_point1_pwm, 2),
516         SENSOR_ATTR(fan4_input, 0444, show_fan_value, NULL, 3),
517         SENSOR_ATTR(fan4_div,   0644, show_fan_div, store_fan_div, 3),
518         SENSOR_ATTR(fan4_alarm, 0444, show_fan_alarm, NULL, 3),
519         SENSOR_ATTR(fan4_fault, 0444, show_fan_fault, NULL, 3),
520         SENSOR_ATTR(pwm4_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
521                 store_pwm_auto_point1_pwm, 3),
522         SENSOR_ATTR(fan5_input, 0444, show_fan_value, NULL, 4),
523         SENSOR_ATTR(fan5_div,   0644, show_fan_div, store_fan_div, 4),
524         SENSOR_ATTR(fan5_alarm, 0444, show_fan_alarm, NULL, 4),
525         SENSOR_ATTR(fan5_fault, 0444, show_fan_fault, NULL, 4),
526         SENSOR_ATTR(pwm5_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
527                 store_pwm_auto_point1_pwm, 4),
528         SENSOR_ATTR(fan6_input, 0444, show_fan_value, NULL, 5),
529         SENSOR_ATTR(fan6_div,   0644, show_fan_div, store_fan_div, 5),
530         SENSOR_ATTR(fan6_alarm, 0444, show_fan_alarm, NULL, 5),
531         SENSOR_ATTR(fan6_fault, 0444, show_fan_fault, NULL, 5),
532         SENSOR_ATTR(pwm6_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
533                 store_pwm_auto_point1_pwm, 5),
534 };
535
536
537 /*
538  * Real code
539  */
540
541 /* DMI decode routine to read voltage scaling factors from special DMI tables,
542    which are available on FSC machines with an fscher or later chip. */
543 static void fschmd_dmi_decode(const struct dmi_header *header)
544 {
545         int i, mult[3] = { 0 }, offset[3] = { 0 }, vref = 0, found = 0;
546
547         /* dmi code ugliness, we get passed the address of the contents of
548            a complete DMI record, but in the form of a dmi_header pointer, in
549            reality this address holds header->length bytes of which the header
550            are the first 4 bytes */
551         u8 *dmi_data = (u8 *)header;
552
553         /* We are looking for OEM-specific type 185 */
554         if (header->type != 185)
555                 return;
556
557         /* we are looking for what Siemens calls "subtype" 19, the subtype
558            is stored in byte 5 of the dmi block */
559         if (header->length < 5 || dmi_data[4] != 19)
560                 return;
561
562         /* After the subtype comes 1 unknown byte and then blocks of 5 bytes,
563            consisting of what Siemens calls an "Entity" number, followed by
564            2 16-bit words in LSB first order */
565         for (i = 6; (i + 4) < header->length; i += 5) {
566                 /* entity 1 - 3: voltage multiplier and offset */
567                 if (dmi_data[i] >= 1 && dmi_data[i] <= 3) {
568                         /* Our in sensors order and the DMI order differ */
569                         const int shuffle[3] = { 1, 0, 2 };
570                         int in = shuffle[dmi_data[i] - 1];
571
572                         /* Check for twice the same entity */
573                         if (found & (1 << in))
574                                 return;
575
576                         mult[in] = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
577                         offset[in] = dmi_data[i + 3] | (dmi_data[i + 4] << 8);
578
579                         found |= 1 << in;
580                 }
581
582                 /* entity 7: reference voltage */
583                 if (dmi_data[i] == 7) {
584                         /* Check for twice the same entity */
585                         if (found & 0x08)
586                                 return;
587
588                         vref = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
589
590                         found |= 0x08;
591                 }
592         }
593
594         if (found == 0x0F) {
595                 for (i = 0; i < 3; i++) {
596                         dmi_mult[i] = mult[i] * 10;
597                         dmi_offset[i] = offset[i] * 10;
598                 }
599                 dmi_vref = vref;
600         }
601 }
602
603 static int fschmd_detect(struct i2c_adapter *adapter, int address, int kind)
604 {
605         struct i2c_client *client;
606         struct fschmd_data *data;
607         u8 revision;
608         const char * const names[5] = { "Poseidon", "Hermes", "Scylla",
609                                         "Heracles", "Heimdall" };
610         const char * const client_names[5] = { "fscpos", "fscher", "fscscy",
611                                                 "fschrc", "fschmd" };
612         int i, err = 0;
613
614         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
615                 return 0;
616
617         /* OK. For now, we presume we have a valid client. We now create the
618          * client structure, even though we cannot fill it completely yet.
619          * But it allows us to access i2c_smbus_read_byte_data. */
620         if (!(data = kzalloc(sizeof(struct fschmd_data), GFP_KERNEL)))
621                 return -ENOMEM;
622
623         client = &data->client;
624         i2c_set_clientdata(client, data);
625         client->addr = address;
626         client->adapter = adapter;
627         client->driver = &fschmd_driver;
628         mutex_init(&data->update_lock);
629
630         /* Detect & Identify the chip */
631         if (kind <= 0) {
632                 char id[4];
633
634                 id[0] = i2c_smbus_read_byte_data(client,
635                                 FSCHMD_REG_IDENT_0);
636                 id[1] = i2c_smbus_read_byte_data(client,
637                                 FSCHMD_REG_IDENT_1);
638                 id[2] = i2c_smbus_read_byte_data(client,
639                                 FSCHMD_REG_IDENT_2);
640                 id[3] = '\0';
641
642                 if (!strcmp(id, "PEG"))
643                         kind = fscpos;
644                 else if (!strcmp(id, "HER"))
645                         kind = fscher;
646                 else if (!strcmp(id, "SCY"))
647                         kind = fscscy;
648                 else if (!strcmp(id, "HRC"))
649                         kind = fschrc;
650                 else if (!strcmp(id, "HMD"))
651                         kind = fschmd;
652                 else
653                         goto exit_free;
654         }
655
656         if (kind == fscpos) {
657                 /* The Poseidon has hardwired temp limits, fill these
658                    in for the alarm resetting code */
659                 data->temp_max[0] = 70 + 128;
660                 data->temp_max[1] = 50 + 128;
661                 data->temp_max[2] = 50 + 128;
662         }
663
664         /* Read the special DMI table for fscher and newer chips */
665         if (kind == fscher || kind >= fschrc) {
666                 dmi_walk(fschmd_dmi_decode);
667                 if (dmi_vref == -1) {
668                         printk(KERN_WARNING FSCHMD_NAME
669                                 ": Couldn't get voltage scaling factors from "
670                                 "BIOS DMI table, using builtin defaults\n");
671                         dmi_vref = 33;
672                 }
673         }
674
675         /* i2c kind goes from 1-5, we want from 0-4 to address arrays */
676         data->kind = kind - 1;
677         strlcpy(client->name, client_names[data->kind], I2C_NAME_SIZE);
678
679         /* Tell the I2C layer a new client has arrived */
680         if ((err = i2c_attach_client(client)))
681                 goto exit_free;
682
683         for (i = 0; i < ARRAY_SIZE(fschmd_attr); i++) {
684                 err = device_create_file(&client->dev,
685                                         &fschmd_attr[i].dev_attr);
686                 if (err)
687                         goto exit_detach;
688         }
689
690         for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++) {
691                 /* Poseidon doesn't have TEMP_LIMIT registers */
692                 if (kind == fscpos && fschmd_temp_attr[i].dev_attr.show ==
693                                 show_temp_max)
694                         continue;
695
696                 err = device_create_file(&client->dev,
697                                         &fschmd_temp_attr[i].dev_attr);
698                 if (err)
699                         goto exit_detach;
700         }
701
702         for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++) {
703                 /* Poseidon doesn't have a FAN_MIN register for its 3rd fan */
704                 if (kind == fscpos &&
705                                 !strcmp(fschmd_fan_attr[i].dev_attr.attr.name,
706                                         "pwm3_auto_point1_pwm"))
707                         continue;
708
709                 err = device_create_file(&client->dev,
710                                         &fschmd_fan_attr[i].dev_attr);
711                 if (err)
712                         goto exit_detach;
713         }
714
715         data->hwmon_dev = hwmon_device_register(&client->dev);
716         if (IS_ERR(data->hwmon_dev)) {
717                 err = PTR_ERR(data->hwmon_dev);
718                 data->hwmon_dev = NULL;
719                 goto exit_detach;
720         }
721
722         revision = i2c_smbus_read_byte_data(client, FSCHMD_REG_REVISION);
723         printk(KERN_INFO FSCHMD_NAME ": Detected FSC %s chip, revision: %d\n",
724                 names[data->kind], (int) revision);
725
726         return 0;
727
728 exit_detach:
729         fschmd_detach_client(client); /* will also free data for us */
730         return err;
731
732 exit_free:
733         kfree(data);
734         return err;
735 }
736
737 static int fschmd_attach_adapter(struct i2c_adapter *adapter)
738 {
739         if (!(adapter->class & I2C_CLASS_HWMON))
740                 return 0;
741         return i2c_probe(adapter, &addr_data, fschmd_detect);
742 }
743
744 static int fschmd_detach_client(struct i2c_client *client)
745 {
746         struct fschmd_data *data = i2c_get_clientdata(client);
747         int i, err;
748
749         /* Check if registered in case we're called from fschmd_detect
750            to cleanup after an error */
751         if (data->hwmon_dev)
752                 hwmon_device_unregister(data->hwmon_dev);
753
754         for (i = 0; i < ARRAY_SIZE(fschmd_attr); i++)
755                 device_remove_file(&client->dev, &fschmd_attr[i].dev_attr);
756         for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++)
757                 device_remove_file(&client->dev,
758                                         &fschmd_temp_attr[i].dev_attr);
759         for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++)
760                 device_remove_file(&client->dev,
761                                         &fschmd_fan_attr[i].dev_attr);
762
763         if ((err = i2c_detach_client(client)))
764                 return err;
765
766         kfree(data);
767         return 0;
768 }
769
770 static struct fschmd_data *fschmd_update_device(struct device *dev)
771 {
772         struct i2c_client *client = to_i2c_client(dev);
773         struct fschmd_data *data = i2c_get_clientdata(client);
774         int i;
775
776         mutex_lock(&data->update_lock);
777
778         if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
779
780                 for (i = 0; i < FSCHMD_NO_TEMP_SENSORS[data->kind]; i++) {
781                         data->temp_act[i] = i2c_smbus_read_byte_data(client,
782                                         FSCHMD_REG_TEMP_ACT[data->kind][i]);
783                         data->temp_status[i] = i2c_smbus_read_byte_data(client,
784                                         FSCHMD_REG_TEMP_STATE[data->kind][i]);
785
786                         /* The fscpos doesn't have TEMP_LIMIT registers */
787                         if (FSCHMD_REG_TEMP_LIMIT[data->kind][i])
788                                 data->temp_max[i] = i2c_smbus_read_byte_data(
789                                         client,
790                                         FSCHMD_REG_TEMP_LIMIT[data->kind][i]);
791
792                         /* reset alarm if the alarm condition is gone,
793                            the chip doesn't do this itself */
794                         if ((data->temp_status[i] & FSCHMD_TEMP_ALARM_MASK) ==
795                                         FSCHMD_TEMP_ALARM_MASK &&
796                                         data->temp_act[i] < data->temp_max[i])
797                                 i2c_smbus_write_byte_data(client,
798                                         FSCHMD_REG_TEMP_STATE[data->kind][i],
799                                         FSCHMD_TEMP_ALERT_MASK);
800                 }
801
802                 for (i = 0; i < FSCHMD_NO_FAN_SENSORS[data->kind]; i++) {
803                         data->fan_act[i] = i2c_smbus_read_byte_data(client,
804                                         FSCHMD_REG_FAN_ACT[data->kind][i]);
805                         data->fan_status[i] = i2c_smbus_read_byte_data(client,
806                                         FSCHMD_REG_FAN_STATE[data->kind][i]);
807                         data->fan_ripple[i] = i2c_smbus_read_byte_data(client,
808                                         FSCHMD_REG_FAN_RIPPLE[data->kind][i]);
809
810                         /* The fscpos third fan doesn't have a fan_min */
811                         if (FSCHMD_REG_FAN_MIN[data->kind][i])
812                                 data->fan_min[i] = i2c_smbus_read_byte_data(
813                                         client,
814                                         FSCHMD_REG_FAN_MIN[data->kind][i]);
815
816                         /* reset fan status if speed is back to > 0 */
817                         if ((data->fan_status[i] & FSCHMD_FAN_ALARM_MASK) &&
818                                         data->fan_act[i])
819                                 i2c_smbus_write_byte_data(client,
820                                         FSCHMD_REG_FAN_STATE[data->kind][i],
821                                         FSCHMD_FAN_ALARM_MASK);
822                 }
823
824                 for (i = 0; i < 3; i++)
825                         data->volt[i] = i2c_smbus_read_byte_data(client,
826                                                 FSCHMD_REG_VOLT[i]);
827
828                 data->global_control = i2c_smbus_read_byte_data(client,
829                                                 FSCHMD_REG_CONTROL);
830
831                 /* To be implemented in the future
832                 data->watchdog[0] = i2c_smbus_read_byte_data(client,
833                                                 FSCHMD_REG_WDOG_PRESET);
834                 data->watchdog[1] = i2c_smbus_read_byte_data(client,
835                                                 FSCHMD_REG_WDOG_STATE);
836                 data->watchdog[2] = i2c_smbus_read_byte_data(client,
837                                                 FSCHMD_REG_WDOG_CONTROL); */
838
839                 data->last_updated = jiffies;
840                 data->valid = 1;
841         }
842
843         mutex_unlock(&data->update_lock);
844
845         return data;
846 }
847
848 static int __init fschmd_init(void)
849 {
850         return i2c_add_driver(&fschmd_driver);
851 }
852
853 static void __exit fschmd_exit(void)
854 {
855         i2c_del_driver(&fschmd_driver);
856 }
857
858 MODULE_AUTHOR("Hans de Goede <j.w.r.degoede@hhs.nl>");
859 MODULE_DESCRIPTION("FSC Poseidon, Hermes, Scylla, Heracles and "
860                         "Heimdall driver");
861 MODULE_LICENSE("GPL");
862
863 module_init(fschmd_init);
864 module_exit(fschmd_exit);