1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
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
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.
40 * - strong_pullup = 0 Disable strong pullup completely
41 * - strong_pullup = 1 Enable automatic strong pullup detection
42 * - strong_pullup = 2 Force strong pullup
44 static int w1_strong_pullup = 1;
45 module_param_named(strong_pullup, w1_strong_pullup, int, 0);
47 /* Counter for devices supporting bulk reading */
48 static u16 bulk_read_device_counter; /* =0 as per C standard */
50 /* This command should be in public header w1.h but is not */
51 #define W1_RECALL_EEPROM 0xB8
53 /* Nb of try for an operation */
54 #define W1_THERM_MAX_TRY 5
56 /* ms delay to retry bus mutex */
57 #define W1_THERM_RETRY_DELAY 20
59 /* delay in ms to write in EEPROM */
60 #define W1_THERM_EEPROM_WRITE_DELAY 10
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 */
66 #define MIN_TEMP -55 /* min temperature that can be measured */
67 #define MAX_TEMP 125 /* max temperature that can be measured */
69 /* Allowed values for sysfs conv_time attribute */
70 #define CONV_TIME_DEFAULT 0
71 #define CONV_TIME_MEASURE 1
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 */
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*/
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
98 * return a pointer on the slave w1_therm_family_converter struct:
99 * always test family data existence before using this macro
101 #define SLAVE_SPECIFIC_FUNC(sl) \
102 (((struct w1_therm_family_data *)(sl->family_data))->specific_functions)
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
108 #define SLAVE_POWERMODE(sl) \
109 (((struct w1_therm_family_data *)(sl->family_data))->external_powered)
112 * return the resolution in bit of the sl slave : <0 unknown
113 * always test family data existence before using this macro
115 #define SLAVE_RESOLUTION(sl) \
116 (((struct w1_therm_family_data *)(sl->family_data))->resolution)
119 * return the conv_time_override of the sl slave
120 * always test family data existence before using this macro
122 #define SLAVE_CONV_TIME_OVERRIDE(sl) \
123 (((struct w1_therm_family_data *)(sl->family_data))->conv_time_override)
126 * return the features of the sl slave
127 * always test family data existence before using this macro
129 #define SLAVE_FEATURES(sl) \
130 (((struct w1_therm_family_data *)(sl->family_data))->features)
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
138 #define SLAVE_CONVERT_TRIGGERED(sl) \
139 (((struct w1_therm_family_data *)(sl->family_data))->convert_triggered)
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)
145 /* Structs definition */
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
159 struct w1_therm_family_converter {
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);
172 * struct w1_therm_family_data - device data
173 * @rom: ROM device id (64bit Lasered ROM code + 1 CRC byte)
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
184 struct w1_therm_family_data {
187 int external_powered;
189 int convert_triggered;
190 int conv_time_override;
191 unsigned int features;
192 struct w1_therm_family_converter *specific_functions;
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
207 /* Hardware Functions declaration */
210 * reset_select_slave() - reset and select a slave
211 * @sl: the slave to select
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.
219 * Context: The w1 master lock must be held.
221 * Return: 0 if success, negative kernel error code otherwise.
223 static int reset_select_slave(struct w1_slave *sl);
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
230 * Return: 0 if success, -kernel error code otherwise
232 static int convert_t(struct w1_slave *sl, struct therm_info *info);
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
239 * Return: 0 if success, -kernel error code otherwise
241 static int read_scratchpad(struct w1_slave *sl, struct therm_info *info);
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)
249 * Return: 0 if success, -kernel error code otherwise
251 static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes);
254 * copy_scratchpad() - Copy the content of scratchpad in device EEPROM
255 * @sl: slave involved
257 * Return: 0 if success, -kernel error code otherwise
259 static int copy_scratchpad(struct w1_slave *sl);
262 * recall_eeprom() - Restore EEPROM data to device RAM
263 * @sl: slave involved
265 * Return: 0 if success, -kernel error code otherwise
267 static int recall_eeprom(struct w1_slave *sl);
270 * read_powermode() - Query the power mode of the slave
271 * @sl: slave to retrieve the power mode
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.
277 * * 0 parasite powered device
278 * * 1 externally powered device
279 * * <0 kernel error code
281 static int read_powermode(struct w1_slave *sl);
284 * trigger_bulk_read() - function to trigger a bulk read on the bus
285 * @dev_master: the device master of the bus
287 * Send a SKIP ROM follow by a CONVERT T commmand 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.
291 * Return: 0 if success, -kernel error code otherwise
293 static int trigger_bulk_read(struct w1_master *dev_master);
295 /* Sysfs interface declaration */
297 static ssize_t w1_slave_show(struct device *device,
298 struct device_attribute *attr, char *buf);
300 static ssize_t w1_slave_store(struct device *device,
301 struct device_attribute *attr, const char *buf, size_t size);
303 static ssize_t w1_seq_show(struct device *device,
304 struct device_attribute *attr, char *buf);
306 static ssize_t temperature_show(struct device *device,
307 struct device_attribute *attr, char *buf);
309 static ssize_t ext_power_show(struct device *device,
310 struct device_attribute *attr, char *buf);
312 static ssize_t resolution_show(struct device *device,
313 struct device_attribute *attr, char *buf);
315 static ssize_t resolution_store(struct device *device,
316 struct device_attribute *attr, const char *buf, size_t size);
318 static ssize_t eeprom_cmd_store(struct device *device,
319 struct device_attribute *attr, const char *buf, size_t size);
321 static ssize_t alarms_store(struct device *device,
322 struct device_attribute *attr, const char *buf, size_t size);
324 static ssize_t alarms_show(struct device *device,
325 struct device_attribute *attr, char *buf);
327 static ssize_t therm_bulk_read_store(struct device *device,
328 struct device_attribute *attr, const char *buf, size_t size);
330 static ssize_t therm_bulk_read_show(struct device *device,
331 struct device_attribute *attr, char *buf);
333 static ssize_t conv_time_show(struct device *device,
334 struct device_attribute *attr, char *buf);
336 static ssize_t conv_time_store(struct device *device,
337 struct device_attribute *attr, const char *buf,
340 static ssize_t features_show(struct device *device,
341 struct device_attribute *attr, char *buf);
343 static ssize_t features_store(struct device *device,
344 struct device_attribute *attr, const char *buf,
346 /* Attributes declarations */
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);
358 static DEVICE_ATTR_RW(therm_bulk_read); /* attribut at master level */
360 /* Interface Functions declaration */
363 * w1_therm_add_slave() - Called when a new slave is discovered
364 * @sl: slave just discovered by the master.
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.
369 * Return: 0 - If success, negative kernel code otherwise
371 static int w1_therm_add_slave(struct w1_slave *sl);
374 * w1_therm_remove_slave() - Called when a slave is removed
375 * @sl: slave to be removed.
377 * Called by the master when the slave is considered not to be on the bus
378 * anymore. Used to free memory.
380 static void w1_therm_remove_slave(struct w1_slave *sl);
382 /* Family attributes */
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,
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,
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,
420 /* Attribute groups */
422 ATTRIBUTE_GROUPS(w1_therm);
423 ATTRIBUTE_GROUPS(w1_ds18s20);
424 ATTRIBUTE_GROUPS(w1_ds28ea00);
426 #if IS_REACHABLE(CONFIG_HWMON)
427 static int w1_read_temp(struct device *dev, u32 attr, int channel,
430 static umode_t w1_is_visible(const void *_data, enum hwmon_sensor_types type,
431 u32 attr, int channel)
433 return attr == hwmon_temp_input ? 0444 : 0;
436 static int w1_read(struct device *dev, enum hwmon_sensor_types type,
437 u32 attr, int channel, long *val)
441 return w1_read_temp(dev, attr, channel, val);
447 static const u32 w1_temp_config[] = {
452 static const struct hwmon_channel_info w1_temp = {
454 .config = w1_temp_config,
457 static const struct hwmon_channel_info *w1_info[] = {
462 static const struct hwmon_ops w1_hwmon_ops = {
463 .is_visible = w1_is_visible,
467 static const struct hwmon_chip_info w1_chip_info = {
468 .ops = &w1_hwmon_ops,
471 #define W1_CHIPINFO (&w1_chip_info)
473 #define W1_CHIPINFO NULL
476 /* Family operations */
478 static const struct w1_family_ops w1_therm_fops = {
479 .add_slave = w1_therm_add_slave,
480 .remove_slave = w1_therm_remove_slave,
481 .groups = w1_therm_groups,
482 .chip_info = W1_CHIPINFO,
485 static const struct w1_family_ops w1_ds18s20_fops = {
486 .add_slave = w1_therm_add_slave,
487 .remove_slave = w1_therm_remove_slave,
488 .groups = w1_ds18s20_groups,
489 .chip_info = W1_CHIPINFO,
492 static const struct w1_family_ops w1_ds28ea00_fops = {
493 .add_slave = w1_therm_add_slave,
494 .remove_slave = w1_therm_remove_slave,
495 .groups = w1_ds28ea00_groups,
496 .chip_info = W1_CHIPINFO,
499 /* Family binding operations struct */
501 static struct w1_family w1_therm_family_DS18S20 = {
502 .fid = W1_THERM_DS18S20,
503 .fops = &w1_ds18s20_fops,
506 static struct w1_family w1_therm_family_DS18B20 = {
507 .fid = W1_THERM_DS18B20,
508 .fops = &w1_therm_fops,
511 static struct w1_family w1_therm_family_DS1822 = {
512 .fid = W1_THERM_DS1822,
513 .fops = &w1_therm_fops,
516 static struct w1_family w1_therm_family_DS28EA00 = {
517 .fid = W1_THERM_DS28EA00,
518 .fops = &w1_ds28ea00_fops,
521 static struct w1_family w1_therm_family_DS1825 = {
522 .fid = W1_THERM_DS1825,
523 .fops = &w1_therm_fops,
526 /* Device dependent func */
528 static inline int w1_DS18B20_convert_time(struct w1_slave *sl)
532 if (!sl->family_data)
533 return -ENODEV; /* device unknown */
535 if (SLAVE_CONV_TIME_OVERRIDE(sl) != CONV_TIME_DEFAULT)
536 return SLAVE_CONV_TIME_OVERRIDE(sl);
538 /* Return the conversion time, depending on resolution,
539 * select maximum conversion time among all compatible devices
541 switch (SLAVE_RESOLUTION(sl)) {
555 ret = 850; /* GX20MH01 only. Datasheet says 500ms, but that's not enough. */
558 ret = 1600; /* GX20MH01 only. Datasheet says 1000ms - not enough */
566 static inline int w1_DS18S20_convert_time(struct w1_slave *sl)
568 if (!sl->family_data)
569 return -ENODEV; /* device unknown */
571 if (SLAVE_CONV_TIME_OVERRIDE(sl) == CONV_TIME_DEFAULT)
572 return 750; /* default for DS18S20 */
574 return SLAVE_CONV_TIME_OVERRIDE(sl);
577 static inline int w1_DS18B20_write_data(struct w1_slave *sl,
580 return write_scratchpad(sl, data, 3);
583 static inline int w1_DS18S20_write_data(struct w1_slave *sl,
586 /* No config register */
587 return write_scratchpad(sl, data, 2);
590 static inline int w1_DS18B20_set_resolution(struct w1_slave *sl, int val)
593 struct therm_info info, info2;
595 /* DS18B20 resolution is 9 to 12 bits */
596 /* GX20MH01 resolution is 9 to 14 bits */
597 if (val < W1_THERM_RESOLUTION_MIN || val > W1_THERM_RESOLUTION_MAX)
600 /* Calc bit value from resolution */
601 val = (val - W1_THERM_RESOLUTION_MIN) << W1_THERM_RESOLUTION_SHIFT;
604 * Read the scratchpad to change only the required bits
605 * (bit5 & bit 6 from byte 4)
607 ret = read_scratchpad(sl, &info);
613 info.rom[4] &= ~W1_THERM_RESOLUTION_MASK;
616 /* Write data in the device RAM */
617 ret = w1_DS18B20_write_data(sl, info.rom + 2);
621 /* Have to read back the resolution to verify an actual value
622 * GX20MH01 and DS18B20 are indistinguishable by family number, but resolutions differ
623 * Some DS18B20 clones don't support resolution change
625 ret = read_scratchpad(sl, &info2);
627 /* Scratchpad read fail */
630 if ((info2.rom[4] & W1_THERM_RESOLUTION_MASK) == (info.rom[4] & W1_THERM_RESOLUTION_MASK))
633 /* Resolution verify error */
637 static inline int w1_DS18B20_get_resolution(struct w1_slave *sl)
641 struct therm_info info;
643 ret = read_scratchpad(sl, &info);
648 resolution = ((info.rom[4] & W1_THERM_RESOLUTION_MASK) >> W1_THERM_RESOLUTION_SHIFT)
649 + W1_THERM_RESOLUTION_MIN;
650 /* GX20MH01 has one special case:
651 * >=14 means 14 bits when getting resolution from bit value.
652 * Other devices have no more then 12 bits.
654 if (resolution > W1_THERM_RESOLUTION_MAX)
655 resolution = W1_THERM_RESOLUTION_MAX;
661 * w1_DS18B20_convert_temp() - temperature computation for DS18B20
662 * @rom: data read from device RAM (8 data bytes + 1 CRC byte)
664 * Can be called for any DS18B20 compliant device.
666 * Return: value in millidegrees Celsius.
668 static inline int w1_DS18B20_convert_temp(u8 rom[9])
673 /* Signed 16-bit value to unsigned, cpu order */
674 bv = le16_to_cpup((__le16 *)rom);
676 /* Config register bit R2 = 1 - GX20MH01 in 13 or 14 bit resolution mode */
678 /* Insert two temperature bits from config register */
679 /* Avoid arithmetic shift of signed value */
680 bv = (bv << 2) | (rom[4] & 3);
681 t = (s16) bv; /* Degrees, lowest bit is 2^-6 */
682 return (int)t * 1000 / 64; /* Sign-extend to int; millidegrees */
684 t = (s16)bv; /* Degrees, lowest bit is 2^-4 */
685 return (int)t * 1000 / 16; /* Sign-extend to int; millidegrees */
689 * w1_DS18S20_convert_temp() - temperature computation for DS18S20
690 * @rom: data read from device RAM (8 data bytes + 1 CRC byte)
692 * Can be called for any DS18S20 compliant device.
694 * Return: value in millidegrees Celsius.
696 static inline int w1_DS18S20_convert_temp(u8 rom[9])
701 pr_debug("%s: Invalid argument for conversion\n", __func__);
706 t = ((s32)rom[0] >> 1)*1000;
708 t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
711 h = 1000*((s32)rom[7] - (s32)rom[6]);
718 /* Device capability description */
719 /* GX20MH01 device shares family number and structure with DS18B20 */
721 static struct w1_therm_family_converter w1_therm_families[] = {
723 .f = &w1_therm_family_DS18S20,
724 .convert = w1_DS18S20_convert_temp,
725 .get_conversion_time = w1_DS18S20_convert_time,
726 .set_resolution = NULL, /* no config register */
727 .get_resolution = NULL, /* no config register */
728 .write_data = w1_DS18S20_write_data,
732 .f = &w1_therm_family_DS1822,
733 .convert = w1_DS18B20_convert_temp,
734 .get_conversion_time = w1_DS18B20_convert_time,
735 .set_resolution = w1_DS18B20_set_resolution,
736 .get_resolution = w1_DS18B20_get_resolution,
737 .write_data = w1_DS18B20_write_data,
741 /* Also used for GX20MH01 */
742 .f = &w1_therm_family_DS18B20,
743 .convert = w1_DS18B20_convert_temp,
744 .get_conversion_time = w1_DS18B20_convert_time,
745 .set_resolution = w1_DS18B20_set_resolution,
746 .get_resolution = w1_DS18B20_get_resolution,
747 .write_data = w1_DS18B20_write_data,
751 .f = &w1_therm_family_DS28EA00,
752 .convert = w1_DS18B20_convert_temp,
753 .get_conversion_time = w1_DS18B20_convert_time,
754 .set_resolution = w1_DS18B20_set_resolution,
755 .get_resolution = w1_DS18B20_get_resolution,
756 .write_data = w1_DS18B20_write_data,
760 .f = &w1_therm_family_DS1825,
761 .convert = w1_DS18B20_convert_temp,
762 .get_conversion_time = w1_DS18B20_convert_time,
763 .set_resolution = w1_DS18B20_set_resolution,
764 .get_resolution = w1_DS18B20_get_resolution,
765 .write_data = w1_DS18B20_write_data,
770 /* Helpers Functions */
773 * device_family() - Retrieve a pointer on &struct w1_therm_family_converter
774 * @sl: slave to retrieve the device specific structure
776 * Return: pointer to the slaves's family converter, NULL if not known
778 static struct w1_therm_family_converter *device_family(struct w1_slave *sl)
780 struct w1_therm_family_converter *ret = NULL;
783 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
784 if (w1_therm_families[i].f->fid == sl->family->fid) {
785 ret = &w1_therm_families[i];
793 * bus_mutex_lock() - Acquire the mutex
794 * @lock: w1 bus mutex to acquire
796 * It try to acquire the mutex W1_THERM_MAX_TRY times and wait
797 * W1_THERM_RETRY_DELAY between 2 attempts.
799 * Return: true is mutex is acquired and lock, false otherwise
801 static inline bool bus_mutex_lock(struct mutex *lock)
803 int max_trying = W1_THERM_MAX_TRY;
805 /* try to acquire the mutex, if not, sleep retry_delay before retry) */
806 while (mutex_lock_interruptible(lock) != 0 && max_trying > 0) {
807 unsigned long sleep_rem;
809 sleep_rem = msleep_interruptible(W1_THERM_RETRY_DELAY);
815 return false; /* Didn't acquire the bus mutex */
821 * check_family_data() - Check if family data and specific functions are present
822 * @sl: W1 device data
824 * Return: 0 - OK, negative value - error
826 static int check_family_data(struct w1_slave *sl)
828 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
830 "%s: Device is not supported by the driver\n", __func__);
831 return -EINVAL; /* No device family */
837 * bulk_read_support() - check if slave support bulk read
838 * @sl: device to check the ability
840 * Return: true if bulk read is supported, false if not or error
842 static inline bool bulk_read_support(struct w1_slave *sl)
844 if (SLAVE_SPECIFIC_FUNC(sl))
845 return SLAVE_SPECIFIC_FUNC(sl)->bulk_read;
848 "%s: Device not supported by the driver\n", __func__);
850 return false; /* No device family */
854 * conversion_time() - get the Tconv for the slave
855 * @sl: device to get the conversion time
857 * On device supporting resolution settings, conversion time depend
858 * on the resolution setting. This helper function get the slave timing,
859 * depending on its current setting.
861 * Return: conversion time in ms, negative values are kernel error code
863 static inline int conversion_time(struct w1_slave *sl)
865 if (SLAVE_SPECIFIC_FUNC(sl))
866 return SLAVE_SPECIFIC_FUNC(sl)->get_conversion_time(sl);
869 "%s: Device not supported by the driver\n", __func__);
871 return -ENODEV; /* No device family */
875 * temperature_from_RAM() - Convert the read info to temperature
876 * @sl: device that sent the RAM data
877 * @rom: read value on the slave device RAM
879 * Device dependent, the function bind the correct computation method.
881 * Return: temperature in 1/1000degC, 0 on error.
883 static inline int temperature_from_RAM(struct w1_slave *sl, u8 rom[9])
885 if (SLAVE_SPECIFIC_FUNC(sl))
886 return SLAVE_SPECIFIC_FUNC(sl)->convert(rom);
889 "%s: Device not supported by the driver\n", __func__);
891 return 0; /* No device family */
895 * int_to_short() - Safe casting of int to short
897 * @i: integer to be converted to short
899 * Device register use 1 byte to store signed integer.
900 * This helper function convert the int in a signed short,
901 * using the min/max values that device can measure as limits.
902 * min/max values are defined by macro.
904 * Return: a short in the range of min/max value
906 static inline s8 int_to_short(int i)
908 /* Prepare to cast to short by eliminating out of range values */
909 i = clamp(i, MIN_TEMP, MAX_TEMP);
913 /* Interface Functions */
915 static int w1_therm_add_slave(struct w1_slave *sl)
917 struct w1_therm_family_converter *sl_family_conv;
919 /* Allocate memory */
920 sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
922 if (!sl->family_data)
925 atomic_set(THERM_REFCNT(sl->family_data), 1);
927 /* Get a pointer to the device specific function struct */
928 sl_family_conv = device_family(sl);
929 if (!sl_family_conv) {
930 kfree(sl->family_data);
933 /* save this pointer to the device structure */
934 SLAVE_SPECIFIC_FUNC(sl) = sl_family_conv;
936 if (bulk_read_support(sl)) {
938 * add the sys entry to trigger bulk_read
939 * at master level only the 1st time
941 if (!bulk_read_device_counter) {
942 int err = device_create_file(&sl->master->dev,
943 &dev_attr_therm_bulk_read);
947 "%s: Device has been added, but bulk read is unavailable. err=%d\n",
950 /* Increment the counter */
951 bulk_read_device_counter++;
954 /* Getting the power mode of the device {external, parasite} */
955 SLAVE_POWERMODE(sl) = read_powermode(sl);
957 if (SLAVE_POWERMODE(sl) < 0) {
958 /* no error returned as device has been added */
960 "%s: Device has been added, but power_mode may be corrupted. err=%d\n",
961 __func__, SLAVE_POWERMODE(sl));
964 /* Getting the resolution of the device */
965 if (SLAVE_SPECIFIC_FUNC(sl)->get_resolution) {
966 SLAVE_RESOLUTION(sl) =
967 SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl);
968 if (SLAVE_RESOLUTION(sl) < 0) {
969 /* no error returned as device has been added */
971 "%s:Device has been added, but resolution may be corrupted. err=%d\n",
972 __func__, SLAVE_RESOLUTION(sl));
976 /* Finally initialize convert_triggered flag */
977 SLAVE_CONVERT_TRIGGERED(sl) = 0;
982 static void w1_therm_remove_slave(struct w1_slave *sl)
984 int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
986 if (bulk_read_support(sl)) {
987 bulk_read_device_counter--;
988 /* Delete the entry if no more device support the feature */
989 if (!bulk_read_device_counter)
990 device_remove_file(&sl->master->dev,
991 &dev_attr_therm_bulk_read);
996 refcnt = atomic_read(THERM_REFCNT(sl->family_data));
998 kfree(sl->family_data);
999 sl->family_data = NULL;
1002 /* Hardware Functions */
1004 /* Safe version of reset_select_slave - avoid using the one in w_io.c */
1005 static int reset_select_slave(struct w1_slave *sl)
1007 u8 match[9] = { W1_MATCH_ROM, };
1008 u64 rn = le64_to_cpu(*((u64 *)&sl->reg_num));
1010 if (w1_reset_bus(sl->master))
1013 memcpy(&match[1], &rn, 8);
1014 w1_write_block(sl->master, match, 9);
1020 * w1_poll_completion - Poll for operation completion, with timeout
1021 * @dev_master: the device master of the bus
1022 * @tout_ms: timeout in milliseconds
1024 * The device is answering 0's while an operation is in progress and 1's after it completes
1025 * Timeout may happen if the previous command was not recognised due to a line noise
1027 * Return: 0 - OK, negative error - timeout
1029 static int w1_poll_completion(struct w1_master *dev_master, int tout_ms)
1033 for (i = 0; i < tout_ms/W1_POLL_PERIOD; i++) {
1034 /* Delay is before poll, for device to recognize a command */
1035 msleep(W1_POLL_PERIOD);
1037 /* Compare all 8 bits to mitigate a noise on the bus */
1038 if (w1_read_8(dev_master) == 0xFF)
1041 if (i == tout_ms/W1_POLL_PERIOD)
1047 static int convert_t(struct w1_slave *sl, struct therm_info *info)
1049 struct w1_master *dev_master = sl->master;
1050 int max_trying = W1_THERM_MAX_TRY;
1055 if (!sl->family_data)
1058 strong_pullup = (w1_strong_pullup == 2 ||
1059 (!SLAVE_POWERMODE(sl) &&
1062 if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) {
1064 "%s: Disabling W1_THERM_POLL_COMPLETION in parasite power mode.\n",
1066 SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION;
1069 /* get conversion duration device and id dependent */
1070 t_conv = conversion_time(sl);
1072 memset(info->rom, 0, sizeof(info->rom));
1074 /* prevent the slave from going away in sleep */
1075 atomic_inc(THERM_REFCNT(sl->family_data));
1077 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1078 ret = -EAGAIN; /* Didn't acquire the mutex */
1082 while (max_trying-- && ret) { /* ret should be 0 */
1086 /* safe version to select slave */
1087 if (!reset_select_slave(sl)) {
1088 unsigned long sleep_rem;
1090 /* 750ms strong pullup (or delay) after the convert */
1092 w1_next_pullup(dev_master, t_conv);
1094 w1_write_8(dev_master, W1_CONVERT_TEMP);
1096 if (strong_pullup) { /*some device need pullup */
1097 sleep_rem = msleep_interruptible(t_conv);
1098 if (sleep_rem != 0) {
1102 mutex_unlock(&dev_master->bus_mutex);
1103 } else { /*no device need pullup */
1104 if (SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) {
1105 ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP);
1107 dev_dbg(&sl->dev, "%s: Timeout\n", __func__);
1110 mutex_unlock(&dev_master->bus_mutex);
1113 mutex_unlock(&dev_master->bus_mutex);
1114 sleep_rem = msleep_interruptible(t_conv);
1115 if (sleep_rem != 0) {
1121 ret = read_scratchpad(sl, info);
1123 /* If enabled, check for conversion success */
1124 if ((SLAVE_FEATURES(sl) & W1_THERM_CHECK_RESULT) &&
1125 (info->rom[6] == 0xC) &&
1126 ((info->rom[1] == 0x5 && info->rom[0] == 0x50) ||
1127 (info->rom[1] == 0x7 && info->rom[0] == 0xFF))
1129 /* Invalid reading (scratchpad byte 6 = 0xC)
1130 * due to insufficient conversion time
1142 mutex_unlock(&dev_master->bus_mutex);
1144 atomic_dec(THERM_REFCNT(sl->family_data));
1149 static int conv_time_measure(struct w1_slave *sl, int *conv_time)
1151 struct therm_info inf,
1153 struct w1_master *dev_master = sl->master;
1154 int max_trying = W1_THERM_MAX_TRY;
1158 if (!sl->family_data)
1161 strong_pullup = (w1_strong_pullup == 2 ||
1162 (!SLAVE_POWERMODE(sl) &&
1165 if (strong_pullup) {
1166 pr_info("%s: Measure with strong_pullup is not supported.\n", __func__);
1170 memset(info->rom, 0, sizeof(info->rom));
1172 /* prevent the slave from going away in sleep */
1173 atomic_inc(THERM_REFCNT(sl->family_data));
1175 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1176 ret = -EAGAIN; /* Didn't acquire the mutex */
1180 while (max_trying-- && ret) { /* ret should be 0 */
1183 /* safe version to select slave */
1184 if (!reset_select_slave(sl)) {
1187 /*no device need pullup */
1188 w1_write_8(dev_master, W1_CONVERT_TEMP);
1191 ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP);
1193 dev_dbg(&sl->dev, "%s: Timeout\n", __func__);
1197 /* 1.2x increase for variation and changes over temperature range */
1198 *conv_time = jiffies_to_msecs(j_end-j_start)*12/10;
1199 pr_debug("W1 Measure complete, conv_time = %d, HZ=%d.\n",
1201 if (*conv_time <= CONV_TIME_MEASURE) {
1205 mutex_unlock(&dev_master->bus_mutex);
1206 ret = read_scratchpad(sl, info);
1212 mutex_unlock(&dev_master->bus_mutex);
1214 atomic_dec(THERM_REFCNT(sl->family_data));
1219 static int read_scratchpad(struct w1_slave *sl, struct therm_info *info)
1221 struct w1_master *dev_master = sl->master;
1222 int max_trying = W1_THERM_MAX_TRY;
1227 if (!sl->family_data)
1230 memset(info->rom, 0, sizeof(info->rom));
1232 /* prevent the slave from going away in sleep */
1233 atomic_inc(THERM_REFCNT(sl->family_data));
1235 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1236 ret = -EAGAIN; /* Didn't acquire the mutex */
1240 while (max_trying-- && ret) { /* ret should be 0 */
1241 /* safe version to select slave */
1242 if (!reset_select_slave(sl)) {
1245 w1_write_8(dev_master, W1_READ_SCRATCHPAD);
1247 nb_bytes_read = w1_read_block(dev_master, info->rom, 9);
1248 if (nb_bytes_read != 9) {
1250 "w1_read_block(): returned %u instead of 9.\n",
1255 info->crc = w1_calc_crc8(info->rom, 8);
1257 if (info->rom[8] == info->crc) {
1261 ret = -EIO; /* CRC not checked */
1265 mutex_unlock(&dev_master->bus_mutex);
1268 atomic_dec(THERM_REFCNT(sl->family_data));
1273 static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes)
1275 struct w1_master *dev_master = sl->master;
1276 int max_trying = W1_THERM_MAX_TRY;
1279 if (!sl->family_data)
1282 /* prevent the slave from going away in sleep */
1283 atomic_inc(THERM_REFCNT(sl->family_data));
1285 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1286 ret = -EAGAIN; /* Didn't acquire the mutex */
1290 while (max_trying-- && ret) { /* ret should be 0 */
1291 /* safe version to select slave */
1292 if (!reset_select_slave(sl)) {
1293 w1_write_8(dev_master, W1_WRITE_SCRATCHPAD);
1294 w1_write_block(dev_master, data, nb_bytes);
1298 mutex_unlock(&dev_master->bus_mutex);
1301 atomic_dec(THERM_REFCNT(sl->family_data));
1306 static int copy_scratchpad(struct w1_slave *sl)
1308 struct w1_master *dev_master = sl->master;
1309 int max_trying = W1_THERM_MAX_TRY;
1310 int t_write, ret = -ENODEV;
1313 if (!sl->family_data)
1316 t_write = W1_THERM_EEPROM_WRITE_DELAY;
1317 strong_pullup = (w1_strong_pullup == 2 ||
1318 (!SLAVE_POWERMODE(sl) &&
1321 /* prevent the slave from going away in sleep */
1322 atomic_inc(THERM_REFCNT(sl->family_data));
1324 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1325 ret = -EAGAIN; /* Didn't acquire the mutex */
1329 while (max_trying-- && ret) { /* ret should be 0 */
1330 /* safe version to select slave */
1331 if (!reset_select_slave(sl)) {
1332 unsigned long sleep_rem;
1334 /* 10ms strong pullup (or delay) after the convert */
1336 w1_next_pullup(dev_master, t_write);
1338 w1_write_8(dev_master, W1_COPY_SCRATCHPAD);
1340 if (strong_pullup) {
1341 sleep_rem = msleep_interruptible(t_write);
1342 if (sleep_rem != 0) {
1353 mutex_unlock(&dev_master->bus_mutex);
1355 atomic_dec(THERM_REFCNT(sl->family_data));
1360 static int recall_eeprom(struct w1_slave *sl)
1362 struct w1_master *dev_master = sl->master;
1363 int max_trying = W1_THERM_MAX_TRY;
1366 if (!sl->family_data)
1369 /* prevent the slave from going away in sleep */
1370 atomic_inc(THERM_REFCNT(sl->family_data));
1372 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1373 ret = -EAGAIN; /* Didn't acquire the mutex */
1377 while (max_trying-- && ret) { /* ret should be 0 */
1378 /* safe version to select slave */
1379 if (!reset_select_slave(sl)) {
1381 w1_write_8(dev_master, W1_RECALL_EEPROM);
1382 ret = w1_poll_completion(dev_master, W1_POLL_RECALL_EEPROM);
1387 mutex_unlock(&dev_master->bus_mutex);
1390 atomic_dec(THERM_REFCNT(sl->family_data));
1395 static int read_powermode(struct w1_slave *sl)
1397 struct w1_master *dev_master = sl->master;
1398 int max_trying = W1_THERM_MAX_TRY;
1401 if (!sl->family_data)
1404 /* prevent the slave from going away in sleep */
1405 atomic_inc(THERM_REFCNT(sl->family_data));
1407 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1408 ret = -EAGAIN; /* Didn't acquire the mutex */
1412 while ((max_trying--) && (ret < 0)) {
1413 /* safe version to select slave */
1414 if (!reset_select_slave(sl)) {
1415 w1_write_8(dev_master, W1_READ_PSUPPLY);
1417 * Emit a read time slot and read only one bit,
1418 * 1 is externally powered,
1419 * 0 is parasite powered
1421 ret = w1_touch_bit(dev_master, 1);
1422 /* ret should be either 1 either 0 */
1425 mutex_unlock(&dev_master->bus_mutex);
1428 atomic_dec(THERM_REFCNT(sl->family_data));
1433 static int trigger_bulk_read(struct w1_master *dev_master)
1435 struct w1_slave *sl = NULL; /* used to iterate through slaves */
1436 int max_trying = W1_THERM_MAX_TRY;
1439 bool strong_pullup = false;
1442 * Check whether there are parasite powered device on the bus,
1443 * and compute duration of conversion for these devices
1444 * so we can apply a strong pullup if required
1446 list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
1447 if (!sl->family_data)
1449 if (bulk_read_support(sl)) {
1450 int t_cur = conversion_time(sl);
1452 t_conv = t_cur > t_conv ? t_cur : t_conv;
1453 strong_pullup = strong_pullup ||
1454 (w1_strong_pullup == 2 ||
1455 (!SLAVE_POWERMODE(sl) &&
1461 * t_conv is the max conversion time required on the bus
1462 * If its 0, no device support the bulk read feature
1467 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1468 ret = -EAGAIN; /* Didn't acquire the mutex */
1472 while ((max_trying--) && (ret < 0)) { /* ret should be either 0 */
1474 if (!w1_reset_bus(dev_master)) { /* Just reset the bus */
1475 unsigned long sleep_rem;
1477 w1_write_8(dev_master, W1_SKIP_ROM);
1479 if (strong_pullup) /* Apply pullup if required */
1480 w1_next_pullup(dev_master, t_conv);
1482 w1_write_8(dev_master, W1_CONVERT_TEMP);
1484 /* set a flag to instruct that converT pending */
1485 list_for_each_entry(sl,
1486 &dev_master->slist, w1_slave_entry) {
1487 if (bulk_read_support(sl))
1488 SLAVE_CONVERT_TRIGGERED(sl) = -1;
1491 if (strong_pullup) { /* some device need pullup */
1492 sleep_rem = msleep_interruptible(t_conv);
1493 if (sleep_rem != 0) {
1497 mutex_unlock(&dev_master->bus_mutex);
1499 mutex_unlock(&dev_master->bus_mutex);
1500 sleep_rem = msleep_interruptible(t_conv);
1501 if (sleep_rem != 0) {
1512 mutex_unlock(&dev_master->bus_mutex);
1514 /* set a flag to register convsersion is done */
1515 list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
1516 if (bulk_read_support(sl))
1517 SLAVE_CONVERT_TRIGGERED(sl) = 1;
1523 /* Sysfs Interface definition */
1525 static ssize_t w1_slave_show(struct device *device,
1526 struct device_attribute *attr, char *buf)
1528 struct w1_slave *sl = dev_to_w1_slave(device);
1529 struct therm_info info;
1530 u8 *family_data = sl->family_data;
1532 ssize_t c = PAGE_SIZE;
1534 if (bulk_read_support(sl)) {
1535 if (SLAVE_CONVERT_TRIGGERED(sl) < 0) {
1537 "%s: Conversion in progress, retry later\n",
1540 } else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) {
1541 /* A bulk read has been issued, read the device RAM */
1542 ret = read_scratchpad(sl, &info);
1543 SLAVE_CONVERT_TRIGGERED(sl) = 0;
1545 ret = convert_t(sl, &info);
1547 ret = convert_t(sl, &info);
1551 "%s: Temperature data may be corrupted. err=%d\n",
1556 for (i = 0; i < 9; ++i)
1557 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]);
1558 c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
1559 info.crc, (info.verdict) ? "YES" : "NO");
1562 memcpy(family_data, info.rom, sizeof(info.rom));
1564 dev_warn(device, "%s:Read failed CRC check\n", __func__);
1566 for (i = 0; i < 9; ++i)
1567 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
1568 ((u8 *)family_data)[i]);
1570 c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
1571 temperature_from_RAM(sl, info.rom));
1573 ret = PAGE_SIZE - c;
1577 static ssize_t w1_slave_store(struct device *device,
1578 struct device_attribute *attr, const char *buf,
1582 struct w1_slave *sl = dev_to_w1_slave(device);
1584 ret = kstrtoint(buf, 10, &val); /* converting user entry to int */
1586 if (ret) { /* conversion error */
1588 "%s: conversion error. err= %d\n", __func__, ret);
1589 return size; /* return size to avoid call back again */
1592 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1594 "%s: Device not supported by the driver\n", __func__);
1595 return size; /* No device family */
1598 if (val == 0) /* val=0 : trigger a EEPROM save */
1599 ret = copy_scratchpad(sl);
1601 if (SLAVE_SPECIFIC_FUNC(sl)->set_resolution)
1602 ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val);
1606 dev_warn(device, "%s: Set resolution - error %d\n", __func__, ret);
1607 /* Propagate error to userspace */
1610 SLAVE_RESOLUTION(sl) = val;
1611 /* Reset the conversion time to default - it depends on resolution */
1612 SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT;
1614 return size; /* always return size to avoid infinite calling */
1617 static ssize_t temperature_show(struct device *device,
1618 struct device_attribute *attr, char *buf)
1620 struct w1_slave *sl = dev_to_w1_slave(device);
1621 struct therm_info info;
1624 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1626 "%s: Device not supported by the driver\n", __func__);
1627 return 0; /* No device family */
1630 if (bulk_read_support(sl)) {
1631 if (SLAVE_CONVERT_TRIGGERED(sl) < 0) {
1633 "%s: Conversion in progress, retry later\n",
1636 } else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) {
1637 /* A bulk read has been issued, read the device RAM */
1638 ret = read_scratchpad(sl, &info);
1639 SLAVE_CONVERT_TRIGGERED(sl) = 0;
1641 ret = convert_t(sl, &info);
1643 ret = convert_t(sl, &info);
1647 "%s: Temperature data may be corrupted. err=%d\n",
1652 return sprintf(buf, "%d\n", temperature_from_RAM(sl, info.rom));
1655 static ssize_t ext_power_show(struct device *device,
1656 struct device_attribute *attr, char *buf)
1658 struct w1_slave *sl = dev_to_w1_slave(device);
1660 if (!sl->family_data) {
1662 "%s: Device not supported by the driver\n", __func__);
1663 return 0; /* No device family */
1666 /* Getting the power mode of the device {external, parasite} */
1667 SLAVE_POWERMODE(sl) = read_powermode(sl);
1669 if (SLAVE_POWERMODE(sl) < 0) {
1671 "%s: Power_mode may be corrupted. err=%d\n",
1672 __func__, SLAVE_POWERMODE(sl));
1674 return sprintf(buf, "%d\n", SLAVE_POWERMODE(sl));
1677 static ssize_t resolution_show(struct device *device,
1678 struct device_attribute *attr, char *buf)
1680 struct w1_slave *sl = dev_to_w1_slave(device);
1682 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1684 "%s: Device not supported by the driver\n", __func__);
1685 return 0; /* No device family */
1688 /* get the correct function depending on the device */
1689 SLAVE_RESOLUTION(sl) = SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl);
1690 if (SLAVE_RESOLUTION(sl) < 0) {
1692 "%s: Resolution may be corrupted. err=%d\n",
1693 __func__, SLAVE_RESOLUTION(sl));
1696 return sprintf(buf, "%d\n", SLAVE_RESOLUTION(sl));
1699 static ssize_t resolution_store(struct device *device,
1700 struct device_attribute *attr, const char *buf, size_t size)
1702 struct w1_slave *sl = dev_to_w1_slave(device);
1706 ret = kstrtoint(buf, 10, &val); /* converting user entry to int */
1708 if (ret) { /* conversion error */
1710 "%s: conversion error. err= %d\n", __func__, ret);
1711 return size; /* return size to avoid call back again */
1714 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1716 "%s: Device not supported by the driver\n", __func__);
1717 return size; /* No device family */
1721 * Don't deal with the val enterd by user,
1722 * only device knows what is correct or not
1725 /* get the correct function depending on the device */
1726 ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val);
1731 SLAVE_RESOLUTION(sl) = val;
1732 /* Reset the conversion time to default because it depends on resolution */
1733 SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT;
1738 static ssize_t eeprom_cmd_store(struct device *device,
1739 struct device_attribute *attr, const char *buf, size_t size)
1741 struct w1_slave *sl = dev_to_w1_slave(device);
1742 int ret = -EINVAL; /* Invalid argument */
1744 if (size == sizeof(EEPROM_CMD_WRITE)) {
1745 if (!strncmp(buf, EEPROM_CMD_WRITE, sizeof(EEPROM_CMD_WRITE)-1))
1746 ret = copy_scratchpad(sl);
1747 } else if (size == sizeof(EEPROM_CMD_READ)) {
1748 if (!strncmp(buf, EEPROM_CMD_READ, sizeof(EEPROM_CMD_READ)-1))
1749 ret = recall_eeprom(sl);
1753 dev_info(device, "%s: error in process %d\n", __func__, ret);
1758 static ssize_t alarms_show(struct device *device,
1759 struct device_attribute *attr, char *buf)
1761 struct w1_slave *sl = dev_to_w1_slave(device);
1764 struct therm_info scratchpad;
1766 ret = read_scratchpad(sl, &scratchpad);
1769 th = scratchpad.rom[2]; /* TH is byte 2 */
1770 tl = scratchpad.rom[3]; /* TL is byte 3 */
1773 "%s: error reading alarms register %d\n",
1777 return sprintf(buf, "%hd %hd\n", tl, th);
1780 static ssize_t alarms_store(struct device *device,
1781 struct device_attribute *attr, const char *buf, size_t size)
1783 struct w1_slave *sl = dev_to_w1_slave(device);
1784 struct therm_info info;
1785 u8 new_config_register[3]; /* array of data to be written */
1788 s8 tl, th, tt; /* 1 byte per value + temp ring order */
1789 char *p_args, *orig;
1791 p_args = orig = kmalloc(size, GFP_KERNEL);
1792 /* Safe string copys as buf is const */
1795 "%s: error unable to allocate memory %d\n",
1799 strcpy(p_args, buf);
1801 /* Split string using space char */
1802 token = strsep(&p_args, " ");
1806 "%s: error parsing args %d\n", __func__, -EINVAL);
1810 /* Convert 1st entry to int */
1811 ret = kstrtoint (token, 10, &temp);
1814 "%s: error parsing args %d\n", __func__, ret);
1818 tl = int_to_short(temp);
1820 /* Split string using space char */
1821 token = strsep(&p_args, " ");
1824 "%s: error parsing args %d\n", __func__, -EINVAL);
1827 /* Convert 2nd entry to int */
1828 ret = kstrtoint (token, 10, &temp);
1831 "%s: error parsing args %d\n", __func__, ret);
1835 /* Prepare to cast to short by eliminating out of range values */
1836 th = int_to_short(temp);
1838 /* Reorder if required th and tl */
1840 tt = tl; tl = th; th = tt;
1844 * Read the scratchpad to change only the required bits
1845 * (th : byte 2 - tl: byte 3)
1847 ret = read_scratchpad(sl, &info);
1849 new_config_register[0] = th; /* Byte 2 */
1850 new_config_register[1] = tl; /* Byte 3 */
1851 new_config_register[2] = info.rom[4];/* Byte 4 */
1854 "%s: error reading from the slave device %d\n",
1859 /* Write data in the device RAM */
1860 if (!SLAVE_SPECIFIC_FUNC(sl)) {
1862 "%s: Device not supported by the driver %d\n",
1867 ret = SLAVE_SPECIFIC_FUNC(sl)->write_data(sl, new_config_register);
1870 "%s: error writing to the slave device %d\n",
1874 /* free allocated memory */
1880 static ssize_t therm_bulk_read_store(struct device *device,
1881 struct device_attribute *attr, const char *buf, size_t size)
1883 struct w1_master *dev_master = dev_to_w1_master(device);
1884 int ret = -EINVAL; /* Invalid argument */
1886 if (size == sizeof(BULK_TRIGGER_CMD))
1887 if (!strncmp(buf, BULK_TRIGGER_CMD,
1888 sizeof(BULK_TRIGGER_CMD)-1))
1889 ret = trigger_bulk_read(dev_master);
1893 "%s: unable to trigger a bulk read on the bus. err=%d\n",
1899 static ssize_t therm_bulk_read_show(struct device *device,
1900 struct device_attribute *attr, char *buf)
1902 struct w1_master *dev_master = dev_to_w1_master(device);
1903 struct w1_slave *sl = NULL;
1906 list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
1907 if (sl->family_data) {
1908 if (bulk_read_support(sl)) {
1909 if (SLAVE_CONVERT_TRIGGERED(sl) == -1) {
1913 if (SLAVE_CONVERT_TRIGGERED(sl) == 1)
1914 /* continue to check other slaves */
1920 return sprintf(buf, "%d\n", ret);
1923 static ssize_t conv_time_show(struct device *device,
1924 struct device_attribute *attr, char *buf)
1926 struct w1_slave *sl = dev_to_w1_slave(device);
1928 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1930 "%s: Device is not supported by the driver\n", __func__);
1931 return 0; /* No device family */
1933 return sprintf(buf, "%d\n", conversion_time(sl));
1936 static ssize_t conv_time_store(struct device *device,
1937 struct device_attribute *attr, const char *buf, size_t size)
1940 struct w1_slave *sl = dev_to_w1_slave(device);
1942 if (kstrtoint(buf, 10, &val)) /* converting user entry to int */
1945 if (check_family_data(sl))
1948 if (val != CONV_TIME_MEASURE) {
1949 if (val >= CONV_TIME_DEFAULT)
1950 SLAVE_CONV_TIME_OVERRIDE(sl) = val;
1957 ret = conv_time_measure(sl, &conv_time);
1960 SLAVE_CONV_TIME_OVERRIDE(sl) = conv_time;
1965 static ssize_t features_show(struct device *device,
1966 struct device_attribute *attr, char *buf)
1968 struct w1_slave *sl = dev_to_w1_slave(device);
1970 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1972 "%s: Device not supported by the driver\n", __func__);
1973 return 0; /* No device family */
1975 return sprintf(buf, "%u\n", SLAVE_FEATURES(sl));
1978 static ssize_t features_store(struct device *device,
1979 struct device_attribute *attr, const char *buf, size_t size)
1983 struct w1_slave *sl = dev_to_w1_slave(device);
1985 ret = kstrtouint(buf, 10, &val); /* converting user entry to int */
1987 return -EINVAL; /* invalid number */
1989 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1990 dev_info(device, "%s: Device not supported by the driver\n", __func__);
1994 if ((val & W1_THERM_FEATURES_MASK) != val)
1997 SLAVE_FEATURES(sl) = val;
1999 strong_pullup = (w1_strong_pullup == 2 ||
2000 (!SLAVE_POWERMODE(sl) &&
2003 if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) {
2005 "%s: W1_THERM_POLL_COMPLETION disabled in parasite power mode.\n",
2007 SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION;
2013 #if IS_REACHABLE(CONFIG_HWMON)
2014 static int w1_read_temp(struct device *device, u32 attr, int channel,
2017 struct w1_slave *sl = dev_get_drvdata(device);
2018 struct therm_info info;
2022 case hwmon_temp_input:
2023 ret = convert_t(sl, &info);
2027 if (!info.verdict) {
2032 *val = temperature_from_RAM(sl, info.rom);
2044 #define W1_42_CHAIN 0x99
2045 #define W1_42_CHAIN_OFF 0x3C
2046 #define W1_42_CHAIN_OFF_INV 0xC3
2047 #define W1_42_CHAIN_ON 0x5A
2048 #define W1_42_CHAIN_ON_INV 0xA5
2049 #define W1_42_CHAIN_DONE 0x96
2050 #define W1_42_CHAIN_DONE_INV 0x69
2051 #define W1_42_COND_READ 0x0F
2052 #define W1_42_SUCCESS_CONFIRM_BYTE 0xAA
2053 #define W1_42_FINISHED_BYTE 0xFF
2054 static ssize_t w1_seq_show(struct device *device,
2055 struct device_attribute *attr, char *buf)
2057 struct w1_slave *sl = dev_to_w1_slave(device);
2058 ssize_t c = PAGE_SIZE;
2062 struct w1_reg_num *reg_num;
2065 mutex_lock(&sl->master->bus_mutex);
2066 /* Place all devices in CHAIN state */
2067 if (w1_reset_bus(sl->master))
2069 w1_write_8(sl->master, W1_SKIP_ROM);
2070 w1_write_8(sl->master, W1_42_CHAIN);
2071 w1_write_8(sl->master, W1_42_CHAIN_ON);
2072 w1_write_8(sl->master, W1_42_CHAIN_ON_INV);
2073 msleep(sl->master->pullup_duration);
2075 /* check for acknowledgment */
2076 ack = w1_read_8(sl->master);
2077 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
2080 /* In case the bus fails to send 0xFF, limit */
2081 for (i = 0; i <= 64; i++) {
2082 if (w1_reset_bus(sl->master))
2085 w1_write_8(sl->master, W1_42_COND_READ);
2086 w1_read_block(sl->master, (u8 *)&rn, 8);
2087 reg_num = (struct w1_reg_num *) &rn;
2088 if (reg_num->family == W1_42_FINISHED_BYTE)
2090 if (sl->reg_num.id == reg_num->id)
2093 w1_write_8(sl->master, W1_42_CHAIN);
2094 w1_write_8(sl->master, W1_42_CHAIN_DONE);
2095 w1_write_8(sl->master, W1_42_CHAIN_DONE_INV);
2096 w1_read_block(sl->master, &ack, sizeof(ack));
2098 /* check for acknowledgment */
2099 ack = w1_read_8(sl->master);
2100 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
2105 /* Exit from CHAIN state */
2106 if (w1_reset_bus(sl->master))
2108 w1_write_8(sl->master, W1_SKIP_ROM);
2109 w1_write_8(sl->master, W1_42_CHAIN);
2110 w1_write_8(sl->master, W1_42_CHAIN_OFF);
2111 w1_write_8(sl->master, W1_42_CHAIN_OFF_INV);
2113 /* check for acknowledgment */
2114 ack = w1_read_8(sl->master);
2115 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
2117 mutex_unlock(&sl->master->bus_mutex);
2119 c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq);
2120 return PAGE_SIZE - c;
2122 mutex_unlock(&sl->master->bus_mutex);
2126 static int __init w1_therm_init(void)
2130 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
2131 err = w1_register_family(w1_therm_families[i].f);
2133 w1_therm_families[i].broken = 1;
2139 static void __exit w1_therm_fini(void)
2143 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
2144 if (!w1_therm_families[i].broken)
2145 w1_unregister_family(w1_therm_families[i].f);
2148 module_init(w1_therm_init);
2149 module_exit(w1_therm_fini);
2152 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
2153 MODULE_LICENSE("GPL");
2154 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20));
2155 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822));
2156 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20));
2157 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825));
2158 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00));