1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (c) 2017 Maciej S. Szmigiero <mail@maciej.szmigiero.name>
6 * Based on w1_therm.c copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/mutex.h>
14 #include <linux/string.h>
15 #include <linux/types.h>
19 #define W1_FAMILY_DS2405 0x05
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("Maciej S. Szmigiero <mail@maciej.szmigiero.name>");
23 MODULE_DESCRIPTION("Driver for 1-wire Dallas DS2405 PIO.");
24 MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2405));
26 static int w1_ds2405_select(struct w1_slave *sl, bool only_active)
28 struct w1_master *dev = sl->master;
30 u64 dev_addr = le64_to_cpu(*(u64 *)&sl->reg_num);
33 if (w1_reset_bus(dev) != 0)
37 * We cannot use a normal Match ROM command
38 * since doing so would toggle PIO state
40 w1_write_8(dev, only_active ? W1_ALARM_SEARCH : W1_SEARCH);
42 for (bit_ctr = 0; bit_ctr < 64; bit_ctr++) {
43 int bit2send = !!(dev_addr & BIT(bit_ctr));
46 ret = w1_triplet(dev, bit2send);
48 if ((ret & (BIT(0) | BIT(1))) ==
49 (BIT(0) | BIT(1))) /* no devices found */
52 if (!!(ret & BIT(2)) != bit2send)
53 /* wrong direction taken - no such device */
60 static int w1_ds2405_read_pio(struct w1_slave *sl)
62 if (w1_ds2405_select(sl, true))
63 return 0; /* "active" means PIO is low */
65 if (w1_ds2405_select(sl, false))
71 static ssize_t state_show(struct device *device,
72 struct device_attribute *attr, char *buf)
74 struct w1_slave *sl = dev_to_w1_slave(device);
75 struct w1_master *dev = sl->master;
81 ret = mutex_lock_interruptible(&dev->bus_mutex);
85 if (!w1_ds2405_select(sl, false)) {
90 state = w1_read_8(dev);
93 dev_err(device, "non-consistent state %x\n", state);
98 *buf = state ? '1' : '0';
103 mutex_unlock(&dev->bus_mutex);
108 static ssize_t output_show(struct device *device,
109 struct device_attribute *attr, char *buf)
111 struct w1_slave *sl = dev_to_w1_slave(device);
112 struct w1_master *dev = sl->master;
117 ret = mutex_lock_interruptible(&dev->bus_mutex);
121 ret = w1_ds2405_read_pio(sl);
127 *buf = ret ? '1' : '0';
132 mutex_unlock(&dev->bus_mutex);
137 static ssize_t output_store(struct device *device,
138 struct device_attribute *attr,
139 const char *buf, size_t count)
141 struct w1_slave *sl = dev_to_w1_slave(device);
142 struct w1_master *dev = sl->master;
144 int ret, current_pio;
151 if (sscanf(buf, " %u%n", &val, &ret) < 1)
154 if (val != 0 && val != 1)
159 ret = mutex_lock_interruptible(&dev->bus_mutex);
163 current_pio = w1_ds2405_read_pio(sl);
164 if (current_pio < 0) {
165 f_retval = current_pio;
169 if (current_pio == val)
172 if (w1_reset_bus(dev) != 0) {
178 * can't use w1_reset_select_slave() here since it uses Skip ROM if
179 * there is only one device on bus
182 u64 dev_addr = le64_to_cpu(*(u64 *)&sl->reg_num);
185 cmd[0] = W1_MATCH_ROM;
186 memcpy(&cmd[1], &dev_addr, sizeof(dev_addr));
188 w1_write_block(dev, cmd, sizeof(cmd));
193 mutex_unlock(&dev->bus_mutex);
198 static DEVICE_ATTR_RO(state);
199 static DEVICE_ATTR_RW(output);
201 static struct attribute *w1_ds2405_attrs[] = {
202 &dev_attr_state.attr,
203 &dev_attr_output.attr,
207 ATTRIBUTE_GROUPS(w1_ds2405);
209 static const struct w1_family_ops w1_ds2405_fops = {
210 .groups = w1_ds2405_groups
213 static struct w1_family w1_family_ds2405 = {
214 .fid = W1_FAMILY_DS2405,
215 .fops = &w1_ds2405_fops
218 module_w1_family(w1_family_ds2405);