wm8350-hwmon.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/hwmon/wm8350-hwmon.c - Wolfson Microelectronics WM8350 PMIC
  4. * hardware monitoring features.
  5. *
  6. * Copyright (C) 2009 Wolfson Microelectronics plc
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/err.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/hwmon.h>
  13. #include <linux/hwmon-sysfs.h>
  14. #include <linux/mfd/wm8350/core.h>
  15. #include <linux/mfd/wm8350/comparator.h>
  16. static const char * const input_names[] = {
  17. [WM8350_AUXADC_USB] = "USB",
  18. [WM8350_AUXADC_LINE] = "Line",
  19. [WM8350_AUXADC_BATT] = "Battery",
  20. };
  21. static ssize_t show_voltage(struct device *dev,
  22. struct device_attribute *attr, char *buf)
  23. {
  24. struct wm8350 *wm8350 = dev_get_drvdata(dev);
  25. int channel = to_sensor_dev_attr(attr)->index;
  26. int val;
  27. val = wm8350_read_auxadc(wm8350, channel, 0, 0) * WM8350_AUX_COEFF;
  28. val = DIV_ROUND_CLOSEST(val, 1000);
  29. return sprintf(buf, "%d\n", val);
  30. }
  31. static ssize_t show_label(struct device *dev,
  32. struct device_attribute *attr, char *buf)
  33. {
  34. int channel = to_sensor_dev_attr(attr)->index;
  35. return sprintf(buf, "%s\n", input_names[channel]);
  36. }
  37. #define WM8350_NAMED_VOLTAGE(id, name) \
  38. static SENSOR_DEVICE_ATTR(in##id##_input, S_IRUGO, show_voltage,\
  39. NULL, name); \
  40. static SENSOR_DEVICE_ATTR(in##id##_label, S_IRUGO, show_label, \
  41. NULL, name)
  42. WM8350_NAMED_VOLTAGE(0, WM8350_AUXADC_USB);
  43. WM8350_NAMED_VOLTAGE(1, WM8350_AUXADC_BATT);
  44. WM8350_NAMED_VOLTAGE(2, WM8350_AUXADC_LINE);
  45. static struct attribute *wm8350_attrs[] = {
  46. &sensor_dev_attr_in0_input.dev_attr.attr,
  47. &sensor_dev_attr_in0_label.dev_attr.attr,
  48. &sensor_dev_attr_in1_input.dev_attr.attr,
  49. &sensor_dev_attr_in1_label.dev_attr.attr,
  50. &sensor_dev_attr_in2_input.dev_attr.attr,
  51. &sensor_dev_attr_in2_label.dev_attr.attr,
  52. NULL,
  53. };
  54. ATTRIBUTE_GROUPS(wm8350);
  55. static int wm8350_hwmon_probe(struct platform_device *pdev)
  56. {
  57. struct wm8350 *wm8350 = platform_get_drvdata(pdev);
  58. struct device *hwmon_dev;
  59. hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, "wm8350",
  60. wm8350,
  61. wm8350_groups);
  62. return PTR_ERR_OR_ZERO(hwmon_dev);
  63. }
  64. static struct platform_driver wm8350_hwmon_driver = {
  65. .probe = wm8350_hwmon_probe,
  66. .driver = {
  67. .name = "wm8350-hwmon",
  68. },
  69. };
  70. module_platform_driver(wm8350_hwmon_driver);
  71. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  72. MODULE_DESCRIPTION("WM8350 Hardware Monitoring");
  73. MODULE_LICENSE("GPL");
  74. MODULE_ALIAS("platform:wm8350-hwmon");