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