18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *	w1_ds2423.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2010 Mika Laitio <lamikr@pilppa.org>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * This driver will read and write the value of 4 counters to w1_slave file in
88c2ecf20Sopenharmony_ci * sys filesystem.
98c2ecf20Sopenharmony_ci * Inspired by the w1_therm and w1_ds2431 drivers.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
158c2ecf20Sopenharmony_ci#include <linux/device.h>
168c2ecf20Sopenharmony_ci#include <linux/types.h>
178c2ecf20Sopenharmony_ci#include <linux/delay.h>
188c2ecf20Sopenharmony_ci#include <linux/crc16.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <linux/w1.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define W1_COUNTER_DS2423	0x1D
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define CRC16_VALID	0xb001
258c2ecf20Sopenharmony_ci#define CRC16_INIT	0
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define COUNTER_COUNT 4
288c2ecf20Sopenharmony_ci#define READ_BYTE_COUNT 42
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic ssize_t w1_slave_show(struct device *device,
318c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *out_buf)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	struct w1_slave *sl = dev_to_w1_slave(device);
348c2ecf20Sopenharmony_ci	struct w1_master *dev = sl->master;
358c2ecf20Sopenharmony_ci	u8 rbuf[COUNTER_COUNT * READ_BYTE_COUNT];
368c2ecf20Sopenharmony_ci	u8 wrbuf[3];
378c2ecf20Sopenharmony_ci	int rom_addr;
388c2ecf20Sopenharmony_ci	int read_byte_count;
398c2ecf20Sopenharmony_ci	int result;
408c2ecf20Sopenharmony_ci	ssize_t c;
418c2ecf20Sopenharmony_ci	int ii;
428c2ecf20Sopenharmony_ci	int p;
438c2ecf20Sopenharmony_ci	int crc;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	c		= PAGE_SIZE;
468c2ecf20Sopenharmony_ci	rom_addr	= (12 << 5) + 31;
478c2ecf20Sopenharmony_ci	wrbuf[0]	= 0xA5;
488c2ecf20Sopenharmony_ci	wrbuf[1]	= rom_addr & 0xFF;
498c2ecf20Sopenharmony_ci	wrbuf[2]	= rom_addr >> 8;
508c2ecf20Sopenharmony_ci	mutex_lock(&dev->bus_mutex);
518c2ecf20Sopenharmony_ci	if (!w1_reset_select_slave(sl)) {
528c2ecf20Sopenharmony_ci		w1_write_block(dev, wrbuf, 3);
538c2ecf20Sopenharmony_ci		read_byte_count = 0;
548c2ecf20Sopenharmony_ci		for (p = 0; p < 4; p++) {
558c2ecf20Sopenharmony_ci			/*
568c2ecf20Sopenharmony_ci			 * 1 byte for first bytes in ram page read
578c2ecf20Sopenharmony_ci			 * 4 bytes for counter
588c2ecf20Sopenharmony_ci			 * 4 bytes for zero bits
598c2ecf20Sopenharmony_ci			 * 2 bytes for crc
608c2ecf20Sopenharmony_ci			 * 31 remaining bytes from the ram page
618c2ecf20Sopenharmony_ci			 */
628c2ecf20Sopenharmony_ci			read_byte_count += w1_read_block(dev,
638c2ecf20Sopenharmony_ci				rbuf + (p * READ_BYTE_COUNT), READ_BYTE_COUNT);
648c2ecf20Sopenharmony_ci			for (ii = 0; ii < READ_BYTE_COUNT; ++ii)
658c2ecf20Sopenharmony_ci				c -= snprintf(out_buf + PAGE_SIZE - c,
668c2ecf20Sopenharmony_ci					c, "%02x ",
678c2ecf20Sopenharmony_ci					rbuf[(p * READ_BYTE_COUNT) + ii]);
688c2ecf20Sopenharmony_ci			if (read_byte_count != (p + 1) * READ_BYTE_COUNT) {
698c2ecf20Sopenharmony_ci				dev_warn(device,
708c2ecf20Sopenharmony_ci					"w1_counter_read() returned %u bytes "
718c2ecf20Sopenharmony_ci					"instead of %d bytes wanted.\n",
728c2ecf20Sopenharmony_ci					read_byte_count,
738c2ecf20Sopenharmony_ci					READ_BYTE_COUNT);
748c2ecf20Sopenharmony_ci				c -= snprintf(out_buf + PAGE_SIZE - c,
758c2ecf20Sopenharmony_ci					c, "crc=NO\n");
768c2ecf20Sopenharmony_ci			} else {
778c2ecf20Sopenharmony_ci				if (p == 0) {
788c2ecf20Sopenharmony_ci					crc = crc16(CRC16_INIT, wrbuf, 3);
798c2ecf20Sopenharmony_ci					crc = crc16(crc, rbuf, 11);
808c2ecf20Sopenharmony_ci				} else {
818c2ecf20Sopenharmony_ci					/*
828c2ecf20Sopenharmony_ci					 * DS2423 calculates crc from all bytes
838c2ecf20Sopenharmony_ci					 * read after the previous crc bytes.
848c2ecf20Sopenharmony_ci					 */
858c2ecf20Sopenharmony_ci					crc = crc16(CRC16_INIT,
868c2ecf20Sopenharmony_ci						(rbuf + 11) +
878c2ecf20Sopenharmony_ci						((p - 1) * READ_BYTE_COUNT),
888c2ecf20Sopenharmony_ci						READ_BYTE_COUNT);
898c2ecf20Sopenharmony_ci				}
908c2ecf20Sopenharmony_ci				if (crc == CRC16_VALID) {
918c2ecf20Sopenharmony_ci					result = 0;
928c2ecf20Sopenharmony_ci					for (ii = 4; ii > 0; ii--) {
938c2ecf20Sopenharmony_ci						result <<= 8;
948c2ecf20Sopenharmony_ci						result |= rbuf[(p *
958c2ecf20Sopenharmony_ci							READ_BYTE_COUNT) + ii];
968c2ecf20Sopenharmony_ci					}
978c2ecf20Sopenharmony_ci					c -= snprintf(out_buf + PAGE_SIZE - c,
988c2ecf20Sopenharmony_ci						c, "crc=YES c=%d\n", result);
998c2ecf20Sopenharmony_ci				} else {
1008c2ecf20Sopenharmony_ci					c -= snprintf(out_buf + PAGE_SIZE - c,
1018c2ecf20Sopenharmony_ci						c, "crc=NO\n");
1028c2ecf20Sopenharmony_ci				}
1038c2ecf20Sopenharmony_ci			}
1048c2ecf20Sopenharmony_ci		}
1058c2ecf20Sopenharmony_ci	} else {
1068c2ecf20Sopenharmony_ci		c -= snprintf(out_buf + PAGE_SIZE - c, c, "Connection error");
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci	mutex_unlock(&dev->bus_mutex);
1098c2ecf20Sopenharmony_ci	return PAGE_SIZE - c;
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(w1_slave);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic struct attribute *w1_f1d_attrs[] = {
1158c2ecf20Sopenharmony_ci	&dev_attr_w1_slave.attr,
1168c2ecf20Sopenharmony_ci	NULL,
1178c2ecf20Sopenharmony_ci};
1188c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(w1_f1d);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic const struct w1_family_ops w1_f1d_fops = {
1218c2ecf20Sopenharmony_ci	.groups		= w1_f1d_groups,
1228c2ecf20Sopenharmony_ci};
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic struct w1_family w1_family_1d = {
1258c2ecf20Sopenharmony_ci	.fid = W1_COUNTER_DS2423,
1268c2ecf20Sopenharmony_ci	.fops = &w1_f1d_fops,
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_cimodule_w1_family(w1_family_1d);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ciMODULE_AUTHOR("Mika Laitio <lamikr@pilppa.org>");
1318c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("w1 family 1d driver for DS2423, 4 counters and 4kb ram");
1328c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1338c2ecf20Sopenharmony_ciMODULE_ALIAS("w1-family-" __stringify(W1_COUNTER_DS2423));
134