GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / hwmon / scpi-hwmon.c
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
26 struct 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
35 struct scpi_thermal_zone {
36         int sensor_id;
37         struct scpi_sensors *scpi_sensors;
38 };
39
40 struct 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
49 static 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
57 static 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
65 static 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
73 static 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 */
93 static ssize_t
94 scpi_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         /*
111          * Temperature sensor values are treated as signed values based on
112          * observation even though that is not explicitly specified, and
113          * because an unsigned u64 temperature does not really make practical
114          * sense especially when the temperature is below zero degrees Celsius.
115          */
116         if (sensor->info.class == TEMPERATURE)
117                 return sprintf(buf, "%lld\n", (s64)value);
118
119         return sprintf(buf, "%llu\n", value);
120 }
121
122 static ssize_t
123 scpi_show_label(struct device *dev, struct device_attribute *attr, char *buf)
124 {
125         struct sensor_data *sensor;
126
127         sensor = container_of(attr, struct sensor_data, dev_attr_label);
128
129         return sprintf(buf, "%s\n", sensor->info.name);
130 }
131
132 static const struct thermal_zone_of_device_ops scpi_sensor_ops = {
133         .get_temp = scpi_read_temp,
134 };
135
136 static const struct of_device_id scpi_of_match[] = {
137         {.compatible = "arm,scpi-sensors", .data = &scpi_scale},
138         {.compatible = "amlogic,meson-gxbb-scpi-sensors", .data = &gxbb_scpi_scale},
139         {},
140 };
141 MODULE_DEVICE_TABLE(of, scpi_of_match);
142
143 static int scpi_hwmon_probe(struct platform_device *pdev)
144 {
145         u16 nr_sensors, i;
146         const u32 *scale;
147         int num_temp = 0, num_volt = 0, num_current = 0, num_power = 0;
148         int num_energy = 0;
149         struct scpi_ops *scpi_ops;
150         struct device *hwdev, *dev = &pdev->dev;
151         struct scpi_sensors *scpi_sensors;
152         const struct of_device_id *of_id;
153         int idx, ret;
154
155         scpi_ops = get_scpi_ops();
156         if (!scpi_ops)
157                 return -EPROBE_DEFER;
158
159         ret = scpi_ops->sensor_get_capability(&nr_sensors);
160         if (ret)
161                 return ret;
162
163         if (!nr_sensors)
164                 return -ENODEV;
165
166         scpi_sensors = devm_kzalloc(dev, sizeof(*scpi_sensors), GFP_KERNEL);
167         if (!scpi_sensors)
168                 return -ENOMEM;
169
170         scpi_sensors->data = devm_kcalloc(dev, nr_sensors,
171                                    sizeof(*scpi_sensors->data), GFP_KERNEL);
172         if (!scpi_sensors->data)
173                 return -ENOMEM;
174
175         scpi_sensors->attrs = devm_kcalloc(dev, (nr_sensors * 2) + 1,
176                                    sizeof(*scpi_sensors->attrs), GFP_KERNEL);
177         if (!scpi_sensors->attrs)
178                 return -ENOMEM;
179
180         scpi_sensors->scpi_ops = scpi_ops;
181
182         of_id = of_match_device(scpi_of_match, &pdev->dev);
183         if (!of_id) {
184                 dev_err(&pdev->dev, "Unable to initialize scpi-hwmon data\n");
185                 return -ENODEV;
186         }
187         scale = of_id->data;
188
189         for (i = 0, idx = 0; i < nr_sensors; i++) {
190                 struct sensor_data *sensor = &scpi_sensors->data[idx];
191
192                 ret = scpi_ops->sensor_get_info(i, &sensor->info);
193                 if (ret)
194                         return ret;
195
196                 switch (sensor->info.class) {
197                 case TEMPERATURE:
198                         snprintf(sensor->input, sizeof(sensor->input),
199                                  "temp%d_input", num_temp + 1);
200                         snprintf(sensor->label, sizeof(sensor->input),
201                                  "temp%d_label", num_temp + 1);
202                         num_temp++;
203                         break;
204                 case VOLTAGE:
205                         snprintf(sensor->input, sizeof(sensor->input),
206                                  "in%d_input", num_volt);
207                         snprintf(sensor->label, sizeof(sensor->input),
208                                  "in%d_label", num_volt);
209                         num_volt++;
210                         break;
211                 case CURRENT:
212                         snprintf(sensor->input, sizeof(sensor->input),
213                                  "curr%d_input", num_current + 1);
214                         snprintf(sensor->label, sizeof(sensor->input),
215                                  "curr%d_label", num_current + 1);
216                         num_current++;
217                         break;
218                 case POWER:
219                         snprintf(sensor->input, sizeof(sensor->input),
220                                  "power%d_input", num_power + 1);
221                         snprintf(sensor->label, sizeof(sensor->input),
222                                  "power%d_label", num_power + 1);
223                         num_power++;
224                         break;
225                 case ENERGY:
226                         snprintf(sensor->input, sizeof(sensor->input),
227                                  "energy%d_input", num_energy + 1);
228                         snprintf(sensor->label, sizeof(sensor->input),
229                                  "energy%d_label", num_energy + 1);
230                         num_energy++;
231                         break;
232                 default:
233                         continue;
234                 }
235
236                 sensor->scale = scale[sensor->info.class];
237
238                 sensor->dev_attr_input.attr.mode = S_IRUGO;
239                 sensor->dev_attr_input.show = scpi_show_sensor;
240                 sensor->dev_attr_input.attr.name = sensor->input;
241
242                 sensor->dev_attr_label.attr.mode = S_IRUGO;
243                 sensor->dev_attr_label.show = scpi_show_label;
244                 sensor->dev_attr_label.attr.name = sensor->label;
245
246                 scpi_sensors->attrs[idx << 1] = &sensor->dev_attr_input.attr;
247                 scpi_sensors->attrs[(idx << 1) + 1] = &sensor->dev_attr_label.attr;
248
249                 sysfs_attr_init(scpi_sensors->attrs[idx << 1]);
250                 sysfs_attr_init(scpi_sensors->attrs[(idx << 1) + 1]);
251                 idx++;
252         }
253
254         scpi_sensors->group.attrs = scpi_sensors->attrs;
255         scpi_sensors->groups[0] = &scpi_sensors->group;
256
257         platform_set_drvdata(pdev, scpi_sensors);
258
259         hwdev = devm_hwmon_device_register_with_groups(dev,
260                         "scpi_sensors", scpi_sensors, scpi_sensors->groups);
261
262         if (IS_ERR(hwdev))
263                 return PTR_ERR(hwdev);
264
265         /*
266          * Register the temperature sensors with the thermal framework
267          * to allow their usage in setting up the thermal zones from
268          * device tree.
269          *
270          * NOTE: Not all temperature sensors maybe used for thermal
271          * control
272          */
273         INIT_LIST_HEAD(&scpi_sensors->thermal_zones);
274         for (i = 0; i < nr_sensors; i++) {
275                 struct sensor_data *sensor = &scpi_sensors->data[i];
276                 struct thermal_zone_device *z;
277                 struct scpi_thermal_zone *zone;
278
279                 if (sensor->info.class != TEMPERATURE)
280                         continue;
281
282                 zone = devm_kzalloc(dev, sizeof(*zone), GFP_KERNEL);
283                 if (!zone)
284                         return -ENOMEM;
285
286                 zone->sensor_id = i;
287                 zone->scpi_sensors = scpi_sensors;
288                 z = devm_thermal_zone_of_sensor_register(dev,
289                                                          sensor->info.sensor_id,
290                                                          zone,
291                                                          &scpi_sensor_ops);
292                 /*
293                  * The call to thermal_zone_of_sensor_register returns
294                  * an error for sensors that are not associated with
295                  * any thermal zones or if the thermal subsystem is
296                  * not configured.
297                  */
298                 if (IS_ERR(z)) {
299                         devm_kfree(dev, zone);
300                         continue;
301                 }
302         }
303
304         return 0;
305 }
306
307 static struct platform_driver scpi_hwmon_platdrv = {
308         .driver = {
309                 .name   = "scpi-hwmon",
310                 .of_match_table = scpi_of_match,
311         },
312         .probe          = scpi_hwmon_probe,
313 };
314 module_platform_driver(scpi_hwmon_platdrv);
315
316 MODULE_AUTHOR("Punit Agrawal <punit.agrawal@arm.com>");
317 MODULE_DESCRIPTION("ARM SCPI HWMON interface driver");
318 MODULE_LICENSE("GPL v2");