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