hwmon: (fschmd) Cleanups for watchdog support
[deliverable/linux.git] / drivers / hwmon / fschmd.c
CommitLineData
569ff102
HG
1/* fschmd.c
2 *
453e308d 3 * Copyright (C) 2007,2008 Hans de Goede <hdegoede@redhat.com>
569ff102
HG
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>
7845cd79 44#include <linux/dmi.h>
569ff102
HG
45
46/* Addresses to scan */
25e9c86d 47static const unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
569ff102
HG
48
49/* Insmod parameters */
50I2C_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
453e308d 66#define FSCHMD_CONTROL_ALERT_LED 0x01
569ff102
HG
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 */
74static 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. */
80static 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 */
89static 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 */
98static 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 */
107static 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
115static const int FSCHMD_NO_FAN_SENSORS[5] = { 3, 3, 6, 4, 5 };
116
117/* Fan status register bitmasks */
453e308d
HG
118#define FSCHMD_FAN_ALARM 0x04 /* called fault by FSC! */
119#define FSCHMD_FAN_NOT_PRESENT 0x08 /* not documented */
569ff102
HG
120
121
122/* actual temperature registers */
123static 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 */
132static 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 */
7dcf9a31 137 { 0x71, 0x81, 0x91, 0xd1, 0xe1 }, /* hmd */
569ff102
HG
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?? */
145static 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 */
7dcf9a31 150 { 0x76, 0x86, 0x96, 0xd6, 0xe6 }, /* hmd */
569ff102
HG
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.
155static const u8 FSCHER_REG_TEMP_AUTOP1[] = { 0x73, 0x83, 0x93 };
156static const u8 FSCHER_REG_TEMP_AUTOP2[] = { 0x75, 0x85, 0x95 }; */
157
158static const int FSCHMD_NO_TEMP_SENSORS[5] = { 3, 3, 4, 3, 5 };
159
160/* temp status register bitmasks */
453e308d
HG
161#define FSCHMD_TEMP_WORKING 0x01
162#define FSCHMD_TEMP_ALERT 0x02
569ff102
HG
163/* there only really is an alarm if the sensor is working and alert == 1 */
164#define FSCHMD_TEMP_ALARM_MASK \
453e308d 165 (FSCHMD_TEMP_WORKING | FSCHMD_TEMP_ALERT)
569ff102
HG
166
167/*
168 * Functions declarations
169 */
170
40ac1994
JD
171static int fschmd_probe(struct i2c_client *client,
172 const struct i2c_device_id *id);
173static int fschmd_detect(struct i2c_client *client, int kind,
174 struct i2c_board_info *info);
175static int fschmd_remove(struct i2c_client *client);
569ff102
HG
176static struct fschmd_data *fschmd_update_device(struct device *dev);
177
178/*
179 * Driver data (common to all clients)
180 */
181
40ac1994
JD
182static const struct i2c_device_id fschmd_id[] = {
183 { "fscpos", fscpos },
184 { "fscher", fscher },
185 { "fscscy", fscscy },
186 { "fschrc", fschrc },
187 { "fschmd", fschmd },
188 { }
189};
190MODULE_DEVICE_TABLE(i2c, fschmd_id);
191
569ff102 192static struct i2c_driver fschmd_driver = {
40ac1994 193 .class = I2C_CLASS_HWMON,
569ff102 194 .driver = {
453e308d 195 .name = "fschmd",
569ff102 196 },
40ac1994
JD
197 .probe = fschmd_probe,
198 .remove = fschmd_remove,
199 .id_table = fschmd_id,
200 .detect = fschmd_detect,
201 .address_data = &addr_data,
569ff102
HG
202};
203
204/*
205 * Client data (each client gets its own)
206 */
207
208struct fschmd_data {
569ff102
HG
209 struct device *hwmon_dev;
210 struct mutex update_lock;
211 int kind;
212 char valid; /* zero until following fields are valid */
213 unsigned long last_updated; /* in jiffies */
214
215 /* register values */
216 u8 global_control; /* global control register */
217 u8 volt[3]; /* 12, 5, battery voltage */
218 u8 temp_act[5]; /* temperature */
219 u8 temp_status[5]; /* status of sensor */
220 u8 temp_max[5]; /* high temp limit, notice: undocumented! */
221 u8 fan_act[6]; /* fans revolutions per second */
222 u8 fan_status[6]; /* fan status */
223 u8 fan_min[6]; /* fan min value for rps */
224 u8 fan_ripple[6]; /* divider for rps */
225};
226
7845cd79
HG
227/* Global variables to hold information read from special DMI tables, which are
228 available on FSC machines with an fscher or later chip. */
229static int dmi_mult[3] = { 490, 200, 100 };
230static int dmi_offset[3] = { 0, 0, 0 };
231static int dmi_vref = -1;
232
233
569ff102
HG
234/*
235 * Sysfs attr show / store functions
236 */
237
238static ssize_t show_in_value(struct device *dev,
239 struct device_attribute *devattr, char *buf)
240{
241 const int max_reading[3] = { 14200, 6600, 3300 };
242 int index = to_sensor_dev_attr(devattr)->index;
243 struct fschmd_data *data = fschmd_update_device(dev);
244
7845cd79
HG
245 /* fscher / fschrc - 1 as data->kind is an array index, not a chips */
246 if (data->kind == (fscher - 1) || data->kind >= (fschrc - 1))
247 return sprintf(buf, "%d\n", (data->volt[index] * dmi_vref *
248 dmi_mult[index]) / 255 + dmi_offset[index]);
249 else
250 return sprintf(buf, "%d\n", (data->volt[index] *
251 max_reading[index] + 128) / 255);
569ff102
HG
252}
253
254
255#define TEMP_FROM_REG(val) (((val) - 128) * 1000)
256
257static ssize_t show_temp_value(struct device *dev,
258 struct device_attribute *devattr, char *buf)
259{
260 int index = to_sensor_dev_attr(devattr)->index;
261 struct fschmd_data *data = fschmd_update_device(dev);
262
263 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_act[index]));
264}
265
266static ssize_t show_temp_max(struct device *dev,
267 struct device_attribute *devattr, char *buf)
268{
269 int index = to_sensor_dev_attr(devattr)->index;
270 struct fschmd_data *data = fschmd_update_device(dev);
271
272 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[index]));
273}
274
275static ssize_t store_temp_max(struct device *dev, struct device_attribute
276 *devattr, const char *buf, size_t count)
277{
278 int index = to_sensor_dev_attr(devattr)->index;
279 struct fschmd_data *data = dev_get_drvdata(dev);
280 long v = simple_strtol(buf, NULL, 10) / 1000;
281
282 v = SENSORS_LIMIT(v, -128, 127) + 128;
283
284 mutex_lock(&data->update_lock);
40ac1994 285 i2c_smbus_write_byte_data(to_i2c_client(dev),
569ff102
HG
286 FSCHMD_REG_TEMP_LIMIT[data->kind][index], v);
287 data->temp_max[index] = v;
288 mutex_unlock(&data->update_lock);
289
290 return count;
291}
292
293static ssize_t show_temp_fault(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 /* bit 0 set means sensor working ok, so no fault! */
453e308d 300 if (data->temp_status[index] & FSCHMD_TEMP_WORKING)
569ff102
HG
301 return sprintf(buf, "0\n");
302 else
303 return sprintf(buf, "1\n");
304}
305
306static ssize_t show_temp_alarm(struct device *dev,
307 struct device_attribute *devattr, char *buf)
308{
309 int index = to_sensor_dev_attr(devattr)->index;
310 struct fschmd_data *data = fschmd_update_device(dev);
311
312 if ((data->temp_status[index] & FSCHMD_TEMP_ALARM_MASK) ==
313 FSCHMD_TEMP_ALARM_MASK)
314 return sprintf(buf, "1\n");
315 else
316 return sprintf(buf, "0\n");
317}
318
319
320#define RPM_FROM_REG(val) ((val) * 60)
321
322static ssize_t show_fan_value(struct device *dev,
323 struct device_attribute *devattr, char *buf)
324{
325 int index = to_sensor_dev_attr(devattr)->index;
326 struct fschmd_data *data = fschmd_update_device(dev);
327
328 return sprintf(buf, "%u\n", RPM_FROM_REG(data->fan_act[index]));
329}
330
331static ssize_t show_fan_div(struct device *dev,
332 struct device_attribute *devattr, char *buf)
333{
334 int index = to_sensor_dev_attr(devattr)->index;
335 struct fschmd_data *data = fschmd_update_device(dev);
336
337 /* bits 2..7 reserved => mask with 3 */
338 return sprintf(buf, "%d\n", 1 << (data->fan_ripple[index] & 3));
339}
340
341static ssize_t store_fan_div(struct device *dev, struct device_attribute
342 *devattr, const char *buf, size_t count)
343{
344 u8 reg;
345 int index = to_sensor_dev_attr(devattr)->index;
346 struct fschmd_data *data = dev_get_drvdata(dev);
347 /* supported values: 2, 4, 8 */
348 unsigned long v = simple_strtoul(buf, NULL, 10);
349
350 switch (v) {
351 case 2: v = 1; break;
352 case 4: v = 2; break;
353 case 8: v = 3; break;
354 default:
355 dev_err(dev, "fan_div value %lu not supported. "
356 "Choose one of 2, 4 or 8!\n", v);
357 return -EINVAL;
358 }
359
360 mutex_lock(&data->update_lock);
361
40ac1994 362 reg = i2c_smbus_read_byte_data(to_i2c_client(dev),
569ff102
HG
363 FSCHMD_REG_FAN_RIPPLE[data->kind][index]);
364
365 /* bits 2..7 reserved => mask with 0x03 */
366 reg &= ~0x03;
367 reg |= v;
368
40ac1994 369 i2c_smbus_write_byte_data(to_i2c_client(dev),
569ff102
HG
370 FSCHMD_REG_FAN_RIPPLE[data->kind][index], reg);
371
372 data->fan_ripple[index] = reg;
373
374 mutex_unlock(&data->update_lock);
375
376 return count;
377}
378
379static ssize_t show_fan_alarm(struct device *dev,
380 struct device_attribute *devattr, char *buf)
381{
382 int index = to_sensor_dev_attr(devattr)->index;
383 struct fschmd_data *data = fschmd_update_device(dev);
384
453e308d 385 if (data->fan_status[index] & FSCHMD_FAN_ALARM)
569ff102
HG
386 return sprintf(buf, "1\n");
387 else
388 return sprintf(buf, "0\n");
389}
390
391static ssize_t show_fan_fault(struct device *dev,
392 struct device_attribute *devattr, char *buf)
393{
394 int index = to_sensor_dev_attr(devattr)->index;
395 struct fschmd_data *data = fschmd_update_device(dev);
396
453e308d 397 if (data->fan_status[index] & FSCHMD_FAN_NOT_PRESENT)
569ff102
HG
398 return sprintf(buf, "1\n");
399 else
400 return sprintf(buf, "0\n");
401}
402
403
404static ssize_t show_pwm_auto_point1_pwm(struct device *dev,
405 struct device_attribute *devattr, char *buf)
406{
407 int index = to_sensor_dev_attr(devattr)->index;
408 int val = fschmd_update_device(dev)->fan_min[index];
409
410 /* 0 = allow turning off, 1-255 = 50-100% */
411 if (val)
412 val = val / 2 + 128;
413
414 return sprintf(buf, "%d\n", val);
415}
416
417static ssize_t store_pwm_auto_point1_pwm(struct device *dev,
418 struct device_attribute *devattr, const char *buf, size_t count)
419{
420 int index = to_sensor_dev_attr(devattr)->index;
421 struct fschmd_data *data = dev_get_drvdata(dev);
422 unsigned long v = simple_strtoul(buf, NULL, 10);
423
424 /* register: 0 = allow turning off, 1-255 = 50-100% */
425 if (v) {
426 v = SENSORS_LIMIT(v, 128, 255);
427 v = (v - 128) * 2 + 1;
428 }
429
430 mutex_lock(&data->update_lock);
431
40ac1994 432 i2c_smbus_write_byte_data(to_i2c_client(dev),
569ff102
HG
433 FSCHMD_REG_FAN_MIN[data->kind][index], v);
434 data->fan_min[index] = v;
435
436 mutex_unlock(&data->update_lock);
437
438 return count;
439}
440
441
442/* The FSC hwmon family has the ability to force an attached alert led to flash
443 from software, we export this as an alert_led sysfs attr */
444static ssize_t show_alert_led(struct device *dev,
445 struct device_attribute *devattr, char *buf)
446{
447 struct fschmd_data *data = fschmd_update_device(dev);
448
453e308d 449 if (data->global_control & FSCHMD_CONTROL_ALERT_LED)
569ff102
HG
450 return sprintf(buf, "1\n");
451 else
452 return sprintf(buf, "0\n");
453}
454
455static ssize_t store_alert_led(struct device *dev,
456 struct device_attribute *devattr, const char *buf, size_t count)
457{
458 u8 reg;
459 struct fschmd_data *data = dev_get_drvdata(dev);
460 unsigned long v = simple_strtoul(buf, NULL, 10);
461
462 mutex_lock(&data->update_lock);
463
40ac1994 464 reg = i2c_smbus_read_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL);
569ff102
HG
465
466 if (v)
453e308d 467 reg |= FSCHMD_CONTROL_ALERT_LED;
569ff102 468 else
453e308d 469 reg &= ~FSCHMD_CONTROL_ALERT_LED;
569ff102 470
40ac1994 471 i2c_smbus_write_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL, reg);
569ff102
HG
472
473 data->global_control = reg;
474
475 mutex_unlock(&data->update_lock);
476
477 return count;
478}
479
480static struct sensor_device_attribute fschmd_attr[] = {
481 SENSOR_ATTR(in0_input, 0444, show_in_value, NULL, 0),
482 SENSOR_ATTR(in1_input, 0444, show_in_value, NULL, 1),
483 SENSOR_ATTR(in2_input, 0444, show_in_value, NULL, 2),
484 SENSOR_ATTR(alert_led, 0644, show_alert_led, store_alert_led, 0),
485};
486
487static struct sensor_device_attribute fschmd_temp_attr[] = {
488 SENSOR_ATTR(temp1_input, 0444, show_temp_value, NULL, 0),
489 SENSOR_ATTR(temp1_max, 0644, show_temp_max, store_temp_max, 0),
490 SENSOR_ATTR(temp1_fault, 0444, show_temp_fault, NULL, 0),
491 SENSOR_ATTR(temp1_alarm, 0444, show_temp_alarm, NULL, 0),
492 SENSOR_ATTR(temp2_input, 0444, show_temp_value, NULL, 1),
493 SENSOR_ATTR(temp2_max, 0644, show_temp_max, store_temp_max, 1),
494 SENSOR_ATTR(temp2_fault, 0444, show_temp_fault, NULL, 1),
495 SENSOR_ATTR(temp2_alarm, 0444, show_temp_alarm, NULL, 1),
496 SENSOR_ATTR(temp3_input, 0444, show_temp_value, NULL, 2),
497 SENSOR_ATTR(temp3_max, 0644, show_temp_max, store_temp_max, 2),
498 SENSOR_ATTR(temp3_fault, 0444, show_temp_fault, NULL, 2),
499 SENSOR_ATTR(temp3_alarm, 0444, show_temp_alarm, NULL, 2),
500 SENSOR_ATTR(temp4_input, 0444, show_temp_value, NULL, 3),
501 SENSOR_ATTR(temp4_max, 0644, show_temp_max, store_temp_max, 3),
502 SENSOR_ATTR(temp4_fault, 0444, show_temp_fault, NULL, 3),
503 SENSOR_ATTR(temp4_alarm, 0444, show_temp_alarm, NULL, 3),
504 SENSOR_ATTR(temp5_input, 0444, show_temp_value, NULL, 4),
505 SENSOR_ATTR(temp5_max, 0644, show_temp_max, store_temp_max, 4),
506 SENSOR_ATTR(temp5_fault, 0444, show_temp_fault, NULL, 4),
507 SENSOR_ATTR(temp5_alarm, 0444, show_temp_alarm, NULL, 4),
508};
509
510static struct sensor_device_attribute fschmd_fan_attr[] = {
511 SENSOR_ATTR(fan1_input, 0444, show_fan_value, NULL, 0),
512 SENSOR_ATTR(fan1_div, 0644, show_fan_div, store_fan_div, 0),
513 SENSOR_ATTR(fan1_alarm, 0444, show_fan_alarm, NULL, 0),
514 SENSOR_ATTR(fan1_fault, 0444, show_fan_fault, NULL, 0),
515 SENSOR_ATTR(pwm1_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
516 store_pwm_auto_point1_pwm, 0),
517 SENSOR_ATTR(fan2_input, 0444, show_fan_value, NULL, 1),
518 SENSOR_ATTR(fan2_div, 0644, show_fan_div, store_fan_div, 1),
519 SENSOR_ATTR(fan2_alarm, 0444, show_fan_alarm, NULL, 1),
520 SENSOR_ATTR(fan2_fault, 0444, show_fan_fault, NULL, 1),
521 SENSOR_ATTR(pwm2_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
522 store_pwm_auto_point1_pwm, 1),
523 SENSOR_ATTR(fan3_input, 0444, show_fan_value, NULL, 2),
524 SENSOR_ATTR(fan3_div, 0644, show_fan_div, store_fan_div, 2),
525 SENSOR_ATTR(fan3_alarm, 0444, show_fan_alarm, NULL, 2),
526 SENSOR_ATTR(fan3_fault, 0444, show_fan_fault, NULL, 2),
527 SENSOR_ATTR(pwm3_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
528 store_pwm_auto_point1_pwm, 2),
529 SENSOR_ATTR(fan4_input, 0444, show_fan_value, NULL, 3),
530 SENSOR_ATTR(fan4_div, 0644, show_fan_div, store_fan_div, 3),
531 SENSOR_ATTR(fan4_alarm, 0444, show_fan_alarm, NULL, 3),
532 SENSOR_ATTR(fan4_fault, 0444, show_fan_fault, NULL, 3),
533 SENSOR_ATTR(pwm4_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
534 store_pwm_auto_point1_pwm, 3),
535 SENSOR_ATTR(fan5_input, 0444, show_fan_value, NULL, 4),
536 SENSOR_ATTR(fan5_div, 0644, show_fan_div, store_fan_div, 4),
537 SENSOR_ATTR(fan5_alarm, 0444, show_fan_alarm, NULL, 4),
538 SENSOR_ATTR(fan5_fault, 0444, show_fan_fault, NULL, 4),
539 SENSOR_ATTR(pwm5_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
540 store_pwm_auto_point1_pwm, 4),
541 SENSOR_ATTR(fan6_input, 0444, show_fan_value, NULL, 5),
542 SENSOR_ATTR(fan6_div, 0644, show_fan_div, store_fan_div, 5),
543 SENSOR_ATTR(fan6_alarm, 0444, show_fan_alarm, NULL, 5),
544 SENSOR_ATTR(fan6_fault, 0444, show_fan_fault, NULL, 5),
545 SENSOR_ATTR(pwm6_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
546 store_pwm_auto_point1_pwm, 5),
547};
548
549
550/*
551 * Real code
552 */
553
7845cd79
HG
554/* DMI decode routine to read voltage scaling factors from special DMI tables,
555 which are available on FSC machines with an fscher or later chip. */
556static void fschmd_dmi_decode(const struct dmi_header *header)
557{
558 int i, mult[3] = { 0 }, offset[3] = { 0 }, vref = 0, found = 0;
559
560 /* dmi code ugliness, we get passed the address of the contents of
561 a complete DMI record, but in the form of a dmi_header pointer, in
562 reality this address holds header->length bytes of which the header
563 are the first 4 bytes */
564 u8 *dmi_data = (u8 *)header;
565
566 /* We are looking for OEM-specific type 185 */
567 if (header->type != 185)
568 return;
569
570 /* we are looking for what Siemens calls "subtype" 19, the subtype
571 is stored in byte 5 of the dmi block */
572 if (header->length < 5 || dmi_data[4] != 19)
573 return;
574
575 /* After the subtype comes 1 unknown byte and then blocks of 5 bytes,
576 consisting of what Siemens calls an "Entity" number, followed by
577 2 16-bit words in LSB first order */
578 for (i = 6; (i + 4) < header->length; i += 5) {
579 /* entity 1 - 3: voltage multiplier and offset */
580 if (dmi_data[i] >= 1 && dmi_data[i] <= 3) {
581 /* Our in sensors order and the DMI order differ */
582 const int shuffle[3] = { 1, 0, 2 };
583 int in = shuffle[dmi_data[i] - 1];
584
585 /* Check for twice the same entity */
586 if (found & (1 << in))
587 return;
588
589 mult[in] = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
590 offset[in] = dmi_data[i + 3] | (dmi_data[i + 4] << 8);
591
592 found |= 1 << in;
593 }
594
595 /* entity 7: reference voltage */
596 if (dmi_data[i] == 7) {
597 /* Check for twice the same entity */
598 if (found & 0x08)
599 return;
600
601 vref = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
602
603 found |= 0x08;
604 }
605 }
606
607 if (found == 0x0F) {
608 for (i = 0; i < 3; i++) {
609 dmi_mult[i] = mult[i] * 10;
610 dmi_offset[i] = offset[i] * 10;
611 }
612 dmi_vref = vref;
613 }
614}
615
40ac1994
JD
616static int fschmd_detect(struct i2c_client *client, int kind,
617 struct i2c_board_info *info)
569ff102 618{
40ac1994 619 struct i2c_adapter *adapter = client->adapter;
569ff102
HG
620 const char * const client_names[5] = { "fscpos", "fscher", "fscscy",
621 "fschrc", "fschmd" };
569ff102
HG
622
623 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
40ac1994 624 return -ENODEV;
569ff102
HG
625
626 /* Detect & Identify the chip */
627 if (kind <= 0) {
628 char id[4];
629
630 id[0] = i2c_smbus_read_byte_data(client,
631 FSCHMD_REG_IDENT_0);
632 id[1] = i2c_smbus_read_byte_data(client,
633 FSCHMD_REG_IDENT_1);
634 id[2] = i2c_smbus_read_byte_data(client,
635 FSCHMD_REG_IDENT_2);
636 id[3] = '\0';
637
638 if (!strcmp(id, "PEG"))
639 kind = fscpos;
640 else if (!strcmp(id, "HER"))
641 kind = fscher;
642 else if (!strcmp(id, "SCY"))
643 kind = fscscy;
644 else if (!strcmp(id, "HRC"))
645 kind = fschrc;
646 else if (!strcmp(id, "HMD"))
647 kind = fschmd;
648 else
40ac1994 649 return -ENODEV;
569ff102
HG
650 }
651
40ac1994
JD
652 strlcpy(info->type, client_names[kind - 1], I2C_NAME_SIZE);
653
654 return 0;
655}
656
657static int fschmd_probe(struct i2c_client *client,
658 const struct i2c_device_id *id)
659{
660 struct fschmd_data *data;
661 u8 revision;
662 const char * const names[5] = { "Poseidon", "Hermes", "Scylla",
663 "Heracles", "Heimdall" };
664 int i, err;
665 enum chips kind = id->driver_data;
666
667 data = kzalloc(sizeof(struct fschmd_data), GFP_KERNEL);
668 if (!data)
669 return -ENOMEM;
670
671 i2c_set_clientdata(client, data);
672 mutex_init(&data->update_lock);
673
569ff102
HG
674 if (kind == fscpos) {
675 /* The Poseidon has hardwired temp limits, fill these
676 in for the alarm resetting code */
677 data->temp_max[0] = 70 + 128;
678 data->temp_max[1] = 50 + 128;
679 data->temp_max[2] = 50 + 128;
680 }
681
7845cd79 682 /* Read the special DMI table for fscher and newer chips */
453e308d 683 if ((kind == fscher || kind >= fschrc) && dmi_vref == -1) {
7845cd79
HG
684 dmi_walk(fschmd_dmi_decode);
685 if (dmi_vref == -1) {
453e308d
HG
686 dev_warn(&client->dev,
687 "Couldn't get voltage scaling factors from "
7845cd79
HG
688 "BIOS DMI table, using builtin defaults\n");
689 dmi_vref = 33;
690 }
691 }
692
569ff102
HG
693 /* i2c kind goes from 1-5, we want from 0-4 to address arrays */
694 data->kind = kind - 1;
569ff102
HG
695
696 for (i = 0; i < ARRAY_SIZE(fschmd_attr); i++) {
697 err = device_create_file(&client->dev,
698 &fschmd_attr[i].dev_attr);
699 if (err)
700 goto exit_detach;
701 }
702
703 for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++) {
704 /* Poseidon doesn't have TEMP_LIMIT registers */
705 if (kind == fscpos && fschmd_temp_attr[i].dev_attr.show ==
706 show_temp_max)
707 continue;
708
709 err = device_create_file(&client->dev,
710 &fschmd_temp_attr[i].dev_attr);
711 if (err)
712 goto exit_detach;
713 }
714
715 for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++) {
716 /* Poseidon doesn't have a FAN_MIN register for its 3rd fan */
717 if (kind == fscpos &&
718 !strcmp(fschmd_fan_attr[i].dev_attr.attr.name,
719 "pwm3_auto_point1_pwm"))
720 continue;
721
722 err = device_create_file(&client->dev,
723 &fschmd_fan_attr[i].dev_attr);
724 if (err)
725 goto exit_detach;
726 }
727
728 data->hwmon_dev = hwmon_device_register(&client->dev);
729 if (IS_ERR(data->hwmon_dev)) {
730 err = PTR_ERR(data->hwmon_dev);
731 data->hwmon_dev = NULL;
732 goto exit_detach;
733 }
734
735 revision = i2c_smbus_read_byte_data(client, FSCHMD_REG_REVISION);
453e308d 736 dev_info(&client->dev, "Detected FSC %s chip, revision: %d\n",
569ff102
HG
737 names[data->kind], (int) revision);
738
739 return 0;
740
741exit_detach:
40ac1994 742 fschmd_remove(client); /* will also free data for us */
569ff102
HG
743 return err;
744}
745
40ac1994 746static int fschmd_remove(struct i2c_client *client)
569ff102
HG
747{
748 struct fschmd_data *data = i2c_get_clientdata(client);
40ac1994 749 int i;
569ff102
HG
750
751 /* Check if registered in case we're called from fschmd_detect
752 to cleanup after an error */
753 if (data->hwmon_dev)
754 hwmon_device_unregister(data->hwmon_dev);
755
756 for (i = 0; i < ARRAY_SIZE(fschmd_attr); i++)
757 device_remove_file(&client->dev, &fschmd_attr[i].dev_attr);
758 for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++)
759 device_remove_file(&client->dev,
760 &fschmd_temp_attr[i].dev_attr);
761 for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++)
762 device_remove_file(&client->dev,
763 &fschmd_fan_attr[i].dev_attr);
764
569ff102
HG
765 kfree(data);
766 return 0;
767}
768
769static struct fschmd_data *fschmd_update_device(struct device *dev)
770{
771 struct i2c_client *client = to_i2c_client(dev);
772 struct fschmd_data *data = i2c_get_clientdata(client);
773 int i;
774
775 mutex_lock(&data->update_lock);
776
777 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
778
779 for (i = 0; i < FSCHMD_NO_TEMP_SENSORS[data->kind]; i++) {
780 data->temp_act[i] = i2c_smbus_read_byte_data(client,
781 FSCHMD_REG_TEMP_ACT[data->kind][i]);
782 data->temp_status[i] = i2c_smbus_read_byte_data(client,
783 FSCHMD_REG_TEMP_STATE[data->kind][i]);
784
785 /* The fscpos doesn't have TEMP_LIMIT registers */
786 if (FSCHMD_REG_TEMP_LIMIT[data->kind][i])
787 data->temp_max[i] = i2c_smbus_read_byte_data(
788 client,
789 FSCHMD_REG_TEMP_LIMIT[data->kind][i]);
790
791 /* reset alarm if the alarm condition is gone,
792 the chip doesn't do this itself */
793 if ((data->temp_status[i] & FSCHMD_TEMP_ALARM_MASK) ==
794 FSCHMD_TEMP_ALARM_MASK &&
795 data->temp_act[i] < data->temp_max[i])
796 i2c_smbus_write_byte_data(client,
797 FSCHMD_REG_TEMP_STATE[data->kind][i],
453e308d 798 FSCHMD_TEMP_ALERT);
569ff102
HG
799 }
800
801 for (i = 0; i < FSCHMD_NO_FAN_SENSORS[data->kind]; i++) {
802 data->fan_act[i] = i2c_smbus_read_byte_data(client,
803 FSCHMD_REG_FAN_ACT[data->kind][i]);
804 data->fan_status[i] = i2c_smbus_read_byte_data(client,
805 FSCHMD_REG_FAN_STATE[data->kind][i]);
806 data->fan_ripple[i] = i2c_smbus_read_byte_data(client,
807 FSCHMD_REG_FAN_RIPPLE[data->kind][i]);
808
809 /* The fscpos third fan doesn't have a fan_min */
810 if (FSCHMD_REG_FAN_MIN[data->kind][i])
811 data->fan_min[i] = i2c_smbus_read_byte_data(
812 client,
813 FSCHMD_REG_FAN_MIN[data->kind][i]);
814
815 /* reset fan status if speed is back to > 0 */
453e308d 816 if ((data->fan_status[i] & FSCHMD_FAN_ALARM) &&
569ff102
HG
817 data->fan_act[i])
818 i2c_smbus_write_byte_data(client,
819 FSCHMD_REG_FAN_STATE[data->kind][i],
453e308d 820 FSCHMD_FAN_ALARM);
569ff102
HG
821 }
822
823 for (i = 0; i < 3; i++)
824 data->volt[i] = i2c_smbus_read_byte_data(client,
825 FSCHMD_REG_VOLT[i]);
826
827 data->global_control = i2c_smbus_read_byte_data(client,
828 FSCHMD_REG_CONTROL);
829
830 /* To be implemented in the future
831 data->watchdog[0] = i2c_smbus_read_byte_data(client,
832 FSCHMD_REG_WDOG_PRESET);
833 data->watchdog[1] = i2c_smbus_read_byte_data(client,
834 FSCHMD_REG_WDOG_STATE);
835 data->watchdog[2] = i2c_smbus_read_byte_data(client,
836 FSCHMD_REG_WDOG_CONTROL); */
837
838 data->last_updated = jiffies;
839 data->valid = 1;
840 }
841
842 mutex_unlock(&data->update_lock);
843
844 return data;
845}
846
847static int __init fschmd_init(void)
848{
849 return i2c_add_driver(&fschmd_driver);
850}
851
852static void __exit fschmd_exit(void)
853{
854 i2c_del_driver(&fschmd_driver);
855}
856
453e308d 857MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
569ff102
HG
858MODULE_DESCRIPTION("FSC Poseidon, Hermes, Scylla, Heracles and "
859 "Heimdall driver");
860MODULE_LICENSE("GPL");
861
862module_init(fschmd_init);
863module_exit(fschmd_exit);
This page took 0.57063 seconds and 5 git commands to generate.