Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * System Control and Power Interface(SCPI) based hwmon sensor driver
4 *
5 * Copyright (C) 2015 ARM Ltd.
6 * Punit Agrawal <punit.agrawal@arm.com>
7 */
8
9#include <linux/hwmon.h>
10#include <linux/module.h>
11#include <linux/of_device.h>
12#include <linux/platform_device.h>
13#include <linux/scpi_protocol.h>
14#include <linux/slab.h>
15#include <linux/sysfs.h>
16#include <linux/thermal.h>
17
18struct sensor_data {
19 unsigned int scale;
20 struct scpi_sensor_info info;
21 struct device_attribute dev_attr_input;
22 struct device_attribute dev_attr_label;
23 char input[20];
24 char label[20];
25};
26
27struct scpi_thermal_zone {
28 int sensor_id;
29 struct scpi_sensors *scpi_sensors;
30};
31
32struct scpi_sensors {
33 struct scpi_ops *scpi_ops;
34 struct sensor_data *data;
35 struct list_head thermal_zones;
36 struct attribute **attrs;
37 struct attribute_group group;
38 const struct attribute_group *groups[2];
39};
40
41static const u32 gxbb_scpi_scale[] = {
42 [TEMPERATURE] = 1, /* (celsius) */
43 [VOLTAGE] = 1000, /* (millivolts) */
44 [CURRENT] = 1000, /* (milliamperes) */
45 [POWER] = 1000000, /* (microwatts) */
46 [ENERGY] = 1000000, /* (microjoules) */
47};
48
49static const u32 scpi_scale[] = {
50 [TEMPERATURE] = 1000, /* (millicelsius) */
51 [VOLTAGE] = 1000, /* (millivolts) */
52 [CURRENT] = 1000, /* (milliamperes) */
53 [POWER] = 1000000, /* (microwatts) */
54 [ENERGY] = 1000000, /* (microjoules) */
55};
56
57static void scpi_scale_reading(u64 *value, struct sensor_data *sensor)
58{
59 if (scpi_scale[sensor->info.class] != sensor->scale) {
60 *value *= scpi_scale[sensor->info.class];
61 do_div(*value, sensor->scale);
62 }
63}
64
65static int scpi_read_temp(void *dev, int *temp)
66{
67 struct scpi_thermal_zone *zone = dev;
68 struct scpi_sensors *scpi_sensors = zone->scpi_sensors;
69 struct scpi_ops *scpi_ops = scpi_sensors->scpi_ops;
70 struct sensor_data *sensor = &scpi_sensors->data[zone->sensor_id];
71 u64 value;
72 int ret;
73
74 ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value);
75 if (ret)
76 return ret;
77
78 scpi_scale_reading(&value, sensor);
79
80 *temp = value;
81 return 0;
82}
83
84/* hwmon callback functions */
85static ssize_t
86scpi_show_sensor(struct device *dev, struct device_attribute *attr, char *buf)
87{
88 struct scpi_sensors *scpi_sensors = dev_get_drvdata(dev);
89 struct scpi_ops *scpi_ops = scpi_sensors->scpi_ops;
90 struct sensor_data *sensor;
91 u64 value;
92 int ret;
93
94 sensor = container_of(attr, struct sensor_data, dev_attr_input);
95
96 ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value);
97 if (ret)
98 return ret;
99
100 scpi_scale_reading(&value, sensor);
101
102 return sprintf(buf, "%llu\n", value);
103}
104
105static ssize_t
106scpi_show_label(struct device *dev, struct device_attribute *attr, char *buf)
107{
108 struct sensor_data *sensor;
109
110 sensor = container_of(attr, struct sensor_data, dev_attr_label);
111
112 return sprintf(buf, "%s\n", sensor->info.name);
113}
114
115static const struct thermal_zone_of_device_ops scpi_sensor_ops = {
116 .get_temp = scpi_read_temp,
117};
118
119static const struct of_device_id scpi_of_match[] = {
120 {.compatible = "arm,scpi-sensors", .data = &scpi_scale},
121 {.compatible = "amlogic,meson-gxbb-scpi-sensors", .data = &gxbb_scpi_scale},
122 {},
123};
124MODULE_DEVICE_TABLE(of, scpi_of_match);
125
126static int scpi_hwmon_probe(struct platform_device *pdev)
127{
128 u16 nr_sensors, i;
129 const u32 *scale;
130 int num_temp = 0, num_volt = 0, num_current = 0, num_power = 0;
131 int num_energy = 0;
132 struct scpi_ops *scpi_ops;
133 struct device *hwdev, *dev = &pdev->dev;
134 struct scpi_sensors *scpi_sensors;
135 const struct of_device_id *of_id;
136 int idx, ret;
137
138 scpi_ops = get_scpi_ops();
139 if (!scpi_ops)
140 return -EPROBE_DEFER;
141
142 ret = scpi_ops->sensor_get_capability(&nr_sensors);
143 if (ret)
144 return ret;
145
146 if (!nr_sensors)
147 return -ENODEV;
148
149 scpi_sensors = devm_kzalloc(dev, sizeof(*scpi_sensors), GFP_KERNEL);
150 if (!scpi_sensors)
151 return -ENOMEM;
152
153 scpi_sensors->data = devm_kcalloc(dev, nr_sensors,
154 sizeof(*scpi_sensors->data), GFP_KERNEL);
155 if (!scpi_sensors->data)
156 return -ENOMEM;
157
158 scpi_sensors->attrs = devm_kcalloc(dev, (nr_sensors * 2) + 1,
159 sizeof(*scpi_sensors->attrs), GFP_KERNEL);
160 if (!scpi_sensors->attrs)
161 return -ENOMEM;
162
163 scpi_sensors->scpi_ops = scpi_ops;
164
165 of_id = of_match_device(scpi_of_match, &pdev->dev);
166 if (!of_id) {
167 dev_err(&pdev->dev, "Unable to initialize scpi-hwmon data\n");
168 return -ENODEV;
169 }
170 scale = of_id->data;
171
172 for (i = 0, idx = 0; i < nr_sensors; i++) {
173 struct sensor_data *sensor = &scpi_sensors->data[idx];
174
175 ret = scpi_ops->sensor_get_info(i, &sensor->info);
176 if (ret)
177 return ret;
178
179 switch (sensor->info.class) {
180 case TEMPERATURE:
181 snprintf(sensor->input, sizeof(sensor->input),
182 "temp%d_input", num_temp + 1);
183 snprintf(sensor->label, sizeof(sensor->input),
184 "temp%d_label", num_temp + 1);
185 num_temp++;
186 break;
187 case VOLTAGE:
188 snprintf(sensor->input, sizeof(sensor->input),
189 "in%d_input", num_volt);
190 snprintf(sensor->label, sizeof(sensor->input),
191 "in%d_label", num_volt);
192 num_volt++;
193 break;
194 case CURRENT:
195 snprintf(sensor->input, sizeof(sensor->input),
196 "curr%d_input", num_current + 1);
197 snprintf(sensor->label, sizeof(sensor->input),
198 "curr%d_label", num_current + 1);
199 num_current++;
200 break;
201 case POWER:
202 snprintf(sensor->input, sizeof(sensor->input),
203 "power%d_input", num_power + 1);
204 snprintf(sensor->label, sizeof(sensor->input),
205 "power%d_label", num_power + 1);
206 num_power++;
207 break;
208 case ENERGY:
209 snprintf(sensor->input, sizeof(sensor->input),
210 "energy%d_input", num_energy + 1);
211 snprintf(sensor->label, sizeof(sensor->input),
212 "energy%d_label", num_energy + 1);
213 num_energy++;
214 break;
215 default:
216 continue;
217 }
218
219 sensor->scale = scale[sensor->info.class];
220
221 sensor->dev_attr_input.attr.mode = 0444;
222 sensor->dev_attr_input.show = scpi_show_sensor;
223 sensor->dev_attr_input.attr.name = sensor->input;
224
225 sensor->dev_attr_label.attr.mode = 0444;
226 sensor->dev_attr_label.show = scpi_show_label;
227 sensor->dev_attr_label.attr.name = sensor->label;
228
229 scpi_sensors->attrs[idx << 1] = &sensor->dev_attr_input.attr;
230 scpi_sensors->attrs[(idx << 1) + 1] = &sensor->dev_attr_label.attr;
231
232 sysfs_attr_init(scpi_sensors->attrs[idx << 1]);
233 sysfs_attr_init(scpi_sensors->attrs[(idx << 1) + 1]);
234 idx++;
235 }
236
237 scpi_sensors->group.attrs = scpi_sensors->attrs;
238 scpi_sensors->groups[0] = &scpi_sensors->group;
239
240 platform_set_drvdata(pdev, scpi_sensors);
241
242 hwdev = devm_hwmon_device_register_with_groups(dev,
243 "scpi_sensors", scpi_sensors, scpi_sensors->groups);
244
245 if (IS_ERR(hwdev))
246 return PTR_ERR(hwdev);
247
248 /*
249 * Register the temperature sensors with the thermal framework
250 * to allow their usage in setting up the thermal zones from
251 * device tree.
252 *
253 * NOTE: Not all temperature sensors maybe used for thermal
254 * control
255 */
256 INIT_LIST_HEAD(&scpi_sensors->thermal_zones);
257 for (i = 0; i < nr_sensors; i++) {
258 struct sensor_data *sensor = &scpi_sensors->data[i];
259 struct thermal_zone_device *z;
260 struct scpi_thermal_zone *zone;
261
262 if (sensor->info.class != TEMPERATURE)
263 continue;
264
265 zone = devm_kzalloc(dev, sizeof(*zone), GFP_KERNEL);
266 if (!zone)
267 return -ENOMEM;
268
269 zone->sensor_id = i;
270 zone->scpi_sensors = scpi_sensors;
271 z = devm_thermal_zone_of_sensor_register(dev,
272 sensor->info.sensor_id,
273 zone,
274 &scpi_sensor_ops);
275 /*
276 * The call to thermal_zone_of_sensor_register returns
277 * an error for sensors that are not associated with
278 * any thermal zones or if the thermal subsystem is
279 * not configured.
280 */
281 if (IS_ERR(z))
282 devm_kfree(dev, zone);
283 }
284
285 return 0;
286}
287
288static struct platform_driver scpi_hwmon_platdrv = {
289 .driver = {
290 .name = "scpi-hwmon",
291 .of_match_table = scpi_of_match,
292 },
293 .probe = scpi_hwmon_probe,
294};
295module_platform_driver(scpi_hwmon_platdrv);
296
297MODULE_AUTHOR("Punit Agrawal <punit.agrawal@arm.com>");
298MODULE_DESCRIPTION("ARM SCPI HWMON interface driver");
299MODULE_LICENSE("GPL v2");
1/*
2 * System Control and Power Interface(SCPI) based hwmon sensor driver
3 *
4 * Copyright (C) 2015 ARM Ltd.
5 * Punit Agrawal <punit.agrawal@arm.com>
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/of_device.h>
20#include <linux/platform_device.h>
21#include <linux/scpi_protocol.h>
22#include <linux/slab.h>
23#include <linux/sysfs.h>
24#include <linux/thermal.h>
25
26struct sensor_data {
27 unsigned int scale;
28 struct scpi_sensor_info info;
29 struct device_attribute dev_attr_input;
30 struct device_attribute dev_attr_label;
31 char input[20];
32 char label[20];
33};
34
35struct scpi_thermal_zone {
36 int sensor_id;
37 struct scpi_sensors *scpi_sensors;
38};
39
40struct scpi_sensors {
41 struct scpi_ops *scpi_ops;
42 struct sensor_data *data;
43 struct list_head thermal_zones;
44 struct attribute **attrs;
45 struct attribute_group group;
46 const struct attribute_group *groups[2];
47};
48
49static const u32 gxbb_scpi_scale[] = {
50 [TEMPERATURE] = 1, /* (celsius) */
51 [VOLTAGE] = 1000, /* (millivolts) */
52 [CURRENT] = 1000, /* (milliamperes) */
53 [POWER] = 1000000, /* (microwatts) */
54 [ENERGY] = 1000000, /* (microjoules) */
55};
56
57static const u32 scpi_scale[] = {
58 [TEMPERATURE] = 1000, /* (millicelsius) */
59 [VOLTAGE] = 1000, /* (millivolts) */
60 [CURRENT] = 1000, /* (milliamperes) */
61 [POWER] = 1000000, /* (microwatts) */
62 [ENERGY] = 1000000, /* (microjoules) */
63};
64
65static void scpi_scale_reading(u64 *value, struct sensor_data *sensor)
66{
67 if (scpi_scale[sensor->info.class] != sensor->scale) {
68 *value *= scpi_scale[sensor->info.class];
69 do_div(*value, sensor->scale);
70 }
71}
72
73static int scpi_read_temp(void *dev, int *temp)
74{
75 struct scpi_thermal_zone *zone = dev;
76 struct scpi_sensors *scpi_sensors = zone->scpi_sensors;
77 struct scpi_ops *scpi_ops = scpi_sensors->scpi_ops;
78 struct sensor_data *sensor = &scpi_sensors->data[zone->sensor_id];
79 u64 value;
80 int ret;
81
82 ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value);
83 if (ret)
84 return ret;
85
86 scpi_scale_reading(&value, sensor);
87
88 *temp = value;
89 return 0;
90}
91
92/* hwmon callback functions */
93static ssize_t
94scpi_show_sensor(struct device *dev, struct device_attribute *attr, char *buf)
95{
96 struct scpi_sensors *scpi_sensors = dev_get_drvdata(dev);
97 struct scpi_ops *scpi_ops = scpi_sensors->scpi_ops;
98 struct sensor_data *sensor;
99 u64 value;
100 int ret;
101
102 sensor = container_of(attr, struct sensor_data, dev_attr_input);
103
104 ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value);
105 if (ret)
106 return ret;
107
108 scpi_scale_reading(&value, sensor);
109
110 return sprintf(buf, "%llu\n", value);
111}
112
113static ssize_t
114scpi_show_label(struct device *dev, struct device_attribute *attr, char *buf)
115{
116 struct sensor_data *sensor;
117
118 sensor = container_of(attr, struct sensor_data, dev_attr_label);
119
120 return sprintf(buf, "%s\n", sensor->info.name);
121}
122
123static const struct thermal_zone_of_device_ops scpi_sensor_ops = {
124 .get_temp = scpi_read_temp,
125};
126
127static const struct of_device_id scpi_of_match[] = {
128 {.compatible = "arm,scpi-sensors", .data = &scpi_scale},
129 {.compatible = "amlogic,meson-gxbb-scpi-sensors", .data = &gxbb_scpi_scale},
130 {},
131};
132MODULE_DEVICE_TABLE(of, scpi_of_match);
133
134static int scpi_hwmon_probe(struct platform_device *pdev)
135{
136 u16 nr_sensors, i;
137 const u32 *scale;
138 int num_temp = 0, num_volt = 0, num_current = 0, num_power = 0;
139 int num_energy = 0;
140 struct scpi_ops *scpi_ops;
141 struct device *hwdev, *dev = &pdev->dev;
142 struct scpi_sensors *scpi_sensors;
143 const struct of_device_id *of_id;
144 int idx, ret;
145
146 scpi_ops = get_scpi_ops();
147 if (!scpi_ops)
148 return -EPROBE_DEFER;
149
150 ret = scpi_ops->sensor_get_capability(&nr_sensors);
151 if (ret)
152 return ret;
153
154 if (!nr_sensors)
155 return -ENODEV;
156
157 scpi_sensors = devm_kzalloc(dev, sizeof(*scpi_sensors), GFP_KERNEL);
158 if (!scpi_sensors)
159 return -ENOMEM;
160
161 scpi_sensors->data = devm_kcalloc(dev, nr_sensors,
162 sizeof(*scpi_sensors->data), GFP_KERNEL);
163 if (!scpi_sensors->data)
164 return -ENOMEM;
165
166 scpi_sensors->attrs = devm_kcalloc(dev, (nr_sensors * 2) + 1,
167 sizeof(*scpi_sensors->attrs), GFP_KERNEL);
168 if (!scpi_sensors->attrs)
169 return -ENOMEM;
170
171 scpi_sensors->scpi_ops = scpi_ops;
172
173 of_id = of_match_device(scpi_of_match, &pdev->dev);
174 if (!of_id) {
175 dev_err(&pdev->dev, "Unable to initialize scpi-hwmon data\n");
176 return -ENODEV;
177 }
178 scale = of_id->data;
179
180 for (i = 0, idx = 0; i < nr_sensors; i++) {
181 struct sensor_data *sensor = &scpi_sensors->data[idx];
182
183 ret = scpi_ops->sensor_get_info(i, &sensor->info);
184 if (ret)
185 return ret;
186
187 switch (sensor->info.class) {
188 case TEMPERATURE:
189 snprintf(sensor->input, sizeof(sensor->input),
190 "temp%d_input", num_temp + 1);
191 snprintf(sensor->label, sizeof(sensor->input),
192 "temp%d_label", num_temp + 1);
193 num_temp++;
194 break;
195 case VOLTAGE:
196 snprintf(sensor->input, sizeof(sensor->input),
197 "in%d_input", num_volt);
198 snprintf(sensor->label, sizeof(sensor->input),
199 "in%d_label", num_volt);
200 num_volt++;
201 break;
202 case CURRENT:
203 snprintf(sensor->input, sizeof(sensor->input),
204 "curr%d_input", num_current + 1);
205 snprintf(sensor->label, sizeof(sensor->input),
206 "curr%d_label", num_current + 1);
207 num_current++;
208 break;
209 case POWER:
210 snprintf(sensor->input, sizeof(sensor->input),
211 "power%d_input", num_power + 1);
212 snprintf(sensor->label, sizeof(sensor->input),
213 "power%d_label", num_power + 1);
214 num_power++;
215 break;
216 case ENERGY:
217 snprintf(sensor->input, sizeof(sensor->input),
218 "energy%d_input", num_energy + 1);
219 snprintf(sensor->label, sizeof(sensor->input),
220 "energy%d_label", num_energy + 1);
221 num_energy++;
222 break;
223 default:
224 continue;
225 }
226
227 sensor->scale = scale[sensor->info.class];
228
229 sensor->dev_attr_input.attr.mode = S_IRUGO;
230 sensor->dev_attr_input.show = scpi_show_sensor;
231 sensor->dev_attr_input.attr.name = sensor->input;
232
233 sensor->dev_attr_label.attr.mode = S_IRUGO;
234 sensor->dev_attr_label.show = scpi_show_label;
235 sensor->dev_attr_label.attr.name = sensor->label;
236
237 scpi_sensors->attrs[idx << 1] = &sensor->dev_attr_input.attr;
238 scpi_sensors->attrs[(idx << 1) + 1] = &sensor->dev_attr_label.attr;
239
240 sysfs_attr_init(scpi_sensors->attrs[idx << 1]);
241 sysfs_attr_init(scpi_sensors->attrs[(idx << 1) + 1]);
242 idx++;
243 }
244
245 scpi_sensors->group.attrs = scpi_sensors->attrs;
246 scpi_sensors->groups[0] = &scpi_sensors->group;
247
248 platform_set_drvdata(pdev, scpi_sensors);
249
250 hwdev = devm_hwmon_device_register_with_groups(dev,
251 "scpi_sensors", scpi_sensors, scpi_sensors->groups);
252
253 if (IS_ERR(hwdev))
254 return PTR_ERR(hwdev);
255
256 /*
257 * Register the temperature sensors with the thermal framework
258 * to allow their usage in setting up the thermal zones from
259 * device tree.
260 *
261 * NOTE: Not all temperature sensors maybe used for thermal
262 * control
263 */
264 INIT_LIST_HEAD(&scpi_sensors->thermal_zones);
265 for (i = 0; i < nr_sensors; i++) {
266 struct sensor_data *sensor = &scpi_sensors->data[i];
267 struct thermal_zone_device *z;
268 struct scpi_thermal_zone *zone;
269
270 if (sensor->info.class != TEMPERATURE)
271 continue;
272
273 zone = devm_kzalloc(dev, sizeof(*zone), GFP_KERNEL);
274 if (!zone)
275 return -ENOMEM;
276
277 zone->sensor_id = i;
278 zone->scpi_sensors = scpi_sensors;
279 z = devm_thermal_zone_of_sensor_register(dev,
280 sensor->info.sensor_id,
281 zone,
282 &scpi_sensor_ops);
283 /*
284 * The call to thermal_zone_of_sensor_register returns
285 * an error for sensors that are not associated with
286 * any thermal zones or if the thermal subsystem is
287 * not configured.
288 */
289 if (IS_ERR(z)) {
290 devm_kfree(dev, zone);
291 continue;
292 }
293 }
294
295 return 0;
296}
297
298static struct platform_driver scpi_hwmon_platdrv = {
299 .driver = {
300 .name = "scpi-hwmon",
301 .of_match_table = scpi_of_match,
302 },
303 .probe = scpi_hwmon_probe,
304};
305module_platform_driver(scpi_hwmon_platdrv);
306
307MODULE_AUTHOR("Punit Agrawal <punit.agrawal@arm.com>");
308MODULE_DESCRIPTION("ARM SCPI HWMON interface driver");
309MODULE_LICENSE("GPL v2");