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>
29 #define W1_FAMILY_DS2405 0x05
31 MODULE_LICENSE("GPL");
33 MODULE_DESCRIPTION("Driver for 1-wire Dallas DS2405 PIO.");
34 MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2405));
36 static int w1_ds2405_select(struct w1_slave *sl, bool only_active)
38 struct w1_master *dev = sl->master;
40 u64 dev_addr = le64_to_cpu(*(u64 *)&sl->reg_num);
43 if (w1_reset_bus(dev) != 0)
47 * We cannot use a normal Match ROM command
48 * since doing so would toggle PIO state
50 w1_write_8(dev, only_active ? W1_ALARM_SEARCH : W1_SEARCH);
52 for (bit_ctr = 0; bit_ctr < 64; bit_ctr++) {
53 int bit2send = !!(dev_addr & BIT(bit_ctr));
56 ret = w1_triplet(dev, bit2send);
58 if ((ret & (BIT(0) | BIT(1))) ==
59 (BIT(0) | BIT(1))) /* no devices found */
62 if (!!(ret & BIT(2)) != bit2send)
63 /* wrong direction taken - no such device */
70 static int w1_ds2405_read_pio(struct w1_slave *sl)
72 if (w1_ds2405_select(sl, true))
73 return 0; /* "active" means PIO is low */
75 if (w1_ds2405_select(sl, false))
81 static ssize_t state_show(struct device *device,
82 struct device_attribute *attr, char *buf)
84 struct w1_slave *sl = dev_to_w1_slave(device);
85 struct w1_master *dev = sl->master;
91 ret = mutex_lock_interruptible(&dev->bus_mutex);
95 if (!w1_ds2405_select(sl, false)) {
100 state = w1_read_8(dev);
103 dev_err(device, "non-consistent state %x\n", state);
108 *buf = state ? '1' : '0';
113 mutex_unlock(&dev->bus_mutex);
118 static ssize_t output_show(struct device *device,
119 struct device_attribute *attr, char *buf)
121 struct w1_slave *sl = dev_to_w1_slave(device);
122 struct w1_master *dev = sl->master;
127 ret = mutex_lock_interruptible(&dev->bus_mutex);
131 ret = w1_ds2405_read_pio(sl);
137 *buf = ret ? '1' : '0';
142 mutex_unlock(&dev->bus_mutex);
147 static ssize_t output_store(struct device *device,
148 struct device_attribute *attr,
149 const char *buf, size_t count)
151 struct w1_slave *sl = dev_to_w1_slave(device);
152 struct w1_master *dev = sl->master;
154 int ret, current_pio;
161 if (sscanf(buf, " %u%n", &val, &ret) < 1)
164 if (val != 0 && val != 1)
169 ret = mutex_lock_interruptible(&dev->bus_mutex);
173 current_pio = w1_ds2405_read_pio(sl);
174 if (current_pio < 0) {
175 f_retval = current_pio;
179 if (current_pio == val)
182 if (w1_reset_bus(dev) != 0) {
188 * can't use w1_reset_select_slave() here since it uses Skip ROM if
189 * there is only one device on bus
192 u64 dev_addr = le64_to_cpu(*(u64 *)&sl->reg_num);
195 cmd[0] = W1_MATCH_ROM;
196 memcpy(&cmd[1], &dev_addr, sizeof(dev_addr));
198 w1_write_block(dev, cmd, sizeof(cmd));
203 mutex_unlock(&dev->bus_mutex);
208 static DEVICE_ATTR_RO(state);
209 static DEVICE_ATTR_RW(output);
211 static struct attribute *w1_ds2405_attrs[] = {
212 &dev_attr_state.attr,
213 &dev_attr_output.attr,
217 ATTRIBUTE_GROUPS(w1_ds2405);
219 static struct w1_family_ops w1_ds2405_fops = {
220 .groups = w1_ds2405_groups
223 static struct w1_family w1_family_ds2405 = {
224 .fid = W1_FAMILY_DS2405,
225 .fops = &w1_ds2405_fops
228 module_w1_family(w1_family_ds2405);