18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * w1_ds2805 - w1 family 0d (DS28E05) driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2016 Andrew Worsley amworsley@gmail.com 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/kernel.h> 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/moduleparam.h> 118c2ecf20Sopenharmony_ci#include <linux/device.h> 128c2ecf20Sopenharmony_ci#include <linux/types.h> 138c2ecf20Sopenharmony_ci#include <linux/delay.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include <linux/w1.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#define W1_EEPROM_DS2805 0x0D 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#define W1_F0D_EEPROM_SIZE 128 208c2ecf20Sopenharmony_ci#define W1_F0D_PAGE_BITS 3 218c2ecf20Sopenharmony_ci#define W1_F0D_PAGE_SIZE (1<<W1_F0D_PAGE_BITS) 228c2ecf20Sopenharmony_ci#define W1_F0D_PAGE_MASK 0x0F 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define W1_F0D_SCRATCH_BITS 1 258c2ecf20Sopenharmony_ci#define W1_F0D_SCRATCH_SIZE (1<<W1_F0D_SCRATCH_BITS) 268c2ecf20Sopenharmony_ci#define W1_F0D_SCRATCH_MASK (W1_F0D_SCRATCH_SIZE-1) 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define W1_F0D_READ_EEPROM 0xF0 298c2ecf20Sopenharmony_ci#define W1_F0D_WRITE_EEPROM 0x55 308c2ecf20Sopenharmony_ci#define W1_F0D_RELEASE 0xFF 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#define W1_F0D_CS_OK 0xAA /* Chip Status Ok */ 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci#define W1_F0D_TPROG_MS 16 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci#define W1_F0D_READ_RETRIES 10 378c2ecf20Sopenharmony_ci#define W1_F0D_READ_MAXLEN W1_F0D_EEPROM_SIZE 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci/* 408c2ecf20Sopenharmony_ci * Check the file size bounds and adjusts count as needed. 418c2ecf20Sopenharmony_ci * This would not be needed if the file size didn't reset to 0 after a write. 428c2ecf20Sopenharmony_ci */ 438c2ecf20Sopenharmony_cistatic inline size_t w1_f0d_fix_count(loff_t off, size_t count, size_t size) 448c2ecf20Sopenharmony_ci{ 458c2ecf20Sopenharmony_ci if (off > size) 468c2ecf20Sopenharmony_ci return 0; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci if ((off + count) > size) 498c2ecf20Sopenharmony_ci return size - off; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci return count; 528c2ecf20Sopenharmony_ci} 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci/* 558c2ecf20Sopenharmony_ci * Read a block from W1 ROM two times and compares the results. 568c2ecf20Sopenharmony_ci * If they are equal they are returned, otherwise the read 578c2ecf20Sopenharmony_ci * is repeated W1_F0D_READ_RETRIES times. 588c2ecf20Sopenharmony_ci * 598c2ecf20Sopenharmony_ci * count must not exceed W1_F0D_READ_MAXLEN. 608c2ecf20Sopenharmony_ci */ 618c2ecf20Sopenharmony_cistatic int w1_f0d_readblock(struct w1_slave *sl, int off, int count, char *buf) 628c2ecf20Sopenharmony_ci{ 638c2ecf20Sopenharmony_ci u8 wrbuf[3]; 648c2ecf20Sopenharmony_ci u8 cmp[W1_F0D_READ_MAXLEN]; 658c2ecf20Sopenharmony_ci int tries = W1_F0D_READ_RETRIES; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci do { 688c2ecf20Sopenharmony_ci wrbuf[0] = W1_F0D_READ_EEPROM; 698c2ecf20Sopenharmony_ci wrbuf[1] = off & 0x7f; 708c2ecf20Sopenharmony_ci wrbuf[2] = 0; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci if (w1_reset_select_slave(sl)) 738c2ecf20Sopenharmony_ci return -1; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci w1_write_block(sl->master, wrbuf, sizeof(wrbuf)); 768c2ecf20Sopenharmony_ci w1_read_block(sl->master, buf, count); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci if (w1_reset_select_slave(sl)) 798c2ecf20Sopenharmony_ci return -1; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci w1_write_block(sl->master, wrbuf, sizeof(wrbuf)); 828c2ecf20Sopenharmony_ci w1_read_block(sl->master, cmp, count); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci if (!memcmp(cmp, buf, count)) 858c2ecf20Sopenharmony_ci return 0; 868c2ecf20Sopenharmony_ci } while (--tries); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci dev_err(&sl->dev, "proof reading failed %d times\n", 898c2ecf20Sopenharmony_ci W1_F0D_READ_RETRIES); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci return -1; 928c2ecf20Sopenharmony_ci} 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_cistatic ssize_t w1_f0d_read_bin(struct file *filp, struct kobject *kobj, 958c2ecf20Sopenharmony_ci struct bin_attribute *bin_attr, 968c2ecf20Sopenharmony_ci char *buf, loff_t off, size_t count) 978c2ecf20Sopenharmony_ci{ 988c2ecf20Sopenharmony_ci struct w1_slave *sl = kobj_to_w1_slave(kobj); 998c2ecf20Sopenharmony_ci int todo = count; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci count = w1_f0d_fix_count(off, count, W1_F0D_EEPROM_SIZE); 1028c2ecf20Sopenharmony_ci if (count == 0) 1038c2ecf20Sopenharmony_ci return 0; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci mutex_lock(&sl->master->mutex); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci /* read directly from the EEPROM in chunks of W1_F0D_READ_MAXLEN */ 1088c2ecf20Sopenharmony_ci while (todo > 0) { 1098c2ecf20Sopenharmony_ci int block_read; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci if (todo >= W1_F0D_READ_MAXLEN) 1128c2ecf20Sopenharmony_ci block_read = W1_F0D_READ_MAXLEN; 1138c2ecf20Sopenharmony_ci else 1148c2ecf20Sopenharmony_ci block_read = todo; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci if (w1_f0d_readblock(sl, off, block_read, buf) < 0) { 1178c2ecf20Sopenharmony_ci count = -EIO; 1188c2ecf20Sopenharmony_ci break; 1198c2ecf20Sopenharmony_ci } 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci todo -= W1_F0D_READ_MAXLEN; 1228c2ecf20Sopenharmony_ci buf += W1_F0D_READ_MAXLEN; 1238c2ecf20Sopenharmony_ci off += W1_F0D_READ_MAXLEN; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci mutex_unlock(&sl->master->mutex); 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci return count; 1298c2ecf20Sopenharmony_ci} 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci/* 1328c2ecf20Sopenharmony_ci * Writes to the scratchpad and reads it back for verification. 1338c2ecf20Sopenharmony_ci * Then copies the scratchpad to EEPROM. 1348c2ecf20Sopenharmony_ci * The data must be aligned at W1_F0D_SCRATCH_SIZE bytes and 1358c2ecf20Sopenharmony_ci * must be W1_F0D_SCRATCH_SIZE bytes long. 1368c2ecf20Sopenharmony_ci * The master must be locked. 1378c2ecf20Sopenharmony_ci * 1388c2ecf20Sopenharmony_ci * @param sl The slave structure 1398c2ecf20Sopenharmony_ci * @param addr Address for the write 1408c2ecf20Sopenharmony_ci * @param len length must be <= (W1_F0D_PAGE_SIZE - (addr & W1_F0D_PAGE_MASK)) 1418c2ecf20Sopenharmony_ci * @param data The data to write 1428c2ecf20Sopenharmony_ci * @return 0=Success -1=failure 1438c2ecf20Sopenharmony_ci */ 1448c2ecf20Sopenharmony_cistatic int w1_f0d_write(struct w1_slave *sl, int addr, int len, const u8 *data) 1458c2ecf20Sopenharmony_ci{ 1468c2ecf20Sopenharmony_ci int tries = W1_F0D_READ_RETRIES; 1478c2ecf20Sopenharmony_ci u8 wrbuf[3]; 1488c2ecf20Sopenharmony_ci u8 rdbuf[W1_F0D_SCRATCH_SIZE]; 1498c2ecf20Sopenharmony_ci u8 cs; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci if ((addr & 1) || (len != 2)) { 1528c2ecf20Sopenharmony_ci dev_err(&sl->dev, "%s: bad addr/len - addr=%#x len=%d\n", 1538c2ecf20Sopenharmony_ci __func__, addr, len); 1548c2ecf20Sopenharmony_ci return -1; 1558c2ecf20Sopenharmony_ci } 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ciretry: 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci /* Write the data to the scratchpad */ 1608c2ecf20Sopenharmony_ci if (w1_reset_select_slave(sl)) 1618c2ecf20Sopenharmony_ci return -1; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci wrbuf[0] = W1_F0D_WRITE_EEPROM; 1648c2ecf20Sopenharmony_ci wrbuf[1] = addr & 0xff; 1658c2ecf20Sopenharmony_ci wrbuf[2] = 0xff; /* ?? from Example */ 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci w1_write_block(sl->master, wrbuf, sizeof(wrbuf)); 1688c2ecf20Sopenharmony_ci w1_write_block(sl->master, data, len); 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci w1_read_block(sl->master, rdbuf, sizeof(rdbuf)); 1718c2ecf20Sopenharmony_ci /* Compare what was read against the data written */ 1728c2ecf20Sopenharmony_ci if ((rdbuf[0] != data[0]) || (rdbuf[1] != data[1])) { 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci if (--tries) 1758c2ecf20Sopenharmony_ci goto retry; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci dev_err(&sl->dev, 1788c2ecf20Sopenharmony_ci "could not write to eeprom, scratchpad compare failed %d times\n", 1798c2ecf20Sopenharmony_ci W1_F0D_READ_RETRIES); 1808c2ecf20Sopenharmony_ci pr_info("%s: rdbuf = %#x %#x data = %#x %#x\n", 1818c2ecf20Sopenharmony_ci __func__, rdbuf[0], rdbuf[1], data[0], data[1]); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci return -1; 1848c2ecf20Sopenharmony_ci } 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci /* Trigger write out to EEPROM */ 1878c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_F0D_RELEASE); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci /* Sleep for tprog ms to wait for the write to complete */ 1908c2ecf20Sopenharmony_ci msleep(W1_F0D_TPROG_MS); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci /* Check CS (Command Status) == 0xAA ? */ 1938c2ecf20Sopenharmony_ci cs = w1_read_8(sl->master); 1948c2ecf20Sopenharmony_ci if (cs != W1_F0D_CS_OK) { 1958c2ecf20Sopenharmony_ci dev_err(&sl->dev, "save to eeprom failed = CS=%#x\n", cs); 1968c2ecf20Sopenharmony_ci return -1; 1978c2ecf20Sopenharmony_ci } 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci return 0; 2008c2ecf20Sopenharmony_ci} 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_cistatic ssize_t w1_f0d_write_bin(struct file *filp, struct kobject *kobj, 2038c2ecf20Sopenharmony_ci struct bin_attribute *bin_attr, 2048c2ecf20Sopenharmony_ci char *buf, loff_t off, size_t count) 2058c2ecf20Sopenharmony_ci{ 2068c2ecf20Sopenharmony_ci struct w1_slave *sl = kobj_to_w1_slave(kobj); 2078c2ecf20Sopenharmony_ci int addr, len; 2088c2ecf20Sopenharmony_ci int copy; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci count = w1_f0d_fix_count(off, count, W1_F0D_EEPROM_SIZE); 2118c2ecf20Sopenharmony_ci if (count == 0) 2128c2ecf20Sopenharmony_ci return 0; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci mutex_lock(&sl->master->mutex); 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci /* Can only write data in blocks of the size of the scratchpad */ 2178c2ecf20Sopenharmony_ci addr = off; 2188c2ecf20Sopenharmony_ci len = count; 2198c2ecf20Sopenharmony_ci while (len > 0) { 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci /* if len too short or addr not aligned */ 2228c2ecf20Sopenharmony_ci if (len < W1_F0D_SCRATCH_SIZE || addr & W1_F0D_SCRATCH_MASK) { 2238c2ecf20Sopenharmony_ci char tmp[W1_F0D_SCRATCH_SIZE]; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci /* read the block and update the parts to be written */ 2268c2ecf20Sopenharmony_ci if (w1_f0d_readblock(sl, addr & ~W1_F0D_SCRATCH_MASK, 2278c2ecf20Sopenharmony_ci W1_F0D_SCRATCH_SIZE, tmp)) { 2288c2ecf20Sopenharmony_ci count = -EIO; 2298c2ecf20Sopenharmony_ci goto out_up; 2308c2ecf20Sopenharmony_ci } 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci /* copy at most to the boundary of the PAGE or len */ 2338c2ecf20Sopenharmony_ci copy = W1_F0D_SCRATCH_SIZE - 2348c2ecf20Sopenharmony_ci (addr & W1_F0D_SCRATCH_MASK); 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci if (copy > len) 2378c2ecf20Sopenharmony_ci copy = len; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci memcpy(&tmp[addr & W1_F0D_SCRATCH_MASK], buf, copy); 2408c2ecf20Sopenharmony_ci if (w1_f0d_write(sl, addr & ~W1_F0D_SCRATCH_MASK, 2418c2ecf20Sopenharmony_ci W1_F0D_SCRATCH_SIZE, tmp) < 0) { 2428c2ecf20Sopenharmony_ci count = -EIO; 2438c2ecf20Sopenharmony_ci goto out_up; 2448c2ecf20Sopenharmony_ci } 2458c2ecf20Sopenharmony_ci } else { 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci copy = W1_F0D_SCRATCH_SIZE; 2488c2ecf20Sopenharmony_ci if (w1_f0d_write(sl, addr, copy, buf) < 0) { 2498c2ecf20Sopenharmony_ci count = -EIO; 2508c2ecf20Sopenharmony_ci goto out_up; 2518c2ecf20Sopenharmony_ci } 2528c2ecf20Sopenharmony_ci } 2538c2ecf20Sopenharmony_ci buf += copy; 2548c2ecf20Sopenharmony_ci addr += copy; 2558c2ecf20Sopenharmony_ci len -= copy; 2568c2ecf20Sopenharmony_ci } 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ciout_up: 2598c2ecf20Sopenharmony_ci mutex_unlock(&sl->master->mutex); 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci return count; 2628c2ecf20Sopenharmony_ci} 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_cistatic struct bin_attribute w1_f0d_bin_attr = { 2658c2ecf20Sopenharmony_ci .attr = { 2668c2ecf20Sopenharmony_ci .name = "eeprom", 2678c2ecf20Sopenharmony_ci .mode = S_IRUGO | S_IWUSR, 2688c2ecf20Sopenharmony_ci }, 2698c2ecf20Sopenharmony_ci .size = W1_F0D_EEPROM_SIZE, 2708c2ecf20Sopenharmony_ci .read = w1_f0d_read_bin, 2718c2ecf20Sopenharmony_ci .write = w1_f0d_write_bin, 2728c2ecf20Sopenharmony_ci}; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_cistatic int w1_f0d_add_slave(struct w1_slave *sl) 2758c2ecf20Sopenharmony_ci{ 2768c2ecf20Sopenharmony_ci return sysfs_create_bin_file(&sl->dev.kobj, &w1_f0d_bin_attr); 2778c2ecf20Sopenharmony_ci} 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_cistatic void w1_f0d_remove_slave(struct w1_slave *sl) 2808c2ecf20Sopenharmony_ci{ 2818c2ecf20Sopenharmony_ci sysfs_remove_bin_file(&sl->dev.kobj, &w1_f0d_bin_attr); 2828c2ecf20Sopenharmony_ci} 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_cistatic const struct w1_family_ops w1_f0d_fops = { 2858c2ecf20Sopenharmony_ci .add_slave = w1_f0d_add_slave, 2868c2ecf20Sopenharmony_ci .remove_slave = w1_f0d_remove_slave, 2878c2ecf20Sopenharmony_ci}; 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_cistatic struct w1_family w1_family_0d = { 2908c2ecf20Sopenharmony_ci .fid = W1_EEPROM_DS2805, 2918c2ecf20Sopenharmony_ci .fops = &w1_f0d_fops, 2928c2ecf20Sopenharmony_ci}; 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_cistatic int __init w1_f0d_init(void) 2958c2ecf20Sopenharmony_ci{ 2968c2ecf20Sopenharmony_ci pr_info("%s()\n", __func__); 2978c2ecf20Sopenharmony_ci return w1_register_family(&w1_family_0d); 2988c2ecf20Sopenharmony_ci} 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_cistatic void __exit w1_f0d_fini(void) 3018c2ecf20Sopenharmony_ci{ 3028c2ecf20Sopenharmony_ci pr_info("%s()\n", __func__); 3038c2ecf20Sopenharmony_ci w1_unregister_family(&w1_family_0d); 3048c2ecf20Sopenharmony_ci} 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_cimodule_init(w1_f0d_init); 3078c2ecf20Sopenharmony_cimodule_exit(w1_f0d_fini); 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 3108c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andrew Worsley amworsley@gmail.com"); 3118c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("w1 family 0d driver for DS2805, 1kb EEPROM"); 312