1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * w1_ds2413.c - w1 family 3a (DS2413) driver 4 * based on w1_ds2408.c by Jean-Francois Dagenais <dagenaisj@sonatest.com> 5 * 6 * Copyright (c) 2013 Mariusz Bialonczyk <manio@skyboo.net> 7 */ 8 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/moduleparam.h> 12 #include <linux/device.h> 13 #include <linux/types.h> 14 #include <linux/delay.h> 15 #include <linux/slab.h> 16 17 #include <linux/w1.h> 18 19 #define W1_FAMILY_DS2413 0x3A 20 21 #define W1_F3A_RETRIES 3 22 #define W1_F3A_FUNC_PIO_ACCESS_READ 0xF5 23 #define W1_F3A_FUNC_PIO_ACCESS_WRITE 0x5A 24 #define W1_F3A_SUCCESS_CONFIRM_BYTE 0xAA 25 26 static ssize_t state_read(struct file *filp, struct kobject *kobj, 27 struct bin_attribute *bin_attr, char *buf, loff_t off, 28 size_t count) 29 { 30 struct w1_slave *sl = kobj_to_w1_slave(kobj); 31 dev_dbg(&sl->dev, 32 "Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p", 33 bin_attr->attr.name, kobj, (unsigned int)off, count, buf); 34 35 if (off != 0) 36 return 0; 37 if (!buf) 38 return -EINVAL; 39 40 mutex_lock(&sl->master->bus_mutex); 41 dev_dbg(&sl->dev, "mutex locked"); 42 43 if (w1_reset_select_slave(sl)) { 44 mutex_unlock(&sl->master->bus_mutex); 45 return -EIO; 46 } 47 48 w1_write_8(sl->master, W1_F3A_FUNC_PIO_ACCESS_READ); 49 *buf = w1_read_8(sl->master); 50 51 mutex_unlock(&sl->master->bus_mutex); 52 dev_dbg(&sl->dev, "mutex unlocked"); 53 54 /* check for correct complement */ 55 if ((*buf & 0x0F) != ((~*buf >> 4) & 0x0F)) 56 return -EIO; 57 else 58 return 1; 59 } 60 61 static BIN_ATTR_RO(state, 1); 62 63 static ssize_t output_write(struct file *filp, struct kobject *kobj, 64 struct bin_attribute *bin_attr, char *buf, 65 loff_t off, size_t count) 66 { 67 struct w1_slave *sl = kobj_to_w1_slave(kobj); 68 u8 w1_buf[3]; 69 unsigned int retries = W1_F3A_RETRIES; 70 71 if (count != 1 || off != 0) 72 return -EFAULT; 73 74 dev_dbg(&sl->dev, "locking mutex for write_output"); 75 mutex_lock(&sl->master->bus_mutex); 76 dev_dbg(&sl->dev, "mutex locked"); 77 78 if (w1_reset_select_slave(sl)) 79 goto error; 80 81 /* according to the DS2413 datasheet the most significant 6 bits 82 should be set to "1"s, so do it now */ 83 *buf = *buf | 0xFC; 84 85 while (retries--) { 86 w1_buf[0] = W1_F3A_FUNC_PIO_ACCESS_WRITE; 87 w1_buf[1] = *buf; 88 w1_buf[2] = ~(*buf); 89 w1_write_block(sl->master, w1_buf, 3); 90 91 if (w1_read_8(sl->master) == W1_F3A_SUCCESS_CONFIRM_BYTE) { 92 mutex_unlock(&sl->master->bus_mutex); 93 dev_dbg(&sl->dev, "mutex unlocked, retries:%d", retries); 94 return 1; 95 } 96 if (w1_reset_resume_command(sl->master)) 97 goto error; 98 } 99 100 error: 101 mutex_unlock(&sl->master->bus_mutex); 102 dev_dbg(&sl->dev, "mutex unlocked in error, retries:%d", retries); 103 return -EIO; 104 } 105 106 static BIN_ATTR(output, S_IRUGO | S_IWUSR | S_IWGRP, NULL, output_write, 1); 107 108 static struct bin_attribute *w1_f3a_bin_attrs[] = { 109 &bin_attr_state, 110 &bin_attr_output, 111 NULL, 112 }; 113 114 static const struct attribute_group w1_f3a_group = { 115 .bin_attrs = w1_f3a_bin_attrs, 116 }; 117 118 static const struct attribute_group *w1_f3a_groups[] = { 119 &w1_f3a_group, 120 NULL, 121 }; 122 123 static struct w1_family_ops w1_f3a_fops = { 124 .groups = w1_f3a_groups, 125 }; 126 127 static struct w1_family w1_family_3a = { 128 .fid = W1_FAMILY_DS2413, 129 .fops = &w1_f3a_fops, 130 }; 131 module_w1_family(w1_family_3a); 132 133 MODULE_AUTHOR("Mariusz Bialonczyk <manio@skyboo.net>"); 134 MODULE_DESCRIPTION("w1 family 3a driver for DS2413 2 Pin IO"); 135 MODULE_LICENSE("GPL"); 136 MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2413)); 137