Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / w1 / slaves / w1_ds2405.c
blob1d9a1183e83f52b7264cf497862d1ed71681b339
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * w1_ds2405.c
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>
7 */
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>
17 #include <linux/w1.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);
31 unsigned int bit_ctr;
33 if (w1_reset_bus(dev) != 0)
34 return 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));
44 u8 ret;
46 ret = w1_triplet(dev, bit2send);
48 if ((ret & (BIT(0) | BIT(1))) ==
49 (BIT(0) | BIT(1))) /* no devices found */
50 return 0;
52 if (!!(ret & BIT(2)) != bit2send)
53 /* wrong direction taken - no such device */
54 return 0;
57 return 1;
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))
66 return 1;
68 return -ENODEV;
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;
77 int ret;
78 ssize_t f_retval;
79 u8 state;
81 ret = mutex_lock_interruptible(&dev->bus_mutex);
82 if (ret)
83 return ret;
85 if (!w1_ds2405_select(sl, false)) {
86 f_retval = -ENODEV;
87 goto out_unlock;
90 state = w1_read_8(dev);
91 if (state != 0 &&
92 state != 0xff) {
93 dev_err(device, "non-consistent state %x\n", state);
94 f_retval = -EIO;
95 goto out_unlock;
98 *buf = state ? '1' : '0';
99 f_retval = 1;
101 out_unlock:
102 w1_reset_bus(dev);
103 mutex_unlock(&dev->bus_mutex);
105 return f_retval;
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;
114 int ret;
115 ssize_t f_retval;
117 ret = mutex_lock_interruptible(&dev->bus_mutex);
118 if (ret)
119 return ret;
121 ret = w1_ds2405_read_pio(sl);
122 if (ret < 0) {
123 f_retval = ret;
124 goto out_unlock;
127 *buf = ret ? '1' : '0';
128 f_retval = 1;
130 out_unlock:
131 w1_reset_bus(dev);
132 mutex_unlock(&dev->bus_mutex);
134 return f_retval;
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;
145 unsigned int val;
146 ssize_t f_retval;
148 if (count < 1)
149 return -EINVAL;
151 if (sscanf(buf, " %u%n", &val, &ret) < 1)
152 return -EINVAL;
154 if (val != 0 && val != 1)
155 return -EINVAL;
157 f_retval = ret;
159 ret = mutex_lock_interruptible(&dev->bus_mutex);
160 if (ret)
161 return ret;
163 current_pio = w1_ds2405_read_pio(sl);
164 if (current_pio < 0) {
165 f_retval = current_pio;
166 goto out_unlock;
169 if (current_pio == val)
170 goto out_unlock;
172 if (w1_reset_bus(dev) != 0) {
173 f_retval = -ENODEV;
174 goto out_unlock;
178 * can't use w1_reset_select_slave() here since it uses Skip ROM if
179 * there is only one device on bus
181 do {
182 u64 dev_addr = le64_to_cpu(*(u64 *)&sl->reg_num);
183 u8 cmd[9];
185 cmd[0] = W1_MATCH_ROM;
186 memcpy(&cmd[1], &dev_addr, sizeof(dev_addr));
188 w1_write_block(dev, cmd, sizeof(cmd));
189 } while (0);
191 out_unlock:
192 w1_reset_bus(dev);
193 mutex_unlock(&dev->bus_mutex);
195 return f_retval;
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,
204 NULL
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);