4 * Copyright (c) 2010 Mika Laitio <lamikr@pilppa.org>
6 * This driver will read and write the value of 4 counters to w1_slave file in
8 * Inspired by the w1_therm and w1_ds2431 drivers.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the therms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/device.h>
29 #include <linux/types.h>
30 #include <linux/delay.h>
31 #include <linux/crc16.h>
35 #define W1_COUNTER_DS2423 0x1D
37 #define CRC16_VALID 0xb001
40 #define COUNTER_COUNT 4
41 #define READ_BYTE_COUNT 42
43 static ssize_t
w1_slave_show(struct device
*device
,
44 struct device_attribute
*attr
, char *out_buf
)
46 struct w1_slave
*sl
= dev_to_w1_slave(device
);
47 struct w1_master
*dev
= sl
->master
;
48 u8 rbuf
[COUNTER_COUNT
* READ_BYTE_COUNT
];
59 rom_addr
= (12 << 5) + 31;
61 wrbuf
[1] = rom_addr
& 0xFF;
62 wrbuf
[2] = rom_addr
>> 8;
63 mutex_lock(&dev
->bus_mutex
);
64 if (!w1_reset_select_slave(sl
)) {
65 w1_write_block(dev
, wrbuf
, 3);
67 for (p
= 0; p
< 4; p
++) {
69 * 1 byte for first bytes in ram page read
71 * 4 bytes for zero bits
73 * 31 remaining bytes from the ram page
75 read_byte_count
+= w1_read_block(dev
,
76 rbuf
+ (p
* READ_BYTE_COUNT
), READ_BYTE_COUNT
);
77 for (ii
= 0; ii
< READ_BYTE_COUNT
; ++ii
)
78 c
-= snprintf(out_buf
+ PAGE_SIZE
- c
,
80 rbuf
[(p
* READ_BYTE_COUNT
) + ii
]);
81 if (read_byte_count
!= (p
+ 1) * READ_BYTE_COUNT
) {
83 "w1_counter_read() returned %u bytes "
84 "instead of %d bytes wanted.\n",
87 c
-= snprintf(out_buf
+ PAGE_SIZE
- c
,
91 crc
= crc16(CRC16_INIT
, wrbuf
, 3);
92 crc
= crc16(crc
, rbuf
, 11);
95 * DS2423 calculates crc from all bytes
96 * read after the previous crc bytes.
98 crc
= crc16(CRC16_INIT
,
100 ((p
- 1) * READ_BYTE_COUNT
),
103 if (crc
== CRC16_VALID
) {
105 for (ii
= 4; ii
> 0; ii
--) {
108 READ_BYTE_COUNT
) + ii
];
110 c
-= snprintf(out_buf
+ PAGE_SIZE
- c
,
111 c
, "crc=YES c=%d\n", result
);
113 c
-= snprintf(out_buf
+ PAGE_SIZE
- c
,
119 c
-= snprintf(out_buf
+ PAGE_SIZE
- c
, c
, "Connection error");
121 mutex_unlock(&dev
->bus_mutex
);
122 return PAGE_SIZE
- c
;
125 static DEVICE_ATTR_RO(w1_slave
);
127 static struct attribute
*w1_f1d_attrs
[] = {
128 &dev_attr_w1_slave
.attr
,
131 ATTRIBUTE_GROUPS(w1_f1d
);
133 static struct w1_family_ops w1_f1d_fops
= {
134 .groups
= w1_f1d_groups
,
137 static struct w1_family w1_family_1d
= {
138 .fid
= W1_COUNTER_DS2423
,
139 .fops
= &w1_f1d_fops
,
141 module_w1_family(w1_family_1d
);
143 MODULE_AUTHOR("Mika Laitio <lamikr@pilppa.org>");
144 MODULE_DESCRIPTION("w1 family 1d driver for DS2423, 4 counters and 4kb ram");
145 MODULE_LICENSE("GPL");
146 MODULE_ALIAS("w1-family-" __stringify(W1_COUNTER_DS2423
));