max31722.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include <linux/acpi.h>
  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. #define MAX31722_REG_CFG 0x00
  18. #define MAX31722_REG_TEMP_LSB 0x01
  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. struct max31722_data {
  25. struct device *hwmon_dev;
  26. struct spi_device *spi_device;
  27. u8 mode;
  28. };
  29. static int max31722_set_mode(struct max31722_data *data, u8 mode)
  30. {
  31. int ret;
  32. struct spi_device *spi = data->spi_device;
  33. u8 buf[2] = {
  34. MAX31722_REG_CFG | MAX31722_WRITE_MASK,
  35. (data->mode & MAX31722_MODE_MASK) | mode
  36. };
  37. ret = spi_write(spi, &buf, sizeof(buf));
  38. if (ret < 0) {
  39. dev_err(&spi->dev, "failed to set sensor mode.\n");
  40. return ret;
  41. }
  42. data->mode = (data->mode & MAX31722_MODE_MASK) | mode;
  43. return 0;
  44. }
  45. static ssize_t max31722_show_temp(struct device *dev,
  46. struct device_attribute *attr,
  47. char *buf)
  48. {
  49. ssize_t ret;
  50. struct max31722_data *data = dev_get_drvdata(dev);
  51. ret = spi_w8r16(data->spi_device, MAX31722_REG_TEMP_LSB);
  52. if (ret < 0)
  53. return ret;
  54. /* Keep 12 bits and multiply by the scale of 62.5 millidegrees/bit. */
  55. return sprintf(buf, "%d\n", (s16)le16_to_cpu(ret) * 125 / 32);
  56. }
  57. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
  58. max31722_show_temp, NULL, 0);
  59. static struct attribute *max31722_attrs[] = {
  60. &sensor_dev_attr_temp1_input.dev_attr.attr,
  61. NULL,
  62. };
  63. ATTRIBUTE_GROUPS(max31722);
  64. static int max31722_probe(struct spi_device *spi)
  65. {
  66. int ret;
  67. struct max31722_data *data;
  68. data = devm_kzalloc(&spi->dev, sizeof(*data), GFP_KERNEL);
  69. if (!data)
  70. return -ENOMEM;
  71. spi_set_drvdata(spi, data);
  72. data->spi_device = spi;
  73. /*
  74. * Set SD bit to 0 so we can have continuous measurements.
  75. * Set resolution to 12 bits for maximum precision.
  76. */
  77. data->mode = MAX31722_MODE_CONTINUOUS | MAX31722_RESOLUTION_12BIT;
  78. ret = max31722_set_mode(data, MAX31722_MODE_CONTINUOUS);
  79. if (ret < 0)
  80. return ret;
  81. data->hwmon_dev = hwmon_device_register_with_groups(&spi->dev,
  82. spi->modalias,
  83. data,
  84. max31722_groups);
  85. if (IS_ERR(data->hwmon_dev)) {
  86. max31722_set_mode(data, MAX31722_MODE_STANDBY);
  87. return PTR_ERR(data->hwmon_dev);
  88. }
  89. return 0;
  90. }
  91. static int max31722_remove(struct spi_device *spi)
  92. {
  93. struct max31722_data *data = spi_get_drvdata(spi);
  94. hwmon_device_unregister(data->hwmon_dev);
  95. return max31722_set_mode(data, MAX31722_MODE_STANDBY);
  96. }
  97. static int __maybe_unused max31722_suspend(struct device *dev)
  98. {
  99. struct spi_device *spi_device = to_spi_device(dev);
  100. struct max31722_data *data = spi_get_drvdata(spi_device);
  101. return max31722_set_mode(data, MAX31722_MODE_STANDBY);
  102. }
  103. static int __maybe_unused max31722_resume(struct device *dev)
  104. {
  105. struct spi_device *spi_device = to_spi_device(dev);
  106. struct max31722_data *data = spi_get_drvdata(spi_device);
  107. return max31722_set_mode(data, MAX31722_MODE_CONTINUOUS);
  108. }
  109. static SIMPLE_DEV_PM_OPS(max31722_pm_ops, max31722_suspend, max31722_resume);
  110. static const struct spi_device_id max31722_spi_id[] = {
  111. {"max31722", 0},
  112. {"max31723", 0},
  113. {}
  114. };
  115. static const struct acpi_device_id __maybe_unused max31722_acpi_id[] = {
  116. {"MAX31722", 0},
  117. {"MAX31723", 0},
  118. {}
  119. };
  120. MODULE_DEVICE_TABLE(spi, max31722_spi_id);
  121. static struct spi_driver max31722_driver = {
  122. .driver = {
  123. .name = "max31722",
  124. .pm = &max31722_pm_ops,
  125. .acpi_match_table = ACPI_PTR(max31722_acpi_id),
  126. },
  127. .probe = max31722_probe,
  128. .remove = max31722_remove,
  129. .id_table = max31722_spi_id,
  130. };
  131. module_spi_driver(max31722_driver);
  132. MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
  133. MODULE_DESCRIPTION("max31722 sensor driver");
  134. MODULE_LICENSE("GPL v2");