w1_ds2408.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * w1_ds2408.c - w1 family 29 (DS2408) driver
  3. *
  4. * Copyright (c) 2010 Jean-Francois Dagenais <dagenaisj@sonatest.com>
  5. *
  6. * This source code is licensed under the GNU General Public License,
  7. * Version 2. See the file COPYING for more details.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/device.h>
  13. #include <linux/types.h>
  14. #include <linux/delay.h>
  15. #include <linux/slab.h>
  16. #include "../w1.h"
  17. #include "../w1_int.h"
  18. #include "../w1_family.h"
  19. MODULE_LICENSE("GPL");
  20. MODULE_AUTHOR("Jean-Francois Dagenais <dagenaisj@sonatest.com>");
  21. MODULE_DESCRIPTION("w1 family 29 driver for DS2408 8 Pin IO");
  22. MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2408));
  23. #define W1_F29_RETRIES 3
  24. #define W1_F29_REG_LOGIG_STATE 0x88 /* R */
  25. #define W1_F29_REG_OUTPUT_LATCH_STATE 0x89 /* R */
  26. #define W1_F29_REG_ACTIVITY_LATCH_STATE 0x8A /* R */
  27. #define W1_F29_REG_COND_SEARCH_SELECT_MASK 0x8B /* RW */
  28. #define W1_F29_REG_COND_SEARCH_POL_SELECT 0x8C /* RW */
  29. #define W1_F29_REG_CONTROL_AND_STATUS 0x8D /* RW */
  30. #define W1_F29_FUNC_READ_PIO_REGS 0xF0
  31. #define W1_F29_FUNC_CHANN_ACCESS_READ 0xF5
  32. #define W1_F29_FUNC_CHANN_ACCESS_WRITE 0x5A
  33. /* also used to write the control/status reg (0x8D): */
  34. #define W1_F29_FUNC_WRITE_COND_SEARCH_REG 0xCC
  35. #define W1_F29_FUNC_RESET_ACTIVITY_LATCHES 0xC3
  36. #define W1_F29_SUCCESS_CONFIRM_BYTE 0xAA
  37. static int _read_reg(struct w1_slave *sl, u8 address, unsigned char* buf)
  38. {
  39. u8 wrbuf[3];
  40. dev_dbg(&sl->dev,
  41. "Reading with slave: %p, reg addr: %0#4x, buff addr: %p",
  42. sl, (unsigned int)address, buf);
  43. if (!buf)
  44. return -EINVAL;
  45. mutex_lock(&sl->master->bus_mutex);
  46. dev_dbg(&sl->dev, "mutex locked");
  47. if (w1_reset_select_slave(sl)) {
  48. mutex_unlock(&sl->master->bus_mutex);
  49. return -EIO;
  50. }
  51. wrbuf[0] = W1_F29_FUNC_READ_PIO_REGS;
  52. wrbuf[1] = address;
  53. wrbuf[2] = 0;
  54. w1_write_block(sl->master, wrbuf, 3);
  55. *buf = w1_read_8(sl->master);
  56. mutex_unlock(&sl->master->bus_mutex);
  57. dev_dbg(&sl->dev, "mutex unlocked");
  58. return 1;
  59. }
  60. static ssize_t state_read(struct file *filp, struct kobject *kobj,
  61. struct bin_attribute *bin_attr, char *buf, loff_t off,
  62. size_t count)
  63. {
  64. dev_dbg(&kobj_to_w1_slave(kobj)->dev,
  65. "Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p",
  66. bin_attr->attr.name, kobj, (unsigned int)off, count, buf);
  67. if (count != 1 || off != 0)
  68. return -EFAULT;
  69. return _read_reg(kobj_to_w1_slave(kobj), W1_F29_REG_LOGIG_STATE, buf);
  70. }
  71. static ssize_t output_read(struct file *filp, struct kobject *kobj,
  72. struct bin_attribute *bin_attr, char *buf,
  73. loff_t off, size_t count)
  74. {
  75. dev_dbg(&kobj_to_w1_slave(kobj)->dev,
  76. "Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p",
  77. bin_attr->attr.name, kobj, (unsigned int)off, count, buf);
  78. if (count != 1 || off != 0)
  79. return -EFAULT;
  80. return _read_reg(kobj_to_w1_slave(kobj),
  81. W1_F29_REG_OUTPUT_LATCH_STATE, buf);
  82. }
  83. static ssize_t activity_read(struct file *filp, struct kobject *kobj,
  84. struct bin_attribute *bin_attr, char *buf,
  85. loff_t off, size_t count)
  86. {
  87. dev_dbg(&kobj_to_w1_slave(kobj)->dev,
  88. "Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p",
  89. bin_attr->attr.name, kobj, (unsigned int)off, count, buf);
  90. if (count != 1 || off != 0)
  91. return -EFAULT;
  92. return _read_reg(kobj_to_w1_slave(kobj),
  93. W1_F29_REG_ACTIVITY_LATCH_STATE, buf);
  94. }
  95. static ssize_t cond_search_mask_read(struct file *filp, struct kobject *kobj,
  96. struct bin_attribute *bin_attr, char *buf,
  97. loff_t off, size_t count)
  98. {
  99. dev_dbg(&kobj_to_w1_slave(kobj)->dev,
  100. "Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p",
  101. bin_attr->attr.name, kobj, (unsigned int)off, count, buf);
  102. if (count != 1 || off != 0)
  103. return -EFAULT;
  104. return _read_reg(kobj_to_w1_slave(kobj),
  105. W1_F29_REG_COND_SEARCH_SELECT_MASK, buf);
  106. }
  107. static ssize_t cond_search_polarity_read(struct file *filp,
  108. struct kobject *kobj,
  109. struct bin_attribute *bin_attr,
  110. char *buf, loff_t off, size_t count)
  111. {
  112. if (count != 1 || off != 0)
  113. return -EFAULT;
  114. return _read_reg(kobj_to_w1_slave(kobj),
  115. W1_F29_REG_COND_SEARCH_POL_SELECT, buf);
  116. }
  117. static ssize_t status_control_read(struct file *filp, struct kobject *kobj,
  118. struct bin_attribute *bin_attr, char *buf,
  119. loff_t off, size_t count)
  120. {
  121. if (count != 1 || off != 0)
  122. return -EFAULT;
  123. return _read_reg(kobj_to_w1_slave(kobj),
  124. W1_F29_REG_CONTROL_AND_STATUS, buf);
  125. }
  126. static ssize_t output_write(struct file *filp, struct kobject *kobj,
  127. struct bin_attribute *bin_attr, char *buf,
  128. loff_t off, size_t count)
  129. {
  130. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  131. u8 w1_buf[3];
  132. u8 readBack;
  133. unsigned int retries = W1_F29_RETRIES;
  134. if (count != 1 || off != 0)
  135. return -EFAULT;
  136. dev_dbg(&sl->dev, "locking mutex for write_output");
  137. mutex_lock(&sl->master->bus_mutex);
  138. dev_dbg(&sl->dev, "mutex locked");
  139. if (w1_reset_select_slave(sl))
  140. goto error;
  141. while (retries--) {
  142. w1_buf[0] = W1_F29_FUNC_CHANN_ACCESS_WRITE;
  143. w1_buf[1] = *buf;
  144. w1_buf[2] = ~(*buf);
  145. w1_write_block(sl->master, w1_buf, 3);
  146. readBack = w1_read_8(sl->master);
  147. if (readBack != W1_F29_SUCCESS_CONFIRM_BYTE) {
  148. if (w1_reset_resume_command(sl->master))
  149. goto error;
  150. /* try again, the slave is ready for a command */
  151. continue;
  152. }
  153. #ifdef CONFIG_W1_SLAVE_DS2408_READBACK
  154. /* here the master could read another byte which
  155. would be the PIO reg (the actual pin logic state)
  156. since in this driver we don't know which pins are
  157. in and outs, there's no value to read the state and
  158. compare. with (*buf) so end this command abruptly: */
  159. if (w1_reset_resume_command(sl->master))
  160. goto error;
  161. /* go read back the output latches */
  162. /* (the direct effect of the write above) */
  163. w1_buf[0] = W1_F29_FUNC_READ_PIO_REGS;
  164. w1_buf[1] = W1_F29_REG_OUTPUT_LATCH_STATE;
  165. w1_buf[2] = 0;
  166. w1_write_block(sl->master, w1_buf, 3);
  167. /* read the result of the READ_PIO_REGS command */
  168. if (w1_read_8(sl->master) == *buf)
  169. #endif
  170. {
  171. /* success! */
  172. mutex_unlock(&sl->master->bus_mutex);
  173. dev_dbg(&sl->dev,
  174. "mutex unlocked, retries:%d", retries);
  175. return 1;
  176. }
  177. }
  178. error:
  179. mutex_unlock(&sl->master->bus_mutex);
  180. dev_dbg(&sl->dev, "mutex unlocked in error, retries:%d", retries);
  181. return -EIO;
  182. }
  183. /**
  184. * Writing to the activity file resets the activity latches.
  185. */
  186. static ssize_t activity_write(struct file *filp, struct kobject *kobj,
  187. struct bin_attribute *bin_attr, char *buf,
  188. loff_t off, size_t count)
  189. {
  190. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  191. unsigned int retries = W1_F29_RETRIES;
  192. if (count != 1 || off != 0)
  193. return -EFAULT;
  194. mutex_lock(&sl->master->bus_mutex);
  195. if (w1_reset_select_slave(sl))
  196. goto error;
  197. while (retries--) {
  198. w1_write_8(sl->master, W1_F29_FUNC_RESET_ACTIVITY_LATCHES);
  199. if (w1_read_8(sl->master) == W1_F29_SUCCESS_CONFIRM_BYTE) {
  200. mutex_unlock(&sl->master->bus_mutex);
  201. return 1;
  202. }
  203. if (w1_reset_resume_command(sl->master))
  204. goto error;
  205. }
  206. error:
  207. mutex_unlock(&sl->master->bus_mutex);
  208. return -EIO;
  209. }
  210. static ssize_t status_control_write(struct file *filp, struct kobject *kobj,
  211. struct bin_attribute *bin_attr, char *buf,
  212. loff_t off, size_t count)
  213. {
  214. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  215. u8 w1_buf[4];
  216. unsigned int retries = W1_F29_RETRIES;
  217. if (count != 1 || off != 0)
  218. return -EFAULT;
  219. mutex_lock(&sl->master->bus_mutex);
  220. if (w1_reset_select_slave(sl))
  221. goto error;
  222. while (retries--) {
  223. w1_buf[0] = W1_F29_FUNC_WRITE_COND_SEARCH_REG;
  224. w1_buf[1] = W1_F29_REG_CONTROL_AND_STATUS;
  225. w1_buf[2] = 0;
  226. w1_buf[3] = *buf;
  227. w1_write_block(sl->master, w1_buf, 4);
  228. if (w1_reset_resume_command(sl->master))
  229. goto error;
  230. w1_buf[0] = W1_F29_FUNC_READ_PIO_REGS;
  231. w1_buf[1] = W1_F29_REG_CONTROL_AND_STATUS;
  232. w1_buf[2] = 0;
  233. w1_write_block(sl->master, w1_buf, 3);
  234. if (w1_read_8(sl->master) == *buf) {
  235. /* success! */
  236. mutex_unlock(&sl->master->bus_mutex);
  237. return 1;
  238. }
  239. }
  240. error:
  241. mutex_unlock(&sl->master->bus_mutex);
  242. return -EIO;
  243. }
  244. /*
  245. * This is a special sequence we must do to ensure the P0 output is not stuck
  246. * in test mode. This is described in rev 2 of the ds2408's datasheet
  247. * (http://datasheets.maximintegrated.com/en/ds/DS2408.pdf) under
  248. * "APPLICATION INFORMATION/Power-up timing".
  249. */
  250. static int w1_f29_disable_test_mode(struct w1_slave *sl)
  251. {
  252. int res;
  253. u8 magic[10] = {0x96, };
  254. u64 rn = le64_to_cpu(*((u64*)&sl->reg_num));
  255. memcpy(&magic[1], &rn, 8);
  256. magic[9] = 0x3C;
  257. mutex_lock(&sl->master->bus_mutex);
  258. res = w1_reset_bus(sl->master);
  259. if (res)
  260. goto out;
  261. w1_write_block(sl->master, magic, ARRAY_SIZE(magic));
  262. res = w1_reset_bus(sl->master);
  263. out:
  264. mutex_unlock(&sl->master->bus_mutex);
  265. return res;
  266. }
  267. static BIN_ATTR_RO(state, 1);
  268. static BIN_ATTR_RW(output, 1);
  269. static BIN_ATTR_RW(activity, 1);
  270. static BIN_ATTR_RO(cond_search_mask, 1);
  271. static BIN_ATTR_RO(cond_search_polarity, 1);
  272. static BIN_ATTR_RW(status_control, 1);
  273. static struct bin_attribute *w1_f29_bin_attrs[] = {
  274. &bin_attr_state,
  275. &bin_attr_output,
  276. &bin_attr_activity,
  277. &bin_attr_cond_search_mask,
  278. &bin_attr_cond_search_polarity,
  279. &bin_attr_status_control,
  280. NULL,
  281. };
  282. static const struct attribute_group w1_f29_group = {
  283. .bin_attrs = w1_f29_bin_attrs,
  284. };
  285. static const struct attribute_group *w1_f29_groups[] = {
  286. &w1_f29_group,
  287. NULL,
  288. };
  289. static struct w1_family_ops w1_f29_fops = {
  290. .add_slave = w1_f29_disable_test_mode,
  291. .groups = w1_f29_groups,
  292. };
  293. static struct w1_family w1_family_29 = {
  294. .fid = W1_FAMILY_DS2408,
  295. .fops = &w1_f29_fops,
  296. };
  297. static int __init w1_f29_init(void)
  298. {
  299. return w1_register_family(&w1_family_29);
  300. }
  301. static void __exit w1_f29_exit(void)
  302. {
  303. w1_unregister_family(&w1_family_29);
  304. }
  305. module_init(w1_f29_init);
  306. module_exit(w1_f29_exit);