1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * adm1025.c 4 * 5 * Copyright (C) 2000 Chen-Yuan Wu <gwu@esoft.com> 6 * Copyright (C) 2003-2009 Jean Delvare <jdelvare@suse.de> 7 * 8 * The ADM1025 is a sensor chip made by Analog Devices. It reports up to 6 9 * voltages (including its own power source) and up to two temperatures 10 * (its own plus up to one external one). Voltages are scaled internally 11 * (which is not the common way) with ratios such that the nominal value 12 * of each voltage correspond to a register value of 192 (which means a 13 * resolution of about 0.5% of the nominal value). Temperature values are 14 * reported with a 1 deg resolution and a 3 deg accuracy. Complete 15 * datasheet can be obtained from Analog's website at: 16 * https://www.onsemi.com/PowerSolutions/product.do?id=ADM1025 17 * 18 * This driver also supports the ADM1025A, which differs from the ADM1025 19 * only in that it has "open-drain VID inputs while the ADM1025 has 20 * on-chip 100k pull-ups on the VID inputs". It doesn't make any 21 * difference for us. 22 * 23 * This driver also supports the NE1619, a sensor chip made by Philips. 24 * That chip is similar to the ADM1025A, with a few differences. The only 25 * difference that matters to us is that the NE1619 has only two possible 26 * addresses while the ADM1025A has a third one. Complete datasheet can be 27 * obtained from Philips's website at: 28 * http://www.semiconductors.philips.com/pip/NE1619DS.html 29 * 30 * Since the ADM1025 was the first chipset supported by this driver, most 31 * comments will refer to this chipset, but are actually general and 32 * concern all supported chipsets, unless mentioned otherwise. 33 */ 34 35 #include <linux/module.h> 36 #include <linux/init.h> 37 #include <linux/slab.h> 38 #include <linux/jiffies.h> 39 #include <linux/i2c.h> 40 #include <linux/hwmon.h> 41 #include <linux/hwmon-sysfs.h> 42 #include <linux/hwmon-vid.h> 43 #include <linux/err.h> 44 #include <linux/mutex.h> 45 46 /* 47 * Addresses to scan 48 * ADM1025 and ADM1025A have three possible addresses: 0x2c, 0x2d and 0x2e. 49 * NE1619 has two possible addresses: 0x2c and 0x2d. 50 */ 51 52 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; 53 54 enum chips { adm1025, ne1619 }; 55 56 /* 57 * The ADM1025 registers 58 */ 59 60 #define ADM1025_REG_MAN_ID 0x3E 61 #define ADM1025_REG_CHIP_ID 0x3F 62 #define ADM1025_REG_CONFIG 0x40 63 #define ADM1025_REG_STATUS1 0x41 64 #define ADM1025_REG_STATUS2 0x42 65 #define ADM1025_REG_IN(nr) (0x20 + (nr)) 66 #define ADM1025_REG_IN_MAX(nr) (0x2B + (nr) * 2) 67 #define ADM1025_REG_IN_MIN(nr) (0x2C + (nr) * 2) 68 #define ADM1025_REG_TEMP(nr) (0x26 + (nr)) 69 #define ADM1025_REG_TEMP_HIGH(nr) (0x37 + (nr) * 2) 70 #define ADM1025_REG_TEMP_LOW(nr) (0x38 + (nr) * 2) 71 #define ADM1025_REG_VID 0x47 72 #define ADM1025_REG_VID4 0x49 73 74 /* 75 * Conversions and various macros 76 * The ADM1025 uses signed 8-bit values for temperatures. 77 */ 78 79 static const int in_scale[6] = { 2500, 2250, 3300, 5000, 12000, 3300 }; 80 81 #define IN_FROM_REG(reg, scale) (((reg) * (scale) + 96) / 192) 82 #define IN_TO_REG(val, scale) ((val) <= 0 ? 0 : \ 83 (val) >= (scale) * 255 / 192 ? 255 : \ 84 ((val) * 192 + (scale) / 2) / (scale)) 85 86 #define TEMP_FROM_REG(reg) ((reg) * 1000) 87 #define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \ 88 (val) >= 126500 ? 127 : \ 89 (((val) < 0 ? (val) - 500 : \ 90 (val) + 500) / 1000)) 91 92 /* 93 * Client data (each client gets its own) 94 */ 95 96 struct adm1025_data { 97 struct i2c_client *client; 98 const struct attribute_group *groups[3]; 99 struct mutex update_lock; 100 char valid; /* zero until following fields are valid */ 101 unsigned long last_updated; /* in jiffies */ 102 103 u8 in[6]; /* register value */ 104 u8 in_max[6]; /* register value */ 105 u8 in_min[6]; /* register value */ 106 s8 temp[2]; /* register value */ 107 s8 temp_min[2]; /* register value */ 108 s8 temp_max[2]; /* register value */ 109 u16 alarms; /* register values, combined */ 110 u8 vid; /* register values, combined */ 111 u8 vrm; 112 }; 113 114 static struct adm1025_data *adm1025_update_device(struct device *dev) 115 { 116 struct adm1025_data *data = dev_get_drvdata(dev); 117 struct i2c_client *client = data->client; 118 119 mutex_lock(&data->update_lock); 120 121 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) { 122 int i; 123 124 dev_dbg(&client->dev, "Updating data.\n"); 125 for (i = 0; i < 6; i++) { 126 data->in[i] = i2c_smbus_read_byte_data(client, 127 ADM1025_REG_IN(i)); 128 data->in_min[i] = i2c_smbus_read_byte_data(client, 129 ADM1025_REG_IN_MIN(i)); 130 data->in_max[i] = i2c_smbus_read_byte_data(client, 131 ADM1025_REG_IN_MAX(i)); 132 } 133 for (i = 0; i < 2; i++) { 134 data->temp[i] = i2c_smbus_read_byte_data(client, 135 ADM1025_REG_TEMP(i)); 136 data->temp_min[i] = i2c_smbus_read_byte_data(client, 137 ADM1025_REG_TEMP_LOW(i)); 138 data->temp_max[i] = i2c_smbus_read_byte_data(client, 139 ADM1025_REG_TEMP_HIGH(i)); 140 } 141 data->alarms = i2c_smbus_read_byte_data(client, 142 ADM1025_REG_STATUS1) 143 | (i2c_smbus_read_byte_data(client, 144 ADM1025_REG_STATUS2) << 8); 145 data->vid = (i2c_smbus_read_byte_data(client, 146 ADM1025_REG_VID) & 0x0f) 147 | ((i2c_smbus_read_byte_data(client, 148 ADM1025_REG_VID4) & 0x01) << 4); 149 150 data->last_updated = jiffies; 151 data->valid = 1; 152 } 153 154 mutex_unlock(&data->update_lock); 155 156 return data; 157 } 158 159 /* 160 * Sysfs stuff 161 */ 162 163 static ssize_t 164 in_show(struct device *dev, struct device_attribute *attr, char *buf) 165 { 166 int index = to_sensor_dev_attr(attr)->index; 167 struct adm1025_data *data = adm1025_update_device(dev); 168 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[index], 169 in_scale[index])); 170 } 171 172 static ssize_t 173 in_min_show(struct device *dev, struct device_attribute *attr, char *buf) 174 { 175 int index = to_sensor_dev_attr(attr)->index; 176 struct adm1025_data *data = adm1025_update_device(dev); 177 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[index], 178 in_scale[index])); 179 } 180 181 static ssize_t 182 in_max_show(struct device *dev, struct device_attribute *attr, char *buf) 183 { 184 int index = to_sensor_dev_attr(attr)->index; 185 struct adm1025_data *data = adm1025_update_device(dev); 186 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[index], 187 in_scale[index])); 188 } 189 190 static ssize_t 191 temp_show(struct device *dev, struct device_attribute *attr, char *buf) 192 { 193 int index = to_sensor_dev_attr(attr)->index; 194 struct adm1025_data *data = adm1025_update_device(dev); 195 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[index])); 196 } 197 198 static ssize_t 199 temp_min_show(struct device *dev, struct device_attribute *attr, char *buf) 200 { 201 int index = to_sensor_dev_attr(attr)->index; 202 struct adm1025_data *data = adm1025_update_device(dev); 203 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[index])); 204 } 205 206 static ssize_t 207 temp_max_show(struct device *dev, struct device_attribute *attr, char *buf) 208 { 209 int index = to_sensor_dev_attr(attr)->index; 210 struct adm1025_data *data = adm1025_update_device(dev); 211 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[index])); 212 } 213 214 static ssize_t in_min_store(struct device *dev, struct device_attribute *attr, 215 const char *buf, size_t count) 216 { 217 int index = to_sensor_dev_attr(attr)->index; 218 struct adm1025_data *data = dev_get_drvdata(dev); 219 struct i2c_client *client = data->client; 220 long val; 221 int err; 222 223 err = kstrtol(buf, 10, &val); 224 if (err) 225 return err; 226 227 mutex_lock(&data->update_lock); 228 data->in_min[index] = IN_TO_REG(val, in_scale[index]); 229 i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MIN(index), 230 data->in_min[index]); 231 mutex_unlock(&data->update_lock); 232 return count; 233 } 234 235 static ssize_t in_max_store(struct device *dev, struct device_attribute *attr, 236 const char *buf, size_t count) 237 { 238 int index = to_sensor_dev_attr(attr)->index; 239 struct adm1025_data *data = dev_get_drvdata(dev); 240 struct i2c_client *client = data->client; 241 long val; 242 int err; 243 244 err = kstrtol(buf, 10, &val); 245 if (err) 246 return err; 247 248 mutex_lock(&data->update_lock); 249 data->in_max[index] = IN_TO_REG(val, in_scale[index]); 250 i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MAX(index), 251 data->in_max[index]); 252 mutex_unlock(&data->update_lock); 253 return count; 254 } 255 256 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0); 257 static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0); 258 static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0); 259 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1); 260 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1); 261 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1); 262 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2); 263 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2); 264 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2); 265 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3); 266 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3); 267 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3); 268 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4); 269 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4); 270 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4); 271 static SENSOR_DEVICE_ATTR_RO(in5_input, in, 5); 272 static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5); 273 static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5); 274 275 static ssize_t temp_min_store(struct device *dev, 276 struct device_attribute *attr, const char *buf, 277 size_t count) 278 { 279 int index = to_sensor_dev_attr(attr)->index; 280 struct adm1025_data *data = dev_get_drvdata(dev); 281 struct i2c_client *client = data->client; 282 long val; 283 int err; 284 285 err = kstrtol(buf, 10, &val); 286 if (err) 287 return err; 288 289 mutex_lock(&data->update_lock); 290 data->temp_min[index] = TEMP_TO_REG(val); 291 i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_LOW(index), 292 data->temp_min[index]); 293 mutex_unlock(&data->update_lock); 294 return count; 295 } 296 297 static ssize_t temp_max_store(struct device *dev, 298 struct device_attribute *attr, const char *buf, 299 size_t count) 300 { 301 int index = to_sensor_dev_attr(attr)->index; 302 struct adm1025_data *data = dev_get_drvdata(dev); 303 struct i2c_client *client = data->client; 304 long val; 305 int err; 306 307 err = kstrtol(buf, 10, &val); 308 if (err) 309 return err; 310 311 mutex_lock(&data->update_lock); 312 data->temp_max[index] = TEMP_TO_REG(val); 313 i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_HIGH(index), 314 data->temp_max[index]); 315 mutex_unlock(&data->update_lock); 316 return count; 317 } 318 319 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0); 320 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0); 321 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0); 322 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1); 323 static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1); 324 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1); 325 326 static ssize_t 327 alarms_show(struct device *dev, struct device_attribute *attr, char *buf) 328 { 329 struct adm1025_data *data = adm1025_update_device(dev); 330 return sprintf(buf, "%u\n", data->alarms); 331 } 332 static DEVICE_ATTR_RO(alarms); 333 334 static ssize_t 335 alarm_show(struct device *dev, struct device_attribute *attr, char *buf) 336 { 337 int bitnr = to_sensor_dev_attr(attr)->index; 338 struct adm1025_data *data = adm1025_update_device(dev); 339 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); 340 } 341 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0); 342 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1); 343 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2); 344 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3); 345 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8); 346 static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9); 347 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 5); 348 static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 4); 349 static SENSOR_DEVICE_ATTR_RO(temp1_fault, alarm, 14); 350 351 static ssize_t 352 cpu0_vid_show(struct device *dev, struct device_attribute *attr, char *buf) 353 { 354 struct adm1025_data *data = adm1025_update_device(dev); 355 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm)); 356 } 357 static DEVICE_ATTR_RO(cpu0_vid); 358 359 static ssize_t 360 vrm_show(struct device *dev, struct device_attribute *attr, char *buf) 361 { 362 struct adm1025_data *data = dev_get_drvdata(dev); 363 return sprintf(buf, "%u\n", data->vrm); 364 } 365 static ssize_t vrm_store(struct device *dev, struct device_attribute *attr, 366 const char *buf, size_t count) 367 { 368 struct adm1025_data *data = dev_get_drvdata(dev); 369 unsigned long val; 370 int err; 371 372 err = kstrtoul(buf, 10, &val); 373 if (err) 374 return err; 375 376 if (val > 255) 377 return -EINVAL; 378 379 data->vrm = val; 380 return count; 381 } 382 static DEVICE_ATTR_RW(vrm); 383 384 /* 385 * Real code 386 */ 387 388 static struct attribute *adm1025_attributes[] = { 389 &sensor_dev_attr_in0_input.dev_attr.attr, 390 &sensor_dev_attr_in1_input.dev_attr.attr, 391 &sensor_dev_attr_in2_input.dev_attr.attr, 392 &sensor_dev_attr_in3_input.dev_attr.attr, 393 &sensor_dev_attr_in5_input.dev_attr.attr, 394 &sensor_dev_attr_in0_min.dev_attr.attr, 395 &sensor_dev_attr_in1_min.dev_attr.attr, 396 &sensor_dev_attr_in2_min.dev_attr.attr, 397 &sensor_dev_attr_in3_min.dev_attr.attr, 398 &sensor_dev_attr_in5_min.dev_attr.attr, 399 &sensor_dev_attr_in0_max.dev_attr.attr, 400 &sensor_dev_attr_in1_max.dev_attr.attr, 401 &sensor_dev_attr_in2_max.dev_attr.attr, 402 &sensor_dev_attr_in3_max.dev_attr.attr, 403 &sensor_dev_attr_in5_max.dev_attr.attr, 404 &sensor_dev_attr_in0_alarm.dev_attr.attr, 405 &sensor_dev_attr_in1_alarm.dev_attr.attr, 406 &sensor_dev_attr_in2_alarm.dev_attr.attr, 407 &sensor_dev_attr_in3_alarm.dev_attr.attr, 408 &sensor_dev_attr_in5_alarm.dev_attr.attr, 409 &sensor_dev_attr_temp1_input.dev_attr.attr, 410 &sensor_dev_attr_temp2_input.dev_attr.attr, 411 &sensor_dev_attr_temp1_min.dev_attr.attr, 412 &sensor_dev_attr_temp2_min.dev_attr.attr, 413 &sensor_dev_attr_temp1_max.dev_attr.attr, 414 &sensor_dev_attr_temp2_max.dev_attr.attr, 415 &sensor_dev_attr_temp1_alarm.dev_attr.attr, 416 &sensor_dev_attr_temp2_alarm.dev_attr.attr, 417 &sensor_dev_attr_temp1_fault.dev_attr.attr, 418 &dev_attr_alarms.attr, 419 &dev_attr_cpu0_vid.attr, 420 &dev_attr_vrm.attr, 421 NULL 422 }; 423 424 static const struct attribute_group adm1025_group = { 425 .attrs = adm1025_attributes, 426 }; 427 428 static struct attribute *adm1025_attributes_in4[] = { 429 &sensor_dev_attr_in4_input.dev_attr.attr, 430 &sensor_dev_attr_in4_min.dev_attr.attr, 431 &sensor_dev_attr_in4_max.dev_attr.attr, 432 &sensor_dev_attr_in4_alarm.dev_attr.attr, 433 NULL 434 }; 435 436 static const struct attribute_group adm1025_group_in4 = { 437 .attrs = adm1025_attributes_in4, 438 }; 439 440 /* Return 0 if detection is successful, -ENODEV otherwise */ 441 static int adm1025_detect(struct i2c_client *client, 442 struct i2c_board_info *info) 443 { 444 struct i2c_adapter *adapter = client->adapter; 445 const char *name; 446 u8 man_id, chip_id; 447 448 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 449 return -ENODEV; 450 451 /* Check for unused bits */ 452 if ((i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG) & 0x80) 453 || (i2c_smbus_read_byte_data(client, ADM1025_REG_STATUS1) & 0xC0) 454 || (i2c_smbus_read_byte_data(client, ADM1025_REG_STATUS2) & 0xBC)) { 455 dev_dbg(&adapter->dev, "ADM1025 detection failed at 0x%02x\n", 456 client->addr); 457 return -ENODEV; 458 } 459 460 /* Identification */ 461 chip_id = i2c_smbus_read_byte_data(client, ADM1025_REG_CHIP_ID); 462 if ((chip_id & 0xF0) != 0x20) 463 return -ENODEV; 464 465 man_id = i2c_smbus_read_byte_data(client, ADM1025_REG_MAN_ID); 466 if (man_id == 0x41) 467 name = "adm1025"; 468 else if (man_id == 0xA1 && client->addr != 0x2E) 469 name = "ne1619"; 470 else 471 return -ENODEV; 472 473 strlcpy(info->type, name, I2C_NAME_SIZE); 474 475 return 0; 476 } 477 478 static void adm1025_init_client(struct i2c_client *client) 479 { 480 u8 reg; 481 struct adm1025_data *data = i2c_get_clientdata(client); 482 int i; 483 484 data->vrm = vid_which_vrm(); 485 486 /* 487 * Set high limits 488 * Usually we avoid setting limits on driver init, but it happens 489 * that the ADM1025 comes with stupid default limits (all registers 490 * set to 0). In case the chip has not gone through any limit 491 * setting yet, we better set the high limits to the max so that 492 * no alarm triggers. 493 */ 494 for (i = 0; i < 6; i++) { 495 reg = i2c_smbus_read_byte_data(client, 496 ADM1025_REG_IN_MAX(i)); 497 if (reg == 0) 498 i2c_smbus_write_byte_data(client, 499 ADM1025_REG_IN_MAX(i), 500 0xFF); 501 } 502 for (i = 0; i < 2; i++) { 503 reg = i2c_smbus_read_byte_data(client, 504 ADM1025_REG_TEMP_HIGH(i)); 505 if (reg == 0) 506 i2c_smbus_write_byte_data(client, 507 ADM1025_REG_TEMP_HIGH(i), 508 0x7F); 509 } 510 511 /* 512 * Start the conversions 513 */ 514 reg = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG); 515 if (!(reg & 0x01)) 516 i2c_smbus_write_byte_data(client, ADM1025_REG_CONFIG, 517 (reg&0x7E)|0x01); 518 } 519 520 static int adm1025_probe(struct i2c_client *client) 521 { 522 struct device *dev = &client->dev; 523 struct device *hwmon_dev; 524 struct adm1025_data *data; 525 u8 config; 526 527 data = devm_kzalloc(dev, sizeof(struct adm1025_data), GFP_KERNEL); 528 if (!data) 529 return -ENOMEM; 530 531 i2c_set_clientdata(client, data); 532 data->client = client; 533 mutex_init(&data->update_lock); 534 535 /* Initialize the ADM1025 chip */ 536 adm1025_init_client(client); 537 538 /* sysfs hooks */ 539 data->groups[0] = &adm1025_group; 540 /* Pin 11 is either in4 (+12V) or VID4 */ 541 config = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG); 542 if (!(config & 0x20)) 543 data->groups[1] = &adm1025_group_in4; 544 545 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 546 data, data->groups); 547 return PTR_ERR_OR_ZERO(hwmon_dev); 548 } 549 550 static const struct i2c_device_id adm1025_id[] = { 551 { "adm1025", adm1025 }, 552 { "ne1619", ne1619 }, 553 { } 554 }; 555 MODULE_DEVICE_TABLE(i2c, adm1025_id); 556 557 static struct i2c_driver adm1025_driver = { 558 .class = I2C_CLASS_HWMON, 559 .driver = { 560 .name = "adm1025", 561 }, 562 .probe_new = adm1025_probe, 563 .id_table = adm1025_id, 564 .detect = adm1025_detect, 565 .address_list = normal_i2c, 566 }; 567 568 module_i2c_driver(adm1025_driver); 569 570 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>"); 571 MODULE_DESCRIPTION("ADM1025 driver"); 572 MODULE_LICENSE("GPL"); 573