Ticket #2332: f71882fg-fanspeed.patch

File f71882fg-fanspeed.patch, 8.1 kB (added by ticket, 6 months ago)

Second version of the patch that includes a rough PWM interface (only pwm[1-4] controls, no pwm*_enable)

  • drivers/hwmon/f71882fg.c

    old new  
    5757#define F71882FG_REG_IN1_HIGH           0x32 
    5858 
    5959#define F71882FG_REG_FAN(nr)            (0xA0 + (16 * (nr))) 
     60#define F71882FG_REG_FAN_SPEED(nr)      (0xA2 + (16 * (nr))) 
    6061#define F71882FG_REG_FAN_STATUS         0x92 
    6162#define F71882FG_REG_FAN_BEEP           0x93 
     63#define F71882FG_REG_FAN_TYPE           0x94 
     64#define F71882FG_REG_FAN_MODE           0x96 
    6265 
    6366#define F71882FG_REG_TEMP(nr)           (0x72 + 2 * (nr)) 
    6467#define F71882FG_REG_TEMP_OVT(nr)       (0x82 + 2 * (nr)) 
     
    102105        u16     fan[4]; 
    103106        u8      fan_status; 
    104107        u8      fan_beep; 
     108        u8      fan_type; 
     109        u8      fan_mode; 
     110        u16     fan_speed[4]; 
    105111        u8      temp[3]; 
    106112        u8      temp_ovt[3]; 
    107113        u8      temp_high[3]; 
     
    132138/* Sysfs Fan */ 
    133139static ssize_t show_fan(struct device *dev, struct device_attribute *devattr, 
    134140        char *buf); 
     141static ssize_t show_fan_type(struct device *dev, struct device_attribute 
     142        *devattr, char *buf); 
     143static ssize_t show_fan_mode(struct device *dev, struct device_attribute 
     144        *devattr, char *buf); 
    135145static ssize_t show_fan_beep(struct device *dev, struct device_attribute 
    136146        *devattr, char *buf); 
    137147static ssize_t store_fan_beep(struct device *dev, struct device_attribute 
    138148        *devattr, const char *buf, size_t count); 
     149static ssize_t show_fan_speed(struct device *dev, struct device_attribute 
     150        *devattr, char *buf); 
     151static ssize_t store_fan_speed(struct device *dev, struct device_attribute 
     152        *devattr, const char *buf, size_t count); 
    139153static ssize_t show_fan_alarm(struct device *dev, struct device_attribute 
    140154        *devattr, char *buf); 
     155/* Sysfs Fan PWM */ 
     156static ssize_t show_pwm(struct device *dev, struct device_attribute 
     157        *devattr, char *buf); 
     158static ssize_t store_pwm(struct device *dev, struct device_attribute 
     159        *devattr, const char *buf, size_t count); 
    141160/* Sysfs Temp */ 
    142161static ssize_t show_temp(struct device *dev, struct device_attribute 
    143162        *devattr, char *buf); 
     
    251270        SENSOR_ATTR(fan1_beep, S_IRUGO|S_IWUSR, show_fan_beep, 
    252271                store_fan_beep, 0), 
    253272        SENSOR_ATTR(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0), 
     273        SENSOR_ATTR(fan1_type, S_IRUGO, show_fan_type, NULL, 0), 
     274        SENSOR_ATTR(fan1_mode, S_IRUGO, show_fan_mode, NULL, 0), 
     275        SENSOR_ATTR(fan1_speed, S_IRUGO|S_IWUSR, show_fan_speed, 
     276                store_fan_speed, 0), 
     277        SENSOR_ATTR(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0), 
    254278        SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1), 
    255279        SENSOR_ATTR(fan2_beep, S_IRUGO|S_IWUSR, show_fan_beep, 
    256280                store_fan_beep, 1), 
    257281        SENSOR_ATTR(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 1), 
     282        SENSOR_ATTR(fan2_type, S_IRUGO, show_fan_type, NULL, 1), 
     283        SENSOR_ATTR(fan2_mode, S_IRUGO, show_fan_mode, NULL, 1), 
     284        SENSOR_ATTR(fan2_speed, S_IRUGO|S_IWUSR, show_fan_speed, 
     285                store_fan_speed, 1), 
     286        SENSOR_ATTR(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 1), 
    258287        SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2), 
    259288        SENSOR_ATTR(fan3_beep, S_IRUGO|S_IWUSR, show_fan_beep, 
    260289                store_fan_beep, 2), 
    261290        SENSOR_ATTR(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 2), 
     291        SENSOR_ATTR(fan3_type, S_IRUGO, show_fan_type, NULL, 2), 
     292        SENSOR_ATTR(fan3_mode, S_IRUGO, show_fan_mode, NULL, 2), 
     293        SENSOR_ATTR(fan3_speed, S_IRUGO|S_IWUSR, show_fan_speed, 
     294                store_fan_speed, 2), 
     295        SENSOR_ATTR(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 2), 
    262296        SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3), 
    263297        SENSOR_ATTR(fan4_beep, S_IRUGO|S_IWUSR, show_fan_beep, 
    264298                store_fan_beep, 3), 
    265         SENSOR_ATTR(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 3) 
     299        SENSOR_ATTR(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 3), 
     300        SENSOR_ATTR(fan4_type, S_IRUGO, show_fan_type, NULL, 3), 
     301        SENSOR_ATTR(fan4_mode, S_IRUGO, show_fan_mode, NULL, 3), 
     302        SENSOR_ATTR(fan4_speed, S_IRUGO|S_IWUSR, show_fan_speed, 
     303                store_fan_speed, 3), 
     304        SENSOR_ATTR(pwm4, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 3), 
    266305}; 
    267306 
    268307 
     
    379418                data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP); 
    380419 
    381420                data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP); 
     421                data->fan_type = f71882fg_read8(data, F71882FG_REG_FAN_TYPE); 
     422                data->fan_mode = f71882fg_read8(data, F71882FG_REG_FAN_MODE); 
    382423 
    383424                data->last_limits = jiffies; 
    384425        } 
     
    395436 
    396437                data->fan_status = f71882fg_read8(data, 
    397438                                                F71882FG_REG_FAN_STATUS); 
    398                 for (nr = 0; nr < 4; nr++) 
     439                for (nr = 0; nr < 4; nr++) { 
    399440                        data->fan[nr] = f71882fg_read16(data, 
    400441                                                F71882FG_REG_FAN(nr)); 
     442                        data->fan_speed[nr] = f71882fg_read16(data, 
     443                                                F71882FG_REG_FAN_SPEED(nr)); 
     444                } 
    401445 
    402446                data->in_status = f71882fg_read8(data, 
    403447                                                F71882FG_REG_IN_STATUS); 
     
    471515                return sprintf(buf, "0\n"); 
    472516} 
    473517 
     518static ssize_t show_fan_type(struct device *dev, struct device_attribute 
     519        *devattr, char *buf) 
     520{ 
     521        struct f71882fg_data *data = f71882fg_update_device(dev); 
     522        int nr = to_sensor_dev_attr(devattr)->index; 
     523 
     524        /* 2-bit values */ 
     525        switch(data->fan_type & (3 << (2*nr)) >> (2*nr)) { 
     526          case 0: 
     527                return sprintf(buf, "0 PWM (pushpull)\n"); 
     528          case 1: 
     529                return sprintf(buf, "1 linear\n"); 
     530          case 2: 
     531                return sprintf(buf, "2 PWM (open drain, 4-wire)\n"); 
     532          default: 
     533                return sprintf(buf, "3 invalid\n"); 
     534        } 
     535} 
     536 
     537static ssize_t show_fan_mode(struct device *dev, struct device_attribute 
     538        *devattr, char *buf) 
     539{ 
     540        struct f71882fg_data *data = f71882fg_update_device(dev); 
     541        int nr = to_sensor_dev_attr(devattr)->index; 
     542 
     543        /* 2-bit values */ 
     544        switch(data->fan_mode & (3 << (2*nr)) >> (2*nr)) { 
     545          case 0: 
     546                return sprintf(buf, "0 auto (RPM)\n"); 
     547          case 1: 
     548                return sprintf(buf, "1 auto (PWM)\n"); 
     549          case 2: 
     550                return sprintf(buf, "2 manual (RPM)\n"); 
     551          default: 
     552                return sprintf(buf, "3 manual (PWM)\n"); 
     553        } 
     554} 
     555 
     556static ssize_t show_fan_speed(struct device *dev, struct device_attribute 
     557        *devattr, char *buf) 
     558{ 
     559        struct f71882fg_data *data = f71882fg_update_device(dev); 
     560        int nr = to_sensor_dev_attr(devattr)->index; 
     561 
     562        /* 2-bit values */ 
     563        return sprintf(buf, "%hu\n", data->fan_speed[nr]); 
     564} 
     565 
     566static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, 
     567        char *buf) 
     568{ 
     569        struct f71882fg_data *data = f71882fg_update_device(dev); 
     570        int nr = to_sensor_dev_attr(devattr)->index; 
     571        int speed = fan_from_reg(data->fan[nr]); 
     572 
     573        u8 fan_mode = (data->fan_mode & (3 << (2*nr))) >> 2*nr; 
     574 
     575        if (speed == FAN_MIN_DETECT) 
     576                speed = 0; 
     577 
     578        if(fan_mode % 2 == 0) { 
     579                /* no PWM */ 
     580                return sprintf(buf, "0\n"); 
     581        } else { 
     582                return sprintf(buf, "%hu\n", data->fan_speed[nr]); 
     583        } 
     584} 
     585 
     586static ssize_t store_fan_speed(struct device *dev, struct device_attribute 
     587        *devattr, const char *buf, size_t count) 
     588{ 
     589        struct f71882fg_data *data = dev_get_drvdata(dev); 
     590        int nr = to_sensor_dev_attr(devattr)->index; 
     591        int val = (u16)simple_strtoul(buf, NULL, 10); 
     592        u8 msb, lsb; 
     593 
     594        u8 fan_mode = (data->fan_mode & (3 << (2*nr))) >> 2*nr; 
     595        if(fan_mode <= 1) { 
     596                /* auto mode, register is read-only */ 
     597                printk(KERN_INFO DRVNAME ": not setting speed of fan #%d: fan mode is auto\n", nr+1); 
     598                return count; 
     599        } 
     600 
     601        mutex_lock(&data->update_lock); 
     602        data->fan_speed[nr] = val; 
     603        msb = (u8)(data->fan_speed[nr] >> 8); 
     604        lsb = (u8)(data->fan_speed[nr] & 0xff); 
     605        if(fan_mode % 2 == 0) { 
     606                /* only write MSB in RPM mode */ 
     607                printk(KERN_INFO DRVNAME ": set speed of fan #%d: msb=%d\n", nr+1, (int)lsb); 
     608                f71882fg_write8(data, F71882FG_REG_FAN_SPEED(nr), msb); 
     609        } 
     610        printk(KERN_INFO DRVNAME ": set speed of fan #%d: lsb=%d\n", nr+1, (int)lsb); 
     611        f71882fg_write8(data, F71882FG_REG_FAN_SPEED(nr)+1, lsb); 
     612        mutex_unlock(&data->update_lock); 
     613 
     614        return count; 
     615} 
     616 
     617static ssize_t store_pwm(struct device *dev, struct device_attribute 
     618        *devattr, const char *buf, size_t count) 
     619{ 
     620        struct f71882fg_data *data = dev_get_drvdata(dev); 
     621        int nr = to_sensor_dev_attr(devattr)->index; 
     622        int val = (u16)simple_strtoul(buf, NULL, 10); 
     623        u8 lsb; 
     624 
     625        u8 fan_mode = (data->fan_mode & (3 << (2*nr))) >> 2*nr; 
     626 
     627        if(fan_mode != 3) { 
     628                /* no manual PWM */ 
     629                printk(KERN_INFO DRVNAME ": not setting speed of fan #%d: fan mode is auto or RPM\n", nr+1); 
     630                return count; 
     631        } 
     632 
     633        mutex_lock(&data->update_lock); 
     634        data->fan_speed[nr] = val; 
     635        lsb = (u8)(data->fan_speed[nr] & 0xff); 
     636        f71882fg_write8(data, F71882FG_REG_FAN_SPEED(nr)+1, lsb); 
     637        mutex_unlock(&data->update_lock); 
     638 
     639        return count; 
     640} 
     641 
    474642static ssize_t show_in(struct device *dev, struct device_attribute *devattr, 
    475643        char *buf) 
    476644{