blob: 0b84adb5e88ef181f188ea208066383aa87ab88d [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;
Pawel Moll3b9334a2014-04-30 16:46:29 +010029 struct regmap *reg;
Pawel Moll48ed8872012-09-17 18:40:09 +010030};
31
Pawel Moll48ed8872012-09-17 18:40:09 +010032static ssize_t vexpress_hwmon_label_show(struct device *dev,
33 struct device_attribute *dev_attr, char *buffer)
34{
35 const char *label = of_get_property(dev->of_node, "label", NULL);
36
Pawel Moll48ed8872012-09-17 18:40:09 +010037 return snprintf(buffer, PAGE_SIZE, "%s\n", label);
38}
39
40static ssize_t vexpress_hwmon_u32_show(struct device *dev,
41 struct device_attribute *dev_attr, char *buffer)
42{
43 struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
44 int err;
45 u32 value;
46
Pawel Moll3b9334a2014-04-30 16:46:29 +010047 err = regmap_read(data->reg, 0, &value);
Pawel Moll48ed8872012-09-17 18:40:09 +010048 if (err)
49 return err;
50
51 return snprintf(buffer, PAGE_SIZE, "%u\n", value /
52 to_sensor_dev_attr(dev_attr)->index);
53}
54
55static ssize_t vexpress_hwmon_u64_show(struct device *dev,
56 struct device_attribute *dev_attr, char *buffer)
57{
58 struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
59 int err;
60 u32 value_hi, value_lo;
61
Pawel Moll3b9334a2014-04-30 16:46:29 +010062 err = regmap_read(data->reg, 0, &value_lo);
Pawel Moll48ed8872012-09-17 18:40:09 +010063 if (err)
64 return err;
65
Pawel Moll3b9334a2014-04-30 16:46:29 +010066 err = regmap_read(data->reg, 1, &value_hi);
Pawel Moll48ed8872012-09-17 18:40:09 +010067 if (err)
68 return err;
69
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));
73}
74
Pawel Mollb2e54112014-04-23 18:27:05 +010075static umode_t vexpress_hwmon_attr_is_visible(struct kobject *kobj,
76 struct attribute *attr, int index)
77{
78 struct device *dev = kobj_to_dev(kobj);
79 struct device_attribute *dev_attr = container_of(attr,
80 struct device_attribute, attr);
81
82 if (dev_attr->show == vexpress_hwmon_label_show &&
83 !of_get_property(dev->of_node, "label", NULL))
84 return 0;
85
86 return attr->mode;
87}
88
Pawel Moll52feaca2014-04-23 18:27:04 +010089struct vexpress_hwmon_type {
90 const char *name;
91 const struct attribute_group **attr_groups;
92};
93
Pawel Moll48ed8872012-09-17 18:40:09 +010094#if !defined(CONFIG_REGULATOR_VEXPRESS)
Guenter Roeckfa75f742018-12-10 14:02:23 -080095static DEVICE_ATTR(in1_label, 0444, vexpress_hwmon_label_show, NULL);
96static SENSOR_DEVICE_ATTR_RO(in1_input, vexpress_hwmon_u32, 1000);
Pawel Moll78cebd02014-06-12 15:14:33 +010097static struct attribute *vexpress_hwmon_attrs_volt[] = {
98 &dev_attr_in1_label.attr,
99 &sensor_dev_attr_in1_input.dev_attr.attr,
100 NULL
101};
Pawel Moll48ed8872012-09-17 18:40:09 +0100102static struct attribute_group vexpress_hwmon_group_volt = {
Pawel Mollb2e54112014-04-23 18:27:05 +0100103 .is_visible = vexpress_hwmon_attr_is_visible,
Pawel Moll48ed8872012-09-17 18:40:09 +0100104 .attrs = vexpress_hwmon_attrs_volt,
105};
Pawel Moll52feaca2014-04-23 18:27:04 +0100106static struct vexpress_hwmon_type vexpress_hwmon_volt = {
107 .name = "vexpress_volt",
108 .attr_groups = (const struct attribute_group *[]) {
109 &vexpress_hwmon_group_volt,
110 NULL,
111 },
112};
Pawel Moll48ed8872012-09-17 18:40:09 +0100113#endif
114
Guenter Roeckfa75f742018-12-10 14:02:23 -0800115static DEVICE_ATTR(curr1_label, 0444, vexpress_hwmon_label_show, NULL);
116static SENSOR_DEVICE_ATTR_RO(curr1_input, vexpress_hwmon_u32, 1000);
Pawel Moll78cebd02014-06-12 15:14:33 +0100117static struct attribute *vexpress_hwmon_attrs_amp[] = {
118 &dev_attr_curr1_label.attr,
119 &sensor_dev_attr_curr1_input.dev_attr.attr,
120 NULL
121};
Pawel Moll48ed8872012-09-17 18:40:09 +0100122static struct attribute_group vexpress_hwmon_group_amp = {
Pawel Mollb2e54112014-04-23 18:27:05 +0100123 .is_visible = vexpress_hwmon_attr_is_visible,
Pawel Moll48ed8872012-09-17 18:40:09 +0100124 .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
Guenter Roeckfa75f742018-12-10 14:02:23 -0800134static DEVICE_ATTR(temp1_label, 0444, vexpress_hwmon_label_show, NULL);
135static SENSOR_DEVICE_ATTR_RO(temp1_input, vexpress_hwmon_u32, 1000);
Pawel Moll78cebd02014-06-12 15:14:33 +0100136static struct attribute *vexpress_hwmon_attrs_temp[] = {
137 &dev_attr_temp1_label.attr,
138 &sensor_dev_attr_temp1_input.dev_attr.attr,
139 NULL
140};
Pawel Moll48ed8872012-09-17 18:40:09 +0100141static struct attribute_group vexpress_hwmon_group_temp = {
Pawel Mollb2e54112014-04-23 18:27:05 +0100142 .is_visible = vexpress_hwmon_attr_is_visible,
Pawel Moll48ed8872012-09-17 18:40:09 +0100143 .attrs = vexpress_hwmon_attrs_temp,
144};
Pawel Moll52feaca2014-04-23 18:27:04 +0100145static struct vexpress_hwmon_type vexpress_hwmon_temp = {
146 .name = "vexpress_temp",
147 .attr_groups = (const struct attribute_group *[]) {
148 &vexpress_hwmon_group_temp,
149 NULL
150 },
151};
Pawel Moll48ed8872012-09-17 18:40:09 +0100152
Guenter Roeckfa75f742018-12-10 14:02:23 -0800153static DEVICE_ATTR(power1_label, 0444, vexpress_hwmon_label_show, NULL);
154static SENSOR_DEVICE_ATTR_RO(power1_input, vexpress_hwmon_u32, 1);
Pawel Moll78cebd02014-06-12 15:14:33 +0100155static struct attribute *vexpress_hwmon_attrs_power[] = {
156 &dev_attr_power1_label.attr,
157 &sensor_dev_attr_power1_input.dev_attr.attr,
158 NULL
159};
Pawel Moll48ed8872012-09-17 18:40:09 +0100160static struct attribute_group vexpress_hwmon_group_power = {
Pawel Mollb2e54112014-04-23 18:27:05 +0100161 .is_visible = vexpress_hwmon_attr_is_visible,
Pawel Moll48ed8872012-09-17 18:40:09 +0100162 .attrs = vexpress_hwmon_attrs_power,
163};
Pawel Moll52feaca2014-04-23 18:27:04 +0100164static struct vexpress_hwmon_type vexpress_hwmon_power = {
165 .name = "vexpress_power",
166 .attr_groups = (const struct attribute_group *[]) {
167 &vexpress_hwmon_group_power,
168 NULL
169 },
170};
Pawel Moll48ed8872012-09-17 18:40:09 +0100171
Guenter Roeckfa75f742018-12-10 14:02:23 -0800172static DEVICE_ATTR(energy1_label, 0444, vexpress_hwmon_label_show, NULL);
173static SENSOR_DEVICE_ATTR_RO(energy1_input, vexpress_hwmon_u64, 1);
Pawel Moll78cebd02014-06-12 15:14:33 +0100174static struct attribute *vexpress_hwmon_attrs_energy[] = {
175 &dev_attr_energy1_label.attr,
176 &sensor_dev_attr_energy1_input.dev_attr.attr,
177 NULL
178};
Pawel Moll48ed8872012-09-17 18:40:09 +0100179static struct attribute_group vexpress_hwmon_group_energy = {
Pawel Mollb2e54112014-04-23 18:27:05 +0100180 .is_visible = vexpress_hwmon_attr_is_visible,
Pawel Moll48ed8872012-09-17 18:40:09 +0100181 .attrs = vexpress_hwmon_attrs_energy,
182};
Pawel Moll52feaca2014-04-23 18:27:04 +0100183static struct vexpress_hwmon_type vexpress_hwmon_energy = {
184 .name = "vexpress_energy",
185 .attr_groups = (const struct attribute_group *[]) {
186 &vexpress_hwmon_group_energy,
187 NULL
188 },
189};
Pawel Moll48ed8872012-09-17 18:40:09 +0100190
Fabian Frederickd720aca2015-03-16 20:54:36 +0100191static const struct of_device_id vexpress_hwmon_of_match[] = {
Pawel Moll48ed8872012-09-17 18:40:09 +0100192#if !defined(CONFIG_REGULATOR_VEXPRESS)
193 {
194 .compatible = "arm,vexpress-volt",
Pawel Moll52feaca2014-04-23 18:27:04 +0100195 .data = &vexpress_hwmon_volt,
Pawel Moll48ed8872012-09-17 18:40:09 +0100196 },
197#endif
198 {
199 .compatible = "arm,vexpress-amp",
Pawel Moll52feaca2014-04-23 18:27:04 +0100200 .data = &vexpress_hwmon_amp,
Pawel Moll48ed8872012-09-17 18:40:09 +0100201 }, {
202 .compatible = "arm,vexpress-temp",
Pawel Moll52feaca2014-04-23 18:27:04 +0100203 .data = &vexpress_hwmon_temp,
Pawel Moll48ed8872012-09-17 18:40:09 +0100204 }, {
205 .compatible = "arm,vexpress-power",
Pawel Moll52feaca2014-04-23 18:27:04 +0100206 .data = &vexpress_hwmon_power,
Pawel Moll48ed8872012-09-17 18:40:09 +0100207 }, {
208 .compatible = "arm,vexpress-energy",
Pawel Moll52feaca2014-04-23 18:27:04 +0100209 .data = &vexpress_hwmon_energy,
Pawel Moll48ed8872012-09-17 18:40:09 +0100210 },
211 {}
212};
213MODULE_DEVICE_TABLE(of, vexpress_hwmon_of_match);
214
215static int vexpress_hwmon_probe(struct platform_device *pdev)
216{
Pawel Moll48ed8872012-09-17 18:40:09 +0100217 const struct of_device_id *match;
218 struct vexpress_hwmon_data *data;
Pawel Moll52feaca2014-04-23 18:27:04 +0100219 const struct vexpress_hwmon_type *type;
Pawel Moll48ed8872012-09-17 18:40:09 +0100220
221 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
222 if (!data)
223 return -ENOMEM;
224 platform_set_drvdata(pdev, data);
225
226 match = of_match_device(vexpress_hwmon_of_match, &pdev->dev);
227 if (!match)
228 return -ENODEV;
Pawel Moll52feaca2014-04-23 18:27:04 +0100229 type = match->data;
Pawel Moll48ed8872012-09-17 18:40:09 +0100230
Pawel Moll3b9334a2014-04-30 16:46:29 +0100231 data->reg = devm_regmap_init_vexpress_config(&pdev->dev);
232 if (IS_ERR(data->reg))
233 return PTR_ERR(data->reg);
Pawel Moll48ed8872012-09-17 18:40:09 +0100234
Pawel Moll78cebd02014-06-12 15:14:33 +0100235 data->hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev,
236 type->name, data, type->attr_groups);
Pawel Moll48ed8872012-09-17 18:40:09 +0100237
Pawel Moll78cebd02014-06-12 15:14:33 +0100238 return PTR_ERR_OR_ZERO(data->hwmon_dev);
Pawel Moll48ed8872012-09-17 18:40:09 +0100239}
240
241static struct platform_driver vexpress_hwmon_driver = {
242 .probe = vexpress_hwmon_probe,
Pawel Moll48ed8872012-09-17 18:40:09 +0100243 .driver = {
244 .name = DRVNAME,
Pawel Moll48ed8872012-09-17 18:40:09 +0100245 .of_match_table = vexpress_hwmon_of_match,
246 },
247};
248
249module_platform_driver(vexpress_hwmon_driver);
250
251MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
252MODULE_DESCRIPTION("Versatile Express hwmon sensors driver");
253MODULE_LICENSE("GPL");
254MODULE_ALIAS("platform:vexpress-hwmon");