Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * lm78.c - Part of lm_sensors, Linux kernel modules for hardware
0004  *      monitoring
0005  * Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
0006  * Copyright (c) 2007, 2011  Jean Delvare <jdelvare@suse.de>
0007  */
0008 
0009 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0010 
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/slab.h>
0014 #include <linux/jiffies.h>
0015 #include <linux/i2c.h>
0016 #include <linux/hwmon.h>
0017 #include <linux/hwmon-vid.h>
0018 #include <linux/hwmon-sysfs.h>
0019 #include <linux/err.h>
0020 #include <linux/mutex.h>
0021 
0022 #ifdef CONFIG_ISA
0023 #include <linux/platform_device.h>
0024 #include <linux/ioport.h>
0025 #include <linux/io.h>
0026 #endif
0027 
0028 /* Addresses to scan */
0029 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
0030                         0x2e, 0x2f, I2C_CLIENT_END };
0031 enum chips { lm78, lm79 };
0032 
0033 /* Many LM78 constants specified below */
0034 
0035 /* Length of ISA address segment */
0036 #define LM78_EXTENT 8
0037 
0038 /* Where are the ISA address/data registers relative to the base address */
0039 #define LM78_ADDR_REG_OFFSET 5
0040 #define LM78_DATA_REG_OFFSET 6
0041 
0042 /* The LM78 registers */
0043 #define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
0044 #define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
0045 #define LM78_REG_IN(nr) (0x20 + (nr))
0046 
0047 #define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
0048 #define LM78_REG_FAN(nr) (0x28 + (nr))
0049 
0050 #define LM78_REG_TEMP 0x27
0051 #define LM78_REG_TEMP_OVER 0x39
0052 #define LM78_REG_TEMP_HYST 0x3a
0053 
0054 #define LM78_REG_ALARM1 0x41
0055 #define LM78_REG_ALARM2 0x42
0056 
0057 #define LM78_REG_VID_FANDIV 0x47
0058 
0059 #define LM78_REG_CONFIG 0x40
0060 #define LM78_REG_CHIPID 0x49
0061 #define LM78_REG_I2C_ADDR 0x48
0062 
0063 /*
0064  * Conversions. Rounding and limit checking is only done on the TO_REG
0065  * variants.
0066  */
0067 
0068 /*
0069  * IN: mV (0V to 4.08V)
0070  * REG: 16mV/bit
0071  */
0072 static inline u8 IN_TO_REG(unsigned long val)
0073 {
0074     unsigned long nval = clamp_val(val, 0, 4080);
0075     return (nval + 8) / 16;
0076 }
0077 #define IN_FROM_REG(val) ((val) *  16)
0078 
0079 static inline u8 FAN_TO_REG(long rpm, int div)
0080 {
0081     if (rpm <= 0)
0082         return 255;
0083     if (rpm > 1350000)
0084         return 1;
0085     return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
0086 }
0087 
0088 static inline int FAN_FROM_REG(u8 val, int div)
0089 {
0090     return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
0091 }
0092 
0093 /*
0094  * TEMP: mC (-128C to +127C)
0095  * REG: 1C/bit, two's complement
0096  */
0097 static inline s8 TEMP_TO_REG(long val)
0098 {
0099     int nval = clamp_val(val, -128000, 127000) ;
0100     return nval < 0 ? (nval - 500) / 1000 : (nval + 500) / 1000;
0101 }
0102 
0103 static inline int TEMP_FROM_REG(s8 val)
0104 {
0105     return val * 1000;
0106 }
0107 
0108 #define DIV_FROM_REG(val) (1 << (val))
0109 
0110 struct lm78_data {
0111     struct i2c_client *client;
0112     struct mutex lock;
0113     enum chips type;
0114 
0115     /* For ISA device only */
0116     const char *name;
0117     int isa_addr;
0118 
0119     struct mutex update_lock;
0120     bool valid;     /* true if following fields are valid */
0121     unsigned long last_updated; /* In jiffies */
0122 
0123     u8 in[7];       /* Register value */
0124     u8 in_max[7];       /* Register value */
0125     u8 in_min[7];       /* Register value */
0126     u8 fan[3];      /* Register value */
0127     u8 fan_min[3];      /* Register value */
0128     s8 temp;        /* Register value */
0129     s8 temp_over;       /* Register value */
0130     s8 temp_hyst;       /* Register value */
0131     u8 fan_div[3];      /* Register encoding, shifted right */
0132     u8 vid;         /* Register encoding, combined */
0133     u16 alarms;     /* Register encoding, combined */
0134 };
0135 
0136 static int lm78_read_value(struct lm78_data *data, u8 reg);
0137 static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value);
0138 static struct lm78_data *lm78_update_device(struct device *dev);
0139 static void lm78_init_device(struct lm78_data *data);
0140 
0141 /* 7 Voltages */
0142 static ssize_t in_show(struct device *dev, struct device_attribute *da,
0143                char *buf)
0144 {
0145     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0146     struct lm78_data *data = lm78_update_device(dev);
0147     return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index]));
0148 }
0149 
0150 static ssize_t in_min_show(struct device *dev, struct device_attribute *da,
0151                char *buf)
0152 {
0153     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0154     struct lm78_data *data = lm78_update_device(dev);
0155     return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index]));
0156 }
0157 
0158 static ssize_t in_max_show(struct device *dev, struct device_attribute *da,
0159                char *buf)
0160 {
0161     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0162     struct lm78_data *data = lm78_update_device(dev);
0163     return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index]));
0164 }
0165 
0166 static ssize_t in_min_store(struct device *dev, struct device_attribute *da,
0167                 const char *buf, size_t count)
0168 {
0169     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0170     struct lm78_data *data = dev_get_drvdata(dev);
0171     int nr = attr->index;
0172     unsigned long val;
0173     int err;
0174 
0175     err = kstrtoul(buf, 10, &val);
0176     if (err)
0177         return err;
0178 
0179     mutex_lock(&data->update_lock);
0180     data->in_min[nr] = IN_TO_REG(val);
0181     lm78_write_value(data, LM78_REG_IN_MIN(nr), data->in_min[nr]);
0182     mutex_unlock(&data->update_lock);
0183     return count;
0184 }
0185 
0186 static ssize_t in_max_store(struct device *dev, struct device_attribute *da,
0187                 const char *buf, size_t count)
0188 {
0189     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0190     struct lm78_data *data = dev_get_drvdata(dev);
0191     int nr = attr->index;
0192     unsigned long val;
0193     int err;
0194 
0195     err = kstrtoul(buf, 10, &val);
0196     if (err)
0197         return err;
0198 
0199     mutex_lock(&data->update_lock);
0200     data->in_max[nr] = IN_TO_REG(val);
0201     lm78_write_value(data, LM78_REG_IN_MAX(nr), data->in_max[nr]);
0202     mutex_unlock(&data->update_lock);
0203     return count;
0204 }
0205 
0206 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
0207 static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
0208 static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
0209 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
0210 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
0211 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
0212 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
0213 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
0214 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
0215 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
0216 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
0217 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
0218 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
0219 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
0220 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
0221 static SENSOR_DEVICE_ATTR_RO(in5_input, in, 5);
0222 static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
0223 static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
0224 static SENSOR_DEVICE_ATTR_RO(in6_input, in, 6);
0225 static SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 6);
0226 static SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 6);
0227 
0228 /* Temperature */
0229 static ssize_t temp1_input_show(struct device *dev,
0230                 struct device_attribute *da, char *buf)
0231 {
0232     struct lm78_data *data = lm78_update_device(dev);
0233     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
0234 }
0235 
0236 static ssize_t temp1_max_show(struct device *dev, struct device_attribute *da,
0237                   char *buf)
0238 {
0239     struct lm78_data *data = lm78_update_device(dev);
0240     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
0241 }
0242 
0243 static ssize_t temp1_max_store(struct device *dev,
0244                    struct device_attribute *da, const char *buf,
0245                    size_t count)
0246 {
0247     struct lm78_data *data = dev_get_drvdata(dev);
0248     long val;
0249     int err;
0250 
0251     err = kstrtol(buf, 10, &val);
0252     if (err)
0253         return err;
0254 
0255     mutex_lock(&data->update_lock);
0256     data->temp_over = TEMP_TO_REG(val);
0257     lm78_write_value(data, LM78_REG_TEMP_OVER, data->temp_over);
0258     mutex_unlock(&data->update_lock);
0259     return count;
0260 }
0261 
0262 static ssize_t temp1_max_hyst_show(struct device *dev,
0263                    struct device_attribute *da, char *buf)
0264 {
0265     struct lm78_data *data = lm78_update_device(dev);
0266     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
0267 }
0268 
0269 static ssize_t temp1_max_hyst_store(struct device *dev,
0270                     struct device_attribute *da,
0271                     const char *buf, size_t count)
0272 {
0273     struct lm78_data *data = dev_get_drvdata(dev);
0274     long val;
0275     int err;
0276 
0277     err = kstrtol(buf, 10, &val);
0278     if (err)
0279         return err;
0280 
0281     mutex_lock(&data->update_lock);
0282     data->temp_hyst = TEMP_TO_REG(val);
0283     lm78_write_value(data, LM78_REG_TEMP_HYST, data->temp_hyst);
0284     mutex_unlock(&data->update_lock);
0285     return count;
0286 }
0287 
0288 static DEVICE_ATTR_RO(temp1_input);
0289 static DEVICE_ATTR_RW(temp1_max);
0290 static DEVICE_ATTR_RW(temp1_max_hyst);
0291 
0292 /* 3 Fans */
0293 static ssize_t fan_show(struct device *dev, struct device_attribute *da,
0294             char *buf)
0295 {
0296     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0297     struct lm78_data *data = lm78_update_device(dev);
0298     int nr = attr->index;
0299     return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
0300         DIV_FROM_REG(data->fan_div[nr])));
0301 }
0302 
0303 static ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
0304                 char *buf)
0305 {
0306     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0307     struct lm78_data *data = lm78_update_device(dev);
0308     int nr = attr->index;
0309     return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
0310         DIV_FROM_REG(data->fan_div[nr])));
0311 }
0312 
0313 static ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
0314                  const char *buf, size_t count)
0315 {
0316     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0317     struct lm78_data *data = dev_get_drvdata(dev);
0318     int nr = attr->index;
0319     unsigned long val;
0320     int err;
0321 
0322     err = kstrtoul(buf, 10, &val);
0323     if (err)
0324         return err;
0325 
0326     mutex_lock(&data->update_lock);
0327     data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
0328     lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
0329     mutex_unlock(&data->update_lock);
0330     return count;
0331 }
0332 
0333 static ssize_t fan_div_show(struct device *dev, struct device_attribute *da,
0334                 char *buf)
0335 {
0336     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0337     struct lm78_data *data = lm78_update_device(dev);
0338     return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
0339 }
0340 
0341 /*
0342  * Note: we save and restore the fan minimum here, because its value is
0343  * determined in part by the fan divisor.  This follows the principle of
0344  * least surprise; the user doesn't expect the fan minimum to change just
0345  * because the divisor changed.
0346  */
0347 static ssize_t fan_div_store(struct device *dev, struct device_attribute *da,
0348                  const char *buf, size_t count)
0349 {
0350     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0351     struct lm78_data *data = dev_get_drvdata(dev);
0352     int nr = attr->index;
0353     unsigned long min;
0354     u8 reg;
0355     unsigned long val;
0356     int err;
0357 
0358     err = kstrtoul(buf, 10, &val);
0359     if (err)
0360         return err;
0361 
0362     mutex_lock(&data->update_lock);
0363     min = FAN_FROM_REG(data->fan_min[nr],
0364                DIV_FROM_REG(data->fan_div[nr]));
0365 
0366     switch (val) {
0367     case 1:
0368         data->fan_div[nr] = 0;
0369         break;
0370     case 2:
0371         data->fan_div[nr] = 1;
0372         break;
0373     case 4:
0374         data->fan_div[nr] = 2;
0375         break;
0376     case 8:
0377         data->fan_div[nr] = 3;
0378         break;
0379     default:
0380         dev_err(dev,
0381             "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
0382             val);
0383         mutex_unlock(&data->update_lock);
0384         return -EINVAL;
0385     }
0386 
0387     reg = lm78_read_value(data, LM78_REG_VID_FANDIV);
0388     switch (nr) {
0389     case 0:
0390         reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
0391         break;
0392     case 1:
0393         reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
0394         break;
0395     }
0396     lm78_write_value(data, LM78_REG_VID_FANDIV, reg);
0397 
0398     data->fan_min[nr] =
0399         FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
0400     lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
0401     mutex_unlock(&data->update_lock);
0402 
0403     return count;
0404 }
0405 
0406 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
0407 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
0408 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
0409 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
0410 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
0411 static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
0412 
0413 /* Fan 3 divisor is locked in H/W */
0414 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
0415 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
0416 static SENSOR_DEVICE_ATTR_RO(fan3_div, fan_div, 2);
0417 
0418 /* VID */
0419 static ssize_t cpu0_vid_show(struct device *dev, struct device_attribute *da,
0420                  char *buf)
0421 {
0422     struct lm78_data *data = lm78_update_device(dev);
0423     return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82));
0424 }
0425 static DEVICE_ATTR_RO(cpu0_vid);
0426 
0427 /* Alarms */
0428 static ssize_t alarms_show(struct device *dev, struct device_attribute *da,
0429                char *buf)
0430 {
0431     struct lm78_data *data = lm78_update_device(dev);
0432     return sprintf(buf, "%u\n", data->alarms);
0433 }
0434 static DEVICE_ATTR_RO(alarms);
0435 
0436 static ssize_t alarm_show(struct device *dev, struct device_attribute *da,
0437               char *buf)
0438 {
0439     struct lm78_data *data = lm78_update_device(dev);
0440     int nr = to_sensor_dev_attr(da)->index;
0441     return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
0442 }
0443 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
0444 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
0445 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
0446 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
0447 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
0448 static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
0449 static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 10);
0450 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
0451 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
0452 static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, 11);
0453 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
0454 
0455 static struct attribute *lm78_attrs[] = {
0456     &sensor_dev_attr_in0_input.dev_attr.attr,
0457     &sensor_dev_attr_in0_min.dev_attr.attr,
0458     &sensor_dev_attr_in0_max.dev_attr.attr,
0459     &sensor_dev_attr_in0_alarm.dev_attr.attr,
0460     &sensor_dev_attr_in1_input.dev_attr.attr,
0461     &sensor_dev_attr_in1_min.dev_attr.attr,
0462     &sensor_dev_attr_in1_max.dev_attr.attr,
0463     &sensor_dev_attr_in1_alarm.dev_attr.attr,
0464     &sensor_dev_attr_in2_input.dev_attr.attr,
0465     &sensor_dev_attr_in2_min.dev_attr.attr,
0466     &sensor_dev_attr_in2_max.dev_attr.attr,
0467     &sensor_dev_attr_in2_alarm.dev_attr.attr,
0468     &sensor_dev_attr_in3_input.dev_attr.attr,
0469     &sensor_dev_attr_in3_min.dev_attr.attr,
0470     &sensor_dev_attr_in3_max.dev_attr.attr,
0471     &sensor_dev_attr_in3_alarm.dev_attr.attr,
0472     &sensor_dev_attr_in4_input.dev_attr.attr,
0473     &sensor_dev_attr_in4_min.dev_attr.attr,
0474     &sensor_dev_attr_in4_max.dev_attr.attr,
0475     &sensor_dev_attr_in4_alarm.dev_attr.attr,
0476     &sensor_dev_attr_in5_input.dev_attr.attr,
0477     &sensor_dev_attr_in5_min.dev_attr.attr,
0478     &sensor_dev_attr_in5_max.dev_attr.attr,
0479     &sensor_dev_attr_in5_alarm.dev_attr.attr,
0480     &sensor_dev_attr_in6_input.dev_attr.attr,
0481     &sensor_dev_attr_in6_min.dev_attr.attr,
0482     &sensor_dev_attr_in6_max.dev_attr.attr,
0483     &sensor_dev_attr_in6_alarm.dev_attr.attr,
0484     &dev_attr_temp1_input.attr,
0485     &dev_attr_temp1_max.attr,
0486     &dev_attr_temp1_max_hyst.attr,
0487     &sensor_dev_attr_temp1_alarm.dev_attr.attr,
0488     &sensor_dev_attr_fan1_input.dev_attr.attr,
0489     &sensor_dev_attr_fan1_min.dev_attr.attr,
0490     &sensor_dev_attr_fan1_div.dev_attr.attr,
0491     &sensor_dev_attr_fan1_alarm.dev_attr.attr,
0492     &sensor_dev_attr_fan2_input.dev_attr.attr,
0493     &sensor_dev_attr_fan2_min.dev_attr.attr,
0494     &sensor_dev_attr_fan2_div.dev_attr.attr,
0495     &sensor_dev_attr_fan2_alarm.dev_attr.attr,
0496     &sensor_dev_attr_fan3_input.dev_attr.attr,
0497     &sensor_dev_attr_fan3_min.dev_attr.attr,
0498     &sensor_dev_attr_fan3_div.dev_attr.attr,
0499     &sensor_dev_attr_fan3_alarm.dev_attr.attr,
0500     &dev_attr_alarms.attr,
0501     &dev_attr_cpu0_vid.attr,
0502 
0503     NULL
0504 };
0505 
0506 ATTRIBUTE_GROUPS(lm78);
0507 
0508 /*
0509  * ISA related code
0510  */
0511 #ifdef CONFIG_ISA
0512 
0513 /* ISA device, if found */
0514 static struct platform_device *pdev;
0515 
0516 static unsigned short isa_address = 0x290;
0517 
0518 static struct lm78_data *lm78_data_if_isa(void)
0519 {
0520     return pdev ? platform_get_drvdata(pdev) : NULL;
0521 }
0522 
0523 /* Returns 1 if the I2C chip appears to be an alias of the ISA chip */
0524 static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
0525 {
0526     struct lm78_data *isa;
0527     int i;
0528 
0529     if (!pdev)  /* No ISA chip */
0530         return 0;
0531     isa = platform_get_drvdata(pdev);
0532 
0533     if (lm78_read_value(isa, LM78_REG_I2C_ADDR) != client->addr)
0534         return 0;   /* Address doesn't match */
0535     if ((lm78_read_value(isa, LM78_REG_CHIPID) & 0xfe) != (chipid & 0xfe))
0536         return 0;   /* Chip type doesn't match */
0537 
0538     /*
0539      * We compare all the limit registers, the config register and the
0540      * interrupt mask registers
0541      */
0542     for (i = 0x2b; i <= 0x3d; i++) {
0543         if (lm78_read_value(isa, i) !=
0544             i2c_smbus_read_byte_data(client, i))
0545             return 0;
0546     }
0547     if (lm78_read_value(isa, LM78_REG_CONFIG) !=
0548         i2c_smbus_read_byte_data(client, LM78_REG_CONFIG))
0549         return 0;
0550     for (i = 0x43; i <= 0x46; i++) {
0551         if (lm78_read_value(isa, i) !=
0552             i2c_smbus_read_byte_data(client, i))
0553             return 0;
0554     }
0555 
0556     return 1;
0557 }
0558 #else /* !CONFIG_ISA */
0559 
0560 static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
0561 {
0562     return 0;
0563 }
0564 
0565 static struct lm78_data *lm78_data_if_isa(void)
0566 {
0567     return NULL;
0568 }
0569 #endif /* CONFIG_ISA */
0570 
0571 static int lm78_i2c_detect(struct i2c_client *client,
0572                struct i2c_board_info *info)
0573 {
0574     int i;
0575     struct lm78_data *isa = lm78_data_if_isa();
0576     const char *client_name;
0577     struct i2c_adapter *adapter = client->adapter;
0578     int address = client->addr;
0579 
0580     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0581         return -ENODEV;
0582 
0583     /*
0584      * We block updates of the ISA device to minimize the risk of
0585      * concurrent access to the same LM78 chip through different
0586      * interfaces.
0587      */
0588     if (isa)
0589         mutex_lock(&isa->update_lock);
0590 
0591     if ((i2c_smbus_read_byte_data(client, LM78_REG_CONFIG) & 0x80)
0592      || i2c_smbus_read_byte_data(client, LM78_REG_I2C_ADDR) != address)
0593         goto err_nodev;
0594 
0595     /* Explicitly prevent the misdetection of Winbond chips */
0596     i = i2c_smbus_read_byte_data(client, 0x4f);
0597     if (i == 0xa3 || i == 0x5c)
0598         goto err_nodev;
0599 
0600     /* Determine the chip type. */
0601     i = i2c_smbus_read_byte_data(client, LM78_REG_CHIPID);
0602     if (i == 0x00 || i == 0x20  /* LM78 */
0603      || i == 0x40)          /* LM78-J */
0604         client_name = "lm78";
0605     else if ((i & 0xfe) == 0xc0)
0606         client_name = "lm79";
0607     else
0608         goto err_nodev;
0609 
0610     if (lm78_alias_detect(client, i)) {
0611         dev_dbg(&adapter->dev,
0612             "Device at 0x%02x appears to be the same as ISA device\n",
0613             address);
0614         goto err_nodev;
0615     }
0616 
0617     if (isa)
0618         mutex_unlock(&isa->update_lock);
0619 
0620     strlcpy(info->type, client_name, I2C_NAME_SIZE);
0621 
0622     return 0;
0623 
0624  err_nodev:
0625     if (isa)
0626         mutex_unlock(&isa->update_lock);
0627     return -ENODEV;
0628 }
0629 
0630 static const struct i2c_device_id lm78_i2c_id[];
0631 
0632 static int lm78_i2c_probe(struct i2c_client *client)
0633 {
0634     struct device *dev = &client->dev;
0635     struct device *hwmon_dev;
0636     struct lm78_data *data;
0637 
0638     data = devm_kzalloc(dev, sizeof(struct lm78_data), GFP_KERNEL);
0639     if (!data)
0640         return -ENOMEM;
0641 
0642     data->client = client;
0643     data->type = i2c_match_id(lm78_i2c_id, client)->driver_data;
0644 
0645     /* Initialize the LM78 chip */
0646     lm78_init_device(data);
0647 
0648     hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
0649                                data, lm78_groups);
0650     return PTR_ERR_OR_ZERO(hwmon_dev);
0651 }
0652 
0653 static const struct i2c_device_id lm78_i2c_id[] = {
0654     { "lm78", lm78 },
0655     { "lm79", lm79 },
0656     { }
0657 };
0658 MODULE_DEVICE_TABLE(i2c, lm78_i2c_id);
0659 
0660 static struct i2c_driver lm78_driver = {
0661     .class      = I2C_CLASS_HWMON,
0662     .driver = {
0663         .name   = "lm78",
0664     },
0665     .probe_new  = lm78_i2c_probe,
0666     .id_table   = lm78_i2c_id,
0667     .detect     = lm78_i2c_detect,
0668     .address_list   = normal_i2c,
0669 };
0670 
0671 /*
0672  * The SMBus locks itself, but ISA access must be locked explicitly!
0673  * We don't want to lock the whole ISA bus, so we lock each client
0674  * separately.
0675  * We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
0676  * would slow down the LM78 access and should not be necessary.
0677  */
0678 static int lm78_read_value(struct lm78_data *data, u8 reg)
0679 {
0680     struct i2c_client *client = data->client;
0681 
0682 #ifdef CONFIG_ISA
0683     if (!client) { /* ISA device */
0684         int res;
0685         mutex_lock(&data->lock);
0686         outb_p(reg, data->isa_addr + LM78_ADDR_REG_OFFSET);
0687         res = inb_p(data->isa_addr + LM78_DATA_REG_OFFSET);
0688         mutex_unlock(&data->lock);
0689         return res;
0690     } else
0691 #endif
0692         return i2c_smbus_read_byte_data(client, reg);
0693 }
0694 
0695 static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value)
0696 {
0697     struct i2c_client *client = data->client;
0698 
0699 #ifdef CONFIG_ISA
0700     if (!client) { /* ISA device */
0701         mutex_lock(&data->lock);
0702         outb_p(reg, data->isa_addr + LM78_ADDR_REG_OFFSET);
0703         outb_p(value, data->isa_addr + LM78_DATA_REG_OFFSET);
0704         mutex_unlock(&data->lock);
0705         return 0;
0706     } else
0707 #endif
0708         return i2c_smbus_write_byte_data(client, reg, value);
0709 }
0710 
0711 static void lm78_init_device(struct lm78_data *data)
0712 {
0713     u8 config;
0714     int i;
0715 
0716     /* Start monitoring */
0717     config = lm78_read_value(data, LM78_REG_CONFIG);
0718     if ((config & 0x09) != 0x01)
0719         lm78_write_value(data, LM78_REG_CONFIG,
0720                  (config & 0xf7) | 0x01);
0721 
0722     /* A few vars need to be filled upon startup */
0723     for (i = 0; i < 3; i++) {
0724         data->fan_min[i] = lm78_read_value(data,
0725                     LM78_REG_FAN_MIN(i));
0726     }
0727 
0728     mutex_init(&data->update_lock);
0729 }
0730 
0731 static struct lm78_data *lm78_update_device(struct device *dev)
0732 {
0733     struct lm78_data *data = dev_get_drvdata(dev);
0734     int i;
0735 
0736     mutex_lock(&data->update_lock);
0737 
0738     if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
0739         || !data->valid) {
0740 
0741         dev_dbg(dev, "Starting lm78 update\n");
0742 
0743         for (i = 0; i <= 6; i++) {
0744             data->in[i] =
0745                 lm78_read_value(data, LM78_REG_IN(i));
0746             data->in_min[i] =
0747                 lm78_read_value(data, LM78_REG_IN_MIN(i));
0748             data->in_max[i] =
0749                 lm78_read_value(data, LM78_REG_IN_MAX(i));
0750         }
0751         for (i = 0; i < 3; i++) {
0752             data->fan[i] =
0753                 lm78_read_value(data, LM78_REG_FAN(i));
0754             data->fan_min[i] =
0755                 lm78_read_value(data, LM78_REG_FAN_MIN(i));
0756         }
0757         data->temp = lm78_read_value(data, LM78_REG_TEMP);
0758         data->temp_over =
0759             lm78_read_value(data, LM78_REG_TEMP_OVER);
0760         data->temp_hyst =
0761             lm78_read_value(data, LM78_REG_TEMP_HYST);
0762         i = lm78_read_value(data, LM78_REG_VID_FANDIV);
0763         data->vid = i & 0x0f;
0764         if (data->type == lm79)
0765             data->vid |=
0766                 (lm78_read_value(data, LM78_REG_CHIPID) &
0767                  0x01) << 4;
0768         else
0769             data->vid |= 0x10;
0770         data->fan_div[0] = (i >> 4) & 0x03;
0771         data->fan_div[1] = i >> 6;
0772         data->alarms = lm78_read_value(data, LM78_REG_ALARM1) +
0773             (lm78_read_value(data, LM78_REG_ALARM2) << 8);
0774         data->last_updated = jiffies;
0775         data->valid = true;
0776 
0777         data->fan_div[2] = 1;
0778     }
0779 
0780     mutex_unlock(&data->update_lock);
0781 
0782     return data;
0783 }
0784 
0785 #ifdef CONFIG_ISA
0786 static int lm78_isa_probe(struct platform_device *pdev)
0787 {
0788     struct device *dev = &pdev->dev;
0789     struct device *hwmon_dev;
0790     struct lm78_data *data;
0791     struct resource *res;
0792 
0793     /* Reserve the ISA region */
0794     res = platform_get_resource(pdev, IORESOURCE_IO, 0);
0795     if (!devm_request_region(dev, res->start + LM78_ADDR_REG_OFFSET,
0796                  2, "lm78"))
0797         return -EBUSY;
0798 
0799     data = devm_kzalloc(dev, sizeof(struct lm78_data), GFP_KERNEL);
0800     if (!data)
0801         return -ENOMEM;
0802 
0803     mutex_init(&data->lock);
0804     data->isa_addr = res->start;
0805     platform_set_drvdata(pdev, data);
0806 
0807     if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) {
0808         data->type = lm79;
0809         data->name = "lm79";
0810     } else {
0811         data->type = lm78;
0812         data->name = "lm78";
0813     }
0814 
0815     /* Initialize the LM78 chip */
0816     lm78_init_device(data);
0817 
0818     hwmon_dev = devm_hwmon_device_register_with_groups(dev, data->name,
0819                                data, lm78_groups);
0820     return PTR_ERR_OR_ZERO(hwmon_dev);
0821 }
0822 
0823 static struct platform_driver lm78_isa_driver = {
0824     .driver = {
0825         .name   = "lm78",
0826     },
0827     .probe      = lm78_isa_probe,
0828 };
0829 
0830 /* return 1 if a supported chip is found, 0 otherwise */
0831 static int __init lm78_isa_found(unsigned short address)
0832 {
0833     int val, save, found = 0;
0834     int port;
0835 
0836     /*
0837      * Some boards declare base+0 to base+7 as a PNP device, some base+4
0838      * to base+7 and some base+5 to base+6. So we better request each port
0839      * individually for the probing phase.
0840      */
0841     for (port = address; port < address + LM78_EXTENT; port++) {
0842         if (!request_region(port, 1, "lm78")) {
0843             pr_debug("Failed to request port 0x%x\n", port);
0844             goto release;
0845         }
0846     }
0847 
0848 #define REALLY_SLOW_IO
0849     /*
0850      * We need the timeouts for at least some LM78-like
0851      * chips. But only if we read 'undefined' registers.
0852      */
0853     val = inb_p(address + 1);
0854     if (inb_p(address + 2) != val
0855      || inb_p(address + 3) != val
0856      || inb_p(address + 7) != val)
0857         goto release;
0858 #undef REALLY_SLOW_IO
0859 
0860     /*
0861      * We should be able to change the 7 LSB of the address port. The
0862      * MSB (busy flag) should be clear initially, set after the write.
0863      */
0864     save = inb_p(address + LM78_ADDR_REG_OFFSET);
0865     if (save & 0x80)
0866         goto release;
0867     val = ~save & 0x7f;
0868     outb_p(val, address + LM78_ADDR_REG_OFFSET);
0869     if (inb_p(address + LM78_ADDR_REG_OFFSET) != (val | 0x80)) {
0870         outb_p(save, address + LM78_ADDR_REG_OFFSET);
0871         goto release;
0872     }
0873 
0874     /* We found a device, now see if it could be an LM78 */
0875     outb_p(LM78_REG_CONFIG, address + LM78_ADDR_REG_OFFSET);
0876     val = inb_p(address + LM78_DATA_REG_OFFSET);
0877     if (val & 0x80)
0878         goto release;
0879     outb_p(LM78_REG_I2C_ADDR, address + LM78_ADDR_REG_OFFSET);
0880     val = inb_p(address + LM78_DATA_REG_OFFSET);
0881     if (val < 0x03 || val > 0x77)   /* Not a valid I2C address */
0882         goto release;
0883 
0884     /* The busy flag should be clear again */
0885     if (inb_p(address + LM78_ADDR_REG_OFFSET) & 0x80)
0886         goto release;
0887 
0888     /* Explicitly prevent the misdetection of Winbond chips */
0889     outb_p(0x4f, address + LM78_ADDR_REG_OFFSET);
0890     val = inb_p(address + LM78_DATA_REG_OFFSET);
0891     if (val == 0xa3 || val == 0x5c)
0892         goto release;
0893 
0894     /* Explicitly prevent the misdetection of ITE chips */
0895     outb_p(0x58, address + LM78_ADDR_REG_OFFSET);
0896     val = inb_p(address + LM78_DATA_REG_OFFSET);
0897     if (val == 0x90)
0898         goto release;
0899 
0900     /* Determine the chip type */
0901     outb_p(LM78_REG_CHIPID, address + LM78_ADDR_REG_OFFSET);
0902     val = inb_p(address + LM78_DATA_REG_OFFSET);
0903     if (val == 0x00 || val == 0x20  /* LM78 */
0904      || val == 0x40         /* LM78-J */
0905      || (val & 0xfe) == 0xc0)   /* LM79 */
0906         found = 1;
0907 
0908     if (found)
0909         pr_info("Found an %s chip at %#x\n",
0910             val & 0x80 ? "LM79" : "LM78", (int)address);
0911 
0912  release:
0913     for (port--; port >= address; port--)
0914         release_region(port, 1);
0915     return found;
0916 }
0917 
0918 static int __init lm78_isa_device_add(unsigned short address)
0919 {
0920     struct resource res = {
0921         .start  = address,
0922         .end    = address + LM78_EXTENT - 1,
0923         .name   = "lm78",
0924         .flags  = IORESOURCE_IO,
0925     };
0926     int err;
0927 
0928     pdev = platform_device_alloc("lm78", address);
0929     if (!pdev) {
0930         err = -ENOMEM;
0931         pr_err("Device allocation failed\n");
0932         goto exit;
0933     }
0934 
0935     err = platform_device_add_resources(pdev, &res, 1);
0936     if (err) {
0937         pr_err("Device resource addition failed (%d)\n", err);
0938         goto exit_device_put;
0939     }
0940 
0941     err = platform_device_add(pdev);
0942     if (err) {
0943         pr_err("Device addition failed (%d)\n", err);
0944         goto exit_device_put;
0945     }
0946 
0947     return 0;
0948 
0949  exit_device_put:
0950     platform_device_put(pdev);
0951  exit:
0952     pdev = NULL;
0953     return err;
0954 }
0955 
0956 static int __init lm78_isa_register(void)
0957 {
0958     int res;
0959 
0960     if (lm78_isa_found(isa_address)) {
0961         res = platform_driver_register(&lm78_isa_driver);
0962         if (res)
0963             goto exit;
0964 
0965         /* Sets global pdev as a side effect */
0966         res = lm78_isa_device_add(isa_address);
0967         if (res)
0968             goto exit_unreg_isa_driver;
0969     }
0970 
0971     return 0;
0972 
0973  exit_unreg_isa_driver:
0974     platform_driver_unregister(&lm78_isa_driver);
0975  exit:
0976     return res;
0977 }
0978 
0979 static void lm78_isa_unregister(void)
0980 {
0981     if (pdev) {
0982         platform_device_unregister(pdev);
0983         platform_driver_unregister(&lm78_isa_driver);
0984     }
0985 }
0986 #else /* !CONFIG_ISA */
0987 
0988 static int __init lm78_isa_register(void)
0989 {
0990     return 0;
0991 }
0992 
0993 static void lm78_isa_unregister(void)
0994 {
0995 }
0996 #endif /* CONFIG_ISA */
0997 
0998 static int __init sm_lm78_init(void)
0999 {
1000     int res;
1001 
1002     /*
1003      * We register the ISA device first, so that we can skip the
1004      * registration of an I2C interface to the same device.
1005      */
1006     res = lm78_isa_register();
1007     if (res)
1008         goto exit;
1009 
1010     res = i2c_add_driver(&lm78_driver);
1011     if (res)
1012         goto exit_unreg_isa_device;
1013 
1014     return 0;
1015 
1016  exit_unreg_isa_device:
1017     lm78_isa_unregister();
1018  exit:
1019     return res;
1020 }
1021 
1022 static void __exit sm_lm78_exit(void)
1023 {
1024     lm78_isa_unregister();
1025     i2c_del_driver(&lm78_driver);
1026 }
1027 
1028 MODULE_AUTHOR("Frodo Looijaard, Jean Delvare <jdelvare@suse.de>");
1029 MODULE_DESCRIPTION("LM78/LM79 driver");
1030 MODULE_LICENSE("GPL");
1031 
1032 module_init(sm_lm78_init);
1033 module_exit(sm_lm78_exit);