WIP FPC-III support
[linux/fpc-iii.git] / drivers / hwmon / occ / common.c
blob7a5e539b567bfb8778e2a3c763490a816732e312
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright IBM Corp 2019
4 #include <linux/device.h>
5 #include <linux/export.h>
6 #include <linux/hwmon.h>
7 #include <linux/hwmon-sysfs.h>
8 #include <linux/jiffies.h>
9 #include <linux/kernel.h>
10 #include <linux/math64.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/sysfs.h>
14 #include <asm/unaligned.h>
16 #include "common.h"
18 #define EXTN_FLAG_SENSOR_ID BIT(7)
20 #define OCC_ERROR_COUNT_THRESHOLD 2 /* required by OCC spec */
22 #define OCC_STATE_SAFE 4
23 #define OCC_SAFE_TIMEOUT msecs_to_jiffies(60000) /* 1 min */
25 #define OCC_UPDATE_FREQUENCY msecs_to_jiffies(1000)
27 #define OCC_TEMP_SENSOR_FAULT 0xFF
29 #define OCC_FRU_TYPE_VRM 3
31 /* OCC sensor type and version definitions */
33 struct temp_sensor_1 {
34 u16 sensor_id;
35 u16 value;
36 } __packed;
38 struct temp_sensor_2 {
39 u32 sensor_id;
40 u8 fru_type;
41 u8 value;
42 } __packed;
44 struct temp_sensor_10 {
45 u32 sensor_id;
46 u8 fru_type;
47 u8 value;
48 u8 throttle;
49 u8 reserved;
50 } __packed;
52 struct freq_sensor_1 {
53 u16 sensor_id;
54 u16 value;
55 } __packed;
57 struct freq_sensor_2 {
58 u32 sensor_id;
59 u16 value;
60 } __packed;
62 struct power_sensor_1 {
63 u16 sensor_id;
64 u32 update_tag;
65 u32 accumulator;
66 u16 value;
67 } __packed;
69 struct power_sensor_2 {
70 u32 sensor_id;
71 u8 function_id;
72 u8 apss_channel;
73 u16 reserved;
74 u32 update_tag;
75 u64 accumulator;
76 u16 value;
77 } __packed;
79 struct power_sensor_data {
80 u16 value;
81 u32 update_tag;
82 u64 accumulator;
83 } __packed;
85 struct power_sensor_data_and_time {
86 u16 update_time;
87 u16 value;
88 u32 update_tag;
89 u64 accumulator;
90 } __packed;
92 struct power_sensor_a0 {
93 u32 sensor_id;
94 struct power_sensor_data_and_time system;
95 u32 reserved;
96 struct power_sensor_data_and_time proc;
97 struct power_sensor_data vdd;
98 struct power_sensor_data vdn;
99 } __packed;
101 struct caps_sensor_2 {
102 u16 cap;
103 u16 system_power;
104 u16 n_cap;
105 u16 max;
106 u16 min;
107 u16 user;
108 u8 user_source;
109 } __packed;
111 struct caps_sensor_3 {
112 u16 cap;
113 u16 system_power;
114 u16 n_cap;
115 u16 max;
116 u16 hard_min;
117 u16 soft_min;
118 u16 user;
119 u8 user_source;
120 } __packed;
122 struct extended_sensor {
123 union {
124 u8 name[4];
125 u32 sensor_id;
127 u8 flags;
128 u8 reserved;
129 u8 data[6];
130 } __packed;
132 static int occ_poll(struct occ *occ)
134 int rc;
135 u16 checksum = occ->poll_cmd_data + occ->seq_no + 1;
136 u8 cmd[8];
137 struct occ_poll_response_header *header;
139 /* big endian */
140 cmd[0] = occ->seq_no++; /* sequence number */
141 cmd[1] = 0; /* cmd type */
142 cmd[2] = 0; /* data length msb */
143 cmd[3] = 1; /* data length lsb */
144 cmd[4] = occ->poll_cmd_data; /* data */
145 cmd[5] = checksum >> 8; /* checksum msb */
146 cmd[6] = checksum & 0xFF; /* checksum lsb */
147 cmd[7] = 0;
149 /* mutex should already be locked if necessary */
150 rc = occ->send_cmd(occ, cmd);
151 if (rc) {
152 occ->last_error = rc;
153 if (occ->error_count++ > OCC_ERROR_COUNT_THRESHOLD)
154 occ->error = rc;
156 goto done;
159 /* clear error since communication was successful */
160 occ->error_count = 0;
161 occ->last_error = 0;
162 occ->error = 0;
164 /* check for safe state */
165 header = (struct occ_poll_response_header *)occ->resp.data;
166 if (header->occ_state == OCC_STATE_SAFE) {
167 if (occ->last_safe) {
168 if (time_after(jiffies,
169 occ->last_safe + OCC_SAFE_TIMEOUT))
170 occ->error = -EHOSTDOWN;
171 } else {
172 occ->last_safe = jiffies;
174 } else {
175 occ->last_safe = 0;
178 done:
179 occ_sysfs_poll_done(occ);
180 return rc;
183 static int occ_set_user_power_cap(struct occ *occ, u16 user_power_cap)
185 int rc;
186 u8 cmd[8];
187 u16 checksum = 0x24;
188 __be16 user_power_cap_be = cpu_to_be16(user_power_cap);
190 cmd[0] = 0;
191 cmd[1] = 0x22;
192 cmd[2] = 0;
193 cmd[3] = 2;
195 memcpy(&cmd[4], &user_power_cap_be, 2);
197 checksum += cmd[4] + cmd[5];
198 cmd[6] = checksum >> 8;
199 cmd[7] = checksum & 0xFF;
201 rc = mutex_lock_interruptible(&occ->lock);
202 if (rc)
203 return rc;
205 rc = occ->send_cmd(occ, cmd);
207 mutex_unlock(&occ->lock);
209 return rc;
212 int occ_update_response(struct occ *occ)
214 int rc = mutex_lock_interruptible(&occ->lock);
216 if (rc)
217 return rc;
219 /* limit the maximum rate of polling the OCC */
220 if (time_after(jiffies, occ->last_update + OCC_UPDATE_FREQUENCY)) {
221 rc = occ_poll(occ);
222 occ->last_update = jiffies;
223 } else {
224 rc = occ->last_error;
227 mutex_unlock(&occ->lock);
228 return rc;
231 static ssize_t occ_show_temp_1(struct device *dev,
232 struct device_attribute *attr, char *buf)
234 int rc;
235 u32 val = 0;
236 struct temp_sensor_1 *temp;
237 struct occ *occ = dev_get_drvdata(dev);
238 struct occ_sensors *sensors = &occ->sensors;
239 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
241 rc = occ_update_response(occ);
242 if (rc)
243 return rc;
245 temp = ((struct temp_sensor_1 *)sensors->temp.data) + sattr->index;
247 switch (sattr->nr) {
248 case 0:
249 val = get_unaligned_be16(&temp->sensor_id);
250 break;
251 case 1:
253 * If a sensor reading has expired and couldn't be refreshed,
254 * OCC returns 0xFFFF for that sensor.
256 if (temp->value == 0xFFFF)
257 return -EREMOTEIO;
258 val = get_unaligned_be16(&temp->value) * 1000;
259 break;
260 default:
261 return -EINVAL;
264 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
267 static ssize_t occ_show_temp_2(struct device *dev,
268 struct device_attribute *attr, char *buf)
270 int rc;
271 u32 val = 0;
272 struct temp_sensor_2 *temp;
273 struct occ *occ = dev_get_drvdata(dev);
274 struct occ_sensors *sensors = &occ->sensors;
275 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
277 rc = occ_update_response(occ);
278 if (rc)
279 return rc;
281 temp = ((struct temp_sensor_2 *)sensors->temp.data) + sattr->index;
283 switch (sattr->nr) {
284 case 0:
285 val = get_unaligned_be32(&temp->sensor_id);
286 break;
287 case 1:
288 val = temp->value;
289 if (val == OCC_TEMP_SENSOR_FAULT)
290 return -EREMOTEIO;
293 * VRM doesn't return temperature, only alarm bit. This
294 * attribute maps to tempX_alarm instead of tempX_input for
295 * VRM
297 if (temp->fru_type != OCC_FRU_TYPE_VRM) {
298 /* sensor not ready */
299 if (val == 0)
300 return -EAGAIN;
302 val *= 1000;
304 break;
305 case 2:
306 val = temp->fru_type;
307 break;
308 case 3:
309 val = temp->value == OCC_TEMP_SENSOR_FAULT;
310 break;
311 default:
312 return -EINVAL;
315 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
318 static ssize_t occ_show_temp_10(struct device *dev,
319 struct device_attribute *attr, char *buf)
321 int rc;
322 u32 val = 0;
323 struct temp_sensor_10 *temp;
324 struct occ *occ = dev_get_drvdata(dev);
325 struct occ_sensors *sensors = &occ->sensors;
326 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
328 rc = occ_update_response(occ);
329 if (rc)
330 return rc;
332 temp = ((struct temp_sensor_10 *)sensors->temp.data) + sattr->index;
334 switch (sattr->nr) {
335 case 0:
336 val = get_unaligned_be32(&temp->sensor_id);
337 break;
338 case 1:
339 val = temp->value;
340 if (val == OCC_TEMP_SENSOR_FAULT)
341 return -EREMOTEIO;
344 * VRM doesn't return temperature, only alarm bit. This
345 * attribute maps to tempX_alarm instead of tempX_input for
346 * VRM
348 if (temp->fru_type != OCC_FRU_TYPE_VRM) {
349 /* sensor not ready */
350 if (val == 0)
351 return -EAGAIN;
353 val *= 1000;
355 break;
356 case 2:
357 val = temp->fru_type;
358 break;
359 case 3:
360 val = temp->value == OCC_TEMP_SENSOR_FAULT;
361 break;
362 case 4:
363 val = temp->throttle * 1000;
364 break;
365 default:
366 return -EINVAL;
369 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
372 static ssize_t occ_show_freq_1(struct device *dev,
373 struct device_attribute *attr, char *buf)
375 int rc;
376 u16 val = 0;
377 struct freq_sensor_1 *freq;
378 struct occ *occ = dev_get_drvdata(dev);
379 struct occ_sensors *sensors = &occ->sensors;
380 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
382 rc = occ_update_response(occ);
383 if (rc)
384 return rc;
386 freq = ((struct freq_sensor_1 *)sensors->freq.data) + sattr->index;
388 switch (sattr->nr) {
389 case 0:
390 val = get_unaligned_be16(&freq->sensor_id);
391 break;
392 case 1:
393 val = get_unaligned_be16(&freq->value);
394 break;
395 default:
396 return -EINVAL;
399 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
402 static ssize_t occ_show_freq_2(struct device *dev,
403 struct device_attribute *attr, char *buf)
405 int rc;
406 u32 val = 0;
407 struct freq_sensor_2 *freq;
408 struct occ *occ = dev_get_drvdata(dev);
409 struct occ_sensors *sensors = &occ->sensors;
410 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
412 rc = occ_update_response(occ);
413 if (rc)
414 return rc;
416 freq = ((struct freq_sensor_2 *)sensors->freq.data) + sattr->index;
418 switch (sattr->nr) {
419 case 0:
420 val = get_unaligned_be32(&freq->sensor_id);
421 break;
422 case 1:
423 val = get_unaligned_be16(&freq->value);
424 break;
425 default:
426 return -EINVAL;
429 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
432 static ssize_t occ_show_power_1(struct device *dev,
433 struct device_attribute *attr, char *buf)
435 int rc;
436 u64 val = 0;
437 struct power_sensor_1 *power;
438 struct occ *occ = dev_get_drvdata(dev);
439 struct occ_sensors *sensors = &occ->sensors;
440 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
442 rc = occ_update_response(occ);
443 if (rc)
444 return rc;
446 power = ((struct power_sensor_1 *)sensors->power.data) + sattr->index;
448 switch (sattr->nr) {
449 case 0:
450 val = get_unaligned_be16(&power->sensor_id);
451 break;
452 case 1:
453 val = get_unaligned_be32(&power->accumulator) /
454 get_unaligned_be32(&power->update_tag);
455 val *= 1000000ULL;
456 break;
457 case 2:
458 val = (u64)get_unaligned_be32(&power->update_tag) *
459 occ->powr_sample_time_us;
460 break;
461 case 3:
462 val = get_unaligned_be16(&power->value) * 1000000ULL;
463 break;
464 default:
465 return -EINVAL;
468 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
471 static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
473 u64 divisor = get_unaligned_be32(samples);
475 return (divisor == 0) ? 0 :
476 div64_u64(get_unaligned_be64(accum) * 1000000ULL, divisor);
479 static ssize_t occ_show_power_2(struct device *dev,
480 struct device_attribute *attr, char *buf)
482 int rc;
483 u64 val = 0;
484 struct power_sensor_2 *power;
485 struct occ *occ = dev_get_drvdata(dev);
486 struct occ_sensors *sensors = &occ->sensors;
487 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
489 rc = occ_update_response(occ);
490 if (rc)
491 return rc;
493 power = ((struct power_sensor_2 *)sensors->power.data) + sattr->index;
495 switch (sattr->nr) {
496 case 0:
497 return snprintf(buf, PAGE_SIZE - 1, "%u_%u_%u\n",
498 get_unaligned_be32(&power->sensor_id),
499 power->function_id, power->apss_channel);
500 case 1:
501 val = occ_get_powr_avg(&power->accumulator,
502 &power->update_tag);
503 break;
504 case 2:
505 val = (u64)get_unaligned_be32(&power->update_tag) *
506 occ->powr_sample_time_us;
507 break;
508 case 3:
509 val = get_unaligned_be16(&power->value) * 1000000ULL;
510 break;
511 default:
512 return -EINVAL;
515 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
518 static ssize_t occ_show_power_a0(struct device *dev,
519 struct device_attribute *attr, char *buf)
521 int rc;
522 u64 val = 0;
523 struct power_sensor_a0 *power;
524 struct occ *occ = dev_get_drvdata(dev);
525 struct occ_sensors *sensors = &occ->sensors;
526 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
528 rc = occ_update_response(occ);
529 if (rc)
530 return rc;
532 power = ((struct power_sensor_a0 *)sensors->power.data) + sattr->index;
534 switch (sattr->nr) {
535 case 0:
536 return snprintf(buf, PAGE_SIZE - 1, "%u_system\n",
537 get_unaligned_be32(&power->sensor_id));
538 case 1:
539 val = occ_get_powr_avg(&power->system.accumulator,
540 &power->system.update_tag);
541 break;
542 case 2:
543 val = (u64)get_unaligned_be32(&power->system.update_tag) *
544 occ->powr_sample_time_us;
545 break;
546 case 3:
547 val = get_unaligned_be16(&power->system.value) * 1000000ULL;
548 break;
549 case 4:
550 return snprintf(buf, PAGE_SIZE - 1, "%u_proc\n",
551 get_unaligned_be32(&power->sensor_id));
552 case 5:
553 val = occ_get_powr_avg(&power->proc.accumulator,
554 &power->proc.update_tag);
555 break;
556 case 6:
557 val = (u64)get_unaligned_be32(&power->proc.update_tag) *
558 occ->powr_sample_time_us;
559 break;
560 case 7:
561 val = get_unaligned_be16(&power->proc.value) * 1000000ULL;
562 break;
563 case 8:
564 return snprintf(buf, PAGE_SIZE - 1, "%u_vdd\n",
565 get_unaligned_be32(&power->sensor_id));
566 case 9:
567 val = occ_get_powr_avg(&power->vdd.accumulator,
568 &power->vdd.update_tag);
569 break;
570 case 10:
571 val = (u64)get_unaligned_be32(&power->vdd.update_tag) *
572 occ->powr_sample_time_us;
573 break;
574 case 11:
575 val = get_unaligned_be16(&power->vdd.value) * 1000000ULL;
576 break;
577 case 12:
578 return snprintf(buf, PAGE_SIZE - 1, "%u_vdn\n",
579 get_unaligned_be32(&power->sensor_id));
580 case 13:
581 val = occ_get_powr_avg(&power->vdn.accumulator,
582 &power->vdn.update_tag);
583 break;
584 case 14:
585 val = (u64)get_unaligned_be32(&power->vdn.update_tag) *
586 occ->powr_sample_time_us;
587 break;
588 case 15:
589 val = get_unaligned_be16(&power->vdn.value) * 1000000ULL;
590 break;
591 default:
592 return -EINVAL;
595 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
598 static ssize_t occ_show_caps_1_2(struct device *dev,
599 struct device_attribute *attr, char *buf)
601 int rc;
602 u64 val = 0;
603 struct caps_sensor_2 *caps;
604 struct occ *occ = dev_get_drvdata(dev);
605 struct occ_sensors *sensors = &occ->sensors;
606 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
608 rc = occ_update_response(occ);
609 if (rc)
610 return rc;
612 caps = ((struct caps_sensor_2 *)sensors->caps.data) + sattr->index;
614 switch (sattr->nr) {
615 case 0:
616 return snprintf(buf, PAGE_SIZE - 1, "system\n");
617 case 1:
618 val = get_unaligned_be16(&caps->cap) * 1000000ULL;
619 break;
620 case 2:
621 val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
622 break;
623 case 3:
624 val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
625 break;
626 case 4:
627 val = get_unaligned_be16(&caps->max) * 1000000ULL;
628 break;
629 case 5:
630 val = get_unaligned_be16(&caps->min) * 1000000ULL;
631 break;
632 case 6:
633 val = get_unaligned_be16(&caps->user) * 1000000ULL;
634 break;
635 case 7:
636 if (occ->sensors.caps.version == 1)
637 return -EINVAL;
639 val = caps->user_source;
640 break;
641 default:
642 return -EINVAL;
645 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
648 static ssize_t occ_show_caps_3(struct device *dev,
649 struct device_attribute *attr, char *buf)
651 int rc;
652 u64 val = 0;
653 struct caps_sensor_3 *caps;
654 struct occ *occ = dev_get_drvdata(dev);
655 struct occ_sensors *sensors = &occ->sensors;
656 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
658 rc = occ_update_response(occ);
659 if (rc)
660 return rc;
662 caps = ((struct caps_sensor_3 *)sensors->caps.data) + sattr->index;
664 switch (sattr->nr) {
665 case 0:
666 return snprintf(buf, PAGE_SIZE - 1, "system\n");
667 case 1:
668 val = get_unaligned_be16(&caps->cap) * 1000000ULL;
669 break;
670 case 2:
671 val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
672 break;
673 case 3:
674 val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
675 break;
676 case 4:
677 val = get_unaligned_be16(&caps->max) * 1000000ULL;
678 break;
679 case 5:
680 val = get_unaligned_be16(&caps->hard_min) * 1000000ULL;
681 break;
682 case 6:
683 val = get_unaligned_be16(&caps->user) * 1000000ULL;
684 break;
685 case 7:
686 val = caps->user_source;
687 break;
688 default:
689 return -EINVAL;
692 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
695 static ssize_t occ_store_caps_user(struct device *dev,
696 struct device_attribute *attr,
697 const char *buf, size_t count)
699 int rc;
700 u16 user_power_cap;
701 unsigned long long value;
702 struct occ *occ = dev_get_drvdata(dev);
704 rc = kstrtoull(buf, 0, &value);
705 if (rc)
706 return rc;
708 user_power_cap = div64_u64(value, 1000000ULL); /* microwatt to watt */
710 rc = occ_set_user_power_cap(occ, user_power_cap);
711 if (rc)
712 return rc;
714 return count;
717 static ssize_t occ_show_extended(struct device *dev,
718 struct device_attribute *attr, char *buf)
720 int rc;
721 struct extended_sensor *extn;
722 struct occ *occ = dev_get_drvdata(dev);
723 struct occ_sensors *sensors = &occ->sensors;
724 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
726 rc = occ_update_response(occ);
727 if (rc)
728 return rc;
730 extn = ((struct extended_sensor *)sensors->extended.data) +
731 sattr->index;
733 switch (sattr->nr) {
734 case 0:
735 if (extn->flags & EXTN_FLAG_SENSOR_ID)
736 rc = snprintf(buf, PAGE_SIZE - 1, "%u",
737 get_unaligned_be32(&extn->sensor_id));
738 else
739 rc = snprintf(buf, PAGE_SIZE - 1, "%02x%02x%02x%02x\n",
740 extn->name[0], extn->name[1],
741 extn->name[2], extn->name[3]);
742 break;
743 case 1:
744 rc = snprintf(buf, PAGE_SIZE - 1, "%02x\n", extn->flags);
745 break;
746 case 2:
747 rc = snprintf(buf, PAGE_SIZE - 1, "%02x%02x%02x%02x%02x%02x\n",
748 extn->data[0], extn->data[1], extn->data[2],
749 extn->data[3], extn->data[4], extn->data[5]);
750 break;
751 default:
752 return -EINVAL;
755 return rc;
759 * Some helper macros to make it easier to define an occ_attribute. Since these
760 * are dynamically allocated, we shouldn't use the existing kernel macros which
761 * stringify the name argument.
763 #define ATTR_OCC(_name, _mode, _show, _store) { \
764 .attr = { \
765 .name = _name, \
766 .mode = VERIFY_OCTAL_PERMISSIONS(_mode), \
767 }, \
768 .show = _show, \
769 .store = _store, \
772 #define SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index) { \
773 .dev_attr = ATTR_OCC(_name, _mode, _show, _store), \
774 .index = _index, \
775 .nr = _nr, \
778 #define OCC_INIT_ATTR(_name, _mode, _show, _store, _nr, _index) \
779 ((struct sensor_device_attribute_2) \
780 SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index))
783 * Allocate and instatiate sensor_device_attribute_2s. It's most efficient to
784 * use our own instead of the built-in hwmon attribute types.
786 static int occ_setup_sensor_attrs(struct occ *occ)
788 unsigned int i, s, num_attrs = 0;
789 struct device *dev = occ->bus_dev;
790 struct occ_sensors *sensors = &occ->sensors;
791 struct occ_attribute *attr;
792 struct temp_sensor_2 *temp;
793 ssize_t (*show_temp)(struct device *, struct device_attribute *,
794 char *) = occ_show_temp_1;
795 ssize_t (*show_freq)(struct device *, struct device_attribute *,
796 char *) = occ_show_freq_1;
797 ssize_t (*show_power)(struct device *, struct device_attribute *,
798 char *) = occ_show_power_1;
799 ssize_t (*show_caps)(struct device *, struct device_attribute *,
800 char *) = occ_show_caps_1_2;
802 switch (sensors->temp.version) {
803 case 1:
804 num_attrs += (sensors->temp.num_sensors * 2);
805 break;
806 case 2:
807 num_attrs += (sensors->temp.num_sensors * 4);
808 show_temp = occ_show_temp_2;
809 break;
810 case 0x10:
811 num_attrs += (sensors->temp.num_sensors * 5);
812 show_temp = occ_show_temp_10;
813 break;
814 default:
815 sensors->temp.num_sensors = 0;
818 switch (sensors->freq.version) {
819 case 2:
820 show_freq = occ_show_freq_2;
821 fallthrough;
822 case 1:
823 num_attrs += (sensors->freq.num_sensors * 2);
824 break;
825 default:
826 sensors->freq.num_sensors = 0;
829 switch (sensors->power.version) {
830 case 2:
831 show_power = occ_show_power_2;
832 fallthrough;
833 case 1:
834 num_attrs += (sensors->power.num_sensors * 4);
835 break;
836 case 0xA0:
837 num_attrs += (sensors->power.num_sensors * 16);
838 show_power = occ_show_power_a0;
839 break;
840 default:
841 sensors->power.num_sensors = 0;
844 switch (sensors->caps.version) {
845 case 1:
846 num_attrs += (sensors->caps.num_sensors * 7);
847 break;
848 case 3:
849 show_caps = occ_show_caps_3;
850 fallthrough;
851 case 2:
852 num_attrs += (sensors->caps.num_sensors * 8);
853 break;
854 default:
855 sensors->caps.num_sensors = 0;
858 switch (sensors->extended.version) {
859 case 1:
860 num_attrs += (sensors->extended.num_sensors * 3);
861 break;
862 default:
863 sensors->extended.num_sensors = 0;
866 occ->attrs = devm_kzalloc(dev, sizeof(*occ->attrs) * num_attrs,
867 GFP_KERNEL);
868 if (!occ->attrs)
869 return -ENOMEM;
871 /* null-terminated list */
872 occ->group.attrs = devm_kzalloc(dev, sizeof(*occ->group.attrs) *
873 num_attrs + 1, GFP_KERNEL);
874 if (!occ->group.attrs)
875 return -ENOMEM;
877 attr = occ->attrs;
879 for (i = 0; i < sensors->temp.num_sensors; ++i) {
880 s = i + 1;
881 temp = ((struct temp_sensor_2 *)sensors->temp.data) + i;
883 snprintf(attr->name, sizeof(attr->name), "temp%d_label", s);
884 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
885 0, i);
886 attr++;
888 if (sensors->temp.version > 1 &&
889 temp->fru_type == OCC_FRU_TYPE_VRM) {
890 snprintf(attr->name, sizeof(attr->name),
891 "temp%d_alarm", s);
892 } else {
893 snprintf(attr->name, sizeof(attr->name),
894 "temp%d_input", s);
897 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
898 1, i);
899 attr++;
901 if (sensors->temp.version > 1) {
902 snprintf(attr->name, sizeof(attr->name),
903 "temp%d_fru_type", s);
904 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
905 show_temp, NULL, 2, i);
906 attr++;
908 snprintf(attr->name, sizeof(attr->name),
909 "temp%d_fault", s);
910 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
911 show_temp, NULL, 3, i);
912 attr++;
914 if (sensors->temp.version == 0x10) {
915 snprintf(attr->name, sizeof(attr->name),
916 "temp%d_max", s);
917 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
918 show_temp, NULL,
919 4, i);
920 attr++;
925 for (i = 0; i < sensors->freq.num_sensors; ++i) {
926 s = i + 1;
928 snprintf(attr->name, sizeof(attr->name), "freq%d_label", s);
929 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
930 0, i);
931 attr++;
933 snprintf(attr->name, sizeof(attr->name), "freq%d_input", s);
934 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
935 1, i);
936 attr++;
939 if (sensors->power.version == 0xA0) {
941 * Special case for many-attribute power sensor. Split it into
942 * a sensor number per power type, emulating several sensors.
944 for (i = 0; i < sensors->power.num_sensors; ++i) {
945 unsigned int j;
946 unsigned int nr = 0;
948 s = (i * 4) + 1;
950 for (j = 0; j < 4; ++j) {
951 snprintf(attr->name, sizeof(attr->name),
952 "power%d_label", s);
953 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
954 show_power, NULL,
955 nr++, i);
956 attr++;
958 snprintf(attr->name, sizeof(attr->name),
959 "power%d_average", s);
960 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
961 show_power, NULL,
962 nr++, i);
963 attr++;
965 snprintf(attr->name, sizeof(attr->name),
966 "power%d_average_interval", s);
967 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
968 show_power, NULL,
969 nr++, i);
970 attr++;
972 snprintf(attr->name, sizeof(attr->name),
973 "power%d_input", s);
974 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
975 show_power, NULL,
976 nr++, i);
977 attr++;
979 s++;
983 s = (sensors->power.num_sensors * 4) + 1;
984 } else {
985 for (i = 0; i < sensors->power.num_sensors; ++i) {
986 s = i + 1;
988 snprintf(attr->name, sizeof(attr->name),
989 "power%d_label", s);
990 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
991 show_power, NULL, 0, i);
992 attr++;
994 snprintf(attr->name, sizeof(attr->name),
995 "power%d_average", s);
996 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
997 show_power, NULL, 1, i);
998 attr++;
1000 snprintf(attr->name, sizeof(attr->name),
1001 "power%d_average_interval", s);
1002 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1003 show_power, NULL, 2, i);
1004 attr++;
1006 snprintf(attr->name, sizeof(attr->name),
1007 "power%d_input", s);
1008 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1009 show_power, NULL, 3, i);
1010 attr++;
1013 s = sensors->power.num_sensors + 1;
1016 if (sensors->caps.num_sensors >= 1) {
1017 snprintf(attr->name, sizeof(attr->name), "power%d_label", s);
1018 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1019 0, 0);
1020 attr++;
1022 snprintf(attr->name, sizeof(attr->name), "power%d_cap", s);
1023 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1024 1, 0);
1025 attr++;
1027 snprintf(attr->name, sizeof(attr->name), "power%d_input", s);
1028 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1029 2, 0);
1030 attr++;
1032 snprintf(attr->name, sizeof(attr->name),
1033 "power%d_cap_not_redundant", s);
1034 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1035 3, 0);
1036 attr++;
1038 snprintf(attr->name, sizeof(attr->name), "power%d_cap_max", s);
1039 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1040 4, 0);
1041 attr++;
1043 snprintf(attr->name, sizeof(attr->name), "power%d_cap_min", s);
1044 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1045 5, 0);
1046 attr++;
1048 snprintf(attr->name, sizeof(attr->name), "power%d_cap_user",
1050 attr->sensor = OCC_INIT_ATTR(attr->name, 0644, show_caps,
1051 occ_store_caps_user, 6, 0);
1052 attr++;
1054 if (sensors->caps.version > 1) {
1055 snprintf(attr->name, sizeof(attr->name),
1056 "power%d_cap_user_source", s);
1057 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1058 show_caps, NULL, 7, 0);
1059 attr++;
1063 for (i = 0; i < sensors->extended.num_sensors; ++i) {
1064 s = i + 1;
1066 snprintf(attr->name, sizeof(attr->name), "extn%d_label", s);
1067 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1068 occ_show_extended, NULL, 0, i);
1069 attr++;
1071 snprintf(attr->name, sizeof(attr->name), "extn%d_flags", s);
1072 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1073 occ_show_extended, NULL, 1, i);
1074 attr++;
1076 snprintf(attr->name, sizeof(attr->name), "extn%d_input", s);
1077 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1078 occ_show_extended, NULL, 2, i);
1079 attr++;
1082 /* put the sensors in the group */
1083 for (i = 0; i < num_attrs; ++i) {
1084 sysfs_attr_init(&occ->attrs[i].sensor.dev_attr.attr);
1085 occ->group.attrs[i] = &occ->attrs[i].sensor.dev_attr.attr;
1088 return 0;
1091 /* only need to do this once at startup, as OCC won't change sensors on us */
1092 static void occ_parse_poll_response(struct occ *occ)
1094 unsigned int i, old_offset, offset = 0, size = 0;
1095 struct occ_sensor *sensor;
1096 struct occ_sensors *sensors = &occ->sensors;
1097 struct occ_response *resp = &occ->resp;
1098 struct occ_poll_response *poll =
1099 (struct occ_poll_response *)&resp->data[0];
1100 struct occ_poll_response_header *header = &poll->header;
1101 struct occ_sensor_data_block *block = &poll->block;
1103 dev_info(occ->bus_dev, "OCC found, code level: %.16s\n",
1104 header->occ_code_level);
1106 for (i = 0; i < header->num_sensor_data_blocks; ++i) {
1107 block = (struct occ_sensor_data_block *)((u8 *)block + offset);
1108 old_offset = offset;
1109 offset = (block->header.num_sensors *
1110 block->header.sensor_length) + sizeof(block->header);
1111 size += offset;
1113 /* validate all the length/size fields */
1114 if ((size + sizeof(*header)) >= OCC_RESP_DATA_BYTES) {
1115 dev_warn(occ->bus_dev, "exceeded response buffer\n");
1116 return;
1119 dev_dbg(occ->bus_dev, " %04x..%04x: %.4s (%d sensors)\n",
1120 old_offset, offset - 1, block->header.eye_catcher,
1121 block->header.num_sensors);
1123 /* match sensor block type */
1124 if (strncmp(block->header.eye_catcher, "TEMP", 4) == 0)
1125 sensor = &sensors->temp;
1126 else if (strncmp(block->header.eye_catcher, "FREQ", 4) == 0)
1127 sensor = &sensors->freq;
1128 else if (strncmp(block->header.eye_catcher, "POWR", 4) == 0)
1129 sensor = &sensors->power;
1130 else if (strncmp(block->header.eye_catcher, "CAPS", 4) == 0)
1131 sensor = &sensors->caps;
1132 else if (strncmp(block->header.eye_catcher, "EXTN", 4) == 0)
1133 sensor = &sensors->extended;
1134 else {
1135 dev_warn(occ->bus_dev, "sensor not supported %.4s\n",
1136 block->header.eye_catcher);
1137 continue;
1140 sensor->num_sensors = block->header.num_sensors;
1141 sensor->version = block->header.sensor_format;
1142 sensor->data = &block->data;
1145 dev_dbg(occ->bus_dev, "Max resp size: %u+%zd=%zd\n", size,
1146 sizeof(*header), size + sizeof(*header));
1149 int occ_setup(struct occ *occ, const char *name)
1151 int rc;
1153 mutex_init(&occ->lock);
1154 occ->groups[0] = &occ->group;
1156 /* no need to lock */
1157 rc = occ_poll(occ);
1158 if (rc == -ESHUTDOWN) {
1159 dev_info(occ->bus_dev, "host is not ready\n");
1160 return rc;
1161 } else if (rc < 0) {
1162 dev_err(occ->bus_dev, "failed to get OCC poll response: %d\n",
1163 rc);
1164 return rc;
1167 occ_parse_poll_response(occ);
1169 rc = occ_setup_sensor_attrs(occ);
1170 if (rc) {
1171 dev_err(occ->bus_dev, "failed to setup sensor attrs: %d\n",
1172 rc);
1173 return rc;
1176 occ->hwmon = devm_hwmon_device_register_with_groups(occ->bus_dev, name,
1177 occ, occ->groups);
1178 if (IS_ERR(occ->hwmon)) {
1179 rc = PTR_ERR(occ->hwmon);
1180 dev_err(occ->bus_dev, "failed to register hwmon device: %d\n",
1181 rc);
1182 return rc;
1185 rc = occ_setup_sysfs(occ);
1186 if (rc)
1187 dev_err(occ->bus_dev, "failed to setup sysfs: %d\n", rc);
1189 return rc;
1191 EXPORT_SYMBOL_GPL(occ_setup);
1193 MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
1194 MODULE_DESCRIPTION("Common OCC hwmon code");
1195 MODULE_LICENSE("GPL");