xref: /openbmc/linux/drivers/hwmon/max31722.c (revision 1f012283)
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 	int ret;
107 
108 	hwmon_device_unregister(data->hwmon_dev);
109 
110 	ret = max31722_set_mode(data, MAX31722_MODE_STANDBY);
111 	if (ret)
112 		/* There is nothing we can do about this ... */
113 		dev_warn(&spi->dev, "Failed to put device in stand-by mode\n");
114 
115 	return 0;
116 }
117 
118 static int __maybe_unused max31722_suspend(struct device *dev)
119 {
120 	struct spi_device *spi_device = to_spi_device(dev);
121 	struct max31722_data *data = spi_get_drvdata(spi_device);
122 
123 	return max31722_set_mode(data, MAX31722_MODE_STANDBY);
124 }
125 
126 static int __maybe_unused max31722_resume(struct device *dev)
127 {
128 	struct spi_device *spi_device = to_spi_device(dev);
129 	struct max31722_data *data = spi_get_drvdata(spi_device);
130 
131 	return max31722_set_mode(data, MAX31722_MODE_CONTINUOUS);
132 }
133 
134 static SIMPLE_DEV_PM_OPS(max31722_pm_ops, max31722_suspend, max31722_resume);
135 
136 static const struct spi_device_id max31722_spi_id[] = {
137 	{"max31722", 0},
138 	{"max31723", 0},
139 	{}
140 };
141 MODULE_DEVICE_TABLE(spi, max31722_spi_id);
142 
143 static struct spi_driver max31722_driver = {
144 	.driver = {
145 		.name = "max31722",
146 		.pm = &max31722_pm_ops,
147 	},
148 	.probe =            max31722_probe,
149 	.remove =           max31722_remove,
150 	.id_table =         max31722_spi_id,
151 };
152 
153 module_spi_driver(max31722_driver);
154 
155 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
156 MODULE_DESCRIPTION("max31722 sensor driver");
157 MODULE_LICENSE("GPL v2");
158