w1_ds2423.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * w1_ds2423.c
  4. *
  5. * Copyright (c) 2010 Mika Laitio <[email protected]>
  6. *
  7. * This driver will read and write the value of 4 counters to w1_slave file in
  8. * sys filesystem.
  9. * Inspired by the w1_therm and w1_ds2431 drivers.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/device.h>
  15. #include <linux/types.h>
  16. #include <linux/delay.h>
  17. #include <linux/crc16.h>
  18. #include <linux/w1.h>
  19. #define W1_COUNTER_DS2423 0x1D
  20. #define CRC16_VALID 0xb001
  21. #define CRC16_INIT 0
  22. #define COUNTER_COUNT 4
  23. #define READ_BYTE_COUNT 42
  24. static ssize_t w1_slave_show(struct device *device,
  25. struct device_attribute *attr, char *out_buf)
  26. {
  27. struct w1_slave *sl = dev_to_w1_slave(device);
  28. struct w1_master *dev = sl->master;
  29. u8 rbuf[COUNTER_COUNT * READ_BYTE_COUNT];
  30. u8 wrbuf[3];
  31. int rom_addr;
  32. int read_byte_count;
  33. int result;
  34. ssize_t c;
  35. int ii;
  36. int p;
  37. int crc;
  38. c = PAGE_SIZE;
  39. rom_addr = (12 << 5) + 31;
  40. wrbuf[0] = 0xA5;
  41. wrbuf[1] = rom_addr & 0xFF;
  42. wrbuf[2] = rom_addr >> 8;
  43. mutex_lock(&dev->bus_mutex);
  44. if (!w1_reset_select_slave(sl)) {
  45. w1_write_block(dev, wrbuf, 3);
  46. read_byte_count = 0;
  47. for (p = 0; p < 4; p++) {
  48. /*
  49. * 1 byte for first bytes in ram page read
  50. * 4 bytes for counter
  51. * 4 bytes for zero bits
  52. * 2 bytes for crc
  53. * 31 remaining bytes from the ram page
  54. */
  55. read_byte_count += w1_read_block(dev,
  56. rbuf + (p * READ_BYTE_COUNT), READ_BYTE_COUNT);
  57. for (ii = 0; ii < READ_BYTE_COUNT; ++ii)
  58. c -= snprintf(out_buf + PAGE_SIZE - c,
  59. c, "%02x ",
  60. rbuf[(p * READ_BYTE_COUNT) + ii]);
  61. if (read_byte_count != (p + 1) * READ_BYTE_COUNT) {
  62. dev_warn(device,
  63. "w1_counter_read() returned %u bytes "
  64. "instead of %d bytes wanted.\n",
  65. read_byte_count,
  66. READ_BYTE_COUNT);
  67. c -= snprintf(out_buf + PAGE_SIZE - c,
  68. c, "crc=NO\n");
  69. } else {
  70. if (p == 0) {
  71. crc = crc16(CRC16_INIT, wrbuf, 3);
  72. crc = crc16(crc, rbuf, 11);
  73. } else {
  74. /*
  75. * DS2423 calculates crc from all bytes
  76. * read after the previous crc bytes.
  77. */
  78. crc = crc16(CRC16_INIT,
  79. (rbuf + 11) +
  80. ((p - 1) * READ_BYTE_COUNT),
  81. READ_BYTE_COUNT);
  82. }
  83. if (crc == CRC16_VALID) {
  84. result = 0;
  85. for (ii = 4; ii > 0; ii--) {
  86. result <<= 8;
  87. result |= rbuf[(p *
  88. READ_BYTE_COUNT) + ii];
  89. }
  90. c -= snprintf(out_buf + PAGE_SIZE - c,
  91. c, "crc=YES c=%d\n", result);
  92. } else {
  93. c -= snprintf(out_buf + PAGE_SIZE - c,
  94. c, "crc=NO\n");
  95. }
  96. }
  97. }
  98. } else {
  99. c -= snprintf(out_buf + PAGE_SIZE - c, c, "Connection error");
  100. }
  101. mutex_unlock(&dev->bus_mutex);
  102. return PAGE_SIZE - c;
  103. }
  104. static DEVICE_ATTR_RO(w1_slave);
  105. static struct attribute *w1_f1d_attrs[] = {
  106. &dev_attr_w1_slave.attr,
  107. NULL,
  108. };
  109. ATTRIBUTE_GROUPS(w1_f1d);
  110. static const struct w1_family_ops w1_f1d_fops = {
  111. .groups = w1_f1d_groups,
  112. };
  113. static struct w1_family w1_family_1d = {
  114. .fid = W1_COUNTER_DS2423,
  115. .fops = &w1_f1d_fops,
  116. };
  117. module_w1_family(w1_family_1d);
  118. MODULE_AUTHOR("Mika Laitio <[email protected]>");
  119. MODULE_DESCRIPTION("w1 family 1d driver for DS2423, 4 counters and 4kb ram");
  120. MODULE_LICENSE("GPL");
  121. MODULE_ALIAS("w1-family-" __stringify(W1_COUNTER_DS2423));