Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / fschmd.c
blob3a025b9634249a98fac156c32e670997f6c69bab
1 /* fschmd.c
3 * Copyright (C) 2007 Hans de Goede <j.w.r.degoede@hhs.nl>
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.
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.
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.
21 * Merged Fujitsu Siemens hwmon driver, supporting the Poseidon, Hermes,
22 * Scylla, Heracles and Heimdall chips
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>
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>
46 /* Addresses to scan */
47 <<<<<<< HEAD:drivers/hwmon/fschmd.c
48 static unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
49 =======
50 static const unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
51 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/fschmd.c
53 /* Insmod parameters */
54 I2C_CLIENT_INSMOD_5(fscpos, fscher, fscscy, fschrc, fschmd);
57 * The FSCHMD registers and other defines
60 /* chip identification */
61 #define FSCHMD_REG_IDENT_0 0x00
62 #define FSCHMD_REG_IDENT_1 0x01
63 #define FSCHMD_REG_IDENT_2 0x02
64 #define FSCHMD_REG_REVISION 0x03
66 /* global control and status */
67 #define FSCHMD_REG_EVENT_STATE 0x04
68 #define FSCHMD_REG_CONTROL 0x05
70 #define FSCHMD_CONTROL_ALERT_LED_MASK 0x01
72 /* watchdog (support to be implemented) */
73 #define FSCHMD_REG_WDOG_PRESET 0x28
74 #define FSCHMD_REG_WDOG_STATE 0x23
75 #define FSCHMD_REG_WDOG_CONTROL 0x21
77 /* voltages, weird order is to keep the same order as the old drivers */
78 static const u8 FSCHMD_REG_VOLT[3] = { 0x45, 0x42, 0x48 };
80 /* minimum pwm at which the fan is driven (pwm can by increased depending on
81 the temp. Notice that for the scy some fans share there minimum speed.
82 Also notice that with the scy the sensor order is different then with the
83 other chips, this order was in the 2.4 driver and kept for consistency. */
84 static const u8 FSCHMD_REG_FAN_MIN[5][6] = {
85 { 0x55, 0x65 }, /* pos */
86 { 0x55, 0x65, 0xb5 }, /* her */
87 { 0x65, 0x65, 0x55, 0xa5, 0x55, 0xa5 }, /* scy */
88 { 0x55, 0x65, 0xa5, 0xb5 }, /* hrc */
89 { 0x55, 0x65, 0xa5, 0xb5, 0xc5 }, /* hmd */
92 /* actual fan speed */
93 static const u8 FSCHMD_REG_FAN_ACT[5][6] = {
94 { 0x0e, 0x6b, 0xab }, /* pos */
95 { 0x0e, 0x6b, 0xbb }, /* her */
96 { 0x6b, 0x6c, 0x0e, 0xab, 0x5c, 0xbb }, /* scy */
97 { 0x0e, 0x6b, 0xab, 0xbb }, /* hrc */
98 { 0x5b, 0x6b, 0xab, 0xbb, 0xcb }, /* hmd */
101 /* fan status registers */
102 static const u8 FSCHMD_REG_FAN_STATE[5][6] = {
103 { 0x0d, 0x62, 0xa2 }, /* pos */
104 { 0x0d, 0x62, 0xb2 }, /* her */
105 { 0x62, 0x61, 0x0d, 0xa2, 0x52, 0xb2 }, /* scy */
106 { 0x0d, 0x62, 0xa2, 0xb2 }, /* hrc */
107 { 0x52, 0x62, 0xa2, 0xb2, 0xc2 }, /* hmd */
110 /* fan ripple / divider registers */
111 static const u8 FSCHMD_REG_FAN_RIPPLE[5][6] = {
112 { 0x0f, 0x6f, 0xaf }, /* pos */
113 { 0x0f, 0x6f, 0xbf }, /* her */
114 { 0x6f, 0x6f, 0x0f, 0xaf, 0x0f, 0xbf }, /* scy */
115 { 0x0f, 0x6f, 0xaf, 0xbf }, /* hrc */
116 { 0x5f, 0x6f, 0xaf, 0xbf, 0xcf }, /* hmd */
119 static const int FSCHMD_NO_FAN_SENSORS[5] = { 3, 3, 6, 4, 5 };
121 /* Fan status register bitmasks */
122 #define FSCHMD_FAN_ALARM_MASK 0x04 /* called fault by FSC! */
123 #define FSCHMD_FAN_NOT_PRESENT_MASK 0x08 /* not documented */
126 /* actual temperature registers */
127 static const u8 FSCHMD_REG_TEMP_ACT[5][5] = {
128 { 0x64, 0x32, 0x35 }, /* pos */
129 { 0x64, 0x32, 0x35 }, /* her */
130 { 0x64, 0xD0, 0x32, 0x35 }, /* scy */
131 { 0x64, 0x32, 0x35 }, /* hrc */
132 { 0x70, 0x80, 0x90, 0xd0, 0xe0 }, /* hmd */
135 /* temperature state registers */
136 static const u8 FSCHMD_REG_TEMP_STATE[5][5] = {
137 { 0x71, 0x81, 0x91 }, /* pos */
138 { 0x71, 0x81, 0x91 }, /* her */
139 { 0x71, 0xd1, 0x81, 0x91 }, /* scy */
140 { 0x71, 0x81, 0x91 }, /* hrc */
141 { 0x71, 0x81, 0x91, 0xd1, 0xe1 }, /* hmd */
144 /* temperature high limit registers, FSC does not document these. Proven to be
145 there with field testing on the fscher and fschrc, already supported / used
146 in the fscscy 2.4 driver. FSC has confirmed that the fschmd has registers
147 at these addresses, but doesn't want to confirm they are the same as with
148 the fscher?? */
149 static const u8 FSCHMD_REG_TEMP_LIMIT[5][5] = {
150 { 0, 0, 0 }, /* pos */
151 { 0x76, 0x86, 0x96 }, /* her */
152 { 0x76, 0xd6, 0x86, 0x96 }, /* scy */
153 { 0x76, 0x86, 0x96 }, /* hrc */
154 { 0x76, 0x86, 0x96, 0xd6, 0xe6 }, /* hmd */
157 /* These were found through experimenting with an fscher, currently they are
158 not used, but we keep them around for future reference.
159 static const u8 FSCHER_REG_TEMP_AUTOP1[] = { 0x73, 0x83, 0x93 };
160 static const u8 FSCHER_REG_TEMP_AUTOP2[] = { 0x75, 0x85, 0x95 }; */
162 static const int FSCHMD_NO_TEMP_SENSORS[5] = { 3, 3, 4, 3, 5 };
164 /* temp status register bitmasks */
165 #define FSCHMD_TEMP_WORKING_MASK 0x01
166 #define FSCHMD_TEMP_ALERT_MASK 0x02
167 /* there only really is an alarm if the sensor is working and alert == 1 */
168 #define FSCHMD_TEMP_ALARM_MASK \
169 (FSCHMD_TEMP_WORKING_MASK | FSCHMD_TEMP_ALERT_MASK)
171 /* our driver name */
172 #define FSCHMD_NAME "fschmd"
175 * Functions declarations
178 static int fschmd_attach_adapter(struct i2c_adapter *adapter);
179 static int fschmd_detach_client(struct i2c_client *client);
180 static struct fschmd_data *fschmd_update_device(struct device *dev);
183 * Driver data (common to all clients)
186 static struct i2c_driver fschmd_driver = {
187 .driver = {
188 .name = FSCHMD_NAME,
190 .attach_adapter = fschmd_attach_adapter,
191 .detach_client = fschmd_detach_client,
195 * Client data (each client gets its own)
198 struct fschmd_data {
199 struct i2c_client client;
200 struct device *hwmon_dev;
201 struct mutex update_lock;
202 int kind;
203 char valid; /* zero until following fields are valid */
204 unsigned long last_updated; /* in jiffies */
206 /* register values */
207 u8 global_control; /* global control register */
208 u8 volt[3]; /* 12, 5, battery voltage */
209 u8 temp_act[5]; /* temperature */
210 u8 temp_status[5]; /* status of sensor */
211 u8 temp_max[5]; /* high temp limit, notice: undocumented! */
212 u8 fan_act[6]; /* fans revolutions per second */
213 u8 fan_status[6]; /* fan status */
214 u8 fan_min[6]; /* fan min value for rps */
215 u8 fan_ripple[6]; /* divider for rps */
218 /* Global variables to hold information read from special DMI tables, which are
219 available on FSC machines with an fscher or later chip. */
220 static int dmi_mult[3] = { 490, 200, 100 };
221 static int dmi_offset[3] = { 0, 0, 0 };
222 static int dmi_vref = -1;
226 * Sysfs attr show / store functions
229 static ssize_t show_in_value(struct device *dev,
230 struct device_attribute *devattr, char *buf)
232 const int max_reading[3] = { 14200, 6600, 3300 };
233 int index = to_sensor_dev_attr(devattr)->index;
234 struct fschmd_data *data = fschmd_update_device(dev);
236 /* fscher / fschrc - 1 as data->kind is an array index, not a chips */
237 if (data->kind == (fscher - 1) || data->kind >= (fschrc - 1))
238 return sprintf(buf, "%d\n", (data->volt[index] * dmi_vref *
239 dmi_mult[index]) / 255 + dmi_offset[index]);
240 else
241 return sprintf(buf, "%d\n", (data->volt[index] *
242 max_reading[index] + 128) / 255);
246 #define TEMP_FROM_REG(val) (((val) - 128) * 1000)
248 static ssize_t show_temp_value(struct device *dev,
249 struct device_attribute *devattr, char *buf)
251 int index = to_sensor_dev_attr(devattr)->index;
252 struct fschmd_data *data = fschmd_update_device(dev);
254 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_act[index]));
257 static ssize_t show_temp_max(struct device *dev,
258 struct device_attribute *devattr, char *buf)
260 int index = to_sensor_dev_attr(devattr)->index;
261 struct fschmd_data *data = fschmd_update_device(dev);
263 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[index]));
266 static ssize_t store_temp_max(struct device *dev, struct device_attribute
267 *devattr, const char *buf, size_t count)
269 int index = to_sensor_dev_attr(devattr)->index;
270 struct fschmd_data *data = dev_get_drvdata(dev);
271 long v = simple_strtol(buf, NULL, 10) / 1000;
273 v = SENSORS_LIMIT(v, -128, 127) + 128;
275 mutex_lock(&data->update_lock);
276 i2c_smbus_write_byte_data(&data->client,
277 FSCHMD_REG_TEMP_LIMIT[data->kind][index], v);
278 data->temp_max[index] = v;
279 mutex_unlock(&data->update_lock);
281 return count;
284 static ssize_t show_temp_fault(struct device *dev,
285 struct device_attribute *devattr, char *buf)
287 int index = to_sensor_dev_attr(devattr)->index;
288 struct fschmd_data *data = fschmd_update_device(dev);
290 /* bit 0 set means sensor working ok, so no fault! */
291 if (data->temp_status[index] & FSCHMD_TEMP_WORKING_MASK)
292 return sprintf(buf, "0\n");
293 else
294 return sprintf(buf, "1\n");
297 static ssize_t show_temp_alarm(struct device *dev,
298 struct device_attribute *devattr, char *buf)
300 int index = to_sensor_dev_attr(devattr)->index;
301 struct fschmd_data *data = fschmd_update_device(dev);
303 if ((data->temp_status[index] & FSCHMD_TEMP_ALARM_MASK) ==
304 FSCHMD_TEMP_ALARM_MASK)
305 return sprintf(buf, "1\n");
306 else
307 return sprintf(buf, "0\n");
311 #define RPM_FROM_REG(val) ((val) * 60)
313 static ssize_t show_fan_value(struct device *dev,
314 struct device_attribute *devattr, char *buf)
316 int index = to_sensor_dev_attr(devattr)->index;
317 struct fschmd_data *data = fschmd_update_device(dev);
319 return sprintf(buf, "%u\n", RPM_FROM_REG(data->fan_act[index]));
322 static ssize_t show_fan_div(struct device *dev,
323 struct device_attribute *devattr, char *buf)
325 int index = to_sensor_dev_attr(devattr)->index;
326 struct fschmd_data *data = fschmd_update_device(dev);
328 /* bits 2..7 reserved => mask with 3 */
329 return sprintf(buf, "%d\n", 1 << (data->fan_ripple[index] & 3));
332 static ssize_t store_fan_div(struct device *dev, struct device_attribute
333 *devattr, const char *buf, size_t count)
335 u8 reg;
336 int index = to_sensor_dev_attr(devattr)->index;
337 struct fschmd_data *data = dev_get_drvdata(dev);
338 /* supported values: 2, 4, 8 */
339 unsigned long v = simple_strtoul(buf, NULL, 10);
341 switch (v) {
342 case 2: v = 1; break;
343 case 4: v = 2; break;
344 case 8: v = 3; break;
345 default:
346 dev_err(dev, "fan_div value %lu not supported. "
347 "Choose one of 2, 4 or 8!\n", v);
348 return -EINVAL;
351 mutex_lock(&data->update_lock);
353 reg = i2c_smbus_read_byte_data(&data->client,
354 FSCHMD_REG_FAN_RIPPLE[data->kind][index]);
356 /* bits 2..7 reserved => mask with 0x03 */
357 reg &= ~0x03;
358 reg |= v;
360 i2c_smbus_write_byte_data(&data->client,
361 FSCHMD_REG_FAN_RIPPLE[data->kind][index], reg);
363 data->fan_ripple[index] = reg;
365 mutex_unlock(&data->update_lock);
367 return count;
370 static ssize_t show_fan_alarm(struct device *dev,
371 struct device_attribute *devattr, char *buf)
373 int index = to_sensor_dev_attr(devattr)->index;
374 struct fschmd_data *data = fschmd_update_device(dev);
376 if (data->fan_status[index] & FSCHMD_FAN_ALARM_MASK)
377 return sprintf(buf, "1\n");
378 else
379 return sprintf(buf, "0\n");
382 static ssize_t show_fan_fault(struct device *dev,
383 struct device_attribute *devattr, char *buf)
385 int index = to_sensor_dev_attr(devattr)->index;
386 struct fschmd_data *data = fschmd_update_device(dev);
388 if (data->fan_status[index] & FSCHMD_FAN_NOT_PRESENT_MASK)
389 return sprintf(buf, "1\n");
390 else
391 return sprintf(buf, "0\n");
395 static ssize_t show_pwm_auto_point1_pwm(struct device *dev,
396 struct device_attribute *devattr, char *buf)
398 int index = to_sensor_dev_attr(devattr)->index;
399 int val = fschmd_update_device(dev)->fan_min[index];
401 /* 0 = allow turning off, 1-255 = 50-100% */
402 if (val)
403 val = val / 2 + 128;
405 return sprintf(buf, "%d\n", val);
408 static ssize_t store_pwm_auto_point1_pwm(struct device *dev,
409 struct device_attribute *devattr, const char *buf, size_t count)
411 int index = to_sensor_dev_attr(devattr)->index;
412 struct fschmd_data *data = dev_get_drvdata(dev);
413 unsigned long v = simple_strtoul(buf, NULL, 10);
415 /* register: 0 = allow turning off, 1-255 = 50-100% */
416 if (v) {
417 v = SENSORS_LIMIT(v, 128, 255);
418 v = (v - 128) * 2 + 1;
421 mutex_lock(&data->update_lock);
423 i2c_smbus_write_byte_data(&data->client,
424 FSCHMD_REG_FAN_MIN[data->kind][index], v);
425 data->fan_min[index] = v;
427 mutex_unlock(&data->update_lock);
429 return count;
433 /* The FSC hwmon family has the ability to force an attached alert led to flash
434 from software, we export this as an alert_led sysfs attr */
435 static ssize_t show_alert_led(struct device *dev,
436 struct device_attribute *devattr, char *buf)
438 struct fschmd_data *data = fschmd_update_device(dev);
440 if (data->global_control & FSCHMD_CONTROL_ALERT_LED_MASK)
441 return sprintf(buf, "1\n");
442 else
443 return sprintf(buf, "0\n");
446 static ssize_t store_alert_led(struct device *dev,
447 struct device_attribute *devattr, const char *buf, size_t count)
449 u8 reg;
450 struct fschmd_data *data = dev_get_drvdata(dev);
451 unsigned long v = simple_strtoul(buf, NULL, 10);
453 mutex_lock(&data->update_lock);
455 reg = i2c_smbus_read_byte_data(&data->client, FSCHMD_REG_CONTROL);
457 if (v)
458 reg |= FSCHMD_CONTROL_ALERT_LED_MASK;
459 else
460 reg &= ~FSCHMD_CONTROL_ALERT_LED_MASK;
462 i2c_smbus_write_byte_data(&data->client, FSCHMD_REG_CONTROL, reg);
464 data->global_control = reg;
466 mutex_unlock(&data->update_lock);
468 return count;
471 static struct sensor_device_attribute fschmd_attr[] = {
472 SENSOR_ATTR(in0_input, 0444, show_in_value, NULL, 0),
473 SENSOR_ATTR(in1_input, 0444, show_in_value, NULL, 1),
474 SENSOR_ATTR(in2_input, 0444, show_in_value, NULL, 2),
475 SENSOR_ATTR(alert_led, 0644, show_alert_led, store_alert_led, 0),
478 static struct sensor_device_attribute fschmd_temp_attr[] = {
479 SENSOR_ATTR(temp1_input, 0444, show_temp_value, NULL, 0),
480 SENSOR_ATTR(temp1_max, 0644, show_temp_max, store_temp_max, 0),
481 SENSOR_ATTR(temp1_fault, 0444, show_temp_fault, NULL, 0),
482 SENSOR_ATTR(temp1_alarm, 0444, show_temp_alarm, NULL, 0),
483 SENSOR_ATTR(temp2_input, 0444, show_temp_value, NULL, 1),
484 SENSOR_ATTR(temp2_max, 0644, show_temp_max, store_temp_max, 1),
485 SENSOR_ATTR(temp2_fault, 0444, show_temp_fault, NULL, 1),
486 SENSOR_ATTR(temp2_alarm, 0444, show_temp_alarm, NULL, 1),
487 SENSOR_ATTR(temp3_input, 0444, show_temp_value, NULL, 2),
488 SENSOR_ATTR(temp3_max, 0644, show_temp_max, store_temp_max, 2),
489 SENSOR_ATTR(temp3_fault, 0444, show_temp_fault, NULL, 2),
490 SENSOR_ATTR(temp3_alarm, 0444, show_temp_alarm, NULL, 2),
491 SENSOR_ATTR(temp4_input, 0444, show_temp_value, NULL, 3),
492 SENSOR_ATTR(temp4_max, 0644, show_temp_max, store_temp_max, 3),
493 SENSOR_ATTR(temp4_fault, 0444, show_temp_fault, NULL, 3),
494 SENSOR_ATTR(temp4_alarm, 0444, show_temp_alarm, NULL, 3),
495 SENSOR_ATTR(temp5_input, 0444, show_temp_value, NULL, 4),
496 SENSOR_ATTR(temp5_max, 0644, show_temp_max, store_temp_max, 4),
497 SENSOR_ATTR(temp5_fault, 0444, show_temp_fault, NULL, 4),
498 SENSOR_ATTR(temp5_alarm, 0444, show_temp_alarm, NULL, 4),
501 static struct sensor_device_attribute fschmd_fan_attr[] = {
502 SENSOR_ATTR(fan1_input, 0444, show_fan_value, NULL, 0),
503 SENSOR_ATTR(fan1_div, 0644, show_fan_div, store_fan_div, 0),
504 SENSOR_ATTR(fan1_alarm, 0444, show_fan_alarm, NULL, 0),
505 SENSOR_ATTR(fan1_fault, 0444, show_fan_fault, NULL, 0),
506 SENSOR_ATTR(pwm1_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
507 store_pwm_auto_point1_pwm, 0),
508 SENSOR_ATTR(fan2_input, 0444, show_fan_value, NULL, 1),
509 SENSOR_ATTR(fan2_div, 0644, show_fan_div, store_fan_div, 1),
510 SENSOR_ATTR(fan2_alarm, 0444, show_fan_alarm, NULL, 1),
511 SENSOR_ATTR(fan2_fault, 0444, show_fan_fault, NULL, 1),
512 SENSOR_ATTR(pwm2_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
513 store_pwm_auto_point1_pwm, 1),
514 SENSOR_ATTR(fan3_input, 0444, show_fan_value, NULL, 2),
515 SENSOR_ATTR(fan3_div, 0644, show_fan_div, store_fan_div, 2),
516 SENSOR_ATTR(fan3_alarm, 0444, show_fan_alarm, NULL, 2),
517 SENSOR_ATTR(fan3_fault, 0444, show_fan_fault, NULL, 2),
518 SENSOR_ATTR(pwm3_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
519 store_pwm_auto_point1_pwm, 2),
520 SENSOR_ATTR(fan4_input, 0444, show_fan_value, NULL, 3),
521 SENSOR_ATTR(fan4_div, 0644, show_fan_div, store_fan_div, 3),
522 SENSOR_ATTR(fan4_alarm, 0444, show_fan_alarm, NULL, 3),
523 SENSOR_ATTR(fan4_fault, 0444, show_fan_fault, NULL, 3),
524 SENSOR_ATTR(pwm4_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
525 store_pwm_auto_point1_pwm, 3),
526 SENSOR_ATTR(fan5_input, 0444, show_fan_value, NULL, 4),
527 SENSOR_ATTR(fan5_div, 0644, show_fan_div, store_fan_div, 4),
528 SENSOR_ATTR(fan5_alarm, 0444, show_fan_alarm, NULL, 4),
529 SENSOR_ATTR(fan5_fault, 0444, show_fan_fault, NULL, 4),
530 SENSOR_ATTR(pwm5_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
531 store_pwm_auto_point1_pwm, 4),
532 SENSOR_ATTR(fan6_input, 0444, show_fan_value, NULL, 5),
533 SENSOR_ATTR(fan6_div, 0644, show_fan_div, store_fan_div, 5),
534 SENSOR_ATTR(fan6_alarm, 0444, show_fan_alarm, NULL, 5),
535 SENSOR_ATTR(fan6_fault, 0444, show_fan_fault, NULL, 5),
536 SENSOR_ATTR(pwm6_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
537 store_pwm_auto_point1_pwm, 5),
542 * Real code
545 /* DMI decode routine to read voltage scaling factors from special DMI tables,
546 which are available on FSC machines with an fscher or later chip. */
547 static void fschmd_dmi_decode(const struct dmi_header *header)
549 int i, mult[3] = { 0 }, offset[3] = { 0 }, vref = 0, found = 0;
551 /* dmi code ugliness, we get passed the address of the contents of
552 a complete DMI record, but in the form of a dmi_header pointer, in
553 reality this address holds header->length bytes of which the header
554 are the first 4 bytes */
555 u8 *dmi_data = (u8 *)header;
557 /* We are looking for OEM-specific type 185 */
558 if (header->type != 185)
559 return;
561 /* we are looking for what Siemens calls "subtype" 19, the subtype
562 is stored in byte 5 of the dmi block */
563 if (header->length < 5 || dmi_data[4] != 19)
564 return;
566 /* After the subtype comes 1 unknown byte and then blocks of 5 bytes,
567 consisting of what Siemens calls an "Entity" number, followed by
568 2 16-bit words in LSB first order */
569 for (i = 6; (i + 4) < header->length; i += 5) {
570 /* entity 1 - 3: voltage multiplier and offset */
571 if (dmi_data[i] >= 1 && dmi_data[i] <= 3) {
572 /* Our in sensors order and the DMI order differ */
573 const int shuffle[3] = { 1, 0, 2 };
574 int in = shuffle[dmi_data[i] - 1];
576 /* Check for twice the same entity */
577 if (found & (1 << in))
578 return;
580 mult[in] = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
581 offset[in] = dmi_data[i + 3] | (dmi_data[i + 4] << 8);
583 found |= 1 << in;
586 /* entity 7: reference voltage */
587 if (dmi_data[i] == 7) {
588 /* Check for twice the same entity */
589 if (found & 0x08)
590 return;
592 vref = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
594 found |= 0x08;
598 if (found == 0x0F) {
599 for (i = 0; i < 3; i++) {
600 dmi_mult[i] = mult[i] * 10;
601 dmi_offset[i] = offset[i] * 10;
603 dmi_vref = vref;
607 static int fschmd_detect(struct i2c_adapter *adapter, int address, int kind)
609 struct i2c_client *client;
610 struct fschmd_data *data;
611 u8 revision;
612 const char * const names[5] = { "Poseidon", "Hermes", "Scylla",
613 "Heracles", "Heimdall" };
614 const char * const client_names[5] = { "fscpos", "fscher", "fscscy",
615 "fschrc", "fschmd" };
616 int i, err = 0;
618 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
619 return 0;
621 /* OK. For now, we presume we have a valid client. We now create the
622 * client structure, even though we cannot fill it completely yet.
623 * But it allows us to access i2c_smbus_read_byte_data. */
624 if (!(data = kzalloc(sizeof(struct fschmd_data), GFP_KERNEL)))
625 return -ENOMEM;
627 client = &data->client;
628 i2c_set_clientdata(client, data);
629 client->addr = address;
630 client->adapter = adapter;
631 client->driver = &fschmd_driver;
632 mutex_init(&data->update_lock);
634 /* Detect & Identify the chip */
635 if (kind <= 0) {
636 char id[4];
638 id[0] = i2c_smbus_read_byte_data(client,
639 FSCHMD_REG_IDENT_0);
640 id[1] = i2c_smbus_read_byte_data(client,
641 FSCHMD_REG_IDENT_1);
642 id[2] = i2c_smbus_read_byte_data(client,
643 FSCHMD_REG_IDENT_2);
644 id[3] = '\0';
646 if (!strcmp(id, "PEG"))
647 kind = fscpos;
648 else if (!strcmp(id, "HER"))
649 kind = fscher;
650 else if (!strcmp(id, "SCY"))
651 kind = fscscy;
652 else if (!strcmp(id, "HRC"))
653 kind = fschrc;
654 else if (!strcmp(id, "HMD"))
655 kind = fschmd;
656 else
657 goto exit_free;
660 if (kind == fscpos) {
661 /* The Poseidon has hardwired temp limits, fill these
662 in for the alarm resetting code */
663 data->temp_max[0] = 70 + 128;
664 data->temp_max[1] = 50 + 128;
665 data->temp_max[2] = 50 + 128;
668 /* Read the special DMI table for fscher and newer chips */
669 if (kind == fscher || kind >= fschrc) {
670 dmi_walk(fschmd_dmi_decode);
671 if (dmi_vref == -1) {
672 printk(KERN_WARNING FSCHMD_NAME
673 ": Couldn't get voltage scaling factors from "
674 "BIOS DMI table, using builtin defaults\n");
675 dmi_vref = 33;
679 /* i2c kind goes from 1-5, we want from 0-4 to address arrays */
680 data->kind = kind - 1;
681 strlcpy(client->name, client_names[data->kind], I2C_NAME_SIZE);
683 /* Tell the I2C layer a new client has arrived */
684 if ((err = i2c_attach_client(client)))
685 goto exit_free;
687 for (i = 0; i < ARRAY_SIZE(fschmd_attr); i++) {
688 err = device_create_file(&client->dev,
689 &fschmd_attr[i].dev_attr);
690 if (err)
691 goto exit_detach;
694 for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++) {
695 /* Poseidon doesn't have TEMP_LIMIT registers */
696 if (kind == fscpos && fschmd_temp_attr[i].dev_attr.show ==
697 show_temp_max)
698 continue;
700 err = device_create_file(&client->dev,
701 &fschmd_temp_attr[i].dev_attr);
702 if (err)
703 goto exit_detach;
706 for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++) {
707 /* Poseidon doesn't have a FAN_MIN register for its 3rd fan */
708 if (kind == fscpos &&
709 !strcmp(fschmd_fan_attr[i].dev_attr.attr.name,
710 "pwm3_auto_point1_pwm"))
711 continue;
713 err = device_create_file(&client->dev,
714 &fschmd_fan_attr[i].dev_attr);
715 if (err)
716 goto exit_detach;
719 data->hwmon_dev = hwmon_device_register(&client->dev);
720 if (IS_ERR(data->hwmon_dev)) {
721 err = PTR_ERR(data->hwmon_dev);
722 data->hwmon_dev = NULL;
723 goto exit_detach;
726 revision = i2c_smbus_read_byte_data(client, FSCHMD_REG_REVISION);
727 printk(KERN_INFO FSCHMD_NAME ": Detected FSC %s chip, revision: %d\n",
728 names[data->kind], (int) revision);
730 return 0;
732 exit_detach:
733 fschmd_detach_client(client); /* will also free data for us */
734 return err;
736 exit_free:
737 kfree(data);
738 return err;
741 static int fschmd_attach_adapter(struct i2c_adapter *adapter)
743 if (!(adapter->class & I2C_CLASS_HWMON))
744 return 0;
745 return i2c_probe(adapter, &addr_data, fschmd_detect);
748 static int fschmd_detach_client(struct i2c_client *client)
750 struct fschmd_data *data = i2c_get_clientdata(client);
751 int i, err;
753 /* Check if registered in case we're called from fschmd_detect
754 to cleanup after an error */
755 if (data->hwmon_dev)
756 hwmon_device_unregister(data->hwmon_dev);
758 for (i = 0; i < ARRAY_SIZE(fschmd_attr); i++)
759 device_remove_file(&client->dev, &fschmd_attr[i].dev_attr);
760 for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++)
761 device_remove_file(&client->dev,
762 &fschmd_temp_attr[i].dev_attr);
763 for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++)
764 device_remove_file(&client->dev,
765 &fschmd_fan_attr[i].dev_attr);
767 if ((err = i2c_detach_client(client)))
768 return err;
770 kfree(data);
771 return 0;
774 static struct fschmd_data *fschmd_update_device(struct device *dev)
776 struct i2c_client *client = to_i2c_client(dev);
777 struct fschmd_data *data = i2c_get_clientdata(client);
778 int i;
780 mutex_lock(&data->update_lock);
782 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
784 for (i = 0; i < FSCHMD_NO_TEMP_SENSORS[data->kind]; i++) {
785 data->temp_act[i] = i2c_smbus_read_byte_data(client,
786 FSCHMD_REG_TEMP_ACT[data->kind][i]);
787 data->temp_status[i] = i2c_smbus_read_byte_data(client,
788 FSCHMD_REG_TEMP_STATE[data->kind][i]);
790 /* The fscpos doesn't have TEMP_LIMIT registers */
791 if (FSCHMD_REG_TEMP_LIMIT[data->kind][i])
792 data->temp_max[i] = i2c_smbus_read_byte_data(
793 client,
794 FSCHMD_REG_TEMP_LIMIT[data->kind][i]);
796 /* reset alarm if the alarm condition is gone,
797 the chip doesn't do this itself */
798 if ((data->temp_status[i] & FSCHMD_TEMP_ALARM_MASK) ==
799 FSCHMD_TEMP_ALARM_MASK &&
800 data->temp_act[i] < data->temp_max[i])
801 i2c_smbus_write_byte_data(client,
802 FSCHMD_REG_TEMP_STATE[data->kind][i],
803 FSCHMD_TEMP_ALERT_MASK);
806 for (i = 0; i < FSCHMD_NO_FAN_SENSORS[data->kind]; i++) {
807 data->fan_act[i] = i2c_smbus_read_byte_data(client,
808 FSCHMD_REG_FAN_ACT[data->kind][i]);
809 data->fan_status[i] = i2c_smbus_read_byte_data(client,
810 FSCHMD_REG_FAN_STATE[data->kind][i]);
811 data->fan_ripple[i] = i2c_smbus_read_byte_data(client,
812 FSCHMD_REG_FAN_RIPPLE[data->kind][i]);
814 /* The fscpos third fan doesn't have a fan_min */
815 if (FSCHMD_REG_FAN_MIN[data->kind][i])
816 data->fan_min[i] = i2c_smbus_read_byte_data(
817 client,
818 FSCHMD_REG_FAN_MIN[data->kind][i]);
820 /* reset fan status if speed is back to > 0 */
821 if ((data->fan_status[i] & FSCHMD_FAN_ALARM_MASK) &&
822 data->fan_act[i])
823 i2c_smbus_write_byte_data(client,
824 FSCHMD_REG_FAN_STATE[data->kind][i],
825 FSCHMD_FAN_ALARM_MASK);
828 for (i = 0; i < 3; i++)
829 data->volt[i] = i2c_smbus_read_byte_data(client,
830 FSCHMD_REG_VOLT[i]);
832 data->global_control = i2c_smbus_read_byte_data(client,
833 FSCHMD_REG_CONTROL);
835 /* To be implemented in the future
836 data->watchdog[0] = i2c_smbus_read_byte_data(client,
837 FSCHMD_REG_WDOG_PRESET);
838 data->watchdog[1] = i2c_smbus_read_byte_data(client,
839 FSCHMD_REG_WDOG_STATE);
840 data->watchdog[2] = i2c_smbus_read_byte_data(client,
841 FSCHMD_REG_WDOG_CONTROL); */
843 data->last_updated = jiffies;
844 data->valid = 1;
847 mutex_unlock(&data->update_lock);
849 return data;
852 static int __init fschmd_init(void)
854 return i2c_add_driver(&fschmd_driver);
857 static void __exit fschmd_exit(void)
859 i2c_del_driver(&fschmd_driver);
862 MODULE_AUTHOR("Hans de Goede <j.w.r.degoede@hhs.nl>");
863 MODULE_DESCRIPTION("FSC Poseidon, Hermes, Scylla, Heracles and "
864 "Heimdall driver");
865 MODULE_LICENSE("GPL");
867 module_init(fschmd_init);
868 module_exit(fschmd_exit);