4 * Copyright (c) 2017 Maciej S. Szmigiero <mail@maciej.szmigiero.name>
5 * Based on w1_therm.c copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the therms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/device.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/mutex.h>
24 #include <linux/string.h>
25 #include <linux/types.h>
28 #include "../w1_family.h"
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Maciej S. Szmigiero <mail@maciej.szmigiero.name>");
32 MODULE_DESCRIPTION("Driver for 1-wire Dallas DS2405 PIO.");
33 MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2405
));
35 static int w1_ds2405_select(struct w1_slave
*sl
, bool only_active
)
37 struct w1_master
*dev
= sl
->master
;
39 u64 dev_addr
= le64_to_cpu(*(u64
*)&sl
->reg_num
);
42 if (w1_reset_bus(dev
) != 0)
46 * We cannot use a normal Match ROM command
47 * since doing so would toggle PIO state
49 w1_write_8(dev
, only_active
? W1_ALARM_SEARCH
: W1_SEARCH
);
51 for (bit_ctr
= 0; bit_ctr
< 64; bit_ctr
++) {
52 int bit2send
= !!(dev_addr
& BIT(bit_ctr
));
55 ret
= w1_triplet(dev
, bit2send
);
57 if ((ret
& (BIT(0) | BIT(1))) ==
58 (BIT(0) | BIT(1))) /* no devices found */
61 if (!!(ret
& BIT(2)) != bit2send
)
62 /* wrong direction taken - no such device */
69 static int w1_ds2405_read_pio(struct w1_slave
*sl
)
71 if (w1_ds2405_select(sl
, true))
72 return 0; /* "active" means PIO is low */
74 if (w1_ds2405_select(sl
, false))
80 static ssize_t
state_show(struct device
*device
,
81 struct device_attribute
*attr
, char *buf
)
83 struct w1_slave
*sl
= dev_to_w1_slave(device
);
84 struct w1_master
*dev
= sl
->master
;
90 ret
= mutex_lock_interruptible(&dev
->bus_mutex
);
94 if (!w1_ds2405_select(sl
, false)) {
99 state
= w1_read_8(dev
);
102 dev_err(device
, "non-consistent state %x\n", state
);
107 *buf
= state
? '1' : '0';
112 mutex_unlock(&dev
->bus_mutex
);
117 static ssize_t
output_show(struct device
*device
,
118 struct device_attribute
*attr
, char *buf
)
120 struct w1_slave
*sl
= dev_to_w1_slave(device
);
121 struct w1_master
*dev
= sl
->master
;
126 ret
= mutex_lock_interruptible(&dev
->bus_mutex
);
130 ret
= w1_ds2405_read_pio(sl
);
136 *buf
= ret
? '1' : '0';
141 mutex_unlock(&dev
->bus_mutex
);
146 static ssize_t
output_store(struct device
*device
,
147 struct device_attribute
*attr
,
148 const char *buf
, size_t count
)
150 struct w1_slave
*sl
= dev_to_w1_slave(device
);
151 struct w1_master
*dev
= sl
->master
;
153 int ret
, current_pio
;
160 if (sscanf(buf
, " %u%n", &val
, &ret
) < 1)
163 if (val
!= 0 && val
!= 1)
168 ret
= mutex_lock_interruptible(&dev
->bus_mutex
);
172 current_pio
= w1_ds2405_read_pio(sl
);
173 if (current_pio
< 0) {
174 f_retval
= current_pio
;
178 if (current_pio
== val
)
181 if (w1_reset_bus(dev
) != 0) {
187 * can't use w1_reset_select_slave() here since it uses Skip ROM if
188 * there is only one device on bus
191 u64 dev_addr
= le64_to_cpu(*(u64
*)&sl
->reg_num
);
194 cmd
[0] = W1_MATCH_ROM
;
195 memcpy(&cmd
[1], &dev_addr
, sizeof(dev_addr
));
197 w1_write_block(dev
, cmd
, sizeof(cmd
));
202 mutex_unlock(&dev
->bus_mutex
);
207 static DEVICE_ATTR_RO(state
);
208 static DEVICE_ATTR_RW(output
);
210 static struct attribute
*w1_ds2405_attrs
[] = {
211 &dev_attr_state
.attr
,
212 &dev_attr_output
.attr
,
216 ATTRIBUTE_GROUPS(w1_ds2405
);
218 static struct w1_family_ops w1_ds2405_fops
= {
219 .groups
= w1_ds2405_groups
222 static struct w1_family w1_family_ds2405
= {
223 .fid
= W1_FAMILY_DS2405
,
224 .fops
= &w1_ds2405_fops
227 module_w1_family(w1_family_ds2405
);