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
->last_update
+ OCC_UPDATE_FREQUENCY
)) {
222 occ
->last_update
= jiffies
;
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 snprintf(buf
, PAGE_SIZE
- 1, "%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 snprintf(buf
, PAGE_SIZE
- 1, "%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 snprintf(buf
, PAGE_SIZE
- 1, "%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 snprintf(buf
, PAGE_SIZE
- 1, "%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 snprintf(buf
, PAGE_SIZE
- 1, "%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 snprintf(buf
, PAGE_SIZE
- 1, "%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 snprintf(buf
, PAGE_SIZE
- 1, "%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 snprintf(buf
, PAGE_SIZE
- 1, "%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 snprintf(buf
, PAGE_SIZE
- 1, "%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 snprintf(buf
, PAGE_SIZE
- 1, "%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 snprintf(buf
, PAGE_SIZE
- 1, "%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 snprintf(buf
, PAGE_SIZE
- 1, "%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 snprintf(buf
, PAGE_SIZE
- 1, "%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 snprintf(buf
, PAGE_SIZE
- 1, "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 snprintf(buf
, PAGE_SIZE
- 1, "%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 snprintf(buf
, PAGE_SIZE
- 1, "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 snprintf(buf
, PAGE_SIZE
- 1, "%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
= snprintf(buf
, PAGE_SIZE
- 1, "%u",
737 get_unaligned_be32(&extn
->sensor_id
));
739 rc
= snprintf(buf
, PAGE_SIZE
- 1, "%02x%02x%02x%02x\n",
740 extn
->name
[0], extn
->name
[1],
741 extn
->name
[2], extn
->name
[3]);
744 rc
= snprintf(buf
, PAGE_SIZE
- 1, "%02x\n", extn
->flags
);
747 rc
= snprintf(buf
, PAGE_SIZE
- 1, "%02x%02x%02x%02x%02x%02x\n",
748 extn
->data
[0], extn
->data
[1], extn
->data
[2],
749 extn
->data
[3], extn
->data
[4], extn
->data
[5]);
759 * Some helper macros to make it easier to define an occ_attribute. Since these
760 * are dynamically allocated, we shouldn't use the existing kernel macros which
761 * stringify the name argument.
763 #define ATTR_OCC(_name, _mode, _show, _store) { \
766 .mode = VERIFY_OCTAL_PERMISSIONS(_mode), \
772 #define SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index) { \
773 .dev_attr = ATTR_OCC(_name, _mode, _show, _store), \
778 #define OCC_INIT_ATTR(_name, _mode, _show, _store, _nr, _index) \
779 ((struct sensor_device_attribute_2) \
780 SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index))
783 * Allocate and instatiate sensor_device_attribute_2s. It's most efficient to
784 * use our own instead of the built-in hwmon attribute types.
786 static int occ_setup_sensor_attrs(struct occ
*occ
)
788 unsigned int i
, s
, num_attrs
= 0;
789 struct device
*dev
= occ
->bus_dev
;
790 struct occ_sensors
*sensors
= &occ
->sensors
;
791 struct occ_attribute
*attr
;
792 struct temp_sensor_2
*temp
;
793 ssize_t (*show_temp
)(struct device
*, struct device_attribute
*,
794 char *) = occ_show_temp_1
;
795 ssize_t (*show_freq
)(struct device
*, struct device_attribute
*,
796 char *) = occ_show_freq_1
;
797 ssize_t (*show_power
)(struct device
*, struct device_attribute
*,
798 char *) = occ_show_power_1
;
799 ssize_t (*show_caps
)(struct device
*, struct device_attribute
*,
800 char *) = occ_show_caps_1_2
;
802 switch (sensors
->temp
.version
) {
804 num_attrs
+= (sensors
->temp
.num_sensors
* 2);
807 num_attrs
+= (sensors
->temp
.num_sensors
* 4);
808 show_temp
= occ_show_temp_2
;
811 num_attrs
+= (sensors
->temp
.num_sensors
* 5);
812 show_temp
= occ_show_temp_10
;
815 sensors
->temp
.num_sensors
= 0;
818 switch (sensors
->freq
.version
) {
820 show_freq
= occ_show_freq_2
;
823 num_attrs
+= (sensors
->freq
.num_sensors
* 2);
826 sensors
->freq
.num_sensors
= 0;
829 switch (sensors
->power
.version
) {
831 show_power
= occ_show_power_2
;
834 num_attrs
+= (sensors
->power
.num_sensors
* 4);
837 num_attrs
+= (sensors
->power
.num_sensors
* 16);
838 show_power
= occ_show_power_a0
;
841 sensors
->power
.num_sensors
= 0;
844 switch (sensors
->caps
.version
) {
846 num_attrs
+= (sensors
->caps
.num_sensors
* 7);
849 show_caps
= occ_show_caps_3
;
852 num_attrs
+= (sensors
->caps
.num_sensors
* 8);
855 sensors
->caps
.num_sensors
= 0;
858 switch (sensors
->extended
.version
) {
860 num_attrs
+= (sensors
->extended
.num_sensors
* 3);
863 sensors
->extended
.num_sensors
= 0;
866 occ
->attrs
= devm_kzalloc(dev
, sizeof(*occ
->attrs
) * num_attrs
,
871 /* null-terminated list */
872 occ
->group
.attrs
= devm_kzalloc(dev
, sizeof(*occ
->group
.attrs
) *
873 num_attrs
+ 1, GFP_KERNEL
);
874 if (!occ
->group
.attrs
)
879 for (i
= 0; i
< sensors
->temp
.num_sensors
; ++i
) {
881 temp
= ((struct temp_sensor_2
*)sensors
->temp
.data
) + i
;
883 snprintf(attr
->name
, sizeof(attr
->name
), "temp%d_label", s
);
884 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_temp
, NULL
,
888 if (sensors
->temp
.version
> 1 &&
889 temp
->fru_type
== OCC_FRU_TYPE_VRM
) {
890 snprintf(attr
->name
, sizeof(attr
->name
),
893 snprintf(attr
->name
, sizeof(attr
->name
),
897 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_temp
, NULL
,
901 if (sensors
->temp
.version
> 1) {
902 snprintf(attr
->name
, sizeof(attr
->name
),
903 "temp%d_fru_type", s
);
904 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
905 show_temp
, NULL
, 2, i
);
908 snprintf(attr
->name
, sizeof(attr
->name
),
910 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
911 show_temp
, NULL
, 3, i
);
914 if (sensors
->temp
.version
== 0x10) {
915 snprintf(attr
->name
, sizeof(attr
->name
),
917 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
925 for (i
= 0; i
< sensors
->freq
.num_sensors
; ++i
) {
928 snprintf(attr
->name
, sizeof(attr
->name
), "freq%d_label", s
);
929 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_freq
, NULL
,
933 snprintf(attr
->name
, sizeof(attr
->name
), "freq%d_input", s
);
934 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_freq
, NULL
,
939 if (sensors
->power
.version
== 0xA0) {
941 * Special case for many-attribute power sensor. Split it into
942 * a sensor number per power type, emulating several sensors.
944 for (i
= 0; i
< sensors
->power
.num_sensors
; ++i
) {
950 for (j
= 0; j
< 4; ++j
) {
951 snprintf(attr
->name
, sizeof(attr
->name
),
953 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
958 snprintf(attr
->name
, sizeof(attr
->name
),
959 "power%d_average", s
);
960 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
965 snprintf(attr
->name
, sizeof(attr
->name
),
966 "power%d_average_interval", s
);
967 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
972 snprintf(attr
->name
, sizeof(attr
->name
),
974 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
983 s
= (sensors
->power
.num_sensors
* 4) + 1;
985 for (i
= 0; i
< sensors
->power
.num_sensors
; ++i
) {
988 snprintf(attr
->name
, sizeof(attr
->name
),
990 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
991 show_power
, NULL
, 0, i
);
994 snprintf(attr
->name
, sizeof(attr
->name
),
995 "power%d_average", s
);
996 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
997 show_power
, NULL
, 1, i
);
1000 snprintf(attr
->name
, sizeof(attr
->name
),
1001 "power%d_average_interval", s
);
1002 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
1003 show_power
, NULL
, 2, i
);
1006 snprintf(attr
->name
, sizeof(attr
->name
),
1007 "power%d_input", s
);
1008 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
1009 show_power
, NULL
, 3, i
);
1013 s
= sensors
->power
.num_sensors
+ 1;
1016 if (sensors
->caps
.num_sensors
>= 1) {
1017 snprintf(attr
->name
, sizeof(attr
->name
), "power%d_label", s
);
1018 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_caps
, NULL
,
1022 snprintf(attr
->name
, sizeof(attr
->name
), "power%d_cap", s
);
1023 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_caps
, NULL
,
1027 snprintf(attr
->name
, sizeof(attr
->name
), "power%d_input", s
);
1028 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_caps
, NULL
,
1032 snprintf(attr
->name
, sizeof(attr
->name
),
1033 "power%d_cap_not_redundant", s
);
1034 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_caps
, NULL
,
1038 snprintf(attr
->name
, sizeof(attr
->name
), "power%d_cap_max", s
);
1039 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_caps
, NULL
,
1043 snprintf(attr
->name
, sizeof(attr
->name
), "power%d_cap_min", s
);
1044 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444, show_caps
, NULL
,
1048 snprintf(attr
->name
, sizeof(attr
->name
), "power%d_cap_user",
1050 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0644, show_caps
,
1051 occ_store_caps_user
, 6, 0);
1054 if (sensors
->caps
.version
> 1) {
1055 snprintf(attr
->name
, sizeof(attr
->name
),
1056 "power%d_cap_user_source", s
);
1057 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
1058 show_caps
, NULL
, 7, 0);
1063 for (i
= 0; i
< sensors
->extended
.num_sensors
; ++i
) {
1066 snprintf(attr
->name
, sizeof(attr
->name
), "extn%d_label", s
);
1067 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
1068 occ_show_extended
, NULL
, 0, i
);
1071 snprintf(attr
->name
, sizeof(attr
->name
), "extn%d_flags", s
);
1072 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
1073 occ_show_extended
, NULL
, 1, i
);
1076 snprintf(attr
->name
, sizeof(attr
->name
), "extn%d_input", s
);
1077 attr
->sensor
= OCC_INIT_ATTR(attr
->name
, 0444,
1078 occ_show_extended
, NULL
, 2, i
);
1082 /* put the sensors in the group */
1083 for (i
= 0; i
< num_attrs
; ++i
) {
1084 sysfs_attr_init(&occ
->attrs
[i
].sensor
.dev_attr
.attr
);
1085 occ
->group
.attrs
[i
] = &occ
->attrs
[i
].sensor
.dev_attr
.attr
;
1091 /* only need to do this once at startup, as OCC won't change sensors on us */
1092 static void occ_parse_poll_response(struct occ
*occ
)
1094 unsigned int i
, old_offset
, offset
= 0, size
= 0;
1095 struct occ_sensor
*sensor
;
1096 struct occ_sensors
*sensors
= &occ
->sensors
;
1097 struct occ_response
*resp
= &occ
->resp
;
1098 struct occ_poll_response
*poll
=
1099 (struct occ_poll_response
*)&resp
->data
[0];
1100 struct occ_poll_response_header
*header
= &poll
->header
;
1101 struct occ_sensor_data_block
*block
= &poll
->block
;
1103 dev_info(occ
->bus_dev
, "OCC found, code level: %.16s\n",
1104 header
->occ_code_level
);
1106 for (i
= 0; i
< header
->num_sensor_data_blocks
; ++i
) {
1107 block
= (struct occ_sensor_data_block
*)((u8
*)block
+ offset
);
1108 old_offset
= offset
;
1109 offset
= (block
->header
.num_sensors
*
1110 block
->header
.sensor_length
) + sizeof(block
->header
);
1113 /* validate all the length/size fields */
1114 if ((size
+ sizeof(*header
)) >= OCC_RESP_DATA_BYTES
) {
1115 dev_warn(occ
->bus_dev
, "exceeded response buffer\n");
1119 dev_dbg(occ
->bus_dev
, " %04x..%04x: %.4s (%d sensors)\n",
1120 old_offset
, offset
- 1, block
->header
.eye_catcher
,
1121 block
->header
.num_sensors
);
1123 /* match sensor block type */
1124 if (strncmp(block
->header
.eye_catcher
, "TEMP", 4) == 0)
1125 sensor
= &sensors
->temp
;
1126 else if (strncmp(block
->header
.eye_catcher
, "FREQ", 4) == 0)
1127 sensor
= &sensors
->freq
;
1128 else if (strncmp(block
->header
.eye_catcher
, "POWR", 4) == 0)
1129 sensor
= &sensors
->power
;
1130 else if (strncmp(block
->header
.eye_catcher
, "CAPS", 4) == 0)
1131 sensor
= &sensors
->caps
;
1132 else if (strncmp(block
->header
.eye_catcher
, "EXTN", 4) == 0)
1133 sensor
= &sensors
->extended
;
1135 dev_warn(occ
->bus_dev
, "sensor not supported %.4s\n",
1136 block
->header
.eye_catcher
);
1140 sensor
->num_sensors
= block
->header
.num_sensors
;
1141 sensor
->version
= block
->header
.sensor_format
;
1142 sensor
->data
= &block
->data
;
1145 dev_dbg(occ
->bus_dev
, "Max resp size: %u+%zd=%zd\n", size
,
1146 sizeof(*header
), size
+ sizeof(*header
));
1149 int occ_setup(struct occ
*occ
, const char *name
)
1153 mutex_init(&occ
->lock
);
1154 occ
->groups
[0] = &occ
->group
;
1156 /* no need to lock */
1158 if (rc
== -ESHUTDOWN
) {
1159 dev_info(occ
->bus_dev
, "host is not ready\n");
1161 } else if (rc
< 0) {
1162 dev_err(occ
->bus_dev
, "failed to get OCC poll response: %d\n",
1167 occ_parse_poll_response(occ
);
1169 rc
= occ_setup_sensor_attrs(occ
);
1171 dev_err(occ
->bus_dev
, "failed to setup sensor attrs: %d\n",
1176 occ
->hwmon
= devm_hwmon_device_register_with_groups(occ
->bus_dev
, name
,
1178 if (IS_ERR(occ
->hwmon
)) {
1179 rc
= PTR_ERR(occ
->hwmon
);
1180 dev_err(occ
->bus_dev
, "failed to register hwmon device: %d\n",
1185 rc
= occ_setup_sysfs(occ
);
1187 dev_err(occ
->bus_dev
, "failed to setup sysfs: %d\n", rc
);
1191 EXPORT_SYMBOL_GPL(occ_setup
);
1193 MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
1194 MODULE_DESCRIPTION("Common OCC hwmon code");
1195 MODULE_LICENSE("GPL");