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;
32 static ssize_t vexpress_hwmon_label_show(struct device *dev,
33 struct device_attribute *dev_attr, char *buffer)
35 const char *label = of_get_property(dev->of_node, "label", NULL);
37 return snprintf(buffer, PAGE_SIZE, "%s\n", label);
40 static ssize_t vexpress_hwmon_u32_show(struct device *dev,
41 struct device_attribute *dev_attr, char *buffer)
43 struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
47 err = regmap_read(data->reg, 0, &value);
51 return snprintf(buffer, PAGE_SIZE, "%u\n", value /
52 to_sensor_dev_attr(dev_attr)->index);
55 static ssize_t vexpress_hwmon_u64_show(struct device *dev,
56 struct device_attribute *dev_attr, char *buffer)
58 struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
60 u32 value_hi, value_lo;
62 err = regmap_read(data->reg, 0, &value_lo);
66 err = regmap_read(data->reg, 1, &value_hi);
70 return snprintf(buffer, PAGE_SIZE, "%llu\n",
71 div_u64(((u64)value_hi << 32) | value_lo,
72 to_sensor_dev_attr(dev_attr)->index));
75 static umode_t vexpress_hwmon_attr_is_visible(struct kobject *kobj,
76 struct attribute *attr, int index)
78 struct device *dev = kobj_to_dev(kobj);
79 struct device_attribute *dev_attr = container_of(attr,
80 struct device_attribute, attr);
82 if (dev_attr->show == vexpress_hwmon_label_show &&
83 !of_get_property(dev->of_node, "label", NULL))
89 struct vexpress_hwmon_type {
91 const struct attribute_group **attr_groups;
94 #if !defined(CONFIG_REGULATOR_VEXPRESS)
95 static DEVICE_ATTR(in1_label, 0444, vexpress_hwmon_label_show, NULL);
96 static SENSOR_DEVICE_ATTR_RO(in1_input, vexpress_hwmon_u32, 1000);
97 static struct attribute *vexpress_hwmon_attrs_volt[] = {
98 &dev_attr_in1_label.attr,
99 &sensor_dev_attr_in1_input.dev_attr.attr,
102 static struct attribute_group vexpress_hwmon_group_volt = {
103 .is_visible = vexpress_hwmon_attr_is_visible,
104 .attrs = vexpress_hwmon_attrs_volt,
106 static struct vexpress_hwmon_type vexpress_hwmon_volt = {
107 .name = "vexpress_volt",
108 .attr_groups = (const struct attribute_group *[]) {
109 &vexpress_hwmon_group_volt,
115 static DEVICE_ATTR(curr1_label, 0444, vexpress_hwmon_label_show, NULL);
116 static SENSOR_DEVICE_ATTR_RO(curr1_input, vexpress_hwmon_u32, 1000);
117 static struct attribute *vexpress_hwmon_attrs_amp[] = {
118 &dev_attr_curr1_label.attr,
119 &sensor_dev_attr_curr1_input.dev_attr.attr,
122 static struct attribute_group vexpress_hwmon_group_amp = {
123 .is_visible = vexpress_hwmon_attr_is_visible,
124 .attrs = vexpress_hwmon_attrs_amp,
126 static struct vexpress_hwmon_type vexpress_hwmon_amp = {
127 .name = "vexpress_amp",
128 .attr_groups = (const struct attribute_group *[]) {
129 &vexpress_hwmon_group_amp,
134 static DEVICE_ATTR(temp1_label, 0444, vexpress_hwmon_label_show, NULL);
135 static SENSOR_DEVICE_ATTR_RO(temp1_input, vexpress_hwmon_u32, 1000);
136 static struct attribute *vexpress_hwmon_attrs_temp[] = {
137 &dev_attr_temp1_label.attr,
138 &sensor_dev_attr_temp1_input.dev_attr.attr,
141 static struct attribute_group vexpress_hwmon_group_temp = {
142 .is_visible = vexpress_hwmon_attr_is_visible,
143 .attrs = vexpress_hwmon_attrs_temp,
145 static struct vexpress_hwmon_type vexpress_hwmon_temp = {
146 .name = "vexpress_temp",
147 .attr_groups = (const struct attribute_group *[]) {
148 &vexpress_hwmon_group_temp,
153 static DEVICE_ATTR(power1_label, 0444, vexpress_hwmon_label_show, NULL);
154 static SENSOR_DEVICE_ATTR_RO(power1_input, vexpress_hwmon_u32, 1);
155 static struct attribute *vexpress_hwmon_attrs_power[] = {
156 &dev_attr_power1_label.attr,
157 &sensor_dev_attr_power1_input.dev_attr.attr,
160 static struct attribute_group vexpress_hwmon_group_power = {
161 .is_visible = vexpress_hwmon_attr_is_visible,
162 .attrs = vexpress_hwmon_attrs_power,
164 static struct vexpress_hwmon_type vexpress_hwmon_power = {
165 .name = "vexpress_power",
166 .attr_groups = (const struct attribute_group *[]) {
167 &vexpress_hwmon_group_power,
172 static DEVICE_ATTR(energy1_label, 0444, vexpress_hwmon_label_show, NULL);
173 static SENSOR_DEVICE_ATTR_RO(energy1_input, vexpress_hwmon_u64, 1);
174 static struct attribute *vexpress_hwmon_attrs_energy[] = {
175 &dev_attr_energy1_label.attr,
176 &sensor_dev_attr_energy1_input.dev_attr.attr,
179 static struct attribute_group vexpress_hwmon_group_energy = {
180 .is_visible = vexpress_hwmon_attr_is_visible,
181 .attrs = vexpress_hwmon_attrs_energy,
183 static struct vexpress_hwmon_type vexpress_hwmon_energy = {
184 .name = "vexpress_energy",
185 .attr_groups = (const struct attribute_group *[]) {
186 &vexpress_hwmon_group_energy,
191 static const struct of_device_id vexpress_hwmon_of_match[] = {
192 #if !defined(CONFIG_REGULATOR_VEXPRESS)
194 .compatible = "arm,vexpress-volt",
195 .data = &vexpress_hwmon_volt,
199 .compatible = "arm,vexpress-amp",
200 .data = &vexpress_hwmon_amp,
202 .compatible = "arm,vexpress-temp",
203 .data = &vexpress_hwmon_temp,
205 .compatible = "arm,vexpress-power",
206 .data = &vexpress_hwmon_power,
208 .compatible = "arm,vexpress-energy",
209 .data = &vexpress_hwmon_energy,
213 MODULE_DEVICE_TABLE(of, vexpress_hwmon_of_match);
215 static int vexpress_hwmon_probe(struct platform_device *pdev)
217 const struct of_device_id *match;
218 struct vexpress_hwmon_data *data;
219 const struct vexpress_hwmon_type *type;
221 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
224 platform_set_drvdata(pdev, data);
226 match = of_match_device(vexpress_hwmon_of_match, &pdev->dev);
231 data->reg = devm_regmap_init_vexpress_config(&pdev->dev);
232 if (IS_ERR(data->reg))
233 return PTR_ERR(data->reg);
235 data->hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev,
236 type->name, data, type->attr_groups);
238 return PTR_ERR_OR_ZERO(data->hwmon_dev);
241 static struct platform_driver vexpress_hwmon_driver = {
242 .probe = vexpress_hwmon_probe,
245 .of_match_table = vexpress_hwmon_of_match,
249 module_platform_driver(vexpress_hwmon_driver);
251 MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
252 MODULE_DESCRIPTION("Versatile Express hwmon sensors driver");
253 MODULE_LICENSE("GPL");
254 MODULE_ALIAS("platform:vexpress-hwmon");