140b0b3f8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e3af95e6SMariusz Bialonczyk /*
3e3af95e6SMariusz Bialonczyk * 1-Wire implementation for the ds2438 chip
4e3af95e6SMariusz Bialonczyk *
5e3af95e6SMariusz Bialonczyk * Copyright (c) 2017 Mariusz Bialonczyk <manio@skyboo.net>
6e3af95e6SMariusz Bialonczyk */
7e3af95e6SMariusz Bialonczyk
8e3af95e6SMariusz Bialonczyk #include <linux/kernel.h>
9e3af95e6SMariusz Bialonczyk #include <linux/module.h>
10e3af95e6SMariusz Bialonczyk #include <linux/device.h>
11e3af95e6SMariusz Bialonczyk #include <linux/types.h>
12e3af95e6SMariusz Bialonczyk #include <linux/delay.h>
13e3af95e6SMariusz Bialonczyk
14de0d6dbdSAndrew F. Davis #include <linux/w1.h>
15de0d6dbdSAndrew F. Davis
16de0d6dbdSAndrew F. Davis #define W1_FAMILY_DS2438 0x26
17e3af95e6SMariusz Bialonczyk
18e3af95e6SMariusz Bialonczyk #define W1_DS2438_RETRIES 3
19e3af95e6SMariusz Bialonczyk
20e3af95e6SMariusz Bialonczyk /* Memory commands */
21e3af95e6SMariusz Bialonczyk #define W1_DS2438_READ_SCRATCH 0xBE
22e3af95e6SMariusz Bialonczyk #define W1_DS2438_WRITE_SCRATCH 0x4E
23e3af95e6SMariusz Bialonczyk #define W1_DS2438_COPY_SCRATCH 0x48
24e3af95e6SMariusz Bialonczyk #define W1_DS2438_RECALL_MEMORY 0xB8
25e3af95e6SMariusz Bialonczyk /* Register commands */
26e3af95e6SMariusz Bialonczyk #define W1_DS2438_CONVERT_TEMP 0x44
27e3af95e6SMariusz Bialonczyk #define W1_DS2438_CONVERT_VOLTAGE 0xB4
28e3af95e6SMariusz Bialonczyk
29e3af95e6SMariusz Bialonczyk #define DS2438_PAGE_SIZE 8
30e3af95e6SMariusz Bialonczyk #define DS2438_ADC_INPUT_VAD 0
31e3af95e6SMariusz Bialonczyk #define DS2438_ADC_INPUT_VDD 1
32e3af95e6SMariusz Bialonczyk #define DS2438_MAX_CONVERSION_TIME 10 /* ms */
33e3af95e6SMariusz Bialonczyk
34e3af95e6SMariusz Bialonczyk /* Page #0 definitions */
35e3af95e6SMariusz Bialonczyk #define DS2438_STATUS_REG 0x00 /* Status/Configuration Register */
36e3af95e6SMariusz Bialonczyk #define DS2438_STATUS_IAD (1 << 0) /* Current A/D Control Bit */
37e3af95e6SMariusz Bialonczyk #define DS2438_STATUS_CA (1 << 1) /* Current Accumulator Configuration */
38e3af95e6SMariusz Bialonczyk #define DS2438_STATUS_EE (1 << 2) /* Current Accumulator Shadow Selector bit */
39e3af95e6SMariusz Bialonczyk #define DS2438_STATUS_AD (1 << 3) /* Voltage A/D Input Select Bit */
40e3af95e6SMariusz Bialonczyk #define DS2438_STATUS_TB (1 << 4) /* Temperature Busy Flag */
41e3af95e6SMariusz Bialonczyk #define DS2438_STATUS_NVB (1 << 5) /* Nonvolatile Memory Busy Flag */
42e3af95e6SMariusz Bialonczyk #define DS2438_STATUS_ADB (1 << 6) /* A/D Converter Busy Flag */
43e3af95e6SMariusz Bialonczyk
44e3af95e6SMariusz Bialonczyk #define DS2438_TEMP_LSB 0x01
45e3af95e6SMariusz Bialonczyk #define DS2438_TEMP_MSB 0x02
46e3af95e6SMariusz Bialonczyk #define DS2438_VOLTAGE_LSB 0x03
47e3af95e6SMariusz Bialonczyk #define DS2438_VOLTAGE_MSB 0x04
48e3af95e6SMariusz Bialonczyk #define DS2438_CURRENT_LSB 0x05
49e3af95e6SMariusz Bialonczyk #define DS2438_CURRENT_MSB 0x06
50e3af95e6SMariusz Bialonczyk #define DS2438_THRESHOLD 0x07
51e3af95e6SMariusz Bialonczyk
52fd6ec5d7SLuiz Sampaio /* Page #1 definitions */
53fd6ec5d7SLuiz Sampaio #define DS2438_ETM_0 0x00
54fd6ec5d7SLuiz Sampaio #define DS2438_ETM_1 0x01
55fd6ec5d7SLuiz Sampaio #define DS2438_ETM_2 0x02
56fd6ec5d7SLuiz Sampaio #define DS2438_ETM_3 0x03
57fd6ec5d7SLuiz Sampaio #define DS2438_ICA 0x04
58fd6ec5d7SLuiz Sampaio #define DS2438_OFFSET_LSB 0x05
59fd6ec5d7SLuiz Sampaio #define DS2438_OFFSET_MSB 0x06
60fd6ec5d7SLuiz Sampaio
w1_ds2438_get_page(struct w1_slave * sl,int pageno,u8 * buf)61e5cf84e8SColin Ian King static int w1_ds2438_get_page(struct w1_slave *sl, int pageno, u8 *buf)
62e3af95e6SMariusz Bialonczyk {
63e3af95e6SMariusz Bialonczyk unsigned int retries = W1_DS2438_RETRIES;
64e3af95e6SMariusz Bialonczyk u8 w1_buf[2];
65e3af95e6SMariusz Bialonczyk u8 crc;
66e3af95e6SMariusz Bialonczyk size_t count;
67e3af95e6SMariusz Bialonczyk
68e3af95e6SMariusz Bialonczyk while (retries--) {
69e3af95e6SMariusz Bialonczyk if (w1_reset_select_slave(sl))
70e3af95e6SMariusz Bialonczyk continue;
71e3af95e6SMariusz Bialonczyk w1_buf[0] = W1_DS2438_RECALL_MEMORY;
721f5e7518SLuiz Sampaio w1_buf[1] = (u8)pageno;
73e3af95e6SMariusz Bialonczyk w1_write_block(sl->master, w1_buf, 2);
74e3af95e6SMariusz Bialonczyk
75e3af95e6SMariusz Bialonczyk if (w1_reset_select_slave(sl))
76e3af95e6SMariusz Bialonczyk continue;
77e3af95e6SMariusz Bialonczyk w1_buf[0] = W1_DS2438_READ_SCRATCH;
781f5e7518SLuiz Sampaio w1_buf[1] = (u8)pageno;
79e3af95e6SMariusz Bialonczyk w1_write_block(sl->master, w1_buf, 2);
80e3af95e6SMariusz Bialonczyk
81e3af95e6SMariusz Bialonczyk count = w1_read_block(sl->master, buf, DS2438_PAGE_SIZE + 1);
82e3af95e6SMariusz Bialonczyk if (count == DS2438_PAGE_SIZE + 1) {
83e3af95e6SMariusz Bialonczyk crc = w1_calc_crc8(buf, DS2438_PAGE_SIZE);
84e3af95e6SMariusz Bialonczyk
85e3af95e6SMariusz Bialonczyk /* check for correct CRC */
86e3af95e6SMariusz Bialonczyk if ((u8)buf[DS2438_PAGE_SIZE] == crc)
87e3af95e6SMariusz Bialonczyk return 0;
88e3af95e6SMariusz Bialonczyk }
89e3af95e6SMariusz Bialonczyk }
90e3af95e6SMariusz Bialonczyk return -1;
91e3af95e6SMariusz Bialonczyk }
92e3af95e6SMariusz Bialonczyk
w1_ds2438_get_temperature(struct w1_slave * sl,int16_t * temperature)93e5cf84e8SColin Ian King static int w1_ds2438_get_temperature(struct w1_slave *sl, int16_t *temperature)
94e3af95e6SMariusz Bialonczyk {
95e3af95e6SMariusz Bialonczyk unsigned int retries = W1_DS2438_RETRIES;
96e3af95e6SMariusz Bialonczyk u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
97e3af95e6SMariusz Bialonczyk unsigned int tm = DS2438_MAX_CONVERSION_TIME;
98e3af95e6SMariusz Bialonczyk unsigned long sleep_rem;
99e3af95e6SMariusz Bialonczyk int ret;
100e3af95e6SMariusz Bialonczyk
101e3af95e6SMariusz Bialonczyk mutex_lock(&sl->master->bus_mutex);
102e3af95e6SMariusz Bialonczyk
103e3af95e6SMariusz Bialonczyk while (retries--) {
104e3af95e6SMariusz Bialonczyk if (w1_reset_select_slave(sl))
105e3af95e6SMariusz Bialonczyk continue;
106e3af95e6SMariusz Bialonczyk w1_write_8(sl->master, W1_DS2438_CONVERT_TEMP);
107e3af95e6SMariusz Bialonczyk
108e3af95e6SMariusz Bialonczyk mutex_unlock(&sl->master->bus_mutex);
109e3af95e6SMariusz Bialonczyk sleep_rem = msleep_interruptible(tm);
110e3af95e6SMariusz Bialonczyk if (sleep_rem != 0) {
111e3af95e6SMariusz Bialonczyk ret = -1;
112e3af95e6SMariusz Bialonczyk goto post_unlock;
113e3af95e6SMariusz Bialonczyk }
114e3af95e6SMariusz Bialonczyk
115e3af95e6SMariusz Bialonczyk if (mutex_lock_interruptible(&sl->master->bus_mutex) != 0) {
116e3af95e6SMariusz Bialonczyk ret = -1;
117e3af95e6SMariusz Bialonczyk goto post_unlock;
118e3af95e6SMariusz Bialonczyk }
119e3af95e6SMariusz Bialonczyk
120e3af95e6SMariusz Bialonczyk break;
121e3af95e6SMariusz Bialonczyk }
122e3af95e6SMariusz Bialonczyk
123e3af95e6SMariusz Bialonczyk if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
124e3af95e6SMariusz Bialonczyk *temperature = (((int16_t) w1_buf[DS2438_TEMP_MSB]) << 8) | ((uint16_t) w1_buf[DS2438_TEMP_LSB]);
125e3af95e6SMariusz Bialonczyk ret = 0;
126e3af95e6SMariusz Bialonczyk } else
127e3af95e6SMariusz Bialonczyk ret = -1;
128e3af95e6SMariusz Bialonczyk
129e3af95e6SMariusz Bialonczyk mutex_unlock(&sl->master->bus_mutex);
130e3af95e6SMariusz Bialonczyk
131e3af95e6SMariusz Bialonczyk post_unlock:
132e3af95e6SMariusz Bialonczyk return ret;
133e3af95e6SMariusz Bialonczyk }
134e3af95e6SMariusz Bialonczyk
w1_ds2438_change_config_bit(struct w1_slave * sl,u8 mask,u8 value)135e5cf84e8SColin Ian King static int w1_ds2438_change_config_bit(struct w1_slave *sl, u8 mask, u8 value)
136e3af95e6SMariusz Bialonczyk {
137e3af95e6SMariusz Bialonczyk unsigned int retries = W1_DS2438_RETRIES;
138e3af95e6SMariusz Bialonczyk u8 w1_buf[3];
139e3af95e6SMariusz Bialonczyk u8 status;
140e3af95e6SMariusz Bialonczyk int perform_write = 0;
141e3af95e6SMariusz Bialonczyk
142e3af95e6SMariusz Bialonczyk while (retries--) {
143e3af95e6SMariusz Bialonczyk if (w1_reset_select_slave(sl))
144e3af95e6SMariusz Bialonczyk continue;
145e3af95e6SMariusz Bialonczyk w1_buf[0] = W1_DS2438_RECALL_MEMORY;
146e3af95e6SMariusz Bialonczyk w1_buf[1] = 0x00;
147e3af95e6SMariusz Bialonczyk w1_write_block(sl->master, w1_buf, 2);
148e3af95e6SMariusz Bialonczyk
149e3af95e6SMariusz Bialonczyk if (w1_reset_select_slave(sl))
150e3af95e6SMariusz Bialonczyk continue;
151e3af95e6SMariusz Bialonczyk w1_buf[0] = W1_DS2438_READ_SCRATCH;
152e3af95e6SMariusz Bialonczyk w1_buf[1] = 0x00;
153e3af95e6SMariusz Bialonczyk w1_write_block(sl->master, w1_buf, 2);
154e3af95e6SMariusz Bialonczyk
155e3af95e6SMariusz Bialonczyk /* reading one byte of result */
156e3af95e6SMariusz Bialonczyk status = w1_read_8(sl->master);
157e3af95e6SMariusz Bialonczyk
158e3af95e6SMariusz Bialonczyk /* if bit0=1, set a value to a mask for easy compare */
159e3af95e6SMariusz Bialonczyk if (value)
160e3af95e6SMariusz Bialonczyk value = mask;
161e3af95e6SMariusz Bialonczyk
162e3af95e6SMariusz Bialonczyk if ((status & mask) == value)
163e3af95e6SMariusz Bialonczyk return 0; /* already set as requested */
164ddb20bcfSLuiz Sampaio
165e3af95e6SMariusz Bialonczyk /* changing bit */
166e3af95e6SMariusz Bialonczyk status ^= mask;
167e3af95e6SMariusz Bialonczyk perform_write = 1;
168ddb20bcfSLuiz Sampaio
169e3af95e6SMariusz Bialonczyk break;
170e3af95e6SMariusz Bialonczyk }
171e3af95e6SMariusz Bialonczyk
172e3af95e6SMariusz Bialonczyk if (perform_write) {
173e3af95e6SMariusz Bialonczyk retries = W1_DS2438_RETRIES;
174e3af95e6SMariusz Bialonczyk while (retries--) {
175e3af95e6SMariusz Bialonczyk if (w1_reset_select_slave(sl))
176e3af95e6SMariusz Bialonczyk continue;
177e3af95e6SMariusz Bialonczyk w1_buf[0] = W1_DS2438_WRITE_SCRATCH;
178e3af95e6SMariusz Bialonczyk w1_buf[1] = 0x00;
179e3af95e6SMariusz Bialonczyk w1_buf[2] = status;
180e3af95e6SMariusz Bialonczyk w1_write_block(sl->master, w1_buf, 3);
181e3af95e6SMariusz Bialonczyk
182e3af95e6SMariusz Bialonczyk if (w1_reset_select_slave(sl))
183e3af95e6SMariusz Bialonczyk continue;
184e3af95e6SMariusz Bialonczyk w1_buf[0] = W1_DS2438_COPY_SCRATCH;
185e3af95e6SMariusz Bialonczyk w1_buf[1] = 0x00;
186e3af95e6SMariusz Bialonczyk w1_write_block(sl->master, w1_buf, 2);
187e3af95e6SMariusz Bialonczyk
188e3af95e6SMariusz Bialonczyk return 0;
189e3af95e6SMariusz Bialonczyk }
190e3af95e6SMariusz Bialonczyk }
191e3af95e6SMariusz Bialonczyk return -1;
192e3af95e6SMariusz Bialonczyk }
193e3af95e6SMariusz Bialonczyk
w1_ds2438_change_offset_register(struct w1_slave * sl,u8 * value)194*c999fbbdSLuiz Sampaio static int w1_ds2438_change_offset_register(struct w1_slave *sl, u8 *value)
195*c999fbbdSLuiz Sampaio {
196*c999fbbdSLuiz Sampaio unsigned int retries = W1_DS2438_RETRIES;
197*c999fbbdSLuiz Sampaio u8 w1_buf[9];
198*c999fbbdSLuiz Sampaio u8 w1_page1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
199*c999fbbdSLuiz Sampaio
200*c999fbbdSLuiz Sampaio if (w1_ds2438_get_page(sl, 1, w1_page1_buf) == 0) {
201*c999fbbdSLuiz Sampaio memcpy(&w1_buf[2], w1_page1_buf, DS2438_PAGE_SIZE - 1); /* last register reserved */
202*c999fbbdSLuiz Sampaio w1_buf[7] = value[0]; /* change only offset register */
203*c999fbbdSLuiz Sampaio w1_buf[8] = value[1];
204*c999fbbdSLuiz Sampaio while (retries--) {
205*c999fbbdSLuiz Sampaio if (w1_reset_select_slave(sl))
206*c999fbbdSLuiz Sampaio continue;
207*c999fbbdSLuiz Sampaio w1_buf[0] = W1_DS2438_WRITE_SCRATCH;
208*c999fbbdSLuiz Sampaio w1_buf[1] = 0x01; /* write to page 1 */
209*c999fbbdSLuiz Sampaio w1_write_block(sl->master, w1_buf, 9);
210*c999fbbdSLuiz Sampaio
211*c999fbbdSLuiz Sampaio if (w1_reset_select_slave(sl))
212*c999fbbdSLuiz Sampaio continue;
213*c999fbbdSLuiz Sampaio w1_buf[0] = W1_DS2438_COPY_SCRATCH;
214*c999fbbdSLuiz Sampaio w1_buf[1] = 0x01;
215*c999fbbdSLuiz Sampaio w1_write_block(sl->master, w1_buf, 2);
216*c999fbbdSLuiz Sampaio return 0;
217*c999fbbdSLuiz Sampaio }
218*c999fbbdSLuiz Sampaio }
219*c999fbbdSLuiz Sampaio return -1;
220*c999fbbdSLuiz Sampaio }
221*c999fbbdSLuiz Sampaio
w1_ds2438_get_voltage(struct w1_slave * sl,int adc_input,uint16_t * voltage)2226eaafbb6SJulien Folly static int w1_ds2438_get_voltage(struct w1_slave *sl,
223e5cf84e8SColin Ian King int adc_input, uint16_t *voltage)
224e3af95e6SMariusz Bialonczyk {
225e3af95e6SMariusz Bialonczyk unsigned int retries = W1_DS2438_RETRIES;
226e3af95e6SMariusz Bialonczyk u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
227e3af95e6SMariusz Bialonczyk unsigned int tm = DS2438_MAX_CONVERSION_TIME;
228e3af95e6SMariusz Bialonczyk unsigned long sleep_rem;
229e3af95e6SMariusz Bialonczyk int ret;
230e3af95e6SMariusz Bialonczyk
231e3af95e6SMariusz Bialonczyk mutex_lock(&sl->master->bus_mutex);
232e3af95e6SMariusz Bialonczyk
233e3af95e6SMariusz Bialonczyk if (w1_ds2438_change_config_bit(sl, DS2438_STATUS_AD, adc_input)) {
234e3af95e6SMariusz Bialonczyk ret = -1;
235e3af95e6SMariusz Bialonczyk goto pre_unlock;
236e3af95e6SMariusz Bialonczyk }
237e3af95e6SMariusz Bialonczyk
238e3af95e6SMariusz Bialonczyk while (retries--) {
239e3af95e6SMariusz Bialonczyk if (w1_reset_select_slave(sl))
240e3af95e6SMariusz Bialonczyk continue;
241e3af95e6SMariusz Bialonczyk w1_write_8(sl->master, W1_DS2438_CONVERT_VOLTAGE);
242e3af95e6SMariusz Bialonczyk
243e3af95e6SMariusz Bialonczyk mutex_unlock(&sl->master->bus_mutex);
244e3af95e6SMariusz Bialonczyk sleep_rem = msleep_interruptible(tm);
245e3af95e6SMariusz Bialonczyk if (sleep_rem != 0) {
246e3af95e6SMariusz Bialonczyk ret = -1;
247e3af95e6SMariusz Bialonczyk goto post_unlock;
248e3af95e6SMariusz Bialonczyk }
249e3af95e6SMariusz Bialonczyk
250e3af95e6SMariusz Bialonczyk if (mutex_lock_interruptible(&sl->master->bus_mutex) != 0) {
251e3af95e6SMariusz Bialonczyk ret = -1;
252e3af95e6SMariusz Bialonczyk goto post_unlock;
253e3af95e6SMariusz Bialonczyk }
254e3af95e6SMariusz Bialonczyk
255e3af95e6SMariusz Bialonczyk break;
256e3af95e6SMariusz Bialonczyk }
257e3af95e6SMariusz Bialonczyk
258e3af95e6SMariusz Bialonczyk if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
259e3af95e6SMariusz Bialonczyk *voltage = (((uint16_t) w1_buf[DS2438_VOLTAGE_MSB]) << 8) | ((uint16_t) w1_buf[DS2438_VOLTAGE_LSB]);
260e3af95e6SMariusz Bialonczyk ret = 0;
261e3af95e6SMariusz Bialonczyk } else
262e3af95e6SMariusz Bialonczyk ret = -1;
263e3af95e6SMariusz Bialonczyk
264e3af95e6SMariusz Bialonczyk pre_unlock:
265e3af95e6SMariusz Bialonczyk mutex_unlock(&sl->master->bus_mutex);
266e3af95e6SMariusz Bialonczyk
267e3af95e6SMariusz Bialonczyk post_unlock:
268e3af95e6SMariusz Bialonczyk return ret;
269e3af95e6SMariusz Bialonczyk }
270e3af95e6SMariusz Bialonczyk
w1_ds2438_get_current(struct w1_slave * sl,int16_t * voltage)2716eaafbb6SJulien Folly static int w1_ds2438_get_current(struct w1_slave *sl, int16_t *voltage)
2726eaafbb6SJulien Folly {
2736eaafbb6SJulien Folly u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
2746eaafbb6SJulien Folly int ret;
2756eaafbb6SJulien Folly
2766eaafbb6SJulien Folly mutex_lock(&sl->master->bus_mutex);
2776eaafbb6SJulien Folly
2786eaafbb6SJulien Folly if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
2796eaafbb6SJulien Folly /* The voltage measured across current sense resistor RSENS. */
2806eaafbb6SJulien Folly *voltage = (((int16_t) w1_buf[DS2438_CURRENT_MSB]) << 8) | ((int16_t) w1_buf[DS2438_CURRENT_LSB]);
2816eaafbb6SJulien Folly ret = 0;
2826eaafbb6SJulien Folly } else
2836eaafbb6SJulien Folly ret = -1;
2846eaafbb6SJulien Folly
2856eaafbb6SJulien Folly mutex_unlock(&sl->master->bus_mutex);
2866eaafbb6SJulien Folly
2876eaafbb6SJulien Folly return ret;
2886eaafbb6SJulien Folly }
2896eaafbb6SJulien Folly
iad_write(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)290e3af95e6SMariusz Bialonczyk static ssize_t iad_write(struct file *filp, struct kobject *kobj,
291e3af95e6SMariusz Bialonczyk struct bin_attribute *bin_attr, char *buf,
292e3af95e6SMariusz Bialonczyk loff_t off, size_t count)
293e3af95e6SMariusz Bialonczyk {
294e3af95e6SMariusz Bialonczyk struct w1_slave *sl = kobj_to_w1_slave(kobj);
295e3af95e6SMariusz Bialonczyk int ret;
296e3af95e6SMariusz Bialonczyk
297e3af95e6SMariusz Bialonczyk if (count != 1 || off != 0)
298e3af95e6SMariusz Bialonczyk return -EFAULT;
299e3af95e6SMariusz Bialonczyk
300e3af95e6SMariusz Bialonczyk mutex_lock(&sl->master->bus_mutex);
301e3af95e6SMariusz Bialonczyk
302e3af95e6SMariusz Bialonczyk if (w1_ds2438_change_config_bit(sl, DS2438_STATUS_IAD, *buf & 0x01) == 0)
303e3af95e6SMariusz Bialonczyk ret = 1;
304e3af95e6SMariusz Bialonczyk else
305e3af95e6SMariusz Bialonczyk ret = -EIO;
306e3af95e6SMariusz Bialonczyk
307e3af95e6SMariusz Bialonczyk mutex_unlock(&sl->master->bus_mutex);
308e3af95e6SMariusz Bialonczyk
309e3af95e6SMariusz Bialonczyk return ret;
310e3af95e6SMariusz Bialonczyk }
311e3af95e6SMariusz Bialonczyk
iad_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)3126eaafbb6SJulien Folly static ssize_t iad_read(struct file *filp, struct kobject *kobj,
3136eaafbb6SJulien Folly struct bin_attribute *bin_attr, char *buf,
3146eaafbb6SJulien Folly loff_t off, size_t count)
3156eaafbb6SJulien Folly {
3166eaafbb6SJulien Folly struct w1_slave *sl = kobj_to_w1_slave(kobj);
3176eaafbb6SJulien Folly int ret;
3186eaafbb6SJulien Folly int16_t voltage;
3196eaafbb6SJulien Folly
3206eaafbb6SJulien Folly if (off != 0)
3216eaafbb6SJulien Folly return 0;
3226eaafbb6SJulien Folly if (!buf)
3236eaafbb6SJulien Folly return -EINVAL;
3246eaafbb6SJulien Folly
325c9f27135SLuiz Sampaio if (w1_ds2438_get_current(sl, &voltage) == 0)
3266eaafbb6SJulien Folly ret = snprintf(buf, count, "%i\n", voltage);
327c9f27135SLuiz Sampaio else
3286eaafbb6SJulien Folly ret = -EIO;
3296eaafbb6SJulien Folly
3306eaafbb6SJulien Folly return ret;
3316eaafbb6SJulien Folly }
3326eaafbb6SJulien Folly
page0_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)333e3af95e6SMariusz Bialonczyk static ssize_t page0_read(struct file *filp, struct kobject *kobj,
334e3af95e6SMariusz Bialonczyk struct bin_attribute *bin_attr, char *buf,
335e3af95e6SMariusz Bialonczyk loff_t off, size_t count)
336e3af95e6SMariusz Bialonczyk {
337e3af95e6SMariusz Bialonczyk struct w1_slave *sl = kobj_to_w1_slave(kobj);
338e3af95e6SMariusz Bialonczyk int ret;
339e3af95e6SMariusz Bialonczyk u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
340e3af95e6SMariusz Bialonczyk
341e3af95e6SMariusz Bialonczyk if (off != 0)
342e3af95e6SMariusz Bialonczyk return 0;
343e3af95e6SMariusz Bialonczyk if (!buf)
344e3af95e6SMariusz Bialonczyk return -EINVAL;
345e3af95e6SMariusz Bialonczyk
346e3af95e6SMariusz Bialonczyk mutex_lock(&sl->master->bus_mutex);
347e3af95e6SMariusz Bialonczyk
3486eaafbb6SJulien Folly /* Read no more than page0 size */
3496eaafbb6SJulien Folly if (count > DS2438_PAGE_SIZE)
3506eaafbb6SJulien Folly count = DS2438_PAGE_SIZE;
3516eaafbb6SJulien Folly
352e3af95e6SMariusz Bialonczyk if (w1_ds2438_get_page(sl, 0, w1_buf) == 0) {
3536eaafbb6SJulien Folly memcpy(buf, &w1_buf, count);
3546eaafbb6SJulien Folly ret = count;
355e3af95e6SMariusz Bialonczyk } else
356e3af95e6SMariusz Bialonczyk ret = -EIO;
357e3af95e6SMariusz Bialonczyk
358e3af95e6SMariusz Bialonczyk mutex_unlock(&sl->master->bus_mutex);
359e3af95e6SMariusz Bialonczyk
360e3af95e6SMariusz Bialonczyk return ret;
361e3af95e6SMariusz Bialonczyk }
362e3af95e6SMariusz Bialonczyk
page1_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)363fd6ec5d7SLuiz Sampaio static ssize_t page1_read(struct file *filp, struct kobject *kobj,
364fd6ec5d7SLuiz Sampaio struct bin_attribute *bin_attr, char *buf,
365fd6ec5d7SLuiz Sampaio loff_t off, size_t count)
366fd6ec5d7SLuiz Sampaio {
367fd6ec5d7SLuiz Sampaio struct w1_slave *sl = kobj_to_w1_slave(kobj);
368fd6ec5d7SLuiz Sampaio int ret;
369fd6ec5d7SLuiz Sampaio u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
370fd6ec5d7SLuiz Sampaio
371fd6ec5d7SLuiz Sampaio if (off != 0)
372fd6ec5d7SLuiz Sampaio return 0;
373fd6ec5d7SLuiz Sampaio if (!buf)
374fd6ec5d7SLuiz Sampaio return -EINVAL;
375fd6ec5d7SLuiz Sampaio
376fd6ec5d7SLuiz Sampaio mutex_lock(&sl->master->bus_mutex);
377fd6ec5d7SLuiz Sampaio
378fd6ec5d7SLuiz Sampaio /* Read no more than page1 size */
379fd6ec5d7SLuiz Sampaio if (count > DS2438_PAGE_SIZE)
380fd6ec5d7SLuiz Sampaio count = DS2438_PAGE_SIZE;
381fd6ec5d7SLuiz Sampaio
382fd6ec5d7SLuiz Sampaio if (w1_ds2438_get_page(sl, 1, w1_buf) == 0) {
383fd6ec5d7SLuiz Sampaio memcpy(buf, &w1_buf, count);
384fd6ec5d7SLuiz Sampaio ret = count;
385fd6ec5d7SLuiz Sampaio } else
386fd6ec5d7SLuiz Sampaio ret = -EIO;
387fd6ec5d7SLuiz Sampaio
388fd6ec5d7SLuiz Sampaio mutex_unlock(&sl->master->bus_mutex);
389fd6ec5d7SLuiz Sampaio
390fd6ec5d7SLuiz Sampaio return ret;
391fd6ec5d7SLuiz Sampaio }
392fd6ec5d7SLuiz Sampaio
offset_write(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)393*c999fbbdSLuiz Sampaio static ssize_t offset_write(struct file *filp, struct kobject *kobj,
394*c999fbbdSLuiz Sampaio struct bin_attribute *bin_attr, char *buf,
395*c999fbbdSLuiz Sampaio loff_t off, size_t count)
396*c999fbbdSLuiz Sampaio {
397*c999fbbdSLuiz Sampaio struct w1_slave *sl = kobj_to_w1_slave(kobj);
398*c999fbbdSLuiz Sampaio int ret;
399*c999fbbdSLuiz Sampaio
400*c999fbbdSLuiz Sampaio mutex_lock(&sl->master->bus_mutex);
401*c999fbbdSLuiz Sampaio
402*c999fbbdSLuiz Sampaio if (w1_ds2438_change_offset_register(sl, buf) == 0)
403*c999fbbdSLuiz Sampaio ret = count;
404*c999fbbdSLuiz Sampaio else
405*c999fbbdSLuiz Sampaio ret = -EIO;
406*c999fbbdSLuiz Sampaio
407*c999fbbdSLuiz Sampaio mutex_unlock(&sl->master->bus_mutex);
408*c999fbbdSLuiz Sampaio
409*c999fbbdSLuiz Sampaio return ret;
410*c999fbbdSLuiz Sampaio }
411*c999fbbdSLuiz Sampaio
temperature_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)412e3af95e6SMariusz Bialonczyk static ssize_t temperature_read(struct file *filp, struct kobject *kobj,
413e3af95e6SMariusz Bialonczyk struct bin_attribute *bin_attr, char *buf,
414e3af95e6SMariusz Bialonczyk loff_t off, size_t count)
415e3af95e6SMariusz Bialonczyk {
416e3af95e6SMariusz Bialonczyk struct w1_slave *sl = kobj_to_w1_slave(kobj);
417e3af95e6SMariusz Bialonczyk int ret;
418e3af95e6SMariusz Bialonczyk int16_t temp;
419e3af95e6SMariusz Bialonczyk
420e3af95e6SMariusz Bialonczyk if (off != 0)
421e3af95e6SMariusz Bialonczyk return 0;
422e3af95e6SMariusz Bialonczyk if (!buf)
423e3af95e6SMariusz Bialonczyk return -EINVAL;
424e3af95e6SMariusz Bialonczyk
425c9f27135SLuiz Sampaio if (w1_ds2438_get_temperature(sl, &temp) == 0)
4266eaafbb6SJulien Folly ret = snprintf(buf, count, "%i\n", temp);
427c9f27135SLuiz Sampaio else
428e3af95e6SMariusz Bialonczyk ret = -EIO;
429e3af95e6SMariusz Bialonczyk
430e3af95e6SMariusz Bialonczyk return ret;
431e3af95e6SMariusz Bialonczyk }
432e3af95e6SMariusz Bialonczyk
vad_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)433e3af95e6SMariusz Bialonczyk static ssize_t vad_read(struct file *filp, struct kobject *kobj,
434e3af95e6SMariusz Bialonczyk struct bin_attribute *bin_attr, char *buf,
435e3af95e6SMariusz Bialonczyk loff_t off, size_t count)
436e3af95e6SMariusz Bialonczyk {
437e3af95e6SMariusz Bialonczyk struct w1_slave *sl = kobj_to_w1_slave(kobj);
438e3af95e6SMariusz Bialonczyk int ret;
439e3af95e6SMariusz Bialonczyk uint16_t voltage;
440e3af95e6SMariusz Bialonczyk
441e3af95e6SMariusz Bialonczyk if (off != 0)
442e3af95e6SMariusz Bialonczyk return 0;
443e3af95e6SMariusz Bialonczyk if (!buf)
444e3af95e6SMariusz Bialonczyk return -EINVAL;
445e3af95e6SMariusz Bialonczyk
446c9f27135SLuiz Sampaio if (w1_ds2438_get_voltage(sl, DS2438_ADC_INPUT_VAD, &voltage) == 0)
4476eaafbb6SJulien Folly ret = snprintf(buf, count, "%u\n", voltage);
448c9f27135SLuiz Sampaio else
449e3af95e6SMariusz Bialonczyk ret = -EIO;
450e3af95e6SMariusz Bialonczyk
451e3af95e6SMariusz Bialonczyk return ret;
452e3af95e6SMariusz Bialonczyk }
453e3af95e6SMariusz Bialonczyk
vdd_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)454e3af95e6SMariusz Bialonczyk static ssize_t vdd_read(struct file *filp, struct kobject *kobj,
455e3af95e6SMariusz Bialonczyk struct bin_attribute *bin_attr, char *buf,
456e3af95e6SMariusz Bialonczyk loff_t off, size_t count)
457e3af95e6SMariusz Bialonczyk {
458e3af95e6SMariusz Bialonczyk struct w1_slave *sl = kobj_to_w1_slave(kobj);
459e3af95e6SMariusz Bialonczyk int ret;
460e3af95e6SMariusz Bialonczyk uint16_t voltage;
461e3af95e6SMariusz Bialonczyk
462e3af95e6SMariusz Bialonczyk if (off != 0)
463e3af95e6SMariusz Bialonczyk return 0;
464e3af95e6SMariusz Bialonczyk if (!buf)
465e3af95e6SMariusz Bialonczyk return -EINVAL;
466e3af95e6SMariusz Bialonczyk
467c9f27135SLuiz Sampaio if (w1_ds2438_get_voltage(sl, DS2438_ADC_INPUT_VDD, &voltage) == 0)
4686eaafbb6SJulien Folly ret = snprintf(buf, count, "%u\n", voltage);
469c9f27135SLuiz Sampaio else
470e3af95e6SMariusz Bialonczyk ret = -EIO;
471e3af95e6SMariusz Bialonczyk
472e3af95e6SMariusz Bialonczyk return ret;
473e3af95e6SMariusz Bialonczyk }
474e3af95e6SMariusz Bialonczyk
47567c69642SLuiz Sampaio static BIN_ATTR_RW(iad, 0);
476e3af95e6SMariusz Bialonczyk static BIN_ATTR_RO(page0, DS2438_PAGE_SIZE);
477fd6ec5d7SLuiz Sampaio static BIN_ATTR_RO(page1, DS2438_PAGE_SIZE);
478*c999fbbdSLuiz Sampaio static BIN_ATTR_WO(offset, 2);
479e3af95e6SMariusz Bialonczyk static BIN_ATTR_RO(temperature, 0/* real length varies */);
480e3af95e6SMariusz Bialonczyk static BIN_ATTR_RO(vad, 0/* real length varies */);
481e3af95e6SMariusz Bialonczyk static BIN_ATTR_RO(vdd, 0/* real length varies */);
482e3af95e6SMariusz Bialonczyk
483e3af95e6SMariusz Bialonczyk static struct bin_attribute *w1_ds2438_bin_attrs[] = {
484e3af95e6SMariusz Bialonczyk &bin_attr_iad,
485e3af95e6SMariusz Bialonczyk &bin_attr_page0,
486fd6ec5d7SLuiz Sampaio &bin_attr_page1,
487*c999fbbdSLuiz Sampaio &bin_attr_offset,
488e3af95e6SMariusz Bialonczyk &bin_attr_temperature,
489e3af95e6SMariusz Bialonczyk &bin_attr_vad,
490e3af95e6SMariusz Bialonczyk &bin_attr_vdd,
491e3af95e6SMariusz Bialonczyk NULL,
492e3af95e6SMariusz Bialonczyk };
493e3af95e6SMariusz Bialonczyk
494e3af95e6SMariusz Bialonczyk static const struct attribute_group w1_ds2438_group = {
495e3af95e6SMariusz Bialonczyk .bin_attrs = w1_ds2438_bin_attrs,
496e3af95e6SMariusz Bialonczyk };
497e3af95e6SMariusz Bialonczyk
498e3af95e6SMariusz Bialonczyk static const struct attribute_group *w1_ds2438_groups[] = {
499e3af95e6SMariusz Bialonczyk &w1_ds2438_group,
500e3af95e6SMariusz Bialonczyk NULL,
501e3af95e6SMariusz Bialonczyk };
502e3af95e6SMariusz Bialonczyk
50357de2dfcSRikard Falkeborn static const struct w1_family_ops w1_ds2438_fops = {
504e3af95e6SMariusz Bialonczyk .groups = w1_ds2438_groups,
505e3af95e6SMariusz Bialonczyk };
506e3af95e6SMariusz Bialonczyk
507e3af95e6SMariusz Bialonczyk static struct w1_family w1_ds2438_family = {
508e3af95e6SMariusz Bialonczyk .fid = W1_FAMILY_DS2438,
509e3af95e6SMariusz Bialonczyk .fops = &w1_ds2438_fops,
510e3af95e6SMariusz Bialonczyk };
511e3af95e6SMariusz Bialonczyk module_w1_family(w1_ds2438_family);
512e3af95e6SMariusz Bialonczyk
513e3af95e6SMariusz Bialonczyk MODULE_LICENSE("GPL");
514e3af95e6SMariusz Bialonczyk MODULE_AUTHOR("Mariusz Bialonczyk <manio@skyboo.net>");
515e3af95e6SMariusz Bialonczyk MODULE_DESCRIPTION("1-wire driver for Maxim/Dallas DS2438 Smart Battery Monitor");
516e3af95e6SMariusz Bialonczyk MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2438));
517