Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * emc2103.c - Support for SMSC EMC2103
0004  * Copyright (c) 2010 SMSC
0005  */
0006 
0007 #include <linux/module.h>
0008 #include <linux/init.h>
0009 #include <linux/slab.h>
0010 #include <linux/jiffies.h>
0011 #include <linux/i2c.h>
0012 #include <linux/hwmon.h>
0013 #include <linux/hwmon-sysfs.h>
0014 #include <linux/err.h>
0015 #include <linux/mutex.h>
0016 
0017 /* Addresses scanned */
0018 static const unsigned short normal_i2c[] = { 0x2E, I2C_CLIENT_END };
0019 
0020 static const u8 REG_TEMP[4] = { 0x00, 0x02, 0x04, 0x06 };
0021 static const u8 REG_TEMP_MIN[4] = { 0x3c, 0x38, 0x39, 0x3a };
0022 static const u8 REG_TEMP_MAX[4] = { 0x34, 0x30, 0x31, 0x32 };
0023 
0024 #define REG_CONF1       0x20
0025 #define REG_TEMP_MAX_ALARM  0x24
0026 #define REG_TEMP_MIN_ALARM  0x25
0027 #define REG_FAN_CONF1       0x42
0028 #define REG_FAN_TARGET_LO   0x4c
0029 #define REG_FAN_TARGET_HI   0x4d
0030 #define REG_FAN_TACH_HI     0x4e
0031 #define REG_FAN_TACH_LO     0x4f
0032 #define REG_PRODUCT_ID      0xfd
0033 #define REG_MFG_ID      0xfe
0034 
0035 /* equation 4 from datasheet: rpm = (3932160 * multipler) / count */
0036 #define FAN_RPM_FACTOR      3932160
0037 
0038 /*
0039  * 2103-2 and 2103-4's 3rd temperature sensor can be connected to two diodes
0040  * in anti-parallel mode, and in this configuration both can be read
0041  * independently (so we have 4 temperature inputs).  The device can't
0042  * detect if it's connected in this mode, so we have to manually enable
0043  * it.  Default is to leave the device in the state it's already in (-1).
0044  * This parameter allows APD mode to be optionally forced on or off
0045  */
0046 static int apd = -1;
0047 module_param(apd, bint, 0);
0048 MODULE_PARM_DESC(apd, "Set to zero to disable anti-parallel diode mode");
0049 
0050 struct temperature {
0051     s8  degrees;
0052     u8  fraction;   /* 0-7 multiples of 0.125 */
0053 };
0054 
0055 struct emc2103_data {
0056     struct i2c_client   *client;
0057     const struct        attribute_group *groups[4];
0058     struct mutex        update_lock;
0059     bool            valid;      /* registers are valid */
0060     bool            fan_rpm_control;
0061     int         temp_count; /* num of temp sensors */
0062     unsigned long       last_updated;   /* in jiffies */
0063     struct temperature  temp[4];    /* internal + 3 external */
0064     s8          temp_min[4];    /* no fractional part */
0065     s8          temp_max[4];    /* no fractional part */
0066     u8          temp_min_alarm;
0067     u8          temp_max_alarm;
0068     u8          fan_multiplier;
0069     u16         fan_tach;
0070     u16         fan_target;
0071 };
0072 
0073 static int read_u8_from_i2c(struct i2c_client *client, u8 i2c_reg, u8 *output)
0074 {
0075     int status = i2c_smbus_read_byte_data(client, i2c_reg);
0076     if (status < 0) {
0077         dev_warn(&client->dev, "reg 0x%02x, err %d\n",
0078             i2c_reg, status);
0079     } else {
0080         *output = status;
0081     }
0082     return status;
0083 }
0084 
0085 static void read_temp_from_i2c(struct i2c_client *client, u8 i2c_reg,
0086                    struct temperature *temp)
0087 {
0088     u8 degrees, fractional;
0089 
0090     if (read_u8_from_i2c(client, i2c_reg, &degrees) < 0)
0091         return;
0092 
0093     if (read_u8_from_i2c(client, i2c_reg + 1, &fractional) < 0)
0094         return;
0095 
0096     temp->degrees = degrees;
0097     temp->fraction = (fractional & 0xe0) >> 5;
0098 }
0099 
0100 static void read_fan_from_i2c(struct i2c_client *client, u16 *output,
0101                   u8 hi_addr, u8 lo_addr)
0102 {
0103     u8 high_byte, lo_byte;
0104 
0105     if (read_u8_from_i2c(client, hi_addr, &high_byte) < 0)
0106         return;
0107 
0108     if (read_u8_from_i2c(client, lo_addr, &lo_byte) < 0)
0109         return;
0110 
0111     *output = ((u16)high_byte << 5) | (lo_byte >> 3);
0112 }
0113 
0114 static void write_fan_target_to_i2c(struct i2c_client *client, u16 new_target)
0115 {
0116     u8 high_byte = (new_target & 0x1fe0) >> 5;
0117     u8 low_byte = (new_target & 0x001f) << 3;
0118     i2c_smbus_write_byte_data(client, REG_FAN_TARGET_LO, low_byte);
0119     i2c_smbus_write_byte_data(client, REG_FAN_TARGET_HI, high_byte);
0120 }
0121 
0122 static void read_fan_config_from_i2c(struct i2c_client *client)
0123 
0124 {
0125     struct emc2103_data *data = i2c_get_clientdata(client);
0126     u8 conf1;
0127 
0128     if (read_u8_from_i2c(client, REG_FAN_CONF1, &conf1) < 0)
0129         return;
0130 
0131     data->fan_multiplier = 1 << ((conf1 & 0x60) >> 5);
0132     data->fan_rpm_control = (conf1 & 0x80) != 0;
0133 }
0134 
0135 static struct emc2103_data *emc2103_update_device(struct device *dev)
0136 {
0137     struct emc2103_data *data = dev_get_drvdata(dev);
0138     struct i2c_client *client = data->client;
0139 
0140     mutex_lock(&data->update_lock);
0141 
0142     if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
0143         || !data->valid) {
0144         int i;
0145 
0146         for (i = 0; i < data->temp_count; i++) {
0147             read_temp_from_i2c(client, REG_TEMP[i], &data->temp[i]);
0148             read_u8_from_i2c(client, REG_TEMP_MIN[i],
0149                 &data->temp_min[i]);
0150             read_u8_from_i2c(client, REG_TEMP_MAX[i],
0151                 &data->temp_max[i]);
0152         }
0153 
0154         read_u8_from_i2c(client, REG_TEMP_MIN_ALARM,
0155             &data->temp_min_alarm);
0156         read_u8_from_i2c(client, REG_TEMP_MAX_ALARM,
0157             &data->temp_max_alarm);
0158 
0159         read_fan_from_i2c(client, &data->fan_tach,
0160             REG_FAN_TACH_HI, REG_FAN_TACH_LO);
0161         read_fan_from_i2c(client, &data->fan_target,
0162             REG_FAN_TARGET_HI, REG_FAN_TARGET_LO);
0163         read_fan_config_from_i2c(client);
0164 
0165         data->last_updated = jiffies;
0166         data->valid = true;
0167     }
0168 
0169     mutex_unlock(&data->update_lock);
0170 
0171     return data;
0172 }
0173 
0174 static ssize_t
0175 temp_show(struct device *dev, struct device_attribute *da, char *buf)
0176 {
0177     int nr = to_sensor_dev_attr(da)->index;
0178     struct emc2103_data *data = emc2103_update_device(dev);
0179     int millidegrees = data->temp[nr].degrees * 1000
0180         + data->temp[nr].fraction * 125;
0181     return sprintf(buf, "%d\n", millidegrees);
0182 }
0183 
0184 static ssize_t
0185 temp_min_show(struct device *dev, struct device_attribute *da, char *buf)
0186 {
0187     int nr = to_sensor_dev_attr(da)->index;
0188     struct emc2103_data *data = emc2103_update_device(dev);
0189     int millidegrees = data->temp_min[nr] * 1000;
0190     return sprintf(buf, "%d\n", millidegrees);
0191 }
0192 
0193 static ssize_t
0194 temp_max_show(struct device *dev, struct device_attribute *da, char *buf)
0195 {
0196     int nr = to_sensor_dev_attr(da)->index;
0197     struct emc2103_data *data = emc2103_update_device(dev);
0198     int millidegrees = data->temp_max[nr] * 1000;
0199     return sprintf(buf, "%d\n", millidegrees);
0200 }
0201 
0202 static ssize_t
0203 temp_fault_show(struct device *dev, struct device_attribute *da, char *buf)
0204 {
0205     int nr = to_sensor_dev_attr(da)->index;
0206     struct emc2103_data *data = emc2103_update_device(dev);
0207     bool fault = (data->temp[nr].degrees == -128);
0208     return sprintf(buf, "%d\n", fault ? 1 : 0);
0209 }
0210 
0211 static ssize_t
0212 temp_min_alarm_show(struct device *dev, struct device_attribute *da,
0213             char *buf)
0214 {
0215     int nr = to_sensor_dev_attr(da)->index;
0216     struct emc2103_data *data = emc2103_update_device(dev);
0217     bool alarm = data->temp_min_alarm & (1 << nr);
0218     return sprintf(buf, "%d\n", alarm ? 1 : 0);
0219 }
0220 
0221 static ssize_t
0222 temp_max_alarm_show(struct device *dev, struct device_attribute *da,
0223             char *buf)
0224 {
0225     int nr = to_sensor_dev_attr(da)->index;
0226     struct emc2103_data *data = emc2103_update_device(dev);
0227     bool alarm = data->temp_max_alarm & (1 << nr);
0228     return sprintf(buf, "%d\n", alarm ? 1 : 0);
0229 }
0230 
0231 static ssize_t temp_min_store(struct device *dev, struct device_attribute *da,
0232                   const char *buf, size_t count)
0233 {
0234     int nr = to_sensor_dev_attr(da)->index;
0235     struct emc2103_data *data = dev_get_drvdata(dev);
0236     struct i2c_client *client = data->client;
0237     long val;
0238 
0239     int result = kstrtol(buf, 10, &val);
0240     if (result < 0)
0241         return result;
0242 
0243     val = DIV_ROUND_CLOSEST(clamp_val(val, -63000, 127000), 1000);
0244 
0245     mutex_lock(&data->update_lock);
0246     data->temp_min[nr] = val;
0247     i2c_smbus_write_byte_data(client, REG_TEMP_MIN[nr], val);
0248     mutex_unlock(&data->update_lock);
0249 
0250     return count;
0251 }
0252 
0253 static ssize_t temp_max_store(struct device *dev, struct device_attribute *da,
0254                   const char *buf, size_t count)
0255 {
0256     int nr = to_sensor_dev_attr(da)->index;
0257     struct emc2103_data *data = dev_get_drvdata(dev);
0258     struct i2c_client *client = data->client;
0259     long val;
0260 
0261     int result = kstrtol(buf, 10, &val);
0262     if (result < 0)
0263         return result;
0264 
0265     val = DIV_ROUND_CLOSEST(clamp_val(val, -63000, 127000), 1000);
0266 
0267     mutex_lock(&data->update_lock);
0268     data->temp_max[nr] = val;
0269     i2c_smbus_write_byte_data(client, REG_TEMP_MAX[nr], val);
0270     mutex_unlock(&data->update_lock);
0271 
0272     return count;
0273 }
0274 
0275 static ssize_t
0276 fan1_input_show(struct device *dev, struct device_attribute *da, char *buf)
0277 {
0278     struct emc2103_data *data = emc2103_update_device(dev);
0279     int rpm = 0;
0280     if (data->fan_tach != 0)
0281         rpm = (FAN_RPM_FACTOR * data->fan_multiplier) / data->fan_tach;
0282     return sprintf(buf, "%d\n", rpm);
0283 }
0284 
0285 static ssize_t
0286 fan1_div_show(struct device *dev, struct device_attribute *da, char *buf)
0287 {
0288     struct emc2103_data *data = emc2103_update_device(dev);
0289     int fan_div = 8 / data->fan_multiplier;
0290     return sprintf(buf, "%d\n", fan_div);
0291 }
0292 
0293 /*
0294  * Note: we also update the fan target here, because its value is
0295  * determined in part by the fan clock divider.  This follows the principle
0296  * of least surprise; the user doesn't expect the fan target to change just
0297  * because the divider changed.
0298  */
0299 static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,
0300                   const char *buf, size_t count)
0301 {
0302     struct emc2103_data *data = emc2103_update_device(dev);
0303     struct i2c_client *client = data->client;
0304     int new_range_bits, old_div = 8 / data->fan_multiplier;
0305     long new_div;
0306 
0307     int status = kstrtol(buf, 10, &new_div);
0308     if (status < 0)
0309         return status;
0310 
0311     if (new_div == old_div) /* No change */
0312         return count;
0313 
0314     switch (new_div) {
0315     case 1:
0316         new_range_bits = 3;
0317         break;
0318     case 2:
0319         new_range_bits = 2;
0320         break;
0321     case 4:
0322         new_range_bits = 1;
0323         break;
0324     case 8:
0325         new_range_bits = 0;
0326         break;
0327     default:
0328         return -EINVAL;
0329     }
0330 
0331     mutex_lock(&data->update_lock);
0332 
0333     status = i2c_smbus_read_byte_data(client, REG_FAN_CONF1);
0334     if (status < 0) {
0335         dev_dbg(&client->dev, "reg 0x%02x, err %d\n",
0336             REG_FAN_CONF1, status);
0337         mutex_unlock(&data->update_lock);
0338         return status;
0339     }
0340     status &= 0x9F;
0341     status |= (new_range_bits << 5);
0342     i2c_smbus_write_byte_data(client, REG_FAN_CONF1, status);
0343 
0344     data->fan_multiplier = 8 / new_div;
0345 
0346     /* update fan target if high byte is not disabled */
0347     if ((data->fan_target & 0x1fe0) != 0x1fe0) {
0348         u16 new_target = (data->fan_target * old_div) / new_div;
0349         data->fan_target = min(new_target, (u16)0x1fff);
0350         write_fan_target_to_i2c(client, data->fan_target);
0351     }
0352 
0353     /* invalidate data to force re-read from hardware */
0354     data->valid = false;
0355 
0356     mutex_unlock(&data->update_lock);
0357     return count;
0358 }
0359 
0360 static ssize_t
0361 fan1_target_show(struct device *dev, struct device_attribute *da, char *buf)
0362 {
0363     struct emc2103_data *data = emc2103_update_device(dev);
0364     int rpm = 0;
0365 
0366     /* high byte of 0xff indicates disabled so return 0 */
0367     if ((data->fan_target != 0) && ((data->fan_target & 0x1fe0) != 0x1fe0))
0368         rpm = (FAN_RPM_FACTOR * data->fan_multiplier)
0369             / data->fan_target;
0370 
0371     return sprintf(buf, "%d\n", rpm);
0372 }
0373 
0374 static ssize_t fan1_target_store(struct device *dev,
0375                  struct device_attribute *da, const char *buf,
0376                  size_t count)
0377 {
0378     struct emc2103_data *data = emc2103_update_device(dev);
0379     struct i2c_client *client = data->client;
0380     unsigned long rpm_target;
0381 
0382     int result = kstrtoul(buf, 10, &rpm_target);
0383     if (result < 0)
0384         return result;
0385 
0386     /* Datasheet states 16384 as maximum RPM target (table 3.2) */
0387     rpm_target = clamp_val(rpm_target, 0, 16384);
0388 
0389     mutex_lock(&data->update_lock);
0390 
0391     if (rpm_target == 0)
0392         data->fan_target = 0x1fff;
0393     else
0394         data->fan_target = clamp_val(
0395             (FAN_RPM_FACTOR * data->fan_multiplier) / rpm_target,
0396             0, 0x1fff);
0397 
0398     write_fan_target_to_i2c(client, data->fan_target);
0399 
0400     mutex_unlock(&data->update_lock);
0401     return count;
0402 }
0403 
0404 static ssize_t
0405 fan1_fault_show(struct device *dev, struct device_attribute *da, char *buf)
0406 {
0407     struct emc2103_data *data = emc2103_update_device(dev);
0408     bool fault = ((data->fan_tach & 0x1fe0) == 0x1fe0);
0409     return sprintf(buf, "%d\n", fault ? 1 : 0);
0410 }
0411 
0412 static ssize_t
0413 pwm1_enable_show(struct device *dev, struct device_attribute *da, char *buf)
0414 {
0415     struct emc2103_data *data = emc2103_update_device(dev);
0416     return sprintf(buf, "%d\n", data->fan_rpm_control ? 3 : 0);
0417 }
0418 
0419 static ssize_t pwm1_enable_store(struct device *dev,
0420                  struct device_attribute *da, const char *buf,
0421                  size_t count)
0422 {
0423     struct emc2103_data *data = dev_get_drvdata(dev);
0424     struct i2c_client *client = data->client;
0425     long new_value;
0426     u8 conf_reg;
0427 
0428     int result = kstrtol(buf, 10, &new_value);
0429     if (result < 0)
0430         return result;
0431 
0432     mutex_lock(&data->update_lock);
0433     switch (new_value) {
0434     case 0:
0435         data->fan_rpm_control = false;
0436         break;
0437     case 3:
0438         data->fan_rpm_control = true;
0439         break;
0440     default:
0441         count = -EINVAL;
0442         goto err;
0443     }
0444 
0445     result = read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
0446     if (result < 0) {
0447         count = result;
0448         goto err;
0449     }
0450 
0451     if (data->fan_rpm_control)
0452         conf_reg |= 0x80;
0453     else
0454         conf_reg &= ~0x80;
0455 
0456     i2c_smbus_write_byte_data(client, REG_FAN_CONF1, conf_reg);
0457 err:
0458     mutex_unlock(&data->update_lock);
0459     return count;
0460 }
0461 
0462 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
0463 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
0464 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
0465 static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0);
0466 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, temp_min_alarm, 0);
0467 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, temp_max_alarm, 0);
0468 
0469 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
0470 static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
0471 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
0472 static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1);
0473 static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, temp_min_alarm, 1);
0474 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, temp_max_alarm, 1);
0475 
0476 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
0477 static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
0478 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
0479 static SENSOR_DEVICE_ATTR_RO(temp3_fault, temp_fault, 2);
0480 static SENSOR_DEVICE_ATTR_RO(temp3_min_alarm, temp_min_alarm, 2);
0481 static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, temp_max_alarm, 2);
0482 
0483 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
0484 static SENSOR_DEVICE_ATTR_RW(temp4_min, temp_min, 3);
0485 static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
0486 static SENSOR_DEVICE_ATTR_RO(temp4_fault, temp_fault, 3);
0487 static SENSOR_DEVICE_ATTR_RO(temp4_min_alarm, temp_min_alarm, 3);
0488 static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, temp_max_alarm, 3);
0489 
0490 static DEVICE_ATTR_RO(fan1_input);
0491 static DEVICE_ATTR_RW(fan1_div);
0492 static DEVICE_ATTR_RW(fan1_target);
0493 static DEVICE_ATTR_RO(fan1_fault);
0494 
0495 static DEVICE_ATTR_RW(pwm1_enable);
0496 
0497 /* sensors present on all models */
0498 static struct attribute *emc2103_attributes[] = {
0499     &sensor_dev_attr_temp1_input.dev_attr.attr,
0500     &sensor_dev_attr_temp1_min.dev_attr.attr,
0501     &sensor_dev_attr_temp1_max.dev_attr.attr,
0502     &sensor_dev_attr_temp1_fault.dev_attr.attr,
0503     &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
0504     &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
0505     &sensor_dev_attr_temp2_input.dev_attr.attr,
0506     &sensor_dev_attr_temp2_min.dev_attr.attr,
0507     &sensor_dev_attr_temp2_max.dev_attr.attr,
0508     &sensor_dev_attr_temp2_fault.dev_attr.attr,
0509     &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
0510     &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
0511     &dev_attr_fan1_input.attr,
0512     &dev_attr_fan1_div.attr,
0513     &dev_attr_fan1_target.attr,
0514     &dev_attr_fan1_fault.attr,
0515     &dev_attr_pwm1_enable.attr,
0516     NULL
0517 };
0518 
0519 /* extra temperature sensors only present on 2103-2 and 2103-4 */
0520 static struct attribute *emc2103_attributes_temp3[] = {
0521     &sensor_dev_attr_temp3_input.dev_attr.attr,
0522     &sensor_dev_attr_temp3_min.dev_attr.attr,
0523     &sensor_dev_attr_temp3_max.dev_attr.attr,
0524     &sensor_dev_attr_temp3_fault.dev_attr.attr,
0525     &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
0526     &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
0527     NULL
0528 };
0529 
0530 /* extra temperature sensors only present on 2103-2 and 2103-4 in APD mode */
0531 static struct attribute *emc2103_attributes_temp4[] = {
0532     &sensor_dev_attr_temp4_input.dev_attr.attr,
0533     &sensor_dev_attr_temp4_min.dev_attr.attr,
0534     &sensor_dev_attr_temp4_max.dev_attr.attr,
0535     &sensor_dev_attr_temp4_fault.dev_attr.attr,
0536     &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
0537     &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
0538     NULL
0539 };
0540 
0541 static const struct attribute_group emc2103_group = {
0542     .attrs = emc2103_attributes,
0543 };
0544 
0545 static const struct attribute_group emc2103_temp3_group = {
0546     .attrs = emc2103_attributes_temp3,
0547 };
0548 
0549 static const struct attribute_group emc2103_temp4_group = {
0550     .attrs = emc2103_attributes_temp4,
0551 };
0552 
0553 static int
0554 emc2103_probe(struct i2c_client *client)
0555 {
0556     struct emc2103_data *data;
0557     struct device *hwmon_dev;
0558     int status, idx = 0;
0559 
0560     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0561         return -EIO;
0562 
0563     data = devm_kzalloc(&client->dev, sizeof(struct emc2103_data),
0564                 GFP_KERNEL);
0565     if (!data)
0566         return -ENOMEM;
0567 
0568     i2c_set_clientdata(client, data);
0569     data->client = client;
0570     mutex_init(&data->update_lock);
0571 
0572     /* 2103-2 and 2103-4 have 3 external diodes, 2103-1 has 1 */
0573     status = i2c_smbus_read_byte_data(client, REG_PRODUCT_ID);
0574     if (status == 0x24) {
0575         /* 2103-1 only has 1 external diode */
0576         data->temp_count = 2;
0577     } else {
0578         /* 2103-2 and 2103-4 have 3 or 4 external diodes */
0579         status = i2c_smbus_read_byte_data(client, REG_CONF1);
0580         if (status < 0) {
0581             dev_dbg(&client->dev, "reg 0x%02x, err %d\n", REG_CONF1,
0582                 status);
0583             return status;
0584         }
0585 
0586         /* detect current state of hardware */
0587         data->temp_count = (status & 0x01) ? 4 : 3;
0588 
0589         /* force APD state if module parameter is set */
0590         if (apd == 0) {
0591             /* force APD mode off */
0592             data->temp_count = 3;
0593             status &= ~(0x01);
0594             i2c_smbus_write_byte_data(client, REG_CONF1, status);
0595         } else if (apd == 1) {
0596             /* force APD mode on */
0597             data->temp_count = 4;
0598             status |= 0x01;
0599             i2c_smbus_write_byte_data(client, REG_CONF1, status);
0600         }
0601     }
0602 
0603     /* sysfs hooks */
0604     data->groups[idx++] = &emc2103_group;
0605     if (data->temp_count >= 3)
0606         data->groups[idx++] = &emc2103_temp3_group;
0607     if (data->temp_count == 4)
0608         data->groups[idx++] = &emc2103_temp4_group;
0609 
0610     hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
0611                                client->name, data,
0612                                data->groups);
0613     if (IS_ERR(hwmon_dev))
0614         return PTR_ERR(hwmon_dev);
0615 
0616     dev_info(&client->dev, "%s: sensor '%s'\n",
0617          dev_name(hwmon_dev), client->name);
0618 
0619     return 0;
0620 }
0621 
0622 static const struct i2c_device_id emc2103_ids[] = {
0623     { "emc2103", 0, },
0624     { /* LIST END */ }
0625 };
0626 MODULE_DEVICE_TABLE(i2c, emc2103_ids);
0627 
0628 /* Return 0 if detection is successful, -ENODEV otherwise */
0629 static int
0630 emc2103_detect(struct i2c_client *new_client, struct i2c_board_info *info)
0631 {
0632     struct i2c_adapter *adapter = new_client->adapter;
0633     int manufacturer, product;
0634 
0635     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0636         return -ENODEV;
0637 
0638     manufacturer = i2c_smbus_read_byte_data(new_client, REG_MFG_ID);
0639     if (manufacturer != 0x5D)
0640         return -ENODEV;
0641 
0642     product = i2c_smbus_read_byte_data(new_client, REG_PRODUCT_ID);
0643     if ((product != 0x24) && (product != 0x26))
0644         return -ENODEV;
0645 
0646     strlcpy(info->type, "emc2103", I2C_NAME_SIZE);
0647 
0648     return 0;
0649 }
0650 
0651 static struct i2c_driver emc2103_driver = {
0652     .class      = I2C_CLASS_HWMON,
0653     .driver = {
0654         .name   = "emc2103",
0655     },
0656     .probe_new  = emc2103_probe,
0657     .id_table   = emc2103_ids,
0658     .detect     = emc2103_detect,
0659     .address_list   = normal_i2c,
0660 };
0661 
0662 module_i2c_driver(emc2103_driver);
0663 
0664 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
0665 MODULE_DESCRIPTION("SMSC EMC2103 hwmon driver");
0666 MODULE_LICENSE("GPL");