2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * Copyright (C) 2012 ARM Limited
14 #define DRVNAME "vexpress-hwmon"
15 #define pr_fmt(fmt) DRVNAME ": " fmt
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/hwmon.h>
20 #include <linux/hwmon-sysfs.h>
21 #include <linux/module.h>
23 #include <linux/of_device.h>
24 #include <linux/platform_device.h>
25 #include <linux/vexpress.h>
27 struct vexpress_hwmon_data {
28 struct device *hwmon_dev;
29 struct vexpress_config_func *func;
32 static ssize_t vexpress_hwmon_name_show(struct device *dev,
33 struct device_attribute *dev_attr, char *buffer)
35 const char *compatible = of_get_property(dev->of_node, "compatible",
38 return sprintf(buffer, "%s\n", compatible);
41 static ssize_t vexpress_hwmon_label_show(struct device *dev,
42 struct device_attribute *dev_attr, char *buffer)
44 const char *label = of_get_property(dev->of_node, "label", NULL);
49 return snprintf(buffer, PAGE_SIZE, "%s\n", label);
52 static ssize_t vexpress_hwmon_u32_show(struct device *dev,
53 struct device_attribute *dev_attr, char *buffer)
55 struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
59 err = vexpress_config_read(data->func, 0, &value);
63 return snprintf(buffer, PAGE_SIZE, "%u\n", value /
64 to_sensor_dev_attr(dev_attr)->index);
67 static ssize_t vexpress_hwmon_u64_show(struct device *dev,
68 struct device_attribute *dev_attr, char *buffer)
70 struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
72 u32 value_hi, value_lo;
74 err = vexpress_config_read(data->func, 0, &value_lo);
78 err = vexpress_config_read(data->func, 1, &value_hi);
82 return snprintf(buffer, PAGE_SIZE, "%llu\n",
83 div_u64(((u64)value_hi << 32) | value_lo,
84 to_sensor_dev_attr(dev_attr)->index));
87 static DEVICE_ATTR(name, S_IRUGO, vexpress_hwmon_name_show, NULL);
89 #define VEXPRESS_HWMON_ATTRS(_name, _label_attr, _input_attr) \
90 struct attribute *vexpress_hwmon_attrs_##_name[] = { \
91 &dev_attr_name.attr, \
92 &dev_attr_##_label_attr.attr, \
93 &sensor_dev_attr_##_input_attr.dev_attr.attr, \
97 #if !defined(CONFIG_REGULATOR_VEXPRESS)
98 static DEVICE_ATTR(in1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
99 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, vexpress_hwmon_u32_show,
101 static VEXPRESS_HWMON_ATTRS(volt, in1_label, in1_input);
102 static struct attribute_group vexpress_hwmon_group_volt = {
103 .attrs = vexpress_hwmon_attrs_volt,
107 static DEVICE_ATTR(curr1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
108 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, vexpress_hwmon_u32_show,
110 static VEXPRESS_HWMON_ATTRS(amp, curr1_label, curr1_input);
111 static struct attribute_group vexpress_hwmon_group_amp = {
112 .attrs = vexpress_hwmon_attrs_amp,
115 static DEVICE_ATTR(temp1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
116 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, vexpress_hwmon_u32_show,
118 static VEXPRESS_HWMON_ATTRS(temp, temp1_label, temp1_input);
119 static struct attribute_group vexpress_hwmon_group_temp = {
120 .attrs = vexpress_hwmon_attrs_temp,
123 static DEVICE_ATTR(power1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
124 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, vexpress_hwmon_u32_show,
126 static VEXPRESS_HWMON_ATTRS(power, power1_label, power1_input);
127 static struct attribute_group vexpress_hwmon_group_power = {
128 .attrs = vexpress_hwmon_attrs_power,
131 static DEVICE_ATTR(energy1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
132 static SENSOR_DEVICE_ATTR(energy1_input, S_IRUGO, vexpress_hwmon_u64_show,
134 static VEXPRESS_HWMON_ATTRS(energy, energy1_label, energy1_input);
135 static struct attribute_group vexpress_hwmon_group_energy = {
136 .attrs = vexpress_hwmon_attrs_energy,
139 static struct of_device_id vexpress_hwmon_of_match[] = {
140 #if !defined(CONFIG_REGULATOR_VEXPRESS)
142 .compatible = "arm,vexpress-volt",
143 .data = &vexpress_hwmon_group_volt,
147 .compatible = "arm,vexpress-amp",
148 .data = &vexpress_hwmon_group_amp,
150 .compatible = "arm,vexpress-temp",
151 .data = &vexpress_hwmon_group_temp,
153 .compatible = "arm,vexpress-power",
154 .data = &vexpress_hwmon_group_power,
156 .compatible = "arm,vexpress-energy",
157 .data = &vexpress_hwmon_group_energy,
161 MODULE_DEVICE_TABLE(of, vexpress_hwmon_of_match);
163 static int vexpress_hwmon_probe(struct platform_device *pdev)
166 const struct of_device_id *match;
167 struct vexpress_hwmon_data *data;
169 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
172 platform_set_drvdata(pdev, data);
174 match = of_match_device(vexpress_hwmon_of_match, &pdev->dev);
178 data->func = vexpress_config_func_get_by_dev(&pdev->dev);
182 err = sysfs_create_group(&pdev->dev.kobj, match->data);
186 data->hwmon_dev = hwmon_device_register(&pdev->dev);
187 if (IS_ERR(data->hwmon_dev)) {
188 err = PTR_ERR(data->hwmon_dev);
195 sysfs_remove_group(&pdev->dev.kobj, match->data);
196 vexpress_config_func_put(data->func);
200 static int vexpress_hwmon_remove(struct platform_device *pdev)
202 struct vexpress_hwmon_data *data = platform_get_drvdata(pdev);
203 const struct of_device_id *match;
205 hwmon_device_unregister(data->hwmon_dev);
207 match = of_match_device(vexpress_hwmon_of_match, &pdev->dev);
208 sysfs_remove_group(&pdev->dev.kobj, match->data);
210 vexpress_config_func_put(data->func);
215 static struct platform_driver vexpress_hwmon_driver = {
216 .probe = vexpress_hwmon_probe,
217 .remove = vexpress_hwmon_remove,
220 .owner = THIS_MODULE,
221 .of_match_table = vexpress_hwmon_of_match,
225 module_platform_driver(vexpress_hwmon_driver);
227 MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
228 MODULE_DESCRIPTION("Versatile Express hwmon sensors driver");
229 MODULE_LICENSE("GPL");
230 MODULE_ALIAS("platform:vexpress-hwmon");