Skip to content

Commit ea98b29

Browse files
punitagrawalsudeep-holla
authored andcommitted
hwmon: Support sensors exported via ARM SCP interface
Create a driver to add support for SoC sensors exported by the System Control Processor (SCP) via the System Control and Power Interface (SCPI). The supported sensor types is one of voltage, temperature, current, and power. The sensor labels and values provided by the SCP are exported via the hwmon sysfs interface. Signed-off-by: Punit Agrawal <[email protected]> Acked-by: Guenter Roeck <[email protected]> Cc: Sudeep Holla <[email protected]>
1 parent 38a1bdc commit ea98b29

File tree

4 files changed

+228
-0
lines changed

4 files changed

+228
-0
lines changed

Documentation/hwmon/scpi-hwmon

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Kernel driver scpi-hwmon
2+
========================
3+
4+
Supported chips:
5+
* Chips based on ARM System Control Processor Interface
6+
Addresses scanned: -
7+
Datasheet: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0922b/index.html
8+
9+
Author: Punit Agrawal <[email protected]>
10+
11+
Description
12+
-----------
13+
14+
This driver supports hardware monitoring for SoC's based on the ARM
15+
System Control Processor (SCP) implementing the System Control
16+
Processor Interface (SCPI). The following sensor types are supported
17+
by the SCP -
18+
19+
* temperature
20+
* voltage
21+
* current
22+
* power
23+
24+
The SCP interface provides an API to query the available sensors and
25+
their values which are then exported to userspace by this driver.
26+
27+
Usage Notes
28+
-----------
29+
30+
The driver relies on device tree node to indicate the presence of SCPI
31+
support in the kernel. See
32+
Documentation/devicetree/bindings/arm/arm,scpi.txt for details of the
33+
devicetree node.

drivers/hwmon/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,14 @@ config SENSORS_APPLESMC
321321
Say Y here if you have an applicable laptop and want to experience
322322
the awesome power of applesmc.
323323

324+
config SENSORS_ARM_SCPI
325+
tristate "ARM SCPI Sensors"
326+
depends on ARM_SCPI_PROTOCOL
327+
help
328+
This driver provides support for temperature, voltage, current
329+
and power sensors available on ARM Ltd's SCP based platforms. The
330+
actual number and type of sensors exported depend on the platform.
331+
324332
config SENSORS_ASB100
325333
tristate "Asus ASB100 Bach"
326334
depends on X86 && I2C

drivers/hwmon/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ obj-$(CONFIG_SENSORS_ADT7462) += adt7462.o
4444
obj-$(CONFIG_SENSORS_ADT7470) += adt7470.o
4545
obj-$(CONFIG_SENSORS_ADT7475) += adt7475.o
4646
obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o
47+
obj-$(CONFIG_SENSORS_ARM_SCPI) += scpi-hwmon.o
4748
obj-$(CONFIG_SENSORS_ASC7621) += asc7621.o
4849
obj-$(CONFIG_SENSORS_ATXP1) += atxp1.o
4950
obj-$(CONFIG_SENSORS_CORETEMP) += coretemp.o

drivers/hwmon/scpi-hwmon.c

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* System Control and Power Interface(SCPI) based hwmon sensor driver
3+
*
4+
* Copyright (C) 2015 ARM Ltd.
5+
* Punit Agrawal <[email protected]>
6+
*
7+
* This program is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License version 2 as
9+
* published by the Free Software Foundation.
10+
*
11+
* This program is distributed "as is" WITHOUT ANY WARRANTY of any
12+
* kind, whether express or implied; without even the implied warranty
13+
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*/
16+
17+
#include <linux/hwmon.h>
18+
#include <linux/module.h>
19+
#include <linux/platform_device.h>
20+
#include <linux/scpi_protocol.h>
21+
#include <linux/slab.h>
22+
#include <linux/sysfs.h>
23+
24+
struct sensor_data {
25+
struct scpi_sensor_info info;
26+
struct device_attribute dev_attr_input;
27+
struct device_attribute dev_attr_label;
28+
char input[20];
29+
char label[20];
30+
};
31+
32+
struct scpi_sensors {
33+
struct scpi_ops *scpi_ops;
34+
struct sensor_data *data;
35+
struct attribute **attrs;
36+
struct attribute_group group;
37+
const struct attribute_group *groups[2];
38+
};
39+
40+
/* hwmon callback functions */
41+
static ssize_t
42+
scpi_show_sensor(struct device *dev, struct device_attribute *attr, char *buf)
43+
{
44+
struct scpi_sensors *scpi_sensors = dev_get_drvdata(dev);
45+
struct scpi_ops *scpi_ops = scpi_sensors->scpi_ops;
46+
struct sensor_data *sensor;
47+
u32 value;
48+
int ret;
49+
50+
sensor = container_of(attr, struct sensor_data, dev_attr_input);
51+
52+
ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value);
53+
if (ret)
54+
return ret;
55+
56+
return sprintf(buf, "%u\n", value);
57+
}
58+
59+
static ssize_t
60+
scpi_show_label(struct device *dev, struct device_attribute *attr, char *buf)
61+
{
62+
struct sensor_data *sensor;
63+
64+
sensor = container_of(attr, struct sensor_data, dev_attr_label);
65+
66+
return sprintf(buf, "%s\n", sensor->info.name);
67+
}
68+
69+
static int scpi_hwmon_probe(struct platform_device *pdev)
70+
{
71+
u16 nr_sensors, i;
72+
int num_temp = 0, num_volt = 0, num_current = 0, num_power = 0;
73+
struct scpi_ops *scpi_ops;
74+
struct device *hwdev, *dev = &pdev->dev;
75+
struct scpi_sensors *scpi_sensors;
76+
int ret;
77+
78+
scpi_ops = get_scpi_ops();
79+
if (!scpi_ops)
80+
return -EPROBE_DEFER;
81+
82+
ret = scpi_ops->sensor_get_capability(&nr_sensors);
83+
if (ret)
84+
return ret;
85+
86+
if (!nr_sensors)
87+
return -ENODEV;
88+
89+
scpi_sensors = devm_kzalloc(dev, sizeof(*scpi_sensors), GFP_KERNEL);
90+
if (!scpi_sensors)
91+
return -ENOMEM;
92+
93+
scpi_sensors->data = devm_kcalloc(dev, nr_sensors,
94+
sizeof(*scpi_sensors->data), GFP_KERNEL);
95+
if (!scpi_sensors->data)
96+
return -ENOMEM;
97+
98+
scpi_sensors->attrs = devm_kcalloc(dev, (nr_sensors * 2) + 1,
99+
sizeof(*scpi_sensors->attrs), GFP_KERNEL);
100+
if (!scpi_sensors->attrs)
101+
return -ENOMEM;
102+
103+
scpi_sensors->scpi_ops = scpi_ops;
104+
105+
for (i = 0; i < nr_sensors; i++) {
106+
struct sensor_data *sensor = &scpi_sensors->data[i];
107+
108+
ret = scpi_ops->sensor_get_info(i, &sensor->info);
109+
if (ret)
110+
return ret;
111+
112+
switch (sensor->info.class) {
113+
case TEMPERATURE:
114+
snprintf(sensor->input, sizeof(sensor->input),
115+
"temp%d_input", num_temp + 1);
116+
snprintf(sensor->label, sizeof(sensor->input),
117+
"temp%d_label", num_temp + 1);
118+
num_temp++;
119+
break;
120+
case VOLTAGE:
121+
snprintf(sensor->input, sizeof(sensor->input),
122+
"in%d_input", num_volt);
123+
snprintf(sensor->label, sizeof(sensor->input),
124+
"in%d_label", num_volt);
125+
num_volt++;
126+
break;
127+
case CURRENT:
128+
snprintf(sensor->input, sizeof(sensor->input),
129+
"curr%d_input", num_current + 1);
130+
snprintf(sensor->label, sizeof(sensor->input),
131+
"curr%d_label", num_current + 1);
132+
num_current++;
133+
break;
134+
case POWER:
135+
snprintf(sensor->input, sizeof(sensor->input),
136+
"power%d_input", num_power + 1);
137+
snprintf(sensor->label, sizeof(sensor->input),
138+
"power%d_label", num_power + 1);
139+
num_power++;
140+
break;
141+
default:
142+
break;
143+
}
144+
145+
sensor->dev_attr_input.attr.mode = S_IRUGO;
146+
sensor->dev_attr_input.show = scpi_show_sensor;
147+
sensor->dev_attr_input.attr.name = sensor->input;
148+
149+
sensor->dev_attr_label.attr.mode = S_IRUGO;
150+
sensor->dev_attr_label.show = scpi_show_label;
151+
sensor->dev_attr_label.attr.name = sensor->label;
152+
153+
scpi_sensors->attrs[i << 1] = &sensor->dev_attr_input.attr;
154+
scpi_sensors->attrs[(i << 1) + 1] = &sensor->dev_attr_label.attr;
155+
156+
sysfs_attr_init(scpi_sensors->attrs[i << 1]);
157+
sysfs_attr_init(scpi_sensors->attrs[(i << 1) + 1]);
158+
}
159+
160+
scpi_sensors->group.attrs = scpi_sensors->attrs;
161+
scpi_sensors->groups[0] = &scpi_sensors->group;
162+
163+
hwdev = devm_hwmon_device_register_with_groups(dev,
164+
"scpi_sensors", scpi_sensors, scpi_sensors->groups);
165+
166+
return PTR_ERR_OR_ZERO(hwdev);
167+
}
168+
169+
static const struct of_device_id scpi_of_match[] = {
170+
{.compatible = "arm,scpi-sensors"},
171+
{},
172+
};
173+
174+
static struct platform_driver scpi_hwmon_platdrv = {
175+
.driver = {
176+
.name = "scpi-hwmon",
177+
.owner = THIS_MODULE,
178+
.of_match_table = scpi_of_match,
179+
},
180+
.probe = scpi_hwmon_probe,
181+
};
182+
module_platform_driver(scpi_hwmon_platdrv);
183+
184+
MODULE_AUTHOR("Punit Agrawal <[email protected]>");
185+
MODULE_DESCRIPTION("ARM SCPI HWMON interface driver");
186+
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)