wm8350-hwmon.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 ssize_t show_name(struct device *dev,
  29. struct device_attribute *attr, char *buf)
  30. {
  31. return sprintf(buf, "wm8350\n");
  32. }
  33. static const char *input_names[] = {
  34. [WM8350_AUXADC_USB] = "USB",
  35. [WM8350_AUXADC_LINE] = "Line",
  36. [WM8350_AUXADC_BATT] = "Battery",
  37. };
  38. static ssize_t show_voltage(struct device *dev,
  39. struct device_attribute *attr, char *buf)
  40. {
  41. struct wm8350 *wm8350 = dev_get_drvdata(dev);
  42. int channel = to_sensor_dev_attr(attr)->index;
  43. int val;
  44. val = wm8350_read_auxadc(wm8350, channel, 0, 0) * WM8350_AUX_COEFF;
  45. val = DIV_ROUND_CLOSEST(val, 1000);
  46. return sprintf(buf, "%d\n", val);
  47. }
  48. static ssize_t show_label(struct device *dev,
  49. struct device_attribute *attr, char *buf)
  50. {
  51. int channel = to_sensor_dev_attr(attr)->index;
  52. return sprintf(buf, "%s\n", input_names[channel]);
  53. }
  54. #define WM8350_NAMED_VOLTAGE(id, name) \
  55. static SENSOR_DEVICE_ATTR(in##id##_input, S_IRUGO, show_voltage,\
  56. NULL, name); \
  57. static SENSOR_DEVICE_ATTR(in##id##_label, S_IRUGO, show_label, \
  58. NULL, name)
  59. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  60. WM8350_NAMED_VOLTAGE(0, WM8350_AUXADC_USB);
  61. WM8350_NAMED_VOLTAGE(1, WM8350_AUXADC_BATT);
  62. WM8350_NAMED_VOLTAGE(2, WM8350_AUXADC_LINE);
  63. static struct attribute *wm8350_attributes[] = {
  64. &dev_attr_name.attr,
  65. &sensor_dev_attr_in0_input.dev_attr.attr,
  66. &sensor_dev_attr_in0_label.dev_attr.attr,
  67. &sensor_dev_attr_in1_input.dev_attr.attr,
  68. &sensor_dev_attr_in1_label.dev_attr.attr,
  69. &sensor_dev_attr_in2_input.dev_attr.attr,
  70. &sensor_dev_attr_in2_label.dev_attr.attr,
  71. NULL,
  72. };
  73. static const struct attribute_group wm8350_attr_group = {
  74. .attrs = wm8350_attributes,
  75. };
  76. static int __devinit wm8350_hwmon_probe(struct platform_device *pdev)
  77. {
  78. struct wm8350 *wm8350 = platform_get_drvdata(pdev);
  79. int ret;
  80. ret = sysfs_create_group(&pdev->dev.kobj, &wm8350_attr_group);
  81. if (ret)
  82. goto err;
  83. wm8350->hwmon.classdev = hwmon_device_register(&pdev->dev);
  84. if (IS_ERR(wm8350->hwmon.classdev)) {
  85. ret = PTR_ERR(wm8350->hwmon.classdev);
  86. goto err_group;
  87. }
  88. return 0;
  89. err_group:
  90. sysfs_remove_group(&pdev->dev.kobj, &wm8350_attr_group);
  91. err:
  92. return ret;
  93. }
  94. static int __devexit wm8350_hwmon_remove(struct platform_device *pdev)
  95. {
  96. struct wm8350 *wm8350 = platform_get_drvdata(pdev);
  97. hwmon_device_unregister(wm8350->hwmon.classdev);
  98. sysfs_remove_group(&pdev->dev.kobj, &wm8350_attr_group);
  99. return 0;
  100. }
  101. static struct platform_driver wm8350_hwmon_driver = {
  102. .probe = wm8350_hwmon_probe,
  103. .remove = __devexit_p(wm8350_hwmon_remove),
  104. .driver = {
  105. .name = "wm8350-hwmon",
  106. .owner = THIS_MODULE,
  107. },
  108. };
  109. static int __init wm8350_hwmon_init(void)
  110. {
  111. return platform_driver_register(&wm8350_hwmon_driver);
  112. }
  113. module_init(wm8350_hwmon_init);
  114. static void __exit wm8350_hwmon_exit(void)
  115. {
  116. platform_driver_unregister(&wm8350_hwmon_driver);
  117. }
  118. module_exit(wm8350_hwmon_exit);
  119. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  120. MODULE_DESCRIPTION("WM8350 Hardware Monitoring");
  121. MODULE_LICENSE("GPL");
  122. MODULE_ALIAS("platform:wm8350-hwmon");