18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * 1-Wire implementation for the ds2438 chip
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2017 Mariusz Bialonczyk <manio@skyboo.net>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/device.h>
118c2ecf20Sopenharmony_ci#include <linux/types.h>
128c2ecf20Sopenharmony_ci#include <linux/delay.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/w1.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define W1_FAMILY_DS2438		0x26
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define W1_DS2438_RETRIES		3
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* Memory commands */
218c2ecf20Sopenharmony_ci#define W1_DS2438_READ_SCRATCH		0xBE
228c2ecf20Sopenharmony_ci#define W1_DS2438_WRITE_SCRATCH		0x4E
238c2ecf20Sopenharmony_ci#define W1_DS2438_COPY_SCRATCH		0x48
248c2ecf20Sopenharmony_ci#define W1_DS2438_RECALL_MEMORY		0xB8
258c2ecf20Sopenharmony_ci/* Register commands */
268c2ecf20Sopenharmony_ci#define W1_DS2438_CONVERT_TEMP		0x44
278c2ecf20Sopenharmony_ci#define W1_DS2438_CONVERT_VOLTAGE	0xB4
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define DS2438_PAGE_SIZE		8
308c2ecf20Sopenharmony_ci#define DS2438_ADC_INPUT_VAD		0
318c2ecf20Sopenharmony_ci#define DS2438_ADC_INPUT_VDD		1
328c2ecf20Sopenharmony_ci#define DS2438_MAX_CONVERSION_TIME	10		/* ms */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* Page #0 definitions */
358c2ecf20Sopenharmony_ci#define DS2438_STATUS_REG		0x00		/* Status/Configuration Register */
368c2ecf20Sopenharmony_ci#define DS2438_STATUS_IAD		(1 << 0)	/* Current A/D Control Bit */
378c2ecf20Sopenharmony_ci#define DS2438_STATUS_CA		(1 << 1)	/* Current Accumulator Configuration */
388c2ecf20Sopenharmony_ci#define DS2438_STATUS_EE		(1 << 2)	/* Current Accumulator Shadow Selector bit */
398c2ecf20Sopenharmony_ci#define DS2438_STATUS_AD		(1 << 3)	/* Voltage A/D Input Select Bit */
408c2ecf20Sopenharmony_ci#define DS2438_STATUS_TB		(1 << 4)	/* Temperature Busy Flag */
418c2ecf20Sopenharmony_ci#define DS2438_STATUS_NVB		(1 << 5)	/* Nonvolatile Memory Busy Flag */
428c2ecf20Sopenharmony_ci#define DS2438_STATUS_ADB		(1 << 6)	/* A/D Converter Busy Flag */
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define DS2438_TEMP_LSB			0x01
458c2ecf20Sopenharmony_ci#define DS2438_TEMP_MSB			0x02
468c2ecf20Sopenharmony_ci#define DS2438_VOLTAGE_LSB		0x03
478c2ecf20Sopenharmony_ci#define DS2438_VOLTAGE_MSB		0x04
488c2ecf20Sopenharmony_ci#define DS2438_CURRENT_LSB		0x05
498c2ecf20Sopenharmony_ci#define DS2438_CURRENT_MSB		0x06
508c2ecf20Sopenharmony_ci#define DS2438_THRESHOLD		0x07
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic int w1_ds2438_get_page(struct w1_slave *sl, int pageno, u8 *buf)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	unsigned int retries = W1_DS2438_RETRIES;
558c2ecf20Sopenharmony_ci	u8 w1_buf[2];
568c2ecf20Sopenharmony_ci	u8 crc;
578c2ecf20Sopenharmony_ci	size_t count;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	while (retries--) {
608c2ecf20Sopenharmony_ci		crc = 0;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci		if (w1_reset_select_slave(sl))
638c2ecf20Sopenharmony_ci			continue;
648c2ecf20Sopenharmony_ci		w1_buf[0] = W1_DS2438_RECALL_MEMORY;
658c2ecf20Sopenharmony_ci		w1_buf[1] = (u8)pageno;
668c2ecf20Sopenharmony_ci		w1_write_block(sl->master, w1_buf, 2);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci		if (w1_reset_select_slave(sl))
698c2ecf20Sopenharmony_ci			continue;
708c2ecf20Sopenharmony_ci		w1_buf[0] = W1_DS2438_READ_SCRATCH;
718c2ecf20Sopenharmony_ci		w1_buf[1] = (u8)pageno;
728c2ecf20Sopenharmony_ci		w1_write_block(sl->master, w1_buf, 2);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci		count = w1_read_block(sl->master, buf, DS2438_PAGE_SIZE + 1);
758c2ecf20Sopenharmony_ci		if (count == DS2438_PAGE_SIZE + 1) {
768c2ecf20Sopenharmony_ci			crc = w1_calc_crc8(buf, DS2438_PAGE_SIZE);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci			/* check for correct CRC */
798c2ecf20Sopenharmony_ci			if ((u8)buf[DS2438_PAGE_SIZE] == crc)
808c2ecf20Sopenharmony_ci				return 0;
818c2ecf20Sopenharmony_ci		}
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci	return -1;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int w1_ds2438_get_temperature(struct w1_slave *sl, int16_t *temperature)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	unsigned int retries = W1_DS2438_RETRIES;
898c2ecf20Sopenharmony_ci	u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
908c2ecf20Sopenharmony_ci	unsigned int tm = DS2438_MAX_CONVERSION_TIME;
918c2ecf20Sopenharmony_ci	unsigned long sleep_rem;
928c2ecf20Sopenharmony_ci	int ret;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	mutex_lock(&sl->master->bus_mutex);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	while (retries--) {
978c2ecf20Sopenharmony_ci		if (w1_reset_select_slave(sl))
988c2ecf20Sopenharmony_ci			continue;
998c2ecf20Sopenharmony_ci		w1_write_8(sl->master, W1_DS2438_CONVERT_TEMP);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci		mutex_unlock(&sl->master->bus_mutex);
1028c2ecf20Sopenharmony_ci		sleep_rem = msleep_interruptible(tm);
1038c2ecf20Sopenharmony_ci		if (sleep_rem != 0) {
1048c2ecf20Sopenharmony_ci			ret = -1;
1058c2ecf20Sopenharmony_ci			goto post_unlock;
1068c2ecf20Sopenharmony_ci		}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci		if (mutex_lock_interruptible(&sl->master->bus_mutex) != 0) {
1098c2ecf20Sopenharmony_ci			ret = -1;
1108c2ecf20Sopenharmony_ci			goto post_unlock;
1118c2ecf20Sopenharmony_ci		}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci		break;
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
1178c2ecf20Sopenharmony_ci		*temperature = (((int16_t) w1_buf[DS2438_TEMP_MSB]) << 8) | ((uint16_t) w1_buf[DS2438_TEMP_LSB]);
1188c2ecf20Sopenharmony_ci		ret = 0;
1198c2ecf20Sopenharmony_ci	} else
1208c2ecf20Sopenharmony_ci		ret = -1;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	mutex_unlock(&sl->master->bus_mutex);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cipost_unlock:
1258c2ecf20Sopenharmony_ci	return ret;
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic int w1_ds2438_change_config_bit(struct w1_slave *sl, u8 mask, u8 value)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	unsigned int retries = W1_DS2438_RETRIES;
1318c2ecf20Sopenharmony_ci	u8 w1_buf[3];
1328c2ecf20Sopenharmony_ci	u8 status;
1338c2ecf20Sopenharmony_ci	int perform_write = 0;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	while (retries--) {
1368c2ecf20Sopenharmony_ci		if (w1_reset_select_slave(sl))
1378c2ecf20Sopenharmony_ci			continue;
1388c2ecf20Sopenharmony_ci		w1_buf[0] = W1_DS2438_RECALL_MEMORY;
1398c2ecf20Sopenharmony_ci		w1_buf[1] = 0x00;
1408c2ecf20Sopenharmony_ci		w1_write_block(sl->master, w1_buf, 2);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci		if (w1_reset_select_slave(sl))
1438c2ecf20Sopenharmony_ci			continue;
1448c2ecf20Sopenharmony_ci		w1_buf[0] = W1_DS2438_READ_SCRATCH;
1458c2ecf20Sopenharmony_ci		w1_buf[1] = 0x00;
1468c2ecf20Sopenharmony_ci		w1_write_block(sl->master, w1_buf, 2);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci		/* reading one byte of result */
1498c2ecf20Sopenharmony_ci		status = w1_read_8(sl->master);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci		/* if bit0=1, set a value to a mask for easy compare */
1528c2ecf20Sopenharmony_ci		if (value)
1538c2ecf20Sopenharmony_ci			value = mask;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci		if ((status & mask) == value)
1568c2ecf20Sopenharmony_ci			return 0;	/* already set as requested */
1578c2ecf20Sopenharmony_ci		else {
1588c2ecf20Sopenharmony_ci			/* changing bit */
1598c2ecf20Sopenharmony_ci			status ^= mask;
1608c2ecf20Sopenharmony_ci			perform_write = 1;
1618c2ecf20Sopenharmony_ci		}
1628c2ecf20Sopenharmony_ci		break;
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	if (perform_write) {
1668c2ecf20Sopenharmony_ci		retries = W1_DS2438_RETRIES;
1678c2ecf20Sopenharmony_ci		while (retries--) {
1688c2ecf20Sopenharmony_ci			if (w1_reset_select_slave(sl))
1698c2ecf20Sopenharmony_ci				continue;
1708c2ecf20Sopenharmony_ci			w1_buf[0] = W1_DS2438_WRITE_SCRATCH;
1718c2ecf20Sopenharmony_ci			w1_buf[1] = 0x00;
1728c2ecf20Sopenharmony_ci			w1_buf[2] = status;
1738c2ecf20Sopenharmony_ci			w1_write_block(sl->master, w1_buf, 3);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci			if (w1_reset_select_slave(sl))
1768c2ecf20Sopenharmony_ci				continue;
1778c2ecf20Sopenharmony_ci			w1_buf[0] = W1_DS2438_COPY_SCRATCH;
1788c2ecf20Sopenharmony_ci			w1_buf[1] = 0x00;
1798c2ecf20Sopenharmony_ci			w1_write_block(sl->master, w1_buf, 2);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci			return 0;
1828c2ecf20Sopenharmony_ci		}
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci	return -1;
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic int w1_ds2438_get_voltage(struct w1_slave *sl,
1888c2ecf20Sopenharmony_ci				 int adc_input, uint16_t *voltage)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	unsigned int retries = W1_DS2438_RETRIES;
1918c2ecf20Sopenharmony_ci	u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
1928c2ecf20Sopenharmony_ci	unsigned int tm = DS2438_MAX_CONVERSION_TIME;
1938c2ecf20Sopenharmony_ci	unsigned long sleep_rem;
1948c2ecf20Sopenharmony_ci	int ret;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	mutex_lock(&sl->master->bus_mutex);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	if (w1_ds2438_change_config_bit(sl, DS2438_STATUS_AD, adc_input)) {
1998c2ecf20Sopenharmony_ci		ret = -1;
2008c2ecf20Sopenharmony_ci		goto pre_unlock;
2018c2ecf20Sopenharmony_ci	}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	while (retries--) {
2048c2ecf20Sopenharmony_ci		if (w1_reset_select_slave(sl))
2058c2ecf20Sopenharmony_ci			continue;
2068c2ecf20Sopenharmony_ci		w1_write_8(sl->master, W1_DS2438_CONVERT_VOLTAGE);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci		mutex_unlock(&sl->master->bus_mutex);
2098c2ecf20Sopenharmony_ci		sleep_rem = msleep_interruptible(tm);
2108c2ecf20Sopenharmony_ci		if (sleep_rem != 0) {
2118c2ecf20Sopenharmony_ci			ret = -1;
2128c2ecf20Sopenharmony_ci			goto post_unlock;
2138c2ecf20Sopenharmony_ci		}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci		if (mutex_lock_interruptible(&sl->master->bus_mutex) != 0) {
2168c2ecf20Sopenharmony_ci			ret = -1;
2178c2ecf20Sopenharmony_ci			goto post_unlock;
2188c2ecf20Sopenharmony_ci		}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci		break;
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
2248c2ecf20Sopenharmony_ci		*voltage = (((uint16_t) w1_buf[DS2438_VOLTAGE_MSB]) << 8) | ((uint16_t) w1_buf[DS2438_VOLTAGE_LSB]);
2258c2ecf20Sopenharmony_ci		ret = 0;
2268c2ecf20Sopenharmony_ci	} else
2278c2ecf20Sopenharmony_ci		ret = -1;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cipre_unlock:
2308c2ecf20Sopenharmony_ci	mutex_unlock(&sl->master->bus_mutex);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cipost_unlock:
2338c2ecf20Sopenharmony_ci	return ret;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic int w1_ds2438_get_current(struct w1_slave *sl, int16_t *voltage)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
2398c2ecf20Sopenharmony_ci	int ret;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	mutex_lock(&sl->master->bus_mutex);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
2448c2ecf20Sopenharmony_ci		/* The voltage measured across current sense resistor RSENS. */
2458c2ecf20Sopenharmony_ci		*voltage = (((int16_t) w1_buf[DS2438_CURRENT_MSB]) << 8) | ((int16_t) w1_buf[DS2438_CURRENT_LSB]);
2468c2ecf20Sopenharmony_ci		ret = 0;
2478c2ecf20Sopenharmony_ci	} else
2488c2ecf20Sopenharmony_ci		ret = -1;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	mutex_unlock(&sl->master->bus_mutex);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	return ret;
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistatic ssize_t iad_write(struct file *filp, struct kobject *kobj,
2568c2ecf20Sopenharmony_ci			 struct bin_attribute *bin_attr, char *buf,
2578c2ecf20Sopenharmony_ci			 loff_t off, size_t count)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	struct w1_slave *sl = kobj_to_w1_slave(kobj);
2608c2ecf20Sopenharmony_ci	int ret;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	if (count != 1 || off != 0)
2638c2ecf20Sopenharmony_ci		return -EFAULT;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	mutex_lock(&sl->master->bus_mutex);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	if (w1_ds2438_change_config_bit(sl, DS2438_STATUS_IAD, *buf & 0x01) == 0)
2688c2ecf20Sopenharmony_ci		ret = 1;
2698c2ecf20Sopenharmony_ci	else
2708c2ecf20Sopenharmony_ci		ret = -EIO;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	mutex_unlock(&sl->master->bus_mutex);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	return ret;
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic ssize_t iad_read(struct file *filp, struct kobject *kobj,
2788c2ecf20Sopenharmony_ci			struct bin_attribute *bin_attr, char *buf,
2798c2ecf20Sopenharmony_ci			loff_t off, size_t count)
2808c2ecf20Sopenharmony_ci{
2818c2ecf20Sopenharmony_ci	struct w1_slave *sl = kobj_to_w1_slave(kobj);
2828c2ecf20Sopenharmony_ci	int ret;
2838c2ecf20Sopenharmony_ci	int16_t voltage;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	if (off != 0)
2868c2ecf20Sopenharmony_ci		return 0;
2878c2ecf20Sopenharmony_ci	if (!buf)
2888c2ecf20Sopenharmony_ci		return -EINVAL;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	if (w1_ds2438_get_current(sl, &voltage) == 0) {
2918c2ecf20Sopenharmony_ci		ret = snprintf(buf, count, "%i\n", voltage);
2928c2ecf20Sopenharmony_ci	} else
2938c2ecf20Sopenharmony_ci		ret = -EIO;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	return ret;
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_cistatic ssize_t page0_read(struct file *filp, struct kobject *kobj,
2998c2ecf20Sopenharmony_ci			  struct bin_attribute *bin_attr, char *buf,
3008c2ecf20Sopenharmony_ci			  loff_t off, size_t count)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	struct w1_slave *sl = kobj_to_w1_slave(kobj);
3038c2ecf20Sopenharmony_ci	int ret;
3048c2ecf20Sopenharmony_ci	u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	if (off != 0)
3078c2ecf20Sopenharmony_ci		return 0;
3088c2ecf20Sopenharmony_ci	if (!buf)
3098c2ecf20Sopenharmony_ci		return -EINVAL;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	mutex_lock(&sl->master->bus_mutex);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	/* Read no more than page0 size */
3148c2ecf20Sopenharmony_ci	if (count > DS2438_PAGE_SIZE)
3158c2ecf20Sopenharmony_ci		count = DS2438_PAGE_SIZE;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
3188c2ecf20Sopenharmony_ci		memcpy(buf, &w1_buf, count);
3198c2ecf20Sopenharmony_ci		ret = count;
3208c2ecf20Sopenharmony_ci	} else
3218c2ecf20Sopenharmony_ci		ret = -EIO;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	mutex_unlock(&sl->master->bus_mutex);
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	return ret;
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_cistatic ssize_t temperature_read(struct file *filp, struct kobject *kobj,
3298c2ecf20Sopenharmony_ci				struct bin_attribute *bin_attr, char *buf,
3308c2ecf20Sopenharmony_ci				loff_t off, size_t count)
3318c2ecf20Sopenharmony_ci{
3328c2ecf20Sopenharmony_ci	struct w1_slave *sl = kobj_to_w1_slave(kobj);
3338c2ecf20Sopenharmony_ci	int ret;
3348c2ecf20Sopenharmony_ci	int16_t temp;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	if (off != 0)
3378c2ecf20Sopenharmony_ci		return 0;
3388c2ecf20Sopenharmony_ci	if (!buf)
3398c2ecf20Sopenharmony_ci		return -EINVAL;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	if (w1_ds2438_get_temperature(sl, &temp) == 0) {
3428c2ecf20Sopenharmony_ci		ret = snprintf(buf, count, "%i\n", temp);
3438c2ecf20Sopenharmony_ci	} else
3448c2ecf20Sopenharmony_ci		ret = -EIO;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	return ret;
3478c2ecf20Sopenharmony_ci}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_cistatic ssize_t vad_read(struct file *filp, struct kobject *kobj,
3508c2ecf20Sopenharmony_ci			struct bin_attribute *bin_attr, char *buf,
3518c2ecf20Sopenharmony_ci			loff_t off, size_t count)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	struct w1_slave *sl = kobj_to_w1_slave(kobj);
3548c2ecf20Sopenharmony_ci	int ret;
3558c2ecf20Sopenharmony_ci	uint16_t voltage;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	if (off != 0)
3588c2ecf20Sopenharmony_ci		return 0;
3598c2ecf20Sopenharmony_ci	if (!buf)
3608c2ecf20Sopenharmony_ci		return -EINVAL;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	if (w1_ds2438_get_voltage(sl, DS2438_ADC_INPUT_VAD, &voltage) == 0) {
3638c2ecf20Sopenharmony_ci		ret = snprintf(buf, count, "%u\n", voltage);
3648c2ecf20Sopenharmony_ci	} else
3658c2ecf20Sopenharmony_ci		ret = -EIO;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	return ret;
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_cistatic ssize_t vdd_read(struct file *filp, struct kobject *kobj,
3718c2ecf20Sopenharmony_ci			struct bin_attribute *bin_attr, char *buf,
3728c2ecf20Sopenharmony_ci			loff_t off, size_t count)
3738c2ecf20Sopenharmony_ci{
3748c2ecf20Sopenharmony_ci	struct w1_slave *sl = kobj_to_w1_slave(kobj);
3758c2ecf20Sopenharmony_ci	int ret;
3768c2ecf20Sopenharmony_ci	uint16_t voltage;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	if (off != 0)
3798c2ecf20Sopenharmony_ci		return 0;
3808c2ecf20Sopenharmony_ci	if (!buf)
3818c2ecf20Sopenharmony_ci		return -EINVAL;
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	if (w1_ds2438_get_voltage(sl, DS2438_ADC_INPUT_VDD, &voltage) == 0) {
3848c2ecf20Sopenharmony_ci		ret = snprintf(buf, count, "%u\n", voltage);
3858c2ecf20Sopenharmony_ci	} else
3868c2ecf20Sopenharmony_ci		ret = -EIO;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	return ret;
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic BIN_ATTR(iad, S_IRUGO | S_IWUSR | S_IWGRP, iad_read, iad_write, 0);
3928c2ecf20Sopenharmony_cistatic BIN_ATTR_RO(page0, DS2438_PAGE_SIZE);
3938c2ecf20Sopenharmony_cistatic BIN_ATTR_RO(temperature, 0/* real length varies */);
3948c2ecf20Sopenharmony_cistatic BIN_ATTR_RO(vad, 0/* real length varies */);
3958c2ecf20Sopenharmony_cistatic BIN_ATTR_RO(vdd, 0/* real length varies */);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_cistatic struct bin_attribute *w1_ds2438_bin_attrs[] = {
3988c2ecf20Sopenharmony_ci	&bin_attr_iad,
3998c2ecf20Sopenharmony_ci	&bin_attr_page0,
4008c2ecf20Sopenharmony_ci	&bin_attr_temperature,
4018c2ecf20Sopenharmony_ci	&bin_attr_vad,
4028c2ecf20Sopenharmony_ci	&bin_attr_vdd,
4038c2ecf20Sopenharmony_ci	NULL,
4048c2ecf20Sopenharmony_ci};
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_cistatic const struct attribute_group w1_ds2438_group = {
4078c2ecf20Sopenharmony_ci	.bin_attrs = w1_ds2438_bin_attrs,
4088c2ecf20Sopenharmony_ci};
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_cistatic const struct attribute_group *w1_ds2438_groups[] = {
4118c2ecf20Sopenharmony_ci	&w1_ds2438_group,
4128c2ecf20Sopenharmony_ci	NULL,
4138c2ecf20Sopenharmony_ci};
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_cistatic const struct w1_family_ops w1_ds2438_fops = {
4168c2ecf20Sopenharmony_ci	.groups		= w1_ds2438_groups,
4178c2ecf20Sopenharmony_ci};
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cistatic struct w1_family w1_ds2438_family = {
4208c2ecf20Sopenharmony_ci	.fid = W1_FAMILY_DS2438,
4218c2ecf20Sopenharmony_ci	.fops = &w1_ds2438_fops,
4228c2ecf20Sopenharmony_ci};
4238c2ecf20Sopenharmony_cimodule_w1_family(w1_ds2438_family);
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
4268c2ecf20Sopenharmony_ciMODULE_AUTHOR("Mariusz Bialonczyk <manio@skyboo.net>");
4278c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("1-wire driver for Maxim/Dallas DS2438 Smart Battery Monitor");
4288c2ecf20Sopenharmony_ciMODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2438));
429