18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *	w1_ds28e04.c - w1 family 1C (DS28E04) driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2012 Markus Franke <franke.m@sebakmt.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#include <linux/slab.h>
158c2ecf20Sopenharmony_ci#include <linux/crc16.h>
168c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define CRC16_INIT		0
198c2ecf20Sopenharmony_ci#define CRC16_VALID		0xb001
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <linux/w1.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define W1_FAMILY_DS28E04	0x1C
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* Allow the strong pullup to be disabled, but default to enabled.
268c2ecf20Sopenharmony_ci * If it was disabled a parasite powered device might not get the required
278c2ecf20Sopenharmony_ci * current to copy the data from the scratchpad to EEPROM.  If it is enabled
288c2ecf20Sopenharmony_ci * parasite powered devices have a better chance of getting the current
298c2ecf20Sopenharmony_ci * required.
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_cistatic int w1_strong_pullup = 1;
328c2ecf20Sopenharmony_cimodule_param_named(strong_pullup, w1_strong_pullup, int, 0);
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* enable/disable CRC checking on DS28E04-100 memory accesses */
358c2ecf20Sopenharmony_cistatic bool w1_enable_crccheck = true;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define W1_EEPROM_SIZE		512
388c2ecf20Sopenharmony_ci#define W1_PAGE_COUNT		16
398c2ecf20Sopenharmony_ci#define W1_PAGE_SIZE		32
408c2ecf20Sopenharmony_ci#define W1_PAGE_BITS		5
418c2ecf20Sopenharmony_ci#define W1_PAGE_MASK		0x1F
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define W1_F1C_READ_EEPROM	0xF0
448c2ecf20Sopenharmony_ci#define W1_F1C_WRITE_SCRATCH	0x0F
458c2ecf20Sopenharmony_ci#define W1_F1C_READ_SCRATCH	0xAA
468c2ecf20Sopenharmony_ci#define W1_F1C_COPY_SCRATCH	0x55
478c2ecf20Sopenharmony_ci#define W1_F1C_ACCESS_WRITE	0x5A
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define W1_1C_REG_LOGIC_STATE	0x220
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistruct w1_f1C_data {
528c2ecf20Sopenharmony_ci	u8	memory[W1_EEPROM_SIZE];
538c2ecf20Sopenharmony_ci	u32	validcrc;
548c2ecf20Sopenharmony_ci};
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/**
578c2ecf20Sopenharmony_ci * Check the file size bounds and adjusts count as needed.
588c2ecf20Sopenharmony_ci * This would not be needed if the file size didn't reset to 0 after a write.
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_cistatic inline size_t w1_f1C_fix_count(loff_t off, size_t count, size_t size)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	if (off > size)
638c2ecf20Sopenharmony_ci		return 0;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	if ((off + count) > size)
668c2ecf20Sopenharmony_ci		return size - off;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	return count;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic int w1_f1C_refresh_block(struct w1_slave *sl, struct w1_f1C_data *data,
728c2ecf20Sopenharmony_ci				int block)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	u8	wrbuf[3];
758c2ecf20Sopenharmony_ci	int	off = block * W1_PAGE_SIZE;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	if (data->validcrc & (1 << block))
788c2ecf20Sopenharmony_ci		return 0;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	if (w1_reset_select_slave(sl)) {
818c2ecf20Sopenharmony_ci		data->validcrc = 0;
828c2ecf20Sopenharmony_ci		return -EIO;
838c2ecf20Sopenharmony_ci	}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	wrbuf[0] = W1_F1C_READ_EEPROM;
868c2ecf20Sopenharmony_ci	wrbuf[1] = off & 0xff;
878c2ecf20Sopenharmony_ci	wrbuf[2] = off >> 8;
888c2ecf20Sopenharmony_ci	w1_write_block(sl->master, wrbuf, 3);
898c2ecf20Sopenharmony_ci	w1_read_block(sl->master, &data->memory[off], W1_PAGE_SIZE);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	/* cache the block if the CRC is valid */
928c2ecf20Sopenharmony_ci	if (crc16(CRC16_INIT, &data->memory[off], W1_PAGE_SIZE) == CRC16_VALID)
938c2ecf20Sopenharmony_ci		data->validcrc |= (1 << block);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	return 0;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic int w1_f1C_read(struct w1_slave *sl, int addr, int len, char *data)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	u8 wrbuf[3];
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	/* read directly from the EEPROM */
1038c2ecf20Sopenharmony_ci	if (w1_reset_select_slave(sl))
1048c2ecf20Sopenharmony_ci		return -EIO;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	wrbuf[0] = W1_F1C_READ_EEPROM;
1078c2ecf20Sopenharmony_ci	wrbuf[1] = addr & 0xff;
1088c2ecf20Sopenharmony_ci	wrbuf[2] = addr >> 8;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	w1_write_block(sl->master, wrbuf, sizeof(wrbuf));
1118c2ecf20Sopenharmony_ci	return w1_read_block(sl->master, data, len);
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
1158c2ecf20Sopenharmony_ci			   struct bin_attribute *bin_attr, char *buf,
1168c2ecf20Sopenharmony_ci			   loff_t off, size_t count)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct w1_slave *sl = kobj_to_w1_slave(kobj);
1198c2ecf20Sopenharmony_ci	struct w1_f1C_data *data = sl->family_data;
1208c2ecf20Sopenharmony_ci	int i, min_page, max_page;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	count = w1_f1C_fix_count(off, count, W1_EEPROM_SIZE);
1238c2ecf20Sopenharmony_ci	if (count == 0)
1248c2ecf20Sopenharmony_ci		return 0;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	mutex_lock(&sl->master->mutex);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	if (w1_enable_crccheck) {
1298c2ecf20Sopenharmony_ci		min_page = (off >> W1_PAGE_BITS);
1308c2ecf20Sopenharmony_ci		max_page = (off + count - 1) >> W1_PAGE_BITS;
1318c2ecf20Sopenharmony_ci		for (i = min_page; i <= max_page; i++) {
1328c2ecf20Sopenharmony_ci			if (w1_f1C_refresh_block(sl, data, i)) {
1338c2ecf20Sopenharmony_ci				count = -EIO;
1348c2ecf20Sopenharmony_ci				goto out_up;
1358c2ecf20Sopenharmony_ci			}
1368c2ecf20Sopenharmony_ci		}
1378c2ecf20Sopenharmony_ci		memcpy(buf, &data->memory[off], count);
1388c2ecf20Sopenharmony_ci	} else {
1398c2ecf20Sopenharmony_ci		count = w1_f1C_read(sl, off, count, buf);
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ciout_up:
1438c2ecf20Sopenharmony_ci	mutex_unlock(&sl->master->mutex);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	return count;
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci/**
1498c2ecf20Sopenharmony_ci * Writes to the scratchpad and reads it back for verification.
1508c2ecf20Sopenharmony_ci * Then copies the scratchpad to EEPROM.
1518c2ecf20Sopenharmony_ci * The data must be on one page.
1528c2ecf20Sopenharmony_ci * The master must be locked.
1538c2ecf20Sopenharmony_ci *
1548c2ecf20Sopenharmony_ci * @param sl	The slave structure
1558c2ecf20Sopenharmony_ci * @param addr	Address for the write
1568c2ecf20Sopenharmony_ci * @param len   length must be <= (W1_PAGE_SIZE - (addr & W1_PAGE_MASK))
1578c2ecf20Sopenharmony_ci * @param data	The data to write
1588c2ecf20Sopenharmony_ci * @return	0=Success -1=failure
1598c2ecf20Sopenharmony_ci */
1608c2ecf20Sopenharmony_cistatic int w1_f1C_write(struct w1_slave *sl, int addr, int len, const u8 *data)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	u8 wrbuf[4];
1638c2ecf20Sopenharmony_ci	u8 rdbuf[W1_PAGE_SIZE + 3];
1648c2ecf20Sopenharmony_ci	u8 es = (addr + len - 1) & 0x1f;
1658c2ecf20Sopenharmony_ci	unsigned int tm = 10;
1668c2ecf20Sopenharmony_ci	int i;
1678c2ecf20Sopenharmony_ci	struct w1_f1C_data *f1C = sl->family_data;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	/* Write the data to the scratchpad */
1708c2ecf20Sopenharmony_ci	if (w1_reset_select_slave(sl))
1718c2ecf20Sopenharmony_ci		return -1;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	wrbuf[0] = W1_F1C_WRITE_SCRATCH;
1748c2ecf20Sopenharmony_ci	wrbuf[1] = addr & 0xff;
1758c2ecf20Sopenharmony_ci	wrbuf[2] = addr >> 8;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	w1_write_block(sl->master, wrbuf, 3);
1788c2ecf20Sopenharmony_ci	w1_write_block(sl->master, data, len);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	/* Read the scratchpad and verify */
1818c2ecf20Sopenharmony_ci	if (w1_reset_select_slave(sl))
1828c2ecf20Sopenharmony_ci		return -1;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	w1_write_8(sl->master, W1_F1C_READ_SCRATCH);
1858c2ecf20Sopenharmony_ci	w1_read_block(sl->master, rdbuf, len + 3);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	/* Compare what was read against the data written */
1888c2ecf20Sopenharmony_ci	if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
1898c2ecf20Sopenharmony_ci	    (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0))
1908c2ecf20Sopenharmony_ci		return -1;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/* Copy the scratchpad to EEPROM */
1938c2ecf20Sopenharmony_ci	if (w1_reset_select_slave(sl))
1948c2ecf20Sopenharmony_ci		return -1;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	wrbuf[0] = W1_F1C_COPY_SCRATCH;
1978c2ecf20Sopenharmony_ci	wrbuf[3] = es;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	for (i = 0; i < sizeof(wrbuf); ++i) {
2008c2ecf20Sopenharmony_ci		/* issue 10ms strong pullup (or delay) on the last byte
2018c2ecf20Sopenharmony_ci		   for writing the data from the scratchpad to EEPROM */
2028c2ecf20Sopenharmony_ci		if (w1_strong_pullup && i == sizeof(wrbuf)-1)
2038c2ecf20Sopenharmony_ci			w1_next_pullup(sl->master, tm);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci		w1_write_8(sl->master, wrbuf[i]);
2068c2ecf20Sopenharmony_ci	}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	if (!w1_strong_pullup)
2098c2ecf20Sopenharmony_ci		msleep(tm);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	if (w1_enable_crccheck) {
2128c2ecf20Sopenharmony_ci		/* invalidate cached data */
2138c2ecf20Sopenharmony_ci		f1C->validcrc &= ~(1 << (addr >> W1_PAGE_BITS));
2148c2ecf20Sopenharmony_ci	}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	/* Reset the bus to wake up the EEPROM (this may not be needed) */
2178c2ecf20Sopenharmony_ci	w1_reset_bus(sl->master);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	return 0;
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic ssize_t eeprom_write(struct file *filp, struct kobject *kobj,
2238c2ecf20Sopenharmony_ci			    struct bin_attribute *bin_attr, char *buf,
2248c2ecf20Sopenharmony_ci			    loff_t off, size_t count)
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	struct w1_slave *sl = kobj_to_w1_slave(kobj);
2288c2ecf20Sopenharmony_ci	int addr, len, idx;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	count = w1_f1C_fix_count(off, count, W1_EEPROM_SIZE);
2318c2ecf20Sopenharmony_ci	if (count == 0)
2328c2ecf20Sopenharmony_ci		return 0;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	if (w1_enable_crccheck) {
2358c2ecf20Sopenharmony_ci		/* can only write full blocks in cached mode */
2368c2ecf20Sopenharmony_ci		if ((off & W1_PAGE_MASK) || (count & W1_PAGE_MASK)) {
2378c2ecf20Sopenharmony_ci			dev_err(&sl->dev, "invalid offset/count off=%d cnt=%zd\n",
2388c2ecf20Sopenharmony_ci				(int)off, count);
2398c2ecf20Sopenharmony_ci			return -EINVAL;
2408c2ecf20Sopenharmony_ci		}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci		/* make sure the block CRCs are valid */
2438c2ecf20Sopenharmony_ci		for (idx = 0; idx < count; idx += W1_PAGE_SIZE) {
2448c2ecf20Sopenharmony_ci			if (crc16(CRC16_INIT, &buf[idx], W1_PAGE_SIZE)
2458c2ecf20Sopenharmony_ci				!= CRC16_VALID) {
2468c2ecf20Sopenharmony_ci				dev_err(&sl->dev, "bad CRC at offset %d\n",
2478c2ecf20Sopenharmony_ci					(int)off);
2488c2ecf20Sopenharmony_ci				return -EINVAL;
2498c2ecf20Sopenharmony_ci			}
2508c2ecf20Sopenharmony_ci		}
2518c2ecf20Sopenharmony_ci	}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	mutex_lock(&sl->master->mutex);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	/* Can only write data to one page at a time */
2568c2ecf20Sopenharmony_ci	idx = 0;
2578c2ecf20Sopenharmony_ci	while (idx < count) {
2588c2ecf20Sopenharmony_ci		addr = off + idx;
2598c2ecf20Sopenharmony_ci		len = W1_PAGE_SIZE - (addr & W1_PAGE_MASK);
2608c2ecf20Sopenharmony_ci		if (len > (count - idx))
2618c2ecf20Sopenharmony_ci			len = count - idx;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci		if (w1_f1C_write(sl, addr, len, &buf[idx]) < 0) {
2648c2ecf20Sopenharmony_ci			count = -EIO;
2658c2ecf20Sopenharmony_ci			goto out_up;
2668c2ecf20Sopenharmony_ci		}
2678c2ecf20Sopenharmony_ci		idx += len;
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ciout_up:
2718c2ecf20Sopenharmony_ci	mutex_unlock(&sl->master->mutex);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	return count;
2748c2ecf20Sopenharmony_ci}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic BIN_ATTR_RW(eeprom, W1_EEPROM_SIZE);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_cistatic ssize_t pio_read(struct file *filp, struct kobject *kobj,
2798c2ecf20Sopenharmony_ci			struct bin_attribute *bin_attr, char *buf, loff_t off,
2808c2ecf20Sopenharmony_ci			size_t count)
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	struct w1_slave *sl = kobj_to_w1_slave(kobj);
2848c2ecf20Sopenharmony_ci	int ret;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	/* check arguments */
2878c2ecf20Sopenharmony_ci	if (off != 0 || count != 1 || buf == NULL)
2888c2ecf20Sopenharmony_ci		return -EINVAL;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	mutex_lock(&sl->master->mutex);
2918c2ecf20Sopenharmony_ci	ret = w1_f1C_read(sl, W1_1C_REG_LOGIC_STATE, count, buf);
2928c2ecf20Sopenharmony_ci	mutex_unlock(&sl->master->mutex);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	return ret;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_cistatic ssize_t pio_write(struct file *filp, struct kobject *kobj,
2988c2ecf20Sopenharmony_ci			 struct bin_attribute *bin_attr, char *buf, loff_t off,
2998c2ecf20Sopenharmony_ci			 size_t count)
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	struct w1_slave *sl = kobj_to_w1_slave(kobj);
3038c2ecf20Sopenharmony_ci	u8 wrbuf[3];
3048c2ecf20Sopenharmony_ci	u8 ack;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	/* check arguments */
3078c2ecf20Sopenharmony_ci	if (off != 0 || count != 1 || buf == NULL)
3088c2ecf20Sopenharmony_ci		return -EINVAL;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	mutex_lock(&sl->master->mutex);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	/* Write the PIO data */
3138c2ecf20Sopenharmony_ci	if (w1_reset_select_slave(sl)) {
3148c2ecf20Sopenharmony_ci		mutex_unlock(&sl->master->mutex);
3158c2ecf20Sopenharmony_ci		return -1;
3168c2ecf20Sopenharmony_ci	}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	/* set bit 7..2 to value '1' */
3198c2ecf20Sopenharmony_ci	*buf = *buf | 0xFC;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	wrbuf[0] = W1_F1C_ACCESS_WRITE;
3228c2ecf20Sopenharmony_ci	wrbuf[1] = *buf;
3238c2ecf20Sopenharmony_ci	wrbuf[2] = ~(*buf);
3248c2ecf20Sopenharmony_ci	w1_write_block(sl->master, wrbuf, 3);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	w1_read_block(sl->master, &ack, sizeof(ack));
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	mutex_unlock(&sl->master->mutex);
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	/* check for acknowledgement */
3318c2ecf20Sopenharmony_ci	if (ack != 0xAA)
3328c2ecf20Sopenharmony_ci		return -EIO;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	return count;
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistatic BIN_ATTR_RW(pio, 1);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic ssize_t crccheck_show(struct device *dev, struct device_attribute *attr,
3408c2ecf20Sopenharmony_ci			     char *buf)
3418c2ecf20Sopenharmony_ci{
3428c2ecf20Sopenharmony_ci	return sysfs_emit(buf, "%d\n", w1_enable_crccheck);
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic ssize_t crccheck_store(struct device *dev, struct device_attribute *attr,
3468c2ecf20Sopenharmony_ci			      const char *buf, size_t count)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	int err = kstrtobool(buf, &w1_enable_crccheck);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	if (err)
3518c2ecf20Sopenharmony_ci		return err;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	return count;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(crccheck);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_cistatic struct attribute *w1_f1C_attrs[] = {
3598c2ecf20Sopenharmony_ci	&dev_attr_crccheck.attr,
3608c2ecf20Sopenharmony_ci	NULL,
3618c2ecf20Sopenharmony_ci};
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic struct bin_attribute *w1_f1C_bin_attrs[] = {
3648c2ecf20Sopenharmony_ci	&bin_attr_eeprom,
3658c2ecf20Sopenharmony_ci	&bin_attr_pio,
3668c2ecf20Sopenharmony_ci	NULL,
3678c2ecf20Sopenharmony_ci};
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_cistatic const struct attribute_group w1_f1C_group = {
3708c2ecf20Sopenharmony_ci	.attrs		= w1_f1C_attrs,
3718c2ecf20Sopenharmony_ci	.bin_attrs	= w1_f1C_bin_attrs,
3728c2ecf20Sopenharmony_ci};
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_cistatic const struct attribute_group *w1_f1C_groups[] = {
3758c2ecf20Sopenharmony_ci	&w1_f1C_group,
3768c2ecf20Sopenharmony_ci	NULL,
3778c2ecf20Sopenharmony_ci};
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_cistatic int w1_f1C_add_slave(struct w1_slave *sl)
3808c2ecf20Sopenharmony_ci{
3818c2ecf20Sopenharmony_ci	struct w1_f1C_data *data = NULL;
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	if (w1_enable_crccheck) {
3848c2ecf20Sopenharmony_ci		data = kzalloc(sizeof(struct w1_f1C_data), GFP_KERNEL);
3858c2ecf20Sopenharmony_ci		if (!data)
3868c2ecf20Sopenharmony_ci			return -ENOMEM;
3878c2ecf20Sopenharmony_ci		sl->family_data = data;
3888c2ecf20Sopenharmony_ci	}
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	return 0;
3918c2ecf20Sopenharmony_ci}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_cistatic void w1_f1C_remove_slave(struct w1_slave *sl)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	kfree(sl->family_data);
3968c2ecf20Sopenharmony_ci	sl->family_data = NULL;
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_cistatic const struct w1_family_ops w1_f1C_fops = {
4008c2ecf20Sopenharmony_ci	.add_slave      = w1_f1C_add_slave,
4018c2ecf20Sopenharmony_ci	.remove_slave   = w1_f1C_remove_slave,
4028c2ecf20Sopenharmony_ci	.groups		= w1_f1C_groups,
4038c2ecf20Sopenharmony_ci};
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_cistatic struct w1_family w1_family_1C = {
4068c2ecf20Sopenharmony_ci	.fid = W1_FAMILY_DS28E04,
4078c2ecf20Sopenharmony_ci	.fops = &w1_f1C_fops,
4088c2ecf20Sopenharmony_ci};
4098c2ecf20Sopenharmony_cimodule_w1_family(w1_family_1C);
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ciMODULE_AUTHOR("Markus Franke <franke.m@sebakmt.com>, <franm@hrz.tu-chemnitz.de>");
4128c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("w1 family 1C driver for DS28E04, 4kb EEPROM and PIO");
4138c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
4148c2ecf20Sopenharmony_ciMODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS28E04));
415