blob: 7f9690560f4065d5159ae6fadeb2b726dee18579 [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
46 if (!label)
47 return -ENOENT;
48
49 return snprintf(buffer, PAGE_SIZE, "%s\n", label);
50}
51
52static ssize_t vexpress_hwmon_u32_show(struct device *dev,
53 struct device_attribute *dev_attr, char *buffer)
54{
55 struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
56 int err;
57 u32 value;
58
59 err = vexpress_config_read(data->func, 0, &value);
60 if (err)
61 return err;
62
63 return snprintf(buffer, PAGE_SIZE, "%u\n", value /
64 to_sensor_dev_attr(dev_attr)->index);
65}
66
67static ssize_t vexpress_hwmon_u64_show(struct device *dev,
68 struct device_attribute *dev_attr, char *buffer)
69{
70 struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
71 int err;
72 u32 value_hi, value_lo;
73
74 err = vexpress_config_read(data->func, 0, &value_lo);
75 if (err)
76 return err;
77
78 err = vexpress_config_read(data->func, 1, &value_hi);
79 if (err)
80 return err;
81
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));
85}
86
87static DEVICE_ATTR(name, S_IRUGO, vexpress_hwmon_name_show, NULL);
88
89#define VEXPRESS_HWMON_ATTRS(_name, _label_attr, _input_attr) \
90struct 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, \
94 NULL \
95}
96
Pawel Moll52feaca2014-04-23 18:27:04 +010097struct vexpress_hwmon_type {
98 const char *name;
99 const struct attribute_group **attr_groups;
100};
101
Pawel Moll48ed8872012-09-17 18:40:09 +0100102#if !defined(CONFIG_REGULATOR_VEXPRESS)
103static DEVICE_ATTR(in1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
104static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, vexpress_hwmon_u32_show,
105 NULL, 1000);
106static VEXPRESS_HWMON_ATTRS(volt, in1_label, in1_input);
107static struct attribute_group vexpress_hwmon_group_volt = {
108 .attrs = vexpress_hwmon_attrs_volt,
109};
Pawel Moll52feaca2014-04-23 18:27:04 +0100110static struct vexpress_hwmon_type vexpress_hwmon_volt = {
111 .name = "vexpress_volt",
112 .attr_groups = (const struct attribute_group *[]) {
113 &vexpress_hwmon_group_volt,
114 NULL,
115 },
116};
Pawel Moll48ed8872012-09-17 18:40:09 +0100117#endif
118
119static DEVICE_ATTR(curr1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
120static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, vexpress_hwmon_u32_show,
121 NULL, 1000);
122static VEXPRESS_HWMON_ATTRS(amp, curr1_label, curr1_input);
123static struct attribute_group vexpress_hwmon_group_amp = {
124 .attrs = vexpress_hwmon_attrs_amp,
125};
Pawel Moll52feaca2014-04-23 18:27:04 +0100126static struct vexpress_hwmon_type vexpress_hwmon_amp = {
127 .name = "vexpress_amp",
128 .attr_groups = (const struct attribute_group *[]) {
129 &vexpress_hwmon_group_amp,
130 NULL
131 },
132};
Pawel Moll48ed8872012-09-17 18:40:09 +0100133
134static DEVICE_ATTR(temp1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
135static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, vexpress_hwmon_u32_show,
136 NULL, 1000);
137static VEXPRESS_HWMON_ATTRS(temp, temp1_label, temp1_input);
138static struct attribute_group vexpress_hwmon_group_temp = {
139 .attrs = vexpress_hwmon_attrs_temp,
140};
Pawel Moll52feaca2014-04-23 18:27:04 +0100141static struct vexpress_hwmon_type vexpress_hwmon_temp = {
142 .name = "vexpress_temp",
143 .attr_groups = (const struct attribute_group *[]) {
144 &vexpress_hwmon_group_temp,
145 NULL
146 },
147};
Pawel Moll48ed8872012-09-17 18:40:09 +0100148
149static DEVICE_ATTR(power1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
150static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, vexpress_hwmon_u32_show,
151 NULL, 1);
152static VEXPRESS_HWMON_ATTRS(power, power1_label, power1_input);
153static struct attribute_group vexpress_hwmon_group_power = {
154 .attrs = vexpress_hwmon_attrs_power,
155};
Pawel Moll52feaca2014-04-23 18:27:04 +0100156static struct vexpress_hwmon_type vexpress_hwmon_power = {
157 .name = "vexpress_power",
158 .attr_groups = (const struct attribute_group *[]) {
159 &vexpress_hwmon_group_power,
160 NULL
161 },
162};
Pawel Moll48ed8872012-09-17 18:40:09 +0100163
164static DEVICE_ATTR(energy1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
165static SENSOR_DEVICE_ATTR(energy1_input, S_IRUGO, vexpress_hwmon_u64_show,
166 NULL, 1);
167static VEXPRESS_HWMON_ATTRS(energy, energy1_label, energy1_input);
168static struct attribute_group vexpress_hwmon_group_energy = {
169 .attrs = vexpress_hwmon_attrs_energy,
170};
Pawel Moll52feaca2014-04-23 18:27:04 +0100171static struct vexpress_hwmon_type vexpress_hwmon_energy = {
172 .name = "vexpress_energy",
173 .attr_groups = (const struct attribute_group *[]) {
174 &vexpress_hwmon_group_energy,
175 NULL
176 },
177};
Pawel Moll48ed8872012-09-17 18:40:09 +0100178
179static struct of_device_id vexpress_hwmon_of_match[] = {
180#if !defined(CONFIG_REGULATOR_VEXPRESS)
181 {
182 .compatible = "arm,vexpress-volt",
Pawel Moll52feaca2014-04-23 18:27:04 +0100183 .data = &vexpress_hwmon_volt,
Pawel Moll48ed8872012-09-17 18:40:09 +0100184 },
185#endif
186 {
187 .compatible = "arm,vexpress-amp",
Pawel Moll52feaca2014-04-23 18:27:04 +0100188 .data = &vexpress_hwmon_amp,
Pawel Moll48ed8872012-09-17 18:40:09 +0100189 }, {
190 .compatible = "arm,vexpress-temp",
Pawel Moll52feaca2014-04-23 18:27:04 +0100191 .data = &vexpress_hwmon_temp,
Pawel Moll48ed8872012-09-17 18:40:09 +0100192 }, {
193 .compatible = "arm,vexpress-power",
Pawel Moll52feaca2014-04-23 18:27:04 +0100194 .data = &vexpress_hwmon_power,
Pawel Moll48ed8872012-09-17 18:40:09 +0100195 }, {
196 .compatible = "arm,vexpress-energy",
Pawel Moll52feaca2014-04-23 18:27:04 +0100197 .data = &vexpress_hwmon_energy,
Pawel Moll48ed8872012-09-17 18:40:09 +0100198 },
199 {}
200};
201MODULE_DEVICE_TABLE(of, vexpress_hwmon_of_match);
202
203static int vexpress_hwmon_probe(struct platform_device *pdev)
204{
205 int err;
206 const struct of_device_id *match;
207 struct vexpress_hwmon_data *data;
Pawel Moll52feaca2014-04-23 18:27:04 +0100208 const struct vexpress_hwmon_type *type;
Pawel Moll48ed8872012-09-17 18:40:09 +0100209
210 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
211 if (!data)
212 return -ENOMEM;
213 platform_set_drvdata(pdev, data);
214
215 match = of_match_device(vexpress_hwmon_of_match, &pdev->dev);
216 if (!match)
217 return -ENODEV;
Pawel Moll52feaca2014-04-23 18:27:04 +0100218 type = match->data;
219 data->name = type->name;
Pawel Moll48ed8872012-09-17 18:40:09 +0100220
221 data->func = vexpress_config_func_get_by_dev(&pdev->dev);
222 if (!data->func)
223 return -ENODEV;
224
Pawel Moll52feaca2014-04-23 18:27:04 +0100225 err = sysfs_create_groups(&pdev->dev.kobj, type->attr_groups);
Pawel Moll48ed8872012-09-17 18:40:09 +0100226 if (err)
227 goto error;
228
229 data->hwmon_dev = hwmon_device_register(&pdev->dev);
230 if (IS_ERR(data->hwmon_dev)) {
231 err = PTR_ERR(data->hwmon_dev);
232 goto error;
233 }
234
235 return 0;
236
237error:
238 sysfs_remove_group(&pdev->dev.kobj, match->data);
239 vexpress_config_func_put(data->func);
240 return err;
241}
242
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800243static int vexpress_hwmon_remove(struct platform_device *pdev)
Pawel Moll48ed8872012-09-17 18:40:09 +0100244{
245 struct vexpress_hwmon_data *data = platform_get_drvdata(pdev);
246 const struct of_device_id *match;
247
248 hwmon_device_unregister(data->hwmon_dev);
249
250 match = of_match_device(vexpress_hwmon_of_match, &pdev->dev);
251 sysfs_remove_group(&pdev->dev.kobj, match->data);
252
253 vexpress_config_func_put(data->func);
254
255 return 0;
256}
257
258static struct platform_driver vexpress_hwmon_driver = {
259 .probe = vexpress_hwmon_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800260 .remove = vexpress_hwmon_remove,
Pawel Moll48ed8872012-09-17 18:40:09 +0100261 .driver = {
262 .name = DRVNAME,
263 .owner = THIS_MODULE,
264 .of_match_table = vexpress_hwmon_of_match,
265 },
266};
267
268module_platform_driver(vexpress_hwmon_driver);
269
270MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
271MODULE_DESCRIPTION("Versatile Express hwmon sensors driver");
272MODULE_LICENSE("GPL");
273MODULE_ALIAS("platform:vexpress-hwmon");