Linux 4.19.133
[linux/fpc-iii.git] / drivers / w1 / slaves / w1_ds2438.c
blob7c4e33dbee4d5b332c3fd8cb5355b979faf3437d
1 /*
2 * 1-Wire implementation for the ds2438 chip
4 * Copyright (c) 2017 Mariusz Bialonczyk <manio@skyboo.net>
6 * This source code is licensed under the GNU General Public License,
7 * Version 2. See the file COPYING for more details.
8 */
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/types.h>
14 #include <linux/delay.h>
16 #include <linux/w1.h>
18 #define W1_FAMILY_DS2438 0x26
20 #define W1_DS2438_RETRIES 3
22 /* Memory commands */
23 #define W1_DS2438_READ_SCRATCH 0xBE
24 #define W1_DS2438_WRITE_SCRATCH 0x4E
25 #define W1_DS2438_COPY_SCRATCH 0x48
26 #define W1_DS2438_RECALL_MEMORY 0xB8
27 /* Register commands */
28 #define W1_DS2438_CONVERT_TEMP 0x44
29 #define W1_DS2438_CONVERT_VOLTAGE 0xB4
31 #define DS2438_PAGE_SIZE 8
32 #define DS2438_ADC_INPUT_VAD 0
33 #define DS2438_ADC_INPUT_VDD 1
34 #define DS2438_MAX_CONVERSION_TIME 10 /* ms */
36 /* Page #0 definitions */
37 #define DS2438_STATUS_REG 0x00 /* Status/Configuration Register */
38 #define DS2438_STATUS_IAD (1 << 0) /* Current A/D Control Bit */
39 #define DS2438_STATUS_CA (1 << 1) /* Current Accumulator Configuration */
40 #define DS2438_STATUS_EE (1 << 2) /* Current Accumulator Shadow Selector bit */
41 #define DS2438_STATUS_AD (1 << 3) /* Voltage A/D Input Select Bit */
42 #define DS2438_STATUS_TB (1 << 4) /* Temperature Busy Flag */
43 #define DS2438_STATUS_NVB (1 << 5) /* Nonvolatile Memory Busy Flag */
44 #define DS2438_STATUS_ADB (1 << 6) /* A/D Converter Busy Flag */
46 #define DS2438_TEMP_LSB 0x01
47 #define DS2438_TEMP_MSB 0x02
48 #define DS2438_VOLTAGE_LSB 0x03
49 #define DS2438_VOLTAGE_MSB 0x04
50 #define DS2438_CURRENT_LSB 0x05
51 #define DS2438_CURRENT_MSB 0x06
52 #define DS2438_THRESHOLD 0x07
54 static int w1_ds2438_get_page(struct w1_slave *sl, int pageno, u8 *buf)
56 unsigned int retries = W1_DS2438_RETRIES;
57 u8 w1_buf[2];
58 u8 crc;
59 size_t count;
61 while (retries--) {
62 crc = 0;
64 if (w1_reset_select_slave(sl))
65 continue;
66 w1_buf[0] = W1_DS2438_RECALL_MEMORY;
67 w1_buf[1] = 0x00;
68 w1_write_block(sl->master, w1_buf, 2);
70 if (w1_reset_select_slave(sl))
71 continue;
72 w1_buf[0] = W1_DS2438_READ_SCRATCH;
73 w1_buf[1] = 0x00;
74 w1_write_block(sl->master, w1_buf, 2);
76 count = w1_read_block(sl->master, buf, DS2438_PAGE_SIZE + 1);
77 if (count == DS2438_PAGE_SIZE + 1) {
78 crc = w1_calc_crc8(buf, DS2438_PAGE_SIZE);
80 /* check for correct CRC */
81 if ((u8)buf[DS2438_PAGE_SIZE] == crc)
82 return 0;
85 return -1;
88 static int w1_ds2438_get_temperature(struct w1_slave *sl, int16_t *temperature)
90 unsigned int retries = W1_DS2438_RETRIES;
91 u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
92 unsigned int tm = DS2438_MAX_CONVERSION_TIME;
93 unsigned long sleep_rem;
94 int ret;
96 mutex_lock(&sl->master->bus_mutex);
98 while (retries--) {
99 if (w1_reset_select_slave(sl))
100 continue;
101 w1_write_8(sl->master, W1_DS2438_CONVERT_TEMP);
103 mutex_unlock(&sl->master->bus_mutex);
104 sleep_rem = msleep_interruptible(tm);
105 if (sleep_rem != 0) {
106 ret = -1;
107 goto post_unlock;
110 if (mutex_lock_interruptible(&sl->master->bus_mutex) != 0) {
111 ret = -1;
112 goto post_unlock;
115 break;
118 if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
119 *temperature = (((int16_t) w1_buf[DS2438_TEMP_MSB]) << 8) | ((uint16_t) w1_buf[DS2438_TEMP_LSB]);
120 ret = 0;
121 } else
122 ret = -1;
124 mutex_unlock(&sl->master->bus_mutex);
126 post_unlock:
127 return ret;
130 static int w1_ds2438_change_config_bit(struct w1_slave *sl, u8 mask, u8 value)
132 unsigned int retries = W1_DS2438_RETRIES;
133 u8 w1_buf[3];
134 u8 status;
135 int perform_write = 0;
137 while (retries--) {
138 if (w1_reset_select_slave(sl))
139 continue;
140 w1_buf[0] = W1_DS2438_RECALL_MEMORY;
141 w1_buf[1] = 0x00;
142 w1_write_block(sl->master, w1_buf, 2);
144 if (w1_reset_select_slave(sl))
145 continue;
146 w1_buf[0] = W1_DS2438_READ_SCRATCH;
147 w1_buf[1] = 0x00;
148 w1_write_block(sl->master, w1_buf, 2);
150 /* reading one byte of result */
151 status = w1_read_8(sl->master);
153 /* if bit0=1, set a value to a mask for easy compare */
154 if (value)
155 value = mask;
157 if ((status & mask) == value)
158 return 0; /* already set as requested */
159 else {
160 /* changing bit */
161 status ^= mask;
162 perform_write = 1;
164 break;
167 if (perform_write) {
168 retries = W1_DS2438_RETRIES;
169 while (retries--) {
170 if (w1_reset_select_slave(sl))
171 continue;
172 w1_buf[0] = W1_DS2438_WRITE_SCRATCH;
173 w1_buf[1] = 0x00;
174 w1_buf[2] = status;
175 w1_write_block(sl->master, w1_buf, 3);
177 if (w1_reset_select_slave(sl))
178 continue;
179 w1_buf[0] = W1_DS2438_COPY_SCRATCH;
180 w1_buf[1] = 0x00;
181 w1_write_block(sl->master, w1_buf, 2);
183 return 0;
186 return -1;
189 static int w1_ds2438_get_voltage(struct w1_slave *sl,
190 int adc_input, uint16_t *voltage)
192 unsigned int retries = W1_DS2438_RETRIES;
193 u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
194 unsigned int tm = DS2438_MAX_CONVERSION_TIME;
195 unsigned long sleep_rem;
196 int ret;
198 mutex_lock(&sl->master->bus_mutex);
200 if (w1_ds2438_change_config_bit(sl, DS2438_STATUS_AD, adc_input)) {
201 ret = -1;
202 goto pre_unlock;
205 while (retries--) {
206 if (w1_reset_select_slave(sl))
207 continue;
208 w1_write_8(sl->master, W1_DS2438_CONVERT_VOLTAGE);
210 mutex_unlock(&sl->master->bus_mutex);
211 sleep_rem = msleep_interruptible(tm);
212 if (sleep_rem != 0) {
213 ret = -1;
214 goto post_unlock;
217 if (mutex_lock_interruptible(&sl->master->bus_mutex) != 0) {
218 ret = -1;
219 goto post_unlock;
222 break;
225 if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
226 *voltage = (((uint16_t) w1_buf[DS2438_VOLTAGE_MSB]) << 8) | ((uint16_t) w1_buf[DS2438_VOLTAGE_LSB]);
227 ret = 0;
228 } else
229 ret = -1;
231 pre_unlock:
232 mutex_unlock(&sl->master->bus_mutex);
234 post_unlock:
235 return ret;
238 static int w1_ds2438_get_current(struct w1_slave *sl, int16_t *voltage)
240 u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
241 int ret;
243 mutex_lock(&sl->master->bus_mutex);
245 if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
246 /* The voltage measured across current sense resistor RSENS. */
247 *voltage = (((int16_t) w1_buf[DS2438_CURRENT_MSB]) << 8) | ((int16_t) w1_buf[DS2438_CURRENT_LSB]);
248 ret = 0;
249 } else
250 ret = -1;
252 mutex_unlock(&sl->master->bus_mutex);
254 return ret;
257 static ssize_t iad_write(struct file *filp, struct kobject *kobj,
258 struct bin_attribute *bin_attr, char *buf,
259 loff_t off, size_t count)
261 struct w1_slave *sl = kobj_to_w1_slave(kobj);
262 int ret;
264 if (count != 1 || off != 0)
265 return -EFAULT;
267 mutex_lock(&sl->master->bus_mutex);
269 if (w1_ds2438_change_config_bit(sl, DS2438_STATUS_IAD, *buf & 0x01) == 0)
270 ret = 1;
271 else
272 ret = -EIO;
274 mutex_unlock(&sl->master->bus_mutex);
276 return ret;
279 static ssize_t iad_read(struct file *filp, struct kobject *kobj,
280 struct bin_attribute *bin_attr, char *buf,
281 loff_t off, size_t count)
283 struct w1_slave *sl = kobj_to_w1_slave(kobj);
284 int ret;
285 int16_t voltage;
287 if (off != 0)
288 return 0;
289 if (!buf)
290 return -EINVAL;
292 if (w1_ds2438_get_current(sl, &voltage) == 0) {
293 ret = snprintf(buf, count, "%i\n", voltage);
294 } else
295 ret = -EIO;
297 return ret;
300 static ssize_t page0_read(struct file *filp, struct kobject *kobj,
301 struct bin_attribute *bin_attr, char *buf,
302 loff_t off, size_t count)
304 struct w1_slave *sl = kobj_to_w1_slave(kobj);
305 int ret;
306 u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
308 if (off != 0)
309 return 0;
310 if (!buf)
311 return -EINVAL;
313 mutex_lock(&sl->master->bus_mutex);
315 /* Read no more than page0 size */
316 if (count > DS2438_PAGE_SIZE)
317 count = DS2438_PAGE_SIZE;
319 if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
320 memcpy(buf, &w1_buf, count);
321 ret = count;
322 } else
323 ret = -EIO;
325 mutex_unlock(&sl->master->bus_mutex);
327 return ret;
330 static ssize_t temperature_read(struct file *filp, struct kobject *kobj,
331 struct bin_attribute *bin_attr, char *buf,
332 loff_t off, size_t count)
334 struct w1_slave *sl = kobj_to_w1_slave(kobj);
335 int ret;
336 int16_t temp;
338 if (off != 0)
339 return 0;
340 if (!buf)
341 return -EINVAL;
343 if (w1_ds2438_get_temperature(sl, &temp) == 0) {
344 ret = snprintf(buf, count, "%i\n", temp);
345 } else
346 ret = -EIO;
348 return ret;
351 static ssize_t vad_read(struct file *filp, struct kobject *kobj,
352 struct bin_attribute *bin_attr, char *buf,
353 loff_t off, size_t count)
355 struct w1_slave *sl = kobj_to_w1_slave(kobj);
356 int ret;
357 uint16_t voltage;
359 if (off != 0)
360 return 0;
361 if (!buf)
362 return -EINVAL;
364 if (w1_ds2438_get_voltage(sl, DS2438_ADC_INPUT_VAD, &voltage) == 0) {
365 ret = snprintf(buf, count, "%u\n", voltage);
366 } else
367 ret = -EIO;
369 return ret;
372 static ssize_t vdd_read(struct file *filp, struct kobject *kobj,
373 struct bin_attribute *bin_attr, char *buf,
374 loff_t off, size_t count)
376 struct w1_slave *sl = kobj_to_w1_slave(kobj);
377 int ret;
378 uint16_t voltage;
380 if (off != 0)
381 return 0;
382 if (!buf)
383 return -EINVAL;
385 if (w1_ds2438_get_voltage(sl, DS2438_ADC_INPUT_VDD, &voltage) == 0) {
386 ret = snprintf(buf, count, "%u\n", voltage);
387 } else
388 ret = -EIO;
390 return ret;
393 static BIN_ATTR(iad, S_IRUGO | S_IWUSR | S_IWGRP, iad_read, iad_write, 0);
394 static BIN_ATTR_RO(page0, DS2438_PAGE_SIZE);
395 static BIN_ATTR_RO(temperature, 0/* real length varies */);
396 static BIN_ATTR_RO(vad, 0/* real length varies */);
397 static BIN_ATTR_RO(vdd, 0/* real length varies */);
399 static struct bin_attribute *w1_ds2438_bin_attrs[] = {
400 &bin_attr_iad,
401 &bin_attr_page0,
402 &bin_attr_temperature,
403 &bin_attr_vad,
404 &bin_attr_vdd,
405 NULL,
408 static const struct attribute_group w1_ds2438_group = {
409 .bin_attrs = w1_ds2438_bin_attrs,
412 static const struct attribute_group *w1_ds2438_groups[] = {
413 &w1_ds2438_group,
414 NULL,
417 static struct w1_family_ops w1_ds2438_fops = {
418 .groups = w1_ds2438_groups,
421 static struct w1_family w1_ds2438_family = {
422 .fid = W1_FAMILY_DS2438,
423 .fops = &w1_ds2438_fops,
425 module_w1_family(w1_ds2438_family);
427 MODULE_LICENSE("GPL");
428 MODULE_AUTHOR("Mariusz Bialonczyk <manio@skyboo.net>");
429 MODULE_DESCRIPTION("1-wire driver for Maxim/Dallas DS2438 Smart Battery Monitor");
430 MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2438));