1
2
3
4
5
6
7
8#include <asm/types.h>
9
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>
19#include <linux/string.h>
20#include <linux/jiffies.h>
21
22#include <linux/w1.h>
23
24#define W1_THERM_DS18S20 0x10
25#define W1_THERM_DS1822 0x22
26#define W1_THERM_DS18B20 0x28
27#define W1_THERM_DS1825 0x3B
28#define W1_THERM_DS28EA00 0x42
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44static int w1_strong_pullup = 1;
45module_param_named(strong_pullup, w1_strong_pullup, int, 0);
46
47
48static u16 bulk_read_device_counter;
49
50
51#define W1_RECALL_EEPROM 0xB8
52
53
54#define W1_THERM_MAX_TRY 5
55
56
57#define W1_THERM_RETRY_DELAY 20
58
59
60#define W1_THERM_EEPROM_WRITE_DELAY 10
61
62#define EEPROM_CMD_WRITE "save"
63#define EEPROM_CMD_READ "restore"
64#define BULK_TRIGGER_CMD "trigger"
65
66#define MIN_TEMP -55
67#define MAX_TEMP 125
68
69
70#define CONV_TIME_DEFAULT 0
71#define CONV_TIME_MEASURE 1
72
73
74#define W1_THERM_CHECK_RESULT 1
75#define W1_THERM_POLL_COMPLETION 2
76#define W1_THERM_FEATURES_MASK 3
77
78
79#define W1_POLL_PERIOD 32
80#define W1_POLL_CONVERT_TEMP 2000
81#define W1_POLL_RECALL_EEPROM 500
82
83
84
85#define W1_THERM_RESOLUTION_MASK 0xE0
86
87#define W1_THERM_RESOLUTION_SHIFT 5
88
89#define W1_THERM_RESOLUTION_SHIFT 5
90
91#define W1_THERM_RESOLUTION_MIN 9
92
93#define W1_THERM_RESOLUTION_MAX 14
94
95
96
97
98
99
100
101#define SLAVE_SPECIFIC_FUNC(sl) \
102 (((struct w1_therm_family_data *)(sl->family_data))->specific_functions)
103
104
105
106
107
108#define SLAVE_POWERMODE(sl) \
109 (((struct w1_therm_family_data *)(sl->family_data))->external_powered)
110
111
112
113
114
115#define SLAVE_RESOLUTION(sl) \
116 (((struct w1_therm_family_data *)(sl->family_data))->resolution)
117
118
119
120
121
122 #define SLAVE_CONV_TIME_OVERRIDE(sl) \
123 (((struct w1_therm_family_data *)(sl->family_data))->conv_time_override)
124
125
126
127
128
129 #define SLAVE_FEATURES(sl) \
130 (((struct w1_therm_family_data *)(sl->family_data))->features)
131
132
133
134
135
136
137
138#define SLAVE_CONVERT_TRIGGERED(sl) \
139 (((struct w1_therm_family_data *)(sl->family_data))->convert_triggered)
140
141
142#define THERM_REFCNT(family_data) \
143 (&((struct w1_therm_family_data *)family_data)->refcnt)
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159struct w1_therm_family_converter {
160 u8 broken;
161 u16 reserved;
162 struct w1_family *f;
163 int (*convert)(u8 rom[9]);
164 int (*get_conversion_time)(struct w1_slave *sl);
165 int (*set_resolution)(struct w1_slave *sl, int val);
166 int (*get_resolution)(struct w1_slave *sl);
167 int (*write_data)(struct w1_slave *sl, const u8 *data);
168 bool bulk_read;
169};
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184struct w1_therm_family_data {
185 uint8_t rom[9];
186 atomic_t refcnt;
187 int external_powered;
188 int resolution;
189 int convert_triggered;
190 int conv_time_override;
191 unsigned int features;
192 struct w1_therm_family_converter *specific_functions;
193};
194
195
196
197
198
199
200
201struct therm_info {
202 u8 rom[9];
203 u8 crc;
204 u8 verdict;
205};
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223static int reset_select_slave(struct w1_slave *sl);
224
225
226
227
228
229
230
231
232static int convert_t(struct w1_slave *sl, struct therm_info *info);
233
234
235
236
237
238
239
240
241static int read_scratchpad(struct w1_slave *sl, struct therm_info *info);
242
243
244
245
246
247
248
249
250
251static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes);
252
253
254
255
256
257
258
259static int copy_scratchpad(struct w1_slave *sl);
260
261
262
263
264
265
266
267static int recall_eeprom(struct w1_slave *sl);
268
269
270
271
272
273
274
275
276
277
278
279
280
281static int read_powermode(struct w1_slave *sl);
282
283
284
285
286
287
288
289
290
291
292
293static int trigger_bulk_read(struct w1_master *dev_master);
294
295
296
297static ssize_t w1_slave_show(struct device *device,
298 struct device_attribute *attr, char *buf);
299
300static ssize_t w1_slave_store(struct device *device,
301 struct device_attribute *attr, const char *buf, size_t size);
302
303static ssize_t w1_seq_show(struct device *device,
304 struct device_attribute *attr, char *buf);
305
306static ssize_t temperature_show(struct device *device,
307 struct device_attribute *attr, char *buf);
308
309static ssize_t ext_power_show(struct device *device,
310 struct device_attribute *attr, char *buf);
311
312static ssize_t resolution_show(struct device *device,
313 struct device_attribute *attr, char *buf);
314
315static ssize_t resolution_store(struct device *device,
316 struct device_attribute *attr, const char *buf, size_t size);
317
318static ssize_t eeprom_cmd_store(struct device *device,
319 struct device_attribute *attr, const char *buf, size_t size);
320
321static ssize_t alarms_store(struct device *device,
322 struct device_attribute *attr, const char *buf, size_t size);
323
324static ssize_t alarms_show(struct device *device,
325 struct device_attribute *attr, char *buf);
326
327static ssize_t therm_bulk_read_store(struct device *device,
328 struct device_attribute *attr, const char *buf, size_t size);
329
330static ssize_t therm_bulk_read_show(struct device *device,
331 struct device_attribute *attr, char *buf);
332
333static ssize_t conv_time_show(struct device *device,
334 struct device_attribute *attr, char *buf);
335
336static ssize_t conv_time_store(struct device *device,
337 struct device_attribute *attr, const char *buf,
338 size_t size);
339
340static ssize_t features_show(struct device *device,
341 struct device_attribute *attr, char *buf);
342
343static ssize_t features_store(struct device *device,
344 struct device_attribute *attr, const char *buf,
345 size_t size);
346
347
348static DEVICE_ATTR_RW(w1_slave);
349static DEVICE_ATTR_RO(w1_seq);
350static DEVICE_ATTR_RO(temperature);
351static DEVICE_ATTR_RO(ext_power);
352static DEVICE_ATTR_RW(resolution);
353static DEVICE_ATTR_WO(eeprom_cmd);
354static DEVICE_ATTR_RW(alarms);
355static DEVICE_ATTR_RW(conv_time);
356static DEVICE_ATTR_RW(features);
357
358static DEVICE_ATTR_RW(therm_bulk_read);
359
360
361
362
363
364
365
366
367
368
369
370
371static int w1_therm_add_slave(struct w1_slave *sl);
372
373
374
375
376
377
378
379
380static void w1_therm_remove_slave(struct w1_slave *sl);
381
382
383
384static struct attribute *w1_therm_attrs[] = {
385 &dev_attr_w1_slave.attr,
386 &dev_attr_temperature.attr,
387 &dev_attr_ext_power.attr,
388 &dev_attr_resolution.attr,
389 &dev_attr_eeprom_cmd.attr,
390 &dev_attr_alarms.attr,
391 &dev_attr_conv_time.attr,
392 &dev_attr_features.attr,
393 NULL,
394};
395
396static struct attribute *w1_ds18s20_attrs[] = {
397 &dev_attr_w1_slave.attr,
398 &dev_attr_temperature.attr,
399 &dev_attr_ext_power.attr,
400 &dev_attr_eeprom_cmd.attr,
401 &dev_attr_alarms.attr,
402 &dev_attr_conv_time.attr,
403 &dev_attr_features.attr,
404 NULL,
405};
406
407static struct attribute *w1_ds28ea00_attrs[] = {
408 &dev_attr_w1_slave.attr,
409 &dev_attr_w1_seq.attr,
410 &dev_attr_temperature.attr,
411 &dev_attr_ext_power.attr,
412 &dev_attr_resolution.attr,
413 &dev_attr_eeprom_cmd.attr,
414 &dev_attr_alarms.attr,
415 &dev_attr_conv_time.attr,
416 &dev_attr_features.attr,
417 NULL,
418};
419
420
421
422ATTRIBUTE_GROUPS(w1_therm);
423ATTRIBUTE_GROUPS(w1_ds18s20);
424ATTRIBUTE_GROUPS(w1_ds28ea00);
425
426#if IS_REACHABLE(CONFIG_HWMON)
427static int w1_read_temp(struct device *dev, u32 attr, int channel,
428 long *val);
429
430static umode_t w1_is_visible(const void *_data, enum hwmon_sensor_types type,
431 u32 attr, int channel)
432{
433 return attr == hwmon_temp_input ? 0444 : 0;
434}
435
436static int w1_read(struct device *dev, enum hwmon_sensor_types type,
437 u32 attr, int channel, long *val)
438{
439 switch (type) {
440 case hwmon_temp:
441 return w1_read_temp(dev, attr, channel, val);
442 default:
443 return -EOPNOTSUPP;
444 }
445}
446
447static const u32 w1_temp_config[] = {
448 HWMON_T_INPUT,
449 0
450};
451
452static const struct hwmon_channel_info w1_temp = {
453 .type = hwmon_temp,
454 .config = w1_temp_config,
455};
456
457static const struct hwmon_channel_info *w1_info[] = {
458 &w1_temp,
459 NULL
460};
461
462static const struct hwmon_ops w1_hwmon_ops = {
463 .is_visible = w1_is_visible,
464 .read = w1_read,
465};
466
467static const struct hwmon_chip_info w1_chip_info = {
468 .ops = &w1_hwmon_ops,
469 .info = w1_info,
470};
471#define W1_CHIPINFO (&w1_chip_info)
472#else
473#define W1_CHIPINFO NULL
474#endif
475
476
477
478static const struct w1_family_ops w1_therm_fops = {
479 .add_slave = w1_therm_add_slave,
480 .remove_slave = w1_therm_remove_slave,
481 .groups = w1_therm_groups,
482 .chip_info = W1_CHIPINFO,
483};
484
485static const struct w1_family_ops w1_ds18s20_fops = {
486 .add_slave = w1_therm_add_slave,
487 .remove_slave = w1_therm_remove_slave,
488 .groups = w1_ds18s20_groups,
489 .chip_info = W1_CHIPINFO,
490};
491
492static const struct w1_family_ops w1_ds28ea00_fops = {
493 .add_slave = w1_therm_add_slave,
494 .remove_slave = w1_therm_remove_slave,
495 .groups = w1_ds28ea00_groups,
496 .chip_info = W1_CHIPINFO,
497};
498
499
500
501static struct w1_family w1_therm_family_DS18S20 = {
502 .fid = W1_THERM_DS18S20,
503 .fops = &w1_ds18s20_fops,
504};
505
506static struct w1_family w1_therm_family_DS18B20 = {
507 .fid = W1_THERM_DS18B20,
508 .fops = &w1_therm_fops,
509};
510
511static struct w1_family w1_therm_family_DS1822 = {
512 .fid = W1_THERM_DS1822,
513 .fops = &w1_therm_fops,
514};
515
516static struct w1_family w1_therm_family_DS28EA00 = {
517 .fid = W1_THERM_DS28EA00,
518 .fops = &w1_ds28ea00_fops,
519};
520
521static struct w1_family w1_therm_family_DS1825 = {
522 .fid = W1_THERM_DS1825,
523 .fops = &w1_therm_fops,
524};
525
526
527
528static inline int w1_DS18B20_convert_time(struct w1_slave *sl)
529{
530 int ret;
531
532 if (!sl->family_data)
533 return -ENODEV;
534
535 if (SLAVE_CONV_TIME_OVERRIDE(sl) != CONV_TIME_DEFAULT)
536 return SLAVE_CONV_TIME_OVERRIDE(sl);
537
538
539
540
541 switch (SLAVE_RESOLUTION(sl)) {
542 case 9:
543 ret = 95;
544 break;
545 case 10:
546 ret = 190;
547 break;
548 case 11:
549 ret = 375;
550 break;
551 case 12:
552 ret = 750;
553 break;
554 case 13:
555 ret = 850;
556 break;
557 case 14:
558 ret = 1600;
559 break;
560 default:
561 ret = 750;
562 }
563 return ret;
564}
565
566static inline int w1_DS18S20_convert_time(struct w1_slave *sl)
567{
568 if (!sl->family_data)
569 return -ENODEV;
570
571 if (SLAVE_CONV_TIME_OVERRIDE(sl) == CONV_TIME_DEFAULT)
572 return 750;
573 else
574 return SLAVE_CONV_TIME_OVERRIDE(sl);
575}
576
577static inline int w1_DS18B20_write_data(struct w1_slave *sl,
578 const u8 *data)
579{
580 return write_scratchpad(sl, data, 3);
581}
582
583static inline int w1_DS18S20_write_data(struct w1_slave *sl,
584 const u8 *data)
585{
586
587 return write_scratchpad(sl, data, 2);
588}
589
590static inline int w1_DS18B20_set_resolution(struct w1_slave *sl, int val)
591{
592 int ret;
593 struct therm_info info, info2;
594
595
596
597 if (val < W1_THERM_RESOLUTION_MIN || val > W1_THERM_RESOLUTION_MAX)
598 return -EINVAL;
599
600
601 val = (val - W1_THERM_RESOLUTION_MIN) << W1_THERM_RESOLUTION_SHIFT;
602
603
604
605
606
607 ret = read_scratchpad(sl, &info);
608
609 if (ret)
610 return ret;
611
612
613 info.rom[4] &= ~W1_THERM_RESOLUTION_MASK;
614 info.rom[4] |= val;
615
616
617 ret = w1_DS18B20_write_data(sl, info.rom + 2);
618 if (ret)
619 return ret;
620
621
622
623
624
625 ret = read_scratchpad(sl, &info2);
626 if (ret)
627
628 return ret;
629
630 if ((info2.rom[4] & W1_THERM_RESOLUTION_MASK) == (info.rom[4] & W1_THERM_RESOLUTION_MASK))
631 return 0;
632
633
634 return -EIO;
635}
636
637static inline int w1_DS18B20_get_resolution(struct w1_slave *sl)
638{
639 int ret;
640 int resolution;
641 struct therm_info info;
642
643 ret = read_scratchpad(sl, &info);
644
645 if (ret)
646 return ret;
647
648 resolution = ((info.rom[4] & W1_THERM_RESOLUTION_MASK) >> W1_THERM_RESOLUTION_SHIFT)
649 + W1_THERM_RESOLUTION_MIN;
650
651
652
653
654 if (resolution > W1_THERM_RESOLUTION_MAX)
655 resolution = W1_THERM_RESOLUTION_MAX;
656
657 return resolution;
658}
659
660
661
662
663
664
665
666
667
668static inline int w1_DS18B20_convert_temp(u8 rom[9])
669{
670 u16 bv;
671 s16 t;
672
673
674 bv = le16_to_cpup((__le16 *)rom);
675
676
677 if (rom[4] & 0x80) {
678
679
680 bv = (bv << 2) | (rom[4] & 3);
681 t = (s16) bv;
682 return (int)t * 1000 / 64;
683 }
684 t = (s16)bv;
685 return (int)t * 1000 / 16;
686}
687
688
689
690
691
692
693
694
695
696static inline int w1_DS18S20_convert_temp(u8 rom[9])
697{
698 int t, h;
699
700 if (!rom[7]) {
701 pr_debug("%s: Invalid argument for conversion\n", __func__);
702 return 0;
703 }
704
705 if (rom[1] == 0)
706 t = ((s32)rom[0] >> 1)*1000;
707 else
708 t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
709
710 t -= 250;
711 h = 1000*((s32)rom[7] - (s32)rom[6]);
712 h /= (s32)rom[7];
713 t += h;
714
715 return t;
716}
717
718
719
720
721static struct w1_therm_family_converter w1_therm_families[] = {
722 {
723 .f = &w1_therm_family_DS18S20,
724 .convert = w1_DS18S20_convert_temp,
725 .get_conversion_time = w1_DS18S20_convert_time,
726 .set_resolution = NULL,
727 .get_resolution = NULL,
728 .write_data = w1_DS18S20_write_data,
729 .bulk_read = true
730 },
731 {
732 .f = &w1_therm_family_DS1822,
733 .convert = w1_DS18B20_convert_temp,
734 .get_conversion_time = w1_DS18B20_convert_time,
735 .set_resolution = w1_DS18B20_set_resolution,
736 .get_resolution = w1_DS18B20_get_resolution,
737 .write_data = w1_DS18B20_write_data,
738 .bulk_read = true
739 },
740 {
741
742 .f = &w1_therm_family_DS18B20,
743 .convert = w1_DS18B20_convert_temp,
744 .get_conversion_time = w1_DS18B20_convert_time,
745 .set_resolution = w1_DS18B20_set_resolution,
746 .get_resolution = w1_DS18B20_get_resolution,
747 .write_data = w1_DS18B20_write_data,
748 .bulk_read = true
749 },
750 {
751 .f = &w1_therm_family_DS28EA00,
752 .convert = w1_DS18B20_convert_temp,
753 .get_conversion_time = w1_DS18B20_convert_time,
754 .set_resolution = w1_DS18B20_set_resolution,
755 .get_resolution = w1_DS18B20_get_resolution,
756 .write_data = w1_DS18B20_write_data,
757 .bulk_read = false
758 },
759 {
760 .f = &w1_therm_family_DS1825,
761 .convert = w1_DS18B20_convert_temp,
762 .get_conversion_time = w1_DS18B20_convert_time,
763 .set_resolution = w1_DS18B20_set_resolution,
764 .get_resolution = w1_DS18B20_get_resolution,
765 .write_data = w1_DS18B20_write_data,
766 .bulk_read = true
767 }
768};
769
770
771
772
773
774
775
776
777
778static struct w1_therm_family_converter *device_family(struct w1_slave *sl)
779{
780 struct w1_therm_family_converter *ret = NULL;
781 int i;
782
783 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
784 if (w1_therm_families[i].f->fid == sl->family->fid) {
785 ret = &w1_therm_families[i];
786 break;
787 }
788 }
789 return ret;
790}
791
792
793
794
795
796
797
798
799
800
801static inline bool bus_mutex_lock(struct mutex *lock)
802{
803 int max_trying = W1_THERM_MAX_TRY;
804
805
806 while (mutex_lock_interruptible(lock) != 0 && max_trying > 0) {
807 unsigned long sleep_rem;
808
809 sleep_rem = msleep_interruptible(W1_THERM_RETRY_DELAY);
810 if (!sleep_rem)
811 max_trying--;
812 }
813
814 if (!max_trying)
815 return false;
816
817 return true;
818}
819
820
821
822
823
824
825
826static int check_family_data(struct w1_slave *sl)
827{
828 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
829 dev_info(&sl->dev,
830 "%s: Device is not supported by the driver\n", __func__);
831 return -EINVAL;
832 }
833 return 0;
834}
835
836
837
838
839
840
841
842static inline bool bulk_read_support(struct w1_slave *sl)
843{
844 if (SLAVE_SPECIFIC_FUNC(sl))
845 return SLAVE_SPECIFIC_FUNC(sl)->bulk_read;
846
847 dev_info(&sl->dev,
848 "%s: Device not supported by the driver\n", __func__);
849
850 return false;
851}
852
853
854
855
856
857
858
859
860
861
862
863static inline int conversion_time(struct w1_slave *sl)
864{
865 if (SLAVE_SPECIFIC_FUNC(sl))
866 return SLAVE_SPECIFIC_FUNC(sl)->get_conversion_time(sl);
867
868 dev_info(&sl->dev,
869 "%s: Device not supported by the driver\n", __func__);
870
871 return -ENODEV;
872}
873
874
875
876
877
878
879
880
881
882
883static inline int temperature_from_RAM(struct w1_slave *sl, u8 rom[9])
884{
885 if (SLAVE_SPECIFIC_FUNC(sl))
886 return SLAVE_SPECIFIC_FUNC(sl)->convert(rom);
887
888 dev_info(&sl->dev,
889 "%s: Device not supported by the driver\n", __func__);
890
891 return 0;
892}
893
894
895
896
897
898
899
900
901
902
903
904
905
906static inline s8 int_to_short(int i)
907{
908
909 i = clamp(i, MIN_TEMP, MAX_TEMP);
910 return (s8) i;
911}
912
913
914
915static int w1_therm_add_slave(struct w1_slave *sl)
916{
917 struct w1_therm_family_converter *sl_family_conv;
918
919
920 sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
921 GFP_KERNEL);
922 if (!sl->family_data)
923 return -ENOMEM;
924
925 atomic_set(THERM_REFCNT(sl->family_data), 1);
926
927
928 sl_family_conv = device_family(sl);
929 if (!sl_family_conv) {
930 kfree(sl->family_data);
931 return -ENODEV;
932 }
933
934 SLAVE_SPECIFIC_FUNC(sl) = sl_family_conv;
935
936 if (bulk_read_support(sl)) {
937
938
939
940
941 if (!bulk_read_device_counter) {
942 int err = device_create_file(&sl->master->dev,
943 &dev_attr_therm_bulk_read);
944
945 if (err)
946 dev_warn(&sl->dev,
947 "%s: Device has been added, but bulk read is unavailable. err=%d\n",
948 __func__, err);
949 }
950
951 bulk_read_device_counter++;
952 }
953
954
955 SLAVE_POWERMODE(sl) = read_powermode(sl);
956
957 if (SLAVE_POWERMODE(sl) < 0) {
958
959 dev_warn(&sl->dev,
960 "%s: Device has been added, but power_mode may be corrupted. err=%d\n",
961 __func__, SLAVE_POWERMODE(sl));
962 }
963
964
965 if (SLAVE_SPECIFIC_FUNC(sl)->get_resolution) {
966 SLAVE_RESOLUTION(sl) =
967 SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl);
968 if (SLAVE_RESOLUTION(sl) < 0) {
969
970 dev_warn(&sl->dev,
971 "%s:Device has been added, but resolution may be corrupted. err=%d\n",
972 __func__, SLAVE_RESOLUTION(sl));
973 }
974 }
975
976
977 SLAVE_CONVERT_TRIGGERED(sl) = 0;
978
979 return 0;
980}
981
982static void w1_therm_remove_slave(struct w1_slave *sl)
983{
984 int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
985
986 if (bulk_read_support(sl)) {
987 bulk_read_device_counter--;
988
989 if (!bulk_read_device_counter)
990 device_remove_file(&sl->master->dev,
991 &dev_attr_therm_bulk_read);
992 }
993
994 while (refcnt) {
995 msleep(1000);
996 refcnt = atomic_read(THERM_REFCNT(sl->family_data));
997 }
998 kfree(sl->family_data);
999 sl->family_data = NULL;
1000}
1001
1002
1003
1004
1005static int reset_select_slave(struct w1_slave *sl)
1006{
1007 u8 match[9] = { W1_MATCH_ROM, };
1008 u64 rn = le64_to_cpu(*((u64 *)&sl->reg_num));
1009
1010 if (w1_reset_bus(sl->master))
1011 return -ENODEV;
1012
1013 memcpy(&match[1], &rn, 8);
1014 w1_write_block(sl->master, match, 9);
1015
1016 return 0;
1017}
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029static int w1_poll_completion(struct w1_master *dev_master, int tout_ms)
1030{
1031 int i;
1032
1033 for (i = 0; i < tout_ms/W1_POLL_PERIOD; i++) {
1034
1035 msleep(W1_POLL_PERIOD);
1036
1037
1038 if (w1_read_8(dev_master) == 0xFF)
1039 break;
1040 }
1041 if (i == tout_ms/W1_POLL_PERIOD)
1042 return -EIO;
1043
1044 return 0;
1045}
1046
1047static int convert_t(struct w1_slave *sl, struct therm_info *info)
1048{
1049 struct w1_master *dev_master = sl->master;
1050 int max_trying = W1_THERM_MAX_TRY;
1051 int t_conv;
1052 int ret = -ENODEV;
1053 bool strong_pullup;
1054
1055 if (!sl->family_data)
1056 goto error;
1057
1058 strong_pullup = (w1_strong_pullup == 2 ||
1059 (!SLAVE_POWERMODE(sl) &&
1060 w1_strong_pullup));
1061
1062 if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) {
1063 dev_warn(&sl->dev,
1064 "%s: Disabling W1_THERM_POLL_COMPLETION in parasite power mode.\n",
1065 __func__);
1066 SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION;
1067 }
1068
1069
1070 t_conv = conversion_time(sl);
1071
1072 memset(info->rom, 0, sizeof(info->rom));
1073
1074
1075 atomic_inc(THERM_REFCNT(sl->family_data));
1076
1077 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1078 ret = -EAGAIN;
1079 goto dec_refcnt;
1080 }
1081
1082 while (max_trying-- && ret) {
1083
1084 info->verdict = 0;
1085 info->crc = 0;
1086
1087 if (!reset_select_slave(sl)) {
1088 unsigned long sleep_rem;
1089
1090
1091 if (strong_pullup)
1092 w1_next_pullup(dev_master, t_conv);
1093
1094 w1_write_8(dev_master, W1_CONVERT_TEMP);
1095
1096 if (strong_pullup) {
1097 sleep_rem = msleep_interruptible(t_conv);
1098 if (sleep_rem != 0) {
1099 ret = -EINTR;
1100 goto mt_unlock;
1101 }
1102 mutex_unlock(&dev_master->bus_mutex);
1103 } else {
1104 if (SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) {
1105 ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP);
1106 if (ret) {
1107 dev_dbg(&sl->dev, "%s: Timeout\n", __func__);
1108 goto mt_unlock;
1109 }
1110 mutex_unlock(&dev_master->bus_mutex);
1111 } else {
1112
1113 mutex_unlock(&dev_master->bus_mutex);
1114 sleep_rem = msleep_interruptible(t_conv);
1115 if (sleep_rem != 0) {
1116 ret = -EINTR;
1117 goto dec_refcnt;
1118 }
1119 }
1120 }
1121 ret = read_scratchpad(sl, info);
1122
1123
1124 if ((SLAVE_FEATURES(sl) & W1_THERM_CHECK_RESULT) &&
1125 (info->rom[6] == 0xC) &&
1126 ((info->rom[1] == 0x5 && info->rom[0] == 0x50) ||
1127 (info->rom[1] == 0x7 && info->rom[0] == 0xFF))
1128 ) {
1129
1130
1131
1132
1133 ret = -EIO;
1134 }
1135
1136 goto dec_refcnt;
1137 }
1138
1139 }
1140
1141mt_unlock:
1142 mutex_unlock(&dev_master->bus_mutex);
1143dec_refcnt:
1144 atomic_dec(THERM_REFCNT(sl->family_data));
1145error:
1146 return ret;
1147}
1148
1149static int conv_time_measure(struct w1_slave *sl, int *conv_time)
1150{
1151 struct therm_info inf,
1152 *info = &inf;
1153 struct w1_master *dev_master = sl->master;
1154 int max_trying = W1_THERM_MAX_TRY;
1155 int ret = -ENODEV;
1156 bool strong_pullup;
1157
1158 if (!sl->family_data)
1159 goto error;
1160
1161 strong_pullup = (w1_strong_pullup == 2 ||
1162 (!SLAVE_POWERMODE(sl) &&
1163 w1_strong_pullup));
1164
1165 if (strong_pullup) {
1166 pr_info("%s: Measure with strong_pullup is not supported.\n", __func__);
1167 return -EINVAL;
1168 }
1169
1170 memset(info->rom, 0, sizeof(info->rom));
1171
1172
1173 atomic_inc(THERM_REFCNT(sl->family_data));
1174
1175 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1176 ret = -EAGAIN;
1177 goto dec_refcnt;
1178 }
1179
1180 while (max_trying-- && ret) {
1181 info->verdict = 0;
1182 info->crc = 0;
1183
1184 if (!reset_select_slave(sl)) {
1185 int j_start, j_end;
1186
1187
1188 w1_write_8(dev_master, W1_CONVERT_TEMP);
1189
1190 j_start = jiffies;
1191 ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP);
1192 if (ret) {
1193 dev_dbg(&sl->dev, "%s: Timeout\n", __func__);
1194 goto mt_unlock;
1195 }
1196 j_end = jiffies;
1197
1198 *conv_time = jiffies_to_msecs(j_end-j_start)*12/10;
1199 pr_debug("W1 Measure complete, conv_time = %d, HZ=%d.\n",
1200 *conv_time, HZ);
1201 if (*conv_time <= CONV_TIME_MEASURE) {
1202 ret = -EIO;
1203 goto mt_unlock;
1204 }
1205 mutex_unlock(&dev_master->bus_mutex);
1206 ret = read_scratchpad(sl, info);
1207 goto dec_refcnt;
1208 }
1209
1210 }
1211mt_unlock:
1212 mutex_unlock(&dev_master->bus_mutex);
1213dec_refcnt:
1214 atomic_dec(THERM_REFCNT(sl->family_data));
1215error:
1216 return ret;
1217}
1218
1219static int read_scratchpad(struct w1_slave *sl, struct therm_info *info)
1220{
1221 struct w1_master *dev_master = sl->master;
1222 int max_trying = W1_THERM_MAX_TRY;
1223 int ret = -ENODEV;
1224
1225 info->verdict = 0;
1226
1227 if (!sl->family_data)
1228 goto error;
1229
1230 memset(info->rom, 0, sizeof(info->rom));
1231
1232
1233 atomic_inc(THERM_REFCNT(sl->family_data));
1234
1235 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1236 ret = -EAGAIN;
1237 goto dec_refcnt;
1238 }
1239
1240 while (max_trying-- && ret) {
1241
1242 if (!reset_select_slave(sl)) {
1243 u8 nb_bytes_read;
1244
1245 w1_write_8(dev_master, W1_READ_SCRATCHPAD);
1246
1247 nb_bytes_read = w1_read_block(dev_master, info->rom, 9);
1248 if (nb_bytes_read != 9) {
1249 dev_warn(&sl->dev,
1250 "w1_read_block(): returned %u instead of 9.\n",
1251 nb_bytes_read);
1252 ret = -EIO;
1253 }
1254
1255 info->crc = w1_calc_crc8(info->rom, 8);
1256
1257 if (info->rom[8] == info->crc) {
1258 info->verdict = 1;
1259 ret = 0;
1260 } else
1261 ret = -EIO;
1262 }
1263
1264 }
1265 mutex_unlock(&dev_master->bus_mutex);
1266
1267dec_refcnt:
1268 atomic_dec(THERM_REFCNT(sl->family_data));
1269error:
1270 return ret;
1271}
1272
1273static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes)
1274{
1275 struct w1_master *dev_master = sl->master;
1276 int max_trying = W1_THERM_MAX_TRY;
1277 int ret = -ENODEV;
1278
1279 if (!sl->family_data)
1280 goto error;
1281
1282
1283 atomic_inc(THERM_REFCNT(sl->family_data));
1284
1285 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1286 ret = -EAGAIN;
1287 goto dec_refcnt;
1288 }
1289
1290 while (max_trying-- && ret) {
1291
1292 if (!reset_select_slave(sl)) {
1293 w1_write_8(dev_master, W1_WRITE_SCRATCHPAD);
1294 w1_write_block(dev_master, data, nb_bytes);
1295 ret = 0;
1296 }
1297 }
1298 mutex_unlock(&dev_master->bus_mutex);
1299
1300dec_refcnt:
1301 atomic_dec(THERM_REFCNT(sl->family_data));
1302error:
1303 return ret;
1304}
1305
1306static int copy_scratchpad(struct w1_slave *sl)
1307{
1308 struct w1_master *dev_master = sl->master;
1309 int max_trying = W1_THERM_MAX_TRY;
1310 int t_write, ret = -ENODEV;
1311 bool strong_pullup;
1312
1313 if (!sl->family_data)
1314 goto error;
1315
1316 t_write = W1_THERM_EEPROM_WRITE_DELAY;
1317 strong_pullup = (w1_strong_pullup == 2 ||
1318 (!SLAVE_POWERMODE(sl) &&
1319 w1_strong_pullup));
1320
1321
1322 atomic_inc(THERM_REFCNT(sl->family_data));
1323
1324 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1325 ret = -EAGAIN;
1326 goto dec_refcnt;
1327 }
1328
1329 while (max_trying-- && ret) {
1330
1331 if (!reset_select_slave(sl)) {
1332 unsigned long sleep_rem;
1333
1334
1335 if (strong_pullup)
1336 w1_next_pullup(dev_master, t_write);
1337
1338 w1_write_8(dev_master, W1_COPY_SCRATCHPAD);
1339
1340 if (strong_pullup) {
1341 sleep_rem = msleep_interruptible(t_write);
1342 if (sleep_rem != 0) {
1343 ret = -EINTR;
1344 goto mt_unlock;
1345 }
1346 }
1347 ret = 0;
1348 }
1349
1350 }
1351
1352mt_unlock:
1353 mutex_unlock(&dev_master->bus_mutex);
1354dec_refcnt:
1355 atomic_dec(THERM_REFCNT(sl->family_data));
1356error:
1357 return ret;
1358}
1359
1360static int recall_eeprom(struct w1_slave *sl)
1361{
1362 struct w1_master *dev_master = sl->master;
1363 int max_trying = W1_THERM_MAX_TRY;
1364 int ret = -ENODEV;
1365
1366 if (!sl->family_data)
1367 goto error;
1368
1369
1370 atomic_inc(THERM_REFCNT(sl->family_data));
1371
1372 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1373 ret = -EAGAIN;
1374 goto dec_refcnt;
1375 }
1376
1377 while (max_trying-- && ret) {
1378
1379 if (!reset_select_slave(sl)) {
1380
1381 w1_write_8(dev_master, W1_RECALL_EEPROM);
1382 ret = w1_poll_completion(dev_master, W1_POLL_RECALL_EEPROM);
1383 }
1384
1385 }
1386
1387 mutex_unlock(&dev_master->bus_mutex);
1388
1389dec_refcnt:
1390 atomic_dec(THERM_REFCNT(sl->family_data));
1391error:
1392 return ret;
1393}
1394
1395static int read_powermode(struct w1_slave *sl)
1396{
1397 struct w1_master *dev_master = sl->master;
1398 int max_trying = W1_THERM_MAX_TRY;
1399 int ret = -ENODEV;
1400
1401 if (!sl->family_data)
1402 goto error;
1403
1404
1405 atomic_inc(THERM_REFCNT(sl->family_data));
1406
1407 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1408 ret = -EAGAIN;
1409 goto dec_refcnt;
1410 }
1411
1412 while ((max_trying--) && (ret < 0)) {
1413
1414 if (!reset_select_slave(sl)) {
1415 w1_write_8(dev_master, W1_READ_PSUPPLY);
1416
1417
1418
1419
1420
1421 ret = w1_touch_bit(dev_master, 1);
1422
1423 }
1424 }
1425 mutex_unlock(&dev_master->bus_mutex);
1426
1427dec_refcnt:
1428 atomic_dec(THERM_REFCNT(sl->family_data));
1429error:
1430 return ret;
1431}
1432
1433static int trigger_bulk_read(struct w1_master *dev_master)
1434{
1435 struct w1_slave *sl = NULL;
1436 int max_trying = W1_THERM_MAX_TRY;
1437 int t_conv = 0;
1438 int ret = -ENODEV;
1439 bool strong_pullup = false;
1440
1441
1442
1443
1444
1445
1446 list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
1447 if (!sl->family_data)
1448 goto error;
1449 if (bulk_read_support(sl)) {
1450 int t_cur = conversion_time(sl);
1451
1452 t_conv = t_cur > t_conv ? t_cur : t_conv;
1453 strong_pullup = strong_pullup ||
1454 (w1_strong_pullup == 2 ||
1455 (!SLAVE_POWERMODE(sl) &&
1456 w1_strong_pullup));
1457 }
1458 }
1459
1460
1461
1462
1463
1464 if (!t_conv)
1465 goto error;
1466
1467 if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1468 ret = -EAGAIN;
1469 goto error;
1470 }
1471
1472 while ((max_trying--) && (ret < 0)) {
1473
1474 if (!w1_reset_bus(dev_master)) {
1475 unsigned long sleep_rem;
1476
1477 w1_write_8(dev_master, W1_SKIP_ROM);
1478
1479 if (strong_pullup)
1480 w1_next_pullup(dev_master, t_conv);
1481
1482 w1_write_8(dev_master, W1_CONVERT_TEMP);
1483
1484
1485 list_for_each_entry(sl,
1486 &dev_master->slist, w1_slave_entry) {
1487 if (bulk_read_support(sl))
1488 SLAVE_CONVERT_TRIGGERED(sl) = -1;
1489 }
1490
1491 if (strong_pullup) {
1492 sleep_rem = msleep_interruptible(t_conv);
1493 if (sleep_rem != 0) {
1494 ret = -EINTR;
1495 goto mt_unlock;
1496 }
1497 mutex_unlock(&dev_master->bus_mutex);
1498 } else {
1499 mutex_unlock(&dev_master->bus_mutex);
1500 sleep_rem = msleep_interruptible(t_conv);
1501 if (sleep_rem != 0) {
1502 ret = -EINTR;
1503 goto set_flag;
1504 }
1505 }
1506 ret = 0;
1507 goto set_flag;
1508 }
1509 }
1510
1511mt_unlock:
1512 mutex_unlock(&dev_master->bus_mutex);
1513set_flag:
1514
1515 list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
1516 if (bulk_read_support(sl))
1517 SLAVE_CONVERT_TRIGGERED(sl) = 1;
1518 }
1519error:
1520 return ret;
1521}
1522
1523
1524
1525static ssize_t w1_slave_show(struct device *device,
1526 struct device_attribute *attr, char *buf)
1527{
1528 struct w1_slave *sl = dev_to_w1_slave(device);
1529 struct therm_info info;
1530 u8 *family_data = sl->family_data;
1531 int ret, i;
1532 ssize_t c = PAGE_SIZE;
1533
1534 if (bulk_read_support(sl)) {
1535 if (SLAVE_CONVERT_TRIGGERED(sl) < 0) {
1536 dev_dbg(device,
1537 "%s: Conversion in progress, retry later\n",
1538 __func__);
1539 return 0;
1540 } else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) {
1541
1542 ret = read_scratchpad(sl, &info);
1543 SLAVE_CONVERT_TRIGGERED(sl) = 0;
1544 } else
1545 ret = convert_t(sl, &info);
1546 } else
1547 ret = convert_t(sl, &info);
1548
1549 if (ret < 0) {
1550 dev_dbg(device,
1551 "%s: Temperature data may be corrupted. err=%d\n",
1552 __func__, ret);
1553 return 0;
1554 }
1555
1556 for (i = 0; i < 9; ++i)
1557 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]);
1558 c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
1559 info.crc, (info.verdict) ? "YES" : "NO");
1560
1561 if (info.verdict)
1562 memcpy(family_data, info.rom, sizeof(info.rom));
1563 else
1564 dev_warn(device, "%s:Read failed CRC check\n", __func__);
1565
1566 for (i = 0; i < 9; ++i)
1567 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
1568 ((u8 *)family_data)[i]);
1569
1570 c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
1571 temperature_from_RAM(sl, info.rom));
1572
1573 ret = PAGE_SIZE - c;
1574 return ret;
1575}
1576
1577static ssize_t w1_slave_store(struct device *device,
1578 struct device_attribute *attr, const char *buf,
1579 size_t size)
1580{
1581 int val, ret = 0;
1582 struct w1_slave *sl = dev_to_w1_slave(device);
1583
1584 ret = kstrtoint(buf, 10, &val);
1585
1586 if (ret) {
1587 dev_info(device,
1588 "%s: conversion error. err= %d\n", __func__, ret);
1589 return size;
1590 }
1591
1592 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1593 dev_info(device,
1594 "%s: Device not supported by the driver\n", __func__);
1595 return size;
1596 }
1597
1598 if (val == 0)
1599 ret = copy_scratchpad(sl);
1600 else {
1601 if (SLAVE_SPECIFIC_FUNC(sl)->set_resolution)
1602 ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val);
1603 }
1604
1605 if (ret) {
1606 dev_warn(device, "%s: Set resolution - error %d\n", __func__, ret);
1607
1608 return ret;
1609 }
1610 SLAVE_RESOLUTION(sl) = val;
1611
1612 SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT;
1613
1614 return size;
1615}
1616
1617static ssize_t temperature_show(struct device *device,
1618 struct device_attribute *attr, char *buf)
1619{
1620 struct w1_slave *sl = dev_to_w1_slave(device);
1621 struct therm_info info;
1622 int ret = 0;
1623
1624 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1625 dev_info(device,
1626 "%s: Device not supported by the driver\n", __func__);
1627 return 0;
1628 }
1629
1630 if (bulk_read_support(sl)) {
1631 if (SLAVE_CONVERT_TRIGGERED(sl) < 0) {
1632 dev_dbg(device,
1633 "%s: Conversion in progress, retry later\n",
1634 __func__);
1635 return 0;
1636 } else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) {
1637
1638 ret = read_scratchpad(sl, &info);
1639 SLAVE_CONVERT_TRIGGERED(sl) = 0;
1640 } else
1641 ret = convert_t(sl, &info);
1642 } else
1643 ret = convert_t(sl, &info);
1644
1645 if (ret < 0) {
1646 dev_dbg(device,
1647 "%s: Temperature data may be corrupted. err=%d\n",
1648 __func__, ret);
1649 return 0;
1650 }
1651
1652 return sprintf(buf, "%d\n", temperature_from_RAM(sl, info.rom));
1653}
1654
1655static ssize_t ext_power_show(struct device *device,
1656 struct device_attribute *attr, char *buf)
1657{
1658 struct w1_slave *sl = dev_to_w1_slave(device);
1659
1660 if (!sl->family_data) {
1661 dev_info(device,
1662 "%s: Device not supported by the driver\n", __func__);
1663 return 0;
1664 }
1665
1666
1667 SLAVE_POWERMODE(sl) = read_powermode(sl);
1668
1669 if (SLAVE_POWERMODE(sl) < 0) {
1670 dev_dbg(device,
1671 "%s: Power_mode may be corrupted. err=%d\n",
1672 __func__, SLAVE_POWERMODE(sl));
1673 }
1674 return sprintf(buf, "%d\n", SLAVE_POWERMODE(sl));
1675}
1676
1677static ssize_t resolution_show(struct device *device,
1678 struct device_attribute *attr, char *buf)
1679{
1680 struct w1_slave *sl = dev_to_w1_slave(device);
1681
1682 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1683 dev_info(device,
1684 "%s: Device not supported by the driver\n", __func__);
1685 return 0;
1686 }
1687
1688
1689 SLAVE_RESOLUTION(sl) = SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl);
1690 if (SLAVE_RESOLUTION(sl) < 0) {
1691 dev_dbg(device,
1692 "%s: Resolution may be corrupted. err=%d\n",
1693 __func__, SLAVE_RESOLUTION(sl));
1694 }
1695
1696 return sprintf(buf, "%d\n", SLAVE_RESOLUTION(sl));
1697}
1698
1699static ssize_t resolution_store(struct device *device,
1700 struct device_attribute *attr, const char *buf, size_t size)
1701{
1702 struct w1_slave *sl = dev_to_w1_slave(device);
1703 int val;
1704 int ret = 0;
1705
1706 ret = kstrtoint(buf, 10, &val);
1707
1708 if (ret) {
1709 dev_info(device,
1710 "%s: conversion error. err= %d\n", __func__, ret);
1711 return size;
1712 }
1713
1714 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1715 dev_info(device,
1716 "%s: Device not supported by the driver\n", __func__);
1717 return size;
1718 }
1719
1720
1721
1722
1723
1724
1725
1726 ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val);
1727
1728 if (ret)
1729 return ret;
1730
1731 SLAVE_RESOLUTION(sl) = val;
1732
1733 SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT;
1734
1735 return size;
1736}
1737
1738static ssize_t eeprom_cmd_store(struct device *device,
1739 struct device_attribute *attr, const char *buf, size_t size)
1740{
1741 struct w1_slave *sl = dev_to_w1_slave(device);
1742 int ret = -EINVAL;
1743
1744 if (size == sizeof(EEPROM_CMD_WRITE)) {
1745 if (!strncmp(buf, EEPROM_CMD_WRITE, sizeof(EEPROM_CMD_WRITE)-1))
1746 ret = copy_scratchpad(sl);
1747 } else if (size == sizeof(EEPROM_CMD_READ)) {
1748 if (!strncmp(buf, EEPROM_CMD_READ, sizeof(EEPROM_CMD_READ)-1))
1749 ret = recall_eeprom(sl);
1750 }
1751
1752 if (ret)
1753 dev_info(device, "%s: error in process %d\n", __func__, ret);
1754
1755 return size;
1756}
1757
1758static ssize_t alarms_show(struct device *device,
1759 struct device_attribute *attr, char *buf)
1760{
1761 struct w1_slave *sl = dev_to_w1_slave(device);
1762 int ret;
1763 s8 th = 0, tl = 0;
1764 struct therm_info scratchpad;
1765
1766 ret = read_scratchpad(sl, &scratchpad);
1767
1768 if (!ret) {
1769 th = scratchpad.rom[2];
1770 tl = scratchpad.rom[3];
1771 } else {
1772 dev_info(device,
1773 "%s: error reading alarms register %d\n",
1774 __func__, ret);
1775 }
1776
1777 return sprintf(buf, "%hd %hd\n", tl, th);
1778}
1779
1780static ssize_t alarms_store(struct device *device,
1781 struct device_attribute *attr, const char *buf, size_t size)
1782{
1783 struct w1_slave *sl = dev_to_w1_slave(device);
1784 struct therm_info info;
1785 u8 new_config_register[3];
1786 int temp, ret;
1787 char *token = NULL;
1788 s8 tl, th, tt;
1789 char *p_args, *orig;
1790
1791 p_args = orig = kmalloc(size, GFP_KERNEL);
1792
1793 if (!p_args) {
1794 dev_warn(device,
1795 "%s: error unable to allocate memory %d\n",
1796 __func__, -ENOMEM);
1797 return size;
1798 }
1799 strcpy(p_args, buf);
1800
1801
1802 token = strsep(&p_args, " ");
1803
1804 if (!token) {
1805 dev_info(device,
1806 "%s: error parsing args %d\n", __func__, -EINVAL);
1807 goto free_m;
1808 }
1809
1810
1811 ret = kstrtoint (token, 10, &temp);
1812 if (ret) {
1813 dev_info(device,
1814 "%s: error parsing args %d\n", __func__, ret);
1815 goto free_m;
1816 }
1817
1818 tl = int_to_short(temp);
1819
1820
1821 token = strsep(&p_args, " ");
1822 if (!token) {
1823 dev_info(device,
1824 "%s: error parsing args %d\n", __func__, -EINVAL);
1825 goto free_m;
1826 }
1827
1828 ret = kstrtoint (token, 10, &temp);
1829 if (ret) {
1830 dev_info(device,
1831 "%s: error parsing args %d\n", __func__, ret);
1832 goto free_m;
1833 }
1834
1835
1836 th = int_to_short(temp);
1837
1838
1839 if (tl > th) {
1840 tt = tl; tl = th; th = tt;
1841 }
1842
1843
1844
1845
1846
1847 ret = read_scratchpad(sl, &info);
1848 if (!ret) {
1849 new_config_register[0] = th;
1850 new_config_register[1] = tl;
1851 new_config_register[2] = info.rom[4];
1852 } else {
1853 dev_info(device,
1854 "%s: error reading from the slave device %d\n",
1855 __func__, ret);
1856 goto free_m;
1857 }
1858
1859
1860 if (!SLAVE_SPECIFIC_FUNC(sl)) {
1861 dev_info(device,
1862 "%s: Device not supported by the driver %d\n",
1863 __func__, -ENODEV);
1864 goto free_m;
1865 }
1866
1867 ret = SLAVE_SPECIFIC_FUNC(sl)->write_data(sl, new_config_register);
1868 if (ret)
1869 dev_info(device,
1870 "%s: error writing to the slave device %d\n",
1871 __func__, ret);
1872
1873free_m:
1874
1875 kfree(orig);
1876
1877 return size;
1878}
1879
1880static ssize_t therm_bulk_read_store(struct device *device,
1881 struct device_attribute *attr, const char *buf, size_t size)
1882{
1883 struct w1_master *dev_master = dev_to_w1_master(device);
1884 int ret = -EINVAL;
1885
1886 if (size == sizeof(BULK_TRIGGER_CMD))
1887 if (!strncmp(buf, BULK_TRIGGER_CMD,
1888 sizeof(BULK_TRIGGER_CMD)-1))
1889 ret = trigger_bulk_read(dev_master);
1890
1891 if (ret)
1892 dev_info(device,
1893 "%s: unable to trigger a bulk read on the bus. err=%d\n",
1894 __func__, ret);
1895
1896 return size;
1897}
1898
1899static ssize_t therm_bulk_read_show(struct device *device,
1900 struct device_attribute *attr, char *buf)
1901{
1902 struct w1_master *dev_master = dev_to_w1_master(device);
1903 struct w1_slave *sl = NULL;
1904 int ret = 0;
1905
1906 list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
1907 if (sl->family_data) {
1908 if (bulk_read_support(sl)) {
1909 if (SLAVE_CONVERT_TRIGGERED(sl) == -1) {
1910 ret = -1;
1911 goto show_result;
1912 }
1913 if (SLAVE_CONVERT_TRIGGERED(sl) == 1)
1914
1915 ret = 1;
1916 }
1917 }
1918 }
1919show_result:
1920 return sprintf(buf, "%d\n", ret);
1921}
1922
1923static ssize_t conv_time_show(struct device *device,
1924 struct device_attribute *attr, char *buf)
1925{
1926 struct w1_slave *sl = dev_to_w1_slave(device);
1927
1928 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1929 dev_info(device,
1930 "%s: Device is not supported by the driver\n", __func__);
1931 return 0;
1932 }
1933 return sprintf(buf, "%d\n", conversion_time(sl));
1934}
1935
1936static ssize_t conv_time_store(struct device *device,
1937 struct device_attribute *attr, const char *buf, size_t size)
1938{
1939 int val, ret = 0;
1940 struct w1_slave *sl = dev_to_w1_slave(device);
1941
1942 if (kstrtoint(buf, 10, &val))
1943 return -EINVAL;
1944
1945 if (check_family_data(sl))
1946 return -ENODEV;
1947
1948 if (val != CONV_TIME_MEASURE) {
1949 if (val >= CONV_TIME_DEFAULT)
1950 SLAVE_CONV_TIME_OVERRIDE(sl) = val;
1951 else
1952 return -EINVAL;
1953
1954 } else {
1955 int conv_time;
1956
1957 ret = conv_time_measure(sl, &conv_time);
1958 if (ret)
1959 return -EIO;
1960 SLAVE_CONV_TIME_OVERRIDE(sl) = conv_time;
1961 }
1962 return size;
1963}
1964
1965static ssize_t features_show(struct device *device,
1966 struct device_attribute *attr, char *buf)
1967{
1968 struct w1_slave *sl = dev_to_w1_slave(device);
1969
1970 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1971 dev_info(device,
1972 "%s: Device not supported by the driver\n", __func__);
1973 return 0;
1974 }
1975 return sprintf(buf, "%u\n", SLAVE_FEATURES(sl));
1976}
1977
1978static ssize_t features_store(struct device *device,
1979 struct device_attribute *attr, const char *buf, size_t size)
1980{
1981 int val, ret = 0;
1982 bool strong_pullup;
1983 struct w1_slave *sl = dev_to_w1_slave(device);
1984
1985 ret = kstrtouint(buf, 10, &val);
1986 if (ret)
1987 return -EINVAL;
1988
1989 if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1990 dev_info(device, "%s: Device not supported by the driver\n", __func__);
1991 return -ENODEV;
1992 }
1993
1994 if ((val & W1_THERM_FEATURES_MASK) != val)
1995 return -EINVAL;
1996
1997 SLAVE_FEATURES(sl) = val;
1998
1999 strong_pullup = (w1_strong_pullup == 2 ||
2000 (!SLAVE_POWERMODE(sl) &&
2001 w1_strong_pullup));
2002
2003 if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) {
2004 dev_warn(&sl->dev,
2005 "%s: W1_THERM_POLL_COMPLETION disabled in parasite power mode.\n",
2006 __func__);
2007 SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION;
2008 }
2009
2010 return size;
2011}
2012
2013#if IS_REACHABLE(CONFIG_HWMON)
2014static int w1_read_temp(struct device *device, u32 attr, int channel,
2015 long *val)
2016{
2017 struct w1_slave *sl = dev_get_drvdata(device);
2018 struct therm_info info;
2019 int ret;
2020
2021 switch (attr) {
2022 case hwmon_temp_input:
2023 ret = convert_t(sl, &info);
2024 if (ret)
2025 return ret;
2026
2027 if (!info.verdict) {
2028 ret = -EIO;
2029 return ret;
2030 }
2031
2032 *val = temperature_from_RAM(sl, info.rom);
2033 ret = 0;
2034 break;
2035 default:
2036 ret = -EOPNOTSUPP;
2037 break;
2038 }
2039
2040 return ret;
2041}
2042#endif
2043
2044#define W1_42_CHAIN 0x99
2045#define W1_42_CHAIN_OFF 0x3C
2046#define W1_42_CHAIN_OFF_INV 0xC3
2047#define W1_42_CHAIN_ON 0x5A
2048#define W1_42_CHAIN_ON_INV 0xA5
2049#define W1_42_CHAIN_DONE 0x96
2050#define W1_42_CHAIN_DONE_INV 0x69
2051#define W1_42_COND_READ 0x0F
2052#define W1_42_SUCCESS_CONFIRM_BYTE 0xAA
2053#define W1_42_FINISHED_BYTE 0xFF
2054static ssize_t w1_seq_show(struct device *device,
2055 struct device_attribute *attr, char *buf)
2056{
2057 struct w1_slave *sl = dev_to_w1_slave(device);
2058 ssize_t c = PAGE_SIZE;
2059 int i;
2060 u8 ack;
2061 u64 rn;
2062 struct w1_reg_num *reg_num;
2063 int seq = 0;
2064
2065 mutex_lock(&sl->master->bus_mutex);
2066
2067 if (w1_reset_bus(sl->master))
2068 goto error;
2069 w1_write_8(sl->master, W1_SKIP_ROM);
2070 w1_write_8(sl->master, W1_42_CHAIN);
2071 w1_write_8(sl->master, W1_42_CHAIN_ON);
2072 w1_write_8(sl->master, W1_42_CHAIN_ON_INV);
2073 msleep(sl->master->pullup_duration);
2074
2075
2076 ack = w1_read_8(sl->master);
2077 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
2078 goto error;
2079
2080
2081 for (i = 0; i <= 64; i++) {
2082 if (w1_reset_bus(sl->master))
2083 goto error;
2084
2085 w1_write_8(sl->master, W1_42_COND_READ);
2086 w1_read_block(sl->master, (u8 *)&rn, 8);
2087 reg_num = (struct w1_reg_num *) &rn;
2088 if (reg_num->family == W1_42_FINISHED_BYTE)
2089 break;
2090 if (sl->reg_num.id == reg_num->id)
2091 seq = i;
2092
2093 w1_write_8(sl->master, W1_42_CHAIN);
2094 w1_write_8(sl->master, W1_42_CHAIN_DONE);
2095 w1_write_8(sl->master, W1_42_CHAIN_DONE_INV);
2096 w1_read_block(sl->master, &ack, sizeof(ack));
2097
2098
2099 ack = w1_read_8(sl->master);
2100 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
2101 goto error;
2102
2103 }
2104
2105
2106 if (w1_reset_bus(sl->master))
2107 goto error;
2108 w1_write_8(sl->master, W1_SKIP_ROM);
2109 w1_write_8(sl->master, W1_42_CHAIN);
2110 w1_write_8(sl->master, W1_42_CHAIN_OFF);
2111 w1_write_8(sl->master, W1_42_CHAIN_OFF_INV);
2112
2113
2114 ack = w1_read_8(sl->master);
2115 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
2116 goto error;
2117 mutex_unlock(&sl->master->bus_mutex);
2118
2119 c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq);
2120 return PAGE_SIZE - c;
2121error:
2122 mutex_unlock(&sl->master->bus_mutex);
2123 return -EIO;
2124}
2125
2126static int __init w1_therm_init(void)
2127{
2128 int err, i;
2129
2130 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
2131 err = w1_register_family(w1_therm_families[i].f);
2132 if (err)
2133 w1_therm_families[i].broken = 1;
2134 }
2135
2136 return 0;
2137}
2138
2139static void __exit w1_therm_fini(void)
2140{
2141 int i;
2142
2143 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
2144 if (!w1_therm_families[i].broken)
2145 w1_unregister_family(w1_therm_families[i].f);
2146}
2147
2148module_init(w1_therm_init);
2149module_exit(w1_therm_fini);
2150
2151MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
2152MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
2153MODULE_LICENSE("GPL");
2154MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20));
2155MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822));
2156MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20));
2157MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825));
2158MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00));
2159