1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
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>
22 #define W1_THERM_DS18S20 0x10
23 #define W1_THERM_DS1822 0x22
24 #define W1_THERM_DS18B20 0x28
25 #define W1_THERM_DS1825 0x3B
26 #define W1_THERM_DS28EA00 0x42
28 /* Allow the strong pullup to be disabled, but default to enabled.
29 * If it was disabled a parasite powered device might not get the require
30 * current to do a temperature conversion. If it is enabled parasite powered
31 * devices have a better chance of getting the current required.
32 * In case the parasite power-detection is not working (seems to be the case
33 * for some DS18S20) the strong pullup can also be forced, regardless of the
34 * power state of the devices.
37 * - strong_pullup = 0 Disable strong pullup completely
38 * - strong_pullup = 1 Enable automatic strong pullup detection
39 * - strong_pullup = 2 Force strong pullup
41 static int w1_strong_pullup
= 1;
42 module_param_named(strong_pullup
, w1_strong_pullup
, int, 0);
44 struct w1_therm_family_data
{
55 /* return the address of the refcnt in the family data */
56 #define THERM_REFCNT(family_data) \
57 (&((struct w1_therm_family_data *)family_data)->refcnt)
59 static int w1_therm_add_slave(struct w1_slave
*sl
)
61 sl
->family_data
= kzalloc(sizeof(struct w1_therm_family_data
),
65 atomic_set(THERM_REFCNT(sl
->family_data
), 1);
69 static void w1_therm_remove_slave(struct w1_slave
*sl
)
71 int refcnt
= atomic_sub_return(1, THERM_REFCNT(sl
->family_data
));
75 refcnt
= atomic_read(THERM_REFCNT(sl
->family_data
));
77 kfree(sl
->family_data
);
78 sl
->family_data
= NULL
;
81 static ssize_t
w1_slave_show(struct device
*device
,
82 struct device_attribute
*attr
, char *buf
);
84 static ssize_t
w1_slave_store(struct device
*device
,
85 struct device_attribute
*attr
, const char *buf
, size_t size
);
87 static ssize_t
w1_seq_show(struct device
*device
,
88 struct device_attribute
*attr
, char *buf
);
90 static DEVICE_ATTR_RW(w1_slave
);
91 static DEVICE_ATTR_RO(w1_seq
);
93 static struct attribute
*w1_therm_attrs
[] = {
94 &dev_attr_w1_slave
.attr
,
98 static struct attribute
*w1_ds28ea00_attrs
[] = {
99 &dev_attr_w1_slave
.attr
,
100 &dev_attr_w1_seq
.attr
,
104 ATTRIBUTE_GROUPS(w1_therm
);
105 ATTRIBUTE_GROUPS(w1_ds28ea00
);
107 #if IS_REACHABLE(CONFIG_HWMON)
108 static int w1_read_temp(struct device
*dev
, u32 attr
, int channel
,
111 static umode_t
w1_is_visible(const void *_data
, enum hwmon_sensor_types type
,
112 u32 attr
, int channel
)
114 return attr
== hwmon_temp_input
? 0444 : 0;
117 static int w1_read(struct device
*dev
, enum hwmon_sensor_types type
,
118 u32 attr
, int channel
, long *val
)
122 return w1_read_temp(dev
, attr
, channel
, val
);
128 static const u32 w1_temp_config
[] = {
133 static const struct hwmon_channel_info w1_temp
= {
135 .config
= w1_temp_config
,
138 static const struct hwmon_channel_info
*w1_info
[] = {
143 static const struct hwmon_ops w1_hwmon_ops
= {
144 .is_visible
= w1_is_visible
,
148 static const struct hwmon_chip_info w1_chip_info
= {
149 .ops
= &w1_hwmon_ops
,
152 #define W1_CHIPINFO (&w1_chip_info)
154 #define W1_CHIPINFO NULL
157 static struct w1_family_ops w1_therm_fops
= {
158 .add_slave
= w1_therm_add_slave
,
159 .remove_slave
= w1_therm_remove_slave
,
160 .groups
= w1_therm_groups
,
161 .chip_info
= W1_CHIPINFO
,
164 static struct w1_family_ops w1_ds28ea00_fops
= {
165 .add_slave
= w1_therm_add_slave
,
166 .remove_slave
= w1_therm_remove_slave
,
167 .groups
= w1_ds28ea00_groups
,
168 .chip_info
= W1_CHIPINFO
,
171 static struct w1_family w1_therm_family_DS18S20
= {
172 .fid
= W1_THERM_DS18S20
,
173 .fops
= &w1_therm_fops
,
176 static struct w1_family w1_therm_family_DS18B20
= {
177 .fid
= W1_THERM_DS18B20
,
178 .fops
= &w1_therm_fops
,
181 static struct w1_family w1_therm_family_DS1822
= {
182 .fid
= W1_THERM_DS1822
,
183 .fops
= &w1_therm_fops
,
186 static struct w1_family w1_therm_family_DS28EA00
= {
187 .fid
= W1_THERM_DS28EA00
,
188 .fops
= &w1_ds28ea00_fops
,
191 static struct w1_family w1_therm_family_DS1825
= {
192 .fid
= W1_THERM_DS1825
,
193 .fops
= &w1_therm_fops
,
196 struct w1_therm_family_converter
{
200 int (*convert
)(u8 rom
[9]);
201 int (*precision
)(struct device
*device
, int val
);
202 int (*eeprom
)(struct device
*device
);
205 /* write configuration to eeprom */
206 static inline int w1_therm_eeprom(struct device
*device
);
208 /* Set precision for conversion */
209 static inline int w1_DS18B20_precision(struct device
*device
, int val
);
210 static inline int w1_DS18S20_precision(struct device
*device
, int val
);
212 /* The return value is millidegrees Centigrade. */
213 static inline int w1_DS18B20_convert_temp(u8 rom
[9]);
214 static inline int w1_DS18S20_convert_temp(u8 rom
[9]);
216 static struct w1_therm_family_converter w1_therm_families
[] = {
218 .f
= &w1_therm_family_DS18S20
,
219 .convert
= w1_DS18S20_convert_temp
,
220 .precision
= w1_DS18S20_precision
,
221 .eeprom
= w1_therm_eeprom
224 .f
= &w1_therm_family_DS1822
,
225 .convert
= w1_DS18B20_convert_temp
,
226 .precision
= w1_DS18S20_precision
,
227 .eeprom
= w1_therm_eeprom
230 .f
= &w1_therm_family_DS18B20
,
231 .convert
= w1_DS18B20_convert_temp
,
232 .precision
= w1_DS18B20_precision
,
233 .eeprom
= w1_therm_eeprom
236 .f
= &w1_therm_family_DS28EA00
,
237 .convert
= w1_DS18B20_convert_temp
,
238 .precision
= w1_DS18S20_precision
,
239 .eeprom
= w1_therm_eeprom
242 .f
= &w1_therm_family_DS1825
,
243 .convert
= w1_DS18B20_convert_temp
,
244 .precision
= w1_DS18S20_precision
,
245 .eeprom
= w1_therm_eeprom
249 static inline int w1_therm_eeprom(struct device
*device
)
251 struct w1_slave
*sl
= dev_to_w1_slave(device
);
252 struct w1_master
*dev
= sl
->master
;
253 u8 rom
[9], external_power
;
254 int ret
, max_trying
= 10;
255 u8
*family_data
= sl
->family_data
;
257 if (!sl
->family_data
) {
262 /* prevent the slave from going away in sleep */
263 atomic_inc(THERM_REFCNT(family_data
));
265 ret
= mutex_lock_interruptible(&dev
->bus_mutex
);
269 memset(rom
, 0, sizeof(rom
));
271 while (max_trying
--) {
272 if (!w1_reset_select_slave(sl
)) {
273 unsigned int tm
= 10;
274 unsigned long sleep_rem
;
276 /* check if in parasite mode */
277 w1_write_8(dev
, W1_READ_PSUPPLY
);
278 external_power
= w1_read_8(dev
);
280 if (w1_reset_select_slave(sl
))
283 /* 10ms strong pullup/delay after the copy command */
284 if (w1_strong_pullup
== 2 ||
285 (!external_power
&& w1_strong_pullup
))
286 w1_next_pullup(dev
, tm
);
288 w1_write_8(dev
, W1_COPY_SCRATCHPAD
);
290 if (external_power
) {
291 mutex_unlock(&dev
->bus_mutex
);
293 sleep_rem
= msleep_interruptible(tm
);
294 if (sleep_rem
!= 0) {
299 ret
= mutex_lock_interruptible(&dev
->bus_mutex
);
302 } else if (!w1_strong_pullup
) {
303 sleep_rem
= msleep_interruptible(tm
);
304 if (sleep_rem
!= 0) {
315 mutex_unlock(&dev
->bus_mutex
);
317 atomic_dec(THERM_REFCNT(family_data
));
322 /* DS18S20 does not feature configuration register */
323 static inline int w1_DS18S20_precision(struct device
*device
, int val
)
328 static inline int w1_DS18B20_precision(struct device
*device
, int val
)
330 struct w1_slave
*sl
= dev_to_w1_slave(device
);
331 struct w1_master
*dev
= sl
->master
;
333 int ret
, max_trying
= 10;
334 u8
*family_data
= sl
->family_data
;
335 uint8_t precision_bits
;
338 if (val
> 12 || val
< 9) {
339 pr_warn("Unsupported precision\n");
344 if (!sl
->family_data
) {
349 /* prevent the slave from going away in sleep */
350 atomic_inc(THERM_REFCNT(family_data
));
352 ret
= mutex_lock_interruptible(&dev
->bus_mutex
);
356 memset(rom
, 0, sizeof(rom
));
358 /* translate precision to bitmask (see datasheet page 9) */
361 precision_bits
= 0x00;
364 precision_bits
= 0x20;
367 precision_bits
= 0x40;
371 precision_bits
= 0x60;
375 while (max_trying
--) {
378 if (!w1_reset_select_slave(sl
)) {
381 /* read values to only alter precision bits */
382 w1_write_8(dev
, W1_READ_SCRATCHPAD
);
383 count
= w1_read_block(dev
, rom
, 9);
385 dev_warn(device
, "w1_read_block() returned %u instead of 9.\n", count
);
387 crc
= w1_calc_crc8(rom
, 8);
389 rom
[4] = (rom
[4] & ~mask
) | (precision_bits
& mask
);
391 if (!w1_reset_select_slave(sl
)) {
392 w1_write_8(dev
, W1_WRITE_SCRATCHPAD
);
393 w1_write_8(dev
, rom
[2]);
394 w1_write_8(dev
, rom
[3]);
395 w1_write_8(dev
, rom
[4]);
403 mutex_unlock(&dev
->bus_mutex
);
405 atomic_dec(THERM_REFCNT(family_data
));
410 static inline int w1_DS18B20_convert_temp(u8 rom
[9])
412 s16 t
= le16_to_cpup((__le16
*)rom
);
417 static inline int w1_DS18S20_convert_temp(u8 rom
[9])
425 t
= ((s32
)rom
[0] >> 1)*1000;
427 t
= 1000*(-1*(s32
)(0x100-rom
[0]) >> 1);
430 h
= 1000*((s32
)rom
[7] - (s32
)rom
[6]);
437 static inline int w1_convert_temp(u8 rom
[9], u8 fid
)
441 for (i
= 0; i
< ARRAY_SIZE(w1_therm_families
); ++i
)
442 if (w1_therm_families
[i
].f
->fid
== fid
)
443 return w1_therm_families
[i
].convert(rom
);
448 static ssize_t
w1_slave_store(struct device
*device
,
449 struct device_attribute
*attr
, const char *buf
,
453 struct w1_slave
*sl
= dev_to_w1_slave(device
);
456 ret
= kstrtoint(buf
, 0, &val
);
460 for (i
= 0; i
< ARRAY_SIZE(w1_therm_families
); ++i
) {
461 if (w1_therm_families
[i
].f
->fid
== sl
->family
->fid
) {
462 /* zero value indicates to write current configuration to eeprom */
464 ret
= w1_therm_families
[i
].eeprom(device
);
466 ret
= w1_therm_families
[i
].precision(device
, val
);
473 static ssize_t
read_therm(struct device
*device
,
474 struct w1_slave
*sl
, struct therm_info
*info
)
476 struct w1_master
*dev
= sl
->master
;
478 int ret
, max_trying
= 10;
479 u8
*family_data
= sl
->family_data
;
486 /* prevent the slave from going away in sleep */
487 atomic_inc(THERM_REFCNT(family_data
));
489 ret
= mutex_lock_interruptible(&dev
->bus_mutex
);
493 memset(info
->rom
, 0, sizeof(info
->rom
));
495 while (max_trying
--) {
500 if (!w1_reset_select_slave(sl
)) {
502 unsigned int tm
= 750;
503 unsigned long sleep_rem
;
505 w1_write_8(dev
, W1_READ_PSUPPLY
);
506 external_power
= w1_read_8(dev
);
508 if (w1_reset_select_slave(sl
))
511 /* 750ms strong pullup (or delay) after the convert */
512 if (w1_strong_pullup
== 2 ||
513 (!external_power
&& w1_strong_pullup
))
514 w1_next_pullup(dev
, tm
);
516 w1_write_8(dev
, W1_CONVERT_TEMP
);
518 if (external_power
) {
519 mutex_unlock(&dev
->bus_mutex
);
521 sleep_rem
= msleep_interruptible(tm
);
522 if (sleep_rem
!= 0) {
527 ret
= mutex_lock_interruptible(&dev
->bus_mutex
);
530 } else if (!w1_strong_pullup
) {
531 sleep_rem
= msleep_interruptible(tm
);
532 if (sleep_rem
!= 0) {
538 if (!w1_reset_select_slave(sl
)) {
540 w1_write_8(dev
, W1_READ_SCRATCHPAD
);
541 count
= w1_read_block(dev
, info
->rom
, 9);
543 dev_warn(device
, "w1_read_block() "
544 "returned %u instead of 9.\n",
548 info
->crc
= w1_calc_crc8(info
->rom
, 8);
550 if (info
->rom
[8] == info
->crc
)
560 mutex_unlock(&dev
->bus_mutex
);
562 atomic_dec(THERM_REFCNT(family_data
));
567 static ssize_t
w1_slave_show(struct device
*device
,
568 struct device_attribute
*attr
, char *buf
)
570 struct w1_slave
*sl
= dev_to_w1_slave(device
);
571 struct therm_info info
;
572 u8
*family_data
= sl
->family_data
;
574 ssize_t c
= PAGE_SIZE
;
575 u8 fid
= sl
->family
->fid
;
577 ret
= read_therm(device
, sl
, &info
);
581 for (i
= 0; i
< 9; ++i
)
582 c
-= snprintf(buf
+ PAGE_SIZE
- c
, c
, "%02x ", info
.rom
[i
]);
583 c
-= snprintf(buf
+ PAGE_SIZE
- c
, c
, ": crc=%02x %s\n",
584 info
.crc
, (info
.verdict
) ? "YES" : "NO");
586 memcpy(family_data
, info
.rom
, sizeof(info
.rom
));
588 dev_warn(device
, "Read failed CRC check\n");
590 for (i
= 0; i
< 9; ++i
)
591 c
-= snprintf(buf
+ PAGE_SIZE
- c
, c
, "%02x ",
592 ((u8
*)family_data
)[i
]);
594 c
-= snprintf(buf
+ PAGE_SIZE
- c
, c
, "t=%d\n",
595 w1_convert_temp(info
.rom
, fid
));
600 #if IS_REACHABLE(CONFIG_HWMON)
601 static int w1_read_temp(struct device
*device
, u32 attr
, int channel
,
604 struct w1_slave
*sl
= dev_get_drvdata(device
);
605 struct therm_info info
;
606 u8 fid
= sl
->family
->fid
;
610 case hwmon_temp_input
:
611 ret
= read_therm(device
, sl
, &info
);
620 *val
= w1_convert_temp(info
.rom
, fid
);
632 #define W1_42_CHAIN 0x99
633 #define W1_42_CHAIN_OFF 0x3C
634 #define W1_42_CHAIN_OFF_INV 0xC3
635 #define W1_42_CHAIN_ON 0x5A
636 #define W1_42_CHAIN_ON_INV 0xA5
637 #define W1_42_CHAIN_DONE 0x96
638 #define W1_42_CHAIN_DONE_INV 0x69
639 #define W1_42_COND_READ 0x0F
640 #define W1_42_SUCCESS_CONFIRM_BYTE 0xAA
641 #define W1_42_FINISHED_BYTE 0xFF
642 static ssize_t
w1_seq_show(struct device
*device
,
643 struct device_attribute
*attr
, char *buf
)
645 struct w1_slave
*sl
= dev_to_w1_slave(device
);
646 ssize_t c
= PAGE_SIZE
;
651 struct w1_reg_num
*reg_num
;
654 mutex_lock(&sl
->master
->bus_mutex
);
655 /* Place all devices in CHAIN state */
656 if (w1_reset_bus(sl
->master
))
658 w1_write_8(sl
->master
, W1_SKIP_ROM
);
659 w1_write_8(sl
->master
, W1_42_CHAIN
);
660 w1_write_8(sl
->master
, W1_42_CHAIN_ON
);
661 w1_write_8(sl
->master
, W1_42_CHAIN_ON_INV
);
662 msleep(sl
->master
->pullup_duration
);
664 /* check for acknowledgment */
665 ack
= w1_read_8(sl
->master
);
666 if (ack
!= W1_42_SUCCESS_CONFIRM_BYTE
)
669 /* In case the bus fails to send 0xFF, limit*/
670 for (i
= 0; i
<= 64; i
++) {
671 if (w1_reset_bus(sl
->master
))
674 w1_write_8(sl
->master
, W1_42_COND_READ
);
675 rv
= w1_read_block(sl
->master
, (u8
*)&rn
, 8);
676 reg_num
= (struct w1_reg_num
*) &rn
;
677 if (reg_num
->family
== W1_42_FINISHED_BYTE
)
679 if (sl
->reg_num
.id
== reg_num
->id
)
682 w1_write_8(sl
->master
, W1_42_CHAIN
);
683 w1_write_8(sl
->master
, W1_42_CHAIN_DONE
);
684 w1_write_8(sl
->master
, W1_42_CHAIN_DONE_INV
);
685 w1_read_block(sl
->master
, &ack
, sizeof(ack
));
687 /* check for acknowledgment */
688 ack
= w1_read_8(sl
->master
);
689 if (ack
!= W1_42_SUCCESS_CONFIRM_BYTE
)
694 /* Exit from CHAIN state */
695 if (w1_reset_bus(sl
->master
))
697 w1_write_8(sl
->master
, W1_SKIP_ROM
);
698 w1_write_8(sl
->master
, W1_42_CHAIN
);
699 w1_write_8(sl
->master
, W1_42_CHAIN_OFF
);
700 w1_write_8(sl
->master
, W1_42_CHAIN_OFF_INV
);
702 /* check for acknowledgment */
703 ack
= w1_read_8(sl
->master
);
704 if (ack
!= W1_42_SUCCESS_CONFIRM_BYTE
)
706 mutex_unlock(&sl
->master
->bus_mutex
);
708 c
-= snprintf(buf
+ PAGE_SIZE
- c
, c
, "%d\n", seq
);
709 return PAGE_SIZE
- c
;
711 mutex_unlock(&sl
->master
->bus_mutex
);
715 static int __init
w1_therm_init(void)
719 for (i
= 0; i
< ARRAY_SIZE(w1_therm_families
); ++i
) {
720 err
= w1_register_family(w1_therm_families
[i
].f
);
722 w1_therm_families
[i
].broken
= 1;
728 static void __exit
w1_therm_fini(void)
732 for (i
= 0; i
< ARRAY_SIZE(w1_therm_families
); ++i
)
733 if (!w1_therm_families
[i
].broken
)
734 w1_unregister_family(w1_therm_families
[i
].f
);
737 module_init(w1_therm_init
);
738 module_exit(w1_therm_fini
);
740 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
741 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
742 MODULE_LICENSE("GPL");
743 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20
));
744 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822
));
745 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20
));
746 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825
));
747 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00
));