1 // SPDX-License-Identifier: GPL-2.0-only
3 * 1-Wire implementation for the ds2438 chip
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/device.h>
11 #include <linux/types.h>
12 #include <linux/delay.h>
16 #define W1_FAMILY_DS2438 0x26
18 #define W1_DS2438_RETRIES 3
21 #define W1_DS2438_READ_SCRATCH 0xBE
22 #define W1_DS2438_WRITE_SCRATCH 0x4E
23 #define W1_DS2438_COPY_SCRATCH 0x48
24 #define W1_DS2438_RECALL_MEMORY 0xB8
25 /* Register commands */
26 #define W1_DS2438_CONVERT_TEMP 0x44
27 #define W1_DS2438_CONVERT_VOLTAGE 0xB4
29 #define DS2438_PAGE_SIZE 8
30 #define DS2438_ADC_INPUT_VAD 0
31 #define DS2438_ADC_INPUT_VDD 1
32 #define DS2438_MAX_CONVERSION_TIME 10 /* ms */
34 /* Page #0 definitions */
35 #define DS2438_STATUS_REG 0x00 /* Status/Configuration Register */
36 #define DS2438_STATUS_IAD (1 << 0) /* Current A/D Control Bit */
37 #define DS2438_STATUS_CA (1 << 1) /* Current Accumulator Configuration */
38 #define DS2438_STATUS_EE (1 << 2) /* Current Accumulator Shadow Selector bit */
39 #define DS2438_STATUS_AD (1 << 3) /* Voltage A/D Input Select Bit */
40 #define DS2438_STATUS_TB (1 << 4) /* Temperature Busy Flag */
41 #define DS2438_STATUS_NVB (1 << 5) /* Nonvolatile Memory Busy Flag */
42 #define DS2438_STATUS_ADB (1 << 6) /* A/D Converter Busy Flag */
44 #define DS2438_TEMP_LSB 0x01
45 #define DS2438_TEMP_MSB 0x02
46 #define DS2438_VOLTAGE_LSB 0x03
47 #define DS2438_VOLTAGE_MSB 0x04
48 #define DS2438_CURRENT_LSB 0x05
49 #define DS2438_CURRENT_MSB 0x06
50 #define DS2438_THRESHOLD 0x07
52 static int w1_ds2438_get_page(struct w1_slave *sl, int pageno, u8 *buf)
54 unsigned int retries = W1_DS2438_RETRIES;
62 if (w1_reset_select_slave(sl))
64 w1_buf[0] = W1_DS2438_RECALL_MEMORY;
66 w1_write_block(sl->master, w1_buf, 2);
68 if (w1_reset_select_slave(sl))
70 w1_buf[0] = W1_DS2438_READ_SCRATCH;
72 w1_write_block(sl->master, w1_buf, 2);
74 count = w1_read_block(sl->master, buf, DS2438_PAGE_SIZE + 1);
75 if (count == DS2438_PAGE_SIZE + 1) {
76 crc = w1_calc_crc8(buf, DS2438_PAGE_SIZE);
78 /* check for correct CRC */
79 if ((u8)buf[DS2438_PAGE_SIZE] == crc)
86 static int w1_ds2438_get_temperature(struct w1_slave *sl, int16_t *temperature)
88 unsigned int retries = W1_DS2438_RETRIES;
89 u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
90 unsigned int tm = DS2438_MAX_CONVERSION_TIME;
91 unsigned long sleep_rem;
94 mutex_lock(&sl->master->bus_mutex);
97 if (w1_reset_select_slave(sl))
99 w1_write_8(sl->master, W1_DS2438_CONVERT_TEMP);
101 mutex_unlock(&sl->master->bus_mutex);
102 sleep_rem = msleep_interruptible(tm);
103 if (sleep_rem != 0) {
108 if (mutex_lock_interruptible(&sl->master->bus_mutex) != 0) {
116 if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
117 *temperature = (((int16_t) w1_buf[DS2438_TEMP_MSB]) << 8) | ((uint16_t) w1_buf[DS2438_TEMP_LSB]);
122 mutex_unlock(&sl->master->bus_mutex);
128 static int w1_ds2438_change_config_bit(struct w1_slave *sl, u8 mask, u8 value)
130 unsigned int retries = W1_DS2438_RETRIES;
133 int perform_write = 0;
136 if (w1_reset_select_slave(sl))
138 w1_buf[0] = W1_DS2438_RECALL_MEMORY;
140 w1_write_block(sl->master, w1_buf, 2);
142 if (w1_reset_select_slave(sl))
144 w1_buf[0] = W1_DS2438_READ_SCRATCH;
146 w1_write_block(sl->master, w1_buf, 2);
148 /* reading one byte of result */
149 status = w1_read_8(sl->master);
151 /* if bit0=1, set a value to a mask for easy compare */
155 if ((status & mask) == value)
156 return 0; /* already set as requested */
166 retries = W1_DS2438_RETRIES;
168 if (w1_reset_select_slave(sl))
170 w1_buf[0] = W1_DS2438_WRITE_SCRATCH;
173 w1_write_block(sl->master, w1_buf, 3);
175 if (w1_reset_select_slave(sl))
177 w1_buf[0] = W1_DS2438_COPY_SCRATCH;
179 w1_write_block(sl->master, w1_buf, 2);
187 static int w1_ds2438_get_voltage(struct w1_slave *sl,
188 int adc_input, uint16_t *voltage)
190 unsigned int retries = W1_DS2438_RETRIES;
191 u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
192 unsigned int tm = DS2438_MAX_CONVERSION_TIME;
193 unsigned long sleep_rem;
196 mutex_lock(&sl->master->bus_mutex);
198 if (w1_ds2438_change_config_bit(sl, DS2438_STATUS_AD, adc_input)) {
204 if (w1_reset_select_slave(sl))
206 w1_write_8(sl->master, W1_DS2438_CONVERT_VOLTAGE);
208 mutex_unlock(&sl->master->bus_mutex);
209 sleep_rem = msleep_interruptible(tm);
210 if (sleep_rem != 0) {
215 if (mutex_lock_interruptible(&sl->master->bus_mutex) != 0) {
223 if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
224 *voltage = (((uint16_t) w1_buf[DS2438_VOLTAGE_MSB]) << 8) | ((uint16_t) w1_buf[DS2438_VOLTAGE_LSB]);
230 mutex_unlock(&sl->master->bus_mutex);
236 static int w1_ds2438_get_current(struct w1_slave *sl, int16_t *voltage)
238 u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
241 mutex_lock(&sl->master->bus_mutex);
243 if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
244 /* The voltage measured across current sense resistor RSENS. */
245 *voltage = (((int16_t) w1_buf[DS2438_CURRENT_MSB]) << 8) | ((int16_t) w1_buf[DS2438_CURRENT_LSB]);
250 mutex_unlock(&sl->master->bus_mutex);
255 static ssize_t iad_write(struct file *filp, struct kobject *kobj,
256 struct bin_attribute *bin_attr, char *buf,
257 loff_t off, size_t count)
259 struct w1_slave *sl = kobj_to_w1_slave(kobj);
262 if (count != 1 || off != 0)
265 mutex_lock(&sl->master->bus_mutex);
267 if (w1_ds2438_change_config_bit(sl, DS2438_STATUS_IAD, *buf & 0x01) == 0)
272 mutex_unlock(&sl->master->bus_mutex);
277 static ssize_t iad_read(struct file *filp, struct kobject *kobj,
278 struct bin_attribute *bin_attr, char *buf,
279 loff_t off, size_t count)
281 struct w1_slave *sl = kobj_to_w1_slave(kobj);
290 if (w1_ds2438_get_current(sl, &voltage) == 0) {
291 ret = snprintf(buf, count, "%i\n", voltage);
298 static ssize_t page0_read(struct file *filp, struct kobject *kobj,
299 struct bin_attribute *bin_attr, char *buf,
300 loff_t off, size_t count)
302 struct w1_slave *sl = kobj_to_w1_slave(kobj);
304 u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
311 mutex_lock(&sl->master->bus_mutex);
313 /* Read no more than page0 size */
314 if (count > DS2438_PAGE_SIZE)
315 count = DS2438_PAGE_SIZE;
317 if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
318 memcpy(buf, &w1_buf, count);
323 mutex_unlock(&sl->master->bus_mutex);
328 static ssize_t temperature_read(struct file *filp, struct kobject *kobj,
329 struct bin_attribute *bin_attr, char *buf,
330 loff_t off, size_t count)
332 struct w1_slave *sl = kobj_to_w1_slave(kobj);
341 if (w1_ds2438_get_temperature(sl, &temp) == 0) {
342 ret = snprintf(buf, count, "%i\n", temp);
349 static ssize_t vad_read(struct file *filp, struct kobject *kobj,
350 struct bin_attribute *bin_attr, char *buf,
351 loff_t off, size_t count)
353 struct w1_slave *sl = kobj_to_w1_slave(kobj);
362 if (w1_ds2438_get_voltage(sl, DS2438_ADC_INPUT_VAD, &voltage) == 0) {
363 ret = snprintf(buf, count, "%u\n", voltage);
370 static ssize_t vdd_read(struct file *filp, struct kobject *kobj,
371 struct bin_attribute *bin_attr, char *buf,
372 loff_t off, size_t count)
374 struct w1_slave *sl = kobj_to_w1_slave(kobj);
383 if (w1_ds2438_get_voltage(sl, DS2438_ADC_INPUT_VDD, &voltage) == 0) {
384 ret = snprintf(buf, count, "%u\n", voltage);
391 static BIN_ATTR(iad, S_IRUGO | S_IWUSR | S_IWGRP, iad_read, iad_write, 0);
392 static BIN_ATTR_RO(page0, DS2438_PAGE_SIZE);
393 static BIN_ATTR_RO(temperature, 0/* real length varies */);
394 static BIN_ATTR_RO(vad, 0/* real length varies */);
395 static BIN_ATTR_RO(vdd, 0/* real length varies */);
397 static struct bin_attribute *w1_ds2438_bin_attrs[] = {
400 &bin_attr_temperature,
406 static const struct attribute_group w1_ds2438_group = {
407 .bin_attrs = w1_ds2438_bin_attrs,
410 static const struct attribute_group *w1_ds2438_groups[] = {
415 static struct w1_family_ops w1_ds2438_fops = {
416 .groups = w1_ds2438_groups,
419 static struct w1_family w1_ds2438_family = {
420 .fid = W1_FAMILY_DS2438,
421 .fops = &w1_ds2438_fops,
423 module_w1_family(w1_ds2438_family);
425 MODULE_LICENSE("GPL");
427 MODULE_DESCRIPTION("1-wire driver for Maxim/Dallas DS2438 Smart Battery Monitor");
428 MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2438));