Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * adm1029.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
0004  *
0005  * Copyright (C) 2006 Corentin LABBE <clabbe.montjoie@gmail.com>
0006  *
0007  * Based on LM83 Driver by Jean Delvare <jdelvare@suse.de>
0008  *
0009  * Give only processor, motherboard temperatures and fan tachs
0010  * Very rare chip please let me know if you use it
0011  *
0012  * http://www.analog.com/UploadedFiles/Data_Sheets/ADM1029.pdf
0013  */
0014 
0015 #include <linux/module.h>
0016 #include <linux/init.h>
0017 #include <linux/slab.h>
0018 #include <linux/jiffies.h>
0019 #include <linux/i2c.h>
0020 #include <linux/hwmon-sysfs.h>
0021 #include <linux/hwmon.h>
0022 #include <linux/err.h>
0023 #include <linux/mutex.h>
0024 
0025 /*
0026  * Addresses to scan
0027  */
0028 
0029 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
0030                         0x2e, 0x2f, I2C_CLIENT_END
0031 };
0032 
0033 /*
0034  * The ADM1029 registers
0035  * Manufacturer ID is 0x41 for Analog Devices
0036  */
0037 
0038 #define ADM1029_REG_MAN_ID          0x0D
0039 #define ADM1029_REG_CHIP_ID         0x0E
0040 #define ADM1029_REG_CONFIG          0x01
0041 #define ADM1029_REG_NB_FAN_SUPPORT      0x02
0042 
0043 #define ADM1029_REG_TEMP_DEVICES_INSTALLED  0x06
0044 
0045 #define ADM1029_REG_LOCAL_TEMP          0xA0
0046 #define ADM1029_REG_REMOTE1_TEMP        0xA1
0047 #define ADM1029_REG_REMOTE2_TEMP        0xA2
0048 
0049 #define ADM1029_REG_LOCAL_TEMP_HIGH     0x90
0050 #define ADM1029_REG_REMOTE1_TEMP_HIGH       0x91
0051 #define ADM1029_REG_REMOTE2_TEMP_HIGH       0x92
0052 
0053 #define ADM1029_REG_LOCAL_TEMP_LOW      0x98
0054 #define ADM1029_REG_REMOTE1_TEMP_LOW        0x99
0055 #define ADM1029_REG_REMOTE2_TEMP_LOW        0x9A
0056 
0057 #define ADM1029_REG_FAN1            0x70
0058 #define ADM1029_REG_FAN2            0x71
0059 
0060 #define ADM1029_REG_FAN1_MIN            0x78
0061 #define ADM1029_REG_FAN2_MIN            0x79
0062 
0063 #define ADM1029_REG_FAN1_CONFIG         0x68
0064 #define ADM1029_REG_FAN2_CONFIG         0x69
0065 
0066 #define TEMP_FROM_REG(val)  ((val) * 1000)
0067 
0068 #define DIV_FROM_REG(val)   (1 << (((val) >> 6) - 1))
0069 
0070 /* Registers to be checked by adm1029_update_device() */
0071 static const u8 ADM1029_REG_TEMP[] = {
0072     ADM1029_REG_LOCAL_TEMP,
0073     ADM1029_REG_REMOTE1_TEMP,
0074     ADM1029_REG_REMOTE2_TEMP,
0075     ADM1029_REG_LOCAL_TEMP_HIGH,
0076     ADM1029_REG_REMOTE1_TEMP_HIGH,
0077     ADM1029_REG_REMOTE2_TEMP_HIGH,
0078     ADM1029_REG_LOCAL_TEMP_LOW,
0079     ADM1029_REG_REMOTE1_TEMP_LOW,
0080     ADM1029_REG_REMOTE2_TEMP_LOW,
0081 };
0082 
0083 static const u8 ADM1029_REG_FAN[] = {
0084     ADM1029_REG_FAN1,
0085     ADM1029_REG_FAN2,
0086     ADM1029_REG_FAN1_MIN,
0087     ADM1029_REG_FAN2_MIN,
0088 };
0089 
0090 static const u8 ADM1029_REG_FAN_DIV[] = {
0091     ADM1029_REG_FAN1_CONFIG,
0092     ADM1029_REG_FAN2_CONFIG,
0093 };
0094 
0095 /*
0096  * Client data (each client gets its own)
0097  */
0098 
0099 struct adm1029_data {
0100     struct i2c_client *client;
0101     struct mutex update_lock; /* protect register access */
0102     bool valid;     /* false until following fields are valid */
0103     unsigned long last_updated; /* in jiffies */
0104 
0105     /* registers values, signed for temperature, unsigned for other stuff */
0106     s8 temp[ARRAY_SIZE(ADM1029_REG_TEMP)];
0107     u8 fan[ARRAY_SIZE(ADM1029_REG_FAN)];
0108     u8 fan_div[ARRAY_SIZE(ADM1029_REG_FAN_DIV)];
0109 };
0110 
0111 /*
0112  * function that update the status of the chips (temperature for example)
0113  */
0114 static struct adm1029_data *adm1029_update_device(struct device *dev)
0115 {
0116     struct adm1029_data *data = dev_get_drvdata(dev);
0117     struct i2c_client *client = data->client;
0118 
0119     mutex_lock(&data->update_lock);
0120     /*
0121      * Use the "cache" Luke, don't recheck values
0122      * if there are already checked not a long time later
0123      */
0124     if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
0125         int nr;
0126 
0127         dev_dbg(&client->dev, "Updating adm1029 data\n");
0128 
0129         for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_TEMP); nr++) {
0130             data->temp[nr] =
0131                 i2c_smbus_read_byte_data(client,
0132                              ADM1029_REG_TEMP[nr]);
0133         }
0134         for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN); nr++) {
0135             data->fan[nr] =
0136                 i2c_smbus_read_byte_data(client,
0137                              ADM1029_REG_FAN[nr]);
0138         }
0139         for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN_DIV); nr++) {
0140             data->fan_div[nr] =
0141                 i2c_smbus_read_byte_data(client,
0142                              ADM1029_REG_FAN_DIV[nr]);
0143         }
0144 
0145         data->last_updated = jiffies;
0146         data->valid = true;
0147     }
0148 
0149     mutex_unlock(&data->update_lock);
0150 
0151     return data;
0152 }
0153 
0154 /*
0155  * Sysfs stuff
0156  */
0157 
0158 static ssize_t
0159 temp_show(struct device *dev, struct device_attribute *devattr, char *buf)
0160 {
0161     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0162     struct adm1029_data *data = adm1029_update_device(dev);
0163 
0164     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
0165 }
0166 
0167 static ssize_t
0168 fan_show(struct device *dev, struct device_attribute *devattr, char *buf)
0169 {
0170     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0171     struct adm1029_data *data = adm1029_update_device(dev);
0172     u16 val;
0173 
0174     if (data->fan[attr->index] == 0 ||
0175         (data->fan_div[attr->index] & 0xC0) == 0 ||
0176         data->fan[attr->index] == 255) {
0177         return sprintf(buf, "0\n");
0178     }
0179 
0180     val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index])
0181         / data->fan[attr->index];
0182     return sprintf(buf, "%d\n", val);
0183 }
0184 
0185 static ssize_t
0186 fan_div_show(struct device *dev, struct device_attribute *devattr, char *buf)
0187 {
0188     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0189     struct adm1029_data *data = adm1029_update_device(dev);
0190 
0191     if ((data->fan_div[attr->index] & 0xC0) == 0)
0192         return sprintf(buf, "0\n");
0193     return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
0194 }
0195 
0196 static ssize_t fan_div_store(struct device *dev,
0197                  struct device_attribute *devattr,
0198                  const char *buf, size_t count)
0199 {
0200     struct adm1029_data *data = dev_get_drvdata(dev);
0201     struct i2c_client *client = data->client;
0202     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0203     u8 reg;
0204     long val;
0205     int ret = kstrtol(buf, 10, &val);
0206 
0207     if (ret < 0)
0208         return ret;
0209 
0210     mutex_lock(&data->update_lock);
0211 
0212     /*Read actual config */
0213     reg = i2c_smbus_read_byte_data(client,
0214                        ADM1029_REG_FAN_DIV[attr->index]);
0215 
0216     switch (val) {
0217     case 1:
0218         val = 1;
0219         break;
0220     case 2:
0221         val = 2;
0222         break;
0223     case 4:
0224         val = 3;
0225         break;
0226     default:
0227         mutex_unlock(&data->update_lock);
0228         dev_err(&client->dev,
0229             "fan_div value %ld not supported. Choose one of 1, 2 or 4!\n",
0230             val);
0231         return -EINVAL;
0232     }
0233     /* Update the value */
0234     reg = (reg & 0x3F) | (val << 6);
0235 
0236     /* Update the cache */
0237     data->fan_div[attr->index] = reg;
0238 
0239     /* Write value */
0240     i2c_smbus_write_byte_data(client,
0241                   ADM1029_REG_FAN_DIV[attr->index], reg);
0242     mutex_unlock(&data->update_lock);
0243 
0244     return count;
0245 }
0246 
0247 /* Access rights on sysfs. */
0248 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
0249 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
0250 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
0251 
0252 static SENSOR_DEVICE_ATTR_RO(temp1_max, temp, 3);
0253 static SENSOR_DEVICE_ATTR_RO(temp2_max, temp, 4);
0254 static SENSOR_DEVICE_ATTR_RO(temp3_max, temp, 5);
0255 
0256 static SENSOR_DEVICE_ATTR_RO(temp1_min, temp, 6);
0257 static SENSOR_DEVICE_ATTR_RO(temp2_min, temp, 7);
0258 static SENSOR_DEVICE_ATTR_RO(temp3_min, temp, 8);
0259 
0260 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
0261 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
0262 
0263 static SENSOR_DEVICE_ATTR_RO(fan1_min, fan, 2);
0264 static SENSOR_DEVICE_ATTR_RO(fan2_min, fan, 3);
0265 
0266 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
0267 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
0268 
0269 static struct attribute *adm1029_attrs[] = {
0270     &sensor_dev_attr_temp1_input.dev_attr.attr,
0271     &sensor_dev_attr_temp1_min.dev_attr.attr,
0272     &sensor_dev_attr_temp1_max.dev_attr.attr,
0273     &sensor_dev_attr_temp2_input.dev_attr.attr,
0274     &sensor_dev_attr_temp2_min.dev_attr.attr,
0275     &sensor_dev_attr_temp2_max.dev_attr.attr,
0276     &sensor_dev_attr_temp3_input.dev_attr.attr,
0277     &sensor_dev_attr_temp3_min.dev_attr.attr,
0278     &sensor_dev_attr_temp3_max.dev_attr.attr,
0279     &sensor_dev_attr_fan1_input.dev_attr.attr,
0280     &sensor_dev_attr_fan2_input.dev_attr.attr,
0281     &sensor_dev_attr_fan1_min.dev_attr.attr,
0282     &sensor_dev_attr_fan2_min.dev_attr.attr,
0283     &sensor_dev_attr_fan1_div.dev_attr.attr,
0284     &sensor_dev_attr_fan2_div.dev_attr.attr,
0285     NULL
0286 };
0287 
0288 ATTRIBUTE_GROUPS(adm1029);
0289 
0290 /*
0291  * Real code
0292  */
0293 
0294 /* Return 0 if detection is successful, -ENODEV otherwise */
0295 static int adm1029_detect(struct i2c_client *client,
0296               struct i2c_board_info *info)
0297 {
0298     struct i2c_adapter *adapter = client->adapter;
0299     u8 man_id, chip_id, temp_devices_installed, nb_fan_support;
0300 
0301     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0302         return -ENODEV;
0303 
0304     /*
0305      * ADM1029 doesn't have CHIP ID, check just MAN ID
0306      * For better detection we check also ADM1029_TEMP_DEVICES_INSTALLED,
0307      * ADM1029_REG_NB_FAN_SUPPORT and compare it with possible values
0308      * documented
0309      */
0310 
0311     man_id = i2c_smbus_read_byte_data(client, ADM1029_REG_MAN_ID);
0312     chip_id = i2c_smbus_read_byte_data(client, ADM1029_REG_CHIP_ID);
0313     temp_devices_installed = i2c_smbus_read_byte_data(client,
0314                     ADM1029_REG_TEMP_DEVICES_INSTALLED);
0315     nb_fan_support = i2c_smbus_read_byte_data(client,
0316                           ADM1029_REG_NB_FAN_SUPPORT);
0317     /* 0x41 is Analog Devices */
0318     if (man_id != 0x41 || (temp_devices_installed & 0xf9) != 0x01 ||
0319         nb_fan_support != 0x03)
0320         return -ENODEV;
0321 
0322     if ((chip_id & 0xF0) != 0x00) {
0323         /*
0324          * There are no "official" CHIP ID, so actually
0325          * we use Major/Minor revision for that
0326          */
0327         pr_info("Unknown major revision %x, please let us know\n",
0328             chip_id);
0329         return -ENODEV;
0330     }
0331 
0332     strlcpy(info->type, "adm1029", I2C_NAME_SIZE);
0333 
0334     return 0;
0335 }
0336 
0337 static int adm1029_init_client(struct i2c_client *client)
0338 {
0339     u8 config;
0340 
0341     config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
0342     if ((config & 0x10) == 0) {
0343         i2c_smbus_write_byte_data(client, ADM1029_REG_CONFIG,
0344                       config | 0x10);
0345     }
0346     /* recheck config */
0347     config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
0348     if ((config & 0x10) == 0) {
0349         dev_err(&client->dev, "Initialization failed!\n");
0350         return 0;
0351     }
0352     return 1;
0353 }
0354 
0355 static int adm1029_probe(struct i2c_client *client)
0356 {
0357     struct device *dev = &client->dev;
0358     struct adm1029_data *data;
0359     struct device *hwmon_dev;
0360 
0361     data = devm_kzalloc(dev, sizeof(struct adm1029_data), GFP_KERNEL);
0362     if (!data)
0363         return -ENOMEM;
0364 
0365     data->client = client;
0366     mutex_init(&data->update_lock);
0367 
0368     /*
0369      * Initialize the ADM1029 chip
0370      * Check config register
0371      */
0372     if (adm1029_init_client(client) == 0)
0373         return -ENODEV;
0374 
0375     hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
0376                                data,
0377                                adm1029_groups);
0378     return PTR_ERR_OR_ZERO(hwmon_dev);
0379 }
0380 
0381 static const struct i2c_device_id adm1029_id[] = {
0382     { "adm1029", 0 },
0383     { }
0384 };
0385 MODULE_DEVICE_TABLE(i2c, adm1029_id);
0386 
0387 static struct i2c_driver adm1029_driver = {
0388     .class      = I2C_CLASS_HWMON,
0389     .driver = {
0390         .name = "adm1029",
0391     },
0392     .probe_new  = adm1029_probe,
0393     .id_table   = adm1029_id,
0394     .detect     = adm1029_detect,
0395     .address_list   = normal_i2c,
0396 };
0397 
0398 module_i2c_driver(adm1029_driver);
0399 
0400 MODULE_AUTHOR("Corentin LABBE <clabbe.montjoie@gmail.com>");
0401 MODULE_DESCRIPTION("adm1029 driver");
0402 MODULE_LICENSE("GPL v2");