This source file includes following definitions.
- max31856_read
- max31856_write
- max31856_init
- max31856_thermocouple_read
- max31856_read_raw
- show_fault
- show_fault_ovuv
- show_fault_oc
- max31856_probe
1
2
3
4
5
6
7
8
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/err.h>
12 #include <linux/spi/spi.h>
13 #include <linux/iio/iio.h>
14 #include <linux/iio/sysfs.h>
15 #include <dt-bindings/iio/temperature/thermocouple.h>
16
17
18
19
20 #define MAX31856_RD_WR_BIT BIT(7)
21
22 #define MAX31856_CR0_AUTOCONVERT BIT(7)
23 #define MAX31856_CR0_1SHOT BIT(6)
24 #define MAX31856_CR0_OCFAULT BIT(4)
25 #define MAX31856_CR0_OCFAULT_MASK GENMASK(5, 4)
26 #define MAX31856_TC_TYPE_MASK GENMASK(3, 0)
27 #define MAX31856_FAULT_OVUV BIT(1)
28 #define MAX31856_FAULT_OPEN BIT(0)
29
30
31 #define MAX31856_CR0_REG 0x00
32 #define MAX31856_CR1_REG 0x01
33 #define MAX31856_MASK_REG 0x02
34 #define MAX31856_CJHF_REG 0x03
35 #define MAX31856_CJLF_REG 0x04
36 #define MAX31856_LTHFTH_REG 0x05
37 #define MAX31856_LTHFTL_REG 0x06
38 #define MAX31856_LTLFTH_REG 0x07
39 #define MAX31856_LTLFTL_REG 0x08
40 #define MAX31856_CJTO_REG 0x09
41 #define MAX31856_CJTH_REG 0x0A
42 #define MAX31856_CJTL_REG 0x0B
43 #define MAX31856_LTCBH_REG 0x0C
44 #define MAX31856_LTCBM_REG 0x0D
45 #define MAX31856_LTCBL_REG 0x0E
46 #define MAX31856_SR_REG 0x0F
47
48 static const struct iio_chan_spec max31856_channels[] = {
49 {
50 .type = IIO_TEMP,
51 .info_mask_separate =
52 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
53 },
54 {
55 .type = IIO_TEMP,
56 .channel2 = IIO_MOD_TEMP_AMBIENT,
57 .modified = 1,
58 .info_mask_separate =
59 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
60 },
61 };
62
63 struct max31856_data {
64 struct spi_device *spi;
65 u32 thermocouple_type;
66 };
67
68 static int max31856_read(struct max31856_data *data, u8 reg,
69 u8 val[], unsigned int read_size)
70 {
71 return spi_write_then_read(data->spi, ®, 1, val, read_size);
72 }
73
74 static int max31856_write(struct max31856_data *data, u8 reg,
75 unsigned int val)
76 {
77 u8 buf[2];
78
79 buf[0] = reg | (MAX31856_RD_WR_BIT);
80 buf[1] = val;
81
82 return spi_write(data->spi, buf, 2);
83 }
84
85 static int max31856_init(struct max31856_data *data)
86 {
87 int ret;
88 u8 reg_cr0_val, reg_cr1_val;
89
90
91
92
93
94 ret = max31856_read(data, MAX31856_CR0_REG, ®_cr0_val, 1);
95 if (ret)
96 return ret;
97
98 reg_cr0_val &= ~MAX31856_CR0_AUTOCONVERT;
99 ret = max31856_write(data, MAX31856_CR0_REG, reg_cr0_val);
100 if (ret)
101 return ret;
102
103
104 ret = max31856_read(data, MAX31856_CR1_REG, ®_cr1_val, 1);
105 if (ret)
106 return ret;
107
108 reg_cr1_val &= ~MAX31856_TC_TYPE_MASK;
109 reg_cr1_val |= data->thermocouple_type;
110 ret = max31856_write(data, MAX31856_CR1_REG, reg_cr1_val);
111 if (ret)
112 return ret;
113
114
115
116
117
118
119 reg_cr0_val &= ~MAX31856_CR0_OCFAULT_MASK;
120 reg_cr0_val |= MAX31856_CR0_OCFAULT;
121
122
123 reg_cr0_val &= ~MAX31856_CR0_1SHOT;
124 reg_cr0_val |= MAX31856_CR0_AUTOCONVERT;
125
126 return max31856_write(data, MAX31856_CR0_REG, reg_cr0_val);
127 }
128
129 static int max31856_thermocouple_read(struct max31856_data *data,
130 struct iio_chan_spec const *chan,
131 int *val)
132 {
133 int ret, offset_cjto;
134 u8 reg_val[3];
135
136 switch (chan->channel2) {
137 case IIO_NO_MOD:
138
139
140
141
142 ret = max31856_read(data, MAX31856_LTCBH_REG, reg_val, 3);
143 if (ret)
144 return ret;
145
146 *val = (reg_val[0] << 16 | reg_val[1] << 8 | reg_val[2]) >> 5;
147
148 if (reg_val[0] & 0x80)
149 *val -= 0x80000;
150 break;
151
152 case IIO_MOD_TEMP_AMBIENT:
153
154
155
156
157 ret = max31856_read(data, MAX31856_CJTO_REG, reg_val, 3);
158 if (ret)
159 return ret;
160
161 offset_cjto = reg_val[0];
162
163 *val = (reg_val[1] << 8 | reg_val[2]) >> 2;
164
165 *val += offset_cjto;
166
167 if (reg_val[1] & 0x80)
168 *val -= 0x4000;
169 break;
170
171 default:
172 return -EINVAL;
173 }
174
175 ret = max31856_read(data, MAX31856_SR_REG, reg_val, 1);
176 if (ret)
177 return ret;
178
179 if (reg_val[0] & (MAX31856_FAULT_OVUV | MAX31856_FAULT_OPEN))
180 return -EIO;
181
182 return ret;
183 }
184
185 static int max31856_read_raw(struct iio_dev *indio_dev,
186 struct iio_chan_spec const *chan,
187 int *val, int *val2, long mask)
188 {
189 struct max31856_data *data = iio_priv(indio_dev);
190 int ret;
191
192 switch (mask) {
193 case IIO_CHAN_INFO_RAW:
194 ret = max31856_thermocouple_read(data, chan, val);
195 if (ret)
196 return ret;
197 return IIO_VAL_INT;
198 case IIO_CHAN_INFO_SCALE:
199 switch (chan->channel2) {
200 case IIO_MOD_TEMP_AMBIENT:
201
202 *val = 15;
203 *val2 = 625000;
204 ret = IIO_VAL_INT_PLUS_MICRO;
205 break;
206 default:
207
208 *val = 7;
209 *val2 = 812500;
210 return IIO_VAL_INT_PLUS_MICRO;
211 }
212 break;
213 default:
214 ret = -EINVAL;
215 break;
216 }
217
218 return ret;
219 }
220
221 static ssize_t show_fault(struct device *dev, u8 faultbit, char *buf)
222 {
223 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
224 struct max31856_data *data = iio_priv(indio_dev);
225 u8 reg_val;
226 int ret;
227 bool fault;
228
229 ret = max31856_read(data, MAX31856_SR_REG, ®_val, 1);
230 if (ret)
231 return ret;
232
233 fault = reg_val & faultbit;
234
235 return sprintf(buf, "%d\n", fault);
236 }
237
238 static ssize_t show_fault_ovuv(struct device *dev,
239 struct device_attribute *attr,
240 char *buf)
241 {
242 return show_fault(dev, MAX31856_FAULT_OVUV, buf);
243 }
244
245 static ssize_t show_fault_oc(struct device *dev,
246 struct device_attribute *attr,
247 char *buf)
248 {
249 return show_fault(dev, MAX31856_FAULT_OPEN, buf);
250 }
251
252 static IIO_DEVICE_ATTR(fault_ovuv, 0444, show_fault_ovuv, NULL, 0);
253 static IIO_DEVICE_ATTR(fault_oc, 0444, show_fault_oc, NULL, 0);
254
255 static struct attribute *max31856_attributes[] = {
256 &iio_dev_attr_fault_ovuv.dev_attr.attr,
257 &iio_dev_attr_fault_oc.dev_attr.attr,
258 NULL,
259 };
260
261 static const struct attribute_group max31856_group = {
262 .attrs = max31856_attributes,
263 };
264
265 static const struct iio_info max31856_info = {
266 .read_raw = max31856_read_raw,
267 .attrs = &max31856_group,
268 };
269
270 static int max31856_probe(struct spi_device *spi)
271 {
272 const struct spi_device_id *id = spi_get_device_id(spi);
273 struct iio_dev *indio_dev;
274 struct max31856_data *data;
275 int ret;
276
277 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
278 if (!indio_dev)
279 return -ENOMEM;
280
281 data = iio_priv(indio_dev);
282 data->spi = spi;
283
284 spi_set_drvdata(spi, indio_dev);
285
286 indio_dev->info = &max31856_info;
287 indio_dev->dev.parent = &spi->dev;
288 indio_dev->dev.of_node = spi->dev.of_node;
289 indio_dev->name = id->name;
290 indio_dev->modes = INDIO_DIRECT_MODE;
291 indio_dev->channels = max31856_channels;
292 indio_dev->num_channels = ARRAY_SIZE(max31856_channels);
293
294 ret = of_property_read_u32(spi->dev.of_node, "thermocouple-type",
295 &data->thermocouple_type);
296
297 if (ret) {
298 dev_info(&spi->dev,
299 "Could not read thermocouple type DT property, configuring as a K-Type\n");
300 data->thermocouple_type = THERMOCOUPLE_TYPE_K;
301 }
302
303
304
305
306
307 switch (data->thermocouple_type) {
308 case THERMOCOUPLE_TYPE_B:
309 case THERMOCOUPLE_TYPE_E:
310 case THERMOCOUPLE_TYPE_J:
311 case THERMOCOUPLE_TYPE_K:
312 case THERMOCOUPLE_TYPE_N:
313 case THERMOCOUPLE_TYPE_R:
314 case THERMOCOUPLE_TYPE_S:
315 case THERMOCOUPLE_TYPE_T:
316 break;
317 default:
318 dev_err(&spi->dev,
319 "error: thermocouple-type %u not supported by max31856\n"
320 , data->thermocouple_type);
321 return -EINVAL;
322 }
323
324 ret = max31856_init(data);
325 if (ret) {
326 dev_err(&spi->dev, "error: Failed to configure max31856\n");
327 return ret;
328 }
329
330 return devm_iio_device_register(&spi->dev, indio_dev);
331 }
332
333 static const struct spi_device_id max31856_id[] = {
334 { "max31856", 0 },
335 { }
336 };
337 MODULE_DEVICE_TABLE(spi, max31856_id);
338
339 static const struct of_device_id max31856_of_match[] = {
340 { .compatible = "maxim,max31856" },
341 { }
342 };
343 MODULE_DEVICE_TABLE(of, max31856_of_match);
344
345 static struct spi_driver max31856_driver = {
346 .driver = {
347 .name = "max31856",
348 .of_match_table = max31856_of_match,
349 },
350 .probe = max31856_probe,
351 .id_table = max31856_id,
352 };
353 module_spi_driver(max31856_driver);
354
355 MODULE_AUTHOR("Paresh Chaudhary <paresh.chaudhary@rockwellcollins.com>");
356 MODULE_AUTHOR("Patrick Havelange <patrick.havelange@essensium.com>");
357 MODULE_DESCRIPTION("Maxim MAX31856 thermocouple sensor driver");
358 MODULE_LICENSE("GPL");