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