w1_ds2438.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. static 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. static 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. static 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. static int w1_ds2438_get_voltage(struct w1_slave *sl,
  157. int adc_input, uint16_t *voltage)
  158. {
  159. unsigned int retries = W1_DS2438_RETRIES;
  160. u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
  161. unsigned int tm = DS2438_MAX_CONVERSION_TIME;
  162. unsigned long sleep_rem;
  163. int ret;
  164. mutex_lock(&sl->master->bus_mutex);
  165. if (w1_ds2438_change_config_bit(sl, DS2438_STATUS_AD, adc_input)) {
  166. ret = -1;
  167. goto pre_unlock;
  168. }
  169. while (retries--) {
  170. if (w1_reset_select_slave(sl))
  171. continue;
  172. w1_write_8(sl->master, W1_DS2438_CONVERT_VOLTAGE);
  173. mutex_unlock(&sl->master->bus_mutex);
  174. sleep_rem = msleep_interruptible(tm);
  175. if (sleep_rem != 0) {
  176. ret = -1;
  177. goto post_unlock;
  178. }
  179. if (mutex_lock_interruptible(&sl->master->bus_mutex) != 0) {
  180. ret = -1;
  181. goto post_unlock;
  182. }
  183. break;
  184. }
  185. if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
  186. *voltage = (((uint16_t) w1_buf[DS2438_VOLTAGE_MSB]) << 8) | ((uint16_t) w1_buf[DS2438_VOLTAGE_LSB]);
  187. ret = 0;
  188. } else
  189. ret = -1;
  190. pre_unlock:
  191. mutex_unlock(&sl->master->bus_mutex);
  192. post_unlock:
  193. return ret;
  194. }
  195. static int w1_ds2438_get_current(struct w1_slave *sl, int16_t *voltage)
  196. {
  197. u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
  198. int ret;
  199. mutex_lock(&sl->master->bus_mutex);
  200. if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
  201. /* The voltage measured across current sense resistor RSENS. */
  202. *voltage = (((int16_t) w1_buf[DS2438_CURRENT_MSB]) << 8) | ((int16_t) w1_buf[DS2438_CURRENT_LSB]);
  203. ret = 0;
  204. } else
  205. ret = -1;
  206. mutex_unlock(&sl->master->bus_mutex);
  207. return ret;
  208. }
  209. static ssize_t iad_write(struct file *filp, struct kobject *kobj,
  210. struct bin_attribute *bin_attr, char *buf,
  211. loff_t off, size_t count)
  212. {
  213. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  214. int ret;
  215. if (count != 1 || off != 0)
  216. return -EFAULT;
  217. mutex_lock(&sl->master->bus_mutex);
  218. if (w1_ds2438_change_config_bit(sl, DS2438_STATUS_IAD, *buf & 0x01) == 0)
  219. ret = 1;
  220. else
  221. ret = -EIO;
  222. mutex_unlock(&sl->master->bus_mutex);
  223. return ret;
  224. }
  225. static ssize_t iad_read(struct file *filp, struct kobject *kobj,
  226. struct bin_attribute *bin_attr, char *buf,
  227. loff_t off, size_t count)
  228. {
  229. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  230. int ret;
  231. int16_t voltage;
  232. if (off != 0)
  233. return 0;
  234. if (!buf)
  235. return -EINVAL;
  236. if (w1_ds2438_get_current(sl, &voltage) == 0) {
  237. ret = snprintf(buf, count, "%i\n", voltage);
  238. } else
  239. ret = -EIO;
  240. return ret;
  241. }
  242. static ssize_t page0_read(struct file *filp, struct kobject *kobj,
  243. struct bin_attribute *bin_attr, char *buf,
  244. loff_t off, size_t count)
  245. {
  246. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  247. int ret;
  248. u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
  249. if (off != 0)
  250. return 0;
  251. if (!buf)
  252. return -EINVAL;
  253. mutex_lock(&sl->master->bus_mutex);
  254. /* Read no more than page0 size */
  255. if (count > DS2438_PAGE_SIZE)
  256. count = DS2438_PAGE_SIZE;
  257. if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
  258. memcpy(buf, &w1_buf, count);
  259. ret = count;
  260. } else
  261. ret = -EIO;
  262. mutex_unlock(&sl->master->bus_mutex);
  263. return ret;
  264. }
  265. static ssize_t temperature_read(struct file *filp, struct kobject *kobj,
  266. struct bin_attribute *bin_attr, char *buf,
  267. loff_t off, size_t count)
  268. {
  269. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  270. int ret;
  271. int16_t temp;
  272. if (off != 0)
  273. return 0;
  274. if (!buf)
  275. return -EINVAL;
  276. if (w1_ds2438_get_temperature(sl, &temp) == 0) {
  277. ret = snprintf(buf, count, "%i\n", temp);
  278. } else
  279. ret = -EIO;
  280. return ret;
  281. }
  282. static ssize_t vad_read(struct file *filp, struct kobject *kobj,
  283. struct bin_attribute *bin_attr, char *buf,
  284. loff_t off, size_t count)
  285. {
  286. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  287. int ret;
  288. uint16_t voltage;
  289. if (off != 0)
  290. return 0;
  291. if (!buf)
  292. return -EINVAL;
  293. if (w1_ds2438_get_voltage(sl, DS2438_ADC_INPUT_VAD, &voltage) == 0) {
  294. ret = snprintf(buf, count, "%u\n", voltage);
  295. } else
  296. ret = -EIO;
  297. return ret;
  298. }
  299. static ssize_t vdd_read(struct file *filp, struct kobject *kobj,
  300. struct bin_attribute *bin_attr, char *buf,
  301. loff_t off, size_t count)
  302. {
  303. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  304. int ret;
  305. uint16_t voltage;
  306. if (off != 0)
  307. return 0;
  308. if (!buf)
  309. return -EINVAL;
  310. if (w1_ds2438_get_voltage(sl, DS2438_ADC_INPUT_VDD, &voltage) == 0) {
  311. ret = snprintf(buf, count, "%u\n", voltage);
  312. } else
  313. ret = -EIO;
  314. return ret;
  315. }
  316. static BIN_ATTR(iad, S_IRUGO | S_IWUSR | S_IWGRP, iad_read, iad_write, 0);
  317. static BIN_ATTR_RO(page0, DS2438_PAGE_SIZE);
  318. static BIN_ATTR_RO(temperature, 0/* real length varies */);
  319. static BIN_ATTR_RO(vad, 0/* real length varies */);
  320. static BIN_ATTR_RO(vdd, 0/* real length varies */);
  321. static struct bin_attribute *w1_ds2438_bin_attrs[] = {
  322. &bin_attr_iad,
  323. &bin_attr_page0,
  324. &bin_attr_temperature,
  325. &bin_attr_vad,
  326. &bin_attr_vdd,
  327. NULL,
  328. };
  329. static const struct attribute_group w1_ds2438_group = {
  330. .bin_attrs = w1_ds2438_bin_attrs,
  331. };
  332. static const struct attribute_group *w1_ds2438_groups[] = {
  333. &w1_ds2438_group,
  334. NULL,
  335. };
  336. static struct w1_family_ops w1_ds2438_fops = {
  337. .groups = w1_ds2438_groups,
  338. };
  339. static struct w1_family w1_ds2438_family = {
  340. .fid = W1_FAMILY_DS2438,
  341. .fops = &w1_ds2438_fops,
  342. };
  343. module_w1_family(w1_ds2438_family);
  344. MODULE_LICENSE("GPL");
  345. MODULE_AUTHOR("Mariusz Bialonczyk <manio@skyboo.net>");
  346. MODULE_DESCRIPTION("1-wire driver for Maxim/Dallas DS2438 Smart Battery Monitor");
  347. MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2438));