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