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>
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 {
38 struct temp_sensor_2 {
44 struct temp_sensor_10 {
52 struct freq_sensor_1 {
57 struct freq_sensor_2 {
62 struct power_sensor_1 {
69 struct power_sensor_2 {
79 struct power_sensor_data {
85 struct power_sensor_data_and_time {
92 struct power_sensor_a0 {
94 struct power_sensor_data_and_time system;
96 struct power_sensor_data_and_time proc;
97 struct power_sensor_data vdd;
98 struct power_sensor_data vdn;
101 struct caps_sensor_2 {
111 struct caps_sensor_3 {
122 struct extended_sensor {
132 static int occ_poll(struct occ *occ)
135 u16 checksum = occ->poll_cmd_data + occ->seq_no + 1;
137 struct occ_poll_response_header *header;
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 */
149 /* mutex should already be locked if necessary */
150 rc = occ->send_cmd(occ, cmd);
152 occ->last_error = rc;
153 if (occ->error_count++ > OCC_ERROR_COUNT_THRESHOLD)
159 /* clear error since communication was successful */
160 occ->error_count = 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;
172 occ->last_safe = jiffies;
179 occ_sysfs_poll_done(occ);
183 static int occ_set_user_power_cap(struct occ *occ, u16 user_power_cap)
188 __be16 user_power_cap_be = cpu_to_be16(user_power_cap);
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);
205 rc = occ->send_cmd(occ, cmd);
207 mutex_unlock(&occ->lock);
212 int occ_update_response(struct occ *occ)
214 int rc = mutex_lock_interruptible(&occ->lock);
219 /* limit the maximum rate of polling the OCC */
220 if (time_after(jiffies, occ->next_update)) {
222 occ->next_update = jiffies + OCC_UPDATE_FREQUENCY;
224 rc = occ->last_error;
227 mutex_unlock(&occ->lock);
231 static ssize_t occ_show_temp_1(struct device *dev,
232 struct device_attribute *attr, char *buf)
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);
245 temp = ((struct temp_sensor_1 *)sensors->temp.data) + sattr->index;
249 val = get_unaligned_be16(&temp->sensor_id);
253 * If a sensor reading has expired and couldn't be refreshed,
254 * OCC returns 0xFFFF for that sensor.
256 if (temp->value == 0xFFFF)
258 val = get_unaligned_be16(&temp->value) * 1000;
264 return sysfs_emit(buf, "%u\n", val);
267 static ssize_t occ_show_temp_2(struct device *dev,
268 struct device_attribute *attr, char *buf)
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);
281 temp = ((struct temp_sensor_2 *)sensors->temp.data) + sattr->index;
285 val = get_unaligned_be32(&temp->sensor_id);
289 if (val == OCC_TEMP_SENSOR_FAULT)
293 * VRM doesn't return temperature, only alarm bit. This
294 * attribute maps to tempX_alarm instead of tempX_input for
297 if (temp->fru_type != OCC_FRU_TYPE_VRM) {
298 /* sensor not ready */
306 val = temp->fru_type;
309 val = temp->value == OCC_TEMP_SENSOR_FAULT;
315 return sysfs_emit(buf, "%u\n", val);
318 static ssize_t occ_show_temp_10(struct device *dev,
319 struct device_attribute *attr, char *buf)
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);
332 temp = ((struct temp_sensor_10 *)sensors->temp.data) + sattr->index;
336 val = get_unaligned_be32(&temp->sensor_id);
340 if (val == OCC_TEMP_SENSOR_FAULT)
344 * VRM doesn't return temperature, only alarm bit. This
345 * attribute maps to tempX_alarm instead of tempX_input for
348 if (temp->fru_type != OCC_FRU_TYPE_VRM) {
349 /* sensor not ready */
357 val = temp->fru_type;
360 val = temp->value == OCC_TEMP_SENSOR_FAULT;
363 val = temp->throttle * 1000;
369 return sysfs_emit(buf, "%u\n", val);
372 static ssize_t occ_show_freq_1(struct device *dev,
373 struct device_attribute *attr, char *buf)
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);
386 freq = ((struct freq_sensor_1 *)sensors->freq.data) + sattr->index;
390 val = get_unaligned_be16(&freq->sensor_id);
393 val = get_unaligned_be16(&freq->value);
399 return sysfs_emit(buf, "%u\n", val);
402 static ssize_t occ_show_freq_2(struct device *dev,
403 struct device_attribute *attr, char *buf)
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);
416 freq = ((struct freq_sensor_2 *)sensors->freq.data) + sattr->index;
420 val = get_unaligned_be32(&freq->sensor_id);
423 val = get_unaligned_be16(&freq->value);
429 return sysfs_emit(buf, "%u\n", val);
432 static ssize_t occ_show_power_1(struct device *dev,
433 struct device_attribute *attr, char *buf)
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);
446 power = ((struct power_sensor_1 *)sensors->power.data) + sattr->index;
450 val = get_unaligned_be16(&power->sensor_id);
453 val = get_unaligned_be32(&power->accumulator) /
454 get_unaligned_be32(&power->update_tag);
458 val = (u64)get_unaligned_be32(&power->update_tag) *
459 occ->powr_sample_time_us;
462 val = get_unaligned_be16(&power->value) * 1000000ULL;
468 return sysfs_emit(buf, "%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)
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);
493 power = ((struct power_sensor_2 *)sensors->power.data) + sattr->index;
497 return sysfs_emit(buf, "%u_%u_%u\n",
498 get_unaligned_be32(&power->sensor_id),
499 power->function_id, power->apss_channel);
501 val = occ_get_powr_avg(&power->accumulator,
505 val = (u64)get_unaligned_be32(&power->update_tag) *
506 occ->powr_sample_time_us;
509 val = get_unaligned_be16(&power->value) * 1000000ULL;
515 return sysfs_emit(buf, "%llu\n", val);
518 static ssize_t occ_show_power_a0(struct device *dev,
519 struct device_attribute *attr, char *buf)
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);
532 power = ((struct power_sensor_a0 *)sensors->power.data) + sattr->index;
536 return sysfs_emit(buf, "%u_system\n",
537 get_unaligned_be32(&power->sensor_id));
539 val = occ_get_powr_avg(&power->system.accumulator,
540 &power->system.update_tag);
543 val = (u64)get_unaligned_be32(&power->system.update_tag) *
544 occ->powr_sample_time_us;
547 val = get_unaligned_be16(&power->system.value) * 1000000ULL;
550 return sysfs_emit(buf, "%u_proc\n",
551 get_unaligned_be32(&power->sensor_id));
553 val = occ_get_powr_avg(&power->proc.accumulator,
554 &power->proc.update_tag);
557 val = (u64)get_unaligned_be32(&power->proc.update_tag) *
558 occ->powr_sample_time_us;
561 val = get_unaligned_be16(&power->proc.value) * 1000000ULL;
564 return sysfs_emit(buf, "%u_vdd\n",
565 get_unaligned_be32(&power->sensor_id));
567 val = occ_get_powr_avg(&power->vdd.accumulator,
568 &power->vdd.update_tag);
571 val = (u64)get_unaligned_be32(&power->vdd.update_tag) *
572 occ->powr_sample_time_us;
575 val = get_unaligned_be16(&power->vdd.value) * 1000000ULL;
578 return sysfs_emit(buf, "%u_vdn\n",
579 get_unaligned_be32(&power->sensor_id));
581 val = occ_get_powr_avg(&power->vdn.accumulator,
582 &power->vdn.update_tag);
585 val = (u64)get_unaligned_be32(&power->vdn.update_tag) *
586 occ->powr_sample_time_us;
589 val = get_unaligned_be16(&power->vdn.value) * 1000000ULL;
595 return sysfs_emit(buf, "%llu\n", val);
598 static ssize_t occ_show_caps_1_2(struct device *dev,
599 struct device_attribute *attr, char *buf)
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);
612 caps = ((struct caps_sensor_2 *)sensors->caps.data) + sattr->index;
616 return sysfs_emit(buf, "system\n");
618 val = get_unaligned_be16(&caps->cap) * 1000000ULL;
621 val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
624 val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
627 val = get_unaligned_be16(&caps->max) * 1000000ULL;
630 val = get_unaligned_be16(&caps->min) * 1000000ULL;
633 val = get_unaligned_be16(&caps->user) * 1000000ULL;
636 if (occ->sensors.caps.version == 1)
639 val = caps->user_source;
645 return sysfs_emit(buf, "%llu\n", val);
648 static ssize_t occ_show_caps_3(struct device *dev,
649 struct device_attribute *attr, char *buf)
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);
662 caps = ((struct caps_sensor_3 *)sensors->caps.data) + sattr->index;
666 return sysfs_emit(buf, "system\n");
668 val = get_unaligned_be16(&caps->cap) * 1000000ULL;
671 val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
674 val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
677 val = get_unaligned_be16(&caps->max) * 1000000ULL;
680 val = get_unaligned_be16(&caps->hard_min) * 1000000ULL;
683 val = get_unaligned_be16(&caps->user) * 1000000ULL;
686 val = caps->user_source;
692 return sysfs_emit(buf, "%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)
701 unsigned long long value;
702 struct occ *occ = dev_get_drvdata(dev);
704 rc = kstrtoull(buf, 0, &value);
708 user_power_cap = div64_u64(value, 1000000ULL); /* microwatt to watt */
710 rc = occ_set_user_power_cap(occ, user_power_cap);
717 static ssize_t occ_show_extended(struct device *dev,
718 struct device_attribute *attr, char *buf)
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);
730 extn = ((struct extended_sensor *)sensors->extended.data) +
735 if (extn->flags & EXTN_FLAG_SENSOR_ID) {
736 rc = sysfs_emit(buf, "%u",
737 get_unaligned_be32(&extn->sensor_id));
739 rc = sysfs_emit(buf, "%02x%02x%02x%02x\n",
740 extn->name[0], extn->name[1],
741 extn->name[2], extn->name[3]);
745 rc = sysfs_emit(buf, "%02x\n", extn->flags);
748 rc = sysfs_emit(buf, "%02x%02x%02x%02x%02x%02x\n",
749 extn->data[0], extn->data[1], extn->data[2],
750 extn->data[3], extn->data[4], extn->data[5]);
760 * Some helper macros to make it easier to define an occ_attribute. Since these
761 * are dynamically allocated, we shouldn't use the existing kernel macros which
762 * stringify the name argument.
764 #define ATTR_OCC(_name, _mode, _show, _store) { \
767 .mode = VERIFY_OCTAL_PERMISSIONS(_mode), \
773 #define SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index) { \
774 .dev_attr = ATTR_OCC(_name, _mode, _show, _store), \
779 #define OCC_INIT_ATTR(_name, _mode, _show, _store, _nr, _index) \
780 ((struct sensor_device_attribute_2) \
781 SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index))
784 * Allocate and instatiate sensor_device_attribute_2s. It's most efficient to
785 * use our own instead of the built-in hwmon attribute types.
787 static int occ_setup_sensor_attrs(struct occ *occ)
789 unsigned int i, s, num_attrs = 0;
790 struct device *dev = occ->bus_dev;
791 struct occ_sensors *sensors = &occ->sensors;
792 struct occ_attribute *attr;
793 struct temp_sensor_2 *temp;
794 ssize_t (*show_temp)(struct device *, struct device_attribute *,
795 char *) = occ_show_temp_1;
796 ssize_t (*show_freq)(struct device *, struct device_attribute *,
797 char *) = occ_show_freq_1;
798 ssize_t (*show_power)(struct device *, struct device_attribute *,
799 char *) = occ_show_power_1;
800 ssize_t (*show_caps)(struct device *, struct device_attribute *,
801 char *) = occ_show_caps_1_2;
803 switch (sensors->temp.version) {
805 num_attrs += (sensors->temp.num_sensors * 2);
808 num_attrs += (sensors->temp.num_sensors * 4);
809 show_temp = occ_show_temp_2;
812 num_attrs += (sensors->temp.num_sensors * 5);
813 show_temp = occ_show_temp_10;
816 sensors->temp.num_sensors = 0;
819 switch (sensors->freq.version) {
821 show_freq = occ_show_freq_2;
824 num_attrs += (sensors->freq.num_sensors * 2);
827 sensors->freq.num_sensors = 0;
830 switch (sensors->power.version) {
832 show_power = occ_show_power_2;
835 num_attrs += (sensors->power.num_sensors * 4);
838 num_attrs += (sensors->power.num_sensors * 16);
839 show_power = occ_show_power_a0;
842 sensors->power.num_sensors = 0;
845 switch (sensors->caps.version) {
847 num_attrs += (sensors->caps.num_sensors * 7);
850 show_caps = occ_show_caps_3;
853 num_attrs += (sensors->caps.num_sensors * 8);
856 sensors->caps.num_sensors = 0;
859 switch (sensors->extended.version) {
861 num_attrs += (sensors->extended.num_sensors * 3);
864 sensors->extended.num_sensors = 0;
867 occ->attrs = devm_kzalloc(dev, sizeof(*occ->attrs) * num_attrs,
872 /* null-terminated list */
873 occ->group.attrs = devm_kzalloc(dev, sizeof(*occ->group.attrs) *
874 num_attrs + 1, GFP_KERNEL);
875 if (!occ->group.attrs)
880 for (i = 0; i < sensors->temp.num_sensors; ++i) {
882 temp = ((struct temp_sensor_2 *)sensors->temp.data) + i;
884 snprintf(attr->name, sizeof(attr->name), "temp%d_label", s);
885 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
889 if (sensors->temp.version > 1 &&
890 temp->fru_type == OCC_FRU_TYPE_VRM) {
891 snprintf(attr->name, sizeof(attr->name),
894 snprintf(attr->name, sizeof(attr->name),
898 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
902 if (sensors->temp.version > 1) {
903 snprintf(attr->name, sizeof(attr->name),
904 "temp%d_fru_type", s);
905 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
906 show_temp, NULL, 2, i);
909 snprintf(attr->name, sizeof(attr->name),
911 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
912 show_temp, NULL, 3, i);
915 if (sensors->temp.version == 0x10) {
916 snprintf(attr->name, sizeof(attr->name),
918 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
926 for (i = 0; i < sensors->freq.num_sensors; ++i) {
929 snprintf(attr->name, sizeof(attr->name), "freq%d_label", s);
930 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
934 snprintf(attr->name, sizeof(attr->name), "freq%d_input", s);
935 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
940 if (sensors->power.version == 0xA0) {
942 * Special case for many-attribute power sensor. Split it into
943 * a sensor number per power type, emulating several sensors.
945 for (i = 0; i < sensors->power.num_sensors; ++i) {
951 for (j = 0; j < 4; ++j) {
952 snprintf(attr->name, sizeof(attr->name),
954 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
959 snprintf(attr->name, sizeof(attr->name),
960 "power%d_average", s);
961 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
966 snprintf(attr->name, sizeof(attr->name),
967 "power%d_average_interval", s);
968 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
973 snprintf(attr->name, sizeof(attr->name),
975 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
984 s = (sensors->power.num_sensors * 4) + 1;
986 for (i = 0; i < sensors->power.num_sensors; ++i) {
989 snprintf(attr->name, sizeof(attr->name),
991 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
992 show_power, NULL, 0, i);
995 snprintf(attr->name, sizeof(attr->name),
996 "power%d_average", s);
997 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
998 show_power, NULL, 1, i);
1001 snprintf(attr->name, sizeof(attr->name),
1002 "power%d_average_interval", s);
1003 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1004 show_power, NULL, 2, i);
1007 snprintf(attr->name, sizeof(attr->name),
1008 "power%d_input", s);
1009 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1010 show_power, NULL, 3, i);
1014 s = sensors->power.num_sensors + 1;
1017 if (sensors->caps.num_sensors >= 1) {
1018 snprintf(attr->name, sizeof(attr->name), "power%d_label", s);
1019 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1023 snprintf(attr->name, sizeof(attr->name), "power%d_cap", s);
1024 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1028 snprintf(attr->name, sizeof(attr->name), "power%d_input", s);
1029 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1033 snprintf(attr->name, sizeof(attr->name),
1034 "power%d_cap_not_redundant", s);
1035 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1039 snprintf(attr->name, sizeof(attr->name), "power%d_cap_max", s);
1040 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1044 snprintf(attr->name, sizeof(attr->name), "power%d_cap_min", s);
1045 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1049 snprintf(attr->name, sizeof(attr->name), "power%d_cap_user",
1051 attr->sensor = OCC_INIT_ATTR(attr->name, 0644, show_caps,
1052 occ_store_caps_user, 6, 0);
1055 if (sensors->caps.version > 1) {
1056 snprintf(attr->name, sizeof(attr->name),
1057 "power%d_cap_user_source", s);
1058 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1059 show_caps, NULL, 7, 0);
1064 for (i = 0; i < sensors->extended.num_sensors; ++i) {
1067 snprintf(attr->name, sizeof(attr->name), "extn%d_label", s);
1068 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1069 occ_show_extended, NULL, 0, i);
1072 snprintf(attr->name, sizeof(attr->name), "extn%d_flags", s);
1073 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1074 occ_show_extended, NULL, 1, i);
1077 snprintf(attr->name, sizeof(attr->name), "extn%d_input", s);
1078 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1079 occ_show_extended, NULL, 2, i);
1083 /* put the sensors in the group */
1084 for (i = 0; i < num_attrs; ++i) {
1085 sysfs_attr_init(&occ->attrs[i].sensor.dev_attr.attr);
1086 occ->group.attrs[i] = &occ->attrs[i].sensor.dev_attr.attr;
1092 /* only need to do this once at startup, as OCC won't change sensors on us */
1093 static void occ_parse_poll_response(struct occ *occ)
1095 unsigned int i, old_offset, offset = 0, size = 0;
1096 struct occ_sensor *sensor;
1097 struct occ_sensors *sensors = &occ->sensors;
1098 struct occ_response *resp = &occ->resp;
1099 struct occ_poll_response *poll =
1100 (struct occ_poll_response *)&resp->data[0];
1101 struct occ_poll_response_header *header = &poll->header;
1102 struct occ_sensor_data_block *block = &poll->block;
1104 dev_info(occ->bus_dev, "OCC found, code level: %.16s\n",
1105 header->occ_code_level);
1107 for (i = 0; i < header->num_sensor_data_blocks; ++i) {
1108 block = (struct occ_sensor_data_block *)((u8 *)block + offset);
1109 old_offset = offset;
1110 offset = (block->header.num_sensors *
1111 block->header.sensor_length) + sizeof(block->header);
1114 /* validate all the length/size fields */
1115 if ((size + sizeof(*header)) >= OCC_RESP_DATA_BYTES) {
1116 dev_warn(occ->bus_dev, "exceeded response buffer\n");
1120 dev_dbg(occ->bus_dev, " %04x..%04x: %.4s (%d sensors)\n",
1121 old_offset, offset - 1, block->header.eye_catcher,
1122 block->header.num_sensors);
1124 /* match sensor block type */
1125 if (strncmp(block->header.eye_catcher, "TEMP", 4) == 0)
1126 sensor = &sensors->temp;
1127 else if (strncmp(block->header.eye_catcher, "FREQ", 4) == 0)
1128 sensor = &sensors->freq;
1129 else if (strncmp(block->header.eye_catcher, "POWR", 4) == 0)
1130 sensor = &sensors->power;
1131 else if (strncmp(block->header.eye_catcher, "CAPS", 4) == 0)
1132 sensor = &sensors->caps;
1133 else if (strncmp(block->header.eye_catcher, "EXTN", 4) == 0)
1134 sensor = &sensors->extended;
1136 dev_warn(occ->bus_dev, "sensor not supported %.4s\n",
1137 block->header.eye_catcher);
1141 sensor->num_sensors = block->header.num_sensors;
1142 sensor->version = block->header.sensor_format;
1143 sensor->data = &block->data;
1146 dev_dbg(occ->bus_dev, "Max resp size: %u+%zd=%zd\n", size,
1147 sizeof(*header), size + sizeof(*header));
1150 int occ_setup(struct occ *occ, const char *name)
1154 /* start with 1 to avoid false match with zero-initialized SRAM buffer */
1156 mutex_init(&occ->lock);
1157 occ->groups[0] = &occ->group;
1159 /* no need to lock */
1161 if (rc == -ESHUTDOWN) {
1162 dev_info(occ->bus_dev, "host is not ready\n");
1164 } else if (rc < 0) {
1165 dev_err(occ->bus_dev,
1166 "failed to get OCC poll response=%02x: %d\n",
1167 occ->resp.return_status, rc);
1171 occ->next_update = jiffies + OCC_UPDATE_FREQUENCY;
1172 occ_parse_poll_response(occ);
1174 rc = occ_setup_sensor_attrs(occ);
1176 dev_err(occ->bus_dev, "failed to setup sensor attrs: %d\n",
1181 occ->hwmon = devm_hwmon_device_register_with_groups(occ->bus_dev, name,
1183 if (IS_ERR(occ->hwmon)) {
1184 rc = PTR_ERR(occ->hwmon);
1185 dev_err(occ->bus_dev, "failed to register hwmon device: %d\n",
1190 rc = occ_setup_sysfs(occ);
1192 dev_err(occ->bus_dev, "failed to setup sysfs: %d\n", rc);
1196 EXPORT_SYMBOL_GPL(occ_setup);
1198 MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
1199 MODULE_DESCRIPTION("Common OCC hwmon code");
1200 MODULE_LICENSE("GPL");