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