Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * gl518sm.c - Part of lm_sensors, Linux kernel modules for hardware
0004  *             monitoring
0005  * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
0006  * Kyosti Malkki <kmalkki@cc.hut.fi>
0007  * Copyright (C) 2004 Hong-Gunn Chew <hglinux@gunnet.org> and
0008  * Jean Delvare <jdelvare@suse.de>
0009  *
0010  * Ported to Linux 2.6 by Hong-Gunn Chew with the help of Jean Delvare
0011  * and advice of Greg Kroah-Hartman.
0012  *
0013  * Notes about the port:
0014  * Release 0x00 of the GL518SM chipset doesn't support reading of in0,
0015  * in1 nor in2. The original driver had an ugly workaround to get them
0016  * anyway (changing limits and watching alarms trigger and wear off).
0017  * We did not keep that part of the original driver in the Linux 2.6
0018  * version, since it was making the driver significantly more complex
0019  * with no real benefit.
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 
0033 /* Addresses to scan */
0034 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
0035 
0036 enum chips { gl518sm_r00, gl518sm_r80 };
0037 
0038 /* Many GL518 constants specified below */
0039 
0040 /* The GL518 registers */
0041 #define GL518_REG_CHIP_ID   0x00
0042 #define GL518_REG_REVISION  0x01
0043 #define GL518_REG_VENDOR_ID 0x02
0044 #define GL518_REG_CONF      0x03
0045 #define GL518_REG_TEMP_IN   0x04
0046 #define GL518_REG_TEMP_MAX  0x05
0047 #define GL518_REG_TEMP_HYST 0x06
0048 #define GL518_REG_FAN_COUNT 0x07
0049 #define GL518_REG_FAN_LIMIT 0x08
0050 #define GL518_REG_VIN1_LIMIT    0x09
0051 #define GL518_REG_VIN2_LIMIT    0x0a
0052 #define GL518_REG_VIN3_LIMIT    0x0b
0053 #define GL518_REG_VDD_LIMIT 0x0c
0054 #define GL518_REG_VIN3      0x0d
0055 #define GL518_REG_MISC      0x0f
0056 #define GL518_REG_ALARM     0x10
0057 #define GL518_REG_MASK      0x11
0058 #define GL518_REG_INT       0x12
0059 #define GL518_REG_VIN2      0x13
0060 #define GL518_REG_VIN1      0x14
0061 #define GL518_REG_VDD       0x15
0062 
0063 
0064 /*
0065  * Conversions. Rounding and limit checking is only done on the TO_REG
0066  * variants. Note that you should be a bit careful with which arguments
0067  * these macros are called: arguments may be evaluated more than once.
0068  * Fixing this is just not worth it.
0069  */
0070 
0071 #define RAW_FROM_REG(val)   val
0072 
0073 #define BOOL_FROM_REG(val)  ((val) ? 0 : 1)
0074 #define BOOL_TO_REG(val)    ((val) ? 0 : 1)
0075 
0076 #define TEMP_CLAMP(val)     clamp_val(val, -119000, 136000)
0077 #define TEMP_TO_REG(val)    (DIV_ROUND_CLOSEST(TEMP_CLAMP(val), 1000) + 119)
0078 #define TEMP_FROM_REG(val)  (((val) - 119) * 1000)
0079 
0080 static inline u8 FAN_TO_REG(long rpm, int div)
0081 {
0082     long rpmdiv;
0083     if (rpm == 0)
0084         return 0;
0085     rpmdiv = clamp_val(rpm, 1, 960000) * div;
0086     return clamp_val((480000 + rpmdiv / 2) / rpmdiv, 1, 255);
0087 }
0088 #define FAN_FROM_REG(val, div)  ((val) == 0 ? 0 : (480000 / ((val) * (div))))
0089 
0090 #define IN_CLAMP(val)       clamp_val(val, 0, 255 * 19)
0091 #define IN_TO_REG(val)      DIV_ROUND_CLOSEST(IN_CLAMP(val), 19)
0092 #define IN_FROM_REG(val)    ((val) * 19)
0093 
0094 #define VDD_CLAMP(val)      clamp_val(val, 0, 255 * 95 / 4)
0095 #define VDD_TO_REG(val)     DIV_ROUND_CLOSEST(VDD_CLAMP(val) * 4, 95)
0096 #define VDD_FROM_REG(val)   DIV_ROUND_CLOSEST((val) * 95, 4)
0097 
0098 #define DIV_FROM_REG(val)   (1 << (val))
0099 
0100 #define BEEP_MASK_TO_REG(val)   ((val) & 0x7f & data->alarm_mask)
0101 #define BEEP_MASK_FROM_REG(val) ((val) & 0x7f)
0102 
0103 /* Each client has this additional data */
0104 struct gl518_data {
0105     struct i2c_client *client;
0106     const struct attribute_group *groups[3];
0107     enum chips type;
0108 
0109     struct mutex update_lock;
0110     bool valid;     /* true if following fields are valid */
0111     unsigned long last_updated; /* In jiffies */
0112 
0113     u8 voltage_in[4];   /* Register values; [0] = VDD */
0114     u8 voltage_min[4];  /* Register values; [0] = VDD */
0115     u8 voltage_max[4];  /* Register values; [0] = VDD */
0116     u8 fan_in[2];
0117     u8 fan_min[2];
0118     u8 fan_div[2];      /* Register encoding, shifted right */
0119     u8 fan_auto1;       /* Boolean */
0120     u8 temp_in;     /* Register values */
0121     u8 temp_max;        /* Register values */
0122     u8 temp_hyst;       /* Register values */
0123     u8 alarms;      /* Register value */
0124     u8 alarm_mask;
0125     u8 beep_mask;       /* Register value */
0126     u8 beep_enable;     /* Boolean */
0127 };
0128 
0129 /*
0130  * Registers 0x07 to 0x0c are word-sized, others are byte-sized
0131  * GL518 uses a high-byte first convention, which is exactly opposite to
0132  * the SMBus standard.
0133  */
0134 static int gl518_read_value(struct i2c_client *client, u8 reg)
0135 {
0136     if ((reg >= 0x07) && (reg <= 0x0c))
0137         return i2c_smbus_read_word_swapped(client, reg);
0138     else
0139         return i2c_smbus_read_byte_data(client, reg);
0140 }
0141 
0142 static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
0143 {
0144     if ((reg >= 0x07) && (reg <= 0x0c))
0145         return i2c_smbus_write_word_swapped(client, reg, value);
0146     else
0147         return i2c_smbus_write_byte_data(client, reg, value);
0148 }
0149 
0150 static struct gl518_data *gl518_update_device(struct device *dev)
0151 {
0152     struct gl518_data *data = dev_get_drvdata(dev);
0153     struct i2c_client *client = data->client;
0154     int val;
0155 
0156     mutex_lock(&data->update_lock);
0157 
0158     if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
0159         || !data->valid) {
0160         dev_dbg(&client->dev, "Starting gl518 update\n");
0161 
0162         data->alarms = gl518_read_value(client, GL518_REG_INT);
0163         data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
0164 
0165         val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
0166         data->voltage_min[0] = val & 0xff;
0167         data->voltage_max[0] = (val >> 8) & 0xff;
0168         val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
0169         data->voltage_min[1] = val & 0xff;
0170         data->voltage_max[1] = (val >> 8) & 0xff;
0171         val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
0172         data->voltage_min[2] = val & 0xff;
0173         data->voltage_max[2] = (val >> 8) & 0xff;
0174         val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
0175         data->voltage_min[3] = val & 0xff;
0176         data->voltage_max[3] = (val >> 8) & 0xff;
0177 
0178         val = gl518_read_value(client, GL518_REG_FAN_COUNT);
0179         data->fan_in[0] = (val >> 8) & 0xff;
0180         data->fan_in[1] = val & 0xff;
0181 
0182         val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
0183         data->fan_min[0] = (val >> 8) & 0xff;
0184         data->fan_min[1] = val & 0xff;
0185 
0186         data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
0187         data->temp_max =
0188             gl518_read_value(client, GL518_REG_TEMP_MAX);
0189         data->temp_hyst =
0190             gl518_read_value(client, GL518_REG_TEMP_HYST);
0191 
0192         val = gl518_read_value(client, GL518_REG_MISC);
0193         data->fan_div[0] = (val >> 6) & 0x03;
0194         data->fan_div[1] = (val >> 4) & 0x03;
0195         data->fan_auto1  = (val >> 3) & 0x01;
0196 
0197         data->alarms &= data->alarm_mask;
0198 
0199         val = gl518_read_value(client, GL518_REG_CONF);
0200         data->beep_enable = (val >> 2) & 1;
0201 
0202         if (data->type != gl518sm_r00) {
0203             data->voltage_in[0] =
0204                 gl518_read_value(client, GL518_REG_VDD);
0205             data->voltage_in[1] =
0206                 gl518_read_value(client, GL518_REG_VIN1);
0207             data->voltage_in[2] =
0208                 gl518_read_value(client, GL518_REG_VIN2);
0209         }
0210         data->voltage_in[3] =
0211             gl518_read_value(client, GL518_REG_VIN3);
0212 
0213         data->last_updated = jiffies;
0214         data->valid = true;
0215     }
0216 
0217     mutex_unlock(&data->update_lock);
0218 
0219     return data;
0220 }
0221 
0222 /*
0223  * Sysfs stuff
0224  */
0225 
0226 #define show(type, suffix, value)                   \
0227 static ssize_t show_##suffix(struct device *dev,            \
0228                  struct device_attribute *attr, char *buf)  \
0229 {                                   \
0230     struct gl518_data *data = gl518_update_device(dev);     \
0231     return sprintf(buf, "%d\n", type##_FROM_REG(data->value));  \
0232 }
0233 
0234 show(TEMP, temp_input1, temp_in);
0235 show(TEMP, temp_max1, temp_max);
0236 show(TEMP, temp_hyst1, temp_hyst);
0237 show(BOOL, fan_auto1, fan_auto1);
0238 show(VDD, in_input0, voltage_in[0]);
0239 show(IN, in_input1, voltage_in[1]);
0240 show(IN, in_input2, voltage_in[2]);
0241 show(IN, in_input3, voltage_in[3]);
0242 show(VDD, in_min0, voltage_min[0]);
0243 show(IN, in_min1, voltage_min[1]);
0244 show(IN, in_min2, voltage_min[2]);
0245 show(IN, in_min3, voltage_min[3]);
0246 show(VDD, in_max0, voltage_max[0]);
0247 show(IN, in_max1, voltage_max[1]);
0248 show(IN, in_max2, voltage_max[2]);
0249 show(IN, in_max3, voltage_max[3]);
0250 show(RAW, alarms, alarms);
0251 show(BOOL, beep_enable, beep_enable);
0252 show(BEEP_MASK, beep_mask, beep_mask);
0253 
0254 static ssize_t fan_input_show(struct device *dev,
0255                   struct device_attribute *attr, char *buf)
0256 {
0257     int nr = to_sensor_dev_attr(attr)->index;
0258     struct gl518_data *data = gl518_update_device(dev);
0259     return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_in[nr],
0260                     DIV_FROM_REG(data->fan_div[nr])));
0261 }
0262 
0263 static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
0264                 char *buf)
0265 {
0266     int nr = to_sensor_dev_attr(attr)->index;
0267     struct gl518_data *data = gl518_update_device(dev);
0268     return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
0269                     DIV_FROM_REG(data->fan_div[nr])));
0270 }
0271 
0272 static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
0273                 char *buf)
0274 {
0275     int nr = to_sensor_dev_attr(attr)->index;
0276     struct gl518_data *data = gl518_update_device(dev);
0277     return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
0278 }
0279 
0280 #define set(type, suffix, value, reg)                   \
0281 static ssize_t set_##suffix(struct device *dev,             \
0282                 struct device_attribute *attr,      \
0283                 const char *buf, size_t count)      \
0284 {                                   \
0285     struct gl518_data *data = dev_get_drvdata(dev);         \
0286     struct i2c_client *client = data->client;           \
0287     long val;                           \
0288     int err = kstrtol(buf, 10, &val);               \
0289     if (err)                            \
0290         return err;                     \
0291                                     \
0292     mutex_lock(&data->update_lock);                 \
0293     data->value = type##_TO_REG(val);               \
0294     gl518_write_value(client, reg, data->value);            \
0295     mutex_unlock(&data->update_lock);               \
0296     return count;                           \
0297 }
0298 
0299 #define set_bits(type, suffix, value, reg, mask, shift)         \
0300 static ssize_t set_##suffix(struct device *dev,             \
0301                 struct device_attribute *attr,      \
0302                 const char *buf, size_t count)      \
0303 {                                   \
0304     struct gl518_data *data = dev_get_drvdata(dev);         \
0305     struct i2c_client *client = data->client;           \
0306     int regvalue;                           \
0307     unsigned long val;                      \
0308     int err = kstrtoul(buf, 10, &val);              \
0309     if (err)                            \
0310         return err;                     \
0311                                     \
0312     mutex_lock(&data->update_lock);                 \
0313     regvalue = gl518_read_value(client, reg);           \
0314     data->value = type##_TO_REG(val);               \
0315     regvalue = (regvalue & ~mask) | (data->value << shift);     \
0316     gl518_write_value(client, reg, regvalue);           \
0317     mutex_unlock(&data->update_lock);               \
0318     return count;                           \
0319 }
0320 
0321 #define set_low(type, suffix, value, reg)               \
0322     set_bits(type, suffix, value, reg, 0x00ff, 0)
0323 #define set_high(type, suffix, value, reg)              \
0324     set_bits(type, suffix, value, reg, 0xff00, 8)
0325 
0326 set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
0327 set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
0328 set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
0329 set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
0330 set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
0331 set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
0332 set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
0333 set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
0334 set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
0335 set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
0336 set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
0337 set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
0338 set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
0339 
0340 static ssize_t fan_min_store(struct device *dev,
0341                  struct device_attribute *attr, const char *buf,
0342                  size_t count)
0343 {
0344     struct gl518_data *data = dev_get_drvdata(dev);
0345     struct i2c_client *client = data->client;
0346     int nr = to_sensor_dev_attr(attr)->index;
0347     int regvalue;
0348     unsigned long val;
0349     int err;
0350 
0351     err = kstrtoul(buf, 10, &val);
0352     if (err)
0353         return err;
0354 
0355     mutex_lock(&data->update_lock);
0356     regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
0357     data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
0358     regvalue = (regvalue & (0xff << (8 * nr)))
0359          | (data->fan_min[nr] << (8 * (1 - nr)));
0360     gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
0361 
0362     data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
0363     if (data->fan_min[nr] == 0)
0364         data->alarm_mask &= ~(0x20 << nr);
0365     else
0366         data->alarm_mask |= (0x20 << nr);
0367     data->beep_mask &= data->alarm_mask;
0368     gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
0369 
0370     mutex_unlock(&data->update_lock);
0371     return count;
0372 }
0373 
0374 static ssize_t fan_div_store(struct device *dev,
0375                  struct device_attribute *attr, const char *buf,
0376                  size_t count)
0377 {
0378     struct gl518_data *data = dev_get_drvdata(dev);
0379     struct i2c_client *client = data->client;
0380     int nr = to_sensor_dev_attr(attr)->index;
0381     int regvalue;
0382     unsigned long val;
0383     int err;
0384 
0385     err = kstrtoul(buf, 10, &val);
0386     if (err)
0387         return err;
0388 
0389     switch (val) {
0390     case 1:
0391         val = 0;
0392         break;
0393     case 2:
0394         val = 1;
0395         break;
0396     case 4:
0397         val = 2;
0398         break;
0399     case 8:
0400         val = 3;
0401         break;
0402     default:
0403         dev_err(dev,
0404             "Invalid fan clock divider %lu, choose one of 1, 2, 4 or 8\n",
0405             val);
0406         return -EINVAL;
0407     }
0408 
0409     mutex_lock(&data->update_lock);
0410     regvalue = gl518_read_value(client, GL518_REG_MISC);
0411     data->fan_div[nr] = val;
0412     regvalue = (regvalue & ~(0xc0 >> (2 * nr)))
0413          | (data->fan_div[nr] << (6 - 2 * nr));
0414     gl518_write_value(client, GL518_REG_MISC, regvalue);
0415     mutex_unlock(&data->update_lock);
0416     return count;
0417 }
0418 
0419 static DEVICE_ATTR(temp1_input, 0444, show_temp_input1, NULL);
0420 static DEVICE_ATTR(temp1_max, 0644, show_temp_max1, set_temp_max1);
0421 static DEVICE_ATTR(temp1_max_hyst, 0644,
0422            show_temp_hyst1, set_temp_hyst1);
0423 static DEVICE_ATTR(fan1_auto, 0644, show_fan_auto1, set_fan_auto1);
0424 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
0425 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
0426 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
0427 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
0428 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
0429 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
0430 static DEVICE_ATTR(in0_input, 0444, show_in_input0, NULL);
0431 static DEVICE_ATTR(in1_input, 0444, show_in_input1, NULL);
0432 static DEVICE_ATTR(in2_input, 0444, show_in_input2, NULL);
0433 static DEVICE_ATTR(in3_input, 0444, show_in_input3, NULL);
0434 static DEVICE_ATTR(in0_min, 0644, show_in_min0, set_in_min0);
0435 static DEVICE_ATTR(in1_min, 0644, show_in_min1, set_in_min1);
0436 static DEVICE_ATTR(in2_min, 0644, show_in_min2, set_in_min2);
0437 static DEVICE_ATTR(in3_min, 0644, show_in_min3, set_in_min3);
0438 static DEVICE_ATTR(in0_max, 0644, show_in_max0, set_in_max0);
0439 static DEVICE_ATTR(in1_max, 0644, show_in_max1, set_in_max1);
0440 static DEVICE_ATTR(in2_max, 0644, show_in_max2, set_in_max2);
0441 static DEVICE_ATTR(in3_max, 0644, show_in_max3, set_in_max3);
0442 static DEVICE_ATTR(alarms, 0444, show_alarms, NULL);
0443 static DEVICE_ATTR(beep_enable, 0644,
0444            show_beep_enable, set_beep_enable);
0445 static DEVICE_ATTR(beep_mask, 0644,
0446            show_beep_mask, set_beep_mask);
0447 
0448 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
0449               char *buf)
0450 {
0451     int bitnr = to_sensor_dev_attr(attr)->index;
0452     struct gl518_data *data = gl518_update_device(dev);
0453     return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
0454 }
0455 
0456 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
0457 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
0458 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
0459 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
0460 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
0461 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 5);
0462 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 6);
0463 
0464 static ssize_t beep_show(struct device *dev, struct device_attribute *attr,
0465              char *buf)
0466 {
0467     int bitnr = to_sensor_dev_attr(attr)->index;
0468     struct gl518_data *data = gl518_update_device(dev);
0469     return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
0470 }
0471 
0472 static ssize_t beep_store(struct device *dev, struct device_attribute *attr,
0473               const char *buf, size_t count)
0474 {
0475     struct gl518_data *data = dev_get_drvdata(dev);
0476     struct i2c_client *client = data->client;
0477     int bitnr = to_sensor_dev_attr(attr)->index;
0478     unsigned long bit;
0479     int err;
0480 
0481     err = kstrtoul(buf, 10, &bit);
0482     if (err)
0483         return err;
0484 
0485     if (bit & ~1)
0486         return -EINVAL;
0487 
0488     mutex_lock(&data->update_lock);
0489     data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
0490     if (bit)
0491         data->beep_mask |= (1 << bitnr);
0492     else
0493         data->beep_mask &= ~(1 << bitnr);
0494     gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
0495     mutex_unlock(&data->update_lock);
0496     return count;
0497 }
0498 
0499 static SENSOR_DEVICE_ATTR_RW(in0_beep, beep, 0);
0500 static SENSOR_DEVICE_ATTR_RW(in1_beep, beep, 1);
0501 static SENSOR_DEVICE_ATTR_RW(in2_beep, beep, 2);
0502 static SENSOR_DEVICE_ATTR_RW(in3_beep, beep, 3);
0503 static SENSOR_DEVICE_ATTR_RW(temp1_beep, beep, 4);
0504 static SENSOR_DEVICE_ATTR_RW(fan1_beep, beep, 5);
0505 static SENSOR_DEVICE_ATTR_RW(fan2_beep, beep, 6);
0506 
0507 static struct attribute *gl518_attributes[] = {
0508     &dev_attr_in3_input.attr,
0509     &dev_attr_in0_min.attr,
0510     &dev_attr_in1_min.attr,
0511     &dev_attr_in2_min.attr,
0512     &dev_attr_in3_min.attr,
0513     &dev_attr_in0_max.attr,
0514     &dev_attr_in1_max.attr,
0515     &dev_attr_in2_max.attr,
0516     &dev_attr_in3_max.attr,
0517     &sensor_dev_attr_in0_alarm.dev_attr.attr,
0518     &sensor_dev_attr_in1_alarm.dev_attr.attr,
0519     &sensor_dev_attr_in2_alarm.dev_attr.attr,
0520     &sensor_dev_attr_in3_alarm.dev_attr.attr,
0521     &sensor_dev_attr_in0_beep.dev_attr.attr,
0522     &sensor_dev_attr_in1_beep.dev_attr.attr,
0523     &sensor_dev_attr_in2_beep.dev_attr.attr,
0524     &sensor_dev_attr_in3_beep.dev_attr.attr,
0525 
0526     &dev_attr_fan1_auto.attr,
0527     &sensor_dev_attr_fan1_input.dev_attr.attr,
0528     &sensor_dev_attr_fan2_input.dev_attr.attr,
0529     &sensor_dev_attr_fan1_min.dev_attr.attr,
0530     &sensor_dev_attr_fan2_min.dev_attr.attr,
0531     &sensor_dev_attr_fan1_div.dev_attr.attr,
0532     &sensor_dev_attr_fan2_div.dev_attr.attr,
0533     &sensor_dev_attr_fan1_alarm.dev_attr.attr,
0534     &sensor_dev_attr_fan2_alarm.dev_attr.attr,
0535     &sensor_dev_attr_fan1_beep.dev_attr.attr,
0536     &sensor_dev_attr_fan2_beep.dev_attr.attr,
0537 
0538     &dev_attr_temp1_input.attr,
0539     &dev_attr_temp1_max.attr,
0540     &dev_attr_temp1_max_hyst.attr,
0541     &sensor_dev_attr_temp1_alarm.dev_attr.attr,
0542     &sensor_dev_attr_temp1_beep.dev_attr.attr,
0543 
0544     &dev_attr_alarms.attr,
0545     &dev_attr_beep_enable.attr,
0546     &dev_attr_beep_mask.attr,
0547     NULL
0548 };
0549 
0550 static const struct attribute_group gl518_group = {
0551     .attrs = gl518_attributes,
0552 };
0553 
0554 static struct attribute *gl518_attributes_r80[] = {
0555     &dev_attr_in0_input.attr,
0556     &dev_attr_in1_input.attr,
0557     &dev_attr_in2_input.attr,
0558     NULL
0559 };
0560 
0561 static const struct attribute_group gl518_group_r80 = {
0562     .attrs = gl518_attributes_r80,
0563 };
0564 
0565 /*
0566  * Real code
0567  */
0568 
0569 /* Return 0 if detection is successful, -ENODEV otherwise */
0570 static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info)
0571 {
0572     struct i2c_adapter *adapter = client->adapter;
0573     int rev;
0574 
0575     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
0576                      I2C_FUNC_SMBUS_WORD_DATA))
0577         return -ENODEV;
0578 
0579     /* Now, we do the remaining detection. */
0580     if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80)
0581      || (gl518_read_value(client, GL518_REG_CONF) & 0x80))
0582         return -ENODEV;
0583 
0584     /* Determine the chip type. */
0585     rev = gl518_read_value(client, GL518_REG_REVISION);
0586     if (rev != 0x00 && rev != 0x80)
0587         return -ENODEV;
0588 
0589     strlcpy(info->type, "gl518sm", I2C_NAME_SIZE);
0590 
0591     return 0;
0592 }
0593 
0594 /*
0595  * Called when we have found a new GL518SM.
0596  * Note that we preserve D4:NoFan2 and D2:beep_enable.
0597  */
0598 static void gl518_init_client(struct i2c_client *client)
0599 {
0600     /* Make sure we leave D7:Reset untouched */
0601     u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
0602 
0603     /* Comparator mode (D3=0), standby mode (D6=0) */
0604     gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
0605 
0606     /* Never interrupts */
0607     gl518_write_value(client, GL518_REG_MASK, 0x00);
0608 
0609     /* Clear status register (D5=1), start (D6=1) */
0610     gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
0611     gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
0612 }
0613 
0614 static int gl518_probe(struct i2c_client *client)
0615 {
0616     struct device *dev = &client->dev;
0617     struct device *hwmon_dev;
0618     struct gl518_data *data;
0619     int revision;
0620 
0621     data = devm_kzalloc(dev, sizeof(struct gl518_data), GFP_KERNEL);
0622     if (!data)
0623         return -ENOMEM;
0624 
0625     data->client = client;
0626     revision = gl518_read_value(client, GL518_REG_REVISION);
0627     data->type = revision == 0x80 ? gl518sm_r80 : gl518sm_r00;
0628     mutex_init(&data->update_lock);
0629 
0630     /* Initialize the GL518SM chip */
0631     data->alarm_mask = 0xff;
0632     gl518_init_client(client);
0633 
0634     /* sysfs hooks */
0635     data->groups[0] = &gl518_group;
0636     if (data->type == gl518sm_r80)
0637         data->groups[1] = &gl518_group_r80;
0638 
0639     hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
0640                                data, data->groups);
0641     return PTR_ERR_OR_ZERO(hwmon_dev);
0642 }
0643 
0644 static const struct i2c_device_id gl518_id[] = {
0645     { "gl518sm", 0 },
0646     { }
0647 };
0648 MODULE_DEVICE_TABLE(i2c, gl518_id);
0649 
0650 static struct i2c_driver gl518_driver = {
0651     .class      = I2C_CLASS_HWMON,
0652     .driver = {
0653         .name   = "gl518sm",
0654     },
0655     .probe_new  = gl518_probe,
0656     .id_table   = gl518_id,
0657     .detect     = gl518_detect,
0658     .address_list   = normal_i2c,
0659 };
0660 
0661 module_i2c_driver(gl518_driver);
0662 
0663 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
0664     "Kyosti Malkki <kmalkki@cc.hut.fi> and "
0665     "Hong-Gunn Chew <hglinux@gunnet.org>");
0666 MODULE_DESCRIPTION("GL518SM driver");
0667 MODULE_LICENSE("GPL");