blob: 7e49da50bc69b38b8758984c52298cc4db26c311 [file] [log] [blame]
Punit Agrawalea98b292015-05-21 15:08:45 +01001/*
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>
Carlo Caioneccc9b822017-05-30 11:05:07 +020019#include <linux/of_device.h>
Punit Agrawalea98b292015-05-21 15:08:45 +010020#include <linux/platform_device.h>
21#include <linux/scpi_protocol.h>
22#include <linux/slab.h>
23#include <linux/sysfs.h>
Punit Agrawal68acc772015-06-24 19:03:16 +010024#include <linux/thermal.h>
Punit Agrawalea98b292015-05-21 15:08:45 +010025
26struct sensor_data {
Carlo Caioneccc9b822017-05-30 11:05:07 +020027 unsigned int scale;
Punit Agrawalea98b292015-05-21 15:08:45 +010028 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
Punit Agrawal68acc772015-06-24 19:03:16 +010035struct scpi_thermal_zone {
Punit Agrawal68acc772015-06-24 19:03:16 +010036 int sensor_id;
37 struct scpi_sensors *scpi_sensors;
Punit Agrawal68acc772015-06-24 19:03:16 +010038};
39
Punit Agrawalea98b292015-05-21 15:08:45 +010040struct scpi_sensors {
41 struct scpi_ops *scpi_ops;
42 struct sensor_data *data;
Punit Agrawal68acc772015-06-24 19:03:16 +010043 struct list_head thermal_zones;
Punit Agrawalea98b292015-05-21 15:08:45 +010044 struct attribute **attrs;
45 struct attribute_group group;
46 const struct attribute_group *groups[2];
47};
48
Carlo Caioneccc9b822017-05-30 11:05:07 +020049static 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
Punit Agrawal68acc772015-06-24 19:03:16 +010073static 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];
Sudeep Holla2e874152016-01-14 17:58:02 +000079 u64 value;
Punit Agrawal68acc772015-06-24 19:03:16 +010080 int ret;
81
82 ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value);
83 if (ret)
84 return ret;
85
Carlo Caioneccc9b822017-05-30 11:05:07 +020086 scpi_scale_reading(&value, sensor);
87
Punit Agrawal68acc772015-06-24 19:03:16 +010088 *temp = value;
89 return 0;
90}
91
Punit Agrawalea98b292015-05-21 15:08:45 +010092/* 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;
Sudeep Holla2e874152016-01-14 17:58:02 +000099 u64 value;
Punit Agrawalea98b292015-05-21 15:08:45 +0100100 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
Carlo Caioneccc9b822017-05-30 11:05:07 +0200108 scpi_scale_reading(&value, sensor);
109
Sudeep Holla2e874152016-01-14 17:58:02 +0000110 return sprintf(buf, "%llu\n", value);
Punit Agrawalea98b292015-05-21 15:08:45 +0100111}
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
Julia Lawall778fb962017-08-08 17:09:03 +0200123static const struct thermal_zone_of_device_ops scpi_sensor_ops = {
Punit Agrawal68acc772015-06-24 19:03:16 +0100124 .get_temp = scpi_read_temp,
125};
126
Carlo Caioneccc9b822017-05-30 11:05:07 +0200127static 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
Punit Agrawalea98b292015-05-21 15:08:45 +0100134static int scpi_hwmon_probe(struct platform_device *pdev)
135{
136 u16 nr_sensors, i;
Carlo Caioneccc9b822017-05-30 11:05:07 +0200137 const u32 *scale;
Punit Agrawalea98b292015-05-21 15:08:45 +0100138 int num_temp = 0, num_volt = 0, num_current = 0, num_power = 0;
Sudeep Hollafb3b07e2016-01-25 10:53:38 +0000139 int num_energy = 0;
Punit Agrawalea98b292015-05-21 15:08:45 +0100140 struct scpi_ops *scpi_ops;
141 struct device *hwdev, *dev = &pdev->dev;
142 struct scpi_sensors *scpi_sensors;
Carlo Caioneccc9b822017-05-30 11:05:07 +0200143 const struct of_device_id *of_id;
Eduardo Valentind7817ff2016-03-09 13:03:17 -0800144 int idx, ret;
Punit Agrawalea98b292015-05-21 15:08:45 +0100145
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
Carlo Caioneccc9b822017-05-30 11:05:07 +0200173 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
Sudeep Hollacef03d72015-10-28 17:17:31 +0000180 for (i = 0, idx = 0; i < nr_sensors; i++) {
181 struct sensor_data *sensor = &scpi_sensors->data[idx];
Punit Agrawalea98b292015-05-21 15:08:45 +0100182
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;
Sudeep Hollafb3b07e2016-01-25 10:53:38 +0000216 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;
Punit Agrawalea98b292015-05-21 15:08:45 +0100223 default:
Sudeep Hollacef03d72015-10-28 17:17:31 +0000224 continue;
Punit Agrawalea98b292015-05-21 15:08:45 +0100225 }
226
Carlo Caioneccc9b822017-05-30 11:05:07 +0200227 sensor->scale = scale[sensor->info.class];
228
Punit Agrawalea98b292015-05-21 15:08:45 +0100229 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
Sudeep Hollacef03d72015-10-28 17:17:31 +0000237 scpi_sensors->attrs[idx << 1] = &sensor->dev_attr_input.attr;
238 scpi_sensors->attrs[(idx << 1) + 1] = &sensor->dev_attr_label.attr;
Punit Agrawalea98b292015-05-21 15:08:45 +0100239
Sudeep Hollacef03d72015-10-28 17:17:31 +0000240 sysfs_attr_init(scpi_sensors->attrs[idx << 1]);
241 sysfs_attr_init(scpi_sensors->attrs[(idx << 1) + 1]);
242 idx++;
Punit Agrawalea98b292015-05-21 15:08:45 +0100243 }
244
245 scpi_sensors->group.attrs = scpi_sensors->attrs;
246 scpi_sensors->groups[0] = &scpi_sensors->group;
247
Punit Agrawal68acc772015-06-24 19:03:16 +0100248 platform_set_drvdata(pdev, scpi_sensors);
249
Punit Agrawalea98b292015-05-21 15:08:45 +0100250 hwdev = devm_hwmon_device_register_with_groups(dev,
251 "scpi_sensors", scpi_sensors, scpi_sensors->groups);
252
Punit Agrawal68acc772015-06-24 19:03:16 +0100253 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];
Eduardo Valentind7817ff2016-03-09 13:03:17 -0800267 struct thermal_zone_device *z;
Punit Agrawal68acc772015-06-24 19:03:16 +0100268 struct scpi_thermal_zone *zone;
269
270 if (sensor->info.class != TEMPERATURE)
271 continue;
272
273 zone = devm_kzalloc(dev, sizeof(*zone), GFP_KERNEL);
Eduardo Valentind7817ff2016-03-09 13:03:17 -0800274 if (!zone)
275 return -ENOMEM;
Punit Agrawal68acc772015-06-24 19:03:16 +0100276
277 zone->sensor_id = i;
278 zone->scpi_sensors = scpi_sensors;
Eduardo Valentind7817ff2016-03-09 13:03:17 -0800279 z = devm_thermal_zone_of_sensor_register(dev,
280 sensor->info.sensor_id,
281 zone,
282 &scpi_sensor_ops);
Punit Agrawal68acc772015-06-24 19:03:16 +0100283 /*
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 */
Eduardo Valentind7817ff2016-03-09 13:03:17 -0800289 if (IS_ERR(z)) {
Punit Agrawal68acc772015-06-24 19:03:16 +0100290 devm_kfree(dev, zone);
291 continue;
292 }
Punit Agrawal68acc772015-06-24 19:03:16 +0100293 }
294
295 return 0;
Punit Agrawalea98b292015-05-21 15:08:45 +0100296}
297
Punit Agrawalea98b292015-05-21 15:08:45 +0100298static struct platform_driver scpi_hwmon_platdrv = {
299 .driver = {
300 .name = "scpi-hwmon",
Punit Agrawalea98b292015-05-21 15:08:45 +0100301 .of_match_table = scpi_of_match,
302 },
303 .probe = scpi_hwmon_probe,
Punit Agrawalea98b292015-05-21 15:08:45 +0100304};
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");