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>
32 #include <linux/hwmon.h>
36 #define W1_THERM_DS18S20 0x10
37 #define W1_THERM_DS1822 0x22
38 #define W1_THERM_DS18B20 0x28
39 #define W1_THERM_DS1825 0x3B
40 #define W1_THERM_DS28EA00 0x42
42 /* Allow the strong pullup to be disabled, but default to enabled.
43 * If it was disabled a parasite powered device might not get the require
44 * current to do a temperature conversion. If it is enabled parasite powered
45 * devices have a better chance of getting the current required.
46 * In case the parasite power-detection is not working (seems to be the case
47 * for some DS18S20) the strong pullup can also be forced, regardless of the
48 * power state of the devices.
51 * - strong_pullup = 0 Disable strong pullup completely
52 * - strong_pullup = 1 Enable automatic strong pullup detection
53 * - strong_pullup = 2 Force strong pullup
55 static int w1_strong_pullup = 1;
56 module_param_named(strong_pullup, w1_strong_pullup, int, 0);
58 struct w1_therm_family_data {
69 /* return the address of the refcnt in the family data */
70 #define THERM_REFCNT(family_data) \
71 (&((struct w1_therm_family_data *)family_data)->refcnt)
73 static int w1_therm_add_slave(struct w1_slave *sl)
75 sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
79 atomic_set(THERM_REFCNT(sl->family_data), 1);
83 static void w1_therm_remove_slave(struct w1_slave *sl)
85 int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
89 refcnt = atomic_read(THERM_REFCNT(sl->family_data));
91 kfree(sl->family_data);
92 sl->family_data = NULL;
95 static ssize_t w1_slave_show(struct device *device,
96 struct device_attribute *attr, char *buf);
98 static ssize_t w1_slave_store(struct device *device,
99 struct device_attribute *attr, const char *buf, size_t size);
101 static ssize_t w1_seq_show(struct device *device,
102 struct device_attribute *attr, char *buf);
104 static DEVICE_ATTR_RW(w1_slave);
105 static DEVICE_ATTR_RO(w1_seq);
107 static struct attribute *w1_therm_attrs[] = {
108 &dev_attr_w1_slave.attr,
112 static struct attribute *w1_ds28ea00_attrs[] = {
113 &dev_attr_w1_slave.attr,
114 &dev_attr_w1_seq.attr,
118 ATTRIBUTE_GROUPS(w1_therm);
119 ATTRIBUTE_GROUPS(w1_ds28ea00);
121 #if IS_REACHABLE(CONFIG_HWMON)
122 static int w1_read_temp(struct device *dev, u32 attr, int channel,
125 static umode_t w1_is_visible(const void *_data, enum hwmon_sensor_types type,
126 u32 attr, int channel)
128 return attr == hwmon_temp_input ? 0444 : 0;
131 static int w1_read(struct device *dev, enum hwmon_sensor_types type,
132 u32 attr, int channel, long *val)
136 return w1_read_temp(dev, attr, channel, val);
142 static const u32 w1_temp_config[] = {
147 static const struct hwmon_channel_info w1_temp = {
149 .config = w1_temp_config,
152 static const struct hwmon_channel_info *w1_info[] = {
157 static const struct hwmon_ops w1_hwmon_ops = {
158 .is_visible = w1_is_visible,
162 static const struct hwmon_chip_info w1_chip_info = {
163 .ops = &w1_hwmon_ops,
166 #define W1_CHIPINFO (&w1_chip_info)
168 #define W1_CHIPINFO NULL
171 static struct w1_family_ops w1_therm_fops = {
172 .add_slave = w1_therm_add_slave,
173 .remove_slave = w1_therm_remove_slave,
174 .groups = w1_therm_groups,
175 .chip_info = W1_CHIPINFO,
178 static struct w1_family_ops w1_ds28ea00_fops = {
179 .add_slave = w1_therm_add_slave,
180 .remove_slave = w1_therm_remove_slave,
181 .groups = w1_ds28ea00_groups,
182 .chip_info = W1_CHIPINFO,
185 static struct w1_family w1_therm_family_DS18S20 = {
186 .fid = W1_THERM_DS18S20,
187 .fops = &w1_therm_fops,
190 static struct w1_family w1_therm_family_DS18B20 = {
191 .fid = W1_THERM_DS18B20,
192 .fops = &w1_therm_fops,
195 static struct w1_family w1_therm_family_DS1822 = {
196 .fid = W1_THERM_DS1822,
197 .fops = &w1_therm_fops,
200 static struct w1_family w1_therm_family_DS28EA00 = {
201 .fid = W1_THERM_DS28EA00,
202 .fops = &w1_ds28ea00_fops,
205 static struct w1_family w1_therm_family_DS1825 = {
206 .fid = W1_THERM_DS1825,
207 .fops = &w1_therm_fops,
210 struct w1_therm_family_converter {
214 int (*convert)(u8 rom[9]);
215 int (*precision)(struct device *device, int val);
216 int (*eeprom)(struct device *device);
219 /* write configuration to eeprom */
220 static inline int w1_therm_eeprom(struct device *device);
222 /* Set precision for conversion */
223 static inline int w1_DS18B20_precision(struct device *device, int val);
224 static inline int w1_DS18S20_precision(struct device *device, int val);
226 /* The return value is millidegrees Centigrade. */
227 static inline int w1_DS18B20_convert_temp(u8 rom[9]);
228 static inline int w1_DS18S20_convert_temp(u8 rom[9]);
230 static struct w1_therm_family_converter w1_therm_families[] = {
232 .f = &w1_therm_family_DS18S20,
233 .convert = w1_DS18S20_convert_temp,
234 .precision = w1_DS18S20_precision,
235 .eeprom = w1_therm_eeprom
238 .f = &w1_therm_family_DS1822,
239 .convert = w1_DS18B20_convert_temp,
240 .precision = w1_DS18S20_precision,
241 .eeprom = w1_therm_eeprom
244 .f = &w1_therm_family_DS18B20,
245 .convert = w1_DS18B20_convert_temp,
246 .precision = w1_DS18B20_precision,
247 .eeprom = w1_therm_eeprom
250 .f = &w1_therm_family_DS28EA00,
251 .convert = w1_DS18B20_convert_temp,
252 .precision = w1_DS18S20_precision,
253 .eeprom = w1_therm_eeprom
256 .f = &w1_therm_family_DS1825,
257 .convert = w1_DS18B20_convert_temp,
258 .precision = w1_DS18S20_precision,
259 .eeprom = w1_therm_eeprom
263 static inline int w1_therm_eeprom(struct device *device)
265 struct w1_slave *sl = dev_to_w1_slave(device);
266 struct w1_master *dev = sl->master;
267 u8 rom[9], external_power;
268 int ret, max_trying = 10;
269 u8 *family_data = sl->family_data;
271 ret = mutex_lock_interruptible(&dev->bus_mutex);
275 if (!sl->family_data) {
280 /* prevent the slave from going away in sleep */
281 atomic_inc(THERM_REFCNT(family_data));
282 memset(rom, 0, sizeof(rom));
284 while (max_trying--) {
285 if (!w1_reset_select_slave(sl)) {
286 unsigned int tm = 10;
287 unsigned long sleep_rem;
289 /* check if in parasite mode */
290 w1_write_8(dev, W1_READ_PSUPPLY);
291 external_power = w1_read_8(dev);
293 if (w1_reset_select_slave(sl))
296 /* 10ms strong pullup/delay after the copy command */
297 if (w1_strong_pullup == 2 ||
298 (!external_power && w1_strong_pullup))
299 w1_next_pullup(dev, tm);
301 w1_write_8(dev, W1_COPY_SCRATCHPAD);
303 if (external_power) {
304 mutex_unlock(&dev->bus_mutex);
306 sleep_rem = msleep_interruptible(tm);
307 if (sleep_rem != 0) {
312 ret = mutex_lock_interruptible(&dev->bus_mutex);
315 } else if (!w1_strong_pullup) {
316 sleep_rem = msleep_interruptible(tm);
317 if (sleep_rem != 0) {
328 mutex_unlock(&dev->bus_mutex);
331 atomic_dec(THERM_REFCNT(family_data));
335 /* DS18S20 does not feature configuration register */
336 static inline int w1_DS18S20_precision(struct device *device, int val)
341 static inline int w1_DS18B20_precision(struct device *device, int val)
343 struct w1_slave *sl = dev_to_w1_slave(device);
344 struct w1_master *dev = sl->master;
346 int ret, max_trying = 10;
347 u8 *family_data = sl->family_data;
348 uint8_t precision_bits;
351 if (val > 12 || val < 9) {
352 pr_warn("Unsupported precision\n");
356 ret = mutex_lock_interruptible(&dev->bus_mutex);
360 if (!sl->family_data) {
365 /* prevent the slave from going away in sleep */
366 atomic_inc(THERM_REFCNT(family_data));
367 memset(rom, 0, sizeof(rom));
369 /* translate precision to bitmask (see datasheet page 9) */
372 precision_bits = 0x00;
375 precision_bits = 0x20;
378 precision_bits = 0x40;
382 precision_bits = 0x60;
386 while (max_trying--) {
389 if (!w1_reset_select_slave(sl)) {
392 /* read values to only alter precision bits */
393 w1_write_8(dev, W1_READ_SCRATCHPAD);
394 count = w1_read_block(dev, rom, 9);
396 dev_warn(device, "w1_read_block() returned %u instead of 9.\n", count);
398 crc = w1_calc_crc8(rom, 8);
400 rom[4] = (rom[4] & ~mask) | (precision_bits & mask);
402 if (!w1_reset_select_slave(sl)) {
403 w1_write_8(dev, W1_WRITE_SCRATCHPAD);
404 w1_write_8(dev, rom[2]);
405 w1_write_8(dev, rom[3]);
406 w1_write_8(dev, rom[4]);
415 mutex_unlock(&dev->bus_mutex);
418 atomic_dec(THERM_REFCNT(family_data));
422 static inline int w1_DS18B20_convert_temp(u8 rom[9])
424 s16 t = le16_to_cpup((__le16 *)rom);
429 static inline int w1_DS18S20_convert_temp(u8 rom[9])
437 t = ((s32)rom[0] >> 1)*1000;
439 t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
442 h = 1000*((s32)rom[7] - (s32)rom[6]);
449 static inline int w1_convert_temp(u8 rom[9], u8 fid)
453 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
454 if (w1_therm_families[i].f->fid == fid)
455 return w1_therm_families[i].convert(rom);
460 static ssize_t w1_slave_store(struct device *device,
461 struct device_attribute *attr, const char *buf,
465 struct w1_slave *sl = dev_to_w1_slave(device);
468 ret = kstrtoint(buf, 0, &val);
472 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
473 if (w1_therm_families[i].f->fid == sl->family->fid) {
474 /* zero value indicates to write current configuration to eeprom */
476 ret = w1_therm_families[i].eeprom(device);
478 ret = w1_therm_families[i].precision(device, val);
485 static ssize_t read_therm(struct device *device,
486 struct w1_slave *sl, struct therm_info *info)
488 struct w1_master *dev = sl->master;
490 int ret, max_trying = 10;
491 u8 *family_data = sl->family_data;
493 ret = mutex_lock_interruptible(&dev->bus_mutex);
502 /* prevent the slave from going away in sleep */
503 atomic_inc(THERM_REFCNT(family_data));
504 memset(info->rom, 0, sizeof(info->rom));
506 while (max_trying--) {
511 if (!w1_reset_select_slave(sl)) {
513 unsigned int tm = 750;
514 unsigned long sleep_rem;
516 w1_write_8(dev, W1_READ_PSUPPLY);
517 external_power = w1_read_8(dev);
519 if (w1_reset_select_slave(sl))
522 /* 750ms strong pullup (or delay) after the convert */
523 if (w1_strong_pullup == 2 ||
524 (!external_power && w1_strong_pullup))
525 w1_next_pullup(dev, tm);
527 w1_write_8(dev, W1_CONVERT_TEMP);
529 if (external_power) {
530 mutex_unlock(&dev->bus_mutex);
532 sleep_rem = msleep_interruptible(tm);
533 if (sleep_rem != 0) {
538 ret = mutex_lock_interruptible(&dev->bus_mutex);
541 } else if (!w1_strong_pullup) {
542 sleep_rem = msleep_interruptible(tm);
543 if (sleep_rem != 0) {
549 if (!w1_reset_select_slave(sl)) {
551 w1_write_8(dev, W1_READ_SCRATCHPAD);
552 count = w1_read_block(dev, info->rom, 9);
554 dev_warn(device, "w1_read_block() "
555 "returned %u instead of 9.\n",
559 info->crc = w1_calc_crc8(info->rom, 8);
561 if (info->rom[8] == info->crc)
571 atomic_dec(THERM_REFCNT(family_data));
573 mutex_unlock(&dev->bus_mutex);
578 static ssize_t w1_slave_show(struct device *device,
579 struct device_attribute *attr, char *buf)
581 struct w1_slave *sl = dev_to_w1_slave(device);
582 struct therm_info info;
583 u8 *family_data = sl->family_data;
585 ssize_t c = PAGE_SIZE;
586 u8 fid = sl->family->fid;
588 ret = read_therm(device, sl, &info);
592 for (i = 0; i < 9; ++i)
593 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]);
594 c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
595 info.crc, (info.verdict) ? "YES" : "NO");
597 memcpy(family_data, info.rom, sizeof(info.rom));
599 dev_warn(device, "Read failed CRC check\n");
601 for (i = 0; i < 9; ++i)
602 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
603 ((u8 *)family_data)[i]);
605 c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
606 w1_convert_temp(info.rom, fid));
611 #if IS_REACHABLE(CONFIG_HWMON)
612 static int w1_read_temp(struct device *device, u32 attr, int channel,
615 struct w1_slave *sl = dev_get_drvdata(device);
616 struct therm_info info;
617 u8 fid = sl->family->fid;
621 case hwmon_temp_input:
622 ret = read_therm(device, sl, &info);
631 *val = w1_convert_temp(info.rom, fid);
643 #define W1_42_CHAIN 0x99
644 #define W1_42_CHAIN_OFF 0x3C
645 #define W1_42_CHAIN_OFF_INV 0xC3
646 #define W1_42_CHAIN_ON 0x5A
647 #define W1_42_CHAIN_ON_INV 0xA5
648 #define W1_42_CHAIN_DONE 0x96
649 #define W1_42_CHAIN_DONE_INV 0x69
650 #define W1_42_COND_READ 0x0F
651 #define W1_42_SUCCESS_CONFIRM_BYTE 0xAA
652 #define W1_42_FINISHED_BYTE 0xFF
653 static ssize_t w1_seq_show(struct device *device,
654 struct device_attribute *attr, char *buf)
656 struct w1_slave *sl = dev_to_w1_slave(device);
657 ssize_t c = PAGE_SIZE;
662 struct w1_reg_num *reg_num;
665 mutex_lock(&sl->master->bus_mutex);
666 /* Place all devices in CHAIN state */
667 if (w1_reset_bus(sl->master))
669 w1_write_8(sl->master, W1_SKIP_ROM);
670 w1_write_8(sl->master, W1_42_CHAIN);
671 w1_write_8(sl->master, W1_42_CHAIN_ON);
672 w1_write_8(sl->master, W1_42_CHAIN_ON_INV);
673 msleep(sl->master->pullup_duration);
675 /* check for acknowledgment */
676 ack = w1_read_8(sl->master);
677 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
680 /* In case the bus fails to send 0xFF, limit*/
681 for (i = 0; i <= 64; i++) {
682 if (w1_reset_bus(sl->master))
685 w1_write_8(sl->master, W1_42_COND_READ);
686 rv = w1_read_block(sl->master, (u8 *)&rn, 8);
687 reg_num = (struct w1_reg_num *) &rn;
688 if (reg_num->family == W1_42_FINISHED_BYTE)
690 if (sl->reg_num.id == reg_num->id)
693 w1_write_8(sl->master, W1_42_CHAIN);
694 w1_write_8(sl->master, W1_42_CHAIN_DONE);
695 w1_write_8(sl->master, W1_42_CHAIN_DONE_INV);
696 w1_read_block(sl->master, &ack, sizeof(ack));
698 /* check for acknowledgment */
699 ack = w1_read_8(sl->master);
700 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
705 /* Exit from CHAIN state */
706 if (w1_reset_bus(sl->master))
708 w1_write_8(sl->master, W1_SKIP_ROM);
709 w1_write_8(sl->master, W1_42_CHAIN);
710 w1_write_8(sl->master, W1_42_CHAIN_OFF);
711 w1_write_8(sl->master, W1_42_CHAIN_OFF_INV);
713 /* check for acknowledgment */
714 ack = w1_read_8(sl->master);
715 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
717 mutex_unlock(&sl->master->bus_mutex);
719 c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq);
720 return PAGE_SIZE - c;
722 mutex_unlock(&sl->master->bus_mutex);
726 static int __init w1_therm_init(void)
730 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
731 err = w1_register_family(w1_therm_families[i].f);
733 w1_therm_families[i].broken = 1;
739 static void __exit w1_therm_fini(void)
743 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
744 if (!w1_therm_families[i].broken)
745 w1_unregister_family(w1_therm_families[i].f);
748 module_init(w1_therm_init);
749 module_exit(w1_therm_fini);
751 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
752 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
753 MODULE_LICENSE("GPL");
754 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20));
755 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822));
756 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20));
757 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825));
758 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00));