ad7314.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * AD7314 digital temperature sensor driver for AD7314, ADT7301 and ADT7302
  3. *
  4. * Copyright 2010 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. *
  8. * Conversion to hwmon from IIO done by Jonathan Cameron <jic23@cam.ac.uk>
  9. */
  10. #include <linux/device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/sysfs.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/module.h>
  16. #include <linux/err.h>
  17. #include <linux/hwmon.h>
  18. #include <linux/hwmon-sysfs.h>
  19. #include <linux/bitops.h>
  20. /*
  21. * AD7314 temperature masks
  22. */
  23. #define AD7314_TEMP_MASK 0x7FE0
  24. #define AD7314_TEMP_SHIFT 5
  25. /*
  26. * ADT7301 and ADT7302 temperature masks
  27. */
  28. #define ADT7301_TEMP_MASK 0x3FFF
  29. enum ad7314_variant {
  30. adt7301,
  31. adt7302,
  32. ad7314,
  33. };
  34. struct ad7314_data {
  35. struct spi_device *spi_dev;
  36. struct device *hwmon_dev;
  37. u16 rx ____cacheline_aligned;
  38. };
  39. static int ad7314_spi_read(struct ad7314_data *chip)
  40. {
  41. int ret;
  42. ret = spi_read(chip->spi_dev, (u8 *)&chip->rx, sizeof(chip->rx));
  43. if (ret < 0) {
  44. dev_err(&chip->spi_dev->dev, "SPI read error\n");
  45. return ret;
  46. }
  47. return be16_to_cpu(chip->rx);
  48. }
  49. static ssize_t ad7314_show_temperature(struct device *dev,
  50. struct device_attribute *attr,
  51. char *buf)
  52. {
  53. struct ad7314_data *chip = dev_get_drvdata(dev);
  54. s16 data;
  55. int ret;
  56. ret = ad7314_spi_read(chip);
  57. if (ret < 0)
  58. return ret;
  59. switch (spi_get_device_id(chip->spi_dev)->driver_data) {
  60. case ad7314:
  61. data = (ret & AD7314_TEMP_MASK) >> AD7314_TEMP_SHIFT;
  62. data = sign_extend32(data, 9);
  63. return sprintf(buf, "%d\n", 250 * data);
  64. case adt7301:
  65. case adt7302:
  66. /*
  67. * Documented as a 13 bit twos complement register
  68. * with a sign bit - which is a 14 bit 2's complement
  69. * register. 1lsb - 31.25 milli degrees centigrade
  70. */
  71. data = ret & ADT7301_TEMP_MASK;
  72. data = sign_extend32(data, 13);
  73. return sprintf(buf, "%d\n",
  74. DIV_ROUND_CLOSEST(data * 3125, 100));
  75. default:
  76. return -EINVAL;
  77. }
  78. }
  79. static ssize_t ad7314_show_name(struct device *dev,
  80. struct device_attribute *devattr, char *buf)
  81. {
  82. return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
  83. }
  84. static DEVICE_ATTR(name, S_IRUGO, ad7314_show_name, NULL);
  85. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
  86. ad7314_show_temperature, NULL, 0);
  87. static struct attribute *ad7314_attributes[] = {
  88. &dev_attr_name.attr,
  89. &sensor_dev_attr_temp1_input.dev_attr.attr,
  90. NULL,
  91. };
  92. static const struct attribute_group ad7314_group = {
  93. .attrs = ad7314_attributes,
  94. };
  95. static int ad7314_probe(struct spi_device *spi_dev)
  96. {
  97. int ret;
  98. struct ad7314_data *chip;
  99. chip = devm_kzalloc(&spi_dev->dev, sizeof(*chip), GFP_KERNEL);
  100. if (chip == NULL)
  101. return -ENOMEM;
  102. spi_set_drvdata(spi_dev, chip);
  103. ret = sysfs_create_group(&spi_dev->dev.kobj, &ad7314_group);
  104. if (ret < 0)
  105. return ret;
  106. chip->hwmon_dev = hwmon_device_register(&spi_dev->dev);
  107. if (IS_ERR(chip->hwmon_dev)) {
  108. ret = PTR_ERR(chip->hwmon_dev);
  109. goto error_remove_group;
  110. }
  111. chip->spi_dev = spi_dev;
  112. return 0;
  113. error_remove_group:
  114. sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group);
  115. return ret;
  116. }
  117. static int ad7314_remove(struct spi_device *spi_dev)
  118. {
  119. struct ad7314_data *chip = spi_get_drvdata(spi_dev);
  120. hwmon_device_unregister(chip->hwmon_dev);
  121. sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group);
  122. return 0;
  123. }
  124. static const struct spi_device_id ad7314_id[] = {
  125. { "adt7301", adt7301 },
  126. { "adt7302", adt7302 },
  127. { "ad7314", ad7314 },
  128. { }
  129. };
  130. MODULE_DEVICE_TABLE(spi, ad7314_id);
  131. static struct spi_driver ad7314_driver = {
  132. .driver = {
  133. .name = "ad7314",
  134. },
  135. .probe = ad7314_probe,
  136. .remove = ad7314_remove,
  137. .id_table = ad7314_id,
  138. };
  139. module_spi_driver(ad7314_driver);
  140. MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
  141. MODULE_DESCRIPTION("Analog Devices AD7314, ADT7301 and ADT7302 digital temperature sensor driver");
  142. MODULE_LICENSE("GPL v2");