1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * w1_therm.c 4 * 5 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net> 6 */ 7 8 #include <asm/types.h> 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/moduleparam.h> 13 #include <linux/sched.h> 14 #include <linux/device.h> 15 #include <linux/types.h> 16 #include <linux/slab.h> 17 #include <linux/delay.h> 18 #include <linux/hwmon.h> 19 #include <linux/string.h> 20 #include <linux/jiffies.h> 21 22 #include <linux/w1.h> 23 24 #define W1_THERM_DS18S20 0x10 25 #define W1_THERM_DS1822 0x22 26 #define W1_THERM_DS18B20 0x28 27 #define W1_THERM_DS1825 0x3B 28 #define W1_THERM_DS28EA00 0x42 29 30 /* 31 * Allow the strong pullup to be disabled, but default to enabled. 32 * If it was disabled a parasite powered device might not get the require 33 * current to do a temperature conversion. If it is enabled parasite powered 34 * devices have a better chance of getting the current required. 35 * In case the parasite power-detection is not working (seems to be the case 36 * for some DS18S20) the strong pullup can also be forced, regardless of the 37 * power state of the devices. 38 * 39 * Summary of options: 40 * - strong_pullup = 0 Disable strong pullup completely 41 * - strong_pullup = 1 Enable automatic strong pullup detection 42 * - strong_pullup = 2 Force strong pullup 43 */ 44 static int w1_strong_pullup = 1; 45 module_param_named(strong_pullup, w1_strong_pullup, int, 0); 46 47 /* Counter for devices supporting bulk reading */ 48 static u16 bulk_read_device_counter; /* =0 as per C standard */ 49 50 /* This command should be in public header w1.h but is not */ 51 #define W1_RECALL_EEPROM 0xB8 52 53 /* Nb of try for an operation */ 54 #define W1_THERM_MAX_TRY 5 55 56 /* ms delay to retry bus mutex */ 57 #define W1_THERM_RETRY_DELAY 20 58 59 /* delay in ms to write in EEPROM */ 60 #define W1_THERM_EEPROM_WRITE_DELAY 10 61 62 #define EEPROM_CMD_WRITE "save" /* cmd for write eeprom sysfs */ 63 #define EEPROM_CMD_READ "restore" /* cmd for read eeprom sysfs */ 64 #define BULK_TRIGGER_CMD "trigger" /* cmd to trigger a bulk read */ 65 66 #define MIN_TEMP -55 /* min temperature that can be measured */ 67 #define MAX_TEMP 125 /* max temperature that can be measured */ 68 69 /* Allowed values for sysfs conv_time attribute */ 70 #define CONV_TIME_DEFAULT 0 71 #define CONV_TIME_MEASURE 1 72 73 /* Bits in sysfs "features" value */ 74 #define W1_THERM_CHECK_RESULT 1 /* Enable conversion success check */ 75 #define W1_THERM_POLL_COMPLETION 2 /* Poll for conversion completion */ 76 #define W1_THERM_FEATURES_MASK 3 /* All values mask */ 77 78 /* Poll period in milliseconds. Should be less then a shortest operation on the device */ 79 #define W1_POLL_PERIOD 32 80 #define W1_POLL_CONVERT_TEMP 2000 /* Timeout for W1_CONVERT_TEMP, ms */ 81 #define W1_POLL_RECALL_EEPROM 500 /* Timeout for W1_RECALL_EEPROM, ms*/ 82 83 /* Masks for resolution functions, work with all devices */ 84 /* Bit mask for config register for all devices, bits 7,6,5 */ 85 #define W1_THERM_RESOLUTION_MASK 0xE0 86 /* Bit offset of resolution in config register for all devices */ 87 #define W1_THERM_RESOLUTION_SHIFT 5 88 /* Bit offset of resolution in config register for all devices */ 89 #define W1_THERM_RESOLUTION_SHIFT 5 90 /* Add this to bit value to get resolution */ 91 #define W1_THERM_RESOLUTION_MIN 9 92 /* Maximum allowed value */ 93 #define W1_THERM_RESOLUTION_MAX 14 94 95 /* Helpers Macros */ 96 97 /* 98 * return a pointer on the slave w1_therm_family_converter struct: 99 * always test family data existence before using this macro 100 */ 101 #define SLAVE_SPECIFIC_FUNC(sl) \ 102 (((struct w1_therm_family_data *)(sl->family_data))->specific_functions) 103 104 /* 105 * return the power mode of the sl slave : 1-ext, 0-parasite, <0 unknown 106 * always test family data existence before using this macro 107 */ 108 #define SLAVE_POWERMODE(sl) \ 109 (((struct w1_therm_family_data *)(sl->family_data))->external_powered) 110 111 /* 112 * return the resolution in bit of the sl slave : <0 unknown 113 * always test family data existence before using this macro 114 */ 115 #define SLAVE_RESOLUTION(sl) \ 116 (((struct w1_therm_family_data *)(sl->family_data))->resolution) 117 118 /* 119 * return the conv_time_override of the sl slave 120 * always test family data existence before using this macro 121 */ 122 #define SLAVE_CONV_TIME_OVERRIDE(sl) \ 123 (((struct w1_therm_family_data *)(sl->family_data))->conv_time_override) 124 125 /* 126 * return the features of the sl slave 127 * always test family data existence before using this macro 128 */ 129 #define SLAVE_FEATURES(sl) \ 130 (((struct w1_therm_family_data *)(sl->family_data))->features) 131 132 /* 133 * return whether or not a converT command has been issued to the slave 134 * * 0: no bulk read is pending 135 * * -1: conversion is in progress 136 * * 1: conversion done, result to be read 137 */ 138 #define SLAVE_CONVERT_TRIGGERED(sl) \ 139 (((struct w1_therm_family_data *)(sl->family_data))->convert_triggered) 140 141 /* return the address of the refcnt in the family data */ 142 #define THERM_REFCNT(family_data) \ 143 (&((struct w1_therm_family_data *)family_data)->refcnt) 144 145 /* Structs definition */ 146 147 /** 148 * struct w1_therm_family_converter - bind device specific functions 149 * @broken: flag for non-registred families 150 * @reserved: not used here 151 * @f: pointer to the device binding structure 152 * @convert: pointer to the device conversion function 153 * @get_conversion_time: pointer to the device conversion time function 154 * @set_resolution: pointer to the device set_resolution function 155 * @get_resolution: pointer to the device get_resolution function 156 * @write_data: pointer to the device writing function (2 or 3 bytes) 157 * @bulk_read: true if device family support bulk read, false otherwise 158 */ 159 struct w1_therm_family_converter { 160 u8 broken; 161 u16 reserved; 162 struct w1_family *f; 163 int (*convert)(u8 rom[9]); 164 int (*get_conversion_time)(struct w1_slave *sl); 165 int (*set_resolution)(struct w1_slave *sl, int val); 166 int (*get_resolution)(struct w1_slave *sl); 167 int (*write_data)(struct w1_slave *sl, const u8 *data); 168 bool bulk_read; 169 }; 170 171 /** 172 * struct w1_therm_family_data - device data 173 * @rom: ROM device id (64bit Lasered ROM code + 1 CRC byte) 174 * @refcnt: ref count 175 * @external_powered: 1 device powered externally, 176 * 0 device parasite powered, 177 * -x error or undefined 178 * @resolution: current device resolution 179 * @convert_triggered: conversion state of the device 180 * @conv_time_override: user selected conversion time or CONV_TIME_DEFAULT 181 * @features: bit mask - enable temperature validity check, poll for completion 182 * @specific_functions: pointer to struct of device specific function 183 */ 184 struct w1_therm_family_data { 185 uint8_t rom[9]; 186 atomic_t refcnt; 187 int external_powered; 188 int resolution; 189 int convert_triggered; 190 int conv_time_override; 191 unsigned int features; 192 struct w1_therm_family_converter *specific_functions; 193 }; 194 195 /** 196 * struct therm_info - store temperature reading 197 * @rom: read device data (8 data bytes + 1 CRC byte) 198 * @crc: computed crc from rom 199 * @verdict: 1 crc checked, 0 crc not matching 200 */ 201 struct therm_info { 202 u8 rom[9]; 203 u8 crc; 204 u8 verdict; 205 }; 206 207 /* Hardware Functions declaration */ 208 209 /** 210 * reset_select_slave() - reset and select a slave 211 * @sl: the slave to select 212 * 213 * Resets the bus and select the slave by sending a ROM MATCH cmd 214 * w1_reset_select_slave() from w1_io.c could not be used here because 215 * it sent a SKIP ROM command if only one device is on the line. 216 * At the beginning of the such process, sl->master->slave_count is 1 even if 217 * more devices are on the line, causing collision on the line. 218 * 219 * Context: The w1 master lock must be held. 220 * 221 * Return: 0 if success, negative kernel error code otherwise. 222 */ 223 static int reset_select_slave(struct w1_slave *sl); 224 225 /** 226 * convert_t() - Query the device for temperature conversion and read 227 * @sl: pointer to the slave to read 228 * @info: pointer to a structure to store the read results 229 * 230 * Return: 0 if success, -kernel error code otherwise 231 */ 232 static int convert_t(struct w1_slave *sl, struct therm_info *info); 233 234 /** 235 * read_scratchpad() - read the data in device RAM 236 * @sl: pointer to the slave to read 237 * @info: pointer to a structure to store the read results 238 * 239 * Return: 0 if success, -kernel error code otherwise 240 */ 241 static int read_scratchpad(struct w1_slave *sl, struct therm_info *info); 242 243 /** 244 * write_scratchpad() - write nb_bytes in the device RAM 245 * @sl: pointer to the slave to write in 246 * @data: pointer to an array of 3 bytes, as 3 bytes MUST be written 247 * @nb_bytes: number of bytes to be written (2 for DS18S20, 3 otherwise) 248 * 249 * Return: 0 if success, -kernel error code otherwise 250 */ 251 static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes); 252 253 /** 254 * copy_scratchpad() - Copy the content of scratchpad in device EEPROM 255 * @sl: slave involved 256 * 257 * Return: 0 if success, -kernel error code otherwise 258 */ 259 static int copy_scratchpad(struct w1_slave *sl); 260 261 /** 262 * recall_eeprom() - Restore EEPROM data to device RAM 263 * @sl: slave involved 264 * 265 * Return: 0 if success, -kernel error code otherwise 266 */ 267 static int recall_eeprom(struct w1_slave *sl); 268 269 /** 270 * read_powermode() - Query the power mode of the slave 271 * @sl: slave to retrieve the power mode 272 * 273 * Ask the device to get its power mode (external or parasite) 274 * and store the power status in the &struct w1_therm_family_data. 275 * 276 * Return: 277 * * 0 parasite powered device 278 * * 1 externally powered device 279 * * <0 kernel error code 280 */ 281 static int read_powermode(struct w1_slave *sl); 282 283 /** 284 * trigger_bulk_read() - function to trigger a bulk read on the bus 285 * @dev_master: the device master of the bus 286 * 287 * Send a SKIP ROM follow by a CONVERT T command on the bus. 288 * It also set the status flag in each slave &struct w1_therm_family_data 289 * to signal that a conversion is in progress. 290 * 291 * Return: 0 if success, -kernel error code otherwise 292 */ 293 static int trigger_bulk_read(struct w1_master *dev_master); 294 295 /* Sysfs interface declaration */ 296 297 static ssize_t w1_slave_show(struct device *device, 298 struct device_attribute *attr, char *buf); 299 300 static ssize_t w1_slave_store(struct device *device, 301 struct device_attribute *attr, const char *buf, size_t size); 302 303 static ssize_t w1_seq_show(struct device *device, 304 struct device_attribute *attr, char *buf); 305 306 static ssize_t temperature_show(struct device *device, 307 struct device_attribute *attr, char *buf); 308 309 static ssize_t ext_power_show(struct device *device, 310 struct device_attribute *attr, char *buf); 311 312 static ssize_t resolution_show(struct device *device, 313 struct device_attribute *attr, char *buf); 314 315 static ssize_t resolution_store(struct device *device, 316 struct device_attribute *attr, const char *buf, size_t size); 317 318 static ssize_t eeprom_cmd_store(struct device *device, 319 struct device_attribute *attr, const char *buf, size_t size); 320 321 static ssize_t alarms_store(struct device *device, 322 struct device_attribute *attr, const char *buf, size_t size); 323 324 static ssize_t alarms_show(struct device *device, 325 struct device_attribute *attr, char *buf); 326 327 static ssize_t therm_bulk_read_store(struct device *device, 328 struct device_attribute *attr, const char *buf, size_t size); 329 330 static ssize_t therm_bulk_read_show(struct device *device, 331 struct device_attribute *attr, char *buf); 332 333 static ssize_t conv_time_show(struct device *device, 334 struct device_attribute *attr, char *buf); 335 336 static ssize_t conv_time_store(struct device *device, 337 struct device_attribute *attr, const char *buf, 338 size_t size); 339 340 static ssize_t features_show(struct device *device, 341 struct device_attribute *attr, char *buf); 342 343 static ssize_t features_store(struct device *device, 344 struct device_attribute *attr, const char *buf, 345 size_t size); 346 /* Attributes declarations */ 347 348 static DEVICE_ATTR_RW(w1_slave); 349 static DEVICE_ATTR_RO(w1_seq); 350 static DEVICE_ATTR_RO(temperature); 351 static DEVICE_ATTR_RO(ext_power); 352 static DEVICE_ATTR_RW(resolution); 353 static DEVICE_ATTR_WO(eeprom_cmd); 354 static DEVICE_ATTR_RW(alarms); 355 static DEVICE_ATTR_RW(conv_time); 356 static DEVICE_ATTR_RW(features); 357 358 static DEVICE_ATTR_RW(therm_bulk_read); /* attribut at master level */ 359 360 /* Interface Functions declaration */ 361 362 /** 363 * w1_therm_add_slave() - Called when a new slave is discovered 364 * @sl: slave just discovered by the master. 365 * 366 * Called by the master when the slave is discovered on the bus. Used to 367 * initialize slave state before the beginning of any communication. 368 * 369 * Return: 0 - If success, negative kernel code otherwise 370 */ 371 static int w1_therm_add_slave(struct w1_slave *sl); 372 373 /** 374 * w1_therm_remove_slave() - Called when a slave is removed 375 * @sl: slave to be removed. 376 * 377 * Called by the master when the slave is considered not to be on the bus 378 * anymore. Used to free memory. 379 */ 380 static void w1_therm_remove_slave(struct w1_slave *sl); 381 382 /* Family attributes */ 383 384 static struct attribute *w1_therm_attrs[] = { 385 &dev_attr_w1_slave.attr, 386 &dev_attr_temperature.attr, 387 &dev_attr_ext_power.attr, 388 &dev_attr_resolution.attr, 389 &dev_attr_eeprom_cmd.attr, 390 &dev_attr_alarms.attr, 391 &dev_attr_conv_time.attr, 392 &dev_attr_features.attr, 393 NULL, 394 }; 395 396 static struct attribute *w1_ds18s20_attrs[] = { 397 &dev_attr_w1_slave.attr, 398 &dev_attr_temperature.attr, 399 &dev_attr_ext_power.attr, 400 &dev_attr_eeprom_cmd.attr, 401 &dev_attr_alarms.attr, 402 &dev_attr_conv_time.attr, 403 &dev_attr_features.attr, 404 NULL, 405 }; 406 407 static struct attribute *w1_ds28ea00_attrs[] = { 408 &dev_attr_w1_slave.attr, 409 &dev_attr_w1_seq.attr, 410 &dev_attr_temperature.attr, 411 &dev_attr_ext_power.attr, 412 &dev_attr_resolution.attr, 413 &dev_attr_eeprom_cmd.attr, 414 &dev_attr_alarms.attr, 415 &dev_attr_conv_time.attr, 416 &dev_attr_features.attr, 417 NULL, 418 }; 419 420 /* Attribute groups */ 421 422 ATTRIBUTE_GROUPS(w1_therm); 423 ATTRIBUTE_GROUPS(w1_ds18s20); 424 ATTRIBUTE_GROUPS(w1_ds28ea00); 425 426 #if IS_REACHABLE(CONFIG_HWMON) 427 static int w1_read_temp(struct device *dev, u32 attr, int channel, 428 long *val); 429 430 static umode_t w1_is_visible(const void *_data, enum hwmon_sensor_types type, 431 u32 attr, int channel) 432 { 433 return attr == hwmon_temp_input ? 0444 : 0; 434 } 435 436 static int w1_read(struct device *dev, enum hwmon_sensor_types type, 437 u32 attr, int channel, long *val) 438 { 439 switch (type) { 440 case hwmon_temp: 441 return w1_read_temp(dev, attr, channel, val); 442 default: 443 return -EOPNOTSUPP; 444 } 445 } 446 447 static const struct hwmon_channel_info * const w1_info[] = { 448 HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT), 449 NULL 450 }; 451 452 static const struct hwmon_ops w1_hwmon_ops = { 453 .is_visible = w1_is_visible, 454 .read = w1_read, 455 }; 456 457 static const struct hwmon_chip_info w1_chip_info = { 458 .ops = &w1_hwmon_ops, 459 .info = w1_info, 460 }; 461 #define W1_CHIPINFO (&w1_chip_info) 462 #else 463 #define W1_CHIPINFO NULL 464 #endif 465 466 /* Family operations */ 467 468 static const struct w1_family_ops w1_therm_fops = { 469 .add_slave = w1_therm_add_slave, 470 .remove_slave = w1_therm_remove_slave, 471 .groups = w1_therm_groups, 472 .chip_info = W1_CHIPINFO, 473 }; 474 475 static const struct w1_family_ops w1_ds18s20_fops = { 476 .add_slave = w1_therm_add_slave, 477 .remove_slave = w1_therm_remove_slave, 478 .groups = w1_ds18s20_groups, 479 .chip_info = W1_CHIPINFO, 480 }; 481 482 static const struct w1_family_ops w1_ds28ea00_fops = { 483 .add_slave = w1_therm_add_slave, 484 .remove_slave = w1_therm_remove_slave, 485 .groups = w1_ds28ea00_groups, 486 .chip_info = W1_CHIPINFO, 487 }; 488 489 /* Family binding operations struct */ 490 491 static struct w1_family w1_therm_family_DS18S20 = { 492 .fid = W1_THERM_DS18S20, 493 .fops = &w1_ds18s20_fops, 494 }; 495 496 static struct w1_family w1_therm_family_DS18B20 = { 497 .fid = W1_THERM_DS18B20, 498 .fops = &w1_therm_fops, 499 }; 500 501 static struct w1_family w1_therm_family_DS1822 = { 502 .fid = W1_THERM_DS1822, 503 .fops = &w1_therm_fops, 504 }; 505 506 static struct w1_family w1_therm_family_DS28EA00 = { 507 .fid = W1_THERM_DS28EA00, 508 .fops = &w1_ds28ea00_fops, 509 }; 510 511 static struct w1_family w1_therm_family_DS1825 = { 512 .fid = W1_THERM_DS1825, 513 .fops = &w1_therm_fops, 514 }; 515 516 /* Device dependent func */ 517 518 static inline int w1_DS18B20_convert_time(struct w1_slave *sl) 519 { 520 int ret; 521 522 if (!sl->family_data) 523 return -ENODEV; /* device unknown */ 524 525 if (SLAVE_CONV_TIME_OVERRIDE(sl) != CONV_TIME_DEFAULT) 526 return SLAVE_CONV_TIME_OVERRIDE(sl); 527 528 /* Return the conversion time, depending on resolution, 529 * select maximum conversion time among all compatible devices 530 */ 531 switch (SLAVE_RESOLUTION(sl)) { 532 case 9: 533 ret = 95; 534 break; 535 case 10: 536 ret = 190; 537 break; 538 case 11: 539 ret = 375; 540 break; 541 case 12: 542 ret = 750; 543 break; 544 case 13: 545 ret = 850; /* GX20MH01 only. Datasheet says 500ms, but that's not enough. */ 546 break; 547 case 14: 548 ret = 1600; /* GX20MH01 only. Datasheet says 1000ms - not enough */ 549 break; 550 default: 551 ret = 750; 552 } 553 return ret; 554 } 555 556 static inline int w1_DS18S20_convert_time(struct w1_slave *sl) 557 { 558 if (!sl->family_data) 559 return -ENODEV; /* device unknown */ 560 561 if (SLAVE_CONV_TIME_OVERRIDE(sl) == CONV_TIME_DEFAULT) 562 return 750; /* default for DS18S20 */ 563 else 564 return SLAVE_CONV_TIME_OVERRIDE(sl); 565 } 566 567 static inline int w1_DS1825_convert_time(struct w1_slave *sl) 568 { 569 int ret; 570 571 if (!sl->family_data) 572 return -ENODEV; /* device unknown */ 573 574 if (SLAVE_CONV_TIME_OVERRIDE(sl) != CONV_TIME_DEFAULT) 575 return SLAVE_CONV_TIME_OVERRIDE(sl); 576 577 /* Return the conversion time, depending on resolution, 578 * select maximum conversion time among all compatible devices 579 */ 580 switch (SLAVE_RESOLUTION(sl)) { 581 case 9: 582 ret = 95; 583 break; 584 case 10: 585 ret = 190; 586 break; 587 case 11: 588 ret = 375; 589 break; 590 case 12: 591 ret = 750; 592 break; 593 case 14: 594 ret = 100; /* MAX31850 only. Datasheet says 100ms */ 595 break; 596 default: 597 ret = 750; 598 } 599 return ret; 600 } 601 602 static inline int w1_DS18B20_write_data(struct w1_slave *sl, 603 const u8 *data) 604 { 605 return write_scratchpad(sl, data, 3); 606 } 607 608 static inline int w1_DS18S20_write_data(struct w1_slave *sl, 609 const u8 *data) 610 { 611 /* No config register */ 612 return write_scratchpad(sl, data, 2); 613 } 614 615 static inline int w1_DS18B20_set_resolution(struct w1_slave *sl, int val) 616 { 617 int ret; 618 struct therm_info info, info2; 619 620 /* DS18B20 resolution is 9 to 12 bits */ 621 /* GX20MH01 resolution is 9 to 14 bits */ 622 /* MAX31850 resolution is fixed 14 bits */ 623 if (val < W1_THERM_RESOLUTION_MIN || val > W1_THERM_RESOLUTION_MAX) 624 return -EINVAL; 625 626 /* Calc bit value from resolution */ 627 val = (val - W1_THERM_RESOLUTION_MIN) << W1_THERM_RESOLUTION_SHIFT; 628 629 /* 630 * Read the scratchpad to change only the required bits 631 * (bit5 & bit 6 from byte 4) 632 */ 633 ret = read_scratchpad(sl, &info); 634 635 if (ret) 636 return ret; 637 638 639 info.rom[4] &= ~W1_THERM_RESOLUTION_MASK; 640 info.rom[4] |= val; 641 642 /* Write data in the device RAM */ 643 ret = w1_DS18B20_write_data(sl, info.rom + 2); 644 if (ret) 645 return ret; 646 647 /* Have to read back the resolution to verify an actual value 648 * GX20MH01 and DS18B20 are indistinguishable by family number, but resolutions differ 649 * Some DS18B20 clones don't support resolution change 650 */ 651 ret = read_scratchpad(sl, &info2); 652 if (ret) 653 /* Scratchpad read fail */ 654 return ret; 655 656 if ((info2.rom[4] & W1_THERM_RESOLUTION_MASK) == (info.rom[4] & W1_THERM_RESOLUTION_MASK)) 657 return 0; 658 659 /* Resolution verify error */ 660 return -EIO; 661 } 662 663 static inline int w1_DS18B20_get_resolution(struct w1_slave *sl) 664 { 665 int ret; 666 int resolution; 667 struct therm_info info; 668 669 ret = read_scratchpad(sl, &info); 670 671 if (ret) 672 return ret; 673 674 resolution = ((info.rom[4] & W1_THERM_RESOLUTION_MASK) >> W1_THERM_RESOLUTION_SHIFT) 675 + W1_THERM_RESOLUTION_MIN; 676 /* GX20MH01 has one special case: 677 * >=14 means 14 bits when getting resolution from bit value. 678 * MAX31850 delivers fixed 15 and has 14 bits. 679 * Other devices have no more then 12 bits. 680 */ 681 if (resolution > W1_THERM_RESOLUTION_MAX) 682 resolution = W1_THERM_RESOLUTION_MAX; 683 684 return resolution; 685 } 686 687 /** 688 * w1_DS18B20_convert_temp() - temperature computation for DS18B20 689 * @rom: data read from device RAM (8 data bytes + 1 CRC byte) 690 * 691 * Can be called for any DS18B20 compliant device. 692 * 693 * Return: value in millidegrees Celsius. 694 */ 695 static inline int w1_DS18B20_convert_temp(u8 rom[9]) 696 { 697 u16 bv; 698 s16 t; 699 700 /* Signed 16-bit value to unsigned, cpu order */ 701 bv = le16_to_cpup((__le16 *)rom); 702 703 /* Config register bit R2 = 1 - GX20MH01 in 13 or 14 bit resolution mode */ 704 if (rom[4] & 0x80) { 705 /* Insert two temperature bits from config register */ 706 /* Avoid arithmetic shift of signed value */ 707 bv = (bv << 2) | (rom[4] & 3); 708 t = (s16) bv; /* Degrees, lowest bit is 2^-6 */ 709 return (int)t * 1000 / 64; /* Sign-extend to int; millidegrees */ 710 } 711 t = (s16)bv; /* Degrees, lowest bit is 2^-4 */ 712 return (int)t * 1000 / 16; /* Sign-extend to int; millidegrees */ 713 } 714 715 /** 716 * w1_DS18S20_convert_temp() - temperature computation for DS18S20 717 * @rom: data read from device RAM (8 data bytes + 1 CRC byte) 718 * 719 * Can be called for any DS18S20 compliant device. 720 * 721 * Return: value in millidegrees Celsius. 722 */ 723 static inline int w1_DS18S20_convert_temp(u8 rom[9]) 724 { 725 int t, h; 726 727 if (!rom[7]) { 728 pr_debug("%s: Invalid argument for conversion\n", __func__); 729 return 0; 730 } 731 732 if (rom[1] == 0) 733 t = ((s32)rom[0] >> 1)*1000; 734 else 735 t = 1000*(-1*(s32)(0x100-rom[0]) >> 1); 736 737 t -= 250; 738 h = 1000*((s32)rom[7] - (s32)rom[6]); 739 h /= (s32)rom[7]; 740 t += h; 741 742 return t; 743 } 744 745 /** 746 * w1_DS1825_convert_temp() - temperature computation for DS1825 747 * @rom: data read from device RAM (8 data bytes + 1 CRC byte) 748 * 749 * Can be called for any DS1825 compliant device. 750 * Is used by MAX31850, too 751 * 752 * Return: value in millidegrees Celsius. 753 */ 754 755 static inline int w1_DS1825_convert_temp(u8 rom[9]) 756 { 757 u16 bv; 758 s16 t; 759 760 /* Signed 16-bit value to unsigned, cpu order */ 761 bv = le16_to_cpup((__le16 *)rom); 762 763 /* Config register bit 7 = 1 - MA31850 found, 14 bit resolution */ 764 if (rom[4] & 0x80) { 765 /* Mask out bits 0 (Fault) and 1 (Reserved) */ 766 /* Avoid arithmetic shift of signed value */ 767 bv = (bv & 0xFFFC); /* Degrees, lowest 4 bits are 2^-1, 2^-2 and 2 zero bits */ 768 } 769 t = (s16)bv; /* Degrees, lowest bit is 2^-4 */ 770 return (int)t * 1000 / 16; /* Sign-extend to int; millidegrees */ 771 } 772 773 /* Device capability description */ 774 /* GX20MH01 device shares family number and structure with DS18B20 */ 775 776 static struct w1_therm_family_converter w1_therm_families[] = { 777 { 778 .f = &w1_therm_family_DS18S20, 779 .convert = w1_DS18S20_convert_temp, 780 .get_conversion_time = w1_DS18S20_convert_time, 781 .set_resolution = NULL, /* no config register */ 782 .get_resolution = NULL, /* no config register */ 783 .write_data = w1_DS18S20_write_data, 784 .bulk_read = true 785 }, 786 { 787 .f = &w1_therm_family_DS1822, 788 .convert = w1_DS18B20_convert_temp, 789 .get_conversion_time = w1_DS18B20_convert_time, 790 .set_resolution = w1_DS18B20_set_resolution, 791 .get_resolution = w1_DS18B20_get_resolution, 792 .write_data = w1_DS18B20_write_data, 793 .bulk_read = true 794 }, 795 { 796 /* Also used for GX20MH01 */ 797 .f = &w1_therm_family_DS18B20, 798 .convert = w1_DS18B20_convert_temp, 799 .get_conversion_time = w1_DS18B20_convert_time, 800 .set_resolution = w1_DS18B20_set_resolution, 801 .get_resolution = w1_DS18B20_get_resolution, 802 .write_data = w1_DS18B20_write_data, 803 .bulk_read = true 804 }, 805 { 806 .f = &w1_therm_family_DS28EA00, 807 .convert = w1_DS18B20_convert_temp, 808 .get_conversion_time = w1_DS18B20_convert_time, 809 .set_resolution = w1_DS18B20_set_resolution, 810 .get_resolution = w1_DS18B20_get_resolution, 811 .write_data = w1_DS18B20_write_data, 812 .bulk_read = false 813 }, 814 { 815 /* Also used for MAX31850 */ 816 .f = &w1_therm_family_DS1825, 817 .convert = w1_DS1825_convert_temp, 818 .get_conversion_time = w1_DS1825_convert_time, 819 .set_resolution = w1_DS18B20_set_resolution, 820 .get_resolution = w1_DS18B20_get_resolution, 821 .write_data = w1_DS18B20_write_data, 822 .bulk_read = true 823 } 824 }; 825 826 /* Helpers Functions */ 827 828 /** 829 * device_family() - Retrieve a pointer on &struct w1_therm_family_converter 830 * @sl: slave to retrieve the device specific structure 831 * 832 * Return: pointer to the slaves's family converter, NULL if not known 833 */ 834 static struct w1_therm_family_converter *device_family(struct w1_slave *sl) 835 { 836 struct w1_therm_family_converter *ret = NULL; 837 int i; 838 839 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) { 840 if (w1_therm_families[i].f->fid == sl->family->fid) { 841 ret = &w1_therm_families[i]; 842 break; 843 } 844 } 845 return ret; 846 } 847 848 /** 849 * bus_mutex_lock() - Acquire the mutex 850 * @lock: w1 bus mutex to acquire 851 * 852 * It try to acquire the mutex W1_THERM_MAX_TRY times and wait 853 * W1_THERM_RETRY_DELAY between 2 attempts. 854 * 855 * Return: true is mutex is acquired and lock, false otherwise 856 */ 857 static inline bool bus_mutex_lock(struct mutex *lock) 858 { 859 int max_trying = W1_THERM_MAX_TRY; 860 861 /* try to acquire the mutex, if not, sleep retry_delay before retry) */ 862 while (mutex_lock_interruptible(lock) != 0 && max_trying > 0) { 863 unsigned long sleep_rem; 864 865 sleep_rem = msleep_interruptible(W1_THERM_RETRY_DELAY); 866 if (!sleep_rem) 867 max_trying--; 868 } 869 870 if (!max_trying) 871 return false; /* Didn't acquire the bus mutex */ 872 873 return true; 874 } 875 876 /** 877 * check_family_data() - Check if family data and specific functions are present 878 * @sl: W1 device data 879 * 880 * Return: 0 - OK, negative value - error 881 */ 882 static int check_family_data(struct w1_slave *sl) 883 { 884 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 885 dev_info(&sl->dev, 886 "%s: Device is not supported by the driver\n", __func__); 887 return -EINVAL; /* No device family */ 888 } 889 return 0; 890 } 891 892 /** 893 * bulk_read_support() - check if slave support bulk read 894 * @sl: device to check the ability 895 * 896 * Return: true if bulk read is supported, false if not or error 897 */ 898 static inline bool bulk_read_support(struct w1_slave *sl) 899 { 900 if (SLAVE_SPECIFIC_FUNC(sl)) 901 return SLAVE_SPECIFIC_FUNC(sl)->bulk_read; 902 903 dev_info(&sl->dev, 904 "%s: Device not supported by the driver\n", __func__); 905 906 return false; /* No device family */ 907 } 908 909 /** 910 * conversion_time() - get the Tconv for the slave 911 * @sl: device to get the conversion time 912 * 913 * On device supporting resolution settings, conversion time depend 914 * on the resolution setting. This helper function get the slave timing, 915 * depending on its current setting. 916 * 917 * Return: conversion time in ms, negative values are kernel error code 918 */ 919 static inline int conversion_time(struct w1_slave *sl) 920 { 921 if (SLAVE_SPECIFIC_FUNC(sl)) 922 return SLAVE_SPECIFIC_FUNC(sl)->get_conversion_time(sl); 923 924 dev_info(&sl->dev, 925 "%s: Device not supported by the driver\n", __func__); 926 927 return -ENODEV; /* No device family */ 928 } 929 930 /** 931 * temperature_from_RAM() - Convert the read info to temperature 932 * @sl: device that sent the RAM data 933 * @rom: read value on the slave device RAM 934 * 935 * Device dependent, the function bind the correct computation method. 936 * 937 * Return: temperature in 1/1000degC, 0 on error. 938 */ 939 static inline int temperature_from_RAM(struct w1_slave *sl, u8 rom[9]) 940 { 941 if (SLAVE_SPECIFIC_FUNC(sl)) 942 return SLAVE_SPECIFIC_FUNC(sl)->convert(rom); 943 944 dev_info(&sl->dev, 945 "%s: Device not supported by the driver\n", __func__); 946 947 return 0; /* No device family */ 948 } 949 950 /** 951 * int_to_short() - Safe casting of int to short 952 * 953 * @i: integer to be converted to short 954 * 955 * Device register use 1 byte to store signed integer. 956 * This helper function convert the int in a signed short, 957 * using the min/max values that device can measure as limits. 958 * min/max values are defined by macro. 959 * 960 * Return: a short in the range of min/max value 961 */ 962 static inline s8 int_to_short(int i) 963 { 964 /* Prepare to cast to short by eliminating out of range values */ 965 i = clamp(i, MIN_TEMP, MAX_TEMP); 966 return (s8) i; 967 } 968 969 /* Interface Functions */ 970 971 static int w1_therm_add_slave(struct w1_slave *sl) 972 { 973 struct w1_therm_family_converter *sl_family_conv; 974 975 /* Allocate memory */ 976 sl->family_data = kzalloc(sizeof(struct w1_therm_family_data), 977 GFP_KERNEL); 978 if (!sl->family_data) 979 return -ENOMEM; 980 981 atomic_set(THERM_REFCNT(sl->family_data), 1); 982 983 /* Get a pointer to the device specific function struct */ 984 sl_family_conv = device_family(sl); 985 if (!sl_family_conv) { 986 kfree(sl->family_data); 987 return -ENODEV; 988 } 989 /* save this pointer to the device structure */ 990 SLAVE_SPECIFIC_FUNC(sl) = sl_family_conv; 991 992 if (bulk_read_support(sl)) { 993 /* 994 * add the sys entry to trigger bulk_read 995 * at master level only the 1st time 996 */ 997 if (!bulk_read_device_counter) { 998 int err = device_create_file(&sl->master->dev, 999 &dev_attr_therm_bulk_read); 1000 1001 if (err) 1002 dev_warn(&sl->dev, 1003 "%s: Device has been added, but bulk read is unavailable. err=%d\n", 1004 __func__, err); 1005 } 1006 /* Increment the counter */ 1007 bulk_read_device_counter++; 1008 } 1009 1010 /* Getting the power mode of the device {external, parasite} */ 1011 SLAVE_POWERMODE(sl) = read_powermode(sl); 1012 1013 if (SLAVE_POWERMODE(sl) < 0) { 1014 /* no error returned as device has been added */ 1015 dev_warn(&sl->dev, 1016 "%s: Device has been added, but power_mode may be corrupted. err=%d\n", 1017 __func__, SLAVE_POWERMODE(sl)); 1018 } 1019 1020 /* Getting the resolution of the device */ 1021 if (SLAVE_SPECIFIC_FUNC(sl)->get_resolution) { 1022 SLAVE_RESOLUTION(sl) = 1023 SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl); 1024 if (SLAVE_RESOLUTION(sl) < 0) { 1025 /* no error returned as device has been added */ 1026 dev_warn(&sl->dev, 1027 "%s:Device has been added, but resolution may be corrupted. err=%d\n", 1028 __func__, SLAVE_RESOLUTION(sl)); 1029 } 1030 } 1031 1032 /* Finally initialize convert_triggered flag */ 1033 SLAVE_CONVERT_TRIGGERED(sl) = 0; 1034 1035 return 0; 1036 } 1037 1038 static void w1_therm_remove_slave(struct w1_slave *sl) 1039 { 1040 int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data)); 1041 1042 if (bulk_read_support(sl)) { 1043 bulk_read_device_counter--; 1044 /* Delete the entry if no more device support the feature */ 1045 if (!bulk_read_device_counter) 1046 device_remove_file(&sl->master->dev, 1047 &dev_attr_therm_bulk_read); 1048 } 1049 1050 while (refcnt) { 1051 msleep(1000); 1052 refcnt = atomic_read(THERM_REFCNT(sl->family_data)); 1053 } 1054 kfree(sl->family_data); 1055 sl->family_data = NULL; 1056 } 1057 1058 /* Hardware Functions */ 1059 1060 /* Safe version of reset_select_slave - avoid using the one in w_io.c */ 1061 static int reset_select_slave(struct w1_slave *sl) 1062 { 1063 u8 match[9] = { W1_MATCH_ROM, }; 1064 u64 rn = le64_to_cpu(*((u64 *)&sl->reg_num)); 1065 1066 if (w1_reset_bus(sl->master)) 1067 return -ENODEV; 1068 1069 memcpy(&match[1], &rn, 8); 1070 w1_write_block(sl->master, match, 9); 1071 1072 return 0; 1073 } 1074 1075 /** 1076 * w1_poll_completion - Poll for operation completion, with timeout 1077 * @dev_master: the device master of the bus 1078 * @tout_ms: timeout in milliseconds 1079 * 1080 * The device is answering 0's while an operation is in progress and 1's after it completes 1081 * Timeout may happen if the previous command was not recognised due to a line noise 1082 * 1083 * Return: 0 - OK, negative error - timeout 1084 */ 1085 static int w1_poll_completion(struct w1_master *dev_master, int tout_ms) 1086 { 1087 int i; 1088 1089 for (i = 0; i < tout_ms/W1_POLL_PERIOD; i++) { 1090 /* Delay is before poll, for device to recognize a command */ 1091 msleep(W1_POLL_PERIOD); 1092 1093 /* Compare all 8 bits to mitigate a noise on the bus */ 1094 if (w1_read_8(dev_master) == 0xFF) 1095 break; 1096 } 1097 if (i == tout_ms/W1_POLL_PERIOD) 1098 return -EIO; 1099 1100 return 0; 1101 } 1102 1103 static int convert_t(struct w1_slave *sl, struct therm_info *info) 1104 { 1105 struct w1_master *dev_master = sl->master; 1106 int max_trying = W1_THERM_MAX_TRY; 1107 int t_conv; 1108 int ret = -ENODEV; 1109 bool strong_pullup; 1110 1111 if (!sl->family_data) 1112 goto error; 1113 1114 strong_pullup = (w1_strong_pullup == 2 || 1115 (!SLAVE_POWERMODE(sl) && 1116 w1_strong_pullup)); 1117 1118 if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) { 1119 dev_warn(&sl->dev, 1120 "%s: Disabling W1_THERM_POLL_COMPLETION in parasite power mode.\n", 1121 __func__); 1122 SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION; 1123 } 1124 1125 /* get conversion duration device and id dependent */ 1126 t_conv = conversion_time(sl); 1127 1128 memset(info->rom, 0, sizeof(info->rom)); 1129 1130 /* prevent the slave from going away in sleep */ 1131 atomic_inc(THERM_REFCNT(sl->family_data)); 1132 1133 if (!bus_mutex_lock(&dev_master->bus_mutex)) { 1134 ret = -EAGAIN; /* Didn't acquire the mutex */ 1135 goto dec_refcnt; 1136 } 1137 1138 while (max_trying-- && ret) { /* ret should be 0 */ 1139 1140 info->verdict = 0; 1141 info->crc = 0; 1142 /* safe version to select slave */ 1143 if (!reset_select_slave(sl)) { 1144 unsigned long sleep_rem; 1145 1146 /* 750ms strong pullup (or delay) after the convert */ 1147 if (strong_pullup) 1148 w1_next_pullup(dev_master, t_conv); 1149 1150 w1_write_8(dev_master, W1_CONVERT_TEMP); 1151 1152 if (SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) { 1153 ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP); 1154 if (ret) { 1155 dev_dbg(&sl->dev, "%s: Timeout\n", __func__); 1156 goto mt_unlock; 1157 } 1158 mutex_unlock(&dev_master->bus_mutex); 1159 } else if (!strong_pullup) { /*no device need pullup */ 1160 sleep_rem = msleep_interruptible(t_conv); 1161 if (sleep_rem != 0) { 1162 ret = -EINTR; 1163 goto mt_unlock; 1164 } 1165 mutex_unlock(&dev_master->bus_mutex); 1166 } else { /*some device need pullup */ 1167 mutex_unlock(&dev_master->bus_mutex); 1168 sleep_rem = msleep_interruptible(t_conv); 1169 if (sleep_rem != 0) { 1170 ret = -EINTR; 1171 goto dec_refcnt; 1172 } 1173 } 1174 ret = read_scratchpad(sl, info); 1175 1176 /* If enabled, check for conversion success */ 1177 if ((SLAVE_FEATURES(sl) & W1_THERM_CHECK_RESULT) && 1178 (info->rom[6] == 0xC) && 1179 ((info->rom[1] == 0x5 && info->rom[0] == 0x50) || 1180 (info->rom[1] == 0x7 && info->rom[0] == 0xFF)) 1181 ) { 1182 /* Invalid reading (scratchpad byte 6 = 0xC) 1183 * due to insufficient conversion time 1184 * or power failure. 1185 */ 1186 ret = -EIO; 1187 } 1188 1189 goto dec_refcnt; 1190 } 1191 1192 } 1193 1194 mt_unlock: 1195 mutex_unlock(&dev_master->bus_mutex); 1196 dec_refcnt: 1197 atomic_dec(THERM_REFCNT(sl->family_data)); 1198 error: 1199 return ret; 1200 } 1201 1202 static int conv_time_measure(struct w1_slave *sl, int *conv_time) 1203 { 1204 struct therm_info inf, 1205 *info = &inf; 1206 struct w1_master *dev_master = sl->master; 1207 int max_trying = W1_THERM_MAX_TRY; 1208 int ret = -ENODEV; 1209 bool strong_pullup; 1210 1211 if (!sl->family_data) 1212 goto error; 1213 1214 strong_pullup = (w1_strong_pullup == 2 || 1215 (!SLAVE_POWERMODE(sl) && 1216 w1_strong_pullup)); 1217 1218 if (strong_pullup) { 1219 pr_info("%s: Measure with strong_pullup is not supported.\n", __func__); 1220 return -EINVAL; 1221 } 1222 1223 memset(info->rom, 0, sizeof(info->rom)); 1224 1225 /* prevent the slave from going away in sleep */ 1226 atomic_inc(THERM_REFCNT(sl->family_data)); 1227 1228 if (!bus_mutex_lock(&dev_master->bus_mutex)) { 1229 ret = -EAGAIN; /* Didn't acquire the mutex */ 1230 goto dec_refcnt; 1231 } 1232 1233 while (max_trying-- && ret) { /* ret should be 0 */ 1234 info->verdict = 0; 1235 info->crc = 0; 1236 /* safe version to select slave */ 1237 if (!reset_select_slave(sl)) { 1238 int j_start, j_end; 1239 1240 /*no device need pullup */ 1241 w1_write_8(dev_master, W1_CONVERT_TEMP); 1242 1243 j_start = jiffies; 1244 ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP); 1245 if (ret) { 1246 dev_dbg(&sl->dev, "%s: Timeout\n", __func__); 1247 goto mt_unlock; 1248 } 1249 j_end = jiffies; 1250 /* 1.2x increase for variation and changes over temperature range */ 1251 *conv_time = jiffies_to_msecs(j_end-j_start)*12/10; 1252 pr_debug("W1 Measure complete, conv_time = %d, HZ=%d.\n", 1253 *conv_time, HZ); 1254 if (*conv_time <= CONV_TIME_MEASURE) { 1255 ret = -EIO; 1256 goto mt_unlock; 1257 } 1258 mutex_unlock(&dev_master->bus_mutex); 1259 ret = read_scratchpad(sl, info); 1260 goto dec_refcnt; 1261 } 1262 1263 } 1264 mt_unlock: 1265 mutex_unlock(&dev_master->bus_mutex); 1266 dec_refcnt: 1267 atomic_dec(THERM_REFCNT(sl->family_data)); 1268 error: 1269 return ret; 1270 } 1271 1272 static int read_scratchpad(struct w1_slave *sl, struct therm_info *info) 1273 { 1274 struct w1_master *dev_master = sl->master; 1275 int max_trying = W1_THERM_MAX_TRY; 1276 int ret = -ENODEV; 1277 1278 info->verdict = 0; 1279 1280 if (!sl->family_data) 1281 goto error; 1282 1283 memset(info->rom, 0, sizeof(info->rom)); 1284 1285 /* prevent the slave from going away in sleep */ 1286 atomic_inc(THERM_REFCNT(sl->family_data)); 1287 1288 if (!bus_mutex_lock(&dev_master->bus_mutex)) { 1289 ret = -EAGAIN; /* Didn't acquire the mutex */ 1290 goto dec_refcnt; 1291 } 1292 1293 while (max_trying-- && ret) { /* ret should be 0 */ 1294 /* safe version to select slave */ 1295 if (!reset_select_slave(sl)) { 1296 u8 nb_bytes_read; 1297 1298 w1_write_8(dev_master, W1_READ_SCRATCHPAD); 1299 1300 nb_bytes_read = w1_read_block(dev_master, info->rom, 9); 1301 if (nb_bytes_read != 9) { 1302 dev_warn(&sl->dev, 1303 "w1_read_block(): returned %u instead of 9.\n", 1304 nb_bytes_read); 1305 ret = -EIO; 1306 } 1307 1308 info->crc = w1_calc_crc8(info->rom, 8); 1309 1310 if (info->rom[8] == info->crc) { 1311 info->verdict = 1; 1312 ret = 0; 1313 } else 1314 ret = -EIO; /* CRC not checked */ 1315 } 1316 1317 } 1318 mutex_unlock(&dev_master->bus_mutex); 1319 1320 dec_refcnt: 1321 atomic_dec(THERM_REFCNT(sl->family_data)); 1322 error: 1323 return ret; 1324 } 1325 1326 static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes) 1327 { 1328 struct w1_master *dev_master = sl->master; 1329 int max_trying = W1_THERM_MAX_TRY; 1330 int ret = -ENODEV; 1331 1332 if (!sl->family_data) 1333 goto error; 1334 1335 /* prevent the slave from going away in sleep */ 1336 atomic_inc(THERM_REFCNT(sl->family_data)); 1337 1338 if (!bus_mutex_lock(&dev_master->bus_mutex)) { 1339 ret = -EAGAIN; /* Didn't acquire the mutex */ 1340 goto dec_refcnt; 1341 } 1342 1343 while (max_trying-- && ret) { /* ret should be 0 */ 1344 /* safe version to select slave */ 1345 if (!reset_select_slave(sl)) { 1346 w1_write_8(dev_master, W1_WRITE_SCRATCHPAD); 1347 w1_write_block(dev_master, data, nb_bytes); 1348 ret = 0; 1349 } 1350 } 1351 mutex_unlock(&dev_master->bus_mutex); 1352 1353 dec_refcnt: 1354 atomic_dec(THERM_REFCNT(sl->family_data)); 1355 error: 1356 return ret; 1357 } 1358 1359 static int copy_scratchpad(struct w1_slave *sl) 1360 { 1361 struct w1_master *dev_master = sl->master; 1362 int max_trying = W1_THERM_MAX_TRY; 1363 int t_write, ret = -ENODEV; 1364 bool strong_pullup; 1365 1366 if (!sl->family_data) 1367 goto error; 1368 1369 t_write = W1_THERM_EEPROM_WRITE_DELAY; 1370 strong_pullup = (w1_strong_pullup == 2 || 1371 (!SLAVE_POWERMODE(sl) && 1372 w1_strong_pullup)); 1373 1374 /* prevent the slave from going away in sleep */ 1375 atomic_inc(THERM_REFCNT(sl->family_data)); 1376 1377 if (!bus_mutex_lock(&dev_master->bus_mutex)) { 1378 ret = -EAGAIN; /* Didn't acquire the mutex */ 1379 goto dec_refcnt; 1380 } 1381 1382 while (max_trying-- && ret) { /* ret should be 0 */ 1383 /* safe version to select slave */ 1384 if (!reset_select_slave(sl)) { 1385 unsigned long sleep_rem; 1386 1387 /* 10ms strong pullup (or delay) after the convert */ 1388 if (strong_pullup) 1389 w1_next_pullup(dev_master, t_write); 1390 1391 w1_write_8(dev_master, W1_COPY_SCRATCHPAD); 1392 1393 if (strong_pullup) { 1394 sleep_rem = msleep_interruptible(t_write); 1395 if (sleep_rem != 0) { 1396 ret = -EINTR; 1397 goto mt_unlock; 1398 } 1399 } 1400 ret = 0; 1401 } 1402 1403 } 1404 1405 mt_unlock: 1406 mutex_unlock(&dev_master->bus_mutex); 1407 dec_refcnt: 1408 atomic_dec(THERM_REFCNT(sl->family_data)); 1409 error: 1410 return ret; 1411 } 1412 1413 static int recall_eeprom(struct w1_slave *sl) 1414 { 1415 struct w1_master *dev_master = sl->master; 1416 int max_trying = W1_THERM_MAX_TRY; 1417 int ret = -ENODEV; 1418 1419 if (!sl->family_data) 1420 goto error; 1421 1422 /* prevent the slave from going away in sleep */ 1423 atomic_inc(THERM_REFCNT(sl->family_data)); 1424 1425 if (!bus_mutex_lock(&dev_master->bus_mutex)) { 1426 ret = -EAGAIN; /* Didn't acquire the mutex */ 1427 goto dec_refcnt; 1428 } 1429 1430 while (max_trying-- && ret) { /* ret should be 0 */ 1431 /* safe version to select slave */ 1432 if (!reset_select_slave(sl)) { 1433 1434 w1_write_8(dev_master, W1_RECALL_EEPROM); 1435 ret = w1_poll_completion(dev_master, W1_POLL_RECALL_EEPROM); 1436 } 1437 1438 } 1439 1440 mutex_unlock(&dev_master->bus_mutex); 1441 1442 dec_refcnt: 1443 atomic_dec(THERM_REFCNT(sl->family_data)); 1444 error: 1445 return ret; 1446 } 1447 1448 static int read_powermode(struct w1_slave *sl) 1449 { 1450 struct w1_master *dev_master = sl->master; 1451 int max_trying = W1_THERM_MAX_TRY; 1452 int ret = -ENODEV; 1453 1454 if (!sl->family_data) 1455 goto error; 1456 1457 /* prevent the slave from going away in sleep */ 1458 atomic_inc(THERM_REFCNT(sl->family_data)); 1459 1460 if (!bus_mutex_lock(&dev_master->bus_mutex)) { 1461 ret = -EAGAIN; /* Didn't acquire the mutex */ 1462 goto dec_refcnt; 1463 } 1464 1465 while ((max_trying--) && (ret < 0)) { 1466 /* safe version to select slave */ 1467 if (!reset_select_slave(sl)) { 1468 w1_write_8(dev_master, W1_READ_PSUPPLY); 1469 /* 1470 * Emit a read time slot and read only one bit, 1471 * 1 is externally powered, 1472 * 0 is parasite powered 1473 */ 1474 ret = w1_touch_bit(dev_master, 1); 1475 /* ret should be either 1 either 0 */ 1476 } 1477 } 1478 mutex_unlock(&dev_master->bus_mutex); 1479 1480 dec_refcnt: 1481 atomic_dec(THERM_REFCNT(sl->family_data)); 1482 error: 1483 return ret; 1484 } 1485 1486 static int trigger_bulk_read(struct w1_master *dev_master) 1487 { 1488 struct w1_slave *sl = NULL; /* used to iterate through slaves */ 1489 int max_trying = W1_THERM_MAX_TRY; 1490 int t_conv = 0; 1491 int ret = -ENODEV; 1492 bool strong_pullup = false; 1493 1494 /* 1495 * Check whether there are parasite powered device on the bus, 1496 * and compute duration of conversion for these devices 1497 * so we can apply a strong pullup if required 1498 */ 1499 list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) { 1500 if (!sl->family_data) 1501 goto error; 1502 if (bulk_read_support(sl)) { 1503 int t_cur = conversion_time(sl); 1504 1505 t_conv = max(t_cur, t_conv); 1506 strong_pullup = strong_pullup || 1507 (w1_strong_pullup == 2 || 1508 (!SLAVE_POWERMODE(sl) && 1509 w1_strong_pullup)); 1510 } 1511 } 1512 1513 /* 1514 * t_conv is the max conversion time required on the bus 1515 * If its 0, no device support the bulk read feature 1516 */ 1517 if (!t_conv) 1518 goto error; 1519 1520 if (!bus_mutex_lock(&dev_master->bus_mutex)) { 1521 ret = -EAGAIN; /* Didn't acquire the mutex */ 1522 goto error; 1523 } 1524 1525 while ((max_trying--) && (ret < 0)) { /* ret should be either 0 */ 1526 1527 if (!w1_reset_bus(dev_master)) { /* Just reset the bus */ 1528 unsigned long sleep_rem; 1529 1530 w1_write_8(dev_master, W1_SKIP_ROM); 1531 1532 if (strong_pullup) /* Apply pullup if required */ 1533 w1_next_pullup(dev_master, t_conv); 1534 1535 w1_write_8(dev_master, W1_CONVERT_TEMP); 1536 1537 /* set a flag to instruct that converT pending */ 1538 list_for_each_entry(sl, 1539 &dev_master->slist, w1_slave_entry) { 1540 if (bulk_read_support(sl)) 1541 SLAVE_CONVERT_TRIGGERED(sl) = -1; 1542 } 1543 1544 if (strong_pullup) { /* some device need pullup */ 1545 sleep_rem = msleep_interruptible(t_conv); 1546 if (sleep_rem != 0) { 1547 ret = -EINTR; 1548 goto mt_unlock; 1549 } 1550 mutex_unlock(&dev_master->bus_mutex); 1551 } else { 1552 mutex_unlock(&dev_master->bus_mutex); 1553 sleep_rem = msleep_interruptible(t_conv); 1554 if (sleep_rem != 0) { 1555 ret = -EINTR; 1556 goto set_flag; 1557 } 1558 } 1559 ret = 0; 1560 goto set_flag; 1561 } 1562 } 1563 1564 mt_unlock: 1565 mutex_unlock(&dev_master->bus_mutex); 1566 set_flag: 1567 /* set a flag to register convsersion is done */ 1568 list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) { 1569 if (bulk_read_support(sl)) 1570 SLAVE_CONVERT_TRIGGERED(sl) = 1; 1571 } 1572 error: 1573 return ret; 1574 } 1575 1576 /* Sysfs Interface definition */ 1577 1578 static ssize_t w1_slave_show(struct device *device, 1579 struct device_attribute *attr, char *buf) 1580 { 1581 struct w1_slave *sl = dev_to_w1_slave(device); 1582 struct therm_info info; 1583 u8 *family_data = sl->family_data; 1584 int ret, i; 1585 ssize_t c = PAGE_SIZE; 1586 1587 if (bulk_read_support(sl)) { 1588 if (SLAVE_CONVERT_TRIGGERED(sl) < 0) { 1589 dev_dbg(device, 1590 "%s: Conversion in progress, retry later\n", 1591 __func__); 1592 return 0; 1593 } else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) { 1594 /* A bulk read has been issued, read the device RAM */ 1595 ret = read_scratchpad(sl, &info); 1596 SLAVE_CONVERT_TRIGGERED(sl) = 0; 1597 } else 1598 ret = convert_t(sl, &info); 1599 } else 1600 ret = convert_t(sl, &info); 1601 1602 if (ret < 0) { 1603 dev_dbg(device, 1604 "%s: Temperature data may be corrupted. err=%d\n", 1605 __func__, ret); 1606 return 0; 1607 } 1608 1609 for (i = 0; i < 9; ++i) 1610 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]); 1611 c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n", 1612 info.crc, (info.verdict) ? "YES" : "NO"); 1613 1614 if (info.verdict) 1615 memcpy(family_data, info.rom, sizeof(info.rom)); 1616 else 1617 dev_warn(device, "%s:Read failed CRC check\n", __func__); 1618 1619 for (i = 0; i < 9; ++i) 1620 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", 1621 ((u8 *)family_data)[i]); 1622 1623 c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n", 1624 temperature_from_RAM(sl, info.rom)); 1625 1626 ret = PAGE_SIZE - c; 1627 return ret; 1628 } 1629 1630 static ssize_t w1_slave_store(struct device *device, 1631 struct device_attribute *attr, const char *buf, 1632 size_t size) 1633 { 1634 int val, ret = 0; 1635 struct w1_slave *sl = dev_to_w1_slave(device); 1636 1637 ret = kstrtoint(buf, 10, &val); /* converting user entry to int */ 1638 1639 if (ret) { /* conversion error */ 1640 dev_info(device, 1641 "%s: conversion error. err= %d\n", __func__, ret); 1642 return size; /* return size to avoid call back again */ 1643 } 1644 1645 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 1646 dev_info(device, 1647 "%s: Device not supported by the driver\n", __func__); 1648 return size; /* No device family */ 1649 } 1650 1651 if (val == 0) /* val=0 : trigger a EEPROM save */ 1652 ret = copy_scratchpad(sl); 1653 else { 1654 if (SLAVE_SPECIFIC_FUNC(sl)->set_resolution) 1655 ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val); 1656 } 1657 1658 if (ret) { 1659 dev_warn(device, "%s: Set resolution - error %d\n", __func__, ret); 1660 /* Propagate error to userspace */ 1661 return ret; 1662 } 1663 SLAVE_RESOLUTION(sl) = val; 1664 /* Reset the conversion time to default - it depends on resolution */ 1665 SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT; 1666 1667 return size; /* always return size to avoid infinite calling */ 1668 } 1669 1670 static ssize_t temperature_show(struct device *device, 1671 struct device_attribute *attr, char *buf) 1672 { 1673 struct w1_slave *sl = dev_to_w1_slave(device); 1674 struct therm_info info; 1675 int ret = 0; 1676 1677 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 1678 dev_info(device, 1679 "%s: Device not supported by the driver\n", __func__); 1680 return 0; /* No device family */ 1681 } 1682 1683 if (bulk_read_support(sl)) { 1684 if (SLAVE_CONVERT_TRIGGERED(sl) < 0) { 1685 dev_dbg(device, 1686 "%s: Conversion in progress, retry later\n", 1687 __func__); 1688 return 0; 1689 } else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) { 1690 /* A bulk read has been issued, read the device RAM */ 1691 ret = read_scratchpad(sl, &info); 1692 SLAVE_CONVERT_TRIGGERED(sl) = 0; 1693 } else 1694 ret = convert_t(sl, &info); 1695 } else 1696 ret = convert_t(sl, &info); 1697 1698 if (ret < 0) { 1699 dev_dbg(device, 1700 "%s: Temperature data may be corrupted. err=%d\n", 1701 __func__, ret); 1702 return 0; 1703 } 1704 1705 return sprintf(buf, "%d\n", temperature_from_RAM(sl, info.rom)); 1706 } 1707 1708 static ssize_t ext_power_show(struct device *device, 1709 struct device_attribute *attr, char *buf) 1710 { 1711 struct w1_slave *sl = dev_to_w1_slave(device); 1712 1713 if (!sl->family_data) { 1714 dev_info(device, 1715 "%s: Device not supported by the driver\n", __func__); 1716 return 0; /* No device family */ 1717 } 1718 1719 /* Getting the power mode of the device {external, parasite} */ 1720 SLAVE_POWERMODE(sl) = read_powermode(sl); 1721 1722 if (SLAVE_POWERMODE(sl) < 0) { 1723 dev_dbg(device, 1724 "%s: Power_mode may be corrupted. err=%d\n", 1725 __func__, SLAVE_POWERMODE(sl)); 1726 } 1727 return sprintf(buf, "%d\n", SLAVE_POWERMODE(sl)); 1728 } 1729 1730 static ssize_t resolution_show(struct device *device, 1731 struct device_attribute *attr, char *buf) 1732 { 1733 struct w1_slave *sl = dev_to_w1_slave(device); 1734 1735 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 1736 dev_info(device, 1737 "%s: Device not supported by the driver\n", __func__); 1738 return 0; /* No device family */ 1739 } 1740 1741 /* get the correct function depending on the device */ 1742 SLAVE_RESOLUTION(sl) = SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl); 1743 if (SLAVE_RESOLUTION(sl) < 0) { 1744 dev_dbg(device, 1745 "%s: Resolution may be corrupted. err=%d\n", 1746 __func__, SLAVE_RESOLUTION(sl)); 1747 } 1748 1749 return sprintf(buf, "%d\n", SLAVE_RESOLUTION(sl)); 1750 } 1751 1752 static ssize_t resolution_store(struct device *device, 1753 struct device_attribute *attr, const char *buf, size_t size) 1754 { 1755 struct w1_slave *sl = dev_to_w1_slave(device); 1756 int val; 1757 int ret = 0; 1758 1759 ret = kstrtoint(buf, 10, &val); /* converting user entry to int */ 1760 1761 if (ret) { /* conversion error */ 1762 dev_info(device, 1763 "%s: conversion error. err= %d\n", __func__, ret); 1764 return size; /* return size to avoid call back again */ 1765 } 1766 1767 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 1768 dev_info(device, 1769 "%s: Device not supported by the driver\n", __func__); 1770 return size; /* No device family */ 1771 } 1772 1773 /* 1774 * Don't deal with the val enterd by user, 1775 * only device knows what is correct or not 1776 */ 1777 1778 /* get the correct function depending on the device */ 1779 ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val); 1780 1781 if (ret) 1782 return ret; 1783 1784 SLAVE_RESOLUTION(sl) = val; 1785 /* Reset the conversion time to default because it depends on resolution */ 1786 SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT; 1787 1788 return size; 1789 } 1790 1791 static ssize_t eeprom_cmd_store(struct device *device, 1792 struct device_attribute *attr, const char *buf, size_t size) 1793 { 1794 struct w1_slave *sl = dev_to_w1_slave(device); 1795 int ret = -EINVAL; /* Invalid argument */ 1796 1797 if (size == sizeof(EEPROM_CMD_WRITE)) { 1798 if (!strncmp(buf, EEPROM_CMD_WRITE, sizeof(EEPROM_CMD_WRITE)-1)) 1799 ret = copy_scratchpad(sl); 1800 } else if (size == sizeof(EEPROM_CMD_READ)) { 1801 if (!strncmp(buf, EEPROM_CMD_READ, sizeof(EEPROM_CMD_READ)-1)) 1802 ret = recall_eeprom(sl); 1803 } 1804 1805 if (ret) 1806 dev_info(device, "%s: error in process %d\n", __func__, ret); 1807 1808 return size; 1809 } 1810 1811 static ssize_t alarms_show(struct device *device, 1812 struct device_attribute *attr, char *buf) 1813 { 1814 struct w1_slave *sl = dev_to_w1_slave(device); 1815 int ret; 1816 s8 th = 0, tl = 0; 1817 struct therm_info scratchpad; 1818 1819 ret = read_scratchpad(sl, &scratchpad); 1820 1821 if (!ret) { 1822 th = scratchpad.rom[2]; /* TH is byte 2 */ 1823 tl = scratchpad.rom[3]; /* TL is byte 3 */ 1824 } else { 1825 dev_info(device, 1826 "%s: error reading alarms register %d\n", 1827 __func__, ret); 1828 } 1829 1830 return sprintf(buf, "%hd %hd\n", tl, th); 1831 } 1832 1833 static ssize_t alarms_store(struct device *device, 1834 struct device_attribute *attr, const char *buf, size_t size) 1835 { 1836 struct w1_slave *sl = dev_to_w1_slave(device); 1837 struct therm_info info; 1838 u8 new_config_register[3]; /* array of data to be written */ 1839 int temp, ret; 1840 char *token = NULL; 1841 s8 tl, th; /* 1 byte per value + temp ring order */ 1842 char *p_args, *orig; 1843 1844 p_args = orig = kmalloc(size, GFP_KERNEL); 1845 /* Safe string copys as buf is const */ 1846 if (!p_args) { 1847 dev_warn(device, 1848 "%s: error unable to allocate memory %d\n", 1849 __func__, -ENOMEM); 1850 return size; 1851 } 1852 strcpy(p_args, buf); 1853 1854 /* Split string using space char */ 1855 token = strsep(&p_args, " "); 1856 1857 if (!token) { 1858 dev_info(device, 1859 "%s: error parsing args %d\n", __func__, -EINVAL); 1860 goto free_m; 1861 } 1862 1863 /* Convert 1st entry to int */ 1864 ret = kstrtoint (token, 10, &temp); 1865 if (ret) { 1866 dev_info(device, 1867 "%s: error parsing args %d\n", __func__, ret); 1868 goto free_m; 1869 } 1870 1871 tl = int_to_short(temp); 1872 1873 /* Split string using space char */ 1874 token = strsep(&p_args, " "); 1875 if (!token) { 1876 dev_info(device, 1877 "%s: error parsing args %d\n", __func__, -EINVAL); 1878 goto free_m; 1879 } 1880 /* Convert 2nd entry to int */ 1881 ret = kstrtoint (token, 10, &temp); 1882 if (ret) { 1883 dev_info(device, 1884 "%s: error parsing args %d\n", __func__, ret); 1885 goto free_m; 1886 } 1887 1888 /* Prepare to cast to short by eliminating out of range values */ 1889 th = int_to_short(temp); 1890 1891 /* Reorder if required th and tl */ 1892 if (tl > th) 1893 swap(tl, th); 1894 1895 /* 1896 * Read the scratchpad to change only the required bits 1897 * (th : byte 2 - tl: byte 3) 1898 */ 1899 ret = read_scratchpad(sl, &info); 1900 if (!ret) { 1901 new_config_register[0] = th; /* Byte 2 */ 1902 new_config_register[1] = tl; /* Byte 3 */ 1903 new_config_register[2] = info.rom[4];/* Byte 4 */ 1904 } else { 1905 dev_info(device, 1906 "%s: error reading from the slave device %d\n", 1907 __func__, ret); 1908 goto free_m; 1909 } 1910 1911 /* Write data in the device RAM */ 1912 if (!SLAVE_SPECIFIC_FUNC(sl)) { 1913 dev_info(device, 1914 "%s: Device not supported by the driver %d\n", 1915 __func__, -ENODEV); 1916 goto free_m; 1917 } 1918 1919 ret = SLAVE_SPECIFIC_FUNC(sl)->write_data(sl, new_config_register); 1920 if (ret) 1921 dev_info(device, 1922 "%s: error writing to the slave device %d\n", 1923 __func__, ret); 1924 1925 free_m: 1926 /* free allocated memory */ 1927 kfree(orig); 1928 1929 return size; 1930 } 1931 1932 static ssize_t therm_bulk_read_store(struct device *device, 1933 struct device_attribute *attr, const char *buf, size_t size) 1934 { 1935 struct w1_master *dev_master = dev_to_w1_master(device); 1936 int ret = -EINVAL; /* Invalid argument */ 1937 1938 if (size == sizeof(BULK_TRIGGER_CMD)) 1939 if (!strncmp(buf, BULK_TRIGGER_CMD, 1940 sizeof(BULK_TRIGGER_CMD)-1)) 1941 ret = trigger_bulk_read(dev_master); 1942 1943 if (ret) 1944 dev_info(device, 1945 "%s: unable to trigger a bulk read on the bus. err=%d\n", 1946 __func__, ret); 1947 1948 return size; 1949 } 1950 1951 static ssize_t therm_bulk_read_show(struct device *device, 1952 struct device_attribute *attr, char *buf) 1953 { 1954 struct w1_master *dev_master = dev_to_w1_master(device); 1955 struct w1_slave *sl = NULL; 1956 int ret = 0; 1957 1958 list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) { 1959 if (sl->family_data) { 1960 if (bulk_read_support(sl)) { 1961 if (SLAVE_CONVERT_TRIGGERED(sl) == -1) { 1962 ret = -1; 1963 goto show_result; 1964 } 1965 if (SLAVE_CONVERT_TRIGGERED(sl) == 1) 1966 /* continue to check other slaves */ 1967 ret = 1; 1968 } 1969 } 1970 } 1971 show_result: 1972 return sprintf(buf, "%d\n", ret); 1973 } 1974 1975 static ssize_t conv_time_show(struct device *device, 1976 struct device_attribute *attr, char *buf) 1977 { 1978 struct w1_slave *sl = dev_to_w1_slave(device); 1979 1980 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 1981 dev_info(device, 1982 "%s: Device is not supported by the driver\n", __func__); 1983 return 0; /* No device family */ 1984 } 1985 return sprintf(buf, "%d\n", conversion_time(sl)); 1986 } 1987 1988 static ssize_t conv_time_store(struct device *device, 1989 struct device_attribute *attr, const char *buf, size_t size) 1990 { 1991 int val, ret = 0; 1992 struct w1_slave *sl = dev_to_w1_slave(device); 1993 1994 if (kstrtoint(buf, 10, &val)) /* converting user entry to int */ 1995 return -EINVAL; 1996 1997 if (check_family_data(sl)) 1998 return -ENODEV; 1999 2000 if (val != CONV_TIME_MEASURE) { 2001 if (val >= CONV_TIME_DEFAULT) 2002 SLAVE_CONV_TIME_OVERRIDE(sl) = val; 2003 else 2004 return -EINVAL; 2005 2006 } else { 2007 int conv_time; 2008 2009 ret = conv_time_measure(sl, &conv_time); 2010 if (ret) 2011 return -EIO; 2012 SLAVE_CONV_TIME_OVERRIDE(sl) = conv_time; 2013 } 2014 return size; 2015 } 2016 2017 static ssize_t features_show(struct device *device, 2018 struct device_attribute *attr, char *buf) 2019 { 2020 struct w1_slave *sl = dev_to_w1_slave(device); 2021 2022 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 2023 dev_info(device, 2024 "%s: Device not supported by the driver\n", __func__); 2025 return 0; /* No device family */ 2026 } 2027 return sprintf(buf, "%u\n", SLAVE_FEATURES(sl)); 2028 } 2029 2030 static ssize_t features_store(struct device *device, 2031 struct device_attribute *attr, const char *buf, size_t size) 2032 { 2033 int val, ret = 0; 2034 bool strong_pullup; 2035 struct w1_slave *sl = dev_to_w1_slave(device); 2036 2037 ret = kstrtouint(buf, 10, &val); /* converting user entry to int */ 2038 if (ret) 2039 return -EINVAL; /* invalid number */ 2040 2041 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) { 2042 dev_info(device, "%s: Device not supported by the driver\n", __func__); 2043 return -ENODEV; 2044 } 2045 2046 if ((val & W1_THERM_FEATURES_MASK) != val) 2047 return -EINVAL; 2048 2049 SLAVE_FEATURES(sl) = val; 2050 2051 strong_pullup = (w1_strong_pullup == 2 || 2052 (!SLAVE_POWERMODE(sl) && 2053 w1_strong_pullup)); 2054 2055 if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) { 2056 dev_warn(&sl->dev, 2057 "%s: W1_THERM_POLL_COMPLETION disabled in parasite power mode.\n", 2058 __func__); 2059 SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION; 2060 } 2061 2062 return size; 2063 } 2064 2065 #if IS_REACHABLE(CONFIG_HWMON) 2066 static int w1_read_temp(struct device *device, u32 attr, int channel, 2067 long *val) 2068 { 2069 struct w1_slave *sl = dev_get_drvdata(device); 2070 struct therm_info info; 2071 int ret; 2072 2073 switch (attr) { 2074 case hwmon_temp_input: 2075 ret = convert_t(sl, &info); 2076 if (ret) 2077 return ret; 2078 2079 if (!info.verdict) { 2080 ret = -EIO; 2081 return ret; 2082 } 2083 2084 *val = temperature_from_RAM(sl, info.rom); 2085 ret = 0; 2086 break; 2087 default: 2088 ret = -EOPNOTSUPP; 2089 break; 2090 } 2091 2092 return ret; 2093 } 2094 #endif 2095 2096 #define W1_42_CHAIN 0x99 2097 #define W1_42_CHAIN_OFF 0x3C 2098 #define W1_42_CHAIN_OFF_INV 0xC3 2099 #define W1_42_CHAIN_ON 0x5A 2100 #define W1_42_CHAIN_ON_INV 0xA5 2101 #define W1_42_CHAIN_DONE 0x96 2102 #define W1_42_CHAIN_DONE_INV 0x69 2103 #define W1_42_COND_READ 0x0F 2104 #define W1_42_SUCCESS_CONFIRM_BYTE 0xAA 2105 #define W1_42_FINISHED_BYTE 0xFF 2106 static ssize_t w1_seq_show(struct device *device, 2107 struct device_attribute *attr, char *buf) 2108 { 2109 struct w1_slave *sl = dev_to_w1_slave(device); 2110 ssize_t c = PAGE_SIZE; 2111 int i; 2112 u8 ack; 2113 u64 rn; 2114 struct w1_reg_num *reg_num; 2115 int seq = 0; 2116 2117 mutex_lock(&sl->master->bus_mutex); 2118 /* Place all devices in CHAIN state */ 2119 if (w1_reset_bus(sl->master)) 2120 goto error; 2121 w1_write_8(sl->master, W1_SKIP_ROM); 2122 w1_write_8(sl->master, W1_42_CHAIN); 2123 w1_write_8(sl->master, W1_42_CHAIN_ON); 2124 w1_write_8(sl->master, W1_42_CHAIN_ON_INV); 2125 msleep(sl->master->pullup_duration); 2126 2127 /* check for acknowledgment */ 2128 ack = w1_read_8(sl->master); 2129 if (ack != W1_42_SUCCESS_CONFIRM_BYTE) 2130 goto error; 2131 2132 /* In case the bus fails to send 0xFF, limit */ 2133 for (i = 0; i <= 64; i++) { 2134 if (w1_reset_bus(sl->master)) 2135 goto error; 2136 2137 w1_write_8(sl->master, W1_42_COND_READ); 2138 w1_read_block(sl->master, (u8 *)&rn, 8); 2139 reg_num = (struct w1_reg_num *) &rn; 2140 if (reg_num->family == W1_42_FINISHED_BYTE) 2141 break; 2142 if (sl->reg_num.id == reg_num->id) 2143 seq = i; 2144 2145 if (w1_reset_bus(sl->master)) 2146 goto error; 2147 2148 /* Put the device into chain DONE state */ 2149 w1_write_8(sl->master, W1_MATCH_ROM); 2150 w1_write_block(sl->master, (u8 *)&rn, 8); 2151 w1_write_8(sl->master, W1_42_CHAIN); 2152 w1_write_8(sl->master, W1_42_CHAIN_DONE); 2153 w1_write_8(sl->master, W1_42_CHAIN_DONE_INV); 2154 2155 /* check for acknowledgment */ 2156 ack = w1_read_8(sl->master); 2157 if (ack != W1_42_SUCCESS_CONFIRM_BYTE) 2158 goto error; 2159 } 2160 2161 /* Exit from CHAIN state */ 2162 if (w1_reset_bus(sl->master)) 2163 goto error; 2164 w1_write_8(sl->master, W1_SKIP_ROM); 2165 w1_write_8(sl->master, W1_42_CHAIN); 2166 w1_write_8(sl->master, W1_42_CHAIN_OFF); 2167 w1_write_8(sl->master, W1_42_CHAIN_OFF_INV); 2168 2169 /* check for acknowledgment */ 2170 ack = w1_read_8(sl->master); 2171 if (ack != W1_42_SUCCESS_CONFIRM_BYTE) 2172 goto error; 2173 mutex_unlock(&sl->master->bus_mutex); 2174 2175 c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq); 2176 return PAGE_SIZE - c; 2177 error: 2178 mutex_unlock(&sl->master->bus_mutex); 2179 return -EIO; 2180 } 2181 2182 static int __init w1_therm_init(void) 2183 { 2184 int err, i; 2185 2186 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) { 2187 err = w1_register_family(w1_therm_families[i].f); 2188 if (err) 2189 w1_therm_families[i].broken = 1; 2190 } 2191 2192 return 0; 2193 } 2194 2195 static void __exit w1_therm_fini(void) 2196 { 2197 int i; 2198 2199 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) 2200 if (!w1_therm_families[i].broken) 2201 w1_unregister_family(w1_therm_families[i].f); 2202 } 2203 2204 module_init(w1_therm_init); 2205 module_exit(w1_therm_fini); 2206 2207 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>"); 2208 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family."); 2209 MODULE_LICENSE("GPL"); 2210 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20)); 2211 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822)); 2212 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20)); 2213 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825)); 2214 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00)); 2215