ccaf29994a42023a74c49e841e68239c9629931b
[sfrench/cifs-2.6.git] / drivers / w1 / slaves / w1_therm.c
1 /*
2  *      w1_therm.c
3  *
4  * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
5  *
6  *
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.
11  *
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.
16  *
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
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
33 #include "../w1.h"
34 #include "../w1_int.h"
35 #include "../w1_family.h"
36
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.
44  *
45  * Summary of options:
46  * - strong_pullup = 0  Disable strong pullup completely
47  * - strong_pullup = 1  Enable automatic strong pullup detection
48  * - strong_pullup = 2  Force strong pullup
49  */
50 static int w1_strong_pullup = 1;
51 module_param_named(strong_pullup, w1_strong_pullup, int, 0);
52
53 struct w1_therm_family_data {
54         uint8_t rom[9];
55         atomic_t refcnt;
56 };
57
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)
61
62 static int w1_therm_add_slave(struct w1_slave *sl)
63 {
64         sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
65                 GFP_KERNEL);
66         if (!sl->family_data)
67                 return -ENOMEM;
68         atomic_set(THERM_REFCNT(sl->family_data), 1);
69         return 0;
70 }
71
72 static void w1_therm_remove_slave(struct w1_slave *sl)
73 {
74         int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
75
76         while (refcnt) {
77                 msleep(1000);
78                 refcnt = atomic_read(THERM_REFCNT(sl->family_data));
79         }
80         kfree(sl->family_data);
81         sl->family_data = NULL;
82 }
83
84 static ssize_t w1_slave_show(struct device *device,
85         struct device_attribute *attr, char *buf);
86
87 static ssize_t w1_slave_store(struct device *device,
88         struct device_attribute *attr, const char *buf, size_t size);
89
90 static ssize_t w1_seq_show(struct device *device,
91         struct device_attribute *attr, char *buf);
92
93 static DEVICE_ATTR_RW(w1_slave);
94 static DEVICE_ATTR_RO(w1_seq);
95
96 static struct attribute *w1_therm_attrs[] = {
97         &dev_attr_w1_slave.attr,
98         NULL,
99 };
100
101 static struct attribute *w1_ds28ea00_attrs[] = {
102         &dev_attr_w1_slave.attr,
103         &dev_attr_w1_seq.attr,
104         NULL,
105 };
106 ATTRIBUTE_GROUPS(w1_therm);
107 ATTRIBUTE_GROUPS(w1_ds28ea00);
108
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,
113 };
114
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,
119 };
120
121 static struct w1_family w1_therm_family_DS18S20 = {
122         .fid = W1_THERM_DS18S20,
123         .fops = &w1_therm_fops,
124 };
125
126 static struct w1_family w1_therm_family_DS18B20 = {
127         .fid = W1_THERM_DS18B20,
128         .fops = &w1_therm_fops,
129 };
130
131 static struct w1_family w1_therm_family_DS1822 = {
132         .fid = W1_THERM_DS1822,
133         .fops = &w1_therm_fops,
134 };
135
136 static struct w1_family w1_therm_family_DS28EA00 = {
137         .fid = W1_THERM_DS28EA00,
138         .fops = &w1_ds28ea00_fops,
139 };
140
141 static struct w1_family w1_therm_family_DS1825 = {
142         .fid = W1_THERM_DS1825,
143         .fops = &w1_therm_fops,
144 };
145
146 struct w1_therm_family_converter {
147         u8                      broken;
148         u16                     reserved;
149         struct w1_family        *f;
150         int                     (*convert)(u8 rom[9]);
151         int                     (*precision)(struct device *device, int val);
152         int                     (*eeprom)(struct device *device);
153 };
154
155 /* write configuration to eeprom */
156 static inline int w1_therm_eeprom(struct device *device);
157
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);
161
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]);
165
166 static struct w1_therm_family_converter w1_therm_families[] = {
167         {
168                 .f              = &w1_therm_family_DS18S20,
169                 .convert        = w1_DS18S20_convert_temp,
170                 .precision      = w1_DS18S20_precision,
171                 .eeprom         = w1_therm_eeprom
172         },
173         {
174                 .f              = &w1_therm_family_DS1822,
175                 .convert        = w1_DS18B20_convert_temp,
176                 .precision      = w1_DS18S20_precision,
177                 .eeprom         = w1_therm_eeprom
178         },
179         {
180                 .f              = &w1_therm_family_DS18B20,
181                 .convert        = w1_DS18B20_convert_temp,
182                 .precision      = w1_DS18B20_precision,
183                 .eeprom         = w1_therm_eeprom
184         },
185         {
186                 .f              = &w1_therm_family_DS28EA00,
187                 .convert        = w1_DS18B20_convert_temp,
188                 .precision      = w1_DS18S20_precision,
189                 .eeprom         = w1_therm_eeprom
190         },
191         {
192                 .f              = &w1_therm_family_DS1825,
193                 .convert        = w1_DS18B20_convert_temp,
194                 .precision      = w1_DS18S20_precision,
195                 .eeprom         = w1_therm_eeprom
196         }
197 };
198
199 static inline int w1_therm_eeprom(struct device *device)
200 {
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;
206
207         ret = mutex_lock_interruptible(&dev->bus_mutex);
208         if (ret != 0)
209                 goto post_unlock;
210
211         if (!sl->family_data) {
212                 ret = -ENODEV;
213                 goto pre_unlock;
214         }
215
216         /* prevent the slave from going away in sleep */
217         atomic_inc(THERM_REFCNT(family_data));
218         memset(rom, 0, sizeof(rom));
219
220         while (max_trying--) {
221                 if (!w1_reset_select_slave(sl)) {
222                         unsigned int tm = 10;
223                         unsigned long sleep_rem;
224
225                         /* check if in parasite mode */
226                         w1_write_8(dev, W1_READ_PSUPPLY);
227                         external_power = w1_read_8(dev);
228
229                         if (w1_reset_select_slave(sl))
230                                 continue;
231
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);
236
237                         w1_write_8(dev, W1_COPY_SCRATCHPAD);
238
239                         if (external_power) {
240                                 mutex_unlock(&dev->bus_mutex);
241
242                                 sleep_rem = msleep_interruptible(tm);
243                                 if (sleep_rem != 0) {
244                                         ret = -EINTR;
245                                         goto post_unlock;
246                                 }
247
248                                 ret = mutex_lock_interruptible(&dev->bus_mutex);
249                                 if (ret != 0)
250                                         goto post_unlock;
251                         } else if (!w1_strong_pullup) {
252                                 sleep_rem = msleep_interruptible(tm);
253                                 if (sleep_rem != 0) {
254                                         ret = -EINTR;
255                                         goto pre_unlock;
256                                 }
257                         }
258
259                         break;
260                 }
261         }
262
263 pre_unlock:
264         mutex_unlock(&dev->bus_mutex);
265
266 post_unlock:
267         atomic_dec(THERM_REFCNT(family_data));
268         return ret;
269 }
270
271 /* DS18S20 does not feature configuration register */
272 static inline int w1_DS18S20_precision(struct device *device, int val)
273 {
274         return 0;
275 }
276
277 static inline int w1_DS18B20_precision(struct device *device, int val)
278 {
279         struct w1_slave *sl = dev_to_w1_slave(device);
280         struct w1_master *dev = sl->master;
281         u8 rom[9], crc;
282         int ret, max_trying = 10;
283         u8 *family_data = sl->family_data;
284         uint8_t precision_bits;
285         uint8_t mask = 0x60;
286
287         if (val > 12 || val < 9) {
288                 pr_warn("Unsupported precision\n");
289                 return -1;
290         }
291
292         ret = mutex_lock_interruptible(&dev->bus_mutex);
293         if (ret != 0)
294                 goto post_unlock;
295
296         if (!sl->family_data) {
297                 ret = -ENODEV;
298                 goto pre_unlock;
299         }
300
301         /* prevent the slave from going away in sleep */
302         atomic_inc(THERM_REFCNT(family_data));
303         memset(rom, 0, sizeof(rom));
304
305         /* translate precision to bitmask (see datasheet page 9) */
306         switch (val) {
307         case 9:
308                 precision_bits = 0x00;
309                 break;
310         case 10:
311                 precision_bits = 0x20;
312                 break;
313         case 11:
314                 precision_bits = 0x40;
315                 break;
316         case 12:
317         default:
318                 precision_bits = 0x60;
319                 break;
320         }
321
322         while (max_trying--) {
323                 crc = 0;
324
325                 if (!w1_reset_select_slave(sl)) {
326                         int count = 0;
327
328                         /* read values to only alter precision bits */
329                         w1_write_8(dev, W1_READ_SCRATCHPAD);
330                         count = w1_read_block(dev, rom, 9);
331                         if (count != 9)
332                                 dev_warn(device, "w1_read_block() returned %u instead of 9.\n", count);
333
334                         crc = w1_calc_crc8(rom, 8);
335                         if (rom[8] == crc) {
336                                 rom[4] = (rom[4] & ~mask) | (precision_bits & mask);
337
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]);
343
344                                         break;
345                                 }
346                         }
347                 }
348         }
349
350 pre_unlock:
351         mutex_unlock(&dev->bus_mutex);
352
353 post_unlock:
354         atomic_dec(THERM_REFCNT(family_data));
355         return ret;
356 }
357
358 static inline int w1_DS18B20_convert_temp(u8 rom[9])
359 {
360         s16 t = le16_to_cpup((__le16 *)rom);
361
362         return t*1000/16;
363 }
364
365 static inline int w1_DS18S20_convert_temp(u8 rom[9])
366 {
367         int t, h;
368
369         if (!rom[7])
370                 return 0;
371
372         if (rom[1] == 0)
373                 t = ((s32)rom[0] >> 1)*1000;
374         else
375                 t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
376
377         t -= 250;
378         h = 1000*((s32)rom[7] - (s32)rom[6]);
379         h /= (s32)rom[7];
380         t += h;
381
382         return t;
383 }
384
385 static inline int w1_convert_temp(u8 rom[9], u8 fid)
386 {
387         int i;
388
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);
392
393         return 0;
394 }
395
396 static ssize_t w1_slave_store(struct device *device,
397                               struct device_attribute *attr, const char *buf,
398                               size_t size)
399 {
400         int val, ret;
401         struct w1_slave *sl = dev_to_w1_slave(device);
402         int i;
403
404         ret = kstrtoint(buf, 0, &val);
405         if (ret)
406                 return ret;
407
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 */
411                         if (val == 0)
412                                 ret = w1_therm_families[i].eeprom(device);
413                         else
414                                 ret = w1_therm_families[i].precision(device, val);
415                         break;
416                 }
417         }
418         return ret ? : size;
419 }
420
421 static ssize_t w1_slave_show(struct device *device,
422         struct device_attribute *attr, char *buf)
423 {
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;
430
431         ret = mutex_lock_interruptible(&dev->bus_mutex);
432         if (ret != 0)
433                 goto post_unlock;
434
435         if (!sl->family_data) {
436                 ret = -ENODEV;
437                 goto pre_unlock;
438         }
439
440         /* prevent the slave from going away in sleep */
441         atomic_inc(THERM_REFCNT(family_data));
442         memset(rom, 0, sizeof(rom));
443
444         while (max_trying--) {
445
446                 verdict = 0;
447                 crc = 0;
448
449                 if (!w1_reset_select_slave(sl)) {
450                         int count = 0;
451                         unsigned int tm = 750;
452                         unsigned long sleep_rem;
453
454                         w1_write_8(dev, W1_READ_PSUPPLY);
455                         external_power = w1_read_8(dev);
456
457                         if (w1_reset_select_slave(sl))
458                                 continue;
459
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);
464
465                         w1_write_8(dev, W1_CONVERT_TEMP);
466
467                         if (external_power) {
468                                 mutex_unlock(&dev->bus_mutex);
469
470                                 sleep_rem = msleep_interruptible(tm);
471                                 if (sleep_rem != 0) {
472                                         ret = -EINTR;
473                                         goto post_unlock;
474                                 }
475
476                                 ret = mutex_lock_interruptible(&dev->bus_mutex);
477                                 if (ret != 0)
478                                         goto post_unlock;
479                         } else if (!w1_strong_pullup) {
480                                 sleep_rem = msleep_interruptible(tm);
481                                 if (sleep_rem != 0) {
482                                         ret = -EINTR;
483                                         goto pre_unlock;
484                                 }
485                         }
486
487                         if (!w1_reset_select_slave(sl)) {
488
489                                 w1_write_8(dev, W1_READ_SCRATCHPAD);
490                                 count = w1_read_block(dev, rom, 9);
491                                 if (count != 9) {
492                                         dev_warn(device, "w1_read_block() "
493                                                 "returned %u instead of 9.\n",
494                                                 count);
495                                 }
496
497                                 crc = w1_calc_crc8(rom, 8);
498
499                                 if (rom[8] == crc)
500                                         verdict = 1;
501                         }
502                 }
503
504                 if (verdict)
505                         break;
506         }
507
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");
512         if (verdict)
513                 memcpy(family_data, rom, sizeof(rom));
514         else
515                 dev_warn(device, "Read failed CRC check\n");
516
517         for (i = 0; i < 9; ++i)
518                 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
519                               ((u8 *)family_data)[i]);
520
521         c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
522                 w1_convert_temp(rom, sl->family->fid));
523         ret = PAGE_SIZE - c;
524
525 pre_unlock:
526         mutex_unlock(&dev->bus_mutex);
527
528 post_unlock:
529         atomic_dec(THERM_REFCNT(family_data));
530         return ret;
531 }
532
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)
545 {
546         struct w1_slave *sl = dev_to_w1_slave(device);
547         ssize_t c = PAGE_SIZE;
548         int rv;
549         int i;
550         u8 ack;
551         u64 rn;
552         struct w1_reg_num *reg_num;
553         int seq = 0;
554
555         mutex_lock(&sl->master->bus_mutex);
556         /* Place all devices in CHAIN state */
557         if (w1_reset_bus(sl->master))
558                 goto error;
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);
564
565         /* check for acknowledgment */
566         ack = w1_read_8(sl->master);
567         if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
568                 goto error;
569
570         /* In case the bus fails to send 0xFF, limit*/
571         for (i = 0; i <= 64; i++) {
572                 if (w1_reset_bus(sl->master))
573                         goto error;
574
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)
579                         break;
580                 if (sl->reg_num.id == reg_num->id)
581                         seq = i;
582
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));
587
588                 /* check for acknowledgment */
589                 ack = w1_read_8(sl->master);
590                 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
591                         goto error;
592
593         }
594
595         /* Exit from CHAIN state */
596         if (w1_reset_bus(sl->master))
597                 goto error;
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);
602
603         /* check for acknowledgment */
604         ack = w1_read_8(sl->master);
605         if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
606                 goto error;
607         mutex_unlock(&sl->master->bus_mutex);
608
609         c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq);
610         return PAGE_SIZE - c;
611 error:
612         mutex_unlock(&sl->master->bus_mutex);
613         return -EIO;
614 }
615
616 static int __init w1_therm_init(void)
617 {
618         int err, i;
619
620         for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
621                 err = w1_register_family(w1_therm_families[i].f);
622                 if (err)
623                         w1_therm_families[i].broken = 1;
624         }
625
626         return 0;
627 }
628
629 static void __exit w1_therm_fini(void)
630 {
631         int i;
632
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);
636 }
637
638 module_init(w1_therm_init);
639 module_exit(w1_therm_fini);
640
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));