2 * w1_ds28e04.c - w1 family 1C (DS28E04) driver
4 * Copyright (c) 2012 Markus Franke <franke.m@sebakmt.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>
16 #include <linux/slab.h>
17 #include <linux/crc16.h>
18 #include <linux/uaccess.h>
21 #define CRC16_VALID 0xb001
25 #define W1_FAMILY_DS28E04 0x1C
27 /* Allow the strong pullup to be disabled, but default to enabled.
28 * If it was disabled a parasite powered device might not get the required
29 * current to copy the data from the scratchpad to EEPROM. If it is enabled
30 * parasite powered devices have a better chance of getting the current
33 static int w1_strong_pullup
= 1;
34 module_param_named(strong_pullup
, w1_strong_pullup
, int, 0);
36 /* enable/disable CRC checking on DS28E04-100 memory accesses */
37 static char w1_enable_crccheck
= 1;
39 #define W1_EEPROM_SIZE 512
40 #define W1_PAGE_COUNT 16
41 #define W1_PAGE_SIZE 32
42 #define W1_PAGE_BITS 5
43 #define W1_PAGE_MASK 0x1F
45 #define W1_F1C_READ_EEPROM 0xF0
46 #define W1_F1C_WRITE_SCRATCH 0x0F
47 #define W1_F1C_READ_SCRATCH 0xAA
48 #define W1_F1C_COPY_SCRATCH 0x55
49 #define W1_F1C_ACCESS_WRITE 0x5A
51 #define W1_1C_REG_LOGIC_STATE 0x220
54 u8 memory
[W1_EEPROM_SIZE
];
59 * Check the file size bounds and adjusts count as needed.
60 * This would not be needed if the file size didn't reset to 0 after a write.
62 static inline size_t w1_f1C_fix_count(loff_t off
, size_t count
, size_t size
)
67 if ((off
+ count
) > size
)
73 static int w1_f1C_refresh_block(struct w1_slave
*sl
, struct w1_f1C_data
*data
,
77 int off
= block
* W1_PAGE_SIZE
;
79 if (data
->validcrc
& (1 << block
))
82 if (w1_reset_select_slave(sl
)) {
87 wrbuf
[0] = W1_F1C_READ_EEPROM
;
88 wrbuf
[1] = off
& 0xff;
90 w1_write_block(sl
->master
, wrbuf
, 3);
91 w1_read_block(sl
->master
, &data
->memory
[off
], W1_PAGE_SIZE
);
93 /* cache the block if the CRC is valid */
94 if (crc16(CRC16_INIT
, &data
->memory
[off
], W1_PAGE_SIZE
) == CRC16_VALID
)
95 data
->validcrc
|= (1 << block
);
100 static int w1_f1C_read(struct w1_slave
*sl
, int addr
, int len
, char *data
)
104 /* read directly from the EEPROM */
105 if (w1_reset_select_slave(sl
))
108 wrbuf
[0] = W1_F1C_READ_EEPROM
;
109 wrbuf
[1] = addr
& 0xff;
110 wrbuf
[2] = addr
>> 8;
112 w1_write_block(sl
->master
, wrbuf
, sizeof(wrbuf
));
113 return w1_read_block(sl
->master
, data
, len
);
116 static ssize_t
eeprom_read(struct file
*filp
, struct kobject
*kobj
,
117 struct bin_attribute
*bin_attr
, char *buf
,
118 loff_t off
, size_t count
)
120 struct w1_slave
*sl
= kobj_to_w1_slave(kobj
);
121 struct w1_f1C_data
*data
= sl
->family_data
;
122 int i
, min_page
, max_page
;
124 count
= w1_f1C_fix_count(off
, count
, W1_EEPROM_SIZE
);
128 mutex_lock(&sl
->master
->mutex
);
130 if (w1_enable_crccheck
) {
131 min_page
= (off
>> W1_PAGE_BITS
);
132 max_page
= (off
+ count
- 1) >> W1_PAGE_BITS
;
133 for (i
= min_page
; i
<= max_page
; i
++) {
134 if (w1_f1C_refresh_block(sl
, data
, i
)) {
139 memcpy(buf
, &data
->memory
[off
], count
);
141 count
= w1_f1C_read(sl
, off
, count
, buf
);
145 mutex_unlock(&sl
->master
->mutex
);
151 * Writes to the scratchpad and reads it back for verification.
152 * Then copies the scratchpad to EEPROM.
153 * The data must be on one page.
154 * The master must be locked.
156 * @param sl The slave structure
157 * @param addr Address for the write
158 * @param len length must be <= (W1_PAGE_SIZE - (addr & W1_PAGE_MASK))
159 * @param data The data to write
160 * @return 0=Success -1=failure
162 static int w1_f1C_write(struct w1_slave
*sl
, int addr
, int len
, const u8
*data
)
165 u8 rdbuf
[W1_PAGE_SIZE
+ 3];
166 u8 es
= (addr
+ len
- 1) & 0x1f;
167 unsigned int tm
= 10;
169 struct w1_f1C_data
*f1C
= sl
->family_data
;
171 /* Write the data to the scratchpad */
172 if (w1_reset_select_slave(sl
))
175 wrbuf
[0] = W1_F1C_WRITE_SCRATCH
;
176 wrbuf
[1] = addr
& 0xff;
177 wrbuf
[2] = addr
>> 8;
179 w1_write_block(sl
->master
, wrbuf
, 3);
180 w1_write_block(sl
->master
, data
, len
);
182 /* Read the scratchpad and verify */
183 if (w1_reset_select_slave(sl
))
186 w1_write_8(sl
->master
, W1_F1C_READ_SCRATCH
);
187 w1_read_block(sl
->master
, rdbuf
, len
+ 3);
189 /* Compare what was read against the data written */
190 if ((rdbuf
[0] != wrbuf
[1]) || (rdbuf
[1] != wrbuf
[2]) ||
191 (rdbuf
[2] != es
) || (memcmp(data
, &rdbuf
[3], len
) != 0))
194 /* Copy the scratchpad to EEPROM */
195 if (w1_reset_select_slave(sl
))
198 wrbuf
[0] = W1_F1C_COPY_SCRATCH
;
201 for (i
= 0; i
< sizeof(wrbuf
); ++i
) {
202 /* issue 10ms strong pullup (or delay) on the last byte
203 for writing the data from the scratchpad to EEPROM */
204 if (w1_strong_pullup
&& i
== sizeof(wrbuf
)-1)
205 w1_next_pullup(sl
->master
, tm
);
207 w1_write_8(sl
->master
, wrbuf
[i
]);
210 if (!w1_strong_pullup
)
213 if (w1_enable_crccheck
) {
214 /* invalidate cached data */
215 f1C
->validcrc
&= ~(1 << (addr
>> W1_PAGE_BITS
));
218 /* Reset the bus to wake up the EEPROM (this may not be needed) */
219 w1_reset_bus(sl
->master
);
224 static ssize_t
eeprom_write(struct file
*filp
, struct kobject
*kobj
,
225 struct bin_attribute
*bin_attr
, char *buf
,
226 loff_t off
, size_t count
)
229 struct w1_slave
*sl
= kobj_to_w1_slave(kobj
);
232 count
= w1_f1C_fix_count(off
, count
, W1_EEPROM_SIZE
);
236 if (w1_enable_crccheck
) {
237 /* can only write full blocks in cached mode */
238 if ((off
& W1_PAGE_MASK
) || (count
& W1_PAGE_MASK
)) {
239 dev_err(&sl
->dev
, "invalid offset/count off=%d cnt=%zd\n",
244 /* make sure the block CRCs are valid */
245 for (idx
= 0; idx
< count
; idx
+= W1_PAGE_SIZE
) {
246 if (crc16(CRC16_INIT
, &buf
[idx
], W1_PAGE_SIZE
)
248 dev_err(&sl
->dev
, "bad CRC at offset %d\n",
255 mutex_lock(&sl
->master
->mutex
);
257 /* Can only write data to one page at a time */
259 while (idx
< count
) {
261 len
= W1_PAGE_SIZE
- (addr
& W1_PAGE_MASK
);
262 if (len
> (count
- idx
))
265 if (w1_f1C_write(sl
, addr
, len
, &buf
[idx
]) < 0) {
273 mutex_unlock(&sl
->master
->mutex
);
278 static BIN_ATTR_RW(eeprom
, W1_EEPROM_SIZE
);
280 static ssize_t
pio_read(struct file
*filp
, struct kobject
*kobj
,
281 struct bin_attribute
*bin_attr
, char *buf
, loff_t off
,
285 struct w1_slave
*sl
= kobj_to_w1_slave(kobj
);
288 /* check arguments */
289 if (off
!= 0 || count
!= 1 || buf
== NULL
)
292 mutex_lock(&sl
->master
->mutex
);
293 ret
= w1_f1C_read(sl
, W1_1C_REG_LOGIC_STATE
, count
, buf
);
294 mutex_unlock(&sl
->master
->mutex
);
299 static ssize_t
pio_write(struct file
*filp
, struct kobject
*kobj
,
300 struct bin_attribute
*bin_attr
, char *buf
, loff_t off
,
304 struct w1_slave
*sl
= kobj_to_w1_slave(kobj
);
308 /* check arguments */
309 if (off
!= 0 || count
!= 1 || buf
== NULL
)
312 mutex_lock(&sl
->master
->mutex
);
314 /* Write the PIO data */
315 if (w1_reset_select_slave(sl
)) {
316 mutex_unlock(&sl
->master
->mutex
);
320 /* set bit 7..2 to value '1' */
323 wrbuf
[0] = W1_F1C_ACCESS_WRITE
;
326 w1_write_block(sl
->master
, wrbuf
, 3);
328 w1_read_block(sl
->master
, &ack
, sizeof(ack
));
330 mutex_unlock(&sl
->master
->mutex
);
332 /* check for acknowledgement */
339 static BIN_ATTR_RW(pio
, 1);
341 static ssize_t
crccheck_show(struct device
*dev
, struct device_attribute
*attr
,
344 if (put_user(w1_enable_crccheck
+ 0x30, buf
))
347 return sizeof(w1_enable_crccheck
);
350 static ssize_t
crccheck_store(struct device
*dev
, struct device_attribute
*attr
,
351 const char *buf
, size_t count
)
355 if (count
!= 1 || !buf
)
358 if (get_user(val
, buf
))
361 /* convert to decimal */
363 if (val
!= 0 && val
!= 1)
366 /* set the new value */
367 w1_enable_crccheck
= val
;
369 return sizeof(w1_enable_crccheck
);
372 static DEVICE_ATTR_RW(crccheck
);
374 static struct attribute
*w1_f1C_attrs
[] = {
375 &dev_attr_crccheck
.attr
,
379 static struct bin_attribute
*w1_f1C_bin_attrs
[] = {
385 static const struct attribute_group w1_f1C_group
= {
386 .attrs
= w1_f1C_attrs
,
387 .bin_attrs
= w1_f1C_bin_attrs
,
390 static const struct attribute_group
*w1_f1C_groups
[] = {
395 static int w1_f1C_add_slave(struct w1_slave
*sl
)
397 struct w1_f1C_data
*data
= NULL
;
399 if (w1_enable_crccheck
) {
400 data
= kzalloc(sizeof(struct w1_f1C_data
), GFP_KERNEL
);
403 sl
->family_data
= data
;
409 static void w1_f1C_remove_slave(struct w1_slave
*sl
)
411 kfree(sl
->family_data
);
412 sl
->family_data
= NULL
;
415 static struct w1_family_ops w1_f1C_fops
= {
416 .add_slave
= w1_f1C_add_slave
,
417 .remove_slave
= w1_f1C_remove_slave
,
418 .groups
= w1_f1C_groups
,
421 static struct w1_family w1_family_1C
= {
422 .fid
= W1_FAMILY_DS28E04
,
423 .fops
= &w1_f1C_fops
,
425 module_w1_family(w1_family_1C
);
427 MODULE_AUTHOR("Markus Franke <franke.m@sebakmt.com>, <franm@hrz.tu-chemnitz.de>");
428 MODULE_DESCRIPTION("w1 family 1C driver for DS28E04, 4kb EEPROM and PIO");
429 MODULE_LICENSE("GPL");
430 MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS28E04
));