w1_ds2438.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * 1-Wire implementation for the ds2438 chip
  3. *
  4. * Copyright (c) 2017 Mariusz Bialonczyk <manio@skyboo.net>
  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/device.h>
  12. #include <linux/types.h>
  13. #include <linux/delay.h>
  14. #include <linux/w1.h>
  15. #define W1_FAMILY_DS2438 0x26
  16. #define W1_DS2438_RETRIES 3
  17. /* Memory commands */
  18. #define W1_DS2438_READ_SCRATCH 0xBE
  19. #define W1_DS2438_WRITE_SCRATCH 0x4E
  20. #define W1_DS2438_COPY_SCRATCH 0x48
  21. #define W1_DS2438_RECALL_MEMORY 0xB8
  22. /* Register commands */
  23. #define W1_DS2438_CONVERT_TEMP 0x44
  24. #define W1_DS2438_CONVERT_VOLTAGE 0xB4
  25. #define DS2438_PAGE_SIZE 8
  26. #define DS2438_ADC_INPUT_VAD 0
  27. #define DS2438_ADC_INPUT_VDD 1
  28. #define DS2438_MAX_CONVERSION_TIME 10 /* ms */
  29. /* Page #0 definitions */
  30. #define DS2438_STATUS_REG 0x00 /* Status/Configuration Register */
  31. #define DS2438_STATUS_IAD (1 << 0) /* Current A/D Control Bit */
  32. #define DS2438_STATUS_CA (1 << 1) /* Current Accumulator Configuration */
  33. #define DS2438_STATUS_EE (1 << 2) /* Current Accumulator Shadow Selector bit */
  34. #define DS2438_STATUS_AD (1 << 3) /* Voltage A/D Input Select Bit */
  35. #define DS2438_STATUS_TB (1 << 4) /* Temperature Busy Flag */
  36. #define DS2438_STATUS_NVB (1 << 5) /* Nonvolatile Memory Busy Flag */
  37. #define DS2438_STATUS_ADB (1 << 6) /* A/D Converter Busy Flag */
  38. #define DS2438_TEMP_LSB 0x01
  39. #define DS2438_TEMP_MSB 0x02
  40. #define DS2438_VOLTAGE_LSB 0x03
  41. #define DS2438_VOLTAGE_MSB 0x04
  42. #define DS2438_CURRENT_LSB 0x05
  43. #define DS2438_CURRENT_MSB 0x06
  44. #define DS2438_THRESHOLD 0x07
  45. int w1_ds2438_get_page(struct w1_slave *sl, int pageno, u8 *buf)
  46. {
  47. unsigned int retries = W1_DS2438_RETRIES;
  48. u8 w1_buf[2];
  49. u8 crc;
  50. size_t count;
  51. while (retries--) {
  52. crc = 0;
  53. if (w1_reset_select_slave(sl))
  54. continue;
  55. w1_buf[0] = W1_DS2438_RECALL_MEMORY;
  56. w1_buf[1] = 0x00;
  57. w1_write_block(sl->master, w1_buf, 2);
  58. if (w1_reset_select_slave(sl))
  59. continue;
  60. w1_buf[0] = W1_DS2438_READ_SCRATCH;
  61. w1_buf[1] = 0x00;
  62. w1_write_block(sl->master, w1_buf, 2);
  63. count = w1_read_block(sl->master, buf, DS2438_PAGE_SIZE + 1);
  64. if (count == DS2438_PAGE_SIZE + 1) {
  65. crc = w1_calc_crc8(buf, DS2438_PAGE_SIZE);
  66. /* check for correct CRC */
  67. if ((u8)buf[DS2438_PAGE_SIZE] == crc)
  68. return 0;
  69. }
  70. }
  71. return -1;
  72. }
  73. int w1_ds2438_get_temperature(struct w1_slave *sl, int16_t *temperature)
  74. {
  75. unsigned int retries = W1_DS2438_RETRIES;
  76. u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
  77. unsigned int tm = DS2438_MAX_CONVERSION_TIME;
  78. unsigned long sleep_rem;
  79. int ret;
  80. mutex_lock(&sl->master->bus_mutex);
  81. while (retries--) {
  82. if (w1_reset_select_slave(sl))
  83. continue;
  84. w1_write_8(sl->master, W1_DS2438_CONVERT_TEMP);
  85. mutex_unlock(&sl->master->bus_mutex);
  86. sleep_rem = msleep_interruptible(tm);
  87. if (sleep_rem != 0) {
  88. ret = -1;
  89. goto post_unlock;
  90. }
  91. if (mutex_lock_interruptible(&sl->master->bus_mutex) != 0) {
  92. ret = -1;
  93. goto post_unlock;
  94. }
  95. break;
  96. }
  97. if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
  98. *temperature = (((int16_t) w1_buf[DS2438_TEMP_MSB]) << 8) | ((uint16_t) w1_buf[DS2438_TEMP_LSB]);
  99. ret = 0;
  100. } else
  101. ret = -1;
  102. mutex_unlock(&sl->master->bus_mutex);
  103. post_unlock:
  104. return ret;
  105. }
  106. int w1_ds2438_change_config_bit(struct w1_slave *sl, u8 mask, u8 value)
  107. {
  108. unsigned int retries = W1_DS2438_RETRIES;
  109. u8 w1_buf[3];
  110. u8 status;
  111. int perform_write = 0;
  112. while (retries--) {
  113. if (w1_reset_select_slave(sl))
  114. continue;
  115. w1_buf[0] = W1_DS2438_RECALL_MEMORY;
  116. w1_buf[1] = 0x00;
  117. w1_write_block(sl->master, w1_buf, 2);
  118. if (w1_reset_select_slave(sl))
  119. continue;
  120. w1_buf[0] = W1_DS2438_READ_SCRATCH;
  121. w1_buf[1] = 0x00;
  122. w1_write_block(sl->master, w1_buf, 2);
  123. /* reading one byte of result */
  124. status = w1_read_8(sl->master);
  125. /* if bit0=1, set a value to a mask for easy compare */
  126. if (value)
  127. value = mask;
  128. if ((status & mask) == value)
  129. return 0; /* already set as requested */
  130. else {
  131. /* changing bit */
  132. status ^= mask;
  133. perform_write = 1;
  134. }
  135. break;
  136. }
  137. if (perform_write) {
  138. retries = W1_DS2438_RETRIES;
  139. while (retries--) {
  140. if (w1_reset_select_slave(sl))
  141. continue;
  142. w1_buf[0] = W1_DS2438_WRITE_SCRATCH;
  143. w1_buf[1] = 0x00;
  144. w1_buf[2] = status;
  145. w1_write_block(sl->master, w1_buf, 3);
  146. if (w1_reset_select_slave(sl))
  147. continue;
  148. w1_buf[0] = W1_DS2438_COPY_SCRATCH;
  149. w1_buf[1] = 0x00;
  150. w1_write_block(sl->master, w1_buf, 2);
  151. return 0;
  152. }
  153. }
  154. return -1;
  155. }
  156. uint16_t w1_ds2438_get_voltage(struct w1_slave *sl, int adc_input, uint16_t *voltage)
  157. {
  158. unsigned int retries = W1_DS2438_RETRIES;
  159. u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
  160. unsigned int tm = DS2438_MAX_CONVERSION_TIME;
  161. unsigned long sleep_rem;
  162. int ret;
  163. mutex_lock(&sl->master->bus_mutex);
  164. if (w1_ds2438_change_config_bit(sl, DS2438_STATUS_AD, adc_input)) {
  165. ret = -1;
  166. goto pre_unlock;
  167. }
  168. while (retries--) {
  169. if (w1_reset_select_slave(sl))
  170. continue;
  171. w1_write_8(sl->master, W1_DS2438_CONVERT_VOLTAGE);
  172. mutex_unlock(&sl->master->bus_mutex);
  173. sleep_rem = msleep_interruptible(tm);
  174. if (sleep_rem != 0) {
  175. ret = -1;
  176. goto post_unlock;
  177. }
  178. if (mutex_lock_interruptible(&sl->master->bus_mutex) != 0) {
  179. ret = -1;
  180. goto post_unlock;
  181. }
  182. break;
  183. }
  184. if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
  185. *voltage = (((uint16_t) w1_buf[DS2438_VOLTAGE_MSB]) << 8) | ((uint16_t) w1_buf[DS2438_VOLTAGE_LSB]);
  186. ret = 0;
  187. } else
  188. ret = -1;
  189. pre_unlock:
  190. mutex_unlock(&sl->master->bus_mutex);
  191. post_unlock:
  192. return ret;
  193. }
  194. static ssize_t iad_write(struct file *filp, struct kobject *kobj,
  195. struct bin_attribute *bin_attr, char *buf,
  196. loff_t off, size_t count)
  197. {
  198. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  199. int ret;
  200. if (count != 1 || off != 0)
  201. return -EFAULT;
  202. mutex_lock(&sl->master->bus_mutex);
  203. if (w1_ds2438_change_config_bit(sl, DS2438_STATUS_IAD, *buf & 0x01) == 0)
  204. ret = 1;
  205. else
  206. ret = -EIO;
  207. mutex_unlock(&sl->master->bus_mutex);
  208. return ret;
  209. }
  210. static ssize_t page0_read(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. int ret;
  216. u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
  217. if (off != 0)
  218. return 0;
  219. if (!buf)
  220. return -EINVAL;
  221. mutex_lock(&sl->master->bus_mutex);
  222. if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
  223. memcpy(buf, &w1_buf, DS2438_PAGE_SIZE);
  224. ret = DS2438_PAGE_SIZE;
  225. } else
  226. ret = -EIO;
  227. mutex_unlock(&sl->master->bus_mutex);
  228. return ret;
  229. }
  230. static ssize_t temperature_read(struct file *filp, struct kobject *kobj,
  231. struct bin_attribute *bin_attr, char *buf,
  232. loff_t off, size_t count)
  233. {
  234. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  235. int ret;
  236. ssize_t c = PAGE_SIZE;
  237. int16_t temp;
  238. if (off != 0)
  239. return 0;
  240. if (!buf)
  241. return -EINVAL;
  242. if (w1_ds2438_get_temperature(sl, &temp) == 0) {
  243. c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", temp);
  244. ret = PAGE_SIZE - c;
  245. } else
  246. ret = -EIO;
  247. return ret;
  248. }
  249. static ssize_t vad_read(struct file *filp, struct kobject *kobj,
  250. struct bin_attribute *bin_attr, char *buf,
  251. loff_t off, size_t count)
  252. {
  253. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  254. int ret;
  255. ssize_t c = PAGE_SIZE;
  256. uint16_t voltage;
  257. if (off != 0)
  258. return 0;
  259. if (!buf)
  260. return -EINVAL;
  261. if (w1_ds2438_get_voltage(sl, DS2438_ADC_INPUT_VAD, &voltage) == 0) {
  262. c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", voltage);
  263. ret = PAGE_SIZE - c;
  264. } else
  265. ret = -EIO;
  266. return ret;
  267. }
  268. static ssize_t vdd_read(struct file *filp, struct kobject *kobj,
  269. struct bin_attribute *bin_attr, char *buf,
  270. loff_t off, size_t count)
  271. {
  272. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  273. int ret;
  274. ssize_t c = PAGE_SIZE;
  275. uint16_t voltage;
  276. if (off != 0)
  277. return 0;
  278. if (!buf)
  279. return -EINVAL;
  280. if (w1_ds2438_get_voltage(sl, DS2438_ADC_INPUT_VDD, &voltage) == 0) {
  281. c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", voltage);
  282. ret = PAGE_SIZE - c;
  283. } else
  284. ret = -EIO;
  285. return ret;
  286. }
  287. static BIN_ATTR(iad, S_IRUGO | S_IWUSR | S_IWGRP, NULL, iad_write, 1);
  288. static BIN_ATTR_RO(page0, DS2438_PAGE_SIZE);
  289. static BIN_ATTR_RO(temperature, 0/* real length varies */);
  290. static BIN_ATTR_RO(vad, 0/* real length varies */);
  291. static BIN_ATTR_RO(vdd, 0/* real length varies */);
  292. static struct bin_attribute *w1_ds2438_bin_attrs[] = {
  293. &bin_attr_iad,
  294. &bin_attr_page0,
  295. &bin_attr_temperature,
  296. &bin_attr_vad,
  297. &bin_attr_vdd,
  298. NULL,
  299. };
  300. static const struct attribute_group w1_ds2438_group = {
  301. .bin_attrs = w1_ds2438_bin_attrs,
  302. };
  303. static const struct attribute_group *w1_ds2438_groups[] = {
  304. &w1_ds2438_group,
  305. NULL,
  306. };
  307. static struct w1_family_ops w1_ds2438_fops = {
  308. .groups = w1_ds2438_groups,
  309. };
  310. static struct w1_family w1_ds2438_family = {
  311. .fid = W1_FAMILY_DS2438,
  312. .fops = &w1_ds2438_fops,
  313. };
  314. module_w1_family(w1_ds2438_family);
  315. MODULE_LICENSE("GPL");
  316. MODULE_AUTHOR("Mariusz Bialonczyk <manio@skyboo.net>");
  317. MODULE_DESCRIPTION("1-wire driver for Maxim/Dallas DS2438 Smart Battery Monitor");
  318. MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2438));