4 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the therms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <asm/types.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/sched.h>
28 #include <linux/device.h>
29 #include <linux/types.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
34 #include "../w1_int.h"
35 #include "../w1_family.h"
37 MODULE_LICENSE("GPL");
38 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
39 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
40 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20
));
41 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822
));
42 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20
));
43 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825
));
44 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00
));
46 /* Allow the strong pullup to be disabled, but default to enabled.
47 * If it was disabled a parasite powered device might not get the require
48 * current to do a temperature conversion. If it is enabled parasite powered
49 * devices have a better chance of getting the current required.
50 * In case the parasite power-detection is not working (seems to be the case
51 * for some DS18S20) the strong pullup can also be forced, regardless of the
52 * power state of the devices.
55 * - strong_pullup = 0 Disable strong pullup completely
56 * - strong_pullup = 1 Enable automatic strong pullup detection
57 * - strong_pullup = 2 Force strong pullup
59 static int w1_strong_pullup
= 1;
60 module_param_named(strong_pullup
, w1_strong_pullup
, int, 0);
62 struct w1_therm_family_data
{
67 /* return the address of the refcnt in the family data */
68 #define THERM_REFCNT(family_data) \
69 (&((struct w1_therm_family_data *)family_data)->refcnt)
71 static int w1_therm_add_slave(struct w1_slave
*sl
)
73 sl
->family_data
= kzalloc(sizeof(struct w1_therm_family_data
),
77 atomic_set(THERM_REFCNT(sl
->family_data
), 1);
81 static void w1_therm_remove_slave(struct w1_slave
*sl
)
83 int refcnt
= atomic_sub_return(1, THERM_REFCNT(sl
->family_data
));
87 refcnt
= atomic_read(THERM_REFCNT(sl
->family_data
));
89 kfree(sl
->family_data
);
90 sl
->family_data
= NULL
;
93 static ssize_t
w1_slave_show(struct device
*device
,
94 struct device_attribute
*attr
, char *buf
);
96 static ssize_t
w1_slave_store(struct device
*device
,
97 struct device_attribute
*attr
, const char *buf
, size_t size
);
99 static ssize_t
w1_seq_show(struct device
*device
,
100 struct device_attribute
*attr
, char *buf
);
102 static DEVICE_ATTR_RW(w1_slave
);
103 static DEVICE_ATTR_RO(w1_seq
);
105 static struct attribute
*w1_therm_attrs
[] = {
106 &dev_attr_w1_slave
.attr
,
110 static struct attribute
*w1_ds28ea00_attrs
[] = {
111 &dev_attr_w1_slave
.attr
,
112 &dev_attr_w1_seq
.attr
,
115 ATTRIBUTE_GROUPS(w1_therm
);
116 ATTRIBUTE_GROUPS(w1_ds28ea00
);
118 static struct w1_family_ops w1_therm_fops
= {
119 .add_slave
= w1_therm_add_slave
,
120 .remove_slave
= w1_therm_remove_slave
,
121 .groups
= w1_therm_groups
,
124 static struct w1_family_ops w1_ds28ea00_fops
= {
125 .add_slave
= w1_therm_add_slave
,
126 .remove_slave
= w1_therm_remove_slave
,
127 .groups
= w1_ds28ea00_groups
,
130 static struct w1_family w1_therm_family_DS18S20
= {
131 .fid
= W1_THERM_DS18S20
,
132 .fops
= &w1_therm_fops
,
135 static struct w1_family w1_therm_family_DS18B20
= {
136 .fid
= W1_THERM_DS18B20
,
137 .fops
= &w1_therm_fops
,
140 static struct w1_family w1_therm_family_DS1822
= {
141 .fid
= W1_THERM_DS1822
,
142 .fops
= &w1_therm_fops
,
145 static struct w1_family w1_therm_family_DS28EA00
= {
146 .fid
= W1_THERM_DS28EA00
,
147 .fops
= &w1_ds28ea00_fops
,
150 static struct w1_family w1_therm_family_DS1825
= {
151 .fid
= W1_THERM_DS1825
,
152 .fops
= &w1_therm_fops
,
155 struct w1_therm_family_converter
{
159 int (*convert
)(u8 rom
[9]);
160 int (*precision
)(struct device
*device
, int val
);
161 int (*eeprom
)(struct device
*device
);
164 /* write configuration to eeprom */
165 static inline int w1_therm_eeprom(struct device
*device
);
167 /* Set precision for conversion */
168 static inline int w1_DS18B20_precision(struct device
*device
, int val
);
169 static inline int w1_DS18S20_precision(struct device
*device
, int val
);
171 /* The return value is millidegrees Centigrade. */
172 static inline int w1_DS18B20_convert_temp(u8 rom
[9]);
173 static inline int w1_DS18S20_convert_temp(u8 rom
[9]);
175 static struct w1_therm_family_converter w1_therm_families
[] = {
177 .f
= &w1_therm_family_DS18S20
,
178 .convert
= w1_DS18S20_convert_temp
,
179 .precision
= w1_DS18S20_precision
,
180 .eeprom
= w1_therm_eeprom
183 .f
= &w1_therm_family_DS1822
,
184 .convert
= w1_DS18B20_convert_temp
,
185 .precision
= w1_DS18S20_precision
,
186 .eeprom
= w1_therm_eeprom
189 .f
= &w1_therm_family_DS18B20
,
190 .convert
= w1_DS18B20_convert_temp
,
191 .precision
= w1_DS18B20_precision
,
192 .eeprom
= w1_therm_eeprom
195 .f
= &w1_therm_family_DS28EA00
,
196 .convert
= w1_DS18B20_convert_temp
,
197 .precision
= w1_DS18S20_precision
,
198 .eeprom
= w1_therm_eeprom
201 .f
= &w1_therm_family_DS1825
,
202 .convert
= w1_DS18B20_convert_temp
,
203 .precision
= w1_DS18S20_precision
,
204 .eeprom
= w1_therm_eeprom
208 static inline int w1_therm_eeprom(struct device
*device
)
210 struct w1_slave
*sl
= dev_to_w1_slave(device
);
211 struct w1_master
*dev
= sl
->master
;
212 u8 rom
[9], external_power
;
213 int ret
, max_trying
= 10;
214 u8
*family_data
= sl
->family_data
;
216 ret
= mutex_lock_interruptible(&dev
->bus_mutex
);
220 if (!sl
->family_data
) {
225 /* prevent the slave from going away in sleep */
226 atomic_inc(THERM_REFCNT(family_data
));
227 memset(rom
, 0, sizeof(rom
));
229 while (max_trying
--) {
230 if (!w1_reset_select_slave(sl
)) {
231 unsigned int tm
= 10;
232 unsigned long sleep_rem
;
234 /* check if in parasite mode */
235 w1_write_8(dev
, W1_READ_PSUPPLY
);
236 external_power
= w1_read_8(dev
);
238 if (w1_reset_select_slave(sl
))
241 /* 10ms strong pullup/delay after the copy command */
242 if (w1_strong_pullup
== 2 ||
243 (!external_power
&& w1_strong_pullup
))
244 w1_next_pullup(dev
, tm
);
246 w1_write_8(dev
, W1_COPY_SCRATCHPAD
);
248 if (external_power
) {
249 mutex_unlock(&dev
->bus_mutex
);
251 sleep_rem
= msleep_interruptible(tm
);
252 if (sleep_rem
!= 0) {
257 ret
= mutex_lock_interruptible(&dev
->bus_mutex
);
260 } else if (!w1_strong_pullup
) {
261 sleep_rem
= msleep_interruptible(tm
);
262 if (sleep_rem
!= 0) {
273 mutex_unlock(&dev
->bus_mutex
);
276 atomic_dec(THERM_REFCNT(family_data
));
280 /* DS18S20 does not feature configuration register */
281 static inline int w1_DS18S20_precision(struct device
*device
, int val
)
286 static inline int w1_DS18B20_precision(struct device
*device
, int val
)
288 struct w1_slave
*sl
= dev_to_w1_slave(device
);
289 struct w1_master
*dev
= sl
->master
;
291 int ret
, max_trying
= 10;
292 u8
*family_data
= sl
->family_data
;
293 uint8_t precision_bits
;
296 if (val
> 12 || val
< 9) {
297 pr_warn("Unsupported precision\n");
301 ret
= mutex_lock_interruptible(&dev
->bus_mutex
);
305 if (!sl
->family_data
) {
310 /* prevent the slave from going away in sleep */
311 atomic_inc(THERM_REFCNT(family_data
));
312 memset(rom
, 0, sizeof(rom
));
314 /* translate precision to bitmask (see datasheet page 9) */
317 precision_bits
= 0x00;
320 precision_bits
= 0x20;
323 precision_bits
= 0x40;
327 precision_bits
= 0x60;
331 while (max_trying
--) {
334 if (!w1_reset_select_slave(sl
)) {
337 /* read values to only alter precision bits */
338 w1_write_8(dev
, W1_READ_SCRATCHPAD
);
339 count
= w1_read_block(dev
, rom
, 9);
341 dev_warn(device
, "w1_read_block() returned %u instead of 9.\n", count
);
343 crc
= w1_calc_crc8(rom
, 8);
345 rom
[4] = (rom
[4] & ~mask
) | (precision_bits
& mask
);
347 if (!w1_reset_select_slave(sl
)) {
348 w1_write_8(dev
, W1_WRITE_SCRATCHPAD
);
349 w1_write_8(dev
, rom
[2]);
350 w1_write_8(dev
, rom
[3]);
351 w1_write_8(dev
, rom
[4]);
360 mutex_unlock(&dev
->bus_mutex
);
363 atomic_dec(THERM_REFCNT(family_data
));
367 static inline int w1_DS18B20_convert_temp(u8 rom
[9])
369 s16 t
= le16_to_cpup((__le16
*)rom
);
374 static inline int w1_DS18S20_convert_temp(u8 rom
[9])
382 t
= ((s32
)rom
[0] >> 1)*1000;
384 t
= 1000*(-1*(s32
)(0x100-rom
[0]) >> 1);
387 h
= 1000*((s32
)rom
[7] - (s32
)rom
[6]);
394 static inline int w1_convert_temp(u8 rom
[9], u8 fid
)
398 for (i
= 0; i
< ARRAY_SIZE(w1_therm_families
); ++i
)
399 if (w1_therm_families
[i
].f
->fid
== fid
)
400 return w1_therm_families
[i
].convert(rom
);
405 static ssize_t
w1_slave_store(struct device
*device
,
406 struct device_attribute
*attr
, const char *buf
,
410 struct w1_slave
*sl
= dev_to_w1_slave(device
);
413 ret
= kstrtoint(buf
, 0, &val
);
417 for (i
= 0; i
< ARRAY_SIZE(w1_therm_families
); ++i
) {
418 if (w1_therm_families
[i
].f
->fid
== sl
->family
->fid
) {
419 /* zero value indicates to write current configuration to eeprom */
421 ret
= w1_therm_families
[i
].eeprom(device
);
423 ret
= w1_therm_families
[i
].precision(device
, val
);
430 static ssize_t
w1_slave_show(struct device
*device
,
431 struct device_attribute
*attr
, char *buf
)
433 struct w1_slave
*sl
= dev_to_w1_slave(device
);
434 struct w1_master
*dev
= sl
->master
;
435 u8 rom
[9], crc
, verdict
, external_power
;
436 int i
, ret
, max_trying
= 10;
437 ssize_t c
= PAGE_SIZE
;
438 u8
*family_data
= sl
->family_data
;
440 ret
= mutex_lock_interruptible(&dev
->bus_mutex
);
444 if (!sl
->family_data
) {
449 /* prevent the slave from going away in sleep */
450 atomic_inc(THERM_REFCNT(family_data
));
451 memset(rom
, 0, sizeof(rom
));
453 while (max_trying
--) {
458 if (!w1_reset_select_slave(sl
)) {
460 unsigned int tm
= 750;
461 unsigned long sleep_rem
;
463 w1_write_8(dev
, W1_READ_PSUPPLY
);
464 external_power
= w1_read_8(dev
);
466 if (w1_reset_select_slave(sl
))
469 /* 750ms strong pullup (or delay) after the convert */
470 if (w1_strong_pullup
== 2 ||
471 (!external_power
&& w1_strong_pullup
))
472 w1_next_pullup(dev
, tm
);
474 w1_write_8(dev
, W1_CONVERT_TEMP
);
476 if (external_power
) {
477 mutex_unlock(&dev
->bus_mutex
);
479 sleep_rem
= msleep_interruptible(tm
);
480 if (sleep_rem
!= 0) {
485 ret
= mutex_lock_interruptible(&dev
->bus_mutex
);
488 } else if (!w1_strong_pullup
) {
489 sleep_rem
= msleep_interruptible(tm
);
490 if (sleep_rem
!= 0) {
496 if (!w1_reset_select_slave(sl
)) {
498 w1_write_8(dev
, W1_READ_SCRATCHPAD
);
499 count
= w1_read_block(dev
, rom
, 9);
501 dev_warn(device
, "w1_read_block() "
502 "returned %u instead of 9.\n",
506 crc
= w1_calc_crc8(rom
, 8);
517 for (i
= 0; i
< 9; ++i
)
518 c
-= snprintf(buf
+ PAGE_SIZE
- c
, c
, "%02x ", rom
[i
]);
519 c
-= snprintf(buf
+ PAGE_SIZE
- c
, c
, ": crc=%02x %s\n",
520 crc
, (verdict
) ? "YES" : "NO");
522 memcpy(family_data
, rom
, sizeof(rom
));
524 dev_warn(device
, "Read failed CRC check\n");
526 for (i
= 0; i
< 9; ++i
)
527 c
-= snprintf(buf
+ PAGE_SIZE
- c
, c
, "%02x ",
528 ((u8
*)family_data
)[i
]);
530 c
-= snprintf(buf
+ PAGE_SIZE
- c
, c
, "t=%d\n",
531 w1_convert_temp(rom
, sl
->family
->fid
));
535 mutex_unlock(&dev
->bus_mutex
);
538 atomic_dec(THERM_REFCNT(family_data
));
542 #define W1_42_CHAIN 0x99
543 #define W1_42_CHAIN_OFF 0x3C
544 #define W1_42_CHAIN_OFF_INV 0xC3
545 #define W1_42_CHAIN_ON 0x5A
546 #define W1_42_CHAIN_ON_INV 0xA5
547 #define W1_42_CHAIN_DONE 0x96
548 #define W1_42_CHAIN_DONE_INV 0x69
549 #define W1_42_COND_READ 0x0F
550 #define W1_42_SUCCESS_CONFIRM_BYTE 0xAA
551 #define W1_42_FINISHED_BYTE 0xFF
552 static ssize_t
w1_seq_show(struct device
*device
,
553 struct device_attribute
*attr
, char *buf
)
555 struct w1_slave
*sl
= dev_to_w1_slave(device
);
556 ssize_t c
= PAGE_SIZE
;
561 struct w1_reg_num
*reg_num
;
564 mutex_lock(&sl
->master
->bus_mutex
);
565 /* Place all devices in CHAIN state */
566 if (w1_reset_bus(sl
->master
))
568 w1_write_8(sl
->master
, W1_SKIP_ROM
);
569 w1_write_8(sl
->master
, W1_42_CHAIN
);
570 w1_write_8(sl
->master
, W1_42_CHAIN_ON
);
571 w1_write_8(sl
->master
, W1_42_CHAIN_ON_INV
);
572 msleep(sl
->master
->pullup_duration
);
574 /* check for acknowledgment */
575 ack
= w1_read_8(sl
->master
);
576 if (ack
!= W1_42_SUCCESS_CONFIRM_BYTE
)
579 /* In case the bus fails to send 0xFF, limit*/
580 for (i
= 0; i
<= 64; i
++) {
581 if (w1_reset_bus(sl
->master
))
584 w1_write_8(sl
->master
, W1_42_COND_READ
);
585 rv
= w1_read_block(sl
->master
, (u8
*)&rn
, 8);
586 reg_num
= (struct w1_reg_num
*) &rn
;
587 if (reg_num
->family
== W1_42_FINISHED_BYTE
)
589 if (sl
->reg_num
.id
== reg_num
->id
)
592 w1_write_8(sl
->master
, W1_42_CHAIN
);
593 w1_write_8(sl
->master
, W1_42_CHAIN_DONE
);
594 w1_write_8(sl
->master
, W1_42_CHAIN_DONE_INV
);
595 w1_read_block(sl
->master
, &ack
, sizeof(ack
));
597 /* check for acknowledgment */
598 ack
= w1_read_8(sl
->master
);
599 if (ack
!= W1_42_SUCCESS_CONFIRM_BYTE
)
604 /* Exit from CHAIN state */
605 if (w1_reset_bus(sl
->master
))
607 w1_write_8(sl
->master
, W1_SKIP_ROM
);
608 w1_write_8(sl
->master
, W1_42_CHAIN
);
609 w1_write_8(sl
->master
, W1_42_CHAIN_OFF
);
610 w1_write_8(sl
->master
, W1_42_CHAIN_OFF_INV
);
612 /* check for acknowledgment */
613 ack
= w1_read_8(sl
->master
);
614 if (ack
!= W1_42_SUCCESS_CONFIRM_BYTE
)
616 mutex_unlock(&sl
->master
->bus_mutex
);
618 c
-= snprintf(buf
+ PAGE_SIZE
- c
, c
, "%d\n", seq
);
619 return PAGE_SIZE
- c
;
621 mutex_unlock(&sl
->master
->bus_mutex
);
625 static int __init
w1_therm_init(void)
629 for (i
= 0; i
< ARRAY_SIZE(w1_therm_families
); ++i
) {
630 err
= w1_register_family(w1_therm_families
[i
].f
);
632 w1_therm_families
[i
].broken
= 1;
638 static void __exit
w1_therm_fini(void)
642 for (i
= 0; i
< ARRAY_SIZE(w1_therm_families
); ++i
)
643 if (!w1_therm_families
[i
].broken
)
644 w1_unregister_family(w1_therm_families
[i
].f
);
647 module_init(w1_therm_init
);
648 module_exit(w1_therm_fini
);