ad7314.c 4.0 KB

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