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