drm/modes: Fix drm_mode_vrefres() docs
[drm/drm-misc.git] / drivers / iio / temperature / max31865.c
blob5a6fbe3c80e5f607ea122e98d1a71e83bd77e4b1
1 // SPDX-License-Identifier: GPL-2.0-only
3 /*
4 * Copyright (c) Linumiz 2021
6 * max31865.c - Maxim MAX31865 RTD-to-Digital Converter sensor driver
8 * Author: Navin Sankar Velliangiri <navin@linumiz.com>
9 */
11 #include <linux/ctype.h>
12 #include <linux/delay.h>
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/property.h>
20 #include <linux/spi/spi.h>
21 #include <linux/unaligned.h>
24 * The MSB of the register value determines whether the following byte will
25 * be written or read. If it is 0, read will follow and if it is 1, write
26 * will follow.
28 #define MAX31865_RD_WR_BIT BIT(7)
30 #define MAX31865_CFG_VBIAS BIT(7)
31 #define MAX31865_CFG_1SHOT BIT(5)
32 #define MAX31865_3WIRE_RTD BIT(4)
33 #define MAX31865_FAULT_STATUS_CLEAR BIT(1)
34 #define MAX31865_FILTER_50HZ BIT(0)
36 /* The MAX31865 registers */
37 #define MAX31865_CFG_REG 0x00
38 #define MAX31865_RTD_MSB 0x01
39 #define MAX31865_FAULT_STATUS 0x07
41 #define MAX31865_FAULT_OVUV BIT(2)
43 static const char max31865_show_samp_freq[] = "50 60";
45 static const struct iio_chan_spec max31865_channels[] = {
46 { /* RTD Temperature */
47 .type = IIO_TEMP,
48 .info_mask_separate =
49 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE)
53 struct max31865_data {
54 struct spi_device *spi;
55 struct mutex lock;
56 bool filter_50hz;
57 bool three_wire;
58 u8 buf[2] __aligned(IIO_DMA_MINALIGN);
61 static int max31865_read(struct max31865_data *data, u8 reg,
62 unsigned int read_size)
64 return spi_write_then_read(data->spi, &reg, 1, data->buf, read_size);
67 static int max31865_write(struct max31865_data *data, size_t len)
69 return spi_write(data->spi, data->buf, len);
72 static int enable_bias(struct max31865_data *data)
74 u8 cfg;
75 int ret;
77 ret = max31865_read(data, MAX31865_CFG_REG, 1);
78 if (ret)
79 return ret;
81 cfg = data->buf[0];
83 data->buf[0] = MAX31865_CFG_REG | MAX31865_RD_WR_BIT;
84 data->buf[1] = cfg | MAX31865_CFG_VBIAS;
86 return max31865_write(data, 2);
89 static int disable_bias(struct max31865_data *data)
91 u8 cfg;
92 int ret;
94 ret = max31865_read(data, MAX31865_CFG_REG, 1);
95 if (ret)
96 return ret;
98 cfg = data->buf[0];
99 cfg &= ~MAX31865_CFG_VBIAS;
101 data->buf[0] = MAX31865_CFG_REG | MAX31865_RD_WR_BIT;
102 data->buf[1] = cfg;
104 return max31865_write(data, 2);
107 static int max31865_rtd_read(struct max31865_data *data, int *val)
109 u8 reg;
110 int ret;
112 /* Enable BIAS to start the conversion */
113 ret = enable_bias(data);
114 if (ret)
115 return ret;
117 /* wait 10.5ms before initiating the conversion */
118 msleep(11);
120 ret = max31865_read(data, MAX31865_CFG_REG, 1);
121 if (ret)
122 return ret;
124 reg = data->buf[0];
125 reg |= MAX31865_CFG_1SHOT | MAX31865_FAULT_STATUS_CLEAR;
126 data->buf[0] = MAX31865_CFG_REG | MAX31865_RD_WR_BIT;
127 data->buf[1] = reg;
129 ret = max31865_write(data, 2);
130 if (ret)
131 return ret;
133 if (data->filter_50hz) {
134 /* 50Hz filter mode requires 62.5ms to complete */
135 msleep(63);
136 } else {
137 /* 60Hz filter mode requires 52ms to complete */
138 msleep(52);
141 ret = max31865_read(data, MAX31865_RTD_MSB, 2);
142 if (ret)
143 return ret;
145 *val = get_unaligned_be16(&data->buf) >> 1;
147 return disable_bias(data);
150 static int max31865_read_raw(struct iio_dev *indio_dev,
151 struct iio_chan_spec const *chan,
152 int *val, int *val2, long mask)
154 struct max31865_data *data = iio_priv(indio_dev);
155 int ret;
157 switch (mask) {
158 case IIO_CHAN_INFO_RAW:
159 mutex_lock(&data->lock);
160 ret = max31865_rtd_read(data, val);
161 mutex_unlock(&data->lock);
162 if (ret)
163 return ret;
164 return IIO_VAL_INT;
165 case IIO_CHAN_INFO_SCALE:
166 /* Temp. Data resolution is 0.03125 degree centigrade */
167 *val = 31;
168 *val2 = 250000; /* 1000 * 0.03125 */
169 return IIO_VAL_INT_PLUS_MICRO;
170 default:
171 return -EINVAL;
175 static int max31865_init(struct max31865_data *data)
177 u8 cfg;
178 int ret;
180 ret = max31865_read(data, MAX31865_CFG_REG, 1);
181 if (ret)
182 return ret;
184 cfg = data->buf[0];
186 if (data->three_wire)
187 /* 3-wire RTD connection */
188 cfg |= MAX31865_3WIRE_RTD;
190 if (data->filter_50hz)
191 /* 50Hz noise rejection filter */
192 cfg |= MAX31865_FILTER_50HZ;
194 data->buf[0] = MAX31865_CFG_REG | MAX31865_RD_WR_BIT;
195 data->buf[1] = cfg;
197 return max31865_write(data, 2);
200 static ssize_t show_fault(struct device *dev, u8 faultbit, char *buf)
202 int ret;
203 bool fault;
204 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
205 struct max31865_data *data = iio_priv(indio_dev);
207 ret = max31865_read(data, MAX31865_FAULT_STATUS, 1);
208 if (ret)
209 return ret;
211 fault = data->buf[0] & faultbit;
213 return sysfs_emit(buf, "%d\n", fault);
216 static ssize_t show_fault_ovuv(struct device *dev,
217 struct device_attribute *attr,
218 char *buf)
220 return show_fault(dev, MAX31865_FAULT_OVUV, buf);
223 static ssize_t show_filter(struct device *dev,
224 struct device_attribute *attr,
225 char *buf)
227 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
228 struct max31865_data *data = iio_priv(indio_dev);
230 return sysfs_emit(buf, "%d\n", data->filter_50hz ? 50 : 60);
233 static ssize_t set_filter(struct device *dev,
234 struct device_attribute *attr,
235 const char *buf,
236 size_t len)
238 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
239 struct max31865_data *data = iio_priv(indio_dev);
240 unsigned int freq;
241 int ret;
243 ret = kstrtouint(buf, 10, &freq);
244 if (ret)
245 return ret;
247 switch (freq) {
248 case 50:
249 data->filter_50hz = true;
250 break;
251 case 60:
252 data->filter_50hz = false;
253 break;
254 default:
255 return -EINVAL;
258 mutex_lock(&data->lock);
259 ret = max31865_init(data);
260 mutex_unlock(&data->lock);
261 if (ret)
262 return ret;
264 return len;
267 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(max31865_show_samp_freq);
268 static IIO_DEVICE_ATTR(fault_ovuv, 0444, show_fault_ovuv, NULL, 0);
269 static IIO_DEVICE_ATTR(in_filter_notch_center_frequency, 0644,
270 show_filter, set_filter, 0);
272 static struct attribute *max31865_attributes[] = {
273 &iio_dev_attr_fault_ovuv.dev_attr.attr,
274 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
275 &iio_dev_attr_in_filter_notch_center_frequency.dev_attr.attr,
276 NULL,
279 static const struct attribute_group max31865_group = {
280 .attrs = max31865_attributes,
283 static const struct iio_info max31865_info = {
284 .read_raw = max31865_read_raw,
285 .attrs = &max31865_group,
288 static int max31865_probe(struct spi_device *spi)
290 const struct spi_device_id *id = spi_get_device_id(spi);
291 struct iio_dev *indio_dev;
292 struct max31865_data *data;
293 int ret;
295 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
296 if (!indio_dev)
297 return -ENOMEM;
299 data = iio_priv(indio_dev);
300 data->spi = spi;
301 data->filter_50hz = false;
302 mutex_init(&data->lock);
304 indio_dev->info = &max31865_info;
305 indio_dev->name = id->name;
306 indio_dev->modes = INDIO_DIRECT_MODE;
307 indio_dev->channels = max31865_channels;
308 indio_dev->num_channels = ARRAY_SIZE(max31865_channels);
310 if (device_property_read_bool(&spi->dev, "maxim,3-wire")) {
311 /* select 3 wire */
312 data->three_wire = 1;
313 } else {
314 /* select 2 or 4 wire */
315 data->three_wire = 0;
318 ret = max31865_init(data);
319 if (ret) {
320 dev_err(&spi->dev, "error: Failed to configure max31865\n");
321 return ret;
324 return devm_iio_device_register(&spi->dev, indio_dev);
327 static const struct spi_device_id max31865_id[] = {
328 { "max31865", 0 },
331 MODULE_DEVICE_TABLE(spi, max31865_id);
333 static const struct of_device_id max31865_of_match[] = {
334 { .compatible = "maxim,max31865" },
337 MODULE_DEVICE_TABLE(of, max31865_of_match);
339 static struct spi_driver max31865_driver = {
340 .driver = {
341 .name = "max31865",
342 .of_match_table = max31865_of_match,
344 .probe = max31865_probe,
345 .id_table = max31865_id,
347 module_spi_driver(max31865_driver);
349 MODULE_AUTHOR("Navin Sankar Velliangiri <navin@linumiz.com>");
350 MODULE_DESCRIPTION("Maxim MAX31865 RTD-to-Digital Converter sensor driver");
351 MODULE_LICENSE("GPL v2");