Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * max31722 - hwmon driver for Maxim Integrated MAX31722/MAX31723 SPI
0004  * digital thermometer and thermostats.
0005  *
0006  * Copyright (c) 2016, Intel Corporation.
0007  */
0008 
0009 #include <linux/hwmon.h>
0010 #include <linux/hwmon-sysfs.h>
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/spi/spi.h>
0014 
0015 #define MAX31722_REG_CFG                0x00
0016 #define MAX31722_REG_TEMP_LSB               0x01
0017 
0018 #define MAX31722_MODE_CONTINUOUS            0x00
0019 #define MAX31722_MODE_STANDBY               0x01
0020 #define MAX31722_MODE_MASK              0xFE
0021 #define MAX31722_RESOLUTION_12BIT           0x06
0022 #define MAX31722_WRITE_MASK             0x80
0023 
0024 struct max31722_data {
0025     struct device *hwmon_dev;
0026     struct spi_device *spi_device;
0027     u8 mode;
0028 };
0029 
0030 static int max31722_set_mode(struct max31722_data *data, u8 mode)
0031 {
0032     int ret;
0033     struct spi_device *spi = data->spi_device;
0034     u8 buf[2] = {
0035         MAX31722_REG_CFG | MAX31722_WRITE_MASK,
0036         (data->mode & MAX31722_MODE_MASK) | mode
0037     };
0038 
0039     ret = spi_write(spi, &buf, sizeof(buf));
0040     if (ret < 0) {
0041         dev_err(&spi->dev, "failed to set sensor mode.\n");
0042         return ret;
0043     }
0044     data->mode = (data->mode & MAX31722_MODE_MASK) | mode;
0045 
0046     return 0;
0047 }
0048 
0049 static ssize_t max31722_temp_show(struct device *dev,
0050                   struct device_attribute *attr, char *buf)
0051 {
0052     ssize_t ret;
0053     struct max31722_data *data = dev_get_drvdata(dev);
0054 
0055     ret = spi_w8r16(data->spi_device, MAX31722_REG_TEMP_LSB);
0056     if (ret < 0)
0057         return ret;
0058     /* Keep 12 bits and multiply by the scale of 62.5 millidegrees/bit. */
0059     return sprintf(buf, "%d\n", (s16)le16_to_cpu(ret) * 125 / 32);
0060 }
0061 
0062 static SENSOR_DEVICE_ATTR_RO(temp1_input, max31722_temp, 0);
0063 
0064 static struct attribute *max31722_attrs[] = {
0065     &sensor_dev_attr_temp1_input.dev_attr.attr,
0066     NULL,
0067 };
0068 
0069 ATTRIBUTE_GROUPS(max31722);
0070 
0071 static int max31722_probe(struct spi_device *spi)
0072 {
0073     int ret;
0074     struct max31722_data *data;
0075 
0076     data = devm_kzalloc(&spi->dev, sizeof(*data), GFP_KERNEL);
0077     if (!data)
0078         return -ENOMEM;
0079 
0080     spi_set_drvdata(spi, data);
0081     data->spi_device = spi;
0082     /*
0083      * Set SD bit to 0 so we can have continuous measurements.
0084      * Set resolution to 12 bits for maximum precision.
0085      */
0086     data->mode = MAX31722_MODE_CONTINUOUS | MAX31722_RESOLUTION_12BIT;
0087     ret = max31722_set_mode(data, MAX31722_MODE_CONTINUOUS);
0088     if (ret < 0)
0089         return ret;
0090 
0091     data->hwmon_dev = hwmon_device_register_with_groups(&spi->dev,
0092                                 spi->modalias,
0093                                 data,
0094                                 max31722_groups);
0095     if (IS_ERR(data->hwmon_dev)) {
0096         max31722_set_mode(data, MAX31722_MODE_STANDBY);
0097         return PTR_ERR(data->hwmon_dev);
0098     }
0099 
0100     return 0;
0101 }
0102 
0103 static void max31722_remove(struct spi_device *spi)
0104 {
0105     struct max31722_data *data = spi_get_drvdata(spi);
0106     int ret;
0107 
0108     hwmon_device_unregister(data->hwmon_dev);
0109 
0110     ret = max31722_set_mode(data, MAX31722_MODE_STANDBY);
0111     if (ret)
0112         /* There is nothing we can do about this ... */
0113         dev_warn(&spi->dev, "Failed to put device in stand-by mode\n");
0114 }
0115 
0116 static int __maybe_unused max31722_suspend(struct device *dev)
0117 {
0118     struct spi_device *spi_device = to_spi_device(dev);
0119     struct max31722_data *data = spi_get_drvdata(spi_device);
0120 
0121     return max31722_set_mode(data, MAX31722_MODE_STANDBY);
0122 }
0123 
0124 static int __maybe_unused max31722_resume(struct device *dev)
0125 {
0126     struct spi_device *spi_device = to_spi_device(dev);
0127     struct max31722_data *data = spi_get_drvdata(spi_device);
0128 
0129     return max31722_set_mode(data, MAX31722_MODE_CONTINUOUS);
0130 }
0131 
0132 static SIMPLE_DEV_PM_OPS(max31722_pm_ops, max31722_suspend, max31722_resume);
0133 
0134 static const struct spi_device_id max31722_spi_id[] = {
0135     {"max31722", 0},
0136     {"max31723", 0},
0137     {}
0138 };
0139 MODULE_DEVICE_TABLE(spi, max31722_spi_id);
0140 
0141 static struct spi_driver max31722_driver = {
0142     .driver = {
0143         .name = "max31722",
0144         .pm = &max31722_pm_ops,
0145     },
0146     .probe =            max31722_probe,
0147     .remove =           max31722_remove,
0148     .id_table =         max31722_spi_id,
0149 };
0150 
0151 module_spi_driver(max31722_driver);
0152 
0153 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
0154 MODULE_DESCRIPTION("max31722 sensor driver");
0155 MODULE_LICENSE("GPL v2");