wm831x-hwmon.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * drivers/hwmon/wm831x-hwmon.c - Wolfson Microelectronics WM831x PMIC
  3. * hardware monitoring features.
  4. *
  5. * Copyright (C) 2009 Wolfson Microelectronics plc
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License v2 as published by the
  9. * Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/err.h>
  24. #include <linux/hwmon.h>
  25. #include <linux/hwmon-sysfs.h>
  26. #include <linux/slab.h>
  27. #include <linux/mfd/wm831x/core.h>
  28. #include <linux/mfd/wm831x/auxadc.h>
  29. static const char * const input_names[] = {
  30. [WM831X_AUX_SYSVDD] = "SYSVDD",
  31. [WM831X_AUX_USB] = "USB",
  32. [WM831X_AUX_BKUP_BATT] = "Backup battery",
  33. [WM831X_AUX_BATT] = "Battery",
  34. [WM831X_AUX_WALL] = "WALL",
  35. [WM831X_AUX_CHIP_TEMP] = "PMIC",
  36. [WM831X_AUX_BATT_TEMP] = "Battery",
  37. };
  38. static ssize_t show_voltage(struct device *dev,
  39. struct device_attribute *attr, char *buf)
  40. {
  41. struct wm831x *wm831x = dev_get_drvdata(dev);
  42. int channel = to_sensor_dev_attr(attr)->index;
  43. int ret;
  44. ret = wm831x_auxadc_read_uv(wm831x, channel);
  45. if (ret < 0)
  46. return ret;
  47. return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(ret, 1000));
  48. }
  49. static ssize_t show_chip_temp(struct device *dev,
  50. struct device_attribute *attr, char *buf)
  51. {
  52. struct wm831x *wm831x = dev_get_drvdata(dev);
  53. int channel = to_sensor_dev_attr(attr)->index;
  54. int ret;
  55. ret = wm831x_auxadc_read(wm831x, channel);
  56. if (ret < 0)
  57. return ret;
  58. /* Degrees celsius = (512.18-ret) / 1.0983 */
  59. ret = 512180 - (ret * 1000);
  60. ret = DIV_ROUND_CLOSEST(ret * 10000, 10983);
  61. return sprintf(buf, "%d\n", ret);
  62. }
  63. static ssize_t show_label(struct device *dev,
  64. struct device_attribute *attr, char *buf)
  65. {
  66. int channel = to_sensor_dev_attr(attr)->index;
  67. return sprintf(buf, "%s\n", input_names[channel]);
  68. }
  69. #define WM831X_VOLTAGE(id, name) \
  70. static SENSOR_DEVICE_ATTR(in##id##_input, S_IRUGO, show_voltage, \
  71. NULL, name)
  72. #define WM831X_NAMED_VOLTAGE(id, name) \
  73. WM831X_VOLTAGE(id, name); \
  74. static SENSOR_DEVICE_ATTR(in##id##_label, S_IRUGO, show_label, \
  75. NULL, name)
  76. WM831X_VOLTAGE(0, WM831X_AUX_AUX1);
  77. WM831X_VOLTAGE(1, WM831X_AUX_AUX2);
  78. WM831X_VOLTAGE(2, WM831X_AUX_AUX3);
  79. WM831X_VOLTAGE(3, WM831X_AUX_AUX4);
  80. WM831X_NAMED_VOLTAGE(4, WM831X_AUX_SYSVDD);
  81. WM831X_NAMED_VOLTAGE(5, WM831X_AUX_USB);
  82. WM831X_NAMED_VOLTAGE(6, WM831X_AUX_BATT);
  83. WM831X_NAMED_VOLTAGE(7, WM831X_AUX_WALL);
  84. WM831X_NAMED_VOLTAGE(8, WM831X_AUX_BKUP_BATT);
  85. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_chip_temp, NULL,
  86. WM831X_AUX_CHIP_TEMP);
  87. static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_label, NULL,
  88. WM831X_AUX_CHIP_TEMP);
  89. /*
  90. * Report as a voltage since conversion depends on external components
  91. * and that's what the ABI wants.
  92. */
  93. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_voltage, NULL,
  94. WM831X_AUX_BATT_TEMP);
  95. static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, show_label, NULL,
  96. WM831X_AUX_BATT_TEMP);
  97. static struct attribute *wm831x_attrs[] = {
  98. &sensor_dev_attr_in0_input.dev_attr.attr,
  99. &sensor_dev_attr_in1_input.dev_attr.attr,
  100. &sensor_dev_attr_in2_input.dev_attr.attr,
  101. &sensor_dev_attr_in3_input.dev_attr.attr,
  102. &sensor_dev_attr_in4_input.dev_attr.attr,
  103. &sensor_dev_attr_in4_label.dev_attr.attr,
  104. &sensor_dev_attr_in5_input.dev_attr.attr,
  105. &sensor_dev_attr_in5_label.dev_attr.attr,
  106. &sensor_dev_attr_in6_input.dev_attr.attr,
  107. &sensor_dev_attr_in6_label.dev_attr.attr,
  108. &sensor_dev_attr_in7_input.dev_attr.attr,
  109. &sensor_dev_attr_in7_label.dev_attr.attr,
  110. &sensor_dev_attr_in8_input.dev_attr.attr,
  111. &sensor_dev_attr_in8_label.dev_attr.attr,
  112. &sensor_dev_attr_temp1_input.dev_attr.attr,
  113. &sensor_dev_attr_temp1_label.dev_attr.attr,
  114. &sensor_dev_attr_temp2_input.dev_attr.attr,
  115. &sensor_dev_attr_temp2_label.dev_attr.attr,
  116. NULL
  117. };
  118. ATTRIBUTE_GROUPS(wm831x);
  119. static int wm831x_hwmon_probe(struct platform_device *pdev)
  120. {
  121. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  122. struct device *hwmon_dev;
  123. hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, "wm831x",
  124. wm831x,
  125. wm831x_groups);
  126. return PTR_ERR_OR_ZERO(hwmon_dev);
  127. }
  128. static struct platform_driver wm831x_hwmon_driver = {
  129. .probe = wm831x_hwmon_probe,
  130. .driver = {
  131. .name = "wm831x-hwmon",
  132. },
  133. };
  134. module_platform_driver(wm831x_hwmon_driver);
  135. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  136. MODULE_DESCRIPTION("WM831x Hardware Monitoring");
  137. MODULE_LICENSE("GPL");
  138. MODULE_ALIAS("platform:wm831x-hwmon");