Loading...
1/* fschmd.c
2 *
3 * Copyright (C) 2007 - 2009 Hans de Goede <hdegoede@redhat.com>
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, Heimdall, Hades and Syleus 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#include <linux/fs.h>
46#include <linux/watchdog.h>
47#include <linux/miscdevice.h>
48#include <linux/uaccess.h>
49#include <linux/kref.h>
50
51/* Addresses to scan */
52static const unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
53
54/* Insmod parameters */
55static int nowayout = WATCHDOG_NOWAYOUT;
56module_param(nowayout, int, 0);
57MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
58 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
59
60enum chips { fscpos, fscher, fscscy, fschrc, fschmd, fschds, fscsyl };
61
62/*
63 * The FSCHMD registers and other defines
64 */
65
66/* chip identification */
67#define FSCHMD_REG_IDENT_0 0x00
68#define FSCHMD_REG_IDENT_1 0x01
69#define FSCHMD_REG_IDENT_2 0x02
70#define FSCHMD_REG_REVISION 0x03
71
72/* global control and status */
73#define FSCHMD_REG_EVENT_STATE 0x04
74#define FSCHMD_REG_CONTROL 0x05
75
76#define FSCHMD_CONTROL_ALERT_LED 0x01
77
78/* watchdog */
79static const u8 FSCHMD_REG_WDOG_CONTROL[7] =
80 { 0x21, 0x21, 0x21, 0x21, 0x21, 0x28, 0x28 };
81static const u8 FSCHMD_REG_WDOG_STATE[7] =
82 { 0x23, 0x23, 0x23, 0x23, 0x23, 0x29, 0x29 };
83static const u8 FSCHMD_REG_WDOG_PRESET[7] =
84 { 0x28, 0x28, 0x28, 0x28, 0x28, 0x2a, 0x2a };
85
86#define FSCHMD_WDOG_CONTROL_TRIGGER 0x10
87#define FSCHMD_WDOG_CONTROL_STARTED 0x10 /* the same as trigger */
88#define FSCHMD_WDOG_CONTROL_STOP 0x20
89#define FSCHMD_WDOG_CONTROL_RESOLUTION 0x40
90
91#define FSCHMD_WDOG_STATE_CARDRESET 0x02
92
93/* voltages, weird order is to keep the same order as the old drivers */
94static const u8 FSCHMD_REG_VOLT[7][6] = {
95 { 0x45, 0x42, 0x48 }, /* pos */
96 { 0x45, 0x42, 0x48 }, /* her */
97 { 0x45, 0x42, 0x48 }, /* scy */
98 { 0x45, 0x42, 0x48 }, /* hrc */
99 { 0x45, 0x42, 0x48 }, /* hmd */
100 { 0x21, 0x20, 0x22 }, /* hds */
101 { 0x21, 0x20, 0x22, 0x23, 0x24, 0x25 }, /* syl */
102};
103
104static const int FSCHMD_NO_VOLT_SENSORS[7] = { 3, 3, 3, 3, 3, 3, 6 };
105
106/* minimum pwm at which the fan is driven (pwm can by increased depending on
107 the temp. Notice that for the scy some fans share there minimum speed.
108 Also notice that with the scy the sensor order is different than with the
109 other chips, this order was in the 2.4 driver and kept for consistency. */
110static const u8 FSCHMD_REG_FAN_MIN[7][7] = {
111 { 0x55, 0x65 }, /* pos */
112 { 0x55, 0x65, 0xb5 }, /* her */
113 { 0x65, 0x65, 0x55, 0xa5, 0x55, 0xa5 }, /* scy */
114 { 0x55, 0x65, 0xa5, 0xb5 }, /* hrc */
115 { 0x55, 0x65, 0xa5, 0xb5, 0xc5 }, /* hmd */
116 { 0x55, 0x65, 0xa5, 0xb5, 0xc5 }, /* hds */
117 { 0x54, 0x64, 0x74, 0x84, 0x94, 0xa4, 0xb4 }, /* syl */
118};
119
120/* actual fan speed */
121static const u8 FSCHMD_REG_FAN_ACT[7][7] = {
122 { 0x0e, 0x6b, 0xab }, /* pos */
123 { 0x0e, 0x6b, 0xbb }, /* her */
124 { 0x6b, 0x6c, 0x0e, 0xab, 0x5c, 0xbb }, /* scy */
125 { 0x0e, 0x6b, 0xab, 0xbb }, /* hrc */
126 { 0x5b, 0x6b, 0xab, 0xbb, 0xcb }, /* hmd */
127 { 0x5b, 0x6b, 0xab, 0xbb, 0xcb }, /* hds */
128 { 0x57, 0x67, 0x77, 0x87, 0x97, 0xa7, 0xb7 }, /* syl */
129};
130
131/* fan status registers */
132static const u8 FSCHMD_REG_FAN_STATE[7][7] = {
133 { 0x0d, 0x62, 0xa2 }, /* pos */
134 { 0x0d, 0x62, 0xb2 }, /* her */
135 { 0x62, 0x61, 0x0d, 0xa2, 0x52, 0xb2 }, /* scy */
136 { 0x0d, 0x62, 0xa2, 0xb2 }, /* hrc */
137 { 0x52, 0x62, 0xa2, 0xb2, 0xc2 }, /* hmd */
138 { 0x52, 0x62, 0xa2, 0xb2, 0xc2 }, /* hds */
139 { 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0 }, /* syl */
140};
141
142/* fan ripple / divider registers */
143static const u8 FSCHMD_REG_FAN_RIPPLE[7][7] = {
144 { 0x0f, 0x6f, 0xaf }, /* pos */
145 { 0x0f, 0x6f, 0xbf }, /* her */
146 { 0x6f, 0x6f, 0x0f, 0xaf, 0x0f, 0xbf }, /* scy */
147 { 0x0f, 0x6f, 0xaf, 0xbf }, /* hrc */
148 { 0x5f, 0x6f, 0xaf, 0xbf, 0xcf }, /* hmd */
149 { 0x5f, 0x6f, 0xaf, 0xbf, 0xcf }, /* hds */
150 { 0x56, 0x66, 0x76, 0x86, 0x96, 0xa6, 0xb6 }, /* syl */
151};
152
153static const int FSCHMD_NO_FAN_SENSORS[7] = { 3, 3, 6, 4, 5, 5, 7 };
154
155/* Fan status register bitmasks */
156#define FSCHMD_FAN_ALARM 0x04 /* called fault by FSC! */
157#define FSCHMD_FAN_NOT_PRESENT 0x08
158#define FSCHMD_FAN_DISABLED 0x80
159
160
161/* actual temperature registers */
162static const u8 FSCHMD_REG_TEMP_ACT[7][11] = {
163 { 0x64, 0x32, 0x35 }, /* pos */
164 { 0x64, 0x32, 0x35 }, /* her */
165 { 0x64, 0xD0, 0x32, 0x35 }, /* scy */
166 { 0x64, 0x32, 0x35 }, /* hrc */
167 { 0x70, 0x80, 0x90, 0xd0, 0xe0 }, /* hmd */
168 { 0x70, 0x80, 0x90, 0xd0, 0xe0 }, /* hds */
169 { 0x58, 0x68, 0x78, 0x88, 0x98, 0xa8, /* syl */
170 0xb8, 0xc8, 0xd8, 0xe8, 0xf8 },
171};
172
173/* temperature state registers */
174static const u8 FSCHMD_REG_TEMP_STATE[7][11] = {
175 { 0x71, 0x81, 0x91 }, /* pos */
176 { 0x71, 0x81, 0x91 }, /* her */
177 { 0x71, 0xd1, 0x81, 0x91 }, /* scy */
178 { 0x71, 0x81, 0x91 }, /* hrc */
179 { 0x71, 0x81, 0x91, 0xd1, 0xe1 }, /* hmd */
180 { 0x71, 0x81, 0x91, 0xd1, 0xe1 }, /* hds */
181 { 0x59, 0x69, 0x79, 0x89, 0x99, 0xa9, /* syl */
182 0xb9, 0xc9, 0xd9, 0xe9, 0xf9 },
183};
184
185/* temperature high limit registers, FSC does not document these. Proven to be
186 there with field testing on the fscher and fschrc, already supported / used
187 in the fscscy 2.4 driver. FSC has confirmed that the fschmd has registers
188 at these addresses, but doesn't want to confirm they are the same as with
189 the fscher?? */
190static const u8 FSCHMD_REG_TEMP_LIMIT[7][11] = {
191 { 0, 0, 0 }, /* pos */
192 { 0x76, 0x86, 0x96 }, /* her */
193 { 0x76, 0xd6, 0x86, 0x96 }, /* scy */
194 { 0x76, 0x86, 0x96 }, /* hrc */
195 { 0x76, 0x86, 0x96, 0xd6, 0xe6 }, /* hmd */
196 { 0x76, 0x86, 0x96, 0xd6, 0xe6 }, /* hds */
197 { 0x5a, 0x6a, 0x7a, 0x8a, 0x9a, 0xaa, /* syl */
198 0xba, 0xca, 0xda, 0xea, 0xfa },
199};
200
201/* These were found through experimenting with an fscher, currently they are
202 not used, but we keep them around for future reference.
203 On the fscsyl AUTOP1 lives at 0x#c (so 0x5c for fan1, 0x6c for fan2, etc),
204 AUTOP2 lives at 0x#e, and 0x#1 is a bitmask defining which temps influence
205 the fan speed.
206static const u8 FSCHER_REG_TEMP_AUTOP1[] = { 0x73, 0x83, 0x93 };
207static const u8 FSCHER_REG_TEMP_AUTOP2[] = { 0x75, 0x85, 0x95 }; */
208
209static const int FSCHMD_NO_TEMP_SENSORS[7] = { 3, 3, 4, 3, 5, 5, 11 };
210
211/* temp status register bitmasks */
212#define FSCHMD_TEMP_WORKING 0x01
213#define FSCHMD_TEMP_ALERT 0x02
214#define FSCHMD_TEMP_DISABLED 0x80
215/* there only really is an alarm if the sensor is working and alert == 1 */
216#define FSCHMD_TEMP_ALARM_MASK \
217 (FSCHMD_TEMP_WORKING | FSCHMD_TEMP_ALERT)
218
219/*
220 * Functions declarations
221 */
222
223static int fschmd_probe(struct i2c_client *client,
224 const struct i2c_device_id *id);
225static int fschmd_detect(struct i2c_client *client,
226 struct i2c_board_info *info);
227static int fschmd_remove(struct i2c_client *client);
228static struct fschmd_data *fschmd_update_device(struct device *dev);
229
230/*
231 * Driver data (common to all clients)
232 */
233
234static const struct i2c_device_id fschmd_id[] = {
235 { "fscpos", fscpos },
236 { "fscher", fscher },
237 { "fscscy", fscscy },
238 { "fschrc", fschrc },
239 { "fschmd", fschmd },
240 { "fschds", fschds },
241 { "fscsyl", fscsyl },
242 { }
243};
244MODULE_DEVICE_TABLE(i2c, fschmd_id);
245
246static struct i2c_driver fschmd_driver = {
247 .class = I2C_CLASS_HWMON,
248 .driver = {
249 .name = "fschmd",
250 },
251 .probe = fschmd_probe,
252 .remove = fschmd_remove,
253 .id_table = fschmd_id,
254 .detect = fschmd_detect,
255 .address_list = normal_i2c,
256};
257
258/*
259 * Client data (each client gets its own)
260 */
261
262struct fschmd_data {
263 struct i2c_client *client;
264 struct device *hwmon_dev;
265 struct mutex update_lock;
266 struct mutex watchdog_lock;
267 struct list_head list; /* member of the watchdog_data_list */
268 struct kref kref;
269 struct miscdevice watchdog_miscdev;
270 enum chips kind;
271 unsigned long watchdog_is_open;
272 char watchdog_expect_close;
273 char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
274 char valid; /* zero until following fields are valid */
275 unsigned long last_updated; /* in jiffies */
276
277 /* register values */
278 u8 revision; /* chip revision */
279 u8 global_control; /* global control register */
280 u8 watchdog_control; /* watchdog control register */
281 u8 watchdog_state; /* watchdog status register */
282 u8 watchdog_preset; /* watchdog counter preset on trigger val */
283 u8 volt[6]; /* voltage */
284 u8 temp_act[11]; /* temperature */
285 u8 temp_status[11]; /* status of sensor */
286 u8 temp_max[11]; /* high temp limit, notice: undocumented! */
287 u8 fan_act[7]; /* fans revolutions per second */
288 u8 fan_status[7]; /* fan status */
289 u8 fan_min[7]; /* fan min value for rps */
290 u8 fan_ripple[7]; /* divider for rps */
291};
292
293/* Global variables to hold information read from special DMI tables, which are
294 available on FSC machines with an fscher or later chip. There is no need to
295 protect these with a lock as they are only modified from our attach function
296 which always gets called with the i2c-core lock held and never accessed
297 before the attach function is done with them. */
298static int dmi_mult[6] = { 490, 200, 100, 100, 200, 100 };
299static int dmi_offset[6] = { 0, 0, 0, 0, 0, 0 };
300static int dmi_vref = -1;
301
302/* Somewhat ugly :( global data pointer list with all fschmd devices, so that
303 we can find our device data as when using misc_register there is no other
304 method to get to ones device data from the open fop. */
305static LIST_HEAD(watchdog_data_list);
306/* Note this lock not only protect list access, but also data.kref access */
307static DEFINE_MUTEX(watchdog_data_mutex);
308
309/* Release our data struct when we're detached from the i2c client *and* all
310 references to our watchdog device are released */
311static void fschmd_release_resources(struct kref *ref)
312{
313 struct fschmd_data *data = container_of(ref, struct fschmd_data, kref);
314 kfree(data);
315}
316
317/*
318 * Sysfs attr show / store functions
319 */
320
321static ssize_t show_in_value(struct device *dev,
322 struct device_attribute *devattr, char *buf)
323{
324 const int max_reading[3] = { 14200, 6600, 3300 };
325 int index = to_sensor_dev_attr(devattr)->index;
326 struct fschmd_data *data = fschmd_update_device(dev);
327
328 if (data->kind == fscher || data->kind >= fschrc)
329 return sprintf(buf, "%d\n", (data->volt[index] * dmi_vref *
330 dmi_mult[index]) / 255 + dmi_offset[index]);
331 else
332 return sprintf(buf, "%d\n", (data->volt[index] *
333 max_reading[index] + 128) / 255);
334}
335
336
337#define TEMP_FROM_REG(val) (((val) - 128) * 1000)
338
339static ssize_t show_temp_value(struct device *dev,
340 struct device_attribute *devattr, char *buf)
341{
342 int index = to_sensor_dev_attr(devattr)->index;
343 struct fschmd_data *data = fschmd_update_device(dev);
344
345 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_act[index]));
346}
347
348static ssize_t show_temp_max(struct device *dev,
349 struct device_attribute *devattr, char *buf)
350{
351 int index = to_sensor_dev_attr(devattr)->index;
352 struct fschmd_data *data = fschmd_update_device(dev);
353
354 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[index]));
355}
356
357static ssize_t store_temp_max(struct device *dev, struct device_attribute
358 *devattr, const char *buf, size_t count)
359{
360 int index = to_sensor_dev_attr(devattr)->index;
361 struct fschmd_data *data = dev_get_drvdata(dev);
362 long v = simple_strtol(buf, NULL, 10) / 1000;
363
364 v = SENSORS_LIMIT(v, -128, 127) + 128;
365
366 mutex_lock(&data->update_lock);
367 i2c_smbus_write_byte_data(to_i2c_client(dev),
368 FSCHMD_REG_TEMP_LIMIT[data->kind][index], v);
369 data->temp_max[index] = v;
370 mutex_unlock(&data->update_lock);
371
372 return count;
373}
374
375static ssize_t show_temp_fault(struct device *dev,
376 struct device_attribute *devattr, char *buf)
377{
378 int index = to_sensor_dev_attr(devattr)->index;
379 struct fschmd_data *data = fschmd_update_device(dev);
380
381 /* bit 0 set means sensor working ok, so no fault! */
382 if (data->temp_status[index] & FSCHMD_TEMP_WORKING)
383 return sprintf(buf, "0\n");
384 else
385 return sprintf(buf, "1\n");
386}
387
388static ssize_t show_temp_alarm(struct device *dev,
389 struct device_attribute *devattr, char *buf)
390{
391 int index = to_sensor_dev_attr(devattr)->index;
392 struct fschmd_data *data = fschmd_update_device(dev);
393
394 if ((data->temp_status[index] & FSCHMD_TEMP_ALARM_MASK) ==
395 FSCHMD_TEMP_ALARM_MASK)
396 return sprintf(buf, "1\n");
397 else
398 return sprintf(buf, "0\n");
399}
400
401
402#define RPM_FROM_REG(val) ((val) * 60)
403
404static ssize_t show_fan_value(struct device *dev,
405 struct device_attribute *devattr, char *buf)
406{
407 int index = to_sensor_dev_attr(devattr)->index;
408 struct fschmd_data *data = fschmd_update_device(dev);
409
410 return sprintf(buf, "%u\n", RPM_FROM_REG(data->fan_act[index]));
411}
412
413static ssize_t show_fan_div(struct device *dev,
414 struct device_attribute *devattr, char *buf)
415{
416 int index = to_sensor_dev_attr(devattr)->index;
417 struct fschmd_data *data = fschmd_update_device(dev);
418
419 /* bits 2..7 reserved => mask with 3 */
420 return sprintf(buf, "%d\n", 1 << (data->fan_ripple[index] & 3));
421}
422
423static ssize_t store_fan_div(struct device *dev, struct device_attribute
424 *devattr, const char *buf, size_t count)
425{
426 u8 reg;
427 int index = to_sensor_dev_attr(devattr)->index;
428 struct fschmd_data *data = dev_get_drvdata(dev);
429 /* supported values: 2, 4, 8 */
430 unsigned long v = simple_strtoul(buf, NULL, 10);
431
432 switch (v) {
433 case 2: v = 1; break;
434 case 4: v = 2; break;
435 case 8: v = 3; break;
436 default:
437 dev_err(dev, "fan_div value %lu not supported. "
438 "Choose one of 2, 4 or 8!\n", v);
439 return -EINVAL;
440 }
441
442 mutex_lock(&data->update_lock);
443
444 reg = i2c_smbus_read_byte_data(to_i2c_client(dev),
445 FSCHMD_REG_FAN_RIPPLE[data->kind][index]);
446
447 /* bits 2..7 reserved => mask with 0x03 */
448 reg &= ~0x03;
449 reg |= v;
450
451 i2c_smbus_write_byte_data(to_i2c_client(dev),
452 FSCHMD_REG_FAN_RIPPLE[data->kind][index], reg);
453
454 data->fan_ripple[index] = reg;
455
456 mutex_unlock(&data->update_lock);
457
458 return count;
459}
460
461static ssize_t show_fan_alarm(struct device *dev,
462 struct device_attribute *devattr, char *buf)
463{
464 int index = to_sensor_dev_attr(devattr)->index;
465 struct fschmd_data *data = fschmd_update_device(dev);
466
467 if (data->fan_status[index] & FSCHMD_FAN_ALARM)
468 return sprintf(buf, "1\n");
469 else
470 return sprintf(buf, "0\n");
471}
472
473static ssize_t show_fan_fault(struct device *dev,
474 struct device_attribute *devattr, char *buf)
475{
476 int index = to_sensor_dev_attr(devattr)->index;
477 struct fschmd_data *data = fschmd_update_device(dev);
478
479 if (data->fan_status[index] & FSCHMD_FAN_NOT_PRESENT)
480 return sprintf(buf, "1\n");
481 else
482 return sprintf(buf, "0\n");
483}
484
485
486static ssize_t show_pwm_auto_point1_pwm(struct device *dev,
487 struct device_attribute *devattr, char *buf)
488{
489 int index = to_sensor_dev_attr(devattr)->index;
490 struct fschmd_data *data = fschmd_update_device(dev);
491 int val = data->fan_min[index];
492
493 /* 0 = allow turning off (except on the syl), 1-255 = 50-100% */
494 if (val || data->kind == fscsyl)
495 val = val / 2 + 128;
496
497 return sprintf(buf, "%d\n", val);
498}
499
500static ssize_t store_pwm_auto_point1_pwm(struct device *dev,
501 struct device_attribute *devattr, const char *buf, size_t count)
502{
503 int index = to_sensor_dev_attr(devattr)->index;
504 struct fschmd_data *data = dev_get_drvdata(dev);
505 unsigned long v = simple_strtoul(buf, NULL, 10);
506
507 /* reg: 0 = allow turning off (except on the syl), 1-255 = 50-100% */
508 if (v || data->kind == fscsyl) {
509 v = SENSORS_LIMIT(v, 128, 255);
510 v = (v - 128) * 2 + 1;
511 }
512
513 mutex_lock(&data->update_lock);
514
515 i2c_smbus_write_byte_data(to_i2c_client(dev),
516 FSCHMD_REG_FAN_MIN[data->kind][index], v);
517 data->fan_min[index] = v;
518
519 mutex_unlock(&data->update_lock);
520
521 return count;
522}
523
524
525/* The FSC hwmon family has the ability to force an attached alert led to flash
526 from software, we export this as an alert_led sysfs attr */
527static ssize_t show_alert_led(struct device *dev,
528 struct device_attribute *devattr, char *buf)
529{
530 struct fschmd_data *data = fschmd_update_device(dev);
531
532 if (data->global_control & FSCHMD_CONTROL_ALERT_LED)
533 return sprintf(buf, "1\n");
534 else
535 return sprintf(buf, "0\n");
536}
537
538static ssize_t store_alert_led(struct device *dev,
539 struct device_attribute *devattr, const char *buf, size_t count)
540{
541 u8 reg;
542 struct fschmd_data *data = dev_get_drvdata(dev);
543 unsigned long v = simple_strtoul(buf, NULL, 10);
544
545 mutex_lock(&data->update_lock);
546
547 reg = i2c_smbus_read_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL);
548
549 if (v)
550 reg |= FSCHMD_CONTROL_ALERT_LED;
551 else
552 reg &= ~FSCHMD_CONTROL_ALERT_LED;
553
554 i2c_smbus_write_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL, reg);
555
556 data->global_control = reg;
557
558 mutex_unlock(&data->update_lock);
559
560 return count;
561}
562
563static DEVICE_ATTR(alert_led, 0644, show_alert_led, store_alert_led);
564
565static struct sensor_device_attribute fschmd_attr[] = {
566 SENSOR_ATTR(in0_input, 0444, show_in_value, NULL, 0),
567 SENSOR_ATTR(in1_input, 0444, show_in_value, NULL, 1),
568 SENSOR_ATTR(in2_input, 0444, show_in_value, NULL, 2),
569 SENSOR_ATTR(in3_input, 0444, show_in_value, NULL, 3),
570 SENSOR_ATTR(in4_input, 0444, show_in_value, NULL, 4),
571 SENSOR_ATTR(in5_input, 0444, show_in_value, NULL, 5),
572};
573
574static struct sensor_device_attribute fschmd_temp_attr[] = {
575 SENSOR_ATTR(temp1_input, 0444, show_temp_value, NULL, 0),
576 SENSOR_ATTR(temp1_max, 0644, show_temp_max, store_temp_max, 0),
577 SENSOR_ATTR(temp1_fault, 0444, show_temp_fault, NULL, 0),
578 SENSOR_ATTR(temp1_alarm, 0444, show_temp_alarm, NULL, 0),
579 SENSOR_ATTR(temp2_input, 0444, show_temp_value, NULL, 1),
580 SENSOR_ATTR(temp2_max, 0644, show_temp_max, store_temp_max, 1),
581 SENSOR_ATTR(temp2_fault, 0444, show_temp_fault, NULL, 1),
582 SENSOR_ATTR(temp2_alarm, 0444, show_temp_alarm, NULL, 1),
583 SENSOR_ATTR(temp3_input, 0444, show_temp_value, NULL, 2),
584 SENSOR_ATTR(temp3_max, 0644, show_temp_max, store_temp_max, 2),
585 SENSOR_ATTR(temp3_fault, 0444, show_temp_fault, NULL, 2),
586 SENSOR_ATTR(temp3_alarm, 0444, show_temp_alarm, NULL, 2),
587 SENSOR_ATTR(temp4_input, 0444, show_temp_value, NULL, 3),
588 SENSOR_ATTR(temp4_max, 0644, show_temp_max, store_temp_max, 3),
589 SENSOR_ATTR(temp4_fault, 0444, show_temp_fault, NULL, 3),
590 SENSOR_ATTR(temp4_alarm, 0444, show_temp_alarm, NULL, 3),
591 SENSOR_ATTR(temp5_input, 0444, show_temp_value, NULL, 4),
592 SENSOR_ATTR(temp5_max, 0644, show_temp_max, store_temp_max, 4),
593 SENSOR_ATTR(temp5_fault, 0444, show_temp_fault, NULL, 4),
594 SENSOR_ATTR(temp5_alarm, 0444, show_temp_alarm, NULL, 4),
595 SENSOR_ATTR(temp6_input, 0444, show_temp_value, NULL, 5),
596 SENSOR_ATTR(temp6_max, 0644, show_temp_max, store_temp_max, 5),
597 SENSOR_ATTR(temp6_fault, 0444, show_temp_fault, NULL, 5),
598 SENSOR_ATTR(temp6_alarm, 0444, show_temp_alarm, NULL, 5),
599 SENSOR_ATTR(temp7_input, 0444, show_temp_value, NULL, 6),
600 SENSOR_ATTR(temp7_max, 0644, show_temp_max, store_temp_max, 6),
601 SENSOR_ATTR(temp7_fault, 0444, show_temp_fault, NULL, 6),
602 SENSOR_ATTR(temp7_alarm, 0444, show_temp_alarm, NULL, 6),
603 SENSOR_ATTR(temp8_input, 0444, show_temp_value, NULL, 7),
604 SENSOR_ATTR(temp8_max, 0644, show_temp_max, store_temp_max, 7),
605 SENSOR_ATTR(temp8_fault, 0444, show_temp_fault, NULL, 7),
606 SENSOR_ATTR(temp8_alarm, 0444, show_temp_alarm, NULL, 7),
607 SENSOR_ATTR(temp9_input, 0444, show_temp_value, NULL, 8),
608 SENSOR_ATTR(temp9_max, 0644, show_temp_max, store_temp_max, 8),
609 SENSOR_ATTR(temp9_fault, 0444, show_temp_fault, NULL, 8),
610 SENSOR_ATTR(temp9_alarm, 0444, show_temp_alarm, NULL, 8),
611 SENSOR_ATTR(temp10_input, 0444, show_temp_value, NULL, 9),
612 SENSOR_ATTR(temp10_max, 0644, show_temp_max, store_temp_max, 9),
613 SENSOR_ATTR(temp10_fault, 0444, show_temp_fault, NULL, 9),
614 SENSOR_ATTR(temp10_alarm, 0444, show_temp_alarm, NULL, 9),
615 SENSOR_ATTR(temp11_input, 0444, show_temp_value, NULL, 10),
616 SENSOR_ATTR(temp11_max, 0644, show_temp_max, store_temp_max, 10),
617 SENSOR_ATTR(temp11_fault, 0444, show_temp_fault, NULL, 10),
618 SENSOR_ATTR(temp11_alarm, 0444, show_temp_alarm, NULL, 10),
619};
620
621static struct sensor_device_attribute fschmd_fan_attr[] = {
622 SENSOR_ATTR(fan1_input, 0444, show_fan_value, NULL, 0),
623 SENSOR_ATTR(fan1_div, 0644, show_fan_div, store_fan_div, 0),
624 SENSOR_ATTR(fan1_alarm, 0444, show_fan_alarm, NULL, 0),
625 SENSOR_ATTR(fan1_fault, 0444, show_fan_fault, NULL, 0),
626 SENSOR_ATTR(pwm1_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
627 store_pwm_auto_point1_pwm, 0),
628 SENSOR_ATTR(fan2_input, 0444, show_fan_value, NULL, 1),
629 SENSOR_ATTR(fan2_div, 0644, show_fan_div, store_fan_div, 1),
630 SENSOR_ATTR(fan2_alarm, 0444, show_fan_alarm, NULL, 1),
631 SENSOR_ATTR(fan2_fault, 0444, show_fan_fault, NULL, 1),
632 SENSOR_ATTR(pwm2_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
633 store_pwm_auto_point1_pwm, 1),
634 SENSOR_ATTR(fan3_input, 0444, show_fan_value, NULL, 2),
635 SENSOR_ATTR(fan3_div, 0644, show_fan_div, store_fan_div, 2),
636 SENSOR_ATTR(fan3_alarm, 0444, show_fan_alarm, NULL, 2),
637 SENSOR_ATTR(fan3_fault, 0444, show_fan_fault, NULL, 2),
638 SENSOR_ATTR(pwm3_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
639 store_pwm_auto_point1_pwm, 2),
640 SENSOR_ATTR(fan4_input, 0444, show_fan_value, NULL, 3),
641 SENSOR_ATTR(fan4_div, 0644, show_fan_div, store_fan_div, 3),
642 SENSOR_ATTR(fan4_alarm, 0444, show_fan_alarm, NULL, 3),
643 SENSOR_ATTR(fan4_fault, 0444, show_fan_fault, NULL, 3),
644 SENSOR_ATTR(pwm4_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
645 store_pwm_auto_point1_pwm, 3),
646 SENSOR_ATTR(fan5_input, 0444, show_fan_value, NULL, 4),
647 SENSOR_ATTR(fan5_div, 0644, show_fan_div, store_fan_div, 4),
648 SENSOR_ATTR(fan5_alarm, 0444, show_fan_alarm, NULL, 4),
649 SENSOR_ATTR(fan5_fault, 0444, show_fan_fault, NULL, 4),
650 SENSOR_ATTR(pwm5_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
651 store_pwm_auto_point1_pwm, 4),
652 SENSOR_ATTR(fan6_input, 0444, show_fan_value, NULL, 5),
653 SENSOR_ATTR(fan6_div, 0644, show_fan_div, store_fan_div, 5),
654 SENSOR_ATTR(fan6_alarm, 0444, show_fan_alarm, NULL, 5),
655 SENSOR_ATTR(fan6_fault, 0444, show_fan_fault, NULL, 5),
656 SENSOR_ATTR(pwm6_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
657 store_pwm_auto_point1_pwm, 5),
658 SENSOR_ATTR(fan7_input, 0444, show_fan_value, NULL, 6),
659 SENSOR_ATTR(fan7_div, 0644, show_fan_div, store_fan_div, 6),
660 SENSOR_ATTR(fan7_alarm, 0444, show_fan_alarm, NULL, 6),
661 SENSOR_ATTR(fan7_fault, 0444, show_fan_fault, NULL, 6),
662 SENSOR_ATTR(pwm7_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
663 store_pwm_auto_point1_pwm, 6),
664};
665
666
667/*
668 * Watchdog routines
669 */
670
671static int watchdog_set_timeout(struct fschmd_data *data, int timeout)
672{
673 int ret, resolution;
674 int kind = data->kind + 1; /* 0-x array index -> 1-x module param */
675
676 /* 2 second or 60 second resolution? */
677 if (timeout <= 510 || kind == fscpos || kind == fscscy)
678 resolution = 2;
679 else
680 resolution = 60;
681
682 if (timeout < resolution || timeout > (resolution * 255))
683 return -EINVAL;
684
685 mutex_lock(&data->watchdog_lock);
686 if (!data->client) {
687 ret = -ENODEV;
688 goto leave;
689 }
690
691 if (resolution == 2)
692 data->watchdog_control &= ~FSCHMD_WDOG_CONTROL_RESOLUTION;
693 else
694 data->watchdog_control |= FSCHMD_WDOG_CONTROL_RESOLUTION;
695
696 data->watchdog_preset = DIV_ROUND_UP(timeout, resolution);
697
698 /* Write new timeout value */
699 i2c_smbus_write_byte_data(data->client,
700 FSCHMD_REG_WDOG_PRESET[data->kind], data->watchdog_preset);
701 /* Write new control register, do not trigger! */
702 i2c_smbus_write_byte_data(data->client,
703 FSCHMD_REG_WDOG_CONTROL[data->kind],
704 data->watchdog_control & ~FSCHMD_WDOG_CONTROL_TRIGGER);
705
706 ret = data->watchdog_preset * resolution;
707
708leave:
709 mutex_unlock(&data->watchdog_lock);
710 return ret;
711}
712
713static int watchdog_get_timeout(struct fschmd_data *data)
714{
715 int timeout;
716
717 mutex_lock(&data->watchdog_lock);
718 if (data->watchdog_control & FSCHMD_WDOG_CONTROL_RESOLUTION)
719 timeout = data->watchdog_preset * 60;
720 else
721 timeout = data->watchdog_preset * 2;
722 mutex_unlock(&data->watchdog_lock);
723
724 return timeout;
725}
726
727static int watchdog_trigger(struct fschmd_data *data)
728{
729 int ret = 0;
730
731 mutex_lock(&data->watchdog_lock);
732 if (!data->client) {
733 ret = -ENODEV;
734 goto leave;
735 }
736
737 data->watchdog_control |= FSCHMD_WDOG_CONTROL_TRIGGER;
738 i2c_smbus_write_byte_data(data->client,
739 FSCHMD_REG_WDOG_CONTROL[data->kind],
740 data->watchdog_control);
741leave:
742 mutex_unlock(&data->watchdog_lock);
743 return ret;
744}
745
746static int watchdog_stop(struct fschmd_data *data)
747{
748 int ret = 0;
749
750 mutex_lock(&data->watchdog_lock);
751 if (!data->client) {
752 ret = -ENODEV;
753 goto leave;
754 }
755
756 data->watchdog_control &= ~FSCHMD_WDOG_CONTROL_STARTED;
757 /* Don't store the stop flag in our watchdog control register copy, as
758 its a write only bit (read always returns 0) */
759 i2c_smbus_write_byte_data(data->client,
760 FSCHMD_REG_WDOG_CONTROL[data->kind],
761 data->watchdog_control | FSCHMD_WDOG_CONTROL_STOP);
762leave:
763 mutex_unlock(&data->watchdog_lock);
764 return ret;
765}
766
767static int watchdog_open(struct inode *inode, struct file *filp)
768{
769 struct fschmd_data *pos, *data = NULL;
770 int watchdog_is_open;
771
772 /* We get called from drivers/char/misc.c with misc_mtx hold, and we
773 call misc_register() from fschmd_probe() with watchdog_data_mutex
774 hold, as misc_register() takes the misc_mtx lock, this is a possible
775 deadlock, so we use mutex_trylock here. */
776 if (!mutex_trylock(&watchdog_data_mutex))
777 return -ERESTARTSYS;
778 list_for_each_entry(pos, &watchdog_data_list, list) {
779 if (pos->watchdog_miscdev.minor == iminor(inode)) {
780 data = pos;
781 break;
782 }
783 }
784 /* Note we can never not have found data, so we don't check for this */
785 watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
786 if (!watchdog_is_open)
787 kref_get(&data->kref);
788 mutex_unlock(&watchdog_data_mutex);
789
790 if (watchdog_is_open)
791 return -EBUSY;
792
793 /* Start the watchdog */
794 watchdog_trigger(data);
795 filp->private_data = data;
796
797 return nonseekable_open(inode, filp);
798}
799
800static int watchdog_release(struct inode *inode, struct file *filp)
801{
802 struct fschmd_data *data = filp->private_data;
803
804 if (data->watchdog_expect_close) {
805 watchdog_stop(data);
806 data->watchdog_expect_close = 0;
807 } else {
808 watchdog_trigger(data);
809 dev_crit(&data->client->dev,
810 "unexpected close, not stopping watchdog!\n");
811 }
812
813 clear_bit(0, &data->watchdog_is_open);
814
815 mutex_lock(&watchdog_data_mutex);
816 kref_put(&data->kref, fschmd_release_resources);
817 mutex_unlock(&watchdog_data_mutex);
818
819 return 0;
820}
821
822static ssize_t watchdog_write(struct file *filp, const char __user *buf,
823 size_t count, loff_t *offset)
824{
825 int ret;
826 struct fschmd_data *data = filp->private_data;
827
828 if (count) {
829 if (!nowayout) {
830 size_t i;
831
832 /* Clear it in case it was set with a previous write */
833 data->watchdog_expect_close = 0;
834
835 for (i = 0; i != count; i++) {
836 char c;
837 if (get_user(c, buf + i))
838 return -EFAULT;
839 if (c == 'V')
840 data->watchdog_expect_close = 1;
841 }
842 }
843 ret = watchdog_trigger(data);
844 if (ret < 0)
845 return ret;
846 }
847 return count;
848}
849
850static long watchdog_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
851{
852 struct watchdog_info ident = {
853 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
854 WDIOF_CARDRESET,
855 .identity = "FSC watchdog"
856 };
857 int i, ret = 0;
858 struct fschmd_data *data = filp->private_data;
859
860 switch (cmd) {
861 case WDIOC_GETSUPPORT:
862 ident.firmware_version = data->revision;
863 if (!nowayout)
864 ident.options |= WDIOF_MAGICCLOSE;
865 if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
866 ret = -EFAULT;
867 break;
868
869 case WDIOC_GETSTATUS:
870 ret = put_user(0, (int __user *)arg);
871 break;
872
873 case WDIOC_GETBOOTSTATUS:
874 if (data->watchdog_state & FSCHMD_WDOG_STATE_CARDRESET)
875 ret = put_user(WDIOF_CARDRESET, (int __user *)arg);
876 else
877 ret = put_user(0, (int __user *)arg);
878 break;
879
880 case WDIOC_KEEPALIVE:
881 ret = watchdog_trigger(data);
882 break;
883
884 case WDIOC_GETTIMEOUT:
885 i = watchdog_get_timeout(data);
886 ret = put_user(i, (int __user *)arg);
887 break;
888
889 case WDIOC_SETTIMEOUT:
890 if (get_user(i, (int __user *)arg)) {
891 ret = -EFAULT;
892 break;
893 }
894 ret = watchdog_set_timeout(data, i);
895 if (ret > 0)
896 ret = put_user(ret, (int __user *)arg);
897 break;
898
899 case WDIOC_SETOPTIONS:
900 if (get_user(i, (int __user *)arg)) {
901 ret = -EFAULT;
902 break;
903 }
904
905 if (i & WDIOS_DISABLECARD)
906 ret = watchdog_stop(data);
907 else if (i & WDIOS_ENABLECARD)
908 ret = watchdog_trigger(data);
909 else
910 ret = -EINVAL;
911
912 break;
913 default:
914 ret = -ENOTTY;
915 }
916 return ret;
917}
918
919static const struct file_operations watchdog_fops = {
920 .owner = THIS_MODULE,
921 .llseek = no_llseek,
922 .open = watchdog_open,
923 .release = watchdog_release,
924 .write = watchdog_write,
925 .unlocked_ioctl = watchdog_ioctl,
926};
927
928
929/*
930 * Detect, register, unregister and update device functions
931 */
932
933/* DMI decode routine to read voltage scaling factors from special DMI tables,
934 which are available on FSC machines with an fscher or later chip. */
935static void fschmd_dmi_decode(const struct dmi_header *header, void *dummy)
936{
937 int i, mult[3] = { 0 }, offset[3] = { 0 }, vref = 0, found = 0;
938
939 /* dmi code ugliness, we get passed the address of the contents of
940 a complete DMI record, but in the form of a dmi_header pointer, in
941 reality this address holds header->length bytes of which the header
942 are the first 4 bytes */
943 u8 *dmi_data = (u8 *)header;
944
945 /* We are looking for OEM-specific type 185 */
946 if (header->type != 185)
947 return;
948
949 /* we are looking for what Siemens calls "subtype" 19, the subtype
950 is stored in byte 5 of the dmi block */
951 if (header->length < 5 || dmi_data[4] != 19)
952 return;
953
954 /* After the subtype comes 1 unknown byte and then blocks of 5 bytes,
955 consisting of what Siemens calls an "Entity" number, followed by
956 2 16-bit words in LSB first order */
957 for (i = 6; (i + 4) < header->length; i += 5) {
958 /* entity 1 - 3: voltage multiplier and offset */
959 if (dmi_data[i] >= 1 && dmi_data[i] <= 3) {
960 /* Our in sensors order and the DMI order differ */
961 const int shuffle[3] = { 1, 0, 2 };
962 int in = shuffle[dmi_data[i] - 1];
963
964 /* Check for twice the same entity */
965 if (found & (1 << in))
966 return;
967
968 mult[in] = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
969 offset[in] = dmi_data[i + 3] | (dmi_data[i + 4] << 8);
970
971 found |= 1 << in;
972 }
973
974 /* entity 7: reference voltage */
975 if (dmi_data[i] == 7) {
976 /* Check for twice the same entity */
977 if (found & 0x08)
978 return;
979
980 vref = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
981
982 found |= 0x08;
983 }
984 }
985
986 if (found == 0x0F) {
987 for (i = 0; i < 3; i++) {
988 dmi_mult[i] = mult[i] * 10;
989 dmi_offset[i] = offset[i] * 10;
990 }
991 /* According to the docs there should be separate dmi entries
992 for the mult's and offsets of in3-5 of the syl, but on
993 my test machine these are not present */
994 dmi_mult[3] = dmi_mult[2];
995 dmi_mult[4] = dmi_mult[1];
996 dmi_mult[5] = dmi_mult[2];
997 dmi_offset[3] = dmi_offset[2];
998 dmi_offset[4] = dmi_offset[1];
999 dmi_offset[5] = dmi_offset[2];
1000 dmi_vref = vref;
1001 }
1002}
1003
1004static int fschmd_detect(struct i2c_client *client,
1005 struct i2c_board_info *info)
1006{
1007 enum chips kind;
1008 struct i2c_adapter *adapter = client->adapter;
1009 char id[4];
1010
1011 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1012 return -ENODEV;
1013
1014 /* Detect & Identify the chip */
1015 id[0] = i2c_smbus_read_byte_data(client, FSCHMD_REG_IDENT_0);
1016 id[1] = i2c_smbus_read_byte_data(client, FSCHMD_REG_IDENT_1);
1017 id[2] = i2c_smbus_read_byte_data(client, FSCHMD_REG_IDENT_2);
1018 id[3] = '\0';
1019
1020 if (!strcmp(id, "PEG"))
1021 kind = fscpos;
1022 else if (!strcmp(id, "HER"))
1023 kind = fscher;
1024 else if (!strcmp(id, "SCY"))
1025 kind = fscscy;
1026 else if (!strcmp(id, "HRC"))
1027 kind = fschrc;
1028 else if (!strcmp(id, "HMD"))
1029 kind = fschmd;
1030 else if (!strcmp(id, "HDS"))
1031 kind = fschds;
1032 else if (!strcmp(id, "SYL"))
1033 kind = fscsyl;
1034 else
1035 return -ENODEV;
1036
1037 strlcpy(info->type, fschmd_id[kind].name, I2C_NAME_SIZE);
1038
1039 return 0;
1040}
1041
1042static int fschmd_probe(struct i2c_client *client,
1043 const struct i2c_device_id *id)
1044{
1045 struct fschmd_data *data;
1046 const char * const names[7] = { "Poseidon", "Hermes", "Scylla",
1047 "Heracles", "Heimdall", "Hades", "Syleus" };
1048 const int watchdog_minors[] = { WATCHDOG_MINOR, 212, 213, 214, 215 };
1049 int i, err;
1050 enum chips kind = id->driver_data;
1051
1052 data = kzalloc(sizeof(struct fschmd_data), GFP_KERNEL);
1053 if (!data)
1054 return -ENOMEM;
1055
1056 i2c_set_clientdata(client, data);
1057 mutex_init(&data->update_lock);
1058 mutex_init(&data->watchdog_lock);
1059 INIT_LIST_HEAD(&data->list);
1060 kref_init(&data->kref);
1061 /* Store client pointer in our data struct for watchdog usage
1062 (where the client is found through a data ptr instead of the
1063 otherway around) */
1064 data->client = client;
1065 data->kind = kind;
1066
1067 if (kind == fscpos) {
1068 /* The Poseidon has hardwired temp limits, fill these
1069 in for the alarm resetting code */
1070 data->temp_max[0] = 70 + 128;
1071 data->temp_max[1] = 50 + 128;
1072 data->temp_max[2] = 50 + 128;
1073 }
1074
1075 /* Read the special DMI table for fscher and newer chips */
1076 if ((kind == fscher || kind >= fschrc) && dmi_vref == -1) {
1077 dmi_walk(fschmd_dmi_decode, NULL);
1078 if (dmi_vref == -1) {
1079 dev_warn(&client->dev,
1080 "Couldn't get voltage scaling factors from "
1081 "BIOS DMI table, using builtin defaults\n");
1082 dmi_vref = 33;
1083 }
1084 }
1085
1086 /* Read in some never changing registers */
1087 data->revision = i2c_smbus_read_byte_data(client, FSCHMD_REG_REVISION);
1088 data->global_control = i2c_smbus_read_byte_data(client,
1089 FSCHMD_REG_CONTROL);
1090 data->watchdog_control = i2c_smbus_read_byte_data(client,
1091 FSCHMD_REG_WDOG_CONTROL[data->kind]);
1092 data->watchdog_state = i2c_smbus_read_byte_data(client,
1093 FSCHMD_REG_WDOG_STATE[data->kind]);
1094 data->watchdog_preset = i2c_smbus_read_byte_data(client,
1095 FSCHMD_REG_WDOG_PRESET[data->kind]);
1096
1097 err = device_create_file(&client->dev, &dev_attr_alert_led);
1098 if (err)
1099 goto exit_detach;
1100
1101 for (i = 0; i < FSCHMD_NO_VOLT_SENSORS[data->kind]; i++) {
1102 err = device_create_file(&client->dev,
1103 &fschmd_attr[i].dev_attr);
1104 if (err)
1105 goto exit_detach;
1106 }
1107
1108 for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++) {
1109 /* Poseidon doesn't have TEMP_LIMIT registers */
1110 if (kind == fscpos && fschmd_temp_attr[i].dev_attr.show ==
1111 show_temp_max)
1112 continue;
1113
1114 if (kind == fscsyl) {
1115 if (i % 4 == 0)
1116 data->temp_status[i / 4] =
1117 i2c_smbus_read_byte_data(client,
1118 FSCHMD_REG_TEMP_STATE
1119 [data->kind][i / 4]);
1120 if (data->temp_status[i / 4] & FSCHMD_TEMP_DISABLED)
1121 continue;
1122 }
1123
1124 err = device_create_file(&client->dev,
1125 &fschmd_temp_attr[i].dev_attr);
1126 if (err)
1127 goto exit_detach;
1128 }
1129
1130 for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++) {
1131 /* Poseidon doesn't have a FAN_MIN register for its 3rd fan */
1132 if (kind == fscpos &&
1133 !strcmp(fschmd_fan_attr[i].dev_attr.attr.name,
1134 "pwm3_auto_point1_pwm"))
1135 continue;
1136
1137 if (kind == fscsyl) {
1138 if (i % 5 == 0)
1139 data->fan_status[i / 5] =
1140 i2c_smbus_read_byte_data(client,
1141 FSCHMD_REG_FAN_STATE
1142 [data->kind][i / 5]);
1143 if (data->fan_status[i / 5] & FSCHMD_FAN_DISABLED)
1144 continue;
1145 }
1146
1147 err = device_create_file(&client->dev,
1148 &fschmd_fan_attr[i].dev_attr);
1149 if (err)
1150 goto exit_detach;
1151 }
1152
1153 data->hwmon_dev = hwmon_device_register(&client->dev);
1154 if (IS_ERR(data->hwmon_dev)) {
1155 err = PTR_ERR(data->hwmon_dev);
1156 data->hwmon_dev = NULL;
1157 goto exit_detach;
1158 }
1159
1160 /* We take the data_mutex lock early so that watchdog_open() cannot
1161 run when misc_register() has completed, but we've not yet added
1162 our data to the watchdog_data_list (and set the default timeout) */
1163 mutex_lock(&watchdog_data_mutex);
1164 for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
1165 /* Register our watchdog part */
1166 snprintf(data->watchdog_name, sizeof(data->watchdog_name),
1167 "watchdog%c", (i == 0) ? '\0' : ('0' + i));
1168 data->watchdog_miscdev.name = data->watchdog_name;
1169 data->watchdog_miscdev.fops = &watchdog_fops;
1170 data->watchdog_miscdev.minor = watchdog_minors[i];
1171 err = misc_register(&data->watchdog_miscdev);
1172 if (err == -EBUSY)
1173 continue;
1174 if (err) {
1175 data->watchdog_miscdev.minor = 0;
1176 dev_err(&client->dev,
1177 "Registering watchdog chardev: %d\n", err);
1178 break;
1179 }
1180
1181 list_add(&data->list, &watchdog_data_list);
1182 watchdog_set_timeout(data, 60);
1183 dev_info(&client->dev,
1184 "Registered watchdog chardev major 10, minor: %d\n",
1185 watchdog_minors[i]);
1186 break;
1187 }
1188 if (i == ARRAY_SIZE(watchdog_minors)) {
1189 data->watchdog_miscdev.minor = 0;
1190 dev_warn(&client->dev, "Couldn't register watchdog chardev "
1191 "(due to no free minor)\n");
1192 }
1193 mutex_unlock(&watchdog_data_mutex);
1194
1195 dev_info(&client->dev, "Detected FSC %s chip, revision: %d\n",
1196 names[data->kind], (int) data->revision);
1197
1198 return 0;
1199
1200exit_detach:
1201 fschmd_remove(client); /* will also free data for us */
1202 return err;
1203}
1204
1205static int fschmd_remove(struct i2c_client *client)
1206{
1207 struct fschmd_data *data = i2c_get_clientdata(client);
1208 int i;
1209
1210 /* Unregister the watchdog (if registered) */
1211 if (data->watchdog_miscdev.minor) {
1212 misc_deregister(&data->watchdog_miscdev);
1213 if (data->watchdog_is_open) {
1214 dev_warn(&client->dev,
1215 "i2c client detached with watchdog open! "
1216 "Stopping watchdog.\n");
1217 watchdog_stop(data);
1218 }
1219 mutex_lock(&watchdog_data_mutex);
1220 list_del(&data->list);
1221 mutex_unlock(&watchdog_data_mutex);
1222 /* Tell the watchdog code the client is gone */
1223 mutex_lock(&data->watchdog_lock);
1224 data->client = NULL;
1225 mutex_unlock(&data->watchdog_lock);
1226 }
1227
1228 /* Check if registered in case we're called from fschmd_detect
1229 to cleanup after an error */
1230 if (data->hwmon_dev)
1231 hwmon_device_unregister(data->hwmon_dev);
1232
1233 device_remove_file(&client->dev, &dev_attr_alert_led);
1234 for (i = 0; i < (FSCHMD_NO_VOLT_SENSORS[data->kind]); i++)
1235 device_remove_file(&client->dev, &fschmd_attr[i].dev_attr);
1236 for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++)
1237 device_remove_file(&client->dev,
1238 &fschmd_temp_attr[i].dev_attr);
1239 for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++)
1240 device_remove_file(&client->dev,
1241 &fschmd_fan_attr[i].dev_attr);
1242
1243 mutex_lock(&watchdog_data_mutex);
1244 kref_put(&data->kref, fschmd_release_resources);
1245 mutex_unlock(&watchdog_data_mutex);
1246
1247 return 0;
1248}
1249
1250static struct fschmd_data *fschmd_update_device(struct device *dev)
1251{
1252 struct i2c_client *client = to_i2c_client(dev);
1253 struct fschmd_data *data = i2c_get_clientdata(client);
1254 int i;
1255
1256 mutex_lock(&data->update_lock);
1257
1258 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
1259
1260 for (i = 0; i < FSCHMD_NO_TEMP_SENSORS[data->kind]; i++) {
1261 data->temp_act[i] = i2c_smbus_read_byte_data(client,
1262 FSCHMD_REG_TEMP_ACT[data->kind][i]);
1263 data->temp_status[i] = i2c_smbus_read_byte_data(client,
1264 FSCHMD_REG_TEMP_STATE[data->kind][i]);
1265
1266 /* The fscpos doesn't have TEMP_LIMIT registers */
1267 if (FSCHMD_REG_TEMP_LIMIT[data->kind][i])
1268 data->temp_max[i] = i2c_smbus_read_byte_data(
1269 client,
1270 FSCHMD_REG_TEMP_LIMIT[data->kind][i]);
1271
1272 /* reset alarm if the alarm condition is gone,
1273 the chip doesn't do this itself */
1274 if ((data->temp_status[i] & FSCHMD_TEMP_ALARM_MASK) ==
1275 FSCHMD_TEMP_ALARM_MASK &&
1276 data->temp_act[i] < data->temp_max[i])
1277 i2c_smbus_write_byte_data(client,
1278 FSCHMD_REG_TEMP_STATE[data->kind][i],
1279 data->temp_status[i]);
1280 }
1281
1282 for (i = 0; i < FSCHMD_NO_FAN_SENSORS[data->kind]; i++) {
1283 data->fan_act[i] = i2c_smbus_read_byte_data(client,
1284 FSCHMD_REG_FAN_ACT[data->kind][i]);
1285 data->fan_status[i] = i2c_smbus_read_byte_data(client,
1286 FSCHMD_REG_FAN_STATE[data->kind][i]);
1287 data->fan_ripple[i] = i2c_smbus_read_byte_data(client,
1288 FSCHMD_REG_FAN_RIPPLE[data->kind][i]);
1289
1290 /* The fscpos third fan doesn't have a fan_min */
1291 if (FSCHMD_REG_FAN_MIN[data->kind][i])
1292 data->fan_min[i] = i2c_smbus_read_byte_data(
1293 client,
1294 FSCHMD_REG_FAN_MIN[data->kind][i]);
1295
1296 /* reset fan status if speed is back to > 0 */
1297 if ((data->fan_status[i] & FSCHMD_FAN_ALARM) &&
1298 data->fan_act[i])
1299 i2c_smbus_write_byte_data(client,
1300 FSCHMD_REG_FAN_STATE[data->kind][i],
1301 data->fan_status[i]);
1302 }
1303
1304 for (i = 0; i < FSCHMD_NO_VOLT_SENSORS[data->kind]; i++)
1305 data->volt[i] = i2c_smbus_read_byte_data(client,
1306 FSCHMD_REG_VOLT[data->kind][i]);
1307
1308 data->last_updated = jiffies;
1309 data->valid = 1;
1310 }
1311
1312 mutex_unlock(&data->update_lock);
1313
1314 return data;
1315}
1316
1317static int __init fschmd_init(void)
1318{
1319 return i2c_add_driver(&fschmd_driver);
1320}
1321
1322static void __exit fschmd_exit(void)
1323{
1324 i2c_del_driver(&fschmd_driver);
1325}
1326
1327MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
1328MODULE_DESCRIPTION("FSC Poseidon, Hermes, Scylla, Heracles, Heimdall, Hades "
1329 "and Syleus driver");
1330MODULE_LICENSE("GPL");
1331
1332module_init(fschmd_init);
1333module_exit(fschmd_exit);
1/*
2 * fschmd.c
3 *
4 * Copyright (C) 2007 - 2009 Hans de Goede <hdegoede@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21/*
22 * Merged Fujitsu Siemens hwmon driver, supporting the Poseidon, Hermes,
23 * Scylla, Heracles, Heimdall, Hades and Syleus chips
24 *
25 * Based on the original 2.4 fscscy, 2.6 fscpos, 2.6 fscher and 2.6
26 * (candidate) fschmd drivers:
27 * Copyright (C) 2006 Thilo Cestonaro
28 * <thilo.cestonaro.external@fujitsu-siemens.com>
29 * Copyright (C) 2004, 2005 Stefan Ott <stefan@desire.ch>
30 * Copyright (C) 2003, 2004 Reinhard Nissl <rnissl@gmx.de>
31 * Copyright (c) 2001 Martin Knoblauch <mkn@teraport.de, knobi@knobisoft.de>
32 * Copyright (C) 2000 Hermann Jung <hej@odn.de>
33 */
34
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/slab.h>
38#include <linux/jiffies.h>
39#include <linux/i2c.h>
40#include <linux/hwmon.h>
41#include <linux/hwmon-sysfs.h>
42#include <linux/err.h>
43#include <linux/mutex.h>
44#include <linux/sysfs.h>
45#include <linux/dmi.h>
46#include <linux/fs.h>
47#include <linux/watchdog.h>
48#include <linux/miscdevice.h>
49#include <linux/uaccess.h>
50#include <linux/kref.h>
51
52/* Addresses to scan */
53static const unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
54
55/* Insmod parameters */
56static bool nowayout = WATCHDOG_NOWAYOUT;
57module_param(nowayout, bool, 0);
58MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
59 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
60
61enum chips { fscpos, fscher, fscscy, fschrc, fschmd, fschds, fscsyl };
62
63/*
64 * The FSCHMD registers and other defines
65 */
66
67/* chip identification */
68#define FSCHMD_REG_IDENT_0 0x00
69#define FSCHMD_REG_IDENT_1 0x01
70#define FSCHMD_REG_IDENT_2 0x02
71#define FSCHMD_REG_REVISION 0x03
72
73/* global control and status */
74#define FSCHMD_REG_EVENT_STATE 0x04
75#define FSCHMD_REG_CONTROL 0x05
76
77#define FSCHMD_CONTROL_ALERT_LED 0x01
78
79/* watchdog */
80static const u8 FSCHMD_REG_WDOG_CONTROL[7] = {
81 0x21, 0x21, 0x21, 0x21, 0x21, 0x28, 0x28 };
82static const u8 FSCHMD_REG_WDOG_STATE[7] = {
83 0x23, 0x23, 0x23, 0x23, 0x23, 0x29, 0x29 };
84static const u8 FSCHMD_REG_WDOG_PRESET[7] = {
85 0x28, 0x28, 0x28, 0x28, 0x28, 0x2a, 0x2a };
86
87#define FSCHMD_WDOG_CONTROL_TRIGGER 0x10
88#define FSCHMD_WDOG_CONTROL_STARTED 0x10 /* the same as trigger */
89#define FSCHMD_WDOG_CONTROL_STOP 0x20
90#define FSCHMD_WDOG_CONTROL_RESOLUTION 0x40
91
92#define FSCHMD_WDOG_STATE_CARDRESET 0x02
93
94/* voltages, weird order is to keep the same order as the old drivers */
95static const u8 FSCHMD_REG_VOLT[7][6] = {
96 { 0x45, 0x42, 0x48 }, /* pos */
97 { 0x45, 0x42, 0x48 }, /* her */
98 { 0x45, 0x42, 0x48 }, /* scy */
99 { 0x45, 0x42, 0x48 }, /* hrc */
100 { 0x45, 0x42, 0x48 }, /* hmd */
101 { 0x21, 0x20, 0x22 }, /* hds */
102 { 0x21, 0x20, 0x22, 0x23, 0x24, 0x25 }, /* syl */
103};
104
105static const int FSCHMD_NO_VOLT_SENSORS[7] = { 3, 3, 3, 3, 3, 3, 6 };
106
107/*
108 * minimum pwm at which the fan is driven (pwm can by increased depending on
109 * the temp. Notice that for the scy some fans share there minimum speed.
110 * Also notice that with the scy the sensor order is different than with the
111 * other chips, this order was in the 2.4 driver and kept for consistency.
112 */
113static const u8 FSCHMD_REG_FAN_MIN[7][7] = {
114 { 0x55, 0x65 }, /* pos */
115 { 0x55, 0x65, 0xb5 }, /* her */
116 { 0x65, 0x65, 0x55, 0xa5, 0x55, 0xa5 }, /* scy */
117 { 0x55, 0x65, 0xa5, 0xb5 }, /* hrc */
118 { 0x55, 0x65, 0xa5, 0xb5, 0xc5 }, /* hmd */
119 { 0x55, 0x65, 0xa5, 0xb5, 0xc5 }, /* hds */
120 { 0x54, 0x64, 0x74, 0x84, 0x94, 0xa4, 0xb4 }, /* syl */
121};
122
123/* actual fan speed */
124static const u8 FSCHMD_REG_FAN_ACT[7][7] = {
125 { 0x0e, 0x6b, 0xab }, /* pos */
126 { 0x0e, 0x6b, 0xbb }, /* her */
127 { 0x6b, 0x6c, 0x0e, 0xab, 0x5c, 0xbb }, /* scy */
128 { 0x0e, 0x6b, 0xab, 0xbb }, /* hrc */
129 { 0x5b, 0x6b, 0xab, 0xbb, 0xcb }, /* hmd */
130 { 0x5b, 0x6b, 0xab, 0xbb, 0xcb }, /* hds */
131 { 0x57, 0x67, 0x77, 0x87, 0x97, 0xa7, 0xb7 }, /* syl */
132};
133
134/* fan status registers */
135static const u8 FSCHMD_REG_FAN_STATE[7][7] = {
136 { 0x0d, 0x62, 0xa2 }, /* pos */
137 { 0x0d, 0x62, 0xb2 }, /* her */
138 { 0x62, 0x61, 0x0d, 0xa2, 0x52, 0xb2 }, /* scy */
139 { 0x0d, 0x62, 0xa2, 0xb2 }, /* hrc */
140 { 0x52, 0x62, 0xa2, 0xb2, 0xc2 }, /* hmd */
141 { 0x52, 0x62, 0xa2, 0xb2, 0xc2 }, /* hds */
142 { 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0 }, /* syl */
143};
144
145/* fan ripple / divider registers */
146static const u8 FSCHMD_REG_FAN_RIPPLE[7][7] = {
147 { 0x0f, 0x6f, 0xaf }, /* pos */
148 { 0x0f, 0x6f, 0xbf }, /* her */
149 { 0x6f, 0x6f, 0x0f, 0xaf, 0x0f, 0xbf }, /* scy */
150 { 0x0f, 0x6f, 0xaf, 0xbf }, /* hrc */
151 { 0x5f, 0x6f, 0xaf, 0xbf, 0xcf }, /* hmd */
152 { 0x5f, 0x6f, 0xaf, 0xbf, 0xcf }, /* hds */
153 { 0x56, 0x66, 0x76, 0x86, 0x96, 0xa6, 0xb6 }, /* syl */
154};
155
156static const int FSCHMD_NO_FAN_SENSORS[7] = { 3, 3, 6, 4, 5, 5, 7 };
157
158/* Fan status register bitmasks */
159#define FSCHMD_FAN_ALARM 0x04 /* called fault by FSC! */
160#define FSCHMD_FAN_NOT_PRESENT 0x08
161#define FSCHMD_FAN_DISABLED 0x80
162
163
164/* actual temperature registers */
165static const u8 FSCHMD_REG_TEMP_ACT[7][11] = {
166 { 0x64, 0x32, 0x35 }, /* pos */
167 { 0x64, 0x32, 0x35 }, /* her */
168 { 0x64, 0xD0, 0x32, 0x35 }, /* scy */
169 { 0x64, 0x32, 0x35 }, /* hrc */
170 { 0x70, 0x80, 0x90, 0xd0, 0xe0 }, /* hmd */
171 { 0x70, 0x80, 0x90, 0xd0, 0xe0 }, /* hds */
172 { 0x58, 0x68, 0x78, 0x88, 0x98, 0xa8, /* syl */
173 0xb8, 0xc8, 0xd8, 0xe8, 0xf8 },
174};
175
176/* temperature state registers */
177static const u8 FSCHMD_REG_TEMP_STATE[7][11] = {
178 { 0x71, 0x81, 0x91 }, /* pos */
179 { 0x71, 0x81, 0x91 }, /* her */
180 { 0x71, 0xd1, 0x81, 0x91 }, /* scy */
181 { 0x71, 0x81, 0x91 }, /* hrc */
182 { 0x71, 0x81, 0x91, 0xd1, 0xe1 }, /* hmd */
183 { 0x71, 0x81, 0x91, 0xd1, 0xe1 }, /* hds */
184 { 0x59, 0x69, 0x79, 0x89, 0x99, 0xa9, /* syl */
185 0xb9, 0xc9, 0xd9, 0xe9, 0xf9 },
186};
187
188/*
189 * temperature high limit registers, FSC does not document these. Proven to be
190 * there with field testing on the fscher and fschrc, already supported / used
191 * in the fscscy 2.4 driver. FSC has confirmed that the fschmd has registers
192 * at these addresses, but doesn't want to confirm they are the same as with
193 * the fscher??
194 */
195static const u8 FSCHMD_REG_TEMP_LIMIT[7][11] = {
196 { 0, 0, 0 }, /* pos */
197 { 0x76, 0x86, 0x96 }, /* her */
198 { 0x76, 0xd6, 0x86, 0x96 }, /* scy */
199 { 0x76, 0x86, 0x96 }, /* hrc */
200 { 0x76, 0x86, 0x96, 0xd6, 0xe6 }, /* hmd */
201 { 0x76, 0x86, 0x96, 0xd6, 0xe6 }, /* hds */
202 { 0x5a, 0x6a, 0x7a, 0x8a, 0x9a, 0xaa, /* syl */
203 0xba, 0xca, 0xda, 0xea, 0xfa },
204};
205
206/*
207 * These were found through experimenting with an fscher, currently they are
208 * not used, but we keep them around for future reference.
209 * On the fscsyl AUTOP1 lives at 0x#c (so 0x5c for fan1, 0x6c for fan2, etc),
210 * AUTOP2 lives at 0x#e, and 0x#1 is a bitmask defining which temps influence
211 * the fan speed.
212 * static const u8 FSCHER_REG_TEMP_AUTOP1[] = { 0x73, 0x83, 0x93 };
213 * static const u8 FSCHER_REG_TEMP_AUTOP2[] = { 0x75, 0x85, 0x95 };
214 */
215
216static const int FSCHMD_NO_TEMP_SENSORS[7] = { 3, 3, 4, 3, 5, 5, 11 };
217
218/* temp status register bitmasks */
219#define FSCHMD_TEMP_WORKING 0x01
220#define FSCHMD_TEMP_ALERT 0x02
221#define FSCHMD_TEMP_DISABLED 0x80
222/* there only really is an alarm if the sensor is working and alert == 1 */
223#define FSCHMD_TEMP_ALARM_MASK \
224 (FSCHMD_TEMP_WORKING | FSCHMD_TEMP_ALERT)
225
226/*
227 * Functions declarations
228 */
229
230static int fschmd_probe(struct i2c_client *client,
231 const struct i2c_device_id *id);
232static int fschmd_detect(struct i2c_client *client,
233 struct i2c_board_info *info);
234static int fschmd_remove(struct i2c_client *client);
235static struct fschmd_data *fschmd_update_device(struct device *dev);
236
237/*
238 * Driver data (common to all clients)
239 */
240
241static const struct i2c_device_id fschmd_id[] = {
242 { "fscpos", fscpos },
243 { "fscher", fscher },
244 { "fscscy", fscscy },
245 { "fschrc", fschrc },
246 { "fschmd", fschmd },
247 { "fschds", fschds },
248 { "fscsyl", fscsyl },
249 { }
250};
251MODULE_DEVICE_TABLE(i2c, fschmd_id);
252
253static struct i2c_driver fschmd_driver = {
254 .class = I2C_CLASS_HWMON,
255 .driver = {
256 .name = "fschmd",
257 },
258 .probe = fschmd_probe,
259 .remove = fschmd_remove,
260 .id_table = fschmd_id,
261 .detect = fschmd_detect,
262 .address_list = normal_i2c,
263};
264
265/*
266 * Client data (each client gets its own)
267 */
268
269struct fschmd_data {
270 struct i2c_client *client;
271 struct device *hwmon_dev;
272 struct mutex update_lock;
273 struct mutex watchdog_lock;
274 struct list_head list; /* member of the watchdog_data_list */
275 struct kref kref;
276 struct miscdevice watchdog_miscdev;
277 enum chips kind;
278 unsigned long watchdog_is_open;
279 char watchdog_expect_close;
280 char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
281 char valid; /* zero until following fields are valid */
282 unsigned long last_updated; /* in jiffies */
283
284 /* register values */
285 u8 revision; /* chip revision */
286 u8 global_control; /* global control register */
287 u8 watchdog_control; /* watchdog control register */
288 u8 watchdog_state; /* watchdog status register */
289 u8 watchdog_preset; /* watchdog counter preset on trigger val */
290 u8 volt[6]; /* voltage */
291 u8 temp_act[11]; /* temperature */
292 u8 temp_status[11]; /* status of sensor */
293 u8 temp_max[11]; /* high temp limit, notice: undocumented! */
294 u8 fan_act[7]; /* fans revolutions per second */
295 u8 fan_status[7]; /* fan status */
296 u8 fan_min[7]; /* fan min value for rps */
297 u8 fan_ripple[7]; /* divider for rps */
298};
299
300/*
301 * Global variables to hold information read from special DMI tables, which are
302 * available on FSC machines with an fscher or later chip. There is no need to
303 * protect these with a lock as they are only modified from our attach function
304 * which always gets called with the i2c-core lock held and never accessed
305 * before the attach function is done with them.
306 */
307static int dmi_mult[6] = { 490, 200, 100, 100, 200, 100 };
308static int dmi_offset[6] = { 0, 0, 0, 0, 0, 0 };
309static int dmi_vref = -1;
310
311/*
312 * Somewhat ugly :( global data pointer list with all fschmd devices, so that
313 * we can find our device data as when using misc_register there is no other
314 * method to get to ones device data from the open fop.
315 */
316static LIST_HEAD(watchdog_data_list);
317/* Note this lock not only protect list access, but also data.kref access */
318static DEFINE_MUTEX(watchdog_data_mutex);
319
320/*
321 * Release our data struct when we're detached from the i2c client *and* all
322 * references to our watchdog device are released
323 */
324static void fschmd_release_resources(struct kref *ref)
325{
326 struct fschmd_data *data = container_of(ref, struct fschmd_data, kref);
327 kfree(data);
328}
329
330/*
331 * Sysfs attr show / store functions
332 */
333
334static ssize_t show_in_value(struct device *dev,
335 struct device_attribute *devattr, char *buf)
336{
337 const int max_reading[3] = { 14200, 6600, 3300 };
338 int index = to_sensor_dev_attr(devattr)->index;
339 struct fschmd_data *data = fschmd_update_device(dev);
340
341 if (data->kind == fscher || data->kind >= fschrc)
342 return sprintf(buf, "%d\n", (data->volt[index] * dmi_vref *
343 dmi_mult[index]) / 255 + dmi_offset[index]);
344 else
345 return sprintf(buf, "%d\n", (data->volt[index] *
346 max_reading[index] + 128) / 255);
347}
348
349
350#define TEMP_FROM_REG(val) (((val) - 128) * 1000)
351
352static ssize_t show_temp_value(struct device *dev,
353 struct device_attribute *devattr, char *buf)
354{
355 int index = to_sensor_dev_attr(devattr)->index;
356 struct fschmd_data *data = fschmd_update_device(dev);
357
358 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_act[index]));
359}
360
361static ssize_t show_temp_max(struct device *dev,
362 struct device_attribute *devattr, char *buf)
363{
364 int index = to_sensor_dev_attr(devattr)->index;
365 struct fschmd_data *data = fschmd_update_device(dev);
366
367 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[index]));
368}
369
370static ssize_t store_temp_max(struct device *dev, struct device_attribute
371 *devattr, const char *buf, size_t count)
372{
373 int index = to_sensor_dev_attr(devattr)->index;
374 struct fschmd_data *data = dev_get_drvdata(dev);
375 long v;
376 int err;
377
378 err = kstrtol(buf, 10, &v);
379 if (err)
380 return err;
381
382 v = SENSORS_LIMIT(v / 1000, -128, 127) + 128;
383
384 mutex_lock(&data->update_lock);
385 i2c_smbus_write_byte_data(to_i2c_client(dev),
386 FSCHMD_REG_TEMP_LIMIT[data->kind][index], v);
387 data->temp_max[index] = v;
388 mutex_unlock(&data->update_lock);
389
390 return count;
391}
392
393static ssize_t show_temp_fault(struct device *dev,
394 struct device_attribute *devattr, char *buf)
395{
396 int index = to_sensor_dev_attr(devattr)->index;
397 struct fschmd_data *data = fschmd_update_device(dev);
398
399 /* bit 0 set means sensor working ok, so no fault! */
400 if (data->temp_status[index] & FSCHMD_TEMP_WORKING)
401 return sprintf(buf, "0\n");
402 else
403 return sprintf(buf, "1\n");
404}
405
406static ssize_t show_temp_alarm(struct device *dev,
407 struct device_attribute *devattr, char *buf)
408{
409 int index = to_sensor_dev_attr(devattr)->index;
410 struct fschmd_data *data = fschmd_update_device(dev);
411
412 if ((data->temp_status[index] & FSCHMD_TEMP_ALARM_MASK) ==
413 FSCHMD_TEMP_ALARM_MASK)
414 return sprintf(buf, "1\n");
415 else
416 return sprintf(buf, "0\n");
417}
418
419
420#define RPM_FROM_REG(val) ((val) * 60)
421
422static ssize_t show_fan_value(struct device *dev,
423 struct device_attribute *devattr, char *buf)
424{
425 int index = to_sensor_dev_attr(devattr)->index;
426 struct fschmd_data *data = fschmd_update_device(dev);
427
428 return sprintf(buf, "%u\n", RPM_FROM_REG(data->fan_act[index]));
429}
430
431static ssize_t show_fan_div(struct device *dev,
432 struct device_attribute *devattr, char *buf)
433{
434 int index = to_sensor_dev_attr(devattr)->index;
435 struct fschmd_data *data = fschmd_update_device(dev);
436
437 /* bits 2..7 reserved => mask with 3 */
438 return sprintf(buf, "%d\n", 1 << (data->fan_ripple[index] & 3));
439}
440
441static ssize_t store_fan_div(struct device *dev, struct device_attribute
442 *devattr, const char *buf, size_t count)
443{
444 u8 reg;
445 int index = to_sensor_dev_attr(devattr)->index;
446 struct fschmd_data *data = dev_get_drvdata(dev);
447 /* supported values: 2, 4, 8 */
448 unsigned long v;
449 int err;
450
451 err = kstrtoul(buf, 10, &v);
452 if (err)
453 return err;
454
455 switch (v) {
456 case 2:
457 v = 1;
458 break;
459 case 4:
460 v = 2;
461 break;
462 case 8:
463 v = 3;
464 break;
465 default:
466 dev_err(dev, "fan_div value %lu not supported. "
467 "Choose one of 2, 4 or 8!\n", v);
468 return -EINVAL;
469 }
470
471 mutex_lock(&data->update_lock);
472
473 reg = i2c_smbus_read_byte_data(to_i2c_client(dev),
474 FSCHMD_REG_FAN_RIPPLE[data->kind][index]);
475
476 /* bits 2..7 reserved => mask with 0x03 */
477 reg &= ~0x03;
478 reg |= v;
479
480 i2c_smbus_write_byte_data(to_i2c_client(dev),
481 FSCHMD_REG_FAN_RIPPLE[data->kind][index], reg);
482
483 data->fan_ripple[index] = reg;
484
485 mutex_unlock(&data->update_lock);
486
487 return count;
488}
489
490static ssize_t show_fan_alarm(struct device *dev,
491 struct device_attribute *devattr, char *buf)
492{
493 int index = to_sensor_dev_attr(devattr)->index;
494 struct fschmd_data *data = fschmd_update_device(dev);
495
496 if (data->fan_status[index] & FSCHMD_FAN_ALARM)
497 return sprintf(buf, "1\n");
498 else
499 return sprintf(buf, "0\n");
500}
501
502static ssize_t show_fan_fault(struct device *dev,
503 struct device_attribute *devattr, char *buf)
504{
505 int index = to_sensor_dev_attr(devattr)->index;
506 struct fschmd_data *data = fschmd_update_device(dev);
507
508 if (data->fan_status[index] & FSCHMD_FAN_NOT_PRESENT)
509 return sprintf(buf, "1\n");
510 else
511 return sprintf(buf, "0\n");
512}
513
514
515static ssize_t show_pwm_auto_point1_pwm(struct device *dev,
516 struct device_attribute *devattr, char *buf)
517{
518 int index = to_sensor_dev_attr(devattr)->index;
519 struct fschmd_data *data = fschmd_update_device(dev);
520 int val = data->fan_min[index];
521
522 /* 0 = allow turning off (except on the syl), 1-255 = 50-100% */
523 if (val || data->kind == fscsyl)
524 val = val / 2 + 128;
525
526 return sprintf(buf, "%d\n", val);
527}
528
529static ssize_t store_pwm_auto_point1_pwm(struct device *dev,
530 struct device_attribute *devattr, const char *buf, size_t count)
531{
532 int index = to_sensor_dev_attr(devattr)->index;
533 struct fschmd_data *data = dev_get_drvdata(dev);
534 unsigned long v;
535 int err;
536
537 err = kstrtoul(buf, 10, &v);
538 if (err)
539 return err;
540
541 /* reg: 0 = allow turning off (except on the syl), 1-255 = 50-100% */
542 if (v || data->kind == fscsyl) {
543 v = SENSORS_LIMIT(v, 128, 255);
544 v = (v - 128) * 2 + 1;
545 }
546
547 mutex_lock(&data->update_lock);
548
549 i2c_smbus_write_byte_data(to_i2c_client(dev),
550 FSCHMD_REG_FAN_MIN[data->kind][index], v);
551 data->fan_min[index] = v;
552
553 mutex_unlock(&data->update_lock);
554
555 return count;
556}
557
558
559/*
560 * The FSC hwmon family has the ability to force an attached alert led to flash
561 * from software, we export this as an alert_led sysfs attr
562 */
563static ssize_t show_alert_led(struct device *dev,
564 struct device_attribute *devattr, char *buf)
565{
566 struct fschmd_data *data = fschmd_update_device(dev);
567
568 if (data->global_control & FSCHMD_CONTROL_ALERT_LED)
569 return sprintf(buf, "1\n");
570 else
571 return sprintf(buf, "0\n");
572}
573
574static ssize_t store_alert_led(struct device *dev,
575 struct device_attribute *devattr, const char *buf, size_t count)
576{
577 u8 reg;
578 struct fschmd_data *data = dev_get_drvdata(dev);
579 unsigned long v;
580 int err;
581
582 err = kstrtoul(buf, 10, &v);
583 if (err)
584 return err;
585
586 mutex_lock(&data->update_lock);
587
588 reg = i2c_smbus_read_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL);
589
590 if (v)
591 reg |= FSCHMD_CONTROL_ALERT_LED;
592 else
593 reg &= ~FSCHMD_CONTROL_ALERT_LED;
594
595 i2c_smbus_write_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL, reg);
596
597 data->global_control = reg;
598
599 mutex_unlock(&data->update_lock);
600
601 return count;
602}
603
604static DEVICE_ATTR(alert_led, 0644, show_alert_led, store_alert_led);
605
606static struct sensor_device_attribute fschmd_attr[] = {
607 SENSOR_ATTR(in0_input, 0444, show_in_value, NULL, 0),
608 SENSOR_ATTR(in1_input, 0444, show_in_value, NULL, 1),
609 SENSOR_ATTR(in2_input, 0444, show_in_value, NULL, 2),
610 SENSOR_ATTR(in3_input, 0444, show_in_value, NULL, 3),
611 SENSOR_ATTR(in4_input, 0444, show_in_value, NULL, 4),
612 SENSOR_ATTR(in5_input, 0444, show_in_value, NULL, 5),
613};
614
615static struct sensor_device_attribute fschmd_temp_attr[] = {
616 SENSOR_ATTR(temp1_input, 0444, show_temp_value, NULL, 0),
617 SENSOR_ATTR(temp1_max, 0644, show_temp_max, store_temp_max, 0),
618 SENSOR_ATTR(temp1_fault, 0444, show_temp_fault, NULL, 0),
619 SENSOR_ATTR(temp1_alarm, 0444, show_temp_alarm, NULL, 0),
620 SENSOR_ATTR(temp2_input, 0444, show_temp_value, NULL, 1),
621 SENSOR_ATTR(temp2_max, 0644, show_temp_max, store_temp_max, 1),
622 SENSOR_ATTR(temp2_fault, 0444, show_temp_fault, NULL, 1),
623 SENSOR_ATTR(temp2_alarm, 0444, show_temp_alarm, NULL, 1),
624 SENSOR_ATTR(temp3_input, 0444, show_temp_value, NULL, 2),
625 SENSOR_ATTR(temp3_max, 0644, show_temp_max, store_temp_max, 2),
626 SENSOR_ATTR(temp3_fault, 0444, show_temp_fault, NULL, 2),
627 SENSOR_ATTR(temp3_alarm, 0444, show_temp_alarm, NULL, 2),
628 SENSOR_ATTR(temp4_input, 0444, show_temp_value, NULL, 3),
629 SENSOR_ATTR(temp4_max, 0644, show_temp_max, store_temp_max, 3),
630 SENSOR_ATTR(temp4_fault, 0444, show_temp_fault, NULL, 3),
631 SENSOR_ATTR(temp4_alarm, 0444, show_temp_alarm, NULL, 3),
632 SENSOR_ATTR(temp5_input, 0444, show_temp_value, NULL, 4),
633 SENSOR_ATTR(temp5_max, 0644, show_temp_max, store_temp_max, 4),
634 SENSOR_ATTR(temp5_fault, 0444, show_temp_fault, NULL, 4),
635 SENSOR_ATTR(temp5_alarm, 0444, show_temp_alarm, NULL, 4),
636 SENSOR_ATTR(temp6_input, 0444, show_temp_value, NULL, 5),
637 SENSOR_ATTR(temp6_max, 0644, show_temp_max, store_temp_max, 5),
638 SENSOR_ATTR(temp6_fault, 0444, show_temp_fault, NULL, 5),
639 SENSOR_ATTR(temp6_alarm, 0444, show_temp_alarm, NULL, 5),
640 SENSOR_ATTR(temp7_input, 0444, show_temp_value, NULL, 6),
641 SENSOR_ATTR(temp7_max, 0644, show_temp_max, store_temp_max, 6),
642 SENSOR_ATTR(temp7_fault, 0444, show_temp_fault, NULL, 6),
643 SENSOR_ATTR(temp7_alarm, 0444, show_temp_alarm, NULL, 6),
644 SENSOR_ATTR(temp8_input, 0444, show_temp_value, NULL, 7),
645 SENSOR_ATTR(temp8_max, 0644, show_temp_max, store_temp_max, 7),
646 SENSOR_ATTR(temp8_fault, 0444, show_temp_fault, NULL, 7),
647 SENSOR_ATTR(temp8_alarm, 0444, show_temp_alarm, NULL, 7),
648 SENSOR_ATTR(temp9_input, 0444, show_temp_value, NULL, 8),
649 SENSOR_ATTR(temp9_max, 0644, show_temp_max, store_temp_max, 8),
650 SENSOR_ATTR(temp9_fault, 0444, show_temp_fault, NULL, 8),
651 SENSOR_ATTR(temp9_alarm, 0444, show_temp_alarm, NULL, 8),
652 SENSOR_ATTR(temp10_input, 0444, show_temp_value, NULL, 9),
653 SENSOR_ATTR(temp10_max, 0644, show_temp_max, store_temp_max, 9),
654 SENSOR_ATTR(temp10_fault, 0444, show_temp_fault, NULL, 9),
655 SENSOR_ATTR(temp10_alarm, 0444, show_temp_alarm, NULL, 9),
656 SENSOR_ATTR(temp11_input, 0444, show_temp_value, NULL, 10),
657 SENSOR_ATTR(temp11_max, 0644, show_temp_max, store_temp_max, 10),
658 SENSOR_ATTR(temp11_fault, 0444, show_temp_fault, NULL, 10),
659 SENSOR_ATTR(temp11_alarm, 0444, show_temp_alarm, NULL, 10),
660};
661
662static struct sensor_device_attribute fschmd_fan_attr[] = {
663 SENSOR_ATTR(fan1_input, 0444, show_fan_value, NULL, 0),
664 SENSOR_ATTR(fan1_div, 0644, show_fan_div, store_fan_div, 0),
665 SENSOR_ATTR(fan1_alarm, 0444, show_fan_alarm, NULL, 0),
666 SENSOR_ATTR(fan1_fault, 0444, show_fan_fault, NULL, 0),
667 SENSOR_ATTR(pwm1_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
668 store_pwm_auto_point1_pwm, 0),
669 SENSOR_ATTR(fan2_input, 0444, show_fan_value, NULL, 1),
670 SENSOR_ATTR(fan2_div, 0644, show_fan_div, store_fan_div, 1),
671 SENSOR_ATTR(fan2_alarm, 0444, show_fan_alarm, NULL, 1),
672 SENSOR_ATTR(fan2_fault, 0444, show_fan_fault, NULL, 1),
673 SENSOR_ATTR(pwm2_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
674 store_pwm_auto_point1_pwm, 1),
675 SENSOR_ATTR(fan3_input, 0444, show_fan_value, NULL, 2),
676 SENSOR_ATTR(fan3_div, 0644, show_fan_div, store_fan_div, 2),
677 SENSOR_ATTR(fan3_alarm, 0444, show_fan_alarm, NULL, 2),
678 SENSOR_ATTR(fan3_fault, 0444, show_fan_fault, NULL, 2),
679 SENSOR_ATTR(pwm3_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
680 store_pwm_auto_point1_pwm, 2),
681 SENSOR_ATTR(fan4_input, 0444, show_fan_value, NULL, 3),
682 SENSOR_ATTR(fan4_div, 0644, show_fan_div, store_fan_div, 3),
683 SENSOR_ATTR(fan4_alarm, 0444, show_fan_alarm, NULL, 3),
684 SENSOR_ATTR(fan4_fault, 0444, show_fan_fault, NULL, 3),
685 SENSOR_ATTR(pwm4_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
686 store_pwm_auto_point1_pwm, 3),
687 SENSOR_ATTR(fan5_input, 0444, show_fan_value, NULL, 4),
688 SENSOR_ATTR(fan5_div, 0644, show_fan_div, store_fan_div, 4),
689 SENSOR_ATTR(fan5_alarm, 0444, show_fan_alarm, NULL, 4),
690 SENSOR_ATTR(fan5_fault, 0444, show_fan_fault, NULL, 4),
691 SENSOR_ATTR(pwm5_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
692 store_pwm_auto_point1_pwm, 4),
693 SENSOR_ATTR(fan6_input, 0444, show_fan_value, NULL, 5),
694 SENSOR_ATTR(fan6_div, 0644, show_fan_div, store_fan_div, 5),
695 SENSOR_ATTR(fan6_alarm, 0444, show_fan_alarm, NULL, 5),
696 SENSOR_ATTR(fan6_fault, 0444, show_fan_fault, NULL, 5),
697 SENSOR_ATTR(pwm6_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
698 store_pwm_auto_point1_pwm, 5),
699 SENSOR_ATTR(fan7_input, 0444, show_fan_value, NULL, 6),
700 SENSOR_ATTR(fan7_div, 0644, show_fan_div, store_fan_div, 6),
701 SENSOR_ATTR(fan7_alarm, 0444, show_fan_alarm, NULL, 6),
702 SENSOR_ATTR(fan7_fault, 0444, show_fan_fault, NULL, 6),
703 SENSOR_ATTR(pwm7_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
704 store_pwm_auto_point1_pwm, 6),
705};
706
707
708/*
709 * Watchdog routines
710 */
711
712static int watchdog_set_timeout(struct fschmd_data *data, int timeout)
713{
714 int ret, resolution;
715 int kind = data->kind + 1; /* 0-x array index -> 1-x module param */
716
717 /* 2 second or 60 second resolution? */
718 if (timeout <= 510 || kind == fscpos || kind == fscscy)
719 resolution = 2;
720 else
721 resolution = 60;
722
723 if (timeout < resolution || timeout > (resolution * 255))
724 return -EINVAL;
725
726 mutex_lock(&data->watchdog_lock);
727 if (!data->client) {
728 ret = -ENODEV;
729 goto leave;
730 }
731
732 if (resolution == 2)
733 data->watchdog_control &= ~FSCHMD_WDOG_CONTROL_RESOLUTION;
734 else
735 data->watchdog_control |= FSCHMD_WDOG_CONTROL_RESOLUTION;
736
737 data->watchdog_preset = DIV_ROUND_UP(timeout, resolution);
738
739 /* Write new timeout value */
740 i2c_smbus_write_byte_data(data->client,
741 FSCHMD_REG_WDOG_PRESET[data->kind], data->watchdog_preset);
742 /* Write new control register, do not trigger! */
743 i2c_smbus_write_byte_data(data->client,
744 FSCHMD_REG_WDOG_CONTROL[data->kind],
745 data->watchdog_control & ~FSCHMD_WDOG_CONTROL_TRIGGER);
746
747 ret = data->watchdog_preset * resolution;
748
749leave:
750 mutex_unlock(&data->watchdog_lock);
751 return ret;
752}
753
754static int watchdog_get_timeout(struct fschmd_data *data)
755{
756 int timeout;
757
758 mutex_lock(&data->watchdog_lock);
759 if (data->watchdog_control & FSCHMD_WDOG_CONTROL_RESOLUTION)
760 timeout = data->watchdog_preset * 60;
761 else
762 timeout = data->watchdog_preset * 2;
763 mutex_unlock(&data->watchdog_lock);
764
765 return timeout;
766}
767
768static int watchdog_trigger(struct fschmd_data *data)
769{
770 int ret = 0;
771
772 mutex_lock(&data->watchdog_lock);
773 if (!data->client) {
774 ret = -ENODEV;
775 goto leave;
776 }
777
778 data->watchdog_control |= FSCHMD_WDOG_CONTROL_TRIGGER;
779 i2c_smbus_write_byte_data(data->client,
780 FSCHMD_REG_WDOG_CONTROL[data->kind],
781 data->watchdog_control);
782leave:
783 mutex_unlock(&data->watchdog_lock);
784 return ret;
785}
786
787static int watchdog_stop(struct fschmd_data *data)
788{
789 int ret = 0;
790
791 mutex_lock(&data->watchdog_lock);
792 if (!data->client) {
793 ret = -ENODEV;
794 goto leave;
795 }
796
797 data->watchdog_control &= ~FSCHMD_WDOG_CONTROL_STARTED;
798 /*
799 * Don't store the stop flag in our watchdog control register copy, as
800 * its a write only bit (read always returns 0)
801 */
802 i2c_smbus_write_byte_data(data->client,
803 FSCHMD_REG_WDOG_CONTROL[data->kind],
804 data->watchdog_control | FSCHMD_WDOG_CONTROL_STOP);
805leave:
806 mutex_unlock(&data->watchdog_lock);
807 return ret;
808}
809
810static int watchdog_open(struct inode *inode, struct file *filp)
811{
812 struct fschmd_data *pos, *data = NULL;
813 int watchdog_is_open;
814
815 /*
816 * We get called from drivers/char/misc.c with misc_mtx hold, and we
817 * call misc_register() from fschmd_probe() with watchdog_data_mutex
818 * hold, as misc_register() takes the misc_mtx lock, this is a possible
819 * deadlock, so we use mutex_trylock here.
820 */
821 if (!mutex_trylock(&watchdog_data_mutex))
822 return -ERESTARTSYS;
823 list_for_each_entry(pos, &watchdog_data_list, list) {
824 if (pos->watchdog_miscdev.minor == iminor(inode)) {
825 data = pos;
826 break;
827 }
828 }
829 /* Note we can never not have found data, so we don't check for this */
830 watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
831 if (!watchdog_is_open)
832 kref_get(&data->kref);
833 mutex_unlock(&watchdog_data_mutex);
834
835 if (watchdog_is_open)
836 return -EBUSY;
837
838 /* Start the watchdog */
839 watchdog_trigger(data);
840 filp->private_data = data;
841
842 return nonseekable_open(inode, filp);
843}
844
845static int watchdog_release(struct inode *inode, struct file *filp)
846{
847 struct fschmd_data *data = filp->private_data;
848
849 if (data->watchdog_expect_close) {
850 watchdog_stop(data);
851 data->watchdog_expect_close = 0;
852 } else {
853 watchdog_trigger(data);
854 dev_crit(&data->client->dev,
855 "unexpected close, not stopping watchdog!\n");
856 }
857
858 clear_bit(0, &data->watchdog_is_open);
859
860 mutex_lock(&watchdog_data_mutex);
861 kref_put(&data->kref, fschmd_release_resources);
862 mutex_unlock(&watchdog_data_mutex);
863
864 return 0;
865}
866
867static ssize_t watchdog_write(struct file *filp, const char __user *buf,
868 size_t count, loff_t *offset)
869{
870 int ret;
871 struct fschmd_data *data = filp->private_data;
872
873 if (count) {
874 if (!nowayout) {
875 size_t i;
876
877 /* Clear it in case it was set with a previous write */
878 data->watchdog_expect_close = 0;
879
880 for (i = 0; i != count; i++) {
881 char c;
882 if (get_user(c, buf + i))
883 return -EFAULT;
884 if (c == 'V')
885 data->watchdog_expect_close = 1;
886 }
887 }
888 ret = watchdog_trigger(data);
889 if (ret < 0)
890 return ret;
891 }
892 return count;
893}
894
895static long watchdog_ioctl(struct file *filp, unsigned int cmd,
896 unsigned long arg)
897{
898 struct watchdog_info ident = {
899 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
900 WDIOF_CARDRESET,
901 .identity = "FSC watchdog"
902 };
903 int i, ret = 0;
904 struct fschmd_data *data = filp->private_data;
905
906 switch (cmd) {
907 case WDIOC_GETSUPPORT:
908 ident.firmware_version = data->revision;
909 if (!nowayout)
910 ident.options |= WDIOF_MAGICCLOSE;
911 if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
912 ret = -EFAULT;
913 break;
914
915 case WDIOC_GETSTATUS:
916 ret = put_user(0, (int __user *)arg);
917 break;
918
919 case WDIOC_GETBOOTSTATUS:
920 if (data->watchdog_state & FSCHMD_WDOG_STATE_CARDRESET)
921 ret = put_user(WDIOF_CARDRESET, (int __user *)arg);
922 else
923 ret = put_user(0, (int __user *)arg);
924 break;
925
926 case WDIOC_KEEPALIVE:
927 ret = watchdog_trigger(data);
928 break;
929
930 case WDIOC_GETTIMEOUT:
931 i = watchdog_get_timeout(data);
932 ret = put_user(i, (int __user *)arg);
933 break;
934
935 case WDIOC_SETTIMEOUT:
936 if (get_user(i, (int __user *)arg)) {
937 ret = -EFAULT;
938 break;
939 }
940 ret = watchdog_set_timeout(data, i);
941 if (ret > 0)
942 ret = put_user(ret, (int __user *)arg);
943 break;
944
945 case WDIOC_SETOPTIONS:
946 if (get_user(i, (int __user *)arg)) {
947 ret = -EFAULT;
948 break;
949 }
950
951 if (i & WDIOS_DISABLECARD)
952 ret = watchdog_stop(data);
953 else if (i & WDIOS_ENABLECARD)
954 ret = watchdog_trigger(data);
955 else
956 ret = -EINVAL;
957
958 break;
959 default:
960 ret = -ENOTTY;
961 }
962 return ret;
963}
964
965static const struct file_operations watchdog_fops = {
966 .owner = THIS_MODULE,
967 .llseek = no_llseek,
968 .open = watchdog_open,
969 .release = watchdog_release,
970 .write = watchdog_write,
971 .unlocked_ioctl = watchdog_ioctl,
972};
973
974
975/*
976 * Detect, register, unregister and update device functions
977 */
978
979/*
980 * DMI decode routine to read voltage scaling factors from special DMI tables,
981 * which are available on FSC machines with an fscher or later chip.
982 */
983static void fschmd_dmi_decode(const struct dmi_header *header, void *dummy)
984{
985 int i, mult[3] = { 0 }, offset[3] = { 0 }, vref = 0, found = 0;
986
987 /*
988 * dmi code ugliness, we get passed the address of the contents of
989 * a complete DMI record, but in the form of a dmi_header pointer, in
990 * reality this address holds header->length bytes of which the header
991 * are the first 4 bytes
992 */
993 u8 *dmi_data = (u8 *)header;
994
995 /* We are looking for OEM-specific type 185 */
996 if (header->type != 185)
997 return;
998
999 /*
1000 * we are looking for what Siemens calls "subtype" 19, the subtype
1001 * is stored in byte 5 of the dmi block
1002 */
1003 if (header->length < 5 || dmi_data[4] != 19)
1004 return;
1005
1006 /*
1007 * After the subtype comes 1 unknown byte and then blocks of 5 bytes,
1008 * consisting of what Siemens calls an "Entity" number, followed by
1009 * 2 16-bit words in LSB first order
1010 */
1011 for (i = 6; (i + 4) < header->length; i += 5) {
1012 /* entity 1 - 3: voltage multiplier and offset */
1013 if (dmi_data[i] >= 1 && dmi_data[i] <= 3) {
1014 /* Our in sensors order and the DMI order differ */
1015 const int shuffle[3] = { 1, 0, 2 };
1016 int in = shuffle[dmi_data[i] - 1];
1017
1018 /* Check for twice the same entity */
1019 if (found & (1 << in))
1020 return;
1021
1022 mult[in] = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
1023 offset[in] = dmi_data[i + 3] | (dmi_data[i + 4] << 8);
1024
1025 found |= 1 << in;
1026 }
1027
1028 /* entity 7: reference voltage */
1029 if (dmi_data[i] == 7) {
1030 /* Check for twice the same entity */
1031 if (found & 0x08)
1032 return;
1033
1034 vref = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
1035
1036 found |= 0x08;
1037 }
1038 }
1039
1040 if (found == 0x0F) {
1041 for (i = 0; i < 3; i++) {
1042 dmi_mult[i] = mult[i] * 10;
1043 dmi_offset[i] = offset[i] * 10;
1044 }
1045 /*
1046 * According to the docs there should be separate dmi entries
1047 * for the mult's and offsets of in3-5 of the syl, but on
1048 * my test machine these are not present
1049 */
1050 dmi_mult[3] = dmi_mult[2];
1051 dmi_mult[4] = dmi_mult[1];
1052 dmi_mult[5] = dmi_mult[2];
1053 dmi_offset[3] = dmi_offset[2];
1054 dmi_offset[4] = dmi_offset[1];
1055 dmi_offset[5] = dmi_offset[2];
1056 dmi_vref = vref;
1057 }
1058}
1059
1060static int fschmd_detect(struct i2c_client *client,
1061 struct i2c_board_info *info)
1062{
1063 enum chips kind;
1064 struct i2c_adapter *adapter = client->adapter;
1065 char id[4];
1066
1067 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1068 return -ENODEV;
1069
1070 /* Detect & Identify the chip */
1071 id[0] = i2c_smbus_read_byte_data(client, FSCHMD_REG_IDENT_0);
1072 id[1] = i2c_smbus_read_byte_data(client, FSCHMD_REG_IDENT_1);
1073 id[2] = i2c_smbus_read_byte_data(client, FSCHMD_REG_IDENT_2);
1074 id[3] = '\0';
1075
1076 if (!strcmp(id, "PEG"))
1077 kind = fscpos;
1078 else if (!strcmp(id, "HER"))
1079 kind = fscher;
1080 else if (!strcmp(id, "SCY"))
1081 kind = fscscy;
1082 else if (!strcmp(id, "HRC"))
1083 kind = fschrc;
1084 else if (!strcmp(id, "HMD"))
1085 kind = fschmd;
1086 else if (!strcmp(id, "HDS"))
1087 kind = fschds;
1088 else if (!strcmp(id, "SYL"))
1089 kind = fscsyl;
1090 else
1091 return -ENODEV;
1092
1093 strlcpy(info->type, fschmd_id[kind].name, I2C_NAME_SIZE);
1094
1095 return 0;
1096}
1097
1098static int fschmd_probe(struct i2c_client *client,
1099 const struct i2c_device_id *id)
1100{
1101 struct fschmd_data *data;
1102 const char * const names[7] = { "Poseidon", "Hermes", "Scylla",
1103 "Heracles", "Heimdall", "Hades", "Syleus" };
1104 const int watchdog_minors[] = { WATCHDOG_MINOR, 212, 213, 214, 215 };
1105 int i, err;
1106 enum chips kind = id->driver_data;
1107
1108 data = kzalloc(sizeof(struct fschmd_data), GFP_KERNEL);
1109 if (!data)
1110 return -ENOMEM;
1111
1112 i2c_set_clientdata(client, data);
1113 mutex_init(&data->update_lock);
1114 mutex_init(&data->watchdog_lock);
1115 INIT_LIST_HEAD(&data->list);
1116 kref_init(&data->kref);
1117 /*
1118 * Store client pointer in our data struct for watchdog usage
1119 * (where the client is found through a data ptr instead of the
1120 * otherway around)
1121 */
1122 data->client = client;
1123 data->kind = kind;
1124
1125 if (kind == fscpos) {
1126 /*
1127 * The Poseidon has hardwired temp limits, fill these
1128 * in for the alarm resetting code
1129 */
1130 data->temp_max[0] = 70 + 128;
1131 data->temp_max[1] = 50 + 128;
1132 data->temp_max[2] = 50 + 128;
1133 }
1134
1135 /* Read the special DMI table for fscher and newer chips */
1136 if ((kind == fscher || kind >= fschrc) && dmi_vref == -1) {
1137 dmi_walk(fschmd_dmi_decode, NULL);
1138 if (dmi_vref == -1) {
1139 dev_warn(&client->dev,
1140 "Couldn't get voltage scaling factors from "
1141 "BIOS DMI table, using builtin defaults\n");
1142 dmi_vref = 33;
1143 }
1144 }
1145
1146 /* Read in some never changing registers */
1147 data->revision = i2c_smbus_read_byte_data(client, FSCHMD_REG_REVISION);
1148 data->global_control = i2c_smbus_read_byte_data(client,
1149 FSCHMD_REG_CONTROL);
1150 data->watchdog_control = i2c_smbus_read_byte_data(client,
1151 FSCHMD_REG_WDOG_CONTROL[data->kind]);
1152 data->watchdog_state = i2c_smbus_read_byte_data(client,
1153 FSCHMD_REG_WDOG_STATE[data->kind]);
1154 data->watchdog_preset = i2c_smbus_read_byte_data(client,
1155 FSCHMD_REG_WDOG_PRESET[data->kind]);
1156
1157 err = device_create_file(&client->dev, &dev_attr_alert_led);
1158 if (err)
1159 goto exit_detach;
1160
1161 for (i = 0; i < FSCHMD_NO_VOLT_SENSORS[data->kind]; i++) {
1162 err = device_create_file(&client->dev,
1163 &fschmd_attr[i].dev_attr);
1164 if (err)
1165 goto exit_detach;
1166 }
1167
1168 for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++) {
1169 /* Poseidon doesn't have TEMP_LIMIT registers */
1170 if (kind == fscpos && fschmd_temp_attr[i].dev_attr.show ==
1171 show_temp_max)
1172 continue;
1173
1174 if (kind == fscsyl) {
1175 if (i % 4 == 0)
1176 data->temp_status[i / 4] =
1177 i2c_smbus_read_byte_data(client,
1178 FSCHMD_REG_TEMP_STATE
1179 [data->kind][i / 4]);
1180 if (data->temp_status[i / 4] & FSCHMD_TEMP_DISABLED)
1181 continue;
1182 }
1183
1184 err = device_create_file(&client->dev,
1185 &fschmd_temp_attr[i].dev_attr);
1186 if (err)
1187 goto exit_detach;
1188 }
1189
1190 for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++) {
1191 /* Poseidon doesn't have a FAN_MIN register for its 3rd fan */
1192 if (kind == fscpos &&
1193 !strcmp(fschmd_fan_attr[i].dev_attr.attr.name,
1194 "pwm3_auto_point1_pwm"))
1195 continue;
1196
1197 if (kind == fscsyl) {
1198 if (i % 5 == 0)
1199 data->fan_status[i / 5] =
1200 i2c_smbus_read_byte_data(client,
1201 FSCHMD_REG_FAN_STATE
1202 [data->kind][i / 5]);
1203 if (data->fan_status[i / 5] & FSCHMD_FAN_DISABLED)
1204 continue;
1205 }
1206
1207 err = device_create_file(&client->dev,
1208 &fschmd_fan_attr[i].dev_attr);
1209 if (err)
1210 goto exit_detach;
1211 }
1212
1213 data->hwmon_dev = hwmon_device_register(&client->dev);
1214 if (IS_ERR(data->hwmon_dev)) {
1215 err = PTR_ERR(data->hwmon_dev);
1216 data->hwmon_dev = NULL;
1217 goto exit_detach;
1218 }
1219
1220 /*
1221 * We take the data_mutex lock early so that watchdog_open() cannot
1222 * run when misc_register() has completed, but we've not yet added
1223 * our data to the watchdog_data_list (and set the default timeout)
1224 */
1225 mutex_lock(&watchdog_data_mutex);
1226 for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
1227 /* Register our watchdog part */
1228 snprintf(data->watchdog_name, sizeof(data->watchdog_name),
1229 "watchdog%c", (i == 0) ? '\0' : ('0' + i));
1230 data->watchdog_miscdev.name = data->watchdog_name;
1231 data->watchdog_miscdev.fops = &watchdog_fops;
1232 data->watchdog_miscdev.minor = watchdog_minors[i];
1233 err = misc_register(&data->watchdog_miscdev);
1234 if (err == -EBUSY)
1235 continue;
1236 if (err) {
1237 data->watchdog_miscdev.minor = 0;
1238 dev_err(&client->dev,
1239 "Registering watchdog chardev: %d\n", err);
1240 break;
1241 }
1242
1243 list_add(&data->list, &watchdog_data_list);
1244 watchdog_set_timeout(data, 60);
1245 dev_info(&client->dev,
1246 "Registered watchdog chardev major 10, minor: %d\n",
1247 watchdog_minors[i]);
1248 break;
1249 }
1250 if (i == ARRAY_SIZE(watchdog_minors)) {
1251 data->watchdog_miscdev.minor = 0;
1252 dev_warn(&client->dev, "Couldn't register watchdog chardev "
1253 "(due to no free minor)\n");
1254 }
1255 mutex_unlock(&watchdog_data_mutex);
1256
1257 dev_info(&client->dev, "Detected FSC %s chip, revision: %d\n",
1258 names[data->kind], (int) data->revision);
1259
1260 return 0;
1261
1262exit_detach:
1263 fschmd_remove(client); /* will also free data for us */
1264 return err;
1265}
1266
1267static int fschmd_remove(struct i2c_client *client)
1268{
1269 struct fschmd_data *data = i2c_get_clientdata(client);
1270 int i;
1271
1272 /* Unregister the watchdog (if registered) */
1273 if (data->watchdog_miscdev.minor) {
1274 misc_deregister(&data->watchdog_miscdev);
1275 if (data->watchdog_is_open) {
1276 dev_warn(&client->dev,
1277 "i2c client detached with watchdog open! "
1278 "Stopping watchdog.\n");
1279 watchdog_stop(data);
1280 }
1281 mutex_lock(&watchdog_data_mutex);
1282 list_del(&data->list);
1283 mutex_unlock(&watchdog_data_mutex);
1284 /* Tell the watchdog code the client is gone */
1285 mutex_lock(&data->watchdog_lock);
1286 data->client = NULL;
1287 mutex_unlock(&data->watchdog_lock);
1288 }
1289
1290 /*
1291 * Check if registered in case we're called from fschmd_detect
1292 * to cleanup after an error
1293 */
1294 if (data->hwmon_dev)
1295 hwmon_device_unregister(data->hwmon_dev);
1296
1297 device_remove_file(&client->dev, &dev_attr_alert_led);
1298 for (i = 0; i < (FSCHMD_NO_VOLT_SENSORS[data->kind]); i++)
1299 device_remove_file(&client->dev, &fschmd_attr[i].dev_attr);
1300 for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++)
1301 device_remove_file(&client->dev,
1302 &fschmd_temp_attr[i].dev_attr);
1303 for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++)
1304 device_remove_file(&client->dev,
1305 &fschmd_fan_attr[i].dev_attr);
1306
1307 mutex_lock(&watchdog_data_mutex);
1308 kref_put(&data->kref, fschmd_release_resources);
1309 mutex_unlock(&watchdog_data_mutex);
1310
1311 return 0;
1312}
1313
1314static struct fschmd_data *fschmd_update_device(struct device *dev)
1315{
1316 struct i2c_client *client = to_i2c_client(dev);
1317 struct fschmd_data *data = i2c_get_clientdata(client);
1318 int i;
1319
1320 mutex_lock(&data->update_lock);
1321
1322 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
1323
1324 for (i = 0; i < FSCHMD_NO_TEMP_SENSORS[data->kind]; i++) {
1325 data->temp_act[i] = i2c_smbus_read_byte_data(client,
1326 FSCHMD_REG_TEMP_ACT[data->kind][i]);
1327 data->temp_status[i] = i2c_smbus_read_byte_data(client,
1328 FSCHMD_REG_TEMP_STATE[data->kind][i]);
1329
1330 /* The fscpos doesn't have TEMP_LIMIT registers */
1331 if (FSCHMD_REG_TEMP_LIMIT[data->kind][i])
1332 data->temp_max[i] = i2c_smbus_read_byte_data(
1333 client,
1334 FSCHMD_REG_TEMP_LIMIT[data->kind][i]);
1335
1336 /*
1337 * reset alarm if the alarm condition is gone,
1338 * the chip doesn't do this itself
1339 */
1340 if ((data->temp_status[i] & FSCHMD_TEMP_ALARM_MASK) ==
1341 FSCHMD_TEMP_ALARM_MASK &&
1342 data->temp_act[i] < data->temp_max[i])
1343 i2c_smbus_write_byte_data(client,
1344 FSCHMD_REG_TEMP_STATE[data->kind][i],
1345 data->temp_status[i]);
1346 }
1347
1348 for (i = 0; i < FSCHMD_NO_FAN_SENSORS[data->kind]; i++) {
1349 data->fan_act[i] = i2c_smbus_read_byte_data(client,
1350 FSCHMD_REG_FAN_ACT[data->kind][i]);
1351 data->fan_status[i] = i2c_smbus_read_byte_data(client,
1352 FSCHMD_REG_FAN_STATE[data->kind][i]);
1353 data->fan_ripple[i] = i2c_smbus_read_byte_data(client,
1354 FSCHMD_REG_FAN_RIPPLE[data->kind][i]);
1355
1356 /* The fscpos third fan doesn't have a fan_min */
1357 if (FSCHMD_REG_FAN_MIN[data->kind][i])
1358 data->fan_min[i] = i2c_smbus_read_byte_data(
1359 client,
1360 FSCHMD_REG_FAN_MIN[data->kind][i]);
1361
1362 /* reset fan status if speed is back to > 0 */
1363 if ((data->fan_status[i] & FSCHMD_FAN_ALARM) &&
1364 data->fan_act[i])
1365 i2c_smbus_write_byte_data(client,
1366 FSCHMD_REG_FAN_STATE[data->kind][i],
1367 data->fan_status[i]);
1368 }
1369
1370 for (i = 0; i < FSCHMD_NO_VOLT_SENSORS[data->kind]; i++)
1371 data->volt[i] = i2c_smbus_read_byte_data(client,
1372 FSCHMD_REG_VOLT[data->kind][i]);
1373
1374 data->last_updated = jiffies;
1375 data->valid = 1;
1376 }
1377
1378 mutex_unlock(&data->update_lock);
1379
1380 return data;
1381}
1382
1383module_i2c_driver(fschmd_driver);
1384
1385MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
1386MODULE_DESCRIPTION("FSC Poseidon, Hermes, Scylla, Heracles, Heimdall, Hades "
1387 "and Syleus driver");
1388MODULE_LICENSE("GPL");