2 * w1_ds2433.c - w1 family 23 (DS2433) driver
4 * Copyright (c) 2005 Ben Gardner <bgardner@wabtec.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the smems of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/device.h>
15 #include <linux/types.h>
16 #include <linux/delay.h>
21 #include "w1_family.h"
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
25 MODULE_DESCRIPTION("w1 family 23 driver for DS2433, 4kb EEPROM");
27 #define W1_EEPROM_SIZE 512
28 #define W1_PAGE_SIZE 32
29 #define W1_PAGE_BITS 5
30 #define W1_PAGE_MASK 0x1F
32 #define W1_F23_READ_EEPROM 0xF0
33 #define W1_F23_WRITE_SCRATCH 0x0F
34 #define W1_F23_READ_SCRATCH 0xAA
35 #define W1_F23_COPY_SCRATCH 0x55
38 * Check the file size bounds and adjusts count as needed.
39 * This may not be needed if the sysfs layer checks bounds.
41 static inline size_t w1_f23_fix_count(loff_t off, size_t count, size_t size)
46 if ((off + count) > size)
52 static ssize_t w1_f23_read_bin(struct kobject *kobj, char *buf, loff_t off, size_t count)
54 struct w1_slave *sl = kobj_to_w1_slave(kobj);
57 if ((count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE)) == 0)
60 atomic_inc(&sl->refcnt);
61 if (down_interruptible(&sl->master->mutex)) {
66 /* read directly from the EEPROM */
67 if (w1_reset_select_slave(sl)) {
72 wrbuf[0] = W1_F23_READ_EEPROM;
73 wrbuf[1] = off & 0xff;
75 w1_write_block(sl->master, wrbuf, 3);
76 w1_read_block(sl->master, buf, count);
79 up(&sl->master->mutex);
81 atomic_dec(&sl->refcnt);
87 * Writes to the scratchpad and reads it back for verification.
88 * The master must be locked.
90 * @param sl The slave structure
91 * @param addr Address for the write
92 * @param len length must be <= (W1_PAGE_SIZE - (addr & W1_PAGE_MASK))
93 * @param data The data to write
94 * @return 0=Success -1=failure
96 static int w1_f23_write(struct w1_slave *sl, int addr, int len, const u8 *data)
99 u8 rdbuf[W1_PAGE_SIZE + 3];
100 u8 es = (addr + len - 1) & 0x1f;
102 /* Write the data to the scratchpad */
103 if (w1_reset_select_slave(sl))
106 wrbuf[0] = W1_F23_WRITE_SCRATCH;
107 wrbuf[1] = addr & 0xff;
108 wrbuf[2] = addr >> 8;
110 w1_write_block(sl->master, wrbuf, 3);
111 w1_write_block(sl->master, data, len);
113 /* Read the scratchpad and verify */
114 if (w1_reset_select_slave(sl))
117 w1_write_8(sl->master, W1_F23_READ_SCRATCH);
118 w1_read_block(sl->master, rdbuf, len + 3);
120 /* Compare what was read against the data written */
121 if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
122 (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0))
125 /* Copy the scratchpad to EEPROM */
126 if (w1_reset_select_slave(sl))
129 wrbuf[0] = W1_F23_COPY_SCRATCH;
131 w1_write_block(sl->master, wrbuf, 4);
133 /* Sleep for 5 ms to wait for the write to complete */
136 /* Reset the bus to wake up the EEPROM (this may not be needed) */
137 w1_reset_bus(sl->master);
142 static ssize_t w1_f23_write_bin(struct kobject *kobj, char *buf, loff_t off,
145 struct w1_slave *sl = kobj_to_w1_slave(kobj);
148 if ((count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE)) == 0)
151 atomic_inc(&sl->refcnt);
152 if (down_interruptible(&sl->master->mutex)) {
157 /* Can only write data to one page at a time */
159 while (idx < count) {
161 len = W1_PAGE_SIZE - (addr & W1_PAGE_MASK);
162 if (len > (count - idx))
165 if (w1_f23_write(sl, addr, len, &buf[idx]) < 0) {
173 up(&sl->master->mutex);
175 atomic_dec(&sl->refcnt);
180 static struct bin_attribute w1_f23_bin_attr = {
183 .mode = S_IRUGO | S_IWUSR,
184 .owner = THIS_MODULE,
186 .size = W1_EEPROM_SIZE,
187 .read = w1_f23_read_bin,
188 .write = w1_f23_write_bin,
191 static int w1_f23_add_slave(struct w1_slave *sl)
193 return sysfs_create_bin_file(&sl->dev.kobj, &w1_f23_bin_attr);
196 static void w1_f23_remove_slave(struct w1_slave *sl)
198 sysfs_remove_bin_file(&sl->dev.kobj, &w1_f23_bin_attr);
201 static struct w1_family_ops w1_f23_fops = {
202 .add_slave = w1_f23_add_slave,
203 .remove_slave = w1_f23_remove_slave,
206 static struct w1_family w1_family_23 = {
207 .fid = W1_EEPROM_DS2433,
208 .fops = &w1_f23_fops,
211 static int __init w1_f23_init(void)
213 return w1_register_family(&w1_family_23);
216 static void __exit w1_f23_fini(void)
218 w1_unregister_family(&w1_family_23);
221 module_init(w1_f23_init);
222 module_exit(w1_f23_fini);