2 * w1_ds2431.c - w1 family 2d (DS2431) driver
4 * Copyright (c) 2008 Bernhard Weirich <bernhard.weirich@riedel.net>
6 * Heavily inspired by w1_DS2433 driver from Ben Gardner <bgardner@wabtec.com>
8 * This source code is licensed under the GNU General Public License,
9 * Version 2. See the file COPYING for more details.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/device.h>
16 #include <linux/types.h>
17 #include <linux/delay.h>
20 #include "../w1_int.h"
21 #include "../w1_family.h"
23 #define W1_F2D_EEPROM_SIZE 128
24 #define W1_F2D_PAGE_COUNT 4
25 #define W1_F2D_PAGE_BITS 5
26 #define W1_F2D_PAGE_SIZE (1<<W1_F2D_PAGE_BITS)
27 #define W1_F2D_PAGE_MASK 0x1F
29 #define W1_F2D_SCRATCH_BITS 3
30 #define W1_F2D_SCRATCH_SIZE (1<<W1_F2D_SCRATCH_BITS)
31 #define W1_F2D_SCRATCH_MASK (W1_F2D_SCRATCH_SIZE-1)
33 #define W1_F2D_READ_EEPROM 0xF0
34 #define W1_F2D_WRITE_SCRATCH 0x0F
35 #define W1_F2D_READ_SCRATCH 0xAA
36 #define W1_F2D_COPY_SCRATCH 0x55
39 #define W1_F2D_TPROG_MS 11
41 #define W1_F2D_READ_RETRIES 10
42 #define W1_F2D_READ_MAXLEN 8
45 * Check the file size bounds and adjusts count as needed.
46 * This would not be needed if the file size didn't reset to 0 after a write.
48 static inline size_t w1_f2d_fix_count(loff_t off
, size_t count
, size_t size
)
53 if ((off
+ count
) > size
)
60 * Read a block from W1 ROM two times and compares the results.
61 * If they are equal they are returned, otherwise the read
62 * is repeated W1_F2D_READ_RETRIES times.
64 * count must not exceed W1_F2D_READ_MAXLEN.
66 static int w1_f2d_readblock(struct w1_slave
*sl
, int off
, int count
, char *buf
)
69 u8 cmp
[W1_F2D_READ_MAXLEN
];
70 int tries
= W1_F2D_READ_RETRIES
;
73 wrbuf
[0] = W1_F2D_READ_EEPROM
;
74 wrbuf
[1] = off
& 0xff;
77 if (w1_reset_select_slave(sl
))
80 w1_write_block(sl
->master
, wrbuf
, 3);
81 w1_read_block(sl
->master
, buf
, count
);
83 if (w1_reset_select_slave(sl
))
86 w1_write_block(sl
->master
, wrbuf
, 3);
87 w1_read_block(sl
->master
, cmp
, count
);
89 if (!memcmp(cmp
, buf
, count
))
93 dev_err(&sl
->dev
, "proof reading failed %d times\n",
99 static ssize_t
w1_f2d_read_bin(struct file
*filp
, struct kobject
*kobj
,
100 struct bin_attribute
*bin_attr
,
101 char *buf
, loff_t off
, size_t count
)
103 struct w1_slave
*sl
= kobj_to_w1_slave(kobj
);
106 count
= w1_f2d_fix_count(off
, count
, W1_F2D_EEPROM_SIZE
);
110 mutex_lock(&sl
->master
->bus_mutex
);
112 /* read directly from the EEPROM in chunks of W1_F2D_READ_MAXLEN */
116 if (todo
>= W1_F2D_READ_MAXLEN
)
117 block_read
= W1_F2D_READ_MAXLEN
;
121 if (w1_f2d_readblock(sl
, off
, block_read
, buf
) < 0)
124 todo
-= W1_F2D_READ_MAXLEN
;
125 buf
+= W1_F2D_READ_MAXLEN
;
126 off
+= W1_F2D_READ_MAXLEN
;
129 mutex_unlock(&sl
->master
->bus_mutex
);
135 * Writes to the scratchpad and reads it back for verification.
136 * Then copies the scratchpad to EEPROM.
137 * The data must be aligned at W1_F2D_SCRATCH_SIZE bytes and
138 * must be W1_F2D_SCRATCH_SIZE bytes long.
139 * The master must be locked.
141 * @param sl The slave structure
142 * @param addr Address for the write
143 * @param len length must be <= (W1_F2D_PAGE_SIZE - (addr & W1_F2D_PAGE_MASK))
144 * @param data The data to write
145 * @return 0=Success -1=failure
147 static int w1_f2d_write(struct w1_slave
*sl
, int addr
, int len
, const u8
*data
)
149 int tries
= W1_F2D_READ_RETRIES
;
151 u8 rdbuf
[W1_F2D_SCRATCH_SIZE
+ 3];
152 u8 es
= (addr
+ len
- 1) % W1_F2D_SCRATCH_SIZE
;
156 /* Write the data to the scratchpad */
157 if (w1_reset_select_slave(sl
))
160 wrbuf
[0] = W1_F2D_WRITE_SCRATCH
;
161 wrbuf
[1] = addr
& 0xff;
162 wrbuf
[2] = addr
>> 8;
164 w1_write_block(sl
->master
, wrbuf
, 3);
165 w1_write_block(sl
->master
, data
, len
);
167 /* Read the scratchpad and verify */
168 if (w1_reset_select_slave(sl
))
171 w1_write_8(sl
->master
, W1_F2D_READ_SCRATCH
);
172 w1_read_block(sl
->master
, rdbuf
, len
+ 3);
174 /* Compare what was read against the data written */
175 if ((rdbuf
[0] != wrbuf
[1]) || (rdbuf
[1] != wrbuf
[2]) ||
176 (rdbuf
[2] != es
) || (memcmp(data
, &rdbuf
[3], len
) != 0)) {
182 "could not write to eeprom, scratchpad compare failed %d times\n",
183 W1_F2D_READ_RETRIES
);
188 /* Copy the scratchpad to EEPROM */
189 if (w1_reset_select_slave(sl
))
192 wrbuf
[0] = W1_F2D_COPY_SCRATCH
;
194 w1_write_block(sl
->master
, wrbuf
, 4);
196 /* Sleep for tprog ms to wait for the write to complete */
197 msleep(W1_F2D_TPROG_MS
);
199 /* Reset the bus to wake up the EEPROM */
200 w1_reset_bus(sl
->master
);
205 static ssize_t
w1_f2d_write_bin(struct file
*filp
, struct kobject
*kobj
,
206 struct bin_attribute
*bin_attr
,
207 char *buf
, loff_t off
, size_t count
)
209 struct w1_slave
*sl
= kobj_to_w1_slave(kobj
);
213 count
= w1_f2d_fix_count(off
, count
, W1_F2D_EEPROM_SIZE
);
217 mutex_lock(&sl
->master
->bus_mutex
);
219 /* Can only write data in blocks of the size of the scratchpad */
224 /* if len too short or addr not aligned */
225 if (len
< W1_F2D_SCRATCH_SIZE
|| addr
& W1_F2D_SCRATCH_MASK
) {
226 char tmp
[W1_F2D_SCRATCH_SIZE
];
228 /* read the block and update the parts to be written */
229 if (w1_f2d_readblock(sl
, addr
& ~W1_F2D_SCRATCH_MASK
,
230 W1_F2D_SCRATCH_SIZE
, tmp
)) {
235 /* copy at most to the boundary of the PAGE or len */
236 copy
= W1_F2D_SCRATCH_SIZE
-
237 (addr
& W1_F2D_SCRATCH_MASK
);
242 memcpy(&tmp
[addr
& W1_F2D_SCRATCH_MASK
], buf
, copy
);
243 if (w1_f2d_write(sl
, addr
& ~W1_F2D_SCRATCH_MASK
,
244 W1_F2D_SCRATCH_SIZE
, tmp
) < 0) {
250 copy
= W1_F2D_SCRATCH_SIZE
;
251 if (w1_f2d_write(sl
, addr
, copy
, buf
) < 0) {
262 mutex_unlock(&sl
->master
->bus_mutex
);
267 static struct bin_attribute w1_f2d_bin_attr
= {
270 .mode
= S_IRUGO
| S_IWUSR
,
272 .size
= W1_F2D_EEPROM_SIZE
,
273 .read
= w1_f2d_read_bin
,
274 .write
= w1_f2d_write_bin
,
277 static int w1_f2d_add_slave(struct w1_slave
*sl
)
279 return sysfs_create_bin_file(&sl
->dev
.kobj
, &w1_f2d_bin_attr
);
282 static void w1_f2d_remove_slave(struct w1_slave
*sl
)
284 sysfs_remove_bin_file(&sl
->dev
.kobj
, &w1_f2d_bin_attr
);
287 static struct w1_family_ops w1_f2d_fops
= {
288 .add_slave
= w1_f2d_add_slave
,
289 .remove_slave
= w1_f2d_remove_slave
,
292 static struct w1_family w1_family_2d
= {
293 .fid
= W1_EEPROM_DS2431
,
294 .fops
= &w1_f2d_fops
,
297 static int __init
w1_f2d_init(void)
299 return w1_register_family(&w1_family_2d
);
302 static void __exit
w1_f2d_fini(void)
304 w1_unregister_family(&w1_family_2d
);
307 module_init(w1_f2d_init
);
308 module_exit(w1_f2d_fini
);
310 MODULE_LICENSE("GPL");
311 MODULE_AUTHOR("Bernhard Weirich <bernhard.weirich@riedel.net>");
312 MODULE_DESCRIPTION("w1 family 2d driver for DS2431, 1kb EEPROM");
313 MODULE_ALIAS("w1-family-" __stringify(W1_EEPROM_DS2431
));