w1_ds2413.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * w1_ds2413.c - w1 family 3a (DS2413) driver
  3. * based on w1_ds2408.c by Jean-Francois Dagenais <dagenaisj@sonatest.com>
  4. *
  5. * Copyright (c) 2013 Mariusz Bialonczyk <manio@skyboo.net>
  6. *
  7. * This source code is licensed under the GNU General Public License,
  8. * Version 2. See the file COPYING for more details.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/device.h>
  14. #include <linux/types.h>
  15. #include <linux/delay.h>
  16. #include <linux/slab.h>
  17. #include "../w1.h"
  18. #include "../w1_int.h"
  19. #include "../w1_family.h"
  20. MODULE_LICENSE("GPL");
  21. MODULE_AUTHOR("Mariusz Bialonczyk <manio@skyboo.net>");
  22. MODULE_DESCRIPTION("w1 family 3a driver for DS2413 2 Pin IO");
  23. MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2413));
  24. #define W1_F3A_RETRIES 3
  25. #define W1_F3A_FUNC_PIO_ACCESS_READ 0xF5
  26. #define W1_F3A_FUNC_PIO_ACCESS_WRITE 0x5A
  27. #define W1_F3A_SUCCESS_CONFIRM_BYTE 0xAA
  28. static ssize_t state_read(struct file *filp, struct kobject *kobj,
  29. struct bin_attribute *bin_attr, char *buf, loff_t off,
  30. size_t count)
  31. {
  32. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  33. dev_dbg(&sl->dev,
  34. "Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p",
  35. bin_attr->attr.name, kobj, (unsigned int)off, count, buf);
  36. if (off != 0)
  37. return 0;
  38. if (!buf)
  39. return -EINVAL;
  40. mutex_lock(&sl->master->bus_mutex);
  41. dev_dbg(&sl->dev, "mutex locked");
  42. if (w1_reset_select_slave(sl)) {
  43. mutex_unlock(&sl->master->bus_mutex);
  44. return -EIO;
  45. }
  46. w1_write_8(sl->master, W1_F3A_FUNC_PIO_ACCESS_READ);
  47. *buf = w1_read_8(sl->master);
  48. mutex_unlock(&sl->master->bus_mutex);
  49. dev_dbg(&sl->dev, "mutex unlocked");
  50. /* check for correct complement */
  51. if ((*buf & 0x0F) != ((~*buf >> 4) & 0x0F))
  52. return -EIO;
  53. else
  54. return 1;
  55. }
  56. static BIN_ATTR_RO(state, 1);
  57. static ssize_t output_write(struct file *filp, struct kobject *kobj,
  58. struct bin_attribute *bin_attr, char *buf,
  59. loff_t off, size_t count)
  60. {
  61. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  62. u8 w1_buf[3];
  63. unsigned int retries = W1_F3A_RETRIES;
  64. if (count != 1 || off != 0)
  65. return -EFAULT;
  66. dev_dbg(&sl->dev, "locking mutex for write_output");
  67. mutex_lock(&sl->master->bus_mutex);
  68. dev_dbg(&sl->dev, "mutex locked");
  69. if (w1_reset_select_slave(sl))
  70. goto error;
  71. /* according to the DS2413 datasheet the most significant 6 bits
  72. should be set to "1"s, so do it now */
  73. *buf = *buf | 0xFC;
  74. while (retries--) {
  75. w1_buf[0] = W1_F3A_FUNC_PIO_ACCESS_WRITE;
  76. w1_buf[1] = *buf;
  77. w1_buf[2] = ~(*buf);
  78. w1_write_block(sl->master, w1_buf, 3);
  79. if (w1_read_8(sl->master) == W1_F3A_SUCCESS_CONFIRM_BYTE) {
  80. mutex_unlock(&sl->master->bus_mutex);
  81. dev_dbg(&sl->dev, "mutex unlocked, retries:%d", retries);
  82. return 1;
  83. }
  84. if (w1_reset_resume_command(sl->master))
  85. goto error;
  86. }
  87. error:
  88. mutex_unlock(&sl->master->bus_mutex);
  89. dev_dbg(&sl->dev, "mutex unlocked in error, retries:%d", retries);
  90. return -EIO;
  91. }
  92. static BIN_ATTR(output, S_IRUGO | S_IWUSR | S_IWGRP, NULL, output_write, 1);
  93. static struct bin_attribute *w1_f3a_bin_attrs[] = {
  94. &bin_attr_state,
  95. &bin_attr_output,
  96. NULL,
  97. };
  98. static const struct attribute_group w1_f3a_group = {
  99. .bin_attrs = w1_f3a_bin_attrs,
  100. };
  101. static const struct attribute_group *w1_f3a_groups[] = {
  102. &w1_f3a_group,
  103. NULL,
  104. };
  105. static struct w1_family_ops w1_f3a_fops = {
  106. .groups = w1_f3a_groups,
  107. };
  108. static struct w1_family w1_family_3a = {
  109. .fid = W1_FAMILY_DS2413,
  110. .fops = &w1_f3a_fops,
  111. };
  112. static int __init w1_f3a_init(void)
  113. {
  114. return w1_register_family(&w1_family_3a);
  115. }
  116. static void __exit w1_f3a_exit(void)
  117. {
  118. w1_unregister_family(&w1_family_3a);
  119. }
  120. module_init(w1_f3a_init);
  121. module_exit(w1_f3a_exit);