projects/linux·blob master · refs

156 lines · 3.9 KiB

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#include <linux/sysfs.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
25struct max31722_data {
26 struct device *hwmon_dev;
27 struct spi_device *spi_device;
28 u8 mode;
29};
30
31static 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
50static 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 sysfs_emit(buf, "%d\n", (s16)le16_to_cpu(ret) * 125 / 32);
61}
62
63static SENSOR_DEVICE_ATTR_RO(temp1_input, max31722_temp, 0);
64
65static struct attribute *max31722_attrs[] = {
66 &sensor_dev_attr_temp1_input.dev_attr.attr,
67 NULL,
68};
69
70ATTRIBUTE_GROUPS(max31722);
71
72static 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
104static void max31722_remove(struct spi_device *spi)
105{
106 struct max31722_data *data = spi_get_drvdata(spi);
107 int ret;
108
109 hwmon_device_unregister(data->hwmon_dev);
110
111 ret = max31722_set_mode(data, MAX31722_MODE_STANDBY);
112 if (ret)
113 /* There is nothing we can do about this ... */
114 dev_warn(&spi->dev, "Failed to put device in stand-by mode\n");
115}
116
117static int 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
125static int 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
133static DEFINE_SIMPLE_DEV_PM_OPS(max31722_pm_ops, max31722_suspend, max31722_resume);
134
135static const struct spi_device_id max31722_spi_id[] = {
136 {"max31722", 0},
137 {"max31723", 0},
138 {}
139};
140MODULE_DEVICE_TABLE(spi, max31722_spi_id);
141
142static struct spi_driver max31722_driver = {
143 .driver = {
144 .name = "max31722",
145 .pm = pm_sleep_ptr(&max31722_pm_ops),
146 },
147 .probe = max31722_probe,
148 .remove = max31722_remove,
149 .id_table = max31722_spi_id,
150};
151
152module_spi_driver(max31722_driver);
153
154MODULE_AUTHOR("Tiberiu Breana <[email protected]>");
155MODULE_DESCRIPTION("max31722 sensor driver");
156MODULE_LICENSE("GPL v2");