• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 
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 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 
w1_DS18B20_convert_time(struct w1_slave * sl)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 
w1_DS18S20_convert_time(struct w1_slave * sl)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 
w1_DS18B20_write_data(struct w1_slave * sl,const u8 * data)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 
w1_DS18S20_write_data(struct w1_slave * sl,const u8 * data)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 
w1_DS18B20_set_resolution(struct w1_slave * sl,int val)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 
w1_DS18B20_get_resolution(struct w1_slave * sl)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  */
w1_DS18B20_convert_temp(u8 rom[9])668 static inline int w1_DS18B20_convert_temp(u8 rom[9])
669 {
670 	u16 bv;
671 	s16 t;
672 
673 	/* Signed 16-bit value to unsigned, cpu order */
674 	bv = le16_to_cpup((__le16 *)rom);
675 
676 	/* Config register bit R2 = 1 - GX20MH01 in 13 or 14 bit resolution mode */
677 	if (rom[4] & 0x80) {
678 		/* Insert two temperature bits from config register */
679 		/* Avoid arithmetic shift of signed value */
680 		bv = (bv << 2) | (rom[4] & 3);
681 		t = (s16) bv;	/* Degrees, lowest bit is 2^-6 */
682 		return (int)t * 1000 / 64;	/* Sign-extend to int; millidegrees */
683 	}
684 	t = (s16)bv;	/* Degrees, lowest bit is 2^-4 */
685 	return (int)t * 1000 / 16;	/* Sign-extend to int; millidegrees */
686 }
687 
688 /**
689  * w1_DS18S20_convert_temp() - temperature computation for DS18S20
690  * @rom: data read from device RAM (8 data bytes + 1 CRC byte)
691  *
692  * Can be called for any DS18S20 compliant device.
693  *
694  * Return: value in millidegrees Celsius.
695  */
w1_DS18S20_convert_temp(u8 rom[9])696 static inline int w1_DS18S20_convert_temp(u8 rom[9])
697 {
698 	int t, h;
699 
700 	if (!rom[7]) {
701 		pr_debug("%s: Invalid argument for conversion\n", __func__);
702 		return 0;
703 	}
704 
705 	if (rom[1] == 0)
706 		t = ((s32)rom[0] >> 1)*1000;
707 	else
708 		t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
709 
710 	t -= 250;
711 	h = 1000*((s32)rom[7] - (s32)rom[6]);
712 	h /= (s32)rom[7];
713 	t += h;
714 
715 	return t;
716 }
717 
718 /* Device capability description */
719 /* GX20MH01 device shares family number and structure with DS18B20 */
720 
721 static struct w1_therm_family_converter w1_therm_families[] = {
722 	{
723 		.f				= &w1_therm_family_DS18S20,
724 		.convert			= w1_DS18S20_convert_temp,
725 		.get_conversion_time	= w1_DS18S20_convert_time,
726 		.set_resolution		= NULL,	/* no config register */
727 		.get_resolution		= NULL,	/* no config register */
728 		.write_data			= w1_DS18S20_write_data,
729 		.bulk_read			= true
730 	},
731 	{
732 		.f				= &w1_therm_family_DS1822,
733 		.convert			= w1_DS18B20_convert_temp,
734 		.get_conversion_time	= w1_DS18B20_convert_time,
735 		.set_resolution		= w1_DS18B20_set_resolution,
736 		.get_resolution		= w1_DS18B20_get_resolution,
737 		.write_data			= w1_DS18B20_write_data,
738 		.bulk_read			= true
739 	},
740 	{
741 		/* Also used for GX20MH01 */
742 		.f				= &w1_therm_family_DS18B20,
743 		.convert			= w1_DS18B20_convert_temp,
744 		.get_conversion_time	= w1_DS18B20_convert_time,
745 		.set_resolution		= w1_DS18B20_set_resolution,
746 		.get_resolution		= w1_DS18B20_get_resolution,
747 		.write_data			= w1_DS18B20_write_data,
748 		.bulk_read			= true
749 	},
750 	{
751 		.f				= &w1_therm_family_DS28EA00,
752 		.convert			= w1_DS18B20_convert_temp,
753 		.get_conversion_time	= w1_DS18B20_convert_time,
754 		.set_resolution		= w1_DS18B20_set_resolution,
755 		.get_resolution		= w1_DS18B20_get_resolution,
756 		.write_data			= w1_DS18B20_write_data,
757 		.bulk_read			= false
758 	},
759 	{
760 		.f				= &w1_therm_family_DS1825,
761 		.convert			= w1_DS18B20_convert_temp,
762 		.get_conversion_time	= w1_DS18B20_convert_time,
763 		.set_resolution		= w1_DS18B20_set_resolution,
764 		.get_resolution		= w1_DS18B20_get_resolution,
765 		.write_data			= w1_DS18B20_write_data,
766 		.bulk_read			= true
767 	}
768 };
769 
770 /* Helpers Functions */
771 
772 /**
773  * device_family() - Retrieve a pointer on &struct w1_therm_family_converter
774  * @sl: slave to retrieve the device specific structure
775  *
776  * Return: pointer to the slaves's family converter, NULL if not known
777  */
device_family(struct w1_slave * sl)778 static struct w1_therm_family_converter *device_family(struct w1_slave *sl)
779 {
780 	struct w1_therm_family_converter *ret = NULL;
781 	int i;
782 
783 	for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
784 		if (w1_therm_families[i].f->fid == sl->family->fid) {
785 			ret = &w1_therm_families[i];
786 			break;
787 		}
788 	}
789 	return ret;
790 }
791 
792 /**
793  * bus_mutex_lock() - Acquire the mutex
794  * @lock: w1 bus mutex to acquire
795  *
796  * It try to acquire the mutex W1_THERM_MAX_TRY times and wait
797  * W1_THERM_RETRY_DELAY between 2 attempts.
798  *
799  * Return: true is mutex is acquired and lock, false otherwise
800  */
bus_mutex_lock(struct mutex * lock)801 static inline bool bus_mutex_lock(struct mutex *lock)
802 {
803 	int max_trying = W1_THERM_MAX_TRY;
804 
805 	/* try to acquire the mutex, if not, sleep retry_delay before retry) */
806 	while (mutex_lock_interruptible(lock) != 0 && max_trying > 0) {
807 		unsigned long sleep_rem;
808 
809 		sleep_rem = msleep_interruptible(W1_THERM_RETRY_DELAY);
810 		if (!sleep_rem)
811 			max_trying--;
812 	}
813 
814 	if (!max_trying)
815 		return false;	/* Didn't acquire the bus mutex */
816 
817 	return true;
818 }
819 
820 /**
821  * check_family_data() - Check if family data and specific functions are present
822  * @sl: W1 device data
823  *
824  * Return: 0 - OK, negative value - error
825  */
check_family_data(struct w1_slave * sl)826 static int check_family_data(struct w1_slave *sl)
827 {
828 	if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
829 		dev_info(&sl->dev,
830 			 "%s: Device is not supported by the driver\n", __func__);
831 		return -EINVAL;  /* No device family */
832 	}
833 	return 0;
834 }
835 
836 /**
837  * bulk_read_support() - check if slave support bulk read
838  * @sl: device to check the ability
839  *
840  * Return: true if bulk read is supported, false if not or error
841  */
bulk_read_support(struct w1_slave * sl)842 static inline bool bulk_read_support(struct w1_slave *sl)
843 {
844 	if (SLAVE_SPECIFIC_FUNC(sl))
845 		return SLAVE_SPECIFIC_FUNC(sl)->bulk_read;
846 
847 	dev_info(&sl->dev,
848 		"%s: Device not supported by the driver\n", __func__);
849 
850 	return false;  /* No device family */
851 }
852 
853 /**
854  * conversion_time() - get the Tconv for the slave
855  * @sl: device to get the conversion time
856  *
857  * On device supporting resolution settings, conversion time depend
858  * on the resolution setting. This helper function get the slave timing,
859  * depending on its current setting.
860  *
861  * Return: conversion time in ms, negative values are kernel error code
862  */
conversion_time(struct w1_slave * sl)863 static inline int conversion_time(struct w1_slave *sl)
864 {
865 	if (SLAVE_SPECIFIC_FUNC(sl))
866 		return SLAVE_SPECIFIC_FUNC(sl)->get_conversion_time(sl);
867 
868 	dev_info(&sl->dev,
869 		"%s: Device not supported by the driver\n", __func__);
870 
871 	return -ENODEV;  /* No device family */
872 }
873 
874 /**
875  * temperature_from_RAM() - Convert the read info to temperature
876  * @sl: device that sent the RAM data
877  * @rom: read value on the slave device RAM
878  *
879  * Device dependent, the function bind the correct computation method.
880  *
881  * Return: temperature in 1/1000degC, 0 on error.
882  */
temperature_from_RAM(struct w1_slave * sl,u8 rom[9])883 static inline int temperature_from_RAM(struct w1_slave *sl, u8 rom[9])
884 {
885 	if (SLAVE_SPECIFIC_FUNC(sl))
886 		return SLAVE_SPECIFIC_FUNC(sl)->convert(rom);
887 
888 	dev_info(&sl->dev,
889 		"%s: Device not supported by the driver\n", __func__);
890 
891 	return 0;  /* No device family */
892 }
893 
894 /**
895  * int_to_short() - Safe casting of int to short
896  *
897  * @i: integer to be converted to short
898  *
899  * Device register use 1 byte to store signed integer.
900  * This helper function convert the int in a signed short,
901  * using the min/max values that device can measure as limits.
902  * min/max values are defined by macro.
903  *
904  * Return: a short in the range of min/max value
905  */
int_to_short(int i)906 static inline s8 int_to_short(int i)
907 {
908 	/* Prepare to cast to short by eliminating out of range values */
909 	i = clamp(i, MIN_TEMP, MAX_TEMP);
910 	return (s8) i;
911 }
912 
913 /* Interface Functions */
914 
w1_therm_add_slave(struct w1_slave * sl)915 static int w1_therm_add_slave(struct w1_slave *sl)
916 {
917 	struct w1_therm_family_converter *sl_family_conv;
918 
919 	/* Allocate memory */
920 	sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
921 		GFP_KERNEL);
922 	if (!sl->family_data)
923 		return -ENOMEM;
924 
925 	atomic_set(THERM_REFCNT(sl->family_data), 1);
926 
927 	/* Get a pointer to the device specific function struct */
928 	sl_family_conv = device_family(sl);
929 	if (!sl_family_conv) {
930 		kfree(sl->family_data);
931 		return -ENODEV;
932 	}
933 	/* save this pointer to the device structure */
934 	SLAVE_SPECIFIC_FUNC(sl) = sl_family_conv;
935 
936 	if (bulk_read_support(sl)) {
937 		/*
938 		 * add the sys entry to trigger bulk_read
939 		 * at master level only the 1st time
940 		 */
941 		if (!bulk_read_device_counter) {
942 			int err = device_create_file(&sl->master->dev,
943 				&dev_attr_therm_bulk_read);
944 
945 			if (err)
946 				dev_warn(&sl->dev,
947 				"%s: Device has been added, but bulk read is unavailable. err=%d\n",
948 				__func__, err);
949 		}
950 		/* Increment the counter */
951 		bulk_read_device_counter++;
952 	}
953 
954 	/* Getting the power mode of the device {external, parasite} */
955 	SLAVE_POWERMODE(sl) = read_powermode(sl);
956 
957 	if (SLAVE_POWERMODE(sl) < 0) {
958 		/* no error returned as device has been added */
959 		dev_warn(&sl->dev,
960 			"%s: Device has been added, but power_mode may be corrupted. err=%d\n",
961 			 __func__, SLAVE_POWERMODE(sl));
962 	}
963 
964 	/* Getting the resolution of the device */
965 	if (SLAVE_SPECIFIC_FUNC(sl)->get_resolution) {
966 		SLAVE_RESOLUTION(sl) =
967 			SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl);
968 		if (SLAVE_RESOLUTION(sl) < 0) {
969 			/* no error returned as device has been added */
970 			dev_warn(&sl->dev,
971 				"%s:Device has been added, but resolution may be corrupted. err=%d\n",
972 				__func__, SLAVE_RESOLUTION(sl));
973 		}
974 	}
975 
976 	/* Finally initialize convert_triggered flag */
977 	SLAVE_CONVERT_TRIGGERED(sl) = 0;
978 
979 	return 0;
980 }
981 
w1_therm_remove_slave(struct w1_slave * sl)982 static void w1_therm_remove_slave(struct w1_slave *sl)
983 {
984 	int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
985 
986 	if (bulk_read_support(sl)) {
987 		bulk_read_device_counter--;
988 		/* Delete the entry if no more device support the feature */
989 		if (!bulk_read_device_counter)
990 			device_remove_file(&sl->master->dev,
991 				&dev_attr_therm_bulk_read);
992 	}
993 
994 	while (refcnt) {
995 		msleep(1000);
996 		refcnt = atomic_read(THERM_REFCNT(sl->family_data));
997 	}
998 	kfree(sl->family_data);
999 	sl->family_data = NULL;
1000 }
1001 
1002 /* Hardware Functions */
1003 
1004 /* Safe version of reset_select_slave - avoid using the one in w_io.c */
reset_select_slave(struct w1_slave * sl)1005 static int reset_select_slave(struct w1_slave *sl)
1006 {
1007 	u8 match[9] = { W1_MATCH_ROM, };
1008 	u64 rn = le64_to_cpu(*((u64 *)&sl->reg_num));
1009 
1010 	if (w1_reset_bus(sl->master))
1011 		return -ENODEV;
1012 
1013 	memcpy(&match[1], &rn, 8);
1014 	w1_write_block(sl->master, match, 9);
1015 
1016 	return 0;
1017 }
1018 
1019 /**
1020  * w1_poll_completion - Poll for operation completion, with timeout
1021  * @dev_master: the device master of the bus
1022  * @tout_ms: timeout in milliseconds
1023  *
1024  * The device is answering 0's while an operation is in progress and 1's after it completes
1025  * Timeout may happen if the previous command was not recognised due to a line noise
1026  *
1027  * Return: 0 - OK, negative error - timeout
1028  */
w1_poll_completion(struct w1_master * dev_master,int tout_ms)1029 static int w1_poll_completion(struct w1_master *dev_master, int tout_ms)
1030 {
1031 	int i;
1032 
1033 	for (i = 0; i < tout_ms/W1_POLL_PERIOD; i++) {
1034 		/* Delay is before poll, for device to recognize a command */
1035 		msleep(W1_POLL_PERIOD);
1036 
1037 		/* Compare all 8 bits to mitigate a noise on the bus */
1038 		if (w1_read_8(dev_master) == 0xFF)
1039 			break;
1040 	}
1041 	if (i == tout_ms/W1_POLL_PERIOD)
1042 		return -EIO;
1043 
1044 	return 0;
1045 }
1046 
convert_t(struct w1_slave * sl,struct therm_info * info)1047 static int convert_t(struct w1_slave *sl, struct therm_info *info)
1048 {
1049 	struct w1_master *dev_master = sl->master;
1050 	int max_trying = W1_THERM_MAX_TRY;
1051 	int t_conv;
1052 	int ret = -ENODEV;
1053 	bool strong_pullup;
1054 
1055 	if (!sl->family_data)
1056 		goto error;
1057 
1058 	strong_pullup = (w1_strong_pullup == 2 ||
1059 					(!SLAVE_POWERMODE(sl) &&
1060 					w1_strong_pullup));
1061 
1062 	if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) {
1063 		dev_warn(&sl->dev,
1064 			"%s: Disabling W1_THERM_POLL_COMPLETION in parasite power mode.\n",
1065 			__func__);
1066 		SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION;
1067 	}
1068 
1069 	/* get conversion duration device and id dependent */
1070 	t_conv = conversion_time(sl);
1071 
1072 	memset(info->rom, 0, sizeof(info->rom));
1073 
1074 	/* prevent the slave from going away in sleep */
1075 	atomic_inc(THERM_REFCNT(sl->family_data));
1076 
1077 	if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1078 		ret = -EAGAIN;	/* Didn't acquire the mutex */
1079 		goto dec_refcnt;
1080 	}
1081 
1082 	while (max_trying-- && ret) { /* ret should be 0 */
1083 
1084 		info->verdict = 0;
1085 		info->crc = 0;
1086 		/* safe version to select slave */
1087 		if (!reset_select_slave(sl)) {
1088 			unsigned long sleep_rem;
1089 
1090 			/* 750ms strong pullup (or delay) after the convert */
1091 			if (strong_pullup)
1092 				w1_next_pullup(dev_master, t_conv);
1093 
1094 			w1_write_8(dev_master, W1_CONVERT_TEMP);
1095 
1096 			if (SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) {
1097 				ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP);
1098 				if (ret) {
1099 					dev_dbg(&sl->dev, "%s: Timeout\n", __func__);
1100 					goto mt_unlock;
1101 				}
1102 				mutex_unlock(&dev_master->bus_mutex);
1103 			} else if (!strong_pullup) { /*no device need pullup */
1104 				sleep_rem = msleep_interruptible(t_conv);
1105 				if (sleep_rem != 0) {
1106 					ret = -EINTR;
1107 					goto mt_unlock;
1108 				}
1109 				mutex_unlock(&dev_master->bus_mutex);
1110 			} else { /*some device need pullup */
1111 				mutex_unlock(&dev_master->bus_mutex);
1112 				sleep_rem = msleep_interruptible(t_conv);
1113 				if (sleep_rem != 0) {
1114 					ret = -EINTR;
1115 					goto dec_refcnt;
1116 				}
1117 			}
1118 			ret = read_scratchpad(sl, info);
1119 
1120 			/* If enabled, check for conversion success */
1121 			if ((SLAVE_FEATURES(sl) & W1_THERM_CHECK_RESULT) &&
1122 				(info->rom[6] == 0xC) &&
1123 				((info->rom[1] == 0x5 && info->rom[0] == 0x50) ||
1124 				(info->rom[1] == 0x7 && info->rom[0] == 0xFF))
1125 			) {
1126 				/* Invalid reading (scratchpad byte 6 = 0xC)
1127 				 * due to insufficient conversion time
1128 				 * or power failure.
1129 				 */
1130 				ret = -EIO;
1131 			}
1132 
1133 			goto dec_refcnt;
1134 		}
1135 
1136 	}
1137 
1138 mt_unlock:
1139 	mutex_unlock(&dev_master->bus_mutex);
1140 dec_refcnt:
1141 	atomic_dec(THERM_REFCNT(sl->family_data));
1142 error:
1143 	return ret;
1144 }
1145 
conv_time_measure(struct w1_slave * sl,int * conv_time)1146 static int conv_time_measure(struct w1_slave *sl, int *conv_time)
1147 {
1148 	struct therm_info inf,
1149 		*info = &inf;
1150 	struct w1_master *dev_master = sl->master;
1151 	int max_trying = W1_THERM_MAX_TRY;
1152 	int ret = -ENODEV;
1153 	bool strong_pullup;
1154 
1155 	if (!sl->family_data)
1156 		goto error;
1157 
1158 	strong_pullup = (w1_strong_pullup == 2 ||
1159 		(!SLAVE_POWERMODE(sl) &&
1160 		w1_strong_pullup));
1161 
1162 	if (strong_pullup) {
1163 		pr_info("%s: Measure with strong_pullup is not supported.\n", __func__);
1164 		return -EINVAL;
1165 	}
1166 
1167 	memset(info->rom, 0, sizeof(info->rom));
1168 
1169 	/* prevent the slave from going away in sleep */
1170 	atomic_inc(THERM_REFCNT(sl->family_data));
1171 
1172 	if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1173 		ret = -EAGAIN;	/* Didn't acquire the mutex */
1174 		goto dec_refcnt;
1175 	}
1176 
1177 	while (max_trying-- && ret) { /* ret should be 0 */
1178 		info->verdict = 0;
1179 		info->crc = 0;
1180 		/* safe version to select slave */
1181 		if (!reset_select_slave(sl)) {
1182 			int j_start, j_end;
1183 
1184 			/*no device need pullup */
1185 			w1_write_8(dev_master, W1_CONVERT_TEMP);
1186 
1187 			j_start = jiffies;
1188 			ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP);
1189 			if (ret) {
1190 				dev_dbg(&sl->dev, "%s: Timeout\n", __func__);
1191 				goto mt_unlock;
1192 			}
1193 			j_end = jiffies;
1194 			/* 1.2x increase for variation and changes over temperature range */
1195 			*conv_time = jiffies_to_msecs(j_end-j_start)*12/10;
1196 			pr_debug("W1 Measure complete, conv_time = %d, HZ=%d.\n",
1197 				*conv_time, HZ);
1198 			if (*conv_time <= CONV_TIME_MEASURE) {
1199 				ret = -EIO;
1200 				goto mt_unlock;
1201 			}
1202 			mutex_unlock(&dev_master->bus_mutex);
1203 			ret = read_scratchpad(sl, info);
1204 			goto dec_refcnt;
1205 		}
1206 
1207 	}
1208 mt_unlock:
1209 	mutex_unlock(&dev_master->bus_mutex);
1210 dec_refcnt:
1211 	atomic_dec(THERM_REFCNT(sl->family_data));
1212 error:
1213 	return ret;
1214 }
1215 
read_scratchpad(struct w1_slave * sl,struct therm_info * info)1216 static int read_scratchpad(struct w1_slave *sl, struct therm_info *info)
1217 {
1218 	struct w1_master *dev_master = sl->master;
1219 	int max_trying = W1_THERM_MAX_TRY;
1220 	int ret = -ENODEV;
1221 
1222 	info->verdict = 0;
1223 
1224 	if (!sl->family_data)
1225 		goto error;
1226 
1227 	memset(info->rom, 0, sizeof(info->rom));
1228 
1229 	/* prevent the slave from going away in sleep */
1230 	atomic_inc(THERM_REFCNT(sl->family_data));
1231 
1232 	if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1233 		ret = -EAGAIN;	/* Didn't acquire the mutex */
1234 		goto dec_refcnt;
1235 	}
1236 
1237 	while (max_trying-- && ret) { /* ret should be 0 */
1238 		/* safe version to select slave */
1239 		if (!reset_select_slave(sl)) {
1240 			u8 nb_bytes_read;
1241 
1242 			w1_write_8(dev_master, W1_READ_SCRATCHPAD);
1243 
1244 			nb_bytes_read = w1_read_block(dev_master, info->rom, 9);
1245 			if (nb_bytes_read != 9) {
1246 				dev_warn(&sl->dev,
1247 					"w1_read_block(): returned %u instead of 9.\n",
1248 					nb_bytes_read);
1249 				ret = -EIO;
1250 			}
1251 
1252 			info->crc = w1_calc_crc8(info->rom, 8);
1253 
1254 			if (info->rom[8] == info->crc) {
1255 				info->verdict = 1;
1256 				ret = 0;
1257 			} else
1258 				ret = -EIO; /* CRC not checked */
1259 		}
1260 
1261 	}
1262 	mutex_unlock(&dev_master->bus_mutex);
1263 
1264 dec_refcnt:
1265 	atomic_dec(THERM_REFCNT(sl->family_data));
1266 error:
1267 	return ret;
1268 }
1269 
write_scratchpad(struct w1_slave * sl,const u8 * data,u8 nb_bytes)1270 static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes)
1271 {
1272 	struct w1_master *dev_master = sl->master;
1273 	int max_trying = W1_THERM_MAX_TRY;
1274 	int ret = -ENODEV;
1275 
1276 	if (!sl->family_data)
1277 		goto error;
1278 
1279 	/* prevent the slave from going away in sleep */
1280 	atomic_inc(THERM_REFCNT(sl->family_data));
1281 
1282 	if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1283 		ret = -EAGAIN;	/* Didn't acquire the mutex */
1284 		goto dec_refcnt;
1285 	}
1286 
1287 	while (max_trying-- && ret) { /* ret should be 0 */
1288 		/* safe version to select slave */
1289 		if (!reset_select_slave(sl)) {
1290 			w1_write_8(dev_master, W1_WRITE_SCRATCHPAD);
1291 			w1_write_block(dev_master, data, nb_bytes);
1292 			ret = 0;
1293 		}
1294 	}
1295 	mutex_unlock(&dev_master->bus_mutex);
1296 
1297 dec_refcnt:
1298 	atomic_dec(THERM_REFCNT(sl->family_data));
1299 error:
1300 	return ret;
1301 }
1302 
copy_scratchpad(struct w1_slave * sl)1303 static int copy_scratchpad(struct w1_slave *sl)
1304 {
1305 	struct w1_master *dev_master = sl->master;
1306 	int max_trying = W1_THERM_MAX_TRY;
1307 	int t_write, ret = -ENODEV;
1308 	bool strong_pullup;
1309 
1310 	if (!sl->family_data)
1311 		goto error;
1312 
1313 	t_write = W1_THERM_EEPROM_WRITE_DELAY;
1314 	strong_pullup = (w1_strong_pullup == 2 ||
1315 					(!SLAVE_POWERMODE(sl) &&
1316 					w1_strong_pullup));
1317 
1318 	/* prevent the slave from going away in sleep */
1319 	atomic_inc(THERM_REFCNT(sl->family_data));
1320 
1321 	if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1322 		ret = -EAGAIN;	/* Didn't acquire the mutex */
1323 		goto dec_refcnt;
1324 	}
1325 
1326 	while (max_trying-- && ret) { /* ret should be 0 */
1327 		/* safe version to select slave */
1328 		if (!reset_select_slave(sl)) {
1329 			unsigned long sleep_rem;
1330 
1331 			/* 10ms strong pullup (or delay) after the convert */
1332 			if (strong_pullup)
1333 				w1_next_pullup(dev_master, t_write);
1334 
1335 			w1_write_8(dev_master, W1_COPY_SCRATCHPAD);
1336 
1337 			if (strong_pullup) {
1338 				sleep_rem = msleep_interruptible(t_write);
1339 				if (sleep_rem != 0) {
1340 					ret = -EINTR;
1341 					goto mt_unlock;
1342 				}
1343 			}
1344 			ret = 0;
1345 		}
1346 
1347 	}
1348 
1349 mt_unlock:
1350 	mutex_unlock(&dev_master->bus_mutex);
1351 dec_refcnt:
1352 	atomic_dec(THERM_REFCNT(sl->family_data));
1353 error:
1354 	return ret;
1355 }
1356 
recall_eeprom(struct w1_slave * sl)1357 static int recall_eeprom(struct w1_slave *sl)
1358 {
1359 	struct w1_master *dev_master = sl->master;
1360 	int max_trying = W1_THERM_MAX_TRY;
1361 	int ret = -ENODEV;
1362 
1363 	if (!sl->family_data)
1364 		goto error;
1365 
1366 	/* prevent the slave from going away in sleep */
1367 	atomic_inc(THERM_REFCNT(sl->family_data));
1368 
1369 	if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1370 		ret = -EAGAIN;	/* Didn't acquire the mutex */
1371 		goto dec_refcnt;
1372 	}
1373 
1374 	while (max_trying-- && ret) { /* ret should be 0 */
1375 		/* safe version to select slave */
1376 		if (!reset_select_slave(sl)) {
1377 
1378 			w1_write_8(dev_master, W1_RECALL_EEPROM);
1379 			ret = w1_poll_completion(dev_master, W1_POLL_RECALL_EEPROM);
1380 		}
1381 
1382 	}
1383 
1384 	mutex_unlock(&dev_master->bus_mutex);
1385 
1386 dec_refcnt:
1387 	atomic_dec(THERM_REFCNT(sl->family_data));
1388 error:
1389 	return ret;
1390 }
1391 
read_powermode(struct w1_slave * sl)1392 static int read_powermode(struct w1_slave *sl)
1393 {
1394 	struct w1_master *dev_master = sl->master;
1395 	int max_trying = W1_THERM_MAX_TRY;
1396 	int  ret = -ENODEV;
1397 
1398 	if (!sl->family_data)
1399 		goto error;
1400 
1401 	/* prevent the slave from going away in sleep */
1402 	atomic_inc(THERM_REFCNT(sl->family_data));
1403 
1404 	if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1405 		ret = -EAGAIN;	/* Didn't acquire the mutex */
1406 		goto dec_refcnt;
1407 	}
1408 
1409 	while ((max_trying--) && (ret < 0)) {
1410 		/* safe version to select slave */
1411 		if (!reset_select_slave(sl)) {
1412 			w1_write_8(dev_master, W1_READ_PSUPPLY);
1413 			/*
1414 			 * Emit a read time slot and read only one bit,
1415 			 * 1 is externally powered,
1416 			 * 0 is parasite powered
1417 			 */
1418 			ret = w1_touch_bit(dev_master, 1);
1419 			/* ret should be either 1 either 0 */
1420 		}
1421 	}
1422 	mutex_unlock(&dev_master->bus_mutex);
1423 
1424 dec_refcnt:
1425 	atomic_dec(THERM_REFCNT(sl->family_data));
1426 error:
1427 	return ret;
1428 }
1429 
trigger_bulk_read(struct w1_master * dev_master)1430 static int trigger_bulk_read(struct w1_master *dev_master)
1431 {
1432 	struct w1_slave *sl = NULL; /* used to iterate through slaves */
1433 	int max_trying = W1_THERM_MAX_TRY;
1434 	int t_conv = 0;
1435 	int ret = -ENODEV;
1436 	bool strong_pullup = false;
1437 
1438 	/*
1439 	 * Check whether there are parasite powered device on the bus,
1440 	 * and compute duration of conversion for these devices
1441 	 * so we can apply a strong pullup if required
1442 	 */
1443 	list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
1444 		if (!sl->family_data)
1445 			goto error;
1446 		if (bulk_read_support(sl)) {
1447 			int t_cur = conversion_time(sl);
1448 
1449 			t_conv = t_cur > t_conv ? t_cur : t_conv;
1450 			strong_pullup = strong_pullup ||
1451 					(w1_strong_pullup == 2 ||
1452 					(!SLAVE_POWERMODE(sl) &&
1453 					w1_strong_pullup));
1454 		}
1455 	}
1456 
1457 	/*
1458 	 * t_conv is the max conversion time required on the bus
1459 	 * If its 0, no device support the bulk read feature
1460 	 */
1461 	if (!t_conv)
1462 		goto error;
1463 
1464 	if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1465 		ret = -EAGAIN;	/* Didn't acquire the mutex */
1466 		goto error;
1467 	}
1468 
1469 	while ((max_trying--) && (ret < 0)) { /* ret should be either 0 */
1470 
1471 		if (!w1_reset_bus(dev_master)) {	/* Just reset the bus */
1472 			unsigned long sleep_rem;
1473 
1474 			w1_write_8(dev_master, W1_SKIP_ROM);
1475 
1476 			if (strong_pullup)	/* Apply pullup if required */
1477 				w1_next_pullup(dev_master, t_conv);
1478 
1479 			w1_write_8(dev_master, W1_CONVERT_TEMP);
1480 
1481 			/* set a flag to instruct that converT pending */
1482 			list_for_each_entry(sl,
1483 				&dev_master->slist, w1_slave_entry) {
1484 				if (bulk_read_support(sl))
1485 					SLAVE_CONVERT_TRIGGERED(sl) = -1;
1486 			}
1487 
1488 			if (strong_pullup) { /* some device need pullup */
1489 				sleep_rem = msleep_interruptible(t_conv);
1490 				if (sleep_rem != 0) {
1491 					ret = -EINTR;
1492 					goto mt_unlock;
1493 				}
1494 				mutex_unlock(&dev_master->bus_mutex);
1495 			} else {
1496 				mutex_unlock(&dev_master->bus_mutex);
1497 				sleep_rem = msleep_interruptible(t_conv);
1498 				if (sleep_rem != 0) {
1499 					ret = -EINTR;
1500 					goto set_flag;
1501 				}
1502 			}
1503 			ret = 0;
1504 			goto set_flag;
1505 		}
1506 	}
1507 
1508 mt_unlock:
1509 	mutex_unlock(&dev_master->bus_mutex);
1510 set_flag:
1511 	/* set a flag to register convsersion is done */
1512 	list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
1513 		if (bulk_read_support(sl))
1514 			SLAVE_CONVERT_TRIGGERED(sl) = 1;
1515 	}
1516 error:
1517 	return ret;
1518 }
1519 
1520 /* Sysfs Interface definition */
1521 
w1_slave_show(struct device * device,struct device_attribute * attr,char * buf)1522 static ssize_t w1_slave_show(struct device *device,
1523 			     struct device_attribute *attr, char *buf)
1524 {
1525 	struct w1_slave *sl = dev_to_w1_slave(device);
1526 	struct therm_info info;
1527 	u8 *family_data = sl->family_data;
1528 	int ret, i;
1529 	ssize_t c = PAGE_SIZE;
1530 
1531 	if (bulk_read_support(sl)) {
1532 		if (SLAVE_CONVERT_TRIGGERED(sl) < 0) {
1533 			dev_dbg(device,
1534 				"%s: Conversion in progress, retry later\n",
1535 				__func__);
1536 			return 0;
1537 		} else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) {
1538 			/* A bulk read has been issued, read the device RAM */
1539 			ret = read_scratchpad(sl, &info);
1540 			SLAVE_CONVERT_TRIGGERED(sl) = 0;
1541 		} else
1542 			ret = convert_t(sl, &info);
1543 	} else
1544 		ret = convert_t(sl, &info);
1545 
1546 	if (ret < 0) {
1547 		dev_dbg(device,
1548 			"%s: Temperature data may be corrupted. err=%d\n",
1549 			__func__, ret);
1550 		return 0;
1551 	}
1552 
1553 	for (i = 0; i < 9; ++i)
1554 		c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]);
1555 	c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
1556 		      info.crc, (info.verdict) ? "YES" : "NO");
1557 
1558 	if (info.verdict)
1559 		memcpy(family_data, info.rom, sizeof(info.rom));
1560 	else
1561 		dev_warn(device, "%s:Read failed CRC check\n", __func__);
1562 
1563 	for (i = 0; i < 9; ++i)
1564 		c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
1565 			      ((u8 *)family_data)[i]);
1566 
1567 	c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
1568 			temperature_from_RAM(sl, info.rom));
1569 
1570 	ret = PAGE_SIZE - c;
1571 	return ret;
1572 }
1573 
w1_slave_store(struct device * device,struct device_attribute * attr,const char * buf,size_t size)1574 static ssize_t w1_slave_store(struct device *device,
1575 			      struct device_attribute *attr, const char *buf,
1576 			      size_t size)
1577 {
1578 	int val, ret = 0;
1579 	struct w1_slave *sl = dev_to_w1_slave(device);
1580 
1581 	ret = kstrtoint(buf, 10, &val); /* converting user entry to int */
1582 
1583 	if (ret) {	/* conversion error */
1584 		dev_info(device,
1585 			"%s: conversion error. err= %d\n", __func__, ret);
1586 		return size;	/* return size to avoid call back again */
1587 	}
1588 
1589 	if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1590 		dev_info(device,
1591 			"%s: Device not supported by the driver\n", __func__);
1592 		return size;  /* No device family */
1593 	}
1594 
1595 	if (val == 0)	/* val=0 : trigger a EEPROM save */
1596 		ret = copy_scratchpad(sl);
1597 	else {
1598 		if (SLAVE_SPECIFIC_FUNC(sl)->set_resolution)
1599 			ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val);
1600 	}
1601 
1602 	if (ret) {
1603 		dev_warn(device, "%s: Set resolution - error %d\n", __func__, ret);
1604 		/* Propagate error to userspace */
1605 		return ret;
1606 	}
1607 	SLAVE_RESOLUTION(sl) = val;
1608 	/* Reset the conversion time to default - it depends on resolution */
1609 	SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT;
1610 
1611 	return size; /* always return size to avoid infinite calling */
1612 }
1613 
temperature_show(struct device * device,struct device_attribute * attr,char * buf)1614 static ssize_t temperature_show(struct device *device,
1615 	struct device_attribute *attr, char *buf)
1616 {
1617 	struct w1_slave *sl = dev_to_w1_slave(device);
1618 	struct therm_info info;
1619 	int ret = 0;
1620 
1621 	if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1622 		dev_info(device,
1623 			"%s: Device not supported by the driver\n", __func__);
1624 		return 0;  /* No device family */
1625 	}
1626 
1627 	if (bulk_read_support(sl)) {
1628 		if (SLAVE_CONVERT_TRIGGERED(sl) < 0) {
1629 			dev_dbg(device,
1630 				"%s: Conversion in progress, retry later\n",
1631 				__func__);
1632 			return 0;
1633 		} else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) {
1634 			/* A bulk read has been issued, read the device RAM */
1635 			ret = read_scratchpad(sl, &info);
1636 			SLAVE_CONVERT_TRIGGERED(sl) = 0;
1637 		} else
1638 			ret = convert_t(sl, &info);
1639 	} else
1640 		ret = convert_t(sl, &info);
1641 
1642 	if (ret < 0) {
1643 		dev_dbg(device,
1644 			"%s: Temperature data may be corrupted. err=%d\n",
1645 			__func__, ret);
1646 		return 0;
1647 	}
1648 
1649 	return sprintf(buf, "%d\n", temperature_from_RAM(sl, info.rom));
1650 }
1651 
ext_power_show(struct device * device,struct device_attribute * attr,char * buf)1652 static ssize_t ext_power_show(struct device *device,
1653 	struct device_attribute *attr, char *buf)
1654 {
1655 	struct w1_slave *sl = dev_to_w1_slave(device);
1656 
1657 	if (!sl->family_data) {
1658 		dev_info(device,
1659 			"%s: Device not supported by the driver\n", __func__);
1660 		return 0;  /* No device family */
1661 	}
1662 
1663 	/* Getting the power mode of the device {external, parasite} */
1664 	SLAVE_POWERMODE(sl) = read_powermode(sl);
1665 
1666 	if (SLAVE_POWERMODE(sl) < 0) {
1667 		dev_dbg(device,
1668 			"%s: Power_mode may be corrupted. err=%d\n",
1669 			__func__, SLAVE_POWERMODE(sl));
1670 	}
1671 	return sprintf(buf, "%d\n", SLAVE_POWERMODE(sl));
1672 }
1673 
resolution_show(struct device * device,struct device_attribute * attr,char * buf)1674 static ssize_t resolution_show(struct device *device,
1675 	struct device_attribute *attr, char *buf)
1676 {
1677 	struct w1_slave *sl = dev_to_w1_slave(device);
1678 
1679 	if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1680 		dev_info(device,
1681 			"%s: Device not supported by the driver\n", __func__);
1682 		return 0;  /* No device family */
1683 	}
1684 
1685 	/* get the correct function depending on the device */
1686 	SLAVE_RESOLUTION(sl) = SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl);
1687 	if (SLAVE_RESOLUTION(sl) < 0) {
1688 		dev_dbg(device,
1689 			"%s: Resolution may be corrupted. err=%d\n",
1690 			__func__, SLAVE_RESOLUTION(sl));
1691 	}
1692 
1693 	return sprintf(buf, "%d\n", SLAVE_RESOLUTION(sl));
1694 }
1695 
resolution_store(struct device * device,struct device_attribute * attr,const char * buf,size_t size)1696 static ssize_t resolution_store(struct device *device,
1697 	struct device_attribute *attr, const char *buf, size_t size)
1698 {
1699 	struct w1_slave *sl = dev_to_w1_slave(device);
1700 	int val;
1701 	int ret = 0;
1702 
1703 	ret = kstrtoint(buf, 10, &val); /* converting user entry to int */
1704 
1705 	if (ret) {	/* conversion error */
1706 		dev_info(device,
1707 			"%s: conversion error. err= %d\n", __func__, ret);
1708 		return size;	/* return size to avoid call back again */
1709 	}
1710 
1711 	if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1712 		dev_info(device,
1713 			"%s: Device not supported by the driver\n", __func__);
1714 		return size;  /* No device family */
1715 	}
1716 
1717 	/*
1718 	 * Don't deal with the val enterd by user,
1719 	 * only device knows what is correct or not
1720 	 */
1721 
1722 	/* get the correct function depending on the device */
1723 	ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val);
1724 
1725 	if (ret)
1726 		return ret;
1727 
1728 	SLAVE_RESOLUTION(sl) = val;
1729 	/* Reset the conversion time to default because it depends on resolution */
1730 	SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT;
1731 
1732 	return size;
1733 }
1734 
eeprom_cmd_store(struct device * device,struct device_attribute * attr,const char * buf,size_t size)1735 static ssize_t eeprom_cmd_store(struct device *device,
1736 	struct device_attribute *attr, const char *buf, size_t size)
1737 {
1738 	struct w1_slave *sl = dev_to_w1_slave(device);
1739 	int ret = -EINVAL; /* Invalid argument */
1740 
1741 	if (size == sizeof(EEPROM_CMD_WRITE)) {
1742 		if (!strncmp(buf, EEPROM_CMD_WRITE, sizeof(EEPROM_CMD_WRITE)-1))
1743 			ret = copy_scratchpad(sl);
1744 	} else if (size == sizeof(EEPROM_CMD_READ)) {
1745 		if (!strncmp(buf, EEPROM_CMD_READ, sizeof(EEPROM_CMD_READ)-1))
1746 			ret = recall_eeprom(sl);
1747 	}
1748 
1749 	if (ret)
1750 		dev_info(device, "%s: error in process %d\n", __func__, ret);
1751 
1752 	return size;
1753 }
1754 
alarms_show(struct device * device,struct device_attribute * attr,char * buf)1755 static ssize_t alarms_show(struct device *device,
1756 	struct device_attribute *attr, char *buf)
1757 {
1758 	struct w1_slave *sl = dev_to_w1_slave(device);
1759 	int ret;
1760 	s8 th = 0, tl = 0;
1761 	struct therm_info scratchpad;
1762 
1763 	ret = read_scratchpad(sl, &scratchpad);
1764 
1765 	if (!ret)	{
1766 		th = scratchpad.rom[2]; /* TH is byte 2 */
1767 		tl = scratchpad.rom[3]; /* TL is byte 3 */
1768 	} else {
1769 		dev_info(device,
1770 			"%s: error reading alarms register %d\n",
1771 			__func__, ret);
1772 	}
1773 
1774 	return sprintf(buf, "%hd %hd\n", tl, th);
1775 }
1776 
alarms_store(struct device * device,struct device_attribute * attr,const char * buf,size_t size)1777 static ssize_t alarms_store(struct device *device,
1778 	struct device_attribute *attr, const char *buf, size_t size)
1779 {
1780 	struct w1_slave *sl = dev_to_w1_slave(device);
1781 	struct therm_info info;
1782 	u8 new_config_register[3];	/* array of data to be written */
1783 	int temp, ret;
1784 	char *token = NULL;
1785 	s8 tl, th, tt;	/* 1 byte per value + temp ring order */
1786 	char *p_args, *orig;
1787 
1788 	p_args = orig = kmalloc(size, GFP_KERNEL);
1789 	/* Safe string copys as buf is const */
1790 	if (!p_args) {
1791 		dev_warn(device,
1792 			"%s: error unable to allocate memory %d\n",
1793 			__func__, -ENOMEM);
1794 		return size;
1795 	}
1796 	strcpy(p_args, buf);
1797 
1798 	/* Split string using space char */
1799 	token = strsep(&p_args, " ");
1800 
1801 	if (!token)	{
1802 		dev_info(device,
1803 			"%s: error parsing args %d\n", __func__, -EINVAL);
1804 		goto free_m;
1805 	}
1806 
1807 	/* Convert 1st entry to int */
1808 	ret = kstrtoint (token, 10, &temp);
1809 	if (ret) {
1810 		dev_info(device,
1811 			"%s: error parsing args %d\n", __func__, ret);
1812 		goto free_m;
1813 	}
1814 
1815 	tl = int_to_short(temp);
1816 
1817 	/* Split string using space char */
1818 	token = strsep(&p_args, " ");
1819 	if (!token)	{
1820 		dev_info(device,
1821 			"%s: error parsing args %d\n", __func__, -EINVAL);
1822 		goto free_m;
1823 	}
1824 	/* Convert 2nd entry to int */
1825 	ret = kstrtoint (token, 10, &temp);
1826 	if (ret) {
1827 		dev_info(device,
1828 			"%s: error parsing args %d\n", __func__, ret);
1829 		goto free_m;
1830 	}
1831 
1832 	/* Prepare to cast to short by eliminating out of range values */
1833 	th = int_to_short(temp);
1834 
1835 	/* Reorder if required th and tl */
1836 	if (tl > th) {
1837 		tt = tl; tl = th; th = tt;
1838 	}
1839 
1840 	/*
1841 	 * Read the scratchpad to change only the required bits
1842 	 * (th : byte 2 - tl: byte 3)
1843 	 */
1844 	ret = read_scratchpad(sl, &info);
1845 	if (!ret) {
1846 		new_config_register[0] = th;	/* Byte 2 */
1847 		new_config_register[1] = tl;	/* Byte 3 */
1848 		new_config_register[2] = info.rom[4];/* Byte 4 */
1849 	} else {
1850 		dev_info(device,
1851 			"%s: error reading from the slave device %d\n",
1852 			__func__, ret);
1853 		goto free_m;
1854 	}
1855 
1856 	/* Write data in the device RAM */
1857 	if (!SLAVE_SPECIFIC_FUNC(sl)) {
1858 		dev_info(device,
1859 			"%s: Device not supported by the driver %d\n",
1860 			__func__, -ENODEV);
1861 		goto free_m;
1862 	}
1863 
1864 	ret = SLAVE_SPECIFIC_FUNC(sl)->write_data(sl, new_config_register);
1865 	if (ret)
1866 		dev_info(device,
1867 			"%s: error writing to the slave device %d\n",
1868 			__func__, ret);
1869 
1870 free_m:
1871 	/* free allocated memory */
1872 	kfree(orig);
1873 
1874 	return size;
1875 }
1876 
therm_bulk_read_store(struct device * device,struct device_attribute * attr,const char * buf,size_t size)1877 static ssize_t therm_bulk_read_store(struct device *device,
1878 	struct device_attribute *attr, const char *buf, size_t size)
1879 {
1880 	struct w1_master *dev_master = dev_to_w1_master(device);
1881 	int ret = -EINVAL; /* Invalid argument */
1882 
1883 	if (size == sizeof(BULK_TRIGGER_CMD))
1884 		if (!strncmp(buf, BULK_TRIGGER_CMD,
1885 				sizeof(BULK_TRIGGER_CMD)-1))
1886 			ret = trigger_bulk_read(dev_master);
1887 
1888 	if (ret)
1889 		dev_info(device,
1890 			"%s: unable to trigger a bulk read on the bus. err=%d\n",
1891 			__func__, ret);
1892 
1893 	return size;
1894 }
1895 
therm_bulk_read_show(struct device * device,struct device_attribute * attr,char * buf)1896 static ssize_t therm_bulk_read_show(struct device *device,
1897 	struct device_attribute *attr, char *buf)
1898 {
1899 	struct w1_master *dev_master = dev_to_w1_master(device);
1900 	struct w1_slave *sl = NULL;
1901 	int ret = 0;
1902 
1903 	list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
1904 		if (sl->family_data) {
1905 			if (bulk_read_support(sl)) {
1906 				if (SLAVE_CONVERT_TRIGGERED(sl) == -1) {
1907 					ret = -1;
1908 					goto show_result;
1909 				}
1910 				if (SLAVE_CONVERT_TRIGGERED(sl) == 1)
1911 					/* continue to check other slaves */
1912 					ret = 1;
1913 			}
1914 		}
1915 	}
1916 show_result:
1917 	return sprintf(buf, "%d\n", ret);
1918 }
1919 
conv_time_show(struct device * device,struct device_attribute * attr,char * buf)1920 static ssize_t conv_time_show(struct device *device,
1921 	struct device_attribute *attr, char *buf)
1922 {
1923 	struct w1_slave *sl = dev_to_w1_slave(device);
1924 
1925 	if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1926 		dev_info(device,
1927 			"%s: Device is not supported by the driver\n", __func__);
1928 		return 0;  /* No device family */
1929 	}
1930 	return sprintf(buf, "%d\n", conversion_time(sl));
1931 }
1932 
conv_time_store(struct device * device,struct device_attribute * attr,const char * buf,size_t size)1933 static ssize_t conv_time_store(struct device *device,
1934 	struct device_attribute *attr, const char *buf, size_t size)
1935 {
1936 	int val, ret = 0;
1937 	struct w1_slave *sl = dev_to_w1_slave(device);
1938 
1939 	if (kstrtoint(buf, 10, &val)) /* converting user entry to int */
1940 		return -EINVAL;
1941 
1942 	if (check_family_data(sl))
1943 		return -ENODEV;
1944 
1945 	if (val != CONV_TIME_MEASURE) {
1946 		if (val >= CONV_TIME_DEFAULT)
1947 			SLAVE_CONV_TIME_OVERRIDE(sl) = val;
1948 		else
1949 			return -EINVAL;
1950 
1951 	} else {
1952 		int conv_time;
1953 
1954 		ret = conv_time_measure(sl, &conv_time);
1955 		if (ret)
1956 			return -EIO;
1957 		SLAVE_CONV_TIME_OVERRIDE(sl) = conv_time;
1958 	}
1959 	return size;
1960 }
1961 
features_show(struct device * device,struct device_attribute * attr,char * buf)1962 static ssize_t features_show(struct device *device,
1963 			     struct device_attribute *attr, char *buf)
1964 {
1965 	struct w1_slave *sl = dev_to_w1_slave(device);
1966 
1967 	if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1968 		dev_info(device,
1969 			 "%s: Device not supported by the driver\n", __func__);
1970 		return 0;  /* No device family */
1971 	}
1972 	return sprintf(buf, "%u\n", SLAVE_FEATURES(sl));
1973 }
1974 
features_store(struct device * device,struct device_attribute * attr,const char * buf,size_t size)1975 static ssize_t features_store(struct device *device,
1976 			      struct device_attribute *attr, const char *buf, size_t size)
1977 {
1978 	int val, ret = 0;
1979 	bool strong_pullup;
1980 	struct w1_slave *sl = dev_to_w1_slave(device);
1981 
1982 	ret = kstrtouint(buf, 10, &val); /* converting user entry to int */
1983 	if (ret)
1984 		return -EINVAL;  /* invalid number */
1985 
1986 	if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1987 		dev_info(device, "%s: Device not supported by the driver\n", __func__);
1988 		return -ENODEV;
1989 	}
1990 
1991 	if ((val & W1_THERM_FEATURES_MASK) != val)
1992 		return -EINVAL;
1993 
1994 	SLAVE_FEATURES(sl) = val;
1995 
1996 	strong_pullup = (w1_strong_pullup == 2 ||
1997 			 (!SLAVE_POWERMODE(sl) &&
1998 			  w1_strong_pullup));
1999 
2000 	if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) {
2001 		dev_warn(&sl->dev,
2002 			 "%s: W1_THERM_POLL_COMPLETION disabled in parasite power mode.\n",
2003 			 __func__);
2004 		SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION;
2005 	}
2006 
2007 	return size;
2008 }
2009 
2010 #if IS_REACHABLE(CONFIG_HWMON)
w1_read_temp(struct device * device,u32 attr,int channel,long * val)2011 static int w1_read_temp(struct device *device, u32 attr, int channel,
2012 			long *val)
2013 {
2014 	struct w1_slave *sl = dev_get_drvdata(device);
2015 	struct therm_info info;
2016 	int ret;
2017 
2018 	switch (attr) {
2019 	case hwmon_temp_input:
2020 		ret = convert_t(sl, &info);
2021 		if (ret)
2022 			return ret;
2023 
2024 		if (!info.verdict) {
2025 			ret = -EIO;
2026 			return ret;
2027 		}
2028 
2029 		*val = temperature_from_RAM(sl, info.rom);
2030 		ret = 0;
2031 		break;
2032 	default:
2033 		ret = -EOPNOTSUPP;
2034 		break;
2035 	}
2036 
2037 	return ret;
2038 }
2039 #endif
2040 
2041 #define W1_42_CHAIN	0x99
2042 #define W1_42_CHAIN_OFF	0x3C
2043 #define W1_42_CHAIN_OFF_INV	0xC3
2044 #define W1_42_CHAIN_ON	0x5A
2045 #define W1_42_CHAIN_ON_INV	0xA5
2046 #define W1_42_CHAIN_DONE 0x96
2047 #define W1_42_CHAIN_DONE_INV 0x69
2048 #define W1_42_COND_READ	0x0F
2049 #define W1_42_SUCCESS_CONFIRM_BYTE 0xAA
2050 #define W1_42_FINISHED_BYTE 0xFF
w1_seq_show(struct device * device,struct device_attribute * attr,char * buf)2051 static ssize_t w1_seq_show(struct device *device,
2052 	struct device_attribute *attr, char *buf)
2053 {
2054 	struct w1_slave *sl = dev_to_w1_slave(device);
2055 	ssize_t c = PAGE_SIZE;
2056 	int i;
2057 	u8 ack;
2058 	u64 rn;
2059 	struct w1_reg_num *reg_num;
2060 	int seq = 0;
2061 
2062 	mutex_lock(&sl->master->bus_mutex);
2063 	/* Place all devices in CHAIN state */
2064 	if (w1_reset_bus(sl->master))
2065 		goto error;
2066 	w1_write_8(sl->master, W1_SKIP_ROM);
2067 	w1_write_8(sl->master, W1_42_CHAIN);
2068 	w1_write_8(sl->master, W1_42_CHAIN_ON);
2069 	w1_write_8(sl->master, W1_42_CHAIN_ON_INV);
2070 	msleep(sl->master->pullup_duration);
2071 
2072 	/* check for acknowledgment */
2073 	ack = w1_read_8(sl->master);
2074 	if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
2075 		goto error;
2076 
2077 	/* In case the bus fails to send 0xFF, limit */
2078 	for (i = 0; i <= 64; i++) {
2079 		if (w1_reset_bus(sl->master))
2080 			goto error;
2081 
2082 		w1_write_8(sl->master, W1_42_COND_READ);
2083 		w1_read_block(sl->master, (u8 *)&rn, 8);
2084 		reg_num = (struct w1_reg_num *) &rn;
2085 		if (reg_num->family == W1_42_FINISHED_BYTE)
2086 			break;
2087 		if (sl->reg_num.id == reg_num->id)
2088 			seq = i;
2089 
2090 		if (w1_reset_bus(sl->master))
2091 			goto error;
2092 
2093 		/* Put the device into chain DONE state */
2094 		w1_write_8(sl->master, W1_MATCH_ROM);
2095 		w1_write_block(sl->master, (u8 *)&rn, 8);
2096 		w1_write_8(sl->master, W1_42_CHAIN);
2097 		w1_write_8(sl->master, W1_42_CHAIN_DONE);
2098 		w1_write_8(sl->master, W1_42_CHAIN_DONE_INV);
2099 
2100 		/* check for acknowledgment */
2101 		ack = w1_read_8(sl->master);
2102 		if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
2103 			goto error;
2104 	}
2105 
2106 	/* Exit from CHAIN state */
2107 	if (w1_reset_bus(sl->master))
2108 		goto error;
2109 	w1_write_8(sl->master, W1_SKIP_ROM);
2110 	w1_write_8(sl->master, W1_42_CHAIN);
2111 	w1_write_8(sl->master, W1_42_CHAIN_OFF);
2112 	w1_write_8(sl->master, W1_42_CHAIN_OFF_INV);
2113 
2114 	/* check for acknowledgment */
2115 	ack = w1_read_8(sl->master);
2116 	if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
2117 		goto error;
2118 	mutex_unlock(&sl->master->bus_mutex);
2119 
2120 	c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq);
2121 	return PAGE_SIZE - c;
2122 error:
2123 	mutex_unlock(&sl->master->bus_mutex);
2124 	return -EIO;
2125 }
2126 
w1_therm_init(void)2127 static int __init w1_therm_init(void)
2128 {
2129 	int err, i;
2130 
2131 	for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
2132 		err = w1_register_family(w1_therm_families[i].f);
2133 		if (err)
2134 			w1_therm_families[i].broken = 1;
2135 	}
2136 
2137 	return 0;
2138 }
2139 
w1_therm_fini(void)2140 static void __exit w1_therm_fini(void)
2141 {
2142 	int i;
2143 
2144 	for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
2145 		if (!w1_therm_families[i].broken)
2146 			w1_unregister_family(w1_therm_families[i].f);
2147 }
2148 
2149 module_init(w1_therm_init);
2150 module_exit(w1_therm_fini);
2151 
2152 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
2153 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
2154 MODULE_LICENSE("GPL");
2155 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20));
2156 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822));
2157 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20));
2158 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825));
2159 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00));
2160