Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *	w1_ds2408.c - w1 family 29 (DS2408) driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2010 Jean-Francois Dagenais <dagenaisj@sonatest.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/w1.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define W1_FAMILY_DS2408	0x29
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define W1_F29_RETRIES		3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define W1_F29_REG_LOGIG_STATE             0x88 /* R */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define W1_F29_REG_OUTPUT_LATCH_STATE      0x89 /* R */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define W1_F29_REG_ACTIVITY_LATCH_STATE    0x8A /* R */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define W1_F29_REG_COND_SEARCH_SELECT_MASK 0x8B /* RW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define W1_F29_REG_COND_SEARCH_POL_SELECT  0x8C /* RW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define W1_F29_REG_CONTROL_AND_STATUS      0x8D /* RW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define W1_F29_FUNC_READ_PIO_REGS          0xF0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define W1_F29_FUNC_CHANN_ACCESS_READ      0xF5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define W1_F29_FUNC_CHANN_ACCESS_WRITE     0x5A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) /* also used to write the control/status reg (0x8D): */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define W1_F29_FUNC_WRITE_COND_SEARCH_REG  0xCC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define W1_F29_FUNC_RESET_ACTIVITY_LATCHES 0xC3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define W1_F29_SUCCESS_CONFIRM_BYTE        0xAA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static int _read_reg(struct w1_slave *sl, u8 address, unsigned char* buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	u8 wrbuf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	dev_dbg(&sl->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 			"Reading with slave: %p, reg addr: %0#4x, buff addr: %p",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 			sl, (unsigned int)address, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	mutex_lock(&sl->master->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	dev_dbg(&sl->dev, "mutex locked");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (w1_reset_select_slave(sl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		mutex_unlock(&sl->master->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	wrbuf[0] = W1_F29_FUNC_READ_PIO_REGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	wrbuf[1] = address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	wrbuf[2] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	w1_write_block(sl->master, wrbuf, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	*buf = w1_read_8(sl->master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	mutex_unlock(&sl->master->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	dev_dbg(&sl->dev, "mutex unlocked");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static ssize_t state_read(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			  struct bin_attribute *bin_attr, char *buf, loff_t off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			  size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	dev_dbg(&kobj_to_w1_slave(kobj)->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		"Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		bin_attr->attr.name, kobj, (unsigned int)off, count, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (count != 1 || off != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	return _read_reg(kobj_to_w1_slave(kobj), W1_F29_REG_LOGIG_STATE, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static ssize_t output_read(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			   struct bin_attribute *bin_attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			   loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	dev_dbg(&kobj_to_w1_slave(kobj)->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		"Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		bin_attr->attr.name, kobj, (unsigned int)off, count, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (count != 1 || off != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return _read_reg(kobj_to_w1_slave(kobj),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 					 W1_F29_REG_OUTPUT_LATCH_STATE, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static ssize_t activity_read(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			     struct bin_attribute *bin_attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			     loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	dev_dbg(&kobj_to_w1_slave(kobj)->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		"Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		bin_attr->attr.name, kobj, (unsigned int)off, count, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (count != 1 || off != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	return _read_reg(kobj_to_w1_slave(kobj),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 					 W1_F29_REG_ACTIVITY_LATCH_STATE, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static ssize_t cond_search_mask_read(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 				     struct bin_attribute *bin_attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 				     loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	dev_dbg(&kobj_to_w1_slave(kobj)->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		"Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		bin_attr->attr.name, kobj, (unsigned int)off, count, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (count != 1 || off != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return _read_reg(kobj_to_w1_slave(kobj),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		W1_F29_REG_COND_SEARCH_SELECT_MASK, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static ssize_t cond_search_polarity_read(struct file *filp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 					 struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 					 struct bin_attribute *bin_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 					 char *buf, loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (count != 1 || off != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	return _read_reg(kobj_to_w1_slave(kobj),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		W1_F29_REG_COND_SEARCH_POL_SELECT, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static ssize_t status_control_read(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 				   struct bin_attribute *bin_attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 				   loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (count != 1 || off != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	return _read_reg(kobj_to_w1_slave(kobj),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		W1_F29_REG_CONTROL_AND_STATUS, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) #ifdef CONFIG_W1_SLAVE_DS2408_READBACK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static bool optional_read_back_valid(struct w1_slave *sl, u8 expected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	u8 w1_buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (w1_reset_resume_command(sl->master))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	w1_buf[0] = W1_F29_FUNC_READ_PIO_REGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	w1_buf[1] = W1_F29_REG_OUTPUT_LATCH_STATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	w1_buf[2] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	w1_write_block(sl->master, w1_buf, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return (w1_read_8(sl->master) == expected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static bool optional_read_back_valid(struct w1_slave *sl, u8 expected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static ssize_t output_write(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			    struct bin_attribute *bin_attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			    loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	struct w1_slave *sl = kobj_to_w1_slave(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	u8 w1_buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	unsigned int retries = W1_F29_RETRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	ssize_t bytes_written = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (count != 1 || off != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	dev_dbg(&sl->dev, "locking mutex for write_output");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	mutex_lock(&sl->master->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	dev_dbg(&sl->dev, "mutex locked");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (w1_reset_select_slave(sl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		goto out;
^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) 		w1_buf[0] = W1_F29_FUNC_CHANN_ACCESS_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		w1_buf[1] = *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		w1_buf[2] = ~(*buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		w1_write_block(sl->master, w1_buf, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		if (w1_read_8(sl->master) == W1_F29_SUCCESS_CONFIRM_BYTE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		    optional_read_back_valid(sl, *buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			bytes_written = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		if (w1_reset_resume_command(sl->master))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			goto out; /* unrecoverable error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		/* try again, the slave is ready for a command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	} while (--retries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	mutex_unlock(&sl->master->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	dev_dbg(&sl->dev, "%s, mutex unlocked retries:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		(bytes_written > 0) ? "succeeded" : "error", retries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	return bytes_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)  * Writing to the activity file resets the activity latches.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static ssize_t activity_write(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			      struct bin_attribute *bin_attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			      loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	struct w1_slave *sl = kobj_to_w1_slave(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	unsigned int retries = W1_F29_RETRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	if (count != 1 || off != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	mutex_lock(&sl->master->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (w1_reset_select_slave(sl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	while (retries--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		w1_write_8(sl->master, W1_F29_FUNC_RESET_ACTIVITY_LATCHES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		if (w1_read_8(sl->master) == W1_F29_SUCCESS_CONFIRM_BYTE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			mutex_unlock(&sl->master->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		if (w1_reset_resume_command(sl->master))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	mutex_unlock(&sl->master->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static ssize_t status_control_write(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 				    struct bin_attribute *bin_attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 				    loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	struct w1_slave *sl = kobj_to_w1_slave(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	u8 w1_buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	unsigned int retries = W1_F29_RETRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	if (count != 1 || off != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	mutex_lock(&sl->master->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (w1_reset_select_slave(sl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	while (retries--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		w1_buf[0] = W1_F29_FUNC_WRITE_COND_SEARCH_REG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		w1_buf[1] = W1_F29_REG_CONTROL_AND_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		w1_buf[2] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		w1_buf[3] = *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		w1_write_block(sl->master, w1_buf, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		if (w1_reset_resume_command(sl->master))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		w1_buf[0] = W1_F29_FUNC_READ_PIO_REGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		w1_buf[1] = W1_F29_REG_CONTROL_AND_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		w1_buf[2] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		w1_write_block(sl->master, w1_buf, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		if (w1_read_8(sl->master) == *buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			/* success! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			mutex_unlock(&sl->master->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	mutex_unlock(&sl->master->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)  * This is a special sequence we must do to ensure the P0 output is not stuck
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)  * in test mode. This is described in rev 2 of the ds2408's datasheet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)  * (http://datasheets.maximintegrated.com/en/ds/DS2408.pdf) under
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)  * "APPLICATION INFORMATION/Power-up timing".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static int w1_f29_disable_test_mode(struct w1_slave *sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	u8 magic[10] = {0x96, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	u64 rn = le64_to_cpu(*((u64*)&sl->reg_num));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	memcpy(&magic[1], &rn, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	magic[9] = 0x3C;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	mutex_lock(&sl->master->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	res = w1_reset_bus(sl->master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	w1_write_block(sl->master, magic, ARRAY_SIZE(magic));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	res = w1_reset_bus(sl->master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	mutex_unlock(&sl->master->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static BIN_ATTR_RO(state, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static BIN_ATTR_RW(output, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static BIN_ATTR_RW(activity, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static BIN_ATTR_RO(cond_search_mask, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static BIN_ATTR_RO(cond_search_polarity, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static BIN_ATTR_RW(status_control, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static struct bin_attribute *w1_f29_bin_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	&bin_attr_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	&bin_attr_output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	&bin_attr_activity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	&bin_attr_cond_search_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	&bin_attr_cond_search_polarity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	&bin_attr_status_control,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static const struct attribute_group w1_f29_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	.bin_attrs = w1_f29_bin_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static const struct attribute_group *w1_f29_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	&w1_f29_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static const struct w1_family_ops w1_f29_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	.add_slave      = w1_f29_disable_test_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	.groups		= w1_f29_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static struct w1_family w1_family_29 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	.fid = W1_FAMILY_DS2408,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	.fops = &w1_f29_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) module_w1_family(w1_family_29);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) MODULE_AUTHOR("Jean-Francois Dagenais <dagenaisj@sonatest.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) MODULE_DESCRIPTION("w1 family 29 driver for DS2408 8 Pin IO");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2408));