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/property.h>
14 #include <linux/sysfs.h>
15 #include <asm/unaligned.h>
19 #define EXTN_FLAG_SENSOR_ID BIT(7)
21 #define OCC_ERROR_COUNT_THRESHOLD 2 /* required by OCC spec */
23 #define OCC_STATE_SAFE 4
24 #define OCC_SAFE_TIMEOUT msecs_to_jiffies(60000) /* 1 min */
26 #define OCC_UPDATE_FREQUENCY msecs_to_jiffies(1000)
28 #define OCC_TEMP_SENSOR_FAULT 0xFF
30 #define OCC_FRU_TYPE_VRM 3
32 /* OCC sensor type and version definitions */
34 struct temp_sensor_1 {
39 struct temp_sensor_2 {
45 struct temp_sensor_10 {
53 struct freq_sensor_1 {
58 struct freq_sensor_2 {
63 struct power_sensor_1 {
70 struct power_sensor_2 {
80 struct power_sensor_data {
86 struct power_sensor_data_and_time {
93 struct power_sensor_a0 {
95 struct power_sensor_data_and_time system;
97 struct power_sensor_data_and_time proc;
98 struct power_sensor_data vdd;
99 struct power_sensor_data vdn;
102 struct caps_sensor_2 {
112 struct caps_sensor_3 {
123 struct extended_sensor {
133 static int occ_poll(struct occ *occ)
137 struct occ_poll_response_header *header;
140 cmd[0] = 0; /* 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] = 0; /* checksum msb */
146 cmd[6] = 0; /* checksum lsb */
148 /* mutex should already be locked if necessary */
149 rc = occ->send_cmd(occ, cmd, sizeof(cmd), &occ->resp, sizeof(occ->resp));
151 occ->last_error = rc;
152 if (occ->error_count++ > OCC_ERROR_COUNT_THRESHOLD)
158 /* clear error since communication was successful */
159 occ->error_count = 0;
163 /* check for safe state */
164 header = (struct occ_poll_response_header *)occ->resp.data;
165 if (header->occ_state == OCC_STATE_SAFE) {
166 if (occ->last_safe) {
167 if (time_after(jiffies,
168 occ->last_safe + OCC_SAFE_TIMEOUT))
169 occ->error = -EHOSTDOWN;
171 occ->last_safe = jiffies;
178 occ_sysfs_poll_done(occ);
182 static int occ_set_user_power_cap(struct occ *occ, u16 user_power_cap)
187 __be16 user_power_cap_be = cpu_to_be16(user_power_cap);
189 cmd[0] = 0; /* sequence number */
190 cmd[1] = 0x22; /* cmd type */
191 cmd[2] = 0; /* data length msb */
192 cmd[3] = 2; /* data length lsb */
194 memcpy(&cmd[4], &user_power_cap_be, 2);
196 cmd[6] = 0; /* checksum msb */
197 cmd[7] = 0; /* checksum lsb */
199 rc = mutex_lock_interruptible(&occ->lock);
203 rc = occ->send_cmd(occ, cmd, sizeof(cmd), resp, sizeof(resp));
205 mutex_unlock(&occ->lock);
210 int occ_update_response(struct occ *occ)
212 int rc = mutex_lock_interruptible(&occ->lock);
217 /* limit the maximum rate of polling the OCC */
218 if (time_after(jiffies, occ->next_update)) {
220 occ->next_update = jiffies + OCC_UPDATE_FREQUENCY;
222 rc = occ->last_error;
225 mutex_unlock(&occ->lock);
229 static ssize_t occ_show_temp_1(struct device *dev,
230 struct device_attribute *attr, char *buf)
234 struct temp_sensor_1 *temp;
235 struct occ *occ = dev_get_drvdata(dev);
236 struct occ_sensors *sensors = &occ->sensors;
237 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
239 rc = occ_update_response(occ);
243 temp = ((struct temp_sensor_1 *)sensors->temp.data) + sattr->index;
247 val = get_unaligned_be16(&temp->sensor_id);
251 * If a sensor reading has expired and couldn't be refreshed,
252 * OCC returns 0xFFFF for that sensor.
254 if (temp->value == 0xFFFF)
256 val = get_unaligned_be16(&temp->value) * 1000;
262 return sysfs_emit(buf, "%u\n", val);
265 static ssize_t occ_show_temp_2(struct device *dev,
266 struct device_attribute *attr, char *buf)
270 struct temp_sensor_2 *temp;
271 struct occ *occ = dev_get_drvdata(dev);
272 struct occ_sensors *sensors = &occ->sensors;
273 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
275 rc = occ_update_response(occ);
279 temp = ((struct temp_sensor_2 *)sensors->temp.data) + sattr->index;
283 val = get_unaligned_be32(&temp->sensor_id);
287 if (val == OCC_TEMP_SENSOR_FAULT)
291 * VRM doesn't return temperature, only alarm bit. This
292 * attribute maps to tempX_alarm instead of tempX_input for
295 if (temp->fru_type != OCC_FRU_TYPE_VRM) {
296 /* sensor not ready */
304 val = temp->fru_type;
307 val = temp->value == OCC_TEMP_SENSOR_FAULT;
313 return sysfs_emit(buf, "%u\n", val);
316 static ssize_t occ_show_temp_10(struct device *dev,
317 struct device_attribute *attr, char *buf)
321 struct temp_sensor_10 *temp;
322 struct occ *occ = dev_get_drvdata(dev);
323 struct occ_sensors *sensors = &occ->sensors;
324 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
326 rc = occ_update_response(occ);
330 temp = ((struct temp_sensor_10 *)sensors->temp.data) + sattr->index;
334 val = get_unaligned_be32(&temp->sensor_id);
338 if (val == OCC_TEMP_SENSOR_FAULT)
341 /* sensor not ready */
348 val = temp->fru_type;
351 val = temp->value == OCC_TEMP_SENSOR_FAULT;
354 val = temp->throttle * 1000;
360 return sysfs_emit(buf, "%u\n", val);
363 static ssize_t occ_show_freq_1(struct device *dev,
364 struct device_attribute *attr, char *buf)
368 struct freq_sensor_1 *freq;
369 struct occ *occ = dev_get_drvdata(dev);
370 struct occ_sensors *sensors = &occ->sensors;
371 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
373 rc = occ_update_response(occ);
377 freq = ((struct freq_sensor_1 *)sensors->freq.data) + sattr->index;
381 val = get_unaligned_be16(&freq->sensor_id);
384 val = get_unaligned_be16(&freq->value);
390 return sysfs_emit(buf, "%u\n", val);
393 static ssize_t occ_show_freq_2(struct device *dev,
394 struct device_attribute *attr, char *buf)
398 struct freq_sensor_2 *freq;
399 struct occ *occ = dev_get_drvdata(dev);
400 struct occ_sensors *sensors = &occ->sensors;
401 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
403 rc = occ_update_response(occ);
407 freq = ((struct freq_sensor_2 *)sensors->freq.data) + sattr->index;
411 val = get_unaligned_be32(&freq->sensor_id);
414 val = get_unaligned_be16(&freq->value);
420 return sysfs_emit(buf, "%u\n", val);
423 static ssize_t occ_show_power_1(struct device *dev,
424 struct device_attribute *attr, char *buf)
428 struct power_sensor_1 *power;
429 struct occ *occ = dev_get_drvdata(dev);
430 struct occ_sensors *sensors = &occ->sensors;
431 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
433 rc = occ_update_response(occ);
437 power = ((struct power_sensor_1 *)sensors->power.data) + sattr->index;
441 val = get_unaligned_be16(&power->sensor_id);
444 val = get_unaligned_be32(&power->accumulator) /
445 get_unaligned_be32(&power->update_tag);
449 val = (u64)get_unaligned_be32(&power->update_tag) *
450 occ->powr_sample_time_us;
453 val = get_unaligned_be16(&power->value) * 1000000ULL;
459 return sysfs_emit(buf, "%llu\n", val);
462 static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
464 u64 divisor = get_unaligned_be32(samples);
466 return (divisor == 0) ? 0 :
467 div64_u64(get_unaligned_be64(accum) * 1000000ULL, divisor);
470 static ssize_t occ_show_power_2(struct device *dev,
471 struct device_attribute *attr, char *buf)
475 struct power_sensor_2 *power;
476 struct occ *occ = dev_get_drvdata(dev);
477 struct occ_sensors *sensors = &occ->sensors;
478 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
480 rc = occ_update_response(occ);
484 power = ((struct power_sensor_2 *)sensors->power.data) + sattr->index;
488 return sysfs_emit(buf, "%u_%u_%u\n",
489 get_unaligned_be32(&power->sensor_id),
490 power->function_id, power->apss_channel);
492 val = occ_get_powr_avg(&power->accumulator,
496 val = (u64)get_unaligned_be32(&power->update_tag) *
497 occ->powr_sample_time_us;
500 val = get_unaligned_be16(&power->value) * 1000000ULL;
506 return sysfs_emit(buf, "%llu\n", val);
509 static ssize_t occ_show_power_a0(struct device *dev,
510 struct device_attribute *attr, char *buf)
514 struct power_sensor_a0 *power;
515 struct occ *occ = dev_get_drvdata(dev);
516 struct occ_sensors *sensors = &occ->sensors;
517 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
519 rc = occ_update_response(occ);
523 power = ((struct power_sensor_a0 *)sensors->power.data) + sattr->index;
527 return sysfs_emit(buf, "%u_system\n",
528 get_unaligned_be32(&power->sensor_id));
530 val = occ_get_powr_avg(&power->system.accumulator,
531 &power->system.update_tag);
534 val = (u64)get_unaligned_be32(&power->system.update_tag) *
535 occ->powr_sample_time_us;
538 val = get_unaligned_be16(&power->system.value) * 1000000ULL;
541 return sysfs_emit(buf, "%u_proc\n",
542 get_unaligned_be32(&power->sensor_id));
544 val = occ_get_powr_avg(&power->proc.accumulator,
545 &power->proc.update_tag);
548 val = (u64)get_unaligned_be32(&power->proc.update_tag) *
549 occ->powr_sample_time_us;
552 val = get_unaligned_be16(&power->proc.value) * 1000000ULL;
555 return sysfs_emit(buf, "%u_vdd\n",
556 get_unaligned_be32(&power->sensor_id));
558 val = occ_get_powr_avg(&power->vdd.accumulator,
559 &power->vdd.update_tag);
562 val = (u64)get_unaligned_be32(&power->vdd.update_tag) *
563 occ->powr_sample_time_us;
566 val = get_unaligned_be16(&power->vdd.value) * 1000000ULL;
569 return sysfs_emit(buf, "%u_vdn\n",
570 get_unaligned_be32(&power->sensor_id));
572 val = occ_get_powr_avg(&power->vdn.accumulator,
573 &power->vdn.update_tag);
576 val = (u64)get_unaligned_be32(&power->vdn.update_tag) *
577 occ->powr_sample_time_us;
580 val = get_unaligned_be16(&power->vdn.value) * 1000000ULL;
586 return sysfs_emit(buf, "%llu\n", val);
589 static ssize_t occ_show_caps_1_2(struct device *dev,
590 struct device_attribute *attr, char *buf)
594 struct caps_sensor_2 *caps;
595 struct occ *occ = dev_get_drvdata(dev);
596 struct occ_sensors *sensors = &occ->sensors;
597 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
599 rc = occ_update_response(occ);
603 caps = ((struct caps_sensor_2 *)sensors->caps.data) + sattr->index;
607 return sysfs_emit(buf, "system\n");
609 val = get_unaligned_be16(&caps->cap) * 1000000ULL;
612 val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
615 val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
618 val = get_unaligned_be16(&caps->max) * 1000000ULL;
621 val = get_unaligned_be16(&caps->min) * 1000000ULL;
624 val = get_unaligned_be16(&caps->user) * 1000000ULL;
627 if (occ->sensors.caps.version == 1)
630 val = caps->user_source;
636 return sysfs_emit(buf, "%llu\n", val);
639 static ssize_t occ_show_caps_3(struct device *dev,
640 struct device_attribute *attr, char *buf)
644 struct caps_sensor_3 *caps;
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 caps = ((struct caps_sensor_3 *)sensors->caps.data) + sattr->index;
657 return sysfs_emit(buf, "system\n");
659 val = get_unaligned_be16(&caps->cap) * 1000000ULL;
662 val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
665 val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
668 val = get_unaligned_be16(&caps->max) * 1000000ULL;
671 val = get_unaligned_be16(&caps->hard_min) * 1000000ULL;
674 val = get_unaligned_be16(&caps->user) * 1000000ULL;
677 val = caps->user_source;
680 val = get_unaligned_be16(&caps->soft_min) * 1000000ULL;
686 return sysfs_emit(buf, "%llu\n", val);
689 static ssize_t occ_store_caps_user(struct device *dev,
690 struct device_attribute *attr,
691 const char *buf, size_t count)
695 unsigned long long value;
696 struct occ *occ = dev_get_drvdata(dev);
698 rc = kstrtoull(buf, 0, &value);
702 user_power_cap = div64_u64(value, 1000000ULL); /* microwatt to watt */
704 rc = occ_set_user_power_cap(occ, user_power_cap);
711 static ssize_t occ_show_extended(struct device *dev,
712 struct device_attribute *attr, char *buf)
715 struct extended_sensor *extn;
716 struct occ *occ = dev_get_drvdata(dev);
717 struct occ_sensors *sensors = &occ->sensors;
718 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
720 rc = occ_update_response(occ);
724 extn = ((struct extended_sensor *)sensors->extended.data) +
729 if (extn->flags & EXTN_FLAG_SENSOR_ID) {
730 rc = sysfs_emit(buf, "%u",
731 get_unaligned_be32(&extn->sensor_id));
733 rc = sysfs_emit(buf, "%4phN\n", extn->name);
737 rc = sysfs_emit(buf, "%02x\n", extn->flags);
740 rc = sysfs_emit(buf, "%6phN\n", extn->data);
750 * Some helper macros to make it easier to define an occ_attribute. Since these
751 * are dynamically allocated, we shouldn't use the existing kernel macros which
752 * stringify the name argument.
754 #define ATTR_OCC(_name, _mode, _show, _store) { \
757 .mode = VERIFY_OCTAL_PERMISSIONS(_mode), \
763 #define SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index) { \
764 .dev_attr = ATTR_OCC(_name, _mode, _show, _store), \
769 #define OCC_INIT_ATTR(_name, _mode, _show, _store, _nr, _index) \
770 ((struct sensor_device_attribute_2) \
771 SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index))
774 * Allocate and instatiate sensor_device_attribute_2s. It's most efficient to
775 * use our own instead of the built-in hwmon attribute types.
777 static int occ_setup_sensor_attrs(struct occ *occ)
779 unsigned int i, s, num_attrs = 0;
780 struct device *dev = occ->bus_dev;
781 struct occ_sensors *sensors = &occ->sensors;
782 struct occ_attribute *attr;
783 struct temp_sensor_2 *temp;
784 ssize_t (*show_temp)(struct device *, struct device_attribute *,
785 char *) = occ_show_temp_1;
786 ssize_t (*show_freq)(struct device *, struct device_attribute *,
787 char *) = occ_show_freq_1;
788 ssize_t (*show_power)(struct device *, struct device_attribute *,
789 char *) = occ_show_power_1;
790 ssize_t (*show_caps)(struct device *, struct device_attribute *,
791 char *) = occ_show_caps_1_2;
793 switch (sensors->temp.version) {
795 num_attrs += (sensors->temp.num_sensors * 2);
798 num_attrs += (sensors->temp.num_sensors * 4);
799 show_temp = occ_show_temp_2;
802 num_attrs += (sensors->temp.num_sensors * 5);
803 show_temp = occ_show_temp_10;
806 sensors->temp.num_sensors = 0;
809 switch (sensors->freq.version) {
811 show_freq = occ_show_freq_2;
814 num_attrs += (sensors->freq.num_sensors * 2);
817 sensors->freq.num_sensors = 0;
820 switch (sensors->power.version) {
822 show_power = occ_show_power_2;
825 num_attrs += (sensors->power.num_sensors * 4);
828 num_attrs += (sensors->power.num_sensors * 16);
829 show_power = occ_show_power_a0;
832 sensors->power.num_sensors = 0;
835 switch (sensors->caps.version) {
837 num_attrs += (sensors->caps.num_sensors * 7);
840 num_attrs += (sensors->caps.num_sensors * 8);
843 show_caps = occ_show_caps_3;
844 num_attrs += (sensors->caps.num_sensors * 9);
847 sensors->caps.num_sensors = 0;
850 switch (sensors->extended.version) {
852 num_attrs += (sensors->extended.num_sensors * 3);
855 sensors->extended.num_sensors = 0;
858 occ->attrs = devm_kzalloc(dev, sizeof(*occ->attrs) * num_attrs,
863 /* null-terminated list */
864 occ->group.attrs = devm_kzalloc(dev, sizeof(*occ->group.attrs) *
865 num_attrs + 1, GFP_KERNEL);
866 if (!occ->group.attrs)
871 for (i = 0; i < sensors->temp.num_sensors; ++i) {
873 temp = ((struct temp_sensor_2 *)sensors->temp.data) + i;
875 snprintf(attr->name, sizeof(attr->name), "temp%d_label", s);
876 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
880 if (sensors->temp.version == 2 &&
881 temp->fru_type == OCC_FRU_TYPE_VRM) {
882 snprintf(attr->name, sizeof(attr->name),
885 snprintf(attr->name, sizeof(attr->name),
889 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
893 if (sensors->temp.version > 1) {
894 snprintf(attr->name, sizeof(attr->name),
895 "temp%d_fru_type", s);
896 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
897 show_temp, NULL, 2, i);
900 snprintf(attr->name, sizeof(attr->name),
902 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
903 show_temp, NULL, 3, i);
906 if (sensors->temp.version == 0x10) {
907 snprintf(attr->name, sizeof(attr->name),
909 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
917 for (i = 0; i < sensors->freq.num_sensors; ++i) {
920 snprintf(attr->name, sizeof(attr->name), "freq%d_label", s);
921 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
925 snprintf(attr->name, sizeof(attr->name), "freq%d_input", s);
926 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
931 if (sensors->power.version == 0xA0) {
933 * Special case for many-attribute power sensor. Split it into
934 * a sensor number per power type, emulating several sensors.
936 for (i = 0; i < sensors->power.num_sensors; ++i) {
942 for (j = 0; j < 4; ++j) {
943 snprintf(attr->name, sizeof(attr->name),
945 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
950 snprintf(attr->name, sizeof(attr->name),
951 "power%d_average", s);
952 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
957 snprintf(attr->name, sizeof(attr->name),
958 "power%d_average_interval", s);
959 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
964 snprintf(attr->name, sizeof(attr->name),
966 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
975 s = (sensors->power.num_sensors * 4) + 1;
977 for (i = 0; i < sensors->power.num_sensors; ++i) {
980 snprintf(attr->name, sizeof(attr->name),
982 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
983 show_power, NULL, 0, i);
986 snprintf(attr->name, sizeof(attr->name),
987 "power%d_average", s);
988 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
989 show_power, NULL, 1, i);
992 snprintf(attr->name, sizeof(attr->name),
993 "power%d_average_interval", s);
994 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
995 show_power, NULL, 2, i);
998 snprintf(attr->name, sizeof(attr->name),
1000 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1001 show_power, NULL, 3, i);
1005 s = sensors->power.num_sensors + 1;
1008 if (sensors->caps.num_sensors >= 1) {
1009 snprintf(attr->name, sizeof(attr->name), "power%d_label", s);
1010 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1014 snprintf(attr->name, sizeof(attr->name), "power%d_cap", s);
1015 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1019 snprintf(attr->name, sizeof(attr->name), "power%d_input", s);
1020 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1024 snprintf(attr->name, sizeof(attr->name),
1025 "power%d_cap_not_redundant", s);
1026 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1030 snprintf(attr->name, sizeof(attr->name), "power%d_cap_max", s);
1031 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1035 snprintf(attr->name, sizeof(attr->name), "power%d_cap_min", s);
1036 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1040 snprintf(attr->name, sizeof(attr->name), "power%d_cap_user",
1042 attr->sensor = OCC_INIT_ATTR(attr->name, 0644, show_caps,
1043 occ_store_caps_user, 6, 0);
1046 if (sensors->caps.version > 1) {
1047 snprintf(attr->name, sizeof(attr->name),
1048 "power%d_cap_user_source", s);
1049 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1050 show_caps, NULL, 7, 0);
1053 if (sensors->caps.version > 2) {
1054 snprintf(attr->name, sizeof(attr->name),
1055 "power%d_cap_min_soft", s);
1056 attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
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_active(struct occ *occ, bool active)
1152 int rc = mutex_lock_interruptible(&occ->lock);
1163 occ->error_count = 0;
1168 dev_err(occ->bus_dev,
1169 "failed to get OCC poll response=%02x: %d\n",
1170 occ->resp.return_status, rc);
1175 occ->next_update = jiffies + OCC_UPDATE_FREQUENCY;
1176 occ_parse_poll_response(occ);
1178 rc = occ_setup_sensor_attrs(occ);
1180 dev_err(occ->bus_dev,
1181 "failed to setup sensor attrs: %d\n", rc);
1185 occ->hwmon = hwmon_device_register_with_groups(occ->bus_dev,
1188 if (IS_ERR(occ->hwmon)) {
1189 rc = PTR_ERR(occ->hwmon);
1191 dev_err(occ->bus_dev,
1192 "failed to register hwmon device: %d\n", rc);
1202 hwmon_device_unregister(occ->hwmon);
1203 occ->active = false;
1208 mutex_unlock(&occ->lock);
1212 int occ_setup(struct occ *occ)
1216 mutex_init(&occ->lock);
1217 occ->groups[0] = &occ->group;
1219 rc = occ_setup_sysfs(occ);
1221 dev_err(occ->bus_dev, "failed to setup sysfs: %d\n", rc);
1225 if (!device_property_read_bool(occ->bus_dev, "ibm,no-poll-on-init")) {
1226 rc = occ_active(occ, true);
1228 occ_shutdown_sysfs(occ);
1233 EXPORT_SYMBOL_GPL(occ_setup);
1235 void occ_shutdown(struct occ *occ)
1237 mutex_lock(&occ->lock);
1239 occ_shutdown_sysfs(occ);
1242 hwmon_device_unregister(occ->hwmon);
1245 mutex_unlock(&occ->lock);
1247 EXPORT_SYMBOL_GPL(occ_shutdown);
1250 MODULE_DESCRIPTION("Common OCC hwmon code");
1251 MODULE_LICENSE("GPL");