w1_therm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 ssize_t w1_seq_show(struct device *device,
  85. struct device_attribute *attr, char *buf);
  86. static DEVICE_ATTR_RO(w1_slave);
  87. static DEVICE_ATTR_RO(w1_seq);
  88. static struct attribute *w1_therm_attrs[] = {
  89. &dev_attr_w1_slave.attr,
  90. NULL,
  91. };
  92. static struct attribute *w1_ds28ea00_attrs[] = {
  93. &dev_attr_w1_slave.attr,
  94. &dev_attr_w1_seq.attr,
  95. NULL,
  96. };
  97. ATTRIBUTE_GROUPS(w1_therm);
  98. ATTRIBUTE_GROUPS(w1_ds28ea00);
  99. static struct w1_family_ops w1_therm_fops = {
  100. .add_slave = w1_therm_add_slave,
  101. .remove_slave = w1_therm_remove_slave,
  102. .groups = w1_therm_groups,
  103. };
  104. static struct w1_family_ops w1_ds28ea00_fops = {
  105. .add_slave = w1_therm_add_slave,
  106. .remove_slave = w1_therm_remove_slave,
  107. .groups = w1_ds28ea00_groups,
  108. };
  109. static struct w1_family w1_therm_family_DS18S20 = {
  110. .fid = W1_THERM_DS18S20,
  111. .fops = &w1_therm_fops,
  112. };
  113. static struct w1_family w1_therm_family_DS18B20 = {
  114. .fid = W1_THERM_DS18B20,
  115. .fops = &w1_therm_fops,
  116. };
  117. static struct w1_family w1_therm_family_DS1822 = {
  118. .fid = W1_THERM_DS1822,
  119. .fops = &w1_therm_fops,
  120. };
  121. static struct w1_family w1_therm_family_DS28EA00 = {
  122. .fid = W1_THERM_DS28EA00,
  123. .fops = &w1_ds28ea00_fops,
  124. };
  125. static struct w1_family w1_therm_family_DS1825 = {
  126. .fid = W1_THERM_DS1825,
  127. .fops = &w1_therm_fops,
  128. };
  129. struct w1_therm_family_converter
  130. {
  131. u8 broken;
  132. u16 reserved;
  133. struct w1_family *f;
  134. int (*convert)(u8 rom[9]);
  135. };
  136. /* The return value is millidegrees Centigrade. */
  137. static inline int w1_DS18B20_convert_temp(u8 rom[9]);
  138. static inline int w1_DS18S20_convert_temp(u8 rom[9]);
  139. static struct w1_therm_family_converter w1_therm_families[] = {
  140. {
  141. .f = &w1_therm_family_DS18S20,
  142. .convert = w1_DS18S20_convert_temp
  143. },
  144. {
  145. .f = &w1_therm_family_DS1822,
  146. .convert = w1_DS18B20_convert_temp
  147. },
  148. {
  149. .f = &w1_therm_family_DS18B20,
  150. .convert = w1_DS18B20_convert_temp
  151. },
  152. {
  153. .f = &w1_therm_family_DS28EA00,
  154. .convert = w1_DS18B20_convert_temp
  155. },
  156. {
  157. .f = &w1_therm_family_DS1825,
  158. .convert = w1_DS18B20_convert_temp
  159. }
  160. };
  161. static inline int w1_DS18B20_convert_temp(u8 rom[9])
  162. {
  163. s16 t = le16_to_cpup((__le16 *)rom);
  164. return t*1000/16;
  165. }
  166. static inline int w1_DS18S20_convert_temp(u8 rom[9])
  167. {
  168. int t, h;
  169. if (!rom[7])
  170. return 0;
  171. if (rom[1] == 0)
  172. t = ((s32)rom[0] >> 1)*1000;
  173. else
  174. t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
  175. t -= 250;
  176. h = 1000*((s32)rom[7] - (s32)rom[6]);
  177. h /= (s32)rom[7];
  178. t += h;
  179. return t;
  180. }
  181. static inline int w1_convert_temp(u8 rom[9], u8 fid)
  182. {
  183. int i;
  184. for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
  185. if (w1_therm_families[i].f->fid == fid)
  186. return w1_therm_families[i].convert(rom);
  187. return 0;
  188. }
  189. static ssize_t w1_slave_show(struct device *device,
  190. struct device_attribute *attr, char *buf)
  191. {
  192. struct w1_slave *sl = dev_to_w1_slave(device);
  193. struct w1_master *dev = sl->master;
  194. u8 rom[9], crc, verdict, external_power;
  195. int i, ret, max_trying = 10;
  196. ssize_t c = PAGE_SIZE;
  197. u8 *family_data = sl->family_data;
  198. ret = mutex_lock_interruptible(&dev->bus_mutex);
  199. if (ret != 0)
  200. goto post_unlock;
  201. if(!sl->family_data)
  202. {
  203. ret = -ENODEV;
  204. goto pre_unlock;
  205. }
  206. /* prevent the slave from going away in sleep */
  207. atomic_inc(THERM_REFCNT(family_data));
  208. memset(rom, 0, sizeof(rom));
  209. while (max_trying--) {
  210. verdict = 0;
  211. crc = 0;
  212. if (!w1_reset_select_slave(sl)) {
  213. int count = 0;
  214. unsigned int tm = 750;
  215. unsigned long sleep_rem;
  216. w1_write_8(dev, W1_READ_PSUPPLY);
  217. external_power = w1_read_8(dev);
  218. if (w1_reset_select_slave(sl))
  219. continue;
  220. /* 750ms strong pullup (or delay) after the convert */
  221. if (w1_strong_pullup == 2 ||
  222. (!external_power && w1_strong_pullup))
  223. w1_next_pullup(dev, tm);
  224. w1_write_8(dev, W1_CONVERT_TEMP);
  225. if (external_power) {
  226. mutex_unlock(&dev->bus_mutex);
  227. sleep_rem = msleep_interruptible(tm);
  228. if (sleep_rem != 0) {
  229. ret = -EINTR;
  230. goto post_unlock;
  231. }
  232. ret = mutex_lock_interruptible(&dev->bus_mutex);
  233. if (ret != 0)
  234. goto post_unlock;
  235. } else if (!w1_strong_pullup) {
  236. sleep_rem = msleep_interruptible(tm);
  237. if (sleep_rem != 0) {
  238. ret = -EINTR;
  239. goto pre_unlock;
  240. }
  241. }
  242. if (!w1_reset_select_slave(sl)) {
  243. w1_write_8(dev, W1_READ_SCRATCHPAD);
  244. if ((count = w1_read_block(dev, rom, 9)) != 9) {
  245. dev_warn(device, "w1_read_block() "
  246. "returned %u instead of 9.\n",
  247. count);
  248. }
  249. crc = w1_calc_crc8(rom, 8);
  250. if (rom[8] == crc)
  251. verdict = 1;
  252. }
  253. }
  254. if (verdict)
  255. break;
  256. }
  257. for (i = 0; i < 9; ++i)
  258. c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", rom[i]);
  259. c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
  260. crc, (verdict) ? "YES" : "NO");
  261. if (verdict)
  262. memcpy(family_data, rom, sizeof(rom));
  263. else
  264. dev_warn(device, "Read failed CRC check\n");
  265. for (i = 0; i < 9; ++i)
  266. c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
  267. ((u8 *)family_data)[i]);
  268. c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
  269. w1_convert_temp(rom, sl->family->fid));
  270. ret = PAGE_SIZE - c;
  271. pre_unlock:
  272. mutex_unlock(&dev->bus_mutex);
  273. post_unlock:
  274. atomic_dec(THERM_REFCNT(family_data));
  275. return ret;
  276. }
  277. #define W1_42_CHAIN 0x99
  278. #define W1_42_CHAIN_OFF 0x3C
  279. #define W1_42_CHAIN_OFF_INV 0xC3
  280. #define W1_42_CHAIN_ON 0x5A
  281. #define W1_42_CHAIN_ON_INV 0xA5
  282. #define W1_42_CHAIN_DONE 0x96
  283. #define W1_42_CHAIN_DONE_INV 0x69
  284. #define W1_42_COND_READ 0x0F
  285. #define W1_42_SUCCESS_CONFIRM_BYTE 0xAA
  286. #define W1_42_FINISHED_BYTE 0xFF
  287. static ssize_t w1_seq_show(struct device *device,
  288. struct device_attribute *attr, char *buf)
  289. {
  290. struct w1_slave *sl = dev_to_w1_slave(device);
  291. ssize_t c = PAGE_SIZE;
  292. int rv;
  293. int i;
  294. u8 ack;
  295. u64 rn;
  296. struct w1_reg_num *reg_num;
  297. int seq = 0;
  298. mutex_lock(&sl->master->bus_mutex);
  299. /* Place all devices in CHAIN state */
  300. if (w1_reset_bus(sl->master))
  301. goto error;
  302. w1_write_8(sl->master, W1_SKIP_ROM);
  303. w1_write_8(sl->master, W1_42_CHAIN);
  304. w1_write_8(sl->master, W1_42_CHAIN_ON);
  305. w1_write_8(sl->master, W1_42_CHAIN_ON_INV);
  306. msleep(sl->master->pullup_duration);
  307. /* check for acknowledgment */
  308. ack = w1_read_8(sl->master);
  309. if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
  310. goto error;
  311. /* In case the bus fails to send 0xFF, limit*/
  312. for (i = 0; i <= 64; i++) {
  313. if (w1_reset_bus(sl->master))
  314. goto error;
  315. w1_write_8(sl->master, W1_42_COND_READ);
  316. rv = w1_read_block(sl->master, (u8 *)&rn, 8);
  317. reg_num = (struct w1_reg_num *) &rn;
  318. if (reg_num->family == W1_42_FINISHED_BYTE)
  319. break;
  320. if (sl->reg_num.id == reg_num->id)
  321. seq = i;
  322. w1_write_8(sl->master, W1_42_CHAIN);
  323. w1_write_8(sl->master, W1_42_CHAIN_DONE);
  324. w1_write_8(sl->master, W1_42_CHAIN_DONE_INV);
  325. w1_read_block(sl->master, &ack, sizeof(ack));
  326. /* check for acknowledgment */
  327. ack = w1_read_8(sl->master);
  328. if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
  329. goto error;
  330. }
  331. /* Exit from CHAIN state */
  332. if (w1_reset_bus(sl->master))
  333. goto error;
  334. w1_write_8(sl->master, W1_SKIP_ROM);
  335. w1_write_8(sl->master, W1_42_CHAIN);
  336. w1_write_8(sl->master, W1_42_CHAIN_OFF);
  337. w1_write_8(sl->master, W1_42_CHAIN_OFF_INV);
  338. /* check for acknowledgment */
  339. ack = w1_read_8(sl->master);
  340. if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
  341. goto error;
  342. mutex_unlock(&sl->master->bus_mutex);
  343. c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq);
  344. return PAGE_SIZE - c;
  345. error:
  346. mutex_unlock(&sl->master->bus_mutex);
  347. return -EIO;
  348. }
  349. static int __init w1_therm_init(void)
  350. {
  351. int err, i;
  352. for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
  353. err = w1_register_family(w1_therm_families[i].f);
  354. if (err)
  355. w1_therm_families[i].broken = 1;
  356. }
  357. return 0;
  358. }
  359. static void __exit w1_therm_fini(void)
  360. {
  361. int i;
  362. for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
  363. if (!w1_therm_families[i].broken)
  364. w1_unregister_family(w1_therm_families[i].f);
  365. }
  366. module_init(w1_therm_init);
  367. module_exit(w1_therm_fini);