1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <asm/types.h>
23
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>
33
34#include <linux/w1.h>
35
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55static int w1_strong_pullup = 1;
56module_param_named(strong_pullup, w1_strong_pullup, int, 0);
57
58struct w1_therm_family_data {
59 uint8_t rom[9];
60 atomic_t refcnt;
61};
62
63struct therm_info {
64 u8 rom[9];
65 u8 crc;
66 u8 verdict;
67};
68
69
70#define THERM_REFCNT(family_data) \
71 (&((struct w1_therm_family_data *)family_data)->refcnt)
72
73static int w1_therm_add_slave(struct w1_slave *sl)
74{
75 sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
76 GFP_KERNEL);
77 if (!sl->family_data)
78 return -ENOMEM;
79 atomic_set(THERM_REFCNT(sl->family_data), 1);
80 return 0;
81}
82
83static void w1_therm_remove_slave(struct w1_slave *sl)
84{
85 int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
86
87 while (refcnt) {
88 msleep(1000);
89 refcnt = atomic_read(THERM_REFCNT(sl->family_data));
90 }
91 kfree(sl->family_data);
92 sl->family_data = NULL;
93}
94
95static ssize_t w1_slave_show(struct device *device,
96 struct device_attribute *attr, char *buf);
97
98static ssize_t w1_slave_store(struct device *device,
99 struct device_attribute *attr, const char *buf, size_t size);
100
101static ssize_t w1_seq_show(struct device *device,
102 struct device_attribute *attr, char *buf);
103
104static DEVICE_ATTR_RW(w1_slave);
105static DEVICE_ATTR_RO(w1_seq);
106
107static struct attribute *w1_therm_attrs[] = {
108 &dev_attr_w1_slave.attr,
109 NULL,
110};
111
112static struct attribute *w1_ds28ea00_attrs[] = {
113 &dev_attr_w1_slave.attr,
114 &dev_attr_w1_seq.attr,
115 NULL,
116};
117
118ATTRIBUTE_GROUPS(w1_therm);
119ATTRIBUTE_GROUPS(w1_ds28ea00);
120
121#if IS_REACHABLE(CONFIG_HWMON)
122static int w1_read_temp(struct device *dev, u32 attr, int channel,
123 long *val);
124
125static umode_t w1_is_visible(const void *_data, enum hwmon_sensor_types type,
126 u32 attr, int channel)
127{
128 return attr == hwmon_temp_input ? 0444 : 0;
129}
130
131static int w1_read(struct device *dev, enum hwmon_sensor_types type,
132 u32 attr, int channel, long *val)
133{
134 switch (type) {
135 case hwmon_temp:
136 return w1_read_temp(dev, attr, channel, val);
137 default:
138 return -EOPNOTSUPP;
139 }
140}
141
142static const u32 w1_temp_config[] = {
143 HWMON_T_INPUT,
144 0
145};
146
147static const struct hwmon_channel_info w1_temp = {
148 .type = hwmon_temp,
149 .config = w1_temp_config,
150};
151
152static const struct hwmon_channel_info *w1_info[] = {
153 &w1_temp,
154 NULL
155};
156
157static const struct hwmon_ops w1_hwmon_ops = {
158 .is_visible = w1_is_visible,
159 .read = w1_read,
160};
161
162static const struct hwmon_chip_info w1_chip_info = {
163 .ops = &w1_hwmon_ops,
164 .info = w1_info,
165};
166#define W1_CHIPINFO (&w1_chip_info)
167#else
168#define W1_CHIPINFO NULL
169#endif
170
171static 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,
176};
177
178static 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,
183};
184
185static struct w1_family w1_therm_family_DS18S20 = {
186 .fid = W1_THERM_DS18S20,
187 .fops = &w1_therm_fops,
188};
189
190static struct w1_family w1_therm_family_DS18B20 = {
191 .fid = W1_THERM_DS18B20,
192 .fops = &w1_therm_fops,
193};
194
195static struct w1_family w1_therm_family_DS1822 = {
196 .fid = W1_THERM_DS1822,
197 .fops = &w1_therm_fops,
198};
199
200static struct w1_family w1_therm_family_DS28EA00 = {
201 .fid = W1_THERM_DS28EA00,
202 .fops = &w1_ds28ea00_fops,
203};
204
205static struct w1_family w1_therm_family_DS1825 = {
206 .fid = W1_THERM_DS1825,
207 .fops = &w1_therm_fops,
208};
209
210struct w1_therm_family_converter {
211 u8 broken;
212 u16 reserved;
213 struct w1_family *f;
214 int (*convert)(u8 rom[9]);
215 int (*precision)(struct device *device, int val);
216 int (*eeprom)(struct device *device);
217};
218
219
220static inline int w1_therm_eeprom(struct device *device);
221
222
223static inline int w1_DS18B20_precision(struct device *device, int val);
224static inline int w1_DS18S20_precision(struct device *device, int val);
225
226
227static inline int w1_DS18B20_convert_temp(u8 rom[9]);
228static inline int w1_DS18S20_convert_temp(u8 rom[9]);
229
230static struct w1_therm_family_converter w1_therm_families[] = {
231 {
232 .f = &w1_therm_family_DS18S20,
233 .convert = w1_DS18S20_convert_temp,
234 .precision = w1_DS18S20_precision,
235 .eeprom = w1_therm_eeprom
236 },
237 {
238 .f = &w1_therm_family_DS1822,
239 .convert = w1_DS18B20_convert_temp,
240 .precision = w1_DS18S20_precision,
241 .eeprom = w1_therm_eeprom
242 },
243 {
244 .f = &w1_therm_family_DS18B20,
245 .convert = w1_DS18B20_convert_temp,
246 .precision = w1_DS18B20_precision,
247 .eeprom = w1_therm_eeprom
248 },
249 {
250 .f = &w1_therm_family_DS28EA00,
251 .convert = w1_DS18B20_convert_temp,
252 .precision = w1_DS18S20_precision,
253 .eeprom = w1_therm_eeprom
254 },
255 {
256 .f = &w1_therm_family_DS1825,
257 .convert = w1_DS18B20_convert_temp,
258 .precision = w1_DS18S20_precision,
259 .eeprom = w1_therm_eeprom
260 }
261};
262
263static inline int w1_therm_eeprom(struct device *device)
264{
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;
270
271 if (!sl->family_data) {
272 ret = -ENODEV;
273 goto error;
274 }
275
276
277 atomic_inc(THERM_REFCNT(family_data));
278
279 ret = mutex_lock_interruptible(&dev->bus_mutex);
280 if (ret != 0)
281 goto dec_refcnt;
282
283 memset(rom, 0, sizeof(rom));
284
285 while (max_trying--) {
286 if (!w1_reset_select_slave(sl)) {
287 unsigned int tm = 10;
288 unsigned long sleep_rem;
289
290
291 w1_write_8(dev, W1_READ_PSUPPLY);
292 external_power = w1_read_8(dev);
293
294 if (w1_reset_select_slave(sl))
295 continue;
296
297
298 if (w1_strong_pullup == 2 ||
299 (!external_power && w1_strong_pullup))
300 w1_next_pullup(dev, tm);
301
302 w1_write_8(dev, W1_COPY_SCRATCHPAD);
303
304 if (external_power) {
305 mutex_unlock(&dev->bus_mutex);
306
307 sleep_rem = msleep_interruptible(tm);
308 if (sleep_rem != 0) {
309 ret = -EINTR;
310 goto dec_refcnt;
311 }
312
313 ret = mutex_lock_interruptible(&dev->bus_mutex);
314 if (ret != 0)
315 goto dec_refcnt;
316 } else if (!w1_strong_pullup) {
317 sleep_rem = msleep_interruptible(tm);
318 if (sleep_rem != 0) {
319 ret = -EINTR;
320 goto mt_unlock;
321 }
322 }
323
324 break;
325 }
326 }
327
328mt_unlock:
329 mutex_unlock(&dev->bus_mutex);
330dec_refcnt:
331 atomic_dec(THERM_REFCNT(family_data));
332error:
333 return ret;
334}
335
336
337static inline int w1_DS18S20_precision(struct device *device, int val)
338{
339 return 0;
340}
341
342static inline int w1_DS18B20_precision(struct device *device, int val)
343{
344 struct w1_slave *sl = dev_to_w1_slave(device);
345 struct w1_master *dev = sl->master;
346 u8 rom[9], crc;
347 int ret, max_trying = 10;
348 u8 *family_data = sl->family_data;
349 uint8_t precision_bits;
350 uint8_t mask = 0x60;
351
352 if (val > 12 || val < 9) {
353 pr_warn("Unsupported precision\n");
354 ret = -EINVAL;
355 goto error;
356 }
357
358 if (!sl->family_data) {
359 ret = -ENODEV;
360 goto error;
361 }
362
363
364 atomic_inc(THERM_REFCNT(family_data));
365
366 ret = mutex_lock_interruptible(&dev->bus_mutex);
367 if (ret != 0)
368 goto dec_refcnt;
369
370 memset(rom, 0, sizeof(rom));
371
372
373 switch (val) {
374 case 9:
375 precision_bits = 0x00;
376 break;
377 case 10:
378 precision_bits = 0x20;
379 break;
380 case 11:
381 precision_bits = 0x40;
382 break;
383 case 12:
384 default:
385 precision_bits = 0x60;
386 break;
387 }
388
389 while (max_trying--) {
390 crc = 0;
391
392 if (!w1_reset_select_slave(sl)) {
393 int count = 0;
394
395
396 w1_write_8(dev, W1_READ_SCRATCHPAD);
397 count = w1_read_block(dev, rom, 9);
398 if (count != 9)
399 dev_warn(device, "w1_read_block() returned %u instead of 9.\n", count);
400
401 crc = w1_calc_crc8(rom, 8);
402 if (rom[8] == crc) {
403 rom[4] = (rom[4] & ~mask) | (precision_bits & mask);
404
405 if (!w1_reset_select_slave(sl)) {
406 w1_write_8(dev, W1_WRITE_SCRATCHPAD);
407 w1_write_8(dev, rom[2]);
408 w1_write_8(dev, rom[3]);
409 w1_write_8(dev, rom[4]);
410
411 break;
412 }
413 }
414 }
415 }
416
417 mutex_unlock(&dev->bus_mutex);
418dec_refcnt:
419 atomic_dec(THERM_REFCNT(family_data));
420error:
421 return ret;
422}
423
424static inline int w1_DS18B20_convert_temp(u8 rom[9])
425{
426 s16 t = le16_to_cpup((__le16 *)rom);
427
428 return t*1000/16;
429}
430
431static inline int w1_DS18S20_convert_temp(u8 rom[9])
432{
433 int t, h;
434
435 if (!rom[7])
436 return 0;
437
438 if (rom[1] == 0)
439 t = ((s32)rom[0] >> 1)*1000;
440 else
441 t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
442
443 t -= 250;
444 h = 1000*((s32)rom[7] - (s32)rom[6]);
445 h /= (s32)rom[7];
446 t += h;
447
448 return t;
449}
450
451static inline int w1_convert_temp(u8 rom[9], u8 fid)
452{
453 int i;
454
455 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
456 if (w1_therm_families[i].f->fid == fid)
457 return w1_therm_families[i].convert(rom);
458
459 return 0;
460}
461
462static ssize_t w1_slave_store(struct device *device,
463 struct device_attribute *attr, const char *buf,
464 size_t size)
465{
466 int val, ret;
467 struct w1_slave *sl = dev_to_w1_slave(device);
468 int i;
469
470 ret = kstrtoint(buf, 0, &val);
471 if (ret)
472 return ret;
473
474 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
475 if (w1_therm_families[i].f->fid == sl->family->fid) {
476
477 if (val == 0)
478 ret = w1_therm_families[i].eeprom(device);
479 else
480 ret = w1_therm_families[i].precision(device, val);
481 break;
482 }
483 }
484 return ret ? : size;
485}
486
487static ssize_t read_therm(struct device *device,
488 struct w1_slave *sl, struct therm_info *info)
489{
490 struct w1_master *dev = sl->master;
491 u8 external_power;
492 int ret, max_trying = 10;
493 u8 *family_data = sl->family_data;
494
495 if (!family_data) {
496 ret = -ENODEV;
497 goto error;
498 }
499
500
501 atomic_inc(THERM_REFCNT(family_data));
502
503 ret = mutex_lock_interruptible(&dev->bus_mutex);
504 if (ret != 0)
505 goto dec_refcnt;
506
507 memset(info->rom, 0, sizeof(info->rom));
508
509 while (max_trying--) {
510
511 info->verdict = 0;
512 info->crc = 0;
513
514 if (!w1_reset_select_slave(sl)) {
515 int count = 0;
516 unsigned int tm = 750;
517 unsigned long sleep_rem;
518
519 w1_write_8(dev, W1_READ_PSUPPLY);
520 external_power = w1_read_8(dev);
521
522 if (w1_reset_select_slave(sl))
523 continue;
524
525
526 if (w1_strong_pullup == 2 ||
527 (!external_power && w1_strong_pullup))
528 w1_next_pullup(dev, tm);
529
530 w1_write_8(dev, W1_CONVERT_TEMP);
531
532 if (external_power) {
533 mutex_unlock(&dev->bus_mutex);
534
535 sleep_rem = msleep_interruptible(tm);
536 if (sleep_rem != 0) {
537 ret = -EINTR;
538 goto dec_refcnt;
539 }
540
541 ret = mutex_lock_interruptible(&dev->bus_mutex);
542 if (ret != 0)
543 goto dec_refcnt;
544 } else if (!w1_strong_pullup) {
545 sleep_rem = msleep_interruptible(tm);
546 if (sleep_rem != 0) {
547 ret = -EINTR;
548 goto mt_unlock;
549 }
550 }
551
552 if (!w1_reset_select_slave(sl)) {
553
554 w1_write_8(dev, W1_READ_SCRATCHPAD);
555 count = w1_read_block(dev, info->rom, 9);
556 if (count != 9) {
557 dev_warn(device, "w1_read_block() "
558 "returned %u instead of 9.\n",
559 count);
560 }
561
562 info->crc = w1_calc_crc8(info->rom, 8);
563
564 if (info->rom[8] == info->crc)
565 info->verdict = 1;
566 }
567 }
568
569 if (info->verdict)
570 break;
571 }
572
573mt_unlock:
574 mutex_unlock(&dev->bus_mutex);
575dec_refcnt:
576 atomic_dec(THERM_REFCNT(family_data));
577error:
578 return ret;
579}
580
581static ssize_t w1_slave_show(struct device *device,
582 struct device_attribute *attr, char *buf)
583{
584 struct w1_slave *sl = dev_to_w1_slave(device);
585 struct therm_info info;
586 u8 *family_data = sl->family_data;
587 int ret, i;
588 ssize_t c = PAGE_SIZE;
589 u8 fid = sl->family->fid;
590
591 ret = read_therm(device, sl, &info);
592 if (ret)
593 return ret;
594
595 for (i = 0; i < 9; ++i)
596 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]);
597 c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
598 info.crc, (info.verdict) ? "YES" : "NO");
599 if (info.verdict)
600 memcpy(family_data, info.rom, sizeof(info.rom));
601 else
602 dev_warn(device, "Read failed CRC check\n");
603
604 for (i = 0; i < 9; ++i)
605 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
606 ((u8 *)family_data)[i]);
607
608 c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
609 w1_convert_temp(info.rom, fid));
610 ret = PAGE_SIZE - c;
611 return ret;
612}
613
614#if IS_REACHABLE(CONFIG_HWMON)
615static int w1_read_temp(struct device *device, u32 attr, int channel,
616 long *val)
617{
618 struct w1_slave *sl = dev_get_drvdata(device);
619 struct therm_info info;
620 u8 fid = sl->family->fid;
621 int ret;
622
623 switch (attr) {
624 case hwmon_temp_input:
625 ret = read_therm(device, sl, &info);
626 if (ret)
627 return ret;
628
629 if (!info.verdict) {
630 ret = -EIO;
631 return ret;
632 }
633
634 *val = w1_convert_temp(info.rom, fid);
635 ret = 0;
636 break;
637 default:
638 ret = -EOPNOTSUPP;
639 break;
640 }
641
642 return ret;
643}
644#endif
645
646#define W1_42_CHAIN 0x99
647#define W1_42_CHAIN_OFF 0x3C
648#define W1_42_CHAIN_OFF_INV 0xC3
649#define W1_42_CHAIN_ON 0x5A
650#define W1_42_CHAIN_ON_INV 0xA5
651#define W1_42_CHAIN_DONE 0x96
652#define W1_42_CHAIN_DONE_INV 0x69
653#define W1_42_COND_READ 0x0F
654#define W1_42_SUCCESS_CONFIRM_BYTE 0xAA
655#define W1_42_FINISHED_BYTE 0xFF
656static ssize_t w1_seq_show(struct device *device,
657 struct device_attribute *attr, char *buf)
658{
659 struct w1_slave *sl = dev_to_w1_slave(device);
660 ssize_t c = PAGE_SIZE;
661 int rv;
662 int i;
663 u8 ack;
664 u64 rn;
665 struct w1_reg_num *reg_num;
666 int seq = 0;
667
668 mutex_lock(&sl->master->bus_mutex);
669
670 if (w1_reset_bus(sl->master))
671 goto error;
672 w1_write_8(sl->master, W1_SKIP_ROM);
673 w1_write_8(sl->master, W1_42_CHAIN);
674 w1_write_8(sl->master, W1_42_CHAIN_ON);
675 w1_write_8(sl->master, W1_42_CHAIN_ON_INV);
676 msleep(sl->master->pullup_duration);
677
678
679 ack = w1_read_8(sl->master);
680 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
681 goto error;
682
683
684 for (i = 0; i <= 64; i++) {
685 if (w1_reset_bus(sl->master))
686 goto error;
687
688 w1_write_8(sl->master, W1_42_COND_READ);
689 rv = w1_read_block(sl->master, (u8 *)&rn, 8);
690 reg_num = (struct w1_reg_num *) &rn;
691 if (reg_num->family == W1_42_FINISHED_BYTE)
692 break;
693 if (sl->reg_num.id == reg_num->id)
694 seq = i;
695
696 w1_write_8(sl->master, W1_42_CHAIN);
697 w1_write_8(sl->master, W1_42_CHAIN_DONE);
698 w1_write_8(sl->master, W1_42_CHAIN_DONE_INV);
699 w1_read_block(sl->master, &ack, sizeof(ack));
700
701
702 ack = w1_read_8(sl->master);
703 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
704 goto error;
705
706 }
707
708
709 if (w1_reset_bus(sl->master))
710 goto error;
711 w1_write_8(sl->master, W1_SKIP_ROM);
712 w1_write_8(sl->master, W1_42_CHAIN);
713 w1_write_8(sl->master, W1_42_CHAIN_OFF);
714 w1_write_8(sl->master, W1_42_CHAIN_OFF_INV);
715
716
717 ack = w1_read_8(sl->master);
718 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
719 goto error;
720 mutex_unlock(&sl->master->bus_mutex);
721
722 c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq);
723 return PAGE_SIZE - c;
724error:
725 mutex_unlock(&sl->master->bus_mutex);
726 return -EIO;
727}
728
729static int __init w1_therm_init(void)
730{
731 int err, i;
732
733 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
734 err = w1_register_family(w1_therm_families[i].f);
735 if (err)
736 w1_therm_families[i].broken = 1;
737 }
738
739 return 0;
740}
741
742static void __exit w1_therm_fini(void)
743{
744 int i;
745
746 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
747 if (!w1_therm_families[i].broken)
748 w1_unregister_family(w1_therm_families[i].f);
749}
750
751module_init(w1_therm_init);
752module_exit(w1_therm_fini);
753
754MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
755MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
756MODULE_LICENSE("GPL");
757MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20));
758MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822));
759MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20));
760MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825));
761MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00));
762