Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / w1 / slaves / w1_ds2433.c
blob250b7f7ec429e92a6146452a20d591644af4e083
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * w1_ds2433.c - w1 family 23 (DS2433) & 43 (DS28EC20) eeprom driver
5 * Copyright (c) 2005 Ben Gardner <bgardner@wabtec.com>
6 * Copyright (c) 2023 Marc Ferland <marc.ferland@sonatest.com>
7 */
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/device.h>
13 #include <linux/types.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
16 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
17 #include <linux/crc16.h>
19 #define CRC16_INIT 0
20 #define CRC16_VALID 0xb001
22 #endif
24 #include <linux/w1.h>
26 #define W1_EEPROM_DS2433 0x23
27 #define W1_EEPROM_DS28EC20 0x43
29 #define W1_EEPROM_DS2433_SIZE 512
30 #define W1_EEPROM_DS28EC20_SIZE 2560
32 #define W1_PAGE_SIZE 32
33 #define W1_PAGE_BITS 5
34 #define W1_PAGE_MASK 0x1F
35 #define W1_VALIDCRC_MAX 96
37 #define W1_F23_READ_EEPROM 0xF0
38 #define W1_F23_WRITE_SCRATCH 0x0F
39 #define W1_F23_READ_SCRATCH 0xAA
40 #define W1_F23_COPY_SCRATCH 0x55
42 struct ds2433_config {
43 size_t eeprom_size; /* eeprom size in bytes */
44 unsigned int page_count; /* number of 256 bits pages */
45 unsigned int tprog; /* time in ms for page programming */
48 static const struct ds2433_config config_f23 = {
49 .eeprom_size = W1_EEPROM_DS2433_SIZE,
50 .page_count = 16,
51 .tprog = 5,
54 static const struct ds2433_config config_f43 = {
55 .eeprom_size = W1_EEPROM_DS28EC20_SIZE,
56 .page_count = 80,
57 .tprog = 10,
60 struct w1_f23_data {
61 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
62 u8 *memory;
63 DECLARE_BITMAP(validcrc, W1_VALIDCRC_MAX);
64 #endif
65 const struct ds2433_config *cfg;
69 * Check the file size bounds and adjusts count as needed.
70 * This would not be needed if the file size didn't reset to 0 after a write.
72 static inline size_t w1_f23_fix_count(loff_t off, size_t count, size_t size)
74 if (off > size)
75 return 0;
77 if ((off + count) > size)
78 return (size - off);
80 return count;
83 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
84 static int w1_f23_refresh_block(struct w1_slave *sl, struct w1_f23_data *data,
85 int block)
87 u8 wrbuf[3];
88 int off = block * W1_PAGE_SIZE;
90 if (test_bit(block, data->validcrc))
91 return 0;
93 if (w1_reset_select_slave(sl)) {
94 bitmap_zero(data->validcrc, data->cfg->page_count);
95 return -EIO;
98 wrbuf[0] = W1_F23_READ_EEPROM;
99 wrbuf[1] = off & 0xff;
100 wrbuf[2] = off >> 8;
101 w1_write_block(sl->master, wrbuf, 3);
102 w1_read_block(sl->master, &data->memory[off], W1_PAGE_SIZE);
104 /* cache the block if the CRC is valid */
105 if (crc16(CRC16_INIT, &data->memory[off], W1_PAGE_SIZE) == CRC16_VALID)
106 set_bit(block, data->validcrc);
108 return 0;
110 #endif /* CONFIG_W1_SLAVE_DS2433_CRC */
112 static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
113 struct bin_attribute *bin_attr, char *buf,
114 loff_t off, size_t count)
116 struct w1_slave *sl = kobj_to_w1_slave(kobj);
117 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
118 struct w1_f23_data *data = sl->family_data;
119 int i, min_page, max_page;
120 #else
121 u8 wrbuf[3];
122 #endif
124 count = w1_f23_fix_count(off, count, bin_attr->size);
125 if (!count)
126 return 0;
128 mutex_lock(&sl->master->bus_mutex);
130 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
132 min_page = (off >> W1_PAGE_BITS);
133 max_page = (off + count - 1) >> W1_PAGE_BITS;
134 for (i = min_page; i <= max_page; i++) {
135 if (w1_f23_refresh_block(sl, data, i)) {
136 count = -EIO;
137 goto out_up;
140 memcpy(buf, &data->memory[off], count);
142 #else /* CONFIG_W1_SLAVE_DS2433_CRC */
144 /* read directly from the EEPROM */
145 if (w1_reset_select_slave(sl)) {
146 count = -EIO;
147 goto out_up;
150 wrbuf[0] = W1_F23_READ_EEPROM;
151 wrbuf[1] = off & 0xff;
152 wrbuf[2] = off >> 8;
153 w1_write_block(sl->master, wrbuf, 3);
154 w1_read_block(sl->master, buf, count);
156 #endif /* CONFIG_W1_SLAVE_DS2433_CRC */
158 out_up:
159 mutex_unlock(&sl->master->bus_mutex);
161 return count;
165 * w1_f23_write() - Writes to the scratchpad and reads it back for verification.
166 * @sl: The slave structure
167 * @addr: Address for the write
168 * @len: length must be <= (W1_PAGE_SIZE - (addr & W1_PAGE_MASK))
169 * @data: The data to write
171 * Then copies the scratchpad to EEPROM.
172 * The data must be on one page.
173 * The master must be locked.
175 * Return: 0=Success, -1=failure
177 static int w1_f23_write(struct w1_slave *sl, int addr, int len, const u8 *data)
179 struct w1_f23_data *f23 = sl->family_data;
180 u8 wrbuf[4];
181 u8 rdbuf[W1_PAGE_SIZE + 3];
182 u8 es = (addr + len - 1) & 0x1f;
184 /* Write the data to the scratchpad */
185 if (w1_reset_select_slave(sl))
186 return -1;
188 wrbuf[0] = W1_F23_WRITE_SCRATCH;
189 wrbuf[1] = addr & 0xff;
190 wrbuf[2] = addr >> 8;
192 w1_write_block(sl->master, wrbuf, 3);
193 w1_write_block(sl->master, data, len);
195 /* Read the scratchpad and verify */
196 if (w1_reset_select_slave(sl))
197 return -1;
199 w1_write_8(sl->master, W1_F23_READ_SCRATCH);
200 w1_read_block(sl->master, rdbuf, len + 3);
202 /* Compare what was read against the data written */
203 if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
204 (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0))
205 return -1;
207 /* Copy the scratchpad to EEPROM */
208 if (w1_reset_select_slave(sl))
209 return -1;
211 wrbuf[0] = W1_F23_COPY_SCRATCH;
212 wrbuf[3] = es;
213 w1_write_block(sl->master, wrbuf, 4);
215 /* Sleep for tprog ms to wait for the write to complete */
216 msleep(f23->cfg->tprog);
218 /* Reset the bus to wake up the EEPROM (this may not be needed) */
219 w1_reset_bus(sl->master);
220 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
221 clear_bit(addr >> W1_PAGE_BITS, f23->validcrc);
222 #endif
223 return 0;
226 static ssize_t eeprom_write(struct file *filp, struct kobject *kobj,
227 struct bin_attribute *bin_attr, char *buf,
228 loff_t off, size_t count)
230 struct w1_slave *sl = kobj_to_w1_slave(kobj);
231 int addr, len, idx;
233 count = w1_f23_fix_count(off, count, bin_attr->size);
234 if (!count)
235 return 0;
237 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
238 /* can only write full blocks in cached mode */
239 if ((off & W1_PAGE_MASK) || (count & W1_PAGE_MASK)) {
240 dev_err(&sl->dev, "invalid offset/count off=%d cnt=%zd\n",
241 (int)off, count);
242 return -EINVAL;
245 /* make sure the block CRCs are valid */
246 for (idx = 0; idx < count; idx += W1_PAGE_SIZE) {
247 if (crc16(CRC16_INIT, &buf[idx], W1_PAGE_SIZE) != CRC16_VALID) {
248 dev_err(&sl->dev, "bad CRC at offset %d\n", (int)off);
249 return -EINVAL;
252 #endif /* CONFIG_W1_SLAVE_DS2433_CRC */
254 mutex_lock(&sl->master->bus_mutex);
256 /* Can only write data to one page at a time */
257 idx = 0;
258 while (idx < count) {
259 addr = off + idx;
260 len = W1_PAGE_SIZE - (addr & W1_PAGE_MASK);
261 if (len > (count - idx))
262 len = count - idx;
264 if (w1_f23_write(sl, addr, len, &buf[idx]) < 0) {
265 count = -EIO;
266 goto out_up;
268 idx += len;
271 out_up:
272 mutex_unlock(&sl->master->bus_mutex);
274 return count;
277 static struct bin_attribute bin_attr_f23_eeprom = {
278 .attr = { .name = "eeprom", .mode = 0644 },
279 .read = eeprom_read,
280 .write = eeprom_write,
281 .size = W1_EEPROM_DS2433_SIZE,
284 static struct bin_attribute bin_attr_f43_eeprom = {
285 .attr = { .name = "eeprom", .mode = 0644 },
286 .read = eeprom_read,
287 .write = eeprom_write,
288 .size = W1_EEPROM_DS28EC20_SIZE,
291 static struct bin_attribute *w1_f23_bin_attributes[] = {
292 &bin_attr_f23_eeprom,
293 NULL,
296 static const struct attribute_group w1_f23_group = {
297 .bin_attrs = w1_f23_bin_attributes,
300 static const struct attribute_group *w1_f23_groups[] = {
301 &w1_f23_group,
302 NULL,
305 static struct bin_attribute *w1_f43_bin_attributes[] = {
306 &bin_attr_f43_eeprom,
307 NULL,
310 static const struct attribute_group w1_f43_group = {
311 .bin_attrs = w1_f43_bin_attributes,
314 static const struct attribute_group *w1_f43_groups[] = {
315 &w1_f43_group,
316 NULL,
319 static int w1_f23_add_slave(struct w1_slave *sl)
321 struct w1_f23_data *data;
323 data = kzalloc(sizeof(struct w1_f23_data), GFP_KERNEL);
324 if (!data)
325 return -ENOMEM;
327 switch (sl->family->fid) {
328 case W1_EEPROM_DS2433:
329 data->cfg = &config_f23;
330 break;
331 case W1_EEPROM_DS28EC20:
332 data->cfg = &config_f43;
333 break;
336 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
337 if (data->cfg->page_count > W1_VALIDCRC_MAX) {
338 dev_err(&sl->dev, "page count too big for crc bitmap\n");
339 kfree(data);
340 return -EINVAL;
342 data->memory = kzalloc(data->cfg->eeprom_size, GFP_KERNEL);
343 if (!data->memory) {
344 kfree(data);
345 return -ENOMEM;
347 bitmap_zero(data->validcrc, data->cfg->page_count);
348 #endif /* CONFIG_W1_SLAVE_DS2433_CRC */
349 sl->family_data = data;
351 return 0;
354 static void w1_f23_remove_slave(struct w1_slave *sl)
356 struct w1_f23_data *data = sl->family_data;
357 sl->family_data = NULL;
358 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
359 kfree(data->memory);
360 #endif /* CONFIG_W1_SLAVE_DS2433_CRC */
361 kfree(data);
364 static const struct w1_family_ops w1_f23_fops = {
365 .add_slave = w1_f23_add_slave,
366 .remove_slave = w1_f23_remove_slave,
367 .groups = w1_f23_groups,
370 static const struct w1_family_ops w1_f43_fops = {
371 .add_slave = w1_f23_add_slave,
372 .remove_slave = w1_f23_remove_slave,
373 .groups = w1_f43_groups,
376 static struct w1_family w1_family_23 = {
377 .fid = W1_EEPROM_DS2433,
378 .fops = &w1_f23_fops,
381 static struct w1_family w1_family_43 = {
382 .fid = W1_EEPROM_DS28EC20,
383 .fops = &w1_f43_fops,
386 static int __init w1_ds2433_init(void)
388 int err;
390 err = w1_register_family(&w1_family_23);
391 if (err)
392 return err;
394 err = w1_register_family(&w1_family_43);
395 if (err)
396 goto err_43;
398 return 0;
400 err_43:
401 w1_unregister_family(&w1_family_23);
402 return err;
405 static void __exit w1_ds2433_exit(void)
407 w1_unregister_family(&w1_family_23);
408 w1_unregister_family(&w1_family_43);
411 module_init(w1_ds2433_init);
412 module_exit(w1_ds2433_exit);
414 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
415 MODULE_AUTHOR("Marc Ferland <marc.ferland@sonatest.com>");
416 MODULE_DESCRIPTION("w1 family 23/43 driver for DS2433 (4kb) and DS28EC20 (20kb)");
417 MODULE_LICENSE("GPL");
418 MODULE_ALIAS("w1-family-" __stringify(W1_EEPROM_DS2433));
419 MODULE_ALIAS("w1-family-" __stringify(W1_EEPROM_DS28EC20));