]> git.sur5r.net Git - groeck-nct6775/blobdiff - nct6775.c
Initial support for NCT6792D - untested
[groeck-nct6775] / nct6775.c
index caa972d7ff16d408b393cc5e5c136b6efde26ad1..79633230369ad0fdce8a8ea0514c1ba795ca015b 100644 (file)
--- a/nct6775.c
+++ b/nct6775.c
@@ -38,6 +38,7 @@
  * nct6776f     9      5       3       6+3    0xc330 0xc1    0x5ca3
  * nct6779d    15      5       5       2+6    0xc560 0xc1    0x5ca3
  * nct6791d    15      6       6       2+6    0xc800 0xc1    0x5ca3
+ * nct6792d    15      6       6       2+6    0xc911 0xc1    0x5ca3
  *
  * #temp lists the number of monitored temperature sources (first value) plus
  * the number of directly connectable temperature sensors (second value).
@@ -62,7 +63,7 @@
 
 #define USE_ALTERNATE
 
-enum kinds { nct6106, nct6775, nct6776, nct6779, nct6791 };
+enum kinds { nct6106, nct6775, nct6776, nct6779, nct6791, nct6792 };
 
 /* used to set data->name = nct6775_device_names[data->sio_kind] */
 static const char * const nct6775_device_names[] = {
@@ -71,6 +72,7 @@ static const char * const nct6775_device_names[] = {
        "nct6776",
        "nct6779",
        "nct6791",
+       "nct6792",
 };
 
 static unsigned short force_id;
@@ -101,6 +103,7 @@ MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
 #define SIO_NCT6776_ID         0xc330
 #define SIO_NCT6779_ID         0xc560
 #define SIO_NCT6791_ID         0xc800
+#define SIO_NCT6792_ID         0xc910
 #define SIO_ID_MASK            0xFFF0
 
 enum pwm_enable { off, manual, thermal_cruise, speed_cruise, sf3, sf4 };
@@ -736,11 +739,12 @@ struct nct6775_data {
        enum kinds kind;
        const char *name;
 
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
        struct device *hwmon_dev;
-       struct attribute_group *group_in;
-       struct attribute_group *group_fan;
-       struct attribute_group *group_temp;
-       struct attribute_group *group_pwm;
+#endif
+
+       int num_attr_groups;
+       const struct attribute_group *groups[6];
 
        u16 reg_temp[5][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst,
                                    * 3=temp_crit, 4=temp_lcrit
@@ -954,7 +958,7 @@ nct6775_create_attr_group(struct device *dev, struct sensor_template_group *tg,
        struct sensor_device_attribute_2 *a2;
        struct attribute **attrs;
        struct sensor_device_template **t;
-       int err, i, j, count;
+       int i, count;
 
        if (repeat <= 0)
                return ERR_PTR(-EINVAL);
@@ -985,7 +989,7 @@ nct6775_create_attr_group(struct device *dev, struct sensor_template_group *tg,
 
        for (i = 0; i < repeat; i++) {
                t = tg->templates;
-               for (j = 0; *t != NULL; j++) {
+               while (*t != NULL) {
                        snprintf(su->name, sizeof(su->name),
                                 (*t)->dev_attr.attr.name, tg->base + i);
                        if ((*t)->s2) {
@@ -1014,10 +1018,6 @@ nct6775_create_attr_group(struct device *dev, struct sensor_template_group *tg,
                }
        }
 
-       err = sysfs_create_group(&dev->kobj, group);
-       if (err)
-               return ERR_PTR(-ENOMEM);
-
        return group;
 }
 
@@ -1052,6 +1052,7 @@ static bool is_word_sized(struct nct6775_data *data, u16 reg)
                  reg == 0x73 || reg == 0x75 || reg == 0x77;
        case nct6779:
        case nct6791:
+       case nct6792:
                return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
                  ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x0b) ||
                  reg == 0x402 ||
@@ -1326,19 +1327,20 @@ static void nct6775_update_pwm(struct device *dev)
                if (reg & 0x80)
                        data->pwm[2][i] = 0;
 
-               if (data->REG_WEIGHT_TEMP_SEL[i]) {
-                       reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[i]);
-                       data->pwm_weight_temp_sel[i] = reg & 0x1f;
-                       /* If weight is disabled, report weight source as 0 */
-                       if (j == 1 && !(reg & 0x80))
-                               data->pwm_weight_temp_sel[i] = 0;
+               if (!data->REG_WEIGHT_TEMP_SEL[i])
+                       continue;
 
-                       /* Weight temp data */
-                       for (j = 0; j < ARRAY_SIZE(data->weight_temp); j++) {
-                               data->weight_temp[j][i]
-                                 = nct6775_read_value(data,
-                                               data->REG_WEIGHT_TEMP[j][i]);
-                       }
+               reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[i]);
+               data->pwm_weight_temp_sel[i] = reg & 0x1f;
+               /* If weight is disabled, report weight source as 0 */
+               if (j == 1 && !(reg & 0x80))
+                       data->pwm_weight_temp_sel[i] = 0;
+
+               /* Weight temp data */
+               for (j = 0; j < ARRAY_SIZE(data->weight_temp); j++) {
+                       data->weight_temp[j][i]
+                         = nct6775_read_value(data,
+                                              data->REG_WEIGHT_TEMP[j][i]);
                }
        }
 }
@@ -1399,6 +1401,7 @@ static void nct6775_update_pwm_limits(struct device *dev)
                case nct6106:
                case nct6779:
                case nct6791:
+               case nct6792:
                        reg = nct6775_read_value(data,
                                        data->REG_CRITICAL_PWM_ENABLE[i]);
                        if (reg & data->CRITICAL_PWM_ENABLE_MASK)
@@ -1471,7 +1474,8 @@ static struct nct6775_data *nct6775_update_device(struct device *dev)
                                          = nct6775_read_temp(data,
                                                data->reg_temp[j][i]);
                        }
-                       if (!(data->have_temp_fixed & (1 << i)))
+                       if (i >= NUM_TEMP_FIXED ||
+                           !(data->have_temp_fixed & (1 << i)))
                                continue;
                        data->temp_offset[i]
                          = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]);
@@ -1559,7 +1563,7 @@ static int find_temp_source(struct nct6775_data *data, int index, int count)
                if (src == source)
                        return nr;
        }
-       return -1;
+       return -ENODEV;
 }
 
 static ssize_t
@@ -1658,7 +1662,7 @@ store_temp_beep(struct device *dev, struct device_attribute *attr,
 
        nr = find_temp_source(data, sattr->index, data->num_temp_beeps);
        if (nr < 0)
-               return -ENODEV;
+               return nr;
 
        bit = data->BEEP_BITS[nr + TEMP_ALARM_BASE];
        regindex = bit >> 3;
@@ -2739,6 +2743,7 @@ store_fan_time(struct device *dev, struct device_attribute *attr,
        return count;
 }
 
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
 static ssize_t
 show_name(struct device *dev, struct device_attribute *attr, char *buf)
 {
@@ -2748,6 +2753,7 @@ show_name(struct device *dev, struct device_attribute *attr, char *buf)
 }
 
 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
+#endif
 
 static ssize_t
 show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
@@ -2807,6 +2813,7 @@ store_auto_pwm(struct device *dev, struct device_attribute *attr,
                case nct6106:
                case nct6779:
                case nct6791:
+               case nct6792:
                        nct6775_write_value(data, data->REG_CRITICAL_PWM[nr],
                                            val);
                        reg = nct6775_read_value(data,
@@ -3000,7 +3007,6 @@ static struct sensor_device_template *nct6775_attributes_pwm_template[] = {
        &sensor_dev_template_pwm_auto_point6_temp,
        &sensor_dev_template_pwm_auto_point7_pwm,
        &sensor_dev_template_pwm_auto_point7_temp,      /* 35 */
-
        NULL
 };
 
@@ -3078,16 +3084,16 @@ static umode_t nct6775_other_is_visible(struct kobject *kobj,
        struct device *dev = container_of(kobj, struct device, kobj);
        struct nct6775_data *data = dev_get_drvdata(dev);
 
-       if (index == 1 && !data->have_vid)
+       if (index == 0 && !data->have_vid)
                return 0;
 
-       if (index == 2 || index == 3) {
-               if (data->ALARM_BITS[INTRUSION_ALARM_BASE + index - 2] < 0)
+       if (index == 1 || index == 2) {
+               if (data->ALARM_BITS[INTRUSION_ALARM_BASE + index - 1] < 0)
                        return 0;
        }
 
-       if (index == 4 || index == 5) {
-               if (data->BEEP_BITS[INTRUSION_ALARM_BASE + index - 4] < 0)
+       if (index == 3 || index == 4) {
+               if (data->BEEP_BITS[INTRUSION_ALARM_BASE + index - 3] < 0)
                        return 0;
        }
 
@@ -3100,14 +3106,15 @@ static umode_t nct6775_other_is_visible(struct kobject *kobj,
  * Any change in order or content must be matched.
  */
 static struct attribute *nct6775_attributes_other[] = {
+       &dev_attr_cpu0_vid.attr,                                /* 0 */
+       &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,        /* 1 */
+       &sensor_dev_attr_intrusion1_alarm.dev_attr.attr,        /* 2 */
+       &sensor_dev_attr_intrusion0_beep.dev_attr.attr,         /* 3 */
+       &sensor_dev_attr_intrusion1_beep.dev_attr.attr,         /* 4 */
+       &sensor_dev_attr_beep_enable.dev_attr.attr,             /* 5 */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
        &dev_attr_name.attr,
-       &dev_attr_cpu0_vid.attr,                                /* 1 */
-       &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,        /* 2 */
-       &sensor_dev_attr_intrusion1_alarm.dev_attr.attr,        /* 3 */
-       &sensor_dev_attr_intrusion0_beep.dev_attr.attr,         /* 4 */
-       &sensor_dev_attr_intrusion1_beep.dev_attr.attr,         /* 5 */
-       &sensor_dev_attr_beep_enable.dev_attr.attr,             /* 6 */
-
+#endif
        NULL
 };
 
@@ -3116,27 +3123,6 @@ static const struct attribute_group nct6775_group_other = {
        .is_visible = nct6775_other_is_visible,
 };
 
-/*
- * Driver and device management
- */
-
-static void nct6775_device_remove_files(struct device *dev)
-{
-       struct nct6775_data *data = dev_get_drvdata(dev);
-
-       if (data->group_pwm)
-               sysfs_remove_group(&dev->kobj, data->group_pwm);
-       if (data->group_in)
-               sysfs_remove_group(&dev->kobj, data->group_in);
-       if (data->group_fan)
-               sysfs_remove_group(&dev->kobj, data->group_fan);
-       if (data->group_temp)
-               sysfs_remove_group(&dev->kobj, data->group_temp);
-
-       sysfs_remove_group(&dev->kobj, &nct6775_group_other);
-}
-
-/* Get the monitoring functions started */
 static inline void nct6775_init_device(struct nct6775_data *data)
 {
        int i;
@@ -3241,7 +3227,7 @@ nct6775_check_fan_inputs(struct nct6775_data *data)
                pwm4pin = false;
                pwm5pin = false;
                pwm6pin = false;
-       } else {        /* NCT6779D or NCT6791D */
+       } else {        /* NCT6779D, NCT6791D, or NCT6792D */
                regval = superio_inb(sioreg, 0x1c);
 
                fan3pin = !(regval & (1 << 5));
@@ -3254,7 +3240,7 @@ nct6775_check_fan_inputs(struct nct6775_data *data)
 
                fan4min = fan4pin;
 
-               if (data->kind == nct6791) {
+               if (data->kind == nct6791 || data->kind == nct6792) {
                        regval = superio_inb(sioreg, 0x2d);
                        fan6pin = (regval & (1 << 1));
                        pwm6pin = (regval & (1 << 0));
@@ -3302,7 +3288,7 @@ static void add_temp_sensors(struct nct6775_data *data, const u16 *regp,
 static int nct6775_probe(struct platform_device *pdev)
 {
        struct device *dev = &pdev->dev;
-       struct nct6775_sio_data *sio_data = dev->platform_data;
+       struct nct6775_sio_data *sio_data = dev_get_platdata(dev);
        struct nct6775_data *data;
        struct resource *res;
        int i, s, err = 0;
@@ -3313,6 +3299,7 @@ static int nct6775_probe(struct platform_device *pdev)
        int num_reg_temp, num_reg_temp_mon;
        u8 cr2a;
        struct attribute_group *group;
+       struct device *hwmon_dev;
 
        res = platform_get_resource(pdev, IORESOURCE_IO, 0);
        if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
@@ -3625,6 +3612,7 @@ static int nct6775_probe(struct platform_device *pdev)
 
                break;
        case nct6791:
+       case nct6792:
                data->in_num = 15;
                data->pwm_num = 6;
                data->auto_pwm_num = 4;
@@ -3809,7 +3797,8 @@ static int nct6775_probe(struct platform_device *pdev)
                    !strlen(data->temp_label[src])) {
                        dev_info(dev,
                                 "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
-                                src, i, data->REG_TEMP_SEL[i], reg_temp_mon[i]);
+                                src, i, data->REG_TEMP_SEL[i],
+                                reg_temp_mon[i]);
                        continue;
                }
 
@@ -3890,6 +3879,7 @@ static int nct6775_probe(struct platform_device *pdev)
        case nct6106:
        case nct6779:
        case nct6791:
+       case nct6792:
                break;
        }
 
@@ -3921,6 +3911,7 @@ static int nct6775_probe(struct platform_device *pdev)
                        tmp |= 0x3e;
                        break;
                case nct6791:
+               case nct6792:
                        tmp |= 0x7e;
                        break;
                }
@@ -3940,62 +3931,72 @@ static int nct6775_probe(struct platform_device *pdev)
        /* Register sysfs hooks */
        group = nct6775_create_attr_group(dev, &nct6775_pwm_template_group,
                                          data->pwm_num);
-       if (IS_ERR(group)) {
-               err = PTR_ERR(group);
-               goto exit_remove;
-       }
-       data->group_pwm = group;
+       if (IS_ERR(group))
+               return PTR_ERR(group);
+
+       data->groups[data->num_attr_groups++] = group;
 
        group = nct6775_create_attr_group(dev, &nct6775_in_template_group,
                                          fls(data->have_in));
-       if (IS_ERR(group)) {
-               err = PTR_ERR(group);
-               goto exit_remove;
-       }
-       data->group_in = group;
+       if (IS_ERR(group))
+               return PTR_ERR(group);
+
+       data->groups[data->num_attr_groups++] = group;
 
        group = nct6775_create_attr_group(dev, &nct6775_fan_template_group,
                                          fls(data->has_fan));
-       if (IS_ERR(group)) {
-               err = PTR_ERR(group);
-               goto exit_remove;
-       }
-       data->group_fan = group;
+       if (IS_ERR(group))
+               return PTR_ERR(group);
+
+       data->groups[data->num_attr_groups++] = group;
 
        group = nct6775_create_attr_group(dev, &nct6775_temp_template_group,
                                          fls(data->have_temp));
-       if (IS_ERR(group)) {
-               err = PTR_ERR(group);
-               goto exit_remove;
-       }
-       data->group_temp = group;
+       if (IS_ERR(group))
+               return PTR_ERR(group);
 
-       err = sysfs_create_group(&dev->kobj, &nct6775_group_other);
-       if (err)
-               goto exit_remove;
+       data->groups[data->num_attr_groups++] = group;
+       data->groups[data->num_attr_groups++] = &nct6775_group_other;
 
-       data->hwmon_dev = hwmon_device_register(dev);
-       if (IS_ERR(data->hwmon_dev)) {
-               err = PTR_ERR(data->hwmon_dev);
-               goto exit_remove;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
+       err = sysfs_create_groups(&dev->kobj, data->groups);
+       if (err < 0)
+               return err;
+       hwmon_dev = hwmon_device_register(dev);
+       if (IS_ERR(hwmon_dev)) {
+               sysfs_remove_groups(&dev->kobj, data->groups);
+               return PTR_ERR(hwmon_dev);
        }
-
-       return 0;
-
-exit_remove:
-       nct6775_device_remove_files(dev);
-       return err;
+       data->hwmon_dev = hwmon_dev;
+#else
+       hwmon_dev = devm_hwmon_device_register_with_groups(dev, data->name,
+                                                          data, data->groups);
+#endif
+       return PTR_ERR_OR_ZERO(hwmon_dev);
 }
 
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
 static int nct6775_remove(struct platform_device *pdev)
 {
        struct nct6775_data *data = platform_get_drvdata(pdev);
 
        hwmon_device_unregister(data->hwmon_dev);
-       nct6775_device_remove_files(&pdev->dev);
-
+       sysfs_remove_groups(&pdev->dev.kobj, data->groups);
        return 0;
 }
+#endif
+
+static void nct6791_enable_io_mapping(int sioaddr)
+{
+       int val;
+
+       val = superio_inb(sioaddr, NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE);
+       if (val & 0x10) {
+               pr_info("Enabling hardware monitor logical device mappings.\n");
+               superio_outb(sioaddr, NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE,
+                            val & ~0x10);
+       }
+}
 
 #ifdef CONFIG_PM
 static int nct6775_suspend(struct device *dev)
@@ -4021,21 +4022,12 @@ static int nct6775_resume(struct device *dev)
        mutex_lock(&data->update_lock);
        data->bank = 0xff;              /* Force initial bank selection */
 
-       if (data->kind == nct6791) {
-               int val;
-
+       if (data->kind == nct6791 || data->kind == nct6792) {
                err = superio_enter(data->sioreg);
                if (err)
                        goto abort;
 
-               val = superio_inb(data->sioreg,
-                                 NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE);
-               if (val & 0x10) {
-                       pr_info("Re-enabling hardware monitor logical device mappings.\n");
-                       superio_outb(data->sioreg,
-                                    NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE,
-                                    val & ~0x10);
-               }
+               nct6791_enable_io_mapping(data->sioreg);
                superio_exit(data->sioreg);
        }
 
@@ -4080,12 +4072,14 @@ abort:
        data->valid = false;
        mutex_unlock(&data->update_lock);
 
-       return 0;
+       return err;
 }
 
 static const struct dev_pm_ops nct6775_dev_pm_ops = {
        .suspend = nct6775_suspend,
        .resume = nct6775_resume,
+       .freeze = nct6775_suspend,
+       .restore = nct6775_resume,
 };
 
 #define NCT6775_DEV_PM_OPS     (&nct6775_dev_pm_ops)
@@ -4100,7 +4094,9 @@ static struct platform_driver nct6775_driver = {
                .pm     = NCT6775_DEV_PM_OPS,
        },
        .probe          = nct6775_probe,
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
        .remove         = nct6775_remove,
+#endif
 };
 
 static const char * const nct6775_sio_names[] __initconst = {
@@ -4109,6 +4105,7 @@ static const char * const nct6775_sio_names[] __initconst = {
        "NCT6776D/F",
        "NCT6779D",
        "NCT6791D",
+       "NCT6792D",
 };
 
 /* nct6775_find() looks for a '627 in the Super-I/O config space */
@@ -4143,6 +4140,9 @@ static int __init nct6775_find(int sioaddr, struct nct6775_sio_data *sio_data)
        case SIO_NCT6791_ID:
                sio_data->kind = nct6791;
                break;
+       case SIO_NCT6792_ID:
+               sio_data->kind = nct6792;
+               break;
        default:
                if (val != 0xffff)
                        pr_debug("unsupported chip ID: 0x%04x\n", val);
@@ -4167,15 +4167,9 @@ static int __init nct6775_find(int sioaddr, struct nct6775_sio_data *sio_data)
                pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
                superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
        }
-       if (sio_data->kind == nct6791) {
-               val = superio_inb(sioaddr, NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE);
-               if (val & 0x10) {
-                       pr_info("Enabling hardware monitor logical device mappings.\n");
-                       superio_outb(sioaddr,
-                                    NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE,
-                                    val & ~0x10);
-               }
-       }
+
+       if (sio_data->kind == nct6791 || sio_data->kind == nct6792)
+               nct6791_enable_io_mapping(sioaddr);
 
        superio_exit(sioaddr);
        pr_info("Found %s or compatible chip at %#x:%#x\n",
@@ -4188,7 +4182,7 @@ static int __init nct6775_find(int sioaddr, struct nct6775_sio_data *sio_data)
 /*
  * when Super-I/O functions move to a separate file, the Super-I/O
  * bus will manage the lifetime of the device and this module will only keep
- * track of the nct6775 driver. But since we platform_device_alloc(), we
+ * track of the nct6775 driver. But since we use platform_device_alloc(), we
  * must keep track of the device
  */
 static struct platform_device *pdev[2];