Skip to content

Commit 01f938b

Browse files
pelwellpopcornmix
authored andcommitted
Merge pull request #1059 from pelwell/rpi-4.0.y
w1_therm: Back-port locking improvements from 4.2-rc1
1 parent 995c955 commit 01f938b

File tree

3 files changed

+117
-2
lines changed

3 files changed

+117
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
What: /sys/bus/w1/devices/.../w1_seq
2+
Date: Apr 2015
3+
Contact: Matt Campbell <[email protected]>
4+
Description: Support for the DS28EA00 chain sequence function
5+
see Documentation/w1/slaves/w1_therm for detailed information
6+
Users: any user space application which wants to communicate with DS28EA00

Documentation/w1/slaves/w1_therm

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ Author: Evgeniy Polyakov <[email protected]>
1111
Description
1212
-----------
1313

14-
w1_therm provides basic temperature conversion for ds18*20 devices.
14+
w1_therm provides basic temperature conversion for ds18*20 devices, and the
15+
ds28ea00 device.
1516
supported family codes:
1617
W1_THERM_DS18S20 0x10
1718
W1_THERM_DS1822 0x22
1819
W1_THERM_DS18B20 0x28
1920
W1_THERM_DS1825 0x3B
21+
W1_THERM_DS28EA00 0x42
2022

2123
Support is provided through the sysfs w1_slave file. Each open and
2224
read sequence will initiate a temperature conversion then provide two
@@ -48,3 +50,10 @@ resistor). The DS18b20 temperature sensor specification lists a
4850
maximum current draw of 1.5mA and that a 5k pullup resistor is not
4951
sufficient. The strong pullup is designed to provide the additional
5052
current required.
53+
54+
The DS28EA00 provides an additional two pins for implementing a sequence
55+
detection algorithm. This feature allows you to determine the physical
56+
location of the chip in the 1-wire bus without needing pre-existing
57+
knowledge of the bus ordering. Support is provided through the sysfs
58+
w1_seq file. The file will contain a single line with an integer value
59+
representing the device index in the bus starting at 0.

drivers/w1/slaves/w1_therm.c

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,37 @@ static void w1_therm_remove_slave(struct w1_slave *sl)
9292
static ssize_t w1_slave_show(struct device *device,
9393
struct device_attribute *attr, char *buf);
9494

95+
static ssize_t w1_seq_show(struct device *device,
96+
struct device_attribute *attr, char *buf);
97+
9598
static DEVICE_ATTR_RO(w1_slave);
99+
static DEVICE_ATTR_RO(w1_seq);
96100

97101
static struct attribute *w1_therm_attrs[] = {
98102
&dev_attr_w1_slave.attr,
99103
NULL,
100104
};
105+
106+
static struct attribute *w1_ds28ea00_attrs[] = {
107+
&dev_attr_w1_slave.attr,
108+
&dev_attr_w1_seq.attr,
109+
NULL,
110+
};
101111
ATTRIBUTE_GROUPS(w1_therm);
112+
ATTRIBUTE_GROUPS(w1_ds28ea00);
102113

103114
static struct w1_family_ops w1_therm_fops = {
104115
.add_slave = w1_therm_add_slave,
105116
.remove_slave = w1_therm_remove_slave,
106117
.groups = w1_therm_groups,
107118
};
108119

120+
static struct w1_family_ops w1_ds28ea00_fops = {
121+
.add_slave = w1_therm_add_slave,
122+
.remove_slave = w1_therm_remove_slave,
123+
.groups = w1_ds28ea00_groups,
124+
};
125+
109126
static struct w1_family w1_therm_family_DS18S20 = {
110127
.fid = W1_THERM_DS18S20,
111128
.fops = &w1_therm_fops,
@@ -123,7 +140,7 @@ static struct w1_family w1_therm_family_DS1822 = {
123140

124141
static struct w1_family w1_therm_family_DS28EA00 = {
125142
.fid = W1_THERM_DS28EA00,
126-
.fops = &w1_therm_fops,
143+
.fops = &w1_ds28ea00_fops,
127144
};
128145

129146
static struct w1_family w1_therm_family_DS1825 = {
@@ -316,6 +333,89 @@ static ssize_t w1_slave_show(struct device *device,
316333
return ret;
317334
}
318335

336+
#define W1_42_CHAIN 0x99
337+
#define W1_42_CHAIN_OFF 0x3C
338+
#define W1_42_CHAIN_OFF_INV 0xC3
339+
#define W1_42_CHAIN_ON 0x5A
340+
#define W1_42_CHAIN_ON_INV 0xA5
341+
#define W1_42_CHAIN_DONE 0x96
342+
#define W1_42_CHAIN_DONE_INV 0x69
343+
#define W1_42_COND_READ 0x0F
344+
#define W1_42_SUCCESS_CONFIRM_BYTE 0xAA
345+
#define W1_42_FINISHED_BYTE 0xFF
346+
static ssize_t w1_seq_show(struct device *device,
347+
struct device_attribute *attr, char *buf)
348+
{
349+
struct w1_slave *sl = dev_to_w1_slave(device);
350+
ssize_t c = PAGE_SIZE;
351+
int rv;
352+
int i;
353+
u8 ack;
354+
u64 rn;
355+
struct w1_reg_num *reg_num;
356+
int seq = 0;
357+
358+
mutex_lock(&sl->master->bus_mutex);
359+
/* Place all devices in CHAIN state */
360+
if (w1_reset_bus(sl->master))
361+
goto error;
362+
w1_write_8(sl->master, W1_SKIP_ROM);
363+
w1_write_8(sl->master, W1_42_CHAIN);
364+
w1_write_8(sl->master, W1_42_CHAIN_ON);
365+
w1_write_8(sl->master, W1_42_CHAIN_ON_INV);
366+
msleep(sl->master->pullup_duration);
367+
368+
/* check for acknowledgment */
369+
ack = w1_read_8(sl->master);
370+
if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
371+
goto error;
372+
373+
/* In case the bus fails to send 0xFF, limit*/
374+
for (i = 0; i <= 64; i++) {
375+
if (w1_reset_bus(sl->master))
376+
goto error;
377+
378+
w1_write_8(sl->master, W1_42_COND_READ);
379+
rv = w1_read_block(sl->master, (u8 *)&rn, 8);
380+
reg_num = (struct w1_reg_num *) &rn;
381+
if (reg_num->family == W1_42_FINISHED_BYTE)
382+
break;
383+
if (sl->reg_num.id == reg_num->id)
384+
seq = i;
385+
386+
w1_write_8(sl->master, W1_42_CHAIN);
387+
w1_write_8(sl->master, W1_42_CHAIN_DONE);
388+
w1_write_8(sl->master, W1_42_CHAIN_DONE_INV);
389+
w1_read_block(sl->master, &ack, sizeof(ack));
390+
391+
/* check for acknowledgment */
392+
ack = w1_read_8(sl->master);
393+
if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
394+
goto error;
395+
396+
}
397+
398+
/* Exit from CHAIN state */
399+
if (w1_reset_bus(sl->master))
400+
goto error;
401+
w1_write_8(sl->master, W1_SKIP_ROM);
402+
w1_write_8(sl->master, W1_42_CHAIN);
403+
w1_write_8(sl->master, W1_42_CHAIN_OFF);
404+
w1_write_8(sl->master, W1_42_CHAIN_OFF_INV);
405+
406+
/* check for acknowledgment */
407+
ack = w1_read_8(sl->master);
408+
if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
409+
goto error;
410+
mutex_unlock(&sl->master->bus_mutex);
411+
412+
c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq);
413+
return PAGE_SIZE - c;
414+
error:
415+
mutex_unlock(&sl->master->bus_mutex);
416+
return -EIO;
417+
}
418+
319419
static int __init w1_therm_init(void)
320420
{
321421
int err, i;

0 commit comments

Comments
 (0)