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