Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *	w1_ds2423.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2010 Mika Laitio <lamikr@pilppa.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * This driver will read and write the value of 4 counters to w1_slave file in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * sys filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Inspired by the w1_therm and w1_ds2431 drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/crc16.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/w1.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define W1_COUNTER_DS2423	0x1D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define CRC16_VALID	0xb001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define CRC16_INIT	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define COUNTER_COUNT 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define READ_BYTE_COUNT 42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static ssize_t w1_slave_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 			     struct device_attribute *attr, char *out_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct w1_slave *sl = dev_to_w1_slave(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct w1_master *dev = sl->master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	u8 rbuf[COUNTER_COUNT * READ_BYTE_COUNT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	u8 wrbuf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	int rom_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	int read_byte_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	ssize_t c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	int ii;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	int p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	int crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	c		= PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	rom_addr	= (12 << 5) + 31;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	wrbuf[0]	= 0xA5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	wrbuf[1]	= rom_addr & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	wrbuf[2]	= rom_addr >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	mutex_lock(&dev->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (!w1_reset_select_slave(sl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		w1_write_block(dev, wrbuf, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		read_byte_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		for (p = 0; p < 4; p++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			 * 1 byte for first bytes in ram page read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			 * 4 bytes for counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			 * 4 bytes for zero bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 			 * 2 bytes for crc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			 * 31 remaining bytes from the ram page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 			read_byte_count += w1_read_block(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 				rbuf + (p * READ_BYTE_COUNT), READ_BYTE_COUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 			for (ii = 0; ii < READ_BYTE_COUNT; ++ii)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 				c -= snprintf(out_buf + PAGE_SIZE - c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 					c, "%02x ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 					rbuf[(p * READ_BYTE_COUNT) + ii]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			if (read_byte_count != (p + 1) * READ_BYTE_COUNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 				dev_warn(device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 					"w1_counter_read() returned %u bytes "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 					"instead of %d bytes wanted.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 					read_byte_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 					READ_BYTE_COUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 				c -= snprintf(out_buf + PAGE_SIZE - c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 					c, "crc=NO\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 				if (p == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 					crc = crc16(CRC16_INIT, wrbuf, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 					crc = crc16(crc, rbuf, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 					/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 					 * DS2423 calculates crc from all bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 					 * read after the previous crc bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 					crc = crc16(CRC16_INIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 						(rbuf + 11) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 						((p - 1) * READ_BYTE_COUNT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 						READ_BYTE_COUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 				if (crc == CRC16_VALID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 					result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 					for (ii = 4; ii > 0; ii--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 						result <<= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 						result |= rbuf[(p *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 							READ_BYTE_COUNT) + ii];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 					c -= snprintf(out_buf + PAGE_SIZE - c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 						c, "crc=YES c=%d\n", result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 					c -= snprintf(out_buf + PAGE_SIZE - c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 						c, "crc=NO\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		c -= snprintf(out_buf + PAGE_SIZE - c, c, "Connection error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	mutex_unlock(&dev->bus_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	return PAGE_SIZE - c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static DEVICE_ATTR_RO(w1_slave);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static struct attribute *w1_f1d_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	&dev_attr_w1_slave.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ATTRIBUTE_GROUPS(w1_f1d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static const struct w1_family_ops w1_f1d_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	.groups		= w1_f1d_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static struct w1_family w1_family_1d = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	.fid = W1_COUNTER_DS2423,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	.fops = &w1_f1d_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) module_w1_family(w1_family_1d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) MODULE_AUTHOR("Mika Laitio <lamikr@pilppa.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) MODULE_DESCRIPTION("w1 family 1d driver for DS2423, 4 counters and 4kb ram");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) MODULE_ALIAS("w1-family-" __stringify(W1_COUNTER_DS2423));