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>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/vexpress.h>
26 struct vexpress_hwmon_data
{
27 struct device
*hwmon_dev
;
28 struct vexpress_config_func
*func
;
31 static ssize_t
vexpress_hwmon_name_show(struct device
*dev
,
32 struct device_attribute
*dev_attr
, char *buffer
)
34 const char *compatible
= of_get_property(dev
->of_node
, "compatible",
37 return sprintf(buffer
, "%s\n", compatible
);
40 static ssize_t
vexpress_hwmon_label_show(struct device
*dev
,
41 struct device_attribute
*dev_attr
, char *buffer
)
43 const char *label
= of_get_property(dev
->of_node
, "label", NULL
);
48 return snprintf(buffer
, PAGE_SIZE
, "%s\n", label
);
51 static ssize_t
vexpress_hwmon_u32_show(struct device
*dev
,
52 struct device_attribute
*dev_attr
, char *buffer
)
54 struct vexpress_hwmon_data
*data
= dev_get_drvdata(dev
);
58 err
= vexpress_config_read(data
->func
, 0, &value
);
62 return snprintf(buffer
, PAGE_SIZE
, "%u\n", value
/
63 to_sensor_dev_attr(dev_attr
)->index
);
66 static ssize_t
vexpress_hwmon_u64_show(struct device
*dev
,
67 struct device_attribute
*dev_attr
, char *buffer
)
69 struct vexpress_hwmon_data
*data
= dev_get_drvdata(dev
);
71 u32 value_hi
, value_lo
;
73 err
= vexpress_config_read(data
->func
, 0, &value_lo
);
77 err
= vexpress_config_read(data
->func
, 1, &value_hi
);
81 return snprintf(buffer
, PAGE_SIZE
, "%llu\n",
82 div_u64(((u64
)value_hi
<< 32) | value_lo
,
83 to_sensor_dev_attr(dev_attr
)->index
));
86 static DEVICE_ATTR(name
, S_IRUGO
, vexpress_hwmon_name_show
, NULL
);
88 #define VEXPRESS_HWMON_ATTRS(_name, _label_attr, _input_attr) \
89 struct attribute *vexpress_hwmon_attrs_##_name[] = { \
90 &dev_attr_name.attr, \
91 &dev_attr_##_label_attr.attr, \
92 &sensor_dev_attr_##_input_attr.dev_attr.attr, \
96 #if !defined(CONFIG_REGULATOR_VEXPRESS)
97 static DEVICE_ATTR(in1_label
, S_IRUGO
, vexpress_hwmon_label_show
, NULL
);
98 static SENSOR_DEVICE_ATTR(in1_input
, S_IRUGO
, vexpress_hwmon_u32_show
,
100 static VEXPRESS_HWMON_ATTRS(volt
, in1_label
, in1_input
);
101 static struct attribute_group vexpress_hwmon_group_volt
= {
102 .attrs
= vexpress_hwmon_attrs_volt
,
106 static DEVICE_ATTR(curr1_label
, S_IRUGO
, vexpress_hwmon_label_show
, NULL
);
107 static SENSOR_DEVICE_ATTR(curr1_input
, S_IRUGO
, vexpress_hwmon_u32_show
,
109 static VEXPRESS_HWMON_ATTRS(amp
, curr1_label
, curr1_input
);
110 static struct attribute_group vexpress_hwmon_group_amp
= {
111 .attrs
= vexpress_hwmon_attrs_amp
,
114 static DEVICE_ATTR(temp1_label
, S_IRUGO
, vexpress_hwmon_label_show
, NULL
);
115 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, vexpress_hwmon_u32_show
,
117 static VEXPRESS_HWMON_ATTRS(temp
, temp1_label
, temp1_input
);
118 static struct attribute_group vexpress_hwmon_group_temp
= {
119 .attrs
= vexpress_hwmon_attrs_temp
,
122 static DEVICE_ATTR(power1_label
, S_IRUGO
, vexpress_hwmon_label_show
, NULL
);
123 static SENSOR_DEVICE_ATTR(power1_input
, S_IRUGO
, vexpress_hwmon_u32_show
,
125 static VEXPRESS_HWMON_ATTRS(power
, power1_label
, power1_input
);
126 static struct attribute_group vexpress_hwmon_group_power
= {
127 .attrs
= vexpress_hwmon_attrs_power
,
130 static DEVICE_ATTR(energy1_label
, S_IRUGO
, vexpress_hwmon_label_show
, NULL
);
131 static SENSOR_DEVICE_ATTR(energy1_input
, S_IRUGO
, vexpress_hwmon_u64_show
,
133 static VEXPRESS_HWMON_ATTRS(energy
, energy1_label
, energy1_input
);
134 static struct attribute_group vexpress_hwmon_group_energy
= {
135 .attrs
= vexpress_hwmon_attrs_energy
,
138 static struct of_device_id vexpress_hwmon_of_match
[] = {
139 #if !defined(CONFIG_REGULATOR_VEXPRESS)
141 .compatible
= "arm,vexpress-volt",
142 .data
= &vexpress_hwmon_group_volt
,
146 .compatible
= "arm,vexpress-amp",
147 .data
= &vexpress_hwmon_group_amp
,
149 .compatible
= "arm,vexpress-temp",
150 .data
= &vexpress_hwmon_group_temp
,
152 .compatible
= "arm,vexpress-power",
153 .data
= &vexpress_hwmon_group_power
,
155 .compatible
= "arm,vexpress-energy",
156 .data
= &vexpress_hwmon_group_energy
,
160 MODULE_DEVICE_TABLE(of
, vexpress_hwmon_of_match
);
162 static int vexpress_hwmon_probe(struct platform_device
*pdev
)
165 const struct of_device_id
*match
;
166 struct vexpress_hwmon_data
*data
;
168 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
171 platform_set_drvdata(pdev
, data
);
173 match
= of_match_device(vexpress_hwmon_of_match
, &pdev
->dev
);
177 data
->func
= vexpress_config_func_get_by_dev(&pdev
->dev
);
181 err
= sysfs_create_group(&pdev
->dev
.kobj
, match
->data
);
185 data
->hwmon_dev
= hwmon_device_register(&pdev
->dev
);
186 if (IS_ERR(data
->hwmon_dev
)) {
187 err
= PTR_ERR(data
->hwmon_dev
);
194 sysfs_remove_group(&pdev
->dev
.kobj
, match
->data
);
195 vexpress_config_func_put(data
->func
);
199 static int vexpress_hwmon_remove(struct platform_device
*pdev
)
201 struct vexpress_hwmon_data
*data
= platform_get_drvdata(pdev
);
202 const struct of_device_id
*match
;
204 hwmon_device_unregister(data
->hwmon_dev
);
206 match
= of_match_device(vexpress_hwmon_of_match
, &pdev
->dev
);
207 sysfs_remove_group(&pdev
->dev
.kobj
, match
->data
);
209 vexpress_config_func_put(data
->func
);
214 static struct platform_driver vexpress_hwmon_driver
= {
215 .probe
= vexpress_hwmon_probe
,
216 .remove
= vexpress_hwmon_remove
,
219 .owner
= THIS_MODULE
,
220 .of_match_table
= vexpress_hwmon_of_match
,
224 module_platform_driver(vexpress_hwmon_driver
);
226 MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
227 MODULE_DESCRIPTION("Versatile Express hwmon sensors driver");
228 MODULE_LICENSE("GPL");
229 MODULE_ALIAS("platform:vexpress-hwmon");