max31722.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #include <linux/hwmon.h>
  9. #include <linux/hwmon-sysfs.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/spi/spi.h>
  13. #define MAX31722_REG_CFG 0x00
  14. #define MAX31722_REG_TEMP_LSB 0x01
  15. #define MAX31722_MODE_CONTINUOUS 0x00
  16. #define MAX31722_MODE_STANDBY 0x01
  17. #define MAX31722_MODE_MASK 0xFE
  18. #define MAX31722_RESOLUTION_12BIT 0x06
  19. #define MAX31722_WRITE_MASK 0x80
  20. struct max31722_data {
  21. struct device *hwmon_dev;
  22. struct spi_device *spi_device;
  23. u8 mode;
  24. };
  25. static int max31722_set_mode(struct max31722_data *data, u8 mode)
  26. {
  27. int ret;
  28. struct spi_device *spi = data->spi_device;
  29. u8 buf[2] = {
  30. MAX31722_REG_CFG | MAX31722_WRITE_MASK,
  31. (data->mode & MAX31722_MODE_MASK) | mode
  32. };
  33. ret = spi_write(spi, &buf, sizeof(buf));
  34. if (ret < 0) {
  35. dev_err(&spi->dev, "failed to set sensor mode.\n");
  36. return ret;
  37. }
  38. data->mode = (data->mode & MAX31722_MODE_MASK) | mode;
  39. return 0;
  40. }
  41. static ssize_t max31722_temp_show(struct device *dev,
  42. struct device_attribute *attr, char *buf)
  43. {
  44. ssize_t ret;
  45. struct max31722_data *data = dev_get_drvdata(dev);
  46. ret = spi_w8r16(data->spi_device, MAX31722_REG_TEMP_LSB);
  47. if (ret < 0)
  48. return ret;
  49. /* Keep 12 bits and multiply by the scale of 62.5 millidegrees/bit. */
  50. return sprintf(buf, "%d\n", (s16)le16_to_cpu(ret) * 125 / 32);
  51. }
  52. static SENSOR_DEVICE_ATTR_RO(temp1_input, max31722_temp, 0);
  53. static struct attribute *max31722_attrs[] = {
  54. &sensor_dev_attr_temp1_input.dev_attr.attr,
  55. NULL,
  56. };
  57. ATTRIBUTE_GROUPS(max31722);
  58. static int max31722_probe(struct spi_device *spi)
  59. {
  60. int ret;
  61. struct max31722_data *data;
  62. data = devm_kzalloc(&spi->dev, sizeof(*data), GFP_KERNEL);
  63. if (!data)
  64. return -ENOMEM;
  65. spi_set_drvdata(spi, data);
  66. data->spi_device = spi;
  67. /*
  68. * Set SD bit to 0 so we can have continuous measurements.
  69. * Set resolution to 12 bits for maximum precision.
  70. */
  71. data->mode = MAX31722_MODE_CONTINUOUS | MAX31722_RESOLUTION_12BIT;
  72. ret = max31722_set_mode(data, MAX31722_MODE_CONTINUOUS);
  73. if (ret < 0)
  74. return ret;
  75. data->hwmon_dev = hwmon_device_register_with_groups(&spi->dev,
  76. spi->modalias,
  77. data,
  78. max31722_groups);
  79. if (IS_ERR(data->hwmon_dev)) {
  80. max31722_set_mode(data, MAX31722_MODE_STANDBY);
  81. return PTR_ERR(data->hwmon_dev);
  82. }
  83. return 0;
  84. }
  85. static void max31722_remove(struct spi_device *spi)
  86. {
  87. struct max31722_data *data = spi_get_drvdata(spi);
  88. int ret;
  89. hwmon_device_unregister(data->hwmon_dev);
  90. ret = max31722_set_mode(data, MAX31722_MODE_STANDBY);
  91. if (ret)
  92. /* There is nothing we can do about this ... */
  93. dev_warn(&spi->dev, "Failed to put device in stand-by mode\n");
  94. }
  95. static int max31722_suspend(struct device *dev)
  96. {
  97. struct spi_device *spi_device = to_spi_device(dev);
  98. struct max31722_data *data = spi_get_drvdata(spi_device);
  99. return max31722_set_mode(data, MAX31722_MODE_STANDBY);
  100. }
  101. static int max31722_resume(struct device *dev)
  102. {
  103. struct spi_device *spi_device = to_spi_device(dev);
  104. struct max31722_data *data = spi_get_drvdata(spi_device);
  105. return max31722_set_mode(data, MAX31722_MODE_CONTINUOUS);
  106. }
  107. static DEFINE_SIMPLE_DEV_PM_OPS(max31722_pm_ops, max31722_suspend, max31722_resume);
  108. static const struct spi_device_id max31722_spi_id[] = {
  109. {"max31722", 0},
  110. {"max31723", 0},
  111. {}
  112. };
  113. MODULE_DEVICE_TABLE(spi, max31722_spi_id);
  114. static struct spi_driver max31722_driver = {
  115. .driver = {
  116. .name = "max31722",
  117. .pm = pm_sleep_ptr(&max31722_pm_ops),
  118. },
  119. .probe = max31722_probe,
  120. .remove = max31722_remove,
  121. .id_table = max31722_spi_id,
  122. };
  123. module_spi_driver(max31722_driver);
  124. MODULE_AUTHOR("Tiberiu Breana <[email protected]>");
  125. MODULE_DESCRIPTION("max31722 sensor driver");
  126. MODULE_LICENSE("GPL v2");