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