xref: /OK3568_Linux_fs/kernel/drivers/w1/slaves/w1_ds2805.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * w1_ds2805 - w1 family 0d (DS28E05) driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2016 Andrew Worsley amworsley@gmail.com
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/kernel.h>
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/moduleparam.h>
11*4882a593Smuzhiyun #include <linux/device.h>
12*4882a593Smuzhiyun #include <linux/types.h>
13*4882a593Smuzhiyun #include <linux/delay.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <linux/w1.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #define W1_EEPROM_DS2805       0x0D
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #define W1_F0D_EEPROM_SIZE		128
20*4882a593Smuzhiyun #define W1_F0D_PAGE_BITS		3
21*4882a593Smuzhiyun #define W1_F0D_PAGE_SIZE		(1<<W1_F0D_PAGE_BITS)
22*4882a593Smuzhiyun #define W1_F0D_PAGE_MASK		0x0F
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define W1_F0D_SCRATCH_BITS  1
25*4882a593Smuzhiyun #define W1_F0D_SCRATCH_SIZE  (1<<W1_F0D_SCRATCH_BITS)
26*4882a593Smuzhiyun #define W1_F0D_SCRATCH_MASK  (W1_F0D_SCRATCH_SIZE-1)
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #define W1_F0D_READ_EEPROM	0xF0
29*4882a593Smuzhiyun #define W1_F0D_WRITE_EEPROM	0x55
30*4882a593Smuzhiyun #define W1_F0D_RELEASE		0xFF
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define W1_F0D_CS_OK		0xAA /* Chip Status Ok */
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #define W1_F0D_TPROG_MS		16
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #define W1_F0D_READ_RETRIES		10
37*4882a593Smuzhiyun #define W1_F0D_READ_MAXLEN		W1_F0D_EEPROM_SIZE
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /*
40*4882a593Smuzhiyun  * Check the file size bounds and adjusts count as needed.
41*4882a593Smuzhiyun  * This would not be needed if the file size didn't reset to 0 after a write.
42*4882a593Smuzhiyun  */
w1_f0d_fix_count(loff_t off,size_t count,size_t size)43*4882a593Smuzhiyun static inline size_t w1_f0d_fix_count(loff_t off, size_t count, size_t size)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	if (off > size)
46*4882a593Smuzhiyun 		return 0;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	if ((off + count) > size)
49*4882a593Smuzhiyun 		return size - off;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	return count;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /*
55*4882a593Smuzhiyun  * Read a block from W1 ROM two times and compares the results.
56*4882a593Smuzhiyun  * If they are equal they are returned, otherwise the read
57*4882a593Smuzhiyun  * is repeated W1_F0D_READ_RETRIES times.
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * count must not exceed W1_F0D_READ_MAXLEN.
60*4882a593Smuzhiyun  */
w1_f0d_readblock(struct w1_slave * sl,int off,int count,char * buf)61*4882a593Smuzhiyun static int w1_f0d_readblock(struct w1_slave *sl, int off, int count, char *buf)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	u8 wrbuf[3];
64*4882a593Smuzhiyun 	u8 cmp[W1_F0D_READ_MAXLEN];
65*4882a593Smuzhiyun 	int tries = W1_F0D_READ_RETRIES;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	do {
68*4882a593Smuzhiyun 		wrbuf[0] = W1_F0D_READ_EEPROM;
69*4882a593Smuzhiyun 		wrbuf[1] = off & 0x7f;
70*4882a593Smuzhiyun 		wrbuf[2] = 0;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 		if (w1_reset_select_slave(sl))
73*4882a593Smuzhiyun 			return -1;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 		w1_write_block(sl->master, wrbuf, sizeof(wrbuf));
76*4882a593Smuzhiyun 		w1_read_block(sl->master, buf, count);
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 		if (w1_reset_select_slave(sl))
79*4882a593Smuzhiyun 			return -1;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 		w1_write_block(sl->master, wrbuf, sizeof(wrbuf));
82*4882a593Smuzhiyun 		w1_read_block(sl->master, cmp, count);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 		if (!memcmp(cmp, buf, count))
85*4882a593Smuzhiyun 			return 0;
86*4882a593Smuzhiyun 	} while (--tries);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	dev_err(&sl->dev, "proof reading failed %d times\n",
89*4882a593Smuzhiyun 			W1_F0D_READ_RETRIES);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	return -1;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
w1_f0d_read_bin(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)94*4882a593Smuzhiyun static ssize_t w1_f0d_read_bin(struct file *filp, struct kobject *kobj,
95*4882a593Smuzhiyun 			       struct bin_attribute *bin_attr,
96*4882a593Smuzhiyun 			       char *buf, loff_t off, size_t count)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	struct w1_slave *sl = kobj_to_w1_slave(kobj);
99*4882a593Smuzhiyun 	int todo = count;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	count = w1_f0d_fix_count(off, count, W1_F0D_EEPROM_SIZE);
102*4882a593Smuzhiyun 	if (count == 0)
103*4882a593Smuzhiyun 		return 0;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	mutex_lock(&sl->master->mutex);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	/* read directly from the EEPROM in chunks of W1_F0D_READ_MAXLEN */
108*4882a593Smuzhiyun 	while (todo > 0) {
109*4882a593Smuzhiyun 		int block_read;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 		if (todo >= W1_F0D_READ_MAXLEN)
112*4882a593Smuzhiyun 			block_read = W1_F0D_READ_MAXLEN;
113*4882a593Smuzhiyun 		else
114*4882a593Smuzhiyun 			block_read = todo;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 		if (w1_f0d_readblock(sl, off, block_read, buf) < 0) {
117*4882a593Smuzhiyun 			count = -EIO;
118*4882a593Smuzhiyun 			break;
119*4882a593Smuzhiyun 		}
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 		todo -= W1_F0D_READ_MAXLEN;
122*4882a593Smuzhiyun 		buf += W1_F0D_READ_MAXLEN;
123*4882a593Smuzhiyun 		off += W1_F0D_READ_MAXLEN;
124*4882a593Smuzhiyun 	}
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	mutex_unlock(&sl->master->mutex);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	return count;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun /*
132*4882a593Smuzhiyun  * Writes to the scratchpad and reads it back for verification.
133*4882a593Smuzhiyun  * Then copies the scratchpad to EEPROM.
134*4882a593Smuzhiyun  * The data must be aligned at W1_F0D_SCRATCH_SIZE bytes and
135*4882a593Smuzhiyun  * must be W1_F0D_SCRATCH_SIZE bytes long.
136*4882a593Smuzhiyun  * The master must be locked.
137*4882a593Smuzhiyun  *
138*4882a593Smuzhiyun  * @param sl	The slave structure
139*4882a593Smuzhiyun  * @param addr	Address for the write
140*4882a593Smuzhiyun  * @param len   length must be <= (W1_F0D_PAGE_SIZE - (addr & W1_F0D_PAGE_MASK))
141*4882a593Smuzhiyun  * @param data	The data to write
142*4882a593Smuzhiyun  * @return	0=Success -1=failure
143*4882a593Smuzhiyun  */
w1_f0d_write(struct w1_slave * sl,int addr,int len,const u8 * data)144*4882a593Smuzhiyun static int w1_f0d_write(struct w1_slave *sl, int addr, int len, const u8 *data)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	int tries = W1_F0D_READ_RETRIES;
147*4882a593Smuzhiyun 	u8 wrbuf[3];
148*4882a593Smuzhiyun 	u8 rdbuf[W1_F0D_SCRATCH_SIZE];
149*4882a593Smuzhiyun 	u8 cs;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	if ((addr & 1) || (len != 2)) {
152*4882a593Smuzhiyun 		dev_err(&sl->dev, "%s: bad addr/len -  addr=%#x len=%d\n",
153*4882a593Smuzhiyun 		    __func__, addr, len);
154*4882a593Smuzhiyun 		return -1;
155*4882a593Smuzhiyun 	}
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun retry:
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	/* Write the data to the scratchpad */
160*4882a593Smuzhiyun 	if (w1_reset_select_slave(sl))
161*4882a593Smuzhiyun 		return -1;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	wrbuf[0] = W1_F0D_WRITE_EEPROM;
164*4882a593Smuzhiyun 	wrbuf[1] = addr & 0xff;
165*4882a593Smuzhiyun 	wrbuf[2] = 0xff; /* ?? from Example */
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	w1_write_block(sl->master, wrbuf, sizeof(wrbuf));
168*4882a593Smuzhiyun 	w1_write_block(sl->master, data, len);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	w1_read_block(sl->master, rdbuf, sizeof(rdbuf));
171*4882a593Smuzhiyun 	/* Compare what was read against the data written */
172*4882a593Smuzhiyun 	if ((rdbuf[0] != data[0]) || (rdbuf[1] != data[1])) {
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 		if (--tries)
175*4882a593Smuzhiyun 			goto retry;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 		dev_err(&sl->dev,
178*4882a593Smuzhiyun 			"could not write to eeprom, scratchpad compare failed %d times\n",
179*4882a593Smuzhiyun 			W1_F0D_READ_RETRIES);
180*4882a593Smuzhiyun 		pr_info("%s: rdbuf = %#x %#x data = %#x %#x\n",
181*4882a593Smuzhiyun 		    __func__, rdbuf[0], rdbuf[1], data[0], data[1]);
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 		return -1;
184*4882a593Smuzhiyun 	}
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	/* Trigger write out to EEPROM */
187*4882a593Smuzhiyun 	w1_write_8(sl->master, W1_F0D_RELEASE);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	/* Sleep for tprog ms to wait for the write to complete */
190*4882a593Smuzhiyun 	msleep(W1_F0D_TPROG_MS);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	/* Check CS (Command Status) == 0xAA ? */
193*4882a593Smuzhiyun 	cs = w1_read_8(sl->master);
194*4882a593Smuzhiyun 	if (cs != W1_F0D_CS_OK) {
195*4882a593Smuzhiyun 		dev_err(&sl->dev, "save to eeprom failed = CS=%#x\n", cs);
196*4882a593Smuzhiyun 		return -1;
197*4882a593Smuzhiyun 	}
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	return 0;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
w1_f0d_write_bin(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)202*4882a593Smuzhiyun static ssize_t w1_f0d_write_bin(struct file *filp, struct kobject *kobj,
203*4882a593Smuzhiyun 				struct bin_attribute *bin_attr,
204*4882a593Smuzhiyun 				char *buf, loff_t off, size_t count)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	struct w1_slave *sl = kobj_to_w1_slave(kobj);
207*4882a593Smuzhiyun 	int addr, len;
208*4882a593Smuzhiyun 	int copy;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	count = w1_f0d_fix_count(off, count, W1_F0D_EEPROM_SIZE);
211*4882a593Smuzhiyun 	if (count == 0)
212*4882a593Smuzhiyun 		return 0;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	mutex_lock(&sl->master->mutex);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	/* Can only write data in blocks of the size of the scratchpad */
217*4882a593Smuzhiyun 	addr = off;
218*4882a593Smuzhiyun 	len = count;
219*4882a593Smuzhiyun 	while (len > 0) {
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 		/* if len too short or addr not aligned */
222*4882a593Smuzhiyun 		if (len < W1_F0D_SCRATCH_SIZE || addr & W1_F0D_SCRATCH_MASK) {
223*4882a593Smuzhiyun 			char tmp[W1_F0D_SCRATCH_SIZE];
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 			/* read the block and update the parts to be written */
226*4882a593Smuzhiyun 			if (w1_f0d_readblock(sl, addr & ~W1_F0D_SCRATCH_MASK,
227*4882a593Smuzhiyun 					W1_F0D_SCRATCH_SIZE, tmp)) {
228*4882a593Smuzhiyun 				count = -EIO;
229*4882a593Smuzhiyun 				goto out_up;
230*4882a593Smuzhiyun 			}
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 			/* copy at most to the boundary of the PAGE or len */
233*4882a593Smuzhiyun 			copy = W1_F0D_SCRATCH_SIZE -
234*4882a593Smuzhiyun 				(addr & W1_F0D_SCRATCH_MASK);
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 			if (copy > len)
237*4882a593Smuzhiyun 				copy = len;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 			memcpy(&tmp[addr & W1_F0D_SCRATCH_MASK], buf, copy);
240*4882a593Smuzhiyun 			if (w1_f0d_write(sl, addr & ~W1_F0D_SCRATCH_MASK,
241*4882a593Smuzhiyun 					W1_F0D_SCRATCH_SIZE, tmp) < 0) {
242*4882a593Smuzhiyun 				count = -EIO;
243*4882a593Smuzhiyun 				goto out_up;
244*4882a593Smuzhiyun 			}
245*4882a593Smuzhiyun 		} else {
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 			copy = W1_F0D_SCRATCH_SIZE;
248*4882a593Smuzhiyun 			if (w1_f0d_write(sl, addr, copy, buf) < 0) {
249*4882a593Smuzhiyun 				count = -EIO;
250*4882a593Smuzhiyun 				goto out_up;
251*4882a593Smuzhiyun 			}
252*4882a593Smuzhiyun 		}
253*4882a593Smuzhiyun 		buf += copy;
254*4882a593Smuzhiyun 		addr += copy;
255*4882a593Smuzhiyun 		len -= copy;
256*4882a593Smuzhiyun 	}
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun out_up:
259*4882a593Smuzhiyun 	mutex_unlock(&sl->master->mutex);
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	return count;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun static struct bin_attribute w1_f0d_bin_attr = {
265*4882a593Smuzhiyun 	.attr = {
266*4882a593Smuzhiyun 		.name = "eeprom",
267*4882a593Smuzhiyun 		.mode = S_IRUGO | S_IWUSR,
268*4882a593Smuzhiyun 	},
269*4882a593Smuzhiyun 	.size = W1_F0D_EEPROM_SIZE,
270*4882a593Smuzhiyun 	.read = w1_f0d_read_bin,
271*4882a593Smuzhiyun 	.write = w1_f0d_write_bin,
272*4882a593Smuzhiyun };
273*4882a593Smuzhiyun 
w1_f0d_add_slave(struct w1_slave * sl)274*4882a593Smuzhiyun static int w1_f0d_add_slave(struct w1_slave *sl)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun 	return sysfs_create_bin_file(&sl->dev.kobj, &w1_f0d_bin_attr);
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun 
w1_f0d_remove_slave(struct w1_slave * sl)279*4882a593Smuzhiyun static void w1_f0d_remove_slave(struct w1_slave *sl)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun 	sysfs_remove_bin_file(&sl->dev.kobj, &w1_f0d_bin_attr);
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun static const struct w1_family_ops w1_f0d_fops = {
285*4882a593Smuzhiyun 	.add_slave      = w1_f0d_add_slave,
286*4882a593Smuzhiyun 	.remove_slave   = w1_f0d_remove_slave,
287*4882a593Smuzhiyun };
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun static struct w1_family w1_family_0d = {
290*4882a593Smuzhiyun 	.fid = W1_EEPROM_DS2805,
291*4882a593Smuzhiyun 	.fops = &w1_f0d_fops,
292*4882a593Smuzhiyun };
293*4882a593Smuzhiyun 
w1_f0d_init(void)294*4882a593Smuzhiyun static int __init w1_f0d_init(void)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun 	pr_info("%s()\n", __func__);
297*4882a593Smuzhiyun 	return w1_register_family(&w1_family_0d);
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun 
w1_f0d_fini(void)300*4882a593Smuzhiyun static void __exit w1_f0d_fini(void)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun 	pr_info("%s()\n", __func__);
303*4882a593Smuzhiyun 	w1_unregister_family(&w1_family_0d);
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun module_init(w1_f0d_init);
307*4882a593Smuzhiyun module_exit(w1_f0d_fini);
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun MODULE_LICENSE("GPL");
310*4882a593Smuzhiyun MODULE_AUTHOR("Andrew Worsley amworsley@gmail.com");
311*4882a593Smuzhiyun MODULE_DESCRIPTION("w1 family 0d driver for DS2805, 1kb EEPROM");
312