summaryrefslogtreecommitdiffstats
path: root/drivers/w1/slaves/w1_ds2438.c
blob: 5ededb4965e143ab774d2705b2ad1e6b0d4fa51b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * 1-Wire implementation for the ds2438 chip
 *
 * Copyright (c) 2017 Mariusz Bialonczyk <manio@skyboo.net>
 *
 * This source code is licensed under the GNU General Public License,
 * Version 2. See the file COPYING for more details.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/delay.h>

#include "../w1.h"
#include "../w1_family.h"

#define W1_DS2438_RETRIES		3

/* Memory commands */
#define W1_DS2438_READ_SCRATCH		0xBE
#define W1_DS2438_WRITE_SCRATCH		0x4E
#define W1_DS2438_COPY_SCRATCH		0x48
#define W1_DS2438_RECALL_MEMORY		0xB8
/* Register commands */
#define W1_DS2438_CONVERT_TEMP		0x44
#define W1_DS2438_CONVERT_VOLTAGE	0xB4

#define DS2438_PAGE_SIZE		8
#define DS2438_ADC_INPUT_VAD		0
#define DS2438_ADC_INPUT_VDD		1
#define DS2438_MAX_CONVERSION_TIME	10		/* ms */

/* Page #0 definitions */
#define DS2438_STATUS_REG		0x00		/* Status/Configuration Register */
#define DS2438_STATUS_IAD		(1 << 0)	/* Current A/D Control Bit */
#define DS2438_STATUS_CA		(1 << 1)	/* Current Accumulator Configuration */
#define DS2438_STATUS_EE		(1 << 2)	/* Current Accumulator Shadow Selector bit */
#define DS2438_STATUS_AD		(1 << 3)	/* Voltage A/D Input Select Bit */
#define DS2438_STATUS_TB		(1 << 4)	/* Temperature Busy Flag */
#define DS2438_STATUS_NVB		(1 << 5)	/* Nonvolatile Memory Busy Flag */
#define DS2438_STATUS_ADB		(1 << 6)	/* A/D Converter Busy Flag */

#define DS2438_TEMP_LSB			0x01
#define DS2438_TEMP_MSB			0x02
#define DS2438_VOLTAGE_LSB		0x03
#define DS2438_VOLTAGE_MSB		0x04
#define DS2438_CURRENT_LSB		0x05
#define DS2438_CURRENT_MSB		0x06
#define DS2438_THRESHOLD		0x07

int w1_ds2438_get_page(struct w1_slave *sl, int pageno, u8 *buf)
{
	unsigned int retries = W1_DS2438_RETRIES;
	u8 w1_buf[2];
	u8 crc;
	size_t count;

	while (retries--) {
		crc = 0;

		if (w1_reset_select_slave(sl))
			continue;
		w1_buf[0] = W1_DS2438_RECALL_MEMORY;
		w1_buf[1] = 0x00;
		w1_write_block(sl->master, w1_buf, 2);

		if (w1_reset_select_slave(sl))
			continue;
		w1_buf[0] = W1_DS2438_READ_SCRATCH;
		w1_buf[1] = 0x00;
		w1_write_block(sl->master, w1_buf, 2);

		count = w1_read_block(sl->master, buf, DS2438_PAGE_SIZE + 1);
		if (count == DS2438_PAGE_SIZE + 1) {
			crc = w1_calc_crc8(buf, DS2438_PAGE_SIZE);

			/* check for correct CRC */
			if ((u8)buf[DS2438_PAGE_SIZE] == crc)
				return 0;
		}
	}
	return -1;
}

int w1_ds2438_get_temperature(struct w1_slave *sl, int16_t *temperature)
{
	unsigned int retries = W1_DS2438_RETRIES;
	u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
	unsigned int tm = DS2438_MAX_CONVERSION_TIME;
	unsigned long sleep_rem;
	int ret;

	mutex_lock(&sl->master->bus_mutex);

	while (retries--) {
		if (w1_reset_select_slave(sl))
			continue;
		w1_write_8(sl->master, W1_DS2438_CONVERT_TEMP);

		mutex_unlock(&sl->master->bus_mutex);
		sleep_rem = msleep_interruptible(tm);
		if (sleep_rem != 0) {
			ret = -1;
			goto post_unlock;
		}

		if (mutex_lock_interruptible(&sl->master->bus_mutex) != 0) {
			ret = -1;
			goto post_unlock;
		}

		break;
	}

	if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
		*temperature = (((int16_t) w1_buf[DS2438_TEMP_MSB]) << 8) | ((uint16_t) w1_buf[DS2438_TEMP_LSB]);
		ret = 0;
	} else
		ret = -1;

	mutex_unlock(&sl->master->bus_mutex);

post_unlock:
	return ret;
}

int w1_ds2438_change_config_bit(struct w1_slave *sl, u8 mask, u8 value)
{
	unsigned int retries = W1_DS2438_RETRIES;
	u8 w1_buf[3];
	u8 status;
	int perform_write = 0;

	while (retries--) {
		if (w1_reset_select_slave(sl))
			continue;
		w1_buf[0] = W1_DS2438_RECALL_MEMORY;
		w1_buf[1] = 0x00;
		w1_write_block(sl->master, w1_buf, 2);

		if (w1_reset_select_slave(sl))
			continue;
		w1_buf[0] = W1_DS2438_READ_SCRATCH;
		w1_buf[1] = 0x00;
		w1_write_block(sl->master, w1_buf, 2);

		/* reading one byte of result */
		status = w1_read_8(sl->master);

		/* if bit0=1, set a value to a mask for easy compare */
		if (value)
			value = mask;

		if ((status & mask) == value)
			return 0;	/* already set as requested */
		else {
			/* changing bit */
			status ^= mask;
			perform_write = 1;
		}
		break;
	}

	if (perform_write) {
		retries = W1_DS2438_RETRIES;
		while (retries--) {
			if (w1_reset_select_slave(sl))
				continue;
			w1_buf[0] = W1_DS2438_WRITE_SCRATCH;
			w1_buf[1] = 0x00;
			w1_buf[2] = status;
			w1_write_block(sl->master, w1_buf, 3);

			if (w1_reset_select_slave(sl))
				continue;
			w1_buf[0] = W1_DS2438_COPY_SCRATCH;
			w1_buf[1] = 0x00;
			w1_write_block(sl->master, w1_buf, 2);

			return 0;
		}
	}
	return -1;
}

uint16_t w1_ds2438_get_voltage(struct w1_slave *sl, int adc_input, uint16_t *voltage)
{
	unsigned int retries = W1_DS2438_RETRIES;
	u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
	unsigned int tm = DS2438_MAX_CONVERSION_TIME;
	unsigned long sleep_rem;
	int ret;

	mutex_lock(&sl->master->bus_mutex);

	if (w1_ds2438_change_config_bit(sl, DS2438_STATUS_AD, adc_input)) {
		ret = -1;
		goto pre_unlock;
	}

	while (retries--) {
		if (w1_reset_select_slave(sl))
			continue;
		w1_write_8(sl->master, W1_DS2438_CONVERT_VOLTAGE);

		mutex_unlock(&sl->master->bus_mutex);
		sleep_rem = msleep_interruptible(tm);
		if (sleep_rem != 0) {
			ret = -1;
			goto post_unlock;
		}

		if (mutex_lock_interruptible(&sl->master->bus_mutex) != 0) {
			ret = -1;
			goto post_unlock;
		}

		break;
	}

	if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
		*voltage = (((uint16_t) w1_buf[DS2438_VOLTAGE_MSB]) << 8) | ((uint16_t) w1_buf[DS2438_VOLTAGE_LSB]);
		ret = 0;
	} else
		ret = -1;

pre_unlock:
	mutex_unlock(&sl->master->bus_mutex);

post_unlock:
	return ret;
}

static ssize_t iad_write(struct file *filp, struct kobject *kobj,
			 struct bin_attribute *bin_attr, char *buf,
			 loff_t off, size_t count)
{
	struct w1_slave *sl = kobj_to_w1_slave(kobj);
	int ret;

	if (count != 1 || off != 0)
		return -EFAULT;

	mutex_lock(&sl->master->bus_mutex);

	if (w1_ds2438_change_config_bit(sl, DS2438_STATUS_IAD, *buf & 0x01) == 0)
		ret = 1;
	else
		ret = -EIO;

	mutex_unlock(&sl->master->bus_mutex);

	return ret;
}

static ssize_t page0_read(struct file *filp, struct kobject *kobj,
			  struct bin_attribute *bin_attr, char *buf,
			  loff_t off, size_t count)
{
	struct w1_slave *sl = kobj_to_w1_slave(kobj);
	int ret;
	u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];

	if (off != 0)
		return 0;
	if (!buf)
		return -EINVAL;

	mutex_lock(&sl->master->bus_mutex);

	if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
		memcpy(buf, &w1_buf, DS2438_PAGE_SIZE);
		ret = DS2438_PAGE_SIZE;
	} else
		ret = -EIO;

	mutex_unlock(&sl->master->bus_mutex);

	return ret;
}

static ssize_t temperature_read(struct file *filp, struct kobject *kobj,
				struct bin_attribute *bin_attr, char *buf,
				loff_t off, size_t count)
{
	struct w1_slave *sl = kobj_to_w1_slave(kobj);
	int ret;
	ssize_t c = PAGE_SIZE;
	int16_t temp;

	if (off != 0)
		return 0;
	if (!buf)
		return -EINVAL;

	if (w1_ds2438_get_temperature(sl, &temp) == 0) {
		c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", temp);
		ret = PAGE_SIZE - c;
	} else
		ret = -EIO;

	return ret;
}

static ssize_t vad_read(struct file *filp, struct kobject *kobj,
			struct bin_attribute *bin_attr, char *buf,
			loff_t off, size_t count)
{
	struct w1_slave *sl = kobj_to_w1_slave(kobj);
	int ret;
	ssize_t c = PAGE_SIZE;
	uint16_t voltage;

	if (off != 0)
		return 0;
	if (!buf)
		return -EINVAL;

	if (w1_ds2438_get_voltage(sl, DS2438_ADC_INPUT_VAD, &voltage) == 0) {
		c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", voltage);
		ret = PAGE_SIZE - c;
	} else
		ret = -EIO;

	return ret;
}

static ssize_t vdd_read(struct file *filp, struct kobject *kobj,
			struct bin_attribute *bin_attr, char *buf,
			loff_t off, size_t count)
{
	struct w1_slave *sl = kobj_to_w1_slave(kobj);
	int ret;
	ssize_t c = PAGE_SIZE;
	uint16_t voltage;

	if (off != 0)
		return 0;
	if (!buf)
		return -EINVAL;

	if (w1_ds2438_get_voltage(sl, DS2438_ADC_INPUT_VDD, &voltage) == 0) {
		c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", voltage);
		ret = PAGE_SIZE - c;
	} else
		ret = -EIO;

	return ret;
}

static BIN_ATTR(iad, S_IRUGO | S_IWUSR | S_IWGRP, NULL, iad_write, 1);
static BIN_ATTR_RO(page0, DS2438_PAGE_SIZE);
static BIN_ATTR_RO(temperature, 0/* real length varies */);
static BIN_ATTR_RO(vad, 0/* real length varies */);
static BIN_ATTR_RO(vdd, 0/* real length varies */);

static struct bin_attribute *w1_ds2438_bin_attrs[] = {
	&bin_attr_iad,
	&bin_attr_page0,
	&bin_attr_temperature,
	&bin_attr_vad,
	&bin_attr_vdd,
	NULL,
};

static const struct attribute_group w1_ds2438_group = {
	.bin_attrs = w1_ds2438_bin_attrs,
};

static const struct attribute_group *w1_ds2438_groups[] = {
	&w1_ds2438_group,
	NULL,
};

static struct w1_family_ops w1_ds2438_fops = {
	.groups		= w1_ds2438_groups,
};

static struct w1_family w1_ds2438_family = {
	.fid = W1_FAMILY_DS2438,
	.fops = &w1_ds2438_fops,
};
module_w1_family(w1_ds2438_family);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Mariusz Bialonczyk <manio@skyboo.net>");
MODULE_DESCRIPTION("1-wire driver for Maxim/Dallas DS2438 Smart Battery Monitor");
MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2438));
OpenPOWER on IntegriCloud