gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / w1 / slaves / w1_therm.c
blobe028e00927991cd6aa1e91dcca6ce7fb9d5b79a1
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * w1_therm.c
5 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
6 */
8 #include <asm/types.h>
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>
20 #include <linux/w1.h>
22 #define W1_THERM_DS18S20 0x10
23 #define W1_THERM_DS1822 0x22
24 #define W1_THERM_DS18B20 0x28
25 #define W1_THERM_DS1825 0x3B
26 #define W1_THERM_DS28EA00 0x42
28 /* Allow the strong pullup to be disabled, but default to enabled.
29 * If it was disabled a parasite powered device might not get the require
30 * current to do a temperature conversion. If it is enabled parasite powered
31 * devices have a better chance of getting the current required.
32 * In case the parasite power-detection is not working (seems to be the case
33 * for some DS18S20) the strong pullup can also be forced, regardless of the
34 * power state of the devices.
36 * Summary of options:
37 * - strong_pullup = 0 Disable strong pullup completely
38 * - strong_pullup = 1 Enable automatic strong pullup detection
39 * - strong_pullup = 2 Force strong pullup
41 static int w1_strong_pullup = 1;
42 module_param_named(strong_pullup, w1_strong_pullup, int, 0);
44 struct w1_therm_family_data {
45 uint8_t rom[9];
46 atomic_t refcnt;
49 struct therm_info {
50 u8 rom[9];
51 u8 crc;
52 u8 verdict;
55 /* return the address of the refcnt in the family data */
56 #define THERM_REFCNT(family_data) \
57 (&((struct w1_therm_family_data *)family_data)->refcnt)
59 static int w1_therm_add_slave(struct w1_slave *sl)
61 sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
62 GFP_KERNEL);
63 if (!sl->family_data)
64 return -ENOMEM;
65 atomic_set(THERM_REFCNT(sl->family_data), 1);
66 return 0;
69 static void w1_therm_remove_slave(struct w1_slave *sl)
71 int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
73 while (refcnt) {
74 msleep(1000);
75 refcnt = atomic_read(THERM_REFCNT(sl->family_data));
77 kfree(sl->family_data);
78 sl->family_data = NULL;
81 static ssize_t w1_slave_show(struct device *device,
82 struct device_attribute *attr, char *buf);
84 static ssize_t w1_slave_store(struct device *device,
85 struct device_attribute *attr, const char *buf, size_t size);
87 static ssize_t w1_seq_show(struct device *device,
88 struct device_attribute *attr, char *buf);
90 static DEVICE_ATTR_RW(w1_slave);
91 static DEVICE_ATTR_RO(w1_seq);
93 static struct attribute *w1_therm_attrs[] = {
94 &dev_attr_w1_slave.attr,
95 NULL,
98 static struct attribute *w1_ds28ea00_attrs[] = {
99 &dev_attr_w1_slave.attr,
100 &dev_attr_w1_seq.attr,
101 NULL,
104 ATTRIBUTE_GROUPS(w1_therm);
105 ATTRIBUTE_GROUPS(w1_ds28ea00);
107 #if IS_REACHABLE(CONFIG_HWMON)
108 static int w1_read_temp(struct device *dev, u32 attr, int channel,
109 long *val);
111 static umode_t w1_is_visible(const void *_data, enum hwmon_sensor_types type,
112 u32 attr, int channel)
114 return attr == hwmon_temp_input ? 0444 : 0;
117 static int w1_read(struct device *dev, enum hwmon_sensor_types type,
118 u32 attr, int channel, long *val)
120 switch (type) {
121 case hwmon_temp:
122 return w1_read_temp(dev, attr, channel, val);
123 default:
124 return -EOPNOTSUPP;
128 static const u32 w1_temp_config[] = {
129 HWMON_T_INPUT,
133 static const struct hwmon_channel_info w1_temp = {
134 .type = hwmon_temp,
135 .config = w1_temp_config,
138 static const struct hwmon_channel_info *w1_info[] = {
139 &w1_temp,
140 NULL
143 static const struct hwmon_ops w1_hwmon_ops = {
144 .is_visible = w1_is_visible,
145 .read = w1_read,
148 static const struct hwmon_chip_info w1_chip_info = {
149 .ops = &w1_hwmon_ops,
150 .info = w1_info,
152 #define W1_CHIPINFO (&w1_chip_info)
153 #else
154 #define W1_CHIPINFO NULL
155 #endif
157 static struct w1_family_ops w1_therm_fops = {
158 .add_slave = w1_therm_add_slave,
159 .remove_slave = w1_therm_remove_slave,
160 .groups = w1_therm_groups,
161 .chip_info = W1_CHIPINFO,
164 static struct w1_family_ops w1_ds28ea00_fops = {
165 .add_slave = w1_therm_add_slave,
166 .remove_slave = w1_therm_remove_slave,
167 .groups = w1_ds28ea00_groups,
168 .chip_info = W1_CHIPINFO,
171 static struct w1_family w1_therm_family_DS18S20 = {
172 .fid = W1_THERM_DS18S20,
173 .fops = &w1_therm_fops,
176 static struct w1_family w1_therm_family_DS18B20 = {
177 .fid = W1_THERM_DS18B20,
178 .fops = &w1_therm_fops,
181 static struct w1_family w1_therm_family_DS1822 = {
182 .fid = W1_THERM_DS1822,
183 .fops = &w1_therm_fops,
186 static struct w1_family w1_therm_family_DS28EA00 = {
187 .fid = W1_THERM_DS28EA00,
188 .fops = &w1_ds28ea00_fops,
191 static struct w1_family w1_therm_family_DS1825 = {
192 .fid = W1_THERM_DS1825,
193 .fops = &w1_therm_fops,
196 struct w1_therm_family_converter {
197 u8 broken;
198 u16 reserved;
199 struct w1_family *f;
200 int (*convert)(u8 rom[9]);
201 int (*precision)(struct device *device, int val);
202 int (*eeprom)(struct device *device);
205 /* write configuration to eeprom */
206 static inline int w1_therm_eeprom(struct device *device);
208 /* Set precision for conversion */
209 static inline int w1_DS18B20_precision(struct device *device, int val);
210 static inline int w1_DS18S20_precision(struct device *device, int val);
212 /* The return value is millidegrees Centigrade. */
213 static inline int w1_DS18B20_convert_temp(u8 rom[9]);
214 static inline int w1_DS18S20_convert_temp(u8 rom[9]);
216 static struct w1_therm_family_converter w1_therm_families[] = {
218 .f = &w1_therm_family_DS18S20,
219 .convert = w1_DS18S20_convert_temp,
220 .precision = w1_DS18S20_precision,
221 .eeprom = w1_therm_eeprom
224 .f = &w1_therm_family_DS1822,
225 .convert = w1_DS18B20_convert_temp,
226 .precision = w1_DS18S20_precision,
227 .eeprom = w1_therm_eeprom
230 .f = &w1_therm_family_DS18B20,
231 .convert = w1_DS18B20_convert_temp,
232 .precision = w1_DS18B20_precision,
233 .eeprom = w1_therm_eeprom
236 .f = &w1_therm_family_DS28EA00,
237 .convert = w1_DS18B20_convert_temp,
238 .precision = w1_DS18S20_precision,
239 .eeprom = w1_therm_eeprom
242 .f = &w1_therm_family_DS1825,
243 .convert = w1_DS18B20_convert_temp,
244 .precision = w1_DS18S20_precision,
245 .eeprom = w1_therm_eeprom
249 static inline int w1_therm_eeprom(struct device *device)
251 struct w1_slave *sl = dev_to_w1_slave(device);
252 struct w1_master *dev = sl->master;
253 u8 rom[9], external_power;
254 int ret, max_trying = 10;
255 u8 *family_data = sl->family_data;
257 if (!sl->family_data) {
258 ret = -ENODEV;
259 goto error;
262 /* prevent the slave from going away in sleep */
263 atomic_inc(THERM_REFCNT(family_data));
265 ret = mutex_lock_interruptible(&dev->bus_mutex);
266 if (ret != 0)
267 goto dec_refcnt;
269 memset(rom, 0, sizeof(rom));
271 while (max_trying--) {
272 if (!w1_reset_select_slave(sl)) {
273 unsigned int tm = 10;
274 unsigned long sleep_rem;
276 /* check if in parasite mode */
277 w1_write_8(dev, W1_READ_PSUPPLY);
278 external_power = w1_read_8(dev);
280 if (w1_reset_select_slave(sl))
281 continue;
283 /* 10ms strong pullup/delay after the copy command */
284 if (w1_strong_pullup == 2 ||
285 (!external_power && w1_strong_pullup))
286 w1_next_pullup(dev, tm);
288 w1_write_8(dev, W1_COPY_SCRATCHPAD);
290 if (external_power) {
291 mutex_unlock(&dev->bus_mutex);
293 sleep_rem = msleep_interruptible(tm);
294 if (sleep_rem != 0) {
295 ret = -EINTR;
296 goto dec_refcnt;
299 ret = mutex_lock_interruptible(&dev->bus_mutex);
300 if (ret != 0)
301 goto dec_refcnt;
302 } else if (!w1_strong_pullup) {
303 sleep_rem = msleep_interruptible(tm);
304 if (sleep_rem != 0) {
305 ret = -EINTR;
306 goto mt_unlock;
310 break;
314 mt_unlock:
315 mutex_unlock(&dev->bus_mutex);
316 dec_refcnt:
317 atomic_dec(THERM_REFCNT(family_data));
318 error:
319 return ret;
322 /* DS18S20 does not feature configuration register */
323 static inline int w1_DS18S20_precision(struct device *device, int val)
325 return 0;
328 static inline int w1_DS18B20_precision(struct device *device, int val)
330 struct w1_slave *sl = dev_to_w1_slave(device);
331 struct w1_master *dev = sl->master;
332 u8 rom[9], crc;
333 int ret, max_trying = 10;
334 u8 *family_data = sl->family_data;
335 uint8_t precision_bits;
336 uint8_t mask = 0x60;
338 if (val > 12 || val < 9) {
339 pr_warn("Unsupported precision\n");
340 ret = -EINVAL;
341 goto error;
344 if (!sl->family_data) {
345 ret = -ENODEV;
346 goto error;
349 /* prevent the slave from going away in sleep */
350 atomic_inc(THERM_REFCNT(family_data));
352 ret = mutex_lock_interruptible(&dev->bus_mutex);
353 if (ret != 0)
354 goto dec_refcnt;
356 memset(rom, 0, sizeof(rom));
358 /* translate precision to bitmask (see datasheet page 9) */
359 switch (val) {
360 case 9:
361 precision_bits = 0x00;
362 break;
363 case 10:
364 precision_bits = 0x20;
365 break;
366 case 11:
367 precision_bits = 0x40;
368 break;
369 case 12:
370 default:
371 precision_bits = 0x60;
372 break;
375 while (max_trying--) {
376 crc = 0;
378 if (!w1_reset_select_slave(sl)) {
379 int count = 0;
381 /* read values to only alter precision bits */
382 w1_write_8(dev, W1_READ_SCRATCHPAD);
383 count = w1_read_block(dev, rom, 9);
384 if (count != 9)
385 dev_warn(device, "w1_read_block() returned %u instead of 9.\n", count);
387 crc = w1_calc_crc8(rom, 8);
388 if (rom[8] == crc) {
389 rom[4] = (rom[4] & ~mask) | (precision_bits & mask);
391 if (!w1_reset_select_slave(sl)) {
392 w1_write_8(dev, W1_WRITE_SCRATCHPAD);
393 w1_write_8(dev, rom[2]);
394 w1_write_8(dev, rom[3]);
395 w1_write_8(dev, rom[4]);
397 break;
403 mutex_unlock(&dev->bus_mutex);
404 dec_refcnt:
405 atomic_dec(THERM_REFCNT(family_data));
406 error:
407 return ret;
410 static inline int w1_DS18B20_convert_temp(u8 rom[9])
412 s16 t = le16_to_cpup((__le16 *)rom);
414 return t*1000/16;
417 static inline int w1_DS18S20_convert_temp(u8 rom[9])
419 int t, h;
421 if (!rom[7])
422 return 0;
424 if (rom[1] == 0)
425 t = ((s32)rom[0] >> 1)*1000;
426 else
427 t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
429 t -= 250;
430 h = 1000*((s32)rom[7] - (s32)rom[6]);
431 h /= (s32)rom[7];
432 t += h;
434 return t;
437 static inline int w1_convert_temp(u8 rom[9], u8 fid)
439 int i;
441 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
442 if (w1_therm_families[i].f->fid == fid)
443 return w1_therm_families[i].convert(rom);
445 return 0;
448 static ssize_t w1_slave_store(struct device *device,
449 struct device_attribute *attr, const char *buf,
450 size_t size)
452 int val, ret;
453 struct w1_slave *sl = dev_to_w1_slave(device);
454 int i;
456 ret = kstrtoint(buf, 0, &val);
457 if (ret)
458 return ret;
460 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
461 if (w1_therm_families[i].f->fid == sl->family->fid) {
462 /* zero value indicates to write current configuration to eeprom */
463 if (val == 0)
464 ret = w1_therm_families[i].eeprom(device);
465 else
466 ret = w1_therm_families[i].precision(device, val);
467 break;
470 return ret ? : size;
473 static ssize_t read_therm(struct device *device,
474 struct w1_slave *sl, struct therm_info *info)
476 struct w1_master *dev = sl->master;
477 u8 external_power;
478 int ret, max_trying = 10;
479 u8 *family_data = sl->family_data;
481 if (!family_data) {
482 ret = -ENODEV;
483 goto error;
486 /* prevent the slave from going away in sleep */
487 atomic_inc(THERM_REFCNT(family_data));
489 ret = mutex_lock_interruptible(&dev->bus_mutex);
490 if (ret != 0)
491 goto dec_refcnt;
493 memset(info->rom, 0, sizeof(info->rom));
495 while (max_trying--) {
497 info->verdict = 0;
498 info->crc = 0;
500 if (!w1_reset_select_slave(sl)) {
501 int count = 0;
502 unsigned int tm = 750;
503 unsigned long sleep_rem;
505 w1_write_8(dev, W1_READ_PSUPPLY);
506 external_power = w1_read_8(dev);
508 if (w1_reset_select_slave(sl))
509 continue;
511 /* 750ms strong pullup (or delay) after the convert */
512 if (w1_strong_pullup == 2 ||
513 (!external_power && w1_strong_pullup))
514 w1_next_pullup(dev, tm);
516 w1_write_8(dev, W1_CONVERT_TEMP);
518 if (external_power) {
519 mutex_unlock(&dev->bus_mutex);
521 sleep_rem = msleep_interruptible(tm);
522 if (sleep_rem != 0) {
523 ret = -EINTR;
524 goto dec_refcnt;
527 ret = mutex_lock_interruptible(&dev->bus_mutex);
528 if (ret != 0)
529 goto dec_refcnt;
530 } else if (!w1_strong_pullup) {
531 sleep_rem = msleep_interruptible(tm);
532 if (sleep_rem != 0) {
533 ret = -EINTR;
534 goto mt_unlock;
538 if (!w1_reset_select_slave(sl)) {
540 w1_write_8(dev, W1_READ_SCRATCHPAD);
541 count = w1_read_block(dev, info->rom, 9);
542 if (count != 9) {
543 dev_warn(device, "w1_read_block() "
544 "returned %u instead of 9.\n",
545 count);
548 info->crc = w1_calc_crc8(info->rom, 8);
550 if (info->rom[8] == info->crc)
551 info->verdict = 1;
555 if (info->verdict)
556 break;
559 mt_unlock:
560 mutex_unlock(&dev->bus_mutex);
561 dec_refcnt:
562 atomic_dec(THERM_REFCNT(family_data));
563 error:
564 return ret;
567 static ssize_t w1_slave_show(struct device *device,
568 struct device_attribute *attr, char *buf)
570 struct w1_slave *sl = dev_to_w1_slave(device);
571 struct therm_info info;
572 u8 *family_data = sl->family_data;
573 int ret, i;
574 ssize_t c = PAGE_SIZE;
575 u8 fid = sl->family->fid;
577 ret = read_therm(device, sl, &info);
578 if (ret)
579 return ret;
581 for (i = 0; i < 9; ++i)
582 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]);
583 c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
584 info.crc, (info.verdict) ? "YES" : "NO");
585 if (info.verdict)
586 memcpy(family_data, info.rom, sizeof(info.rom));
587 else
588 dev_warn(device, "Read failed CRC check\n");
590 for (i = 0; i < 9; ++i)
591 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
592 ((u8 *)family_data)[i]);
594 c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
595 w1_convert_temp(info.rom, fid));
596 ret = PAGE_SIZE - c;
597 return ret;
600 #if IS_REACHABLE(CONFIG_HWMON)
601 static int w1_read_temp(struct device *device, u32 attr, int channel,
602 long *val)
604 struct w1_slave *sl = dev_get_drvdata(device);
605 struct therm_info info;
606 u8 fid = sl->family->fid;
607 int ret;
609 switch (attr) {
610 case hwmon_temp_input:
611 ret = read_therm(device, sl, &info);
612 if (ret)
613 return ret;
615 if (!info.verdict) {
616 ret = -EIO;
617 return ret;
620 *val = w1_convert_temp(info.rom, fid);
621 ret = 0;
622 break;
623 default:
624 ret = -EOPNOTSUPP;
625 break;
628 return ret;
630 #endif
632 #define W1_42_CHAIN 0x99
633 #define W1_42_CHAIN_OFF 0x3C
634 #define W1_42_CHAIN_OFF_INV 0xC3
635 #define W1_42_CHAIN_ON 0x5A
636 #define W1_42_CHAIN_ON_INV 0xA5
637 #define W1_42_CHAIN_DONE 0x96
638 #define W1_42_CHAIN_DONE_INV 0x69
639 #define W1_42_COND_READ 0x0F
640 #define W1_42_SUCCESS_CONFIRM_BYTE 0xAA
641 #define W1_42_FINISHED_BYTE 0xFF
642 static ssize_t w1_seq_show(struct device *device,
643 struct device_attribute *attr, char *buf)
645 struct w1_slave *sl = dev_to_w1_slave(device);
646 ssize_t c = PAGE_SIZE;
647 int rv;
648 int i;
649 u8 ack;
650 u64 rn;
651 struct w1_reg_num *reg_num;
652 int seq = 0;
654 mutex_lock(&sl->master->bus_mutex);
655 /* Place all devices in CHAIN state */
656 if (w1_reset_bus(sl->master))
657 goto error;
658 w1_write_8(sl->master, W1_SKIP_ROM);
659 w1_write_8(sl->master, W1_42_CHAIN);
660 w1_write_8(sl->master, W1_42_CHAIN_ON);
661 w1_write_8(sl->master, W1_42_CHAIN_ON_INV);
662 msleep(sl->master->pullup_duration);
664 /* check for acknowledgment */
665 ack = w1_read_8(sl->master);
666 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
667 goto error;
669 /* In case the bus fails to send 0xFF, limit*/
670 for (i = 0; i <= 64; i++) {
671 if (w1_reset_bus(sl->master))
672 goto error;
674 w1_write_8(sl->master, W1_42_COND_READ);
675 rv = w1_read_block(sl->master, (u8 *)&rn, 8);
676 reg_num = (struct w1_reg_num *) &rn;
677 if (reg_num->family == W1_42_FINISHED_BYTE)
678 break;
679 if (sl->reg_num.id == reg_num->id)
680 seq = i;
682 w1_write_8(sl->master, W1_42_CHAIN);
683 w1_write_8(sl->master, W1_42_CHAIN_DONE);
684 w1_write_8(sl->master, W1_42_CHAIN_DONE_INV);
685 w1_read_block(sl->master, &ack, sizeof(ack));
687 /* check for acknowledgment */
688 ack = w1_read_8(sl->master);
689 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
690 goto error;
694 /* Exit from CHAIN state */
695 if (w1_reset_bus(sl->master))
696 goto error;
697 w1_write_8(sl->master, W1_SKIP_ROM);
698 w1_write_8(sl->master, W1_42_CHAIN);
699 w1_write_8(sl->master, W1_42_CHAIN_OFF);
700 w1_write_8(sl->master, W1_42_CHAIN_OFF_INV);
702 /* check for acknowledgment */
703 ack = w1_read_8(sl->master);
704 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
705 goto error;
706 mutex_unlock(&sl->master->bus_mutex);
708 c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq);
709 return PAGE_SIZE - c;
710 error:
711 mutex_unlock(&sl->master->bus_mutex);
712 return -EIO;
715 static int __init w1_therm_init(void)
717 int err, i;
719 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
720 err = w1_register_family(w1_therm_families[i].f);
721 if (err)
722 w1_therm_families[i].broken = 1;
725 return 0;
728 static void __exit w1_therm_fini(void)
730 int i;
732 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
733 if (!w1_therm_families[i].broken)
734 w1_unregister_family(w1_therm_families[i].f);
737 module_init(w1_therm_init);
738 module_exit(w1_therm_fini);
740 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
741 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
742 MODULE_LICENSE("GPL");
743 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20));
744 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822));
745 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20));
746 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825));
747 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00));