Commit | Line | Data |
---|---|---|
40b0b3f8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
31b4ca3e MB |
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> | |
31b4ca3e MB |
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 | ||
de0d6dbd AD |
17 | #include <linux/w1.h> |
18 | ||
19 | #define W1_FAMILY_DS2413 0x3A | |
31b4ca3e | 20 | |
31b4ca3e MB |
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 | |
3856032a | 25 | #define W1_F3A_INVALID_PIO_STATE 0xFF |
31b4ca3e | 26 | |
729fb9d3 GKH |
27 | static ssize_t state_read(struct file *filp, struct kobject *kobj, |
28 | struct bin_attribute *bin_attr, char *buf, loff_t off, | |
29 | size_t count) | |
31b4ca3e MB |
30 | { |
31 | struct w1_slave *sl = kobj_to_w1_slave(kobj); | |
c50d09a8 MB |
32 | unsigned int retries = W1_F3A_RETRIES; |
33 | ssize_t bytes_read = -EIO; | |
aacd152e | 34 | u8 state; |
c50d09a8 | 35 | |
31b4ca3e MB |
36 | dev_dbg(&sl->dev, |
37 | "Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p", | |
38 | bin_attr->attr.name, kobj, (unsigned int)off, count, buf); | |
39 | ||
40 | if (off != 0) | |
41 | return 0; | |
42 | if (!buf) | |
43 | return -EINVAL; | |
44 | ||
45 | mutex_lock(&sl->master->bus_mutex); | |
46 | dev_dbg(&sl->dev, "mutex locked"); | |
47 | ||
3856032a | 48 | next: |
c50d09a8 MB |
49 | if (w1_reset_select_slave(sl)) |
50 | goto out; | |
31b4ca3e | 51 | |
c50d09a8 MB |
52 | while (retries--) { |
53 | w1_write_8(sl->master, W1_F3A_FUNC_PIO_ACCESS_READ); | |
31b4ca3e | 54 | |
aacd152e MB |
55 | state = w1_read_8(sl->master); |
56 | if ((state & 0x0F) == ((~state >> 4) & 0x0F)) { | |
3856032a | 57 | /* complement is correct */ |
aacd152e | 58 | *buf = state; |
c50d09a8 MB |
59 | bytes_read = 1; |
60 | goto out; | |
aacd152e | 61 | } else if (state == W1_F3A_INVALID_PIO_STATE) { |
3856032a MB |
62 | /* slave didn't respond, try to select it again */ |
63 | dev_warn(&sl->dev, "slave device did not respond to PIO_ACCESS_READ, " \ | |
64 | "reselecting, retries left: %d\n", retries); | |
65 | goto next; | |
c50d09a8 MB |
66 | } |
67 | ||
68 | if (w1_reset_resume_command(sl->master)) | |
69 | goto out; /* unrecoverable error */ | |
31b4ca3e | 70 | |
c50d09a8 MB |
71 | dev_warn(&sl->dev, "PIO_ACCESS_READ error, retries left: %d\n", retries); |
72 | } | |
73 | ||
74 | out: | |
75 | mutex_unlock(&sl->master->bus_mutex); | |
76 | dev_dbg(&sl->dev, "%s, mutex unlocked, retries: %d\n", | |
77 | (bytes_read > 0) ? "succeeded" : "error", retries); | |
78 | return bytes_read; | |
31b4ca3e MB |
79 | } |
80 | ||
729fb9d3 GKH |
81 | static BIN_ATTR_RO(state, 1); |
82 | ||
83 | static ssize_t output_write(struct file *filp, struct kobject *kobj, | |
84 | struct bin_attribute *bin_attr, char *buf, | |
85 | loff_t off, size_t count) | |
31b4ca3e MB |
86 | { |
87 | struct w1_slave *sl = kobj_to_w1_slave(kobj); | |
88 | u8 w1_buf[3]; | |
89 | unsigned int retries = W1_F3A_RETRIES; | |
ae2ee27a | 90 | ssize_t bytes_written = -EIO; |
31b4ca3e MB |
91 | |
92 | if (count != 1 || off != 0) | |
93 | return -EFAULT; | |
94 | ||
95 | dev_dbg(&sl->dev, "locking mutex for write_output"); | |
96 | mutex_lock(&sl->master->bus_mutex); | |
97 | dev_dbg(&sl->dev, "mutex locked"); | |
98 | ||
99 | if (w1_reset_select_slave(sl)) | |
ae2ee27a | 100 | goto out; |
31b4ca3e | 101 | |
ad9c36be KK |
102 | /* |
103 | * according to the DS2413 datasheet the most significant 6 bits | |
104 | * should be set to "1"s, so do it now | |
105 | */ | |
31b4ca3e MB |
106 | *buf = *buf | 0xFC; |
107 | ||
108 | while (retries--) { | |
109 | w1_buf[0] = W1_F3A_FUNC_PIO_ACCESS_WRITE; | |
110 | w1_buf[1] = *buf; | |
111 | w1_buf[2] = ~(*buf); | |
112 | w1_write_block(sl->master, w1_buf, 3); | |
113 | ||
114 | if (w1_read_8(sl->master) == W1_F3A_SUCCESS_CONFIRM_BYTE) { | |
ae2ee27a MB |
115 | bytes_written = 1; |
116 | goto out; | |
31b4ca3e MB |
117 | } |
118 | if (w1_reset_resume_command(sl->master)) | |
ae2ee27a MB |
119 | goto out; /* unrecoverable error */ |
120 | ||
121 | dev_warn(&sl->dev, "PIO_ACCESS_WRITE error, retries left: %d\n", retries); | |
31b4ca3e MB |
122 | } |
123 | ||
ae2ee27a | 124 | out: |
31b4ca3e | 125 | mutex_unlock(&sl->master->bus_mutex); |
ae2ee27a MB |
126 | dev_dbg(&sl->dev, "%s, mutex unlocked, retries: %d\n", |
127 | (bytes_written > 0) ? "succeeded" : "error", retries); | |
128 | return bytes_written; | |
31b4ca3e MB |
129 | } |
130 | ||
921e0f2f | 131 | static BIN_ATTR(output, 0664, NULL, output_write, 1); |
729fb9d3 GKH |
132 | |
133 | static struct bin_attribute *w1_f3a_bin_attrs[] = { | |
134 | &bin_attr_state, | |
135 | &bin_attr_output, | |
136 | NULL, | |
31b4ca3e MB |
137 | }; |
138 | ||
729fb9d3 GKH |
139 | static const struct attribute_group w1_f3a_group = { |
140 | .bin_attrs = w1_f3a_bin_attrs, | |
141 | }; | |
31b4ca3e | 142 | |
729fb9d3 GKH |
143 | static const struct attribute_group *w1_f3a_groups[] = { |
144 | &w1_f3a_group, | |
145 | NULL, | |
146 | }; | |
31b4ca3e | 147 | |
57de2dfc | 148 | static const struct w1_family_ops w1_f3a_fops = { |
729fb9d3 | 149 | .groups = w1_f3a_groups, |
31b4ca3e MB |
150 | }; |
151 | ||
152 | static struct w1_family w1_family_3a = { | |
153 | .fid = W1_FAMILY_DS2413, | |
154 | .fops = &w1_f3a_fops, | |
155 | }; | |
939fc832 | 156 | module_w1_family(w1_family_3a); |
50fa2951 AD |
157 | |
158 | MODULE_AUTHOR("Mariusz Bialonczyk <manio@skyboo.net>"); | |
159 | MODULE_DESCRIPTION("w1 family 3a driver for DS2413 2 Pin IO"); | |
160 | MODULE_LICENSE("GPL"); | |
161 | MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2413)); |