Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 2 | * w1_therm.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Evgeniy Polyakov | a801876 | 2011-08-25 15:59:06 -0700 | [diff] [blame] | 4 | * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net> |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 22 | #include <asm/types.h> |
| 23 | |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/moduleparam.h> |
Al Viro | f6a5703 | 2006-10-18 01:47:25 -0400 | [diff] [blame] | 27 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/device.h> |
| 29 | #include <linux/types.h> |
David Fries | eb2c0da | 2014-01-15 22:29:24 -0600 | [diff] [blame] | 30 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/delay.h> |
| 32 | |
Evgeniy Polyakov | bd529cf | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 33 | #include "../w1.h" |
Evgeniy Polyakov | bd529cf | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 34 | #include "../w1_int.h" |
| 35 | #include "../w1_family.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | MODULE_LICENSE("GPL"); |
Evgeniy Polyakov | a801876 | 2011-08-25 15:59:06 -0700 | [diff] [blame] | 38 | MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family."); |
Alexander Stein | 8d7bda5 | 2013-05-26 20:06:50 +0200 | [diff] [blame] | 40 | MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20)); |
| 41 | MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822)); |
| 42 | MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20)); |
| 43 | MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825)); |
| 44 | MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
David Fries | 6cd1597 | 2008-10-15 22:04:43 -0700 | [diff] [blame] | 46 | /* Allow the strong pullup to be disabled, but default to enabled. |
| 47 | * If it was disabled a parasite powered device might not get the require |
| 48 | * current to do a temperature conversion. If it is enabled parasite powered |
| 49 | * devices have a better chance of getting the current required. |
Michael Arndt | 29e5507 | 2013-02-17 20:51:20 +0100 | [diff] [blame] | 50 | * In case the parasite power-detection is not working (seems to be the case |
| 51 | * for some DS18S20) the strong pullup can also be forced, regardless of the |
| 52 | * power state of the devices. |
| 53 | * |
| 54 | * Summary of options: |
| 55 | * - strong_pullup = 0 Disable strong pullup completely |
| 56 | * - strong_pullup = 1 Enable automatic strong pullup detection |
| 57 | * - strong_pullup = 2 Force strong pullup |
David Fries | 6cd1597 | 2008-10-15 22:04:43 -0700 | [diff] [blame] | 58 | */ |
| 59 | static int w1_strong_pullup = 1; |
| 60 | module_param_named(strong_pullup, w1_strong_pullup, int, 0); |
| 61 | |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 62 | struct w1_therm_family_data { |
| 63 | uint8_t rom[9]; |
| 64 | atomic_t refcnt; |
| 65 | }; |
| 66 | |
| 67 | /* return the address of the refcnt in the family data */ |
| 68 | #define THERM_REFCNT(family_data) \ |
| 69 | (&((struct w1_therm_family_data*)family_data)->refcnt) |
| 70 | |
David Fries | eb2c0da | 2014-01-15 22:29:24 -0600 | [diff] [blame] | 71 | static int w1_therm_add_slave(struct w1_slave *sl) |
| 72 | { |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 73 | sl->family_data = kzalloc(sizeof(struct w1_therm_family_data), |
| 74 | GFP_KERNEL); |
David Fries | eb2c0da | 2014-01-15 22:29:24 -0600 | [diff] [blame] | 75 | if (!sl->family_data) |
| 76 | return -ENOMEM; |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 77 | atomic_set(THERM_REFCNT(sl->family_data), 1); |
David Fries | eb2c0da | 2014-01-15 22:29:24 -0600 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static void w1_therm_remove_slave(struct w1_slave *sl) |
| 82 | { |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 83 | int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data)); |
Ben Werbowyj | d4c3f97 | 2016-07-22 14:33:33 +1000 | [diff] [blame^] | 84 | while (refcnt) { |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 85 | msleep(1000); |
| 86 | refcnt = atomic_read(THERM_REFCNT(sl->family_data)); |
| 87 | } |
David Fries | eb2c0da | 2014-01-15 22:29:24 -0600 | [diff] [blame] | 88 | kfree(sl->family_data); |
| 89 | sl->family_data = NULL; |
| 90 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Greg Kroah-Hartman | b8a9f44 | 2013-08-21 15:44:56 -0700 | [diff] [blame] | 92 | static ssize_t w1_slave_show(struct device *device, |
David Fries | 347ba8a | 2008-10-15 22:04:51 -0700 | [diff] [blame] | 93 | struct device_attribute *attr, char *buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Ben Sen | 0a19f12 | 2016-05-01 23:23:33 +0200 | [diff] [blame] | 95 | static ssize_t w1_slave_store(struct device *device, |
| 96 | struct device_attribute *attr, const char *buf, size_t size); |
| 97 | |
Matt Campbell | d9411e5 | 2015-04-28 07:44:17 -0400 | [diff] [blame] | 98 | static ssize_t w1_seq_show(struct device *device, |
| 99 | struct device_attribute *attr, char *buf); |
| 100 | |
Ben Sen | 0a19f12 | 2016-05-01 23:23:33 +0200 | [diff] [blame] | 101 | static DEVICE_ATTR_RW(w1_slave); |
Matt Campbell | d9411e5 | 2015-04-28 07:44:17 -0400 | [diff] [blame] | 102 | static DEVICE_ATTR_RO(w1_seq); |
Evgeniy Polyakov | d2a4ef6 | 2005-08-11 17:27:50 +0400 | [diff] [blame] | 103 | |
Greg Kroah-Hartman | b8a9f44 | 2013-08-21 15:44:56 -0700 | [diff] [blame] | 104 | static struct attribute *w1_therm_attrs[] = { |
| 105 | &dev_attr_w1_slave.attr, |
| 106 | NULL, |
| 107 | }; |
Matt Campbell | d9411e5 | 2015-04-28 07:44:17 -0400 | [diff] [blame] | 108 | |
| 109 | static struct attribute *w1_ds28ea00_attrs[] = { |
| 110 | &dev_attr_w1_slave.attr, |
| 111 | &dev_attr_w1_seq.attr, |
| 112 | NULL, |
| 113 | }; |
Greg Kroah-Hartman | b8a9f44 | 2013-08-21 15:44:56 -0700 | [diff] [blame] | 114 | ATTRIBUTE_GROUPS(w1_therm); |
Matt Campbell | d9411e5 | 2015-04-28 07:44:17 -0400 | [diff] [blame] | 115 | ATTRIBUTE_GROUPS(w1_ds28ea00); |
Evgeniy Polyakov | d2a4ef6 | 2005-08-11 17:27:50 +0400 | [diff] [blame] | 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | static struct w1_family_ops w1_therm_fops = { |
David Fries | eb2c0da | 2014-01-15 22:29:24 -0600 | [diff] [blame] | 118 | .add_slave = w1_therm_add_slave, |
| 119 | .remove_slave = w1_therm_remove_slave, |
Greg Kroah-Hartman | b8a9f44 | 2013-08-21 15:44:56 -0700 | [diff] [blame] | 120 | .groups = w1_therm_groups, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
Matt Campbell | d9411e5 | 2015-04-28 07:44:17 -0400 | [diff] [blame] | 123 | static struct w1_family_ops w1_ds28ea00_fops = { |
| 124 | .add_slave = w1_therm_add_slave, |
| 125 | .remove_slave = w1_therm_remove_slave, |
| 126 | .groups = w1_ds28ea00_groups, |
| 127 | }; |
| 128 | |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 129 | static struct w1_family w1_therm_family_DS18S20 = { |
| 130 | .fid = W1_THERM_DS18S20, |
| 131 | .fops = &w1_therm_fops, |
| 132 | }; |
| 133 | |
| 134 | static struct w1_family w1_therm_family_DS18B20 = { |
| 135 | .fid = W1_THERM_DS18B20, |
| 136 | .fops = &w1_therm_fops, |
| 137 | }; |
Evgeniy Polyakov | d2a4ef6 | 2005-08-11 17:27:50 +0400 | [diff] [blame] | 138 | |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 139 | static struct w1_family w1_therm_family_DS1822 = { |
| 140 | .fid = W1_THERM_DS1822, |
| 141 | .fops = &w1_therm_fops, |
| 142 | }; |
| 143 | |
Christian Glindkamp | f7b1371 | 2011-07-26 16:08:55 -0700 | [diff] [blame] | 144 | static struct w1_family w1_therm_family_DS28EA00 = { |
| 145 | .fid = W1_THERM_DS28EA00, |
Matt Campbell | d9411e5 | 2015-04-28 07:44:17 -0400 | [diff] [blame] | 146 | .fops = &w1_ds28ea00_fops, |
Christian Glindkamp | f7b1371 | 2011-07-26 16:08:55 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
Raphael Assenat | f3261df | 2012-08-16 12:56:40 -0400 | [diff] [blame] | 149 | static struct w1_family w1_therm_family_DS1825 = { |
| 150 | .fid = W1_THERM_DS1825, |
| 151 | .fops = &w1_therm_fops, |
| 152 | }; |
| 153 | |
Ben Werbowyj | d4c3f97 | 2016-07-22 14:33:33 +1000 | [diff] [blame^] | 154 | struct w1_therm_family_converter { |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 155 | u8 broken; |
| 156 | u16 reserved; |
| 157 | struct w1_family *f; |
| 158 | int (*convert)(u8 rom[9]); |
Ben Sen | 0a19f12 | 2016-05-01 23:23:33 +0200 | [diff] [blame] | 159 | int (*precision)(struct device *device, int val); |
| 160 | int (*eeprom)(struct device *device); |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 161 | }; |
| 162 | |
Ben Sen | 0a19f12 | 2016-05-01 23:23:33 +0200 | [diff] [blame] | 163 | /* write configuration to eeprom */ |
| 164 | static inline int w1_therm_eeprom(struct device *device); |
| 165 | |
| 166 | /* Set precision for conversion */ |
| 167 | static inline int w1_DS18B20_precision(struct device *device, int val); |
| 168 | static inline int w1_DS18S20_precision(struct device *device, int val); |
| 169 | |
David Fries | 7129b12 | 2008-02-06 01:38:09 -0800 | [diff] [blame] | 170 | /* The return value is millidegrees Centigrade. */ |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 171 | static inline int w1_DS18B20_convert_temp(u8 rom[9]); |
| 172 | static inline int w1_DS18S20_convert_temp(u8 rom[9]); |
| 173 | |
| 174 | static struct w1_therm_family_converter w1_therm_families[] = { |
| 175 | { |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 176 | .f = &w1_therm_family_DS18S20, |
Ben Sen | 0a19f12 | 2016-05-01 23:23:33 +0200 | [diff] [blame] | 177 | .convert = w1_DS18S20_convert_temp, |
| 178 | .precision = w1_DS18S20_precision, |
| 179 | .eeprom = w1_therm_eeprom |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 180 | }, |
| 181 | { |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 182 | .f = &w1_therm_family_DS1822, |
Ben Sen | 0a19f12 | 2016-05-01 23:23:33 +0200 | [diff] [blame] | 183 | .convert = w1_DS18B20_convert_temp, |
| 184 | .precision = w1_DS18S20_precision, |
| 185 | .eeprom = w1_therm_eeprom |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 186 | }, |
| 187 | { |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 188 | .f = &w1_therm_family_DS18B20, |
Ben Sen | 0a19f12 | 2016-05-01 23:23:33 +0200 | [diff] [blame] | 189 | .convert = w1_DS18B20_convert_temp, |
| 190 | .precision = w1_DS18B20_precision, |
| 191 | .eeprom = w1_therm_eeprom |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 192 | }, |
Christian Glindkamp | f7b1371 | 2011-07-26 16:08:55 -0700 | [diff] [blame] | 193 | { |
| 194 | .f = &w1_therm_family_DS28EA00, |
Ben Sen | 0a19f12 | 2016-05-01 23:23:33 +0200 | [diff] [blame] | 195 | .convert = w1_DS18B20_convert_temp, |
| 196 | .precision = w1_DS18S20_precision, |
| 197 | .eeprom = w1_therm_eeprom |
Christian Glindkamp | f7b1371 | 2011-07-26 16:08:55 -0700 | [diff] [blame] | 198 | }, |
Raphael Assenat | f3261df | 2012-08-16 12:56:40 -0400 | [diff] [blame] | 199 | { |
| 200 | .f = &w1_therm_family_DS1825, |
Ben Sen | 0a19f12 | 2016-05-01 23:23:33 +0200 | [diff] [blame] | 201 | .convert = w1_DS18B20_convert_temp, |
| 202 | .precision = w1_DS18S20_precision, |
| 203 | .eeprom = w1_therm_eeprom |
Raphael Assenat | f3261df | 2012-08-16 12:56:40 -0400 | [diff] [blame] | 204 | } |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 205 | }; |
| 206 | |
Ben Sen | 0a19f12 | 2016-05-01 23:23:33 +0200 | [diff] [blame] | 207 | static inline int w1_therm_eeprom(struct device *device) |
| 208 | { |
| 209 | struct w1_slave *sl = dev_to_w1_slave(device); |
| 210 | struct w1_master *dev = sl->master; |
| 211 | u8 rom[9], external_power; |
| 212 | int ret, max_trying = 10; |
| 213 | u8 *family_data = sl->family_data; |
| 214 | |
| 215 | ret = mutex_lock_interruptible(&dev->bus_mutex); |
| 216 | if (ret != 0) |
| 217 | goto post_unlock; |
| 218 | |
| 219 | if (!sl->family_data) { |
| 220 | ret = -ENODEV; |
| 221 | goto pre_unlock; |
| 222 | } |
| 223 | |
| 224 | /* prevent the slave from going away in sleep */ |
| 225 | atomic_inc(THERM_REFCNT(family_data)); |
| 226 | memset(rom, 0, sizeof(rom)); |
| 227 | |
| 228 | while (max_trying--) { |
| 229 | if (!w1_reset_select_slave(sl)) { |
| 230 | unsigned int tm = 10; |
| 231 | unsigned long sleep_rem; |
| 232 | |
| 233 | /* check if in parasite mode */ |
| 234 | w1_write_8(dev, W1_READ_PSUPPLY); |
| 235 | external_power = w1_read_8(dev); |
| 236 | |
| 237 | if (w1_reset_select_slave(sl)) |
| 238 | continue; |
| 239 | |
| 240 | /* 10ms strong pullup/delay after the copy command */ |
| 241 | if (w1_strong_pullup == 2 || |
| 242 | (!external_power && w1_strong_pullup)) |
| 243 | w1_next_pullup(dev, tm); |
| 244 | |
| 245 | w1_write_8(dev, W1_COPY_SCRATCHPAD); |
| 246 | |
| 247 | if (external_power) { |
| 248 | mutex_unlock(&dev->bus_mutex); |
| 249 | |
| 250 | sleep_rem = msleep_interruptible(tm); |
| 251 | if (sleep_rem != 0) { |
| 252 | ret = -EINTR; |
| 253 | goto post_unlock; |
| 254 | } |
| 255 | |
| 256 | ret = mutex_lock_interruptible(&dev->bus_mutex); |
| 257 | if (ret != 0) |
| 258 | goto post_unlock; |
| 259 | } else if (!w1_strong_pullup) { |
| 260 | sleep_rem = msleep_interruptible(tm); |
| 261 | if (sleep_rem != 0) { |
| 262 | ret = -EINTR; |
| 263 | goto pre_unlock; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | pre_unlock: |
| 272 | mutex_unlock(&dev->bus_mutex); |
| 273 | |
| 274 | post_unlock: |
| 275 | atomic_dec(THERM_REFCNT(family_data)); |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | /* DS18S20 does not feature configuration register */ |
| 280 | static inline int w1_DS18S20_precision(struct device *device, int val) |
| 281 | { |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static inline int w1_DS18B20_precision(struct device *device, int val) |
| 286 | { |
| 287 | struct w1_slave *sl = dev_to_w1_slave(device); |
| 288 | struct w1_master *dev = sl->master; |
| 289 | u8 rom[9], crc; |
| 290 | int ret, max_trying = 10; |
| 291 | u8 *family_data = sl->family_data; |
| 292 | uint8_t precision_bits; |
| 293 | uint8_t mask = 0x60; |
| 294 | |
Ben Werbowyj | d4c3f97 | 2016-07-22 14:33:33 +1000 | [diff] [blame^] | 295 | if (val > 12 || val < 9) { |
Ben Sen | 0a19f12 | 2016-05-01 23:23:33 +0200 | [diff] [blame] | 296 | pr_warn("Unsupported precision\n"); |
| 297 | return -1; |
| 298 | } |
| 299 | |
| 300 | ret = mutex_lock_interruptible(&dev->bus_mutex); |
| 301 | if (ret != 0) |
| 302 | goto post_unlock; |
| 303 | |
| 304 | if (!sl->family_data) { |
| 305 | ret = -ENODEV; |
| 306 | goto pre_unlock; |
| 307 | } |
| 308 | |
| 309 | /* prevent the slave from going away in sleep */ |
| 310 | atomic_inc(THERM_REFCNT(family_data)); |
| 311 | memset(rom, 0, sizeof(rom)); |
| 312 | |
| 313 | /* translate precision to bitmask (see datasheet page 9) */ |
| 314 | switch (val) { |
| 315 | case 9: |
| 316 | precision_bits = 0x00; |
| 317 | break; |
| 318 | case 10: |
| 319 | precision_bits = 0x20; |
| 320 | break; |
| 321 | case 11: |
| 322 | precision_bits = 0x40; |
| 323 | break; |
| 324 | case 12: |
| 325 | default: |
| 326 | precision_bits = 0x60; |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | while (max_trying--) { |
| 331 | crc = 0; |
| 332 | |
| 333 | if (!w1_reset_select_slave(sl)) { |
| 334 | int count = 0; |
| 335 | |
| 336 | /* read values to only alter precision bits */ |
| 337 | w1_write_8(dev, W1_READ_SCRATCHPAD); |
| 338 | if ((count = w1_read_block(dev, rom, 9)) != 9) |
| 339 | dev_warn(device, "w1_read_block() returned %u instead of 9.\n", count); |
| 340 | |
| 341 | crc = w1_calc_crc8(rom, 8); |
| 342 | if (rom[8] == crc) { |
| 343 | rom[4] = (rom[4] & ~mask) | (precision_bits & mask); |
| 344 | |
| 345 | if (!w1_reset_select_slave(sl)) { |
| 346 | w1_write_8(dev, W1_WRITE_SCRATCHPAD); |
| 347 | w1_write_8(dev, rom[2]); |
| 348 | w1_write_8(dev, rom[3]); |
| 349 | w1_write_8(dev, rom[4]); |
| 350 | |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | pre_unlock: |
| 358 | mutex_unlock(&dev->bus_mutex); |
| 359 | |
| 360 | post_unlock: |
| 361 | atomic_dec(THERM_REFCNT(family_data)); |
| 362 | return ret; |
| 363 | } |
| 364 | |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 365 | static inline int w1_DS18B20_convert_temp(u8 rom[9]) |
| 366 | { |
Ian Dall | 9a6a1ec | 2010-04-23 13:17:53 -0400 | [diff] [blame] | 367 | s16 t = le16_to_cpup((__le16 *)rom); |
| 368 | return t*1000/16; |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | static inline int w1_DS18S20_convert_temp(u8 rom[9]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
| 373 | int t, h; |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 374 | |
| 375 | if (!rom[7]) |
| 376 | return 0; |
Evgeniy Polyakov | bd529cf | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | if (rom[1] == 0) |
| 379 | t = ((s32)rom[0] >> 1)*1000; |
| 380 | else |
| 381 | t = 1000*(-1*(s32)(0x100-rom[0]) >> 1); |
Evgeniy Polyakov | bd529cf | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | t -= 250; |
| 384 | h = 1000*((s32)rom[7] - (s32)rom[6]); |
| 385 | h /= (s32)rom[7]; |
| 386 | t += h; |
| 387 | |
| 388 | return t; |
| 389 | } |
| 390 | |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 391 | static inline int w1_convert_temp(u8 rom[9], u8 fid) |
| 392 | { |
| 393 | int i; |
| 394 | |
Ahmed S. Darwish | 9d0094d | 2007-02-12 00:52:05 -0800 | [diff] [blame] | 395 | for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) |
Evgeniy Polyakov | 4e470aa | 2005-05-20 22:50:33 +0400 | [diff] [blame] | 396 | if (w1_therm_families[i].f->fid == fid) |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 397 | return w1_therm_families[i].convert(rom); |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Ben Sen | 0a19f12 | 2016-05-01 23:23:33 +0200 | [diff] [blame] | 402 | static ssize_t w1_slave_store(struct device *device, |
| 403 | struct device_attribute *attr, const char *buf, |
| 404 | size_t size) |
| 405 | { |
| 406 | int val, ret; |
| 407 | struct w1_slave *sl = dev_to_w1_slave(device); |
| 408 | int i; |
| 409 | |
| 410 | ret = kstrtoint(buf, 0, &val); |
| 411 | if (ret) |
| 412 | return ret; |
| 413 | |
| 414 | for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) { |
| 415 | if (w1_therm_families[i].f->fid == sl->family->fid) { |
| 416 | /* zero value indicates to write current configuration to eeprom */ |
| 417 | if (0 == val) |
| 418 | ret = w1_therm_families[i].eeprom(device); |
| 419 | else |
| 420 | ret = w1_therm_families[i].precision(device, val); |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | return ret ? : size; |
| 425 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Greg Kroah-Hartman | b8a9f44 | 2013-08-21 15:44:56 -0700 | [diff] [blame] | 427 | static ssize_t w1_slave_show(struct device *device, |
David Fries | 347ba8a | 2008-10-15 22:04:51 -0700 | [diff] [blame] | 428 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | { |
David Fries | 347ba8a | 2008-10-15 22:04:51 -0700 | [diff] [blame] | 430 | struct w1_slave *sl = dev_to_w1_slave(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | struct w1_master *dev = sl->master; |
Maciej Szmigiero | 377195c | 2011-11-16 00:43:16 +0100 | [diff] [blame] | 432 | u8 rom[9], crc, verdict, external_power; |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 433 | int i, ret, max_trying = 10; |
David Fries | 347ba8a | 2008-10-15 22:04:51 -0700 | [diff] [blame] | 434 | ssize_t c = PAGE_SIZE; |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 435 | u8 *family_data = sl->family_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 437 | ret = mutex_lock_interruptible(&dev->bus_mutex); |
| 438 | if (ret != 0) |
| 439 | goto post_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Ben Werbowyj | d4c3f97 | 2016-07-22 14:33:33 +1000 | [diff] [blame^] | 441 | if (!sl->family_data) { |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 442 | ret = -ENODEV; |
| 443 | goto pre_unlock; |
| 444 | } |
| 445 | |
| 446 | /* prevent the slave from going away in sleep */ |
| 447 | atomic_inc(THERM_REFCNT(family_data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | memset(rom, 0, sizeof(rom)); |
| 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | while (max_trying--) { |
David Stevenson | 867ff98 | 2012-12-18 01:37:56 +0000 | [diff] [blame] | 451 | |
| 452 | verdict = 0; |
| 453 | crc = 0; |
| 454 | |
Evgeniy Polyakov | ea7d8f6 | 2005-08-11 17:27:49 +0400 | [diff] [blame] | 455 | if (!w1_reset_select_slave(sl)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | int count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | unsigned int tm = 750; |
Maciej Szmigiero | 377195c | 2011-11-16 00:43:16 +0100 | [diff] [blame] | 458 | unsigned long sleep_rem; |
| 459 | |
| 460 | w1_write_8(dev, W1_READ_PSUPPLY); |
| 461 | external_power = w1_read_8(dev); |
| 462 | |
| 463 | if (w1_reset_select_slave(sl)) |
| 464 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
David Fries | 6cd1597 | 2008-10-15 22:04:43 -0700 | [diff] [blame] | 466 | /* 750ms strong pullup (or delay) after the convert */ |
Michael Arndt | 29e5507 | 2013-02-17 20:51:20 +0100 | [diff] [blame] | 467 | if (w1_strong_pullup == 2 || |
| 468 | (!external_power && w1_strong_pullup)) |
David Fries | 6cd1597 | 2008-10-15 22:04:43 -0700 | [diff] [blame] | 469 | w1_next_pullup(dev, tm); |
Maciej Szmigiero | 377195c | 2011-11-16 00:43:16 +0100 | [diff] [blame] | 470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | w1_write_8(dev, W1_CONVERT_TEMP); |
Maciej Szmigiero | 377195c | 2011-11-16 00:43:16 +0100 | [diff] [blame] | 472 | |
| 473 | if (external_power) { |
NeilBrown | b02f8be | 2012-05-18 15:59:52 +1000 | [diff] [blame] | 474 | mutex_unlock(&dev->bus_mutex); |
Maciej Szmigiero | 377195c | 2011-11-16 00:43:16 +0100 | [diff] [blame] | 475 | |
| 476 | sleep_rem = msleep_interruptible(tm); |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 477 | if (sleep_rem != 0) { |
| 478 | ret = -EINTR; |
| 479 | goto post_unlock; |
| 480 | } |
Maciej Szmigiero | 377195c | 2011-11-16 00:43:16 +0100 | [diff] [blame] | 481 | |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 482 | ret = mutex_lock_interruptible(&dev->bus_mutex); |
| 483 | if (ret != 0) |
| 484 | goto post_unlock; |
Maciej Szmigiero | 377195c | 2011-11-16 00:43:16 +0100 | [diff] [blame] | 485 | } else if (!w1_strong_pullup) { |
| 486 | sleep_rem = msleep_interruptible(tm); |
| 487 | if (sleep_rem != 0) { |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 488 | ret = -EINTR; |
| 489 | goto pre_unlock; |
Maciej Szmigiero | 377195c | 2011-11-16 00:43:16 +0100 | [diff] [blame] | 490 | } |
| 491 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
Evgeniy Polyakov | ea7d8f6 | 2005-08-11 17:27:49 +0400 | [diff] [blame] | 493 | if (!w1_reset_select_slave(sl)) { |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | w1_write_8(dev, W1_READ_SCRATCHPAD); |
| 496 | if ((count = w1_read_block(dev, rom, 9)) != 9) { |
David Fries | 347ba8a | 2008-10-15 22:04:51 -0700 | [diff] [blame] | 497 | dev_warn(device, "w1_read_block() " |
| 498 | "returned %u instead of 9.\n", |
| 499 | count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | crc = w1_calc_crc8(rom, 8); |
| 503 | |
David Fries | 80c002d | 2008-01-22 03:31:39 -0800 | [diff] [blame] | 504 | if (rom[8] == crc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | verdict = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
David Stevenson | 867ff98 | 2012-12-18 01:37:56 +0000 | [diff] [blame] | 509 | if (verdict) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | break; |
| 511 | } |
| 512 | |
| 513 | for (i = 0; i < 9; ++i) |
David Fries | 347ba8a | 2008-10-15 22:04:51 -0700 | [diff] [blame] | 514 | c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", rom[i]); |
| 515 | c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n", |
Ben Sen | 0a19f12 | 2016-05-01 23:23:33 +0200 | [diff] [blame] | 516 | crc, (verdict) ? "YES" : "NO"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | if (verdict) |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 518 | memcpy(family_data, rom, sizeof(rom)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | else |
David Stevenson | 867ff98 | 2012-12-18 01:37:56 +0000 | [diff] [blame] | 520 | dev_warn(device, "Read failed CRC check\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
| 522 | for (i = 0; i < 9; ++i) |
David Fries | eb2c0da | 2014-01-15 22:29:24 -0600 | [diff] [blame] | 523 | c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 524 | ((u8 *)family_data)[i]); |
Evgeniy Polyakov | bd529cf | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 525 | |
David Fries | 347ba8a | 2008-10-15 22:04:51 -0700 | [diff] [blame] | 526 | c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n", |
| 527 | w1_convert_temp(rom, sl->family->fid)); |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 528 | ret = PAGE_SIZE - c; |
| 529 | |
| 530 | pre_unlock: |
NeilBrown | b02f8be | 2012-05-18 15:59:52 +1000 | [diff] [blame] | 531 | mutex_unlock(&dev->bus_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
David Fries | f7134ee | 2015-05-08 19:51:50 -0500 | [diff] [blame] | 533 | post_unlock: |
| 534 | atomic_dec(THERM_REFCNT(family_data)); |
| 535 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Matt Campbell | d9411e5 | 2015-04-28 07:44:17 -0400 | [diff] [blame] | 538 | #define W1_42_CHAIN 0x99 |
| 539 | #define W1_42_CHAIN_OFF 0x3C |
| 540 | #define W1_42_CHAIN_OFF_INV 0xC3 |
| 541 | #define W1_42_CHAIN_ON 0x5A |
| 542 | #define W1_42_CHAIN_ON_INV 0xA5 |
| 543 | #define W1_42_CHAIN_DONE 0x96 |
| 544 | #define W1_42_CHAIN_DONE_INV 0x69 |
| 545 | #define W1_42_COND_READ 0x0F |
| 546 | #define W1_42_SUCCESS_CONFIRM_BYTE 0xAA |
| 547 | #define W1_42_FINISHED_BYTE 0xFF |
| 548 | static ssize_t w1_seq_show(struct device *device, |
| 549 | struct device_attribute *attr, char *buf) |
| 550 | { |
| 551 | struct w1_slave *sl = dev_to_w1_slave(device); |
| 552 | ssize_t c = PAGE_SIZE; |
| 553 | int rv; |
| 554 | int i; |
| 555 | u8 ack; |
| 556 | u64 rn; |
| 557 | struct w1_reg_num *reg_num; |
| 558 | int seq = 0; |
| 559 | |
Dan Carpenter | 0c6d5c8 | 2015-06-04 12:04:12 +0300 | [diff] [blame] | 560 | mutex_lock(&sl->master->bus_mutex); |
Matt Campbell | d9411e5 | 2015-04-28 07:44:17 -0400 | [diff] [blame] | 561 | /* Place all devices in CHAIN state */ |
| 562 | if (w1_reset_bus(sl->master)) |
| 563 | goto error; |
| 564 | w1_write_8(sl->master, W1_SKIP_ROM); |
| 565 | w1_write_8(sl->master, W1_42_CHAIN); |
| 566 | w1_write_8(sl->master, W1_42_CHAIN_ON); |
| 567 | w1_write_8(sl->master, W1_42_CHAIN_ON_INV); |
| 568 | msleep(sl->master->pullup_duration); |
| 569 | |
| 570 | /* check for acknowledgment */ |
| 571 | ack = w1_read_8(sl->master); |
| 572 | if (ack != W1_42_SUCCESS_CONFIRM_BYTE) |
| 573 | goto error; |
| 574 | |
| 575 | /* In case the bus fails to send 0xFF, limit*/ |
| 576 | for (i = 0; i <= 64; i++) { |
| 577 | if (w1_reset_bus(sl->master)) |
| 578 | goto error; |
| 579 | |
| 580 | w1_write_8(sl->master, W1_42_COND_READ); |
| 581 | rv = w1_read_block(sl->master, (u8 *)&rn, 8); |
| 582 | reg_num = (struct w1_reg_num *) &rn; |
Dan Carpenter | a14ef24 | 2015-06-01 12:55:37 +0300 | [diff] [blame] | 583 | if (reg_num->family == W1_42_FINISHED_BYTE) |
Matt Campbell | d9411e5 | 2015-04-28 07:44:17 -0400 | [diff] [blame] | 584 | break; |
| 585 | if (sl->reg_num.id == reg_num->id) |
| 586 | seq = i; |
| 587 | |
| 588 | w1_write_8(sl->master, W1_42_CHAIN); |
| 589 | w1_write_8(sl->master, W1_42_CHAIN_DONE); |
| 590 | w1_write_8(sl->master, W1_42_CHAIN_DONE_INV); |
| 591 | w1_read_block(sl->master, &ack, sizeof(ack)); |
| 592 | |
| 593 | /* check for acknowledgment */ |
| 594 | ack = w1_read_8(sl->master); |
| 595 | if (ack != W1_42_SUCCESS_CONFIRM_BYTE) |
| 596 | goto error; |
| 597 | |
| 598 | } |
| 599 | |
| 600 | /* Exit from CHAIN state */ |
| 601 | if (w1_reset_bus(sl->master)) |
| 602 | goto error; |
| 603 | w1_write_8(sl->master, W1_SKIP_ROM); |
| 604 | w1_write_8(sl->master, W1_42_CHAIN); |
| 605 | w1_write_8(sl->master, W1_42_CHAIN_OFF); |
| 606 | w1_write_8(sl->master, W1_42_CHAIN_OFF_INV); |
| 607 | |
| 608 | /* check for acknowledgment */ |
| 609 | ack = w1_read_8(sl->master); |
| 610 | if (ack != W1_42_SUCCESS_CONFIRM_BYTE) |
| 611 | goto error; |
Dan Carpenter | 0c6d5c8 | 2015-06-04 12:04:12 +0300 | [diff] [blame] | 612 | mutex_unlock(&sl->master->bus_mutex); |
Matt Campbell | d9411e5 | 2015-04-28 07:44:17 -0400 | [diff] [blame] | 613 | |
| 614 | c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq); |
| 615 | return PAGE_SIZE - c; |
| 616 | error: |
| 617 | mutex_unlock(&sl->master->bus_mutex); |
| 618 | return -EIO; |
| 619 | } |
| 620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | static int __init w1_therm_init(void) |
| 622 | { |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 623 | int err, i; |
| 624 | |
Ahmed S. Darwish | 9d0094d | 2007-02-12 00:52:05 -0800 | [diff] [blame] | 625 | for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) { |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 626 | err = w1_register_family(w1_therm_families[i].f); |
| 627 | if (err) |
| 628 | w1_therm_families[i].broken = 1; |
| 629 | } |
| 630 | |
| 631 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | static void __exit w1_therm_fini(void) |
| 635 | { |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 636 | int i; |
| 637 | |
Ahmed S. Darwish | 9d0094d | 2007-02-12 00:52:05 -0800 | [diff] [blame] | 638 | for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) |
Evgeniy Polyakov | 718a538 | 2005-04-17 22:58:14 +0400 | [diff] [blame] | 639 | if (!w1_therm_families[i].broken) |
| 640 | w1_unregister_family(w1_therm_families[i].f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | module_init(w1_therm_init); |
| 644 | module_exit(w1_therm_fini); |