^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * w1_ds2405.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2017 Maciej S. Szmigiero <mail@maciej.szmigiero.name>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Based on w1_therm.c copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/w1.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define W1_FAMILY_DS2405 0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) MODULE_AUTHOR("Maciej S. Szmigiero <mail@maciej.szmigiero.name>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) MODULE_DESCRIPTION("Driver for 1-wire Dallas DS2405 PIO.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2405));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int w1_ds2405_select(struct w1_slave *sl, bool only_active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct w1_master *dev = sl->master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) u64 dev_addr = le64_to_cpu(*(u64 *)&sl->reg_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unsigned int bit_ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (w1_reset_bus(dev) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * We cannot use a normal Match ROM command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * since doing so would toggle PIO state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) w1_write_8(dev, only_active ? W1_ALARM_SEARCH : W1_SEARCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) for (bit_ctr = 0; bit_ctr < 64; bit_ctr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int bit2send = !!(dev_addr & BIT(bit_ctr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) u8 ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) ret = w1_triplet(dev, bit2send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if ((ret & (BIT(0) | BIT(1))) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) (BIT(0) | BIT(1))) /* no devices found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (!!(ret & BIT(2)) != bit2send)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* wrong direction taken - no such device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static int w1_ds2405_read_pio(struct w1_slave *sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (w1_ds2405_select(sl, true))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return 0; /* "active" means PIO is low */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (w1_ds2405_select(sl, false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static ssize_t state_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct w1_slave *sl = dev_to_w1_slave(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct w1_master *dev = sl->master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) ssize_t f_retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) u8 state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) ret = mutex_lock_interruptible(&dev->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!w1_ds2405_select(sl, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) f_retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) state = w1_read_8(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (state != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) state != 0xff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) dev_err(device, "non-consistent state %x\n", state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) f_retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) *buf = state ? '1' : '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) f_retval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) w1_reset_bus(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) mutex_unlock(&dev->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return f_retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static ssize_t output_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct w1_slave *sl = dev_to_w1_slave(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct w1_master *dev = sl->master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) ssize_t f_retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ret = mutex_lock_interruptible(&dev->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ret = w1_ds2405_read_pio(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) f_retval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) *buf = ret ? '1' : '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) f_retval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) w1_reset_bus(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) mutex_unlock(&dev->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return f_retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static ssize_t output_store(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct w1_slave *sl = dev_to_w1_slave(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct w1_master *dev = sl->master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int ret, current_pio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) ssize_t f_retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (count < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (sscanf(buf, " %u%n", &val, &ret) < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (val != 0 && val != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) f_retval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) ret = mutex_lock_interruptible(&dev->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) current_pio = w1_ds2405_read_pio(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (current_pio < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) f_retval = current_pio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (current_pio == val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (w1_reset_bus(dev) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) f_retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * can't use w1_reset_select_slave() here since it uses Skip ROM if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * there is only one device on bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) u64 dev_addr = le64_to_cpu(*(u64 *)&sl->reg_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) u8 cmd[9];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) cmd[0] = W1_MATCH_ROM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) memcpy(&cmd[1], &dev_addr, sizeof(dev_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) w1_write_block(dev, cmd, sizeof(cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) } while (0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) w1_reset_bus(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) mutex_unlock(&dev->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return f_retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static DEVICE_ATTR_RO(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static DEVICE_ATTR_RW(output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static struct attribute *w1_ds2405_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) &dev_attr_state.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) &dev_attr_output.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) ATTRIBUTE_GROUPS(w1_ds2405);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static const struct w1_family_ops w1_ds2405_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) .groups = w1_ds2405_groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static struct w1_family w1_family_ds2405 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) .fid = W1_FAMILY_DS2405,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) .fops = &w1_ds2405_fops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) module_w1_family(w1_family_ds2405);