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