w1_ds2413.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * w1_ds2413.c - w1 family 3a (DS2413) driver
  4. * based on w1_ds2408.c by Jean-Francois Dagenais <[email protected]>
  5. *
  6. * Copyright (c) 2013 Mariusz Bialonczyk <[email protected]>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/device.h>
  12. #include <linux/types.h>
  13. #include <linux/delay.h>
  14. #include <linux/slab.h>
  15. #include <linux/w1.h>
  16. #define W1_FAMILY_DS2413 0x3A
  17. #define W1_F3A_RETRIES 3
  18. #define W1_F3A_FUNC_PIO_ACCESS_READ 0xF5
  19. #define W1_F3A_FUNC_PIO_ACCESS_WRITE 0x5A
  20. #define W1_F3A_SUCCESS_CONFIRM_BYTE 0xAA
  21. #define W1_F3A_INVALID_PIO_STATE 0xFF
  22. static ssize_t state_read(struct file *filp, struct kobject *kobj,
  23. struct bin_attribute *bin_attr, char *buf, loff_t off,
  24. size_t count)
  25. {
  26. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  27. unsigned int retries = W1_F3A_RETRIES;
  28. ssize_t bytes_read = -EIO;
  29. u8 state;
  30. dev_dbg(&sl->dev,
  31. "Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p",
  32. bin_attr->attr.name, kobj, (unsigned int)off, count, buf);
  33. if (off != 0)
  34. return 0;
  35. if (!buf)
  36. return -EINVAL;
  37. mutex_lock(&sl->master->bus_mutex);
  38. dev_dbg(&sl->dev, "mutex locked");
  39. next:
  40. if (w1_reset_select_slave(sl))
  41. goto out;
  42. while (retries--) {
  43. w1_write_8(sl->master, W1_F3A_FUNC_PIO_ACCESS_READ);
  44. state = w1_read_8(sl->master);
  45. if ((state & 0x0F) == ((~state >> 4) & 0x0F)) {
  46. /* complement is correct */
  47. *buf = state;
  48. bytes_read = 1;
  49. goto out;
  50. } else if (state == W1_F3A_INVALID_PIO_STATE) {
  51. /* slave didn't respond, try to select it again */
  52. dev_warn(&sl->dev, "slave device did not respond to PIO_ACCESS_READ, " \
  53. "reselecting, retries left: %d\n", retries);
  54. goto next;
  55. }
  56. if (w1_reset_resume_command(sl->master))
  57. goto out; /* unrecoverable error */
  58. dev_warn(&sl->dev, "PIO_ACCESS_READ error, retries left: %d\n", retries);
  59. }
  60. out:
  61. mutex_unlock(&sl->master->bus_mutex);
  62. dev_dbg(&sl->dev, "%s, mutex unlocked, retries: %d\n",
  63. (bytes_read > 0) ? "succeeded" : "error", retries);
  64. return bytes_read;
  65. }
  66. static BIN_ATTR_RO(state, 1);
  67. static ssize_t output_write(struct file *filp, struct kobject *kobj,
  68. struct bin_attribute *bin_attr, char *buf,
  69. loff_t off, size_t count)
  70. {
  71. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  72. u8 w1_buf[3];
  73. unsigned int retries = W1_F3A_RETRIES;
  74. ssize_t bytes_written = -EIO;
  75. if (count != 1 || off != 0)
  76. return -EFAULT;
  77. dev_dbg(&sl->dev, "locking mutex for write_output");
  78. mutex_lock(&sl->master->bus_mutex);
  79. dev_dbg(&sl->dev, "mutex locked");
  80. if (w1_reset_select_slave(sl))
  81. goto out;
  82. /* according to the DS2413 datasheet the most significant 6 bits
  83. should be set to "1"s, so do it now */
  84. *buf = *buf | 0xFC;
  85. while (retries--) {
  86. w1_buf[0] = W1_F3A_FUNC_PIO_ACCESS_WRITE;
  87. w1_buf[1] = *buf;
  88. w1_buf[2] = ~(*buf);
  89. w1_write_block(sl->master, w1_buf, 3);
  90. if (w1_read_8(sl->master) == W1_F3A_SUCCESS_CONFIRM_BYTE) {
  91. bytes_written = 1;
  92. goto out;
  93. }
  94. if (w1_reset_resume_command(sl->master))
  95. goto out; /* unrecoverable error */
  96. dev_warn(&sl->dev, "PIO_ACCESS_WRITE error, retries left: %d\n", retries);
  97. }
  98. out:
  99. mutex_unlock(&sl->master->bus_mutex);
  100. dev_dbg(&sl->dev, "%s, mutex unlocked, retries: %d\n",
  101. (bytes_written > 0) ? "succeeded" : "error", retries);
  102. return bytes_written;
  103. }
  104. static BIN_ATTR(output, S_IRUGO | S_IWUSR | S_IWGRP, NULL, output_write, 1);
  105. static struct bin_attribute *w1_f3a_bin_attrs[] = {
  106. &bin_attr_state,
  107. &bin_attr_output,
  108. NULL,
  109. };
  110. static const struct attribute_group w1_f3a_group = {
  111. .bin_attrs = w1_f3a_bin_attrs,
  112. };
  113. static const struct attribute_group *w1_f3a_groups[] = {
  114. &w1_f3a_group,
  115. NULL,
  116. };
  117. static const struct w1_family_ops w1_f3a_fops = {
  118. .groups = w1_f3a_groups,
  119. };
  120. static struct w1_family w1_family_3a = {
  121. .fid = W1_FAMILY_DS2413,
  122. .fops = &w1_f3a_fops,
  123. };
  124. module_w1_family(w1_family_3a);
  125. MODULE_AUTHOR("Mariusz Bialonczyk <[email protected]>");
  126. MODULE_DESCRIPTION("w1 family 3a driver for DS2413 2 Pin IO");
  127. MODULE_LICENSE("GPL");
  128. MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2413));