1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright IBM Corp 2019
4 #include <linux/device.h>
5 #include <linux/hwmon.h>
6 #include <linux/hwmon-sysfs.h>
7 #include <linux/jiffies.h>
8 #include <linux/kernel.h>
9 #include <linux/math64.h>
10 #include <linux/mutex.h>
11 #include <linux/sysfs.h>
12 #include <asm/unaligned.h>
16 #define EXTN_FLAG_SENSOR_ID BIT(7)
18 #define OCC_ERROR_COUNT_THRESHOLD 2 /* required by OCC spec */
20 #define OCC_STATE_SAFE 4
21 #define OCC_SAFE_TIMEOUT msecs_to_jiffies(60000) /* 1 min */
23 #define OCC_UPDATE_FREQUENCY msecs_to_jiffies(1000)
25 #define OCC_TEMP_SENSOR_FAULT 0xFF
27 #define OCC_FRU_TYPE_VRM 3
29 /* OCC sensor type and version definitions */
31 struct temp_sensor_1 {
36 struct temp_sensor_2 {
42 struct freq_sensor_1 {
47 struct freq_sensor_2 {
52 struct power_sensor_1 {
59 struct power_sensor_2 {
69 struct power_sensor_data {
75 struct power_sensor_data_and_time {
82 struct power_sensor_a0 {
84 struct power_sensor_data_and_time system;
86 struct power_sensor_data_and_time proc;
87 struct power_sensor_data vdd;
88 struct power_sensor_data vdn;
91 struct caps_sensor_2 {
101 struct caps_sensor_3 {
112 struct extended_sensor {
122 static int occ_poll(struct occ *occ)
125 u16 checksum = occ->poll_cmd_data + 1;
127 struct occ_poll_response_header *header;
130 cmd[0] = 0; /* sequence number */
131 cmd[1] = 0; /* cmd type */
132 cmd[2] = 0; /* data length msb */
133 cmd[3] = 1; /* data length lsb */
134 cmd[4] = occ->poll_cmd_data; /* data */
135 cmd[5] = checksum >> 8; /* checksum msb */
136 cmd[6] = checksum & 0xFF; /* checksum lsb */
139 /* mutex should already be locked if necessary */
140 rc = occ->send_cmd(occ, cmd);
142 if (occ->error_count++ > OCC_ERROR_COUNT_THRESHOLD)
148 /* clear error since communication was successful */
149 occ->error_count = 0;
152 /* check for safe state */
153 header = (struct occ_poll_response_header *)occ->resp.data;
154 if (header->occ_state == OCC_STATE_SAFE) {
155 if (occ->last_safe) {
156 if (time_after(jiffies,
157 occ->last_safe + OCC_SAFE_TIMEOUT))
158 occ->error = -EHOSTDOWN;
160 occ->last_safe = jiffies;
167 occ_sysfs_poll_done(occ);
171 static int occ_set_user_power_cap(struct occ *occ, u16 user_power_cap)
176 __be16 user_power_cap_be = cpu_to_be16(user_power_cap);
183 memcpy(&cmd[4], &user_power_cap_be, 2);
185 checksum += cmd[4] + cmd[5];
186 cmd[6] = checksum >> 8;
187 cmd[7] = checksum & 0xFF;
189 rc = mutex_lock_interruptible(&occ->lock);
193 rc = occ->send_cmd(occ, cmd);
195 mutex_unlock(&occ->lock);
200 int occ_update_response(struct occ *occ)
202 int rc = mutex_lock_interruptible(&occ->lock);
207 /* limit the maximum rate of polling the OCC */
208 if (time_after(jiffies, occ->last_update + OCC_UPDATE_FREQUENCY)) {
210 occ->last_update = jiffies;
213 mutex_unlock(&occ->lock);
217 static ssize_t occ_show_temp_1(struct device *dev,
218 struct device_attribute *attr, char *buf)
222 struct temp_sensor_1 *temp;
223 struct occ *occ = dev_get_drvdata(dev);
224 struct occ_sensors *sensors = &occ->sensors;
225 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
227 rc = occ_update_response(occ);
231 temp = ((struct temp_sensor_1 *)sensors->temp.data) + sattr->index;
235 val = get_unaligned_be16(&temp->sensor_id);
238 val = get_unaligned_be16(&temp->value) * 1000;
244 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
247 static ssize_t occ_show_temp_2(struct device *dev,
248 struct device_attribute *attr, char *buf)
252 struct temp_sensor_2 *temp;
253 struct occ *occ = dev_get_drvdata(dev);
254 struct occ_sensors *sensors = &occ->sensors;
255 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
257 rc = occ_update_response(occ);
261 temp = ((struct temp_sensor_2 *)sensors->temp.data) + sattr->index;
265 val = get_unaligned_be32(&temp->sensor_id);
269 if (val == OCC_TEMP_SENSOR_FAULT)
273 * VRM doesn't return temperature, only alarm bit. This
274 * attribute maps to tempX_alarm instead of tempX_input for
277 if (temp->fru_type != OCC_FRU_TYPE_VRM) {
278 /* sensor not ready */
286 val = temp->fru_type;
289 val = temp->value == OCC_TEMP_SENSOR_FAULT;
295 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
298 static ssize_t occ_show_freq_1(struct device *dev,
299 struct device_attribute *attr, char *buf)
303 struct freq_sensor_1 *freq;
304 struct occ *occ = dev_get_drvdata(dev);
305 struct occ_sensors *sensors = &occ->sensors;
306 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
308 rc = occ_update_response(occ);
312 freq = ((struct freq_sensor_1 *)sensors->freq.data) + sattr->index;
316 val = get_unaligned_be16(&freq->sensor_id);
319 val = get_unaligned_be16(&freq->value);
325 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
328 static ssize_t occ_show_freq_2(struct device *dev,
329 struct device_attribute *attr, char *buf)
333 struct freq_sensor_2 *freq;
334 struct occ *occ = dev_get_drvdata(dev);
335 struct occ_sensors *sensors = &occ->sensors;
336 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
338 rc = occ_update_response(occ);
342 freq = ((struct freq_sensor_2 *)sensors->freq.data) + sattr->index;
346 val = get_unaligned_be32(&freq->sensor_id);
349 val = get_unaligned_be16(&freq->value);
355 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
358 static ssize_t occ_show_power_1(struct device *dev,
359 struct device_attribute *attr, char *buf)
363 struct power_sensor_1 *power;
364 struct occ *occ = dev_get_drvdata(dev);
365 struct occ_sensors *sensors = &occ->sensors;
366 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
368 rc = occ_update_response(occ);
372 power = ((struct power_sensor_1 *)sensors->power.data) + sattr->index;
376 val = get_unaligned_be16(&power->sensor_id);
379 val = get_unaligned_be32(&power->accumulator) /
380 get_unaligned_be32(&power->update_tag);
384 val = (u64)get_unaligned_be32(&power->update_tag) *
385 occ->powr_sample_time_us;
388 val = get_unaligned_be16(&power->value) * 1000000ULL;
394 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
397 static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
399 return div64_u64(get_unaligned_be64(accum) * 1000000ULL,
400 get_unaligned_be32(samples));
403 static ssize_t occ_show_power_2(struct device *dev,
404 struct device_attribute *attr, char *buf)
408 struct power_sensor_2 *power;
409 struct occ *occ = dev_get_drvdata(dev);
410 struct occ_sensors *sensors = &occ->sensors;
411 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
413 rc = occ_update_response(occ);
417 power = ((struct power_sensor_2 *)sensors->power.data) + sattr->index;
421 return snprintf(buf, PAGE_SIZE - 1, "%u_%u_%u\n",
422 get_unaligned_be32(&power->sensor_id),
423 power->function_id, power->apss_channel);
425 val = occ_get_powr_avg(&power->accumulator,
429 val = (u64)get_unaligned_be32(&power->update_tag) *
430 occ->powr_sample_time_us;
433 val = get_unaligned_be16(&power->value) * 1000000ULL;
439 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
442 static ssize_t occ_show_power_a0(struct device *dev,
443 struct device_attribute *attr, char *buf)
447 struct power_sensor_a0 *power;
448 struct occ *occ = dev_get_drvdata(dev);
449 struct occ_sensors *sensors = &occ->sensors;
450 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
452 rc = occ_update_response(occ);
456 power = ((struct power_sensor_a0 *)sensors->power.data) + sattr->index;
460 return snprintf(buf, PAGE_SIZE - 1, "%u_system\n",
461 get_unaligned_be32(&power->sensor_id));
463 val = occ_get_powr_avg(&power->system.accumulator,
464 &power->system.update_tag);
467 val = (u64)get_unaligned_be32(&power->system.update_tag) *
468 occ->powr_sample_time_us;
471 val = get_unaligned_be16(&power->system.value) * 1000000ULL;
474 return snprintf(buf, PAGE_SIZE - 1, "%u_proc\n",
475 get_unaligned_be32(&power->sensor_id));
477 val = occ_get_powr_avg(&power->proc.accumulator,
478 &power->proc.update_tag);
481 val = (u64)get_unaligned_be32(&power->proc.update_tag) *
482 occ->powr_sample_time_us;
485 val = get_unaligned_be16(&power->proc.value) * 1000000ULL;
488 return snprintf(buf, PAGE_SIZE - 1, "%u_vdd\n",
489 get_unaligned_be32(&power->sensor_id));
491 val = occ_get_powr_avg(&power->vdd.accumulator,
492 &power->vdd.update_tag);
495 val = (u64)get_unaligned_be32(&power->vdd.update_tag) *
496 occ->powr_sample_time_us;
499 val = get_unaligned_be16(&power->vdd.value) * 1000000ULL;
502 return snprintf(buf, PAGE_SIZE - 1, "%u_vdn\n",
503 get_unaligned_be32(&power->sensor_id));
505 val = occ_get_powr_avg(&power->vdn.accumulator,
506 &power->vdn.update_tag);
509 val = (u64)get_unaligned_be32(&power->vdn.update_tag) *
510 occ->powr_sample_time_us;
513 val = get_unaligned_be16(&power->vdn.value) * 1000000ULL;
519 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
522 static ssize_t occ_show_caps_1_2(struct device *dev,
523 struct device_attribute *attr, char *buf)
527 struct caps_sensor_2 *caps;
528 struct occ *occ = dev_get_drvdata(dev);
529 struct occ_sensors *sensors = &occ->sensors;
530 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
532 rc = occ_update_response(occ);
536 caps = ((struct caps_sensor_2 *)sensors->caps.data) + sattr->index;
540 return snprintf(buf, PAGE_SIZE - 1, "system\n");
542 val = get_unaligned_be16(&caps->cap) * 1000000ULL;
545 val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
548 val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
551 val = get_unaligned_be16(&caps->max) * 1000000ULL;
554 val = get_unaligned_be16(&caps->min) * 1000000ULL;
557 val = get_unaligned_be16(&caps->user) * 1000000ULL;
560 if (occ->sensors.caps.version == 1)
563 val = caps->user_source;
569 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
572 static ssize_t occ_show_caps_3(struct device *dev,
573 struct device_attribute *attr, char *buf)
577 struct caps_sensor_3 *caps;
578 struct occ *occ = dev_get_drvdata(dev);
579 struct occ_sensors *sensors = &occ->sensors;
580 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
582 rc = occ_update_response(occ);
586 caps = ((struct caps_sensor_3 *)sensors->caps.data) + sattr->index;
590 return snprintf(buf, PAGE_SIZE - 1, "system\n");
592 val = get_unaligned_be16(&caps->cap) * 1000000ULL;
595 val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
598 val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
601 val = get_unaligned_be16(&caps->max) * 1000000ULL;
604 val = get_unaligned_be16(&caps->hard_min) * 1000000ULL;
607 val = get_unaligned_be16(&caps->user) * 1000000ULL;
610 val = caps->user_source;
616 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
619 static ssize_t occ_store_caps_user(struct device *dev,
620 struct device_attribute *attr,
621 const char *buf, size_t count)
625 unsigned long long value;
626 struct occ *occ = dev_get_drvdata(dev);
628 rc = kstrtoull(buf, 0, &value);
632 user_power_cap = div64_u64(value, 1000000ULL); /* microwatt to watt */
634 rc = occ_set_user_power_cap(occ, user_power_cap);
641 static ssize_t occ_show_extended(struct device *dev,
642 struct device_attribute *attr, char *buf)
645 struct extended_sensor *extn;
646 struct occ *occ = dev_get_drvdata(dev);
647 struct occ_sensors *sensors = &occ->sensors;
648 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
650 rc = occ_update_response(occ);
654 extn = ((struct extended_sensor *)sensors->extended.data) +
659 if (extn->flags & EXTN_FLAG_SENSOR_ID)
660 rc = snprintf(buf, PAGE_SIZE - 1, "%u",
661 get_unaligned_be32(&extn->sensor_id));
663 rc = snprintf(buf, PAGE_SIZE - 1, "%02x%02x%02x%02x\n",
664 extn->name[0], extn->name[1],
665 extn->name[2], extn->name[3]);
668 rc = snprintf(buf, PAGE_SIZE - 1, "%02x\n", extn->flags);
671 rc = snprintf(buf, PAGE_SIZE - 1, "%02x%02x%02x%02x%02x%02x\n",
672 extn->data[0], extn->data[1], extn->data[2],
673 extn->data[3], extn->data[4], extn->data[5]);
683 * Some helper macros to make it easier to define an occ_attribute. Since these
684 * are dynamically allocated, we shouldn't use the existing kernel macros which
685 * stringify the name argument.
687 #define ATTR_OCC(_name, _mode, _show, _store) { \
690 .mode = VERIFY_OCTAL_PERMISSIONS(_mode), \
696 #define SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index) { \
697 .dev_attr = ATTR_OCC(_name, _mode, _show, _store), \
702 #define OCC_INIT_ATTR(_name, _mode, _show, _store, _nr, _index) \
703 ((struct sensor_device_attribute_2) \
704 SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index))
707 * Allocate and instatiate sensor_device_attribute_2s. It's most efficient to
708 * use our own instead of the built-in hwmon attribute types.
710 static int occ_setup_sensor_attrs(struct occ *occ)
712 unsigned int i, s, num_attrs = 0;
713 struct device *dev = occ->bus_dev;
714 struct occ_sensors *sensors = &occ->sensors;
715 struct occ_attribute *attr;
716 struct temp_sensor_2 *temp;
717 ssize_t (*show_temp)(struct device *, struct device_attribute *,
718 char *) = occ_show_temp_1;
719 ssize_t (*show_freq)(struct device *, struct device_attribute *,
720 char *) = occ_show_freq_1;
721 ssize_t (*show_power)(struct device *, struct device_attribute *,
722 char *) = occ_show_power_1;
723 ssize_t (*show_caps)(struct device *, struct device_attribute *,
724 char *) = occ_show_caps_1_2;
726 switch (sensors->temp.version) {
728 num_attrs += (sensors->temp.num_sensors * 2);
731 num_attrs += (sensors->temp.num_sensors * 4);
732 show_temp = occ_show_temp_2;
735 sensors->temp.num_sensors = 0;
738 switch (sensors->freq.version) {
740 show_freq = occ_show_freq_2;
743 num_attrs += (sensors->freq.num_sensors * 2);
746 sensors->freq.num_sensors = 0;
749 switch (sensors->power.version) {
751 show_power = occ_show_power_2;
754 num_attrs += (sensors->power.num_sensors * 4);
757 num_attrs += (sensors->power.num_sensors * 16);
758 show_power = occ_show_power_a0;
761 sensors->power.num_sensors = 0;
764 switch (sensors->caps.version) {
766 num_attrs += (sensors->caps.num_sensors * 7);
769 show_caps = occ_show_caps_3;
772 num_attrs += (sensors->caps.num_sensors * 8);
775 sensors->caps.num_sensors = 0;
778 switch (sensors->extended.version) {
780 num_attrs += (sensors->extended.num_sensors * 3);
783 sensors->extended.num_sensors = 0;
786 occ->attrs = devm_kzalloc(dev, sizeof(*occ->attrs) * num_attrs,
791 /* null-terminated list */
792 occ->group.attrs = devm_kzalloc(dev, sizeof(*occ->group.attrs) *
793 num_attrs + 1, GFP_KERNEL);
794 if (!occ->group.attrs)
799 for (i = 0; i < sensors->temp.num_sensors; ++i) {
801 temp = ((struct temp_sensor_2 *)sensors->temp.data) + i;
803 snprintf(attr->name, sizeof(attr->name), "temp%d_label", s);
804 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
808 if (sensors->temp.version > 1 &&
809 temp->fru_type == OCC_FRU_TYPE_VRM) {
810 snprintf(attr->name, sizeof(attr->name),
813 snprintf(attr->name, sizeof(attr->name),
817 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
821 if (sensors->temp.version > 1) {
822 snprintf(attr->name, sizeof(attr->name),
823 "temp%d_fru_type", s);
824 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
825 show_temp, NULL, 2, i);
828 snprintf(attr->name, sizeof(attr->name),
830 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
831 show_temp, NULL, 3, i);
836 for (i = 0; i < sensors->freq.num_sensors; ++i) {
839 snprintf(attr->name, sizeof(attr->name), "freq%d_label", s);
840 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
844 snprintf(attr->name, sizeof(attr->name), "freq%d_input", s);
845 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
850 if (sensors->power.version == 0xA0) {
852 * Special case for many-attribute power sensor. Split it into
853 * a sensor number per power type, emulating several sensors.
855 for (i = 0; i < sensors->power.num_sensors; ++i) {
861 for (j = 0; j < 4; ++j) {
862 snprintf(attr->name, sizeof(attr->name),
864 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
869 snprintf(attr->name, sizeof(attr->name),
870 "power%d_average", s);
871 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
876 snprintf(attr->name, sizeof(attr->name),
877 "power%d_average_interval", s);
878 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
883 snprintf(attr->name, sizeof(attr->name),
885 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
894 for (i = 0; i < sensors->power.num_sensors; ++i) {
897 snprintf(attr->name, sizeof(attr->name),
899 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
900 show_power, NULL, 0, i);
903 snprintf(attr->name, sizeof(attr->name),
904 "power%d_average", s);
905 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
906 show_power, NULL, 1, i);
909 snprintf(attr->name, sizeof(attr->name),
910 "power%d_average_interval", s);
911 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
912 show_power, NULL, 2, i);
915 snprintf(attr->name, sizeof(attr->name),
917 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
918 show_power, NULL, 3, i);
923 if (sensors->caps.num_sensors >= 1) {
924 s = sensors->power.num_sensors + 1;
926 snprintf(attr->name, sizeof(attr->name), "power%d_label", s);
927 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
931 snprintf(attr->name, sizeof(attr->name), "power%d_cap", s);
932 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
936 snprintf(attr->name, sizeof(attr->name), "power%d_input", s);
937 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
941 snprintf(attr->name, sizeof(attr->name),
942 "power%d_cap_not_redundant", s);
943 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
947 snprintf(attr->name, sizeof(attr->name), "power%d_cap_max", s);
948 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
952 snprintf(attr->name, sizeof(attr->name), "power%d_cap_min", s);
953 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
957 snprintf(attr->name, sizeof(attr->name), "power%d_cap_user",
959 attr->sensor = OCC_INIT_ATTR(attr->name, 0644, show_caps,
960 occ_store_caps_user, 6, 0);
963 if (sensors->caps.version > 1) {
964 snprintf(attr->name, sizeof(attr->name),
965 "power%d_cap_user_source", s);
966 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
967 show_caps, NULL, 7, 0);
972 for (i = 0; i < sensors->extended.num_sensors; ++i) {
975 snprintf(attr->name, sizeof(attr->name), "extn%d_label", s);
976 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
977 occ_show_extended, NULL, 0, i);
980 snprintf(attr->name, sizeof(attr->name), "extn%d_flags", s);
981 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
982 occ_show_extended, NULL, 1, i);
985 snprintf(attr->name, sizeof(attr->name), "extn%d_input", s);
986 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
987 occ_show_extended, NULL, 2, i);
991 /* put the sensors in the group */
992 for (i = 0; i < num_attrs; ++i) {
993 sysfs_attr_init(&occ->attrs[i].sensor.dev_attr.attr);
994 occ->group.attrs[i] = &occ->attrs[i].sensor.dev_attr.attr;
1000 /* only need to do this once at startup, as OCC won't change sensors on us */
1001 static void occ_parse_poll_response(struct occ *occ)
1003 unsigned int i, old_offset, offset = 0, size = 0;
1004 struct occ_sensor *sensor;
1005 struct occ_sensors *sensors = &occ->sensors;
1006 struct occ_response *resp = &occ->resp;
1007 struct occ_poll_response *poll =
1008 (struct occ_poll_response *)&resp->data[0];
1009 struct occ_poll_response_header *header = &poll->header;
1010 struct occ_sensor_data_block *block = &poll->block;
1012 dev_info(occ->bus_dev, "OCC found, code level: %.16s\n",
1013 header->occ_code_level);
1015 for (i = 0; i < header->num_sensor_data_blocks; ++i) {
1016 block = (struct occ_sensor_data_block *)((u8 *)block + offset);
1017 old_offset = offset;
1018 offset = (block->header.num_sensors *
1019 block->header.sensor_length) + sizeof(block->header);
1022 /* validate all the length/size fields */
1023 if ((size + sizeof(*header)) >= OCC_RESP_DATA_BYTES) {
1024 dev_warn(occ->bus_dev, "exceeded response buffer\n");
1028 dev_dbg(occ->bus_dev, " %04x..%04x: %.4s (%d sensors)\n",
1029 old_offset, offset - 1, block->header.eye_catcher,
1030 block->header.num_sensors);
1032 /* match sensor block type */
1033 if (strncmp(block->header.eye_catcher, "TEMP", 4) == 0)
1034 sensor = &sensors->temp;
1035 else if (strncmp(block->header.eye_catcher, "FREQ", 4) == 0)
1036 sensor = &sensors->freq;
1037 else if (strncmp(block->header.eye_catcher, "POWR", 4) == 0)
1038 sensor = &sensors->power;
1039 else if (strncmp(block->header.eye_catcher, "CAPS", 4) == 0)
1040 sensor = &sensors->caps;
1041 else if (strncmp(block->header.eye_catcher, "EXTN", 4) == 0)
1042 sensor = &sensors->extended;
1044 dev_warn(occ->bus_dev, "sensor not supported %.4s\n",
1045 block->header.eye_catcher);
1049 sensor->num_sensors = block->header.num_sensors;
1050 sensor->version = block->header.sensor_format;
1051 sensor->data = &block->data;
1054 dev_dbg(occ->bus_dev, "Max resp size: %u+%zd=%zd\n", size,
1055 sizeof(*header), size + sizeof(*header));
1058 int occ_setup(struct occ *occ, const char *name)
1062 mutex_init(&occ->lock);
1063 occ->groups[0] = &occ->group;
1065 /* no need to lock */
1067 if (rc == -ESHUTDOWN) {
1068 dev_info(occ->bus_dev, "host is not ready\n");
1070 } else if (rc < 0) {
1071 dev_err(occ->bus_dev, "failed to get OCC poll response: %d\n",
1076 occ_parse_poll_response(occ);
1078 rc = occ_setup_sensor_attrs(occ);
1080 dev_err(occ->bus_dev, "failed to setup sensor attrs: %d\n",
1085 occ->hwmon = devm_hwmon_device_register_with_groups(occ->bus_dev, name,
1087 if (IS_ERR(occ->hwmon)) {
1088 rc = PTR_ERR(occ->hwmon);
1089 dev_err(occ->bus_dev, "failed to register hwmon device: %d\n",
1094 rc = occ_setup_sysfs(occ);
1096 dev_err(occ->bus_dev, "failed to setup sysfs: %d\n", rc);