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/acpi.h>
10 #include <linux/hwmon.h>
11 #include <linux/hwmon-sysfs.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/spi/spi.h>
16 #define MAX31722_REG_CFG 0x00
17 #define MAX31722_REG_TEMP_LSB 0x01
19 #define MAX31722_MODE_CONTINUOUS 0x00
20 #define MAX31722_MODE_STANDBY 0x01
21 #define MAX31722_MODE_MASK 0xFE
22 #define MAX31722_RESOLUTION_12BIT 0x06
23 #define MAX31722_WRITE_MASK 0x80
25 struct max31722_data
{
26 struct device
*hwmon_dev
;
27 struct spi_device
*spi_device
;
31 static int max31722_set_mode(struct max31722_data
*data
, u8 mode
)
34 struct spi_device
*spi
= data
->spi_device
;
36 MAX31722_REG_CFG
| MAX31722_WRITE_MASK
,
37 (data
->mode
& MAX31722_MODE_MASK
) | mode
40 ret
= spi_write(spi
, &buf
, sizeof(buf
));
42 dev_err(&spi
->dev
, "failed to set sensor mode.\n");
45 data
->mode
= (data
->mode
& MAX31722_MODE_MASK
) | mode
;
50 static ssize_t
max31722_temp_show(struct device
*dev
,
51 struct device_attribute
*attr
, char *buf
)
54 struct max31722_data
*data
= dev_get_drvdata(dev
);
56 ret
= spi_w8r16(data
->spi_device
, MAX31722_REG_TEMP_LSB
);
59 /* Keep 12 bits and multiply by the scale of 62.5 millidegrees/bit. */
60 return sprintf(buf
, "%d\n", (s16
)le16_to_cpu(ret
) * 125 / 32);
63 static SENSOR_DEVICE_ATTR_RO(temp1_input
, max31722_temp
, 0);
65 static struct attribute
*max31722_attrs
[] = {
66 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
70 ATTRIBUTE_GROUPS(max31722
);
72 static int max31722_probe(struct spi_device
*spi
)
75 struct max31722_data
*data
;
77 data
= devm_kzalloc(&spi
->dev
, sizeof(*data
), GFP_KERNEL
);
81 spi_set_drvdata(spi
, data
);
82 data
->spi_device
= spi
;
84 * Set SD bit to 0 so we can have continuous measurements.
85 * Set resolution to 12 bits for maximum precision.
87 data
->mode
= MAX31722_MODE_CONTINUOUS
| MAX31722_RESOLUTION_12BIT
;
88 ret
= max31722_set_mode(data
, MAX31722_MODE_CONTINUOUS
);
92 data
->hwmon_dev
= hwmon_device_register_with_groups(&spi
->dev
,
96 if (IS_ERR(data
->hwmon_dev
)) {
97 max31722_set_mode(data
, MAX31722_MODE_STANDBY
);
98 return PTR_ERR(data
->hwmon_dev
);
104 static int max31722_remove(struct spi_device
*spi
)
106 struct max31722_data
*data
= spi_get_drvdata(spi
);
108 hwmon_device_unregister(data
->hwmon_dev
);
110 return max31722_set_mode(data
, MAX31722_MODE_STANDBY
);
113 static int __maybe_unused
max31722_suspend(struct device
*dev
)
115 struct spi_device
*spi_device
= to_spi_device(dev
);
116 struct max31722_data
*data
= spi_get_drvdata(spi_device
);
118 return max31722_set_mode(data
, MAX31722_MODE_STANDBY
);
121 static int __maybe_unused
max31722_resume(struct device
*dev
)
123 struct spi_device
*spi_device
= to_spi_device(dev
);
124 struct max31722_data
*data
= spi_get_drvdata(spi_device
);
126 return max31722_set_mode(data
, MAX31722_MODE_CONTINUOUS
);
129 static SIMPLE_DEV_PM_OPS(max31722_pm_ops
, max31722_suspend
, max31722_resume
);
131 static const struct spi_device_id max31722_spi_id
[] = {
137 static const struct acpi_device_id __maybe_unused max31722_acpi_id
[] = {
143 MODULE_DEVICE_TABLE(spi
, max31722_spi_id
);
145 static struct spi_driver max31722_driver
= {
148 .pm
= &max31722_pm_ops
,
149 .acpi_match_table
= ACPI_PTR(max31722_acpi_id
),
151 .probe
= max31722_probe
,
152 .remove
= max31722_remove
,
153 .id_table
= max31722_spi_id
,
156 module_spi_driver(max31722_driver
);
158 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
159 MODULE_DESCRIPTION("max31722 sensor driver");
160 MODULE_LICENSE("GPL v2");