1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/device.h>
4 #include <linux/hwmon.h>
5 #include <linux/hwmon-sysfs.h>
6 #include <linux/jiffies.h>
7 #include <linux/kernel.h>
8 #include <linux/math64.h>
9 #include <linux/mutex.h>
10 #include <linux/sysfs.h>
11 #include <asm/unaligned.h>
15 #define EXTN_FLAG_SENSOR_ID BIT(7)
17 #define OCC_ERROR_COUNT_THRESHOLD 2 /* required by OCC spec */
19 #define OCC_STATE_SAFE 4
20 #define OCC_SAFE_TIMEOUT msecs_to_jiffies(60000) /* 1 min */
22 #define OCC_UPDATE_FREQUENCY msecs_to_jiffies(1000)
24 #define OCC_TEMP_SENSOR_FAULT 0xFF
26 #define OCC_FRU_TYPE_VRM 3
28 /* OCC sensor type and version definitions */
30 struct temp_sensor_1 {
35 struct temp_sensor_2 {
41 struct freq_sensor_1 {
46 struct freq_sensor_2 {
51 struct power_sensor_1 {
58 struct power_sensor_2 {
68 struct power_sensor_data {
74 struct power_sensor_data_and_time {
81 struct power_sensor_a0 {
83 struct power_sensor_data_and_time system;
85 struct power_sensor_data_and_time proc;
86 struct power_sensor_data vdd;
87 struct power_sensor_data vdn;
90 struct caps_sensor_2 {
100 struct caps_sensor_3 {
111 struct extended_sensor {
121 static int occ_poll(struct occ *occ)
124 u16 checksum = occ->poll_cmd_data + 1;
126 struct occ_poll_response_header *header;
129 cmd[0] = 0; /* sequence number */
130 cmd[1] = 0; /* cmd type */
131 cmd[2] = 0; /* data length msb */
132 cmd[3] = 1; /* data length lsb */
133 cmd[4] = occ->poll_cmd_data; /* data */
134 cmd[5] = checksum >> 8; /* checksum msb */
135 cmd[6] = checksum & 0xFF; /* checksum lsb */
138 /* mutex should already be locked if necessary */
139 rc = occ->send_cmd(occ, cmd);
141 if (occ->error_count++ > OCC_ERROR_COUNT_THRESHOLD)
147 /* clear error since communication was successful */
148 occ->error_count = 0;
151 /* check for safe state */
152 header = (struct occ_poll_response_header *)occ->resp.data;
153 if (header->occ_state == OCC_STATE_SAFE) {
154 if (occ->last_safe) {
155 if (time_after(jiffies,
156 occ->last_safe + OCC_SAFE_TIMEOUT))
157 occ->error = -EHOSTDOWN;
159 occ->last_safe = jiffies;
166 occ_sysfs_poll_done(occ);
170 static int occ_set_user_power_cap(struct occ *occ, u16 user_power_cap)
175 __be16 user_power_cap_be = cpu_to_be16(user_power_cap);
182 memcpy(&cmd[4], &user_power_cap_be, 2);
184 checksum += cmd[4] + cmd[5];
185 cmd[6] = checksum >> 8;
186 cmd[7] = checksum & 0xFF;
188 rc = mutex_lock_interruptible(&occ->lock);
192 rc = occ->send_cmd(occ, cmd);
194 mutex_unlock(&occ->lock);
199 int occ_update_response(struct occ *occ)
201 int rc = mutex_lock_interruptible(&occ->lock);
206 /* limit the maximum rate of polling the OCC */
207 if (time_after(jiffies, occ->last_update + OCC_UPDATE_FREQUENCY)) {
209 occ->last_update = jiffies;
212 mutex_unlock(&occ->lock);
216 static ssize_t occ_show_temp_1(struct device *dev,
217 struct device_attribute *attr, char *buf)
221 struct temp_sensor_1 *temp;
222 struct occ *occ = dev_get_drvdata(dev);
223 struct occ_sensors *sensors = &occ->sensors;
224 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
226 rc = occ_update_response(occ);
230 temp = ((struct temp_sensor_1 *)sensors->temp.data) + sattr->index;
234 val = get_unaligned_be16(&temp->sensor_id);
237 val = get_unaligned_be16(&temp->value) * 1000;
243 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
246 static ssize_t occ_show_temp_2(struct device *dev,
247 struct device_attribute *attr, char *buf)
251 struct temp_sensor_2 *temp;
252 struct occ *occ = dev_get_drvdata(dev);
253 struct occ_sensors *sensors = &occ->sensors;
254 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
256 rc = occ_update_response(occ);
260 temp = ((struct temp_sensor_2 *)sensors->temp.data) + sattr->index;
264 val = get_unaligned_be32(&temp->sensor_id);
268 if (val == OCC_TEMP_SENSOR_FAULT)
272 * VRM doesn't return temperature, only alarm bit. This
273 * attribute maps to tempX_alarm instead of tempX_input for
276 if (temp->fru_type != OCC_FRU_TYPE_VRM) {
277 /* sensor not ready */
285 val = temp->fru_type;
288 val = temp->value == OCC_TEMP_SENSOR_FAULT;
294 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
297 static ssize_t occ_show_freq_1(struct device *dev,
298 struct device_attribute *attr, char *buf)
302 struct freq_sensor_1 *freq;
303 struct occ *occ = dev_get_drvdata(dev);
304 struct occ_sensors *sensors = &occ->sensors;
305 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
307 rc = occ_update_response(occ);
311 freq = ((struct freq_sensor_1 *)sensors->freq.data) + sattr->index;
315 val = get_unaligned_be16(&freq->sensor_id);
318 val = get_unaligned_be16(&freq->value);
324 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
327 static ssize_t occ_show_freq_2(struct device *dev,
328 struct device_attribute *attr, char *buf)
332 struct freq_sensor_2 *freq;
333 struct occ *occ = dev_get_drvdata(dev);
334 struct occ_sensors *sensors = &occ->sensors;
335 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
337 rc = occ_update_response(occ);
341 freq = ((struct freq_sensor_2 *)sensors->freq.data) + sattr->index;
345 val = get_unaligned_be32(&freq->sensor_id);
348 val = get_unaligned_be16(&freq->value);
354 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
357 static ssize_t occ_show_power_1(struct device *dev,
358 struct device_attribute *attr, char *buf)
362 struct power_sensor_1 *power;
363 struct occ *occ = dev_get_drvdata(dev);
364 struct occ_sensors *sensors = &occ->sensors;
365 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
367 rc = occ_update_response(occ);
371 power = ((struct power_sensor_1 *)sensors->power.data) + sattr->index;
375 val = get_unaligned_be16(&power->sensor_id);
378 val = get_unaligned_be32(&power->accumulator) /
379 get_unaligned_be32(&power->update_tag);
383 val = get_unaligned_be32(&power->update_tag) *
384 occ->powr_sample_time_us;
387 val = get_unaligned_be16(&power->value) * 1000000ULL;
393 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
396 static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
398 return div64_u64(get_unaligned_be64(accum) * 1000000ULL,
399 get_unaligned_be32(samples));
402 static ssize_t occ_show_power_2(struct device *dev,
403 struct device_attribute *attr, char *buf)
407 struct power_sensor_2 *power;
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 power = ((struct power_sensor_2 *)sensors->power.data) + sattr->index;
420 return snprintf(buf, PAGE_SIZE - 1, "%u_%u_%u\n",
421 get_unaligned_be32(&power->sensor_id),
422 power->function_id, power->apss_channel);
424 val = occ_get_powr_avg(&power->accumulator,
428 val = get_unaligned_be32(&power->update_tag) *
429 occ->powr_sample_time_us;
432 val = get_unaligned_be16(&power->value) * 1000000ULL;
438 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
441 static ssize_t occ_show_power_a0(struct device *dev,
442 struct device_attribute *attr, char *buf)
446 struct power_sensor_a0 *power;
447 struct occ *occ = dev_get_drvdata(dev);
448 struct occ_sensors *sensors = &occ->sensors;
449 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
451 rc = occ_update_response(occ);
455 power = ((struct power_sensor_a0 *)sensors->power.data) + sattr->index;
459 return snprintf(buf, PAGE_SIZE - 1, "%u_system\n",
460 get_unaligned_be32(&power->sensor_id));
462 val = occ_get_powr_avg(&power->system.accumulator,
463 &power->system.update_tag);
466 val = get_unaligned_be32(&power->system.update_tag) *
467 occ->powr_sample_time_us;
470 val = get_unaligned_be16(&power->system.value) * 1000000ULL;
473 return snprintf(buf, PAGE_SIZE - 1, "%u_proc\n",
474 get_unaligned_be32(&power->sensor_id));
476 val = occ_get_powr_avg(&power->proc.accumulator,
477 &power->proc.update_tag);
480 val = get_unaligned_be32(&power->proc.update_tag) *
481 occ->powr_sample_time_us;
484 val = get_unaligned_be16(&power->proc.value) * 1000000ULL;
487 return snprintf(buf, PAGE_SIZE - 1, "%u_vdd\n",
488 get_unaligned_be32(&power->sensor_id));
490 val = occ_get_powr_avg(&power->vdd.accumulator,
491 &power->vdd.update_tag);
494 val = get_unaligned_be32(&power->vdd.update_tag) *
495 occ->powr_sample_time_us;
498 val = get_unaligned_be16(&power->vdd.value) * 1000000ULL;
501 return snprintf(buf, PAGE_SIZE - 1, "%u_vdn\n",
502 get_unaligned_be32(&power->sensor_id));
504 val = occ_get_powr_avg(&power->vdn.accumulator,
505 &power->vdn.update_tag);
508 val = get_unaligned_be32(&power->vdn.update_tag) *
509 occ->powr_sample_time_us;
512 val = get_unaligned_be16(&power->vdn.value) * 1000000ULL;
518 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
521 static ssize_t occ_show_caps_1_2(struct device *dev,
522 struct device_attribute *attr, char *buf)
526 struct caps_sensor_2 *caps;
527 struct occ *occ = dev_get_drvdata(dev);
528 struct occ_sensors *sensors = &occ->sensors;
529 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
531 rc = occ_update_response(occ);
535 caps = ((struct caps_sensor_2 *)sensors->caps.data) + sattr->index;
539 return snprintf(buf, PAGE_SIZE - 1, "system\n");
541 val = get_unaligned_be16(&caps->cap) * 1000000ULL;
544 val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
547 val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
550 val = get_unaligned_be16(&caps->max) * 1000000ULL;
553 val = get_unaligned_be16(&caps->min) * 1000000ULL;
556 val = get_unaligned_be16(&caps->user) * 1000000ULL;
559 if (occ->sensors.caps.version == 1)
562 val = caps->user_source;
568 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
571 static ssize_t occ_show_caps_3(struct device *dev,
572 struct device_attribute *attr, char *buf)
576 struct caps_sensor_3 *caps;
577 struct occ *occ = dev_get_drvdata(dev);
578 struct occ_sensors *sensors = &occ->sensors;
579 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
581 rc = occ_update_response(occ);
585 caps = ((struct caps_sensor_3 *)sensors->caps.data) + sattr->index;
589 return snprintf(buf, PAGE_SIZE - 1, "system\n");
591 val = get_unaligned_be16(&caps->cap) * 1000000ULL;
594 val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
597 val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
600 val = get_unaligned_be16(&caps->max) * 1000000ULL;
603 val = get_unaligned_be16(&caps->hard_min) * 1000000ULL;
606 val = get_unaligned_be16(&caps->user) * 1000000ULL;
609 val = caps->user_source;
615 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
618 static ssize_t occ_store_caps_user(struct device *dev,
619 struct device_attribute *attr,
620 const char *buf, size_t count)
624 unsigned long long value;
625 struct occ *occ = dev_get_drvdata(dev);
627 rc = kstrtoull(buf, 0, &value);
631 user_power_cap = div64_u64(value, 1000000ULL); /* microwatt to watt */
633 rc = occ_set_user_power_cap(occ, user_power_cap);
640 static ssize_t occ_show_extended(struct device *dev,
641 struct device_attribute *attr, char *buf)
644 struct extended_sensor *extn;
645 struct occ *occ = dev_get_drvdata(dev);
646 struct occ_sensors *sensors = &occ->sensors;
647 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
649 rc = occ_update_response(occ);
653 extn = ((struct extended_sensor *)sensors->extended.data) +
658 if (extn->flags & EXTN_FLAG_SENSOR_ID)
659 rc = snprintf(buf, PAGE_SIZE - 1, "%u",
660 get_unaligned_be32(&extn->sensor_id));
662 rc = snprintf(buf, PAGE_SIZE - 1, "%02x%02x%02x%02x\n",
663 extn->name[0], extn->name[1],
664 extn->name[2], extn->name[3]);
667 rc = snprintf(buf, PAGE_SIZE - 1, "%02x\n", extn->flags);
670 rc = snprintf(buf, PAGE_SIZE - 1, "%02x%02x%02x%02x%02x%02x\n",
671 extn->data[0], extn->data[1], extn->data[2],
672 extn->data[3], extn->data[4], extn->data[5]);
682 * Some helper macros to make it easier to define an occ_attribute. Since these
683 * are dynamically allocated, we shouldn't use the existing kernel macros which
684 * stringify the name argument.
686 #define ATTR_OCC(_name, _mode, _show, _store) { \
689 .mode = VERIFY_OCTAL_PERMISSIONS(_mode), \
695 #define SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index) { \
696 .dev_attr = ATTR_OCC(_name, _mode, _show, _store), \
701 #define OCC_INIT_ATTR(_name, _mode, _show, _store, _nr, _index) \
702 ((struct sensor_device_attribute_2) \
703 SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index))
706 * Allocate and instatiate sensor_device_attribute_2s. It's most efficient to
707 * use our own instead of the built-in hwmon attribute types.
709 static int occ_setup_sensor_attrs(struct occ *occ)
711 unsigned int i, s, num_attrs = 0;
712 struct device *dev = occ->bus_dev;
713 struct occ_sensors *sensors = &occ->sensors;
714 struct occ_attribute *attr;
715 struct temp_sensor_2 *temp;
716 ssize_t (*show_temp)(struct device *, struct device_attribute *,
717 char *) = occ_show_temp_1;
718 ssize_t (*show_freq)(struct device *, struct device_attribute *,
719 char *) = occ_show_freq_1;
720 ssize_t (*show_power)(struct device *, struct device_attribute *,
721 char *) = occ_show_power_1;
722 ssize_t (*show_caps)(struct device *, struct device_attribute *,
723 char *) = occ_show_caps_1_2;
725 switch (sensors->temp.version) {
727 num_attrs += (sensors->temp.num_sensors * 2);
730 num_attrs += (sensors->temp.num_sensors * 4);
731 show_temp = occ_show_temp_2;
734 sensors->temp.num_sensors = 0;
737 switch (sensors->freq.version) {
739 show_freq = occ_show_freq_2;
742 num_attrs += (sensors->freq.num_sensors * 2);
745 sensors->freq.num_sensors = 0;
748 switch (sensors->power.version) {
750 show_power = occ_show_power_2;
753 num_attrs += (sensors->power.num_sensors * 4);
756 num_attrs += (sensors->power.num_sensors * 16);
757 show_power = occ_show_power_a0;
760 sensors->power.num_sensors = 0;
763 switch (sensors->caps.version) {
765 num_attrs += (sensors->caps.num_sensors * 7);
768 show_caps = occ_show_caps_3;
771 num_attrs += (sensors->caps.num_sensors * 8);
774 sensors->caps.num_sensors = 0;
777 switch (sensors->extended.version) {
779 num_attrs += (sensors->extended.num_sensors * 3);
782 sensors->extended.num_sensors = 0;
785 occ->attrs = devm_kzalloc(dev, sizeof(*occ->attrs) * num_attrs,
790 /* null-terminated list */
791 occ->group.attrs = devm_kzalloc(dev, sizeof(*occ->group.attrs) *
792 num_attrs + 1, GFP_KERNEL);
793 if (!occ->group.attrs)
798 for (i = 0; i < sensors->temp.num_sensors; ++i) {
800 temp = ((struct temp_sensor_2 *)sensors->temp.data) + i;
802 snprintf(attr->name, sizeof(attr->name), "temp%d_label", s);
803 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
807 if (sensors->temp.version > 1 &&
808 temp->fru_type == OCC_FRU_TYPE_VRM) {
809 snprintf(attr->name, sizeof(attr->name),
812 snprintf(attr->name, sizeof(attr->name),
816 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
820 if (sensors->temp.version > 1) {
821 snprintf(attr->name, sizeof(attr->name),
822 "temp%d_fru_type", s);
823 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
824 show_temp, NULL, 2, i);
827 snprintf(attr->name, sizeof(attr->name),
829 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
830 show_temp, NULL, 3, i);
835 for (i = 0; i < sensors->freq.num_sensors; ++i) {
838 snprintf(attr->name, sizeof(attr->name), "freq%d_label", s);
839 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
843 snprintf(attr->name, sizeof(attr->name), "freq%d_input", s);
844 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
849 if (sensors->power.version == 0xA0) {
851 * Special case for many-attribute power sensor. Split it into
852 * a sensor number per power type, emulating several sensors.
854 for (i = 0; i < sensors->power.num_sensors; ++i) {
860 for (j = 0; j < 4; ++j) {
861 snprintf(attr->name, sizeof(attr->name),
863 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
868 snprintf(attr->name, sizeof(attr->name),
869 "power%d_average", s);
870 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
875 snprintf(attr->name, sizeof(attr->name),
876 "power%d_average_interval", s);
877 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
882 snprintf(attr->name, sizeof(attr->name),
884 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
893 for (i = 0; i < sensors->power.num_sensors; ++i) {
896 snprintf(attr->name, sizeof(attr->name),
898 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
899 show_power, NULL, 0, i);
902 snprintf(attr->name, sizeof(attr->name),
903 "power%d_average", s);
904 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
905 show_power, NULL, 1, i);
908 snprintf(attr->name, sizeof(attr->name),
909 "power%d_average_interval", s);
910 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
911 show_power, NULL, 2, i);
914 snprintf(attr->name, sizeof(attr->name),
916 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
917 show_power, NULL, 3, i);
922 if (sensors->caps.num_sensors >= 1) {
923 s = sensors->power.num_sensors + 1;
925 snprintf(attr->name, sizeof(attr->name), "power%d_label", s);
926 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
930 snprintf(attr->name, sizeof(attr->name), "power%d_cap", s);
931 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
935 snprintf(attr->name, sizeof(attr->name), "power%d_input", s);
936 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
940 snprintf(attr->name, sizeof(attr->name),
941 "power%d_cap_not_redundant", s);
942 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
946 snprintf(attr->name, sizeof(attr->name), "power%d_cap_max", s);
947 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
951 snprintf(attr->name, sizeof(attr->name), "power%d_cap_min", s);
952 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
956 snprintf(attr->name, sizeof(attr->name), "power%d_cap_user",
958 attr->sensor = OCC_INIT_ATTR(attr->name, 0644, show_caps,
959 occ_store_caps_user, 6, 0);
962 if (sensors->caps.version > 1) {
963 snprintf(attr->name, sizeof(attr->name),
964 "power%d_cap_user_source", s);
965 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
966 show_caps, NULL, 7, 0);
971 for (i = 0; i < sensors->extended.num_sensors; ++i) {
974 snprintf(attr->name, sizeof(attr->name), "extn%d_label", s);
975 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
976 occ_show_extended, NULL, 0, i);
979 snprintf(attr->name, sizeof(attr->name), "extn%d_flags", s);
980 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
981 occ_show_extended, NULL, 1, i);
984 snprintf(attr->name, sizeof(attr->name), "extn%d_input", s);
985 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
986 occ_show_extended, NULL, 2, i);
990 /* put the sensors in the group */
991 for (i = 0; i < num_attrs; ++i) {
992 sysfs_attr_init(&occ->attrs[i].sensor.dev_attr.attr);
993 occ->group.attrs[i] = &occ->attrs[i].sensor.dev_attr.attr;
999 /* only need to do this once at startup, as OCC won't change sensors on us */
1000 static void occ_parse_poll_response(struct occ *occ)
1002 unsigned int i, old_offset, offset = 0, size = 0;
1003 struct occ_sensor *sensor;
1004 struct occ_sensors *sensors = &occ->sensors;
1005 struct occ_response *resp = &occ->resp;
1006 struct occ_poll_response *poll =
1007 (struct occ_poll_response *)&resp->data[0];
1008 struct occ_poll_response_header *header = &poll->header;
1009 struct occ_sensor_data_block *block = &poll->block;
1011 dev_info(occ->bus_dev, "OCC found, code level: %.16s\n",
1012 header->occ_code_level);
1014 for (i = 0; i < header->num_sensor_data_blocks; ++i) {
1015 block = (struct occ_sensor_data_block *)((u8 *)block + offset);
1016 old_offset = offset;
1017 offset = (block->header.num_sensors *
1018 block->header.sensor_length) + sizeof(block->header);
1021 /* validate all the length/size fields */
1022 if ((size + sizeof(*header)) >= OCC_RESP_DATA_BYTES) {
1023 dev_warn(occ->bus_dev, "exceeded response buffer\n");
1027 dev_dbg(occ->bus_dev, " %04x..%04x: %.4s (%d sensors)\n",
1028 old_offset, offset - 1, block->header.eye_catcher,
1029 block->header.num_sensors);
1031 /* match sensor block type */
1032 if (strncmp(block->header.eye_catcher, "TEMP", 4) == 0)
1033 sensor = &sensors->temp;
1034 else if (strncmp(block->header.eye_catcher, "FREQ", 4) == 0)
1035 sensor = &sensors->freq;
1036 else if (strncmp(block->header.eye_catcher, "POWR", 4) == 0)
1037 sensor = &sensors->power;
1038 else if (strncmp(block->header.eye_catcher, "CAPS", 4) == 0)
1039 sensor = &sensors->caps;
1040 else if (strncmp(block->header.eye_catcher, "EXTN", 4) == 0)
1041 sensor = &sensors->extended;
1043 dev_warn(occ->bus_dev, "sensor not supported %.4s\n",
1044 block->header.eye_catcher);
1048 sensor->num_sensors = block->header.num_sensors;
1049 sensor->version = block->header.sensor_format;
1050 sensor->data = &block->data;
1053 dev_dbg(occ->bus_dev, "Max resp size: %u+%zd=%zd\n", size,
1054 sizeof(*header), size + sizeof(*header));
1057 int occ_setup(struct occ *occ, const char *name)
1061 mutex_init(&occ->lock);
1062 occ->groups[0] = &occ->group;
1064 /* no need to lock */
1066 if (rc == -ESHUTDOWN) {
1067 dev_info(occ->bus_dev, "host is not ready\n");
1069 } else if (rc < 0) {
1070 dev_err(occ->bus_dev, "failed to get OCC poll response: %d\n",
1075 occ_parse_poll_response(occ);
1077 rc = occ_setup_sensor_attrs(occ);
1079 dev_err(occ->bus_dev, "failed to setup sensor attrs: %d\n",
1084 occ->hwmon = devm_hwmon_device_register_with_groups(occ->bus_dev, name,
1086 if (IS_ERR(occ->hwmon)) {
1087 rc = PTR_ERR(occ->hwmon);
1088 dev_err(occ->bus_dev, "failed to register hwmon device: %d\n",
1093 rc = occ_setup_sysfs(occ);
1095 dev_err(occ->bus_dev, "failed to setup sysfs: %d\n", rc);