blob: fa0c2f1fb443286ffbf20d3e2fcaac1aeeb4e1a4 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Guenter Roeck525ad372012-01-14 13:30:47 -08002/*
3 * fschmd.c
Hans de Goede569ff102007-10-11 08:06:29 -04004 *
Hans de Goedec69ab2b2009-03-30 21:46:45 +02005 * Copyright (C) 2007 - 2009 Hans de Goede <hdegoede@redhat.com>
Hans de Goede569ff102007-10-11 08:06:29 -04006 */
7
8/*
9 * Merged Fujitsu Siemens hwmon driver, supporting the Poseidon, Hermes,
Hans de Goedede15f092009-03-30 21:46:45 +020010 * Scylla, Heracles, Heimdall, Hades and Syleus chips
Hans de Goede569ff102007-10-11 08:06:29 -040011 *
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>
Hans de Goede7845cd72007-12-20 16:42:59 +010032#include <linux/dmi.h>
Hans de Goede97950c32009-01-07 16:37:33 +010033#include <linux/fs.h>
34#include <linux/watchdog.h>
35#include <linux/miscdevice.h>
36#include <linux/uaccess.h>
37#include <linux/kref.h>
Hans de Goede569ff102007-10-11 08:06:29 -040038
39/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050040static const unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
Hans de Goede569ff102007-10-11 08:06:29 -040041
42/* Insmod parameters */
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010043static bool nowayout = WATCHDOG_NOWAYOUT;
44module_param(nowayout, bool, 0);
Hans de Goede97950c32009-01-07 16:37:33 +010045MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
46 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Jean Delvaree5e9f442009-12-14 21:17:27 +010047
48enum chips { fscpos, fscher, fscscy, fschrc, fschmd, fschds, fscsyl };
Hans de Goede569ff102007-10-11 08:06:29 -040049
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
Hans de Goede453e3082009-01-07 16:37:33 +010064#define FSCHMD_CONTROL_ALERT_LED 0x01
Hans de Goede569ff102007-10-11 08:06:29 -040065
Hans de Goede97950c32009-01-07 16:37:33 +010066/* watchdog */
Guenter Roeck525ad372012-01-14 13:30:47 -080067static const u8 FSCHMD_REG_WDOG_CONTROL[7] = {
68 0x21, 0x21, 0x21, 0x21, 0x21, 0x28, 0x28 };
69static const u8 FSCHMD_REG_WDOG_STATE[7] = {
70 0x23, 0x23, 0x23, 0x23, 0x23, 0x29, 0x29 };
71static const u8 FSCHMD_REG_WDOG_PRESET[7] = {
72 0x28, 0x28, 0x28, 0x28, 0x28, 0x2a, 0x2a };
Hans de Goede569ff102007-10-11 08:06:29 -040073
Hans de Goede97950c32009-01-07 16:37:33 +010074#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
Hans de Goede569ff102007-10-11 08:06:29 -040081/* voltages, weird order is to keep the same order as the old drivers */
Hans de Goedede15f092009-03-30 21:46:45 +020082static const u8 FSCHMD_REG_VOLT[7][6] = {
Hans de Goedec69ab2b2009-03-30 21:46:45 +020083 { 0x45, 0x42, 0x48 }, /* pos */
84 { 0x45, 0x42, 0x48 }, /* her */
85 { 0x45, 0x42, 0x48 }, /* scy */
86 { 0x45, 0x42, 0x48 }, /* hrc */
87 { 0x45, 0x42, 0x48 }, /* hmd */
Hans de Goedede15f092009-03-30 21:46:45 +020088 { 0x21, 0x20, 0x22 }, /* hds */
Hans de Goedec69ab2b2009-03-30 21:46:45 +020089 { 0x21, 0x20, 0x22, 0x23, 0x24, 0x25 }, /* syl */
90};
91
Hans de Goedede15f092009-03-30 21:46:45 +020092static const int FSCHMD_NO_VOLT_SENSORS[7] = { 3, 3, 3, 3, 3, 3, 6 };
Hans de Goede569ff102007-10-11 08:06:29 -040093
Guenter Roeck525ad372012-01-14 13:30:47 -080094/*
Wolfram Sang2b2acdc2018-05-06 13:23:48 +020095 * minimum pwm at which the fan is driven (pwm can be increased depending on
Guenter Roeck525ad372012-01-14 13:30:47 -080096 * 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 */
Hans de Goedede15f092009-03-30 21:46:45 +0200100static const u8 FSCHMD_REG_FAN_MIN[7][7] = {
Hans de Goede569ff102007-10-11 08:06:29 -0400101 { 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 */
Hans de Goedede15f092009-03-30 21:46:45 +0200106 { 0x55, 0x65, 0xa5, 0xb5, 0xc5 }, /* hds */
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200107 { 0x54, 0x64, 0x74, 0x84, 0x94, 0xa4, 0xb4 }, /* syl */
Hans de Goede569ff102007-10-11 08:06:29 -0400108};
109
110/* actual fan speed */
Hans de Goedede15f092009-03-30 21:46:45 +0200111static const u8 FSCHMD_REG_FAN_ACT[7][7] = {
Hans de Goede569ff102007-10-11 08:06:29 -0400112 { 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 */
Hans de Goedede15f092009-03-30 21:46:45 +0200117 { 0x5b, 0x6b, 0xab, 0xbb, 0xcb }, /* hds */
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200118 { 0x57, 0x67, 0x77, 0x87, 0x97, 0xa7, 0xb7 }, /* syl */
Hans de Goede569ff102007-10-11 08:06:29 -0400119};
120
121/* fan status registers */
Hans de Goedede15f092009-03-30 21:46:45 +0200122static const u8 FSCHMD_REG_FAN_STATE[7][7] = {
Hans de Goede569ff102007-10-11 08:06:29 -0400123 { 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 */
Hans de Goedede15f092009-03-30 21:46:45 +0200128 { 0x52, 0x62, 0xa2, 0xb2, 0xc2 }, /* hds */
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200129 { 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0 }, /* syl */
Hans de Goede569ff102007-10-11 08:06:29 -0400130};
131
132/* fan ripple / divider registers */
Hans de Goedede15f092009-03-30 21:46:45 +0200133static const u8 FSCHMD_REG_FAN_RIPPLE[7][7] = {
Hans de Goede569ff102007-10-11 08:06:29 -0400134 { 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 */
Hans de Goedede15f092009-03-30 21:46:45 +0200139 { 0x5f, 0x6f, 0xaf, 0xbf, 0xcf }, /* hds */
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200140 { 0x56, 0x66, 0x76, 0x86, 0x96, 0xa6, 0xb6 }, /* syl */
Hans de Goede569ff102007-10-11 08:06:29 -0400141};
142
Hans de Goedede15f092009-03-30 21:46:45 +0200143static const int FSCHMD_NO_FAN_SENSORS[7] = { 3, 3, 6, 4, 5, 5, 7 };
Hans de Goede569ff102007-10-11 08:06:29 -0400144
145/* Fan status register bitmasks */
Hans de Goede453e3082009-01-07 16:37:33 +0100146#define FSCHMD_FAN_ALARM 0x04 /* called fault by FSC! */
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200147#define FSCHMD_FAN_NOT_PRESENT 0x08
148#define FSCHMD_FAN_DISABLED 0x80
Hans de Goede569ff102007-10-11 08:06:29 -0400149
150
151/* actual temperature registers */
Hans de Goedede15f092009-03-30 21:46:45 +0200152static const u8 FSCHMD_REG_TEMP_ACT[7][11] = {
Hans de Goede569ff102007-10-11 08:06:29 -0400153 { 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 */
Hans de Goedede15f092009-03-30 21:46:45 +0200158 { 0x70, 0x80, 0x90, 0xd0, 0xe0 }, /* hds */
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200159 { 0x58, 0x68, 0x78, 0x88, 0x98, 0xa8, /* syl */
160 0xb8, 0xc8, 0xd8, 0xe8, 0xf8 },
Hans de Goede569ff102007-10-11 08:06:29 -0400161};
162
163/* temperature state registers */
Hans de Goedede15f092009-03-30 21:46:45 +0200164static const u8 FSCHMD_REG_TEMP_STATE[7][11] = {
Hans de Goede569ff102007-10-11 08:06:29 -0400165 { 0x71, 0x81, 0x91 }, /* pos */
166 { 0x71, 0x81, 0x91 }, /* her */
167 { 0x71, 0xd1, 0x81, 0x91 }, /* scy */
168 { 0x71, 0x81, 0x91 }, /* hrc */
Jean Delvare7dcf9a32007-11-24 17:45:09 -0500169 { 0x71, 0x81, 0x91, 0xd1, 0xe1 }, /* hmd */
Hans de Goedede15f092009-03-30 21:46:45 +0200170 { 0x71, 0x81, 0x91, 0xd1, 0xe1 }, /* hds */
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200171 { 0x59, 0x69, 0x79, 0x89, 0x99, 0xa9, /* syl */
172 0xb9, 0xc9, 0xd9, 0xe9, 0xf9 },
Hans de Goede569ff102007-10-11 08:06:29 -0400173};
174
Guenter Roeck525ad372012-01-14 13:30:47 -0800175/*
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 */
Hans de Goedede15f092009-03-30 21:46:45 +0200182static const u8 FSCHMD_REG_TEMP_LIMIT[7][11] = {
Hans de Goede569ff102007-10-11 08:06:29 -0400183 { 0, 0, 0 }, /* pos */
184 { 0x76, 0x86, 0x96 }, /* her */
185 { 0x76, 0xd6, 0x86, 0x96 }, /* scy */
186 { 0x76, 0x86, 0x96 }, /* hrc */
Jean Delvare7dcf9a32007-11-24 17:45:09 -0500187 { 0x76, 0x86, 0x96, 0xd6, 0xe6 }, /* hmd */
Hans de Goedede15f092009-03-30 21:46:45 +0200188 { 0x76, 0x86, 0x96, 0xd6, 0xe6 }, /* hds */
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200189 { 0x5a, 0x6a, 0x7a, 0x8a, 0x9a, 0xaa, /* syl */
190 0xba, 0xca, 0xda, 0xea, 0xfa },
Hans de Goede569ff102007-10-11 08:06:29 -0400191};
192
Guenter Roeck525ad372012-01-14 13:30:47 -0800193/*
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 */
Hans de Goede569ff102007-10-11 08:06:29 -0400202
Hans de Goedede15f092009-03-30 21:46:45 +0200203static const int FSCHMD_NO_TEMP_SENSORS[7] = { 3, 3, 4, 3, 5, 5, 11 };
Hans de Goede569ff102007-10-11 08:06:29 -0400204
205/* temp status register bitmasks */
Hans de Goede453e3082009-01-07 16:37:33 +0100206#define FSCHMD_TEMP_WORKING 0x01
207#define FSCHMD_TEMP_ALERT 0x02
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200208#define FSCHMD_TEMP_DISABLED 0x80
Hans de Goede569ff102007-10-11 08:06:29 -0400209/* there only really is an alarm if the sensor is working and alert == 1 */
210#define FSCHMD_TEMP_ALARM_MASK \
Hans de Goede453e3082009-01-07 16:37:33 +0100211 (FSCHMD_TEMP_WORKING | FSCHMD_TEMP_ALERT)
Hans de Goede569ff102007-10-11 08:06:29 -0400212
213/*
214 * Functions declarations
215 */
216
Jean Delvare40ac1992008-07-16 19:30:12 +0200217static int fschmd_probe(struct i2c_client *client,
218 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100219static int fschmd_detect(struct i2c_client *client,
Jean Delvare40ac1992008-07-16 19:30:12 +0200220 struct i2c_board_info *info);
221static int fschmd_remove(struct i2c_client *client);
Hans de Goede569ff102007-10-11 08:06:29 -0400222static struct fschmd_data *fschmd_update_device(struct device *dev);
223
224/*
225 * Driver data (common to all clients)
226 */
227
Jean Delvare40ac1992008-07-16 19:30:12 +0200228static const struct i2c_device_id fschmd_id[] = {
229 { "fscpos", fscpos },
230 { "fscher", fscher },
231 { "fscscy", fscscy },
232 { "fschrc", fschrc },
233 { "fschmd", fschmd },
Hans de Goedede15f092009-03-30 21:46:45 +0200234 { "fschds", fschds },
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200235 { "fscsyl", fscsyl },
Jean Delvare40ac1992008-07-16 19:30:12 +0200236 { }
237};
238MODULE_DEVICE_TABLE(i2c, fschmd_id);
239
Hans de Goede569ff102007-10-11 08:06:29 -0400240static struct i2c_driver fschmd_driver = {
Jean Delvare40ac1992008-07-16 19:30:12 +0200241 .class = I2C_CLASS_HWMON,
Hans de Goede569ff102007-10-11 08:06:29 -0400242 .driver = {
Hans de Goede453e3082009-01-07 16:37:33 +0100243 .name = "fschmd",
Hans de Goede569ff102007-10-11 08:06:29 -0400244 },
Jean Delvare40ac1992008-07-16 19:30:12 +0200245 .probe = fschmd_probe,
246 .remove = fschmd_remove,
247 .id_table = fschmd_id,
248 .detect = fschmd_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100249 .address_list = normal_i2c,
Hans de Goede569ff102007-10-11 08:06:29 -0400250};
251
252/*
253 * Client data (each client gets its own)
254 */
255
256struct fschmd_data {
Hans de Goede97950c32009-01-07 16:37:33 +0100257 struct i2c_client *client;
Hans de Goede569ff102007-10-11 08:06:29 -0400258 struct device *hwmon_dev;
259 struct mutex update_lock;
Hans de Goede97950c32009-01-07 16:37:33 +0100260 struct mutex watchdog_lock;
261 struct list_head list; /* member of the watchdog_data_list */
262 struct kref kref;
263 struct miscdevice watchdog_miscdev;
Jean Delvaredc71afe2010-03-05 22:17:26 +0100264 enum chips kind;
Hans de Goede97950c32009-01-07 16:37:33 +0100265 unsigned long watchdog_is_open;
266 char watchdog_expect_close;
267 char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
Hans de Goede569ff102007-10-11 08:06:29 -0400268 char valid; /* zero until following fields are valid */
269 unsigned long last_updated; /* in jiffies */
270
271 /* register values */
Hans de Goede97950c32009-01-07 16:37:33 +0100272 u8 revision; /* chip revision */
Hans de Goede569ff102007-10-11 08:06:29 -0400273 u8 global_control; /* global control register */
Hans de Goede97950c32009-01-07 16:37:33 +0100274 u8 watchdog_control; /* watchdog control register */
275 u8 watchdog_state; /* watchdog status register */
276 u8 watchdog_preset; /* watchdog counter preset on trigger val */
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200277 u8 volt[6]; /* voltage */
278 u8 temp_act[11]; /* temperature */
279 u8 temp_status[11]; /* status of sensor */
280 u8 temp_max[11]; /* high temp limit, notice: undocumented! */
281 u8 fan_act[7]; /* fans revolutions per second */
282 u8 fan_status[7]; /* fan status */
283 u8 fan_min[7]; /* fan min value for rps */
284 u8 fan_ripple[7]; /* divider for rps */
Hans de Goede569ff102007-10-11 08:06:29 -0400285};
286
Guenter Roeck525ad372012-01-14 13:30:47 -0800287/*
288 * Global variables to hold information read from special DMI tables, which are
289 * available on FSC machines with an fscher or later chip. There is no need to
290 * protect these with a lock as they are only modified from our attach function
291 * which always gets called with the i2c-core lock held and never accessed
292 * before the attach function is done with them.
293 */
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200294static int dmi_mult[6] = { 490, 200, 100, 100, 200, 100 };
295static int dmi_offset[6] = { 0, 0, 0, 0, 0, 0 };
Hans de Goede7845cd72007-12-20 16:42:59 +0100296static int dmi_vref = -1;
297
Guenter Roeck525ad372012-01-14 13:30:47 -0800298/*
299 * Somewhat ugly :( global data pointer list with all fschmd devices, so that
300 * we can find our device data as when using misc_register there is no other
301 * method to get to ones device data from the open fop.
302 */
Hans de Goede97950c32009-01-07 16:37:33 +0100303static LIST_HEAD(watchdog_data_list);
304/* Note this lock not only protect list access, but also data.kref access */
305static DEFINE_MUTEX(watchdog_data_mutex);
306
Guenter Roeck525ad372012-01-14 13:30:47 -0800307/*
308 * Release our data struct when we're detached from the i2c client *and* all
309 * references to our watchdog device are released
310 */
Hans de Goede97950c32009-01-07 16:37:33 +0100311static void fschmd_release_resources(struct kref *ref)
312{
313 struct fschmd_data *data = container_of(ref, struct fschmd_data, kref);
314 kfree(data);
315}
Hans de Goede7845cd72007-12-20 16:42:59 +0100316
Hans de Goede569ff102007-10-11 08:06:29 -0400317/*
318 * Sysfs attr show / store functions
319 */
320
Guenter Roeck22ed7882018-12-10 14:02:06 -0800321static ssize_t in_value_show(struct device *dev,
322 struct device_attribute *devattr, char *buf)
Hans de Goede569ff102007-10-11 08:06:29 -0400323{
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
Jean Delvaredc71afe2010-03-05 22:17:26 +0100328 if (data->kind == fscher || data->kind >= fschrc)
Hans de Goede7845cd72007-12-20 16:42:59 +0100329 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);
Hans de Goede569ff102007-10-11 08:06:29 -0400334}
335
336
337#define TEMP_FROM_REG(val) (((val) - 128) * 1000)
338
Guenter Roeck22ed7882018-12-10 14:02:06 -0800339static ssize_t temp_value_show(struct device *dev,
340 struct device_attribute *devattr, char *buf)
Hans de Goede569ff102007-10-11 08:06:29 -0400341{
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
Guenter Roeck22ed7882018-12-10 14:02:06 -0800348static ssize_t temp_max_show(struct device *dev,
349 struct device_attribute *devattr, char *buf)
Hans de Goede569ff102007-10-11 08:06:29 -0400350{
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
Guenter Roeck22ed7882018-12-10 14:02:06 -0800357static ssize_t temp_max_store(struct device *dev,
358 struct device_attribute *devattr,
359 const char *buf, size_t count)
Hans de Goede569ff102007-10-11 08:06:29 -0400360{
361 int index = to_sensor_dev_attr(devattr)->index;
362 struct fschmd_data *data = dev_get_drvdata(dev);
Guenter Roeck525ad372012-01-14 13:30:47 -0800363 long v;
364 int err;
Hans de Goede569ff102007-10-11 08:06:29 -0400365
Guenter Roeck525ad372012-01-14 13:30:47 -0800366 err = kstrtol(buf, 10, &v);
367 if (err)
368 return err;
369
Guenter Roeck2a844c12013-01-09 08:09:34 -0800370 v = clamp_val(v / 1000, -128, 127) + 128;
Hans de Goede569ff102007-10-11 08:06:29 -0400371
372 mutex_lock(&data->update_lock);
Jean Delvare40ac1992008-07-16 19:30:12 +0200373 i2c_smbus_write_byte_data(to_i2c_client(dev),
Hans de Goede569ff102007-10-11 08:06:29 -0400374 FSCHMD_REG_TEMP_LIMIT[data->kind][index], v);
375 data->temp_max[index] = v;
376 mutex_unlock(&data->update_lock);
377
378 return count;
379}
380
Guenter Roeck22ed7882018-12-10 14:02:06 -0800381static ssize_t temp_fault_show(struct device *dev,
382 struct device_attribute *devattr, char *buf)
Hans de Goede569ff102007-10-11 08:06:29 -0400383{
384 int index = to_sensor_dev_attr(devattr)->index;
385 struct fschmd_data *data = fschmd_update_device(dev);
386
387 /* bit 0 set means sensor working ok, so no fault! */
Hans de Goede453e3082009-01-07 16:37:33 +0100388 if (data->temp_status[index] & FSCHMD_TEMP_WORKING)
Hans de Goede569ff102007-10-11 08:06:29 -0400389 return sprintf(buf, "0\n");
390 else
391 return sprintf(buf, "1\n");
392}
393
Guenter Roeck22ed7882018-12-10 14:02:06 -0800394static ssize_t temp_alarm_show(struct device *dev,
395 struct device_attribute *devattr, char *buf)
Hans de Goede569ff102007-10-11 08:06:29 -0400396{
397 int index = to_sensor_dev_attr(devattr)->index;
398 struct fschmd_data *data = fschmd_update_device(dev);
399
400 if ((data->temp_status[index] & FSCHMD_TEMP_ALARM_MASK) ==
401 FSCHMD_TEMP_ALARM_MASK)
402 return sprintf(buf, "1\n");
403 else
404 return sprintf(buf, "0\n");
405}
406
407
408#define RPM_FROM_REG(val) ((val) * 60)
409
Guenter Roeck22ed7882018-12-10 14:02:06 -0800410static ssize_t fan_value_show(struct device *dev,
411 struct device_attribute *devattr, char *buf)
Hans de Goede569ff102007-10-11 08:06:29 -0400412{
413 int index = to_sensor_dev_attr(devattr)->index;
414 struct fschmd_data *data = fschmd_update_device(dev);
415
416 return sprintf(buf, "%u\n", RPM_FROM_REG(data->fan_act[index]));
417}
418
Guenter Roeck22ed7882018-12-10 14:02:06 -0800419static ssize_t fan_div_show(struct device *dev,
420 struct device_attribute *devattr, char *buf)
Hans de Goede569ff102007-10-11 08:06:29 -0400421{
422 int index = to_sensor_dev_attr(devattr)->index;
423 struct fschmd_data *data = fschmd_update_device(dev);
424
425 /* bits 2..7 reserved => mask with 3 */
426 return sprintf(buf, "%d\n", 1 << (data->fan_ripple[index] & 3));
427}
428
Guenter Roeck22ed7882018-12-10 14:02:06 -0800429static ssize_t fan_div_store(struct device *dev,
430 struct device_attribute *devattr,
431 const char *buf, size_t count)
Hans de Goede569ff102007-10-11 08:06:29 -0400432{
433 u8 reg;
434 int index = to_sensor_dev_attr(devattr)->index;
435 struct fschmd_data *data = dev_get_drvdata(dev);
436 /* supported values: 2, 4, 8 */
Guenter Roeck525ad372012-01-14 13:30:47 -0800437 unsigned long v;
438 int err;
439
440 err = kstrtoul(buf, 10, &v);
441 if (err)
442 return err;
Hans de Goede569ff102007-10-11 08:06:29 -0400443
444 switch (v) {
Guenter Roeck525ad372012-01-14 13:30:47 -0800445 case 2:
446 v = 1;
447 break;
448 case 4:
449 v = 2;
450 break;
451 case 8:
452 v = 3;
453 break;
Hans de Goede569ff102007-10-11 08:06:29 -0400454 default:
Guenter Roeckb55f3752013-01-10 10:01:24 -0800455 dev_err(dev,
456 "fan_div value %lu not supported. Choose one of 2, 4 or 8!\n",
457 v);
Hans de Goede569ff102007-10-11 08:06:29 -0400458 return -EINVAL;
459 }
460
461 mutex_lock(&data->update_lock);
462
Jean Delvare40ac1992008-07-16 19:30:12 +0200463 reg = i2c_smbus_read_byte_data(to_i2c_client(dev),
Hans de Goede569ff102007-10-11 08:06:29 -0400464 FSCHMD_REG_FAN_RIPPLE[data->kind][index]);
465
466 /* bits 2..7 reserved => mask with 0x03 */
467 reg &= ~0x03;
468 reg |= v;
469
Jean Delvare40ac1992008-07-16 19:30:12 +0200470 i2c_smbus_write_byte_data(to_i2c_client(dev),
Hans de Goede569ff102007-10-11 08:06:29 -0400471 FSCHMD_REG_FAN_RIPPLE[data->kind][index], reg);
472
473 data->fan_ripple[index] = reg;
474
475 mutex_unlock(&data->update_lock);
476
477 return count;
478}
479
Guenter Roeck22ed7882018-12-10 14:02:06 -0800480static ssize_t fan_alarm_show(struct device *dev,
481 struct device_attribute *devattr, char *buf)
Hans de Goede569ff102007-10-11 08:06:29 -0400482{
483 int index = to_sensor_dev_attr(devattr)->index;
484 struct fschmd_data *data = fschmd_update_device(dev);
485
Hans de Goede453e3082009-01-07 16:37:33 +0100486 if (data->fan_status[index] & FSCHMD_FAN_ALARM)
Hans de Goede569ff102007-10-11 08:06:29 -0400487 return sprintf(buf, "1\n");
488 else
489 return sprintf(buf, "0\n");
490}
491
Guenter Roeck22ed7882018-12-10 14:02:06 -0800492static ssize_t fan_fault_show(struct device *dev,
493 struct device_attribute *devattr, char *buf)
Hans de Goede569ff102007-10-11 08:06:29 -0400494{
495 int index = to_sensor_dev_attr(devattr)->index;
496 struct fschmd_data *data = fschmd_update_device(dev);
497
Hans de Goede453e3082009-01-07 16:37:33 +0100498 if (data->fan_status[index] & FSCHMD_FAN_NOT_PRESENT)
Hans de Goede569ff102007-10-11 08:06:29 -0400499 return sprintf(buf, "1\n");
500 else
501 return sprintf(buf, "0\n");
502}
503
504
Guenter Roeck22ed7882018-12-10 14:02:06 -0800505static ssize_t pwm_auto_point1_pwm_show(struct device *dev,
506 struct device_attribute *devattr,
507 char *buf)
Hans de Goede569ff102007-10-11 08:06:29 -0400508{
509 int index = to_sensor_dev_attr(devattr)->index;
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200510 struct fschmd_data *data = fschmd_update_device(dev);
511 int val = data->fan_min[index];
Hans de Goede569ff102007-10-11 08:06:29 -0400512
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200513 /* 0 = allow turning off (except on the syl), 1-255 = 50-100% */
Jean Delvaredc71afe2010-03-05 22:17:26 +0100514 if (val || data->kind == fscsyl)
Hans de Goede569ff102007-10-11 08:06:29 -0400515 val = val / 2 + 128;
516
517 return sprintf(buf, "%d\n", val);
518}
519
Guenter Roeck22ed7882018-12-10 14:02:06 -0800520static ssize_t pwm_auto_point1_pwm_store(struct device *dev,
521 struct device_attribute *devattr,
522 const char *buf, size_t count)
Hans de Goede569ff102007-10-11 08:06:29 -0400523{
524 int index = to_sensor_dev_attr(devattr)->index;
525 struct fschmd_data *data = dev_get_drvdata(dev);
Guenter Roeck525ad372012-01-14 13:30:47 -0800526 unsigned long v;
527 int err;
528
529 err = kstrtoul(buf, 10, &v);
530 if (err)
531 return err;
Hans de Goede569ff102007-10-11 08:06:29 -0400532
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200533 /* reg: 0 = allow turning off (except on the syl), 1-255 = 50-100% */
Jean Delvaredc71afe2010-03-05 22:17:26 +0100534 if (v || data->kind == fscsyl) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800535 v = clamp_val(v, 128, 255);
Hans de Goede569ff102007-10-11 08:06:29 -0400536 v = (v - 128) * 2 + 1;
537 }
538
539 mutex_lock(&data->update_lock);
540
Jean Delvare40ac1992008-07-16 19:30:12 +0200541 i2c_smbus_write_byte_data(to_i2c_client(dev),
Hans de Goede569ff102007-10-11 08:06:29 -0400542 FSCHMD_REG_FAN_MIN[data->kind][index], v);
543 data->fan_min[index] = v;
544
545 mutex_unlock(&data->update_lock);
546
547 return count;
548}
549
550
Guenter Roeck525ad372012-01-14 13:30:47 -0800551/*
552 * The FSC hwmon family has the ability to force an attached alert led to flash
553 * from software, we export this as an alert_led sysfs attr
554 */
Julia Lawalle34e8852016-12-22 13:04:40 +0100555static ssize_t alert_led_show(struct device *dev,
Hans de Goede569ff102007-10-11 08:06:29 -0400556 struct device_attribute *devattr, char *buf)
557{
558 struct fschmd_data *data = fschmd_update_device(dev);
559
Hans de Goede453e3082009-01-07 16:37:33 +0100560 if (data->global_control & FSCHMD_CONTROL_ALERT_LED)
Hans de Goede569ff102007-10-11 08:06:29 -0400561 return sprintf(buf, "1\n");
562 else
563 return sprintf(buf, "0\n");
564}
565
Julia Lawalle34e8852016-12-22 13:04:40 +0100566static ssize_t alert_led_store(struct device *dev,
Hans de Goede569ff102007-10-11 08:06:29 -0400567 struct device_attribute *devattr, const char *buf, size_t count)
568{
569 u8 reg;
570 struct fschmd_data *data = dev_get_drvdata(dev);
Guenter Roeck525ad372012-01-14 13:30:47 -0800571 unsigned long v;
572 int err;
573
574 err = kstrtoul(buf, 10, &v);
575 if (err)
576 return err;
Hans de Goede569ff102007-10-11 08:06:29 -0400577
578 mutex_lock(&data->update_lock);
579
Jean Delvare40ac1992008-07-16 19:30:12 +0200580 reg = i2c_smbus_read_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL);
Hans de Goede569ff102007-10-11 08:06:29 -0400581
582 if (v)
Hans de Goede453e3082009-01-07 16:37:33 +0100583 reg |= FSCHMD_CONTROL_ALERT_LED;
Hans de Goede569ff102007-10-11 08:06:29 -0400584 else
Hans de Goede453e3082009-01-07 16:37:33 +0100585 reg &= ~FSCHMD_CONTROL_ALERT_LED;
Hans de Goede569ff102007-10-11 08:06:29 -0400586
Jean Delvare40ac1992008-07-16 19:30:12 +0200587 i2c_smbus_write_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL, reg);
Hans de Goede569ff102007-10-11 08:06:29 -0400588
589 data->global_control = reg;
590
591 mutex_unlock(&data->update_lock);
592
593 return count;
594}
595
Julia Lawalle34e8852016-12-22 13:04:40 +0100596static DEVICE_ATTR_RW(alert_led);
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200597
Hans de Goede569ff102007-10-11 08:06:29 -0400598static struct sensor_device_attribute fschmd_attr[] = {
Guenter Roeck22ed7882018-12-10 14:02:06 -0800599 SENSOR_ATTR_RO(in0_input, in_value, 0),
600 SENSOR_ATTR_RO(in1_input, in_value, 1),
601 SENSOR_ATTR_RO(in2_input, in_value, 2),
602 SENSOR_ATTR_RO(in3_input, in_value, 3),
603 SENSOR_ATTR_RO(in4_input, in_value, 4),
604 SENSOR_ATTR_RO(in5_input, in_value, 5),
Hans de Goede569ff102007-10-11 08:06:29 -0400605};
606
607static struct sensor_device_attribute fschmd_temp_attr[] = {
Guenter Roeck22ed7882018-12-10 14:02:06 -0800608 SENSOR_ATTR_RO(temp1_input, temp_value, 0),
609 SENSOR_ATTR_RW(temp1_max, temp_max, 0),
610 SENSOR_ATTR_RO(temp1_fault, temp_fault, 0),
611 SENSOR_ATTR_RO(temp1_alarm, temp_alarm, 0),
612 SENSOR_ATTR_RO(temp2_input, temp_value, 1),
613 SENSOR_ATTR_RW(temp2_max, temp_max, 1),
614 SENSOR_ATTR_RO(temp2_fault, temp_fault, 1),
615 SENSOR_ATTR_RO(temp2_alarm, temp_alarm, 1),
616 SENSOR_ATTR_RO(temp3_input, temp_value, 2),
617 SENSOR_ATTR_RW(temp3_max, temp_max, 2),
618 SENSOR_ATTR_RO(temp3_fault, temp_fault, 2),
619 SENSOR_ATTR_RO(temp3_alarm, temp_alarm, 2),
620 SENSOR_ATTR_RO(temp4_input, temp_value, 3),
621 SENSOR_ATTR_RW(temp4_max, temp_max, 3),
622 SENSOR_ATTR_RO(temp4_fault, temp_fault, 3),
623 SENSOR_ATTR_RO(temp4_alarm, temp_alarm, 3),
624 SENSOR_ATTR_RO(temp5_input, temp_value, 4),
625 SENSOR_ATTR_RW(temp5_max, temp_max, 4),
626 SENSOR_ATTR_RO(temp5_fault, temp_fault, 4),
627 SENSOR_ATTR_RO(temp5_alarm, temp_alarm, 4),
628 SENSOR_ATTR_RO(temp6_input, temp_value, 5),
629 SENSOR_ATTR_RW(temp6_max, temp_max, 5),
630 SENSOR_ATTR_RO(temp6_fault, temp_fault, 5),
631 SENSOR_ATTR_RO(temp6_alarm, temp_alarm, 5),
632 SENSOR_ATTR_RO(temp7_input, temp_value, 6),
633 SENSOR_ATTR_RW(temp7_max, temp_max, 6),
634 SENSOR_ATTR_RO(temp7_fault, temp_fault, 6),
635 SENSOR_ATTR_RO(temp7_alarm, temp_alarm, 6),
636 SENSOR_ATTR_RO(temp8_input, temp_value, 7),
637 SENSOR_ATTR_RW(temp8_max, temp_max, 7),
638 SENSOR_ATTR_RO(temp8_fault, temp_fault, 7),
639 SENSOR_ATTR_RO(temp8_alarm, temp_alarm, 7),
640 SENSOR_ATTR_RO(temp9_input, temp_value, 8),
641 SENSOR_ATTR_RW(temp9_max, temp_max, 8),
642 SENSOR_ATTR_RO(temp9_fault, temp_fault, 8),
643 SENSOR_ATTR_RO(temp9_alarm, temp_alarm, 8),
644 SENSOR_ATTR_RO(temp10_input, temp_value, 9),
645 SENSOR_ATTR_RW(temp10_max, temp_max, 9),
646 SENSOR_ATTR_RO(temp10_fault, temp_fault, 9),
647 SENSOR_ATTR_RO(temp10_alarm, temp_alarm, 9),
648 SENSOR_ATTR_RO(temp11_input, temp_value, 10),
649 SENSOR_ATTR_RW(temp11_max, temp_max, 10),
650 SENSOR_ATTR_RO(temp11_fault, temp_fault, 10),
651 SENSOR_ATTR_RO(temp11_alarm, temp_alarm, 10),
Hans de Goede569ff102007-10-11 08:06:29 -0400652};
653
654static struct sensor_device_attribute fschmd_fan_attr[] = {
Guenter Roeck22ed7882018-12-10 14:02:06 -0800655 SENSOR_ATTR_RO(fan1_input, fan_value, 0),
656 SENSOR_ATTR_RW(fan1_div, fan_div, 0),
657 SENSOR_ATTR_RO(fan1_alarm, fan_alarm, 0),
658 SENSOR_ATTR_RO(fan1_fault, fan_fault, 0),
659 SENSOR_ATTR_RW(pwm1_auto_point1_pwm, pwm_auto_point1_pwm, 0),
660 SENSOR_ATTR_RO(fan2_input, fan_value, 1),
661 SENSOR_ATTR_RW(fan2_div, fan_div, 1),
662 SENSOR_ATTR_RO(fan2_alarm, fan_alarm, 1),
663 SENSOR_ATTR_RO(fan2_fault, fan_fault, 1),
664 SENSOR_ATTR_RW(pwm2_auto_point1_pwm, pwm_auto_point1_pwm, 1),
665 SENSOR_ATTR_RO(fan3_input, fan_value, 2),
666 SENSOR_ATTR_RW(fan3_div, fan_div, 2),
667 SENSOR_ATTR_RO(fan3_alarm, fan_alarm, 2),
668 SENSOR_ATTR_RO(fan3_fault, fan_fault, 2),
669 SENSOR_ATTR_RW(pwm3_auto_point1_pwm, pwm_auto_point1_pwm, 2),
670 SENSOR_ATTR_RO(fan4_input, fan_value, 3),
671 SENSOR_ATTR_RW(fan4_div, fan_div, 3),
672 SENSOR_ATTR_RO(fan4_alarm, fan_alarm, 3),
673 SENSOR_ATTR_RO(fan4_fault, fan_fault, 3),
674 SENSOR_ATTR_RW(pwm4_auto_point1_pwm, pwm_auto_point1_pwm, 3),
675 SENSOR_ATTR_RO(fan5_input, fan_value, 4),
676 SENSOR_ATTR_RW(fan5_div, fan_div, 4),
677 SENSOR_ATTR_RO(fan5_alarm, fan_alarm, 4),
678 SENSOR_ATTR_RO(fan5_fault, fan_fault, 4),
679 SENSOR_ATTR_RW(pwm5_auto_point1_pwm, pwm_auto_point1_pwm, 4),
680 SENSOR_ATTR_RO(fan6_input, fan_value, 5),
681 SENSOR_ATTR_RW(fan6_div, fan_div, 5),
682 SENSOR_ATTR_RO(fan6_alarm, fan_alarm, 5),
683 SENSOR_ATTR_RO(fan6_fault, fan_fault, 5),
684 SENSOR_ATTR_RW(pwm6_auto_point1_pwm, pwm_auto_point1_pwm, 5),
685 SENSOR_ATTR_RO(fan7_input, fan_value, 6),
686 SENSOR_ATTR_RW(fan7_div, fan_div, 6),
687 SENSOR_ATTR_RO(fan7_alarm, fan_alarm, 6),
688 SENSOR_ATTR_RO(fan7_fault, fan_fault, 6),
689 SENSOR_ATTR_RW(pwm7_auto_point1_pwm, pwm_auto_point1_pwm, 6),
Hans de Goede569ff102007-10-11 08:06:29 -0400690};
691
692
693/*
Hans de Goede97950c32009-01-07 16:37:33 +0100694 * Watchdog routines
695 */
696
697static int watchdog_set_timeout(struct fschmd_data *data, int timeout)
698{
699 int ret, resolution;
700 int kind = data->kind + 1; /* 0-x array index -> 1-x module param */
701
702 /* 2 second or 60 second resolution? */
703 if (timeout <= 510 || kind == fscpos || kind == fscscy)
704 resolution = 2;
705 else
706 resolution = 60;
707
708 if (timeout < resolution || timeout > (resolution * 255))
709 return -EINVAL;
710
711 mutex_lock(&data->watchdog_lock);
712 if (!data->client) {
713 ret = -ENODEV;
714 goto leave;
715 }
716
717 if (resolution == 2)
718 data->watchdog_control &= ~FSCHMD_WDOG_CONTROL_RESOLUTION;
719 else
720 data->watchdog_control |= FSCHMD_WDOG_CONTROL_RESOLUTION;
721
722 data->watchdog_preset = DIV_ROUND_UP(timeout, resolution);
723
724 /* Write new timeout value */
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200725 i2c_smbus_write_byte_data(data->client,
726 FSCHMD_REG_WDOG_PRESET[data->kind], data->watchdog_preset);
Hans de Goede97950c32009-01-07 16:37:33 +0100727 /* Write new control register, do not trigger! */
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200728 i2c_smbus_write_byte_data(data->client,
729 FSCHMD_REG_WDOG_CONTROL[data->kind],
Hans de Goede97950c32009-01-07 16:37:33 +0100730 data->watchdog_control & ~FSCHMD_WDOG_CONTROL_TRIGGER);
731
732 ret = data->watchdog_preset * resolution;
733
734leave:
735 mutex_unlock(&data->watchdog_lock);
736 return ret;
737}
738
739static int watchdog_get_timeout(struct fschmd_data *data)
740{
741 int timeout;
742
743 mutex_lock(&data->watchdog_lock);
744 if (data->watchdog_control & FSCHMD_WDOG_CONTROL_RESOLUTION)
745 timeout = data->watchdog_preset * 60;
746 else
747 timeout = data->watchdog_preset * 2;
748 mutex_unlock(&data->watchdog_lock);
749
750 return timeout;
751}
752
753static int watchdog_trigger(struct fschmd_data *data)
754{
755 int ret = 0;
756
757 mutex_lock(&data->watchdog_lock);
758 if (!data->client) {
759 ret = -ENODEV;
760 goto leave;
761 }
762
763 data->watchdog_control |= FSCHMD_WDOG_CONTROL_TRIGGER;
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200764 i2c_smbus_write_byte_data(data->client,
765 FSCHMD_REG_WDOG_CONTROL[data->kind],
766 data->watchdog_control);
Hans de Goede97950c32009-01-07 16:37:33 +0100767leave:
768 mutex_unlock(&data->watchdog_lock);
769 return ret;
770}
771
772static int watchdog_stop(struct fschmd_data *data)
773{
774 int ret = 0;
775
776 mutex_lock(&data->watchdog_lock);
777 if (!data->client) {
778 ret = -ENODEV;
779 goto leave;
780 }
781
782 data->watchdog_control &= ~FSCHMD_WDOG_CONTROL_STARTED;
Guenter Roeck525ad372012-01-14 13:30:47 -0800783 /*
784 * Don't store the stop flag in our watchdog control register copy, as
785 * its a write only bit (read always returns 0)
786 */
Hans de Goedec69ab2b2009-03-30 21:46:45 +0200787 i2c_smbus_write_byte_data(data->client,
788 FSCHMD_REG_WDOG_CONTROL[data->kind],
Hans de Goede97950c32009-01-07 16:37:33 +0100789 data->watchdog_control | FSCHMD_WDOG_CONTROL_STOP);
790leave:
791 mutex_unlock(&data->watchdog_lock);
792 return ret;
793}
794
795static int watchdog_open(struct inode *inode, struct file *filp)
796{
797 struct fschmd_data *pos, *data = NULL;
Hans de Goedec4536152010-01-25 15:00:50 +0100798 int watchdog_is_open;
Hans de Goede97950c32009-01-07 16:37:33 +0100799
Guenter Roeck525ad372012-01-14 13:30:47 -0800800 /*
801 * We get called from drivers/char/misc.c with misc_mtx hold, and we
802 * call misc_register() from fschmd_probe() with watchdog_data_mutex
803 * hold, as misc_register() takes the misc_mtx lock, this is a possible
804 * deadlock, so we use mutex_trylock here.
805 */
Hans de Goede97950c32009-01-07 16:37:33 +0100806 if (!mutex_trylock(&watchdog_data_mutex))
807 return -ERESTARTSYS;
808 list_for_each_entry(pos, &watchdog_data_list, list) {
809 if (pos->watchdog_miscdev.minor == iminor(inode)) {
810 data = pos;
811 break;
812 }
813 }
814 /* Note we can never not have found data, so we don't check for this */
Hans de Goedec4536152010-01-25 15:00:50 +0100815 watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
816 if (!watchdog_is_open)
817 kref_get(&data->kref);
Hans de Goede97950c32009-01-07 16:37:33 +0100818 mutex_unlock(&watchdog_data_mutex);
819
Hans de Goedec4536152010-01-25 15:00:50 +0100820 if (watchdog_is_open)
Hans de Goede97950c32009-01-07 16:37:33 +0100821 return -EBUSY;
822
823 /* Start the watchdog */
824 watchdog_trigger(data);
825 filp->private_data = data;
826
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300827 return stream_open(inode, filp);
Hans de Goede97950c32009-01-07 16:37:33 +0100828}
829
830static int watchdog_release(struct inode *inode, struct file *filp)
831{
832 struct fschmd_data *data = filp->private_data;
833
834 if (data->watchdog_expect_close) {
835 watchdog_stop(data);
836 data->watchdog_expect_close = 0;
837 } else {
838 watchdog_trigger(data);
839 dev_crit(&data->client->dev,
840 "unexpected close, not stopping watchdog!\n");
841 }
842
843 clear_bit(0, &data->watchdog_is_open);
844
845 mutex_lock(&watchdog_data_mutex);
846 kref_put(&data->kref, fschmd_release_resources);
847 mutex_unlock(&watchdog_data_mutex);
848
849 return 0;
850}
851
852static ssize_t watchdog_write(struct file *filp, const char __user *buf,
853 size_t count, loff_t *offset)
854{
Roel Kluinc7702c32009-10-24 13:28:45 +0200855 int ret;
Hans de Goede97950c32009-01-07 16:37:33 +0100856 struct fschmd_data *data = filp->private_data;
857
858 if (count) {
859 if (!nowayout) {
860 size_t i;
861
862 /* Clear it in case it was set with a previous write */
863 data->watchdog_expect_close = 0;
864
865 for (i = 0; i != count; i++) {
866 char c;
867 if (get_user(c, buf + i))
868 return -EFAULT;
869 if (c == 'V')
870 data->watchdog_expect_close = 1;
871 }
872 }
873 ret = watchdog_trigger(data);
874 if (ret < 0)
875 return ret;
876 }
877 return count;
878}
879
Guenter Roeck525ad372012-01-14 13:30:47 -0800880static long watchdog_ioctl(struct file *filp, unsigned int cmd,
881 unsigned long arg)
Hans de Goede97950c32009-01-07 16:37:33 +0100882{
Jean Delvare1b7243e2011-01-12 21:55:11 +0100883 struct watchdog_info ident = {
Hans de Goede97950c32009-01-07 16:37:33 +0100884 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
885 WDIOF_CARDRESET,
886 .identity = "FSC watchdog"
887 };
888 int i, ret = 0;
889 struct fschmd_data *data = filp->private_data;
890
891 switch (cmd) {
892 case WDIOC_GETSUPPORT:
893 ident.firmware_version = data->revision;
894 if (!nowayout)
895 ident.options |= WDIOF_MAGICCLOSE;
896 if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
897 ret = -EFAULT;
898 break;
899
900 case WDIOC_GETSTATUS:
901 ret = put_user(0, (int __user *)arg);
902 break;
903
904 case WDIOC_GETBOOTSTATUS:
905 if (data->watchdog_state & FSCHMD_WDOG_STATE_CARDRESET)
906 ret = put_user(WDIOF_CARDRESET, (int __user *)arg);
907 else
908 ret = put_user(0, (int __user *)arg);
909 break;
910
911 case WDIOC_KEEPALIVE:
912 ret = watchdog_trigger(data);
913 break;
914
915 case WDIOC_GETTIMEOUT:
916 i = watchdog_get_timeout(data);
917 ret = put_user(i, (int __user *)arg);
918 break;
919
920 case WDIOC_SETTIMEOUT:
921 if (get_user(i, (int __user *)arg)) {
922 ret = -EFAULT;
923 break;
924 }
925 ret = watchdog_set_timeout(data, i);
926 if (ret > 0)
927 ret = put_user(ret, (int __user *)arg);
928 break;
929
930 case WDIOC_SETOPTIONS:
931 if (get_user(i, (int __user *)arg)) {
932 ret = -EFAULT;
933 break;
934 }
935
936 if (i & WDIOS_DISABLECARD)
937 ret = watchdog_stop(data);
938 else if (i & WDIOS_ENABLECARD)
939 ret = watchdog_trigger(data);
940 else
941 ret = -EINVAL;
942
943 break;
944 default:
945 ret = -ENOTTY;
946 }
Hans de Goede97950c32009-01-07 16:37:33 +0100947 return ret;
948}
949
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700950static const struct file_operations watchdog_fops = {
Hans de Goede97950c32009-01-07 16:37:33 +0100951 .owner = THIS_MODULE,
952 .llseek = no_llseek,
953 .open = watchdog_open,
954 .release = watchdog_release,
955 .write = watchdog_write,
Arnd Bergmann55929332010-04-27 00:24:05 +0200956 .unlocked_ioctl = watchdog_ioctl,
Hans de Goede97950c32009-01-07 16:37:33 +0100957};
958
959
960/*
961 * Detect, register, unregister and update device functions
Hans de Goede569ff102007-10-11 08:06:29 -0400962 */
963
Guenter Roeck525ad372012-01-14 13:30:47 -0800964/*
965 * DMI decode routine to read voltage scaling factors from special DMI tables,
966 * which are available on FSC machines with an fscher or later chip.
967 */
Jean Delvaree7a19c562009-03-30 21:46:44 +0200968static void fschmd_dmi_decode(const struct dmi_header *header, void *dummy)
Hans de Goede7845cd72007-12-20 16:42:59 +0100969{
970 int i, mult[3] = { 0 }, offset[3] = { 0 }, vref = 0, found = 0;
971
Guenter Roeck525ad372012-01-14 13:30:47 -0800972 /*
973 * dmi code ugliness, we get passed the address of the contents of
974 * a complete DMI record, but in the form of a dmi_header pointer, in
975 * reality this address holds header->length bytes of which the header
976 * are the first 4 bytes
977 */
Hans de Goede7845cd72007-12-20 16:42:59 +0100978 u8 *dmi_data = (u8 *)header;
979
980 /* We are looking for OEM-specific type 185 */
981 if (header->type != 185)
982 return;
983
Guenter Roeck525ad372012-01-14 13:30:47 -0800984 /*
985 * we are looking for what Siemens calls "subtype" 19, the subtype
986 * is stored in byte 5 of the dmi block
987 */
Hans de Goede7845cd72007-12-20 16:42:59 +0100988 if (header->length < 5 || dmi_data[4] != 19)
989 return;
990
Guenter Roeck525ad372012-01-14 13:30:47 -0800991 /*
992 * After the subtype comes 1 unknown byte and then blocks of 5 bytes,
993 * consisting of what Siemens calls an "Entity" number, followed by
994 * 2 16-bit words in LSB first order
995 */
Hans de Goede7845cd72007-12-20 16:42:59 +0100996 for (i = 6; (i + 4) < header->length; i += 5) {
997 /* entity 1 - 3: voltage multiplier and offset */
998 if (dmi_data[i] >= 1 && dmi_data[i] <= 3) {
999 /* Our in sensors order and the DMI order differ */
1000 const int shuffle[3] = { 1, 0, 2 };
1001 int in = shuffle[dmi_data[i] - 1];
1002
1003 /* Check for twice the same entity */
1004 if (found & (1 << in))
1005 return;
1006
1007 mult[in] = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
1008 offset[in] = dmi_data[i + 3] | (dmi_data[i + 4] << 8);
1009
1010 found |= 1 << in;
1011 }
1012
1013 /* entity 7: reference voltage */
1014 if (dmi_data[i] == 7) {
1015 /* Check for twice the same entity */
1016 if (found & 0x08)
1017 return;
1018
1019 vref = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
1020
1021 found |= 0x08;
1022 }
1023 }
1024
1025 if (found == 0x0F) {
1026 for (i = 0; i < 3; i++) {
1027 dmi_mult[i] = mult[i] * 10;
1028 dmi_offset[i] = offset[i] * 10;
1029 }
Guenter Roeck525ad372012-01-14 13:30:47 -08001030 /*
1031 * According to the docs there should be separate dmi entries
1032 * for the mult's and offsets of in3-5 of the syl, but on
1033 * my test machine these are not present
1034 */
Hans de Goedec69ab2b2009-03-30 21:46:45 +02001035 dmi_mult[3] = dmi_mult[2];
1036 dmi_mult[4] = dmi_mult[1];
1037 dmi_mult[5] = dmi_mult[2];
1038 dmi_offset[3] = dmi_offset[2];
1039 dmi_offset[4] = dmi_offset[1];
1040 dmi_offset[5] = dmi_offset[2];
Hans de Goede7845cd72007-12-20 16:42:59 +01001041 dmi_vref = vref;
1042 }
1043}
1044
Jean Delvare310ec792009-12-14 21:17:23 +01001045static int fschmd_detect(struct i2c_client *client,
Jean Delvare40ac1992008-07-16 19:30:12 +02001046 struct i2c_board_info *info)
Hans de Goede569ff102007-10-11 08:06:29 -04001047{
Jean Delvare52df6442009-12-09 20:35:57 +01001048 enum chips kind;
Jean Delvare40ac1992008-07-16 19:30:12 +02001049 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +01001050 char id[4];
Hans de Goede569ff102007-10-11 08:06:29 -04001051
1052 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare40ac1992008-07-16 19:30:12 +02001053 return -ENODEV;
Hans de Goede569ff102007-10-11 08:06:29 -04001054
1055 /* Detect & Identify the chip */
Jean Delvare52df6442009-12-09 20:35:57 +01001056 id[0] = i2c_smbus_read_byte_data(client, FSCHMD_REG_IDENT_0);
1057 id[1] = i2c_smbus_read_byte_data(client, FSCHMD_REG_IDENT_1);
1058 id[2] = i2c_smbus_read_byte_data(client, FSCHMD_REG_IDENT_2);
1059 id[3] = '\0';
Hans de Goede569ff102007-10-11 08:06:29 -04001060
Jean Delvare52df6442009-12-09 20:35:57 +01001061 if (!strcmp(id, "PEG"))
1062 kind = fscpos;
1063 else if (!strcmp(id, "HER"))
1064 kind = fscher;
1065 else if (!strcmp(id, "SCY"))
1066 kind = fscscy;
1067 else if (!strcmp(id, "HRC"))
1068 kind = fschrc;
1069 else if (!strcmp(id, "HMD"))
1070 kind = fschmd;
1071 else if (!strcmp(id, "HDS"))
1072 kind = fschds;
1073 else if (!strcmp(id, "SYL"))
1074 kind = fscsyl;
1075 else
1076 return -ENODEV;
Hans de Goede569ff102007-10-11 08:06:29 -04001077
Jean Delvaredc71afe2010-03-05 22:17:26 +01001078 strlcpy(info->type, fschmd_id[kind].name, I2C_NAME_SIZE);
Jean Delvare40ac1992008-07-16 19:30:12 +02001079
1080 return 0;
1081}
1082
1083static int fschmd_probe(struct i2c_client *client,
1084 const struct i2c_device_id *id)
1085{
1086 struct fschmd_data *data;
Hans de Goedede15f092009-03-30 21:46:45 +02001087 const char * const names[7] = { "Poseidon", "Hermes", "Scylla",
1088 "Heracles", "Heimdall", "Hades", "Syleus" };
Hans de Goede97950c32009-01-07 16:37:33 +01001089 const int watchdog_minors[] = { WATCHDOG_MINOR, 212, 213, 214, 215 };
Jean Delvare40ac1992008-07-16 19:30:12 +02001090 int i, err;
1091 enum chips kind = id->driver_data;
1092
1093 data = kzalloc(sizeof(struct fschmd_data), GFP_KERNEL);
1094 if (!data)
1095 return -ENOMEM;
1096
1097 i2c_set_clientdata(client, data);
1098 mutex_init(&data->update_lock);
Hans de Goede97950c32009-01-07 16:37:33 +01001099 mutex_init(&data->watchdog_lock);
1100 INIT_LIST_HEAD(&data->list);
1101 kref_init(&data->kref);
Guenter Roeck525ad372012-01-14 13:30:47 -08001102 /*
1103 * Store client pointer in our data struct for watchdog usage
1104 * (where the client is found through a data ptr instead of the
1105 * otherway around)
1106 */
Hans de Goede97950c32009-01-07 16:37:33 +01001107 data->client = client;
Jean Delvaredc71afe2010-03-05 22:17:26 +01001108 data->kind = kind;
Jean Delvare40ac1992008-07-16 19:30:12 +02001109
Hans de Goede569ff102007-10-11 08:06:29 -04001110 if (kind == fscpos) {
Guenter Roeck525ad372012-01-14 13:30:47 -08001111 /*
1112 * The Poseidon has hardwired temp limits, fill these
1113 * in for the alarm resetting code
1114 */
Hans de Goede569ff102007-10-11 08:06:29 -04001115 data->temp_max[0] = 70 + 128;
1116 data->temp_max[1] = 50 + 128;
1117 data->temp_max[2] = 50 + 128;
1118 }
1119
Hans de Goede7845cd72007-12-20 16:42:59 +01001120 /* Read the special DMI table for fscher and newer chips */
Hans de Goede453e3082009-01-07 16:37:33 +01001121 if ((kind == fscher || kind >= fschrc) && dmi_vref == -1) {
Jean Delvaree7a19c562009-03-30 21:46:44 +02001122 dmi_walk(fschmd_dmi_decode, NULL);
Hans de Goede7845cd72007-12-20 16:42:59 +01001123 if (dmi_vref == -1) {
Hans de Goede453e3082009-01-07 16:37:33 +01001124 dev_warn(&client->dev,
1125 "Couldn't get voltage scaling factors from "
Hans de Goede7845cd72007-12-20 16:42:59 +01001126 "BIOS DMI table, using builtin defaults\n");
1127 dmi_vref = 33;
1128 }
1129 }
1130
Hans de Goede97950c32009-01-07 16:37:33 +01001131 /* Read in some never changing registers */
1132 data->revision = i2c_smbus_read_byte_data(client, FSCHMD_REG_REVISION);
1133 data->global_control = i2c_smbus_read_byte_data(client,
1134 FSCHMD_REG_CONTROL);
1135 data->watchdog_control = i2c_smbus_read_byte_data(client,
Hans de Goedec69ab2b2009-03-30 21:46:45 +02001136 FSCHMD_REG_WDOG_CONTROL[data->kind]);
Hans de Goede97950c32009-01-07 16:37:33 +01001137 data->watchdog_state = i2c_smbus_read_byte_data(client,
Hans de Goedec69ab2b2009-03-30 21:46:45 +02001138 FSCHMD_REG_WDOG_STATE[data->kind]);
Hans de Goede97950c32009-01-07 16:37:33 +01001139 data->watchdog_preset = i2c_smbus_read_byte_data(client,
Hans de Goedec69ab2b2009-03-30 21:46:45 +02001140 FSCHMD_REG_WDOG_PRESET[data->kind]);
Hans de Goede97950c32009-01-07 16:37:33 +01001141
Hans de Goedec69ab2b2009-03-30 21:46:45 +02001142 err = device_create_file(&client->dev, &dev_attr_alert_led);
1143 if (err)
1144 goto exit_detach;
Hans de Goede569ff102007-10-11 08:06:29 -04001145
Hans de Goedec69ab2b2009-03-30 21:46:45 +02001146 for (i = 0; i < FSCHMD_NO_VOLT_SENSORS[data->kind]; i++) {
Hans de Goede569ff102007-10-11 08:06:29 -04001147 err = device_create_file(&client->dev,
1148 &fschmd_attr[i].dev_attr);
1149 if (err)
1150 goto exit_detach;
1151 }
1152
1153 for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++) {
1154 /* Poseidon doesn't have TEMP_LIMIT registers */
1155 if (kind == fscpos && fschmd_temp_attr[i].dev_attr.show ==
Guenter Roeck22ed7882018-12-10 14:02:06 -08001156 temp_max_show)
Hans de Goede569ff102007-10-11 08:06:29 -04001157 continue;
1158
Hans de Goedec69ab2b2009-03-30 21:46:45 +02001159 if (kind == fscsyl) {
1160 if (i % 4 == 0)
1161 data->temp_status[i / 4] =
1162 i2c_smbus_read_byte_data(client,
1163 FSCHMD_REG_TEMP_STATE
1164 [data->kind][i / 4]);
1165 if (data->temp_status[i / 4] & FSCHMD_TEMP_DISABLED)
1166 continue;
1167 }
1168
Hans de Goede569ff102007-10-11 08:06:29 -04001169 err = device_create_file(&client->dev,
1170 &fschmd_temp_attr[i].dev_attr);
1171 if (err)
1172 goto exit_detach;
1173 }
1174
1175 for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++) {
1176 /* Poseidon doesn't have a FAN_MIN register for its 3rd fan */
1177 if (kind == fscpos &&
1178 !strcmp(fschmd_fan_attr[i].dev_attr.attr.name,
1179 "pwm3_auto_point1_pwm"))
1180 continue;
1181
Hans de Goedec69ab2b2009-03-30 21:46:45 +02001182 if (kind == fscsyl) {
1183 if (i % 5 == 0)
1184 data->fan_status[i / 5] =
1185 i2c_smbus_read_byte_data(client,
1186 FSCHMD_REG_FAN_STATE
1187 [data->kind][i / 5]);
1188 if (data->fan_status[i / 5] & FSCHMD_FAN_DISABLED)
1189 continue;
1190 }
1191
Hans de Goede569ff102007-10-11 08:06:29 -04001192 err = device_create_file(&client->dev,
1193 &fschmd_fan_attr[i].dev_attr);
1194 if (err)
1195 goto exit_detach;
1196 }
1197
1198 data->hwmon_dev = hwmon_device_register(&client->dev);
1199 if (IS_ERR(data->hwmon_dev)) {
1200 err = PTR_ERR(data->hwmon_dev);
1201 data->hwmon_dev = NULL;
1202 goto exit_detach;
1203 }
1204
Guenter Roeck525ad372012-01-14 13:30:47 -08001205 /*
1206 * We take the data_mutex lock early so that watchdog_open() cannot
1207 * run when misc_register() has completed, but we've not yet added
1208 * our data to the watchdog_data_list (and set the default timeout)
1209 */
Hans de Goede97950c32009-01-07 16:37:33 +01001210 mutex_lock(&watchdog_data_mutex);
1211 for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
1212 /* Register our watchdog part */
1213 snprintf(data->watchdog_name, sizeof(data->watchdog_name),
1214 "watchdog%c", (i == 0) ? '\0' : ('0' + i));
1215 data->watchdog_miscdev.name = data->watchdog_name;
1216 data->watchdog_miscdev.fops = &watchdog_fops;
1217 data->watchdog_miscdev.minor = watchdog_minors[i];
1218 err = misc_register(&data->watchdog_miscdev);
1219 if (err == -EBUSY)
1220 continue;
1221 if (err) {
1222 data->watchdog_miscdev.minor = 0;
1223 dev_err(&client->dev,
1224 "Registering watchdog chardev: %d\n", err);
1225 break;
1226 }
1227
1228 list_add(&data->list, &watchdog_data_list);
1229 watchdog_set_timeout(data, 60);
1230 dev_info(&client->dev,
1231 "Registered watchdog chardev major 10, minor: %d\n",
1232 watchdog_minors[i]);
1233 break;
1234 }
1235 if (i == ARRAY_SIZE(watchdog_minors)) {
1236 data->watchdog_miscdev.minor = 0;
Guenter Roeckb55f3752013-01-10 10:01:24 -08001237 dev_warn(&client->dev,
1238 "Couldn't register watchdog chardev (due to no free minor)\n");
Hans de Goede97950c32009-01-07 16:37:33 +01001239 }
1240 mutex_unlock(&watchdog_data_mutex);
1241
Hans de Goede453e3082009-01-07 16:37:33 +01001242 dev_info(&client->dev, "Detected FSC %s chip, revision: %d\n",
Hans de Goede97950c32009-01-07 16:37:33 +01001243 names[data->kind], (int) data->revision);
Hans de Goede569ff102007-10-11 08:06:29 -04001244
1245 return 0;
1246
1247exit_detach:
Jean Delvare40ac1992008-07-16 19:30:12 +02001248 fschmd_remove(client); /* will also free data for us */
Hans de Goede569ff102007-10-11 08:06:29 -04001249 return err;
1250}
1251
Jean Delvare40ac1992008-07-16 19:30:12 +02001252static int fschmd_remove(struct i2c_client *client)
Hans de Goede569ff102007-10-11 08:06:29 -04001253{
1254 struct fschmd_data *data = i2c_get_clientdata(client);
Jean Delvare40ac1992008-07-16 19:30:12 +02001255 int i;
Hans de Goede569ff102007-10-11 08:06:29 -04001256
Hans de Goede97950c32009-01-07 16:37:33 +01001257 /* Unregister the watchdog (if registered) */
1258 if (data->watchdog_miscdev.minor) {
1259 misc_deregister(&data->watchdog_miscdev);
1260 if (data->watchdog_is_open) {
1261 dev_warn(&client->dev,
1262 "i2c client detached with watchdog open! "
1263 "Stopping watchdog.\n");
1264 watchdog_stop(data);
1265 }
1266 mutex_lock(&watchdog_data_mutex);
1267 list_del(&data->list);
1268 mutex_unlock(&watchdog_data_mutex);
1269 /* Tell the watchdog code the client is gone */
1270 mutex_lock(&data->watchdog_lock);
1271 data->client = NULL;
1272 mutex_unlock(&data->watchdog_lock);
1273 }
1274
Guenter Roeck525ad372012-01-14 13:30:47 -08001275 /*
1276 * Check if registered in case we're called from fschmd_detect
1277 * to cleanup after an error
1278 */
Hans de Goede569ff102007-10-11 08:06:29 -04001279 if (data->hwmon_dev)
1280 hwmon_device_unregister(data->hwmon_dev);
1281
Hans de Goedec69ab2b2009-03-30 21:46:45 +02001282 device_remove_file(&client->dev, &dev_attr_alert_led);
1283 for (i = 0; i < (FSCHMD_NO_VOLT_SENSORS[data->kind]); i++)
Hans de Goede569ff102007-10-11 08:06:29 -04001284 device_remove_file(&client->dev, &fschmd_attr[i].dev_attr);
1285 for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++)
1286 device_remove_file(&client->dev,
1287 &fschmd_temp_attr[i].dev_attr);
1288 for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++)
1289 device_remove_file(&client->dev,
1290 &fschmd_fan_attr[i].dev_attr);
1291
Hans de Goede97950c32009-01-07 16:37:33 +01001292 mutex_lock(&watchdog_data_mutex);
1293 kref_put(&data->kref, fschmd_release_resources);
1294 mutex_unlock(&watchdog_data_mutex);
1295
Hans de Goede569ff102007-10-11 08:06:29 -04001296 return 0;
1297}
1298
1299static struct fschmd_data *fschmd_update_device(struct device *dev)
1300{
1301 struct i2c_client *client = to_i2c_client(dev);
1302 struct fschmd_data *data = i2c_get_clientdata(client);
1303 int i;
1304
1305 mutex_lock(&data->update_lock);
1306
1307 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
1308
1309 for (i = 0; i < FSCHMD_NO_TEMP_SENSORS[data->kind]; i++) {
1310 data->temp_act[i] = i2c_smbus_read_byte_data(client,
1311 FSCHMD_REG_TEMP_ACT[data->kind][i]);
1312 data->temp_status[i] = i2c_smbus_read_byte_data(client,
1313 FSCHMD_REG_TEMP_STATE[data->kind][i]);
1314
1315 /* The fscpos doesn't have TEMP_LIMIT registers */
1316 if (FSCHMD_REG_TEMP_LIMIT[data->kind][i])
1317 data->temp_max[i] = i2c_smbus_read_byte_data(
1318 client,
1319 FSCHMD_REG_TEMP_LIMIT[data->kind][i]);
1320
Guenter Roeck525ad372012-01-14 13:30:47 -08001321 /*
1322 * reset alarm if the alarm condition is gone,
1323 * the chip doesn't do this itself
1324 */
Hans de Goede569ff102007-10-11 08:06:29 -04001325 if ((data->temp_status[i] & FSCHMD_TEMP_ALARM_MASK) ==
1326 FSCHMD_TEMP_ALARM_MASK &&
1327 data->temp_act[i] < data->temp_max[i])
1328 i2c_smbus_write_byte_data(client,
1329 FSCHMD_REG_TEMP_STATE[data->kind][i],
Hans de Goedec69ab2b2009-03-30 21:46:45 +02001330 data->temp_status[i]);
Hans de Goede569ff102007-10-11 08:06:29 -04001331 }
1332
1333 for (i = 0; i < FSCHMD_NO_FAN_SENSORS[data->kind]; i++) {
1334 data->fan_act[i] = i2c_smbus_read_byte_data(client,
1335 FSCHMD_REG_FAN_ACT[data->kind][i]);
1336 data->fan_status[i] = i2c_smbus_read_byte_data(client,
1337 FSCHMD_REG_FAN_STATE[data->kind][i]);
1338 data->fan_ripple[i] = i2c_smbus_read_byte_data(client,
1339 FSCHMD_REG_FAN_RIPPLE[data->kind][i]);
1340
1341 /* The fscpos third fan doesn't have a fan_min */
1342 if (FSCHMD_REG_FAN_MIN[data->kind][i])
1343 data->fan_min[i] = i2c_smbus_read_byte_data(
1344 client,
1345 FSCHMD_REG_FAN_MIN[data->kind][i]);
1346
1347 /* reset fan status if speed is back to > 0 */
Hans de Goede453e3082009-01-07 16:37:33 +01001348 if ((data->fan_status[i] & FSCHMD_FAN_ALARM) &&
Hans de Goede569ff102007-10-11 08:06:29 -04001349 data->fan_act[i])
1350 i2c_smbus_write_byte_data(client,
1351 FSCHMD_REG_FAN_STATE[data->kind][i],
Hans de Goedec69ab2b2009-03-30 21:46:45 +02001352 data->fan_status[i]);
Hans de Goede569ff102007-10-11 08:06:29 -04001353 }
1354
Hans de Goedec69ab2b2009-03-30 21:46:45 +02001355 for (i = 0; i < FSCHMD_NO_VOLT_SENSORS[data->kind]; i++)
Hans de Goede569ff102007-10-11 08:06:29 -04001356 data->volt[i] = i2c_smbus_read_byte_data(client,
Hans de Goedec69ab2b2009-03-30 21:46:45 +02001357 FSCHMD_REG_VOLT[data->kind][i]);
Hans de Goede569ff102007-10-11 08:06:29 -04001358
Hans de Goede569ff102007-10-11 08:06:29 -04001359 data->last_updated = jiffies;
1360 data->valid = 1;
1361 }
1362
1363 mutex_unlock(&data->update_lock);
1364
1365 return data;
1366}
1367
Axel Linf0967ee2012-01-20 15:38:18 +08001368module_i2c_driver(fschmd_driver);
Hans de Goede569ff102007-10-11 08:06:29 -04001369
Hans de Goede453e3082009-01-07 16:37:33 +01001370MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
Hans de Goedede15f092009-03-30 21:46:45 +02001371MODULE_DESCRIPTION("FSC Poseidon, Hermes, Scylla, Heracles, Heimdall, Hades "
1372 "and Syleus driver");
Hans de Goede569ff102007-10-11 08:06:29 -04001373MODULE_LICENSE("GPL");