wm8350-hwmon.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * drivers/hwmon/wm8350-hwmon.c - Wolfson Microelectronics WM8350 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/err.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/hwmon.h>
  25. #include <linux/hwmon-sysfs.h>
  26. #include <linux/mfd/wm8350/core.h>
  27. #include <linux/mfd/wm8350/comparator.h>
  28. static const char * const input_names[] = {
  29. [WM8350_AUXADC_USB] = "USB",
  30. [WM8350_AUXADC_LINE] = "Line",
  31. [WM8350_AUXADC_BATT] = "Battery",
  32. };
  33. static ssize_t show_voltage(struct device *dev,
  34. struct device_attribute *attr, char *buf)
  35. {
  36. struct wm8350 *wm8350 = dev_get_drvdata(dev);
  37. int channel = to_sensor_dev_attr(attr)->index;
  38. int val;
  39. val = wm8350_read_auxadc(wm8350, channel, 0, 0) * WM8350_AUX_COEFF;
  40. val = DIV_ROUND_CLOSEST(val, 1000);
  41. return sprintf(buf, "%d\n", val);
  42. }
  43. static ssize_t show_label(struct device *dev,
  44. struct device_attribute *attr, char *buf)
  45. {
  46. int channel = to_sensor_dev_attr(attr)->index;
  47. return sprintf(buf, "%s\n", input_names[channel]);
  48. }
  49. #define WM8350_NAMED_VOLTAGE(id, name) \
  50. static SENSOR_DEVICE_ATTR(in##id##_input, S_IRUGO, show_voltage,\
  51. NULL, name); \
  52. static SENSOR_DEVICE_ATTR(in##id##_label, S_IRUGO, show_label, \
  53. NULL, name)
  54. WM8350_NAMED_VOLTAGE(0, WM8350_AUXADC_USB);
  55. WM8350_NAMED_VOLTAGE(1, WM8350_AUXADC_BATT);
  56. WM8350_NAMED_VOLTAGE(2, WM8350_AUXADC_LINE);
  57. static struct attribute *wm8350_attrs[] = {
  58. &sensor_dev_attr_in0_input.dev_attr.attr,
  59. &sensor_dev_attr_in0_label.dev_attr.attr,
  60. &sensor_dev_attr_in1_input.dev_attr.attr,
  61. &sensor_dev_attr_in1_label.dev_attr.attr,
  62. &sensor_dev_attr_in2_input.dev_attr.attr,
  63. &sensor_dev_attr_in2_label.dev_attr.attr,
  64. NULL,
  65. };
  66. ATTRIBUTE_GROUPS(wm8350);
  67. static int wm8350_hwmon_probe(struct platform_device *pdev)
  68. {
  69. struct wm8350 *wm8350 = platform_get_drvdata(pdev);
  70. struct device *hwmon_dev;
  71. hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, "wm8350",
  72. wm8350,
  73. wm8350_groups);
  74. return PTR_ERR_OR_ZERO(hwmon_dev);
  75. }
  76. static struct platform_driver wm8350_hwmon_driver = {
  77. .probe = wm8350_hwmon_probe,
  78. .driver = {
  79. .name = "wm8350-hwmon",
  80. },
  81. };
  82. module_platform_driver(wm8350_hwmon_driver);
  83. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  84. MODULE_DESCRIPTION("WM8350 Hardware Monitoring");
  85. MODULE_LICENSE("GPL");
  86. MODULE_ALIAS("platform:wm8350-hwmon");