ad7314.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /*
  20. * AD7314 temperature masks
  21. */
  22. #define AD7314_TEMP_MASK 0x7FE0
  23. #define AD7314_TEMP_SHIFT 5
  24. /*
  25. * ADT7301 and ADT7302 temperature masks
  26. */
  27. #define ADT7301_TEMP_MASK 0x3FFF
  28. enum ad7314_variant {
  29. adt7301,
  30. adt7302,
  31. ad7314,
  32. };
  33. struct ad7314_data {
  34. struct spi_device *spi_dev;
  35. struct device *hwmon_dev;
  36. u16 rx ____cacheline_aligned;
  37. };
  38. static int ad7314_spi_read(struct ad7314_data *chip)
  39. {
  40. int ret;
  41. ret = spi_read(chip->spi_dev, (u8 *)&chip->rx, sizeof(chip->rx));
  42. if (ret < 0) {
  43. dev_err(&chip->spi_dev->dev, "SPI read error\n");
  44. return ret;
  45. }
  46. return be16_to_cpu(chip->rx);
  47. }
  48. static ssize_t ad7314_show_temperature(struct device *dev,
  49. struct device_attribute *attr,
  50. char *buf)
  51. {
  52. struct ad7314_data *chip = dev_get_drvdata(dev);
  53. s16 data;
  54. int ret;
  55. ret = ad7314_spi_read(chip);
  56. if (ret < 0)
  57. return ret;
  58. switch (spi_get_device_id(chip->spi_dev)->driver_data) {
  59. case ad7314:
  60. data = (ret & AD7314_TEMP_MASK) >> AD7314_TEMP_SHIFT;
  61. data = (data << 6) >> 6;
  62. return sprintf(buf, "%d\n", 250 * data);
  63. case adt7301:
  64. case adt7302:
  65. /*
  66. * Documented as a 13 bit twos complement register
  67. * with a sign bit - which is a 14 bit 2's complement
  68. * register. 1lsb - 31.25 milli degrees centigrade
  69. */
  70. data = ret & ADT7301_TEMP_MASK;
  71. data = (data << 2) >> 2;
  72. return sprintf(buf, "%d\n",
  73. DIV_ROUND_CLOSEST(data * 3125, 100));
  74. default:
  75. return -EINVAL;
  76. }
  77. }
  78. static ssize_t ad7314_show_name(struct device *dev,
  79. struct device_attribute *devattr, char *buf)
  80. {
  81. return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
  82. }
  83. static DEVICE_ATTR(name, S_IRUGO, ad7314_show_name, NULL);
  84. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
  85. ad7314_show_temperature, NULL, 0);
  86. static struct attribute *ad7314_attributes[] = {
  87. &dev_attr_name.attr,
  88. &sensor_dev_attr_temp1_input.dev_attr.attr,
  89. NULL,
  90. };
  91. static const struct attribute_group ad7314_group = {
  92. .attrs = ad7314_attributes,
  93. };
  94. static int ad7314_probe(struct spi_device *spi_dev)
  95. {
  96. int ret;
  97. struct ad7314_data *chip;
  98. chip = devm_kzalloc(&spi_dev->dev, sizeof(*chip), GFP_KERNEL);
  99. if (chip == NULL)
  100. return -ENOMEM;
  101. dev_set_drvdata(&spi_dev->dev, chip);
  102. ret = sysfs_create_group(&spi_dev->dev.kobj, &ad7314_group);
  103. if (ret < 0)
  104. return ret;
  105. chip->hwmon_dev = hwmon_device_register(&spi_dev->dev);
  106. if (IS_ERR(chip->hwmon_dev)) {
  107. ret = PTR_ERR(chip->hwmon_dev);
  108. goto error_remove_group;
  109. }
  110. chip->spi_dev = spi_dev;
  111. return 0;
  112. error_remove_group:
  113. sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group);
  114. return ret;
  115. }
  116. static int ad7314_remove(struct spi_device *spi_dev)
  117. {
  118. struct ad7314_data *chip = dev_get_drvdata(&spi_dev->dev);
  119. hwmon_device_unregister(chip->hwmon_dev);
  120. sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group);
  121. return 0;
  122. }
  123. static const struct spi_device_id ad7314_id[] = {
  124. { "adt7301", adt7301 },
  125. { "adt7302", adt7302 },
  126. { "ad7314", ad7314 },
  127. { }
  128. };
  129. MODULE_DEVICE_TABLE(spi, ad7314_id);
  130. static struct spi_driver ad7314_driver = {
  131. .driver = {
  132. .name = "ad7314",
  133. .owner = THIS_MODULE,
  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"
  142. " temperature sensor driver");
  143. MODULE_LICENSE("GPL v2");