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
max31722_set_mode(struct max31722_data * data,u8 mode)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
max31722_temp_show(struct device * dev,struct device_attribute * attr,char * buf)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
max31722_probe(struct spi_device * spi)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
max31722_remove(struct spi_device * spi)103 static void 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
max31722_suspend(struct device * dev)116 static int 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
max31722_resume(struct device * dev)124 static int 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 DEFINE_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 MODULE_DEVICE_TABLE(spi, max31722_spi_id);
140
141 static struct spi_driver max31722_driver = {
142 .driver = {
143 .name = "max31722",
144 .pm = pm_sleep_ptr(&max31722_pm_ops),
145 },
146 .probe = max31722_probe,
147 .remove = max31722_remove,
148 .id_table = max31722_spi_id,
149 };
150
151 module_spi_driver(max31722_driver);
152
153 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
154 MODULE_DESCRIPTION("max31722 sensor driver");
155 MODULE_LICENSE("GPL v2");
156