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