Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * gl520sm.c - Part of lm_sensors, Linux kernel modules for hardware
0004  *         monitoring
0005  * Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>,
0006  *               Kyösti Mälkki <kmalkki@cc.hut.fi>
0007  * Copyright (c) 2005   Maarten Deprez <maartendeprez@users.sourceforge.net>
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/init.h>
0012 #include <linux/slab.h>
0013 #include <linux/jiffies.h>
0014 #include <linux/i2c.h>
0015 #include <linux/hwmon.h>
0016 #include <linux/hwmon-sysfs.h>
0017 #include <linux/hwmon-vid.h>
0018 #include <linux/err.h>
0019 #include <linux/mutex.h>
0020 #include <linux/sysfs.h>
0021 
0022 /* Type of the extra sensor */
0023 static unsigned short extra_sensor_type;
0024 module_param(extra_sensor_type, ushort, 0);
0025 MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)");
0026 
0027 /* Addresses to scan */
0028 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
0029 
0030 /*
0031  * Many GL520 constants specified below
0032  * One of the inputs can be configured as either temp or voltage.
0033  * That's why _TEMP2 and _IN4 access the same register
0034  */
0035 
0036 /* The GL520 registers */
0037 #define GL520_REG_CHIP_ID       0x00
0038 #define GL520_REG_REVISION      0x01
0039 #define GL520_REG_CONF          0x03
0040 #define GL520_REG_MASK          0x11
0041 
0042 #define GL520_REG_VID_INPUT     0x02
0043 
0044 static const u8 GL520_REG_IN_INPUT[]    = { 0x15, 0x14, 0x13, 0x0d, 0x0e };
0045 static const u8 GL520_REG_IN_LIMIT[]    = { 0x0c, 0x09, 0x0a, 0x0b };
0046 static const u8 GL520_REG_IN_MIN[]  = { 0x0c, 0x09, 0x0a, 0x0b, 0x18 };
0047 static const u8 GL520_REG_IN_MAX[]  = { 0x0c, 0x09, 0x0a, 0x0b, 0x17 };
0048 
0049 static const u8 GL520_REG_TEMP_INPUT[]      = { 0x04, 0x0e };
0050 static const u8 GL520_REG_TEMP_MAX[]        = { 0x05, 0x17 };
0051 static const u8 GL520_REG_TEMP_MAX_HYST[]   = { 0x06, 0x18 };
0052 
0053 #define GL520_REG_FAN_INPUT     0x07
0054 #define GL520_REG_FAN_MIN       0x08
0055 #define GL520_REG_FAN_DIV       0x0f
0056 #define GL520_REG_FAN_OFF       GL520_REG_FAN_DIV
0057 
0058 #define GL520_REG_ALARMS        0x12
0059 #define GL520_REG_BEEP_MASK     0x10
0060 #define GL520_REG_BEEP_ENABLE       GL520_REG_CONF
0061 
0062 /* Client data */
0063 struct gl520_data {
0064     struct i2c_client *client;
0065     const struct attribute_group *groups[3];
0066     struct mutex update_lock;
0067     bool valid;     /* false until the following fields are valid */
0068     unsigned long last_updated; /* in jiffies */
0069 
0070     u8 vid;
0071     u8 vrm;
0072     u8 in_input[5];     /* [0] = VVD */
0073     u8 in_min[5];       /* [0] = VDD */
0074     u8 in_max[5];       /* [0] = VDD */
0075     u8 fan_input[2];
0076     u8 fan_min[2];
0077     u8 fan_div[2];
0078     u8 fan_off;
0079     u8 temp_input[2];
0080     u8 temp_max[2];
0081     u8 temp_max_hyst[2];
0082     u8 alarms;
0083     u8 beep_enable;
0084     u8 beep_mask;
0085     u8 alarm_mask;
0086     u8 two_temps;
0087 };
0088 
0089 /*
0090  * Registers 0x07 to 0x0c are word-sized, others are byte-sized
0091  * GL520 uses a high-byte first convention
0092  */
0093 static int gl520_read_value(struct i2c_client *client, u8 reg)
0094 {
0095     if ((reg >= 0x07) && (reg <= 0x0c))
0096         return i2c_smbus_read_word_swapped(client, reg);
0097     else
0098         return i2c_smbus_read_byte_data(client, reg);
0099 }
0100 
0101 static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value)
0102 {
0103     if ((reg >= 0x07) && (reg <= 0x0c))
0104         return i2c_smbus_write_word_swapped(client, reg, value);
0105     else
0106         return i2c_smbus_write_byte_data(client, reg, value);
0107 }
0108 
0109 static struct gl520_data *gl520_update_device(struct device *dev)
0110 {
0111     struct gl520_data *data = dev_get_drvdata(dev);
0112     struct i2c_client *client = data->client;
0113     int val, i;
0114 
0115     mutex_lock(&data->update_lock);
0116 
0117     if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
0118 
0119         dev_dbg(&client->dev, "Starting gl520sm update\n");
0120 
0121         data->alarms = gl520_read_value(client, GL520_REG_ALARMS);
0122         data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
0123         data->vid = gl520_read_value(client,
0124                          GL520_REG_VID_INPUT) & 0x1f;
0125 
0126         for (i = 0; i < 4; i++) {
0127             data->in_input[i] = gl520_read_value(client,
0128                             GL520_REG_IN_INPUT[i]);
0129             val = gl520_read_value(client, GL520_REG_IN_LIMIT[i]);
0130             data->in_min[i] = val & 0xff;
0131             data->in_max[i] = (val >> 8) & 0xff;
0132         }
0133 
0134         val = gl520_read_value(client, GL520_REG_FAN_INPUT);
0135         data->fan_input[0] = (val >> 8) & 0xff;
0136         data->fan_input[1] = val & 0xff;
0137 
0138         val = gl520_read_value(client, GL520_REG_FAN_MIN);
0139         data->fan_min[0] = (val >> 8) & 0xff;
0140         data->fan_min[1] = val & 0xff;
0141 
0142         data->temp_input[0] = gl520_read_value(client,
0143                         GL520_REG_TEMP_INPUT[0]);
0144         data->temp_max[0] = gl520_read_value(client,
0145                         GL520_REG_TEMP_MAX[0]);
0146         data->temp_max_hyst[0] = gl520_read_value(client,
0147                         GL520_REG_TEMP_MAX_HYST[0]);
0148 
0149         val = gl520_read_value(client, GL520_REG_FAN_DIV);
0150         data->fan_div[0] = (val >> 6) & 0x03;
0151         data->fan_div[1] = (val >> 4) & 0x03;
0152         data->fan_off = (val >> 2) & 0x01;
0153 
0154         data->alarms &= data->alarm_mask;
0155 
0156         val = gl520_read_value(client, GL520_REG_CONF);
0157         data->beep_enable = !((val >> 2) & 1);
0158 
0159         /* Temp1 and Vin4 are the same input */
0160         if (data->two_temps) {
0161             data->temp_input[1] = gl520_read_value(client,
0162                         GL520_REG_TEMP_INPUT[1]);
0163             data->temp_max[1] = gl520_read_value(client,
0164                         GL520_REG_TEMP_MAX[1]);
0165             data->temp_max_hyst[1] = gl520_read_value(client,
0166                         GL520_REG_TEMP_MAX_HYST[1]);
0167         } else {
0168             data->in_input[4] = gl520_read_value(client,
0169                         GL520_REG_IN_INPUT[4]);
0170             data->in_min[4] = gl520_read_value(client,
0171                         GL520_REG_IN_MIN[4]);
0172             data->in_max[4] = gl520_read_value(client,
0173                         GL520_REG_IN_MAX[4]);
0174         }
0175 
0176         data->last_updated = jiffies;
0177         data->valid = true;
0178     }
0179 
0180     mutex_unlock(&data->update_lock);
0181 
0182     return data;
0183 }
0184 
0185 /*
0186  * Sysfs stuff
0187  */
0188 
0189 static ssize_t cpu0_vid_show(struct device *dev,
0190                  struct device_attribute *attr, char *buf)
0191 {
0192     struct gl520_data *data = gl520_update_device(dev);
0193     return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
0194 }
0195 static DEVICE_ATTR_RO(cpu0_vid);
0196 
0197 #define VDD_FROM_REG(val)   DIV_ROUND_CLOSEST((val) * 95, 4)
0198 #define VDD_CLAMP(val)      clamp_val(val, 0, 255 * 95 / 4)
0199 #define VDD_TO_REG(val)     DIV_ROUND_CLOSEST(VDD_CLAMP(val) * 4, 95)
0200 
0201 #define IN_FROM_REG(val)    ((val) * 19)
0202 #define IN_CLAMP(val)       clamp_val(val, 0, 255 * 19)
0203 #define IN_TO_REG(val)      DIV_ROUND_CLOSEST(IN_CLAMP(val), 19)
0204 
0205 static ssize_t in_input_show(struct device *dev,
0206                  struct device_attribute *attr, char *buf)
0207 {
0208     int n = to_sensor_dev_attr(attr)->index;
0209     struct gl520_data *data = gl520_update_device(dev);
0210     u8 r = data->in_input[n];
0211 
0212     if (n == 0)
0213         return sprintf(buf, "%d\n", VDD_FROM_REG(r));
0214     else
0215         return sprintf(buf, "%d\n", IN_FROM_REG(r));
0216 }
0217 
0218 static ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
0219                char *buf)
0220 {
0221     int n = to_sensor_dev_attr(attr)->index;
0222     struct gl520_data *data = gl520_update_device(dev);
0223     u8 r = data->in_min[n];
0224 
0225     if (n == 0)
0226         return sprintf(buf, "%d\n", VDD_FROM_REG(r));
0227     else
0228         return sprintf(buf, "%d\n", IN_FROM_REG(r));
0229 }
0230 
0231 static ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
0232                char *buf)
0233 {
0234     int n = to_sensor_dev_attr(attr)->index;
0235     struct gl520_data *data = gl520_update_device(dev);
0236     u8 r = data->in_max[n];
0237 
0238     if (n == 0)
0239         return sprintf(buf, "%d\n", VDD_FROM_REG(r));
0240     else
0241         return sprintf(buf, "%d\n", IN_FROM_REG(r));
0242 }
0243 
0244 static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
0245                 const char *buf, size_t count)
0246 {
0247     struct gl520_data *data = dev_get_drvdata(dev);
0248     struct i2c_client *client = data->client;
0249     int n = to_sensor_dev_attr(attr)->index;
0250     u8 r;
0251     long v;
0252     int err;
0253 
0254     err = kstrtol(buf, 10, &v);
0255     if (err)
0256         return err;
0257 
0258     mutex_lock(&data->update_lock);
0259 
0260     if (n == 0)
0261         r = VDD_TO_REG(v);
0262     else
0263         r = IN_TO_REG(v);
0264 
0265     data->in_min[n] = r;
0266 
0267     if (n < 4)
0268         gl520_write_value(client, GL520_REG_IN_MIN[n],
0269                   (gl520_read_value(client, GL520_REG_IN_MIN[n])
0270                    & ~0xff) | r);
0271     else
0272         gl520_write_value(client, GL520_REG_IN_MIN[n], r);
0273 
0274     mutex_unlock(&data->update_lock);
0275     return count;
0276 }
0277 
0278 static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
0279                 const char *buf, size_t count)
0280 {
0281     struct gl520_data *data = dev_get_drvdata(dev);
0282     struct i2c_client *client = data->client;
0283     int n = to_sensor_dev_attr(attr)->index;
0284     u8 r;
0285     long v;
0286     int err;
0287 
0288     err = kstrtol(buf, 10, &v);
0289     if (err)
0290         return err;
0291 
0292     if (n == 0)
0293         r = VDD_TO_REG(v);
0294     else
0295         r = IN_TO_REG(v);
0296 
0297     mutex_lock(&data->update_lock);
0298 
0299     data->in_max[n] = r;
0300 
0301     if (n < 4)
0302         gl520_write_value(client, GL520_REG_IN_MAX[n],
0303                   (gl520_read_value(client, GL520_REG_IN_MAX[n])
0304                    & ~0xff00) | (r << 8));
0305     else
0306         gl520_write_value(client, GL520_REG_IN_MAX[n], r);
0307 
0308     mutex_unlock(&data->update_lock);
0309     return count;
0310 }
0311 
0312 static SENSOR_DEVICE_ATTR_RO(in0_input, in_input, 0);
0313 static SENSOR_DEVICE_ATTR_RO(in1_input, in_input, 1);
0314 static SENSOR_DEVICE_ATTR_RO(in2_input, in_input, 2);
0315 static SENSOR_DEVICE_ATTR_RO(in3_input, in_input, 3);
0316 static SENSOR_DEVICE_ATTR_RO(in4_input, in_input, 4);
0317 static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
0318 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
0319 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
0320 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
0321 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
0322 static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
0323 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
0324 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
0325 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
0326 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
0327 
0328 #define DIV_FROM_REG(val) (1 << (val))
0329 #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : (480000 / ((val) << (div))))
0330 
0331 #define FAN_BASE(div)       (480000 >> (div))
0332 #define FAN_CLAMP(val, div) clamp_val(val, FAN_BASE(div) / 255, \
0333                       FAN_BASE(div))
0334 #define FAN_TO_REG(val, div)    ((val) == 0 ? 0 : \
0335                  DIV_ROUND_CLOSEST(480000, \
0336                         FAN_CLAMP(val, div) << (div)))
0337 
0338 static ssize_t fan_input_show(struct device *dev,
0339                   struct device_attribute *attr, char *buf)
0340 {
0341     int n = to_sensor_dev_attr(attr)->index;
0342     struct gl520_data *data = gl520_update_device(dev);
0343 
0344     return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n],
0345                          data->fan_div[n]));
0346 }
0347 
0348 static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
0349                 char *buf)
0350 {
0351     int n = to_sensor_dev_attr(attr)->index;
0352     struct gl520_data *data = gl520_update_device(dev);
0353 
0354     return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n],
0355                          data->fan_div[n]));
0356 }
0357 
0358 static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
0359                 char *buf)
0360 {
0361     int n = to_sensor_dev_attr(attr)->index;
0362     struct gl520_data *data = gl520_update_device(dev);
0363 
0364     return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n]));
0365 }
0366 
0367 static ssize_t fan1_off_show(struct device *dev,
0368                  struct device_attribute *attr, char *buf)
0369 {
0370     struct gl520_data *data = gl520_update_device(dev);
0371     return sprintf(buf, "%d\n", data->fan_off);
0372 }
0373 
0374 static ssize_t fan_min_store(struct device *dev,
0375                  struct device_attribute *attr, const char *buf,
0376                  size_t count)
0377 {
0378     struct gl520_data *data = dev_get_drvdata(dev);
0379     struct i2c_client *client = data->client;
0380     int n = to_sensor_dev_attr(attr)->index;
0381     u8 r;
0382     unsigned long v;
0383     int err;
0384 
0385     err = kstrtoul(buf, 10, &v);
0386     if (err)
0387         return err;
0388 
0389     mutex_lock(&data->update_lock);
0390     r = FAN_TO_REG(v, data->fan_div[n]);
0391     data->fan_min[n] = r;
0392 
0393     if (n == 0)
0394         gl520_write_value(client, GL520_REG_FAN_MIN,
0395                   (gl520_read_value(client, GL520_REG_FAN_MIN)
0396                    & ~0xff00) | (r << 8));
0397     else
0398         gl520_write_value(client, GL520_REG_FAN_MIN,
0399                   (gl520_read_value(client, GL520_REG_FAN_MIN)
0400                    & ~0xff) | r);
0401 
0402     data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
0403     if (data->fan_min[n] == 0)
0404         data->alarm_mask &= (n == 0) ? ~0x20 : ~0x40;
0405     else
0406         data->alarm_mask |= (n == 0) ? 0x20 : 0x40;
0407     data->beep_mask &= data->alarm_mask;
0408     gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
0409 
0410     mutex_unlock(&data->update_lock);
0411     return count;
0412 }
0413 
0414 static ssize_t fan_div_store(struct device *dev,
0415                  struct device_attribute *attr, const char *buf,
0416                  size_t count)
0417 {
0418     struct gl520_data *data = dev_get_drvdata(dev);
0419     struct i2c_client *client = data->client;
0420     int n = to_sensor_dev_attr(attr)->index;
0421     u8 r;
0422     unsigned long v;
0423     int err;
0424 
0425     err = kstrtoul(buf, 10, &v);
0426     if (err)
0427         return err;
0428 
0429     switch (v) {
0430     case 1:
0431         r = 0;
0432         break;
0433     case 2:
0434         r = 1;
0435         break;
0436     case 4:
0437         r = 2;
0438         break;
0439     case 8:
0440         r = 3;
0441         break;
0442     default:
0443         dev_err(&client->dev,
0444     "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v);
0445         return -EINVAL;
0446     }
0447 
0448     mutex_lock(&data->update_lock);
0449     data->fan_div[n] = r;
0450 
0451     if (n == 0)
0452         gl520_write_value(client, GL520_REG_FAN_DIV,
0453                   (gl520_read_value(client, GL520_REG_FAN_DIV)
0454                    & ~0xc0) | (r << 6));
0455     else
0456         gl520_write_value(client, GL520_REG_FAN_DIV,
0457                   (gl520_read_value(client, GL520_REG_FAN_DIV)
0458                    & ~0x30) | (r << 4));
0459 
0460     mutex_unlock(&data->update_lock);
0461     return count;
0462 }
0463 
0464 static ssize_t fan1_off_store(struct device *dev,
0465                   struct device_attribute *attr, const char *buf,
0466                   size_t count)
0467 {
0468     struct gl520_data *data = dev_get_drvdata(dev);
0469     struct i2c_client *client = data->client;
0470     u8 r;
0471     unsigned long v;
0472     int err;
0473 
0474     err = kstrtoul(buf, 10, &v);
0475     if (err)
0476         return err;
0477 
0478     r = (v ? 1 : 0);
0479 
0480     mutex_lock(&data->update_lock);
0481     data->fan_off = r;
0482     gl520_write_value(client, GL520_REG_FAN_OFF,
0483               (gl520_read_value(client, GL520_REG_FAN_OFF)
0484                & ~0x0c) | (r << 2));
0485     mutex_unlock(&data->update_lock);
0486     return count;
0487 }
0488 
0489 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
0490 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
0491 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
0492 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
0493 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
0494 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
0495 static DEVICE_ATTR_RW(fan1_off);
0496 
0497 #define TEMP_FROM_REG(val)  (((val) - 130) * 1000)
0498 #define TEMP_CLAMP(val)     clamp_val(val, -130000, 125000)
0499 #define TEMP_TO_REG(val)    (DIV_ROUND_CLOSEST(TEMP_CLAMP(val), 1000) + 130)
0500 
0501 static ssize_t temp_input_show(struct device *dev,
0502                    struct device_attribute *attr, char *buf)
0503 {
0504     int n = to_sensor_dev_attr(attr)->index;
0505     struct gl520_data *data = gl520_update_device(dev);
0506 
0507     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n]));
0508 }
0509 
0510 static ssize_t temp_max_show(struct device *dev,
0511                  struct device_attribute *attr, char *buf)
0512 {
0513     int n = to_sensor_dev_attr(attr)->index;
0514     struct gl520_data *data = gl520_update_device(dev);
0515 
0516     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n]));
0517 }
0518 
0519 static ssize_t temp_max_hyst_show(struct device *dev,
0520                   struct device_attribute *attr, char *buf)
0521 {
0522     int n = to_sensor_dev_attr(attr)->index;
0523     struct gl520_data *data = gl520_update_device(dev);
0524 
0525     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n]));
0526 }
0527 
0528 static ssize_t temp_max_store(struct device *dev,
0529                   struct device_attribute *attr, const char *buf,
0530                   size_t count)
0531 {
0532     struct gl520_data *data = dev_get_drvdata(dev);
0533     struct i2c_client *client = data->client;
0534     int n = to_sensor_dev_attr(attr)->index;
0535     long v;
0536     int err;
0537 
0538     err = kstrtol(buf, 10, &v);
0539     if (err)
0540         return err;
0541 
0542     mutex_lock(&data->update_lock);
0543     data->temp_max[n] = TEMP_TO_REG(v);
0544     gl520_write_value(client, GL520_REG_TEMP_MAX[n], data->temp_max[n]);
0545     mutex_unlock(&data->update_lock);
0546     return count;
0547 }
0548 
0549 static ssize_t temp_max_hyst_store(struct device *dev,
0550                    struct device_attribute *attr,
0551                    const char *buf, size_t count)
0552 {
0553     struct gl520_data *data = dev_get_drvdata(dev);
0554     struct i2c_client *client = data->client;
0555     int n = to_sensor_dev_attr(attr)->index;
0556     long v;
0557     int err;
0558 
0559     err = kstrtol(buf, 10, &v);
0560     if (err)
0561         return err;
0562 
0563     mutex_lock(&data->update_lock);
0564     data->temp_max_hyst[n] = TEMP_TO_REG(v);
0565     gl520_write_value(client, GL520_REG_TEMP_MAX_HYST[n],
0566               data->temp_max_hyst[n]);
0567     mutex_unlock(&data->update_lock);
0568     return count;
0569 }
0570 
0571 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
0572 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1);
0573 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
0574 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
0575 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp_max_hyst, 0);
0576 static SENSOR_DEVICE_ATTR_RW(temp2_max_hyst, temp_max_hyst, 1);
0577 
0578 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
0579                char *buf)
0580 {
0581     struct gl520_data *data = gl520_update_device(dev);
0582     return sprintf(buf, "%d\n", data->alarms);
0583 }
0584 
0585 static ssize_t beep_enable_show(struct device *dev,
0586                 struct device_attribute *attr, char *buf)
0587 {
0588     struct gl520_data *data = gl520_update_device(dev);
0589     return sprintf(buf, "%d\n", data->beep_enable);
0590 }
0591 
0592 static ssize_t beep_mask_show(struct device *dev,
0593                   struct device_attribute *attr, char *buf)
0594 {
0595     struct gl520_data *data = gl520_update_device(dev);
0596     return sprintf(buf, "%d\n", data->beep_mask);
0597 }
0598 
0599 static ssize_t beep_enable_store(struct device *dev,
0600                  struct device_attribute *attr,
0601                  const char *buf, size_t count)
0602 {
0603     struct gl520_data *data = dev_get_drvdata(dev);
0604     struct i2c_client *client = data->client;
0605     u8 r;
0606     unsigned long v;
0607     int err;
0608 
0609     err = kstrtoul(buf, 10, &v);
0610     if (err)
0611         return err;
0612 
0613     r = (v ? 0 : 1);
0614 
0615     mutex_lock(&data->update_lock);
0616     data->beep_enable = !r;
0617     gl520_write_value(client, GL520_REG_BEEP_ENABLE,
0618               (gl520_read_value(client, GL520_REG_BEEP_ENABLE)
0619                & ~0x04) | (r << 2));
0620     mutex_unlock(&data->update_lock);
0621     return count;
0622 }
0623 
0624 static ssize_t beep_mask_store(struct device *dev,
0625                    struct device_attribute *attr, const char *buf,
0626                    size_t count)
0627 {
0628     struct gl520_data *data = dev_get_drvdata(dev);
0629     struct i2c_client *client = data->client;
0630     unsigned long r;
0631     int err;
0632 
0633     err = kstrtoul(buf, 10, &r);
0634     if (err)
0635         return err;
0636 
0637     mutex_lock(&data->update_lock);
0638     r &= data->alarm_mask;
0639     data->beep_mask = r;
0640     gl520_write_value(client, GL520_REG_BEEP_MASK, r);
0641     mutex_unlock(&data->update_lock);
0642     return count;
0643 }
0644 
0645 static DEVICE_ATTR_RO(alarms);
0646 static DEVICE_ATTR_RW(beep_enable);
0647 static DEVICE_ATTR_RW(beep_mask);
0648 
0649 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
0650               char *buf)
0651 {
0652     int bit_nr = to_sensor_dev_attr(attr)->index;
0653     struct gl520_data *data = gl520_update_device(dev);
0654 
0655     return sprintf(buf, "%d\n", (data->alarms >> bit_nr) & 1);
0656 }
0657 
0658 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
0659 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
0660 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
0661 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
0662 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
0663 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 5);
0664 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 6);
0665 static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 7);
0666 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 7);
0667 
0668 static ssize_t beep_show(struct device *dev, struct device_attribute *attr,
0669              char *buf)
0670 {
0671     int bitnr = to_sensor_dev_attr(attr)->index;
0672     struct gl520_data *data = gl520_update_device(dev);
0673 
0674     return sprintf(buf, "%d\n", (data->beep_mask >> bitnr) & 1);
0675 }
0676 
0677 static ssize_t beep_store(struct device *dev, struct device_attribute *attr,
0678               const char *buf, size_t count)
0679 {
0680     struct gl520_data *data = dev_get_drvdata(dev);
0681     struct i2c_client *client = data->client;
0682     int bitnr = to_sensor_dev_attr(attr)->index;
0683     unsigned long bit;
0684 
0685     int err;
0686 
0687     err = kstrtoul(buf, 10, &bit);
0688     if (err)
0689         return err;
0690     if (bit & ~1)
0691         return -EINVAL;
0692 
0693     mutex_lock(&data->update_lock);
0694     data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
0695     if (bit)
0696         data->beep_mask |= (1 << bitnr);
0697     else
0698         data->beep_mask &= ~(1 << bitnr);
0699     gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
0700     mutex_unlock(&data->update_lock);
0701     return count;
0702 }
0703 
0704 static SENSOR_DEVICE_ATTR_RW(in0_beep, beep, 0);
0705 static SENSOR_DEVICE_ATTR_RW(in1_beep, beep, 1);
0706 static SENSOR_DEVICE_ATTR_RW(in2_beep, beep, 2);
0707 static SENSOR_DEVICE_ATTR_RW(in3_beep, beep, 3);
0708 static SENSOR_DEVICE_ATTR_RW(temp1_beep, beep, 4);
0709 static SENSOR_DEVICE_ATTR_RW(fan1_beep, beep, 5);
0710 static SENSOR_DEVICE_ATTR_RW(fan2_beep, beep, 6);
0711 static SENSOR_DEVICE_ATTR_RW(temp2_beep, beep, 7);
0712 static SENSOR_DEVICE_ATTR_RW(in4_beep, beep, 7);
0713 
0714 static struct attribute *gl520_attributes[] = {
0715     &dev_attr_cpu0_vid.attr,
0716 
0717     &sensor_dev_attr_in0_input.dev_attr.attr,
0718     &sensor_dev_attr_in0_min.dev_attr.attr,
0719     &sensor_dev_attr_in0_max.dev_attr.attr,
0720     &sensor_dev_attr_in0_alarm.dev_attr.attr,
0721     &sensor_dev_attr_in0_beep.dev_attr.attr,
0722     &sensor_dev_attr_in1_input.dev_attr.attr,
0723     &sensor_dev_attr_in1_min.dev_attr.attr,
0724     &sensor_dev_attr_in1_max.dev_attr.attr,
0725     &sensor_dev_attr_in1_alarm.dev_attr.attr,
0726     &sensor_dev_attr_in1_beep.dev_attr.attr,
0727     &sensor_dev_attr_in2_input.dev_attr.attr,
0728     &sensor_dev_attr_in2_min.dev_attr.attr,
0729     &sensor_dev_attr_in2_max.dev_attr.attr,
0730     &sensor_dev_attr_in2_alarm.dev_attr.attr,
0731     &sensor_dev_attr_in2_beep.dev_attr.attr,
0732     &sensor_dev_attr_in3_input.dev_attr.attr,
0733     &sensor_dev_attr_in3_min.dev_attr.attr,
0734     &sensor_dev_attr_in3_max.dev_attr.attr,
0735     &sensor_dev_attr_in3_alarm.dev_attr.attr,
0736     &sensor_dev_attr_in3_beep.dev_attr.attr,
0737 
0738     &sensor_dev_attr_fan1_input.dev_attr.attr,
0739     &sensor_dev_attr_fan1_min.dev_attr.attr,
0740     &sensor_dev_attr_fan1_div.dev_attr.attr,
0741     &sensor_dev_attr_fan1_alarm.dev_attr.attr,
0742     &sensor_dev_attr_fan1_beep.dev_attr.attr,
0743     &dev_attr_fan1_off.attr,
0744     &sensor_dev_attr_fan2_input.dev_attr.attr,
0745     &sensor_dev_attr_fan2_min.dev_attr.attr,
0746     &sensor_dev_attr_fan2_div.dev_attr.attr,
0747     &sensor_dev_attr_fan2_alarm.dev_attr.attr,
0748     &sensor_dev_attr_fan2_beep.dev_attr.attr,
0749 
0750     &sensor_dev_attr_temp1_input.dev_attr.attr,
0751     &sensor_dev_attr_temp1_max.dev_attr.attr,
0752     &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
0753     &sensor_dev_attr_temp1_alarm.dev_attr.attr,
0754     &sensor_dev_attr_temp1_beep.dev_attr.attr,
0755 
0756     &dev_attr_alarms.attr,
0757     &dev_attr_beep_enable.attr,
0758     &dev_attr_beep_mask.attr,
0759     NULL
0760 };
0761 
0762 static const struct attribute_group gl520_group = {
0763     .attrs = gl520_attributes,
0764 };
0765 
0766 static struct attribute *gl520_attributes_in4[] = {
0767     &sensor_dev_attr_in4_input.dev_attr.attr,
0768     &sensor_dev_attr_in4_min.dev_attr.attr,
0769     &sensor_dev_attr_in4_max.dev_attr.attr,
0770     &sensor_dev_attr_in4_alarm.dev_attr.attr,
0771     &sensor_dev_attr_in4_beep.dev_attr.attr,
0772     NULL
0773 };
0774 
0775 static struct attribute *gl520_attributes_temp2[] = {
0776     &sensor_dev_attr_temp2_input.dev_attr.attr,
0777     &sensor_dev_attr_temp2_max.dev_attr.attr,
0778     &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
0779     &sensor_dev_attr_temp2_alarm.dev_attr.attr,
0780     &sensor_dev_attr_temp2_beep.dev_attr.attr,
0781     NULL
0782 };
0783 
0784 static const struct attribute_group gl520_group_in4 = {
0785     .attrs = gl520_attributes_in4,
0786 };
0787 
0788 static const struct attribute_group gl520_group_temp2 = {
0789     .attrs = gl520_attributes_temp2,
0790 };
0791 
0792 
0793 /*
0794  * Real code
0795  */
0796 
0797 /* Return 0 if detection is successful, -ENODEV otherwise */
0798 static int gl520_detect(struct i2c_client *client, struct i2c_board_info *info)
0799 {
0800     struct i2c_adapter *adapter = client->adapter;
0801 
0802     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
0803                      I2C_FUNC_SMBUS_WORD_DATA))
0804         return -ENODEV;
0805 
0806     /* Determine the chip type. */
0807     if ((gl520_read_value(client, GL520_REG_CHIP_ID) != 0x20) ||
0808         ((gl520_read_value(client, GL520_REG_REVISION) & 0x7f) != 0x00) ||
0809         ((gl520_read_value(client, GL520_REG_CONF) & 0x80) != 0x00)) {
0810         dev_dbg(&client->dev, "Unknown chip type, skipping\n");
0811         return -ENODEV;
0812     }
0813 
0814     strlcpy(info->type, "gl520sm", I2C_NAME_SIZE);
0815 
0816     return 0;
0817 }
0818 
0819 /* Called when we have found a new GL520SM. */
0820 static void gl520_init_client(struct i2c_client *client)
0821 {
0822     struct gl520_data *data = i2c_get_clientdata(client);
0823     u8 oldconf, conf;
0824 
0825     conf = oldconf = gl520_read_value(client, GL520_REG_CONF);
0826 
0827     data->alarm_mask = 0xff;
0828     data->vrm = vid_which_vrm();
0829 
0830     if (extra_sensor_type == 1)
0831         conf &= ~0x10;
0832     else if (extra_sensor_type == 2)
0833         conf |= 0x10;
0834     data->two_temps = !(conf & 0x10);
0835 
0836     /* If IRQ# is disabled, we can safely force comparator mode */
0837     if (!(conf & 0x20))
0838         conf &= 0xf7;
0839 
0840     /* Enable monitoring if needed */
0841     conf |= 0x40;
0842 
0843     if (conf != oldconf)
0844         gl520_write_value(client, GL520_REG_CONF, conf);
0845 
0846     gl520_update_device(&(client->dev));
0847 
0848     if (data->fan_min[0] == 0)
0849         data->alarm_mask &= ~0x20;
0850     if (data->fan_min[1] == 0)
0851         data->alarm_mask &= ~0x40;
0852 
0853     data->beep_mask &= data->alarm_mask;
0854     gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
0855 }
0856 
0857 static int gl520_probe(struct i2c_client *client)
0858 {
0859     struct device *dev = &client->dev;
0860     struct device *hwmon_dev;
0861     struct gl520_data *data;
0862 
0863     data = devm_kzalloc(dev, sizeof(struct gl520_data), GFP_KERNEL);
0864     if (!data)
0865         return -ENOMEM;
0866 
0867     i2c_set_clientdata(client, data);
0868     mutex_init(&data->update_lock);
0869     data->client = client;
0870 
0871     /* Initialize the GL520SM chip */
0872     gl520_init_client(client);
0873 
0874     /* sysfs hooks */
0875     data->groups[0] = &gl520_group;
0876 
0877     if (data->two_temps)
0878         data->groups[1] = &gl520_group_temp2;
0879     else
0880         data->groups[1] = &gl520_group_in4;
0881 
0882     hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
0883                                data, data->groups);
0884     return PTR_ERR_OR_ZERO(hwmon_dev);
0885 }
0886 
0887 static const struct i2c_device_id gl520_id[] = {
0888     { "gl520sm", 0 },
0889     { }
0890 };
0891 MODULE_DEVICE_TABLE(i2c, gl520_id);
0892 
0893 static struct i2c_driver gl520_driver = {
0894     .class      = I2C_CLASS_HWMON,
0895     .driver = {
0896         .name   = "gl520sm",
0897     },
0898     .probe_new  = gl520_probe,
0899     .id_table   = gl520_id,
0900     .detect     = gl520_detect,
0901     .address_list   = normal_i2c,
0902 };
0903 
0904 module_i2c_driver(gl520_driver);
0905 
0906 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
0907     "Kyösti Mälkki <kmalkki@cc.hut.fi>, "
0908     "Maarten Deprez <maartendeprez@users.sourceforge.net>");
0909 MODULE_DESCRIPTION("GL520SM driver");
0910 MODULE_LICENSE("GPL");