Linux 4.18.10
[linux/fpc-iii.git] / drivers / w1 / slaves / w1_ds28e04.c
blobec234b846eb3cbede0ca07564068fee3135d53f2
1 /*
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.
8 */
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>
20 #define CRC16_INIT 0
21 #define CRC16_VALID 0xb001
23 #include <linux/w1.h>
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
31 * required.
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
53 struct w1_f1C_data {
54 u8 memory[W1_EEPROM_SIZE];
55 u32 validcrc;
58 /**
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)
64 if (off > size)
65 return 0;
67 if ((off + count) > size)
68 return size - off;
70 return count;
73 static int w1_f1C_refresh_block(struct w1_slave *sl, struct w1_f1C_data *data,
74 int block)
76 u8 wrbuf[3];
77 int off = block * W1_PAGE_SIZE;
79 if (data->validcrc & (1 << block))
80 return 0;
82 if (w1_reset_select_slave(sl)) {
83 data->validcrc = 0;
84 return -EIO;
87 wrbuf[0] = W1_F1C_READ_EEPROM;
88 wrbuf[1] = off & 0xff;
89 wrbuf[2] = off >> 8;
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);
97 return 0;
100 static int w1_f1C_read(struct w1_slave *sl, int addr, int len, char *data)
102 u8 wrbuf[3];
104 /* read directly from the EEPROM */
105 if (w1_reset_select_slave(sl))
106 return -EIO;
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);
125 if (count == 0)
126 return 0;
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)) {
135 count = -EIO;
136 goto out_up;
139 memcpy(buf, &data->memory[off], count);
140 } else {
141 count = w1_f1C_read(sl, off, count, buf);
144 out_up:
145 mutex_unlock(&sl->master->mutex);
147 return count;
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)
164 u8 wrbuf[4];
165 u8 rdbuf[W1_PAGE_SIZE + 3];
166 u8 es = (addr + len - 1) & 0x1f;
167 unsigned int tm = 10;
168 int i;
169 struct w1_f1C_data *f1C = sl->family_data;
171 /* Write the data to the scratchpad */
172 if (w1_reset_select_slave(sl))
173 return -1;
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))
184 return -1;
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))
192 return -1;
194 /* Copy the scratchpad to EEPROM */
195 if (w1_reset_select_slave(sl))
196 return -1;
198 wrbuf[0] = W1_F1C_COPY_SCRATCH;
199 wrbuf[3] = es;
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)
211 msleep(tm);
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);
221 return 0;
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);
230 int addr, len, idx;
232 count = w1_f1C_fix_count(off, count, W1_EEPROM_SIZE);
233 if (count == 0)
234 return 0;
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",
240 (int)off, count);
241 return -EINVAL;
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)
247 != CRC16_VALID) {
248 dev_err(&sl->dev, "bad CRC at offset %d\n",
249 (int)off);
250 return -EINVAL;
255 mutex_lock(&sl->master->mutex);
257 /* Can only write data to one page at a time */
258 idx = 0;
259 while (idx < count) {
260 addr = off + idx;
261 len = W1_PAGE_SIZE - (addr & W1_PAGE_MASK);
262 if (len > (count - idx))
263 len = count - idx;
265 if (w1_f1C_write(sl, addr, len, &buf[idx]) < 0) {
266 count = -EIO;
267 goto out_up;
269 idx += len;
272 out_up:
273 mutex_unlock(&sl->master->mutex);
275 return count;
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,
282 size_t count)
285 struct w1_slave *sl = kobj_to_w1_slave(kobj);
286 int ret;
288 /* check arguments */
289 if (off != 0 || count != 1 || buf == NULL)
290 return -EINVAL;
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);
296 return ret;
299 static ssize_t pio_write(struct file *filp, struct kobject *kobj,
300 struct bin_attribute *bin_attr, char *buf, loff_t off,
301 size_t count)
304 struct w1_slave *sl = kobj_to_w1_slave(kobj);
305 u8 wrbuf[3];
306 u8 ack;
308 /* check arguments */
309 if (off != 0 || count != 1 || buf == NULL)
310 return -EINVAL;
312 mutex_lock(&sl->master->mutex);
314 /* Write the PIO data */
315 if (w1_reset_select_slave(sl)) {
316 mutex_unlock(&sl->master->mutex);
317 return -1;
320 /* set bit 7..2 to value '1' */
321 *buf = *buf | 0xFC;
323 wrbuf[0] = W1_F1C_ACCESS_WRITE;
324 wrbuf[1] = *buf;
325 wrbuf[2] = ~(*buf);
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 */
333 if (ack != 0xAA)
334 return -EIO;
336 return count;
339 static BIN_ATTR_RW(pio, 1);
341 static ssize_t crccheck_show(struct device *dev, struct device_attribute *attr,
342 char *buf)
344 if (put_user(w1_enable_crccheck + 0x30, buf))
345 return -EFAULT;
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)
353 char val;
355 if (count != 1 || !buf)
356 return -EINVAL;
358 if (get_user(val, buf))
359 return -EFAULT;
361 /* convert to decimal */
362 val = val - 0x30;
363 if (val != 0 && val != 1)
364 return -EINVAL;
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,
376 NULL,
379 static struct bin_attribute *w1_f1C_bin_attrs[] = {
380 &bin_attr_eeprom,
381 &bin_attr_pio,
382 NULL,
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[] = {
391 &w1_f1C_group,
392 NULL,
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);
401 if (!data)
402 return -ENOMEM;
403 sl->family_data = data;
406 return 0;
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));