w1_ds2405.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * w1_ds2405.c
  3. *
  4. * Copyright (c) 2017 Maciej S. Szmigiero <mail@maciej.szmigiero.name>
  5. * Based on w1_therm.c copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the therms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/device.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/mutex.h>
  23. #include <linux/string.h>
  24. #include <linux/types.h>
  25. #include <linux/w1.h>
  26. #define W1_FAMILY_DS2405 0x05
  27. MODULE_LICENSE("GPL");
  28. MODULE_AUTHOR("Maciej S. Szmigiero <mail@maciej.szmigiero.name>");
  29. MODULE_DESCRIPTION("Driver for 1-wire Dallas DS2405 PIO.");
  30. MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2405));
  31. static int w1_ds2405_select(struct w1_slave *sl, bool only_active)
  32. {
  33. struct w1_master *dev = sl->master;
  34. u64 dev_addr = le64_to_cpu(*(u64 *)&sl->reg_num);
  35. unsigned int bit_ctr;
  36. if (w1_reset_bus(dev) != 0)
  37. return 0;
  38. /*
  39. * We cannot use a normal Match ROM command
  40. * since doing so would toggle PIO state
  41. */
  42. w1_write_8(dev, only_active ? W1_ALARM_SEARCH : W1_SEARCH);
  43. for (bit_ctr = 0; bit_ctr < 64; bit_ctr++) {
  44. int bit2send = !!(dev_addr & BIT(bit_ctr));
  45. u8 ret;
  46. ret = w1_triplet(dev, bit2send);
  47. if ((ret & (BIT(0) | BIT(1))) ==
  48. (BIT(0) | BIT(1))) /* no devices found */
  49. return 0;
  50. if (!!(ret & BIT(2)) != bit2send)
  51. /* wrong direction taken - no such device */
  52. return 0;
  53. }
  54. return 1;
  55. }
  56. static int w1_ds2405_read_pio(struct w1_slave *sl)
  57. {
  58. if (w1_ds2405_select(sl, true))
  59. return 0; /* "active" means PIO is low */
  60. if (w1_ds2405_select(sl, false))
  61. return 1;
  62. return -ENODEV;
  63. }
  64. static ssize_t state_show(struct device *device,
  65. struct device_attribute *attr, char *buf)
  66. {
  67. struct w1_slave *sl = dev_to_w1_slave(device);
  68. struct w1_master *dev = sl->master;
  69. int ret;
  70. ssize_t f_retval;
  71. u8 state;
  72. ret = mutex_lock_interruptible(&dev->bus_mutex);
  73. if (ret)
  74. return ret;
  75. if (!w1_ds2405_select(sl, false)) {
  76. f_retval = -ENODEV;
  77. goto out_unlock;
  78. }
  79. state = w1_read_8(dev);
  80. if (state != 0 &&
  81. state != 0xff) {
  82. dev_err(device, "non-consistent state %x\n", state);
  83. f_retval = -EIO;
  84. goto out_unlock;
  85. }
  86. *buf = state ? '1' : '0';
  87. f_retval = 1;
  88. out_unlock:
  89. w1_reset_bus(dev);
  90. mutex_unlock(&dev->bus_mutex);
  91. return f_retval;
  92. }
  93. static ssize_t output_show(struct device *device,
  94. struct device_attribute *attr, char *buf)
  95. {
  96. struct w1_slave *sl = dev_to_w1_slave(device);
  97. struct w1_master *dev = sl->master;
  98. int ret;
  99. ssize_t f_retval;
  100. ret = mutex_lock_interruptible(&dev->bus_mutex);
  101. if (ret)
  102. return ret;
  103. ret = w1_ds2405_read_pio(sl);
  104. if (ret < 0) {
  105. f_retval = ret;
  106. goto out_unlock;
  107. }
  108. *buf = ret ? '1' : '0';
  109. f_retval = 1;
  110. out_unlock:
  111. w1_reset_bus(dev);
  112. mutex_unlock(&dev->bus_mutex);
  113. return f_retval;
  114. }
  115. static ssize_t output_store(struct device *device,
  116. struct device_attribute *attr,
  117. const char *buf, size_t count)
  118. {
  119. struct w1_slave *sl = dev_to_w1_slave(device);
  120. struct w1_master *dev = sl->master;
  121. int ret, current_pio;
  122. unsigned int val;
  123. ssize_t f_retval;
  124. if (count < 1)
  125. return -EINVAL;
  126. if (sscanf(buf, " %u%n", &val, &ret) < 1)
  127. return -EINVAL;
  128. if (val != 0 && val != 1)
  129. return -EINVAL;
  130. f_retval = ret;
  131. ret = mutex_lock_interruptible(&dev->bus_mutex);
  132. if (ret)
  133. return ret;
  134. current_pio = w1_ds2405_read_pio(sl);
  135. if (current_pio < 0) {
  136. f_retval = current_pio;
  137. goto out_unlock;
  138. }
  139. if (current_pio == val)
  140. goto out_unlock;
  141. if (w1_reset_bus(dev) != 0) {
  142. f_retval = -ENODEV;
  143. goto out_unlock;
  144. }
  145. /*
  146. * can't use w1_reset_select_slave() here since it uses Skip ROM if
  147. * there is only one device on bus
  148. */
  149. do {
  150. u64 dev_addr = le64_to_cpu(*(u64 *)&sl->reg_num);
  151. u8 cmd[9];
  152. cmd[0] = W1_MATCH_ROM;
  153. memcpy(&cmd[1], &dev_addr, sizeof(dev_addr));
  154. w1_write_block(dev, cmd, sizeof(cmd));
  155. } while (0);
  156. out_unlock:
  157. w1_reset_bus(dev);
  158. mutex_unlock(&dev->bus_mutex);
  159. return f_retval;
  160. }
  161. static DEVICE_ATTR_RO(state);
  162. static DEVICE_ATTR_RW(output);
  163. static struct attribute *w1_ds2405_attrs[] = {
  164. &dev_attr_state.attr,
  165. &dev_attr_output.attr,
  166. NULL
  167. };
  168. ATTRIBUTE_GROUPS(w1_ds2405);
  169. static struct w1_family_ops w1_ds2405_fops = {
  170. .groups = w1_ds2405_groups
  171. };
  172. static struct w1_family w1_family_ds2405 = {
  173. .fid = W1_FAMILY_DS2405,
  174. .fops = &w1_ds2405_fops
  175. };
  176. module_w1_family(w1_family_ds2405);