rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives |
| 3 | * Copyright (C) 2007-2008, Advanced Micro Devices, Inc. |
| 4 | * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net> |
| 5 | * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com> |
| 6 | * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de> |
| 7 | * |
| 8 | * Derived from the lm83 driver by Jean Delvare |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/of_device.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/hwmon.h> |
| 21 | #include <linux/hwmon-sysfs.h> |
| 22 | #include <linux/hwmon-vid.h> |
| 23 | #include <linux/err.h> |
| 24 | #include <linux/jiffies.h> |
| 25 | #include <linux/util_macros.h> |
| 26 | |
| 27 | /* Indexes for the sysfs hooks */ |
| 28 | |
| 29 | #define INPUT 0 |
| 30 | #define MIN 1 |
| 31 | #define MAX 2 |
| 32 | #define CONTROL 3 |
| 33 | #define OFFSET 3 |
| 34 | #define AUTOMIN 4 |
| 35 | #define THERM 5 |
| 36 | #define HYSTERSIS 6 |
| 37 | |
| 38 | /* |
| 39 | * These are unique identifiers for the sysfs functions - unlike the |
| 40 | * numbers above, these are not also indexes into an array |
| 41 | */ |
| 42 | |
| 43 | #define ALARM 9 |
| 44 | #define FAULT 10 |
| 45 | |
| 46 | /* 7475 Common Registers */ |
| 47 | |
| 48 | #define REG_DEVREV2 0x12 /* ADT7490 only */ |
| 49 | |
| 50 | #define REG_VTT 0x1E /* ADT7490 only */ |
| 51 | #define REG_EXTEND3 0x1F /* ADT7490 only */ |
| 52 | |
| 53 | #define REG_VOLTAGE_BASE 0x20 |
| 54 | #define REG_TEMP_BASE 0x25 |
| 55 | #define REG_TACH_BASE 0x28 |
| 56 | #define REG_PWM_BASE 0x30 |
| 57 | #define REG_PWM_MAX_BASE 0x38 |
| 58 | |
| 59 | #define REG_DEVID 0x3D |
| 60 | #define REG_VENDID 0x3E |
| 61 | #define REG_DEVID2 0x3F |
| 62 | |
| 63 | #define REG_CONFIG1 0x40 |
| 64 | |
| 65 | #define REG_STATUS1 0x41 |
| 66 | #define REG_STATUS2 0x42 |
| 67 | |
| 68 | #define REG_VID 0x43 /* ADT7476 only */ |
| 69 | |
| 70 | #define REG_VOLTAGE_MIN_BASE 0x44 |
| 71 | #define REG_VOLTAGE_MAX_BASE 0x45 |
| 72 | |
| 73 | #define REG_TEMP_MIN_BASE 0x4E |
| 74 | #define REG_TEMP_MAX_BASE 0x4F |
| 75 | |
| 76 | #define REG_TACH_MIN_BASE 0x54 |
| 77 | |
| 78 | #define REG_PWM_CONFIG_BASE 0x5C |
| 79 | |
| 80 | #define REG_TEMP_TRANGE_BASE 0x5F |
| 81 | |
| 82 | #define REG_ENHANCE_ACOUSTICS1 0x62 |
| 83 | #define REG_ENHANCE_ACOUSTICS2 0x63 |
| 84 | |
| 85 | #define REG_PWM_MIN_BASE 0x64 |
| 86 | |
| 87 | #define REG_TEMP_TMIN_BASE 0x67 |
| 88 | #define REG_TEMP_THERM_BASE 0x6A |
| 89 | |
| 90 | #define REG_REMOTE1_HYSTERSIS 0x6D |
| 91 | #define REG_REMOTE2_HYSTERSIS 0x6E |
| 92 | |
| 93 | #define REG_TEMP_OFFSET_BASE 0x70 |
| 94 | |
| 95 | #define REG_CONFIG2 0x73 |
| 96 | |
| 97 | #define REG_EXTEND1 0x76 |
| 98 | #define REG_EXTEND2 0x77 |
| 99 | |
| 100 | #define REG_CONFIG3 0x78 |
| 101 | #define REG_CONFIG5 0x7C |
| 102 | #define REG_CONFIG4 0x7D |
| 103 | |
| 104 | #define REG_STATUS4 0x81 /* ADT7490 only */ |
| 105 | |
| 106 | #define REG_VTT_MIN 0x84 /* ADT7490 only */ |
| 107 | #define REG_VTT_MAX 0x86 /* ADT7490 only */ |
| 108 | |
| 109 | #define VID_VIDSEL 0x80 /* ADT7476 only */ |
| 110 | |
| 111 | #define CONFIG2_ATTN 0x20 |
| 112 | |
| 113 | #define CONFIG3_SMBALERT 0x01 |
| 114 | #define CONFIG3_THERM 0x02 |
| 115 | |
| 116 | #define CONFIG4_PINFUNC 0x03 |
| 117 | #define CONFIG4_MAXDUTY 0x08 |
| 118 | #define CONFIG4_ATTN_IN10 0x30 |
| 119 | #define CONFIG4_ATTN_IN43 0xC0 |
| 120 | |
| 121 | #define CONFIG5_TWOSCOMP 0x01 |
| 122 | #define CONFIG5_TEMPOFFSET 0x02 |
| 123 | #define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */ |
| 124 | |
| 125 | /* ADT7475 Settings */ |
| 126 | |
| 127 | #define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */ |
| 128 | #define ADT7475_TEMP_COUNT 3 |
| 129 | #define ADT7475_TACH_COUNT 4 |
| 130 | #define ADT7475_PWM_COUNT 3 |
| 131 | |
| 132 | /* Macro to read the registers */ |
| 133 | |
| 134 | #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg)) |
| 135 | |
| 136 | /* Macros to easily index the registers */ |
| 137 | |
| 138 | #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2)) |
| 139 | #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2)) |
| 140 | |
| 141 | #define PWM_REG(idx) (REG_PWM_BASE + (idx)) |
| 142 | #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx)) |
| 143 | #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx)) |
| 144 | #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx)) |
| 145 | |
| 146 | #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx)) |
| 147 | #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2)) |
| 148 | #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2)) |
| 149 | |
| 150 | #define TEMP_REG(idx) (REG_TEMP_BASE + (idx)) |
| 151 | #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2)) |
| 152 | #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2)) |
| 153 | #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx)) |
| 154 | #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx)) |
| 155 | #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx)) |
| 156 | #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx)) |
| 157 | |
| 158 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; |
| 159 | |
| 160 | enum chips { adt7473, adt7475, adt7476, adt7490 }; |
| 161 | |
| 162 | static const struct i2c_device_id adt7475_id[] = { |
| 163 | { "adt7473", adt7473 }, |
| 164 | { "adt7475", adt7475 }, |
| 165 | { "adt7476", adt7476 }, |
| 166 | { "adt7490", adt7490 }, |
| 167 | { } |
| 168 | }; |
| 169 | MODULE_DEVICE_TABLE(i2c, adt7475_id); |
| 170 | |
| 171 | static const struct of_device_id adt7475_of_match[] = { |
| 172 | { |
| 173 | .compatible = "adi,adt7473", |
| 174 | .data = (void *)adt7473 |
| 175 | }, |
| 176 | { |
| 177 | .compatible = "adi,adt7475", |
| 178 | .data = (void *)adt7475 |
| 179 | }, |
| 180 | { |
| 181 | .compatible = "adi,adt7476", |
| 182 | .data = (void *)adt7476 |
| 183 | }, |
| 184 | { |
| 185 | .compatible = "adi,adt7490", |
| 186 | .data = (void *)adt7490 |
| 187 | }, |
| 188 | { }, |
| 189 | }; |
| 190 | MODULE_DEVICE_TABLE(of, adt7475_of_match); |
| 191 | |
| 192 | struct adt7475_data { |
| 193 | struct device *hwmon_dev; |
| 194 | struct mutex lock; |
| 195 | |
| 196 | unsigned long measure_updated; |
| 197 | unsigned long limits_updated; |
| 198 | char valid; |
| 199 | |
| 200 | u8 config4; |
| 201 | u8 config5; |
| 202 | u8 has_voltage; |
| 203 | u8 bypass_attn; /* Bypass voltage attenuator */ |
| 204 | u8 has_pwm2:1; |
| 205 | u8 has_fan4:1; |
| 206 | u8 has_vid:1; |
| 207 | u32 alarms; |
| 208 | u16 voltage[3][6]; |
| 209 | u16 temp[7][3]; |
| 210 | u16 tach[2][4]; |
| 211 | u8 pwm[4][3]; |
| 212 | u8 range[3]; |
| 213 | u8 pwmctl[3]; |
| 214 | u8 pwmchan[3]; |
| 215 | u8 enh_acoustics[2]; |
| 216 | |
| 217 | u8 vid; |
| 218 | u8 vrm; |
| 219 | }; |
| 220 | |
| 221 | static struct i2c_driver adt7475_driver; |
| 222 | static struct adt7475_data *adt7475_update_device(struct device *dev); |
| 223 | static void adt7475_read_hystersis(struct i2c_client *client); |
| 224 | static void adt7475_read_pwm(struct i2c_client *client, int index); |
| 225 | |
| 226 | /* Given a temp value, convert it to register value */ |
| 227 | |
| 228 | static inline u16 temp2reg(struct adt7475_data *data, long val) |
| 229 | { |
| 230 | u16 ret; |
| 231 | |
| 232 | if (!(data->config5 & CONFIG5_TWOSCOMP)) { |
| 233 | val = clamp_val(val, -64000, 191000); |
| 234 | ret = (val + 64500) / 1000; |
| 235 | } else { |
| 236 | val = clamp_val(val, -128000, 127000); |
| 237 | if (val < -500) |
| 238 | ret = (256500 + val) / 1000; |
| 239 | else |
| 240 | ret = (val + 500) / 1000; |
| 241 | } |
| 242 | |
| 243 | return ret << 2; |
| 244 | } |
| 245 | |
| 246 | /* Given a register value, convert it to a real temp value */ |
| 247 | |
| 248 | static inline int reg2temp(struct adt7475_data *data, u16 reg) |
| 249 | { |
| 250 | if (data->config5 & CONFIG5_TWOSCOMP) { |
| 251 | if (reg >= 512) |
| 252 | return (reg - 1024) * 250; |
| 253 | else |
| 254 | return reg * 250; |
| 255 | } else |
| 256 | return (reg - 256) * 250; |
| 257 | } |
| 258 | |
| 259 | static inline int tach2rpm(u16 tach) |
| 260 | { |
| 261 | if (tach == 0 || tach == 0xFFFF) |
| 262 | return 0; |
| 263 | |
| 264 | return (90000 * 60) / tach; |
| 265 | } |
| 266 | |
| 267 | static inline u16 rpm2tach(unsigned long rpm) |
| 268 | { |
| 269 | if (rpm == 0) |
| 270 | return 0; |
| 271 | |
| 272 | return clamp_val((90000 * 60) / rpm, 1, 0xFFFF); |
| 273 | } |
| 274 | |
| 275 | /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */ |
| 276 | static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = { |
| 277 | { 45, 94 }, /* +2.5V */ |
| 278 | { 175, 525 }, /* Vccp */ |
| 279 | { 68, 71 }, /* Vcc */ |
| 280 | { 93, 47 }, /* +5V */ |
| 281 | { 120, 20 }, /* +12V */ |
| 282 | { 45, 45 }, /* Vtt */ |
| 283 | }; |
| 284 | |
| 285 | static inline int reg2volt(int channel, u16 reg, u8 bypass_attn) |
| 286 | { |
| 287 | const int *r = adt7473_in_scaling[channel]; |
| 288 | |
| 289 | if (bypass_attn & (1 << channel)) |
| 290 | return DIV_ROUND_CLOSEST(reg * 2250, 1024); |
| 291 | return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024); |
| 292 | } |
| 293 | |
| 294 | static inline u16 volt2reg(int channel, long volt, u8 bypass_attn) |
| 295 | { |
| 296 | const int *r = adt7473_in_scaling[channel]; |
| 297 | long reg; |
| 298 | |
| 299 | if (bypass_attn & (1 << channel)) |
| 300 | reg = DIV_ROUND_CLOSEST(volt * 1024, 2250); |
| 301 | else |
| 302 | reg = DIV_ROUND_CLOSEST(volt * r[1] * 1024, |
| 303 | (r[0] + r[1]) * 2250); |
| 304 | return clamp_val(reg, 0, 1023) & (0xff << 2); |
| 305 | } |
| 306 | |
| 307 | static int adt7475_read_word(struct i2c_client *client, int reg) |
| 308 | { |
| 309 | int val1, val2; |
| 310 | |
| 311 | val1 = i2c_smbus_read_byte_data(client, reg); |
| 312 | if (val1 < 0) |
| 313 | return val1; |
| 314 | val2 = i2c_smbus_read_byte_data(client, reg + 1); |
| 315 | if (val2 < 0) |
| 316 | return val2; |
| 317 | |
| 318 | return val1 | (val2 << 8); |
| 319 | } |
| 320 | |
| 321 | static void adt7475_write_word(struct i2c_client *client, int reg, u16 val) |
| 322 | { |
| 323 | i2c_smbus_write_byte_data(client, reg + 1, val >> 8); |
| 324 | i2c_smbus_write_byte_data(client, reg, val & 0xFF); |
| 325 | } |
| 326 | |
| 327 | static ssize_t show_voltage(struct device *dev, struct device_attribute *attr, |
| 328 | char *buf) |
| 329 | { |
| 330 | struct adt7475_data *data = adt7475_update_device(dev); |
| 331 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 332 | unsigned short val; |
| 333 | |
| 334 | switch (sattr->nr) { |
| 335 | case ALARM: |
| 336 | return sprintf(buf, "%d\n", |
| 337 | (data->alarms >> sattr->index) & 1); |
| 338 | default: |
| 339 | val = data->voltage[sattr->nr][sattr->index]; |
| 340 | return sprintf(buf, "%d\n", |
| 341 | reg2volt(sattr->index, val, data->bypass_attn)); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | static ssize_t set_voltage(struct device *dev, struct device_attribute *attr, |
| 346 | const char *buf, size_t count) |
| 347 | { |
| 348 | |
| 349 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 350 | struct i2c_client *client = to_i2c_client(dev); |
| 351 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 352 | unsigned char reg; |
| 353 | long val; |
| 354 | |
| 355 | if (kstrtol(buf, 10, &val)) |
| 356 | return -EINVAL; |
| 357 | |
| 358 | mutex_lock(&data->lock); |
| 359 | |
| 360 | data->voltage[sattr->nr][sattr->index] = |
| 361 | volt2reg(sattr->index, val, data->bypass_attn); |
| 362 | |
| 363 | if (sattr->index < ADT7475_VOLTAGE_COUNT) { |
| 364 | if (sattr->nr == MIN) |
| 365 | reg = VOLTAGE_MIN_REG(sattr->index); |
| 366 | else |
| 367 | reg = VOLTAGE_MAX_REG(sattr->index); |
| 368 | } else { |
| 369 | if (sattr->nr == MIN) |
| 370 | reg = REG_VTT_MIN; |
| 371 | else |
| 372 | reg = REG_VTT_MAX; |
| 373 | } |
| 374 | |
| 375 | i2c_smbus_write_byte_data(client, reg, |
| 376 | data->voltage[sattr->nr][sattr->index] >> 2); |
| 377 | mutex_unlock(&data->lock); |
| 378 | |
| 379 | return count; |
| 380 | } |
| 381 | |
| 382 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, |
| 383 | char *buf) |
| 384 | { |
| 385 | struct adt7475_data *data = adt7475_update_device(dev); |
| 386 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 387 | int out; |
| 388 | |
| 389 | switch (sattr->nr) { |
| 390 | case HYSTERSIS: |
| 391 | mutex_lock(&data->lock); |
| 392 | out = data->temp[sattr->nr][sattr->index]; |
| 393 | if (sattr->index != 1) |
| 394 | out = (out >> 4) & 0xF; |
| 395 | else |
| 396 | out = (out & 0xF); |
| 397 | /* |
| 398 | * Show the value as an absolute number tied to |
| 399 | * THERM |
| 400 | */ |
| 401 | out = reg2temp(data, data->temp[THERM][sattr->index]) - |
| 402 | out * 1000; |
| 403 | mutex_unlock(&data->lock); |
| 404 | break; |
| 405 | |
| 406 | case OFFSET: |
| 407 | /* |
| 408 | * Offset is always 2's complement, regardless of the |
| 409 | * setting in CONFIG5 |
| 410 | */ |
| 411 | mutex_lock(&data->lock); |
| 412 | out = (s8)data->temp[sattr->nr][sattr->index]; |
| 413 | if (data->config5 & CONFIG5_TEMPOFFSET) |
| 414 | out *= 1000; |
| 415 | else |
| 416 | out *= 500; |
| 417 | mutex_unlock(&data->lock); |
| 418 | break; |
| 419 | |
| 420 | case ALARM: |
| 421 | out = (data->alarms >> (sattr->index + 4)) & 1; |
| 422 | break; |
| 423 | |
| 424 | case FAULT: |
| 425 | /* Note - only for remote1 and remote2 */ |
| 426 | out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000)); |
| 427 | break; |
| 428 | |
| 429 | default: |
| 430 | /* All other temp values are in the configured format */ |
| 431 | out = reg2temp(data, data->temp[sattr->nr][sattr->index]); |
| 432 | } |
| 433 | |
| 434 | return sprintf(buf, "%d\n", out); |
| 435 | } |
| 436 | |
| 437 | static ssize_t set_temp(struct device *dev, struct device_attribute *attr, |
| 438 | const char *buf, size_t count) |
| 439 | { |
| 440 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 441 | struct i2c_client *client = to_i2c_client(dev); |
| 442 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 443 | unsigned char reg = 0; |
| 444 | u8 out; |
| 445 | int temp; |
| 446 | long val; |
| 447 | |
| 448 | if (kstrtol(buf, 10, &val)) |
| 449 | return -EINVAL; |
| 450 | |
| 451 | mutex_lock(&data->lock); |
| 452 | |
| 453 | /* We need the config register in all cases for temp <-> reg conv. */ |
| 454 | data->config5 = adt7475_read(REG_CONFIG5); |
| 455 | |
| 456 | switch (sattr->nr) { |
| 457 | case OFFSET: |
| 458 | if (data->config5 & CONFIG5_TEMPOFFSET) { |
| 459 | val = clamp_val(val, -63000, 127000); |
| 460 | out = data->temp[OFFSET][sattr->index] = val / 1000; |
| 461 | } else { |
| 462 | val = clamp_val(val, -63000, 64000); |
| 463 | out = data->temp[OFFSET][sattr->index] = val / 500; |
| 464 | } |
| 465 | break; |
| 466 | |
| 467 | case HYSTERSIS: |
| 468 | /* |
| 469 | * The value will be given as an absolute value, turn it |
| 470 | * into an offset based on THERM |
| 471 | */ |
| 472 | |
| 473 | /* Read fresh THERM and HYSTERSIS values from the chip */ |
| 474 | data->temp[THERM][sattr->index] = |
| 475 | adt7475_read(TEMP_THERM_REG(sattr->index)) << 2; |
| 476 | adt7475_read_hystersis(client); |
| 477 | |
| 478 | temp = reg2temp(data, data->temp[THERM][sattr->index]); |
| 479 | val = clamp_val(val, temp - 15000, temp); |
| 480 | val = (temp - val) / 1000; |
| 481 | |
| 482 | if (sattr->index != 1) { |
| 483 | data->temp[HYSTERSIS][sattr->index] &= 0xF0; |
| 484 | data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4; |
| 485 | } else { |
| 486 | data->temp[HYSTERSIS][sattr->index] &= 0x0F; |
| 487 | data->temp[HYSTERSIS][sattr->index] |= (val & 0xF); |
| 488 | } |
| 489 | |
| 490 | out = data->temp[HYSTERSIS][sattr->index]; |
| 491 | break; |
| 492 | |
| 493 | default: |
| 494 | data->temp[sattr->nr][sattr->index] = temp2reg(data, val); |
| 495 | |
| 496 | /* |
| 497 | * We maintain an extra 2 digits of precision for simplicity |
| 498 | * - shift those back off before writing the value |
| 499 | */ |
| 500 | out = (u8) (data->temp[sattr->nr][sattr->index] >> 2); |
| 501 | } |
| 502 | |
| 503 | switch (sattr->nr) { |
| 504 | case MIN: |
| 505 | reg = TEMP_MIN_REG(sattr->index); |
| 506 | break; |
| 507 | case MAX: |
| 508 | reg = TEMP_MAX_REG(sattr->index); |
| 509 | break; |
| 510 | case OFFSET: |
| 511 | reg = TEMP_OFFSET_REG(sattr->index); |
| 512 | break; |
| 513 | case AUTOMIN: |
| 514 | reg = TEMP_TMIN_REG(sattr->index); |
| 515 | break; |
| 516 | case THERM: |
| 517 | reg = TEMP_THERM_REG(sattr->index); |
| 518 | break; |
| 519 | case HYSTERSIS: |
| 520 | if (sattr->index != 2) |
| 521 | reg = REG_REMOTE1_HYSTERSIS; |
| 522 | else |
| 523 | reg = REG_REMOTE2_HYSTERSIS; |
| 524 | |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | i2c_smbus_write_byte_data(client, reg, out); |
| 529 | |
| 530 | mutex_unlock(&data->lock); |
| 531 | return count; |
| 532 | } |
| 533 | |
| 534 | /* Assuming CONFIG6[SLOW] is 0 */ |
| 535 | static const int ad7475_st_map[] = { |
| 536 | 37500, 18800, 12500, 7500, 4700, 3100, 1600, 800, |
| 537 | }; |
| 538 | |
| 539 | static ssize_t show_temp_st(struct device *dev, struct device_attribute *attr, |
| 540 | char *buf) |
| 541 | { |
| 542 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 543 | struct i2c_client *client = to_i2c_client(dev); |
| 544 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 545 | long val; |
| 546 | |
| 547 | switch (sattr->index) { |
| 548 | case 0: |
| 549 | val = data->enh_acoustics[0] & 0xf; |
| 550 | break; |
| 551 | case 1: |
| 552 | val = (data->enh_acoustics[1] >> 4) & 0xf; |
| 553 | break; |
| 554 | case 2: |
| 555 | default: |
| 556 | val = data->enh_acoustics[1] & 0xf; |
| 557 | break; |
| 558 | } |
| 559 | |
| 560 | if (val & 0x8) |
| 561 | return sprintf(buf, "%d\n", ad7475_st_map[val & 0x7]); |
| 562 | else |
| 563 | return sprintf(buf, "0\n"); |
| 564 | } |
| 565 | |
| 566 | static ssize_t set_temp_st(struct device *dev, struct device_attribute *attr, |
| 567 | const char *buf, size_t count) |
| 568 | { |
| 569 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 570 | struct i2c_client *client = to_i2c_client(dev); |
| 571 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 572 | unsigned char reg; |
| 573 | int shift, idx; |
| 574 | ulong val; |
| 575 | |
| 576 | if (kstrtoul(buf, 10, &val)) |
| 577 | return -EINVAL; |
| 578 | |
| 579 | switch (sattr->index) { |
| 580 | case 0: |
| 581 | reg = REG_ENHANCE_ACOUSTICS1; |
| 582 | shift = 0; |
| 583 | idx = 0; |
| 584 | break; |
| 585 | case 1: |
| 586 | reg = REG_ENHANCE_ACOUSTICS2; |
| 587 | shift = 0; |
| 588 | idx = 1; |
| 589 | break; |
| 590 | case 2: |
| 591 | default: |
| 592 | reg = REG_ENHANCE_ACOUSTICS2; |
| 593 | shift = 4; |
| 594 | idx = 1; |
| 595 | break; |
| 596 | } |
| 597 | |
| 598 | if (val > 0) { |
| 599 | val = find_closest_descending(val, ad7475_st_map, |
| 600 | ARRAY_SIZE(ad7475_st_map)); |
| 601 | val |= 0x8; |
| 602 | } |
| 603 | |
| 604 | mutex_lock(&data->lock); |
| 605 | |
| 606 | data->enh_acoustics[idx] &= ~(0xf << shift); |
| 607 | data->enh_acoustics[idx] |= (val << shift); |
| 608 | |
| 609 | i2c_smbus_write_byte_data(client, reg, data->enh_acoustics[idx]); |
| 610 | |
| 611 | mutex_unlock(&data->lock); |
| 612 | |
| 613 | return count; |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * Table of autorange values - the user will write the value in millidegrees, |
| 618 | * and we'll convert it |
| 619 | */ |
| 620 | static const int autorange_table[] = { |
| 621 | 2000, 2500, 3330, 4000, 5000, 6670, 8000, |
| 622 | 10000, 13330, 16000, 20000, 26670, 32000, 40000, |
| 623 | 53330, 80000 |
| 624 | }; |
| 625 | |
| 626 | static ssize_t show_point2(struct device *dev, struct device_attribute *attr, |
| 627 | char *buf) |
| 628 | { |
| 629 | struct adt7475_data *data = adt7475_update_device(dev); |
| 630 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 631 | int out, val; |
| 632 | |
| 633 | mutex_lock(&data->lock); |
| 634 | out = (data->range[sattr->index] >> 4) & 0x0F; |
| 635 | val = reg2temp(data, data->temp[AUTOMIN][sattr->index]); |
| 636 | mutex_unlock(&data->lock); |
| 637 | |
| 638 | return sprintf(buf, "%d\n", val + autorange_table[out]); |
| 639 | } |
| 640 | |
| 641 | static ssize_t set_point2(struct device *dev, struct device_attribute *attr, |
| 642 | const char *buf, size_t count) |
| 643 | { |
| 644 | struct i2c_client *client = to_i2c_client(dev); |
| 645 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 646 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 647 | int temp; |
| 648 | long val; |
| 649 | |
| 650 | if (kstrtol(buf, 10, &val)) |
| 651 | return -EINVAL; |
| 652 | |
| 653 | mutex_lock(&data->lock); |
| 654 | |
| 655 | /* Get a fresh copy of the needed registers */ |
| 656 | data->config5 = adt7475_read(REG_CONFIG5); |
| 657 | data->temp[AUTOMIN][sattr->index] = |
| 658 | adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2; |
| 659 | data->range[sattr->index] = |
| 660 | adt7475_read(TEMP_TRANGE_REG(sattr->index)); |
| 661 | |
| 662 | /* |
| 663 | * The user will write an absolute value, so subtract the start point |
| 664 | * to figure the range |
| 665 | */ |
| 666 | temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]); |
| 667 | val = clamp_val(val, temp + autorange_table[0], |
| 668 | temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]); |
| 669 | val -= temp; |
| 670 | |
| 671 | /* Find the nearest table entry to what the user wrote */ |
| 672 | val = find_closest(val, autorange_table, ARRAY_SIZE(autorange_table)); |
| 673 | |
| 674 | data->range[sattr->index] &= ~0xF0; |
| 675 | data->range[sattr->index] |= val << 4; |
| 676 | |
| 677 | i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), |
| 678 | data->range[sattr->index]); |
| 679 | |
| 680 | mutex_unlock(&data->lock); |
| 681 | return count; |
| 682 | } |
| 683 | |
| 684 | static ssize_t show_tach(struct device *dev, struct device_attribute *attr, |
| 685 | char *buf) |
| 686 | { |
| 687 | struct adt7475_data *data = adt7475_update_device(dev); |
| 688 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 689 | int out; |
| 690 | |
| 691 | if (sattr->nr == ALARM) |
| 692 | out = (data->alarms >> (sattr->index + 10)) & 1; |
| 693 | else |
| 694 | out = tach2rpm(data->tach[sattr->nr][sattr->index]); |
| 695 | |
| 696 | return sprintf(buf, "%d\n", out); |
| 697 | } |
| 698 | |
| 699 | static ssize_t set_tach(struct device *dev, struct device_attribute *attr, |
| 700 | const char *buf, size_t count) |
| 701 | { |
| 702 | |
| 703 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 704 | struct i2c_client *client = to_i2c_client(dev); |
| 705 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 706 | unsigned long val; |
| 707 | |
| 708 | if (kstrtoul(buf, 10, &val)) |
| 709 | return -EINVAL; |
| 710 | |
| 711 | mutex_lock(&data->lock); |
| 712 | |
| 713 | data->tach[MIN][sattr->index] = rpm2tach(val); |
| 714 | |
| 715 | adt7475_write_word(client, TACH_MIN_REG(sattr->index), |
| 716 | data->tach[MIN][sattr->index]); |
| 717 | |
| 718 | mutex_unlock(&data->lock); |
| 719 | return count; |
| 720 | } |
| 721 | |
| 722 | static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, |
| 723 | char *buf) |
| 724 | { |
| 725 | struct adt7475_data *data = adt7475_update_device(dev); |
| 726 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 727 | |
| 728 | return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]); |
| 729 | } |
| 730 | |
| 731 | static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr, |
| 732 | char *buf) |
| 733 | { |
| 734 | struct adt7475_data *data = adt7475_update_device(dev); |
| 735 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 736 | |
| 737 | return sprintf(buf, "%d\n", data->pwmchan[sattr->index]); |
| 738 | } |
| 739 | |
| 740 | static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr, |
| 741 | char *buf) |
| 742 | { |
| 743 | struct adt7475_data *data = adt7475_update_device(dev); |
| 744 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 745 | |
| 746 | return sprintf(buf, "%d\n", data->pwmctl[sattr->index]); |
| 747 | } |
| 748 | |
| 749 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, |
| 750 | const char *buf, size_t count) |
| 751 | { |
| 752 | |
| 753 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 754 | struct i2c_client *client = to_i2c_client(dev); |
| 755 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 756 | unsigned char reg = 0; |
| 757 | long val; |
| 758 | |
| 759 | if (kstrtol(buf, 10, &val)) |
| 760 | return -EINVAL; |
| 761 | |
| 762 | mutex_lock(&data->lock); |
| 763 | |
| 764 | switch (sattr->nr) { |
| 765 | case INPUT: |
| 766 | /* Get a fresh value for CONTROL */ |
| 767 | data->pwm[CONTROL][sattr->index] = |
| 768 | adt7475_read(PWM_CONFIG_REG(sattr->index)); |
| 769 | |
| 770 | /* |
| 771 | * If we are not in manual mode, then we shouldn't allow |
| 772 | * the user to set the pwm speed |
| 773 | */ |
| 774 | if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) { |
| 775 | mutex_unlock(&data->lock); |
| 776 | return count; |
| 777 | } |
| 778 | |
| 779 | reg = PWM_REG(sattr->index); |
| 780 | break; |
| 781 | |
| 782 | case MIN: |
| 783 | reg = PWM_MIN_REG(sattr->index); |
| 784 | break; |
| 785 | |
| 786 | case MAX: |
| 787 | reg = PWM_MAX_REG(sattr->index); |
| 788 | break; |
| 789 | } |
| 790 | |
| 791 | data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF); |
| 792 | i2c_smbus_write_byte_data(client, reg, |
| 793 | data->pwm[sattr->nr][sattr->index]); |
| 794 | mutex_unlock(&data->lock); |
| 795 | |
| 796 | return count; |
| 797 | } |
| 798 | |
| 799 | static ssize_t show_stall_disable(struct device *dev, |
| 800 | struct device_attribute *attr, char *buf) |
| 801 | { |
| 802 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 803 | struct i2c_client *client = to_i2c_client(dev); |
| 804 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 805 | u8 mask = BIT(5 + sattr->index); |
| 806 | |
| 807 | return sprintf(buf, "%d\n", !!(data->enh_acoustics[0] & mask)); |
| 808 | } |
| 809 | |
| 810 | static ssize_t set_stall_disable(struct device *dev, |
| 811 | struct device_attribute *attr, const char *buf, |
| 812 | size_t count) |
| 813 | { |
| 814 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 815 | struct i2c_client *client = to_i2c_client(dev); |
| 816 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 817 | long val; |
| 818 | u8 mask = BIT(5 + sattr->index); |
| 819 | |
| 820 | if (kstrtol(buf, 10, &val)) |
| 821 | return -EINVAL; |
| 822 | |
| 823 | mutex_lock(&data->lock); |
| 824 | |
| 825 | data->enh_acoustics[0] &= ~mask; |
| 826 | if (val) |
| 827 | data->enh_acoustics[0] |= mask; |
| 828 | |
| 829 | i2c_smbus_write_byte_data(client, REG_ENHANCE_ACOUSTICS1, |
| 830 | data->enh_acoustics[0]); |
| 831 | |
| 832 | mutex_unlock(&data->lock); |
| 833 | |
| 834 | return count; |
| 835 | } |
| 836 | |
| 837 | /* Called by set_pwmctrl and set_pwmchan */ |
| 838 | |
| 839 | static int hw_set_pwm(struct i2c_client *client, int index, |
| 840 | unsigned int pwmctl, unsigned int pwmchan) |
| 841 | { |
| 842 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 843 | long val = 0; |
| 844 | |
| 845 | switch (pwmctl) { |
| 846 | case 0: |
| 847 | val = 0x03; /* Run at full speed */ |
| 848 | break; |
| 849 | case 1: |
| 850 | val = 0x07; /* Manual mode */ |
| 851 | break; |
| 852 | case 2: |
| 853 | switch (pwmchan) { |
| 854 | case 1: |
| 855 | /* Remote1 controls PWM */ |
| 856 | val = 0x00; |
| 857 | break; |
| 858 | case 2: |
| 859 | /* local controls PWM */ |
| 860 | val = 0x01; |
| 861 | break; |
| 862 | case 4: |
| 863 | /* remote2 controls PWM */ |
| 864 | val = 0x02; |
| 865 | break; |
| 866 | case 6: |
| 867 | /* local/remote2 control PWM */ |
| 868 | val = 0x05; |
| 869 | break; |
| 870 | case 7: |
| 871 | /* All three control PWM */ |
| 872 | val = 0x06; |
| 873 | break; |
| 874 | default: |
| 875 | return -EINVAL; |
| 876 | } |
| 877 | break; |
| 878 | default: |
| 879 | return -EINVAL; |
| 880 | } |
| 881 | |
| 882 | data->pwmctl[index] = pwmctl; |
| 883 | data->pwmchan[index] = pwmchan; |
| 884 | |
| 885 | data->pwm[CONTROL][index] &= ~0xE0; |
| 886 | data->pwm[CONTROL][index] |= (val & 7) << 5; |
| 887 | |
| 888 | i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), |
| 889 | data->pwm[CONTROL][index]); |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr, |
| 895 | const char *buf, size_t count) |
| 896 | { |
| 897 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 898 | struct i2c_client *client = to_i2c_client(dev); |
| 899 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 900 | int r; |
| 901 | long val; |
| 902 | |
| 903 | if (kstrtol(buf, 10, &val)) |
| 904 | return -EINVAL; |
| 905 | |
| 906 | mutex_lock(&data->lock); |
| 907 | /* Read Modify Write PWM values */ |
| 908 | adt7475_read_pwm(client, sattr->index); |
| 909 | r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val); |
| 910 | if (r) |
| 911 | count = r; |
| 912 | mutex_unlock(&data->lock); |
| 913 | |
| 914 | return count; |
| 915 | } |
| 916 | |
| 917 | static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr, |
| 918 | const char *buf, size_t count) |
| 919 | { |
| 920 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 921 | struct i2c_client *client = to_i2c_client(dev); |
| 922 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 923 | int r; |
| 924 | long val; |
| 925 | |
| 926 | if (kstrtol(buf, 10, &val)) |
| 927 | return -EINVAL; |
| 928 | |
| 929 | mutex_lock(&data->lock); |
| 930 | /* Read Modify Write PWM values */ |
| 931 | adt7475_read_pwm(client, sattr->index); |
| 932 | r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]); |
| 933 | if (r) |
| 934 | count = r; |
| 935 | mutex_unlock(&data->lock); |
| 936 | |
| 937 | return count; |
| 938 | } |
| 939 | |
| 940 | /* List of frequencies for the PWM */ |
| 941 | static const int pwmfreq_table[] = { |
| 942 | 11, 14, 22, 29, 35, 44, 58, 88, 22500 |
| 943 | }; |
| 944 | |
| 945 | static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr, |
| 946 | char *buf) |
| 947 | { |
| 948 | struct adt7475_data *data = adt7475_update_device(dev); |
| 949 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 950 | int i = clamp_val(data->range[sattr->index] & 0xf, 0, |
| 951 | ARRAY_SIZE(pwmfreq_table) - 1); |
| 952 | |
| 953 | return sprintf(buf, "%d\n", pwmfreq_table[i]); |
| 954 | } |
| 955 | |
| 956 | static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr, |
| 957 | const char *buf, size_t count) |
| 958 | { |
| 959 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 960 | struct i2c_client *client = to_i2c_client(dev); |
| 961 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 962 | int out; |
| 963 | long val; |
| 964 | |
| 965 | if (kstrtol(buf, 10, &val)) |
| 966 | return -EINVAL; |
| 967 | |
| 968 | out = find_closest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table)); |
| 969 | |
| 970 | mutex_lock(&data->lock); |
| 971 | |
| 972 | data->range[sattr->index] = |
| 973 | adt7475_read(TEMP_TRANGE_REG(sattr->index)); |
| 974 | data->range[sattr->index] &= ~0xf; |
| 975 | data->range[sattr->index] |= out; |
| 976 | |
| 977 | i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), |
| 978 | data->range[sattr->index]); |
| 979 | |
| 980 | mutex_unlock(&data->lock); |
| 981 | return count; |
| 982 | } |
| 983 | |
| 984 | static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev, |
| 985 | struct device_attribute *devattr, |
| 986 | char *buf) |
| 987 | { |
| 988 | struct adt7475_data *data = adt7475_update_device(dev); |
| 989 | return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY)); |
| 990 | } |
| 991 | |
| 992 | static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev, |
| 993 | struct device_attribute *devattr, |
| 994 | const char *buf, size_t count) |
| 995 | { |
| 996 | struct i2c_client *client = to_i2c_client(dev); |
| 997 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 998 | long val; |
| 999 | |
| 1000 | if (kstrtol(buf, 10, &val)) |
| 1001 | return -EINVAL; |
| 1002 | if (val != 0 && val != 1) |
| 1003 | return -EINVAL; |
| 1004 | |
| 1005 | mutex_lock(&data->lock); |
| 1006 | data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4); |
| 1007 | if (val) |
| 1008 | data->config4 |= CONFIG4_MAXDUTY; |
| 1009 | else |
| 1010 | data->config4 &= ~CONFIG4_MAXDUTY; |
| 1011 | i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4); |
| 1012 | mutex_unlock(&data->lock); |
| 1013 | |
| 1014 | return count; |
| 1015 | } |
| 1016 | |
| 1017 | static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr, |
| 1018 | char *buf) |
| 1019 | { |
| 1020 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 1021 | return sprintf(buf, "%d\n", (int)data->vrm); |
| 1022 | } |
| 1023 | |
| 1024 | static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr, |
| 1025 | const char *buf, size_t count) |
| 1026 | { |
| 1027 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 1028 | long val; |
| 1029 | |
| 1030 | if (kstrtol(buf, 10, &val)) |
| 1031 | return -EINVAL; |
| 1032 | if (val < 0 || val > 255) |
| 1033 | return -EINVAL; |
| 1034 | data->vrm = val; |
| 1035 | |
| 1036 | return count; |
| 1037 | } |
| 1038 | |
| 1039 | static ssize_t cpu0_vid_show(struct device *dev, |
| 1040 | struct device_attribute *devattr, char *buf) |
| 1041 | { |
| 1042 | struct adt7475_data *data = adt7475_update_device(dev); |
| 1043 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 1044 | } |
| 1045 | |
| 1046 | static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0); |
| 1047 | static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage, |
| 1048 | set_voltage, MAX, 0); |
| 1049 | static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage, |
| 1050 | set_voltage, MIN, 0); |
| 1051 | static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0); |
| 1052 | static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1); |
| 1053 | static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage, |
| 1054 | set_voltage, MAX, 1); |
| 1055 | static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage, |
| 1056 | set_voltage, MIN, 1); |
| 1057 | static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1); |
| 1058 | static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2); |
| 1059 | static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage, |
| 1060 | set_voltage, MAX, 2); |
| 1061 | static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage, |
| 1062 | set_voltage, MIN, 2); |
| 1063 | static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2); |
| 1064 | static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3); |
| 1065 | static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage, |
| 1066 | set_voltage, MAX, 3); |
| 1067 | static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage, |
| 1068 | set_voltage, MIN, 3); |
| 1069 | static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3); |
| 1070 | static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4); |
| 1071 | static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage, |
| 1072 | set_voltage, MAX, 4); |
| 1073 | static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage, |
| 1074 | set_voltage, MIN, 4); |
| 1075 | static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8); |
| 1076 | static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5); |
| 1077 | static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage, |
| 1078 | set_voltage, MAX, 5); |
| 1079 | static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage, |
| 1080 | set_voltage, MIN, 5); |
| 1081 | static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31); |
| 1082 | static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0); |
| 1083 | static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0); |
| 1084 | static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0); |
| 1085 | static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 1086 | MAX, 0); |
| 1087 | static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 1088 | MIN, 0); |
| 1089 | static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp, |
| 1090 | set_temp, OFFSET, 0); |
| 1091 | static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR, |
| 1092 | show_temp, set_temp, AUTOMIN, 0); |
| 1093 | static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR, |
| 1094 | show_point2, set_point2, 0, 0); |
| 1095 | static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 1096 | THERM, 0); |
| 1097 | static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp, |
| 1098 | set_temp, HYSTERSIS, 0); |
| 1099 | static SENSOR_DEVICE_ATTR_2(temp1_smoothing, S_IRUGO | S_IWUSR, show_temp_st, |
| 1100 | set_temp_st, 0, 0); |
| 1101 | static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1); |
| 1102 | static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1); |
| 1103 | static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 1104 | MAX, 1); |
| 1105 | static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 1106 | MIN, 1); |
| 1107 | static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp, |
| 1108 | set_temp, OFFSET, 1); |
| 1109 | static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR, |
| 1110 | show_temp, set_temp, AUTOMIN, 1); |
| 1111 | static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR, |
| 1112 | show_point2, set_point2, 0, 1); |
| 1113 | static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 1114 | THERM, 1); |
| 1115 | static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp, |
| 1116 | set_temp, HYSTERSIS, 1); |
| 1117 | static SENSOR_DEVICE_ATTR_2(temp2_smoothing, S_IRUGO | S_IWUSR, show_temp_st, |
| 1118 | set_temp_st, 0, 1); |
| 1119 | static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2); |
| 1120 | static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2); |
| 1121 | static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2); |
| 1122 | static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 1123 | MAX, 2); |
| 1124 | static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 1125 | MIN, 2); |
| 1126 | static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp, |
| 1127 | set_temp, OFFSET, 2); |
| 1128 | static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR, |
| 1129 | show_temp, set_temp, AUTOMIN, 2); |
| 1130 | static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR, |
| 1131 | show_point2, set_point2, 0, 2); |
| 1132 | static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 1133 | THERM, 2); |
| 1134 | static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp, |
| 1135 | set_temp, HYSTERSIS, 2); |
| 1136 | static SENSOR_DEVICE_ATTR_2(temp3_smoothing, S_IRUGO | S_IWUSR, show_temp_st, |
| 1137 | set_temp_st, 0, 2); |
| 1138 | static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0); |
| 1139 | static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach, |
| 1140 | MIN, 0); |
| 1141 | static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0); |
| 1142 | static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1); |
| 1143 | static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach, |
| 1144 | MIN, 1); |
| 1145 | static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1); |
| 1146 | static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2); |
| 1147 | static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach, |
| 1148 | MIN, 2); |
| 1149 | static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2); |
| 1150 | static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3); |
| 1151 | static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach, |
| 1152 | MIN, 3); |
| 1153 | static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3); |
| 1154 | static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, |
| 1155 | 0); |
| 1156 | static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq, |
| 1157 | set_pwmfreq, INPUT, 0); |
| 1158 | static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl, |
| 1159 | set_pwmctrl, INPUT, 0); |
| 1160 | static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR, |
| 1161 | show_pwmchan, set_pwmchan, INPUT, 0); |
| 1162 | static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 1163 | set_pwm, MIN, 0); |
| 1164 | static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 1165 | set_pwm, MAX, 0); |
| 1166 | static SENSOR_DEVICE_ATTR_2(pwm1_stall_disable, S_IRUGO | S_IWUSR, |
| 1167 | show_stall_disable, set_stall_disable, 0, 0); |
| 1168 | static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, |
| 1169 | 1); |
| 1170 | static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq, |
| 1171 | set_pwmfreq, INPUT, 1); |
| 1172 | static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl, |
| 1173 | set_pwmctrl, INPUT, 1); |
| 1174 | static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR, |
| 1175 | show_pwmchan, set_pwmchan, INPUT, 1); |
| 1176 | static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 1177 | set_pwm, MIN, 1); |
| 1178 | static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 1179 | set_pwm, MAX, 1); |
| 1180 | static SENSOR_DEVICE_ATTR_2(pwm2_stall_disable, S_IRUGO | S_IWUSR, |
| 1181 | show_stall_disable, set_stall_disable, 0, 1); |
| 1182 | static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, |
| 1183 | 2); |
| 1184 | static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq, |
| 1185 | set_pwmfreq, INPUT, 2); |
| 1186 | static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl, |
| 1187 | set_pwmctrl, INPUT, 2); |
| 1188 | static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR, |
| 1189 | show_pwmchan, set_pwmchan, INPUT, 2); |
| 1190 | static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 1191 | set_pwm, MIN, 2); |
| 1192 | static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 1193 | set_pwm, MAX, 2); |
| 1194 | static SENSOR_DEVICE_ATTR_2(pwm3_stall_disable, S_IRUGO | S_IWUSR, |
| 1195 | show_stall_disable, set_stall_disable, 0, 2); |
| 1196 | |
| 1197 | /* Non-standard name, might need revisiting */ |
| 1198 | static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit); |
| 1199 | |
| 1200 | static DEVICE_ATTR_RW(vrm); |
| 1201 | static DEVICE_ATTR_RO(cpu0_vid); |
| 1202 | |
| 1203 | static struct attribute *adt7475_attrs[] = { |
| 1204 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 1205 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 1206 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 1207 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
| 1208 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 1209 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 1210 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 1211 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
| 1212 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 1213 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
| 1214 | &sensor_dev_attr_temp1_fault.dev_attr.attr, |
| 1215 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 1216 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 1217 | &sensor_dev_attr_temp1_offset.dev_attr.attr, |
| 1218 | &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr, |
| 1219 | &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr, |
| 1220 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 1221 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, |
| 1222 | &sensor_dev_attr_temp1_smoothing.dev_attr.attr, |
| 1223 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 1224 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
| 1225 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 1226 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 1227 | &sensor_dev_attr_temp2_offset.dev_attr.attr, |
| 1228 | &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr, |
| 1229 | &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr, |
| 1230 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 1231 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, |
| 1232 | &sensor_dev_attr_temp2_smoothing.dev_attr.attr, |
| 1233 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 1234 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
| 1235 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
| 1236 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 1237 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 1238 | &sensor_dev_attr_temp3_offset.dev_attr.attr, |
| 1239 | &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr, |
| 1240 | &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr, |
| 1241 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
| 1242 | &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, |
| 1243 | &sensor_dev_attr_temp3_smoothing.dev_attr.attr, |
| 1244 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 1245 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 1246 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
| 1247 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 1248 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 1249 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
| 1250 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 1251 | &sensor_dev_attr_fan3_min.dev_attr.attr, |
| 1252 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
| 1253 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 1254 | &sensor_dev_attr_pwm1_freq.dev_attr.attr, |
| 1255 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
| 1256 | &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr, |
| 1257 | &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, |
| 1258 | &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, |
| 1259 | &sensor_dev_attr_pwm1_stall_disable.dev_attr.attr, |
| 1260 | &sensor_dev_attr_pwm3.dev_attr.attr, |
| 1261 | &sensor_dev_attr_pwm3_freq.dev_attr.attr, |
| 1262 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, |
| 1263 | &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr, |
| 1264 | &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, |
| 1265 | &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, |
| 1266 | &sensor_dev_attr_pwm3_stall_disable.dev_attr.attr, |
| 1267 | &dev_attr_pwm_use_point2_pwm_at_crit.attr, |
| 1268 | NULL, |
| 1269 | }; |
| 1270 | |
| 1271 | static struct attribute *fan4_attrs[] = { |
| 1272 | &sensor_dev_attr_fan4_input.dev_attr.attr, |
| 1273 | &sensor_dev_attr_fan4_min.dev_attr.attr, |
| 1274 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, |
| 1275 | NULL |
| 1276 | }; |
| 1277 | |
| 1278 | static struct attribute *pwm2_attrs[] = { |
| 1279 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 1280 | &sensor_dev_attr_pwm2_freq.dev_attr.attr, |
| 1281 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, |
| 1282 | &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr, |
| 1283 | &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, |
| 1284 | &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, |
| 1285 | &sensor_dev_attr_pwm2_stall_disable.dev_attr.attr, |
| 1286 | NULL |
| 1287 | }; |
| 1288 | |
| 1289 | static struct attribute *in0_attrs[] = { |
| 1290 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 1291 | &sensor_dev_attr_in0_max.dev_attr.attr, |
| 1292 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 1293 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
| 1294 | NULL |
| 1295 | }; |
| 1296 | |
| 1297 | static struct attribute *in3_attrs[] = { |
| 1298 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 1299 | &sensor_dev_attr_in3_max.dev_attr.attr, |
| 1300 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 1301 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
| 1302 | NULL |
| 1303 | }; |
| 1304 | |
| 1305 | static struct attribute *in4_attrs[] = { |
| 1306 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 1307 | &sensor_dev_attr_in4_max.dev_attr.attr, |
| 1308 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 1309 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
| 1310 | NULL |
| 1311 | }; |
| 1312 | |
| 1313 | static struct attribute *in5_attrs[] = { |
| 1314 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 1315 | &sensor_dev_attr_in5_max.dev_attr.attr, |
| 1316 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 1317 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
| 1318 | NULL |
| 1319 | }; |
| 1320 | |
| 1321 | static struct attribute *vid_attrs[] = { |
| 1322 | &dev_attr_cpu0_vid.attr, |
| 1323 | &dev_attr_vrm.attr, |
| 1324 | NULL |
| 1325 | }; |
| 1326 | |
| 1327 | static const struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs }; |
| 1328 | static const struct attribute_group fan4_attr_group = { .attrs = fan4_attrs }; |
| 1329 | static const struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs }; |
| 1330 | static const struct attribute_group in0_attr_group = { .attrs = in0_attrs }; |
| 1331 | static const struct attribute_group in3_attr_group = { .attrs = in3_attrs }; |
| 1332 | static const struct attribute_group in4_attr_group = { .attrs = in4_attrs }; |
| 1333 | static const struct attribute_group in5_attr_group = { .attrs = in5_attrs }; |
| 1334 | static const struct attribute_group vid_attr_group = { .attrs = vid_attrs }; |
| 1335 | |
| 1336 | static int adt7475_detect(struct i2c_client *client, |
| 1337 | struct i2c_board_info *info) |
| 1338 | { |
| 1339 | struct i2c_adapter *adapter = client->adapter; |
| 1340 | int vendid, devid, devid2; |
| 1341 | const char *name; |
| 1342 | |
| 1343 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 1344 | return -ENODEV; |
| 1345 | |
| 1346 | vendid = adt7475_read(REG_VENDID); |
| 1347 | devid2 = adt7475_read(REG_DEVID2); |
| 1348 | if (vendid != 0x41 || /* Analog Devices */ |
| 1349 | (devid2 & 0xf8) != 0x68) |
| 1350 | return -ENODEV; |
| 1351 | |
| 1352 | devid = adt7475_read(REG_DEVID); |
| 1353 | if (devid == 0x73) |
| 1354 | name = "adt7473"; |
| 1355 | else if (devid == 0x75 && client->addr == 0x2e) |
| 1356 | name = "adt7475"; |
| 1357 | else if (devid == 0x76) |
| 1358 | name = "adt7476"; |
| 1359 | else if ((devid2 & 0xfc) == 0x6c) |
| 1360 | name = "adt7490"; |
| 1361 | else { |
| 1362 | dev_dbg(&adapter->dev, |
| 1363 | "Couldn't detect an ADT7473/75/76/90 part at " |
| 1364 | "0x%02x\n", (unsigned int)client->addr); |
| 1365 | return -ENODEV; |
| 1366 | } |
| 1367 | |
| 1368 | strlcpy(info->type, name, I2C_NAME_SIZE); |
| 1369 | |
| 1370 | return 0; |
| 1371 | } |
| 1372 | |
| 1373 | static void adt7475_remove_files(struct i2c_client *client, |
| 1374 | struct adt7475_data *data) |
| 1375 | { |
| 1376 | sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group); |
| 1377 | if (data->has_fan4) |
| 1378 | sysfs_remove_group(&client->dev.kobj, &fan4_attr_group); |
| 1379 | if (data->has_pwm2) |
| 1380 | sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group); |
| 1381 | if (data->has_voltage & (1 << 0)) |
| 1382 | sysfs_remove_group(&client->dev.kobj, &in0_attr_group); |
| 1383 | if (data->has_voltage & (1 << 3)) |
| 1384 | sysfs_remove_group(&client->dev.kobj, &in3_attr_group); |
| 1385 | if (data->has_voltage & (1 << 4)) |
| 1386 | sysfs_remove_group(&client->dev.kobj, &in4_attr_group); |
| 1387 | if (data->has_voltage & (1 << 5)) |
| 1388 | sysfs_remove_group(&client->dev.kobj, &in5_attr_group); |
| 1389 | if (data->has_vid) |
| 1390 | sysfs_remove_group(&client->dev.kobj, &vid_attr_group); |
| 1391 | } |
| 1392 | |
| 1393 | static int adt7475_probe(struct i2c_client *client, |
| 1394 | const struct i2c_device_id *id) |
| 1395 | { |
| 1396 | enum chips chip; |
| 1397 | static const char * const names[] = { |
| 1398 | [adt7473] = "ADT7473", |
| 1399 | [adt7475] = "ADT7475", |
| 1400 | [adt7476] = "ADT7476", |
| 1401 | [adt7490] = "ADT7490", |
| 1402 | }; |
| 1403 | |
| 1404 | struct adt7475_data *data; |
| 1405 | int i, ret = 0, revision; |
| 1406 | u8 config2, config3; |
| 1407 | |
| 1408 | data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); |
| 1409 | if (data == NULL) |
| 1410 | return -ENOMEM; |
| 1411 | |
| 1412 | mutex_init(&data->lock); |
| 1413 | i2c_set_clientdata(client, data); |
| 1414 | |
| 1415 | if (client->dev.of_node) |
| 1416 | chip = (enum chips)of_device_get_match_data(&client->dev); |
| 1417 | else |
| 1418 | chip = id->driver_data; |
| 1419 | |
| 1420 | /* Initialize device-specific values */ |
| 1421 | switch (chip) { |
| 1422 | case adt7476: |
| 1423 | data->has_voltage = 0x0e; /* in1 to in3 */ |
| 1424 | revision = adt7475_read(REG_DEVID2) & 0x07; |
| 1425 | break; |
| 1426 | case adt7490: |
| 1427 | data->has_voltage = 0x3e; /* in1 to in5 */ |
| 1428 | revision = adt7475_read(REG_DEVID2) & 0x03; |
| 1429 | if (revision == 0x03) |
| 1430 | revision += adt7475_read(REG_DEVREV2); |
| 1431 | break; |
| 1432 | default: |
| 1433 | data->has_voltage = 0x06; /* in1, in2 */ |
| 1434 | revision = adt7475_read(REG_DEVID2) & 0x07; |
| 1435 | } |
| 1436 | |
| 1437 | config3 = adt7475_read(REG_CONFIG3); |
| 1438 | /* Pin PWM2 may alternatively be used for ALERT output */ |
| 1439 | if (!(config3 & CONFIG3_SMBALERT)) |
| 1440 | data->has_pwm2 = 1; |
| 1441 | /* Meaning of this bit is inverted for the ADT7473-1 */ |
| 1442 | if (id->driver_data == adt7473 && revision >= 1) |
| 1443 | data->has_pwm2 = !data->has_pwm2; |
| 1444 | |
| 1445 | data->config4 = adt7475_read(REG_CONFIG4); |
| 1446 | /* Pin TACH4 may alternatively be used for THERM */ |
| 1447 | if ((data->config4 & CONFIG4_PINFUNC) == 0x0) |
| 1448 | data->has_fan4 = 1; |
| 1449 | |
| 1450 | /* |
| 1451 | * THERM configuration is more complex on the ADT7476 and ADT7490, |
| 1452 | * because 2 different pins (TACH4 and +2.5 Vin) can be used for |
| 1453 | * this function |
| 1454 | */ |
| 1455 | if (id->driver_data == adt7490) { |
| 1456 | if ((data->config4 & CONFIG4_PINFUNC) == 0x1 && |
| 1457 | !(config3 & CONFIG3_THERM)) |
| 1458 | data->has_fan4 = 1; |
| 1459 | } |
| 1460 | if (id->driver_data == adt7476 || id->driver_data == adt7490) { |
| 1461 | if (!(config3 & CONFIG3_THERM) || |
| 1462 | (data->config4 & CONFIG4_PINFUNC) == 0x1) |
| 1463 | data->has_voltage |= (1 << 0); /* in0 */ |
| 1464 | } |
| 1465 | |
| 1466 | /* |
| 1467 | * On the ADT7476, the +12V input pin may instead be used as VID5, |
| 1468 | * and VID pins may alternatively be used as GPIO |
| 1469 | */ |
| 1470 | if (id->driver_data == adt7476) { |
| 1471 | u8 vid = adt7475_read(REG_VID); |
| 1472 | if (!(vid & VID_VIDSEL)) |
| 1473 | data->has_voltage |= (1 << 4); /* in4 */ |
| 1474 | |
| 1475 | data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO); |
| 1476 | } |
| 1477 | |
| 1478 | /* Voltage attenuators can be bypassed, globally or individually */ |
| 1479 | config2 = adt7475_read(REG_CONFIG2); |
| 1480 | if (config2 & CONFIG2_ATTN) { |
| 1481 | data->bypass_attn = (0x3 << 3) | 0x3; |
| 1482 | } else { |
| 1483 | data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) | |
| 1484 | ((data->config4 & CONFIG4_ATTN_IN43) >> 3); |
| 1485 | } |
| 1486 | data->bypass_attn &= data->has_voltage; |
| 1487 | |
| 1488 | /* |
| 1489 | * Call adt7475_read_pwm for all pwm's as this will reprogram any |
| 1490 | * pwm's which are disabled to manual mode with 0% duty cycle |
| 1491 | */ |
| 1492 | for (i = 0; i < ADT7475_PWM_COUNT; i++) |
| 1493 | adt7475_read_pwm(client, i); |
| 1494 | |
| 1495 | /* Start monitoring */ |
| 1496 | switch (chip) { |
| 1497 | case adt7475: |
| 1498 | case adt7476: |
| 1499 | i2c_smbus_write_byte_data(client, REG_CONFIG1, |
| 1500 | adt7475_read(REG_CONFIG1) | 0x01); |
| 1501 | break; |
| 1502 | default: |
| 1503 | break; |
| 1504 | } |
| 1505 | |
| 1506 | ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group); |
| 1507 | if (ret) |
| 1508 | return ret; |
| 1509 | |
| 1510 | /* Features that can be disabled individually */ |
| 1511 | if (data->has_fan4) { |
| 1512 | ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group); |
| 1513 | if (ret) |
| 1514 | goto eremove; |
| 1515 | } |
| 1516 | if (data->has_pwm2) { |
| 1517 | ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group); |
| 1518 | if (ret) |
| 1519 | goto eremove; |
| 1520 | } |
| 1521 | if (data->has_voltage & (1 << 0)) { |
| 1522 | ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group); |
| 1523 | if (ret) |
| 1524 | goto eremove; |
| 1525 | } |
| 1526 | if (data->has_voltage & (1 << 3)) { |
| 1527 | ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group); |
| 1528 | if (ret) |
| 1529 | goto eremove; |
| 1530 | } |
| 1531 | if (data->has_voltage & (1 << 4)) { |
| 1532 | ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group); |
| 1533 | if (ret) |
| 1534 | goto eremove; |
| 1535 | } |
| 1536 | if (data->has_voltage & (1 << 5)) { |
| 1537 | ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group); |
| 1538 | if (ret) |
| 1539 | goto eremove; |
| 1540 | } |
| 1541 | if (data->has_vid) { |
| 1542 | data->vrm = vid_which_vrm(); |
| 1543 | ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group); |
| 1544 | if (ret) |
| 1545 | goto eremove; |
| 1546 | } |
| 1547 | |
| 1548 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 1549 | if (IS_ERR(data->hwmon_dev)) { |
| 1550 | ret = PTR_ERR(data->hwmon_dev); |
| 1551 | goto eremove; |
| 1552 | } |
| 1553 | |
| 1554 | dev_info(&client->dev, "%s device, revision %d\n", |
| 1555 | names[id->driver_data], revision); |
| 1556 | if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2) |
| 1557 | dev_info(&client->dev, "Optional features:%s%s%s%s%s\n", |
| 1558 | (data->has_voltage & (1 << 0)) ? " in0" : "", |
| 1559 | (data->has_voltage & (1 << 4)) ? " in4" : "", |
| 1560 | data->has_fan4 ? " fan4" : "", |
| 1561 | data->has_pwm2 ? " pwm2" : "", |
| 1562 | data->has_vid ? " vid" : ""); |
| 1563 | if (data->bypass_attn) |
| 1564 | dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n", |
| 1565 | (data->bypass_attn & (1 << 0)) ? " in0" : "", |
| 1566 | (data->bypass_attn & (1 << 1)) ? " in1" : "", |
| 1567 | (data->bypass_attn & (1 << 3)) ? " in3" : "", |
| 1568 | (data->bypass_attn & (1 << 4)) ? " in4" : ""); |
| 1569 | |
| 1570 | return 0; |
| 1571 | |
| 1572 | eremove: |
| 1573 | adt7475_remove_files(client, data); |
| 1574 | return ret; |
| 1575 | } |
| 1576 | |
| 1577 | static int adt7475_remove(struct i2c_client *client) |
| 1578 | { |
| 1579 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 1580 | |
| 1581 | hwmon_device_unregister(data->hwmon_dev); |
| 1582 | adt7475_remove_files(client, data); |
| 1583 | |
| 1584 | return 0; |
| 1585 | } |
| 1586 | |
| 1587 | static struct i2c_driver adt7475_driver = { |
| 1588 | .class = I2C_CLASS_HWMON, |
| 1589 | .driver = { |
| 1590 | .name = "adt7475", |
| 1591 | .of_match_table = of_match_ptr(adt7475_of_match), |
| 1592 | }, |
| 1593 | .probe = adt7475_probe, |
| 1594 | .remove = adt7475_remove, |
| 1595 | .id_table = adt7475_id, |
| 1596 | .detect = adt7475_detect, |
| 1597 | .address_list = normal_i2c, |
| 1598 | }; |
| 1599 | |
| 1600 | static void adt7475_read_hystersis(struct i2c_client *client) |
| 1601 | { |
| 1602 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 1603 | |
| 1604 | data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS); |
| 1605 | data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0]; |
| 1606 | data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS); |
| 1607 | } |
| 1608 | |
| 1609 | static void adt7475_read_pwm(struct i2c_client *client, int index) |
| 1610 | { |
| 1611 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 1612 | unsigned int v; |
| 1613 | |
| 1614 | data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index)); |
| 1615 | |
| 1616 | /* |
| 1617 | * Figure out the internal value for pwmctrl and pwmchan |
| 1618 | * based on the current settings |
| 1619 | */ |
| 1620 | v = (data->pwm[CONTROL][index] >> 5) & 7; |
| 1621 | |
| 1622 | if (v == 3) |
| 1623 | data->pwmctl[index] = 0; |
| 1624 | else if (v == 7) |
| 1625 | data->pwmctl[index] = 1; |
| 1626 | else if (v == 4) { |
| 1627 | /* |
| 1628 | * The fan is disabled - we don't want to |
| 1629 | * support that, so change to manual mode and |
| 1630 | * set the duty cycle to 0 instead |
| 1631 | */ |
| 1632 | data->pwm[INPUT][index] = 0; |
| 1633 | data->pwm[CONTROL][index] &= ~0xE0; |
| 1634 | data->pwm[CONTROL][index] |= (7 << 5); |
| 1635 | |
| 1636 | i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), |
| 1637 | data->pwm[INPUT][index]); |
| 1638 | |
| 1639 | i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), |
| 1640 | data->pwm[CONTROL][index]); |
| 1641 | |
| 1642 | data->pwmctl[index] = 1; |
| 1643 | } else { |
| 1644 | data->pwmctl[index] = 2; |
| 1645 | |
| 1646 | switch (v) { |
| 1647 | case 0: |
| 1648 | data->pwmchan[index] = 1; |
| 1649 | break; |
| 1650 | case 1: |
| 1651 | data->pwmchan[index] = 2; |
| 1652 | break; |
| 1653 | case 2: |
| 1654 | data->pwmchan[index] = 4; |
| 1655 | break; |
| 1656 | case 5: |
| 1657 | data->pwmchan[index] = 6; |
| 1658 | break; |
| 1659 | case 6: |
| 1660 | data->pwmchan[index] = 7; |
| 1661 | break; |
| 1662 | } |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | static struct adt7475_data *adt7475_update_device(struct device *dev) |
| 1667 | { |
| 1668 | struct i2c_client *client = to_i2c_client(dev); |
| 1669 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 1670 | u16 ext; |
| 1671 | int i; |
| 1672 | |
| 1673 | mutex_lock(&data->lock); |
| 1674 | |
| 1675 | /* Measurement values update every 2 seconds */ |
| 1676 | if (time_after(jiffies, data->measure_updated + HZ * 2) || |
| 1677 | !data->valid) { |
| 1678 | data->alarms = adt7475_read(REG_STATUS2) << 8; |
| 1679 | data->alarms |= adt7475_read(REG_STATUS1); |
| 1680 | |
| 1681 | ext = (adt7475_read(REG_EXTEND2) << 8) | |
| 1682 | adt7475_read(REG_EXTEND1); |
| 1683 | for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) { |
| 1684 | if (!(data->has_voltage & (1 << i))) |
| 1685 | continue; |
| 1686 | data->voltage[INPUT][i] = |
| 1687 | (adt7475_read(VOLTAGE_REG(i)) << 2) | |
| 1688 | ((ext >> (i * 2)) & 3); |
| 1689 | } |
| 1690 | |
| 1691 | for (i = 0; i < ADT7475_TEMP_COUNT; i++) |
| 1692 | data->temp[INPUT][i] = |
| 1693 | (adt7475_read(TEMP_REG(i)) << 2) | |
| 1694 | ((ext >> ((i + 5) * 2)) & 3); |
| 1695 | |
| 1696 | if (data->has_voltage & (1 << 5)) { |
| 1697 | data->alarms |= adt7475_read(REG_STATUS4) << 24; |
| 1698 | ext = adt7475_read(REG_EXTEND3); |
| 1699 | data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 | |
| 1700 | ((ext >> 4) & 3); |
| 1701 | } |
| 1702 | |
| 1703 | for (i = 0; i < ADT7475_TACH_COUNT; i++) { |
| 1704 | if (i == 3 && !data->has_fan4) |
| 1705 | continue; |
| 1706 | data->tach[INPUT][i] = |
| 1707 | adt7475_read_word(client, TACH_REG(i)); |
| 1708 | } |
| 1709 | |
| 1710 | /* Updated by hw when in auto mode */ |
| 1711 | for (i = 0; i < ADT7475_PWM_COUNT; i++) { |
| 1712 | if (i == 1 && !data->has_pwm2) |
| 1713 | continue; |
| 1714 | data->pwm[INPUT][i] = adt7475_read(PWM_REG(i)); |
| 1715 | } |
| 1716 | |
| 1717 | if (data->has_vid) |
| 1718 | data->vid = adt7475_read(REG_VID) & 0x3f; |
| 1719 | |
| 1720 | data->measure_updated = jiffies; |
| 1721 | } |
| 1722 | |
| 1723 | /* Limits and settings, should never change update every 60 seconds */ |
| 1724 | if (time_after(jiffies, data->limits_updated + HZ * 60) || |
| 1725 | !data->valid) { |
| 1726 | data->config4 = adt7475_read(REG_CONFIG4); |
| 1727 | data->config5 = adt7475_read(REG_CONFIG5); |
| 1728 | |
| 1729 | for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) { |
| 1730 | if (!(data->has_voltage & (1 << i))) |
| 1731 | continue; |
| 1732 | /* Adjust values so they match the input precision */ |
| 1733 | data->voltage[MIN][i] = |
| 1734 | adt7475_read(VOLTAGE_MIN_REG(i)) << 2; |
| 1735 | data->voltage[MAX][i] = |
| 1736 | adt7475_read(VOLTAGE_MAX_REG(i)) << 2; |
| 1737 | } |
| 1738 | |
| 1739 | if (data->has_voltage & (1 << 5)) { |
| 1740 | data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2; |
| 1741 | data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2; |
| 1742 | } |
| 1743 | |
| 1744 | for (i = 0; i < ADT7475_TEMP_COUNT; i++) { |
| 1745 | /* Adjust values so they match the input precision */ |
| 1746 | data->temp[MIN][i] = |
| 1747 | adt7475_read(TEMP_MIN_REG(i)) << 2; |
| 1748 | data->temp[MAX][i] = |
| 1749 | adt7475_read(TEMP_MAX_REG(i)) << 2; |
| 1750 | data->temp[AUTOMIN][i] = |
| 1751 | adt7475_read(TEMP_TMIN_REG(i)) << 2; |
| 1752 | data->temp[THERM][i] = |
| 1753 | adt7475_read(TEMP_THERM_REG(i)) << 2; |
| 1754 | data->temp[OFFSET][i] = |
| 1755 | adt7475_read(TEMP_OFFSET_REG(i)); |
| 1756 | } |
| 1757 | adt7475_read_hystersis(client); |
| 1758 | |
| 1759 | for (i = 0; i < ADT7475_TACH_COUNT; i++) { |
| 1760 | if (i == 3 && !data->has_fan4) |
| 1761 | continue; |
| 1762 | data->tach[MIN][i] = |
| 1763 | adt7475_read_word(client, TACH_MIN_REG(i)); |
| 1764 | } |
| 1765 | |
| 1766 | for (i = 0; i < ADT7475_PWM_COUNT; i++) { |
| 1767 | if (i == 1 && !data->has_pwm2) |
| 1768 | continue; |
| 1769 | data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i)); |
| 1770 | data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i)); |
| 1771 | /* Set the channel and control information */ |
| 1772 | adt7475_read_pwm(client, i); |
| 1773 | } |
| 1774 | |
| 1775 | data->range[0] = adt7475_read(TEMP_TRANGE_REG(0)); |
| 1776 | data->range[1] = adt7475_read(TEMP_TRANGE_REG(1)); |
| 1777 | data->range[2] = adt7475_read(TEMP_TRANGE_REG(2)); |
| 1778 | |
| 1779 | data->limits_updated = jiffies; |
| 1780 | data->valid = 1; |
| 1781 | } |
| 1782 | |
| 1783 | mutex_unlock(&data->lock); |
| 1784 | |
| 1785 | return data; |
| 1786 | } |
| 1787 | |
| 1788 | module_i2c_driver(adt7475_driver); |
| 1789 | |
| 1790 | MODULE_AUTHOR("Advanced Micro Devices, Inc"); |
| 1791 | MODULE_DESCRIPTION("adt7475 driver"); |
| 1792 | MODULE_LICENSE("GPL"); |