xref: /linux/drivers/hwmon/max31722.c (revision 662fa3d6099374c4615bf64d06895e3573b935b2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * max31722 - hwmon driver for Maxim Integrated MAX31722/MAX31723 SPI
4  * digital thermometer and thermostats.
5  *
6  * Copyright (c) 2016, Intel Corporation.
7  */
8 
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>
14 
15 #define MAX31722_REG_CFG				0x00
16 #define MAX31722_REG_TEMP_LSB				0x01
17 
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
23 
24 struct max31722_data {
25 	struct device *hwmon_dev;
26 	struct spi_device *spi_device;
27 	u8 mode;
28 };
29 
30 static int max31722_set_mode(struct max31722_data *data, u8 mode)
31 {
32 	int ret;
33 	struct spi_device *spi = data->spi_device;
34 	u8 buf[2] = {
35 		MAX31722_REG_CFG | MAX31722_WRITE_MASK,
36 		(data->mode & MAX31722_MODE_MASK) | mode
37 	};
38 
39 	ret = spi_write(spi, &buf, sizeof(buf));
40 	if (ret < 0) {
41 		dev_err(&spi->dev, "failed to set sensor mode.\n");
42 		return ret;
43 	}
44 	data->mode = (data->mode & MAX31722_MODE_MASK) | mode;
45 
46 	return 0;
47 }
48 
49 static ssize_t max31722_temp_show(struct device *dev,
50 				  struct device_attribute *attr, char *buf)
51 {
52 	ssize_t ret;
53 	struct max31722_data *data = dev_get_drvdata(dev);
54 
55 	ret = spi_w8r16(data->spi_device, MAX31722_REG_TEMP_LSB);
56 	if (ret < 0)
57 		return ret;
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);
60 }
61 
62 static SENSOR_DEVICE_ATTR_RO(temp1_input, max31722_temp, 0);
63 
64 static struct attribute *max31722_attrs[] = {
65 	&sensor_dev_attr_temp1_input.dev_attr.attr,
66 	NULL,
67 };
68 
69 ATTRIBUTE_GROUPS(max31722);
70 
71 static int max31722_probe(struct spi_device *spi)
72 {
73 	int ret;
74 	struct max31722_data *data;
75 
76 	data = devm_kzalloc(&spi->dev, sizeof(*data), GFP_KERNEL);
77 	if (!data)
78 		return -ENOMEM;
79 
80 	spi_set_drvdata(spi, data);
81 	data->spi_device = spi;
82 	/*
83 	 * Set SD bit to 0 so we can have continuous measurements.
84 	 * Set resolution to 12 bits for maximum precision.
85 	 */
86 	data->mode = MAX31722_MODE_CONTINUOUS | MAX31722_RESOLUTION_12BIT;
87 	ret = max31722_set_mode(data, MAX31722_MODE_CONTINUOUS);
88 	if (ret < 0)
89 		return ret;
90 
91 	data->hwmon_dev = hwmon_device_register_with_groups(&spi->dev,
92 							    spi->modalias,
93 							    data,
94 							    max31722_groups);
95 	if (IS_ERR(data->hwmon_dev)) {
96 		max31722_set_mode(data, MAX31722_MODE_STANDBY);
97 		return PTR_ERR(data->hwmon_dev);
98 	}
99 
100 	return 0;
101 }
102 
103 static int max31722_remove(struct spi_device *spi)
104 {
105 	struct max31722_data *data = spi_get_drvdata(spi);
106 
107 	hwmon_device_unregister(data->hwmon_dev);
108 
109 	return max31722_set_mode(data, MAX31722_MODE_STANDBY);
110 }
111 
112 static int __maybe_unused max31722_suspend(struct device *dev)
113 {
114 	struct spi_device *spi_device = to_spi_device(dev);
115 	struct max31722_data *data = spi_get_drvdata(spi_device);
116 
117 	return max31722_set_mode(data, MAX31722_MODE_STANDBY);
118 }
119 
120 static int __maybe_unused max31722_resume(struct device *dev)
121 {
122 	struct spi_device *spi_device = to_spi_device(dev);
123 	struct max31722_data *data = spi_get_drvdata(spi_device);
124 
125 	return max31722_set_mode(data, MAX31722_MODE_CONTINUOUS);
126 }
127 
128 static SIMPLE_DEV_PM_OPS(max31722_pm_ops, max31722_suspend, max31722_resume);
129 
130 static const struct spi_device_id max31722_spi_id[] = {
131 	{"max31722", 0},
132 	{"max31723", 0},
133 	{}
134 };
135 MODULE_DEVICE_TABLE(spi, max31722_spi_id);
136 
137 static struct spi_driver max31722_driver = {
138 	.driver = {
139 		.name = "max31722",
140 		.pm = &max31722_pm_ops,
141 	},
142 	.probe =            max31722_probe,
143 	.remove =           max31722_remove,
144 	.id_table =         max31722_spi_id,
145 };
146 
147 module_spi_driver(max31722_driver);
148 
149 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
150 MODULE_DESCRIPTION("max31722 sensor driver");
151 MODULE_LICENSE("GPL v2");
152