1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * lm75.c - Part of lm_sensors, Linux kernel modules for hardware 4 * monitoring 5 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> 6 */ 7 8 #include <linux/module.h> 9 #include <linux/init.h> 10 #include <linux/interrupt.h> 11 #include <linux/slab.h> 12 #include <linux/jiffies.h> 13 #include <linux/i2c.h> 14 #include <linux/i3c/device.h> 15 #include <linux/hwmon.h> 16 #include <linux/err.h> 17 #include <linux/of.h> 18 #include <linux/regmap.h> 19 #include <linux/util_macros.h> 20 #include <linux/regulator/consumer.h> 21 #include "lm75.h" 22 23 /* 24 * This driver handles the LM75 and compatible digital temperature sensors. 25 */ 26 27 enum lm75_type { /* keep sorted in alphabetical order */ 28 adt75, 29 as6200, 30 at30ts74, 31 ds1775, 32 ds75, 33 ds7505, 34 g751, 35 lm75, 36 lm75a, 37 lm75b, 38 max6625, 39 max6626, 40 max31725, 41 mcp980x, 42 p3t1750, 43 p3t1755, 44 pct2075, 45 stds75, 46 stlm75, 47 tcn75, 48 tmp100, 49 tmp101, 50 tmp105, 51 tmp112, 52 tmp175, 53 tmp275, 54 tmp75, 55 tmp75b, 56 tmp75c, 57 tmp1075, 58 }; 59 60 /** 61 * struct lm75_params - lm75 configuration parameters. 62 * @config_reg_16bits: Configure register size is 2 bytes. 63 * @set_mask: Bits to set in configuration register when configuring 64 * the chip. 65 * @clr_mask: Bits to clear in configuration register when configuring 66 * the chip. 67 * @default_resolution: Default number of bits to represent the temperature 68 * value. 69 * @resolution_limits: Limit register resolution. Optional. Should be set if 70 * the resolution of limit registers does not match the 71 * resolution of the temperature register. 72 * @resolutions: List of resolutions associated with sample times. 73 * Optional. Should be set if num_sample_times is larger 74 * than 1, and if the resolution changes with sample times. 75 * If set, number of entries must match num_sample_times. 76 * @default_sample_time:Sample time to be set by default. 77 * @num_sample_times: Number of possible sample times to be set. Optional. 78 * Should be set if the number of sample times is larger 79 * than one. 80 * @sample_times: All the possible sample times to be set. Mandatory if 81 * num_sample_times is larger than 1. If set, number of 82 * entries must match num_sample_times. 83 * @alarm: Alarm bit is supported. 84 */ 85 86 struct lm75_params { 87 bool config_reg_16bits; 88 u16 set_mask; 89 u16 clr_mask; 90 u8 default_resolution; 91 u8 resolution_limits; 92 const u8 *resolutions; 93 unsigned int default_sample_time; 94 u8 num_sample_times; 95 const unsigned int *sample_times; 96 bool alarm; 97 }; 98 99 /* Addresses scanned */ 100 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c, 101 0x4d, 0x4e, 0x4f, I2C_CLIENT_END }; 102 103 /* The LM75 registers */ 104 #define LM75_REG_TEMP 0x00 105 #define LM75_REG_CONF 0x01 106 #define LM75_REG_HYST 0x02 107 #define LM75_REG_MAX 0x03 108 #define PCT2075_REG_IDLE 0x04 109 110 struct lm75_data { 111 const char *label; 112 struct regmap *regmap; 113 u16 orig_conf; 114 u8 resolution; /* In bits, 9 to 16 */ 115 unsigned int sample_time; /* In ms */ 116 enum lm75_type kind; 117 const struct lm75_params *params; 118 u8 reg_buf[1]; 119 u8 val_buf[3]; 120 }; 121 122 /*-----------------------------------------------------------------------*/ 123 124 static const u8 lm75_sample_set_masks[] = { 0 << 5, 1 << 5, 2 << 5, 3 << 5 }; 125 126 #define LM75_SAMPLE_CLEAR_MASK (3 << 5) 127 128 /* The structure below stores the configuration values of the supported devices. 129 * In case of being supported multiple configurations, the default one must 130 * always be the first element of the array 131 */ 132 static const struct lm75_params device_params[] = { 133 [adt75] = { 134 .clr_mask = 1 << 5, /* not one-shot mode */ 135 .default_resolution = 12, 136 .default_sample_time = MSEC_PER_SEC / 10, 137 }, 138 [as6200] = { 139 .config_reg_16bits = true, 140 .set_mask = 0x94C0, /* 8 sample/s, 4 CF, positive polarity */ 141 .default_resolution = 12, 142 .default_sample_time = 125, 143 .num_sample_times = 4, 144 .sample_times = (unsigned int []){ 125, 250, 1000, 4000 }, 145 .alarm = true, 146 }, 147 [at30ts74] = { 148 .set_mask = 3 << 5, /* 12-bit mode*/ 149 .default_resolution = 12, 150 .default_sample_time = 200, 151 .num_sample_times = 4, 152 .sample_times = (unsigned int []){ 25, 50, 100, 200 }, 153 .resolutions = (u8 []) {9, 10, 11, 12 }, 154 }, 155 [ds1775] = { 156 .clr_mask = 3 << 5, 157 .set_mask = 2 << 5, /* 11-bit mode */ 158 .default_resolution = 11, 159 .default_sample_time = 500, 160 .num_sample_times = 4, 161 .sample_times = (unsigned int []){ 125, 250, 500, 1000 }, 162 .resolutions = (u8 []) {9, 10, 11, 12 }, 163 }, 164 [ds75] = { 165 .clr_mask = 3 << 5, 166 .set_mask = 2 << 5, /* 11-bit mode */ 167 .default_resolution = 11, 168 .default_sample_time = 600, 169 .num_sample_times = 4, 170 .sample_times = (unsigned int []){ 150, 300, 600, 1200 }, 171 .resolutions = (u8 []) {9, 10, 11, 12 }, 172 }, 173 [stds75] = { 174 .clr_mask = 3 << 5, 175 .set_mask = 2 << 5, /* 11-bit mode */ 176 .default_resolution = 11, 177 .default_sample_time = 600, 178 .num_sample_times = 4, 179 .sample_times = (unsigned int []){ 150, 300, 600, 1200 }, 180 .resolutions = (u8 []) {9, 10, 11, 12 }, 181 }, 182 [stlm75] = { 183 .default_resolution = 9, 184 .default_sample_time = MSEC_PER_SEC / 6, 185 }, 186 [ds7505] = { 187 .set_mask = 3 << 5, /* 12-bit mode*/ 188 .default_resolution = 12, 189 .default_sample_time = 200, 190 .num_sample_times = 4, 191 .sample_times = (unsigned int []){ 25, 50, 100, 200 }, 192 .resolutions = (u8 []) {9, 10, 11, 12 }, 193 }, 194 [g751] = { 195 .default_resolution = 9, 196 .default_sample_time = MSEC_PER_SEC / 10, 197 }, 198 [lm75] = { 199 .default_resolution = 9, 200 .default_sample_time = MSEC_PER_SEC / 10, 201 }, 202 [lm75a] = { 203 .default_resolution = 9, 204 .default_sample_time = MSEC_PER_SEC / 10, 205 }, 206 [lm75b] = { 207 .default_resolution = 11, 208 .default_sample_time = MSEC_PER_SEC / 10, 209 }, 210 [max6625] = { 211 .default_resolution = 9, 212 .default_sample_time = MSEC_PER_SEC / 7, 213 }, 214 [max6626] = { 215 .default_resolution = 12, 216 .default_sample_time = MSEC_PER_SEC / 7, 217 .resolution_limits = 9, 218 }, 219 [max31725] = { 220 .default_resolution = 16, 221 .default_sample_time = MSEC_PER_SEC / 20, 222 }, 223 [tcn75] = { 224 .default_resolution = 9, 225 .default_sample_time = MSEC_PER_SEC / 18, 226 }, 227 [p3t1750] = { 228 .clr_mask = 1 << 1 | 1 << 7, /* disable SMBAlert and one-shot */ 229 .default_resolution = 12, 230 .default_sample_time = 55, 231 .num_sample_times = 4, 232 .sample_times = (unsigned int []){ 28, 55, 110, 220 }, 233 }, 234 [p3t1755] = { 235 .clr_mask = 1 << 1 | 1 << 7, /* disable SMBAlert and one-shot */ 236 .default_resolution = 12, 237 .default_sample_time = 55, 238 .num_sample_times = 4, 239 .sample_times = (unsigned int []){ 28, 55, 110, 220 }, 240 }, 241 [pct2075] = { 242 .default_resolution = 11, 243 .default_sample_time = MSEC_PER_SEC / 10, 244 .num_sample_times = 31, 245 .sample_times = (unsigned int []){ 100, 200, 300, 400, 500, 600, 246 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 247 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 248 2800, 2900, 3000, 3100 }, 249 }, 250 [mcp980x] = { 251 .set_mask = 3 << 5, /* 12-bit mode */ 252 .clr_mask = 1 << 7, /* not one-shot mode */ 253 .default_resolution = 12, 254 .resolution_limits = 9, 255 .default_sample_time = 240, 256 .num_sample_times = 4, 257 .sample_times = (unsigned int []){ 30, 60, 120, 240 }, 258 .resolutions = (u8 []) {9, 10, 11, 12 }, 259 }, 260 [tmp100] = { 261 .set_mask = 3 << 5, /* 12-bit mode */ 262 .clr_mask = 1 << 7, /* not one-shot mode */ 263 .default_resolution = 12, 264 .default_sample_time = 320, 265 .num_sample_times = 4, 266 .sample_times = (unsigned int []){ 40, 80, 160, 320 }, 267 .resolutions = (u8 []) {9, 10, 11, 12 }, 268 }, 269 [tmp101] = { 270 .set_mask = 3 << 5, /* 12-bit mode */ 271 .clr_mask = 1 << 7, /* not one-shot mode */ 272 .default_resolution = 12, 273 .default_sample_time = 320, 274 .num_sample_times = 4, 275 .sample_times = (unsigned int []){ 40, 80, 160, 320 }, 276 .resolutions = (u8 []) {9, 10, 11, 12 }, 277 }, 278 [tmp105] = { 279 .set_mask = 3 << 5, /* 12-bit mode */ 280 .clr_mask = 1 << 7, /* not one-shot mode*/ 281 .default_resolution = 12, 282 .default_sample_time = 220, 283 .num_sample_times = 4, 284 .sample_times = (unsigned int []){ 28, 55, 110, 220 }, 285 .resolutions = (u8 []) {9, 10, 11, 12 }, 286 }, 287 [tmp112] = { 288 .config_reg_16bits = true, 289 .set_mask = 0x60C0, /* 12-bit mode, 8 samples / second */ 290 .clr_mask = 1 << 15, /* no one-shot mode*/ 291 .default_resolution = 12, 292 .default_sample_time = 125, 293 .num_sample_times = 4, 294 .sample_times = (unsigned int []){ 125, 250, 1000, 4000 }, 295 .alarm = true, 296 }, 297 [tmp175] = { 298 .set_mask = 3 << 5, /* 12-bit mode */ 299 .clr_mask = 1 << 7, /* not one-shot mode*/ 300 .default_resolution = 12, 301 .default_sample_time = 220, 302 .num_sample_times = 4, 303 .sample_times = (unsigned int []){ 28, 55, 110, 220 }, 304 .resolutions = (u8 []) {9, 10, 11, 12 }, 305 }, 306 [tmp275] = { 307 .set_mask = 3 << 5, /* 12-bit mode */ 308 .clr_mask = 1 << 7, /* not one-shot mode*/ 309 .default_resolution = 12, 310 .default_sample_time = 220, 311 .num_sample_times = 4, 312 .sample_times = (unsigned int []){ 28, 55, 110, 220 }, 313 .resolutions = (u8 []) {9, 10, 11, 12 }, 314 }, 315 [tmp75] = { 316 .set_mask = 3 << 5, /* 12-bit mode */ 317 .clr_mask = 1 << 7, /* not one-shot mode*/ 318 .default_resolution = 12, 319 .default_sample_time = 220, 320 .num_sample_times = 4, 321 .sample_times = (unsigned int []){ 28, 55, 110, 220 }, 322 .resolutions = (u8 []) {9, 10, 11, 12 }, 323 }, 324 [tmp75b] = { /* not one-shot mode, Conversion rate 37Hz */ 325 .clr_mask = 1 << 7 | 3 << 5, 326 .default_resolution = 12, 327 .default_sample_time = MSEC_PER_SEC / 37, 328 .sample_times = (unsigned int []){ MSEC_PER_SEC / 37, 329 MSEC_PER_SEC / 18, 330 MSEC_PER_SEC / 9, MSEC_PER_SEC / 4 }, 331 .num_sample_times = 4, 332 }, 333 [tmp75c] = { 334 .clr_mask = 1 << 5, /*not one-shot mode*/ 335 .default_resolution = 12, 336 .default_sample_time = MSEC_PER_SEC / 12, 337 }, 338 [tmp1075] = { /* not one-shot mode, 27.5 ms sample rate */ 339 .clr_mask = 1 << 5 | 1 << 6 | 1 << 7, 340 .default_resolution = 12, 341 .default_sample_time = 28, 342 .num_sample_times = 4, 343 .sample_times = (unsigned int []){ 28, 55, 110, 220 }, 344 } 345 }; 346 347 static inline long lm75_reg_to_mc(s16 temp, u8 resolution) 348 { 349 return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8); 350 } 351 352 static inline int lm75_write_config(struct lm75_data *data, u16 set_mask, 353 u16 clr_mask) 354 { 355 return regmap_update_bits(data->regmap, LM75_REG_CONF, 356 clr_mask | LM75_SHUTDOWN, set_mask); 357 } 358 359 static irqreturn_t lm75_alarm_handler(int irq, void *private) 360 { 361 struct device *hwmon_dev = private; 362 363 hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_alarm, 0); 364 return IRQ_HANDLED; 365 } 366 367 static int lm75_read_string(struct device *dev, enum hwmon_sensor_types type, 368 u32 attr, int channel, const char **str) 369 { 370 struct lm75_data *data = dev_get_drvdata(dev); 371 372 *str = data->label; 373 374 return 0; 375 } 376 377 static int lm75_read(struct device *dev, enum hwmon_sensor_types type, 378 u32 attr, int channel, long *val) 379 { 380 struct lm75_data *data = dev_get_drvdata(dev); 381 unsigned int regval; 382 int err, reg; 383 384 switch (type) { 385 case hwmon_chip: 386 switch (attr) { 387 case hwmon_chip_update_interval: 388 *val = data->sample_time; 389 break; 390 default: 391 return -EINVAL; 392 } 393 break; 394 case hwmon_temp: 395 switch (attr) { 396 case hwmon_temp_input: 397 reg = LM75_REG_TEMP; 398 break; 399 case hwmon_temp_max: 400 reg = LM75_REG_MAX; 401 break; 402 case hwmon_temp_max_hyst: 403 reg = LM75_REG_HYST; 404 break; 405 case hwmon_temp_alarm: 406 reg = LM75_REG_CONF; 407 break; 408 default: 409 return -EINVAL; 410 } 411 err = regmap_read(data->regmap, reg, ®val); 412 if (err < 0) 413 return err; 414 415 if (attr == hwmon_temp_alarm) { 416 switch (data->kind) { 417 case as6200: 418 case tmp112: 419 *val = (regval >> 13) & 0x1; 420 break; 421 default: 422 return -EINVAL; 423 } 424 } else { 425 *val = lm75_reg_to_mc(regval, data->resolution); 426 } 427 break; 428 default: 429 return -EINVAL; 430 } 431 return 0; 432 } 433 434 static int lm75_write_temp(struct device *dev, u32 attr, long temp) 435 { 436 struct lm75_data *data = dev_get_drvdata(dev); 437 u8 resolution; 438 int reg; 439 440 switch (attr) { 441 case hwmon_temp_max: 442 reg = LM75_REG_MAX; 443 break; 444 case hwmon_temp_max_hyst: 445 reg = LM75_REG_HYST; 446 break; 447 default: 448 return -EINVAL; 449 } 450 451 /* 452 * Resolution of limit registers is assumed to be the same as the 453 * temperature input register resolution unless given explicitly. 454 */ 455 if (data->params->resolution_limits) 456 resolution = data->params->resolution_limits; 457 else 458 resolution = data->resolution; 459 460 temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX); 461 temp = DIV_ROUND_CLOSEST(temp << (resolution - 8), 462 1000) << (16 - resolution); 463 464 return regmap_write(data->regmap, reg, (u16)temp); 465 } 466 467 static int lm75_update_interval(struct device *dev, long val) 468 { 469 struct lm75_data *data = dev_get_drvdata(dev); 470 u8 index; 471 s32 err; 472 473 index = find_closest(val, data->params->sample_times, 474 (int)data->params->num_sample_times); 475 476 switch (data->kind) { 477 default: 478 err = lm75_write_config(data, lm75_sample_set_masks[index], 479 LM75_SAMPLE_CLEAR_MASK); 480 if (err) 481 return err; 482 483 data->sample_time = data->params->sample_times[index]; 484 if (data->params->resolutions) 485 data->resolution = data->params->resolutions[index]; 486 break; 487 case tmp112: 488 case as6200: 489 err = regmap_update_bits(data->regmap, LM75_REG_CONF, 490 0xc000, (3 - index) << 14); 491 if (err < 0) 492 return err; 493 data->sample_time = data->params->sample_times[index]; 494 break; 495 case pct2075: 496 err = regmap_write(data->regmap, PCT2075_REG_IDLE, index + 1); 497 if (err) 498 return err; 499 data->sample_time = data->params->sample_times[index]; 500 break; 501 } 502 return 0; 503 } 504 505 static int lm75_write_chip(struct device *dev, u32 attr, long val) 506 { 507 switch (attr) { 508 case hwmon_chip_update_interval: 509 return lm75_update_interval(dev, val); 510 default: 511 return -EINVAL; 512 } 513 return 0; 514 } 515 516 static int lm75_write(struct device *dev, enum hwmon_sensor_types type, 517 u32 attr, int channel, long val) 518 { 519 switch (type) { 520 case hwmon_chip: 521 return lm75_write_chip(dev, attr, val); 522 case hwmon_temp: 523 return lm75_write_temp(dev, attr, val); 524 default: 525 return -EINVAL; 526 } 527 return 0; 528 } 529 530 static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type, 531 u32 attr, int channel) 532 { 533 const struct lm75_data *config_data = data; 534 535 switch (type) { 536 case hwmon_chip: 537 switch (attr) { 538 case hwmon_chip_update_interval: 539 if (config_data->params->num_sample_times > 1) 540 return 0644; 541 return 0444; 542 } 543 break; 544 case hwmon_temp: 545 switch (attr) { 546 case hwmon_temp_input: 547 return 0444; 548 case hwmon_temp_label: 549 /* Hide label node if label is not provided */ 550 return config_data->label ? 0444 : 0; 551 case hwmon_temp_max: 552 case hwmon_temp_max_hyst: 553 return 0644; 554 case hwmon_temp_alarm: 555 if (config_data->params->alarm) 556 return 0444; 557 break; 558 } 559 break; 560 default: 561 break; 562 } 563 return 0; 564 } 565 566 static const struct hwmon_channel_info * const lm75_info[] = { 567 HWMON_CHANNEL_INFO(chip, 568 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL), 569 HWMON_CHANNEL_INFO(temp, 570 HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_MAX | HWMON_T_MAX_HYST | 571 HWMON_T_ALARM), 572 NULL 573 }; 574 575 static const struct hwmon_ops lm75_hwmon_ops = { 576 .is_visible = lm75_is_visible, 577 .read_string = lm75_read_string, 578 .read = lm75_read, 579 .write = lm75_write, 580 }; 581 582 static const struct hwmon_chip_info lm75_chip_info = { 583 .ops = &lm75_hwmon_ops, 584 .info = lm75_info, 585 }; 586 587 static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg) 588 { 589 return reg != LM75_REG_TEMP; 590 } 591 592 static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg) 593 { 594 return reg == LM75_REG_TEMP || reg == LM75_REG_CONF; 595 } 596 597 static int lm75_i2c_reg_read(void *context, unsigned int reg, unsigned int *val) 598 { 599 struct i2c_client *client = context; 600 struct lm75_data *data = i2c_get_clientdata(client); 601 int ret; 602 603 if (reg == LM75_REG_CONF) { 604 if (!data->params->config_reg_16bits) 605 ret = i2c_smbus_read_byte_data(client, LM75_REG_CONF); 606 else 607 ret = i2c_smbus_read_word_data(client, LM75_REG_CONF); 608 } else { 609 ret = i2c_smbus_read_word_swapped(client, reg); 610 } 611 if (ret < 0) 612 return ret; 613 *val = ret; 614 return 0; 615 } 616 617 static int lm75_i2c_reg_write(void *context, unsigned int reg, unsigned int val) 618 { 619 struct i2c_client *client = context; 620 struct lm75_data *data = i2c_get_clientdata(client); 621 622 if (reg == PCT2075_REG_IDLE || 623 (reg == LM75_REG_CONF && !data->params->config_reg_16bits)) 624 return i2c_smbus_write_byte_data(client, reg, val); 625 else if (reg == LM75_REG_CONF) 626 return i2c_smbus_write_word_data(client, reg, val); 627 return i2c_smbus_write_word_swapped(client, reg, val); 628 } 629 630 static const struct regmap_bus lm75_i2c_regmap_bus = { 631 .reg_read = lm75_i2c_reg_read, 632 .reg_write = lm75_i2c_reg_write, 633 }; 634 635 static int lm75_i3c_reg_read(void *context, unsigned int reg, unsigned int *val) 636 { 637 struct i3c_device *i3cdev = context; 638 struct lm75_data *data = i3cdev_get_drvdata(i3cdev); 639 struct i3c_xfer xfers[] = { 640 { 641 .rnw = false, 642 .len = 1, 643 .data.out = data->reg_buf, 644 }, 645 { 646 .rnw = true, 647 .len = 2, 648 .data.in = data->val_buf, 649 }, 650 }; 651 int ret; 652 653 data->reg_buf[0] = reg; 654 655 if (reg == LM75_REG_CONF && !data->params->config_reg_16bits) 656 xfers[1].len--; 657 658 ret = i3c_device_do_xfers(i3cdev, xfers, 2, I3C_SDR); 659 if (ret < 0) 660 return ret; 661 662 if (reg == LM75_REG_CONF && !data->params->config_reg_16bits) 663 *val = data->val_buf[0]; 664 else if (reg == LM75_REG_CONF) 665 *val = data->val_buf[0] | (data->val_buf[1] << 8); 666 else 667 *val = data->val_buf[1] | (data->val_buf[0] << 8); 668 669 return 0; 670 } 671 672 static int lm75_i3c_reg_write(void *context, unsigned int reg, unsigned int val) 673 { 674 struct i3c_device *i3cdev = context; 675 struct lm75_data *data = i3cdev_get_drvdata(i3cdev); 676 struct i3c_xfer xfers[] = { 677 { 678 .rnw = false, 679 .len = 3, 680 .data.out = data->val_buf, 681 }, 682 }; 683 684 data->val_buf[0] = reg; 685 686 if (reg == PCT2075_REG_IDLE || 687 (reg == LM75_REG_CONF && !data->params->config_reg_16bits)) { 688 xfers[0].len--; 689 data->val_buf[1] = val & 0xff; 690 } else if (reg == LM75_REG_CONF) { 691 data->val_buf[1] = val & 0xff; 692 data->val_buf[2] = (val >> 8) & 0xff; 693 } else { 694 data->val_buf[1] = (val >> 8) & 0xff; 695 data->val_buf[2] = val & 0xff; 696 } 697 698 return i3c_device_do_xfers(i3cdev, xfers, 1, I3C_SDR); 699 } 700 701 static const struct regmap_bus lm75_i3c_regmap_bus = { 702 .reg_read = lm75_i3c_reg_read, 703 .reg_write = lm75_i3c_reg_write, 704 }; 705 706 static const struct regmap_config lm75_regmap_config = { 707 .reg_bits = 8, 708 .val_bits = 16, 709 .max_register = PCT2075_REG_IDLE, 710 .writeable_reg = lm75_is_writeable_reg, 711 .volatile_reg = lm75_is_volatile_reg, 712 .val_format_endian = REGMAP_ENDIAN_BIG, 713 .cache_type = REGCACHE_MAPLE, 714 .use_single_read = true, 715 .use_single_write = true, 716 }; 717 718 static void lm75_remove(void *data) 719 { 720 struct lm75_data *lm75 = data; 721 722 regmap_write(lm75->regmap, LM75_REG_CONF, lm75->orig_conf); 723 } 724 725 static int lm75_generic_probe(struct device *dev, const char *name, 726 enum lm75_type kind, int irq, struct regmap *regmap) 727 { 728 struct device *hwmon_dev; 729 struct lm75_data *data; 730 int status, err; 731 732 data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL); 733 if (!data) 734 return -ENOMEM; 735 736 /* needed by custom regmap callbacks */ 737 dev_set_drvdata(dev, data); 738 739 /* Save the connected input label if available */ 740 device_property_read_string(dev, "label", &data->label); 741 742 data->kind = kind; 743 data->regmap = regmap; 744 745 err = devm_regulator_get_enable(dev, "vs"); 746 if (err) 747 return err; 748 749 /* Set to LM75 resolution (9 bits, 1/2 degree C) and range. 750 * Then tweak to be more precise when appropriate. 751 */ 752 753 data->params = &device_params[data->kind]; 754 755 /* Save default sample time and resolution*/ 756 data->sample_time = data->params->default_sample_time; 757 data->resolution = data->params->default_resolution; 758 759 /* Cache original configuration */ 760 err = regmap_read(data->regmap, LM75_REG_CONF, &status); 761 if (err) 762 return err; 763 data->orig_conf = status; 764 765 err = lm75_write_config(data, data->params->set_mask, 766 data->params->clr_mask); 767 if (err) 768 return err; 769 770 err = devm_add_action_or_reset(dev, lm75_remove, data); 771 if (err) 772 return err; 773 774 hwmon_dev = devm_hwmon_device_register_with_info(dev, name, data, 775 &lm75_chip_info, NULL); 776 if (IS_ERR(hwmon_dev)) 777 return PTR_ERR(hwmon_dev); 778 779 if (irq) { 780 if (data->params->alarm) { 781 err = devm_request_threaded_irq(dev, 782 irq, 783 NULL, 784 &lm75_alarm_handler, 785 IRQF_ONESHOT, 786 name, 787 hwmon_dev); 788 if (err) 789 return err; 790 } else { 791 /* alarm is only supported for chips with alarm bit */ 792 dev_err(dev, "alarm interrupt is not supported\n"); 793 } 794 } 795 796 dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), name); 797 798 return 0; 799 } 800 801 static int lm75_i2c_probe(struct i2c_client *client) 802 { 803 struct device *dev = &client->dev; 804 struct regmap *regmap; 805 806 if (!i2c_check_functionality(client->adapter, 807 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA)) 808 return -EOPNOTSUPP; 809 810 regmap = devm_regmap_init(dev, &lm75_i2c_regmap_bus, client, &lm75_regmap_config); 811 if (IS_ERR(regmap)) 812 return PTR_ERR(regmap); 813 814 return lm75_generic_probe(dev, client->name, (uintptr_t)i2c_get_match_data(client), 815 client->irq, regmap); 816 } 817 818 static const struct i2c_device_id lm75_i2c_ids[] = { 819 { "adt75", adt75, }, 820 { "as6200", as6200, }, 821 { "at30ts74", at30ts74, }, 822 { "ds1775", ds1775, }, 823 { "ds75", ds75, }, 824 { "ds7505", ds7505, }, 825 { "g751", g751, }, 826 { "lm75", lm75, }, 827 { "lm75a", lm75a, }, 828 { "lm75b", lm75b, }, 829 { "max6625", max6625, }, 830 { "max6626", max6626, }, 831 { "max31725", max31725, }, 832 { "max31726", max31725, }, 833 { "mcp980x", mcp980x, }, 834 { "p3t1750", p3t1750, }, 835 { "p3t1755", p3t1755, }, 836 { "pct2075", pct2075, }, 837 { "stds75", stds75, }, 838 { "stlm75", stlm75, }, 839 { "tcn75", tcn75, }, 840 { "tmp100", tmp100, }, 841 { "tmp101", tmp101, }, 842 { "tmp105", tmp105, }, 843 { "tmp112", tmp112, }, 844 { "tmp175", tmp175, }, 845 { "tmp275", tmp275, }, 846 { "tmp75", tmp75, }, 847 { "tmp75b", tmp75b, }, 848 { "tmp75c", tmp75c, }, 849 { "tmp1075", tmp1075, }, 850 { /* LIST END */ } 851 }; 852 MODULE_DEVICE_TABLE(i2c, lm75_i2c_ids); 853 854 struct lm75_i3c_device { 855 enum lm75_type type; 856 const char *name; 857 }; 858 859 static const struct lm75_i3c_device lm75_i3c_p3t1755 = { 860 .name = "p3t1755", 861 .type = p3t1755, 862 }; 863 864 static const struct i3c_device_id lm75_i3c_ids[] = { 865 I3C_DEVICE(0x011b, 0x152a, &lm75_i3c_p3t1755), 866 { /* LIST END */ } 867 }; 868 MODULE_DEVICE_TABLE(i3c, lm75_i3c_ids); 869 870 static int lm75_i3c_probe(struct i3c_device *i3cdev) 871 { 872 struct device *dev = i3cdev_to_dev(i3cdev); 873 const struct lm75_i3c_device *id_data; 874 struct regmap *regmap; 875 876 regmap = devm_regmap_init(dev, &lm75_i3c_regmap_bus, i3cdev, &lm75_regmap_config); 877 if (IS_ERR(regmap)) 878 return PTR_ERR(regmap); 879 880 id_data = i3c_device_match_id(i3cdev, lm75_i3c_ids)->data; 881 882 return lm75_generic_probe(dev, id_data->name, id_data->type, 0, regmap); 883 } 884 885 static const struct of_device_id __maybe_unused lm75_of_match[] = { 886 { 887 .compatible = "adi,adt75", 888 .data = (void *)adt75 889 }, 890 { 891 .compatible = "ams,as6200", 892 .data = (void *)as6200 893 }, 894 { 895 .compatible = "atmel,at30ts74", 896 .data = (void *)at30ts74 897 }, 898 { 899 .compatible = "dallas,ds1775", 900 .data = (void *)ds1775 901 }, 902 { 903 .compatible = "dallas,ds75", 904 .data = (void *)ds75 905 }, 906 { 907 .compatible = "dallas,ds7505", 908 .data = (void *)ds7505 909 }, 910 { 911 .compatible = "gmt,g751", 912 .data = (void *)g751 913 }, 914 { 915 .compatible = "national,lm75", 916 .data = (void *)lm75 917 }, 918 { 919 .compatible = "national,lm75a", 920 .data = (void *)lm75a 921 }, 922 { 923 .compatible = "national,lm75b", 924 .data = (void *)lm75b 925 }, 926 { 927 .compatible = "maxim,max6625", 928 .data = (void *)max6625 929 }, 930 { 931 .compatible = "maxim,max6626", 932 .data = (void *)max6626 933 }, 934 { 935 .compatible = "maxim,max31725", 936 .data = (void *)max31725 937 }, 938 { 939 .compatible = "maxim,max31726", 940 .data = (void *)max31725 941 }, 942 { 943 .compatible = "maxim,mcp980x", 944 .data = (void *)mcp980x 945 }, 946 { 947 .compatible = "nxp,p3t1750", 948 .data = (void *)p3t1750 949 }, 950 { 951 .compatible = "nxp,p3t1755", 952 .data = (void *)p3t1755 953 }, 954 { 955 .compatible = "nxp,pct2075", 956 .data = (void *)pct2075 957 }, 958 { 959 .compatible = "st,stds75", 960 .data = (void *)stds75 961 }, 962 { 963 .compatible = "st,stlm75", 964 .data = (void *)stlm75 965 }, 966 { 967 .compatible = "microchip,tcn75", 968 .data = (void *)tcn75 969 }, 970 { 971 .compatible = "ti,tmp100", 972 .data = (void *)tmp100 973 }, 974 { 975 .compatible = "ti,tmp101", 976 .data = (void *)tmp101 977 }, 978 { 979 .compatible = "ti,tmp105", 980 .data = (void *)tmp105 981 }, 982 { 983 .compatible = "ti,tmp112", 984 .data = (void *)tmp112 985 }, 986 { 987 .compatible = "ti,tmp175", 988 .data = (void *)tmp175 989 }, 990 { 991 .compatible = "ti,tmp275", 992 .data = (void *)tmp275 993 }, 994 { 995 .compatible = "ti,tmp75", 996 .data = (void *)tmp75 997 }, 998 { 999 .compatible = "ti,tmp75b", 1000 .data = (void *)tmp75b 1001 }, 1002 { 1003 .compatible = "ti,tmp75c", 1004 .data = (void *)tmp75c 1005 }, 1006 { 1007 .compatible = "ti,tmp1075", 1008 .data = (void *)tmp1075 1009 }, 1010 { }, 1011 }; 1012 MODULE_DEVICE_TABLE(of, lm75_of_match); 1013 1014 #define LM75A_ID 0xA1 1015 1016 /* Return 0 if detection is successful, -ENODEV otherwise */ 1017 static int lm75_detect(struct i2c_client *new_client, 1018 struct i2c_board_info *info) 1019 { 1020 struct i2c_adapter *adapter = new_client->adapter; 1021 int i; 1022 int conf, hyst, os; 1023 bool is_lm75a = 0; 1024 1025 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 1026 I2C_FUNC_SMBUS_WORD_DATA)) 1027 return -ENODEV; 1028 1029 /* 1030 * Now, we do the remaining detection. There is no identification- 1031 * dedicated register so we have to rely on several tricks: 1032 * unused bits, registers cycling over 8-address boundaries, 1033 * addresses 0x04-0x07 returning the last read value. 1034 * The cycling+unused addresses combination is not tested, 1035 * since it would significantly slow the detection down and would 1036 * hardly add any value. 1037 * 1038 * The National Semiconductor LM75A is different than earlier 1039 * LM75s. It has an ID byte of 0xaX (where X is the chip 1040 * revision, with 1 being the only revision in existence) in 1041 * register 7, and unused registers return 0xff rather than the 1042 * last read value. 1043 * 1044 * Note that this function only detects the original National 1045 * Semiconductor LM75 and the LM75A. Clones from other vendors 1046 * aren't detected, on purpose, because they are typically never 1047 * found on PC hardware. They are found on embedded designs where 1048 * they can be instantiated explicitly so detection is not needed. 1049 * The absence of identification registers on all these clones 1050 * would make their exhaustive detection very difficult and weak, 1051 * and odds are that the driver would bind to unsupported devices. 1052 */ 1053 1054 /* Unused bits */ 1055 conf = i2c_smbus_read_byte_data(new_client, 1); 1056 if (conf & 0xe0) 1057 return -ENODEV; 1058 1059 /* First check for LM75A */ 1060 if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) { 1061 /* 1062 * LM75A returns 0xff on unused registers so 1063 * just to be sure we check for that too. 1064 */ 1065 if (i2c_smbus_read_byte_data(new_client, 4) != 0xff 1066 || i2c_smbus_read_byte_data(new_client, 5) != 0xff 1067 || i2c_smbus_read_byte_data(new_client, 6) != 0xff) 1068 return -ENODEV; 1069 is_lm75a = 1; 1070 hyst = i2c_smbus_read_byte_data(new_client, 2); 1071 os = i2c_smbus_read_byte_data(new_client, 3); 1072 } else { /* Traditional style LM75 detection */ 1073 /* Unused addresses */ 1074 hyst = i2c_smbus_read_byte_data(new_client, 2); 1075 if (i2c_smbus_read_byte_data(new_client, 4) != hyst 1076 || i2c_smbus_read_byte_data(new_client, 5) != hyst 1077 || i2c_smbus_read_byte_data(new_client, 6) != hyst 1078 || i2c_smbus_read_byte_data(new_client, 7) != hyst) 1079 return -ENODEV; 1080 os = i2c_smbus_read_byte_data(new_client, 3); 1081 if (i2c_smbus_read_byte_data(new_client, 4) != os 1082 || i2c_smbus_read_byte_data(new_client, 5) != os 1083 || i2c_smbus_read_byte_data(new_client, 6) != os 1084 || i2c_smbus_read_byte_data(new_client, 7) != os) 1085 return -ENODEV; 1086 } 1087 /* 1088 * It is very unlikely that this is a LM75 if both 1089 * hysteresis and temperature limit registers are 0. 1090 */ 1091 if (hyst == 0 && os == 0) 1092 return -ENODEV; 1093 1094 /* Addresses cycling */ 1095 for (i = 8; i <= 248; i += 40) { 1096 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf 1097 || i2c_smbus_read_byte_data(new_client, i + 2) != hyst 1098 || i2c_smbus_read_byte_data(new_client, i + 3) != os) 1099 return -ENODEV; 1100 if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7) 1101 != LM75A_ID) 1102 return -ENODEV; 1103 } 1104 1105 strscpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE); 1106 1107 return 0; 1108 } 1109 1110 #ifdef CONFIG_PM 1111 static int lm75_suspend(struct device *dev) 1112 { 1113 struct lm75_data *data = dev_get_drvdata(dev); 1114 1115 return regmap_update_bits(data->regmap, LM75_REG_CONF, LM75_SHUTDOWN, LM75_SHUTDOWN); 1116 } 1117 1118 static int lm75_resume(struct device *dev) 1119 { 1120 struct lm75_data *data = dev_get_drvdata(dev); 1121 1122 return regmap_update_bits(data->regmap, LM75_REG_CONF, LM75_SHUTDOWN, 0); 1123 } 1124 1125 static const struct dev_pm_ops lm75_dev_pm_ops = { 1126 .suspend = lm75_suspend, 1127 .resume = lm75_resume, 1128 }; 1129 #define LM75_DEV_PM_OPS (&lm75_dev_pm_ops) 1130 #else 1131 #define LM75_DEV_PM_OPS NULL 1132 #endif /* CONFIG_PM */ 1133 1134 static struct i2c_driver lm75_i2c_driver = { 1135 .class = I2C_CLASS_HWMON, 1136 .driver = { 1137 .name = "lm75", 1138 .of_match_table = of_match_ptr(lm75_of_match), 1139 .pm = LM75_DEV_PM_OPS, 1140 }, 1141 .probe = lm75_i2c_probe, 1142 .id_table = lm75_i2c_ids, 1143 .detect = lm75_detect, 1144 .address_list = normal_i2c, 1145 }; 1146 1147 static struct i3c_driver lm75_i3c_driver = { 1148 .driver = { 1149 .name = "lm75_i3c", 1150 }, 1151 .probe = lm75_i3c_probe, 1152 .id_table = lm75_i3c_ids, 1153 }; 1154 1155 module_i3c_i2c_driver(lm75_i3c_driver, &lm75_i2c_driver) 1156 1157 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"); 1158 MODULE_DESCRIPTION("LM75 driver"); 1159 MODULE_LICENSE("GPL"); 1160