1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
2 /*
3 * Apple SMC hwmon driver for Apple Silicon platforms
4 *
5 * The System Management Controller on Apple Silicon devices is responsible for
6 * measuring data from sensors across the SoC and machine. These include power,
7 * temperature, voltage and current sensors. Some "sensors" actually expose
8 * derived values. An example of this is the key PHPC, which is an estimate
9 * of the heat energy being dissipated by the SoC.
10 *
11 * While each SoC only has one SMC variant, each platform exposes a different
12 * set of sensors. For example, M1 MacBooks expose battery telemetry sensors
13 * which are not present on the M1 Mac mini. For this reason, the available
14 * sensors for a given platform are described in the device tree in a child
15 * node of the SMC device. We must walk this list of available sensors and
16 * populate the required hwmon data structures at runtime.
17 *
18 * Originally based on a concept by Jean-Francois Bortolotti <jeff@borto.fr>
19 *
20 * Copyright The Asahi Linux Contributors
21 */
22
23 #include <linux/bitfield.h>
24 #include <linux/hwmon.h>
25 #include <linux/math64.h>
26 #include <linux/mfd/macsmc.h>
27 #include <linux/module.h>
28 #include <linux/of.h>
29 #include <linux/platform_device.h>
30
31 #define MAX_LABEL_LENGTH 32
32
33 /* Temperature, voltage, current, power, fan(s) */
34 #define NUM_SENSOR_TYPES 5
35
36 #define FLT_EXP_BIAS 127
37 #define FLT_EXP_MASK GENMASK(30, 23)
38 #define FLT_MANT_BIAS 23
39 #define FLT_MANT_MASK GENMASK(22, 0)
40 #define FLT_SIGN_MASK BIT(31)
41
42 static bool fan_control;
43 module_param_unsafe(fan_control, bool, 0644);
44 MODULE_PARM_DESC(fan_control,
45 "Override the SMC to set your own fan speeds on supported machines");
46
47 struct macsmc_hwmon_sensor {
48 struct apple_smc_key_info info;
49 smc_key macsmc_key;
50 char label[MAX_LABEL_LENGTH];
51 u32 attrs;
52 };
53
54 struct macsmc_hwmon_fan {
55 struct macsmc_hwmon_sensor now;
56 struct macsmc_hwmon_sensor min;
57 struct macsmc_hwmon_sensor max;
58 struct macsmc_hwmon_sensor set;
59 struct macsmc_hwmon_sensor mode;
60 char label[MAX_LABEL_LENGTH];
61 u32 attrs;
62 bool manual;
63 };
64
65 struct macsmc_hwmon_sensors {
66 struct hwmon_channel_info channel_info;
67 struct macsmc_hwmon_sensor *sensors;
68 u32 count;
69 };
70
71 struct macsmc_hwmon_fans {
72 struct hwmon_channel_info channel_info;
73 struct macsmc_hwmon_fan *fans;
74 u32 count;
75 };
76
77 struct macsmc_hwmon {
78 struct device *dev;
79 struct apple_smc *smc;
80 struct device *hwmon_dev;
81 struct hwmon_chip_info chip_info;
82 /* Chip + sensor types + NULL */
83 const struct hwmon_channel_info *channel_infos[1 + NUM_SENSOR_TYPES + 1];
84 struct macsmc_hwmon_sensors temp;
85 struct macsmc_hwmon_sensors volt;
86 struct macsmc_hwmon_sensors curr;
87 struct macsmc_hwmon_sensors power;
88 struct macsmc_hwmon_fans fan;
89 };
90
macsmc_hwmon_read_label(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,const char ** str)91 static int macsmc_hwmon_read_label(struct device *dev,
92 enum hwmon_sensor_types type, u32 attr,
93 int channel, const char **str)
94 {
95 struct macsmc_hwmon *hwmon = dev_get_drvdata(dev);
96
97 switch (type) {
98 case hwmon_temp:
99 *str = hwmon->temp.sensors[channel].label;
100 break;
101 case hwmon_in:
102 *str = hwmon->volt.sensors[channel].label;
103 break;
104 case hwmon_curr:
105 *str = hwmon->curr.sensors[channel].label;
106 break;
107 case hwmon_power:
108 *str = hwmon->power.sensors[channel].label;
109 break;
110 case hwmon_fan:
111 *str = hwmon->fan.fans[channel].label;
112 break;
113 default:
114 return -EOPNOTSUPP;
115 }
116
117 return 0;
118 }
119
120 /*
121 * A number of sensors report data in a 48.16 fixed-point decimal format that is
122 * not used by any other function of the SMC.
123 */
macsmc_hwmon_read_ioft_scaled(struct apple_smc * smc,smc_key key,u64 * p,int scale)124 static int macsmc_hwmon_read_ioft_scaled(struct apple_smc *smc, smc_key key,
125 u64 *p, int scale)
126 {
127 u64 val;
128 int ret;
129
130 ret = apple_smc_read_u64(smc, key, &val);
131 if (ret < 0)
132 return ret;
133
134 *p = mul_u64_u32_div(val, scale, 65536);
135
136 return 0;
137 }
138
139 /*
140 * Many sensors report their data as IEEE-754 floats. No other SMC function uses
141 * them.
142 */
macsmc_hwmon_read_f32_scaled(struct apple_smc * smc,smc_key key,long * p,int scale)143 static int macsmc_hwmon_read_f32_scaled(struct apple_smc *smc, smc_key key,
144 long *p, int scale)
145 {
146 u32 fval;
147 u64 val;
148 int ret, exp;
149
150 ret = apple_smc_read_u32(smc, key, &fval);
151 if (ret < 0)
152 return ret;
153
154 val = ((u64)((fval & FLT_MANT_MASK) | BIT(23)));
155 exp = ((fval >> 23) & 0xff) - FLT_EXP_BIAS - FLT_MANT_BIAS;
156
157 /* We never have negatively scaled SMC floats */
158 val *= scale;
159
160 if (exp > 63)
161 val = U64_MAX;
162 else if (exp < -63)
163 val = 0;
164 else if (exp < 0)
165 val >>= -exp;
166 else if (exp != 0 && (val & ~((1ULL << (64 - exp)) - 1))) /* overflow */
167 val = U64_MAX;
168 else
169 val <<= exp;
170
171 if (fval & FLT_SIGN_MASK) {
172 if (val > (u64)LONG_MAX + 1)
173 *p = LONG_MIN;
174 else
175 *p = -(long)val;
176 } else {
177 if (val > (u64)LONG_MAX)
178 *p = LONG_MAX;
179 else
180 *p = (long)val;
181 }
182
183 return 0;
184 }
185
186 /*
187 * The SMC has keys of multiple types, denoted by a FourCC of the same format
188 * as the key ID. We don't know what data type a key encodes until we poke at it.
189 */
macsmc_hwmon_read_key(struct apple_smc * smc,struct macsmc_hwmon_sensor * sensor,int scale,long * val)190 static int macsmc_hwmon_read_key(struct apple_smc *smc,
191 struct macsmc_hwmon_sensor *sensor, int scale,
192 long *val)
193 {
194 int ret;
195
196 switch (sensor->info.type_code) {
197 /* 32-bit IEEE 754 float */
198 case __SMC_KEY('f', 'l', 't', ' '): {
199 long flt_ = 0;
200
201 ret = macsmc_hwmon_read_f32_scaled(smc, sensor->macsmc_key,
202 &flt_, scale);
203 if (ret)
204 return ret;
205
206 *val = flt_;
207 break;
208 }
209 /* 48.16 fixed point decimal */
210 case __SMC_KEY('i', 'o', 'f', 't'): {
211 u64 ioft = 0;
212
213 ret = macsmc_hwmon_read_ioft_scaled(smc, sensor->macsmc_key,
214 &ioft, scale);
215 if (ret)
216 return ret;
217
218 if (ioft > LONG_MAX)
219 *val = LONG_MAX;
220 else
221 *val = (long)ioft;
222 break;
223 }
224 default:
225 return -EOPNOTSUPP;
226 }
227
228 return 0;
229 }
230
macsmc_hwmon_write_f32(struct apple_smc * smc,smc_key key,long value)231 static int macsmc_hwmon_write_f32(struct apple_smc *smc, smc_key key, long value)
232 {
233 u64 val;
234 u32 fval = 0;
235 int exp, neg;
236
237 neg = value < 0;
238 val = abs(value);
239
240 if (val) {
241 exp = __fls(val);
242
243 if (exp > 23)
244 val >>= exp - 23;
245 else
246 val <<= 23 - exp;
247
248 fval = FIELD_PREP(FLT_SIGN_MASK, neg) |
249 FIELD_PREP(FLT_EXP_MASK, exp + FLT_EXP_BIAS) |
250 FIELD_PREP(FLT_MANT_MASK, val & FLT_MANT_MASK);
251 }
252
253 return apple_smc_write_u32(smc, key, fval);
254 }
255
macsmc_hwmon_write_key(struct apple_smc * smc,struct macsmc_hwmon_sensor * sensor,long val)256 static int macsmc_hwmon_write_key(struct apple_smc *smc,
257 struct macsmc_hwmon_sensor *sensor, long val)
258 {
259 switch (sensor->info.type_code) {
260 /* 32-bit IEEE 754 float */
261 case __SMC_KEY('f', 'l', 't', ' '):
262 return macsmc_hwmon_write_f32(smc, sensor->macsmc_key, val);
263 /* unsigned 8-bit integer */
264 case __SMC_KEY('u', 'i', '8', ' '):
265 return apple_smc_write_u8(smc, sensor->macsmc_key, val);
266 default:
267 return -EOPNOTSUPP;
268 }
269 }
270
macsmc_hwmon_read_fan(struct macsmc_hwmon * hwmon,u32 attr,int chan,long * val)271 static int macsmc_hwmon_read_fan(struct macsmc_hwmon *hwmon, u32 attr, int chan,
272 long *val)
273 {
274 switch (attr) {
275 case hwmon_fan_input:
276 return macsmc_hwmon_read_key(hwmon->smc,
277 &hwmon->fan.fans[chan].now, 1, val);
278 case hwmon_fan_min:
279 return macsmc_hwmon_read_key(hwmon->smc,
280 &hwmon->fan.fans[chan].min, 1, val);
281 case hwmon_fan_max:
282 return macsmc_hwmon_read_key(hwmon->smc,
283 &hwmon->fan.fans[chan].max, 1, val);
284 case hwmon_fan_target:
285 return macsmc_hwmon_read_key(hwmon->smc,
286 &hwmon->fan.fans[chan].set, 1, val);
287 default:
288 return -EOPNOTSUPP;
289 }
290 }
291
macsmc_hwmon_write_fan(struct device * dev,u32 attr,int channel,long val)292 static int macsmc_hwmon_write_fan(struct device *dev, u32 attr, int channel,
293 long val)
294 {
295 struct macsmc_hwmon *hwmon = dev_get_drvdata(dev);
296 long min, max;
297 int ret;
298
299 if (!fan_control || hwmon->fan.fans[channel].mode.macsmc_key == 0)
300 return -EOPNOTSUPP;
301
302 /*
303 * The SMC does no sanity checks on requested fan speeds, so we need to.
304 */
305 ret = macsmc_hwmon_read_key(hwmon->smc, &hwmon->fan.fans[channel].min,
306 1, &min);
307 if (ret)
308 return ret;
309
310 ret = macsmc_hwmon_read_key(hwmon->smc, &hwmon->fan.fans[channel].max,
311 1, &max);
312 if (ret)
313 return ret;
314
315 if (val >= min && val <= max) {
316 if (!hwmon->fan.fans[channel].manual) {
317 /* Write 1 to mode key for manual control */
318 ret = macsmc_hwmon_write_key(hwmon->smc,
319 &hwmon->fan.fans[channel].mode, 1);
320 if (ret < 0)
321 return ret;
322
323 hwmon->fan.fans[channel].manual = true;
324 }
325 return macsmc_hwmon_write_key(hwmon->smc,
326 &hwmon->fan.fans[channel].set, val);
327 } else if (!val) {
328 if (hwmon->fan.fans[channel].manual) {
329 ret = macsmc_hwmon_write_key(hwmon->smc,
330 &hwmon->fan.fans[channel].mode, 0);
331 if (ret < 0)
332 return ret;
333
334 hwmon->fan.fans[channel].manual = false;
335 }
336 } else {
337 return -EINVAL;
338 }
339
340 return 0;
341 }
342
macsmc_hwmon_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)343 static int macsmc_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
344 u32 attr, int channel, long *val)
345 {
346 struct macsmc_hwmon *hwmon = dev_get_drvdata(dev);
347 int ret = 0;
348
349 switch (type) {
350 case hwmon_temp:
351 ret = macsmc_hwmon_read_key(hwmon->smc,
352 &hwmon->temp.sensors[channel], 1000, val);
353 break;
354 case hwmon_in:
355 ret = macsmc_hwmon_read_key(hwmon->smc,
356 &hwmon->volt.sensors[channel], 1000, val);
357 break;
358 case hwmon_curr:
359 ret = macsmc_hwmon_read_key(hwmon->smc,
360 &hwmon->curr.sensors[channel], 1000, val);
361 break;
362 case hwmon_power:
363 /* SMC returns power in Watts with acceptable precision to scale to uW */
364 ret = macsmc_hwmon_read_key(hwmon->smc,
365 &hwmon->power.sensors[channel],
366 1000000, val);
367 break;
368 case hwmon_fan:
369 ret = macsmc_hwmon_read_fan(hwmon, attr, channel, val);
370 break;
371 default:
372 return -EOPNOTSUPP;
373 }
374
375 return ret;
376 }
377
macsmc_hwmon_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)378 static int macsmc_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
379 u32 attr, int channel, long val)
380 {
381 switch (type) {
382 case hwmon_fan:
383 return macsmc_hwmon_write_fan(dev, attr, channel, val);
384 default:
385 return -EOPNOTSUPP;
386 }
387 }
388
macsmc_hwmon_fan_is_visible(const struct macsmc_hwmon_fan * fan,u32 attr)389 static umode_t macsmc_hwmon_fan_is_visible(const struct macsmc_hwmon_fan *fan,
390 u32 attr)
391 {
392 if (fan->attrs & BIT(attr)) {
393 if (attr == hwmon_fan_target && fan_control && fan->mode.macsmc_key)
394 return 0644;
395
396 return 0444;
397 }
398
399 return 0;
400 }
401
macsmc_hwmon_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)402 static umode_t macsmc_hwmon_is_visible(const void *data,
403 enum hwmon_sensor_types type, u32 attr,
404 int channel)
405 {
406 const struct macsmc_hwmon *hwmon = data;
407 struct macsmc_hwmon_sensor *sensor;
408
409 switch (type) {
410 case hwmon_in:
411 sensor = &hwmon->volt.sensors[channel];
412 break;
413 case hwmon_curr:
414 sensor = &hwmon->curr.sensors[channel];
415 break;
416 case hwmon_power:
417 sensor = &hwmon->power.sensors[channel];
418 break;
419 case hwmon_temp:
420 sensor = &hwmon->temp.sensors[channel];
421 break;
422 case hwmon_fan:
423 return macsmc_hwmon_fan_is_visible(&hwmon->fan.fans[channel], attr);
424 default:
425 return 0;
426 }
427
428 /* Sensors only register ro attributes */
429 if (sensor->attrs & BIT(attr))
430 return 0444;
431
432 return 0;
433 }
434
435 static const struct hwmon_ops macsmc_hwmon_ops = {
436 .is_visible = macsmc_hwmon_is_visible,
437 .read = macsmc_hwmon_read,
438 .read_string = macsmc_hwmon_read_label,
439 .write = macsmc_hwmon_write,
440 };
441
442 /*
443 * Get the key metadata, including key data type, from the SMC.
444 */
macsmc_hwmon_parse_key(struct device * dev,struct apple_smc * smc,struct macsmc_hwmon_sensor * sensor,const char * key)445 static int macsmc_hwmon_parse_key(struct device *dev, struct apple_smc *smc,
446 struct macsmc_hwmon_sensor *sensor,
447 const char *key)
448 {
449 int ret;
450
451 ret = apple_smc_get_key_info(smc, _SMC_KEY(key), &sensor->info);
452 if (ret) {
453 dev_dbg(dev, "Failed to retrieve key info for %s\n", key);
454 return ret;
455 }
456
457 sensor->macsmc_key = _SMC_KEY(key);
458
459 return 0;
460 }
461
462 /*
463 * A sensor is a single key-value pair as made available by the SMC.
464 * The devicetree gives us the SMC key ID and a friendly name where the
465 * purpose of the sensor is known.
466 */
macsmc_hwmon_create_sensor(struct device * dev,struct apple_smc * smc,struct device_node * sensor_node,struct macsmc_hwmon_sensor * sensor)467 static int macsmc_hwmon_create_sensor(struct device *dev, struct apple_smc *smc,
468 struct device_node *sensor_node,
469 struct macsmc_hwmon_sensor *sensor)
470 {
471 const char *key, *label;
472 int ret;
473
474 ret = of_property_read_string(sensor_node, "apple,key-id", &key);
475 if (ret) {
476 dev_dbg(dev, "Could not find apple,key-id in sensor node\n");
477 return ret;
478 }
479
480 ret = macsmc_hwmon_parse_key(dev, smc, sensor, key);
481 if (ret)
482 return ret;
483
484 ret = of_property_read_string(sensor_node, "label", &label);
485 if (ret)
486 dev_dbg(dev, "No label found for sensor %s\n", key);
487 else
488 strscpy_pad(sensor->label, label, sizeof(sensor->label));
489
490 return 0;
491 }
492
493 /*
494 * Fan data is exposed by the SMC as multiple sensors.
495 *
496 * The devicetree schema reuses apple,key-id for the actual fan speed sensor.
497 * Min, max and target keys do not need labels, so we can reuse label
498 * for naming the entire fan.
499 */
macsmc_hwmon_create_fan(struct device * dev,struct apple_smc * smc,struct device_node * fan_node,struct macsmc_hwmon_fan * fan)500 static int macsmc_hwmon_create_fan(struct device *dev, struct apple_smc *smc,
501 struct device_node *fan_node,
502 struct macsmc_hwmon_fan *fan)
503 {
504 const char *label, *now, *min, *max, *set, *mode;
505 int ret;
506
507 ret = of_property_read_string(fan_node, "apple,key-id", &now);
508 if (ret) {
509 dev_err(dev, "apple,key-id not found in fan node!\n");
510 return ret;
511 }
512
513 ret = macsmc_hwmon_parse_key(dev, smc, &fan->now, now);
514 if (ret)
515 return ret;
516
517 fan->attrs = HWMON_F_INPUT;
518
519 ret = of_property_read_string(fan_node, "label", &label);
520 if (ret) {
521 dev_dbg(dev, "No label found for fan %s\n", now);
522 } else {
523 strscpy_pad(fan->label, label, sizeof(fan->label));
524 fan->attrs |= HWMON_F_LABEL;
525 }
526
527 /* The following keys are not required to simply monitor fan speed */
528 if (!of_property_read_string(fan_node, "apple,fan-minimum", &min)) {
529 ret = macsmc_hwmon_parse_key(dev, smc, &fan->min, min);
530 if (ret)
531 return ret;
532
533 fan->attrs |= HWMON_F_MIN;
534 }
535
536 if (!of_property_read_string(fan_node, "apple,fan-maximum", &max)) {
537 ret = macsmc_hwmon_parse_key(dev, smc, &fan->max, max);
538 if (ret)
539 return ret;
540
541 fan->attrs |= HWMON_F_MAX;
542 }
543
544 if (!of_property_read_string(fan_node, "apple,fan-target", &set)) {
545 ret = macsmc_hwmon_parse_key(dev, smc, &fan->set, set);
546 if (ret)
547 return ret;
548
549 fan->attrs |= HWMON_F_TARGET;
550 }
551
552 if (!of_property_read_string(fan_node, "apple,fan-mode", &mode)) {
553 ret = macsmc_hwmon_parse_key(dev, smc, &fan->mode, mode);
554 if (ret)
555 return ret;
556 }
557
558 /* Initialise fan control mode to automatic */
559 fan->manual = false;
560
561 return 0;
562 }
563
macsmc_hwmon_populate_sensors(struct macsmc_hwmon * hwmon,struct device_node * hwmon_node)564 static int macsmc_hwmon_populate_sensors(struct macsmc_hwmon *hwmon,
565 struct device_node *hwmon_node)
566 {
567 struct device_node *key_node __maybe_unused;
568 struct macsmc_hwmon_sensor *sensor;
569 u32 n_current = 0, n_fan = 0, n_power = 0, n_temperature = 0, n_voltage = 0;
570
571 for_each_child_of_node_with_prefix(hwmon_node, key_node, "current-") {
572 n_current++;
573 }
574
575 if (n_current) {
576 hwmon->curr.sensors = devm_kcalloc(hwmon->dev, n_current,
577 sizeof(struct macsmc_hwmon_sensor), GFP_KERNEL);
578 if (!hwmon->curr.sensors)
579 return -ENOMEM;
580
581 for_each_child_of_node_with_prefix(hwmon_node, key_node, "current-") {
582 sensor = &hwmon->curr.sensors[hwmon->curr.count];
583 if (!macsmc_hwmon_create_sensor(hwmon->dev, hwmon->smc, key_node, sensor)) {
584 sensor->attrs = HWMON_C_INPUT;
585
586 if (*sensor->label)
587 sensor->attrs |= HWMON_C_LABEL;
588
589 hwmon->curr.count++;
590 }
591 }
592 }
593
594 for_each_child_of_node_with_prefix(hwmon_node, key_node, "fan-") {
595 n_fan++;
596 }
597
598 if (n_fan) {
599 hwmon->fan.fans = devm_kcalloc(hwmon->dev, n_fan,
600 sizeof(struct macsmc_hwmon_fan), GFP_KERNEL);
601 if (!hwmon->fan.fans)
602 return -ENOMEM;
603
604 for_each_child_of_node_with_prefix(hwmon_node, key_node, "fan-") {
605 if (!macsmc_hwmon_create_fan(hwmon->dev, hwmon->smc, key_node,
606 &hwmon->fan.fans[hwmon->fan.count]))
607 hwmon->fan.count++;
608 }
609 }
610
611 for_each_child_of_node_with_prefix(hwmon_node, key_node, "power-") {
612 n_power++;
613 }
614
615 if (n_power) {
616 hwmon->power.sensors = devm_kcalloc(hwmon->dev, n_power,
617 sizeof(struct macsmc_hwmon_sensor), GFP_KERNEL);
618 if (!hwmon->power.sensors)
619 return -ENOMEM;
620
621 for_each_child_of_node_with_prefix(hwmon_node, key_node, "power-") {
622 sensor = &hwmon->power.sensors[hwmon->power.count];
623 if (!macsmc_hwmon_create_sensor(hwmon->dev, hwmon->smc, key_node, sensor)) {
624 sensor->attrs = HWMON_P_INPUT;
625
626 if (*sensor->label)
627 sensor->attrs |= HWMON_P_LABEL;
628
629 hwmon->power.count++;
630 }
631 }
632 }
633
634 for_each_child_of_node_with_prefix(hwmon_node, key_node, "temperature-") {
635 n_temperature++;
636 }
637
638 if (n_temperature) {
639 hwmon->temp.sensors = devm_kcalloc(hwmon->dev, n_temperature,
640 sizeof(struct macsmc_hwmon_sensor), GFP_KERNEL);
641 if (!hwmon->temp.sensors)
642 return -ENOMEM;
643
644 for_each_child_of_node_with_prefix(hwmon_node, key_node, "temperature-") {
645 sensor = &hwmon->temp.sensors[hwmon->temp.count];
646 if (!macsmc_hwmon_create_sensor(hwmon->dev, hwmon->smc, key_node, sensor)) {
647 sensor->attrs = HWMON_T_INPUT;
648
649 if (*sensor->label)
650 sensor->attrs |= HWMON_T_LABEL;
651
652 hwmon->temp.count++;
653 }
654 }
655 }
656
657 for_each_child_of_node_with_prefix(hwmon_node, key_node, "voltage-") {
658 n_voltage++;
659 }
660
661 if (n_voltage) {
662 hwmon->volt.sensors = devm_kcalloc(hwmon->dev, n_voltage,
663 sizeof(struct macsmc_hwmon_sensor), GFP_KERNEL);
664 if (!hwmon->volt.sensors)
665 return -ENOMEM;
666
667 for_each_child_of_node_with_prefix(hwmon_node, key_node, "voltage-") {
668 sensor = &hwmon->volt.sensors[hwmon->volt.count];
669 if (!macsmc_hwmon_create_sensor(hwmon->dev, hwmon->smc, key_node, sensor)) {
670 sensor->attrs = HWMON_I_INPUT;
671
672 if (*sensor->label)
673 sensor->attrs |= HWMON_I_LABEL;
674
675 hwmon->volt.count++;
676 }
677 }
678 }
679
680 return 0;
681 }
682
683 /* Create NULL-terminated config arrays */
macsmc_hwmon_populate_configs(u32 * configs,const struct macsmc_hwmon_sensors * sensors)684 static void macsmc_hwmon_populate_configs(u32 *configs, const struct macsmc_hwmon_sensors *sensors)
685 {
686 int idx;
687
688 for (idx = 0; idx < sensors->count; idx++)
689 configs[idx] = sensors->sensors[idx].attrs;
690 }
691
macsmc_hwmon_populate_fan_configs(u32 * configs,const struct macsmc_hwmon_fans * fans)692 static void macsmc_hwmon_populate_fan_configs(u32 *configs, const struct macsmc_hwmon_fans *fans)
693 {
694 int idx;
695
696 for (idx = 0; idx < fans->count; idx++)
697 configs[idx] = fans->fans[idx].attrs;
698 }
699
700 static const struct hwmon_channel_info *const macsmc_chip_channel_info =
701 HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ);
702
macsmc_hwmon_create_infos(struct macsmc_hwmon * hwmon)703 static int macsmc_hwmon_create_infos(struct macsmc_hwmon *hwmon)
704 {
705 struct hwmon_channel_info *channel_info;
706 int i = 0;
707
708 /* chip */
709 hwmon->channel_infos[i++] = macsmc_chip_channel_info;
710
711 if (hwmon->curr.count) {
712 channel_info = &hwmon->curr.channel_info;
713 channel_info->type = hwmon_curr;
714 channel_info->config = devm_kcalloc(hwmon->dev, hwmon->curr.count + 1,
715 sizeof(u32), GFP_KERNEL);
716 if (!channel_info->config)
717 return -ENOMEM;
718
719 macsmc_hwmon_populate_configs((u32 *)channel_info->config, &hwmon->curr);
720 hwmon->channel_infos[i++] = channel_info;
721 }
722
723 if (hwmon->fan.count) {
724 channel_info = &hwmon->fan.channel_info;
725 channel_info->type = hwmon_fan;
726 channel_info->config = devm_kcalloc(hwmon->dev, hwmon->fan.count + 1,
727 sizeof(u32), GFP_KERNEL);
728 if (!channel_info->config)
729 return -ENOMEM;
730
731 macsmc_hwmon_populate_fan_configs((u32 *)channel_info->config, &hwmon->fan);
732 hwmon->channel_infos[i++] = channel_info;
733 }
734
735 if (hwmon->power.count) {
736 channel_info = &hwmon->power.channel_info;
737 channel_info->type = hwmon_power;
738 channel_info->config = devm_kcalloc(hwmon->dev, hwmon->power.count + 1,
739 sizeof(u32), GFP_KERNEL);
740 if (!channel_info->config)
741 return -ENOMEM;
742
743 macsmc_hwmon_populate_configs((u32 *)channel_info->config, &hwmon->power);
744 hwmon->channel_infos[i++] = channel_info;
745 }
746
747 if (hwmon->temp.count) {
748 channel_info = &hwmon->temp.channel_info;
749 channel_info->type = hwmon_temp;
750 channel_info->config = devm_kcalloc(hwmon->dev, hwmon->temp.count + 1,
751 sizeof(u32), GFP_KERNEL);
752 if (!channel_info->config)
753 return -ENOMEM;
754
755 macsmc_hwmon_populate_configs((u32 *)channel_info->config, &hwmon->temp);
756 hwmon->channel_infos[i++] = channel_info;
757 }
758
759 if (hwmon->volt.count) {
760 channel_info = &hwmon->volt.channel_info;
761 channel_info->type = hwmon_in;
762 channel_info->config = devm_kcalloc(hwmon->dev, hwmon->volt.count + 1,
763 sizeof(u32), GFP_KERNEL);
764 if (!channel_info->config)
765 return -ENOMEM;
766
767 macsmc_hwmon_populate_configs((u32 *)channel_info->config, &hwmon->volt);
768 hwmon->channel_infos[i++] = channel_info;
769 }
770
771 return 0;
772 }
773
macsmc_hwmon_probe(struct platform_device * pdev)774 static int macsmc_hwmon_probe(struct platform_device *pdev)
775 {
776 struct apple_smc *smc = dev_get_drvdata(pdev->dev.parent);
777 struct macsmc_hwmon *hwmon;
778 int ret;
779
780 /*
781 * The MFD driver will try to probe us unconditionally. Some devices
782 * with the SMC do not have hwmon capabilities. Only probe if we have
783 * a hwmon node.
784 */
785 if (!pdev->dev.of_node)
786 return -ENODEV;
787
788 hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon),
789 GFP_KERNEL);
790 if (!hwmon)
791 return -ENOMEM;
792
793 hwmon->dev = &pdev->dev;
794 hwmon->smc = smc;
795
796 ret = macsmc_hwmon_populate_sensors(hwmon, hwmon->dev->of_node);
797 if (ret) {
798 dev_err(hwmon->dev, "Could not parse sensors\n");
799 return ret;
800 }
801
802 if (!hwmon->curr.count && !hwmon->fan.count &&
803 !hwmon->power.count && !hwmon->temp.count &&
804 !hwmon->volt.count) {
805 dev_err(hwmon->dev,
806 "No valid sensors found of any supported type\n");
807 return -ENODEV;
808 }
809
810 ret = macsmc_hwmon_create_infos(hwmon);
811 if (ret)
812 return ret;
813
814 hwmon->chip_info.ops = &macsmc_hwmon_ops;
815 hwmon->chip_info.info =
816 (const struct hwmon_channel_info *const *)&hwmon->channel_infos;
817
818 hwmon->hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
819 "macsmc_hwmon", hwmon,
820 &hwmon->chip_info, NULL);
821 if (IS_ERR(hwmon->hwmon_dev))
822 return dev_err_probe(hwmon->dev, PTR_ERR(hwmon->hwmon_dev),
823 "Probing SMC hwmon device failed\n");
824
825 dev_dbg(hwmon->dev, "Registered SMC hwmon device. Sensors:\n");
826 dev_dbg(hwmon->dev,
827 "Current: %d, Fans: %d, Power: %d, Temperature: %d, Voltage: %d",
828 hwmon->curr.count, hwmon->fan.count,
829 hwmon->power.count, hwmon->temp.count,
830 hwmon->volt.count);
831
832 return 0;
833 }
834
835 static const struct of_device_id macsmc_hwmon_of_table[] = {
836 { .compatible = "apple,smc-hwmon" },
837 {}
838 };
839 MODULE_DEVICE_TABLE(of, macsmc_hwmon_of_table);
840
841 static struct platform_driver macsmc_hwmon_driver = {
842 .probe = macsmc_hwmon_probe,
843 .driver = {
844 .name = "macsmc-hwmon",
845 .of_match_table = macsmc_hwmon_of_table,
846 },
847 };
848 module_platform_driver(macsmc_hwmon_driver);
849
850 MODULE_DESCRIPTION("Apple Silicon SMC hwmon driver");
851 MODULE_AUTHOR("James Calligeros <jcalligeros99@gmail.com>");
852 MODULE_LICENSE("Dual MIT/GPL");
853