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