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 freq_sensor_1
{
49 struct freq_sensor_2
{
54 struct power_sensor_1
{
61 struct power_sensor_2
{
71 struct power_sensor_data
{
77 struct power_sensor_data_and_time
{
84 struct power_sensor_a0
{
86 struct power_sensor_data_and_time system
;
88 struct power_sensor_data_and_time proc
;
89 struct power_sensor_data vdd
;
90 struct power_sensor_data vdn
;
93 struct caps_sensor_2
{
103 struct caps_sensor_3
{
114 struct extended_sensor
{
124 static int occ_poll(struct occ
*occ
)
127 u16 checksum
= occ
->poll_cmd_data
+ occ
->seq_no
+ 1;
129 struct occ_poll_response_header
*header
;
132 cmd
[0] = occ
->seq_no
++; /* sequence number */
133 cmd
[1] = 0; /* cmd type */
134 cmd
[2] = 0; /* data length msb */
135 cmd
[3] = 1; /* data length lsb */
136 cmd
[4] = occ
->poll_cmd_data
; /* data */
137 cmd
[5] = checksum
>> 8; /* checksum msb */
138 cmd
[6] = checksum
& 0xFF; /* checksum lsb */
141 /* mutex should already be locked if necessary */
142 rc
= occ
->send_cmd(occ
, cmd
);
144 occ
->last_error
= rc
;
145 if (occ
->error_count
++ > OCC_ERROR_COUNT_THRESHOLD
)
151 /* clear error since communication was successful */
152 occ
->error_count
= 0;
156 /* check for safe state */
157 header
= (struct occ_poll_response_header
*)occ
->resp
.data
;
158 if (header
->occ_state
== OCC_STATE_SAFE
) {
159 if (occ
->last_safe
) {
160 if (time_after(jiffies
,
161 occ
->last_safe
+ OCC_SAFE_TIMEOUT
))
162 occ
->error
= -EHOSTDOWN
;
164 occ
->last_safe
= jiffies
;
171 occ_sysfs_poll_done(occ
);
175 static int occ_set_user_power_cap(struct occ
*occ
, u16 user_power_cap
)
180 __be16 user_power_cap_be
= cpu_to_be16(user_power_cap
);
187 memcpy(&cmd
[4], &user_power_cap_be
, 2);
189 checksum
+= cmd
[4] + cmd
[5];
190 cmd
[6] = checksum
>> 8;
191 cmd
[7] = checksum
& 0xFF;
193 rc
= mutex_lock_interruptible(&occ
->lock
);
197 rc
= occ
->send_cmd(occ
, cmd
);
199 mutex_unlock(&occ
->lock
);
204 int occ_update_response(struct occ
*occ
)
206 int rc
= mutex_lock_interruptible(&occ
->lock
);
211 /* limit the maximum rate of polling the OCC */
212 if (time_after(jiffies
, occ
->last_update
+ OCC_UPDATE_FREQUENCY
)) {
214 occ
->last_update
= jiffies
;
216 rc
= occ
->last_error
;
219 mutex_unlock(&occ
->lock
);
223 static ssize_t
occ_show_temp_1(struct device
*dev
,
224 struct device_attribute
*attr
, char *buf
)
228 struct temp_sensor_1
*temp
;
229 struct occ
*occ
= dev_get_drvdata(dev
);
230 struct occ_sensors
*sensors
= &occ
->sensors
;
231 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
233 rc
= occ_update_response(occ
);
237 temp
= ((struct temp_sensor_1
*)sensors
->temp
.data
) + sattr
->index
;
241 val
= get_unaligned_be16(&temp
->sensor_id
);
245 * If a sensor reading has expired and couldn't be refreshed,
246 * OCC returns 0xFFFF for that sensor.
248 if (temp
->value
== 0xFFFF)
250 val
= get_unaligned_be16(&temp
->value
) * 1000;
256 return snprintf(buf
, PAGE_SIZE
- 1, "%u\n", val
);
259 static ssize_t
occ_show_temp_2(struct device
*dev
,
260 struct device_attribute
*attr
, char *buf
)
264 struct temp_sensor_2
*temp
;
265 struct occ
*occ
= dev_get_drvdata(dev
);
266 struct occ_sensors
*sensors
= &occ
->sensors
;
267 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
269 rc
= occ_update_response(occ
);
273 temp
= ((struct temp_sensor_2
*)sensors
->temp
.data
) + sattr
->index
;
277 val
= get_unaligned_be32(&temp
->sensor_id
);
281 if (val
== OCC_TEMP_SENSOR_FAULT
)
285 * VRM doesn't return temperature, only alarm bit. This
286 * attribute maps to tempX_alarm instead of tempX_input for
289 if (temp
->fru_type
!= OCC_FRU_TYPE_VRM
) {
290 /* sensor not ready */
298 val
= temp
->fru_type
;
301 val
= temp
->value
== OCC_TEMP_SENSOR_FAULT
;
307 return snprintf(buf
, PAGE_SIZE
- 1, "%u\n", val
);
310 static ssize_t
occ_show_freq_1(struct device
*dev
,
311 struct device_attribute
*attr
, char *buf
)
315 struct freq_sensor_1
*freq
;
316 struct occ
*occ
= dev_get_drvdata(dev
);
317 struct occ_sensors
*sensors
= &occ
->sensors
;
318 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
320 rc
= occ_update_response(occ
);
324 freq
= ((struct freq_sensor_1
*)sensors
->freq
.data
) + sattr
->index
;
328 val
= get_unaligned_be16(&freq
->sensor_id
);
331 val
= get_unaligned_be16(&freq
->value
);
337 return snprintf(buf
, PAGE_SIZE
- 1, "%u\n", val
);
340 static ssize_t
occ_show_freq_2(struct device
*dev
,
341 struct device_attribute
*attr
, char *buf
)
345 struct freq_sensor_2
*freq
;
346 struct occ
*occ
= dev_get_drvdata(dev
);
347 struct occ_sensors
*sensors
= &occ
->sensors
;
348 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
350 rc
= occ_update_response(occ
);
354 freq
= ((struct freq_sensor_2
*)sensors
->freq
.data
) + sattr
->index
;
358 val
= get_unaligned_be32(&freq
->sensor_id
);
361 val
= get_unaligned_be16(&freq
->value
);
367 return snprintf(buf
, PAGE_SIZE
- 1, "%u\n", val
);
370 static ssize_t
occ_show_power_1(struct device
*dev
,
371 struct device_attribute
*attr
, char *buf
)
375 struct power_sensor_1
*power
;
376 struct occ
*occ
= dev_get_drvdata(dev
);
377 struct occ_sensors
*sensors
= &occ
->sensors
;
378 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
380 rc
= occ_update_response(occ
);
384 power
= ((struct power_sensor_1
*)sensors
->power
.data
) + sattr
->index
;
388 val
= get_unaligned_be16(&power
->sensor_id
);
391 val
= get_unaligned_be32(&power
->accumulator
) /
392 get_unaligned_be32(&power
->update_tag
);
396 val
= (u64
)get_unaligned_be32(&power
->update_tag
) *
397 occ
->powr_sample_time_us
;
400 val
= get_unaligned_be16(&power
->value
) * 1000000ULL;
406 return snprintf(buf
, PAGE_SIZE
- 1, "%llu\n", val
);
409 static u64
occ_get_powr_avg(u64
*accum
, u32
*samples
)
411 u64 divisor
= get_unaligned_be32(samples
);
413 return (divisor
== 0) ? 0 :
414 div64_u64(get_unaligned_be64(accum
) * 1000000ULL, divisor
);
417 static ssize_t
occ_show_power_2(struct device
*dev
,
418 struct device_attribute
*attr
, char *buf
)
422 struct power_sensor_2
*power
;
423 struct occ
*occ
= dev_get_drvdata(dev
);
424 struct occ_sensors
*sensors
= &occ
->sensors
;
425 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
427 rc
= occ_update_response(occ
);
431 power
= ((struct power_sensor_2
*)sensors
->power
.data
) + sattr
->index
;
435 return snprintf(buf
, PAGE_SIZE
- 1, "%u_%u_%u\n",
436 get_unaligned_be32(&power
->sensor_id
),
437 power
->function_id
, power
->apss_channel
);
439 val
= occ_get_powr_avg(&power
->accumulator
,
443 val
= (u64
)get_unaligned_be32(&power
->update_tag
) *
444 occ
->powr_sample_time_us
;
447 val
= get_unaligned_be16(&power
->value
) * 1000000ULL;
453 return snprintf(buf
, PAGE_SIZE
- 1, "%llu\n", val
);
456 static ssize_t
occ_show_power_a0(struct device
*dev
,
457 struct device_attribute
*attr
, char *buf
)
461 struct power_sensor_a0
*power
;
462 struct occ
*occ
= dev_get_drvdata(dev
);
463 struct occ_sensors
*sensors
= &occ
->sensors
;
464 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
466 rc
= occ_update_response(occ
);
470 power
= ((struct power_sensor_a0
*)sensors
->power
.data
) + sattr
->index
;
474 return snprintf(buf
, PAGE_SIZE
- 1, "%u_system\n",
475 get_unaligned_be32(&power
->sensor_id
));
477 val
= occ_get_powr_avg(&power
->system
.accumulator
,
478 &power
->system
.update_tag
);
481 val
= (u64
)get_unaligned_be32(&power
->system
.update_tag
) *
482 occ
->powr_sample_time_us
;
485 val
= get_unaligned_be16(&power
->system
.value
) * 1000000ULL;
488 return snprintf(buf
, PAGE_SIZE
- 1, "%u_proc\n",
489 get_unaligned_be32(&power
->sensor_id
));
491 val
= occ_get_powr_avg(&power
->proc
.accumulator
,
492 &power
->proc
.update_tag
);
495 val
= (u64
)get_unaligned_be32(&power
->proc
.update_tag
) *
496 occ
->powr_sample_time_us
;
499 val
= get_unaligned_be16(&power
->proc
.value
) * 1000000ULL;
502 return snprintf(buf
, PAGE_SIZE
- 1, "%u_vdd\n",
503 get_unaligned_be32(&power
->sensor_id
));
505 val
= occ_get_powr_avg(&power
->vdd
.accumulator
,
506 &power
->vdd
.update_tag
);
509 val
= (u64
)get_unaligned_be32(&power
->vdd
.update_tag
) *
510 occ
->powr_sample_time_us
;
513 val
= get_unaligned_be16(&power
->vdd
.value
) * 1000000ULL;
516 return snprintf(buf
, PAGE_SIZE
- 1, "%u_vdn\n",
517 get_unaligned_be32(&power
->sensor_id
));
519 val
= occ_get_powr_avg(&power
->vdn
.accumulator
,
520 &power
->vdn
.update_tag
);
523 val
= (u64
)get_unaligned_be32(&power
->vdn
.update_tag
) *
524 occ
->powr_sample_time_us
;
527 val
= get_unaligned_be16(&power
->vdn
.value
) * 1000000ULL;
533 return snprintf(buf
, PAGE_SIZE
- 1, "%llu\n", val
);
536 static ssize_t
occ_show_caps_1_2(struct device
*dev
,
537 struct device_attribute
*attr
, char *buf
)
541 struct caps_sensor_2
*caps
;
542 struct occ
*occ
= dev_get_drvdata(dev
);
543 struct occ_sensors
*sensors
= &occ
->sensors
;
544 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
546 rc
= occ_update_response(occ
);
550 caps
= ((struct caps_sensor_2
*)sensors
->caps
.data
) + sattr
->index
;
554 return snprintf(buf
, PAGE_SIZE
- 1, "system\n");
556 val
= get_unaligned_be16(&caps
->cap
) * 1000000ULL;
559 val
= get_unaligned_be16(&caps
->system_power
) * 1000000ULL;
562 val
= get_unaligned_be16(&caps
->n_cap
) * 1000000ULL;
565 val
= get_unaligned_be16(&caps
->max
) * 1000000ULL;
568 val
= get_unaligned_be16(&caps
->min
) * 1000000ULL;
571 val
= get_unaligned_be16(&caps
->user
) * 1000000ULL;
574 if (occ
->sensors
.caps
.version
== 1)
577 val
= caps
->user_source
;
583 return snprintf(buf
, PAGE_SIZE
- 1, "%llu\n", val
);
586 static ssize_t
occ_show_caps_3(struct device
*dev
,
587 struct device_attribute
*attr
, char *buf
)
591 struct caps_sensor_3
*caps
;
592 struct occ
*occ
= dev_get_drvdata(dev
);
593 struct occ_sensors
*sensors
= &occ
->sensors
;
594 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
596 rc
= occ_update_response(occ
);
600 caps
= ((struct caps_sensor_3
*)sensors
->caps
.data
) + sattr
->index
;
604 return snprintf(buf
, PAGE_SIZE
- 1, "system\n");
606 val
= get_unaligned_be16(&caps
->cap
) * 1000000ULL;
609 val
= get_unaligned_be16(&caps
->system_power
) * 1000000ULL;
612 val
= get_unaligned_be16(&caps
->n_cap
) * 1000000ULL;
615 val
= get_unaligned_be16(&caps
->max
) * 1000000ULL;
618 val
= get_unaligned_be16(&caps
->hard_min
) * 1000000ULL;
621 val
= get_unaligned_be16(&caps
->user
) * 1000000ULL;
624 val
= caps
->user_source
;
630 return snprintf(buf
, PAGE_SIZE
- 1, "%llu\n", val
);
633 static ssize_t
occ_store_caps_user(struct device
*dev
,
634 struct device_attribute
*attr
,
635 const char *buf
, size_t count
)
639 unsigned long long value
;
640 struct occ
*occ
= dev_get_drvdata(dev
);
642 rc
= kstrtoull(buf
, 0, &value
);
646 user_power_cap
= div64_u64(value
, 1000000ULL); /* microwatt to watt */
648 rc
= occ_set_user_power_cap(occ
, user_power_cap
);
655 static ssize_t
occ_show_extended(struct device
*dev
,
656 struct device_attribute
*attr
, char *buf
)
659 struct extended_sensor
*extn
;
660 struct occ
*occ
= dev_get_drvdata(dev
);
661 struct occ_sensors
*sensors
= &occ
->sensors
;
662 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
664 rc
= occ_update_response(occ
);
668 extn
= ((struct extended_sensor
*)sensors
->extended
.data
) +
673 if (extn
->flags
& EXTN_FLAG_SENSOR_ID
)
674 rc
= snprintf(buf
, PAGE_SIZE
- 1, "%u",
675 get_unaligned_be32(&extn
->sensor_id
));
677 rc
= snprintf(buf
, PAGE_SIZE
- 1, "%02x%02x%02x%02x\n",
678 extn
->name
[0], extn
->name
[1],
679 extn
->name
[2], extn
->name
[3]);
682 rc
= snprintf(buf
, PAGE_SIZE
- 1, "%02x\n", extn
->flags
);
685 rc
= snprintf(buf
, PAGE_SIZE
- 1, "%02x%02x%02x%02x%02x%02x\n",
686 extn
->data
[0], extn
->data
[1], extn
->data
[2],
687 extn
->data
[3], extn
->data
[4], extn
->data
[5]);
697 * Some helper macros to make it easier to define an occ_attribute. Since these
698 * are dynamically allocated, we shouldn't use the existing kernel macros which
699 * stringify the name argument.
701 #define ATTR_OCC(_name, _mode, _show, _store) { \
704 .mode = VERIFY_OCTAL_PERMISSIONS(_mode), \
710 #define SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index) { \
711 .dev_attr = ATTR_OCC(_name, _mode, _show, _store), \
716 #define OCC_INIT_ATTR(_name, _mode, _show, _store, _nr, _index) \
717 ((struct sensor_device_attribute_2) \
718 SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index))
721 * Allocate and instatiate sensor_device_attribute_2s. It's most efficient to
722 * use our own instead of the built-in hwmon attribute types.
724 static int occ_setup_sensor_attrs(struct occ
*occ
)
726 unsigned int i
, s
, num_attrs
= 0;
727 struct device
*dev
= occ
->bus_dev
;
728 struct occ_sensors
*sensors
= &occ
->sensors
;
729 struct occ_attribute
*attr
;
730 struct temp_sensor_2
*temp
;
731 ssize_t (*show_temp
)(struct device
*, struct device_attribute
*,
732 char *) = occ_show_temp_1
;
733 ssize_t (*show_freq
)(struct device
*, struct device_attribute
*,
734 char *) = occ_show_freq_1
;
735 ssize_t (*show_power
)(struct device
*, struct device_attribute
*,
736 char *) = occ_show_power_1
;
737 ssize_t (*show_caps
)(struct device
*, struct device_attribute
*,
738 char *) = occ_show_caps_1_2
;
740 switch (sensors
->temp
.version
) {
742 num_attrs
+= (sensors
->temp
.num_sensors
* 2);
745 num_attrs
+= (sensors
->temp
.num_sensors
* 4);
746 show_temp
= occ_show_temp_2
;
749 sensors
->temp
.num_sensors
= 0;
752 switch (sensors
->freq
.version
) {
754 show_freq
= occ_show_freq_2
;
757 num_attrs
+= (sensors
->freq
.num_sensors
* 2);
760 sensors
->freq
.num_sensors
= 0;
763 switch (sensors
->power
.version
) {
765 show_power
= occ_show_power_2
;
768 num_attrs
+= (sensors
->power
.num_sensors
* 4);
771 num_attrs
+= (sensors
->power
.num_sensors
* 16);
772 show_power
= occ_show_power_a0
;
775 sensors
->power
.num_sensors
= 0;
778 switch (sensors
->caps
.version
) {
780 num_attrs
+= (sensors
->caps
.num_sensors
* 7);
783 show_caps
= occ_show_caps_3
;
786 num_attrs
+= (sensors
->caps
.num_sensors
* 8);
789 sensors
->caps
.num_sensors
= 0;
792 switch (sensors
->extended
.version
) {
794 num_attrs
+= (sensors
->extended
.num_sensors
* 3);
797 sensors
->extended
.num_sensors
= 0;
800 occ
->attrs
= devm_kzalloc(dev
, sizeof(*occ
->attrs
) * num_attrs
,
805 /* null-terminated list */
806 occ
->group
.attrs
= devm_kzalloc(dev
, sizeof(*occ
->group
.attrs
) *
807 num_attrs
+ 1, GFP_KERNEL
);
808 if (!occ
->group
.attrs
)
813 for (i
= 0; i
< sensors
->temp
.num_sensors
; ++i
) {
815 temp
= ((struct temp_sensor_2
*)sensors
->temp
.data
) + i
;
817 snprintf(attr
->name
, sizeof(attr
->name
), "temp%d_label", s
);
818 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_temp
, NULL
,
822 if (sensors
->temp
.version
> 1 &&
823 temp
->fru_type
== OCC_FRU_TYPE_VRM
) {
824 snprintf(attr
->name
, sizeof(attr
->name
),
827 snprintf(attr
->name
, sizeof(attr
->name
),
831 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_temp
, NULL
,
835 if (sensors
->temp
.version
> 1) {
836 snprintf(attr
->name
, sizeof(attr
->name
),
837 "temp%d_fru_type", s
);
838 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
839 show_temp
, NULL
, 2, i
);
842 snprintf(attr
->name
, sizeof(attr
->name
),
844 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
845 show_temp
, NULL
, 3, i
);
850 for (i
= 0; i
< sensors
->freq
.num_sensors
; ++i
) {
853 snprintf(attr
->name
, sizeof(attr
->name
), "freq%d_label", s
);
854 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_freq
, NULL
,
858 snprintf(attr
->name
, sizeof(attr
->name
), "freq%d_input", s
);
859 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_freq
, NULL
,
864 if (sensors
->power
.version
== 0xA0) {
866 * Special case for many-attribute power sensor. Split it into
867 * a sensor number per power type, emulating several sensors.
869 for (i
= 0; i
< sensors
->power
.num_sensors
; ++i
) {
875 for (j
= 0; j
< 4; ++j
) {
876 snprintf(attr
->name
, sizeof(attr
->name
),
878 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
883 snprintf(attr
->name
, sizeof(attr
->name
),
884 "power%d_average", s
);
885 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
890 snprintf(attr
->name
, sizeof(attr
->name
),
891 "power%d_average_interval", s
);
892 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
897 snprintf(attr
->name
, sizeof(attr
->name
),
899 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
908 s
= (sensors
->power
.num_sensors
* 4) + 1;
910 for (i
= 0; i
< sensors
->power
.num_sensors
; ++i
) {
913 snprintf(attr
->name
, sizeof(attr
->name
),
915 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
916 show_power
, NULL
, 0, i
);
919 snprintf(attr
->name
, sizeof(attr
->name
),
920 "power%d_average", s
);
921 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
922 show_power
, NULL
, 1, i
);
925 snprintf(attr
->name
, sizeof(attr
->name
),
926 "power%d_average_interval", s
);
927 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
928 show_power
, NULL
, 2, i
);
931 snprintf(attr
->name
, sizeof(attr
->name
),
933 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
934 show_power
, NULL
, 3, i
);
938 s
= sensors
->power
.num_sensors
+ 1;
941 if (sensors
->caps
.num_sensors
>= 1) {
942 snprintf(attr
->name
, sizeof(attr
->name
), "power%d_label", s
);
943 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_caps
, NULL
,
947 snprintf(attr
->name
, sizeof(attr
->name
), "power%d_cap", s
);
948 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_caps
, NULL
,
952 snprintf(attr
->name
, sizeof(attr
->name
), "power%d_input", s
);
953 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_caps
, NULL
,
957 snprintf(attr
->name
, sizeof(attr
->name
),
958 "power%d_cap_not_redundant", s
);
959 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_caps
, NULL
,
963 snprintf(attr
->name
, sizeof(attr
->name
), "power%d_cap_max", s
);
964 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_caps
, NULL
,
968 snprintf(attr
->name
, sizeof(attr
->name
), "power%d_cap_min", s
);
969 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_caps
, NULL
,
973 snprintf(attr
->name
, sizeof(attr
->name
), "power%d_cap_user",
975 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0644, show_caps
,
976 occ_store_caps_user
, 6, 0);
979 if (sensors
->caps
.version
> 1) {
980 snprintf(attr
->name
, sizeof(attr
->name
),
981 "power%d_cap_user_source", s
);
982 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
983 show_caps
, NULL
, 7, 0);
988 for (i
= 0; i
< sensors
->extended
.num_sensors
; ++i
) {
991 snprintf(attr
->name
, sizeof(attr
->name
), "extn%d_label", s
);
992 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
993 occ_show_extended
, NULL
, 0, i
);
996 snprintf(attr
->name
, sizeof(attr
->name
), "extn%d_flags", s
);
997 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
998 occ_show_extended
, NULL
, 1, i
);
1001 snprintf(attr
->name
, sizeof(attr
->name
), "extn%d_input", s
);
1002 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
1003 occ_show_extended
, NULL
, 2, i
);
1007 /* put the sensors in the group */
1008 for (i
= 0; i
< num_attrs
; ++i
) {
1009 sysfs_attr_init(&occ
->attrs
[i
].sensor
.dev_attr
.attr
);
1010 occ
->group
.attrs
[i
] = &occ
->attrs
[i
].sensor
.dev_attr
.attr
;
1016 /* only need to do this once at startup, as OCC won't change sensors on us */
1017 static void occ_parse_poll_response(struct occ
*occ
)
1019 unsigned int i
, old_offset
, offset
= 0, size
= 0;
1020 struct occ_sensor
*sensor
;
1021 struct occ_sensors
*sensors
= &occ
->sensors
;
1022 struct occ_response
*resp
= &occ
->resp
;
1023 struct occ_poll_response
*poll
=
1024 (struct occ_poll_response
*)&resp
->data
[0];
1025 struct occ_poll_response_header
*header
= &poll
->header
;
1026 struct occ_sensor_data_block
*block
= &poll
->block
;
1028 dev_info(occ
->bus_dev
, "OCC found, code level: %.16s\n",
1029 header
->occ_code_level
);
1031 for (i
= 0; i
< header
->num_sensor_data_blocks
; ++i
) {
1032 block
= (struct occ_sensor_data_block
*)((u8
*)block
+ offset
);
1033 old_offset
= offset
;
1034 offset
= (block
->header
.num_sensors
*
1035 block
->header
.sensor_length
) + sizeof(block
->header
);
1038 /* validate all the length/size fields */
1039 if ((size
+ sizeof(*header
)) >= OCC_RESP_DATA_BYTES
) {
1040 dev_warn(occ
->bus_dev
, "exceeded response buffer\n");
1044 dev_dbg(occ
->bus_dev
, " %04x..%04x: %.4s (%d sensors)\n",
1045 old_offset
, offset
- 1, block
->header
.eye_catcher
,
1046 block
->header
.num_sensors
);
1048 /* match sensor block type */
1049 if (strncmp(block
->header
.eye_catcher
, "TEMP", 4) == 0)
1050 sensor
= &sensors
->temp
;
1051 else if (strncmp(block
->header
.eye_catcher
, "FREQ", 4) == 0)
1052 sensor
= &sensors
->freq
;
1053 else if (strncmp(block
->header
.eye_catcher
, "POWR", 4) == 0)
1054 sensor
= &sensors
->power
;
1055 else if (strncmp(block
->header
.eye_catcher
, "CAPS", 4) == 0)
1056 sensor
= &sensors
->caps
;
1057 else if (strncmp(block
->header
.eye_catcher
, "EXTN", 4) == 0)
1058 sensor
= &sensors
->extended
;
1060 dev_warn(occ
->bus_dev
, "sensor not supported %.4s\n",
1061 block
->header
.eye_catcher
);
1065 sensor
->num_sensors
= block
->header
.num_sensors
;
1066 sensor
->version
= block
->header
.sensor_format
;
1067 sensor
->data
= &block
->data
;
1070 dev_dbg(occ
->bus_dev
, "Max resp size: %u+%zd=%zd\n", size
,
1071 sizeof(*header
), size
+ sizeof(*header
));
1074 int occ_setup(struct occ
*occ
, const char *name
)
1078 mutex_init(&occ
->lock
);
1079 occ
->groups
[0] = &occ
->group
;
1081 /* no need to lock */
1083 if (rc
== -ESHUTDOWN
) {
1084 dev_info(occ
->bus_dev
, "host is not ready\n");
1086 } else if (rc
< 0) {
1087 dev_err(occ
->bus_dev
, "failed to get OCC poll response: %d\n",
1092 occ_parse_poll_response(occ
);
1094 rc
= occ_setup_sensor_attrs(occ
);
1096 dev_err(occ
->bus_dev
, "failed to setup sensor attrs: %d\n",
1101 occ
->hwmon
= devm_hwmon_device_register_with_groups(occ
->bus_dev
, name
,
1103 if (IS_ERR(occ
->hwmon
)) {
1104 rc
= PTR_ERR(occ
->hwmon
);
1105 dev_err(occ
->bus_dev
, "failed to register hwmon device: %d\n",
1110 rc
= occ_setup_sysfs(occ
);
1112 dev_err(occ
->bus_dev
, "failed to setup sysfs: %d\n", rc
);
1116 EXPORT_SYMBOL_GPL(occ_setup
);
1118 MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
1119 MODULE_DESCRIPTION("Common OCC hwmon code");
1120 MODULE_LICENSE("GPL");