hwmon: (adm1031) Various cleanups

* Rename new_client to client
* Drop redundant initializations to 0
* Drop trailing space
* Other whitespace cleanups
* Split/fold a few long lines
* Constify static data
* Optimizations in set_fan_div()

Signed-off-by: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Mark M. Hoffman <mhoffman@lightlink.com>

authored by

Jean Delvare and committed by
Mark M. Hoffman
6d6006b8 38a1f0e9

+62 -64
+62 -64
drivers/hwmon/adm1031.c
··· 5 Supports adm1030 / adm1031 6 Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org> 7 Reworked by Jean Delvare <khali@linux-fr.org> 8 - 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 2 of the License, or ··· 32 33 /* Following macros takes channel parameter starting from 0 to 2 */ 34 #define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr)) 35 - #define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr)) 36 #define ADM1031_REG_PWM (0x22) 37 #define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr)) 38 39 - #define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4*(nr)) 40 - #define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4*(nr)) 41 - #define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4*(nr)) 42 43 - #define ADM1031_REG_TEMP(nr) (0xa + (nr)) 44 #define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr)) 45 46 #define ADM1031_REG_STATUS(nr) (0x2 + (nr)) 47 48 - #define ADM1031_REG_CONF1 0x0 49 - #define ADM1031_REG_CONF2 0x1 50 - #define ADM1031_REG_EXT_TEMP 0x6 51 52 #define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */ 53 #define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */ ··· 78 /* The chan_select_table contains the possible configurations for 79 * auto fan control. 80 */ 81 - auto_chan_table_t *chan_select_table; 82 u16 alarm; 83 u8 conf1; 84 u8 conf2; ··· 181 #define GET_FAN_AUTO_BITFIELD(data, idx) \ 182 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2] 183 184 - /* The tables below contains the possible values for the auto fan 185 * control bitfields. the index in the table is the register value. 186 * MSb is the auto fan control enable bit, so the four first entries 187 * in the table disables auto fan control when both bitfields are zero. 188 */ 189 - static auto_chan_table_t auto_channel_select_table_adm1031 = { 190 - {0, 0}, {0, 0}, {0, 0}, {0, 0}, 191 - {2 /*0b010 */ , 4 /*0b100 */ }, 192 - {2 /*0b010 */ , 2 /*0b010 */ }, 193 - {4 /*0b100 */ , 4 /*0b100 */ }, 194 - {7 /*0b111 */ , 7 /*0b111 */ }, 195 }; 196 197 - static auto_chan_table_t auto_channel_select_table_adm1030 = { 198 - {0, 0}, {0, 0}, {0, 0}, {0, 0}, 199 - {2 /*0b10 */ , 0}, 200 - {0xff /*invalid */ , 0}, 201 - {0xff /*invalid */ , 0}, 202 - {3 /*0b11 */ , 0}, 203 }; 204 205 /* That function checks if a bitfield is valid and returns the other bitfield ··· 228 break; 229 } else if (val == (*data->chan_select_table)[i][chan] && 230 first_match == -1) { 231 - /* Save the first match in case of an exact match has not been 232 - * found 233 */ 234 first_match = i; 235 } ··· 264 old_fan_mode = data->conf1; 265 266 mutex_lock(&data->update_lock); 267 - 268 if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) { 269 mutex_unlock(&data->update_lock); 270 return ret; 271 } 272 - if (((data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1)) & ADM1031_CONF1_AUTO_MODE) ^ 273 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) { 274 if (data->conf1 & ADM1031_CONF1_AUTO_MODE){ 275 - /* Switch to Auto Fan Mode 276 - * Save PWM registers 277 * Set PWM registers to 33% Both */ 278 data->old_pwm[0] = data->pwm[0]; 279 data->old_pwm[1] = data->pwm[1]; ··· 284 data->pwm[0] = data->old_pwm[0]; 285 data->pwm[1] = data->old_pwm[1]; 286 /* Restore PWM registers */ 287 - adm1031_write_value(client, ADM1031_REG_PWM, 288 data->pwm[0] | (data->pwm[1] << 4)); 289 } 290 } ··· 315 static ssize_t show_auto_temp_off(struct device *dev, char *buf, int nr) 316 { 317 struct adm1031_data *data = adm1031_update_device(dev); 318 - return sprintf(buf, "%d\n", 319 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr])); 320 } 321 static ssize_t show_auto_temp_min(struct device *dev, char *buf, int nr) ··· 408 int reg; 409 410 mutex_lock(&data->update_lock); 411 - if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) && 412 (((val>>4) & 0xf) != 5)) { 413 /* In automatic mode, the only PWM accepted is 33% */ 414 mutex_unlock(&data->update_lock); ··· 472 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0]) 473 || data->temp[1] >= 474 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]) 475 - || (data->chip_type == adm1031 476 && data->temp[2] >= 477 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2])); 478 break; ··· 515 516 mutex_lock(&data->update_lock); 517 if (val) { 518 - data->fan_min[nr] = 519 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr])); 520 } else { 521 data->fan_min[nr] = 0xff; ··· 536 537 tmp = val == 8 ? 0xc0 : 538 val == 4 ? 0x80 : 539 - val == 2 ? 0x40 : 540 - val == 1 ? 0x00 : 541 0xff; 542 if (tmp == 0xff) 543 return -EINVAL; 544 - 545 mutex_lock(&data->update_lock); 546 /* Get fresh readings */ 547 data->fan_div[nr] = adm1031_read_value(client, ··· 551 552 /* Write the new clock divider and fan min */ 553 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]); 554 - data->fan_div[nr] = (tmp & 0xC0) | (0x3f & data->fan_div[nr]); 555 - new_min = data->fan_min[nr] * old_div / 556 - FAN_DIV_FROM_REG(data->fan_div[nr]); 557 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min; 558 559 - adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr), 560 data->fan_div[nr]); 561 - adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), 562 data->fan_min[nr]); 563 564 /* Invalidate the cache: fan speed is no longer valid */ ··· 796 /* This function is called by i2c_probe */ 797 static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind) 798 { 799 - struct i2c_client *new_client; 800 struct adm1031_data *data; 801 int err = 0; 802 const char *name = ""; ··· 809 goto exit; 810 } 811 812 - new_client = &data->client; 813 - i2c_set_clientdata(new_client, data); 814 - new_client->addr = address; 815 - new_client->adapter = adapter; 816 - new_client->driver = &adm1031_driver; 817 - new_client->flags = 0; 818 819 if (kind < 0) { 820 int id, co; 821 - id = i2c_smbus_read_byte_data(new_client, 0x3d); 822 - co = i2c_smbus_read_byte_data(new_client, 0x3e); 823 824 if (!((id == 0x31 || id == 0x30) && co == 0x41)) 825 goto exit_free; ··· 839 } 840 data->chip_type = kind; 841 842 - strlcpy(new_client->name, name, I2C_NAME_SIZE); 843 - data->valid = 0; 844 mutex_init(&data->update_lock); 845 846 /* Tell the I2C layer a new client has arrived */ 847 - if ((err = i2c_attach_client(new_client))) 848 goto exit_free; 849 850 /* Initialize the ADM1031 chip */ 851 - adm1031_init_client(new_client); 852 853 /* Register sysfs hooks */ 854 - if ((err = sysfs_create_group(&new_client->dev.kobj, &adm1031_group))) 855 goto exit_detach; 856 857 if (kind == adm1031) { 858 - if ((err = sysfs_create_group(&new_client->dev.kobj, 859 &adm1031_group_opt))) 860 goto exit_remove; 861 } 862 863 - data->hwmon_dev = hwmon_device_register(&new_client->dev); 864 if (IS_ERR(data->hwmon_dev)) { 865 err = PTR_ERR(data->hwmon_dev); 866 goto exit_remove; ··· 868 return 0; 869 870 exit_remove: 871 - sysfs_remove_group(&new_client->dev.kobj, &adm1031_group); 872 - sysfs_remove_group(&new_client->dev.kobj, &adm1031_group_opt); 873 exit_detach: 874 - i2c_detach_client(new_client); 875 exit_free: 876 kfree(data); 877 exit: ··· 903 if (data->chip_type == adm1031) { 904 mask |= (ADM1031_CONF2_PWM2_ENABLE | 905 ADM1031_CONF2_TACH2_ENABLE); 906 - } 907 /* Initialize the ADM1031 chip (enables fan speed reading ) */ 908 read_val = adm1031_read_value(client, ADM1031_REG_CONF2); 909 if ((read_val | mask) != read_val) { ··· 982 if (data->chip_type == adm1030) { 983 data->alarm &= 0xc0ff; 984 } 985 - 986 for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) { 987 data->fan_div[chan] = 988 adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan)); ··· 991 data->fan[chan] = 992 adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan)); 993 data->pwm[chan] = 994 - 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >> 995 (4*chan)); 996 } 997 data->last_updated = jiffies;
··· 5 Supports adm1030 / adm1031 6 Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org> 7 Reworked by Jean Delvare <khali@linux-fr.org> 8 + 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 2 of the License, or ··· 32 33 /* Following macros takes channel parameter starting from 0 to 2 */ 34 #define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr)) 35 + #define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr)) 36 #define ADM1031_REG_PWM (0x22) 37 #define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr)) 38 39 + #define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr)) 40 + #define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr)) 41 + #define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr)) 42 43 + #define ADM1031_REG_TEMP(nr) (0x0a + (nr)) 44 #define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr)) 45 46 #define ADM1031_REG_STATUS(nr) (0x2 + (nr)) 47 48 + #define ADM1031_REG_CONF1 0x00 49 + #define ADM1031_REG_CONF2 0x01 50 + #define ADM1031_REG_EXT_TEMP 0x06 51 52 #define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */ 53 #define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */ ··· 78 /* The chan_select_table contains the possible configurations for 79 * auto fan control. 80 */ 81 + const auto_chan_table_t *chan_select_table; 82 u16 alarm; 83 u8 conf1; 84 u8 conf2; ··· 181 #define GET_FAN_AUTO_BITFIELD(data, idx) \ 182 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2] 183 184 + /* The tables below contains the possible values for the auto fan 185 * control bitfields. the index in the table is the register value. 186 * MSb is the auto fan control enable bit, so the four first entries 187 * in the table disables auto fan control when both bitfields are zero. 188 */ 189 + static const auto_chan_table_t auto_channel_select_table_adm1031 = { 190 + { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 191 + { 2 /* 0b010 */ , 4 /* 0b100 */ }, 192 + { 2 /* 0b010 */ , 2 /* 0b010 */ }, 193 + { 4 /* 0b100 */ , 4 /* 0b100 */ }, 194 + { 7 /* 0b111 */ , 7 /* 0b111 */ }, 195 }; 196 197 + static const auto_chan_table_t auto_channel_select_table_adm1030 = { 198 + { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 199 + { 2 /* 0b10 */ , 0 }, 200 + { 0xff /* invalid */ , 0 }, 201 + { 0xff /* invalid */ , 0 }, 202 + { 3 /* 0b11 */ , 0 }, 203 }; 204 205 /* That function checks if a bitfield is valid and returns the other bitfield ··· 228 break; 229 } else if (val == (*data->chan_select_table)[i][chan] && 230 first_match == -1) { 231 + /* Save the first match in case of an exact match has 232 + * not been found 233 */ 234 first_match = i; 235 } ··· 264 old_fan_mode = data->conf1; 265 266 mutex_lock(&data->update_lock); 267 + 268 if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) { 269 mutex_unlock(&data->update_lock); 270 return ret; 271 } 272 + data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1); 273 + if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^ 274 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) { 275 if (data->conf1 & ADM1031_CONF1_AUTO_MODE){ 276 + /* Switch to Auto Fan Mode 277 + * Save PWM registers 278 * Set PWM registers to 33% Both */ 279 data->old_pwm[0] = data->pwm[0]; 280 data->old_pwm[1] = data->pwm[1]; ··· 283 data->pwm[0] = data->old_pwm[0]; 284 data->pwm[1] = data->old_pwm[1]; 285 /* Restore PWM registers */ 286 + adm1031_write_value(client, ADM1031_REG_PWM, 287 data->pwm[0] | (data->pwm[1] << 4)); 288 } 289 } ··· 314 static ssize_t show_auto_temp_off(struct device *dev, char *buf, int nr) 315 { 316 struct adm1031_data *data = adm1031_update_device(dev); 317 + return sprintf(buf, "%d\n", 318 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr])); 319 } 320 static ssize_t show_auto_temp_min(struct device *dev, char *buf, int nr) ··· 407 int reg; 408 409 mutex_lock(&data->update_lock); 410 + if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) && 411 (((val>>4) & 0xf) != 5)) { 412 /* In automatic mode, the only PWM accepted is 33% */ 413 mutex_unlock(&data->update_lock); ··· 471 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0]) 472 || data->temp[1] >= 473 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]) 474 + || (data->chip_type == adm1031 475 && data->temp[2] >= 476 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2])); 477 break; ··· 514 515 mutex_lock(&data->update_lock); 516 if (val) { 517 + data->fan_min[nr] = 518 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr])); 519 } else { 520 data->fan_min[nr] = 0xff; ··· 535 536 tmp = val == 8 ? 0xc0 : 537 val == 4 ? 0x80 : 538 + val == 2 ? 0x40 : 539 + val == 1 ? 0x00 : 540 0xff; 541 if (tmp == 0xff) 542 return -EINVAL; 543 + 544 mutex_lock(&data->update_lock); 545 /* Get fresh readings */ 546 data->fan_div[nr] = adm1031_read_value(client, ··· 550 551 /* Write the new clock divider and fan min */ 552 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]); 553 + data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]); 554 + new_min = data->fan_min[nr] * old_div / val; 555 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min; 556 557 + adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr), 558 data->fan_div[nr]); 559 + adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), 560 data->fan_min[nr]); 561 562 /* Invalidate the cache: fan speed is no longer valid */ ··· 796 /* This function is called by i2c_probe */ 797 static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind) 798 { 799 + struct i2c_client *client; 800 struct adm1031_data *data; 801 int err = 0; 802 const char *name = ""; ··· 809 goto exit; 810 } 811 812 + client = &data->client; 813 + i2c_set_clientdata(client, data); 814 + client->addr = address; 815 + client->adapter = adapter; 816 + client->driver = &adm1031_driver; 817 818 if (kind < 0) { 819 int id, co; 820 + id = i2c_smbus_read_byte_data(client, 0x3d); 821 + co = i2c_smbus_read_byte_data(client, 0x3e); 822 823 if (!((id == 0x31 || id == 0x30) && co == 0x41)) 824 goto exit_free; ··· 840 } 841 data->chip_type = kind; 842 843 + strlcpy(client->name, name, I2C_NAME_SIZE); 844 mutex_init(&data->update_lock); 845 846 /* Tell the I2C layer a new client has arrived */ 847 + if ((err = i2c_attach_client(client))) 848 goto exit_free; 849 850 /* Initialize the ADM1031 chip */ 851 + adm1031_init_client(client); 852 853 /* Register sysfs hooks */ 854 + if ((err = sysfs_create_group(&client->dev.kobj, &adm1031_group))) 855 goto exit_detach; 856 857 if (kind == adm1031) { 858 + if ((err = sysfs_create_group(&client->dev.kobj, 859 &adm1031_group_opt))) 860 goto exit_remove; 861 } 862 863 + data->hwmon_dev = hwmon_device_register(&client->dev); 864 if (IS_ERR(data->hwmon_dev)) { 865 err = PTR_ERR(data->hwmon_dev); 866 goto exit_remove; ··· 870 return 0; 871 872 exit_remove: 873 + sysfs_remove_group(&client->dev.kobj, &adm1031_group); 874 + sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt); 875 exit_detach: 876 + i2c_detach_client(client); 877 exit_free: 878 kfree(data); 879 exit: ··· 905 if (data->chip_type == adm1031) { 906 mask |= (ADM1031_CONF2_PWM2_ENABLE | 907 ADM1031_CONF2_TACH2_ENABLE); 908 + } 909 /* Initialize the ADM1031 chip (enables fan speed reading ) */ 910 read_val = adm1031_read_value(client, ADM1031_REG_CONF2); 911 if ((read_val | mask) != read_val) { ··· 984 if (data->chip_type == adm1030) { 985 data->alarm &= 0xc0ff; 986 } 987 + 988 for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) { 989 data->fan_div[chan] = 990 adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan)); ··· 993 data->fan[chan] = 994 adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan)); 995 data->pwm[chan] = 996 + 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >> 997 (4*chan)); 998 } 999 data->last_updated = jiffies;