blob: 83a5036b990bc505a9d5f7ddd9e264256b3c3877 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Evgeniy Polyakov77859252005-05-20 22:33:25 +04002 * w1_therm.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Evgeniy Polyakova8018762011-08-25 15:59:06 -07004 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
Evgeniy Polyakov77859252005-05-20 22:33:25 +04005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 Virof6a57032006-10-18 01:47:25 -040027#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/device.h>
29#include <linux/types.h>
David Frieseb2c0da2014-01-15 22:29:24 -060030#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/delay.h>
32
Evgeniy Polyakovbd529cf2005-12-06 13:38:28 +030033#include "../w1.h"
Evgeniy Polyakovbd529cf2005-12-06 13:38:28 +030034#include "../w1_int.h"
35#include "../w1_family.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37MODULE_LICENSE("GPL");
Evgeniy Polyakova8018762011-08-25 15:59:06 -070038MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
Alexander Stein8d7bda52013-05-26 20:06:50 +020040MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20));
41MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822));
42MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20));
43MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825));
44MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00));
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
David Fries6cd15972008-10-15 22:04:43 -070046/* 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 Arndt29e55072013-02-17 20:51:20 +010050 * 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 Fries6cd15972008-10-15 22:04:43 -070058 */
59static int w1_strong_pullup = 1;
60module_param_named(strong_pullup, w1_strong_pullup, int, 0);
61
David Friesf7134ee2015-05-08 19:51:50 -050062struct 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 Frieseb2c0da2014-01-15 22:29:24 -060071static int w1_therm_add_slave(struct w1_slave *sl)
72{
David Friesf7134ee2015-05-08 19:51:50 -050073 sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
74 GFP_KERNEL);
David Frieseb2c0da2014-01-15 22:29:24 -060075 if (!sl->family_data)
76 return -ENOMEM;
David Friesf7134ee2015-05-08 19:51:50 -050077 atomic_set(THERM_REFCNT(sl->family_data), 1);
David Frieseb2c0da2014-01-15 22:29:24 -060078 return 0;
79}
80
81static void w1_therm_remove_slave(struct w1_slave *sl)
82{
David Friesf7134ee2015-05-08 19:51:50 -050083 int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
Ben Werbowyjd4c3f972016-07-22 14:33:33 +100084 while (refcnt) {
David Friesf7134ee2015-05-08 19:51:50 -050085 msleep(1000);
86 refcnt = atomic_read(THERM_REFCNT(sl->family_data));
87 }
David Frieseb2c0da2014-01-15 22:29:24 -060088 kfree(sl->family_data);
89 sl->family_data = NULL;
90}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Greg Kroah-Hartmanb8a9f442013-08-21 15:44:56 -070092static ssize_t w1_slave_show(struct device *device,
David Fries347ba8a2008-10-15 22:04:51 -070093 struct device_attribute *attr, char *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Ben Sen0a19f122016-05-01 23:23:33 +020095static ssize_t w1_slave_store(struct device *device,
96 struct device_attribute *attr, const char *buf, size_t size);
97
Matt Campbelld9411e52015-04-28 07:44:17 -040098static ssize_t w1_seq_show(struct device *device,
99 struct device_attribute *attr, char *buf);
100
Ben Sen0a19f122016-05-01 23:23:33 +0200101static DEVICE_ATTR_RW(w1_slave);
Matt Campbelld9411e52015-04-28 07:44:17 -0400102static DEVICE_ATTR_RO(w1_seq);
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400103
Greg Kroah-Hartmanb8a9f442013-08-21 15:44:56 -0700104static struct attribute *w1_therm_attrs[] = {
105 &dev_attr_w1_slave.attr,
106 NULL,
107};
Matt Campbelld9411e52015-04-28 07:44:17 -0400108
109static struct attribute *w1_ds28ea00_attrs[] = {
110 &dev_attr_w1_slave.attr,
111 &dev_attr_w1_seq.attr,
112 NULL,
113};
Greg Kroah-Hartmanb8a9f442013-08-21 15:44:56 -0700114ATTRIBUTE_GROUPS(w1_therm);
Matt Campbelld9411e52015-04-28 07:44:17 -0400115ATTRIBUTE_GROUPS(w1_ds28ea00);
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static struct w1_family_ops w1_therm_fops = {
David Frieseb2c0da2014-01-15 22:29:24 -0600118 .add_slave = w1_therm_add_slave,
119 .remove_slave = w1_therm_remove_slave,
Greg Kroah-Hartmanb8a9f442013-08-21 15:44:56 -0700120 .groups = w1_therm_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Matt Campbelld9411e52015-04-28 07:44:17 -0400123static 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 Polyakov718a5382005-04-17 22:58:14 +0400129static struct w1_family w1_therm_family_DS18S20 = {
130 .fid = W1_THERM_DS18S20,
131 .fops = &w1_therm_fops,
132};
133
134static struct w1_family w1_therm_family_DS18B20 = {
135 .fid = W1_THERM_DS18B20,
136 .fops = &w1_therm_fops,
137};
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400138
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400139static struct w1_family w1_therm_family_DS1822 = {
140 .fid = W1_THERM_DS1822,
141 .fops = &w1_therm_fops,
142};
143
Christian Glindkampf7b13712011-07-26 16:08:55 -0700144static struct w1_family w1_therm_family_DS28EA00 = {
145 .fid = W1_THERM_DS28EA00,
Matt Campbelld9411e52015-04-28 07:44:17 -0400146 .fops = &w1_ds28ea00_fops,
Christian Glindkampf7b13712011-07-26 16:08:55 -0700147};
148
Raphael Assenatf3261df2012-08-16 12:56:40 -0400149static struct w1_family w1_therm_family_DS1825 = {
150 .fid = W1_THERM_DS1825,
151 .fops = &w1_therm_fops,
152};
153
Ben Werbowyjd4c3f972016-07-22 14:33:33 +1000154struct w1_therm_family_converter {
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400155 u8 broken;
156 u16 reserved;
157 struct w1_family *f;
158 int (*convert)(u8 rom[9]);
Ben Sen0a19f122016-05-01 23:23:33 +0200159 int (*precision)(struct device *device, int val);
160 int (*eeprom)(struct device *device);
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400161};
162
Ben Sen0a19f122016-05-01 23:23:33 +0200163/* write configuration to eeprom */
164static inline int w1_therm_eeprom(struct device *device);
165
166/* Set precision for conversion */
167static inline int w1_DS18B20_precision(struct device *device, int val);
168static inline int w1_DS18S20_precision(struct device *device, int val);
169
David Fries7129b122008-02-06 01:38:09 -0800170/* The return value is millidegrees Centigrade. */
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400171static inline int w1_DS18B20_convert_temp(u8 rom[9]);
172static inline int w1_DS18S20_convert_temp(u8 rom[9]);
173
174static struct w1_therm_family_converter w1_therm_families[] = {
175 {
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400176 .f = &w1_therm_family_DS18S20,
Ben Sen0a19f122016-05-01 23:23:33 +0200177 .convert = w1_DS18S20_convert_temp,
178 .precision = w1_DS18S20_precision,
179 .eeprom = w1_therm_eeprom
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400180 },
181 {
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400182 .f = &w1_therm_family_DS1822,
Ben Sen0a19f122016-05-01 23:23:33 +0200183 .convert = w1_DS18B20_convert_temp,
184 .precision = w1_DS18S20_precision,
185 .eeprom = w1_therm_eeprom
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400186 },
187 {
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400188 .f = &w1_therm_family_DS18B20,
Ben Sen0a19f122016-05-01 23:23:33 +0200189 .convert = w1_DS18B20_convert_temp,
190 .precision = w1_DS18B20_precision,
191 .eeprom = w1_therm_eeprom
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400192 },
Christian Glindkampf7b13712011-07-26 16:08:55 -0700193 {
194 .f = &w1_therm_family_DS28EA00,
Ben Sen0a19f122016-05-01 23:23:33 +0200195 .convert = w1_DS18B20_convert_temp,
196 .precision = w1_DS18S20_precision,
197 .eeprom = w1_therm_eeprom
Christian Glindkampf7b13712011-07-26 16:08:55 -0700198 },
Raphael Assenatf3261df2012-08-16 12:56:40 -0400199 {
200 .f = &w1_therm_family_DS1825,
Ben Sen0a19f122016-05-01 23:23:33 +0200201 .convert = w1_DS18B20_convert_temp,
202 .precision = w1_DS18S20_precision,
203 .eeprom = w1_therm_eeprom
Raphael Assenatf3261df2012-08-16 12:56:40 -0400204 }
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400205};
206
Ben Sen0a19f122016-05-01 23:23:33 +0200207static 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
271pre_unlock:
272 mutex_unlock(&dev->bus_mutex);
273
274post_unlock:
275 atomic_dec(THERM_REFCNT(family_data));
276 return ret;
277}
278
279/* DS18S20 does not feature configuration register */
280static inline int w1_DS18S20_precision(struct device *device, int val)
281{
282 return 0;
283}
284
285static 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 Werbowyjd4c3f972016-07-22 14:33:33 +1000295 if (val > 12 || val < 9) {
Ben Sen0a19f122016-05-01 23:23:33 +0200296 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);
Ben Werbowyjaaf16f72016-07-22 14:33:34 +1000338 count = w1_read_block(dev, rom, 9);
339 if (count != 9)
Ben Sen0a19f122016-05-01 23:23:33 +0200340 dev_warn(device, "w1_read_block() returned %u instead of 9.\n", count);
341
342 crc = w1_calc_crc8(rom, 8);
343 if (rom[8] == crc) {
344 rom[4] = (rom[4] & ~mask) | (precision_bits & mask);
345
346 if (!w1_reset_select_slave(sl)) {
347 w1_write_8(dev, W1_WRITE_SCRATCHPAD);
348 w1_write_8(dev, rom[2]);
349 w1_write_8(dev, rom[3]);
350 w1_write_8(dev, rom[4]);
351
352 break;
353 }
354 }
355 }
356 }
357
358pre_unlock:
359 mutex_unlock(&dev->bus_mutex);
360
361post_unlock:
362 atomic_dec(THERM_REFCNT(family_data));
363 return ret;
364}
365
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400366static inline int w1_DS18B20_convert_temp(u8 rom[9])
367{
Ian Dall9a6a1ec2010-04-23 13:17:53 -0400368 s16 t = le16_to_cpup((__le16 *)rom);
369 return t*1000/16;
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400370}
371
372static inline int w1_DS18S20_convert_temp(u8 rom[9])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 int t, h;
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400375
376 if (!rom[7])
377 return 0;
Evgeniy Polyakovbd529cf2005-12-06 13:38:28 +0300378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (rom[1] == 0)
380 t = ((s32)rom[0] >> 1)*1000;
381 else
382 t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
Evgeniy Polyakovbd529cf2005-12-06 13:38:28 +0300383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 t -= 250;
385 h = 1000*((s32)rom[7] - (s32)rom[6]);
386 h /= (s32)rom[7];
387 t += h;
388
389 return t;
390}
391
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400392static inline int w1_convert_temp(u8 rom[9], u8 fid)
393{
394 int i;
395
Ahmed S. Darwish9d0094d2007-02-12 00:52:05 -0800396 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
Evgeniy Polyakov4e470aa2005-05-20 22:50:33 +0400397 if (w1_therm_families[i].f->fid == fid)
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400398 return w1_therm_families[i].convert(rom);
399
400 return 0;
401}
402
Ben Sen0a19f122016-05-01 23:23:33 +0200403static ssize_t w1_slave_store(struct device *device,
404 struct device_attribute *attr, const char *buf,
405 size_t size)
406{
407 int val, ret;
408 struct w1_slave *sl = dev_to_w1_slave(device);
409 int i;
410
411 ret = kstrtoint(buf, 0, &val);
412 if (ret)
413 return ret;
414
415 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
416 if (w1_therm_families[i].f->fid == sl->family->fid) {
417 /* zero value indicates to write current configuration to eeprom */
418 if (0 == val)
419 ret = w1_therm_families[i].eeprom(device);
420 else
421 ret = w1_therm_families[i].precision(device, val);
422 break;
423 }
424 }
425 return ret ? : size;
426}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Greg Kroah-Hartmanb8a9f442013-08-21 15:44:56 -0700428static ssize_t w1_slave_show(struct device *device,
David Fries347ba8a2008-10-15 22:04:51 -0700429 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
David Fries347ba8a2008-10-15 22:04:51 -0700431 struct w1_slave *sl = dev_to_w1_slave(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 struct w1_master *dev = sl->master;
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100433 u8 rom[9], crc, verdict, external_power;
David Friesf7134ee2015-05-08 19:51:50 -0500434 int i, ret, max_trying = 10;
David Fries347ba8a2008-10-15 22:04:51 -0700435 ssize_t c = PAGE_SIZE;
David Friesf7134ee2015-05-08 19:51:50 -0500436 u8 *family_data = sl->family_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
David Friesf7134ee2015-05-08 19:51:50 -0500438 ret = mutex_lock_interruptible(&dev->bus_mutex);
439 if (ret != 0)
440 goto post_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Ben Werbowyjd4c3f972016-07-22 14:33:33 +1000442 if (!sl->family_data) {
David Friesf7134ee2015-05-08 19:51:50 -0500443 ret = -ENODEV;
444 goto pre_unlock;
445 }
446
447 /* prevent the slave from going away in sleep */
448 atomic_inc(THERM_REFCNT(family_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 memset(rom, 0, sizeof(rom));
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 while (max_trying--) {
David Stevenson867ff982012-12-18 01:37:56 +0000452
453 verdict = 0;
454 crc = 0;
455
Evgeniy Polyakovea7d8f62005-08-11 17:27:49 +0400456 if (!w1_reset_select_slave(sl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 unsigned int tm = 750;
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100459 unsigned long sleep_rem;
460
461 w1_write_8(dev, W1_READ_PSUPPLY);
462 external_power = w1_read_8(dev);
463
464 if (w1_reset_select_slave(sl))
465 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
David Fries6cd15972008-10-15 22:04:43 -0700467 /* 750ms strong pullup (or delay) after the convert */
Michael Arndt29e55072013-02-17 20:51:20 +0100468 if (w1_strong_pullup == 2 ||
469 (!external_power && w1_strong_pullup))
David Fries6cd15972008-10-15 22:04:43 -0700470 w1_next_pullup(dev, tm);
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 w1_write_8(dev, W1_CONVERT_TEMP);
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100473
474 if (external_power) {
NeilBrownb02f8be2012-05-18 15:59:52 +1000475 mutex_unlock(&dev->bus_mutex);
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100476
477 sleep_rem = msleep_interruptible(tm);
David Friesf7134ee2015-05-08 19:51:50 -0500478 if (sleep_rem != 0) {
479 ret = -EINTR;
480 goto post_unlock;
481 }
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100482
David Friesf7134ee2015-05-08 19:51:50 -0500483 ret = mutex_lock_interruptible(&dev->bus_mutex);
484 if (ret != 0)
485 goto post_unlock;
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100486 } else if (!w1_strong_pullup) {
487 sleep_rem = msleep_interruptible(tm);
488 if (sleep_rem != 0) {
David Friesf7134ee2015-05-08 19:51:50 -0500489 ret = -EINTR;
490 goto pre_unlock;
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100491 }
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Evgeniy Polyakovea7d8f62005-08-11 17:27:49 +0400494 if (!w1_reset_select_slave(sl)) {
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 w1_write_8(dev, W1_READ_SCRATCHPAD);
Ben Werbowyjaaf16f72016-07-22 14:33:34 +1000497 count = w1_read_block(dev, rom, 9);
498 if (count != 9) {
David Fries347ba8a2008-10-15 22:04:51 -0700499 dev_warn(device, "w1_read_block() "
500 "returned %u instead of 9.\n",
501 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503
504 crc = w1_calc_crc8(rom, 8);
505
David Fries80c002d2008-01-22 03:31:39 -0800506 if (rom[8] == crc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 verdict = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 }
510
David Stevenson867ff982012-12-18 01:37:56 +0000511 if (verdict)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 break;
513 }
514
515 for (i = 0; i < 9; ++i)
David Fries347ba8a2008-10-15 22:04:51 -0700516 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", rom[i]);
517 c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
Ben Sen0a19f122016-05-01 23:23:33 +0200518 crc, (verdict) ? "YES" : "NO");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (verdict)
David Friesf7134ee2015-05-08 19:51:50 -0500520 memcpy(family_data, rom, sizeof(rom));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 else
David Stevenson867ff982012-12-18 01:37:56 +0000522 dev_warn(device, "Read failed CRC check\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 for (i = 0; i < 9; ++i)
David Frieseb2c0da2014-01-15 22:29:24 -0600525 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
David Friesf7134ee2015-05-08 19:51:50 -0500526 ((u8 *)family_data)[i]);
Evgeniy Polyakovbd529cf2005-12-06 13:38:28 +0300527
David Fries347ba8a2008-10-15 22:04:51 -0700528 c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
529 w1_convert_temp(rom, sl->family->fid));
David Friesf7134ee2015-05-08 19:51:50 -0500530 ret = PAGE_SIZE - c;
531
532pre_unlock:
NeilBrownb02f8be2012-05-18 15:59:52 +1000533 mutex_unlock(&dev->bus_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
David Friesf7134ee2015-05-08 19:51:50 -0500535post_unlock:
536 atomic_dec(THERM_REFCNT(family_data));
537 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Matt Campbelld9411e52015-04-28 07:44:17 -0400540#define W1_42_CHAIN 0x99
541#define W1_42_CHAIN_OFF 0x3C
542#define W1_42_CHAIN_OFF_INV 0xC3
543#define W1_42_CHAIN_ON 0x5A
544#define W1_42_CHAIN_ON_INV 0xA5
545#define W1_42_CHAIN_DONE 0x96
546#define W1_42_CHAIN_DONE_INV 0x69
547#define W1_42_COND_READ 0x0F
548#define W1_42_SUCCESS_CONFIRM_BYTE 0xAA
549#define W1_42_FINISHED_BYTE 0xFF
550static ssize_t w1_seq_show(struct device *device,
551 struct device_attribute *attr, char *buf)
552{
553 struct w1_slave *sl = dev_to_w1_slave(device);
554 ssize_t c = PAGE_SIZE;
555 int rv;
556 int i;
557 u8 ack;
558 u64 rn;
559 struct w1_reg_num *reg_num;
560 int seq = 0;
561
Dan Carpenter0c6d5c82015-06-04 12:04:12 +0300562 mutex_lock(&sl->master->bus_mutex);
Matt Campbelld9411e52015-04-28 07:44:17 -0400563 /* Place all devices in CHAIN state */
564 if (w1_reset_bus(sl->master))
565 goto error;
566 w1_write_8(sl->master, W1_SKIP_ROM);
567 w1_write_8(sl->master, W1_42_CHAIN);
568 w1_write_8(sl->master, W1_42_CHAIN_ON);
569 w1_write_8(sl->master, W1_42_CHAIN_ON_INV);
570 msleep(sl->master->pullup_duration);
571
572 /* check for acknowledgment */
573 ack = w1_read_8(sl->master);
574 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
575 goto error;
576
577 /* In case the bus fails to send 0xFF, limit*/
578 for (i = 0; i <= 64; i++) {
579 if (w1_reset_bus(sl->master))
580 goto error;
581
582 w1_write_8(sl->master, W1_42_COND_READ);
583 rv = w1_read_block(sl->master, (u8 *)&rn, 8);
584 reg_num = (struct w1_reg_num *) &rn;
Dan Carpentera14ef242015-06-01 12:55:37 +0300585 if (reg_num->family == W1_42_FINISHED_BYTE)
Matt Campbelld9411e52015-04-28 07:44:17 -0400586 break;
587 if (sl->reg_num.id == reg_num->id)
588 seq = i;
589
590 w1_write_8(sl->master, W1_42_CHAIN);
591 w1_write_8(sl->master, W1_42_CHAIN_DONE);
592 w1_write_8(sl->master, W1_42_CHAIN_DONE_INV);
593 w1_read_block(sl->master, &ack, sizeof(ack));
594
595 /* check for acknowledgment */
596 ack = w1_read_8(sl->master);
597 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
598 goto error;
599
600 }
601
602 /* Exit from CHAIN state */
603 if (w1_reset_bus(sl->master))
604 goto error;
605 w1_write_8(sl->master, W1_SKIP_ROM);
606 w1_write_8(sl->master, W1_42_CHAIN);
607 w1_write_8(sl->master, W1_42_CHAIN_OFF);
608 w1_write_8(sl->master, W1_42_CHAIN_OFF_INV);
609
610 /* check for acknowledgment */
611 ack = w1_read_8(sl->master);
612 if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
613 goto error;
Dan Carpenter0c6d5c82015-06-04 12:04:12 +0300614 mutex_unlock(&sl->master->bus_mutex);
Matt Campbelld9411e52015-04-28 07:44:17 -0400615
616 c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq);
617 return PAGE_SIZE - c;
618error:
619 mutex_unlock(&sl->master->bus_mutex);
620 return -EIO;
621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623static int __init w1_therm_init(void)
624{
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400625 int err, i;
626
Ahmed S. Darwish9d0094d2007-02-12 00:52:05 -0800627 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400628 err = w1_register_family(w1_therm_families[i].f);
629 if (err)
630 w1_therm_families[i].broken = 1;
631 }
632
633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
636static void __exit w1_therm_fini(void)
637{
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400638 int i;
639
Ahmed S. Darwish9d0094d2007-02-12 00:52:05 -0800640 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400641 if (!w1_therm_families[i].broken)
642 w1_unregister_family(w1_therm_families[i].f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645module_init(w1_therm_init);
646module_exit(w1_therm_fini);