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