1 // SPDX-License-Identifier: GPL-2.0-only
3 * max31722 - hwmon driver for Maxim Integrated MAX31722/MAX31723 SPI
4 * digital thermometer and thermostats.
6 * Copyright (c) 2016, Intel Corporation.
9 #include <linux/hwmon.h>
10 #include <linux/hwmon-sysfs.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/spi/spi.h>
15 #define MAX31722_REG_CFG 0x00
16 #define MAX31722_REG_TEMP_LSB 0x01
18 #define MAX31722_MODE_CONTINUOUS 0x00
19 #define MAX31722_MODE_STANDBY 0x01
20 #define MAX31722_MODE_MASK 0xFE
21 #define MAX31722_RESOLUTION_12BIT 0x06
22 #define MAX31722_WRITE_MASK 0x80
24 struct max31722_data
{
25 struct device
*hwmon_dev
;
26 struct spi_device
*spi_device
;
30 static int max31722_set_mode(struct max31722_data
*data
, u8 mode
)
33 struct spi_device
*spi
= data
->spi_device
;
35 MAX31722_REG_CFG
| MAX31722_WRITE_MASK
,
36 (data
->mode
& MAX31722_MODE_MASK
) | mode
39 ret
= spi_write(spi
, &buf
, sizeof(buf
));
41 dev_err(&spi
->dev
, "failed to set sensor mode.\n");
44 data
->mode
= (data
->mode
& MAX31722_MODE_MASK
) | mode
;
49 static ssize_t
max31722_temp_show(struct device
*dev
,
50 struct device_attribute
*attr
, char *buf
)
53 struct max31722_data
*data
= dev_get_drvdata(dev
);
55 ret
= spi_w8r16(data
->spi_device
, MAX31722_REG_TEMP_LSB
);
58 /* Keep 12 bits and multiply by the scale of 62.5 millidegrees/bit. */
59 return sprintf(buf
, "%d\n", (s16
)le16_to_cpu(ret
) * 125 / 32);
62 static SENSOR_DEVICE_ATTR_RO(temp1_input
, max31722_temp
, 0);
64 static struct attribute
*max31722_attrs
[] = {
65 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
69 ATTRIBUTE_GROUPS(max31722
);
71 static int max31722_probe(struct spi_device
*spi
)
74 struct max31722_data
*data
;
76 data
= devm_kzalloc(&spi
->dev
, sizeof(*data
), GFP_KERNEL
);
80 spi_set_drvdata(spi
, data
);
81 data
->spi_device
= spi
;
83 * Set SD bit to 0 so we can have continuous measurements.
84 * Set resolution to 12 bits for maximum precision.
86 data
->mode
= MAX31722_MODE_CONTINUOUS
| MAX31722_RESOLUTION_12BIT
;
87 ret
= max31722_set_mode(data
, MAX31722_MODE_CONTINUOUS
);
91 data
->hwmon_dev
= hwmon_device_register_with_groups(&spi
->dev
,
95 if (IS_ERR(data
->hwmon_dev
)) {
96 max31722_set_mode(data
, MAX31722_MODE_STANDBY
);
97 return PTR_ERR(data
->hwmon_dev
);
103 static void max31722_remove(struct spi_device
*spi
)
105 struct max31722_data
*data
= spi_get_drvdata(spi
);
108 hwmon_device_unregister(data
->hwmon_dev
);
110 ret
= max31722_set_mode(data
, MAX31722_MODE_STANDBY
);
112 /* There is nothing we can do about this ... */
113 dev_warn(&spi
->dev
, "Failed to put device in stand-by mode\n");
116 static int max31722_suspend(struct device
*dev
)
118 struct spi_device
*spi_device
= to_spi_device(dev
);
119 struct max31722_data
*data
= spi_get_drvdata(spi_device
);
121 return max31722_set_mode(data
, MAX31722_MODE_STANDBY
);
124 static int max31722_resume(struct device
*dev
)
126 struct spi_device
*spi_device
= to_spi_device(dev
);
127 struct max31722_data
*data
= spi_get_drvdata(spi_device
);
129 return max31722_set_mode(data
, MAX31722_MODE_CONTINUOUS
);
132 static DEFINE_SIMPLE_DEV_PM_OPS(max31722_pm_ops
, max31722_suspend
, max31722_resume
);
134 static const struct spi_device_id max31722_spi_id
[] = {
139 MODULE_DEVICE_TABLE(spi
, max31722_spi_id
);
141 static struct spi_driver max31722_driver
= {
144 .pm
= pm_sleep_ptr(&max31722_pm_ops
),
146 .probe
= max31722_probe
,
147 .remove
= max31722_remove
,
148 .id_table
= max31722_spi_id
,
151 module_spi_driver(max31722_driver
);
153 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
154 MODULE_DESCRIPTION("max31722 sensor driver");
155 MODULE_LICENSE("GPL v2");