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 /* Allow the strong pullup to be disabled, but default to enabled.
38 * If it was disabled a parasite powered device might not get the require
39 * current to do a temperature conversion. If it is enabled parasite powered
40 * devices have a better chance of getting the current required.
41 * In case the parasite power-detection is not working (seems to be the case
42 * for some DS18S20) the strong pullup can also be forced, regardless of the
43 * power state of the devices.
46 * - strong_pullup = 0 Disable strong pullup completely
47 * - strong_pullup = 1 Enable automatic strong pullup detection
48 * - strong_pullup = 2 Force strong pullup
50 static int w1_strong_pullup = 1;
51 module_param_named(strong_pullup, w1_strong_pullup, int, 0);
53 struct w1_therm_family_data {
58 /* return the address of the refcnt in the family data */
59 #define THERM_REFCNT(family_data) \
60 (&((struct w1_therm_family_data *)family_data)->refcnt)
62 static int w1_therm_add_slave(struct w1_slave *sl)
64 sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
68 atomic_set(THERM_REFCNT(sl->family_data), 1);
72 static void w1_therm_remove_slave(struct w1_slave *sl)
74 int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
78 refcnt = atomic_read(THERM_REFCNT(sl->family_data));
80 kfree(sl->family_data);
81 sl->family_data = NULL;
84 static ssize_t w1_slave_show(struct device *device,
85 struct device_attribute *attr, char *buf);
87 static ssize_t w1_slave_store(struct device *device,
88 struct device_attribute *attr, const char *buf, size_t size);
90 static ssize_t w1_seq_show(struct device *device,
91 struct device_attribute *attr, char *buf);
93 static DEVICE_ATTR_RW(w1_slave);
94 static DEVICE_ATTR_RO(w1_seq);
96 static struct attribute *w1_therm_attrs[] = {
97 &dev_attr_w1_slave.attr,
101 static struct attribute *w1_ds28ea00_attrs[] = {
102 &dev_attr_w1_slave.attr,
103 &dev_attr_w1_seq.attr,
106 ATTRIBUTE_GROUPS(w1_therm);
107 ATTRIBUTE_GROUPS(w1_ds28ea00);
109 static struct w1_family_ops w1_therm_fops = {
110 .add_slave = w1_therm_add_slave,
111 .remove_slave = w1_therm_remove_slave,
112 .groups = w1_therm_groups,
115 static struct w1_family_ops w1_ds28ea00_fops = {
116 .add_slave = w1_therm_add_slave,
117 .remove_slave = w1_therm_remove_slave,
118 .groups = w1_ds28ea00_groups,
121 static struct w1_family w1_therm_family_DS18S20 = {
122 .fid = W1_THERM_DS18S20,
123 .fops = &w1_therm_fops,
126 static struct w1_family w1_therm_family_DS18B20 = {
127 .fid = W1_THERM_DS18B20,
128 .fops = &w1_therm_fops,
131 static struct w1_family w1_therm_family_DS1822 = {
132 .fid = W1_THERM_DS1822,
133 .fops = &w1_therm_fops,
136 static struct w1_family w1_therm_family_DS28EA00 = {
137 .fid = W1_THERM_DS28EA00,
138 .fops = &w1_ds28ea00_fops,
141 static struct w1_family w1_therm_family_DS1825 = {
142 .fid = W1_THERM_DS1825,
143 .fops = &w1_therm_fops,
146 struct w1_therm_family_converter {
150 int (*convert)(u8 rom[9]);
151 int (*precision)(struct device *device, int val);
152 int (*eeprom)(struct device *device);
155 /* write configuration to eeprom */
156 static inline int w1_therm_eeprom(struct device *device);
158 /* Set precision for conversion */
159 static inline int w1_DS18B20_precision(struct device *device, int val);
160 static inline int w1_DS18S20_precision(struct device *device, int val);
162 /* The return value is millidegrees Centigrade. */
163 static inline int w1_DS18B20_convert_temp(u8 rom[9]);
164 static inline int w1_DS18S20_convert_temp(u8 rom[9]);
166 static struct w1_therm_family_converter w1_therm_families[] = {
168 .f = &w1_therm_family_DS18S20,
169 .convert = w1_DS18S20_convert_temp,
170 .precision = w1_DS18S20_precision,
171 .eeprom = w1_therm_eeprom
174 .f = &w1_therm_family_DS1822,
175 .convert = w1_DS18B20_convert_temp,
176 .precision = w1_DS18S20_precision,
177 .eeprom = w1_therm_eeprom
180 .f = &w1_therm_family_DS18B20,
181 .convert = w1_DS18B20_convert_temp,
182 .precision = w1_DS18B20_precision,
183 .eeprom = w1_therm_eeprom
186 .f = &w1_therm_family_DS28EA00,
187 .convert = w1_DS18B20_convert_temp,
188 .precision = w1_DS18S20_precision,
189 .eeprom = w1_therm_eeprom
192 .f = &w1_therm_family_DS1825,
193 .convert = w1_DS18B20_convert_temp,
194 .precision = w1_DS18S20_precision,
195 .eeprom = w1_therm_eeprom
199 static inline int w1_therm_eeprom(struct device *device)
201 struct w1_slave *sl = dev_to_w1_slave(device);
202 struct w1_master *dev = sl->master;
203 u8 rom[9], external_power;
204 int ret, max_trying = 10;
205 u8 *family_data = sl->family_data;
207 ret = mutex_lock_interruptible(&dev->bus_mutex);
211 if (!sl->family_data) {
216 /* prevent the slave from going away in sleep */
217 atomic_inc(THERM_REFCNT(family_data));
218 memset(rom, 0, sizeof(rom));
220 while (max_trying--) {
221 if (!w1_reset_select_slave(sl)) {
222 unsigned int tm = 10;
223 unsigned long sleep_rem;
225 /* check if in parasite mode */
226 w1_write_8(dev, W1_READ_PSUPPLY);
227 external_power = w1_read_8(dev);
229 if (w1_reset_select_slave(sl))
232 /* 10ms strong pullup/delay after the copy command */
233 if (w1_strong_pullup == 2 ||
234 (!external_power && w1_strong_pullup))
235 w1_next_pullup(dev, tm);
237 w1_write_8(dev, W1_COPY_SCRATCHPAD);
239 if (external_power) {
240 mutex_unlock(&dev->bus_mutex);
242 sleep_rem = msleep_interruptible(tm);
243 if (sleep_rem != 0) {
248 ret = mutex_lock_interruptible(&dev->bus_mutex);
251 } else if (!w1_strong_pullup) {
252 sleep_rem = msleep_interruptible(tm);
253 if (sleep_rem != 0) {
264 mutex_unlock(&dev->bus_mutex);
267 atomic_dec(THERM_REFCNT(family_data));
271 /* DS18S20 does not feature configuration register */
272 static inline int w1_DS18S20_precision(struct device *device, int val)
277 static inline int w1_DS18B20_precision(struct device *device, int val)
279 struct w1_slave *sl = dev_to_w1_slave(device);
280 struct w1_master *dev = sl->master;
282 int ret, max_trying = 10;
283 u8 *family_data = sl->family_data;
284 uint8_t precision_bits;
287 if (val > 12 || val < 9) {
288 pr_warn("Unsupported precision\n");
292 ret = mutex_lock_interruptible(&dev->bus_mutex);
296 if (!sl->family_data) {
301 /* prevent the slave from going away in sleep */
302 atomic_inc(THERM_REFCNT(family_data));
303 memset(rom, 0, sizeof(rom));
305 /* translate precision to bitmask (see datasheet page 9) */
308 precision_bits = 0x00;
311 precision_bits = 0x20;
314 precision_bits = 0x40;
318 precision_bits = 0x60;
322 while (max_trying--) {
325 if (!w1_reset_select_slave(sl)) {
328 /* read values to only alter precision bits */
329 w1_write_8(dev, W1_READ_SCRATCHPAD);
330 count = w1_read_block(dev, rom, 9);
332 dev_warn(device, "w1_read_block() returned %u instead of 9.\n", count);
334 crc = w1_calc_crc8(rom, 8);
336 rom[4] = (rom[4] & ~mask) | (precision_bits & mask);
338 if (!w1_reset_select_slave(sl)) {
339 w1_write_8(dev, W1_WRITE_SCRATCHPAD);
340 w1_write_8(dev, rom[2]);
341 w1_write_8(dev, rom[3]);
342 w1_write_8(dev, rom[4]);
351 mutex_unlock(&dev->bus_mutex);
354 atomic_dec(THERM_REFCNT(family_data));
358 static inline int w1_DS18B20_convert_temp(u8 rom[9])
360 s16 t = le16_to_cpup((__le16 *)rom);
365 static inline int w1_DS18S20_convert_temp(u8 rom[9])
373 t = ((s32)rom[0] >> 1)*1000;
375 t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
378 h = 1000*((s32)rom[7] - (s32)rom[6]);
385 static inline int w1_convert_temp(u8 rom[9], u8 fid)
389 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
390 if (w1_therm_families[i].f->fid == fid)
391 return w1_therm_families[i].convert(rom);
396 static ssize_t w1_slave_store(struct device *device,
397 struct device_attribute *attr, const char *buf,
401 struct w1_slave *sl = dev_to_w1_slave(device);
404 ret = kstrtoint(buf, 0, &val);
408 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
409 if (w1_therm_families[i].f->fid == sl->family->fid) {
410 /* zero value indicates to write current configuration to eeprom */
412 ret = w1_therm_families[i].eeprom(device);
414 ret = w1_therm_families[i].precision(device, val);
421 static ssize_t w1_slave_show(struct device *device,
422 struct device_attribute *attr, char *buf)
424 struct w1_slave *sl = dev_to_w1_slave(device);
425 struct w1_master *dev = sl->master;
426 u8 rom[9], crc, verdict, external_power;
427 int i, ret, max_trying = 10;
428 ssize_t c = PAGE_SIZE;
429 u8 *family_data = sl->family_data;
431 ret = mutex_lock_interruptible(&dev->bus_mutex);
435 if (!sl->family_data) {
440 /* prevent the slave from going away in sleep */
441 atomic_inc(THERM_REFCNT(family_data));
442 memset(rom, 0, sizeof(rom));
444 while (max_trying--) {
449 if (!w1_reset_select_slave(sl)) {
451 unsigned int tm = 750;
452 unsigned long sleep_rem;
454 w1_write_8(dev, W1_READ_PSUPPLY);
455 external_power = w1_read_8(dev);
457 if (w1_reset_select_slave(sl))
460 /* 750ms strong pullup (or delay) after the convert */
461 if (w1_strong_pullup == 2 ||
462 (!external_power && w1_strong_pullup))
463 w1_next_pullup(dev, tm);
465 w1_write_8(dev, W1_CONVERT_TEMP);
467 if (external_power) {
468 mutex_unlock(&dev->bus_mutex);
470 sleep_rem = msleep_interruptible(tm);
471 if (sleep_rem != 0) {
476 ret = mutex_lock_interruptible(&dev->bus_mutex);
479 } else if (!w1_strong_pullup) {
480 sleep_rem = msleep_interruptible(tm);
481 if (sleep_rem != 0) {
487 if (!w1_reset_select_slave(sl)) {
489 w1_write_8(dev, W1_READ_SCRATCHPAD);
490 count = w1_read_block(dev, rom, 9);
492 dev_warn(device, "w1_read_block() "
493 "returned %u instead of 9.\n",
497 crc = w1_calc_crc8(rom, 8);
508 for (i = 0; i < 9; ++i)
509 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", rom[i]);
510 c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
511 crc, (verdict) ? "YES" : "NO");
513 memcpy(family_data, rom, sizeof(rom));
515 dev_warn(device, "Read failed CRC check\n");
517 for (i = 0; i < 9; ++i)
518 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
519 ((u8 *)family_data)[i]);
521 c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
522 w1_convert_temp(rom, sl->family->fid));
526 mutex_unlock(&dev->bus_mutex);
529 atomic_dec(THERM_REFCNT(family_data));
533 #define W1_42_CHAIN 0x99
534 #define W1_42_CHAIN_OFF 0x3C
535 #define W1_42_CHAIN_OFF_INV 0xC3
536 #define W1_42_CHAIN_ON 0x5A
537 #define W1_42_CHAIN_ON_INV 0xA5
538 #define W1_42_CHAIN_DONE 0x96
539 #define W1_42_CHAIN_DONE_INV 0x69
540 #define W1_42_COND_READ 0x0F
541 #define W1_42_SUCCESS_CONFIRM_BYTE 0xAA
542 #define W1_42_FINISHED_BYTE 0xFF
543 static ssize_t w1_seq_show(struct device *device,
544 struct device_attribute *attr, char *buf)
546 struct w1_slave *sl = dev_to_w1_slave(device);
547 ssize_t c = PAGE_SIZE;
552 struct w1_reg_num *reg_num;
555 mutex_lock(&sl->master->bus_mutex);
556 /* Place all devices in CHAIN state */
557 if (w1_reset_bus(sl->master))
559 w1_write_8(sl->master, W1_SKIP_ROM);
560 w1_write_8(sl->master, W1_42_CHAIN);
561 w1_write_8(sl->master, W1_42_CHAIN_ON);
562 w1_write_8(sl->master, W1_42_CHAIN_ON_INV);
563 msleep(sl->master->pullup_duration);
565 /* check for acknowledgment */
566 ack = w1_read_8(sl->master);
567 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
570 /* In case the bus fails to send 0xFF, limit*/
571 for (i = 0; i <= 64; i++) {
572 if (w1_reset_bus(sl->master))
575 w1_write_8(sl->master, W1_42_COND_READ);
576 rv = w1_read_block(sl->master, (u8 *)&rn, 8);
577 reg_num = (struct w1_reg_num *) &rn;
578 if (reg_num->family == W1_42_FINISHED_BYTE)
580 if (sl->reg_num.id == reg_num->id)
583 w1_write_8(sl->master, W1_42_CHAIN);
584 w1_write_8(sl->master, W1_42_CHAIN_DONE);
585 w1_write_8(sl->master, W1_42_CHAIN_DONE_INV);
586 w1_read_block(sl->master, &ack, sizeof(ack));
588 /* check for acknowledgment */
589 ack = w1_read_8(sl->master);
590 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
595 /* Exit from CHAIN state */
596 if (w1_reset_bus(sl->master))
598 w1_write_8(sl->master, W1_SKIP_ROM);
599 w1_write_8(sl->master, W1_42_CHAIN);
600 w1_write_8(sl->master, W1_42_CHAIN_OFF);
601 w1_write_8(sl->master, W1_42_CHAIN_OFF_INV);
603 /* check for acknowledgment */
604 ack = w1_read_8(sl->master);
605 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
607 mutex_unlock(&sl->master->bus_mutex);
609 c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq);
610 return PAGE_SIZE - c;
612 mutex_unlock(&sl->master->bus_mutex);
616 static int __init w1_therm_init(void)
620 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
621 err = w1_register_family(w1_therm_families[i].f);
623 w1_therm_families[i].broken = 1;
629 static void __exit w1_therm_fini(void)
633 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
634 if (!w1_therm_families[i].broken)
635 w1_unregister_family(w1_therm_families[i].f);
638 module_init(w1_therm_init);
639 module_exit(w1_therm_fini);
641 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
642 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
643 MODULE_LICENSE("GPL");
644 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20));
645 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822));
646 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20));
647 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825));
648 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00));