blob: 8242b75d96c87e9b69a079f00e871e70f1867c7f [file] [log] [blame]
Pawel Moll48ed8872012-09-17 18:40:09 +01001/*
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.
5 *
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.
10 *
11 * Copyright (C) 2012 ARM Limited
12 */
13
14#define DRVNAME "vexpress-hwmon"
15#define pr_fmt(fmt) DRVNAME ": " fmt
16
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>
Guenter Roeck08245ad2012-12-31 00:04:59 -080022#include <linux/of.h>
Pawel Moll48ed8872012-09-17 18:40:09 +010023#include <linux/of_device.h>
24#include <linux/platform_device.h>
25#include <linux/vexpress.h>
26
27struct vexpress_hwmon_data {
28 struct device *hwmon_dev;
29 struct vexpress_config_func *func;
Pawel Moll52feaca2014-04-23 18:27:04 +010030 const char *name;
Pawel Moll48ed8872012-09-17 18:40:09 +010031};
32
33static ssize_t vexpress_hwmon_name_show(struct device *dev,
34 struct device_attribute *dev_attr, char *buffer)
35{
Pawel Moll52feaca2014-04-23 18:27:04 +010036 struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
Pawel Moll48ed8872012-09-17 18:40:09 +010037
Pawel Moll52feaca2014-04-23 18:27:04 +010038 return sprintf(buffer, "%s\n", data->name);
Pawel Moll48ed8872012-09-17 18:40:09 +010039}
40
41static ssize_t vexpress_hwmon_label_show(struct device *dev,
42 struct device_attribute *dev_attr, char *buffer)
43{
44 const char *label = of_get_property(dev->of_node, "label", NULL);
45
Pawel Moll48ed8872012-09-17 18:40:09 +010046 return snprintf(buffer, PAGE_SIZE, "%s\n", label);
47}
48
49static ssize_t vexpress_hwmon_u32_show(struct device *dev,
50 struct device_attribute *dev_attr, char *buffer)
51{
52 struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
53 int err;
54 u32 value;
55
56 err = vexpress_config_read(data->func, 0, &value);
57 if (err)
58 return err;
59
60 return snprintf(buffer, PAGE_SIZE, "%u\n", value /
61 to_sensor_dev_attr(dev_attr)->index);
62}
63
64static ssize_t vexpress_hwmon_u64_show(struct device *dev,
65 struct device_attribute *dev_attr, char *buffer)
66{
67 struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
68 int err;
69 u32 value_hi, value_lo;
70
71 err = vexpress_config_read(data->func, 0, &value_lo);
72 if (err)
73 return err;
74
75 err = vexpress_config_read(data->func, 1, &value_hi);
76 if (err)
77 return err;
78
79 return snprintf(buffer, PAGE_SIZE, "%llu\n",
80 div_u64(((u64)value_hi << 32) | value_lo,
81 to_sensor_dev_attr(dev_attr)->index));
82}
83
Pawel Mollb2e54112014-04-23 18:27:05 +010084static umode_t vexpress_hwmon_attr_is_visible(struct kobject *kobj,
85 struct attribute *attr, int index)
86{
87 struct device *dev = kobj_to_dev(kobj);
88 struct device_attribute *dev_attr = container_of(attr,
89 struct device_attribute, attr);
90
91 if (dev_attr->show == vexpress_hwmon_label_show &&
92 !of_get_property(dev->of_node, "label", NULL))
93 return 0;
94
95 return attr->mode;
96}
97
Pawel Moll48ed8872012-09-17 18:40:09 +010098static DEVICE_ATTR(name, S_IRUGO, vexpress_hwmon_name_show, NULL);
99
100#define VEXPRESS_HWMON_ATTRS(_name, _label_attr, _input_attr) \
101struct attribute *vexpress_hwmon_attrs_##_name[] = { \
102 &dev_attr_name.attr, \
103 &dev_attr_##_label_attr.attr, \
104 &sensor_dev_attr_##_input_attr.dev_attr.attr, \
105 NULL \
106}
107
Pawel Moll52feaca2014-04-23 18:27:04 +0100108struct vexpress_hwmon_type {
109 const char *name;
110 const struct attribute_group **attr_groups;
111};
112
Pawel Moll48ed8872012-09-17 18:40:09 +0100113#if !defined(CONFIG_REGULATOR_VEXPRESS)
114static DEVICE_ATTR(in1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
115static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, vexpress_hwmon_u32_show,
116 NULL, 1000);
117static VEXPRESS_HWMON_ATTRS(volt, in1_label, in1_input);
118static struct attribute_group vexpress_hwmon_group_volt = {
Pawel Mollb2e54112014-04-23 18:27:05 +0100119 .is_visible = vexpress_hwmon_attr_is_visible,
Pawel Moll48ed8872012-09-17 18:40:09 +0100120 .attrs = vexpress_hwmon_attrs_volt,
121};
Pawel Moll52feaca2014-04-23 18:27:04 +0100122static struct vexpress_hwmon_type vexpress_hwmon_volt = {
123 .name = "vexpress_volt",
124 .attr_groups = (const struct attribute_group *[]) {
125 &vexpress_hwmon_group_volt,
126 NULL,
127 },
128};
Pawel Moll48ed8872012-09-17 18:40:09 +0100129#endif
130
131static DEVICE_ATTR(curr1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
132static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, vexpress_hwmon_u32_show,
133 NULL, 1000);
134static VEXPRESS_HWMON_ATTRS(amp, curr1_label, curr1_input);
135static struct attribute_group vexpress_hwmon_group_amp = {
Pawel Mollb2e54112014-04-23 18:27:05 +0100136 .is_visible = vexpress_hwmon_attr_is_visible,
Pawel Moll48ed8872012-09-17 18:40:09 +0100137 .attrs = vexpress_hwmon_attrs_amp,
138};
Pawel Moll52feaca2014-04-23 18:27:04 +0100139static struct vexpress_hwmon_type vexpress_hwmon_amp = {
140 .name = "vexpress_amp",
141 .attr_groups = (const struct attribute_group *[]) {
142 &vexpress_hwmon_group_amp,
143 NULL
144 },
145};
Pawel Moll48ed8872012-09-17 18:40:09 +0100146
147static DEVICE_ATTR(temp1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
148static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, vexpress_hwmon_u32_show,
149 NULL, 1000);
150static VEXPRESS_HWMON_ATTRS(temp, temp1_label, temp1_input);
151static struct attribute_group vexpress_hwmon_group_temp = {
Pawel Mollb2e54112014-04-23 18:27:05 +0100152 .is_visible = vexpress_hwmon_attr_is_visible,
Pawel Moll48ed8872012-09-17 18:40:09 +0100153 .attrs = vexpress_hwmon_attrs_temp,
154};
Pawel Moll52feaca2014-04-23 18:27:04 +0100155static struct vexpress_hwmon_type vexpress_hwmon_temp = {
156 .name = "vexpress_temp",
157 .attr_groups = (const struct attribute_group *[]) {
158 &vexpress_hwmon_group_temp,
159 NULL
160 },
161};
Pawel Moll48ed8872012-09-17 18:40:09 +0100162
163static DEVICE_ATTR(power1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
164static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, vexpress_hwmon_u32_show,
165 NULL, 1);
166static VEXPRESS_HWMON_ATTRS(power, power1_label, power1_input);
167static struct attribute_group vexpress_hwmon_group_power = {
Pawel Mollb2e54112014-04-23 18:27:05 +0100168 .is_visible = vexpress_hwmon_attr_is_visible,
Pawel Moll48ed8872012-09-17 18:40:09 +0100169 .attrs = vexpress_hwmon_attrs_power,
170};
Pawel Moll52feaca2014-04-23 18:27:04 +0100171static struct vexpress_hwmon_type vexpress_hwmon_power = {
172 .name = "vexpress_power",
173 .attr_groups = (const struct attribute_group *[]) {
174 &vexpress_hwmon_group_power,
175 NULL
176 },
177};
Pawel Moll48ed8872012-09-17 18:40:09 +0100178
179static DEVICE_ATTR(energy1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
180static SENSOR_DEVICE_ATTR(energy1_input, S_IRUGO, vexpress_hwmon_u64_show,
181 NULL, 1);
182static VEXPRESS_HWMON_ATTRS(energy, energy1_label, energy1_input);
183static struct attribute_group vexpress_hwmon_group_energy = {
Pawel Mollb2e54112014-04-23 18:27:05 +0100184 .is_visible = vexpress_hwmon_attr_is_visible,
Pawel Moll48ed8872012-09-17 18:40:09 +0100185 .attrs = vexpress_hwmon_attrs_energy,
186};
Pawel Moll52feaca2014-04-23 18:27:04 +0100187static struct vexpress_hwmon_type vexpress_hwmon_energy = {
188 .name = "vexpress_energy",
189 .attr_groups = (const struct attribute_group *[]) {
190 &vexpress_hwmon_group_energy,
191 NULL
192 },
193};
Pawel Moll48ed8872012-09-17 18:40:09 +0100194
195static struct of_device_id vexpress_hwmon_of_match[] = {
196#if !defined(CONFIG_REGULATOR_VEXPRESS)
197 {
198 .compatible = "arm,vexpress-volt",
Pawel Moll52feaca2014-04-23 18:27:04 +0100199 .data = &vexpress_hwmon_volt,
Pawel Moll48ed8872012-09-17 18:40:09 +0100200 },
201#endif
202 {
203 .compatible = "arm,vexpress-amp",
Pawel Moll52feaca2014-04-23 18:27:04 +0100204 .data = &vexpress_hwmon_amp,
Pawel Moll48ed8872012-09-17 18:40:09 +0100205 }, {
206 .compatible = "arm,vexpress-temp",
Pawel Moll52feaca2014-04-23 18:27:04 +0100207 .data = &vexpress_hwmon_temp,
Pawel Moll48ed8872012-09-17 18:40:09 +0100208 }, {
209 .compatible = "arm,vexpress-power",
Pawel Moll52feaca2014-04-23 18:27:04 +0100210 .data = &vexpress_hwmon_power,
Pawel Moll48ed8872012-09-17 18:40:09 +0100211 }, {
212 .compatible = "arm,vexpress-energy",
Pawel Moll52feaca2014-04-23 18:27:04 +0100213 .data = &vexpress_hwmon_energy,
Pawel Moll48ed8872012-09-17 18:40:09 +0100214 },
215 {}
216};
217MODULE_DEVICE_TABLE(of, vexpress_hwmon_of_match);
218
219static int vexpress_hwmon_probe(struct platform_device *pdev)
220{
221 int err;
222 const struct of_device_id *match;
223 struct vexpress_hwmon_data *data;
Pawel Moll52feaca2014-04-23 18:27:04 +0100224 const struct vexpress_hwmon_type *type;
Pawel Moll48ed8872012-09-17 18:40:09 +0100225
226 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
227 if (!data)
228 return -ENOMEM;
229 platform_set_drvdata(pdev, data);
230
231 match = of_match_device(vexpress_hwmon_of_match, &pdev->dev);
232 if (!match)
233 return -ENODEV;
Pawel Moll52feaca2014-04-23 18:27:04 +0100234 type = match->data;
235 data->name = type->name;
Pawel Moll48ed8872012-09-17 18:40:09 +0100236
237 data->func = vexpress_config_func_get_by_dev(&pdev->dev);
238 if (!data->func)
239 return -ENODEV;
240
Pawel Moll52feaca2014-04-23 18:27:04 +0100241 err = sysfs_create_groups(&pdev->dev.kobj, type->attr_groups);
Pawel Moll48ed8872012-09-17 18:40:09 +0100242 if (err)
243 goto error;
244
245 data->hwmon_dev = hwmon_device_register(&pdev->dev);
246 if (IS_ERR(data->hwmon_dev)) {
247 err = PTR_ERR(data->hwmon_dev);
248 goto error;
249 }
250
251 return 0;
252
253error:
254 sysfs_remove_group(&pdev->dev.kobj, match->data);
255 vexpress_config_func_put(data->func);
256 return err;
257}
258
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800259static int vexpress_hwmon_remove(struct platform_device *pdev)
Pawel Moll48ed8872012-09-17 18:40:09 +0100260{
261 struct vexpress_hwmon_data *data = platform_get_drvdata(pdev);
262 const struct of_device_id *match;
263
264 hwmon_device_unregister(data->hwmon_dev);
265
266 match = of_match_device(vexpress_hwmon_of_match, &pdev->dev);
267 sysfs_remove_group(&pdev->dev.kobj, match->data);
268
269 vexpress_config_func_put(data->func);
270
271 return 0;
272}
273
274static struct platform_driver vexpress_hwmon_driver = {
275 .probe = vexpress_hwmon_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800276 .remove = vexpress_hwmon_remove,
Pawel Moll48ed8872012-09-17 18:40:09 +0100277 .driver = {
278 .name = DRVNAME,
279 .owner = THIS_MODULE,
280 .of_match_table = vexpress_hwmon_of_match,
281 },
282};
283
284module_platform_driver(vexpress_hwmon_driver);
285
286MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
287MODULE_DESCRIPTION("Versatile Express hwmon sensors driver");
288MODULE_LICENSE("GPL");
289MODULE_ALIAS("platform:vexpress-hwmon");