Commit | Line | Data |
---|---|---|
23297edb | 1 | // SPDX-License-Identifier: GPL-2.0 |
ea98b29a PA |
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> | |
ea98b29a PA |
7 | */ |
8 | ||
9 | #include <linux/hwmon.h> | |
10 | #include <linux/module.h> | |
39f03438 | 11 | #include <linux/of.h> |
ea98b29a PA |
12 | #include <linux/platform_device.h> |
13 | #include <linux/scpi_protocol.h> | |
14 | #include <linux/slab.h> | |
15 | #include <linux/sysfs.h> | |
68acc77a | 16 | #include <linux/thermal.h> |
ea98b29a PA |
17 | |
18 | struct sensor_data { | |
ccc9b826 | 19 | unsigned int scale; |
ea98b29a PA |
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 | ||
68acc77a | 27 | struct scpi_thermal_zone { |
68acc77a PA |
28 | int sensor_id; |
29 | struct scpi_sensors *scpi_sensors; | |
68acc77a PA |
30 | }; |
31 | ||
ea98b29a PA |
32 | struct scpi_sensors { |
33 | struct scpi_ops *scpi_ops; | |
34 | struct sensor_data *data; | |
68acc77a | 35 | struct list_head thermal_zones; |
ea98b29a PA |
36 | struct attribute **attrs; |
37 | struct attribute_group group; | |
38 | const struct attribute_group *groups[2]; | |
39 | }; | |
40 | ||
ccc9b826 CC |
41 | static 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 | ||
49 | static 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 | ||
57 | static 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 | ||
e5181331 | 65 | static int scpi_read_temp(struct thermal_zone_device *tz, int *temp) |
68acc77a | 66 | { |
0ce637a5 | 67 | struct scpi_thermal_zone *zone = thermal_zone_device_priv(tz); |
68acc77a PA |
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]; | |
2e874159 | 71 | u64 value; |
68acc77a PA |
72 | int ret; |
73 | ||
74 | ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value); | |
75 | if (ret) | |
76 | return ret; | |
77 | ||
ccc9b826 CC |
78 | scpi_scale_reading(&value, sensor); |
79 | ||
68acc77a PA |
80 | *temp = value; |
81 | return 0; | |
82 | } | |
83 | ||
ea98b29a PA |
84 | /* hwmon callback functions */ |
85 | static ssize_t | |
86 | scpi_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; | |
2e874159 | 91 | u64 value; |
ea98b29a PA |
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 | ||
ccc9b826 CC |
100 | scpi_scale_reading(&value, sensor); |
101 | ||
78d13552 RL |
102 | /* |
103 | * Temperature sensor values are treated as signed values based on | |
104 | * observation even though that is not explicitly specified, and | |
105 | * because an unsigned u64 temperature does not really make practical | |
106 | * sense especially when the temperature is below zero degrees Celsius. | |
107 | */ | |
108 | if (sensor->info.class == TEMPERATURE) | |
109 | return sprintf(buf, "%lld\n", (s64)value); | |
110 | ||
2e874159 | 111 | return sprintf(buf, "%llu\n", value); |
ea98b29a PA |
112 | } |
113 | ||
114 | static ssize_t | |
115 | scpi_show_label(struct device *dev, struct device_attribute *attr, char *buf) | |
116 | { | |
117 | struct sensor_data *sensor; | |
118 | ||
119 | sensor = container_of(attr, struct sensor_data, dev_attr_label); | |
120 | ||
121 | return sprintf(buf, "%s\n", sensor->info.name); | |
122 | } | |
123 | ||
e5181331 | 124 | static const struct thermal_zone_device_ops scpi_sensor_ops = { |
68acc77a PA |
125 | .get_temp = scpi_read_temp, |
126 | }; | |
127 | ||
ccc9b826 CC |
128 | static const struct of_device_id scpi_of_match[] = { |
129 | {.compatible = "arm,scpi-sensors", .data = &scpi_scale}, | |
130 | {.compatible = "amlogic,meson-gxbb-scpi-sensors", .data = &gxbb_scpi_scale}, | |
131 | {}, | |
132 | }; | |
133 | MODULE_DEVICE_TABLE(of, scpi_of_match); | |
134 | ||
ea98b29a PA |
135 | static int scpi_hwmon_probe(struct platform_device *pdev) |
136 | { | |
137 | u16 nr_sensors, i; | |
ccc9b826 | 138 | const u32 *scale; |
ea98b29a | 139 | int num_temp = 0, num_volt = 0, num_current = 0, num_power = 0; |
fb3b07ef | 140 | int num_energy = 0; |
ea98b29a PA |
141 | struct scpi_ops *scpi_ops; |
142 | struct device *hwdev, *dev = &pdev->dev; | |
143 | struct scpi_sensors *scpi_sensors; | |
d7817ff2 | 144 | int idx, ret; |
ea98b29a PA |
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 | ||
fd6ca3f5 MC |
173 | scale = of_device_get_match_data(&pdev->dev); |
174 | if (!scale) { | |
ccc9b826 CC |
175 | dev_err(&pdev->dev, "Unable to initialize scpi-hwmon data\n"); |
176 | return -ENODEV; | |
177 | } | |
ccc9b826 | 178 | |
cef03d7e SH |
179 | for (i = 0, idx = 0; i < nr_sensors; i++) { |
180 | struct sensor_data *sensor = &scpi_sensors->data[idx]; | |
ea98b29a PA |
181 | |
182 | ret = scpi_ops->sensor_get_info(i, &sensor->info); | |
183 | if (ret) | |
184 | return ret; | |
185 | ||
186 | switch (sensor->info.class) { | |
187 | case TEMPERATURE: | |
188 | snprintf(sensor->input, sizeof(sensor->input), | |
189 | "temp%d_input", num_temp + 1); | |
190 | snprintf(sensor->label, sizeof(sensor->input), | |
191 | "temp%d_label", num_temp + 1); | |
192 | num_temp++; | |
193 | break; | |
194 | case VOLTAGE: | |
195 | snprintf(sensor->input, sizeof(sensor->input), | |
196 | "in%d_input", num_volt); | |
197 | snprintf(sensor->label, sizeof(sensor->input), | |
198 | "in%d_label", num_volt); | |
199 | num_volt++; | |
200 | break; | |
201 | case CURRENT: | |
202 | snprintf(sensor->input, sizeof(sensor->input), | |
203 | "curr%d_input", num_current + 1); | |
204 | snprintf(sensor->label, sizeof(sensor->input), | |
205 | "curr%d_label", num_current + 1); | |
206 | num_current++; | |
207 | break; | |
208 | case POWER: | |
209 | snprintf(sensor->input, sizeof(sensor->input), | |
210 | "power%d_input", num_power + 1); | |
211 | snprintf(sensor->label, sizeof(sensor->input), | |
212 | "power%d_label", num_power + 1); | |
213 | num_power++; | |
214 | break; | |
fb3b07ef SH |
215 | case ENERGY: |
216 | snprintf(sensor->input, sizeof(sensor->input), | |
217 | "energy%d_input", num_energy + 1); | |
218 | snprintf(sensor->label, sizeof(sensor->input), | |
219 | "energy%d_label", num_energy + 1); | |
220 | num_energy++; | |
221 | break; | |
ea98b29a | 222 | default: |
cef03d7e | 223 | continue; |
ea98b29a PA |
224 | } |
225 | ||
ccc9b826 CC |
226 | sensor->scale = scale[sensor->info.class]; |
227 | ||
6a0785aa | 228 | sensor->dev_attr_input.attr.mode = 0444; |
ea98b29a PA |
229 | sensor->dev_attr_input.show = scpi_show_sensor; |
230 | sensor->dev_attr_input.attr.name = sensor->input; | |
231 | ||
6a0785aa | 232 | sensor->dev_attr_label.attr.mode = 0444; |
ea98b29a PA |
233 | sensor->dev_attr_label.show = scpi_show_label; |
234 | sensor->dev_attr_label.attr.name = sensor->label; | |
235 | ||
cef03d7e SH |
236 | scpi_sensors->attrs[idx << 1] = &sensor->dev_attr_input.attr; |
237 | scpi_sensors->attrs[(idx << 1) + 1] = &sensor->dev_attr_label.attr; | |
ea98b29a | 238 | |
cef03d7e SH |
239 | sysfs_attr_init(scpi_sensors->attrs[idx << 1]); |
240 | sysfs_attr_init(scpi_sensors->attrs[(idx << 1) + 1]); | |
241 | idx++; | |
ea98b29a PA |
242 | } |
243 | ||
244 | scpi_sensors->group.attrs = scpi_sensors->attrs; | |
245 | scpi_sensors->groups[0] = &scpi_sensors->group; | |
246 | ||
68acc77a PA |
247 | platform_set_drvdata(pdev, scpi_sensors); |
248 | ||
ea98b29a PA |
249 | hwdev = devm_hwmon_device_register_with_groups(dev, |
250 | "scpi_sensors", scpi_sensors, scpi_sensors->groups); | |
251 | ||
68acc77a PA |
252 | if (IS_ERR(hwdev)) |
253 | return PTR_ERR(hwdev); | |
254 | ||
255 | /* | |
256 | * Register the temperature sensors with the thermal framework | |
257 | * to allow their usage in setting up the thermal zones from | |
258 | * device tree. | |
259 | * | |
260 | * NOTE: Not all temperature sensors maybe used for thermal | |
261 | * control | |
262 | */ | |
263 | INIT_LIST_HEAD(&scpi_sensors->thermal_zones); | |
264 | for (i = 0; i < nr_sensors; i++) { | |
265 | struct sensor_data *sensor = &scpi_sensors->data[i]; | |
d7817ff2 | 266 | struct thermal_zone_device *z; |
68acc77a PA |
267 | struct scpi_thermal_zone *zone; |
268 | ||
269 | if (sensor->info.class != TEMPERATURE) | |
270 | continue; | |
271 | ||
272 | zone = devm_kzalloc(dev, sizeof(*zone), GFP_KERNEL); | |
d7817ff2 EV |
273 | if (!zone) |
274 | return -ENOMEM; | |
68acc77a PA |
275 | |
276 | zone->sensor_id = i; | |
277 | zone->scpi_sensors = scpi_sensors; | |
e5181331 DL |
278 | z = devm_thermal_of_zone_register(dev, |
279 | sensor->info.sensor_id, | |
280 | zone, | |
281 | &scpi_sensor_ops); | |
68acc77a PA |
282 | /* |
283 | * The call to thermal_zone_of_sensor_register returns | |
284 | * an error for sensors that are not associated with | |
285 | * any thermal zones or if the thermal subsystem is | |
286 | * not configured. | |
287 | */ | |
3045b5d6 | 288 | if (IS_ERR(z)) |
68acc77a | 289 | devm_kfree(dev, zone); |
68acc77a PA |
290 | } |
291 | ||
68acc77a | 292 | return 0; |
ea98b29a PA |
293 | } |
294 | ||
ea98b29a PA |
295 | static struct platform_driver scpi_hwmon_platdrv = { |
296 | .driver = { | |
297 | .name = "scpi-hwmon", | |
ea98b29a PA |
298 | .of_match_table = scpi_of_match, |
299 | }, | |
300 | .probe = scpi_hwmon_probe, | |
301 | }; | |
302 | module_platform_driver(scpi_hwmon_platdrv); | |
303 | ||
304 | MODULE_AUTHOR("Punit Agrawal <punit.agrawal@arm.com>"); | |
305 | MODULE_DESCRIPTION("ARM SCPI HWMON interface driver"); | |
306 | MODULE_LICENSE("GPL v2"); |