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