2 * w1_ds2805 - w1 family 0d (DS28E05) driver
4 * Copyright (c) 2016 Andrew Worsley amworsley@gmail.com
6 * This source code is licensed under the GNU General Public License,
7 * Version 2. See the file COPYING for more details.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/device.h>
14 #include <linux/types.h>
15 #include <linux/delay.h>
19 #define W1_EEPROM_DS2805 0x0D
21 #define W1_F0D_EEPROM_SIZE 128
22 #define W1_F0D_PAGE_BITS 3
23 #define W1_F0D_PAGE_SIZE (1<<W1_F0D_PAGE_BITS)
24 #define W1_F0D_PAGE_MASK 0x0F
26 #define W1_F0D_SCRATCH_BITS 1
27 #define W1_F0D_SCRATCH_SIZE (1<<W1_F0D_SCRATCH_BITS)
28 #define W1_F0D_SCRATCH_MASK (W1_F0D_SCRATCH_SIZE-1)
30 #define W1_F0D_READ_EEPROM 0xF0
31 #define W1_F0D_WRITE_EEPROM 0x55
32 #define W1_F0D_RELEASE 0xFF
34 #define W1_F0D_CS_OK 0xAA /* Chip Status Ok */
36 #define W1_F0D_TPROG_MS 16
38 #define W1_F0D_READ_RETRIES 10
39 #define W1_F0D_READ_MAXLEN W1_F0D_EEPROM_SIZE
42 * Check the file size bounds and adjusts count as needed.
43 * This would not be needed if the file size didn't reset to 0 after a write.
45 static inline size_t w1_f0d_fix_count(loff_t off
, size_t count
, size_t size
)
50 if ((off
+ count
) > size
)
57 * Read a block from W1 ROM two times and compares the results.
58 * If they are equal they are returned, otherwise the read
59 * is repeated W1_F0D_READ_RETRIES times.
61 * count must not exceed W1_F0D_READ_MAXLEN.
63 static int w1_f0d_readblock(struct w1_slave
*sl
, int off
, int count
, char *buf
)
66 u8 cmp
[W1_F0D_READ_MAXLEN
];
67 int tries
= W1_F0D_READ_RETRIES
;
70 wrbuf
[0] = W1_F0D_READ_EEPROM
;
71 wrbuf
[1] = off
& 0x7f;
74 if (w1_reset_select_slave(sl
))
77 w1_write_block(sl
->master
, wrbuf
, sizeof(wrbuf
));
78 w1_read_block(sl
->master
, buf
, count
);
80 if (w1_reset_select_slave(sl
))
83 w1_write_block(sl
->master
, wrbuf
, sizeof(wrbuf
));
84 w1_read_block(sl
->master
, cmp
, count
);
86 if (!memcmp(cmp
, buf
, count
))
90 dev_err(&sl
->dev
, "proof reading failed %d times\n",
96 static ssize_t
w1_f0d_read_bin(struct file
*filp
, struct kobject
*kobj
,
97 struct bin_attribute
*bin_attr
,
98 char *buf
, loff_t off
, size_t count
)
100 struct w1_slave
*sl
= kobj_to_w1_slave(kobj
);
103 count
= w1_f0d_fix_count(off
, count
, W1_F0D_EEPROM_SIZE
);
107 mutex_lock(&sl
->master
->mutex
);
109 /* read directly from the EEPROM in chunks of W1_F0D_READ_MAXLEN */
113 if (todo
>= W1_F0D_READ_MAXLEN
)
114 block_read
= W1_F0D_READ_MAXLEN
;
118 if (w1_f0d_readblock(sl
, off
, block_read
, buf
) < 0) {
123 todo
-= W1_F0D_READ_MAXLEN
;
124 buf
+= W1_F0D_READ_MAXLEN
;
125 off
+= W1_F0D_READ_MAXLEN
;
128 mutex_unlock(&sl
->master
->mutex
);
134 * Writes to the scratchpad and reads it back for verification.
135 * Then copies the scratchpad to EEPROM.
136 * The data must be aligned at W1_F0D_SCRATCH_SIZE bytes and
137 * must be W1_F0D_SCRATCH_SIZE bytes long.
138 * The master must be locked.
140 * @param sl The slave structure
141 * @param addr Address for the write
142 * @param len length must be <= (W1_F0D_PAGE_SIZE - (addr & W1_F0D_PAGE_MASK))
143 * @param data The data to write
144 * @return 0=Success -1=failure
146 static int w1_f0d_write(struct w1_slave
*sl
, int addr
, int len
, const u8
*data
)
148 int tries
= W1_F0D_READ_RETRIES
;
150 u8 rdbuf
[W1_F0D_SCRATCH_SIZE
];
153 if ((addr
& 1) || (len
!= 2)) {
154 dev_err(&sl
->dev
, "%s: bad addr/len - addr=%#x len=%d\n",
155 __func__
, addr
, len
);
161 /* Write the data to the scratchpad */
162 if (w1_reset_select_slave(sl
))
165 wrbuf
[0] = W1_F0D_WRITE_EEPROM
;
166 wrbuf
[1] = addr
& 0xff;
167 wrbuf
[2] = 0xff; /* ?? from Example */
169 w1_write_block(sl
->master
, wrbuf
, sizeof(wrbuf
));
170 w1_write_block(sl
->master
, data
, len
);
172 w1_read_block(sl
->master
, rdbuf
, sizeof(rdbuf
));
173 /* Compare what was read against the data written */
174 if ((rdbuf
[0] != data
[0]) || (rdbuf
[1] != data
[1])) {
180 "could not write to eeprom, scratchpad compare failed %d times\n",
181 W1_F0D_READ_RETRIES
);
182 pr_info("%s: rdbuf = %#x %#x data = %#x %#x\n",
183 __func__
, rdbuf
[0], rdbuf
[1], data
[0], data
[1]);
188 /* Trigger write out to EEPROM */
189 w1_write_8(sl
->master
, W1_F0D_RELEASE
);
191 /* Sleep for tprog ms to wait for the write to complete */
192 msleep(W1_F0D_TPROG_MS
);
194 /* Check CS (Command Status) == 0xAA ? */
195 cs
= w1_read_8(sl
->master
);
196 if (cs
!= W1_F0D_CS_OK
) {
197 dev_err(&sl
->dev
, "save to eeprom failed = CS=%#x\n", cs
);
204 static ssize_t
w1_f0d_write_bin(struct file
*filp
, struct kobject
*kobj
,
205 struct bin_attribute
*bin_attr
,
206 char *buf
, loff_t off
, size_t count
)
208 struct w1_slave
*sl
= kobj_to_w1_slave(kobj
);
212 count
= w1_f0d_fix_count(off
, count
, W1_F0D_EEPROM_SIZE
);
216 mutex_lock(&sl
->master
->mutex
);
218 /* Can only write data in blocks of the size of the scratchpad */
223 /* if len too short or addr not aligned */
224 if (len
< W1_F0D_SCRATCH_SIZE
|| addr
& W1_F0D_SCRATCH_MASK
) {
225 char tmp
[W1_F0D_SCRATCH_SIZE
];
227 /* read the block and update the parts to be written */
228 if (w1_f0d_readblock(sl
, addr
& ~W1_F0D_SCRATCH_MASK
,
229 W1_F0D_SCRATCH_SIZE
, tmp
)) {
234 /* copy at most to the boundary of the PAGE or len */
235 copy
= W1_F0D_SCRATCH_SIZE
-
236 (addr
& W1_F0D_SCRATCH_MASK
);
241 memcpy(&tmp
[addr
& W1_F0D_SCRATCH_MASK
], buf
, copy
);
242 if (w1_f0d_write(sl
, addr
& ~W1_F0D_SCRATCH_MASK
,
243 W1_F0D_SCRATCH_SIZE
, tmp
) < 0) {
249 copy
= W1_F0D_SCRATCH_SIZE
;
250 if (w1_f0d_write(sl
, addr
, copy
, buf
) < 0) {
261 mutex_unlock(&sl
->master
->mutex
);
266 static struct bin_attribute w1_f0d_bin_attr
= {
269 .mode
= S_IRUGO
| S_IWUSR
,
271 .size
= W1_F0D_EEPROM_SIZE
,
272 .read
= w1_f0d_read_bin
,
273 .write
= w1_f0d_write_bin
,
276 static int w1_f0d_add_slave(struct w1_slave
*sl
)
278 return sysfs_create_bin_file(&sl
->dev
.kobj
, &w1_f0d_bin_attr
);
281 static void w1_f0d_remove_slave(struct w1_slave
*sl
)
283 sysfs_remove_bin_file(&sl
->dev
.kobj
, &w1_f0d_bin_attr
);
286 static struct w1_family_ops w1_f0d_fops
= {
287 .add_slave
= w1_f0d_add_slave
,
288 .remove_slave
= w1_f0d_remove_slave
,
291 static struct w1_family w1_family_2d
= {
292 .fid
= W1_EEPROM_DS2805
,
293 .fops
= &w1_f0d_fops
,
296 static int __init
w1_f0d_init(void)
298 pr_info("%s()\n", __func__
);
299 return w1_register_family(&w1_family_2d
);
302 static void __exit
w1_f0d_fini(void)
304 pr_info("%s()\n", __func__
);
305 w1_unregister_family(&w1_family_2d
);
308 module_init(w1_f0d_init
);
309 module_exit(w1_f0d_fini
);
311 MODULE_LICENSE("GPL");
312 MODULE_AUTHOR("Andrew Worsley amworsley@gmail.com");
313 MODULE_DESCRIPTION("w1 family 0d driver for DS2805, 1kb EEPROM");