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>
21 #define W1_EEPROM_DS2431 0x2D
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
eeprom_read(struct file
*filp
, struct kobject
*kobj
,
100 struct bin_attribute
*bin_attr
, char *buf
,
101 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
eeprom_write(struct file
*filp
, struct kobject
*kobj
,
206 struct bin_attribute
*bin_attr
, char *buf
,
207 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 BIN_ATTR_RW(eeprom
, W1_F2D_EEPROM_SIZE
);
269 static struct bin_attribute
*w1_f2d_bin_attrs
[] = {
274 static const struct attribute_group w1_f2d_group
= {
275 .bin_attrs
= w1_f2d_bin_attrs
,
278 static const struct attribute_group
*w1_f2d_groups
[] = {
283 static struct w1_family_ops w1_f2d_fops
= {
284 .groups
= w1_f2d_groups
,
287 static struct w1_family w1_family_2d
= {
288 .fid
= W1_EEPROM_DS2431
,
289 .fops
= &w1_f2d_fops
,
291 module_w1_family(w1_family_2d
);
293 MODULE_AUTHOR("Bernhard Weirich <bernhard.weirich@riedel.net>");
294 MODULE_DESCRIPTION("w1 family 2d driver for DS2431, 1kb EEPROM");
295 MODULE_LICENSE("GPL");
296 MODULE_ALIAS("w1-family-" __stringify(W1_EEPROM_DS2431
));