w1_ds2406.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * w1_ds2406.c - w1 family 12 (DS2406) driver
  4. * based on w1_ds2413.c by Mariusz Bialonczyk <[email protected]>
  5. *
  6. * Copyright (c) 2014 Scott Alfter <[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/crc16.h>
  16. #include <linux/w1.h>
  17. #define W1_FAMILY_DS2406 0x12
  18. #define W1_F12_FUNC_READ_STATUS 0xAA
  19. #define W1_F12_FUNC_WRITE_STATUS 0x55
  20. static ssize_t w1_f12_read_state(
  21. struct file *filp, struct kobject *kobj,
  22. struct bin_attribute *bin_attr,
  23. char *buf, loff_t off, size_t count)
  24. {
  25. u8 w1_buf[6]={W1_F12_FUNC_READ_STATUS, 7, 0, 0, 0, 0};
  26. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  27. u16 crc=0;
  28. int i;
  29. ssize_t rtnval=1;
  30. if (off != 0)
  31. return 0;
  32. if (!buf)
  33. return -EINVAL;
  34. mutex_lock(&sl->master->bus_mutex);
  35. if (w1_reset_select_slave(sl)) {
  36. mutex_unlock(&sl->master->bus_mutex);
  37. return -EIO;
  38. }
  39. w1_write_block(sl->master, w1_buf, 3);
  40. w1_read_block(sl->master, w1_buf+3, 3);
  41. for (i=0; i<6; i++)
  42. crc=crc16_byte(crc, w1_buf[i]);
  43. if (crc==0xb001) /* good read? */
  44. *buf=((w1_buf[3]>>5)&3)|0x30;
  45. else
  46. rtnval=-EIO;
  47. mutex_unlock(&sl->master->bus_mutex);
  48. return rtnval;
  49. }
  50. static ssize_t w1_f12_write_output(
  51. struct file *filp, struct kobject *kobj,
  52. struct bin_attribute *bin_attr,
  53. char *buf, loff_t off, size_t count)
  54. {
  55. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  56. u8 w1_buf[6]={W1_F12_FUNC_WRITE_STATUS, 7, 0, 0, 0, 0};
  57. u16 crc=0;
  58. int i;
  59. ssize_t rtnval=1;
  60. if (count != 1 || off != 0)
  61. return -EFAULT;
  62. mutex_lock(&sl->master->bus_mutex);
  63. if (w1_reset_select_slave(sl)) {
  64. mutex_unlock(&sl->master->bus_mutex);
  65. return -EIO;
  66. }
  67. w1_buf[3] = (((*buf)&3)<<5)|0x1F;
  68. w1_write_block(sl->master, w1_buf, 4);
  69. w1_read_block(sl->master, w1_buf+4, 2);
  70. for (i=0; i<6; i++)
  71. crc=crc16_byte(crc, w1_buf[i]);
  72. if (crc==0xb001) /* good read? */
  73. w1_write_8(sl->master, 0xFF);
  74. else
  75. rtnval=-EIO;
  76. mutex_unlock(&sl->master->bus_mutex);
  77. return rtnval;
  78. }
  79. #define NB_SYSFS_BIN_FILES 2
  80. static struct bin_attribute w1_f12_sysfs_bin_files[NB_SYSFS_BIN_FILES] = {
  81. {
  82. .attr = {
  83. .name = "state",
  84. .mode = S_IRUGO,
  85. },
  86. .size = 1,
  87. .read = w1_f12_read_state,
  88. },
  89. {
  90. .attr = {
  91. .name = "output",
  92. .mode = S_IRUGO | S_IWUSR | S_IWGRP,
  93. },
  94. .size = 1,
  95. .write = w1_f12_write_output,
  96. }
  97. };
  98. static int w1_f12_add_slave(struct w1_slave *sl)
  99. {
  100. int err = 0;
  101. int i;
  102. for (i = 0; i < NB_SYSFS_BIN_FILES && !err; ++i)
  103. err = sysfs_create_bin_file(
  104. &sl->dev.kobj,
  105. &(w1_f12_sysfs_bin_files[i]));
  106. if (err)
  107. while (--i >= 0)
  108. sysfs_remove_bin_file(&sl->dev.kobj,
  109. &(w1_f12_sysfs_bin_files[i]));
  110. return err;
  111. }
  112. static void w1_f12_remove_slave(struct w1_slave *sl)
  113. {
  114. int i;
  115. for (i = NB_SYSFS_BIN_FILES - 1; i >= 0; --i)
  116. sysfs_remove_bin_file(&sl->dev.kobj,
  117. &(w1_f12_sysfs_bin_files[i]));
  118. }
  119. static const struct w1_family_ops w1_f12_fops = {
  120. .add_slave = w1_f12_add_slave,
  121. .remove_slave = w1_f12_remove_slave,
  122. };
  123. static struct w1_family w1_family_12 = {
  124. .fid = W1_FAMILY_DS2406,
  125. .fops = &w1_f12_fops,
  126. };
  127. module_w1_family(w1_family_12);
  128. MODULE_AUTHOR("Scott Alfter <[email protected]>");
  129. MODULE_DESCRIPTION("w1 family 12 driver for DS2406 2 Pin IO");
  130. MODULE_LICENSE("GPL");