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