w1_therm.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * w1_therm.c
  3. *
  4. * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the therms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <asm/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/sched.h>
  26. #include <linux/device.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/delay.h>
  30. #include "../w1.h"
  31. #include "../w1_int.h"
  32. #include "../w1_family.h"
  33. MODULE_LICENSE("GPL");
  34. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  35. MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
  36. MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20));
  37. MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822));
  38. MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20));
  39. MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825));
  40. MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00));
  41. /* Allow the strong pullup to be disabled, but default to enabled.
  42. * If it was disabled a parasite powered device might not get the require
  43. * current to do a temperature conversion. If it is enabled parasite powered
  44. * devices have a better chance of getting the current required.
  45. * In case the parasite power-detection is not working (seems to be the case
  46. * for some DS18S20) the strong pullup can also be forced, regardless of the
  47. * power state of the devices.
  48. *
  49. * Summary of options:
  50. * - strong_pullup = 0 Disable strong pullup completely
  51. * - strong_pullup = 1 Enable automatic strong pullup detection
  52. * - strong_pullup = 2 Force strong pullup
  53. */
  54. static int w1_strong_pullup = 1;
  55. module_param_named(strong_pullup, w1_strong_pullup, int, 0);
  56. struct w1_therm_family_data {
  57. uint8_t rom[9];
  58. atomic_t refcnt;
  59. };
  60. /* return the address of the refcnt in the family data */
  61. #define THERM_REFCNT(family_data) \
  62. (&((struct w1_therm_family_data*)family_data)->refcnt)
  63. static int w1_therm_add_slave(struct w1_slave *sl)
  64. {
  65. sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
  66. GFP_KERNEL);
  67. if (!sl->family_data)
  68. return -ENOMEM;
  69. atomic_set(THERM_REFCNT(sl->family_data), 1);
  70. return 0;
  71. }
  72. static void w1_therm_remove_slave(struct w1_slave *sl)
  73. {
  74. int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
  75. while(refcnt) {
  76. msleep(1000);
  77. refcnt = atomic_read(THERM_REFCNT(sl->family_data));
  78. }
  79. kfree(sl->family_data);
  80. sl->family_data = NULL;
  81. }
  82. static ssize_t w1_slave_show(struct device *device,
  83. struct device_attribute *attr, char *buf);
  84. static DEVICE_ATTR_RO(w1_slave);
  85. static struct attribute *w1_therm_attrs[] = {
  86. &dev_attr_w1_slave.attr,
  87. NULL,
  88. };
  89. ATTRIBUTE_GROUPS(w1_therm);
  90. static struct w1_family_ops w1_therm_fops = {
  91. .add_slave = w1_therm_add_slave,
  92. .remove_slave = w1_therm_remove_slave,
  93. .groups = w1_therm_groups,
  94. };
  95. static struct w1_family w1_therm_family_DS18S20 = {
  96. .fid = W1_THERM_DS18S20,
  97. .fops = &w1_therm_fops,
  98. };
  99. static struct w1_family w1_therm_family_DS18B20 = {
  100. .fid = W1_THERM_DS18B20,
  101. .fops = &w1_therm_fops,
  102. };
  103. static struct w1_family w1_therm_family_DS1822 = {
  104. .fid = W1_THERM_DS1822,
  105. .fops = &w1_therm_fops,
  106. };
  107. static struct w1_family w1_therm_family_DS28EA00 = {
  108. .fid = W1_THERM_DS28EA00,
  109. .fops = &w1_therm_fops,
  110. };
  111. static struct w1_family w1_therm_family_DS1825 = {
  112. .fid = W1_THERM_DS1825,
  113. .fops = &w1_therm_fops,
  114. };
  115. struct w1_therm_family_converter
  116. {
  117. u8 broken;
  118. u16 reserved;
  119. struct w1_family *f;
  120. int (*convert)(u8 rom[9]);
  121. };
  122. /* The return value is millidegrees Centigrade. */
  123. static inline int w1_DS18B20_convert_temp(u8 rom[9]);
  124. static inline int w1_DS18S20_convert_temp(u8 rom[9]);
  125. static struct w1_therm_family_converter w1_therm_families[] = {
  126. {
  127. .f = &w1_therm_family_DS18S20,
  128. .convert = w1_DS18S20_convert_temp
  129. },
  130. {
  131. .f = &w1_therm_family_DS1822,
  132. .convert = w1_DS18B20_convert_temp
  133. },
  134. {
  135. .f = &w1_therm_family_DS18B20,
  136. .convert = w1_DS18B20_convert_temp
  137. },
  138. {
  139. .f = &w1_therm_family_DS28EA00,
  140. .convert = w1_DS18B20_convert_temp
  141. },
  142. {
  143. .f = &w1_therm_family_DS1825,
  144. .convert = w1_DS18B20_convert_temp
  145. }
  146. };
  147. static inline int w1_DS18B20_convert_temp(u8 rom[9])
  148. {
  149. s16 t = le16_to_cpup((__le16 *)rom);
  150. return t*1000/16;
  151. }
  152. static inline int w1_DS18S20_convert_temp(u8 rom[9])
  153. {
  154. int t, h;
  155. if (!rom[7])
  156. return 0;
  157. if (rom[1] == 0)
  158. t = ((s32)rom[0] >> 1)*1000;
  159. else
  160. t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
  161. t -= 250;
  162. h = 1000*((s32)rom[7] - (s32)rom[6]);
  163. h /= (s32)rom[7];
  164. t += h;
  165. return t;
  166. }
  167. static inline int w1_convert_temp(u8 rom[9], u8 fid)
  168. {
  169. int i;
  170. for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
  171. if (w1_therm_families[i].f->fid == fid)
  172. return w1_therm_families[i].convert(rom);
  173. return 0;
  174. }
  175. static ssize_t w1_slave_show(struct device *device,
  176. struct device_attribute *attr, char *buf)
  177. {
  178. struct w1_slave *sl = dev_to_w1_slave(device);
  179. struct w1_master *dev = sl->master;
  180. u8 rom[9], crc, verdict, external_power;
  181. int i, ret, max_trying = 10;
  182. ssize_t c = PAGE_SIZE;
  183. u8 *family_data = sl->family_data;
  184. ret = mutex_lock_interruptible(&dev->bus_mutex);
  185. if (ret != 0)
  186. goto post_unlock;
  187. if(!sl->family_data)
  188. {
  189. ret = -ENODEV;
  190. goto pre_unlock;
  191. }
  192. /* prevent the slave from going away in sleep */
  193. atomic_inc(THERM_REFCNT(family_data));
  194. memset(rom, 0, sizeof(rom));
  195. while (max_trying--) {
  196. verdict = 0;
  197. crc = 0;
  198. if (!w1_reset_select_slave(sl)) {
  199. int count = 0;
  200. unsigned int tm = 750;
  201. unsigned long sleep_rem;
  202. w1_write_8(dev, W1_READ_PSUPPLY);
  203. external_power = w1_read_8(dev);
  204. if (w1_reset_select_slave(sl))
  205. continue;
  206. /* 750ms strong pullup (or delay) after the convert */
  207. if (w1_strong_pullup == 2 ||
  208. (!external_power && w1_strong_pullup))
  209. w1_next_pullup(dev, tm);
  210. w1_write_8(dev, W1_CONVERT_TEMP);
  211. if (external_power) {
  212. mutex_unlock(&dev->bus_mutex);
  213. sleep_rem = msleep_interruptible(tm);
  214. if (sleep_rem != 0) {
  215. ret = -EINTR;
  216. goto post_unlock;
  217. }
  218. ret = mutex_lock_interruptible(&dev->bus_mutex);
  219. if (ret != 0)
  220. goto post_unlock;
  221. } else if (!w1_strong_pullup) {
  222. sleep_rem = msleep_interruptible(tm);
  223. if (sleep_rem != 0) {
  224. ret = -EINTR;
  225. goto pre_unlock;
  226. }
  227. }
  228. if (!w1_reset_select_slave(sl)) {
  229. w1_write_8(dev, W1_READ_SCRATCHPAD);
  230. if ((count = w1_read_block(dev, rom, 9)) != 9) {
  231. dev_warn(device, "w1_read_block() "
  232. "returned %u instead of 9.\n",
  233. count);
  234. }
  235. crc = w1_calc_crc8(rom, 8);
  236. if (rom[8] == crc)
  237. verdict = 1;
  238. }
  239. }
  240. if (verdict)
  241. break;
  242. }
  243. for (i = 0; i < 9; ++i)
  244. c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", rom[i]);
  245. c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
  246. crc, (verdict) ? "YES" : "NO");
  247. if (verdict)
  248. memcpy(family_data, rom, sizeof(rom));
  249. else
  250. dev_warn(device, "Read failed CRC check\n");
  251. for (i = 0; i < 9; ++i)
  252. c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
  253. ((u8 *)family_data)[i]);
  254. c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
  255. w1_convert_temp(rom, sl->family->fid));
  256. ret = PAGE_SIZE - c;
  257. pre_unlock:
  258. mutex_unlock(&dev->bus_mutex);
  259. post_unlock:
  260. atomic_dec(THERM_REFCNT(family_data));
  261. return ret;
  262. }
  263. static int __init w1_therm_init(void)
  264. {
  265. int err, i;
  266. for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
  267. err = w1_register_family(w1_therm_families[i].f);
  268. if (err)
  269. w1_therm_families[i].broken = 1;
  270. }
  271. return 0;
  272. }
  273. static void __exit w1_therm_fini(void)
  274. {
  275. int i;
  276. for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
  277. if (!w1_therm_families[i].broken)
  278. w1_unregister_family(w1_therm_families[i].f);
  279. }
  280. module_init(w1_therm_init);
  281. module_exit(w1_therm_fini);