xref: /openbmc/linux/drivers/hwmon/max31722.c (revision 8e8e69d6)
1 /*
2  * max31722 - hwmon driver for Maxim Integrated MAX31722/MAX31723 SPI
3  * digital thermometer and thermostats.
4  *
5  * Copyright (c) 2016, Intel Corporation.
6  *
7  * This file is subject to the terms and conditions of version 2 of
8  * the GNU General Public License. See the file COPYING in the main
9  * directory of this archive for more details.
10  */
11 
12 #include <linux/acpi.h>
13 #include <linux/hwmon.h>
14 #include <linux/hwmon-sysfs.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/spi/spi.h>
18 
19 #define MAX31722_REG_CFG				0x00
20 #define MAX31722_REG_TEMP_LSB				0x01
21 
22 #define MAX31722_MODE_CONTINUOUS			0x00
23 #define MAX31722_MODE_STANDBY				0x01
24 #define MAX31722_MODE_MASK				0xFE
25 #define MAX31722_RESOLUTION_12BIT			0x06
26 #define MAX31722_WRITE_MASK				0x80
27 
28 struct max31722_data {
29 	struct device *hwmon_dev;
30 	struct spi_device *spi_device;
31 	u8 mode;
32 };
33 
34 static int max31722_set_mode(struct max31722_data *data, u8 mode)
35 {
36 	int ret;
37 	struct spi_device *spi = data->spi_device;
38 	u8 buf[2] = {
39 		MAX31722_REG_CFG | MAX31722_WRITE_MASK,
40 		(data->mode & MAX31722_MODE_MASK) | mode
41 	};
42 
43 	ret = spi_write(spi, &buf, sizeof(buf));
44 	if (ret < 0) {
45 		dev_err(&spi->dev, "failed to set sensor mode.\n");
46 		return ret;
47 	}
48 	data->mode = (data->mode & MAX31722_MODE_MASK) | mode;
49 
50 	return 0;
51 }
52 
53 static ssize_t max31722_temp_show(struct device *dev,
54 				  struct device_attribute *attr, char *buf)
55 {
56 	ssize_t ret;
57 	struct max31722_data *data = dev_get_drvdata(dev);
58 
59 	ret = spi_w8r16(data->spi_device, MAX31722_REG_TEMP_LSB);
60 	if (ret < 0)
61 		return ret;
62 	/* Keep 12 bits and multiply by the scale of 62.5 millidegrees/bit. */
63 	return sprintf(buf, "%d\n", (s16)le16_to_cpu(ret) * 125 / 32);
64 }
65 
66 static SENSOR_DEVICE_ATTR_RO(temp1_input, max31722_temp, 0);
67 
68 static struct attribute *max31722_attrs[] = {
69 	&sensor_dev_attr_temp1_input.dev_attr.attr,
70 	NULL,
71 };
72 
73 ATTRIBUTE_GROUPS(max31722);
74 
75 static int max31722_probe(struct spi_device *spi)
76 {
77 	int ret;
78 	struct max31722_data *data;
79 
80 	data = devm_kzalloc(&spi->dev, sizeof(*data), GFP_KERNEL);
81 	if (!data)
82 		return -ENOMEM;
83 
84 	spi_set_drvdata(spi, data);
85 	data->spi_device = spi;
86 	/*
87 	 * Set SD bit to 0 so we can have continuous measurements.
88 	 * Set resolution to 12 bits for maximum precision.
89 	 */
90 	data->mode = MAX31722_MODE_CONTINUOUS | MAX31722_RESOLUTION_12BIT;
91 	ret = max31722_set_mode(data, MAX31722_MODE_CONTINUOUS);
92 	if (ret < 0)
93 		return ret;
94 
95 	data->hwmon_dev = hwmon_device_register_with_groups(&spi->dev,
96 							    spi->modalias,
97 							    data,
98 							    max31722_groups);
99 	if (IS_ERR(data->hwmon_dev)) {
100 		max31722_set_mode(data, MAX31722_MODE_STANDBY);
101 		return PTR_ERR(data->hwmon_dev);
102 	}
103 
104 	return 0;
105 }
106 
107 static int max31722_remove(struct spi_device *spi)
108 {
109 	struct max31722_data *data = spi_get_drvdata(spi);
110 
111 	hwmon_device_unregister(data->hwmon_dev);
112 
113 	return max31722_set_mode(data, MAX31722_MODE_STANDBY);
114 }
115 
116 static int __maybe_unused max31722_suspend(struct device *dev)
117 {
118 	struct spi_device *spi_device = to_spi_device(dev);
119 	struct max31722_data *data = spi_get_drvdata(spi_device);
120 
121 	return max31722_set_mode(data, MAX31722_MODE_STANDBY);
122 }
123 
124 static int __maybe_unused max31722_resume(struct device *dev)
125 {
126 	struct spi_device *spi_device = to_spi_device(dev);
127 	struct max31722_data *data = spi_get_drvdata(spi_device);
128 
129 	return max31722_set_mode(data, MAX31722_MODE_CONTINUOUS);
130 }
131 
132 static SIMPLE_DEV_PM_OPS(max31722_pm_ops, max31722_suspend, max31722_resume);
133 
134 static const struct spi_device_id max31722_spi_id[] = {
135 	{"max31722", 0},
136 	{"max31723", 0},
137 	{}
138 };
139 
140 static const struct acpi_device_id __maybe_unused max31722_acpi_id[] = {
141 	{"MAX31722", 0},
142 	{"MAX31723", 0},
143 	{}
144 };
145 
146 MODULE_DEVICE_TABLE(spi, max31722_spi_id);
147 
148 static struct spi_driver max31722_driver = {
149 	.driver = {
150 		.name = "max31722",
151 		.pm = &max31722_pm_ops,
152 		.acpi_match_table = ACPI_PTR(max31722_acpi_id),
153 	},
154 	.probe =            max31722_probe,
155 	.remove =           max31722_remove,
156 	.id_table =         max31722_spi_id,
157 };
158 
159 module_spi_driver(max31722_driver);
160 
161 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
162 MODULE_DESCRIPTION("max31722 sensor driver");
163 MODULE_LICENSE("GPL v2");
164