ad7314.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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, s16 *data)
  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. *data = be16_to_cpu(chip->rx);
  53. return ret;
  54. }
  55. static ssize_t ad7314_show_temperature(struct device *dev,
  56. struct device_attribute *attr,
  57. char *buf)
  58. {
  59. struct ad7314_data *chip = dev_get_drvdata(dev);
  60. s16 data;
  61. int ret;
  62. ret = ad7314_spi_read(chip, &data);
  63. if (ret < 0)
  64. return ret;
  65. switch (spi_get_device_id(chip->spi_dev)->driver_data) {
  66. case ad7314:
  67. data = (data & AD7314_TEMP_MASK) >> AD7314_TEMP_OFFSET;
  68. data = (data << 6) >> 6;
  69. return sprintf(buf, "%d\n", 250 * data);
  70. case adt7301:
  71. case adt7302:
  72. /*
  73. * Documented as a 13 bit twos complement register
  74. * with a sign bit - which is a 14 bit 2's complement
  75. * register. 1lsb - 31.25 milli degrees centigrade
  76. */
  77. data &= ADT7301_TEMP_MASK;
  78. data = (data << 2) >> 2;
  79. return sprintf(buf, "%d\n",
  80. DIV_ROUND_CLOSEST(data * 3125, 100));
  81. default:
  82. return -EINVAL;
  83. }
  84. }
  85. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
  86. ad7314_show_temperature, NULL, 0);
  87. static struct attribute *ad7314_attributes[] = {
  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 __devinit ad7314_probe(struct spi_device *spi_dev)
  95. {
  96. int ret;
  97. struct ad7314_data *chip;
  98. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  99. if (chip == NULL) {
  100. ret = -ENOMEM;
  101. goto error_ret;
  102. }
  103. dev_set_drvdata(&spi_dev->dev, chip);
  104. ret = sysfs_create_group(&spi_dev->dev.kobj, &ad7314_group);
  105. if (ret < 0)
  106. goto error_free_chip;
  107. chip->hwmon_dev = hwmon_device_register(&spi_dev->dev);
  108. if (IS_ERR(chip->hwmon_dev)) {
  109. ret = PTR_ERR(chip->hwmon_dev);
  110. goto error_remove_group;
  111. }
  112. return 0;
  113. error_remove_group:
  114. sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group);
  115. error_free_chip:
  116. kfree(chip);
  117. error_ret:
  118. return ret;
  119. }
  120. static int __devexit ad7314_remove(struct spi_device *spi_dev)
  121. {
  122. struct ad7314_data *chip = dev_get_drvdata(&spi_dev->dev);
  123. hwmon_device_unregister(chip->hwmon_dev);
  124. sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group);
  125. kfree(chip);
  126. return 0;
  127. }
  128. static const struct spi_device_id ad7314_id[] = {
  129. { "adt7301", adt7301 },
  130. { "adt7302", adt7302 },
  131. { "ad7314", ad7314 },
  132. { }
  133. };
  134. MODULE_DEVICE_TABLE(spi, ad7314_id);
  135. static struct spi_driver ad7314_driver = {
  136. .driver = {
  137. .name = "ad7314",
  138. .bus = &spi_bus_type,
  139. .owner = THIS_MODULE,
  140. },
  141. .probe = ad7314_probe,
  142. .remove = __devexit_p(ad7314_remove),
  143. .id_table = ad7314_id,
  144. };
  145. static __init int ad7314_init(void)
  146. {
  147. return spi_register_driver(&ad7314_driver);
  148. }
  149. module_init(ad7314_init);
  150. static __exit void ad7314_exit(void)
  151. {
  152. spi_unregister_driver(&ad7314_driver);
  153. }
  154. module_exit(ad7314_exit);
  155. MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
  156. MODULE_DESCRIPTION("Analog Devices AD7314, ADT7301 and ADT7302 digital"
  157. " temperature sensor driver");
  158. MODULE_LICENSE("GPL v2");