Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hwmon: (lm80) Convert fan display function macros into functions

Convert fan display function macros into functions to reduce
code size and improve code readability.

Code size reduction is about 200 bytes on x86_64.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>

+41 -34
+41 -34
drivers/hwmon/lm80.c
··· 116 116 i_num_in 117 117 }; 118 118 119 + enum fan_index { 120 + f_input, 121 + f_min, 122 + f_num_fan 123 + }; 124 + 119 125 /* 120 126 * Client data (each client gets its own) 121 127 */ ··· 134 128 unsigned long last_updated; /* In jiffies */ 135 129 136 130 u8 in[i_num_in][7]; /* Register value, 1st index is enum in_index */ 137 - u8 fan[2]; /* Register value */ 138 - u8 fan_min[2]; /* Register value */ 131 + u8 fan[f_num_fan][2]; /* Register value, 1st index enum fan_index */ 139 132 u8 fan_div[2]; /* Register encoding, shifted right */ 140 133 s16 temp[t_num_temp]; /* Register values, normalized to 16 bit */ 141 134 u16 alarms; /* Register encoding, combined */ ··· 212 207 return count; 213 208 } 214 209 215 - #define show_fan(suffix, value) \ 216 - static ssize_t show_fan_##suffix(struct device *dev, \ 217 - struct device_attribute *attr, char *buf) \ 218 - { \ 219 - int nr = to_sensor_dev_attr(attr)->index; \ 220 - struct lm80_data *data = lm80_update_device(dev); \ 221 - if (IS_ERR(data)) \ 222 - return PTR_ERR(data); \ 223 - return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[nr], \ 224 - DIV_FROM_REG(data->fan_div[nr]))); \ 210 + static ssize_t show_fan(struct device *dev, struct device_attribute *attr, 211 + char *buf) 212 + { 213 + int index = to_sensor_dev_attr_2(attr)->index; 214 + int nr = to_sensor_dev_attr_2(attr)->nr; 215 + struct lm80_data *data = lm80_update_device(dev); 216 + if (IS_ERR(data)) 217 + return PTR_ERR(data); 218 + return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr][index], 219 + DIV_FROM_REG(data->fan_div[index]))); 225 220 } 226 - show_fan(min, fan_min) 227 - show_fan(input, fan) 228 221 229 222 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr, 230 223 char *buf) ··· 237 234 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, 238 235 const char *buf, size_t count) 239 236 { 240 - int nr = to_sensor_dev_attr(attr)->index; 237 + int index = to_sensor_dev_attr_2(attr)->index; 238 + int nr = to_sensor_dev_attr_2(attr)->nr; 241 239 struct lm80_data *data = dev_get_drvdata(dev); 242 240 struct i2c_client *client = data->client; 243 241 unsigned long val; ··· 247 243 return err; 248 244 249 245 mutex_lock(&data->update_lock); 250 - data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); 251 - lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]); 246 + data->fan[nr][index] = FAN_TO_REG(val, 247 + DIV_FROM_REG(data->fan_div[index])); 248 + lm80_write_value(client, LM80_REG_FAN_MIN(index + 1), 249 + data->fan[nr][index]); 252 250 mutex_unlock(&data->update_lock); 253 251 return count; 254 252 } ··· 275 269 276 270 /* Save fan_min */ 277 271 mutex_lock(&data->update_lock); 278 - min = FAN_FROM_REG(data->fan_min[nr], 272 + min = FAN_FROM_REG(data->fan[f_min][nr], 279 273 DIV_FROM_REG(data->fan_div[nr])); 280 274 281 275 switch (val) { ··· 299 293 return -EINVAL; 300 294 } 301 295 302 - reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1)))) 303 - | (data->fan_div[nr] << (2 * (nr + 1))); 296 + reg = (lm80_read_value(client, LM80_REG_FANDIV) & 297 + ~(3 << (2 * (nr + 1)))) | (data->fan_div[nr] << (2 * (nr + 1))); 304 298 lm80_write_value(client, LM80_REG_FANDIV, reg); 305 299 306 300 /* Restore fan_min */ 307 - data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); 308 - lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]); 301 + data->fan[f_min][nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); 302 + lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), 303 + data->fan[f_min][nr]); 309 304 mutex_unlock(&data->update_lock); 310 305 311 306 return count; ··· 395 388 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, i_input, 4); 396 389 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, i_input, 5); 397 390 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, i_input, 6); 398 - static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, 399 - show_fan_min, set_fan_min, 0); 400 - static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, 401 - show_fan_min, set_fan_min, 1); 402 - static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0); 403 - static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1); 391 + static SENSOR_DEVICE_ATTR_2(fan1_min, S_IWUSR | S_IRUGO, 392 + show_fan, set_fan_min, f_min, 0); 393 + static SENSOR_DEVICE_ATTR_2(fan2_min, S_IWUSR | S_IRUGO, 394 + show_fan, set_fan_min, f_min, 1); 395 + static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, f_input, 0); 396 + static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, f_input, 1); 404 397 static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, 405 398 show_fan_div, set_fan_div, 0); 406 399 static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO, ··· 544 537 lm80_init_client(client); 545 538 546 539 /* A few vars need to be filled upon startup */ 547 - data->fan_min[0] = lm80_read_value(client, LM80_REG_FAN_MIN(1)); 548 - data->fan_min[1] = lm80_read_value(client, LM80_REG_FAN_MIN(2)); 540 + data->fan[f_min][0] = lm80_read_value(client, LM80_REG_FAN_MIN(1)); 541 + data->fan[f_min][1] = lm80_read_value(client, LM80_REG_FAN_MIN(2)); 549 542 550 543 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 551 544 data, lm80_groups); ··· 615 608 rv = lm80_read_value(client, LM80_REG_FAN1); 616 609 if (rv < 0) 617 610 goto abort; 618 - data->fan[0] = rv; 611 + data->fan[f_input][0] = rv; 619 612 620 613 rv = lm80_read_value(client, LM80_REG_FAN_MIN(1)); 621 614 if (rv < 0) 622 615 goto abort; 623 - data->fan_min[0] = rv; 616 + data->fan[f_min][0] = rv; 624 617 625 618 rv = lm80_read_value(client, LM80_REG_FAN2); 626 619 if (rv < 0) 627 620 goto abort; 628 - data->fan[1] = rv; 621 + data->fan[f_input][1] = rv; 629 622 630 623 rv = lm80_read_value(client, LM80_REG_FAN_MIN(2)); 631 624 if (rv < 0) 632 625 goto abort; 633 - data->fan_min[1] = rv; 626 + data->fan[f_min][1] = rv; 634 627 635 628 prev_rv = rv = lm80_read_value(client, LM80_REG_TEMP); 636 629 if (rv < 0)