Thomas Gleixner | 21eb0be | 2019-05-29 07:12:45 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mark Brown | fb6c023 | 2009-07-20 12:43:45 +0100 | [diff] [blame] | 2 | /* |
| 3 | * drivers/hwmon/wm8350-hwmon.c - Wolfson Microelectronics WM8350 PMIC |
| 4 | * hardware monitoring features. |
| 5 | * |
| 6 | * Copyright (C) 2009 Wolfson Microelectronics plc |
Mark Brown | fb6c023 | 2009-07-20 12:43:45 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/hwmon.h> |
| 14 | #include <linux/hwmon-sysfs.h> |
| 15 | |
| 16 | #include <linux/mfd/wm8350/core.h> |
| 17 | #include <linux/mfd/wm8350/comparator.h> |
| 18 | |
Guenter Roeck | a68abd3 | 2012-01-20 09:43:54 -0800 | [diff] [blame] | 19 | static const char * const input_names[] = { |
Mark Brown | fb6c023 | 2009-07-20 12:43:45 +0100 | [diff] [blame] | 20 | [WM8350_AUXADC_USB] = "USB", |
| 21 | [WM8350_AUXADC_LINE] = "Line", |
| 22 | [WM8350_AUXADC_BATT] = "Battery", |
| 23 | }; |
| 24 | |
Mark Brown | fb6c023 | 2009-07-20 12:43:45 +0100 | [diff] [blame] | 25 | static ssize_t show_voltage(struct device *dev, |
| 26 | struct device_attribute *attr, char *buf) |
| 27 | { |
| 28 | struct wm8350 *wm8350 = dev_get_drvdata(dev); |
| 29 | int channel = to_sensor_dev_attr(attr)->index; |
| 30 | int val; |
| 31 | |
| 32 | val = wm8350_read_auxadc(wm8350, channel, 0, 0) * WM8350_AUX_COEFF; |
| 33 | val = DIV_ROUND_CLOSEST(val, 1000); |
| 34 | |
| 35 | return sprintf(buf, "%d\n", val); |
| 36 | } |
| 37 | |
| 38 | static ssize_t show_label(struct device *dev, |
| 39 | struct device_attribute *attr, char *buf) |
| 40 | { |
| 41 | int channel = to_sensor_dev_attr(attr)->index; |
| 42 | |
| 43 | return sprintf(buf, "%s\n", input_names[channel]); |
| 44 | } |
| 45 | |
| 46 | #define WM8350_NAMED_VOLTAGE(id, name) \ |
| 47 | static SENSOR_DEVICE_ATTR(in##id##_input, S_IRUGO, show_voltage,\ |
| 48 | NULL, name); \ |
| 49 | static SENSOR_DEVICE_ATTR(in##id##_label, S_IRUGO, show_label, \ |
| 50 | NULL, name) |
| 51 | |
Mark Brown | fb6c023 | 2009-07-20 12:43:45 +0100 | [diff] [blame] | 52 | WM8350_NAMED_VOLTAGE(0, WM8350_AUXADC_USB); |
| 53 | WM8350_NAMED_VOLTAGE(1, WM8350_AUXADC_BATT); |
| 54 | WM8350_NAMED_VOLTAGE(2, WM8350_AUXADC_LINE); |
| 55 | |
Axel Lin | b3aabd6 | 2014-06-17 23:12:08 +0800 | [diff] [blame] | 56 | static struct attribute *wm8350_attrs[] = { |
Mark Brown | fb6c023 | 2009-07-20 12:43:45 +0100 | [diff] [blame] | 57 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 58 | &sensor_dev_attr_in0_label.dev_attr.attr, |
| 59 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 60 | &sensor_dev_attr_in1_label.dev_attr.attr, |
| 61 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 62 | &sensor_dev_attr_in2_label.dev_attr.attr, |
| 63 | |
| 64 | NULL, |
| 65 | }; |
| 66 | |
Axel Lin | b3aabd6 | 2014-06-17 23:12:08 +0800 | [diff] [blame] | 67 | ATTRIBUTE_GROUPS(wm8350); |
Mark Brown | fb6c023 | 2009-07-20 12:43:45 +0100 | [diff] [blame] | 68 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 69 | static int wm8350_hwmon_probe(struct platform_device *pdev) |
Mark Brown | fb6c023 | 2009-07-20 12:43:45 +0100 | [diff] [blame] | 70 | { |
| 71 | struct wm8350 *wm8350 = platform_get_drvdata(pdev); |
Axel Lin | b3aabd6 | 2014-06-17 23:12:08 +0800 | [diff] [blame] | 72 | struct device *hwmon_dev; |
Mark Brown | fb6c023 | 2009-07-20 12:43:45 +0100 | [diff] [blame] | 73 | |
Axel Lin | b3aabd6 | 2014-06-17 23:12:08 +0800 | [diff] [blame] | 74 | hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, "wm8350", |
| 75 | wm8350, |
| 76 | wm8350_groups); |
| 77 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Mark Brown | fb6c023 | 2009-07-20 12:43:45 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static struct platform_driver wm8350_hwmon_driver = { |
| 81 | .probe = wm8350_hwmon_probe, |
Mark Brown | fb6c023 | 2009-07-20 12:43:45 +0100 | [diff] [blame] | 82 | .driver = { |
| 83 | .name = "wm8350-hwmon", |
Mark Brown | fb6c023 | 2009-07-20 12:43:45 +0100 | [diff] [blame] | 84 | }, |
| 85 | }; |
| 86 | |
Axel Lin | 25a236a | 2011-11-25 02:31:00 -0500 | [diff] [blame] | 87 | module_platform_driver(wm8350_hwmon_driver); |
Mark Brown | fb6c023 | 2009-07-20 12:43:45 +0100 | [diff] [blame] | 88 | |
| 89 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 90 | MODULE_DESCRIPTION("WM8350 Hardware Monitoring"); |
| 91 | MODULE_LICENSE("GPL"); |
| 92 | MODULE_ALIAS("platform:wm8350-hwmon"); |