2 * max31722 - hwmon driver for Maxim Integrated MAX31722/MAX31723 SPI
3 * digital thermometer and thermostats.
5 * Copyright (c) 2016, Intel Corporation.
7 * This file is subject to the terms and conditions of version 2 of
8 * the GNU General Public License. See the file COPYING in the main
9 * directory of this archive for more details.
12 #include <linux/hwmon.h>
13 #include <linux/hwmon-sysfs.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/spi/spi.h>
18 #define MAX31722_REG_CFG 0x00
19 #define MAX31722_REG_TEMP_LSB 0x01
21 #define MAX31722_MODE_CONTINUOUS 0x00
22 #define MAX31722_MODE_STANDBY 0x01
23 #define MAX31722_MODE_MASK 0xFE
24 #define MAX31722_RESOLUTION_12BIT 0x06
25 #define MAX31722_WRITE_MASK 0x80
27 struct max31722_data {
28 struct device *hwmon_dev;
29 struct spi_device *spi_device;
33 static int max31722_set_mode(struct max31722_data *data, u8 mode)
36 struct spi_device *spi = data->spi_device;
38 MAX31722_REG_CFG | MAX31722_WRITE_MASK,
39 (data->mode & MAX31722_MODE_MASK) | mode
42 ret = spi_write(spi, &buf, sizeof(buf));
44 dev_err(&spi->dev, "failed to set sensor mode.\n");
47 data->mode = (data->mode & MAX31722_MODE_MASK) | mode;
52 static ssize_t max31722_show_temp(struct device *dev,
53 struct device_attribute *attr,
57 struct max31722_data *data = dev_get_drvdata(dev);
59 ret = spi_w8r16(data->spi_device, MAX31722_REG_TEMP_LSB);
62 /* Keep 12 bits and multiply by the scale of 62.5 millidegrees/bit. */
63 return sprintf(buf, "%d\n", (s16)le16_to_cpu(ret) * 125 / 32);
66 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
67 max31722_show_temp, NULL, 0);
69 static struct attribute *max31722_attrs[] = {
70 &sensor_dev_attr_temp1_input.dev_attr.attr,
74 ATTRIBUTE_GROUPS(max31722);
76 static int max31722_probe(struct spi_device *spi)
79 struct max31722_data *data;
81 data = devm_kzalloc(&spi->dev, sizeof(*data), GFP_KERNEL);
85 spi_set_drvdata(spi, data);
86 data->spi_device = spi;
88 * Set SD bit to 0 so we can have continuous measurements.
89 * Set resolution to 12 bits for maximum precision.
91 data->mode = MAX31722_MODE_CONTINUOUS | MAX31722_RESOLUTION_12BIT;
92 ret = max31722_set_mode(data, MAX31722_MODE_CONTINUOUS);
96 data->hwmon_dev = hwmon_device_register_with_groups(&spi->dev,
100 if (IS_ERR(data->hwmon_dev)) {
101 max31722_set_mode(data, MAX31722_MODE_STANDBY);
102 return PTR_ERR(data->hwmon_dev);
108 static int max31722_remove(struct spi_device *spi)
110 struct max31722_data *data = spi_get_drvdata(spi);
112 hwmon_device_unregister(data->hwmon_dev);
114 return max31722_set_mode(data, MAX31722_MODE_STANDBY);
117 static int __maybe_unused max31722_suspend(struct device *dev)
119 struct spi_device *spi_device = to_spi_device(dev);
120 struct max31722_data *data = spi_get_drvdata(spi_device);
122 return max31722_set_mode(data, MAX31722_MODE_STANDBY);
125 static int __maybe_unused max31722_resume(struct device *dev)
127 struct spi_device *spi_device = to_spi_device(dev);
128 struct max31722_data *data = spi_get_drvdata(spi_device);
130 return max31722_set_mode(data, MAX31722_MODE_CONTINUOUS);
133 static SIMPLE_DEV_PM_OPS(max31722_pm_ops, max31722_suspend, max31722_resume);
135 static const struct spi_device_id max31722_spi_id[] = {
140 MODULE_DEVICE_TABLE(spi, max31722_spi_id);
142 static struct spi_driver max31722_driver = {
145 .pm = &max31722_pm_ops,
147 .probe = max31722_probe,
148 .remove = max31722_remove,
149 .id_table = max31722_spi_id,
152 module_spi_driver(max31722_driver);
154 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
155 MODULE_DESCRIPTION("max31722 sensor driver");
156 MODULE_LICENSE("GPL v2");