Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * fschmd.c
0004  *
0005  * Copyright (C) 2007 - 2009 Hans de Goede <hdegoede@redhat.com>
0006  */
0007 
0008 /*
0009  *  Merged Fujitsu Siemens hwmon driver, supporting the Poseidon, Hermes,
0010  *  Scylla, Heracles, Heimdall, Hades and Syleus chips
0011  *
0012  *  Based on the original 2.4 fscscy, 2.6 fscpos, 2.6 fscher and 2.6
0013  *  (candidate) fschmd drivers:
0014  *  Copyright (C) 2006 Thilo Cestonaro
0015  *          <thilo.cestonaro.external@fujitsu-siemens.com>
0016  *  Copyright (C) 2004, 2005 Stefan Ott <stefan@desire.ch>
0017  *  Copyright (C) 2003, 2004 Reinhard Nissl <rnissl@gmx.de>
0018  *  Copyright (c) 2001 Martin Knoblauch <mkn@teraport.de, knobi@knobisoft.de>
0019  *  Copyright (C) 2000 Hermann Jung <hej@odn.de>
0020  */
0021 
0022 #include <linux/module.h>
0023 #include <linux/init.h>
0024 #include <linux/slab.h>
0025 #include <linux/jiffies.h>
0026 #include <linux/i2c.h>
0027 #include <linux/hwmon.h>
0028 #include <linux/hwmon-sysfs.h>
0029 #include <linux/err.h>
0030 #include <linux/mutex.h>
0031 #include <linux/sysfs.h>
0032 #include <linux/dmi.h>
0033 #include <linux/fs.h>
0034 #include <linux/watchdog.h>
0035 #include <linux/miscdevice.h>
0036 #include <linux/uaccess.h>
0037 #include <linux/kref.h>
0038 
0039 /* Addresses to scan */
0040 static const unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
0041 
0042 /* Insmod parameters */
0043 static bool nowayout = WATCHDOG_NOWAYOUT;
0044 module_param(nowayout, bool, 0);
0045 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
0046     __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
0047 
0048 enum chips { fscpos, fscher, fscscy, fschrc, fschmd, fschds, fscsyl };
0049 
0050 /*
0051  * The FSCHMD registers and other defines
0052  */
0053 
0054 /* chip identification */
0055 #define FSCHMD_REG_IDENT_0      0x00
0056 #define FSCHMD_REG_IDENT_1      0x01
0057 #define FSCHMD_REG_IDENT_2      0x02
0058 #define FSCHMD_REG_REVISION     0x03
0059 
0060 /* global control and status */
0061 #define FSCHMD_REG_EVENT_STATE      0x04
0062 #define FSCHMD_REG_CONTROL      0x05
0063 
0064 #define FSCHMD_CONTROL_ALERT_LED    0x01
0065 
0066 /* watchdog */
0067 static const u8 FSCHMD_REG_WDOG_CONTROL[7] = {
0068     0x21, 0x21, 0x21, 0x21, 0x21, 0x28, 0x28 };
0069 static const u8 FSCHMD_REG_WDOG_STATE[7] = {
0070     0x23, 0x23, 0x23, 0x23, 0x23, 0x29, 0x29 };
0071 static const u8 FSCHMD_REG_WDOG_PRESET[7] = {
0072     0x28, 0x28, 0x28, 0x28, 0x28, 0x2a, 0x2a };
0073 
0074 #define FSCHMD_WDOG_CONTROL_TRIGGER 0x10
0075 #define FSCHMD_WDOG_CONTROL_STARTED 0x10 /* the same as trigger */
0076 #define FSCHMD_WDOG_CONTROL_STOP    0x20
0077 #define FSCHMD_WDOG_CONTROL_RESOLUTION  0x40
0078 
0079 #define FSCHMD_WDOG_STATE_CARDRESET 0x02
0080 
0081 /* voltages, weird order is to keep the same order as the old drivers */
0082 static const u8 FSCHMD_REG_VOLT[7][6] = {
0083     { 0x45, 0x42, 0x48 },               /* pos */
0084     { 0x45, 0x42, 0x48 },               /* her */
0085     { 0x45, 0x42, 0x48 },               /* scy */
0086     { 0x45, 0x42, 0x48 },               /* hrc */
0087     { 0x45, 0x42, 0x48 },               /* hmd */
0088     { 0x21, 0x20, 0x22 },               /* hds */
0089     { 0x21, 0x20, 0x22, 0x23, 0x24, 0x25 },     /* syl */
0090 };
0091 
0092 static const int FSCHMD_NO_VOLT_SENSORS[7] = { 3, 3, 3, 3, 3, 3, 6 };
0093 
0094 /*
0095  * minimum pwm at which the fan is driven (pwm can be increased depending on
0096  * the temp. Notice that for the scy some fans share there minimum speed.
0097  * Also notice that with the scy the sensor order is different than with the
0098  * other chips, this order was in the 2.4 driver and kept for consistency.
0099  */
0100 static const u8 FSCHMD_REG_FAN_MIN[7][7] = {
0101     { 0x55, 0x65 },                 /* pos */
0102     { 0x55, 0x65, 0xb5 },               /* her */
0103     { 0x65, 0x65, 0x55, 0xa5, 0x55, 0xa5 },     /* scy */
0104     { 0x55, 0x65, 0xa5, 0xb5 },         /* hrc */
0105     { 0x55, 0x65, 0xa5, 0xb5, 0xc5 },       /* hmd */
0106     { 0x55, 0x65, 0xa5, 0xb5, 0xc5 },       /* hds */
0107     { 0x54, 0x64, 0x74, 0x84, 0x94, 0xa4, 0xb4 },   /* syl */
0108 };
0109 
0110 /* actual fan speed */
0111 static const u8 FSCHMD_REG_FAN_ACT[7][7] = {
0112     { 0x0e, 0x6b, 0xab },               /* pos */
0113     { 0x0e, 0x6b, 0xbb },               /* her */
0114     { 0x6b, 0x6c, 0x0e, 0xab, 0x5c, 0xbb },     /* scy */
0115     { 0x0e, 0x6b, 0xab, 0xbb },         /* hrc */
0116     { 0x5b, 0x6b, 0xab, 0xbb, 0xcb },       /* hmd */
0117     { 0x5b, 0x6b, 0xab, 0xbb, 0xcb },       /* hds */
0118     { 0x57, 0x67, 0x77, 0x87, 0x97, 0xa7, 0xb7 },   /* syl */
0119 };
0120 
0121 /* fan status registers */
0122 static const u8 FSCHMD_REG_FAN_STATE[7][7] = {
0123     { 0x0d, 0x62, 0xa2 },               /* pos */
0124     { 0x0d, 0x62, 0xb2 },               /* her */
0125     { 0x62, 0x61, 0x0d, 0xa2, 0x52, 0xb2 },     /* scy */
0126     { 0x0d, 0x62, 0xa2, 0xb2 },         /* hrc */
0127     { 0x52, 0x62, 0xa2, 0xb2, 0xc2 },       /* hmd */
0128     { 0x52, 0x62, 0xa2, 0xb2, 0xc2 },       /* hds */
0129     { 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0 },   /* syl */
0130 };
0131 
0132 /* fan ripple / divider registers */
0133 static const u8 FSCHMD_REG_FAN_RIPPLE[7][7] = {
0134     { 0x0f, 0x6f, 0xaf },               /* pos */
0135     { 0x0f, 0x6f, 0xbf },               /* her */
0136     { 0x6f, 0x6f, 0x0f, 0xaf, 0x0f, 0xbf },     /* scy */
0137     { 0x0f, 0x6f, 0xaf, 0xbf },         /* hrc */
0138     { 0x5f, 0x6f, 0xaf, 0xbf, 0xcf },       /* hmd */
0139     { 0x5f, 0x6f, 0xaf, 0xbf, 0xcf },       /* hds */
0140     { 0x56, 0x66, 0x76, 0x86, 0x96, 0xa6, 0xb6 },   /* syl */
0141 };
0142 
0143 static const int FSCHMD_NO_FAN_SENSORS[7] = { 3, 3, 6, 4, 5, 5, 7 };
0144 
0145 /* Fan status register bitmasks */
0146 #define FSCHMD_FAN_ALARM    0x04 /* called fault by FSC! */
0147 #define FSCHMD_FAN_NOT_PRESENT  0x08
0148 #define FSCHMD_FAN_DISABLED 0x80
0149 
0150 
0151 /* actual temperature registers */
0152 static const u8 FSCHMD_REG_TEMP_ACT[7][11] = {
0153     { 0x64, 0x32, 0x35 },               /* pos */
0154     { 0x64, 0x32, 0x35 },               /* her */
0155     { 0x64, 0xD0, 0x32, 0x35 },         /* scy */
0156     { 0x64, 0x32, 0x35 },               /* hrc */
0157     { 0x70, 0x80, 0x90, 0xd0, 0xe0 },       /* hmd */
0158     { 0x70, 0x80, 0x90, 0xd0, 0xe0 },       /* hds */
0159     { 0x58, 0x68, 0x78, 0x88, 0x98, 0xa8,       /* syl */
0160       0xb8, 0xc8, 0xd8, 0xe8, 0xf8 },
0161 };
0162 
0163 /* temperature state registers */
0164 static const u8 FSCHMD_REG_TEMP_STATE[7][11] = {
0165     { 0x71, 0x81, 0x91 },               /* pos */
0166     { 0x71, 0x81, 0x91 },               /* her */
0167     { 0x71, 0xd1, 0x81, 0x91 },         /* scy */
0168     { 0x71, 0x81, 0x91 },               /* hrc */
0169     { 0x71, 0x81, 0x91, 0xd1, 0xe1 },       /* hmd */
0170     { 0x71, 0x81, 0x91, 0xd1, 0xe1 },       /* hds */
0171     { 0x59, 0x69, 0x79, 0x89, 0x99, 0xa9,       /* syl */
0172       0xb9, 0xc9, 0xd9, 0xe9, 0xf9 },
0173 };
0174 
0175 /*
0176  * temperature high limit registers, FSC does not document these. Proven to be
0177  * there with field testing on the fscher and fschrc, already supported / used
0178  * in the fscscy 2.4 driver. FSC has confirmed that the fschmd has registers
0179  * at these addresses, but doesn't want to confirm they are the same as with
0180  * the fscher??
0181  */
0182 static const u8 FSCHMD_REG_TEMP_LIMIT[7][11] = {
0183     { 0, 0, 0 },                    /* pos */
0184     { 0x76, 0x86, 0x96 },               /* her */
0185     { 0x76, 0xd6, 0x86, 0x96 },         /* scy */
0186     { 0x76, 0x86, 0x96 },               /* hrc */
0187     { 0x76, 0x86, 0x96, 0xd6, 0xe6 },       /* hmd */
0188     { 0x76, 0x86, 0x96, 0xd6, 0xe6 },       /* hds */
0189     { 0x5a, 0x6a, 0x7a, 0x8a, 0x9a, 0xaa,       /* syl */
0190       0xba, 0xca, 0xda, 0xea, 0xfa },
0191 };
0192 
0193 /*
0194  * These were found through experimenting with an fscher, currently they are
0195  * not used, but we keep them around for future reference.
0196  * On the fscsyl AUTOP1 lives at 0x#c (so 0x5c for fan1, 0x6c for fan2, etc),
0197  * AUTOP2 lives at 0x#e, and 0x#1 is a bitmask defining which temps influence
0198  * the fan speed.
0199  * static const u8 FSCHER_REG_TEMP_AUTOP1[] =   { 0x73, 0x83, 0x93 };
0200  * static const u8 FSCHER_REG_TEMP_AUTOP2[] =   { 0x75, 0x85, 0x95 };
0201  */
0202 
0203 static const int FSCHMD_NO_TEMP_SENSORS[7] = { 3, 3, 4, 3, 5, 5, 11 };
0204 
0205 /* temp status register bitmasks */
0206 #define FSCHMD_TEMP_WORKING 0x01
0207 #define FSCHMD_TEMP_ALERT   0x02
0208 #define FSCHMD_TEMP_DISABLED    0x80
0209 /* there only really is an alarm if the sensor is working and alert == 1 */
0210 #define FSCHMD_TEMP_ALARM_MASK \
0211     (FSCHMD_TEMP_WORKING | FSCHMD_TEMP_ALERT)
0212 
0213 /*
0214  * Functions declarations
0215  */
0216 
0217 static int fschmd_probe(struct i2c_client *client);
0218 static int fschmd_detect(struct i2c_client *client,
0219              struct i2c_board_info *info);
0220 static int fschmd_remove(struct i2c_client *client);
0221 static struct fschmd_data *fschmd_update_device(struct device *dev);
0222 
0223 /*
0224  * Driver data (common to all clients)
0225  */
0226 
0227 static const struct i2c_device_id fschmd_id[] = {
0228     { "fscpos", fscpos },
0229     { "fscher", fscher },
0230     { "fscscy", fscscy },
0231     { "fschrc", fschrc },
0232     { "fschmd", fschmd },
0233     { "fschds", fschds },
0234     { "fscsyl", fscsyl },
0235     { }
0236 };
0237 MODULE_DEVICE_TABLE(i2c, fschmd_id);
0238 
0239 static struct i2c_driver fschmd_driver = {
0240     .class      = I2C_CLASS_HWMON,
0241     .driver = {
0242         .name   = "fschmd",
0243     },
0244     .probe_new  = fschmd_probe,
0245     .remove     = fschmd_remove,
0246     .id_table   = fschmd_id,
0247     .detect     = fschmd_detect,
0248     .address_list   = normal_i2c,
0249 };
0250 
0251 /*
0252  * Client data (each client gets its own)
0253  */
0254 
0255 struct fschmd_data {
0256     struct i2c_client *client;
0257     struct device *hwmon_dev;
0258     struct mutex update_lock;
0259     struct mutex watchdog_lock;
0260     struct list_head list; /* member of the watchdog_data_list */
0261     struct kref kref;
0262     struct miscdevice watchdog_miscdev;
0263     enum chips kind;
0264     unsigned long watchdog_is_open;
0265     char watchdog_expect_close;
0266     char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
0267     bool valid; /* false until following fields are valid */
0268     unsigned long last_updated; /* in jiffies */
0269 
0270     /* register values */
0271     u8 revision;            /* chip revision */
0272     u8 global_control;  /* global control register */
0273     u8 watchdog_control;    /* watchdog control register */
0274     u8 watchdog_state;      /* watchdog status register */
0275     u8 watchdog_preset;     /* watchdog counter preset on trigger val */
0276     u8 volt[6];     /* voltage */
0277     u8 temp_act[11];    /* temperature */
0278     u8 temp_status[11]; /* status of sensor */
0279     u8 temp_max[11];    /* high temp limit, notice: undocumented! */
0280     u8 fan_act[7];      /* fans revolutions per second */
0281     u8 fan_status[7];   /* fan status */
0282     u8 fan_min[7];      /* fan min value for rps */
0283     u8 fan_ripple[7];   /* divider for rps */
0284 };
0285 
0286 /*
0287  * Global variables to hold information read from special DMI tables, which are
0288  * available on FSC machines with an fscher or later chip. There is no need to
0289  * protect these with a lock as they are only modified from our attach function
0290  * which always gets called with the i2c-core lock held and never accessed
0291  * before the attach function is done with them.
0292  */
0293 static int dmi_mult[6] = { 490, 200, 100, 100, 200, 100 };
0294 static int dmi_offset[6] = { 0, 0, 0, 0, 0, 0 };
0295 static int dmi_vref = -1;
0296 
0297 /*
0298  * Somewhat ugly :( global data pointer list with all fschmd devices, so that
0299  * we can find our device data as when using misc_register there is no other
0300  * method to get to ones device data from the open fop.
0301  */
0302 static LIST_HEAD(watchdog_data_list);
0303 /* Note this lock not only protect list access, but also data.kref access */
0304 static DEFINE_MUTEX(watchdog_data_mutex);
0305 
0306 /*
0307  * Release our data struct when we're detached from the i2c client *and* all
0308  * references to our watchdog device are released
0309  */
0310 static void fschmd_release_resources(struct kref *ref)
0311 {
0312     struct fschmd_data *data = container_of(ref, struct fschmd_data, kref);
0313     kfree(data);
0314 }
0315 
0316 /*
0317  * Sysfs attr show / store functions
0318  */
0319 
0320 static ssize_t in_value_show(struct device *dev,
0321                  struct device_attribute *devattr, char *buf)
0322 {
0323     const int max_reading[3] = { 14200, 6600, 3300 };
0324     int index = to_sensor_dev_attr(devattr)->index;
0325     struct fschmd_data *data = fschmd_update_device(dev);
0326 
0327     if (data->kind == fscher || data->kind >= fschrc)
0328         return sprintf(buf, "%d\n", (data->volt[index] * dmi_vref *
0329             dmi_mult[index]) / 255 + dmi_offset[index]);
0330     else
0331         return sprintf(buf, "%d\n", (data->volt[index] *
0332             max_reading[index] + 128) / 255);
0333 }
0334 
0335 
0336 #define TEMP_FROM_REG(val)  (((val) - 128) * 1000)
0337 
0338 static ssize_t temp_value_show(struct device *dev,
0339                    struct device_attribute *devattr, char *buf)
0340 {
0341     int index = to_sensor_dev_attr(devattr)->index;
0342     struct fschmd_data *data = fschmd_update_device(dev);
0343 
0344     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_act[index]));
0345 }
0346 
0347 static ssize_t temp_max_show(struct device *dev,
0348                  struct device_attribute *devattr, char *buf)
0349 {
0350     int index = to_sensor_dev_attr(devattr)->index;
0351     struct fschmd_data *data = fschmd_update_device(dev);
0352 
0353     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[index]));
0354 }
0355 
0356 static ssize_t temp_max_store(struct device *dev,
0357                   struct device_attribute *devattr,
0358                   const char *buf, size_t count)
0359 {
0360     int index = to_sensor_dev_attr(devattr)->index;
0361     struct fschmd_data *data = dev_get_drvdata(dev);
0362     long v;
0363     int err;
0364 
0365     err = kstrtol(buf, 10, &v);
0366     if (err)
0367         return err;
0368 
0369     v = clamp_val(v / 1000, -128, 127) + 128;
0370 
0371     mutex_lock(&data->update_lock);
0372     i2c_smbus_write_byte_data(to_i2c_client(dev),
0373         FSCHMD_REG_TEMP_LIMIT[data->kind][index], v);
0374     data->temp_max[index] = v;
0375     mutex_unlock(&data->update_lock);
0376 
0377     return count;
0378 }
0379 
0380 static ssize_t temp_fault_show(struct device *dev,
0381                    struct device_attribute *devattr, char *buf)
0382 {
0383     int index = to_sensor_dev_attr(devattr)->index;
0384     struct fschmd_data *data = fschmd_update_device(dev);
0385 
0386     /* bit 0 set means sensor working ok, so no fault! */
0387     if (data->temp_status[index] & FSCHMD_TEMP_WORKING)
0388         return sprintf(buf, "0\n");
0389     else
0390         return sprintf(buf, "1\n");
0391 }
0392 
0393 static ssize_t temp_alarm_show(struct device *dev,
0394                    struct device_attribute *devattr, char *buf)
0395 {
0396     int index = to_sensor_dev_attr(devattr)->index;
0397     struct fschmd_data *data = fschmd_update_device(dev);
0398 
0399     if ((data->temp_status[index] & FSCHMD_TEMP_ALARM_MASK) ==
0400             FSCHMD_TEMP_ALARM_MASK)
0401         return sprintf(buf, "1\n");
0402     else
0403         return sprintf(buf, "0\n");
0404 }
0405 
0406 
0407 #define RPM_FROM_REG(val)   ((val) * 60)
0408 
0409 static ssize_t fan_value_show(struct device *dev,
0410                   struct device_attribute *devattr, char *buf)
0411 {
0412     int index = to_sensor_dev_attr(devattr)->index;
0413     struct fschmd_data *data = fschmd_update_device(dev);
0414 
0415     return sprintf(buf, "%u\n", RPM_FROM_REG(data->fan_act[index]));
0416 }
0417 
0418 static ssize_t fan_div_show(struct device *dev,
0419                 struct device_attribute *devattr, char *buf)
0420 {
0421     int index = to_sensor_dev_attr(devattr)->index;
0422     struct fschmd_data *data = fschmd_update_device(dev);
0423 
0424     /* bits 2..7 reserved => mask with 3 */
0425     return sprintf(buf, "%d\n", 1 << (data->fan_ripple[index] & 3));
0426 }
0427 
0428 static ssize_t fan_div_store(struct device *dev,
0429                  struct device_attribute *devattr,
0430                  const char *buf, size_t count)
0431 {
0432     u8 reg;
0433     int index = to_sensor_dev_attr(devattr)->index;
0434     struct fschmd_data *data = dev_get_drvdata(dev);
0435     /* supported values: 2, 4, 8 */
0436     unsigned long v;
0437     int err;
0438 
0439     err = kstrtoul(buf, 10, &v);
0440     if (err)
0441         return err;
0442 
0443     switch (v) {
0444     case 2:
0445         v = 1;
0446         break;
0447     case 4:
0448         v = 2;
0449         break;
0450     case 8:
0451         v = 3;
0452         break;
0453     default:
0454         dev_err(dev,
0455             "fan_div value %lu not supported. Choose one of 2, 4 or 8!\n",
0456             v);
0457         return -EINVAL;
0458     }
0459 
0460     mutex_lock(&data->update_lock);
0461 
0462     reg = i2c_smbus_read_byte_data(to_i2c_client(dev),
0463         FSCHMD_REG_FAN_RIPPLE[data->kind][index]);
0464 
0465     /* bits 2..7 reserved => mask with 0x03 */
0466     reg &= ~0x03;
0467     reg |= v;
0468 
0469     i2c_smbus_write_byte_data(to_i2c_client(dev),
0470         FSCHMD_REG_FAN_RIPPLE[data->kind][index], reg);
0471 
0472     data->fan_ripple[index] = reg;
0473 
0474     mutex_unlock(&data->update_lock);
0475 
0476     return count;
0477 }
0478 
0479 static ssize_t fan_alarm_show(struct device *dev,
0480                   struct device_attribute *devattr, char *buf)
0481 {
0482     int index = to_sensor_dev_attr(devattr)->index;
0483     struct fschmd_data *data = fschmd_update_device(dev);
0484 
0485     if (data->fan_status[index] & FSCHMD_FAN_ALARM)
0486         return sprintf(buf, "1\n");
0487     else
0488         return sprintf(buf, "0\n");
0489 }
0490 
0491 static ssize_t fan_fault_show(struct device *dev,
0492                   struct device_attribute *devattr, char *buf)
0493 {
0494     int index = to_sensor_dev_attr(devattr)->index;
0495     struct fschmd_data *data = fschmd_update_device(dev);
0496 
0497     if (data->fan_status[index] & FSCHMD_FAN_NOT_PRESENT)
0498         return sprintf(buf, "1\n");
0499     else
0500         return sprintf(buf, "0\n");
0501 }
0502 
0503 
0504 static ssize_t pwm_auto_point1_pwm_show(struct device *dev,
0505                     struct device_attribute *devattr,
0506                     char *buf)
0507 {
0508     int index = to_sensor_dev_attr(devattr)->index;
0509     struct fschmd_data *data = fschmd_update_device(dev);
0510     int val = data->fan_min[index];
0511 
0512     /* 0 = allow turning off (except on the syl), 1-255 = 50-100% */
0513     if (val || data->kind == fscsyl)
0514         val = val / 2 + 128;
0515 
0516     return sprintf(buf, "%d\n", val);
0517 }
0518 
0519 static ssize_t pwm_auto_point1_pwm_store(struct device *dev,
0520                      struct device_attribute *devattr,
0521                      const char *buf, size_t count)
0522 {
0523     int index = to_sensor_dev_attr(devattr)->index;
0524     struct fschmd_data *data = dev_get_drvdata(dev);
0525     unsigned long v;
0526     int err;
0527 
0528     err = kstrtoul(buf, 10, &v);
0529     if (err)
0530         return err;
0531 
0532     /* reg: 0 = allow turning off (except on the syl), 1-255 = 50-100% */
0533     if (v || data->kind == fscsyl) {
0534         v = clamp_val(v, 128, 255);
0535         v = (v - 128) * 2 + 1;
0536     }
0537 
0538     mutex_lock(&data->update_lock);
0539 
0540     i2c_smbus_write_byte_data(to_i2c_client(dev),
0541         FSCHMD_REG_FAN_MIN[data->kind][index], v);
0542     data->fan_min[index] = v;
0543 
0544     mutex_unlock(&data->update_lock);
0545 
0546     return count;
0547 }
0548 
0549 
0550 /*
0551  * The FSC hwmon family has the ability to force an attached alert led to flash
0552  * from software, we export this as an alert_led sysfs attr
0553  */
0554 static ssize_t alert_led_show(struct device *dev,
0555     struct device_attribute *devattr, char *buf)
0556 {
0557     struct fschmd_data *data = fschmd_update_device(dev);
0558 
0559     if (data->global_control & FSCHMD_CONTROL_ALERT_LED)
0560         return sprintf(buf, "1\n");
0561     else
0562         return sprintf(buf, "0\n");
0563 }
0564 
0565 static ssize_t alert_led_store(struct device *dev,
0566     struct device_attribute *devattr, const char *buf, size_t count)
0567 {
0568     u8 reg;
0569     struct fschmd_data *data = dev_get_drvdata(dev);
0570     unsigned long v;
0571     int err;
0572 
0573     err = kstrtoul(buf, 10, &v);
0574     if (err)
0575         return err;
0576 
0577     mutex_lock(&data->update_lock);
0578 
0579     reg = i2c_smbus_read_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL);
0580 
0581     if (v)
0582         reg |= FSCHMD_CONTROL_ALERT_LED;
0583     else
0584         reg &= ~FSCHMD_CONTROL_ALERT_LED;
0585 
0586     i2c_smbus_write_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL, reg);
0587 
0588     data->global_control = reg;
0589 
0590     mutex_unlock(&data->update_lock);
0591 
0592     return count;
0593 }
0594 
0595 static DEVICE_ATTR_RW(alert_led);
0596 
0597 static struct sensor_device_attribute fschmd_attr[] = {
0598     SENSOR_ATTR_RO(in0_input, in_value, 0),
0599     SENSOR_ATTR_RO(in1_input, in_value, 1),
0600     SENSOR_ATTR_RO(in2_input, in_value, 2),
0601     SENSOR_ATTR_RO(in3_input, in_value, 3),
0602     SENSOR_ATTR_RO(in4_input, in_value, 4),
0603     SENSOR_ATTR_RO(in5_input, in_value, 5),
0604 };
0605 
0606 static struct sensor_device_attribute fschmd_temp_attr[] = {
0607     SENSOR_ATTR_RO(temp1_input, temp_value, 0),
0608     SENSOR_ATTR_RW(temp1_max, temp_max, 0),
0609     SENSOR_ATTR_RO(temp1_fault, temp_fault, 0),
0610     SENSOR_ATTR_RO(temp1_alarm, temp_alarm, 0),
0611     SENSOR_ATTR_RO(temp2_input, temp_value, 1),
0612     SENSOR_ATTR_RW(temp2_max, temp_max, 1),
0613     SENSOR_ATTR_RO(temp2_fault, temp_fault, 1),
0614     SENSOR_ATTR_RO(temp2_alarm, temp_alarm, 1),
0615     SENSOR_ATTR_RO(temp3_input, temp_value, 2),
0616     SENSOR_ATTR_RW(temp3_max, temp_max, 2),
0617     SENSOR_ATTR_RO(temp3_fault, temp_fault, 2),
0618     SENSOR_ATTR_RO(temp3_alarm, temp_alarm, 2),
0619     SENSOR_ATTR_RO(temp4_input, temp_value, 3),
0620     SENSOR_ATTR_RW(temp4_max, temp_max, 3),
0621     SENSOR_ATTR_RO(temp4_fault, temp_fault, 3),
0622     SENSOR_ATTR_RO(temp4_alarm, temp_alarm, 3),
0623     SENSOR_ATTR_RO(temp5_input, temp_value, 4),
0624     SENSOR_ATTR_RW(temp5_max, temp_max, 4),
0625     SENSOR_ATTR_RO(temp5_fault, temp_fault, 4),
0626     SENSOR_ATTR_RO(temp5_alarm, temp_alarm, 4),
0627     SENSOR_ATTR_RO(temp6_input, temp_value, 5),
0628     SENSOR_ATTR_RW(temp6_max, temp_max, 5),
0629     SENSOR_ATTR_RO(temp6_fault, temp_fault, 5),
0630     SENSOR_ATTR_RO(temp6_alarm, temp_alarm, 5),
0631     SENSOR_ATTR_RO(temp7_input, temp_value, 6),
0632     SENSOR_ATTR_RW(temp7_max, temp_max, 6),
0633     SENSOR_ATTR_RO(temp7_fault, temp_fault, 6),
0634     SENSOR_ATTR_RO(temp7_alarm, temp_alarm, 6),
0635     SENSOR_ATTR_RO(temp8_input, temp_value, 7),
0636     SENSOR_ATTR_RW(temp8_max, temp_max, 7),
0637     SENSOR_ATTR_RO(temp8_fault, temp_fault, 7),
0638     SENSOR_ATTR_RO(temp8_alarm, temp_alarm, 7),
0639     SENSOR_ATTR_RO(temp9_input, temp_value, 8),
0640     SENSOR_ATTR_RW(temp9_max, temp_max, 8),
0641     SENSOR_ATTR_RO(temp9_fault, temp_fault, 8),
0642     SENSOR_ATTR_RO(temp9_alarm, temp_alarm, 8),
0643     SENSOR_ATTR_RO(temp10_input, temp_value, 9),
0644     SENSOR_ATTR_RW(temp10_max, temp_max, 9),
0645     SENSOR_ATTR_RO(temp10_fault, temp_fault, 9),
0646     SENSOR_ATTR_RO(temp10_alarm, temp_alarm, 9),
0647     SENSOR_ATTR_RO(temp11_input, temp_value, 10),
0648     SENSOR_ATTR_RW(temp11_max, temp_max, 10),
0649     SENSOR_ATTR_RO(temp11_fault, temp_fault, 10),
0650     SENSOR_ATTR_RO(temp11_alarm, temp_alarm, 10),
0651 };
0652 
0653 static struct sensor_device_attribute fschmd_fan_attr[] = {
0654     SENSOR_ATTR_RO(fan1_input, fan_value, 0),
0655     SENSOR_ATTR_RW(fan1_div, fan_div, 0),
0656     SENSOR_ATTR_RO(fan1_alarm, fan_alarm, 0),
0657     SENSOR_ATTR_RO(fan1_fault, fan_fault, 0),
0658     SENSOR_ATTR_RW(pwm1_auto_point1_pwm, pwm_auto_point1_pwm, 0),
0659     SENSOR_ATTR_RO(fan2_input, fan_value, 1),
0660     SENSOR_ATTR_RW(fan2_div, fan_div, 1),
0661     SENSOR_ATTR_RO(fan2_alarm, fan_alarm, 1),
0662     SENSOR_ATTR_RO(fan2_fault, fan_fault, 1),
0663     SENSOR_ATTR_RW(pwm2_auto_point1_pwm, pwm_auto_point1_pwm, 1),
0664     SENSOR_ATTR_RO(fan3_input, fan_value, 2),
0665     SENSOR_ATTR_RW(fan3_div, fan_div, 2),
0666     SENSOR_ATTR_RO(fan3_alarm, fan_alarm, 2),
0667     SENSOR_ATTR_RO(fan3_fault, fan_fault, 2),
0668     SENSOR_ATTR_RW(pwm3_auto_point1_pwm, pwm_auto_point1_pwm, 2),
0669     SENSOR_ATTR_RO(fan4_input, fan_value, 3),
0670     SENSOR_ATTR_RW(fan4_div, fan_div, 3),
0671     SENSOR_ATTR_RO(fan4_alarm, fan_alarm, 3),
0672     SENSOR_ATTR_RO(fan4_fault, fan_fault, 3),
0673     SENSOR_ATTR_RW(pwm4_auto_point1_pwm, pwm_auto_point1_pwm, 3),
0674     SENSOR_ATTR_RO(fan5_input, fan_value, 4),
0675     SENSOR_ATTR_RW(fan5_div, fan_div, 4),
0676     SENSOR_ATTR_RO(fan5_alarm, fan_alarm, 4),
0677     SENSOR_ATTR_RO(fan5_fault, fan_fault, 4),
0678     SENSOR_ATTR_RW(pwm5_auto_point1_pwm, pwm_auto_point1_pwm, 4),
0679     SENSOR_ATTR_RO(fan6_input, fan_value, 5),
0680     SENSOR_ATTR_RW(fan6_div, fan_div, 5),
0681     SENSOR_ATTR_RO(fan6_alarm, fan_alarm, 5),
0682     SENSOR_ATTR_RO(fan6_fault, fan_fault, 5),
0683     SENSOR_ATTR_RW(pwm6_auto_point1_pwm, pwm_auto_point1_pwm, 5),
0684     SENSOR_ATTR_RO(fan7_input, fan_value, 6),
0685     SENSOR_ATTR_RW(fan7_div, fan_div, 6),
0686     SENSOR_ATTR_RO(fan7_alarm, fan_alarm, 6),
0687     SENSOR_ATTR_RO(fan7_fault, fan_fault, 6),
0688     SENSOR_ATTR_RW(pwm7_auto_point1_pwm, pwm_auto_point1_pwm, 6),
0689 };
0690 
0691 
0692 /*
0693  * Watchdog routines
0694  */
0695 
0696 static int watchdog_set_timeout(struct fschmd_data *data, int timeout)
0697 {
0698     int ret, resolution;
0699     int kind = data->kind + 1; /* 0-x array index -> 1-x module param */
0700 
0701     /* 2 second or 60 second resolution? */
0702     if (timeout <= 510 || kind == fscpos || kind == fscscy)
0703         resolution = 2;
0704     else
0705         resolution = 60;
0706 
0707     if (timeout < resolution || timeout > (resolution * 255))
0708         return -EINVAL;
0709 
0710     mutex_lock(&data->watchdog_lock);
0711     if (!data->client) {
0712         ret = -ENODEV;
0713         goto leave;
0714     }
0715 
0716     if (resolution == 2)
0717         data->watchdog_control &= ~FSCHMD_WDOG_CONTROL_RESOLUTION;
0718     else
0719         data->watchdog_control |= FSCHMD_WDOG_CONTROL_RESOLUTION;
0720 
0721     data->watchdog_preset = DIV_ROUND_UP(timeout, resolution);
0722 
0723     /* Write new timeout value */
0724     i2c_smbus_write_byte_data(data->client,
0725         FSCHMD_REG_WDOG_PRESET[data->kind], data->watchdog_preset);
0726     /* Write new control register, do not trigger! */
0727     i2c_smbus_write_byte_data(data->client,
0728         FSCHMD_REG_WDOG_CONTROL[data->kind],
0729         data->watchdog_control & ~FSCHMD_WDOG_CONTROL_TRIGGER);
0730 
0731     ret = data->watchdog_preset * resolution;
0732 
0733 leave:
0734     mutex_unlock(&data->watchdog_lock);
0735     return ret;
0736 }
0737 
0738 static int watchdog_get_timeout(struct fschmd_data *data)
0739 {
0740     int timeout;
0741 
0742     mutex_lock(&data->watchdog_lock);
0743     if (data->watchdog_control & FSCHMD_WDOG_CONTROL_RESOLUTION)
0744         timeout = data->watchdog_preset * 60;
0745     else
0746         timeout = data->watchdog_preset * 2;
0747     mutex_unlock(&data->watchdog_lock);
0748 
0749     return timeout;
0750 }
0751 
0752 static int watchdog_trigger(struct fschmd_data *data)
0753 {
0754     int ret = 0;
0755 
0756     mutex_lock(&data->watchdog_lock);
0757     if (!data->client) {
0758         ret = -ENODEV;
0759         goto leave;
0760     }
0761 
0762     data->watchdog_control |= FSCHMD_WDOG_CONTROL_TRIGGER;
0763     i2c_smbus_write_byte_data(data->client,
0764                   FSCHMD_REG_WDOG_CONTROL[data->kind],
0765                   data->watchdog_control);
0766 leave:
0767     mutex_unlock(&data->watchdog_lock);
0768     return ret;
0769 }
0770 
0771 static int watchdog_stop(struct fschmd_data *data)
0772 {
0773     int ret = 0;
0774 
0775     mutex_lock(&data->watchdog_lock);
0776     if (!data->client) {
0777         ret = -ENODEV;
0778         goto leave;
0779     }
0780 
0781     data->watchdog_control &= ~FSCHMD_WDOG_CONTROL_STARTED;
0782     /*
0783      * Don't store the stop flag in our watchdog control register copy, as
0784      * its a write only bit (read always returns 0)
0785      */
0786     i2c_smbus_write_byte_data(data->client,
0787         FSCHMD_REG_WDOG_CONTROL[data->kind],
0788         data->watchdog_control | FSCHMD_WDOG_CONTROL_STOP);
0789 leave:
0790     mutex_unlock(&data->watchdog_lock);
0791     return ret;
0792 }
0793 
0794 static int watchdog_open(struct inode *inode, struct file *filp)
0795 {
0796     struct fschmd_data *pos, *data = NULL;
0797     int watchdog_is_open;
0798 
0799     /*
0800      * We get called from drivers/char/misc.c with misc_mtx hold, and we
0801      * call misc_register() from fschmd_probe() with watchdog_data_mutex
0802      * hold, as misc_register() takes the misc_mtx lock, this is a possible
0803      * deadlock, so we use mutex_trylock here.
0804      */
0805     if (!mutex_trylock(&watchdog_data_mutex))
0806         return -ERESTARTSYS;
0807     list_for_each_entry(pos, &watchdog_data_list, list) {
0808         if (pos->watchdog_miscdev.minor == iminor(inode)) {
0809             data = pos;
0810             break;
0811         }
0812     }
0813     /* Note we can never not have found data, so we don't check for this */
0814     watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
0815     if (!watchdog_is_open)
0816         kref_get(&data->kref);
0817     mutex_unlock(&watchdog_data_mutex);
0818 
0819     if (watchdog_is_open)
0820         return -EBUSY;
0821 
0822     /* Start the watchdog */
0823     watchdog_trigger(data);
0824     filp->private_data = data;
0825 
0826     return stream_open(inode, filp);
0827 }
0828 
0829 static int watchdog_release(struct inode *inode, struct file *filp)
0830 {
0831     struct fschmd_data *data = filp->private_data;
0832 
0833     if (data->watchdog_expect_close) {
0834         watchdog_stop(data);
0835         data->watchdog_expect_close = 0;
0836     } else {
0837         watchdog_trigger(data);
0838         dev_crit(&data->client->dev,
0839             "unexpected close, not stopping watchdog!\n");
0840     }
0841 
0842     clear_bit(0, &data->watchdog_is_open);
0843 
0844     mutex_lock(&watchdog_data_mutex);
0845     kref_put(&data->kref, fschmd_release_resources);
0846     mutex_unlock(&watchdog_data_mutex);
0847 
0848     return 0;
0849 }
0850 
0851 static ssize_t watchdog_write(struct file *filp, const char __user *buf,
0852     size_t count, loff_t *offset)
0853 {
0854     int ret;
0855     struct fschmd_data *data = filp->private_data;
0856 
0857     if (count) {
0858         if (!nowayout) {
0859             size_t i;
0860 
0861             /* Clear it in case it was set with a previous write */
0862             data->watchdog_expect_close = 0;
0863 
0864             for (i = 0; i != count; i++) {
0865                 char c;
0866                 if (get_user(c, buf + i))
0867                     return -EFAULT;
0868                 if (c == 'V')
0869                     data->watchdog_expect_close = 1;
0870             }
0871         }
0872         ret = watchdog_trigger(data);
0873         if (ret < 0)
0874             return ret;
0875     }
0876     return count;
0877 }
0878 
0879 static long watchdog_ioctl(struct file *filp, unsigned int cmd,
0880                unsigned long arg)
0881 {
0882     struct watchdog_info ident = {
0883         .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
0884                 WDIOF_CARDRESET,
0885         .identity = "FSC watchdog"
0886     };
0887     int i, ret = 0;
0888     struct fschmd_data *data = filp->private_data;
0889 
0890     switch (cmd) {
0891     case WDIOC_GETSUPPORT:
0892         ident.firmware_version = data->revision;
0893         if (!nowayout)
0894             ident.options |= WDIOF_MAGICCLOSE;
0895         if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
0896             ret = -EFAULT;
0897         break;
0898 
0899     case WDIOC_GETSTATUS:
0900         ret = put_user(0, (int __user *)arg);
0901         break;
0902 
0903     case WDIOC_GETBOOTSTATUS:
0904         if (data->watchdog_state & FSCHMD_WDOG_STATE_CARDRESET)
0905             ret = put_user(WDIOF_CARDRESET, (int __user *)arg);
0906         else
0907             ret = put_user(0, (int __user *)arg);
0908         break;
0909 
0910     case WDIOC_KEEPALIVE:
0911         ret = watchdog_trigger(data);
0912         break;
0913 
0914     case WDIOC_GETTIMEOUT:
0915         i = watchdog_get_timeout(data);
0916         ret = put_user(i, (int __user *)arg);
0917         break;
0918 
0919     case WDIOC_SETTIMEOUT:
0920         if (get_user(i, (int __user *)arg)) {
0921             ret = -EFAULT;
0922             break;
0923         }
0924         ret = watchdog_set_timeout(data, i);
0925         if (ret > 0)
0926             ret = put_user(ret, (int __user *)arg);
0927         break;
0928 
0929     case WDIOC_SETOPTIONS:
0930         if (get_user(i, (int __user *)arg)) {
0931             ret = -EFAULT;
0932             break;
0933         }
0934 
0935         if (i & WDIOS_DISABLECARD)
0936             ret = watchdog_stop(data);
0937         else if (i & WDIOS_ENABLECARD)
0938             ret = watchdog_trigger(data);
0939         else
0940             ret = -EINVAL;
0941 
0942         break;
0943     default:
0944         ret = -ENOTTY;
0945     }
0946     return ret;
0947 }
0948 
0949 static const struct file_operations watchdog_fops = {
0950     .owner = THIS_MODULE,
0951     .llseek = no_llseek,
0952     .open = watchdog_open,
0953     .release = watchdog_release,
0954     .write = watchdog_write,
0955     .unlocked_ioctl = watchdog_ioctl,
0956     .compat_ioctl = compat_ptr_ioctl,
0957 };
0958 
0959 
0960 /*
0961  * Detect, register, unregister and update device functions
0962  */
0963 
0964 /*
0965  * DMI decode routine to read voltage scaling factors from special DMI tables,
0966  * which are available on FSC machines with an fscher or later chip.
0967  */
0968 static void fschmd_dmi_decode(const struct dmi_header *header, void *dummy)
0969 {
0970     int i, mult[3] = { 0 }, offset[3] = { 0 }, vref = 0, found = 0;
0971 
0972     /*
0973      * dmi code ugliness, we get passed the address of the contents of
0974      * a complete DMI record, but in the form of a dmi_header pointer, in
0975      * reality this address holds header->length bytes of which the header
0976      * are the first 4 bytes
0977      */
0978     u8 *dmi_data = (u8 *)header;
0979 
0980     /* We are looking for OEM-specific type 185 */
0981     if (header->type != 185)
0982         return;
0983 
0984     /*
0985      * we are looking for what Siemens calls "subtype" 19, the subtype
0986      * is stored in byte 5 of the dmi block
0987      */
0988     if (header->length < 5 || dmi_data[4] != 19)
0989         return;
0990 
0991     /*
0992      * After the subtype comes 1 unknown byte and then blocks of 5 bytes,
0993      * consisting of what Siemens calls an "Entity" number, followed by
0994      * 2 16-bit words in LSB first order
0995      */
0996     for (i = 6; (i + 4) < header->length; i += 5) {
0997         /* entity 1 - 3: voltage multiplier and offset */
0998         if (dmi_data[i] >= 1 && dmi_data[i] <= 3) {
0999             /* 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         }
1030         /*
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          */
1035         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];
1041         dmi_vref = vref;
1042     }
1043 }
1044 
1045 static int fschmd_detect(struct i2c_client *client,
1046              struct i2c_board_info *info)
1047 {
1048     enum chips kind;
1049     struct i2c_adapter *adapter = client->adapter;
1050     char id[4];
1051 
1052     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1053         return -ENODEV;
1054 
1055     /* Detect & Identify the chip */
1056     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';
1060 
1061     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;
1077 
1078     strlcpy(info->type, fschmd_id[kind].name, I2C_NAME_SIZE);
1079 
1080     return 0;
1081 }
1082 
1083 static int fschmd_probe(struct i2c_client *client)
1084 {
1085     struct fschmd_data *data;
1086     const char * const names[7] = { "Poseidon", "Hermes", "Scylla",
1087                 "Heracles", "Heimdall", "Hades", "Syleus" };
1088     const int watchdog_minors[] = { WATCHDOG_MINOR, 212, 213, 214, 215 };
1089     int i, err;
1090     enum chips kind = i2c_match_id(fschmd_id, client)->driver_data;
1091 
1092     data = kzalloc(sizeof(struct fschmd_data), GFP_KERNEL);
1093     if (!data)
1094         return -ENOMEM;
1095 
1096     i2c_set_clientdata(client, data);
1097     mutex_init(&data->update_lock);
1098     mutex_init(&data->watchdog_lock);
1099     INIT_LIST_HEAD(&data->list);
1100     kref_init(&data->kref);
1101     /*
1102      * Store client pointer in our data struct for watchdog usage
1103      * (where the client is found through a data ptr instead of the
1104      * otherway around)
1105      */
1106     data->client = client;
1107     data->kind = kind;
1108 
1109     if (kind == fscpos) {
1110         /*
1111          * The Poseidon has hardwired temp limits, fill these
1112          * in for the alarm resetting code
1113          */
1114         data->temp_max[0] = 70 + 128;
1115         data->temp_max[1] = 50 + 128;
1116         data->temp_max[2] = 50 + 128;
1117     }
1118 
1119     /* Read the special DMI table for fscher and newer chips */
1120     if ((kind == fscher || kind >= fschrc) && dmi_vref == -1) {
1121         dmi_walk(fschmd_dmi_decode, NULL);
1122         if (dmi_vref == -1) {
1123             dev_warn(&client->dev,
1124                 "Couldn't get voltage scaling factors from "
1125                 "BIOS DMI table, using builtin defaults\n");
1126             dmi_vref = 33;
1127         }
1128     }
1129 
1130     /* Read in some never changing registers */
1131     data->revision = i2c_smbus_read_byte_data(client, FSCHMD_REG_REVISION);
1132     data->global_control = i2c_smbus_read_byte_data(client,
1133                     FSCHMD_REG_CONTROL);
1134     data->watchdog_control = i2c_smbus_read_byte_data(client,
1135                     FSCHMD_REG_WDOG_CONTROL[data->kind]);
1136     data->watchdog_state = i2c_smbus_read_byte_data(client,
1137                     FSCHMD_REG_WDOG_STATE[data->kind]);
1138     data->watchdog_preset = i2c_smbus_read_byte_data(client,
1139                     FSCHMD_REG_WDOG_PRESET[data->kind]);
1140 
1141     err = device_create_file(&client->dev, &dev_attr_alert_led);
1142     if (err)
1143         goto exit_detach;
1144 
1145     for (i = 0; i < FSCHMD_NO_VOLT_SENSORS[data->kind]; i++) {
1146         err = device_create_file(&client->dev,
1147                     &fschmd_attr[i].dev_attr);
1148         if (err)
1149             goto exit_detach;
1150     }
1151 
1152     for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++) {
1153         /* Poseidon doesn't have TEMP_LIMIT registers */
1154         if (kind == fscpos && fschmd_temp_attr[i].dev_attr.show ==
1155                 temp_max_show)
1156             continue;
1157 
1158         if (kind == fscsyl) {
1159             if (i % 4 == 0)
1160                 data->temp_status[i / 4] =
1161                     i2c_smbus_read_byte_data(client,
1162                         FSCHMD_REG_TEMP_STATE
1163                         [data->kind][i / 4]);
1164             if (data->temp_status[i / 4] & FSCHMD_TEMP_DISABLED)
1165                 continue;
1166         }
1167 
1168         err = device_create_file(&client->dev,
1169                     &fschmd_temp_attr[i].dev_attr);
1170         if (err)
1171             goto exit_detach;
1172     }
1173 
1174     for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++) {
1175         /* Poseidon doesn't have a FAN_MIN register for its 3rd fan */
1176         if (kind == fscpos &&
1177                 !strcmp(fschmd_fan_attr[i].dev_attr.attr.name,
1178                     "pwm3_auto_point1_pwm"))
1179             continue;
1180 
1181         if (kind == fscsyl) {
1182             if (i % 5 == 0)
1183                 data->fan_status[i / 5] =
1184                     i2c_smbus_read_byte_data(client,
1185                         FSCHMD_REG_FAN_STATE
1186                         [data->kind][i / 5]);
1187             if (data->fan_status[i / 5] & FSCHMD_FAN_DISABLED)
1188                 continue;
1189         }
1190 
1191         err = device_create_file(&client->dev,
1192                     &fschmd_fan_attr[i].dev_attr);
1193         if (err)
1194             goto exit_detach;
1195     }
1196 
1197     data->hwmon_dev = hwmon_device_register(&client->dev);
1198     if (IS_ERR(data->hwmon_dev)) {
1199         err = PTR_ERR(data->hwmon_dev);
1200         data->hwmon_dev = NULL;
1201         goto exit_detach;
1202     }
1203 
1204     /*
1205      * We take the data_mutex lock early so that watchdog_open() cannot
1206      * run when misc_register() has completed, but we've not yet added
1207      * our data to the watchdog_data_list (and set the default timeout)
1208      */
1209     mutex_lock(&watchdog_data_mutex);
1210     for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
1211         /* Register our watchdog part */
1212         snprintf(data->watchdog_name, sizeof(data->watchdog_name),
1213             "watchdog%c", (i == 0) ? '\0' : ('0' + i));
1214         data->watchdog_miscdev.name = data->watchdog_name;
1215         data->watchdog_miscdev.fops = &watchdog_fops;
1216         data->watchdog_miscdev.minor = watchdog_minors[i];
1217         err = misc_register(&data->watchdog_miscdev);
1218         if (err == -EBUSY)
1219             continue;
1220         if (err) {
1221             data->watchdog_miscdev.minor = 0;
1222             dev_err(&client->dev,
1223                 "Registering watchdog chardev: %d\n", err);
1224             break;
1225         }
1226 
1227         list_add(&data->list, &watchdog_data_list);
1228         watchdog_set_timeout(data, 60);
1229         dev_info(&client->dev,
1230             "Registered watchdog chardev major 10, minor: %d\n",
1231             watchdog_minors[i]);
1232         break;
1233     }
1234     if (i == ARRAY_SIZE(watchdog_minors)) {
1235         data->watchdog_miscdev.minor = 0;
1236         dev_warn(&client->dev,
1237              "Couldn't register watchdog chardev (due to no free minor)\n");
1238     }
1239     mutex_unlock(&watchdog_data_mutex);
1240 
1241     dev_info(&client->dev, "Detected FSC %s chip, revision: %d\n",
1242         names[data->kind], (int) data->revision);
1243 
1244     return 0;
1245 
1246 exit_detach:
1247     fschmd_remove(client); /* will also free data for us */
1248     return err;
1249 }
1250 
1251 static int fschmd_remove(struct i2c_client *client)
1252 {
1253     struct fschmd_data *data = i2c_get_clientdata(client);
1254     int i;
1255 
1256     /* Unregister the watchdog (if registered) */
1257     if (data->watchdog_miscdev.minor) {
1258         misc_deregister(&data->watchdog_miscdev);
1259         if (data->watchdog_is_open) {
1260             dev_warn(&client->dev,
1261                 "i2c client detached with watchdog open! "
1262                 "Stopping watchdog.\n");
1263             watchdog_stop(data);
1264         }
1265         mutex_lock(&watchdog_data_mutex);
1266         list_del(&data->list);
1267         mutex_unlock(&watchdog_data_mutex);
1268         /* Tell the watchdog code the client is gone */
1269         mutex_lock(&data->watchdog_lock);
1270         data->client = NULL;
1271         mutex_unlock(&data->watchdog_lock);
1272     }
1273 
1274     /*
1275      * Check if registered in case we're called from fschmd_detect
1276      * to cleanup after an error
1277      */
1278     if (data->hwmon_dev)
1279         hwmon_device_unregister(data->hwmon_dev);
1280 
1281     device_remove_file(&client->dev, &dev_attr_alert_led);
1282     for (i = 0; i < (FSCHMD_NO_VOLT_SENSORS[data->kind]); i++)
1283         device_remove_file(&client->dev, &fschmd_attr[i].dev_attr);
1284     for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++)
1285         device_remove_file(&client->dev,
1286                     &fschmd_temp_attr[i].dev_attr);
1287     for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++)
1288         device_remove_file(&client->dev,
1289                     &fschmd_fan_attr[i].dev_attr);
1290 
1291     mutex_lock(&watchdog_data_mutex);
1292     kref_put(&data->kref, fschmd_release_resources);
1293     mutex_unlock(&watchdog_data_mutex);
1294 
1295     return 0;
1296 }
1297 
1298 static struct fschmd_data *fschmd_update_device(struct device *dev)
1299 {
1300     struct i2c_client *client = to_i2c_client(dev);
1301     struct fschmd_data *data = i2c_get_clientdata(client);
1302     int i;
1303 
1304     mutex_lock(&data->update_lock);
1305 
1306     if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
1307 
1308         for (i = 0; i < FSCHMD_NO_TEMP_SENSORS[data->kind]; i++) {
1309             data->temp_act[i] = i2c_smbus_read_byte_data(client,
1310                     FSCHMD_REG_TEMP_ACT[data->kind][i]);
1311             data->temp_status[i] = i2c_smbus_read_byte_data(client,
1312                     FSCHMD_REG_TEMP_STATE[data->kind][i]);
1313 
1314             /* The fscpos doesn't have TEMP_LIMIT registers */
1315             if (FSCHMD_REG_TEMP_LIMIT[data->kind][i])
1316                 data->temp_max[i] = i2c_smbus_read_byte_data(
1317                     client,
1318                     FSCHMD_REG_TEMP_LIMIT[data->kind][i]);
1319 
1320             /*
1321              * reset alarm if the alarm condition is gone,
1322              * the chip doesn't do this itself
1323              */
1324             if ((data->temp_status[i] & FSCHMD_TEMP_ALARM_MASK) ==
1325                     FSCHMD_TEMP_ALARM_MASK &&
1326                     data->temp_act[i] < data->temp_max[i])
1327                 i2c_smbus_write_byte_data(client,
1328                     FSCHMD_REG_TEMP_STATE[data->kind][i],
1329                     data->temp_status[i]);
1330         }
1331 
1332         for (i = 0; i < FSCHMD_NO_FAN_SENSORS[data->kind]; i++) {
1333             data->fan_act[i] = i2c_smbus_read_byte_data(client,
1334                     FSCHMD_REG_FAN_ACT[data->kind][i]);
1335             data->fan_status[i] = i2c_smbus_read_byte_data(client,
1336                     FSCHMD_REG_FAN_STATE[data->kind][i]);
1337             data->fan_ripple[i] = i2c_smbus_read_byte_data(client,
1338                     FSCHMD_REG_FAN_RIPPLE[data->kind][i]);
1339 
1340             /* The fscpos third fan doesn't have a fan_min */
1341             if (FSCHMD_REG_FAN_MIN[data->kind][i])
1342                 data->fan_min[i] = i2c_smbus_read_byte_data(
1343                     client,
1344                     FSCHMD_REG_FAN_MIN[data->kind][i]);
1345 
1346             /* reset fan status if speed is back to > 0 */
1347             if ((data->fan_status[i] & FSCHMD_FAN_ALARM) &&
1348                     data->fan_act[i])
1349                 i2c_smbus_write_byte_data(client,
1350                     FSCHMD_REG_FAN_STATE[data->kind][i],
1351                     data->fan_status[i]);
1352         }
1353 
1354         for (i = 0; i < FSCHMD_NO_VOLT_SENSORS[data->kind]; i++)
1355             data->volt[i] = i2c_smbus_read_byte_data(client,
1356                            FSCHMD_REG_VOLT[data->kind][i]);
1357 
1358         data->last_updated = jiffies;
1359         data->valid = true;
1360     }
1361 
1362     mutex_unlock(&data->update_lock);
1363 
1364     return data;
1365 }
1366 
1367 module_i2c_driver(fschmd_driver);
1368 
1369 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
1370 MODULE_DESCRIPTION("FSC Poseidon, Hermes, Scylla, Heracles, Heimdall, Hades "
1371             "and Syleus driver");
1372 MODULE_LICENSE("GPL");