Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  Driver for the ADT7411 (I2C/SPI 8 channel 10 bit ADC & temperature-sensor)
0004  *
0005  *  Copyright (C) 2008, 2010 Pengutronix
0006  *
0007  *  TODO: SPI, use power-down mode for suspend?, interrupt handling?
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/err.h>
0014 #include <linux/mutex.h>
0015 #include <linux/jiffies.h>
0016 #include <linux/i2c.h>
0017 #include <linux/hwmon.h>
0018 #include <linux/hwmon-sysfs.h>
0019 #include <linux/slab.h>
0020 
0021 #define ADT7411_REG_STAT_1          0x00
0022 #define ADT7411_STAT_1_INT_TEMP_HIGH        BIT(0)
0023 #define ADT7411_STAT_1_INT_TEMP_LOW     BIT(1)
0024 #define ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1   BIT(2)
0025 #define ADT7411_STAT_1_EXT_TEMP_LOW     BIT(3)
0026 #define ADT7411_STAT_1_EXT_TEMP_FAULT       BIT(4)
0027 #define ADT7411_STAT_1_AIN2         BIT(5)
0028 #define ADT7411_STAT_1_AIN3         BIT(6)
0029 #define ADT7411_STAT_1_AIN4         BIT(7)
0030 #define ADT7411_REG_STAT_2          0x01
0031 #define ADT7411_STAT_2_AIN5         BIT(0)
0032 #define ADT7411_STAT_2_AIN6         BIT(1)
0033 #define ADT7411_STAT_2_AIN7         BIT(2)
0034 #define ADT7411_STAT_2_AIN8         BIT(3)
0035 #define ADT7411_STAT_2_VDD          BIT(4)
0036 #define ADT7411_REG_INT_TEMP_VDD_LSB        0x03
0037 #define ADT7411_REG_EXT_TEMP_AIN14_LSB      0x04
0038 #define ADT7411_REG_VDD_MSB         0x06
0039 #define ADT7411_REG_INT_TEMP_MSB        0x07
0040 #define ADT7411_REG_EXT_TEMP_AIN1_MSB       0x08
0041 
0042 #define ADT7411_REG_CFG1            0x18
0043 #define ADT7411_CFG1_START_MONITOR      BIT(0)
0044 #define ADT7411_CFG1_RESERVED_BIT1      BIT(1)
0045 #define ADT7411_CFG1_EXT_TDM            BIT(2)
0046 #define ADT7411_CFG1_RESERVED_BIT3      BIT(3)
0047 
0048 #define ADT7411_REG_CFG2            0x19
0049 #define ADT7411_CFG2_DISABLE_AVG        BIT(5)
0050 
0051 #define ADT7411_REG_CFG3            0x1a
0052 #define ADT7411_CFG3_ADC_CLK_225        BIT(0)
0053 #define ADT7411_CFG3_RESERVED_BIT1      BIT(1)
0054 #define ADT7411_CFG3_RESERVED_BIT2      BIT(2)
0055 #define ADT7411_CFG3_RESERVED_BIT3      BIT(3)
0056 #define ADT7411_CFG3_REF_VDD            BIT(4)
0057 
0058 #define ADT7411_REG_VDD_HIGH            0x23
0059 #define ADT7411_REG_VDD_LOW         0x24
0060 #define ADT7411_REG_TEMP_HIGH(nr)       (0x25 + 2 * (nr))
0061 #define ADT7411_REG_TEMP_LOW(nr)        (0x26 + 2 * (nr))
0062 #define ADT7411_REG_IN_HIGH(nr)     ((nr) > 1 \
0063                           ? 0x2b + 2 * ((nr)-2) \
0064                           : 0x27)
0065 #define ADT7411_REG_IN_LOW(nr)          ((nr) > 1 \
0066                           ? 0x2c + 2 * ((nr)-2) \
0067                           : 0x28)
0068 
0069 #define ADT7411_REG_DEVICE_ID           0x4d
0070 #define ADT7411_REG_MANUFACTURER_ID     0x4e
0071 
0072 #define ADT7411_DEVICE_ID           0x2
0073 #define ADT7411_MANUFACTURER_ID         0x41
0074 
0075 static const unsigned short normal_i2c[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END };
0076 
0077 static const u8 adt7411_in_alarm_reg[] = {
0078     ADT7411_REG_STAT_2,
0079     ADT7411_REG_STAT_1,
0080     ADT7411_REG_STAT_1,
0081     ADT7411_REG_STAT_1,
0082     ADT7411_REG_STAT_1,
0083     ADT7411_REG_STAT_2,
0084     ADT7411_REG_STAT_2,
0085     ADT7411_REG_STAT_2,
0086     ADT7411_REG_STAT_2,
0087 };
0088 
0089 static const u8 adt7411_in_alarm_bits[] = {
0090     ADT7411_STAT_2_VDD,
0091     ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1,
0092     ADT7411_STAT_1_AIN2,
0093     ADT7411_STAT_1_AIN3,
0094     ADT7411_STAT_1_AIN4,
0095     ADT7411_STAT_2_AIN5,
0096     ADT7411_STAT_2_AIN6,
0097     ADT7411_STAT_2_AIN7,
0098     ADT7411_STAT_2_AIN8,
0099 };
0100 
0101 struct adt7411_data {
0102     struct mutex device_lock;   /* for "atomic" device accesses */
0103     struct mutex update_lock;
0104     unsigned long next_update;
0105     long vref_cached;
0106     struct i2c_client *client;
0107     bool use_ext_temp;
0108 };
0109 
0110 /*
0111  * When reading a register containing (up to 4) lsb, all associated
0112  * msb-registers get locked by the hardware. After _one_ of those msb is read,
0113  * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
0114  * is protected here with a mutex, too.
0115  */
0116 static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
0117                 u8 msb_reg, u8 lsb_shift)
0118 {
0119     struct adt7411_data *data = i2c_get_clientdata(client);
0120     int val, tmp;
0121 
0122     mutex_lock(&data->device_lock);
0123 
0124     val = i2c_smbus_read_byte_data(client, lsb_reg);
0125     if (val < 0)
0126         goto exit_unlock;
0127 
0128     tmp = (val >> lsb_shift) & 3;
0129     val = i2c_smbus_read_byte_data(client, msb_reg);
0130 
0131     if (val >= 0)
0132         val = (val << 2) | tmp;
0133 
0134  exit_unlock:
0135     mutex_unlock(&data->device_lock);
0136 
0137     return val;
0138 }
0139 
0140 static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
0141                 bool flag)
0142 {
0143     struct adt7411_data *data = i2c_get_clientdata(client);
0144     int ret, val;
0145 
0146     mutex_lock(&data->device_lock);
0147 
0148     ret = i2c_smbus_read_byte_data(client, reg);
0149     if (ret < 0)
0150         goto exit_unlock;
0151 
0152     if (flag)
0153         val = ret | bit;
0154     else
0155         val = ret & ~bit;
0156 
0157     ret = i2c_smbus_write_byte_data(client, reg, val);
0158 
0159  exit_unlock:
0160     mutex_unlock(&data->device_lock);
0161     return ret;
0162 }
0163 
0164 static ssize_t adt7411_show_bit(struct device *dev,
0165                 struct device_attribute *attr, char *buf)
0166 {
0167     struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
0168     struct adt7411_data *data = dev_get_drvdata(dev);
0169     struct i2c_client *client = data->client;
0170     int ret = i2c_smbus_read_byte_data(client, attr2->index);
0171 
0172     return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
0173 }
0174 
0175 static ssize_t adt7411_set_bit(struct device *dev,
0176                    struct device_attribute *attr, const char *buf,
0177                    size_t count)
0178 {
0179     struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
0180     struct adt7411_data *data = dev_get_drvdata(dev);
0181     struct i2c_client *client = data->client;
0182     int ret;
0183     unsigned long flag;
0184 
0185     ret = kstrtoul(buf, 0, &flag);
0186     if (ret || flag > 1)
0187         return -EINVAL;
0188 
0189     ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
0190 
0191     /* force update */
0192     mutex_lock(&data->update_lock);
0193     data->next_update = jiffies;
0194     mutex_unlock(&data->update_lock);
0195 
0196     return ret < 0 ? ret : count;
0197 }
0198 
0199 #define ADT7411_BIT_ATTR(__name, __reg, __bit) \
0200     SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
0201     adt7411_set_bit, __bit, __reg)
0202 
0203 static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
0204 static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
0205 static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);
0206 
0207 static struct attribute *adt7411_attrs[] = {
0208     &sensor_dev_attr_no_average.dev_attr.attr,
0209     &sensor_dev_attr_fast_sampling.dev_attr.attr,
0210     &sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
0211     NULL
0212 };
0213 ATTRIBUTE_GROUPS(adt7411);
0214 
0215 static int adt7411_read_in_alarm(struct device *dev, int channel, long *val)
0216 {
0217     struct adt7411_data *data = dev_get_drvdata(dev);
0218     struct i2c_client *client = data->client;
0219     int ret;
0220 
0221     ret = i2c_smbus_read_byte_data(client, adt7411_in_alarm_reg[channel]);
0222     if (ret < 0)
0223         return ret;
0224     *val = !!(ret & adt7411_in_alarm_bits[channel]);
0225     return 0;
0226 }
0227 
0228 static int adt7411_read_in_vdd(struct device *dev, u32 attr, long *val)
0229 {
0230     struct adt7411_data *data = dev_get_drvdata(dev);
0231     struct i2c_client *client = data->client;
0232     int ret;
0233 
0234     switch (attr) {
0235     case hwmon_in_input:
0236         ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
0237                       ADT7411_REG_VDD_MSB, 2);
0238         if (ret < 0)
0239             return ret;
0240         *val = ret * 7000 / 1024;
0241         return 0;
0242     case hwmon_in_min:
0243         ret = i2c_smbus_read_byte_data(client, ADT7411_REG_VDD_LOW);
0244         if (ret < 0)
0245             return ret;
0246         *val = ret * 7000 / 256;
0247         return 0;
0248     case hwmon_in_max:
0249         ret = i2c_smbus_read_byte_data(client, ADT7411_REG_VDD_HIGH);
0250         if (ret < 0)
0251             return ret;
0252         *val = ret * 7000 / 256;
0253         return 0;
0254     case hwmon_in_alarm:
0255         return adt7411_read_in_alarm(dev, 0, val);
0256     default:
0257         return -EOPNOTSUPP;
0258     }
0259 }
0260 
0261 static int adt7411_update_vref(struct device *dev)
0262 {
0263     struct adt7411_data *data = dev_get_drvdata(dev);
0264     struct i2c_client *client = data->client;
0265     int val;
0266 
0267     if (time_after_eq(jiffies, data->next_update)) {
0268         val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
0269         if (val < 0)
0270             return val;
0271 
0272         if (val & ADT7411_CFG3_REF_VDD) {
0273             val = adt7411_read_in_vdd(dev, hwmon_in_input,
0274                           &data->vref_cached);
0275             if (val < 0)
0276                 return val;
0277         } else {
0278             data->vref_cached = 2250;
0279         }
0280 
0281         data->next_update = jiffies + HZ;
0282     }
0283 
0284     return 0;
0285 }
0286 
0287 static int adt7411_read_in_chan(struct device *dev, u32 attr, int channel,
0288                 long *val)
0289 {
0290     struct adt7411_data *data = dev_get_drvdata(dev);
0291     struct i2c_client *client = data->client;
0292 
0293     int ret;
0294     int reg, lsb_reg, lsb_shift;
0295     int nr = channel - 1;
0296 
0297     mutex_lock(&data->update_lock);
0298     ret = adt7411_update_vref(dev);
0299     if (ret < 0)
0300         goto exit_unlock;
0301 
0302     switch (attr) {
0303     case hwmon_in_input:
0304         lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
0305         lsb_shift = 2 * (nr & 0x03);
0306         ret = adt7411_read_10_bit(client, lsb_reg,
0307                       ADT7411_REG_EXT_TEMP_AIN1_MSB + nr,
0308                       lsb_shift);
0309         if (ret < 0)
0310             goto exit_unlock;
0311         *val = ret * data->vref_cached / 1024;
0312         ret = 0;
0313         break;
0314     case hwmon_in_min:
0315     case hwmon_in_max:
0316         reg = (attr == hwmon_in_min)
0317             ? ADT7411_REG_IN_LOW(channel)
0318             : ADT7411_REG_IN_HIGH(channel);
0319         ret = i2c_smbus_read_byte_data(client, reg);
0320         if (ret < 0)
0321             goto exit_unlock;
0322         *val = ret * data->vref_cached / 256;
0323         ret = 0;
0324         break;
0325     case hwmon_in_alarm:
0326         ret = adt7411_read_in_alarm(dev, channel, val);
0327         break;
0328     default:
0329         ret = -EOPNOTSUPP;
0330         break;
0331     }
0332  exit_unlock:
0333     mutex_unlock(&data->update_lock);
0334     return ret;
0335 }
0336 
0337 static int adt7411_read_in(struct device *dev, u32 attr, int channel,
0338                long *val)
0339 {
0340     if (channel == 0)
0341         return adt7411_read_in_vdd(dev, attr, val);
0342     else
0343         return adt7411_read_in_chan(dev, attr, channel, val);
0344 }
0345 
0346 
0347 static int adt7411_read_temp_alarm(struct device *dev, u32 attr, int channel,
0348                    long *val)
0349 {
0350     struct adt7411_data *data = dev_get_drvdata(dev);
0351     struct i2c_client *client = data->client;
0352     int ret, bit;
0353 
0354     ret = i2c_smbus_read_byte_data(client, ADT7411_REG_STAT_1);
0355     if (ret < 0)
0356         return ret;
0357 
0358     switch (attr) {
0359     case hwmon_temp_min_alarm:
0360         bit = channel ? ADT7411_STAT_1_EXT_TEMP_LOW
0361                   : ADT7411_STAT_1_INT_TEMP_LOW;
0362         break;
0363     case hwmon_temp_max_alarm:
0364         bit = channel ? ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1
0365                   : ADT7411_STAT_1_INT_TEMP_HIGH;
0366         break;
0367     case hwmon_temp_fault:
0368         bit = ADT7411_STAT_1_EXT_TEMP_FAULT;
0369         break;
0370     default:
0371         return -EOPNOTSUPP;
0372     }
0373 
0374     *val = !!(ret & bit);
0375     return 0;
0376 }
0377 
0378 static int adt7411_read_temp(struct device *dev, u32 attr, int channel,
0379                  long *val)
0380 {
0381     struct adt7411_data *data = dev_get_drvdata(dev);
0382     struct i2c_client *client = data->client;
0383     int ret, reg, regl, regh;
0384 
0385     switch (attr) {
0386     case hwmon_temp_input:
0387         regl = channel ? ADT7411_REG_EXT_TEMP_AIN14_LSB :
0388                  ADT7411_REG_INT_TEMP_VDD_LSB;
0389         regh = channel ? ADT7411_REG_EXT_TEMP_AIN1_MSB :
0390                  ADT7411_REG_INT_TEMP_MSB;
0391         ret = adt7411_read_10_bit(client, regl, regh, 0);
0392         if (ret < 0)
0393             return ret;
0394         ret = ret & 0x200 ? ret - 0x400 : ret; /* 10 bit signed */
0395         *val = ret * 250;
0396         return 0;
0397     case hwmon_temp_min:
0398     case hwmon_temp_max:
0399         reg = (attr == hwmon_temp_min)
0400             ? ADT7411_REG_TEMP_LOW(channel)
0401             : ADT7411_REG_TEMP_HIGH(channel);
0402         ret = i2c_smbus_read_byte_data(client, reg);
0403         if (ret < 0)
0404             return ret;
0405         ret = ret & 0x80 ? ret - 0x100 : ret; /* 8 bit signed */
0406         *val = ret * 1000;
0407         return 0;
0408     case hwmon_temp_min_alarm:
0409     case hwmon_temp_max_alarm:
0410     case hwmon_temp_fault:
0411         return adt7411_read_temp_alarm(dev, attr, channel, val);
0412     default:
0413         return -EOPNOTSUPP;
0414     }
0415 }
0416 
0417 static int adt7411_read(struct device *dev, enum hwmon_sensor_types type,
0418             u32 attr, int channel, long *val)
0419 {
0420     switch (type) {
0421     case hwmon_in:
0422         return adt7411_read_in(dev, attr, channel, val);
0423     case hwmon_temp:
0424         return adt7411_read_temp(dev, attr, channel, val);
0425     default:
0426         return -EOPNOTSUPP;
0427     }
0428 }
0429 
0430 static int adt7411_write_in_vdd(struct device *dev, u32 attr, long val)
0431 {
0432     struct adt7411_data *data = dev_get_drvdata(dev);
0433     struct i2c_client *client = data->client;
0434     int reg;
0435 
0436     val = clamp_val(val, 0, 255 * 7000 / 256);
0437     val = DIV_ROUND_CLOSEST(val * 256, 7000);
0438 
0439     switch (attr) {
0440     case hwmon_in_min:
0441         reg = ADT7411_REG_VDD_LOW;
0442         break;
0443     case hwmon_in_max:
0444         reg = ADT7411_REG_VDD_HIGH;
0445         break;
0446     default:
0447         return -EOPNOTSUPP;
0448     }
0449 
0450     return i2c_smbus_write_byte_data(client, reg, val);
0451 }
0452 
0453 static int adt7411_write_in_chan(struct device *dev, u32 attr, int channel,
0454                  long val)
0455 {
0456     struct adt7411_data *data = dev_get_drvdata(dev);
0457     struct i2c_client *client = data->client;
0458     int ret, reg;
0459 
0460     mutex_lock(&data->update_lock);
0461     ret = adt7411_update_vref(dev);
0462     if (ret < 0)
0463         goto exit_unlock;
0464     val = clamp_val(val, 0, 255 * data->vref_cached / 256);
0465     val = DIV_ROUND_CLOSEST(val * 256, data->vref_cached);
0466 
0467     switch (attr) {
0468     case hwmon_in_min:
0469         reg = ADT7411_REG_IN_LOW(channel);
0470         break;
0471     case hwmon_in_max:
0472         reg = ADT7411_REG_IN_HIGH(channel);
0473         break;
0474     default:
0475         ret = -EOPNOTSUPP;
0476         goto exit_unlock;
0477     }
0478 
0479     ret = i2c_smbus_write_byte_data(client, reg, val);
0480  exit_unlock:
0481     mutex_unlock(&data->update_lock);
0482     return ret;
0483 }
0484 
0485 static int adt7411_write_in(struct device *dev, u32 attr, int channel,
0486                 long val)
0487 {
0488     if (channel == 0)
0489         return adt7411_write_in_vdd(dev, attr, val);
0490     else
0491         return adt7411_write_in_chan(dev, attr, channel, val);
0492 }
0493 
0494 static int adt7411_write_temp(struct device *dev, u32 attr, int channel,
0495                   long val)
0496 {
0497     struct adt7411_data *data = dev_get_drvdata(dev);
0498     struct i2c_client *client = data->client;
0499     int reg;
0500 
0501     val = clamp_val(val, -128000, 127000);
0502     val = DIV_ROUND_CLOSEST(val, 1000);
0503 
0504     switch (attr) {
0505     case hwmon_temp_min:
0506         reg = ADT7411_REG_TEMP_LOW(channel);
0507         break;
0508     case hwmon_temp_max:
0509         reg = ADT7411_REG_TEMP_HIGH(channel);
0510         break;
0511     default:
0512         return -EOPNOTSUPP;
0513     }
0514 
0515     return i2c_smbus_write_byte_data(client, reg, val);
0516 }
0517 
0518 static int adt7411_write(struct device *dev, enum hwmon_sensor_types type,
0519              u32 attr, int channel, long val)
0520 {
0521     switch (type) {
0522     case hwmon_in:
0523         return adt7411_write_in(dev, attr, channel, val);
0524     case hwmon_temp:
0525         return adt7411_write_temp(dev, attr, channel, val);
0526     default:
0527         return -EOPNOTSUPP;
0528     }
0529 }
0530 
0531 static umode_t adt7411_is_visible(const void *_data,
0532                   enum hwmon_sensor_types type,
0533                   u32 attr, int channel)
0534 {
0535     const struct adt7411_data *data = _data;
0536     bool visible;
0537 
0538     switch (type) {
0539     case hwmon_in:
0540         visible = channel == 0 || channel >= 3 || !data->use_ext_temp;
0541         switch (attr) {
0542         case hwmon_in_input:
0543         case hwmon_in_alarm:
0544             return visible ? S_IRUGO : 0;
0545         case hwmon_in_min:
0546         case hwmon_in_max:
0547             return visible ? S_IRUGO | S_IWUSR : 0;
0548         }
0549         break;
0550     case hwmon_temp:
0551         visible = channel == 0 || data->use_ext_temp;
0552         switch (attr) {
0553         case hwmon_temp_input:
0554         case hwmon_temp_min_alarm:
0555         case hwmon_temp_max_alarm:
0556         case hwmon_temp_fault:
0557             return visible ? S_IRUGO : 0;
0558         case hwmon_temp_min:
0559         case hwmon_temp_max:
0560             return visible ? S_IRUGO | S_IWUSR : 0;
0561         }
0562         break;
0563     default:
0564         break;
0565     }
0566     return 0;
0567 }
0568 
0569 static int adt7411_detect(struct i2c_client *client,
0570               struct i2c_board_info *info)
0571 {
0572     int val;
0573 
0574     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0575         return -ENODEV;
0576 
0577     val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID);
0578     if (val < 0 || val != ADT7411_MANUFACTURER_ID) {
0579         dev_dbg(&client->dev,
0580             "Wrong manufacturer ID. Got %d, expected %d\n",
0581             val, ADT7411_MANUFACTURER_ID);
0582         return -ENODEV;
0583     }
0584 
0585     val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
0586     if (val < 0 || val != ADT7411_DEVICE_ID) {
0587         dev_dbg(&client->dev,
0588             "Wrong device ID. Got %d, expected %d\n",
0589             val, ADT7411_DEVICE_ID);
0590         return -ENODEV;
0591     }
0592 
0593     strlcpy(info->type, "adt7411", I2C_NAME_SIZE);
0594 
0595     return 0;
0596 }
0597 
0598 static int adt7411_init_device(struct adt7411_data *data)
0599 {
0600     int ret;
0601     u8 val;
0602 
0603     ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG3);
0604     if (ret < 0)
0605         return ret;
0606 
0607     /*
0608      * We must only write zero to bit 1 and bit 2 and only one to bit 3
0609      * according to the datasheet.
0610      */
0611     val = ret;
0612     val &= ~(ADT7411_CFG3_RESERVED_BIT1 | ADT7411_CFG3_RESERVED_BIT2);
0613     val |= ADT7411_CFG3_RESERVED_BIT3;
0614 
0615     ret = i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG3, val);
0616     if (ret < 0)
0617         return ret;
0618 
0619     ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG1);
0620     if (ret < 0)
0621         return ret;
0622 
0623     data->use_ext_temp = ret & ADT7411_CFG1_EXT_TDM;
0624 
0625     /*
0626      * We must only write zero to bit 1 and only one to bit 3 according to
0627      * the datasheet.
0628      */
0629     val = ret;
0630     val &= ~ADT7411_CFG1_RESERVED_BIT1;
0631     val |= ADT7411_CFG1_RESERVED_BIT3;
0632 
0633     /* enable monitoring */
0634     val |= ADT7411_CFG1_START_MONITOR;
0635 
0636     return i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG1, val);
0637 }
0638 
0639 static const struct hwmon_channel_info *adt7411_info[] = {
0640     HWMON_CHANNEL_INFO(in,
0641                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
0642                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
0643                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
0644                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
0645                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
0646                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
0647                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
0648                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
0649                HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM),
0650     HWMON_CHANNEL_INFO(temp,
0651                HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MIN_ALARM |
0652                HWMON_T_MAX | HWMON_T_MAX_ALARM,
0653                HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MIN_ALARM |
0654                HWMON_T_MAX | HWMON_T_MAX_ALARM | HWMON_T_FAULT),
0655     NULL
0656 };
0657 
0658 static const struct hwmon_ops adt7411_hwmon_ops = {
0659     .is_visible = adt7411_is_visible,
0660     .read = adt7411_read,
0661     .write = adt7411_write,
0662 };
0663 
0664 static const struct hwmon_chip_info adt7411_chip_info = {
0665     .ops = &adt7411_hwmon_ops,
0666     .info = adt7411_info,
0667 };
0668 
0669 static int adt7411_probe(struct i2c_client *client)
0670 {
0671     struct device *dev = &client->dev;
0672     struct adt7411_data *data;
0673     struct device *hwmon_dev;
0674     int ret;
0675 
0676     data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0677     if (!data)
0678         return -ENOMEM;
0679 
0680     i2c_set_clientdata(client, data);
0681     data->client = client;
0682     mutex_init(&data->device_lock);
0683     mutex_init(&data->update_lock);
0684 
0685     ret = adt7411_init_device(data);
0686     if (ret < 0)
0687         return ret;
0688 
0689     /* force update on first occasion */
0690     data->next_update = jiffies;
0691 
0692     hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
0693                              data,
0694                              &adt7411_chip_info,
0695                              adt7411_groups);
0696     return PTR_ERR_OR_ZERO(hwmon_dev);
0697 }
0698 
0699 static const struct i2c_device_id adt7411_id[] = {
0700     { "adt7411", 0 },
0701     { }
0702 };
0703 MODULE_DEVICE_TABLE(i2c, adt7411_id);
0704 
0705 static struct i2c_driver adt7411_driver = {
0706     .driver     = {
0707         .name       = "adt7411",
0708     },
0709     .probe_new = adt7411_probe,
0710     .id_table = adt7411_id,
0711     .detect = adt7411_detect,
0712     .address_list = normal_i2c,
0713     .class = I2C_CLASS_HWMON,
0714 };
0715 
0716 module_i2c_driver(adt7411_driver);
0717 
0718 MODULE_AUTHOR("Sascha Hauer, Wolfram Sang <kernel@pengutronix.de>");
0719 MODULE_DESCRIPTION("ADT7411 driver");
0720 MODULE_LICENSE("GPL v2");